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 "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(const int &dialogType, const QNetworkCookie *cookiePointer, const bool &isDurable) : QDialog(nullptr)
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 settings 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 // Populate the expiration date if it exists.
87 if (!originalCookie.isSessionCookie())
89 // Check the expiration box.
90 expirationCheckBoxPointer->setChecked(true);
92 // Enable the expiration date time edit.
93 expirationDateTimeEditPointer->setEnabled(true);
95 // Set the expiration date.
96 expirationDateTimeEditPointer->setDateTime(originalCookie.expirationDate());
100 // Connect the line edits.
101 connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
102 connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
103 connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
104 connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
106 // Connect the check boxes.
107 connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
109 // Connect the buttons.
110 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
111 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
117 void AddOrEditCookieDialog::saveCookie()
119 // Delete the old cookie if this is an edit dialog.
121 emit deleteCookie(originalCookie);
123 // Create the variables.
124 QNetworkCookie cookie;
126 // Populate the cookie.
127 cookie.setDomain(domainLineEditPointer->text().toLower());
128 cookie.setName(nameLineEditPointer->text().toUtf8());
129 cookie.setPath(pathLineEditPointer->text());
130 cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
131 cookie.setSecure(secureCheckBoxPointer->isChecked());
132 cookie.setValue(valueLineEditPointer->text().toUtf8());
134 // Populate the expiration date if it is specified.
135 if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
137 // Get the durable status.
138 const bool isDurable = durableCheckBoxPointer->isChecked();
140 // Update the durable cookies database.
141 if (originalIsDurable) // The cookie is currently in the durable cookies database.
143 if (isDurable) // Update the cookie in the database.
145 qDebug() << "Updating a durable cookie.";
147 // Update the cookie in the durable cookies database.
148 CookiesDatabase::updateCookie(originalCookie, cookie);
150 else // Delete the cookie from the database.
152 qDebug() << "Deleting a durable cookie.";
154 // Delete the cookie from the durable cookies database.
155 CookiesDatabase::deleteCookie(originalCookie);
158 else if (isDurable) // The cookie is being added to the durable cookies database.
160 qDebug() << "Adding a durable cookie.";
162 // Add the cookie to the durable cookies database.
163 CookiesDatabase::addCookie(cookie);
166 // Add the cookie to the store, the list, and the tree.
167 emit addCookie(cookie, isDurable);
173 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
175 // Update the state of the of the expiration date time edit.
179 // Disable the expiration date time.
180 expirationDateTimeEditPointer->setEnabled(false);
185 // Enable the expiration date time edit.
186 expirationDateTimeEditPointer->setEnabled(true);
192 void AddOrEditCookieDialog::updateUi() const
194 // Update the state of the save button based on all the required fields containing text.
195 saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
196 valueLineEditPointer->text().isEmpty());