]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/databases/CookiesDatabase.cpp
Implement loading of new tabs from the context menu.
[PrivacyBrowserPC.git] / src / databases / CookiesDatabase.cpp
index adc4f1ba2c108362e59bbcda99276a54c0e4f219..207f45b89795c3ead31d00313cb78654890056c7 100644 (file)
@@ -146,6 +146,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.
@@ -224,6 +258,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.
@@ -285,14 +358,14 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie)
             (isUpdateQuery.value(2).toBool() != cookie.isSecure()) ||
             (isUpdateQuery.value(3).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;