2 * Copyright 2022-2024 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 "AddOrEditCookieDialog.h"
22 #include "ui_AddOrEditCookieDialog.h"
23 #include "databases/CookiesDatabase.h"
25 // KDE Framework headers.
26 #include <KLocalizedString>
29 #include <QPushButton>
32 // Define the public static constants.
33 const int AddOrEditCookieDialog::AddCookie = 0;
34 const int AddOrEditCookieDialog::EditCookie = 1;
36 // Construct the class.
37 AddOrEditCookieDialog::AddOrEditCookieDialog(QWidget *parentWidgetPointer, const int dialogType, const QNetworkCookie *cookiePointer, const bool isDurable) : QDialog(parentWidgetPointer)
39 // Set the dialog window title according to the dialog type.
40 if (dialogType == AddCookie)
41 setWindowTitle(i18nc("The add cookie dialog window title.", "Add Cookie"));
43 setWindowTitle(i18nc("The edit cookie dialog window title.", "Edit Cookie"));
45 // Populate the class variables.
46 isEditDialog = (dialogType == EditCookie);
47 originalIsDurable = isDurable;
49 // Set the window modality.
50 setWindowModality(Qt::WindowModality::ApplicationModal);
52 // Instantiate the cookie dialog UI.
53 Ui::AddOrEditCookieDialog addOrEditCookieDialogUi;
56 addOrEditCookieDialogUi.setupUi(this);
58 // Get handles for the widgets.
59 domainLineEditPointer = addOrEditCookieDialogUi.domainLineEdit;
60 nameLineEditPointer = addOrEditCookieDialogUi.nameLineEdit;
61 durableCheckBoxPointer = addOrEditCookieDialogUi.durableCheckBox;
62 expirationCheckBoxPointer = addOrEditCookieDialogUi.expirationCheckBox;
63 expirationDateTimeEditPointer = addOrEditCookieDialogUi.expirationDateTimeEdit;
64 pathLineEditPointer = addOrEditCookieDialogUi.pathLineEdit;
65 httpOnlyCheckBoxPointer = addOrEditCookieDialogUi.httpOnlyCheckBox;
66 secureCheckBoxPointer = addOrEditCookieDialogUi.secureCheckBox;
67 valueLineEditPointer = addOrEditCookieDialogUi.valueLineEdit;
68 QDialogButtonBox *dialogButtonBoxPointer = addOrEditCookieDialogUi.dialogButtonBox;
69 saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
71 // Populate the dialog if editing a cookie.
74 // Store the old cookie.
75 originalCookie = *cookiePointer;
77 // Populate the widgets.
78 domainLineEditPointer->setText(originalCookie.domain());
79 durableCheckBoxPointer->setChecked(originalIsDurable);
80 nameLineEditPointer->setText(originalCookie.name());
81 pathLineEditPointer->setText(originalCookie.path());
82 httpOnlyCheckBoxPointer->setChecked(originalCookie.isHttpOnly());
83 secureCheckBoxPointer->setChecked(originalCookie.isSecure());
84 valueLineEditPointer->setText(originalCookie.value());
86 // Scroll to the beginning of the line edits.
87 domainLineEditPointer->setCursorPosition(0);
88 nameLineEditPointer->setCursorPosition(0);
89 pathLineEditPointer->setCursorPosition(0);
90 valueLineEditPointer->setCursorPosition(0);
92 // Populate the expiration date if it exists.
93 if (!originalCookie.isSessionCookie())
95 // Check the expiration box.
96 expirationCheckBoxPointer->setChecked(true);
98 // Enable the expiration date time edit.
99 expirationDateTimeEditPointer->setEnabled(true);
101 // Set the expiration date.
102 expirationDateTimeEditPointer->setDateTime(originalCookie.expirationDate());
106 // Connect the line edits.
107 connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
108 connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
109 connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
110 connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
112 // Connect the check boxes.
113 connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
115 // Connect the buttons.
116 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
117 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
123 void AddOrEditCookieDialog::saveCookie()
125 // Delete the old cookie if this is an edit dialog.
127 emit deleteCookie(originalCookie);
129 // Create the variables.
130 QNetworkCookie cookie;
132 // Populate the cookie.
133 cookie.setDomain(domainLineEditPointer->text().toLower());
134 cookie.setName(nameLineEditPointer->text().toUtf8());
135 cookie.setPath(pathLineEditPointer->text());
136 cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
137 cookie.setSecure(secureCheckBoxPointer->isChecked());
138 cookie.setValue(valueLineEditPointer->text().toUtf8());
140 // Populate the expiration date if it is specified.
141 if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
143 // Get the durable status.
144 const bool isDurable = durableCheckBoxPointer->isChecked();
146 // Update the durable cookies database.
147 if (originalIsDurable) // The cookie is currently in the durable cookies database.
149 if (isDurable) // Update the cookie in the database.
151 qDebug() << "Updating a durable cookie.";
153 // Update the cookie in the durable cookies database.
154 CookiesDatabase::updateCookie(originalCookie, cookie);
156 else // Delete the cookie from the database.
158 qDebug() << "Deleting a durable cookie.";
160 // Delete the cookie from the durable cookies database.
161 CookiesDatabase::deleteCookie(originalCookie);
164 else if (isDurable) // The cookie is being added to the durable cookies database.
166 qDebug() << "Adding a durable cookie.";
168 // Add the cookie to the durable cookies database.
169 CookiesDatabase::addCookie(cookie);
172 // Add the cookie to the store, the list, and the tree.
173 emit addCookie(cookie, isDurable);
179 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
181 // Update the state of the of the expiration date time edit.
185 // Disable the expiration date time.
186 expirationDateTimeEditPointer->setEnabled(false);
191 // Enable the expiration date time edit.
192 expirationDateTimeEditPointer->setEnabled(true);
198 void AddOrEditCookieDialog::updateUi() const
200 // Update the state of the save button based on all the required fields containing text.
201 saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
202 valueLineEditPointer->text().isEmpty());