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 //
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "private/storage_p.h"
|
2010-10-01 18:58:15 +02:00
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlRecord>
|
|
|
|
|
|
|
|
//KDE
|
2010-07-13 03:07:30 +02:00
|
|
|
#include <kdebug.h>
|
2010-10-01 18:58:15 +02:00
|
|
|
#include <kstandarddirs.h>
|
|
|
|
|
2010-10-23 14:04:24 +02:00
|
|
|
//Plasma
|
|
|
|
#include "applet.h"
|
|
|
|
#include "dataengine.h"
|
|
|
|
#include "abstractrunner.h"
|
|
|
|
|
2010-10-01 18:58:15 +02:00
|
|
|
static uint connectionId = 0;
|
2010-07-13 03:07:30 +02:00
|
|
|
|
|
|
|
//Storage Job implentation
|
|
|
|
StorageJob::StorageJob(const QString& destination,
|
|
|
|
const QString& operation,
|
|
|
|
const QMap<QString, QVariant>& parameters,
|
|
|
|
QObject *parent)
|
2010-10-23 14:04:24 +02:00
|
|
|
: ServiceJob(destination, operation, parameters, parent),
|
|
|
|
m_clientName(destination)
|
2010-07-13 03:07:30 +02:00
|
|
|
{
|
2010-10-01 18:58:15 +02:00
|
|
|
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg(++connectionId));
|
|
|
|
m_db.setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage.db"));
|
|
|
|
|
|
|
|
if (!m_db.open()) {
|
|
|
|
kWarning() << "Unable to open the plasma storage cache database: " << m_db.lastError();
|
2010-10-23 14:04:24 +02:00
|
|
|
} else if (!m_db.tables().contains(m_clientName)) {
|
2010-10-01 18:58:15 +02:00
|
|
|
QSqlQuery query(m_db);
|
2010-10-23 14:04:24 +02:00
|
|
|
//bindValue doesn't seem to be able to replace stuff in create table
|
2010-11-02 23:05:48 +01:00
|
|
|
query.prepare(QString("create table ")+m_clientName+" (valueGroup varchar(256), id varchar(256), data clob, creationTime datetime, accessTime datetime, primary key (valueGroup, id))");
|
2010-10-23 14:04:24 +02:00
|
|
|
query.exec();
|
2010-10-01 18:58:15 +02:00
|
|
|
}
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageJob::start()
|
|
|
|
{
|
2010-10-01 18:58:15 +02:00
|
|
|
if (!m_db.isOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
QMap<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";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
if (operationName() == "save") {
|
2010-10-01 18:58:15 +02:00
|
|
|
QSqlQuery query(m_db);
|
2010-10-23 14:06:21 +02:00
|
|
|
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and id = :id");
|
2010-10-23 14:14:33 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2010-10-01 20:02:13 +02:00
|
|
|
query.bindValue(":id", params["key"].toString());
|
|
|
|
query.exec();
|
|
|
|
|
2010-11-02 23:05:48 +01:00
|
|
|
query.prepare("insert into "+m_clientName+" values(:valueGroup, :id, :datavalue, date('now'), date('now'))");
|
2010-10-01 18:58:15 +02:00
|
|
|
query.bindValue(":id", params["key"].toString());
|
2010-10-23 14:14:33 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2010-10-01 18:58:15 +02:00
|
|
|
query.bindValue(":datavalue", params["data"]);
|
|
|
|
const bool success = query.exec();
|
|
|
|
setResult(success);
|
2010-07-29 01:28:31 +02:00
|
|
|
return;
|
2010-10-01 18:58:15 +02:00
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
} else if (operationName() == "retrieve") {
|
2010-10-01 18:58:15 +02:00
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
2010-10-22 23:22:15 +02:00
|
|
|
//a bit redundant but should be the faster way with less string concatenation as possible
|
2010-10-24 19:04:43 +02:00
|
|
|
if (params["key"].toString().isEmpty()) {
|
2010-11-02 23:05:48 +01:00
|
|
|
//update modification time
|
|
|
|
query.prepare("update "+m_clientName+" set accessTime=date('now') where valueGroup=:valueGroup");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.exec();
|
|
|
|
|
2010-10-23 14:04:24 +02:00
|
|
|
query.prepare("select * from "+m_clientName+" where valueGroup=:valueGroup");
|
2010-10-23 14:14:33 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2010-10-22 23:22:15 +02:00
|
|
|
} else {
|
2010-11-02 23:05:48 +01:00
|
|
|
//update modification time
|
|
|
|
query.prepare("update "+m_clientName+" set accessTime=date('now') where valueGroup=:valueGroup and id=:key");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
query.exec();
|
|
|
|
|
2010-10-24 19:04:43 +02:00
|
|
|
query.prepare("select * from "+m_clientName+" where valueGroup=:valueGroup and id=:key");
|
2010-10-23 14:14:33 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2010-10-22 23:22:15 +02:00
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
}
|
|
|
|
|
2010-10-01 18:58:15 +02:00
|
|
|
const bool success = query.exec();
|
|
|
|
|
2010-07-29 01:28:31 +02:00
|
|
|
QHash<QString, QVariant> h;
|
2010-10-01 18:58:15 +02:00
|
|
|
if (success) {
|
|
|
|
QSqlRecord rec = query.record();
|
|
|
|
const int keyColumn = rec.indexOf("id");
|
|
|
|
const int dataColumn = rec.indexOf("data");
|
|
|
|
|
|
|
|
while (query.next()) {
|
|
|
|
h[query.value(keyColumn).toString()] = query.value(dataColumn).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
setResult(h);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return;
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
2010-10-01 18:58:15 +02:00
|
|
|
|
2010-11-02 23:05:48 +01:00
|
|
|
} else if (operationName() == "delete") {
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
|
|
|
if (params["key"].toString().isEmpty()) {
|
|
|
|
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
} else {
|
|
|
|
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and id=:key");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = query.exec();
|
|
|
|
setResult(success);
|
|
|
|
|
2010-10-22 23:22:15 +02:00
|
|
|
} else if (operationName() == "expire") {
|
|
|
|
QSqlQuery query(m_db);
|
2010-11-02 23:05:48 +01:00
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
query.prepare("delete from "+m_clientName+" where accessTime < :date");
|
|
|
|
QDateTime time(QDateTime::currentDateTime());
|
|
|
|
time.addSecs(-params["age"].toUInt());
|
|
|
|
query.bindValue(":date", time.toTime_t());
|
|
|
|
} else {
|
|
|
|
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and accessTime < :date");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
QDateTime time(QDateTime::currentDateTime());
|
|
|
|
time.addSecs(-params["age"].toUInt());
|
|
|
|
query.bindValue(":date", time.toTime_t());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = query.exec();
|
|
|
|
setResult(success);
|
2010-10-22 23:22:15 +02:00
|
|
|
|
2010-10-01 18:58:15 +02:00
|
|
|
} else {
|
|
|
|
setError(true);
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::ServiceJob* Storage::createJob(const QString &operation, QMap<QString, QVariant> ¶meters)
|
|
|
|
{
|
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
|
2010-10-23 14:04:24 +02:00
|
|
|
Storage::Storage(QObject* parent) : Plasma::Service(parent)
|
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;
|
|
|
|
|
|
|
|
QString clientName("data");
|
|
|
|
while ((parentObject = parentObject->parent())) {
|
|
|
|
Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(parentObject);
|
|
|
|
if (applet) {
|
|
|
|
m_clientName = applet->pluginName();
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "storage_p.moc"
|