This commit is contained in:
Your Name
2025-04-21 11:19:05 +12:00
parent 8c85fe8819
commit 10d663971c
20 changed files with 839 additions and 280 deletions

124
src/config.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "dropshell.hpp"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
namespace fs = boost::filesystem;
namespace pt = boost::property_tree;
namespace dropshell {
// Default user directory
static std::string 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";
if (!fs::exists(user_path)) {
// create path
fs::create_directories(user_path.parent_path());
}
path = user_path.string();
return true;
}
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
path = "";
return false;
}
bool get_user_directory(std::string& path) {
path = user_directory;
return !path.empty();
}
bool load_config() {
std::string config_path;
if (!get_config_path(config_path))
return true;
if (config_path.empty()) {
// No config file found, but this is not an error
return true;
}
try {
pt::ptree tree;
pt::read_ini(config_path, tree);
bool config_okay = true;
// Try to read user directory from config
try {
user_directory = tree.get<std::string>("user.directory");
} catch (const pt::ptree_error&) {
std::cerr << "Warning: User directory not set in config" << std::endl;
config_okay = false; // Not a critical error
}
// 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
}
}
void init_user_directory(const std::string& path) {
// Convert to absolute path
fs::path abs_path = fs::absolute(path);
// Create directory if it doesn't exist
if (!fs::exists(abs_path)) {
throw std::runtime_error("The user directory does not exist: " + abs_path.string());
}
// Update config file
std::string config_path;
if (!get_config_path(config_path)) {
// No config file exists, create one in user's home directory
const char* home = std::getenv("HOME");
if (!home) {
throw std::runtime_error("HOME environment variable not set");
}
fs::path config_dir = fs::path(home) / ".config" / "dropshell";
if (!fs::exists(config_dir)) {
fs::create_directories(config_dir);
}
config_path = (config_dir / "dropshell.conf").string();
}
try {
pt::ptree tree;
// Read existing config if it exists
if (fs::exists(config_path)) {
pt::read_ini(config_path, tree);
}
// Update user directory
tree.put("user.directory", abs_path.string());
// Write back to config file
pt::write_ini(config_path, tree);
// Update in-memory value
user_directory = abs_path.string();
std::cout << "User directory initialized to: " << abs_path.string() << std::endl;
} catch (const std::exception& e) {
throw std::runtime_error("Failed to update config: " + std::string(e.what()));
}
}
} // namespace dropshell