Commit de83098e authored by Julian Sparber's avatar Julian Sparber

accountsettings: add api for changing account name

* add http call for changing account name
* update cached account name

https://gitlab.gnome.org/World/fractal/issues/21
parent 96bcb94b
......@@ -151,6 +151,10 @@ impl Backend {
let r = user::get_username(self);
bkerror!(r, tx, BKResponse::UserNameError);
}
Ok(BKCommand::SetUserName(name)) => {
let r = user::set_username(self, name);
bkerror!(r, tx, BKResponse::SetUserNameError);
}
Ok(BKCommand::GetAvatar) => {
let r = user::get_avatar(self);
bkerror!(r, tx, BKResponse::AvatarError);
......
......@@ -24,6 +24,7 @@ pub enum BKCommand {
#[allow(dead_code)]
Guest(String),
GetUsername,
SetUserName(String),
GetAvatar,
Sync,
SyncForced,
......@@ -69,6 +70,7 @@ pub enum BKResponse {
Token(String, String),
Logout,
Name(String),
SetUserName(String),
Avatar(String),
Sync(String),
Rooms(Vec<Room>, Option<Room>),
......@@ -103,6 +105,7 @@ pub enum BKResponse {
//errors
UserNameError(Error),
SetUserNameError(Error),
AvatarError(Error),
LoginError(Error),
LogoutError(Error),
......
......@@ -31,6 +31,23 @@ pub fn get_username(bk: &Backend) -> Result<(), Error> {
Ok(())
}
pub fn set_username(bk: &Backend, name: String) -> Result<(), Error> {
let id = bk.data.lock().unwrap().user_id.clone();
let url = bk.url(&format!("profile/{}/displayname", id.clone()), vec![])?;
let attrs = json!({
"displayname": name,
});
let tx = bk.tx.clone();
query!("put", &url, &attrs,
|_| { tx.send(BKResponse::SetUserName(name)).unwrap(); },
|err| { tx.send(BKResponse::SetUserNameError(err)).unwrap(); }
);
Ok(())
}
pub fn get_avatar(bk: &Backend) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
let userid = bk.data.lock().unwrap().user_id.clone();
......
......@@ -353,6 +353,7 @@
<object class="GtkLabel" id="account_settings_homeserver">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="halign">start</property>
<property name="label" translatable="yes">None</property>
</object>
<packing>
......@@ -377,9 +378,10 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="account_settings_matrix_id">
<object class="GtkLabel" id="account_settings_uid">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="halign">start</property>
<property name="label" translatable="yes">None</property>
</object>
<packing>
......@@ -407,6 +409,7 @@
<object class="GtkLabel" id="account_settings_device_id">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="halign">start</property>
<property name="label" translatable="yes">None</property>
</object>
<packing>
......
......@@ -49,6 +49,10 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
let u = Some(username);
APPOP!(set_username, (u));
}
Ok(BKResponse::SetUserName(username)) => {
let u = Some(username);
APPOP!(set_username, (u));
}
Ok(BKResponse::Avatar(path)) => {
let av = Some(path);
APPOP!(set_avatar, (av));
......
......@@ -13,6 +13,9 @@ impl App {
let cancel = self.ui.builder
.get_object::<gtk::Button>("cancel_account_settings")
.expect("Can't find cancel_account_settings in ui file.");
let confirm = self.ui.builder
.get_object::<gtk::Button>("apply_account_settings")
.expect("Can't find join_room_button in ui file.");
let dialog = self.ui.builder
.get_object::<gtk::Dialog>("account_settings_dialog")
.expect("Can't find account_settings_dialog in ui file.");
......@@ -32,6 +35,11 @@ impl App {
op.lock().unwrap().close_account_settings_dialog();
}));
confirm.connect_clicked(clone!(op => move |_| {
op.lock().unwrap().apply_account_settings();
op.lock().unwrap().close_account_settings_dialog();
}));
advanced_toggle.connect_button_press_event(clone!(builder => move |this, _| {
let widget = builder
.get_object::<gtk::Revealer>("account_settings_advanced")
......@@ -51,7 +59,7 @@ impl App {
let widget = builder
.get_object::<gtk::Revealer>("account_settings_delete")
.expect("Can't find account_settings_delete in ui file.");
if widget.get_reveal_child() {
if widget.get_reveal_child() {
this.get_style_context().unwrap().remove_class("advanced_revealer_divider");
widget.set_reveal_child(false);
}
......
......@@ -4,7 +4,7 @@ use self::gtk::prelude::*;
use appop::AppOp;
//use backend::BKCommand;
use backend::BKCommand;
use widgets;
use widgets::AvatarExt;
......@@ -19,12 +19,20 @@ impl AppOp {
let name = self.ui.builder
.get_object::<gtk::Entry>("account_settings_name")
.expect("Can't find account_settings_name in ui file.");
let uid = self.ui.builder
.get_object::<gtk::Label>("account_settings_uid")
.expect("Can't find account_settings_uid in ui file.");
let homeserver = self.ui.builder
.get_object::<gtk::Label>("account_settings_homeserver")
.expect("Can't find account_settings_homeserver in ui file.");
/* remove all old avatar from the popover */
for w in avatar.get_children().iter() {
avatar.remove(w);
}
uid.set_text(&self.uid.clone().unwrap_or_default());
homeserver.set_text(&self.server_url);
name.set_text(&self.username.clone().unwrap_or_default());
name.grab_focus_without_selecting();
name.set_position(-1);
......@@ -36,6 +44,19 @@ impl AppOp {
dialog.present();
}
pub fn apply_account_settings(&self) {
let name = self.ui.builder
.get_object::<gtk::Entry>("account_settings_name")
.expect("Can't find account_settings_name in ui file.");
let old_username = self.username.clone().unwrap_or_default();
let username = name.get_text().unwrap_or_default();
if old_username != username {
self.backend.send(BKCommand::SetUserName(username)).unwrap();
}
}
pub fn close_account_settings_dialog(&mut self) {
let dialog = self.ui.builder
.get_object::<gtk::Dialog>("account_settings_dialog")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment