[fractal/bilelmoussaoui/bump-gtk-rs: 3/7] EntryExt: get_text returns a GString now



commit 5d1a645e340b334e3a3ca1ef480c33f817e772e8
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Wed Jul 15 19:00:24 2020 +0200

    EntryExt: get_text returns a GString now
    
    instead of Option<GString>

 fractal-gtk/src/actions/login.rs               |  16 ++-
 fractal-gtk/src/app/connect/account.rs         |  34 +++----
 fractal-gtk/src/app/connect/roomlist_search.rs |   3 +-
 fractal-gtk/src/appop/account.rs               | 131 ++++++++++++-------------
 fractal-gtk/src/appop/directory.rs             |   2 +-
 fractal-gtk/src/appop/room.rs                  |   6 +-
 fractal-gtk/src/widgets/address.rs             |  42 ++++----
 fractal-gtk/src/widgets/login.rs               |  94 +++++++++---------
 fractal-gtk/src/widgets/members_list.rs        |   2 +-
 fractal-gtk/src/widgets/message.rs             |  30 +++---
 fractal-gtk/src/widgets/message_menu.rs        |   8 +-
 fractal-gtk/src/widgets/room.rs                |   6 +-
 fractal-gtk/src/widgets/room_settings.rs       |  10 +-
 13 files changed, 179 insertions(+), 205 deletions(-)
---
diff --git a/fractal-gtk/src/actions/login.rs b/fractal-gtk/src/actions/login.rs
index d971c2f9..8bac5a90 100644
--- a/fractal-gtk/src/actions/login.rs
+++ b/fractal-gtk/src/actions/login.rs
@@ -89,15 +89,13 @@ pub fn new(
     @weak server_entry,
     @weak err_label
     => move |_, _| {
-        if let Some(txt) = server_entry.get_text() {
-            if txt.is_empty() {
-                err_label.show();
-            } else {
-                err_label.hide();
-                let state = LoginState::Credentials;
-                stack.set_visible_child_name(&state.to_string());
-                back.borrow_mut().push(state);
-            }
+        if server_entry.get_text().is_empty() {
+            err_label.show();
+        } else {
+            err_label.hide();
+            let state = LoginState::Credentials;
+            stack.set_visible_child_name(&state.to_string());
+            back.borrow_mut().push(state);
         }
     }));
 
diff --git a/fractal-gtk/src/app/connect/account.rs b/fractal-gtk/src/app/connect/account.rs
index d314f490..5d36ecbe 100644
--- a/fractal-gtk/src/app/connect/account.rs
+++ b/fractal-gtk/src/app/connect/account.rs
@@ -110,12 +110,13 @@ impl App {
 
         let button = name_btn.clone();
         name_entry.connect_property_text_notify(clone!(@strong op => move |w| {
-            if let Some(text) = w.get_text().filter(|text| !text.is_empty()) {
+            let username = w.get_text();
+            if username != "" {
                 if op.try_lock()
                     .ok()
                     .and_then(|guard| guard.login_data.clone())
                     .and_then(|login_data| login_data.username)
-                    .filter(|username| *username != text)
+                    .filter(|u| *u != username)
                     .is_some()
                 {
                     button.show();
@@ -167,18 +168,17 @@ impl App {
 
             let mut empty = true;
             let mut matching = true;
-            if let Some(new) = new.get_text() {
-                if let Some(verify) = verify.get_text() {
-                    if let Some(old) = old.get_text() {
-                        if new != verify {
-                            matching = false;
-                        }
-                        if new != "" && verify != "" && old != "" {
-                            empty = false;
-                        }
-                    }
-                }
+            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 != "" && verify_p != "" && old_p != "" {
+                empty = false;
             }
+
             if matching {
                 hint.hide();
             } else {
@@ -221,11 +221,9 @@ impl App {
 
         destruction_entry.connect_property_text_notify(
             clone!(@strong destruction_btn => move |w| {
-                if let Some(text) = w.get_text() {
-                    if text != "" {
-                        destruction_btn.set_sensitive(true);
-                        return;
-                    }
+                if w.get_text() != "" {
+                    destruction_btn.set_sensitive(true);
+                    return;
                 }
                 destruction_btn.set_sensitive(false);
             }),
diff --git a/fractal-gtk/src/app/connect/roomlist_search.rs b/fractal-gtk/src/app/connect/roomlist_search.rs
index 68d356a5..6f1b6e00 100644
--- a/fractal-gtk/src/app/connect/roomlist_search.rs
+++ b/fractal-gtk/src/app/connect/roomlist_search.rs
@@ -35,8 +35,7 @@ impl App {
 
         search_entry.connect_search_changed(clone!(@strong op => move |entry| {
             op.lock().unwrap().filter_rooms(
-                entry.get_text()
-                    .map(|gstr| gstr.to_string())
+                Some(entry.get_text().to_string())
             );
         }));
 
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index 2b22a933..b7567f05 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -103,11 +103,9 @@ impl AppOp {
         });
 
         entry.connect_property_text_notify(move |w| {
-            if let Some(text) = w.get_text() {
-                if text != "" {
-                    button.set_sensitive(true);
-                    return;
-                }
+            if w.get_text() != "" {
+                button.set_sensitive(true);
+                return;
             }
             button.set_sensitive(false);
         });
@@ -115,22 +113,22 @@ impl AppOp {
         let value = entry;
         dialog.connect_response(move |w, r| {
             if let gtk::ResponseType::Ok = r {
-                if let Some(token) = value.get_text().map(|gstr| gstr.to_string()) {
-                    let server_url = login_data.server_url.clone();
-                    let secret = secret.clone();
-                    let sid = sid.clone();
-                    thread::spawn(move || {
-                        match user::submit_phone_token(server_url, secret, sid, token) {
-                            Ok((sid, secret)) => {
-                                let secret = Some(secret);
-                                APPOP!(valid_phone_token, (sid, secret));
-                            }
-                            Err(err) => {
-                                err.handle_error();
-                            }
+                let token = value.get_text().to_string();
+
+                let server_url = login_data.server_url.clone();
+                let secret = secret.clone();
+                let sid = sid.clone();
+                thread::spawn(move || {
+                    match user::submit_phone_token(server_url, secret, sid, token) {
+                        Ok((sid, secret)) => {
+                            let secret = Some(secret);
+                            APPOP!(valid_phone_token, (sid, secret));
                         }
-                    });
-                }
+                        Err(err) => {
+                            err.handle_error();
+                        }
+                    }
+                });
             }
             w.destroy();
         });
@@ -594,9 +592,7 @@ impl AppOp {
             .expect("Can't find account_settings_name_button in ui file.");
 
         let old_username = login_data.username.clone().unwrap_or_default();
-        let username = name
-            .get_text()
-            .map_or(String::new(), |gstr| gstr.to_string());
+        let username = name.get_text().to_string();
 
         if old_username != username {
             let spinner = gtk::Spinner::new();
@@ -670,29 +666,27 @@ impl AppOp {
             .get_object::<gtk::Stack>("account_settings_password_stack")
             .expect("Can't find account_settings_password_stack in ui file.");
 
-        if let Some(old) = old_password.get_text() {
-            if let Some(new) = new_password.get_text() {
-                if old != "" && new != "" {
-                    password_btn.set_sensitive(false);
-                    password_btn_stack.set_visible_child_name("spinner");
-                    thread::spawn(move || {
-                        match user::change_password(
-                            login_data.server_url,
-                            login_data.access_token,
-                            login_data.uid.localpart().into(),
-                            old.to_string(),
-                            new.to_string(),
-                        ) {
-                            Ok(_) => {
-                                APPOP!(password_changed);
-                            }
-                            Err(err) => {
-                                err.handle_error();
-                            }
-                        }
-                    });
+        let old = old_password.get_text();
+        let new = new_password.get_text();
+        if old != "" && new != "" {
+            password_btn.set_sensitive(false);
+            password_btn_stack.set_visible_child_name("spinner");
+            thread::spawn(move || {
+                match user::change_password(
+                    login_data.server_url,
+                    login_data.access_token,
+                    login_data.uid.localpart().into(),
+                    old.to_string(),
+                    new.to_string(),
+                ) {
+                    Ok(_) => {
+                        APPOP!(password_changed);
+                    }
+                    Err(err) => {
+                        err.handle_error();
+                    }
                 }
-            }
+            });
         }
     }
 
@@ -787,31 +781,30 @@ impl AppOp {
         dialog.add_button("Cancel", gtk::ResponseType::Cancel);
 
         let _flag = mark.get_active(); // TODO: This is not used, remove from UI?
-        if let Some(password) = entry.get_text().map(|gstr| gstr.to_string()) {
-            dialog.connect_response(move |w, r| {
-                if let gtk::ResponseType::Ok = r {
-                    let password = password.clone();
-                    let login_data = login_data.clone();
-                    thread::spawn(move || {
-                        match user::account_destruction(
-                            login_data.server_url.clone(),
-                            login_data.access_token.clone(),
-                            login_data.uid.localpart().into(),
-                            password,
-                        ) {
-                            Ok(_) => {
-                                APPOP!(account_destruction_logoff);
-                            }
-                            Err(err) => {
-                                err.handle_error();
-                            }
+        let password = entry.get_text().to_string();
+        dialog.connect_response(move |w, r| {
+            if let gtk::ResponseType::Ok = r {
+                let password = password.clone();
+                let login_data = login_data.clone();
+                thread::spawn(move || {
+                    match user::account_destruction(
+                        login_data.server_url.clone(),
+                        login_data.access_token.clone(),
+                        login_data.uid.localpart().into(),
+                        password,
+                    ) {
+                        Ok(_) => {
+                            APPOP!(account_destruction_logoff);
                         }
-                    });
-                }
-                w.destroy();
-            });
-            dialog.show_all();
-        }
+                        Err(err) => {
+                            err.handle_error();
+                        }
+                    }
+                });
+            }
+            w.destroy();
+        });
+        dialog.show_all();
     }
     pub fn account_destruction_logoff(&self) {
         /* Do logout */
diff --git a/fractal-gtk/src/appop/directory.rs b/fractal-gtk/src/appop/directory.rs
index b34d91c6..76fdc33c 100644
--- a/fractal-gtk/src/appop/directory.rs
+++ b/fractal-gtk/src/appop/directory.rs
@@ -128,7 +128,7 @@ impl AppOp {
             q.set_sensitive(false);
         }
 
-        let search_term = q.get_text().unwrap().to_string();
+        let search_term = q.get_text().to_string();
         if let RoomSearchPagination::NoMorePages = self.directory_pagination {
             // there are no more rooms. We don't need to request for more
             return;
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 1231d20c..ec1864b4 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -385,9 +385,7 @@ impl AppOp {
             .get_object::<gtk::ToggleButton>("private_visibility_button")
             .expect("Can't find private_visibility_button in ui file.");
 
-        let n = name
-            .get_text()
-            .map_or(String::new(), |gstr| gstr.to_string());
+        let n = name.get_text().to_string();
         // Since the switcher
         let privacy = if private.get_active() {
             room::RoomType::Private
@@ -554,7 +552,7 @@ impl AppOp {
             .get_object::<gtk::Entry>("join_room_name")
             .expect("Can't find join_room_name in ui file.")
             .get_text()
-            .map_or(String::new(), |gstr| gstr.to_string())
+            .to_string()
             .trim()
             .try_into();
 
diff --git a/fractal-gtk/src/widgets/address.rs b/fractal-gtk/src/widgets/address.rs
index 43c57c2d..d92bf903 100644
--- a/fractal-gtk/src/widgets/address.rs
+++ b/fractal-gtk/src/widgets/address.rs
@@ -134,19 +134,18 @@ impl<'a> Address<'a> {
         let button = self.button.clone();
         let medium = self.medium.clone();
         self.entry.connect_property_text_notify(move |w| {
-            if let Some(text) = w.get_text() {
-                if text != "" {
-                    /* FIXME: use better validation */
-                    match medium {
-                        AddressType::Email => {
-                            button.set_sensitive(text.contains('@') && text.contains('.'));
-                        }
-                        AddressType::Phone => {}
-                    };
-                    button.show();
-                } else {
-                    button.hide();
-                }
+            let username = w.get_text();
+            if username != "" {
+                /* FIXME: use better validation */
+                match medium {
+                    AddressType::Email => {
+                        button.set_sensitive(username.contains('@') && username.contains('.'));
+                    }
+                    AddressType::Phone => {}
+                };
+                button.show();
+            } else {
+                button.hide();
             }
         });
 
@@ -187,15 +186,14 @@ impl<'a> Address<'a> {
                     }
                 }
                 Some(AddressAction::Add) => {
-                    if let Some(address) = entry.get_text().map(|gstr| gstr.to_string()) {
-                        add_address(
-                            medium,
-                            id_server.clone(),
-                            address,
-                            server_url.clone(),
-                            access_token.clone(),
-                        );
-                    }
+                    let address = entry.get_text().to_string();
+                    add_address(
+                        medium,
+                        id_server.clone(),
+                        address,
+                        server_url.clone(),
+                        access_token.clone(),
+                    );
                 }
                 _ => {}
             }
diff --git a/fractal-gtk/src/widgets/login.rs b/fractal-gtk/src/widgets/login.rs
index f7d47bff..fba8573c 100644
--- a/fractal-gtk/src/widgets/login.rs
+++ b/fractal-gtk/src/widgets/login.rs
@@ -72,56 +72,54 @@ impl LoginWidget {
         @weak password_entry,
         @weak err_label
         => move |_, _| {
-            if let Some(txt) = server_entry.get_text() {
-                let username = username_entry
-                    .get_text()
-                    .map_or(String::new(), |gstr| gstr.to_string());
-
-                let password = password_entry
-                    .get_text()
-                    .map_or(String::new(), |gstr| gstr.to_string());
-
-                let txt = String::from(txt).trim().to_string();
-                let txt = if txt.starts_with("http://";) || txt.starts_with("https://";) {
-                    txt
+            let username = username_entry
+                .get_text()
+                .to_string();
+
+            let password = password_entry
+                .get_text()
+                .to_string();
+
+            let txt = server_entry.get_text().to_string().trim().to_string();
+            let txt = if txt.starts_with("http://";) || txt.starts_with("https://";) {
+                txt
+            } else {
+                format!("https://{}";, &txt)
+            };
+            let txt = if !txt.ends_with('/') { txt + "/" } else { txt };
+
+            if !password.is_empty() && !username.is_empty() {
+                // take the user's homeserver value if the
+                // well-known request fails
+                let mut homeserver_url = if let Ok(hs_url) = Url::parse(&txt) {
+                    hs_url
                 } else {
-                    format!("https://{}";, &txt)
+                    let msg = i18n("Malformed server URL");
+                    ErrorDialog::new(false, &msg);
+                    return;
                 };
-                let txt = if !txt.ends_with('/') { txt + "/" } else { txt };
-
-                if !password.is_empty() && !username.is_empty() {
-                    // take the user's homeserver value if the
-                    // well-known request fails
-                    let mut homeserver_url = if let Ok(hs_url) = Url::parse(&txt) {
-                        hs_url
-                    } else {
-                        let msg = i18n("Malformed server URL");
-                        ErrorDialog::new(false, &msg);
-                        return;
-                    };
-
-                    let mut idserver = globals::DEFAULT_IDENTITYSERVER.clone();
-                    match get_well_known(homeserver_url.clone()) {
-                        Ok(response) => {
-                            info!("Got well-known response from {}: {:#?}", &txt, response);
-                            homeserver_url = response.homeserver.base_url;
-                            idserver = response
-                                .identity_server
-                                .map(|ids| ids.base_url)
-                                .unwrap_or(idserver);
-                        }
-                        Err(e) => info!("Failed to .well-known request: {:#?}", e),
-                    };
-
-                    err_label.hide();
-                    op.lock().unwrap().set_state(AppState::Loading);
-                    op.lock().unwrap().since = None;
-                    op.lock()
-                        .unwrap()
-                        .connect(username, password, homeserver_url, idserver);
-                } else {
-                    err_label.show();
-                }
+
+                let mut idserver = globals::DEFAULT_IDENTITYSERVER.clone();
+                match get_well_known(homeserver_url.clone()) {
+                    Ok(response) => {
+                        info!("Got well-known response from {}: {:#?}", &txt, response);
+                        homeserver_url = response.homeserver.base_url;
+                        idserver = response
+                            .identity_server
+                            .map(|ids| ids.base_url)
+                            .unwrap_or(idserver);
+                    }
+                    Err(e) => info!("Failed to .well-known request: {:#?}", e),
+                };
+
+                err_label.hide();
+                op.lock().unwrap().set_state(AppState::Loading);
+                op.lock().unwrap().since = None;
+                op.lock()
+                    .unwrap()
+                    .connect(username, password, homeserver_url, idserver);
+            } else {
+                err_label.show();
             }
         }));
 
diff --git a/fractal-gtk/src/widgets/members_list.rs b/fractal-gtk/src/widgets/members_list.rs
index 9b8c6845..bb65479f 100644
--- a/fractal-gtk/src/widgets/members_list.rs
+++ b/fractal-gtk/src/widgets/members_list.rs
@@ -84,7 +84,7 @@ impl MembersList {
                 container.clone(),
                 members.clone(),
                 error.clone(),
-                w.get_text().map(|gstr| gstr.to_string()),
+                Some(w.get_text().to_string()),
             );
         });
         /* we need to remove the handler when the member list is destroyed */
diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs
index 126edaae..dccb92fa 100644
--- a/fractal-gtk/src/widgets/message.rs
+++ b/fractal-gtk/src/widgets/message.rs
@@ -340,33 +340,27 @@ impl MessageBox {
             for part in msg_parts.iter() {
                 let highlights = msg.highlights.clone();
                 part.connect_property_cursor_position_notify(move |w| {
-                    if let Some(text) = w.get_text() {
-                        let attr = pango::AttrList::new();
-                        for light in highlights.clone() {
-                            highlight_username(w.clone(), &attr, &light, text.to_string());
-                        }
-                        w.set_attributes(Some(&attr));
+                    let attr = pango::AttrList::new();
+                    for light in highlights.clone() {
+                        highlight_username(w.clone(), &attr, &light, w.get_text().to_string());
                     }
+                    w.set_attributes(Some(&attr));
                 });
 
                 let highlights = msg.highlights.clone();
                 part.connect_property_selection_bound_notify(move |w| {
-                    if let Some(text) = w.get_text() {
-                        let attr = pango::AttrList::new();
-                        for light in highlights.clone() {
-                            highlight_username(w.clone(), &attr, &light, text.to_string());
-                        }
-                        w.set_attributes(Some(&attr));
+                    let attr = pango::AttrList::new();
+                    for light in highlights.clone() {
+                        highlight_username(w.clone(), &attr, &light, w.get_text().to_string());
                     }
+                    w.set_attributes(Some(&attr));
                 });
 
-                if let Some(text) = part.get_text() {
-                    let attr = pango::AttrList::new();
-                    for light in msg.highlights.clone() {
-                        highlight_username(part.clone(), &attr, &light, text.to_string());
-                    }
-                    part.set_attributes(Some(&attr));
+                let attr = pango::AttrList::new();
+                for light in msg.highlights.clone() {
+                    highlight_username(part.clone(), &attr, &light, part.get_text().to_string());
                 }
+                part.set_attributes(Some(&attr));
             }
         }
 
diff --git a/fractal-gtk/src/widgets/message_menu.rs b/fractal-gtk/src/widgets/message_menu.rs
index 7abec213..6a5e1db5 100644
--- a/fractal-gtk/src/widgets/message_menu.rs
+++ b/fractal-gtk/src/widgets/message_menu.rs
@@ -194,8 +194,12 @@ fn get_selected_text(event_widget: Option<&gtk::Label>) -> Option<SelectedText>
     let w = event_widget?;
     match w.get_selection_bounds() {
         Some((s, e)) => {
-            let text = w.get_text()?;
-            let slice: String = text.chars().take(e as usize).skip(s as usize).collect();
+            let slice: String = w
+                .get_text()
+                .chars()
+                .take(e as usize)
+                .skip(s as usize)
+                .collect();
             Some(SelectedText {
                 widget: w.downgrade(),
                 text: slice,
diff --git a/fractal-gtk/src/widgets/room.rs b/fractal-gtk/src/widgets/room.rs
index 389980f1..9b76c12a 100644
--- a/fractal-gtk/src/widgets/room.rs
+++ b/fractal-gtk/src/widgets/room.rs
@@ -102,11 +102,7 @@ impl<'a> RoomBox<'a> {
             alias_label.set_xalign(0.0);
 
             details_box.add(&name_label);
-            if !topic_label
-                .get_text()
-                .map_or(String::new(), |gstr| gstr.to_string())
-                .is_empty()
-            {
+            if !topic_label.get_text().to_string().is_empty() {
                 details_box.add(&topic_label);
             }
             details_box.add(&alias_label);
diff --git a/fractal-gtk/src/widgets/room_settings.rs b/fractal-gtk/src/widgets/room_settings.rs
index 3b0a0506..7f5e8473 100644
--- a/fractal-gtk/src/widgets/room_settings.rs
+++ b/fractal-gtk/src/widgets/room_settings.rs
@@ -128,8 +128,7 @@ impl RoomSettings {
         let button = name_btn.clone();
         name_entry.connect_property_text_notify(clone!(@strong this => move |w| {
             let result = this.borrow().validate_room_name(
-                w.get_text()
-                    .map(|gstr| gstr.to_string())
+                Some(w.get_text().to_string())
             );
             button.set_visible(result.is_some());
         }));
@@ -137,8 +136,7 @@ impl RoomSettings {
         let button = topic_btn.clone();
         topic_entry.connect_property_text_notify(clone!(@strong this => move |w| {
             let result = this.borrow().validate_room_topic(
-                w.get_text()
-                    .map(|gstr| gstr.to_string())
+                Some(w.get_text().to_string())
             );
             button.set_visible(result.is_some());
         }));
@@ -485,7 +483,7 @@ impl RoomSettings {
             .get_object::<gtk::Button>("room_settings_room_name_button")
             .expect("Can't find room_settings_name_button in ui file.");
 
-        let new_name = entry.get_text()?.to_string();
+        let new_name = entry.get_text().to_string();
         let room = &self.room;
 
         let spinner = gtk::Spinner::new();
@@ -542,7 +540,7 @@ impl RoomSettings {
             .builder
             .get_object::<gtk::Button>("room_settings_room_topic_button")
             .expect("Can't find room_settings_topic_button in ui file.");
-        let topic = name.get_text()?.to_string();
+        let topic = name.get_text().to_string();
 
         let room = &self.room;
 


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