]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/databases/DomainsDatabase.cpp
Move `enabled` above `disabled` in the domain settings spinners. https://redmine...
[PrivacyBrowserPC.git] / src / databases / DomainsDatabase.cpp
1 /*
2  * Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "DomainsDatabase.h"
22 #include "helpers/UserAgentHelper.h"
23
24 // Define the private static schema constants.
25 const int DomainsDatabase::SCHEMA_VERSION = 6;
26
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";
38
39 // Construct the class.
40 DomainsDatabase::DomainsDatabase() {}
41
42 void DomainsDatabase::addDatabase()
43 {
44     // Add the domain settings database.
45     QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION_NAME);
46
47     // Set the database name.
48     domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
49
50     // Open the database.
51     if (domainsDatabase.open())  // Opening the database succeeded.
52     {
53         // Check to see if the domains table already exists.
54         if (domainsDatabase.tables().contains(DOMAINS_TABLE))  // The domains table already exists.
55         {
56             // Query the database schema version.
57             QSqlQuery schemaVersionQuery = domainsDatabase.exec(QStringLiteral("PRAGMA user_version"));
58
59             // Move to the first record.
60             schemaVersionQuery.first();
61
62             // Get the current schema version.
63             int currentSchemaVersion = schemaVersionQuery.value(0).toInt();
64
65             // Check to see if the schema has been updated.
66             if (currentSchemaVersion < SCHEMA_VERSION)
67             {
68                 // Run the schema update code.
69                 switch (currentSchemaVersion)
70                 {
71                     // Upgrade from schema version 0 to schema version 1.
72                     case 0:
73                     {
74                         // Add the JavaScript column.
75                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
76
77                         // Fall through to the next case.
78                         [[fallthrough]];
79                     }
80
81                     // Upgrade from schema version 1 to schema version 2.
82                     case 1:
83                     {
84                         // Add the User Agent column.
85                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
86
87                         // Fall through to the next case.
88                         [[fallthrough]];
89                     }
90
91                     // Upgrade from schema version 2 to schema version 3.
92                     case 2:
93                     {
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");
97
98                         // Fall through to the next case.
99                         [[fallthrough]];
100                     }
101
102                     // Upgrade from schema version 3 to schema version 4.
103                     case 3:
104                     {
105                         // Add the DOM Storage column.
106                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
107
108                         // Fall through to the next case.
109                         [[fallthrough]];
110                     }
111
112                     // Upgrade from schema version 4 to schema version 5.
113                     case 4:
114                     {
115                         // Add the Local Storage column.
116                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
117
118                         // Fall through to the next case.
119                         [[fallthrough]];
120                     }
121
122                     // Upgrade from schema version 5 to schema version 6.
123                     case 5:
124                     {
125                         // Instantiate a spinner query.
126                         QSqlQuery spinnerQuery(domainsDatabase);
127
128                         // Set the query to be forward only (increases performance while iterating over the query).
129                         spinnerQuery.setForwardOnly(true);
130
131                         // Prepare the query.
132                         spinnerQuery.prepare("SELECT " + ID + "," + JAVASCRIPT + "," + LOCAL_STORAGE + "," + DOM_STORAGE + " FROM " + DOMAINS_TABLE);
133
134                         // Execute the query.
135                         spinnerQuery.exec();
136
137                         // Update the spinner values so that enabled is 1 and disabled is 2.
138                         while (spinnerQuery.next())
139                         {
140                             // Initialize the new spinner values.
141                             int newJavaScriptValue = SYSTEM_DEFAULT;
142                             int newLocalStorageValue = SYSTEM_DEFAULT;
143                             int newDomStorageValue = SYSTEM_DEFAULT;
144
145                             // Update the new JavaScript value if needed.
146                             switch (spinnerQuery.value(JAVASCRIPT).toInt())
147                             {
148                                 // Disabled used to be 1.
149                                 case 1:
150                                 {
151                                     // Update the value to be 2.
152                                     newJavaScriptValue = DISABLED;
153
154                                     break;
155                                 }
156
157                                 // Enabled used to be 2.
158                                 case 2:
159                                 {
160                                     // Update the new value to be 1.
161                                     newJavaScriptValue = ENABLED;
162
163                                     break;
164                                 }
165                             }
166
167                             // Update the new local storage value if needed.
168                             switch (spinnerQuery.value(LOCAL_STORAGE).toInt())
169                             {
170                                 // Disabled used to be 1.
171                                 case 1:
172                                 {
173                                     // Update the new value to be 2.
174                                     newLocalStorageValue = DISABLED;
175
176                                     break;
177                                 }
178
179                                 // Enabled used to be 2.
180                                 case 2:
181                                 {
182                                     // Update the new value to be 1.
183                                     newLocalStorageValue = ENABLED;
184
185                                     break;
186                                 }
187                             }
188
189                             // Update the new DOM storage value if needed.
190                             switch (spinnerQuery.value(DOM_STORAGE).toInt())
191                             {
192                                 // Disabled used to be 1.
193                                 case 1:
194                                 {
195                                     // Update the new value to be 2.
196                                     newDomStorageValue = DISABLED;
197
198                                     break;
199                                 }
200
201                                 // Enabled used to be 2.
202                                 case 2:
203                                 {
204                                     // Update the new value to be 1.
205                                     newDomStorageValue = ENABLED;
206
207                                     break;
208                                 }
209                             }
210
211                             // Create an update spinner query.
212                             QSqlQuery updateSpinnerQuery(domainsDatabase);
213
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");
220
221                             // Bind the values.
222                             updateSpinnerQuery.bindValue(":javascript", newJavaScriptValue);
223                             updateSpinnerQuery.bindValue(":local_storage", newLocalStorageValue);
224                             updateSpinnerQuery.bindValue(":dom_storage", newDomStorageValue);
225                             updateSpinnerQuery.bindValue(":id", spinnerQuery.value(ID));
226
227                             // Execute the query.
228                             updateSpinnerQuery.exec();
229                         }
230
231                         // Fall through to the next case.
232                         // [[fallthrough]];
233                     }
234                 }
235
236                 // Update the schema version.
237                 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
238             }
239         }
240         else  // The domains table does not exist.
241         {
242             // Instantiate a create table query.
243             QSqlQuery createTableQuery(domainsDatabase);
244
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)");
255
256             // Execute the query.
257             if (!createTableQuery.exec())
258             {
259                 // Log any errors.
260                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
261             }
262
263             // Set the schema version.
264             domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
265         }
266     }
267     else  // Opening the database failed.
268     {
269         // Write the last database error message to the debug output.
270         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
271     }
272 };
273
274 QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname)
275 {
276     // Get a handle for the domains database.
277     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
278
279     // Instantiate the all domain names query.
280     QSqlQuery allDomainNamesQuery(domainsDatabase);
281
282     // Set the query to be forward only (increases performance while iterating over the query).
283     allDomainNamesQuery.setForwardOnly(true);
284
285     // Prepare the query.
286     allDomainNamesQuery.prepare("SELECT " + ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
287
288     // Execute the query.
289     allDomainNamesQuery.exec();
290
291     // Create a domains settings map.
292     QMap<QString, int> domainSettingsMap;
293
294     // Populate the domain settings map.
295     while (allDomainNamesQuery.next())
296     {
297         // Add the domain name and database ID to the map.
298         domainSettingsMap.insert(allDomainNamesQuery.value(DOMAIN_NAME).toString(), allDomainNamesQuery.value(ID).toInt());
299     }
300
301     // Initialize the database ID tracker.
302     int databaseId = -1;
303
304     // Get the database ID if the hostname is found in the domain settings set.
305     if (domainSettingsMap.contains(hostname))
306     {
307         databaseId = domainSettingsMap.value(hostname);
308     }
309
310     // Create a subdomain string.
311     QString subdomain = hostname;
312
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.
315     {
316         // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
317         if (domainSettingsMap.contains("*." + subdomain))
318         {
319             // Get the database ID.
320             databaseId = domainSettingsMap.value("*." + subdomain);
321         }
322
323         // Strip out the first subdomain.
324         subdomain = subdomain.section('.', 1);
325     }
326
327     // Instantiate the domain lookup query.
328     QSqlQuery domainLookupQuery(domainsDatabase);
329
330     // Prepare the domain lookup query.
331     domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + ID + " = " + QString::number(databaseId));
332
333     // Execute the query.
334     domainLookupQuery.exec();
335
336     // Move to the first entry.
337     domainLookupQuery.first();
338
339     // Return the query.
340     return domainLookupQuery;
341 }