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
60
61pub fn purge_temp_folders() -> sharing::PisPasResult<()> {
62 tracing::info!("Purging temp folders");
63 let temp_dir = std::env::temp_dir();
64 let excluded_path = sharing::fs::current_executable_folder();
65 for entry in std::fs::read_dir(temp_dir)? {
66 let entry = entry?;
67 let path = entry.path();
68 if path.is_dir() && excluded_path != path {
69 if path.to_str().unwrap().contains("pispas-") {
71 tracing::info!("Deleting temp folder: {}", path.display());
72 std::fs::remove_dir_all(&path)?;
73 }
74 }
75 }
76 Ok(())
77}