2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "CookiesDialog.h"
22 #include "ui_CookiesDialog.h"
23 #include "ui_CookieWidget.h"
25 // The KDE Frameworks headers.
26 #include <KLocalizedString>
28 // The Qt toolkit headers.
30 #include <QMessageBox>
32 CookiesDialog::CookiesDialog(QList<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
34 // Set the dialog window title.
35 setWindowTitle(i18nc("The cookies dialog window title", "Cookies"));
37 // Set the window modality.
38 setWindowModality(Qt::WindowModality::WindowModal);
40 // Instantiate the cookie settings dialog UI.
41 Ui::CookiesDialog cookiesDialogUi;
44 cookiesDialogUi.setupUi(this);
46 // Create the scroll area widget.
47 QWidget *scrollAreaWidgetPointer = new QWidget();
49 // Create the cookies VBox layout.
50 cookiesVBoxLayoutPointer = new QVBoxLayout();
52 // Populate the VBoxLayout.
53 for (QNetworkCookie cookie : *cookieListPointer)
55 // Create a cookie display widget.
56 QWidget *cookieDisplayWidgetPointer = new QWidget();
58 // Instantiate the cookie widget dialog UI.
59 Ui::CookieWidget cookieWidgetUi;
62 cookieWidgetUi.setupUi(cookieDisplayWidgetPointer);
64 // Get handles for the views.
65 QLabel *domainLabelPointer = cookieWidgetUi.domainLabel;
66 QLabel *nameLabelPointer = cookieWidgetUi.nameLabel;
67 QLabel *expirationDateLabelPointer = cookieWidgetUi.expirationDateLabel;
68 QLabel *pathLabelPointer = cookieWidgetUi.pathLabel;
69 QCheckBox *httpOnlyCheckBoxPointer = cookieWidgetUi.httpOnlyCheckBox;
70 QCheckBox *secureCheckBoxPointer = cookieWidgetUi.secureCheckBox;
71 QLabel *valueLabelPointer = cookieWidgetUi.valueLabel;
73 // Populate the views.
74 domainLabelPointer->setText("<font size=\"+1\"><b>" + cookie.domain() + "</b></font>");
75 nameLabelPointer->setText("<font size=\"+1\"><b>" + cookie.name() + "</b></font>");
76 expirationDateLabelPointer->setText("<b>" + cookie.expirationDate().toString() + "</b>");
77 pathLabelPointer->setText("<b>" + cookie.path() + "</b>");
78 httpOnlyCheckBoxPointer->setChecked(cookie.isHttpOnly());
79 secureCheckBoxPointer->setChecked(cookie.isSecure());
80 valueLabelPointer->setText("<b>" + cookie.value() + "</b>");
82 // Add the widget to the cookies VBox layout.
83 cookiesVBoxLayoutPointer->addWidget(cookieDisplayWidgetPointer);
86 QFrame *lineFrame = new QFrame();
89 lineFrame->setFrameShape(QFrame::HLine);
90 lineFrame->setFrameShadow(QFrame::Sunken);
92 // Add the line to the cookies VBox layout.
93 cookiesVBoxLayoutPointer->addWidget(lineFrame);
96 // Set the scroll area widget layout.
97 scrollAreaWidgetPointer->setLayout(cookiesVBoxLayoutPointer);
99 // Get a handle for the scroll area.
100 QScrollArea *scrollAreaPointer = cookiesDialogUi.scrollArea;
102 // Set the scroll area widget.
103 scrollAreaPointer->setWidget(scrollAreaWidgetPointer);
105 // Get handles for the buttons.
106 QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
107 QPushButton *cancelButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
109 // Add a delete all button to the dialog button box.
110 deleteAllButtonPointer = dialogButtonBoxPointer->addButton(i18nc("Delete all cookies button", "Delete all"), QDialogButtonBox::ActionRole);
112 // Set the delete all button icon.
113 deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
115 // Connect the buttons.
116 connect(deleteAllButtonPointer, SIGNAL(released()), this, SLOT(showDeleteAllMessageBox()));
117 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
119 // Set the cancel button to be the default.
120 cancelButtonPointer->setDefault(true);
126 void CookiesDialog::showDeleteAllMessageBox() const
128 // Instantiate a delete all message box.
129 QMessageBox deleteAllCookiesMessageBox;
132 deleteAllCookiesMessageBox.setIcon(QMessageBox::Warning);
134 // Set the window title.
135 deleteAllCookiesMessageBox.setWindowTitle(i18nc("Delete all cookies dialog title", "Delete All Cookies"));
138 deleteAllCookiesMessageBox.setText(i18nc("Delete all cookies dialog text", "Delete all cookies?"));
140 // Set the standard buttons.
141 deleteAllCookiesMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
143 // Set the default button.
144 deleteAllCookiesMessageBox.setDefaultButton(QMessageBox::No);
146 // Display the dialog and capture the return value.
147 int returnValue = deleteAllCookiesMessageBox.exec();
149 // Delete all cookies if instructed.
150 if (returnValue == QMessageBox::Yes)
152 // Delete all the cookies.
153 emit deleteAllCookies();
155 // Clear the cookie list.
156 cookieListPointer->clear();
158 // Create a layout item pointer.
159 QLayoutItem *layoutItemPointer;
161 // Delete each cookie widget.
162 while ((layoutItemPointer = cookiesVBoxLayoutPointer->takeAt(0)) != nullptr)
164 // Delete the widget.
165 delete layoutItemPointer->widget();
167 // Delete the layout.
168 delete layoutItemPointer;
176 void CookiesDialog::updateUi() const
178 // Set the status of the buttons.
179 deleteAllButtonPointer->setEnabled(cookieListPointer->count() > 0);