From 16392bf7d6465e78d79f9b5aae975b6c2448a043 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 12:36:06 +0100 Subject: [PATCH 01/10] initial implementation --- src-tauri/Cargo.lock | 10 ++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/auto_launcher.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index f533f1303..5aee553cb 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4947,6 +4947,15 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "planif" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7502ee5af341c06aa00593d655d09e4b7126f0e886b80745c218d6dc674e8b21" +dependencies = [ + "windows 0.48.0", +] + [[package]] name = "plist" version = "1.7.0" @@ -6770,6 +6779,7 @@ dependencies = [ "nvml-wrapper", "open", "phraze", + "planif", "rand 0.8.5", "regex", "reqwest", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 22ea1f3fa..5d4c6af87 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -87,6 +87,7 @@ xz2 = { version = "0.1.7", features = ["static"] } # static bind lzma zip = "2.2.0" dirs = "5.0.1" tauri-plugin-process = "2" +planif = "1.0.0" [target.'cfg(windows)'.dependencies] winreg = "0.52.0" diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index e43185327..ecbe5f220 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -26,6 +26,7 @@ use anyhow::anyhow; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; use dunce::canonicalize; use log::{info, warn}; +use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::Settings}; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; @@ -123,6 +124,34 @@ impl AutoLauncher { Ok(()) } + pub async fn create_task_scheduler_for_admin_startup(&self, is_triggered: bool) -> Result<(), anyhow::Error> { + let task_scheduler = TaskScheduler::new()?; + let com_runtime = task_scheduler.get_com()?; + let schedule_builder = ScheduleBuilder::new(&com_runtime)?; + + let app_exe = current_exe()?; + let app_exe = canonicalize(&app_exe)?; + let app_name = app_exe + .file_stem() + .and_then(|file| file.to_str()) + .ok_or(anyhow!("Failed to get file stem"))?; + + let app_path = app_exe + .as_os_str() + .to_str() + .ok_or(anyhow!("Failed to convert path to string"))? + .to_string(); + + + schedule_builder.create_boot() + .author("Tari Universe")? + .trigger("startup_trigger", is_triggered)? + .action(Action::new("startup_action", &app_path, "", "-Verb RunAs"))? + .build()? + .register("Tari Universe startup",TaskCreationFlags::CreateOrUpdate as i32)?; + + } + pub async fn initialize_auto_launcher( &self, is_auto_launcher_enabled: bool, From 3c0d17d7aa82e85f569c1337533aff285a0ff7c8 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 12:39:33 +0100 Subject: [PATCH 02/10] use admin autolaunch --- src-tauri/src/auto_launcher.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index ecbe5f220..b9da96f28 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -124,6 +124,26 @@ impl AutoLauncher { Ok(()) } + async fn toggle_windows_admin_auto_launcher( + &self, + config_is_auto_launcher_enabled: bool, + ) -> Result<(), anyhow::Error> { + let is_auto_launcher_enabled = auto_launcher.is_enabled().unwrap_or(false); + + if config_is_auto_launcher_enabled && !is_auto_launcher_enabled { + info!(target: LOG_TARGET, "Enabling admin auto-launcher"); + self.create_task_scheduler_for_admin_startup(true).await?; + + } + + if !config_is_auto_launcher_enabled && is_auto_launcher_enabled { + info!(target: LOG_TARGET, "Disabling admin auto-launcher"); + self.create_task_scheduler_for_admin_startup(false).await?; + } + + Ok(()) + } + pub async fn create_task_scheduler_for_admin_startup(&self, is_triggered: bool) -> Result<(), anyhow::Error> { let task_scheduler = TaskScheduler::new()?; let com_runtime = task_scheduler.get_com()?; @@ -175,6 +195,8 @@ impl AutoLauncher { let auto_launcher = AutoLauncher::build_auto_launcher(app_name, &app_path)?; AutoLauncher::toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled)?; + #[cfg(target_os = "windows")] + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled).await?; let _ = &self.auto_launcher.write().await.replace(auto_launcher); From 8e8ea97852e0e1fda2a6fac7dbd7b7ed84894fed Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 16:28:31 +0100 Subject: [PATCH 03/10] mvp --- src-tauri/Cargo.lock | 18 ++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/auto_launcher.rs | 53 ++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5aee553cb..f363f45fd 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -6811,6 +6811,7 @@ dependencies = [ "thiserror 1.0.69", "tokio", "tokio-util 0.7.13", + "whoami", "winreg 0.52.0", "xz2", "zip", @@ -8463,6 +8464,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.99" @@ -8664,6 +8671,17 @@ dependencies = [ "rustix", ] +[[package]] +name = "whoami" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d" +dependencies = [ + "redox_syscall", + "wasite", + "web-sys", +] + [[package]] name = "widestring" version = "1.1.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5d4c6af87..d3f110ec4 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -88,6 +88,7 @@ zip = "2.2.0" dirs = "5.0.1" tauri-plugin-process = "2" planif = "1.0.0" +whoami = "1.5.2" [target.'cfg(windows)'.dependencies] winreg = "0.52.0" diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index b9da96f28..ba526ada7 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -20,15 +20,16 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::sync::LazyLock; +use std::{sync::LazyLock, time::Duration}; use anyhow::anyhow; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; use dunce::canonicalize; use log::{info, warn}; -use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::Settings}; +use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::{Compatibility, LogonType, NetworkSettings, PrincipalSettings, RunLevel, Settings}}; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; +use whoami::{username, username_os, realname, realname_os}; const LOG_TARGET: &str = "tari::universe::auto_launcher"; @@ -128,33 +129,32 @@ impl AutoLauncher { &self, config_is_auto_launcher_enabled: bool, ) -> Result<(), anyhow::Error> { - let is_auto_launcher_enabled = auto_launcher.is_enabled().unwrap_or(false); - if config_is_auto_launcher_enabled && !is_auto_launcher_enabled { + if config_is_auto_launcher_enabled { info!(target: LOG_TARGET, "Enabling admin auto-launcher"); - self.create_task_scheduler_for_admin_startup(true).await?; + self.create_task_scheduler_for_admin_startup(true).await.map_err( + |e| anyhow!("Failed to create task scheduler for admin startup: {}", e), + ); } - if !config_is_auto_launcher_enabled && is_auto_launcher_enabled { + if !config_is_auto_launcher_enabled { info!(target: LOG_TARGET, "Disabling admin auto-launcher"); - self.create_task_scheduler_for_admin_startup(false).await?; + self.create_task_scheduler_for_admin_startup(false).await.map_err( + |e| anyhow!("Failed to create task scheduler for admin startup: {}", e), + ); } Ok(()) } - pub async fn create_task_scheduler_for_admin_startup(&self, is_triggered: bool) -> Result<(), anyhow::Error> { + pub async fn create_task_scheduler_for_admin_startup(&self, is_triggered: bool) -> Result<(), Box> { let task_scheduler = TaskScheduler::new()?; - let com_runtime = task_scheduler.get_com()?; + let com_runtime = task_scheduler.get_com(); let schedule_builder = ScheduleBuilder::new(&com_runtime)?; let app_exe = current_exe()?; let app_exe = canonicalize(&app_exe)?; - let app_name = app_exe - .file_stem() - .and_then(|file| file.to_str()) - .ok_or(anyhow!("Failed to get file stem"))?; let app_path = app_exe .as_os_str() @@ -163,13 +163,32 @@ impl AutoLauncher { .to_string(); - schedule_builder.create_boot() + schedule_builder.create_logon() .author("Tari Universe")? .trigger("startup_trigger", is_triggered)? - .action(Action::new("startup_action", &app_path, "", "-Verb RunAs"))? + .action(Action::new("startup_action", &app_path, "", ""))? + .principal(PrincipalSettings { + display_name: "Tari Universe".to_string(), + group_id: None, + user_id: Some(username()), + id: "Tari universe principal".to_string(), + logon_type: LogonType::InteractiveToken, + run_level: RunLevel::Highest, + })? + .settings(Settings { + stop_if_going_on_batteries: Some(false), + start_when_available: Some(true), + run_only_if_network_available: Some(false), + run_only_if_idle: Some(false), + enabled: Some(true), + disallow_start_if_on_batteries: Some(false), + ..Default::default() + })? + .build()? .register("Tari Universe startup",TaskCreationFlags::CreateOrUpdate as i32)?; + Ok(()) } pub async fn initialize_auto_launcher( @@ -220,6 +239,8 @@ impl AutoLauncher { match auto_launcher_ref { Some(auto_launcher) => { AutoLauncher::toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled)?; + #[cfg(target_os = "windows")] + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled).await?; } None => { warn!(target: LOG_TARGET, "Could not get auto-launcher reference"); @@ -233,4 +254,4 @@ impl AutoLauncher { pub fn current() -> &'static AutoLauncher { &INSTANCE } -} +} \ No newline at end of file From 287d9fee68f14ba04093b5a131b798a8eb7f3471 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 16:36:52 +0100 Subject: [PATCH 04/10] ci fixes --- src-tauri/src/auto_launcher.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index ba526ada7..0dada30fc 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -20,16 +20,16 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::{sync::LazyLock, time::Duration}; +use std::sync::LazyLock; use anyhow::anyhow; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; use dunce::canonicalize; use log::{info, warn}; -use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::{Compatibility, LogonType, NetworkSettings, PrincipalSettings, RunLevel, Settings}}; +use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::{LogonType, PrincipalSettings, RunLevel, Settings}}; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; -use whoami::{username, username_os, realname, realname_os}; +use whoami::username; const LOG_TARGET: &str = "tari::universe::auto_launcher"; From 170db94af1c3f1f1e19fb49794056d0803448396 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 16:37:05 +0100 Subject: [PATCH 05/10] cargo fmt --- src-tauri/src/auto_launcher.rs | 46 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 0dada30fc..641a9dbe2 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -26,7 +26,12 @@ use anyhow::anyhow; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; use dunce::canonicalize; use log::{info, warn}; -use planif::{enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::{LogonType, PrincipalSettings, RunLevel, Settings}}; +use planif::{ + enums::TaskCreationFlags, + schedule::TaskScheduler, + schedule_builder::{Action, ScheduleBuilder}, + settings::{LogonType, PrincipalSettings, RunLevel, Settings}, +}; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; use whoami::username; @@ -129,26 +134,27 @@ impl AutoLauncher { &self, config_is_auto_launcher_enabled: bool, ) -> Result<(), anyhow::Error> { - if config_is_auto_launcher_enabled { info!(target: LOG_TARGET, "Enabling admin auto-launcher"); - self.create_task_scheduler_for_admin_startup(true).await.map_err( - |e| anyhow!("Failed to create task scheduler for admin startup: {}", e), - ); - + self.create_task_scheduler_for_admin_startup(true) + .await + .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e)); } if !config_is_auto_launcher_enabled { info!(target: LOG_TARGET, "Disabling admin auto-launcher"); - self.create_task_scheduler_for_admin_startup(false).await.map_err( - |e| anyhow!("Failed to create task scheduler for admin startup: {}", e), - ); + self.create_task_scheduler_for_admin_startup(false) + .await + .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e)); } Ok(()) } - pub async fn create_task_scheduler_for_admin_startup(&self, is_triggered: bool) -> Result<(), Box> { + pub async fn create_task_scheduler_for_admin_startup( + &self, + is_triggered: bool, + ) -> Result<(), Box> { let task_scheduler = TaskScheduler::new()?; let com_runtime = task_scheduler.get_com(); let schedule_builder = ScheduleBuilder::new(&com_runtime)?; @@ -162,8 +168,8 @@ impl AutoLauncher { .ok_or(anyhow!("Failed to convert path to string"))? .to_string(); - - schedule_builder.create_logon() + schedule_builder + .create_logon() .author("Tari Universe")? .trigger("startup_trigger", is_triggered)? .action(Action::new("startup_action", &app_path, "", ""))? @@ -184,11 +190,13 @@ impl AutoLauncher { disallow_start_if_on_batteries: Some(false), ..Default::default() })? - .build()? - .register("Tari Universe startup",TaskCreationFlags::CreateOrUpdate as i32)?; + .register( + "Tari Universe startup", + TaskCreationFlags::CreateOrUpdate as i32, + )?; - Ok(()) + Ok(()) } pub async fn initialize_auto_launcher( @@ -215,7 +223,8 @@ impl AutoLauncher { AutoLauncher::toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled)?; #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled).await?; + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; let _ = &self.auto_launcher.write().await.replace(auto_launcher); @@ -240,7 +249,8 @@ impl AutoLauncher { Some(auto_launcher) => { AutoLauncher::toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled)?; #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled).await?; + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; } None => { warn!(target: LOG_TARGET, "Could not get auto-launcher reference"); @@ -254,4 +264,4 @@ impl AutoLauncher { pub fn current() -> &'static AutoLauncher { &INSTANCE } -} \ No newline at end of file +} From ba5a778a41b3a01d8bd2f1580e243abe35891ea7 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Tue, 10 Dec 2024 16:53:30 +0100 Subject: [PATCH 06/10] fix clippy --- src-tauri/src/auto_launcher.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 641a9dbe2..2c40bc08a 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -138,15 +138,15 @@ impl AutoLauncher { info!(target: LOG_TARGET, "Enabling admin auto-launcher"); self.create_task_scheduler_for_admin_startup(true) .await - .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e)); - } + .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e))?; + }; if !config_is_auto_launcher_enabled { info!(target: LOG_TARGET, "Disabling admin auto-launcher"); self.create_task_scheduler_for_admin_startup(false) .await - .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e)); - } + .map_err(|e| anyhow!("Failed to create task scheduler for admin startup: {}", e))?; + }; Ok(()) } From 6c3b168d17a18fd65ae7b2373d8d6f126a77ea75 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Wed, 11 Dec 2024 09:15:02 +0100 Subject: [PATCH 07/10] fixes after review --- src-tauri/src/auto_launcher.rs | 49 +++++++++++++++------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 2c40bc08a..6a303230b 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -36,16 +36,12 @@ use tauri::utils::platform::current_exe; use tokio::sync::RwLock; use whoami::username; +use crate::utils::platform_utils::{CurrentOperatingSystem, PlatformUtils}; + const LOG_TARGET: &str = "tari::universe::auto_launcher"; static INSTANCE: LazyLock = LazyLock::new(AutoLauncher::new); -pub enum CurrentOperatingSystem { - Windows, - Linux, - MacOS, -} - pub struct AutoLauncher { auto_launcher: RwLock>, } @@ -57,22 +53,10 @@ impl AutoLauncher { } } - fn detect_current_os() -> CurrentOperatingSystem { - if cfg!(target_os = "windows") { - CurrentOperatingSystem::Windows - } else if cfg!(target_os = "linux") { - CurrentOperatingSystem::Linux - } else if cfg!(target_os = "macos") { - CurrentOperatingSystem::MacOS - } else { - panic!("Unsupported OS"); - } - } - fn build_auto_launcher(app_name: &str, app_path: &str) -> Result { info!(target: LOG_TARGET, "Building auto-launcher with app_name: {} and app_path: {}", app_name, app_path); - match AutoLauncher::detect_current_os() { + match PlatformUtils::detect_current_os() { CurrentOperatingSystem::Windows => { return AutoLaunchBuilder::new() .set_app_name(app_name) @@ -100,7 +84,8 @@ impl AutoLauncher { } } - fn toggle_auto_launcher( + async fn toggle_auto_launcher( + &self, auto_launcher: &AutoLaunch, config_is_auto_launcher_enabled: bool, ) -> Result<(), anyhow::Error> { @@ -108,13 +93,19 @@ impl AutoLauncher { if config_is_auto_launcher_enabled && !is_auto_launcher_enabled { info!(target: LOG_TARGET, "Enabling auto-launcher"); - match AutoLauncher::detect_current_os() { + match PlatformUtils::detect_current_os() { CurrentOperatingSystem::MacOS => { // This for some reason fixes the issue where macOS starts two instances of the app // when auto-launcher is enabled and when during shutdown user selects to reopen the apps after restart auto_launcher.disable()?; auto_launcher.enable()?; } + CurrentOperatingSystem::Windows => { + auto_launcher.enable()?; + // To startup application as admin on windows, we need to create a task scheduler + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; + } _ => { auto_launcher.enable()?; } @@ -124,6 +115,14 @@ impl AutoLauncher { if !config_is_auto_launcher_enabled && is_auto_launcher_enabled { info!(target: LOG_TARGET, "Disabling auto-launcher"); + match PlatformUtils::detect_current_os() { + CurrentOperatingSystem::Windows => { + self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + .await?; + auto_launcher.disable()?; + } + _ => {} + } auto_launcher.disable()?; } @@ -221,9 +220,7 @@ impl AutoLauncher { info!(target: LOG_TARGET, "Building auto-launcher with app_name: {} and app_path: {}", app_name, app_path); let auto_launcher = AutoLauncher::build_auto_launcher(app_name, &app_path)?; - AutoLauncher::toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled)?; - #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + self.toggle_auto_launcher(&auto_launcher, is_auto_launcher_enabled) .await?; let _ = &self.auto_launcher.write().await.replace(auto_launcher); @@ -247,9 +244,7 @@ impl AutoLauncher { let auto_launcher_ref = auto_launcher.as_ref(); match auto_launcher_ref { Some(auto_launcher) => { - AutoLauncher::toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled)?; - #[cfg(target_os = "windows")] - self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) + self.toggle_auto_launcher(auto_launcher, is_auto_launcher_enabled) .await?; } None => { From feff64f547b475c3c611041a8c3c16889dd125e6 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Wed, 11 Dec 2024 09:21:20 +0100 Subject: [PATCH 08/10] fix clippy --- src-tauri/src/auto_launcher.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 6a303230b..3dce94ba3 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -121,9 +121,10 @@ impl AutoLauncher { .await?; auto_launcher.disable()?; } - _ => {} + _ => { + auto_launcher.disable()?; + } } - auto_launcher.disable()?; } Ok(()) From 106d0a828fb7bbb068379b96eaa3c3b304b01c2a Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Wed, 11 Dec 2024 09:44:07 +0100 Subject: [PATCH 09/10] limit planif build to windows --- src-tauri/Cargo.toml | 4 ++-- src-tauri/src/auto_launcher.rs | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d3f110ec4..dce3dd98e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -87,10 +87,10 @@ xz2 = { version = "0.1.7", features = ["static"] } # static bind lzma zip = "2.2.0" dirs = "5.0.1" tauri-plugin-process = "2" -planif = "1.0.0" -whoami = "1.5.2" [target.'cfg(windows)'.dependencies] +planif = "1.0.0" +whoami = "1.5.2" winreg = "0.52.0" # needed for keymanager. TODO: Find a way of creating a keymanager without bundling sqlite diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index 3dce94ba3..c8a2aec1c 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -26,15 +26,17 @@ use anyhow::anyhow; use auto_launch::{AutoLaunch, AutoLaunchBuilder}; use dunce::canonicalize; use log::{info, warn}; +#[cfg(target_os = "windows")] use planif::{ enums::TaskCreationFlags, schedule::TaskScheduler, schedule_builder::{Action, ScheduleBuilder}, settings::{LogonType, PrincipalSettings, RunLevel, Settings}, }; +#[cfg(target_os = "windows")] +use whoami::username; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; -use whoami::username; use crate::utils::platform_utils::{CurrentOperatingSystem, PlatformUtils}; @@ -103,6 +105,7 @@ impl AutoLauncher { CurrentOperatingSystem::Windows => { auto_launcher.enable()?; // To startup application as admin on windows, we need to create a task scheduler + #[cfg(target_os = "windows")] self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) .await?; } @@ -117,6 +120,7 @@ impl AutoLauncher { info!(target: LOG_TARGET, "Disabling auto-launcher"); match PlatformUtils::detect_current_os() { CurrentOperatingSystem::Windows => { + #[cfg(target_os = "windows")] self.toggle_windows_admin_auto_launcher(is_auto_launcher_enabled) .await?; auto_launcher.disable()?; @@ -130,6 +134,7 @@ impl AutoLauncher { Ok(()) } + #[cfg(target_os = "windows")] async fn toggle_windows_admin_auto_launcher( &self, config_is_auto_launcher_enabled: bool, @@ -151,6 +156,7 @@ impl AutoLauncher { Ok(()) } + #[cfg(target_os = "windows")] pub async fn create_task_scheduler_for_admin_startup( &self, is_triggered: bool, From 8c12932faf3adb47ffe72107ad34e5197d82fec4 Mon Sep 17 00:00:00 2001 From: Misieq01 Date: Wed, 11 Dec 2024 09:47:05 +0100 Subject: [PATCH 10/10] cargo fmt --- src-tauri/src/auto_launcher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/auto_launcher.rs b/src-tauri/src/auto_launcher.rs index c8a2aec1c..91543e422 100644 --- a/src-tauri/src/auto_launcher.rs +++ b/src-tauri/src/auto_launcher.rs @@ -33,10 +33,10 @@ use planif::{ schedule_builder::{Action, ScheduleBuilder}, settings::{LogonType, PrincipalSettings, RunLevel, Settings}, }; -#[cfg(target_os = "windows")] -use whoami::username; use tauri::utils::platform::current_exe; use tokio::sync::RwLock; +#[cfg(target_os = "windows")] +use whoami::username; use crate::utils::platform_utils::{CurrentOperatingSystem, PlatformUtils};