Merge pull request #400 from francesco-gaglione/recent_section

Recents section
This commit is contained in:
Jeremy Soller 2024-09-10 10:02:13 -06:00 committed by GitHub
commit c4c92be708
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 210 additions and 3 deletions

View file

@ -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<Item> {
items
}
fn uri_to_path(uri: String) -> Option<PathBuf> {
//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<Item> {
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::<DateTime<Utc>>() {
Ok(last_edit) => last_edit,
Err(_) => continue,
};
let last_visit = match bookmark.visited.parse::<DateTime<Utc>>() {
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 {