2007-05-20 22:36:59 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2007-05-20 22:36:59 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 21:06:18 +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.
|
2007-05-20 22:36:59 +02: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-07-23 02:22:16 +02:00
|
|
|
#include "datacontainer.h"
|
2007-05-21 16:28:03 +02:00
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
|
2008-02-19 10:12:34 +01:00
|
|
|
#include "plasma.h"
|
|
|
|
#include "datacontainer_p.h"
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer::DataContainer(QObject* parent)
|
2007-05-20 22:36:59 +02:00
|
|
|
: QObject(parent),
|
|
|
|
d(new Private())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
DataContainer::~DataContainer()
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
const DataEngine::Data DataContainer::data() const
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
|
|
|
return d->data;
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
void DataContainer::setData(const QString& key, const QVariant& value)
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
2007-06-17 19:38:54 +02:00
|
|
|
if (value.isNull() || !value.isValid()) {
|
|
|
|
d->data.remove(key);
|
|
|
|
} else {
|
|
|
|
d->data[key] = value;
|
|
|
|
}
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
d->dirty = true;
|
2007-12-28 06:00:56 +01:00
|
|
|
d->updateTs.start();
|
2007-05-20 22:36:59 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
void DataContainer::clearData()
|
2007-07-09 11:33:15 +02:00
|
|
|
{
|
|
|
|
if (d->data.count() < 1) {
|
|
|
|
// avoid an update if we don't have any data anyways
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->data.clear();
|
|
|
|
d->dirty = true;
|
2008-01-01 13:18:26 +01:00
|
|
|
d->updateTs.start();
|
2007-07-09 11:33:15 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
void DataContainer::checkForUpdate()
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
|
|
|
if (d->dirty) {
|
2007-11-06 08:20:08 +01:00
|
|
|
emit dataUpdated(objectName(), d->data);
|
2007-09-13 22:34:17 +02:00
|
|
|
|
|
|
|
foreach (SignalRelay* relay, d->relays) {
|
|
|
|
relay->checkQueueing();
|
|
|
|
}
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
d->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:25:59 +02:00
|
|
|
int DataContainer::timeSinceLastUpdate() const
|
2007-09-13 22:34:17 +02:00
|
|
|
{
|
2007-12-28 06:00:56 +01:00
|
|
|
//FIXME: we still assume it's been <24h
|
|
|
|
//and ignore possible daylight savings changes
|
|
|
|
return d->updateTs.elapsed();
|
2007-09-13 22:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DataContainer::hasUpdates() const
|
|
|
|
{
|
2008-01-22 18:03:34 +01:00
|
|
|
if (d->cached) {
|
|
|
|
//some signalrelay needs us to pretend we did an update
|
|
|
|
d->cached = false;
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-13 22:34:17 +02:00
|
|
|
return d->dirty;
|
|
|
|
}
|
|
|
|
|
2008-01-22 18:03:34 +01:00
|
|
|
void DataContainer::setNeedsUpdate(bool update)
|
|
|
|
{
|
|
|
|
d->cached = update;
|
|
|
|
}
|
|
|
|
|
2007-09-02 00:23:03 +02:00
|
|
|
void DataContainer::checkUsage()
|
2007-06-10 07:39:27 +02:00
|
|
|
{
|
2007-09-11 02:49:51 +02:00
|
|
|
if (d->relays.count() < 1 &&
|
2007-11-06 08:20:08 +01:00
|
|
|
receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) {
|
2007-09-02 00:23:03 +02:00
|
|
|
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
|
|
|
|
emit unused(objectName());
|
2007-06-10 07:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-12 19:53:54 +02:00
|
|
|
void DataContainer::connectVisualization(QObject* visualization, uint updateInterval, Plasma::IntervalAlignment alignment)
|
2007-09-12 19:03:06 +02:00
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
// kDebug() << "connecting visualization" << (void*)visualization << "at interval of" << updateInterval;
|
2007-09-12 19:03:06 +02:00
|
|
|
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
|
2007-11-06 08:20:08 +01:00
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
2007-11-17 22:42:36 +01:00
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-12 19:03:06 +02:00
|
|
|
|
|
|
|
if (relay->isUnused()) {
|
2007-09-16 18:25:59 +02:00
|
|
|
d->relays.remove(relay->m_interval);
|
2007-09-12 19:03:06 +02:00
|
|
|
delete relay;
|
|
|
|
}
|
2008-01-08 02:25:09 +01:00
|
|
|
// kDebug() << " already connected, but to a relay";
|
2007-09-12 19:03:06 +02:00
|
|
|
} else if (updateInterval < 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!
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << " already connected, nothing to do";
|
2007-09-12 19:03:06 +02:00
|
|
|
return;
|
|
|
|
} else {
|
2007-11-06 08:20:08 +01:00
|
|
|
disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-12 19:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
connect(visualization, SIGNAL(destroyed(QObject*)),
|
2007-09-13 22:34:17 +02:00
|
|
|
this, SLOT(disconnectVisualization(QObject*)));//, Qt::QueuedConnection);
|
2007-09-12 19:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d->relayObjects[visualization] = 0;
|
|
|
|
|
|
|
|
if (updateInterval < 1) {
|
2008-01-08 02:25:09 +01:00
|
|
|
// kDebug() << " connecting directly";
|
2007-11-06 08:20:08 +01:00
|
|
|
connect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-12 19:03:06 +02:00
|
|
|
} else {
|
2008-01-08 02:25:09 +01:00
|
|
|
// kDebug() << " connecting to a relay";
|
2007-09-12 19:53:54 +02:00
|
|
|
connect(d->signalRelay(this, visualization, updateInterval, alignment),
|
2007-11-06 08:20:08 +01:00
|
|
|
SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-12 19:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:49:51 +02:00
|
|
|
void DataContainer::disconnectVisualization(QObject* visualization)
|
|
|
|
{
|
|
|
|
QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
|
|
|
|
|
2007-09-12 19:03:06 +02:00
|
|
|
if (objIt == d->relayObjects.end() || !objIt.value()) {
|
|
|
|
// it is connected directly to the DataContainer itself
|
2007-11-06 08:20:08 +01:00
|
|
|
disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-11 02:49:51 +02:00
|
|
|
} else {
|
|
|
|
SignalRelay *relay = objIt.value();
|
2007-11-06 08:20:08 +01:00
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-11 02:49:51 +02:00
|
|
|
|
|
|
|
if (relay->isUnused()) {
|
2007-09-16 18:25:59 +02:00
|
|
|
d->relays.remove(relay->m_interval);
|
2007-09-11 02:49:51 +02:00
|
|
|
delete relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-09-12 19:03:06 +02:00
|
|
|
d->relayObjects.erase(objIt);
|
2007-09-02 00:23:03 +02:00
|
|
|
checkUsage();
|
2007-06-10 07:39:27 +02:00
|
|
|
}
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
#include "datacontainer.moc"
|
2007-09-11 02:49:51 +02:00
|
|
|
#include "datacontainer_p.moc"
|
2007-05-20 22:36:59 +02:00
|
|
|
|