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.
23 #include "kcookiesmanagement.h"
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>
45 #include <kiconloader.h>
46 #include <klineedit.h>
47 #include <kmessagebox.h>
51 #include "kcookiesmain.h"
52 #include "kcookiespolicies.h"
66 CookieListViewItem::CookieListViewItem(QTreeWidget
*parent
, const QString
&dom
)
67 :QTreeWidgetItem(parent
)
72 CookieListViewItem::CookieListViewItem(QTreeWidgetItem
*parent
, CookieProp
*cookie
)
73 :QTreeWidgetItem(parent
)
78 CookieListViewItem::~CookieListViewItem()
83 void CookieListViewItem::init( CookieProp
* cookie
, const QString
&domain
,
88 mCookiesLoaded
= cookieLoaded
;
91 setText(1, KUrl::fromAce(mCookie
->host
.toLatin1()));
93 setText(0, KUrl::fromAce(mDomain
.toLatin1()));
96 CookieProp
* CookieListViewItem::leaveCookie()
98 CookieProp
*ret
= mCookie
;
103 KCookiesManagement::KCookiesManagement(const KComponentData
&componentData
, QWidget
*parent
)
104 : KCModule(componentData
, parent
)
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;
132 KCookiesManagement::~KCookiesManagement()
136 void KCookiesManagement::load()
142 void KCookiesManagement::save()
144 // If delete all cookies was requested!
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
);
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
);
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())
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
,
190 if( !reply
.isValid() )
196 list
.removeOne(cookie
);
202 deletedCookies
.remove(cookiesDom
.key());
205 emit
changed( false );
208 void KCookiesManagement::defaults()
212 emit
changed (false);
215 void KCookiesManagement::reset(bool 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
);
257 const QStringList domains
= reply
;
259 if ( dlg
->lvCookies
->topLevelItemCount() > 0 )
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() )
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
),
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
)
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
),
328 if( !reply
.isValid() )
331 const QStringList fieldVal
= reply
;
333 QStringList::const_iterator c
= fieldVal
.begin();
334 if (c
== fieldVal
.end()) // empty list, do not crash
336 cookie
->value
= *c
++;
337 unsigned tmp
= (*c
++).toUInt();
340 cookie
->expireDate
= i18n("End of session");
344 expDate
.setTime_t(tmp
);
345 cookie
->expireDate
= KGlobal::locale()->formatDateTime(expDate
);
349 cookie
->secure
= i18n(tmp
? "Yes" : "No");
350 cookie
->allLoaded
= true;
354 void KCookiesManagement::showCookieDetails(QTreeWidgetItem
* item
)
356 kDebug () << "::showCookieDetails... ";
359 CookieProp
*cookie
= static_cast<CookieListViewItem
*>(item
)->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);
376 clearCookieDetails();
377 dlg
->pbPolicy
->setEnabled(false);
380 dlg
->pbDelete
->setEnabled(true);
383 void KCookiesManagement::doPolicy()
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() );
399 domain
= parent
->domain ();
402 KCookiesMain
* mainDlg
=static_cast<KCookiesMain
*>( mainWidget
);
403 // must be present or something is really wrong.
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.
410 policyDlg
->addNewPolicy(domain
);
414 void KCookiesManagement::deleteCookie()
416 QTreeWidgetItem
* currentItem
= dlg
->lvCookies
->currentItem();
417 CookieListViewItem
*item
= static_cast<CookieListViewItem
*>( currentItem
);
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
);
425 if(parent
->childCount() == 0)
430 deletedDomains
.append(item
->domain());
434 currentItem
= dlg
->lvCookies
->currentItem();
437 dlg
->lvCookies
->setCurrentItem( currentItem
);
438 showCookieDetails( currentItem
);
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()
455 emit
changed( true );
458 #include "kcookiesmanagement.moc"