]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddOrEditCookieDialog.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[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 #include "databases/CookiesDatabase.h"
24
25 // KDE Framework headers.
26 #include <KLocalizedString>
27
28 // Qt toolkit header.
29 #include <QPushButton>
30 #include <QUrl>
31
32 // Define the public static constants.
33 const int AddOrEditCookieDialog::AddCookie = 0;
34 const int AddOrEditCookieDialog::EditCookie = 1;
35
36 // Construct the class.
37 AddOrEditCookieDialog::AddOrEditCookieDialog(const int &dialogType, const QNetworkCookie *cookiePointer, const bool &isDurable) : QDialog(nullptr)
38 {
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"));
42     else
43         setWindowTitle(i18nc("The edit cookie dialog window title.", "Edit Cookie"));
44
45     // Populate the class variables.
46     isEditDialog = (dialogType == EditCookie);
47     originalIsDurable = isDurable;
48
49     // Set the window modality.
50     setWindowModality(Qt::WindowModality::ApplicationModal);
51
52     // Instantiate the cookie settings dialog UI.
53     Ui::AddOrEditCookieDialog addOrEditCookieDialogUi;
54
55     // Setup the UI.
56     addOrEditCookieDialogUi.setupUi(this);
57
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);
70
71     // Populate the dialog if editing a cookie.
72     if (isEditDialog)
73     {
74         // Store the old cookie.
75         originalCookie = *cookiePointer;
76
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());
85
86         // Populate the expiration date if it exists.
87         if (!originalCookie.isSessionCookie())
88         {
89             // Check the expiration box.
90             expirationCheckBoxPointer->setChecked(true);
91
92             // Enable the expiration date time edit.
93             expirationDateTimeEditPointer->setEnabled(true);
94
95             // Set the expiration date.
96             expirationDateTimeEditPointer->setDateTime(originalCookie.expirationDate());
97         }
98     }
99
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()));
105
106     // Connect the check boxes.
107     connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
108
109     // Connect the buttons.
110     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
111     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
112
113     // Update the UI.
114     updateUi();
115 }
116
117 void AddOrEditCookieDialog::saveCookie()
118 {
119     // Delete the old cookie if this is an edit dialog.
120     if (isEditDialog)
121         emit deleteCookie(originalCookie);
122
123     // Create the variables.
124     QNetworkCookie cookie;
125
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());
133
134     // Populate the expiration date if it is specified.
135     if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
136
137     // Get the durable status.
138     const bool isDurable = durableCheckBoxPointer->isChecked();
139
140     // Update the durable cookies database.
141     if (originalIsDurable)  // The cookie is currently in the durable cookies database.
142     {
143         if (isDurable)  // Update the cookie in the database.
144         {
145             qDebug() << "Updating a durable cookie.";
146
147             // Update the cookie in the durable cookies database.
148             CookiesDatabase::updateCookie(originalCookie, cookie);
149         }
150         else  // Delete the cookie from the database.
151         {
152             qDebug() << "Deleting a durable cookie.";
153
154             // Delete the cookie from the durable cookies database.
155             CookiesDatabase::deleteCookie(originalCookie);
156         }
157     }
158     else if (isDurable)  // The cookie is being added to the durable cookies database.
159     {
160         qDebug() << "Adding a durable cookie.";
161
162         // Add the cookie to the durable cookies database.
163         CookiesDatabase::addCookie(cookie);
164     }
165
166     // Add the cookie to the store, the list, and the tree.
167     emit addCookie(cookie, isDurable);
168
169     // Close the dialog.
170     reject();
171 }
172
173 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
174 {
175     // Update the state of the of the expiration date time edit.
176     switch (newState)
177     {
178         case Qt::Unchecked:
179             // Disable the expiration date time.
180             expirationDateTimeEditPointer->setEnabled(false);
181
182             break;
183
184         case Qt::Checked:
185             // Enable the expiration date time edit.
186             expirationDateTimeEditPointer->setEnabled(true);
187
188             break;
189     }
190 }
191
192 void AddOrEditCookieDialog::updateUi() const
193 {
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());
197 }