|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #ifndef QMESSAGECONTENTCONTAINER_P_H |
|
42 #define QMESSAGECONTENTCONTAINER_P_H |
|
43 |
|
44 #include "qmessageglobal.h" |
|
45 #include "qmessagecontentcontainer.h" |
|
46 #include "qmessage.h" |
|
47 |
|
48 #ifdef USE_QMF_IMPLEMENTATION |
|
49 #include "qmfhelpers_p.h" |
|
50 #else |
|
51 #include "qmessage_p.h" |
|
52 #if defined(Q_OS_WIN) |
|
53 #include "winhelpers_p.h" |
|
54 #include "qmessagecontentcontainerid_p.h" |
|
55 #endif |
|
56 #endif |
|
57 |
|
58 #include <QSharedData> |
|
59 #include <QList> |
|
60 #include <QMultiMap> |
|
61 #include <QDebug> |
|
62 #include <QFile> |
|
63 #include <QFileInfo> |
|
64 |
|
65 QTM_BEGIN_NAMESPACE |
|
66 |
|
67 #ifdef USE_QMF_IMPLEMENTATION |
|
68 class QMessageContentContainerPrivate |
|
69 { |
|
70 public: |
|
71 mutable QMessage *_message; |
|
72 mutable QMailMessagePart _part; |
|
73 mutable QMailMessagePartContainer *_container; |
|
74 |
|
75 QByteArray _type; |
|
76 QByteArray _subType; |
|
77 QByteArray _charset; |
|
78 QByteArray _name; |
|
79 QByteArray _content; |
|
80 QString _textContent; |
|
81 QString _filename; |
|
82 |
|
83 QMessageContentContainerPrivate() |
|
84 : _message(0), |
|
85 _container(&_part) |
|
86 { |
|
87 } |
|
88 |
|
89 void setDerivedMessage(QMessage *derived) |
|
90 { |
|
91 _message = derived; |
|
92 _part = QMailMessagePart(); |
|
93 _container = QmfHelpers::convert(_message); |
|
94 } |
|
95 }; |
|
96 |
|
97 #else |
|
98 class QMessageContentContainerPrivate |
|
99 { |
|
100 Q_DECLARE_PUBLIC(QMessageContentContainer) |
|
101 |
|
102 public: |
|
103 QMessageContentContainerPrivate(QMessageContentContainer *contentContainer) |
|
104 : |
|
105 q_ptr(contentContainer), |
|
106 _message(0), |
|
107 #ifdef Q_OS_WIN |
|
108 _attachmentNumber(0), |
|
109 #endif |
|
110 _available(false), |
|
111 _size(0) |
|
112 { |
|
113 } |
|
114 |
|
115 QMessageContentContainerPrivate(const QMessageContentContainerPrivate& other) |
|
116 : |
|
117 q_ptr(other.q_ptr), |
|
118 _message(other._message), |
|
119 #ifdef Q_OS_WIN |
|
120 _attachmentNumber(other._attachmentNumber), |
|
121 _containingMessageId(other._containingMessageId), |
|
122 #endif |
|
123 _attachments(other._attachments), |
|
124 _available(other._available), |
|
125 _size(other._size), |
|
126 _type(other._type), |
|
127 _subType(other._subType), |
|
128 _charset(other._charset), |
|
129 _name(other._name), |
|
130 _content(other._content), |
|
131 _textContent(other._textContent), |
|
132 _filename(other._filename), |
|
133 _messageId(other._messageId), |
|
134 _id(other._id), |
|
135 _header(other._header) |
|
136 { |
|
137 } |
|
138 |
|
139 QMessageContentContainerPrivate &operator=(const QMessageContentContainerPrivate &other) |
|
140 { |
|
141 q_ptr = other.q_ptr; |
|
142 _message = other._message; |
|
143 #ifdef Q_OS_WIN |
|
144 _attachmentNumber = other._attachmentNumber; |
|
145 _containingMessageId = other._containingMessageId; |
|
146 #endif |
|
147 _available = other._available; |
|
148 _size = other._size; |
|
149 _attachments = other._attachments; |
|
150 _type = other._type; |
|
151 _subType = other._subType; |
|
152 _charset = other._charset; |
|
153 _name = other._name; |
|
154 _content = other._content; |
|
155 _textContent = other._textContent; |
|
156 _filename = other._filename; |
|
157 _messageId = other._messageId; |
|
158 _id = other._id; |
|
159 _header = other._header; |
|
160 |
|
161 return *this; |
|
162 } |
|
163 |
|
164 #ifdef Q_OS_WIN |
|
165 static QMessageContentContainer from(const QMessageId &id, ULONG number) |
|
166 { |
|
167 QMessageContentContainer result; |
|
168 result.d_ptr->_containingMessageId = id; |
|
169 result.d_ptr->_attachmentNumber = number; |
|
170 result.d_ptr->_available = true; |
|
171 return result; |
|
172 } |
|
173 #endif |
|
174 |
|
175 QMessageContentContainer *q_ptr; |
|
176 QMessage *_message; |
|
177 |
|
178 #ifdef Q_OS_WIN |
|
179 QMessageId _containingMessageId; |
|
180 ULONG _attachmentNumber; |
|
181 #endif |
|
182 |
|
183 QByteArray _type; |
|
184 QByteArray _subType; |
|
185 QByteArray _charset; |
|
186 QByteArray _name; |
|
187 QByteArray _content; |
|
188 QString _textContent; |
|
189 QString _filename; |
|
190 QMessageId _messageId; |
|
191 QMessageContentContainerId _id; |
|
192 bool _available; |
|
193 int _size; |
|
194 QList<QMessageContentContainer> _attachments; |
|
195 QMultiMap<QByteArray, QString> _header; |
|
196 |
|
197 bool isMessage() const |
|
198 { |
|
199 return (_message != 0); |
|
200 } |
|
201 |
|
202 void setDerivedMessage(QMessage *derived) |
|
203 { |
|
204 _message = derived; |
|
205 } |
|
206 |
|
207 void clearContents() |
|
208 { |
|
209 _type = QByteArray("text"); |
|
210 _subType = QByteArray("plain"); |
|
211 _charset = QByteArray(); |
|
212 _name = QByteArray(); |
|
213 _content = QByteArray(); |
|
214 _textContent = QString(); |
|
215 _filename = QString(); |
|
216 _available = false; |
|
217 _size = 0; |
|
218 _header.clear(); |
|
219 _attachments.clear(); |
|
220 } |
|
221 |
|
222 void setContentType(const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
223 { |
|
224 clearContents(); |
|
225 |
|
226 _type = type; |
|
227 _subType = subType; |
|
228 _charset = charset; |
|
229 } |
|
230 |
|
231 void setContent(const QString &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
232 { |
|
233 setContentType(type, subType, charset); |
|
234 |
|
235 _textContent = content; |
|
236 _size = content.length(); |
|
237 #ifdef Q_OS_WIN |
|
238 // Approximate size in bytes, not chars |
|
239 _size *= sizeof(TCHAR); |
|
240 #endif |
|
241 _available = true; |
|
242 } |
|
243 |
|
244 void setContent(const QByteArray &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
245 { |
|
246 setContentType(type, subType, charset); |
|
247 |
|
248 _content = content; |
|
249 _size = content.length(); |
|
250 _available = true; |
|
251 } |
|
252 |
|
253 void setHeaderField(const QByteArray &name, const QByteArray &value) |
|
254 { |
|
255 _header.remove(name); |
|
256 _header.insert(name, value); |
|
257 } |
|
258 |
|
259 QMessageContentContainer *attachment(const QMessageContentContainerId &id) |
|
260 { |
|
261 if (isMessage()) { |
|
262 #ifdef Q_OS_WIN |
|
263 _message->d_ptr->ensureAttachmentsPresent(_message); |
|
264 #endif |
|
265 |
|
266 if (id == bodyContentId()) { |
|
267 return _message; |
|
268 } else { |
|
269 foreach (const QMessageContentContainer &container, _attachments) { |
|
270 if (container.d_ptr->_id == id) { |
|
271 return const_cast<QMessageContentContainer*>(&container); |
|
272 } |
|
273 } |
|
274 } |
|
275 } |
|
276 |
|
277 return 0; |
|
278 } |
|
279 |
|
280 const QMessageContentContainer *attachment(const QMessageContentContainerId &id) const |
|
281 { |
|
282 if (isMessage()) { |
|
283 #ifdef Q_OS_WIN |
|
284 _message->d_ptr->ensureAttachmentsPresent(_message); |
|
285 #endif |
|
286 |
|
287 if (id == bodyContentId()) { |
|
288 return _message; |
|
289 } else { |
|
290 foreach (const QMessageContentContainer &container, _attachments) { |
|
291 if (container.d_ptr->_id == id) { |
|
292 return &container; |
|
293 } |
|
294 } |
|
295 } |
|
296 } |
|
297 |
|
298 return 0; |
|
299 } |
|
300 |
|
301 bool createAttachment(const QString& attachmentPath) |
|
302 { |
|
303 //set the attachment data |
|
304 |
|
305 if (!QFile::exists(attachmentPath)) { |
|
306 qWarning() << "Could not create attachment. File " << attachmentPath << " does not exist"; |
|
307 return false; |
|
308 } |
|
309 |
|
310 QFile attachmentFile(attachmentPath); |
|
311 if (!attachmentFile.open(QIODevice::ReadOnly)) { |
|
312 qWarning() << "Could not open attachment " << attachmentPath; |
|
313 return false; |
|
314 } |
|
315 |
|
316 _content = attachmentFile.readAll(); |
|
317 _available = true; |
|
318 |
|
319 attachmentFile.close(); |
|
320 |
|
321 //set the suggested file name for the attachment |
|
322 |
|
323 QFileInfo fi(attachmentPath); |
|
324 _name = fi.fileName().toLatin1(); |
|
325 _size = fi.size(); |
|
326 |
|
327 //set the attachment filepath |
|
328 |
|
329 _filename = attachmentPath; |
|
330 |
|
331 //set the mime-type |
|
332 |
|
333 QByteArray mimeType; |
|
334 |
|
335 QString extension(fi.suffix()); |
|
336 #ifdef Q_OS_WIN |
|
337 mimeType = WinHelpers::contentTypeFromExtension(extension); |
|
338 #else |
|
339 // TODO |
|
340 #endif |
|
341 int index = mimeType.indexOf("/"); |
|
342 if (index != -1) { |
|
343 _type = mimeType.left(index).trimmed(); |
|
344 _subType = mimeType.mid(index + 1).trimmed(); |
|
345 } |
|
346 |
|
347 return true; |
|
348 } |
|
349 |
|
350 QMessageContentContainerId appendContent(QMessageContentContainer& container) |
|
351 { |
|
352 #ifdef OS_WIN |
|
353 if (!isMessage()) { |
|
354 qWarning() << "Unable to add child QMessageContentContainer. MAPI only supports single level nesting of containers."; |
|
355 return; |
|
356 } |
|
357 #endif |
|
358 container.d_ptr->_id = QMessageContentContainerId(QString::number(_attachments.count()+1)); |
|
359 _attachments.append(container); |
|
360 return container.d_ptr->_id; |
|
361 } |
|
362 |
|
363 QMessageContentContainerId prependContent(QMessageContentContainer& container) |
|
364 { |
|
365 #ifdef OS_WIN |
|
366 if (!isMessage()) { |
|
367 qWarning() << "Unable to add child QMessageContentContainer. MAPI only supports single level nesting of containers."; |
|
368 return; |
|
369 } |
|
370 #endif |
|
371 _attachments.prepend(container); |
|
372 for (int i = 0; i < _attachments.count(); ++i) { |
|
373 _attachments[i].d_ptr->_id = QMessageContentContainerId(QString::number(i+1)); |
|
374 } |
|
375 return _attachments[0].d_ptr->_id; |
|
376 } |
|
377 |
|
378 QMessageContentContainerId bodyContentId() const |
|
379 { |
|
380 return QMessageContentContainerId(QString::number(0)); |
|
381 } |
|
382 |
|
383 static QString attachmentFilename(const QMessageContentContainer& container) |
|
384 { |
|
385 return container.d_ptr->_filename; |
|
386 } |
|
387 }; |
|
388 #endif |
|
389 |
|
390 QTM_END_NAMESPACE |
|
391 |
|
392 #endif |