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"
24 // KDE Framework headers.
25 #include <KLocalizedString>
28 #include <QPushButton>
31 // Define the public static constants.
32 const int AddOrEditCookieDialog::AddCookie = 0;
33 const int AddOrEditCookieDialog::EditCookie = 1;
35 AddOrEditCookieDialog::AddOrEditCookieDialog(const int &dialogType, const QNetworkCookie *cookiePointer) : QDialog(nullptr)
37 // Set the dialog window title according to the dialog type.
38 if (dialogType == AddCookie)
39 setWindowTitle(i18nc("The add cookie dialog window title.", "Add Cookie"));
41 setWindowTitle(i18nc("The edit cookie dialog window title.", "Edit Cookie"));
43 // Populate the is edit dialog boolean.
44 isEditDialog = (dialogType == EditCookie);
46 // Set the window modality.
47 setWindowModality(Qt::WindowModality::ApplicationModal);
49 // Instantiate the cookie settings dialog UI.
50 Ui::AddOrEditCookieDialog addOrEditCookieDialogUi;
53 addOrEditCookieDialogUi.setupUi(this);
55 // Get handles for the widgets.
56 domainLineEditPointer = addOrEditCookieDialogUi.domainLineEdit;
57 nameLineEditPointer = addOrEditCookieDialogUi.nameLineEdit;
58 expirationCheckBoxPointer = addOrEditCookieDialogUi.expirationCheckBox;
59 expirationDateTimeEditPointer = addOrEditCookieDialogUi.expirationDateTimeEdit;
60 pathLineEditPointer = addOrEditCookieDialogUi.pathLineEdit;
61 httpOnlyCheckBoxPointer = addOrEditCookieDialogUi.httpOnlyCheckBox;
62 secureCheckBoxPointer = addOrEditCookieDialogUi.secureCheckBox;
63 valueLineEditPointer = addOrEditCookieDialogUi.valueLineEdit;
64 QDialogButtonBox *dialogButtonBoxPointer = addOrEditCookieDialogUi.dialogButtonBox;
65 saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
67 // Populate the dialog if editing a cookie.
70 // Store the old cookie.
71 oldCookie = *cookiePointer;
73 // Populate the widgets.
74 domainLineEditPointer->setText(oldCookie.domain());
75 nameLineEditPointer->setText(oldCookie.name());
76 pathLineEditPointer->setText(oldCookie.path());
77 httpOnlyCheckBoxPointer->setChecked(oldCookie.isHttpOnly());
78 secureCheckBoxPointer->setChecked(oldCookie.isSecure());
79 valueLineEditPointer->setText(oldCookie.value());
81 // Populate the expiration date if it exists.
82 if (!oldCookie.isSessionCookie())
84 // Check the expiration box.
85 expirationCheckBoxPointer->setChecked(true);
87 // Enable the expiration date time edit.
88 expirationDateTimeEditPointer->setEnabled(true);
90 // Set the expiration date.
91 expirationDateTimeEditPointer->setDateTime(oldCookie.expirationDate());
95 // Connect the line edits.
96 connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
97 connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
98 connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
99 connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
101 // Connect the check boxes.
102 connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
104 // Connect the buttons.
105 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
106 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
112 void AddOrEditCookieDialog::saveCookie()
114 // Delete the old cookie if this is an edit dialog.
116 emit deleteCookie(oldCookie);
118 // Create the variables.
119 QNetworkCookie cookie;
121 // Populate the cookie.
122 cookie.setDomain(domainLineEditPointer->text());
123 cookie.setName(nameLineEditPointer->text().toUtf8());
124 cookie.setPath(pathLineEditPointer->text());
125 cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
126 cookie.setSecure(secureCheckBoxPointer->isChecked());
127 cookie.setValue(valueLineEditPointer->text().toUtf8());
129 // Populate the expiration date if it is specified.
130 if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
133 emit addCookie(cookie);
139 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
141 // Update the state of the of the expiration date time edit.
145 // Disable the expiration date time.
146 expirationDateTimeEditPointer->setEnabled(false);
151 // Enable the expiration date time edit.
152 expirationDateTimeEditPointer->setEnabled(true);
158 void AddOrEditCookieDialog::updateUi() const
160 // Update the state of the save button based on all the required fields containing text.
161 saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
162 valueLineEditPointer->text().isEmpty());