PlasmaActivitiesStats

ResourcesDatabaseSchema.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic(at)kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "ResourcesDatabaseSchema.h"
8
9#include <QCoreApplication>
10#include <QStandardPaths>
11#include <QVariant>
12
13namespace Common
14{
15namespace ResourcesDatabaseSchema
16{
17const QString name = QStringLiteral("Resources");
18
19QLatin1String version()
20{
21 return QLatin1String("2015.02.09");
22}
23
24QStringList schema()
25{
26 return QStringList{
27
28 // Schema information table, used for versioning
29 QStringLiteral("CREATE TABLE IF NOT EXISTS SchemaInfo ("
30 "key text PRIMARY KEY, value text"
31 ")"),
32
33 QStringLiteral("INSERT OR IGNORE INTO schemaInfo VALUES ('version', '%1')").arg(version()),
34 QStringLiteral("UPDATE schemaInfo SET value = '%1' WHERE key = 'version'").arg(version()),
35
36 // The ResourceEvent table saves the Opened/Closed event pairs for
37 // a resource. The Accessed event is mapped to those.
38 // Focussing events are not stored in order not to get a
39 // huge database file and to lessen writes to the disk.
40 QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceEvent ("
41 "usedActivity TEXT, "
42 "initiatingAgent TEXT, "
43 "targettedResource TEXT, "
44 "start INTEGER, "
45 "end INTEGER "
46 ")"),
47
48 // The ResourceScoreCache table stores the calculated scores
49 // for resources based on the recorded events.
50 QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceScoreCache ("
51 "usedActivity TEXT, "
52 "initiatingAgent TEXT, "
53 "targettedResource TEXT, "
54 "scoreType INTEGER, "
55 "cachedScore FLOAT, "
56 "firstUpdate INTEGER, "
57 "lastUpdate INTEGER, "
58 "PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
59 ")"),
60
61 // @since 2014.05.05
62 // The ResourceLink table stores the information, formerly kept by
63 // Nepomuk, of which resources are linked to which activities.
64 // The additional features compared to the old days are
65 // the ability to limit the link to specific applications, and
66 // to create global links.
67 QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceLink ("
68 "usedActivity TEXT, "
69 "initiatingAgent TEXT, "
70 "targettedResource TEXT, "
71 "PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
72 ")"),
73
74 // @since 2015.01.18
75 // The ResourceInfo table stores the collected information about a
76 // resource that is not agent nor activity related like the
77 // title and the mime type.
78 // If these are automatically retrieved (works for files), the
79 // flag is set to true. This is done for the agents to be able to
80 // override these.
81 QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceInfo ("
82 "targettedResource TEXT, "
83 "title TEXT, "
84 "mimetype TEXT, "
85 "autoTitle INTEGER, "
86 "autoMimetype INTEGER, "
87 "PRIMARY KEY(targettedResource)"
88 ")")}
89
90 ;
91}
92
93// TODO: This will require some refactoring after we introduce more databases
94QString defaultPath()
95{
96 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kactivitymanagerd/resources/database");
97}
98
99const char *overrideFlagProperty = "org.kde.KActivities.ResourcesDatabase.overrideDatabase";
100const char *overrideFileProperty = "org.kde.KActivities.ResourcesDatabase.overrideDatabaseFile";
101
102QString path()
103{
104 auto app = QCoreApplication::instance();
105
106 return app->property(overrideFlagProperty).toBool() ? app->property(overrideFileProperty).toString() : defaultPath();
107}
108
109void overridePath(const QString &path)
110{
111 auto app = QCoreApplication::instance();
112
113 app->setProperty(overrideFlagProperty, true);
114 app->setProperty(overrideFileProperty, path);
115}
116
117void initSchema(Database &database)
118{
119 QString dbSchemaVersion;
120
121 auto query = database.execQuery( //
122 QStringLiteral("SELECT value FROM SchemaInfo WHERE key = 'version'"));
123
124 if (query.next()) {
125 dbSchemaVersion = query.value(0).toString();
126 }
127
128 // Early bail-out if the schema is up-to-date
129 if (dbSchemaVersion == version()) {
130 return;
131 }
132
133 // Transition to KF5:
134 // We left the world of Nepomuk, and all the ontologies went
135 // across the sea to the Undying Lands.
136 // This needs to be done before executing the schema() queries
137 // so that we do not create new (empty) tables and block these
138 // queries from being executed.
139 if (dbSchemaVersion < QStringLiteral("2014.04.14")) {
140 database.execQuery( //
141 QStringLiteral("ALTER TABLE nuao_DesktopEvent RENAME TO ResourceEvent"));
142 database.execQuery( //
143 QStringLiteral("ALTER TABLE kext_ResourceScoreCache RENAME TO ResourceScoreCache"));
144 }
145
146 database.execQueries(ResourcesDatabaseSchema::schema());
147
148 // We can not allow empty fields for activity and agent, they need to
149 // be at least magic values. These do not change the structure
150 // of the database, but the old data.
151 if (dbSchemaVersion < QStringLiteral("2015.02.09")) {
152 const QString updateActivity = QStringLiteral(
153 "SET usedActivity=':global' "
154 "WHERE usedActivity IS NULL OR usedActivity = ''");
155
156 const QString updateAgent = QStringLiteral(
157 "SET initiatingAgent=':global' "
158 "WHERE initiatingAgent IS NULL OR initiatingAgent = ''");
159
160 // When the activity field was empty, it meant the file was
161 // linked to all activities (aka :global)
162 database.execQuery(QStringLiteral("UPDATE ResourceLink ") + updateActivity);
163
164 // When the agent field was empty, it meant the file was not
165 // linked to a specified agent (aka :global)
166 database.execQuery(QStringLiteral("UPDATE ResourceLink ") + updateAgent);
167
168 // These were not supposed to be empty, but in the case they were,
169 // deal with them as well
170 database.execQuery(QStringLiteral("UPDATE ResourceEvent ") + updateActivity);
171 database.execQuery(QStringLiteral("UPDATE ResourceEvent ") + updateAgent);
172 database.execQuery(QStringLiteral("UPDATE ResourceScoreCache ") + updateActivity);
173 database.execQuery(QStringLiteral("UPDATE ResourceScoreCache ") + updateAgent);
174 }
175}
176
177} // namespace Common
178} // namespace ResourcesDatabaseSchema
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QCoreApplication * instance()
T value(qsizetype i) const const
QString writableLocation(StandardLocation type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:02 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.