Oauth: add dummy assistant w/ first page

This commit is contained in:
Félix Baylac-Jacqué 2022-07-06 16:44:47 +02:00
parent cb747e9253
commit 670a4b37e7
No known key found for this signature in database
GPG Key ID: EFD315F31848DBA4
9 changed files with 107 additions and 74 deletions

23
Cargo.lock generated
View File

@ -114,21 +114,10 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "error"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6e606f14042bb87cc02ef6a14db6c90ab92ed6f62d87e69377bc759fd7987cc"
dependencies = [
"traitobject",
"typeable",
]
[[package]]
name = "federatz"
version = "0.1.0"
dependencies = [
"error",
"gtk4",
"serde",
"serde_json",
@ -928,18 +917,6 @@ dependencies = [
"serde",
]
[[package]]
name = "traitobject"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
[[package]]
name = "typeable"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"
[[package]]
name = "ucd-trie"
version = "0.1.3"

View File

@ -3,10 +3,7 @@ name = "federatz"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
error = "*"
gtk4 = "*"
serde = {version = "*", features = ["derive"]}
serde_json = "*"

View File

@ -2,62 +2,24 @@ use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Box, Orientation};
use std::cell::{RefCell};
use std::cell::RefCell;
use std::rc::Rc;
mod oauth;
mod ui;
use ui::widgets::oauth::create_oauth_assistant;
fn main() {
let app = Application::builder()
.application_id("fr.alternativebit.federatz")
.build();
app.connect_activate(|app| {
// We create the main window.
let window = ApplicationWindow::builder()
.application(app)
.default_width(320)
.default_height(200)
.title("Hello, World!")
.build();
let vert_box = Box::new(Orientation::Vertical, 10);
let oauth_registered_app = Rc::new(RefCell::new(None));
let cloned_oauth_registered_app = oauth_registered_app.clone();
let button_reg = Button::with_label("Register App");
button_reg.connect_clicked(move |_| {
let ap_reg = oauth::register_app("social.alternativebit.fr", "federatz-test");
match ap_reg {
Ok(ap_reg) => {
{
oauth_registered_app.replace(Some(ap_reg.clone()));
println!("{:?}", ap_reg.clone());
}
},
Err(_) => ()
}
});
let button_auth = Button::with_label("Authorize App");
button_auth.connect_clicked(move |_| {
let orab = cloned_oauth_registered_app.borrow().clone();
match orab {
Some(orab) => {
let url = oauth::gen_authorize_url(
"social.alternativebit.fr",
&orab);
println!("{}", url);
},
None => ()
}
});
vert_box.append(&button_reg);
vert_box.append(&button_auth);
window.set_child(Some(&vert_box));
// Show the window.
window.show();
});
app.connect_activate(build_ui);
app.run();
}
fn build_ui(app: &Application) {
let oauth_assistant = create_oauth_assistant(app);
oauth_assistant.present();
}

1
src/ui/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod widgets;

1
src/ui/widgets/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod oauth;

View File

@ -0,0 +1,15 @@
use gtk4 as gtk;
use gtk::glib;
mod page1;
pub fn create_oauth_assistant(app: &gtk::Application) -> gtk::Assistant {
let assistant = gtk::Assistant::builder()
.application(app)
.title("Authenticate on your home instance")
.build();
let page1 = page1::OauthAssistantPage1::new();
assistant.append_page(&page1);
assistant.set_page_type(&page1, gtk::AssistantPageType::Intro);
assistant
}

View File

@ -0,0 +1,41 @@
use gtk4 as gtk;
use gtk::subclass::prelude::*;
use gtk::subclass::window::WindowImpl;
use gtk::prelude::*;
use gtk::{glib, CompositeTemplate, Label, Box, Entry};
use glib::subclass::InitializingObject;
#[derive(Debug, Default, CompositeTemplate)]
#[template(file = "page1.ui")]
pub struct OauthAssistantPage1 {
#[template_child]
pub instance_uri_text_entry: TemplateChild<Entry>,
#[template_child]
pub instance_uri_label: TemplateChild<Label>,
}
#[glib::object_subclass]
impl ObjectSubclass for OauthAssistantPage1 {
const NAME: &'static str = "oauthAssistantPage1";
type Type = super::OauthAssistantPage1;
type ParentType = Box;
fn class_init(c: &mut Self::Class) {
c.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for OauthAssistantPage1 {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
}
}
impl WindowImpl for OauthAssistantPage1 {}
impl WidgetImpl for OauthAssistantPage1 {}
impl BoxImpl for OauthAssistantPage1 {}
impl ApplicationWindowImpl for OauthAssistantPage1 {}

View File

@ -0,0 +1,18 @@
use gtk4 as gtk;
use glib::Object;
use gtk::{gio, glib, Application};
mod imp;
glib::wrapper! {
pub struct OauthAssistantPage1(ObjectSubclass<imp::OauthAssistantPage1>)
@extends gtk::Widget,
@implements gtk::Accessible, gtk::ConstraintTarget,
gtk::Buildable, gtk::Orientable;
}
impl OauthAssistantPage1 {
pub fn new() -> Self {
Object::new(&[]).expect("Failed to create oauth assistant page 1")
}
}

View File

@ -0,0 +1,21 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.10.2 -->
<interface>
<!-- interface-name page1.ui -->
<requires lib="gtk" version="4.6"/>
<template class="oauthAssistantPage1" parent="GtkBox">
<property name="margin-end">4</property>
<property name="margin-start">3</property>
<property name="spacing">1</property>
<child>
<object class="GtkLabel" id="instance_uri_label">
<property name="label">Instance URI:</property>
</object>
</child>
<child>
<object class="GtkEntry" id="instance_uri_text_entry">
<property name="placeholder-text">mastodon.social</property>
</object>
</child>
</template>
</interface>