fixed config

This commit is contained in:
Radek Davidek 2026-03-13 14:01:28 +01:00
parent b03ba089f0
commit 6d48e2f400

View File

@ -1,4 +1,42 @@
#include "config_manager.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <iterator>
#include <glib/gstdio.h>
namespace {
std::string escapeString(const std::string &str) {
std::string result;
for (char c : str) {
if (c == '\\' || c == ':') {
result += '\\';
}
result += c;
}
return result;
}
std::string unescapeString(const std::string &str) {
std::string result;
bool escaped = false;
for (char c : str) {
if (escaped) {
result += c;
escaped = false;
} else if (c == '\\') {
escaped = true;
} else {
result += c;
}
}
return result;
}
} // anonymous namespace
ConfigManager::ConfigManager() {
// Default values
@ -30,26 +68,157 @@ ConfigManager::ConfigManager() {
sensorEnabled["temp"] = false;
}
std::string ConfigManager::getConfigFilePath() const {
// Use current directory as config path
return "./.temp_monitor.conf";
}
void ConfigManager::load() {
// TODO: Load from file
std::string configPath = getConfigFilePath();
std::ifstream file(configPath);
if (!file.is_open()) {
// No config file, use defaults
return;
}
std::string line;
while (std::getline(file, line)) {
// Skip comments and empty lines
if (line.empty() || line[0] == '#' || line[0] == ';') {
continue;
}
// Parse key=value
size_t eqPos = line.find('=');
if (eqPos == std::string::npos) {
continue;
}
std::string key = line.substr(0, eqPos);
std::string value = line.substr(eqPos + 1);
// Trim whitespace
auto trim = [](std::string &s) {
// Trim from end
while (!s.empty() && std::isspace(s.back())) {
s.pop_back();
}
// Trim from beginning
while (!s.empty() && std::isspace(*s.begin())) {
s.erase(s.begin());
}
};
trim(key);
trim(value);
// Unescape special characters
key = unescapeString(key);
value = unescapeString(value);
// Parse values
try {
if (key == "windowWidth") {
windowWidth = std::stoi(value);
} else if (key == "windowHeight") {
windowHeight = std::stoi(value);
} else if (key == "windowX") {
windowX = std::stoi(value);
} else if (key == "windowY") {
windowY = std::stoi(value);
} else if (key == "hasSavedWindowPosition") {
hasSavedWindowPosition = (value == "1" || value == "true");
} else if (key == "pollingTime") {
pollingTime = std::stoi(value);
} else if (key == "historyLength") {
historyLength = std::stoi(value);
} else if (key == "showPerCoreMonitoring") {
showPerCoreMonitoring = (value == "1" || value == "true");
} else if (key == "gpuMonitoring") {
sensorEnabled["gpu"] = (value == "1" || value == "true");
} else if (key == "color_") {
// Parse color_XXX
if (value.length() >= 7 && value[0] == '#') {
sensorColors[key.substr(6)] = value;
}
} else if (key.substr(0, 6) == "color_" && value.length() >= 7 && value[0] == '#') {
sensorColors[key.substr(6)] = value;
} else if (key.substr(0, 5) == "name_" && !value.empty()) {
sensorNames[key.substr(5)] = value;
} else if (key.substr(0, 8) == "enabled_" && (value == "1" || value == "0")) {
sensorEnabled[key.substr(8)] = (value == "1");
}
} catch (...) {
// Skip invalid values
}
}
file.close();
}
void ConfigManager::save() {
// TODO: Save to file
std::string configPath = getConfigFilePath();
std::cerr << "[DEBUG] Saving config to: " << configPath << std::endl;
// Create directory if it doesn't exist
std::string configDir;
size_t lastSlash = configPath.rfind('/');
if (lastSlash != std::string::npos && lastSlash > 0) {
configDir = configPath.substr(0, lastSlash);
std::cerr << "[DEBUG] Creating directory: " << configDir << std::endl;
if (g_mkdir_with_parents(configDir.c_str(), 0755) != 0) {
std::cerr << "[WARNING] Failed to create config directory: " << configDir << std::endl;
}
}
std::ofstream file(configPath);
if (!file.is_open()) {
std::cerr << "[ERROR] Failed to open config file: " << configPath << std::endl;
return;
}
// Write settings
file << "# Temperature Monitor Configuration\n";
file << "# Generated automatically - do not edit manually\n\n";
file << "windowWidth=" << windowWidth << "\n";
file << "windowHeight=" << windowHeight << "\n";
file << "windowX=" << windowX << "\n";
file << "windowY=" << windowY << "\n";
file << "hasSavedWindowPosition=" << (hasSavedWindowPosition ? "1" : "0") << "\n";
file << "pollingTime=" << pollingTime << "\n";
file << "historyLength=" << historyLength << "\n";
file << "showPerCoreMonitoring=" << (showPerCoreMonitoring ? "1" : "0") << "\n";
file << "gpuMonitoring=" << (sensorEnabled.count("gpu") > 0 && sensorEnabled["gpu"] ? "1" : "0") << "\n";
// Write sensor colors
for (const auto &pair : sensorColors) {
file << "color_" << escapeString(pair.first) << "=" << escapeString(pair.second) << "\n";
}
// Write sensor names
for (const auto &pair : sensorNames) {
file << "name_" << escapeString(pair.first) << "=" << escapeString(pair.second) << "\n";
}
// Write sensor enabled states
for (const auto &pair : sensorEnabled) {
file << "enabled_" << escapeString(pair.first) << "=" << (pair.second ? "1" : "0") << "\n";
}
file.close();
}
std::string ConfigManager::getConfigValue(const std::string &key) const {
// TODO: Implement
(void)key;
return "";
}
bool ConfigManager::isGpuMonitoringEnabled() const {
// Check if enabled_gpu is set in sensorEnabled map
// This follows the pattern used for other sensor settings
auto it = sensorEnabled.find("gpu");
if (it != sensorEnabled.end()) {
return it->second;
}
// Default to enabled if not explicitly configured
return true;
}