examples/querymessages/main.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:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include <QCoreApplication>
       
    42 #include <qmessagemanager.h>
       
    43 #include <qdebug.h>
       
    44 #ifdef Q_OS_SYMBIAN
       
    45 #include <iostream>
       
    46 #endif
       
    47 
       
    48 QTM_USE_NAMESPACE
       
    49 
       
    50 static void printMessage(const QString& msg)
       
    51 {
       
    52 #ifdef Q_OS_SYMBIAN
       
    53     std::cout << qPrintable(msg) << std::endl;
       
    54 #else
       
    55     qDebug() << qPrintable(msg);
       
    56 #endif
       
    57 }
       
    58 
       
    59 int main(int argc, char *argv[])
       
    60 {
       
    61     QCoreApplication app(argc, argv);
       
    62 
       
    63     printMessage("Querying messages...");
       
    64 
       
    65 //! [setup-query]
       
    66     // Match all messages whose status field includes the Incoming flag
       
    67     QMessageFilter filter(QMessageFilter::byStatus(QMessage::Incoming));
       
    68 
       
    69     // Order the matching results by their reception timestamp, in descending order
       
    70     QMessageSortOrder sortOrder(QMessageSortOrder::byReceptionTimeStamp(Qt::DescendingOrder));
       
    71 //! [setup-query]
       
    72 
       
    73 //! [perform-query]
       
    74     // Acquire a handle to the message manager
       
    75     QMessageManager manager;
       
    76 
       
    77     // Find the matching message IDs, limiting our results to a managable number
       
    78     const int max = 100;
       
    79     const QMessageIdList matchingIds(manager.queryMessages(filter, sortOrder, max));
       
    80 //! [perform-query]
       
    81 
       
    82     int n = 0;
       
    83 
       
    84 //! [iterate-results]
       
    85     // Retrieve each message and print requested details
       
    86     foreach (const QMessageId &id, matchingIds) {
       
    87         QMessage message(manager.message(id));
       
    88 //! [iterate-results]
       
    89 
       
    90         if (manager.error() == QMessageManager::NoError) {
       
    91             QStringList result;
       
    92 
       
    93             if (app.arguments().count() < 2) {
       
    94                 // Default to printing only the subject
       
    95                 result.append(message.subject());
       
    96             } else {
       
    97 //! [generate-output]
       
    98                 // Extract the requested data items from this message
       
    99                 foreach (const QString &arg, app.arguments().mid(1)) {
       
   100                     if (arg == "subject") {
       
   101                         result.append(message.subject());
       
   102                     } else if (arg == "date") {
       
   103                         result.append(message.date().toLocalTime().toString());
       
   104 //! [generate-output]
       
   105                     } else if (arg == "receivedDate") {
       
   106                         result.append(message.receivedDate().toLocalTime().toString());
       
   107                     } else if (arg == "size") {
       
   108                         result.append(QString::number(message.size()));
       
   109                     } else if (arg == "priority") {
       
   110                         result.append(message.priority() == QMessage::HighPriority ? "High" : (message.priority() == QMessage::LowPriority ? "Low" : "Normal"));
       
   111                     } else if ((arg == "to") || (arg == "cc") || (arg == "bcc")) {
       
   112                         QStringList addresses;
       
   113                         foreach (const QMessageAddress &addr, (arg == "to" ? message.to() : (arg == "cc" ? message.cc() : message.bcc()))) {
       
   114                             addresses.append(addr.addressee());
       
   115                         }
       
   116                         result.append(addresses.join(","));
       
   117                     } else if (arg == "from") {
       
   118                         result.append(message.from().addressee());
       
   119                     } else if (arg == "type") {
       
   120                         result.append(message.contentType() + '/' + message.contentSubType());
       
   121                     } else if (arg == "body") {
       
   122                         result.append(message.find(message.bodyId()).textContent());
       
   123                     } else if (arg == "attachments") {
       
   124                         QStringList fileNames;
       
   125                         foreach (const QMessageContentContainerId &id, message.attachmentIds()) {
       
   126                             fileNames.append(message.find(id).suggestedFileName());
       
   127                         }
       
   128                         result.append(fileNames.join(","));
       
   129                     }
       
   130                 }
       
   131             }
       
   132 
       
   133 //! [print-result]
       
   134             printMessage(QString::number(++n) + '\t' + result.join("\t"));
       
   135 //! [print-result]
       
   136         }
       
   137     }
       
   138 
       
   139     if(matchingIds.isEmpty())
       
   140         printMessage("No matching messages!");
       
   141 
       
   142     return 0;
       
   143 }
       
   144