• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kio
slaveconfig.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  **/
20 
21 #include "slaveconfig.h"
22 
23 #include <assert.h>
24 
25 #include <QtCore/QHash>
26 
27 #include <kconfig.h>
28 #include <ksharedconfig.h>
29 #include <kprotocolinfo.h>
30 #include <kprotocolmanager.h>
31 
32 using namespace KIO;
33 
34 namespace KIO {
35 
36 class SlaveConfigProtocol
37 {
38 public:
39  SlaveConfigProtocol() {}
40  ~SlaveConfigProtocol()
41  {
42  delete configFile;
43  }
44 
45 public:
46  MetaData global;
47  QHash<QString, MetaData> host;
48  KConfig *configFile;
49 };
50 
51 static void readConfig(KConfig *config, const QString & group, MetaData *metaData)
52 {
53  *metaData += config->entryMap(group);
54 }
55 
56 class SlaveConfigPrivate
57 {
58  public:
59  void readGlobalConfig();
60  SlaveConfigProtocol *readProtocolConfig(const QString &_protocol);
61  SlaveConfigProtocol *findProtocolConfig(const QString &_protocol);
62  void readConfigProtocolHost(const QString &_protocol, SlaveConfigProtocol *scp, const QString &host);
63  public:
64  MetaData global;
65  QHash<QString, SlaveConfigProtocol*> protocol;
66 };
67 
68 void SlaveConfigPrivate::readGlobalConfig()
69 {
70  global.clear();
71  // Read stuff...
72  KSharedConfig::Ptr config = KProtocolManager::config();
73  readConfig(KGlobal::config().data(), "Socks", &global); // Socks settings.
74  if ( config )
75  readConfig(config.data(), "<default>", &global);
76 }
77 
78 SlaveConfigProtocol* SlaveConfigPrivate::readProtocolConfig(const QString &_protocol)
79 {
80  SlaveConfigProtocol *scp = protocol.value(_protocol,0);
81  if (!scp)
82  {
83  QString filename = KProtocolInfo::config(_protocol);
84  scp = new SlaveConfigProtocol;
85  scp->configFile = new KConfig(filename, KConfig::NoGlobals);
86  protocol.insert(_protocol, scp);
87  }
88  // Read global stuff...
89  readConfig(scp->configFile, "<default>", &(scp->global));
90  return scp;
91 }
92 
93 SlaveConfigProtocol* SlaveConfigPrivate::findProtocolConfig(const QString &_protocol)
94 {
95  SlaveConfigProtocol *scp = protocol.value(_protocol,0);
96  if (!scp)
97  scp = readProtocolConfig(_protocol);
98  return scp;
99 }
100 
101 void SlaveConfigPrivate::readConfigProtocolHost(const QString &, SlaveConfigProtocol *scp, const QString &host)
102 {
103  MetaData metaData;
104  scp->host.insert(host, metaData);
105 
106  // Read stuff
107  // Break host into domains
108  QString domain = host;
109 
110  if (!domain.contains('.'))
111  {
112  // Host without domain.
113  if (scp->configFile->hasGroup("<local>")) {
114  readConfig(scp->configFile, "<local>", &metaData);
115  scp->host.insert(host, metaData);
116  }
117  }
118 
119  int pos = 0;
120  do
121  {
122  pos = host.lastIndexOf('.', pos-1);
123 
124  if (pos < 0)
125  domain = host;
126  else
127  domain = host.mid(pos+1);
128 
129  if (scp->configFile->hasGroup(domain)) {
130  readConfig(scp->configFile, domain.toLower(), &metaData);
131  scp->host.insert(host, metaData);
132  }
133  }
134  while (pos > 0);
135 }
136 
137 SlaveConfig *SlaveConfig::self()
138 {
139  K_GLOBAL_STATIC(SlaveConfig, _self)
140  return _self;
141 }
142 
143 SlaveConfig::SlaveConfig()
144  :d(new SlaveConfigPrivate)
145 {
146  d->readGlobalConfig();
147 }
148 
149 SlaveConfig::~SlaveConfig()
150 {
151  qDeleteAll(d->protocol);
152  delete d;
153 }
154 
155 void SlaveConfig::setConfigData(const QString &protocol,
156  const QString &host,
157  const QString &key,
158  const QString &value )
159 {
160  MetaData config;
161  config.insert(key, value);
162  setConfigData(protocol, host, config);
163 }
164 
165 void SlaveConfig::setConfigData(const QString &protocol, const QString &host, const MetaData &config )
166 {
167  if (protocol.isEmpty())
168  d->global += config;
169  else {
170  SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
171  if (host.isEmpty())
172  {
173  scp->global += config;
174  }
175  else
176  {
177  if (!scp->host.contains(host))
178  d->readConfigProtocolHost(protocol, scp, host);
179 
180  MetaData hostConfig = scp->host.value(host);
181  hostConfig += config;
182  scp->host.insert(host, hostConfig);
183  }
184  }
185 }
186 
187 MetaData SlaveConfig::configData(const QString &protocol, const QString &host)
188 {
189  MetaData config = d->global;
190  SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
191  config += scp->global;
192  if (host.isEmpty())
193  return config;
194 
195  if (!scp->host.contains(host))
196  {
197  d->readConfigProtocolHost(protocol, scp, host);
198  emit configNeeded(protocol, host);
199  }
200  MetaData hostConfig = scp->host.value(host);
201  config += hostConfig;
202 
203  return config;
204 }
205 
206 QString SlaveConfig::configData(const QString &protocol, const QString &host, const QString &key)
207 {
208  return configData(protocol, host)[key];
209 }
210 
211 void SlaveConfig::reset()
212 {
213  qDeleteAll(d->protocol);
214  d->protocol.clear();
215  d->readGlobalConfig();
216 }
217 
218 }
219 
220 #include "slaveconfig.moc"
KSharedPtr< KSharedConfig >
KIO::SlaveConfig::configData
MetaData configData(const QString &protocol, const QString &host)
Query slave configuration for slaves of type protocol when dealing with host.
Definition: slaveconfig.cpp:187
KSharedPtr::data
T * data()
KProtocolInfo::config
static QString config(const QString &protocol)
KIO::SlaveConfig::setConfigData
void setConfigData(const QString &protocol, const QString &host, const QString &key, const QString &value)
Configure slaves of type protocol by setting key to value.
Definition: slaveconfig.cpp:155
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kconfig.h
KIO::SlaveConfig::SlaveConfig
SlaveConfig()
Definition: slaveconfig.cpp:143
KIO::MetaData
MetaData is a simple map of key/value strings.
Definition: global.h:396
KIO::SlaveConfig::reset
void reset()
Undo any changes made by calls to setConfigData.
Definition: slaveconfig.cpp:211
config
KSharedConfigPtr config()
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
kprotocolmanager.h
KIO::readConfig
static void readConfig(KConfig *config, const QString &group, MetaData *metaData)
Definition: slaveconfig.cpp:51
KConfig::entryMap
QMap< QString, QString > entryMap(const QString &aGroup=QString()) const
KConfig::NoGlobals
QHash
QString::isEmpty
bool isEmpty() const
ksharedconfig.h
QString
QHash::clear
void clear()
QString::toLower
QString toLower() const
kprotocolinfo.h
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KConfig
QString::mid
QString mid(int position, int n) const
KIO::SlaveConfig
SlaveConfig.
Definition: slaveconfig.h:47
KIO::SlaveConfig::configNeeded
void configNeeded(const QString &protocol, const QString &host)
This signal is raised when a slave of type protocol deals with host for the first time...
QMap::insert
iterator insert(const Key &key, const T &value)
slaveconfig.h
KIO::SlaveConfig::self
static SlaveConfig * self()
Definition: slaveconfig.cpp:137
KIO::SlaveConfig::~SlaveConfig
~SlaveConfig()
Definition: slaveconfig.cpp:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal