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 "AddCookieDialog.h"
22 #include "ui_AddCookieDialog.h"
24 // KDE Framework headers.
25 #include <KLocalizedString>
28 #include <QPushButton>
31 AddCookieDialog::AddCookieDialog() : QDialog(nullptr)
33 // Set the dialog window title.
34 setWindowTitle(i18nc("The add cookie dialog window title", "Add Cookie"));
36 // Set the window modality.
37 setWindowModality(Qt::WindowModality::ApplicationModal);
39 // Instantiate the cookie settings dialog UI.
40 Ui::AddCookieDialog addCookieDialogUi;
43 addCookieDialogUi.setupUi(this);
45 // Get handles for the views.
46 domainLineEditPointer = addCookieDialogUi.domainLineEdit;
47 nameLineEditPointer = addCookieDialogUi.nameLineEdit;
48 expirationCheckBoxPointer = addCookieDialogUi.expirationCheckBox;
49 expirationDateTimeEditPointer = addCookieDialogUi.expirationDateTimeEdit;
50 pathLineEditPointer = addCookieDialogUi.pathLineEdit;
51 httpOnlyCheckBoxPointer = addCookieDialogUi.httpOnlyCheckBox;
52 secureCheckBoxPointer = addCookieDialogUi.secureCheckBox;
53 valueLineEditPointer = addCookieDialogUi.valueLineEdit;
54 QDialogButtonBox *dialogButtonBoxPointer = addCookieDialogUi.dialogButtonBox;
55 saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
57 // Connect the line edits.
58 connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
59 connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
60 connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
61 connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
63 // Connect the check boxes.
64 connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
66 // Connect the buttons.
67 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
68 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
74 void AddCookieDialog::saveCookie()
76 // Create the variables.
77 QNetworkCookie cookie;
79 // Populate the cookie.
80 cookie.setDomain(domainLineEditPointer->text());
81 cookie.setName(nameLineEditPointer->text().toUtf8());
82 cookie.setPath(pathLineEditPointer->text());
83 cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
84 cookie.setSecure(secureCheckBoxPointer->isChecked());
85 cookie.setValue(valueLineEditPointer->text().toUtf8());
87 // Populate the expiration date if it is specified.
88 if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
91 emit addCookie(cookie);
97 void AddCookieDialog::updateExpirationDateTimeState(const int &newState) const
99 // Update the state of the of the expiration date time edit.
103 // Disable the expiration date time.
104 expirationDateTimeEditPointer->setEnabled(false);
109 // Enable the expiration date time edit.
110 expirationDateTimeEditPointer->setEnabled(true);
116 void AddCookieDialog::updateUi() const
118 // Update the state of the save button based on all the required fields containing text.
119 saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
120 valueLineEditPointer->text().isEmpty());