use weak pointers

This commit is contained in:
Aaron Seigo 2011-04-28 23:10:33 +02:00 committed by Marco Martin
parent ba985fcda5
commit 33ed49a303
4 changed files with 38 additions and 14 deletions

View File

@ -55,6 +55,7 @@ StorageJob::StorageJob(const QString& destination,
Plasma::StorageThread::self()->start();
connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob *, const QVariant &)), this, SLOT(resultSlot(StorageJob *, const QVariant &)));
qRegisterMetaType<StorageJob *>();
qRegisterMetaType<QWeakPointer<StorageJob> >();
}
StorageJob::~StorageJob()
@ -87,14 +88,15 @@ void StorageJob::start()
valueGroup = "default";
}
QWeakPointer<StorageJob> me(this);
if (operationName() == "save") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "retrieve") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "delete") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "delete", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "delete", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else if (operationName() == "expire") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params));
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantMap&, params));
} else {
setError(true);
setResult(false);

View File

@ -55,6 +55,7 @@ private:
//End StorageJob
Q_DECLARE_METATYPE(StorageJob *)
Q_DECLARE_METATYPE(QWeakPointer<StorageJob>)
class Storage : public Plasma::Service
{

View File

@ -61,7 +61,7 @@ Plasma::StorageThread *StorageThread::self()
return &privateStorageThreadSelf->self;
}
void StorageThread::initializeDb(StorageJob* caller)
void StorageThread::initializeDb(StorageJob *caller)
{
if (!m_db.open()) {
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
@ -81,8 +81,13 @@ void StorageThread::initializeDb(StorageJob* caller)
m_db.transaction();
}
void StorageThread::save(StorageJob* caller, const QVariantMap &params)
void StorageThread::save(QWeakPointer<StorageJob> wcaller, const QVariantMap &params)
{
StorageJob *caller = wcaller.data();
if (!caller) {
return;
}
initializeDb(caller);
QString valueGroup = params["group"].toString();
if (valueGroup.isEmpty()) {
@ -169,8 +174,13 @@ void StorageThread::save(StorageJob* caller, const QVariantMap &params)
emit newResult(caller, true);
}
void StorageThread::retrieve(StorageJob* caller, const QVariantMap &params)
void StorageThread::retrieve(QWeakPointer<StorageJob> wcaller, const QVariantMap &params)
{
StorageJob *caller = wcaller.data();
if (!caller) {
return;
}
initializeDb(caller);
QString valueGroup = params["group"].toString();
if (valueGroup.isEmpty()) {
@ -234,8 +244,13 @@ void StorageThread::retrieve(StorageJob* caller, const QVariantMap &params)
emit newResult(caller, result);
}
void StorageThread::deleteEntry(StorageJob* caller, const QVariantMap &params)
void StorageThread::deleteEntry(QWeakPointer<StorageJob> wcaller, const QVariantMap &params)
{
StorageJob *caller = wcaller.data();
if (!caller) {
return;
}
initializeDb(caller);
QString valueGroup = params["group"].toString();
if (valueGroup.isEmpty()) {
@ -258,8 +273,13 @@ void StorageThread::deleteEntry(StorageJob* caller, const QVariantMap &params)
emit newResult(caller, success);
}
void StorageThread::expire(StorageJob* caller, const QVariantMap &params)
void StorageThread::expire(QWeakPointer<StorageJob> wcaller, const QVariantMap &params)
{
StorageJob *caller = wcaller.data();
if (!caller) {
return;
}
initializeDb(caller);
QString valueGroup = params["group"].toString();
if (valueGroup.isEmpty()) {

View File

@ -23,6 +23,7 @@
#include <QThread>
#include <QSqlDatabase>
#include <QWeakPointer>
#include "storage_p.h"
@ -39,18 +40,18 @@ public:
void run();
static Plasma::StorageThread *self();
void initializeDb(StorageJob* caller);
public Q_SLOTS:
void save(StorageJob* caller, const QVariantMap &parameters);
void retrieve(StorageJob* caller, const QVariantMap &parameters);
void deleteEntry(StorageJob* caller, const QVariantMap &parameters);
void expire(StorageJob* caller, const QVariantMap &parameters);
void save(QWeakPointer<StorageJob> caller, const QVariantMap &parameters);
void retrieve(QWeakPointer<StorageJob> caller, const QVariantMap &parameters);
void deleteEntry(QWeakPointer<StorageJob> caller, const QVariantMap &parameters);
void expire(QWeakPointer<StorageJob> caller, const QVariantMap &parameters);
Q_SIGNALS:
void newResult(StorageJob* caller, const QVariant &result);
private:
void initializeDb(StorageJob* caller);
QSqlDatabase m_db;
};