]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/CookiesDialog.cpp
Initial Cookies implementation using a QVBoxLayout.
[PrivacyBrowserPC.git] / src / dialogs / CookiesDialog.cpp
diff --git a/src/dialogs/CookiesDialog.cpp b/src/dialogs/CookiesDialog.cpp
new file mode 100644 (file)
index 0000000..3321c65
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ *
+ * Privacy Browser PC is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser PC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Application headers.
+#include "CookiesDialog.h"
+#include "ui_CookiesDialog.h"
+#include "ui_CookieWidget.h"
+
+// The KDE Frameworks headers.
+#include <KLocalizedString>
+
+// The Qt toolkit headers.
+#include <QDateTime>
+#include <QMessageBox>
+
+CookiesDialog::CookiesDialog(QList<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
+{
+    // Set the dialog window title.
+    setWindowTitle(i18nc("The cookies dialog window title", "Cookies"));
+
+    // Set the window modality.
+    setWindowModality(Qt::WindowModality::WindowModal);
+
+    // Instantiate the cookie settings dialog UI.
+    Ui::CookiesDialog cookiesDialogUi;
+
+    // Setup the UI.
+    cookiesDialogUi.setupUi(this);
+
+    // Create the scroll area widget.
+    QWidget *scrollAreaWidgetPointer = new QWidget();
+
+    // Create the cookies VBox layout.
+    cookiesVBoxLayoutPointer = new QVBoxLayout();
+
+    // Populate the VBoxLayout.
+    for (QNetworkCookie cookie : *cookieListPointer)
+    {
+        // Create a cookie display widget.
+        QWidget *cookieDisplayWidgetPointer = new QWidget();
+
+        // Instantiate the cookie widget dialog UI.
+        Ui::CookieWidget cookieWidgetUi;
+
+        // Setup the UI.
+        cookieWidgetUi.setupUi(cookieDisplayWidgetPointer);
+
+        // Get handles for the views.
+        QLabel *domainLabelPointer = cookieWidgetUi.domainLabel;
+        QLabel *nameLabelPointer = cookieWidgetUi.nameLabel;
+        QLabel *expirationDateLabelPointer = cookieWidgetUi.expirationDateLabel;
+        QLabel *pathLabelPointer = cookieWidgetUi.pathLabel;
+        QCheckBox *httpOnlyCheckBoxPointer = cookieWidgetUi.httpOnlyCheckBox;
+        QCheckBox *secureCheckBoxPointer = cookieWidgetUi.secureCheckBox;
+        QLabel *valueLabelPointer = cookieWidgetUi.valueLabel;
+
+        // Populate the views.
+        domainLabelPointer->setText("<font size=\"+1\"><b>" + cookie.domain() + "</b></font>");
+        nameLabelPointer->setText("<font size=\"+1\"><b>" + cookie.name() + "</b></font>");
+        expirationDateLabelPointer->setText("<b>" + cookie.expirationDate().toString() + "</b>");
+        pathLabelPointer->setText("<b>" + cookie.path() + "</b>");
+        httpOnlyCheckBoxPointer->setChecked(cookie.isHttpOnly());
+        secureCheckBoxPointer->setChecked(cookie.isSecure());
+        valueLabelPointer->setText("<b>" + cookie.value() + "</b>");
+
+        // Add the widget to the cookies VBox layout.
+        cookiesVBoxLayoutPointer->addWidget(cookieDisplayWidgetPointer);
+
+        // Create a line.
+        QFrame *lineFrame = new QFrame();
+
+        // Format the line.
+        lineFrame->setFrameShape(QFrame::HLine);
+        lineFrame->setFrameShadow(QFrame::Sunken);
+
+        // Add the line to the cookies VBox layout.
+        cookiesVBoxLayoutPointer->addWidget(lineFrame);
+    }
+
+    // Set the scroll area widget layout.
+    scrollAreaWidgetPointer->setLayout(cookiesVBoxLayoutPointer);
+
+    // Get a handle for the scroll area.
+    QScrollArea *scrollAreaPointer = cookiesDialogUi.scrollArea;
+
+    // Set the scroll area widget.
+    scrollAreaPointer->setWidget(scrollAreaWidgetPointer);
+
+    // Get handles for the buttons.
+    QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
+    QPushButton *cancelButtonPointer = 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);
+
+    // Set the delete all button icon.
+    deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
+
+    // Connect the buttons.
+    connect(deleteAllButtonPointer, SIGNAL(released()), this, SLOT(showDeleteAllMessageBox()));
+    connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+
+    // Set the cancel button to be the default.
+    cancelButtonPointer->setDefault(true);
+
+    // Update the UI.
+    updateUi();
+};
+
+void CookiesDialog::showDeleteAllMessageBox() const
+{
+    // Instantiate a delete all message box.
+    QMessageBox deleteAllCookiesMessageBox;
+
+    // Set the icon.
+    deleteAllCookiesMessageBox.setIcon(QMessageBox::Warning);
+
+    // Set the window title.
+    deleteAllCookiesMessageBox.setWindowTitle(i18nc("Delete all cookies dialog title", "Delete All Cookies"));
+
+    // Set the text.
+    deleteAllCookiesMessageBox.setText(i18nc("Delete all cookies dialog text", "Delete all cookies?"));
+
+    // Set the standard buttons.
+    deleteAllCookiesMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+
+    // Set the default button.
+    deleteAllCookiesMessageBox.setDefaultButton(QMessageBox::No);
+
+    // Display the dialog and capture the return value.
+    int returnValue = deleteAllCookiesMessageBox.exec();
+
+    // Delete all cookies if instructed.
+    if (returnValue == QMessageBox::Yes)
+    {
+        // Delete all the cookies.
+        emit deleteAllCookies();
+
+        // Clear the cookie list.
+        cookieListPointer->clear();
+
+        // Create a layout item pointer.
+        QLayoutItem *layoutItemPointer;
+
+        // Delete each cookie widget.
+        while ((layoutItemPointer = cookiesVBoxLayoutPointer->takeAt(0)) != nullptr)
+        {
+            // Delete the widget.
+            delete layoutItemPointer->widget();
+
+            // Delete the layout.
+            delete layoutItemPointer;
+        }
+
+        // Update the UI.
+        updateUi();
+    }
+}
+
+void CookiesDialog::updateUi() const
+{
+    // Set the status of the buttons.
+    deleteAllButtonPointer->setEnabled(cookieListPointer->count() > 0);
+}