/*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
*
#include "helpers/UserAgentHelper.h"
// Define the private static schema constants.
-const int DomainsDatabase::SCHEMA_VERSION = 5;
+const int DomainsDatabase::SCHEMA_VERSION = 6;
-// Define the public static database constants.
+// Define the public static constants.
const QString DomainsDatabase::CONNECTION_NAME = "domains_database";
-const QString DomainsDatabase::DOMAINS_TABLE = "domains";
-
-// Define the public static database field names.
-const QString DomainsDatabase::_ID = "_id";
+const QString DomainsDatabase::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
+const QString DomainsDatabase::DOM_STORAGE = "dom_storage";
const QString DomainsDatabase::DOMAIN_NAME = "domain_name";
+const QString DomainsDatabase::DOMAINS_TABLE = "domains";
+const QString DomainsDatabase::ID = "_id";
const QString DomainsDatabase::JAVASCRIPT = "javascript";
const QString DomainsDatabase::LOCAL_STORAGE = "local_storage";
-const QString DomainsDatabase::DOM_STORAGE = "dom_storage";
const QString DomainsDatabase::USER_AGENT = "user_agent";
const QString DomainsDatabase::ZOOM_FACTOR = "zoom_factor";
-const QString DomainsDatabase::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
// Construct the class.
DomainsDatabase::DomainsDatabase() {}
// Add the JavaScript column.
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
- // Fallthrough to the next case.
+ // Fall through to the next case.
[[fallthrough]];
}
// Add the User Agent column.
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
- // Fallthrough to the next case.
+ // Fall through to the next case.
[[fallthrough]];
}
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
- // Fallthrough to the next case.
+ // Fall through to the next case.
[[fallthrough]];
}
// Add the DOM Storage column.
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
- // Fallthrough to the next case.
+ // Fall through to the next case.
[[fallthrough]];
}
// Add the Local Storage column.
domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
- // Fallthrough to the next case.
+ // Fall through to the next case.
+ [[fallthrough]];
+ }
+
+ // Upgrade from schema version 5 to schema version 6.
+ case 5:
+ {
+ // Instantiate a spinner query.
+ QSqlQuery spinnerQuery(domainsDatabase);
+
+ // Set the query to be forward only (increases performance while iterating over the query).
+ spinnerQuery.setForwardOnly(true);
+
+ // Prepare the query.
+ spinnerQuery.prepare("SELECT " + ID + "," + JAVASCRIPT + "," + LOCAL_STORAGE + "," + DOM_STORAGE + " FROM " + DOMAINS_TABLE);
+
+ // Execute the query.
+ spinnerQuery.exec();
+
+ // Update the spinner values so that enabled is 1 and disabled is 2.
+ while (spinnerQuery.next())
+ {
+ // Initialize the new spinner values.
+ int newJavaScriptValue = SYSTEM_DEFAULT;
+ int newLocalStorageValue = SYSTEM_DEFAULT;
+ int newDomStorageValue = SYSTEM_DEFAULT;
+
+ // Update the new JavaScript value if needed.
+ switch (spinnerQuery.value(JAVASCRIPT).toInt())
+ {
+ // Disabled used to be 1.
+ case 1:
+ {
+ // Update the value to be 2.
+ newJavaScriptValue = DISABLED;
+
+ break;
+ }
+
+ // Enabled used to be 2.
+ case 2:
+ {
+ // Update the new value to be 1.
+ newJavaScriptValue = ENABLED;
+
+ break;
+ }
+ }
+
+ // Update the new local storage value if needed.
+ switch (spinnerQuery.value(LOCAL_STORAGE).toInt())
+ {
+ // Disabled used to be 1.
+ case 1:
+ {
+ // Update the new value to be 2.
+ newLocalStorageValue = DISABLED;
+
+ break;
+ }
+
+ // Enabled used to be 2.
+ case 2:
+ {
+ // Update the new value to be 1.
+ newLocalStorageValue = ENABLED;
+
+ break;
+ }
+ }
+
+ // Update the new DOM storage value if needed.
+ switch (spinnerQuery.value(DOM_STORAGE).toInt())
+ {
+ // Disabled used to be 1.
+ case 1:
+ {
+ // Update the new value to be 2.
+ newDomStorageValue = DISABLED;
+
+ break;
+ }
+
+ // Enabled used to be 2.
+ case 2:
+ {
+ // Update the new value to be 1.
+ newDomStorageValue = ENABLED;
+
+ break;
+ }
+ }
+
+ // Create an update spinner query.
+ QSqlQuery updateSpinnerQuery(domainsDatabase);
+
+ // Prepare the update spinner query.
+ updateSpinnerQuery.prepare("UPDATE " + DOMAINS_TABLE + " SET " +
+ JAVASCRIPT + " = :javascript , " +
+ LOCAL_STORAGE + " = :local_storage , " +
+ DOM_STORAGE + " = :dom_storage " +
+ " WHERE " + ID + " = :id");
+
+ // Bind the values.
+ updateSpinnerQuery.bindValue(":javascript", newJavaScriptValue);
+ updateSpinnerQuery.bindValue(":local_storage", newLocalStorageValue);
+ updateSpinnerQuery.bindValue(":dom_storage", newDomStorageValue);
+ updateSpinnerQuery.bindValue(":id", spinnerQuery.value(ID));
+
+ // Execute the query.
+ updateSpinnerQuery.exec();
+ }
+
+ // Fall through to the next case.
// [[fallthrough]];
}
}
// Prepare the create table query.
createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
- _ID + " INTEGER PRIMARY KEY, " +
- DOMAIN_NAME + " TEXT, " +
- JAVASCRIPT + " INTEGER DEFAULT 0, " +
- LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
- DOM_STORAGE + " INTEGER DEFAULT 0, " +
- USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
- ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
- CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)"
- );
+ ID + " INTEGER PRIMARY KEY, " +
+ DOMAIN_NAME + " TEXT, " +
+ JAVASCRIPT + " INTEGER DEFAULT 0, " +
+ LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
+ DOM_STORAGE + " INTEGER DEFAULT 0, " +
+ USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
+ ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
+ CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)");
// Execute the query.
if (!createTableQuery.exec())
allDomainNamesQuery.setForwardOnly(true);
// Prepare the query.
- allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
+ allDomainNamesQuery.prepare("SELECT " + ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
// Execute the query.
allDomainNamesQuery.exec();
while (allDomainNamesQuery.next())
{
// Add the domain name and database ID to the map.
- domainSettingsMap.insert(allDomainNamesQuery.record().field(DOMAIN_NAME).value().toString(), allDomainNamesQuery.record().field(_ID).value().toInt());
+ domainSettingsMap.insert(allDomainNamesQuery.value(DOMAIN_NAME).toString(), allDomainNamesQuery.value(ID).toInt());
}
// Initialize the database ID tracker.
QSqlQuery domainLookupQuery(domainsDatabase);
// Prepare the domain lookup query.
- domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
+ domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + ID + " = " + QString::number(databaseId));
// Execute the query.
domainLookupQuery.exec();