KIO

slaveconfig.cpp
1 // -*- c++ -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (c) 2001 Waldo Bastian <[email protected]>
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 <QHash>
24 #include <QThreadStorage>
25 
26 #include <KConfig>
27 #include <KSharedConfig>
28 #include <kprotocolinfo.h>
29 #include <kprotocolmanager.h>
30 
31 using namespace KIO;
32 
33 namespace KIO
34 {
35 
36 class SlaveConfigProtocol
37 {
38 public:
39  SlaveConfigProtocol() {}
40  ~SlaveConfigProtocol()
41  {
42  delete configFile;
43  }
44 
45  SlaveConfigProtocol(const SlaveConfigProtocol &) = delete;
46  SlaveConfigProtocol &operator=(const SlaveConfigProtocol &) = delete;
47 
48 public:
49  MetaData global;
51  KConfig *configFile;
52 };
53 
54 static void readConfig(KConfig *config, const QString &group, MetaData *metaData)
55 {
56  *metaData += config->entryMap(group);
57 }
58 
59 class SlaveConfigPrivate
60 {
61 public:
62  void readGlobalConfig();
63  SlaveConfigProtocol *readProtocolConfig(const QString &_protocol);
64  SlaveConfigProtocol *findProtocolConfig(const QString &_protocol);
65  void readConfigProtocolHost(const QString &_protocol, SlaveConfigProtocol *scp, const QString &host);
66 public:
67  MetaData global;
69 };
70 
71 void SlaveConfigPrivate::readGlobalConfig()
72 {
73  global.clear();
74  // Read stuff...
75  readConfig(KSharedConfig::openConfig().data(), QStringLiteral("Socks"), &global); // Socks settings.
76  global += KProtocolManager::entryMap(QStringLiteral("<default>"));
77 }
78 
79 SlaveConfigProtocol *SlaveConfigPrivate::readProtocolConfig(const QString &_protocol)
80 {
81  SlaveConfigProtocol *scp = protocol.value(_protocol, nullptr);
82  if (!scp) {
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, QStringLiteral("<default>"), &(scp->global));
90  return scp;
91 }
92 
93 SlaveConfigProtocol *SlaveConfigPrivate::findProtocolConfig(const QString &_protocol)
94 {
95  SlaveConfigProtocol *scp = protocol.value(_protocol, nullptr);
96  if (!scp) {
97  scp = readProtocolConfig(_protocol);
98  }
99  return scp;
100 }
101 
102 void SlaveConfigPrivate::readConfigProtocolHost(const QString &, SlaveConfigProtocol *scp, const QString &host)
103 {
104  MetaData metaData;
105  scp->host.insert(host, metaData);
106 
107  // Read stuff
108  // Break host into domains
109  QString domain = host;
110 
111  if (!domain.contains(QLatin1Char('.'))) {
112  // Host without domain.
113  if (scp->configFile->hasGroup("<local>")) {
114  readConfig(scp->configFile, QStringLiteral("<local>"), &metaData);
115  scp->host.insert(host, metaData);
116  }
117  }
118 
119  int pos = 0;
120  do {
121  pos = host.lastIndexOf(QLatin1Char('.'), pos - 1);
122 
123  if (pos < 0) {
124  domain = host;
125  } else {
126  domain = host.mid(pos + 1);
127  }
128 
129  if (scp->configFile->hasGroup(domain)) {
130  readConfig(scp->configFile, domain.toLower(), &metaData);
131  scp->host.insert(host, metaData);
132  }
133  } while (pos > 0);
134 }
135 
136 class SlaveConfigSingleton
137 {
138 public:
139  SlaveConfig instance;
140 };
141 
142 template <typename T>
143 T * perThreadGlobalStatic()
144 {
145  static QThreadStorage<T *> s_storage;
146  if (!s_storage.hasLocalData()) {
147  s_storage.setLocalData(new T);
148  }
149  return s_storage.localData();
150 }
151 //Q_GLOBAL_STATIC(SlaveConfigSingleton, _self)
152 SlaveConfigSingleton *_self() { return perThreadGlobalStatic<SlaveConfigSingleton>(); }
153 
154 
155 SlaveConfig *SlaveConfig::self()
156 {
157  return &_self()->instance;
158 }
159 
160 SlaveConfig::SlaveConfig()
161  : d(new SlaveConfigPrivate)
162 {
163  d->readGlobalConfig();
164 }
165 
166 SlaveConfig::~SlaveConfig()
167 {
168  qDeleteAll(d->protocol);
169  delete d;
170 }
171 
172 void SlaveConfig::setConfigData(const QString &protocol,
173  const QString &host,
174  const QString &key,
175  const QString &value)
176 {
178  config.insert(key, value);
179  setConfigData(protocol, host, config);
180 }
181 
182 void SlaveConfig::setConfigData(const QString &protocol, const QString &host, const MetaData &config)
183 {
184  if (protocol.isEmpty()) {
185  d->global += config;
186  } else {
187  SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
188  if (host.isEmpty()) {
189  scp->global += config;
190  } else {
191  if (!scp->host.contains(host)) {
192  d->readConfigProtocolHost(protocol, scp, host);
193  }
194 
195  MetaData hostConfig = scp->host.value(host);
196  hostConfig += config;
197  scp->host.insert(host, hostConfig);
198  }
199  }
200 }
201 
202 MetaData SlaveConfig::configData(const QString &protocol, const QString &host)
203 {
204  MetaData config = d->global;
205  SlaveConfigProtocol *scp = d->findProtocolConfig(protocol);
206  config += scp->global;
207  if (host.isEmpty()) {
208  return config;
209  }
210 
211  if (!scp->host.contains(host)) {
212  d->readConfigProtocolHost(protocol, scp, host);
213  emit configNeeded(protocol, host);
214  }
215  MetaData hostConfig = scp->host.value(host);
216  config += hostConfig;
217 
218  return config;
219 }
220 
221 QString SlaveConfig::configData(const QString &protocol, const QString &host, const QString &key)
222 {
223  return configData(protocol, host).value(key);
224 }
225 
227 {
228  qDeleteAll(d->protocol);
229  d->protocol.clear();
230  d->readGlobalConfig();
231 }
232 
233 }
234 
MetaData configData(const QString &protocol, const QString &host)
Query slave configuration for slaves of type protocol when dealing with host.
A namespace for KIO globals.
Definition: authinfo.h:34
static QString config(const QString &protocol)
Returns the name of the config file associated with the specified protocol.
void setConfigData(const QString &protocol, const QString &host, const QString &key, const QString &value)
Configure slaves of type protocol by setting key to value.
MetaData is a simple map of key/value strings.
Definition: metadata.h:34
void reset()
Undo any changes made by calls to setConfigData.
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QMap< QString, QString > entryMap(const QString &aGroup=QString()) const
void setLocalData(T data)
bool hasLocalData() const const
KConfigGroup * config()
Returns a configuration object to query config/meta-data information from.
Definition: slavebase.cpp:440
bool isEmpty() const const
void clear()
QString toLower() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void readConfig()
QString mid(int position, int n) const const
SlaveConfig.
Definition: slaveconfig.h:51
QMap::iterator insert(const Key &key, const T &value)
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:58:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.