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




commit 2825c2b7cb00866f349e566f6323482909145649
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/po/POTFILES.in            |   1 -
 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 +-
 5 files changed, 171 insertions(+), 196 deletions(-)
---
diff --git a/fractal-gtk/po/POTFILES.in b/fractal-gtk/po/POTFILES.in
index a4de6785..b1790575 100644
--- a/fractal-gtk/po/POTFILES.in
+++ b/fractal-gtk/po/POTFILES.in
@@ -97,7 +97,6 @@ fractal-gtk/src/passwd.rs
 fractal-gtk/src/ui/about.rs
 fractal-gtk/src/ui/account.rs
 fractal-gtk/src/ui/attach.rs
-fractal-gtk/src/ui/connect/account.rs
 fractal-gtk/src/ui/connect/autocomplete.rs
 fractal-gtk/src/ui/connect/direct.rs
 fractal-gtk/src/ui/connect/directory.rs
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]