]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/DomainSettingsDialog.cpp
Create an add or edit domain settings action.
[PrivacyBrowserPC.git] / src / dialogs / DomainSettingsDialog.cpp
index e3745b52b0c870a500b47c6f5d67243a0c604b31..b7f2c93416038293e5ff79a637a3987735238d5d 100644 (file)
 #include <QMessageBox>
 #include <QPushButton>
 
-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;
@@ -43,6 +48,8 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent)
     domainNameLineEditPointer = domainSettingsDialogUi.domainNameLineEdit;
     javaScriptComboBoxPointer = domainSettingsDialogUi.javaScriptComboBox;
     javaScriptLabelPointer = domainSettingsDialogUi.javaScriptLabel;
+    localStorageComboBoxPointer = domainSettingsDialogUi.localStorageComboBox;
+    localStorageLabelPointer = domainSettingsDialogUi.localStorageLabel;
     userAgentComboBoxPointer = domainSettingsDialogUi.userAgentComboBox;
     userAgentLabelPointer = domainSettingsDialogUi.userAgentLabel;
     zoomFactorComboBoxPointer = domainSettingsDialogUi.zoomFactorComboBox;
@@ -77,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)));
@@ -89,6 +126,7 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent)
     // Connect the domain settings.
     connect(domainNameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(domainNameChanged(QString)));
     connect(javaScriptComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(javaScriptChanged(int)));
+    connect(localStorageComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(localStorageChanged(int)));
     connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(QString)), this, SLOT(userAgentChanged(QString)));
     connect(zoomFactorComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(zoomFactorComboBoxChanged(int)));
     connect(customZoomFactorSpinBoxPointer, SIGNAL(valueChanged(double)), this, SLOT(customZoomFactorChanged(double)));
@@ -105,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.
@@ -149,7 +221,6 @@ void DomainSettingsDialog::customZoomFactorChanged(const double &newValue) const
     updateUi();
 }
 
-
 void DomainSettingsDialog::domainNameChanged(const QString &updatedDomainName) const
 {
     // Update the domains table model.
@@ -159,7 +230,6 @@ void DomainSettingsDialog::domainNameChanged(const QString &updatedDomainName) c
     updateUi();
 }
 
-
 void DomainSettingsDialog::domainSelected(const QModelIndex &modelIndex) const
 {
     // Populate the domain name line edit pointer.
@@ -168,6 +238,9 @@ void DomainSettingsDialog::domainSelected(const QModelIndex &modelIndex) const
     // Populate the JavaScript combo box.
     javaScriptComboBoxPointer->setCurrentIndex(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT)).data().toInt());
 
+    // Populate the local storage combo box.
+    localStorageComboBoxPointer->setCurrentIndex(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::LOCAL_STORAGE)).data().toInt());
+
     // Get the user agent string.
     QString userAgent = modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT)).data().toString();
 
@@ -186,14 +259,24 @@ 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();
+    populateLocalStorageLabel();
     populateUserAgentLabel(userAgentComboBoxPointer->currentText());
 
     // Update the UI.
@@ -213,6 +296,18 @@ void DomainSettingsDialog::javaScriptChanged(const int &newIndex) const
     updateUi();
 }
 
+void DomainSettingsDialog::localStorageChanged(const int &newIndex) const
+{
+    // Update the domains table model.
+    domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::LOCAL_STORAGE)),
+                                      newIndex);
+
+    // Populate the local storage label.
+    populateLocalStorageLabel();
+
+    // Update the UI.
+    updateUi();
+}
 
 void DomainSettingsDialog::ok()
 {
@@ -234,14 +329,8 @@ void DomainSettingsDialog::populateJavaScriptLabel() const
         case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
         {
             // Set the text according to the system default.
-            if (Settings::javaScript())
-            {
-                javaScriptLabelPointer->setText(i18nc("Domains settings labels", "JavaScript enabled"));
-            }
-            else
-            {
-                javaScriptLabelPointer->setText(i18nc("Domain settings labels", "JavaScript disabled"));
-            }
+            if (Settings::javaScript()) javaScriptLabelPointer->setText(i18nc("Domains settings label", "JavaScript enabled"));
+            else javaScriptLabelPointer->setText(i18nc("Domain settings label", "JavaScript disabled"));
 
             break;
         }
@@ -249,7 +338,7 @@ void DomainSettingsDialog::populateJavaScriptLabel() const
         case (DomainsDatabaseHelper::DISABLED):
         {
             // Set the label text in bold.
-            javaScriptLabelPointer->setText(i18nc("Domain settings labels.  The <strong> tags should be retained.", "<strong>JavaScript disabled</strong>"));
+            javaScriptLabelPointer->setText(i18nc("Domain settings label.  The <strong> tags should be retained.", "<strong>JavaScript disabled</strong>"));
 
             break;
         }
@@ -257,7 +346,39 @@ void DomainSettingsDialog::populateJavaScriptLabel() const
         case (DomainsDatabaseHelper::ENABLED):
         {
             // Set the label text in bold.
-            javaScriptLabelPointer->setText(i18nc("Domains settings labels.  The <strong> tags should be retained.", "<strong>JavaScript enabled</strong>"));
+            javaScriptLabelPointer->setText(i18nc("Domains settings label.  The <strong> tags should be retained.", "<strong>JavaScript enabled</strong>"));
+
+            break;
+        }
+    }
+}
+
+void DomainSettingsDialog::populateLocalStorageLabel() const
+{
+    // Populate the label according to the currently selected index.
+    switch (localStorageComboBoxPointer->currentIndex())
+    {
+        case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
+        {
+            // Set the text according to the system default.
+            if (Settings::localStorage()) localStorageLabelPointer->setText(i18nc("Local storage label", "Local storage enabled"));
+            else localStorageLabelPointer->setText(i18nc("Local storage label", "Local storage disabled"));
+
+            break;
+        }
+
+        case (DomainsDatabaseHelper::DISABLED):
+        {
+            // Set the label text in bold.
+            localStorageLabelPointer->setText(i18nc("Local storage label.  The <string> tags should be retained.", "<strong>Local storage disabled</strong>"));
+
+            break;
+        }
+
+        case (DomainsDatabaseHelper::ENABLED):
+        {
+            // Set the label text in bold.
+            localStorageLabelPointer->setText(i18nc("Local storage label.  The <strong> tags should be retained.", "<strong>Local storage enabled</strong>"));
 
             break;
         }
@@ -304,37 +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::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
@@ -428,12 +519,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();