File indexing completed on 2024-12-22 04:43:19

0001 // /* This file is part of the KDE project
0002 //     SPDX-FileCopyrightText: 2023 Stefano Crocco <stefano.crocco@alice.it>
0003 // 
0004 //     SPDX-License-Identifier: LGPL-2.0-or-later
0005 // */
0006 
0007 #include "cookiealertdlg.h"
0008 #include "ui_cookiealertdlg.h"
0009 
0010 #include <QDateTime>
0011 #include <QPushButton>
0012 #include <QDialogButtonBox>
0013 
0014 using namespace KonqInterfaces;
0015 
0016 CookieAlertDlg::CookieAlertDlg(const QNetworkCookie &cookie, QWidget* parent) : QDialog(parent), m_ui(new Ui::CookieAlertDlg), m_cookie(cookie)
0017 {
0018     m_ui->setupUi(this);
0019     m_ui->header->setText(m_ui->header->text().arg(m_cookie.domain()));
0020     m_ui->name->setText(m_cookie.name());
0021     m_ui->value->setText(m_cookie.value());
0022     QString expirationDate = m_cookie.expirationDate().isValid() ? m_cookie.expirationDate().toString() : i18nc("@label the cookie expires when the browser session ends", "End of Session");
0023     m_ui->expires->setText(expirationDate);
0024     m_ui->path->setText(cookie.path());
0025     m_ui->domain->setText(cookie.domain());
0026     QString sec;
0027     if (cookie.isSecure()) {
0028         if (cookie.isHttpOnly()) {
0029             sec = i18nc("@label exposure string - the cookie may only be used by https servers", "Secure servers only");
0030         } else {
0031             sec = i18nc("@label exposure string - the cookie may be used by https servers AND client-side javascripts", "Secure servers, page scripts");
0032         }
0033     } else {
0034         if (cookie.isHttpOnly()) {
0035             sec = i18nc("@label exposure string - the cookie may only be used by http servers", "Servers");
0036         } else {
0037             sec = i18nc("@label exposure string - the cookie may be used by http servers AND client-side javascripts", "Servers, page scripts");
0038         }
0039     }
0040     m_ui->security->setText(sec);
0041     m_acceptBtn = new QPushButton(i18nc("@label accept cookie", "Accept"), this);
0042     m_acceptForSessionBtn = new QPushButton(i18nc("@label accept cookie for this session only", "Accept for this session"), this);
0043     m_ui->buttons->addButton(m_acceptBtn, QDialogButtonBox::AcceptRole);
0044     m_ui->buttons->addButton(m_acceptForSessionBtn, QDialogButtonBox::AcceptRole);
0045     m_ui->buttons->button(QDialogButtonBox::Cancel)->setText(i18nc("@label reject cookie", "Reject"));
0046 
0047     connect(m_ui->buttons, &QDialogButtonBox::clicked, this, &CookieAlertDlg::setChoice);
0048 }
0049 
0050 CookieAlertDlg::~CookieAlertDlg() noexcept
0051 {
0052 }
0053 
0054 void CookieAlertDlg::setChoice(QAbstractButton* btn)
0055 {
0056     if (btn == m_acceptBtn) {
0057         m_choice = CookieJar::CookieAdvice::Accept;
0058     } else if (btn == m_acceptForSessionBtn) {
0059         m_choice = CookieJar::CookieAdvice::AcceptForSession;
0060     } else {
0061         m_choice = CookieJar::CookieAdvice::Reject;
0062     }
0063 }
0064 
0065 CookieJar::CookieAdvice CookieAlertDlg::choice() const
0066 {
0067     return m_choice;
0068 }
0069 
0070 CookieAlertDlg::ApplyTo CookieAlertDlg::applyTo() const
0071 {
0072     if (m_ui->applyToAll->isChecked()) {
0073         return Cookies;
0074     } else if (m_ui->applyToDomain->isChecked()) {
0075         return Domain;
0076     } else {
0077         return This;
0078     }
0079 }
0080