mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
will_umount method in Activity
This commit is contained in:
@@ -32,7 +32,7 @@ use crate::system::config_client::ConfigClient;
|
||||
use crate::system::environment;
|
||||
use crate::ui::activities::{
|
||||
auth_activity::AuthActivity, filetransfer_activity::FileTransferActivity,
|
||||
setup_activity::SetupActivity, Activity,
|
||||
setup_activity::SetupActivity, Activity, ExitReason,
|
||||
};
|
||||
use crate::ui::context::{Context, FileTransferParams};
|
||||
|
||||
@@ -147,21 +147,25 @@ impl ActivityManager {
|
||||
// Draw activity
|
||||
activity.on_draw();
|
||||
// Check if has to be terminated
|
||||
if activity.quit {
|
||||
// Quit activities
|
||||
if let Some(exit_reason) = activity.will_umount() {
|
||||
match exit_reason {
|
||||
ExitReason::Quit => {
|
||||
result = None;
|
||||
break;
|
||||
}
|
||||
if activity.setup {
|
||||
ExitReason::EnterSetup => {
|
||||
// User requested activity
|
||||
result = Some(NextActivity::SetupActivity);
|
||||
break;
|
||||
}
|
||||
if activity.submit {
|
||||
ExitReason::Connect => {
|
||||
// User submitted, set next activity
|
||||
result = Some(NextActivity::FileTransfer);
|
||||
break;
|
||||
}
|
||||
_ => { /* Nothing to do */ }
|
||||
}
|
||||
}
|
||||
// Sleep for ticks
|
||||
sleep(self.interval);
|
||||
}
|
||||
@@ -197,16 +201,20 @@ impl ActivityManager {
|
||||
// Draw activity
|
||||
activity.on_draw();
|
||||
// Check if has to be terminated
|
||||
if activity.quit {
|
||||
// Quit activities
|
||||
if let Some(exit_reason) = activity.will_umount() {
|
||||
match exit_reason {
|
||||
ExitReason::Quit => {
|
||||
result = None;
|
||||
break;
|
||||
}
|
||||
if activity.disconnected {
|
||||
ExitReason::Disconnect => {
|
||||
// User disconnected, set next activity to authentication
|
||||
result = Some(NextActivity::Authentication);
|
||||
break;
|
||||
}
|
||||
_ => { /* Nothing to do */ }
|
||||
}
|
||||
}
|
||||
// Sleep for ticks
|
||||
sleep(self.interval);
|
||||
}
|
||||
@@ -234,7 +242,7 @@ impl ActivityManager {
|
||||
// Draw activity
|
||||
activity.on_draw();
|
||||
// Check if activity has terminated
|
||||
if activity.quit {
|
||||
if let Some(ExitReason::Quit) = activity.will_umount() {
|
||||
break;
|
||||
}
|
||||
// Sleep for ticks
|
||||
|
||||
@@ -33,7 +33,7 @@ extern crate crossterm;
|
||||
extern crate tui;
|
||||
|
||||
// locals
|
||||
use super::{Activity, Context};
|
||||
use super::{Activity, Context, ExitReason};
|
||||
use crate::filetransfer::FileTransferProtocol;
|
||||
use crate::system::bookmarks_client::BookmarksClient;
|
||||
use crate::ui::context::FileTransferParams;
|
||||
@@ -69,9 +69,7 @@ const STORE_KEY_LATEST_VERSION: &str = "AUTH_LATEST_VERSION";
|
||||
///
|
||||
/// AuthActivity is the data holder for the authentication activity
|
||||
pub struct AuthActivity {
|
||||
pub submit: bool, // becomes true after user has submitted fields
|
||||
pub quit: bool, // Becomes true if user has pressed esc
|
||||
pub setup: bool, // Becomes true if user has requested setup
|
||||
exit_reason: Option<ExitReason>,
|
||||
context: Option<Context>,
|
||||
view: View,
|
||||
bookmarks_client: Option<BookmarksClient>,
|
||||
@@ -92,9 +90,7 @@ impl AuthActivity {
|
||||
/// Instantiates a new AuthActivity
|
||||
pub fn new() -> AuthActivity {
|
||||
AuthActivity {
|
||||
submit: false,
|
||||
quit: false,
|
||||
setup: false,
|
||||
exit_reason: None,
|
||||
context: None,
|
||||
view: View::init(),
|
||||
bookmarks_client: None,
|
||||
@@ -197,6 +193,15 @@ impl Activity for AuthActivity {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### will_umount
|
||||
///
|
||||
/// `will_umount` is the method which must be able to report to the activity manager, whether
|
||||
/// the activity should be terminated or not.
|
||||
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
|
||||
fn will_umount(&self) -> Option<&ExitReason> {
|
||||
self.exit_reason.as_ref()
|
||||
}
|
||||
|
||||
/// ### on_destroy
|
||||
///
|
||||
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
||||
|
||||
@@ -244,7 +244,7 @@ impl AuthActivity {
|
||||
}
|
||||
// Enter setup
|
||||
(_, &MSG_KEY_CTRL_C) => {
|
||||
self.setup = true;
|
||||
self.exit_reason = Some(super::ExitReason::EnterSetup);
|
||||
None
|
||||
}
|
||||
// Save bookmark; show popup
|
||||
@@ -306,7 +306,7 @@ impl AuthActivity {
|
||||
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(choice))) => {
|
||||
// If choice is 0, quit termscp
|
||||
if *choice == 0 {
|
||||
self.quit = true;
|
||||
self.exit_reason = Some(super::ExitReason::Quit);
|
||||
}
|
||||
self.umount_quit();
|
||||
None
|
||||
@@ -334,8 +334,8 @@ impl AuthActivity {
|
||||
true => None,
|
||||
false => Some(password),
|
||||
};
|
||||
// Submit true
|
||||
self.submit = true;
|
||||
// Set exit reason
|
||||
self.exit_reason = Some(super::ExitReason::Connect);
|
||||
// Return None
|
||||
None
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ extern crate textwrap;
|
||||
extern crate tui;
|
||||
|
||||
// locals
|
||||
use super::{Activity, Context};
|
||||
use super::{Activity, Context, ExitReason};
|
||||
use crate::filetransfer::ftp_transfer::FtpFileTransfer;
|
||||
use crate::filetransfer::scp_transfer::ScpFileTransfer;
|
||||
use crate::filetransfer::sftp_transfer::SftpFileTransfer;
|
||||
@@ -199,8 +199,7 @@ impl Default for TransferStates {
|
||||
///
|
||||
/// FileTransferActivity is the data holder for the file transfer activity
|
||||
pub struct FileTransferActivity {
|
||||
pub disconnected: bool, // Has disconnected from remote?
|
||||
pub quit: bool, // Has quit term scp?
|
||||
exit_reason: Option<ExitReason>, // Exit reason
|
||||
context: Option<Context>, // Context holder
|
||||
view: View, // View
|
||||
client: Box<dyn FileTransfer>, // File transfer client
|
||||
@@ -221,8 +220,7 @@ impl FileTransferActivity {
|
||||
// Get config client
|
||||
let config_client: Option<ConfigClient> = Self::init_config_client();
|
||||
FileTransferActivity {
|
||||
disconnected: false,
|
||||
quit: false,
|
||||
exit_reason: None,
|
||||
context: None,
|
||||
view: View::init(),
|
||||
client: match protocol {
|
||||
@@ -310,6 +308,15 @@ impl Activity for FileTransferActivity {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### will_umount
|
||||
///
|
||||
/// `will_umount` is the method which must be able to report to the activity manager, whether
|
||||
/// the activity should be terminated or not.
|
||||
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
|
||||
fn will_umount(&self) -> Option<&ExitReason> {
|
||||
self.exit_reason.as_ref()
|
||||
}
|
||||
|
||||
/// ### on_destroy
|
||||
///
|
||||
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
||||
|
||||
@@ -98,7 +98,7 @@ impl FileTransferActivity {
|
||||
// Disconnect
|
||||
let _ = self.client.disconnect();
|
||||
// Quit
|
||||
self.disconnected = true;
|
||||
self.exit_reason = Some(super::ExitReason::Disconnect);
|
||||
}
|
||||
|
||||
/// ### disconnect_and_quit
|
||||
@@ -106,7 +106,7 @@ impl FileTransferActivity {
|
||||
/// disconnect from remote and then quit
|
||||
pub(super) fn disconnect_and_quit(&mut self) {
|
||||
self.disconnect();
|
||||
self.quit = true;
|
||||
self.exit_reason = Some(super::ExitReason::Quit);
|
||||
}
|
||||
|
||||
/// ### reload_remote_dir
|
||||
|
||||
@@ -553,7 +553,7 @@ impl FileTransferActivity {
|
||||
}
|
||||
// -- fatal
|
||||
(COMPONENT_TEXT_FATAL, &MSG_KEY_ESC) | (COMPONENT_TEXT_FATAL, &MSG_KEY_ENTER) => {
|
||||
self.disconnected = true;
|
||||
self.exit_reason = Some(super::ExitReason::Disconnect);
|
||||
None
|
||||
}
|
||||
// -- help
|
||||
|
||||
@@ -33,7 +33,16 @@ pub mod auth_activity;
|
||||
pub mod filetransfer_activity;
|
||||
pub mod setup_activity;
|
||||
|
||||
// Activity trait
|
||||
// -- Exit reason
|
||||
|
||||
pub enum ExitReason {
|
||||
Quit,
|
||||
Connect,
|
||||
Disconnect,
|
||||
EnterSetup,
|
||||
}
|
||||
|
||||
// -- Activity trait
|
||||
|
||||
pub trait Activity {
|
||||
/// ### on_create
|
||||
@@ -49,6 +58,13 @@ pub trait Activity {
|
||||
/// This function must be called at each tick to refresh the interface
|
||||
fn on_draw(&mut self);
|
||||
|
||||
/// ### will_umount
|
||||
///
|
||||
/// `will_umount` is the method which must be able to report to the activity manager, whether
|
||||
/// the activity should be terminated or not.
|
||||
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
|
||||
fn will_umount(&self) -> Option<&ExitReason>;
|
||||
|
||||
/// ### on_destroy
|
||||
///
|
||||
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
||||
|
||||
@@ -35,7 +35,7 @@ extern crate crossterm;
|
||||
extern crate tui;
|
||||
|
||||
// Locals
|
||||
use super::{Activity, Context};
|
||||
use super::{Activity, Context, ExitReason};
|
||||
use crate::ui::layout::view::View;
|
||||
// Ext
|
||||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||
@@ -71,7 +71,7 @@ enum ViewLayout {
|
||||
///
|
||||
/// Setup activity states holder
|
||||
pub struct SetupActivity {
|
||||
pub quit: bool, // Becomes true when user requests the activity to terminate
|
||||
exit_reason: Option<ExitReason>,
|
||||
context: Option<Context>, // Context holder
|
||||
view: View, // View
|
||||
layout: ViewLayout, // View layout
|
||||
@@ -86,7 +86,7 @@ impl Default for SetupActivity {
|
||||
user_input_buffer.push(String::new());
|
||||
}
|
||||
SetupActivity {
|
||||
quit: false,
|
||||
exit_reason: None,
|
||||
context: None,
|
||||
view: View::init(),
|
||||
layout: ViewLayout::SetupForm,
|
||||
@@ -142,6 +142,15 @@ impl Activity for SetupActivity {
|
||||
}
|
||||
}
|
||||
|
||||
/// ### will_umount
|
||||
///
|
||||
/// `will_umount` is the method which must be able to report to the activity manager, whether
|
||||
/// the activity should be terminated or not.
|
||||
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
|
||||
fn will_umount(&self) -> Option<&ExitReason> {
|
||||
self.exit_reason.as_ref()
|
||||
}
|
||||
|
||||
/// ### on_destroy
|
||||
///
|
||||
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
||||
|
||||
@@ -112,12 +112,12 @@ impl SetupActivity {
|
||||
self.mount_error(err.as_str());
|
||||
}
|
||||
// Exit
|
||||
self.quit = true;
|
||||
self.exit_reason = Some(super::ExitReason::Quit);
|
||||
None
|
||||
}
|
||||
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(1))) => {
|
||||
// Quit
|
||||
self.quit = true;
|
||||
self.exit_reason = Some(super::ExitReason::Quit);
|
||||
self.umount_quit();
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user