Fix OpenChange server code and access to Samba4 databases.
[OpenChange-git-clone.git] / libmapi++ / folder.h
blob7516a2fe471c4331ed6d76a5c793b765a99c7783
1 /*
2 libmapi C++ Wrapper
3 Folder Class
5 Copyright (C) Alan Alvarez 2008.
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.
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.
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/>.
21 #ifndef LIBMAPIPP__FOLDER_H__
22 #define LIBMAPIPP__FOLDER_H__
24 #include <iostream> //for debugging
25 #include <vector>
26 #include <boost/shared_ptr.hpp>
28 #include <libmapi++/clibmapi.h>
29 #include <libmapi++/mapi_exception.h>
30 #include <libmapi++/object.h>
31 #include <libmapi++/message.h>
33 namespace libmapipp
36 /**
37 * This class represents a %folder or container within Exchange
39 class folder : public object {
40 public:
41 /**
42 * Pointer to a message
44 typedef boost::shared_ptr<message> message_shared_ptr;
46 typedef std::vector<message_shared_ptr > message_container_type;
48 /**
49 * Pointer to a %folder
51 typedef boost::shared_ptr<folder> folder_shared_ptr;
53 /**
54 * Hierarchy folders
56 * This is a vector (list) of child folders for a given %folder
58 typedef std::vector<folder_shared_ptr> hierarchy_container_type;
60 /**
61 * \brief Constructor
63 * \param parent_folder The parent of this %folder.
64 * \param folder_id This folder's id.
66 folder(object& parent_folder, const mapi_id_t folder_id) throw(mapi_exception)
67 : object(parent_folder.get_session(), "folder"), m_id(folder_id)
69 if (OpenFolder(&parent_folder.data(), folder_id, &m_object) != MAPI_E_SUCCESS)
70 throw mapi_exception(GetLastError(), "folder::folder : OpenFolder");
73 /**
74 * \brief Obtain %folder id
76 * \return This folder's id.
78 mapi_id_t get_id() const { return m_id; }
80 /**
81 * \brief Delete a message that belongs to this %folder
83 * \param message_id The id of the message to delete.
85 void delete_message(mapi_id_t message_id) throw (mapi_exception)
87 if (DeleteMessage(&m_object, &message_id, 1) != MAPI_E_SUCCESS)
88 throw mapi_exception(GetLastError(), "folder::delete_message : DeleteMessage");
91 /**
92 * \brief Fetch all messages in this %folder
94 * \return A container of message shared pointers.
96 message_container_type fetch_messages() throw(mapi_exception)
98 uint32_t contents_table_row_count = 0;
99 mapi_object_t contents_table;
101 mapi_object_init(&contents_table);
102 if (GetContentsTable(&m_object, &contents_table, 0, &contents_table_row_count) != MAPI_E_SUCCESS) {
103 mapi_object_release(&contents_table);
104 throw mapi_exception(GetLastError(), "folder::fetch_messages : GetContentsTable");
107 SPropTagArray* property_tag_array = set_SPropTagArray(m_session.get_memory_ctx(), 0x2, PR_FID,
108 PR_MID);
110 if (SetColumns(&contents_table, property_tag_array) != MAPI_E_SUCCESS) {
111 MAPIFreeBuffer(property_tag_array);
112 mapi_object_release(&contents_table);
113 throw mapi_exception(GetLastError(), "folder::fetch_messages : SetColumns");
116 MAPIFreeBuffer(property_tag_array);
118 uint32_t rows_to_read = contents_table_row_count;
119 SRowSet row_set;
121 message_container_type message_container;
122 message_container.reserve(contents_table_row_count);
124 while( (QueryRows(&contents_table, rows_to_read, TBL_ADVANCE, &row_set) == MAPI_E_SUCCESS) && row_set.cRows) {
125 rows_to_read -= row_set.cRows;
126 for (unsigned int i = 0; i < row_set.cRows; ++i) {
127 try {
128 message_container.push_back(message_shared_ptr(new message(m_session,
129 m_id,
130 row_set.aRow[i].lpProps[1].value.d)));
132 catch(mapi_exception e) {
133 mapi_object_release(&contents_table);
134 throw;
139 mapi_object_release(&contents_table);
141 return message_container;
145 * \brief Fetch all subfolders within this %folder
147 * \return A container of %folder shared pointers.
149 hierarchy_container_type fetch_hierarchy() throw(mapi_exception)
151 mapi_object_t hierarchy_table;
152 uint32_t hierarchy_table_row_count = 0;
154 mapi_object_init(&hierarchy_table);
155 if (GetHierarchyTable(&m_object, &hierarchy_table, 0, &hierarchy_table_row_count) != MAPI_E_SUCCESS) {
156 mapi_object_release(&hierarchy_table);
157 throw mapi_exception(GetLastError(), "folder::fetch_hierarchy : GetHierarchyTable");
160 SPropTagArray* property_tag_array = set_SPropTagArray(m_session.get_memory_ctx(), 0x1, PR_FID);
162 if (SetColumns(&hierarchy_table, property_tag_array)) {
163 MAPIFreeBuffer(property_tag_array);
164 mapi_object_release(&hierarchy_table);
165 throw mapi_exception(GetLastError(), "folder::fetch_hierarchy : SetColumns");
168 MAPIFreeBuffer(property_tag_array);
170 uint32_t rows_to_read = hierarchy_table_row_count;
171 SRowSet row_set;
173 hierarchy_container_type hierarchy_container;
174 hierarchy_container.reserve(hierarchy_table_row_count);
176 while( (QueryRows(&hierarchy_table, rows_to_read, TBL_ADVANCE, &row_set) == MAPI_E_SUCCESS) && row_set.cRows) {
177 rows_to_read -= row_set.cRows;
178 for (unsigned int i = 0; i < row_set.cRows; ++i) {
179 try {
180 hierarchy_container.push_back(folder_shared_ptr(new folder(*this,
181 row_set.aRow[i].lpProps[0].value.d)));
183 catch(mapi_exception e) {
184 mapi_object_release(&hierarchy_table);
185 throw;
190 mapi_object_release(&hierarchy_table);
192 return hierarchy_container;
196 * Destructor
198 virtual ~folder() throw()
202 private:
203 mapi_id_t m_id;
206 } // namespace libmapipp
208 #endif //!LIBMAPIPP__FOLDER_H__