add more spacing
[personal-kdebase.git] / apps / konqueror / settings / kio / kcookiespolicies.cpp
blob98b07b68980ed5f7e1ab10fc10a03e8c9e1efccd
1 /**
2 * kcookiespolicies.cpp - Cookies configuration
4 * Original Authors
5 * Copyright (c) Waldo Bastian <bastian@kde.org>
6 * Copyright (c) 1999 David Faure <faure@kde.org>
7 * Copyright (c) 2008 Urs Wolfer <uwolfer @ kde.org>
9 * Re-written by:
10 * Copyright (c) 2000- Dawit Alemayehu <adawit@kde.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 // Own
28 #include "kcookiespolicies.h"
30 // Qt
31 #include <QtGui/QLayout>
32 #include <QtGui/QCheckBox>
33 #include <QtGui/QPushButton>
34 #include <QtGui/QRadioButton>
35 #include <QtGui/QToolButton>
36 #include <QtGui/QBoxLayout>
37 #include <QtDBus/QtDBus>
39 // KDE
40 #include <kiconloader.h>
41 #include <kmessagebox.h>
42 #include <klocale.h>
43 #include <kconfig.h>
44 #include <kurl.h>
45 #include <kdebug.h>
47 #include "ksaveioconfig.h"
50 KCookiesPolicies::KCookiesPolicies(const KComponentData &componentData, QWidget *parent)
51 :KCModule(componentData, parent)
53 QVBoxLayout *mainLayout = new QVBoxLayout(this);
54 mainLayout->setMargin(0);
55 mainLayout->setSpacing(0);
57 dlg = new KCookiesPolicyDlgUI (this);
58 dlg->kListViewSearchLine->setTreeWidget(dlg->lvDomainPolicy);
59 QList<int> columns;
60 columns.append(0);
61 dlg->kListViewSearchLine->setSearchColumns(columns);
63 dlg->pbNew->setIcon(KIcon("list-add"));
64 dlg->pbChange->setIcon(KIcon("edit-rename"));
65 dlg->pbDelete->setIcon(KIcon("list-remove"));
66 dlg->pbDeleteAll->setIcon(KIcon("edit-delete"));
68 mainLayout->addWidget(dlg);
70 // Connect the main swicth :) Enable/disable cookie support
71 connect( dlg->cbEnableCookies, SIGNAL( toggled(bool) ),
72 SLOT( cookiesEnabled(bool) ) );
73 connect( dlg->cbEnableCookies, SIGNAL( toggled(bool) ),
74 SLOT( configChanged() ) );
76 // Connect the preference check boxes...
77 connect ( dlg->cbRejectCrossDomainCookies, SIGNAL(toggled(bool)),
78 SLOT(configChanged()));
79 connect ( dlg->cbAutoAcceptSessionCookies, SIGNAL(toggled(bool)),
80 SLOT(configChanged()));
81 connect ( dlg->cbIgnoreCookieExpirationDate, SIGNAL(toggled(bool)),
82 SLOT(configChanged()));
84 connect ( dlg->cbAutoAcceptSessionCookies, SIGNAL(toggled(bool)),
85 SLOT(autoAcceptSessionCookies(bool)));
86 connect ( dlg->cbIgnoreCookieExpirationDate, SIGNAL(toggled(bool)),
87 SLOT(ignoreCookieExpirationDate(bool)));
89 connect ( dlg->rbPolicyAsk, SIGNAL(toggled(bool)),
90 SLOT(configChanged()));
91 connect ( dlg->rbPolicyAccept, SIGNAL(toggled(bool)),
92 SLOT(configChanged()));
93 connect ( dlg->rbPolicyReject, SIGNAL(toggled(bool)),
94 SLOT(configChanged()));
95 // Connect signals from the domain specific policy listview.
96 connect( dlg->lvDomainPolicy, SIGNAL(itemSelectionChanged()),
97 SLOT(selectionChanged()) );
98 connect( dlg->lvDomainPolicy, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
99 SLOT(changePressed() ) );
101 // Connect the buttons...
102 connect( dlg->pbNew, SIGNAL(clicked()), SLOT( addPressed() ) );
103 connect( dlg->pbChange, SIGNAL( clicked() ), SLOT( changePressed() ) );
104 connect( dlg->pbDelete, SIGNAL( clicked() ), SLOT( deletePressed() ) );
105 connect( dlg->pbDeleteAll, SIGNAL( clicked() ), SLOT( deleteAllPressed() ) );
108 KCookiesPolicies::~KCookiesPolicies()
112 void KCookiesPolicies::configChanged ()
114 //kDebug() << "KCookiesPolicies::configChanged...";
115 emit changed(true);
118 void KCookiesPolicies::cookiesEnabled( bool enable )
120 dlg->bgDefault->setEnabled( enable );
121 dlg->bgPreferences->setEnabled ( enable );
122 dlg->gbDomainSpecific->setEnabled( enable );
124 if (enable)
126 ignoreCookieExpirationDate ( enable );
127 autoAcceptSessionCookies ( enable );
131 void KCookiesPolicies::ignoreCookieExpirationDate ( bool enable )
133 bool isAutoAcceptChecked = dlg->cbAutoAcceptSessionCookies->isChecked();
134 enable = (enable && isAutoAcceptChecked);
136 dlg->bgDefault->setEnabled( !enable );
137 dlg->gbDomainSpecific->setEnabled( !enable );
140 void KCookiesPolicies::autoAcceptSessionCookies ( bool enable )
142 bool isIgnoreExpirationChecked = dlg->cbIgnoreCookieExpirationDate->isChecked();
143 enable = (enable && isIgnoreExpirationChecked);
145 dlg->bgDefault->setEnabled( !enable );
146 dlg->gbDomainSpecific->setEnabled( !enable );
149 void KCookiesPolicies::addNewPolicy(const QString& domain)
151 PolicyDlg pdlg (i18n("New Cookie Policy"), this);
152 pdlg.setEnableHostEdit (true, domain);
154 if (dlg->rbPolicyAccept->isChecked())
155 pdlg.setPolicy(KCookieAdvice::Reject);
156 else
157 pdlg.setPolicy(KCookieAdvice::Accept);
159 if (pdlg.exec() && !pdlg.domain().isEmpty())
161 QString domain = KUrl::fromAce(pdlg.domain().toLatin1());
162 int advice = pdlg.advice();
164 if ( !handleDuplicate(domain, advice) )
166 const char* strAdvice = KCookieAdvice::adviceToStr(advice);
167 QTreeWidgetItem* index = new QTreeWidgetItem (dlg->lvDomainPolicy,
168 QStringList()<< domain << i18n(strAdvice));
169 m_pDomainPolicy.insert (index, strAdvice);
170 configChanged();
176 void KCookiesPolicies::addPressed()
178 addNewPolicy (QString());
181 void KCookiesPolicies::changePressed()
183 QTreeWidgetItem* index = dlg->lvDomainPolicy->currentItem();
185 if (!index)
186 return;
188 QString oldDomain = index->text(0);
190 PolicyDlg pdlg (i18n("Change Cookie Policy"), this);
191 pdlg.setPolicy (KCookieAdvice::strToAdvice(m_pDomainPolicy[index]));
192 pdlg.setEnableHostEdit (true, oldDomain);
194 if( pdlg.exec() && !pdlg.domain().isEmpty())
196 QString newDomain = KUrl::fromAce(pdlg.domain().toLatin1());
197 int advice = pdlg.advice();
198 if (newDomain == oldDomain || !handleDuplicate(newDomain, advice))
200 m_pDomainPolicy[index] = KCookieAdvice::adviceToStr(advice);
201 index->setText(0, newDomain);
202 index->setText(1, i18n(m_pDomainPolicy[index]) );
203 configChanged();
208 bool KCookiesPolicies::handleDuplicate( const QString& domain, int advice )
210 QTreeWidgetItem* item = dlg->lvDomainPolicy->topLevelItem(0);
211 while ( item != 0 )
213 if ( item->text(0) == domain )
215 QString msg = i18n("<qt>A policy already exists for"
216 "<center><b>%1</b></center>"
217 "Do you want to replace it?</qt>", domain);
218 int res = KMessageBox::warningContinueCancel(this, msg,
219 i18n("Duplicate Policy"),
220 KGuiItem(i18n("Replace")));
221 if ( res == KMessageBox::Continue )
223 m_pDomainPolicy[item]= KCookieAdvice::adviceToStr(advice);
224 item->setText(0, domain);
225 item->setText(1, i18n(m_pDomainPolicy[item]));
226 configChanged();
227 return true;
229 else
230 return true; // User Cancelled!!
232 item = dlg->lvDomainPolicy->itemBelow(item);
234 return false;
237 void KCookiesPolicies::deletePressed()
239 QTreeWidgetItem* nextItem = 0L;
241 Q_FOREACH(QTreeWidgetItem* item, dlg->lvDomainPolicy->selectedItems()) {
242 nextItem = dlg->lvDomainPolicy->itemBelow(item);
243 if (!nextItem)
244 nextItem = dlg->lvDomainPolicy->itemAbove(item);
246 delete item;
249 if (nextItem)
250 nextItem->setSelected(true);
252 updateButtons();
253 configChanged();
256 void KCookiesPolicies::deleteAllPressed()
258 m_pDomainPolicy.clear();
259 dlg->lvDomainPolicy->clear();
260 updateButtons();
261 configChanged();
264 void KCookiesPolicies::updateButtons()
266 bool hasItems = dlg->lvDomainPolicy->topLevelItemCount() > 0;
268 dlg->pbChange->setEnabled ((hasItems && d_itemsSelected == 1));
269 dlg->pbDelete->setEnabled ((hasItems && d_itemsSelected > 0));
270 dlg->pbDeleteAll->setEnabled ( hasItems );
273 void KCookiesPolicies::updateDomainList(const QStringList &domainConfig)
275 dlg->lvDomainPolicy->clear();
277 QStringList::ConstIterator it = domainConfig.begin();
278 for (; it != domainConfig.end(); ++it)
280 QString domain;
281 KCookieAdvice::Value advice = KCookieAdvice::Dunno;
283 splitDomainAdvice(*it, domain, advice);
285 if (!domain.isEmpty())
287 QTreeWidgetItem* index = new QTreeWidgetItem( dlg->lvDomainPolicy, QStringList() << KUrl::fromAce(domain.toLatin1()) <<
288 i18n(KCookieAdvice::adviceToStr(advice)) );
289 m_pDomainPolicy[index] = KCookieAdvice::adviceToStr(advice);
294 void KCookiesPolicies::selectionChanged ()
296 d_itemsSelected = dlg->lvDomainPolicy->selectedItems().count();
298 updateButtons ();
301 void KCookiesPolicies::load()
303 d_itemsSelected = 0;
305 KConfig cfg ("kcookiejarrc");
306 KConfigGroup group = cfg.group ("Cookie Policy");
308 bool enableCookies = group.readEntry("Cookies", true);
309 dlg->cbEnableCookies->setChecked (enableCookies);
310 cookiesEnabled( enableCookies );
312 KCookieAdvice::Value advice = KCookieAdvice::strToAdvice (group.readEntry(
313 "CookieGlobalAdvice", "Ask"));
314 switch (advice)
316 case KCookieAdvice::Accept:
317 dlg->rbPolicyAccept->setChecked (true);
318 break;
319 case KCookieAdvice::Reject:
320 dlg->rbPolicyReject->setChecked (true);
321 break;
322 case KCookieAdvice::Ask:
323 case KCookieAdvice::Dunno:
324 default:
325 dlg->rbPolicyAsk->setChecked (true);
328 bool enable = group.readEntry("RejectCrossDomainCookies", true);
329 dlg->cbRejectCrossDomainCookies->setChecked (enable);
331 bool sessionCookies = group.readEntry("AcceptSessionCookies", true);
332 dlg->cbAutoAcceptSessionCookies->setChecked (sessionCookies);
333 bool cookieExpiration = group.readEntry("IgnoreExpirationDate", false);
334 dlg->cbIgnoreCookieExpirationDate->setChecked (cookieExpiration);
335 updateDomainList(group.readEntry("CookieDomainAdvice", QStringList()));
337 if (enableCookies)
339 ignoreCookieExpirationDate( cookieExpiration );
340 autoAcceptSessionCookies( sessionCookies );
341 updateButtons();
345 void KCookiesPolicies::save()
347 KConfig cfg ( "kcookiejarrc" );
348 KConfigGroup group = cfg.group( "Cookie Policy" );
350 bool state = dlg->cbEnableCookies->isChecked();
351 group.writeEntry( "Cookies", state );
352 state = dlg->cbRejectCrossDomainCookies->isChecked();
353 group.writeEntry( "RejectCrossDomainCookies", state );
354 state = dlg->cbAutoAcceptSessionCookies->isChecked();
355 group.writeEntry( "AcceptSessionCookies", state );
356 state = dlg->cbIgnoreCookieExpirationDate->isChecked();
357 group.writeEntry( "IgnoreExpirationDate", state );
359 QString advice;
360 if (dlg->rbPolicyAccept->isChecked())
361 advice = KCookieAdvice::adviceToStr(KCookieAdvice::Accept);
362 else if (dlg->rbPolicyReject->isChecked())
363 advice = KCookieAdvice::adviceToStr(KCookieAdvice::Reject);
364 else
365 advice = KCookieAdvice::adviceToStr(KCookieAdvice::Ask);
367 group.writeEntry("CookieGlobalAdvice", advice);
369 QStringList domainConfig;
370 QTreeWidgetItem *at = dlg->lvDomainPolicy->topLevelItem(0);
372 while( at )
374 domainConfig.append(QString::fromLatin1("%1:%2").arg(QString(KUrl::toAce(at->text(0)))).arg(m_pDomainPolicy[at]));
375 at = dlg->lvDomainPolicy->itemBelow(at);
378 group.writeEntry("CookieDomainAdvice", domainConfig);
379 group.sync();
381 // Update the cookiejar...
382 if (!dlg->cbEnableCookies->isChecked())
384 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
385 kded.call( "shutdown" );
387 else
389 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
390 QDBusReply<void> reply = kded.call( "reloadPolicy" );
391 if (!reply.isValid())
392 KMessageBox::sorry(0, i18n("Unable to communicate with the cookie handler service.\n"
393 "Any changes you made will not take effect until the service "
394 "is restarted."));
397 // Force running io-slave to reload configurations...
398 KSaveIOConfig::updateRunningIOSlaves (this);
399 emit changed( false );
403 void KCookiesPolicies::defaults()
405 dlg->cbEnableCookies->setChecked( true );
406 dlg->rbPolicyAsk->setChecked( true );
407 dlg->rbPolicyAccept->setChecked( false );
408 dlg->rbPolicyReject->setChecked( false );
409 dlg->cbRejectCrossDomainCookies->setChecked( true );
410 dlg->cbAutoAcceptSessionCookies->setChecked( true );
411 dlg->cbIgnoreCookieExpirationDate->setChecked( false );
412 dlg->lvDomainPolicy->clear();
414 cookiesEnabled( dlg->cbEnableCookies->isChecked() );
415 updateButtons();
418 void KCookiesPolicies::splitDomainAdvice (const QString& cfg, QString &domain,
419 KCookieAdvice::Value &advice)
421 int sepPos = cfg.lastIndexOf(':');
423 // Ignore any policy that does not contain a domain...
424 if ( sepPos <= 0 )
425 return;
427 domain = cfg.left(sepPos);
428 advice = KCookieAdvice::strToAdvice( cfg.mid( sepPos+1 ) );
431 QString KCookiesPolicies::quickHelp() const
433 return i18n("<p><h1>Cookies</h1> Cookies contain information that Konqueror"
434 " (or any other KDE application using the HTTP protocol) stores"
435 " on your computer from a remote Internet server. This means"
436 " that a web server can store information about you and your"
437 " browsing activities on your machine for later use. You might"
438 " consider this an invasion of privacy.</p><p>However, cookies are"
439 " useful in certain situations. For example, they are often used"
440 " by Internet shops, so you can 'put things into a shopping"
441 " basket'. Some sites require you have a browser that supports"
442 " cookies.</p><p>Because most people want a compromise between privacy"
443 " and the benefits cookies offer, KDE offers you the ability to"
444 " customize the way it handles cookies. You might, for example"
445 " want to set KDE's default policy to ask you whenever a server"
446 " wants to set a cookie or simply reject or accept everything."
447 " For example, you might choose to accept all cookies from your"
448 " favorite shopping web site. For this all you have to do is"
449 " either browse to that particular site and when you are presented"
450 " with the cookie dialog box, click on <i> This domain </i> under"
451 " the 'apply to' tab and choose accept or simply specify the name"
452 " of the site in the <i> Domain Specific Policy </i> tab and set"
453 " it to accept. This enables you to receive cookies from trusted"
454 " web sites without being asked every time KDE receives a cookie.</p>"
458 #include "kcookiespolicies.moc"