From 05c6dbcf03cdc0a8a87457ff9bd4d72c2dc34a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Moreno?= Date: Sat, 23 Dec 2017 11:30:08 +0100 Subject: [PATCH] Server url stored in the AppOp --- fractal-gtk/src/app.rs | 25 +++++++++++++------------ fractal-gtk/src/widgets/roomlist.rs | 23 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/fractal-gtk/src/app.rs b/fractal-gtk/src/app.rs index fe111221..75d5778a 100644 --- a/fractal-gtk/src/app.rs +++ b/fractal-gtk/src/app.rs @@ -76,6 +76,7 @@ pub struct AppOp { pub username: Option, pub uid: Option, + pub server_url: String, pub autoscroll: bool, pub active_room: Option, @@ -121,6 +122,7 @@ impl AppOp { rooms: HashMap::new(), username: None, uid: None, + server_url: String::from("https://matrix.org"), syncing: false, tmp_msgs: vec![], state: AppState::Login, @@ -188,7 +190,7 @@ impl AppOp { } #[allow(dead_code)] - pub fn register(&self) { + pub fn register(&mut self) { let user_entry: gtk::Entry = self.gtk_builder .get_object("register_username") .expect("Can't find register_username in ui file."); @@ -220,7 +222,7 @@ impl AppOp { return; } - let server_url = match server_entry.get_text() { + self.server_url = match server_entry.get_text() { Some(s) => s, None => String::from("https://matrix.org"), }; @@ -233,17 +235,17 @@ impl AppOp { let uname = username.clone(); let pass = password.clone(); - let ser = server_url.clone(); + let ser = self.server_url.clone(); self.backend.send(BKCommand::Register(uname, pass, ser)).unwrap(); } - pub fn connect(&self, username: Option, password: Option, server: Option) -> Option<()> { - let server_url = match server { + pub fn connect(&mut self, username: Option, password: Option, server: Option) -> Option<()> { + self.server_url = match server { Some(s) => s, None => String::from("https://matrix.org"), }; - self.store_pass(username.clone()?, password.clone()?, server_url.clone()) + self.store_pass(username.clone()?, password.clone()?, self.server_url.clone()) .unwrap_or_else(|_| { // TODO: show an error println!("Error: Can't store the password using libsecret"); @@ -251,19 +253,19 @@ impl AppOp { let uname = username?; let pass = password?; - let ser = server_url; + let ser = self.server_url.clone(); self.backend.send(BKCommand::Login(uname, pass, ser)).unwrap(); Some(()) } #[allow(dead_code)] - pub fn connect_guest(&self, server: Option) { - let server_url = match server { + pub fn connect_guest(&mut self, server: Option) { + self.server_url = match server { Some(s) => s, None => String::from("https://matrix.org"), }; - self.backend.send(BKCommand::Guest(server_url)).unwrap(); + self.backend.send(BKCommand::Guest(self.server_url.clone())).unwrap(); } pub fn get_username(&self) { @@ -484,8 +486,7 @@ impl AppOp { // TODO: sort by last message array.sort_by(|x, y| x.name.clone().unwrap_or_default().to_lowercase().cmp(&y.name.clone().unwrap_or_default().to_lowercase())); - // TODO: Get the server url... Store it at login - self.roomlist = widgets::RoomList::new(None); + self.roomlist = widgets::RoomList::new(Some(self.server_url.clone())); container.add(&self.roomlist.widget()); for v in array { diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs index 36f3b75d..62a03dd9 100644 --- a/fractal-gtk/src/widgets/roomlist.rs +++ b/fractal-gtk/src/widgets/roomlist.rs @@ -8,6 +8,22 @@ use self::gtk::prelude::*; use widgets::roomrow::RoomRow; use types::Room; + +fn get_url(url: Option) -> Url { + let defurl = Url::parse("https://matrix.org").unwrap(); + + match url { + Some(u) => { + match Url::parse(&u) { + Ok(url) => url, + Err(_) => defurl, + } + } + None => defurl, + } +} + + pub struct RoomList { pub rooms: HashMap, pub baseu: Url, @@ -21,12 +37,9 @@ pub struct RoomList { } impl RoomList { - pub fn new(url: Option) -> RoomList { + pub fn new(url: Option) -> RoomList { let list = gtk::ListBox::new(); - let baseu = match url { - Some(u) => u.clone(), - None => Url::parse("https://matrix.org").unwrap() - }; + let baseu = get_url(url); let rooms = HashMap::new(); let roomvec = vec![];