X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FAddCookieDialog.cpp;fp=src%2Fdialogs%2FAddCookieDialog.cpp;h=a81d4338bd4df79567b3b485e043c43ce3fb3d72;hp=0000000000000000000000000000000000000000;hb=9b6cee96126484925bec4f4ab30c2b880df687fe;hpb=8933c941521c591a962034ecf3486c9143bf1f80 diff --git a/src/dialogs/AddCookieDialog.cpp b/src/dialogs/AddCookieDialog.cpp new file mode 100644 index 0000000..a81d433 --- /dev/null +++ b/src/dialogs/AddCookieDialog.cpp @@ -0,0 +1,121 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of 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 . + */ + +// Application headers. +#include "AddCookieDialog.h" +#include "ui_AddCookieDialog.h" + +// KDE Framework headers. +#include + +// Qt toolkit header. +#include +#include + +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()); +}