From 9aaf5d019013defaf1373abf77105b27d2ce70a2 Mon Sep 17 00:00:00 2001 From: tetektoza Date: Fri, 20 Oct 2023 16:38:55 +0200 Subject: [PATCH] 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 | 26 +++++++++++++++++++++++--- source/config.h | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/source/config.cpp b/source/config.cpp index c05a4cb73..3aebb73d8 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -1,15 +1,26 @@ #include "config.h" +#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 +52,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 +59,17 @@ void Config::storeConfiguration() saveJson.close(); } +bool Config::createDirectoriesOnPath() +{ +#ifdef Q_OS_WINDOWS + jsonFilePath = QStandardPaths::AppLocalDataLocation + "/.config/diasurgical/d1-graphics-tool/"; +#else + jsonFilePath = QDir::homePath() + "/.config/diasurgical/d1-graphics-tool/"; +#endif + + 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 0ab55f435..611e3873f 100644 --- a/source/config.h +++ b/source/config.h @@ -8,4 +8,8 @@ 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; };