]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/views/BrowserView.cpp
Switch the Domain Settings implementation to a Dialog.
[PrivacyBrowserPC.git] / src / views / BrowserView.cpp
index 44c170a72e46e77e5429157cca01f908b058c160..aa9ffffa18132bb0d6d18d5802758a1a0310183b 100644 (file)
 #include "Settings.h"
 #include "ui_BrowserView.h"
 #include "UrlRequestInterceptor.h"
+#include "dialogs/DomainSettingsDialog.h"
+#include "helpers/DomainsDatabaseHelper.h"
 #include "helpers/SearchEngineHelper.h"
 #include "helpers/UserAgentHelper.h"
 #include "windows/BrowserWindow.h"
-#include "windows/DomainSettingsWindow.h"
 
 // Qt framework headers.
 #include <QAction>
@@ -72,6 +73,13 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
     connect(domainSettingsButtonPointer, SIGNAL(clicked()), this, SLOT(openDomainSettings()));
 
+    // Get the URL line edit palettes.
+    noDomainSettingsPalette = urlLineEditPointer->palette();
+    domainSettingsPalette = urlLineEditPointer->palette();
+
+    // Modify the domain settings palette.
+    domainSettingsPalette.setColor(QPalette::Base, Qt::green);
+
     // Instantiate the mouse event pointer.
     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
 
@@ -88,7 +96,7 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
 
     // Reapply the domain settings when the host changes.
-    connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading()));
+    connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
 
     // Disable the cache.
     webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
@@ -113,23 +121,83 @@ void BrowserView::applyApplicationSettings()
 void BrowserView::applyDomainSettingsAndReload() const
 {
     // Apply the domain settings.  `true` reloads the website.
-    applyDomainSettings(true);
+    applyDomainSettings(webEngineViewPointer->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.
-void BrowserView::applyDomainSettingsWithoutReloading() const
+void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname) const
 {
     // Apply the domain settings  `false` does not reload the website.
-    applyDomainSettings(false);
+    applyDomainSettings(hostname, false);
 }
 
-void BrowserView::applyDomainSettings(bool reloadWebsite) const
+void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) const
 {
-    // Set the JavaScript status.
-    webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
+    // Get the record for the hostname.
+    QSqlQuery domainQuery = DomainsDatabaseHelper::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();
+
+        // Set the JavaScript status.
+        switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
+        {
+            case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
+            {
+                // Set the default JavaScript status.
+                webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
+
+                break;
+            }
+
+            case (DomainsDatabaseHelper::DISABLED):
+            {
+                // Disable JavaScript.
+                webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
+
+                break;
+            }
+
+            case (DomainsDatabaseHelper::ENABLED):
+            {
+                // Enable JavaScript.
+                webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
+
+                break;
+            }
+        }
+
+        // Apply the user agent.
+        webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
+
+        // Set the zoom factor.
+        webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
+
+        // Apply the domain settings palette to the URL line edit.
+        urlLineEditPointer->setPalette(domainSettingsPalette);
+    }
+    else  // The hostname does not have domain settings.
+    {
+        // Set the JavaScript status.
+        webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
+
+        // Apply the user agent.
+        webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
+
+        // Set the zoom factor.
+        webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
+
+        // Apply the no domain settings palette to the URL line edit.
+        urlLineEditPointer->setPalette(noDomainSettingsPalette);
+    }
 
     // Update the JavaScript button.
-    if (Settings::javaScript())
+    if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
     {
         javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
     }
@@ -138,12 +206,6 @@ void BrowserView::applyDomainSettings(bool reloadWebsite) const
         javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
     }
 
-    // Apply the user agent.
-    webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
-
-    // Set the zoom factor.
-    webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
-
     // Emit the on-the-fly menu update signals.
     emit userAgentUpdated(Settings::userAgent());
     emit zoomFactorUpdated(Settings::zoomFactor());
@@ -242,45 +304,20 @@ void BrowserView::loadUrlFromTextBox(QString urlFromUser) const
 
 void BrowserView::openDomainSettings() const
 {
-    // Get a list of the top level widgets.
-    const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
+    // Instantiate the domain settings window.
+    DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
 
-    // Initialize a domain settings window exists boolean.
-    bool domainSettingsWindowExists = false;
+    // Set the dialog window title.
+    domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
 
-    // Iterate through the top level widgets.
-    for (QWidget *widgetPointer : topLevelWidgets)
-    {
-        // Check for an existing domain settings window.
-        if (widgetPointer->objectName() == QStringLiteral("domain_settings"))
-        {
-            // Show the existing domain settings window if it is hidden.
-            widgetPointer->show();
-
-            // Raise the existing domain settings window if it is below other windows.
-            widgetPointer->raise();
-
-            // Restore the existing domain settings window if it has been minimized.
-            if (widgetPointer->isMinimized()) {
-                widgetPointer->showNormal();
-            }
-
-            // Activate the existing domain settings window, which brings its virtual desktop into focus.
-            widgetPointer->activateWindow();
+    // Resize the dialog window.
+    domainSettingsDialogPointer->resize(1500, 1000);
 
-            // Update the domain settings window exists boolean.
-            domainSettingsWindowExists = true;
-        }
-    }
+    // Set the modality.
+    domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
 
-    if (!domainSettingsWindowExists)
-    {
-        // Instantiate the domain settings window.
-        DomainSettingsWindow *domainSettingsWindowPointer = new DomainSettingsWindow();
-
-        // Show the window.
-        domainSettingsWindowPointer->show();
-    }
+    // Show the dialog.
+    domainSettingsDialogPointer->show();
 }
 
 void BrowserView::pageLinkHovered(const QString &linkUrl) const