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>) {
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);
}
}
}

View File

@@ -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<FsEntry> = 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<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
///
/// 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)));
}
}

View File

@@ -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,
}
}
}

View File

@@ -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<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()
}
// -- 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