Files
dropshell/src/config.cpp
2025-04-23 13:57:28 +12:00

68 lines
1.8 KiB
C++

#include "dropshell.hpp"
#include "init_user_directory.hpp"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "config.hpp"
namespace fs = boost::filesystem;
namespace pt = boost::property_tree;
namespace dropshell {
// Default user directory
static bool config_loaded = false;
bool is_config_loaded() {
return config_loaded;
}
bool get_config_path(std::string &path)
{
// Try ~/.config/dropshell/dropshell.conf
const char* home = std::getenv("HOME");
if (home) {
fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.conf";
path = user_path.string();
return fs::exists(path);
}
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
path = "";
return false;
}
bool load_config() {
std::string config_path;
if (!get_config_path(config_path))
return false;
try {
pt::ptree tree;
pt::read_ini(config_path, tree);
bool config_okay = false;
// Try to read user directory from config
try {
std::string user_dir;
user_dir = tree.get<std::string>("user.directory");
// Update user directory through the new interface
set_user_directory(user_dir);
config_okay = true;
} catch (const pt::ptree_error&) {
std::cerr << "Warning: User directory not set in config" << std::endl;
}
// config loaded okay.
config_loaded = config_okay;
return true;
} catch (const std::exception& e) {
std::cerr << "Warning: Error reading config file: " << e.what() << std::endl;
return true; // Not a critical error
}
}
} // namespace dropshell