-
Notifications
You must be signed in to change notification settings - Fork 0
/
remitview.cpp
72 lines (58 loc) · 2.12 KB
/
remitview.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
#include "remitview.h"
#include "constants.h"
#include "settings.h"
#include <QWebEngineNavigationRequest>
#include <QWebEngineNewWindowRequest>
RemitView::RemitView(QWidget* parent):
QWebEngineView(Constants::webProfile(), parent),
loggingIn(false)
{
connect(page(), &QWebEnginePage::navigationRequested, this, &RemitView::handleNavigationRequest);
connect(page(), &QWebEnginePage::newWindowRequested, this, &RemitView::handleNewWindowRequest);
page()->load((Settings::remitUrl()));
}
bool routeMatches(const QUrl& a, const QUrl& b) {
static auto options = QUrl::RemoveQuery |
QUrl::RemoveFragment |
QUrl::RemoveUserInfo |
QUrl::StripTrailingSlash;
return a.matches(b, options);
}
bool RemitView::handleUrlAndEmitSignals(const QUrl& url) {
if (loggingIn) {
// During login, we can't redirect to a different profile or a different browser, so:
// - don't fire navigation-events while we're logging in.
// - accept all navigation requests, open them in this window
auto oauthRedirectUrl = Settings::remitBaseUrl();
oauthRedirectUrl.setPath("/auth");
if (routeMatches(url, oauthRedirectUrl)) {
// We're done with logging in after this
loggingIn = false;
}
return true;
}
if (url.host() == Settings::githubUrl().host()) {
emit githubNavigationRequested(url);
return false;
}
if (url.host() != Settings::remitBaseUrl().host()) {
emit externalNavigationRequested(url);
return false;
}
auto loginUrl = Settings::remitBaseUrl();
loginUrl.setPath("/login");
if (routeMatches(url, loginUrl)) {
loggingIn = true;
}
return true;
}
void RemitView::handleNavigationRequest(QWebEngineNavigationRequest& request) {
if (handleUrlAndEmitSignals(request.url()))
request.accept();
else
request.reject();
}
void RemitView::handleNewWindowRequest(QWebEngineNewWindowRequest& request) {
if (handleUrlAndEmitSignals(request.requestedUrl()))
request.openIn(page());
}