diff --git a/Cargo.lock b/Cargo.lock index 58eed4f..0102160 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -929,6 +929,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" +[[package]] +name = "cfb" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f" +dependencies = [ + "byteorder", + "fnv", + "uuid", +] + [[package]] name = "cfg-expr" version = "0.15.8" @@ -1256,6 +1267,7 @@ dependencies = [ "open", "paste", "rayon", + "recently-used-xbel", "regex", "rust-embed", "serde", @@ -3223,6 +3235,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "infer" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc150e5ce2330295b8616ce0e3f53250e53af31759a9dbedad1621ba29151847" +dependencies = [ + "cfb", +] + [[package]] name = "inotify" version = "0.9.6" @@ -4679,6 +4700,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "quick-xml" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96a05e2e8efddfa51a84ca47cec303fac86c8541b686d37cac5efc0e094417bc" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "quote" version = "1.0.37" @@ -4772,6 +4803,22 @@ dependencies = [ "font-types", ] +[[package]] +name = "recently-used-xbel" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "079a81183e41e5cf17fd9ec55db30d6be6cddfad7fd619862efac27f1be28c9b" +dependencies = [ + "chrono", + "dirs 5.0.1", + "infer", + "mime_guess", + "quick-xml 0.36.1", + "serde", + "thiserror", + "url", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -6203,6 +6250,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" + [[package]] name = "uzers" version = "0.12.1" diff --git a/Cargo.toml b/Cargo.toml index ab555cf..002281a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ i18n-embed = { version = "0.14", features = [ i18n-embed-fl = "0.7" rust-embed = "8" slotmap = "1.0.7" +recently-used-xbel = "1.1.0" zip = "2.1.6" unix_permissions_ext = "0.1.2" uzers = "0.12.0" diff --git a/i18n/ar/cosmic_files.ftl b/i18n/ar/cosmic_files.ftl index acd208a..920647c 100644 --- a/i18n/ar/cosmic_files.ftl +++ b/i18n/ar/cosmic_files.ftl @@ -4,6 +4,7 @@ empty-folder-hidden = مجلّد فارغ (يحتوي على ملفّات أو filesystem = نظام ملفات home = منزل trash = مُهملات +recents = حديثاً # List view name = اسم diff --git a/i18n/cs/cosmic_files.ftl b/i18n/cs/cosmic_files.ftl index 395e234..02a9563 100644 --- a/i18n/cs/cosmic_files.ftl +++ b/i18n/cs/cosmic_files.ftl @@ -3,6 +3,7 @@ empty-folder = Složka je prázdná empty-folder-hidden = Složka je prázdná (obsahuje skryté položky) filesystem = Souborový systém home = Domů +recents = Nedávné trash = Koš # List view diff --git a/i18n/de/cosmic_files.ftl b/i18n/de/cosmic_files.ftl index 7abaca1..d5da3c2 100644 --- a/i18n/de/cosmic_files.ftl +++ b/i18n/de/cosmic_files.ftl @@ -4,6 +4,7 @@ empty-folder-hidden = Leerer Ordner (hat versteckte Elemente) filesystem = Dateisystem home = Benutzerordner trash = Papierkorb +recents = Aktuelle undo = Rückgängig # Listenansicht diff --git a/i18n/en/cosmic_files.ftl b/i18n/en/cosmic_files.ftl index 0358237..61086f1 100644 --- a/i18n/en/cosmic_files.ftl +++ b/i18n/en/cosmic_files.ftl @@ -6,6 +6,7 @@ filesystem = Filesystem home = Home notification-in-progress = File operations are in progress. trash = Trash +recents = Recents undo = Undo # List view diff --git a/i18n/es/cosmic_files.ftl b/i18n/es/cosmic_files.ftl index 0f63d36..4cc352b 100644 --- a/i18n/es/cosmic_files.ftl +++ b/i18n/es/cosmic_files.ftl @@ -3,6 +3,7 @@ empty-folder = Carpeta vacía empty-folder-hidden = Carpeta vacía (tiene elementos escondidos) filesystem = Sistema de archivos home = Inicio +recents = Reciente trash = Papelera # List view diff --git a/i18n/fr/cosmic_files.ftl b/i18n/fr/cosmic_files.ftl index fe402ed..9d2117b 100644 --- a/i18n/fr/cosmic_files.ftl +++ b/i18n/fr/cosmic_files.ftl @@ -6,6 +6,7 @@ filesystem = Système de fichiers home = Dossier personnel notification-in-progress = Les opérations sur les fichiers sont en cours. trash = Corbeille +recents = Récente undo = Annuler # List view diff --git a/i18n/it/cosmic_files.ftl b/i18n/it/cosmic_files.ftl index 5b82286..2b99a1e 100644 --- a/i18n/it/cosmic_files.ftl +++ b/i18n/it/cosmic_files.ftl @@ -6,6 +6,7 @@ filesystem = Filesystem home = Home notification-in-progress = Operazioni sui file in corso ... trash = Cestino +recents = Recenti undo = Annulla ultima operazione # List view diff --git a/i18n/uk/cosmic_files.ftl b/i18n/uk/cosmic_files.ftl index c025a4b..1f93011 100644 --- a/i18n/uk/cosmic_files.ftl +++ b/i18n/uk/cosmic_files.ftl @@ -4,6 +4,7 @@ empty-folder-hidden = Тека порожня (але містить прихо filesystem = Файлова система home = Домівка trash = Смітник +recents = недавній undo = Скасувати # List view diff --git a/src/app.rs b/src/app.rs index 97834f6..fecbe71 100644 --- a/src/app.rs +++ b/src/app.rs @@ -110,6 +110,7 @@ pub enum Action { ZoomDefault, ZoomIn, ZoomOut, + Recents, } impl Action { @@ -170,6 +171,7 @@ impl Action { Action::ZoomDefault => Message::TabMessage(entity_opt, tab::Message::ZoomDefault), Action::ZoomIn => Message::TabMessage(entity_opt, tab::Message::ZoomIn), Action::ZoomOut => Message::TabMessage(entity_opt, tab::Message::ZoomOut), + Action::Recents => Message::Recents, } } } @@ -277,6 +279,7 @@ pub enum Message { DndExitTab, DndDropTab(Entity, Option, DndAction), DndDropNav(Entity, Option, DndAction), + Recents, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -549,6 +552,13 @@ impl App { fn update_nav_model(&mut self) { let mut nav_model = segmented_button::ModelBuilder::default(); + + nav_model = nav_model.insert(|b| { + b.text(fl!("recents")) + .icon(widget::icon::from_name("accessories-clock-symbolic")) + .data(Location::Recents) + }); + for (favorite_i, favorite) in self.config.favorites.iter().enumerate() { if let Some(path) = favorite.path_opt() { let name = if matches!(favorite, Favorite::Home) { @@ -1633,7 +1643,14 @@ impl Application for App { Message::OpenWith(path, app) => { if let Some(mut command) = app.command(Some(path.clone())) { match spawn_detached(&mut command) { - Ok(()) => {} + Ok(()) => { + let _ = recently_used_xbel::update_recently_used( + &path, + App::APP_ID.to_string(), + "cosmic-files".to_string(), + None, + ); + } Err(err) => { log::warn!("failed to open {:?} with {:?}: {}", path, app.id, err) } @@ -2030,7 +2047,14 @@ impl Application for App { } tab::Command::OpenFile(item_path) => { match open::that_detached(&item_path) { - Ok(()) => (), + Ok(()) => { + let _ = recently_used_xbel::update_recently_used( + &item_path, + App::APP_ID.to_string(), + "cosmic-files".to_string(), + None, + ); + } Err(err) => { log::warn!("failed to open {:?}: {}", item_path, err); } @@ -2346,6 +2370,9 @@ impl Application for App { self.dialog_pages.push_front(DialogPage::EmptyTrash); } }, + Message::Recents => { + return self.open_tab(Location::Recents, false, None); + } } Command::none() diff --git a/src/dialog.rs b/src/dialog.rs index c713ac9..47ec081 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -25,6 +25,7 @@ use notify_debouncer_full::{ notify::{self, RecommendedWatcher, Watcher}, DebouncedEvent, Debouncer, FileIdMap, }; +use recently_used_xbel::update_recently_used; use std::{ any::TypeId, collections::{HashMap, HashSet}, @@ -440,6 +441,13 @@ impl Application for App { let accept_label = flags.kind.accept_label(); let mut nav_model = segmented_button::ModelBuilder::default(); + + nav_model = nav_model.insert(move |b| { + b.text(fl!("recents")) + .icon(widget::icon::from_name("accessories-clock-symbolic").size(16)) + .data(Location::Recents) + }); + if let Some(dir) = dirs::home_dir() { nav_model = nav_model.insert(move |b| { b.text(fl!("home")) @@ -666,6 +674,12 @@ impl Application for App { if item.selected { if let Some(path) = &item.path_opt { paths.push(path.clone()); + let _ = update_recently_used( + &path.clone(), + App::APP_ID.to_string(), + "cosmic-files".to_string(), + None, + ); } } } diff --git a/src/menu.rs b/src/menu.rs index adae6ad..e04ef89 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -97,7 +97,7 @@ pub fn context_menu<'a>( let mut children: Vec> = Vec::new(); match tab.location { - Location::Path(_) | Location::Search(_, _) => { + Location::Path(_) | Location::Search(_, _) | Location::Recents => { if selected > 0 { if selected_dir == 1 && selected == 1 || selected_dir == 0 { children.push(menu_item(fl!("open"), Action::Open).into()); diff --git a/src/tab.rs b/src/tab.rs index 789c7f7..2f39b6e 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -33,8 +33,10 @@ use cosmic::{ Element, }; +use chrono::{DateTime, Utc}; use mime_guess::{mime, Mime}; use once_cell::sync::Lazy; +use recently_used_xbel::{Error, RecentlyUsed}; use serde::{Deserialize, Serialize}; use std::{ cell::{Cell, RefCell}, @@ -589,11 +591,88 @@ pub fn scan_trash(sizes: IconSizes) -> Vec { items } +fn uri_to_path(uri: String) -> Option { + //TODO support for external drive or cloud? + if uri.starts_with("file://") { + let path_str = &uri[7..]; + Some(PathBuf::from(path_str)) + } else { + None + } +} + +pub fn scan_recents(sizes: IconSizes) -> Vec { + let mut recent_files = recently_used_xbel::parse_file(); + + let mut recents = Vec::new(); + + match recent_files { + Ok(recent_files) => { + for bookmark in recent_files.bookmarks { + let uri = bookmark.href; + let path = match uri_to_path(uri) { + None => continue, + Some(path) => path, + }; + let last_edit = match bookmark.modified.parse::>() { + Ok(last_edit) => last_edit, + Err(_) => continue, + }; + let last_visit = match bookmark.visited.parse::>() { + Ok(last_visit) => last_visit, + Err(_) => continue, + }; + let path_buf = PathBuf::from(path); + let path_exist = path_buf.exists(); + + if path_exist { + let file_name = path_buf.file_name(); + + if let Some(name) = file_name { + let name = name.to_string_lossy().to_string(); + + let metadata = match path_buf.metadata() { + Ok(ok) => ok, + Err(err) => { + log::warn!( + "failed to read metadata for entry at {:?}: {}", + path_buf.clone(), + err + ); + continue; + } + }; + let item = item_from_entry(path_buf, name, metadata, sizes); + recents.push(( + item, + if last_edit.le(&last_visit) { + last_edit + } else { + last_visit + }, + )) + } + } else { + log::warn!("recent file path not exist: {:?}", path_buf); + } + } + } + Err(err) => { + log::warn!("Error reading recent files: {:?}", err); + } + } + + recents.sort_by(|a, b| b.1.cmp(&a.1)); + + recents.into_iter().take(50).map(|(item, _)| item).collect() +} + #[derive(Clone, Debug, Eq, PartialEq)] pub enum Location { Path(PathBuf), Search(PathBuf, String), Trash, + Recents, } impl std::fmt::Display for Location { @@ -602,6 +681,7 @@ impl std::fmt::Display for Location { Self::Path(path) => write!(f, "{}", path.display()), Self::Search(path, term) => write!(f, "search {} for {}", path.display(), term), Self::Trash => write!(f, "trash"), + Self::Recents => write!(f, "recents"), } } } @@ -612,6 +692,7 @@ impl Location { Self::Path(path) => scan_path(path, sizes), Self::Search(path, term) => scan_search(path, term, sizes), Self::Trash => scan_trash(sizes), + Self::Recents => scan_recents(sizes), } } } @@ -1088,6 +1169,9 @@ impl Tab { Location::Trash => { fl!("trash") } + Location::Recents => { + fl!("recents") + } } } @@ -1765,6 +1849,7 @@ impl Tab { Location::Trash => { cd = Some(location); } + Location::Recents => cd = Some(location), } } Message::LocationUp => { @@ -1906,6 +1991,9 @@ impl Tab { Location::Trash => { log::warn!("Copy to trash is not supported."); } + Location::Recents => { + log::warn!("Copy to recents is not supported."); + } }; } Message::Drop(None) => { @@ -1978,6 +2066,7 @@ impl Tab { Location::Path(path) => path.is_dir(), Location::Search(path, _term) => path.is_dir(), Location::Trash => true, + Location::Recents => true, } { let prev_path = if let Location::Path(path) = &self.location { Some(path.clone()) @@ -2341,6 +2430,21 @@ impl Tab { .into(), ); } + Location::Recents => { + let mut row = widget::row::with_capacity(2) + .align_items(Alignment::Center) + .spacing(space_xxxs); + row = row.push(widget::icon::from_name("accessories-clock-symbolic").size(16)); + row = row.push(widget::text::heading(fl!("recents"))); + + children.push( + widget::button(row) + .padding(space_xxxs) + .on_press(Message::Location(Location::Recents)) + .style(theme::Button::Text) + .into(), + ); + } } for child in children {