qtmobility/plugins/contacts/symbiansim/src/cntsimcontactremoverequest.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
--- a/qtmobility/plugins/contacts/symbiansim/src/cntsimcontactremoverequest.cpp	Fri Apr 16 15:51:22 2010 +0300
+++ b/qtmobility/plugins/contacts/symbiansim/src/cntsimcontactremoverequest.cpp	Mon May 03 13:18:40 2010 +0300
@@ -43,14 +43,16 @@
 #include "cntsymbiansimengine.h"
 #include "cntsimstore.h"
 #include <qcontactremoverequest.h>
-#include <QTimer>
+#include <QDebug>
 
 CntSimContactRemoveRequest::CntSimContactRemoveRequest(CntSymbianSimEngine *engine, QContactRemoveRequest *req)
-    :CntAbstractSimRequest(engine),
-     m_req(req)
+    :CntAbstractSimRequest(engine, req)
 {
     connect( simStore(), SIGNAL(removeComplete(QContactManager::Error)),
         this, SLOT(removeComplete(QContactManager::Error)), Qt::QueuedConnection );
+    
+    connect( simStore(), SIGNAL(getReservedSlotsComplete(QList<int>, QContactManager::Error)),
+        this, SLOT(getReservedSlotsComplete(QList<int>, QContactManager::Error)), Qt::QueuedConnection );
 }
 
 CntSimContactRemoveRequest::~CntSimContactRemoveRequest()
@@ -58,44 +60,49 @@
     cancel();
 }
 
-bool CntSimContactRemoveRequest::start()
-{    
-    if (simStore()->isBusy())
-        return false;
+void CntSimContactRemoveRequest::run()
+{
+    QContactRemoveRequest *r = req<QContactRemoveRequest>();
     
-    m_contactIds = m_req->contactIds();
+    if (!r->isActive())
+        return;
+       
+    m_contactIds = r->contactIds();
     m_errorMap.clear();
     m_index = 0;
-    singleShotTimer(0, this, SLOT(removeNext()));
-    QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::ActiveState);
-    return true; 
-}
-
-bool CntSimContactRemoveRequest::cancel()
-{
-    if (m_req->isActive()) {
-        cancelTimer();
-        simStore()->cancel();
-        QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::CanceledState);
-        return true;
-    }
-    return false;
+#ifdef SYMBIANSIM_BACKEND_CHECK_BEFORE_REMOVE
+    m_reservedSlots.clear();
+    getReservedSlots();    
+#else
+    removeNext();
+#endif
 }
 
 void CntSimContactRemoveRequest::removeComplete(QContactManager::Error error)
 {
+    if (!req()->isActive())
+        return;
+    
     if (error)
         m_errorMap.insert(m_index, error);
+    
     m_index++;
-    removeNext();
+    singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
 }
 
 void CntSimContactRemoveRequest::removeNext()
 {
-    if (m_req->isCanceled())
+    QContactRemoveRequest *r = req<QContactRemoveRequest>();
+    
+    if (!r->isActive())
         return;
     
-    // All contacts written?
+    if (r->contactIds().count() == 0) {
+        QContactManagerEngine::updateContactRemoveRequest(r, QContactManager::BadArgumentError, m_errorMap, QContactAbstractRequest::FinishedState);
+        return;
+    }        
+    
+    // All contacts removed?
     if (m_index >= m_contactIds.count())
     {
         // Take first error from errormap (if any)
@@ -103,17 +110,55 @@
         if (m_errorMap.count())
             error = m_errorMap.begin().value();
 
-        QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::FinishedState);
-        QContactManagerEngine::updateContactRemoveRequest(m_req, error, m_errorMap);
+        QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
         return;
     }
 
     // Remove next contact
     QContactLocalId contactId = m_contactIds.at(m_index);
-    QContactManager::Error error = simStore()->remove(contactId); 
+    QContactManager::Error error = QContactManager::NoError;
+    
+#ifdef SYMBIANSIM_BACKEND_CHECK_BEFORE_REMOVE
+    if (m_reservedSlots.contains(contactId))
+        simStore()->remove(contactId, &error);
+    else
+        error = QContactManager::DoesNotExistError;
+#else
+    simStore()->remove(contactId, &error);
+#endif
+
     if (error) {
         m_errorMap.insert(m_index, error);
         m_index++;
-        singleShotTimer(0, this, SLOT(removeNext()));
+        singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
     }
 }
+
+void CntSimContactRemoveRequest::getReservedSlotsComplete(QList<int> reservedSlots, QContactManager::Error error)
+{
+    QContactRemoveRequest *r = req<QContactRemoveRequest>();
+    
+    if (!r->isActive())
+        return;
+    
+    if (error != QContactManager::NoError && error != QContactManager::DoesNotExistError) {
+        QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
+        return;
+    }
+
+    m_reservedSlots = reservedSlots;
+    singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
+}
+
+void CntSimContactRemoveRequest::getReservedSlots()
+{
+    QContactRemoveRequest *r = req<QContactRemoveRequest>();
+    
+    if (!r->isActive())
+        return;
+    
+    QContactManager::Error error = QContactManager::NoError;
+    if (!simStore()->getReservedSlots(&error)) {
+        QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
+    }
+}