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
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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-09-12 19:53:54 +02:00
|
|
|
#include "plasma.h"
|
2007-07-23 02:22:16 +02:00
|
|
|
#include "datacontainer.h"
|
2007-09-11 02:49:51 +02:00
|
|
|
#include "datacontainer_p.h"
|
2007-05-21 16:28:03 +02:00
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
if (!d->data.contains(key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->data.remove(key);
|
|
|
|
} else {
|
|
|
|
d->data[key] = value;
|
|
|
|
}
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
d->dirty = true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
void DataContainer::checkForUpdate()
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
|
|
|
if (d->dirty) {
|
2007-05-22 04:49:54 +02:00
|
|
|
emit updated(objectName(), d->data);
|
2007-05-20 22:36:59 +02:00
|
|
|
d->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 &&
|
|
|
|
receivers(SIGNAL(updated(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
|
|
|
{
|
|
|
|
// kDebug() << "connecting visualization" << (void*)visualization << "at interval of" << updateInterval;
|
|
|
|
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
|
|
|
|
disconnect(relay, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
|
|
|
|
if (relay->isUnused()) {
|
2007-09-12 19:53:54 +02:00
|
|
|
d->relays.erase(d->relays.find(relay->m_interval));
|
2007-09-12 19:03:06 +02:00
|
|
|
delete relay;
|
|
|
|
}
|
|
|
|
// kDebug() << " already connected, but to a relay";
|
|
|
|
} 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!
|
|
|
|
kDebug() << " already connected, nothing to do";
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
disconnect(this, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
connect(visualization, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(disconnectVisualization(QObject*)), Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->relayObjects[visualization] = 0;
|
|
|
|
|
|
|
|
if (updateInterval < 1) {
|
|
|
|
// kDebug() << " connecting directly";
|
|
|
|
connect(this, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
} else {
|
|
|
|
// kDebug() << " connecting to a relay";
|
2007-09-12 19:53:54 +02:00
|
|
|
connect(d->signalRelay(this, visualization, updateInterval, alignment),
|
2007-09-12 19:03:06 +02:00
|
|
|
SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-11 02:49:51 +02:00
|
|
|
disconnect(this, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
} else {
|
|
|
|
SignalRelay *relay = objIt.value();
|
|
|
|
disconnect(relay, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
|
|
|
|
if (relay->isUnused()) {
|
2007-09-12 19:53:54 +02:00
|
|
|
d->relays.erase(d->relays.find(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
|
|
|
|