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