[ui] Add auto-mount on first launch functionality #52

This commit is contained in:
2025-09-06 18:19:41 -05:00
parent f4a7e0e187
commit f473d5c855
8 changed files with 131 additions and 100 deletions

View File

@@ -51,7 +51,9 @@ template <typename thread_t>
#endif // defined(__APPLE__)
#if defined(__linux__)
[[nodiscard]] auto create_autostart_entry(create_autostart_opts opts) -> bool;
[[nodiscard]] auto create_autostart_entry(create_autostart_opts opts,
bool overwrite_existing = true)
-> bool;
#endif // defined(__linux__)
[[nodiscard]] auto get_last_error_code() -> int;

View File

@@ -42,12 +42,16 @@ void free_console();
[[nodiscard]] auto run_process_elevated(std::vector<const char *> args) -> int;
struct shortcut_cfg final {
std::wstring arguments;
std::wstring exe_path;
std::wstring location{get_startup_folder()};
std::wstring shortcut_name;
std::wstring working_directory;
};
[[nodiscard]]
auto create_shortcut(const std::wstring &exe_path,
const std::wstring &arguments,
const std::wstring &working_directory,
const std::wstring &shortcut_name = L"",
const std::wstring &location = get_startup_folder())
auto create_shortcut(const shortcut_cfg &cfg, bool overwrite_existing = true)
-> bool;
[[nodiscard]] auto

View File

@@ -180,11 +180,12 @@ auto convert_to_uint64(const pthread_t &thread) -> std::uint64_t {
#endif // !defined(__APPLE__)
#if defined(__linux__)
auto create_autostart_entry(create_autostart_opts opts) -> bool {
auto create_autostart_entry(create_autostart_opts opts, bool overwrite_existing)
-> bool {
REPERTORY_USES_FUNCTION_NAME();
auto file = desktop_file_path_for(opts.app_name);
if (utils::file::file{file}.exists()) {
if (utils::file::file{file}.exists() && not overwrite_existing) {
return true;
}
@@ -248,8 +249,6 @@ auto create_autostart_entry(create_autostart_opts opts) -> bool {
chmod(file.c_str(), 0644);
#endif // defined(__linux__) || defined(__APPLE__)
utils::error::handle_info(
function_name, fmt::format("created auto-start entry|path|{}", file));
return true;
}
#endif // defined(__linux__)
@@ -283,13 +282,7 @@ auto remove_autostart_entry(std::string_view name) -> bool {
return true;
}
auto ret = utils::file::file{file}.remove();
if (ret) {
utils::error::handle_info(
function_name, fmt::format("removed auto-start entry|path|{}", file));
}
return ret;
return utils::file::file{file}.remove();
}
#endif // defined(__linux__)

View File

@@ -156,11 +156,7 @@ auto get_startup_folder() -> std::wstring {
return str;
}
auto create_shortcut(const std::wstring &exe_path,
const std::wstring &arguments,
const std::wstring &working_directory,
const std::wstring &shortcut_name,
const std::wstring &location) -> bool {
auto create_shortcut(const shortcut_cfg &cfg, bool overwrite_existing) -> bool {
REPERTORY_USES_FUNCTION_NAME();
const auto hr_hex = [](HRESULT hr) -> std::string {
@@ -170,26 +166,29 @@ auto create_shortcut(const std::wstring &exe_path,
return oss.str();
};
if (location.empty()) {
utils::error::handle_error(function_name, "Shortcut location was empty.");
if (cfg.location.empty()) {
utils::error::handle_error(function_name, "shortcut location was empty");
return false;
}
{
std::error_code ec_mk;
std::filesystem::create_directories(std::filesystem::path{location}, ec_mk);
if (not utils::file::directory{cfg.location}.create_directory()) {
utils::error::handle_error(function_name,
"failed to create shortcut directory|path|" +
utils::string::to_utf8(cfg.location));
return false;
}
std::filesystem::path exe_p{exe_path};
std::wstring final_name = shortcut_name.empty()
? (exe_p.stem().wstring() + L".lnk")
: shortcut_name;
auto final_name = cfg.shortcut_name.empty()
? utils::path::strip_to_file_name(cfg.exe_path)
: cfg.shortcut_name;
if (not final_name.ends_with(L".lnk")) {
final_name += L".lnk";
}
const std::filesystem::path lnk_path =
std::filesystem::path{location} / final_name;
auto lnk_path = utils::path::combine(cfg.location, {final_name});
if (utils::file::file{lnk_path}.exists() && not overwrite_existing) {
return true;
}
IShellLinkW *psl{nullptr};
HRESULT result = ::CoCreateInstance(CLSID_ShellLink, nullptr,
@@ -202,7 +201,7 @@ auto create_shortcut(const std::wstring &exe_path,
return false;
}
result = psl->SetPath(exe_path.c_str());
result = psl->SetPath(cfg.exe_path.c_str());
if (FAILED(result)) {
utils::error::handle_error(function_name,
std::string("IShellLink::SetPath failed: ") +
@@ -211,8 +210,8 @@ auto create_shortcut(const std::wstring &exe_path,
return false;
}
if (not arguments.empty()) {
result = psl->SetArguments(arguments.c_str());
if (not cfg.arguments.empty()) {
result = psl->SetArguments(cfg.arguments.c_str());
if (FAILED(result)) {
utils::error::handle_error(
function_name,
@@ -222,8 +221,8 @@ auto create_shortcut(const std::wstring &exe_path,
}
}
if (not working_directory.empty()) {
result = psl->SetWorkingDirectory(working_directory.c_str());
if (not cfg.working_directory.empty()) {
result = psl->SetWorkingDirectory(cfg.working_directory.c_str());
if (FAILED(result)) {
utils::error::handle_error(
function_name,
@@ -243,10 +242,10 @@ auto create_shortcut(const std::wstring &exe_path,
return false;
}
// Best-effort overwrite
{
std::error_code ec;
std::filesystem::remove(lnk_path, ec);
if (not utils::file::file{lnk_path}.remove()) {
utils::error::handle_error(
function_name, "failed to remove existing shortcut|path|" + lnk_path);
return false;
}
IPersistFile *ppf{nullptr};