From cca335d6b9751fbf0e87daa5f122a1b8770488c8 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 11 Apr 2022 13:57:50 -0700 Subject: [PATCH] Create an add or edit domain settings action. --- src/dialogs/DomainSettingsDialog.cpp | 112 ++++++++++++++++++--------- src/dialogs/DomainSettingsDialog.h | 8 +- src/views/BrowserView.cpp | 6 +- src/views/BrowserView.h | 4 +- src/windows/BrowserWindow.cpp | 59 ++++++++++++-- src/windows/BrowserWindow.h | 9 ++- 6 files changed, 145 insertions(+), 53 deletions(-) diff --git a/src/dialogs/DomainSettingsDialog.cpp b/src/dialogs/DomainSettingsDialog.cpp index 462b22d..b7f2c93 100644 --- a/src/dialogs/DomainSettingsDialog.cpp +++ b/src/dialogs/DomainSettingsDialog.cpp @@ -29,7 +29,12 @@ #include #include -DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent) +// Define the public static int constants. +const int DomainSettingsDialog::SHOW_ALL_DOMAINS = 0; +const int DomainSettingsDialog::ADD_DOMAIN = 1; +const int DomainSettingsDialog::EDIT_DOMAIN = 2; + +DomainSettingsDialog::DomainSettingsDialog(const int &startType, const QString &domainName) : QDialog(nullptr) { // Instantiate the domain settings view UI. Ui::DomainSettingsDialog domainSettingsDialogUi; @@ -79,11 +84,41 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent) // Read the data from the database and apply it to the table model. domainsTableModelPointer->select(); - // Select the first entry in the list view. - domainsListViewPointer->setCurrentIndex(domainsTableModelPointer->index(0, domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME))); + // Setup the dialog according to the start type. + switch (startType) + { + case SHOW_ALL_DOMAINS: + { + // Select the first entry in the list view. + domainsListViewPointer->setCurrentIndex(domainsTableModelPointer->index(0, domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME))); - // Populate the domain settings. - domainSelected(domainsListViewPointer->selectionModel()->currentIndex()); + // Populate the domain settings. + domainSelected(domainsListViewPointer->selectionModel()->currentIndex()); + + break; + } + + case ADD_DOMAIN: + { + // Add the new domain. + addDomain(domainName); + + break; + } + + case EDIT_DOMAIN: + { + // Find the index for the new domain. `1` returns the first match. + QModelIndexList newDomainIndex = domainsTableModelPointer->match(domainsTableModelPointer->index(0, domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME)), + Qt::DisplayRole, domainName, 1, Qt::MatchWrap); + + // Move to the new domain. + domainsListViewPointer->setCurrentIndex(newDomainIndex[0]); + + // Populate the domain settings. + domainSelected(domainsListViewPointer->selectionModel()->currentIndex()); + } + } // Handle clicks on the domains. connect(domainsListViewPointer, SIGNAL(activated(QModelIndex)), this, SLOT(domainSelected(QModelIndex))); @@ -108,6 +143,40 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent) updateUi(); } +void DomainSettingsDialog::addDomain(const QString &domainName) const +{ + // Create a new domain record. + QSqlRecord newDomainRecord = QSqlDatabase::database(DomainsDatabaseHelper::CONNECTION_NAME).record(DomainsDatabaseHelper::DOMAINS_TABLE); + + // Set the values for the new domain. + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME), domainName); + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT), DomainsDatabaseHelper::SYSTEM_DEFAULT); + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::LOCAL_STORAGE), DomainsDatabaseHelper::SYSTEM_DEFAULT); + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT), UserAgentHelper::SYSTEM_DEFAULT_DATABASE); + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::ZOOM_FACTOR), DomainsDatabaseHelper::SYSTEM_DEFAULT); + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR), 1.0); + + // Insert the new domain. `-1` appends it to the end. + domainsTableModelPointer->insertRecord(-1, newDomainRecord); + + // Submit all pending changes. + domainsTableModelPointer->submitAll(); + + // Find the index for the new domain. `-1` allows for multiple entries to be returned. + QModelIndexList newDomainIndex = domainsTableModelPointer->match(domainsTableModelPointer->index(0, domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME)), + Qt::DisplayRole, domainName, -1, Qt::MatchWrap); + + // Move to the new domain. If there are multiple domains with the same name, the new one should be the last in the list. + domainsListViewPointer->setCurrentIndex(newDomainIndex[newDomainIndex.size() - 1]); + + // Populate the domain settings. + domainSelected(domainsListViewPointer->selectionModel()->currentIndex()); + + // Update the UI. + updateUi(); +} + + void DomainSettingsDialog::apply() const { // Get the current index. @@ -356,38 +425,7 @@ void DomainSettingsDialog::showAddMessageBox() QLineEdit::Normal, QString(), &okClicked); // Add the new domain if the user clicked OK. - if (okClicked) - { - // Create a new domain record. - QSqlRecord newDomainRecord = QSqlDatabase::database(DomainsDatabaseHelper::CONNECTION_NAME).record(DomainsDatabaseHelper::DOMAINS_TABLE); - - // Set the values for the new domain. - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME), newDomainName); - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT), DomainsDatabaseHelper::SYSTEM_DEFAULT); - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::LOCAL_STORAGE), DomainsDatabaseHelper::SYSTEM_DEFAULT); - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT), UserAgentHelper::SYSTEM_DEFAULT_DATABASE); - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::ZOOM_FACTOR), DomainsDatabaseHelper::SYSTEM_DEFAULT); - newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR), 1.0); - - // Insert the new domain. `-1` appends it to the end. - domainsTableModelPointer->insertRecord(-1, newDomainRecord); - - // Submit all pending changes. - domainsTableModelPointer->submitAll(); - - // Find the index for the new domain. `-1` allows for multiple entries to be returned. - QModelIndexList newDomainIndex = domainsTableModelPointer->match(domainsTableModelPointer->index(0, domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME)), - Qt::DisplayRole, newDomainName, -1, Qt::MatchWrap); - - // Move to the new domain. If there are multiple domains with the same name, the new one should be the last in the list. - domainsListViewPointer->setCurrentIndex(newDomainIndex[newDomainIndex.size() - 1]); - - // Populate the domain settings. - domainSelected(domainsListViewPointer->selectionModel()->currentIndex()); - - // Update the UI. - updateUi(); - } + if (okClicked) addDomain(newDomainName); } void DomainSettingsDialog::showDeleteMessageBox() const diff --git a/src/dialogs/DomainSettingsDialog.h b/src/dialogs/DomainSettingsDialog.h index 41438ce..5663787 100644 --- a/src/dialogs/DomainSettingsDialog.h +++ b/src/dialogs/DomainSettingsDialog.h @@ -36,7 +36,12 @@ class DomainSettingsDialog : public QDialog public: // The primary constructor. - explicit DomainSettingsDialog(QWidget *parent = nullptr); + explicit DomainSettingsDialog(const int &startType = SHOW_ALL_DOMAINS, const QString &domainName = QStringLiteral("")); + + // The public static int constants. + static const int SHOW_ALL_DOMAINS; + static const int ADD_DOMAIN; + static const int EDIT_DOMAIN; signals: void domainSettingsUpdated() const; @@ -76,6 +81,7 @@ private: QComboBox *zoomFactorComboBoxPointer; // The private functions. + void addDomain(const QString &domainName) const; void populateJavaScriptLabel() const; void populateLocalStorageLabel() const; void populateUserAgentLabel(const QString &userAgentName) const; diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index 933a300..edcd26c 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -209,7 +209,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(currentZoomFactor); // Apply the domain settings palette to the URL line edit. - emit updateDomainSettingsIndicator(true); + emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString()); } else // The hostname does not have domain settings. { @@ -229,7 +229,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); // Apply the no domain settings palette to the URL line edit. - emit updateDomainSettingsIndicator(false); + emit updateDomainSettingsIndicator(false, QStringLiteral("")); } // Emit the update actions signals. @@ -405,7 +405,7 @@ void BrowserView::toggleLocalStorage() const void BrowserView::updateUrl(const QUrl &url) const { // Update the URL line edit. - emit updateUrlLineEdit(url.toString()); + emit updateUrlLineEdit(url); // Update the status of the forward and back buttons. emit updateBackAction(webEngineHistoryPointer->canGoBack()); diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index 1401468..3c3bd7d 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -50,12 +50,12 @@ signals: void linkHovered(const QString &linkUrl) const; void showProgressBar(const int &progress) const; void updateBackAction(const bool &isEnabled) const; - void updateDomainSettingsIndicator(const bool status) const; + void updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain) const; void updateForwardAction(const bool &isEnabled) const; void updateJavaScriptAction(const bool &isEnabled) const; void updateLocalStorageAction(const bool &isEnabled) const; void updateSearchEngineActions(const QString &searchEngine) const; - void updateUrlLineEdit(const QString &newUrl) const; + void updateUrlLineEdit(const QUrl &newUrl) const; void updateUserAgentActions(const QString &userAgent) const; void updateZoomFactorAction(const double &zoomFactor) const; diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 13f470e..4461419 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -148,7 +148,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo"))); searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search"))); zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom"))); - domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("network-server-symbolic"))); + domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure"))); // Update the on-the-fly menus. connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString)), this, SLOT(updateUserAgentActions(QString))); @@ -182,6 +182,12 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Create a URL line edit. urlLineEditPointer = new KLineEdit(); + // Add an edit or add domain settings action to the URL line edit. + QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition); + + // Add or edit the current domain settings. + connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings())); + // Populate the URL toolbar. urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer); @@ -189,7 +195,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString))); // Update the URL line edit on page loads. - connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QString)), this, SLOT(updateUrlLineEdit(QString))); + connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl))); // Get a handle for the status bar. QStatusBar *statusBarPointer = statusBar(); @@ -215,12 +221,45 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9")); // Update the applied palette. - connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool)), this, SLOT(updateDomainSettingsIndicator(bool))); + connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString))); // Load the initial website. browserViewPointer->loadInitialWebsite(); } +void BrowserWindow::addOrEditDomainSettings() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Create the domain settings dialog pointer. + DomainSettingsDialog *domainSettingsDialogPointer; + + // Run the commands according to the current domain settings status. + if (currentDomainSettingsDomain == "") // Domain settings are not currently applied. + { + // Instruct the domain settings dialog to add a new domain. + domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host()); + } + else // Domain settings are currently applied. + { + // Instruct the domain settings dialog to edit the current domain. + domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain); + } + + // Set the dialog window title. + domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings")); + + // Set the modality. + domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);; + + // Show the dialog. + domainSettingsDialogPointer->show(); + + // Reload the tabs when domain settings are updated. + connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload())); +} + void BrowserWindow::back() const { // Remove the focus from the URL line edit. @@ -292,7 +331,7 @@ void BrowserWindow::openDomainSettings() const // Remove the focus from the URL line edit. urlLineEditPointer->clearFocus(); - // Instantiate the domain settings window. + // Instantiate the domain settings dialog. DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog(); // Set the dialog window title. @@ -425,11 +464,14 @@ void BrowserWindow::toggleLocalStorage() const browserViewPointer->toggleLocalStorage(); } -void BrowserWindow::updateDomainSettingsIndicator(const bool &status) const +void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain) { // Set the domain palette according to the status. if (status) urlLineEditPointer->setPalette(domainSettingsPalette); else urlLineEditPointer->setPalette(noDomainSettingsPalette); + + // Store the domain. + currentDomainSettingsDomain = domainSettingsDomain; } void BrowserWindow::updateJavaScriptAction(const bool &isEnabled) const @@ -560,14 +602,17 @@ void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) c searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString)); } -void BrowserWindow::updateUrlLineEdit(const QString &newUrl) const +void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl) { // Update the URL line edit if it does not have focus. if (!urlLineEditPointer->hasFocus()) { // Update the URL line edit. - urlLineEditPointer->setText(newUrl); + urlLineEditPointer->setText(newUrl.toString()); } + + // Store the current URL. + currentUrl = newUrl; } void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index b3630db..e4eee69 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -45,6 +45,7 @@ public: private Q_SLOTS: // The private slots. + void addOrEditDomainSettings() const; void back() const; void fileNew() const; void forward() const; @@ -57,23 +58,25 @@ private Q_SLOTS: void showProgressBar(const int &progress) const; void toggleJavaScript() const; void toggleLocalStorage() const; - void updateDomainSettingsIndicator(const bool &status) const; + void updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain); void updateJavaScriptAction(const bool &isEnabled) const; void updateLocalStorageAction(const bool &isEnabled) const; void updateSearchEngineActions(const QString &searchEngine) const; void updateUserAgentActions(const QString &userAgent) const; void updateZoomFactorAction(const double &zoomFactor); void updateSearchEngineLabel(const QString &searchEngineString) const; - void updateUrlLineEdit(const QString &newUrl) const; + void updateUrlLineEdit(const QUrl &newUrl); void updateUserAgentLabel(const QString &userAgentDatabaseName) const; private: // The private variables. BrowserView *browserViewPointer; KConfigDialog *configDialogPointer; + QString currentDomainSettingsDomain; + QUrl currentUrl; + double currentZoomFactor; QAction *domainSettingsActionPointer; QPalette domainSettingsPalette; - double currentZoomFactor; QAction *javaScriptActionPointer; QAction *localStorageActionPointer; QPalette noDomainSettingsPalette; -- 2.45.2