]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/databases/CookiesDatabase.cpp
Partial filter list implementation.
[PrivacyBrowserPC.git] / src / databases / CookiesDatabase.cpp
index adc4f1ba2c108362e59bbcda99276a54c0e4f219..e8a870b82d15b00a034e05ccf1e233efb3cc9097 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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>.
  *
 // Define the private static schema constants.
 const int CookiesDatabase::SCHEMA_VERSION = 0;
 
-// Define the public static database constants.
+// Define the public static constants.
 const QString CookiesDatabase::CONNECTION_NAME = "cookies_database";
 const QString CookiesDatabase::COOKIES_TABLE = "cookies";
-
-// Define the public static database field names.
-const QString CookiesDatabase::_ID = "_id";
 const QString CookiesDatabase::DOMAIN = "domain";
-const QString CookiesDatabase::NAME = "name";
-const QString CookiesDatabase::PATH = "path";
 const QString CookiesDatabase::EXPIRATION_DATE = "expiration_date";
 const QString CookiesDatabase::HTTP_ONLY = "http_only";
+const QString CookiesDatabase::ID = "_id";
+const QString CookiesDatabase::NAME = "name";
+const QString CookiesDatabase::PATH = "path";
 const QString CookiesDatabase::SECURE = "secure";
 const QString CookiesDatabase::VALUE = "value";
 
@@ -83,15 +81,14 @@ void CookiesDatabase::addDatabase()
 
             // Prepare the create table query.
             createTableQuery.prepare("CREATE TABLE " + COOKIES_TABLE + "(" +
-                _ID + " INTEGER PRIMARY KEY, " +
-                DOMAIN + " TEXT NOT NULL, " +
-                NAME + " TEXT NOT NULL, " +
-                PATH + " TEXT NOT NULL, " +
-                EXPIRATION_DATE + " TEXT, " +
-                HTTP_ONLY + " INTEGER NOT NULL DEFAULT 0, " +
-                SECURE + " INTEGER NOT NULL DEFAULT 0, " +
-                VALUE + " TEXT NOT NULL)"
-            );
+                                     ID + " INTEGER PRIMARY KEY, " +
+                                     DOMAIN + " TEXT NOT NULL, " +
+                                     NAME + " TEXT NOT NULL, " +
+                                     PATH + " TEXT NOT NULL, " +
+                                     EXPIRATION_DATE + " TEXT, " +
+                                     HTTP_ONLY + " INTEGER NOT NULL DEFAULT 0, " +
+                                     SECURE + " INTEGER NOT NULL DEFAULT 0, " +
+                                     VALUE + " TEXT NOT NULL)");
 
             // Execute the query.
             if (!createTableQuery.exec())
@@ -128,8 +125,15 @@ void CookiesDatabase::addCookie(const QNetworkCookie &cookie)
         QSqlQuery addCookieQuery(cookiesDatabase);
 
         // Prepare the add cookie query.
-        addCookieQuery.prepare("INSERT INTO " + COOKIES_TABLE + " (" + DOMAIN + ", " + NAME + ", " + PATH + ", " + EXPIRATION_DATE + ", " + HTTP_ONLY + ", " + SECURE + ", " + VALUE + ") "
-                            "VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)"
+        addCookieQuery.prepare("INSERT INTO " + COOKIES_TABLE + " (" +
+                                DOMAIN + ", " +
+                                NAME + ", " +
+                                PATH + ", " +
+                                EXPIRATION_DATE + ", " +
+                                HTTP_ONLY + ", " +
+                                SECURE + ", " +
+                                VALUE + ") "
+                                "VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)"
         );
 
         // Bind the values.
@@ -146,6 +150,40 @@ void CookiesDatabase::addCookie(const QNetworkCookie &cookie)
     }
 }
 
+int CookiesDatabase::cookieCount()
+{
+    // Get a handle for the cookies database.
+    QSqlDatabase cookiesDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+    // Instantiate a count cookies query.
+    QSqlQuery countCookiesQuery(cookiesDatabase);
+
+    // Set the query to be forward only.
+    countCookiesQuery.setForwardOnly(true);
+
+    // Prepare the query.
+    countCookiesQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE);
+
+    // Execute the query.
+    countCookiesQuery.exec();
+
+    // Move to the last row.
+    countCookiesQuery.last();
+
+    // Initialize a number of cookies variable.
+    int numberOfCookies = 0;
+
+    // Check to see if the query is valid (there is at least one cookie).
+    if (countCookiesQuery.isValid())
+    {
+        // Get the number of rows (which is zero based) and add one to calculate the number of cookies.
+        numberOfCookies = countCookiesQuery.at() + 1;
+    }
+
+    // Return the number of cookies.
+    return numberOfCookies;
+}
+
 void CookiesDatabase::deleteAllCookies()
 {
     // Get a handle for the cookies database.
@@ -170,7 +208,10 @@ void CookiesDatabase::deleteCookie(const QNetworkCookie &cookie)
     QSqlQuery deleteCookieQuery(cookiesDatabase);
 
     // Prepare the delete cookie query.
-    deleteCookieQuery.prepare("DELETE FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+    deleteCookieQuery.prepare("DELETE FROM " + COOKIES_TABLE + " WHERE " +
+                              DOMAIN + " = :domain AND " +
+                              NAME + " = :name AND " +
+                              PATH + " = :path");
 
     // Bind the values.
     deleteCookieQuery.bindValue(":domain", cookie.domain());
@@ -189,7 +230,7 @@ QList<QNetworkCookie*>* CookiesDatabase::getCookies()
     // Instantiate a cookies query.
     QSqlQuery cookiesQuery(cookiesDatabase);
 
-    // Set the query to be forward only.
+    // Set the query to be forward only, which is more performant.
     cookiesQuery.setForwardOnly(true);
 
     // Prepare the cookies query.
@@ -224,6 +265,45 @@ QList<QNetworkCookie*>* CookiesDatabase::getCookies()
     return cookieListPointer;
 }
 
+QNetworkCookie* CookiesDatabase::getCookieById(const int &id)
+{
+    // Get a handle for the cookies database.
+    QSqlDatabase cookiesDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+    // Instantiate a cookie query.
+    QSqlQuery cookieQuery(cookiesDatabase);
+
+    // Set the query to be forward only.
+    cookieQuery.setForwardOnly(true);
+
+    // Prepare the cookies query.
+    cookieQuery.prepare("SELECT * FROM " + COOKIES_TABLE + " WHERE " + ID + " = :id");
+
+    // Bind the values.
+    cookieQuery.bindValue(":id", id);
+
+    // Execute the query.
+    cookieQuery.exec();
+
+    // Move to the first entry.
+    cookieQuery.first();
+
+    // Create a cookie.
+    QNetworkCookie *cookiePointer = new QNetworkCookie();
+
+    // Populate the cookie.
+    cookiePointer->setDomain(cookieQuery.value(DOMAIN).toString());
+    cookiePointer->setName(cookieQuery.value(NAME).toString().toUtf8());
+    cookiePointer->setPath(cookieQuery.value(PATH).toString());
+    cookiePointer->setExpirationDate(QDateTime::fromString(cookieQuery.value(EXPIRATION_DATE).toString(), Qt::ISODate));
+    cookiePointer->setHttpOnly(cookieQuery.value(HTTP_ONLY).toBool());
+    cookiePointer->setSecure(cookieQuery.value(SECURE).toBool());
+    cookiePointer->setValue(cookieQuery.value(VALUE).toString().toUtf8());
+
+    // Return the cookie.
+    return cookiePointer;
+}
+
 bool CookiesDatabase::isDurable(const QNetworkCookie &cookie)
 {
     // Get a handle for the cookies database.
@@ -236,7 +316,10 @@ bool CookiesDatabase::isDurable(const QNetworkCookie &cookie)
     isDurableQuery.setForwardOnly(true);
 
     // Prepare the is durable query.
-    isDurableQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+    isDurableQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE + " WHERE " +
+                            DOMAIN + " = :domain AND " +
+                            NAME + " = :name AND " +
+                            PATH + " = :path");
 
     // Bind the values.
     isDurableQuery.bindValue(":domain", cookie.domain());
@@ -262,8 +345,14 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie)
     QSqlQuery isUpdateQuery(cookiesDatabase);
 
     // Prepare the is update query.
-    isUpdateQuery.prepare("SELECT " + EXPIRATION_DATE + " , " + HTTP_ONLY + " , " + SECURE + " , " + VALUE + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " +
-                          NAME + " = :name AND " + PATH + " = :path");
+    isUpdateQuery.prepare("SELECT " + EXPIRATION_DATE + " , " +
+                          HTTP_ONLY + " , " +
+                          SECURE + " , " +
+                          VALUE +
+                          " FROM " + COOKIES_TABLE +
+                          " WHERE " + DOMAIN + " = :domain AND " +
+                          NAME + " = :name AND " +
+                          PATH + " = :path");
 
     // Bind the values.
     isUpdateQuery.bindValue(":domain", cookie.domain());
@@ -280,19 +369,19 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie)
     if (isUpdateQuery.isValid())  // The cookie exists in the database.
     {
         // Check to see if the cookie data has changed.
-        if ((QDateTime::fromString(isUpdateQuery.value(0).toString(), Qt::ISODate) != cookie.expirationDate()) ||
-            (isUpdateQuery.value(1).toBool() != cookie.isHttpOnly()) ||
-            (isUpdateQuery.value(2).toBool() != cookie.isSecure()) ||
-            (isUpdateQuery.value(3).toString().toUtf8() != cookie.value()))  // The cookies data has changed.
+        if ((QDateTime::fromString(isUpdateQuery.value(EXPIRATION_DATE).toString(), Qt::ISODate) != cookie.expirationDate()) ||
+            (isUpdateQuery.value(HTTP_ONLY).toBool() != cookie.isHttpOnly()) ||
+            (isUpdateQuery.value(SECURE).toBool() != cookie.isSecure()) ||
+            (isUpdateQuery.value(VALUE).toString().toUtf8() != cookie.value()))  // The cookies data has changed.
         {
-            qDebug() << "The durable cookie data has changed.";
+            //qDebug() << "The durable cookie data has changed.";
 
             // Return true.
             return true;
         }
         else  // The cookie data has not changed.
         {
-            qDebug() << "The durable cookie data is unchanged.";
+            //qDebug() << "The durable cookie data is unchanged.";
 
             // Return false.
             return false;
@@ -313,9 +402,15 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &cookie)
     // Create the update cookie query.
     QSqlQuery updateCookieQuery(cookiesDatabase);
 
-    // Prepare the edit cookie query.
-    updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + EXPIRATION_DATE + " = :expiration_date , " + HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " +
-                              VALUE + " = :value WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+    // Prepare the update cookie query.
+    updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE +
+                              " SET " + EXPIRATION_DATE + " = :expiration_date , " +
+                              HTTP_ONLY + " = :http_only , " +
+                              SECURE + " = :secure , " +
+                              VALUE + " = :value " +
+                              "WHERE " + DOMAIN + " = :domain AND " +
+                              NAME + " = :name AND " +
+                              PATH + " = :path");
 
     // Bind the values.
     updateCookieQuery.bindValue(":domain", cookie.domain());
@@ -342,7 +437,10 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &oldCookie, const QNetwo
     oldCookieQuery.setForwardOnly(true);
 
     // Prepare the old cookie query.
-    oldCookieQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+    oldCookieQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE +
+                           " WHERE " + DOMAIN + " = :domain AND " +
+                           NAME + " = :name AND " +
+                           PATH + " = :path");
 
     // Bind the values.
     oldCookieQuery.bindValue(":domain", oldCookie.domain());
@@ -359,8 +457,15 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &oldCookie, const QNetwo
     QSqlQuery updateCookieQuery(cookiesDatabase);
 
     // Prepare the update cookie query.
-    updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + DOMAIN + " = :domain , " + NAME + " = :name , " + PATH + " = :path , " + EXPIRATION_DATE + " = :expiration_date , " +
-                              HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " + VALUE + " = :value WHERE " + _ID + " = :id");
+    updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE +
+                              " SET " + DOMAIN + " = :domain , " +
+                              NAME + " = :name , " +
+                              PATH + " = :path , " +
+                              EXPIRATION_DATE + " = :expiration_date , " +
+                              HTTP_ONLY + " = :http_only , " +
+                              SECURE + " = :secure , " +
+                              VALUE + " = :value " +
+                              "WHERE " + ID + " = :id");
 
     // Bind the values.
     updateCookieQuery.bindValue(":id", oldCookieQuery.value(0).toLongLong());