Skip to content

Commit 9d0d729

Browse files
authored
Refact/update printer (rustdesk#11748)
* refact: update printer Signed-off-by: fufesou <linlong1266@gmail.com> * fix: uninstall the printer for normal users Signed-off-by: fufesou <linlong1266@gmail.com> --------- Signed-off-by: fufesou <linlong1266@gmail.com>
1 parent 4c354ee commit 9d0d729

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

src/platform/windows.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ fn get_after_install(
12671267
}
12681268

12691269
pub fn install_me(options: &str, path: String, silent: bool, debug: bool) -> ResultType<()> {
1270-
let uninstall_str = get_uninstall(false);
1270+
let uninstall_str = get_uninstall(false, false);
12711271
let mut path = path.trim_end_matches('\\').to_owned();
12721272
let (subkey, _path, start_menu, exe) = get_default_install_info();
12731273
let mut exe = exe;
@@ -1428,10 +1428,10 @@ cscript \"{uninstall_shortcut}\"
14281428
{tray_shortcuts}
14291429
{shortcuts}
14301430
copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{path}\\\"
1431-
{install_remote_printer}
14321431
{dels}
14331432
{import_config}
14341433
{after_install}
1434+
{install_remote_printer}
14351435
{sleep}
14361436
",
14371437
version = crate::VERSION.replace("-", "."),
@@ -1488,22 +1488,39 @@ fn get_before_uninstall(kill_self: bool) -> String {
14881488
)
14891489
}
14901490

1491-
fn get_uninstall(kill_self: bool) -> String {
1491+
/// Constructs the uninstall command string for the application.
1492+
///
1493+
/// # Parameters
1494+
/// - `kill_self`: The command will kill the process of current app name. If `true`, it will kill
1495+
/// the current process as well. If `false`, it will exclude the current process from the kill
1496+
/// command.
1497+
/// - `uninstall_printer`: If `true`, includes commands to uninstall the remote printer.
1498+
///
1499+
/// # Details
1500+
/// The `uninstall_printer` parameter determines whether the command to uninstall the remote printer
1501+
/// is included in the generated uninstall script. If `uninstall_printer` is `false`, the printer
1502+
/// related command is omitted from the script.
1503+
fn get_uninstall(kill_self: bool, uninstall_printer: bool) -> String {
14921504
let reg_uninstall_string = get_reg("UninstallString");
14931505
if reg_uninstall_string.to_lowercase().contains("msiexec.exe") {
14941506
return reg_uninstall_string;
14951507
}
14961508

14971509
let mut uninstall_cert_cmd = "".to_string();
1510+
let mut uninstall_printer_cmd = "".to_string();
14981511
if let Ok(exe) = std::env::current_exe() {
14991512
if let Some(exe_path) = exe.to_str() {
15001513
uninstall_cert_cmd = format!("\"{}\" --uninstall-cert", exe_path);
1514+
if uninstall_printer {
1515+
uninstall_printer_cmd = format!("\"{}\" --uninstall-remote-printer", &exe_path);
1516+
}
15011517
}
15021518
}
15031519
let (subkey, path, start_menu, _) = get_install_info();
15041520
format!(
15051521
"
15061522
{before_uninstall}
1523+
{uninstall_printer_cmd}
15071524
{uninstall_cert_cmd}
15081525
reg delete {subkey} /f
15091526
{uninstall_amyuni_idd}
@@ -1519,10 +1536,7 @@ fn get_uninstall(kill_self: bool) -> String {
15191536
}
15201537

15211538
pub fn uninstall_me(kill_self: bool) -> ResultType<()> {
1522-
if crate::platform::is_win_10_or_greater() {
1523-
remote_printer::uninstall_printer(&crate::get_app_name());
1524-
}
1525-
run_cmds(get_uninstall(kill_self), true, "uninstall")
1539+
run_cmds(get_uninstall(kill_self, true), true, "uninstall")
15261540
}
15271541

15281542
fn write_cmds(cmds: String, ext: &str, tip: &str) -> ResultType<std::path::PathBuf> {
@@ -2467,6 +2481,19 @@ reg add {subkey} /f /v EstimatedSize /t REG_DWORD /d {size}
24672481
} else {
24682482
"".to_owned()
24692483
};
2484+
2485+
// No need to check the install option here, `is_rd_printer_installed` rarely fails.
2486+
let is_printer_installed = remote_printer::is_rd_printer_installed(&app_name).unwrap_or(false);
2487+
// Do nothing if the printer is not installed or failed to query if the printer is installed.
2488+
let (uninstall_printer_cmd, install_printer_cmd) = if is_printer_installed {
2489+
(
2490+
format!("\"{}\" --uninstall-remote-printer", &src_exe),
2491+
format!("\"{}\" --install-remote-printer", &src_exe),
2492+
)
2493+
} else {
2494+
("".to_owned(), "".to_owned())
2495+
};
2496+
24702497
// We do not try to remove all files in the old version.
24712498
// Because I don't know whether additional files will be installed here after installation, such as drivers.
24722499
// Just copy files to the installation directory works fine.
@@ -2487,6 +2514,8 @@ taskkill /F /IM {app_name}.exe{filter}
24872514
{reg_cmd}
24882515
{copy_exe}
24892516
{restore_service_cmd}
2517+
{uninstall_printer_cmd}
2518+
{install_printer_cmd}
24902519
{sleep}
24912520
",
24922521
app_name = app_name,

0 commit comments

Comments
 (0)