From 9aabbd634f730869cf28cdd93f50861c262b2dc8 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 7 Feb 2025 23:47:45 +0200 Subject: [PATCH] auto-resolve mime type on empty --- .../window/tab/item/page/input/titan/file.rs | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/app/browser/window/tab/item/page/input/titan/file.rs b/src/app/browser/window/tab/item/page/input/titan/file.rs index fc91c720..5f063f63 100644 --- a/src/app/browser/window/tab/item/page/input/titan/file.rs +++ b/src/app/browser/window/tab/item/page/input/titan/file.rs @@ -1,5 +1,9 @@ use super::{Control, Header}; -use gtk::{glib::Bytes, Button}; +use gtk::{ + gio::FileQueryInfoFlags, + glib::{Bytes, Priority}, + Button, +}; use std::{cell::RefCell, rc::Rc}; pub struct File { @@ -33,6 +37,7 @@ impl File { button.connect_clicked({ let control = control.clone(); let buffer = buffer.clone(); + let header = header.clone(); move |this| { const CLASS: (&str, &str, &str) = ("error", "warning", "success"); @@ -47,26 +52,58 @@ impl File { .build() .open(Window::NONE, Cancellable::NONE, { let control = control.clone(); + let header = header.clone(); let buffer = buffer.clone(); let this = this.clone(); move |result| match result { Ok(file) => match file.path() { Some(path) => { this.set_label("Buffering, please wait.."); // @TODO progress - file.load_bytes_async(Cancellable::NONE, move |result| { - match result { - Ok((bytes, _)) => { - control.update(Some(bytes.len()), None); - buffer.replace(Some(bytes)); + file.load_bytes_async(Cancellable::NONE, { + let file = file.clone(); + move |result| { + match result { + 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_label(path.to_str().unwrap()); - this.set_sensitive(true); - } - Err(e) => { - this.set_css_classes(&[CLASS.0]); - this.set_label(e.message()); - this.set_sensitive(true); + this.set_css_classes(&[CLASS.2]); + this.set_label(path.to_str().unwrap()); + this.set_sensitive(true); + } + } + Err(e) => { + this.set_css_classes(&[CLASS.0]); + this.set_label(e.message()); + this.set_sensitive(true); + } } } })