| 1 |
/* |
| 2 |
* This file is part of buteo-sync-plugins package |
| 3 |
* |
| 4 |
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 5 |
* Copyright (C) 2013 - 2021 Jolla Ltd. |
| 6 |
* |
| 7 |
* Contact: Sateesh Kavuri <[email protected]> |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public License |
| 11 |
* version 2.1 as published by the Free Software Foundation. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 |
* 02110-1301 USA |
| 22 |
* |
| 23 |
*/ |
| 24 |
#include <QFile> |
| 25 |
#include <QStringListIterator> |
| 26 |
#include "SyncMLPluginLogging.h" |
| 27 |
#include "ContactsStorage.h" |
| 28 |
#include "SimpleItem.h" |
| 29 |
#include "SyncMLCommon.h" |
| 30 |
#include "SyncMLConfig.h" |
| 31 |
#include "ProfileEngineDefs.h" |
| 32 |
|
| 33 |
#include <QContact> |
| 34 |
|
| 35 |
const char* CTCAPSFILENAME11 = "CTCaps_contacts_11.xml"; |
| 36 |
const char* CTCAPSFILENAME12 = "CTCaps_contacts_12.xml"; |
| 37 |
|
| 38 |
|
| 39 |
ContactStorage::ContactStorage(const QString& aPluginName) |
| 40 |
: Buteo::StoragePlugin(aPluginName), iBackend( 0 ) |
| 41 |
{ |
| 42 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 43 |
} |
| 44 |
|
| 45 |
ContactStorage::~ContactStorage() |
| 46 |
{ |
| 47 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 48 |
if(iBackend) { |
| 49 |
qCWarning(lcSyncMLPlugin) << "Uninit method has not been called!"; |
| 50 |
delete iBackend; |
| 51 |
iBackend = 0; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
//////////////////////////////////////////////////////////////////////////////////// |
| 56 |
//////////// Below functions are derived from storage plugin /////////////// |
| 57 |
//////////////////////////////////////////////////////////////////////////////////// |
| 58 |
|
| 59 |
bool ContactStorage::init( const QMap<QString, QString>& aProperties ) |
| 60 |
{ |
| 61 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 62 |
|
| 63 |
iDeletedItems.uninit(); |
| 64 |
|
| 65 |
const QString dbFile = "hcontacts.db"; |
| 66 |
QString fullDbPath = SyncMLConfig::getDatabasePath() + dbFile; |
| 67 |
|
| 68 |
if( !iDeletedItems.init(fullDbPath) ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
QVersitDocument::VersitType vCardVersion; |
| 73 |
|
| 74 |
iProperties = aProperties; |
| 75 |
|
| 76 |
QString propVersion = iProperties[STORAGE_DEFAULT_MIME_VERSION_PROP]; |
| 77 |
|
| 78 |
if(propVersion == "3.0") { |
| 79 |
qCDebug(lcSyncMLPlugin) << "Storage is using VCard version 3.0"; |
| 80 |
vCardVersion = QVersitDocument::VCard30Type; |
| 81 |
iProperties[STORAGE_DEFAULT_MIME_PROP] = "text/vcard"; |
| 82 |
} |
| 83 |
else { |
| 84 |
qCDebug(lcSyncMLPlugin) << "Storage is using VCard version 2.1"; |
| 85 |
vCardVersion = QVersitDocument::VCard21Type; |
| 86 |
} |
| 87 |
|
| 88 |
iProperties[STORAGE_SYNCML_CTCAPS_PROP_11] = getCtCaps( CTCAPSFILENAME11 ); |
| 89 |
iProperties[STORAGE_SYNCML_CTCAPS_PROP_12] = getCtCaps( CTCAPSFILENAME12 ); |
| 90 |
|
| 91 |
iBackend = new ContactsBackend(vCardVersion, |
| 92 |
iProperties.value(STORAGE_SYNC_TARGET), |
| 93 |
iProperties.value(STORAGE_ORIGIN_ID)); |
| 94 |
|
| 95 |
if( !iBackend->init() ) { |
| 96 |
qCCritical(lcSyncMLPlugin) << "Failed to init contacts backend"; |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if( !doInitItemAnalysis() ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
bool ContactStorage::uninit() |
| 108 |
{ |
| 109 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 110 |
|
| 111 |
doUninitItemAnalysis(); |
| 112 |
|
| 113 |
// If the backend object is NULL, there is nothing to do anyway, |
| 114 |
// so the default value can be 'true' here. |
| 115 |
bool backendUninitOk = true; |
| 116 |
|
| 117 |
if( iBackend != NULL) { |
| 118 |
backendUninitOk = iBackend->uninit(); |
| 119 |
delete iBackend; |
| 120 |
iBackend = NULL; |
| 121 |
} |
| 122 |
|
| 123 |
bool deleteItemsIdStorageUninitOk = iDeletedItems.uninit(); |
| 124 |
|
| 125 |
return (backendUninitOk && deleteItemsIdStorageUninitOk); |
| 126 |
} |
| 127 |
|
| 128 |
bool ContactStorage::getAllItems(QList<Buteo::StorageItem*> &aItems) |
| 129 |
{ |
| 130 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 131 |
bool operationStatus = false; |
| 132 |
QList<QContactLocalId> list; |
| 133 |
|
| 134 |
if(iBackend) { |
| 135 |
list = iBackend->getAllContactIds(); |
| 136 |
if(list.size() != 0) { |
| 137 |
qDebug() << " Number of items retrieved from Contacts " << list.size(); |
| 138 |
aItems = getStoreList(list); |
| 139 |
} |
| 140 |
operationStatus = true; |
| 141 |
} |
| 142 |
return operationStatus; |
| 143 |
} |
| 144 |
|
| 145 |
bool ContactStorage::getAllItemIds( QList<QString>& aItems ) |
| 146 |
{ |
| 147 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 148 |
bool operationStatus = false; |
| 149 |
QList<QContactLocalId> list; |
| 150 |
if(iBackend) { |
| 151 |
list = iBackend->getAllContactIds(); |
| 152 |
qDebug() << " Number of items retrieved from Contacts " << list.size(); |
| 153 |
foreach(QContactLocalId id , list) { |
| 154 |
aItems.append(id.toString()); |
| 155 |
} |
| 156 |
operationStatus = true; |
| 157 |
} |
| 158 |
return operationStatus; |
| 159 |
} |
| 160 |
|
| 161 |
bool ContactStorage::getNewItems(QList<Buteo::StorageItem*> &aItems ,const QDateTime& aTime) |
| 162 |
{ |
| 163 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 164 |
bool operationStatus = false; |
| 165 |
QList<QContactLocalId> list; |
| 166 |
if(iBackend) { |
| 167 |
qDebug() << "****** getNewItems : Added After: ********" << aTime; |
| 168 |
list = iBackend->getAllNewContactIds(aTime); |
| 169 |
if(list.size() != 0) { |
| 170 |
qDebug() << "New Item List Size is " << list.size(); |
| 171 |
aItems = getStoreList(list); |
| 172 |
} |
| 173 |
operationStatus = true; |
| 174 |
} |
| 175 |
return operationStatus; |
| 176 |
} |
| 177 |
|
| 178 |
bool ContactStorage::getNewItemIds( QList<QString>& aNewItemIds, const QDateTime& aTime ) |
| 179 |
{ |
| 180 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 181 |
bool operationStatus = false; |
| 182 |
QList<QContactLocalId> list; |
| 183 |
|
| 184 |
if(iBackend) { |
| 185 |
qDebug() << "****** getNewItem Ids : Added After: ********" << aTime; |
| 186 |
list = iBackend->getAllNewContactIds(aTime); |
| 187 |
|
| 188 |
foreach(QContactLocalId id , list) { |
| 189 |
aNewItemIds.append(id.toString()); |
| 190 |
} |
| 191 |
|
| 192 |
operationStatus = true; |
| 193 |
} |
| 194 |
return operationStatus; |
| 195 |
} |
| 196 |
|
| 197 |
bool ContactStorage::getModifiedItems( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime ) |
| 198 |
{ |
| 199 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 200 |
QList<QContactLocalId> list; |
| 201 |
bool operationStatus = false; |
| 202 |
|
| 203 |
if(iBackend) { |
| 204 |
qDebug() << "******* getModifiedItems: From ********" << aTime; |
| 205 |
|
| 206 |
list = iBackend->getAllModifiedContactIds(aTime); |
| 207 |
|
| 208 |
aModifiedItems = getStoreList(list); |
| 209 |
|
| 210 |
operationStatus = true; |
| 211 |
} |
| 212 |
|
| 213 |
return operationStatus; |
| 214 |
} |
| 215 |
|
| 216 |
bool ContactStorage::getModifiedItemIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime ) |
| 217 |
{ |
| 218 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 219 |
QList<QContactLocalId> list; |
| 220 |
bool operationStatus = false; |
| 221 |
|
| 222 |
if(iBackend) { |
| 223 |
qDebug() << "******* getModifiedItemIds : From ********" << aTime; |
| 224 |
|
| 225 |
list = iBackend->getAllModifiedContactIds(aTime); |
| 226 |
|
| 227 |
foreach(QContactLocalId id , list) { |
| 228 |
aModifiedItemIds.append(id.toString()); |
| 229 |
} |
| 230 |
operationStatus = true; |
| 231 |
} |
| 232 |
return operationStatus; |
| 233 |
} |
| 234 |
|
| 235 |
bool ContactStorage::getDeletedItemIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime ) |
| 236 |
{ |
| 237 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 238 |
qCDebug(lcSyncMLPlugin) << "Getting deleted contacts since" << aTime; |
| 239 |
|
| 240 |
return iDeletedItems.getDeletedItems( aDeletedItemIds, aTime ); |
| 241 |
} |
| 242 |
|
| 243 |
Buteo::StorageItem* ContactStorage::newItem() |
| 244 |
{ |
| 245 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 246 |
return new SimpleItem; |
| 247 |
} |
| 248 |
|
| 249 |
QList<Buteo::StorageItem*> ContactStorage::getItems( const QStringList& aItemIdList ) |
| 250 |
{ |
| 251 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 252 |
|
| 253 |
QList<Buteo::StorageItem*> items; |
| 254 |
QMap<QString,QString> vcards; |
| 255 |
QList<QContactLocalId> ids; |
| 256 |
QStringListIterator itr( aItemIdList ); |
| 257 |
|
| 258 |
if( iBackend ) |
| 259 |
{ |
| 260 |
foreach (const QString itr, aItemIdList) |
| 261 |
{ |
| 262 |
ids.append( QContactId::fromString (itr) ); |
| 263 |
} |
| 264 |
iBackend->getContacts( ids, vcards ); |
| 265 |
|
| 266 |
QMapIterator<QString,QString> i(vcards); |
| 267 |
while( i.hasNext() ) |
| 268 |
{ |
| 269 |
i.next(); |
| 270 |
if( !i.value().isEmpty() ) |
| 271 |
{ |
| 272 |
SimpleItem *item = new SimpleItem; |
| 273 |
item->setId( i.key() ); |
| 274 |
item->setType( iProperties[STORAGE_DEFAULT_MIME_PROP] ); |
| 275 |
item->write( 0, i.value().toUtf8() ); |
| 276 |
items.append( item ); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
qCWarning(lcSyncMLPlugin) << "Contact with id " << i.key() <<" doesn't exist!"; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return items; |
| 286 |
} |
| 287 |
|
| 288 |
Buteo::StorageItem* ContactStorage::getItem( const QString& aItemId ) |
| 289 |
{ |
| 290 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 291 |
|
| 292 |
if( !iBackend ) |
| 293 |
{ |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
SimpleItem* newItem = NULL; |
| 298 |
|
| 299 |
QContactLocalId id; |
| 300 |
id = QContactId::fromString (aItemId); |
| 301 |
QContact contact; |
| 302 |
|
| 303 |
iBackend->getContact( id, contact ); |
| 304 |
QDateTime creationTime = iBackend->getCreationTime( contact ); |
| 305 |
|
| 306 |
if( iFreshItems.contains( id.toString () ) ) |
| 307 |
{ |
| 308 |
qCDebug(lcSyncMLPlugin) << "Intercepted fresh item:" << id.toString (); |
| 309 |
iSnapshot.insert( id.toString (), creationTime ); |
| 310 |
iFreshItems.removeOne( id.toString () ); |
| 311 |
} |
| 312 |
|
| 313 |
QString contactData = iBackend->convertQContactToVCard( contact ); |
| 314 |
|
| 315 |
if(!contactData.isEmpty()) |
| 316 |
{ |
| 317 |
newItem = new SimpleItem; |
| 318 |
newItem->setId(aItemId); |
| 319 |
newItem->setType(iProperties[STORAGE_DEFAULT_MIME_PROP]); |
| 320 |
newItem->write(0,contactData.toUtf8()); |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
qCWarning(lcSyncMLPlugin) << "Contact does not exist:" << aItemId; |
| 325 |
} |
| 326 |
|
| 327 |
return newItem; |
| 328 |
} |
| 329 |
|
| 330 |
ContactStorage::OperationStatus ContactStorage::addItem(Buteo::StorageItem& aItem) |
| 331 |
{ |
| 332 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 333 |
|
| 334 |
QList<Buteo::StorageItem*> items; |
| 335 |
items.append( &aItem ); |
| 336 |
|
| 337 |
QList<ContactStorage::OperationStatus> status = addItems( items ); |
| 338 |
|
| 339 |
return status.first(); |
| 340 |
} |
| 341 |
|
| 342 |
QList<ContactStorage::OperationStatus> ContactStorage::addItems( const QList<Buteo::StorageItem*>& aItems ) |
| 343 |
{ |
| 344 |
|
| 345 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 346 |
|
| 347 |
QList<ContactStorage::OperationStatus> storageErrorList; |
| 348 |
QDateTime currentTime = QDateTime::currentDateTime(); |
| 349 |
|
| 350 |
if( !iBackend ) |
| 351 |
{ |
| 352 |
for ( int i = 0; i < aItems.size(); i++) |
| 353 |
{ |
| 354 |
storageErrorList.append(STATUS_ERROR); |
| 355 |
} |
| 356 |
return storageErrorList; |
| 357 |
} |
| 358 |
|
| 359 |
QList<QString> contactsList; |
| 360 |
foreach(Buteo::StorageItem *item , aItems) { |
| 361 |
QByteArray data; |
| 362 |
item->read(0,item->getSize(),data); |
| 363 |
contactsList.append(QString::fromUtf8(data.data())); |
| 364 |
} |
| 365 |
|
| 366 |
QList<QString> contactsIdList; |
| 367 |
QMap<int, ContactsStatus> contactsErrorMap; |
| 368 |
bool retVal = iBackend->addContacts(contactsList, contactsErrorMap); |
| 369 |
|
| 370 |
if((contactsErrorMap.size() != contactsList.size()) || |
| 371 |
(contactsList.size() != aItems.size())) |
| 372 |
{ |
| 373 |
|
| 374 |
qCWarning(lcSyncMLPlugin) << "Something Wrong with Batch Addition in Contacts Backend : " << retVal; |
| 375 |
qCDebug(lcSyncMLPlugin) << "contactsErrorMap.size() " << contactsErrorMap.size(); |
| 376 |
qCDebug(lcSyncMLPlugin) << "contactsList.size()" << contactsList.size(); |
| 377 |
qCDebug(lcSyncMLPlugin) << "aItems.size()" << aItems.size(); |
| 378 |
|
| 379 |
for ( int i = 0; i < aItems.size(); i++) { |
| 380 |
storageErrorList.append(STATUS_ERROR); |
| 381 |
} |
| 382 |
|
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
|
| 387 |
QMapIterator<int ,ContactsStatus> i(contactsErrorMap); |
| 388 |
int j = 0; |
| 389 |
while (i.hasNext()) { |
| 390 |
i.next(); |
| 391 |
Buteo::StorageItem *item = aItems[j]; |
| 392 |
item->setId(i.value().id); |
| 393 |
|
| 394 |
ContactStorage::OperationStatus status = mapErrorStatus(i.value().errorCode); |
| 395 |
|
| 396 |
if( status == STATUS_OK ) |
| 397 |
{ |
| 398 |
// This item was successfully added, so let's add it to the snapshot |
| 399 |
iSnapshot.insert( i.value().id, currentTime ); |
| 400 |
} |
| 401 |
|
| 402 |
storageErrorList.append(status); |
| 403 |
j++; |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
return storageErrorList; |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
ContactStorage::OperationStatus ContactStorage::modifyItem(Buteo::StorageItem& aItem) |
| 414 |
{ |
| 415 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 416 |
|
| 417 |
ContactStorage::OperationStatus status = STATUS_ERROR; |
| 418 |
|
| 419 |
if(iBackend ) { |
| 420 |
QString strID = aItem.getId(); |
| 421 |
QByteArray data; |
| 422 |
aItem.read( 0, aItem.getSize(), data ); |
| 423 |
QString Contact = QString::fromUtf8( data ); |
| 424 |
qDebug() << "Modifying an Item with data : " << Contact; |
| 425 |
qDebug() << "Modifying an Item with ID : " << strID; |
| 426 |
QContactManager::Error error = iBackend->modifyContact(strID ,Contact); |
| 427 |
status = mapErrorStatus(error); |
| 428 |
qDebug() << "After Modification String ID is " << strID; |
| 429 |
} |
| 430 |
|
| 431 |
return status; |
| 432 |
} |
| 433 |
|
| 434 |
QList<ContactStorage::OperationStatus> ContactStorage::modifyItems(const QList<Buteo::StorageItem *> &aItems) |
| 435 |
{ |
| 436 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 437 |
QList<ContactStorage::OperationStatus> storageErrorList; |
| 438 |
|
| 439 |
qDebug() << "Items to Modify :" << aItems.size(); |
| 440 |
|
| 441 |
if(iBackend) { |
| 442 |
|
| 443 |
QStringList contactsList; |
| 444 |
QStringList contactsIdList; |
| 445 |
foreach(Buteo::StorageItem *item , aItems) { |
| 446 |
QByteArray data; |
| 447 |
item->read(0,item->getSize(),data); |
| 448 |
contactsList.append(QString::fromUtf8(data.data())); |
| 449 |
contactsIdList.append(item->getId()); |
| 450 |
} |
| 451 |
|
| 452 |
QMap<int, ContactsStatus> contactsErrorMap = |
| 453 |
iBackend->modifyContacts(contactsList, contactsIdList); |
| 454 |
|
| 455 |
if(contactsErrorMap.size() != contactsList.size()) |
| 456 |
{ |
| 457 |
|
| 458 |
qCWarning(lcSyncMLPlugin) << "Something Wrong with Batch Mofication in Contacts Backend"; |
| 459 |
qCDebug(lcSyncMLPlugin) << "contactsErrroMap.size() " << contactsErrorMap.size(); |
| 460 |
qCDebug(lcSyncMLPlugin) << "contactsList.size()" << contactsList.size(); |
| 461 |
for ( int i = 0; i < aItems.size(); i++) { |
| 462 |
storageErrorList.append(STATUS_ERROR); |
| 463 |
} |
| 464 |
|
| 465 |
} else { |
| 466 |
|
| 467 |
QMapIterator<int, ContactsStatus> i(contactsErrorMap); |
| 468 |
int j = 0; |
| 469 |
while (i.hasNext()) { |
| 470 |
i.next(); |
| 471 |
Buteo::StorageItem *item = aItems[j]; |
| 472 |
item->setId(i.value().id); |
| 473 |
qCDebug(lcSyncMLPlugin) << "Id set in Storage " << item->getId(); |
| 474 |
storageErrorList.append(mapErrorStatus(i.value().errorCode)); |
| 475 |
j++; |
| 476 |
} |
| 477 |
|
| 478 |
} // end if contactsErrroMap.size() != contactsList.size() |
| 479 |
|
| 480 |
} else { |
| 481 |
|
| 482 |
for ( int i = 0; i < aItems.size(); i++) { |
| 483 |
storageErrorList.append(STATUS_ERROR); |
| 484 |
} |
| 485 |
|
| 486 |
} |
| 487 |
return storageErrorList; |
| 488 |
} |
| 489 |
|
| 490 |
ContactStorage::OperationStatus ContactStorage::deleteItem( const QString& aItemId ) |
| 491 |
{ |
| 492 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 493 |
|
| 494 |
QList<QString> itemIds; |
| 495 |
itemIds.append( aItemId ); |
| 496 |
|
| 497 |
QList<ContactStorage::OperationStatus> status = deleteItems( itemIds ); |
| 498 |
|
| 499 |
return status.first(); |
| 500 |
} |
| 501 |
|
| 502 |
QList<ContactStorage::OperationStatus> ContactStorage::deleteItems(const QList<QString>& aItemIds ) |
| 503 |
{ |
| 504 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 505 |
|
| 506 |
QList<ContactStorage::OperationStatus> statusList; |
| 507 |
QDateTime currentTime = QDateTime::currentDateTime(); |
| 508 |
|
| 509 |
if( !iBackend ) |
| 510 |
{ |
| 511 |
for ( int i = 0; i < aItemIds.size(); i++) |
| 512 |
{ |
| 513 |
statusList.append(STATUS_ERROR); |
| 514 |
} |
| 515 |
return statusList; |
| 516 |
} |
| 517 |
|
| 518 |
QMap<int, ContactsStatus> errorMap = iBackend->deleteContacts(aItemIds); |
| 519 |
if(errorMap.size() !=aItemIds.size() ) |
| 520 |
{ |
| 521 |
qCWarning(lcSyncMLPlugin) << "Something wrong with batch deletion of Contacts"; |
| 522 |
qCDebug(lcSyncMLPlugin) << "contactsErrroMap.size() " << errorMap.size(); |
| 523 |
qCDebug(lcSyncMLPlugin) << "contactsList.size()" << aItemIds.size(); |
| 524 |
for ( int i = 0; i < aItemIds.size(); i++) |
| 525 |
{ |
| 526 |
statusList.append(STATUS_ERROR); |
| 527 |
} |
| 528 |
} |
| 529 |
else |
| 530 |
{ |
| 531 |
QList<QString> itemIds; |
| 532 |
QList<QDateTime> creationTimes; |
| 533 |
QList<QDateTime> deletionTimes; |
| 534 |
|
| 535 |
QMapIterator<int, ContactsStatus> i(errorMap); |
| 536 |
int j = 0; |
| 537 |
while( i.hasNext() ) |
| 538 |
{ |
| 539 |
i.next(); |
| 540 |
ContactStorage::OperationStatus status = mapErrorStatus( i.value().errorCode ); |
| 541 |
|
| 542 |
if( status == STATUS_OK ) |
| 543 |
{ |
| 544 |
// This item was successfully deleted, so let's remove it from the snapshot and |
| 545 |
// add it to the deleted items. |
| 546 |
|
| 547 |
QString itemId = aItemIds[j]; |
| 548 |
|
| 549 |
itemIds.append( itemId ); |
| 550 |
creationTimes.append( iSnapshot.value( itemId )); |
| 551 |
iSnapshot.remove( itemId ); |
| 552 |
deletionTimes.append( currentTime ); |
| 553 |
} |
| 554 |
|
| 555 |
statusList.append( status ); |
| 556 |
j++; |
| 557 |
} |
| 558 |
|
| 559 |
if( !itemIds.isEmpty() ) |
| 560 |
{ |
| 561 |
iDeletedItems.addDeletedItems( itemIds, creationTimes, deletionTimes ); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
return statusList; |
| 567 |
} |
| 568 |
|
| 569 |
bool ContactStorage::doInitItemAnalysis() |
| 570 |
{ |
| 571 |
/* Items are analyzed using two sets of items: those that existed at the end |
| 572 |
* of the previous time this plugin was uninitiated (called snapshot), and those |
| 573 |
* that exists now (called backend). The point of the analysis is to divide the |
| 574 |
* items into 3 groups: |
| 575 |
* 1) Items that exist only in the snapshot. These items have been deleted, and |
| 576 |
* should be moved to the list of deleted items. The creation time of these |
| 577 |
* items is known, as they are stored in the database. |
| 578 |
* 2) Items that exist both in the snapshot and the backend. Since there's no |
| 579 |
* change in the lifetime of these items, they do not need any special |
| 580 |
* attention. |
| 581 |
* 3) Items that exist only in the backed. These items have been created recently, |
| 582 |
* so their creation time needs to be fetched from the backend before they |
| 583 |
* are added to snapshot we are storing to database during uninit(). We could |
| 584 |
* retrieve creation times here, but because these items are usually accessed |
| 585 |
* anyway during a session, we will just add them to a list of "fresh items" |
| 586 |
* which will be checked whenever a contact is retrieved from backend. |
| 587 |
* The creation times of all remaining "fresh" items which were not accessed |
| 588 |
* during the session will be retrieved in uninit() |
| 589 |
* |
| 590 |
* After items have been analyzed, the in-memory snapshot will be identical to |
| 591 |
* backend. In-memory snapshot is updated whenever we add or delete items. In |
| 592 |
* the item analysis of uninit() we therefore have an up-to-date snapshot of the |
| 593 |
* contents of the backend. Changes that external application do the beckend |
| 594 |
* during the sync session will get discovered in the next session. |
| 595 |
*/ |
| 596 |
|
| 597 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 598 |
|
| 599 |
Q_ASSERT( iBackend ); |
| 600 |
iSnapshot.clear(); |
| 601 |
iFreshItems.clear(); |
| 602 |
|
| 603 |
QDateTime currentTime = QDateTime::currentDateTime(); |
| 604 |
QMap<QString, QDateTime> snapshot; |
| 605 |
QList<QString> backend; |
| 606 |
QList<QString> freshItems; |
| 607 |
|
| 608 |
// ** Retrieve previous snapshot from db and convert to QHash |
| 609 |
QList<QString> snapshotItems; |
| 610 |
QList<QDateTime> snapshotCreationTimes; |
| 611 |
|
| 612 |
if( !iDeletedItems.getSnapshot(snapshotItems, snapshotCreationTimes) ) { |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
for( int i = 0; i < snapshotItems.count(); ++i ) |
| 617 |
{ |
| 618 |
snapshot.insert( snapshotItems[i], snapshotCreationTimes[i] ); |
| 619 |
} |
| 620 |
|
| 621 |
// ** Retrieve backend |
| 622 |
QList<QContactId> backendIds = iBackend->getAllContactIds(); |
| 623 |
foreach (const QContactId id, backendIds) { |
| 624 |
backend << id.toString (); |
| 625 |
} |
| 626 |
|
| 627 |
qCDebug(lcSyncMLPlugin) << "Found" << snapshot.count() << "items from snapshot"; |
| 628 |
qCDebug(lcSyncMLPlugin) << "Found" << backend.count() << "items from backend"; |
| 629 |
|
| 630 |
QList<QString> itemIds; |
| 631 |
QList<QDateTime> creationTimes; |
| 632 |
QList<QDateTime> deletionTimes; |
| 633 |
|
| 634 |
// ** Find items only in the snapshot and mark them as deleted |
| 635 |
QMutableMapIterator<QString, QDateTime> i( snapshot ); |
| 636 |
|
| 637 |
while( i.hasNext() ) |
| 638 |
{ |
| 639 |
i.next(); |
| 640 |
if( !backend.contains( i.key() ) ) |
| 641 |
{ |
| 642 |
itemIds.append( i.key() ); |
| 643 |
creationTimes.append( i.value() ); |
| 644 |
deletionTimes.append( currentTime ); |
| 645 |
i.remove(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
qCDebug(lcSyncMLPlugin) << "Detected" << itemIds.count() <<"deleted items"; |
| 650 |
|
| 651 |
if( !itemIds.isEmpty() ) |
| 652 |
{ |
| 653 |
iDeletedItems.addDeletedItems( itemIds, creationTimes, deletionTimes ); |
| 654 |
} |
| 655 |
|
| 656 |
// ** Find items only in backend and mark them as fresh items |
| 657 |
|
| 658 |
for( int i = 0; i < backend.count(); ++i ) |
| 659 |
{ |
| 660 |
if( !snapshot.contains( backend[i] ) ) |
| 661 |
{ |
| 662 |
freshItems.append( backend[i] ); |
| 663 |
snapshot.insert( backend[i], QDateTime() ); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
iSnapshot = snapshot; |
| 668 |
iFreshItems = freshItems; |
| 669 |
|
| 670 |
qCDebug(lcSyncMLPlugin) << "Detected" << iFreshItems.count() <<"fresh items"; |
| 671 |
|
| 672 |
return true; |
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
bool ContactStorage::doUninitItemAnalysis() |
| 677 |
{ |
| 678 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 679 |
|
| 680 |
Q_ASSERT( iBackend ); |
| 681 |
|
| 682 |
// ** Retrieve creation times for the remaining "fresh" items from the backend |
| 683 |
|
| 684 |
if( !iFreshItems.isEmpty() ) |
| 685 |
{ |
| 686 |
qCDebug(lcSyncMLPlugin) << "Retrieving creation times for" << iFreshItems.count() << "fresh items"; |
| 687 |
|
| 688 |
QList<QContactId> idList; |
| 689 |
foreach (const QString freshItem, iFreshItems) { |
| 690 |
idList << QContactId::fromString (freshItem); |
| 691 |
} |
| 692 |
QList<QDateTime> freshCreationTimes = iBackend->getCreationTimes( idList ); |
| 693 |
|
| 694 |
for( int i = 0; i < iFreshItems.count(); ++i ) |
| 695 |
{ |
| 696 |
iSnapshot.insert( iFreshItems[i], freshCreationTimes[i] ); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
// ** Store the snapshot to the database |
| 701 |
|
| 702 |
QTime timer; |
| 703 |
timer.start(); |
| 704 |
QList<QString> intIds = iSnapshot.keys(); |
| 705 |
|
| 706 |
QList<QString> ids; |
| 707 |
for( int i = 0; i < intIds.count(); ++i ) |
| 708 |
{ |
| 709 |
ids.append( intIds[i] ); |
| 710 |
} |
| 711 |
|
| 712 |
QList<QDateTime> creationTimes = iSnapshot.values(); |
| 713 |
|
| 714 |
qCDebug(lcSyncMLPlugin) << "Storing" << ids.count() <<"items to snapshot"; |
| 715 |
|
| 716 |
iDeletedItems.setSnapshot( ids, creationTimes ); |
| 717 |
|
| 718 |
iSnapshot.clear(); |
| 719 |
iFreshItems.clear(); |
| 720 |
|
| 721 |
return true; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
QList<Buteo::StorageItem*> ContactStorage::getStoreList(QList<QContactLocalId> &aStrIDList) |
| 726 |
{ |
| 727 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 728 |
|
| 729 |
QList<Buteo::StorageItem*> itemList; |
| 730 |
|
| 731 |
|
| 732 |
if (iBackend != NULL) { |
| 733 |
QMap<QString,QString> idDataMap; |
| 734 |
iBackend->getContacts(aStrIDList, idDataMap); |
| 735 |
QMapIterator<QString, QString > iter(idDataMap); |
| 736 |
|
| 737 |
while (iter.hasNext()) { |
| 738 |
iter.next(); |
| 739 |
SimpleItem* item = convertVcardToStorageItem(QContactId::fromString (iter.key()), iter.value()); |
| 740 |
if (item != NULL) { |
| 741 |
itemList.append(item); |
| 742 |
} |
| 743 |
|
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
return itemList; |
| 748 |
} |
| 749 |
|
| 750 |
QByteArray ContactStorage::getCtCaps( const QString& aFilename ) const |
| 751 |
{ |
| 752 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 753 |
|
| 754 |
QFile ctCapsFile( SyncMLConfig::getXmlDataPath() + aFilename ); |
| 755 |
QByteArray ctCaps; |
| 756 |
|
| 757 |
if( ctCapsFile.open(QIODevice::ReadOnly)) { |
| 758 |
ctCaps = ctCapsFile.readAll(); |
| 759 |
ctCapsFile.close(); |
| 760 |
} else { |
| 761 |
qCWarning(lcSyncMLPlugin) << "Failed to open CTCaps file for contacts storage:" << aFilename; |
| 762 |
} |
| 763 |
|
| 764 |
return ctCaps; |
| 765 |
} |
| 766 |
|
| 767 |
ContactStorage::OperationStatus ContactStorage::mapErrorStatus\ |
| 768 |
(const QContactManager::Error &aContactError) const |
| 769 |
{ |
| 770 |
ContactStorage::OperationStatus iStorageStatus = STATUS_ERROR; |
| 771 |
switch(aContactError) { |
| 772 |
case QContactManager::NoError: |
| 773 |
iStorageStatus = STATUS_OK; |
| 774 |
break; |
| 775 |
|
| 776 |
case QContactManager::DoesNotExistError: |
| 777 |
iStorageStatus = STATUS_NOT_FOUND; |
| 778 |
break; |
| 779 |
|
| 780 |
case QContactManager::AlreadyExistsError: |
| 781 |
iStorageStatus = STATUS_DUPLICATE; |
| 782 |
break; |
| 783 |
|
| 784 |
case QContactManager::InvalidDetailError: |
| 785 |
case QContactManager::VersionMismatchError: |
| 786 |
case QContactManager::InvalidContactTypeError: |
| 787 |
iStorageStatus = STATUS_INVALID_FORMAT; |
| 788 |
break; |
| 789 |
|
| 790 |
case QContactManager::OutOfMemoryError: |
| 791 |
iStorageStatus = STATUS_STORAGE_FULL; |
| 792 |
break; |
| 793 |
|
| 794 |
case QContactManager::LimitReachedError: |
| 795 |
iStorageStatus = STATUS_OBJECT_TOO_BIG; |
| 796 |
break; |
| 797 |
|
| 798 |
case QContactManager::InvalidRelationshipError: |
| 799 |
case QContactManager::LockedError: |
| 800 |
case QContactManager::DetailAccessError: |
| 801 |
case QContactManager::PermissionsError: |
| 802 |
case QContactManager::NotSupportedError: |
| 803 |
case QContactManager::BadArgumentError: |
| 804 |
case QContactManager::UnspecifiedError: |
| 805 |
default: |
| 806 |
iStorageStatus = STATUS_ERROR; |
| 807 |
break; |
| 808 |
} |
| 809 |
return iStorageStatus; |
| 810 |
} |
| 811 |
|
| 812 |
/*! |
| 813 |
\fn ContactStorage::convertVcardToStorageItem(const QContactLocalId, const QString&) |
| 814 |
*/ |
| 815 |
SimpleItem* ContactStorage::convertVcardToStorageItem(const QContactLocalId aItemKey, |
| 816 |
const QString& aItemData) |
| 817 |
{ |
| 818 |
|
| 819 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 820 |
|
| 821 |
SimpleItem* storageItem = new SimpleItem; |
| 822 |
|
| 823 |
if(storageItem != NULL) { |
| 824 |
qDebug() << "ID is " << aItemKey; |
| 825 |
qDebug() << "Data is " << aItemData; |
| 826 |
storageItem->write( 0, aItemData.toUtf8() ); |
| 827 |
storageItem->setId(aItemKey.toString()); |
| 828 |
storageItem->setType(iProperties[STORAGE_DEFAULT_MIME_PROP]); |
| 829 |
} |
| 830 |
else { |
| 831 |
qCWarning(lcSyncMLPlugin) << "Memory Allocation Failed"; |
| 832 |
} |
| 833 |
|
| 834 |
return storageItem; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
Buteo::StoragePlugin* ContactsStoragePluginLoader::createPlugin(const QString& aPluginName) |
| 839 |
{ |
| 840 |
return new ContactStorage(aPluginName); |
| 841 |
} |
| 842 |
|