]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/CookiesDialog.cpp
3321c6569f9f069f582f7f8c3e04f8fdae97fec8
[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 "CookiesDialog.h"
22 #include "ui_CookiesDialog.h"
23 #include "ui_CookieWidget.h"
24
25 // The KDE Frameworks headers.
26 #include <KLocalizedString>
27
28 // The Qt toolkit headers.
29 #include <QDateTime>
30 #include <QMessageBox>
31
32 CookiesDialog::CookiesDialog(QList<QNetworkCookie> *originalCookieListPointer) : QDialog(nullptr), cookieListPointer(originalCookieListPointer)
33 {
34     // Set the dialog window title.
35     setWindowTitle(i18nc("The cookies dialog window title", "Cookies"));
36
37     // Set the window modality.
38     setWindowModality(Qt::WindowModality::WindowModal);
39
40     // Instantiate the cookie settings dialog UI.
41     Ui::CookiesDialog cookiesDialogUi;
42
43     // Setup the UI.
44     cookiesDialogUi.setupUi(this);
45
46     // Create the scroll area widget.
47     QWidget *scrollAreaWidgetPointer = new QWidget();
48
49     // Create the cookies VBox layout.
50     cookiesVBoxLayoutPointer = new QVBoxLayout();
51
52     // Populate the VBoxLayout.
53     for (QNetworkCookie cookie : *cookieListPointer)
54     {
55         // Create a cookie display widget.
56         QWidget *cookieDisplayWidgetPointer = new QWidget();
57
58         // Instantiate the cookie widget dialog UI.
59         Ui::CookieWidget cookieWidgetUi;
60
61         // Setup the UI.
62         cookieWidgetUi.setupUi(cookieDisplayWidgetPointer);
63
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;
72
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>");
81
82         // Add the widget to the cookies VBox layout.
83         cookiesVBoxLayoutPointer->addWidget(cookieDisplayWidgetPointer);
84
85         // Create a line.
86         QFrame *lineFrame = new QFrame();
87
88         // Format the line.
89         lineFrame->setFrameShape(QFrame::HLine);
90         lineFrame->setFrameShadow(QFrame::Sunken);
91
92         // Add the line to the cookies VBox layout.
93         cookiesVBoxLayoutPointer->addWidget(lineFrame);
94     }
95
96     // Set the scroll area widget layout.
97     scrollAreaWidgetPointer->setLayout(cookiesVBoxLayoutPointer);
98
99     // Get a handle for the scroll area.
100     QScrollArea *scrollAreaPointer = cookiesDialogUi.scrollArea;
101
102     // Set the scroll area widget.
103     scrollAreaPointer->setWidget(scrollAreaWidgetPointer);
104
105     // Get handles for the buttons.
106     QDialogButtonBox *dialogButtonBoxPointer = cookiesDialogUi.dialogButtonBox;
107     QPushButton *cancelButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
108
109     // Add a delete all button to the dialog button box.
110     deleteAllButtonPointer = dialogButtonBoxPointer->addButton(i18nc("Delete all cookies button", "Delete all"), QDialogButtonBox::ActionRole);
111
112     // Set the delete all button icon.
113     deleteAllButtonPointer->setIcon(QIcon::fromTheme("delete"));
114
115     // Connect the buttons.
116     connect(deleteAllButtonPointer, SIGNAL(released()), this, SLOT(showDeleteAllMessageBox()));
117     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
118
119     // Set the cancel button to be the default.
120     cancelButtonPointer->setDefault(true);
121
122     // Update the UI.
123     updateUi();
124 };
125
126 void CookiesDialog::showDeleteAllMessageBox() const
127 {
128     // Instantiate a delete all message box.
129     QMessageBox deleteAllCookiesMessageBox;
130
131     // Set the icon.
132     deleteAllCookiesMessageBox.setIcon(QMessageBox::Warning);
133
134     // Set the window title.
135     deleteAllCookiesMessageBox.setWindowTitle(i18nc("Delete all cookies dialog title", "Delete All Cookies"));
136
137     // Set the text.
138     deleteAllCookiesMessageBox.setText(i18nc("Delete all cookies dialog text", "Delete all cookies?"));
139
140     // Set the standard buttons.
141     deleteAllCookiesMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
142
143     // Set the default button.
144     deleteAllCookiesMessageBox.setDefaultButton(QMessageBox::No);
145
146     // Display the dialog and capture the return value.
147     int returnValue = deleteAllCookiesMessageBox.exec();
148
149     // Delete all cookies if instructed.
150     if (returnValue == QMessageBox::Yes)
151     {
152         // Delete all the cookies.
153         emit deleteAllCookies();
154
155         // Clear the cookie list.
156         cookieListPointer->clear();
157
158         // Create a layout item pointer.
159         QLayoutItem *layoutItemPointer;
160
161         // Delete each cookie widget.
162         while ((layoutItemPointer = cookiesVBoxLayoutPointer->takeAt(0)) != nullptr)
163         {
164             // Delete the widget.
165             delete layoutItemPointer->widget();
166
167             // Delete the layout.
168             delete layoutItemPointer;
169         }
170
171         // Update the UI.
172         updateUi();
173     }
174 }
175
176 void CookiesDialog::updateUi() const
177 {
178     // Set the status of the buttons.
179     deleteAllButtonPointer->setEnabled(cookieListPointer->count() > 0);
180 }