pispas_elevator/
win.rs

1use std::path::PathBuf;
2use std::thread::sleep;
3use easy_trace::instruments::tracing;
4
5pub fn tray_remove_files(path: &PathBuf) {
6    for tray in 0..5 {
7        let removed = sharing::fs::remove_files(path);
8        if removed.is_ok() {
9            break;
10        }
11        sleep(std::time::Duration::from_millis(1000));
12        if tray == 9 && removed.is_err() {
13            tracing::info!("Error removing files => {:?} error {:?}", path, removed);
14        }
15    }
16}
17
18pub fn remove_linked_files() -> Result<(), Box<dyn std::error::Error>> {
19    #[cfg(target_os = "windows")]
20    {
21        let path_icon = sharing::paths::icon_desktop().display().to_string();
22        match std::fs::remove_file(path_icon.as_str()) {
23            Ok(_) => tracing::error!("Icon removed"),
24            Err(e) => tracing::error!("Error removing icon: {} path {:?}", e, path_icon.as_str()),
25        }
26    }
27
28    if let Err(e) = purge_temp_folders() {
29        tracing::error!("Error purging temp folders: {}", e);
30    }
31
32    #[cfg(target_os = "windows")]
33    {
34        tray_remove_files(&sharing::paths::get_install_dir());
35        tray_remove_files(&sharing::paths::HOME_DIR);
36    }
37
38    #[cfg(target_os = "windows")]
39    match sharing::remove_registry_entries() {
40        Ok(_) => tracing::info!("Registry entries removed"),
41        Err(e) => tracing::error!("Error removing registry entries: {}", e),
42    }
43
44    Ok(())
45}
46
47pub fn link_to_desktop() -> Result<(), Box<dyn std::error::Error>> {
48    let path_icon = sharing::paths::icon_desktop().display().to_string();
49    let path_icon_bin = sharing::paths::tray_icon_path().display().to_string();
50    tracing::info!("link_to_desktop : {:?} from {:?}", path_icon, path_icon_bin);
51    match std::os::windows::fs::symlink_file(path_icon_bin.as_str(), path_icon.as_str()) {
52        Ok(_) => tracing::info!("Link created"),
53        Err(e) => tracing::error!("Error creating link: {}", e),
54    }
55    sleep(std::time::Duration::from_millis(100));
56
57    Ok(())
58}
59
60pub fn launch_tray_icon() -> Result<(), Box<dyn std::error::Error>> {
61    #[cfg(target_os = "windows")]
62    {
63        let tray_string = sharing::paths::tray_icon_path().display().to_string();
64
65        if let Err(err) = sharing::natives::api::run_in_all_sessions(&tray_string, "",true, false) {
66            tracing::error!("Error launching tray icon: {} for all users", err);
67        }
68
69        sleep(std::time::Duration::from_secs(1));
70    }
71    Ok(())
72}
73
74pub fn purge_temp_folders() -> sharing::PisPasResult<()> {
75    tracing::info!("Purging temp folders");
76    let temp_dir = std::env::temp_dir();
77    let excluded_path = sharing::fs::current_executable_folder();
78    for entry in std::fs::read_dir(temp_dir)? {
79        let entry = entry?;
80        let path = entry.path();
81        if path.is_dir() && excluded_path != path {
82            // if path contains our pattern delete it
83            if path.to_str().unwrap().contains("pispas-") {
84                tracing::info!("Deleting temp folder: {}", path.display());
85                std::fs::remove_dir_all(&path)?;
86            }
87        }
88    }
89    Ok(())
90}