2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "service.h"
|
|
|
|
#include "private/service_p.h"
|
|
|
|
|
2009-09-09 17:37:03 +02:00
|
|
|
#include "config-plasma.h"
|
2009-09-09 15:40:16 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTimer>
|
2013-04-03 00:10:04 +02:00
|
|
|
#include <QQuickItem>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2013-07-29 19:05:59 +02:00
|
|
|
#include <QDebug>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kservice.h>
|
|
|
|
#include <kservicetypetrader.h>
|
|
|
|
#include <ksharedconfig.h>
|
2012-06-12 12:59:34 +02:00
|
|
|
|
|
|
|
#include <qstandardpaths.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "configloader.h"
|
|
|
|
#include "version.h"
|
2008-11-21 02:26:43 +01:00
|
|
|
#include "private/configloader_p.h"
|
2013-05-02 13:31:18 +02:00
|
|
|
#include "private/configloaderhandler_p.h"
|
2010-07-15 23:06:21 +02:00
|
|
|
#include "pluginloader.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2013-05-02 13:31:18 +02:00
|
|
|
class ConfigLoaderHandlerMap : public ConfigLoaderHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConfigLoaderHandlerMap(ConfigLoader *config, ConfigLoaderPrivate *d)
|
|
|
|
: ConfigLoaderHandler(config, d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void addItem();
|
2013-05-02 14:46:29 +02:00
|
|
|
const QMap<QString, QVariantMap> &groupsMap() const;
|
2013-05-02 13:31:18 +02:00
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
private:
|
|
|
|
QMap<QString, QVariantMap> m_groupsMap;
|
2013-05-02 13:31:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void ConfigLoaderHandlerMap::addItem()
|
|
|
|
{
|
|
|
|
if (name().isEmpty()) {
|
|
|
|
if (key().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setName(key());
|
|
|
|
} else if (key().isEmpty()) {
|
|
|
|
if (name().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setKey(name());
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
if (!m_groupsMap.contains(currentGroup())) {
|
|
|
|
m_groupsMap[currentGroup()] = QVariantMap();
|
2013-05-02 15:33:57 +02:00
|
|
|
m_groupsMap[currentGroup()]["_name"] = currentGroup();
|
2013-05-02 13:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type() == "bool") {
|
|
|
|
bool defaultVal = defaultValue().toLower() == "true";
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultVal;
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "color") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = QColor(defaultValue());
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "datetime") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = QDateTime::fromString(defaultValue());
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "enum") {
|
|
|
|
key() = (key().isEmpty()) ? name() : key();
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toUInt();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "font") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = QFont(defaultValue());
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "int") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toInt();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "password") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "path") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "string") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "stringlist") {
|
|
|
|
//FIXME: the split() is naive and will break on lists with ,'s in them
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().split(',');
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "uint") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toUInt();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "url") {
|
|
|
|
setKey((key().isEmpty()) ? name() : key());
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = QUrl::fromUserInput(defaultValue());
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "double") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toDouble();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "intlist") {
|
|
|
|
QStringList tmpList = defaultValue().split(',');
|
|
|
|
QList<int> defaultList;
|
|
|
|
foreach (const QString &tmp, tmpList) {
|
|
|
|
defaultList.append(tmp.toInt());
|
|
|
|
}
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = QVariant::fromValue(defaultList);
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "longlong") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toLongLong();
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "point") {
|
|
|
|
QPoint defaultPoint;
|
|
|
|
QStringList tmpList = defaultValue().split(',');
|
|
|
|
if (tmpList.size() >= 2) {
|
|
|
|
defaultPoint.setX(tmpList[0].toInt());
|
|
|
|
defaultPoint.setY(tmpList[1].toInt());
|
|
|
|
}
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultPoint;
|
2013-05-02 13:31:18 +02:00
|
|
|
} 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());
|
|
|
|
}
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = tmpList;
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "size") {
|
|
|
|
QSize defaultSize;
|
|
|
|
QStringList tmpList = defaultValue().split(',');
|
|
|
|
if (tmpList.size() >= 2) {
|
|
|
|
defaultSize.setWidth(tmpList[0].toInt());
|
|
|
|
defaultSize.setHeight(tmpList[1].toInt());
|
|
|
|
}
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = tmpList;
|
2013-05-02 13:31:18 +02:00
|
|
|
} else if (type() == "ulonglong") {
|
2013-05-02 14:46:29 +02:00
|
|
|
m_groupsMap[currentGroup()][key()] = defaultValue().toULongLong();
|
2013-05-02 13:31:18 +02:00
|
|
|
}
|
2013-05-02 14:46:29 +02:00
|
|
|
}
|
2013-05-02 13:31:18 +02:00
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
const QMap<QString, QVariantMap> &ConfigLoaderHandlerMap::groupsMap() const
|
|
|
|
{
|
|
|
|
return m_groupsMap;
|
2013-05-02 13:31:18 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Service::Service(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new ServicePrivate(this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::Service(QObject *parent, const QVariantList &args)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new ServicePrivate(this))
|
|
|
|
{
|
|
|
|
Q_UNUSED(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setDestination(const QString &destination)
|
|
|
|
{
|
|
|
|
d->destination = destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Service::destination() const
|
|
|
|
{
|
|
|
|
return d->destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Service::operationNames() const
|
|
|
|
{
|
2013-05-02 15:33:57 +02:00
|
|
|
if (d->operationsMap.keys().isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
return d->operationsMap.keys();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
QVariantMap Service::operationDescription(const QString &operationName)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2013-05-02 15:33:57 +02:00
|
|
|
if (d->operationsMap.keys().isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2013-05-02 14:46:29 +02:00
|
|
|
return QVariantMap();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2013-07-29 19:05:59 +02:00
|
|
|
//qDebug() << "operation" << operationName
|
2013-05-02 14:46:29 +02:00
|
|
|
// << "requested, has keys" << d->operationsMap.keys();
|
|
|
|
return d->operationsMap.value(operationName);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
ServiceJob *Service::startOperationCall(const QVariantMap &description, QObject *parent)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
// TODO: nested groups?
|
|
|
|
ServiceJob *job = 0;
|
2013-05-02 14:46:29 +02:00
|
|
|
const QString op = !description.isEmpty() ? description.value("_name").toString() : QString();
|
2009-10-20 21:15:32 +02:00
|
|
|
|
2013-05-02 15:33:57 +02:00
|
|
|
if (d->operationsMap.keys().isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2013-05-02 14:46:29 +02:00
|
|
|
} else if (!op.isEmpty() && d->operationsMap.contains(op)) {
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->disabledOperations.contains(op)) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "Operation" << op << "is disabled";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2013-05-02 14:46:29 +02:00
|
|
|
QVariantMap map = description;
|
|
|
|
job = createJob(op, map);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2014-04-26 01:45:47 +02:00
|
|
|
// qDebug() << op << "is not a valid group; valid groups are:" << d->operationsMap.keys();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!job) {
|
2011-07-19 20:00:25 +02:00
|
|
|
job = new NullServiceJob(d->destination, op, this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
job->setParent(parent ? parent : this);
|
2011-04-28 18:01:16 +02:00
|
|
|
QTimer::singleShot(0, job, SLOT(autoStart()));
|
2008-11-04 00:08:39 +01:00
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Service::name() const
|
|
|
|
{
|
|
|
|
return d->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setName(const QString &name)
|
|
|
|
{
|
|
|
|
d->name = name;
|
|
|
|
|
|
|
|
// now reset the config, which may be based on our name
|
2013-05-02 14:46:29 +02:00
|
|
|
d->operationsMap.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
registerOperationsScheme();
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
emit serviceReady(this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setOperationEnabled(const QString &operation, bool enable)
|
|
|
|
{
|
2013-05-02 15:33:57 +02:00
|
|
|
if (d->operationsMap.keys().isEmpty() || !d->operationsMap.contains(operation)) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
d->disabledOperations.remove(operation);
|
2008-12-11 03:47:11 +01:00
|
|
|
} else {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->disabledOperations.insert(operation);
|
|
|
|
}
|
|
|
|
|
2013-05-02 18:11:20 +02:00
|
|
|
emit operationEnabledChanged(operation, enable);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::isOperationEnabled(const QString &operation) const
|
|
|
|
{
|
2013-05-02 14:46:29 +02:00
|
|
|
return d->operationsMap.contains(operation) && !d->disabledOperations.contains(operation);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setOperationsScheme(QIODevice *xml)
|
|
|
|
{
|
2013-05-02 14:46:29 +02:00
|
|
|
d->operationsMap.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2013-05-02 14:46:29 +02:00
|
|
|
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;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::registerOperationsScheme()
|
|
|
|
{
|
2013-05-02 14:46:29 +02:00
|
|
|
if (!d->operationsMap.keys().isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
// we've already done our job. let's go home.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->name.isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "No name found";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-30 20:25:25 +02:00
|
|
|
const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "plasma/services/" + d->name + ".operations");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2013-07-29 19:05:59 +02:00
|
|
|
// qDebug() << "Cannot find operations description:" << d->name << ".operations";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
setOperationsScheme(&file);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
2012-02-08 23:33:03 +01:00
|
|
|
#include "moc_service.cpp"
|