[geary/wip/135-contact-popovers: 12/13] Lookup and use Folks individuals in client contact classes



commit d2bbe568a91d2883159693cc87261e4daee4c788
Author: Michael Gratton <mike vee net>
Date:   Fri Mar 15 09:00:52 2019 +1100

    Lookup and use Folks individuals in client contact classes

 .../application/application-contact-store.vala     | 49 ++++++++++++++++++++--
 src/client/application/application-contact.vala    | 20 +++++++--
 src/client/application/geary-controller.vala       | 12 +++---
 3 files changed, 68 insertions(+), 13 deletions(-)
---
diff --git a/src/client/application/application-contact-store.vala 
b/src/client/application/application-contact-store.vala
index c6df7b76..6ac4eb8d 100644
--- a/src/client/application/application-contact-store.vala
+++ b/src/client/application/application-contact-store.vala
@@ -17,10 +17,14 @@ public class Application.ContactStore {
     /** The account this store aggregates data for. */
     public Geary.Account account { get; private set; }
 
+    private Folks.IndividualAggregator individuals;
+
 
     /** Constructs a new contact store for an account. */
-    public ContactStore(Geary.Account account) {
+    public ContactStore(Geary.Account account,
+                        Folks.IndividualAggregator individuals) {
         this.account = account;
+        this.individuals = individuals;
     }
 
     /**
@@ -31,10 +35,49 @@ public class Application.ContactStore {
      * returned, so if no matching contact already exists a new,
      * non-persistent contact will be returned.
      */
-    public Contact get(Geary.RFC822.MailboxAddress mailbox) {
+    public async Contact load(Geary.RFC822.MailboxAddress mailbox,
+                              GLib.Cancellable cancellable)
+        throws GLib.Error {
+        Folks.Individual? individual = yield search_match(
+            mailbox.address, cancellable
+        );
         Geary.Contact? contact =
             this.account.get_contact_store().get_by_rfc822(mailbox);
-        return new Contact(this, contact, mailbox);
+        return new Contact(this, individual, contact, mailbox);
+    }
+
+    private async Folks.Individual? search_match(string address,
+                                                 GLib.Cancellable cancellable)
+        throws GLib.Error {
+        if (cancellable.is_cancelled()) {
+            throw new GLib.IOError.CANCELLED("Contact load was cancelled");
+        }
+
+        Folks.SearchView view = new Folks.SearchView(
+            this.individuals,
+            new Folks.SimpleQuery(
+                address,
+                new string[] {
+                    Folks.PersonaStore.detail_key(
+                        Folks.PersonaDetail.EMAIL_ADDRESSES
+                    )
+                }
+            )
+        );
+
+        yield view.prepare();
+
+        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);
+        }
+        return match;
     }
 
 }
diff --git a/src/client/application/application-contact.vala b/src/client/application/application-contact.vala
index a7557419..58f8aa05 100644
--- a/src/client/application/application-contact.vala
+++ b/src/client/application/application-contact.vala
@@ -28,27 +28,39 @@ public class Application.Contact {
     /** Determines if {@link display_name} the same as its email address. */
     public bool display_name_is_email { get; private set; default = false; }
 
-    /** Determines if email from this contact should load remote resources. */
+    /**
+     * Determines if email from this contact should load remote resources.
+     *
+     * Will automatically load resources from contacts in the desktop
+     * database, or if the Engine's contact has been flagged to do so.
+     */
     public bool load_remote_resources {
         get {
             return (
-                this.contact != null &&
-                this.contact.contact_flags.always_load_remote_images()
+                this.individual != null ||
+                (this.contact != null &&
+                 this.contact.always_load_remote_images())
             );
         }
     }
 
     private weak ContactStore store;
+    private Folks.Individual? individual;
     private Geary.Contact? contact;
 
 
     internal Contact(ContactStore store,
+                     Folks.Individual? individual,
                      Geary.Contact? contact,
                      Geary.RFC822.MailboxAddress source) {
         this.store = store;
+        this.individual = individual;
         this.contact = contact;
 
-        if (contact != null) {
+        if (individual != null) {
+            this.display_name = individual.display_name;
+            this.display_name_is_trusted = true;
+        } else if (contact != null) {
             this.display_name = contact.real_name;
         } else {
             this.display_name = source.name;
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 92339b04..9335426f 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -140,6 +140,7 @@ public class GearyController : Geary.BaseObject {
     // when closed.
     private GLib.Cancellable? open_cancellable = null;
 
+    private Folks.IndividualAggregator? folks = null;
     private Geary.Folder? current_folder = null;
     private Cancellable cancellable_folder = new Cancellable();
     private Cancellable cancellable_search = new Cancellable();
@@ -267,16 +268,15 @@ public class GearyController : Geary.BaseObject {
             error("Error loading web resources: %s", err.message);
         }
 
-        Folks.IndividualAggregator individuals =
-            Folks.IndividualAggregator.dup();
-        if (!individuals.is_prepared) {
+        this.folks = Folks.IndividualAggregator.dup();
+        if (!this.folks.is_prepared) {
             try {
-                yield individuals.prepare();
+                yield this.folks.prepare();
             } catch (GLib.Error err) {
                 error("Error preparing Folks: %s", err.message);
             }
         }
-        this.avatars = new Application.AvatarStore(individuals);
+        this.avatars = new Application.AvatarStore(this.folks);
 
         // Create the main window (must be done after creating actions.)
         main_window = new MainWindow(this.application);
@@ -929,7 +929,7 @@ public class GearyController : Geary.BaseObject {
         AccountContext context = new AccountContext(
             account,
             new Geary.App.EmailStore(account),
-            new Application.ContactStore(account)
+            new Application.ContactStore(account, this.folks)
         );
 
         // XXX Need to set this early since


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