| 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) 2014 Jolla Ltd. |
| 6 |
* |
| 7 |
* Contributors: Sateesh Kavuri <[email protected]> |
| 8 |
* Bea Lam <[email protected]> |
| 9 |
* Valério Valério <[email protected]> |
| 10 |
* |
| 11 |
* This library is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU Lesser General Public License |
| 13 |
* version 2.1 as published by the Free Software Foundation. |
| 14 |
* |
| 15 |
* This library is distributed in the hope that it will be useful, but |
| 16 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this library; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 23 |
* 02110-1301 USA |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "ContactsBackend.h" |
| 28 |
#include "ContactBuilder.h" |
| 29 |
|
| 30 |
#include <seasideimport.h> |
| 31 |
#include <seasidepropertyhandler.h> |
| 32 |
|
| 33 |
#include "SyncMLPluginLogging.h" |
| 34 |
|
| 35 |
#include <QVersitContactExporter> |
| 36 |
#include <QVersitContactImporter> |
| 37 |
#include <QVersitReader> |
| 38 |
#include <QVersitWriter> |
| 39 |
#include <QContactTimestamp> |
| 40 |
#include <QContactSyncTarget> |
| 41 |
#include <QContactDetailFilter> |
| 42 |
#include <QContactUnionFilter> |
| 43 |
#include <QContactInvalidFilter> |
| 44 |
|
| 45 |
#include <qtcontacts-extensions.h> |
| 46 |
#include <QContactOriginMetadata> |
| 47 |
|
| 48 |
#include <QContactIdFilter> |
| 49 |
|
| 50 |
#include <QBuffer> |
| 51 |
#include <QSet> |
| 52 |
|
| 53 |
|
| 54 |
ContactsBackend::ContactsBackend(QVersitDocument::VersitType aVCardVer, const QString &syncTarget, const QString &originId) : |
| 55 |
iReadMgr(NULL), iWriteMgr(NULL), iVCardVer(aVCardVer) //CID 26531 |
| 56 |
, iSyncTarget(syncTarget) |
| 57 |
, iOriginId(originId) |
| 58 |
{ |
| 59 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 60 |
} |
| 61 |
|
| 62 |
ContactsBackend::~ContactsBackend() |
| 63 |
{ |
| 64 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 65 |
} |
| 66 |
|
| 67 |
bool ContactsBackend::init() |
| 68 |
{ |
| 69 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 70 |
|
| 71 |
QMap<QString, QString> params; |
| 72 |
params.insert(QStringLiteral("nonprivileged"), QStringLiteral("true")); |
| 73 |
iReadMgr = new QContactManager(QLatin1String("org.nemomobile.contacts.sqlite"), params); |
| 74 |
|
| 75 |
iWriteMgr = new QContactManager(QLatin1String("org.nemomobile.contacts.sqlite")); |
| 76 |
|
| 77 |
return (iReadMgr != NULL && iWriteMgr != NULL); |
| 78 |
} |
| 79 |
|
| 80 |
bool ContactsBackend::uninit() |
| 81 |
{ |
| 82 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 83 |
|
| 84 |
delete iReadMgr; |
| 85 |
iReadMgr = NULL; |
| 86 |
|
| 87 |
delete iWriteMgr; |
| 88 |
iWriteMgr = NULL; |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
QList<QContactLocalId> ContactsBackend::getAllContactIds() |
| 96 |
{ |
| 97 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 98 |
QList<QContactLocalId> contactIDs; |
| 99 |
|
| 100 |
if (iReadMgr != NULL) { |
| 101 |
contactIDs = iReadMgr->contactIds(); |
| 102 |
} else { |
| 103 |
qCWarning(lcSyncMLPlugin) << "Contacts backend not available"; |
| 104 |
} |
| 105 |
|
| 106 |
return contactIDs; |
| 107 |
} |
| 108 |
|
| 109 |
QList<QContactLocalId> ContactsBackend::getAllNewContactIds(const QDateTime &aTimeStamp) |
| 110 |
{ |
| 111 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 112 |
|
| 113 |
qCDebug(lcSyncMLPlugin) << "Retrieve New Contacts Since " << aTimeStamp; |
| 114 |
|
| 115 |
QList<QContactLocalId> idList; |
| 116 |
const QContactChangeLogFilter::EventType eventType = |
| 117 |
QContactChangeLogFilter::EventAdded; |
| 118 |
|
| 119 |
getSpecifiedContactIds(eventType, aTimeStamp, idList); |
| 120 |
|
| 121 |
return idList; |
| 122 |
} |
| 123 |
|
| 124 |
QList<QContactLocalId> ContactsBackend::getAllModifiedContactIds(const QDateTime &aTimeStamp) |
| 125 |
{ |
| 126 |
|
| 127 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 128 |
|
| 129 |
qCDebug(lcSyncMLPlugin) << "Retrieve Modified Contacts Since " << aTimeStamp; |
| 130 |
|
| 131 |
QList<QContactLocalId> idList; |
| 132 |
const QContactChangeLogFilter::EventType eventType = |
| 133 |
QContactChangeLogFilter::EventChanged; |
| 134 |
|
| 135 |
getSpecifiedContactIds(eventType, aTimeStamp, idList); |
| 136 |
|
| 137 |
return idList; |
| 138 |
} |
| 139 |
|
| 140 |
QList<QContactLocalId> ContactsBackend::getAllDeletedContactIds(const QDateTime &aTimeStamp) |
| 141 |
{ |
| 142 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 143 |
|
| 144 |
qCDebug(lcSyncMLPlugin) << "Retrieve Deleted Contacts Since " << aTimeStamp; |
| 145 |
|
| 146 |
QList<QContactLocalId> idList; |
| 147 |
const QContactChangeLogFilter::EventType eventType = |
| 148 |
QContactChangeLogFilter::EventRemoved; |
| 149 |
|
| 150 |
getSpecifiedContactIds(eventType, aTimeStamp, idList); |
| 151 |
|
| 152 |
return idList; |
| 153 |
} |
| 154 |
|
| 155 |
bool ContactsBackend::addContacts( const QStringList& aContactDataList, |
| 156 |
QMap<int, ContactsStatus>& aStatusMap ) |
| 157 |
{ |
| 158 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 159 |
|
| 160 |
Q_ASSERT( iReadMgr ); |
| 161 |
Q_ASSERT( iWriteMgr ); |
| 162 |
|
| 163 |
QList<QVersitDocument> documents = convertVCardListToVersitDocumentList(aContactDataList); |
| 164 |
if (documents.isEmpty()) { |
| 165 |
qCWarning(lcSyncMLPlugin) << "invalid sync data, aborting"; |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
qCDebug(lcSyncMLPlugin) << "converted" << aContactDataList.size() << "concatenated vCards into" << documents.size() << "versit documents"; |
| 170 |
|
| 171 |
int newCount = 0; |
| 172 |
int updatedCount = 0; |
| 173 |
int ignoredCount = 0; |
| 174 |
ContactBuilder builder(iWriteMgr, iSyncTarget, iOriginId, ContactBuilder::FilterRequiredMode); |
| 175 |
QList<QContact> contactList = SeasideImport::buildImportContacts( |
| 176 |
documents, |
| 177 |
&newCount, |
| 178 |
&updatedCount, |
| 179 |
&ignoredCount, |
| 180 |
&builder); |
| 181 |
qCDebug(lcSyncMLPlugin) << "imported" << contactList.size() << "contacts from" << documents.size() << "versit documents"; |
| 182 |
if (contactList.size() != documents.size()) { |
| 183 |
qCWarning(lcSyncMLPlugin) << "internal error: could not convert every versit document to a contact:" << contactList.size() << "<" << documents.size(); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
prepareContactSave(&contactList); |
| 188 |
qCDebug(lcSyncMLPlugin) << "New contacts:" << newCount << "Updated contacts:" << updatedCount; |
| 189 |
|
| 190 |
QMap<int, QContactManager::Error> errorMap; |
| 191 |
bool retVal = iWriteMgr->saveContacts(&contactList, &errorMap); |
| 192 |
if (!retVal) { |
| 193 |
qCWarning(lcSyncMLPlugin) << "Errors reported while saving contacts:" << iWriteMgr->error(); |
| 194 |
} |
| 195 |
|
| 196 |
// Populate the status value for each addition item (document). |
| 197 |
ContactsStatus status; |
| 198 |
for (int i = 0; i < documents.size(); ++i) { |
| 199 |
status.id = contactList.at(i).id().toString(); |
| 200 |
status.errorCode = errorMap.value(i, QContactManager::NoError); |
| 201 |
aStatusMap.insert(i, status); |
| 202 |
} |
| 203 |
|
| 204 |
return retVal; |
| 205 |
} |
| 206 |
|
| 207 |
QContactManager::Error ContactsBackend::modifyContact(const QString &aID, const QString &aContact) |
| 208 |
{ |
| 209 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 210 |
qCDebug(lcSyncMLPlugin) << "Modifying a Contact with ID" << aID; |
| 211 |
|
| 212 |
QContactManager::Error modificationStatus = QContactManager::UnspecifiedError; |
| 213 |
|
| 214 |
if (iWriteMgr == NULL) { |
| 215 |
qCWarning(lcSyncMLPlugin) << "Contacts backend not available"; |
| 216 |
} else { |
| 217 |
QContact oldContactData; |
| 218 |
getContact(QContactId::fromString (aID), oldContactData); |
| 219 |
|
| 220 |
QList<QVersitDocument> documents = convertVCardListToVersitDocumentList(QStringList() << aContact); |
| 221 |
if (documents.size() < 1) { |
| 222 |
qCWarning(lcSyncMLPlugin) << "Not a valid vCard:" << aContact; |
| 223 |
return QContactManager::UnspecifiedError; |
| 224 |
} |
| 225 |
|
| 226 |
int newCount = 0; |
| 227 |
int updatedCount = 0; |
| 228 |
int ignoredCount = 0; |
| 229 |
ContactBuilder builder(iWriteMgr, iSyncTarget, iOriginId, ContactBuilder::NoFilterRequiredMode); |
| 230 |
QList<QContact> contacts = SeasideImport::buildImportContacts( |
| 231 |
documents, |
| 232 |
&newCount, |
| 233 |
&updatedCount, |
| 234 |
&ignoredCount, |
| 235 |
&builder); |
| 236 |
|
| 237 |
if (contacts.size() < 1) { |
| 238 |
qCWarning(lcSyncMLPlugin) << "Unable to convert vCard to contact:" << aContact; |
| 239 |
return QContactManager::UnspecifiedError; |
| 240 |
} else if (contacts.size() > 1) { |
| 241 |
qCWarning(lcSyncMLPlugin) << "vCard encodes multiple contacts when one is expected:" << aContact; |
| 242 |
// just process the first one, ignore the rest. |
| 243 |
} |
| 244 |
|
| 245 |
QContact newContactData = contacts.first(); |
| 246 |
newContactData.setId(oldContactData.id()); |
| 247 |
bool modificationOk = iWriteMgr->saveContact(&oldContactData); |
| 248 |
modificationStatus = iWriteMgr->error(); |
| 249 |
if(!modificationOk) { |
| 250 |
qCWarning(lcSyncMLPlugin) << "Contact Modification Failed"; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return modificationStatus; |
| 255 |
} |
| 256 |
|
| 257 |
QMap<int,ContactsStatus> ContactsBackend::modifyContacts( |
| 258 |
const QStringList &aVCardDataList, const QStringList &aContactIdList) |
| 259 |
{ |
| 260 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 261 |
|
| 262 |
Q_ASSERT (iWriteMgr); |
| 263 |
ContactsStatus status; |
| 264 |
|
| 265 |
QMap<int,QContactManager::Error> errors; |
| 266 |
QMap<int,ContactsStatus> statusMap; |
| 267 |
|
| 268 |
int newCount = 0; |
| 269 |
int updatedCount = 0; |
| 270 |
int ignoredCount = 0; |
| 271 |
QList<QVersitDocument> documents = convertVCardListToVersitDocumentList(aVCardDataList); |
| 272 |
qCDebug(lcSyncMLPlugin) << "converted" << aVCardDataList.size() << "concatenated vCards into" << documents.size() << "versit documents"; |
| 273 |
ContactBuilder builder(iWriteMgr, iSyncTarget, iOriginId, ContactBuilder::NoFilterRequiredMode); |
| 274 |
QList<QContact> contacts = SeasideImport::buildImportContacts( |
| 275 |
documents, |
| 276 |
&newCount, |
| 277 |
&updatedCount, |
| 278 |
&ignoredCount, |
| 279 |
&builder); |
| 280 |
|
| 281 |
qCDebug(lcSyncMLPlugin) << "imported" << contacts.size() << "contacts from" << documents.size() << "versit documents"; |
| 282 |
if (contacts.size() != aContactIdList.size()) { |
| 283 |
qCWarning(lcSyncMLPlugin) << "internal error: could not convert every versit document to a contact:" << contacts.size() << "<" << documents.size(); |
| 284 |
} else { |
| 285 |
for (int i = 0; i < contacts.size(); i++) { |
| 286 |
qCDebug(lcSyncMLPlugin) << "Id of the contact to be replaced" << aContactIdList.at(i); |
| 287 |
QContactLocalId uniqueContactItemID = QContactId::fromString (aContactIdList.at(i)); |
| 288 |
contacts[i].setId(uniqueContactItemID); |
| 289 |
qCDebug(lcSyncMLPlugin) << "Replacing item's ID " << contacts.at(i); |
| 290 |
} |
| 291 |
|
| 292 |
if(iWriteMgr->saveContacts(&contacts , &errors)) { |
| 293 |
qCDebug(lcSyncMLPlugin) << "Batch Modification of Contacts Succeeded"; |
| 294 |
} else { |
| 295 |
qCDebug(lcSyncMLPlugin) << "Batch Modification of Contacts Failed"; |
| 296 |
} |
| 297 |
|
| 298 |
// QContactManager will populate errorMap only for errors, but we use this as a status map, |
| 299 |
// so populate NoError if there's no error. |
| 300 |
// TODO QContactManager populates indices from the qContactList, but we populate keys, is this OK? |
| 301 |
for (int i = 0; i < contacts.size(); i++) { |
| 302 |
QContactLocalId contactId = contacts.at(i).id(); |
| 303 |
status.id = contactId.toString (); |
| 304 |
if( !errors.contains(i) ) { |
| 305 |
qCDebug(lcSyncMLPlugin) << "No error for contact with id " << contactId << " and index " << i; |
| 306 |
status.errorCode = QContactManager::NoError; |
| 307 |
} else { |
| 308 |
qCDebug(lcSyncMLPlugin) << "contact with id " << contactId << " and index " << i <<" is in error"; |
| 309 |
QContactManager::Error errorCode = errors.value(i); |
| 310 |
status.errorCode = errorCode; |
| 311 |
} |
| 312 |
statusMap.insert(i, status); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return statusMap; |
| 317 |
} |
| 318 |
|
| 319 |
QMap<int , ContactsStatus> ContactsBackend::deleteContacts(const QStringList &aContactIDList) |
| 320 |
{ |
| 321 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 322 |
|
| 323 |
ContactsStatus status; |
| 324 |
QMap<int , QContactManager::Error> errors; |
| 325 |
QMap<int , ContactsStatus> statusMap; |
| 326 |
|
| 327 |
if (iWriteMgr == NULL) { |
| 328 |
for (int i=0; i < aContactIDList.size(); i++) { |
| 329 |
errors.insert(i, QContactManager::UnspecifiedError); |
| 330 |
} |
| 331 |
|
| 332 |
qCWarning(lcSyncMLPlugin) << "Contacts backend not available"; |
| 333 |
} |
| 334 |
else { |
| 335 |
QList<QContactLocalId> qContactIdList; |
| 336 |
foreach (QString id, aContactIDList ) { |
| 337 |
qContactIdList.append(QContactLocalId::fromString (id)); |
| 338 |
} |
| 339 |
|
| 340 |
if(iWriteMgr->removeContacts(qContactIdList , &errors)) { |
| 341 |
qCDebug(lcSyncMLPlugin) << "Successfully Removed all contacts "; |
| 342 |
} |
| 343 |
else { |
| 344 |
qCWarning(lcSyncMLPlugin) << "Failed Removing Contacts"; |
| 345 |
} |
| 346 |
|
| 347 |
// QContactManager will populate errorMap only for errors, but we use this as a status map, |
| 348 |
// so populate NoError if there's no error. |
| 349 |
// TODO QContactManager populates indices from the qContactList, but we populate keys, is this OK? |
| 350 |
for (int i = 0; i < qContactIdList.size(); i++) { |
| 351 |
QContactLocalId contactId = qContactIdList.value(i); |
| 352 |
status.id = contactId.toString (); |
| 353 |
|
| 354 |
if( !errors.contains(i) ) |
| 355 |
{ |
| 356 |
qCDebug(lcSyncMLPlugin) << "No error for contact with id " << contactId << " and index " << i; |
| 357 |
status.errorCode = QContactManager::NoError; |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
qCDebug(lcSyncMLPlugin) << "contact with id " << contactId << " and index " << i <<" is in error"; |
| 362 |
QContactManager::Error errorCode = errors.value(i); |
| 363 |
status.errorCode = errorCode; |
| 364 |
} |
| 365 |
statusMap.insert(i, status); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return statusMap; |
| 370 |
} |
| 371 |
|
| 372 |
void ContactsBackend::prepareContactSave(QList<QContact> *contactList) |
| 373 |
{ |
| 374 |
if (!iSyncTarget.isEmpty() || !iOriginId.isEmpty()) { |
| 375 |
for (int i=0; i<contactList->count(); i++) { |
| 376 |
QContact *contact = &((*contactList)[i]); |
| 377 |
if (!iSyncTarget.isEmpty()) { |
| 378 |
QContactSyncTarget syncTarget = contact->detail<QContactSyncTarget>(); |
| 379 |
syncTarget.setSyncTarget(iSyncTarget); |
| 380 |
contact->saveDetail(&syncTarget); |
| 381 |
} |
| 382 |
if (!iOriginId.isEmpty()) { |
| 383 |
QContactOriginMetadata originMetaData = contact->detail<QContactOriginMetadata>(); |
| 384 |
originMetaData.setId(iOriginId); |
| 385 |
contact->saveDetail(&originMetaData); |
| 386 |
} |
| 387 |
Q_FOREACH (QContactDetail det, contact->details()) { |
| 388 |
det.setValue(QContactDetail__FieldModifiable, true); |
| 389 |
contact->saveDetail(&det); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
QList<QVersitDocument> ContactsBackend::convertVCardListToVersitDocumentList(const QStringList &aVCardList) |
| 396 |
{ |
| 397 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 398 |
|
| 399 |
QList<QVersitDocument> retn; |
| 400 |
Q_FOREACH (const QString &vCard, aVCardList) { |
| 401 |
// remove any characters after the END:VCARD stanza. |
| 402 |
// importantly, we do NOT ensure it ends in \r\n or \r\n\r\n |
| 403 |
// TODO: fix QVersitReader to strip \r\n and \r\n\r\n endings. |
| 404 |
int endIdx = vCard.lastIndexOf(QStringLiteral("END:VCARD"), -1, Qt::CaseInsensitive); |
| 405 |
QString modifiedVCard = vCard.mid(0, endIdx + 9); /* 9 = strlen("END:VCARD") */ |
| 406 |
|
| 407 |
// convert the vCard to a contact. |
| 408 |
QVersitReader versitReader(modifiedVCard.toUtf8()); |
| 409 |
versitReader.startReading(); |
| 410 |
versitReader.waitForFinished(); |
| 411 |
|
| 412 |
QList<QVersitDocument> results = versitReader.results(); |
| 413 |
if (results.size() == 0) { |
| 414 |
qCWarning(lcSyncMLPlugin) << "Unable to convert vCard to versit document:" << versitReader.error() << ":"; |
| 415 |
QStringList erroneousVCardLines = modifiedVCard.split('\n', QString::KeepEmptyParts); |
| 416 |
Q_FOREACH(QString line, erroneousVCardLines) { |
| 417 |
if (line.contains(':') || line.trimmed().isEmpty()) { |
| 418 |
line.replace('\r', "<CR>"); |
| 419 |
line.append("<LF>"); |
| 420 |
qCWarning(lcSyncMLPlugin) << line; |
| 421 |
} |
| 422 |
} |
| 423 |
return QList<QVersitDocument>(); |
| 424 |
} else if (versitReader.results().size() > 1) { |
| 425 |
qCWarning(lcSyncMLPlugin) << "Multiple contacts from single vCard:" << modifiedVCard; |
| 426 |
} |
| 427 |
|
| 428 |
retn.append(results.first()); |
| 429 |
} |
| 430 |
|
| 431 |
return retn; |
| 432 |
} |
| 433 |
|
| 434 |
QString ContactsBackend::convertQContactToVCard(const QContact &aContact) |
| 435 |
{ |
| 436 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 437 |
|
| 438 |
QList<QContact> contactsList; |
| 439 |
contactsList.append (aContact); |
| 440 |
|
| 441 |
QVersitContactExporter contactExporter; |
| 442 |
|
| 443 |
QSet<QContactDetail::DetailType> ignoredDetailTypes = QSet<QContactDetail::DetailType>() |
| 444 |
<< QContactDetail::TypeGlobalPresence |
| 445 |
<< QContactDetail::TypePresence |
| 446 |
<< QContactDetail::TypeOnlineAccount |
| 447 |
<< QContactDetail::TypeVersion |
| 448 |
<< QContactDetail::TypeSyncTarget |
| 449 |
<< QContactDetail::TypeRingtone; |
| 450 |
SeasidePropertyHandler handler(ignoredDetailTypes); |
| 451 |
contactExporter.setDetailHandler(&handler); |
| 452 |
|
| 453 |
QString vCard; |
| 454 |
bool contactsExported = contactExporter.exportContacts(contactsList, iVCardVer); |
| 455 |
if (contactsExported){ |
| 456 |
QList<QVersitDocument> versitDocumentList; |
| 457 |
versitDocumentList = contactExporter.documents(); |
| 458 |
|
| 459 |
QBuffer writeBuf; |
| 460 |
writeBuf.open(QBuffer::ReadWrite); |
| 461 |
|
| 462 |
QVersitWriter writer; |
| 463 |
writer.setDevice(&writeBuf); |
| 464 |
|
| 465 |
if (!writer.startWriting(versitDocumentList)) { |
| 466 |
qCCritical(lcSyncMLPlugin) << "Error While writing -- " << writer.error(); |
| 467 |
} |
| 468 |
|
| 469 |
if (writer.waitForFinished()) { |
| 470 |
vCard = writeBuf.buffer(); |
| 471 |
} |
| 472 |
|
| 473 |
writeBuf.close(); |
| 474 |
} |
| 475 |
return vCard; |
| 476 |
} |
| 477 |
|
| 478 |
QMap<QString, QString> ContactsBackend::convertQContactListToVCardList( |
| 479 |
const QList<QContact> & aContactList) |
| 480 |
{ |
| 481 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 482 |
QMap<QString, QString> idDataMap; |
| 483 |
|
| 484 |
foreach (QContact contact, aContactList) { |
| 485 |
idDataMap[contact.id ().toString ()] = convertQContactToVCard(contact); |
| 486 |
} |
| 487 |
|
| 488 |
return idDataMap; |
| 489 |
} |
| 490 |
|
| 491 |
void ContactsBackend::getSpecifiedContactIds(const QContactChangeLogFilter::EventType aEventType, |
| 492 |
const QDateTime& aTimeStamp, QList<QContactLocalId>& aIdList) |
| 493 |
{ |
| 494 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 495 |
|
| 496 |
QContactChangeLogFilter filter(aEventType); |
| 497 |
filter.setSince(aTimeStamp); |
| 498 |
|
| 499 |
aIdList = iReadMgr->contactIds(filter); |
| 500 |
|
| 501 |
// Fetch the ids from aIdList |
| 502 |
QList<QString> strIdList; |
| 503 |
foreach (const QContactId& id, aIdList) { |
| 504 |
strIdList << id.toString (); |
| 505 |
} |
| 506 |
|
| 507 |
// Filter out ids for items that were added after the specified time. |
| 508 |
if (aEventType != QContactChangeLogFilter::EventAdded) |
| 509 |
{ |
| 510 |
filter.setEventType(QContactChangeLogFilter::EventAdded); |
| 511 |
QList<QContactLocalId> addedList = iReadMgr->contactIds(filter); |
| 512 |
|
| 513 |
QList<QString> addedStrIdList; |
| 514 |
foreach (const QContactId& id, addedList) { |
| 515 |
addedStrIdList << id.toString (); |
| 516 |
} |
| 517 |
|
| 518 |
foreach (const QString addedId, addedStrIdList) { |
| 519 |
strIdList.removeAll (addedId); |
| 520 |
} |
| 521 |
|
| 522 |
foreach (const QContactLocalId &id, addedList) |
| 523 |
{ |
| 524 |
aIdList.removeAll(id); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
// This is a defensive procedure to prevent duplicate items being sent. |
| 529 |
// QSet does not allow duplicates, thus transforming QList to QSet and back |
| 530 |
// again will remove any duplicate items in the original QList. |
| 531 |
int originalIdCount = strIdList.size (); |
| 532 |
QSet<QString> idSet = strIdList.toSet (); |
| 533 |
int idCountAfterDupRemoval = idSet.size (); |
| 534 |
strIdList = idSet.toList (); |
| 535 |
|
| 536 |
qCDebug(lcSyncMLPlugin) << "Item IDs found (returned / incl. duplicates): " << idCountAfterDupRemoval << "/" << originalIdCount; |
| 537 |
|
| 538 |
if (originalIdCount != idCountAfterDupRemoval) { |
| 539 |
qCWarning(lcSyncMLPlugin) << "Contacts backend returned duplicate items for requested list"; |
| 540 |
qCWarning(lcSyncMLPlugin) << "Duplicate item IDs have been removed"; |
| 541 |
} // no else |
| 542 |
|
| 543 |
// Convert strIdList to aIdList (QContactId) |
| 544 |
aIdList.clear(); |
| 545 |
foreach (const QString &id, strIdList) { |
| 546 |
aIdList << QContactId::fromString (id); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
QDateTime ContactsBackend::lastModificationTime(const QContactLocalId &aContactId) |
| 551 |
{ |
| 552 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 553 |
|
| 554 |
QDateTime lastModificationTime = QDateTime::fromTime_t(0); |
| 555 |
|
| 556 |
if (iReadMgr == NULL) { |
| 557 |
qCWarning(lcSyncMLPlugin) << "Contacts backend not available"; |
| 558 |
} |
| 559 |
else { |
| 560 |
QContact contact; |
| 561 |
getContact(aContactId, contact); |
| 562 |
QContactTimestamp contactTimestamps; |
| 563 |
contactTimestamps = contact.detail (QContactTimestamp::Type); |
| 564 |
lastModificationTime = contactTimestamps.lastModified(); |
| 565 |
} |
| 566 |
|
| 567 |
return lastModificationTime; |
| 568 |
} |
| 569 |
|
| 570 |
/*! |
| 571 |
\fn ContactsBackend::getContact(QContactLocalId aContactId) |
| 572 |
*/ |
| 573 |
void ContactsBackend::getContact(const QContactLocalId& aContactId, QContact& aContact) |
| 574 |
{ |
| 575 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 576 |
|
| 577 |
QList<QContactLocalId> contactId; |
| 578 |
contactId.append(aContactId); |
| 579 |
QList<QContact> returnedContacts; |
| 580 |
|
| 581 |
getContacts(contactId, returnedContacts); |
| 582 |
|
| 583 |
if (!returnedContacts.isEmpty()) { |
| 584 |
aContact = returnedContacts.first(); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/*! |
| 589 |
\fn ContactsBackend::getContacts(QContactLocalId aContactId) |
| 590 |
*/ |
| 591 |
void ContactsBackend::getContacts(const QList<QContactLocalId>& aContactIds, |
| 592 |
QList<QContact>& aContacts) |
| 593 |
{ |
| 594 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 595 |
|
| 596 |
QContactIdFilter contactFilter; |
| 597 |
contactFilter.setIds(aContactIds); |
| 598 |
|
| 599 |
if (iReadMgr != NULL) { |
| 600 |
aContacts = iReadMgr->contacts(contactFilter); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
void ContactsBackend::getContacts(const QList<QContactLocalId>& aIdsList, |
| 605 |
QMap<QString,QString>& aDataMap) |
| 606 |
{ |
| 607 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 608 |
|
| 609 |
QList<QContact> returnedContacts; |
| 610 |
|
| 611 |
// As this is an overloaded convenience function, these two functions |
| 612 |
// are utilized to get contacts from the backend and to convert them |
| 613 |
// to vcard format. |
| 614 |
getContacts(aIdsList, returnedContacts); |
| 615 |
aDataMap = convertQContactListToVCardList(returnedContacts); |
| 616 |
} |
| 617 |
|
| 618 |
QDateTime ContactsBackend::getCreationTime( const QContact& aContact ) |
| 619 |
{ |
| 620 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 621 |
|
| 622 |
QContactTimestamp contactTimestamp = aContact.detail<QContactTimestamp>(); |
| 623 |
|
| 624 |
return contactTimestamp.created(); |
| 625 |
} |
| 626 |
|
| 627 |
QList<QDateTime> ContactsBackend::getCreationTimes( const QList<QContactLocalId>& aContactIds ) |
| 628 |
{ |
| 629 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 630 |
|
| 631 |
Q_ASSERT( iReadMgr ); |
| 632 |
|
| 633 |
/* Retrieve QContacts from backend based on id's in aContactsIds. Since we're only interested |
| 634 |
* in timestamps, set up fetch hint accordingly to speed up the operation. |
| 635 |
*/ |
| 636 |
QList<QDateTime> creationTimes; |
| 637 |
QList<QContact> contacts; |
| 638 |
|
| 639 |
QContactIdFilter contactFilter; |
| 640 |
contactFilter.setIds(aContactIds); |
| 641 |
|
| 642 |
QList<QContactDetail::DetailType> detailTypes; |
| 643 |
detailTypes << QContactTimestamp::Type; |
| 644 |
|
| 645 |
/* Set up fetch hints so that not all details of QContacts be fetched: |
| 646 |
* 1) Fetch only QContactTimestamp details |
| 647 |
* 2) Do not try to resolve contact relationships (siblings etc) |
| 648 |
* 3) Do not include action preferences of contacts |
| 649 |
* 4) Do not fetch binary blogs (avatar pictures etc) |
| 650 |
*/ |
| 651 |
QContactFetchHint contactHint; |
| 652 |
contactHint.setOptimizationHints( QContactFetchHint::NoRelationships | |
| 653 |
QContactFetchHint::NoActionPreferences | |
| 654 |
QContactFetchHint::NoBinaryBlobs ); |
| 655 |
|
| 656 |
contactHint.setDetailTypesHint (detailTypes); |
| 657 |
|
| 658 |
QDateTime currentTime = QDateTime::currentDateTime(); |
| 659 |
|
| 660 |
contacts = iReadMgr->contacts( contactFilter, QList<QContactSortOrder>(), contactHint ); |
| 661 |
|
| 662 |
if( contacts.count() == aContactIds.count() ) |
| 663 |
{ |
| 664 |
for( int i = 0; i < aContactIds.count(); ++i ) |
| 665 |
{ |
| 666 |
QDateTime creationTime = currentTime; |
| 667 |
|
| 668 |
for( int a = 0; a < contacts.count(); ++a ) |
| 669 |
{ |
| 670 |
if( contacts[a].id().toString () == aContactIds[i].toString () ) |
| 671 |
{ |
| 672 |
QContactTimestamp contactTimestamp = contacts[a].detail<QContactTimestamp>(); |
| 673 |
if( !contactTimestamp.created().isNull() && |
| 674 |
contactTimestamp.created().isValid() ) |
| 675 |
{ |
| 676 |
creationTime = contactTimestamp.created(); |
| 677 |
} |
| 678 |
contacts.removeAt( a ); |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
creationTimes.append( creationTime ); |
| 684 |
} |
| 685 |
} |
| 686 |
else |
| 687 |
{ |
| 688 |
qCWarning(lcSyncMLPlugin) << "Unable to fetch creation times"; |
| 689 |
for( int i = 0; i < aContactIds.count(); ++i ) |
| 690 |
{ |
| 691 |
creationTimes.append( currentTime ); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
return creationTimes; |
| 696 |
} |