2010-07-13 03:07:30 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// storage.cpp //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 2010 Brian Pritchett <batenkaitos@gmail.com> //
|
2010-10-22 14:29:35 +02:00
|
|
|
// Copyright (C) 2010 Marco Martin <mart@kde.org> //
|
2010-07-13 03:07:30 +02:00
|
|
|
// //
|
|
|
|
// This library is free software; you can redistribute it and/or //
|
|
|
|
// modify it under the terms of the GNU Lesser General Public //
|
|
|
|
// License as published by the Free Software Foundation; either //
|
|
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This library is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
|
|
|
// Lesser General Public License for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU Lesser General Public //
|
|
|
|
// License along with this library; if not, write to the Free Software //
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
|
|
|
|
// 02110-1301 USA //
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-04-28 17:28:03 +02:00
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
#include "private/storage_p.h"
|
2010-10-01 18:58:15 +02:00
|
|
|
|
|
|
|
//Qt
|
2011-04-28 17:28:03 +02:00
|
|
|
#include <QSqlDriver>
|
2010-10-01 18:58:15 +02:00
|
|
|
#include <QSqlError>
|
2011-04-28 17:28:03 +02:00
|
|
|
#include <QSqlField>
|
2010-10-01 18:58:15 +02:00
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlRecord>
|
2011-01-07 11:51:29 +01:00
|
|
|
#include <QThreadStorage>
|
2010-10-01 18:58:15 +02:00
|
|
|
|
|
|
|
//KDE
|
2010-07-13 03:07:30 +02:00
|
|
|
#include <kdebug.h>
|
2012-06-12 15:01:57 +02:00
|
|
|
|
2010-10-01 18:58:15 +02:00
|
|
|
|
2010-10-23 14:04:24 +02:00
|
|
|
//Plasma
|
|
|
|
#include "applet.h"
|
|
|
|
#include "dataengine.h"
|
|
|
|
#include "abstractrunner.h"
|
2011-04-28 18:40:02 +02:00
|
|
|
#include "storagethread_p.h"
|
2010-10-23 14:04:24 +02:00
|
|
|
|
2011-01-07 11:51:29 +01:00
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
StorageJob::StorageJob(const QString& destination,
|
|
|
|
const QString& operation,
|
2011-05-26 19:01:44 +02:00
|
|
|
const QHash<QString, QVariant>& parameters,
|
2010-07-13 03:07:30 +02:00
|
|
|
QObject *parent)
|
2011-04-28 16:30:44 +02:00
|
|
|
: ServiceJob(destination, operation, parameters, parent),
|
|
|
|
m_clientName(destination)
|
2010-07-13 03:07:30 +02:00
|
|
|
{
|
2011-04-28 18:40:02 +02:00
|
|
|
Plasma::StorageThread::self()->start();
|
2012-01-22 03:11:26 +01:00
|
|
|
connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob*,QVariant)), this, SLOT(resultSlot(StorageJob*,QVariant)));
|
2011-04-28 18:40:02 +02:00
|
|
|
qRegisterMetaType<StorageJob *>();
|
2011-04-28 23:10:33 +02:00
|
|
|
qRegisterMetaType<QWeakPointer<StorageJob> >();
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
|
2011-01-07 00:26:14 +01:00
|
|
|
StorageJob::~StorageJob()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-28 16:30:44 +02:00
|
|
|
void StorageJob::setData(const QVariantHash &data)
|
2011-04-28 14:14:11 +02:00
|
|
|
{
|
|
|
|
m_data = data;
|
|
|
|
}
|
|
|
|
|
2011-04-28 16:30:44 +02:00
|
|
|
QVariantHash StorageJob::data() const
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
QString StorageJob::clientName() const
|
|
|
|
{
|
|
|
|
return m_clientName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
void StorageJob::start()
|
|
|
|
{
|
2011-04-28 18:40:02 +02:00
|
|
|
//FIXME: QHASH
|
2011-05-26 19:01:44 +02:00
|
|
|
QHash<QString, QVariant> params = parameters();
|
2010-10-01 18:58:15 +02:00
|
|
|
|
2010-10-23 14:14:33 +02:00
|
|
|
QString valueGroup = params["group"].toString();
|
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
valueGroup = "default";
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:10:33 +02:00
|
|
|
QWeakPointer<StorageJob> me(this);
|
2010-07-13 03:07:30 +02:00
|
|
|
if (operationName() == "save") {
|
2011-05-26 19:01:44 +02:00
|
|
|
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
|
2010-07-13 03:07:30 +02:00
|
|
|
} else if (operationName() == "retrieve") {
|
2011-05-26 19:01:44 +02:00
|
|
|
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "retrieve", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
|
2010-11-02 23:05:48 +01:00
|
|
|
} else if (operationName() == "delete") {
|
2011-05-26 19:01:44 +02:00
|
|
|
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "deleteEntry", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
|
2010-10-22 23:22:15 +02:00
|
|
|
} else if (operationName() == "expire") {
|
2011-05-26 19:01:44 +02:00
|
|
|
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "expire", Qt::QueuedConnection, Q_ARG(QWeakPointer<StorageJob>, me), Q_ARG(const QVariantHash&, params));
|
2010-10-01 18:58:15 +02:00
|
|
|
} else {
|
|
|
|
setError(true);
|
2011-04-28 22:46:38 +02:00
|
|
|
setResult(false);
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
void StorageJob::resultSlot(StorageJob *job, const QVariant &result)
|
|
|
|
{
|
|
|
|
if (job == this) {
|
2011-04-28 23:37:25 +02:00
|
|
|
if (result.type() == QVariant::Hash) {
|
|
|
|
m_data = result.toHash();
|
|
|
|
}
|
2011-04-28 21:31:11 +02:00
|
|
|
setResult(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 19:01:44 +02:00
|
|
|
Plasma::ServiceJob* Storage::createJob(const QString &operation, QHash<QString, QVariant> ¶meters)
|
2010-07-13 03:07:30 +02:00
|
|
|
{
|
2011-04-28 15:18:45 +02:00
|
|
|
if (m_clientName.isEmpty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-23 14:04:24 +02:00
|
|
|
return new StorageJob(m_clientName, operation, parameters, this);
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Storage implementation
|
2011-04-28 15:18:45 +02:00
|
|
|
Storage::Storage(QObject* parent)
|
|
|
|
: Plasma::Service(parent),
|
|
|
|
m_clientName("data")
|
2010-07-13 03:07:30 +02:00
|
|
|
{
|
2010-10-23 14:04:24 +02:00
|
|
|
//search among parents for an applet or dataengine: if found call the table as its plugin name
|
|
|
|
QObject *parentObject = this;
|
|
|
|
|
|
|
|
while ((parentObject = parentObject->parent())) {
|
|
|
|
Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(parentObject);
|
|
|
|
if (applet) {
|
2013-02-12 11:46:26 +01:00
|
|
|
m_clientName = applet->pluginInfo().pluginName();
|
2010-10-23 14:04:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::DataEngine *engine = qobject_cast<Plasma::DataEngine *>(parentObject);
|
|
|
|
if (engine) {
|
|
|
|
m_clientName = engine->pluginName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::AbstractRunner *runner = qobject_cast<Plasma::AbstractRunner *>(parentObject);
|
|
|
|
if (runner) {
|
|
|
|
m_clientName = runner->id();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-23 19:28:08 +02:00
|
|
|
m_clientName = m_clientName.replace('.', "_");
|
|
|
|
m_clientName = m_clientName.replace('-', "_");
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
setName("storage");
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::~Storage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-22 15:37:37 +01:00
|
|
|
#include "moc_storage_p.cpp"
|