applications 0.3.1

A cross-platform library for finding installed applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::common::{App, SearchPath};
use crate::utils::image::{RustImage, RustImageData};
use crate::AppTrait;
use anyhow::Ok;
use parselnk::string_data;
use parselnk::Lnk;
use std::path::{Path, PathBuf};
use windows_icons::get_icon_by_path;
// use walkdir::WalskDir;
use anyhow::Result;
use lnk::ShellLink;
use serde_derive::Deserialize;
use serde_derive::Serialize;
// use std::ffi::OsString;
// use std::os::windows::ffi::OsStringExt;
use std::process::Command;
use walkdir::WalkDir;
// use winapi::um::winuser::{GetForegroundWindow, GetWindowTextLengthW, GetWindowTextW};
use image;
use std::collections::HashSet;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PowerShellLnkParseResult {
    #[serde(rename = "IconLocation")]
    pub icon_location: String,
    #[serde(rename = "Description")]
    pub description: String,
    #[serde(rename = "WorkingDirectory")]
    pub working_directory: String,
    #[serde(rename = "Arguments")]
    pub arguments: String,
    #[serde(rename = "Hotkey")]
    pub hotkey: String,
    #[serde(rename = "WindowStyle")]
    pub window_style: i64,
    #[serde(rename = "TargetPath")]
    pub target_path: String,
}

// fn run_powershell_script(script: &str) -> Result<String> {
//     let output = Command::new("powershell")
//         .arg("-Command")
//         .arg(script)
//         .output()?;
//     let output = String::from_utf8(output.stdout)?;
//     Ok(output)
// }

pub fn parse_lnk_with_powershell_1(lnk_path: PathBuf) -> anyhow::Result<PowerShellLnkParseResult> {
    let lnk_path = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Docker Desktop.lnk";

    let script = format!(
        r#"
        function Get-Shortcut {{
            param (
                [string]$Path
            )
            
            $shell = New-Object -ComObject WScript.Shell
            $shortcut = $shell.CreateShortcut($Path)
            
            $properties = @{{
                TargetPath = $shortcut.TargetPath
                Arguments  = $shortcut.Arguments
                Description = $shortcut.Description
                Hotkey = $shortcut.Hotkey
                IconLocation = $shortcut.IconLocation
                WindowStyle = $shortcut.WindowStyle
                WorkingDirectory = $shortcut.WorkingDirectory
            }}
            
            return [PSCustomObject]$properties
        }}

        Get-Shortcut -Path "{}" | ConvertTo-Json
    "#,
        lnk_path
    );

    let output = Command::new("powershell")
        .arg("-Command")
        .arg(script)
        .output()
        .unwrap();
    let output = String::from_utf8(output.stdout).unwrap();
    // let result: PowerShellLnkParseResult = serde_json::from_str(&output).unwrap();

    let json: PowerShellLnkParseResult = serde_json::from_str(&output.to_string())?;
    Ok(json)
}

pub fn parse_lnk_with_powershell_2(lnk_path: PathBuf) -> anyhow::Result<App> {
    let parsed_json = parse_lnk_with_powershell_1(lnk_path)?;
    let target_path = PathBuf::from(parsed_json.target_path);
    let desktop_path = if parsed_json.working_directory.len() == 0 {
        PathBuf::from(parsed_json.working_directory)
    } else {
        target_path.parent().unwrap().to_path_buf()
    };
    let icon_path = if parsed_json.icon_location.len() == 0 {
        None
    } else {
        Some(PathBuf::from(parsed_json.icon_location))
    };
    let name = if parsed_json.description.len() == 0 {
        target_path
            .parent()
            .unwrap()
            .file_stem()
            .unwrap()
            .to_str()
            .unwrap()
            .to_string()
    } else {
        let desc = parsed_json.description.clone();
        if desc.starts_with("Runs ") {
            // edge case for Tauri apps
            desc[5..].to_string()
        } else {
            desc
        }
    };
    let app = App {
        name: name,
        icon_path: icon_path,
        app_path_exe: Some(target_path),
        app_desktop_path: desktop_path,
    };
    Ok(app)
}

fn parse_lnk(path: PathBuf) -> Option<App> {
    let shortcut = ShellLink::open(&path).unwrap();
    let exe: Option<PathBuf> = match shortcut.link_info() {
        Some(info) => match info.local_base_path() {
            Some(path) => Some(PathBuf::from(path)),
            None => None,
        },
        None => None,
    };
    let work_dir = match shortcut.working_dir() {
        Some(dir) => PathBuf::from(dir),
        None => {
            // if exe is not None, use the exe's parent directory
            match &exe {
                Some(exe) => exe.parent().unwrap().to_path_buf(),
                None => return None,
            }
        }
    };
    let icon_path: Option<PathBuf> = shortcut.icon_location().as_ref().map(PathBuf::from);

    Some(App {
        name: path.file_stem().unwrap().to_str().unwrap().to_string(),
        icon_path,
        app_path_exe: exe,
        app_desktop_path: work_dir,
    })
}

/// Windows have path like this "%windir%\\system32\\mstsc.exe"
/// This function will translate the path to the real path
fn translate_path_alias(path: PathBuf) -> PathBuf {
    let mut path_str = path.to_string_lossy().to_string().to_lowercase();

    // Common Windows environment variables
    let env_vars = vec![
        "%windir%",
        "%systemroot%",
        "%programfiles%",
        "%programfiles(x86)%",
        "%programdata%",
        "%userprofile%",
        "%appdata%",
        "%localappdata%",
        "%public%",
        "%systemdrive%",
    ];

    for var in env_vars {
        if path_str.starts_with(var) {
            let env_name = var.trim_matches('%').to_uppercase();
            if let std::result::Result::Ok(value) = std::env::var(env_name) {
                path_str = path_str.replace(var, &value);
                return PathBuf::from(path_str);
            }
        }
    }

    path
}

fn strip_extended_prefix(path: PathBuf) -> PathBuf {
    let path_str = path.to_string_lossy();
    if path_str.starts_with("\\\\?\\") {
        PathBuf::from(&path_str[4..])
    } else {
        path
    }
}

fn parse_lnk2(path: PathBuf) -> Option<App> {
    let Some(lnk) = Lnk::try_from(path.as_path()).ok() else {
        log::debug!("Failed to parse lnk with Lnk::try_from: {:?}", path);
        return None;
    };

    let icon = lnk.string_data.icon_location.clone().map(|icon| {
        if icon.to_string_lossy().starts_with("%") {
            translate_path_alias(PathBuf::from(icon))
        } else {
            icon
        }
    });
    let mut app_exe_path: Option<PathBuf> = match lnk.link_info.local_base_path {
        Some(path) => Some(PathBuf::from(path)),
        None => lnk.string_data.relative_path.clone(),
    };
    if app_exe_path.is_none() {
        app_exe_path = lnk.string_data.relative_path.clone();
    }

    if app_exe_path.is_none() {
        if let Some(icon_path) = icon.clone() {
            // Clone here before using
            let icon_path = PathBuf::from(icon_path);
            // if icon_path ends with .exe, then it is the app_exe_path

            if let Some(ext) = icon_path.extension() {
                if ext == "exe" {
                    app_exe_path = Some(translate_path_alias(icon_path));
                } else {
                    log::debug!("");
                    return None;
                }
            }
        }
    }
    let Some(app_exe_path) = app_exe_path else {
        log::debug!("app_exe_path is None");
        return None;
    };
    let app_exe_path = translate_path_alias(app_exe_path);
    let exe_abs_path = match app_exe_path.exists() {
        true => app_exe_path,
        false => path.parent().unwrap().join(&app_exe_path),
    };
    log::debug!("exe_abs_path: {:?}", exe_abs_path);
    if !exe_abs_path.exists() {
        log::warn!("exe_abs_path does not exist: {:?}", exe_abs_path);
        return None;
    }

    let exe_abs_path = std::fs::canonicalize(exe_abs_path);
    let exe_path = if exe_abs_path.is_ok() {
        strip_extended_prefix(exe_abs_path.unwrap())
    } else {
        log::debug!("exe_abs_path IS NOT ok: {:?}", path);
        log::error!(
            "Failed to canonicalize path for {:?}: {:?}",
            path,
            exe_abs_path.err().unwrap()
        );
        return None;
    };

    let work_dir = lnk.string_data.working_dir;
    let work_dir = match work_dir {
        Some(dir) => {
            if dir.to_string_lossy().starts_with("%") {
                translate_path_alias(PathBuf::from(dir))
            } else {
                dir
            }
        }
        None => exe_path.parent().unwrap().to_path_buf(),
    };

    let name = path.file_stem().unwrap().to_str().unwrap().to_string();
    Some(App {
        name,
        icon_path: icon,
        app_path_exe: Some(exe_path),
        app_desktop_path: work_dir,
    })
}

pub fn open_file_with(file_path: PathBuf, app: App) {
    let mut command = Command::new(app.app_path_exe.unwrap());
    command.arg(file_path);
    command
        .spawn()
        .expect("Failed to open file with the specified application.");
}

pub fn get_frontmost_application() -> Result<App> {
    todo!();
    // unsafe {
    //     let hwnd = GetForegroundWindow();
    //     let mut buffer = vec![0u16; GetWindowTextLengthW(hwnd) as usize + 1];
    //     let len = GetWindowTextW(hwnd, buffer.as_mut_ptr(), buffer.len() as i32);
    //     if len > 0 {
    //         let window_title = OsString::from_wide(&buffer[..len as usize]);
    //         let app = App {
    //             name: window_title.to_string_lossy().into_owned(),
    //             icon_path: None,
    //             app_path_exe: None,
    //             app_desktop_path: None,
    //         };
    //         Ok(app)
    //     } else {
    //         Err(anyhow::anyhow!("Failed to get frontmost application."))
    //     }
    // }
}

pub fn get_default_search_paths() -> Vec<SearchPath> {
    let mut search_paths = vec![];
    let appdata_path = format!(
        "{}\\Microsoft\\Windows\\Start Menu\\Programs",
        std::env::var("APPDATA").unwrap()
    );
    let default_paths = vec![
        "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs",
        &appdata_path,
    ];
    for path in default_paths {
        search_paths.push(SearchPath::new(PathBuf::from(path), u8::MAX));
    }
    search_paths
}

pub fn get_all_apps(extra_search_paths: &Vec<SearchPath>) -> Result<Vec<App>> {
    // Create a HashSet of search paths starting with the default Windows paths
    let mut search_paths: HashSet<SearchPath> = HashSet::new();

    // Add default Windows paths with unlimited depth
    let default_paths = get_default_search_paths();
    for path in default_paths {
        search_paths.insert(path);
    }

    // Add extra search paths
    for path in extra_search_paths {
        search_paths.insert(path.clone());
    }

    let mut apps = vec![];
    for search_path in search_paths {
        if !search_path.path.exists() {
            continue;
        }

        for entry in WalkDir::new(search_path.path)
            .max_depth(search_path.depth as usize)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();
            if path.is_file() {
                if let Some(extension) = path.extension() {
                    if extension == "lnk" {
                        log::debug!("Found lnk: {:?}", path);
                        let result = App::from_path(&path);
                        if let Some(app) = result.ok() {
                            log::debug!("Added app: {:?}", app);
                            apps.push(app);
                        } else {
                            log::debug!("Failed to create App from path: {:?}", path);
                        }
                    }
                }
            }
        }
    }
    Ok(apps)
}

pub fn get_running_apps() -> Vec<App> {
    vec![]
}

impl AppTrait for App {
    fn load_icon(&self) -> Result<RustImageData> {
        let icon_path = match &self.icon_path {
            Some(path) => Some(path.clone()),
            None => self.app_path_exe.clone(),
        };
        match icon_path {
            Some(path) => {
                let icon_path_str = path.to_string_lossy();
                let icon = get_icon_by_path(&icon_path_str)
                    .map_err(|e| anyhow::anyhow!("Failed to get icon: {}", e))?;
                Ok(RustImageData::from_dynamic_image(
                    image::DynamicImage::ImageRgba8(icon),
                ))
            }
            None => Err(anyhow::anyhow!("No icon path found for the app")),
        }
    }

    fn from_path(path: &Path) -> Result<Self> {
        if let Some(extension) = path.extension() {
            if extension == "lnk" {
                if let Some(app) = parse_lnk2(path.to_path_buf()) {
                    return Ok(app);
                } else {
                    log::debug!("Failed to parse lnk: {:?}", path);
                }
            }
        }
        Err(anyhow::anyhow!(
            "Failed to create App from path: {:?}",
            path
        ))
    }
}

pub fn load_icon(path: &Path) -> Result<RustImageData> {
    let icon_path_str = path.to_string_lossy();
    let icon = get_icon_by_path(&icon_path_str)
        .map_err(|e| anyhow::anyhow!("Failed to get icon: {}", e))?;
    Ok(RustImageData::from_dynamic_image(
        image::DynamicImage::ImageRgba8(icon),
    ))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_all_apps() {
        let extra_search_paths = Vec::new();
        let apps = get_all_apps(&extra_search_paths).unwrap();
        println!("{:#?}", apps);
        println!("{:#?}", apps.len());
        assert!(!apps.is_empty());
    }

    #[test]
    fn test_path_alias() {
        let path = PathBuf::from("%windir%\\system32\\mstsc.exe");
        let path = translate_path_alias(path);
        assert_eq!(
            path.to_string_lossy().to_lowercase(),
            "c:\\windows\\system32\\mstsc.exe"
        );
    }

    // #[test]
    // fn test_parse_lnk_with_powershell() {
    //     let path =
    //         PathBuf::from("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Docker Desktop.lnk");
    //     let result = parse_lnk_with_powershell_1(path).unwrap();
    //     println!("{:#?}", result);
    // }
}