From aa8ed30fdabe936989942f28768a2f174135111e Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 2 May 2013 13:31:18 +0200 Subject: [PATCH] ConfigLoaderHandlerMap that loads config in maps ConfigLoaderHandlerMap populates a qvariantmap instead of a kconfigskeleton will be used by service for operations --- src/plasma/configloader.cpp | 40 +++++++- src/plasma/private/configloaderhandler_p.h | 11 +- src/plasma/private/service_p.h | 2 +- src/plasma/service.cpp | 112 +++++++++++++++++++++ 4 files changed, 162 insertions(+), 3 deletions(-) diff --git a/src/plasma/configloader.cpp b/src/plasma/configloader.cpp index b9b41da28..ce20d16b9 100644 --- a/src/plasma/configloader.cpp +++ b/src/plasma/configloader.cpp @@ -82,7 +82,10 @@ bool ConfigLoaderHandler::startElement(const QString &namespaceURI, const QStrin group = d->baseGroup + '\x1d' + group; } } - m_config->setCurrentGroup(group); + m_currentGroup = group; + if (m_config) { + m_config->setCurrentGroup(group); + } } else if (tag == "entry") { for (int i = 0; i < numAttrs; ++i) { QString name = attrs.localName(i).toLower(); @@ -116,6 +119,41 @@ bool ConfigLoaderHandler::characters(const QString &ch) return true; } +QString ConfigLoaderHandler::name() const +{ + return m_name; +} + +void ConfigLoaderHandler::setName(const QString &name) +{ + m_name = name; +} + +QString ConfigLoaderHandler::key() const +{ + return m_key; +} + +void ConfigLoaderHandler::setKey(const QString &key) +{ + m_key = key; +} + +QString ConfigLoaderHandler::type() const +{ + return m_type; +} + +QString ConfigLoaderHandler::currentGroup() const +{ + return m_currentGroup; +} + +QString ConfigLoaderHandler::defaultValue() const +{ + return m_default; +} + bool ConfigLoaderHandler::endElement(const QString &namespaceURI, const QString &localName, const QString &qName) { diff --git a/src/plasma/private/configloaderhandler_p.h b/src/plasma/private/configloaderhandler_p.h index 865a36c4b..8431abdba 100644 --- a/src/plasma/private/configloaderhandler_p.h +++ b/src/plasma/private/configloaderhandler_p.h @@ -38,14 +38,23 @@ public: const QString &qName); bool characters(const QString &ch); + QString name() const; + void setName(const QString &name); + QString key() const; + void setKey(const QString &name); + QString type() const; + QString currentGroup() const; + QString defaultValue() const; + private: - void addItem(); + virtual void addItem(); void resetState(); ConfigLoader *m_config; ConfigLoaderPrivate *d; int m_min; int m_max; + QString m_currentGroup; QString m_name; QString m_key; QString m_type; diff --git a/src/plasma/private/service_p.h b/src/plasma/private/service_p.h index 4f3cb8247..9a6d97741 100644 --- a/src/plasma/private/service_p.h +++ b/src/plasma/private/service_p.h @@ -80,7 +80,7 @@ public: config(0), dummyConfig(0), publicService(0) - { + { } ~ServicePrivate() diff --git a/src/plasma/service.cpp b/src/plasma/service.cpp index 9821abf9c..73f908c8a 100644 --- a/src/plasma/service.cpp +++ b/src/plasma/service.cpp @@ -38,15 +38,125 @@ #include "configloader.h" #include "version.h" #include "private/configloader_p.h" +#include "private/configloaderhandler_p.h" #include "pluginloader.h" namespace Plasma { +class ConfigLoaderHandlerMap : public ConfigLoaderHandler +{ +public: + ConfigLoaderHandlerMap(ConfigLoader *config, ConfigLoaderPrivate *d) + : ConfigLoaderHandler(config, d) + {} + + void addItem(); + +//private: + QVariantMap m_map; +}; + +void ConfigLoaderHandlerMap::addItem() +{ + if (name().isEmpty()) { + if (key().isEmpty()) { + return; + } + + setName(key()); + } else if (key().isEmpty()) { + if (name().isEmpty()) { + return; + } + + setKey(name()); + } + + QVariantMap map; + if (m_map.contains(currentGroup())) { + map = m_map[currentGroup()].value(); + } else { + m_map[currentGroup()] = QVariantMap(); + } + + + if (type() == "bool") { + bool defaultVal = defaultValue().toLower() == "true"; + map[key()] = defaultVal; + } else if (type() == "color") { + map[key()] = QColor(defaultValue()); + } else if (type() == "datetime") { + map[key()] = QDateTime::fromString(defaultValue()); + } else if (type() == "enum") { + key() = (key().isEmpty()) ? name() : key(); + map[key()] = defaultValue().toUInt(); + } else if (type() == "font") { + map[key()] = QFont(defaultValue()); + } else if (type() == "int") { + map[key()] = defaultValue().toInt(); + } else if (type() == "password") { + map[key()] = defaultValue(); + } else if (type() == "path") { + map[key()] = defaultValue(); + } else if (type() == "string") { + map[key()] = defaultValue(); + } else if (type() == "stringlist") { + //FIXME: the split() is naive and will break on lists with ,'s in them + map[key()] = defaultValue().split(','); + } else if (type() == "uint") { + map[key()] = defaultValue().toUInt(); + } else if (type() == "url") { + setKey((key().isEmpty()) ? name() : key()); + map[key()] = QUrl::fromUserInput(defaultValue()); + } else if (type() == "double") { + map[key()] = defaultValue().toDouble(); + } else if (type() == "intlist") { + QStringList tmpList = defaultValue().split(','); + QList defaultList; + foreach (const QString &tmp, tmpList) { + defaultList.append(tmp.toInt()); + } + map[key()] = QVariant::fromValue(defaultList); + } else if (type() == "longlong") { + map[key()] = defaultValue().toLongLong(); + } else if (type() == "point") { + QPoint defaultPoint; + QStringList tmpList = defaultValue().split(','); + if (tmpList.size() >= 2) { + defaultPoint.setX(tmpList[0].toInt()); + defaultPoint.setY(tmpList[1].toInt()); + } + map[key()] = defaultPoint; + } else if (type() == "rect") { + QRect defaultRect; + QStringList tmpList = defaultValue().split(','); + if (tmpList.size() >= 4) { + defaultRect.setCoords(tmpList[0].toInt(), tmpList[1].toInt(), + tmpList[2].toInt(), tmpList[3].toInt()); + } + map[key()] = tmpList; + } else if (type() == "size") { + QSize defaultSize; + QStringList tmpList = defaultValue().split(','); + if (tmpList.size() >= 2) { + defaultSize.setWidth(tmpList[0].toInt()); + defaultSize.setHeight(tmpList[1].toInt()); + } + map[key()] = tmpList; + } else if (type() == "ulonglong") { + map[key()] = defaultValue().toULongLong(); + } + + m_map[currentGroup()] = map; +} + Service::Service(QObject *parent) : QObject(parent), d(new ServicePrivate(this)) { + ConfigLoaderPrivate *cl = new ConfigLoaderPrivate; + ConfigLoaderHandlerMap map(0, cl); } Service::Service(QObject *parent, const QVariantList &args) @@ -54,6 +164,8 @@ Service::Service(QObject *parent, const QVariantList &args) d(new ServicePrivate(this)) { Q_UNUSED(args) + ConfigLoaderPrivate *cl = new ConfigLoaderPrivate; + ConfigLoaderHandlerMap map(0, cl); } Service::~Service()