From f7d187f5b291667ae519207e567c34140897f57e Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Wed, 4 Nov 2020 23:24:26 -0600 Subject: [PATCH] Use XDG-compliant configuration directory (APPDATA on Windows) --- ProfileManager.cpp | 12 +++++++----- ProfileManager.h | 4 +++- ResourceManager.cpp | 38 ++++++++++++++++++++++++++++++++++++-- ResourceManager.h | 4 +++- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/ProfileManager.cpp b/ProfileManager.cpp index 457f8b17..184ddc0c 100644 --- a/ProfileManager.cpp +++ b/ProfileManager.cpp @@ -1,4 +1,5 @@ #include "ProfileManager.h" +#include "ResourceManager.h" #include "RGBController_Dummy.h" #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING #include @@ -8,8 +9,9 @@ namespace fs = std::experimental::filesystem; -ProfileManager::ProfileManager(std::vector& control) : controllers(control) +ProfileManager::ProfileManager(std::vector& control, std::string config_dir) : controllers(control) { + configuration_directory = config_dir; UpdateProfileList(); } @@ -28,7 +30,7 @@ bool ProfileManager::SaveProfile(std::string profile_name) /*---------------------------------------------------------*\ | Open an output file in binary mode | \*---------------------------------------------------------*/ - std::ofstream controller_file(profile_name, std::ios::out | std::ios::binary); + std::ofstream controller_file(configuration_directory + profile_name, std::ios::out | std::ios::binary); /*---------------------------------------------------------*\ | Write header | @@ -90,7 +92,7 @@ std::vector ProfileManager::LoadProfileToList unsigned int controller_offset = 0; bool ret_val = false; - std::string filename = profile_name; + std::string filename = configuration_directory + profile_name; /*---------------------------------------------------------*\ | Open input file in binary mode | @@ -259,7 +261,7 @@ bool ProfileManager::LoadProfileWithOptions std::vector temp_controller_used; bool ret_val = false; - std::string filename = profile_name; + std::string filename = configuration_directory + profile_name; /*---------------------------------------------------------*\ | Open input file in binary mode | @@ -313,7 +315,7 @@ void ProfileManager::UpdateProfileList() /*---------------------------------------------------------*\ | Load profiles by looking for .orp files in current dir | \*---------------------------------------------------------*/ - for(const auto & entry : fs::directory_iterator(".")) + for(const auto & entry : fs::directory_iterator(configuration_directory)) { std::string filename = entry.path().filename().string(); diff --git a/ProfileManager.h b/ProfileManager.h index 96b0c4f7..3c00c316 100644 --- a/ProfileManager.h +++ b/ProfileManager.h @@ -5,7 +5,7 @@ class ProfileManager { public: - ProfileManager(std::vector& control); + ProfileManager(std::vector& control, std::string config_dir); ~ProfileManager(); bool SaveProfile(std::string profile_name); @@ -33,6 +33,8 @@ protected: std::vector& controllers; private: + std::string configuration_directory; + void UpdateProfileList(); bool LoadProfileWithOptions ( diff --git a/ResourceManager.cpp b/ResourceManager.cpp index 6516160e..d98e20e3 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -12,6 +12,7 @@ #include "ResourceManager.h" #include "ProfileManager.h" +#include #include std::unique_ptr ResourceManager::instance; @@ -46,14 +47,14 @@ ResourceManager::ResourceManager() /*-------------------------------------------------------------------------*\ | Load sizes list from file | \*-------------------------------------------------------------------------*/ - profile_manager = new ProfileManager(rgb_controllers); + profile_manager = new ProfileManager(rgb_controllers, GetConfigurationDirectory()); rgb_controllers_sizes = profile_manager->LoadProfileToList("sizes.ors"); /*-------------------------------------------------------------------------*\ | Load settings from file | \*-------------------------------------------------------------------------*/ settings_manager = new SettingsManager(); - settings_manager->LoadSettings("OpenRGB.json"); + settings_manager->LoadSettings(GetConfigurationDirectory() + "OpenRGB.json"); } ResourceManager::~ResourceManager() @@ -184,6 +185,39 @@ void ResourceManager::DetectionProgressChanged() DetectionProgressMutex.unlock(); } +std::string ResourceManager::GetConfigurationDirectory() +{ + std::string config_dir = ""; + const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); + const char* appdata = getenv("APPDATA"); + + /*-----------------------------------------------------*\ + | Check both XDG_CONFIG_HOME and APPDATA environment | + | variables. If neither exist, use current directory | + \*-----------------------------------------------------*/ + if(xdg_config_home != NULL) + { + config_dir = xdg_config_home; + } + else + { + if(appdata != NULL) + { + config_dir = appdata; + } + } + + /*-----------------------------------------------------*\ + | If a configuration directory was found, append OpenRGB| + \*-----------------------------------------------------*/ + if(config_dir != "") + { + config_dir = config_dir + "/OpenRGB/"; + } + + return(config_dir); +} + NetworkServer* ResourceManager::GetServer() { return(server); diff --git a/ResourceManager.h b/ResourceManager.h index 5bcfe458..fd007b23 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -54,7 +54,9 @@ public: unsigned int GetDetectionPercent(); const char* GetDetectionString(); - + + std::string GetConfigurationDirectory(); + std::vector& GetClients(); NetworkServer* GetServer();