From d0b9510b73bcc083c75dc427b67d9b69c1dfb410 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sun, 7 Aug 2022 00:34:11 +0200 Subject: [PATCH] Rewrite JS and IMGs to use SiteSettingManager - The set* functions needs a redesign - There is a chance that the database update will take longer than the time it takes to start reloading, need to stress test or redesign. - Might still need to look ar StatusBarIcons plugin for optimizations and potential changes - I am still unsure how to store data in the database, so far I use numbers which is good I believe since I want to have 3 states, (default, allow, deny) - Might need to add overloaded functions with bool arguments to make the rest of the code simpler. PS: I believe it is fine to have a bit more complicated logic in backend while the frontend will stay simple. Signed-off-by: Juraj Oravec --- src/lib/other/sitesettingsmanager.cpp | 36 +++++++++++++++++-- src/lib/other/sitesettingsmanager.h | 2 ++ src/lib/webengine/webpage.cpp | 6 ++-- src/plugins/StatusBarIcons/sbi_imagesicon.cpp | 12 +++++-- .../StatusBarIcons/sbi_javascripticon.cpp | 13 +++---- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/lib/other/sitesettingsmanager.cpp b/src/lib/other/sitesettingsmanager.cpp index e1532700a..9adaee5b5 100644 --- a/src/lib/other/sitesettingsmanager.cpp +++ b/src/lib/other/sitesettingsmanager.cpp @@ -46,8 +46,8 @@ SiteWebEngineSettings SiteSettingsManager::getWebEngineSettings(const QUrl& url) SiteWebEngineSettings settings; QSqlQuery query(SqlDatabase::instance()->database()); - query.prepare("SELECT allow_images, allow_javascript FROM history WHERE server=?"); - query.bindValue(0, url.host()); + query.prepare("SELECT allow_images, allow_javascript FROM site_settings WHERE server=?"); + query.addBindValue(url.host()); query.exec(); if (query.next()) { @@ -75,3 +75,35 @@ SiteWebEngineSettings SiteSettingsManager::getWebEngineSettings(const QUrl& url) return settings; } + +void SiteSettingsManager::setJavascript(const QUrl& url, int value) +{ + auto job = new SqlQueryJob(QSL("UPDATE site_settings SET allow_javascript=? WHERE server=?"), this); + job->addBindValue(value); + job->addBindValue(url.host()); + connect(job, &SqlQueryJob::finished, this, [=]() { + if (job->numRowsAffected() == 0) { + auto job = new SqlQueryJob(QSL("INSERT INTO site_settings (server, allow_javascript) VALUES (?,?)"), this); + job->addBindValue(url.host()); + job->addBindValue(value); + job->start(); + } + }); + job->start(); +} + +void SiteSettingsManager::setImages(const QUrl& url, int value) +{ + auto job = new SqlQueryJob(QSL("UPDATE site_settings SET allow_images=? WHERE server=?"), this); + job->addBindValue(value); + job->addBindValue(url.host()); + connect(job, &SqlQueryJob::finished, this, [=]() { + if (job->numRowsAffected() == 0) { + auto job = new SqlQueryJob(QSL("INSERT INTO site_settings (server, allow_images) VALUES (?,?)"), this); + job->addBindValue(url.host()); + job->addBindValue(value); + job->start(); + } + }); + job->start(); +} diff --git a/src/lib/other/sitesettingsmanager.h b/src/lib/other/sitesettingsmanager.h index 9fa2a3a7c..fc1000370 100644 --- a/src/lib/other/sitesettingsmanager.h +++ b/src/lib/other/sitesettingsmanager.h @@ -47,6 +47,8 @@ public: */ SiteWebEngineSettings getWebEngineSettings(const QUrl &url); + void setJavascript(const QUrl &url, int value); + void setImages(const QUrl &url, int value); private: }; diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 910780750..e49887300 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -33,6 +33,7 @@ #include "delayedfilewatcher.h" #include "searchenginesmanager.h" #include "html5permissions/html5permissionsmanager.h" +#include "sitesettingsmanager.h" #include "javascript/externaljsobject.h" #include "tabwidget.h" #include "networkmanager.h" @@ -451,8 +452,9 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio if (result) { if (isMainFrame) { const bool isWeb = url.scheme() == QL1S("http") || url.scheme() == QL1S("https") || url.scheme() == QL1S("file"); - const bool globalJsEnabled = mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled); - settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, isWeb ? globalJsEnabled : true); + const SiteWebEngineSettings siteSettings = mApp->siteSettingsManager()->getWebEngineSettings(url); + settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, isWeb ? siteSettings.allowJavaScript : true); + settings()->setAttribute(QWebEngineSettings::AutoLoadImages, isWeb ? siteSettings.allowImages : true); } Q_EMIT navigationRequestAccepted(url, type, isMainFrame); } diff --git a/src/plugins/StatusBarIcons/sbi_imagesicon.cpp b/src/plugins/StatusBarIcons/sbi_imagesicon.cpp index f7a69c1de..7e5575ad1 100644 --- a/src/plugins/StatusBarIcons/sbi_imagesicon.cpp +++ b/src/plugins/StatusBarIcons/sbi_imagesicon.cpp @@ -21,6 +21,7 @@ #include "tabbedwebview.h" #include "webpage.h" #include "mainapplication.h" +#include "sitesettingsmanager.h" #include #include @@ -58,10 +59,10 @@ void SBI_ImagesIcon::showMenu(const QPoint &point) menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont); if (testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages)) { - menu.addAction(tr("Disable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages); + menu.addAction(tr("Disable loading images"), this, &SBI_ImagesIcon::toggleLoadingImages); } else { - menu.addAction(tr("Enable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages); + menu.addAction(tr("Enable loading images"), this, &SBI_ImagesIcon::toggleLoadingImages); } menu.addSeparator(); @@ -77,8 +78,13 @@ void SBI_ImagesIcon::showMenu(const QPoint &point) void SBI_ImagesIcon::toggleLoadingImages() { + WebPage *page = currentPage(); + if (!page) { + return; + } + bool current = testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages); - setCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages, !current); + mApp->siteSettingsManager()->setImages(page->url(), (!current) ? 1 : -1); // We should reload page on disabling images if (current) { diff --git a/src/plugins/StatusBarIcons/sbi_javascripticon.cpp b/src/plugins/StatusBarIcons/sbi_javascripticon.cpp index 4f0a2c7f8..f8322a65a 100644 --- a/src/plugins/StatusBarIcons/sbi_javascripticon.cpp +++ b/src/plugins/StatusBarIcons/sbi_javascripticon.cpp @@ -21,6 +21,8 @@ #include "tabbedwebview.h" #include "webpage.h" #include "jsoptions.h" +#include "mainapplication.h" +#include "sitesettingsmanager.h" #include #include @@ -51,10 +53,10 @@ void SBI_JavaScriptIcon::showMenu(const QPoint &point) menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont); if (testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled)) { - menu.addAction(tr("Disable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript); + menu.addAction(tr("Disable JavaScript"), this, &SBI_JavaScriptIcon::toggleJavaScript); } else { - menu.addAction(tr("Enable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript); + menu.addAction(tr("Enable JavaScript"), this, &SBI_JavaScriptIcon::toggleJavaScript); } // JavaScript needs to be always enabled for falkon: sites @@ -88,15 +90,10 @@ void SBI_JavaScriptIcon::toggleJavaScript() } bool current = testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled); - setCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled, !current); m_settings[page] = !current; - connect(page, &WebPage::navigationRequestAccepted, this, [=](const QUrl &, WebPage::NavigationType, bool isMainFrame) { - if (isMainFrame) { - page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, m_settings[page]); - } - }); + mApp->siteSettingsManager()->setJavascript(page->url(), m_settings[page] ? 1 : -1); m_window->weView()->reload();