cosmic-files/src/lib.rs

183 lines
5 KiB
Rust
Raw Normal View History

2024-02-01 15:14:14 -07:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
2024-10-02 14:53:24 -06:00
use cosmic::{app::Settings, iced::Limits};
2024-08-20 13:26:10 -06:00
use std::{env, fs, path::PathBuf, process};
2024-02-01 15:14:14 -07:00
use app::{App, Flags};
2024-08-20 13:26:10 -06:00
pub mod app;
2025-08-04 15:13:55 -06:00
mod archive;
2024-05-20 13:48:09 -06:00
pub mod clipboard;
use config::Config;
2024-08-20 13:26:10 -06:00
pub mod config;
pub mod dialog;
2024-02-01 15:14:14 -07:00
mod key_bind;
mod localize;
mod menu;
mod mime_app;
2024-03-04 12:28:16 -07:00
pub mod mime_icon;
mod mounter;
2024-02-01 15:14:14 -07:00
mod mouse_area;
pub mod operation;
2024-03-04 10:28:16 -07:00
mod spawn_detached;
2024-08-20 13:26:10 -06:00
use tab::Location;
2025-07-03 10:17:48 -04:00
use crate::config::State;
2024-08-20 13:26:10 -06:00
pub mod tab;
mod thumbnail_cacher;
mod thumbnailer;
2024-02-01 15:14:14 -07:00
2024-09-13 15:13:37 -06:00
pub(crate) fn err_str<T: ToString>(err: T) -> String {
err.to_string()
}
2024-10-04 11:53:27 -06:00
pub fn desktop_dir() -> PathBuf {
match dirs::desktop_dir() {
Some(path) => path,
None => {
let path = home_dir().join("Desktop");
log::warn!("failed to locate desktop directory, falling back to {path:?}");
path
}
}
}
2024-02-01 15:14:14 -07:00
pub fn home_dir() -> PathBuf {
match dirs::home_dir() {
Some(home) => home,
None => {
2024-10-04 11:53:27 -06:00
let path = PathBuf::from("/");
log::warn!("failed to locate home directory, falling back to {path:?}");
path
2024-02-01 15:14:14 -07:00
}
}
}
pub fn is_wayland() -> bool {
matches!(
cosmic::app::cosmic::windowing_system(),
Some(cosmic::app::cosmic::WindowingSystem::Wayland)
)
}
2024-08-20 13:26:10 -06:00
/// Runs application in desktop mode
#[rustfmt::skip]
pub fn desktop() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
localize::localize();
let (config_handler, config) = Config::load();
2025-07-03 10:17:48 -04:00
let (state_handler, state) = State::load();
2024-08-20 13:26:10 -06:00
let mut settings = Settings::default();
settings = settings.theme(config.app_theme.theme());
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
settings = settings.exit_on_close(false);
settings = settings.transparent(true);
2025-06-18 14:12:00 -04:00
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
2024-08-20 13:26:10 -06:00
{
settings = settings.no_main_window(true);
}
let locations = vec![tab::Location::Desktop(desktop_dir(), String::new(), config.desktop)];
2024-08-20 13:26:10 -06:00
let flags = Flags {
config_handler,
config,
2025-07-03 10:17:48 -04:00
state_handler,
state,
2024-08-20 13:26:10 -06:00
mode: app::Mode::Desktop,
locations,
uris: Vec::new()
2024-08-20 13:26:10 -06:00
};
cosmic::app::run::<App>(settings, flags)?;
Ok(())
}
2024-02-01 15:14:14 -07:00
/// Runs application with these settings
#[rustfmt::skip]
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
localize::localize();
let (config_handler, config) = Config::load();
2025-07-03 10:17:48 -04:00
let (state_handler, state) = State::load();
2024-02-01 15:14:14 -07:00
let mut daemonize = true;
2024-08-20 13:26:10 -06:00
let mut locations = Vec::new();
let mut uris = Vec::new();
2024-08-20 13:26:10 -06:00
for arg in env::args().skip(1) {
let location = if &arg == "--no-daemon" {
daemonize = false;
continue;
} else if &arg == "--trash" {
2024-08-20 13:26:10 -06:00
Location::Trash
} else if &arg == "--recents" {
Location::Recents
} else if &arg == "--network" {
Location::Network("network:///".to_string(), fl!("networks"), None)
2024-08-20 13:26:10 -06:00
} else {
2025-01-23 13:02:51 -07:00
//TODO: support more URLs
let path = match url::Url::parse(&arg) {
Ok(url) if url.scheme() == "file" => match url.to_file_path() {
2025-01-23 13:02:51 -07:00
Ok(path) => path,
Err(()) => {
log::warn!("invalid argument {:?}", arg);
continue;
}
},
Ok(url) => {
uris.push(url);
continue;
}
_ => PathBuf::from(arg),
2025-01-23 13:02:51 -07:00
};
match fs::canonicalize(&path) {
2024-08-20 13:26:10 -06:00
Ok(absolute) => Location::Path(absolute),
Err(err) => {
2025-01-23 13:02:51 -07:00
log::warn!("failed to canonicalize {:?}: {}", path, err);
2024-08-20 13:26:10 -06:00
continue;
}
}
};
locations.push(location);
}
if daemonize {
#[cfg(all(unix, not(target_os = "redox")))]
match fork::daemon(true, true) {
Ok(fork::Fork::Child) => (),
Ok(fork::Fork::Parent(_child_pid)) => process::exit(0),
Err(err) => {
eprintln!("failed to daemonize: {:?}", err);
process::exit(1);
}
}
}
2024-02-01 15:14:14 -07:00
let mut settings = Settings::default();
settings = settings.theme(config.app_theme.theme());
2024-02-10 19:09:37 -07:00
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
settings = settings.exit_on_close(false);
2024-02-01 15:14:14 -07:00
#[cfg(feature = "jemalloc")]
{
settings = settings.default_mmap_threshold(None);
}
2024-02-01 15:14:14 -07:00
let flags = Flags {
config_handler,
config,
2025-07-03 10:17:48 -04:00
state_handler,
state,
2024-08-20 13:26:10 -06:00
mode: app::Mode::App,
locations,
uris
2024-02-01 15:14:14 -07:00
};
cosmic::app::run::<App>(settings, flags)?;
Ok(())
}