aboutsummaryrefslogtreecommitdiff
path: root/src/webwindow.cpp
blob: d7c25f9c93f3d0b8b8110876ab7a32f70f643685 (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
#include <QtGui/QCloseEvent>
#include <QtWebEngineCore/QWebEngineCookieStore>

#include "permissionmanager.hpp"
#include "webwindow.hpp"
#include <QtWidgets/QApplication>
#include <iostream>

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::ForcePersistentCookies);

	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);
										 });
}

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()
{
	this->profile.cookieStore()->deleteAllCookies();
	this->profile.cookieStore()->loadAllCookies();
	this->profile.clearHttpCache();
	this->profile.clearAllVisitedLinks();
	this->profile.clientCertificateStore();
	this->view.reload();
}

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

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