From d981b77ed759608b7d2ba8df58aac946b0c04aa9 Mon Sep 17 00:00:00 2001 From: veeso Date: Thu, 10 Jun 2021 22:36:29 +0200 Subject: [PATCH] Working on open --- .../activities/filetransfer/actions/find.rs | 4 +- .../activities/filetransfer/actions/open.rs | 120 ++++++++++++------ .../activities/filetransfer/actions/submit.rs | 20 +-- src/ui/activities/filetransfer/mod.rs | 16 +++ src/ui/activities/filetransfer/update.rs | 12 ++ 5 files changed, 115 insertions(+), 57 deletions(-) diff --git a/src/ui/activities/filetransfer/actions/find.rs b/src/ui/activities/filetransfer/actions/find.rs index 6e8ead8..d229d8e 100644 --- a/src/ui/activities/filetransfer/actions/find.rs +++ b/src/ui/activities/filetransfer/actions/find.rs @@ -178,10 +178,10 @@ impl FileTransferActivity { fn open_found_file(&mut self, entry: &FsEntry, with: Option<&str>) { match self.browser.tab() { FileExplorerTab::FindLocal | FileExplorerTab::Local => { - self.action_open_local(entry, with); + self.action_open_local_file(entry, with); } FileExplorerTab::FindRemote | FileExplorerTab::Remote => { - self.action_open_remote(entry, with); + self.action_open_remote_file(entry, with); } } } diff --git a/src/ui/activities/filetransfer/actions/open.rs b/src/ui/activities/filetransfer/actions/open.rs index 00b692a..c98796a 100644 --- a/src/ui/activities/filetransfer/actions/open.rs +++ b/src/ui/activities/filetransfer/actions/open.rs @@ -29,54 +29,90 @@ extern crate open; // locals use super::{FileTransferActivity, FsEntry, LogLevel, SelectedEntry}; +// ext +use std::path::PathBuf; impl FileTransferActivity { /// ### action_open_local /// /// Open local file - pub(crate) fn action_open_local(&mut self, entry: &FsEntry, open_with: Option<&str>) { - let real_entry: FsEntry = entry.get_realfile(); - if let FsEntry::File(file) = real_entry { - // Open file - let result = match open_with { - None => open::that(file.abs_path.as_path()), - Some(with) => open::with(file.abs_path.as_path(), with), - }; - // Log result - match result { - Ok(_) => self.log( - LogLevel::Info, - format!("Opened file `{}`", entry.get_abs_path().display(),), + pub(crate) fn action_open_local(&mut self) { + let entries: Vec = match self.get_local_selected_entries() { + SelectedEntry::One(entry) => vec![entry], + SelectedEntry::Many(entries) => entries, + SelectedEntry::None => vec![], + }; + entries + .iter() + .for_each(|x| self.action_open_local_file(x, None)); + } + + /// ### action_open_remote + /// + /// Open local file + pub(crate) fn action_open_remote(&mut self) { + let entries: Vec = match self.get_remote_selected_entries() { + SelectedEntry::One(entry) => vec![entry], + SelectedEntry::Many(entries) => entries, + SelectedEntry::None => vec![], + }; + entries + .iter() + .for_each(|x| self.action_open_remote_file(x, None)); + } + + /// ### action_open_local_file + /// + /// Perform open lopcal file + pub(crate) fn action_open_local_file(&mut self, entry: &FsEntry, open_with: Option<&str>) { + let entry: FsEntry = entry.get_realfile(); + // Open file + let result = match open_with { + None => open::that(entry.get_abs_path().as_path()), + Some(with) => open::with(entry.get_abs_path().as_path(), with), + }; + // Log result + match result { + Ok(_) => self.log( + LogLevel::Info, + format!("Opened file `{}`", entry.get_abs_path().display(),), + ), + Err(err) => self.log( + LogLevel::Error, + format!( + "Failed to open filoe `{}`: {}", + entry.get_abs_path().display(), + err ), - Err(err) => self.log( - LogLevel::Error, - format!( - "Failed to open filoe `{}`: {}", - entry.get_abs_path().display(), - err - ), - ), - } + ), } } /// ### action_open_local /// /// Open remote file. The file is first downloaded to a temporary directory on localhost - pub(crate) fn action_open_remote(&mut self, entry: &FsEntry, open_with: Option<&str>) { - let real_entry: FsEntry = entry.get_realfile(); - if let FsEntry::File(file) = real_entry { - // Download file - let tmp = match self.download_file_as_temp(&file) { - Ok(f) => f, - Err(err) => { - self.log( - LogLevel::Error, - format!("Could not open `{}`: {}", file.abs_path.display(), err), - ); - return; - } - }; + pub(crate) fn action_open_remote_file(&mut self, entry: &FsEntry, open_with: Option<&str>) { + let entry: FsEntry = entry.get_realfile(); + // Download file + let tmpfile: String = match self.get_cache_tmp_name(entry.get_name()) { + None => { + self.log(LogLevel::Error, String::from("Could not create tempdir")); + return; + } + Some(p) => p, + }; + let cache: PathBuf = match self.cache.as_ref() { + None => { + self.log(LogLevel::Error, String::from("Could not create tempdir")); + return; + } + Some(p) => p.path().to_path_buf(), + }; + self.filetransfer_recv(entry, cache.as_path(), Some(tmpfile.clone())); + // Make file and open if file exists + let mut tmp: PathBuf = cache; + tmp.push(tmpfile.as_str()); + if tmp.exists() { // Open file let result = match open_with { None => open::that(tmp.as_path()), @@ -110,9 +146,9 @@ impl FileTransferActivity { SelectedEntry::None => vec![], }; // Open all entries - for entry in entries.iter() { - self.action_open_local(entry, Some(with)); - } + entries + .iter() + .for_each(|x| self.action_open_local_file(x, Some(with))); } /// ### action_remote_open_with @@ -125,8 +161,8 @@ impl FileTransferActivity { SelectedEntry::None => vec![], }; // Open all entries - for entry in entries.iter() { - self.action_open_remote(entry, Some(with)); - } + entries + .iter() + .for_each(|x| self.action_open_remote_file(x, Some(with))); } } diff --git a/src/ui/activities/filetransfer/actions/submit.rs b/src/ui/activities/filetransfer/actions/submit.rs index 712eb16..ea034a7 100644 --- a/src/ui/activities/filetransfer/actions/submit.rs +++ b/src/ui/activities/filetransfer/actions/submit.rs @@ -30,7 +30,7 @@ use super::{FileTransferActivity, FsEntry}; enum SubmitAction { ChangeDir, - OpenFile, + None, } impl FileTransferActivity { @@ -47,19 +47,16 @@ impl FileTransferActivity { // If symlink and is directory, point to symlink match &**symlink_entry { FsEntry::Directory(_) => SubmitAction::ChangeDir, - _ => SubmitAction::OpenFile, + _ => SubmitAction::None, } } - None => SubmitAction::OpenFile, + None => SubmitAction::None, } } }; match action { SubmitAction::ChangeDir => self.action_enter_local_dir(entry, false), - SubmitAction::OpenFile => { - self.action_open_local(&entry, None); - false - } + SubmitAction::None => false, } } @@ -76,19 +73,16 @@ impl FileTransferActivity { // If symlink and is directory, point to symlink match &**symlink_entry { FsEntry::Directory(_) => SubmitAction::ChangeDir, - _ => SubmitAction::OpenFile, + _ => SubmitAction::None, } } - None => SubmitAction::OpenFile, + None => SubmitAction::None, } } }; match action { SubmitAction::ChangeDir => self.action_enter_remote_dir(entry, false), - SubmitAction::OpenFile => { - self.action_open_remote(&entry, None); - false - } + SubmitAction::None => false, } } } diff --git a/src/ui/activities/filetransfer/mod.rs b/src/ui/activities/filetransfer/mod.rs index 61d4bb2..00437eb 100644 --- a/src/ui/activities/filetransfer/mod.rs +++ b/src/ui/activities/filetransfer/mod.rs @@ -193,6 +193,22 @@ impl FileTransferActivity { pub(crate) fn found_mut(&mut self) -> Option<&mut FileExplorer> { self.browser.found_mut() } + + /// ### get_cache_tmp_name + /// + /// Get file name for a file in cache + pub(crate) fn get_cache_tmp_name(&self, name: &str) -> Option { + self.cache.as_ref().map(|_| { + format!( + "{}-{}", + name, + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis() + ) + }) + } } /** diff --git a/src/ui/activities/filetransfer/update.rs b/src/ui/activities/filetransfer/update.rs index decc4fc..4ab9a4d 100644 --- a/src/ui/activities/filetransfer/update.rs +++ b/src/ui/activities/filetransfer/update.rs @@ -211,6 +211,18 @@ impl Update for FileTransferActivity { self.update_remote_filelist() } // -- common explorer keys + (COMPONENT_EXPLORER_LOCAL, &MSG_KEY_SHIFT_ENTER) + | (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_SHIFT_ENTER) + | (COMPONENT_EXPLORER_FIND, &MSG_KEY_SHIFT_ENTER) => { + match self.browser.tab() { + FileExplorerTab::Local => self.action_open_local(), + FileExplorerTab::Remote => self.action_open_remote(), + FileExplorerTab::FindLocal | FileExplorerTab::FindRemote => { + self.action_find_open() + } + } + None + } (COMPONENT_EXPLORER_LOCAL, &MSG_KEY_CHAR_B) | (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_B) => { // Show sorting file