qtmobility/examples/querymessages/main.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 
       
    42 #include <QCoreApplication>
       
    43 #include <qmessagemanager.h>
       
    44 #include <qdebug.h>
       
    45 #ifdef Q_OS_SYMBIAN
       
    46 #include <iostream>
       
    47 #endif
       
    48 
       
    49 QTM_USE_NAMESPACE
       
    50 
       
    51 static void printMessage(const QString& msg)
       
    52 {
       
    53 #ifdef Q_OS_SYMBIAN
       
    54     std::cout << qPrintable(msg) << std::endl;
       
    55 #else
       
    56     qDebug() << qPrintable(msg);
       
    57 #endif
       
    58 }
       
    59 
       
    60 int main(int argc, char *argv[])
       
    61 {
       
    62     QCoreApplication app(argc, argv);
       
    63 
       
    64     printMessage("Querying messages...");
       
    65 
       
    66 //! [setup-query]
       
    67     // Match all messages whose status field includes the Incoming flag
       
    68     QMessageFilter filter(QMessageFilter::byStatus(QMessage::Incoming));
       
    69 
       
    70     // Order the matching results by their reception timestamp, in descending order
       
    71     QMessageSortOrder sortOrder(QMessageSortOrder::byReceptionTimeStamp(Qt::DescendingOrder));
       
    72 //! [setup-query]
       
    73 
       
    74 //! [perform-query]
       
    75     // Acquire a handle to the message manager
       
    76     QMessageManager manager;
       
    77 
       
    78     // Find the matching message IDs, limiting our results to a managable number
       
    79     const int max = 100;
       
    80     const QMessageIdList matchingIds(manager.queryMessages(filter, sortOrder, max));
       
    81 //! [perform-query]
       
    82 
       
    83     int n = 0;
       
    84 
       
    85 //! [iterate-results]
       
    86     // Retrieve each message and print requested details
       
    87     foreach (const QMessageId &id, matchingIds) {
       
    88         QMessage message(manager.message(id));
       
    89 //! [iterate-results]
       
    90 
       
    91         if (manager.error() == QMessageManager::NoError) {
       
    92             QStringList result;
       
    93 
       
    94             if (app.arguments().count() < 2) {
       
    95                 // Default to printing only the subject
       
    96                 result.append(message.subject());
       
    97             } else {
       
    98 //! [generate-output]
       
    99                 // Extract the requested data items from this message
       
   100                 foreach (const QString &arg, app.arguments().mid(1)) {
       
   101                     if (arg == "subject") {
       
   102                         result.append(message.subject());
       
   103                     } else if (arg == "date") {
       
   104                         result.append(message.date().toLocalTime().toString());
       
   105 //! [generate-output]
       
   106                     } else if (arg == "receivedDate") {
       
   107                         result.append(message.receivedDate().toLocalTime().toString());
       
   108                     } else if (arg == "size") {
       
   109                         result.append(QString::number(message.size()));
       
   110                     } else if (arg == "priority") {
       
   111                         result.append(message.priority() == QMessage::HighPriority ? "High" : (message.priority() == QMessage::LowPriority ? "Low" : "Normal"));
       
   112                     } else if ((arg == "to") || (arg == "cc") || (arg == "bcc")) {
       
   113                         QStringList addresses;
       
   114                         foreach (const QMessageAddress &addr, (arg == "to" ? message.to() : (arg == "cc" ? message.cc() : message.bcc()))) {
       
   115                             addresses.append(addr.recipient());
       
   116                         }
       
   117                         result.append(addresses.join(","));
       
   118                     } else if (arg == "from") {
       
   119                         result.append(message.from().recipient());
       
   120                     } else if (arg == "type") {
       
   121                         result.append(message.contentType() + '/' + message.contentSubType());
       
   122                     } else if (arg == "body") {
       
   123                         result.append(message.find(message.bodyId()).textContent());
       
   124                     } else if (arg == "attachments") {
       
   125                         QStringList fileNames;
       
   126                         foreach (const QMessageContentContainerId &id, message.attachmentIds()) {
       
   127                             fileNames.append(message.find(id).suggestedFileName());
       
   128                         }
       
   129                         result.append(fileNames.join(","));
       
   130                     }
       
   131                 }
       
   132             }
       
   133 
       
   134 //! [print-result]
       
   135             printMessage(QString::number(++n) + '\t' + result.join("\t"));
       
   136 //! [print-result]
       
   137         }
       
   138     }
       
   139 
       
   140     if(matchingIds.isEmpty())
       
   141         printMessage("No matching messages!");
       
   142 
       
   143     return 0;
       
   144 }
       
   145