2 * Copyright 2022-2024 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"
23 #include "helpers/UserAgentHelper.h"
25 // Define the private static schema constants.
26 const int DomainsDatabase::SCHEMA_VERSION = 6;
28 // Define the public static constants.
29 const QString DomainsDatabase::CONNECTION_NAME = "domains_database";
30 const QString DomainsDatabase::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
31 const QString DomainsDatabase::DOM_STORAGE = "dom_storage";
32 const QString DomainsDatabase::DOMAIN_NAME = "domain_name";
33 const QString DomainsDatabase::DOMAINS_TABLE = "domains";
34 const QString DomainsDatabase::ID = "_id";
35 const QString DomainsDatabase::JAVASCRIPT = "javascript";
36 const QString DomainsDatabase::LOCAL_STORAGE = "local_storage";
37 const QString DomainsDatabase::USER_AGENT = "user_agent";
38 const QString DomainsDatabase::ZOOM_FACTOR = "zoom_factor";
40 // Construct the class.
41 DomainsDatabase::DomainsDatabase() {}
43 void DomainsDatabase::addDatabase()
45 // Add the domain settings database.
46 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION_NAME);
48 // Set the database name.
49 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
52 if (domainsDatabase.open()) // Opening the database succeeded.
54 // Check to see if the domains table already exists.
55 if (domainsDatabase.tables().contains(DOMAINS_TABLE)) // The domains table already exists.
57 // Query the database schema version.
58 QSqlQuery schemaVersionQuery = domainsDatabase.exec(QStringLiteral("PRAGMA user_version"));
60 // Move to the first record.
61 schemaVersionQuery.first();
63 // Get the current schema version.
64 int currentSchemaVersion = schemaVersionQuery.value(0).toInt();
66 // Check to see if the schema has been updated.
67 if (currentSchemaVersion < SCHEMA_VERSION)
69 // Run the schema update code.
70 switch (currentSchemaVersion)
72 // Upgrade from schema version 0 to schema version 1.
75 // Add the JavaScript column.
76 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
78 // Fall through to the next case.
82 // Upgrade from schema version 1 to schema version 2.
85 // Add the User Agent column.
86 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
88 // Fall through to the next case.
92 // Upgrade from schema version 2 to schema version 3.
95 // Add the Zoom Factor columns.
96 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
97 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
99 // Fall through to the next case.
103 // Upgrade from schema version 3 to schema version 4.
106 // Add the DOM Storage column.
107 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
109 // Fall through to the next case.
113 // Upgrade from schema version 4 to schema version 5.
116 // Add the Local Storage column.
117 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
119 // Fall through to the next case.
123 // Upgrade from schema version 5 to schema version 6.
126 // Instantiate a spinner query.
127 QSqlQuery spinnerQuery(domainsDatabase);
129 // Set the query to be forward only (increases performance while iterating over the query).
130 spinnerQuery.setForwardOnly(true);
132 // Prepare the query.
133 spinnerQuery.prepare("SELECT " + ID + "," + JAVASCRIPT + "," + LOCAL_STORAGE + "," + DOM_STORAGE + " FROM " + DOMAINS_TABLE);
135 // Execute the query.
138 // Update the spinner values so that enabled is 1 and disabled is 2.
139 while (spinnerQuery.next())
141 // Initialize the new spinner values.
142 int newJavaScriptValue = SYSTEM_DEFAULT;
143 int newLocalStorageValue = SYSTEM_DEFAULT;
144 int newDomStorageValue = SYSTEM_DEFAULT;
146 // Update the new JavaScript value if needed.
147 switch (spinnerQuery.value(JAVASCRIPT).toInt())
149 // Disabled used to be 1.
152 // Update the value to be 2.
153 newJavaScriptValue = DISABLED;
158 // Enabled used to be 2.
161 // Update the new value to be 1.
162 newJavaScriptValue = ENABLED;
168 // Update the new local storage value if needed.
169 switch (spinnerQuery.value(LOCAL_STORAGE).toInt())
171 // Disabled used to be 1.
174 // Update the new value to be 2.
175 newLocalStorageValue = DISABLED;
180 // Enabled used to be 2.
183 // Update the new value to be 1.
184 newLocalStorageValue = ENABLED;
190 // Update the new DOM storage value if needed.
191 switch (spinnerQuery.value(DOM_STORAGE).toInt())
193 // Disabled used to be 1.
196 // Update the new value to be 2.
197 newDomStorageValue = DISABLED;
202 // Enabled used to be 2.
205 // Update the new value to be 1.
206 newDomStorageValue = ENABLED;
212 // Create an update spinner query.
213 QSqlQuery updateSpinnerQuery(domainsDatabase);
215 // Prepare the update spinner query.
216 updateSpinnerQuery.prepare("UPDATE " + DOMAINS_TABLE + " SET " +
217 JAVASCRIPT + " = :javascript , " +
218 LOCAL_STORAGE + " = :local_storage , " +
219 DOM_STORAGE + " = :dom_storage " +
220 " WHERE " + ID + " = :id");
223 updateSpinnerQuery.bindValue(":javascript", newJavaScriptValue);
224 updateSpinnerQuery.bindValue(":local_storage", newLocalStorageValue);
225 updateSpinnerQuery.bindValue(":dom_storage", newDomStorageValue);
226 updateSpinnerQuery.bindValue(":id", spinnerQuery.value(ID));
228 // Execute the query.
229 updateSpinnerQuery.exec();
232 // Fall through to the next case.
237 // Update the schema version.
238 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
241 else // The domains table does not exist.
243 // Instantiate a create table query.
244 QSqlQuery createTableQuery(domainsDatabase);
246 // Prepare the create table query.
247 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
248 ID + " INTEGER PRIMARY KEY, " +
249 DOMAIN_NAME + " TEXT, " +
250 JAVASCRIPT + " INTEGER DEFAULT 0, " +
251 LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
252 DOM_STORAGE + " INTEGER DEFAULT 0, " +
253 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
254 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
255 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)");
257 // Execute the query.
258 if (!createTableQuery.exec())
261 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
264 // Set the schema version.
265 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
268 else // Opening the database failed.
270 // Write the last database error message to the debug output.Settings::zoom
271 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
275 void DomainsDatabase::addDomain(const QString &domainName)
278 addDomain(domainName, SYSTEM_DEFAULT, SYSTEM_DEFAULT, SYSTEM_DEFAULT, UserAgentHelper::SYSTEM_DEFAULT_DATABASE, SYSTEM_DEFAULT, Settings::zoomFactor());
281 void DomainsDatabase::addDomain(const QString &domainName, const int javaScriptInt, const int localStorageInt, const int domStorageInt, const QString &userAgentDatabaseString,
282 const int zoomFactorInt, const double currentZoomFactorDouble)
284 // Get a handle for the domains database.
285 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
287 // Instantiate an add domain settings query.
288 QSqlQuery addDomainSettingsQuery(domainsDatabase);
290 // Prepare the query.
291 addDomainSettingsQuery.prepare("INSERT INTO " + DomainsDatabase::DOMAINS_TABLE + " (" +
292 DomainsDatabase::DOMAIN_NAME + ", " +
293 DomainsDatabase::JAVASCRIPT + ", " +
294 DomainsDatabase::LOCAL_STORAGE + ", " +
295 DomainsDatabase::DOM_STORAGE + ", " +
296 DomainsDatabase::USER_AGENT + ", " +
297 DomainsDatabase::ZOOM_FACTOR + ", " +
298 DomainsDatabase::CUSTOM_ZOOM_FACTOR + ") " +
299 "VALUES (:domain_name, :javascript, :local_storage, :dom_storage, :user_agent, :zoom_factor, :custom_zoom_factor)"
302 // Bind the query values.
303 addDomainSettingsQuery.bindValue(":domain_name", domainName);
304 addDomainSettingsQuery.bindValue(":javascript", javaScriptInt);
305 addDomainSettingsQuery.bindValue(":local_storage", localStorageInt);
306 addDomainSettingsQuery.bindValue(":dom_storage", domStorageInt);
307 addDomainSettingsQuery.bindValue(":user_agent", userAgentDatabaseString);
308 addDomainSettingsQuery.bindValue(":zoom_factor", zoomFactorInt);
309 addDomainSettingsQuery.bindValue(":custom_zoom_factor", currentZoomFactorDouble);
311 // Execute the query.
312 addDomainSettingsQuery.exec();
315 QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname)
317 // Get a handle for the domains database.
318 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
320 // Instantiate the all domain names query.
321 QSqlQuery allDomainNamesQuery(domainsDatabase);
323 // Set the query to be forward only (increases performance while iterating over the query).
324 allDomainNamesQuery.setForwardOnly(true);
326 // Prepare the query.
327 allDomainNamesQuery.prepare("SELECT " + ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
329 // Execute the query.
330 allDomainNamesQuery.exec();
332 // Create a domains settings map.
333 QMap<QString, int> domainSettingsMap;
335 // Populate the domain settings map.
336 while (allDomainNamesQuery.next())
338 // Add the domain name and database ID to the map.
339 domainSettingsMap.insert(allDomainNamesQuery.value(DOMAIN_NAME).toString(), allDomainNamesQuery.value(ID).toInt());
342 // Initialize the database ID tracker.
345 // Get the database ID if the hostname is found in the domain settings set.
346 if (domainSettingsMap.contains(hostname))
348 databaseId = domainSettingsMap.value(hostname);
351 // Create a subdomain string.
352 QString subdomain = hostname;
354 // Check all the subdomains of the hostname.
355 while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname.
357 // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
358 if (domainSettingsMap.contains("*." + subdomain))
360 // Get the database ID.
361 databaseId = domainSettingsMap.value("*." + subdomain);
364 // Strip out the first subdomain.
365 subdomain = subdomain.section('.', 1);
368 // Instantiate the domain lookup query.
369 QSqlQuery domainLookupQuery(domainsDatabase);
371 // Prepare the domain lookup query.
372 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + ID + " = " + QString::number(databaseId));
374 // Execute the query.
375 domainLookupQuery.exec();
377 // Move to the first entry.
378 domainLookupQuery.first();
381 return domainLookupQuery;