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