qtmobility/src/messaging/modestengine_maemo.cpp
changeset 5 453da2cfceef
parent 4 90517678cc4f
child 11 06b8e2af4411
--- a/qtmobility/src/messaging/modestengine_maemo.cpp	Mon May 03 13:18:40 2010 +0300
+++ b/qtmobility/src/messaging/modestengine_maemo.cpp	Fri May 14 16:41:33 2010 +0300
@@ -235,6 +235,7 @@
 ModestEngine::ModestEngine()
  : m_queryIds(0)
 {
+    qWarning() << "ModestEngine::ModestEngine Starting to initialize";
     g_type_init();
     m_gconfclient = gconf_client_get_default();
     if (!m_gconfclient) {
@@ -243,12 +244,18 @@
         updateEmailAccounts();
     }
 
+    qWarning() << "ModestEngine::ModestEngine Connecting to Modest DBus Interface";
     // Setup DBus Interface for Modest
     m_ModestDBusInterface = new QDBusInterface(MODEST_DBUS_SERVICE,
                                                MODEST_DBUS_OBJECT,
                                                MODEST_DBUS_IFACE,
                                                QDBusConnection::sessionBus(),
                                                this);
+    if (m_ModestDBusInterface->isValid()) {
+        qWarning() << "ModestEngine::ModestEngine Connected to Modest DBus Interface";
+    } else {
+        qWarning() << "ModestEngine::ModestEngine Unable to connect to Modest DBus Interface";
+    }
 
     // Get notifications of Incoming Messages
     m_ModestDBusInterface->connection().connect(MODEST_DBUS_SERVICE,
@@ -264,6 +271,7 @@
                                                 MODEST_DBUS_SIGNAL_MSG_READ_CHANGED,
                                                this, SLOT(messageReadChangedSlot(QDBusMessage)));
 
+    qWarning() << "ModestEngine::ModestEngine Connecting to Qt Mobility Modest Plugin DBus Interface";
     // Setup Qt Mobility Modest Plugin based DBus Interface for Modest
     m_QtmPluginDBusInterface = new QDBusInterface(MODESTENGINE_QTM_PLUGIN_NAME,
                                                   MODESTENGINE_QTM_PLUGIN_PATH,
@@ -271,6 +279,12 @@
                                                   QDBusConnection::sessionBus(),
                                                   this);
 
+    if (m_QtmPluginDBusInterface->isValid()) {
+        qWarning() << "ModestEngine::ModestEngine Connected to Qt Mobility Modest Plugin DBus Interface";
+    } else {
+        qWarning() << "ModestEngine::ModestEngine Unable to connect to Qt Mobility Modest Plugin DBus Interface";
+    }
+
     qDBusRegisterMetaType< ModestStringMap >();
     qDBusRegisterMetaType< ModestStringMapList >();
 
@@ -298,6 +312,7 @@
     connect(pendingCallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
             this, SLOT(pendingGetUnreadMessagesFinishedSlot(QDBusPendingCallWatcher*)));
 
+    qWarning() << "ModestEngine::ModestEngine Initialized successfully";
 }
 
 ModestEngine::~ModestEngine()
@@ -315,6 +330,10 @@
 {
     MessagingModestMessage modestMessage;
 
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return modestMessage;
+    }
+
     QDBusPendingCall pendingCall = m_QtmPluginDBusInterface->asyncCall("GetMessage",
                                                                        accountId,
                                                                        folderId,
@@ -350,7 +369,7 @@
         modestMessage.dateSent = 0;
         modestMessage.size = 0;
         modestMessage.flags = MessagingModestMessageNotDefined;
-        modestMessage.priority = MessagingModestMessagePriorityDefined;
+        modestMessage.priority = MessagingModestMessagePriorityNotDefined;
     }
 
     return modestMessage;
@@ -483,6 +502,10 @@
 
     QMessageAccountIdList accountIds;
 
+    if (!m_QtmPluginDBusInterface->isValid()) {
+        return accountIds;
+    }
+
     updateEmailAccounts();
     foreach (QMessageAccount value, iAccounts) {
         accountIds.append(value.id());
@@ -736,9 +759,16 @@
 
     QMessageFolderIdList folderIds;
 
+    if (!m_QtmPluginDBusInterface->isValid()) {
+        return folderIds;
+    }
+
     updateEmailAccounts();
 
-    //QDBusMessage msg = m_ModestDBusInterface->call(MODEST_DBUS_METHOD_GET_FOLDERS);
+    if (iAccounts.isEmpty()) {
+        return folderIds;
+    }
+
     QFileInfoList localFolders = this->localFolders();
     QString localRootFolder = this->localRootFolder();
 
@@ -848,7 +878,7 @@
 
     int filenameBegin = filePath.lastIndexOf('/')+1;
     QString fileName = filePath.mid(filenameBegin,filePath.lastIndexOf('.')-filenameBegin);
-    if (fileName != "summary") {
+    if (!fileName.endsWith("summary")) {
         if (events & (IN_MOVED_TO | IN_CREATE)) {
             if (events != (IN_MOVED_TO | IN_MOVED_FROM)) {
                 notification(messageIdFromModestMessageFilePath(filePath), ModestEngine::Added);
@@ -861,6 +891,10 @@
 
 bool ModestEngine::sendEmail(QMessage &message)
 {
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return false;
+    }
+
     ModestStringMap senderInfo;
     ModestStringMap recipients;
     ModestStringMap messageData;
@@ -1131,7 +1165,7 @@
 
             QMessageContentContainerId existingBodyId(message.bodyId());
             if (existingBodyId.isValid()) {
-                if (existingBodyId == container->bodyContentId()) {
+                if (existingBodyId == QMessageContentContainerPrivate::bodyContentId()) {
                     // The body content is in the message itself
                     container->_containingMessageId = messageId.toString();
                     container->_attachmentId = contentId;
@@ -1164,7 +1198,7 @@
                     container->_charset = charset;
                     container->_size = 0;
                     container->_available = true;
-                    privateMessage->_bodyId = container->bodyContentId();
+                    privateMessage->_bodyId = QMessageContentContainerPrivate::bodyContentId();
                 } else {
                     // Add the body as the first attachment
                     QMessageContentContainer newBody;
@@ -1304,6 +1338,10 @@
 
 bool ModestEngine::addMessage(QMessage &message)
 {
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return false;
+    }
+
     QString modestFolder;
     ModestStringMap senderInfo;
     ModestStringMap recipients;
@@ -1420,6 +1458,10 @@
 {
     QMessageIdList ids;
 
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return ids;
+    }
+
     QMessageServicePrivate* privateService = QMessageServicePrivate::implementation(m_service);
     if (privateService->queryMessages(m_service, filter, sortOrder, limit, offset,
                                       QMessageServicePrivate::EnginesToCallModest)) {
@@ -1444,6 +1486,10 @@
 {
     QMessageIdList ids;
 
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return ids;
+    }
+
     QMessageServicePrivate* privateService = QMessageServicePrivate::implementation(m_service);
     if (privateService->queryMessages(m_service, filter, body, matchFlags,
                                       sortOrder, limit, offset,
@@ -1466,6 +1512,10 @@
 {
     int count;
 
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return 0;
+    }
+
     QMessageServicePrivate* privateService = QMessageServicePrivate::implementation(m_service);
     if (privateService->countMessages(m_service, filter, QMessageServicePrivate::EnginesToCallModest)) {
         QObject::connect(&m_service, SIGNAL(stateChanged(QMessageService::State)),
@@ -1500,6 +1550,10 @@
 
 bool ModestEngine::countMessages(QMessageService& messageService, const QMessageFilter &filter)
 {
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return false;
+    }
+
     m_pendingMessageQueries.append(MessageQueryInfo());
 
     MessageQueryInfo &queryInfo = m_pendingMessageQueries[m_pendingMessageQueries.count()-1];
@@ -1526,6 +1580,10 @@
                                  QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder,
                                  uint limit, uint offset) const
 {
+    if (!m_QtmPluginDBusInterface->isValid() || iAccounts.isEmpty()) {
+        return false;
+    }
+
     m_pendingMessageQueries.append(MessageQueryInfo());
 
     MessageQueryInfo &queryInfo = m_pendingMessageQueries[m_pendingMessageQueries.count()-1];
@@ -1543,8 +1601,28 @@
     queryInfo.returnWithSingleShot = false;
 
     if (!startQueryingAndFilteringMessages(m_pendingMessageQueries[m_pendingMessageQueries.count()-1])) {
-        QMessageServicePrivate::implementation(messageService)->setFinished(false);
         m_pendingMessageQueries.removeAt(m_pendingMessageQueries.count()-1);
+        if (m_pendingMessageQueries.count() == 0) {
+            // This was last query in pending queries queue
+            // => Disconnect from "GetHeaders" request related DBus signals
+            // Note: Disconnecting signals is done to optimize signal handling
+            //       <=> Disconnecting prevents unnecessary handling of signals
+            //           which have been sent from other applications using
+            //           Qt Mobility Messaging API
+            m_QtmPluginDBusInterface->connection().disconnect(MODESTENGINE_QTM_PLUGIN_NAME,
+                                                              MODESTENGINE_QTM_PLUGIN_PATH,
+                                                              MODESTENGINE_QTM_PLUGIN_NAME,
+                                                              "HeadersReceived",
+                                                              (ModestEngine*)this,
+                                                              SLOT(searchMessagesHeadersReceivedSlot(QDBusMessage)));
+
+            m_QtmPluginDBusInterface->connection().disconnect(MODESTENGINE_QTM_PLUGIN_NAME,
+                                                              MODESTENGINE_QTM_PLUGIN_PATH,
+                                                              MODESTENGINE_QTM_PLUGIN_NAME,
+                                                              "HeadersFetched",
+                                                              (ModestEngine*)this,
+                                                              SLOT(searchMessagesHeadersFetchedSlot(QDBusMessage)));
+        }
         return false;
     }
 
@@ -1657,7 +1735,9 @@
                 if (cmp == QMessageDataComparator::Equal) {
                     if (pf->_value.toString().length() > 0) {
                         accountIds.append(modestAccountIdFromAccountId(pf->_value.toString()));
-                        msgQueryInfo.realAccountId = pf->_value.toString();
+                        // Local folders are not account specific
+                        // => Make sure that account specific messages are searched from local_folders account
+                        accountIds.append("local_folders");
                         handled = true;
                     }
                 }
@@ -1705,10 +1785,23 @@
                 if (cmp == QMessageDataComparator::Equal) {
                     QMessage::StandardFolder standardFolder = static_cast<QMessage::StandardFolder>(pf->_value.toInt());
                     if (standardFolder == QMessage::SentFolder) {
+                        // Possible "real" accountIds can be ignored in this phase
+                        // because messages are searched from "local_folders" account
+                        // <=> Actual account filtering is done when search from Modest
+                        //     has finished
+                        accountIds.clear();
+
+                        accountIds.append("local_folders");
                         folderUris.append("sent");
-                        if (accountIds.count() == 0) {
-                            accountIds.append("local_folders");
-                        }
+                    } else if (standardFolder == QMessage::DraftsFolder) {
+                        // Possible "real" accountIds can be ignored in this phase
+                        // because messages are searched from "local_folders" account
+                        // <=> Actual account filtering is done when search from Modest
+                        //     has finished
+                        accountIds.clear();
+
+                        accountIds.append("local_folders");
+                        folderUris.append("drafts");
                     }
                     handled = true;
                 }
@@ -1864,40 +1957,6 @@
         m_messageCache.clear();
         msgQueryInfo.queryId = reply.arguments().takeFirst().toInt();
     } else {
-        // Request failed
-        int index = -1;
-        for (int i=0; i < m_pendingMessageQueries.count(); i++) {
-            if (m_pendingMessageQueries[i].queryId == msgQueryInfo.queryId) {
-                index = i;
-                break;
-            }
-        }
-        if (index > -1) {
-            m_pendingMessageQueries.removeAt(index);
-        }
-        msgQueryInfo.privateService->setFinished(false);
-
-        if (m_pendingMessageQueries.count() == 0) {
-            // This was last query in pending queries queue
-            // => Disconnect from "GetHeaders" request related DBus signals
-            // Note: Disconnecting signals is done to optimize signal handling
-            //       <=> Disconnecting prevents unnecessary handling of signals
-            //           which have been sent from other applications using
-            //           Qt Mobility Messaging API
-            m_QtmPluginDBusInterface->connection().disconnect(MODESTENGINE_QTM_PLUGIN_NAME,
-                                                              MODESTENGINE_QTM_PLUGIN_PATH,
-                                                              MODESTENGINE_QTM_PLUGIN_NAME,
-                                                              "HeadersReceived",
-                                                              (ModestEngine*)this,
-                                                              SLOT(searchMessagesHeadersReceivedSlot(QDBusMessage)));
-
-            m_QtmPluginDBusInterface->connection().disconnect(MODESTENGINE_QTM_PLUGIN_NAME,
-                                                              MODESTENGINE_QTM_PLUGIN_PATH,
-                                                              MODESTENGINE_QTM_PLUGIN_NAME,
-                                                              "HeadersFetched",
-                                                              (ModestEngine*)this,
-                                                              SLOT(searchMessagesHeadersFetchedSlot(QDBusMessage)));
-        }
         return false;
     }
 
@@ -1966,7 +2025,8 @@
         modestMessage.dateSent = 0;
         modestMessage.size = 0;
         modestMessage.flags = MessagingModestMessageNotDefined;
-        modestMessage.priority = MessagingModestMessagePriorityDefined;
+        modestMessage.priority = MessagingModestMessagePriorityNotDefined;
+
         QMapIterator<QString, QVariant> j(messages[i]);
         while (j.hasNext()) {
             j.next();
@@ -2271,7 +2331,6 @@
     reply.waitForFinished();
 
     if (reply.isError()) {
-        qWarning () << reply.error();
         return result;
     }
 
@@ -2288,9 +2347,14 @@
 
     QFile file(filePath);
 
-    if (file.open(QIODevice::ReadWrite) == false) {
+    QIODevice::OpenMode openMode = QIODevice::ReadOnly;
+    if (expunge) {
+        openMode = QIODevice::ReadWrite;
+    }
+    
+    if (file.open(openMode) == false) {
         qWarning() << "Failed to open file" << filePath << ": "
-                << file.error();
+                << file.errorString();
         return result;
     }
 
@@ -2308,21 +2372,34 @@
 void ModestEngine::notification(const QMessageId& messageId, NotificationType notificationType) const
 {
     QMessageId realMessageId = messageId;
-
-    if (notificationType == ModestEngine::Removed) {
-        // Make sure that there will not be many Removed notifications
-        // in a row for a same message
-        QString modestMessageId = modestMessageIdFromMessageId(messageId);
-        if (!m_latestRemoveNotifications.contains(modestMessageId)) {
-            if (m_latestRemoveNotifications.count() > 10) {
-                // Remove oldest notification from the beginning of the list
-                m_latestRemoveNotifications.removeFirst();
+    QString modestAccountId = modestAccountIdFromMessageId(messageId);
+    QString modestFolderId = modestFolderIdFromMessageId(messageId);
+    QString modestMessageId = modestMessageIdFromMessageId(messageId);
+
+    // Make sure that there will not be many Added or Removed notifications
+    // in a row for the same message
+    // Make also sure that there will not be updated notification for a
+    // message that has already been notified to be removed.
+    QString searchId;
+    if (notificationType == ModestEngine::Added) {
+        searchId = "A"+modestAccountId+modestFolderId+modestMessageId;
+    } else {
+        searchId = "D"+modestAccountId+modestFolderId+modestMessageId;
+    }
+    if (!m_latestAddOrRemoveNotifications.contains(searchId)) {
+        if ((notificationType == ModestEngine::Added) || (notificationType == ModestEngine::Removed)) {
+            // Only Added & Removed notification will be checked
+            if (m_latestAddOrRemoveNotifications.count() > 10) {
+                // Remove the oldest notification from the beginning of the list
+                m_latestAddOrRemoveNotifications.removeFirst();
             }
             // Append new notification
-            m_latestRemoveNotifications.append(modestMessageId);
-        } else {
-            return;
+            m_latestAddOrRemoveNotifications.append(searchId);
         }
+    } else {
+        // Add or Remove notification for the message was already handled!
+        // => Skip unwanted notification
+        return;
     }
 
     QMessageManager::NotificationFilterIdSet matchingFilters;
@@ -2336,10 +2413,6 @@
         const QMessageFilter &filter(it.value());
 
         if (!messageRetrieved) {
-            QString modestAccountId = modestAccountIdFromMessageId(messageId);
-            QString modestFolderId = modestFolderIdFromMessageId(messageId);
-            QString modestMessageId = modestMessageIdFromMessageId(messageId);
-
             MessagingModestMessage modestMessage = messageFromModest(modestAccountId,
                                                                      modestFolderId,
                                                                      modestMessageId);
@@ -3127,6 +3200,11 @@
     return ModestStringMap(); // stub
 }
 
+void ModestEngine::clearHeaderCache()
+{
+    m_messageCache.clear();
+}
+
 #include "moc_modestengine_maemo_p.cpp"
 
 QTM_END_NAMESPACE