From 3f5e0d0a9fa0e3fafe6af9d304ac561bdfda1956 Mon Sep 17 00:00:00 2001 From: tetektoza Date: Fri, 20 Oct 2023 16:38:55 +0200 Subject: [PATCH 1/2] Fix: Incorrect directory path if tool is installed by package Currently, if we install d1-graphics-tool with a .deb package, it will try to resolve the path to save a .json file to /usr/bin, since currently we save the configuration file wherever the binary is. User doesn't have an access to write to /usr/bin/ directly and this ends up with a problem in reading/writing to a config, resetting the config everytime app is being used. This patch changes it, and leaves the configuration for the file to be saved in: Windows: AppData/.config/diasurgical/d1-graphics-tool/ directory path Otherwise: /home/user/.config/diasurgical/d1-graphics-tool/ directory path --- source/config.cpp | 23 ++++++++++++++++++++--- source/config.h | 5 +++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/source/config.cpp b/source/config.cpp index c05a4cb7..d35bc378 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -1,15 +1,27 @@ #include "config.h" #include +#include +#include #include #include #include +#include static QJsonObject theConfig; +QString Config::jsonFilePath; void Config::loadConfiguration() { - QString jsonFilePath = QCoreApplication::applicationDirPath() + "/D1GraphicsTool.config.json"; + // create directories on the path if they do not exist + if (!Config::createDirectoriesOnPath()) { + qDebug() << "Couldn't resolve path for the config file. Configuration file won't be loaded."; + return; + } + + // add filename to the absolute path + jsonFilePath += "/D1GraphicsTool.config.json"; + bool configurationModified = false; // If configuration file exists load it otherwise create it @@ -41,8 +53,6 @@ void Config::loadConfiguration() void Config::storeConfiguration() { - QString jsonFilePath = QCoreApplication::applicationDirPath() + "/D1GraphicsTool.config.json"; - QFile saveJson(jsonFilePath); saveJson.open(QIODevice::WriteOnly); QJsonDocument saveDoc(theConfig); @@ -50,6 +60,13 @@ void Config::storeConfiguration() saveJson.close(); } +bool Config::createDirectoriesOnPath() +{ + jsonFilePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation); + + return QDir().mkpath(jsonFilePath); +} + QJsonValue Config::value(const QString &name) { return theConfig.value(name); diff --git a/source/config.h b/source/config.h index 0ab55f43..1965da0a 100644 --- a/source/config.h +++ b/source/config.h @@ -8,4 +8,9 @@ class Config { static void storeConfiguration(); static QJsonValue value(const QString &name); static void insert(const QString &key, const QJsonValue &value); + +private: + static bool createDirectoriesOnPath(); + + static QString jsonFilePath; }; From ce4f28fd4d035b80d7f0b67a933549ced232d084 Mon Sep 17 00:00:00 2001 From: tetektoza Date: Fri, 20 Oct 2023 16:56:11 +0200 Subject: [PATCH 2/2] Documentation: Add documentatioon to config.cpp Adds documentation to config.cpp, for functions that were missing it and also for newly added createDirectoriesOnPath(). --- source/config.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/source/config.cpp b/source/config.cpp index d35bc378..a358fb34 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -11,6 +11,13 @@ static QJsonObject theConfig; QString Config::jsonFilePath; +/** + * @brief Loads current configuration from the config .json file + * + * This function loads current configuration from the config .json file. + * It inserts specific values mapping them to keys, and creates path for the + * config if user has deleted it, or runs app for the first time. + */ void Config::loadConfiguration() { // create directories on the path if they do not exist @@ -51,6 +58,9 @@ void Config::loadConfiguration() } } +/** + * @brief Stores current configuration in the config .json file + */ void Config::storeConfiguration() { QFile saveJson(jsonFilePath); @@ -60,6 +70,17 @@ void Config::storeConfiguration() saveJson.close(); } +/** + * @brief Creates directories on certain path + * + * This function creates directories on certain path. Path location depends on + * if user is working on Windows or Mac/Linux. + * + * If the user works on Windows, it will save it under AppData/.config/[...]. + * On any other OS it will save it under /home/user/.config/diasurgical/[...] + * + * @return Returns true if path has been created or already existed - false otherwise + */ bool Config::createDirectoriesOnPath() { jsonFilePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation); @@ -67,11 +88,26 @@ bool Config::createDirectoriesOnPath() return QDir().mkpath(jsonFilePath); } +/** + * @brief Retrieves value from .json config file + * + * This function retrieves value from .json config file by the value + * specified by name parameter. + * + * @return Returns QJsonValue containing value of the parameter specified + * by "name" key, otherwise if not found - returns QJsonValue::Undefined + */ QJsonValue Config::value(const QString &name) { return theConfig.value(name); } +/** + * @brief Inserts value in .json config file + * + * This function inserts value into .json config file, mapping it with + * the key specified in parameters. + */ void Config::insert(const QString &key, const QJsonValue &value) { theConfig.insert(key, value);