* count the number of objects connected to each datasource

* data sources now signal when the are unused
* listen to the unused signal for sources created on demand via dataSourceRequested

svn path=/trunk/KDE/kdebase/workspace/lib/plasma/; revision=673389
This commit is contained in:
Aaron J. Seigo 2007-06-10 05:39:27 +00:00
parent 922aada9ab
commit 0fbe1e47c1
4 changed files with 75 additions and 14 deletions

View File

@ -64,9 +64,9 @@ class DataEngine::Private
return 0;
}
kDebug() << "DataEngine " << engine->objectName()
/* kDebug() << "DataEngine " << engine->objectName()
<< ": could not find DataSource " << sourceName
<< ", creating" << endl;
<< ", creating" << endl;*/
DataSource* s = new DataSource(engine);
s->setObjectName(sourceName);
sources.insert(sourceName, s);
@ -137,10 +137,19 @@ void DataEngine::connectSource(const QString& source, QObject* visualization) co
DataSource* s = d->source(source, false);
if (!s) {
kDebug() << "requesting source " << source << endl;
d->dataSourceRequested(source);
s = d->source(source);
kDebug() << "and no we have " << s << endl;
// we didn't find a data source, so give the engine an opportunity to make one
if (d->dataSourceRequested(source)) {
s = d->source(source);
if (s) {
// now we have a source; since it was created on demand, assume
// it should be removed when not used
connect(s, SIGNAL(unused(QString)), this, SLOT(removeDataSource(QString)));
}
}
}
if (!s) {
return;
}
connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
@ -150,6 +159,18 @@ void DataEngine::connectSource(const QString& source, QObject* visualization) co
Q_ARG(Plasma::DataEngine::Data, s->data()));
}
void DataEngine::disconnectSource(const QString& source, QObject* visualization) const
{
DataSource* s = d->source(source, false);
if (!s) {
return;
}
disconnect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
}
void DataEngine::connectAllSources(QObject* visualization) const
{
foreach (const DataSource* s, d->sources) {
@ -238,6 +259,7 @@ Plasma::DataSource* DataEngine::createDataSource(const QString& source, const QS
void DataEngine::removeDataSource(const QString& source)
{
//kDebug() << "removing source " << source << endl;
SourceDict::iterator it = d->sources.find(source);
if (it != d->sources.end()) {
emit dataSourceRemoved(it.key());

View File

@ -85,6 +85,14 @@ class PLASMA_EXPORT DataEngine : public QObject
**/
void connectSource(const QString& source, QObject* visualization) const;
/**
* 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
**/
void disconnectSource(const QString& source, QObject* visualization) const;
/**
* Connects all sources to an object for data updates. The object must
* have a slot with the following signature:
@ -215,12 +223,6 @@ class PLASMA_EXPORT DataEngine : public QObject
void createDataSource(const QString& source,
const QString& domain = QString());*/
/**
* Removes a data source.
* @param source the name of the data source to remove
**/
void removeDataSource(const QString& source);
/**
* Removes all data sources
**/
@ -247,6 +249,12 @@ class PLASMA_EXPORT DataEngine : public QObject
*/
void checkForUpdates();
/**
* Removes a data source.
* @param source the name of the data source to remove
**/
void removeDataSource(const QString& source);
private:
class Private;
Private* const d;

View File

@ -18,6 +18,7 @@
#include "datasource.h"
#include <QAtomic>
#include <QVariant>
#include <KDebug>
@ -25,7 +26,6 @@
namespace Plasma
{
class DataSource::Private
{
public:
@ -34,7 +34,8 @@ class DataSource::Private
{}
DataEngine::Data data;
bool dirty;
QAtomic connectCount;
bool dirty : 1;
};
DataSource::DataSource(QObject* parent)
@ -67,6 +68,27 @@ void DataSource::checkForUpdate()
}
}
void DataSource::connectNotify(const char *signal)
{
if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
d->connectCount.ref();
}
}
void DataSource::disconnectNotify(const char *signal)
{
if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
if (d->connectCount > 0) {
d->connectCount.deref();
}
if (d->connectCount < 1) {
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
emit unused(objectName());
}
}
}
} // Plasma namespace
#include "datasource.moc"

View File

@ -80,6 +80,15 @@ class PLASMA_EXPORT DataSource : public QObject
**/
void updated(const QString& source, const Plasma::DataEngine::Data& data);
/**
* Emitted when this source becomes unused
**/
void unused(const QString& source);
protected:
void connectNotify(const char *signal);
void disconnectNotify(const char *signal);
private:
class Private;
Private* const d;