From 6da342493b294b0ca2191b7113670d9f1c338d75 Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Wed, 25 Oct 2023 16:11:23 +0200 Subject: [PATCH] Use factorized win_os.hpp --- libmamba/src/core/environment.cpp | 42 ++++++++----------------------- libmamba/src/core/menuinst.cpp | 16 +++++++++--- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/libmamba/src/core/environment.cpp b/libmamba/src/core/environment.cpp index 0adee0c924..db54c6fcb8 100644 --- a/libmamba/src/core/environment.cpp +++ b/libmamba/src/core/environment.cpp @@ -16,6 +16,7 @@ #include "mamba/core/output.hpp" #include "mamba/core/util_os.hpp" +#include "mamba/util/os_win.hpp" #else #include #include @@ -31,35 +32,6 @@ extern "C" namespace mamba::env { -#ifdef _WIN32 - namespace - { - const std::map knownfolders = { - { "programs", FOLDERID_Programs }, { "profile", FOLDERID_Profile }, - { "documents", FOLDERID_Documents }, { "roamingappdata", FOLDERID_RoamingAppData }, - { "programdata", FOLDERID_ProgramData }, { "localappdata", FOLDERID_LocalAppData }, - }; - - auto get_known_folder(const std::string& id) -> fs::u8path - { - wchar_t* localAppData; - HRESULT hres; - - hres = SHGetKnownFolderPath(knownfolders.at(id), KF_FLAG_DONT_VERIFY, nullptr, &localAppData); - - if (FAILED(hres)) - { - throw std::runtime_error("Could not retrieve known folder"); - } - - std::wstring tmp(localAppData); - fs::u8path res(tmp); - CoTaskMemFree(localAppData); - return res; - } - } -#endif - std::optional get(const std::string& key) { #ifdef _WIN32 @@ -311,7 +283,9 @@ namespace mamba::env if (maybe_user_config_dir.empty()) { #ifdef _WIN32 - maybe_user_config_dir = get_known_folder("roamingappdata"); + maybe_user_config_dir = util::get_windows_known_user_folder( + util::WindowsKnowUserFolder::RoamingAppData + ); #else maybe_user_config_dir = home_directory() / ".config"; #endif @@ -325,7 +299,9 @@ namespace mamba::env if (maybe_user_data_dir.empty()) { #ifdef _WIN32 - maybe_user_data_dir = get_known_folder("roamingappdata"); + maybe_user_config_dir = util::get_windows_known_user_folder( + util::WindowsKnowUserFolder::RoamingAppData + ); #else maybe_user_data_dir = home_directory() / ".local" / "share"; #endif @@ -339,7 +315,9 @@ namespace mamba::env if (maybe_user_cache_dir.empty()) { #ifdef _WIN32 - maybe_user_cache_dir = get_known_folder("localappdata"); + maybe_user_config_dir = util::get_windows_known_user_folder( + util::WindowsKnowUserFolder::LocalAppData + ); #else maybe_user_cache_dir = home_directory() / ".cache"; #endif diff --git a/libmamba/src/core/menuinst.cpp b/libmamba/src/core/menuinst.cpp index 946fc0282c..e9b194c0e9 100644 --- a/libmamba/src/core/menuinst.cpp +++ b/libmamba/src/core/menuinst.cpp @@ -9,6 +9,8 @@ #ifdef _WIN32 #include #include + +#include "mamba/util/os_win.hpp" #endif #include "mamba/core/context.hpp" @@ -240,8 +242,12 @@ namespace mamba }; #ifdef _WIN32 - vars["${PERSONALDIR}"] = to_forward_slash(win::get_folder("documents")); - vars["${USERPROFILE}"] = to_forward_slash(win::get_folder("profile")); + vars["${PERSONALDIR}"] = to_forward_slash( + util::get_windows_known_user_folder(util::WindowsKnowUserFolder::Documents) + ); + vars["${USERPROFILE}"] = to_forward_slash( + util::get_windows_known_user_folder(util::WindowsKnowUserFolder::Profile) + ); #endif for (auto& [key, val] : vars) @@ -304,7 +310,11 @@ namespace mamba { cwp_path.string(), target_prefix.string(), env_pyw.string() } ); - fs::u8path target_dir = win::get_folder("programs") / menu_name; + fs::u8path target_dir = util::get_windows_known_user_folder( + util::WindowsKnowUserFolder::Programs + ) + / menu_name; + if (!fs::exists(target_dir)) { fs::create_directories(target_dir);