]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Enable Domain Settings with wildcard subdomains.
authorSoren Stoutner <soren@stoutner.com>
Sat, 19 Mar 2022 20:27:25 +0000 (13:27 -0700)
committerSoren Stoutner <soren@stoutner.com>
Sat, 19 Mar 2022 20:27:25 +0000 (13:27 -0700)
src/helpers/DomainsDatabaseHelper.cpp
src/views/BrowserView.cpp

index b801a0bf4047882f9895637076012608e2fc4201..5de297d229fe688da8a4aa00d9eb00ca4ca48f3e 100644 (file)
@@ -114,20 +114,60 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
     // Get a handle for the domains database.
     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
 
     // Get a handle for the domains database.
     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
 
-    // Instantiate a domain lookup query.
-    QSqlQuery domainLookupQuery(domainsDatabase);
+    // Instantiate the all domain names query.
+    QSqlQuery allDomainNamesQuery(domainsDatabase);
+
+    // Set the query to be forward only (increases performance while iterating over the query).
+    allDomainNamesQuery.setForwardOnly(true);
+
+    // Prepare the query.
+    allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
+
+    // Execute the query.
+    allDomainNamesQuery.exec();
+
+    // Create a domains settings map.
+    QMap<QString, int> domainSettingsMap;
+
+    // Populate the domain settings map.
+    while (allDomainNamesQuery.next())
+    {
+        // Add the domain name and database ID to the map.
+        domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(),
+                                 allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt());
+    }
+
+    // Initialize the database ID tracker.
+    int databaseId = -1;
 
 
-    // Create a hostname field.
-    QSqlField hostnameField(DOMAIN_NAME, QVariant::String);
+    // Get the database ID if the hostname is found in the domain settings set.
+    if (domainSettingsMap.contains(hostname))
+    {
+        databaseId = domainSettingsMap.value(hostname);
+    }
+
+    // Create a subdomain string.
+    QString subdomain = hostname;
+
+    // Check all the subdomains of the hostname.
+    while ((databaseId == -1) && subdomain.contains("."))  // Stop checking when a match is found or there are no more `.` in the hostname.
+    {
+        // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
+        if (domainSettingsMap.contains("*." + subdomain))
+        {
+            // Get the database ID.
+            databaseId = domainSettingsMap.value("*." + subdomain);
+        }
 
 
-    // Set the hostname field value.
-    hostnameField.setValue(hostname);
+        // Strip out the first subdomain.
+        subdomain = subdomain.section('.', 1);
+    }
 
 
-    // SQL escape the hostname field.
-    QString sqlEscapedHostname = domainsDatabase.driver()->formatValue(hostnameField);
+    // Instantiate the domain lookup query.
+    QSqlQuery domainLookupQuery(domainsDatabase);
 
     // Prepare the domain lookup query.
 
     // Prepare the domain lookup query.
-    domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + DOMAIN_NAME + " = " + sqlEscapedHostname);
+    domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
 
     // Execute the query.
     domainLookupQuery.exec();
 
     // Execute the query.
     domainLookupQuery.exec();
@@ -138,4 +178,3 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
     // Return the query.
     return domainLookupQuery;
 }
     // Return the query.
     return domainLookupQuery;
 }
-
index aa9ffffa18132bb0d6d18d5802758a1a0310183b..3a116e267344aef1d6496cee9c781577ecf5f0cc 100644 (file)
@@ -78,7 +78,7 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     domainSettingsPalette = urlLineEditPointer->palette();
 
     // Modify the domain settings palette.
     domainSettingsPalette = urlLineEditPointer->palette();
 
     // Modify the domain settings palette.
-    domainSettingsPalette.setColor(QPalette::Base, Qt::green);
+    domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
 
     // Instantiate the mouse event pointer.
     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
 
     // Instantiate the mouse event pointer.
     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);