kio
sessiondata.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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 #ifdef Q_OS_UNIX
00085 KDEsuClient * m_kdesuClient;
00086 #endif
00087 };
00088
00089 SessionData::AuthDataList::AuthDataList()
00090 {
00091 #ifdef Q_OS_UNIX
00092 m_kdesuClient = new KDEsuClient;
00093 #endif
00094 setAutoDelete(true);
00095 }
00096
00097 SessionData::AuthDataList::~AuthDataList()
00098 {
00099 purgeCachedData();
00100 #ifdef Q_OS_UNIX
00101 delete m_kdesuClient;
00102 m_kdesuClient = 0;
00103 #endif
00104 }
00105
00106 void SessionData::AuthDataList::addData( SessionData::AuthData* d )
00107 {
00108 QPtrListIterator<SessionData::AuthData> it ( *this );
00109 for ( ; it.current(); ++it )
00110 {
00111 if ( it.current()->isKeyMatch( d->key ) )
00112 return;
00113 }
00114 registerAuthData( d );
00115 append( d );
00116 }
00117
00118 void SessionData::AuthDataList::removeData( const QCString& gkey )
00119 {
00120 QPtrListIterator<SessionData::AuthData> it( *this );
00121 for( ; it.current(); ++it )
00122 {
00123 if ( it.current()->isGroupMatch(gkey) && pingCacheDaemon() )
00124 {
00125 unregisterAuthData( it.current() );
00126 remove( it.current() );
00127 }
00128 }
00129 }
00130
00131 bool SessionData::AuthDataList::pingCacheDaemon()
00132 {
00133 #ifdef Q_OS_UNIX
00134 Q_ASSERT(m_kdesuClient);
00135
00136 int success = m_kdesuClient->ping();
00137 if( success == -1 )
00138 {
00139 success = m_kdesuClient->startServer();
00140 if( success == -1 )
00141 return false;
00142 }
00143 return true;
00144 #else
00145 return false;
00146 #endif
00147 }
00148
00149 void SessionData::AuthDataList::registerAuthData( SessionData::AuthData* d )
00150 {
00151 if( !pingCacheDaemon() )
00152 return;
00153
00154 #ifdef Q_OS_UNIX
00155 bool ok;
00156 QCString ref_key = d->key + "-refcount";
00157 int count = m_kdesuClient->getVar(ref_key).toInt( &ok );
00158 if( ok )
00159 {
00160 QCString val;
00161 val.setNum( count+1 );
00162 m_kdesuClient->setVar( ref_key, val, 0, d->group );
00163 }
00164 else
00165 m_kdesuClient->setVar( ref_key, "1", 0, d->group );
00166 #endif
00167 }
00168
00169 void SessionData::AuthDataList::unregisterAuthData( SessionData::AuthData* d )
00170 {
00171 if ( !d || d->persist )
00172 return;
00173
00174 bool ok;
00175 int count;
00176 QCString ref_key = d->key + "-refcount";
00177
00178 #ifdef Q_OS_UNIX
00179 count = m_kdesuClient->getVar( ref_key ).toInt( &ok );
00180 if ( ok )
00181 {
00182 if ( count > 1 )
00183 {
00184 QCString val;
00185 val.setNum(count-1);
00186 m_kdesuClient->setVar( ref_key, val, 0, d->group );
00187 }
00188 else
00189 {
00190 m_kdesuClient->delVars(d->key);
00191 }
00192 }
00193 #endif
00194 }
00195
00196 void SessionData::AuthDataList::purgeCachedData()
00197 {
00198 if ( !isEmpty() && pingCacheDaemon() )
00199 {
00200 QPtrListIterator<SessionData::AuthData> it( *this );
00201 for ( ; it.current(); ++it )
00202 unregisterAuthData( it.current() );
00203 }
00204 }
00205
00206
00207
00208 class SessionData::SessionDataPrivate
00209 {
00210 public:
00211 SessionDataPrivate() {
00212 useCookie = true;
00213 initDone = false;
00214 }
00215
00216 bool initDone;
00217 bool useCookie;
00218 QString charsets;
00219 QString language;
00220 };
00221
00222 SessionData::SessionData()
00223 {
00224 authData = 0;
00225 d = new SessionDataPrivate;
00226 }
00227
00228 SessionData::~SessionData()
00229 {
00230 delete d;
00231 delete authData;
00232 d = 0L;
00233 authData = 0L;
00234 }
00235
00236 void SessionData::configDataFor( MetaData &configData, const QString &proto,
00237 const QString & )
00238 {
00239 if ( (proto.find("http", 0, false) == 0 ) ||
00240 (proto.find("webdav", 0, false) == 0) )
00241 {
00242 if (!d->initDone)
00243 reset();
00244
00245
00246
00247
00248 if ( configData["Cookies"].isEmpty() )
00249 configData["Cookies"] = d->useCookie ? "true" : "false";
00250 if ( configData["Languages"].isEmpty() )
00251 configData["Languages"] = d->language;
00252 if ( configData["Charsets"].isEmpty() )
00253 configData["Charsets"] = d->charsets;
00254 if ( configData["CacheDir"].isEmpty() )
00255 configData["CacheDir"] = KGlobal::dirs()->saveLocation("cache", "http");
00256 if ( configData["UserAgent"].isEmpty() )
00257 {
00258 configData["UserAgent"] = KProtocolManager::defaultUserAgent();
00259 }
00260 }
00261 }
00262
00263 void SessionData::reset()
00264 {
00265 d->initDone = true;
00266
00267 KConfig* cfg = new KConfig("kcookiejarrc", true, false);
00268 cfg->setGroup( "Cookie Policy" );
00269 d->useCookie = cfg->readBoolEntry( "Cookies", true );
00270 delete cfg;
00271
00272 static const QString & english = KGlobal::staticQString( "en" );
00273
00274
00275 QStringList languageList = KGlobal::locale()->languagesTwoAlpha();
00276 QStringList::Iterator it = languageList.find( QString::fromLatin1("C") );
00277 if ( it != languageList.end() )
00278 {
00279 if ( languageList.contains( english ) > 0 )
00280 languageList.remove( it );
00281 else
00282 (*it) = english;
00283 }
00284 if ( !languageList.contains( english ) )
00285 languageList.append( english );
00286
00287 d->language = languageList.join( ", " );
00288
00289 d->charsets = QString::fromLatin1(QTextCodec::codecForLocale()->mimeName()).lower();
00290 KProtocolManager::reparseConfiguration();
00291 }
00292
00293 void SessionData::slotAuthData( const QCString& key, const QCString& gkey,
00294 bool keep )
00295 {
00296 if (!authData)
00297 authData = new AuthDataList;
00298 authData->addData( new SessionData::AuthData(key, gkey, keep) );
00299 }
00300
00301 void SessionData::slotDelAuthData( const QCString& gkey )
00302 {
00303 if (!authData)
00304 return;
00305 authData->removeData( gkey );
00306 }
00307
00308 void SessionData::virtual_hook( int, void* )
00309 { }
00310
00311 }