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
|
|
|
#ifndef PLASMA_DATACONTAINER_H
|
|
|
|
#define PLASMA_DATACONTAINER_H
|
2007-05-20 22:36:59 +02:00
|
|
|
|
|
|
|
#include <QtCore/QHash>
|
|
|
|
#include <QtCore/QObject>
|
|
|
|
|
2007-06-02 19:29:39 +02:00
|
|
|
#include <plasma/plasma_export.h>
|
|
|
|
#include <plasma/dataengine.h>
|
2007-05-20 22:36:59 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
|
|
|
|
class DataContainerPrivate;
|
|
|
|
|
2007-05-27 10:00:46 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* @class DataContainer plasma/datacontainer.h <Plasma/DataContainer>
|
|
|
|
*
|
2007-06-01 01:40:39 +02:00
|
|
|
* @brief A set of data exported via a DataEngine
|
2007-05-27 10:00:46 +02:00
|
|
|
*
|
2007-07-23 02:22:16 +02:00
|
|
|
* Plasma::DataContainer wraps the data exported by a DataEngine
|
2007-05-27 10:00:46 +02:00
|
|
|
* implementation, providing a generic wrapper for the data.
|
|
|
|
*
|
2007-07-23 02:22:16 +02:00
|
|
|
* A DataContainer may have zero or more associated pieces of data which
|
2007-05-27 10:00:46 +02:00
|
|
|
* are keyed by strings. The data itself is stored as QVariants. This allows
|
|
|
|
* easy and flexible retrieval of the information associated with this object
|
2007-07-23 02:22:16 +02:00
|
|
|
* without writing DataContainer or DataEngine specific code in visualizations.
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
|
|
|
* If you are creating your own DataContainer objects (and are passing them to
|
|
|
|
* DataEngine::addSource()), you normally just need to listen to the
|
|
|
|
* updateRequested() signal (as well as any other methods you might have of
|
|
|
|
* being notified of new data) and call setData() to actually update the data.
|
|
|
|
* Then you need to either trigger the scheduleSourcesUpdated signal of the
|
|
|
|
* parent DataEngine or call checkForUpdate() on the DataContainer.
|
|
|
|
*
|
|
|
|
* You also need to set a suitable name for the source with setObjectName().
|
|
|
|
* See DataEngine::addSource() for more information.
|
|
|
|
*
|
|
|
|
* Note that there is normally no need to subclass DataContainer, except as
|
|
|
|
* a way of encapsulating the data retreival for a source, since all notifications
|
|
|
|
* are done via signals rather than virtual methods.
|
2007-05-27 10:00:46 +02:00
|
|
|
**/
|
2007-07-23 02:22:16 +02:00
|
|
|
class PLASMA_EXPORT DataContainer : public QObject
|
2007-05-20 22:36:59 +02:00
|
|
|
{
|
2008-04-25 21:45:09 +02:00
|
|
|
friend class DataEngine;
|
2008-07-01 20:56:43 +02:00
|
|
|
friend class DataEnginePrivate;
|
2007-05-20 22:36:59 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2007-05-27 10:00:46 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Constructs a default DataContainer that has no name or data
|
2007-05-27 10:00:46 +02:00
|
|
|
* associated with it
|
|
|
|
**/
|
2008-08-25 18:03:56 +02:00
|
|
|
explicit DataContainer(QObject *parent = 0);
|
2007-07-23 02:22:16 +02:00
|
|
|
virtual ~DataContainer();
|
2007-05-20 22:36:59 +02:00
|
|
|
|
2007-05-27 10:00:46 +02:00
|
|
|
/**
|
2007-07-23 02:22:16 +02:00
|
|
|
* Returns the data for this DataContainer
|
2007-05-27 10:00:46 +02:00
|
|
|
**/
|
2007-05-20 22:36:59 +02:00
|
|
|
const DataEngine::Data data() const;
|
2007-05-27 10:00:46 +02:00
|
|
|
|
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Set a value for a key.
|
|
|
|
*
|
|
|
|
* This also marks this source as needing to signal an update.
|
|
|
|
*
|
|
|
|
* If you call setData() directly on a DataContainer, you need to
|
|
|
|
* either trigger the scheduleSourcesUpdated() slot for the
|
|
|
|
* data engine it belongs to or call checkForUpdate() on the
|
|
|
|
* DataContainer.
|
2007-05-27 10:00:46 +02:00
|
|
|
*
|
|
|
|
* @param key a string used as the key for the data
|
2007-06-17 19:38:54 +02:00
|
|
|
* @param value a QVariant holding the actual data. If a null or invalid
|
|
|
|
* QVariant is passed in and the key currently exists in the
|
|
|
|
* data, then the data entry is removed
|
2007-05-27 10:00:46 +02:00
|
|
|
**/
|
2008-08-25 18:03:56 +02:00
|
|
|
void setData(const QString &key, const QVariant &value);
|
2007-05-20 22:36:59 +02:00
|
|
|
|
2007-07-09 11:33:15 +02:00
|
|
|
/**
|
2008-04-24 13:56:32 +02:00
|
|
|
* Removes all data currently associated with this source
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
|
|
|
* If you call removeAllData() on a DataContainer, you need to
|
|
|
|
* either trigger the scheduleSourcesUpdated() slot for the
|
|
|
|
* data engine it belongs to or call checkForUpdate() on the
|
|
|
|
* DataContainer.
|
2007-07-09 11:33:15 +02:00
|
|
|
**/
|
2008-04-24 13:56:32 +02:00
|
|
|
void removeAllData();
|
2007-07-09 11:33:15 +02:00
|
|
|
|
2008-04-24 07:58:59 +02:00
|
|
|
/**
|
|
|
|
* @return true if the visualization is currently connected
|
|
|
|
*/
|
|
|
|
bool visualizationIsConnected(QObject *visualization) const;
|
|
|
|
|
2007-09-12 19:03:06 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Connects an object to this DataContainer.
|
|
|
|
*
|
|
|
|
* May be called repeatedly for the same visualization without
|
|
|
|
* side effects
|
2007-09-12 19:03:06 +02:00
|
|
|
*
|
|
|
|
* @param visualization the object to connect to this DataContainer
|
2008-04-24 04:21:22 +02:00
|
|
|
* @param pollingInterval the time in milliseconds between updates
|
2007-09-12 19:03:06 +02:00
|
|
|
**/
|
2008-10-11 18:19:04 +02:00
|
|
|
void connectVisualization(QObject *visualization, uint pollingInterval,
|
|
|
|
Plasma::IntervalAlignment alignment);
|
2007-09-12 19:03:06 +02:00
|
|
|
|
2008-04-24 15:28:23 +02:00
|
|
|
public Q_SLOTS:
|
2007-09-11 02:49:51 +02:00
|
|
|
/**
|
|
|
|
* Disconnects an object from this DataContainer.
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
|
|
|
* Note that if this source was created by DataEngine::sourceRequestEvent(),
|
|
|
|
* it will be deleted by DataEngine once control returns to the event loop.
|
2007-09-11 02:49:51 +02:00
|
|
|
**/
|
2008-08-25 18:03:56 +02:00
|
|
|
void disconnectVisualization(QObject *visualization);
|
2007-09-11 02:49:51 +02:00
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
Q_SIGNALS:
|
2007-05-27 10:00:46 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Emitted when the data has been updated, allowing visualizations to
|
2007-05-27 10:00:46 +02:00
|
|
|
* reflect the new data.
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
|
|
|
* Note that you should not normally emit this directly. Instead, use
|
|
|
|
* checkForUpdates() or the DataEngine::scheduleSourcesUpdated() slot.
|
|
|
|
*
|
|
|
|
* @param source the objectName() of the DataContainer (and hence the name
|
|
|
|
* of the source) that updated its data
|
|
|
|
* @param data the updated data
|
2007-05-27 10:00:46 +02:00
|
|
|
**/
|
2008-08-25 18:03:56 +02:00
|
|
|
void dataUpdated(const QString &source, const Plasma::DataEngine::Data &data);
|
2007-05-20 22:36:59 +02:00
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Emitted when the last visualization is disconnected.
|
|
|
|
*
|
|
|
|
* Note that if this source was created by DataEngine::sourceRequestEvent(),
|
|
|
|
* it will be deleted by DataEngine once control returns to the event loop
|
|
|
|
* after this signal is emitted.
|
|
|
|
*
|
|
|
|
* @param source the name of the source that became unused
|
2007-06-10 07:39:27 +02:00
|
|
|
**/
|
2008-08-25 18:03:56 +02:00
|
|
|
void becameUnused(const QString &source);
|
2007-06-10 07:39:27 +02:00
|
|
|
|
2007-09-11 02:49:51 +02:00
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Emitted when an update is requested.
|
|
|
|
*
|
|
|
|
* If a polling interval was passed connectVisualization(), this signal
|
|
|
|
* will be emitted every time the interval expires.
|
|
|
|
*
|
|
|
|
* Note that if you create your own DataContainer (and pass it to
|
|
|
|
* DataEngine::addSource()), you will need to listen to this signal
|
|
|
|
* and refresh the data when it is triggered.
|
|
|
|
*
|
|
|
|
* @param source the datacontainer the update was requested for. Useful
|
|
|
|
* for classes that update the data for several containers.
|
2007-09-11 02:49:51 +02:00
|
|
|
**/
|
2008-04-24 15:09:21 +02:00
|
|
|
void updateRequested(DataContainer *source);
|
2008-08-02 19:46:32 +02:00
|
|
|
|
2008-04-25 21:45:09 +02:00
|
|
|
protected:
|
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Checks whether any data has changed and, if so, emits dataUpdated().
|
2008-04-25 21:45:09 +02:00
|
|
|
**/
|
|
|
|
void checkForUpdate();
|
|
|
|
|
|
|
|
/**
|
2008-08-25 18:03:56 +02:00
|
|
|
* Returns how long ago, in msecs, that the data in this container was last updated.
|
|
|
|
*
|
|
|
|
* This is used by DataEngine to compress updates that happen more quickly than the
|
|
|
|
* minimum polling interval by calling setNeedsUpdate() instead of calling
|
|
|
|
* updateSourceEvent() immediately.
|
2008-04-25 21:45:09 +02:00
|
|
|
**/
|
|
|
|
uint timeSinceLastUpdate() const;
|
|
|
|
|
2008-04-25 22:33:46 +02:00
|
|
|
/**
|
|
|
|
* Indicates that the data should be treated as dirty the next time hasUpdates() is called.
|
|
|
|
*
|
2008-08-25 18:03:56 +02:00
|
|
|
* This is needed for the case where updateRequested() is triggered but we don't want to
|
|
|
|
* update the data immediately because it has just been updated. The second request won't
|
|
|
|
* be fulfilled in this case, because we never updated the data and so never called
|
|
|
|
* checkForUpdate(). So we claim it needs an update anyway.
|
2008-04-25 22:33:46 +02:00
|
|
|
**/
|
|
|
|
void setNeedsUpdate(bool update = true);
|
|
|
|
|
2008-08-25 18:03:56 +02:00
|
|
|
protected Q_SLOTS:
|
2008-04-25 22:33:46 +02:00
|
|
|
/**
|
|
|
|
* Check if the DataContainer is still in use.
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
2008-04-25 22:33:46 +02:00
|
|
|
* If not the signal "becameUnused" will be emitted.
|
2008-08-25 18:03:56 +02:00
|
|
|
*
|
|
|
|
* Warning: The DataContainer may be invalid after calling this function, because a listener
|
|
|
|
* to becameUnused() may have deleted it.
|
|
|
|
**/
|
2008-04-25 22:33:46 +02:00
|
|
|
void checkUsage();
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
private:
|
2007-09-11 02:49:51 +02:00
|
|
|
friend class SignalRelay;
|
2008-10-11 18:19:04 +02:00
|
|
|
DataContainerPrivate *const d;
|
2007-05-20 22:36:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#endif // multiple inclusion guard
|