src/messaging/modestengine_maemo_p.h
changeset 0 876b1a06bc25
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 
       
    42 #ifndef MODESTENGINE_MAEMO_H
       
    43 #define MODESTENGINE_MAEMO_H
       
    44 
       
    45 #include "qmessagemanager.h"
       
    46 #include "qmessagefilter_p.h"
       
    47 #include "qmessageservice.h"
       
    48 
       
    49 #include "gconf/gconf-client.h"
       
    50 #include "libosso.h"
       
    51 
       
    52 #include <QMap>
       
    53 #include <QString>
       
    54 #include <QDBusArgument>
       
    55 #include <QDBusPendingCallWatcher>
       
    56 #include <QFileInfoList>
       
    57 #include <QThread>
       
    58 #include <QMutex>
       
    59 #include <QEventLoop>
       
    60 
       
    61 class QDBusInterface;
       
    62 class QFileSystemWatcher;
       
    63 class QEventLoop;
       
    64 
       
    65 QTM_BEGIN_NAMESPACE
       
    66 
       
    67 typedef QMap< QString, QString > ModestStringMap;
       
    68 typedef QList< ModestStringMap > ModestStringMapList;
       
    69 
       
    70 static const int maxCacheSize = 100000;
       
    71 
       
    72 class QMessageService;
       
    73 class QMessageServicePrivate;
       
    74 class QMessageStorePrivate;
       
    75 
       
    76 struct MessageQueryInfo
       
    77 {
       
    78     int queryId;
       
    79     QString body;
       
    80     QMessageDataComparator::MatchFlags matchFlags;
       
    81     QMessageFilter filter;
       
    82     QMessageSortOrder sortOrder;
       
    83     int limit;
       
    84     int offset;
       
    85     QMessageServicePrivate* privateService;
       
    86     QDBusPendingCallWatcher* pendingCallWatcher;
       
    87     int currentFilterListIndex;
       
    88     int handledFiltersCount;
       
    89     QMessageIdList ids;
       
    90     QString realAccountId;
       
    91     bool isQuery;
       
    92     bool returnWithSingleShot;
       
    93 };
       
    94 
       
    95 struct ModestUnreadMessageDBusStruct
       
    96 {
       
    97     qlonglong timeStamp;
       
    98     QString subject;
       
    99 };
       
   100 
       
   101 struct ModestAccountsUnreadMessagesDBusStruct
       
   102 {
       
   103     QString accountId;
       
   104     QString accountName;
       
   105     QString accountProtocol;
       
   106     qlonglong unreadCount;
       
   107     QList<ModestUnreadMessageDBusStruct> unreadMessages;
       
   108 };
       
   109 
       
   110 struct ModestMessage
       
   111 {
       
   112     QString id;
       
   113     QString subject;
       
   114     QString folder;
       
   115     QString sender;
       
   116     long long size;
       
   117     bool hasAttachment;
       
   118     bool isUnread;
       
   119     long long timeStamp;
       
   120 };
       
   121 
       
   122 typedef enum {
       
   123     MessagingModestFolderTypeUnknown,
       
   124     MessagingModestFolderTypeNormal,
       
   125     MessagingModestFolderTypeInbox,
       
   126     MessagingModestFolderTypeOutbox,
       
   127     MessagingModestFolderTypeTrash,
       
   128     MessagingModestFolderTypeJunk,
       
   129     MessagingModestFolderTypeSent,
       
   130     MessagingModestFolderTypeRoot,
       
   131     MessagingModestFolderTypeNotes,
       
   132     MessagingModestFolderTypeDrafts,
       
   133     MessagingModestFolderTypeContacts,
       
   134     MessagingModestFolderTypeCalendar,
       
   135     MessagingModestFolderTypeArchive,
       
   136     MessagingModestFolderTypeMerge,
       
   137     MessagingModestFolderTypeNum
       
   138 } MessagingModestFolderType;
       
   139 
       
   140 struct MessagingModestFolder
       
   141 {
       
   142     QString id;
       
   143     QString modestId;
       
   144     QString parentAccountId;
       
   145     QString parentFolderId;
       
   146     MessagingModestFolderType type;
       
   147     QString name;
       
   148     QString localizedName;
       
   149 };
       
   150 
       
   151 typedef enum {
       
   152     MessagingModestMessageNotDefined  = 0,
       
   153     MessagingModestMessageAnswered    = 1<<0,
       
   154     MessagingModestMessageDeleted     = 1<<1,
       
   155     MessagingModestMessageDraft       = 1<<2,
       
   156     MessagingModestMessageFlagged     = 1<<3,
       
   157     MessagingModestMessageSeen        = 1<<4,
       
   158     MessagingModestMessageAttachments = 1<<5,
       
   159     MessagingModestMessageCached      = 1<<6,
       
   160     MessagingModestMessagePartial     = 1<<7,
       
   161     MessagingModestMessageExpunged    = 1<<8,
       
   162     MessagingModestMessageHasPriority = 1<<9|1<<10
       
   163 } MessagingModestMessageFlags;
       
   164 
       
   165 typedef enum {
       
   166     MessagingModestMessagePriorityNotDefined = 0,
       
   167     MessagingModestMessageHighPriority       = 0<<9|1<<10,
       
   168     MessagingModestMessageNormalPriority     = 0<<9|0<<10,
       
   169     MessagingModestMessageLowPriority        = 1<<9|0<<10,
       
   170 } MessagingModestMessagePriority;
       
   171 
       
   172 struct MessagingModestMimePart
       
   173 {   
       
   174     QString mimeType;
       
   175     bool isAttachment;
       
   176     QString fileName;
       
   177     QString contentId;
       
   178 };
       
   179 
       
   180 struct MessagingModestMessage
       
   181 {
       
   182     QString id;
       
   183     QString url;
       
   184     QString accountId;
       
   185     QString folderId;
       
   186     QString mimeType;
       
   187     QString from;
       
   188     QString to;
       
   189     QString cc;
       
   190     QString bcc;
       
   191     QString replyTo;
       
   192     QString subject;
       
   193     qlonglong dateReceived;
       
   194     qlonglong dateSent;
       
   195     qlonglong size;
       
   196     MessagingModestMessageFlags flags;
       
   197     MessagingModestMessagePriority priority;
       
   198     QList<MessagingModestMimePart> mimeParts;
       
   199 };
       
   200 
       
   201 struct INotifyEvent
       
   202 {
       
   203     int watchDescriptor;
       
   204     uint32_t mask;
       
   205     QString fileName;
       
   206 };
       
   207 
       
   208 class INotifyWatcher : public QThread
       
   209 {
       
   210     Q_OBJECT
       
   211 
       
   212 public:
       
   213     enum FileNotification
       
   214     {
       
   215         FileNotificationAdded,
       
   216         FileNotificationUpdated,
       
   217         FileNotificationRemoved
       
   218     };
       
   219 
       
   220     INotifyWatcher();
       
   221     ~INotifyWatcher();
       
   222 
       
   223     void run();
       
   224     int addFile(const QString& path, uint eventsToWatch = 0);
       
   225     QStringList files() const;
       
   226     int addDirectory(const QString& path, uint eventsToWatch = 0);
       
   227     QStringList directories() const;
       
   228     void clear();
       
   229 
       
   230 private slots:
       
   231     void notifySlot();
       
   232 
       
   233 signals:
       
   234    void fileChanged(int watchDescriptor, QString filePath, uint events);
       
   235 
       
   236 private: //Data
       
   237     int m_inotifyFileDescriptor;
       
   238     QMutex m_mutex;
       
   239     QMap<int, QString> m_files;
       
   240     QMap<int, QString> m_dirs;
       
   241 };
       
   242 
       
   243 class ModestEngine : public QObject
       
   244 {
       
   245     Q_OBJECT
       
   246 
       
   247 public:
       
   248     enum EmailProtocol
       
   249     {
       
   250         EmailProtocolUnknown = -1,
       
   251         EmailProtocolPop3 = 1,
       
   252         EmailProtocolIMAP,
       
   253     };
       
   254 
       
   255     enum NotificationType
       
   256     {
       
   257         None = 0,
       
   258         Added,
       
   259         Updated,
       
   260         Removed
       
   261     };
       
   262 
       
   263     static ModestEngine* instance();
       
   264 
       
   265     ModestEngine();
       
   266     ~ModestEngine();
       
   267 
       
   268     bool exportUpdates(const QMessageAccountId &id);
       
   269 
       
   270     QMessageAccountIdList queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder,
       
   271                                         uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
       
   272     int countAccounts(const QMessageAccountFilter &filter) const;
       
   273     QMessageAccount account(const QMessageAccountId &id) const;
       
   274     QMessageAccountId defaultAccount() const;
       
   275 
       
   276     QMessageFolderIdList queryFolders(const QMessageFolderFilter &filter, const QMessageFolderSortOrder &sortOrder,
       
   277                                       uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
       
   278     int countFolders(const QMessageFolderFilter &filter) const;
       
   279     QMessageFolder folder(const QMessageFolderId &id) const;
       
   280 
       
   281     QMessage message(const QMessageId &id, bool useCache = true) const;
       
   282     bool addMessage(QMessage &message);
       
   283     bool updateMessage(QMessage &message);
       
   284     bool removeMessage(const QMessageId &id, QMessageManager::RemovalOption option);
       
   285 
       
   286     bool queryMessages(QMessageService& messageService, const QMessageFilter &filter,
       
   287                        const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
       
   288     bool queryMessages(QMessageService& messageService, const QMessageFilter &filter,
       
   289                        const QString &body, QMessageDataComparator::MatchFlags matchFlags,
       
   290                        const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
       
   291     bool countMessages(QMessageService& messageService, const QMessageFilter &filter);
       
   292 
       
   293     QMessageIdList queryMessagesSync(const QMessageFilter &filter, const QMessageSortOrder &sortOrder,
       
   294                                      uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
       
   295     QMessageIdList queryMessagesSync(const QMessageFilter &filter, const QString &body,
       
   296                                      QMessageDataComparator::MatchFlags matchFlags,
       
   297                                      const QMessageSortOrder &sortOrder, uint limit, uint offset,
       
   298                                      bool &isFiltered, bool &isSorted) const;
       
   299     int countMessagesSync(const QMessageFilter &filter) const;
       
   300 
       
   301     bool sendEmail(QMessage &message);
       
   302     bool composeEmail(const QMessage &message);
       
   303     bool showMessage(const QMessageId &id);
       
   304 
       
   305     QMessageManager::NotificationFilterId registerNotificationFilter(QMessageStorePrivate& messageStore,
       
   306                                                                      const QMessageFilter& filter,
       
   307                                                                      QMessageManager::NotificationFilterId id = 0);
       
   308     void unregisterNotificationFilter(QMessageManager::NotificationFilterId notificationFilterId);
       
   309     QByteArray getMimePart (const QMessageId &id, const QString &attachmentId);
       
   310 
       
   311     bool retrieveBody(QMessageService& messageService, const QMessageId &id);
       
   312     bool retrieve(QMessageService& messageService, const QMessageId &messageId, const QMessageContentContainerId &id, QMessage *msg = 0);
       
   313 
       
   314     void clearHeaderCache();
       
   315 
       
   316 private:
       
   317     QFileInfoList localFolders() const;
       
   318     void appendLocalSubFolders(QFileInfoList& fileInfoList, int startIndex) const;
       
   319     void appendIMAPSubFolders(QFileInfoList& fileInfoList, int startIndex) const;
       
   320     QFileInfoList accountFolders(QMessageAccountId& accountId) const;
       
   321     QString localRootFolder() const;
       
   322     QString accountRootFolder(QMessageAccountId& accountId) const;
       
   323     void foldersFromModest(QList<MessagingModestFolder>& folders) const;
       
   324     bool startObservingModestFolder(const QMessageFolderId& folderId) const;
       
   325     bool stopObservingModestFolder(const QMessageFolderId& folderId) const;
       
   326     EmailProtocol accountEmailProtocol(QMessageAccountId& accountId) const;
       
   327     QString accountEmailProtocolAsString(const QMessageAccountId& accountId) const;
       
   328     QString accountUsername(QMessageAccountId& accountId) const;
       
   329     QString accountHostname(QMessageAccountId& accountId) const;
       
   330 
       
   331     void updateEmailAccounts() const;
       
   332 
       
   333     bool filterMessage(const QMessage& message, QMessageFilterPrivate::SortedMessageFilterList filterList, int start) const;
       
   334     bool queryAndFilterMessages(MessageQueryInfo &msgQueryInfo) const;
       
   335     bool startQueryingAndFilteringMessages(MessageQueryInfo &msgQueryInfo) const;
       
   336     bool searchMessages(MessageQueryInfo &msgQueryInfo, const QStringList& accountIds,
       
   337                         const QStringList& folderUris, const QString& body,
       
   338                         const QDateTime& startTimeStamp, const QDateTime& endTimeStamp,
       
   339                         const QDateTime& startReceptionTimeStamp, const QDateTime& endReceptionTimeStamp) const;
       
   340     void searchNewMessages(const QString& searchString, const QString& folderToSearch,
       
   341                            const QDateTime& startDate, const QDateTime& endDate,
       
   342                            int searchflags, uint minimumMessageSize) const;
       
   343     void handleQueryFinished(int queryIndex) const;
       
   344 
       
   345     void watchAllKnownEmailFolders();
       
   346     void notification(const QMessageId& messageId, NotificationType notificationType) const;
       
   347 
       
   348     QMessageAccountId accountIdFromModestMessageId(const QString& modestMessageId) const;
       
   349     QMessageFolderId folderIdFromModestMessageId(const QString& modestMessageId,
       
   350                                                  const QMessageAccountId accountId = QMessageAccountId()) const;
       
   351 
       
   352     MessagingModestMessage messageFromModest(const QString& accountId,
       
   353                                              const QString &folderId,
       
   354                                              const QString& messageId) const;
       
   355 
       
   356     QString modestAccountIdFromAccountId(const QMessageAccountId& accountId) const;
       
   357     QString modestFolderIdFromFolderId(const QMessageFolderId& folderId) const;
       
   358     QString modestFolderUriFromFolderId(const QMessageFolderId& folderId) const;
       
   359     QString modestAccountIdFromMessageId(const QMessageId& messageId, bool checkProtocol = true) const;
       
   360     QString modestAccountIdFromFolderId(const QMessageFolderId& folderId, bool checkProtocol = true) const;
       
   361     QString modestFolderIdFromMessageId(const QMessageId& messageId) const;
       
   362     QString modestMessageIdFromMessageId(const QMessageId& messageId) const;
       
   363     void replaceProtocol(QString& id, const QString& newProtocol) const;
       
   364     QMessageAccountId realAccountId(const MessagingModestMessage& modestMessage) const;
       
   365     QMessageAccountId accountIdFromMessageId(const QMessageId& messageId) const;
       
   366     QMessageAccountId accountIdFromFolderId(const QMessageFolderId& folderId) const;
       
   367     QMessageAccountId accountIdFromModestAccountId(const QString& accountId) const;
       
   368     QMessageFolderId folderIdFromModestFolderId(const QMessageAccountId& accountId,
       
   369                                                 bool isLocalFolder,
       
   370                                                 const QString& folderId) const;
       
   371     QMessageId messageIdFromModestMessageId(const QString& messageId) const;
       
   372     QMessageId messageIdFromModestMessageFilePath(const QString& messageFilePath) const;
       
   373 
       
   374     QMessage messageFromModestMessage(const MessagingModestMessage& modestMessage,
       
   375                                       QMessageAccountId accountId = QMessageAccountId()) const;
       
   376     void appendAttachmentToMessage(QMessage& message, QMessageContentContainer& attachment) const;
       
   377 
       
   378     static QString unescapeString(const QString& string);
       
   379     static QString escapeString(const QString& string);
       
   380 
       
   381     QMessage::StandardFolder standardFolderFromModestFolderId(const QString& modestFolderId) const;
       
   382     QString modestFolderIdFromStandardFolder(QMessage::StandardFolder standardFolder) const;
       
   383 
       
   384     ModestStringMap getModestSenderInfo(QMessage &message);
       
   385     ModestStringMap getModestRecipients(QMessage &message);
       
   386     ModestStringMap getModestMessageData(QMessage &message);
       
   387     ModestStringMapList getModestAttachments(QMessage &message);
       
   388     ModestStringMapList getModestImages(QMessage &message);
       
   389     uint getModestPriority(QMessage &message);
       
   390     ModestStringMap getModestHeaders(QMessage &message);
       
   391 
       
   392     bool accountExists(const QMessageAccountId& accountId) const;
       
   393 
       
   394 private slots:
       
   395     void searchMessagesHeadersReceivedSlot(QDBusMessage msg);
       
   396     void searchMessagesHeadersFetchedSlot(QDBusMessage msg);
       
   397     void mimePartDownloadFinishedSlot(QDBusMessage msg);
       
   398     void folderUpdatedSlot(QDBusMessage msg);
       
   399     void messageReadChangedSlot(QDBusMessage msg);
       
   400     void pendingGetUnreadMessagesFinishedSlot(QDBusPendingCallWatcher* pendingCallWatcher);
       
   401     void pendingSearchFinishedSlot(QDBusPendingCallWatcher* pendingCallWatcher);
       
   402     void fileChangedSlot(int watchDescriptor, QString filePath, uint events);
       
   403     void sendEmailCallEnded(QDBusPendingCallWatcher *watcher);
       
   404     void addMessageCallEnded(QDBusPendingCallWatcher *watcher);
       
   405     void stateChanged(QMessageService::State newState);
       
   406     void returnQueryResultsSlot();
       
   407     void modestFolderContentsChangedSlot(QDBusMessage msg);
       
   408 
       
   409 private: //Data
       
   410     GConfClient *m_gconfclient;
       
   411 
       
   412     QDBusInterface *m_ModestDBusInterface;
       
   413     QDBusInterface *m_QtmPluginDBusInterface;
       
   414 
       
   415     INotifyWatcher m_MailFoldersWatcher;
       
   416 
       
   417     mutable QHash<QString, QMessageAccount> iAccounts;
       
   418     mutable QMessageAccountId iDefaultEmailAccountId;
       
   419 
       
   420     mutable int m_queryIds;
       
   421     mutable QList<MessageQueryInfo> m_pendingMessageQueries;
       
   422 
       
   423     QMap<QMessageManager::NotificationFilterId, QMessageFilter> m_filters;
       
   424     QMessageManager::NotificationFilterId m_filterId;
       
   425     QMessageStorePrivate* m_messageStore;
       
   426 
       
   427     QMap<QString, QDateTime> accountsLatestTimestamp;
       
   428 
       
   429     mutable QStringList m_latestAddOrRemoveNotifications;
       
   430 
       
   431     mutable QMap<QString, MessagingModestFolder> m_folderCache;
       
   432     mutable QMap<QString, QMessage> m_messageCache;
       
   433 
       
   434     mutable QMap<int, QMessageServicePrivate*> m_pending_downloads;
       
   435 
       
   436     mutable QMessageFolderIdList m_observed_folders;
       
   437 
       
   438     // Following variables are used for sync queries
       
   439     mutable QMessageService m_service;
       
   440     mutable QEventLoop      m_eventLoop;
       
   441     mutable QMessageIdList  m_ids;
       
   442     mutable int             m_count;
       
   443     mutable bool            m_isSorted;
       
   444     mutable bool            m_isFiltered;
       
   445 };
       
   446 
       
   447 QTM_END_NAMESPACE
       
   448 
       
   449 // Marshall the MyStructure data into a D-Bus argument
       
   450 QDBusArgument &operator<<(QDBusArgument &argument, const QtMobility::ModestStringMap &map);
       
   451 
       
   452 // Retrieve the MyStructure data from the D-Bus argument
       
   453 const QDBusArgument &operator>>(const QDBusArgument &argument, QtMobility::ModestStringMap &map);
       
   454 
       
   455 Q_DECLARE_METATYPE(QtMobility::ModestStringMap);
       
   456 Q_DECLARE_METATYPE(QtMobility::ModestStringMapList);
       
   457 Q_DECLARE_METATYPE(QtMobility::INotifyWatcher::FileNotification);
       
   458 Q_DECLARE_METATYPE(QtMobility::ModestUnreadMessageDBusStruct);
       
   459 Q_DECLARE_METATYPE(QtMobility::ModestAccountsUnreadMessagesDBusStruct);
       
   460 Q_DECLARE_METATYPE(QtMobility::ModestMessage);
       
   461 Q_DECLARE_METATYPE(QtMobility::MessagingModestMimePart);
       
   462 Q_DECLARE_METATYPE(QtMobility::MessagingModestFolder);
       
   463 
       
   464 #endif // MODESTENGINE_MAEMO_H
       
   465