-/*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ * SPDX-FileCopyrightText: 2022-2024 Soren Stoutner <soren@stoutner.com>
*
- * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc/>.
*
- * Privacy Browser PC is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
*
- * Privacy Browser PC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
*
- * You should have received a copy of the GNU General Public License
- * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Application headers.
#include "AddOrEditCookieDialog.h"
#include "ui_AddOrEditCookieDialog.h"
+#include "databases/CookiesDatabase.h"
// KDE Framework headers.
#include <KLocalizedString>
const int AddOrEditCookieDialog::AddCookie = 0;
const int AddOrEditCookieDialog::EditCookie = 1;
-AddOrEditCookieDialog::AddOrEditCookieDialog(const int &dialogType, const QNetworkCookie *cookiePointer) : QDialog(nullptr)
+// Construct the class.
+AddOrEditCookieDialog::AddOrEditCookieDialog(QWidget *parentWidgetPointer, const int dialogType, const QNetworkCookie *cookiePointer, const bool isDurable) : QDialog(parentWidgetPointer)
{
// Set the dialog window title according to the dialog type.
if (dialogType == AddCookie)
else
setWindowTitle(i18nc("The edit cookie dialog window title.", "Edit Cookie"));
- // Populate the is edit dialog boolean.
+ // Populate the class variables.
isEditDialog = (dialogType == EditCookie);
+ originalIsDurable = isDurable;
// Set the window modality.
setWindowModality(Qt::WindowModality::ApplicationModal);
- // Instantiate the cookie settings dialog UI.
+ // Instantiate the cookie dialog UI.
Ui::AddOrEditCookieDialog addOrEditCookieDialogUi;
// Setup the UI.
// Get handles for the widgets.
domainLineEditPointer = addOrEditCookieDialogUi.domainLineEdit;
nameLineEditPointer = addOrEditCookieDialogUi.nameLineEdit;
+ durableCheckBoxPointer = addOrEditCookieDialogUi.durableCheckBox;
expirationCheckBoxPointer = addOrEditCookieDialogUi.expirationCheckBox;
expirationDateTimeEditPointer = addOrEditCookieDialogUi.expirationDateTimeEdit;
pathLineEditPointer = addOrEditCookieDialogUi.pathLineEdit;
if (isEditDialog)
{
// Store the old cookie.
- oldCookie = *cookiePointer;
+ originalCookie = *cookiePointer;
// Populate the widgets.
- domainLineEditPointer->setText(oldCookie.domain());
- nameLineEditPointer->setText(oldCookie.name());
- pathLineEditPointer->setText(oldCookie.path());
- httpOnlyCheckBoxPointer->setChecked(oldCookie.isHttpOnly());
- secureCheckBoxPointer->setChecked(oldCookie.isSecure());
- valueLineEditPointer->setText(oldCookie.value());
+ domainLineEditPointer->setText(originalCookie.domain());
+ durableCheckBoxPointer->setChecked(originalIsDurable);
+ nameLineEditPointer->setText(QLatin1String(originalCookie.name()));
+ pathLineEditPointer->setText(originalCookie.path());
+ httpOnlyCheckBoxPointer->setChecked(originalCookie.isHttpOnly());
+ secureCheckBoxPointer->setChecked(originalCookie.isSecure());
+ valueLineEditPointer->setText(QLatin1String(originalCookie.value()));
+
+ // Scroll to the beginning of the line edits.
+ domainLineEditPointer->setCursorPosition(0);
+ nameLineEditPointer->setCursorPosition(0);
+ pathLineEditPointer->setCursorPosition(0);
+ valueLineEditPointer->setCursorPosition(0);
// Populate the expiration date if it exists.
- if (!oldCookie.isSessionCookie())
+ if (!originalCookie.isSessionCookie())
{
// Check the expiration box.
expirationCheckBoxPointer->setChecked(true);
expirationDateTimeEditPointer->setEnabled(true);
// Set the expiration date.
- expirationDateTimeEditPointer->setDateTime(oldCookie.expirationDate());
+ expirationDateTimeEditPointer->setDateTime(originalCookie.expirationDate());
}
}
{
// Delete the old cookie if this is an edit dialog.
if (isEditDialog)
- emit deleteCookie(oldCookie);
+ Q_EMIT deleteCookie(originalCookie);
// Create the variables.
QNetworkCookie cookie;
// Populate the cookie.
- cookie.setDomain(domainLineEditPointer->text());
+ cookie.setDomain(domainLineEditPointer->text().toLower());
cookie.setName(nameLineEditPointer->text().toUtf8());
cookie.setPath(pathLineEditPointer->text());
cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
// Populate the expiration date if it is specified.
if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
- // Add the cookie.
- emit addCookie(cookie);
+ // Get the durable status.
+ const bool isDurable = durableCheckBoxPointer->isChecked();
+
+ // Update the durable cookies database.
+ if (originalIsDurable) // The cookie is currently in the durable cookies database.
+ {
+ if (isDurable) // Update the cookie in the database.
+ {
+ qDebug() << "Updating a durable cookie.";
+
+ // Update the cookie in the durable cookies database.
+ CookiesDatabase::updateCookie(originalCookie, cookie);
+ }
+ else // Delete the cookie from the database.
+ {
+ qDebug() << "Deleting a durable cookie.";
+
+ // Delete the cookie from the durable cookies database.
+ CookiesDatabase::deleteCookie(originalCookie);
+ }
+ }
+ else if (isDurable) // The cookie is being added to the durable cookies database.
+ {
+ qDebug() << "Adding a durable cookie.";
+
+ // Add the cookie to the durable cookies database.
+ CookiesDatabase::addCookie(cookie);
+ }
+
+ // Add the cookie to the store, the list, and the tree.
+ Q_EMIT addCookie(cookie, isDurable);
// Close the dialog.
- reject();
+ close();
}
void AddOrEditCookieDialog::updateExpirationDateTimeState(const int &newState) const
saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
valueLineEditPointer->text().isEmpty());
}
+