qtmobility/plugins/contacts/symbiansim/src/cntsimcontactremoverequest.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    41 
    41 
    42 #include "cntsimcontactremoverequest.h"
    42 #include "cntsimcontactremoverequest.h"
    43 #include "cntsymbiansimengine.h"
    43 #include "cntsymbiansimengine.h"
    44 #include "cntsimstore.h"
    44 #include "cntsimstore.h"
    45 #include <qcontactremoverequest.h>
    45 #include <qcontactremoverequest.h>
    46 #include <QTimer>
    46 #include <QDebug>
    47 
    47 
    48 CntSimContactRemoveRequest::CntSimContactRemoveRequest(CntSymbianSimEngine *engine, QContactRemoveRequest *req)
    48 CntSimContactRemoveRequest::CntSimContactRemoveRequest(CntSymbianSimEngine *engine, QContactRemoveRequest *req)
    49     :CntAbstractSimRequest(engine),
    49     :CntAbstractSimRequest(engine, req)
    50      m_req(req)
       
    51 {
    50 {
    52     connect( simStore(), SIGNAL(removeComplete(QContactManager::Error)),
    51     connect( simStore(), SIGNAL(removeComplete(QContactManager::Error)),
    53         this, SLOT(removeComplete(QContactManager::Error)), Qt::QueuedConnection );
    52         this, SLOT(removeComplete(QContactManager::Error)), Qt::QueuedConnection );
       
    53     
       
    54     connect( simStore(), SIGNAL(getReservedSlotsComplete(QList<int>, QContactManager::Error)),
       
    55         this, SLOT(getReservedSlotsComplete(QList<int>, QContactManager::Error)), Qt::QueuedConnection );
    54 }
    56 }
    55 
    57 
    56 CntSimContactRemoveRequest::~CntSimContactRemoveRequest()
    58 CntSimContactRemoveRequest::~CntSimContactRemoveRequest()
    57 {
    59 {
    58     cancel();
    60     cancel();
    59 }
    61 }
    60 
    62 
    61 bool CntSimContactRemoveRequest::start()
    63 void CntSimContactRemoveRequest::run()
    62 {    
    64 {
    63     if (simStore()->isBusy())
    65     QContactRemoveRequest *r = req<QContactRemoveRequest>();
    64         return false;
       
    65     
    66     
    66     m_contactIds = m_req->contactIds();
    67     if (!r->isActive())
       
    68         return;
       
    69        
       
    70     m_contactIds = r->contactIds();
    67     m_errorMap.clear();
    71     m_errorMap.clear();
    68     m_index = 0;
    72     m_index = 0;
    69     singleShotTimer(0, this, SLOT(removeNext()));
    73 #ifdef SYMBIANSIM_BACKEND_CHECK_BEFORE_REMOVE
    70     QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::ActiveState);
    74     m_reservedSlots.clear();
    71     return true; 
    75     getReservedSlots();    
    72 }
    76 #else
    73 
    77     removeNext();
    74 bool CntSimContactRemoveRequest::cancel()
    78 #endif
    75 {
       
    76     if (m_req->isActive()) {
       
    77         cancelTimer();
       
    78         simStore()->cancel();
       
    79         QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::CanceledState);
       
    80         return true;
       
    81     }
       
    82     return false;
       
    83 }
    79 }
    84 
    80 
    85 void CntSimContactRemoveRequest::removeComplete(QContactManager::Error error)
    81 void CntSimContactRemoveRequest::removeComplete(QContactManager::Error error)
    86 {
    82 {
       
    83     if (!req()->isActive())
       
    84         return;
       
    85     
    87     if (error)
    86     if (error)
    88         m_errorMap.insert(m_index, error);
    87         m_errorMap.insert(m_index, error);
       
    88     
    89     m_index++;
    89     m_index++;
    90     removeNext();
    90     singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
    91 }
    91 }
    92 
    92 
    93 void CntSimContactRemoveRequest::removeNext()
    93 void CntSimContactRemoveRequest::removeNext()
    94 {
    94 {
    95     if (m_req->isCanceled())
    95     QContactRemoveRequest *r = req<QContactRemoveRequest>();
       
    96     
       
    97     if (!r->isActive())
    96         return;
    98         return;
    97     
    99     
    98     // All contacts written?
   100     if (r->contactIds().count() == 0) {
       
   101         QContactManagerEngine::updateContactRemoveRequest(r, QContactManager::BadArgumentError, m_errorMap, QContactAbstractRequest::FinishedState);
       
   102         return;
       
   103     }        
       
   104     
       
   105     // All contacts removed?
    99     if (m_index >= m_contactIds.count())
   106     if (m_index >= m_contactIds.count())
   100     {
   107     {
   101         // Take first error from errormap (if any)
   108         // Take first error from errormap (if any)
   102         QContactManager::Error error = QContactManager::NoError;
   109         QContactManager::Error error = QContactManager::NoError;
   103         if (m_errorMap.count())
   110         if (m_errorMap.count())
   104             error = m_errorMap.begin().value();
   111             error = m_errorMap.begin().value();
   105 
   112 
   106         QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::FinishedState);
   113         QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
   107         QContactManagerEngine::updateContactRemoveRequest(m_req, error, m_errorMap);
       
   108         return;
   114         return;
   109     }
   115     }
   110 
   116 
   111     // Remove next contact
   117     // Remove next contact
   112     QContactLocalId contactId = m_contactIds.at(m_index);
   118     QContactLocalId contactId = m_contactIds.at(m_index);
   113     QContactManager::Error error = simStore()->remove(contactId); 
   119     QContactManager::Error error = QContactManager::NoError;
       
   120     
       
   121 #ifdef SYMBIANSIM_BACKEND_CHECK_BEFORE_REMOVE
       
   122     if (m_reservedSlots.contains(contactId))
       
   123         simStore()->remove(contactId, &error);
       
   124     else
       
   125         error = QContactManager::DoesNotExistError;
       
   126 #else
       
   127     simStore()->remove(contactId, &error);
       
   128 #endif
       
   129 
   114     if (error) {
   130     if (error) {
   115         m_errorMap.insert(m_index, error);
   131         m_errorMap.insert(m_index, error);
   116         m_index++;
   132         m_index++;
   117         singleShotTimer(0, this, SLOT(removeNext()));
   133         singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
   118     }
   134     }
   119 }
   135 }
       
   136 
       
   137 void CntSimContactRemoveRequest::getReservedSlotsComplete(QList<int> reservedSlots, QContactManager::Error error)
       
   138 {
       
   139     QContactRemoveRequest *r = req<QContactRemoveRequest>();
       
   140     
       
   141     if (!r->isActive())
       
   142         return;
       
   143     
       
   144     if (error != QContactManager::NoError && error != QContactManager::DoesNotExistError) {
       
   145         QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
       
   146         return;
       
   147     }
       
   148 
       
   149     m_reservedSlots = reservedSlots;
       
   150     singleShotTimer(KRequestDelay, this, SLOT(removeNext()));
       
   151 }
       
   152 
       
   153 void CntSimContactRemoveRequest::getReservedSlots()
       
   154 {
       
   155     QContactRemoveRequest *r = req<QContactRemoveRequest>();
       
   156     
       
   157     if (!r->isActive())
       
   158         return;
       
   159     
       
   160     QContactManager::Error error = QContactManager::NoError;
       
   161     if (!simStore()->getReservedSlots(&error)) {
       
   162         QContactManagerEngine::updateContactRemoveRequest(r, error, m_errorMap, QContactAbstractRequest::FinishedState);
       
   163     }
       
   164 }