[geary/wip/3.32-avatars: 3/5] Load avatars from Folks



commit e456dca11c0e5af8f72475e1c2b1da04bc4cc00b
Author: Michael Gratton <mike vee net>
Date:   Tue Feb 26 23:55:27 2019 +1100

    Load avatars from Folks
    
    Take a brute-force approach here for the moment.

 .../application/application-avatar-store.vala      | 138 ++++++---------------
 src/client/application/geary-controller.vala       |  14 ++-
 2 files changed, 47 insertions(+), 105 deletions(-)
---
diff --git a/src/client/application/application-avatar-store.vala 
b/src/client/application/application-avatar-store.vala
index a5a2495b..2ce2f2f8 100644
--- a/src/client/application/application-avatar-store.vala
+++ b/src/client/application/application-avatar-store.vala
@@ -12,119 +12,55 @@
 public class Application.AvatarStore : Geary.BaseObject {
 
 
-    // Initiates and manages an avatar load using Gravatar
-    private class AvatarLoader : Geary.BaseObject {
+    private Folks.IndividualAggregator individuals;
 
-        internal Gdk.Pixbuf? avatar = null;
-        internal Geary.Nonblocking.Semaphore lock =
-            new Geary.Nonblocking.Semaphore();
-
-        private string base_url;
-        private Geary.RFC822.MailboxAddress address;
-        private int pixel_size;
 
+    public AvatarStore(Folks.IndividualAggregator individuals) {
+        this.individuals = individuals;
+    }
 
-        internal AvatarLoader(Geary.RFC822.MailboxAddress address,
-                              string base_url,
-                              int pixel_size) {
-            this.address = address;
-            this.base_url = base_url;
-            this.pixel_size = pixel_size;
-        }
+    public void close() {
+        // Noop
+    }
 
-        internal async void load(Soup.Session session,
-                                 Cancellable load_cancelled)
-            throws GLib.Error {
-            Error? workaround_err = null;
-            if (!Geary.String.is_empty_or_whitespace(this.base_url)) {
-                string md5 = GLib.Checksum.compute_for_string(
-                    GLib.ChecksumType.MD5, this.address.address.strip().down()
-                );
-                Soup.Message message = new Soup.Message(
-                    "GET",
-                    "%s/%s?d=%s&s=%d".printf(
-                        this.base_url, md5, "404", this.pixel_size
+    public async Gdk.Pixbuf? load(Geary.RFC822.MailboxAddress mailbox,
+                                  int pixel_size,
+                                  GLib.Cancellable cancellable)
+        throws GLib.Error {
+        Folks.SearchView view = new Folks.SearchView(
+            this.individuals,
+            new Folks.SimpleQuery(
+                mailbox.address,
+                new string[] {
+                    Folks.PersonaStore.detail_key(
+                        Folks.PersonaDetail.EMAIL_ADDRESSES
                     )
-                );
-
-                try {
-                    // We want to just pass load_cancelled to send_async
-                    // here, but per Bug 778720 this is causing some
-                    // crashy race in libsoup's cache implementation, so
-                    // for now just let the load go through and manually
-                    // check to see if the load has been cancelled before
-                    // setting the avatar
-                    InputStream data = yield session.send_async(
-                        message,
-                        null // should be 'load_cancelled'
-                    );
-                    if (message.status_code == 200 &&
-                        data != null &&
-                        !load_cancelled.is_cancelled()) {
-                        this.avatar = yield new Gdk.Pixbuf.from_stream_at_scale_async(
-                            data, pixel_size, pixel_size, true, load_cancelled
-                        );
-                    }
-                } catch (Error err) {
-                    workaround_err = err;
                 }
-            }
+            )
+        );
 
-            this.lock.blind_notify();
+        yield view.prepare();
 
-            if (workaround_err != null) {
-                throw workaround_err;
-            }
+        Folks.Individual? match = null;
+        if (!view.individuals.is_empty) {
+            match = view.individuals.first();
+        }
+        try {
+            yield view.unprepare();
+        } catch (GLib.Error err) {
+            warning("Error unpreparing Folks search: %s", err.message);
         }
 
-    }
-
-
-    private Configuration config;
-
-    private Soup.Session session;
-    private Soup.Cache cache;
-    private Gee.Map<string,AvatarLoader> loaders =
-        new Gee.HashMap<string,AvatarLoader>();
-
-
-    public AvatarStore(Configuration config, GLib.File cache_root) {
-        this.config = config;
-
-        File avatar_cache_dir = cache_root.get_child("avatars");
-        this.cache = new Soup.Cache(
-            avatar_cache_dir.get_path(),
-            Soup.CacheType.SINGLE_USER
-        );
-        this.cache.load();
-        this.cache.set_max_size(16 * 1024 * 1024); // 16MB
-        this.session = new Soup.Session();
-        this.session.add_feature(this.cache);
-    }
-
-    public void close() {
-        this.cache.flush();
-        this.cache.dump();
-    }
-
-    public async Gdk.Pixbuf? load(Geary.RFC822.MailboxAddress address,
-                                    int pixel_size,
-                                    Cancellable load_cancelled)
-        throws Error {
-        string key = address.to_string();
-        AvatarLoader loader = this.loaders.get(key);
-        if (loader == null) {
-            // Haven't started loading the avatar, so do it now
-            loader = new AvatarLoader(
-                address, this.config.avatar_url, pixel_size
+        Gdk.Pixbuf? pixbuf = null;
+        if (match != null && match.avatar != null) {
+            GLib.InputStream data = yield match.avatar.load_async(
+                pixel_size, cancellable
+            );
+            pixbuf = yield new Gdk.Pixbuf.from_stream_at_scale_async(
+                data, pixel_size, pixel_size, true, cancellable
             );
-            this.loaders.set(key, loader);
-            yield loader.load(this.session, load_cancelled);
-        } else {
-            // Load has already started, so wait for it to finish
-            yield loader.lock.wait_async();
         }
-        return loader.avatar;
+        return pixbuf;
     }
 
 }
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 8125f14f..a4c393d8 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -259,10 +259,16 @@ public class GearyController : Geary.BaseObject {
             error("Error loading web resources: %s", err.message);
         }
 
-        this.avatar_store = new Application.AvatarStore(
-            this.application.config,
-            this.application.get_user_cache_directory()
-        );
+        Folks.IndividualAggregator individuals =
+            Folks.IndividualAggregator.dup();
+        if (!individuals.is_prepared) {
+            try {
+                yield individuals.prepare();
+            } catch (GLib.Error err) {
+                error("Error preparing Folks: %s", err.message);
+            }
+        }
+        this.avatar_store = new Application.AvatarStore(individuals);
 
         // Create the main window (must be done after creating actions.)
         main_window = new MainWindow(this.application);


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