diff --git a/sources/queued/include/queued/Queued.h b/sources/queued/include/queued/Queued.h index 8098e94..d365872 100644 --- a/sources/queued/include/queued/Queued.h +++ b/sources/queued/include/queued/Queued.h @@ -24,11 +24,15 @@ #ifndef QUEUED_H #define QUEUED_H +#include "QueuedAdvancedSettings.h" +#include "QueuedConfiguration.h" #include "QueuedDatabase.h" #include "QueuedDebug.h" #include "QueuedEnums.h" #include "QueuedProcess.h" #include "QueuedProcessManager.h" +#include "QueuedSettings.h" +#include "QueuedTokenManager.h" #include "QueuedUser.h" #endif /* QUEUED_H */ diff --git a/sources/queued/include/queued/QueuedAdvancedSettings.h b/sources/queued/include/queued/QueuedAdvancedSettings.h new file mode 100644 index 0000000..e5b2e0e --- /dev/null +++ b/sources/queued/include/queued/QueuedAdvancedSettings.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedAdvancedSettings.h + * Header of Queued library + * @author Evgeniy Alekseev + * @copyright MIT + * @bug https://github.com/arcan1s/queued/issues + */ + + +#ifndef QUEUEDADVANCEDSETTINGS_H +#define QUEUEDADVANCEDSETTINGS_H + +#include + +#include "QueuedConfiguration.h" + + +/** + * @brief implementation over database stored settings + */ +class QueuedAdvancedSettings : public QObject +{ + Q_OBJECT + +public: + /** + * @brief QueuedAdvancedSettings class constructor + * @param parent pointer to parent item + */ + explicit QueuedAdvancedSettings(QObject *parent); + /** + * @brief QueuedAdvancedSettings class destructor + */ + virtual ~QueuedAdvancedSettings(); + /** + * @brief upload configuration from database in to internal format + * @param _value configuration values from database + */ + void setValues(const QList &_values); + +private: +}; + + +#endif /* QUEUEDADVANCEDSETTINGS_H */ diff --git a/sources/queued/include/queued/QueuedConfiguration.h b/sources/queued/include/queued/QueuedConfiguration.h new file mode 100644 index 0000000..87ea9c5 --- /dev/null +++ b/sources/queued/include/queued/QueuedConfiguration.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedCOnfiguration.h + * Header of Queued library + * @author Evgeniy Alekseev + * @copyright MIT + * @bug https://github.com/arcan1s/queued/issues + */ + + +#ifndef QUEUEDCONFIGURATION_H +#define QUEUEDCONFIGURATION_H + +#include + + +/** + * @defgroup QueuedCfg + * @brief Queued configuration related types + */ +namespace QueuedCfg +{ +/** + * @ingroup QueuedCfg + * @struct QueuedAdminSetup + * @brief structure to define administrator user + * @var name + * administrator user name + * @var password + * administrator user password + */ +typedef struct { + QString name; + QString password; +} QueuedAdminSetup; +/** + * m@ingroup QueuedCfg + * @struct QueuedDBSetup + * @brief structure to define database setup + * @var driver + * driver name + * @var hostname + * hostname to connect + * @var password + * password to connect if any + * @var path + * path to database + * @var port + * port to connect + * @var username + * username to connect if any + */ +typedef struct { + QString driver; + QString hostname; + QString password; + QString path; + int port; + QString username; +} QueuedDBSetup; +}; + + +#endif /* QUEUEDCONFIGURATION_H */ diff --git a/sources/queued/include/queued/QueuedDatabase.h b/sources/queued/include/queued/QueuedDatabase.h index 676b2a1..fa97063 100644 --- a/sources/queued/include/queued/QueuedDatabase.h +++ b/sources/queued/include/queued/QueuedDatabase.h @@ -54,9 +54,9 @@ public: * @brief add record to database * @param _table table name * @param _value value to insert - * @return true on success + * @return index of inserted record or -1 if no insertion */ - bool add(const QString &_table, const QVariantHash &_value); + long long add(const QString &_table, const QVariantHash &_value); /** * @brief check and create database */ @@ -132,6 +132,12 @@ private: */ QHash getColumnsInRecord(const QStringList &_columns, const QSqlRecord &_record) const; + /** + * @brief last insertion ID + * @param _table table name + * @return last insertion id from table + */ + long long lastInsertionId(const QString &_table) const; /** * @brief additional function to get payload for query * @param _table table name for search @@ -142,4 +148,5 @@ private: getQueryPayload(const QString &_table, const QVariantHash &_value) const; }; + #endif /* QUEUEDDATABASE_H */ diff --git a/sources/queued/include/queued/QueuedDatabaseSchema.h b/sources/queued/include/queued/QueuedDatabaseSchema.h index 111e07b..4cc921d 100644 --- a/sources/queued/include/queued/QueuedDatabaseSchema.h +++ b/sources/queued/include/queued/QueuedDatabaseSchema.h @@ -28,27 +28,6 @@ #include -/** - * @struct QueuedDBField - * @brief describes database column - * @var name - * column name - * @var sqlDescription - * description to create column - * @var type - * Qt type of column for cast - */ -typedef struct { - QString name; - QString sqlDescription; - QVariant::Type type; -} QueuedDBField; -/** - * @typedef QueuedDBSchema - * custom map for database schemas descriptions - */ -typedef QHash> QueuedDBSchema; - /** * @defgroup QueuedDB * @brief Queued database related constants @@ -56,9 +35,53 @@ typedef QHash> QueuedDBSchema; namespace QueuedDB { /** + *@ingroup QueuedDB +* @struct QueuedDBField +* @brief describes database column +* @var name +* column name +* @var sqlDescription +* description to create column +* @var type +* Qt type of column for cast +*/ +typedef struct { + QString name; + QString sqlDescription; + QVariant::Type type; +} QueuedDBField; +/** + * @ingroup QueuedDB + * @typedef QueuedDBSchema + * custom map for database schemas descriptions + */ +typedef QHash> QueuedDBSchema; +/** + * @ingroup QueuedDB * @brief database schema */ const QueuedDBSchema DBSchema = { + {"settings", + {{"_id", + {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, + {"key", {"key", "TEXT NOT NULL DEFAULT '0'", QVariant::String}}, + {"value", {"value", "TEXT", QVariant::String}}}}, + {"tasks", + {{"_id", + {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, + {"userId", {"userId", "INT NOT NULL DEFAULT 0", QVariant::LongLong}}, + {"command", {"command", "TEXT", QVariant::String}}, + {"arguments", {"arguments", "TEXT", QVariant::String}}, + {"workDirectory", {"workDirectory", "TEXT", QVariant::String}}, + {"nice", {"nice", "INT", QVariant::UInt}}, + {"startTime", {"startTime", "INT", QVariant::LongLong}}, + {"endTime", {"endTime", "INT", QVariant::LongLong}}}}, + {"tokens", + {{"_id", + {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, + {"token", {"token", "TEXT NOT NULL DEFAULT '0'", QVariant::String}}, + {"validUntil", + {"validUntil", "TEXT NOT NULL DEFAULT '0'", QVariant::String}}}}, {"users", {{"_id", {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, @@ -70,17 +93,7 @@ const QueuedDBSchema DBSchema = { {"memory", {"memory", "INT", QVariant::LongLong}}, {"gpumemory", {"gpumemory", "INT", QVariant::LongLong}}, {"storage", {"storage", "INT", QVariant::LongLong}}, - {"permissions", {"permissions", "INT", QVariant::UInt}}}}, - {"tasks", - {{"_id", - {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, - {"userId", {"userId", "INT NOT NULL DEFAULT 0", QVariant::LongLong}}, - {"command", {"command", "TEXT", QVariant::String}}, - {"arguments", {"arguments", "TEXT", QVariant::String}}, - {"workDirectory", {"workDirectory", "TEXT", QVariant::String}}, - {"nice", {"nice", "INT", QVariant::UInt}}, - {"startTime", {"startTime", "INT", QVariant::LongLong}}, - {"endTime", {"endTime", "INT", QVariant::LongLong}}}}}; + {"permissions", {"permissions", "INT", QVariant::UInt}}}}}; }; #endif /* QUEUEDDATABASESCHEMA_H */ diff --git a/sources/queued/include/queued/QueuedDebug.h b/sources/queued/include/queued/QueuedDebug.h index 8cd3836..ddbfc42 100644 --- a/sources/queued/include/queued/QueuedDebug.h +++ b/sources/queued/include/queued/QueuedDebug.h @@ -55,6 +55,7 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_SERV) namespace QueuedDebug { /** + * @ingroup QueuedDebug * @brief default log format */ const char LOG_FORMAT[] = "[%{time " @@ -64,6 +65,7 @@ const char LOG_FORMAT[] = "[%{time " "%{message}"; /** + * @ingroup QueuedDebug * @brief additional method to get build details declared in version.h */ QStringList getBuildData(); diff --git a/sources/queued/include/queued/QueuedEnums.h b/sources/queued/include/queued/QueuedEnums.h index ec83c72..3327071 100644 --- a/sources/queued/include/queued/QueuedEnums.h +++ b/sources/queued/include/queued/QueuedEnums.h @@ -32,6 +32,7 @@ namespace QueuedEnums { /** + * @ingroup QueuedEnums * @enum LimitType * @brief available limit types * @var LimitType::CPUThreads @@ -54,9 +55,8 @@ enum LimitType { }; Q_DECLARE_FLAGS(LimitTypes, LimitType) Q_DECLARE_OPERATORS_FOR_FLAGS(LimitTypes) - - /** + * @ingroup QueuedEnums * @enum Permissions * @brief available user permissions * @var Permissions::Admin diff --git a/sources/queued/include/queued/QueuedProcess.h b/sources/queued/include/queued/QueuedProcess.h index d139075..f1dbedf 100644 --- a/sources/queued/include/queued/QueuedProcess.h +++ b/sources/queued/include/queued/QueuedProcess.h @@ -30,29 +30,6 @@ #include "QueuedEnums.h" -/** - * @struct QueuedProcessDefinition - * @brief structure to define process - * @var cmd - * command line - * @var args - * command line arguments - * @var workingDirectory - * path to working directory - * @var uid - * UID of process - * @var gid - * GID of process - */ -typedef struct { - QString cmd; - QStringList args; - QString workingDirectory; - unsigned int uid; - unsigned int gid; -} QueuedProcessDefinitions; - - /** * @brief implementation over QProcess to run processes */ @@ -63,6 +40,28 @@ class QueuedProcess : public QProcess Q_PROPERTY(QString name READ name) public: + /** + * @struct QueuedProcessDefinition + * @brief structure to define process + * @var cmd + * command line + * @var args + * command line arguments + * @var workingDirectory + * path to working directory + * @var uid + * UID of process + * @var gid + * GID of process + */ + typedef struct { + QString cmd; + QStringList args; + QString workingDirectory; + unsigned int uid; + unsigned int gid; + } QueuedProcessDefinitions; + /** * @brief QueuedProcess class constructor * @param parent pointer to parent item @@ -129,14 +128,15 @@ private: /** * @brief limits array */ - QMap m_limits; + QHash m_limits; /** * @brief convert QString memory value to integer * @param _value value to convert - * @param _status convertion status + * @param _status conversion status * @return converted integer */ - long long convertMemory(QString _value, bool &_status) const; + long long convertMemory(QString _value, bool *_status) const; }; + #endif /* QUEUEDPROCESS_H */ diff --git a/sources/queued/include/queued/QueuedProcessManager.h b/sources/queued/include/queued/QueuedProcessManager.h index 7e8a2ee..e3ff4f5 100644 --- a/sources/queued/include/queued/QueuedProcessManager.h +++ b/sources/queued/include/queued/QueuedProcessManager.h @@ -31,17 +31,6 @@ #include "QueuedProcess.h" -/** - * @typedef QueuedProcessMap - * map of indices to QueuedProcess pointers - */ -typedef QHash QueuedProcessMap; -/** - * @typedef QueuedProcessConnectionMap - * map of indices to related QMetaObject::Connection - */ -typedef QHash QueuedProcessConnectionMap; - /** * @brief implementation over QProcess to run processes */ @@ -52,6 +41,18 @@ class QueuedProcessManager : public QObject public: /** + * @typedef QueuedProcessMap + * map of indices to QueuedProcess pointers + */ + typedef QHash QueuedProcessMap; + /** + * @typedef QueuedProcessConnectionMap + * map of indices to related QMetaObject::Connection + */ + typedef QHash + QueuedProcessConnectionMap; + + /** * @enum OnExitAction * @brief action with child process on destruction * @var OnExitAction::Terminate @@ -77,8 +78,9 @@ public: * @param _definitions process definitions * @return pointer to created task */ - QueuedProcess *add(const long long _index, - const QueuedProcessDefinitions _definitions); + QueuedProcess * + add(const long long _index, + const QueuedProcess::QueuedProcessDefinitions _definitions); /** * @brief default action on exit * @return default action from possible ones @@ -146,4 +148,5 @@ private: QueuedProcessMap m_processes; }; + #endif /* QUEUEDPROCESS_H */ diff --git a/sources/queued/include/queued/QueuedSettings.h b/sources/queued/include/queued/QueuedSettings.h new file mode 100644 index 0000000..30a0063 --- /dev/null +++ b/sources/queued/include/queued/QueuedSettings.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedSettings.h + * Header of Queued library + * @author Evgeniy Alekseev + * @copyright MIT + * @bug https://github.com/arcan1s/queued/issues + */ + + +#ifndef QUEUEDSETTINGS_H +#define QUEUEDSETTINGS_H + +#include + +#include "QueuedConfiguration.h" + + +/** + * @brief implementation over QSettings + */ +class QueuedSettings : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString path READ path) + Q_PROPERTY(QueuedCfg::QueuedAdminSetup admin READ admin) + Q_PROPERTY(QueuedCfg::QueuedDBSetup db READ db) + +public: + /** + * @brief QueuedSettings class constructor + * @param parent pointer to parent item + * @param path path to configuration file + */ + explicit QueuedSettings(QObject *parent, const QString path); + /** + * @brief QueuedSettings class destructor + */ + virtual ~QueuedSettings(); + /** + * @brief administrator settings + * @return QueuedAdminSetup structure + */ + QueuedCfg::QueuedAdminSetup admin() const; + /** + * @brief database settings + * @return QueuedDBSetup structure + */ + QueuedCfg::QueuedDBSetup db() const; + /** + * @brief default path to configuration + * @return default path to configuration file + */ + static QString defaultPath(); + /** + * @brief path to database + * @return path to used database + */ + QString path() const; + +public slots: + /** + * @brief read configuration from file + */ + void readConfiguration(); + +private: + /** + * @brief admin configuration + */ + QueuedCfg::QueuedAdminSetup m_cfgAdmin; + /** + * @brief database configuration + */ + QueuedCfg::QueuedDBSetup m_cfgDB; + /** + * @brief path to configuration + */ + QString m_path; +}; + + +#endif /* QUEUEDSETTINGS_H */ diff --git a/sources/queued/include/queued/QueuedTokenManager.h b/sources/queued/include/queued/QueuedTokenManager.h new file mode 100644 index 0000000..b244489 --- /dev/null +++ b/sources/queued/include/queued/QueuedTokenManager.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedTokenManager.h + * Header of Queued library + * @author Evgeniy Alekseev + * @copyright MIT + * @bug https://github.com/arcan1s/queued/issues + */ + + +#ifndef QUEUEDTOKENMANAGER_H +#define QUEUEDTOKENMANAGER_H + +#include +#include + + +/** + * @brief token management system + */ +class QueuedTokenManager : public QObject +{ + Q_OBJECT + +public: + /** + * @brief QueuedTokenManager class constructor + * @param parent pointer to parent item + */ + explicit QueuedTokenManager(QObject *parent); + /** + * @brief QueuedTokenManager class destructor + */ + virtual ~QueuedTokenManager(); + /** + * @brief check if token is valid + * @param _token token ID + * @return true if token is valid otherwise return false + */ + bool isTokenValid(const QString &_token); + /** + * @brief register new token + * @param _validUntil token valid until + * @return new generated token + */ + QString registerToken(const QDateTime _validUntil); + /** + * @brief upload tokens from database + * @param _value tokens from database + */ + void setValues(const QList &_values); + +public slots: + /** + * @brief method which will be called on token expiring + * @param _token expired token ID + */ + void expireToken(const QString &_token); + +signals: + /** + * @brief signal which will be emitted on token expiration + * @param _token token ID + */ + void tokenExpired(const QString &_token); + /** + * @brief signal which will be emitted on newly created token + * @param _token token ID + * @param _validUntil token valid until + */ + void tokenRegistered(const QString &_token, const QDateTime &_validUntil); + +private: + /** + * @brief token storage + */ + QHash m_tokens; +}; + + +#endif /* QUEUEDTOKENMANAGER_H */ diff --git a/sources/queued/include/queued/QueuedUser.h b/sources/queued/include/queued/QueuedUser.h index d4e3876..8632019 100644 --- a/sources/queued/include/queued/QueuedUser.h +++ b/sources/queued/include/queued/QueuedUser.h @@ -30,41 +30,6 @@ #include "QueuedEnums.h" -/** - * @struct QueuedUserDefinition - * @brief structure to define user - * @var name - * user name - * @var email - * user email - * @var passwordSHA512 - * password hash, may be empty - * @var permissions - * user permissions - * @var cpuLimit - * user limit by CPU - * @var gpuLimit - * user limit by GPU - * @var memoryLimit - * user limit by memory - * @var gpumemoryLimit - * user limit by GPU memory - * @var storageLimit - * user limit by storage - */ -typedef struct { - QString name; - QString email; - QString passwordSHA512; - unsigned int permissions; - long long cpuLimit; - long long gpuLimit; - long long memoryLimit; - long long gpumemoryLimit; - long long storageLimit; -} QueuedUserDefinitions; - - /** * @brief representation of user in queued */ @@ -92,6 +57,40 @@ class QueuedUser : public QObject NOTIFY userUpdated) public: + /** + * @struct QueuedUserDefinition + * @brief structure to define user + * @var name + * user name + * @var email + * user email + * @var passwordSHA512 + * password hash, may be empty + * @var permissions + * user permissions + * @var cpuLimit + * user limit by CPU + * @var gpuLimit + * user limit by GPU + * @var memoryLimit + * user limit by memory + * @var gpumemoryLimit + * user limit by GPU memory + * @var storageLimit + * user limit by storage + */ + typedef struct { + QString name; + QString email; + QString passwordSHA512; + unsigned int permissions; + long long cpuLimit; + long long gpuLimit; + long long memoryLimit; + long long gpumemoryLimit; + long long storageLimit; + } QueuedUserDefinitions; + /** * @brief QueuedUser class constructor * @param parent pointer to parent item diff --git a/sources/queued/src/QueuedAdvancedSettings.cpp b/sources/queued/src/QueuedAdvancedSettings.cpp new file mode 100644 index 0000000..c1b289b --- /dev/null +++ b/sources/queued/src/QueuedAdvancedSettings.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedAdvancedSettings.cpp + * Source code of queued library + * @author Evgeniy Alekseev + * @copyright GPLv3 + * @bug https://github.com/arcan1s/queued/issues + */ + + +#include "queued/Queued.h" + + +/** + * @class QueuedAdvancedSettings + */ +/** + * @fn QueuedAdvancedSettings + */ +QueuedAdvancedSettings::QueuedAdvancedSettings(QObject *parent) + : QObject(parent) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +/** + * @fn ~QueuedAdvancedSettings + */ +QueuedAdvancedSettings::~QueuedAdvancedSettings() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +/** + * @fn setValues + */ +void QueuedAdvancedSettings::setValues(const QList &_values) +{ + qCDebug(LOG_LIB) << "Set values from" << _values; +} diff --git a/sources/queued/src/QueuedDatabase.cpp b/sources/queued/src/QueuedDatabase.cpp index 131a5b7..7c714a9 100644 --- a/sources/queued/src/QueuedDatabase.cpp +++ b/sources/queued/src/QueuedDatabase.cpp @@ -58,7 +58,7 @@ QueuedDatabase::~QueuedDatabase() /** * @fn add */ -bool QueuedDatabase::add(const QString &_table, const QVariantHash &_value) +long long QueuedDatabase::add(const QString &_table, const QVariantHash &_value) { qCDebug(LOG_LIB) << "Add record" << _value << "to table" << _table; @@ -73,10 +73,10 @@ bool QueuedDatabase::add(const QString &_table, const QVariantHash &_value) if (error.isValid()) { qCCritical(LOG_LIB) << "Could not add record" << _value << "to table" << _table << "message" << error.text(); - return false; + return -1; } - return true; + return lastInsertionId(_table); } @@ -269,7 +269,7 @@ void QueuedDatabase::createSchema(const QString &_table) for (auto &column : schemaColumns) { if (columns.contains(column)) continue; - QueuedDBField field = QueuedDB::DBSchema[_table][column]; + QueuedDB::QueuedDBField field = QueuedDB::DBSchema[_table][column]; QSqlQuery query = m_database.exec(QString("ALTER TABLE '%1' ADD `%2` %3") .arg(_table) @@ -319,6 +319,29 @@ QueuedDatabase::getColumnsInRecord(const QStringList &_columns, } +/** + * @fn lastInsertionId + */ +long long QueuedDatabase::lastInsertionId(const QString &_table) const +{ + qCDebug(LOG_LIB) << "Get last row ID from" << _table; + + QSqlQuery query + = m_database.exec(QString("SELECT max(_id) FROM '%1'").arg(_table)); + QSqlError error = query.lastError(); + if (error.isValid()) { + qCCritical(LOG_LIB) << "Could not get last insertion ID"; + return -1; + } + + long long id; + while (query.next()) + id = query.value(0).toLongLong(); + + return id; +} + + /** * @fn getQueryPayload */ diff --git a/sources/queued/src/QueuedProcess.cpp b/sources/queued/src/QueuedProcess.cpp index 8a3d4f2..ef208f6 100644 --- a/sources/queued/src/QueuedProcess.cpp +++ b/sources/queued/src/QueuedProcess.cpp @@ -99,7 +99,7 @@ void QueuedProcess::setLimit(const QueuedEnums::LimitType _limitType, bool status = false; long long intValue = _value.type() == QVariant::String - ? convertMemory(_value.toString(), status) + ? convertMemory(_value.toString(), &status) : _value.toLongLong(&status); if (!status) @@ -141,21 +141,21 @@ void QueuedProcess::run() /** * @fn convertMemory */ -long long QueuedProcess::convertMemory(QString _value, bool &_status) const +long long QueuedProcess::convertMemory(QString _value, bool *_status) const { qCDebug(LOG_LIB) << "Convert memory value" << _value; long long intValue; if (_value.endsWith(QString("K"))) - intValue = _value.remove(QString("K")).toLongLong(&_status) * 1024; + intValue = _value.remove(QString("K")).toLongLong(_status) * 1024; else if (_value.endsWith(QString("M"))) intValue - = _value.remove(QString("M")).toLongLong(&_status) * 1024 * 1024; + = _value.remove(QString("M")).toLongLong(_status) * 1024 * 1024; else if (_value.endsWith(QString("G"))) - intValue = _value.remove(QString("G")).toLongLong(&_status) * 1024 - * 1024 * 1024; + intValue = _value.remove(QString("G")).toLongLong(_status) * 1024 * 1024 + * 1024; else - intValue = _value.toInt(&_status); + intValue = _value.toInt(_status); qCInfo(LOG_LIB) << "Converted value" << intValue; return intValue; diff --git a/sources/queued/src/QueuedProcessManager.cpp b/sources/queued/src/QueuedProcessManager.cpp index ba0de53..f785d4e 100644 --- a/sources/queued/src/QueuedProcessManager.cpp +++ b/sources/queued/src/QueuedProcessManager.cpp @@ -52,9 +52,9 @@ QueuedProcessManager::~QueuedProcessManager() /** * @fn add */ -QueuedProcess * -QueuedProcessManager::add(const long long _index, - const QueuedProcessDefinitions _definitions) +QueuedProcess *QueuedProcessManager::add( + const long long _index, + const QueuedProcess::QueuedProcessDefinitions _definitions) { qCDebug(LOG_LIB) << "Add new process" << _definitions.cmd << "with index" << _index; @@ -99,7 +99,7 @@ QueuedProcess *QueuedProcessManager::process(const long long _index) /** * @fn processes */ -QueuedProcessMap QueuedProcessManager::processes() +QueuedProcessManager::QueuedProcessMap QueuedProcessManager::processes() { return m_processes; } diff --git a/sources/queued/src/QueuedSettings.cpp b/sources/queued/src/QueuedSettings.cpp new file mode 100644 index 0000000..52b40cd --- /dev/null +++ b/sources/queued/src/QueuedSettings.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedSettings.cpp + * Source code of queued library + * @author Evgeniy Alekseev + * @copyright GPLv3 + * @bug https://github.com/arcan1s/queued/issues + */ + + +#include "queued/Queued.h" + +#include +#include + + +/** + * @class QueuedSettings + */ +/** + * @fn QueuedSettings + */ +QueuedSettings::QueuedSettings(QObject *parent, const QString path) + : QObject(parent) + , m_path(path) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + readConfiguration(); +} + + +/** + * @fn ~QueuedSettings + */ +QueuedSettings::~QueuedSettings() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +/** + * @fn admin + */ +QueuedCfg::QueuedAdminSetup QueuedSettings::admin() const +{ + return m_cfgAdmin; +} + + +/** + * @fn db + */ +QueuedCfg::QueuedDBSetup QueuedSettings::db() const +{ + return m_cfgDB; +} + + +/** + * @fn defaultPath + */ +QString QueuedSettings::defaultPath() +{ + QString fileName = QStandardPaths::locate(QStandardPaths::ConfigLocation, + QString("queued.ini")); + qCInfo(LOG_LIB) << "Found configuration file" << fileName; + + return fileName; +} + + +/** + * @fn path + */ +QString QueuedSettings::path() const +{ + return m_path; +} + + +/** + * @fn readConfiguration + */ +void QueuedSettings::readConfiguration() +{ + QSettings settings(m_path, QSettings::IniFormat); + qCInfo(LOG_LIB) << "Read configuration from" << settings.fileName(); + + // administrator related settings + settings.beginGroup(QString("Administrator")); + m_cfgAdmin.name + = settings.value(QString("Username"), QString("root")).toString(); + m_cfgAdmin.password = settings.value(QString("Password")).toString(); + settings.endGroup(); + + // database related settings + settings.beginGroup(QString("Database")); + m_cfgDB.driver + = settings.value(QString("Driver"), QString("QSQLITE")).toString(); + m_cfgDB.hostname = settings.value(QString("Hostname")).toString(); + m_cfgDB.password = settings.value(QString("Password")).toString(); + // get standard path for temporary files + QString defaultDB = QString("%1/queued.db") + .arg(QStandardPaths::writableLocation( + QStandardPaths::TempLocation)); + m_cfgDB.path = settings.value(QString("Path"), defaultDB).toString(); + m_cfgDB.port = settings.value(QString("Port")).toInt(); + m_cfgDB.username = settings.value(QString("Username")).toString(); + settings.endGroup(); +} diff --git a/sources/queued/src/QueuedTokenManager.cpp b/sources/queued/src/QueuedTokenManager.cpp new file mode 100644 index 0000000..01faf29 --- /dev/null +++ b/sources/queued/src/QueuedTokenManager.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2016 Evgeniy Alekseev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ +/** + * @file QueuedTokenManager.cpp + * Source code of queued library + * @author Evgeniy Alekseev + * @copyright GPLv3 + * @bug https://github.com/arcan1s/queued/issues + */ + + +#include "queued/Queued.h" + +#include +#include + + +/** + * @class QueuedTokenManager + */ +/** + * @fn QueuedTokenManager + */ +QueuedTokenManager::QueuedTokenManager(QObject *parent) + : QObject(parent) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +/** + * @fn ~QueuedTokenManager + */ +QueuedTokenManager::~QueuedTokenManager() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +/** + * @fn isTokenValid + */ +bool QueuedTokenManager::isTokenValid(const QString &_token) +{ + qCDebug(LOG_LIB) << "Check token on validity" << _token; + + return m_tokens.contains(_token); +} + + +/** + * @fn registerToken + */ +QString QueuedTokenManager::registerToken(const QDateTime _validUntil) +{ + // generate from uuid + QString token + = QUuid::createUuid().toString().remove(QChar('{')).remove(QChar('}')); + qCInfo(LOG_LIB) << "Registered token" << token << "valid until" + << _validUntil; + + // append to internal storage + m_tokens[token] = _validUntil; + emit(tokenRegistered(token, _validUntil)); + + // and return requester + return token; +} + + +/** + * @fn setValues + */ +void QueuedTokenManager::setValues(const QList &_values) +{ + qCDebug(LOG_LIB) << "Set values from" << _values; + + QDateTime now = QDateTime::currentDateTimeUtc(); + m_tokens.clear(); + for (auto &token : _values) { + QDateTime validUntil = QDateTime::fromString( + token[QString("validUntil")].toString(), Qt::ISODate); + if (validUntil <= now) + continue; + QString tokenId = token[QString("token")].toString(); + m_tokens[tokenId] = validUntil; + QTimer::singleShot(validUntil.toMSecsSinceEpoch() + - now.toMSecsSinceEpoch(), + Qt::VeryCoarseTimer, + [this, tokenId]() { return expireToken(tokenId); }); + } +} + + +/** + * @fn expireToken + */ +void QueuedTokenManager::expireToken(const QString &_token) +{ + qCDebug(LOG_LIB) << "Expire token" << _token; + + if (!m_tokens.contains(_token)) + return; + m_tokens.remove(_token); + emit(tokenExpired(_token)); +} diff --git a/sources/queued/src/QueuedUser.cpp b/sources/queued/src/QueuedUser.cpp index c5a463b..723f459 100644 --- a/sources/queued/src/QueuedUser.cpp +++ b/sources/queued/src/QueuedUser.cpp @@ -32,7 +32,8 @@ /** * @fn QueuedUser */ -QueuedUser::QueuedUser(QObject *parent, const QueuedUserDefinitions &definitions, +QueuedUser::QueuedUser(QObject *parent, + const QueuedUserDefinitions &definitions, const long long index) : QObject(parent) , m_definitions(definitions) diff --git a/sources/version.h.in b/sources/version.h.in index a73e4c0..726b76b 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -19,6 +19,13 @@ const char SPECIAL_THANKS[] = ""; #cmakedefine BUILD_LOAD #cmakedefine BUILD_TESTING +// paths +const char BIN_INSTALL_DIR[] = "@BIN_INSTALL_DIR@"; +const char DATA_INSTALL_DIR[] = "@DATA_INSTALL_DIR@"; +const char INCLUDE_INSTALL_DIR[] = "@INCLUDE_INSTALL_DIR@"; +const char LIB_INSTALL_DIR[] = "@LIB_INSTALL_DIR@"; +const char ROOT_INSTALL_DIR[] = "@CMAKE_INSTALL_PREFIX@"; + // links const char HOMEPAGE[] = "https://arcanis.me/projects/queued"; const char REPOSITORY[] = "https://github.com/arcan1s/queued";