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