forked from gm-vm/openfortivpn-webview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
134 lines (113 loc) · 4.02 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QLoggingCategory>
#include <QMenuBar>
#include <QStandardPaths>
#include <QTextStream>
#include <QWebEngineCookieStore>
#include <QWebEngineHistory>
#include <QWebEngineProfile>
#include <QWebEngineView>
#include <iostream>
Q_LOGGING_CATEGORY(category, "webview")
MainWindow::MainWindow(const bool keepOpen,
const QRegularExpression& urlToWaitForRegex,
QWidget *parent) :
QMainWindow(parent),
webEngineProfile(new QWebEngineProfile("vpn", parent)),
webEngine(new QWebEngineView(webEngineProfile, parent)),
urlToWaitForRegex(urlToWaitForRegex),
keepOpen(keepOpen)
{
setCentralWidget(webEngine);
createMenuBar();
QString appDataLocation = QStandardPaths::locate(QStandardPaths::AppDataLocation,
QString(),
QStandardPaths::LocateDirectory);
QWebEngineProfile *webEngineProfile = webEngine->page()->profile();
webEngineProfile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
webEngineProfile->setCachePath(appDataLocation);
webEngineProfile->setPersistentStoragePath(appDataLocation);
connect(webEngine, &QWebEngineView::titleChanged, this, &MainWindow::updateTitle);
connect(webEngine, &QWebEngineView::urlChanged, this, &MainWindow::handleUrlChange);
connect(webEngineProfile->cookieStore(), &QWebEngineCookieStore::cookieAdded, this,
&MainWindow::onCookieAdded);
connect(webEngineProfile->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this,
&MainWindow::onCookieRemoved);
}
MainWindow::~MainWindow()
{
delete webEngine;
delete webEngineProfile;
}
void MainWindow::loadUrl(const QString &url)
{
webEngine->setUrl(url);
}
void MainWindow::onCookieAdded(const QNetworkCookie &cookie)
{
if (cookie.name() == "SVPNCOOKIE") {
svpncookie = QString(cookie.name()) + "=" + QString(cookie.value());
qCDebug(category) << "SVPNCOOKIE has been received";
// This should maybe also check that the cookie is not empty.
if (didSeeUrlToWaitFor) {
std::cout << svpncookie.toStdString() << std::endl;
if (!keepOpen) {
QApplication::exit(0);
}
}
}
}
void MainWindow::onCookieRemoved(const QNetworkCookie &cookie)
{
if (cookie.name() == "SVPNCOOKIE") {
qCDebug(category) << "SVPNCOOKIE has been removed";
svpncookie = QString();
}
}
void MainWindow::handleUrlChange(const QUrl &url)
{
qCDebug(category) << url.toString();
if (didSeeUrlToWaitFor) return;
if (urlToWaitForRegex.match(url.toString()).hasMatch()) {
qCDebug(category) << "The current URL matches the given regex";
didSeeUrlToWaitFor = true;
bool hasCookieSet = !svpncookie.isEmpty();
if (hasCookieSet) {
std::cout << svpncookie.toStdString() << std::endl;
}
if (!keepOpen) {
QApplication::exit(hasCookieSet ? 0 : 1);
}
}
}
void MainWindow::updateTitle(const QString &title)
{
setWindowTitle(title);
}
void MainWindow::createMenuBar()
{
QAction *reload = new QAction(tr("&Reload"), this);
connect(reload, &QAction::triggered, this,
[this]() {
webEngine->reload();
});
QAction *clearData = new QAction(tr("&Clear data"), this);
connect(clearData, &QAction::triggered, this,
[this]() {
auto profile = webEngine->page()->profile();
profile->clearHttpCache();
profile->clearAllVisitedLinks();
profile->cookieStore()->deleteAllCookies();
webEngine->history()->clear();
});
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(reload);
fileMenu->addSeparator();
fileMenu->addAction(clearData);
}
void MainWindow::closeEvent(QCloseEvent *)
{
QApplication::exit(keepOpen ? 0 : 1);
}