add FileDialog filters

This commit is contained in:
yggverse 2024-11-21 14:54:41 +02:00
parent 6cab09f249
commit 782b7be8ba

View File

@ -1,9 +1,9 @@
use super::Action; use super::Action;
use gtk::{ use gtk::{
gio::{Cancellable, TlsCertificate}, gio::{Cancellable, ListStore, TlsCertificate},
glib::{gformat, GString}, glib::{gformat, GString},
prelude::{ButtonExt, FileExt, TlsCertificateExt, WidgetExt}, prelude::{ButtonExt, FileExt, TlsCertificateExt, WidgetExt},
Button, FileDialog, Window, Button, FileDialog, FileFilter, Window,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
@ -37,37 +37,55 @@ impl File {
let pem = pem.clone(); let pem = pem.clone();
let update = action.update.clone(); let update = action.update.clone();
move |_| { move |_| {
FileDialog::new().open(None::<&Window>, None::<&Cancellable>, { // Init file filters related with PEM extension
let gobject = gobject.clone(); let filters = ListStore::new::<FileFilter>();
let pem = pem.clone();
let update = update.clone(); let filter_all = FileFilter::new();
move |result| { filter_all.add_pattern("*.*");
match result { filter_all.set_name(Some("All files"));
Ok(file) => match file.path() { filters.append(&filter_all);
Some(path) => {
let filename = path.to_str().unwrap(); let filter_pem = FileFilter::new();
match TlsCertificate::from_file(&filename) { filter_pem.add_mime_type("application/x-x509-ca-cert");
Ok(certificate) => { filter_pem.set_name(Some("Certificates (*.pem)"));
pem.replace(to_pem(certificate)); filters.append(&filter_pem);
gobject.set_css_classes(&["success"]);
gobject.set_label(filename) // Init file dialog
} FileDialog::builder()
Err(reason) => { .filters(&filters)
gobject.set_css_classes(&["error"]); .default_filter(&filter_pem)
gobject.set_label(reason.message()) .build()
.open(None::<&Window>, None::<&Cancellable>, {
let gobject = gobject.clone();
let pem = pem.clone();
let update = update.clone();
move |result| {
match result {
Ok(file) => match file.path() {
Some(path) => {
let filename = path.to_str().unwrap();
match TlsCertificate::from_file(&filename) {
Ok(certificate) => {
pem.replace(to_pem(certificate));
gobject.set_css_classes(&["success"]);
gobject.set_label(filename)
}
Err(reason) => {
gobject.set_css_classes(&["error"]);
gobject.set_label(reason.message())
}
} }
} }
None => todo!(),
},
Err(reason) => {
gobject.set_css_classes(&["error"]);
gobject.set_label(reason.message())
} }
None => todo!(),
},
Err(reason) => {
gobject.set_css_classes(&["error"]);
gobject.set_label(reason.message())
} }
update.activate()
} }
update.activate() });
}
});
} }
}); });