aboutsummaryrefslogtreecommitdiff
path: root/src/webwindow.cpp
blob: 5f69b0910ef0e5048035fd3c3b5777ca47801b07 (plain)
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
#include <QtGui/QCloseEvent>
#include <QtGui/QDesktopServices>
#include <QtWebEngineCore/QWebEngineCookieStore>
#include <QtWebEngineCore/QWebEngineNewWindowRequest>
#include <QtWidgets/QApplication>
#include <QtCore/QProcess>
#include <filesystem>
#include <iostream>

#include "permissionmanager.hpp"
#include "webwindow.hpp"

WebWindow::WebWindow(const QString &url)
	: QMainWindow()
	, profile(QUrl(url).host())
	, page(&profile)
	, perm((profile.persistentStoragePath() + "/permissions.state").toStdString())
{
	this->web_configure();

	this->profile.setPersistentCookiesPolicy(
		QWebEngineProfile::AllowPersistentCookies);

	this->view.setPage(&this->page);
	this->view.setUrl(url);

	this->setCentralWidget(&this->view);
}

void
WebWindow::web_configure()
{
	this->page.settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled,
	                                    true);

	this->page.settings()->setAttribute(
		QWebEngineSettings::WebRTCPublicInterfacesOnly, false);

	this->page.settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
	                                    false);

	this->profile.setPushServiceEnabled(true);

	this->page.connect(&this->page,
	                   &QWebEnginePage::featurePermissionRequested,
	                   [&](const QUrl origin, QWebEnginePage::Feature feature) {
											 this->permission_requested(origin, feature);
										 });

	this->page.connect(&this->page,
	                   &QWebEnginePage::newWindowRequested,
	                   [=](QWebEngineNewWindowRequest &request) {
											 QDesktopServices::openUrl(request.requestedUrl());
										 });
}

void
WebWindow::permission_requested(const QUrl origin,
                                QWebEnginePage::Feature feature)
{
	if (this->perm.get(feature)) {
		this->page.setFeaturePermission(
			origin, feature, QWebEnginePage::PermissionGrantedByUser);
	} else {
		this->page.setFeaturePermission(
			origin, feature, QWebEnginePage::PermissionDeniedByUser);
	}
	this->page.setFeaturePermission(
		origin, feature, QWebEnginePage::PermissionUnknown);
}

void
WebWindow::closeEvent(QCloseEvent *event)
{
	this->hide();
	event->ignore();
}

void
WebWindow::connect_icon_changed(std::function<void(const QIcon)> fn)
{
	this->view.connect(&this->view, &QWebEngineView::iconChanged, fn);
}

void
WebWindow::connect_notification(
	std::function<void(std::unique_ptr<QWebEngineNotification>)> fn)
{
	this->profile.setNotificationPresenter(fn);
}

void
WebWindow::connect_title_changed(std::function<void(const QString)> fn)
{
	this->view.connect(&this->view, &QWebEngineView::titleChanged, fn);
}

PermissionManager &
WebWindow::permissions()
{
	return this->perm;
}

void
WebWindow::reset_cookies()
{
	std::filesystem::remove_all(this->profile.persistentStoragePath().toStdString());
	QProcess process;
	QStringList args = QApplication::instance()->arguments();

	if (!args.contains("--open-at-startup")) {
		args.append("--open-at-startup");
	}

	process.startDetached(args.front(), args);
	this->quit();
}

void
WebWindow::toggle_visibility()
{
	this->setVisible(!this->isVisible());
}

void
WebWindow::quit()
{
	QApplication::instance()->quit();
}