2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 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 "dataengine.h"
|
|
|
|
#include "private/dataengine_p.h"
|
2010-10-01 18:58:15 +02:00
|
|
|
#include "private/datacontainer_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <QQueue>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QTime>
|
|
|
|
#include <QTimerEvent>
|
|
|
|
#include <QVariant>
|
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kplugininfo.h>
|
2012-12-29 01:36:40 +01:00
|
|
|
#include <kglobal.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kservice.h>
|
2012-09-24 15:51:14 +02:00
|
|
|
#include <kservicetypetrader.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kstandarddirs.h>
|
2012-11-18 20:26:13 +01:00
|
|
|
#include <klocalizedstring.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "datacontainer.h"
|
|
|
|
#include "package.h"
|
2011-07-19 22:04:22 +02:00
|
|
|
#include "pluginloader.h"
|
2011-04-29 15:18:35 +02:00
|
|
|
#include "remote/authorizationmanager.h"
|
|
|
|
#include "remote/authorizationmanager_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
#include "service.h"
|
|
|
|
#include "scripting/dataenginescript.h"
|
|
|
|
|
2011-06-03 16:04:50 +02:00
|
|
|
#include "private/datacontainer_p.h"
|
2009-11-25 02:09:17 +01:00
|
|
|
#include "private/dataengineservice_p.h"
|
2010-02-18 11:05:31 +01:00
|
|
|
#include "private/remotedataengine_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
#include "private/service_p.h"
|
2010-07-12 22:51:12 +02:00
|
|
|
#include "private/storage_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
DataEngine::DataEngine(QObject *parent, KService::Ptr service)
|
|
|
|
: QObject(parent),
|
2010-08-06 03:26:11 +02:00
|
|
|
d(new DataEnginePrivate(this, KPluginInfo(service)))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::DataEngine(QObject *parent, const QVariantList &args)
|
|
|
|
: QObject(parent),
|
2010-08-06 03:26:11 +02:00
|
|
|
d(new DataEnginePrivate(this, KPluginInfo(KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString()))))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::~DataEngine()
|
|
|
|
{
|
|
|
|
//kDebug() << objectName() << ": bye bye birdy! ";
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataEngine::sources() const
|
|
|
|
{
|
2008-11-08 21:44:03 +01:00
|
|
|
if (d->script) {
|
|
|
|
return d->script->sources();
|
|
|
|
} else {
|
|
|
|
return d->sources.keys();
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Service *DataEngine::serviceForSource(const QString &source)
|
|
|
|
{
|
2008-11-08 21:44:03 +01:00
|
|
|
if (d->script) {
|
2010-03-10 22:41:37 +01:00
|
|
|
Service * s = d->script->serviceForSource(source);
|
|
|
|
if (s) {
|
|
|
|
return s;
|
|
|
|
}
|
2008-11-08 21:44:03 +01:00
|
|
|
}
|
2010-03-10 22:41:37 +01:00
|
|
|
|
|
|
|
return new NullService(source, this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::connectSource(const QString &source, QObject *visualization,
|
|
|
|
uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment intervalAlignment) const
|
|
|
|
{
|
|
|
|
//kDebug() << "connectSource" << source;
|
|
|
|
bool newSource;
|
|
|
|
DataContainer *s = d->requestSource(source, &newSource);
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
// we suppress the immediate invocation of dataUpdated here if the
|
|
|
|
// source was prexisting and they don't request delayed updates
|
|
|
|
// (we want to do an immediate update in that case so they don't
|
|
|
|
// have to wait for the first time out)
|
2010-04-20 21:50:41 +02:00
|
|
|
if (newSource && !s->data().isEmpty()) {
|
|
|
|
newSource = false;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
d->connectSource(s, visualization, pollingInterval, intervalAlignment,
|
|
|
|
!newSource || pollingInterval > 0);
|
|
|
|
//kDebug() << " ==> source connected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::connectAllSources(QObject *visualization, uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment intervalAlignment) const
|
|
|
|
{
|
|
|
|
foreach (DataContainer *s, d->sources) {
|
|
|
|
d->connectSource(s, visualization, pollingInterval, intervalAlignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::disconnectSource(const QString &source, QObject *visualization) const
|
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
s->disconnectVisualization(visualization);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataContainer *DataEngine::containerForSource(const QString &source)
|
|
|
|
{
|
|
|
|
return d->source(source, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::Data DataEngine::query(const QString &source) const
|
|
|
|
{
|
|
|
|
bool newSource;
|
|
|
|
DataContainer *s = d->requestSource(source, &newSource);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
return DataEngine::Data();
|
|
|
|
} else if (!newSource && d->minPollingInterval >= 0 &&
|
|
|
|
s->timeSinceLastUpdate() >= uint(d->minPollingInterval)) {
|
2009-05-09 02:32:45 +02:00
|
|
|
DataEngine *unconstThis = const_cast<DataEngine*>(this);
|
|
|
|
if (unconstThis->updateSourceEvent(source)) {
|
|
|
|
unconstThis->scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::Data data = s->data();
|
2011-06-03 16:04:50 +02:00
|
|
|
s->d->checkUsage();
|
2008-11-04 00:08:39 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::init()
|
|
|
|
{
|
|
|
|
if (d->script) {
|
2009-11-12 07:38:16 +01:00
|
|
|
d->setupScriptSupport();
|
2008-11-04 00:08:39 +01:00
|
|
|
d->script->init();
|
|
|
|
} else {
|
|
|
|
// kDebug() << "called";
|
|
|
|
// default implementation does nothing. this is for engines that have to
|
|
|
|
// start things in motion external to themselves before they can work
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEngine::sourceRequestEvent(const QString &name)
|
|
|
|
{
|
|
|
|
if (d->script) {
|
|
|
|
return d->script->sourceRequestEvent(name);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEngine::updateSourceEvent(const QString &source)
|
|
|
|
{
|
|
|
|
if (d->script) {
|
|
|
|
return d->script->updateSourceEvent(source);
|
|
|
|
} else {
|
|
|
|
//kDebug() << source;
|
|
|
|
return false; //TODO: should this be true to trigger, even needless, updates on every tick?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setData(const QString &source, const QVariant &value)
|
|
|
|
{
|
|
|
|
setData(source, source, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setData(const QString &source, const QString &key, const QVariant &value)
|
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
bool isNew = !s;
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
s = d->source(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->setData(key, value);
|
|
|
|
|
2011-09-01 19:07:22 +02:00
|
|
|
if (isNew && source != d->waitingSourceRequest) {
|
2008-11-04 00:08:39 +01:00
|
|
|
emit sourceAdded(source);
|
|
|
|
}
|
|
|
|
|
2009-05-09 02:32:45 +02:00
|
|
|
scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setData(const QString &source, const Data &data)
|
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
bool isNew = !s;
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
s = d->source(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::const_iterator it = data.constBegin();
|
|
|
|
while (it != data.constEnd()) {
|
|
|
|
s->setData(it.key(), it.value());
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:07:22 +02:00
|
|
|
if (isNew && source != d->waitingSourceRequest) {
|
2008-11-04 00:08:39 +01:00
|
|
|
emit sourceAdded(source);
|
|
|
|
}
|
|
|
|
|
2009-05-09 02:32:45 +02:00
|
|
|
scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::removeAllData(const QString &source)
|
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
if (s) {
|
|
|
|
s->removeAllData();
|
2009-05-09 02:32:45 +02:00
|
|
|
scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::removeData(const QString &source, const QString &key)
|
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
if (s) {
|
|
|
|
s->setData(key, QVariant());
|
2009-05-09 02:32:45 +02:00
|
|
|
scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::addSource(DataContainer *source)
|
|
|
|
{
|
|
|
|
if (d->sources.contains(source->objectName())) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "source named \"" << source->objectName() << "\" already exists.";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject::connect(source, SIGNAL(updateRequested(DataContainer*)),
|
|
|
|
this, SLOT(internalUpdateSource(DataContainer*)));
|
2010-06-10 22:38:36 +02:00
|
|
|
QObject::connect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed(QObject*)));
|
2008-11-04 00:08:39 +01:00
|
|
|
d->sources.insert(source->objectName(), source);
|
|
|
|
emit sourceAdded(source->objectName());
|
2009-05-09 02:32:45 +02:00
|
|
|
scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setMinimumPollingInterval(int minimumMs)
|
|
|
|
{
|
|
|
|
d->minPollingInterval = minimumMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DataEngine::minimumPollingInterval() const
|
|
|
|
{
|
|
|
|
return d->minPollingInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setPollingInterval(uint frequency)
|
|
|
|
{
|
|
|
|
killTimer(d->updateTimerId);
|
|
|
|
d->updateTimerId = 0;
|
|
|
|
|
|
|
|
if (frequency > 0) {
|
|
|
|
d->updateTimerId = startTimer(frequency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::removeSource(const QString &source)
|
|
|
|
{
|
|
|
|
SourceDict::iterator it = d->sources.find(source);
|
|
|
|
if (it != d->sources.end()) {
|
|
|
|
DataContainer *s = it.value();
|
2010-10-01 18:58:15 +02:00
|
|
|
s->d->store();
|
2010-06-09 21:09:13 +02:00
|
|
|
s->disconnect(this);
|
2008-11-04 00:08:39 +01:00
|
|
|
s->deleteLater();
|
|
|
|
d->sources.erase(it);
|
|
|
|
emit sourceRemoved(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::removeAllSources()
|
|
|
|
{
|
|
|
|
QMutableHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2010-05-04 05:28:09 +02:00
|
|
|
Plasma::DataContainer *s = it.value();
|
2011-11-30 15:19:58 +01:00
|
|
|
const QString source = it.key();
|
2011-11-30 09:17:01 +01:00
|
|
|
it.remove();
|
2011-10-03 14:06:34 +02:00
|
|
|
s->disconnect(this);
|
2011-11-28 10:37:21 +01:00
|
|
|
s->deleteLater();
|
2011-11-30 09:17:01 +01:00
|
|
|
emit sourceRemoved(source);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEngine::isValid() const
|
|
|
|
{
|
|
|
|
return d->valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEngine::isEmpty() const
|
|
|
|
{
|
|
|
|
return d->sources.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setValid(bool valid)
|
|
|
|
{
|
|
|
|
d->valid = valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::SourceDict DataEngine::containerDict() const
|
|
|
|
{
|
|
|
|
return d->sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
2009-09-03 21:31:26 +02:00
|
|
|
//kDebug();
|
2009-05-09 02:32:45 +02:00
|
|
|
if (event->timerId() == d->updateTimerId) {
|
|
|
|
// if the freq update is less than 0, don't bother
|
|
|
|
if (d->minPollingInterval < 0) {
|
2009-09-03 21:31:26 +02:00
|
|
|
//kDebug() << "uh oh.. no polling allowed!";
|
2009-05-09 02:32:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-05-09 02:32:45 +02:00
|
|
|
// minPollingInterval
|
|
|
|
if (d->updateTimestamp.elapsed() < d->minPollingInterval) {
|
2009-09-03 21:31:26 +02:00
|
|
|
//kDebug() << "hey now.. slow down!";
|
2009-05-09 02:32:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-05-09 02:32:45 +02:00
|
|
|
d->updateTimestamp.restart();
|
|
|
|
updateAllSources();
|
|
|
|
} else if (event->timerId() == d->checkSourcesTimerId) {
|
2009-08-02 23:56:51 +02:00
|
|
|
killTimer(d->checkSourcesTimerId);
|
|
|
|
d->checkSourcesTimerId = 0;
|
|
|
|
|
2009-05-09 02:32:45 +02:00
|
|
|
QHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.value()->checkForUpdate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QObject::timerEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2008-11-13 08:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::updateAllSources()
|
|
|
|
{
|
2008-11-04 00:08:39 +01:00
|
|
|
QHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
//kDebug() << "updating" << it.key();
|
|
|
|
updateSourceEvent(it.key());
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduleSourcesUpdated();
|
|
|
|
}
|
|
|
|
|
2009-06-27 03:26:33 +02:00
|
|
|
void DataEngine::forceImmediateUpdateOfAllVisualizations()
|
|
|
|
{
|
|
|
|
foreach (DataContainer *source, d->sources) {
|
|
|
|
source->forceImmediateUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void DataEngine::setIcon(const QString &icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DataEngine::icon() const
|
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DataEngine::pluginName() const
|
|
|
|
{
|
|
|
|
if (!d->dataEngineDescription.isValid()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->dataEngineDescription.pluginName();
|
|
|
|
}
|
|
|
|
|
2010-05-14 19:41:40 +02:00
|
|
|
void DataEngine::setDefaultService(const QString &serviceName)
|
|
|
|
{
|
|
|
|
d->serviceName = serviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Service* DataEngine::createDefaultService(QObject *parent)
|
|
|
|
{
|
|
|
|
QVariantList args;
|
|
|
|
args << QVariant::fromValue<DataEngine*>(this);
|
2011-07-19 22:04:22 +02:00
|
|
|
return PluginLoader::self()->loadService(d->serviceName, args, parent);
|
2010-05-14 19:41:40 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
void DataEnginePrivate::publish(AnnouncementMethods methods, const QString &name)
|
|
|
|
{
|
|
|
|
if (!publishedService) {
|
|
|
|
publishedService = new DataEngineService(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
//QString resourceName =
|
|
|
|
//i18nc("%1 is the name of a dataengine, %2 the name of the machine that engine is published
|
|
|
|
//on",
|
|
|
|
//"%1 dataengine on %2", name(), AuthorizationManager::self()->d->myCredentials.name());
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "name: " << name;
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
publishedService->d->publish(methods, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEnginePrivate::unpublish(const QString &name)
|
|
|
|
{
|
2009-09-21 20:15:42 +02:00
|
|
|
Q_UNUSED(name)
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
if (publishedService) {
|
|
|
|
publishedService->d->unpublish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEnginePrivate::isPublished() const
|
|
|
|
{
|
|
|
|
if (publishedService) {
|
|
|
|
return publishedService->d->isPublished();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 13:01:17 +02:00
|
|
|
Package DataEngine::package() const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2011-07-15 13:01:17 +02:00
|
|
|
return d->package ? *d->package : Package();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::scheduleSourcesUpdated()
|
|
|
|
{
|
2009-05-09 02:32:45 +02:00
|
|
|
if (d->checkSourcesTimerId) {
|
|
|
|
return;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-05-09 02:32:45 +02:00
|
|
|
|
|
|
|
d->checkSourcesTimerId = startTimer(0);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DataEngine::name() const
|
|
|
|
{
|
|
|
|
return d->engineName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::setName(const QString &name)
|
|
|
|
{
|
|
|
|
d->engineName = name;
|
|
|
|
setObjectName(name);
|
|
|
|
}
|
|
|
|
|
2010-08-25 23:35:40 +02:00
|
|
|
void DataEngine::setStorageEnabled(const QString &source, bool store)
|
2010-07-12 22:51:12 +02:00
|
|
|
{
|
|
|
|
DataContainer *s = d->source(source, false);
|
2010-10-01 18:58:15 +02:00
|
|
|
if (s) {
|
|
|
|
s->setStorageEnabled(store);
|
|
|
|
}
|
2010-07-12 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
// Private class implementations
|
2010-08-06 03:26:11 +02:00
|
|
|
DataEnginePrivate::DataEnginePrivate(DataEngine *e, const KPluginInfo &info)
|
2008-11-04 00:08:39 +01:00
|
|
|
: q(e),
|
2010-08-06 03:26:11 +02:00
|
|
|
dataEngineDescription(info),
|
2008-11-04 00:08:39 +01:00
|
|
|
refCount(-1), // first ref
|
2009-05-09 02:32:45 +02:00
|
|
|
checkSourcesTimerId(0),
|
2008-11-04 00:08:39 +01:00
|
|
|
updateTimerId(0),
|
|
|
|
minPollingInterval(-1),
|
|
|
|
valid(true),
|
|
|
|
script(0),
|
2009-09-02 04:27:16 +02:00
|
|
|
package(0),
|
|
|
|
publishedService(0)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
updateTimestamp.start();
|
|
|
|
|
2010-08-06 03:26:11 +02:00
|
|
|
if (!info.isValid()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
engineName = i18n("Unnamed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-06 03:26:11 +02:00
|
|
|
engineName = info.name();
|
2008-11-04 00:08:39 +01:00
|
|
|
if (engineName.isEmpty()) {
|
|
|
|
engineName = i18n("Unnamed");
|
|
|
|
}
|
|
|
|
e->setObjectName(engineName);
|
2010-08-06 03:26:11 +02:00
|
|
|
icon = info.icon();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (dataEngineDescription.isValid()) {
|
|
|
|
QString api = dataEngineDescription.property("X-Plasma-API").toString();
|
|
|
|
|
|
|
|
if (!api.isEmpty()) {
|
|
|
|
const QString path =
|
2013-02-08 11:41:31 +01:00
|
|
|
QStandardPaths::locate(QStandardPaths::GenericDataLocation,
|
|
|
|
"plasma/dataengines/" + dataEngineDescription.pluginName() + '/',
|
|
|
|
QStandardPaths::LocateDirectory);
|
2011-07-19 22:04:22 +02:00
|
|
|
package = new Package(PluginLoader::self()->loadPackage("Plasma/DataEngine", api));
|
2011-07-15 13:01:17 +02:00
|
|
|
package->setPath(path);
|
|
|
|
|
|
|
|
if (package->isValid()) {
|
|
|
|
script = Plasma::loadScriptEngine(api, q);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (!script) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "Could not create a" << api << "ScriptEngine for the"
|
|
|
|
<< dataEngineDescription.name() << "DataEngine.";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
delete package;
|
|
|
|
package = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEnginePrivate::~DataEnginePrivate()
|
|
|
|
{
|
|
|
|
delete script;
|
|
|
|
script = 0;
|
|
|
|
delete package;
|
|
|
|
package = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEnginePrivate::internalUpdateSource(DataContainer *source)
|
|
|
|
{
|
|
|
|
if (minPollingInterval > 0 &&
|
|
|
|
source->timeSinceLastUpdate() < (uint)minPollingInterval) {
|
|
|
|
// skip updating this source; it's been too soon
|
|
|
|
//kDebug() << "internal update source is delaying" << source->timeSinceLastUpdate() << minPollingInterval;
|
|
|
|
//but fake an update so that the signalrelay that triggered this gets the data from the
|
|
|
|
//recent update. this way we don't have to worry about queuing - the relay will send a
|
|
|
|
//signal immediately and everyone else is undisturbed.
|
|
|
|
source->setNeedsUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (q->updateSourceEvent(source->objectName())) {
|
|
|
|
//kDebug() << "queuing an update";
|
2009-05-09 02:32:45 +02:00
|
|
|
q->scheduleSourcesUpdated();
|
2008-11-04 00:08:39 +01:00
|
|
|
}/* else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "no update";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEnginePrivate::ref()
|
|
|
|
{
|
|
|
|
--refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEnginePrivate::deref()
|
|
|
|
{
|
|
|
|
++refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEnginePrivate::isUsed() const
|
|
|
|
{
|
|
|
|
return refCount != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataContainer *DataEnginePrivate::source(const QString &sourceName, bool createWhenMissing)
|
|
|
|
{
|
2009-01-06 14:09:05 +01:00
|
|
|
DataEngine::SourceDict::const_iterator it = sources.constFind(sourceName);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (it != sources.constEnd()) {
|
|
|
|
DataContainer *s = it.value();
|
2010-08-25 23:17:54 +02:00
|
|
|
return s;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!createWhenMissing) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-04 23:15:10 +02:00
|
|
|
//kDebug() << "DataEngine " << q->objectName() << ": could not find DataContainer " << sourceName << ", creating";
|
2008-11-04 00:08:39 +01:00
|
|
|
DataContainer *s = new DataContainer(q);
|
|
|
|
s->setObjectName(sourceName);
|
|
|
|
sources.insert(sourceName, s);
|
2012-01-22 03:11:26 +01:00
|
|
|
QObject::connect(s, SIGNAL(destroyed(QObject*)), q, SLOT(sourceDestroyed(QObject*)));
|
2008-11-04 00:08:39 +01:00
|
|
|
QObject::connect(s, SIGNAL(updateRequested(DataContainer*)),
|
|
|
|
q, SLOT(internalUpdateSource(DataContainer*)));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEnginePrivate::connectSource(DataContainer *s, QObject *visualization,
|
|
|
|
uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment align,
|
|
|
|
bool immediateCall)
|
|
|
|
{
|
|
|
|
//kDebug() << "connect source called" << s->objectName() << "with interval" << pollingInterval;
|
2010-02-18 11:05:31 +01:00
|
|
|
|
|
|
|
//FIXME: at the moment a remote dataengine can only poll, a push mechanism will be needed instead
|
|
|
|
if (pollingInterval == 0 && qobject_cast<RemoteDataEngine *>(q)) {
|
|
|
|
pollingInterval = 5000;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
if (pollingInterval > 0) {
|
|
|
|
// never more frequently than allowed, never more than 20 times per second
|
|
|
|
uint min = qMax(50, minPollingInterval); // for qMax below
|
|
|
|
pollingInterval = qMax(min, pollingInterval);
|
|
|
|
|
|
|
|
// align on the 50ms
|
|
|
|
pollingInterval = pollingInterval - (pollingInterval % 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (immediateCall) {
|
|
|
|
// we don't want to do an immediate call if we are simply
|
|
|
|
// reconnecting
|
|
|
|
//kDebug() << "immediate call requested, we have:" << s->visualizationIsConnected(visualization);
|
2009-08-20 11:34:36 +02:00
|
|
|
immediateCall = !s->data().isEmpty() &&
|
|
|
|
!s->visualizationIsConnected(visualization);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s->connectVisualization(visualization, pollingInterval, align);
|
|
|
|
|
|
|
|
if (immediateCall) {
|
|
|
|
QMetaObject::invokeMethod(visualization, "dataUpdated",
|
|
|
|
Q_ARG(QString, s->objectName()),
|
|
|
|
Q_ARG(Plasma::DataEngine::Data, s->data()));
|
2011-03-02 22:04:53 +01:00
|
|
|
s->d->dirty = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 20:04:28 +02:00
|
|
|
void DataEnginePrivate::sourceDestroyed(QObject *object)
|
|
|
|
{
|
2010-06-09 21:09:13 +02:00
|
|
|
DataEngine::SourceDict::iterator it = sources.begin();
|
|
|
|
while (it != sources.end()) {
|
2010-06-09 20:04:28 +02:00
|
|
|
if (it.value() == object) {
|
2010-06-09 21:09:13 +02:00
|
|
|
sources.erase(it);
|
2011-11-28 10:42:40 +01:00
|
|
|
emit q->sourceRemoved(object->objectName());
|
2010-06-09 20:04:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
DataContainer *DataEnginePrivate::requestSource(const QString &sourceName, bool *newSource)
|
|
|
|
{
|
|
|
|
if (newSource) {
|
|
|
|
*newSource = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "requesting source " << sourceName;
|
|
|
|
DataContainer *s = source(sourceName, false);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
// we didn't find a data source, so give the engine an opportunity to make one
|
|
|
|
/*kDebug() << "DataEngine " << q->objectName()
|
|
|
|
<< ": could not find DataContainer " << sourceName
|
|
|
|
<< " will create on request" << endl;*/
|
2011-09-01 19:07:22 +02:00
|
|
|
waitingSourceRequest = sourceName;
|
2008-11-04 00:08:39 +01:00
|
|
|
if (q->sourceRequestEvent(sourceName)) {
|
|
|
|
s = source(sourceName, false);
|
|
|
|
if (s) {
|
|
|
|
// now we have a source; since it was created on demand, assume
|
|
|
|
// it should be removed when not used
|
|
|
|
if (newSource) {
|
|
|
|
*newSource = true;
|
|
|
|
}
|
|
|
|
QObject::connect(s, SIGNAL(becameUnused(QString)), q, SLOT(removeSource(QString)));
|
2011-09-01 19:07:22 +02:00
|
|
|
emit q->sourceAdded(sourceName);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-01 19:07:22 +02:00
|
|
|
waitingSourceRequest.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-11-12 07:38:16 +01:00
|
|
|
// put all setup routines for script here. at this point we can assume that
|
|
|
|
// package exists and that we have a script engine
|
|
|
|
void DataEnginePrivate::setupScriptSupport()
|
|
|
|
{
|
|
|
|
if (!package) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-06 03:26:11 +02:00
|
|
|
/*
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-08-06 03:26:11 +02:00
|
|
|
kDebug() << "sletting up script support, package is in" << package->path()
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-11-12 07:38:16 +01:00
|
|
|
<< "which is a" << package->structure()->type() << "package"
|
|
|
|
<< ", main script is" << package->filePath("mainscript");
|
2010-08-06 03:26:11 +02:00
|
|
|
*/
|
2009-11-12 07:38:16 +01:00
|
|
|
|
2011-05-31 00:09:07 +02:00
|
|
|
const QString translationsPath = package->filePath("translations");
|
2009-11-12 07:38:16 +01:00
|
|
|
if (!translationsPath.isEmpty()) {
|
|
|
|
KGlobal::dirs()->addResourceDir("locale", translationsPath);
|
2012-11-18 20:26:13 +01:00
|
|
|
KLocalizedString::insertCatalog(dataEngineDescription.pluginName());
|
2009-11-12 07:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 15:51:14 +02:00
|
|
|
QStringList DataEngine::listAllEngines(const QString &parentApp)
|
|
|
|
{
|
|
|
|
QString constraint;
|
|
|
|
|
|
|
|
if (parentApp.isEmpty()) {
|
|
|
|
constraint.append("(not exist [X-KDE-ParentApp] or [X-KDE-ParentApp] == '')");
|
|
|
|
} else {
|
|
|
|
constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
|
|
|
|
|
|
|
|
QStringList engines;
|
|
|
|
foreach (const KService::Ptr &service, offers) {
|
|
|
|
QString name = service->property("X-KDE-PluginInfo-Name").toString();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
engines.append(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
KPluginInfo::List DataEngine::listEngineInfo(const QString &parentApp)
|
|
|
|
{
|
|
|
|
return PluginLoader::self()->listDataEngineInfo(parentApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
KPluginInfo::List DataEngine::listEngineInfoByCategory(const QString &category, const QString &parentApp)
|
|
|
|
{
|
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(category);
|
|
|
|
|
|
|
|
if (parentApp.isEmpty()) {
|
|
|
|
constraint.append(" and not exist [X-KDE-ParentApp]");
|
|
|
|
} else {
|
|
|
|
constraint.append(" and [X-KDE-ParentApp] == '").append(parentApp).append("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
|
|
|
|
return KPluginInfo::fromServices(offers);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 23:33:03 +01:00
|
|
|
|
|
|
|
#include "moc_dataengine.cpp"
|