From dc9ff73693442ebea95a1cab2da6393891177519 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 6 Aug 2022 16:53:32 +0200 Subject: [PATCH] Initial introduction of SiteSettingsManager - New database SQL querry - Update current database - Preparation for setting site settings WIP: This is an initial draft and the design of the database table might change as the development goes on. WIP2: Even this commit and commit message will be scraped. Signed-off-by: Juraj Oravec --- src/lib/CMakeLists.txt | 2 + src/lib/app/mainapplication.cpp | 10 ++++ src/lib/app/mainapplication.h | 3 ++ src/lib/app/profilemanager.cpp | 62 +++++++++++++++++++++ src/lib/app/profilemanager.h | 3 ++ src/lib/data/data/browsedata.sql | 18 +++++++ src/lib/other/sitesettingsmanager.cpp | 77 +++++++++++++++++++++++++++ src/lib/other/sitesettingsmanager.h | 53 ++++++++++++++++++ 8 files changed, 228 insertions(+) create mode 100644 src/lib/other/sitesettingsmanager.cpp create mode 100644 src/lib/other/sitesettingsmanager.h diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 4ea872344..2c661c182 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -150,6 +150,7 @@ set(SRCS ${SRCS} other/qzsettings.cpp other/siteinfo.cpp other/siteinfowidget.cpp + other/sitesettingsmanager.cpp other/statusbar.cpp other/updater.cpp other/useragentmanager.cpp @@ -380,6 +381,7 @@ set(SRCS ${SRCS} other/qzsettings.h other/siteinfo.h other/siteinfowidget.h + other/sitesettingsmanager.h other/statusbar.h other/updater.h other/useragentmanager.h diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 4d0cecd97..7d90ab169 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -42,6 +42,7 @@ #include "searchenginesmanager.h" #include "desktopnotificationsfactory.h" #include "html5permissions/html5permissionsmanager.h" +#include "sitesettingsmanager.h" #include "scripts.h" #include "sessionmanager.h" #include "closedwindowsmanager.h" @@ -105,6 +106,7 @@ MainApplication::MainApplication(int &argc, char** argv) , m_closedWindowsManager(nullptr) , m_protocolHandlerManager(nullptr) , m_html5PermissionsManager(nullptr) + , m_siteSettingsManager(nullptr) , m_desktopNotifications(nullptr) , m_webProfile(nullptr) , m_autoSaver(nullptr) @@ -645,6 +647,14 @@ HTML5PermissionsManager* MainApplication::html5PermissionsManager() return m_html5PermissionsManager; } +SiteSettingsManager * MainApplication::siteSettingsManager() +{ + if (!m_siteSettingsManager) { + m_siteSettingsManager = new SiteSettingsManager(this); + } + return m_siteSettingsManager; +} + DesktopNotificationsFactory* MainApplication::desktopNotifications() { if (!m_desktopNotifications) { diff --git a/src/lib/app/mainapplication.h b/src/lib/app/mainapplication.h index 4003ac65f..87e2d6415 100644 --- a/src/lib/app/mainapplication.h +++ b/src/lib/app/mainapplication.h @@ -53,6 +53,7 @@ class ProxyStyle; class SessionManager; class ClosedWindowsManager; class ProtocolHandlerManager; +class SiteSettingsManager; class FALKON_EXPORT MainApplication : public QtSingleApplication { @@ -111,6 +112,7 @@ public: ClosedWindowsManager* closedWindowsManager(); ProtocolHandlerManager *protocolHandlerManager(); HTML5PermissionsManager* html5PermissionsManager(); + SiteSettingsManager* siteSettingsManager(); DesktopNotificationsFactory* desktopNotifications(); QWebEngineProfile* webProfile() const; QWebEngineSettings *webSettings() const; @@ -188,6 +190,7 @@ private: ClosedWindowsManager* m_closedWindowsManager; ProtocolHandlerManager *m_protocolHandlerManager; HTML5PermissionsManager* m_html5PermissionsManager; + SiteSettingsManager* m_siteSettingsManager; DesktopNotificationsFactory* m_desktopNotifications; QWebEngineProfile* m_webProfile; diff --git a/src/lib/app/profilemanager.cpp b/src/lib/app/profilemanager.cpp index 177876049..6e0392050 100644 --- a/src/lib/app/profilemanager.cpp +++ b/src/lib/app/profilemanager.cpp @@ -89,6 +89,7 @@ void ProfileManager::initCurrentProfile(const QString &profileName) updateCurrentProfile(); connectDatabase(); + updateDatabase(); } int ProfileManager::createProfile(const QString &profileName) @@ -311,3 +312,64 @@ void ProfileManager::connectDatabase() SqlDatabase::instance()->setDatabase(db); } + +void ProfileManager::updateDatabase() +{ + if ((Qz::VERSION) == profileVersion) { + return; + } + + Updater::Version prof(profileVersion); + + /* Profile is from newer version than running application */ + if (prof > Updater::Version(Qz::VERSION)) { + // Ignore + return; + } + + /* Do not try to update database of too old profile */ + if (prof < Updater::Version(QStringLiteral("1.9.0"))) { + std::cout << "Falkon: Using profile from QupZilla " << qPrintable(profileVersion) << " is not supported!" << std::endl; + return; + } + + /* Update in 22.11.00 */ + if (prof < Updater::Version(QStringLiteral("22.11.70"))) { + std::cout << "Falkon: Updating database to version " << qPrintable(Qz::VERSION) << std::endl; + + QSqlQuery query(SqlDatabase::instance()->database()); + query.prepare(QStringLiteral( + "CREATE TABLE IF NOT EXISTS site_settings (" + "id INTEGER PRIMARY KEY," + "domain TEXT NOT NULL," + "zoom_level INTEGER DEFAULT 0," + "allow_cookies INTEGER DEFAULT 0," + "allow_images INTEGER DEFAULT 0," + "allow_javascript INTEGER DEFAULT 0," + "allow_notifications INTEGER DEFAULT 0," + "allow_geolocation INTEGER DEFAULT 0," + "allow_media_audio_capture INTEGER DEFAULT 0," + "allow_media_video_capture INTEGER DEFAULT 0," + "allow_media_audio_video_capture INTEGER DEFAULT 0," + "allow_mouse_lock INTEGER DEFAULT 0," + "allow_desktop_video_capture INTEGER DEFAULT 0," + "allow_desktop_audio_video_capture INTEGER DEFAULT 0" + ");" + )); + + if (!query.exec()) { + qCritical() << "Error while creating table 'site_settings' in database: " << query.lastError().text(); + qFatal("ProfileManager::updateDatabase Unable to create table 'site_settings' in the database!"); + } + + query.prepare(QStringLiteral( + "CREATE UNIQUE INDEX IF NOT EXISTS site_settings_domainuniqueindex ON site_settings (domain);" + )); + + if (!query.exec()) { + qCritical() << "Error while creating unique index for table 'site_settings': " << query.lastError().text(); + } + + return; + } +} diff --git a/src/lib/app/profilemanager.h b/src/lib/app/profilemanager.h index 74e266532..57ae238f5 100644 --- a/src/lib/app/profilemanager.h +++ b/src/lib/app/profilemanager.h @@ -52,8 +52,11 @@ private: void updateProfile(const QString ¤t, const QString &profile); void copyDataToProfile(); void migrateFromQupZilla(); + void updateDatabase(); void connectDatabase(); + + QString profileVersion; }; #endif // PROFILEMANAGER_H diff --git a/src/lib/data/data/browsedata.sql b/src/lib/data/data/browsedata.sql index 570756a50..e471a41cf 100644 --- a/src/lib/data/data/browsedata.sql +++ b/src/lib/data/data/browsedata.sql @@ -55,4 +55,22 @@ CREATE TABLE icons ( ); CREATE UNIQUE INDEX icons_urluniqueindex ON icons (url); +CREATE TABLE site_settings ( + id INTEGER PRIMARY KEY, + server TEXT NOT NULL, + zoom_level INTEGER DEFAULT 0, + allow_cookies INTEGER DEFAULT 0, + allow_images INTEGER DEFAULT 0, + allow_javascript INTEGER DEFAULT 0, + allow_notifications INTEGER DEFAULT 0, + allow_geolocation INTEGER DEFAULT 0, + allow_media_audio_capture INTEGER DEFAULT 0, + allow_media_video_capture INTEGER DEFAULT 0, + allow_media_audio_video_capture INTEGER DEFAULT 0, + allow_mouse_lock INTEGER DEFAULT 0, + allow_desktop_video_capture INTEGER DEFAULT 0, + allow_desktop_audio_video_capture INTEGER DEFAULT 0 +); +CREATE UNIQUE INDEX site_settings_serveruniqueindex ON site_settings (server); + -- Data diff --git a/src/lib/other/sitesettingsmanager.cpp b/src/lib/other/sitesettingsmanager.cpp new file mode 100644 index 000000000..e1532700a --- /dev/null +++ b/src/lib/other/sitesettingsmanager.cpp @@ -0,0 +1,77 @@ +/* ============================================================ + * Falkon - Qt web browser + * Copyright (C) 2022 Juraj Oravec + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * ============================================================ */ +#include "sitesettingsmanager.h" + +#include "mainapplication.h" +#include "settings.h" +#include "sqldatabase.h" +#include +#include + +SiteSettingsManager::SiteSettingsManager ( QObject* parent ) +: QObject(parent) +{ + loadSettings(); +} + +SiteSettingsManager::~SiteSettingsManager() noexcept +{ +} + +void SiteSettingsManager::loadSettings() +{ + Settings settings; + settings.beginGroup("Web-Browser-Settings"); +// m_isSaving = settings.value("allowPerDomainZoom", true).toBool(); + settings.endGroup(); +} + +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.exec(); + + if (query.next()) { + int allow_images = query.value("allow_images").toInt(); + int allow_javascript = query.value("allow_javascript").toInt(); + + if (allow_images == 0) { + settings.allowImages = mApp->webSettings()->testAttribute(QWebEngineSettings::AutoLoadImages); + } + else { + settings.allowImages = (allow_images == 1); + } + + if (allow_javascript == 0) { + settings.allowJavaScript = mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled); + } + else { + settings.allowJavaScript = (allow_javascript == 1); + } + } + else { + settings.allowImages = mApp->webSettings()->testAttribute(QWebEngineSettings::AutoLoadImages); + settings.allowJavaScript = mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled); + } + + return settings; +} diff --git a/src/lib/other/sitesettingsmanager.h b/src/lib/other/sitesettingsmanager.h new file mode 100644 index 000000000..9fa2a3a7c --- /dev/null +++ b/src/lib/other/sitesettingsmanager.h @@ -0,0 +1,53 @@ +/* ============================================================ + * Falkon - Qt web browser + * Copyright (C) 2022 Juraj Oravec + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * ============================================================ */ +#ifndef SITESETTINGS_MANAGER_H +#define SITESETTINGS_MANAGER_H + +#include "qzcommon.h" + +class QUrl; + +class SiteWebEngineSettings +{ +public: + bool allowJavaScript; + bool allowImages; +}; + +class FALKON_EXPORT SiteSettingsManager : QObject +{ + Q_OBJECT + +public: + explicit SiteSettingsManager(QObject *parent = 0); + ~SiteSettingsManager(); + + void loadSettings(); + + /** + * @brief Get settings which should be applied to webpage before loading. + * Since this is using blocking database querry group all settings + * in one SQL call for faster laoding. + * @param url Address for which to fetch the settings + */ + SiteWebEngineSettings getWebEngineSettings(const QUrl &url); + +private: +}; + +#endif // SITESETTINGS_MANAGER_H