]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/databases/CookiesDatabase.cpp
Add a durable cookies dialog.
[PrivacyBrowserPC.git] / src / databases / CookiesDatabase.cpp
index adc4f1ba2c108362e59bbcda99276a54c0e4f219..a99290985cd7c5f28e971c3e50250f2567aa5aec 100644 (file)
@@ -146,6 +146,33 @@ 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();
+
+    // Get the number of rows (which is zero based).
+    int numberOfCookies = countCookiesQuery.at() + 1;
+
+    // Return the number of cookies.
+    return numberOfCookies;
+}
+
 void CookiesDatabase::deleteAllCookies()
 {
     // Get a handle for the cookies database.
@@ -224,6 +251,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.