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>
|
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"
|
|
|
|
|
2011-01-07 11:51:29 +01:00
|
|
|
|
|
|
|
class RefCountedDatabase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void ref()
|
|
|
|
{
|
|
|
|
if (m_ref == 0) {
|
2011-01-14 12:23:22 +01:00
|
|
|
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
|
2011-04-28 16:30:44 +02:00
|
|
|
m_db.setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage2.db"));
|
2011-01-07 11:51:29 +01:00
|
|
|
}
|
|
|
|
//Q_ASSERT(db.isValid());
|
|
|
|
m_ref.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool deref()
|
|
|
|
{
|
|
|
|
//kill the database if the last one in use
|
|
|
|
bool last = !m_ref.deref();
|
|
|
|
if (last) {
|
|
|
|
m_db.close();
|
|
|
|
QString name = m_db.connectionName();
|
|
|
|
m_db = QSqlDatabase();
|
|
|
|
QSqlDatabase::removeDatabase(name);
|
|
|
|
}
|
|
|
|
return !last;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QSqlDatabase *database()
|
|
|
|
{
|
|
|
|
return &m_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QSqlDatabase m_db;
|
|
|
|
QAtomicInt m_ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
static QThreadStorage<RefCountedDatabase *> s_databasePool;
|
|
|
|
|
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)
|
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-01-07 11:51:29 +01:00
|
|
|
m_rdb = s_databasePool.localData();
|
|
|
|
if (m_rdb == 0) {
|
|
|
|
s_databasePool.setLocalData(new RefCountedDatabase);
|
|
|
|
m_rdb = s_databasePool.localData();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rdb->ref();
|
2010-10-01 18:58:15 +02:00
|
|
|
|
2011-01-07 11:51:29 +01:00
|
|
|
if (!m_rdb->database()->open()) {
|
|
|
|
kWarning() << "Unable to open the plasma storage cache database: " << m_rdb->database()->lastError();
|
|
|
|
} else if (!m_rdb->database()->tables().contains(m_clientName)) {
|
|
|
|
QSqlQuery query(*m_rdb->database());
|
2011-04-28 16:30:44 +02:00
|
|
|
query.prepare(QString("create table ") + m_clientName + " (valueGroup varchar(256), id varchar(256), txt TEXT, int INTEGER, float REAL, binary BLOB, creationTime datetime, accessTime datetime, primary key (valueGroup, id))");
|
|
|
|
if (!query.exec()) {
|
|
|
|
kWarning() << "Unable to create table for" << m_clientName;
|
|
|
|
m_rdb->database()->close();
|
|
|
|
}
|
2010-10-01 18:58:15 +02:00
|
|
|
}
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
|
2011-01-07 00:26:14 +01:00
|
|
|
StorageJob::~StorageJob()
|
|
|
|
{
|
2011-01-07 11:51:29 +01:00
|
|
|
if (!m_rdb->deref()) {
|
|
|
|
s_databasePool.setLocalData(0);
|
|
|
|
}
|
2011-01-07 00:26:14 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
void StorageJob::start()
|
|
|
|
{
|
2011-01-07 11:51:29 +01:00
|
|
|
if (!m_rdb->database()->isOpen()) {
|
2010-10-01 18:58:15 +02:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:01:34 +02:00
|
|
|
//kDebug() << operationName();
|
|
|
|
m_rdb->database()->transaction();
|
|
|
|
|
2010-07-13 03:07:30 +02:00
|
|
|
if (operationName() == "save") {
|
2011-04-28 14:14:11 +02:00
|
|
|
QSqlQuery query(*m_rdb->database());
|
2011-04-28 17:28:03 +02:00
|
|
|
if (params.value("key").toString().isNull()) {
|
|
|
|
m_data.insert(params.value("key").toString(), params.value("data"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QHashIterator<QString, QVariant> it(m_data);
|
|
|
|
|
|
|
|
QString ids;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
QSqlField field(":id", QVariant::String);
|
|
|
|
field.setValue(it.key());
|
|
|
|
if (!ids.isEmpty()) {
|
|
|
|
ids.append(", ");
|
|
|
|
}
|
|
|
|
ids.append(m_rdb->database()->driver()->formatValue(field));
|
|
|
|
}
|
|
|
|
|
|
|
|
query.prepare("delete from " + m_clientName + " where valueGroup = :valueGroup and id in (" + ids + ");");
|
2011-04-28 14:14:11 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2011-04-28 17:28:03 +02:00
|
|
|
|
|
|
|
if (!query.exec()) {
|
2011-04-28 18:02:23 +02:00
|
|
|
m_rdb->database()->commit();
|
2011-04-28 17:28:03 +02:00
|
|
|
setResult(false);
|
|
|
|
return;
|
|
|
|
}
|
2011-04-28 14:14:11 +02:00
|
|
|
|
2011-04-28 16:52:19 +02:00
|
|
|
query.prepare("insert into " + m_clientName + " values(:valueGroup, :id, :txt, :int, :float, :binary, date('now'), date('now'))");
|
2011-04-28 14:14:11 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
2011-04-28 16:52:19 +02:00
|
|
|
query.bindValue(":txt", QVariant());
|
|
|
|
query.bindValue(":int", QVariant());
|
|
|
|
query.bindValue(":float", QVariant());
|
|
|
|
query.bindValue(":binary", QVariant());
|
2011-04-28 14:14:11 +02:00
|
|
|
|
|
|
|
const QString key = params.value("key").toString();
|
|
|
|
if (!key.isEmpty()) {
|
|
|
|
m_data.insert(key, params["data"]);
|
|
|
|
}
|
|
|
|
|
2011-04-28 17:28:03 +02:00
|
|
|
it.toFront();
|
2011-04-28 14:14:11 +02:00
|
|
|
while (it.hasNext()) {
|
2011-04-28 16:01:15 +02:00
|
|
|
it.next();
|
2011-04-28 17:28:03 +02:00
|
|
|
//kDebug() << "going to insert" << valueGroup << it.key();
|
2011-04-28 14:14:11 +02:00
|
|
|
query.bindValue(":id", it.key());
|
2011-04-28 16:52:19 +02:00
|
|
|
|
|
|
|
QString field;
|
|
|
|
switch (QMetaType::Type(it.value().type())) {
|
|
|
|
case QVariant::String:
|
|
|
|
field = ":txt";
|
|
|
|
break;
|
|
|
|
case QVariant::Int:
|
|
|
|
field = ":int";
|
|
|
|
break;
|
|
|
|
case QVariant::Double:
|
|
|
|
case QMetaType::Float:
|
|
|
|
field = ":float";
|
|
|
|
break;
|
|
|
|
case QVariant::ByteArray:
|
|
|
|
field = ":binary";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.bindValue(field, it.value());
|
|
|
|
|
2011-04-28 14:14:11 +02:00
|
|
|
if (!query.exec()) {
|
2011-04-28 18:01:34 +02:00
|
|
|
//kDebug() << "query failed:" << query.lastQuery() << query.lastError().text();
|
2011-04-28 18:02:23 +02:00
|
|
|
m_rdb->database()->commit();
|
2011-04-28 14:14:11 +02:00
|
|
|
setResult(false);
|
|
|
|
return;
|
|
|
|
}
|
2011-04-28 16:52:19 +02:00
|
|
|
|
|
|
|
query.bindValue(field, QVariant());
|
2011-04-28 14:14:11 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 18:01:34 +02:00
|
|
|
setResult(true);
|
2010-07-13 03:07:30 +02:00
|
|
|
} else if (operationName() == "retrieve") {
|
2011-01-07 11:51:29 +01:00
|
|
|
QSqlQuery query(*m_rdb->database());
|
2010-10-01 18:58:15 +02:00
|
|
|
|
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
|
2011-04-28 16:52:19 +02:00
|
|
|
query.prepare("update " + m_clientName + " set accessTime=date('now') where valueGroup=:valueGroup");
|
2010-11-02 23:05:48 +01:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.exec();
|
|
|
|
|
2011-04-28 16:52:19 +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
|
2011-04-28 17:28:03 +02:00
|
|
|
query.prepare("update " + m_clientName + " set accessTime=date('now') where valueGroup=:valueGroup and id=:key");
|
2010-11-02 23:05:48 +01:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
query.exec();
|
|
|
|
|
2011-04-28 17:28:03 +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();
|
|
|
|
|
2011-04-28 17:28:03 +02:00
|
|
|
m_data.clear();
|
2010-10-01 18:58:15 +02:00
|
|
|
if (success) {
|
|
|
|
QSqlRecord rec = query.record();
|
|
|
|
const int keyColumn = rec.indexOf("id");
|
2011-04-28 17:28:03 +02:00
|
|
|
const int textColumn = rec.indexOf("txt");
|
|
|
|
const int intColumn = rec.indexOf("int");
|
|
|
|
const int floatColumn = rec.indexOf("float");
|
|
|
|
const int binaryColumn = rec.indexOf("binary");
|
2010-10-01 18:58:15 +02:00
|
|
|
|
|
|
|
while (query.next()) {
|
2011-04-28 17:28:03 +02:00
|
|
|
const QString key = query.value(keyColumn).toString();
|
|
|
|
if (!query.value(textColumn).isNull()) {
|
|
|
|
m_data.insert(key, query.value(textColumn));
|
|
|
|
} else if (!query.value(intColumn).isNull()) {
|
|
|
|
m_data.insert(key, query.value(intColumn));
|
|
|
|
} else if (!query.value(floatColumn).isNull()) {
|
|
|
|
m_data.insert(key, query.value(floatColumn));
|
|
|
|
} else if (!query.value(binaryColumn).isNull()) {
|
|
|
|
m_data.insert(key, query.value(binaryColumn));
|
|
|
|
}
|
2010-10-01 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 17:28:03 +02:00
|
|
|
setResult(m_data);
|
2011-04-28 18:09:48 +02:00
|
|
|
} else {
|
|
|
|
setResult(false);
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
2010-11-02 23:05:48 +01:00
|
|
|
} else if (operationName() == "delete") {
|
2011-01-07 11:51:29 +01:00
|
|
|
QSqlQuery query(*m_rdb->database());
|
2010-11-02 23:05:48 +01:00
|
|
|
|
|
|
|
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") {
|
2011-01-07 11:51:29 +01:00
|
|
|
QSqlQuery query(*m_rdb->database());
|
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
|
|
|
}
|
2011-01-07 11:51:29 +01:00
|
|
|
m_rdb->database()->commit();
|
2010-07-13 03:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::ServiceJob* Storage::createJob(const QString &operation, QMap<QString, QVariant> ¶meters)
|
|
|
|
{
|
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) {
|
|
|
|
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"
|