]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/CookiesDialog.cpp
972bc67d1d49b132a9fb4634f9631ff22804012c
[PrivacyBrowserPC.git] / src / dialogs / CookiesDialog.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "AddCookieDialog.h"
22 #include "CookiesDialog.h"
23 #include "ui_CookieDisplayWidget.h"
24 #include "ui_CookiesDialog.h"
25
26 // KDE Frameworks headers.
27 #include <KLocalizedString>
28
29 // Qt toolkit headers.
30 #include <QDateTime>
31 #include <QMessageBox>
32 #include <QUrl>
33
34 CookiesDialog::CookiesDialog(QList<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
35 {
36     // Set the dialog window title.
37     setWindowTitle(i18nc("The cookies dialog window title", "Cookies"));
38
39     // Set the window modality.
40     setWindowModality(Qt::WindowModality::ApplicationModal);
41
42     // Instantiate the cookie settings dialog UI.
43     Ui::CookiesDialog cookiesDialogUi;
44
45     // Setup the UI.
46     cookiesDialogUi.setupUi(this);
47
48     // Get a handle for the scroll area.
49     QScrollArea *scrollAreaPointer = cookiesDialogUi.scrollArea;
50
51     // Create the scroll area widget.
52     QWidget *scrollAreaWidgetPointer = new QWidget();
53
54     // Set the scroll area widget.
55     scrollAreaPointer->setWidget(scrollAreaWidgetPointer);
56
57     // Create a scroll area VBox layout.
58     QVBoxLayout *scrollAreaVBoxLayoutPointer = new QVBoxLayout();
59
60     // Set the scroll area widget layout.
61     scrollAreaWidgetPointer->setLayout(scrollAreaVBoxLayoutPointer);
62
63     // Create the cookies VBox layout.
64     cookiesVBoxLayoutPointer = new QVBoxLayout();
65
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();
69
70     // Populate the VBoxLayout.
71     for (QNetworkCookie cookie : *cookieListPointer)
72     {
73         // Add the cookie to the layout.
74         addCookieToLayout(cookie);
75     }
76
77     // Get handles for the buttons.
78     addCookieButtonPointer = cookiesDialogUi.addCookieButton;
79     QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
80     QPushButton *cancelButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
81
82     // Add a delete all button to the dialog button box.
83     deleteAllButtonPointer = dialogButtonBoxPointer->addButton(i18nc("Delete all cookies button", "Delete all"), QDialogButtonBox::ActionRole);
84
85     // Set the delete all button icon.
86     deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
87
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()));
92
93     // Set the cancel button to be the default.
94     cancelButtonPointer->setDefault(true);
95
96     // Update the UI.
97     updateUi();
98 };
99
100 void CookiesDialog::addCookieFromDialog(const QNetworkCookie &cookie) const
101 {
102     // Add the cookie to the cookie list and the cookie store.
103     emit addCookie(cookie);
104
105     // Add the cookie to the VBox layout.
106     addCookieToLayout(cookie);
107 }
108
109 void CookiesDialog::addCookieToLayout(const QNetworkCookie &cookie) const
110 {
111     // Create a cookie display widget.
112     QWidget *cookieDisplayWidgetPointer = new QWidget();
113
114     // Instantiate the cookie widget dialog UI.
115     Ui::CookieDisplayWidget cookieDisplayWidgetUi;
116
117     // Setup the UI.
118     cookieDisplayWidgetUi.setupUi(cookieDisplayWidgetPointer);
119
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;
128
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>");
137
138     // Add the cookie display widget to the cookies VBox layout.
139     cookiesVBoxLayoutPointer->addWidget(cookieDisplayWidgetPointer);
140
141     // Create a line.
142     QFrame *lineFrame = new QFrame();
143
144     // Format the line.
145     lineFrame->setFrameShape(QFrame::HLine);
146     lineFrame->setFrameShadow(QFrame::Sunken);
147
148     // Add the line to the cookies VBox layout.
149     cookiesVBoxLayoutPointer->addWidget(lineFrame);
150 }
151
152 void CookiesDialog::showAddCookieMessageBox() const
153 {
154     // Instantiate an add cookie dialog.
155     QDialog *addCookieDialogPointer = new AddCookieDialog();
156
157     // Show the dialog.
158     addCookieDialogPointer->show();
159
160     // Add the cookie if directed.
161     connect(addCookieDialogPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieFromDialog(QNetworkCookie)));
162 }
163
164 void CookiesDialog::showDeleteAllMessageBox() const
165 {
166     // Instantiate a delete all message box.
167     QMessageBox deleteAllCookiesMessageBox;
168
169     // Set the icon.
170     deleteAllCookiesMessageBox.setIcon(QMessageBox::Warning);
171
172     // Set the window title.
173     deleteAllCookiesMessageBox.setWindowTitle(i18nc("Delete all cookies dialog title", "Delete All Cookies"));
174
175     // Set the text.
176     deleteAllCookiesMessageBox.setText(i18nc("Delete all cookies dialog text", "Delete all cookies?"));
177
178     // Set the standard buttons.
179     deleteAllCookiesMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
180
181     // Set the default button.
182     deleteAllCookiesMessageBox.setDefaultButton(QMessageBox::No);
183
184     // Display the dialog and capture the return value.
185     int returnValue = deleteAllCookiesMessageBox.exec();
186
187     // Delete all cookies if instructed.
188     if (returnValue == QMessageBox::Yes)
189     {
190         // Delete all the cookies.
191         emit deleteAllCookies();
192
193         // Clear the cookie list.
194         cookieListPointer->clear();
195
196         // Create a layout item pointer.
197         QLayoutItem *layoutItemPointer;
198
199         // Delete each cookie widget.
200         while ((layoutItemPointer = cookiesVBoxLayoutPointer->takeAt(0)) != nullptr)
201         {
202             // Delete the widget.
203             delete layoutItemPointer->widget();
204
205             // Delete the layout.
206             delete layoutItemPointer;
207         }
208
209         // Update the UI.
210         updateUi();
211     }
212 }
213
214 void CookiesDialog::updateUi() const
215 {
216     // Set the status of the buttons.
217     deleteAllButtonPointer->setEnabled(cookieListPointer->count() > 0);
218 }