This commit is contained in:
Your Name
2025-04-21 12:22:03 +12:00
parent c220fca691
commit 34e2bd238c
10 changed files with 184 additions and 79 deletions

View File

@ -1,4 +1,5 @@
#include "dropshell.hpp"
#include "init_user_directory.hpp"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
@ -11,7 +12,6 @@ namespace pt = boost::property_tree;
namespace dropshell {
// Default user directory
static std::string user_directory;
static bool config_loaded = false;
bool is_config_loaded() {
@ -36,11 +36,6 @@ bool get_config_path(std::string& 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))
@ -54,14 +49,18 @@ bool load_config() {
try {
pt::ptree tree;
pt::read_ini(config_path, tree);
bool config_okay = true;
bool config_okay = false;
// Try to read user directory from config
try {
user_directory = tree.get<std::string>("user.directory");
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_okay = false; // Not a critical error
}
// config loaded okay.
@ -74,57 +73,4 @@ bool load_config() {
}
}
void init_user_directory(const std::string& path) {
// Convert to absolute path
fs::path abs_path = fs::absolute(path);
// The directory must exist
if (!fs::exists(abs_path)) {
throw std::runtime_error("The user directory does not exist: " + abs_path.string());
}
// create the servers subdirectory if it doesn't exist
fs::path servers_dir = abs_path / "servers";
if (!fs::exists(servers_dir)) {
fs::create_directories(servers_dir);
}
// 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