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