File indexing completed on 2024-05-05 05:00:09

0001 /*
0002     SPDX-FileCopyrightText: 2002 to whoever created and edited this file before
0003     SPDX-FileCopyrightText: 2002 Leo Savernik <l.savernik@aon.at>
0004     Generalizing the policy dialog
0005 
0006 */
0007 
0008 // Own
0009 #include "policydlg.h"
0010 
0011 // Qt
0012 #include <QLabel>
0013 #include <QComboBox>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 #include <QDialogButtonBox>
0017 // KDE
0018 #include <KLocalizedString>
0019 #include <kmessagebox.h>
0020 
0021 // Local
0022 #include "policies.h"
0023 
0024 PolicyDialog::PolicyDialog(Policies *policies, QWidget *parent, const char *name)
0025     : QDialog(parent),
0026       policies(policies)
0027 {
0028     setObjectName(name);
0029     setModal(true);
0030     setWindowTitle(i18nc("@title:window", "Domain-Specific Policies"));
0031 
0032     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this);
0033     connect(buttonBox, &QDialogButtonBox::accepted, this, &PolicyDialog::accept);
0034     connect(buttonBox, &QDialogButtonBox::rejected, this, &PolicyDialog::reject);
0035     okButton = buttonBox->button(QDialogButtonBox::Ok);
0036 
0037     QFrame *main = new QFrame(this);
0038 
0039     insertIdx = 1;    // index where to insert additional panels
0040     topl = new QVBoxLayout(main);
0041     topl->setContentsMargins(0, 0, 0, 0);
0042 
0043     QGridLayout *grid = new QGridLayout();
0044     topl->addLayout(grid);
0045     grid->setColumnStretch(1, 1);
0046 
0047     QLabel *l = new QLabel(i18n("&Host or domain name:"), main);
0048     grid->addWidget(l, 0, 0);
0049 
0050     le_domain = new QLineEdit(main);
0051     l->setBuddy(le_domain);
0052     grid->addWidget(le_domain, 0, 1);
0053     connect(le_domain, &QLineEdit::textChanged, this, &PolicyDialog::slotTextChanged);
0054 
0055     le_domain->setToolTip(i18n("Enter the name of a host (like www.kde.org) "
0056                                  "or a domain, starting with a dot (like .kde.org or .org)"));
0057 
0058     l_feature_policy = new QLabel(main);
0059     grid->addWidget(l_feature_policy, 1, 0);
0060 
0061     cb_feature_policy = new QComboBox(main);
0062     l_feature_policy->setBuddy(cb_feature_policy);
0063     policy_values << i18n("Use Global") << i18n("Accept") << i18n("Reject");
0064     cb_feature_policy->addItems(policy_values);
0065     grid->addWidget(cb_feature_policy, 1, 1);
0066 
0067     QVBoxLayout *vLayout = new QVBoxLayout(this);
0068     vLayout->addWidget(main);
0069     vLayout->addStretch(1);
0070     vLayout->addWidget(buttonBox);
0071 
0072     le_domain->setFocus();
0073     okButton->setEnabled(!le_domain->text().isEmpty());
0074 }
0075 
0076 PolicyDialog::FeatureEnabledPolicy PolicyDialog::featureEnabledPolicy() const
0077 {
0078     return (FeatureEnabledPolicy)cb_feature_policy->currentIndex();
0079 }
0080 
0081 void PolicyDialog::slotTextChanged(const QString &text)
0082 {
0083     okButton->setEnabled(!text.isEmpty());
0084 }
0085 
0086 void PolicyDialog::setDisableEdit(bool state, const QString &text)
0087 {
0088     le_domain->setText(text);
0089 
0090     le_domain->setEnabled(state);
0091 
0092     if (state) {
0093         cb_feature_policy->setFocus();
0094     }
0095 }
0096 
0097 void PolicyDialog::refresh()
0098 {
0099     FeatureEnabledPolicy pol;
0100 
0101     if (policies->isFeatureEnabledPolicyInherited()) {
0102         pol = InheritGlobal;
0103     } else if (policies->isFeatureEnabled()) {
0104         pol = Accept;
0105     } else {
0106         pol = Reject;
0107     }
0108     cb_feature_policy->setCurrentIndex(pol);
0109 }
0110 
0111 void PolicyDialog::setFeatureEnabledLabel(const QString &text)
0112 {
0113     l_feature_policy->setText(text);
0114 }
0115 
0116 void PolicyDialog::setFeatureEnabledWhatsThis(const QString &text)
0117 {
0118     cb_feature_policy->setToolTip(text);
0119 }
0120 
0121 void PolicyDialog::addPolicyPanel(QWidget *panel)
0122 {
0123     topl->insertWidget(insertIdx++, panel);
0124 }
0125 
0126 QString PolicyDialog::featureEnabledPolicyText() const
0127 {
0128     int pol = cb_feature_policy->currentIndex();
0129     if (pol >= 0 && pol < 3) { // Keep in sync with FeatureEnabledPolicy
0130         return policy_values[pol];
0131     } else {
0132         return QString();
0133     }
0134 }
0135 
0136 void PolicyDialog::accept()
0137 {
0138     if (le_domain->text().isEmpty()) {
0139         KMessageBox::information(nullptr, i18n("You must first enter a domain name."));
0140         return;
0141     }
0142 
0143     FeatureEnabledPolicy pol = (FeatureEnabledPolicy)
0144                                cb_feature_policy->currentIndex();
0145     if (pol == InheritGlobal) {
0146         policies->inheritFeatureEnabledPolicy();
0147     } else if (pol == Reject) {
0148         policies->setFeatureEnabled(false);
0149     } else {
0150         policies->setFeatureEnabled(true);
0151     }
0152     QDialog::accept();
0153 }
0154