use gio file stream to export certificate data

This commit is contained in:
yggverse 2024-12-11 04:12:57 +02:00
parent 86c7e357e8
commit 9eb7a1b0bb

View File

@ -4,11 +4,12 @@ use certificate::Certificate;
use super::list::{item::Value, List}; use super::list::{item::Value, List};
use crate::profile::Profile; use crate::profile::Profile;
use gtk::{ use gtk::{
gio::{Cancellable, ListStore}, gio::{Cancellable, FileCreateFlags, ListStore},
prelude::{ButtonExt, FileExt, WidgetExt}, glib::Priority,
prelude::{ButtonExt, FileExt, OutputStreamExtManual, WidgetExt},
Button, FileDialog, FileFilter, Window, Button, FileDialog, FileFilter, Window,
}; };
use std::{fs::File, io::Write, rc::Rc}; use std::rc::Rc;
const LABEL: &str = "Export"; const LABEL: &str = "Export";
const TOOLTIP_TEXT: &str = "Export selected identity to file"; const TOOLTIP_TEXT: &str = "Export selected identity to file";
@ -67,39 +68,39 @@ impl Save {
let button = button.clone(); let button = button.clone();
move |result| { move |result| {
match result { match result {
Ok(file) => match file.path() { Ok(file) => match file.replace(
Some(path) => match File::create(&path) { None,
Ok(mut destination) => { false,
match destination.write_all( FileCreateFlags::NONE,
certificate.data.as_bytes(), Cancellable::NONE, // @TODO
) { ) {
Ok(stream) => stream.write_async(
certificate.data,
Priority::DEFAULT,
Cancellable::NONE, // @TODO
{
let button = button.clone();
move |result| match result {
Ok(_) => { Ok(_) => {
button.set_css_classes(&[ button.set_css_classes(&[
"success", "success",
]); ]);
button.set_label(&format!( button.set_label(
"Saved to {}", "Saved to destination!",
path.to_string_lossy() )
))
} }
Err(e) => { Err((_, e)) => {
button.set_css_classes(&[ button.set_css_classes(&[
"error", "error",
]); ]);
button.set_label(&e.to_string()) button.set_label(&e.to_string())
} }
} }
} },
Err(e) => { ),
button.set_css_classes(&["error"]); Err(e) => {
button.set_label(&e.to_string()) button.set_css_classes(&["error"]);
} button.set_label(&e.to_string())
},
None => {
button.set_css_classes(&["warning"]);
button.set_label(
"Could not init destination path",
)
} }
}, },
Err(e) => { Err(e) => {