add ConfigGroup here as well
This commit is contained in:
parent
7a7914126e
commit
a1fec8dba8
@ -38,6 +38,7 @@ set(scripting_SRC
|
||||
scripting/appinterface.cpp
|
||||
scripting/applet.cpp
|
||||
scripting/containment.cpp
|
||||
scripting/configgroup.cpp
|
||||
scripting/desktopscriptengine.cpp
|
||||
scripting/i18n.cpp
|
||||
scripting/layouttemplatepackagestructure.cpp
|
||||
|
187
src/shell/scripting/configgroup.cpp
Normal file
187
src/shell/scripting/configgroup.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright 2011-2012 by Sebastian Kügler <sebas@kde.org>
|
||||
* Copyright 2013 by 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 "configgroup.h"
|
||||
#include <QtCore/QTimer>
|
||||
#include <KConfig>
|
||||
#include <KConfigGroup>
|
||||
#include <KDebug>
|
||||
|
||||
class ConfigGroupPrivate {
|
||||
|
||||
public:
|
||||
ConfigGroupPrivate(ConfigGroup *q)
|
||||
: q(q),
|
||||
config(0),
|
||||
configGroup(0)
|
||||
{}
|
||||
|
||||
~ConfigGroupPrivate()
|
||||
{
|
||||
delete configGroup;
|
||||
}
|
||||
|
||||
ConfigGroup* q;
|
||||
KSharedConfigPtr config;
|
||||
KConfigGroup *configGroup;
|
||||
QString file;
|
||||
QTimer *synchTimer;
|
||||
QString group;
|
||||
};
|
||||
|
||||
|
||||
ConfigGroup::ConfigGroup(QObject *parent)
|
||||
: QObject(parent),
|
||||
d(new ConfigGroupPrivate(this))
|
||||
{
|
||||
// Delay and compress everything within 5 seconds into one sync
|
||||
d->synchTimer = new QTimer(this);
|
||||
d->synchTimer->setSingleShot(true);
|
||||
d->synchTimer->setInterval(1500);
|
||||
connect(d->synchTimer, SIGNAL(timeout()), SLOT(sync()));
|
||||
}
|
||||
|
||||
ConfigGroup::~ConfigGroup()
|
||||
{
|
||||
if (d->synchTimer->isActive()) {
|
||||
//kDebug() << "SYNC......";
|
||||
d->synchTimer->stop();
|
||||
d->configGroup->sync();
|
||||
}
|
||||
|
||||
delete d;
|
||||
}
|
||||
|
||||
KConfigGroup* ConfigGroup::configGroup()
|
||||
{
|
||||
return d->configGroup;
|
||||
}
|
||||
|
||||
QString ConfigGroup::file() const
|
||||
{
|
||||
return d->file;
|
||||
}
|
||||
|
||||
void ConfigGroup::setFile(const QString& filename)
|
||||
{
|
||||
if (d->file == filename) {
|
||||
return;
|
||||
}
|
||||
d->file = filename;
|
||||
readConfigFile();
|
||||
emit fileChanged();
|
||||
}
|
||||
|
||||
QString ConfigGroup::group() const
|
||||
{
|
||||
return d->group;
|
||||
}
|
||||
|
||||
void ConfigGroup::setGroup(const QString& groupname)
|
||||
{
|
||||
if (d->group == groupname) {
|
||||
return;
|
||||
}
|
||||
d->group = groupname;
|
||||
readConfigFile();
|
||||
emit groupChanged();
|
||||
emit keyListChanged();
|
||||
}
|
||||
|
||||
QStringList ConfigGroup::keyList() const
|
||||
{
|
||||
if (!d->configGroup) {
|
||||
return QStringList();
|
||||
}
|
||||
return d->configGroup->keyList();
|
||||
}
|
||||
|
||||
QStringList ConfigGroup::groupList() const
|
||||
{
|
||||
return d->configGroup->groupList();
|
||||
}
|
||||
|
||||
bool ConfigGroup::readConfigFile()
|
||||
{
|
||||
// Find parent ConfigGroup
|
||||
ConfigGroup* parentGroup = 0;
|
||||
QObject* current = parent();
|
||||
while (current) {
|
||||
parentGroup = qobject_cast<ConfigGroup*>(current);
|
||||
if (parentGroup) {
|
||||
break;
|
||||
}
|
||||
current = current->parent();
|
||||
}
|
||||
|
||||
delete d->configGroup;
|
||||
d->configGroup = 0;
|
||||
|
||||
if (parentGroup) {
|
||||
d->configGroup = new KConfigGroup(parentGroup->configGroup(), d->group);
|
||||
return true;
|
||||
} else {
|
||||
if (d->file.isEmpty()) {
|
||||
kWarning() << "Could not find KConfig Parent: specify a file or parent to another ConfigGroup";
|
||||
return false;
|
||||
}
|
||||
d->config = KSharedConfig::openConfig(d->file);
|
||||
d->configGroup = new KConfigGroup(d->config, d->group);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Bound methods and slots
|
||||
|
||||
bool ConfigGroup::writeEntry(const QString& key, const QVariant& value)
|
||||
{
|
||||
if (!d->configGroup) {
|
||||
return false;
|
||||
}
|
||||
|
||||
d->configGroup->writeEntry(key, value);
|
||||
d->synchTimer->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
QVariant ConfigGroup::readEntry(const QString& key)
|
||||
{
|
||||
if (!d->configGroup) {
|
||||
return QVariant();
|
||||
}
|
||||
const QVariant value = d->configGroup->readEntry(key, QVariant(""));
|
||||
//kDebug() << " reading setting: " << key << value;
|
||||
return value;
|
||||
}
|
||||
|
||||
void ConfigGroup::deleteEntry(const QString& key)
|
||||
{
|
||||
d->configGroup->deleteEntry(key);
|
||||
}
|
||||
|
||||
void ConfigGroup::sync()
|
||||
{
|
||||
if (d->configGroup) {
|
||||
//kDebug() << "synching config...";
|
||||
d->configGroup->sync();
|
||||
}
|
||||
}
|
||||
|
||||
#include "configgroup.moc"
|
69
src/shell/scripting/configgroup.h
Normal file
69
src/shell/scripting/configgroup.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2011-2012 by Sebastian Kügler <sebas@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.
|
||||
*/
|
||||
|
||||
#ifndef CONFIGGROUP_H
|
||||
#define CONFIGGROUP_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
class KConfigGroup;
|
||||
class ConfigGroupPrivate;
|
||||
|
||||
class ConfigGroup : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString file READ file WRITE setFile NOTIFY fileChanged)
|
||||
Q_PROPERTY(QString group READ group WRITE setGroup NOTIFY groupChanged)
|
||||
Q_PROPERTY(QStringList keyList READ keyList NOTIFY keyListChanged)
|
||||
Q_PROPERTY(QStringList groupList READ groupList NOTIFY groupListChanged)
|
||||
|
||||
public:
|
||||
ConfigGroup(QObject* parent=0);
|
||||
~ConfigGroup();
|
||||
|
||||
KConfigGroup* configGroup();
|
||||
|
||||
QString file() const;
|
||||
void setFile(const QString &filename);
|
||||
QString group() const;
|
||||
void setGroup(const QString &groupname);
|
||||
QStringList keyList() const;
|
||||
QStringList groupList() const;
|
||||
|
||||
Q_INVOKABLE QVariant readEntry(const QString &key);
|
||||
Q_INVOKABLE bool writeEntry(const QString &key, const QVariant &value);
|
||||
Q_INVOKABLE void deleteEntry(const QString& key);
|
||||
|
||||
Q_SIGNALS:
|
||||
void fileChanged();
|
||||
void groupChanged();
|
||||
void keyListChanged();
|
||||
void groupListChanged();
|
||||
|
||||
private:
|
||||
ConfigGroupPrivate* d;
|
||||
|
||||
bool readConfigFile();
|
||||
|
||||
private Q_SLOTS:
|
||||
void sync();
|
||||
};
|
||||
|
||||
#endif
|
@ -49,6 +49,7 @@
|
||||
|
||||
#include "appinterface.h"
|
||||
#include "containment.h"
|
||||
#include "configgroup.h"
|
||||
#include "i18n.h"
|
||||
#include "layouttemplatepackagestructure.h"
|
||||
#include "widget.h"
|
||||
@ -561,6 +562,32 @@ QScriptValue ScriptEngine::knownWallpaperPlugins(QScriptContext *context, QScrip
|
||||
return rv;
|
||||
}
|
||||
|
||||
QScriptValue ScriptEngine::configFile(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
ConfigGroup *file = 0;
|
||||
|
||||
if (context->argumentCount() > 0) {
|
||||
if (context->argument(0).isString()) {
|
||||
file = new ConfigGroup;
|
||||
file->setFile(context->argument(0).toString());
|
||||
if (context->argumentCount() > 1) {
|
||||
file->setGroup(context->argument(1).toString());
|
||||
}
|
||||
} else if (ConfigGroup *parent= qobject_cast<ConfigGroup *>(context->argument(0).toQObject())) {
|
||||
file = new ConfigGroup(parent);
|
||||
}
|
||||
} else {
|
||||
file = new ConfigGroup;
|
||||
}
|
||||
|
||||
QScriptValue v = engine->newQObject(file,
|
||||
QScriptEngine::ScriptOwnership,
|
||||
QScriptEngine::ExcludeSuperClassProperties |
|
||||
QScriptEngine::ExcludeSuperClassMethods);
|
||||
return v;
|
||||
|
||||
}
|
||||
|
||||
void ScriptEngine::setupEngine()
|
||||
{
|
||||
QScriptValue v = globalObject();
|
||||
@ -588,6 +615,7 @@ void ScriptEngine::setupEngine()
|
||||
m_scriptSelf.setProperty("userDataPath", newFunction(ScriptEngine::userDataPath));
|
||||
m_scriptSelf.setProperty("applicationPath", newFunction(ScriptEngine::applicationPath));
|
||||
m_scriptSelf.setProperty("knownWallpaperPlugins", newFunction(ScriptEngine::knownWallpaperPlugins));
|
||||
m_scriptSelf.setProperty("ConfigFile", newFunction(ScriptEngine::configFile));
|
||||
|
||||
setGlobalObject(m_scriptSelf);
|
||||
}
|
||||
|
@ -79,6 +79,7 @@ private:
|
||||
static QScriptValue applicationPath(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue userDataPath(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue knownWallpaperPlugins(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue configFile(QScriptContext *context, QScriptEngine *engine);
|
||||
|
||||
// helpers
|
||||
static QScriptValue createContainment(const QString &type, const QString &defautPlugin,
|
||||
@ -92,7 +93,7 @@ private:
|
||||
QScriptValue m_scriptSelf;
|
||||
};
|
||||
|
||||
static const int PLASMA_DESKTOP_SCRIPTING_VERSION = 6;
|
||||
static const int PLASMA_DESKTOP_SCRIPTING_VERSION = 20;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user