2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "DomainsDatabaseHelper.h"
22 #include "UserAgentHelper.h"
24 // Define the public static domain constants.
25 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
26 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
28 // Define the private static schema constants.
29 const int DomainsDatabaseHelper::SCHEMA_VERSION = 2;
31 // Define the public static database field names.
32 const QString DomainsDatabaseHelper::_ID = "_id";
33 const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name";
34 const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript";
35 const QString DomainsDatabaseHelper::USER_AGENT = "user_agent";
37 // The default constructor.
38 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
40 void DomainsDatabaseHelper::addDatabase()
42 // Add the domain settings database.
43 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
45 // Set the database name.
46 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
49 if (domainsDatabase.open()) // Opening the database succeeded.
51 // Check to see if the domains table already exists.
52 if (domainsDatabase.tables().contains(DOMAINS_TABLE))
54 // Query the database schema version.
55 QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
57 // Move to the first record.
58 getSchemaVersionQuery.first();
60 // Get the current schema version.
61 int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
63 // Check to see if the schama has been updated.
64 if (SCHEMA_VERSION > currentSchemaVersion)
66 // Run schema update code.
67 switch (currentSchemaVersion)
69 // Upgrade from schema version 0.
72 // Add the JavaScript column.
73 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
75 // Set the default value.
76 domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" );
81 // Upgrade from schema version 1.
84 // Add the User Agent column.
85 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
89 // Update the schema version.
90 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
95 // Instantiate a create table query.
96 QSqlQuery createTableQuery(domainsDatabase);
98 // Prepare the create table query.
99 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
100 _ID + " INTEGER PRIMARY KEY, " +
101 DOMAIN_NAME + " TEXT, " +
102 JAVASCRIPT + " INTEGER DEFAULT 0," +
103 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "')"
106 // Execute the query.
107 if (!createTableQuery.exec())
110 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
113 // Set the schema version.
114 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
117 else // Opening the database failed.
119 // Write the last database error message to the debug output.
120 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
124 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
126 // Get a handle for the domains database.
127 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
129 // Instantiate the all domain names query.
130 QSqlQuery allDomainNamesQuery(domainsDatabase);
132 // Set the query to be forward only (increases performance while iterating over the query).
133 allDomainNamesQuery.setForwardOnly(true);
135 // Prepare the query.
136 allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
138 // Execute the query.
139 allDomainNamesQuery.exec();
141 // Create a domains settings map.
142 QMap<QString, int> domainSettingsMap;
144 // Populate the domain settings map.
145 while (allDomainNamesQuery.next())
147 // Add the domain name and database ID to the map.
148 domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(),
149 allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt());
152 // Initialize the database ID tracker.
155 // Get the database ID if the hostname is found in the domain settings set.
156 if (domainSettingsMap.contains(hostname))
158 databaseId = domainSettingsMap.value(hostname);
161 // Create a subdomain string.
162 QString subdomain = hostname;
164 // Check all the subdomains of the hostname.
165 while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname.
167 // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
168 if (domainSettingsMap.contains("*." + subdomain))
170 // Get the database ID.
171 databaseId = domainSettingsMap.value("*." + subdomain);
174 // Strip out the first subdomain.
175 subdomain = subdomain.section('.', 1);
178 // Instantiate the domain lookup query.
179 QSqlQuery domainLookupQuery(domainsDatabase);
181 // Prepare the domain lookup query.
182 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
184 // Execute the query.
185 domainLookupQuery.exec();
187 // Move to the first entry.
188 domainLookupQuery.first();
191 return domainLookupQuery;