This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
/
seasidecache.h
355 lines (291 loc) · 12.2 KB
/
seasidecache.h
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* 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"
#include <qtcontacts-extensions.h>
#include <QContactStatusFlags>
#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 <QVector>
#include <QElapsedTimer>
#include <QAbstractListModel>
#ifdef HAS_MLITE
#include <mgconfitem.h>
#endif
USE_CONTACTS_NAMESPACE
class CONTACTCACHE_EXPORT SeasideNameGroupChangeListener
{
public:
SeasideNameGroupChangeListener() {}
~SeasideNameGroupChangeListener() {}
virtual void nameGroupsUpdated(const QHash<QChar, QSet<quint32> > &groups) = 0;
};
class CONTACTCACHE_EXPORT SeasideCache : public QObject
{
Q_OBJECT
public:
typedef QtContactsSqliteExtensions::ApiContactIdType ContactIdType;
enum FilterType {
FilterNone,
FilterAll,
FilterFavorites,
FilterOnline,
FilterTypesCount
};
enum DisplayLabelOrder {
FirstNameFirst,
LastNameFirst
};
enum ContactState {
ContactAbsent,
ContactRequested,
ContactFetched
};
struct ItemData
{
virtual ~ItemData() {}
virtual QString getDisplayLabel() const = 0;
virtual void displayLabelOrderChanged(DisplayLabelOrder order) = 0;
virtual void updateContact(const QContact &newContact, QContact *oldContact) = 0;
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;
};
struct ModelData
{
virtual ~ModelData() {}
virtual void contactChanged(const QContact &newContact) = 0;
};
struct CacheItem
{
CacheItem() : itemData(0), modelData(0), iid(0), contactState(ContactAbsent) {}
CacheItem(const QContact &contact)
: contact(contact), itemData(0), modelData(0), iid(internalId(contact)),
statusFlags(contact.detail<QContactStatusFlags>().flagsValue()), contactState(ContactAbsent) {}
ContactIdType apiId() const { return SeasideCache::apiId(contact); }
QContact contact;
ItemData *itemData;
ModelData *modelData;
quint32 iid;
quint64 statusFlags;
ContactState contactState;
};
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;
virtual void makePopulated() = 0;
virtual void updateDisplayLabelOrder() = 0;
};
static SeasideCache *instance();
static ContactIdType apiId(const QContact &contact);
static ContactIdType apiId(quint32 iid);
static bool validId(const ContactIdType &id);
#ifndef USING_QTPIM
static bool validId(const QContactId &id);
#endif
static quint32 internalId(const QContact &contact);
static quint32 internalId(const QContactId &id);
#ifndef USING_QTPIM
static quint32 internalId(QContactLocalId id);
#endif
static void registerModel(ListModel *model, FilterType type);
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);
static DisplayLabelOrder displayLabelOrder();
static int contactId(const QContact &contact);
static CacheItem *existingItem(const ContactIdType &id);
#ifdef USING_QTPIM
static CacheItem *existingItem(quint32 iid);
#endif
static CacheItem *itemById(const ContactIdType &id);
#ifdef USING_QTPIM
static CacheItem *itemById(int id);
#endif
static ContactIdType selfContactId();
static QContact contactById(const ContactIdType &id);
static QChar nameGroupForCacheItem(CacheItem *cacheItem);
static QList<QChar> allNameGroups();
static QHash<QChar, QSet<quint32> > nameGroupMembers();
static CacheItem *itemByPhoneNumber(const QString &msisdn);
static CacheItem *itemByEmailAddress(const QString &email);
static bool saveContact(const QContact &contact);
static bool removeContact(const QContact &contact);
static void aggregateContacts(const QContact &contact1, const QContact &contact2);
static void disaggregateContacts(const QContact &contact1, const QContact &contact2);
static bool fetchConstituents(const QContact &contact);
static bool fetchMergeCandidates(const QContact &contact);
static int importContacts(const QString &path);
static QString exportContacts();
static const QVector<ContactIdType> *contacts(FilterType filterType);
static bool isPopulated(FilterType filterType);
static QString generateDisplayLabel(const QContact &contact, DisplayLabelOrder order = FirstNameFirst);
static QString generateDisplayLabelFromNonNameDetails(const QContact &contact);
bool event(QEvent *event);
// For synchronizeLists()
int insertRange(int index, int count, const QList<ContactIdType> &source, int sourceIndex) {
return insertRange(m_fetchFilter, index, count, source, sourceIndex); }
int removeRange(int index, int count) { removeRange(m_fetchFilter, index, count); return 0; }
protected:
void timerEvent(QTimerEvent *event);
void setSortOrder(DisplayLabelOrder order);
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);
void contactsRemoved(const QList<QContactId> &contactIds);
#else
void contactsAdded(const QList<QContactLocalId> &contactIds);
void contactsChanged(const QList<QContactLocalId> &contactIds);
void contactsRemoved(const QList<QContactLocalId> &contactIds);
#endif
void displayLabelOrderChanged();
private:
SeasideCache();
~SeasideCache();
static void checkForExpiry();
void keepPopulated();
void requestUpdate();
void appendContacts(const QList<QContact> &contacts);
void fetchContacts();
void updateContacts(const QList<ContactIdType> &contactIds);
void finalizeUpdate(FilterType filter);
void removeRange(FilterType filter, int index, int count);
int insertRange(
FilterType filter,
int index,
int count,
const QList<ContactIdType> &queryIds,
int queryIndex);
void updateContactData(const ContactIdType &contactId, FilterType filter);
void removeContactData(const ContactIdType &contactId, FilterType filter);
void makePopulated(FilterType filter);
void addToContactNameGroup(quint32 iid, const QChar &group, QList<QChar> *modifiedGroups = 0);
void removeFromContactNameGroup(quint32 iid, const QChar &group, QList<QChar> *modifiedGroups = 0);
void notifyNameGroupsChanged(const QList<QChar> &groups);
void updateConstituentAggregations(const ContactIdType &contactId);
void completeContactAggregation(const ContactIdType &contact1Id, const ContactIdType &contact2Id);
static QContactRelationship makeRelationship(const QString &type, const QContact &contact1, const QContact &contact2);
QBasicTimer m_expiryTimer;
QBasicTimer m_fetchTimer;
QHash<quint32, CacheItem> m_people;
QHash<QString, quint32> m_phoneNumberIds;
QHash<QString, quint32> m_emailAddressIds;
QHash<ContactIdType, QContact> m_contactsToSave;
QHash<QChar, QSet<quint32> > m_contactNameGroups;
QList<QContact> m_contactsToCreate;
QList<ContactIdType> m_contactsToRemove;
QList<ContactIdType> m_changedContacts;
QList<QContactId> m_contactsToFetchConstituents;
QList<QContactId> m_contactsToFetchCandidates;
QList<QPair<ContactLinkRequest, ContactLinkRequest> > m_contactPairsToLink;
QList<QContactRelationship> m_relationshipsToSave;
QList<QContactRelationship> m_relationshipsToRemove;
QList<SeasideNameGroupChangeListener*> m_nameGroupChangeListeners;
QVector<ContactIdType> m_contacts[FilterTypesCount];
QList<ListModel *> m_models[FilterTypesCount];
QSet<QObject *> m_users;
QHash<ContactIdType,int> m_expiredContacts;
QContactManager m_manager;
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;
#ifdef HAS_MLITE
MGConfItem m_displayLabelOrderConf;
#endif
int m_resultsRead;
int m_populated;
int m_cacheIndex;
int m_queryIndex;
int m_appendIndex;
FilterType m_fetchFilter;
DisplayLabelOrder m_displayLabelOrder;
bool m_keepPopulated;
bool m_updatesPending;
bool m_fetchActive;
bool m_refreshRequired;
bool m_contactsUpdated;
QList<ContactIdType> m_constituentIds;
QList<ContactIdType> m_candidateIds;
QElapsedTimer m_timer;
QElapsedTimer m_fetchPostponed;
static SeasideCache *instancePtr;
static QList<QChar> allContactNameGroups;
};
#endif