Working on open

This commit is contained in:
veeso
2021-06-10 22:36:29 +02:00
parent 541a9a55b5
commit d981b77ed7
5 changed files with 115 additions and 57 deletions

View File

@@ -178,10 +178,10 @@ impl FileTransferActivity {
fn open_found_file(&mut self, entry: &FsEntry, with: Option<&str>) { fn open_found_file(&mut self, entry: &FsEntry, with: Option<&str>) {
match self.browser.tab() { match self.browser.tab() {
FileExplorerTab::FindLocal | FileExplorerTab::Local => { FileExplorerTab::FindLocal | FileExplorerTab::Local => {
self.action_open_local(entry, with); self.action_open_local_file(entry, with);
} }
FileExplorerTab::FindRemote | FileExplorerTab::Remote => { FileExplorerTab::FindRemote | FileExplorerTab::Remote => {
self.action_open_remote(entry, with); self.action_open_remote_file(entry, with);
} }
} }
} }

View File

@@ -29,54 +29,90 @@
extern crate open; extern crate open;
// locals // locals
use super::{FileTransferActivity, FsEntry, LogLevel, SelectedEntry}; use super::{FileTransferActivity, FsEntry, LogLevel, SelectedEntry};
// ext
use std::path::PathBuf;
impl FileTransferActivity { impl FileTransferActivity {
/// ### action_open_local /// ### action_open_local
/// ///
/// Open local file /// Open local file
pub(crate) fn action_open_local(&mut self, entry: &FsEntry, open_with: Option<&str>) { pub(crate) fn action_open_local(&mut self) {
let real_entry: FsEntry = entry.get_realfile(); let entries: Vec<FsEntry> = match self.get_local_selected_entries() {
if let FsEntry::File(file) = real_entry { SelectedEntry::One(entry) => vec![entry],
// Open file SelectedEntry::Many(entries) => entries,
let result = match open_with { SelectedEntry::None => vec![],
None => open::that(file.abs_path.as_path()), };
Some(with) => open::with(file.abs_path.as_path(), with), entries
}; .iter()
// Log result .for_each(|x| self.action_open_local_file(x, None));
match result { }
Ok(_) => self.log(
LogLevel::Info, /// ### action_open_remote
format!("Opened file `{}`", entry.get_abs_path().display(),), ///
/// Open local file
pub(crate) fn action_open_remote(&mut self) {
let entries: Vec<FsEntry> = 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 /// ### action_open_local
/// ///
/// Open remote file. The file is first downloaded to a temporary directory on localhost /// 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>) { pub(crate) fn action_open_remote_file(&mut self, entry: &FsEntry, open_with: Option<&str>) {
let real_entry: FsEntry = entry.get_realfile(); let entry: FsEntry = entry.get_realfile();
if let FsEntry::File(file) = real_entry { // Download file
// Download file let tmpfile: String = match self.get_cache_tmp_name(entry.get_name()) {
let tmp = match self.download_file_as_temp(&file) { None => {
Ok(f) => f, self.log(LogLevel::Error, String::from("Could not create tempdir"));
Err(err) => { return;
self.log( }
LogLevel::Error, Some(p) => p,
format!("Could not open `{}`: {}", file.abs_path.display(), err), };
); let cache: PathBuf = match self.cache.as_ref() {
return; 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 // Open file
let result = match open_with { let result = match open_with {
None => open::that(tmp.as_path()), None => open::that(tmp.as_path()),
@@ -110,9 +146,9 @@ impl FileTransferActivity {
SelectedEntry::None => vec![], SelectedEntry::None => vec![],
}; };
// Open all entries // Open all entries
for entry in entries.iter() { entries
self.action_open_local(entry, Some(with)); .iter()
} .for_each(|x| self.action_open_local_file(x, Some(with)));
} }
/// ### action_remote_open_with /// ### action_remote_open_with
@@ -125,8 +161,8 @@ impl FileTransferActivity {
SelectedEntry::None => vec![], SelectedEntry::None => vec![],
}; };
// Open all entries // Open all entries
for entry in entries.iter() { entries
self.action_open_remote(entry, Some(with)); .iter()
} .for_each(|x| self.action_open_remote_file(x, Some(with)));
} }
} }

View File

@@ -30,7 +30,7 @@ use super::{FileTransferActivity, FsEntry};
enum SubmitAction { enum SubmitAction {
ChangeDir, ChangeDir,
OpenFile, None,
} }
impl FileTransferActivity { impl FileTransferActivity {
@@ -47,19 +47,16 @@ impl FileTransferActivity {
// If symlink and is directory, point to symlink // If symlink and is directory, point to symlink
match &**symlink_entry { match &**symlink_entry {
FsEntry::Directory(_) => SubmitAction::ChangeDir, FsEntry::Directory(_) => SubmitAction::ChangeDir,
_ => SubmitAction::OpenFile, _ => SubmitAction::None,
} }
} }
None => SubmitAction::OpenFile, None => SubmitAction::None,
} }
} }
}; };
match action { match action {
SubmitAction::ChangeDir => self.action_enter_local_dir(entry, false), SubmitAction::ChangeDir => self.action_enter_local_dir(entry, false),
SubmitAction::OpenFile => { SubmitAction::None => false,
self.action_open_local(&entry, None);
false
}
} }
} }
@@ -76,19 +73,16 @@ impl FileTransferActivity {
// If symlink and is directory, point to symlink // If symlink and is directory, point to symlink
match &**symlink_entry { match &**symlink_entry {
FsEntry::Directory(_) => SubmitAction::ChangeDir, FsEntry::Directory(_) => SubmitAction::ChangeDir,
_ => SubmitAction::OpenFile, _ => SubmitAction::None,
} }
} }
None => SubmitAction::OpenFile, None => SubmitAction::None,
} }
} }
}; };
match action { match action {
SubmitAction::ChangeDir => self.action_enter_remote_dir(entry, false), SubmitAction::ChangeDir => self.action_enter_remote_dir(entry, false),
SubmitAction::OpenFile => { SubmitAction::None => false,
self.action_open_remote(&entry, None);
false
}
} }
} }
} }

View File

@@ -193,6 +193,22 @@ impl FileTransferActivity {
pub(crate) fn found_mut(&mut self) -> Option<&mut FileExplorer> { pub(crate) fn found_mut(&mut self) -> Option<&mut FileExplorer> {
self.browser.found_mut() 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<String> {
self.cache.as_ref().map(|_| {
format!(
"{}-{}",
name,
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
)
})
}
} }
/** /**

View File

@@ -211,6 +211,18 @@ impl Update for FileTransferActivity {
self.update_remote_filelist() self.update_remote_filelist()
} }
// -- common explorer keys // -- 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_LOCAL, &MSG_KEY_CHAR_B)
| (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_B) => { | (COMPONENT_EXPLORER_REMOTE, &MSG_KEY_CHAR_B) => {
// Show sorting file // Show sorting file