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 "datacontainer.h"
|
|
|
|
#include "private/datacontainer_p.h"
|
2010-07-12 22:51:12 +02:00
|
|
|
#include "private/storage_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "plasma.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
DataContainer::DataContainer(QObject *parent)
|
|
|
|
: QObject(parent),
|
2010-08-06 04:28:50 +02:00
|
|
|
d(new DataContainerPrivate(this))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataContainer::~DataContainer()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataEngine::Data DataContainer::data() const
|
|
|
|
{
|
|
|
|
return d->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::setData(const QString &key, const QVariant &value)
|
|
|
|
{
|
2010-05-18 20:22:13 +02:00
|
|
|
if (!value.isValid()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->data.remove(key);
|
|
|
|
} else {
|
2010-06-09 03:17:16 +02:00
|
|
|
d->data.insert(key, value);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->dirty = true;
|
|
|
|
d->updateTs.start();
|
2010-07-12 22:51:12 +02:00
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
//check if storage is enabled and if storage is needed.
|
|
|
|
//If it is not set to be stored,then this is the first
|
|
|
|
//setData() since the last time it was stored. This
|
|
|
|
//gives us only one singleShot timer.
|
2010-08-25 23:21:04 +02:00
|
|
|
if (isStorageEnabled() || !needsToBeStored()) {
|
|
|
|
QTimer::singleShot(180000, this, SLOT(store()));
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
2010-08-25 23:21:04 +02:00
|
|
|
|
|
|
|
setNeedsToBeStored(true);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::removeAllData()
|
|
|
|
{
|
2008-12-09 21:37:41 +01:00
|
|
|
if (d->data.isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
// avoid an update if we don't have any data anyways
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->data.clear();
|
|
|
|
d->dirty = true;
|
|
|
|
d->updateTs.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataContainer::visualizationIsConnected(QObject *visualization) const
|
|
|
|
{
|
|
|
|
return d->relayObjects.contains(visualization);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::connectVisualization(QObject *visualization, uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment alignment)
|
|
|
|
{
|
|
|
|
//kDebug() << "connecting visualization" << visualization << "at interval of"
|
|
|
|
// << pollingInterval << "to" << objectName();
|
|
|
|
QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
|
|
|
|
bool connected = objIt != d->relayObjects.end();
|
|
|
|
if (connected) {
|
|
|
|
// this visualization is already connected. just adjust the update
|
|
|
|
// frequency if necessary
|
|
|
|
SignalRelay *relay = objIt.value();
|
|
|
|
if (relay) {
|
|
|
|
// connected to a relay
|
|
|
|
//kDebug() << " already connected, but to a relay";
|
|
|
|
if (relay->m_interval == pollingInterval) {
|
|
|
|
//kDebug() << " already connected to a relay of the same interval of"
|
|
|
|
// << pollingInterval << ", nothing to do";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (relay->receiverCount() == 1) {
|
|
|
|
//kDebug() << " removing relay, as it is now unused";
|
|
|
|
d->relays.remove(relay->m_interval);
|
|
|
|
delete relay;
|
|
|
|
} else {
|
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
//relay->isUnused();
|
|
|
|
}
|
|
|
|
} else if (pollingInterval < 1) {
|
|
|
|
// the visualization was connected already, but not to a relay
|
|
|
|
// and it still doesn't want to connect to a relay, so we have
|
|
|
|
// nothing to do!
|
|
|
|
//kDebug() << " already connected, nothing to do";
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connect(visualization, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(disconnectVisualization(QObject*)));//, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pollingInterval < 1) {
|
|
|
|
//kDebug() << " connecting directly";
|
|
|
|
d->relayObjects[visualization] = 0;
|
|
|
|
connect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
} else {
|
|
|
|
//kDebug() << " connecting to a relay";
|
|
|
|
// we only want to do an imediate update if this is not the first object to connect to us
|
|
|
|
// if it is the first visualization, then the source will already have been populated
|
|
|
|
// engine's sourceRequested method
|
|
|
|
bool immediateUpdate = connected || d->relayObjects.count() > 1;
|
|
|
|
SignalRelay *relay = d->signalRelay(this, visualization, pollingInterval,
|
|
|
|
alignment, immediateUpdate);
|
|
|
|
connect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 23:35:40 +02:00
|
|
|
void DataContainer::setStorageEnabled(bool store)
|
2010-07-12 22:51:12 +02:00
|
|
|
{
|
2010-07-29 01:28:31 +02:00
|
|
|
QTime time = QTime::currentTime();
|
|
|
|
qsrand((uint)time.msec());
|
2010-07-12 22:51:12 +02:00
|
|
|
d->enableStorage = store;
|
2010-07-29 01:28:31 +02:00
|
|
|
if (store) {
|
|
|
|
QTimer::singleShot(qrand() % (2000 + 1) , this, SLOT(retrieve()));
|
|
|
|
}
|
2010-07-12 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DataContainer::isStorageEnabled() const
|
|
|
|
{
|
|
|
|
return d->enableStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataContainer::needsToBeStored() const
|
|
|
|
{
|
2010-07-29 01:28:31 +02:00
|
|
|
return !d->isStored;
|
2010-07-12 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::setNeedsToBeStored(bool store)
|
|
|
|
{
|
|
|
|
d->isStored = !store;
|
|
|
|
}
|
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
DataEngine* DataContainer::getDataEngine()
|
|
|
|
{
|
|
|
|
QObject *o = NULL;
|
|
|
|
DataEngine *de = NULL;
|
|
|
|
o = this;
|
|
|
|
while (de == NULL)
|
|
|
|
{
|
|
|
|
o = dynamic_cast<QObject *> (o->parent());
|
|
|
|
if (o == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
de = dynamic_cast<DataEngine *> (o);
|
|
|
|
}
|
|
|
|
return de;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::store()
|
|
|
|
{
|
2010-08-25 23:17:54 +02:00
|
|
|
if (!needsToBeStored() || !isStorageEnabled()){
|
2010-07-29 01:28:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-08-02 18:00:47 +02:00
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
DataEngine* de = getDataEngine();
|
|
|
|
if (de == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2010-08-02 18:00:47 +02:00
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
setNeedsToBeStored(false);
|
2010-08-02 18:00:47 +02:00
|
|
|
|
2010-08-17 11:28:46 +02:00
|
|
|
if (d->store == NULL) {
|
|
|
|
d->store = new Storage(de->name(), 0);
|
2010-08-02 18:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KConfigGroup op = d->store->operationDescription("save");
|
2010-07-29 01:28:31 +02:00
|
|
|
op.writeEntry("source", objectName());
|
|
|
|
DataEngine::Data dataToStore = data();
|
|
|
|
DataEngine::Data::const_iterator it = dataToStore.constBegin();
|
|
|
|
while (it != dataToStore.constEnd() && dataToStore.constEnd() == data().constEnd()) {
|
|
|
|
QVariant v = it.value();
|
2010-08-17 11:28:46 +02:00
|
|
|
if ((it.value().type() == QVariant::String) || (it.value().type() == QVariant::Int)) {
|
|
|
|
op.writeEntry("key", it.key());
|
|
|
|
op.writeEntry("data", it.value());
|
|
|
|
} else {
|
|
|
|
QByteArray b;
|
|
|
|
QDataStream ds(&b, QIODevice::WriteOnly);
|
|
|
|
ds << it.value();
|
|
|
|
op.writeEntry("key", "base64-" + it.key());
|
|
|
|
op.writeEntry("data", b.toBase64());
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
if (d->store == NULL) {
|
2010-08-02 18:00:47 +02:00
|
|
|
d->store = new Storage(de->name(), 0);
|
|
|
|
}
|
2010-08-17 11:28:46 +02:00
|
|
|
ServiceJob* job = d->store->startOperationCall(op);
|
|
|
|
d->storeCount++;
|
|
|
|
connect(job, SIGNAL(finished(KJob*)), this, SLOT(storeJobFinished(KJob*)));
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
2010-08-02 18:00:47 +02:00
|
|
|
}
|
2010-07-29 01:28:31 +02:00
|
|
|
|
2010-08-06 04:22:44 +02:00
|
|
|
void DataContainerPrivate::storeJobFinished(KJob* )
|
2010-08-02 18:00:47 +02:00
|
|
|
{
|
2010-08-06 04:22:44 +02:00
|
|
|
--storeCount;
|
|
|
|
if (storeCount < 1) {
|
|
|
|
store->deleteLater();
|
|
|
|
store = 0;
|
2010-08-02 18:00:47 +02:00
|
|
|
}
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::retrieve()
|
|
|
|
{
|
|
|
|
DataEngine* de = getDataEngine();
|
|
|
|
if (de == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2010-08-17 11:28:46 +02:00
|
|
|
Storage* store = new Storage(de->name(), 0);
|
|
|
|
KConfigGroup retrieveGroup = store->operationDescription("retrieve");
|
2010-07-29 01:28:31 +02:00
|
|
|
retrieveGroup.writeEntry("source", objectName());
|
2010-08-17 11:28:46 +02:00
|
|
|
ServiceJob* retrieveJob = store->startOperationCall(retrieveGroup);
|
2010-07-29 01:28:31 +02:00
|
|
|
connect(retrieveJob, SIGNAL(result(KJob*)), this,
|
|
|
|
SLOT(populateFromStoredData(KJob*)));
|
|
|
|
}
|
|
|
|
|
2010-08-06 04:28:50 +02:00
|
|
|
void DataContainerPrivate::populateFromStoredData(KJob *job)
|
2010-07-29 01:28:31 +02:00
|
|
|
{
|
|
|
|
if (job->error()) {
|
|
|
|
return;
|
|
|
|
}
|
2010-08-06 04:28:50 +02:00
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
DataEngine::Data dataToInsert;
|
|
|
|
ServiceJob* ret = dynamic_cast<ServiceJob*>(job);
|
|
|
|
QHash<QString, QVariant> h = ret->result().toHash();
|
|
|
|
foreach (QString key, h.keys()) {
|
2010-08-17 11:28:46 +02:00
|
|
|
if (key.startsWith("base64-")) {
|
|
|
|
QByteArray b = QByteArray::fromBase64(h[key].toString().toAscii());
|
|
|
|
QDataStream ds(&b, QIODevice::ReadOnly);
|
|
|
|
QVariant v(ds);
|
|
|
|
key.remove(0, 7);
|
|
|
|
dataToInsert.insert(key, v);
|
|
|
|
} else {
|
|
|
|
dataToInsert.insert(key, h[key]);
|
|
|
|
}
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
2010-08-06 04:28:50 +02:00
|
|
|
|
2010-08-17 11:28:46 +02:00
|
|
|
if (!(data.isEmpty()))
|
2010-07-29 01:28:31 +02:00
|
|
|
{
|
2010-08-17 11:28:46 +02:00
|
|
|
//Do not fill the source with old stored
|
|
|
|
//data if it is already populated with new data.
|
|
|
|
return;
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
2010-08-06 04:28:50 +02:00
|
|
|
|
2010-08-17 11:28:46 +02:00
|
|
|
data = dataToInsert;
|
|
|
|
dirty = true;
|
|
|
|
q->checkForUpdate();
|
2010-07-29 01:28:31 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void DataContainer::disconnectVisualization(QObject *visualization)
|
|
|
|
{
|
|
|
|
QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
|
2010-03-11 00:05:39 +01:00
|
|
|
disconnect(visualization, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(disconnectVisualization(QObject*)));//, Qt::QueuedConnection);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (objIt == d->relayObjects.end() || !objIt.value()) {
|
|
|
|
// it is connected directly to the DataContainer itself
|
|
|
|
disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
} else {
|
|
|
|
SignalRelay *relay = objIt.value();
|
|
|
|
|
|
|
|
if (relay->receiverCount() == 1) {
|
|
|
|
d->relays.remove(relay->m_interval);
|
|
|
|
delete relay;
|
|
|
|
} else {
|
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d->relayObjects.erase(objIt);
|
|
|
|
checkUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::checkForUpdate()
|
|
|
|
{
|
2008-12-09 21:37:41 +01:00
|
|
|
//kDebug() << objectName() << d->dirty;
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->dirty) {
|
|
|
|
emit dataUpdated(objectName(), d->data);
|
|
|
|
|
|
|
|
foreach (SignalRelay *relay, d->relays) {
|
|
|
|
relay->checkQueueing();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-27 03:26:33 +02:00
|
|
|
void DataContainer::forceImmediateUpdate()
|
|
|
|
{
|
|
|
|
if (d->dirty) {
|
|
|
|
d->dirty = false;
|
|
|
|
emit dataUpdated(objectName(), d->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (SignalRelay *relay, d->relays) {
|
|
|
|
relay->forceImmediateUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
uint DataContainer::timeSinceLastUpdate() const
|
|
|
|
{
|
|
|
|
//FIXME: we still assume it's been <24h
|
|
|
|
//and ignore possible daylight savings changes
|
|
|
|
return d->updateTs.elapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::setNeedsUpdate(bool update)
|
|
|
|
{
|
|
|
|
d->cached = update;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataContainer::checkUsage()
|
|
|
|
{
|
|
|
|
if (d->relays.count() < 1 &&
|
|
|
|
receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) {
|
|
|
|
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
|
2010-08-25 23:17:54 +02:00
|
|
|
kDebug() << objectName() << "is unused";
|
2008-11-04 00:08:39 +01:00
|
|
|
emit becameUnused(objectName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "datacontainer.moc"
|
|
|
|
|