]> 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-2024 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(QWidget *parentWidgetPointer, const int dialogType, const QNetworkCookie *cookiePointer, const bool isDurable) : QDialog(parentWidgetPointer)
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 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         // Scroll to the beginning of the line edits.
87         domainLineEditPointer->setCursorPosition(0);
88         nameLineEditPointer->setCursorPosition(0);
89         pathLineEditPointer->setCursorPosition(0);
90         valueLineEditPointer->setCursorPosition(0);
91
92         // Populate the expiration date if it exists.
93         if (!originalCookie.isSessionCookie())
94         {
95             // Check the expiration box.
96             expirationCheckBoxPointer->setChecked(true);
97
98             // Enable the expiration date time edit.
99             expirationDateTimeEditPointer->setEnabled(true);
100
101             // Set the expiration date.
102             expirationDateTimeEditPointer->setDateTime(originalCookie.expirationDate());
103         }
104     }
105
106     // Connect the line edits.
107     connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
108     connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
109     connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
110     connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
111
112     // Connect the check boxes.
113     connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
114
115     // Connect the buttons.
116     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
117     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
118
119     // Update the UI.
120     updateUi();
121 }
122
123 void AddOrEditCookieDialog::saveCookie()
124 {
125     // Delete the old cookie if this is an edit dialog.
126     if (isEditDialog)
127         emit deleteCookie(originalCookie);
128
129     // Create the variables.
130     QNetworkCookie cookie;
131
132     // Populate the cookie.
133     cookie.setDomain(domainLineEditPointer->text().toLower());
134     cookie.setName(nameLineEditPointer->text().toUtf8());
135     cookie.setPath(pathLineEditPointer->text());
136     cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
137     cookie.setSecure(secureCheckBoxPointer->isChecked());
138     cookie.setValue(valueLineEditPointer->text().toUtf8());
139
140     // Populate the expiration date if it is specified.
141     if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
142
143     // Get the durable status.
144     const bool isDurable = durableCheckBoxPointer->isChecked();
145
146     // Update the durable cookies database.
147     if (originalIsDurable)  // The cookie is currently in the durable cookies database.
148     {
149         if (isDurable)  // Update the cookie in the database.
150         {
151             qDebug() << "Updating a durable cookie.";
152
153             // Update the cookie in the durable cookies database.
154             CookiesDatabase::updateCookie(originalCookie, cookie);
155         }
156         else  // Delete the cookie from the database.
157         {
158             qDebug() << "Deleting a durable cookie.";
159
160             // Delete the cookie from the durable cookies database.
161             CookiesDatabase::deleteCookie(originalCookie);
162         }
163     }
164     else if (isDurable)  // The cookie is being added to the durable cookies database.
165     {
166         qDebug() << "Adding a durable cookie.";
167
168         // Add the cookie to the durable cookies database.
169         CookiesDatabase::addCookie(cookie);
170     }
171
172     // Add the cookie to the store, the list, and the tree.
173     emit addCookie(cookie, isDurable);
174
175     // Close the dialog.
176     close();
177 }
178
179 void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
180 {
181     // Update the state of the of the expiration date time edit.
182     switch (newState)
183     {
184         case Qt::Unchecked:
185             // Disable the expiration date time.
186             expirationDateTimeEditPointer->setEnabled(false);
187
188             break;
189
190         case Qt::Checked:
191             // Enable the expiration date time edit.
192             expirationDateTimeEditPointer->setEnabled(true);
193
194             break;
195     }
196 }
197
198 void AddOrEditCookieDialog::updateUi() const
199 {
200     // Update the state of the save button based on all the required fields containing text.
201     saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
202                                     valueLineEditPointer->text().isEmpty());
203 }