2 * Copyright 2022-2023 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 "DomainsDatabase.h"
22 #include "helpers/UserAgentHelper.h"
24 // Define the private static schema constants.
25 const int DomainsDatabase::SCHEMA_VERSION = 6;
27 // Define the public static constants.
28 const QString DomainsDatabase::CONNECTION_NAME = "domains_database";
29 const QString DomainsDatabase::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
30 const QString DomainsDatabase::DOM_STORAGE = "dom_storage";
31 const QString DomainsDatabase::DOMAIN_NAME = "domain_name";
32 const QString DomainsDatabase::DOMAINS_TABLE = "domains";
33 const QString DomainsDatabase::ID = "_id";
34 const QString DomainsDatabase::JAVASCRIPT = "javascript";
35 const QString DomainsDatabase::LOCAL_STORAGE = "local_storage";
36 const QString DomainsDatabase::USER_AGENT = "user_agent";
37 const QString DomainsDatabase::ZOOM_FACTOR = "zoom_factor";
39 // Construct the class.
40 DomainsDatabase::DomainsDatabase() {}
42 void DomainsDatabase::addDatabase()
44 // Add the domain settings database.
45 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION_NAME);
47 // Set the database name.
48 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
51 if (domainsDatabase.open()) // Opening the database succeeded.
53 // Check to see if the domains table already exists.
54 if (domainsDatabase.tables().contains(DOMAINS_TABLE)) // The domains table already exists.
56 // Query the database schema version.
57 QSqlQuery schemaVersionQuery = domainsDatabase.exec(QStringLiteral("PRAGMA user_version"));
59 // Move to the first record.
60 schemaVersionQuery.first();
62 // Get the current schema version.
63 int currentSchemaVersion = schemaVersionQuery.value(0).toInt();
65 // Check to see if the schema has been updated.
66 if (currentSchemaVersion < SCHEMA_VERSION)
68 // Run the schema update code.
69 switch (currentSchemaVersion)
71 // Upgrade from schema version 0 to schema version 1.
74 // Add the JavaScript column.
75 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
77 // Fall through to the next case.
81 // Upgrade from schema version 1 to schema version 2.
84 // Add the User Agent column.
85 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
87 // Fall through to the next case.
91 // Upgrade from schema version 2 to schema version 3.
94 // Add the Zoom Factor columns.
95 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
96 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
98 // Fall through to the next case.
102 // Upgrade from schema version 3 to schema version 4.
105 // Add the DOM Storage column.
106 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
108 // Fall through to the next case.
112 // Upgrade from schema version 4 to schema version 5.
115 // Add the Local Storage column.
116 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
118 // Fall through to the next case.
122 // Upgrade from schema version 5 to schema version 6.
125 // Instantiate a spinner query.
126 QSqlQuery spinnerQuery(domainsDatabase);
128 // Set the query to be forward only (increases performance while iterating over the query).
129 spinnerQuery.setForwardOnly(true);
131 // Prepare the query.
132 spinnerQuery.prepare("SELECT " + ID + "," + JAVASCRIPT + "," + LOCAL_STORAGE + "," + DOM_STORAGE + " FROM " + DOMAINS_TABLE);
134 // Execute the query.
137 // Update the spinner values so that enabled is 1 and disabled is 2.
138 while (spinnerQuery.next())
140 // Initialize the new spinner values.
141 int newJavaScriptValue = SYSTEM_DEFAULT;
142 int newLocalStorageValue = SYSTEM_DEFAULT;
143 int newDomStorageValue = SYSTEM_DEFAULT;
145 // Update the new JavaScript value if needed.
146 switch (spinnerQuery.value(JAVASCRIPT).toInt())
148 // Disabled used to be 1.
151 // Update the value to be 2.
152 newJavaScriptValue = DISABLED;
157 // Enabled used to be 2.
160 // Update the new value to be 1.
161 newJavaScriptValue = ENABLED;
167 // Update the new local storage value if needed.
168 switch (spinnerQuery.value(LOCAL_STORAGE).toInt())
170 // Disabled used to be 1.
173 // Update the new value to be 2.
174 newLocalStorageValue = DISABLED;
179 // Enabled used to be 2.
182 // Update the new value to be 1.
183 newLocalStorageValue = ENABLED;
189 // Update the new DOM storage value if needed.
190 switch (spinnerQuery.value(DOM_STORAGE).toInt())
192 // Disabled used to be 1.
195 // Update the new value to be 2.
196 newDomStorageValue = DISABLED;
201 // Enabled used to be 2.
204 // Update the new value to be 1.
205 newDomStorageValue = ENABLED;
211 // Create an update spinner query.
212 QSqlQuery updateSpinnerQuery(domainsDatabase);
214 // Prepare the update spinner query.
215 updateSpinnerQuery.prepare("UPDATE " + DOMAINS_TABLE + " SET " +
216 JAVASCRIPT + " = :javascript , " +
217 LOCAL_STORAGE + " = :local_storage , " +
218 DOM_STORAGE + " = :dom_storage " +
219 " WHERE " + ID + " = :id");
222 updateSpinnerQuery.bindValue(":javascript", newJavaScriptValue);
223 updateSpinnerQuery.bindValue(":local_storage", newLocalStorageValue);
224 updateSpinnerQuery.bindValue(":dom_storage", newDomStorageValue);
225 updateSpinnerQuery.bindValue(":id", spinnerQuery.value(ID));
227 // Execute the query.
228 updateSpinnerQuery.exec();
231 // Fall through to the next case.
236 // Update the schema version.
237 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
240 else // The domains table does not exist.
242 // Instantiate a create table query.
243 QSqlQuery createTableQuery(domainsDatabase);
245 // Prepare the create table query.
246 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
247 ID + " INTEGER PRIMARY KEY, " +
248 DOMAIN_NAME + " TEXT, " +
249 JAVASCRIPT + " INTEGER DEFAULT 0, " +
250 LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
251 DOM_STORAGE + " INTEGER DEFAULT 0, " +
252 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
253 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
254 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)");
256 // Execute the query.
257 if (!createTableQuery.exec())
260 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
263 // Set the schema version.
264 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
267 else // Opening the database failed.
269 // Write the last database error message to the debug output.
270 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
274 QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname)
276 // Get a handle for the domains database.
277 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
279 // Instantiate the all domain names query.
280 QSqlQuery allDomainNamesQuery(domainsDatabase);
282 // Set the query to be forward only (increases performance while iterating over the query).
283 allDomainNamesQuery.setForwardOnly(true);
285 // Prepare the query.
286 allDomainNamesQuery.prepare("SELECT " + ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
288 // Execute the query.
289 allDomainNamesQuery.exec();
291 // Create a domains settings map.
292 QMap<QString, int> domainSettingsMap;
294 // Populate the domain settings map.
295 while (allDomainNamesQuery.next())
297 // Add the domain name and database ID to the map.
298 domainSettingsMap.insert(allDomainNamesQuery.value(DOMAIN_NAME).toString(), allDomainNamesQuery.value(ID).toInt());
301 // Initialize the database ID tracker.
304 // Get the database ID if the hostname is found in the domain settings set.
305 if (domainSettingsMap.contains(hostname))
307 databaseId = domainSettingsMap.value(hostname);
310 // Create a subdomain string.
311 QString subdomain = hostname;
313 // Check all the subdomains of the hostname.
314 while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname.
316 // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
317 if (domainSettingsMap.contains("*." + subdomain))
319 // Get the database ID.
320 databaseId = domainSettingsMap.value("*." + subdomain);
323 // Strip out the first subdomain.
324 subdomain = subdomain.section('.', 1);
327 // Instantiate the domain lookup query.
328 QSqlQuery domainLookupQuery(domainsDatabase);
330 // Prepare the domain lookup query.
331 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + ID + " = " + QString::number(databaseId));
333 // Execute the query.
334 domainLookupQuery.exec();
336 // Move to the first entry.
337 domainLookupQuery.first();
340 return domainLookupQuery;