File indexing completed on 2024-05-19 05:00:54
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 using namespace KonqInterfaces; 0021 0022 class DomainNameValidator : public QValidator 0023 { 0024 Q_OBJECT 0025 public: 0026 DomainNameValidator(QObject *parent) 0027 : QValidator(parent) 0028 { 0029 setObjectName(QStringLiteral("domainValidator")); 0030 } 0031 0032 State validate(QString &input, int &) const override 0033 { 0034 if (input.isEmpty() || (input == QLatin1Char('.'))) { 0035 return Intermediate; 0036 } 0037 0038 const int length = input.length(); 0039 0040 for (int i = 0; i < length; i++) { 0041 if (!input[i].isLetterOrNumber() && input[i] != QLatin1Char('.') && input[i] != QLatin1Char('-')) { 0042 return Invalid; 0043 } 0044 } 0045 0046 return Acceptable; 0047 } 0048 }; 0049 0050 KCookiesPolicySelectionDlg::KCookiesPolicySelectionDlg(QWidget *parent, Qt::WindowFlags flags) 0051 : QDialog(parent, flags) 0052 , mButtonBox(nullptr) 0053 { 0054 QWidget *mainWidget = new QWidget(this); 0055 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0056 mainLayout->addWidget(mainWidget); 0057 mUi.setupUi(mainWidget); 0058 mUi.leDomain->setValidator(new DomainNameValidator(mUi.leDomain)); 0059 mUi.cbPolicy->setMinimumWidth(mUi.cbPolicy->fontMetrics().maxWidth() * 15); 0060 0061 mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0062 mainLayout->addWidget(mButtonBox); 0063 0064 connect(mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0065 connect(mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0066 0067 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0068 connect(mUi.leDomain, &QLineEdit::textEdited, this, &KCookiesPolicySelectionDlg::slotTextChanged); 0069 connect(mUi.cbPolicy, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](const int index) { 0070 slotPolicyChanged(mUi.cbPolicy->itemText(index)); 0071 }); 0072 0073 mUi.leDomain->setFocus(); 0074 } 0075 0076 void KCookiesPolicySelectionDlg::setEnableHostEdit(bool state, const QString &host) 0077 { 0078 if (!host.isEmpty()) { 0079 mUi.leDomain->setText(host); 0080 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(state); 0081 } 0082 0083 mUi.leDomain->setEnabled(state); 0084 } 0085 0086 void KCookiesPolicySelectionDlg::setPolicy(KonqInterfaces::CookieJar::CookieAdvice policy) 0087 { 0088 const bool blocked = mUi.cbPolicy->blockSignals(true); 0089 mUi.cbPolicy->setCurrentIndex(CookieJar::adviceToInt(policy) - 1); 0090 mUi.cbPolicy->blockSignals(blocked); 0091 mOldPolicy = policy; 0092 0093 if (!mUi.leDomain->isEnabled()) { 0094 mUi.cbPolicy->setFocus(); 0095 } 0096 } 0097 0098 CookieJar::CookieAdvice KCookiesPolicySelectionDlg::advice() const 0099 { 0100 return CookieJar::intToAdvice(mUi.cbPolicy->currentIndex() + 1, CookieJar::CookieAdvice::Accept); 0101 } 0102 0103 QString KCookiesPolicySelectionDlg::domain() const 0104 { 0105 return mUi.leDomain->text(); 0106 } 0107 0108 void KCookiesPolicySelectionDlg::slotTextChanged(const QString &text) 0109 { 0110 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(text.length() > 1); 0111 } 0112 0113 void KCookiesPolicySelectionDlg::slotPolicyChanged(const QString &policyText) 0114 { 0115 CookieJar::CookieAdvice policy = KCookieAdvice::strToAdvice(policyText); 0116 if (!mUi.leDomain->isEnabled()) { 0117 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(policy != mOldPolicy); 0118 } else { 0119 slotTextChanged(policyText); 0120 } 0121 } 0122 0123 #include "kcookiespolicyselectiondlg.moc"