Files
tactility/Tactility/Source/service/development/DevelopmentSettings.cpp
T
Ken Van Hoeylandt 1b16184a72 Multi-platform http server (#646)
- Add server support to `http-module`
- Ensure `DevelopmentService` works on all platforms (including posix)
- Ensure the built-in web server with dashboard works on all platforms (inluding posix). It still has some issues with certain featuers, but the basics work.
2026-09-06 14:16:23 +02:00

74 lines
2.0 KiB
C++

#include <Tactility/file/File.h>
#include <Tactility/file/PropertiesFile.h>
#include <Tactility/service/development/DevelopmentSettings.h>
#include <map>
#include <string>
#include <app/paths.h>
#include <tactility/log.h>
namespace tt::service::development {
constexpr auto* TAG = "DevSettings";
static std::string getSettingsFilePath() {
char path[256];
if (app_paths_get_user_data_path("tactility.development", "development.properties", path, sizeof(path)) != ERROR_NONE) {
return "";
}
return path;
}
constexpr auto* SETTINGS_KEY_ENABLE_ON_BOOT = "enableOnBoot";
struct DevelopmentSettings {
bool enableOnBoot;
};
static bool load(DevelopmentSettings& settings) {
auto settings_path = getSettingsFilePath();
if (!file::isFile(settings_path)) {
return false;
}
std::map<std::string, std::string> map;
if (!file::loadPropertiesFile(settings_path, map)) {
return false;
}
if (!map.contains(SETTINGS_KEY_ENABLE_ON_BOOT)) {
return false;
}
auto enable_on_boot_string = map[SETTINGS_KEY_ENABLE_ON_BOOT];
settings.enableOnBoot = (enable_on_boot_string == "true");
return true;
}
static bool save(const DevelopmentSettings& settings) {
std::map<std::string, std::string> map;
map[SETTINGS_KEY_ENABLE_ON_BOOT] = settings.enableOnBoot ? "true" : "false";
auto settings_path = getSettingsFilePath();
if (!file::findOrCreateParentDirectory(settings_path, 0755)) {
LOG_E(TAG, "Failed to create parent dir for %s", settings_path.c_str());
return false;
}
return file::savePropertiesFile(settings_path, map);
}
void setEnableOnBoot(bool enable) {
DevelopmentSettings properties { .enableOnBoot = enable };
if (!save(properties)) {
LOG_E(TAG, "Failed to save %s", getSettingsFilePath().c_str());
}
}
bool shouldEnableOnBoot() {
DevelopmentSettings settings;
if (!load(settings)) {
return false;
}
return settings.enableOnBoot;
}
}