mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-03-10 04:31:14 +00:00
begin ggemini lib integration
This commit is contained in:
parent
3578928969
commit
ef64e7e032
@ -11,7 +11,7 @@ use input::Input;
|
|||||||
use navigation::Navigation;
|
use navigation::Navigation;
|
||||||
use widget::Widget;
|
use widget::Widget;
|
||||||
|
|
||||||
use meta::{Meta, Mime, Status};
|
use meta::{Meta, Status};
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags},
|
gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags},
|
||||||
@ -174,7 +174,6 @@ impl Page {
|
|||||||
let action_update = self.action_update.clone();
|
let action_update = self.action_update.clone();
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
meta.borrow_mut().mime = None;
|
|
||||||
meta.borrow_mut().status = Some(Status::Reload);
|
meta.borrow_mut().status = Some(Status::Reload);
|
||||||
meta.borrow_mut().title = Some(gformat!("Loading.."));
|
meta.borrow_mut().title = Some(gformat!("Loading.."));
|
||||||
meta.borrow_mut().description = None;
|
meta.borrow_mut().description = None;
|
||||||
@ -228,7 +227,7 @@ impl Page {
|
|||||||
gformat!("{}\r\n", &uri.to_str()),
|
gformat!("{}\r\n", &uri.to_str()),
|
||||||
Priority::DEFAULT,
|
Priority::DEFAULT,
|
||||||
Some(&cancellable.clone()),
|
Some(&cancellable.clone()),
|
||||||
move |result| match result {
|
move |o| match o {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// Update
|
// Update
|
||||||
meta.borrow_mut().status = Some(Status::Request);
|
meta.borrow_mut().status = Some(Status::Request);
|
||||||
@ -241,12 +240,21 @@ impl Page {
|
|||||||
vec![0; 0xfffff], // 1Mb @TODO
|
vec![0; 0xfffff], // 1Mb @TODO
|
||||||
Priority::DEFAULT,
|
Priority::DEFAULT,
|
||||||
Some(&cancellable.clone()),
|
Some(&cancellable.clone()),
|
||||||
move |result| match result {
|
move |i| match i {
|
||||||
Ok(response) => {
|
Ok(i) => {
|
||||||
match GString::from_utf8_until_nul(
|
// Define local NS
|
||||||
response.0,
|
use gemini::client::response::{
|
||||||
|
header::{
|
||||||
|
Mime as ResponseMime,
|
||||||
|
Status as ResponseStatus
|
||||||
|
},
|
||||||
|
Response,
|
||||||
|
};
|
||||||
|
|
||||||
|
match Response::from_utf8(
|
||||||
|
&i.0.to_vec(),
|
||||||
) {
|
) {
|
||||||
Ok(data) => {
|
Ok(response) => {
|
||||||
// Format response
|
// Format response
|
||||||
meta.borrow_mut().status = Some(Status::Response);
|
meta.borrow_mut().status = Some(Status::Response);
|
||||||
meta.borrow_mut().description = Some(host);
|
meta.borrow_mut().description = Some(host);
|
||||||
@ -254,107 +262,55 @@ impl Page {
|
|||||||
|
|
||||||
action_update.activate(Some(&id));
|
action_update.activate(Some(&id));
|
||||||
|
|
||||||
// Parse response @TODO read bytes
|
match response.header().status() {
|
||||||
let parts = Regex::split_simple(
|
// 10 | 11
|
||||||
r"^(\d+)?\s([\w]+\/[\w]+)?(.*)?",
|
ResponseStatus::Input | ResponseStatus::SensitiveInput => {
|
||||||
&data,
|
// Format response
|
||||||
RegexCompileFlags::DEFAULT,
|
let status = Status::Input;
|
||||||
RegexMatchFlags::DEFAULT,
|
let title = gformat!("Input expected");
|
||||||
);
|
let description = match response.header().meta() {
|
||||||
|
Some(meta) => match meta.to_gstring() {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_) => title.clone()
|
||||||
|
},
|
||||||
|
None => title.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes
|
// Make input form
|
||||||
match parts.get(1) {
|
match response.header().status() {
|
||||||
Some(code) => match code.as_str() {
|
ResponseStatus::SensitiveInput =>
|
||||||
// Input expected
|
input.set_new_sensitive(
|
||||||
"10" | "11" => {
|
action_page_open, uri,
|
||||||
match parts.get(3) {
|
Some(&description),
|
||||||
Some(placeholder) => {
|
Some(1024),
|
||||||
// Format response
|
),
|
||||||
let status = Status::Input;
|
_ =>
|
||||||
let title = gformat!("Input expected");
|
input.set_new_response(
|
||||||
let description = gformat!("{placeholder}");
|
action_page_open, uri,
|
||||||
|
Some(&description),
|
||||||
|
Some(1024),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Make input form
|
// Update meta
|
||||||
if "11" == code.as_str() { // sensitive input
|
meta.borrow_mut().status = Some(status);
|
||||||
input.set_new_sensitive(
|
meta.borrow_mut().description = Some(description);
|
||||||
action_page_open, uri,
|
meta.borrow_mut().title = Some(title);
|
||||||
Some(&description),
|
|
||||||
Some(1024),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
input.set_new_response(
|
|
||||||
action_page_open, uri,
|
|
||||||
Some(&description),
|
|
||||||
Some(1024),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update meta
|
// Update page
|
||||||
meta.borrow_mut().status = Some(status);
|
action_update.activate(Some(&id));
|
||||||
meta.borrow_mut().description = Some(description);
|
},
|
||||||
meta.borrow_mut().title = Some(title);
|
// 20
|
||||||
|
ResponseStatus::Success =>
|
||||||
// Update page
|
match response.header().mime() {
|
||||||
action_update.activate(Some(&id));
|
Some(ResponseMime::TextGemini) => {
|
||||||
},
|
// Update data
|
||||||
None => todo!(),
|
match response.body().to_gstring() {
|
||||||
}
|
Ok(source) => {
|
||||||
},
|
|
||||||
// Success
|
|
||||||
"20" => {
|
|
||||||
match parts.get(2) {
|
|
||||||
Some(mime) => match mime.as_str() {
|
|
||||||
"text/gemini" => {
|
|
||||||
// Update meta
|
|
||||||
meta.borrow_mut().mime = Some(Mime::TextGemini);
|
|
||||||
|
|
||||||
// Update data
|
|
||||||
match parts.get(4) {
|
|
||||||
Some(source) => {
|
|
||||||
meta.borrow_mut().status = Some(Status::Success);
|
|
||||||
|
|
||||||
// This content type may return parsed title
|
|
||||||
meta.borrow_mut().title = content.set_text_gemini(&uri, &source);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
action_update.activate(Some(&id));
|
|
||||||
},
|
|
||||||
None => todo!(),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"text/plain" => {
|
|
||||||
meta.borrow_mut().status = Some(Status::Success);
|
meta.borrow_mut().status = Some(Status::Success);
|
||||||
meta.borrow_mut().mime = Some(Mime::TextPlain);
|
|
||||||
|
|
||||||
action_update.activate(Some(&id));
|
// This content type may return parsed title
|
||||||
todo!()
|
meta.borrow_mut().title = content.set_text_gemini(&uri, &source);
|
||||||
},
|
|
||||||
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
|
|
||||||
// Update meta
|
|
||||||
meta.borrow_mut().status = Some(Status::Success);
|
|
||||||
meta.borrow_mut().title = Some(gformat!("Picture")); // @TODO
|
|
||||||
meta.borrow_mut().mime = match mime.as_str() {
|
|
||||||
"image/png" => Some(Mime::ImagePng),
|
|
||||||
"image/gif" => Some(Mime::ImageGif),
|
|
||||||
"image/jpeg" => Some(Mime::ImageJpeg),
|
|
||||||
"image/webp" => Some(Mime::ImageWebp),
|
|
||||||
_ => panic!()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Update content
|
|
||||||
content.set_image(); // @TODO
|
|
||||||
|
|
||||||
// Add new history record
|
// Add new history record
|
||||||
let request = uri.to_str();
|
let request = uri.to_str();
|
||||||
@ -371,75 +327,107 @@ impl Page {
|
|||||||
// Update window components
|
// Update window components
|
||||||
action_update.activate(Some(&id));
|
action_update.activate(Some(&id));
|
||||||
},
|
},
|
||||||
_ => {
|
Err(_) => todo!(),
|
||||||
// Define common data
|
|
||||||
let status = Status::Failure;
|
|
||||||
let title = gformat!("Oops");
|
|
||||||
let description = gformat!("Content {mime} not supported");
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
None => todo!(),
|
},
|
||||||
};
|
Some(ResponseMime::TextPlain) => {
|
||||||
},
|
meta.borrow_mut().status = Some(Status::Success);
|
||||||
// Redirect (@TODO implement limits to auto-redirect)
|
|
||||||
"31" => {
|
|
||||||
// Update meta
|
|
||||||
meta.borrow_mut().status = Some(Status::Redirect);
|
|
||||||
meta.borrow_mut().mime = Some(Mime::TextGemini);
|
|
||||||
meta.borrow_mut().title = Some(gformat!("Redirect"));
|
|
||||||
|
|
||||||
action_update.activate(Some(&id));
|
action_update.activate(Some(&id));
|
||||||
|
todo!()
|
||||||
|
},
|
||||||
|
Some(ResponseMime::ImagePng) | Some(ResponseMime::ImageGif) | Some(ResponseMime::ImageJpeg) | Some(ResponseMime::ImageWebp) => {
|
||||||
|
// Update meta
|
||||||
|
meta.borrow_mut().status = Some(Status::Success);
|
||||||
|
meta.borrow_mut().title = Some(gformat!("Picture")); // @TODO
|
||||||
|
|
||||||
// Select widget
|
// Update content
|
||||||
match parts.get(3) {
|
content.set_image(); // @TODO
|
||||||
Some(source) => {
|
|
||||||
let _ = content.set_text_gemini(
|
|
||||||
&uri,
|
|
||||||
// @TODO use template file
|
|
||||||
&gformat!("# Redirect\n\nAuto-follow disabled, click on link below to continue\n\n=> {source}")
|
|
||||||
);
|
|
||||||
},
|
|
||||||
None => todo!(),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// @TODO
|
|
||||||
_ => {
|
|
||||||
// Define common data
|
|
||||||
let status = Status::Failure;
|
|
||||||
let title = gformat!("Oops");
|
|
||||||
let description = gformat!("Status {code} not supported");
|
|
||||||
|
|
||||||
// Update widget
|
// Add new history record
|
||||||
content.set_status_failure(title.as_str(), description.as_str());
|
let request = uri.to_str();
|
||||||
|
|
||||||
// Update meta
|
match navigation.history_current() {
|
||||||
meta.borrow_mut().status = Some(status);
|
Some(current) => {
|
||||||
meta.borrow_mut().title = Some(title);
|
if current != request {
|
||||||
meta.borrow_mut().description = Some(description);
|
navigation.history_add(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => navigation.history_add(request),
|
||||||
|
}
|
||||||
|
|
||||||
// Update window
|
// Update window components
|
||||||
action_update.activate(Some(&id));
|
action_update.activate(Some(&id));
|
||||||
},
|
},
|
||||||
}
|
_ => {
|
||||||
None => todo!(),
|
// 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
|
||||||
|
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));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// 32
|
||||||
|
ResponseStatus::Redirect => {
|
||||||
|
// Update meta
|
||||||
|
meta.borrow_mut().status = Some(Status::Redirect);
|
||||||
|
meta.borrow_mut().title = Some(gformat!("Redirect"));
|
||||||
|
|
||||||
|
action_update.activate(Some(&id));
|
||||||
|
|
||||||
|
// Select widget
|
||||||
|
match response.header().meta() {
|
||||||
|
Some(meta) => {
|
||||||
|
let _ = content.set_text_gemini(
|
||||||
|
&uri,
|
||||||
|
// @TODO use template file
|
||||||
|
&gformat!(
|
||||||
|
"# Redirect\n\nAuto-follow disabled, click on link below to continue\n\n=> {}",
|
||||||
|
match meta.to_gstring() {
|
||||||
|
Ok(url) => url,
|
||||||
|
Err(_) => todo!()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
None => todo!(),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// @TODO
|
||||||
|
_ => {
|
||||||
|
// Define common data
|
||||||
|
let status = Status::Failure;
|
||||||
|
let title = gformat!("Oops");
|
||||||
|
let description = gformat!("Status code not supported");
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(_) => {
|
||||||
// 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!("Failed to read buffer data: {:?}", error);
|
let description = gformat!("Failed to read buffer data");
|
||||||
|
|
||||||
// Update widget
|
// Update widget
|
||||||
content.set_status_failure(title.as_str(), description.as_str());
|
content.set_status_failure(title.as_str(), description.as_str());
|
||||||
|
@ -1,38 +1,30 @@
|
|||||||
use gtk::glib::GString;
|
use gtk::glib::GString;
|
||||||
|
|
||||||
// Page MIME type (not related with gemini status code)
|
|
||||||
// Useful for content renderer detection, etc
|
|
||||||
pub enum Mime {
|
|
||||||
TextGemini,
|
|
||||||
TextPlain,
|
|
||||||
ImagePng,
|
|
||||||
ImageGif,
|
|
||||||
ImageJpeg,
|
|
||||||
ImageWebp,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Internal page status (not related with gemini status code)
|
|
||||||
// Useful for widgets composition
|
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
|
// SensitiveInput,
|
||||||
|
// Complete,
|
||||||
Connect,
|
Connect,
|
||||||
|
// Connected,
|
||||||
|
// Connecting,
|
||||||
Failure,
|
Failure,
|
||||||
Input,
|
Input,
|
||||||
Prepare,
|
Prepare,
|
||||||
|
// ProxyNegotiated,
|
||||||
|
// ProxyNegotiating,
|
||||||
Redirect,
|
Redirect,
|
||||||
Reload,
|
Reload,
|
||||||
Request,
|
Request,
|
||||||
|
// Resolved,
|
||||||
|
// Resolving,
|
||||||
Response,
|
Response,
|
||||||
// @TODO SensitiveInput,
|
|
||||||
Success,
|
Success,
|
||||||
}
|
// TlsHandshaked,
|
||||||
|
// TlsHandshaking,
|
||||||
|
} // @TODO
|
||||||
|
|
||||||
pub struct Meta {
|
pub struct Meta {
|
||||||
// Text meta data for page
|
|
||||||
// Useful to update window title, label text, etc
|
|
||||||
pub title: Option<GString>,
|
pub title: Option<GString>,
|
||||||
pub description: Option<GString>,
|
pub description: Option<GString>,
|
||||||
// Enums
|
|
||||||
pub mime: Option<Mime>,
|
|
||||||
pub status: Option<Status>,
|
pub status: Option<Status>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +33,6 @@ impl Meta {
|
|||||||
Self {
|
Self {
|
||||||
title: None,
|
title: None,
|
||||||
description: None,
|
description: None,
|
||||||
mime: None,
|
|
||||||
status: None,
|
status: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user