]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/windows/BrowserWindow.cpp
Move the Settings Dialog to its own class. https://redmine.stoutner.com/issues/1151
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
index 7533432ec50d8a8cabc7268142e63240044f9394..88f9894aeebb71ca1f43c5ae14c55d9878f8e1f0 100644 (file)
@@ -20,9 +20,6 @@
 // Application headers.
 #include "BrowserWindow.h"
 #include "Settings.h"
-#include "ui_SettingsGeneral.h"
-#include "ui_SettingsPrivacy.h"
-#include "ui_SettingsSpellCheck.h"
 #include "databases/BookmarksDatabase.h"
 #include "databases/DomainsDatabase.h"
 #include "dialogs/AddBookmarkDialog.h"
@@ -32,6 +29,7 @@
 #include "dialogs/DomainSettingsDialog.h"
 #include "dialogs/EditBookmarkDialog.h"
 #include "dialogs/EditFolderDialog.h"
+#include "dialogs/SettingsDialog.h"
 #include "helpers/SearchEngineHelper.h"
 #include "helpers/UserAgentHelper.h"
 #include "structs/BookmarkStruct.h"
@@ -39,6 +37,7 @@
 // KDE Frameworks headers.
 #include <KActionCollection>
 #include <KColorScheme>
+#include <KLocalizedString>
 #include <KXMLGUIFactory>
 
 // Qt toolkit headers.
@@ -47,8 +46,9 @@
 #include <QDBusConnection>
 #include <QDBusConnectionInterface>
 #include <QDBusMessage>
-#include <QFileDialog>
+#include <QGuiApplication>
 #include <QInputDialog>
+#include <QLayout>
 #include <QNetworkCookie>
 #include <QMenuBar>
 #include <QMessageBox>
@@ -592,7 +592,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
     QAction *addBookmarkActionPointer = menuPointer->addAction(QIcon::fromTheme(QLatin1String("bookmark-new")), i18nc("The add bookmark action", "Add Bookmark"), [=]
         {
             // Instantiate an add bookmark dialog.
-            AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(),
+            AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(this, tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(),
                                                                                 tabWidgetPointer->getCurrentTabFavoritIcon(), folderId);
 
             // Update the displayed bookmarks when a new one is added.
@@ -607,7 +607,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
     QAction *addFolderActionPointer = menuPointer->addAction(QIcon::fromTheme(QLatin1String("folder-add")), i18nc("The add folder action", "Add Folder"), [=]
         {
             // Instantiate an add folder dialog.
-            AddFolderDialog *addFolderDialogPointer = new AddFolderDialog(tabWidgetPointer->getCurrentTabFavoritIcon(), folderId);
+            AddFolderDialog *addFolderDialogPointer = new AddFolderDialog(this, tabWidgetPointer->getCurrentTabFavoritIcon(), folderId);
 
             // Update the displayed bookmarks when a folder is added.
             connect(addFolderDialogPointer, SIGNAL(folderAdded()), this, SLOT(populateBookmarksInAllWindows()));
@@ -678,7 +678,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
                 QIcon currentTabFavoriteIcon = tabWidgetPointer->getCurrentTabFavoritIcon();
 
                 // Instantiate an edit folder dialog.
-                QDialog *editFolderDialogPointer = new EditFolderDialog(folderDatabaseId, currentTabFavoriteIcon);
+                QDialog *editFolderDialogPointer = new EditFolderDialog(this, folderDatabaseId, currentTabFavoriteIcon);
 
                 // Show the dialog.
                 editFolderDialogPointer->show();
@@ -769,7 +769,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
     finalBookmarkFolderMenuActionList.prepend(new QPair<QMenu *, QAction *>(menuPointer, deleteFolderActionPointer));
 }
 
-void BrowserWindow::addOrEditDomainSettings() const
+void BrowserWindow::addOrEditDomainSettings()
 {
     // Remove the focus from the URL line edit.
     urlLineEditPointer->clearFocus();
@@ -813,7 +813,7 @@ void BrowserWindow::addOrEditDomainSettings() const
     }
 
     // Create the domain settings dialog pointer.
-    DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsName);
+    DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(this, DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsName);
 
     // Reload the tabs when domain settings are updated.
     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
@@ -864,10 +864,10 @@ void BrowserWindow::decrementZoom()
     updateZoomActions(currentZoomFactorDouble);
 }
 
-void BrowserWindow::editBookmarks() const
+void BrowserWindow::editBookmarks()
 {
     // Instantiate an edit bookmarks dialog.
-    BookmarksDialog *bookmarksDialogPointer = new BookmarksDialog(tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(), tabWidgetPointer->getCurrentTabFavoritIcon());
+    BookmarksDialog *bookmarksDialogPointer = new BookmarksDialog(this, tabWidgetPointer->getCurrentTabFavoritIcon(), tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl());
 
     // Update the displayed bookmarks when edited.
     connect(bookmarksDialogPointer, SIGNAL(bookmarkUpdated()), this, SLOT(populateBookmarksInAllWindows()));
@@ -1397,7 +1397,7 @@ void BrowserWindow::showBookmarkContextMenu(const QPoint &point)
                     QIcon currentTabFavoriteIcon = tabWidgetPointer->getCurrentTabFavoritIcon();
 
                     // Instantiate an edit bookmark dialog.
-                    QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(databaseId, currentTabFavoriteIcon);
+                    QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(this, databaseId, currentTabFavoriteIcon);
 
                     // Show the dialog.
                     editBookmarkDialogPointer->show();
@@ -1481,39 +1481,13 @@ void BrowserWindow::showCookiesDialog()
     connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), tabWidgetPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
 }
 
-void BrowserWindow::showDownloadDirectoryBrowseDialog() const
-{
-    // Get the current download directory.
-    QString currentDownloadDirectory = downloadDirectoryComboBoxPointer->currentText();
-
-    // Resolve the system download directory if specified.
-    if (currentDownloadDirectory == QStringLiteral("System Download Directory"))
-        currentDownloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
-
-    // Get the new download directory.
-    QString newDownloadDirectory = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download directory dialog caption", "Select Download Directory"),
-                                                                     currentDownloadDirectory);
-
-    // Populate the download directory combo box according to the new download location.
-    if (newDownloadDirectory == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation))  // The default download location was selected.
-    {
-        // Populate the download location with the default text.
-        downloadDirectoryComboBoxPointer->setCurrentText("System Download Directory");
-    }
-    else if (newDownloadDirectory != QStringLiteral(""))  // A different directory was selected.
-    {
-        // Populate the download location.
-        downloadDirectoryComboBoxPointer->setCurrentText(newDownloadDirectory);
-    }
-}
-
-void BrowserWindow::showDomainSettingsDialog() const
+void BrowserWindow::showDomainSettingsDialog()
 {
     // Remove the focus from the URL line edit.
     urlLineEditPointer->clearFocus();
 
     // Instantiate the domain settings dialog.
-    DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
+    DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(this);
 
     // Reload the tabs when domain settings are updated.
     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
@@ -1558,186 +1532,28 @@ void BrowserWindow::showProgressBar(const int &progress) const
 
 void BrowserWindow::showSettingsDialog()
 {
-    // Create the settings widgets.
-    QWidget *privacySettingsWidgetPointer = new QWidget;
-    QWidget *generalSettingsWidgetPointer = new QWidget;
-    QWidget *spellCheckSettingsWidgetPointer = new QWidget;
-
-    // Instantiate the settings UI.
-    Ui::PrivacySettings privacySettingsUi;
-    Ui::GeneralSettings generalSettingsUi;
-    Ui::SpellCheckSettings spellCheckSettingsUi;
-
-    // Setup the UI to display the settings widgets.
-    privacySettingsUi.setupUi(privacySettingsWidgetPointer);
-    generalSettingsUi.setupUi(generalSettingsWidgetPointer);
-    spellCheckSettingsUi.setupUi(spellCheckSettingsWidgetPointer);
-
-    // Get handles for the widgets.
-    QCheckBox *javaScriptCheckBoxPointer = privacySettingsUi.kcfg_javaScriptEnabled;
-    QCheckBox *localStorageCheckBoxPointer = privacySettingsUi.kcfg_localStorageEnabled;
-    QCheckBox *domStorageCheckBoxPointer = privacySettingsUi.kcfg_domStorageEnabled;
-    QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
-    userAgentLabelPointer = privacySettingsUi.userAgentLabel;
-    QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
-    searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
-    downloadDirectoryComboBoxPointer = generalSettingsUi.kcfg_downloadDirectory;
-    QPushButton *browseButtonPointer = generalSettingsUi.browseButton;
-    QListWidget *spellCheckListWidgetPointer = spellCheckSettingsUi.spellCheckListWidget;
-
-    // Create a save spell check languages lambda.
-    auto updateCheckBoxes = [javaScriptCheckBoxPointer, localStorageCheckBoxPointer, domStorageCheckBoxPointer] ()
-    {
-        // Only enable the DOM storage check box if both JavaScript and local storage are checked.
-        domStorageCheckBoxPointer->setEnabled(javaScriptCheckBoxPointer->isChecked() && localStorageCheckBoxPointer->isChecked());
-    };
-
-    // Update the status of the DOM storage check box when either JavaScript or local storage are changed.
-    connect(javaScriptCheckBoxPointer, &QCheckBox::stateChanged, this, updateCheckBoxes);
-    connect(localStorageCheckBoxPointer, &QCheckBox::stateChanged, this, updateCheckBoxes);
-
-    // Populate the combo box labels.
-    updateUserAgentLabel(userAgentComboBoxPointer->currentText());
-    updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
-
-    // Update the labels when the combo boxes change.
-    connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
-    connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
-
-    // Connect the download directory directory browse button.
-    connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadDirectoryBrowseDialog()));
-
-    // Create a dictionaries QDir from the `QTWEBENGINE_DICTIONARIES_PATH` environment variable.
-    QDir dictionariesDir = QDir(qEnvironmentVariable("QTWEBENGINE_DICTIONARIES_PATH"));
-
-    // Get a dictionaries string list.
-    QStringList dictionariesStringList = dictionariesDir.entryList(QStringList(QLatin1String("*.bdic")), QDir::Files | QDir::NoSymLinks);
-
-    // Remove the `.bdic` file extensions from the dictionaries list.
-    dictionariesStringList.replaceInStrings(QLatin1String(".bdic"), QLatin1String(""));
-
-    // Get a list of the enabled spell check languages.
-    QStringList enabledSpellCheckLanguagesList = Settings::spellCheckLanguages();
-
-    // Add each dictionary to the spell check list widget.
-    foreach(QString dictionaryString, dictionariesStringList)
-    {
-        // Create a new list widget item pointer.
-        QListWidgetItem *listWidgetItemPointer = new QListWidgetItem();
-
-        // Create a dictionary check box widget with the name of the dictionary string.
-        QCheckBox *dictionaryCheckBoxWidget = new QCheckBox(dictionaryString);
-
-        // Check the language if it is currently enabled.
-        if (enabledSpellCheckLanguagesList.contains(dictionaryString))
-            dictionaryCheckBoxWidget->setCheckState(Qt::Checked);
-        else
-            dictionaryCheckBoxWidget->setCheckState(Qt::Unchecked);
-
-        // Add the list widget item to the spell check list widget.
-        spellCheckListWidgetPointer->addItem(listWidgetItemPointer);
-
-        // Set the list widget item check box widget.
-        spellCheckListWidgetPointer->setItemWidget(listWidgetItemPointer, dictionaryCheckBoxWidget);
-    }
-
     // Get a handle for the KConfig skeleton.
     KConfigSkeleton *kConfigSkeletonPointer = Settings::self();
 
-    // Instantiate a settings config dialog from the settings.kcfg file.
-    configDialogPointer = new KConfigDialog(this, QLatin1String("settings"), kConfigSkeletonPointer);
+    // Instantiate a settings dialog.
+    SettingsDialog *settingsDialogPointer = new SettingsDialog(this, kConfigSkeletonPointer);
 
-    // Create a settings icon string.
-    QString settingsIconString;
-
-    // Get a settings icon that matches the theme.
-    if (QIcon::hasThemeIcon("breeze-settings"))
-    {
-        // KDE uses breeze-settings.
-        settingsIconString = QLatin1String("breeze-settings");
-    }
-    else
-    {
-        // Gnome uses preferences-desktop.
-        settingsIconString = QLatin1String("preferences-desktop");
-    }
-
-    // Add the settings widgets as config dialog pages.
-    configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("Settings tab title", "Privacy"), QLatin1String("privacybrowser"));
-    configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("Settings tab title", "General"), settingsIconString);
-    configDialogPointer->addPage(spellCheckSettingsWidgetPointer, i18nc("Settings tab title", "Spell Check"), QLatin1String("tools-check-spelling"));
-
-    // Get handles for the buttons.
-    QPushButton *applyButtonPointer = configDialogPointer->button(QDialogButtonBox::Apply);
-    QPushButton *okButtonPointer = configDialogPointer->button(QDialogButtonBox::Ok);
-
-    // Prevent interaction with the parent window while the dialog is open.
-    configDialogPointer->setWindowModality(Qt::WindowModal);
-
-    // Make it so.
-    configDialogPointer->show();
+    // Show the dialog
+    settingsDialogPointer->show();
 
     // TODO.  KConfigDialog does not respect expanding size policies.  <https://redmine.stoutner.com/issues/823>
-    //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+    //settingsDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-    //configDialogPointer->adjustSize();
+    //settingsDialogPointer->adjustSize();
 
     // Expand the config dialog.
-    configDialogPointer->resize(1000, 500);
-
-    // Create a save spell check languages lambda.
-    auto saveSpellCheckLanguages = [spellCheckListWidgetPointer, kConfigSkeletonPointer, this] ()
-    {
-        // Create a list of enabled languages.
-        QStringList newSpellCheckLanguages = QStringList();
-
-        // Get a count of all the languages.
-        int allLanguagesCount = spellCheckListWidgetPointer->count();
-
-        // Get a list of all the checked languages.
-        for (int i = 0; i < allLanguagesCount; ++i) {
-            // Get the language item.
-            QListWidgetItem *languageItemPointer = spellCheckListWidgetPointer->item(i);
-
-            // Get the language check box.
-            QCheckBox *languageCheckBoxPointer = qobject_cast<QCheckBox*>(spellCheckListWidgetPointer->itemWidget(languageItemPointer));
-
-            // Add the item to the enabled languages if it is checked.
-            if (languageCheckBoxPointer->checkState() == Qt::Checked)
-            {
-                // Get the text.
-                QString languageString = languageCheckBoxPointer->text();
-
-                // Remove all instances of `&`, which may have been added automatically when creating the check box text.
-                languageString.remove(QChar('&'));
-
-                // Add the language string to the list.
-                newSpellCheckLanguages.append(languageString);
-            }
-        }
-
-        // Update the spell check languages.
-        if (Settings::spellCheckLanguages() != newSpellCheckLanguages)
-        {
-            // Update the spell check languages.
-            Settings::setSpellCheckLanguages(newSpellCheckLanguages);
-
-            // Write the settings to disk.
-            kConfigSkeletonPointer->save();
-        }
-
-        // Apply the spell check languages.
-        tabWidgetPointer->applySpellCheckLanguages();
-    };
-
-    // Process
-    connect(applyButtonPointer, &QPushButton::clicked, this, saveSpellCheckLanguages);
-    connect(okButtonPointer, &QPushButton::clicked, this, saveSpellCheckLanguages);
+    settingsDialogPointer->resize(1000, 500);
 
     // Apply the settings handled by KConfig.
-    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyApplicationSettings()));
-    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
+    connect(settingsDialogPointer, SIGNAL(spellCheckLanguagesUpdated()), tabWidgetPointer, SLOT(applySpellCheckLanguages()));
+    connect(settingsDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyApplicationSettings()));
+    connect(settingsDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
 }
 
 QSize BrowserWindow::sizeHint() const
@@ -2081,12 +1897,6 @@ void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const
     }
 }
 
-void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
-{
-    // Update the search engine label.
-    searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
-}
-
 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
 {
     // Get the new URL string in encoded form, which displays punycode.
@@ -2265,12 +2075,6 @@ void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool
     }
 }
 
-void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
-{
-    // Update the user agent label.
-    userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
-}
-
 void BrowserWindow::updateViewBookmarksToolBarCheckbox(const bool visible)
 {
     // Update the view bookmarks toolbar checkbox.