add more spacing
[personal-kdebase.git] / apps / konqueror / settings / konqhtml / domainlistview.cpp
blob0ab51a3912c0e5a6dc184b9d0973b4575550ad48
1 /*
2 Copyright (c) 2002 Leo Savernik <l.savernik@aon.at>
3 Derived from jsopts.cpp and javaopts.cpp, code copied from there is
4 copyrighted to its respective owners.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 // Own
23 #include "domainlistview.h"
25 // Qt
26 #include <QtGui/QHBoxLayout>
27 #include <QtGui/QVBoxLayout>
28 #include <QtGui/QPushButton>
29 #include <QtGui/QTreeWidget>
31 // KDE
32 #include <kconfig.h>
33 #include <klocale.h>
34 #include <kmessagebox.h>
36 // Local
37 #include "policies.h"
38 #include "policydlg.h"
40 DomainListView::DomainListView(KSharedConfig::Ptr config,const QString &title,
41 QWidget *parent) :
42 QGroupBox(title, parent), config(config) {
43 QHBoxLayout* thisLayout = new QHBoxLayout(this);
44 thisLayout->setSpacing(KDialog::spacingHint());
45 thisLayout->setMargin(KDialog::marginHint());
47 domainSpecificLV = new QTreeWidget(this);
48 domainSpecificLV->setRootIsDecorated(false);
49 domainSpecificLV->setHeaderLabels(QStringList() << i18n("Host/Domain") << i18n("Policy"));
50 domainSpecificLV->setColumnWidth(0, 100);
51 connect(domainSpecificLV,SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), SLOT(changePressed()));
52 connect(domainSpecificLV, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(updateButton()));
53 thisLayout->addWidget(domainSpecificLV);
55 QVBoxLayout* btnsLayout = new QVBoxLayout;
56 thisLayout->addLayout(btnsLayout);
57 addDomainPB = new QPushButton(i18n("&New..."), this);
58 btnsLayout->addWidget(addDomainPB);
59 connect(addDomainPB, SIGNAL(clicked()), SLOT(addPressed()));
61 changeDomainPB = new QPushButton( i18n("Chan&ge..."), this);
62 btnsLayout->addWidget(changeDomainPB);
63 connect(changeDomainPB, SIGNAL(clicked()), this, SLOT(changePressed()));
65 deleteDomainPB = new QPushButton(i18n("De&lete"), this);
66 btnsLayout->addWidget(deleteDomainPB);
67 connect(deleteDomainPB, SIGNAL(clicked()), this, SLOT(deletePressed()));
69 importDomainPB = new QPushButton(i18n("&Import..."), this);
70 btnsLayout->addWidget(importDomainPB);
71 connect(importDomainPB, SIGNAL(clicked()), this, SLOT(importPressed()));
72 importDomainPB->setEnabled(false);
73 importDomainPB->hide();
75 exportDomainPB = new QPushButton(i18n("&Export..."), this);
76 btnsLayout->addWidget(exportDomainPB);
77 connect(exportDomainPB, SIGNAL(clicked()), this, SLOT(exportPressed()));
78 exportDomainPB->setEnabled(false);
79 exportDomainPB->hide();
81 btnsLayout->addStretch();
83 addDomainPB->setWhatsThis( i18n("Click on this button to manually add a host or domain "
84 "specific policy.") );
85 changeDomainPB->setWhatsThis( i18n("Click on this button to change the policy for the "
86 "host or domain selected in the list box.") );
87 deleteDomainPB->setWhatsThis( i18n("Click on this button to delete the policy for the "
88 "host or domain selected in the list box.") );
89 updateButton();
92 DomainListView::~DomainListView() {
93 // free all policies
94 DomainPolicyMap::Iterator it = domainPolicies.begin();
95 for (; it != domainPolicies.end(); ++it) {
96 delete it.value();
97 }/*next it*/
100 void DomainListView::updateButton()
102 QTreeWidgetItem *index = domainSpecificLV->currentItem();
103 bool enable = ( index != 0 );
104 changeDomainPB->setEnabled( enable );
105 deleteDomainPB->setEnabled( enable );
109 void DomainListView::addPressed()
111 // JavaPolicies pol_copy(m_pConfig,m_groupname,false);
112 Policies *pol = createPolicies();
113 pol->defaults();
114 PolicyDialog pDlg(pol, this);
115 setupPolicyDlg(AddButton,pDlg,pol);
116 if( pDlg.exec() ) {
117 QTreeWidgetItem* index = new QTreeWidgetItem( domainSpecificLV, QStringList() << pDlg.domain() <<
118 pDlg.featureEnabledPolicyText() );
119 pol->setDomain(pDlg.domain());
120 domainPolicies.insert(index, pol);
121 domainSpecificLV->setCurrentItem( index );
122 emit changed(true);
123 } else {
124 delete pol;
126 updateButton();
129 void DomainListView::changePressed()
131 QTreeWidgetItem *index = domainSpecificLV->currentItem();
132 if ( index == 0 )
134 KMessageBox::information( 0, i18n("You must first select a policy to be changed." ) );
135 return;
138 Policies *pol = domainPolicies[index];
139 // This must be copied because the policy dialog is allowed to change
140 // the data even if the changes are rejected in the end.
141 Policies *pol_copy = copyPolicies(pol);
143 PolicyDialog pDlg( pol_copy, this );
144 pDlg.setDisableEdit( true, index->text(0) );
145 setupPolicyDlg(ChangeButton,pDlg,pol_copy);
146 if( pDlg.exec() )
148 pol_copy->setDomain(pDlg.domain());
149 domainPolicies[index] = pol_copy;
150 pol_copy = pol;
151 index->setText(0, pDlg.domain() );
152 index->setText(1, pDlg.featureEnabledPolicyText());
153 emit changed(true);
155 delete pol_copy;
158 void DomainListView::deletePressed()
160 QTreeWidgetItem *index = domainSpecificLV->currentItem();
161 if ( index == 0 )
163 KMessageBox::information( 0, i18n("You must first select a policy to delete." ) );
164 return;
167 DomainPolicyMap::Iterator it = domainPolicies.find(index);
168 if (it != domainPolicies.end()) {
169 delete it.value();
170 domainPolicies.erase(it);
171 delete index;
172 emit changed(true);
174 updateButton();
177 void DomainListView::importPressed()
179 // PENDING(kalle) Implement this.
182 void DomainListView::exportPressed()
184 // PENDING(kalle) Implement this.
187 void DomainListView::initialize(const QStringList &domainList)
189 domainSpecificLV->clear();
190 domainPolicies.clear();
191 // JavaPolicies pol(m_pConfig,m_groupname,false);
192 for (QStringList::ConstIterator it = domainList.begin();
193 it != domainList.end(); ++it) {
194 QString domain = *it;
195 Policies *pol = createPolicies();
196 pol->setDomain(domain);
197 pol->load();
199 QString policy;
200 if (pol->isFeatureEnabledPolicyInherited())
201 policy = i18n("Use Global");
202 else if (pol->isFeatureEnabled())
203 policy = i18n("Accept");
204 else
205 policy = i18n("Reject");
206 QTreeWidgetItem *index =
207 new QTreeWidgetItem( domainSpecificLV, QStringList() << domain << policy );
209 domainPolicies[index] = pol;
213 void DomainListView::save(const QString &group, const QString &domainListKey) {
214 QStringList domainList;
215 DomainPolicyMap::Iterator it = domainPolicies.begin();
216 for (; it != domainPolicies.end(); ++it) {
217 QTreeWidgetItem *current = it.key();
218 Policies *pol = it.value();
219 pol->save();
220 domainList.append(current->text(0));
222 config->group(group).writeEntry(domainListKey, domainList);
225 void DomainListView::setupPolicyDlg(PushButton /*trigger*/,
226 PolicyDialog &/*pDlg*/,Policies * /*copy*/) {
227 // do nothing
230 #include "domainlistview.moc"