summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2020-11-28 14:44:50 +0200
committerManos Pitsidianakis <el13635@mail.ntua.gr>2020-11-28 15:59:25 +0200
commit98c1ece28d86a13a84678fb9918174a66f55a637 (patch)
treed246416362bf7dbaa0be7e985dd492c6e7ccd604 /src
parent54b2066f7360840b5feea925c9c73dff09d3edaf (diff)
Update xdg-util dependency to 0.4.0
Diffstat (limited to 'src')
-rw-r--r--src/components/mail/view.rs64
-rw-r--r--src/components/mail/view/envelope.rs42
-rw-r--r--src/components/mail/view/html.rs14
3 files changed, 91 insertions, 29 deletions
diff --git a/src/components/mail/view.rs b/src/components/mail/view.rs
index dd97b1b7..ff2e8cc1 100644
--- a/src/components/mail/view.rs
+++ b/src/components/mail/view.rs
@@ -1910,17 +1910,21 @@ impl Component for MailView {
}
ContentType::Other { .. } => {
let attachment_type = attachment.mime_type();
- let binary = query_default_app(&attachment_type);
let filename = attachment.filename();
- if let Ok(binary) = binary {
+ if let Ok(command) = query_default_app(&attachment_type) {
let p = create_temp_file(
&decode(attachment, None),
filename.as_ref().map(|s| s.as_str()),
None,
true,
);
- match Command::new(&binary)
- .arg(p.path())
+ let (exec_cmd, argument) = desktop_exec_to_command(
+ &command,
+ p.path.display().to_string(),
+ false,
+ );
+ match Command::new(&exec_cmd)
+ .arg(&argument)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
@@ -1932,9 +1936,8 @@ impl Component for MailView {
Err(err) => {
context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(format!(
- "Failed to start {}: {}",
- binary.display(),
- err
+ "Failed to start `{} {}`: {}",
+ &exec_cmd, &argument, err
)),
));
}
@@ -2508,3 +2511,50 @@ fn save_attachment(path: &std::path::Path, bytes: &[u8]) -> Result<()> {
f.flush()?;
Ok(())
}
+
+fn desktop_exec_to_command(command: &str, path: String, is_url: bool) -> (String, String) {
+ /* Purge unused field codes */
+ let command = command
+ .replace("%i", "")
+ .replace("%c", "")
+ .replace("%k", "");
+ if let Some(pos) = command.find("%f").or_else(|| command.find("%F")) {
+ (command[0..pos].trim().to_string(), path)
+ } else if let Some(pos) = command.find("%u").or_else(|| command.find("%U")) {
+ if is_url {
+ (command[0..pos].trim().to_string(), path)
+ } else {
+ (
+ command[0..pos].trim().to_string(),
+ format!("file://{}", path),
+ )
+ }
+ } else {
+ (command, path)
+ }
+}
+
+/*
+#[test]
+fn test_desktop_exec() {
+ for cmd in [
+ "ristretto %F",
+ "/usr/lib/firefox-esr/firefox-esr %u",
+ "/usr/bin/vlc --started-from-file %U",
+ "zathura %U",
+ ]
+ .iter()
+ {
+ println!(
+ "cmd = {} output = {:?}, is_url = false",
+ cmd,
+ desktop_exec_to_command(cmd, "/tmp/file".to_string(), false)
+ );
+ println!(
+ "cmd = {} output = {:?}, is_url = true",
+ cmd,
+ desktop_exec_to_command(cmd, "www.example.com".to_string(), true)
+ );
+ }
+}
+*/
diff --git a/src/components/mail/view/envelope.rs b/src/components/mail/view/envelope.rs
index d8477657..89792470 100644
--- a/src/components/mail/view/envelope.rs
+++ b/src/components/mail/view/envelope.rs
@@ -433,43 +433,55 @@ impl Component for EnvelopeView {
));
return true;
}
- ContentType::Other { ref name, .. } => {
+ ContentType::Other { .. } => {
let attachment_type = u.mime_type();
- let binary = query_default_app(&attachment_type);
- if let Ok(binary) = binary {
+ let filename = u.filename();
+ if let Ok(command) = query_default_app(&attachment_type) {
let p = create_temp_file(
&decode(u, None),
- name.as_ref().map(String::as_str),
+ filename.as_ref().map(|s| s.as_str()),
None,
true,
);
- match Command::new(&binary)
- .arg(p.path())
+ let (exec_cmd, argument) = super::desktop_exec_to_command(
+ &command,
+ p.path.display().to_string(),
+ false,
+ );
+ match Command::new(&exec_cmd)
+ .arg(&argument)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Ok(child) => {
- context.children.push(child);
context.temp_files.push(p);
+ context.children.push(child);
}
Err(err) => {
context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(format!(
- "Failed to start {}: {}",
- binary.display(),
- err
+ "Failed to start `{} {}`: {}",
+ &exec_cmd, &argument, err
)),
));
}
}
} else {
context.replies.push_back(UIEvent::StatusEvent(
- StatusEvent::DisplayMessage(format!(
- "Couldn't find a default application for type {}",
- attachment_type
- )),
- ));
+ StatusEvent::DisplayMessage(if let Some(filename) = filename.as_ref() {
+ format!(
+ "Couldn't find a default application for file {} (type {})",
+ filename,
+ attachment_type
+ )
+ } else {
+ format!(
+ "Couldn't find a default application for type {}",
+ attachment_type
+ )
+ }),
+ ));
return true;
}
}
diff --git a/src/components/mail/view/html.rs b/src/components/mail/view/html.rs
index 1bc02e12..873d0cae 100644
--- a/src/components/mail/view/html.rs
+++ b/src/components/mail/view/html.rs
@@ -131,11 +131,12 @@ impl Component for HtmlView {
}
if let UIEvent::Input(Key::Char('v')) = event {
- let binary = query_default_app("text/html");
- if let Ok(binary) = binary {
+ if let Ok(command) = query_default_app("text/html") {
let p = create_temp_file(&self.bytes, None, None, true);
- match Command::new(&binary)
- .arg(p.path())
+ let (exec_cmd, argument) =
+ super::desktop_exec_to_command(&command, p.path.display().to_string(), false);
+ match Command::new(&exec_cmd)
+ .arg(&argument)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
@@ -147,9 +148,8 @@ impl Component for HtmlView {
Err(err) => {
context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(format!(
- "Failed to start {}: {}",
- binary.display(),
- err
+ "Failed to start `{} {}`: {}",
+ &exec_cmd, &argument, err
)),
));
}