File indexing completed on 2024-12-22 04:41:23

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "html5permissionsdialog.h"
0019 #include "ui_html5permissionsdialog.h"
0020 #include "settings.h"
0021 #include "mainapplication.h"
0022 #include "html5permissionsmanager.h"
0023 
0024 #include <QtWebEngineWidgetsVersion>
0025 
0026 
0027 HTML5PermissionsDialog::HTML5PermissionsDialog(QWidget* parent)
0028     : QDialog(parent)
0029     , ui(new Ui::HTML5PermissionsDialog)
0030 {
0031     setAttribute(Qt::WA_DeleteOnClose);
0032 
0033     ui->setupUi(this);
0034 
0035     loadSettings();
0036 
0037     ui->treeWidget->header()->resizeSection(0, 220);
0038 
0039     connect(ui->remove, &QPushButton::clicked, this, &HTML5PermissionsDialog::removeEntry);
0040     connect(ui->feature, SIGNAL(currentIndexChanged(int)), this, SLOT(featureIndexChanged()));
0041     connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &HTML5PermissionsDialog::saveSettings);
0042 
0043     showFeaturePermissions(currentFeature());
0044 }
0045 
0046 HTML5PermissionsDialog::~HTML5PermissionsDialog()
0047 {
0048     delete ui;
0049 }
0050 
0051 void HTML5PermissionsDialog::showFeaturePermissions(QWebEnginePage::Feature feature)
0052 {
0053     if (!m_granted.contains(feature) || !m_denied.contains(feature)) {
0054         return;
0055     }
0056 
0057     ui->treeWidget->clear();
0058 
0059     const auto grantedSites = m_granted.value(feature);
0060     for (const QString &site : grantedSites) {
0061         auto* item = new QTreeWidgetItem(ui->treeWidget);
0062         item->setText(0, site);
0063         item->setText(1, tr("Allow"));
0064         item->setData(0, Qt::UserRole + 10, Allow);
0065         ui->treeWidget->addTopLevelItem(item);
0066     }
0067 
0068     const auto deniedSites = m_denied.value(feature);
0069     for (const QString &site : deniedSites) {
0070         auto* item = new QTreeWidgetItem(ui->treeWidget);
0071         item->setText(0, site);
0072         item->setText(1, tr("Deny"));
0073         item->setData(0, Qt::UserRole + 10, Deny);
0074         ui->treeWidget->addTopLevelItem(item);
0075     }
0076 }
0077 
0078 void HTML5PermissionsDialog::featureIndexChanged()
0079 {
0080     showFeaturePermissions(currentFeature());
0081 }
0082 
0083 void HTML5PermissionsDialog::removeEntry()
0084 {
0085     QTreeWidgetItem* item = ui->treeWidget->currentItem();
0086     if (!item) {
0087         return;
0088     }
0089 
0090     Role role = static_cast<Role>(item->data(0, Qt::UserRole + 10).toInt());
0091     const QString origin = item->text(0);
0092 
0093     if (role == Allow)
0094         m_granted[currentFeature()].removeOne(origin);
0095     else
0096         m_denied[currentFeature()].removeOne(origin);
0097 
0098     delete item;
0099 }
0100 
0101 QWebEnginePage::Feature HTML5PermissionsDialog::currentFeature() const
0102 {
0103     switch (ui->feature->currentIndex()) {
0104     case 0:
0105         return QWebEnginePage::Notifications;
0106     case 1:
0107         return QWebEnginePage::Geolocation;
0108     case 2:
0109         return QWebEnginePage::MediaAudioCapture;
0110     case 3:
0111         return QWebEnginePage::MediaVideoCapture;
0112     case 4:
0113         return QWebEnginePage::MediaAudioVideoCapture;
0114     case 5:
0115         return QWebEnginePage::MouseLock;
0116     case 6:
0117         return QWebEnginePage::DesktopVideoCapture;
0118     case 7:
0119         return QWebEnginePage::DesktopAudioVideoCapture;
0120     default:
0121         Q_UNREACHABLE();
0122         return QWebEnginePage::Notifications;
0123     }
0124 }
0125 
0126 void HTML5PermissionsDialog::loadSettings()
0127 {
0128     Settings settings;
0129     settings.beginGroup(QSL("HTML5Notifications"));
0130 
0131     m_granted[QWebEnginePage::Notifications] = settings.value(QSL("NotificationsGranted"), QStringList()).toStringList();
0132     m_denied[QWebEnginePage::Notifications] = settings.value(QSL("NotificationsDenied"), QStringList()).toStringList();
0133 
0134     m_granted[QWebEnginePage::Geolocation] = settings.value(QSL("GeolocationGranted"), QStringList()).toStringList();
0135     m_denied[QWebEnginePage::Geolocation] = settings.value(QSL("GeolocationDenied"), QStringList()).toStringList();
0136 
0137     m_granted[QWebEnginePage::MediaAudioCapture] = settings.value(QSL("MediaAudioCaptureGranted"), QStringList()).toStringList();
0138     m_denied[QWebEnginePage::MediaAudioCapture] = settings.value(QSL("MediaAudioCaptureDenied"), QStringList()).toStringList();
0139 
0140     m_granted[QWebEnginePage::MediaVideoCapture] = settings.value(QSL("MediaVideoCaptureGranted"), QStringList()).toStringList();
0141     m_denied[QWebEnginePage::MediaVideoCapture] = settings.value(QSL("MediaVideoCaptureDenied"), QStringList()).toStringList();
0142 
0143     m_granted[QWebEnginePage::MediaAudioVideoCapture] = settings.value(QSL("MediaAudioVideoCaptureGranted"), QStringList()).toStringList();
0144     m_denied[QWebEnginePage::MediaAudioVideoCapture] = settings.value(QSL("MediaAudioVideoCaptureDenied"), QStringList()).toStringList();
0145 
0146     m_granted[QWebEnginePage::MouseLock] = settings.value(QSL("MouseLockGranted"), QStringList()).toStringList();
0147     m_denied[QWebEnginePage::MouseLock] = settings.value(QSL("MouseLockDenied"), QStringList()).toStringList();
0148 
0149     m_granted[QWebEnginePage::DesktopVideoCapture] = settings.value(QSL("DesktopVideoCaptureGranted"), QStringList()).toStringList();
0150     m_denied[QWebEnginePage::DesktopVideoCapture] = settings.value(QSL("DesktopVideoCaptureDenied"), QStringList()).toStringList();
0151 
0152     m_granted[QWebEnginePage::DesktopAudioVideoCapture] = settings.value(QSL("DesktopAudioVideoCaptureGranted"), QStringList()).toStringList();
0153     m_denied[QWebEnginePage::DesktopAudioVideoCapture] = settings.value(QSL("DesktopAudioVideoCaptureDenied"), QStringList()).toStringList();
0154 
0155     settings.endGroup();
0156 }
0157 
0158 void HTML5PermissionsDialog::saveSettings()
0159 {
0160     Settings settings;
0161     settings.beginGroup(QSL("HTML5Notifications"));
0162 
0163     settings.setValue(QSL("NotificationsGranted"), m_granted[QWebEnginePage::Notifications]);
0164     settings.setValue(QSL("NotificationsDenied"), m_denied[QWebEnginePage::Notifications]);
0165 
0166     settings.setValue(QSL("GeolocationGranted"), m_granted[QWebEnginePage::Geolocation]);
0167     settings.setValue(QSL("GeolocationDenied"), m_denied[QWebEnginePage::Geolocation]);
0168 
0169     settings.setValue(QSL("MediaAudioCaptureGranted"), m_granted[QWebEnginePage::MediaAudioCapture]);
0170     settings.setValue(QSL("MediaAudioCaptureDenied"), m_denied[QWebEnginePage::MediaAudioCapture]);
0171 
0172     settings.setValue(QSL("MediaVideoCaptureGranted"), m_granted[QWebEnginePage::MediaVideoCapture]);
0173     settings.setValue(QSL("MediaVideoCaptureDenied"), m_denied[QWebEnginePage::MediaVideoCapture]);
0174 
0175     settings.setValue(QSL("MediaAudioVideoCaptureGranted"), m_granted[QWebEnginePage::MediaAudioVideoCapture]);
0176     settings.setValue(QSL("MediaAudioVideoCaptureDenied"), m_denied[QWebEnginePage::MediaAudioVideoCapture]);
0177 
0178     settings.setValue(QSL("MouseLockGranted"), m_granted[QWebEnginePage::MouseLock]);
0179     settings.setValue(QSL("MouseLockDenied"), m_denied[QWebEnginePage::MouseLock]);
0180 
0181     settings.setValue(QSL("DesktopVideoCaptureGranted"), m_granted[QWebEnginePage::DesktopVideoCapture]);
0182     settings.setValue(QSL("DesktopVideoCaptureDenied"), m_denied[QWebEnginePage::DesktopVideoCapture]);
0183 
0184     settings.setValue(QSL("DesktopAudioVideoCaptureGranted"), m_granted[QWebEnginePage::DesktopAudioVideoCapture]);
0185     settings.setValue(QSL("DesktopAudioVideoCaptureDenied"), m_denied[QWebEnginePage::DesktopAudioVideoCapture]);
0186 
0187     settings.endGroup();
0188 
0189     mApp->html5PermissionsManager()->loadSettings();
0190 }