From 8756d450d1d44dd8e840f7e3de7b1d72ca5b7d8e Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 27 Feb 2023 17:42:40 -0700 Subject: [PATCH] Move `applyDomainSettings()` to `PrivacyWebEngineView`. https://redmine.stoutner.com/issues/966 --- doc/CMakeLists.txt | 18 + doc/index.docbook | 842 ++++++++++----------------- src/widgets/PrivacyWebEngineView.cpp | 181 +++++- src/widgets/PrivacyWebEngineView.h | 15 +- src/widgets/TabWidget.cpp | 271 ++------- src/widgets/TabWidget.h | 10 +- 6 files changed, 567 insertions(+), 770 deletions(-) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index ffd9ed3..22771ec 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -1 +1,19 @@ +# Copyright 2023 Soren Stoutner . +# +# This file is part of Privacy Browser PC . +# +# Privacy Browser PC 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. +# +# Privacy Browser PC 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 Privacy Browser PC. If not, see . + +# Create the documentation. kdoctools_create_handbook(index.docbook INSTALL_DESTINATION ${KDE_INSTALL_DOCBUNDLEDIR}/en SUBDIR privacybrowser) diff --git a/doc/index.docbook b/doc/index.docbook index b135352..5a08451 100644 --- a/doc/index.docbook +++ b/doc/index.docbook @@ -1,550 +1,316 @@ - - privacybrowserpc"> - Translatable Entity"> - - - - -]> - - - - - - - - - - - - - - - - -The &kmyapplication; Handbook - - - - - -George -N. -Ugnacious - -gnu@kde.org - - - - - -2015 -George N. Ugnacious - -&CCBYSA4Notice; - - -2016-04-23 - - -Frameworks xx.yy or Plasma xx.yy or Applications xx.yy or xx.yy (Applications xx.yy) or $applicationname xx.yy - - - - - -&kmyapplication; is an application specially designed to do nothing you would -ever want. - - - - - - -KDE -kdeutils -Kapp -nothing -nothing else - - - - - - - - - -Introduction - - - - -&kmyapplication; is a program that lets you do absolutely nothing. Please report -any problems or feature requests to the &kde; mailing lists. - - - - -Using &kmyapplication; - - - - - - - - -Here's a screenshot of &kmyapplication; - - - - - - Screenshot - - - - - - - -More &kmyapplication; features - -It slices! It dices! and it comes with a free toaster! - -The Squiggle Tool - - - - - Squiggle - - is used to draw squiggly lines all over -the &kmyapplication; main window. It's not a bug, it's a feature! - - - - - - -Command Reference - - Enables search for menu items - -For Applications with default menu items and some special items where user -needs additional information use something like: -"Apart from the common KDE menu items you find these action in the menu: - -File -> Special Action: Explanation of special action - -Tools -> Special Tool: Explanation of special tool - -Use variablelist markup for this ---> - - -The main &kmyapplication; window - - -The File Menu - - - - - - -&Ctrl;N - -File -New - -Creates a new document - - - - - -&Ctrl;S - -File -Save - -Saves the document - - - - - -&Ctrl;Q - -File -Quit - -Quits &kmyapplication; - - - - - - - - - -The Settings and Help Menu - -&kmyapplication; has the common &kde; Settings and Help -menu items, for more information read the sections about the Settings Menu and Help Menu -of the &kde; Fundamentals. - - - - -The Help Menu - -&kmyapplication; has the common &kde; Help menu item, for more information read the section -about the Help Menu of the &kde; Fundamentals. - - - - -Menu Items -Apart from the common &kde; menus described in the Menu -chapter of the &kde; Fundamentals documentation &kmyapplication; has these application specific menu entries: - - - - - -The Help Menu -&kmyapplication; has a default &kde; Help menu as described in the -&kde; Fundamentals -with two additional entries: - - - - - - - -Developer's Guide to &kmyapplication; - - - - -Programming &kmyapplication; plugins is a joy to behold. - - - - - - -Questions and Answers - - - - - - -My Mouse doesn't work. How do I quit &kmyapplication;? - - -You silly goose! Check out the Commands -Section for the answer. - - - - -Why can I not twiddle my documents? - - -You can only twiddle your documents if you have the foobar.lib -installed. - - - - - - - - - -Credits and License - - -&kmyapplication; - - -Program copyright 2010-2014 John Q. Hacker jqh@kde.org - - -Contributors: - -Konqui the &kde; Dragon konqui@kde.org - -Tux the &Linux; Penguin tux@linux.org - - - - - -Documentation Copyright © 2012-2014 George N. Ugnacious gnu@kde.org - - - - - - -&underCCBYSA4; - - - - - - -&underGPL; -&underBSDLicense; -&underArtisticLicense; -&underX11License; + Copyright 2023 Soren Stoutner . - + This file is part of Privacy Browser PC . - ---> - - -Installation - - -How to obtain &kmyapplication; - - + + Privacy Browser"> -&install.intro.documentation; + + SorenStoutner"> + soren@stoutner.com"> - + + - -Requirements + + Translatable Entity"> + +]> - - - -In order to successfully use &kmyapplication;, you need few libraries from -&kf5; 5.1 (bar, baz). Foobar.lib is -required in order to support the advanced &kmyapplication; features. &kmyapplication; uses -about 5 megs of memory to run, but this may vary depending on your -platform and configuration. - - - -All required libraries as well as &kmyapplication; itself can be found -on The &kmyapplication; home page. - - - - -You can find a list of changes at http://apps.kde.org/kapp. - - - - -Compilation and Installation - - - - - -&install.compile.documentation; - - - - -Configuration - -Don't forget to tell your system to start the dtd -dicer-toaster daemon first, or &kmyapplication; won't work! - - - - - -&documentation.index; + + + The &privacybrowser; Handbook + + + &Soren.Stoutner; &Soren.Stoutner.mail; + + + + + + 2016-2017, 2021-2023 + &Soren.Stoutner; + + + + &FDLNotice; + + + 2023-02-22 + + + &privacybrowser; version 0.1 + + + + + + &privacybrowser; is a web browser that respects your privacy. + + + + + + KDE + privacy + browser + + + + + + Introduction + + + &privacybrowser; is currently in an early alpha state. + Most of the features are not yet implemented, but I thought it would be useful to publish it so that users can track the progress and submit feedback. + + + + To distinguish between the Android and the PC version, the website, issue tracker, + and code base refer to this version as Privacy Browser PC. + There is a list of feature requests and known bugs at redmine.stoutner.com. + Users should anticipate that all the current features of Privacy Browser Android + will also be implemented in Privacy Browser PC. + There is no need at this point to create features requests for these as they will be added as I start working on each feature and have a better idea of how they will be implemented. + However, each feature that has already been implemented should be bug free. + If you discover a bug that is not already documented at redmine.stoutner.com please add it. + + + + The best place to discuss the development of Privacy Browser is on the forum. + I also frequently post on my Mastodon account regarding the development status. + + + + + + Using &privacybrowser; + + + + Here's a screenshot of &privacybrowser; + + + + + + Screenshot + + + + + + + More &privacybrowser; features + + + It slices! It dices! and it comes with a free toaster! + + + + The Squiggle Tool + + + + + Squiggle + + is used to draw squiggly lines all over the &privacybrowser; main window. It's not a bug, it's a feature! + + + + + + + Command Reference + + + The main &privacybrowser; window + + + The File Menu + + + + + + + + + &Ctrl;N + + File + New + + + + + + Creates a new document + + + + + + + + + &Ctrl;S + + File + Save + + + + + + Saves the document + + + + + + + + + &Ctrl;Q + + File + Quit + + + + + + Quits &privacybrowser; + + + + + + + + + + The Settings and Help Menu + + + &privacybrowser; has the common &kde; Settings and Help + menu items, for more information read the sections about the Settings Menu and Help Menu + of the &kde; Fundamentals. + + + + + The Help Menu + + + &privacybrowser; has the common &kde; Help menu item, for more information read the section + about the Help Menu of the &kde; Fundamentals. + + + + + Menu Items + + + Apart from the common &kde; menus described in the Menu + chapter of the &kde; Fundamentals documentation &privacybrowser; has these application specific menu entries: + + + + + The Help Menu + + + &privacybrowser; has a default &kde; Help menu as described in the + &kde; Fundamentals + with two additional entries: + + + + + + + + Questions and Answers + + + + + + + My Mouse doesn't work. How do I quit &privacybrowser;? + + + + You silly goose! Check out the Commands Section for the answer. + + + + + + Why can I not twiddle my documents? + + + + + You can only twiddle your documents if you have the foobar.lib installed. + + + + + + + + + Credits and License + + + Program copyright 2016-2017,2021-2023 Soren Stoutner soren@stoutner.com. + + + + Translators: + + Translations will be added in a future release. + + + + + &underGPL; + + + Documentation copyright 2023 Soren Stoutner soren@stoutner.com. + + + + &underFDL; + - - diff --git a/src/widgets/PrivacyWebEngineView.cpp b/src/widgets/PrivacyWebEngineView.cpp index d7b3f8c..bc207cb 100644 --- a/src/widgets/PrivacyWebEngineView.cpp +++ b/src/widgets/PrivacyWebEngineView.cpp @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Soren Stoutner . + * Copyright 2022-2023 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -19,7 +19,10 @@ // Application headers. #include "PrivacyWebEngineView.h" +#include "Settings.h" #include "databases/CookiesDatabase.h" +#include "databases/DomainsDatabase.h" +#include "interceptors/UrlRequestInterceptor.h" #include "windows/BrowserWindow.h" // Qt toolkit headers. @@ -27,7 +30,29 @@ #include // Construct the class. -PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {} +PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) +{ + // Create an off-the-record profile (the default when no profile name is specified). + webEngineProfilePointer = new QWebEngineProfile(QLatin1String("")); + + // Create a WebEngine page. + QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer); + + // Set the WebEngine page. + setPage(webEnginePagePointer); + + // Get handles for the various aspects of the WebEngine. + webEngineSettingsPointer = webEnginePagePointer->settings(); + + // Instantiate the URL request interceptor. + UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor(); + + // Set the URL request interceptor. + webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer); + + // Reapply the domain settings when the host changes. + connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString))); +} void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const { @@ -44,6 +69,154 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const emit updateCookiesAction(cookieListPointer->size()); } +void PrivacyWebEngineView::applyDomainSettingsWithoutReloading(const QString &hostname) +{ + // Apply the domain settings `false` does not reload the website. + applyDomainSettings(hostname, false); +} + +void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) +{ + // Get the record for the hostname. + QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname); + + // Check if the hostname has domain settings. + if (domainQuery.isValid()) // The hostname has domain settings. + { + // Get the domain record. + QSqlRecord domainRecord = domainQuery.record(); + + // Store the domain settings name. + domainSettingsName = domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString(); + + // Set the JavaScript status. + switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt()) + { + // Set the default JavaScript status. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); + + break; + } + + // Disable JavaScript. + case (DomainsDatabase::DISABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); + + break; + } + + // Enable JavaScript. + case (DomainsDatabase::ENABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); + + break; + } + } + + // Set the local storage status. + switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt()) + { + // Set the default local storage status. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + localStorageEnabled = Settings::localStorageEnabled(); + + break; + } + + // Disable local storage. + case (DomainsDatabase::DISABLED): + { + localStorageEnabled = false; + + break; + } + + // Enable local storage. + case (DomainsDatabase::ENABLED): + { + localStorageEnabled = true; + + break; + } + } + + // Set the DOM storage status. + switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt()) + { + // Set the default DOM storage status. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); + + break; + } + + // Disable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::DISABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); + + break; + } + + // Enable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::ENABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); + + break; + } + } + + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString())); + + // Check if a custom zoom factor is set. + if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt()) + { + // Store the current zoom factor. + setZoomFactor(domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble()); + } + else + { + // Reset the current zoom factor. + setZoomFactor(Settings::zoomFactor()); + } + } + else // The hostname does not have domain settings. + { + // Reset the domain settings name. + domainSettingsName = QLatin1String(""); + + // Set the JavaScript status. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); + + // Set the local storage status. + localStorageEnabled = Settings::localStorageEnabled(); + + // Set DOM storage. In QWebEngineSettings it is called Local Storage. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); + + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent())); + + // Set the zoom factor. + setZoomFactor(Settings::zoomFactor()); + } + + // Reload the website if requested. + if (reloadWebsite) + reload(); + + // Update the UI. + emit updateUi(this); +} + void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) { // Get a handle for the QWebEnginePage *webEnginePagePointer = page(); @@ -66,7 +239,7 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType // Get a handle for the browser window. BrowserWindow *browserWindowPointer = qobject_cast(window()); - // Create the requsted window type. + // Create the requested window type. switch (webWindowType) { case QWebEnginePage::WebBrowserTab: @@ -97,7 +270,7 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType default: { - // Return an null pointer for opening a background tab and opening a web dialog. + // Return a null pointer for opening a web dialog. return nullptr; } } diff --git a/src/widgets/PrivacyWebEngineView.h b/src/widgets/PrivacyWebEngineView.h index 31319da..152c56e 100644 --- a/src/widgets/PrivacyWebEngineView.h +++ b/src/widgets/PrivacyWebEngineView.h @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Soren Stoutner . + * Copyright 2022-2023 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -43,15 +43,28 @@ public: int loadProgressInt = -1; bool localStorageEnabled = false; + // The public functions. + void applyDomainSettings(const QString &hostname, const bool reloadWebsite); + signals: // The signals. void updateCookiesAction(const int numberOfCookies) const; + void updateUi(const PrivacyWebEngineView *privacyWebEngineViewPointer) const; public Q_SLOTS: // The public slots. void addCookieToList(const QNetworkCookie &cookie) const; void removeCookieFromList(const QNetworkCookie &cookie) const; +private Q_SLOTS: + // The private slots. + void applyDomainSettingsWithoutReloading(const QString &hostname); + +private: + // The private variables. + QWebEngineProfile *webEngineProfilePointer; + QWebEngineSettings *webEngineSettingsPointer; + protected: // The protected functions. void contextMenuEvent(QContextMenuEvent *contextMenuEvent) override; diff --git a/src/widgets/TabWidget.cpp b/src/widgets/TabWidget.cpp index 5ef7fc5..2e32cab 100644 --- a/src/widgets/TabWidget.cpp +++ b/src/widgets/TabWidget.cpp @@ -23,11 +23,9 @@ #include "ui_AddTabWidget.h" #include "ui_TabWidget.h" #include "databases/CookiesDatabase.h" -#include "databases/DomainsDatabase.h" #include "dialogs/SaveDialog.h" #include "filters/MouseEventFilter.h" #include "helpers/SearchEngineHelper.h" -#include "interceptors/UrlRequestInterceptor.h" #include "windows/BrowserWindow.h" // KDE Framework headers. @@ -61,25 +59,25 @@ TabWidget::TabWidget(QWidget *parent) : QWidget(parent) tabWidgetUi.setupUi(this); // Get a handle for the tab widget. - tabWidgetPointer = tabWidgetUi.tabWidget; + qTabWidgetPointer = tabWidgetUi.tabWidget; // Setup the add tab UI. - addTabWidgetUi.setupUi(tabWidgetPointer); + addTabWidgetUi.setupUi(qTabWidgetPointer); // Get handles for the add tab widgets. QWidget *addTabWidgetPointer = addTabWidgetUi.addTabQWidget; QPushButton *addTabButtonPointer = addTabWidgetUi.addTabButton; // Display the add tab widget. - tabWidgetPointer->setCornerWidget(addTabWidgetPointer); + qTabWidgetPointer->setCornerWidget(addTabWidgetPointer); // Add the first tab. addFirstTab(); // Process tab events. - connect(tabWidgetPointer, SIGNAL(currentChanged(int)), this, SLOT(updateUiWithTabSettings())); + connect(qTabWidgetPointer, SIGNAL(currentChanged(int)), this, SLOT(updateUiWithTabSettings())); connect(addTabButtonPointer, SIGNAL(clicked()), this, SLOT(addTab())); - connect(tabWidgetPointer, SIGNAL(tabCloseRequested(int)), this, SLOT(deleteTab(int))); + connect(qTabWidgetPointer, SIGNAL(tabCloseRequested(int)), this, SLOT(deleteTab(int))); // Store a copy of the WebEngine default user agent. webEngineDefaultUserAgent = currentWebEngineProfilePointer->httpUserAgent(); @@ -98,10 +96,10 @@ TabWidget::TabWidget(QWidget *parent) : QWidget(parent) TabWidget::~TabWidget() { // Manually delete each WebEngine page. - for (int i = 0; i < tabWidgetPointer->count(); ++i) + for (int i = 0; i < qTabWidgetPointer->count(); ++i) { // Get the privacy WebEngine view. - PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast(tabWidgetPointer->widget(i)); + PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast(qTabWidgetPointer->widget(i)); // Deletion the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !` delete privacyWebEngineViewPointer->page(); @@ -111,7 +109,7 @@ TabWidget::~TabWidget() // The cookie is copied instead of referenced so that changes made to the cookie do not create a race condition with the display of the cookie in the dialog. void TabWidget::addCookieToStore(QNetworkCookie cookie, QWebEngineCookieStore *webEngineCookieStorePointer) const { - // Create a url. + // Create a URL. QUrl url; // Check to see if the domain does not start with a `.` because Qt makes this harder than it should be. @@ -141,7 +139,7 @@ void TabWidget::addFirstTab() updateUiWithTabSettings(); // Set the focus on the current tab widget. This prevents the tab bar from showing a blue bar under the label of the first tab. - tabWidgetPointer->currentWidget()->setFocus(); + qTabWidgetPointer->currentWidget()->setFocus(); } PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool backgroundTab) @@ -150,19 +148,14 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const PrivacyWebEngineView *privacyWebEngineViewPointer = new PrivacyWebEngineView(); // Add a new tab. - int newTabIndex = tabWidgetPointer->addTab(privacyWebEngineViewPointer, i18nc("New tab label.", "New Tab")); + int newTabIndex = qTabWidgetPointer->addTab(privacyWebEngineViewPointer, i18nc("New tab label.", "New Tab")); // Set the default tab icon. - tabWidgetPointer->setTabIcon(newTabIndex, defaultTabIcon); + qTabWidgetPointer->setTabIcon(newTabIndex, defaultTabIcon); - // Create an off-the-record profile (the default when no profile name is specified). - QWebEngineProfile *webEngineProfilePointer = new QWebEngineProfile(QLatin1String("")); - - // Create a WebEngine page. - QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer); - - // Set the WebEngine page. - privacyWebEngineViewPointer->setPage(webEnginePagePointer); + // Get handles for the WebEngine page and profile. + QWebEnginePage *webEnginePagePointer = privacyWebEngineViewPointer->page(); + QWebEngineProfile *webEngineProfilePointer = webEnginePagePointer->profile(); // Get handles for the web engine elements. QWebEngineCookieStore *webEngineCookieStorePointer = webEngineProfilePointer->cookieStore(); @@ -181,9 +174,6 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const emit updateBackAction(currentWebEngineHistoryPointer->canGoBack()); emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward()); } - - // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. It can be removed once is fixed. - privacyWebEngineViewPointer->setZoomFactor(currentZoomFactor); }); // Update the progress bar when a load is started. @@ -239,15 +229,6 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const // Handle file downloads. connect(webEngineProfilePointer, SIGNAL(downloadRequested(QWebEngineDownloadItem *)), this, SLOT(showSaveDialog(QWebEngineDownloadItem *))); - // Instantiate the URL request interceptor. - UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor(); - - // Set the URL request interceptor. - webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer); - - // Reapply the domain settings when the host changes. - connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString))); - // Set the local storage filter. webEngineCookieStorePointer->setCookieFilter([privacyWebEngineViewPointer](const QWebEngineCookieStore::FilterRequest &filterRequest) { @@ -322,13 +303,13 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, [this, privacyWebEngineViewPointer] (const QString &title) { // Get the index for this tab. - int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer); + int tabIndex = qTabWidgetPointer->indexOf(privacyWebEngineViewPointer); // Update the title for this tab. - tabWidgetPointer->setTabText(tabIndex, title); + qTabWidgetPointer->setTabText(tabIndex, title); // Update the window title if this is the current tab. - if (tabIndex == tabWidgetPointer->currentIndex()) + if (tabIndex == qTabWidgetPointer->currentIndex()) emit updateWindowTitle(title); }); @@ -336,13 +317,13 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, [privacyWebEngineViewPointer, this] (const QIcon &icon) { // Get the index for this tab. - int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer); + int tabIndex = qTabWidgetPointer->indexOf(privacyWebEngineViewPointer); // Update the icon for this tab. if (icon.isNull()) - tabWidgetPointer->setTabIcon(tabIndex, defaultTabIcon); + qTabWidgetPointer->setTabIcon(tabIndex, defaultTabIcon); else - tabWidgetPointer->setTabIcon(tabIndex, icon); + qTabWidgetPointer->setTabIcon(tabIndex, icon); }); // Enable spell checking. @@ -354,9 +335,12 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const // Populate the zoom factor. This is necessary if a URL is being loaded, like a local URL, that does not trigger `applyDomainSettings()`. privacyWebEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + // Update the UI when domain settings are applied. + connect(privacyWebEngineViewPointer, SIGNAL(updateUi(const PrivacyWebEngineView*)), this, SLOT(updateUiFromWebEngineView(const PrivacyWebEngineView*))); + // Move to the new tab if it is not a background tab. if (!backgroundTab) - tabWidgetPointer->setCurrentIndex(newTabIndex); + qTabWidgetPointer->setCurrentIndex(newTabIndex); // Clear the URL line edit focus so that it populates correctly when opening a new tab from the context menu. if (removeUrlLineEditFocus) @@ -370,9 +354,9 @@ void TabWidget::applyApplicationSettings() { // Set the tab position. if (Settings::tabsOnTop()) - tabWidgetPointer->setTabPosition(QTabWidget::North); + qTabWidgetPointer->setTabPosition(QTabWidget::North); else - tabWidgetPointer->setTabPosition(QTabWidget::South); + qTabWidgetPointer->setTabPosition(QTabWidget::South); // Set the search engine URL. searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine()); @@ -381,174 +365,10 @@ void TabWidget::applyApplicationSettings() emit updateSearchEngineActions(Settings::searchEngine(), true); } -// This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument. -// Once has been resolved this can be `const`. void TabWidget::applyDomainSettingsAndReload() { // Apply the domain settings. `true` reloads the website. - applyDomainSettings(currentPrivacyWebEngineViewPointer->url().host(), true); -} - -// This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument. -// Once has been resolved this can be `const`. -void TabWidget::applyDomainSettingsWithoutReloading(const QString &hostname) -{ - // Apply the domain settings `false` does not reload the website. - applyDomainSettings(hostname, false); -} - -// Once has been resolved this can be `const`. -void TabWidget::applyDomainSettings(const QString &hostname, const bool reloadWebsite) -{ - // Get the record for the hostname. - QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname); - - // Check if the hostname has domain settings. - if (domainQuery.isValid()) // The hostname has domain settings. - { - // Get the domain record. - QSqlRecord domainRecord = domainQuery.record(); - - // Store the domain settings name. - currentPrivacyWebEngineViewPointer->domainSettingsName = domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString(); - - // Set the JavaScript status. - switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt()) - { - // Set the default JavaScript status. - case (DomainsDatabase::SYSTEM_DEFAULT): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); - - break; - } - - // Disable JavaScript. - case (DomainsDatabase::DISABLED): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); - - break; - } - - // Enable JavaScript. - case (DomainsDatabase::ENABLED): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); - - break; - } - } - - // Set the local storage status. - switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt()) - { - // Set the default local storage status. - case (DomainsDatabase::SYSTEM_DEFAULT): - { - currentPrivacyWebEngineViewPointer->localStorageEnabled = Settings::localStorageEnabled(); - - break; - } - - // Disable local storage. - case (DomainsDatabase::DISABLED): - { - currentPrivacyWebEngineViewPointer->localStorageEnabled = false; - - break; - } - - // Enable local storage. - case (DomainsDatabase::ENABLED): - { - currentPrivacyWebEngineViewPointer->localStorageEnabled = true; - - break; - } - } - - // Set the DOM storage status. - switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt()) - { - // Set the default DOM storage status. QWebEngineSettings confusingly calls this local storage. - case (DomainsDatabase::SYSTEM_DEFAULT): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); - - break; - } - - // Disable DOM storage. QWebEngineSettings confusingly calls this local storage. - case (DomainsDatabase::DISABLED): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); - - break; - } - - // Enable DOM storage. QWebEngineSettings confusingly calls this local storage. - case (DomainsDatabase::ENABLED): - { - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); - - break; - } - } - - // Set the user agent. - currentWebEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString())); - - // Check if a custom zoom factor is set. - if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt()) - { - // Store the current zoom factor. - currentZoomFactor = domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble(); - } - else - { - // Reset the current zoom factor. - currentZoomFactor = Settings::zoomFactor(); - } - - // Set the zoom factor. The use of `currentZoomFactor` can be removed once has been resolved. - currentPrivacyWebEngineViewPointer->setZoomFactor(currentZoomFactor); - } - else // The hostname does not have domain settings. - { - // Reset the domain settings name. - currentPrivacyWebEngineViewPointer->domainSettingsName = QLatin1String(""); - - // Set the JavaScript status. - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); - - // Set the local storage status. - currentPrivacyWebEngineViewPointer->localStorageEnabled = Settings::localStorageEnabled(); - - // Set DOM storage. In QWebEngineSettings it is called Local Storage. - currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); - - // Set the user agent. - currentWebEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent())); - - // Store the current zoom factor. This can be removed once has been resolved. - currentZoomFactor = Settings::zoomFactor(); - - // Set the zoom factor. - currentPrivacyWebEngineViewPointer->setZoomFactor(Settings::zoomFactor()); - } - - // Update the UI. - emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QLatin1String("")); - emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); - emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled); - emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); - emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true); - emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor()); - - // Reload the website if requested. - if (reloadWebsite) - currentPrivacyWebEngineViewPointer->reload(); + currentPrivacyWebEngineViewPointer->applyDomainSettings(currentPrivacyWebEngineViewPointer->url().host(), true); } void TabWidget::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer) @@ -584,12 +404,8 @@ void TabWidget::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const currentPrivacyWebEngineViewPointer->reload(); } -// This can be const once has been resolved. -void TabWidget::applyOnTheFlyZoomFactor(const double &zoomFactor) +void TabWidget::applyOnTheFlyZoomFactor(const double &zoomFactor) const { - // Update the current zoom factor. This can be removed once has been resolved. - currentZoomFactor = zoomFactor; - // Set the zoom factor. currentPrivacyWebEngineViewPointer->setZoomFactor(zoomFactor); } @@ -597,13 +413,13 @@ void TabWidget::applyOnTheFlyZoomFactor(const double &zoomFactor) void TabWidget::applySpellCheckLanguages() const { // Get the number of tab. - int numberOfTabs = tabWidgetPointer->count(); + int numberOfTabs = qTabWidgetPointer->count(); // Set the spell check languages for each tab. for (int i = 0; i < numberOfTabs; ++i) { // Get the WebEngine view pointer. - PrivacyWebEngineView *webEngineViewPointer = qobject_cast(tabWidgetPointer->currentWidget()); + PrivacyWebEngineView *webEngineViewPointer = qobject_cast(qTabWidgetPointer->currentWidget()); // Get the WebEngine page pointer. QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page(); @@ -637,13 +453,13 @@ void TabWidget::deleteCookieFromStore(const QNetworkCookie &cookie) const void TabWidget::deleteTab(const int tabIndex) { // Get the privacy WebEngine view. - PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast(tabWidgetPointer->widget(tabIndex)); + PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast(qTabWidgetPointer->widget(tabIndex)); - // Proccess the tab delete according to the number of tabs. - if (tabWidgetPointer->count() > 1) // There is more than one tab. + // Process the tab delete according to the number of tabs. + if (qTabWidgetPointer->count() > 1) // There is more than one tab. { // Delete the tab. - tabWidgetPointer->removeTab(tabIndex); + qTabWidgetPointer->removeTab(tabIndex); // Delete the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !` delete privacyWebEngineViewPointer->page(); @@ -889,7 +705,7 @@ void TabWidget::refresh() const void TabWidget::setTabBarVisible(const bool visible) const { // Set the tab bar visibility. - tabWidgetPointer->tabBar()->setVisible(visible); + qTabWidgetPointer->tabBar()->setVisible(visible); } void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPointer) @@ -1104,10 +920,25 @@ void TabWidget::toggleLocalStorage() currentPrivacyWebEngineViewPointer->reload(); } +void TabWidget::updateUiFromWebEngineView(const PrivacyWebEngineView *privacyWebEngineViewPointer) const +{ + // Only update the UI if the signal was emitted from the current privacy WebEngine. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + { + // Update the UI. + emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QLatin1String("")); + emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); + emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled); + emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); + emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true); + emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor()); + } +} + void TabWidget::updateUiWithTabSettings() { // Update the current WebEngine pointers. - currentPrivacyWebEngineViewPointer = qobject_cast(tabWidgetPointer->currentWidget()); + currentPrivacyWebEngineViewPointer = qobject_cast(qTabWidgetPointer->currentWidget()); currentWebEngineSettingsPointer = currentPrivacyWebEngineViewPointer->settings(); currentWebEnginePagePointer = currentPrivacyWebEngineViewPointer->page(); currentWebEngineProfilePointer = currentWebEnginePagePointer->profile(); diff --git a/src/widgets/TabWidget.h b/src/widgets/TabWidget.h index 72a50a0..ccaefb9 100644 --- a/src/widgets/TabWidget.h +++ b/src/widgets/TabWidget.h @@ -50,7 +50,7 @@ public: ~TabWidget(); // The public functions. - void applyOnTheFlyZoomFactor(const double &zoomFactor); + void applyOnTheFlyZoomFactor(const double &zoomFactor) const; void applySpellCheckLanguages() const; PrivacyWebEngineView* loadBlankInitialWebsite(); void loadInitialWebsite(); @@ -96,7 +96,6 @@ public Q_SLOTS: PrivacyWebEngineView* addTab(const bool removeUrlLineEditFocus=false, const bool backgroundTab=false); void applyApplicationSettings(); void applyDomainSettingsAndReload(); - void applyDomainSettingsWithoutReloading(const QString &hostname); void applyOnTheFlySearchEngine(QAction *searchEngineActionPointer); void applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const; void back() const; @@ -121,12 +120,12 @@ private Q_SLOTS: void pageLinkHovered(const QString &linkUrl) const; void printWebpage(QPrinter *printerPointer) const; void showSaveDialog(QWebEngineDownloadItem *downloadItemPointer); + void updateUiFromWebEngineView(const PrivacyWebEngineView *privacyWebEngineViewPointer) const; void updateUiWithTabSettings(); void useNativeDownloader(QUrl &downloadUrl, QString &suggestedFileName); private: // The private variables. - double currentZoomFactor; // This can be removed once has been resolved. PrivacyWebEngineView *currentPrivacyWebEngineViewPointer; QWebEngineCookieStore *currentWebEngineCookieStorePointer; QWebEngineHistory *currentWebEngineHistoryPointer; @@ -135,11 +134,8 @@ private: QWebEngineSettings *currentWebEngineSettingsPointer; QIcon defaultTabIcon = QIcon::fromTheme(QStringLiteral("globe")); QString searchEngineUrl; - QTabWidget *tabWidgetPointer; + QTabWidget *qTabWidgetPointer; UserAgentHelper *userAgentHelperPointer; bool wipingCurrentFindTextSelection = false; - - // The private functions. - void applyDomainSettings(const QString &hostname, const bool reloadWebsite); }; #endif -- 2.45.2