auto-resolve mime type on empty

This commit is contained in:
yggverse 2025-02-07 23:47:45 +02:00
parent 3152416713
commit 9aabbd634f

View file

@ -1,5 +1,9 @@
use super::{Control, Header}; use super::{Control, Header};
use gtk::{glib::Bytes, Button}; use gtk::{
gio::FileQueryInfoFlags,
glib::{Bytes, Priority},
Button,
};
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
pub struct File { pub struct File {
@ -33,6 +37,7 @@ impl File {
button.connect_clicked({ button.connect_clicked({
let control = control.clone(); let control = control.clone();
let buffer = buffer.clone(); let buffer = buffer.clone();
let header = header.clone();
move |this| { move |this| {
const CLASS: (&str, &str, &str) = ("error", "warning", "success"); const CLASS: (&str, &str, &str) = ("error", "warning", "success");
@ -47,26 +52,58 @@ impl File {
.build() .build()
.open(Window::NONE, Cancellable::NONE, { .open(Window::NONE, Cancellable::NONE, {
let control = control.clone(); let control = control.clone();
let header = header.clone();
let buffer = buffer.clone(); let buffer = buffer.clone();
let this = this.clone(); let this = this.clone();
move |result| match result { move |result| match result {
Ok(file) => match file.path() { Ok(file) => match file.path() {
Some(path) => { Some(path) => {
this.set_label("Buffering, please wait.."); // @TODO progress this.set_label("Buffering, please wait.."); // @TODO progress
file.load_bytes_async(Cancellable::NONE, move |result| { file.load_bytes_async(Cancellable::NONE, {
match result { let file = file.clone();
Ok((bytes, _)) => { move |result| {
control.update(Some(bytes.len()), None); match result {
buffer.replace(Some(bytes)); Ok((bytes, _)) => {
// try autocomplete content type (if None)
if header.borrow().mime.is_none() {
file.query_info_async(
"standard::content-type",
FileQueryInfoFlags::NONE,
Priority::DEFAULT,
Cancellable::NONE,
move |file_info| {
if let Ok(file_info) = file_info {
header.borrow_mut().mime =
file_info.content_type();
}
// async operations completed, unlock the form
control.update(
Some(bytes.len()),
None,
);
buffer.replace(Some(bytes));
this.set_css_classes(&[CLASS.2]);
this.set_label(
path.to_str().unwrap(),
);
this.set_sensitive(true);
},
);
// no async operations left, update/unlock immediately
} else {
control.update(Some(bytes.len()), None);
buffer.replace(Some(bytes));
this.set_css_classes(&[CLASS.2]); this.set_css_classes(&[CLASS.2]);
this.set_label(path.to_str().unwrap()); this.set_label(path.to_str().unwrap());
this.set_sensitive(true); this.set_sensitive(true);
} }
Err(e) => { }
this.set_css_classes(&[CLASS.0]); Err(e) => {
this.set_label(e.message()); this.set_css_classes(&[CLASS.0]);
this.set_sensitive(true); this.set_label(e.message());
this.set_sensitive(true);
}
} }
} }
}) })