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



commit b0ebea641b4ee3d3b6cc51a62cfde2da70cfc96a
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     | 51 ++++++++++++++++++++--
 src/client/application/application-contact.vala    | 28 ++++++++----
 src/client/application/geary-controller.vala       | 12 ++---
 3 files changed, 73 insertions(+), 18 deletions(-)
---
diff --git a/src/client/application/application-contact-store.vala 
b/src/client/application/application-contact-store.vala
index c6df7b76..44753ea3 100644
--- a/src/client/application/application-contact-store.vala
+++ b/src/client/application/application-contact-store.vala
@@ -11,16 +11,20 @@
  * This class aggregates data from for both the Engine and Folks,
  * allowing contacts for a specific account to be queried.
  */
-public class Application.ContactStore {
+public class Application.ContactStore : Geary.BaseObject {
 
 
     /** 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 c325a062..516a8234 100644
--- a/src/client/application/application-contact.vala
+++ b/src/client/application/application-contact.vala
@@ -13,39 +13,51 @@
  * queried. Contacts are obtained from the {@link ContactStore} for an
  * account.
  */
-public class Application.Contact {
+public class Application.Contact : Geary.BaseObject {
 
 
     /** The human-readable name of the contact. */
     public string display_name { get; private set; }
 
-    /** Determines if {@link display_name} is trusted by the user. */
-    public bool display_name_is_trusted { get; private set; default = false; }
-
     /** 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 this contact was loaded from Folks. */
+    public bool is_desktop_contact { get; private set; default = false; }
+
+    /**
+     * 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.is_desktop_contact = 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 84fd1662..bf57245f 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);
@@ -910,7 +910,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]