• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kio

slaveconfig.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
00005  *
00006  * $Id: slaveconfig.cpp 465272 2005-09-29 09:47:40Z mueller $
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License version 2 as published by the Free Software Foundation.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  *  Boston, MA 02110-1301, USA.
00021  **/
00022 
00023 #include <assert.h>
00024 
00025 #include <qdict.h>
00026 
00027 #include <kconfig.h>
00028 #include <kstaticdeleter.h>
00029 #include <kprotocolinfo.h>
00030 #include <kprotocolmanager.h>
00031 
00032 #include "slaveconfig.h"
00033 
00034 using namespace KIO;
00035 
00036 namespace KIO {
00037 
00038 class SlaveConfigProtocol
00039 {
00040 public:
00041   SlaveConfigProtocol() { host.setAutoDelete(true); }
00042   ~SlaveConfigProtocol()
00043   {
00044      delete configFile;
00045   }
00046 
00047 public:
00048   MetaData global;
00049   QDict<MetaData> host;
00050   KConfig *configFile;
00051 };
00052 
00053 static void readConfig(KConfig *config, const QString & group, MetaData *metaData)
00054 {
00055    *metaData += config->entryMap(group);
00056 }
00057 
00058 class SlaveConfigPrivate
00059 {
00060   public:
00061      void readGlobalConfig();
00062      SlaveConfigProtocol *readProtocolConfig(const QString &_protocol);
00063      SlaveConfigProtocol *findProtocolConfig(const QString &_protocol);
00064      void readConfigProtocolHost(const QString &_protocol, SlaveConfigProtocol *scp, const QString &host);
00065   public:
00066      MetaData global;
00067      QDict<SlaveConfigProtocol> protocol;
00068 };
00069 
00070 void SlaveConfigPrivate::readGlobalConfig()
00071 {
00072    global.clear();
00073    // Read stuff...
00074    KConfig *config = KProtocolManager::config();
00075    readConfig(KGlobal::config(), "Socks", &global); // Socks settings.
00076    if ( config )
00077        readConfig(config, "<default>", &global);
00078 }
00079 
00080 SlaveConfigProtocol* SlaveConfigPrivate::readProtocolConfig(const QString &_protocol)
00081 {
00082    SlaveConfigProtocol *scp = protocol.find(_protocol);
00083    if (!scp)
00084    {
00085       QString filename = KProtocolInfo::config(_protocol);
00086       scp = new SlaveConfigProtocol;
00087       scp->configFile = new KConfig(filename, true, false);
00088       protocol.insert(_protocol, scp);
00089    }
00090    // Read global stuff...
00091    readConfig(scp->configFile, "<default>", &(scp->global));
00092    return scp;
00093 }
00094 
00095 SlaveConfigProtocol* SlaveConfigPrivate::findProtocolConfig(const QString &_protocol)
00096 {
00097    SlaveConfigProtocol *scp = protocol.find(_protocol);
00098    if (!scp)
00099       scp = readProtocolConfig(_protocol);
00100    return scp;
00101 }
00102 
00103 void SlaveConfigPrivate::readConfigProtocolHost(const QString &, SlaveConfigProtocol *scp, const QString &host)
00104 {
00105    MetaData *metaData = new MetaData;
00106    scp->host.replace(host, metaData);
00107 
00108    // Read stuff
00109    // Break host into domains
00110    QString domain = host;
00111 
00112    if (!domain.contains('.'))
00113    {
00114       // Host without domain.
00115       if (scp->configFile->hasGroup("<local>"))
00116          readConfig(scp->configFile, "<local>", metaData);
00117    }
00118 
00119    int pos = 0;
00120    do
00121    {
00122       pos = host.findRev('.', pos-1);
00123 
00124       if (pos < 0)
00125         domain = host;
00126       else
00127         domain = host.mid(pos+1);
00128 
00129       if (scp->configFile->hasGroup(domain))
00130          readConfig(scp->configFile, domain.lower(), metaData);
00131    }
00132    while (pos > 0);
00133 }
00134 
00135 
00136 SlaveConfig *SlaveConfig::_self = 0;
00137 static KStaticDeleter<SlaveConfig> slaveconfigsd;
00138 
00139 SlaveConfig *SlaveConfig::self()
00140 {
00141    if (!_self)
00142       _self = slaveconfigsd.setObject(_self, new SlaveConfig);
00143    return _self;
00144 }
00145 
00146 SlaveConfig::SlaveConfig()
00147 {
00148   d = new SlaveConfigPrivate;
00149   d->protocol.setAutoDelete(true);
00150   d->readGlobalConfig();
00151 }
00152 
00153 SlaveConfig::~SlaveConfig()
00154 {
00155    delete d; d = 0;
00156    _self = 0;
00157 }
00158 
00159 void SlaveConfig::setConfigData(const QString &protocol,
00160                                 const QString &host,
00161                                 const QString &key,
00162                                 const QString &value )
00163 {
00164    MetaData config;
00165    config.insert(key, value);
00166    setConfigData(protocol, host, config);
00167 }
00168 
00169 void SlaveConfig::setConfigData(const QString &protocol, const QString &host, const MetaData &config )
00170 {
00171    if (protocol.isEmpty())
00172       d->global += config;
00173    else {
00174       SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
00175       if (host.isEmpty())
00176       {
00177          scp->global += config;
00178       }
00179       else
00180       {
00181          MetaData *hostConfig = scp->host.find(host);
00182          if (!hostConfig)
00183          {
00184             d->readConfigProtocolHost(protocol, scp, host);
00185             hostConfig = scp->host.find(host);
00186             assert(hostConfig);
00187          }
00188          *hostConfig += config;
00189       }
00190    }
00191 }
00192 
00193 MetaData SlaveConfig::configData(const QString &protocol, const QString &host)
00194 {
00195    MetaData config = d->global;
00196    SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
00197    config += scp->global;
00198    if (host.isEmpty())
00199       return config;
00200    MetaData *hostConfig = scp->host.find(host);
00201    if (!hostConfig)
00202    {
00203       d->readConfigProtocolHost(protocol, scp, host);
00204       emit configNeeded(protocol, host);
00205       hostConfig = scp->host.find(host);
00206       assert(hostConfig);
00207    }
00208    config += *hostConfig;
00209    return config;
00210 }
00211 
00212 QString SlaveConfig::configData(const QString &protocol, const QString &host, const QString &key)
00213 {
00214    return configData(protocol, host)[key];
00215 }
00216 
00217 void SlaveConfig::reset()
00218 {
00219    d->protocol.clear();
00220    d->readGlobalConfig();
00221 }
00222 
00223 }
00224 
00225 #include "slaveconfig.moc"

kio

Skip menu "kio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal