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 "AddCookieDialog.h"
22 #include "CookiesDialog.h"
23 #include "ui_CookieDisplayWidget.h"
24 #include "ui_CookiesDialog.h"
26 // KDE Frameworks headers.
27 #include <KLocalizedString>
29 // Qt toolkit headers.
31 #include <QMessageBox>
34 CookiesDialog::CookiesDialog(QList<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
36 // Set the dialog window title.
37 setWindowTitle(i18nc("The cookies dialog window title", "Cookies"));
39 // Set the window modality.
40 setWindowModality(Qt::WindowModality::ApplicationModal);
42 // Instantiate the cookie settings dialog UI.
43 Ui::CookiesDialog cookiesDialogUi;
46 cookiesDialogUi.setupUi(this);
48 // Get a handle for the scroll area.
49 QScrollArea *scrollAreaPointer = cookiesDialogUi.scrollArea;
51 // Create the scroll area widget.
52 QWidget *scrollAreaWidgetPointer = new QWidget();
54 // Set the scroll area widget.
55 scrollAreaPointer->setWidget(scrollAreaWidgetPointer);
57 // Create a scroll area VBox layout.
58 QVBoxLayout *scrollAreaVBoxLayoutPointer = new QVBoxLayout();
60 // Set the scroll area widget layout.
61 scrollAreaWidgetPointer->setLayout(scrollAreaVBoxLayoutPointer);
63 // Create the cookies VBox layout.
64 cookiesVBoxLayoutPointer = new QVBoxLayout();
66 // Populate the scroll area VBox layout. The stretch prevents the cookies from expanding vertically if they are smaller than the dialog.
67 scrollAreaVBoxLayoutPointer->addLayout(cookiesVBoxLayoutPointer);
68 scrollAreaVBoxLayoutPointer->addStretch();
70 // Populate the VBoxLayout.
71 for (QNetworkCookie cookie : *cookieListPointer)
73 // Add the cookie to the layout.
74 addCookieToLayout(cookie);
77 // Get handles for the buttons.
78 addCookieButtonPointer = cookiesDialogUi.addCookieButton;
79 QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
80 QPushButton *cancelButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
82 // Add a delete all button to the dialog button box.
83 deleteAllButtonPointer = dialogButtonBoxPointer->addButton(i18nc("Delete all cookies button", "Delete all"), QDialogButtonBox::ActionRole);
85 // Set the delete all button icon.
86 deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
88 // Connect the buttons.
89 connect(addCookieButtonPointer, SIGNAL(clicked()), this, SLOT(showAddCookieMessageBox()));
90 connect(deleteAllButtonPointer, SIGNAL(clicked()), this, SLOT(showDeleteAllMessageBox()));
91 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
93 // Set the cancel button to be the default.
94 cancelButtonPointer->setDefault(true);
100 void CookiesDialog::addCookieFromDialog(const QNetworkCookie &cookie) const
102 // Add the cookie to the cookie list and the cookie store.
103 emit addCookie(cookie);
105 // Add the cookie to the VBox layout.
106 addCookieToLayout(cookie);
109 void CookiesDialog::addCookieToLayout(const QNetworkCookie &cookie) const
111 // Create a cookie display widget.
112 QWidget *cookieDisplayWidgetPointer = new QWidget();
114 // Instantiate the cookie widget dialog UI.
115 Ui::CookieDisplayWidget cookieDisplayWidgetUi;
118 cookieDisplayWidgetUi.setupUi(cookieDisplayWidgetPointer);
120 // Get handles for the views.
121 QLabel *domainLabelPointer = cookieDisplayWidgetUi.domainLabel;
122 QLabel *nameLabelPointer = cookieDisplayWidgetUi.nameLabel;
123 QLabel *expirationDateLabelPointer = cookieDisplayWidgetUi.expirationDateLabel;
124 QLabel *pathLabelPointer = cookieDisplayWidgetUi.pathLabel;
125 QCheckBox *httpOnlyCheckBoxPointer = cookieDisplayWidgetUi.httpOnlyCheckBox;
126 QCheckBox *secureCheckBoxPointer = cookieDisplayWidgetUi.secureCheckBox;
127 QLabel *valueLabelPointer = cookieDisplayWidgetUi.valueLabel;
129 // Populate the views.
130 domainLabelPointer->setText("<font size=\"+1\"><b>" + cookie.domain() + "</b></font>");
131 nameLabelPointer->setText("<font size=\"+1\"><b>" + cookie.name() + "</b></font>");
132 expirationDateLabelPointer->setText("<b>" + cookie.expirationDate().toString() + "</b>");
133 pathLabelPointer->setText("<b>" + cookie.path() + "</b>");
134 httpOnlyCheckBoxPointer->setChecked(cookie.isHttpOnly());
135 secureCheckBoxPointer->setChecked(cookie.isSecure());
136 valueLabelPointer->setText("<b>" + cookie.value() + "</b>");
138 // Add the cookie display widget to the cookies VBox layout.
139 cookiesVBoxLayoutPointer->addWidget(cookieDisplayWidgetPointer);
142 QFrame *lineFrame = new QFrame();
145 lineFrame->setFrameShape(QFrame::HLine);
146 lineFrame->setFrameShadow(QFrame::Sunken);
148 // Add the line to the cookies VBox layout.
149 cookiesVBoxLayoutPointer->addWidget(lineFrame);
152 void CookiesDialog::showAddCookieMessageBox() const
154 // Instantiate an add cookie dialog.
155 QDialog *addCookieDialogPointer = new AddCookieDialog();
158 addCookieDialogPointer->show();
160 // Add the cookie if directed.
161 connect(addCookieDialogPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieFromDialog(QNetworkCookie)));
164 void CookiesDialog::showDeleteAllMessageBox() const
166 // Instantiate a delete all message box.
167 QMessageBox deleteAllCookiesMessageBox;
170 deleteAllCookiesMessageBox.setIcon(QMessageBox::Warning);
172 // Set the window title.
173 deleteAllCookiesMessageBox.setWindowTitle(i18nc("Delete all cookies dialog title", "Delete All Cookies"));
176 deleteAllCookiesMessageBox.setText(i18nc("Delete all cookies dialog text", "Delete all cookies?"));
178 // Set the standard buttons.
179 deleteAllCookiesMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
181 // Set the default button.
182 deleteAllCookiesMessageBox.setDefaultButton(QMessageBox::No);
184 // Display the dialog and capture the return value.
185 int returnValue = deleteAllCookiesMessageBox.exec();
187 // Delete all cookies if instructed.
188 if (returnValue == QMessageBox::Yes)
190 // Delete all the cookies.
191 emit deleteAllCookies();
193 // Clear the cookie list.
194 cookieListPointer->clear();
196 // Create a layout item pointer.
197 QLayoutItem *layoutItemPointer;
199 // Delete each cookie widget.
200 while ((layoutItemPointer = cookiesVBoxLayoutPointer->takeAt(0)) != nullptr)
202 // Delete the widget.
203 delete layoutItemPointer->widget();
205 // Delete the layout.
206 delete layoutItemPointer;
214 void CookiesDialog::updateUi() const
216 // Set the status of the buttons.
217 deleteAllButtonPointer->setEnabled(cookieListPointer->count() > 0);