Debian Codegraph @

online-accounts-ui/src/external-browser-request.cpp

raw symbols SCIP
1 /*
2 * This file is part of lomiri-online-accounts-ui
3 *
4 * Copyright (C) 2021 UBports Foundation
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 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 "external-browser-request.h"
22
23 #include <online-accounts-common/debug.h>
24 #include "dialog.h"
25 #include <online-accounts-common/globals.h>
26 #include <online-accounts-common/i18n.h>
27
28 #include <LomiriOnlineAccountsPlugin/request-handler.h>
29 #include <QQmlContext>
30 #include <QQmlEngine>
31 #include <QVariant>
32 #include <SignOn/uisessiondata.h>
33 #include <SignOn/uisessiondata_priv.h>
34
35 using namespace SignOnUi;
36
37 namespace SignOnUi {
38
39 class ExternalBrowserRequestPrivate: public QObject
40 {
41 Q_OBJECT
42 Q_DECLARE_PUBLIC(ExternalBrowserRequest)
43 Q_PROPERTY(QString title READ title CONSTANT)
44 Q_PROPERTY(QUrl startUrl READ startUrl CONSTANT)
45
46 public:
47 ExternalBrowserRequestPrivate(ExternalBrowserRequest *request);
48 ~ExternalBrowserRequestPrivate();
49
50 void start();
51
52 QString title() const { return q_ptr->windowTitle(); }
53 QUrl startUrl() const { return m_startUrl; }
54
55 public Q_SLOTS:
56 /* This method must be called when the final URL has been visited.
57 * Typically, a client should have setup a LoopbackServer on localhost, and
58 * forward the URL from the LoopbackServer::visited() signal to this slot.
59 */
60 void urlVisited(const QUrl &url);
61 void cancel();
62
63 private Q_SLOTS:
64 void onFinished();
65
66 private:
67 void buildDialog(const QVariantMap &params);
68 void closeView();
69
70 private:
71 Dialog *m_dialog;
72 QUrl m_startUrl;
73 QUrl m_responseUrl;
74 mutable ExternalBrowserRequest *q_ptr;
75 };
76
77 } // namespace
78
79 ExternalBrowserRequestPrivate::ExternalBrowserRequestPrivate(ExternalBrowserRequest *request):
80 QObject(request),
81 m_dialog(0),
82 q_ptr(request)
83 {
84 }
85
86 ExternalBrowserRequestPrivate::~ExternalBrowserRequestPrivate()
87 {
88 DEBUG();
89 closeView();
90 delete m_dialog;
91 }
92
93 void ExternalBrowserRequestPrivate::start()
94 {
95 Q_Q(ExternalBrowserRequest);
96
97 const QVariantMap &params = q->parameters();
98 DEBUG() << params;
99
100 m_startUrl = params.value(SSOUI_KEY_OPENURL).toString();
101 if (!q->hasHandler()) {
102 buildDialog(params);
103
104 QObject::connect(m_dialog, SIGNAL(finished(int)),
105 this, SLOT(onFinished()));
106
107 m_dialog->engine()->addImportPath(PLUGIN_PRIVATE_MODULE_DIR);
108 m_dialog->rootContext()->setContextProperty("request", this);
109 m_dialog->setSource(QUrl("qrc:/qml/SignOnUiPage.qml"));
110 } else {
111 DEBUG() << "Setting request on handler";
112 q->handler()->setRequest(this);
113 }
114 }
115
116 void ExternalBrowserRequestPrivate::urlVisited(const QUrl &url)
117 {
118 DEBUG() << "Url visited:" << url;
119
120 m_responseUrl = url;
121 onFinished();
122 }
123
124 void ExternalBrowserRequestPrivate::cancel()
125 {
126 Q_Q(ExternalBrowserRequest);
127
128 DEBUG() << "Client requested to cancel";
129 q->setCanceled();
130 closeView();
131 }
132
133 void ExternalBrowserRequestPrivate::onFinished()
134 {
135 Q_Q(ExternalBrowserRequest);
136
137 DEBUG() << "ExternalBrowser dialog closed";
138 QObject::disconnect(m_dialog, SIGNAL(finished(int)),
139 this, SLOT(onFinished()));
140
141 QVariantMap reply;
142 reply[SSOUI_KEY_URLRESPONSE] = m_responseUrl.toString();
143
144 closeView();
145
146 q->setResult(reply);
147 }
148
149 void ExternalBrowserRequestPrivate::buildDialog(const QVariantMap &params)
150 {
151 m_dialog = new Dialog;
152
153 QString title;
154 if (params.contains(SSOUI_KEY_TITLE)) {
155 title = params[SSOUI_KEY_TITLE].toString();
156 } else if (params.contains(SSOUI_KEY_CAPTION)) {
157 title = OnlineAccountsUi::_("Web authentication for %1",
158 SIGNONUI_I18N_DOMAIN).
159 arg(params[SSOUI_KEY_CAPTION].toString());
160 } else {
161 title = OnlineAccountsUi::_("Web authentication",
162 SIGNONUI_I18N_DOMAIN);
163 }
164
165 m_dialog->setTitle(title);
166
167 DEBUG() << "Dialog was built";
168 }
169
170 void ExternalBrowserRequestPrivate::closeView()
171 {
172 Q_Q(ExternalBrowserRequest);
173
174 DEBUG();
175 if (q->hasHandler()) {
176 q->handler()->setRequest(0);
177 } else if (m_dialog) {
178 m_dialog->close();
179 }
180 }
181
182 ExternalBrowserRequest::ExternalBrowserRequest(
183 int id,
184 const QString &clientProfile,
185 const QVariantMap &parameters,
186 QObject *parent):
187 Request(id, clientProfile, parameters, parent),
188 d_ptr(new ExternalBrowserRequestPrivate(this))
189 {
190 }
191
192 ExternalBrowserRequest::~ExternalBrowserRequest()
193 {
194 }
195
196 void ExternalBrowserRequest::start()
197 {
198 Q_D(ExternalBrowserRequest);
199
200 Request::start();
201 d->start();
202 }
203
204 #include "external-browser-request.moc"