From e20ca7a8f023ca330f1dd409a5f13aef48b0daf2 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 17 Mar 2023 08:59:13 -0400 Subject: [PATCH] tray-icon: execute as user (#1046) --- src/system_tray.cpp | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/system_tray.cpp b/src/system_tray.cpp index a7eaab87..12247c4a 100644 --- a/src/system_tray.cpp +++ b/src/system_tray.cpp @@ -6,7 +6,6 @@ #if defined(_WIN32) || defined(_WIN64) #define TRAY_ICON WEB_DIR "images/favicon.ico" -#include #elif defined(__linux__) || defined(linux) || defined(__linux) #define TRAY_ICON "sunshine" #elif defined(__APPLE__) || defined(__MACH__) @@ -20,14 +19,14 @@ // lib includes #include "tray/tray.h" +#include +#include // local includes #include "confighttp.h" #include "main.h" - -// local includes -//#include "platform/common.h" -//#include "process.h" +#include "platform/common.h" +#include "process.h" using namespace std::literals; @@ -39,12 +38,32 @@ namespace system_tray { * @param url The url to open. */ void open_url(const std::string &url) { -// if windows + boost::filesystem::path working_dir; + + // if windows #if defined(_WIN32) || defined(_WIN64) - ShellExecuteA(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + // set working dir to Windows system directory + working_dir = boost::filesystem::path(std::getenv("SystemRoot")); + + // this isn't ideal as it briefly shows a command window + // but start a command built into cmd, not an executable + std::string cmd = R"(cmd /C "start )" + url + R"(")"; #else // if unix - system(("open "s + url).c_str()); + // set working dir to user home directory + working_dir = boost::filesystem::path(std::getenv("HOME")); + std::string cmd = R"(open ")" + url + R"(")"; #endif + + boost::process::environment _env = boost::this_process::environment(); + std::error_code ec; + auto child = platf::run_unprivileged(cmd, working_dir, _env, nullptr, ec, nullptr); + if(ec) { + BOOST_LOG(warning) << "Couldn't open url ["sv << url << "]: System: "sv << ec.message(); + } + else { + BOOST_LOG(info) << "Opened url ["sv << url << "]"sv; + child.detach(); + } } /**