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
use super::{bytes_to_string, should_use, Error, UseCommand}; use crate::{ r#impl::OpenDialogTarget, Dialog, OpenMultipleFile, OpenSingleDir, 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, target: OpenDialogTarget::File, }) } Some(UseCommand::Zenity(command)) => { dialog_implementation_zenity(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: false, target: OpenDialogTarget::File, }) } 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, target: OpenDialogTarget::File, }) } Some(UseCommand::Zenity(command)) => { dialog_implementation_zenity(ImplementationParams { command, dir: self.dir, filter: self.filter, multiple: true, target: OpenDialogTarget::File, }) } 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), } } } impl Dialog for OpenSingleDir<'_> { 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: None, multiple: false, target: OpenDialogTarget::Directory, }) } Some(UseCommand::Zenity(command)) => { dialog_implementation_zenity(ImplementationParams { command, dir: self.dir, filter: None, multiple: false, target: OpenDialogTarget::Directory, }) } None => Err(Error::NoImplementation), } } } struct ImplementationParams<'a> { command: Command, dir: Option<&'a str>, filter: Option<&'a [&'a str]>, multiple: bool, target: OpenDialogTarget, } fn dialog_implementation_kdialog(mut params: ImplementationParams) -> Result<Option<String>> { let command = &mut params.command; match params.target { OpenDialogTarget::File => command.arg("--getopenfilename"), OpenDialogTarget::Directory => command.arg("--getexistingdirectory"), }; 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.target == OpenDialogTarget::Directory { command.arg("--directory"); } 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")), } }