cosmic-files/src/lib.rs

150 lines
4.1 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;
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;
pub mod tab;
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
}
}
}
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();
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);
#[cfg(feature = "wayland")]
{
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,
mode: app::Mode::Desktop,
locations,
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();
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();
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 {
2025-01-23 13:02:51 -07:00
//TODO: support more URLs
let path = match url::Url::parse(&arg) {
Ok(url) => match url.to_file_path() {
Ok(path) => path,
Err(()) => {
log::warn!("invalid argument {:?}", arg);
continue;
}
},
Err(_) => PathBuf::from(arg),
};
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
let flags = Flags {
config_handler,
config,
2024-08-20 13:26:10 -06:00
mode: app::Mode::App,
locations,
2024-02-01 15:14:14 -07:00
};
cosmic::app::run::<App>(settings, flags)?;
Ok(())
}