emailservices/nmailbase/src/nmmessagepart.cpp
changeset 18 578830873419
child 20 ecc8def7944a
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmmessagepart.h"
       
    19 #include "nmmessageenvelope.h"
       
    20 
       
    21 
       
    22 /*!
       
    23     Constructs message part private with null id
       
    24  */
       
    25 NmMessagePartPrivate::NmMessagePartPrivate()
       
    26 		: mOwnId(0), mSize(0), mFetchedSize(0),
       
    27 		mTextContent(0)
       
    28 {
       
    29 }
       
    30 
       
    31 /*!
       
    32     Destructor of message part private
       
    33  */
       
    34 NmMessagePartPrivate::~NmMessagePartPrivate() 
       
    35 {
       
    36 }
       
    37 
       
    38 /*!
       
    39     Checks the content type for the message type
       
    40     @return bool result
       
    41  */
       
    42 bool NmMessagePartPrivate::isMessage() const
       
    43 {
       
    44     bool isMessage = false;
       
    45     if (!mContentType.isEmpty()) {
       
    46 		if (mContentType.contains(NmContentTypeTypeMessage, Qt::CaseInsensitive)) {
       
    47 			isMessage = true;
       
    48     	}
       
    49     }
       
    50     return isMessage;
       
    51 }
       
    52 
       
    53 /*!
       
    54     Checks the content type for the text type
       
    55     @return bool result
       
    56  */
       
    57 bool NmMessagePartPrivate::isTextContent() const
       
    58 {
       
    59 	bool isText = false;
       
    60 	if (!mContentType.isEmpty()) {
       
    61 		if (mContentType.contains(NmContentTypeTextPlain, Qt::CaseInsensitive) ||
       
    62 				mContentType.contains(NmContentTypeTextHtml, Qt::CaseInsensitive)) {
       
    63 			isText = true;
       
    64 		}
       
    65 	}
       
    66 	return isText;
       
    67 }
       
    68 
       
    69 /*!
       
    70     \class NmMessagePart
       
    71     \brief Represents data model of MIME style message part
       
    72  */
       
    73 
       
    74 
       
    75 /*!
       
    76     Constructs message part with null id
       
    77  */
       
    78 NmMessagePart::NmMessagePart()
       
    79 {
       
    80     d = new NmMessagePartPrivate();
       
    81 }
       
    82 
       
    83 /*!
       
    84     Constructs message part with id \a id, parent id and mailbox id is set null
       
    85  */
       
    86 NmMessagePart::NmMessagePart(const NmId &id)
       
    87 {
       
    88     d = new NmMessagePartPrivate();
       
    89     d->mOwnId = id;
       
    90 }
       
    91 
       
    92 /*!
       
    93     Constructs message part with id \a id and with parent id \a parentId,
       
    94     mailbox id is set to null
       
    95  */
       
    96 NmMessagePart::NmMessagePart(const NmId &id, const NmId &parentId)
       
    97 {
       
    98     d = new NmMessagePartPrivate();
       
    99     d->mOwnId = id;
       
   100     mEnvelope.setParentId(parentId);
       
   101 }
       
   102 
       
   103 /*!
       
   104     Constructs message part with id \a id, with parent id \a parentId and
       
   105     with mailbox id \a mailboxId
       
   106  */
       
   107 NmMessagePart::NmMessagePart(
       
   108     const NmId &id,
       
   109     const NmId &parentId,
       
   110     const NmId &mailboxId)
       
   111 {
       
   112 	d = new NmMessagePartPrivate();
       
   113 	d->mOwnId = id;
       
   114     mEnvelope.setParentId(parentId);
       
   115 	mEnvelope.setMailboxId(mailboxId);
       
   116 }
       
   117 
       
   118 /*!
       
   119 	Constructs new NmMessagePart object from private message part data
       
   120  */
       
   121 NmMessagePart::NmMessagePart(
       
   122         QExplicitlySharedDataPointer<NmMessagePartPrivate> nmPrivateMessagePart)
       
   123 {
       
   124 	d = nmPrivateMessagePart;
       
   125 }
       
   126 
       
   127 /*!
       
   128     Copy constructor
       
   129  */
       
   130 NmMessagePart::NmMessagePart(const NmMessagePart &part) : d(part.d)
       
   131 {
       
   132     mEnvelope.setParentId(part.mEnvelope.parentId());
       
   133     mEnvelope.setMailboxId(part.mEnvelope.mailboxId());
       
   134 }
       
   135 
       
   136 /*!
       
   137     Assignment operator
       
   138  */
       
   139 NmMessagePart &NmMessagePart::operator=(const NmMessagePart &part)
       
   140 {
       
   141 	if (this != &part) {
       
   142 		d = part.d;
       
   143 	    mEnvelope.setParentId(part.mEnvelope.parentId());
       
   144 	    mEnvelope.setMailboxId(part.mEnvelope.mailboxId());
       
   145 	}
       
   146 	return *this;
       
   147 }
       
   148 
       
   149 /*!
       
   150     Destructor of message part
       
   151  */
       
   152 NmMessagePart::~NmMessagePart()
       
   153 {
       
   154 	for (int i = 0; i < d->mChildParts.count(); i++) {
       
   155 		delete d->mChildParts[i];
       
   156 	}
       
   157 	d->mChildParts.clear();
       
   158 }
       
   159 
       
   160 /*!
       
   161     Retruns id of message part
       
   162  */
       
   163 NmId NmMessagePart::id() const
       
   164 {
       
   165     return d->mOwnId;
       
   166 }
       
   167 
       
   168 /*!
       
   169     Sets id of message part from \a id
       
   170  */
       
   171 void NmMessagePart::setId(const NmId &id)
       
   172 {
       
   173     d->mOwnId = id;
       
   174 }
       
   175 
       
   176 /*!
       
   177     Returns parts parent id
       
   178  */
       
   179 NmId NmMessagePart::parentId() const
       
   180 {
       
   181     return mEnvelope.parentId();
       
   182 }
       
   183 
       
   184 /*!
       
   185     Sets message part parent id from \a id
       
   186  */
       
   187 void NmMessagePart::setParentId(const NmId &id)
       
   188 {
       
   189     mEnvelope.setParentId(id);
       
   190 }
       
   191 
       
   192 /*!
       
   193     Returns mailbox id where part belong to
       
   194  */
       
   195 NmId NmMessagePart::mailboxId() const
       
   196 {
       
   197     return mEnvelope.mailboxId();
       
   198 }
       
   199 
       
   200 /*!
       
   201     Sets mailbox id from \a id
       
   202  */
       
   203 void NmMessagePart::setMailboxId(const NmId &id)
       
   204 {
       
   205     mEnvelope.setMailboxId(id);
       
   206 }
       
   207 
       
   208 /*!
       
   209     Returns message part size in bytes
       
   210  */
       
   211 quint32 NmMessagePart::size() const
       
   212 {
       
   213     return d->mSize;
       
   214 }
       
   215 
       
   216 /*!
       
   217     Sets message part size, \a size should be in bytes
       
   218  */
       
   219 void NmMessagePart::setSize(quint32 size)
       
   220 {
       
   221     d->mSize = size;
       
   222 }
       
   223 
       
   224 /*!
       
   225     Returns fetched size of message part in bytes
       
   226  */
       
   227 quint32 NmMessagePart::fetchedSize() const
       
   228 {
       
   229     return d->mFetchedSize;
       
   230 }
       
   231 
       
   232 /*!
       
   233     Sets fetched size of message part, \a fetchedSize should be in bytes
       
   234  */
       
   235 void NmMessagePart::setFetchedSize(quint32 fetchedSize)
       
   236 {
       
   237     d->mFetchedSize = fetchedSize;
       
   238 }
       
   239 
       
   240 /*!
       
   241 	Returns true if message part contains mail message (message-in-message)
       
   242  */
       
   243 bool NmMessagePart::isMessage() const
       
   244 {
       
   245 	return d->isMessage();
       
   246 }
       
   247 
       
   248 /*!
       
   249     Sets textual content to part, \a contentType must be rfc 2045 (MIME) style
       
   250     string eg. "text/plain" or "text/html"
       
   251  */
       
   252 void NmMessagePart::setTextContent(
       
   253     const QString &content,
       
   254     const QString &contentType)
       
   255 {
       
   256     d->mTextContent = content;
       
   257     d->mContentType = contentType;
       
   258     d->mBinaryContent.clear();
       
   259 }
       
   260 
       
   261 /*!
       
   262     Returns reference to text content.
       
   263  */
       
   264 const QString &NmMessagePart::textContent() const
       
   265 {
       
   266     return d->mTextContent;
       
   267 }
       
   268 
       
   269 /*!
       
   270     Sets content type string, \a contentType must be rfc 2045 (MIME) style
       
   271     string eg. "multipart/mixed"
       
   272  */
       
   273 void NmMessagePart::setContentType(const QString &contentType)
       
   274 {
       
   275     d->mContentType = contentType;
       
   276 }
       
   277 
       
   278 /*!
       
   279     Returns rfc 2045 (MIME) style content type string of message part
       
   280  */
       
   281 QString NmMessagePart::contentType() const
       
   282 {
       
   283     return d->mContentType;
       
   284 }
       
   285 
       
   286 /*!
       
   287     Sets rfc 2045 (MIME) content description string
       
   288  */
       
   289 void NmMessagePart::setContentDescription(const QString &contentDescription)
       
   290 {
       
   291     d->mContentDescription = contentDescription;
       
   292 }
       
   293 
       
   294 /*!
       
   295     Returns rfc 2045 (MIME) content description string
       
   296  */
       
   297 QString NmMessagePart::contentDescription() const
       
   298 {
       
   299     return d->mContentDescription;
       
   300 }
       
   301 
       
   302 /*!
       
   303     Sets rfc 2045 (MIME) content disposition string
       
   304  */
       
   305 void NmMessagePart::setContentDisposition(const QString &contentDisposition)
       
   306 {
       
   307     d->mContentDisposition = contentDisposition;
       
   308 }
       
   309 
       
   310 /*!
       
   311     Returns rfc 2045 (MIME) content disposition string
       
   312  */
       
   313 QString NmMessagePart::contentDisposition() const
       
   314 {
       
   315     return d->mContentDisposition;
       
   316 }
       
   317 
       
   318 /*!
       
   319     Sets rfc 2045 (MIME) content id string
       
   320  */
       
   321 void NmMessagePart::setContentId(const QString &contentId)
       
   322 {
       
   323     d->mContentId = contentId;
       
   324 }
       
   325 
       
   326 /*!
       
   327     Returns rfc 2045 (MIME) content id string
       
   328  */
       
   329 QString NmMessagePart::contentId()
       
   330 {
       
   331     return d->mContentId;
       
   332 }
       
   333 
       
   334 /*!
       
   335     Sets list of child message part. Ownership of part objects is transferred
       
   336     to this part object and child parts are deleted when this part is deleted
       
   337  */
       
   338 void NmMessagePart::setChildParts(QList<NmMessagePart*> parts)
       
   339 {
       
   340 
       
   341     if (d->mChildParts.count() > 0) {
       
   342         for (int i = 0; i < d->mChildParts.count(); i++) {
       
   343             delete d->mChildParts[i];
       
   344         }
       
   345     d->mChildParts.clear();
       
   346     }
       
   347     for (int i = 0; i < parts.count(); i++) {
       
   348         if (parts[i] != NULL) {
       
   349             parts[i]->setParentId(this->id());
       
   350             parts[i]->setMailboxId(this->mailboxId());
       
   351             d->mChildParts.append(parts[i]);
       
   352         }
       
   353     }
       
   354 }
       
   355 
       
   356 /*!
       
   357     Returns reference to child part list. NOTE: child part objects are
       
   358     modifiable
       
   359  */
       
   360 QList<NmMessagePart*>& NmMessagePart::childParts() const
       
   361 {
       
   362     return d->mChildParts;
       
   363 }
       
   364 
       
   365 /*!
       
   366     Adds one child part obeject end of child parts list. Ownership of part object
       
   367     is transferred to this part and part object is deleted in this part's
       
   368     destructor
       
   369  */
       
   370 void NmMessagePart::addChildPart(NmMessagePart *part)
       
   371 {
       
   372     if (!part) {
       
   373         return;
       
   374     }
       
   375     part->setParentId(this->id());
       
   376     part->setMailboxId(this->mailboxId());
       
   377     d->mChildParts.append(part);
       
   378 }
       
   379 
       
   380 /*!
       
   381     Removes and deletes child part which id is \a partId. If id is not found
       
   382     simply returns
       
   383  */
       
   384 void NmMessagePart::removeChildPart(const NmId &partId)
       
   385 {
       
   386     for (int i = 0; i < d->mChildParts.count(); i++) {
       
   387         if (d->mChildParts[i]->id() == partId) {
       
   388             delete d->mChildParts[i];
       
   389             d->mChildParts.removeAt(i);
       
   390             return;
       
   391         }
       
   392     }
       
   393 }
       
   394 
       
   395 /*!
       
   396     Removes and deletes all child part.
       
   397  */
       
   398 void NmMessagePart::removeAllChildParts()
       
   399 {
       
   400     while (!d->mChildParts.isEmpty()) {
       
   401         delete d->mChildParts.takeFirst();
       
   402     }
       
   403 }
       
   404 
       
   405 /*!
       
   406     Sets binary content to part
       
   407  */
       
   408 void NmMessagePart::setBinaryContent(
       
   409     const QByteArray &content,
       
   410     const QString &contentType)
       
   411 {
       
   412     d->mBinaryContent = content;
       
   413     d->mContentType = contentType;
       
   414     d->mTextContent.clear();
       
   415 }
       
   416 
       
   417 /*!
       
   418     Returns reference to binary content.
       
   419  */
       
   420 const QByteArray &NmMessagePart::binaryContent() const
       
   421 {
       
   422     return d->mBinaryContent;
       
   423 }
       
   424 
       
   425 /**
       
   426    * Sets attachment name for email part. Full path can be
       
   427    * given as input even though only filename is saved. 
       
   428    * 
       
   429    * @param filePath attachment name
       
   430    */      
       
   431 void NmMessagePart::setAttachmentName(const QString &filePath)
       
   432 {
       
   433     Q_UNUSED(filePath);
       
   434 }
       
   435 
       
   436 /*!
       
   437      * returns email part attachment name
       
   438      * @return attachment name
       
   439  */
       
   440 QString NmMessagePart::attachmentName() const
       
   441 {
       
   442     // Look first from Content-Type param "name"
       
   443     QString content = contentType();
       
   444     int ptr = content.indexOf(NmContentTypeParamName);
       
   445     if(ptr >= 0) {
       
   446         d->mAttachmentName = content.section('"',1,1);
       
   447         return d->mAttachmentName;
       
   448     }
       
   449     // then if Content-Disposition is "attachment" look from 
       
   450     // Content-Disposition param "filename" and
       
   451     content = contentDisposition();
       
   452     ptr = content.indexOf(NmContentDispAttachment);
       
   453     if(ptr >= 0) {
       
   454         ptr = content.indexOf(NmContentDispParamFilename);
       
   455         if(ptr > 0) {
       
   456             d->mAttachmentName = content.section('"',1,1);
       
   457             return d->mAttachmentName;
       
   458             }
       
   459         }
       
   460     // finally look if there is Content-Description.
       
   461     return d->mContentDescription;
       
   462 }
       
   463 
       
   464 /*!
       
   465     Recursive child part finder for NmMessage class
       
   466  */
       
   467 NmMessagePart *NmMessagePart::findContentPart(const QString &contentType)
       
   468 {
       
   469     NmMessagePart *ret = NULL;
       
   470 
       
   471     if (!d->mContentType.isEmpty() && d->mContentType.startsWith(contentType)) {
       
   472         ret = this;
       
   473     } else {
       
   474         for (int i = 0; !ret && i < d->mChildParts.count(); i++) {
       
   475             ret = d->mChildParts[i]->findContentPart(contentType);
       
   476         }
       
   477     }
       
   478     return ret;
       
   479 }
       
   480 
       
   481 /**
       
   482  * creates a list containing all email / email part subparts
       
   483  * 
       
   484  * @param email part list given/owned by user
       
   485  */
       
   486 void NmMessagePart::appendAttachments(QList<NmMessagePart*> &attachments) const
       
   487 {
       
   488     QList<NmMessagePart*> messageParts = childParts();
       
   489 
       
   490     int messagePartCount = messageParts.count();
       
   491     for ( int i = 0; i < messagePartCount; i++ ) {
       
   492         QString contentType = messageParts[i]->contentType();
       
   493         if (!contentType.compare(NmContentTypeMultipartMixed) ||
       
   494             !contentType.compare(NmContentTypeMultipartAlternative) ||
       
   495             !contentType.compare(NmContentTypeMultipartDigest) ||
       
   496             !contentType.compare(NmContentTypeMultipartRelated) ||
       
   497             !contentType.compare(NmContentTypeMultipartParallel)) {
       
   498             // get multipart message subparts
       
   499             messageParts[i]->appendAttachments(attachments);
       
   500         }
       
   501         else {
       
   502             attachments.append( messageParts[i] );
       
   503         }
       
   504     }
       
   505 }