diff --git a/src/activity_manager.rs b/src/activity_manager.rs index 13c425c..9022a4c 100644 --- a/src/activity_manager.rs +++ b/src/activity_manager.rs @@ -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,20 +147,24 @@ impl ActivityManager { // Draw activity activity.on_draw(); // Check if has to be terminated - if activity.quit { - // Quit activities - result = None; - break; - } - if activity.setup { - // User requested activity - result = Some(NextActivity::SetupActivity); - break; - } - if activity.submit { - // User submitted, set next activity - result = Some(NextActivity::FileTransfer); - break; + if let Some(exit_reason) = activity.will_umount() { + match exit_reason { + ExitReason::Quit => { + result = None; + break; + } + ExitReason::EnterSetup => { + // User requested activity + result = Some(NextActivity::SetupActivity); + break; + } + ExitReason::Connect => { + // User submitted, set next activity + result = Some(NextActivity::FileTransfer); + break; + } + _ => { /* Nothing to do */ } + } } // Sleep for ticks sleep(self.interval); @@ -197,15 +201,19 @@ impl ActivityManager { // Draw activity activity.on_draw(); // Check if has to be terminated - if activity.quit { - // Quit activities - result = None; - break; - } - if activity.disconnected { - // User disconnected, set next activity to authentication - result = Some(NextActivity::Authentication); - break; + if let Some(exit_reason) = activity.will_umount() { + match exit_reason { + ExitReason::Quit => { + result = None; + break; + } + 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 diff --git a/src/ui/activities/auth_activity/mod.rs b/src/ui/activities/auth_activity/mod.rs index 1d5275e..84bbac1 100644 --- a/src/ui/activities/auth_activity/mod.rs +++ b/src/ui/activities/auth_activity/mod.rs @@ -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, context: Option, view: View, bookmarks_client: Option, @@ -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. diff --git a/src/ui/activities/auth_activity/update.rs b/src/ui/activities/auth_activity/update.rs index 833ebb3..b10b14e 100644 --- a/src/ui/activities/auth_activity/update.rs +++ b/src/ui/activities/auth_activity/update.rs @@ -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 } diff --git a/src/ui/activities/filetransfer_activity/mod.rs b/src/ui/activities/filetransfer_activity/mod.rs index 83ef365..919b8ea 100644 --- a/src/ui/activities/filetransfer_activity/mod.rs +++ b/src/ui/activities/filetransfer_activity/mod.rs @@ -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, // Exit reason context: Option, // Context holder view: View, // View client: Box, // File transfer client @@ -221,8 +220,7 @@ impl FileTransferActivity { // Get config client let config_client: Option = 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. diff --git a/src/ui/activities/filetransfer_activity/session.rs b/src/ui/activities/filetransfer_activity/session.rs index c9b639d..5c76a8a 100644 --- a/src/ui/activities/filetransfer_activity/session.rs +++ b/src/ui/activities/filetransfer_activity/session.rs @@ -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 diff --git a/src/ui/activities/filetransfer_activity/update.rs b/src/ui/activities/filetransfer_activity/update.rs index 11f050a..3caf6f3 100644 --- a/src/ui/activities/filetransfer_activity/update.rs +++ b/src/ui/activities/filetransfer_activity/update.rs @@ -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 diff --git a/src/ui/activities/mod.rs b/src/ui/activities/mod.rs index 8cbbe14..e4b7230 100644 --- a/src/ui/activities/mod.rs +++ b/src/ui/activities/mod.rs @@ -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. diff --git a/src/ui/activities/setup_activity/mod.rs b/src/ui/activities/setup_activity/mod.rs index 432a1f2..a8d8b5c 100644 --- a/src/ui/activities/setup_activity/mod.rs +++ b/src/ui/activities/setup_activity/mod.rs @@ -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, context: Option, // 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. diff --git a/src/ui/activities/setup_activity/update.rs b/src/ui/activities/setup_activity/update.rs index e774baa..9d3a805 100644 --- a/src/ui/activities/setup_activity/update.rs +++ b/src/ui/activities/setup_activity/update.rs @@ -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 }