2007-09-11 02:55:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* 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-09-11 02:55:46 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PLASMA_DATACONTAINER_P_H
|
|
|
|
#define PLASMA_DATACONTAINER_P_H
|
|
|
|
|
|
|
|
#include <QtCore/QTimerEvent>
|
2007-10-06 00:21:25 +02:00
|
|
|
#include <QtCore/QTime>
|
2007-09-11 02:55:46 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class SignalRelay;
|
|
|
|
|
|
|
|
class DataContainer::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
2007-09-13 22:34:17 +02:00
|
|
|
: updateTs(0),
|
|
|
|
dirty(false)
|
2007-09-11 02:55:46 +02:00
|
|
|
{}
|
|
|
|
|
2007-09-12 19:53:54 +02:00
|
|
|
QObject* signalRelay(const DataContainer* dc, QObject *visualization,
|
|
|
|
uint updateInterval, Plasma::IntervalAlignment align);
|
2007-09-11 02:55:46 +02:00
|
|
|
|
|
|
|
DataEngine::Data data;
|
|
|
|
QMap<QObject *, SignalRelay *> relayObjects;
|
|
|
|
QMap<uint, SignalRelay *> relays;
|
2007-09-13 22:34:17 +02:00
|
|
|
int updateTs;
|
2007-09-11 02:55:46 +02:00
|
|
|
bool dirty : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SignalRelay : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2007-09-12 19:53:54 +02:00
|
|
|
SignalRelay(DataContainer* parent, DataContainer::Private *data, uint ival, Plasma::IntervalAlignment align)
|
2007-09-11 02:55:46 +02:00
|
|
|
: QObject(parent),
|
|
|
|
dc(parent),
|
|
|
|
d(data),
|
2007-09-12 19:53:54 +02:00
|
|
|
m_interval(ival),
|
|
|
|
m_align(align),
|
2007-09-16 21:34:51 +02:00
|
|
|
m_resetTimer(true),
|
2007-09-13 22:34:17 +02:00
|
|
|
m_queued(false)
|
2007-09-11 02:55:46 +02:00
|
|
|
{
|
2007-09-13 22:34:17 +02:00
|
|
|
//kDebug() << "signal relay with time of" << m_timerId << "being set up";
|
2007-09-16 21:34:51 +02:00
|
|
|
m_timerId = startTimer(0);
|
2007-09-12 20:32:41 +02:00
|
|
|
if (m_align != Plasma::NoAlignment) {
|
|
|
|
checkAlignment();
|
|
|
|
}
|
2007-09-11 02:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isUnused()
|
|
|
|
{
|
2007-11-06 08:20:08 +01:00
|
|
|
return receivers(SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data))) < 1;
|
2007-09-11 02:55:46 +02:00
|
|
|
}
|
|
|
|
|
2007-09-12 19:53:54 +02:00
|
|
|
void checkAlignment()
|
|
|
|
{
|
|
|
|
int newTime = 0;
|
|
|
|
|
|
|
|
QTime t = QTime::currentTime();
|
|
|
|
if (m_align == Plasma::AlignToMinute) {
|
|
|
|
int seconds = t.second();
|
|
|
|
if (seconds > 2) {
|
|
|
|
newTime = ((60 - seconds) * 1000) + 500;
|
|
|
|
}
|
|
|
|
} else if (m_align == Plasma::AlignToHour) {
|
|
|
|
int minutes = t.minute();
|
|
|
|
int seconds = t.second();
|
|
|
|
if (minutes > 1 || seconds > 10) {
|
|
|
|
newTime = ((60 - minutes) * 1000 * 60) +
|
|
|
|
((60 - seconds) * 1000) + 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newTime) {
|
|
|
|
killTimer(m_timerId);
|
|
|
|
m_timerId = startTimer(newTime);
|
|
|
|
m_resetTimer = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-13 22:34:17 +02:00
|
|
|
void checkQueueing() {
|
|
|
|
if (m_queued) {
|
2007-11-06 08:20:08 +01:00
|
|
|
emit dataUpdated(dc->objectName(), d->data);
|
2007-09-13 22:34:17 +02:00
|
|
|
m_queued = false;
|
2007-09-13 22:41:01 +02:00
|
|
|
//TODO: should we re-align our timer at this point, to avoid
|
|
|
|
// constant queueing due to more-or-less constant time
|
|
|
|
// async update time? this might make sense for
|
|
|
|
// staggered accesses to the same source by multiple
|
|
|
|
// visualizations causing a minimumUpdateInterval violation.
|
|
|
|
// it may not make sense for purely async-and-takes-a-while
|
|
|
|
// type operations (e.g. network fetching).
|
|
|
|
// we need more real world data before making such a change
|
|
|
|
// change
|
|
|
|
//
|
|
|
|
// killTimer(m_timerId);
|
|
|
|
// m_timerId = startTime(m_interval);
|
2007-09-13 22:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:55:46 +02:00
|
|
|
DataContainer *dc;
|
|
|
|
DataContainer::Private *d;
|
2007-09-12 19:53:54 +02:00
|
|
|
uint m_interval;
|
|
|
|
Plasma::IntervalAlignment m_align;
|
|
|
|
int m_timerId;
|
|
|
|
bool m_resetTimer;
|
2007-09-13 22:34:17 +02:00
|
|
|
bool m_queued;
|
2007-09-11 02:55:46 +02:00
|
|
|
|
|
|
|
signals:
|
2007-11-06 08:20:08 +01:00
|
|
|
void dataUpdated(const QString&, const Plasma::DataEngine::Data&);
|
2007-09-11 02:55:46 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void timerEvent(QTimerEvent *event)
|
|
|
|
{
|
2007-09-12 19:53:54 +02:00
|
|
|
if (m_resetTimer) {
|
|
|
|
killTimer(m_timerId);
|
|
|
|
m_timerId = startTimer(m_interval);
|
|
|
|
m_resetTimer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_align != Plasma::NoAlignment) {
|
|
|
|
checkAlignment();
|
|
|
|
}
|
|
|
|
|
2007-09-13 22:34:17 +02:00
|
|
|
emit dc->requestUpdate(dc);
|
|
|
|
if (!dc->hasUpdates()) {
|
|
|
|
// the source wasn't actually updated; so let's put ourselves in the queue
|
2007-11-06 08:20:08 +01:00
|
|
|
// so we get an dataUpdated() when the data does arrive
|
2007-09-13 22:34:17 +02:00
|
|
|
m_queued = true;
|
2007-09-16 21:34:51 +02:00
|
|
|
} else {
|
2007-11-06 08:20:08 +01:00
|
|
|
emit dataUpdated(dc->objectName(), d->data);
|
2007-09-13 22:34:17 +02:00
|
|
|
}
|
2007-09-11 02:55:46 +02:00
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-09-12 19:53:54 +02:00
|
|
|
QObject* DataContainer::Private::signalRelay(const DataContainer* dc, QObject *visualization, uint updateInterval, Plasma::IntervalAlignment align)
|
2007-09-11 02:55:46 +02:00
|
|
|
{
|
|
|
|
QMap<uint, SignalRelay *>::const_iterator relayIt = relays.find(updateInterval);
|
|
|
|
SignalRelay *relay = 0;
|
|
|
|
|
|
|
|
if (relayIt == relays.end()) {
|
2007-09-12 19:53:54 +02:00
|
|
|
relay = new SignalRelay(const_cast<DataContainer*>(dc), this, updateInterval, align);
|
2007-09-11 02:55:46 +02:00
|
|
|
relays[updateInterval] = relay;
|
|
|
|
} else {
|
|
|
|
relay = relayIt.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
relayObjects[visualization] = relay;
|
|
|
|
return relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // multiple inclusion guard
|