check for unused DataContainers in a way that actually works.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=707450
This commit is contained in:
Michael Olbrich 2007-09-01 22:23:03 +00:00
parent c27291a3fc
commit 56429d3adc
3 changed files with 18 additions and 16 deletions

View File

@ -33,7 +33,6 @@ class DataContainer::Private
{}
DataEngine::Data data;
int connectCount;
bool dirty : 1;
};
@ -87,25 +86,17 @@ void DataContainer::checkForUpdate()
}
}
void DataContainer::connectNotify(const char *signal)
void DataContainer::checkUsage()
{
if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
++d->connectCount;
if (receivers(SIGNAL(updated(QString, Plasma::DataEngine::Data))) < 1) {
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
emit unused(objectName());
}
}
void DataContainer::disconnectNotify(const char *signal)
{
if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) {
if (d->connectCount > 0) {
--d->connectCount;
}
if (d->connectCount < 1) {
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
emit unused(objectName());
}
}
checkUsage();
}
} // Plasma namespace

View File

@ -80,6 +80,14 @@ class PLASMA_EXPORT DataContainer : public QObject
**/
void checkForUpdate();
public Q_SLOTS:
/**
* Check if the DataContainer is still in use.
* If not the signal "unused" will be emitted.
* Warning: The DataContainer may be invalid after calling this function.
*/
void checkUsage();
Q_SIGNALS:
/**
* Emitted when the data has been updated, allowing visualization to
@ -93,7 +101,6 @@ class PLASMA_EXPORT DataContainer : public QObject
void unused(const QString& source);
protected:
void connectNotify(const char *signal);
void disconnectNotify(const char *signal);
private:

View File

@ -154,6 +154,7 @@ void DataEngine::connectSource(const QString& source, QObject* visualization) co
return;
}
connect(visualization, SIGNAL(destroyed(QObject*)), s, SLOT(checkUsage()), Qt::QueuedConnection);
connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
QMetaObject::invokeMethod(visualization, "updated",
@ -200,7 +201,9 @@ DataEngine::Data DataEngine::query(const QString& source) const
return DataEngine::Data();
}
return s->data();
DataEngine::Data data = s->data();
s->checkUsage();
return data;
}
void DataEngine::startInit()
@ -296,6 +299,7 @@ void DataEngine::removeSource(const QString& source)
SourceDict::iterator it = d->sources.find(source);
if (it != d->sources.end()) {
emit sourceRemoved(it.key());
it.value()->deleteLater();
d->sources.erase(it);
}
}