[geary] Allow using foreach loops over ConversationEmail's messages.



commit 0c929683bcf5f834296951742fd234ab8892faea
Author: Michael James Gratton <mike vee net>
Date:   Thu Feb 23 10:19:40 2017 +1100

    Allow using foreach loops over ConversationEmail's messages.
    
    * src/client/conversation-viewer/conversation-email.vala
      (ConversationEmail): Rename ::message_view_iterator to just ::iterator,
      update all call sites to use for-each loops rather than
      Gee.Iterator::foreach closures.

 src/client/application/geary-controller.vala       |   32 +++++++--------
 .../conversation-viewer/conversation-email.vala    |   41 +++++++++---------
 .../conversation-viewer/conversation-list-box.vala |   44 ++++++++++---------
 3 files changed, 59 insertions(+), 58 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 240c51f..4f260e9 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -2762,24 +2762,22 @@ public class GearyController : Geary.BaseObject {
                     draft_view.email, null, null, true
                 );
             });
-        view.message_view_iterator().foreach((mview) => {
-                mview.link_activated.connect((link) => {
-                        if (link.down().has_prefix(
-                                Geary.ComposedEmail.MAILTO_SCHEME)) {
-                            compose_mailto(link);
-                        } else {
-                            open_uri(link);
-                        }
-                    });
-                mview.save_image.connect((url, alt_text, buf) => {
-                        on_save_image_extended(view, url, alt_text, buf);
+        foreach (ConversationMessage msg_view in view) {
+            msg_view.link_activated.connect((link) => {
+                    if (link.down().has_prefix(Geary.ComposedEmail.MAILTO_SCHEME)) {
+                        compose_mailto(link);
+                    } else {
+                        open_uri(link);
+                    }
                 });
-                mview.search_activated.connect((op, value) => {
-                        string search = op + ":" + value;
-                        show_search_bar(search);
-                    });
-                return true;
-            });
+            msg_view.save_image.connect((url, alt_text, buf) => {
+                    on_save_image_extended(view, url, alt_text, buf);
+                });
+            msg_view.search_activated.connect((op, value) => {
+                    string search = op + ":" + value;
+                    show_search_bar(search);
+                });
+        }
         view.save_attachments.connect(on_save_attachments);
         view.view_source.connect(on_view_source);
     }
diff --git a/src/client/conversation-viewer/conversation-email.vala 
b/src/client/conversation-viewer/conversation-email.vala
index 725c182..fa6b84c 100644
--- a/src/client/conversation-viewer/conversation-email.vala
+++ b/src/client/conversation-viewer/conversation-email.vala
@@ -25,7 +25,10 @@ public class ConversationEmail : Gtk.Box {
      * Iterator that returns all message views in an email view.
      */
     private class MessageViewIterator :
-        Gee.Traversable<ConversationMessage>, Gee.Iterator<ConversationMessage>, Object {
+        Gee.Traversable<ConversationMessage>,
+        Gee.Iterator<ConversationMessage>,
+        Object {
+
 
         public bool read_only {
             get { return true; }
@@ -498,17 +501,16 @@ public class ConversationEmail : Gtk.Box {
      * attachment names, types and icons.
      */
     public async void start_loading(Cancellable load_cancelled) {
-        message_view_iterator().foreach((view) => {
-                if (!load_cancelled.is_cancelled()) {
-                    primary_message.load_message_body.begin(load_cancelled);
-                }
-                view.load_avatar.begin(
-                    GearyApplication.instance.controller.avatar_session,
-                    load_cancelled
-                );
-
-                return !load_cancelled.is_cancelled();
-            });
+        foreach (ConversationMessage view in this)  {
+            if (load_cancelled.is_cancelled()) {
+                break;
+            }
+            yield primary_message.load_message_body(load_cancelled);
+            view.load_avatar.begin(
+                GearyApplication.instance.controller.avatar_session,
+                load_cancelled
+            );
+        }
 
         // Only load attachments once the web views have finished
         // loading, since we want to know if any attachments marked as
@@ -596,7 +598,7 @@ public class ConversationEmail : Gtk.Box {
     /**
      * Returns a new Iterable over all message views in this email view
      */
-    internal Gee.Iterator<ConversationMessage> message_view_iterator() {
+    internal Gee.Iterator<ConversationMessage> iterator() {
         return new MessageViewIterator(this);
     }
 
@@ -622,13 +624,12 @@ public class ConversationEmail : Gtk.Box {
             });
         view.web_view.notify["has-valid-height"].connect(() => {
                 bool all_loaded = true;
-                message_view_iterator().foreach((view) => {
-                        if (!view.web_view.has_valid_height) {
-                            all_loaded = false;
-                            return false;
-                        }
-                        return true;
-                    });
+                foreach (ConversationMessage message in this) {
+                    if (!message.web_view.has_valid_height) {
+                        all_loaded = false;
+                        break;
+                    }
+                }
                 if (all_loaded == true && !this.message_bodies_loaded) {
                     // Only update the property value if not already
                     // true
diff --git a/src/client/conversation-viewer/conversation-list-box.vala 
b/src/client/conversation-viewer/conversation-list-box.vala
index 2024c2e..97dffba 100644
--- a/src/client/conversation-viewer/conversation-list-box.vala
+++ b/src/client/conversation-viewer/conversation-list-box.vala
@@ -189,12 +189,11 @@ public class ConversationListBox : Gtk.ListBox {
 
         public override void expand() {
             this.is_expanded = true;
-            this.view.message_view_iterator().foreach((view) => {
-                    if (!view.web_view.has_valid_height) {
-                        view.web_view.queue_resize();
-                    }
-                    return true;
-                });
+            foreach (ConversationMessage message in this.view) {
+                if (!message.web_view.has_valid_height) {
+                    message.web_view.queue_resize();
+                }
+            };
             update_row_expansion();
         }
 
@@ -686,9 +685,12 @@ public class ConversationListBox : Gtk.ListBox {
             new Gee.TreeSet<string>((a, b) => a.length - b.length);
         ordered_matches.add_all(search_matches);
         this.ordered_search_terms = ordered_matches;
-        this.foreach((child) => {
-                apply_search_terms((EmailRow) child);
-            });
+        foreach (Gtk.Widget child in get_children()) {
+            EmailRow? row = child as EmailRow;
+            if (row != null) {
+                apply_search_terms(row);
+            }
+        }
     }
 
     /**
@@ -697,13 +699,14 @@ public class ConversationListBox : Gtk.ListBox {
     public void unmark_search_terms() {
         this.ordered_search_terms = null;
         this.foreach((child) => {
-                EmailRow row = (EmailRow) child;
-                if (row.is_search_match) {
-                    row.is_search_match = false;
-                    row.view.message_view_iterator().foreach((msg_view) => {
+                EmailRow? row = child as EmailRow;
+                if (row != null) {
+                    if (row.is_search_match) {
+                        row.is_search_match = false;
+                        foreach (ConversationMessage msg_view in row.view) {
                             msg_view.unmark_search_terms();
-                            return true;
-                        });
+                        }
+                    }
                 }
             });
     }
@@ -966,17 +969,16 @@ public class ConversationListBox : Gtk.ListBox {
 
     private inline void apply_search_terms_impl(EmailRow row) {
         bool found = false;
-        row.view.message_view_iterator().foreach((view) => {
+        foreach (ConversationMessage view in row.view) {
                 if (view.highlight_search_terms(this.ordered_search_terms) > 0) {
                     found = true;
-                    return false;
+                    break;
                 }
-                return true;
-            });
-        row.is_search_match = found;
+        }
         if (found) {
             search_matches_found();
         }
+        row.is_search_match = found;
     }
 
     /**
@@ -994,7 +996,7 @@ public class ConversationListBox : Gtk.ListBox {
     private Gee.Iterator<ConversationMessage> message_view_iterator() {
         return Gee.Iterator.concat<ConversationMessage>(
             email_view_iterator().map<Gee.Iterator<ConversationMessage>>(
-                (email_view) => { return email_view.message_view_iterator(); }
+                (email_view) => { return email_view.iterator(); }
             )
         );
     }


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