10bb73c79a04bc9c68c0a0e8f8cd75ad5ee92203
[tridge/openchange.git] / branches / plugfest / libmapi++ / src / folder.cpp
1 /*
2    libmapi C++ Wrapper
3    Folder 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++/folder.h>
22
23 namespace libmapipp {
24
25 folder::message_container_type folder::fetch_messages() throw(mapi_exception)
26 {
27         uint32_t        contents_table_row_count = 0;
28         mapi_object_t   contents_table;
29
30         mapi_object_init(&contents_table);
31         if (GetContentsTable(&m_object, &contents_table, 0, &contents_table_row_count) != MAPI_E_SUCCESS) {
32                 mapi_object_release(&contents_table);
33                 throw mapi_exception(GetLastError(), "folder::fetch_messages : GetContentsTable");
34         }
35
36         SPropTagArray* property_tag_array = set_SPropTagArray(m_session.get_memory_ctx(), 0x2, PR_FID,
37                                                                                                PR_MID);
38
39         if (SetColumns(&contents_table, property_tag_array) != MAPI_E_SUCCESS) {
40                 MAPIFreeBuffer(property_tag_array);
41                 mapi_object_release(&contents_table);
42                 throw mapi_exception(GetLastError(), "folder::fetch_messages : SetColumns");
43         }
44
45         MAPIFreeBuffer(property_tag_array);
46
47         uint32_t rows_to_read = contents_table_row_count;
48         SRowSet  row_set;
49
50         message_container_type message_container;
51         message_container.reserve(contents_table_row_count);
52
53         while( (QueryRows(&contents_table, rows_to_read, TBL_ADVANCE, &row_set) == MAPI_E_SUCCESS) && row_set.cRows) {
54                 rows_to_read -= row_set.cRows;
55                 for (unsigned int i = 0; i < row_set.cRows; ++i) {
56                         try {
57                                 message_container.push_back(message_shared_ptr(new message(m_session,
58                                                                                            m_id,
59                                                                                            row_set.aRow[i].lpProps[1].value.d)));
60                         } catch(mapi_exception e) {
61                                 mapi_object_release(&contents_table);
62                                 throw;
63                         }
64                 }
65         }
66
67         mapi_object_release(&contents_table);
68
69         return message_container;
70 }
71
72 folder::hierarchy_container_type folder::fetch_hierarchy() throw(mapi_exception)
73 {
74         mapi_object_t   hierarchy_table;
75         uint32_t        hierarchy_table_row_count = 0;
76
77         mapi_object_init(&hierarchy_table);
78         if (GetHierarchyTable(&m_object, &hierarchy_table, 0, &hierarchy_table_row_count) != MAPI_E_SUCCESS) {
79                 mapi_object_release(&hierarchy_table);
80                 throw mapi_exception(GetLastError(), "folder::fetch_hierarchy : GetHierarchyTable");
81         }
82
83         SPropTagArray* property_tag_array = set_SPropTagArray(m_session.get_memory_ctx(), 0x1, PR_FID);
84
85         if (SetColumns(&hierarchy_table, property_tag_array)) {
86                 MAPIFreeBuffer(property_tag_array);
87                 mapi_object_release(&hierarchy_table);
88                 throw mapi_exception(GetLastError(), "folder::fetch_hierarchy : SetColumns");
89         }
90
91         MAPIFreeBuffer(property_tag_array);
92
93         uint32_t rows_to_read = hierarchy_table_row_count;
94         SRowSet  row_set;
95
96         hierarchy_container_type hierarchy_container;
97         hierarchy_container.reserve(hierarchy_table_row_count);
98
99         while( (QueryRows(&hierarchy_table, rows_to_read, TBL_ADVANCE, &row_set) == MAPI_E_SUCCESS) && row_set.cRows) {
100                 rows_to_read -= row_set.cRows;
101                 for (unsigned int i = 0; i < row_set.cRows; ++i) {
102                         try {
103                                 hierarchy_container.push_back(folder_shared_ptr(new folder(*this,
104                                                               row_set.aRow[i].lpProps[0].value.d)));
105                         } catch(mapi_exception e) {
106                                 mapi_object_release(&hierarchy_table);
107                                 throw;
108                         }
109                 }
110         }
111
112         mapi_object_release(&hierarchy_table);
113
114         return hierarchy_container;
115 }
116
117 } // namespace libmapipp
118