use variant maps instead of configgroups

services operationdescriptions are now QVariantMaps
This commit is contained in:
Marco Martin 2013-05-02 14:46:29 +02:00
parent aa8ed30fda
commit b0530820ec
11 changed files with 99 additions and 102 deletions

View File

@ -38,8 +38,8 @@ void StorageTest::initTestCase()
void StorageTest::store()
{
Storage storage;
KConfigGroup op = storage.operationDescription("save");
op.writeEntry("group", "Test");
QVariantMap op = storage.operationDescription("save");
op["group"] = "Test";
Plasma::ServiceJob *job = storage.startOperationCall(op);
StorageJob *storageJob = qobject_cast<StorageJob *>(job);
@ -54,8 +54,8 @@ void StorageTest::store()
void StorageTest::retrieve()
{
Storage storage;
KConfigGroup op = storage.operationDescription("retrieve");
op.writeEntry("group", "Test");
QVariantMap op = storage.operationDescription("retrieve");
op["group"] = "Test";
Plasma::ServiceJob *job = storage.startOperationCall(op);
StorageJob *storageJob = qobject_cast<StorageJob *>(job);
@ -70,8 +70,8 @@ void StorageTest::retrieve()
void StorageTest::deleteEntry()
{
Storage storage;
KConfigGroup op = storage.operationDescription("delete");
op.writeEntry("group", "Test");
QVariantMap op = storage.operationDescription("delete");
op["group"] = "Test";
Plasma::ServiceJob *job = storage.startOperationCall(op);
StorageJob *storageJob = qobject_cast<StorageJob *>(job);
@ -83,7 +83,7 @@ void StorageTest::deleteEntry()
}
op = storage.operationDescription("retrieve");
op.writeEntry("group", "Test");
op["group"] = "Test";
job = storage.startOperationCall(op);
storageJob = qobject_cast<StorageJob *>(job);

View File

@ -201,8 +201,8 @@ void DataContainerPrivate::store()
storage = new Storage(q);
}
KConfigGroup op = storage->operationDescription("save");
op.writeEntry("group", q->objectName());
QVariantMap op = storage->operationDescription("save");
op["group"] = q->objectName();
StorageJob *job = static_cast<StorageJob *>(storage->startOperationCall(op));
job->setData(data);
storageCount++;
@ -229,8 +229,8 @@ void DataContainerPrivate::retrieve()
storage = new Storage(q);
}
KConfigGroup retrieveGroup = storage->operationDescription("retrieve");
retrieveGroup.writeEntry("group", q->objectName());
QVariantMap retrieveGroup = storage->operationDescription("retrieve");
retrieveGroup["group"] = q->objectName();
ServiceJob* retrieveJob = storage->startOperationCall(retrieveGroup);
QObject::connect(retrieveJob, SIGNAL(result(KJob*)), q,
SLOT(populateFromStoredData(KJob*)));
@ -255,9 +255,9 @@ void DataContainerPrivate::populateFromStoredData(KJob *job)
q->forceImmediateUpdate();
}
KConfigGroup expireGroup = storage->operationDescription("expire");
QVariantMap expireGroup = storage->operationDescription("expire");
//expire things older than 4 days
expireGroup.writeEntry("age", 345600);
expireGroup["age"] = 345600;
storage->startOperationCall(expireGroup);
}

View File

@ -67,8 +67,8 @@ void DataEngineConsumerPrivate::slotServiceReady(Plasma::Service *plasmoidServic
#ifndef NDEBUG
kDebug() << "requesting dataengine!";
#endif
KConfigGroup op = plasmoidService->operationDescription("DataEngine");
op.writeEntry("EngineName", engineNameForService.value(plasmoidService));
QVariantMap op = plasmoidService->operationDescription("DataEngine");
op["EngineName"] = engineNameForService.value(plasmoidService);
plasmoidService->startOperationCall(op);
connect(plasmoidService, SIGNAL(finished(Plasma::ServiceJob*)),
this, SLOT(slotJobFinished(Plasma::ServiceJob*)));

View File

@ -45,7 +45,7 @@ class NullServiceJob : public ServiceJob
{
public:
NullServiceJob(const QString &destination, const QString &operation, QObject *parent)
: ServiceJob(destination, operation, QHash<QString, QVariant>(), parent)
: ServiceJob(destination, operation, QVariantMap(), parent)
{
}
@ -66,7 +66,7 @@ public:
setName("NullService");
}
ServiceJob *createJob(const QString &operation, QHash<QString, QVariant> &)
ServiceJob *createJob(const QString &operation, QVariantMap &)
{
return new NullServiceJob(destination(), operation, this);
}
@ -77,7 +77,6 @@ class ServicePrivate
public:
ServicePrivate(Service *service)
: q(service),
config(0),
dummyConfig(0),
publicService(0)
{
@ -85,7 +84,6 @@ public:
~ServicePrivate()
{
delete config;
delete dummyConfig;
}
@ -99,7 +97,7 @@ public:
QString destination;
QString name;
QString resourcename;
ConfigLoader *config;
QMap<QString, QVariantMap> operationsMap;
KConfig *dummyConfig;
DNSSD::PublicService *publicService;
QMultiHash<QWidget *, QString> associatedWidgets;

View File

@ -34,7 +34,7 @@ public:
ServiceJobPrivate(ServiceJob *owner,
const QString &dest,
const QString &op,
const QHash<QString, QVariant> &params);
const QVariantMap &params);
void preventAutoStart();
void autoStart();
@ -42,7 +42,7 @@ public:
ServiceJob *q;
QString destination;
QString operation;
QHash<QString, QVariant> parameters;
QVariantMap parameters;
QVariant result;
bool m_allowAutoStart;
};

View File

@ -43,7 +43,7 @@
StorageJob::StorageJob(const QString& destination,
const QString& operation,
const QHash<QString, QVariant>& parameters,
const QVariantMap& parameters,
QObject *parent)
: ServiceJob(destination, operation, parameters, parent),
m_clientName(destination)
@ -77,7 +77,7 @@ QString StorageJob::clientName() const
void StorageJob::start()
{
//FIXME: QHASH
QHash<QString, QVariant> params = parameters();
QVariantMap params = parameters();
QString valueGroup = params["group"].toString();
if (valueGroup.isEmpty()) {
@ -86,13 +86,13 @@ void StorageJob::start()
QWeakPointer<StorageJob> me(this);
if (operationName() == "save") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "retrieve") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "delete") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "deleteEntry", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "deleteEntry", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "expire") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else {
setError(true);
setResult(false);
@ -109,7 +109,7 @@ void StorageJob::resultSlot(StorageJob *job, const QVariant &result)
}
}
Plasma::ServiceJob* Storage::createJob(const QString &operation, QHash<QString, QVariant> &parameters)
Plasma::ServiceJob* Storage::createJob(const QString &operation, QVariantMap &parameters)
{
if (m_clientName.isEmpty()) {
return 0;

View File

@ -37,7 +37,7 @@ class StorageJob : public Plasma::ServiceJob
public:
StorageJob(const QString& destination,
const QString& operation,
const QHash<QString, QVariant>& parameters,
const QVariantMap& parameters,
QObject *parent = 0);
~StorageJob();
void setData(const QVariantHash &data);
@ -66,7 +66,7 @@ public:
~Storage();
protected:
Plasma::ServiceJob* createJob(const QString &operation, QHash<QString, QVariant> &parameters);
Plasma::ServiceJob* createJob(const QString &operation, QVariantMap &parameters);
private:
QString m_clientName;

View File

@ -52,9 +52,10 @@ public:
{}
void addItem();
const QMap<QString, QVariantMap> &groupsMap() const;
//private:
QVariantMap m_map;
private:
QMap<QString, QVariantMap> m_groupsMap;
};
void ConfigLoaderHandlerMap::addItem()
@ -73,53 +74,50 @@ void ConfigLoaderHandlerMap::addItem()
setKey(name());
}
QVariantMap map;
if (m_map.contains(currentGroup())) {
map = m_map[currentGroup()].value<QVariantMap>();
} else {
m_map[currentGroup()] = QVariantMap();
if (!m_groupsMap.contains(currentGroup())) {
m_groupsMap[currentGroup()] = QVariantMap();
}
if (type() == "bool") {
bool defaultVal = defaultValue().toLower() == "true";
map[key()] = defaultVal;
m_groupsMap[currentGroup()][key()] = defaultVal;
} else if (type() == "color") {
map[key()] = QColor(defaultValue());
m_groupsMap[currentGroup()][key()] = QColor(defaultValue());
} else if (type() == "datetime") {
map[key()] = QDateTime::fromString(defaultValue());
m_groupsMap[currentGroup()][key()] = QDateTime::fromString(defaultValue());
} else if (type() == "enum") {
key() = (key().isEmpty()) ? name() : key();
map[key()] = defaultValue().toUInt();
m_groupsMap[currentGroup()][key()] = defaultValue().toUInt();
} else if (type() == "font") {
map[key()] = QFont(defaultValue());
m_groupsMap[currentGroup()][key()] = QFont(defaultValue());
} else if (type() == "int") {
map[key()] = defaultValue().toInt();
m_groupsMap[currentGroup()][key()] = defaultValue().toInt();
} else if (type() == "password") {
map[key()] = defaultValue();
m_groupsMap[currentGroup()][key()] = defaultValue();
} else if (type() == "path") {
map[key()] = defaultValue();
m_groupsMap[currentGroup()][key()] = defaultValue();
} else if (type() == "string") {
map[key()] = defaultValue();
m_groupsMap[currentGroup()][key()] = defaultValue();
} else if (type() == "stringlist") {
//FIXME: the split() is naive and will break on lists with ,'s in them
map[key()] = defaultValue().split(',');
m_groupsMap[currentGroup()][key()] = defaultValue().split(',');
} else if (type() == "uint") {
map[key()] = defaultValue().toUInt();
m_groupsMap[currentGroup()][key()] = defaultValue().toUInt();
} else if (type() == "url") {
setKey((key().isEmpty()) ? name() : key());
map[key()] = QUrl::fromUserInput(defaultValue());
m_groupsMap[currentGroup()][key()] = QUrl::fromUserInput(defaultValue());
} else if (type() == "double") {
map[key()] = defaultValue().toDouble();
m_groupsMap[currentGroup()][key()] = defaultValue().toDouble();
} else if (type() == "intlist") {
QStringList tmpList = defaultValue().split(',');
QList<int> defaultList;
foreach (const QString &tmp, tmpList) {
defaultList.append(tmp.toInt());
}
map[key()] = QVariant::fromValue(defaultList);
m_groupsMap[currentGroup()][key()] = QVariant::fromValue(defaultList);
} else if (type() == "longlong") {
map[key()] = defaultValue().toLongLong();
m_groupsMap[currentGroup()][key()] = defaultValue().toLongLong();
} else if (type() == "point") {
QPoint defaultPoint;
QStringList tmpList = defaultValue().split(',');
@ -127,7 +125,7 @@ void ConfigLoaderHandlerMap::addItem()
defaultPoint.setX(tmpList[0].toInt());
defaultPoint.setY(tmpList[1].toInt());
}
map[key()] = defaultPoint;
m_groupsMap[currentGroup()][key()] = defaultPoint;
} else if (type() == "rect") {
QRect defaultRect;
QStringList tmpList = defaultValue().split(',');
@ -135,7 +133,7 @@ void ConfigLoaderHandlerMap::addItem()
defaultRect.setCoords(tmpList[0].toInt(), tmpList[1].toInt(),
tmpList[2].toInt(), tmpList[3].toInt());
}
map[key()] = tmpList;
m_groupsMap[currentGroup()][key()] = tmpList;
} else if (type() == "size") {
QSize defaultSize;
QStringList tmpList = defaultValue().split(',');
@ -143,20 +141,21 @@ void ConfigLoaderHandlerMap::addItem()
defaultSize.setWidth(tmpList[0].toInt());
defaultSize.setHeight(tmpList[1].toInt());
}
map[key()] = tmpList;
m_groupsMap[currentGroup()][key()] = tmpList;
} else if (type() == "ulonglong") {
map[key()] = defaultValue().toULongLong();
m_groupsMap[currentGroup()][key()] = defaultValue().toULongLong();
}
}
m_map[currentGroup()] = map;
const QMap<QString, QVariantMap> &ConfigLoaderHandlerMap::groupsMap() const
{
return m_groupsMap;
}
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)
@ -164,8 +163,6 @@ Service::Service(QObject *parent, const QVariantList &args)
d(new ServicePrivate(this))
{
Q_UNUSED(args)
ConfigLoaderPrivate *cl = new ConfigLoaderPrivate;
ConfigLoaderHandlerMap map(0, cl);
}
Service::~Service()
@ -204,38 +201,35 @@ QString Service::destination() const
QStringList Service::operationNames() const
{
if (!d->config) {
if (!d->operationsMap.keys().isEmpty()) {
#ifndef NDEBUG
kDebug() << "No valid operations scheme has been registered";
#endif
return QStringList();
}
return d->config->groupList();
return d->operationsMap.keys();
}
KConfigGroup Service::operationDescription(const QString &operationName)
QVariantMap Service::operationDescription(const QString &operationName)
{
if (!d->config) {
if (!d->operationsMap.keys().isEmpty()) {
#ifndef NDEBUG
kDebug() << "No valid operations scheme has been registered";
#endif
return d->dummyGroup();
return QVariantMap();
}
d->config->writeConfig();
KConfigGroup params(d->config->config(), operationName);
//kDebug() << "operation" << operationName
// << "requested, has keys" << params.keyList() << "from"
// << d->config->config()->name();
return params;
// << "requested, has keys" << d->operationsMap.keys();
return d->operationsMap.value(operationName);
}
QHash<QString, QVariant> Service::parametersFromDescription(const KConfigGroup &description)
/*QHash<QString, QVariant> Service::parametersFromDescription(const KConfigGroup &description)
{
QHash<QString, QVariant> params;
if (!d->config || !description.isValid()) {
if (!d->operationsMap.keys().isEmpty() || !description.isValid()) {
return params;
}
@ -248,30 +242,30 @@ QHash<QString, QVariant> Service::parametersFromDescription(const KConfigGroup &
}
return params;
}
}*/
ServiceJob *Service::startOperationCall(const KConfigGroup &description, QObject *parent)
ServiceJob *Service::startOperationCall(const QVariantMap &description, QObject *parent)
{
// TODO: nested groups?
ServiceJob *job = 0;
const QString op = description.isValid() ? description.name() : QString();
const QString op = !description.isEmpty() ? description.value("_name").toString() : QString();
if (!d->config) {
if (!d->operationsMap.keys().isEmpty()) {
#ifndef NDEBUG
kDebug() << "No valid operations scheme has been registered";
#endif
} else if (!op.isEmpty() && d->config->hasGroup(op)) {
} else if (!op.isEmpty() && d->operationsMap.contains(op)) {
if (d->disabledOperations.contains(op)) {
#ifndef NDEBUG
kDebug() << "Operation" << op << "is disabled";
#endif
} else {
QHash<QString, QVariant> params = parametersFromDescription(description);
job = createJob(op, params);
QVariantMap map = description;
job = createJob(op, map);
}
} else {
#ifndef NDEBUG
kDebug() << op << "is not a valid group; valid groups are:" << d->config->groupList();
kDebug() << op << "is not a valid group; valid groups are:" << d->operationsMap->keys();
#endif
}
@ -319,8 +313,7 @@ void Service::setName(const QString &name)
d->name = name;
// now reset the config, which may be based on our name
delete d->config;
d->config = 0;
d->operationsMap.clear();
delete d->dummyConfig;
d->dummyConfig = 0;
@ -332,7 +325,7 @@ void Service::setName(const QString &name)
void Service::setOperationEnabled(const QString &operation, bool enable)
{
if (!d->config || !d->config->hasGroup(operation)) {
if (!d->operationsMap.keys().isEmpty() || !d->operationsMap.contains(operation)) {
return;
}
@ -365,19 +358,25 @@ void Service::setOperationEnabled(const QString &operation, bool enable)
bool Service::isOperationEnabled(const QString &operation) const
{
return d->config && d->config->hasGroup(operation) && !d->disabledOperations.contains(operation);
return d->operationsMap.contains(operation) && !d->disabledOperations.contains(operation);
}
void Service::setOperationsScheme(QIODevice *xml)
{
delete d->config;
d->operationsMap.clear();
delete d->dummyConfig;
d->dummyConfig = 0;
KSharedConfigPtr c = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig);
d->config = new ConfigLoader(c, xml, this);
d->config->d->setWriteDefaults(true);
ConfigLoaderPrivate *configLoaderPrivate = new ConfigLoaderPrivate;
configLoaderPrivate->setWriteDefaults(true);
ConfigLoaderHandlerMap configLoaderHandler(0, configLoaderPrivate);
QXmlInputSource source(xml);
QXmlSimpleReader reader;
reader.setContentHandler(&configLoaderHandler);
reader.parse(&source, false);
d->operationsMap = configLoaderHandler.groupsMap();
delete configLoaderPrivate;
emit operationsChanged();
@ -385,7 +384,7 @@ void Service::setOperationsScheme(QIODevice *xml)
QHashIterator<QWidget *, QString> it(d->associatedWidgets);
while (it.hasNext()) {
it.next();
it.key()->setEnabled(d->config->hasGroup(it.value()));
it.key()->setEnabled(d->operationsMap.contains(it.value()));
}
}
@ -393,14 +392,14 @@ void Service::setOperationsScheme(QIODevice *xml)
QHashIterator<QQuickItem *, QString> it(d->associatedItems);
while (it.hasNext()) {
it.next();
it.key()->setEnabled(d->config->hasGroup(it.value()));
it.key()->setEnabled(d->operationsMap.contains(it.value()));
}
}
}
void Service::registerOperationsScheme()
{
if (d->config) {
if (!d->operationsMap.keys().isEmpty()) {
// we've already done our job. let's go home.
return;
}

View File

@ -126,7 +126,7 @@ public:
* @param operationName the operation to retrieve parameters for
* @return KConfigGroup containing the parameters
*/
Q_INVOKABLE KConfigGroup operationDescription(const QString &operationName);
Q_INVOKABLE QVariantMap operationDescription(const QString &operationName);
/**
* Called to create a ServiceJob which is associated with a given
@ -135,7 +135,7 @@ public:
* @return a started ServiceJob; the consumer may connect to relevant
* signals before returning to the event loop
*/
Q_INVOKABLE ServiceJob *startOperationCall(const KConfigGroup &description, QObject *parent = 0);
Q_INVOKABLE ServiceJob *startOperationCall(const QVariantMap &description, QObject *parent = 0);
/**
* Query to find if an operation is enabled or not.
@ -177,7 +177,7 @@ public:
* @param description the configuration values to turn into the parameter map
* @since 4.4
*/
Q_INVOKABLE QHash<QString, QVariant> parametersFromDescription(const KConfigGroup &description);
/*Q_INVOKABLE QVariantMap parametersFromDescription(const KConfigGroup &description);*/
Q_SIGNALS:
/**
@ -213,7 +213,7 @@ protected:
* @return a ServiceJob that can be started and monitored by the consumer
*/
virtual ServiceJob *createJob(const QString &operation,
QHash<QString, QVariant> &parameters) = 0;
QVariantMap &parameters) = 0;
/**
* By default this is based on the file in plasma/services/name.operations, but can be

View File

@ -27,7 +27,7 @@ namespace Plasma
{
ServiceJobPrivate::ServiceJobPrivate(ServiceJob *owner, const QString &dest,
const QString &op, const QHash<QString, QVariant> &params)
const QString &op, const QVariantMap &params)
: q(owner),
destination(dest),
operation(op),
@ -56,7 +56,7 @@ void ServiceJobPrivate::autoStart()
}
ServiceJob::ServiceJob(const QString &destination, const QString &operation,
const QHash<QString, QVariant> &parameters, QObject *parent)
const QVariantMap &parameters, QObject *parent)
: KJob(parent),
d(new ServiceJobPrivate(this, destination, operation, parameters))
{
@ -78,7 +78,7 @@ QString ServiceJob::operationName() const
return d->operation;
}
QHash<QString, QVariant> ServiceJob::parameters() const
QVariantMap ServiceJob::parameters() const
{
return d->parameters;
}

View File

@ -68,7 +68,7 @@ public:
* @param parent the parent object for this service
*/
ServiceJob(const QString &destination, const QString &operation,
const QHash<QString, QVariant> &parameters, QObject *parent = 0);
const QVariantMap &parameters, QObject *parent = 0);
/**
* Destructor
@ -88,7 +88,7 @@ public:
/**
* @return the parameters for the operation
*/
QHash<QString, QVariant> parameters() const;
QVariantMap parameters() const;
/**
* Returns the result of the operation