Falkon Develop
Cross-platform Qt-based web browser
html5permissionsnotification.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "ui_html5permissionsnotification.h"
21#include "mainapplication.h"
22#include "iconprovider.h"
23
24#include <QTimer>
25#include <QWebEnginePage>
26#include <QWebEngineView>
27#include <QtWebEngineWidgetsVersion>
28
29
30HTML5PermissionsNotification::HTML5PermissionsNotification(const QUrl &origin, QWebEnginePage* page, const QWebEnginePage::Feature &feature)
31 : AnimatedWidget(AnimatedWidget::Down, 300, nullptr)
33 , m_origin(origin)
34 , m_page(page)
35 , m_feature(feature)
36{
37 setAutoFillBackground(true);
38 ui->setupUi(widget());
39
40 ui->close->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton));
41
42 const QString site = m_origin.host().isEmpty() ? tr("this site") : QSL("<b>%1</b>").arg(m_origin.host());
43
44 switch (feature) {
45 case QWebEnginePage::Notifications:
46 ui->textLabel->setText(tr("Allow %1 to show desktop notifications?").arg(site));
47 break;
48
49 case QWebEnginePage::Geolocation:
50 ui->textLabel->setText(tr("Allow %1 to locate your position?").arg(site));
51 break;
52
53 case QWebEnginePage::MediaAudioCapture:
54 ui->textLabel->setText(tr("Allow %1 to use your microphone?").arg(site));
55 break;
56
57 case QWebEnginePage::MediaVideoCapture:
58 ui->textLabel->setText(tr("Allow %1 to use your camera?").arg(site));
59 break;
60
61 case QWebEnginePage::MediaAudioVideoCapture:
62 ui->textLabel->setText(tr("Allow %1 to use your microphone and camera?").arg(site));
63 break;
64
65 case QWebEnginePage::MouseLock:
66 ui->textLabel->setText(tr("Allow %1 to hide your pointer?").arg(site));
67 break;
68
69 case QWebEnginePage::DesktopVideoCapture:
70 ui->textLabel->setText(tr("Allow %1 to capture your screen?").arg(site));
71 break;
72
73 case QWebEnginePage::DesktopAudioVideoCapture:
74 ui->textLabel->setText(tr("Allow %1 to capture your screen and audio?").arg(site));
75 break;
76 default:
77 qWarning() << "Unknown feature" << feature;
78 break;
79 }
80
81 connect(ui->allow, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::grantPermissions);
82 connect(ui->deny, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::denyPermissions);
83 connect(ui->close, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::denyPermissions);
84
85 connect(m_page.data(), &QWebEnginePage::loadStarted, this, &QObject::deleteLater);
86 connect(m_page.data(), &QWebEnginePage::featurePermissionRequestCanceled, this, [this](const QUrl &origin, QWebEnginePage::Feature feature) {
87 if (origin == m_origin && feature == m_feature) {
88 deleteLater();
89 }
90 });
91
92 startAnimation();
93}
94
95void HTML5PermissionsNotification::grantPermissions()
96{
97 if (!m_page) {
98 return;
99 }
100
101 QTimer::singleShot(0, this, [this]() {
102 // We need to have cursor inside view to correctly grab mouse
103 if (m_feature == QWebEnginePage::MouseLock) {
104 QWidget *view = QWebEngineView::forPage(m_page);
105 QCursor::setPos(view->mapToGlobal(view->rect().center()));
106 }
107
108 m_page->setFeaturePermission(m_origin, m_feature, QWebEnginePage::PermissionGrantedByUser);
109 });
110
111 if (ui->remember->isChecked()) {
112 mApp->html5PermissionsManager()->rememberPermissions(m_origin, m_feature, QWebEnginePage::PermissionGrantedByUser);
113 }
114
115 hide();
116}
117
118void HTML5PermissionsNotification::denyPermissions()
119{
120 if (!m_page) {
121 return;
122 }
123
124 m_page->setFeaturePermission(m_origin, m_feature, QWebEnginePage::PermissionDeniedByUser);
125
126 if (ui->remember->isChecked()) {
127 mApp->html5PermissionsManager()->rememberPermissions(m_origin, m_feature, QWebEnginePage::PermissionDeniedByUser);
128 }
129
130 hide();
131}
132
134{
135 delete ui;
136}
QWidget * widget()
HTML5PermissionsNotification(const QUrl &origin, QWebEnginePage *page, const QWebEnginePage::Feature &feature)
static QIcon standardIcon(QStyle::StandardPixmap icon)
#define mApp
#define QSL(x)
Definition: qzcommon.h:40