1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

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 <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2022-08-07 00:34:11 +02:00
parent b10b310d11
commit d0b9510b73
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B
5 changed files with 54 additions and 15 deletions

View File

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

View File

@ -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:
};

View File

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

View File

@ -21,6 +21,7 @@
#include "tabbedwebview.h"
#include "webpage.h"
#include "mainapplication.h"
#include "sitesettingsmanager.h"
#include <QGraphicsColorizeEffect>
#include <QSettings>
@ -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) {

View File

@ -21,6 +21,8 @@
#include "tabbedwebview.h"
#include "webpage.h"
#include "jsoptions.h"
#include "mainapplication.h"
#include "sitesettingsmanager.h"
#include <QGraphicsColorizeEffect>
#include <QWebEngineSettings>
@ -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();