qtmobility/src/messaging/qmessageservice_maemo.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
--- a/qtmobility/src/messaging/qmessageservice_maemo.cpp	Fri Apr 16 15:51:22 2010 +0300
+++ b/qtmobility/src/messaging/qmessageservice_maemo.cpp	Mon May 03 13:18:40 2010 +0300
@@ -39,31 +39,21 @@
 **
 ****************************************************************************/
 #include "qmessageservice.h"
+#include "qmessageservice_maemo_p.h"
+
+#include "maemohelpers_p.h"
 #include "modestengine_maemo_p.h"
 #include "telepathyengine_maemo_p.h"
-#include <QDebug>
+#include "eventloggerengine_maemo_p.h"
 
 QTM_BEGIN_NAMESPACE
 
-class QMessageServicePrivate : public QObject
-{
-public:
-    QMessageServicePrivate(QMessageService* parent);
-    ~QMessageServicePrivate();
-
-    void setFinished(bool successful);
-
-public:
-    QMessageService* q_ptr;
-    QMessageService::State _state;
-    bool _active;
-    QMessageManager::Error _error;
-};
-
 QMessageServicePrivate::QMessageServicePrivate(QMessageService* parent)
  : q_ptr(parent),
    _state(QMessageService::InactiveState),
-   _active(false)
+   _error(QMessageManager::NoError),
+   _active(false), _actionId(-1),
+   _pendingRequestCount(0)
 {
 }
 
@@ -71,22 +61,271 @@
 {
 }
 
+QMessageServicePrivate* QMessageServicePrivate::implementation(const QMessageService &service)
+{
+    return service.d_ptr;
+}
+
+bool QMessageServicePrivate::queryMessages(QMessageService &messageService,
+                                           const QMessageFilter &filter,
+                                           const QMessageSortOrder &sortOrder,
+                                           uint limit, uint offset,
+                                           EnginesToCall enginesToCall)
+{
+  qDebug() << "QMessageServicePrivate::queryMessages 1";
+    if (_active) {
+        return false;
+    }
+
+    _ids.clear();
+    _sorted = true;
+    _filtered = true;
+
+    _active = true;
+    _error = QMessageManager::NoError;
+    bool modestEngineCalled=false;
+
+    if (enginesToCall & EnginesToCallTelepathy) {
+      _ids= EventLoggerEngine::instance()->filterAndOrderMessages(filter,sortOrder,QString(),QMessageDataComparator::MatchFlags());
+      qDebug() << "QMessageServicePrivate::queryMessages filterAndOrderMessages:";
+    }
+
+    _pendingRequestCount = 0;
+    if (enginesToCall & EnginesToCallModest) {
+      qDebug() << "QMessageServicePrivate::queryMessages modest";
+      modestEngineCalled=true;
+      if (ModestEngine::instance()->queryMessages(messageService, filter, sortOrder, limit, offset)) {
+	  qDebug() << "QMessageServicePrivate::queryMessages modest done";
+	  _pendingRequestCount++;
+
+        }
+      qDebug() << "QMessageServicePrivate::queryMessages modest done 2";
+    } 
+
+    if (!modestEngineCalled && enginesToCall & EnginesToCallTelepathy && _pendingRequestCount==0 ) {
+      qDebug() << "QMessageServicePrivate::queryMessages only eventloggerengine";
+      if (!_sorted) {
+	MessagingHelper::orderMessages(_ids, sortOrder);
+      }
+      MessagingHelper::applyOffsetAndLimitToMessageIdList(_ids, limit, offset);
+      
+      emit q_ptr->messagesFound(_ids);
+
+      qDebug() << "QMessageServicePrivate::queryMessages setFinished(true)";
+      setFinished(true);
+      
+      _ids.clear();
+      qDebug() << "QMessageServicePrivate::queryMessages return true";
+      return true; // Operation initialized and completed
+    }
+
+
+    if (_pendingRequestCount > 0) {
+        _filter = filter;
+        _sortOrder = sortOrder;
+        _limit = limit;
+        _offset = offset;
+
+        _state = QMessageService::ActiveState;
+        emit messageService.stateChanged(_state);
+    } else {
+      qDebug() << "QMessageServicePrivate::queryMessages setFinixhed() active=" << _active;
+      if(_active)setFinished(false);
+    }
+
+    return _active;
+}
+
+bool QMessageServicePrivate::queryMessages(QMessageService &messageService,
+                                           const QMessageFilter &filter,
+                                           const QString &body,
+                                           QMessageDataComparator::MatchFlags matchFlags,
+                                           const QMessageSortOrder &sortOrder,
+                                           uint limit, uint offset,
+                                           EnginesToCall enginesToCall)
+{
+  qDebug() << "QMessageServicePrivate::queryMessages 2";
+    if (_active) {
+        return false;
+    }
+
+    _ids.clear();
+    _sorted = true;
+    _filtered = true;
+
+    _active = true;
+    _error = QMessageManager::NoError;
+
+    _pendingRequestCount = 0;
+
+    bool modestEngineCalled=false;
+    
+    if (enginesToCall & EnginesToCallTelepathy) {
+        _ids= EventLoggerEngine::instance()->filterAndOrderMessages(filter,sortOrder,body,matchFlags); 
+    }
+
+    if (enginesToCall & EnginesToCallModest) {
+      modestEngineCalled=true;
+        if (ModestEngine::instance()->queryMessages(messageService, filter, body, matchFlags,
+                                                    sortOrder, limit, offset)) {
+            _pendingRequestCount++;
+        }
+    }
+
+    if (!modestEngineCalled && enginesToCall & EnginesToCallTelepathy && _pendingRequestCount==0 ) {
+      if (!_sorted) {
+	MessagingHelper::orderMessages(_ids, sortOrder);
+      }
+      MessagingHelper::applyOffsetAndLimitToMessageIdList(_ids, limit, offset);
+      
+      emit q_ptr->messagesFound(_ids);
+      setFinished(true);
+      
+      _ids.clear();
+      qDebug() << "QMessageServicePrivate::queryMessages return true";
+      return true; // Operation initialized and completed
+    }
+
+    if (_pendingRequestCount > 0) {
+        _filter = filter;
+        _sortOrder = sortOrder;
+        _limit = limit;
+        _offset = offset;
+
+        _state = QMessageService::ActiveState;
+        emit stateChanged(_state);
+    } else {
+      if(_active)setFinished(false);
+    }
+
+    return _active;
+}
+
+bool QMessageServicePrivate::countMessages(QMessageService &messageService,
+                                           const QMessageFilter &filter,
+                                           EnginesToCall enginesToCall)
+{
+    if (_active) {
+        return false;
+    }
+
+    _count = 0;
+
+    _active = true;
+    _error = QMessageManager::NoError;
+
+    _pendingRequestCount = 0;
+    if (enginesToCall & EnginesToCallModest) {
+        if (ModestEngine::instance()->countMessages(messageService, filter)) {
+            _pendingRequestCount++;
+        }
+    }
+
+    //TODO: SMS count support
+    //if (enginesToCall & EnginesToCallTelepathy) {
+    //}
+
+    if (_pendingRequestCount > 0) {
+        _state = QMessageService::ActiveState;
+        emit stateChanged(_state);
+    } else {
+        setFinished(false);
+    }
+
+    return _active;
+}
+
+
+void QMessageServicePrivate::setFinished(bool successful)
+{
+  qDebug() << "setFinished" << successful;
+    if (!successful && _pendingRequestCount > 0) {
+        _pendingRequestCount--;
+    }
+
+    if (_pendingRequestCount == 0) {
+        if (!successful && (_error == QMessageManager::NoError)) {
+            // We must report an error of some sort
+            _error = QMessageManager::RequestIncomplete;
+        }
+	qDebug() << "emit stateChanged(FinishedState)";
+        _state = QMessageService::FinishedState;
+        _active = false;
+        emit q_ptr->stateChanged(_state);
+    }
+}
+
+void QMessageServicePrivate::stateChanged(QMessageService::State state)
+{
+    _state = state;
+    qDebug() <<" StateChanged" << state;
+    emit q_ptr->stateChanged(_state);
+}
+
+void QMessageServicePrivate::messagesFound(const QMessageIdList &ids, bool isFiltered, bool isSorted)
+{
+    qDebug() <<" MessagesFound";
+    _pendingRequestCount--;
+
+    if (!isFiltered) {
+        _filtered = false;
+    }
+
+    if (!isSorted) {
+        _sorted = false;
+    } else {
+        if ((ids.count() > 0) && (_ids.count() > 0)) {
+            _sorted = false;
+        }
+    }
+
+    _ids.append(ids);
+
+    if (_pendingRequestCount == 0) {
+        if (!_filtered) {
+            MessagingHelper::filterMessages(_ids, _filter);
+        }
+        if (!_sorted) {
+            MessagingHelper::orderMessages(_ids, _sortOrder);
+        }
+        MessagingHelper::applyOffsetAndLimitToMessageIdList(_ids, _limit, _offset);
+
+        emit q_ptr->messagesFound(_ids);
+
+        setFinished(true);
+
+        _ids.clear();
+        _filter = QMessageFilter();
+        _sortOrder = QMessageSortOrder();
+    }
+}
+
+void QMessageServicePrivate::messagesCounted(int count)
+{
+    _pendingRequestCount--;
+    _count += count;
+    if (_pendingRequestCount == 0) {
+        emit q_ptr->messagesCounted(_count);
+
+        setFinished(true);
+
+        _count = 0;
+    }
+}
+
+void QMessageServicePrivate::progressChanged(uint value, uint total)
+{
+    emit q_ptr->progressChanged(value, total);
+}
+
+
+
 QMessageService::QMessageService(QObject *parent)
  : QObject(parent),
    d_ptr(new QMessageServicePrivate(this))
 {
-}
-
-void QMessageServicePrivate::setFinished(bool successful)
-{
-    if (!successful && (_error == QMessageManager::NoError)) {
-        // We must report an error of some sort
-        _error = QMessageManager::RequestIncomplete;
-    }
-
-    _state = QMessageService::FinishedState;
-    emit q_ptr->stateChanged(_state);
-    _active = false;
+    EventLoggerEngine::instance();
+    TelepathyEngine::instance();
 }
 
 QMessageService::~QMessageService()
@@ -95,63 +334,22 @@
 
 bool QMessageService::queryMessages(const QMessageFilter &filter, const QMessageSortOrder &sortOrder, uint limit, uint offset)
 {
-    if (d_ptr->_active) {
-        return false;
-    }
-
-    d_ptr->_active = true;
-    d_ptr->_error = QMessageManager::NoError;
-
-    if (ModestEngine::instance()->queryMessages(*this, filter, sortOrder, limit, offset)) {
-        d_ptr->_state = QMessageService::ActiveState;
-        emit stateChanged(d_ptr->_state);
-    } else {
-        d_ptr->setFinished(false);
-    }
-
-    return d_ptr->_active;
+    return d_ptr->queryMessages(*this, filter, sortOrder, limit, offset);
 }
 
 bool QMessageService::queryMessages(const QMessageFilter &filter, const QString &body, QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder, uint limit, uint offset)
 {
-    if (d_ptr->_active) {
-        return false;
-    }
-
-    d_ptr->_active = true;
-    d_ptr->_error = QMessageManager::NoError;
-
-    if (ModestEngine::instance()->queryMessages(*this, filter, body, matchFlags, sortOrder, limit, offset)) {
-        d_ptr->_state = QMessageService::ActiveState;
-        emit stateChanged(d_ptr->_state);
-    } else {
-        d_ptr->setFinished(false);
-    }
-
-    return d_ptr->_active;
+    return d_ptr->queryMessages(*this, filter, body, matchFlags, sortOrder, limit, offset);
 }
 
 bool QMessageService::countMessages(const QMessageFilter &filter)
 {
-    if (d_ptr->_active) {
-        return false;
-    }
-
-    d_ptr->_active = true;
-    d_ptr->_error = QMessageManager::NoError;
-
-    if (ModestEngine::instance()->countMessages(*this, filter)) {
-        d_ptr->_state = QMessageService::ActiveState;
-        emit stateChanged(d_ptr->_state);
-    } else {
-        d_ptr->setFinished(false);
-    }
-
-    return d_ptr->_active;
+    return d_ptr->countMessages(*this, filter);
 }
 
 bool QMessageService::send(QMessage &message)
 {
+  //  qDebug() << "QMessageService::send";
     if (d_ptr->_active) {
         return false;
     }
@@ -164,13 +362,33 @@
     d_ptr->_state = QMessageService::ActiveState;
     emit stateChanged(d_ptr->_state);
 
+    QMessageAccountId accountId = message.parentAccountId();
+    QMessage::Type msgType = QMessage::NoType;
+
     // Check message type
-    if(message.type() == QMessage::AnyType || message.type() == QMessage::NoType) {
-        d_ptr->_error = QMessageManager::ConstraintFailure;
-        retVal = false;
+    if (message.type() == QMessage::AnyType || message.type() == QMessage::NoType) {
+        QMessage::TypeFlags types = QMessage::NoType;
+        if (accountId.isValid()) {
+            // ParentAccountId was defined => Message type can be read
+            // from parent account
+            QMessageAccount account = QMessageAccount(accountId);
+            QMessage::TypeFlags types = account.messageTypes();
+            if (types & QMessage::Sms) {
+                msgType = QMessage::Sms;
+            } else if (account.messageTypes() & QMessage::InstantMessage) {
+                msgType = QMessage::InstantMessage;
+            } else if (types & QMessage::Mms) {
+                msgType = QMessage::Mms;
+            } else if (types & QMessage::Email) {
+                msgType = QMessage::Email;
+            }
+        }
+        if (msgType == QMessage::NoType) {
+            d_ptr->_error = QMessageManager::ConstraintFailure;
+            retVal = false;
+        }
     }
 
-    QMessageAccountId accountId = message.parentAccountId();
     if (retVal) {
         // Check account
         if (!accountId.isValid()) {
@@ -185,7 +403,7 @@
     QMessageAccount account(accountId);
     if (retVal) {
         // Check account/message type compatibility
-        if (!(account.messageTypes() & message.type())) {
+        if (!(account.messageTypes() & message.type()) && (msgType == QMessage::NoType)) {
             d_ptr->_error = QMessageManager::ConstraintFailure;
             retVal = false;
         }
@@ -196,21 +414,21 @@
         QMessageAddressList recipients = message.to() + message.bcc() + message.cc();
         if (recipients.isEmpty()) {
             d_ptr->_error = QMessageManager::ConstraintFailure;
-            return false;
+            retVal = false;
         }
     }
 
-    QMessage outgoing(message);
+    if (retVal) {
+        QMessage outgoing(message);
 
-    // Set default account if unset
-    if (!outgoing.parentAccountId().isValid()) {
-        outgoing.setParentAccountId(accountId);
-    }
+        // Set default account if unset
+        if (!outgoing.parentAccountId().isValid()) {
+            outgoing.setParentAccountId(accountId);
+        }
 
-    if (retVal) {
         if (account.messageTypes() & QMessage::Sms) {
             retVal = TelepathyEngine::instance()->sendMessage(message);
-        } else if (account.messageTypes() & QMessage::Xmpp) {
+        } else if (account.messageTypes() & QMessage::InstantMessage) {
             retVal = TelepathyEngine::instance()->sendMessage(message);
         } else if (account.messageTypes() & QMessage::Mms) {
             d_ptr->_error = QMessageManager::NotYetImplemented;
@@ -275,19 +493,63 @@
 
 bool QMessageService::show(const QMessageId& id)
 {
-    Q_UNUSED(id)
-    return false; // stub
+    if (d_ptr->_active) {
+        return false;
+    }
+
+    if (!id.isValid()) {
+        d_ptr->_error = QMessageManager::InvalidId;
+        return false;
+    }
+
+    d_ptr->_active = true;
+    d_ptr->_error = QMessageManager::NoError;
+
+    bool retVal = true;
+    d_ptr->_state = QMessageService::ActiveState;
+    emit stateChanged(d_ptr->_state);
+
+    if (id.toString().startsWith("MO_")) {
+        retVal = ModestEngine::instance()->showMessage(id);
+    } else {
+        retVal = false;
+    }
+
+    d_ptr->setFinished(retVal);
+    return retVal;
 }
 
 bool QMessageService::exportUpdates(const QMessageAccountId &id)
 {
-    Q_UNUSED(id)
-    return false; // stub
+    if (d_ptr->_active) {
+        return false;
+    }
+
+    if (!id.isValid()) {
+        d_ptr->_error = QMessageManager::InvalidId;
+        return false;
+    }
+
+    d_ptr->_active = true;
+    d_ptr->_error = QMessageManager::NoError;
+
+    bool retVal = true;
+    d_ptr->_state = QMessageService::ActiveState;
+    emit stateChanged(d_ptr->_state);
+
+    if (id.toString().startsWith("MO_")) {
+        retVal = ModestEngine::instance()->exportUpdates(id);
+    } else {
+        retVal = false;
+    }
+
+    d_ptr->setFinished(retVal);
+    return retVal;
 }
 
 QMessageService::State QMessageService::state() const
 {
-    return InactiveState; // stub
+    return d_ptr->_state;
 }
 
 void QMessageService::cancel()
@@ -296,7 +558,7 @@
 
 QMessageManager::Error QMessageService::error() const
 {
-    return QMessageManager::NoError;
+    return d_ptr->_error;
 }
 
 QTM_END_NAMESPACE