44 #include "cntsimstore.h" |
44 #include "cntsimstore.h" |
45 #include <qcontactfetchrequest.h> |
45 #include <qcontactfetchrequest.h> |
46 #include <qcontactlocalidfilter.h> |
46 #include <qcontactlocalidfilter.h> |
47 |
47 |
48 CntSimContactFetchRequest::CntSimContactFetchRequest(CntSymbianSimEngine *engine, QContactFetchRequest *req) |
48 CntSimContactFetchRequest::CntSimContactFetchRequest(CntSymbianSimEngine *engine, QContactFetchRequest *req) |
49 :CntAbstractSimRequest(engine), |
49 :CntAbstractSimRequest(engine, req) |
50 m_req(req) |
|
51 { |
50 { |
52 connect( simStore(), SIGNAL(readComplete(QList<QContact>, QContactManager::Error)), |
51 connect( simStore(), SIGNAL(readComplete(QList<QContact>, QContactManager::Error)), |
53 this, SLOT(readComplete(QList<QContact>, QContactManager::Error)), Qt::QueuedConnection ); |
52 this, SLOT(readComplete(QList<QContact>, QContactManager::Error)), Qt::QueuedConnection ); |
54 } |
53 } |
55 |
54 |
56 CntSimContactFetchRequest::~CntSimContactFetchRequest() |
55 CntSimContactFetchRequest::~CntSimContactFetchRequest() |
57 { |
56 { |
58 cancel(); |
57 cancel(); |
59 } |
58 } |
60 |
59 |
61 bool CntSimContactFetchRequest::start() |
60 void CntSimContactFetchRequest::run() |
62 { |
61 { |
63 QContactManager::Error error = QContactManager::NoError; |
62 QContactFetchRequest *r = req<QContactFetchRequest>(); |
|
63 |
|
64 if (!r->isActive()) |
|
65 return; |
64 |
66 |
65 // Get filter |
67 // Get filter |
66 QContactLocalIdFilter lidFilter; |
68 QContactLocalIdFilter lidFilter; |
67 if (m_req->filter().type() == QContactFilter::LocalIdFilter) { |
69 if (r->filter().type() == QContactFilter::LocalIdFilter) { |
68 lidFilter = static_cast<QContactLocalIdFilter>(m_req->filter()); |
70 lidFilter = static_cast<QContactLocalIdFilter>(r->filter()); |
69 } |
71 } |
|
72 |
|
73 // Fetch all contacts and filter the results. |
|
74 // Contacts are fetched starting from index 1, all slots are read |
|
75 // since slots may be not filled in a sequence. |
|
76 int index = 1; |
|
77 int numSlots = simStore()->storeInfo().iTotalEntries; |
70 |
78 |
71 if (lidFilter.ids().count() == 1) { |
79 if (lidFilter.ids().count() == 1) { |
72 // Optimization for performance. Fetch a single contact from store. |
80 // Optimization for performance. Fetch a single contact from store. |
73 // This is mainly for CntSymbianSimEngine::contact(). |
81 // This is mainly for CntSymbianSimEngine::contact(). |
74 int index = lidFilter.ids().at(0); |
82 index = lidFilter.ids().at(0); |
75 error = simStore()->read(index, 1); |
83 numSlots = 1; |
76 } |
84 } |
77 else { |
85 |
78 // Fetch all contacts and filter the results. |
86 QContactManager::Error error = QContactManager::NoError; |
79 // Contacts are fetched starting from index 1, all slots are read |
87 if (!simStore()->read(index, numSlots, &error)) { |
80 // since slots may be not filled in a sequence. |
88 QContactManagerEngine::updateContactFetchRequest(r, QList<QContact>(), error, QContactAbstractRequest::FinishedState); |
81 int numSlots = simStore()->storeInfo().iTotalEntries; |
|
82 error = simStore()->read(1, numSlots); |
|
83 } |
89 } |
84 |
|
85 if (error == QContactManager::NoError) |
|
86 QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::ActiveState); |
|
87 return (error == QContactManager::NoError); |
|
88 } |
|
89 |
|
90 bool CntSimContactFetchRequest::cancel() |
|
91 { |
|
92 if (m_req->isActive()) { |
|
93 simStore()->cancel(); |
|
94 QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::CanceledState); |
|
95 return true; |
|
96 } |
|
97 return false; |
|
98 } |
90 } |
99 |
91 |
100 void CntSimContactFetchRequest::readComplete(QList<QContact> contacts, QContactManager::Error error) |
92 void CntSimContactFetchRequest::readComplete(QList<QContact> contacts, QContactManager::Error error) |
101 { |
93 { |
102 if (!m_req->isActive()) |
94 QContactFetchRequest *r = req<QContactFetchRequest>(); |
|
95 |
|
96 if (!r->isActive()) |
103 return; |
97 return; |
|
98 |
|
99 // Sometimes the sim store will return server busy error. All we can do is |
|
100 // wait and try again. The error seems to occur if we try to read from the |
|
101 // store right after writing some contacts to it. |
|
102 // This was observed with S60 5.0 HW (Tube). |
|
103 if (simStore()->lastAsyncError() == KErrServerBusy) { |
|
104 if (waitAndRetry()) |
|
105 return; |
|
106 } |
104 |
107 |
105 // Filter & sort results |
108 // Filter & sort results |
106 QList<QContact> filteredAndSorted; |
109 QList<QContact> filteredAndSorted; |
107 for (int i=0; i<contacts.count(); i++) { |
110 for (int i=0; i<contacts.count(); i++) { |
108 if (QContactManagerEngine::testFilter(m_req->filter(), contacts.at(i))) |
111 if (QContactManagerEngine::testFilter(r->filter(), contacts.at(i))) |
109 QContactManagerEngine::addSorted(&filteredAndSorted, contacts.at(i), m_req->sorting()); |
112 QContactManagerEngine::addSorted(&filteredAndSorted, contacts.at(i), r->sorting()); |
110 } |
113 } |
111 |
114 |
112 // Complete the request |
115 // Complete the request |
113 QContactManagerEngine::updateRequestState(m_req, QContactAbstractRequest::FinishedState); |
116 QContactManagerEngine::updateContactFetchRequest(r, filteredAndSorted, error, QContactAbstractRequest::FinishedState); |
114 QContactManagerEngine::updateContactFetchRequest(m_req, filteredAndSorted, error); |
|
115 } |
117 } |