File indexing completed on 2024-05-12 15:41:57

0001 /*
0002     SPDX-FileCopyrightText: 2000 Dawit Alemayehu <adawit@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "kcookiespolicyselectiondlg.h"
0009 
0010 // Qt
0011 #include <QDialogButtonBox>
0012 #include <QPushButton>
0013 #include <QValidator>
0014 #include <QWhatsThis>
0015 
0016 // KDE
0017 #include <QLineEdit>
0018 #include <QVBoxLayout>
0019 
0020 class DomainNameValidator : public QValidator
0021 {
0022     Q_OBJECT
0023 public:
0024     DomainNameValidator(QObject *parent)
0025         : QValidator(parent)
0026     {
0027         setObjectName(QStringLiteral("domainValidator"));
0028     }
0029 
0030     State validate(QString &input, int &) const override
0031     {
0032         if (input.isEmpty() || (input == QLatin1Char('.'))) {
0033             return Intermediate;
0034         }
0035 
0036         const int length = input.length();
0037 
0038         for (int i = 0; i < length; i++) {
0039             if (!input[i].isLetterOrNumber() && input[i] != QLatin1Char('.') && input[i] != QLatin1Char('-')) {
0040                 return Invalid;
0041             }
0042         }
0043 
0044         return Acceptable;
0045     }
0046 };
0047 
0048 KCookiesPolicySelectionDlg::KCookiesPolicySelectionDlg(QWidget *parent, Qt::WindowFlags flags)
0049     : QDialog(parent, flags)
0050     , mOldPolicy(-1)
0051     , mButtonBox(nullptr)
0052 {
0053     QWidget *mainWidget = new QWidget(this);
0054     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0055     mainLayout->addWidget(mainWidget);
0056     mUi.setupUi(mainWidget);
0057     mUi.leDomain->setValidator(new DomainNameValidator(mUi.leDomain));
0058     mUi.cbPolicy->setMinimumWidth(mUi.cbPolicy->fontMetrics().maxWidth() * 15);
0059 
0060     mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0061     mainLayout->addWidget(mButtonBox);
0062 
0063     connect(mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0064     connect(mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0065 
0066     mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0067     connect(mUi.leDomain, &QLineEdit::textEdited, this, &KCookiesPolicySelectionDlg::slotTextChanged);
0068     connect(mUi.cbPolicy, qOverload<int>(&QComboBox::currentIndexChanged), this, [this](const int index) {
0069         slotPolicyChanged(mUi.cbPolicy->itemText(index));
0070     });
0071 
0072     mUi.leDomain->setFocus();
0073 }
0074 
0075 void KCookiesPolicySelectionDlg::setEnableHostEdit(bool state, const QString &host)
0076 {
0077     if (!host.isEmpty()) {
0078         mUi.leDomain->setText(host);
0079         mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
0080     }
0081 
0082     mUi.leDomain->setEnabled(state);
0083 }
0084 
0085 void KCookiesPolicySelectionDlg::setPolicy(int policy)
0086 {
0087     if (policy > -1 && policy <= static_cast<int>(mUi.cbPolicy->count())) {
0088         const bool blocked = mUi.cbPolicy->blockSignals(true);
0089         mUi.cbPolicy->setCurrentIndex(policy - 1);
0090         mUi.cbPolicy->blockSignals(blocked);
0091         mOldPolicy = policy;
0092     }
0093 
0094     if (!mUi.leDomain->isEnabled()) {
0095         mUi.cbPolicy->setFocus();
0096     }
0097 }
0098 
0099 int KCookiesPolicySelectionDlg::advice() const
0100 {
0101     return mUi.cbPolicy->currentIndex() + 1;
0102 }
0103 
0104 QString KCookiesPolicySelectionDlg::domain() const
0105 {
0106     return mUi.leDomain->text();
0107 }
0108 
0109 void KCookiesPolicySelectionDlg::slotTextChanged(const QString &text)
0110 {
0111     mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(text.length() > 1);
0112 }
0113 
0114 void KCookiesPolicySelectionDlg::slotPolicyChanged(const QString &policyText)
0115 {
0116     const int policy = KCookieAdvice::strToAdvice(policyText);
0117     if (!mUi.leDomain->isEnabled()) {
0118         mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(policy != mOldPolicy);
0119     } else {
0120         slotTextChanged(policyText);
0121     }
0122 }
0123 
0124 #include "kcookiespolicyselectiondlg.moc"
0125 #include "moc_kcookiespolicyselectiondlg.cpp"