]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/helpers/DomainsDatabaseHelper.cpp
Add User Agent to 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 = 2;
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
37 // The default constructor.
38 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
39
40 void DomainsDatabaseHelper::addDatabase()
41 {
42     // Add the domain settings database.
43     QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
44
45     // Set the database name.
46     domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
47
48     // Open the database.
49     if (domainsDatabase.open())  // Opening the database succeeded.
50     {
51         // Check to see if the domains table already exists.
52         if (domainsDatabase.tables().contains(DOMAINS_TABLE))
53         {
54             // Query the database schema version.
55             QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
56
57             // Move to the first record.
58             getSchemaVersionQuery.first();
59
60             // Get the current schema version.
61             int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
62
63             // Check to see if the schama has been updated.
64             if (SCHEMA_VERSION > currentSchemaVersion)
65             {
66                 // Run schema update code.
67                 switch (currentSchemaVersion)
68                 {
69                     // Upgrade from schema version 0.
70                     case 0:
71                     {
72                         // Add the JavaScript column.
73                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
74
75                         // Set the default value.
76                         domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" );
77
78                         [[fallthrough]];
79                     }
80
81                     // Upgrade from schema version 1.
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                 }
88
89                 // Update the schema version.
90                 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
91             }
92         }
93         else
94         {
95             // Instantiate a create table query.
96             QSqlQuery createTableQuery(domainsDatabase);
97
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 + "')"
104             );
105
106             // Execute the query.
107             if (!createTableQuery.exec())
108             {
109                 // Log any errors.
110                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
111             }
112
113             // Set the schema version.
114             domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
115         }
116     }
117     else  // Opening the database failed.
118     {
119         // Write the last database error message to the debug output.
120         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
121     }
122 };
123
124 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
125 {
126     // Get a handle for the domains database.
127     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
128
129     // Instantiate the all domain names query.
130     QSqlQuery allDomainNamesQuery(domainsDatabase);
131
132     // Set the query to be forward only (increases performance while iterating over the query).
133     allDomainNamesQuery.setForwardOnly(true);
134
135     // Prepare the query.
136     allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
137
138     // Execute the query.
139     allDomainNamesQuery.exec();
140
141     // Create a domains settings map.
142     QMap<QString, int> domainSettingsMap;
143
144     // Populate the domain settings map.
145     while (allDomainNamesQuery.next())
146     {
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());
150     }
151
152     // Initialize the database ID tracker.
153     int databaseId = -1;
154
155     // Get the database ID if the hostname is found in the domain settings set.
156     if (domainSettingsMap.contains(hostname))
157     {
158         databaseId = domainSettingsMap.value(hostname);
159     }
160
161     // Create a subdomain string.
162     QString subdomain = hostname;
163
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.
166     {
167         // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
168         if (domainSettingsMap.contains("*." + subdomain))
169         {
170             // Get the database ID.
171             databaseId = domainSettingsMap.value("*." + subdomain);
172         }
173
174         // Strip out the first subdomain.
175         subdomain = subdomain.section('.', 1);
176     }
177
178     // Instantiate the domain lookup query.
179     QSqlQuery domainLookupQuery(domainsDatabase);
180
181     // Prepare the domain lookup query.
182     domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
183
184     // Execute the query.
185     domainLookupQuery.exec();
186
187     // Move to the first entry.
188     domainLookupQuery.first();
189
190     // Return the query.
191     return domainLookupQuery;
192 }