[gnome-contacts] Added undo support to link operation.



commit 4f27032c10dcb60e837c2b41b7b0968319b90d55
Author: Erick PÃrez Castellanos <erick red gmail com>
Date:   Fri Feb 15 00:31:25 2013 -0500

    Added undo support to link operation.
    
    Unlink operation, which is executed from contact-editor, doesn't
    have undo support.

 src/contacts-app.vala     |    6 +++++-
 src/contacts-linking.vala |   39 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 2 deletions(-)
---
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index 71232c4..2189a6d 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -471,8 +471,9 @@ public class Contacts.App : Gtk.Application {
     show_contact (null);
     select_button.set_active (false);
 
+    LinkOperation2 operation = null;
     link_contacts_list.begin (contact_list, (obj, result) => {
-       link_contacts_list.end (result);
+       operation = link_contacts_list.end (result);
       });
 
     var notification = new Gd.Notification ();
@@ -492,6 +493,9 @@ public class Contacts.App : Gtk.Application {
     /* signal handlers */
     b.clicked.connect ( () => {
        /* here, we will unlink the thing in question */
+       operation.undo.begin ();
+
+       notification.dismiss ();
       });
   }
 
diff --git a/src/contacts-linking.vala b/src/contacts-linking.vala
index 4f210a2..2a3f44e 100644
--- a/src/contacts-linking.vala
+++ b/src/contacts-linking.vala
@@ -715,6 +715,41 @@ namespace Contacts {
   }
 
   public class LinkOperation2 : Object {
+    /* One Set<Persona> per individual linked, with the intention
+     * of restore the old perosonas set on undo operation */
+    LinkedList< HashSet<Persona> > old_personas_distribution;
+
+    public LinkOperation2 () {
+      old_personas_distribution = new  LinkedList< HashSet<Persona> > ();
+    }
+
+    public void add_persona_set (Set<Persona> persona_set) {
+      if (persona_set.size > 0) {
+       var s = new HashSet<Persona> ();
+       foreach (var p in persona_set) {
+         s.add (p);
+       }
+       old_personas_distribution.add (s);
+      }
+    }
+
+    public async void undo () {
+      Individual ind = null;
+      if (old_personas_distribution.size > 0) {
+       var ps = old_personas_distribution.first ();
+       foreach (var p in ps) {
+         ind = p.individual;
+         break;
+       }
+      }
+      if (ind != null) {
+       yield App.app.contacts_store.aggregator.unlink_individual (ind);
+      }
+
+      foreach (var ps in old_personas_distribution) {
+       yield App.app.contacts_store.aggregator.link_personas (ps);
+      }
+    }
   }
 
   public async LinkOperation2 link_contacts_list (LinkedList<Contact> contact_list) {
@@ -722,7 +757,9 @@ namespace Contacts {
 
     var all_personas = new HashSet<Persona> ();
     foreach (var c in contact_list) {
-      all_personas.add_all (c.individual.personas);
+      var ps = c.individual.personas;
+      all_personas.add_all (ps);
+      operation.add_persona_set (ps);
     }
 
     yield App.app.contacts_store.aggregator.link_personas (all_personas);


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