Debian Codegraph @

online-accounts/lib/qml/Lomiri/OnlineAccounts/account.cpp

raw symbols SCIP
1 /*
2 * This file is part of OnlineAccountsModule
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 "account.h"
22 #include "authentication_data.h"
23
24 #include <LomiriOnlineAccounts/Account>
25 #include <LomiriOnlineAccounts/AuthenticationData>
26 #include <LomiriOnlineAccounts/AuthenticationReply>
27 #include <LomiriOnlineAccounts/PendingCall>
28
29 #include <QDebug>
30 #include <QJSEngine>
31
32 using namespace OnlineAccountsModule;
33
34 namespace OnlineAccountsModule {
35
36 class AccountPrivate: public QObject
37 {
38 Q_OBJECT
39 Q_DECLARE_PUBLIC(Account)
40
41 public:
42 AccountPrivate(OnlineAccounts::Account *account, QJSEngine *engine,
43 Account *q);
44
45 private Q_SLOTS:
46 void onAuthenticationFinished();
47
48 private:
49 OnlineAccounts::Account *m_account;
50 QJSEngine *m_engine;
51 mutable Account *q_ptr;
52 };
53
54 } // namespace
55
56 AccountPrivate::AccountPrivate(OnlineAccounts::Account *account,
57 QJSEngine *engine,
58 Account *q):
59 m_account(account),
60 m_engine(engine),
61 q_ptr(q)
62 {
63 QObject::connect(account, SIGNAL(changed()),
64 q, SIGNAL(accountChanged()));
65 QObject::connect(account, SIGNAL(disabled()),
66 q, SIGNAL(validChanged()));
67 }
68
69 void AccountPrivate::onAuthenticationFinished()
70 {
71 Q_Q(Account);
72
73 auto watcher = qobject_cast<OnlineAccounts::PendingCallWatcher*>(sender());
74
75 OnlineAccounts::AuthenticationReply reply(*watcher);
76 QVariantMap map;
77 if (reply.hasError()) {
78 map["errorCode"] = reply.error().code();
79 map["errorText"] = reply.error().text();
80 } else {
81 map = replyToMap(*watcher);
82 }
83
84 Q_EMIT q->authenticationReply(map);
85 }
86
87 /*!
88 * \qmltype Account
89 * \inqmlmodule Lomiri.OnlineAccounts 2.0
90 * \ingroup Lomiri
91 * \brief Representation of an online account
92 *
93 * The Account object holds the information related to an account and provides
94 * methods to interact with it.
95 * It's not possible to create such objects from QML; instead, they are
96 * returned by the \l AccountModel in the \c account role or in the \l
97 * {AccountModel::accountList} { \c accountList} property.
98 *
99 * Here's an example on how to use the account object in a delegate:
100 *
101 * \qml
102 * import QtQuick 2.0
103 * import Lomiri.OnlineAccounts 2.0
104 *
105 * ListView {
106 * model: AccountModel {}
107 * delegate: Button {
108 * text: "Authenticate " + model.displayName
109 * onClicked: model.account.authenticate({})
110 * Connections {
111 * target: model.account
112 * onAuthenticationReply: {
113 * console.log("Access token is " + reply['AccessToken'])
114 * }
115 * }
116 * }
117 * }
118 * \endqml
119 *
120 * \target errorCode
121 * \section3 Error codes used in this module
122 * Some operations, such as the \l Account::authenticate() and the \l
123 * AccountModel::requestAccess() methods, can fail and return one of these
124 * error codes:
125 * \list
126 * \li \c Account.ErrorCodeNoAccount - The accounts is invalid
127 * \li \c Account.ErrorCodeUserCanceled - The operation was canceled by the user
128 * \li \c Account.ErrorCodePermissionDenied - The application has no
129 * permission to complete the operation
130 * \endlist
131 */
132
133 Account::Account(OnlineAccounts::Account *account, QJSEngine *engine,
134 QObject *parent):
135 QObject(parent),
136 d_ptr(new AccountPrivate(account, engine, this))
137 {
138 }
139
140 Account::~Account()
141 {
142 delete d_ptr;
143 }
144
145 /*!
146 * \qmlproperty bool Account::valid
147 *
148 * Whether the account object is valid; this is usually \c true, because the \c
149 * AccountModel never gives out invalid accounts. However, it can happen that a
150 * valid account becomes invalid while the application is using it (if, for
151 * instance, the user deleted the account or revoked the application's access
152 * rights to use it). As soon as this property becomes \c false, the
153 * application should stop using this account.
154 */
155 bool Account::isValid() const
156 {
157 Q_D(const Account);
158 return d->m_account->isValid();
159 }
160
161 /*!
162 * \qmlproperty string Account::displayName
163 *
164 * The display name of the account. This is usually the user's login name, but
165 * applications should not rely on the value of this property. Use it only for
166 * display purposes.
167 */
168 QString Account::displayName() const
169 {
170 Q_D(const Account);
171 return d->m_account->displayName();
172 }
173
174 /*!
175 * \qmlproperty int Account::accountId
176 *
177 * Numeric identifier of the account. This property remains constant during the
178 * lifetime of the account. Note, however, that if the user deletes the account
179 * and re-creates it, its ID will be different.
180 */
181 int Account::accountId() const
182 {
183 Q_D(const Account);
184 return d->m_account->id();
185 }
186
187 /*!
188 * \qmlproperty int Account::serviceId
189 *
190 * Identifier for the service used with the account.
191 */
192 QString Account::serviceId() const
193 {
194 Q_D(const Account);
195 return d->m_account->serviceId();
196 }
197
198 /*!
199 * \qmlproperty int Account::service
200 *
201 * Service data associated with this account. This is an object containing the
202 * following properties:
203 * \list
204 * \li \c serviceId - same service ID returned by Account::serviceId
205 * \li \c displayName - the localized display name for the service
206 * \li \c iconSource - URL for the icon; can be a "file://" URL to a local
207 * file, or an icon from the theme if the URL starts with "image://theme/"
208 * \endlist
209 */
210 QJSValue Account::service() const
211 {
212 Q_D(const Account);
213 #if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
214 return d->m_engine->toScriptValue(d->m_account->service());
215 #else
216 return d->m_engine->toScriptValue(d->m_account->service().toMap());
217 #endif
218 }
219
220 /*!
221 * \qmlproperty enumeration Account::authenticationMethod
222 *
223 * The authentication method used when authenticating with the account.
224 * Currently, these authentication methods are supported:
225 * \list
226 * \li \c Account.AuthenticationMethodOAuth1 - OAuth 1.0
227 * \li \c Account.AuthenticationMethodOAuth2 - OAuth 2.0
228 * \li \c Account.AuthenticationMethodSasl - SASL
229 * \li \c Account.AuthenticationMethodPassword - username/password
230 * \endlist
231 */
232 Account::AuthenticationMethod Account::authenticationMethod() const
233 {
234 Q_D(const Account);
235 return AuthenticationMethod(d->m_account->authenticationMethod());
236 }
237
238 /*!
239 * \qmlproperty jsobject Account::settings
240 *
241 * A dictionary of the settings stored into the account.
242 */
243 QVariantMap Account::settings() const
244 {
245 Q_D(const Account);
246 QVariantMap ret;
247 Q_FOREACH(const QString &key, d->m_account->keys()) {
248 ret.insert(key, d->m_account->setting(key));
249 }
250 return ret;
251 }
252
253 OnlineAccounts::Account *Account::internalObject() const
254 {
255 Q_D(const Account);
256 return d->m_account;
257 }
258
259 /*!
260 * \qmlsignal Account::authenticationReply(jsobject authenticationData)
261 *
262 * Emitted when the authentication completes. The \a authenticationData object
263 * will contain the authentication reply. If the authentication failed, the
264 * following two keys will be present:
265 * \list
266 * \li \c errorCode is an \l {errorCode} {error code}
267 * \li \c errorText is a textual description of the error, not meant for the
268 * end-user; it can be used for debugging purposes
269 * \endlist
270 */
271
272 /*!
273 * \qmlmethod void Account::authenticate(jsobject params)
274 *
275 * Perform the authentication on this account. The \a params parameter can be
276 * used to pass authentication data, such as the ClientId and ClientSecret used
277 * in the OAuth flow. The list of the supported authentication parameters
278 * depend on the authentication method being used, and are documented in the
279 * Online Accounts development Guide in the UBports Developer Portal.
280 *
281 * There are, however, two authentication parameters which are available
282 * regardless of the authentication method being used:
283 * \list
284 * \li \c invalidateCachedReply can be set to \c true when the previous
285 * authentication reply returned an invalid access token. It will ensure
286 * the creation of a new access token.
287 * \li \c interactive is \c true by default; if set to \c false, it will ensure
288 * that no interaction with the user will occur.
289 * \endlist
290 *
291 * Each call to this method will cause the \l authenticationReply signal to be
292 * emitted at some time later. Note that the authentication might involve
293 * interactions with the network or with the end-user, so don't expect a reply
294 * to be emitted immediately.
295 *
296 * \sa authenticationReply
297 */
298 void Account::authenticate(const QVariantMap &params)
299 {
300 Q_D(Account);
301 auto method = d->m_account->authenticationMethod();
302 OnlineAccounts::PendingCall call =
303 d->m_account->authenticate(authenticationDataFromMap(params, method));
304 OnlineAccounts::PendingCallWatcher *watcher =
305 new OnlineAccounts::PendingCallWatcher(call, d->m_account);
306 QObject::connect(watcher, SIGNAL(finished()),
307 d, SLOT(onAuthenticationFinished()));
308 }
309
310 #include "account.moc"