From d7e8e793606a8ee0c784155681f510913f0b43c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Moreno?= Date: Thu, 31 Aug 2017 10:25:44 +0200 Subject: [PATCH] Resolving always the sender display name for messages --- src/backend.rs | 23 ----------------------- src/widgets.rs | 26 ++++++++++++++------------ 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index d695e7ed..852ae512 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -53,7 +53,6 @@ pub enum BKCommand { SyncForced, GetRoomMessages(String), GetRoomMessagesTo(String), - GetAvatarAsync(String, Sender), GetThumbAsync(String, Sender), GetUserInfoAsync(String, Sender<(String, String)>), SendMsg(String, String), @@ -163,10 +162,6 @@ impl Backend { let r = self.get_room_messages(room, true); bkerror!(r, tx, BKResponse::RoomMessagesError); } - Ok(BKCommand::GetAvatarAsync(sender, ctx)) => { - let r = self.get_avatar_async(&sender, ctx); - bkerror!(r, tx, BKResponse::CommandError); - } Ok(BKCommand::GetUserInfoAsync(sender, ctx)) => { let r = self.get_user_info_async(&sender, ctx); bkerror!(r, tx, BKResponse::CommandError); @@ -552,24 +547,6 @@ impl Backend { Ok(url) } - pub fn get_avatar_async(&self, uid: &str, tx: Sender) -> Result<(), Error> { - let baseu = self.get_base_url()?; - - let u = String::from(uid); - thread::spawn(move || { - match get_user_avatar(&baseu, &u) { - Ok((_, fname)) => { - tx.send(fname).unwrap(); - } - Err(_) => { - tx.send(String::from("")).unwrap(); - } - }; - }); - - Ok(()) - } - pub fn get_user_info_async(&self, uid: &str, tx: Sender<(String, String)>) diff --git a/src/widgets.rs b/src/widgets.rs index e3ea0f84..213f7f71 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -25,6 +25,7 @@ use app::AppOp; pub struct MessageBox<'a> { msg: &'a Message, op: &'a AppOp, + username: gtk::Label, } // Room Search item @@ -35,12 +36,11 @@ pub struct RoomBox<'a> { impl<'a> MessageBox<'a> { pub fn new(msg: &'a Message, op: &'a AppOp) -> MessageBox<'a> { - MessageBox { msg: msg, op: op } + let username = gtk::Label::new(""); + MessageBox { msg: msg, op: op, username } } pub fn widget(&self) -> gtk::Box { - let avatar = self.build_room_msg_avatar(); - // msg // +--------+---------+ // | avatar | content | @@ -48,6 +48,7 @@ impl<'a> MessageBox<'a> { let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 5); let content = self.build_room_msg_content(); + let avatar = self.build_room_msg_avatar(); msg_widget.pack_start(&avatar, false, false, 5); msg_widget.pack_start(&content, true, true, 0); @@ -89,15 +90,17 @@ impl<'a> MessageBox<'a> { let backend = self.op.backend.clone(); let avatar = gtk::Image::new_from_icon_name("image-missing", 5); let a = avatar.clone(); + let u = self.username.clone(); - let (tx, rx): (Sender, Receiver) = channel(); - backend.send(BKCommand::GetAvatarAsync(sender, tx)).unwrap(); + let (tx, rx): (Sender<(String, String)>, Receiver<(String, String)>) = channel(); + backend.send(BKCommand::GetUserInfoAsync(sender, tx)).unwrap(); gtk::timeout_add(50, move || match rx.try_recv() { Err(_) => gtk::Continue(true), - Ok(fname) => { - if let Ok(pixbuf) = Pixbuf::new_from_file_at_scale(&fname, 32, 32, false) { + Ok((name, avatar)) => { + if let Ok(pixbuf) = Pixbuf::new_from_file_at_scale(&avatar, 32, 32, false) { a.set_from_pixbuf(&pixbuf); } + u.set_markup(&format!("{}", name)); gtk::Continue(false) } }); @@ -112,12 +115,11 @@ impl<'a> MessageBox<'a> { None => String::from(sender), }; - let username = gtk::Label::new(""); - username.set_markup(&format!("{}", uname)); - username.set_justify(gtk::Justification::Left); - username.set_halign(gtk::Align::Start); + self.username.set_markup(&format!("{}", uname)); + self.username.set_justify(gtk::Justification::Left); + self.username.set_halign(gtk::Align::Start); - username + self.username.clone() } fn build_room_msg_body(&self, body: &str) -> gtk::Box {