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 "sessiondata.h"
00022 #include "sessiondata.moc"
00023
00024 #include <config.h>
00025
00026 #include <QtCore/QList>
00027 #include <QtCore/QTextCodec>
00028
00029 #include <kdebug.h>
00030 #include <kconfiggroup.h>
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 #include <kcharsets.h>
00034 #include <kprotocolmanager.h>
00035 #include <ksharedconfig.h>
00036 #include <kstandarddirs.h>
00037
00038 #include <kdesu/client.h>
00039 #include <kio/slaveconfig.h>
00040 #include <kio/http_slave_defaults.h>
00041
00042 namespace KIO {
00043
00044
00045 struct SessionData::AuthData
00046 {
00047
00048 public:
00049 AuthData() {}
00050
00051 AuthData(const QByteArray& k, const QByteArray& g, bool p) {
00052 key = k;
00053 group = g;
00054 persist = p;
00055 }
00056
00057 bool isKeyMatch( const QByteArray& val ) const {
00058 return (val==key);
00059 }
00060
00061 bool isGroupMatch( const QByteArray& val ) const {
00062 return (val==group);
00063 }
00064
00065 QByteArray key;
00066 QByteArray group;
00067 bool persist;
00068 };
00069
00070 #if 0
00071
00072 class SessionData::AuthDataList : public QList<SessionData::AuthData*>
00073 {
00074 public:
00075 AuthDataList();
00076 ~AuthDataList();
00077
00078 void addData( SessionData::AuthData* );
00079 void removeData( const QByteArray& );
00080
00081 bool pingCacheDaemon();
00082 void registerAuthData( SessionData::AuthData* );
00083 void unregisterAuthData( SessionData::AuthData* );
00084 void purgeCachedData();
00085
00086 private:
00087 #ifdef Q_OS_UNIX
00088 KDEsuClient * m_kdesuClient;
00089 #endif
00090 };
00091
00092 SessionData::AuthDataList::AuthDataList()
00093 {
00094 #ifdef Q_OS_UNIX
00095 m_kdesuClient = new KDEsuClient;
00096 #endif
00097 qDeleteAll(*this);
00098 }
00099
00100 SessionData::AuthDataList::~AuthDataList()
00101 {
00102 purgeCachedData();
00103 #ifdef Q_OS_UNIX
00104 delete m_kdesuClient;
00105 m_kdesuClient = 0;
00106 #endif
00107 }
00108
00109 void SessionData::AuthDataList::addData( SessionData::AuthData* d )
00110 {
00111 QList<SessionData::AuthData*>::iterator it;
00112 for ( it = begin() ; it != end(); ++it )
00113 {
00114 if ( (*it)->isKeyMatch( d->key ) )
00115 return;
00116 }
00117 registerAuthData( d );
00118 append( d );
00119 }
00120
00121 void SessionData::AuthDataList::removeData( const QByteArray& gkey )
00122 {
00123 QList<SessionData::AuthData*>::iterator it;
00124 for ( it = begin() ; it != end(); ++it )
00125 {
00126 if ( (*it)->isGroupMatch(gkey) && pingCacheDaemon() )
00127 {
00128 unregisterAuthData( (*it) );
00129 erase( it );
00130 }
00131 }
00132 }
00133
00134 bool SessionData::AuthDataList::pingCacheDaemon()
00135 {
00136 #ifdef Q_OS_UNIX
00137 Q_ASSERT(m_kdesuClient);
00138
00139 int success = m_kdesuClient->ping();
00140 if( success == -1 )
00141 {
00142 success = m_kdesuClient->startServer();
00143 if( success == -1 )
00144 return false;
00145 }
00146 return true;
00147 #else
00148 return false;
00149 #endif
00150 }
00151
00152 void SessionData::AuthDataList::registerAuthData( SessionData::AuthData* d )
00153 {
00154 if( !pingCacheDaemon() )
00155 return;
00156
00157 #ifdef Q_OS_UNIX
00158 bool ok;
00159 QByteArray ref_key = d->key + "-refcount";
00160 int count = m_kdesuClient->getVar(ref_key).toInt( &ok );
00161 if( ok )
00162 {
00163 QByteArray val;
00164 val.setNum( count+1 );
00165 m_kdesuClient->setVar( ref_key, val, 0, d->group );
00166 }
00167 else
00168 m_kdesuClient->setVar( ref_key, "1", 0, d->group );
00169 #endif
00170 }
00171
00172 void SessionData::AuthDataList::unregisterAuthData( SessionData::AuthData* d )
00173 {
00174 if ( !d || d->persist )
00175 return;
00176
00177 #ifdef Q_OS_UNIX
00178 bool ok;
00179 QByteArray ref_key = d->key + "-refcount";
00180 int count = m_kdesuClient->getVar( ref_key ).toInt( &ok );
00181 if ( ok )
00182 {
00183 if ( count > 1 )
00184 {
00185 QByteArray val;
00186 val.setNum(count-1);
00187 m_kdesuClient->setVar( ref_key, val, 0, d->group );
00188 }
00189 else
00190 {
00191 m_kdesuClient->delVars(d->key);
00192 }
00193 }
00194 #endif
00195 }
00196
00197 void SessionData::AuthDataList::purgeCachedData()
00198 {
00199 if ( !isEmpty() && pingCacheDaemon() )
00200 {
00201 QList<SessionData::AuthData*>::iterator it;
00202 for ( it = begin() ; it != end(); ++it )
00203 unregisterAuthData( (*it) );
00204 }
00205 }
00206 #endif
00207
00208
00209
00210 class SessionData::SessionDataPrivate
00211 {
00212 public:
00213 SessionDataPrivate() {
00214 useCookie = true;
00215 initDone = false;
00216 }
00217
00218 bool initDone;
00219 bool useCookie;
00220 QString charsets;
00221 QString language;
00222 };
00223
00224 SessionData::SessionData()
00225 :d(new SessionDataPrivate)
00226 {
00227
00228 }
00229
00230 SessionData::~SessionData()
00231 {
00232 delete d;
00233 }
00234
00235 void SessionData::configDataFor( MetaData &configData, const QString &proto,
00236 const QString & )
00237 {
00238 if ( (proto.startsWith("http", Qt::CaseInsensitive) ) ||
00239 (proto.startsWith("webdav", Qt::CaseInsensitive) ) )
00240 {
00241 if (!d->initDone)
00242 reset();
00243
00244
00245
00246
00247 if ( configData["Cookies"].isEmpty() )
00248 configData["Cookies"] = d->useCookie ? "true" : "false";
00249 if ( configData["Languages"].isEmpty() )
00250 configData["Languages"] = d->language;
00251 if ( configData["Charsets"].isEmpty() )
00252 configData["Charsets"] = d->charsets;
00253 if ( configData["CacheDir"].isEmpty() )
00254 configData["CacheDir"] = KGlobal::dirs()->saveLocation("cache", "http");
00255 if ( configData["UserAgent"].isEmpty() )
00256 {
00257 configData["UserAgent"] = KProtocolManager::defaultUserAgent();
00258 }
00259 }
00260 }
00261
00262 void SessionData::reset()
00263 {
00264 d->initDone = true;
00265
00266 d->useCookie = KSharedConfig::openConfig("kcookiejarrc", KConfig::NoGlobals)->
00267 group("Cookie Policy" ).
00268 readEntry("Cookies", true);
00269
00270 d->language = KProtocolManager::acceptLanguagesHeader();
00271
00272 d->charsets = QString::fromLatin1(QTextCodec::codecForLocale()->name()).toLower();
00273 KProtocolManager::reparseConfiguration();
00274 }
00275
00276 }