[gnome-contacts] Don't reuse single ContactSheet for multiple contacts.



commit 8f43f9136d84a7562a0f2094d44041ed0d6ea8ea
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Mon Mar 5 06:43:27 2018 +0100

    Don't reuse single ContactSheet for multiple contacts.
    
    That way, there exists a clear relation between a ContactSheet and the
    contact it's displaying. It should also be much easier to exploit the
    common structure of a ContactSheet and a ContactEditor to extract shared
    functions.

 data/contacts.gresource.xml       |  1 +
 data/ui/contacts-contact-sheet.ui | 34 +++++++++++++++
 po/POTFILES.in                    |  1 +
 src/contacts-contact-pane.vala    | 90 +++++++++++++--------------------------
 src/contacts-contact-sheet.vala   | 71 ++++++++++++++----------------
 src/contacts-window.vala          |  2 +-
 6 files changed, 100 insertions(+), 99 deletions(-)
---
diff --git a/data/contacts.gresource.xml b/data/contacts.gresource.xml
index d8e1061..a20079e 100644
--- a/data/contacts.gresource.xml
+++ b/data/contacts.gresource.xml
@@ -8,6 +8,7 @@
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-avatar-selector.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-contact-editor.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-contact-pane.ui</file>
+    <file compressed="true" preprocess="xml-stripblanks">ui/contacts-contact-sheet.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-in-app-notification.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-link-suggestion-grid.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/contacts-linked-personas-dialog.ui</file>
diff --git a/data/ui/contacts-contact-sheet.ui b/data/ui/contacts-contact-sheet.ui
new file mode 100644
index 0000000..a65ece5
--- /dev/null
+++ b/data/ui/contacts-contact-sheet.ui
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.22 -->
+  <template class="ContactsContactSheet" parent="GtkGrid">
+    <property name="visible">True</property>
+    <property name="hexpand">True</property>
+    <property name="vexpand">True</property>
+    <property name="orientation">vertical</property>
+    <property name="row_spacing">12</property>
+    <property name="column_spacing">16</property>
+    <property name="margin">36</property>
+    <property name="margin_bottom">24</property>
+    <child>
+      <object class="GtkLabel" id="name_label">
+        <property name="visible">True</property>
+        <property name="selectable">True</property>
+        <property name="hexpand">True</property>
+        <property name="halign">start</property>
+        <property name="valign">center</property>
+        <property name="margin_start">6</property>
+        <property name="xalign">0</property>
+        <property name="ellipsize">end</property>
+        <style>
+          <class name="contacts-contact-sheet"/>
+        </style>
+      </object>
+      <packing>
+        <property name="left_attach">1</property>
+        <property name="top_attach">0</property>
+        <property name="height">3</property>
+      </packing>
+    </child>
+  </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e684c88..699261d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -8,6 +8,7 @@ data/ui/contacts-accounts-list.ui
 data/ui/contacts-avatar-selector.ui
 data/ui/contacts-contact-editor.ui
 data/ui/contacts-contact-pane.ui
+data/ui/contacts-contact-sheet.ui
 data/ui/contacts-link-suggestion-grid.ui
 data/ui/contacts-linked-personas-dialog.ui
 data/ui/contacts-list-pane.ui
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index cf1d8aa..4ea0583 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -33,7 +33,7 @@ public class Contacts.ContactPane : Stack {
 
   private Store store;
 
-  public Contact? contact;
+  public Contact? contact = null;
 
   [GtkChild]
   private Grid none_selected_page;
@@ -75,29 +75,6 @@ public class Contacts.ContactPane : Stack {
   public signal void display_name_changed (string new_display_name);
 
 
-  public void update_sheet () {
-    if (on_edit_mode) {
-      /* this was triggered by some signal, do nothing */
-      return;
-    }
-
-    sheet.clear ();
-
-    if (contact == null)
-      return;
-
-    sheet.update (contact);
-    set_visible_child (this.contact_sheet_page);
-
-    var matches = contact.store.aggregator.get_potential_matches (contact.individual, MatchResult.HIGH);
-    foreach (var ind in matches.keys) {
-      var c = Contact.from_individual (ind);
-      if (c != null && contact.suggest_link_to (c)) {
-       add_suggestion (c);
-      }
-    }
-  }
-
   public void add_suggestion (Contact c) {
     var parent_overlay = this.get_parent () as Overlay;
 
@@ -125,43 +102,31 @@ public class Contacts.ContactPane : Stack {
       });
   }
 
-  public void show_contact (Contact? new_contact, bool show_matches = true) {
-    if (contact == new_contact)
+  public void show_contact (Contact? contact) {
+    if (this.contact == contact)
       return;
 
-    if (suggestion_grid != null) {
-      suggestion_grid.destroy ();
-      suggestion_grid = null;
+    if (this.suggestion_grid != null) {
+      this.suggestion_grid.destroy ();
+      this.suggestion_grid = null;
     }
 
-    if (contact != null) {
-      contact.individual.personas_changed.disconnect (update_sheet);
-      contact.changed.disconnect (update_sheet);
-    }
-
-    contact = new_contact;
-
-    update_sheet ();
-
-    if (contact != null) {
-      contact.changed.connect (update_sheet);
-      contact.individual.personas_changed.connect (update_sheet);
-    }
-
-    if (contact == null)
+    this.contact = contact;
+    if (this.contact != null) {
+      show_contact_sheet ();
+    } else {
+      remove_contact_sheet ();
       set_visible_child (this.none_selected_page);
+    }
   }
 
   public ContactPane (Window parent_window, Store contacts_store) {
     this.parent_window = parent_window;
     this.store = contacts_store;
-       this.store.quiescent.connect (update_sheet);
 
     this.edit_contact_actions = new SimpleActionGroup ();
     this.edit_contact_actions.add_action_entries (action_entries, this);
 
-    create_contact_sheet ();
-
     this.suggestion_grid = null;
 
     /* edit mode widgetry, third page */
@@ -193,15 +158,23 @@ public class Contacts.ContactPane : Stack {
                                BindingFlags.INVERT_BOOLEAN);
   }
 
-  private void create_contact_sheet () {
-    this.sheet = new ContactSheet ();
-    this.sheet.hexpand = true;
-    this.sheet.vexpand = true;
-    this.sheet.margin = 36;
-    this.sheet.set_margin_bottom (24);
-    this.contact_sheet_container.add (this.sheet);
+  private void show_contact_sheet () {
+    assert (this.contact != null);
 
+    remove_contact_sheet();
+    this.sheet = new ContactSheet (this.contact, this.store);
+    this.contact_sheet_container.add (this.sheet);
     this.sheet.set_focus_vadjustment (this.contact_sheet_page.get_vadjustment ());
+    set_visible_child (this.contact_sheet_page);
+  }
+
+  private void remove_contact_sheet () {
+    if (this.sheet == null)
+      return;
+
+    this.contact_sheet_container.remove (this.sheet);
+    this.sheet.destroy();
+    this.sheet = null;
   }
 
   void on_add_detail (GLib.SimpleAction action, GLib.Variant? parameter) {
@@ -243,7 +216,7 @@ public class Contacts.ContactPane : Stack {
 
       on_edit_mode = true;
 
-      sheet.clear ();
+      remove_contact_sheet ();
 
       if (suggestion_grid != null) {
        suggestion_grid.destroy ();
@@ -265,7 +238,6 @@ public class Contacts.ContactPane : Stack {
                                                  Contact.set_persona_property.end (result);
                                                } catch (Error e2) {
                                                  show_message (e2.message);
-                                                 update_sheet ();
                                                }
                                              });
        }
@@ -302,9 +274,7 @@ public class Contacts.ContactPane : Stack {
       editor.clear ();
 
       if (contact != null) {
-        sheet.clear ();
-        sheet.update (contact);
-        set_visible_child (this.contact_sheet_page);
+        show_contact_sheet ();
       } else {
         set_visible_child (this.none_selected_page);
       }
@@ -314,7 +284,7 @@ public class Contacts.ContactPane : Stack {
   public void new_contact () {
     on_edit_mode = true;
 
-    sheet.clear ();
+    remove_contact_sheet ();
 
     if (suggestion_grid != null) {
       suggestion_grid.destroy ();
diff --git a/src/contacts-contact-sheet.vala b/src/contacts-contact-sheet.vala
index ce9ce7c..3324e5b 100644
--- a/src/contacts-contact-sheet.vala
+++ b/src/contacts-contact-sheet.vala
@@ -1,4 +1,3 @@
-/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
 /*
  * Copyright (C) 2011 Alexander Larsson <alexl redhat com>
  *
@@ -20,8 +19,28 @@ using Gtk;
 using Folks;
 using Gee;
 
+/**
+ * The contact sheet displays the actual information of a contact.
+ *
+ * (Note: to edit a contact, use the {@link ContactEditor} instead.
+ */
+[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-contact-sheet.ui")]
 public class Contacts.ContactSheet : Grid {
 
+  private Contact? contact;
+
+  [GtkChild]
+  private Label name_label;
+
+  public ContactSheet (Contact contact, Store store) {
+      this.contact = contact;
+      this.contact.changed.connect (update);
+      this.contact.individual.personas_changed.connect (update);
+      store.quiescent.connect (update);
+
+      update ();
+  }
+
   Button add_row_with_button (ref int row, string label_value, string value) {
     var type_label = new Label (label_value);
     type_label.xalign = 1.0f;
@@ -88,38 +107,22 @@ public class Contacts.ContactSheet : Grid {
     row++;
   }
 
-  public ContactSheet () {
-    set_row_spacing (12);
-    set_column_spacing (16);
-    set_orientation (Orientation.VERTICAL);
-    get_style_context ().add_class ("contacts-contact-sheet");
-  }
-
-  public void update (Contact c) {
-    var image_frame = new Avatar (PROFILE_SIZE, c);
+  private void update () {
+    var image_frame = new Avatar (PROFILE_SIZE, this.contact);
     image_frame.set_vexpand (false);
     image_frame.set_valign (Align.START);
     attach (image_frame,  0, 0, 1, 3);
 
-    var name_label = new Label (null);
-    name_label.set_hexpand (true);
-    name_label.set_halign (Align.START);
-    name_label.set_valign (Align.CENTER);
-    name_label.margin_start = 6;
-    name_label.set_ellipsize (Pango.EllipsizeMode.END);
-    name_label.xalign = 0.0f;
-    name_label.set_selectable (true);
-
-    c.keep_widget_uptodate (name_label, (w) => {
-        (w as Label).set_markup (Markup.printf_escaped ("<span font='16'>%s</span>", 
c.individual.display_name));
+    this.contact.keep_widget_uptodate (this.name_label, (w) => {
+        this.name_label.set_markup (Markup.printf_escaped ("<span font='16'>%s</span>",
+                                                           this.contact.individual.display_name));
       });
-    attach (name_label,  1, 0, 1, 3);
 
     int i = 3;
     int last_store_position = 0;
     bool is_first_persona = true;
 
-    var personas = c.get_personas_for_display ();
+    var personas = this.contact.get_personas_for_display ();
     /* Cause personas are sorted properly I can do this */
     foreach (var p in personas) {
       if (!is_first_persona) {
@@ -139,7 +142,7 @@ public class Contacts.ContactSheet : Grid {
        foreach (var email in emails) {
          var button = add_row_with_button (ref i, TypeSet.email.format_type (email), email.value);
          button.clicked.connect (() => {
-             Utils.compose_mail ("%s <%s>".printf(c.individual.display_name, email.value));
+             Utils.compose_mail ("%s <%s>".printf(this.contact.individual.display_name, email.value));
            });
        }
       }
@@ -149,10 +152,10 @@ public class Contacts.ContactSheet : Grid {
        var phones = Contact.sort_fields<PhoneFieldDetails>(phone_details.phone_numbers);
        foreach (var phone in phones) {
 #if HAVE_TELEPATHY
-         if (c.store != null && c.store.caller_account != null) {
+         if (this.contact.store != null && this.contact.store.caller_account != null) {
            var button = add_row_with_button (ref i, TypeSet.phone.format_type (phone), phone.value);
            button.clicked.connect (() => {
-            Utils.start_call (phone.value, c.store.caller_account);
+            Utils.start_call (phone.value, this.contact.store.caller_account);
              });
          } else {
            add_row_with_label (ref i, TypeSet.phone.format_type (phone), phone.value);
@@ -171,14 +174,12 @@ public class Contacts.ContactSheet : Grid {
            if (p is Tpf.Persona) {
              var button = add_row_with_button (ref i, ImService.get_display_name (protocol), id.value);
              button.clicked.connect (() => {
-                 var im_persona = c.find_im_persona (protocol, id.value);
+                 var im_persona = this.contact.find_im_persona (protocol, id.value);
                  if (im_persona != null) {
                    var type = im_persona.presence_type;
-                   if (type != PresenceType.UNSET &&
-                       type != PresenceType.ERROR &&
-                       type != PresenceType.OFFLINE &&
-                       type != PresenceType.UNKNOWN) {
-                     Utils.start_chat (c, protocol, id.value);
+                   if (type != PresenceType.UNSET && type != PresenceType.ERROR &&
+                type != PresenceType.OFFLINE && type != PresenceType.UNKNOWN) {
+                     Utils.start_chat (this.contact, protocol, id.value);
                    }
                  }
                });
@@ -233,10 +234,4 @@ public class Contacts.ContactSheet : Grid {
 
     show_all ();
   }
-
-  public void clear () {
-    foreach (var w in get_children ()) {
-      w.destroy ();
-    }
-  }
 }
diff --git a/src/contacts-window.vala b/src/contacts-window.vala
index a626a07..a37023f 100644
--- a/src/contacts-window.vala
+++ b/src/contacts-window.vala
@@ -276,7 +276,7 @@ public class Contacts.Window : Gtk.ApplicationWindow {
     if (this.contact_pane.on_edit_mode)
       stop_editing ();
 
-    this.contact_pane.show_contact (c, false);
+    this.contact_pane.show_contact (c);
     if (list_pane != null)
       list_pane.select_contact (c);
 


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