qtmobility/src/messaging/qmessage.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
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 #ifdef Q_OS_SYMBIAN
       
    43 #include "qmessage_symbian_p.h"
       
    44 #else
       
    45 #include "qmessage_p.h"
       
    46 #endif
       
    47 
       
    48 #include <QTextCodec>
       
    49 #include <QDebug>
       
    50 
       
    51 QTM_BEGIN_NAMESPACE
       
    52 
       
    53 namespace {
       
    54 
       
    55 QList<QByteArray> charsets;
       
    56 
       
    57 }
       
    58 
       
    59 /*!
       
    60     \class QMessage
       
    61 
       
    62     \preliminary
       
    63     \brief The QMessage class provides a convenient interface for working with messages.
       
    64     
       
    65     \ingroup messaging
       
    66    
       
    67     QMessage supports a number of types. Including internet email messages, 
       
    68     the telephony types SMS and MMS, and also XMPP messages.
       
    69      
       
    70     The QMessageId identifier for a message is returned by id(). Messages can be constructed by 
       
    71     retrieval from the messaging store via their identifier using QMessageManager::message(). A 
       
    72     QMessage can also be constructed piece by piece using functions such as 
       
    73     setType(), setFrom(), setTo(), setSubject(), setBody() and appendAttachments(). 
       
    74     
       
    75     If a message has been modified since it was last constructed isModified() returns true.
       
    76 
       
    77     A list of attachments identifiers will be returned by attachmentIds() and an identifier for the 
       
    78     message body will be returned by bodyId(). Attachments can be appended to the content of the 
       
    79     message using appendAttachments(), the body of the message can be set with setBody().
       
    80     
       
    81     The folder and account a message is associated with are returned by parentFolderId() and
       
    82     parentAccountId() respectively.
       
    83 
       
    84     Message data that is less commonly accessed or relatively large should be lazily retrieved 
       
    85     on demand by the associated getter function.
       
    86         
       
    87     Only phone numbers are valid destination addresses for SMS messages, only email addresses are valid
       
    88     destination addresses for Email messages, MMS messages may be addressed to either phone numbers
       
    89     or email addresses. Only XMPP addresses are valid destination addresses for XMPP messages.
       
    90     
       
    91     \sa QMessageContentContainer, QMessageManager, QMessageId
       
    92 */
       
    93 
       
    94 /*!
       
    95     \enum QMessage::Type
       
    96 
       
    97     This enum type is used to describe the type of a message.
       
    98     
       
    99     \value NoType   The message type is not defined.
       
   100     \value Mms      The message is an MMS, Multimedia Messaging Service object.
       
   101     \value Sms      The message is an SMS, Short Message Service object.
       
   102     \value Email    The message is an Email, Internet Message Format object.
       
   103     \value Xmpp     The message is an XMPP, Extensible Messaging and Presence Protocol object.
       
   104     \value AnyType  Bitflag value that matches any message type defined.
       
   105     
       
   106     \sa type(), setType()
       
   107 */
       
   108     
       
   109 /*!
       
   110     \enum QMessage::Status
       
   111 
       
   112     This enum type is used to describe the status of a message.
       
   113 
       
   114     \value Read            This flag indicates that the content of this message has been displayed to the user.
       
   115     \value HasAttachments  This flag indicates that the message contains at least one sub-part with 'Attachment' disposition.
       
   116     \value Incoming        This flag indicates that the message has been sent from an external source.
       
   117     \value Removed         This flag indicates that the message has been deleted from or moved on the originating server.
       
   118     
       
   119     \sa status(), setStatus()
       
   120 */
       
   121 
       
   122 /*!
       
   123     \enum QMessage::Priority
       
   124 
       
   125     Defines the priority of a message.
       
   126 
       
   127     \value HighPriority    The message is high priority.
       
   128     \value NormalPriority  The message is normal priority.
       
   129     \value LowPriority     The message is low priority.
       
   130 */
       
   131 
       
   132 /*!
       
   133     \enum QMessage::StandardFolder
       
   134 
       
   135     Defines the standard folders.
       
   136 
       
   137     \value InboxFolder   Represents the standard inbox folder.
       
   138     \value DraftsFolder  Represents the standard drafts folder.
       
   139     \value OutboxFolder  Represents the standard outbox folder.
       
   140     \value SentFolder    Represents the standard sent folder.
       
   141     \value TrashFolder   Represents the standard trash folder.
       
   142 */
       
   143 
       
   144 /*!
       
   145     \enum QMessage::ResponseType
       
   146 
       
   147     Defines the type of a response to an existing message.
       
   148 
       
   149     \value ReplyToSender    A response to the sender of the existing message.
       
   150     \value ReplyToAll       A response to the sender of the existing message, and any other recipients of that message.
       
   151     \value Forward          A response created to copy the content of the existing message to a new recipient.
       
   152 */
       
   153 
       
   154 /*!
       
   155     \fn QMessage::QMessage()
       
   156     
       
   157     Constructs an empty message.
       
   158 */
       
   159 
       
   160 /*!
       
   161     \fn QMessage::QMessage(const QMessageId& id)
       
   162 
       
   163     Constructs a message from data stored in the messaging store with identifier \a id.
       
   164     
       
   165     \sa QMessageManager::message()
       
   166 */
       
   167 
       
   168 /*!
       
   169     \fn QMessage::QMessage(const QMessage &other)
       
   170 
       
   171     Constructs a copy of \a other.
       
   172 */
       
   173 
       
   174 /*!
       
   175     \internal
       
   176     \fn QMessage::operator=(const QMessage& other)
       
   177 */
       
   178 
       
   179 /*!
       
   180     \fn QMessage::~QMessage()
       
   181     
       
   182     Destroys the message.
       
   183 */
       
   184 
       
   185 /*!
       
   186     \fn QMessage::id() const
       
   187   
       
   188     Returns the identifier of the message.
       
   189 
       
   190     \sa QMessageFilter::byId()
       
   191 */
       
   192     
       
   193 /*!
       
   194     \fn QMessage::type() const
       
   195     
       
   196     Returns the Type of the message.
       
   197     
       
   198     \sa setType(), QMessageFilter::byType()
       
   199 */
       
   200     
       
   201 /*!
       
   202     \fn QMessage::setType(Type t)
       
   203     
       
   204     Sets the Type of the message to \a t.
       
   205     
       
   206     The type of a message may be set for non-empty messages.
       
   207     
       
   208     \sa type()
       
   209 */
       
   210 
       
   211 /*!
       
   212     \fn QMessage::parentAccountId() const
       
   213     
       
   214     Returns the identifier of the parent account of the message if any; otherwise returns an 
       
   215     invalid identifier.
       
   216 */
       
   217     
       
   218 /*!
       
   219     \fn QMessage::setParentAccountId(const QMessageAccountId &accountId)
       
   220     
       
   221     Sets the parent account of the message to the account with identifier \a accountId.
       
   222     
       
   223     This operation is only permitted on new messages that have not yet been inserted into
       
   224     the message store. Attempting to change the parent account of a message already
       
   225     in the message store will result in an error when attempting to update the message
       
   226     with QMessageStore::update().
       
   227 */
       
   228     
       
   229 /*!
       
   230     \fn QMessage::parentFolderId() const
       
   231   
       
   232     Returns the identifier of the folder that contains the message if any; otherwise returns an 
       
   233     invalid identifier.
       
   234 */
       
   235 
       
   236 /*!
       
   237     \fn QMessage::standardFolder() const
       
   238   
       
   239     Returns the standard folder of the message.
       
   240     
       
   241     Defaults to InboxFolder.
       
   242 */
       
   243     
       
   244 /*!
       
   245     \fn QMessage::from() const
       
   246   
       
   247     Returns the originating address of the message.
       
   248 
       
   249     \sa setFrom(), QMessageFilter::bySender()
       
   250 */
       
   251 
       
   252 /*!
       
   253     \fn QMessage::setFrom(const QMessageAddress &address)
       
   254   
       
   255     Sets the from address, that is the originating address of the message to \a address.
       
   256 
       
   257     \sa from()
       
   258 */
       
   259 
       
   260 /*!
       
   261     \fn QMessage::subject() const
       
   262   
       
   263     Returns the subject of the message, if present; otherwise returns a null string.
       
   264 
       
   265     \sa setSubject(), QMessageFilter::bySubject()
       
   266 */
       
   267 
       
   268 /*!
       
   269     \fn QMessage::setSubject(const QString &text)
       
   270     
       
   271     Sets the subject of the message to \a text.
       
   272     
       
   273     \sa subject()
       
   274 */
       
   275 
       
   276 /*!
       
   277     \fn QMessage::date() const
       
   278   
       
   279     Returns the timestamp contained in the origination date header field of the message, if present; 
       
   280     otherwise returns a null timestamp.
       
   281     
       
   282     \sa setDate(), QMessageFilter::byTimeStamp()
       
   283 */
       
   284 
       
   285 /*!
       
   286     \fn QMessage::setDate(const QDateTime &d)
       
   287   
       
   288     Sets the origination date header field specifying the timestamp of the message to \a d.
       
   289     
       
   290     \sa date()
       
   291 */
       
   292 
       
   293 /*!
       
   294     \fn QMessage::receivedDate() const
       
   295   
       
   296     Returns the timestamp placed in the message during reception by the device, if present;
       
   297     otherwise returns a null timestamp.
       
   298     
       
   299     \sa setReceivedDate(), QMessageFilter::byReceptionTimeStamp()
       
   300 */
       
   301 
       
   302 /*!
       
   303     \fn QMessage::setReceivedDate(const QDateTime &d)
       
   304   
       
   305     Sets the timestamp indicating the time of message reception by the device to \a d.
       
   306     
       
   307     \sa receivedDate()
       
   308 */
       
   309 
       
   310 /*! 
       
   311     \fn QMessage::to() const
       
   312   
       
   313     Returns the list of primary recipients for the message.
       
   314 
       
   315     \sa setTo(), QMessageFilter::byRecipients()
       
   316 */
       
   317 
       
   318 /*! 
       
   319     \fn QMessage::setTo(const QMessageAddressList& toList)
       
   320   
       
   321     Sets the list of primary recipients for the message to \a toList.
       
   322     
       
   323     \sa to()
       
   324 */
       
   325 
       
   326 /*! 
       
   327     \fn QMessage::setTo(const QMessageAddress& address)
       
   328   
       
   329     Sets the primary recipient for the message to \a address.
       
   330     
       
   331     \sa to()
       
   332 */
       
   333 
       
   334 /*!
       
   335     \fn QMessage::cc() const
       
   336   
       
   337     Returns the list of all the cc (carbon copy) recipients specified for the message.
       
   338 
       
   339     \sa to(), bcc(), setCc(), QMessageFilter::byRecipients()
       
   340 */  
       
   341 
       
   342 /*!
       
   343    \fn QMessage::setCc(const QMessageAddressList& ccList)
       
   344   
       
   345     Set the list of cc (carbon copy) recipients for the message to \a ccList.
       
   346 
       
   347     \sa cc(), setTo(), setBcc()
       
   348 */  
       
   349 
       
   350 /*!
       
   351     \fn QMessage::bcc() const
       
   352   
       
   353     Returns the list of all the bcc (blind carbon copy) recipients specified for the message.
       
   354 
       
   355     \sa to(), cc(), setBcc()
       
   356 */  
       
   357 
       
   358 /*!
       
   359     \fn QMessage::setBcc(const QMessageAddressList& bccList)
       
   360   
       
   361     Set the list of bcc (blind carbon copy) recipients for the message to \a bccList.
       
   362 
       
   363     \sa bcc(), setTo(), setCc()
       
   364 */  
       
   365 
       
   366 /*!
       
   367     \fn QMessage::status() const
       
   368     
       
   369     Returns the status flags value for the message.
       
   370 
       
   371     \sa setStatus(), QMessageFilter::byStatus()
       
   372 */
       
   373 
       
   374 /*!
       
   375     \fn QMessage::setStatus(QMessage::StatusFlags newStatus)
       
   376     
       
   377     Sets the status flags value for the message to \a newStatus.
       
   378 
       
   379     \sa status()
       
   380 */
       
   381 
       
   382 /*!
       
   383     \fn QMessage::setStatus(QMessage::Status flag, bool set)
       
   384     
       
   385     Sets the status flag \a flag for the message to have the value \a set.
       
   386 
       
   387     \sa status()
       
   388 */
       
   389 
       
   390 /*!
       
   391     \fn QMessage::priority() const
       
   392     
       
   393     Returns the priority of the message.
       
   394 
       
   395     The default is NormalPriority.
       
   396 
       
   397     \sa setPriority(), QMessageFilter::byPriority()
       
   398 */
       
   399 
       
   400 /*!
       
   401     \fn QMessage::setPriority(Priority newPriority)
       
   402     
       
   403     Sets the priority of the message to \a newPriority.
       
   404 
       
   405     \sa priority()
       
   406 */
       
   407 
       
   408 /*!
       
   409     \fn QMessage::size() const
       
   410     
       
   411     Returns the complete size of the message as indicated on the originating server.
       
   412     
       
   413     \sa QMessageFilter::bySize()
       
   414 */
       
   415 
       
   416 /*!
       
   417     \fn QMessage::bodyId() const
       
   418   
       
   419     Returns the identifier for the body content contained by the Message if a body exists; 
       
   420     otherwise returns an invalid identifier.
       
   421     
       
   422     \sa QMessageContentContainer, setBody()
       
   423 */
       
   424 
       
   425 /*!
       
   426     \fn QMessage::setBody(const QString &body, const QByteArray &mimeType)
       
   427   
       
   428     Sets the body text of the message to be the string \a body.
       
   429     
       
   430     The internet media (MIME) content type of the body is set to \a mimeType, if provided.
       
   431     If the \a mimeType is not specified, the content type will default to "text/plain", and
       
   432     the encoding charset will be as determined by preferredCharsets().
       
   433     
       
   434     \sa bodyId(), preferredCharsets()
       
   435 */
       
   436 
       
   437 /*!
       
   438     \fn QMessage::setBody(QTextStream &in, const QByteArray &mimeType)
       
   439   
       
   440     Sets the body text of the message to be the text read from the stream \a in.
       
   441     
       
   442     The internet media (MIME) content type of the body is set to \a mimeType, if provided.
       
   443     If the \a mimeType is not specified, the content type will default to "text/plain", and
       
   444     the encoding charset will be as determined by preferredCharsets().
       
   445     
       
   446     \sa bodyId(), preferredCharsets()
       
   447 */
       
   448 
       
   449 /*!
       
   450     \fn QMessage::attachmentIds() const
       
   451   
       
   452     Returns a list of attachment identifiers for the message.
       
   453 
       
   454     The body of the message will not be included in the list.
       
   455     
       
   456     \sa appendAttachments(), clearAttachments()
       
   457 */
       
   458 
       
   459 /*!
       
   460     \fn QMessage::appendAttachments(const QStringList &fileNames)
       
   461   
       
   462     Append the contents of the files specified by \a fileNames to the end of the list of 
       
   463     attachments for the message. The internet media (MIME) type of the attachments will be 
       
   464     determined by examining the files or file names.
       
   465 
       
   466     \sa attachmentIds(), clearAttachments()
       
   467 */
       
   468 
       
   469 /*!
       
   470     \fn QMessage::clearAttachments()
       
   471   
       
   472     Clears the list of attachments for the message, leaving only the message body, if any.
       
   473 
       
   474     \sa attachmentIds(), appendAttachments()
       
   475 */  
       
   476 
       
   477 /*!
       
   478     \fn QMessage::isModified() const
       
   479     
       
   480     Returns true if the message has been modified since it was constructed; 
       
   481     otherwise returns false.
       
   482 */
       
   483 
       
   484 /*!
       
   485     \fn QMessage::createResponseMessage(ResponseType type) const
       
   486 
       
   487     Creates a new message as a response to this message, with properties predetermined according to \a type.
       
   488   
       
   489     \sa QMessageService::compose()
       
   490 */
       
   491 
       
   492 /*!
       
   493     \fn QMessage::setPreferredCharsets(const QList<QByteArray> &charsetNames)
       
   494     
       
   495     Sets the ordered-by-preference list of names of charsets to use when encoding 
       
   496     unicode QString data to a serialized form to \a charsetNames.
       
   497 
       
   498     The set of valid charset names is returned by QTextCodec::availableCharsets().
       
   499 
       
   500     \sa preferredCharsets(), preferredCharsetFor(), setBody()
       
   501 */
       
   502 void QMessage::setPreferredCharsets(const QList<QByteArray> &charsetNames)
       
   503 {
       
   504     charsets = charsetNames;
       
   505 }
       
   506 
       
   507 /*!
       
   508     \fn QMessage::preferredCharsets()
       
   509     
       
   510     Returns an ordered-by-preference list of charset names to use when encoding 
       
   511     unicode QString data to a serialized form.
       
   512 
       
   513     \sa setPreferredCharsets(), preferredCharsetFor(), setBody()
       
   514 */
       
   515 QList<QByteArray> QMessage::preferredCharsets()
       
   516 {
       
   517     return charsets;
       
   518 }
       
   519 
       
   520 /*!
       
   521     Returns the first charset from the preferred list that is capable of encoding
       
   522     the content of \a text.
       
   523 
       
   524     \sa preferredCharsets(), setBody()
       
   525 */
       
   526 QByteArray QMessage::preferredCharsetFor(const QString &text)
       
   527 {
       
   528     QList<QTextCodec*> codecs;
       
   529     foreach (const QByteArray &name, charsets) {
       
   530         if (QTextCodec* codec = QTextCodec::codecForName(name)) {
       
   531             codecs.append(codec);
       
   532         } else {
       
   533             qWarning() << "No codec is available for:" << name;
       
   534         }
       
   535     }
       
   536 
       
   537     if (!codecs.isEmpty()) {
       
   538         // See if any of these codecs can encode the data
       
   539         QString::const_iterator sit = text.begin(), end = text.end();
       
   540         for ( ; sit != end; ++sit) {
       
   541             QList<QTextCodec*>::iterator cit = codecs.begin();
       
   542             if (!(*cit)->canEncode(*sit)) {
       
   543                 // This codec is not acceptable
       
   544                 cit = codecs.erase(cit);
       
   545                 if (codecs.isEmpty()) {
       
   546                     break;
       
   547                 }
       
   548             } else {
       
   549                 ++cit;
       
   550             }
       
   551         }
       
   552 
       
   553         if (!codecs.isEmpty()) {
       
   554             // Return the first remaining codec
       
   555             return codecs.first()->name();
       
   556         }
       
   557     }
       
   558 
       
   559     return QByteArray();
       
   560 }
       
   561 
       
   562 QTM_END_NAMESPACE