use Cancellable from response

This commit is contained in:
yggverse 2025-01-16 21:42:12 +02:00
parent 9d45688314
commit bb4ddbdddd
3 changed files with 15 additions and 14 deletions

View File

@ -23,7 +23,6 @@ use crate::tool::now;
use gtk::{ use gtk::{
gdk::Texture, gdk::Texture,
gdk_pixbuf::Pixbuf, gdk_pixbuf::Pixbuf,
gio::Cancellable,
glib::{gformat, GString, Priority, Uri}, glib::{gformat, GString, Priority, Uri},
prelude::{EditableExt, FileExt}, prelude::{EditableExt, FileExt},
}; };
@ -329,11 +328,7 @@ impl Page {
window_action.find.simple_action.set_enabled(true); window_action.find.simple_action.set_enabled(true);
browser_action.update.activate(Some(&id)); browser_action.update.activate(Some(&id));
} }
Response::Download { base, stream } => { Response::Download { base, cancellable, stream } => {
// @TODO use `client` wrapper
let cancellable = Cancellable::new(); // @TODO share with `client`
// Init download widget // Init download widget
let status_page = content.to_status_download( let status_page = content.to_status_download(
&uri_to_title(&base), // grab default filename from base URI &uri_to_title(&base), // grab default filename from base URI
@ -349,6 +344,10 @@ impl Page {
Some(&cancellable) Some(&cancellable)
) { ) {
Ok(file_output_stream) => { Ok(file_output_stream) => {
// Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
// show bytes count in loading widget, validate max size for incoming data
// * no dependency of Gemini library here, feel free to use any other `IOStream` processor
gemini::gio::file_output_stream::move_all_from_stream_async( gemini::gio::file_output_stream::move_all_from_stream_async(
stream.clone(), stream.clone(),
file_output_stream, file_output_stream,
@ -396,7 +395,7 @@ impl Page {
// Update window // Update window
browser_action.update.activate(Some(&id)); browser_action.update.activate(Some(&id));
} }
Response::Stream { base, mime, stream } => match mime.as_str() { Response::Stream { base, mime, stream, cancellable } => match mime.as_str() {
// @TODO use client-side const or enum? // @TODO use client-side const or enum?
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => { "image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
// Final image size unknown, show loading widget // Final image size unknown, show loading widget
@ -404,12 +403,10 @@ impl Page {
Some(Duration::from_secs(1)), // show if download time > 1 second Some(Duration::from_secs(1)), // show if download time > 1 second
); );
// @TODO use `client` wrapper // Asynchronously read [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
// to local [MemoryInputStream](https://docs.gtk.org/gio/class.MemoryInputStream.html)
let cancellable = Cancellable::new(); // @TODO share with `client` // show bytes count in loading widget, validate max size for incoming data
// * no dependency of Gemini library here, feel free to use any other `IOStream` processor
// Asynchronously move `InputStream` data from `SocketConnection` into the local `MemoryInputStream`
// this action allows to count the bytes for loading widget and validate max size for incoming data
gemini::gio::memory_input_stream::from_stream_async( gemini::gio::memory_input_stream::from_stream_async(
stream, stream,
cancellable.clone(), cancellable.clone(),

View File

@ -76,6 +76,7 @@ impl Driver {
Ok(response) => callback(Response::Download { Ok(response) => callback(Response::Download {
base: uri.clone(), base: uri.clone(),
stream: response.connection.stream(), stream: response.connection.stream(),
cancellable: cancellable.clone(),
}), }),
Err(e) => callback(Response::Failure(response::Failure::Error { Err(e) => callback(Response::Failure(response::Failure::Error {
message: e.to_string(), message: e.to_string(),
@ -192,6 +193,7 @@ fn handle_gemini(
base, base,
mime, mime,
stream: response.connection.stream(), stream: response.connection.stream(),
cancellable,
}) })
} }
mime => callback(Response::Failure(response::Failure::Mime { mime => callback(Response::Failure(response::Failure::Mime {

View File

@ -7,7 +7,7 @@ pub use failure::Failure;
pub use input::Input; pub use input::Input;
use gtk::{ use gtk::{
gio::IOStream, gio::{Cancellable, IOStream},
glib::{GString, Uri}, glib::{GString, Uri},
}; };
@ -16,6 +16,7 @@ pub enum Response {
Download { Download {
base: Uri, base: Uri,
stream: IOStream, stream: IOStream,
cancellable: Cancellable,
}, },
Failure(Failure), Failure(Failure),
Gemtext { Gemtext {
@ -32,5 +33,6 @@ pub enum Response {
base: Uri, base: Uri,
mime: String, mime: String,
stream: IOStream, stream: IOStream,
cancellable: Cancellable,
}, },
} }