]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddCookieDialog.cpp
Implement manual adding of cookies.
[PrivacyBrowserPC.git] / src / dialogs / AddCookieDialog.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 "AddCookieDialog.h"
22 #include "ui_AddCookieDialog.h"
23
24 // KDE Framework headers.
25 #include <KLocalizedString>
26
27 // Qt toolkit header.
28 #include <QPushButton>
29 #include <QUrl>
30
31 AddCookieDialog::AddCookieDialog() : QDialog(nullptr)
32 {
33    // Set the dialog window title.
34     setWindowTitle(i18nc("The add cookie dialog window title", "Add Cookie"));
35
36     // Set the window modality.
37     setWindowModality(Qt::WindowModality::ApplicationModal);
38
39     // Instantiate the cookie settings dialog UI.
40     Ui::AddCookieDialog addCookieDialogUi;
41
42     // Setup the UI.
43     addCookieDialogUi.setupUi(this);
44
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);
56
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()));
62
63     // Connect the check boxes.
64     connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
65
66     // Connect the buttons.
67     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
68     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
69
70     // Update the UI.
71     updateUi();
72 }
73
74 void AddCookieDialog::saveCookie()
75 {
76     // Create the variables.
77     QNetworkCookie cookie;
78
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());
86
87     // Populate the expiration date if it is specified.
88     if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
89
90     // Add the cookie.
91     emit addCookie(cookie);
92
93     // Close the dialog.
94     reject();
95 }
96
97 void AddCookieDialog::updateExpirationDateTimeState(const int &newState) const
98 {
99     // Update the state of the of the expiration date time edit.
100     switch (newState)
101     {
102         case Qt::Unchecked:
103             // Disable the expiration date time.
104             expirationDateTimeEditPointer->setEnabled(false);
105
106             break;
107
108         case Qt::Checked:
109             // Enable the expiration date time edit.
110             expirationDateTimeEditPointer->setEnabled(true);
111
112             break;
113     }
114 }
115
116 void AddCookieDialog::updateUi() const
117 {
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());
121 }