| use crate::modules::{process, db, device}; |
| use crate::models::Account; |
| use std::fs; |
|
|
| pub trait SystemIntegration: Send + Sync { |
| |
| async fn on_account_switch(&self, account: &crate::models::Account) -> Result<(), String>; |
| |
| |
| fn update_tray(&self); |
| |
| |
| fn show_notification(&self, title: &str, body: &str); |
| } |
|
|
| |
| pub struct DesktopIntegration { |
| pub app_handle: tauri::AppHandle, |
| } |
|
|
| impl SystemIntegration for DesktopIntegration { |
| async fn on_account_switch(&self, account: &crate::models::Account) -> Result<(), String> { |
| crate::modules::logger::log_info(&format!("[Desktop] Executing system switch for: {}", account.email)); |
| |
| |
| let storage_path = device::get_storage_path()?; |
|
|
| |
| if process::is_antigravity_running() { |
| process::close_antigravity(20)?; |
| } |
|
|
| |
| if let Some(ref profile) = account.device_profile { |
| device::write_profile(&storage_path, profile)?; |
| } |
|
|
| |
| let db_path = db::get_db_path()?; |
| if db_path.exists() { |
| let backup_path = db_path.with_extension("vscdb.backup"); |
| let _ = fs::copy(&db_path, &backup_path); |
| } |
| |
| db::inject_token( |
| &db_path, |
| &account.token.access_token, |
| &account.token.refresh_token, |
| account.token.expiry_timestamp, |
| &account.email, |
| account.token.is_gcp_tos, |
| account.token.project_id.as_deref(), |
| )?; |
| |
| |
| if let Some(ref profile) = account.device_profile { |
| let _ = db::write_service_machine_id(&db_path, &profile.mac_machine_id); |
| } |
|
|
| |
| process::start_antigravity()?; |
| |
| |
| let _ = crate::modules::tray::update_tray_menus(&self.app_handle); |
| |
| Ok(()) |
| } |
|
|
| fn update_tray(&self) { |
| let _ = crate::modules::tray::update_tray_menus(&self.app_handle); |
| } |
|
|
| fn show_notification(&self, title: &str, body: &str) { |
| |
| crate::modules::logger::log_info(&format!("[Notification] {}: {}", title, body)); |
| } |
| } |
|
|
| |
| pub struct HeadlessIntegration; |
|
|
| impl SystemIntegration for HeadlessIntegration { |
| async fn on_account_switch(&self, account: &crate::models::Account) -> Result<(), String> { |
| crate::modules::logger::log_info(&format!("[Headless] Account switched in memory: {}", account.email)); |
| |
| |
| Ok(()) |
| } |
|
|
| fn update_tray(&self) { |
| |
| } |
|
|
| fn show_notification(&self, title: &str, body: &str) { |
| crate::modules::logger::log_info(&format!("[Log Notification] {}: {}", title, body)); |
| } |
| } |
| |
| #[derive(Clone)] |
| pub enum SystemManager { |
| Desktop(tauri::AppHandle), |
| Headless, |
| } |
|
|
| impl SystemManager { |
| pub async fn on_account_switch(&self, account: &Account) -> Result<(), String> { |
| match self { |
| SystemManager::Desktop(handle) => { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.on_account_switch(account).await |
| }, |
| SystemManager::Headless => { |
| let integration = HeadlessIntegration; |
| integration.on_account_switch(account).await |
| } |
| } |
| } |
|
|
| pub fn update_tray(&self) { |
| if let SystemManager::Desktop(handle) = self { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.update_tray(); |
| } |
| } |
|
|
| pub fn show_notification(&self, title: &str, body: &str) { |
| match self { |
| SystemManager::Desktop(handle) => { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.show_notification(title, body); |
| }, |
| SystemManager::Headless => { |
| let integration = HeadlessIntegration; |
| integration.show_notification(title, body); |
| } |
| } |
| } |
| } |
|
|
| impl SystemIntegration for SystemManager { |
| async fn on_account_switch(&self, account: &crate::models::Account) -> Result<(), String> { |
| match self { |
| SystemManager::Desktop(handle) => { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.on_account_switch(account).await |
| }, |
| SystemManager::Headless => { |
| let integration = HeadlessIntegration; |
| integration.on_account_switch(account).await |
| } |
| } |
| } |
|
|
| fn update_tray(&self) { |
| match self { |
| SystemManager::Desktop(handle) => { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.update_tray(); |
| }, |
| SystemManager::Headless => { |
| let integration = HeadlessIntegration; |
| integration.update_tray(); |
| } |
| } |
| } |
|
|
| fn show_notification(&self, title: &str, body: &str) { |
| match self { |
| SystemManager::Desktop(handle) => { |
| let integration = DesktopIntegration { app_handle: handle.clone() }; |
| integration.show_notification(title, body); |
| }, |
| SystemManager::Headless => { |
| let integration = HeadlessIntegration; |
| integration.show_notification(title, body); |
| } |
| } |
| } |
| } |
|
|