[gnome-boxes] Fix removal of libvirt machines



commit ae088cbc9f8640c2dbd8b06638b9901ef70eff30
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Nov 30 15:38:46 2011 +0200

    Fix removal of libvirt machines
    
    Because of some leaked references, libvirt machines and associated
    periodic updates weren't actually getting removed. Boxes kept asking
    inexistent domains for stats every second and ended-up flooding the
    console with warnings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665198

 src/app.vala             |    6 +++++-
 src/collection.vala      |    6 ++++++
 src/libvirt-machine.vala |   30 ++++++++++++++----------------
 src/machine.vala         |    2 +-
 src/remote-machine.vala  |    4 +++-
 5 files changed, 29 insertions(+), 19 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index 9dfe5d7..4957d58 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -55,6 +55,9 @@ private class Boxes.App: Boxes.UI {
         collection.item_added.connect ((item) => {
             view.add_item (item);
         });
+        collection.item_removed.connect ((item) => {
+            view.remove_item (item);
+        });
 
         setup_sources.begin ();
     }
@@ -93,7 +96,8 @@ private class Boxes.App: Boxes.UI {
 
         connection.domain_removed.connect ((connection, domain) => {
             var machine = domain.get_data<LibvirtMachine> ("machine");
-            view.remove_item (machine);
+            machine.delete (false);
+            collection.remove_item (machine);
         });
 
         connection.domain_added.connect ((connection, domain) => {
diff --git a/src/collection.vala b/src/collection.vala
index 13688fe..a4e328f 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -7,6 +7,7 @@ private abstract class Boxes.CollectionItem: Boxes.UI {
 private class Boxes.Collection: GLib.Object {
     private Boxes.App app;
     public signal void item_added (CollectionItem item);
+    public signal void item_removed (CollectionItem item);
 
     public GenericArray<CollectionItem> items;
 
@@ -22,6 +23,11 @@ private class Boxes.Collection: GLib.Object {
         items.add (item);
         item_added (item);
     }
+
+    public void remove_item (CollectionItem item) {
+        items.remove (item);
+        item_removed (item);
+    }
 }
 
 private class Boxes.Category: GLib.Object {
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index e5e011c..06713a8 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -84,10 +84,6 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         stats = new MachineStat[STATS_SIZE];
     }
 
-    ~LibvirtMachine () {
-        set_stats_enable (false);
-    }
-
     public LibvirtMachine (CollectionSource source, Boxes.App app,
                            GVir.Connection connection, GVir.Domain domain) {
         base (source, app, domain.get_name ());
@@ -215,7 +211,7 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
     }
 
     private uint stats_id;
-    public void set_stats_enable (bool enable) {
+    private void set_stats_enable (bool enable) {
         if (enable) {
             if (stats_id != 0)
                 return;
@@ -317,18 +313,20 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
         return true;
     }
 
-    public override void delete () {
-        try {
-            if (is_running ())
-                domain.stop (0);
-        } catch (GLib.Error err) {
-            // ignore stop error
-        }
+    public override void delete (bool by_user = true) {
+        if (by_user) {
+            try {
+                if (is_running ())
+                    domain.stop (0);
+            } catch (GLib.Error err) {
+                // ignore stop error
+            }
 
-        try {
-            domain.delete (0);
-        } catch (GLib.Error err) {
-            warning (err.message);
+            try {
+                domain.delete (0);
+            } catch (GLib.Error err) {
+                warning (err.message);
+            }
         }
 
         set_stats_enable (false);
diff --git a/src/machine.vala b/src/machine.vala
index 10b353c..63626da 100644
--- a/src/machine.vala
+++ b/src/machine.vala
@@ -222,7 +222,7 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IProperties {
         return Gdk.pixbuf_get_from_surface (surface, 0, 0, width, height);
     }
 
-    public abstract void delete ();
+    public abstract void delete (bool by_user = true);
 
     public override void ui_state_changed () {
         machine_actor.ui_state = ui_state;
diff --git a/src/remote-machine.vala b/src/remote-machine.vala
index 6bc88b3..fef2684 100644
--- a/src/remote-machine.vala
+++ b/src/remote-machine.vala
@@ -82,7 +82,9 @@ private class Boxes.RemoteMachine: Boxes.Machine, Boxes.IProperties {
         return true;
     }
 
-    public override void delete () {
+    public override void delete (bool by_user = true) {
+        return_if_fail (by_user);
+
         source.delete ();
     }
 }



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