Fix OpenChange server code and access to Samba4 databases.
[OpenChange-git-clone.git] / libmapi++ / attachment.h
blobc5f24eed42051cbf1e30459cd245c149c59828d0
1 /*
2 libmapi C++ Wrapper
3 Attachment 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/>.
22 #ifndef LIBMAPIPP__ATTACHMENT_H__
23 #define LIBMAPIPP__ATTACHMENT_H__
25 #include <iostream> //for debugging
26 #include <string>
28 #include <libmapi++/clibmapi.h>
29 #include <libmapi++/message.h>
31 namespace libmapipp
33 class object;
35 /**
36 * \brief This class represents a message %attachment
38 * A message can contain both text content, and also have attached
39 * (embedded) files and messages. This class represents the attachments
40 * for one messaage.
42 * You may not need to create the attachments yourself, since you can
43 * create a container with all the attachments using message::fetch_attachments().
45 class attachment : public object {
46 public:
47 /** \brief Constructor
49 * \param mapi_message the message that this attachment belongs to.
50 * \param attach_num Attachment Number.
52 attachment(message& mapi_message, const uint32_t attach_num) throw(mapi_exception)
53 : object(mapi_message.get_session(), "attachment"), m_attach_num(attach_num), m_bin_data(NULL), m_data_size(0), m_filename("")
55 if (OpenAttach(&mapi_message.data(), attach_num, &m_object) != MAPI_E_SUCCESS)
56 throw mapi_exception(GetLastError(), "attachment::attachment : OpenAttach");
58 property_container properties = get_property_container();
59 properties << PR_ATTACH_FILENAME << PR_ATTACH_LONG_FILENAME << PR_ATTACH_SIZE << PR_ATTACH_DATA_BIN << PR_ATTACH_METHOD;
60 properties.fetch();
62 const char* filename = static_cast<const char*>(properties[PR_ATTACH_LONG_FILENAME]);
63 if (!filename) {
64 filename = static_cast<const char*>(properties[PR_ATTACH_FILENAME]);
67 if (filename) {
68 m_filename = filename;
71 m_data_size = *(static_cast<const uint32_t*>(properties[PR_ATTACH_SIZE]));
73 const Binary_r* attachment_data = static_cast<const Binary_r*>(properties[PR_ATTACH_DATA_BIN]);
75 // Don't load PR_ATTACH_DATA_BIN if it's embedded in message.
76 // NOTE: Use RopOpenEmbeddedMessage when it is implemented.
77 const uint32_t attach_method = *static_cast<const uint32_t*>(properties[PR_ATTACH_METHOD]);
78 if (attach_method != ATTACH_BY_VALUE)
79 return;
81 // Get Binary Data.
82 if (attachment_data) {
83 m_data_size = attachment_data->cb;
84 m_bin_data = new uint8_t[m_data_size];
85 memcpy(m_bin_data, attachment_data->lpb, attachment_data->cb);
86 } else {
87 mapi_object_t obj_stream;
88 mapi_object_init(&obj_stream);
89 if (OpenStream(&m_object, PR_ATTACH_DATA_BIN, OpenStream_ReadOnly, &obj_stream) != MAPI_E_SUCCESS)
90 throw mapi_exception(GetLastError(), "attachment::attachment : OpenStream");
92 if (GetStreamSize(&obj_stream, &m_data_size) != MAPI_E_SUCCESS)
93 throw mapi_exception(GetLastError(), "attachment::attachment : GetStreamSize");
95 m_bin_data = new uint8_t[m_data_size];
97 uint32_t pos = 0;
98 uint16_t bytes_read = 0;
99 do {
100 if (ReadStream(&obj_stream, m_bin_data+pos, 1024, &bytes_read) != MAPI_E_SUCCESS)
101 throw mapi_exception(GetLastError(), "attachment::attachment : ReadStream");
103 pos += bytes_read;
105 } while (bytes_read && pos < m_data_size);
107 mapi_object_release(&obj_stream);
113 * \brief The %attachment number
115 uint32_t get_num() const { return m_attach_num; }
118 * \brief the contents of the %attachment
120 * \note the length of the array is given by get_data_size()
122 const uint8_t* get_data() const { return m_bin_data; }
125 * \brief the size of the %attachment
127 * \return the size of the %attachment in bytes
129 uint32_t get_data_size() const { return m_data_size; }
132 * \brief the filename of the %attachment
134 * \note not all attachments have file names
136 * \return string containing the file name of the %attachment, if any
138 std::string get_filename() const { return m_filename; }
141 * Destructor
143 virtual ~attachment() throw()
145 if (m_bin_data) delete[] m_bin_data;
148 private:
149 uint32_t m_attach_num;
150 uint8_t* m_bin_data; // (same as unsigned char* ?)
151 uint32_t m_data_size;
152 std::string m_filename;
155 } // namespace libmapipp
157 #endif //!LIBMAPIPP__ATTACHMENT_H__