src/messaging/qmessagecontentcontainer_p.h
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include "messagingutil_p.h"
       
    48 
       
    49 #ifdef USE_QMF_IMPLEMENTATION
       
    50 #include "qmfhelpers_p.h"
       
    51 #else
       
    52 #include "qmessage_p.h"
       
    53 #if defined(Q_OS_WIN)
       
    54 #include "winhelpers_p.h"
       
    55 #include "qmessagecontentcontainerid_p.h"
       
    56 #endif
       
    57 #endif
       
    58 
       
    59 #include <QSharedData>
       
    60 #include <QList>
       
    61 #include <QMultiMap>
       
    62 #include <QDebug>
       
    63 #include <QFile>
       
    64 #include <QFileInfo>
       
    65 
       
    66 QTM_BEGIN_NAMESPACE
       
    67 
       
    68 #ifdef USE_QMF_IMPLEMENTATION
       
    69 class QMessageContentContainerPrivate
       
    70 {
       
    71 public:
       
    72     mutable QMessage *_message;
       
    73     mutable QMailMessagePart _part;
       
    74     mutable QMailMessagePartContainer *_container;
       
    75 
       
    76     QByteArray _type;
       
    77     QByteArray _subType;
       
    78     QByteArray _charset;
       
    79     QByteArray _name;
       
    80     QByteArray _content;
       
    81     QString _textContent;
       
    82     QString _filename;
       
    83 
       
    84     QMessageContentContainerPrivate()
       
    85         : _message(0),
       
    86           _container(&_part)
       
    87     {
       
    88     }
       
    89 
       
    90     void setDerivedMessage(QMessage *derived)
       
    91     {
       
    92         _message = derived;
       
    93         _part = QMailMessagePart();
       
    94         _container = QmfHelpers::convert(_message);
       
    95     }
       
    96 };
       
    97 
       
    98 #else
       
    99 class QMessageContentContainerPrivate
       
   100 {
       
   101     Q_DECLARE_PUBLIC(QMessageContentContainer)
       
   102 
       
   103 public:
       
   104     QMessageContentContainerPrivate(QMessageContentContainer *contentContainer)
       
   105         :
       
   106         q_ptr(contentContainer),
       
   107          _message(0),
       
   108 #ifdef Q_OS_WIN
       
   109          _attachmentNumber(0),
       
   110 #endif
       
   111          _available(false),
       
   112          _size(0)
       
   113     {
       
   114     }
       
   115 
       
   116     QMessageContentContainerPrivate(const QMessageContentContainerPrivate& other)
       
   117         :
       
   118         q_ptr(other.q_ptr),
       
   119         _message(other._message),
       
   120 #ifdef Q_OS_WIN
       
   121         _attachmentNumber(other._attachmentNumber),
       
   122         _containingMessageId(other._containingMessageId),
       
   123 #endif
       
   124         _attachments(other._attachments),
       
   125         _available(other._available),
       
   126         _size(other._size),
       
   127         _type(other._type),
       
   128         _subType(other._subType),
       
   129         _charset(other._charset),
       
   130         _name(other._name),
       
   131         _content(other._content),
       
   132         _textContent(other._textContent),
       
   133         _filename(other._filename),
       
   134         _messageId(other._messageId),
       
   135         _id(other._id),
       
   136         _header(other._header)
       
   137     {
       
   138     }
       
   139 
       
   140     QMessageContentContainerPrivate &operator=(const QMessageContentContainerPrivate &other)
       
   141     {
       
   142         q_ptr = other.q_ptr;
       
   143         _message = other._message;
       
   144 #ifdef Q_OS_WIN
       
   145         _attachmentNumber = other._attachmentNumber;
       
   146         _containingMessageId = other._containingMessageId;
       
   147 #endif
       
   148         _available = other._available;
       
   149         _size = other._size;
       
   150         _attachments = other._attachments;
       
   151         _type = other._type;
       
   152         _subType = other._subType;
       
   153         _charset = other._charset;
       
   154         _name = other._name;
       
   155         _content = other._content;
       
   156         _textContent = other._textContent;
       
   157         _filename = other._filename;
       
   158         _messageId = other._messageId;
       
   159         _id = other._id;
       
   160         _header = other._header;
       
   161 
       
   162         return *this;
       
   163     }
       
   164 
       
   165 #ifdef Q_OS_WIN
       
   166     static QMessageContentContainer from(const QMessageId &id, ULONG number)
       
   167     {
       
   168         QMessageContentContainer result;
       
   169         result.d_ptr->_containingMessageId = id;
       
   170         result.d_ptr->_attachmentNumber = number;
       
   171         result.d_ptr->_available = true;
       
   172         return result;
       
   173     }
       
   174 #endif
       
   175 
       
   176     QMessageContentContainer *q_ptr;
       
   177     QMessage *_message;
       
   178 
       
   179 #ifdef Q_OS_WIN
       
   180     QMessageId _containingMessageId;
       
   181     ULONG _attachmentNumber;
       
   182 #endif
       
   183 
       
   184     QByteArray _type;
       
   185     QByteArray _subType;
       
   186     QByteArray _charset;
       
   187     QByteArray _name;
       
   188     QByteArray _content;
       
   189     QString _textContent;
       
   190     QString _filename;
       
   191     QMessageId _messageId;
       
   192     QMessageContentContainerId _id;
       
   193     bool _available;
       
   194     int _size;
       
   195     QList<QMessageContentContainer> _attachments;
       
   196     QMultiMap<QByteArray, QString>  _header;
       
   197 
       
   198     bool isMessage() const
       
   199     {
       
   200         return (_message != 0);
       
   201     }
       
   202 
       
   203     void setDerivedMessage(QMessage *derived)
       
   204     {
       
   205         _message = derived;
       
   206     }
       
   207 
       
   208     void clearContents()
       
   209     {
       
   210         _type = QByteArray("text");
       
   211         _subType = QByteArray("plain");
       
   212         _charset = QByteArray();
       
   213         _name = QByteArray();
       
   214         _content = QByteArray();
       
   215         _textContent = QString();
       
   216         _filename = QString();
       
   217         _available = false;
       
   218         _size = 0;
       
   219         _header.clear();
       
   220         _attachments.clear();
       
   221     }
       
   222 
       
   223     void setContentType(const QByteArray &type, const QByteArray &subType, const QByteArray &charset)
       
   224     {
       
   225         clearContents();
       
   226 
       
   227         _type = type;
       
   228         _subType = subType;
       
   229         _charset = charset;
       
   230     }
       
   231 
       
   232     void setContent(const QString &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset)
       
   233     {
       
   234         setContentType(type, subType, charset);
       
   235 
       
   236         _textContent = content;
       
   237         _size = content.length();
       
   238 #ifdef Q_OS_WIN
       
   239         // Approximate size in bytes, not chars
       
   240         _size *= sizeof(TCHAR);
       
   241 #endif
       
   242         _available = true;
       
   243     }
       
   244 
       
   245     void setContent(const QByteArray &content, const QByteArray &type, const QByteArray &subType, const QByteArray &charset)
       
   246     {
       
   247         setContentType(type, subType, charset);
       
   248 
       
   249         _content = content;
       
   250         _size = content.length();
       
   251         _available = true;
       
   252     }
       
   253 
       
   254     void setHeaderField(const QByteArray &name, const QByteArray &value)
       
   255     {
       
   256         _header.remove(name);
       
   257         _header.insert(name, value);
       
   258     }
       
   259 
       
   260     QMessageContentContainer *attachment(const QMessageContentContainerId &id)
       
   261     {
       
   262         if (isMessage()) {
       
   263 #ifdef Q_OS_WIN
       
   264             _message->d_ptr->ensureAttachmentsPresent(_message);
       
   265 #endif
       
   266 
       
   267             if (id == bodyContentId()) {
       
   268                 return _message;
       
   269             } else {
       
   270                 foreach (const QMessageContentContainer &container, _attachments) {
       
   271                     if (container.d_ptr->_id == id) {
       
   272                         return const_cast<QMessageContentContainer*>(&container);
       
   273                     }
       
   274                 }
       
   275             }
       
   276         }
       
   277 
       
   278         return 0;
       
   279     }
       
   280 
       
   281     const QMessageContentContainer *attachment(const QMessageContentContainerId &id) const
       
   282     {
       
   283         if (isMessage()) {
       
   284 #ifdef Q_OS_WIN
       
   285             _message->d_ptr->ensureAttachmentsPresent(_message);
       
   286 #endif
       
   287 
       
   288             if (id == bodyContentId()) {
       
   289                 return _message;
       
   290             } else {
       
   291                 foreach (const QMessageContentContainer &container, _attachments) {
       
   292                     if (container.d_ptr->_id == id) {
       
   293                         return &container;
       
   294                     }
       
   295                 }
       
   296             }
       
   297         }
       
   298 
       
   299         return 0;
       
   300     }
       
   301 
       
   302     bool createAttachment(const QString& attachmentPath)
       
   303     {
       
   304         //set the attachment data
       
   305 
       
   306         if (!QFile::exists(attachmentPath)) {
       
   307             qWarning() << "Could not create attachment. File " << attachmentPath << " does not exist";
       
   308             return false;
       
   309         }
       
   310 
       
   311         QFile attachmentFile(attachmentPath);
       
   312         if (!attachmentFile.open(QIODevice::ReadOnly)) {
       
   313             qWarning() << "Could not open attachment " << attachmentPath;
       
   314             return false;
       
   315         }
       
   316 
       
   317         _content = attachmentFile.readAll();
       
   318         _available = true;
       
   319 
       
   320         attachmentFile.close();
       
   321 
       
   322         //set the suggested file name for the attachment
       
   323 
       
   324         QFileInfo fi(attachmentPath);
       
   325         _name = fi.fileName().toLatin1();
       
   326         _size = fi.size();
       
   327 
       
   328         //set the attachment filepath
       
   329 
       
   330         _filename = attachmentPath;
       
   331 
       
   332         //set the mime-type
       
   333 
       
   334         QByteArray mimeType;
       
   335 
       
   336         QString extension(fi.suffix());
       
   337 #ifdef Q_OS_WIN
       
   338         mimeType = WinHelpers::contentTypeFromExtension(extension);
       
   339 #else
       
   340         // TODO
       
   341 #endif
       
   342         int index = mimeType.indexOf("/");
       
   343         if (index != -1) {
       
   344             _type = mimeType.left(index).trimmed();
       
   345             _subType = mimeType.mid(index + 1).trimmed();
       
   346         }
       
   347 
       
   348         return true;
       
   349     }
       
   350 
       
   351     QMessageContentContainerId appendContent(QMessageContentContainer& container)
       
   352     {
       
   353 #ifdef OS_WIN
       
   354         if (!isMessage()) {
       
   355             qWarning() << "Unable to add child QMessageContentContainer. MAPI only supports single level nesting of containers.";
       
   356             return;
       
   357         }
       
   358 #endif
       
   359         container.d_ptr->_id = QMessageContentContainerId(MessagingUtil::addIdPrefix(QString::number(_attachments.count()+1)));
       
   360         _attachments.append(container);
       
   361         return container.d_ptr->_id;
       
   362     }
       
   363 
       
   364     QMessageContentContainerId prependContent(QMessageContentContainer& container)
       
   365     {
       
   366 #ifdef OS_WIN
       
   367         if (!isMessage()) {
       
   368             qWarning() << "Unable to add child QMessageContentContainer. MAPI only supports single level nesting of containers.";
       
   369             return;
       
   370         }
       
   371 #endif
       
   372         _attachments.prepend(container);
       
   373         for (int i = 0; i < _attachments.count(); ++i) {
       
   374             _attachments[i].d_ptr->_id = QMessageContentContainerId(MessagingUtil::addIdPrefix(QString::number(i+1)));
       
   375         }
       
   376         return _attachments[0].d_ptr->_id;
       
   377     }
       
   378 
       
   379     QMessageContentContainerId bodyContentId() const
       
   380     {
       
   381         return QMessageContentContainerId(MessagingUtil::addIdPrefix(QString::number(0)));
       
   382     }
       
   383 
       
   384     static QString attachmentFilename(const QMessageContentContainer& container)
       
   385     {
       
   386         return container.d_ptr->_filename;
       
   387     }
       
   388 };
       
   389 #endif
       
   390 
       
   391 QTM_END_NAMESPACE
       
   392 
       
   393 #endif