]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/DomainSettingsDialog.cpp
Create new domain settings with the currently applied settings. https://redmine...
[PrivacyBrowserPC.git] / src / dialogs / DomainSettingsDialog.cpp
index 82f0f64b890af85781c3187a5f3ecf7902d04f2a..59d7bdb073628886be1e964237a208df26f03adc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
  *
@@ -31,8 +31,7 @@
 
 // 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;
+const int DomainSettingsDialog::EDIT_DOMAIN = 1;
 
 // Construct the class.
 DomainSettingsDialog::DomainSettingsDialog(const int &startType, const QString &domainName) : QDialog(nullptr)
@@ -133,17 +132,6 @@ DomainSettingsDialog::DomainSettingsDialog(const int &startType, const QString &
             break;
         }
 
-        case ADD_DOMAIN:
-        {
-            // Add the new domain.
-            addDomain(domainName);
-
-            // Emit the domain settings updated signal after 100 milliseconds.  This is necessary because the browser window takes time to process the connect command to receive the signal.
-            QTimer::singleShot(100, [this] () { emit domainSettingsUpdated();});
-
-            break;
-        }
-
         case EDIT_DOMAIN:
         {
             // Find the index for the new domain.  `1` returns the first match.
@@ -184,43 +172,6 @@ DomainSettingsDialog::DomainSettingsDialog(const int &startType, const QString &
     updateUi();
 }
 
-void DomainSettingsDialog::addDomain(const QString &domainName) const
-{
-    // Create a new domain record.
-    QSqlRecord newDomainRecord = QSqlDatabase::database(DomainsDatabase::CONNECTION_NAME).record(DomainsDatabase::DOMAINS_TABLE);
-
-    // Set the values for the new domain.
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::DOMAIN_NAME), domainName);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::JAVASCRIPT), DomainsDatabase::SYSTEM_DEFAULT);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::LOCAL_STORAGE), DomainsDatabase::SYSTEM_DEFAULT);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::DOM_STORAGE), DomainsDatabase::SYSTEM_DEFAULT);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::USER_AGENT), UserAgentHelper::SYSTEM_DEFAULT_DATABASE);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::ZOOM_FACTOR), DomainsDatabase::SYSTEM_DEFAULT);
-    newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabase::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(DomainsDatabase::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(domainsSelectionModelPointer->currentIndex());
-
-    // Update the UI.
-    updateUi();
-
-    // Emit the domain settings updated signal.
-    emit domainSettingsUpdated();
-}
-
 void DomainSettingsDialog::apply() const
 {
     // Get the current index.
@@ -560,7 +511,31 @@ void DomainSettingsDialog::showAddMessageBox()
                                                   QLineEdit::Normal, QString(), &okClicked);
 
     // Add the new domain if the user clicked OK.
-    if (okClicked) addDomain(newDomainName);
+    if (okClicked)
+    {
+        // Add the new domain.
+        DomainsDatabase::addDomain(newDomainName);
+
+        // Submit all pending changes.  This reloads the model from the database, getting the new domain added above.
+        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(DomainsDatabase::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(domainsSelectionModelPointer->currentIndex());
+
+        // Update the UI.
+        updateUi();
+
+        // Emit the domain settings updated signal.
+        emit domainSettingsUpdated();
+    }
 }
 
 void DomainSettingsDialog::showDeleteMessageBox() const