2006-12-17 00:04:44 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2006-12-17 00:04:44 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +02:00
|
|
|
* 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.
|
2006-12-17 00:04:44 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "dataengine.h"
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/dataengine_p.h"
|
2007-05-21 16:28:03 +02:00
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
#include <QQueue>
|
2007-05-19 10:38:46 +02:00
|
|
|
#include <QTimer>
|
2007-10-06 00:21:25 +02:00
|
|
|
#include <QTime>
|
2007-09-11 02:49:51 +02:00
|
|
|
#include <QTimerEvent>
|
2007-03-01 09:22:38 +01:00
|
|
|
#include <QVariant>
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include <KDebug>
|
2008-02-04 05:41:40 +01:00
|
|
|
#include <KPluginInfo>
|
|
|
|
#include <KService>
|
2008-04-13 17:01:46 +02:00
|
|
|
#include <KStandardDirs>
|
2007-05-19 10:38:46 +02:00
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
#include "datacontainer.h"
|
2008-04-13 17:01:46 +02:00
|
|
|
#include "package.h"
|
2008-05-18 06:27:54 +02:00
|
|
|
#include "service.h"
|
2008-02-04 05:41:40 +01:00
|
|
|
#include "scripting/dataenginescript.h"
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/service_p.h"
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2006-12-17 00:04:44 +01:00
|
|
|
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2008-02-25 12:24:37 +01:00
|
|
|
DataEngine::DataEngine(QObject* parent, KService::Ptr service)
|
2008-02-04 05:41:40 +01:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new DataEnginePrivate(this, service))
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
2008-04-24 12:07:34 +02:00
|
|
|
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(scheduleSourcesUpdated()));
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::DataEngine(QObject* parent, const QVariantList& args)
|
2007-05-19 10:38:46 +02:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new DataEnginePrivate(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString())))
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2008-04-24 12:07:34 +02:00
|
|
|
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(scheduleSourcesUpdated()));
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::~DataEngine()
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << objectName() << ": bye bye birdy! ";
|
2007-05-19 10:38:46 +02:00
|
|
|
delete d;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-13 06:56:49 +02:00
|
|
|
QStringList DataEngine::sources() const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->sources.keys();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
Service* DataEngine::serviceForSource(const QString &source)
|
|
|
|
{
|
2008-09-03 20:52:23 +02:00
|
|
|
return new NullService(source, this);
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
2007-09-12 19:53:54 +02:00
|
|
|
void DataEngine::connectSource(const QString& source, QObject* visualization,
|
2008-04-24 04:21:22 +02:00
|
|
|
uint pollingInterval, Plasma::IntervalAlignment intervalAlignment) const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "connectSource" << source;
|
2007-11-07 07:10:57 +01:00
|
|
|
bool newSource;
|
|
|
|
DataContainer* s = d->requestSource(source, &newSource);
|
2007-06-10 07:39:27 +02:00
|
|
|
|
2007-09-13 22:34:17 +02:00
|
|
|
if (s) {
|
2007-11-08 04:37:57 +01:00
|
|
|
// 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)
|
2008-04-24 04:21:22 +02:00
|
|
|
d->connectSource(s, visualization, pollingInterval, intervalAlignment, !newSource || pollingInterval > 0);
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << " ==> source connected";
|
2007-06-10 06:03:50 +02:00
|
|
|
}
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
void DataEngine::connectAllSources(QObject* visualization, uint pollingInterval,
|
2007-09-12 19:53:54 +02:00
|
|
|
Plasma::IntervalAlignment intervalAlignment) const
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2007-09-12 19:04:21 +02:00
|
|
|
foreach (DataContainer* s, d->sources) {
|
2008-04-24 04:21:22 +02:00
|
|
|
d->connectSource(s, visualization, pollingInterval, intervalAlignment);
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
2007-05-28 07:43:54 +02:00
|
|
|
}
|
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
void DataEngine::disconnectSource(const QString& source, QObject* visualization) const
|
|
|
|
{
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* s = d->source(source, false);
|
2007-06-10 07:39:27 +02:00
|
|
|
|
2007-09-13 22:34:17 +02:00
|
|
|
if (s) {
|
|
|
|
s->disconnectVisualization(visualization);
|
2007-06-10 07:39:27 +02:00
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-08-29 04:57:04 +02:00
|
|
|
DataContainer* DataEngine::containerForSource(const QString &source)
|
|
|
|
{
|
2007-11-15 07:32:54 +01:00
|
|
|
return d->source(source, false);
|
2007-08-29 04:57:04 +02:00
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
DataEngine::Data DataEngine::query(const QString& source) const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2008-09-03 20:52:23 +02:00
|
|
|
bool newSource;
|
|
|
|
DataContainer* s = d->requestSource(source, &newSource);
|
2007-08-11 10:19:20 +02:00
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
return DataEngine::Data();
|
2008-09-03 20:52:23 +02:00
|
|
|
} else if (!newSource && d->minPollingInterval >= 0 &&
|
|
|
|
s->timeSinceLastUpdate() >= uint(d->minPollingInterval)) {
|
|
|
|
if (const_cast<DataEngine*>(this)->updateSourceEvent(source)) {
|
|
|
|
d->queueUpdate();
|
|
|
|
}
|
2007-08-11 10:19:20 +02:00
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-09-02 00:23:03 +02:00
|
|
|
DataEngine::Data data = s->data();
|
|
|
|
s->checkUsage();
|
|
|
|
return data;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::init()
|
|
|
|
{
|
2008-02-04 05:41:40 +01:00
|
|
|
if (d->script) {
|
|
|
|
d->script->init();
|
|
|
|
} else {
|
2008-03-06 06:18:36 +01:00
|
|
|
// kDebug() << "called";
|
2008-02-04 05:41:40 +01:00
|
|
|
// default implementation does nothing. this is for engines that have to
|
|
|
|
// start things in motion external to themselves before they can work
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 09:32:46 +02:00
|
|
|
bool DataEngine::sourceRequestEvent(const QString &name)
|
2007-06-10 06:03:50 +02:00
|
|
|
{
|
2008-02-04 05:41:40 +01:00
|
|
|
if (d->script) {
|
2008-04-26 14:59:44 +02:00
|
|
|
return d->script->sourceRequestEvent(name);
|
2008-02-04 05:41:40 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-10 06:03:50 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 10:01:18 +02:00
|
|
|
bool DataEngine::updateSourceEvent(const QString& source)
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2008-02-04 05:41:40 +01:00
|
|
|
if (d->script) {
|
2008-04-26 14:59:44 +02:00
|
|
|
return d->script->updateSourceEvent(source);
|
2008-02-04 05:41:40 +01:00
|
|
|
} else {
|
|
|
|
//kDebug() << "updateSource source" << endl;
|
|
|
|
return false; //TODO: should this be true to trigger, even needless, updates on every tick?
|
|
|
|
}
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataEngine::setData(const QString& source, const QVariant& value)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
setData(source, source, value);
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataEngine::setData(const QString& source, const QString& key, const QVariant& value)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2008-03-23 18:03:49 +01:00
|
|
|
DataContainer* s = d->source(source, false);
|
|
|
|
bool isNew = !s;
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
s = d->source(source);
|
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
s->setData(key, value);
|
2008-03-23 18:03:49 +01:00
|
|
|
|
|
|
|
if (isNew) {
|
2008-04-24 09:15:26 +02:00
|
|
|
emit sourceAdded(source);
|
2008-03-23 18:03:49 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
d->queueUpdate();
|
2007-07-09 11:33:15 +02:00
|
|
|
}
|
|
|
|
|
2007-07-17 20:08:57 +02:00
|
|
|
void DataEngine::setData(const QString &source, const Data &data)
|
|
|
|
{
|
2008-03-23 18:03:49 +01:00
|
|
|
DataContainer *s = d->source(source, false);
|
|
|
|
bool isNew = !s;
|
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
s = d->source(source);
|
|
|
|
}
|
|
|
|
|
2007-07-17 20:08:57 +02:00
|
|
|
Data::const_iterator it = data.constBegin();
|
|
|
|
while (it != data.constEnd()) {
|
|
|
|
s->setData(it.key(), it.value());
|
|
|
|
++it;
|
|
|
|
}
|
2008-03-23 18:03:49 +01:00
|
|
|
|
|
|
|
if (isNew) {
|
2008-04-24 09:15:26 +02:00
|
|
|
emit sourceAdded(source);
|
2008-03-23 18:03:49 +01:00
|
|
|
}
|
|
|
|
|
2007-07-17 20:08:57 +02:00
|
|
|
d->queueUpdate();
|
|
|
|
}
|
|
|
|
|
2008-04-24 10:10:30 +02:00
|
|
|
void DataEngine::removeAllData(const QString& source)
|
2007-07-09 11:33:15 +02:00
|
|
|
{
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* s = d->source(source, false);
|
2007-07-09 11:33:15 +02:00
|
|
|
if (s) {
|
2008-04-24 13:56:32 +02:00
|
|
|
s->removeAllData();
|
2007-07-09 11:33:15 +02:00
|
|
|
d->queueUpdate();
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-17 19:38:54 +02:00
|
|
|
void DataEngine::removeData(const QString& source, const QString& key)
|
|
|
|
{
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* s = d->source(source, false);
|
2007-06-17 19:38:54 +02:00
|
|
|
if (s) {
|
|
|
|
s->setData(key, QVariant());
|
|
|
|
d->queueUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
void DataEngine::addSource(DataContainer* source)
|
2007-05-23 08:38:44 +02:00
|
|
|
{
|
2007-06-10 06:03:50 +02:00
|
|
|
SourceDict::const_iterator it = d->sources.find(source->objectName());
|
2007-05-23 08:38:44 +02:00
|
|
|
if (it != d->sources.constEnd()) {
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "source named \"" << source->objectName() << "\" already exists.";
|
2007-05-23 08:38:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-27 09:59:17 +02:00
|
|
|
d->sources.insert(source->objectName(), source);
|
2008-04-24 09:15:26 +02:00
|
|
|
emit sourceAdded(source->objectName());
|
2008-07-29 05:26:23 +02:00
|
|
|
d->queueUpdate();
|
2007-05-23 08:38:44 +02:00
|
|
|
}
|
|
|
|
|
2008-04-25 17:52:13 +02:00
|
|
|
void DataEngine::setMaxSourceCount(uint limit)
|
2008-04-24 22:16:21 +02:00
|
|
|
{
|
|
|
|
if (d->limit == limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->limit = limit;
|
|
|
|
|
|
|
|
if (d->limit > 0) {
|
|
|
|
d->trimQueue();
|
|
|
|
} else {
|
|
|
|
d->sourceQueue.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-25 17:52:13 +02:00
|
|
|
uint DataEngine::maxSourceCount() const
|
2008-04-24 22:16:21 +02:00
|
|
|
{
|
|
|
|
return d->limit;
|
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
void DataEngine::setMinimumPollingInterval(int minimumMs)
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2008-09-03 20:52:23 +02:00
|
|
|
if (minimumMs < 0) {
|
|
|
|
minimumMs = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
d->minPollingInterval = minimumMs;
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
int DataEngine::minimumPollingInterval() const
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2008-04-24 04:21:22 +02:00
|
|
|
return d->minPollingInterval;
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
void DataEngine::setPollingInterval(uint frequency)
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
|
|
|
killTimer(d->updateTimerId);
|
|
|
|
d->updateTimerId = 0;
|
|
|
|
|
|
|
|
if (frequency > 0) {
|
|
|
|
d->updateTimerId = startTimer(frequency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
NOTE: This is not implemented to prevent having to store the value internally.
|
|
|
|
When there is a good use case for needing access to this value, we can
|
|
|
|
add another member to the Private class and add this method.
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
void DataEngine::pollingInterval()
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2008-04-24 04:21:22 +02:00
|
|
|
return d->pollingInterval;
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2007-06-11 23:10:44 +02:00
|
|
|
void DataEngine::removeSource(const QString& source)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "removing source " << source;
|
2007-06-10 06:03:50 +02:00
|
|
|
SourceDict::iterator it = d->sources.find(source);
|
2007-05-19 10:38:46 +02:00
|
|
|
if (it != d->sources.end()) {
|
2008-03-23 18:03:49 +01:00
|
|
|
DataContainer *s = it.value();
|
|
|
|
|
|
|
|
// remove it from the limit queue if we're keeping one
|
|
|
|
if (d->limit > 0) {
|
|
|
|
QQueue<DataContainer*>::iterator it = d->sourceQueue.begin();
|
|
|
|
while (it != d->sourceQueue.end()) {
|
|
|
|
if (*it == s) {
|
|
|
|
d->sourceQueue.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->deleteLater();
|
2007-05-19 10:38:46 +02:00
|
|
|
d->sources.erase(it);
|
2008-03-23 18:03:49 +01:00
|
|
|
emit sourceRemoved(source);
|
2007-05-19 10:38:46 +02:00
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 08:38:14 +02:00
|
|
|
void DataEngine::removeAllSources()
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-07-23 02:22:16 +02:00
|
|
|
QMutableHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
2007-05-19 10:38:46 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2007-06-13 06:56:49 +02:00
|
|
|
emit sourceRemoved(it.key());
|
2007-05-19 10:38:46 +02:00
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
bool DataEngine::isValid() const
|
|
|
|
{
|
|
|
|
return d->valid;
|
|
|
|
}
|
|
|
|
|
2007-11-18 02:32:11 +01:00
|
|
|
bool DataEngine::isEmpty() const
|
|
|
|
{
|
2007-11-19 17:37:46 +01:00
|
|
|
return d->sources.isEmpty();
|
2007-11-18 02:32:11 +01:00
|
|
|
}
|
|
|
|
|
2007-06-04 18:11:55 +02:00
|
|
|
void DataEngine::setValid(bool valid)
|
2007-06-03 00:23:26 +02:00
|
|
|
{
|
|
|
|
d->valid = valid;
|
|
|
|
}
|
|
|
|
|
2008-04-23 21:57:20 +02:00
|
|
|
DataEngine::SourceDict DataEngine::containerDict() const
|
2007-06-10 06:03:50 +02:00
|
|
|
{
|
|
|
|
return d->sources;
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:49:51 +02:00
|
|
|
void DataEngine::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
|
|
|
if (event->timerId() != d->updateTimerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
|
|
|
|
// if the freq update is less than 0, don't bother
|
2008-04-24 04:21:22 +02:00
|
|
|
if (d->minPollingInterval < 0) {
|
2007-09-11 02:49:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
// minPollingInterval
|
|
|
|
if (d->updateTimestamp.elapsed() < d->minPollingInterval) {
|
2007-09-11 02:49:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->updateTimestamp.restart();
|
|
|
|
QHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2008-04-24 10:01:18 +02:00
|
|
|
updateSourceEvent(it.key());
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
2008-04-24 12:07:34 +02:00
|
|
|
scheduleSourcesUpdated();
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2007-05-23 09:13:00 +02:00
|
|
|
void DataEngine::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
QString DataEngine::icon() const
|
2007-05-23 09:13:00 +02:00
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
2008-04-13 17:01:46 +02:00
|
|
|
const Package *DataEngine::package() const
|
|
|
|
{
|
|
|
|
return d->package;
|
|
|
|
}
|
|
|
|
|
2008-04-24 12:07:34 +02:00
|
|
|
void DataEngine::scheduleSourcesUpdated()
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
2007-07-23 02:22:16 +02:00
|
|
|
QHashIterator<QString, Plasma::DataContainer*> it(d->sources);
|
2007-05-19 10:38:46 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.value()->checkForUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-24 00:40:03 +02:00
|
|
|
QString DataEngine::name() const
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
2008-02-25 12:24:37 +01:00
|
|
|
return d->engineName;
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 00:40:03 +02:00
|
|
|
void DataEngine::setName(const QString& name)
|
|
|
|
{
|
|
|
|
d->engineName = name;
|
|
|
|
setObjectName(name);
|
|
|
|
}
|
|
|
|
|
2008-04-29 03:17:12 +02:00
|
|
|
// Private class implementations
|
2008-07-01 20:56:43 +02:00
|
|
|
DataEnginePrivate::DataEnginePrivate(DataEngine* e, KService::Ptr service)
|
2008-04-29 03:17:12 +02:00
|
|
|
: q(e),
|
|
|
|
refCount(-1), // first ref
|
|
|
|
updateTimerId(0),
|
|
|
|
minPollingInterval(-1),
|
|
|
|
limit(0),
|
|
|
|
valid(true),
|
|
|
|
script(0),
|
|
|
|
package(0)
|
|
|
|
{
|
|
|
|
updateTimer = new QTimer(q);
|
|
|
|
updateTimer->setSingleShot(true);
|
|
|
|
updateTimestamp.start();
|
|
|
|
|
|
|
|
if (!service) {
|
2008-05-11 21:19:54 +02:00
|
|
|
engineName = i18n("Unnamed");
|
2008-04-29 03:17:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-08 18:55:05 +02:00
|
|
|
engineName = service->name();
|
2008-04-29 03:17:12 +02:00
|
|
|
if (engineName.isEmpty()) {
|
2008-05-11 21:19:54 +02:00
|
|
|
engineName = i18n("Unnamed");
|
2008-04-29 03:17:12 +02:00
|
|
|
}
|
|
|
|
e->setObjectName(engineName);
|
|
|
|
icon = service->icon();
|
|
|
|
|
|
|
|
KPluginInfo dataEngineDescription(service);
|
|
|
|
if (dataEngineDescription.isValid()) {
|
2008-04-29 18:24:14 +02:00
|
|
|
QString api = dataEngineDescription.property("X-Plasma-API").toString();
|
2008-04-29 03:17:12 +02:00
|
|
|
|
2008-04-29 18:24:14 +02:00
|
|
|
if (!api.isEmpty()) {
|
2008-04-29 03:17:12 +02:00
|
|
|
const QString path = KStandardDirs::locate("data",
|
2008-05-09 07:32:15 +02:00
|
|
|
"plasma/engines/" + dataEngineDescription.pluginName() + '/');
|
2008-04-29 18:24:14 +02:00
|
|
|
PackageStructure::Ptr structure = Plasma::packageStructure(api, Plasma::RunnerComponent);
|
2008-04-29 03:17:12 +02:00
|
|
|
structure->setPath(path);
|
|
|
|
package = new Package(path, structure);
|
|
|
|
|
2008-04-29 18:24:14 +02:00
|
|
|
script = Plasma::loadScriptEngine(api, q);
|
2008-04-29 03:17:12 +02:00
|
|
|
if (!script) {
|
2008-04-29 18:24:14 +02:00
|
|
|
kDebug() << "Could not create a" << api << "ScriptEngine for the"
|
2008-04-29 03:17:12 +02:00
|
|
|
<< dataEngineDescription.name() << "DataEngine.";
|
|
|
|
delete package;
|
|
|
|
package = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 19:42:36 +02:00
|
|
|
DataEnginePrivate::~DataEnginePrivate()
|
|
|
|
{
|
|
|
|
delete package;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void DataEnginePrivate::internalUpdateSource(DataContainer* source)
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
if (minPollingInterval > 0 &&
|
|
|
|
source->timeSinceLastUpdate() < (uint)minPollingInterval) {
|
|
|
|
// skip updating this source; it's been too soon
|
|
|
|
//kDebug() << "internal update source is delaying" << source->timeSinceLastUpdate() << d->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())) {
|
|
|
|
queueUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void DataEnginePrivate::ref()
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
--refCount;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void DataEnginePrivate::deref()
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
++refCount;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
bool DataEnginePrivate::isUsed() const
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
return refCount != 0;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
DataContainer* DataEnginePrivate::source(const QString& sourceName, bool createWhenMissing)
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
DataEngine::SourceDict::const_iterator it = sources.find(sourceName);
|
|
|
|
if (it != sources.constEnd()) {
|
|
|
|
DataContainer* s = it.value();
|
|
|
|
if (limit > 0) {
|
|
|
|
QQueue<DataContainer*>::iterator it = sourceQueue.begin();
|
|
|
|
while (it != sourceQueue.end()) {
|
|
|
|
if (*it == s) {
|
|
|
|
sourceQueue.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
|
|
|
return it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!createWhenMissing) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*kDebug() << "DataEngine " << q->objectName()
|
|
|
|
<< ": could not find DataContainer " << sourceName
|
|
|
|
<< ", creating" << endl;*/
|
|
|
|
DataContainer* s = new DataContainer(q);
|
|
|
|
s->setObjectName(sourceName);
|
|
|
|
sources.insert(sourceName, s);
|
2008-07-01 20:56:43 +02:00
|
|
|
QObject::connect(s, SIGNAL(updateRequested(DataContainer*)),
|
2008-08-26 22:37:53 +02:00
|
|
|
q, SLOT(internalUpdateSource(DataContainer*)));
|
2008-04-29 03:17:12 +02:00
|
|
|
|
|
|
|
if (limit > 0) {
|
|
|
|
trimQueue();
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-08-26 22:37:53 +02:00
|
|
|
void DataEnginePrivate::connectSource(DataContainer* s, QObject* visualization, uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment align, bool immediateCall)
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
//kDebug() << "connect source called with interval" << pollingInterval;
|
|
|
|
if (pollingInterval > 0) {
|
|
|
|
// never more frequently than allowed, never more than 20 times per second
|
2008-08-26 22:37:53 +02:00
|
|
|
uint min = qMax(50, minPollingInterval); // for qMax below
|
2008-04-29 03:17:12 +02:00
|
|
|
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);
|
|
|
|
immediateCall = !s->visualizationIsConnected(visualization);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->connectVisualization(visualization, pollingInterval, align);
|
|
|
|
|
|
|
|
if (immediateCall) {
|
|
|
|
QMetaObject::invokeMethod(visualization, "dataUpdated",
|
2008-08-26 22:37:53 +02:00
|
|
|
Q_ARG(QString, s->objectName()),
|
|
|
|
Q_ARG(Plasma::DataEngine::Data, s->data()));
|
2008-04-29 03:17:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
DataContainer* DataEnginePrivate::requestSource(const QString& sourceName, bool* newSource)
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
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;*/
|
|
|
|
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;
|
|
|
|
}
|
2008-07-01 20:56:43 +02:00
|
|
|
QObject::connect(s, SIGNAL(becameUnused(QString)), q, SLOT(removeSource(QString)));
|
2008-04-29 03:17:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void DataEnginePrivate::trimQueue()
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
uint queueCount = sourceQueue.count();
|
|
|
|
while (queueCount >= limit) {
|
|
|
|
DataContainer* punted = sourceQueue.dequeue();
|
|
|
|
q->removeSource(punted->objectName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void DataEnginePrivate::queueUpdate()
|
2008-04-29 03:17:12 +02:00
|
|
|
{
|
|
|
|
if (updateTimer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTimer->start(0);
|
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include "dataengine.moc"
|