[geary/wip/730682-refine-convo-list: 23/37] Simple, inefficient preview loader implementation.



commit e8233a5f3422603d7f0b1e0d6654d137053ee625
Author: Michael James Gratton <mike vee net>
Date:   Sun Oct 15 21:03:28 2017 +1030

    Simple, inefficient preview loader implementation.
    
    * src/client/conversation-list/preview-loader.vala (PreviewLoader): Very
      early implementation of a preview loader class.
    
    * src/client/conversation-list/conversation-list-item.vala
      (ConversationListItem): Accept a preview loader as a ctor arg, use it
      for loading the preview when updating the UI.
    
    * src/client/conversation-list/conversation-list.vala (ConversationList):
      Construct a new preview loader when binding a new model, pass it to the
      items when constructed.

 po/POTFILES.in                                     |    1 +
 src/CMakeLists.txt                                 |    2 +-
 .../conversation-list/conversation-list-item.vala  |   16 ++++--
 .../conversation-list/conversation-list.vala       |    9 +++-
 src/client/conversation-list/preview-loader.vala   |   61 ++++++++++++++++++++
 5 files changed, 82 insertions(+), 7 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e71383e..11bd439 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -57,6 +57,7 @@ src/client/conversation-list/conversation-list-model.vala
 src/client/conversation-list/conversation-list-store.vala
 src/client/conversation-list/conversation-list-view.vala
 src/client/conversation-list/formatted-conversation-data.vala
+src/client/conversation-list/preview-loader.vala
 src/client/conversation-viewer/conversation-email.vala
 src/client/conversation-viewer/conversation-list-box.vala
 src/client/conversation-viewer/conversation-message.vala
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7b9b6f6..78a1ec2 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -374,7 +374,7 @@ client/conversation-list/conversation-list-model.vala
 client/conversation-list/conversation-list-store.vala
 client/conversation-list/conversation-list-view.vala
 client/conversation-list/formatted-conversation-data.vala
-
+client/conversation-list/preview-loader.vala
 client/conversation-viewer/conversation-email.vala
 client/conversation-viewer/conversation-list-box.vala
 client/conversation-viewer/conversation-message.vala
diff --git a/src/client/conversation-list/conversation-list-item.vala 
b/src/client/conversation-list/conversation-list-item.vala
index 0a6ea0c..af3b1e8 100644
--- a/src/client/conversation-list/conversation-list-item.vala
+++ b/src/client/conversation-list/conversation-list-item.vala
@@ -97,15 +97,18 @@ public class ConversationListItem : Gtk.Grid {
     private Geary.App.Conversation conversation;
     private Gee.List<Geary.RFC822.MailboxAddress> account_addresses;
     private bool use_to;
+    private PreviewLoader preview_loader;
     private Configuration config;
 
     public ConversationListItem(Geary.App.Conversation conversation,
                                 Gee.List<Geary.RFC822.MailboxAddress> account_addresses,
                                 bool use_to,
+                                PreviewLoader preview_loader,
                                 Configuration config) {
         this.conversation = conversation;
         this.account_addresses = account_addresses;
         this.use_to = use_to;
+        this.preview_loader = preview_loader;
         this.config = config;
 
         this.conversation.appended.connect(() => { update(); });
@@ -158,14 +161,17 @@ public class ConversationListItem : Gtk.Grid {
             );
         }
 
-        string preview_text = "long long long long preview";
         if (this.config.display_preview) {
             // XXX load & format preview here
-            // preview_text = XXXX;
-            preview.set_text(preview_text);
-            preview.show();
+            this.preview_loader.load.begin(preview_message, (obj, ret) => {
+                    string? preview_text = this.preview_loader.load.end(ret);
+                    if (preview_text != null) {
+                        this.preview.set_text(preview_text);
+                    }
+                });
+            this.preview.show();
         } else {
-            preview.hide();
+            this.preview.hide();
         }
 
         // conversation list store sorts by date-received, so
diff --git a/src/client/conversation-list/conversation-list.vala 
b/src/client/conversation-list/conversation-list.vala
index c404b97..5972773 100644
--- a/src/client/conversation-list/conversation-list.vala
+++ b/src/client/conversation-list/conversation-list.vala
@@ -59,19 +59,26 @@ public class ConversationList : Gtk.ListBox {
     }
 
     public new void bind_model(Geary.App.ConversationMonitor monitor) {
+        Geary.Folder displayed = monitor.base_folder;
+        Geary.App.EmailStore store = new Geary.App.EmailStore(displayed.account);
+        PreviewLoader loader = new PreviewLoader(store, new Cancellable()); // XXX
+
         monitor.scan_started.connect(on_scan_started);
         monitor.scan_completed.connect(on_scan_completed);
+        monitor.scan_completed.connect(() => {
+                loader.load_remote();
+            });
 
         this.model = new ConversationListModel(monitor);
         this.model.items_changed.connect(on_model_items_changed);
 
-        Geary.Folder displayed = monitor.folder;
         Gee.List<Geary.RFC822.MailboxAddress> account_addresses = 
displayed.account.information.get_all_mailboxes();
         bool use_to = (displayed != null) && displayed.special_folder_type.is_outgoing();
         base.bind_model(this.model, (convo) => {
                 return new ConversationListItem(convo as Geary.App.Conversation,
                                                 account_addresses,
                                                 use_to,
+                                                loader,
                                                 this.config);
             }
         );
diff --git a/src/client/conversation-list/preview-loader.vala 
b/src/client/conversation-list/preview-loader.vala
new file mode 100644
index 0000000..29d3445
--- /dev/null
+++ b/src/client/conversation-list/preview-loader.vala
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2017 Michael Gratton <mike vee net>
+ * Copyright 2016 Software Freedom Conservancy Inc.
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+/**
+ * Loads message body previews for conversation list items asynchronously.
+ */
+public class PreviewLoader : Geary.BaseObject {
+
+    // XXX Remove ALL and NONE when PREVIEW has been fixed. See Bug 714317.
+    private const Geary.Email.Field WITH_PREVIEW_FIELDS =
+        Geary.Email.Field.ENVELOPE | Geary.Email.Field.FLAGS |
+        Geary.Email.Field.PROPERTIES | Geary.Email.Field.PREVIEW |
+        Geary.Email.Field.ALL | Geary.Email.Field.NONE;
+
+    private Geary.App.EmailStore email_store;
+    private Cancellable cancellable;
+    private bool loading_local_only = true;
+
+
+    public PreviewLoader(Geary.App.EmailStore email_store, Cancellable cancellable) {
+        this.email_store = email_store;
+        this.cancellable = cancellable;
+    }
+
+    public void load_remote() {
+        this.loading_local_only = false;
+    }
+
+    public async string? load(Geary.Email target) {
+        Gee.Collection<Geary.EmailIdentifier> pending = new Gee.HashSet<Geary.EmailIdentifier>();
+        pending.add(target.id);
+
+        Geary.Folder.ListFlags flags = (this.loading_local_only)
+            ? Geary.Folder.ListFlags.LOCAL_ONLY
+            : Geary.Folder.ListFlags.NONE;
+
+        Gee.Collection<Geary.Email>? emails = null;
+        try {
+            emails = yield email_store.list_email_by_sparse_id_async(
+                pending, ConversationListStore.WITH_PREVIEW_FIELDS, flags, this.cancellable
+            );
+        } catch (Error err) {
+            // Ignore NOT_FOUND, as that's entirely possible when waiting for the remote to open
+            if (!(err is Geary.EngineError.NOT_FOUND))
+                debug("Unable to fetch preview: %s", err.message);
+        }
+
+        Geary.Email? loaded = Geary.Collection.get_first(emails);
+        string? preview = null;
+        if (loaded != null) {
+            preview = Geary.String.reduce_whitespace(loaded.get_preview_as_string());
+        }
+        return preview;
+    }
+
+}


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