2006-04-13 02:11:16 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2006-04-13 02:11:16 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +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.
|
2006-04-13 02:11:16 +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-05-20 22:36:59 +02:00
|
|
|
#ifndef PLASMA_DATAENGINE_H
|
|
|
|
#define PLASMA_DATAENGINE_H
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include <QtCore/QHash>
|
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtCore/QStringList>
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2007-06-17 01:38:56 +02:00
|
|
|
#include <kgenericfactory.h>
|
2007-05-24 22:01:12 +02:00
|
|
|
|
2007-06-02 19:29:39 +02:00
|
|
|
#include <plasma/plasma_export.h>
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2006-04-13 02:11:16 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
class DataContainer;
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class DataEngine
|
|
|
|
* @brief Data provider for plasmoids (Plasma plugins)
|
|
|
|
*
|
|
|
|
* This is the base class for DataEngines, which provide access to bodies of
|
|
|
|
* data via a common and consistent interface. The common use of a DataEngine
|
|
|
|
* is to provide data to a widget for display. This allows a user interface
|
|
|
|
* element to show all sorts of data: as long as there is a DataEngine, the
|
|
|
|
* data is retrievable.
|
|
|
|
*
|
2007-05-23 09:13:00 +02:00
|
|
|
* DataEngines are loaded as plugins on demand and provide zero, one or more
|
|
|
|
* data sources which are identified by name. For instance, a network
|
|
|
|
* DataEngine might provide a data source for each network interface.
|
2007-05-23 08:38:44 +02:00
|
|
|
**/
|
2007-05-20 22:13:46 +02:00
|
|
|
class PLASMA_EXPORT DataEngine : public QObject
|
2006-04-13 02:11:16 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2007-08-04 14:46:29 +02:00
|
|
|
Q_PROPERTY( QStringList sources READ sources )
|
|
|
|
Q_PROPERTY( bool valid READ isValid )
|
|
|
|
Q_PROPERTY( QString icon READ icon WRITE setIcon )
|
2006-04-13 02:11:16 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef QHash<QString, DataEngine*> Dict;
|
2007-05-20 22:36:59 +02:00
|
|
|
typedef QHash<QString, QVariant> Data;
|
2007-05-21 06:29:00 +02:00
|
|
|
typedef QHashIterator<QString, QVariant> DataIterator;
|
2007-07-23 02:22:16 +02:00
|
|
|
typedef QHash<QString, DataContainer*> SourceDict;
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*
|
|
|
|
* @param parent The parent object.
|
|
|
|
**/
|
2007-06-01 00:16:30 +02:00
|
|
|
explicit DataEngine(QObject* parent = 0);
|
2006-04-13 02:11:16 +02:00
|
|
|
virtual ~DataEngine();
|
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
2007-06-13 06:56:49 +02:00
|
|
|
* @return a list of all the data sources available via this DataEngine
|
|
|
|
* Whether these sources are currently available (which is what
|
|
|
|
* the default implementation provides) or not is up to the
|
|
|
|
* DataEngine to decide.
|
2007-05-23 08:38:44 +02:00
|
|
|
**/
|
2007-06-13 06:56:49 +02:00
|
|
|
virtual QStringList sources() const;
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects a source to an object for data updates. The object must
|
|
|
|
* have a slot with the following signature:
|
|
|
|
*
|
2007-08-29 04:57:04 +02:00
|
|
|
* updated(QString sourceName, Plasma::DataEngine::Data data)
|
2007-05-23 08:38:44 +02:00
|
|
|
*
|
|
|
|
* The data is a QHash of QVariants keyed by QString names, allowing
|
|
|
|
* one data source to provide sets of related data.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param visualization the object to connect the data source to
|
|
|
|
**/
|
2007-08-05 14:39:13 +02:00
|
|
|
Q_INVOKABLE void connectSource(const QString& source, QObject* visualization) const;
|
2007-05-23 08:38:44 +02:00
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
/**
|
|
|
|
* Disconnects a source to an object that was receiving data updates.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param visualization the object to connect the data source to
|
|
|
|
**/
|
2007-08-05 14:39:13 +02:00
|
|
|
Q_INVOKABLE void disconnectSource(const QString& source, QObject* visualization) const;
|
2007-06-10 07:39:27 +02:00
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
/**
|
|
|
|
* Connects all sources to an object for data updates. The object must
|
|
|
|
* have a slot with the following signature:
|
|
|
|
*
|
|
|
|
* SLOT(updated(QString, Plasma::DataEngine::Data))
|
|
|
|
*
|
|
|
|
* The data is a QHash of QVariants keyed by QString names, allowing
|
|
|
|
* one data source to provide sets of related data.
|
|
|
|
*
|
|
|
|
* @param visualization the object to connect the data source to
|
|
|
|
**/
|
2007-08-05 14:39:13 +02:00
|
|
|
Q_INVOKABLE void connectAllSources(QObject* viualization) const;
|
2007-05-28 07:43:54 +02:00
|
|
|
|
2007-08-29 04:57:04 +02:00
|
|
|
/**
|
|
|
|
* Retrevies a pointer to the DataContainer for a given source. This method
|
|
|
|
* should not be used if possible. An exception is for script engines that
|
|
|
|
* can not provide a QMetaObject as required by connectSource for the initial
|
|
|
|
* call to updated(). Using this method, such engines can provide their own
|
|
|
|
* connectSource API.
|
|
|
|
*
|
|
|
|
* @arg source the name of the source.
|
|
|
|
* @return pointer to a DataContainer, or zero on failure
|
|
|
|
**/
|
|
|
|
Q_INVOKABLE DataContainer* containerForSource(const QString &source);
|
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Gets the Data associated with a data source.
|
|
|
|
*
|
|
|
|
* The data is a QHash of QVariants keyed by QString names, allowing
|
|
|
|
* one data source to provide sets of related data.
|
|
|
|
*
|
|
|
|
* @param source the data source to retrieve the data for
|
|
|
|
* @return the Data associated with the source; if the source doesn't
|
|
|
|
* exist an empty data set is returned
|
|
|
|
**/
|
2007-08-28 16:26:31 +02:00
|
|
|
Q_INVOKABLE DataEngine::Data query(const QString& source) const;
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Reference counting method. Calling this method increases the count
|
|
|
|
* by one.
|
|
|
|
**/
|
2006-04-13 02:11:16 +02:00
|
|
|
void ref();
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference counting method. Calling this method decreases the count
|
|
|
|
* by one.
|
|
|
|
**/
|
2006-04-13 02:11:16 +02:00
|
|
|
void deref();
|
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Reference counting method. Used to determine if this DataEngine is
|
|
|
|
* used.
|
2007-06-11 23:11:22 +02:00
|
|
|
* @return true if the reference count is non-zero
|
2007-05-23 08:38:44 +02:00
|
|
|
**/
|
2007-05-23 10:27:09 +02:00
|
|
|
bool isUsed() const;
|
2007-05-22 04:49:54 +02:00
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this engine is valid, otherwise returns false
|
|
|
|
**/
|
|
|
|
bool isValid() const;
|
|
|
|
|
2007-05-23 09:13:00 +02:00
|
|
|
/**
|
|
|
|
* Sets the icon for this data engine
|
|
|
|
**/
|
|
|
|
void setIcon(const QString& icon);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the name of the icon for this data engine; and empty string
|
|
|
|
* is returned if there is no associated icon.
|
|
|
|
**/
|
2007-05-23 10:27:09 +02:00
|
|
|
QString icon() const;
|
|
|
|
|
2007-05-22 04:49:54 +02:00
|
|
|
Q_SIGNALS:
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Emitted when a new data source is created
|
|
|
|
* @param source the name of the new data source
|
|
|
|
**/
|
2007-06-13 06:56:49 +02:00
|
|
|
void newSource(const QString& source);
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emitted when a data source is removed.
|
|
|
|
* @param source the name of the data source that was removed
|
|
|
|
**/
|
2007-06-13 06:56:49 +02:00
|
|
|
void sourceRemoved(const QString& source);
|
2007-05-22 04:49:54 +02:00
|
|
|
|
2006-04-13 02:11:16 +02:00
|
|
|
protected:
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* This method is called when the DataEngine is started. When this
|
|
|
|
* method is called the DataEngine is fully constructed and ready to be
|
|
|
|
* used. This method should be reimplemented by DataEngine subclasses
|
|
|
|
* which have the need to perform a startup routine.
|
|
|
|
**/
|
2006-04-13 02:11:16 +02:00
|
|
|
virtual void init();
|
2007-05-23 08:38:44 +02:00
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
/**
|
|
|
|
* When a source that does not currently exist is requested by the
|
|
|
|
* consumer, this method is called to give the DataEngine the
|
|
|
|
* opportunity to create one.
|
|
|
|
*
|
2007-07-02 12:16:39 +02:00
|
|
|
* The name of the data source (e.g. the source parameter passed into
|
|
|
|
* setData) it must be the same as the name passed to sourceRequested
|
|
|
|
* otherwise the requesting visualization may not receive notice of a
|
|
|
|
* data update.
|
|
|
|
*
|
2007-07-23 02:22:16 +02:00
|
|
|
* @return true if a DataContainer was set up, false otherwise
|
2007-06-10 06:03:50 +02:00
|
|
|
*/
|
2007-06-13 06:56:49 +02:00
|
|
|
virtual bool sourceRequested(const QString &name);
|
2007-06-10 06:03:50 +02:00
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Sets a value for a data source. If the source
|
|
|
|
* doesn't exist then it is created.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param value the data to associated with the source
|
|
|
|
**/
|
2007-06-10 06:03:50 +02:00
|
|
|
void setData(const QString &source, const QVariant &value);
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value for a data source. If the source
|
|
|
|
* doesn't exist then it is created.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param key the key to use for the data
|
|
|
|
* @param value the data to associated with the source
|
|
|
|
**/
|
2007-05-19 10:38:46 +02:00
|
|
|
void setData(const QString& source, const QString& key, const QVariant& value);
|
2007-05-23 08:38:44 +02:00
|
|
|
|
2007-07-17 20:08:57 +02:00
|
|
|
/**
|
|
|
|
* Adds a set of data to a data source. If the source
|
|
|
|
* doesn't exist then it is created.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param data the data to add to the source
|
|
|
|
**/
|
|
|
|
void setData(const QString &source, const Data &data);
|
|
|
|
|
2007-07-09 11:33:15 +02:00
|
|
|
/**
|
|
|
|
* Clears all the data associated with a data source.
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
**/
|
|
|
|
void clearData(const QString& source);
|
|
|
|
|
2007-06-17 19:38:54 +02:00
|
|
|
/**
|
|
|
|
* Removes a data entry from a source
|
|
|
|
*
|
|
|
|
* @param source the name of the data source
|
|
|
|
* @param key the data entry to remove
|
|
|
|
**/
|
|
|
|
void removeData(const QString& source, const QString& key);
|
|
|
|
|
2007-05-23 08:38:44 +02:00
|
|
|
/**
|
|
|
|
* Adds an already constructed data source. The DataEngine takes
|
2007-07-23 02:22:16 +02:00
|
|
|
* ownership of the DataContainer object.
|
|
|
|
* @param source the DataContainer to add to the DataEngine
|
2007-05-23 08:38:44 +02:00
|
|
|
**/
|
2007-07-23 02:22:16 +02:00
|
|
|
void addSource(DataContainer* source);
|
2007-05-23 08:38:44 +02:00
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
/**
|
|
|
|
* Sets an upper limit on the number of data sources to keep in this engine.
|
|
|
|
* If the limit is exceeded, then the oldest data source, as defined by last
|
|
|
|
* update, is dropped.
|
|
|
|
*
|
|
|
|
* @param limit the maximum number of sources to keep active
|
|
|
|
**/
|
|
|
|
void setSourceLimit(uint limit);
|
|
|
|
|
2007-07-23 02:22:16 +02:00
|
|
|
/* DataContainer* domain(const QString &domain);
|
|
|
|
void createDataContainer(const QString& source,
|
2007-05-19 10:38:46 +02:00
|
|
|
const QString& domain = QString());*/
|
2007-05-23 08:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all data sources
|
|
|
|
**/
|
2007-06-13 06:56:49 +02:00
|
|
|
void clearSources();
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
/**
|
|
|
|
* Sets whether or not this engine is valid, e.g. can be used.
|
|
|
|
* In practice, only the internal fall-back engine, the NullEngine
|
|
|
|
* should have need for this.
|
|
|
|
*
|
|
|
|
* @param valid whether or not the engine is valid
|
|
|
|
**/
|
2007-06-04 18:11:55 +02:00
|
|
|
void setValid(bool valid);
|
2007-06-03 00:23:26 +02:00
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
/**
|
2007-07-23 02:22:16 +02:00
|
|
|
* @return the list of active DataContainers.
|
2007-06-10 06:03:50 +02:00
|
|
|
*/
|
|
|
|
SourceDict sourceDict() const;
|
|
|
|
|
|
|
|
protected Q_SLOTS:
|
|
|
|
/**
|
2007-07-23 02:22:16 +02:00
|
|
|
* Call this method when you call setData directly on a DataContainer instead
|
2007-07-02 12:16:39 +02:00
|
|
|
* of using the DataEngine::setData methods.
|
2007-06-10 06:03:50 +02:00
|
|
|
* If this method is not called, no updated(..) signals will be emitted!
|
|
|
|
*/
|
2007-05-19 10:38:46 +02:00
|
|
|
void checkForUpdates();
|
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
/**
|
|
|
|
* Removes a data source.
|
|
|
|
* @param source the name of the data source to remove
|
|
|
|
**/
|
2007-06-11 23:10:44 +02:00
|
|
|
void removeSource(const QString& source);
|
2007-06-10 07:39:27 +02:00
|
|
|
|
2007-07-02 12:16:39 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
**/
|
|
|
|
void startInit();
|
|
|
|
|
2006-04-13 02:11:16 +02:00
|
|
|
private:
|
|
|
|
class Private;
|
2007-05-19 10:38:46 +02:00
|
|
|
Private* const d;
|
2006-04-13 02:11:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
#define K_EXPORT_PLASMA_DATAENGINE(libname, classname) \
|
2007-08-29 03:49:44 +02:00
|
|
|
K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
|
|
|
|
K_EXPORT_PLUGIN(factory("plasma_engine_" #libname))
|
2006-04-13 02:11:16 +02:00
|
|
|
#endif // multiple inclusion guard
|
|
|
|
|