Debian Codegraph @

online-accounts/lib/qt/LomiriOnlineAccounts/manager.cpp

raw symbols SCIP
1 /*
2 * This file is part of libOnlineAccounts
3 *
4 * Copyright (C) 2015 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License version 3, as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "manager_p.h"
22
23 #include <QDBusConnection>
24 #include <QDBusMessage>
25 #include <QDBusPendingCallWatcher>
26 #include <QDebug>
27 #include "account_p.h"
28 #include "authentication_data_p.h"
29 #include <LomiriOnlineAccountsDaemon/dbus_constants.h>
30 #include "pending_call_p.h"
31
32 using namespace OnlineAccounts;
33
34 Q_LOGGING_CATEGORY(DBG_ONLINE_ACCOUNTS, "OnlineAccounts", QtWarningMsg)
35
36 ManagerPrivate::ManagerPrivate(Manager *q, const QString &applicationId,
37 const QDBusConnection& bus):
38 QObject(),
39 m_applicationId(applicationId),
40 m_daemon(ONLINE_ACCOUNTS_MANAGER_SERVICE_NAME,
41 ONLINE_ACCOUNTS_MANAGER_PATH,
42 ONLINE_ACCOUNTS_MANAGER_INTERFACE,
43 bus),
44 m_getAccountsCall(0),
45 q_ptr(q)
46 {
47 qRegisterMetaType<Account*>();
48 qRegisterMetaType<Service>("OnlineAccounts::Service");
49
50 QObject::connect(&m_daemon,
51 SIGNAL(accountChanged(const QString&, const OnlineAccounts::AccountInfo&)),
52 this,
53 SLOT(onAccountChanged(const QString&, const OnlineAccounts::AccountInfo&)));
54 retrieveAccounts();
55 }
56
57 ManagerPrivate::~ManagerPrivate()
58 {
59 delete m_getAccountsCall;
60 m_getAccountsCall = 0;
61 }
62
63 PendingCall ManagerPrivate::authenticate(const AccountInfo &info,
64 const AuthenticationData &authData)
65 {
66 Q_Q(Manager);
67 QDBusPendingCall call = m_daemon.authenticate(info.id(),
68 info.service(),
69 authData.interactive(),
70 authData.mustInvalidateCachedReply(),
71 authData.d->m_parameters);
72 return PendingCall(new PendingCallPrivate(q, call,
73 PendingCallPrivate::Authenticate,
74 authData.method()));
75 }
76
77 PendingCall ManagerPrivate::requestAccess(const QString &service,
78 const QVariantMap &parameters)
79 {
80 Q_Q(Manager);
81 QDBusPendingCall call = m_daemon.requestAccess(service, parameters);
82 return PendingCall(new PendingCallPrivate(q, call,
83 PendingCallPrivate::RequestAccess,
84 AuthenticationMethodUnknown));
85 }
86
87 Account *ManagerPrivate::ensureAccount(const AccountInfo &info)
88 {
89 if (Q_UNLIKELY(info.id() == 0)) return 0;
90 if (Q_UNLIKELY(info.service().isEmpty())) return 0;
91
92 auto i = m_accounts.find({info.id(), info.service()});
93 if (i == m_accounts.end()) {
94 i = m_accounts.insert({info.id(), info.service()}, AccountData(info));
95 }
96
97 AccountData &accountData = i.value();
98 accountData.info = info.stripMetadata();
99 if (!accountData.account) {
100 accountData.account =
101 new Account(new AccountPrivate(q_ptr, accountData.info), this);
102 } else {
103 /* Update the account information */
104 accountData.account->d_ptr->update(accountData.info);
105 }
106
107 return accountData.account;
108 }
109
110 void ManagerPrivate::retrieveAccounts()
111 {
112 if (Q_UNLIKELY(m_getAccountsCall)) return;
113
114 QVariantMap filters;
115 filters["applicationId"] = m_applicationId;
116 m_getAccountsCall =
117 new QDBusPendingCallWatcher(m_daemon.getAccounts(filters));
118 QObject::connect(m_getAccountsCall,
119 SIGNAL(finished(QDBusPendingCallWatcher*)),
120 this, SLOT(onGetAccountsFinished()));
121 }
122
123 void ManagerPrivate::onGetAccountsFinished()
124 {
125 Q_Q(Manager);
126
127 Q_ASSERT(m_getAccountsCall);
128
129 QDBusPendingReply<QList<AccountInfo>,QList<QVariantMap>> reply = *m_getAccountsCall;
130 if (Q_UNLIKELY(reply.isError())) {
131 qCWarning(DBG_ONLINE_ACCOUNTS) << "GetAccounts call failed:" <<
132 reply.error();
133 /* No special handling of the error: the Manager will simply not have
134 * any account */
135 } else {
136 QList<AccountInfo> accountInfos = reply.argumentAt<0>();
137 Q_FOREACH(const AccountInfo &info, accountInfos) {
138 m_accounts.insert({info.id(), info.service()}, AccountData(info));
139 }
140
141 QList<QVariantMap> services = reply.argumentAt<1>();
142 for (const QVariantMap &data: services) {
143 Service service(new Service::ServiceData(data));
144 m_services.insert(service.id(), service);
145 }
146 }
147 m_getAccountsCall->deleteLater();
148 m_getAccountsCall = 0;
149
150 Q_EMIT q->ready();
151 }
152
153 void ManagerPrivate::onAccountChanged(const QString &service,
154 const AccountInfo &info)
155 {
156 Q_Q(Manager);
157 Q_UNUSED(service);
158
159 Account *account = ensureAccount(info);
160 if (info.changeType() == AccountInfo::Enabled) {
161 Q_EMIT q->accountAvailable(account);
162 } else if (info.changeType() == AccountInfo::Disabled) {
163 account->d_ptr->setInvalid();
164 /* We don't delete the account, since the client might be using it, but
165 * we remove it from our list so that we won't return it to the client
166 * anymore.
167 */
168 m_accounts.remove({info.id(), service});
169 }
170
171 /* No need to handle the Update change type: ensureAccount already updates
172 * the account and emits the necessary notification */
173 }
174
175 Manager::Manager(const QString &applicationId, QObject *parent):
176 Manager(applicationId, QDBusConnection::sessionBus(), parent)
177 {
178 }
179
180 Manager::Manager(const QString &applicationId, const QDBusConnection& bus,
181 QObject *parent):
182 QObject(parent),
183 d_ptr(new ManagerPrivate(this, applicationId, bus))
184 {
185 }
186
187 Manager::~Manager()
188 {
189 delete d_ptr;
190 d_ptr = 0;
191 }
192
193 bool Manager::isReady() const
194 {
195 Q_D(const Manager);
196 return !d->m_getAccountsCall;
197 }
198
199 void Manager::waitForReady()
200 {
201 Q_D(Manager);
202 if (d->m_getAccountsCall) {
203 d->m_getAccountsCall->waitForFinished();
204 }
205 }
206
207 QList<Service> Manager::availableServices() const
208 {
209 Q_D(const Manager);
210 return d->m_services.values();
211 }
212
213 QList<Account*> Manager::availableAccounts(const QString &service)
214 {
215 Q_D(Manager);
216
217 QList<Account*> result;
218 for (auto i = d->m_accounts.begin(); i != d->m_accounts.end(); i++) {
219 AccountData &accountData = i.value();
220 if (!service.isEmpty() && accountData.info.service() != service) continue;
221
222 if (!accountData.account) {
223 accountData.account =
224 new Account(new AccountPrivate(this, accountData.info), this);
225 }
226
227 result.append(accountData.account);
228 }
229
230 return result;
231 }
232
233 Account *Manager::account(AccountId accountId)
234 {
235 return account(accountId, QString());
236 }
237
238 Account *Manager::account(AccountId accountId, const QString &serviceId)
239 {
240 Q_D(Manager);
241
242 decltype(d->m_accounts)::iterator i;
243 if (serviceId.isEmpty()) {
244 // Any non-empty service ID will sort after the empty string
245 i = d->m_accounts.lowerBound({accountId, QString()});
246 if (Q_UNLIKELY(i == d->m_accounts.end() ||
247 i.key().first != accountId)) {
248 return 0;
249 }
250 } else {
251 i = d->m_accounts.find({accountId, serviceId});
252 if (Q_UNLIKELY(i == d->m_accounts.end())) return 0;
253 }
254
255 AccountData &accountData = i.value();
256 if (!accountData.account) {
257 accountData.account =
258 new Account(new AccountPrivate(this, accountData.info), this);
259 }
260
261 return accountData.account;
262 }
263
264 PendingCall Manager::requestAccess(const QString &service,
265 const AuthenticationData &authData)
266 {
267 Q_D(Manager);
268 return d->requestAccess(service, authData.d->m_parameters);
269 }