[gnome-contacts/nielsdg/core-should-stay-core: 2/11] contact: Don't keep a reference to the store




commit 245d8a48c51b640b292719e600610b54272cad41
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Wed Sep 7 08:55:52 2022 +0200

    contact: Don't keep a reference to the store
    
    First, we're only using the `Contacts.Store` to get to the aggregator's
    primary store, which we can just get from the
    `Folks.PersonaStore.is_primary_store` property itself.
    
    Second, this means we can make the constructor for a `Contact` a bit
    simpler, as we don't have to actually pass on a `Contacts.Store`
    reference.
    
    Third, this parameterizes `apply_changes()` so that it can choose which
    PersonaStore to save new details into, which is a step towards being
    able to configure the address book when you're editing/creating a
    contact.

 src/contacts-contact-pane.vala |  9 +++++----
 src/core/contacts-contact.vala | 28 ++++++++--------------------
 2 files changed, 13 insertions(+), 24 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 2a16e226..486aa30f 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -84,8 +84,9 @@ public class Contacts.ContactPane : Adw.Bin {
       return;
     }
 
-    if (this.contact == null || this.contact.individual != individual)
-      this.contact = new Contact.for_individual (individual, this.store);
+    if (this.contact == null || this.contact.individual != individual) {
+      this.contact = new Contact.for_individual (individual);
+    }
     show_contact_sheet (this.contact);
   }
 
@@ -175,7 +176,7 @@ public class Contacts.ContactPane : Adw.Bin {
     }
 
     try {
-      yield contact.apply_changes ();
+      yield contact.apply_changes (this.store.aggregator.primary_store);
     } catch (Error err) {
       warning ("Couldn't save changes: %s", err.message);
       // XXX do something better here
@@ -195,7 +196,7 @@ public class Contacts.ContactPane : Adw.Bin {
   }
 
   public void new_contact () {
-    this.contact = new Contact.for_new (this.store);
+    this.contact = new Contact.empty ();
     if (this.on_edit_mode)
       return;
 
diff --git a/src/core/contacts-contact.vala b/src/core/contacts-contact.vala
index 2db325d1..071f6f42 100644
--- a/src/core/contacts-contact.vala
+++ b/src/core/contacts-contact.vala
@@ -33,8 +33,6 @@ 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; }
-
   /** Similar to fetch_display_name(), but never returns null */
   public string display_name {
     owned get { return fetch_display_name () ?? _("Unnamed Person"); }
@@ -53,13 +51,13 @@ 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) {
-    Object (individual: individual, contacts_store: contacts_store);
+  public Contact.for_individual (Individual individual) {
+    Object (individual: individual);
   }
 
   /** Creates a new empty contact */
-  public Contact.for_new (Store? contacts_store) {
-    Object (individual: null, contacts_store: contacts_store);
+  public Contact.empty () {
+    Object (individual: null);
   }
 
   private void on_individual_personas_changed (Individual individual,
@@ -220,8 +218,6 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
    */
   public unowned Chunk? get_most_relevant_chunk (string property_name,
                                                  bool allow_empty = false) {
-    unowned var primary_store = get_primary_store ();
-
     unowned Chunk? result = null;
     for (uint i = 0; i < this.chunks.length; i++) {
       unowned var chunk = this.chunks[i];
@@ -233,8 +229,7 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
         continue;
 
       // If we find a chunk from the primary persona, return immediately
-      if (primary_store != null &&
-          chunk.persona != null && chunk.persona.store == primary_store)
+      if (chunk.persona != null && chunk.persona.store.is_primary_store)
         return chunk;
 
       // Return the first occurrence later if we don't find a primary chunk
@@ -245,12 +240,6 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
     return result;
   }
 
-  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;
@@ -268,8 +257,9 @@ public class Contacts.Contact : GLib.Object, GLib.ListModel {
   /**
    * Applies any pending changes to all chunks. This can mean either a new
    * persona is made, or it is saved in the chunk's referenced persona.
+   * When a new persona is made, it will be added to @store.
    */
-  public async void apply_changes () throws GLib.Error {
+  public async void apply_changes (PersonaStore store) throws GLib.Error {
     // For those that were a persona: save the properties using the API
     for (uint i = 0; i < this.chunks.length; i++) {
       unowned var chunk = this.chunks[i];
@@ -310,9 +300,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 = get_primary_store ();
-      return_if_fail (primary_store != null);
-      var persona = yield primary_store.add_persona_from_details (new_details);
+      var persona = yield store.add_persona_from_details (new_details);
       debug ("Successfully created new persona %p", persona);
       // FIXME: should we set the persona for these chunks?
     }


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