use weak pointers
This commit is contained in:
parent
ba985fcda5
commit
33ed49a303
@ -55,6 +55,7 @@ StorageJob::StorageJob(const QString& destination,
|
|||||||
Plasma::StorageThread::self()->start();
|
Plasma::StorageThread::self()->start();
|
||||||
connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob *, const QVariant &)), this, SLOT(resultSlot(StorageJob *, const QVariant &)));
|
connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob *, const QVariant &)), this, SLOT(resultSlot(StorageJob *, const QVariant &)));
|
||||||
qRegisterMetaType<StorageJob *>();
|
qRegisterMetaType<StorageJob *>();
|
||||||
|
qRegisterMetaType<QWeakPointer<StorageJob> >();
|
||||||
}
|
}
|
||||||
|
|
||||||
StorageJob::~StorageJob()
|
StorageJob::~StorageJob()
|
||||||
@ -87,14 +88,15 @@ void StorageJob::start()
|
|||||||
valueGroup = "default";
|
valueGroup = "default";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QWeakPointer<StorageJob> me(this);
|
||||||
if (operationName() == "save") {
|
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") {
|
} 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") {
|
} 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") {
|
} 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 {
|
} else {
|
||||||
setError(true);
|
setError(true);
|
||||||
setResult(false);
|
setResult(false);
|
||||||
|
@ -55,6 +55,7 @@ private:
|
|||||||
//End StorageJob
|
//End StorageJob
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(StorageJob *)
|
Q_DECLARE_METATYPE(StorageJob *)
|
||||||
|
Q_DECLARE_METATYPE(QWeakPointer<StorageJob>)
|
||||||
|
|
||||||
class Storage : public Plasma::Service
|
class Storage : public Plasma::Service
|
||||||
{
|
{
|
||||||
|
@ -61,7 +61,7 @@ Plasma::StorageThread *StorageThread::self()
|
|||||||
return &privateStorageThreadSelf->self;
|
return &privateStorageThreadSelf->self;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StorageThread::initializeDb(StorageJob* caller)
|
void StorageThread::initializeDb(StorageJob *caller)
|
||||||
{
|
{
|
||||||
if (!m_db.open()) {
|
if (!m_db.open()) {
|
||||||
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
|
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
|
||||||
@ -81,8 +81,13 @@ void StorageThread::initializeDb(StorageJob* caller)
|
|||||||
m_db.transaction();
|
m_db.transaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StorageThread::save(StorageJob* caller, const QVariantMap ¶ms)
|
void StorageThread::save(QWeakPointer<StorageJob> wcaller, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
|
StorageJob *caller = wcaller.data();
|
||||||
|
if (!caller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
initializeDb(caller);
|
initializeDb(caller);
|
||||||
QString valueGroup = params["group"].toString();
|
QString valueGroup = params["group"].toString();
|
||||||
if (valueGroup.isEmpty()) {
|
if (valueGroup.isEmpty()) {
|
||||||
@ -169,8 +174,13 @@ void StorageThread::save(StorageJob* caller, const QVariantMap ¶ms)
|
|||||||
emit newResult(caller, true);
|
emit newResult(caller, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StorageThread::retrieve(StorageJob* caller, const QVariantMap ¶ms)
|
void StorageThread::retrieve(QWeakPointer<StorageJob> wcaller, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
|
StorageJob *caller = wcaller.data();
|
||||||
|
if (!caller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
initializeDb(caller);
|
initializeDb(caller);
|
||||||
QString valueGroup = params["group"].toString();
|
QString valueGroup = params["group"].toString();
|
||||||
if (valueGroup.isEmpty()) {
|
if (valueGroup.isEmpty()) {
|
||||||
@ -234,8 +244,13 @@ void StorageThread::retrieve(StorageJob* caller, const QVariantMap ¶ms)
|
|||||||
emit newResult(caller, result);
|
emit newResult(caller, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StorageThread::deleteEntry(StorageJob* caller, const QVariantMap ¶ms)
|
void StorageThread::deleteEntry(QWeakPointer<StorageJob> wcaller, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
|
StorageJob *caller = wcaller.data();
|
||||||
|
if (!caller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
initializeDb(caller);
|
initializeDb(caller);
|
||||||
QString valueGroup = params["group"].toString();
|
QString valueGroup = params["group"].toString();
|
||||||
if (valueGroup.isEmpty()) {
|
if (valueGroup.isEmpty()) {
|
||||||
@ -258,8 +273,13 @@ void StorageThread::deleteEntry(StorageJob* caller, const QVariantMap ¶ms)
|
|||||||
emit newResult(caller, success);
|
emit newResult(caller, success);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StorageThread::expire(StorageJob* caller, const QVariantMap ¶ms)
|
void StorageThread::expire(QWeakPointer<StorageJob> wcaller, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
|
StorageJob *caller = wcaller.data();
|
||||||
|
if (!caller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
initializeDb(caller);
|
initializeDb(caller);
|
||||||
QString valueGroup = params["group"].toString();
|
QString valueGroup = params["group"].toString();
|
||||||
if (valueGroup.isEmpty()) {
|
if (valueGroup.isEmpty()) {
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
|
#include <QWeakPointer>
|
||||||
|
|
||||||
#include "storage_p.h"
|
#include "storage_p.h"
|
||||||
|
|
||||||
@ -39,18 +40,18 @@ public:
|
|||||||
void run();
|
void run();
|
||||||
|
|
||||||
static Plasma::StorageThread *self();
|
static Plasma::StorageThread *self();
|
||||||
void initializeDb(StorageJob* caller);
|
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void save(StorageJob* caller, const QVariantMap ¶meters);
|
void save(QWeakPointer<StorageJob> caller, const QVariantMap ¶meters);
|
||||||
void retrieve(StorageJob* caller, const QVariantMap ¶meters);
|
void retrieve(QWeakPointer<StorageJob> caller, const QVariantMap ¶meters);
|
||||||
void deleteEntry(StorageJob* caller, const QVariantMap ¶meters);
|
void deleteEntry(QWeakPointer<StorageJob> caller, const QVariantMap ¶meters);
|
||||||
void expire(StorageJob* caller, const QVariantMap ¶meters);
|
void expire(QWeakPointer<StorageJob> caller, const QVariantMap ¶meters);
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void newResult(StorageJob* caller, const QVariant &result);
|
void newResult(StorageJob* caller, const QVariant &result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void initializeDb(StorageJob* caller);
|
||||||
QSqlDatabase m_db;
|
QSqlDatabase m_db;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user