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
use super::{bytes_to_string, should_use, Error, UseCommand}; use crate::{Dialog, OpenMultipleFile, OpenSingleFile, Result}; use std::process::Command; impl Dialog for OpenSingleFile<'_> { type Output = Option<String>; fn show(self) -> Result<Self::Output> { match should_use() { Some(UseCommand::KDialog(command)) => { dialog_implementation_kdialog(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: false, }) } Some(UseCommand::Zenity(command)) => { dialog_implementation_zenity(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: false, }) } None => Err(Error::NoImplementation), } } } impl Dialog for OpenMultipleFile<'_> { type Output = Vec<String>; fn show(self) -> Result<Self::Output> { let lf_separated = match should_use() { Some(UseCommand::KDialog(command)) => { dialog_implementation_kdialog(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: true, }) } Some(UseCommand::Zenity(command)) => { dialog_implementation_zenity(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: true, }) } None => Err(Error::NoImplementation), }; match lf_separated { Ok(Some(s)) => Ok(s.split('\n').map(ToString::to_string).collect()), Ok(_) => Ok(vec![]), Err(e) => Err(e), } } } struct ImplementationParams<'a> { command: Command, dir: Option<&'a str>, filter: Option<&'a [&'a str]>, multiple: bool, } fn dialog_implementation_kdialog(mut params: ImplementationParams) -> Result<Option<String>> { let command = &mut params.command; command.arg("--getopenfilename"); match params.dir { Some(dir) => command.arg(dir), None => command.arg(""), }; if params.multiple { command.args(&["--multiple", "--separate-output"]); } if let Some(filter) = params.filter { let types: Vec<String> = filter.iter().map(|s| format!("*.{}", s)).collect(); command.arg(types.join(" ")); } let output = command.output()?; match output.status.code() { Some(0) => bytes_to_string(output.stdout).map(Some), Some(1) => Ok(None), _ => Err(Error::UnexpectedOutput("kdialog")), } } fn dialog_implementation_zenity(mut params: ImplementationParams) -> Result<Option<String>> { let command = &mut params.command; command.arg("--file-selection"); if params.multiple { command.args(&["--multiple", "--separator", "\n"]); } command.arg("--filename"); match params.dir { Some(dir) => command.arg(dir), None => command.arg(""), }; if let Some(filter) = params.filter { let types: Vec<String> = filter.iter().map(|s| format!("*.{}", s)).collect(); command.arg("--file-filter"); command.arg(types.join(" ")); } let output = command.output()?; match output.status.code() { Some(0) => bytes_to_string(output.stdout).map(Some), Some(1) => Ok(None), _ => Err(Error::UnexpectedOutput("zenity")), } }