Skip to content
Snippets Groups Projects
confighandler.cpp 3.07 KiB
Newer Older
akiraohgaki's avatar
akiraohgaki committed
#include "confighandler.h"

akiraohgaki's avatar
akiraohgaki committed
#include <QStringList>
akiraohgaki's avatar
akiraohgaki committed
#include <QStandardPaths>
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
#include "qtil_dir.h"
akiraohgaki's avatar
akiraohgaki committed

ConfigHandler::ConfigHandler(QObject *parent)
    : QObject(parent)
{
akiraohgaki's avatar
akiraohgaki committed
    appConfig_ = Qtil::Config(":/configs");
    importAppConfigApplication();
    importAppConfigInstallTypes();
QJsonObject ConfigHandler::getAppConfigApplication() const
akiraohgaki's avatar
akiraohgaki committed
{
    return appConfigApplication_;
}

QJsonObject ConfigHandler::getAppConfigInstallTypes() const
akiraohgaki's avatar
akiraohgaki committed
{
    return appConfigInstallTypes_;
}

void ConfigHandler::importAppConfigApplication()
{
    appConfigApplication_ = appConfig_.get("application");
}

void ConfigHandler::importAppConfigInstallTypes()
{
    auto installTypes = appConfig_.get("install_types");
    for (const auto &key : installTypes.keys()) {
        auto installtype = installTypes[key].toObject();
        installtype["destination"] = convertPathString(installtype["destination"].toString());
        installtype["generic_destination"] = convertPathString(installtype["generic_destination"].toString());
        installTypes[key] = installtype;
    }
    auto installTypesAlias = appConfig_.get("install_types_alias");
    for (const auto &key : installTypesAlias.keys()) {
        auto installTypeAlias = installTypesAlias[key].toObject();
        auto baseKey = installTypeAlias["base"].toString();
        if (installTypes.contains(baseKey)) {
            auto installType = installTypes[baseKey].toObject();
            installType["base"] = baseKey;
            installType["name"] = installTypeAlias["name"].toString();
            installTypes[key] = installType;
    appConfigInstallTypes_ = installTypes;
QString ConfigHandler::convertPathString(const QString &path) const
akiraohgaki's avatar
akiraohgaki committed
{
    auto newPath = path;
akiraohgaki's avatar
akiraohgaki committed
    if (newPath.contains("$HOME")) {
akiraohgaki's avatar
akiraohgaki committed
        newPath.replace("$HOME", Qtil::Dir::homePath());
akiraohgaki's avatar
akiraohgaki committed
    }
akiraohgaki's avatar
akiraohgaki committed
    else if (newPath.contains("$XDG_DOCUMENTS_DIR")) {
        newPath.replace("$XDG_DOCUMENTS_DIR", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
    }
    else if (newPath.contains("$XDG_DOWNLOAD_DIR")) {
        newPath.replace("$XDG_DOWNLOAD_DIR", QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
    }
    else if (newPath.contains("$XDG_PICTURES_DIR")) {
        newPath.replace("$XDG_PICTURES_DIR", QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
    }
    else if (newPath.contains("$XDG_MUSIC_DIR")) {
        newPath.replace("$XDG_MUSIC_DIR", QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
    }
    else if (newPath.contains("$XDG_VIDEOS_DIR")) {
        newPath.replace("$XDG_VIDEOS_DIR", QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
    }
akiraohgaki's avatar
akiraohgaki committed
    else if (newPath.contains("$XDG_DATA_HOME")) {
akiraohgaki's avatar
akiraohgaki committed
        newPath.replace("$XDG_DATA_HOME", Qtil::Dir::genericDataPath());
akiraohgaki's avatar
akiraohgaki committed
    }
    else if (newPath.contains("$KDEHOME")) {
akiraohgaki's avatar
akiraohgaki committed
        newPath.replace("$KDEHOME", Qtil::Dir::kdehomePath());
akiraohgaki's avatar
akiraohgaki committed
    }
    else if (newPath.contains("$APP_DATA")) {
akiraohgaki's avatar
akiraohgaki committed
        newPath.replace("$APP_DATA", Qtil::Dir::genericDataPath() + "/" + getAppConfigApplication()["id"].toString());
akiraohgaki's avatar
akiraohgaki committed
    }
    return newPath;
}