Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / kio / kcookiesmanagement.cpp
blob9aeffc9b747ca4c282b49d27231b44b4cf73d99f
1 /**
2 * kcookiesmanagement.cpp - Cookies manager
4 * Copyright 2000-2001 Marco Pinelli <pinmc@orion.it>
5 * Copyright (c) 2000-2001 Dawit Alemayehu <adawit@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 // Own
23 #include "kcookiesmanagement.h"
25 // std
26 #include <assert.h>
28 // Qt
29 #include <QtGui/QApplication>
30 #include <QtGui/QLayout>
31 #include <QtGui/QPushButton>
32 #include <QtGui/QLabel>
33 #include <QtCore/QTimer>
34 #include <QtCore/QDate>
35 #include <QtGui/QToolButton>
36 #include <QtGui/QBoxLayout>
37 #include <QtCore/QList>
39 #include <QtDBus/QtDBus>
41 // KDE
42 #include <kdebug.h>
43 #include <klocale.h>
44 #include <kdialog.h>
45 #include <kiconloader.h>
46 #include <klineedit.h>
47 #include <kmessagebox.h>
48 #include <kurl.h>
50 // Local
51 #include "kcookiesmain.h"
52 #include "kcookiespolicies.h"
54 struct CookieProp
56 QString host;
57 QString name;
58 QString value;
59 QString domain;
60 QString path;
61 QString expireDate;
62 QString secure;
63 bool allLoaded;
66 CookieListViewItem::CookieListViewItem(QTreeWidget *parent, const QString &dom)
67 :QTreeWidgetItem(parent)
69 init( 0, dom );
72 CookieListViewItem::CookieListViewItem(QTreeWidgetItem *parent, CookieProp *cookie)
73 :QTreeWidgetItem(parent)
75 init( cookie );
78 CookieListViewItem::~CookieListViewItem()
80 delete mCookie;
83 void CookieListViewItem::init( CookieProp* cookie, const QString &domain,
84 bool cookieLoaded )
86 mCookie = cookie;
87 mDomain = domain;
88 mCookiesLoaded = cookieLoaded;
90 if (mCookie)
91 setText(1, KUrl::fromAce(mCookie->host.toLatin1()));
92 else
93 setText(0, KUrl::fromAce(mDomain.toLatin1()));
96 CookieProp* CookieListViewItem::leaveCookie()
98 CookieProp *ret = mCookie;
99 mCookie = 0;
100 return ret;
103 KCookiesManagement::KCookiesManagement(const KComponentData &componentData, QWidget *parent)
104 : KCModule(componentData, parent)
106 // Toplevel layout
107 QVBoxLayout* mainLayout = new QVBoxLayout(this);
108 mainLayout->setMargin(0);
109 mainLayout->setSpacing(KDialog::spacingHint());
111 dlg = new KCookiesManagementDlgUI (this);
113 dlg->kListViewSearchLine->setTreeWidget(dlg->lvCookies);
115 dlg->lvCookies->setColumnWidth(0, 150);
117 mainLayout->addWidget(dlg);
119 connect(dlg->lvCookies, SIGNAL(itemExpanded(QTreeWidgetItem*)), SLOT(getCookies(QTreeWidgetItem*)) );
120 connect(dlg->lvCookies, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(showCookieDetails(QTreeWidgetItem*)) );
122 connect(dlg->pbDelete, SIGNAL(clicked()), SLOT(deleteCookie()));
123 connect(dlg->pbDeleteAll, SIGNAL(clicked()), SLOT(deleteAllCookies()));
124 connect(dlg->pbReload, SIGNAL(clicked()), SLOT(getDomains()));
125 connect(dlg->pbPolicy, SIGNAL(clicked()), SLOT(doPolicy()));
127 connect(dlg->lvCookies, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), SLOT(doPolicy()));
128 m_bDeleteAll = false;
129 mainWidget = parent;
132 KCookiesManagement::~KCookiesManagement()
136 void KCookiesManagement::load()
138 reset();
139 getDomains();
142 void KCookiesManagement::save()
144 // If delete all cookies was requested!
145 if(m_bDeleteAll)
147 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
148 QDBusReply<void> reply = kded.call( "deleteAllCookies" );
149 if (!reply.isValid())
151 QString caption = i18n ("D-Bus Communication Error");
152 QString message = i18n ("Unable to delete all the cookies as requested.");
153 KMessageBox::sorry (this, message, caption);
154 return;
157 m_bDeleteAll = false; // deleted[Cookies|Domains] have been cleared yet
160 // Certain groups of cookies were deleted...
161 QStringList::Iterator dIt = deletedDomains.begin();
162 while( dIt != deletedDomains.end() )
164 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
165 QDBusReply<void> reply = kded.call( "deleteCookiesFromDomain",( *dIt ) );
166 if( !reply.isValid() )
168 QString caption = i18n ("D-Bus Communication Error");
169 QString message = i18n ("Unable to delete cookies as requested.");
170 KMessageBox::sorry (this, message, caption);
171 return;
174 dIt = deletedDomains.erase(dIt);
177 // Individual cookies were deleted...
178 bool success = true; // Maybe we can go on...
179 QHashIterator<QString, CookiePropList> cookiesDom(deletedCookies);
180 while(cookiesDom.hasNext())
182 cookiesDom.next();
183 CookiePropList list = cookiesDom.value();
184 foreach(CookieProp *cookie, list)
186 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
187 QDBusReply<void> reply = kded.call( "deleteCookie", cookie->domain,
188 cookie->host, cookie->path,
189 cookie->name );
190 if( !reply.isValid() )
192 success = false;
193 break;
196 list.removeOne(cookie);
199 if(!success)
200 break;
202 deletedCookies.remove(cookiesDom.key());
205 emit changed( false );
208 void KCookiesManagement::defaults()
210 reset();
211 getDomains();
212 emit changed (false);
215 void KCookiesManagement::reset(bool deleteAll)
217 if ( !deleteAll )
218 m_bDeleteAll = false;
220 clearCookieDetails();
221 dlg->lvCookies->clear();
222 deletedDomains.clear();
223 deletedCookies.clear();
224 dlg->pbDelete->setEnabled(false);
225 dlg->pbDeleteAll->setEnabled(false);
226 dlg->pbPolicy->setEnabled(false);
229 void KCookiesManagement::clearCookieDetails()
231 dlg->leName->clear();
232 dlg->leValue->clear();
233 dlg->leDomain->clear();
234 dlg->lePath->clear();
235 dlg->leExpires->clear();
236 dlg->leSecure->clear();
239 QString KCookiesManagement::quickHelp() const
241 return i18n("<h1>Cookies Management Quick Help</h1>" );
244 void KCookiesManagement::getDomains()
246 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
247 QDBusReply<QStringList> reply = kded.call( "findDomains" );
248 if( !reply.isValid() )
250 QString caption = i18n ("Information Lookup Failure");
251 QString message = i18n ("Unable to retrieve information about the "
252 "cookies stored on your computer.");
253 KMessageBox::sorry (this, message, caption);
254 return;
257 const QStringList domains = reply;
259 if ( dlg->lvCookies->topLevelItemCount() > 0 )
261 reset();
262 dlg->lvCookies->setCurrentItem( 0L );
265 CookieListViewItem *dom;
266 for(QStringList::const_iterator dIt = domains.begin(); dIt != domains.end(); dIt++)
268 dom = new CookieListViewItem(dlg->lvCookies, *dIt);
269 dom->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
272 // are ther any cookies?
273 dlg->pbDeleteAll->setEnabled(dlg->lvCookies->topLevelItemCount() > 0);
275 dlg->lvCookies->sortItems(0, Qt::AscendingOrder);
278 Q_DECLARE_METATYPE( QList<int> )
280 void KCookiesManagement::getCookies(QTreeWidgetItem *cookieDom)
282 CookieListViewItem* ckd = static_cast<CookieListViewItem*>(cookieDom);
283 if ( ckd && ckd->cookiesLoaded() )
284 return;
286 QList<int> fields;
287 fields << 0 << 1 << 2 << 3;
288 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
289 QDBusReply<QStringList> reply = kded.call( "findCookies",
290 QVariant::fromValue( fields ),
291 ckd->domain(),
292 QString(),
293 QString(),
294 QString() );
296 if(reply.isValid())
298 const QStringList fieldVal = reply;
299 QStringList::const_iterator fIt = fieldVal.begin();
301 while(fIt != fieldVal.end())
303 CookieProp *details = new CookieProp;
304 details->domain = *fIt++;
305 details->path = *fIt++;
306 details->name = *fIt++;
307 details->host = *fIt++;
308 details->allLoaded = false;
309 new CookieListViewItem(cookieDom, details);
312 static_cast<CookieListViewItem*>(cookieDom)->setCookiesLoaded();
316 bool KCookiesManagement::cookieDetails(CookieProp *cookie)
318 QList<int> fields;
319 fields << 4 << 5 << 7;
321 QDBusInterface kded("org.kde.kded", "/modules/kcookiejar", "org.kde.KCookieServer", QDBusConnection::sessionBus());
322 QDBusReply<QStringList> reply = kded.call( "findCookies",
323 QVariant::fromValue( fields ),
324 cookie->domain,
325 cookie->host,
326 cookie->path,
327 cookie->name);
328 if( !reply.isValid() )
329 return false;
331 const QStringList fieldVal = reply;
333 QStringList::const_iterator c = fieldVal.begin();
334 if (c == fieldVal.end()) // empty list, do not crash
335 return false;
336 cookie->value = *c++;
337 unsigned tmp = (*c++).toUInt();
339 if( tmp == 0 )
340 cookie->expireDate = i18n("End of session");
341 else
343 QDateTime expDate;
344 expDate.setTime_t(tmp);
345 cookie->expireDate = KGlobal::locale()->formatDateTime(expDate);
348 tmp = (*c).toUInt();
349 cookie->secure = i18n(tmp ? "Yes" : "No");
350 cookie->allLoaded = true;
351 return true;
354 void KCookiesManagement::showCookieDetails(QTreeWidgetItem* item)
356 kDebug () << "::showCookieDetails... ";
357 if (!item)
358 return;
359 CookieProp *cookie = static_cast<CookieListViewItem*>(item)->cookie();
360 if( cookie )
362 if( cookie->allLoaded || cookieDetails(cookie) )
364 dlg->leName->setText(cookie->name);
365 dlg->leValue->setText(cookie->value);
366 dlg->leDomain->setText(cookie->domain);
367 dlg->lePath->setText(cookie->path);
368 dlg->leExpires->setText(cookie->expireDate);
369 dlg->leSecure->setText(cookie->secure);
372 dlg->pbPolicy->setEnabled (true);
374 else
376 clearCookieDetails();
377 dlg->pbPolicy->setEnabled(false);
380 dlg->pbDelete->setEnabled(true);
383 void KCookiesManagement::doPolicy()
385 // Get current item
386 CookieListViewItem *item = static_cast<CookieListViewItem*>( dlg->lvCookies->currentItem() );
388 if( item && item->cookie())
390 CookieProp *cookie = item->cookie();
392 QString domain = cookie->domain;
394 if( domain.isEmpty() )
396 CookieListViewItem *parent = static_cast<CookieListViewItem*>( item->parent() );
398 if ( parent )
399 domain = parent->domain ();
402 KCookiesMain* mainDlg =static_cast<KCookiesMain*>( mainWidget );
403 // must be present or something is really wrong.
404 assert (mainDlg);
406 KCookiesPolicies* policyDlg = mainDlg->policyDlg();
407 // must be present unless someone rewrote the widget in which case
408 // this needs to be re-written as well.
409 assert (policyDlg);
410 policyDlg->addNewPolicy(domain);
414 void KCookiesManagement::deleteCookie()
416 QTreeWidgetItem* currentItem = dlg->lvCookies->currentItem();
417 CookieListViewItem *item = static_cast<CookieListViewItem*>( currentItem );
418 if( item->cookie() )
420 CookieListViewItem *parent = static_cast<CookieListViewItem*>(item->parent());
421 CookiePropList list = deletedCookies.value(parent->domain());
422 list.append(item->leaveCookie());
423 deletedCookies.insert(parent->domain(), list);
424 delete item;
425 if(parent->childCount() == 0)
426 delete parent;
428 else
430 deletedDomains.append(item->domain());
431 delete item;
434 currentItem = dlg->lvCookies->currentItem();
435 if ( currentItem )
437 dlg->lvCookies->setCurrentItem( currentItem );
438 showCookieDetails( currentItem );
440 else
441 clearCookieDetails();
443 dlg->pbDelete->setEnabled(dlg->lvCookies->currentItem());
444 dlg->pbDeleteAll->setEnabled(dlg->lvCookies->topLevelItemCount() > 0);
445 dlg->pbPolicy->setEnabled(dlg->lvCookies->currentItem());
447 emit changed( true );
450 void KCookiesManagement::deleteAllCookies()
452 m_bDeleteAll = true;
453 reset(true);
455 emit changed( true );
458 #include "kcookiesmanagement.moc"