From: Soren Stoutner Date: Sat, 19 Mar 2022 20:27:25 +0000 (-0700) Subject: Enable Domain Settings with wildcard subdomains. X-Git-Tag: v0.1~52 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=30f550d27093a50e541e57b6e04b77983c7acbb3;p=PrivacyBrowserPC.git Enable Domain Settings with wildcard subdomains. --- diff --git a/src/helpers/DomainsDatabaseHelper.cpp b/src/helpers/DomainsDatabaseHelper.cpp index b801a0b..5de297d 100644 --- a/src/helpers/DomainsDatabaseHelper.cpp +++ b/src/helpers/DomainsDatabaseHelper.cpp @@ -114,20 +114,60 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname) // 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 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. - 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(); @@ -138,4 +178,3 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname) // Return the query. return domainLookupQuery; } - diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index aa9ffff..3a116e2 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -78,7 +78,7 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) 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);