| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# sessiondata.cpp

    00001 /* This file is part of the KDE project
    00002    Copyright (C) 2000 Dawit Alemayehu <adawit@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Lesser General Public
    00006    License (LGPL) as published by the Free Software Foundation;
    00007    either version 2 of the License, or (at your option) any
    00008    later version.
    00009
    00010    This library is distributed in the hope that it will be useful,
    00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013    Library General Public License for more details.
    00014
    00015    You should have received a copy of the GNU Lesser General Public
    00016    License along with this library; see the file COPYING.LIB.  If not,
    00017    write to the Free Software Foundation, Inc., 59 Temple Place -
    00018    Suite 330, Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qptrlist.h>
    00022 #include <qtextcodec.h>
    00023
    00024 #include <kdebug.h>
    00025 #include <kconfig.h>
    00026 #include <kglobal.h>
    00027 #include <klocale.h>
    00028 #include <kcharsets.h>
    00029 #include <dcopclient.h>
    00030 #include <kprotocolmanager.h>
    00031 #include <kstandarddirs.h>
    00032
    00033 #include <kdesu/client.h>
    00034 #include <kio/slaveconfig.h>
    00035 #include <kio/http_slave_defaults.h>
    00036
    00037 #include "sessiondata.h"
    00038 #include "sessiondata.moc"
    00039
    00040 namespace KIO {
    00041
    00042 /***************************** SessionData::AuthData ************************/
    00043 struct SessionData::AuthData
    00044 {
    00045
    00046 public:
    00047   AuthData() {}
    00048
    00049   AuthData(const QCString& k, const QCString& g, bool p) {
    00050     key = k;
    00051     group = g;
    00052     persist = p;
    00053   }
    00054
    00055   bool isKeyMatch( const QCString& val ) const {
    00056     return (val==key);
    00057   }
    00058
    00059   bool isGroupMatch( const QCString& val ) const {
    00060     return (val==group);
    00061   }
    00062
    00063   QCString key;
    00064   QCString group;
    00065   bool persist;
    00066 };
    00067
    00068 /************************* SessionData::AuthDataList ****************************/
    00069 class SessionData::AuthDataList : public QPtrList<SessionData::AuthData>
    00070 {
    00071 public:
    00072   AuthDataList();
    00073   ~AuthDataList();
    00074
    00075   void addData( SessionData::AuthData* );
    00076   void removeData( const QCString& );
    00077
    00078   bool pingCacheDaemon();
    00079   void registerAuthData( SessionData::AuthData* );
    00080   void unregisterAuthData( SessionData::AuthData* );
    00081   void purgeCachedData();
    00082
    00083 private:
    00084   KDEsuClient * m_kdesuClient;
    00085 };
    00086
    00087 SessionData::AuthDataList::AuthDataList()
    00088 {
    00089   m_kdesuClient = new KDEsuClient;
    00090   setAutoDelete(true);
    00091 }
    00092
    00093 SessionData::AuthDataList::~AuthDataList()
    00094 {
    00095   purgeCachedData();
    00096   delete m_kdesuClient;
    00097   m_kdesuClient = 0;
    00098 }
    00099
    00100 void SessionData::AuthDataList::addData( SessionData::AuthData* d )
    00101 {
    00102   QPtrListIterator<SessionData::AuthData> it ( *this );
    00103   for ( ; it.current(); ++it )
    00104   {
    00105     if ( it.current()->isKeyMatch( d->key ) )
    00106         return;
    00107   }
    00108   registerAuthData( d );
    00109   append( d );
    00110 }
    00111
    00112 void SessionData::AuthDataList::removeData( const QCString& gkey )
    00113 {
    00114   QPtrListIterator<SessionData::AuthData> it( *this );
    00115   for( ; it.current(); ++it )
    00116   {
    00117     if ( it.current()->isGroupMatch(gkey) &&  pingCacheDaemon() )
    00118     {
    00119         unregisterAuthData( it.current() );
    00120         remove( it.current() );
    00121     }
    00122   }
    00123 }
    00124
    00125 bool SessionData::AuthDataList::pingCacheDaemon()
    00126 {
    00127   Q_ASSERT(m_kdesuClient);
    00128
    00129   int sucess = m_kdesuClient->ping();
    00130   if( sucess == -1 )
    00131   {
    00132     sucess = m_kdesuClient->startServer();
    00133     if( sucess == -1 )
    00134         return false;
    00135   }
    00136   return true;
    00137 }
    00138
    00139 void SessionData::AuthDataList::registerAuthData( SessionData::AuthData* d )
    00140 {
    00141   if( !pingCacheDaemon() )
    00142     return;
    00143
    00144   bool ok;
    00145   QCString ref_key = d->key + "-refcount";
    00146   int count = m_kdesuClient->getVar(ref_key).toInt( &ok );
    00147   if( ok )
    00148   {
    00149     QCString val;
    00150     val.setNum( count+1 );
    00151     m_kdesuClient->setVar( ref_key, val, 0, d->group );
    00152   }
    00153   else
    00154     m_kdesuClient->setVar( ref_key, "1", 0, d->group );
    00155 }
    00156
    00157 void SessionData::AuthDataList::unregisterAuthData( SessionData::AuthData* d )
    00158 {
    00159   if ( !d  || d->persist )
    00160     return;
    00161
    00162   bool ok;
    00163   int count;
    00164   QCString ref_key = d->key + "-refcount";
    00165
    00166   count = m_kdesuClient->getVar( ref_key ).toInt( &ok );
    00167   if ( ok )
    00168   {
    00169     if ( count > 1 )
    00170     {
    00171         QCString val;
    00172         val.setNum(count-1);
    00173         m_kdesuClient->setVar( ref_key, val, 0, d->group );
    00174     }
    00175     else
    00176     {
    00177         m_kdesuClient->delVars(d->key);
    00178     }
    00179   }
    00180 }
    00181
    00182 void SessionData::AuthDataList::purgeCachedData()
    00183 {
    00184   if ( !isEmpty() && pingCacheDaemon() )
    00185   {
    00186     QPtrListIterator<SessionData::AuthData> it( *this );
    00187     for ( ; it.current(); ++it )
    00188         unregisterAuthData( it.current() );
    00189   }
    00190 }
    00191
    00192 /********************************* SessionData ****************************/
    00193
    00194 class SessionData::SessionDataPrivate
    00195 {
    00196 public:
    00197   SessionDataPrivate() {
    00198     useCookie = true;
    00199     initDone = false;
    00200   }
    00201
    00202   bool initDone;
    00203   bool useCookie;
    00204   QString charsets;
    00205   QString language;
    00206 };
    00207
    00208 SessionData::SessionData()
    00209 {
    00210   authData = 0;
    00211   d = new SessionDataPrivate;
    00212 }
    00213
    00214 SessionData::~SessionData()
    00215 {
    00216   delete d;
    00217   delete authData;
    00218   d = 0L;
    00219   authData = 0L;
    00220 }
    00221
    00222 void SessionData::configDataFor( MetaData &configData, const QString &proto,
    00223                              const QString & )
    00224 {
    00225   if ( (proto.find("http", 0, false) == 0 ) ||
    00226      (proto.find("webdav", 0, false) == 0) )
    00227   {
    00228     if (!d->initDone)
    00229         reset();
    00230
    00231     // These might have already been set so check first
    00232     // to make sure that we do not trumpt settings sent
    00233     // by apps or end-user.
    00234     if ( configData["Cookies"].isEmpty() )
    00235         configData["Cookies"] = d->useCookie ? "true" : "false";
    00236     if ( configData["Languages"].isEmpty() )
    00237         configData["Languages"] = d->language;
    00238     if ( configData["Charsets"].isEmpty() )
    00239         configData["Charsets"] = d->charsets;
    00240     if ( configData["CacheDir"].isEmpty() )
    00241         configData["CacheDir"] = KGlobal::dirs()->saveLocation("cache", "http");
    00242     if ( configData["UserAgent"].isEmpty() )
    00243     {
    00244       configData["UserAgent"] = KProtocolManager::defaultUserAgent();
    00245     }
    00246   }
    00247 }
    00248
    00249 void SessionData::reset()
    00250 {
    00251     d->initDone = true;
    00252     // Get Cookie settings...
    00253     KConfig* cfg = new KConfig("kcookiejarrc", true, false);
    00254     cfg->setGroup( "Cookie Policy" );
    00255     d->useCookie = cfg->readBoolEntry( "Cookies", true );
    00256     delete cfg;
    00257
    00258     static const QString & english = KGlobal::staticQString( "en" );
    00259
    00260     // Get language settings...
    00261     QStringList languageList = KGlobal::locale()->languagesTwoAlpha();
    00262     QStringList::Iterator it = languageList.find( QString::fromLatin1("C") );
    00263     if ( it != languageList.end() )
    00264     {
    00265         if ( languageList.contains( english ) > 0 )
    00266           languageList.remove( it );
    00267         else
    00268           (*it) = english;
    00269     }
    00270     if ( !languageList.contains( english ) )
    00271        languageList.append( english );
    00272
    00273     d->language = languageList.join( ", " );
    00274
    00275     d->charsets = QString::fromLatin1(QTextCodec::codecForLocale()->mimeName()).lower();
    00276     KProtocolManager::reparseConfiguration();
    00277 }
    00278
    00279 void SessionData::slotAuthData( const QCString& key, const QCString& gkey,
    00280                                  bool keep )
    00281 {
    00282   if (!authData)
    00283     authData = new AuthDataList;
    00284   authData->addData( new SessionData::AuthData(key, gkey, keep) );
    00285 }
    00286
    00287 void SessionData::slotDelAuthData( const QCString& gkey )
    00288 {
    00289   if (!authData)
    00290      return;
    00291   authData->removeData( gkey );
    00292 }
    00293
    00294 void SessionData::virtual_hook( int, void* )
    00295 { /*BASE::virtual_hook( id, data );*/ }
    00296
    00297 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:38 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001