|
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 #include "qmessagecontentcontainer.h" |
|
42 #include "qmessagecontentcontainer_maemo_p.h" |
|
43 #include <QFile> |
|
44 #include <QFileInfo> |
|
45 #include <QTextCodec> |
|
46 |
|
47 QTM_BEGIN_NAMESPACE |
|
48 |
|
49 QMessageContentContainer QMessageContentContainerPrivate::from(long int messageId, |
|
50 unsigned int attachmentId, |
|
51 QByteArray &name, |
|
52 QByteArray &mimeType, |
|
53 QByteArray &mimeSubType, |
|
54 int size) |
|
55 { |
|
56 QMessageContentContainer result; |
|
57 result.d_ptr->_containingMessageId = messageId; |
|
58 result.d_ptr->_attachmentId = attachmentId; |
|
59 result.d_ptr->_name = name; |
|
60 result.d_ptr->_type = mimeType; |
|
61 result.d_ptr->_subType = mimeSubType; |
|
62 result.d_ptr->_size = size; |
|
63 result.d_ptr->_available = true; |
|
64 return result; |
|
65 } |
|
66 |
|
67 QMessageContentContainerPrivate& QMessageContentContainerPrivate::operator=(const QMessageContentContainerPrivate &other) |
|
68 { |
|
69 q_ptr = other.q_ptr; |
|
70 _message = other._message; |
|
71 _available = other._available; |
|
72 _size = other._size; |
|
73 _attachments = other._attachments; |
|
74 _type = other._type; |
|
75 _subType = other._subType; |
|
76 _charset = other._charset; |
|
77 _name = other._name; |
|
78 _content = other._content; |
|
79 _textContent = other._textContent; |
|
80 _filename = other._filename; |
|
81 _messageId = other._messageId; |
|
82 _id = other._id; |
|
83 _header = other._header; |
|
84 _containingMessageId = other._containingMessageId; |
|
85 _attachmentId = other._attachmentId; |
|
86 |
|
87 return *this; |
|
88 } |
|
89 |
|
90 bool QMessageContentContainerPrivate::isMessage() const |
|
91 { |
|
92 return (_message != 0); |
|
93 } |
|
94 |
|
95 void QMessageContentContainerPrivate::setDerivedMessage(QMessage *derived) |
|
96 { |
|
97 _message = derived; |
|
98 } |
|
99 |
|
100 void QMessageContentContainerPrivate::clearContents() |
|
101 { |
|
102 _type = QByteArray("text"); |
|
103 _subType = QByteArray("plain"); |
|
104 _charset = QByteArray(); |
|
105 _name = QByteArray(); |
|
106 _content = QByteArray(); |
|
107 _textContent = QString(); |
|
108 _filename = QByteArray(); |
|
109 _available = false; |
|
110 _size = 0; |
|
111 _header.clear(); |
|
112 _attachments.clear(); |
|
113 _containingMessageId = 0; |
|
114 _attachmentId = 0; |
|
115 } |
|
116 |
|
117 void QMessageContentContainerPrivate::setContentType(const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
118 { |
|
119 clearContents(); |
|
120 |
|
121 _type = type; |
|
122 _subType = subType; |
|
123 _charset = charset; |
|
124 } |
|
125 |
|
126 void QMessageContentContainerPrivate::setContent(const QString &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
127 { |
|
128 setContentType(type, subType, charset); |
|
129 |
|
130 _textContent = content; |
|
131 _size = content.size(); |
|
132 _available = true; |
|
133 } |
|
134 |
|
135 void QMessageContentContainerPrivate::setContent(const QByteArray &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset) |
|
136 { |
|
137 setContentType(type, subType, charset); |
|
138 |
|
139 _content = content; |
|
140 _available = true; |
|
141 } |
|
142 |
|
143 void QMessageContentContainerPrivate::setHeaderField(const QByteArray &name, const QByteArray &value) |
|
144 { |
|
145 _header.remove(name); |
|
146 _header.insert(name, value); |
|
147 } |
|
148 |
|
149 QMessageContentContainer* QMessageContentContainerPrivate::attachment(const QMessageContentContainerId &id) |
|
150 { |
|
151 if (isMessage()) { |
|
152 if (id == bodyContentId()) { |
|
153 return _message; |
|
154 } else { |
|
155 foreach (const QMessageContentContainer &container, _attachments) { |
|
156 if (container.d_ptr->_id == id) { |
|
157 return const_cast<QMessageContentContainer*>(&container); |
|
158 } |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 return 0; |
|
164 } |
|
165 |
|
166 const QMessageContentContainer* QMessageContentContainerPrivate::attachment(const QMessageContentContainerId &id) const |
|
167 { |
|
168 if (isMessage()) { |
|
169 if (id == bodyContentId()) { |
|
170 return _message; |
|
171 } else { |
|
172 foreach (const QMessageContentContainer &container, _attachments) { |
|
173 if (container.d_ptr->_id == id) { |
|
174 return &container; |
|
175 } |
|
176 } |
|
177 } |
|
178 } |
|
179 |
|
180 return 0; |
|
181 } |
|
182 |
|
183 bool QMessageContentContainerPrivate::createAttachment(const QString& attachmentPath) |
|
184 { |
|
185 //set the attachment data |
|
186 |
|
187 if (!QFile::exists(attachmentPath)) { |
|
188 return false; |
|
189 } |
|
190 |
|
191 QFile attachmentFile(attachmentPath); |
|
192 if (!attachmentFile.open(QIODevice::ReadOnly)) { |
|
193 return false; |
|
194 } |
|
195 |
|
196 _content = attachmentFile.readAll(); |
|
197 _available = true; |
|
198 |
|
199 _size = attachmentFile.size(); |
|
200 |
|
201 attachmentFile.close(); |
|
202 QFileInfo fi(attachmentPath); |
|
203 _name = fi.fileName().toLatin1(); |
|
204 |
|
205 //set the mime-type |
|
206 QByteArray mimeType; |
|
207 QString type; |
|
208 // TODO: |
|
209 |
|
210 QString extension(fi.suffix()); |
|
211 int index = mimeType.indexOf("/"); |
|
212 if (index != -1) { |
|
213 _type = mimeType.left(index).trimmed(); |
|
214 _subType = mimeType.mid(index + 1).trimmed(); |
|
215 } |
|
216 |
|
217 // set the whole filepath to _filename |
|
218 _filename = fi.filePath().toLatin1(); |
|
219 |
|
220 return true; |
|
221 } |
|
222 |
|
223 QMessageContentContainerId QMessageContentContainerPrivate::appendContent(QMessageContentContainer& container) |
|
224 { |
|
225 container.d_ptr->_id = QMessageContentContainerId(QString::number(_attachments.count()+1)); |
|
226 _attachments.append(container); |
|
227 return container.d_ptr->_id; |
|
228 } |
|
229 |
|
230 QMessageContentContainerId QMessageContentContainerPrivate::prependContent(QMessageContentContainer& container) |
|
231 { |
|
232 _attachments.prepend(container); |
|
233 for (int i = 0; i < _attachments.count(); ++i) { |
|
234 _attachments[i].d_ptr->_id = QMessageContentContainerId(QString::number(i+1)); |
|
235 } |
|
236 return _attachments[0].d_ptr->_id; |
|
237 } |
|
238 |
|
239 |
|
240 |
|
241 QMessageContentContainerId QMessageContentContainerPrivate::bodyContentId() |
|
242 { |
|
243 return QMessageContentContainerId(QString::number(0)); |
|
244 } |
|
245 |
|
246 QByteArray QMessageContentContainerPrivate::attachmentFilename(const QMessageContentContainer& container) |
|
247 { |
|
248 return container.d_ptr->_filename; |
|
249 } |
|
250 |
|
251 QMessageContentContainerPrivate* QMessageContentContainerPrivate::implementation(const QMessageContentContainer &container) |
|
252 { |
|
253 return container.d_ptr; |
|
254 } |
|
255 |
|
256 QMessageContentContainer::QMessageContentContainer() |
|
257 : d_ptr(new QMessageContentContainerPrivate(this)) |
|
258 { |
|
259 } |
|
260 |
|
261 QMessageContentContainer::QMessageContentContainer(const QMessageContentContainer &other) |
|
262 : d_ptr(new QMessageContentContainerPrivate(this)) |
|
263 { |
|
264 this->operator=(other); |
|
265 } |
|
266 |
|
267 QMessageContentContainer& QMessageContentContainer::operator=(const QMessageContentContainer& other) |
|
268 { |
|
269 if (&other != this) { |
|
270 *d_ptr = *other.d_ptr; |
|
271 } |
|
272 |
|
273 return *this; |
|
274 } |
|
275 |
|
276 QMessageContentContainer::~QMessageContentContainer() |
|
277 { |
|
278 delete d_ptr; |
|
279 } |
|
280 |
|
281 QByteArray QMessageContentContainer::contentType() const |
|
282 { |
|
283 return d_ptr->_type; |
|
284 } |
|
285 |
|
286 QByteArray QMessageContentContainer::contentSubType() const |
|
287 { |
|
288 return d_ptr->_subType; |
|
289 } |
|
290 |
|
291 QByteArray QMessageContentContainer::contentCharset() const |
|
292 { |
|
293 return d_ptr->_charset; |
|
294 } |
|
295 |
|
296 QByteArray QMessageContentContainer::suggestedFileName() const |
|
297 { |
|
298 return d_ptr->_name; |
|
299 } |
|
300 |
|
301 bool QMessageContentContainer::isContentAvailable() const |
|
302 { |
|
303 return d_ptr->_available; |
|
304 } |
|
305 |
|
306 int QMessageContentContainer::size() const |
|
307 { |
|
308 return d_ptr->_size; |
|
309 } |
|
310 |
|
311 QString QMessageContentContainer::textContent() const |
|
312 { |
|
313 //TODO: if (d_ptr->_textContent.isEmpty() && d_ptr->_attachmentId != 0) { |
|
314 //TODO: CMTMEngine* mtmEngine = CMTMEngine::instance(); |
|
315 //TODO: const_cast<QString&>(d_ptr->_textContent) = mtmEngine->attachmentTextContent(d_ptr->_containingMessageId, d_ptr->_attachmentId, d_ptr->_charset); |
|
316 //TODO: } |
|
317 if (!d_ptr->_textContent.isEmpty()) { |
|
318 return d_ptr->_textContent; |
|
319 } |
|
320 |
|
321 QTextCodec *codec = QTextCodec::codecForName(d_ptr->_charset.data()); |
|
322 if (codec) { |
|
323 return codec->toUnicode(d_ptr->_content); |
|
324 } else { |
|
325 return QString::fromLatin1(d_ptr->_content); |
|
326 } |
|
327 } |
|
328 |
|
329 QByteArray QMessageContentContainer::content() const |
|
330 { |
|
331 //TODO: if (d_ptr->_content.isEmpty() && d_ptr->_attachmentId != 0) { |
|
332 //TODO: CMTMEngine* mtmEngine = CMTMEngine::instance(); |
|
333 //TODO: const_cast<QByteArray&>(d_ptr->_content) = mtmEngine->attachmentContent(d_ptr->_containingMessageId, d_ptr->_attachmentId); |
|
334 //TODO: } |
|
335 |
|
336 return d_ptr->_content; |
|
337 } |
|
338 |
|
339 void QMessageContentContainer::writeTextContent(QTextStream& out) const |
|
340 { |
|
341 out << textContent(); |
|
342 } |
|
343 |
|
344 void QMessageContentContainer::writeContent(QDataStream& out) const |
|
345 { |
|
346 QByteArray data(content()); |
|
347 out.writeRawData(data.constData(), data.length()); |
|
348 } |
|
349 |
|
350 QMessageContentContainerIdList QMessageContentContainer::contentIds() const |
|
351 { |
|
352 QMessageContentContainerIdList ids; |
|
353 |
|
354 if (d_ptr->isMessage()) { |
|
355 foreach (const QMessageContentContainer &container, d_ptr->_attachments) { |
|
356 ids.append(container.d_ptr->_id); |
|
357 } |
|
358 } |
|
359 |
|
360 return ids; |
|
361 } |
|
362 |
|
363 QMessageContentContainer QMessageContentContainer::find(const QMessageContentContainerId &id) const |
|
364 { |
|
365 if (d_ptr->isMessage()) { |
|
366 if (const QMessageContentContainer *container = d_ptr->attachment(id)) { |
|
367 return *container; |
|
368 } |
|
369 } |
|
370 |
|
371 return QMessageContentContainer(); |
|
372 } |
|
373 |
|
374 bool QMessageContentContainer::contains(const QMessageContentContainerId &id) const |
|
375 { |
|
376 if (d_ptr->isMessage()) { |
|
377 return (d_ptr->attachment(id) != 0); |
|
378 } |
|
379 |
|
380 return false; |
|
381 } |
|
382 |
|
383 QString QMessageContentContainer::headerFieldValue(const QByteArray &name) const |
|
384 { |
|
385 QMultiMap<QByteArray, QString>::const_iterator it = d_ptr->_header.find(name); |
|
386 if (it != d_ptr->_header.end()) { |
|
387 return it.value(); |
|
388 } |
|
389 |
|
390 return QString(); |
|
391 } |
|
392 |
|
393 QStringList QMessageContentContainer::headerFieldValues(const QByteArray &name) const |
|
394 { |
|
395 QStringList values; |
|
396 |
|
397 QMultiMap<QByteArray, QString>::const_iterator it = d_ptr->_header.find(name); |
|
398 while ((it != d_ptr->_header.end()) && (it.key() == name)) { |
|
399 values.append(it.value()); |
|
400 ++it; |
|
401 } |
|
402 |
|
403 return values; |
|
404 } |
|
405 |
|
406 QList<QByteArray> QMessageContentContainer::headerFields() const |
|
407 { |
|
408 return d_ptr->_header.keys(); |
|
409 } |
|
410 |
|
411 void QMessageContentContainer::setDerivedMessage(QMessage *derived) |
|
412 { |
|
413 d_ptr->setDerivedMessage(derived); |
|
414 } |
|
415 |
|
416 QTM_END_NAMESPACE |