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 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License version 2 as published by the Free Software Foundation. 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 Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 **/ 00020 00021 #include "slaveconfig.h" 00022 00023 #include <assert.h> 00024 00025 #include <QtCore/QHash> 00026 00027 #include <kconfig.h> 00028 #include <ksharedconfig.h> 00029 #include <kprotocolinfo.h> 00030 #include <kprotocolmanager.h> 00031 00032 using namespace KIO; 00033 00034 namespace KIO { 00035 00036 class SlaveConfigProtocol 00037 { 00038 public: 00039 SlaveConfigProtocol() {} 00040 ~SlaveConfigProtocol() 00041 { 00042 delete configFile; 00043 } 00044 00045 public: 00046 MetaData global; 00047 QHash<QString, MetaData> host; 00048 KConfig *configFile; 00049 }; 00050 00051 static void readConfig(KConfig *config, const QString & group, MetaData *metaData) 00052 { 00053 *metaData += config->entryMap(group); 00054 } 00055 00056 class SlaveConfigPrivate 00057 { 00058 public: 00059 void readGlobalConfig(); 00060 SlaveConfigProtocol *readProtocolConfig(const QString &_protocol); 00061 SlaveConfigProtocol *findProtocolConfig(const QString &_protocol); 00062 void readConfigProtocolHost(const QString &_protocol, SlaveConfigProtocol *scp, const QString &host); 00063 public: 00064 MetaData global; 00065 QHash<QString, SlaveConfigProtocol*> protocol; 00066 }; 00067 00068 void SlaveConfigPrivate::readGlobalConfig() 00069 { 00070 global.clear(); 00071 // Read stuff... 00072 KSharedConfig::Ptr config = KProtocolManager::config(); 00073 readConfig(KGlobal::config().data(), "Socks", &global); // Socks settings. 00074 if ( config ) 00075 readConfig(config.data(), "<default>", &global); 00076 } 00077 00078 SlaveConfigProtocol* SlaveConfigPrivate::readProtocolConfig(const QString &_protocol) 00079 { 00080 SlaveConfigProtocol *scp = protocol.value(_protocol,0); 00081 if (!scp) 00082 { 00083 QString filename = KProtocolInfo::config(_protocol); 00084 scp = new SlaveConfigProtocol; 00085 scp->configFile = new KConfig(filename, KConfig::NoGlobals); 00086 protocol.insert(_protocol, scp); 00087 } 00088 // Read global stuff... 00089 readConfig(scp->configFile, "<default>", &(scp->global)); 00090 return scp; 00091 } 00092 00093 SlaveConfigProtocol* SlaveConfigPrivate::findProtocolConfig(const QString &_protocol) 00094 { 00095 SlaveConfigProtocol *scp = protocol.value(_protocol,0); 00096 if (!scp) 00097 scp = readProtocolConfig(_protocol); 00098 return scp; 00099 } 00100 00101 void SlaveConfigPrivate::readConfigProtocolHost(const QString &, SlaveConfigProtocol *scp, const QString &host) 00102 { 00103 MetaData metaData; 00104 scp->host.insert(host, metaData); 00105 00106 // Read stuff 00107 // Break host into domains 00108 QString domain = host; 00109 00110 if (!domain.contains('.')) 00111 { 00112 // Host without domain. 00113 if (scp->configFile->hasGroup("<local>")) { 00114 readConfig(scp->configFile, "<local>", &metaData); 00115 scp->host.insert(host, metaData); 00116 } 00117 } 00118 00119 int pos = 0; 00120 do 00121 { 00122 pos = host.lastIndexOf('.', 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.toLower(), &metaData); 00131 scp->host.insert(host, metaData); 00132 } 00133 } 00134 while (pos > 0); 00135 } 00136 00137 SlaveConfig *SlaveConfig::self() 00138 { 00139 K_GLOBAL_STATIC(SlaveConfig, _self) 00140 return _self; 00141 } 00142 00143 SlaveConfig::SlaveConfig() 00144 :d(new SlaveConfigPrivate) 00145 { 00146 d->readGlobalConfig(); 00147 } 00148 00149 SlaveConfig::~SlaveConfig() 00150 { 00151 qDeleteAll(d->protocol); 00152 delete d; 00153 } 00154 00155 void SlaveConfig::setConfigData(const QString &protocol, 00156 const QString &host, 00157 const QString &key, 00158 const QString &value ) 00159 { 00160 MetaData config; 00161 config.insert(key, value); 00162 setConfigData(protocol, host, config); 00163 } 00164 00165 void SlaveConfig::setConfigData(const QString &protocol, const QString &host, const MetaData &config ) 00166 { 00167 if (protocol.isEmpty()) 00168 d->global += config; 00169 else { 00170 SlaveConfigProtocol *scp = d->findProtocolConfig(protocol); 00171 if (host.isEmpty()) 00172 { 00173 scp->global += config; 00174 } 00175 else 00176 { 00177 if (!scp->host.contains(host)) 00178 d->readConfigProtocolHost(protocol, scp, host); 00179 00180 MetaData hostConfig = scp->host.value(host); 00181 hostConfig += config; 00182 scp->host.insert(host, hostConfig); 00183 } 00184 } 00185 } 00186 00187 MetaData SlaveConfig::configData(const QString &protocol, const QString &host) 00188 { 00189 MetaData config = d->global; 00190 SlaveConfigProtocol *scp = d->findProtocolConfig(protocol); 00191 config += scp->global; 00192 if (host.isEmpty()) 00193 return config; 00194 00195 if (!scp->host.contains(host)) 00196 { 00197 d->readConfigProtocolHost(protocol, scp, host); 00198 emit configNeeded(protocol, host); 00199 } 00200 MetaData hostConfig = scp->host.value(host); 00201 config += hostConfig; 00202 00203 return config; 00204 } 00205 00206 QString SlaveConfig::configData(const QString &protocol, const QString &host, const QString &key) 00207 { 00208 return configData(protocol, host)[key]; 00209 } 00210 00211 void SlaveConfig::reset() 00212 { 00213 qDeleteAll(d->protocol); 00214 d->protocol.clear(); 00215 d->readGlobalConfig(); 00216 } 00217 00218 } 00219 00220 #include "slaveconfig.moc"
KDE 4.6 API Reference