Debian Codegraph @

online-accounts-ui/src/request.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-ui
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * 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 General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <online-accounts-common/debug.h>
22 // TODO #include "dialog-request.h"
23 #include <online-accounts-common/globals.h>
24 #include "provider-request.h"
25 #include "request.h"
26 #include "signonui-request.h"
27
28 #include <QFile>
29 #include <QPointer>
30
31 using namespace OnlineAccountsUi;
32
33 static bool mapIsSuperset(const QVariantMap &test, const QVariantMap &set)
34 {
35 QMapIterator<QString, QVariant> it(set);
36 while (it.hasNext()) {
37 it.next();
38 if (test.value(it.key()) != it.value()) return false;
39 }
40
41 return true;
42 }
43
44 namespace OnlineAccountsUi {
45
46 static QList<Request *> allRequests;
47
48 class RequestPrivate: public QObject
49 {
50 Q_OBJECT
51 Q_DECLARE_PUBLIC(Request)
52
53 public:
54 RequestPrivate(const QString &interface,
55 int id,
56 const QString &clientProfile,
57 const QVariantMap &parameters,
58 Request *request);
59 ~RequestPrivate();
60
61 WId windowId() const {
62 return m_parameters[OAU_KEY_WINDOW_ID].toUInt();
63 }
64
65 private:
66 void setWindow(QWindow *window);
67
68 private:
69 mutable Request *q_ptr;
70 QString m_interface;
71 int m_id;
72 QVariantMap m_parameters;
73 QString m_clientApparmorProfile;
74 bool m_inProgress;
75 QPointer<QWindow> m_window;
76 QString m_errorName;
77 QString m_errorMessage;
78 QVariantMap m_result;
79 int m_delay;
80 };
81
82 } // namespace
83
84 RequestPrivate::RequestPrivate(const QString &interface,
85 int id,
86 const QString &clientProfile,
87 const QVariantMap &parameters,
88 Request *request):
89 QObject(request),
90 q_ptr(request),
91 m_interface(interface),
92 m_id(id),
93 m_parameters(parameters),
94 m_clientApparmorProfile(clientProfile),
95 m_inProgress(false),
96 m_window(0),
97 m_delay(0)
98 {
99 }
100
101 RequestPrivate::~RequestPrivate()
102 {
103 }
104
105 void RequestPrivate::setWindow(QWindow *window)
106 {
107 if (m_window != 0) {
108 qWarning() << "Widget already set";
109 return;
110 }
111
112 m_window = window;
113
114 if (windowId() != 0) {
115 DEBUG() << "Requesting window reparenting";
116 QWindow *parent = QWindow::fromWinId(windowId());
117 window->setTransientParent(parent);
118 }
119 window->show();
120 }
121
122 /* Some unit tests might need to provide a different implementation for the
123 * Request::newRequest() factory method; for this reason, we allow the method
124 * to be excluded from compilation.
125 */
126 #ifndef NO_REQUEST_FACTORY
127 Request *Request::newRequest(const QString &interface,
128 int id,
129 const QString &clientProfile,
130 const QVariantMap &parameters,
131 QObject *parent)
132 {
133 /* If the supported requests types vary considerably, we can create
134 * different subclasses for handling them, and in this method we examine
135 * the @parameters argument to figure out which subclass is the most apt to
136 * handle the request. */
137 if (interface == OAU_INTERFACE) {
138 return new ProviderRequest(interface, id, clientProfile,
139 parameters, parent);
140 } else {
141 Q_ASSERT(interface == SIGNONUI_INTERFACE);
142 return SignOnUi::Request::newRequest(id, clientProfile,
143 parameters, parent);
144 }
145 }
146 #endif
147
148 Request::Request(const QString &interface,
149 int id,
150 const QString &clientProfile,
151 const QVariantMap &parameters,
152 QObject *parent):
153 QObject(parent),
154 d_ptr(new RequestPrivate(interface, id, clientProfile, parameters, this))
155 {
156 allRequests.append(this);
157 }
158
159 Request::~Request()
160 {
161 allRequests.removeOne(this);
162 }
163
164 Request *Request::find(const QVariantMap &match)
165 {
166 Q_FOREACH(Request *r, allRequests) {
167 if (mapIsSuperset(r->parameters(), match)) {
168 return r;
169 }
170 }
171
172 return 0;
173 }
174
175 QString Request::interface() const
176 {
177 Q_D(const Request);
178 return d->m_interface;
179 }
180
181 int Request::id() const
182 {
183 Q_D(const Request);
184 return d->m_id;
185 }
186
187 void Request::setWindow(QWindow *window)
188 {
189 Q_D(Request);
190 d->setWindow(window);
191 }
192
193 WId Request::windowId() const
194 {
195 Q_D(const Request);
196 return d->windowId();
197 }
198
199 bool Request::isInProgress() const
200 {
201 Q_D(const Request);
202 return d->m_inProgress;
203 }
204
205 const QVariantMap &Request::parameters() const
206 {
207 Q_D(const Request);
208 return d->m_parameters;
209 }
210
211 QString Request::clientApparmorProfile() const
212 {
213 Q_D(const Request);
214 return d->m_clientApparmorProfile;
215 }
216
217 QWindow *Request::window() const
218 {
219 Q_D(const Request);
220 return d->m_window;
221 }
222
223 QVariantMap Request::result() const
224 {
225 Q_D(const Request);
226 return d->m_result;
227 }
228
229 QString Request::errorName() const
230 {
231 Q_D(const Request);
232 return d->m_errorName;
233 }
234
235 QString Request::errorMessage() const
236 {
237 Q_D(const Request);
238 return d->m_errorMessage;
239 }
240
241 void Request::setDelay(int delay)
242 {
243 Q_D(Request);
244 d->m_delay = delay;
245 }
246
247 int Request::delay() const
248 {
249 Q_D(const Request);
250 return d->m_delay;
251 }
252
253 void Request::start()
254 {
255 Q_D(Request);
256 if (d->m_inProgress) {
257 qWarning() << "Request already started!";
258 return;
259 }
260 d->m_inProgress = true;
261 }
262
263 void Request::cancel()
264 {
265 setCanceled();
266 }
267
268 void Request::fail(const QString &name, const QString &message)
269 {
270 Q_D(Request);
271
272 DEBUG() << name << message;
273
274 d->m_errorName = name;
275 d->m_errorMessage = message;
276
277 Q_EMIT completed();
278 }
279
280 void Request::setCanceled()
281 {
282 Q_D(Request);
283 if (d->m_inProgress) {
284 fail(OAU_ERROR_USER_CANCELED, QStringLiteral("Canceled"));
285 d->m_inProgress = false;
286 }
287 }
288
289 void Request::setResult(const QVariantMap &result)
290 {
291 Q_D(Request);
292 if (d->m_inProgress) {
293 DEBUG() << result;
294 d->m_result = result;
295
296 Q_EMIT completed();
297 d->m_inProgress = false;
298 }
299 }
300
301 QString Request::mountPoint() const
302 {
303 return qEnvironmentVariableIsSet("SNAP") ?
304 QFile::decodeName(qgetenv("SNAP")) : "";
305 }
306
307 #include "request.moc"