Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Incorrect directory path if tool is installed by package #119

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions source/config.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
#include "config.h"

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

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()
{
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 @@ -39,22 +58,56 @@ void Config::loadConfiguration()
}
}

/**
* @brief Stores current configuration in the config .json file
*/
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();
}

/**
* @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);

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);
Expand Down
5 changes: 5 additions & 0 deletions source/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
AJenbo marked this conversation as resolved.
Show resolved Hide resolved
static bool createDirectoriesOnPath();

static QString jsonFilePath;
};