Browse Source

draft new ggemini api version

master
yggverse 1 month ago
parent
commit
1abda661a7
  1. 351
      src/app/browser/window/tab/item/page.rs

351
src/app/browser/window/tab/item/page.rs

@ -24,8 +24,7 @@ use gtk::{
RegexMatchFlags, Uri, UriFlags, RegexMatchFlags, Uri, UriFlags,
}, },
prelude::{ prelude::{
ActionExt, IOStreamExt, InputStreamExt, OutputStreamExt, SocketClientExt, ActionExt, IOStreamExt, OutputStreamExt, SocketClientExt, StaticVariantType, ToVariant,
StaticVariantType, ToVariant,
}, },
Box, Box,
}; };
@ -362,12 +361,10 @@ impl Page {
// Private helpers @TODO // Private helpers @TODO
fn load_gemini(&self, uri: Uri) { fn load_gemini(&self, uri: Uri) {
// Use local namespaces // Use local namespaces
use gemini::client::{ use gemini::client::response::{
buffer::{Buffer, Error as BufferError}, body::Error as BodyError,
response::{ header::{Mime as ClientMime, Status as ClientStatus},
header::{Mime as ClientMime, Status as ClientStatus}, Body, Header,
Header,
},
}; };
// Init shared objects (async) // Init shared objects (async)
@ -423,141 +420,77 @@ impl Page {
None::<&Cancellable>, None::<&Cancellable>,
move |request| match request { move |request| match request {
Ok(_) => { Ok(_) => {
// Read header from response // Read header from input stream
connection.input_stream().read_bytes_async( Header::from_socket_connection_async(
1024, connection.clone(),
Priority::DEFAULT, Some(Priority::DEFAULT),
None::<&Cancellable>, None::<Cancellable>,
move |response| match response { move |result| match result
Ok(bytes) => { {
// Read header from response Ok(header) => {
match Header::from_response( // Route by status
&bytes match header.status() {
) { ClientStatus::Input | ClientStatus::SensitiveInput => {
Ok(header) => { // Format response
// Route by status let status = Status::Input;
match header.status() { let title = gformat!("Input expected");
ClientStatus::Input | ClientStatus::SensitiveInput => { let description = match header.meta() {
// Format response Some(meta) => match meta.to_gstring() {
let status = Status::Input; Ok(value) => value,
let title = gformat!("Input expected"); Err(_) => title.clone(),
let description = match header.meta() { },
Some(meta) => match meta.to_gstring() { None => title.clone(),
Ok(value) => value, };
Err(_) => title.clone(),
},
None => title.clone(),
};
// Make input form
match header.status() {
ClientStatus::SensitiveInput =>
input.set_new_sensitive(
action_page_open,
uri,
Some(&description),
Some(1024),
),
_ =>
input.set_new_response(
action_page_open,
uri,
Some(&description),
Some(1024),
),
}
// Update meta // Make input form
meta.borrow_mut().status = Some(status); match header.status() {
meta.borrow_mut().description = Some(description); ClientStatus::SensitiveInput =>
meta.borrow_mut().title = Some(title); input.set_new_sensitive(
action_page_open,
uri,
Some(&description),
Some(1024),
),
_ =>
input.set_new_response(
action_page_open,
uri,
Some(&description),
Some(1024),
),
}
// Update page // Update meta
action_update.activate(Some(&id)); meta.borrow_mut().status = Some(status);
}, meta.borrow_mut().description = Some(description);
ClientStatus::Success => { meta.borrow_mut().title = Some(title);
// Route by MIME
match header.mime() {
Some(ClientMime::TextGemini) => {
// Read entire input stream to buffer
Buffer::from_connection_async(
connection,
move |result|{
match result {
Ok(buffer) => {
// Update page meta
meta.borrow_mut().status = Some(Status::Success);
meta.borrow_mut().title = content.set_text_gemini(
&uri,
&match GString::from_utf8(buffer.to_utf8()) {
Ok(gemtext) => gemtext,
Err(_) => todo!()
}
);
// Add new history record
let request = uri.to_str();
match navigation.history_current() {
Some(current) => {
if current != request {
navigation.history_add(request);
}
}
None => navigation.history_add(request),
}
// Update window components // Update page
action_update.activate(Some(&id)); action_update.activate(Some(&id));
} },
Err((reason, message)) => { ClientStatus::Success => {
// Define common data // Route by MIME
let status = Status::Failure; match header.mime() {
let title = gformat!("Oops"); Some(ClientMime::TextGemini) => {
let description = match reason { // Read entire input stream to buffer
BufferError::InputStream => match message { Body::from_socket_connection_async(
Some(error) => gformat!("{error}"), connection,
None => gformat!("Undefined connection error") move |result|{
} , match result {
BufferError::Overflow => gformat!("Buffer overflow"),
};
// Update widget
content.set_status_failure(
title.as_str(),
description.as_str(),
);
// Update meta
meta.borrow_mut().status = Some(status);
meta.borrow_mut().title = Some(title);
meta.borrow_mut().description = Some(description);
// Update window
action_update.activate(Some(&id));
},
}
}
);
},
Some(
ClientMime::ImagePng | ClientMime::ImageGif |
ClientMime::ImageJpeg | ClientMime::ImageWebp
) => {
match Pixbuf::from_stream(
&connection.input_stream(),
None::<&Cancellable>,
) {
Ok(buffer) => { Ok(buffer) => {
// Update page meta // Update page meta
meta.borrow_mut().status = Some(Status::Success); meta.borrow_mut().status = Some(Status::Success);
meta.borrow_mut().title = Some(gformat!("Image")); meta.borrow_mut().title = content.set_text_gemini(
&uri,
// Update page content &match GString::from_utf8(buffer.to_utf8()) {
content.set_image(&buffer); Ok(gemtext) => gemtext,
Err(_) => todo!()
}
);
// Add history record // Add new history record
let request = uri.to_str(); let request = uri.to_str();
match navigation.history_current() { match navigation.history_current() {
Some(current) => { Some(current) => {
if current != request { if current != request {
@ -570,81 +503,141 @@ impl Page {
// Update window components // Update window components
action_update.activate(Some(&id)); action_update.activate(Some(&id));
} }
Err(reason) => { // Pixbuf::from_stream Err((reason, message)) => {
// Define common data // Define common data
let status = Status::Failure; let status = Status::Failure;
let title = gformat!("Oops"); let title = gformat!("Oops");
let description = gformat!("{}", reason.message()); let description = match reason {
BodyError::InputStream => match message {
Some(error) => gformat!("{error}"),
None => gformat!("Undefined connection error")
} ,
BodyError::Overflow => gformat!("Buffer overflow"),
BodyError::Buffer => todo!(),
BodyError::Decode => todo!(),
BodyError::Format => todo!(),
};
// Update widget // Update widget
content.set_status_failure(title.as_str(), description.as_str()); content.set_status_failure(
title.as_str(),
description.as_str(),
);
// Update meta // Update meta
meta.borrow_mut().status = Some(status); meta.borrow_mut().status = Some(status);
meta.borrow_mut().title = Some(title); meta.borrow_mut().title = Some(title);
meta.borrow_mut().description = Some(description); meta.borrow_mut().description = Some(description);
// Update window
action_update.activate(Some(&id));
},
}
}
);
},
Some(
ClientMime::ImagePng | ClientMime::ImageGif |
ClientMime::ImageJpeg | ClientMime::ImageWebp
) => {
match Pixbuf::from_stream(
&connection.input_stream(),
None::<&Cancellable>,
) {
Ok(buffer) => {
// Update page meta
meta.borrow_mut().status = Some(Status::Success);
meta.borrow_mut().title = Some(gformat!("Image"));
// Update page content
content.set_image(&buffer);
// Add history record
let request = uri.to_str();
match navigation.history_current() {
Some(current) => {
if current != request {
navigation.history_add(request);
}
} }
None => navigation.history_add(request),
} }
},
// @TODO stream extensions // Update window components
_ => { action_update.activate(Some(&id));
}
Err(reason) => { // Pixbuf::from_stream
// Define common data // Define common data
let status = Status::Failure; let status = Status::Failure;
let title = gformat!("Oops"); let title = gformat!("Oops");
let description = let description = gformat!("{}", reason.message());
gformat!("Content type not supported");
// Update widget // Update widget
content.set_status_failure( content.set_status_failure(title.as_str(), description.as_str());
title.as_str(),
description.as_str(),
);
// Update meta // Update meta
meta.borrow_mut().status = Some(status); meta.borrow_mut().status = Some(status);
meta.borrow_mut().title = Some(title); meta.borrow_mut().title = Some(title);
meta.borrow_mut().description = Some(description); meta.borrow_mut().description = Some(description);
}
// Update window
action_update.activate(Some(&id));
},
} }
}, },
ClientStatus::Redirect => { // @TODO stream extensions
_ => {
// Define common data
let status = Status::Failure;
let title = gformat!("Oops");
let description =
gformat!("Content type not supported");
// Update widget
content.set_status_failure(
title.as_str(),
description.as_str(),
);
// Update meta // Update meta
meta.borrow_mut().status = Some(Status::Redirect); meta.borrow_mut().status = Some(status);
meta.borrow_mut().title = Some(gformat!("Redirect")); meta.borrow_mut().title = Some(title);
meta.borrow_mut().description = Some(description);
// Build gemtext message for manual redirection @TODO use template?
match header.meta() {
Some(meta) => {
let _ = content.set_text_gemini(
&uri,
&match meta.to_gstring() {
Ok(url) => gformat!(
"# Redirect\n\nAuto-follow disabled, click on link below to continue\n\n=> {url}"
),
Err(_) => gformat!(
"# Redirect\n\nProvider request redirect but have not provided any target."
)
}
);
},
None => content.set_status_failure(
&"Oops",
&"Could not parse redirect meta"
),
}
// Update window
action_update.activate(Some(&id)); action_update.activate(Some(&id));
}, },
} }
}, },
Err(_) => todo!() // ResponseHeader::from_response ClientStatus::Redirect => {
// Update meta
meta.borrow_mut().status = Some(Status::Redirect);
meta.borrow_mut().title = Some(gformat!("Redirect"));
// Build gemtext message for manual redirection @TODO use template?
match header.meta() {
Some(meta) => {
let _ = content.set_text_gemini(
&uri,
&match meta.to_gstring() {
Ok(url) => gformat!(
"# Redirect\n\nAuto-follow disabled, click on link below to continue\n\n=> {url}"
),
Err(_) => gformat!(
"# Redirect\n\nProvider request redirect but have not provided any target."
)
}
);
},
None => content.set_status_failure(
&"Oops",
&"Could not parse redirect meta"
),
}
action_update.activate(Some(&id));
},
} }
} },
Err(_) => todo!(), // InputStream::read_bytes_async Err(_) => todo!() // ResponseHeader::from_response
}, }
); );
} }
Err(_) => todo!(), // OutputStream::write_bytes_async Err(_) => todo!(), // OutputStream::write_bytes_async

Loading…
Cancel
Save