This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
/
seasidecache.h
525 lines (429 loc) · 18.6 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
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."
*/
#ifndef SEASIDECACHE_H
#define SEASIDECACHE_H
#include "contactcacheexport.h"
36
#include "seasidenamegrouper.h"
37
38
#include <qtcontacts-extensions.h>
39
#include <QContactStatusFlags>
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
#include <QContact>
#include <QContactManager>
#include <QContactFetchRequest>
#include <QContactFetchByIdRequest>
#include <QContactRemoveRequest>
#include <QContactSaveRequest>
#include <QContactRelationshipFetchRequest>
#include <QContactRelationshipSaveRequest>
#include <QContactRelationshipRemoveRequest>
#ifdef USING_QTPIM
#include <QContactIdFilter>
#include <QContactIdFetchRequest>
#else
#include <QContactLocalIdFilter>
#include <QContactLocalIdFetchRequest>
#endif
#include <QBasicTimer>
#include <QSet>
#include <QElapsedTimer>
#include <QAbstractListModel>
#ifdef HAS_MLITE
#include <mgconfitem.h>
#endif
68
#ifdef USING_QTPIM
69
70
QTCONTACTS_USE_NAMESPACE
71
72
typedef QContactDetail::DetailType DetailTypeId;
#else
73
74
QTM_USE_NAMESPACE
75
typedef QString DetailTypeId;
76
77
#endif
78
79
80
81
82
83
class CONTACTCACHE_EXPORT SeasideNameGroupChangeListener
{
public:
SeasideNameGroupChangeListener() {}
~SeasideNameGroupChangeListener() {}
84
virtual void nameGroupsUpdated(const QHash<QString, QSet<quint32> > &groups) = 0;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
};
class CONTACTCACHE_EXPORT SeasideCache : public QObject
{
Q_OBJECT
public:
typedef QtContactsSqliteExtensions::ApiContactIdType ContactIdType;
enum FilterType {
FilterNone,
FilterAll,
FilterFavorites,
FilterOnline,
FilterTypesCount
};
101
102
103
104
105
106
107
enum FetchDataType {
FetchNone = 0,
FetchAccountUri = (1 << 0),
FetchPhoneNumber = (1 << 1),
FetchEmailAddress = (1 << 2)
};
108
enum DisplayLabelOrder {
109
110
FirstNameFirst = 0,
LastNameFirst
111
112
113
114
};
enum ContactState {
ContactAbsent,
115
ContactPartial,
116
ContactRequested,
117
ContactComplete
118
119
};
120
121
122
123
124
enum {
// Must be after the highest bit used in QContactStatusFlags::Flag
HasValidOnlineAccount = (QContactStatusFlags::IsOnline << 1)
};
125
126
127
128
129
130
struct ItemData
{
virtual ~ItemData() {}
virtual void displayLabelOrderChanged(DisplayLabelOrder order) = 0;
131
virtual void updateContact(const QContact &newContact, QContact *oldContact, ContactState state) = 0;
132
133
134
135
136
137
138
139
virtual void constituentsFetched(const QList<int> &ids) = 0;
virtual void mergeCandidatesFetched(const QList<int> &ids) = 0;
virtual void aggregationOperationCompleted() = 0;
virtual QList<int> constituents() const = 0;
};
140
141
struct CacheItem;
struct ItemListener
142
{
143
144
ItemListener() : next(0), key(0) {}
virtual ~ItemListener() {}
145
146
147
148
149
150
virtual void itemUpdated(CacheItem *item) = 0;
virtual void itemAboutToBeRemoved(CacheItem *item) = 0;
ItemListener *next;
void *key;
151
152
153
154
};
struct CacheItem
{
155
CacheItem() : itemData(0), iid(0), contactState(ContactAbsent), listeners(0) {}
156
CacheItem(const QContact &contact)
157
158
: contact(contact), itemData(0), iid(internalId(contact)),
statusFlags(contact.detail<QContactStatusFlags>().flagsValue()), contactState(ContactAbsent), listeners(0) {}
159
160
161
ContactIdType apiId() const { return SeasideCache::apiId(contact); }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
ItemListener *appendListener(ItemListener *listener, void *key)
{
if (listeners) {
ItemListener *existing(listeners);
while (existing->next) {
existing = existing->next;
}
existing->next = listener;
} else {
listeners = listener;
}
listener->next = 0;
listener->key = key;
return listener;
}
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
bool removeListener(ItemListener *listener)
{
if (listeners) {
ItemListener *existing(listeners);
ItemListener **previous = &listeners;
while (existing) {
if (existing == listener) {
*previous = listener->next;
return true;
}
previous = &existing->next;
existing = existing->next;
}
}
return false;
}
198
199
ItemListener *listener(void *key)
{
200
201
202
ItemListener *existing(listeners);
while (existing && (existing->key != key) && (existing->next)) {
existing = existing->next;
203
}
204
return (existing && (existing->key == key)) ? existing : 0;
205
206
}
207
208
QContact contact;
ItemData *itemData;
209
quint32 iid;
210
quint64 statusFlags;
211
ContactState contactState;
212
ItemListener *listeners;
213
QString nameGroup;
214
QString displayLabel;
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
};
struct ContactLinkRequest
{
ContactLinkRequest(const SeasideCache::ContactIdType &id) : contactId(id), constituentsFetched(false) {}
ContactLinkRequest(const ContactLinkRequest &req) : contactId(req.contactId), constituentsFetched(req.constituentsFetched) {}
SeasideCache::ContactIdType contactId;
bool constituentsFetched;
};
class ListModel : public QAbstractListModel
{
public:
ListModel(QObject *parent = 0) : QAbstractListModel(parent) {}
virtual ~ListModel() {}
virtual void sourceAboutToRemoveItems(int begin, int end) = 0;
virtual void sourceItemsRemoved() = 0;
virtual void sourceAboutToInsertItems(int begin, int end) = 0;
virtual void sourceItemsInserted(int begin, int end) = 0;
virtual void sourceDataChanged(int begin, int end) = 0;
240
241
virtual void sourceItemsChanged() = 0;
242
243
virtual void makePopulated() = 0;
virtual void updateDisplayLabelOrder() = 0;
244
virtual void updateSortProperty() = 0;
245
virtual void updateGroupProperty() = 0;
246
247
};
248
249
250
251
struct ResolveListener
{
virtual ~ResolveListener() {}
252
253
254
255
256
257
258
259
260
virtual void addressResolved(const QString &first, const QString &second, CacheItem *item) = 0;
};
struct ChangeListener
{
virtual ~ChangeListener() {}
virtual void itemUpdated(CacheItem *item) = 0;
virtual void itemAboutToBeRemoved(CacheItem *item) = 0;
261
262
};
263
static SeasideCache *instance();
264
static QContactManager *manager();
265
266
267
268
269
static ContactIdType apiId(const QContact &contact);
static ContactIdType apiId(quint32 iid);
static bool validId(const ContactIdType &id);
270
271
272
#ifndef USING_QTPIM
static bool validId(const QContactId &id);
#endif
273
274
275
276
277
278
279
static quint32 internalId(const QContact &contact);
static quint32 internalId(const QContactId &id);
#ifndef USING_QTPIM
static quint32 internalId(QContactLocalId id);
#endif
280
static void registerModel(ListModel *model, FilterType type, FetchDataType extraData = FetchNone);
281
282
283
284
285
286
287
288
static void unregisterModel(ListModel *model);
static void registerUser(QObject *user);
static void unregisterUser(QObject *user);
static void registerNameGroupChangeListener(SeasideNameGroupChangeListener *listener);
static void unregisterNameGroupChangeListener(SeasideNameGroupChangeListener *listener);
289
290
291
292
293
static void registerChangeListener(ChangeListener *listener);
static void unregisterChangeListener(ChangeListener *listener);
static void unregisterResolveListener(ResolveListener *listener);
294
295
static void setNameGrouper(SeasideNameGrouper *grouper);
296
static DisplayLabelOrder displayLabelOrder();
297
static QString sortProperty();
298
static QString groupProperty();
299
300
301
302
static int contactId(const QContact &contact);
static CacheItem *existingItem(const ContactIdType &id);
303
#ifdef USING_QTPIM
304
static CacheItem *existingItem(quint32 iid);
305
#endif
306
static CacheItem *itemById(const ContactIdType &id, bool requireComplete = true);
307
#ifdef USING_QTPIM
308
static CacheItem *itemById(int id, bool requireComplete = true);
309
310
311
#endif
static ContactIdType selfContactId();
static QContact contactById(const ContactIdType &id);
312
313
static void ensureCompletion(CacheItem *cacheItem);
314
static void refreshContact(CacheItem *cacheItem);
315
316
317
static QString nameGroup(const CacheItem *cacheItem);
static QString determineNameGroup(const CacheItem *cacheItem);
318
319
320
static QStringList allNameGroups();
static QHash<QString, QSet<quint32> > nameGroupMembers();
321
322
323
324
325
326
327
328
329
static CacheItem *itemByPhoneNumber(const QString &number, bool requireComplete = true);
static CacheItem *itemByEmailAddress(const QString &address, bool requireComplete = true);
static CacheItem *itemByOnlineAccount(const QString &localUid, const QString &remoteUid, bool requireComplete = true);
static CacheItem *resolvePhoneNumber(ResolveListener *listener, const QString &number, bool requireComplete = true);
static CacheItem *resolveEmailAddress(ResolveListener *listener, const QString &address, bool requireComplete = true);
static CacheItem *resolveOnlineAccount(ResolveListener *listener, const QString &localUid, const QString &remoteUid, bool requireComplete = true);
330
static bool saveContact(const QContact &contact);
331
static bool removeContact(const QContact &contact);
332
333
334
335
static void aggregateContacts(const QContact &contact1, const QContact &contact2);
static void disaggregateContacts(const QContact &contact1, const QContact &contact2);
336
337
static bool fetchConstituents(const QContact &contact);
static bool fetchMergeCandidates(const QContact &contact);
338
339
340
341
static int importContacts(const QString &path);
static QString exportContacts();
342
static const QList<quint32> *contacts(FilterType filterType);
343
344
345
346
static bool isPopulated(FilterType filterType);
static QString generateDisplayLabel(const QContact &contact, DisplayLabelOrder order = FirstNameFirst);
static QString generateDisplayLabelFromNonNameDetails(const QContact &contact);
347
static QUrl filteredAvatarUrl(const QContact &contact, const QStringList &metadataFragments = QStringList());
348
349
static QString normalizePhoneNumber(const QString &input);
350
static QString minimizePhoneNumber(const QString &input);
351
352
353
354
bool event(QEvent *event);
// For synchronizeLists()
355
int insertRange(int index, int count, const QList<quint32> &source, int sourceIndex) { return insertRange(m_syncFilter, index, count, source, sourceIndex); }
356
int removeRange(int index, int count) { removeRange(m_syncFilter, index, count); return 0; }
357
358
359
protected:
void timerEvent(QTimerEvent *event);
360
void setSortOrder(const QString &property);
361
362
363
364
365
366
367
368
369
370
private slots:
void contactsAvailable();
void contactIdsAvailable();
void relationshipsAvailable();
void requestStateChanged(QContactAbstractRequest::State state);
void updateContacts();
#ifdef USING_QTPIM
void contactsAdded(const QList<QContactId> &contactIds);
void contactsChanged(const QList<QContactId> &contactIds);
371
void contactsPresenceChanged(const QList<QContactId> &contactIds);
372
373
374
375
void contactsRemoved(const QList<QContactId> &contactIds);
#else
void contactsAdded(const QList<QContactLocalId> &contactIds);
void contactsChanged(const QList<QContactLocalId> &contactIds);
376
void contactsPresenceChanged(const QList<QContactLocalId> &contactIds);
377
378
379
void contactsRemoved(const QList<QContactLocalId> &contactIds);
#endif
void displayLabelOrderChanged();
380
void sortPropertyChanged();
381
void groupPropertyChanged();
382
void displayStatusChanged(const QString &);
383
384
private:
385
386
387
388
389
390
391
392
393
394
enum PopulateProgress {
Unpopulated,
FetchFavorites,
FetchMetadata,
FetchOnline,
Populated,
RefetchFavorites,
RefetchOthers
};
395
396
397
398
399
SeasideCache();
~SeasideCache();
static void checkForExpiry();
400
void keepPopulated(quint32 fetchTypes);
401
402
void requestUpdate();
403
void appendContacts(const QList<QContact> &contacts, FilterType filterType, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);
404
void fetchContacts();
405
void updateContacts(const QList<ContactIdType> &contactIds, QList<ContactIdType> *updateList);
406
407
void applyPendingContactUpdates();
void applyContactUpdates(const QList<QContact> &contacts, bool partialFetch, const QSet<DetailTypeId> &queryDetailTypes);
408
409
410
void resolveUnknownAddresses(const QString &first, const QString &second, CacheItem *item);
bool updateContactIndexing(const QContact &oldContact, const QContact &contact, quint32 iid, const QSet<DetailTypeId> &queryDetailTypes, CacheItem *item);
411
void updateCache(CacheItem *item, const QContact &contact, bool partialFetch, bool initialInsert);
412
void reportItemUpdated(CacheItem *item);
413
414
void removeRange(FilterType filter, int index, int count);
415
int insertRange(FilterType filter, int index, int count, const QList<quint32> &queryIds, int queryIndex);
416
417
418
419
void contactDataChanged(quint32 iid);
void contactDataChanged(quint32 iid, FilterType filter);
void removeContactData(quint32 iid, FilterType filter);
420
421
void makePopulated(FilterType filter);
422
423
424
void addToContactNameGroup(quint32 iid, const QString &group, QSet<QString> *modifiedGroups = 0);
void removeFromContactNameGroup(quint32 iid, const QString &group, QSet<QString> *modifiedGroups = 0);
void notifyNameGroupsChanged(const QSet<QString> &groups);
425
426
427
428
void updateConstituentAggregations(const ContactIdType &contactId);
void completeContactAggregation(const ContactIdType &contact1Id, const ContactIdType &contact2Id);
429
430
void resolveAddress(ResolveListener *listener, const QString &first, const QString &second, bool requireComplete);
431
432
CacheItem *itemMatchingPhoneNumber(const QString &number, const QString &normalized, bool requireComplete);
433
434
int contactIndex(quint32 iid, FilterType filter);
435
436
static QContactRelationship makeRelationship(const QString &type, const QContact &contact1, const QContact &contact2);
437
438
QList<quint32> m_contacts[FilterTypesCount];
439
440
441
QBasicTimer m_expiryTimer;
QBasicTimer m_fetchTimer;
QHash<quint32, CacheItem> m_people;
442
QMultiHash<QString, quint32> m_phoneNumberIds;
443
QHash<QString, quint32> m_emailAddressIds;
444
QHash<QPair<QString, QString>, quint32> m_onlineAccountIds;
445
QHash<ContactIdType, QContact> m_contactsToSave;
446
QHash<QString, QSet<quint32> > m_contactNameGroups;
447
QList<QContact> m_contactsToCreate;
448
QHash<FilterType, QPair<QSet<DetailTypeId>, QList<QContact> > > m_contactsToAppend;
449
QList<QPair<QSet<DetailTypeId>, QList<QContact> > > m_contactsToUpdate;
450
451
QList<ContactIdType> m_contactsToRemove;
QList<ContactIdType> m_changedContacts;
452
QList<ContactIdType> m_presenceChangedContacts;
453
QSet<ContactIdType> m_aggregatedContacts;
454
455
QList<QContactId> m_contactsToFetchConstituents;
QList<QContactId> m_contactsToFetchCandidates;
456
QList<QContactId> m_contactsToLinkTo;
457
458
459
QList<QPair<ContactLinkRequest, ContactLinkRequest> > m_contactPairsToLink;
QList<QContactRelationship> m_relationshipsToSave;
QList<QContactRelationship> m_relationshipsToRemove;
460
QScopedPointer<SeasideNameGrouper> m_nameGrouper;
461
QList<SeasideNameGroupChangeListener*> m_nameGroupChangeListeners;
462
QList<ChangeListener*> m_changeListeners;
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
QList<ListModel *> m_models[FilterTypesCount];
QSet<QObject *> m_users;
QHash<ContactIdType,int> m_expiredContacts;
QContactFetchRequest m_fetchRequest;
QContactFetchByIdRequest m_fetchByIdRequest;
#ifdef USING_QTPIM
QContactIdFetchRequest m_contactIdRequest;
#else
QContactLocalIdFetchRequest m_contactIdRequest;
#endif
QContactRelationshipFetchRequest m_relationshipsFetchRequest;
QContactRemoveRequest m_removeRequest;
QContactSaveRequest m_saveRequest;
QContactRelationshipSaveRequest m_relationshipSaveRequest;
QContactRelationshipRemoveRequest m_relationshipRemoveRequest;
478
479
QList<QContactSortOrder> m_sortOrder;
QList<QContactSortOrder> m_onlineSortOrder;
480
FilterType m_syncFilter;
481
482
#ifdef HAS_MLITE
MGConfItem m_displayLabelOrderConf;
483
MGConfItem m_sortPropertyConf;
484
MGConfItem m_groupPropertyConf;
485
486
487
488
#endif
int m_populated;
int m_cacheIndex;
int m_queryIndex;
489
490
int m_fetchProcessedCount;
int m_fetchByIdProcessedCount;
491
DisplayLabelOrder m_displayLabelOrder;
492
QString m_sortProperty;
493
QString m_groupProperty;
494
bool m_keepPopulated;
495
496
497
PopulateProgress m_populateProgress;
quint32 m_fetchTypes;
bool m_fetchTypesChanged;
498
499
500
bool m_updatesPending;
bool m_refreshRequired;
bool m_contactsUpdated;
501
bool m_displayOff;
502
503
QSet<ContactIdType> m_constituentIds;
QSet<ContactIdType> m_candidateIds;
504
505
506
507
struct ResolveData {
QString first;
QString second;
508
QString compare;
509
510
511
512
bool requireComplete;
ResolveListener *listener;
};
QList<ResolveData> m_resolveAddresses;
513
QList<ResolveData> m_unknownResolveAddresses;
514
QList<ResolveData> m_unknownAddresses;
515
const ResolveData *m_activeResolve;
516
QSet<QString> m_resolvedPhoneNumbers;
517
518
519
520
521
QElapsedTimer m_timer;
QElapsedTimer m_fetchPostponed;
static SeasideCache *instancePtr;
522
static QStringList allContactNameGroups;
523
524
525
};
#endif