2026-07-01 03:32:41 +00:00
|
|
|
#include "ConfigHelper.h"
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QSaveFile>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
ConfigHelper& ConfigHelper::instance()
|
|
|
|
|
{
|
|
|
|
|
static ConfigHelper obj;
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConfigHelper::ConfigHelper()
|
|
|
|
|
{
|
|
|
|
|
m_configPath = QApplication::applicationDirPath() + "/config/app_config.json";
|
|
|
|
|
migrateLegacyIniIfNeeded();
|
|
|
|
|
qDebug() << "Loading app config path:" << m_configPath;
|
|
|
|
|
qDebug() << "File exists?" << QFile::exists(m_configPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ConfigHelper::configPath() const
|
|
|
|
|
{
|
|
|
|
|
return m_configPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ConfigHelper::getValue(const QString& section, const QString& key) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(section);
|
|
|
|
|
QFile file(m_configPath);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
|
const QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &error);
|
|
|
|
|
if (error.error != QJsonParseError::NoError || !document.isObject())
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
return document.object().value(key).toVariant().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigHelper::setValue(const QString& section, const QString& key, const QString& value)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(section);
|
|
|
|
|
QFile input(m_configPath);
|
|
|
|
|
QJsonObject config;
|
|
|
|
|
if (input.exists())
|
|
|
|
|
{
|
|
|
|
|
if (!input.open(QIODevice::ReadOnly))
|
|
|
|
|
return false;
|
|
|
|
|
QJsonParseError parseError;
|
|
|
|
|
const QByteArray raw = input.readAll();
|
|
|
|
|
input.close();
|
|
|
|
|
const QJsonDocument document = QJsonDocument::fromJson(raw, &parseError);
|
|
|
|
|
if (parseError.error != QJsonParseError::NoError || !document.isObject())
|
|
|
|
|
return false;
|
|
|
|
|
config = document.object();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.insert(key, value);
|
|
|
|
|
QDir().mkpath(QFileInfo(m_configPath).path());
|
|
|
|
|
QSaveFile output(m_configPath);
|
|
|
|
|
if (!output.open(QIODevice::WriteOnly))
|
|
|
|
|
return false;
|
|
|
|
|
const QByteArray payload = QJsonDocument(config).toJson(QJsonDocument::Indented);
|
|
|
|
|
if (output.write(payload) != payload.size())
|
|
|
|
|
{
|
|
|
|
|
output.cancelWriting();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return output.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigHelper::migrateLegacyIniIfNeeded()
|
|
|
|
|
{
|
|
|
|
|
if (QFile::exists(m_configPath))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const QString legacyPath = QApplication::applicationDirPath() + "/client.ini";
|
|
|
|
|
if (!QFile::exists(legacyPath))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QSettings ini(legacyPath, QSettings::IniFormat);
|
|
|
|
|
QJsonObject config;
|
|
|
|
|
const auto copyText = [&](const QString& section, const QString& key, const QString& fallback = QString()) {
|
|
|
|
|
const QString value = ini.value(section + "/" + key, fallback).toString();
|
|
|
|
|
config.insert(key, value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
copyText("App", "app_id");
|
|
|
|
|
copyText("App", "app_name", "Marsco Demo App");
|
|
|
|
|
copyText("App", "channel", "stable");
|
|
|
|
|
copyText("App", "current_version", "1.0.0");
|
2026-07-07 09:48:01 +00:00
|
|
|
copyText("App", "client_protocol", "3");
|
2026-07-01 03:32:41 +00:00
|
|
|
copyText("App", "launch_token");
|
2026-07-07 09:48:01 +00:00
|
|
|
copyText("License", "license_key");
|
2026-07-01 03:32:41 +00:00
|
|
|
copyText("Server", "api_base_url");
|
|
|
|
|
copyText("Server", "client_token");
|
|
|
|
|
copyText("Update", "request_timeout_ms", "5000");
|
|
|
|
|
copyText("Update", "temp_folder", "update_temp");
|
|
|
|
|
copyText("Update", "device_id");
|
2026-07-07 09:48:01 +00:00
|
|
|
copyText("Runtime", "main_executable", "MainApp.exe");
|
|
|
|
|
copyText("Runtime", "launcher_executable", "Launcher.exe");
|
|
|
|
|
copyText("Runtime", "updater_executable", "Updater.exe");
|
|
|
|
|
copyText("Runtime", "bootstrap_executable", "Bootstrap.exe");
|
|
|
|
|
copyText("Runtime", "health_check_timeout_ms", "15000");
|
2026-07-01 03:32:41 +00:00
|
|
|
config.insert("platform", "windows");
|
|
|
|
|
config.insert("arch", "x64");
|
|
|
|
|
|
|
|
|
|
QDir().mkpath(QFileInfo(m_configPath).path());
|
|
|
|
|
QSaveFile output(m_configPath);
|
|
|
|
|
if (!output.open(QIODevice::WriteOnly))
|
|
|
|
|
return false;
|
|
|
|
|
output.write(QJsonDocument(config).toJson(QJsonDocument::Indented));
|
|
|
|
|
const bool saved = output.commit();
|
|
|
|
|
if (saved)
|
|
|
|
|
qDebug() << "Migrated legacy client.ini to" << m_configPath;
|
|
|
|
|
return saved;
|
|
|
|
|
}
|