From a23de9787bccde4b6244312f04cb7cfc3b528db6 Mon Sep 17 00:00:00 2001 From: Stephan Sokolow Date: Thu, 24 Oct 2019 20:36:42 -0400 Subject: Add a backend based on KDE's `kdialog` --- src/backends/kdialog.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/backends/kdialog.rs (limited to 'src/backends/kdialog.rs') diff --git a/src/backends/kdialog.rs b/src/backends/kdialog.rs new file mode 100644 index 0000000..153e4c5 --- /dev/null +++ b/src/backends/kdialog.rs @@ -0,0 +1,143 @@ +// Copyright (C) 2019 Robin Krahl +// Copyright (C) 2019 Stephan Sokolow +// SPDX-License-Identifier: MIT + +use std::process; + +use crate::{Choice, Error, Input, Message, Password, Question, Result}; + +/// Subprocess exit codes +/// +/// Note: `kdialog` doesn't have a fixed correspondence between button labels and status codes. +/// The following mappings occur: +/// +/// - Yes/No = `0`/`1` +/// - Yes/No/Cancel = `0`/`1`/`2` +/// - OK/Cancel = `0`/`1` +const OK: i32 = 0; +const CANCEL: i32 = 1; + +/// The `kdialog` backend. +/// +/// This backend uses the external `kdialog` program to display KDE dialog boxes. +#[derive(Debug)] +pub struct KDialog { + icon: Option, + // TODO: --dontagain +} + +impl KDialog { + /// Creates a new `KDialog` instance without configuration. + pub fn new() -> KDialog { + KDialog { icon: None } + } + + /// Sets the icon in the dialog box's titlebar and taskbar button. + /// + /// The icon can be either a name from the user's configured icon theme, such as `error` or + /// `info` or the path to an image to use. + /// + /// The default image depends on the dialog type. + pub fn set_icon(&mut self, icon: impl Into) { + self.icon = Some(icon.into()); + } + + pub(crate) fn is_available() -> bool { + super::is_available("kdialog") + } + + fn execute(&self, args: Vec<&str>, title: &Option) -> Result { + let mut command = process::Command::new("kdialog"); + + if let Some(ref icon) = self.icon { + command.arg("--icon"); + command.arg(icon); + } + if let Some(ref title) = title { + command.arg("--title"); + command.arg(title); + } + + command.args(args); + command.output().map_err(Error::IoError) + } +} + +impl AsRef for KDialog { + fn as_ref(&self) -> &Self { + self + } +} + +fn require_success(status: process::ExitStatus) -> Result<()> { + if status.success() { + Ok(()) + } else { + if let Some(code) = status.code() { + match code { + CANCEL => Ok(()), + _ => Err(Error::from(("kdialog", status))), + } + } else { + Err(Error::from(("kdialog", status))) + } + } +} + +fn get_choice(status: process::ExitStatus) -> Result { + if let Some(code) = status.code() { + match code { + OK => Ok(Choice::Yes), + CANCEL => Ok(Choice::No), + _ => Err(Error::from(("kdialog", status))), + } + } else { + Err(Error::from(("kdialog", status))) + } +} + +fn get_stdout(output: process::Output) -> Result> { + if output.status.success() { + String::from_utf8(output.stdout) + .map(|s| Some(s.trim_end_matches('\n').to_string())) + .map_err(|err| Error::from(err)) + } else { + if let Some(code) = output.status.code() { + match code { + OK => Ok(None), + CANCEL => Ok(None), + _ => Err(Error::from(("kdialog", output.status))), + } + } else { + Err(Error::from(("kdialog", output.status))) + } + } +} + +impl super::Backend for KDialog { + fn show_input(&self, input: &Input) -> Result> { + let mut args = vec!["--inputbox", &input.text]; + if let Some(ref default) = input.default { + args.push(default); + } + self.execute(args, &input.title).and_then(get_stdout) + } + + fn show_message(&self, message: &Message) -> Result<()> { + let args = vec!["--msgbox", &message.text]; + self.execute(args, &message.title) + .and_then(|output| require_success(output.status)) + .map(|_| ()) + } + + fn show_password(&self, password: &Password) -> Result> { + let args = vec!["--password", &password.text]; + self.execute(args, &password.title).and_then(get_stdout) + } + + fn show_question(&self, question: &Question) -> Result { + let args = vec!["--yesno", &question.text]; + self.execute(args, &question.title) + .and_then(|output| get_choice(output.status)) + } +} -- cgit v1.2.1 From 9273f3f45f7418e87b6e53baef78612ef0d6c5e7 Mon Sep 17 00:00:00 2001 From: Stephan Sokolow Date: Thu, 24 Oct 2019 20:44:39 -0400 Subject: Fix clippy complaints that don't change the semantics (Clippy also complains about `new()` without `impl Default`) --- src/backends/kdialog.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/backends/kdialog.rs') diff --git a/src/backends/kdialog.rs b/src/backends/kdialog.rs index 153e4c5..34070c4 100644 --- a/src/backends/kdialog.rs +++ b/src/backends/kdialog.rs @@ -72,15 +72,13 @@ impl AsRef for KDialog { fn require_success(status: process::ExitStatus) -> Result<()> { if status.success() { Ok(()) - } else { - if let Some(code) = status.code() { - match code { - CANCEL => Ok(()), - _ => Err(Error::from(("kdialog", status))), - } - } else { - Err(Error::from(("kdialog", status))) + } else if let Some(code) = status.code() { + match code { + CANCEL => Ok(()), + _ => Err(Error::from(("kdialog", status))), } + } else { + Err(Error::from(("kdialog", status))) } } @@ -100,17 +98,15 @@ fn get_stdout(output: process::Output) -> Result> { if output.status.success() { String::from_utf8(output.stdout) .map(|s| Some(s.trim_end_matches('\n').to_string())) - .map_err(|err| Error::from(err)) - } else { - if let Some(code) = output.status.code() { - match code { - OK => Ok(None), - CANCEL => Ok(None), - _ => Err(Error::from(("kdialog", output.status))), - } - } else { - Err(Error::from(("kdialog", output.status))) + .map_err(Error::from) + } else if let Some(code) = output.status.code() { + match code { + OK => Ok(None), + CANCEL => Ok(None), + _ => Err(Error::from(("kdialog", output.status))), } + } else { + Err(Error::from(("kdialog", output.status))) } } -- cgit v1.2.1 From 0970381271bd07f020a888b027653ca627e8655c Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 27 Oct 2019 10:55:30 +0000 Subject: Implement Default for all backend structs This fixes the clippy warning new_without_default [0]. [0] https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default --- src/backends/kdialog.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/backends/kdialog.rs') diff --git a/src/backends/kdialog.rs b/src/backends/kdialog.rs index 34070c4..c2ddcd0 100644 --- a/src/backends/kdialog.rs +++ b/src/backends/kdialog.rs @@ -20,7 +20,7 @@ const CANCEL: i32 = 1; /// The `kdialog` backend. /// /// This backend uses the external `kdialog` program to display KDE dialog boxes. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct KDialog { icon: Option, // TODO: --dontagain @@ -29,7 +29,7 @@ pub struct KDialog { impl KDialog { /// Creates a new `KDialog` instance without configuration. pub fn new() -> KDialog { - KDialog { icon: None } + Default::default() } /// Sets the icon in the dialog box's titlebar and taskbar button. -- cgit v1.2.1 From 1b50ae033b646db0efc8ea0917685a3a0c8bfc94 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 10 Dec 2019 18:10:30 +0000 Subject: Implement show_file_selection for kdialog backend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds an implementation of Backend’s show_file_selection function to the KDialog backend, using KDialog’s --getopenfilename option. --- src/backends/kdialog.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/backends/kdialog.rs') diff --git a/src/backends/kdialog.rs b/src/backends/kdialog.rs index c2ddcd0..e556ddf 100644 --- a/src/backends/kdialog.rs +++ b/src/backends/kdialog.rs @@ -4,7 +4,7 @@ use std::process; -use crate::{Choice, Error, Input, Message, Password, Question, Result}; +use crate::{Choice, Error, FileSelection, Input, Message, Password, Question, Result}; /// Subprocess exit codes /// @@ -136,4 +136,11 @@ impl super::Backend for KDialog { self.execute(args, &question.title) .and_then(|output| get_choice(output.status)) } + + fn show_file_selection(&self, file_selection: &FileSelection) -> Result> { + let dir = file_selection.path_to_string().ok_or("path not valid")?; + let args = vec!["--getopenfilename", &dir]; + self.execute(args, &file_selection.title) + .and_then(get_stdout) + } } -- cgit v1.2.1 From 89afcb4844dd484f0c9cdfef7e5ff8d751647c43 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 10 Dec 2019 18:46:44 +0000 Subject: Add Open/Save mode to the file selection dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds the option to set a FileSelectionMode, either Open or Save. Not all backends might support this – currently, only zenity and kdialog do. Per default, the Open mode is used (as before). --- src/backends/kdialog.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/backends/kdialog.rs') diff --git a/src/backends/kdialog.rs b/src/backends/kdialog.rs index e556ddf..21928f8 100644 --- a/src/backends/kdialog.rs +++ b/src/backends/kdialog.rs @@ -4,7 +4,9 @@ use std::process; -use crate::{Choice, Error, FileSelection, Input, Message, Password, Question, Result}; +use crate::{ + Choice, Error, FileSelection, FileSelectionMode, Input, Message, Password, Question, Result, +}; /// Subprocess exit codes /// @@ -139,7 +141,11 @@ impl super::Backend for KDialog { fn show_file_selection(&self, file_selection: &FileSelection) -> Result> { let dir = file_selection.path_to_string().ok_or("path not valid")?; - let args = vec!["--getopenfilename", &dir]; + let option = match file_selection.mode { + FileSelectionMode::Open => "--getopenfilename", + FileSelectionMode::Save => "--getsavefilename", + }; + let args = vec![option, &dir]; self.execute(args, &file_selection.title) .and_then(get_stdout) } -- cgit v1.2.1