[geary/wip/765516-gtk-widget-conversation-viewer: 173/207] Tidy up avatar loading.



commit 71e56e9c57d00c04759917e3d92be459139f33a5
Author: Michael James Gratton <mike vee net>
Date:   Sat Sep 10 15:24:44 2016 +1000

    Tidy up avatar loading.
    
    * src/client/application/geary-controller.vala (GearyController::open_async):
      Increase size of cache to 10MB.
      src/client/application/geary-controller.vala
      (GearyController::on_conversations_selected): Don't cancel avatary
      loads, that will be handled by the email view's cancellable now.
    
    * src/client/conversation-viewer/conversation-message.vala
      (ConversationMessage::load_avatar): Send avatar HTTP request
      asynchronously.
      (ConversationMessage::set_avatar): Load avatar image stream
      asynchronously.

 src/client/application/geary-controller.vala       |   11 +++----
 .../conversation-viewer/conversation-message.vala  |   28 +++++++++++--------
 2 files changed, 21 insertions(+), 18 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index d17cae7..c370cf6 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -186,16 +186,16 @@ public class GearyController : Geary.BaseObject {
         // for each conversation load.
         File avatar_cache_dir = GearyApplication.instance.get_user_cache_directory()
             .get_child("avatar_cache");
-        avatar_cache = new Soup.Cache(
+        this.avatar_cache = new Soup.Cache(
             avatar_cache_dir.get_path(),
             Soup.CacheType.SINGLE_USER
         );
-        avatar_cache.load();
-        avatar_cache.set_max_size(4 * 1024 * 1024); // 4MB
-        avatar_session = new Soup.Session.with_options(
+        this.avatar_cache.load();
+        this.avatar_cache.set_max_size(10 * 1024 * 1024); // 4MB
+        this.avatar_session = new Soup.Session.with_options(
             Soup.SESSION_USER_AGENT, "Geary/" + GearyApplication.VERSION
         );
-        avatar_session.add_feature(avatar_cache);
+        this.avatar_session.add_feature(avatar_cache);
 
         // Create the main window (must be done after creating actions.)
         main_window = new MainWindow(GearyApplication.instance);
@@ -1509,7 +1509,6 @@ public class GearyController : Geary.BaseObject {
             case 1:
                 // Cancel existing avatar loads before loading new
                 // convo since that will start loading more avatars
-                avatar_session.flush_queue();
                 viewer.load_conversation.begin(
                     Geary.Collection.get_first(selected),
                     this.current_folder,
diff --git a/src/client/conversation-viewer/conversation-message.vala 
b/src/client/conversation-viewer/conversation-message.vala
index 426ac73..447bbb8 100644
--- a/src/client/conversation-viewer/conversation-message.vala
+++ b/src/client/conversation-viewer/conversation-message.vala
@@ -351,7 +351,7 @@ public class ConversationMessage : Gtk.Grid {
     /**
      * Starts loading the avatar for the message's sender.
      */
-    public async void load_avatar(Soup.Session session, Cancellable load_cancellable) {
+    public async void load_avatar(Soup.Session session, Cancellable load_cancelled) {
         // Queued messages are cancelled in ConversationViewer.clear()
         // rather than here using a callback on load_cancellable since
         // we don't have per-message control using
@@ -366,12 +366,16 @@ public class ConversationMessage : Gtk.Grid {
                     primary, Gravatar.Default.NOT_FOUND, pixel_size * window_scale
                 )
             );
-            session.queue_message(message, (session, message) => {
-                    if (message.status_code == 200 &&
-                        !load_cancellable.is_cancelled()) {
-                        set_avatar(message.response_body.data);
-                    }
-                });
+
+            try {
+                InputStream data =
+                    yield session.send_async(message, load_cancelled);
+                if (data != null) {
+                    yield set_avatar(data, load_cancelled);
+                }
+            } catch (Error err) {
+                debug("Unable to load avatar: %s", err.message);
+            }
         }
     }
 
@@ -666,13 +670,13 @@ public class ConversationMessage : Gtk.Grid {
         return value;
     }
 
-    private void set_avatar(uint8[] image_data) {
+    private async void set_avatar(InputStream data,
+                                  Cancellable load_cancelled) {
         Gdk.Pixbuf avatar_buf = null;
-        Gdk.PixbufLoader loader = new Gdk.PixbufLoader();
         try {
-            loader.write(image_data);
-            loader.close();
-            avatar_buf = loader.get_pixbuf();
+            avatar_buf = yield Gdk.Pixbuf.new_from_stream_async(
+                data, load_cancelled
+            );
         } catch (Error err) {
             debug("Error loading Gravatar response: %s", err.message);
         }


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