]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/helpers/DomainsDatabaseHelper.cpp
Add zoom factor domain settings.
[PrivacyBrowserPC.git] / src / helpers / DomainsDatabaseHelper.cpp
1 /*
2  * Copyright © 2022 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 "DomainsDatabaseHelper.h"
22 #include "UserAgentHelper.h"
23
24 // Define the public static domain constants.
25 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
26 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
27
28 // Define the private static schema constants.
29 const int DomainsDatabaseHelper::SCHEMA_VERSION = 3;
30
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";
36 const QString DomainsDatabaseHelper::ZOOM_FACTOR = "zoom_factor";
37 const QString DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
38
39 // The default constructor.
40 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
41
42 void DomainsDatabaseHelper::addDatabase()
43 {
44     // Add the domain settings database.
45     QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("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))
55         {
56             // Query the database schema version.
57             QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
58
59             // Move to the first record.
60             getSchemaVersionQuery.first();
61
62             // Get the current schema version.
63             int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
64
65             // Check to see if the schama has been updated.
66             if (SCHEMA_VERSION > currentSchemaVersion)
67             {
68                 // Run schema update code.
69                 switch (currentSchemaVersion)
70                 {
71                     // Upgrade from schema version 0.
72                     case 0:
73                     {
74                         // Add the JavaScript column.
75                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
76
77                         // Set the default value.
78                         domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" );
79
80                         // Fallthrough to the next case.
81                         [[fallthrough]];
82                     }
83
84                     // Upgrade from schema version 1.
85                     case 1:
86                     {
87                         // Add the User Agent column.
88                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
89
90                         // Fallthrough to the next case.
91                         [[fallthrough]];
92                     }
93
94                     // Upgrade from schema version 2.
95                     case 2:
96                     {
97                         // Add the Zoom Factor columns.
98                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
99                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
100                     }
101                 }
102
103                 // Update the schema version.
104                 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
105             }
106         }
107         else
108         {
109             // Instantiate a create table query.
110             QSqlQuery createTableQuery(domainsDatabase);
111
112             // Prepare the create table query.
113             createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
114                 _ID + " INTEGER PRIMARY KEY, " +
115                 DOMAIN_NAME + " TEXT, " +
116                 JAVASCRIPT + " INTEGER DEFAULT 0, " +
117                 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
118                 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
119                 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)"
120             );
121
122             // Execute the query.
123             if (!createTableQuery.exec())
124             {
125                 // Log any errors.
126                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
127             }
128
129             // Set the schema version.
130             domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
131         }
132     }
133     else  // Opening the database failed.
134     {
135         // Write the last database error message to the debug output.
136         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
137     }
138 };
139
140 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
141 {
142     // Get a handle for the domains database.
143     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
144
145     // Instantiate the all domain names query.
146     QSqlQuery allDomainNamesQuery(domainsDatabase);
147
148     // Set the query to be forward only (increases performance while iterating over the query).
149     allDomainNamesQuery.setForwardOnly(true);
150
151     // Prepare the query.
152     allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
153
154     // Execute the query.
155     allDomainNamesQuery.exec();
156
157     // Create a domains settings map.
158     QMap<QString, int> domainSettingsMap;
159
160     // Populate the domain settings map.
161     while (allDomainNamesQuery.next())
162     {
163         // Add the domain name and database ID to the map.
164         domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(),
165                                  allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt());
166     }
167
168     // Initialize the database ID tracker.
169     int databaseId = -1;
170
171     // Get the database ID if the hostname is found in the domain settings set.
172     if (domainSettingsMap.contains(hostname))
173     {
174         databaseId = domainSettingsMap.value(hostname);
175     }
176
177     // Create a subdomain string.
178     QString subdomain = hostname;
179
180     // Check all the subdomains of the hostname.
181     while ((databaseId == -1) && subdomain.contains("."))  // Stop checking when a match is found or there are no more `.` in the hostname.
182     {
183         // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
184         if (domainSettingsMap.contains("*." + subdomain))
185         {
186             // Get the database ID.
187             databaseId = domainSettingsMap.value("*." + subdomain);
188         }
189
190         // Strip out the first subdomain.
191         subdomain = subdomain.section('.', 1);
192     }
193
194     // Instantiate the domain lookup query.
195     QSqlQuery domainLookupQuery(domainsDatabase);
196
197     // Prepare the domain lookup query.
198     domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
199
200     // Execute the query.
201     domainLookupQuery.exec();
202
203     // Move to the first entry.
204     domainLookupQuery.first();
205
206     // Return the query.
207     return domainLookupQuery;
208 }