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

Latest commit

 

History

History
3202 lines (2676 loc) · 111 KB

seasidecache.cpp

File metadata and controls

3202 lines (2676 loc) · 111 KB
 
Jul 16, 2013
Jul 16, 2013
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
35
/*
* Copyright (C) 2013 Jolla Mobile <andrew.den.exter@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 "seasidecache.h"
#include "synchronizelists.h"
Dec 10, 2013
Dec 10, 2013
36
37
38
39
40
#include <qtcontacts-extensions_impl.h>
#include <qcontactstatusflags_impl.h>
#include <contactmanagerengine.h>
#include <private/qcontactmanager_p.h>
Jul 30, 2013
Jul 30, 2013
41
Jul 16, 2013
Jul 16, 2013
42
43
#include <QCoreApplication>
#include <QStandardPaths>
Dec 10, 2013
Dec 10, 2013
44
#include <QDBusConnection>
Jul 16, 2013
Jul 16, 2013
45
46
47
48
49
50
51
52
53
#include <QDir>
#include <QEvent>
#include <QFile>
#include <QContactAvatar>
#include <QContactDetailFilter>
#include <QContactDisplayLabel>
#include <QContactEmailAddress>
#include <QContactFavorite>
Aug 5, 2013
Aug 5, 2013
54
#include <QContactGender>
Jul 16, 2013
Jul 16, 2013
55
56
57
58
59
60
61
#include <QContactName>
#include <QContactNickname>
#include <QContactOnlineAccount>
#include <QContactOrganization>
#include <QContactPhoneNumber>
#include <QContactGlobalPresence>
#include <QContactSyncTarget>
Jan 9, 2014
Jan 9, 2014
62
#include <QContactTimestamp>
Jul 16, 2013
Jul 16, 2013
63
64
65
66
67
68
69
70
#include <QVersitContactExporter>
#include <QVersitContactImporter>
#include <QVersitReader>
#include <QVersitWriter>
#include <QtDebug>
Sep 6, 2013
Sep 6, 2013
71
72
#include <mlocale.h>
Dec 10, 2013
Dec 10, 2013
73
74
75
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
Aug 6, 2013
Aug 6, 2013
76
QTVERSIT_USE_NAMESPACE
Jul 16, 2013
Jul 16, 2013
77
Aug 5, 2013
Aug 5, 2013
78
namespace {
Jul 16, 2013
Jul 16, 2013
79
May 27, 2014
May 27, 2014
80
81
Q_GLOBAL_STATIC(CacheConfiguration, cacheConfig)
Sep 6, 2013
Sep 6, 2013
82
83
ML10N::MLocale mLocale;
Jan 28, 2014
Jan 28, 2014
84
const QString aggregateRelationshipType = QContactRelationship::Aggregates();
Jul 31, 2014
Jul 31, 2014
85
const QString isNotRelationshipType = QString::fromLatin1("IsNot");
Jul 16, 2013
Jul 16, 2013
86
Aug 5, 2013
Aug 5, 2013
87
88
const QString syncTargetLocal = QLatin1String("local");
const QString syncTargetWasLocal = QLatin1String("was_local");
Jul 16, 2013
Jul 16, 2013
89
Jan 15, 2014
Jan 15, 2014
90
91
92
93
94
int getContactNameGroupCount()
{
return mLocale.exemplarCharactersIndex().count();
}
Sep 6, 2013
Sep 6, 2013
95
96
97
98
QStringList getAllContactNameGroups()
{
QStringList groups(mLocale.exemplarCharactersIndex());
groups.append(QString::fromLatin1("#"));
Jul 16, 2013
Jul 16, 2013
99
100
101
return groups;
}
Aug 5, 2013
Aug 5, 2013
102
QString managerName()
Jul 16, 2013
Jul 16, 2013
103
{
Dec 10, 2013
Dec 10, 2013
104
return QString::fromLatin1("org.nemomobile.contacts.sqlite");
Jul 16, 2013
Jul 16, 2013
105
106
}
Dec 10, 2013
Dec 10, 2013
107
108
109
110
111
112
113
114
115
QMap<QString, QString> managerParameters()
{
QMap<QString, QString> rv;
// Report presence changes independently from other contact changes
rv.insert(QString::fromLatin1("mergePresenceChanges"), QString::fromLatin1("false"));
return rv;
}
Q_GLOBAL_STATIC_WITH_ARGS(QContactManager, manager, (managerName(), managerParameters()))
Oct 21, 2013
Oct 21, 2013
116
Jan 28, 2014
Jan 28, 2014
117
typedef QList<QContactDetail::DetailType> DetailList;
Aug 5, 2013
Aug 5, 2013
118
119
template<typename T>
Jan 28, 2014
Jan 28, 2014
120
QContactDetail::DetailType detailType()
Aug 5, 2013
Aug 5, 2013
121
122
123
124
{
return T::Type;
}
Jan 28, 2014
Jan 28, 2014
125
QContactDetail::DetailType detailType(const QContactDetail &detail)
Aug 5, 2013
Aug 5, 2013
126
127
128
129
{
return detail.type();
}
Jul 16, 2013
Jul 16, 2013
130
131
132
133
134
135
template<typename T, typename Filter, typename Field>
void setDetailType(Filter &filter, Field field)
{
filter.setDetailType(T::Type, field);
}
Aug 5, 2013
Aug 5, 2013
136
137
138
139
140
141
142
143
144
145
DetailList detailTypesHint(const QContactFetchHint &hint)
{
return hint.detailTypesHint();
}
void setDetailTypesHint(QContactFetchHint &hint, const DetailList &types)
{
hint.setDetailTypesHint(types);
}
Aug 5, 2013
Aug 5, 2013
146
147
148
149
150
151
152
153
154
155
156
157
QContactFetchHint basicFetchHint()
{
QContactFetchHint fetchHint;
// We generally have no use for these things:
fetchHint.setOptimizationHints(QContactFetchHint::NoRelationships |
QContactFetchHint::NoActionPreferences |
QContactFetchHint::NoBinaryBlobs);
return fetchHint;
}
Dec 10, 2013
Dec 10, 2013
158
159
160
161
162
QContactFetchHint presenceFetchHint()
{
QContactFetchHint fetchHint(basicFetchHint());
setDetailTypesHint(fetchHint, DetailList() << detailType<QContactPresence>()
Jun 19, 2014
Jun 19, 2014
163
164
<< detailType<QContactGlobalPresence>()
<< detailType<QContactOnlineAccount>());
Dec 10, 2013
Dec 10, 2013
165
166
167
168
return fetchHint;
}
Jan 9, 2014
Jan 9, 2014
169
DetailList contactsTableDetails()
Aug 5, 2013
Aug 5, 2013
170
171
{
DetailList types;
Jan 9, 2014
Jan 9, 2014
172
173
// These details are reported in every query
Aug 5, 2013
Aug 5, 2013
174
175
176
177
types << detailType<QContactSyncTarget>() <<
detailType<QContactName>() <<
detailType<QContactDisplayLabel>() <<
detailType<QContactFavorite>() <<
Jan 9, 2014
Jan 9, 2014
178
detailType<QContactTimestamp>() <<
Aug 5, 2013
Aug 5, 2013
179
180
181
detailType<QContactGender>() <<
detailType<QContactStatusFlags>();
Jan 9, 2014
Jan 9, 2014
182
183
184
185
186
187
188
189
190
191
return types;
}
QContactFetchHint metadataFetchHint(quint32 fetchTypes = 0)
{
QContactFetchHint fetchHint(basicFetchHint());
// Include all detail types which come from the main contacts table
DetailList types(contactsTableDetails());
Jan 9, 2014
Jan 9, 2014
192
193
194
// Include nickname, as some contacts have no other name
types << detailType<QContactNickname>();
Aug 5, 2013
Aug 5, 2013
195
196
197
198
199
200
201
202
203
if (fetchTypes & SeasideCache::FetchAccountUri) {
types << detailType<QContactOnlineAccount>();
}
if (fetchTypes & SeasideCache::FetchPhoneNumber) {
types << detailType<QContactPhoneNumber>();
}
if (fetchTypes & SeasideCache::FetchEmailAddress) {
types << detailType<QContactEmailAddress>();
}
Jan 9, 2014
Jan 9, 2014
204
205
206
if (fetchTypes & SeasideCache::FetchOrganization) {
types << detailType<QContactOrganization>();
}
Aug 5, 2013
Aug 5, 2013
207
Aug 5, 2013
Aug 5, 2013
208
setDetailTypesHint(fetchHint, types);
Aug 5, 2013
Aug 5, 2013
209
210
211
212
213
214
215
216
return fetchHint;
}
QContactFetchHint onlineFetchHint(quint32 fetchTypes = 0)
{
QContactFetchHint fetchHint(metadataFetchHint(fetchTypes));
// We also need global presence state
Aug 5, 2013
Aug 5, 2013
217
setDetailTypesHint(fetchHint, detailTypesHint(fetchHint) << detailType<QContactGlobalPresence>());
Aug 5, 2013
Aug 5, 2013
218
219
220
221
222
223
224
225
return fetchHint;
}
QContactFetchHint favoriteFetchHint(quint32 fetchTypes = 0)
{
QContactFetchHint fetchHint(onlineFetchHint(fetchTypes));
// We also need avatar info
Aug 5, 2013
Aug 5, 2013
226
setDetailTypesHint(fetchHint, detailTypesHint(fetchHint) << detailType<QContactAvatar>());
Aug 5, 2013
Aug 5, 2013
227
228
229
return fetchHint;
}
Jan 9, 2014
Jan 9, 2014
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
QContactFetchHint extendedMetadataFetchHint(quint32 fetchTypes)
{
QContactFetchHint fetchHint(basicFetchHint());
DetailList types;
// Only query for the specific types we need
if (fetchTypes & SeasideCache::FetchAccountUri) {
types << detailType<QContactOnlineAccount>();
}
if (fetchTypes & SeasideCache::FetchPhoneNumber) {
types << detailType<QContactPhoneNumber>();
}
if (fetchTypes & SeasideCache::FetchEmailAddress) {
types << detailType<QContactEmailAddress>();
}
if (fetchTypes & SeasideCache::FetchOrganization) {
types << detailType<QContactOrganization>();
}
setDetailTypesHint(fetchHint, types);
return fetchHint;
}
Aug 5, 2013
Aug 5, 2013
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
QContactFilter allFilter()
{
return QContactFilter();
}
QContactFilter favoriteFilter()
{
return QContactFavorite::match();
}
QContactFilter nonfavoriteFilter()
{
QContactDetailFilter filter;
setDetailType<QContactFavorite>(filter, QContactFavorite::FieldFavorite);
filter.setMatchFlags(QContactFilter::MatchExactly);
filter.setValue(false);
return filter;
}
QContactFilter onlineFilter()
{
Aug 6, 2013
Aug 6, 2013
276
return QContactStatusFlags::matchFlag(QContactStatusFlags::IsOnline);
Aug 5, 2013
Aug 5, 2013
277
278
279
280
}
QContactFilter aggregateFilter()
{
Sep 2, 2013
Sep 2, 2013
281
282
static const QString aggregate(QString::fromLatin1("aggregate"));
Aug 5, 2013
Aug 5, 2013
283
284
QContactDetailFilter filter;
setDetailType<QContactSyncTarget>(filter, QContactSyncTarget::FieldSyncTarget);
Sep 2, 2013
Sep 2, 2013
285
filter.setValue(aggregate);
Aug 5, 2013
Aug 5, 2013
286
287
288
289
return filter;
}
Aug 30, 2013
Aug 30, 2013
290
291
typedef QPair<QString, QString> StringPair;
Oct 24, 2013
Oct 24, 2013
292
QList<StringPair> addressPairs(const QContactPhoneNumber &phoneNumber)
Aug 30, 2013
Aug 30, 2013
293
{
Oct 24, 2013
Oct 24, 2013
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
QList<StringPair> rv;
const QString normalized(SeasideCache::normalizePhoneNumber(phoneNumber.number()));
if (!normalized.isEmpty()) {
const QChar plus(QChar::fromLatin1('+'));
if (normalized.startsWith(plus)) {
// Also index the complete form of this number
rv.append(qMakePair(QString(), normalized));
}
// Always index the minimized form of the number
const QString minimized(SeasideCache::minimizePhoneNumber(normalized));
rv.append(qMakePair(QString(), minimized));
}
return rv;
Aug 30, 2013
Aug 30, 2013
310
311
312
313
314
315
316
317
318
}
StringPair addressPair(const QContactEmailAddress &emailAddress)
{
return qMakePair(emailAddress.emailAddress().toLower(), QString());
}
StringPair addressPair(const QContactOnlineAccount &account)
{
Oct 15, 2013
Oct 15, 2013
319
StringPair address = qMakePair(account.value<QString>(QContactOnlineAccount__FieldAccountPath), account.accountUri().toLower());
Apr 24, 2014
Apr 24, 2014
320
return !address.first.isEmpty() && !address.second.isEmpty() ? address : StringPair();
Oct 15, 2013
Oct 15, 2013
321
322
323
324
}
bool validAddressPair(const StringPair &address)
{
Apr 24, 2014
Apr 24, 2014
325
return (!address.first.isEmpty() || !address.second.isEmpty());
Aug 30, 2013
Aug 30, 2013
326
327
}
Sep 2, 2013
Sep 2, 2013
328
329
330
331
332
333
334
335
336
337
338
339
340
341
bool ignoreContactForNameGroups(const QContact &contact)
{
static const QString aggregate(QString::fromLatin1("aggregate"));
// Don't include the self contact in name groups
if (SeasideCache::apiId(contact) == SeasideCache::selfContactId()) {
return true;
}
// Also ignore non-aggregate contacts
QContactSyncTarget syncTarget = contact.detail<QContactSyncTarget>();
return (syncTarget.syncTarget() != aggregate);
}
Jan 28, 2014
Jan 28, 2014
342
QList<quint32> internalIds(const QList<QContactId> &ids)
Sep 20, 2013
Sep 20, 2013
343
344
345
346
{
QList<quint32> rv;
rv.reserve(ids.count());
Jan 28, 2014
Jan 28, 2014
347
foreach (const QContactId &id, ids) {
Sep 20, 2013
Sep 20, 2013
348
349
350
351
352
353
rv.append(SeasideCache::internalId(id));
}
return rv;
}
Oct 24, 2013
Oct 24, 2013
354
355
356
357
358
359
360
361
362
363
364
QString::const_iterator firstDtmfChar(QString::const_iterator it, QString::const_iterator end)
{
static const QString dtmfChars(QString::fromLatin1("pPwWxX#*"));
for ( ; it != end; ++it) {
if (dtmfChars.contains(*it))
return it;
}
return end;
}
Oct 24, 2013
Oct 24, 2013
365
366
const int ExactMatch = 100;
Oct 24, 2013
Oct 24, 2013
367
368
369
370
371
372
373
374
375
376
377
378
int matchLength(const QString &lhs, const QString &rhs)
{
if (lhs.isEmpty() || rhs.isEmpty())
return 0;
QString::const_iterator lbegin = lhs.constBegin(), lend = lhs.constEnd();
QString::const_iterator rbegin = rhs.constBegin(), rend = rhs.constEnd();
// Do these numbers contain DTMF elements?
QString::const_iterator ldtmf = firstDtmfChar(lbegin, lend);
QString::const_iterator rdtmf = firstDtmfChar(rbegin, rend);
Oct 24, 2013
Oct 24, 2013
379
380
QString::const_iterator lit, rit;
bool processDtmf = false;
Oct 24, 2013
Oct 24, 2013
381
int matchLength = 0;
Oct 24, 2013
Oct 24, 2013
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
if ((ldtmf != lbegin) && (rdtmf != rbegin)) {
// Start match length calculation at the last non-DTMF digit
lit = ldtmf - 1;
rit = rdtmf - 1;
while (*lit == *rit) {
++matchLength;
--lit;
--rit;
if ((lit == lbegin) || (rit == rbegin)) {
if (*lit == *rit) {
++matchLength;
if ((lit == lbegin) && (rit == rbegin)) {
// We have a complete, exact match - this must be the best match
return ExactMatch;
} else {
// We matched all of one number - continue looking in the DTMF part
processDtmf = true;
}
Oct 24, 2013
Oct 24, 2013
404
}
Oct 24, 2013
Oct 24, 2013
405
break;
Oct 24, 2013
Oct 24, 2013
406
}
Oct 24, 2013
Oct 24, 2013
407
}
Oct 24, 2013
Oct 24, 2013
408
409
410
} else {
// Process the DTMF section for a match
processDtmf = true;
Oct 24, 2013
Oct 24, 2013
411
412
413
414
}
// Have we got a match?
if ((matchLength >= QtContactsSqliteExtensions::DefaultMaximumPhoneNumberCharacters) ||
Oct 24, 2013
Oct 24, 2013
415
processDtmf) {
Oct 24, 2013
Oct 24, 2013
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// See if the match continues into the DTMF area
QString::const_iterator lit = ldtmf;
QString::const_iterator rit = rdtmf;
for ( ; (lit != lend) && (rit != rend); ++lit, ++rit) {
if ((*lit).toLower() != (*rit).toLower())
break;
++matchLength;
}
}
return matchLength;
}
int bestPhoneNumberMatchLength(const QContact &contact, const QString &match)
{
int bestMatchLength = 0;
foreach (const QContactPhoneNumber& phone, contact.details<QContactPhoneNumber>()) {
bestMatchLength = qMax(bestMatchLength, matchLength(SeasideCache::normalizePhoneNumber(phone.number()), match));
Oct 24, 2013
Oct 24, 2013
435
436
437
if (bestMatchLength == ExactMatch) {
return ExactMatch;
}
Oct 24, 2013
Oct 24, 2013
438
439
440
441
442
}
return bestMatchLength;
}
Aug 5, 2013
Aug 5, 2013
443
444
445
}
SeasideCache *SeasideCache::instancePtr = 0;
Jan 15, 2014
Jan 15, 2014
446
int SeasideCache::contactNameGroupCount = getContactNameGroupCount();
Sep 6, 2013
Sep 6, 2013
447
QStringList SeasideCache::allContactNameGroups = getAllContactNameGroups();
Aug 5, 2013
Aug 5, 2013
448
Oct 21, 2013
Oct 21, 2013
449
450
451
452
453
QContactManager* SeasideCache::manager()
{
return ::manager();
}
Jul 16, 2013
Jul 16, 2013
454
455
456
457
458
SeasideCache* SeasideCache::instance()
{
return instancePtr;
}
Jan 28, 2014
Jan 28, 2014
459
QContactId SeasideCache::apiId(const QContact &contact)
Jul 16, 2013
Jul 16, 2013
460
461
462
463
{
return contact.id();
}
Jan 28, 2014
Jan 28, 2014
464
QContactId SeasideCache::apiId(quint32 iid)
Jul 16, 2013
Jul 16, 2013
465
466
467
468
{
return QtContactsSqliteExtensions::apiContactId(iid);
}
Jan 28, 2014
Jan 28, 2014
469
bool SeasideCache::validId(const QContactId &id)
Jul 16, 2013
Jul 16, 2013
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{
return !id.isNull();
}
quint32 SeasideCache::internalId(const QContact &contact)
{
return internalId(contact.id());
}
quint32 SeasideCache::internalId(const QContactId &id)
{
return QtContactsSqliteExtensions::internalContactId(id);
}
SeasideCache::SeasideCache()
Oct 21, 2013
Oct 21, 2013
485
: m_syncFilter(FilterNone)
Jul 16, 2013
Jul 16, 2013
486
487
488
, m_populated(0)
, m_cacheIndex(0)
, m_queryIndex(0)
Sep 19, 2013
Sep 19, 2013
489
490
, m_fetchProcessedCount(0)
, m_fetchByIdProcessedCount(0)
Jul 16, 2013
Jul 16, 2013
491
, m_keepPopulated(false)
Aug 5, 2013
Aug 5, 2013
492
, m_populateProgress(Unpopulated)
Oct 15, 2014
Oct 15, 2014
493
, m_populating(0)
Aug 5, 2013
Aug 5, 2013
494
, m_fetchTypes(0)
Jan 9, 2014
Jan 9, 2014
495
496
, m_extraFetchTypes(0)
, m_dataTypesFetched(0)
Jul 16, 2013
Jul 16, 2013
497
498
499
, m_updatesPending(false)
, m_refreshRequired(false)
, m_contactsUpdated(false)
Dec 10, 2013
Dec 10, 2013
500
, m_displayOff(false)
Jul 16, 2013
Jul 16, 2013
501
502
503
504
505
{
Q_ASSERT(!instancePtr);
instancePtr = this;
m_timer.start();
Jul 26, 2013
Jul 26, 2013
506
m_fetchPostponed.invalidate();
Jul 16, 2013
Jul 16, 2013
507
May 27, 2014
May 27, 2014
508
509
510
511
512
CacheConfiguration *config(cacheConfig());
connect(config, SIGNAL(displayLabelOrderChanged(CacheConfiguration::DisplayLabelOrder)),
this, SLOT(displayLabelOrderChanged(CacheConfiguration::DisplayLabelOrder)));
connect(config, SIGNAL(sortPropertyChanged(QString)), this, SLOT(sortPropertyChanged(QString)));
connect(config, SIGNAL(groupPropertyChanged(QString)), this, SLOT(groupPropertyChanged(QString)));
Jul 16, 2013
Jul 16, 2013
513
Dec 10, 2013
Dec 10, 2013
514
515
516
517
518
if (!QDBusConnection::systemBus().connect(MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_DISPLAY_SIG, this, SLOT(displayStatusChanged(QString)))) {
qWarning() << "Unable to connect to MCE displayStatusChanged signal";
}
Oct 21, 2013
Oct 21, 2013
519
520
QContactManager *mgr(manager());
Dec 10, 2013
Dec 10, 2013
521
522
523
524
525
// The contactsPresenceChanged signal is not exported by QContactManager, so we
// need to find it from the manager's engine object
typedef QtContactsSqliteExtensions::ContactManagerEngine EngineType;
EngineType *cme = dynamic_cast<EngineType *>(QContactManagerData::managerData(mgr)->m_engine);
Oct 21, 2013
Oct 21, 2013
526
527
connect(mgr, SIGNAL(dataChanged()), this, SLOT(updateContacts()));
connect(mgr, SIGNAL(contactsAdded(QList<QContactId>)),
Jul 16, 2013
Jul 16, 2013
528
this, SLOT(contactsAdded(QList<QContactId>)));
Oct 21, 2013
Oct 21, 2013
529
connect(mgr, SIGNAL(contactsChanged(QList<QContactId>)),
Jul 16, 2013
Jul 16, 2013
530
this, SLOT(contactsChanged(QList<QContactId>)));
Dec 10, 2013
Dec 10, 2013
531
532
connect(cme, SIGNAL(contactsPresenceChanged(QList<QContactId>)),
this, SLOT(contactsPresenceChanged(QList<QContactId>)));
Oct 21, 2013
Oct 21, 2013
533
connect(mgr, SIGNAL(contactsRemoved(QList<QContactId>)),
Jul 16, 2013
Jul 16, 2013
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
this, SLOT(contactsRemoved(QList<QContactId>)));
connect(&m_fetchRequest, SIGNAL(resultsAvailable()), this, SLOT(contactsAvailable()));
connect(&m_fetchByIdRequest, SIGNAL(resultsAvailable()), this, SLOT(contactsAvailable()));
connect(&m_contactIdRequest, SIGNAL(resultsAvailable()), this, SLOT(contactIdsAvailable()));
connect(&m_relationshipsFetchRequest, SIGNAL(resultsAvailable()), this, SLOT(relationshipsAvailable()));
connect(&m_fetchRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_fetchByIdRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_contactIdRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_relationshipsFetchRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_removeRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_saveRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_relationshipSaveRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
connect(&m_relationshipRemoveRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(requestStateChanged(QContactAbstractRequest::State)));
Oct 21, 2013
Oct 21, 2013
558
559
560
561
562
563
564
565
m_fetchRequest.setManager(mgr);
m_fetchByIdRequest.setManager(mgr);
m_contactIdRequest.setManager(mgr);
m_relationshipsFetchRequest.setManager(mgr);
m_removeRequest.setManager(mgr);
m_saveRequest.setManager(mgr);
m_relationshipSaveRequest.setManager(mgr);
m_relationshipRemoveRequest.setManager(mgr);
Jul 16, 2013
Jul 16, 2013
566
May 27, 2014
May 27, 2014
567
setSortOrder(sortProperty());
Jul 16, 2013
Jul 16, 2013
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
}
SeasideCache::~SeasideCache()
{
if (instancePtr == this)
instancePtr = 0;
}
void SeasideCache::checkForExpiry()
{
if (instancePtr->m_users.isEmpty()) {
bool unused = true;
for (int i = 0; i < FilterTypesCount; ++i) {
unused &= instancePtr->m_models[i].isEmpty();
}
if (unused) {
instancePtr->m_expiryTimer.start(30000, instancePtr);
}
}
}
Jan 9, 2014
Jan 9, 2014
589
void SeasideCache::registerModel(ListModel *model, FilterType type, FetchDataType requiredTypes, FetchDataType extraTypes)
Jul 16, 2013
Jul 16, 2013
590
591
592
593
594
595
596
597
{
if (!instancePtr) {
new SeasideCache;
} else {
instancePtr->m_expiryTimer.stop();
for (int i = 0; i < FilterTypesCount; ++i)
instancePtr->m_models[i].removeAll(model);
}
Aug 5, 2013
Aug 5, 2013
598
Jul 16, 2013
Jul 16, 2013
599
instancePtr->m_models[type].append(model);
Jan 9, 2014
Jan 9, 2014
600
601
602
603
604
605
instancePtr->keepPopulated(requiredTypes & SeasideCache::FetchTypesMask, extraTypes & SeasideCache::FetchTypesMask);
if (requiredTypes & SeasideCache::FetchTypesMask) {
// If we have filtered models, they will need a contact ID refresh after the cache is populated
instancePtr->m_refreshRequired = true;
}
Jul 16, 2013
Jul 16, 2013
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
}
void SeasideCache::unregisterModel(ListModel *model)
{
for (int i = 0; i < FilterTypesCount; ++i)
instancePtr->m_models[i].removeAll(model);
checkForExpiry();
}
void SeasideCache::registerUser(QObject *user)
{
if (!instancePtr) {
new SeasideCache;
} else {
instancePtr->m_expiryTimer.stop();
}
instancePtr->m_users.insert(user);
}
void SeasideCache::unregisterUser(QObject *user)
{
instancePtr->m_users.remove(user);
checkForExpiry();
}
void SeasideCache::registerNameGroupChangeListener(SeasideNameGroupChangeListener *listener)
{
if (!instancePtr)
new SeasideCache;
instancePtr->m_nameGroupChangeListeners.append(listener);
}
void SeasideCache::unregisterNameGroupChangeListener(SeasideNameGroupChangeListener *listener)
{
if (!instancePtr)
return;
instancePtr->m_nameGroupChangeListeners.removeAll(listener);
}
Aug 6, 2013
Aug 6, 2013
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
void SeasideCache::registerChangeListener(ChangeListener *listener)
{
if (!instancePtr)
new SeasideCache;
instancePtr->m_changeListeners.append(listener);
}
void SeasideCache::unregisterChangeListener(ChangeListener *listener)
{
if (!instancePtr)
return;
instancePtr->m_changeListeners.removeAll(listener);
}
void SeasideCache::unregisterResolveListener(ResolveListener *listener)
{
if (!instancePtr)
return;
QList<ResolveData>::iterator it = instancePtr->m_resolveAddresses.begin();
Aug 30, 2013
Aug 30, 2013
667
while (it != instancePtr->m_resolveAddresses.end()) {
Aug 6, 2013
Aug 6, 2013
668
if (it->listener == listener) {
Oct 17, 2014
Oct 17, 2014
669
670
671
672
if (it->fetchRequest) {
it->fetchRequest->cancel();
delete it->fetchRequest;
}
Aug 6, 2013
Aug 6, 2013
673
674
675
676
677
it = instancePtr->m_resolveAddresses.erase(it);
} else {
++it;
}
}
Aug 30, 2013
Aug 30, 2013
678
679
680
681
682
683
684
685
686
it = instancePtr->m_unknownAddresses.begin();
while (it != instancePtr->m_unknownAddresses.end()) {
if (it->listener == listener) {
it = instancePtr->m_unknownAddresses.erase(it);
} else {
++it;
}
}
Aug 6, 2013
Aug 6, 2013
687
688
}
Aug 7, 2013
Aug 7, 2013
689
690
691
692
693
694
void SeasideCache::setNameGrouper(SeasideNameGrouper *grouper)
{
if (!instancePtr)
new SeasideCache;
instancePtr->m_nameGrouper.reset(grouper);
Oct 22, 2013
Oct 22, 2013
695
allContactNameGroups = instancePtr->m_nameGrouper->allNameGroups();
Jan 15, 2014
Jan 15, 2014
696
contactNameGroupCount = allContactNameGroups.count();
Oct 22, 2013
Oct 22, 2013
697
698
if (!allContactNameGroups.contains(QLatin1String("#")))
allContactNameGroups << QLatin1String("#");
Aug 7, 2013
Aug 7, 2013
699
700
}
Sep 6, 2013
Sep 6, 2013
701
QString SeasideCache::nameGroup(const CacheItem *cacheItem)
Jul 16, 2013
Jul 16, 2013
702
703
{
if (!cacheItem)
Sep 6, 2013
Sep 6, 2013
704
return QString();
Jul 16, 2013
Jul 16, 2013
705
Aug 16, 2013
Aug 16, 2013
706
707
708
return cacheItem->nameGroup;
}
Sep 6, 2013
Sep 6, 2013
709
QString SeasideCache::determineNameGroup(const CacheItem *cacheItem)
Aug 16, 2013
Aug 16, 2013
710
711
{
if (!cacheItem)
Sep 6, 2013
Sep 6, 2013
712
return QString();
Aug 7, 2013
Aug 7, 2013
713
714
if (!instancePtr->m_nameGrouper.isNull()) {
May 27, 2014
May 27, 2014
715
QString group = instancePtr->m_nameGrouper->nameGroupForContact(cacheItem->contact, groupProperty());
Oct 22, 2013
Oct 22, 2013
716
if (!group.isNull() && allContactNameGroups.contains(group)) {
Aug 7, 2013
Aug 7, 2013
717
return group;
Aug 16, 2013
Aug 16, 2013
718
}
Aug 7, 2013
Aug 7, 2013
719
720
}
Aug 29, 2013
Aug 29, 2013
721
const QContactName name(cacheItem->contact.detail<QContactName>());
May 27, 2014
May 27, 2014
722
const QString nameProperty(groupProperty() == QString::fromLatin1("firstName") ? name.firstName() : name.lastName());
Aug 29, 2013
Aug 29, 2013
723
Sep 6, 2013
Sep 6, 2013
724
QString group;
Aug 29, 2013
Aug 29, 2013
725
if (!nameProperty.isEmpty()) {
Sep 6, 2013
Sep 6, 2013
726
group = mLocale.indexBucket(nameProperty);
Nov 7, 2013
Nov 7, 2013
727
} else if (!cacheItem->displayLabel.isEmpty()) {
Sep 6, 2013
Sep 6, 2013
728
group = mLocale.indexBucket(cacheItem->displayLabel);
Jul 16, 2013
Jul 16, 2013
729
730
}
Jan 15, 2014
Jan 15, 2014
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
if (!group.isEmpty()) {
if (!allContactNameGroups.contains(group)) {
// If this group is some kind of digit, group under '#'
if (mLocale.toLatinNumbers(group.mid(0, 1)).at(0).isDigit()) {
group = QString();
}
}
}
if (group.isEmpty()) {
group = QString::fromLatin1("#");
} else if (!allContactNameGroups.contains(group)) {
// Insert before the '#' group, which is always last, and after the pre-defined groups
const int maxIndex = allContactNameGroups.count() - 1;
int index = qMin(contactNameGroupCount, maxIndex);
for ( ; index < maxIndex; ++index) {
if (group < allContactNameGroups.at(index)) {
break;
}
}
allContactNameGroups.insert(index, group);
Jul 16, 2013
Jul 16, 2013
753
}
Jan 15, 2014
Jan 15, 2014
754
Jul 16, 2013
Jul 16, 2013
755
756
757
return group;
}
Sep 6, 2013
Sep 6, 2013
758
QStringList SeasideCache::allNameGroups()
Jul 16, 2013
Jul 16, 2013
759
{
Sep 6, 2013
Sep 6, 2013
760
761
if (!instancePtr)
new SeasideCache;
Jul 16, 2013
Jul 16, 2013
762
763
764
return allContactNameGroups;
}
Sep 6, 2013
Sep 6, 2013
765
QHash<QString, QSet<quint32> > SeasideCache::nameGroupMembers()
Jul 16, 2013
Jul 16, 2013
766
767
768
{
if (instancePtr)
return instancePtr->m_contactNameGroups;
Sep 6, 2013
Sep 6, 2013
769
return QHash<QString, QSet<quint32> >();
Jul 16, 2013
Jul 16, 2013
770
771
772
773
}
SeasideCache::DisplayLabelOrder SeasideCache::displayLabelOrder()
{
May 27, 2014
May 27, 2014
774
return static_cast<DisplayLabelOrder>(cacheConfig()->displayLabelOrder());
Jul 16, 2013
Jul 16, 2013
775
776
}
Aug 29, 2013
Aug 29, 2013
777
778
QString SeasideCache::sortProperty()
{
May 27, 2014
May 27, 2014
779
return cacheConfig()->sortProperty();
Aug 29, 2013
Aug 29, 2013
780
781
}
Aug 30, 2013
Aug 30, 2013
782
783
QString SeasideCache::groupProperty()
{
May 27, 2014
May 27, 2014
784
return cacheConfig()->groupProperty();
Aug 30, 2013
Aug 30, 2013
785
786
}
Jul 16, 2013
Jul 16, 2013
787
788
789
790
791
792
int SeasideCache::contactId(const QContact &contact)
{
quint32 internal = internalId(contact);
return static_cast<int>(internal);
}
Jan 28, 2014
Jan 28, 2014
793
SeasideCache::CacheItem *SeasideCache::itemById(const QContactId &id, bool requireComplete)
Jul 16, 2013
Jul 16, 2013
794
795
796
797
798
799
800
801
802
803
804
805
806
807
{
if (!validId(id))
return 0;
quint32 iid = internalId(id);
CacheItem *item = 0;
QHash<quint32, CacheItem>::iterator it = instancePtr->m_people.find(iid);
if (it != instancePtr->m_people.end()) {
item = &(*it);
} else {
// Insert a new item into the cache if the one doesn't exist.
item = &(instancePtr->m_people[iid]);
Oct 2, 2013
Oct 2, 2013
808
809
810
item->iid = iid;
item->contactState = ContactAbsent;
Jul 16, 2013
Jul 16, 2013
811
812
813
item->contact.setId(id);
}
Aug 5, 2013
Aug 5, 2013
814
815
if (requireComplete) {
ensureCompletion(item);
Jul 16, 2013
Jul 16, 2013
816
817
818
819
}
return item;
}
Aug 5, 2013
Aug 5, 2013
820
SeasideCache::CacheItem *SeasideCache::itemById(int id, bool requireComplete)
Jul 16, 2013
Jul 16, 2013
821
822
823
824
{
if (id != 0) {
QContactId contactId(apiId(static_cast<quint32>(id)));
if (!contactId.isNull()) {
Aug 5, 2013
Aug 5, 2013
825
return itemById(contactId, requireComplete);
Jul 16, 2013
Jul 16, 2013
826
827
828
829
830
831
}
}
return 0;
}
Jan 28, 2014
Jan 28, 2014
832
SeasideCache::CacheItem *SeasideCache::existingItem(const QContactId &id)
Jul 16, 2013
Jul 16, 2013
833
{
Jul 29, 2013
Jul 29, 2013
834
835
return existingItem(internalId(id));
}
Jul 16, 2013
Jul 16, 2013
836
Jul 29, 2013
Jul 29, 2013
837
838
SeasideCache::CacheItem *SeasideCache::existingItem(quint32 iid)
{
Jul 16, 2013
Jul 16, 2013
839
840
841
842
843
844
QHash<quint32, CacheItem>::iterator it = instancePtr->m_people.find(iid);
return it != instancePtr->m_people.end()
? &(*it)
: 0;
}
Jan 28, 2014
Jan 28, 2014
845
QContact SeasideCache::contactById(const QContactId &id)
Jul 16, 2013
Jul 16, 2013
846
847
848
849
850
{
quint32 iid = internalId(id);
return instancePtr->m_people.value(iid, CacheItem()).contact;
}
Aug 5, 2013
Aug 5, 2013
851
void SeasideCache::ensureCompletion(CacheItem *cacheItem)
Jul 16, 2013
Jul 16, 2013
852
{
Aug 5, 2013
Aug 5, 2013
853
if (cacheItem->contactState < ContactRequested) {
Aug 19, 2013
Aug 19, 2013
854
refreshContact(cacheItem);
Aug 5, 2013
Aug 5, 2013
855
856
857
}
}
Aug 19, 2013
Aug 19, 2013
858
859
860
861
862
863
864
void SeasideCache::refreshContact(CacheItem *cacheItem)
{
cacheItem->contactState = ContactRequested;
instancePtr->m_changedContacts.append(cacheItem->apiId());
instancePtr->fetchContacts();
}
Aug 5, 2013
Aug 5, 2013
865
866
SeasideCache::CacheItem *SeasideCache::itemByPhoneNumber(const QString &number, bool requireComplete)
{
Oct 24, 2013
Oct 24, 2013
867
868
869
const QString normalized(normalizePhoneNumber(number));
if (normalized.isEmpty())
return 0;
Oct 24, 2013
Oct 24, 2013
870
Oct 24, 2013
Oct 24, 2013
871
872
873
874
875
const QChar plus(QChar::fromLatin1('+'));
if (normalized.startsWith(plus)) {
// See if there is a match for the complete form of this number
if (CacheItem *item = instancePtr->itemMatchingPhoneNumber(normalized, normalized, requireComplete)) {
return item;
Oct 24, 2013
Oct 24, 2013
876
877
}
}
Aug 5, 2013
Aug 5, 2013
878
Oct 24, 2013
Oct 24, 2013
879
880
881
882
883
884
885
886
887
const QString minimized(minimizePhoneNumber(normalized));
if (((instancePtr->m_fetchTypes & SeasideCache::FetchPhoneNumber) == 0) &&
!instancePtr->m_resolvedPhoneNumbers.contains(minimized)) {
// We haven't previously queried this number, so there may be more matches than any
// that we already have cached; return 0 to force a query
return 0;
}
return instancePtr->itemMatchingPhoneNumber(minimized, normalized, requireComplete);
Jul 16, 2013
Jul 16, 2013
888
889
}
Aug 5, 2013
Aug 5, 2013
890
SeasideCache::CacheItem *SeasideCache::itemByEmailAddress(const QString &email, bool requireComplete)
Jul 16, 2013
Jul 16, 2013
891
{
Apr 10, 2014
Apr 10, 2014
892
893
894
if (email.trimmed().isEmpty())
return 0;
Jul 16, 2013
Jul 16, 2013
895
896
QHash<QString, quint32>::const_iterator it = instancePtr->m_emailAddressIds.find(email.toLower());
if (it != instancePtr->m_emailAddressIds.end())
Aug 5, 2013
Aug 5, 2013
897
898
return itemById(*it, requireComplete);
Jul 16, 2013
Jul 16, 2013
899
900
901
return 0;
}
Aug 5, 2013
Aug 5, 2013
902
903
SeasideCache::CacheItem *SeasideCache::itemByOnlineAccount(const QString &localUid, const QString &remoteUid, bool requireComplete)
{
Apr 10, 2014
Apr 10, 2014
904
905
906
if (localUid.trimmed().isEmpty() || remoteUid.trimmed().isEmpty())
return 0;
Aug 5, 2013
Aug 5, 2013
907
908
909
910
911
912
913
914
915
916
917
918
919
920
QPair<QString, QString> address = qMakePair(localUid, remoteUid.toLower());
QHash<QPair<QString, QString>, quint32>::const_iterator it = instancePtr->m_onlineAccountIds.find(address);
if (it != instancePtr->m_onlineAccountIds.end())
return itemById(*it, requireComplete);
return 0;
}
SeasideCache::CacheItem *SeasideCache::resolvePhoneNumber(ResolveListener *listener, const QString &number, bool requireComplete)
{
CacheItem *item = itemByPhoneNumber(number, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, QString(), number, requireComplete);
Aug 6, 2013
Aug 6, 2013
921
922
} else if (requireComplete) {
ensureCompletion(item);
Aug 5, 2013
Aug 5, 2013
923
924
925
926
927
928
929
930
931
932
}
return item;
}
SeasideCache::CacheItem *SeasideCache::resolveEmailAddress(ResolveListener *listener, const QString &address, bool requireComplete)
{
CacheItem *item = itemByEmailAddress(address, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, address, QString(), requireComplete);
Aug 6, 2013
Aug 6, 2013
933
934
} else if (requireComplete) {
ensureCompletion(item);
Aug 5, 2013
Aug 5, 2013
935
936
937
938
939
940
941
942
943
}
return item;
}
SeasideCache::CacheItem *SeasideCache::resolveOnlineAccount(ResolveListener *listener, const QString &localUid, const QString &remoteUid, bool requireComplete)
{
CacheItem *item = itemByOnlineAccount(localUid, remoteUid, requireComplete);
if (!item) {
instancePtr->resolveAddress(listener, localUid, remoteUid, requireComplete);
Aug 6, 2013
Aug 6, 2013
944
945
} else if (requireComplete) {
ensureCompletion(item);
Aug 5, 2013
Aug 5, 2013
946
947
948
949
}
return item;
}
Jan 28, 2014
Jan 28, 2014
950
QContactId SeasideCache::selfContactId()
Jul 16, 2013
Jul 16, 2013
951
{
Oct 21, 2013
Oct 21, 2013
952
return manager()->selfContactId();
Jul 16, 2013
Jul 16, 2013
953
954
955
956
}
void SeasideCache::requestUpdate()
{
Nov 5, 2013
Nov 5, 2013
957
if (!m_updatesPending) {
Jul 16, 2013
Jul 16, 2013
958
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
Nov 5, 2013
Nov 5, 2013
959
960
m_updatesPending = true;
}
Jul 16, 2013
Jul 16, 2013
961
962
963
964
}
bool SeasideCache::saveContact(const QContact &contact)
{
Jan 28, 2014
Jan 28, 2014
965
QContactId id = apiId(contact);
Jul 16, 2013
Jul 16, 2013
966
967
if (validId(id)) {
instancePtr->m_contactsToSave[id] = contact;
Sep 20, 2013
Sep 20, 2013
968
instancePtr->contactDataChanged(internalId(id));
Jul 16, 2013
Jul 16, 2013
969
970
971
972
973
974
975
976
977
} else {
instancePtr->m_contactsToCreate.append(contact);
}
instancePtr->requestUpdate();
return true;
}
Sep 20, 2013
Sep 20, 2013
978
void SeasideCache::contactDataChanged(quint32 iid)
Jul 16, 2013
Jul 16, 2013
979
{
Sep 20, 2013
Sep 20, 2013
980
981
982
instancePtr->contactDataChanged(iid, FilterFavorites);
instancePtr->contactDataChanged(iid, FilterOnline);
instancePtr->contactDataChanged(iid, FilterAll);
Aug 5, 2013
Aug 5, 2013
983
}
Jul 16, 2013
Jul 16, 2013
984
Sep 20, 2013
Sep 20, 2013
985
void SeasideCache::contactDataChanged(quint32 iid, FilterType filter)
Aug 5, 2013
Aug 5, 2013
986
{
Sep 20, 2013
Sep 20, 2013
987
int row = contactIndex(iid, filter);
Aug 5, 2013
Aug 5, 2013
988
989
990
991
992
993
if (row != -1) {
QList<ListModel *> &models = m_models[filter];
for (int i = 0; i < models.count(); ++i) {
models.at(i)->sourceDataChanged(row, row);
}
}
Jul 16, 2013
Jul 16, 2013
994
995
}
Jul 19, 2013
Jul 19, 2013
996
bool SeasideCache::removeContact(const QContact &contact)
Jul 16, 2013
Jul 16, 2013
997
{
Jan 28, 2014
Jan 28, 2014
998
QContactId id = apiId(contact);
Jul 29, 2013
Jul 29, 2013
999
1000
if (!validId(id))
return false;