Verified Commit 3845e5af authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Run clang-format

If you want git blame to ignore formatting revisions run:
git config blame.ignoreRevsFile .git-blame-ignore-revs
parent 3fa2fd9a
......@@ -6,47 +6,46 @@
#include "Database.h"
#include <utils/d_ptr_implementation.h>
#include <common/database/schema/ResourcesDatabaseSchema.h>
#include <utils/d_ptr_implementation.h>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlField>
#include <QSqlError>
#include <QSqlDriver>
#include <QSqlError>
#include <QSqlField>
#include <QThread>
#include <QDebug>
#include <mutex>
#include <map>
#include <mutex>
#include "DebugResources.h"
namespace Common {
namespace {
namespace Common
{
namespace
{
#ifdef QT_DEBUG
QString lastExecutedQuery;
QString lastExecutedQuery;
#endif
std::mutex databases_mutex;
std::mutex databases_mutex;
struct DatabaseInfo {
Qt::HANDLE thread;
Database::OpenMode openMode;
};
struct DatabaseInfo {
Qt::HANDLE thread;
Database::OpenMode openMode;
};
bool operator<(const DatabaseInfo &left, const DatabaseInfo &right)
{
return
left.thread < right.thread ? true :
left.thread > right.thread ? false :
left.openMode < right.openMode;
}
bool operator<(const DatabaseInfo &left, const DatabaseInfo &right)
{
return left.thread < right.thread ? true : left.thread > right.thread ? false : left.openMode < right.openMode;
}
std::map<DatabaseInfo, std::weak_ptr<Database>> databases;
std::map<DatabaseInfo, std::weak_ptr<Database>> databases;
}
class QSqlDatabaseWrapper {
class QSqlDatabaseWrapper
{
private:
QSqlDatabase m_database;
bool m_open;
......@@ -56,17 +55,14 @@ public:
QSqlDatabaseWrapper(const DatabaseInfo &info)
: m_open(false)
{
m_connectionName =
"kactivities_db_resources_"
// Adding the thread number to the database name
+ QString::number((quintptr)info.thread)
// And whether it is read-only or read-write
+ (info.openMode == Database::ReadOnly ? "_readonly" : "_readwrite");
m_database =
QSqlDatabase::contains(m_connectionName)
? QSqlDatabase::database(m_connectionName)
: QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connectionName);
m_connectionName = "kactivities_db_resources_"
// Adding the thread number to the database name
+ QString::number((quintptr)info.thread)
// And whether it is read-only or read-write
+ (info.openMode == Database::ReadOnly ? "_readonly" : "_readwrite");
m_database = QSqlDatabase::contains(m_connectionName) ? QSqlDatabase::database(m_connectionName)
: QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connectionName);
if (info.openMode == Database::ReadOnly) {
m_database.setConnectOptions(QStringLiteral("QSQLITE_OPEN_READONLY"));
......@@ -78,9 +74,7 @@ public:
m_open = m_database.open();
if (!m_open) {
qCWarning(KAMD_LOG_RESOURCES) << "KActivities: Database is not open: "
<< m_database.connectionName()
<< m_database.databaseName()
qCWarning(KAMD_LOG_RESOURCES) << "KActivities: Database is not open: " << m_database.connectionName() << m_database.databaseName()
<< m_database.lastError();
if (info.openMode == Database::ReadWrite) {
......@@ -115,7 +109,8 @@ public:
}
};
class Database::Private {
class Database::Private
{
public:
Private()
{
......@@ -153,7 +148,7 @@ Database::Ptr Database::instance(Source source, OpenMode openMode)
// We are saving instances per thread and per read/write mode
DatabaseInfo info;
info.thread = QThread::currentThreadId();
info.thread = QThread::currentThreadId();
info.openMode = openMode;
// Do we have an instance matching the request?
......@@ -207,11 +202,10 @@ Database::Ptr Database::instance(Source source, OpenMode openMode)
ptr->setPragma(QStringLiteral("wal_autocheckpoint = 100"));
qCDebug(KAMD_LOG_RESOURCES) << "KActivities: Database connection: " << ptr->d->database->connectionName()
<< "\n query_only: " << ptr->pragma(QStringLiteral("query_only"))
<< "\n journal_mode: " << ptr->pragma(QStringLiteral("journal_mode"))
<< "\n wal_autocheckpoint: " << ptr->pragma(QStringLiteral("wal_autocheckpoint"))
<< "\n synchronous: " << ptr->pragma(QStringLiteral("synchronous"))
;
<< "\n query_only: " << ptr->pragma(QStringLiteral("query_only"))
<< "\n journal_mode: " << ptr->pragma(QStringLiteral("journal_mode"))
<< "\n wal_autocheckpoint: " << ptr->pragma(QStringLiteral("wal_autocheckpoint"))
<< "\n synchronous: " << ptr->pragma(QStringLiteral("synchronous"));
return ptr;
}
......@@ -260,8 +254,7 @@ QSqlQuery Database::execQuery(const QString &query, bool ignoreErrors) const
if (!ignoreErrors && result.lastError().isValid()) {
qCWarning(KAMD_LOG_RESOURCES) << "SQL: "
<< "\n error: " << result.lastError()
<< "\n query: " << query;
<< "\n error: " << result.lastError() << "\n query: " << query;
}
return result;
......@@ -272,7 +265,7 @@ QSqlQuery Database::execQueries(const QStringList &queries) const
{
QSqlQuery result;
for (const auto &query: queries) {
for (const auto &query : queries) {
result = execQuery(query);
}
......
......@@ -7,15 +7,16 @@
#ifndef COMMON_DATABASE_H
#define COMMON_DATABASE_H
#include <utils/d_ptr.h>
#include <memory>
#include <QSqlQuery>
#include <QRegExp>
#include <QObject>
#include <QRegExp>
#include <QSqlQuery>
#include <memory>
#include <utils/d_ptr.h>
namespace Common {
class Database: public QObject {
namespace Common
{
class Database : public QObject
{
Q_OBJECT
public:
......@@ -47,7 +48,8 @@ public:
Database();
friend class Locker;
class Locker {
class Locker
{
public:
explicit Locker(Database &database);
~Locker();
......@@ -58,9 +60,9 @@ public:
void reportError(const QSqlError &error);
#define DATABASE_TRANSACTION(A) \
/* enable this for debugging only: qCDebug(KAMD_LOG_RESOURCES) << "Location:" << __FILE__ << __LINE__; */ \
Common::Database::Locker lock(A)
#define DATABASE_TRANSACTION(A) \
/* enable this for debugging only: qCDebug(KAMD_LOG_RESOURCES) << "Location:" << __FILE__ << __LINE__; */ \
Common::Database::Locker lock(A)
Q_SIGNALS:
void error(const QSqlError &error) const;
......@@ -69,24 +71,20 @@ private:
D_PTR;
};
template <typename EscapeFunction>
QString parseStarPattern(const QString &pattern, const QString &joker,
EscapeFunction escape)
template<typename EscapeFunction>
QString parseStarPattern(const QString &pattern, const QString &joker, EscapeFunction escape)
{
const auto begin = pattern.constBegin();
const auto end = pattern.constEnd();
const auto begin = pattern.constBegin();
const auto end = pattern.constEnd();
auto currentStart = pattern.constBegin();
auto currentStart = pattern.constBegin();
auto currentPosition = pattern.constBegin();
bool isEscaped = false;
// This should be available in the QString class...
auto stringFromIterators = [&](const QString::const_iterator &currentStart,
const QString::const_iterator &currentPosition) {
return pattern.mid(
std::distance(begin, currentStart),
std::distance(currentStart, currentPosition));
auto stringFromIterators = [&](const QString::const_iterator &currentStart, const QString::const_iterator &currentPosition) {
return pattern.mid(std::distance(begin, currentStart), std::distance(currentStart, currentPosition));
};
// Escaping % and _ for sql like
......@@ -108,8 +106,7 @@ QString parseStarPattern(const QString &pattern, const QString &joker,
} else if (*currentPosition == QLatin1Char('*')) {
// Replacing the star with the sql like joker - %
resultPattern.append(escape(stringFromIterators(
currentStart, currentPosition)) + joker);
resultPattern.append(escape(stringFromIterators(currentStart, currentPosition)) + joker);
currentStart = currentPosition + 1;
} else {
......@@ -118,8 +115,7 @@ QString parseStarPattern(const QString &pattern, const QString &joker,
}
if (currentStart != currentPosition) {
resultPattern.append(escape(stringFromIterators(
currentStart, currentPosition)));
resultPattern.append(escape(stringFromIterators(currentStart, currentPosition)));
}
return resultPattern;
......@@ -143,4 +139,3 @@ inline QRegExp starPatternToRegex(const QString &pattern)
} // namespace Common
#endif // COMMON_DATABASE_H
......@@ -6,13 +6,14 @@
#include "ResourcesDatabaseSchema.h"
#include <QCoreApplication>
#include <QStandardPaths>
#include <QVariant>
#include <QCoreApplication>
namespace Common {
namespace ResourcesDatabaseSchema {
namespace Common
{
namespace ResourcesDatabaseSchema
{
const QString name = QStringLiteral("Resources");
QString version()
......@@ -27,29 +28,31 @@ QStringList schema()
return QStringList()
<< // Schema information table, used for versioning
QStringLiteral("CREATE TABLE IF NOT EXISTS SchemaInfo ("
QStringLiteral(
"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())
<< // The ResourceEvent table saves the Opened/Closed event pairs for
// a resource. The Accessed event is mapped to those.
// Focusing events are not stored in order not to get a
// huge database file and to lessen writes to the disk.
QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceEvent ("
QStringLiteral(
"CREATE TABLE IF NOT EXISTS ResourceEvent ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
"start INTEGER, "
"end INTEGER "
")")
")")
<< // The ResourceScoreCache table stores the calculated scores
// for resources based on the recorded events.
QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceScoreCache ("
QStringLiteral(
"CREATE TABLE IF NOT EXISTS ResourceScoreCache ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
......@@ -58,8 +61,7 @@ QStringList schema()
"firstUpdate INTEGER, "
"lastUpdate INTEGER, "
"PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
")")
")")
<< // @since 2014.05.05
// The ResourceLink table stores the information, formerly kept by
......@@ -67,12 +69,13 @@ QStringList schema()
// The additional features compared to the old days are
// the ability to limit the link to specific applications, and
// to create global links.
QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceLink ("
QStringLiteral(
"CREATE TABLE IF NOT EXISTS ResourceLink ("
"usedActivity TEXT, "
"initiatingAgent TEXT, "
"targettedResource TEXT, "
"PRIMARY KEY(usedActivity, initiatingAgent, targettedResource)"
")")
")")
<< // @since 2015.01.18
// The ResourceInfo table stores the collected information about a
......@@ -81,24 +84,23 @@ QStringList schema()
// If these are automatically retrieved (works for files), the
// flag is set to true. This is done for the agents to be able to
// override these.
QStringLiteral("CREATE TABLE IF NOT EXISTS ResourceInfo ("
QStringLiteral(
"CREATE TABLE IF NOT EXISTS ResourceInfo ("
"targettedResource TEXT, "
"title TEXT, "
"mimetype TEXT, "
"autoTitle INTEGER, "
"autoMimetype INTEGER, "
"PRIMARY KEY(targettedResource)"
")")
")")
;
;
}
// TODO: This will require some refactoring after we introduce more databases
QString defaultPath()
{
return QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation)
+ QStringLiteral("/kactivitymanagerd/resources/database");
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kactivitymanagerd/resources/database");
}
const char *overrideFlagProperty = "org.kde.KActivities.ResourcesDatabase.overrideDatabase";
......@@ -108,10 +110,7 @@ QString path()
{
auto app = QCoreApplication::instance();
return
(app->property(overrideFlagProperty).toBool()) ?
app->property(overrideFileProperty).toString() :
defaultPath();
return (app->property(overrideFlagProperty).toBool()) ? app->property(overrideFileProperty).toString() : defaultPath();
}
void overridePath(const QString &path)
......@@ -126,9 +125,8 @@ void initSchema(Database &database)
{
QString dbSchemaVersion;
auto query = database.execQuery(
QStringLiteral("SELECT value FROM SchemaInfo WHERE key = 'version'"),
/* ignore error */ true);
auto query = database.execQuery(QStringLiteral("SELECT value FROM SchemaInfo WHERE key = 'version'"),
/* ignore error */ true);
if (query.next()) {
dbSchemaVersion = query.value(0).toString();
......@@ -146,12 +144,10 @@ void initSchema(Database &database)
// so that we do not create new (empty) tables and block these
// queries from being executed.
if (dbSchemaVersion < QStringLiteral("2014.04.14")) {
database.execQuery(
QStringLiteral("ALTER TABLE nuao_DesktopEvent RENAME TO ResourceEvent"),
/* ignore error */ true);
database.execQuery(
QStringLiteral("ALTER TABLE kext_ResourceScoreCache RENAME TO ResourceScoreCache"),
/* ignore error */ true);
database.execQuery(QStringLiteral("ALTER TABLE nuao_DesktopEvent RENAME TO ResourceEvent"),
/* ignore error */ true);
database.execQuery(QStringLiteral("ALTER TABLE kext_ResourceScoreCache RENAME TO ResourceScoreCache"),
/* ignore error */ true);
}
database.execQueries(ResourcesDatabaseSchema::schema());
......@@ -160,24 +156,24 @@ void initSchema(Database &database)
// some of these should fail.
// WARNING: Sqlite specific!
database.execQueries(QStringList{
".tables",
"SELECT count(*) FROM SchemaInfo",
"SELECT count(*) FROM ResourceEvent",
"SELECT count(*) FROM ResourceScoreCache",
"SELECT count(*) FROM ResourceLink",
"SELECT count(*) FROM ResourceInfo",
});
".tables",
"SELECT count(*) FROM SchemaInfo",
"SELECT count(*) FROM ResourceEvent",
"SELECT count(*) FROM ResourceScoreCache",
"SELECT count(*) FROM ResourceLink",
"SELECT count(*) FROM ResourceInfo",
});
// We can not allow empty fields for activity and agent, they need to
// be at least magic values. These do not change the structure
// of the database, but the old data.
if (dbSchemaVersion < QStringLiteral("2015.02.09")) {
const QString updateActivity =
QStringLiteral("SET usedActivity=':global' "
const QString updateActivity = QStringLiteral(
"SET usedActivity=':global' "
"WHERE usedActivity IS NULL OR usedActivity = ''");
const QString updateAgent =
QStringLiteral("SET initiatingAgent=':global' "
const QString updateAgent = QStringLiteral(
"SET initiatingAgent=':global' "
"WHERE initiatingAgent IS NULL OR initiatingAgent = ''");
// When the activity field was empty, it meant the file was
......@@ -194,10 +190,8 @@ void initSchema(Database &database)
database.execQuery("UPDATE ResourceEvent " + updateAgent);
database.execQuery("UPDATE ResourceScoreCache " + updateActivity);
database.execQuery("UPDATE ResourceScoreCache " + updateAgent);
}
}
} // namespace Common
} // namespace ResourcesDatabaseSchema
......@@ -7,23 +7,23 @@
#ifndef RESOURCESDATABASESCHEMA_H
#define RESOURCESDATABASESCHEMA_H
#include <QStringList>
#include "../Database.h"
#include <QStringList>
namespace Common {
namespace ResourcesDatabaseSchema {
QString version();
namespace Common
{
namespace ResourcesDatabaseSchema
{
QString version();
QStringList schema();
QStringList schema();
QString path();
void overridePath(const QString &path);
QString path();
void overridePath(const QString &path);
void initSchema(Database &database);
void initSchema(Database &database);
} // namespace ResourcesDatabase
} // namespace Common
#endif // RESOURCESDATABASESCHEMA_H
......@@ -6,12 +6,13 @@
#include "org.kde.ActivityManager.Activities.h"
#include <QMetaType>
#include <QDBusMetaType>
#include <QMetaType>
namespace details {
class ActivityInfoStaticInit {
namespace details
{
class ActivityInfoStaticInit
{
public:
ActivityInfoStaticInit()
{
......
......@@ -7,10 +7,10 @@
#ifndef KAMD_ACTIVITIES_DBUS_H
#define KAMD_ACTIVITIES_DBUS_H
#include <QString>
#include <QList>
#include <QDBusArgument>
#include <QDebug>
#include <QList>
#include <QString>
struct ActivityInfo {
QString id;
......@@ -38,7 +38,7 @@ typedef QList<ActivityInfo> ActivityInfoList;
Q_DECLARE_METATYPE(ActivityInfo)
Q_DECLARE_METATYPE(ActivityInfoList)
QDBusArgument &operator<<(QDBusArgument &arg, const ActivityInfo&);
QDBusArgument &operator<<(QDBusArgument &arg, const ActivityInfo &);
const QDBusArgument &operator>>(const QDBusArgument &arg, ActivityInfo &rec);
QDebug operator<<(QDebug dbg, const ActivityInfo &r);
......
......@@ -9,16 +9,14 @@
#include <QString>
#define GLOBAL_ACTIVITY_TAG QStringLiteral(":global")
#define ANY_ACTIVITY_TAG QStringLiteral(":any")
#define GLOBAL_ACTIVITY_TAG QStringLiteral(":global")
#define ANY_ACTIVITY_TAG QStringLiteral(":any")
#define CURRENT_ACTIVITY_TAG QStringLiteral(":current")
#define GLOBAL_AGENT_TAG QStringLiteral(":global")
#define ANY_AGENT_TAG QStringLiteral(":any")
#define CURRENT_AGENT_TAG QStringLiteral(":current")
#define ANY_TYPE_TAG QStringLiteral(":any")
#define GLOBAL_AGENT_TAG QStringLiteral(":global")
#define ANY_AGENT_TAG QStringLiteral(":any")
#define CURRENT_AGENT_TAG QStringLiteral(":current")
#define ANY_TYPE_TAG QStringLiteral(":any")
#endif // SPECIALVALUES_H
......@@ -5,22 +5,22 @@
*/
// Self
#include <kactivities-features.h>
#include "Activities.h"
#include "Activities_p.h"
#include <kactivities-features.h>
// Qt
#include <QtGlobal>
#include <QDBusConnection>
#include <QFile>
#include <QMetaObject>
#include <QStandardPaths>
#include <QFile>
#include <QUuid>
#include <QtGlobal>
// KDE
#include <klocalizedstring.h>
#include <kauthorized.h>
#include <kdelibs4migration.h>
#include <klocalizedstring.h>
#include <ksharedconfig.h>
// Utils
......@@ -30,29 +30,26 @@
// Local
#include "DebugActivities.h"
#include "activitiesadaptor.h"
#include "ksmserver/KSMServer.h"
#include "common/dbus/common.h"
#include "ksmserver/KSMServer.h"
// Private
#define ACTIVITY_MANAGER_CONFIG_FILE_NAME QStringLiteral("kactivitymanagerdrc")
namespace {
inline
bool nameBasedOrdering(const ActivityInfo &info, const ActivityInfo &other)
{
const auto comp =
QString::compare(info.name, other.name, Qt::CaseInsensitive);
return comp < 0 || (comp == 0 && info.id < other.id);
}
namespace
{
inline bool nameBasedOrdering(const ActivityInfo &info, const ActivityInfo &other)
{
const auto comp = QString::compare(info.name, other.name, Qt::CaseInsensitive);
return comp < 0 || (comp == 0 && info.id < other.id);
}
}
Activities::Private::KDE4ConfigurationTransitionChecker::KDE4ConfigurationTransitionChecker()
{
// Checking whether we need to transfer the KActivities/KDE4
// configuration file to the new location.
const QString newConfigLocation
= QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QLatin1Char('/') + ACTIVITY_MANAGER_CONFIG_FILE_NAME;
const QString newConfigLocation = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + ACTIVITY_MANAGER_CONFIG_FILE_NAME;