]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddOrEditCookieDialog.cpp
Rename Local Storage to DOM Storage. https://redmine.stoutner.com/issues/852
[PrivacyBrowserPC.git] / src / dialogs / AddOrEditCookieDialog.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 "AddOrEditCookieDialog.h"
22 #include "ui_AddOrEditCookieDialog.h"
23
24 // KDE Framework headers.
25 #include <KLocalizedString>
26
27 // Qt toolkit header.
28 #include <QPushButton>
29 #include <QUrl>
30
31 // Define the public static constants.
32 const int AddOrEditCookieDialog::AddCookie = 0;
33 const int AddOrEditCookieDialog::EditCookie = 1;
34
35 // Construct the class.
36 AddOrEditCookieDialog::AddOrEditCookieDialog(const int &dialogType, const QNetworkCookie *cookiePointer) : QDialog(nullptr)
37 {
38     // Set the dialog window title according to the dialog type.
39     if (dialogType == AddCookie)
40         setWindowTitle(i18nc("The add cookie dialog window title.", "Add Cookie"));
41     else
42         setWindowTitle(i18nc("The edit cookie dialog window title.", "Edit Cookie"));
43
44     // Populate the is edit dialog boolean.
45     isEditDialog = (dialogType == EditCookie);
46
47     // Set the window modality.
48     setWindowModality(Qt::WindowModality::ApplicationModal);
49
50     // Instantiate the cookie settings dialog UI.
51     Ui::AddOrEditCookieDialog addOrEditCookieDialogUi;
52
53     // Setup the UI.
54     addOrEditCookieDialogUi.setupUi(this);
55
56     // Get handles for the widgets.
57     domainLineEditPointer = addOrEditCookieDialogUi.domainLineEdit;
58     nameLineEditPointer = addOrEditCookieDialogUi.nameLineEdit;
59     expirationCheckBoxPointer = addOrEditCookieDialogUi.expirationCheckBox;
60     expirationDateTimeEditPointer = addOrEditCookieDialogUi.expirationDateTimeEdit;
61     pathLineEditPointer = addOrEditCookieDialogUi.pathLineEdit;
62     httpOnlyCheckBoxPointer = addOrEditCookieDialogUi.httpOnlyCheckBox;
63     secureCheckBoxPointer = addOrEditCookieDialogUi.secureCheckBox;
64     valueLineEditPointer = addOrEditCookieDialogUi.valueLineEdit;
65     QDialogButtonBox *dialogButtonBoxPointer = addOrEditCookieDialogUi.dialogButtonBox;
66     saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
67
68     // Populate the dialog if editing a cookie.
69     if (isEditDialog)
70     {
71         // Store the old cookie.
72         oldCookie = *cookiePointer;
73
74         // Populate the widgets.
75         domainLineEditPointer->setText(oldCookie.domain());
76         nameLineEditPointer->setText(oldCookie.name());
77         pathLineEditPointer->setText(oldCookie.path());
78         httpOnlyCheckBoxPointer->setChecked(oldCookie.isHttpOnly());
79         secureCheckBoxPointer->setChecked(oldCookie.isSecure());
80         valueLineEditPointer->setText(oldCookie.value());
81
82         // Populate the expiration date if it exists.
83         if (!oldCookie.isSessionCookie())
84         {
85             // Check the expiration box.
86             expirationCheckBoxPointer->setChecked(true);
87
88             // Enable the expiration date time edit.
89             expirationDateTimeEditPointer->setEnabled(true);
90
91             // Set the expiration date.
92             expirationDateTimeEditPointer->setDateTime(oldCookie.expirationDate());
93         }
94     }
95
96     // Connect the line edits.
97     connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
98     connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
99     connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
100     connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
101
102     // Connect the check boxes.
103     connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
104
105     // Connect the buttons.
106     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
107     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
108
109     // Update the UI.
110     updateUi();
111 }
112
113 void AddOrEditCookieDialog::saveCookie()
114 {
115     // Delete the old cookie if this is an edit dialog.
116     if (isEditDialog)
117         emit deleteCookie(oldCookie);
118
119     // Create the variables.
120     QNetworkCookie cookie;
121
122     // Populate the cookie.
123     cookie.setDomain(domainLineEditPointer->text());
124     cookie.setName(nameLineEditPointer->text().toUtf8());
125     cookie.setPath(pathLineEditPointer->text());
126     cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
127     cookie.setSecure(secureCheckBoxPointer->isChecked());
128     cookie.setValue(valueLineEditPointer->text().toUtf8());
129
130     // Populate the expiration date if it is specified.
131     if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
132
133     // Add the cookie.
134     emit addCookie(cookie);
135
136     // Close the dialog.
137     reject();
138 }
139
140 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
141 {
142     // Update the state of the of the expiration date time edit.
143     switch (newState)
144     {
145         case Qt::Unchecked:
146             // Disable the expiration date time.
147             expirationDateTimeEditPointer->setEnabled(false);
148
149             break;
150
151         case Qt::Checked:
152             // Enable the expiration date time edit.
153             expirationDateTimeEditPointer->setEnabled(true);
154
155             break;
156     }
157 }
158
159 void AddOrEditCookieDialog::updateUi() const
160 {
161     // Update the state of the save button based on all the required fields containing text.
162     saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
163                                     valueLineEditPointer->text().isEmpty());
164 }