X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FDomainSettingsDialog.cpp;h=42c713ab90e6a75046fb88612d2b2cbb4d9aedb6;hp=a8960cda3bfcc964053646cc7aa8cb5d252d2aac;hb=8933c941521c591a962034ecf3486c9143bf1f80;hpb=455108aa18c90514c0dad3c12dfea98180dfb471 diff --git a/src/dialogs/DomainSettingsDialog.cpp b/src/dialogs/DomainSettingsDialog.cpp index a8960cd..42c713a 100644 --- a/src/dialogs/DomainSettingsDialog.cpp +++ b/src/dialogs/DomainSettingsDialog.cpp @@ -29,9 +29,20 @@ #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. + // Set the window title. + setWindowTitle(i18nc("The domain settings dialog window title", "Domain Settings")); + + // Set the window modality. + setWindowModality(Qt::WindowModality::WindowModal);; + + // Instantiate the domain settings dialog UI. Ui::DomainSettingsDialog domainSettingsDialogUi; // Setup the UI. @@ -79,11 +90,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 +149,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. @@ -190,11 +265,20 @@ void DomainSettingsDialog::domainSelected(const QModelIndex &modelIndex) const // Populate the zoom factor combo box. zoomFactorComboBoxPointer->setCurrentIndex(zoomFactorComboBoxIndex); - // Populate the custom zoom factor spin box. - customZoomFactorSpinBoxPointer->setValue(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR)).data().toDouble()); + // Populate the custom zoom factor spin box according to the zoom factor combo box. + if (zoomFactorComboBoxIndex == 0) // System default zoom factor is selected. + { + // Display the default zoom factor. + customZoomFactorSpinBoxPointer->setValue(Settings::zoomFactor()); + } + else // Custom zoom factor is selected. + { + // Display the custom zoom factor from the domain settings. + customZoomFactorSpinBoxPointer->setValue(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR)).data().toDouble()); + } - // Set the initial visibility of the custom zoom factor spin box. - customZoomFactorSpinBoxPointer->setVisible(zoomFactorComboBoxIndex); + // Set the initial status of the custom zoom factor spin box. + customZoomFactorSpinBoxPointer->setEnabled(zoomFactorComboBoxIndex); // Populate the labels. populateJavaScriptLabel(); @@ -313,7 +397,7 @@ void DomainSettingsDialog::populateUserAgentLabel(const QString &userAgentName) if (userAgentName == UserAgentHelper::SYSTEM_DEFAULT_TRANSLATED) { // Display the system default user agent name. - userAgentLabelPointer->setText(UserAgentHelper::getTranslatedUserAgentName(Settings::userAgent())); + userAgentLabelPointer->setText(UserAgentHelper::getTranslatedUserAgentNameFromDatabaseName(Settings::userAgent())); } else { @@ -347,38 +431,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 @@ -393,10 +446,10 @@ void DomainSettingsDialog::showDeleteMessageBox() const deleteDialogMessageBox.setWindowTitle(i18nc("Delete domain dialog title", "Delete Domain")); // Set the text. - deleteDialogMessageBox.setText(i18nc("Delete domain main message", "Delete the current domain?")); + deleteDialogMessageBox.setText(i18nc("Delete domain dialog main message", "Delete the current domain?")); // Set the informative text. - deleteDialogMessageBox.setInformativeText(i18nc("Delete domain secondary message", "Doing so will also save any pending changes that have been made to other domains.")); + deleteDialogMessageBox.setInformativeText(i18nc("Delete domain dialog secondary message", "Doing so will also save any pending changes that have been made to other domains.")); // Set the standard buttons. deleteDialogMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); @@ -407,6 +460,7 @@ void DomainSettingsDialog::showDeleteMessageBox() const // Display the dialog and capture the return value. int returnValue = deleteDialogMessageBox.exec(); + // Delete the domain if instructed. if (returnValue == QMessageBox::Yes) { // Get the current index. @@ -461,7 +515,7 @@ void DomainSettingsDialog::userAgentChanged(const QString &updatedUserAgent) con { // Update the domains table model. domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT)), - UserAgentHelper::getDatabaseUserAgentName(updatedUserAgent)); + UserAgentHelper::getDatabaseUserAgentNameFromTranslatedName(updatedUserAgent)); // Populate the user agent label. populateUserAgentLabel(updatedUserAgent); @@ -472,12 +526,26 @@ void DomainSettingsDialog::userAgentChanged(const QString &updatedUserAgent) con void DomainSettingsDialog::zoomFactorComboBoxChanged(const int &newIndex) const { + // Get the current model index. + QModelIndex modelIndex = domainsListViewPointer->selectionModel()->currentIndex(); + // Update the domains table model. - domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::ZOOM_FACTOR)), - newIndex); + domainsTableModelPointer->setData(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::ZOOM_FACTOR)), newIndex); + + // Populate the custom zoom factor spin box according to the zoom factor combo box. + if (newIndex == 0) // System default zoom factor is selected. + { + // Display the default zoom factor. + customZoomFactorSpinBoxPointer->setValue(Settings::zoomFactor()); + } + else // Custom zoom factor is selected. + { + // Display the custom zoom factor from the domain settings. + customZoomFactorSpinBoxPointer->setValue(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR)).data().toDouble()); + } - // Update the visibility of the custom zoom factor spin box. - customZoomFactorSpinBoxPointer->setVisible(newIndex); + // Update the status of the custom zoom factor spin box. + customZoomFactorSpinBoxPointer->setEnabled(newIndex); // Update the UI. updateUi();