2021-12-21 16:09:48 -08:00
|
|
|
use crate::deps::*;
|
|
|
|
|
|
2021-09-05 05:51:34 -07:00
|
|
|
use gio::prelude::*;
|
|
|
|
|
use glib::translate::*;
|
|
|
|
|
|
2022-07-14 15:25:03 -04:00
|
|
|
use anyhow::Context;
|
|
|
|
|
|
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
2021-09-22 22:57:54 -07:00
|
|
|
pub fn utf8_collate_key_for_filename(filename: &str) -> String {
|
2021-09-05 05:51:34 -07:00
|
|
|
unsafe {
|
|
|
|
|
from_glib_full(glib::ffi::g_utf8_collate_key_for_filename(
|
|
|
|
|
filename.to_glib_none().0,
|
|
|
|
|
filename.len() as isize,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_file_display_name(file: &gio::File) -> Option<String> {
|
2021-12-27 15:03:31 -08:00
|
|
|
let info = query_attributes(file, vec![&gio::FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME]).ok()?;
|
2021-09-05 05:51:34 -07:00
|
|
|
|
|
|
|
|
Some(info.display_name().to_string())
|
|
|
|
|
}
|
2021-12-27 15:03:31 -08:00
|
|
|
|
2022-07-14 15:25:03 -04:00
|
|
|
pub fn query_attributes(file: &gio::File, attributes: Vec<&str>) -> anyhow::Result<gio::FileInfo> {
|
2021-12-27 15:03:31 -08:00
|
|
|
let mut attributes = attributes;
|
|
|
|
|
let mut attr_str = String::from(attributes.remove(0));
|
|
|
|
|
for attr in attributes {
|
2022-07-14 15:25:03 -04:00
|
|
|
write!(attr_str, ",{}", attr)?;
|
2021-12-27 15:03:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.query_info(
|
|
|
|
|
&attr_str,
|
|
|
|
|
gio::FileQueryInfoFlags::empty(),
|
|
|
|
|
gio::Cancellable::NONE,
|
|
|
|
|
)
|
2022-07-14 15:25:03 -04:00
|
|
|
.context("Failed to query string")
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 11:31:11 -04:00
|
|
|
pub async fn query_attributes_future(
|
|
|
|
|
file: &gio::File,
|
|
|
|
|
attributes: Vec<&str>,
|
|
|
|
|
) -> anyhow::Result<gio::FileInfo> {
|
2022-11-04 19:27:43 +01:00
|
|
|
let mut attr_str = String::from(*attributes.first().context("No attributes")?);
|
2022-07-26 11:31:11 -04:00
|
|
|
|
|
|
|
|
for attr in &attributes[1..] {
|
|
|
|
|
write!(attr_str, ",{}", attr)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.query_info_future(
|
|
|
|
|
&attr_str,
|
|
|
|
|
gio::FileQueryInfoFlags::empty(),
|
|
|
|
|
glib::Priority::default(),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.context("Failed to query attributes")
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 15:25:03 -04:00
|
|
|
pub fn compare_by_name(file_a: &gio::File, file_b: &gio::File) -> std::cmp::Ordering {
|
|
|
|
|
let name_a = get_file_display_name(file_a).unwrap_or_default();
|
|
|
|
|
let name_b = get_file_display_name(file_b).unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
utf8_collate_key_for_filename(&name_a).cmp(&utf8_collate_key_for_filename(&name_b))
|
2021-12-27 15:03:31 -08:00
|
|
|
}
|