qtmobility/src/messaging/qmessage_symbian.cpp
changeset 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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 "qmessage.h"
       
    42 #include "qmessage_symbian_p.h"
       
    43 #include "qmessagecontentcontainer_symbian_p.h"
       
    44 #include "qmessagemanager.h"
       
    45 
       
    46 
       
    47 QTM_BEGIN_NAMESPACE
       
    48 
       
    49 QMessagePrivate::QMessagePrivate(QMessage *message)
       
    50  : q_ptr(message),
       
    51    _type(QMessage::NoType),
       
    52    _size(0),
       
    53    _standardFolder(QMessage::DraftsFolder),
       
    54    _status(0),
       
    55    _priority(QMessage::NormalPriority),
       
    56    _modified(false)
       
    57 {
       
    58 }
       
    59 
       
    60 QMessagePrivate::~QMessagePrivate()
       
    61 {
       
    62 }
       
    63 
       
    64 QString QMessagePrivate::senderName(const QMessage &message)
       
    65 {
       
    66     return message.d_ptr->_senderName;
       
    67 }
       
    68 
       
    69 
       
    70 void QMessagePrivate::setSenderName(const QMessage &message, const QString &senderName)
       
    71 {
       
    72     message.d_ptr->_senderName = senderName;
       
    73 }
       
    74 
       
    75 void QMessagePrivate::setSize(const QMessage &message, int size)
       
    76 {
       
    77     message.d_ptr->_size = size;
       
    78 }
       
    79 
       
    80 void QMessagePrivate::setStandardFolder(QMessage& message, QMessage::StandardFolder sf)
       
    81 {
       
    82     message.d_ptr->_standardFolder = sf;
       
    83     message.d_ptr->_modified = true;
       
    84 }
       
    85 
       
    86 QMessagePrivate* QMessagePrivate::implementation(const QMessage &message)
       
    87 {
       
    88     return message.d_ptr;
       
    89 }
       
    90 
       
    91 QMessageContentContainerPrivate* QMessagePrivate::containerImplementation(const QMessage &message)
       
    92 {
       
    93     return ((QMessageContentContainer*)&message)->d_ptr;
       
    94 }
       
    95 
       
    96 QMessage::QMessage()
       
    97  : d_ptr(new QMessagePrivate(this))
       
    98 {
       
    99 	setDerivedMessage(this);
       
   100 	d_ptr->_size = 0;
       
   101 	d_ptr->_modified = false;
       
   102 }
       
   103 
       
   104 QMessage::QMessage(const QMessageId& id)
       
   105  : d_ptr(new QMessagePrivate(this))
       
   106 {
       
   107 	*this = QMessageManager().message(id);
       
   108 	setDerivedMessage(this);	
       
   109 }
       
   110 
       
   111 QMessage::QMessage(const QMessage &other)
       
   112  :QMessageContentContainer(other),
       
   113     d_ptr(new QMessagePrivate(this))
       
   114 {
       
   115 	this->operator=(other);
       
   116 }
       
   117 
       
   118 QMessage& QMessage::operator=(const QMessage& other)
       
   119 {
       
   120     if (&other != this) {
       
   121         QMessageContentContainer::operator=(other);
       
   122         *d_ptr = *other.d_ptr;
       
   123         setDerivedMessage(this);
       
   124     }
       
   125     
       
   126     return *this;
       
   127 }
       
   128 
       
   129 QMessage::~QMessage()
       
   130 {
       
   131 	delete d_ptr;	
       
   132 }
       
   133 
       
   134 QMessageId QMessage::id() const
       
   135 {
       
   136 	return d_ptr->_id;
       
   137 }
       
   138 
       
   139 QMessage::Type QMessage::type() const
       
   140 {
       
   141 	return d_ptr->_type;
       
   142 }
       
   143 
       
   144 void QMessage::setType(Type t)
       
   145 {
       
   146 	d_ptr->_type = t;
       
   147 	d_ptr->_modified = true;
       
   148 }
       
   149 
       
   150 QMessageAccountId QMessage::parentAccountId() const
       
   151 {
       
   152     return d_ptr->_parentAccountId;
       
   153 }
       
   154 
       
   155 void QMessage::setParentAccountId(const QMessageAccountId &accountId)
       
   156 {
       
   157     d_ptr->_parentAccountId = accountId;
       
   158     d_ptr->_modified = true;
       
   159 }
       
   160 
       
   161 QMessageFolderId QMessage::parentFolderId() const
       
   162 {
       
   163     return d_ptr->_parentFolderId;
       
   164 }
       
   165 
       
   166 QMessage::StandardFolder QMessage::standardFolder() const
       
   167 {
       
   168     return d_ptr->_standardFolder;
       
   169 }
       
   170 
       
   171 QMessageAddress QMessage::from() const
       
   172 {
       
   173 	return d_ptr->_from;
       
   174 }
       
   175 
       
   176 void QMessage::setFrom(const QMessageAddress &address)
       
   177 {
       
   178 	d_ptr->_modified = true;
       
   179 	d_ptr->_from = address;
       
   180     d_ptr->_modified = true;
       
   181 }
       
   182 
       
   183 QString QMessage::subject() const
       
   184 {
       
   185     return d_ptr->_subject;
       
   186 }
       
   187 
       
   188 void QMessage::setSubject(const QString &s)
       
   189 {
       
   190 	d_ptr->_modified = true;   
       
   191 	d_ptr->_subject = s;
       
   192     d_ptr->_modified = true;
       
   193 }
       
   194 
       
   195 QDateTime QMessage::date() const
       
   196 {
       
   197     return d_ptr->_date;
       
   198 }
       
   199 
       
   200 void QMessage::setDate(const QDateTime &d)
       
   201 {
       
   202 	d_ptr->_modified = true;
       
   203 	d_ptr->_date = d;
       
   204     d_ptr->_modified = true;
       
   205 }
       
   206 
       
   207 QDateTime QMessage::receivedDate() const
       
   208 {
       
   209     return d_ptr->_receivedDate;
       
   210 }
       
   211 
       
   212 void QMessage::setReceivedDate(const QDateTime &d)
       
   213 {
       
   214 	d_ptr->_modified = true;
       
   215     d_ptr->_receivedDate = d;
       
   216     d_ptr->_modified = true;
       
   217 }
       
   218 
       
   219 QMessageAddressList QMessage::to() const
       
   220 {
       
   221     return d_ptr->_toList;
       
   222 }
       
   223 
       
   224 void QMessage::setTo(const QMessageAddressList& toList)
       
   225 {
       
   226 	d_ptr->_modified = true;
       
   227     d_ptr->_toList = toList;
       
   228     d_ptr->_modified = true;
       
   229 }
       
   230 
       
   231 void QMessage::setTo(const QMessageAddress& address)
       
   232 {
       
   233 	d_ptr->_modified = true;
       
   234     d_ptr->_toList << address;
       
   235     d_ptr->_modified = true;
       
   236 }
       
   237 
       
   238 QMessageAddressList QMessage::cc() const
       
   239 {
       
   240 	return d_ptr->_ccList;
       
   241 }
       
   242 
       
   243 void QMessage::setCc(const QMessageAddressList& ccList)
       
   244 {
       
   245 	d_ptr->_modified = true;
       
   246 	d_ptr->_ccList = ccList;
       
   247     d_ptr->_modified = true;
       
   248 }
       
   249 
       
   250 QMessageAddressList QMessage::bcc() const
       
   251 {
       
   252     return d_ptr->_bccList;
       
   253 }
       
   254 
       
   255 void QMessage::setBcc(const QMessageAddressList& bccList)
       
   256 {
       
   257 	d_ptr->_modified = true;
       
   258 	d_ptr->_bccList = bccList;
       
   259     d_ptr->_modified = true;
       
   260 }
       
   261 
       
   262 QMessage::StatusFlags QMessage::status() const
       
   263 {
       
   264     return d_ptr->_status;
       
   265 }
       
   266 
       
   267 void QMessage::setStatus(QMessage::StatusFlags newStatus)
       
   268 {
       
   269 	d_ptr->_modified = true;
       
   270     d_ptr->_status = newStatus;
       
   271     d_ptr->_modified = true;
       
   272 }
       
   273 
       
   274 void QMessage::setStatus(QMessage::Status flag, bool set)
       
   275 {
       
   276 	d_ptr->_modified = true;
       
   277 	if (set) {
       
   278 		d_ptr->_status |= flag;
       
   279 	} else {
       
   280 		d_ptr->_status &= ~flag;
       
   281 	}
       
   282     d_ptr->_modified = true;
       
   283 }
       
   284 
       
   285 QMessage::Priority QMessage::priority() const
       
   286 {
       
   287     return d_ptr->_priority;
       
   288 }
       
   289 
       
   290 void QMessage::setPriority(Priority newPriority)
       
   291 {
       
   292 	d_ptr->_modified = true;
       
   293     d_ptr->_priority = newPriority;
       
   294     d_ptr->_modified = true;
       
   295 }
       
   296 
       
   297 int QMessage::size() const
       
   298 {
       
   299     int size = 0;
       
   300     if (d_ptr->_size != 0) {
       
   301         size = d_ptr->_size;
       
   302     } else {
       
   303         QMessageContentContainerPrivate *container(((QMessageContentContainer *)(this))->d_ptr);
       
   304         if (container->_size != 0) {
       
   305             size += container->_size;
       
   306         }
       
   307         foreach (const QMessageContentContainer &attachment, container->_attachments) {
       
   308             size += attachment.size();
       
   309         }
       
   310     }
       
   311     return size;
       
   312 }
       
   313 
       
   314 QMessageContentContainerId QMessage::bodyId() const
       
   315 {
       
   316     // TODO: Example body finding algorithm.
       
   317     // If the content type of the message is text, then that is the body
       
   318     // otherwise if the first part of the body is text then that is the body.
       
   319     
       
   320     return d_ptr->_bodyId;
       
   321 }
       
   322 
       
   323 void QMessage::setBody(const QString &body, const QByteArray &mimeType)
       
   324 {
       
   325     QByteArray mainType("text");
       
   326     QByteArray subType("plain");   
       
   327     QByteArray charset;
       
   328     
       
   329     QString mime = QString(mimeType);
       
   330 
       
   331 	int index = mimeType.indexOf("/");
       
   332 	if (index != -1) {
       
   333 		mainType = mimeType.left(index).trimmed();
       
   334 
       
   335 		subType = mimeType.mid(index + 1).trimmed();
       
   336 		index = subType.indexOf(";");
       
   337 		if (index != -1) {
       
   338 			 QString remainder = subType.mid(index + 1);
       
   339 			subType = subType.left(index).trimmed();
       
   340 
       
   341 			QRegExp charsetPattern("charset=(\\S+)");
       
   342 			index = charsetPattern.indexIn(remainder);
       
   343 			if (index != -1) {
       
   344 				charset = charsetPattern.cap(1).toLatin1();
       
   345 			}
       
   346 	   }
       
   347 	}
       
   348 
       
   349 	if (charset.isEmpty()) {
       
   350 		charset = QMessage::preferredCharsetFor(body);
       
   351 	    if (charset.isEmpty()) {
       
   352 	    charset = "UTF-8";
       
   353 	    }
       
   354 	}
       
   355     
       
   356     QMessageContentContainerPrivate *container(((QMessageContentContainer *)(this))->d_ptr);
       
   357 
       
   358     QMessageContentContainerId existingBodyId(bodyId());
       
   359     if (existingBodyId.isValid()) {
       
   360         if (existingBodyId == QMessageContentContainerPrivate::bodyContentId()) {
       
   361             // The body content is in the message itself
       
   362             container->setContent(body, mainType, subType, charset);
       
   363         } else {
       
   364             // The body content is in the first attachment
       
   365             QMessageContentContainerPrivate *attachmentContainer(container->attachment(existingBodyId)->d_ptr);
       
   366             attachmentContainer->setContent(body, mainType, subType, charset);
       
   367         }
       
   368     } else {
       
   369         if (container->_attachments.isEmpty()) {
       
   370             // Put the content directly into the message
       
   371             container->setContent(body, mainType, subType, charset);
       
   372             d_ptr->_bodyId = QMessageContentContainerPrivate::bodyContentId();
       
   373         } else {
       
   374             // Add the body as the first attachment
       
   375             QMessageContentContainer newBody;
       
   376             newBody.d_ptr->setContent(body, mainType, subType, charset);
       
   377             d_ptr->_bodyId = container->prependContent(newBody);
       
   378         }
       
   379     }
       
   380 
       
   381     d_ptr->_modified = true;
       
   382 }
       
   383 
       
   384 void QMessage::setBody(QTextStream &in, const QByteArray &mimeType)
       
   385 {
       
   386     setBody(in.readAll(), mimeType);
       
   387     d_ptr->_modified = true;
       
   388 }
       
   389 
       
   390 QMessageContentContainerIdList QMessage::attachmentIds() const
       
   391 {
       
   392 	QMessageContentContainerIdList ids;
       
   393 
       
   394 	QMessageContentContainerId msgBodyId(bodyId());
       
   395     foreach (const QMessageContentContainerId &contentId, contentIds()) {
       
   396         if (contentId != msgBodyId) {
       
   397             ids.append(contentId);
       
   398         }
       
   399     }
       
   400 	    
       
   401 	return ids;
       
   402 }
       
   403 
       
   404 void QMessage::appendAttachments(const QStringList &fileNames)
       
   405 {
       
   406 	if (!fileNames.isEmpty()) {
       
   407         d_ptr->_modified = true;
       
   408 
       
   409         QMessageContentContainerPrivate *container(((QMessageContentContainer *)(this))->d_ptr);
       
   410 
       
   411         if (container->_attachments.isEmpty()) {
       
   412             QMessageContentContainerId existingBodyId(bodyId());
       
   413             if (existingBodyId == QMessageContentContainerPrivate::bodyContentId()) {
       
   414                 // The body content is in the message itself - move it to become the first attachment
       
   415                 QMessageContentContainer newBody(*this);
       
   416                 newBody.setDerivedMessage(0);
       
   417 
       
   418                 container->setContentType("multipart", "mixed", "");
       
   419                 d_ptr->_bodyId = container->prependContent(newBody);
       
   420             } else {
       
   421                 // This message is now multipart
       
   422                 container->setContentType("multipart", "mixed", "");
       
   423             }
       
   424 
       
   425             container->_available = true;
       
   426         }
       
   427 
       
   428         foreach (const QString &filename, fileNames) {
       
   429             QMessageContentContainer attachment;
       
   430             if (attachment.d_ptr->createAttachment(filename)) {
       
   431                 container->appendContent(attachment);
       
   432             }
       
   433         }
       
   434 
       
   435         bool haveAttachments = !container->_attachments.isEmpty();
       
   436         setStatus(QMessage::HasAttachments,haveAttachments);
       
   437     }
       
   438     d_ptr->_modified = true;
       
   439 }
       
   440 
       
   441 void QMessage::clearAttachments()
       
   442 {
       
   443 	d_ptr->_modified = true;
       
   444 	QMessageContentContainerPrivate *container(((QMessageContentContainer *)(this))->d_ptr);
       
   445 	container->_attachments.clear();
       
   446 }
       
   447 
       
   448 bool QMessage::isModified() const
       
   449 {
       
   450 	return d_ptr->_modified;
       
   451 }
       
   452 
       
   453 QMessage QMessage::createResponseMessage(ResponseType type) const
       
   454 {
       
   455 	QMessage message;
       
   456 	message.setType(d_ptr->_type);
       
   457 	message.setParentAccountId(d_ptr->_parentAccountId);
       
   458 	message.setDate(QDateTime::currentDateTime());
       
   459 	
       
   460 	QString body;
       
   461 	QMessageContentContainerPrivate *container(((QMessageContentContainer *)(this))->d_ptr);
       
   462 	QMessageContentContainerId existingBodyId(bodyId());
       
   463 	if (existingBodyId.isValid()) {
       
   464 		if (existingBodyId == QMessageContentContainerPrivate::bodyContentId()) {
       
   465 			// The body content is in the message itself
       
   466 			body = textContent();
       
   467 			message.setBody(body);
       
   468 		} else {
       
   469 			// The body content is in the first attachment
       
   470 			QMessageContentContainerPrivate *attachmentContainer(container->attachment(existingBodyId)->d_ptr);
       
   471 			body = attachmentContainer->_textContent;
       
   472 			message.setBody(body);
       
   473 		}
       
   474 	}
       
   475 	
       
   476 	if (type == ReplyToSender) {
       
   477 		message.setTo(d_ptr->_from);
       
   478 		
       
   479 		QString subj = subject();
       
   480 		if (!subj.isEmpty()) {
       
   481 			subj.insert(0, "Re:");
       
   482 			message.setSubject(subj);
       
   483 		}
       
   484 		
       
   485 	} else if (type == ReplyToAll) {
       
   486 		QList<QMessageAddress> addressList;
       
   487 		addressList.append(d_ptr->_from);
       
   488 		message.setTo(addressList);
       
   489 		
       
   490 		QList<QMessageAddress> ccAddressList;
       
   491 		QMessageAddressList toList = to();
       
   492 		foreach(QMessageAddress address, toList) {
       
   493 			ccAddressList.append(address);
       
   494 		}	
       
   495 		QMessageAddressList ccList = cc();
       
   496 		foreach(QMessageAddress ccAddress, ccList) {
       
   497 			ccAddressList.append(ccAddress);
       
   498 		}
       
   499 		message.setCc(ccAddressList);
       
   500 		
       
   501 		QList<QMessageAddress> bccAddressList;
       
   502 		QMessageAddressList bccList = bcc();
       
   503 		foreach(QMessageAddress bccAddress, bccList) {
       
   504 			bccAddressList.append(bccAddress);
       
   505 		}
       
   506 		message.setBcc(bccAddressList);
       
   507 		
       
   508 		QString subj = subject();
       
   509 		if (!subj.isEmpty()) {
       
   510 			subj.insert(0, "Re:");
       
   511 			message.setSubject(subj);
       
   512 		}
       
   513 
       
   514 	} else if (type == Forward) {
       
   515 		QString subj = subject();
       
   516 		if (!subj.isEmpty()) {
       
   517 			subj.insert(0, "Fwd:");
       
   518 			message.setSubject(subj);
       
   519 		}
       
   520 
       
   521 		QMessageContentContainerIdList ids = attachmentIds();
       
   522 		QStringList containerList;
       
   523 		foreach (QMessageContentContainerId id, ids){
       
   524 			QMessageContentContainer container = find(id);
       
   525 			QByteArray filePath = QMessageContentContainerPrivate::attachmentFilename(container);			
       
   526 			containerList.append(QString(filePath));
       
   527 		}
       
   528 		message.appendAttachments(containerList);
       
   529 		
       
   530 	}	
       
   531     return message;
       
   532 }
       
   533 
       
   534 
       
   535 QTM_END_NAMESPACE