From fa46c76af411cf659d787e838337d2f0958a6b94 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Tue, 8 Oct 2024 20:53:10 +0200 Subject: [PATCH] propertiesView: Retrive host path via portal This way we should always be able to get the correct folder name if xdg-desktop-portal > 0.19. --- Cargo.lock | 1 + Cargo.toml | 1 + .../added-get-correct-folder-name-via-portal | 1 + src/metadata.rs | 11 +++++++++++ src/metadata/file.rs | 7 ++++++- src/widgets/properties_view.rs | 14 +++++++------- 6 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 news.d/48.alpha/added-get-correct-folder-name-via-portal diff --git a/Cargo.lock b/Cargo.lock index 02a858f..36ada76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1321,6 +1321,7 @@ dependencies = [ "libgweather", "log", "strum", + "thiserror", "tracing-subscriber", "url", ] diff --git a/Cargo.toml b/Cargo.toml index dc2a5cb..afdf1b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,5 +36,6 @@ libc = "0.2.158" libgweather = "4.4.0" log = "0.4.17" strum = { version = "0.26.2", features = ["derive"] } +thiserror = "1.0.64" tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] } url = "2.3.1" diff --git a/news.d/48.alpha/added-get-correct-folder-name-via-portal b/news.d/48.alpha/added-get-correct-folder-name-via-portal new file mode 100644 index 0000000..f215902 --- /dev/null +++ b/news.d/48.alpha/added-get-correct-folder-name-via-portal @@ -0,0 +1 @@ +Show the correct folder name in the properties view inside flatpaks if the file is only accessible via portal. diff --git a/src/metadata.rs b/src/metadata.rs index 6846670..04372a1 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -24,6 +24,9 @@ mod file; mod gps; +use std::path::PathBuf; +use std::str::FromStr; + pub use file::FileInfo; use glycin::{Frame, FrameDetails, ImageInfoDetails, MemoryFormat}; pub use gps::GPSLocation; @@ -191,6 +194,14 @@ impl Metadata { self.file_info.as_ref().map(|x| x.display_name.to_string()) } + /// Original host path inside flatpaks obtained via xattr + pub fn host_path(&self) -> Option { + self.file_info + .as_ref() + .and_then(|x| x.host_path.as_ref()) + .and_then(|x| PathBuf::from_str(x.as_str()).ok()) + } + pub fn file_size(&self) -> Option { self.file_info .as_ref() diff --git a/src/metadata/file.rs b/src/metadata/file.rs index f2856f9..27f287e 100644 --- a/src/metadata/file.rs +++ b/src/metadata/file.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Sophie Herold +// Copyright (c) 2023-2024 Sophie Herold // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -17,12 +17,15 @@ use crate::deps::*; +const ATTRIBUTE_HOST_PATH: &str = "xattr::document-portal.host-path"; + pub struct FileInfo { pub(super) display_name: glib::GString, pub(super) file_size: Option, pub(super) mime_type: Option, pub(super) created: Option, pub(super) modified: Option, + pub(super) host_path: Option, } impl FileInfo { @@ -35,6 +38,7 @@ impl FileInfo { gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, gio::FILE_ATTRIBUTE_TIME_CREATED, gio::FILE_ATTRIBUTE_TIME_MODIFIED, + ATTRIBUTE_HOST_PATH, ], ) .await?; @@ -50,6 +54,7 @@ impl FileInfo { .and_then(|x| gio::content_type_get_mime_type(&x)), created: file_info.creation_date_time(), modified: file_info.modification_date_time(), + host_path: file_info.attribute_as_string(ATTRIBUTE_HOST_PATH), }) } } diff --git a/src/widgets/properties_view.rs b/src/widgets/properties_view.rs index c59c8ab..130d7e4 100644 --- a/src/widgets/properties_view.rs +++ b/src/widgets/properties_view.rs @@ -190,7 +190,13 @@ mod imp { let metadata = image.metadata(); // Folder - let has_folder = Self::update_row(&self.folder, self.folder_name()); + let parent = metadata + .host_path() + .or_else(|| file.path()) + .as_ref() + .and_then(|x| x.parent()) + .and_then(|p| util::get_file_display_name(&gio::File::for_path(p))); + let has_folder = Self::update_row(&self.folder, parent); // The portal only supports opening folders of files that have a path obj.action_set_enabled("properties.open-folder", file.path().is_some()); // Only show URI if now folder available @@ -242,12 +248,6 @@ mod imp { } } - fn folder_name(&self) -> Option { - self.file() - .and_then(|f| f.parent()) - .and_then(|p| util::get_file_display_name(&p)) - } - fn format_name(&self) -> Option { let image = self.image.borrow(); let metadata = image.as_ref()?.metadata();