changes: Add benchmarking test, doing operations on 5000 contacts
[qtcontacts-tracker:astojiljs-contactsd.git] / plugins / telepathy / cdtpstorage.h
1 /***************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (people-users@projects.maemo.org)
6 **
7 ** This file is part of contactsd.
8 **
9 ** If you have questions regarding the use of this file, please contact
10 ** Nokia at people-users@projects.maemo.org.
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License version 2.1 as published by the Free Software Foundation
15 ** and appearing in the file LICENSE.LGPL included in the packaging
16 ** of this file.
17 **
18 ****************************************************************************/
19
20 #ifndef CDTPSTORAGE_H
21 #define CDTPSTORAGE_H
22
23 #include "cdtpaccount.h"
24 #include "cdtpcontact.h"
25
26 #include <QByteArray>
27 #include <QObject>
28 #include <QString>
29 #include <QUrl>
30
31 #include <QtTracker/ontologies/nco.h>
32 #include <QtTracker/ontologies/nie.h>
33 #include <QtTracker/QLive>
34 #include <QtTracker/Tracker>
35
36 using namespace SopranoLive;
37
38 class CDTpStorageSelectQuery;
39 class CDTpStorageContactResolver;
40 class CDTpStorageSelectAccountsToDelete;
41
42 class CDTpStorage : public QObject
43 {
44     Q_OBJECT
45
46 public:
47     CDTpStorage(QObject *parent = 0);
48     ~CDTpStorage();
49
50     static QString contactLocalId(const QString &contactAccountObjectPath,
51             const QString &contactId);
52     static QString contactLocalId(CDTpContactPtr contactWrapper);
53
54     static QUrl contactIri(const QString &contactLocalId);
55     static QUrl contactIri(CDTpContactPtr contactWrapper);
56
57     static QUrl contactImAddress(const QString &contactAccountObjectPath,
58             const QString &contactId);
59     static QUrl contactImAddress(CDTpContactPtr contactWrapper);
60
61     static QUrl trackerStatusFromTpPresenceType(uint tpPresenceType);
62     static QUrl trackerStatusFromTpPresenceStatus(
63             const QString &tpPresenceStatus);
64     static QUrl authStatus(Tp::Contact::PresenceState);
65
66     static const QUrl defaultGraph;
67     static const QUrl privateGraph;
68
69 public Q_SLOTS:
70     void syncAccountSet(const QList<QString> &accountPaths);
71     void syncAccount(CDTpAccount *accountWrapper);
72     void syncAccount(CDTpAccount *accountWrapper, CDTpAccount::Changes changes);
73     void syncAccountContacts(CDTpAccount *accountWrapper);
74     void syncAccountContacts(CDTpAccount *accountWrapper,
75             const QList<CDTpContactPtr> &contactsAdded,
76             const QList<CDTpContactPtr> &contactsRemoved);
77     void syncAccountContact(CDTpAccount *accountWrapper,
78             CDTpContactPtr contactWrapper, CDTpContact::Changes changes);
79     void setAccountContactsOffline(CDTpAccount *accountWrapper);
80     void removeAccount(const QString &accountObjectPath);
81     void removeContacts(CDTpAccount *accountWrapper,
82             const QList<CDTpContactPtr> &contacts);
83
84 private Q_SLOTS:
85     void onAccountPurgeSelectQueryFinished(CDTpStorageSelectQuery *query);
86     void onAccountOfflineSelectQueryFinished(CDTpStorageSelectQuery *query);
87     void onAccountDeleteSelectQueryFinished(CDTpStorageSelectQuery *query);
88     void onContactPurgeSelectQueryFinished(CDTpStorageSelectQuery *);
89     void onContactDeleteSelectQueryFinished(CDTpStorageSelectQuery *query);
90     void onContactUpdateResolverFinished(CDTpStorageContactResolver *resolver);
91     void onQueueTimerTimeout();
92
93 private:
94     void removeContacts(CDTpStorageSelectQuery *query, bool deleteAccount,
95             QList<QUrl> skipIMAddressList = QList<QUrl>());
96
97     void saveAccountAvatar(RDFUpdate &query, const QByteArray &data, const QString &mimeType,
98             const RDFVariable &imAddress,
99             RDFStatementList &inserts);
100
101     void addContactAliasInfoToQuery(RDFStatementList &inserts,
102             RDFVariableList &lists,
103             const RDFVariable &imAddress,
104             CDTpContactPtr contactWrapper);
105     void addContactPresenceInfoToQuery(RDFStatementList &inserts,
106             RDFVariableList &lists,
107             const RDFVariable &imAddress,
108             CDTpContactPtr contactWrapper);
109     void addContactCapabilitiesInfoToQuery(RDFStatementList &inserts,
110             RDFVariableList &lists,
111             const RDFVariable &imAddress,
112             CDTpContactPtr contactWrapper);
113     void addContactAvatarInfoToQuery(RDFUpdate &query,
114             RDFStatementList &inserts,
115             RDFVariableList &lists,
116             const RDFVariable &imAddress,
117             CDTpContactPtr contactWrapper);
118     void addContactRemoveInfoToQuery(RDFStatementList &deletions,
119             RDFStatementList &inserts,
120             const QString &contactId,
121             CDTpAccount *accountWrapper,
122             CDTpContactPtr contactWrapper);
123     void addContactAuthorizationInfoToQuery(RDFStatementList &inserts,
124             RDFVariableList &lists,
125             const RDFVariable &imAddress,
126             CDTpContactPtr contactWrapper);
127     void addContactInfoToQuery(RDFUpdate &query,
128             const RDFVariable &imContact,
129             CDTpContactPtr contactWrapper);
130     void addContactVoicePhoneNumberToQuery(RDFStatementList &inserts,
131             const RDFVariable &imAffiliation,
132             const QString &phoneNumber);
133     void addContactAddressToQuery(RDFStatementList &inserts,
134             const RDFVariable &imAffiliation,
135             const QString &pobox,
136             const QString &extendedAddress,
137             const QString &streetAddress,
138             const QString &locality,
139             const QString &region,
140             const QString &postalcode,
141             const QString &country);
142     RDFVariable createAffiliation(RDFStatementList &inserts,
143             const RDFVariable &imContact,
144             const Tp::ContactInfoField &field);
145
146     void queueUpdate(CDTpContactPtr contactWrapper, CDTpContact::Changes);
147
148
149     QHash<CDTpContactPtr, CDTpContact::Changes> mUpdateQueue;
150     QTimer mQueueTimer;
151 };
152
153 class CDTpStorageSelectQuery : public QObject
154 {
155     Q_OBJECT
156
157 public:
158     CDTpStorageSelectQuery(const RDFSelect &select, QObject *parent = 0);
159     ~CDTpStorageSelectQuery();
160
161     LiveNodes reply() const { return mReply; }
162
163 Q_SIGNALS:
164     void finished(CDTpStorageSelectQuery *queryWrapper);
165
166 private Q_SLOTS:
167     void onModelUpdated();
168
169 private:
170     LiveNodes mReply;
171 };
172
173 class CDTpStorageAccountSelectQuery: public CDTpStorageSelectQuery
174 {
175     Q_OBJECT
176
177 public:
178     CDTpStorageAccountSelectQuery(CDTpAccount *accountWrapper,
179             const RDFSelect &select, QObject *parent = 0);
180     ~CDTpStorageAccountSelectQuery() {};
181
182     CDTpAccount *accountWrapper() const;
183
184 private:
185     CDTpAccount *mAccountWrapper;
186 };
187
188 class CDTpStorageContactResolver : public QObject
189 {
190     Q_OBJECT
191
192 public:
193     CDTpStorageContactResolver(
194             const QHash<CDTpContactPtr, CDTpContact::Changes> &contactsToResolve,
195             QObject *parent = 0);
196     ~CDTpStorageContactResolver();
197
198     QList<CDTpContactPtr> remoteContacts() const;
199     QString storageIdForContact(const CDTpContactPtr &contactWrapper) const;
200     CDTpContact::Changes contactChanges(const CDTpContactPtr &contactWrapper) const;
201
202 Q_SIGNALS:
203    void finished(CDTpStorageContactResolver *resolveWrapper);
204
205 private Q_SLOTS:
206     void onStorageResolveSelectQueryFinished(CDTpStorageSelectQuery *queryWrapper);
207
208 private:
209     QHash<CDTpContactPtr, QString> mResolvedContacts;
210     QHash<CDTpContactPtr, CDTpContact::Changes> mContacts;
211 };
212
213 #endif // CDTPSTORAGE_H