+/*
+ * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * 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/>.
+ */
+
+// Application headers.
+#include "AddCookieDialog.h"
+#include "ui_AddCookieDialog.h"
+
+// KDE Framework headers.
+#include <KLocalizedString>
+
+// Qt toolkit header.
+#include <QPushButton>
+#include <QUrl>
+
+AddCookieDialog::AddCookieDialog() : QDialog(nullptr)
+{
+ // Set the dialog window title.
+ setWindowTitle(i18nc("The add cookie dialog window title", "Add Cookie"));
+
+ // Set the window modality.
+ setWindowModality(Qt::WindowModality::ApplicationModal);
+
+ // Instantiate the cookie settings dialog UI.
+ Ui::AddCookieDialog addCookieDialogUi;
+
+ // Setup the UI.
+ addCookieDialogUi.setupUi(this);
+
+ // Get handles for the views.
+ domainLineEditPointer = addCookieDialogUi.domainLineEdit;
+ nameLineEditPointer = addCookieDialogUi.nameLineEdit;
+ expirationCheckBoxPointer = addCookieDialogUi.expirationCheckBox;
+ expirationDateTimeEditPointer = addCookieDialogUi.expirationDateTimeEdit;
+ pathLineEditPointer = addCookieDialogUi.pathLineEdit;
+ httpOnlyCheckBoxPointer = addCookieDialogUi.httpOnlyCheckBox;
+ secureCheckBoxPointer = addCookieDialogUi.secureCheckBox;
+ valueLineEditPointer = addCookieDialogUi.valueLineEdit;
+ QDialogButtonBox *dialogButtonBoxPointer = addCookieDialogUi.dialogButtonBox;
+ saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
+
+ // Connect the line edits.
+ connect(domainLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
+ connect(nameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
+ connect(pathLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
+ connect(valueLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(updateUi()));
+
+ // Connect the check boxes.
+ connect(expirationCheckBoxPointer, SIGNAL(stateChanged(int)), this, SLOT(updateExpirationDateTimeState(int)));
+
+ // Connect the buttons.
+ connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(saveCookie()));
+ connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+
+ // Update the UI.
+ updateUi();
+}
+
+void AddCookieDialog::saveCookie()
+{
+ // Create the variables.
+ QNetworkCookie cookie;
+
+ // Populate the cookie.
+ cookie.setDomain(domainLineEditPointer->text());
+ cookie.setName(nameLineEditPointer->text().toUtf8());
+ cookie.setPath(pathLineEditPointer->text());
+ cookie.setHttpOnly(httpOnlyCheckBoxPointer->isChecked());
+ cookie.setSecure(secureCheckBoxPointer->isChecked());
+ cookie.setValue(valueLineEditPointer->text().toUtf8());
+
+ // Populate the expiration date if it is specified.
+ if (expirationCheckBoxPointer->isChecked()) cookie.setExpirationDate(expirationDateTimeEditPointer->dateTime());
+
+ // Add the cookie.
+ emit addCookie(cookie);
+
+ // Close the dialog.
+ reject();
+}
+
+void AddCookieDialog::updateExpirationDateTimeState(const int &newState) const
+{
+ // Update the state of the of the expiration date time edit.
+ switch (newState)
+ {
+ case Qt::Unchecked:
+ // Disable the expiration date time.
+ expirationDateTimeEditPointer->setEnabled(false);
+
+ break;
+
+ case Qt::Checked:
+ // Enable the expiration date time edit.
+ expirationDateTimeEditPointer->setEnabled(true);
+
+ break;
+ }
+}
+
+void AddCookieDialog::updateUi() const
+{
+ // Update the state of the save button based on all the required fields containing text.
+ saveButtonPointer->setDisabled(domainLineEditPointer->text().isEmpty() || nameLineEditPointer->text().isEmpty() || pathLineEditPointer->text().isEmpty() ||
+ valueLineEditPointer->text().isEmpty());
+}