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

Plasma

  • plasma
configloader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2007 Aaron Seigo <aseigo@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "configloader.h"
21 #include "private/configloader_p.h"
22 
23 #include <QColor>
24 #include <QFont>
25 #include <QHash>
26 #include <QXmlContentHandler>
27 #include <QXmlInputSource>
28 #include <QXmlSimpleReader>
29 
30 #include <kdebug.h>
31 #include <kurl.h>
32 
33 namespace Plasma
34 {
35 class ConfigLoaderHandler : public QXmlDefaultHandler
36 {
37 public:
38  ConfigLoaderHandler(ConfigLoader *config, ConfigLoaderPrivate *d);
39  bool startElement(const QString &namespaceURI, const QString &localName,
40  const QString &qName, const QXmlAttributes &atts);
41  bool endElement(const QString &namespaceURI, const QString &localName,
42  const QString &qName);
43  bool characters(const QString &ch);
44 
45 private:
46  void addItem();
47  void resetState();
48 
49  ConfigLoader *m_config;
50  ConfigLoaderPrivate *d;
51  int m_min;
52  int m_max;
53  QString m_name;
54  QString m_key;
55  QString m_type;
56  QString m_label;
57  QString m_default;
58  QString m_cdata;
59  QString m_whatsThis;
60  KConfigSkeleton::ItemEnum::Choice m_choice;
61  QList<KConfigSkeleton::ItemEnum::Choice> m_enumChoices;
62  bool m_haveMin;
63  bool m_haveMax;
64  bool m_inChoice;
65 };
66 
67 void ConfigLoaderPrivate::parse(ConfigLoader *loader, QIODevice *xml)
68 {
69  QXmlInputSource source(xml);
70  QXmlSimpleReader reader;
71  ConfigLoaderHandler handler(loader, this);
72  reader.setContentHandler(&handler);
73  reader.parse(&source, false);
74 }
75 
76 ConfigLoaderHandler::ConfigLoaderHandler(ConfigLoader *config, ConfigLoaderPrivate *d)
77  : QXmlDefaultHandler(),
78  m_config(config),
79  d(d)
80 {
81  resetState();
82 }
83 
84 bool ConfigLoaderHandler::startElement(const QString &namespaceURI, const QString &localName,
85  const QString &qName, const QXmlAttributes &attrs)
86 {
87  Q_UNUSED(namespaceURI)
88  Q_UNUSED(qName)
89 
90 // kDebug() << "ConfigLoaderHandler::startElement(" << localName << qName;
91  int numAttrs = attrs.count();
92  QString tag = localName.toLower();
93  if (tag == "group") {
94  QString group;
95  for (int i = 0; i < numAttrs; ++i) {
96  QString name = attrs.localName(i).toLower();
97  if (name == "name") {
98  //kDebug() << "set group to" << attrs.value(i);
99  group = attrs.value(i);
100  }
101  }
102  if (group.isEmpty()) {
103  group = d->baseGroup;
104  } else {
105  d->groups.append(group);
106  if (!d->baseGroup.isEmpty()) {
107  group = d->baseGroup + '\x1d' + group;
108  }
109  }
110  m_config->setCurrentGroup(group);
111  } else if (tag == "entry") {
112  for (int i = 0; i < numAttrs; ++i) {
113  QString name = attrs.localName(i).toLower();
114  if (name == "name") {
115  m_name = attrs.value(i).trimmed();
116  } else if (name == "type") {
117  m_type = attrs.value(i).toLower();
118  } else if (name == "key") {
119  m_key = attrs.value(i).trimmed();
120  }
121  }
122  } else if (tag == "choice") {
123  m_choice.name.clear();
124  m_choice.label.clear();
125  m_choice.whatsThis.clear();
126  for (int i = 0; i < numAttrs; ++i) {
127  QString name = attrs.localName(i).toLower();
128  if (name == "name") {
129  m_choice.name = attrs.value(i);
130  }
131  }
132  m_inChoice = true;
133  }
134 
135  return true;
136 }
137 
138 bool ConfigLoaderHandler::characters(const QString &ch)
139 {
140  m_cdata.append(ch);
141  return true;
142 }
143 
144 bool ConfigLoaderHandler::endElement(const QString &namespaceURI,
145  const QString &localName, const QString &qName)
146 {
147  Q_UNUSED(namespaceURI)
148  Q_UNUSED(qName)
149 
150 // kDebug() << "ConfigLoaderHandler::endElement(" << localName << qName;
151  const QString tag = localName.toLower();
152  if (tag == "entry") {
153  addItem();
154  resetState();
155  } else if (tag == "label") {
156  if (m_inChoice) {
157  m_choice.label = m_cdata.trimmed();
158  } else {
159  m_label = m_cdata.trimmed();
160  }
161  } else if (tag == "whatsthis") {
162  if (m_inChoice) {
163  m_choice.whatsThis = m_cdata.trimmed();
164  } else {
165  m_whatsThis = m_cdata.trimmed();
166  }
167  } else if (tag == "default") {
168  m_default = m_cdata.trimmed();
169  } else if (tag == "min") {
170  m_min = m_cdata.toInt(&m_haveMin);
171  } else if (tag == "max") {
172  m_max = m_cdata.toInt(&m_haveMax);
173  } else if (tag == "choice") {
174  m_enumChoices.append(m_choice);
175  m_inChoice = false;
176  }
177 
178  m_cdata.clear();
179  return true;
180 }
181 
182 void ConfigLoaderHandler::addItem()
183 {
184  if (m_name.isEmpty()) {
185  if (m_key.isEmpty()) {
186  return;
187  }
188 
189  m_name = m_key;
190  }
191 
192  m_name.remove(' ');
193 
194  KConfigSkeletonItem *item = 0;
195 
196  if (m_type == "bool") {
197  bool defaultValue = m_default.toLower() == "true";
198  item = m_config->addItemBool(m_name, *d->newBool(), defaultValue, m_key);
199  } else if (m_type == "color") {
200  item = m_config->addItemColor(m_name, *d->newColor(), QColor(m_default), m_key);
201  } else if (m_type == "datetime") {
202  item = m_config->addItemDateTime(m_name, *d->newDateTime(),
203  QDateTime::fromString(m_default), m_key);
204  } else if (m_type == "enum") {
205  m_key = (m_key.isEmpty()) ? m_name : m_key;
206  KConfigSkeleton::ItemEnum *enumItem =
207  new KConfigSkeleton::ItemEnum(m_config->currentGroup(),
208  m_key, *d->newInt(),
209  m_enumChoices,
210  m_default.toUInt());
211  m_config->addItem(enumItem, m_name);
212  item = enumItem;
213  } else if (m_type == "font") {
214  item = m_config->addItemFont(m_name, *d->newFont(), QFont(m_default), m_key);
215  } else if (m_type == "int") {
216  KConfigSkeleton::ItemInt *intItem = m_config->addItemInt(m_name, *d->newInt(),
217  m_default.toInt(), m_key);
218 
219  if (m_haveMin) {
220  intItem->setMinValue(m_min);
221  }
222 
223  if (m_haveMax) {
224  intItem->setMaxValue(m_max);
225  }
226 
227  item = intItem;
228  } else if (m_type == "password") {
229  item = m_config->addItemPassword(m_name, *d->newString(), m_default, m_key);
230  } else if (m_type == "path") {
231  item = m_config->addItemPath(m_name, *d->newString(), m_default, m_key);
232  } else if (m_type == "string") {
233  item = m_config->addItemString(m_name, *d->newString(), m_default, m_key);
234  } else if (m_type == "stringlist") {
235  //FIXME: the split() is naive and will break on lists with ,'s in them
236  item = m_config->addItemStringList(m_name, *d->newStringList(),
237  m_default.split(','), m_key);
238  } else if (m_type == "uint") {
239  KConfigSkeleton::ItemUInt *uintItem =
240  m_config->addItemUInt(m_name, *d->newUint(), m_default.toUInt(), m_key);
241  if (m_haveMin) {
242  uintItem->setMinValue(m_min);
243  }
244  if (m_haveMax) {
245  uintItem->setMaxValue(m_max);
246  }
247  item = uintItem;
248  } else if (m_type == "url") {
249  m_key = (m_key.isEmpty()) ? m_name : m_key;
250  KConfigSkeleton::ItemUrl *urlItem =
251  new KConfigSkeleton::ItemUrl(m_config->currentGroup(),
252  m_key, *d->newUrl(),
253  m_default);
254  m_config->addItem(urlItem, m_name);
255  item = urlItem;
256  } else if (m_type == "double") {
257  KConfigSkeleton::ItemDouble *doubleItem = m_config->addItemDouble(m_name,
258  *d->newDouble(), m_default.toDouble(), m_key);
259  if (m_haveMin) {
260  doubleItem->setMinValue(m_min);
261  }
262  if (m_haveMax) {
263  doubleItem->setMaxValue(m_max);
264  }
265  item = doubleItem;
266  } else if (m_type == "intlist") {
267  QStringList tmpList = m_default.split(',');
268  QList<int> defaultList;
269  foreach (const QString &tmp, tmpList) {
270  defaultList.append(tmp.toInt());
271  }
272  item = m_config->addItemIntList(m_name, *d->newIntList(), defaultList, m_key);
273  } else if (m_type == "longlong") {
274  KConfigSkeleton::ItemLongLong *longlongItem = m_config->addItemLongLong(m_name,
275  *d->newLongLong(), m_default.toLongLong(), m_key);
276  if (m_haveMin) {
277  longlongItem->setMinValue(m_min);
278  }
279  if (m_haveMax) {
280  longlongItem->setMaxValue(m_max);
281  }
282  item = longlongItem;
283  /* No addItemPathList in KConfigSkeleton ?
284  } else if (m_type == "PathList") {
285  //FIXME: the split() is naive and will break on lists with ,'s in them
286  item = m_config->addItemPathList(m_name, *d->newStringList(), m_default.split(","), m_key);
287  */
288  } else if (m_type == "point") {
289  QPoint defaultPoint;
290  QStringList tmpList = m_default.split(',');
291  if (tmpList.size() >= 2) {
292  defaultPoint.setX(tmpList[0].toInt());
293  defaultPoint.setY(tmpList[1].toInt());
294  }
295  item = m_config->addItemPoint(m_name, *d->newPoint(), defaultPoint, m_key);
296  } else if (m_type == "rect") {
297  QRect defaultRect;
298  QStringList tmpList = m_default.split(',');
299  if (tmpList.size() >= 4) {
300  defaultRect.setCoords(tmpList[0].toInt(), tmpList[1].toInt(),
301  tmpList[2].toInt(), tmpList[3].toInt());
302  }
303  item = m_config->addItemRect(m_name, *d->newRect(), defaultRect, m_key);
304  } else if (m_type == "size") {
305  QSize defaultSize;
306  QStringList tmpList = m_default.split(',');
307  if (tmpList.size() >= 2) {
308  defaultSize.setWidth(tmpList[0].toInt());
309  defaultSize.setHeight(tmpList[1].toInt());
310  }
311  item = m_config->addItemSize(m_name, *d->newSize(), defaultSize, m_key);
312  } else if (m_type == "ulonglong") {
313  KConfigSkeleton::ItemULongLong *ulonglongItem =
314  m_config->addItemULongLong(m_name, *d->newULongLong(), m_default.toULongLong(), m_key);
315  if (m_haveMin) {
316  ulonglongItem->setMinValue(m_min);
317  }
318  if (m_haveMax) {
319  ulonglongItem->setMaxValue(m_max);
320  }
321  item = ulonglongItem;
322  /* No addItemUrlList in KConfigSkeleton ?
323  } else if (m_type == "urllist") {
324  //FIXME: the split() is naive and will break on lists with ,'s in them
325  QStringList tmpList = m_default.split(",");
326  KUrl::List defaultList;
327  foreach (const QString& tmp, tmpList) {
328  defaultList.append(KUrl(tmp));
329  }
330  item = m_config->addItemUrlList(m_name, *d->newUrlList(), defaultList, m_key);*/
331  }
332 
333  if (item) {
334  item->setLabel(m_label);
335  item->setWhatsThis(m_whatsThis);
336  d->keysToNames.insert(item->group() + item->key(), item->name());
337  }
338 }
339 
340 void ConfigLoaderHandler::resetState()
341 {
342  m_haveMin = false;
343  m_min = 0;
344  m_haveMax = false;
345  m_max = 0;
346  m_name.clear();
347  m_type.clear();
348  m_label.clear();
349  m_default.clear();
350  m_key.clear();
351  m_whatsThis.clear();
352  m_enumChoices.clear();
353  m_inChoice = false;
354 }
355 
356 ConfigLoader::ConfigLoader(const QString &configFile, QIODevice *xml, QObject *parent)
357  : KConfigSkeleton(configFile, parent),
358  d(new ConfigLoaderPrivate)
359 {
360  d->parse(this, xml);
361 }
362 
363 ConfigLoader::ConfigLoader(KSharedConfigPtr config, QIODevice *xml, QObject *parent)
364  : KConfigSkeleton(config, parent),
365  d(new ConfigLoaderPrivate)
366 {
367  d->parse(this, xml);
368 }
369 
370 //FIXME: obviously this is broken and should be using the group as the root,
371 // but KConfigSkeleton does not currently support this. it will eventually though,
372 // at which point this can be addressed properly
373 ConfigLoader::ConfigLoader(const KConfigGroup *config, QIODevice *xml, QObject *parent)
374  : KConfigSkeleton(KSharedConfig::openConfig(config->config()->name()), parent),
375  d(new ConfigLoaderPrivate)
376 {
377  KConfigGroup group = config->parent();
378  d->baseGroup = config->name();
379  while (group.isValid() && group.name() != "<default>") {
380  d->baseGroup = group.name() + '\x1d' + d->baseGroup;
381  group = group.parent();
382  }
383  d->parse(this, xml);
384 }
385 
386 ConfigLoader::~ConfigLoader()
387 {
388  delete d;
389 }
390 
391 KConfigSkeletonItem *ConfigLoader::findItem(const QString &group, const QString &key)
392 {
393  return KConfigSkeleton::findItem(d->keysToNames[group + key]);
394 }
395 
396 KConfigSkeletonItem *ConfigLoader::findItemByName(const QString &name)
397 {
398  return KConfigSkeleton::findItem(name);
399 }
400 
401 QVariant ConfigLoader::property(const QString &name)
402 {
403  KConfigSkeletonItem *item = KConfigSkeleton::findItem(name);
404 
405  if (item) {
406  return item->property();
407  }
408 
409  return QVariant();
410 }
411 
412 bool ConfigLoader::hasGroup(const QString &group) const
413 {
414  return d->groups.contains(group);
415 }
416 
417 QStringList ConfigLoader::groupList() const
418 {
419  return d->groups;
420 }
421 
422 void ConfigLoader::usrWriteConfig()
423 {
424  if (d->saveDefaults) {
425  KConfigSkeletonItem::List itemList = items();
426  for(int i = 0; i < itemList.size(); i++) {
427  KConfigGroup cg(config(), itemList.at(i)->group());
428  cg.writeEntry(itemList.at(i)->key(), "");
429  }
430  }
431 }
432 
433 } // Plasma namespace
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 23 2013 21:58:13 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

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

kdelibs-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 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
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