21 #include "private/configloader_p.h"
26 #include <QXmlContentHandler>
27 #include <QXmlInputSource>
28 #include <QXmlSimpleReader>
35 class ConfigLoaderHandler :
public QXmlDefaultHandler
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);
50 ConfigLoaderPrivate *d;
60 KConfigSkeleton::ItemEnum::Choice m_choice;
61 QList<KConfigSkeleton::ItemEnum::Choice> m_enumChoices;
67 void ConfigLoaderPrivate::parse(
ConfigLoader *loader, QIODevice *xml)
69 QXmlInputSource source(xml);
70 QXmlSimpleReader reader;
71 ConfigLoaderHandler handler(loader,
this);
72 reader.setContentHandler(&handler);
73 reader.parse(&source,
false);
76 ConfigLoaderHandler::ConfigLoaderHandler(
ConfigLoader *config, ConfigLoaderPrivate *d)
77 : QXmlDefaultHandler(),
84 bool ConfigLoaderHandler::startElement(
const QString &namespaceURI,
const QString &localName,
85 const QString &qName,
const QXmlAttributes &attrs)
87 Q_UNUSED(namespaceURI)
91 int numAttrs = attrs.count();
92 QString tag = localName.toLower();
95 for (
int i = 0; i < numAttrs; ++i) {
96 QString name = attrs.localName(i).toLower();
99 group = attrs.value(i);
102 if (group.isEmpty()) {
103 group = d->baseGroup;
105 d->groups.append(group);
106 if (!d->baseGroup.isEmpty()) {
107 group = d->baseGroup +
'\x1d' + group;
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();
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);
138 bool ConfigLoaderHandler::characters(
const QString &ch)
144 bool ConfigLoaderHandler::endElement(
const QString &namespaceURI,
145 const QString &localName,
const QString &qName)
147 Q_UNUSED(namespaceURI)
151 const QString tag = localName.toLower();
152 if (tag == "entry") {
155 }
else if (tag ==
"label") {
157 m_choice.label = m_cdata.trimmed();
159 m_label = m_cdata.trimmed();
161 }
else if (tag ==
"whatsthis") {
163 m_choice.whatsThis = m_cdata.trimmed();
165 m_whatsThis = m_cdata.trimmed();
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);
182 void ConfigLoaderHandler::addItem()
184 if (m_name.isEmpty()) {
185 if (m_key.isEmpty()) {
194 KConfigSkeletonItem *item = 0;
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") {
202 if (m_default.count(
',') == 3) {
203 const QStringList values = m_default.split(
',');
205 color = QColor(values.at(0).toInt(), values.at(1).toInt(), values.at(2).toInt(), values.at(3).toInt());
207 color = QColor(m_default);
210 item = m_config->addItemColor(m_name, *d->newColor(), color, m_key);
211 }
else if (m_type ==
"datetime") {
212 item = m_config->addItemDateTime(m_name, *d->newDateTime(),
213 QDateTime::fromString(m_default), m_key);
214 }
else if (m_type ==
"enum") {
215 m_key = (m_key.isEmpty()) ? m_name : m_key;
216 KConfigSkeleton::ItemEnum *enumItem =
217 new KConfigSkeleton::ItemEnum(m_config->currentGroup(),
221 m_config->addItem(enumItem, m_name);
223 }
else if (m_type ==
"font") {
224 item = m_config->addItemFont(m_name, *d->newFont(), QFont(m_default), m_key);
225 }
else if (m_type ==
"int") {
226 KConfigSkeleton::ItemInt *intItem = m_config->addItemInt(m_name, *d->newInt(),
227 m_default.toInt(), m_key);
230 intItem->setMinValue(m_min);
234 intItem->setMaxValue(m_max);
238 }
else if (m_type ==
"password") {
239 item = m_config->addItemPassword(m_name, *d->newString(), m_default, m_key);
240 }
else if (m_type ==
"path") {
241 item = m_config->addItemPath(m_name, *d->newString(), m_default, m_key);
242 }
else if (m_type ==
"string") {
243 item = m_config->addItemString(m_name, *d->newString(), m_default, m_key);
244 }
else if (m_type ==
"stringlist") {
246 item = m_config->addItemStringList(m_name, *d->newStringList(),
247 m_default.split(
','), m_key);
248 }
else if (m_type ==
"uint") {
249 KConfigSkeleton::ItemUInt *uintItem =
250 m_config->addItemUInt(m_name, *d->newUint(), m_default.toUInt(), m_key);
252 uintItem->setMinValue(m_min);
255 uintItem->setMaxValue(m_max);
258 }
else if (m_type ==
"url") {
259 m_key = (m_key.isEmpty()) ? m_name : m_key;
260 KConfigSkeleton::ItemUrl *urlItem =
261 new KConfigSkeleton::ItemUrl(m_config->currentGroup(),
264 m_config->addItem(urlItem, m_name);
266 }
else if (m_type ==
"double") {
267 KConfigSkeleton::ItemDouble *doubleItem = m_config->addItemDouble(m_name,
268 *d->newDouble(), m_default.toDouble(), m_key);
270 doubleItem->setMinValue(m_min);
273 doubleItem->setMaxValue(m_max);
276 }
else if (m_type ==
"intlist") {
277 QStringList tmpList = m_default.split(
',');
278 QList<int> defaultList;
279 foreach (
const QString &tmp, tmpList) {
280 defaultList.append(tmp.toInt());
282 item = m_config->addItemIntList(m_name, *d->newIntList(), defaultList, m_key);
283 }
else if (m_type ==
"longlong") {
284 KConfigSkeleton::ItemLongLong *longlongItem = m_config->addItemLongLong(m_name,
285 *d->newLongLong(), m_default.toLongLong(), m_key);
287 longlongItem->setMinValue(m_min);
290 longlongItem->setMaxValue(m_max);
298 }
else if (m_type ==
"point") {
300 QStringList tmpList = m_default.split(
',');
301 if (tmpList.size() >= 2) {
302 defaultPoint.setX(tmpList[0].toInt());
303 defaultPoint.setY(tmpList[1].toInt());
305 item = m_config->addItemPoint(m_name, *d->newPoint(), defaultPoint, m_key);
306 }
else if (m_type ==
"rect") {
308 QStringList tmpList = m_default.split(
',');
309 if (tmpList.size() >= 4) {
310 defaultRect.setCoords(tmpList[0].toInt(), tmpList[1].toInt(),
311 tmpList[2].toInt(), tmpList[3].toInt());
313 item = m_config->addItemRect(m_name, *d->newRect(), defaultRect, m_key);
314 }
else if (m_type ==
"size") {
316 QStringList tmpList = m_default.split(
',');
317 if (tmpList.size() >= 2) {
318 defaultSize.setWidth(tmpList[0].toInt());
319 defaultSize.setHeight(tmpList[1].toInt());
321 item = m_config->addItemSize(m_name, *d->newSize(), defaultSize, m_key);
322 }
else if (m_type ==
"ulonglong") {
323 KConfigSkeleton::ItemULongLong *ulonglongItem =
324 m_config->addItemULongLong(m_name, *d->newULongLong(), m_default.toULongLong(), m_key);
326 ulonglongItem->setMinValue(m_min);
329 ulonglongItem->setMaxValue(m_max);
331 item = ulonglongItem;
344 item->setLabel(m_label);
345 item->setWhatsThis(m_whatsThis);
346 d->keysToNames.insert(item->group() + item->key(), item->name());
350 void ConfigLoaderHandler::resetState()
362 m_enumChoices.clear();
368 d(new ConfigLoaderPrivate)
375 d(new ConfigLoaderPrivate)
384 :
KConfigSkeleton(KSharedConfig::openConfig(config->config()->name(), KConfig::SimpleConfig), parent),
385 d(new ConfigLoaderPrivate)
387 KConfigGroup group = config->parent();
388 d->baseGroup = config->name();
389 while (group.isValid() && group.name() !=
"<default>") {
390 d->baseGroup = group.name() +
'\x1d' + d->baseGroup;
391 group = group.parent();
403 return KConfigSkeleton::findItem(d->keysToNames[group + key]);
408 return KConfigSkeleton::findItem(name);
413 KConfigSkeletonItem *item = KConfigSkeleton::findItem(name);
416 return item->property();
424 return d->groups.contains(group);
434 if (d->saveDefaults) {
435 KConfigSkeletonItem::List itemList = items();
436 for(
int i = 0; i < itemList.size(); i++) {
437 KConfigGroup cg(config(), itemList.at(i)->group());
438 cg.writeEntry(itemList.at(i)->key(),
"");
A KConfigSkeleton that populates itself based on KConfigXT XML.
QStringList groupList() const
bool hasGroup(const QString &group) const
Check to see if a group exists.
KConfigSkeletonItem * findItem(const QString &group, const QString &key)
Finds the item for the given group and key.
KConfigSkeletonItem * findItemByName(const QString &name)
Finds an item by its name.
QVariant property(const QString &name)
Returns the property (variantized value) of the named item.
ConfigLoader(const QString &configFile, QIODevice *xml, QObject *parent=0)
Creates a KConfigSkeleton populated using the definition found in the XML data passed in...
void usrWriteConfig()
Hack used to force writing when no default exists in config file.