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




commit fe05b8f586ca99a9742a3686cb415a9f3c362af8
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>

 Cargo.lock                                     |   1 -
 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               | 100 +++++++++----------
 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 +-
 14 files changed, 179 insertions(+), 212 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 93657fea..d0f1733e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -952,7 +952,6 @@ name = "gstreamer"
 version = "0.16.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 checksum = "ce4ce1ba28d3293b8cb8c3d33f50e6da2e5cfeefa59a0d10d922ab8015791609"
-
 dependencies = [
  "bitflags",
  "cfg-if",
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 2caea2f9..3cc96e15 100644
--- a/fractal-gtk/src/app/connect/account.rs
+++ b/fractal-gtk/src/app/connect/account.rs
@@ -104,12 +104,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();
@@ -161,18 +162,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.is_empty() && !verify.is_empty() && !old.is_empty() {
-                            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.is_empty() && !verify_p.is_empty() && !old_p.is_empty() {
+                empty = false;
             }
+
             if matching {
                 hint.hide();
             } else {
@@ -215,11 +215,9 @@ impl App {
 
         destruction_entry.connect_property_text_notify(
             clone!(@strong destruction_btn => move |w| {
-                if let Some(text) = w.get_text() {
-                    if !text.is_empty() {
-                        destruction_btn.set_sensitive(true);
-                        return;
-                    }
+                if !w.get_text().is_empty() {
+                    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 d515aacf..6d7d0e60 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.is_empty() {
-                    button.set_sensitive(true);
-                    return;
-                }
+            if !w.get_text().is_empty() {
+                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();
         });
@@ -603,9 +601,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();
@@ -679,29 +675,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.is_empty() && !new.is_empty() {
-                    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();
+                    }
                 }
-            }
+            });
         }
     }
 
@@ -796,31 +790,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 e123432d..07eeb676 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -386,9 +386,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
@@ -562,7 +560,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 6d51d38a..04817d50 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.is_empty() {
-                    /* 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.is_empty() {
+                /* 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 46857eac..fba8573c 100644
--- a/fractal-gtk/src/widgets/login.rs
+++ b/fractal-gtk/src/widgets/login.rs
@@ -72,62 +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 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 (homeserver_url, idserver) = get_well_known(homeserver_url.clone())
-                        .and_then(|response| {
-                            let hs_url = Url::parse(&response.homeserver.base_url)?;
-                            let ids = response
-                                .identity_server
-                                .as_ref()
-                                .map(|ids| Url::parse(&ids.base_url))
-                                .transpose()?
-                                .unwrap_or(globals::DEFAULT_IDENTITYSERVER.clone());
-                            info!("Got well-known response from {}: {:#?}", &txt, response);
-
-                            Ok((hs_url, ids))
-                        })
-                        .map_err(|e| {
-                            info!("Failed to .well-known request: {:#?}", e);
-                            e
-                        })
-                        .unwrap_or((homeserver_url, globals::DEFAULT_IDENTITYSERVER.clone()));
-
-                    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 a1ef7526..73970ebb 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 7248af73..ebb8ae64 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 0b70d48a..d0d8ee2e 100644
--- a/fractal-gtk/src/widgets/room_settings.rs
+++ b/fractal-gtk/src/widgets/room_settings.rs
@@ -134,8 +134,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());
         }));
@@ -143,8 +142,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());
         }));
@@ -512,7 +510,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();
@@ -569,7 +567,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]