X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FCookiesDialog.cpp;fp=src%2Fdialogs%2FCookiesDialog.cpp;h=3321c6569f9f069f582f7f8c3e04f8fdae97fec8;hp=0000000000000000000000000000000000000000;hb=8933c941521c591a962034ecf3486c9143bf1f80;hpb=34816101e23ae2b489d64d540534125cf2c2e925 diff --git a/src/dialogs/CookiesDialog.cpp b/src/dialogs/CookiesDialog.cpp new file mode 100644 index 0000000..3321c65 --- /dev/null +++ b/src/dialogs/CookiesDialog.cpp @@ -0,0 +1,180 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of 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 . + */ + +// Application headers. +#include "CookiesDialog.h" +#include "ui_CookiesDialog.h" +#include "ui_CookieWidget.h" + +// The KDE Frameworks headers. +#include + +// The Qt toolkit headers. +#include +#include + +CookiesDialog::CookiesDialog(QList *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("" + cookie.domain() + ""); + nameLabelPointer->setText("" + cookie.name() + ""); + expirationDateLabelPointer->setText("" + cookie.expirationDate().toString() + ""); + pathLabelPointer->setText("" + cookie.path() + ""); + httpOnlyCheckBoxPointer->setChecked(cookie.isHttpOnly()); + secureCheckBoxPointer->setChecked(cookie.isSecure()); + valueLabelPointer->setText("" + cookie.value() + ""); + + // 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); +}