X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fdatabases%2FDomainsDatabase.cpp;h=d52647cca107ca5163f9493f173e164ae4060530;hb=03d1e9e85a1fc8c7f561d0c7d9492ef1bee292db;hp=6dfc1bd69e7daad13563454dfedca826f6285f65;hpb=cd1c3d0483b9026736fdcb151d90dda872d8a400;p=PrivacyBrowserPC.git diff --git a/src/databases/DomainsDatabase.cpp b/src/databases/DomainsDatabase.cpp index 6dfc1bd..d52647c 100644 --- a/src/databases/DomainsDatabase.cpp +++ b/src/databases/DomainsDatabase.cpp @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Soren Stoutner . + * Copyright 2022-2024 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -19,24 +19,23 @@ // Application headers. #include "DomainsDatabase.h" +#include "Settings.h" #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() {} @@ -76,7 +75,7 @@ void DomainsDatabase::addDatabase() // 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]]; } @@ -86,7 +85,7 @@ void DomainsDatabase::addDatabase() // 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]]; } @@ -97,7 +96,7 @@ void DomainsDatabase::addDatabase() 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]]; } @@ -107,7 +106,7 @@ void DomainsDatabase::addDatabase() // 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]]; } @@ -117,7 +116,120 @@ void DomainsDatabase::addDatabase() // 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]]; } } @@ -133,15 +245,14 @@ void DomainsDatabase::addDatabase() // 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()) @@ -156,11 +267,51 @@ void DomainsDatabase::addDatabase() } else // Opening the database failed. { - // Write the last database error message to the debug output. + // Write the last database error message to the debug output.Settings::zoom qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError(); } }; +void DomainsDatabase::addDomain(const QString &domainName) +{ + // Add the domain: + addDomain(domainName, SYSTEM_DEFAULT, SYSTEM_DEFAULT, SYSTEM_DEFAULT, UserAgentHelper::SYSTEM_DEFAULT_DATABASE, SYSTEM_DEFAULT, Settings::zoomFactor()); +} + +void DomainsDatabase::addDomain(const QString &domainName, const int javaScriptInt, const int localStorageInt, const int domStorageInt, const QString &userAgentDatabaseString, + const int zoomFactorInt, const double currentZoomFactorDouble) +{ + // Get a handle for the domains database. + QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME); + + // Instantiate an add domain settings query. + QSqlQuery addDomainSettingsQuery(domainsDatabase); + + // Prepare the query. + addDomainSettingsQuery.prepare("INSERT INTO " + DomainsDatabase::DOMAINS_TABLE + " (" + + DomainsDatabase::DOMAIN_NAME + ", " + + DomainsDatabase::JAVASCRIPT + ", " + + DomainsDatabase::LOCAL_STORAGE + ", " + + DomainsDatabase::DOM_STORAGE + ", " + + DomainsDatabase::USER_AGENT + ", " + + DomainsDatabase::ZOOM_FACTOR + ", " + + DomainsDatabase::CUSTOM_ZOOM_FACTOR + ") " + + "VALUES (:domain_name, :javascript, :local_storage, :dom_storage, :user_agent, :zoom_factor, :custom_zoom_factor)" + ); + + // Bind the query values. + addDomainSettingsQuery.bindValue(":domain_name", domainName); + addDomainSettingsQuery.bindValue(":javascript", javaScriptInt); + addDomainSettingsQuery.bindValue(":local_storage", localStorageInt); + addDomainSettingsQuery.bindValue(":dom_storage", domStorageInt); + addDomainSettingsQuery.bindValue(":user_agent", userAgentDatabaseString); + addDomainSettingsQuery.bindValue(":zoom_factor", zoomFactorInt); + addDomainSettingsQuery.bindValue(":custom_zoom_factor", currentZoomFactorDouble); + + // Execute the query. + addDomainSettingsQuery.exec(); +} + QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname) { // Get a handle for the domains database. @@ -173,7 +324,7 @@ QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname) 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(); @@ -185,7 +336,7 @@ QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname) 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. @@ -218,7 +369,7 @@ QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname) 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();