diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-10-26 17:13:33 +0200 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-10-26 17:13:33 +0200 |
commit | 37a36500a368d8e150534f09fde4efe557f858c7 (patch) | |
tree | ba336e5d0fc485b47f46030a07a52957dea3ef63 | |
parent | 7d46af336f214d1acc03afd8b7a13dfb1f655fa1 (diff) |
removable not working, wrong command passed to gio, everything else looks good
-rw-r--r-- | src/event_dispatch.rs | 9 | ||||
-rw-r--r-- | src/removable_devices.rs | 48 | ||||
-rw-r--r-- | src/status.rs | 43 |
3 files changed, 95 insertions, 5 deletions
diff --git a/src/event_dispatch.rs b/src/event_dispatch.rs index 196a814..8dfbe95 100644 --- a/src/event_dispatch.rs +++ b/src/event_dispatch.rs @@ -95,6 +95,15 @@ impl EventDispatcher { Mode::Navigate(Navigate::EncryptedDrive) if c == 'm' => status.mount_encrypted_drive(), Mode::Navigate(Navigate::EncryptedDrive) if c == 'g' => status.go_to_encrypted_drive(), Mode::Navigate(Navigate::EncryptedDrive) if c == 'u' => status.umount_encrypted_drive(), + Mode::Navigate(Navigate::RemovableDevices) if c == 'm' => { + status.mount_removable_device() + } + Mode::Navigate(Navigate::RemovableDevices) if c == 'g' => { + status.go_to_removable_device() + } + Mode::Navigate(Navigate::RemovableDevices) if c == 'u' => { + status.umount_removable_device() + } Mode::Navigate(Navigate::Jump) if c == ' ' => status.jump_remove_selected_flagged(), Mode::Navigate(Navigate::Jump) if c == 'u' => status.clear_flags_and_reset_view(), Mode::Navigate(Navigate::Jump) if c == 'x' => status.delete_single_flagged(), diff --git a/src/removable_devices.rs b/src/removable_devices.rs index a67e8a9..6f793f8 100644 --- a/src/removable_devices.rs +++ b/src/removable_devices.rs @@ -1,5 +1,4 @@ use anyhow::{anyhow, Result}; -use log::info; use crate::{constant_strings_paths::GIO, impl_selectable_content}; @@ -36,6 +35,14 @@ impl RemovableDevices { Some(Self { content, index: 0 }) } } + + pub fn current(&mut self) -> Option<&mut Removable> { + if self.content.is_empty() { + None + } else { + Some(&mut self.content[self.index]) + } + } } #[derive(Debug, Clone, Default)] @@ -47,19 +54,50 @@ pub struct Removable { impl Removable { fn from_gio(line: String) -> Result<Self> { - let line = line.replace("activation_root=", ""); - let device_name = line.replace("mtp:://", ""); + let line = line.replace("activation_root=mtp://", ""); + let device_name = line; let uid = users::get_current_uid(); - let path = format!("/run/user/{uid}/gvfs/{line}"); + let path = format!("/run/user/{uid}/gvfs/mtp:host={device_name}"); let pb_path = std::path::Path::new(&path); let is_mounted = pb_path.exists() && pb_path.read_dir()?.next().is_some(); - log::info!("gio {line} - is_mounted {is_mounted}"); + log::info!("gio {device_name} - is_mounted {is_mounted}"); Ok(Self { device_name, path, is_mounted, }) } + + pub fn mount(&mut self) -> Result<()> { + if self.is_mounted { + return Err(anyhow!("Already mounted {name}", name = self.device_name)); + } + self.is_mounted = std::process::Command::new(GIO) + .args(vec![ + "mount", + &format!("mtp://{name}", name = self.device_name), + ]) + .spawn()? + .wait()? + .success(); + Ok(()) + } + + pub fn umount(&mut self) -> Result<()> { + if !self.is_mounted { + return Err(anyhow!("Not mounted {name}", name = self.device_name)); + } + self.is_mounted = std::process::Command::new(GIO) + .args(vec![ + "mount", + &format!("mtp://{name}", name = self.device_name), + "-u", + ]) + .spawn()? + .wait()? + .success(); + Ok(()) + } } impl_selectable_content!(Removable, RemovableDevices); diff --git a/src/status.rs b/src/status.rs index c448958..03f209d 100644 --- a/src/status.rs +++ b/src/status.rs @@ -729,6 +729,49 @@ impl Status { } } + pub fn mount_removable_device(&mut self) -> Result<()> { + let Some(devices) = &mut self.removable_devices else { + return Ok(()); + }; + let Some(device) = devices.current() else { + return Ok(()); + }; + if device.is_mounted { + return Ok(()); + } + device.mount()?; + Ok(()) + } + + pub fn umount_removable_device(&mut self) -> Result<()> { + let Some(devices) = &mut self.removable_devices else { + return Ok(()); + }; + let Some(device) = devices.current() else { + return Ok(()); + }; + if !device.is_mounted { + return Ok(()); + } + device.umount()?; + Ok(()) + } + + pub fn go_to_removable_device(&mut self) -> Result<()> { + let Some(devices) = &self.removable_devices else { + return Ok(()); + }; + let Some(device) = devices.selected() else { + return Ok(()); + }; + if !device.is_mounted { + return Ok(()); + } + let path = std::path::PathBuf::from(&device.path); + self.selected().set_pathcontent(&path)?; + self.selected().refresh_view() + } + /// Ask for a password of some kind (sudo or device passphrase). pub fn ask_password( &mut self, |