From 653e7aa429e5930672e3ddea3a4dab8becdb55dc Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 24 Sep 2022 02:03:19 +0200 Subject: [PATCH] Rewrite permissions in SiteInfo window Signed-off-by: Juraj Oravec --- src/lib/other/siteinfo.cpp | 48 +++++------- src/lib/other/siteinfo.h | 6 +- src/lib/other/siteinfopermissionitem.cpp | 98 +++++++++--------------- src/lib/other/siteinfopermissionitem.h | 24 +++--- src/lib/other/sitesettingsmanager.cpp | 53 ++++++++++++- src/lib/other/sitesettingsmanager.h | 8 +- 6 files changed, 124 insertions(+), 113 deletions(-) diff --git a/src/lib/other/siteinfo.cpp b/src/lib/other/siteinfo.cpp index cd59c3493..531f1c147 100644 --- a/src/lib/other/siteinfo.cpp +++ b/src/lib/other/siteinfo.cpp @@ -28,7 +28,6 @@ #include "scripts.h" #include "networkmanager.h" #include "locationbar.h" -#include "siteinfopermissionitem.h" #include #include @@ -124,15 +123,8 @@ SiteInfo::SiteInfo(WebView *view) } }); - // Permissions - for (auto &attribute : mApp->siteSettingsManager()->getSupportedAttribute()) { - addPermissionOption(attribute); - } - for (auto &feature : mApp->siteSettingsManager()->getSupportedFeatures()) { - addPermissionOption(feature); - } - - + /* Permissions */ + addSiteSettings(); connect(ui->saveButton, SIGNAL(clicked(QAbstractButton*)), this, SLOT(saveImage())); connect(ui->listWidget, SIGNAL(currentRowChanged(int)), ui->stackedWidget, SLOT(setCurrentIndex(int))); @@ -337,34 +329,30 @@ SiteInfo::~SiteInfo() delete m_certWidget; } -void SiteInfo::addPermissionOption(const SiteSettingsManager::PageOptions option) +SiteInfoPermissionItem* SiteInfo::addPermissionOption(SiteSettingsManager::Permission perm) { - auto perm = mApp->siteSettingsManager()->getPermission(option, m_baseUrl); auto* listItem = new QListWidgetItem(ui->listPermissions); - auto* optionItem = new SiteInfoPermissionItem(option, perm, this); + auto* optionItem = new SiteInfoPermissionItem(perm, this); ui->listPermissions->setItemWidget(listItem, optionItem); listItem->setSizeHint(optionItem->sizeHint()); + + return optionItem; } -void SiteInfo::addPermissionOption(const QWebEnginePage::Feature feature) +void SiteInfo::addSiteSettings() { - auto perm = mApp->siteSettingsManager()->getPermission(feature, m_baseUrl); - auto* listItem = new QListWidgetItem(ui->listPermissions); - auto* optionItem = new SiteInfoPermissionItem(feature, perm, this); - - ui->listPermissions->setItemWidget(listItem, optionItem); - listItem->setSizeHint(optionItem->sizeHint()); -} - -void SiteInfo::addPermissionOption(const QWebEngineSettings::WebAttribute attribute) -{ - auto perm = mApp->siteSettingsManager()->getPermission(attribute, m_baseUrl); - auto* listItem = new QListWidgetItem(ui->listPermissions); - auto* optionItem = new SiteInfoPermissionItem(attribute, perm, this); - - ui->listPermissions->setItemWidget(listItem, optionItem); - listItem->setSizeHint(optionItem->sizeHint()); + auto siteSettings = mApp->siteSettingsManager()->getSiteSettings(m_baseUrl); + // Attributes + for (const auto &attribute : mApp->siteSettingsManager()->getSupportedAttribute()) { + SiteInfoPermissionItem *item = addPermissionOption(siteSettings.attributes[attribute]); + item->setAttribute(attribute); + } + // Permissions + for (const auto &feature : mApp->siteSettingsManager()->getSupportedFeatures()) { + SiteInfoPermissionItem *item = addPermissionOption(siteSettings.features[feature]); + item->setFeature(feature); + } } void SiteInfo::saveSiteSettings() diff --git a/src/lib/other/siteinfo.h b/src/lib/other/siteinfo.h index 9e1157d28..9ec1b0afd 100644 --- a/src/lib/other/siteinfo.h +++ b/src/lib/other/siteinfo.h @@ -20,6 +20,7 @@ #include "qzcommon.h" #include "sitesettingsmanager.h" +#include "siteinfopermissionitem.h" #include #include @@ -58,9 +59,8 @@ private: void showLoadingText(); void showPixmap(QPixmap pixmap); - void addPermissionOption(const SiteSettingsManager::PageOptions option); - void addPermissionOption(const QWebEnginePage::Feature feature); - void addPermissionOption(const QWebEngineSettings::WebAttribute attribute); + void addSiteSettings(); + SiteInfoPermissionItem* addPermissionOption(SiteSettingsManager::Permission perm); Ui::SiteInfo* ui; CertificateInfoWidget* m_certWidget; diff --git a/src/lib/other/siteinfopermissionitem.cpp b/src/lib/other/siteinfopermissionitem.cpp index 1b1c4b62b..0ca384ce3 100644 --- a/src/lib/other/siteinfopermissionitem.cpp +++ b/src/lib/other/siteinfopermissionitem.cpp @@ -22,42 +22,14 @@ #include "sitesettingsmanager.h" -SiteInfoPermissionItem::SiteInfoPermissionItem(const SiteSettingsManager::PageOptions &a_option, const SiteSettingsManager::Permission &a_permission, QWidget* parent) +SiteInfoPermissionItem::SiteInfoPermissionItem(const SiteSettingsManager::Permission& a_permission, QWidget* parent) : QWidget(parent) -, m_option(a_option) -, m_type(Browser) , m_ui(new Ui::SiteInfoPermissionItem()) +, m_hasOptionAsk(true) +, m_hasOptionDefault(true) { m_ui->setupUi(this); - setPermission(a_permission); - setHasOptionAsk(false); - m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(a_option)); -} - -SiteInfoPermissionItem::SiteInfoPermissionItem(const QWebEnginePage::Feature& a_feature, const SiteSettingsManager::Permission& a_permission, QWidget* parent) -: QWidget(parent) -, m_feature(a_feature) -, m_type(Feature) -, m_ui(new Ui::SiteInfoPermissionItem()) -{ - m_ui->setupUi(this); - - setPermission(a_permission); - m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(a_feature)); -} - -SiteInfoPermissionItem::SiteInfoPermissionItem(const QWebEngineSettings::WebAttribute& a_attribute, const SiteSettingsManager::Permission& a_permission, QWidget* parent) -: QWidget(parent) -, m_attribute(a_attribute) -, m_type(Attribute) -, m_ui(new Ui::SiteInfoPermissionItem()) -{ - m_ui->setupUi(this); - - setPermission(a_permission); - setHasOptionAsk(false); - m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(a_attribute)); } SiteInfoPermissionItem::~SiteInfoPermissionItem() @@ -94,7 +66,7 @@ void SiteInfoPermissionItem::setHasOptionDefault(bool hasOptionDefault) m_ui->radioDefault->setVisible(hasOptionDefault); } -void SiteInfoPermissionItem::setPermission(SiteSettingsManager::Permission permission) +void SiteInfoPermissionItem::setPermission(const SiteSettingsManager::Permission permission) { m_ui->radioAllow->setChecked(false); m_ui->radioAsk->setChecked(false); @@ -118,26 +90,6 @@ void SiteInfoPermissionItem::setPermission(SiteSettingsManager::Permission permi qWarning() << "Unknown permission" << permission; m_ui->radioDefault->setChecked(true); } - - SiteSettingsManager::Permission defaultPermission; - switch (m_type) { - case Browser: - defaultPermission = mApp->siteSettingsManager()->getDefaultPermission(m_option); - break; - case Attribute: - defaultPermission = mApp->siteSettingsManager()->getDefaultPermission(m_attribute); - break; - case Feature: - defaultPermission = mApp->siteSettingsManager()->getDefaultPermission(m_feature); - break; - default: - defaultPermission = SiteSettingsManager::Default; - break; - } - if (defaultPermission == SiteSettingsManager::Default) { - defaultPermission = SiteSettingsManager::Ask; - } - m_ui->labelDefaultPermission->setText(mApp->siteSettingsManager()->getPermissionName(defaultPermission)); } SiteSettingsManager::Permission SiteInfoPermissionItem::permission() const @@ -162,14 +114,36 @@ SiteSettingsManager::Permission SiteInfoPermissionItem::permission() const QString SiteInfoPermissionItem::sqlColumn() { - switch (m_type) { - case Browser: - return mApp->siteSettingsManager()->optionToSqlColumn(m_option); - case Attribute: - return mApp->siteSettingsManager()->webAttributeToSqlColumn(m_attribute); - case Feature: - return mApp->siteSettingsManager()->featureToSqlColumn(m_feature); - default: - return QL1S(); - } + return m_sqlColumn; +} + +void SiteInfoPermissionItem::setDefaultPermission(SiteSettingsManager::Permission permission) +{ + if (permission == SiteSettingsManager::Default) { + permission = SiteSettingsManager::Ask; + } + m_ui->labelDefaultPermission->setText(mApp->siteSettingsManager()->getPermissionName(permission)); +} + +void SiteInfoPermissionItem::setAttribute(const QWebEngineSettings::WebAttribute &attribute) +{ + m_sqlColumn = mApp->siteSettingsManager()->webAttributeToSqlColumn(attribute); + m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(attribute)); + setDefaultPermission(mApp->siteSettingsManager()->getDefaultPermission(attribute)); + setHasOptionAsk(false); +} + +void SiteInfoPermissionItem::setFeature(const QWebEnginePage::Feature& feature) +{ + m_sqlColumn = mApp->siteSettingsManager()->featureToSqlColumn(feature); + m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(feature)); + setDefaultPermission(mApp->siteSettingsManager()->getDefaultPermission(feature)); +} + +void SiteInfoPermissionItem::setOption(const SiteSettingsManager::PageOptions& option) +{ + m_sqlColumn = mApp->siteSettingsManager()->optionToSqlColumn(option); + m_ui->label->setText(mApp->siteSettingsManager()->getOptionName(option)); + setDefaultPermission(mApp->siteSettingsManager()->getDefaultPermission(option)); + setHasOptionAsk(false); } diff --git a/src/lib/other/siteinfopermissionitem.h b/src/lib/other/siteinfopermissionitem.h index 95d215db5..92f55348e 100644 --- a/src/lib/other/siteinfopermissionitem.h +++ b/src/lib/other/siteinfopermissionitem.h @@ -36,16 +36,7 @@ class SiteInfoPermissionItem : public QWidget Q_OBJECT public: - enum Type { - Browser = 0, - Feature = 1, - Attribute = 2, - }; - Q_ENUM(Type); - - explicit SiteInfoPermissionItem(const SiteSettingsManager::PageOptions &a_option, const SiteSettingsManager::Permission &a_permission, QWidget* parent = nullptr); - explicit SiteInfoPermissionItem(const QWebEngineSettings::WebAttribute &a_attribute, const SiteSettingsManager::Permission &a_permission, QWidget* parent = nullptr); - explicit SiteInfoPermissionItem(const QWebEnginePage::Feature &a_feature, const SiteSettingsManager::Permission &a_permission, QWidget* parent = nullptr); + explicit SiteInfoPermissionItem(const SiteSettingsManager::Permission &a_permission, QWidget* parent = nullptr); ~SiteInfoPermissionItem(); bool hasOptionAsk() const; @@ -54,17 +45,20 @@ public: SiteSettingsManager::Permission permission() const; QString sqlColumn(); + void setAttribute(const QWebEngineSettings::WebAttribute& attribute); + void setFeature(const QWebEnginePage::Feature &feature); + void setOption(const SiteSettingsManager::PageOptions &option); + void setHasOptionAsk(bool hasAsk); void setHasOptionDefault(bool hasDefault); - void setPermission(SiteSettingsManager::Permission permission); private: + void setPermission(const SiteSettingsManager::Permission permission); + void setDefaultPermission(SiteSettingsManager::Permission permission); + bool m_hasOptionAsk; bool m_hasOptionDefault; - SiteSettingsManager::PageOptions m_option; - QWebEngineSettings::WebAttribute m_attribute; - QWebEnginePage::Feature m_feature; - Type m_type; + QString m_sqlColumn; private: QScopedPointer m_ui; diff --git a/src/lib/other/sitesettingsmanager.cpp b/src/lib/other/sitesettingsmanager.cpp index 97eb697f6..e8dbeae4f 100644 --- a/src/lib/other/sitesettingsmanager.cpp +++ b/src/lib/other/sitesettingsmanager.cpp @@ -62,6 +62,29 @@ SiteSettingsManager::SiteSettingsManager ( QObject* parent ) supportedFeatures.append(QWebEnginePage::MouseLock); supportedFeatures.append(QWebEnginePage::DesktopVideoCapture); supportedFeatures.append(QWebEnginePage::DesktopAudioVideoCapture); + + + everythingSql = QSL("SELECT "); + + for (int i = 0; i < supportedAttribute.size(); ++i) { + if (i > 0) { + everythingSql.append(QSL(", ")); + } + everythingSql.append(webAttributeToSqlColumn(supportedAttribute[i])); + } + + for (int i = 0; i < supportedFeatures.size(); ++i) { + everythingSql.append(QSL(", ")); + everythingSql.append(featureToSqlColumn(supportedFeatures[i])); + } + + everythingSql.append(QSL(", ")); + everythingSql.append(optionToSqlColumn(poAllowCookies)); + + everythingSql.append(QSL(", ")); + everythingSql.append(optionToSqlColumn(poZoomLevel)); + + everythingSql.append(QSL(" FROM site_settings WHERE server=?")); } SiteSettingsManager::~SiteSettingsManager() noexcept @@ -99,7 +122,7 @@ void SiteSettingsManager::loadSettings() settings.beginGroup("Site-Settings-Default-Features"); - for (auto &feature : qAsConst(supportedFeatures)) { + for (const auto &feature : qAsConst(supportedFeatures)) { defaultFeatures[feature] = intToPermission(settings.value(featureToSqlColumn(feature), Ask).toInt()); } settings.endGroup(); @@ -502,3 +525,31 @@ QList SiteSettingsManager::getSupportedFeatures() const { return supportedFeatures; } + +SiteSettingsManager::SiteSettings SiteSettingsManager::getSiteSettings(QUrl& url) +{ + SiteSettings siteSettings; + int i; + + QSqlQuery query(SqlDatabase::instance()->database()); + query.prepare(everythingSql); + query.addBindValue(url.host()); + query.exec(); + + if (query.next()) { + Permission perm; + for (i = 0; i < supportedAttribute.size(); ++i) { + perm = intToPermission(query.value(i).toInt()); + siteSettings.attributes[supportedAttribute[i]] = perm; + } + for (i = 0; i < supportedFeatures.size(); ++i) { + perm = intToPermission(query.value(i + supportedAttribute.size()).toInt()); + siteSettings.features[supportedFeatures[i]] = perm; + } + perm = intToPermission(query.value(supportedAttribute.size() + supportedFeatures.size()).toInt()); + siteSettings.AllowCookies = perm; + siteSettings.ZoomLevel = query.value(supportedAttribute.size() + supportedFeatures.size() + 1).toInt(); + } + + return siteSettings; +} diff --git a/src/lib/other/sitesettingsmanager.h b/src/lib/other/sitesettingsmanager.h index 7b4ce4950..52a9cd596 100644 --- a/src/lib/other/sitesettingsmanager.h +++ b/src/lib/other/sitesettingsmanager.h @@ -49,8 +49,8 @@ public: { Permission AllowCookies; int ZoomLevel; - QHash attributes; /* Enable disable soem feature eg. Javascript, Images etc */ - QHash features; /* HTML permissions */ + QMap attributes; /* Enable disable soem feature eg. Javascript, Images etc */ + QMap features; /* HTML permissions */ QString server; }; @@ -95,6 +95,8 @@ public: QList getSupportedAttribute() const; QList getSupportedFeatures() const; + SiteSettings getSiteSettings(QUrl &url); + private: Permission testAttribute(const QWebEngineSettings::WebAttribute attribute) const; @@ -104,7 +106,9 @@ private: QList supportedAttribute; QList supportedFeatures; QMap defaultFeatures; + QString attributesSql; + QString everythingSql; };