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; DataEngine::Data data;
int connectCount;
bool dirty : 1; 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()) { if (receivers(SIGNAL(updated(QString, Plasma::DataEngine::Data))) < 1) {
++d->connectCount; // DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
emit unused(objectName());
} }
} }
void DataContainer::disconnectNotify(const char *signal) void DataContainer::disconnectNotify(const char *signal)
{ {
if (QLatin1String(signal) == QMetaObject::normalizedSignature(SIGNAL(updated(QString, Plasma::DataEngine::Data))).constData()) { checkUsage();
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());
}
}
} }
} // Plasma namespace } // Plasma namespace

View File

@ -80,6 +80,14 @@ class PLASMA_EXPORT DataContainer : public QObject
**/ **/
void checkForUpdate(); 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: Q_SIGNALS:
/** /**
* Emitted when the data has been updated, allowing visualization to * 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); void unused(const QString& source);
protected: protected:
void connectNotify(const char *signal);
void disconnectNotify(const char *signal); void disconnectNotify(const char *signal);
private: private:

View File

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