[gnome-contacts/nielsdg/contact-nullable-store: 8/8] Contact: allow the contacts store to be null




commit 775ce8ff3327af33412b2ef3b7137dd284126fae
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Tue Sep 6 12:44:16 2022 +0200

    Contact: allow the contacts store to be null
    
    We need to know about the primary store in a `Contacts.Contact` for 2
    reasons:
    
    1. To find out which persona is the "primary" persona (if there is one)
    2. To save any new properties (which are not linked to a persona) in the
       primary store
    
    However, in some cases we do not have a special need for these 2
    properties: in unit tests, and when we want to port our
    (de-)serialization code to work with an array of `Contacts.Contact`
    objects (especially with the importer being in a separate process).
    
    In those cases, it should be allowed to pass `null` instead.

 src/core/contacts-contact.vala | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
---
diff --git a/src/core/contacts-contact.vala b/src/core/contacts-contact.vala
index 889284fd..7a28a3c9 100644
--- a/src/core/contacts-contact.vala
+++ b/src/core/contacts-contact.vala
@@ -33,7 +33,7 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
   /** The underlying individual, if any */
   public unowned Individual? individual { get; construct set; default = null; }
 
-  public unowned Store contacts_store { get; construct set; }
+  public unowned Store? contacts_store { get; construct set; }
 
   /** Similar to fetch_display_name(), but never returns null */
   public string display_name {
@@ -53,12 +53,12 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
   }
 
   /** Creates a Contact that acts as a wrapper around an Individual */
-  public Contact.for_individual (Individual individual, Store contacts_store) {
+  public Contact.for_individual (Individual individual, Store? contacts_store) {
     Object (individual: individual, contacts_store: contacts_store);
   }
 
   /** Creates a new empty contact */
-  public Contact.for_new (Store contacts_store) {
+  public Contact.for_new (Store? contacts_store) {
     Object (individual: null, contacts_store: contacts_store);
   }
 
@@ -225,15 +225,23 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
 
     // From these chunks, select the one from the primary store. If there's
     // none, just select the first one
-    unowned var primary_store = this.contacts_store.aggregator.primary_store;
-    for (uint i = 0; i < chunks.get_n_items (); i++) {
-      var chunk = (Chunk) chunks.get_item (i);
-      if (chunk.persona != null && chunk.persona.store == primary_store)
-        return chunk;
+    unowned var primary_store = get_primary_store ();
+    if (primary_store != null) {
+      for (uint i = 0; i < chunks.get_n_items (); i++) {
+        var chunk = (Chunk) chunks.get_item (i);
+        if (chunk.persona != null && chunk.persona.store == primary_store)
+          return chunk;
+      }
     }
     return (Chunk?) chunks.get_item (0);
   }
 
+  private unowned PersonaStore? get_primary_store () {
+    if (this.contacts_store == null)
+      return null;
+    return this.contacts_store.aggregator.primary_store;
+  }
+
   public Object? get_item (uint i) {
     if (i > this.chunks.length)
       return null;
@@ -293,7 +301,7 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
     }
     if (new_details.size () != 0) {
       debug ("Creating new persona with %u properties", new_details.size ());
-      unowned var primary_store = this.contacts_store.aggregator.primary_store;
+      unowned var primary_store = get_primary_store ();
       return_if_fail (primary_store != null);
       var persona = yield primary_store.add_persona_from_details (new_details);
       debug ("Successfully created new persona %p", persona);


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