From 56429d3adc0e513eb5a69568ff66d4b2852a5c55 Mon Sep 17 00:00:00 2001 From: Michael Olbrich Date: Sat, 1 Sep 2007 22:23:03 +0000 Subject: [PATCH] check for unused DataContainers in a way that actually works. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=707450 --- datacontainer.cpp | 19 +++++-------------- datacontainer.h | 9 ++++++++- dataengine.cpp | 6 +++++- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/datacontainer.cpp b/datacontainer.cpp index f77cedc4f..765a0315d 100644 --- a/datacontainer.cpp +++ b/datacontainer.cpp @@ -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 diff --git a/datacontainer.h b/datacontainer.h index 8864012a7..1e39f1987 100644 --- a/datacontainer.h +++ b/datacontainer.h @@ -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: diff --git a/dataengine.cpp b/dataengine.cpp index 719c42133..c0d3d6454 100644 --- a/dataengine.cpp +++ b/dataengine.cpp @@ -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); } }