41 |
41 |
42 #include "cntsimcontactsaverequest.h" |
42 #include "cntsimcontactsaverequest.h" |
43 #include "cntsymbiansimengine.h" |
43 #include "cntsymbiansimengine.h" |
44 #include "cntsimstore.h" |
44 #include "cntsimstore.h" |
45 #include <qcontactsaverequest.h> |
45 #include <qcontactsaverequest.h> |
46 #include <QTimer> |
|
47 |
46 |
48 CntSimContactSaveRequest::CntSimContactSaveRequest(CntSymbianSimEngine *engine, QContactSaveRequest *req) |
47 CntSimContactSaveRequest::CntSimContactSaveRequest(CntSymbianSimEngine *engine, QContactSaveRequest *req) |
49 :CntAbstractSimRequest(engine), |
48 :CntAbstractSimRequest(engine, req) |
50 m_req(req) |
|
51 { |
49 { |
52 connect( simStore(), SIGNAL(writeComplete(QContact, QContactManager::Error)), |
50 connect( simStore(), SIGNAL(writeComplete(QContact, QContactManager::Error)), |
53 this, SLOT(writeComplete(QContact, QContactManager::Error)), Qt::QueuedConnection ); |
51 this, SLOT(writeComplete(QContact, QContactManager::Error)), Qt::QueuedConnection ); |
54 } |
52 } |
55 |
53 |
56 CntSimContactSaveRequest::~CntSimContactSaveRequest() |
54 CntSimContactSaveRequest::~CntSimContactSaveRequest() |
57 { |
55 { |
58 cancel(); |
56 cancel(); |
59 } |
57 } |
60 |
58 |
61 bool CntSimContactSaveRequest::start() |
59 void CntSimContactSaveRequest::run() |
62 { |
60 { |
63 if (simStore()->isBusy()) |
61 QContactSaveRequest *r = req<QContactSaveRequest>(); |
64 return false; |
|
65 |
62 |
66 m_contacts = m_req->contacts(); |
63 if (!r->isActive()) |
|
64 return; |
|
65 |
|
66 m_contacts = r->contacts(); |
67 m_errorMap.clear(); |
67 m_errorMap.clear(); |
68 m_index = 0; |
68 m_index = 0; |
69 singleShotTimer(0, this, SLOT(writeNext())); |
69 |
70 QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::ActiveState); |
70 writeNext(); |
71 return true; |
|
72 } |
|
73 |
|
74 bool CntSimContactSaveRequest::cancel() |
|
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 } |
71 } |
84 |
72 |
85 void CntSimContactSaveRequest::writeComplete(QContact contact, QContactManager::Error error) |
73 void CntSimContactSaveRequest::writeComplete(QContact contact, QContactManager::Error error) |
86 { |
74 { |
|
75 if (!req()->isActive()) |
|
76 return; |
|
77 |
87 if (error) |
78 if (error) |
88 m_errorMap.insert(m_index, error); |
79 m_errorMap.insert(m_index, error); |
89 engine()->updateDisplayLabel(contact); |
80 engine()->updateDisplayLabel(contact); |
90 m_contacts[m_index] = contact; |
81 m_contacts[m_index] = contact; |
91 m_index++; |
82 m_index++; |
92 writeNext(); |
83 singleShotTimer(KRequestDelay, this, SLOT(writeNext())); |
93 } |
84 } |
94 |
85 |
95 void CntSimContactSaveRequest::writeNext() |
86 void CntSimContactSaveRequest::writeNext() |
96 { |
87 { |
97 if (m_req->isCanceled()) |
88 QContactSaveRequest *r = req<QContactSaveRequest>(); |
|
89 |
|
90 if (!r->isActive()) |
98 return; |
91 return; |
|
92 |
|
93 if (r->contacts().count() == 0) { |
|
94 QContactManagerEngine::updateContactSaveRequest(r, QList<QContact>(), QContactManager::BadArgumentError, m_errorMap, QContactAbstractRequest::FinishedState); |
|
95 return; |
|
96 } |
99 |
97 |
100 // All contacts written? |
98 // All contacts written? |
101 if (m_index >= m_contacts.count()) |
99 if (m_index >= m_contacts.count()) |
102 { |
100 { |
103 // Take first error from errormap (if any) |
101 // Take first error from errormap (if any) |
104 QContactManager::Error error = QContactManager::NoError; |
102 QContactManager::Error error = QContactManager::NoError; |
105 if (m_errorMap.count()) |
103 if (m_errorMap.count()) |
106 error = m_errorMap.begin().value(); |
104 error = m_errorMap.begin().value(); |
107 |
105 |
108 QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::FinishedState); |
106 QContactManagerEngine::updateContactSaveRequest(r, m_contacts, error, m_errorMap, QContactAbstractRequest::FinishedState); |
109 QContactManagerEngine::updateContactSaveRequest(m_req, m_contacts, error, m_errorMap); |
|
110 return; |
107 return; |
111 } |
108 } |
112 |
109 |
113 // Get next contact |
110 // Get next contact |
114 QContact contact = m_contacts.at(m_index); |
111 QContact contact = m_contacts.at(m_index); |
115 |
112 |
116 // Validate & write contact |
113 // Validate & write contact |
117 QContactManager::Error error; |
114 QContactManager::Error error = QContactManager::NoError; |
118 if (engine()->validateContact(contact, error)) |
115 if (engine()->validateContact(contact, &error)) |
119 error = simStore()->write(contact); |
116 simStore()->write(contact, &error); |
120 |
117 |
121 if (error) { |
118 if (error) { |
122 m_errorMap.insert(m_index, error); |
119 m_errorMap.insert(m_index, error); |
123 m_index++; |
120 m_index++; |
124 singleShotTimer(0, this, SLOT(writeNext())); |
121 singleShotTimer(KRequestDelay, this, SLOT(writeNext())); |
125 } |
122 } |
126 } |
123 } |