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

0001 /*
0002     SPDX-FileCopyrightText: 2002 Leo Savernik <l.savernik@aon.at>
0003     Derived from jsopts.cpp and javaopts.cpp, code copied from there is
0004     copyrighted to its respective owners.
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "domainlistview.h"
0011 
0012 // Qt
0013 #include <QHBoxLayout>
0014 #include <QVBoxLayout>
0015 #include <QPushButton>
0016 #include <QTreeWidget>
0017 
0018 // KDE
0019 #include <KLocalizedString>
0020 #include <kmessagebox.h>
0021 #include <KSharedConfig>
0022 #include <kconfiggroup.h>
0023 
0024 // Local
0025 #include "policies.h"
0026 #include "policydlg.h"
0027 
0028 DomainListView::DomainListView(KSharedConfig::Ptr config, const QString &title,
0029                                QWidget *parent) :
0030     QGroupBox(title, parent), config(config)
0031 {
0032     QHBoxLayout *thisLayout = new QHBoxLayout(this);
0033 
0034     domainSpecificLV = new QTreeWidget(this);
0035     domainSpecificLV->setRootIsDecorated(false);
0036     domainSpecificLV->setSortingEnabled(true);
0037     domainSpecificLV->setHeaderLabels(QStringList() << i18n("Host/Domain") << i18n("Policy"));
0038     domainSpecificLV->setColumnWidth(0, 100);
0039     connect(domainSpecificLV, &QTreeWidget::itemDoubleClicked, this, &DomainListView::changePressed);
0040     connect(domainSpecificLV, &QTreeWidget::currentItemChanged, this, &DomainListView::updateButton);
0041     thisLayout->addWidget(domainSpecificLV);
0042 
0043     QVBoxLayout *btnsLayout = new QVBoxLayout;
0044     thisLayout->addLayout(btnsLayout);
0045     addDomainPB = new QPushButton(i18n("&New..."), this);
0046     btnsLayout->addWidget(addDomainPB);
0047     connect(addDomainPB, &QAbstractButton::clicked, this, &DomainListView::addPressed);
0048 
0049     changeDomainPB = new QPushButton(i18n("Chan&ge..."), this);
0050     btnsLayout->addWidget(changeDomainPB);
0051     connect(changeDomainPB, &QAbstractButton::clicked, this, &DomainListView::changePressed);
0052 
0053     deleteDomainPB = new QPushButton(i18n("De&lete"), this);
0054     btnsLayout->addWidget(deleteDomainPB);
0055     connect(deleteDomainPB, &QAbstractButton::clicked, this, &DomainListView::deletePressed);
0056 
0057     importDomainPB = new QPushButton(i18n("&Import..."), this);
0058     btnsLayout->addWidget(importDomainPB);
0059     connect(importDomainPB, &QAbstractButton::clicked, this, &DomainListView::importPressed);
0060     importDomainPB->setEnabled(false);
0061     importDomainPB->hide();
0062 
0063     exportDomainPB = new QPushButton(i18n("&Export..."), this);
0064     btnsLayout->addWidget(exportDomainPB);
0065     connect(exportDomainPB, &QAbstractButton::clicked, this, &DomainListView::exportPressed);
0066     exportDomainPB->setEnabled(false);
0067     exportDomainPB->hide();
0068 
0069     btnsLayout->addStretch();
0070 
0071     addDomainPB->setToolTip(i18n("Click on this button to manually add a host or domain "
0072                                    "specific policy."));
0073     changeDomainPB->setToolTip(i18n("Click on this button to change the policy for the "
0074                                       "host or domain selected in the list box."));
0075     deleteDomainPB->setToolTip(i18n("Click on this button to delete the policy for the "
0076                                       "host or domain selected in the list box."));
0077     updateButton();
0078 }
0079 
0080 DomainListView::~DomainListView()
0081 {
0082     // free all policies
0083     DomainPolicyMap::Iterator it = domainPolicies.begin();
0084     for (; it != domainPolicies.end(); ++it) {
0085         delete it.value();
0086     }/*next it*/
0087 }
0088 
0089 void DomainListView::updateButton()
0090 {
0091     QTreeWidgetItem *index = domainSpecificLV->currentItem();
0092     bool enable = (index != nullptr);
0093     changeDomainPB->setEnabled(enable);
0094     deleteDomainPB->setEnabled(enable);
0095 
0096 }
0097 
0098 void DomainListView::addPressed()
0099 {
0100 //    JavaPolicies pol_copy(m_pConfig,m_groupname,false);
0101     Policies *pol = createPolicies();
0102     pol->defaults();
0103     PolicyDialog pDlg(pol, this);
0104     setupPolicyDlg(AddButton, pDlg, pol);
0105     if (pDlg.exec()) {
0106         QTreeWidgetItem *index = new QTreeWidgetItem(domainSpecificLV, QStringList() << pDlg.domain() <<
0107                 pDlg.featureEnabledPolicyText());
0108         pol->setDomain(pDlg.domain());
0109         domainPolicies.insert(index, pol);
0110         domainSpecificLV->setCurrentItem(index);
0111         emit changed(true);
0112     } else {
0113         delete pol;
0114     }
0115     updateButton();
0116 }
0117 
0118 void DomainListView::changePressed()
0119 {
0120     QTreeWidgetItem *index = domainSpecificLV->currentItem();
0121     if (index == nullptr) {
0122         KMessageBox::information(nullptr, i18n("You must first select a policy to be changed."));
0123         return;
0124     }
0125 
0126     Policies *pol = domainPolicies[index];
0127     // This must be copied because the policy dialog is allowed to change
0128     // the data even if the changes are rejected in the end.
0129     Policies *pol_copy = copyPolicies(pol);
0130 
0131     PolicyDialog pDlg(pol_copy, this);
0132     pDlg.setDisableEdit(true, index->text(0));
0133     setupPolicyDlg(ChangeButton, pDlg, pol_copy);
0134     if (pDlg.exec()) {
0135         pol_copy->setDomain(pDlg.domain());
0136         domainPolicies[index] = pol_copy;
0137         pol_copy = pol;
0138         index->setText(0, pDlg.domain());
0139         index->setText(1, pDlg.featureEnabledPolicyText());
0140         emit changed(true);
0141     }
0142     delete pol_copy;
0143 }
0144 
0145 void DomainListView::deletePressed()
0146 {
0147     QTreeWidgetItem *index = domainSpecificLV->currentItem();
0148     if (index == nullptr) {
0149         KMessageBox::information(nullptr, i18n("You must first select a policy to delete."));
0150         return;
0151     }
0152 
0153     DomainPolicyMap::Iterator it = domainPolicies.find(index);
0154     if (it != domainPolicies.end()) {
0155         delete it.value();
0156         domainPolicies.erase(it);
0157         delete index;
0158         emit changed(true);
0159     }
0160     updateButton();
0161 }
0162 
0163 void DomainListView::importPressed()
0164 {
0165     // PENDING(kalle) Implement this.
0166 }
0167 
0168 void DomainListView::exportPressed()
0169 {
0170     // PENDING(kalle) Implement this.
0171 }
0172 
0173 void DomainListView::initialize(const QStringList &domainList)
0174 {
0175     domainSpecificLV->clear();
0176     domainPolicies.clear();
0177 //    JavaPolicies pol(m_pConfig,m_groupname,false);
0178     for (QStringList::ConstIterator it = domainList.begin();
0179             it != domainList.end(); ++it) {
0180         QString domain = *it;
0181         Policies *pol = createPolicies();
0182         pol->setDomain(domain);
0183         pol->load();
0184 
0185         QString policy;
0186         if (pol->isFeatureEnabledPolicyInherited()) {
0187             policy = i18n("Use Global");
0188         } else if (pol->isFeatureEnabled()) {
0189             policy = i18n("Accept");
0190         } else {
0191             policy = i18n("Reject");
0192         }
0193         QTreeWidgetItem *index =
0194             new QTreeWidgetItem(domainSpecificLV, QStringList() << domain << policy);
0195 
0196         domainPolicies[index] = pol;
0197     }
0198 }
0199 
0200 void DomainListView::save(const QString &group, const QString &domainListKey)
0201 {
0202     QStringList domainList;
0203     DomainPolicyMap::Iterator it = domainPolicies.begin();
0204     for (; it != domainPolicies.end(); ++it) {
0205         QTreeWidgetItem *current = it.key();
0206         Policies *pol = it.value();
0207         pol->save();
0208         domainList.append(current->text(0));
0209     }
0210     config->group(group).writeEntry(domainListKey, domainList);
0211 }
0212 
0213 void DomainListView::setupPolicyDlg(PushButton /*trigger*/,
0214                                     PolicyDialog &/*pDlg*/, Policies * /*copy*/)
0215 {
0216     // do nothing
0217 }
0218