33 #include <tqptrlist.h>
36 #include <dcopclient.h>
38 #include <tdeconfig.h>
40 #include <tdeapplication.h>
41 #include <tdecmdlineargs.h>
42 #include <kstandarddirs.h>
44 #include "kcookiejar.h"
45 #include "kcookiewin.h"
46 #include "kcookieserver.h"
49 TDE_EXPORT KDEDModule *create_kcookiejar(
const TQCString &name)
51 return new KCookieServer(name);
57 enum CookieDetails { CF_DOMAIN=0, CF_PATH, CF_NAME, CF_HOST,
58 CF_VALUE, CF_EXPIRE, CF_PROVER, CF_SECURE };
64 DCOPClientTransaction *transaction;
70 template class TQPtrList<CookieRequest>;
72 class RequestList :
public TQPtrList<CookieRequest>
75 RequestList() : TQPtrList<CookieRequest>() { }
78 KCookieServer::KCookieServer(
const TQCString &name)
81 mOldCookieServer =
new DCOPClient();
82 mOldCookieServer->registerAs(
"kcookiejar",
false);
83 mOldCookieServer->setDaemonMode(
true );
84 mCookieJar =
new KCookieJar;
85 mPendingCookies =
new KHttpCookieList;
86 mPendingCookies->setAutoDelete(
true);
87 mRequestList =
new RequestList;
88 mAdvicePending =
false;
89 mTimer =
new TQTimer();
90 connect( mTimer, TQ_SIGNAL( timeout()), TQ_SLOT( slotSave()));
91 mConfig =
new TDEConfig(
"kcookiejarrc");
92 mCookieJar->loadConfig( mConfig );
94 TQString filename = locateLocal(
"data",
"kcookiejar/cookies");
97 TQString filenameOld = locate(
"data",
"kfm/cookies");
98 if (!filenameOld.isEmpty())
100 mCookieJar->loadCookies( filenameOld );
101 if (mCookieJar->saveCookies( filename))
103 unlink(TQFile::encodeName(filenameOld));
108 mCookieJar->loadCookies( filename);
110 connect(
this, TQ_SIGNAL(windowUnregistered(
long)),
111 this, TQ_SLOT(slotDeleteSessionCookies(
long)));
114 KCookieServer::~KCookieServer()
116 if (mCookieJar->changed())
118 delete mOldCookieServer;
121 delete mPendingCookies;
125 bool KCookieServer::cookiesPending(
const TQString &url, KHttpCookieList *cookieList )
128 TQStringList domains;
131 if (mPendingCookies->isEmpty())
133 if (!KCookieJar::parseURL(url, fqdn, path))
136 mCookieJar->extractDomains( fqdn, domains );
137 for( KHttpCookie *cookie = mPendingCookies->first();
139 cookie = mPendingCookies->next())
141 if (cookie->match( fqdn, domains, path))
145 cookieList->append(cookie);
150 return cookieList->isEmpty();
153 void KCookieServer::addCookies(
const TQString &url,
const TQCString &cookieHeader,
154 long windowId,
bool useDOMFormat )
156 KHttpCookieList cookieList;
158 cookieList = mCookieJar->makeDOMCookies(url, cookieHeader, windowId);
160 cookieList = mCookieJar->makeCookies(url, cookieHeader, windowId);
162 checkCookies(&cookieList);
164 for(KHttpCookiePtr cookie = cookieList.first(); cookie; cookie = cookieList.first())
165 mPendingCookies->append(cookieList.take());
169 mAdvicePending =
true;
170 while (!mPendingCookies->isEmpty())
174 mAdvicePending =
false;
178 void KCookieServer::checkCookies( KHttpCookieList *cookieList)
180 KHttpCookieList *list;
185 list = mPendingCookies;
187 KHttpCookiePtr cookie = list->first();
190 kdDebug(7104) <<
"checkCookies: Asking cookie advice for " << cookie->host() << endl;
191 KCookieAdvice advice = mCookieJar->cookieAdvice(cookie);
196 mCookieJar->addCookie(cookie);
197 cookie = list->current();
203 cookie = list->current();
207 cookie = list->next();
212 if (cookieList || list->isEmpty())
215 KHttpCookiePtr currentCookie = mPendingCookies->first();
217 KHttpCookieList currentList;
218 currentList.append(currentCookie);
219 TQString currentHost = currentCookie->host();
221 cookie = mPendingCookies->next();
224 if (cookie->host() == currentHost)
226 currentList.append(cookie);
228 cookie = mPendingCookies->next();
231 KCookieWin *kw =
new KCookieWin( 0L, currentList,
232 mCookieJar->preferredDefaultPolicy(),
233 mCookieJar->showCookieDetails() );
234 KCookieAdvice userAdvice = kw->advice(mCookieJar, currentCookie);
237 mCookieJar->saveConfig( mConfig );
241 cookie = mPendingCookies->first();
244 if (cookie->host() == currentHost)
249 mPendingCookies->take();
250 mCookieJar->addCookie(cookie);
251 cookie = mPendingCookies->current();
255 mPendingCookies->take();
257 cookie = mPendingCookies->current();
261 tqWarning(__FILE__
":%d Problem!", __LINE__);
262 cookie = mPendingCookies->next();
268 cookie = mPendingCookies->next();
274 for ( CookieRequest *request = mRequestList->first(); request;)
276 if (!cookiesPending( request->url ))
279 TQByteArray replyData;
280 TQString res = mCookieJar->findCookies( request->url, request->DOM, request->windowId );
282 TQDataStream stream2(replyData, IO_WriteOnly);
284 replyType =
"TQString";
285 request->client->endTransaction( request->transaction,
286 replyType, replyData);
287 CookieRequest *tmp = request;
288 request = mRequestList->next();
289 mRequestList->removeRef( tmp );
294 request = mRequestList->next();
297 if (mCookieJar->changed())
301 void KCookieServer::slotSave()
303 TQString filename = locateLocal(
"data",
"kcookiejar/cookies");
304 mCookieJar->saveCookies(filename);
307 void KCookieServer::saveCookieJar()
309 if( mTimer->isActive() )
312 mTimer->start( 1000*60*SAVE_DELAY,
true );
315 void KCookieServer::putCookie( TQStringList& out, KHttpCookie *cookie,
316 const TQValueList<int>& fields )
318 TQValueList<int>::ConstIterator i = fields.begin();
319 for ( ; i != fields.end(); ++i )
324 out << cookie->domain();
327 out << cookie->name();
330 out << cookie->path();
333 out << cookie->host();
336 out << cookie->value();
339 out << TQString::number(cookie->expireDate());
342 out << TQString::number(cookie->protocolVersion());
345 out << TQString::number( cookie->isSecure() ? 1 : 0 );
348 out << TQString::null;
353 bool KCookieServer::cookieMatches( KHttpCookiePtr c,
354 TQString domain, TQString fqdn,
355 TQString path, TQString name )
359 bool hasDomain = !domain.isEmpty();
361 ((hasDomain && c->domain() == domain) ||
362 fqdn == c->host()) &&
363 (c->path() == path) &&
364 (c->name() == name) &&
365 (!c->isExpired(time(0)));
372 KCookieServer::findCookies(TQString url)
374 return findCookies(url, 0);
379 KCookieServer::findCookies(TQString url,
long windowId)
381 if (cookiesPending(url))
383 CookieRequest *request =
new CookieRequest;
384 request->client = callingDcopClient();
385 request->transaction = request->client->beginTransaction();
387 request->DOM =
false;
388 request->windowId = windowId;
389 mRequestList->append( request );
390 return TQString::null;
393 TQString cookies = mCookieJar->findCookies(url,
false, windowId);
395 if (mCookieJar->changed())
403 KCookieServer::findDomains()
406 const TQStringList domains = mCookieJar->getDomainList();
407 for ( TQStringList::ConstIterator domIt = domains.begin();
408 domIt != domains.end(); ++domIt )
412 const KHttpCookieList* list = mCookieJar->getCookieList(*domIt,
"");
413 if ( list && !list->isEmpty() )
421 KCookieServer::findCookies(TQValueList<int> fields,
428 bool allDomCookies = name.isEmpty();
430 const KHttpCookieList* list = mCookieJar->getCookieList(domain, fqdn);
431 if ( list && !list->isEmpty() )
433 TQPtrListIterator<KHttpCookie>it( *list );
434 for ( ; it.current(); ++it )
436 if ( !allDomCookies )
438 if ( cookieMatches(it.current(), domain, fqdn, path, name) )
440 putCookie(result, it.current(), fields);
445 putCookie(result, it.current(), fields);
453 KCookieServer::findDOMCookies(TQString url)
455 return findDOMCookies(url, 0);
460 KCookieServer::findDOMCookies(TQString url,
long windowId)
465 KHttpCookieList pendingCookies;
466 cookiesPending(url, &pendingCookies);
468 return mCookieJar->findCookies(url,
true, windowId, &pendingCookies);
473 KCookieServer::addCookies(TQString arg1, TQCString arg2,
long arg3)
475 addCookies(arg1, arg2, arg3,
false);
480 KCookieServer::deleteCookie(TQString domain, TQString fqdn,
481 TQString path, TQString name)
483 const KHttpCookieList* list = mCookieJar->getCookieList( domain, fqdn );
484 if ( list && !list->isEmpty() )
486 TQPtrListIterator<KHttpCookie>it (*list);
487 for ( ; it.current(); ++it )
489 if( cookieMatches(it.current(), domain, fqdn, path, name) )
491 mCookieJar->eatCookie( it.current() );
501 KCookieServer::deleteCookiesFromDomain(TQString domain)
503 mCookieJar->eatCookiesForDomain(domain);
510 KCookieServer::slotDeleteSessionCookies(
long windowId )
512 deleteSessionCookies(windowId);
517 KCookieServer::deleteSessionCookies(
long windowId )
519 mCookieJar->eatSessionCookies( windowId );
524 KCookieServer::deleteSessionCookiesFor(TQString fqdn,
long windowId)
526 mCookieJar->eatSessionCookies( fqdn, windowId );
532 KCookieServer::deleteAllCookies()
534 mCookieJar->eatAllCookies();
540 KCookieServer::addDOMCookies(TQString arg1, TQCString arg2,
long arg3)
542 addCookies(arg1, arg2, arg3,
true);
547 KCookieServer::setDomainAdvice(TQString url, TQString advice)
551 if (KCookieJar::parseURL(url, fqdn, dummy))
553 TQStringList domains;
554 mCookieJar->extractDomains(fqdn, domains);
556 mCookieJar->setDomainAdvice(domains[domains.count() > 3 ? 3 : 0],
557 KCookieJar::strToAdvice(advice));
559 mCookieJar->saveConfig( mConfig );
565 KCookieServer::getDomainAdvice(TQString url)
567 KCookieAdvice advice = KCookieDunno;
570 if (KCookieJar::parseURL(url, fqdn, dummy))
572 TQStringList domains;
573 mCookieJar->extractDomains(fqdn, domains);
575 TQStringList::ConstIterator it = domains.begin();
576 while ( (advice == KCookieDunno) && (it != domains.end()) )
581 if ( (*it)[0] ==
'.' || (*it) == fqdn )
582 advice = mCookieJar->getDomainAdvice(*it);
585 if (advice == KCookieDunno)
586 advice = mCookieJar->getGlobalAdvice();
588 return KCookieJar::adviceToStr(advice);
593 KCookieServer::reloadPolicy()
595 mCookieJar->loadConfig( mConfig,
true );
600 KCookieServer::shutdown()
605 #include "kcookieserver.moc"