X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fdatabases%2FCookiesDatabase.cpp;h=a99290985cd7c5f28e971c3e50250f2567aa5aec;hb=adf448e4cca7b96f6db9fc2048e9a64fa24a9bba;hp=adc4f1ba2c108362e59bbcda99276a54c0e4f219;hpb=b105f2cce889a132faa9d7a5cdc8505b75965321;p=PrivacyBrowserPC.git diff --git a/src/databases/CookiesDatabase.cpp b/src/databases/CookiesDatabase.cpp index adc4f1b..a992909 100644 --- a/src/databases/CookiesDatabase.cpp +++ b/src/databases/CookiesDatabase.cpp @@ -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* 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.