]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/CookiesDialog.cpp
Add a durable cookies dialog.
[PrivacyBrowserPC.git] / src / dialogs / CookiesDialog.cpp
index fa9679c457a4b4e58e906457cd367ca2ed577c30..612888d2e15fcc42e37ba765be9e6193dc1191ef 100644 (file)
@@ -20,6 +20,7 @@
 // Application headers.
 #include "AddOrEditCookieDialog.h"
 #include "CookiesDialog.h"
+#include "DurableCookiesDialog.h"
 #include "ui_CookiesDialog.h"
 #include "databases/CookiesDatabase.h"
 
@@ -32,7 +33,7 @@
 #include <QShortcut>
 #include <QUrl>
 
-// Construct the class.
+// Define the cookie sort predicate.
 bool cookieSortPredicate(const QNetworkCookie &leftHandCookie, const QNetworkCookie &rightHandCookie)
 {
     // Check to see if the domains are identical.
@@ -97,10 +98,10 @@ bool cookieSortPredicate(const QNetworkCookie &leftHandCookie, const QNetworkCoo
             // Sort the cookies by the top level domain.
             return (leftHandTopLevelDomain < rightHandTopLevelDomain);
         }
-
     }
 }
 
+// Construct the class.
 CookiesDialog::CookiesDialog(std::list<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
 {
     // Set the dialog window title.
@@ -217,7 +218,7 @@ CookiesDialog::CookiesDialog(std::list<QNetworkCookie> *originalCookieListPointe
     // Indicate that all the rows are the same height, wich improves performance.
     treeViewPointer->setUniformRowHeights(true);
 
-    // Disable editing.
+    // Disable editing in the tree view.
     treeViewPointer->setEditTriggers(QAbstractItemView::NoEditTriggers);
 
     // Set the tree model.
@@ -233,23 +234,26 @@ CookiesDialog::CookiesDialog(std::list<QNetworkCookie> *originalCookieListPointe
     addCookieButtonPointer = cookiesDialogUi.addCookieButton;
     editCookieButtonPointer = cookiesDialogUi.editCookieButton;
     deleteCookieButtonPointer = cookiesDialogUi.deleteCookieButton;
+    deleteAllButtonPointer = cookiesDialogUi.deleteAllCookiesButton;
     QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
     QPushButton *closeButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
 
-    // Add a delete all button to the dialog button box.
-    deleteAllButtonPointer = dialogButtonBoxPointer->addButton(i18nc("Delete all cookies button", "Delete all"), QDialogButtonBox::ActionRole);
+    // Add buttons to the dialog button box.
+    durableCookiesButtonPointer = dialogButtonBoxPointer->addButton(i18nc("View the durable cookies button", "Durable cookies - %1", CookiesDatabase::cookieCount()),
+                                                                    QDialogButtonBox::ActionRole);
 
-    // Set the delete all button icon.
-    deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
+    // Set the button icons.
+    durableCookiesButtonPointer->setIcon(QIcon::fromTheme("view-visible"));
 
     // Connect the buttons.
     connect(addCookieButtonPointer, SIGNAL(clicked()), this, SLOT(showAddCookieDialog()));
     connect(editCookieButtonPointer, SIGNAL(clicked()), this, SLOT(showEditCookieDialog()));
     connect(deleteCookieButtonPointer, SIGNAL(clicked()), this, SLOT(showDeleteCookieMessageBox()));
+    connect(durableCookiesButtonPointer, SIGNAL(clicked()), this, SLOT(showDurableCookiesDialog()));
     connect(deleteAllButtonPointer, SIGNAL(clicked()), this, SLOT(showDeleteAllMessageBox()));
     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
 
-    // Set the cancel button to be the default.
+    // Set the close button to be the default.
     closeButtonPointer->setDefault(true);
 
     // Create the keyboard shortcuts.
@@ -484,6 +488,48 @@ void CookiesDialog::deleteDomain(const QModelIndex &modelIndex, const bool &dele
         treeModelPointer->removeRow(modelIndex.row(), parentIndex);
 }
 
+void CookiesDialog::deleteCookieFromDatabase(const QNetworkCookie &cookie) const
+{
+    // Get a list of the matching domains.  There should only be one item in this list
+    QList<QStandardItem *> domainList = treeModelPointer->findItems(cookie.domain());
+
+    // Find any matching cookies.
+    for (QStandardItem *domainItemPointer : domainList)
+    {
+        // Get the number of cookies in the domain.
+        int numberOfCookies = domainItemPointer->rowCount();
+
+        // Initialize the tracking variables.
+        bool cookieFound = false;
+        int currentRow = 0;
+
+        // Find the cookie in the tree model.
+        while (!cookieFound && (currentRow < numberOfCookies))
+        {
+            // Get the name item.
+            QStandardItem *nameItemPointer = domainItemPointer->child(currentRow);
+
+            // Get the name model index.
+            QModelIndex nameModelIndex = nameItemPointer->index();
+
+            // Check to see if the name and the path match.
+            if ((nameModelIndex.data().toString() == cookie.name()) && (nameModelIndex.siblingAtColumn(2).data().toString() == cookie.path()))
+            {
+                // Set the current index.
+                treeSelectionModelPointer->setCurrentIndex(nameModelIndex, QItemSelectionModel::ClearAndSelect);
+
+                // Delete the cookie.
+                deleteCookieFromDialog(cookie);
+
+                // Mark the cookie as found.
+                cookieFound = true;
+            }
+
+            // Move to the next row.
+            ++currentRow;
+        }
+    }
+}
 
 void CookiesDialog::deleteCookieFromDialog(const QNetworkCookie &cookie) const
 {
@@ -654,6 +700,20 @@ void CookiesDialog::showDeleteCookieMessageBox() const
     }
 }
 
+void CookiesDialog::showDurableCookiesDialog() const
+{
+    // Instantiate a durable cookies dialog.
+    QDialog *durableCookiesDialogPointer = new DurableCookiesDialog();
+
+    // Show the dialog.
+    durableCookiesDialogPointer->show();
+
+    // Process cookie changes.
+    connect(durableCookiesDialogPointer, SIGNAL(addingCookie(QNetworkCookie, bool)), this, SLOT(addCookieFromDialog(QNetworkCookie, bool)));
+    connect(durableCookiesDialogPointer, SIGNAL(deletingCookie(QNetworkCookie)), this, SLOT(deleteCookieFromDatabase(QNetworkCookie)));
+    connect(durableCookiesDialogPointer, SIGNAL(updateParentUi()), this, SLOT(updateUi()));
+}
+
 void CookiesDialog::showEditCookieDialog() const
 {
     // Get the current model index.
@@ -718,4 +778,7 @@ void CookiesDialog::updateUi() const
         // Reset the button text.
         deleteCookieButtonPointer->setText(i18nc("Delete cookie button.", "&Delete cookie"));
     }
+
+    // Update the text of the durable cookies button.
+    durableCookiesButtonPointer->setText(i18nc("View the durable cookies button", "Durable cookies - %1", CookiesDatabase::cookieCount()));
 }