* Copyright (C) 2015 Ivan Cukic <ivan.cukic(at)kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* or (at your option) any later version, as published by the Free
* Software Foundation
*
* This program 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 General Public License for more details
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ResourcesDatabaseSchema.h"
#include <QStandardPaths>
#include <QVariant>
#include <QCoreApplication>
namespace Common {
namespace ResourcesDatabaseSchema {
const QString name = QStringLiteral("Resources");
QString version()
{
return "2015.02.09";
}
QStringList schema()
{
return QStringList()
<<
"CREATE TABLE IF NOT EXISTS SchemaInfo ("
"key text PRIMARY KEY, value text"
")"
<< QStringLiteral("INSERT OR IGNORE INTO schemaInfo VALUES ('version', '%1')").arg(version())
<< QStringLiteral("UPDATE schemaInfo SET value = '%1' WHERE key = 'version'").arg(version())
<<
"CREATE TABLE IF NOT EXISTS ResourceEvent ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
"start INTEGER, "
"end INTEGER "
")"
<<
"CREATE TABLE IF NOT EXISTS ResourceScoreCache ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
"scoreType INTEGER, "
"cachedScore FLOAT, "
"firstUpdate INTEGER, "
"lastUpdate INTEGER, "
"PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
")"
<<
"CREATE TABLE IF NOT EXISTS ResourceLink ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
"PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
")"
<<
"CREATE TABLE IF NOT EXISTS ResourceInfo ("
"targettedResource TEXT, "
"title TEXT, "
"mimetype TEXT, "
"autoTitle INTEGER, "
"autoMimetype INTEGER, "
"PRIMARY KEY(targettedResource)"
")"
;
}
QString defaultPath()
{
return QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation)
+ QStringLiteral("/kactivitymanagerd/resources/database");
}
const char *overrideFlagProperty = "org.kde.KActivities.ResourcesDatabase.overrideDatabase";
const char *overrideFileProperty = "org.kde.KActivities.ResourcesDatabase.overrideDatabaseFile";
QString path()
{
auto app = QCoreApplication::instance();
return
(app->property(overrideFlagProperty).toBool()) ?
app->property(overrideFileProperty).toString() :
defaultPath();
}
void overridePath(const QString &path)
{
auto app = QCoreApplication::instance();
app->setProperty(overrideFlagProperty, true);
app->setProperty(overrideFileProperty, path);
}
void initSchema(Database &database)
{
QString dbSchemaVersion;
auto query = database.execQuery(
QStringLiteral("SELECT value FROM SchemaInfo WHERE key = 'version'"),
true);
if (query.next()) {
dbSchemaVersion = query.value(0).toString();
}
if (dbSchemaVersion == version()) {
return;
}
if (dbSchemaVersion < QStringLiteral("2014.04.14")) {
database.execQuery(
QStringLiteral("ALTER TABLE nuao_DesktopEvent RENAME TO ResourceEvent"),
true);
database.execQuery(
QStringLiteral("ALTER TABLE kext_ResourceScoreCache RENAME TO ResourceScoreCache"),
true);
}
database.execQueries(ResourcesDatabaseSchema::schema());
if (dbSchemaVersion < QStringLiteral("2015.02.09")) {
const QString updateActivity =
"SET usedActivity=':global' "
"WHERE usedActivity IS NULL OR usedActivity = ''";
const QString updateAgent =
"SET initiatingAgent=':global' "
"WHERE initiatingAgent IS NULL OR initiatingAgent = ''";
database.execQuery("UPDATE ResourceLink " + updateActivity);
database.execQuery("UPDATE ResourceLink " + updateAgent);
database.execQuery("UPDATE ResourceEvent " + updateActivity);
database.execQuery("UPDATE ResourceEvent " + updateAgent);
database.execQuery("UPDATE ResourceScoreCache " + updateActivity);
database.execQuery("UPDATE ResourceScoreCache " + updateAgent);
}
}
}
}