Skip to content

Commit

Permalink
Fix: Incorrect directory path if tool is installed by package
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tetektoza committed Oct 20, 2023
1 parent 2e62c58 commit 9aaf5d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
26 changes: 23 additions & 3 deletions source/config.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
#include "config.h"

#include <QDir>
#include <QCoreApplication>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>

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
Expand Down Expand Up @@ -41,15 +52,24 @@ void Config::loadConfiguration()

void Config::storeConfiguration()
{
QString jsonFilePath = QCoreApplication::applicationDirPath() + "/D1GraphicsTool.config.json";

QFile saveJson(jsonFilePath);
saveJson.open(QIODevice::WriteOnly);
QJsonDocument saveDoc(theConfig);
saveJson.write(saveDoc.toJson());
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);
Expand Down
4 changes: 4 additions & 0 deletions source/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 9aaf5d0

Please sign in to comment.