Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Latest commit

 

History

History
591 lines (506 loc) · 20.4 KB

seasideimport.cpp

File metadata and controls

591 lines (506 loc) · 20.4 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* Copyright (C) 2013 Jolla Mobile <matthew.vogt@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "seasideimport.h"
#include "seasidecache.h"
Dec 4, 2013
Dec 4, 2013
35
#include "seasidepropertyhandler.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <QContactDetailFilter>
#include <QContactFetchHint>
#include <QContactManager>
#include <QContactSortOrder>
#include <QContactSyncTarget>
#include <QContactAddress>
#include <QContactAnniversary>
#include <QContactAvatar>
#include <QContactBirthday>
#include <QContactEmailAddress>
#include <QContactGuid>
#include <QContactHobby>
#include <QContactName>
#include <QContactNickname>
#include <QContactNote>
#include <QContactOnlineAccount>
#include <QContactOrganization>
#include <QContactPhoneNumber>
#include <QContactRingtone>
#include <QContactTag>
#include <QContactUrl>
#ifdef USING_QTPIM
#include <QContactIdFilter>
#include <QContactExtendedDetail>
#else
#include <QContactLocalIdFilter>
#endif
#include <QVersitContactExporter>
#include <QVersitContactImporter>
#include <QVersitReader>
#include <QVersitWriter>
#include <QHash>
#include <QString>
namespace {
QContactFetchHint basicFetchHint()
{
QContactFetchHint fetchHint;
fetchHint.setOptimizationHints(QContactFetchHint::NoRelationships |
QContactFetchHint::NoActionPreferences |
QContactFetchHint::NoBinaryBlobs);
return fetchHint;
}
QContactFilter localContactFilter()
{
// Contacts that are local to the device have sync target 'local' or 'was_local'
QContactDetailFilter filterLocal, filterWasLocal;
#ifdef USING_QTPIM
filterLocal.setDetailType(QContactSyncTarget::Type, QContactSyncTarget::FieldSyncTarget);
filterWasLocal.setDetailType(QContactSyncTarget::Type, QContactSyncTarget::FieldSyncTarget);
#else
filterLocal.setDetailDefinitionName(QContactSyncTarget::DefinitionName, QContactSyncTarget::FieldSyncTarget);
filterWasLocal.setDetailDefinitionName(QContactSyncTarget::DefinitionName, QContactSyncTarget::FieldSyncTarget);
#endif
filterLocal.setValue(QString::fromLatin1("local"));
filterWasLocal.setValue(QString::fromLatin1("was_local"));
return filterLocal | filterWasLocal;
}
Jan 9, 2014
Jan 9, 2014
105
106
107
108
109
110
111
112
113
114
115
116
bool nameIsEmpty(const QContactName &name)
{
if (name.isEmpty())
return true;
return (name.prefix().isEmpty() &&
name.firstName().isEmpty() &&
name.middleName().isEmpty() &&
name.lastName().isEmpty() &&
name.suffix().isEmpty());
}
117
118
119
120
QString contactNameString(const QContact &contact)
{
QStringList details;
QContactName name(contact.detail<QContactName>());
Jan 9, 2014
Jan 9, 2014
121
122
123
if (nameIsEmpty(name))
return QString();
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
details.append(name.prefix());
details.append(name.firstName());
details.append(name.middleName());
details.append(name.lastName());
details.append(name.suffix());
return details.join(QChar::fromLatin1('|'));
}
template<typename T, typename F>
QVariant detailValue(const T &detail, F field)
{
#ifdef USING_QTPIM
return detail.value(field);
#else
return detail.variantValue(field);
#endif
}
#ifdef USING_QTPIM
typedef QMap<int, QVariant> DetailMap;
#else
typedef QVariantMap DetailMap;
#endif
DetailMap detailValues(const QContactDetail &detail)
{
#ifdef USING_QTPIM
DetailMap rv(detail.values());
#else
DetailMap rv(detail.variantValues());
#endif
return rv;
}
static bool variantEqual(const QVariant &lhs, const QVariant &rhs)
{
#ifdef USING_QTPIM
// Work around incorrect result from QVariant::operator== when variants contain QList<int>
static const int QListIntType = QMetaType::type("QList<int>");
const int lhsType = lhs.userType();
if (lhsType != rhs.userType()) {
return false;
}
if (lhsType == QListIntType) {
return (lhs.value<QList<int> >() == rhs.value<QList<int> >());
}
#endif
return (lhs == rhs);
}
static bool detailValuesSuperset(const QContactDetail &lhs, const QContactDetail &rhs)
{
// True if all values in rhs are present in lhs
const DetailMap lhsValues(detailValues(lhs));
const DetailMap rhsValues(detailValues(rhs));
if (lhsValues.count() < rhsValues.count()) {
return false;
}
foreach (const DetailMap::key_type &key, rhsValues.keys()) {
if (!variantEqual(lhsValues[key], rhsValues[key])) {
return false;
}
}
return true;
}
static void fixupDetail(QContactDetail &)
{
}
#ifdef USING_QTPIM
// Fixup QContactUrl because importer produces incorrectly typed URL field
static void fixupDetail(QContactUrl &url)
{
QVariant urlField = url.value(QContactUrl::FieldUrl);
if (!urlField.isNull()) {
QString urlString = urlField.toString();
if (!urlString.isEmpty()) {
url.setValue(QContactUrl::FieldUrl, QUrl(urlString));
} else {
url.setValue(QContactUrl::FieldUrl, QVariant());
}
}
}
// Fixup QContactOrganization because importer produces invalid department
static void fixupDetail(QContactOrganization &org)
{
QVariant deptField = org.value(QContactOrganization::FieldDepartment);
if (!deptField.isNull()) {
QStringList deptList = deptField.toStringList();
// Remove any empty elements from the list
QStringList::iterator it = deptList.begin();
while (it != deptList.end()) {
if ((*it).isEmpty()) {
it = deptList.erase(it);
} else {
++it;
}
}
if (!deptList.isEmpty()) {
org.setValue(QContactOrganization::FieldDepartment, deptList);
} else {
org.setValue(QContactOrganization::FieldDepartment, QVariant());
}
}
}
#endif
template<typename T>
bool updateExistingDetails(QContact *updateContact, const QContact &importedContact, bool singular = false)
{
bool rv = false;
QList<T> existingDetails(updateContact->details<T>());
if (singular && !existingDetails.isEmpty())
return rv;
foreach (T detail, importedContact.details<T>()) {
// Make any corrections to the input
fixupDetail(detail);
// See if the contact already has a detail which is a superset of this one
bool found = false;
foreach (const T &existing, existingDetails) {
if (detailValuesSuperset(existing, detail)) {
found = true;
break;
}
}
if (!found) {
updateContact->saveDetail(&detail);
rv = true;
}
}
return rv;
}
bool mergeIntoExistingContact(QContact *updateContact, const QContact &importedContact)
{
bool rv = false;
// Update the existing contact with any details in the new import
rv |= updateExistingDetails<QContactAddress>(updateContact, importedContact);
rv |= updateExistingDetails<QContactAnniversary>(updateContact, importedContact);
rv |= updateExistingDetails<QContactAvatar>(updateContact, importedContact);
rv |= updateExistingDetails<QContactBirthday>(updateContact, importedContact, true);
rv |= updateExistingDetails<QContactEmailAddress>(updateContact, importedContact);
rv |= updateExistingDetails<QContactGuid>(updateContact, importedContact);
rv |= updateExistingDetails<QContactHobby>(updateContact, importedContact);
rv |= updateExistingDetails<QContactNickname>(updateContact, importedContact);
rv |= updateExistingDetails<QContactNote>(updateContact, importedContact);
rv |= updateExistingDetails<QContactOnlineAccount>(updateContact, importedContact);
rv |= updateExistingDetails<QContactOrganization>(updateContact, importedContact);
rv |= updateExistingDetails<QContactPhoneNumber>(updateContact, importedContact);
rv |= updateExistingDetails<QContactRingtone>(updateContact, importedContact);
rv |= updateExistingDetails<QContactTag>(updateContact, importedContact);
rv |= updateExistingDetails<QContactUrl>(updateContact, importedContact);
#ifdef USING_QTPIM
rv |= updateExistingDetails<QContactExtendedDetail>(updateContact, importedContact);
#endif
return rv;
}
bool updateExistingContact(QContact *updateContact, const QContact &contact)
{
// Replace the imported contact with the existing version
QContact importedContact(*updateContact);
*updateContact = contact;
return mergeIntoExistingContact(updateContact, importedContact);
}
Nov 5, 2013
Nov 5, 2013
307
308
309
310
311
312
313
314
315
316
317
318
319
void setNickname(QContact &contact, const QString &text)
{
foreach (const QContactNickname &nick, contact.details<QContactNickname>()) {
if (nick.nickname() == text) {
return;
}
}
QContactNickname nick;
nick.setNickname(text);
contact.saveDetail(&nick);
}
320
321
322
323
324
325
326
327
328
329
}
QList<QContact> SeasideImport::buildImportContacts(const QList<QVersitDocument> &details, int *newCount, int *updatedCount)
{
if (newCount)
*newCount = 0;
if (updatedCount)
*updatedCount = 0;
// Read the contacts from the import details
Dec 4, 2013
Dec 4, 2013
330
SeasidePropertyHandler propertyHandler;
331
QVersitContactImporter importer;
Dec 4, 2013
Dec 4, 2013
332
importer.setPropertyHandler(&propertyHandler);
333
334
335
336
importer.importDocuments(details);
QList<QContact> importedContacts(importer.contacts());
Nov 20, 2013
Nov 20, 2013
337
338
339
QHash<QString, int> importGuids;
QHash<QString, int> importNames;
QHash<QString, int> importLabels;
Jan 23, 2014
Jan 23, 2014
341
342
343
344
345
346
347
QSet<QContactDetail::DetailType> unimportableDetailTypes;
unimportableDetailTypes.insert(QContactDetail::TypeFamily);
unimportableDetailTypes.insert(QContactDetail::TypeGeoLocation);
unimportableDetailTypes.insert(QContactDetail::TypeGlobalPresence);
unimportableDetailTypes.insert(QContactDetail::TypeSyncTarget);
unimportableDetailTypes.insert(QContactDetail::TypeVersion);
348
// Merge any duplicates in the import list
Nov 20, 2013
Nov 20, 2013
349
350
QList<QContact>::iterator it = importedContacts.begin();
while (it != importedContacts.end()) {
351
352
QContact &contact(*it);
Jan 23, 2014
Jan 23, 2014
353
354
355
356
357
358
359
360
// Remove any details that our backend can't store
foreach (QContactDetail detail, contact.details()) {
if (unimportableDetailTypes.contains(detail.type())) {
qDebug() << " Removing unimportable detail:" << detail;
contact.removeDetail(&detail);
}
}
361
362
const QString guid = contact.detail<QContactGuid>().guid();
const QString name = contactNameString(contact);
Jan 9, 2014
Jan 9, 2014
363
364
365
366
367
368
369
370
371
372
373
374
const bool emptyName = name.isEmpty();
QString label;
if (emptyName) {
QContactName nameDetail = contact.detail<QContactName>();
contact.removeDetail(&nameDetail);
label = contact.detail<QContactDisplayLabel>().label();
if (label.isEmpty()) {
label = SeasideCache::generateDisplayLabelFromNonNameDetails(contact);
}
}
Nov 20, 2013
Nov 20, 2013
376
377
int previousIndex = -1;
QHash<QString, int>::const_iterator git = importGuids.find(guid);
378
if (git != importGuids.end()) {
Nov 20, 2013
Nov 20, 2013
379
previousIndex = git.value();
Jan 15, 2014
Jan 15, 2014
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
if (!emptyName) {
// If we have a GUID match, but names differ, ignore the match
const QContact &previous(importedContacts[previousIndex]);
const QString previousName = contactNameString(previous);
if (!previousName.isEmpty() && (previousName != name)) {
previousIndex = -1;
// Remove the conflicting GUID from this contact
QContactGuid guidDetail = contact.detail<QContactGuid>();
contact.removeDetail(&guidDetail);
}
}
}
if (previousIndex == -1) {
Jan 9, 2014
Jan 9, 2014
395
396
397
398
399
400
if (!emptyName) {
QHash<QString, int>::const_iterator nit = importNames.find(name);
if (nit != importNames.end()) {
previousIndex = nit.value();
}
} else if (!label.isEmpty()) {
401
// Only if name is empty, use displayLabel - probably SIM import
Jan 9, 2014
Jan 9, 2014
402
403
404
QHash<QString, int>::const_iterator lit = importLabels.find(label);
if (lit != importLabels.end()) {
previousIndex = lit.value();
405
406
407
408
}
}
}
Nov 20, 2013
Nov 20, 2013
409
if (previousIndex != -1) {
410
// Combine these duplicate contacts
Nov 20, 2013
Nov 20, 2013
411
412
413
414
QContact &previous(importedContacts[previousIndex]);
mergeIntoExistingContact(&previous, contact);
it = importedContacts.erase(it);
Nov 20, 2013
Nov 20, 2013
416
const int index = it - importedContacts.begin();
417
if (!guid.isEmpty()) {
Nov 20, 2013
Nov 20, 2013
418
importGuids.insert(guid, index);
419
420
}
if (!emptyName) {
Nov 20, 2013
Nov 20, 2013
421
importNames.insert(name, index);
422
} else if (!label.isEmpty()) {
Nov 20, 2013
Nov 20, 2013
423
importLabels.insert(label, index);
Jan 9, 2014
Jan 9, 2014
425
426
427
428
if (contact.details<QContactNickname>().isEmpty()) {
// Modify this contact to have the label as a nickname
setNickname(contact, label);
}
Nov 20, 2013
Nov 20, 2013
431
432
++it;
}
Nov 4, 2013
Nov 4, 2013
433
434
}
435
436
437
438
439
440
441
442
443
444
// Find all names and GUIDs for local contacts that might match these contacts
QContactFetchHint fetchHint(basicFetchHint());
#ifdef USING_QTPIM
fetchHint.setDetailTypesHint(QList<QContactDetail::DetailType>() << QContactName::Type << QContactNickname::Type << QContactGuid::Type);
#else
fetchHint.setDetailDefinitionsHint(QStringList() << QContactName::DefinitionName << QContactNickname::DefinitionName << QContactGuid::DefinitionName);
#endif
QHash<QString, QContactId> existingGuids;
QHash<QString, QContactId> existingNames;
Jan 15, 2014
Jan 15, 2014
445
QMap<QContactId, QString> existingContactNames;
446
447
448
449
450
451
452
453
454
455
456
457
458
QHash<QString, QContactId> existingNicknames;
QContactManager *mgr(SeasideCache::manager());
foreach (const QContact &contact, mgr->contacts(localContactFilter(), QList<QContactSortOrder>(), fetchHint)) {
const QString guid = contact.detail<QContactGuid>().guid();
const QString name = contactNameString(contact);
if (!guid.isEmpty()) {
existingGuids.insert(guid, contact.id());
}
if (!name.isEmpty()) {
existingNames.insert(name, contact.id());
Jan 15, 2014
Jan 15, 2014
459
existingContactNames.insert(contact.id(), name);
460
461
462
463
464
465
466
}
foreach (const QContactNickname &nick, contact.details<QContactNickname>()) {
existingNicknames.insert(nick.nickname(), contact.id());
}
}
// Find any imported contacts that match contacts we already have
Nov 20, 2013
Nov 20, 2013
467
QMap<QContactId, int> existingIds;
Nov 20, 2013
Nov 20, 2013
469
470
it = importedContacts.begin();
while (it != importedContacts.end()) {
471
const QString guid = (*it).detail<QContactGuid>().guid();
Jan 15, 2014
Jan 15, 2014
472
473
const QString name = contactNameString(*it);
const bool emptyName = name.isEmpty();
474
475
476
477
478
479
480
QContactId existingId;
QHash<QString, QContactId>::const_iterator git = existingGuids.find(guid);
if (git != existingGuids.end()) {
existingId = *git;
Jan 15, 2014
Jan 15, 2014
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
if (!emptyName) {
// If we have a GUID match, but names differ, ignore the match
QMap<QContactId, QString>::iterator nit = existingContactNames.find(existingId);
if (nit != existingContactNames.end()) {
const QString &existingName(*nit);
if (!existingName.isEmpty() && (existingName != name)) {
existingId = QContactId();
// Remove the conflicting GUID from this contact
QContactGuid guidDetail = (*it).detail<QContactGuid>();
(*it).removeDetail(&guidDetail);
}
}
}
}
if (existingId.isNull()) {
if (!emptyName) {
498
499
500
501
502
QHash<QString, QContactId>::const_iterator nit = existingNames.find(name);
if (nit != existingNames.end()) {
existingId = *nit;
}
} else {
Jan 9, 2014
Jan 9, 2014
503
504
505
506
507
508
509
510
foreach (const QContactNickname nick, (*it).details<QContactNickname>()) {
const QString nickname(nick.nickname());
if (!nickname.isEmpty()) {
QHash<QString, QContactId>::const_iterator nit = existingNicknames.find(nickname);
if (nit != existingNicknames.end()) {
existingId = *nit;
break;
}
511
512
513
514
515
}
}
}
}
Jan 9, 2014
Jan 9, 2014
516
if (!existingId.isNull()) {
Nov 20, 2013
Nov 20, 2013
517
QMap<QContactId, int>::iterator eit = existingIds.find(existingId);
518
if (eit == existingIds.end()) {
Nov 20, 2013
Nov 20, 2013
519
520
521
existingIds.insert(existingId, (it - importedContacts.begin()));
++it;
522
523
} else {
// Combine these contacts with matching names
Nov 20, 2013
Nov 20, 2013
524
525
QContact &previous(importedContacts[*eit]);
mergeIntoExistingContact(&previous, *it);
Nov 20, 2013
Nov 20, 2013
527
it = importedContacts.erase(it);
Nov 20, 2013
Nov 20, 2013
529
530
} else {
++it;
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
}
}
int existingCount(existingIds.count());
if (existingCount > 0) {
// Retrieve all the contacts that we have matches for
#ifdef USING_QTPIM
QContactIdFilter idFilter;
idFilter.setIds(existingIds.keys());
#else
QContactLocalIdFilter idFilter;
QList<QContactLocalId> localIds;
foreach (const QContactId &id, existingIds.keys()) {
localids.append(id.toLocal());
}
#endif
QSet<QContactId> modifiedContacts;
QSet<QContactId> unmodifiedContacts;
foreach (const QContact &contact, mgr->contacts(idFilter & localContactFilter(), QList<QContactSortOrder>(), basicFetchHint())) {
Nov 20, 2013
Nov 20, 2013
552
QMap<QContactId, int>::const_iterator it = existingIds.find(contact.id());
553
554
if (it != existingIds.end()) {
// Update the existing version of the contact with any new details
Nov 20, 2013
Nov 20, 2013
555
QContact &importContact(importedContacts[*it]);
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
bool modified = updateExistingContact(&importContact, contact);
if (modified) {
modifiedContacts.insert(importContact.id());
} else {
unmodifiedContacts.insert(importContact.id());
}
} else {
qWarning() << "unable to update existing contact:" << contact.id();
}
}
if (!unmodifiedContacts.isEmpty()) {
QList<QContact>::iterator it = importedContacts.begin();
while (it != importedContacts.end()) {
const QContact &importContact(*it);
const QContactId contactId(importContact.id());
if (unmodifiedContacts.contains(contactId) && !modifiedContacts.contains(contactId)) {
// This contact was not modified by import - don't update it
it = importedContacts.erase(it);
--existingCount;
} else {
++it;
}
}
}
}
if (updatedCount)
*updatedCount = existingCount;
if (newCount)
*newCount = importedContacts.count() - existingCount;
return importedContacts;
}