cosmic-files/src/mounter/mod.rs

139 lines
3.6 KiB
Rust
Raw Normal View History

2024-10-21 13:51:10 -06:00
use cosmic::{iced::Subscription, widget, Task};
use once_cell::sync::Lazy;
use std::{
collections::BTreeMap,
fmt,
path::{Path, PathBuf},
sync::Arc,
};
use tokio::sync::mpsc;
2024-09-13 15:13:37 -06:00
use crate::{config::IconSizes, tab};
#[cfg(feature = "gvfs")]
mod gvfs;
#[derive(Clone)]
pub struct MounterAuth {
pub message: String,
pub username_opt: Option<String>,
pub domain_opt: Option<String>,
pub password_opt: Option<String>,
pub remember_opt: Option<bool>,
pub anonymous_opt: Option<bool>,
}
// Custom debug for MounterAuth to hide password
impl fmt::Debug for MounterAuth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MounterAuth")
.field("username_opt", &self.username_opt)
.field("domain_opt", &self.domain_opt)
.field(
"password_opt",
if self.password_opt.is_some() {
&"Some(*)"
} else {
&"None"
},
)
.field("remember_opt", &self.remember_opt)
.field("anonymous_opt", &self.anonymous_opt)
.finish()
}
}
2024-04-22 13:14:25 -06:00
#[derive(Clone, Debug)]
pub enum MounterItem {
#[cfg(feature = "gvfs")]
Gvfs(gvfs::Item),
2024-04-22 15:32:19 -06:00
#[allow(dead_code)]
None,
2024-04-22 13:14:25 -06:00
}
impl MounterItem {
pub fn name(&self) -> String {
match self {
#[cfg(feature = "gvfs")]
Self::Gvfs(item) => item.name(),
2024-04-22 15:32:19 -06:00
Self::None => unreachable!(),
2024-04-22 13:14:25 -06:00
}
}
pub fn uri(&self) -> String {
match self {
#[cfg(feature = "gvfs")]
Self::Gvfs(item) => item.uri(),
Self::None => unreachable!(),
}
}
2024-04-22 13:14:25 -06:00
pub fn is_mounted(&self) -> bool {
match self {
#[cfg(feature = "gvfs")]
Self::Gvfs(item) => item.is_mounted(),
2024-04-22 15:32:19 -06:00
Self::None => unreachable!(),
2024-04-22 13:14:25 -06:00
}
}
pub fn icon(&self, symbolic: bool) -> Option<widget::icon::Handle> {
2024-04-22 13:14:25 -06:00
match self {
#[cfg(feature = "gvfs")]
Self::Gvfs(item) => item.icon(symbolic),
2024-04-22 15:32:19 -06:00
Self::None => unreachable!(),
2024-04-22 13:14:25 -06:00
}
}
pub fn path(&self) -> Option<PathBuf> {
match self {
#[cfg(feature = "gvfs")]
Self::Gvfs(item) => item.path(),
2024-04-22 15:32:19 -06:00
Self::None => unreachable!(),
2024-04-22 13:14:25 -06:00
}
}
}
2024-04-22 13:14:25 -06:00
pub type MounterItems = Vec<MounterItem>;
#[derive(Clone, Debug)]
pub enum MounterMessage {
Items(MounterItems),
MountResult(MounterItem, Result<bool, String>),
NetworkAuth(String, MounterAuth, mpsc::Sender<MounterAuth>),
NetworkResult(String, Result<bool, String>),
}
2024-09-13 15:13:37 -06:00
pub trait Mounter: Send + Sync {
fn items(&self, sizes: IconSizes) -> Option<MounterItems>;
2024-04-22 13:14:25 -06:00
//TODO: send result
2024-10-21 13:51:10 -06:00
fn mount(&self, item: MounterItem) -> Task<()>;
fn network_drive(&self, uri: String) -> Task<()>;
fn network_scan(
&self,
uri: &str,
sizes: IconSizes,
path: Option<&Path>,
) -> Option<Result<Vec<tab::Item>, String>>;
2024-10-21 13:51:10 -06:00
fn unmount(&self, item: MounterItem) -> Task<()>;
fn subscription(&self) -> Subscription<MounterMessage>;
}
2024-04-22 13:14:25 -06:00
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MounterKey(pub &'static str);
pub type MounterMap = BTreeMap<MounterKey, Box<dyn Mounter>>;
pub type Mounters = Arc<MounterMap>;
pub fn mounters() -> Mounters {
2024-04-22 15:32:19 -06:00
#[allow(unused_mut)]
let mut mounters = MounterMap::new();
#[cfg(feature = "gvfs")]
{
2024-04-22 13:14:25 -06:00
mounters.insert(MounterKey("gvfs"), Box::new(gvfs::Gvfs::new()));
}
Mounters::new(mounters)
}
pub static MOUNTERS: Lazy<Mounters> = Lazy::new(mounters);