[fractal/get-widgets-from-ui-struct: 4/6] Move ui::connect::account to ui::AccountSettings




commit 90374b46fdac391ccc798453272c2819943038c8
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Dec 7 20:45:23 2020 +0100

    Move ui::connect::account to ui::AccountSettings

 fractal-gtk/src/meson.build           |   1 -
 fractal-gtk/src/ui/account.rs         | 169 ++++++++++++++++++++++++++++++
 fractal-gtk/src/ui/connect/account.rs | 192 ----------------------------------
 fractal-gtk/src/ui/connect/mod.rs     |   4 +-
 4 files changed, 171 insertions(+), 195 deletions(-)
---
diff --git a/fractal-gtk/src/meson.build b/fractal-gtk/src/meson.build
index efe3bcb9..2ec9d8b3 100644
--- a/fractal-gtk/src/meson.build
+++ b/fractal-gtk/src/meson.build
@@ -84,7 +84,6 @@ app_sources = files(
   'model/message.rs',
   'model/mod.rs',
   'model/room.rs',
-  'ui/connect/account.rs',
   'ui/connect/autocomplete.rs',
   'ui/connect/directory.rs',
   'ui/connect/direct.rs',
diff --git a/fractal-gtk/src/ui/account.rs b/fractal-gtk/src/ui/account.rs
index 0153d406..9c5fa6dd 100644
--- a/fractal-gtk/src/ui/account.rs
+++ b/fractal-gtk/src/ui/account.rs
@@ -1,9 +1,13 @@
 use super::UI;
+use crate::actions::{AccountSettings as AccountSettingsActions, StateExt};
+use crate::app::AppRuntime;
 use crate::appop::AppOp;
 use crate::appop::UserInfoCache;
 use crate::cache::{download_to_cache, remove_from_cache};
 use crate::widgets;
 use crate::widgets::AvatarExt;
+use gio::ActionMapExt;
+use glib::clone;
 use gtk::prelude::*;
 use matrix_sdk::api::r0::contact::get_contacts::ThirdPartyIdentifier;
 use matrix_sdk::identifiers::{DeviceId, UserId};
@@ -99,6 +103,171 @@ impl AccountSettings {
         }
     }
 
+    pub fn connect(
+        &self,
+        builder: &gtk::Builder,
+        main_window: &libhandy::ApplicationWindow,
+        app_runtime: AppRuntime,
+    ) {
+        let cancel_password = builder
+            .get_object::<gtk::Button>("password-dialog-cancel")
+            .expect("Can't find password-dialog-cancel in ui file.");
+        let confirm_password = builder
+            .get_object::<gtk::Button>("password-dialog-apply")
+            .expect("Can't find password-dialog-apply in ui file.");
+        let password_dialog = builder
+            .get_object::<gtk::Dialog>("password_dialog")
+            .expect("Can't find password_dialog in ui file.");
+        let old_password = builder
+            .get_object::<gtk::Entry>("password-dialog-old-entry")
+            .expect("Can't find password-dialog-old-entry in ui file.");
+        let new_password = builder
+            .get_object::<gtk::Entry>("password-dialog-entry")
+            .expect("Can't find password-dialog-entry in ui file.");
+        let verify_password = builder
+            .get_object::<gtk::Entry>("password-dialog-verify-entry")
+            .expect("Can't find password-dialog-verify-entry in ui file.");
+
+        let actions = AccountSettingsActions::new(main_window.upcast_ref(), app_runtime.clone());
+        self.root
+            .insert_action_group("user-settings", Some(&actions));
+
+        // Body
+        if let Some(action) = actions.lookup_action("change-avatar") {
+            action.bind_button_state(&self.avatar_button);
+            self.avatar_button
+                .set_action_name(Some("user-settings.change-avatar"));
+            self.avatar_button.connect_property_sensitive_notify(
+                clone!(@weak self.avatar_spinner as spinner => move |w| {
+                    if w.get_sensitive() {
+                        spinner.hide();
+                        spinner.stop();
+                    } else {
+                        spinner.start();
+                        spinner.show();
+                    }
+                }),
+            );
+        }
+
+        self.name.connect_property_text_notify(
+            clone!(@strong app_runtime, @strong self.name_button as button => move |w| {
+                app_runtime.update_state_with(clone!(@strong w, @strong button => move |state| {
+                    let username = w.get_text();
+                    if !username.is_empty()
+                        && state
+                            .login_data
+                            .as_ref()
+                            .and_then(|login_data| login_data.username.as_ref())
+                            .filter(|u| **u != username)
+                            .is_some()
+                    {
+                        button.show();
+                        return;
+                    }
+                    button.hide();
+                }));
+            }),
+        );
+
+        self.name
+            .connect_activate(clone!(@strong self.name_button as button => move |_w| {
+                let _ = button.emit("clicked", &[]);
+            }));
+
+        self.name_button
+            .connect_clicked(clone!(@strong app_runtime => move |_w| {
+                app_runtime.update_state_with(|state| state.update_username_account_settings());
+            }));
+
+        fn validate_password_input(builder: &gtk::Builder) {
+            let hint = builder
+                .get_object::<gtk::Label>("password-dialog-verify-hint")
+                .expect("Can't find password-dialog-verify-hint in ui file.");
+            let confirm_password = builder
+                .get_object::<gtk::Button>("password-dialog-apply")
+                .expect("Can't find password-dialog-apply in ui file.");
+            let old = builder
+                .get_object::<gtk::Entry>("password-dialog-old-entry")
+                .expect("Can't find password-dialog-old-entry in ui file.");
+            let new = builder
+                .get_object::<gtk::Entry>("password-dialog-entry")
+                .expect("Can't find password-dialog-entry in ui file.");
+            let verify = builder
+                .get_object::<gtk::Entry>("password-dialog-verify-entry")
+                .expect("Can't find password-dialog-verify-entry in ui file.");
+
+            let mut empty = true;
+            let mut matching = true;
+            let old_p = old.get_text();
+            let new_p = new.get_text();
+            let verify_p = verify.get_text();
+
+            if new_p != verify_p {
+                matching = false;
+            }
+            if !new_p.is_empty() && !verify_p.is_empty() && !old_p.is_empty() {
+                empty = false;
+            }
+
+            if matching {
+                hint.hide();
+            } else {
+                hint.show();
+            }
+
+            confirm_password.set_sensitive(matching && !empty);
+        }
+
+        // Password dialog
+        self.password
+            .connect_clicked(clone!(@strong app_runtime => move |_| {
+                app_runtime.update_state_with(|state| state.show_password_dialog());
+            }));
+
+        password_dialog.connect_delete_event(clone!(@strong app_runtime => move |_, _| {
+            app_runtime.update_state_with(|state| state.close_password_dialog());
+            glib::signal::Inhibit(true)
+        }));
+
+        /* Headerbar */
+        cancel_password.connect_clicked(clone!(@strong app_runtime => move |_| {
+            app_runtime.update_state_with(|state| state.close_password_dialog());
+        }));
+
+        confirm_password.connect_clicked(clone!(@strong app_runtime => move |_| {
+            app_runtime.update_state_with(|state| {
+                state.set_new_password();
+                state.close_password_dialog();
+            });
+        }));
+
+        /* Body */
+        verify_password.connect_property_text_notify(clone!(@strong builder => move |_| {
+            validate_password_input(&builder.clone());
+        }));
+        new_password.connect_property_text_notify(clone!(@strong builder => move |_| {
+            validate_password_input(&builder.clone());
+        }));
+        old_password.connect_property_text_notify(clone!(@strong builder => move |_| {
+            validate_password_input(&builder)
+        }));
+
+        self.delete_password_confirm.connect_property_text_notify(
+            clone!(@strong self.delete_btn as destruction_btn => move |w| {
+                if !w.get_text().is_empty() {
+                    destruction_btn.set_sensitive(true);
+                    return;
+                }
+                destruction_btn.set_sensitive(false);
+            }),
+        );
+
+        self.delete_btn.connect_clicked(move |_| {
+            app_runtime.update_state_with(|state| state.account_destruction());
+        });
+    }
+
     pub fn set_three_pid(&self, data: Option<Vec<ThirdPartyIdentifier>>, op: &AppOp) {
         let mut first_email = true;
         let mut first_phone = true;
diff --git a/fractal-gtk/src/ui/connect/mod.rs b/fractal-gtk/src/ui/connect/mod.rs
index 0249dabc..f7e64b55 100644
--- a/fractal-gtk/src/ui/connect/mod.rs
+++ b/fractal-gtk/src/ui/connect/mod.rs
@@ -1,4 +1,3 @@
-mod account;
 mod autocomplete;
 mod direct;
 mod directory;
@@ -27,7 +26,8 @@ impl UI {
         leave_room::connect(self, app_runtime.clone());
         new_room::connect(self, app_runtime.clone());
         join_room::connect(self, app_runtime.clone());
-        account::connect(self, app_runtime.clone());
+        self.account_settings
+            .connect(&self.builder, &self.main_window, app_runtime.clone());
         invite::connect_dialog(self, app_runtime.clone());
         invite::connect_user(self, app_runtime.clone());
         direct::connect(self, app_runtime.clone());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]