2023-04-19 20:02:01 +02:00
|
|
|
|
// Copyright (c) 2022-2023 Sophie Herold
|
|
|
|
|
|
// Copyright (c) 2022-2023 Christopher Davis
|
2022-07-26 11:31:11 -04:00
|
|
|
|
//
|
|
|
|
|
|
// 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
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
2023-03-10 19:08:26 +01:00
|
|
|
|
use crate::decoder::ImageDimensionDetails;
|
2022-07-26 11:31:11 -04:00
|
|
|
|
use crate::deps::*;
|
|
|
|
|
|
use crate::util;
|
|
|
|
|
|
|
|
|
|
|
|
use adw::prelude::*;
|
|
|
|
|
|
use adw::subclass::prelude::*;
|
|
|
|
|
|
use glib::clone;
|
|
|
|
|
|
use gtk::CompositeTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
use futures::future::{AbortHandle, Abortable};
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
2022-11-06 01:08:43 +01:00
|
|
|
|
use crate::image_metadata::LpImageMetadata;
|
|
|
|
|
|
use crate::widgets::image::LpImage;
|
|
|
|
|
|
|
|
|
|
|
|
const FALLBACK: &str = "–";
|
2022-07-26 11:31:11 -04:00
|
|
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default, CompositeTemplate)]
|
2023-01-23 01:10:15 +01:00
|
|
|
|
#[template(file = "../../data/gtk/properties_view.ui")]
|
2022-07-26 11:31:11 -04:00
|
|
|
|
pub struct LpPropertiesView {
|
2023-03-14 18:56:35 +01:00
|
|
|
|
pub(super) file: RefCell<Option<glib::WeakRef<gio::File>>>,
|
|
|
|
|
|
pub(super) image: RefCell<Option<glib::WeakRef<LpImage>>>,
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2023-03-14 18:56:35 +01:00
|
|
|
|
pub(super) metadata: RefCell<LpImageMetadata>,
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2023-03-14 18:56:35 +01:00
|
|
|
|
pub(super) file_info: RefCell<Option<gio::FileInfo>>,
|
|
|
|
|
|
pub(super) info_handle: RefCell<Option<AbortHandle>>,
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
|
|
impl ObjectSubclass for LpPropertiesView {
|
|
|
|
|
|
const NAME: &'static str = "LpPropertiesView";
|
|
|
|
|
|
type Type = super::LpPropertiesView;
|
|
|
|
|
|
type ParentType = adw::Bin;
|
|
|
|
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
|
|
|
|
|
Self::bind_template(klass);
|
|
|
|
|
|
Self::Type::bind_template_callbacks(klass);
|
|
|
|
|
|
|
2023-01-15 18:42:41 -05:00
|
|
|
|
klass.install_action_async(
|
|
|
|
|
|
"properties.open-folder",
|
|
|
|
|
|
None,
|
|
|
|
|
|
|properties, _, _| async move {
|
|
|
|
|
|
let _ = properties.open_directory().await;
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
|
|
|
|
|
klass.install_action("properties.open-location", None, move |properties, _, _| {
|
|
|
|
|
|
properties.open_location();
|
|
|
|
|
|
});
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
|
|
|
|
|
obj.init_template();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ObjectImpl for LpPropertiesView {
|
|
|
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
|
|
vec![
|
2022-11-24 11:52:37 -05:00
|
|
|
|
glib::ParamSpecObject::builder::<gio::File>("file")
|
|
|
|
|
|
.explicit_notify()
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
glib::ParamSpecObject::builder::<gio::FileInfo>("file-info")
|
|
|
|
|
|
.read_only()
|
|
|
|
|
|
.explicit_notify()
|
|
|
|
|
|
.build(),
|
2022-11-06 01:08:43 +01:00
|
|
|
|
glib::ParamSpecObject::builder::<LpImage>("image")
|
|
|
|
|
|
.explicit_notify()
|
|
|
|
|
|
.build(),
|
2023-03-09 23:46:04 +01:00
|
|
|
|
glib::ParamSpecObject::builder::<LpImageMetadata>("metadata")
|
|
|
|
|
|
.explicit_notify()
|
|
|
|
|
|
.build(),
|
2022-07-26 11:31:11 -04:00
|
|
|
|
]
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-02 21:11:43 +01:00
|
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2023-02-12 02:07:42 +01:00
|
|
|
|
let obj = self.obj();
|
2022-07-26 11:31:11 -04:00
|
|
|
|
let prop_name = pspec.name();
|
|
|
|
|
|
|
|
|
|
|
|
match prop_name {
|
|
|
|
|
|
"file" => obj.file().to_value(),
|
2022-11-06 01:08:43 +01:00
|
|
|
|
"image" => obj.image().to_value(),
|
|
|
|
|
|
"metadata" => self.metadata.borrow().to_value(),
|
2022-07-26 11:31:11 -04:00
|
|
|
|
"file-info" => obj.file_info().to_value(),
|
|
|
|
|
|
_ => unimplemented!("Failed to get property \"{prop_name}\""),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-02 21:11:43 +01:00
|
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
2023-02-12 02:07:42 +01:00
|
|
|
|
let obj = self.obj();
|
2022-07-26 11:31:11 -04:00
|
|
|
|
let prop_name = pspec.name();
|
|
|
|
|
|
|
|
|
|
|
|
match prop_name {
|
2023-01-23 01:10:15 +01:00
|
|
|
|
"image" => obj.set_image(value.get().ok().as_ref()),
|
2023-03-02 21:30:57 -05:00
|
|
|
|
"file" => obj.set_file(value.get().ok().as_ref()),
|
|
|
|
|
|
"metadata" => obj.set_metadata(value.get().unwrap()),
|
2022-07-26 11:31:11 -04:00
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-02 21:11:43 +01:00
|
|
|
|
fn constructed(&self) {
|
2023-02-12 02:07:42 +01:00
|
|
|
|
let obj = self.obj();
|
2022-11-02 21:11:43 +01:00
|
|
|
|
|
|
|
|
|
|
self.parent_constructed();
|
2022-07-26 11:31:11 -04:00
|
|
|
|
obj.action_set_enabled("properties.open-folder", false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl WidgetImpl for LpPropertiesView {}
|
|
|
|
|
|
impl BinImpl for LpPropertiesView {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
|
|
pub struct LpPropertiesView(ObjectSubclass<imp::LpPropertiesView>)
|
|
|
|
|
|
@extends gtk::Widget, adw::Bin,
|
|
|
|
|
|
@implements gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[gtk::template_callbacks]
|
|
|
|
|
|
impl LpPropertiesView {
|
2023-01-23 01:10:15 +01:00
|
|
|
|
fn set_file(&self, file: Option<&gio::File>) {
|
2022-07-26 11:31:11 -04:00
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
2023-04-27 16:00:35 +02:00
|
|
|
|
self.action_set_enabled(
|
|
|
|
|
|
"properties.open-folder",
|
|
|
|
|
|
file.map_or(false, |x| x.is_native()),
|
|
|
|
|
|
);
|
2022-07-26 11:31:11 -04:00
|
|
|
|
|
|
|
|
|
|
if let Some(file) = file {
|
|
|
|
|
|
if let Some(current_file) = self.file() {
|
|
|
|
|
|
if current_file.equal(file) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cancel the file info future. See `build_file_info()` for details.
|
|
|
|
|
|
if let Some(handle) = imp.info_handle.take() {
|
|
|
|
|
|
handle.abort();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.build_file_info(file);
|
|
|
|
|
|
}
|
2023-01-23 01:10:15 +01:00
|
|
|
|
|
|
|
|
|
|
let weak = file.map(|x| x.downgrade());
|
|
|
|
|
|
imp.file.replace(weak);
|
|
|
|
|
|
self.notify("file");
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn file(&self) -> Option<gio::File> {
|
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
imp.file.borrow().as_ref().and_then(|w| w.upgrade())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-23 01:10:15 +01:00
|
|
|
|
fn set_image(&self, image: Option<&LpImage>) {
|
2022-11-06 01:08:43 +01:00
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
|
|
|
|
|
imp.image.replace(image.as_ref().map(|x| x.downgrade()));
|
|
|
|
|
|
self.notify("image");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn image(&self) -> Option<LpImage> {
|
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
imp.image.borrow().as_ref().and_then(|w| w.upgrade())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-26 11:31:11 -04:00
|
|
|
|
fn file_info(&self) -> Option<gio::FileInfo> {
|
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
imp.file_info.borrow().clone()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn build_file_info(&self, file: &gio::File) {
|
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
|
|
|
|
|
// We need to be able to cancel this future so that
|
|
|
|
|
|
// changing files before the metadata loads does not cause
|
|
|
|
|
|
// old metadata to be loaded. In order to make a cancellable
|
|
|
|
|
|
// future, we use `AbortHandle` and `Abortable` from the `futures`
|
|
|
|
|
|
// crate.
|
|
|
|
|
|
//
|
|
|
|
|
|
// `handle` is the `AbortHandle`, which we need to store in order
|
|
|
|
|
|
// to abort the future. `reg` is the `AbortRegistration` that we
|
|
|
|
|
|
// give to `Abortable::new()` to tie our future to the `AbortHandle`.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Here we build the future:
|
|
|
|
|
|
let (handle, reg) = AbortHandle::new_pair();
|
|
|
|
|
|
let fut = Abortable::new(
|
2023-03-09 23:46:04 +01:00
|
|
|
|
clone!(@weak self as view, @strong file => async move {
|
|
|
|
|
|
let info_result = util::query_attributes_future(
|
2022-07-26 11:31:11 -04:00
|
|
|
|
&file,
|
|
|
|
|
|
vec![
|
2023-02-12 02:07:42 +01:00
|
|
|
|
gio::FILE_ATTRIBUTE_STANDARD_SIZE,
|
|
|
|
|
|
gio::FILE_ATTRIBUTE_TIME_CREATED,
|
|
|
|
|
|
gio::FILE_ATTRIBUTE_TIME_MODIFIED,
|
2022-07-26 11:31:11 -04:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2023-03-09 23:46:04 +01:00
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
match info_result {
|
2023-03-10 19:08:26 +01:00
|
|
|
|
Ok(info) => view.on_info_loaded(info),
|
2023-03-09 23:46:04 +01:00
|
|
|
|
Err(err) => log::warn!("Failed to load file info: {err}"),
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
}),
|
|
|
|
|
|
reg,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// ...store the handle:
|
|
|
|
|
|
imp.info_handle.replace(Some(handle));
|
|
|
|
|
|
|
|
|
|
|
|
// ...then spawn the future.
|
2023-11-22 09:12:07 +01:00
|
|
|
|
glib::spawn_future_local(async {
|
2022-07-26 11:31:11 -04:00
|
|
|
|
let _ = fut.await;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-02 21:30:57 -05:00
|
|
|
|
fn set_metadata(&self, metadata: LpImageMetadata) {
|
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
imp.metadata.replace(metadata);
|
|
|
|
|
|
self.notify("metadata");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-26 11:31:11 -04:00
|
|
|
|
// This is where we handle the results of the future.
|
|
|
|
|
|
// This could technically be part of the future, but we keep
|
|
|
|
|
|
// them separate for a bit of cleanliness.
|
2023-03-10 19:08:26 +01:00
|
|
|
|
fn on_info_loaded(&self, info: gio::FileInfo) {
|
2022-07-26 11:31:11 -04:00
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
imp.file_info.replace(Some(info));
|
|
|
|
|
|
self.notify("file-info");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-15 18:42:41 -05:00
|
|
|
|
async fn open_directory(&self) -> anyhow::Result<()> {
|
|
|
|
|
|
let launcher = gtk::FileLauncher::new(self.file().as_ref());
|
|
|
|
|
|
let win = self.native().and_downcast::<gtk::Window>();
|
|
|
|
|
|
if let Err(e) = launcher.open_containing_folder_future(win.as_ref()).await {
|
|
|
|
|
|
log::error!("Could not open parent directory: {e}");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-06 01:08:43 +01:00
|
|
|
|
/// Open GPS location in apps like Maps via `geo:` URI
|
|
|
|
|
|
fn open_location(&self) {
|
|
|
|
|
|
if let Some(uri) = self
|
|
|
|
|
|
.image()
|
|
|
|
|
|
.and_then(|x| x.metadata().gps_location())
|
|
|
|
|
|
.map(|x| x.geo_uri())
|
|
|
|
|
|
{
|
|
|
|
|
|
gio::AppInfo::launch_default_for_uri_async(
|
|
|
|
|
|
&uri,
|
|
|
|
|
|
gio::AppLaunchContext::NONE,
|
|
|
|
|
|
gio::Cancellable::NONE,
|
|
|
|
|
|
|result| {
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
|
|
log::error!("Failed to show geo URI: {err}")
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-26 11:31:11 -04:00
|
|
|
|
// In the LpPropertiesView UI file we define a few `gtk::Expression`s
|
|
|
|
|
|
// that are closures. These closures take either the current `gio::File`
|
|
|
|
|
|
// or the current file's associated `gio::FileInfo` and process them
|
|
|
|
|
|
// accordingly.
|
|
|
|
|
|
//
|
|
|
|
|
|
// In this function we chain `Option`s with `and_then()` in order
|
|
|
|
|
|
// to handle optional results with a fallback, without needing to
|
|
|
|
|
|
// have multiple `match` or `if let` branches, and without needing
|
|
|
|
|
|
// to unwrap.
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn folder_name(&self, file: Option<gio::File>) -> String {
|
|
|
|
|
|
file.and_then(|f| f.parent()) // If the file exists, get the parent
|
|
|
|
|
|
.and_then(|p| util::get_file_display_name(&p)) // if that exists, the display name
|
|
|
|
|
|
.unwrap_or_else(|| FALLBACK.to_owned()) // and if we get nothing, use `FALLBACK`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-10 19:08:26 +01:00
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn image_size(&self, _image_size: Option<glib::Variant>) -> String {
|
|
|
|
|
|
if let Some(image) = self.image() {
|
|
|
|
|
|
match image.dimension_details() {
|
2023-08-04 13:20:41 +02:00
|
|
|
|
ImageDimensionDetails::Svg(string) => string,
|
2023-03-10 19:08:26 +01:00
|
|
|
|
_ => {
|
|
|
|
|
|
let (width, height) = image.image_size();
|
|
|
|
|
|
if width > 0 && height > 0 {
|
|
|
|
|
|
format!("{width}\u{202F}\u{D7}\u{202F}{height}")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
FALLBACK.to_owned()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
FALLBACK.to_owned()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-26 11:31:11 -04:00
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn file_size(&self, info: Option<gio::FileInfo>) -> String {
|
|
|
|
|
|
info.map(|info| {
|
2022-08-05 17:19:47 -04:00
|
|
|
|
let size = info.size() as u64;
|
|
|
|
|
|
glib::format_size(size).to_string()
|
2022-07-26 11:31:11 -04:00
|
|
|
|
})
|
|
|
|
|
|
.unwrap_or_else(|| FALLBACK.to_owned())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn created_date(&self, info: Option<gio::FileInfo>) -> String {
|
|
|
|
|
|
info.and_then(|i| i.creation_date_time())
|
2023-03-14 02:27:31 +01:00
|
|
|
|
.and_then(|t| util::datetime_fmt(&t))
|
2022-07-26 11:31:11 -04:00
|
|
|
|
.unwrap_or_else(|| FALLBACK.to_owned())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn modified_date(&self, info: Option<gio::FileInfo>) -> String {
|
|
|
|
|
|
info.and_then(|i| i.modification_date_time())
|
2023-03-14 02:27:31 +01:00
|
|
|
|
.and_then(|t| util::datetime_fmt(&t))
|
2022-07-26 11:31:11 -04:00
|
|
|
|
.unwrap_or_else(|| FALLBACK.to_owned())
|
|
|
|
|
|
}
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
|
fn has_content(&self, content: Option<String>) -> bool {
|
|
|
|
|
|
content.map_or(false, |x| !x.is_empty())
|
|
|
|
|
|
}
|
2022-07-26 11:31:11 -04:00
|
|
|
|
}
|