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"
|
|
|
|
|
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-02-04 05:41:40 +01:00
|
|
|
#include "scripting/dataenginescript.h"
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2006-12-17 00:04:44 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
class DataEngine::Private
|
|
|
|
{
|
|
|
|
public:
|
2008-02-04 05:41:40 +01:00
|
|
|
Private(DataEngine* e, KService::Ptr service)
|
2007-05-28 07:43:54 +02:00
|
|
|
: engine(e),
|
2008-02-25 12:24:37 +01:00
|
|
|
ref(-1), // first ref
|
2007-09-11 02:49:51 +02:00
|
|
|
updateTimerId(0),
|
2008-04-24 04:21:22 +02:00
|
|
|
minPollingInterval(-1),
|
2007-06-03 00:23:26 +02:00
|
|
|
limit(0),
|
2008-02-04 05:41:40 +01:00
|
|
|
valid(true),
|
2008-04-13 17:01:46 +02:00
|
|
|
script(0),
|
|
|
|
package(0)
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
|
|
|
updateTimer = new QTimer(engine);
|
|
|
|
updateTimer->setSingleShot(true);
|
2007-09-11 02:49:51 +02:00
|
|
|
updateTimestamp.start();
|
2008-02-04 05:41:40 +01:00
|
|
|
|
2008-02-25 12:24:37 +01:00
|
|
|
if (!service) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
engineName = service->property("X-Plasma-EngineName").toString();
|
2008-04-24 00:40:03 +02:00
|
|
|
if (engineName.isEmpty()) {
|
|
|
|
engineName = i18n("Anonymous Engine");
|
|
|
|
}
|
2008-02-25 12:24:37 +01:00
|
|
|
e->setObjectName(engineName);
|
|
|
|
icon = service->icon();
|
|
|
|
|
|
|
|
KPluginInfo dataEngineDescription(service);
|
2008-02-04 05:41:40 +01:00
|
|
|
if (dataEngineDescription.isValid()) {
|
|
|
|
QString language = dataEngineDescription.property("X-Plasma-Language").toString();
|
|
|
|
|
|
|
|
if (!language.isEmpty()) {
|
2008-04-13 17:01:46 +02:00
|
|
|
const QString path = KStandardDirs::locate("data",
|
|
|
|
"plasma/engines/" + dataEngineDescription.pluginName() + "/");
|
|
|
|
PackageStructure::Ptr structure = Plasma::packageStructure(language, Plasma::RunnerComponent);
|
|
|
|
structure->setPath(path);
|
|
|
|
package = new Package(path, structure);
|
|
|
|
|
2008-02-04 05:41:40 +01:00
|
|
|
script = Plasma::loadScriptEngine(language, engine);
|
|
|
|
if (!script) {
|
|
|
|
kDebug() << "Could not create a" << language << "ScriptEngine for the"
|
|
|
|
<< dataEngineDescription.name() << "DataEngine.";
|
2008-04-13 17:01:46 +02:00
|
|
|
delete package;
|
|
|
|
package = 0;
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-19 10:38:46 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* source(const QString& sourceName, bool createWhenMissing = true)
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
2007-06-10 06:03:50 +02:00
|
|
|
DataEngine::SourceDict::const_iterator it = sources.find(sourceName);
|
2007-05-19 10:38:46 +02:00
|
|
|
if (it != sources.constEnd()) {
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* s = it.value();
|
2007-05-28 07:43:54 +02:00
|
|
|
if (limit > 0) {
|
2007-07-23 02:22:16 +02:00
|
|
|
QQueue<DataContainer*>::iterator it = sourceQueue.begin();
|
2007-05-28 07:43:54 +02:00
|
|
|
while (it != sourceQueue.end()) {
|
|
|
|
if (*it == s) {
|
|
|
|
sourceQueue.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
2007-05-19 10:38:46 +02:00
|
|
|
return it.value();
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
if (!createWhenMissing) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
/*kDebug() << "DataEngine " << engine->objectName()
|
2007-07-23 02:22:16 +02:00
|
|
|
<< ": could not find DataContainer " << sourceName
|
2007-06-10 07:39:27 +02:00
|
|
|
<< ", creating" << endl;*/
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* s = new DataContainer(engine);
|
2007-05-27 09:59:17 +02:00
|
|
|
s->setObjectName(sourceName);
|
2007-05-19 10:38:46 +02:00
|
|
|
sources.insert(sourceName, s);
|
2008-04-24 15:09:21 +02:00
|
|
|
connect(s, SIGNAL(updateRequested(DataContainer*)), engine, SLOT(internalUpdateSource(DataContainer*)));
|
2007-05-28 07:43:54 +02:00
|
|
|
|
|
|
|
if (limit > 0) {
|
|
|
|
trimQueue();
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
2007-05-19 10:38:46 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
void connectSource(DataContainer* s, QObject* visualization, uint pollingInterval,
|
2007-11-07 07:10:57 +01:00
|
|
|
Plasma::IntervalAlignment align, bool immediateCall = true)
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
2008-04-24 04:21:22 +02:00
|
|
|
//kDebug() << "connect source called with interval" << pollingInterval;
|
|
|
|
if (pollingInterval > 0) {
|
2007-09-12 19:04:21 +02:00
|
|
|
// never more frequently than allowed, never more than 20 times per second
|
2008-04-24 04:21:22 +02:00
|
|
|
uint min = qMax(50, minPollingInterval); // for qMin below
|
|
|
|
pollingInterval = qMax(min, pollingInterval);
|
2007-09-11 02:49:51 +02:00
|
|
|
|
2007-09-12 19:04:21 +02:00
|
|
|
// align on the 50ms
|
2008-04-24 04:21:22 +02:00
|
|
|
pollingInterval = pollingInterval - (pollingInterval % 50);
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 07:58:59 +02:00
|
|
|
if (immediateCall) {
|
|
|
|
// we don't want to do an immediate call if we are simply
|
|
|
|
// reconnecting
|
2008-04-24 22:09:57 +02:00
|
|
|
//kDebug() << "immediate call requested, we have:" << s->visualizationIsConnected(visualization);
|
2008-04-24 07:58:59 +02:00
|
|
|
immediateCall = !s->visualizationIsConnected(visualization);
|
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
s->connectVisualization(visualization, pollingInterval, align);
|
2007-09-12 19:04:21 +02:00
|
|
|
|
2007-11-07 07:10:57 +01:00
|
|
|
if (immediateCall) {
|
|
|
|
QMetaObject::invokeMethod(visualization, "dataUpdated",
|
|
|
|
Q_ARG(QString, s->objectName()),
|
|
|
|
Q_ARG(Plasma::DataEngine::Data, s->data()));
|
|
|
|
}
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
|
2007-11-07 07:10:57 +01:00
|
|
|
DataContainer* requestSource(const QString& sourceName, bool* newSource = 0)
|
2007-08-11 10:19:20 +02:00
|
|
|
{
|
2007-11-07 07:10:57 +01:00
|
|
|
if (newSource) {
|
|
|
|
*newSource = false;
|
|
|
|
}
|
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "requesting source " << sourceName;
|
2007-08-11 10:19:20 +02:00
|
|
|
DataContainer* s = source(sourceName, false);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
// we didn't find a data source, so give the engine an opportunity to make one
|
2008-01-08 02:25:09 +01:00
|
|
|
/*kDebug() << "DataEngine " << engine->objectName()
|
2007-09-13 22:34:17 +02:00
|
|
|
<< ": could not find DataContainer " << sourceName
|
|
|
|
<< " will create on request" << endl;*/
|
2008-04-24 09:32:46 +02:00
|
|
|
if (engine->sourceRequestEvent(sourceName)) {
|
2007-08-11 10:19:20 +02:00
|
|
|
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
|
2007-11-07 07:10:57 +01:00
|
|
|
if (newSource) {
|
|
|
|
*newSource = true;
|
|
|
|
}
|
2008-04-24 14:22:40 +02:00
|
|
|
connect(s, SIGNAL(becameUnused(QString)), engine, SLOT(removeSource(QString)));
|
2007-08-11 10:19:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-13 22:34:17 +02:00
|
|
|
|
2007-08-11 10:19:20 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
void trimQueue()
|
|
|
|
{
|
2007-09-13 22:34:17 +02:00
|
|
|
uint queueCount = sourceQueue.count();
|
|
|
|
while (queueCount >= limit) {
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer* punted = sourceQueue.dequeue();
|
2007-06-11 23:10:44 +02:00
|
|
|
engine->removeSource(punted->objectName());
|
2007-05-28 07:43:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void queueUpdate()
|
|
|
|
{
|
|
|
|
if (updateTimer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTimer->start(0);
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:49:51 +02:00
|
|
|
DataEngine* engine;
|
2007-08-27 19:27:27 +02:00
|
|
|
int ref;
|
2007-09-11 02:49:51 +02:00
|
|
|
int updateTimerId;
|
2008-04-24 04:21:22 +02:00
|
|
|
int minPollingInterval;
|
2007-09-11 02:49:51 +02:00
|
|
|
QTime updateTimestamp;
|
2007-06-10 06:03:50 +02:00
|
|
|
DataEngine::SourceDict sources;
|
2007-07-23 02:22:16 +02:00
|
|
|
QQueue<DataContainer*> sourceQueue;
|
2007-05-19 10:38:46 +02:00
|
|
|
QTimer* updateTimer;
|
2007-05-23 09:13:00 +02:00
|
|
|
QString icon;
|
2007-05-28 07:43:54 +02:00
|
|
|
uint limit;
|
2007-06-03 00:23:26 +02:00
|
|
|
bool valid;
|
2008-02-04 05:41:40 +01:00
|
|
|
DataEngineScript* script;
|
2008-02-25 12:24:37 +01:00
|
|
|
QString engineName;
|
2008-04-13 17:01:46 +02:00
|
|
|
Package *package;
|
2007-05-19 10:38:46 +02: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-02-25 12:24:37 +01:00
|
|
|
d(new Private(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-02-04 05:41:40 +01:00
|
|
|
d(new Private(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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-08-11 10:19:20 +02:00
|
|
|
DataContainer* s = d->requestSource(source);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
return DataEngine::Data();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2007-09-13 22:34:17 +02:00
|
|
|
void DataEngine::internalUpdateSource(DataContainer* source)
|
2007-09-12 19:04:21 +02:00
|
|
|
{
|
2008-04-24 04:21:22 +02:00
|
|
|
if (d->minPollingInterval > 0 &&
|
2008-04-25 22:33:46 +02:00
|
|
|
source->timeSinceLastUpdate() < (uint)d->minPollingInterval) {
|
2007-09-13 22:34:17 +02:00
|
|
|
// skip updating this source; it's been too soon
|
2008-04-24 04:21:22 +02:00
|
|
|
//kDebug() << "internal update source is delaying" << source->timeSinceLastUpdate() << d->minPollingInterval;
|
2008-01-22 18:03:34 +01:00
|
|
|
//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();
|
2007-09-13 22:34:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-24 10:01:18 +02:00
|
|
|
if (updateSourceEvent(source->objectName())) {
|
2007-09-12 19:04:21 +02:00
|
|
|
d->queueUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return d->script->sourceRequested(name);
|
|
|
|
} 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) {
|
|
|
|
return d->script->updateSource(source);
|
|
|
|
} 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());
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::ref()
|
|
|
|
{
|
2007-08-27 19:27:27 +02:00
|
|
|
--d->ref;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::deref()
|
|
|
|
{
|
2007-08-27 19:27:27 +02:00
|
|
|
++d->ref;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
bool DataEngine::isUsed() const
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->ref != 0;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include "dataengine.moc"
|