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"
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/datacontainer_p.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"
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
DataContainer::DataContainer(QObject *parent)
|
2007-05-20 22:36:59 +02:00
|
|
|
: QObject(parent),
|
2008-08-23 19:35:23 +02:00
|
|
|
d(new DataContainerPrivate)
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +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
|
|
|
}
|
|
|
|
|
2008-04-24 13:56:32 +02:00
|
|
|
void DataContainer::removeAllData()
|
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
|
|
|
}
|
|
|
|
|
2008-04-24 07:58:59 +02:00
|
|
|
bool DataContainer::visualizationIsConnected(QObject *visualization) const
|
|
|
|
{
|
2008-04-24 22:09:57 +02:00
|
|
|
return d->relayObjects.contains(visualization);
|
2008-04-24 07:58:59 +02:00
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
void DataContainer::connectVisualization(QObject *visualization, uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment alignment)
|
2007-09-12 19:03:06 +02:00
|
|
|
{
|
2008-04-29 02:15:26 +02:00
|
|
|
//kDebug() << "connecting visualization" << visualization << "at interval of"
|
|
|
|
// << pollingInterval << "to" << objectName();
|
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
|
2008-03-30 03:58:10 +02:00
|
|
|
//kDebug() << " already connected, but to a relay";
|
2008-04-29 02:26:33 +02:00
|
|
|
if (relay->m_interval == pollingInterval) {
|
|
|
|
//kDebug() << " already connected to a relay of the same interval of"
|
|
|
|
// << pollingInterval << ", nothing to do";
|
|
|
|
return;
|
|
|
|
}
|
2007-09-12 19:03:06 +02:00
|
|
|
|
2008-03-30 03:58:10 +02:00
|
|
|
if (relay->receiverCount() == 1) {
|
|
|
|
//kDebug() << " removing relay, as it is now unused";
|
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-03-30 03:58:10 +02:00
|
|
|
} else {
|
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
|
|
|
//relay->isUnused();
|
2007-09-12 19:03:06 +02:00
|
|
|
}
|
2008-04-24 04:21:22 +02:00
|
|
|
} else if (pollingInterval < 1) {
|
2007-09-12 19:03:06 +02:00
|
|
|
// 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
|
|
|
}
|
2008-03-30 03:58:10 +02:00
|
|
|
} else {
|
2007-09-12 19:03:06 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-24 04:21:22 +02:00
|
|
|
if (pollingInterval < 1) {
|
2008-04-29 02:15:26 +02:00
|
|
|
//kDebug() << " connecting directly";
|
2008-03-30 03:58:10 +02:00
|
|
|
d->relayObjects[visualization] = 0;
|
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-04-29 02:15:26 +02:00
|
|
|
//kDebug() << " connecting to a relay";
|
2008-08-26 22:37:53 +02:00
|
|
|
// 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;
|
2008-10-11 18:19:04 +02:00
|
|
|
SignalRelay *relay = d->signalRelay(this, visualization, pollingInterval,
|
|
|
|
alignment, immediateUpdate);
|
2008-03-30 03:58:10 +02:00
|
|
|
connect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
2007-11-06 08:20:08 +01:00
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-12 19:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
void DataContainer::disconnectVisualization(QObject *visualization)
|
2007-09-11 02:49:51 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2008-03-30 03:58:10 +02:00
|
|
|
if (relay->receiverCount() == 1) {
|
2007-09-16 18:25:59 +02:00
|
|
|
d->relays.remove(relay->m_interval);
|
2007-09-11 02:49:51 +02:00
|
|
|
delete relay;
|
2008-03-30 03:58:10 +02:00
|
|
|
} else {
|
|
|
|
disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
|
2007-09-11 02:49:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-25 21:45:09 +02:00
|
|
|
void DataContainer::checkForUpdate()
|
|
|
|
{
|
|
|
|
if (d->dirty) {
|
|
|
|
emit dataUpdated(objectName(), d->data);
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
foreach (SignalRelay *relay, d->relays) {
|
2008-04-25 21:45:09 +02:00
|
|
|
relay->checkQueueing();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint DataContainer::timeSinceLastUpdate() const
|
|
|
|
{
|
|
|
|
//FIXME: we still assume it's been <24h
|
|
|
|
//and ignore possible daylight savings changes
|
|
|
|
return d->updateTs.elapsed();
|
|
|
|
}
|
|
|
|
|
2008-04-25 22:33:46 +02:00
|
|
|
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!
|
|
|
|
emit becameUnused(objectName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
#include "datacontainer.moc"
|
2007-05-20 22:36:59 +02:00
|
|
|
|