/
pmrwebservicemanager.cpp
293 lines (213 loc) · 10 KB
/
pmrwebservicemanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// PMR Web service manager
//==============================================================================
#include "corecliutils.h"
#include "coreguiutils.h"
#include "pmrauthentication.h"
#include "pmrsupportplugin.h"
#include "pmrsupportpreferenceswidget.h"
#include "pmrwebservice.h"
#include "pmrwebservicemanager.h"
#include "pmrwebserviceresponse.h"
#include "pmrwebviewerdialog.h"
#include "progressbarwidget.h"
#include "webviewerwidget.h"
//==============================================================================
#include <QLayout>
#include <QMainWindow>
#include <QTimer>
#include <QWebView>
//==============================================================================
#include "oauthbegin.h"
#include "o2/o1requestor.h"
#include "oauthend.h"
//==============================================================================
namespace OpenCOR {
namespace PMRSupport {
//==============================================================================
PmrWebServiceManager::PmrWebServiceManager(const QString &pPmrUrl,
PmrWebService *pPmrWebService) :
QNetworkAccessManager(pPmrWebService),
mPmrWebService(pPmrWebService)
{
// Make sure that we get told when there are SSL errors (which would happen
// if the website's certificate is invalid, e.g. it has expired)
connect(this, &PmrWebServiceManager::sslErrors,
this, &PmrWebServiceManager::ignoreSslErrors);
// Create, by updating ourselves, our PMR authentication object
update(pPmrUrl);
}
//==============================================================================
bool PmrWebServiceManager::isAuthenticated() const
{
// Return whether we are authenticated to PMR
return mPmrAuthentication->linked();
}
//==============================================================================
void PmrWebServiceManager::authenticate(bool pAuthenticate)
{
// Authenticate ourselves to PMR
mWebViewerDialogUsed = false;
if (pAuthenticate) {
mPmrAuthentication->link();
} else {
mPmrAuthentication->unlink();
}
}
//==============================================================================
void PmrWebServiceManager::authenticationSucceeded()
{
// Let people know whether we are really authenticated, i.e. linked
emit authenticated(qobject_cast<PmrAuthentication *>(sender())->linked());
}
//==============================================================================
void PmrWebServiceManager::authenticationFailed()
{
// Let the user know if s/he tried to authenticate him/herself but was just
// not able to do so
if (!mWebViewerDialogUsed) {
Core::warningMessageBox(tr("PMR Authentication"),
tr("PMR Authentication could not be performed.")+"<br/><br/>"
+tr("<strong>Note:</strong> you might want to check that your system time is correct."));
}
// Let people know that authentication failed
emit authenticated(false);
}
//==============================================================================
void PmrWebServiceManager::openBrowser(const QUrl &pUrl)
{
// Open the given URL in our Web browser
mWebViewerDialogUsed = true;
if (mWebViewerDialog == nullptr) {
mWebViewerDialog = new PmrWebViewerDialog(Core::mainWindow());
connect(mWebViewerDialog, &Core::Dialog::rejected,
this, &PmrWebServiceManager::authenticationCancelled);
} else {
mWebViewerDialog->load({});
// Note: this is in case we used our Web brower before, in which case it
// will contain something, which we don't want to see when
// reopening it...
mWebViewerDialog->retranslateUi();
}
mWebViewerDialog->load(pUrl);
mWebViewerDialog->exec();
}
//==============================================================================
void PmrWebServiceManager::startCloseBrowser()
{
// Close our temporary Web browser after a slight delay
// Note: the whole idea is that granting/denying access resuts in a new
// (empty) page being loaded, so we want to wait until that page is
// loaded before actually closing our temporary Web browser. So, here,
// we have a short delay to give time to our temporary Web browser to
// start loading that new (empty) page. Then, in closeBrowser(), we
// checking whether the loading is finished and, if so, close our
// temporary Web browser otherwise check again in a bit...
QTimer::singleShot(169, this, &PmrWebServiceManager::closeBrowser);
}
//==============================================================================
void PmrWebServiceManager::closeBrowser()
{
// Close our temporary Web browser, but only if the current page has
// finished loading otherwise try again in a bit
if (mWebViewerDialog->isLoadFinished()) {
mWebViewerDialog->close();
} else {
QTimer::singleShot(169, this, &PmrWebServiceManager::closeBrowser);
}
}
//==============================================================================
void PmrWebServiceManager::ignoreSslErrors(QNetworkReply *pNetworkReply,
const QList<QSslError> &pSslErrors)
{
// Ignore SSL errors since we trust the website and therefore its
// certificate (even if it is invalid, e.g. it has expired)
pNetworkReply->ignoreSslErrors(pSslErrors);
}
//==============================================================================
PmrWebServiceResponse * PmrWebServiceManager::request(const QString &pUrl,
bool pSecureRequest,
bool pUsePost,
const QJsonDocument &pJsonDocument)
{
// Check that we are connected to the Internet
if (!Core::hasInternetConnection()) {
emit error(Core::noInternetConnectionMessage());
return nullptr;
}
// Let the user know that we are (going to be) busy
emit busy(true);
// Send our request to PMR, asking for the response to be compressed
QNetworkRequest networkRequest;
networkRequest.setRawHeader("Accept", "application/json");
networkRequest.setRawHeader("Accept-Encoding", "gzip");
networkRequest.setUrl(QUrl(pUrl));
// Use the authenticated link if it's available
QNetworkReply *networkReply;
if (pSecureRequest && mPmrAuthentication->linked()) {
auto requestor = new O1Requestor(this, mPmrAuthentication, this);
if (!pUsePost && pJsonDocument.isEmpty()) {
networkReply = requestor->get(networkRequest, {});
} else {
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, RequestMimeType);
networkReply = requestor->post(networkRequest, {},
pJsonDocument.isNull()?
QByteArray():
pJsonDocument.toJson(QJsonDocument::Compact));
}
} else {
if (!pUsePost && pJsonDocument.isEmpty()) {
networkReply = get(networkRequest);
} else {
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, RequestMimeType);
networkReply = post(networkRequest,
pJsonDocument.isNull()?
QByteArray():
pJsonDocument.toJson(QJsonDocument::Compact));
}
}
auto pmrWebServiceResponse = new PmrWebServiceResponse(networkReply);
connect(pmrWebServiceResponse, &PmrWebServiceResponse::busy,
mPmrWebService, &PmrWebService::busy);
connect(pmrWebServiceResponse, &PmrWebServiceResponse::error,
mPmrWebService, &PmrWebService::error);
connect(pmrWebServiceResponse, &PmrWebServiceResponse::forbidden,
mPmrWebService, &PmrWebService::forbidden);
return pmrWebServiceResponse;
}
//==============================================================================
void PmrWebServiceManager::update(const QString &pPmrUrl)
{
// Delete any previous instance of our PMR authentication object
delete mPmrAuthentication;
// Create our PMR authentication object
mPmrAuthentication = new PmrAuthentication(pPmrUrl, this);
// Connect some signals
connect(mPmrAuthentication, &PmrAuthentication::linkingSucceeded,
this, &PmrWebServiceManager::authenticationSucceeded);
connect(mPmrAuthentication, &PmrAuthentication::linkingFailed,
this, &PmrWebServiceManager::authenticationFailed);
connect(mPmrAuthentication, &PmrAuthentication::openBrowser,
this, &PmrWebServiceManager::openBrowser);
connect(mPmrAuthentication, &PmrAuthentication::closeBrowser,
this, &PmrWebServiceManager::startCloseBrowser);
}
//==============================================================================
} // namespace PMRSupport
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================