From 33ed49a303c50adfd01735d04486fa1a867a7dd7 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 28 Apr 2011 23:10:33 +0200 Subject: [PATCH] use weak pointers --- private/storage.cpp | 10 ++++++---- private/storage_p.h | 1 + private/storagethread.cpp | 30 +++++++++++++++++++++++++----- private/storagethread_p.h | 11 ++++++----- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/private/storage.cpp b/private/storage.cpp index 010881d05..058814658 100644 --- a/private/storage.cpp +++ b/private/storage.cpp @@ -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(); + qRegisterMetaType >(); } StorageJob::~StorageJob() @@ -87,14 +88,15 @@ void StorageJob::start() valueGroup = "default"; } + QWeakPointer 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, 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, 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, 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, me), Q_ARG(const QVariantMap&, params)); } else { setError(true); setResult(false); diff --git a/private/storage_p.h b/private/storage_p.h index 27d3c4cfb..deb117919 100644 --- a/private/storage_p.h +++ b/private/storage_p.h @@ -55,6 +55,7 @@ private: //End StorageJob Q_DECLARE_METATYPE(StorageJob *) +Q_DECLARE_METATYPE(QWeakPointer) class Storage : public Plasma::Service { diff --git a/private/storagethread.cpp b/private/storagethread.cpp index 31e0a7758..f3515169c 100644 --- a/private/storagethread.cpp +++ b/private/storagethread.cpp @@ -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 ¶ms) +void StorageThread::save(QWeakPointer wcaller, const QVariantMap ¶ms) { + 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 ¶ms) emit newResult(caller, true); } -void StorageThread::retrieve(StorageJob* caller, const QVariantMap ¶ms) +void StorageThread::retrieve(QWeakPointer wcaller, const QVariantMap ¶ms) { + 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 ¶ms) emit newResult(caller, result); } -void StorageThread::deleteEntry(StorageJob* caller, const QVariantMap ¶ms) +void StorageThread::deleteEntry(QWeakPointer wcaller, const QVariantMap ¶ms) { + 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 ¶ms) emit newResult(caller, success); } -void StorageThread::expire(StorageJob* caller, const QVariantMap ¶ms) +void StorageThread::expire(QWeakPointer wcaller, const QVariantMap ¶ms) { + StorageJob *caller = wcaller.data(); + if (!caller) { + return; + } + initializeDb(caller); QString valueGroup = params["group"].toString(); if (valueGroup.isEmpty()) { diff --git a/private/storagethread_p.h b/private/storagethread_p.h index ae00edb4e..5a67e8f90 100644 --- a/private/storagethread_p.h +++ b/private/storagethread_p.h @@ -23,6 +23,7 @@ #include #include +#include #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 ¶meters); - void retrieve(StorageJob* caller, const QVariantMap ¶meters); - void deleteEntry(StorageJob* caller, const QVariantMap ¶meters); - void expire(StorageJob* caller, const QVariantMap ¶meters); + void save(QWeakPointer caller, const QVariantMap ¶meters); + void retrieve(QWeakPointer caller, const QVariantMap ¶meters); + void deleteEntry(QWeakPointer caller, const QVariantMap ¶meters); + void expire(QWeakPointer caller, const QVariantMap ¶meters); Q_SIGNALS: void newResult(StorageJob* caller, const QVariant &result); private: + void initializeDb(StorageJob* caller); QSqlDatabase m_db; };