fe764806e7e625c859d24aa84533ffed19cf2f6e
[tridge/openchange.git] / branches / plugfest / libmapi++ / src / message.cpp
1 /*
2    libmapi C++ Wrapper
3    Message class implementation
4
5    Copyright (C) Alan Alvarez 2008.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <libmapi++/attachment.h>
22
23 namespace libmapipp {
24
25 message::attachment_container_type message::fetch_attachments()
26 {
27         mapi_object_t   attachment_table;
28
29         mapi_object_init(&attachment_table);
30         if (GetAttachmentTable(&m_object, &attachment_table) != MAPI_E_SUCCESS) {
31                 mapi_object_release(&attachment_table);
32                 throw mapi_exception(GetLastError(), "message::fetch_attachments : GetAttachmentTable");
33         }
34
35         SPropTagArray* property_tag_array = set_SPropTagArray(m_session.get_memory_ctx(), 0x1, PR_ATTACH_NUM);
36
37         if (SetColumns(&attachment_table, property_tag_array) != MAPI_E_SUCCESS) {
38                 MAPIFreeBuffer(property_tag_array);
39                 mapi_object_release(&attachment_table);
40                 throw mapi_exception(GetLastError(), "message::fetch_attachments : SetColumns");
41         }
42
43         MAPIFreeBuffer(property_tag_array);
44
45         SRowSet  row_set;
46         attachment_container_type attachment_container;
47
48         while( (QueryRows(&attachment_table, 0x32, TBL_ADVANCE, &row_set) == MAPI_E_SUCCESS) && row_set.cRows) {
49                 for (unsigned int i = 0; i < row_set.cRows; ++i) {
50                         try {
51                                 attachment_container.push_back(attachment_shared_ptr(new attachment(*this, row_set.aRow[i].lpProps[0].value.l)));
52                         }
53                         catch(mapi_exception e) {
54                                 mapi_object_release(&attachment_table);
55                                 throw;
56                         }
57                 }
58         }
59         mapi_object_release(&attachment_table);
60
61         return attachment_container;
62 }
63
64 } // namespace libmapipp