[gnome-contacts] Support deleting most contacts



commit 166756a29f77dd63f3a0ccd364a09f8b1d7b4cea
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Jan 24 15:39:30 2012 +0100

    Support deleting most contacts
    
    If *any* persona is removable we remove that. This generally
    means we unlink the rest.

 src/contacts-app.vala          |   62 +++++++++++++++++++++------------------
 src/contacts-contact-pane.vala |   14 +--------
 src/contacts-contact.vala      |   25 ++++++++++++++++
 3 files changed, 60 insertions(+), 41 deletions(-)
---
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index 930735b..1b3c058 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -25,6 +25,7 @@ public class Contacts.App : Gtk.Application {
   public Store contacts_store;
   private ListPane list_pane;
   private ContactPane contacts_pane;
+  private Gtk.Overlay overlay;
 
   private bool window_delete_event (Gdk.EventAny event) {
     // Clear the contacts so any changed information is stored
@@ -297,7 +298,7 @@ public class Contacts.App : Gtk.Application {
 
     /* We put in an overlay overlapping the left and right pane for the
        notifications, so they can show up below the toolbar */
-    var overlay = new Gtk.Overlay ();
+    overlay = new Gtk.Overlay ();
     Gdk.RGBA transparent = { 0, 0, 0, 0 };
     overlay.override_background_color (0, transparent);
     // Need to put something in here for it to work
@@ -311,34 +312,7 @@ public class Contacts.App : Gtk.Application {
 
     contacts_pane = new ContactPane (contacts_store);
     contacts_pane.set_hexpand (true);
-    contacts_pane.will_delete.connect ( (c) => {
-      var notification = new Gtk.Notification ();
-
-      var g = new Grid ();
-      g.set_column_spacing (8);
-      notification.add (g);
-
-
-      string msg = _("Contact deleted: \"%s\"").printf (c.display_name);
-      var b = new Button.from_stock (Stock.UNDO);
-      g.add (new Label (msg));
-      g.add (b);
-
-      bool really_delete = true;
-      notification.show_all ();
-      var id = notification.dismissed.connect ( () => {
-	  if (really_delete)
-	    contacts_store.aggregator.remove_individual (c.individual);
-      });
-      b.clicked.connect ( () => {
-	  really_delete = false;
-	  notification.dismiss ();
-	  c.show ();
-	  list_pane.select_contact (c);
-	  contacts_pane.show_contact (c);
-	});
-      overlay.add_overlay (notification);
-    });
+    contacts_pane.will_delete.connect (delete_contact);
     grid.attach (contacts_pane, 1, 1, 1, 1);
 
 
@@ -369,6 +343,36 @@ public class Contacts.App : Gtk.Application {
     }
   }
 
+  private void delete_contact (Contact contact) {
+    var notification = new Gtk.Notification ();
+
+    var g = new Grid ();
+    g.set_column_spacing (8);
+    notification.add (g);
+
+    string msg = _("Contact deleted: \"%s\"").printf (contact.display_name);
+    var b = new Button.from_stock (Stock.UNDO);
+    g.add (new Label (msg));
+    g.add (b);
+
+    bool really_delete = true;
+    notification.show_all ();
+    var id = notification.dismissed.connect ( () => {
+	if (really_delete)
+	  contact.remove_personas.begin ( () => {
+	      contact.show ();
+	    });
+      });
+    b.clicked.connect ( () => {
+	really_delete = false;
+	notification.dismiss ();
+	contact.show ();
+	list_pane.select_contact (contact);
+	contacts_pane.show_contact (contact);
+      });
+    overlay.add_overlay (notification);
+  }
+
   private static string individual_id = null;
   private static string email_address = null;
   private static const OptionEntry[] options = {
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 8f51a88..4fadc76 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -1805,25 +1805,15 @@ public class Contacts.ContactPane : ScrolledWindow {
     update_personas ();
 
     bool can_remove = false;
-    bool can_remove_all = true;
 
     if (contact != null) {
       contact.personas_changed.connect (personas_changed_cb);
       contact.changed.connect (contact_changed_cb);
 
-      foreach (var p in contact.individual.personas) {
-	if (p.store.can_remove_personas == MaybeBool.TRUE &&
-	    !(p is Tpf.Persona)) {
-	  can_remove = true;
-	} else {
-	  can_remove_all = false;
-	}
-      }
+      can_remove = contact.can_remove_personas ();
     }
 
-    can_remove_all = can_remove && can_remove_all;
-
-    delete_menu_item.set_sensitive (can_remove_all);
+    delete_menu_item.set_sensitive (can_remove);
     link_menu_item.set_sensitive (contact != null);
   }
 
diff --git a/src/contacts-contact.vala b/src/contacts-contact.vala
index 92d355b..ec9e7f3 100644
--- a/src/contacts-contact.vala
+++ b/src/contacts-contact.vala
@@ -1025,6 +1025,31 @@ public class Contacts.Contact : GLib.Object  {
     return uri;
   }
 
+  /* We claim something is "removable" if at least one persona is removable,
+     that will typically unlink the rest. */
+  public bool can_remove_personas () {
+    foreach (var p in individual.personas) {
+      if (p.store.can_remove_personas == MaybeBool.TRUE &&
+	  !(p is Tpf.Persona)) {
+	return true;
+      }
+    }
+    return false;
+  }
+
+  public async void remove_personas () {
+    var personas = new HashSet<Persona> ();
+    foreach (var p in individual.personas) {
+      if (p.store.can_remove_personas == MaybeBool.TRUE &&
+	  !(p is Tpf.Persona)) {
+	personas.add (p);
+      }
+    }
+    foreach (var persona in personas)  {
+      yield persona.store.remove_persona (persona);
+    }
+  }
+
   public async Persona ensure_primary_persona () throws IndividualAggregatorError, ContactError, PropertyError {
     Persona? p = find_primary_persona ();
     if (p != null)



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