WIP: CDTpStorage::syncAccount ported to qsparql. SaveAvatar still needs to be ported.
[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 <QByteArray>
24 #include <QObject>
25 #include <QString>
26 #include <QUrl>
27
28 #include <QtTracker/Tracker>
29 #include <QtSparql/QSparqlQuery>
30
31 #include "cdtpaccount.h"
32 #include "cdtpcontact.h"
33 #include "cdtpquery.h"
34
35 using namespace SopranoLive;
36
37 class CDTpStorageSyncOperations;
38
39 class CDTpStorage : public QObject
40 {
41     Q_OBJECT
42
43 public:
44     CDTpStorage(QObject *parent = 0);
45     ~CDTpStorage();
46
47     static QString contactLocalId(const QString &contactAccountObjectPath,
48             const QString &contactId);
49     static QString contactLocalId(CDTpContactPtr contactWrapper);
50
51     static QUrl contactIri(const QString &contactLocalId);
52     static QUrl contactIri(CDTpContactPtr contactWrapper);
53
54     static QUrl contactImAddress(const QString &contactAccountObjectPath,
55             const QString &contactId);
56     static QUrl contactImAddress(CDTpContactPtr contactWrapper);
57
58     static QUrl contactAffiliation(const QString &contactAccountObjectPath,
59             const QString &contactId);
60     static QUrl contactAffiliation(CDTpContactPtr contactWrapper);
61
62     static QUrl trackerStatusFromTpPresenceType(uint tpPresenceType);
63
64     static QString trackerStatusFromTpPresenceStatus(
65             const QString &tpPresenceStatus);
66
67     static QUrl authStatus(Tp::Contact::PresenceState);
68
69     static const QUrl defaultGraph;
70     static const QUrl privateGraph;
71
72 Q_SIGNALS:
73     void syncStarted(CDTpAccountPtr accountWrapper);
74     void syncEnded(CDTpAccountPtr accountWrapper, int contactsAdded, int contactsRemoved);
75
76 public Q_SLOTS:
77     void syncAccountSet(const QList<QString> &accountPaths);
78     void syncAccount(CDTpAccountPtr accountWrapper);
79     void syncAccount(CDTpAccountPtr accountWrapper, CDTpAccount::Changes changes);
80     void syncAccountContacts(CDTpAccountPtr accountWrapper);
81     void syncAccountContacts(CDTpAccountPtr accountWrapper,
82             const QList<CDTpContactPtr> &contactsAdded,
83             const QList<CDTpContactPtr> &contactsRemoved);
84     void syncAccountContact(CDTpAccountPtr accountWrapper,
85             CDTpContactPtr contactWrapper, CDTpContact::Changes changes);
86     void setAccountContactsOffline(CDTpAccountPtr accountWrapper);
87     void removeAccount(const QString &accountObjectPath);
88     void removeContacts(CDTpAccountPtr accountWrapper,
89             const QList<CDTpContactPtr> &contacts);
90
91 private Q_SLOTS:
92     void onAccountPurgeSelectQueryFinished(CDTpSelectQuery *query);
93     void onAccountOfflineSelectQueryFinished(CDTpSelectQuery *query);
94     void onContactPurgeSelectQueryFinished(CDTpSelectQuery *);
95     void onContactDeleteSelectQueryFinished(CDTpSelectQuery *query);
96     void onContactUpdateSelectQueryFinished(CDTpSelectQuery *query);
97     void onAccountsUpdateQueryFinished(CDTpUpdateQuery *query);
98     void onQueueTimerTimeout();
99     void onQueryFinished();
100
101 private:
102     void saveAccountAvatar(RDFUpdate &query, const QByteArray &data, const QString &mimeType,
103             const RDFVariable &imAddress,
104             RDFStatementList &inserts);
105
106     void addRemoveContactToQuery(RDFUpdate &query,
107             RDFStatementList &inserts,
108             RDFStatementList &deletions,
109             const CDTpContactsSelectItem &item);
110     void addRemoveContactFromAccountToQuery(RDFStatementList &deletions,
111             const CDTpContactsSelectItem &item);
112
113     void addContactAliasInfoToQuery(RDFStatementList &inserts,
114             RDFVariableList &lists,
115             const RDFVariable &imAddress,
116             CDTpContactPtr contactWrapper);
117     void addContactPresenceInfoToQuery(RDFStatementList &inserts,
118             RDFVariableList &lists,
119             const RDFVariable &imAddress,
120             CDTpContactPtr contactWrapper);
121     void addContactCapabilitiesInfoToQuery(RDFStatementList &inserts,
122             RDFVariableList &lists,
123             const RDFVariable &imAddress,
124             Tp::CapabilitiesBase capabilities);
125     void addContactAvatarInfoToQuery(RDFUpdate &query,
126             RDFStatementList &inserts,
127             RDFVariableList &lists,
128             const RDFVariable &imAddress,
129             CDTpContactPtr contactWrapper);
130     void addContactRemoveInfoToQuery(RDFStatementList &deletions,
131             RDFStatementList &inserts,
132             const QString &contactId,
133             CDTpAccountPtr accountWrapper,
134             CDTpContactPtr contactWrapper);
135     void addContactAuthorizationInfoToQuery(RDFStatementList &inserts,
136             RDFVariableList &lists,
137             const RDFVariable &imAddress,
138             CDTpContactPtr contactWrapper);
139     void addRemoveContactInfoToQuery(RDFUpdate &query,
140             const RDFVariable &imContact,
141             const QUrl &graph);
142     void addContactInfoToQuery(RDFUpdate &query,
143             RDFStatementList &inserts,
144             const RDFVariable &imContact,
145             CDTpContactPtr contactWrapper);
146     void addContactVoicePhoneNumberToQuery(RDFStatementList &graphInserts,
147             RDFStatementList &inserts,
148             const RDFVariable &affiliation,
149             const QString &phoneNumber);
150     void addContactAddressToQuery(RDFStatementList &graphInserts,
151             const RDFVariable &affiliation,
152             const QString &pobox,
153             const QString &extendedAddress,
154             const QString &streetAddress,
155             const QString &locality,
156             const QString &region,
157             const QString &postalcode,
158             const QString &country);
159     void addContactEmailToQuery(RDFStatementList &graphInserts,
160             RDFStatementList &inserts,
161             const RDFVariable &affiliation,
162             const QString &email);
163     RDFVariable ensureAffiliation(QHash<QString, RDFVariable> &map,
164             RDFStatementList &graphInserts,
165             const RDFVariable &imContact,
166             const Tp::ContactInfoField &field);
167     QString safeStringListAt(const QStringList &list, int i);
168
169     void queueUpdate(CDTpContactPtr contactWrapper, CDTpContact::Changes);
170     void oneSyncOperationFinished(CDTpAccountPtr accountWrapper);
171     void executeQuery(const QSparqlQuery &query);
172     static QString sparqlRepresentation(const QDateTime &dateTime);
173
174 private:
175     QHash<CDTpContactPtr, CDTpContact::Changes> mUpdateQueue;
176     QTimer mQueueTimer;
177     QHash<CDTpAccountPtr, CDTpStorageSyncOperations> mSyncOperations;
178 };
179
180 class CDTpStorageSyncOperations
181 {
182 public:
183     CDTpStorageSyncOperations();
184     bool active;
185     int nPendingOperations;
186     int nContactsAdded;
187     int nContactsRemoved;
188 };
189
190 #endif // CDTPSTORAGE_H