[gnome-boxes] libvirt: Move most libvirt code from App to a Broker



commit 6d3fd620701f1677b6dfd38ce8a15dbcf88094ba
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Sat Sep 1 17:30:01 2012 +0200

    libvirt: Move most libvirt code from App to a Broker
    
    This moves most of the libvirt specific code out of App to
    a new LibvirtBroker class, making App more generic.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=681747

 src/Makefile.am         |    1 +
 src/app.vala            |   84 ++++----------------------------------------
 src/libvirt-broker.vala |   89 +++++++++++++++++++++++++++++++++++++++++++++++
 src/vm-creator.vala     |    2 +-
 4 files changed, 99 insertions(+), 77 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 42bc73c..25373eb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -87,6 +87,7 @@ gnome_boxes_SOURCES =				\
 	i-properties-provider.vala		\
 	installer-media.vala 			\
 	iso-extractor.vala			\
+	libvirt-broker.vala			\
 	libvirt-machine.vala			\
 	machine.vala				\
 	main.vala				\
diff --git a/src/app.vala b/src/app.vala
index c1c8c90..70f36f4 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -82,9 +82,8 @@ private class Boxes.App: Boxes.UI {
     public CollectionView view;
 
     private HashTable<string,Broker> brokers;
-    private HashTable<string,GVir.Connection> connections;
     private HashTable<string,CollectionSource> sources;
-    public GVir.Connection default_connection { get { return connections.get ("QEMU Session"); } }
+    public GVir.Connection default_connection { owned get { return LibvirtBroker.get_default ().get_connection ("QEMU Session"); } }
     public CollectionSource default_source { get { return sources.get ("QEMU Session"); } }
 
     private uint configure_id;
@@ -96,7 +95,6 @@ private class Boxes.App: Boxes.UI {
         app = this;
         application = new Boxes.Application ();
         settings = new GLib.Settings ("org.gnome.boxes");
-        connections = new HashTable<string, GVir.Connection> (str_hash, str_equal);
         sources = new HashTable<string,CollectionSource> (str_hash, str_equal);
         brokers = new HashTable<string,Broker> (str_hash, str_equal);
         filter = new Boxes.CollectionFilter ();
@@ -173,6 +171,9 @@ private class Boxes.App: Boxes.UI {
         collection.item_removed.connect ((item) => {
             view.remove_item (item);
         });
+
+        brokers.insert ("libvirt", LibvirtBroker.get_default ());
+
         setup_sources.begin ((obj, result) => {
             setup_sources.end (result);
             is_ready = true;
@@ -350,89 +351,16 @@ private class Boxes.App: Boxes.UI {
         return machine;
     }
 
-    // New == Added after Boxes launch
-    private void try_add_new_domain (CollectionSource source, GVir.Connection connection, GVir.Domain domain) {
-        try {
-            add_domain (source, connection, domain);
-        } catch (GLib.Error error) {
-            warning ("Failed to create source '%s': %s", source.name, error.message);
-        }
-    }
-
-    // Existing == Existed before Boxes was launched
-    private void try_add_existing_domain (CollectionSource source, GVir.Connection connection, GVir.Domain domain) {
-        try {
-            var machine = add_domain (source, connection, domain);
-            var config = machine.domain_config;
-
-            if (VMConfigurator.is_install_config (config) || VMConfigurator.is_live_config (config)) {
-                debug ("Continuing installation/live session for '%s', ..", machine.name);
-                new VMCreator.for_install_completion (machine); // This instance will take care of its own lifecycle
-            }
-        } catch (GLib.Error error) {
-            warning ("Failed to create source '%s': %s", source.name, error.message);
-        }
-    }
-
     public void delete_machine (Machine machine, bool by_user = true) {
         machine.delete (by_user);         // Will also delete associated storage volume if by_user is 'true'
         collection.remove_item (machine);
     }
 
-    private async void setup_libvirt (CollectionSource source) {
-        if (connections.lookup (source.name) != null)
-            return;
-
-        var connection = new GVir.Connection (source.uri);
-
-        try {
-            yield connection.open_async (null);
-            yield connection.fetch_domains_async (null);
-            yield connection.fetch_storage_pools_async (null);
-            var pool = Boxes.get_storage_pool (connection);
-            if (pool != null) {
-                if (pool.get_info ().state == GVir.StoragePoolState.INACTIVE)
-                    yield pool.start_async (0, null);
-                // If default storage pool exists, we should refresh it already
-                yield pool.refresh_async (null);
-            }
-        } catch (GLib.Error error) {
-            warning (error.message);
-        }
-
-        connections.insert (source.name, connection);
-        sources.insert (source.name, source);
-        if (source.name == "QEMU Session") {
-            notify_property ("default-connection");
-            notify_property ("default-source");
-        }
-
-        foreach (var domain in connection.get_domains ())
-            try_add_existing_domain (source, connection, domain);
-
-        connection.domain_removed.connect ((connection, domain) => {
-            var machine = domain.get_data<LibvirtMachine> ("machine");
-            if (machine == null)
-                return; // Looks like we removed the domain ourselves. Nothing to do then..
-
-            delete_machine (machine, false);
-        });
-
-        connection.domain_added.connect ((connection, domain) => {
-            debug ("New domain '%s'", domain.get_name ());
-            try_add_new_domain (source, connection, domain);
-        });
-    }
-
     public async void add_collection_source (CollectionSource source) {
         if (!source.enabled)
             return;
 
         switch (source.source_type) {
-        case "libvirt":
-            yield setup_libvirt (source);
-            break;
-
         case "vnc":
         case "spice":
             try {
@@ -448,6 +376,10 @@ private class Boxes.App: Boxes.UI {
             if (broker != null) {
                 yield broker.add_source (source);
                 sources.insert (source.name, source);
+                if (source.name == "QEMU Session") {
+                    notify_property ("default-connection");
+                    notify_property ("default-source");
+                }
             } else {
                 warning ("Unsupported source type %s", source.source_type);
             }
diff --git a/src/libvirt-broker.vala b/src/libvirt-broker.vala
new file mode 100644
index 0000000..3c41d69
--- /dev/null
+++ b/src/libvirt-broker.vala
@@ -0,0 +1,89 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+using GVir;
+using Gtk;
+
+private class Boxes.LibvirtBroker : Boxes.Broker {
+    private static LibvirtBroker broker;
+    private HashTable<string,GVir.Connection> connections;
+
+    public static LibvirtBroker get_default () {
+        if (broker == null)
+            broker = new LibvirtBroker ();
+
+        return broker;
+    }
+
+    private LibvirtBroker () {
+        connections = new HashTable<string, GVir.Connection> (str_hash, str_equal);
+    }
+
+    public GVir.Connection get_connection (string name) {
+        return_if_fail (broker != null);
+        return broker.connections.get (name);
+    }
+
+    // New == Added after Boxes launch
+    private void try_add_new_domain (CollectionSource source, GVir.Connection connection, GVir.Domain domain) {
+        try {
+            App.app.add_domain (source, connection, domain);
+        } catch (GLib.Error error) {
+            warning ("Failed to create source '%s': %s", source.name, error.message);
+        }
+    }
+
+    // Existing == Existed before Boxes was launched
+    private void try_add_existing_domain (CollectionSource source, GVir.Connection connection, GVir.Domain domain) {
+        try {
+            var machine = App.app.add_domain (source, connection, domain);
+            var config = machine.domain_config;
+
+            if (VMConfigurator.is_install_config (config) || VMConfigurator.is_live_config (config)) {
+                debug ("Continuing installation/live session for '%s', ..", machine.name);
+                new VMCreator.for_install_completion (machine); // This instance will take care of its own lifecycle
+            }
+        } catch (GLib.Error error) {
+            warning ("Failed to create source '%s': %s", source.name, error.message);
+        }
+    }
+
+    public override async void add_source (CollectionSource source) {
+        if (connections.lookup (source.name) != null)
+            return;
+
+        var connection = new GVir.Connection (source.uri);
+
+        try {
+            yield connection.open_async (null);
+            yield connection.fetch_domains_async (null);
+            yield connection.fetch_storage_pools_async (null);
+            var pool = Boxes.get_storage_pool (connection);
+            if (pool != null) {
+                if (pool.get_info ().state == GVir.StoragePoolState.INACTIVE)
+                    yield pool.start_async (0, null);
+                // If default storage pool exists, we should refresh it already
+                yield pool.refresh_async (null);
+            }
+        } catch (GLib.Error error) {
+            warning (error.message);
+        }
+
+        connections.insert (source.name, connection);
+
+        foreach (var domain in connection.get_domains ())
+            try_add_existing_domain (source, connection, domain);
+
+        connection.domain_removed.connect ((connection, domain) => {
+            var machine = domain.get_data<LibvirtMachine> ("machine");
+            if (machine == null)
+                return; // Looks like we removed the domain ourselves. Nothing to do then..
+
+            App.app.delete_machine (machine, false);
+        });
+
+        connection.domain_added.connect ((connection, domain) => {
+            debug ("New domain '%s'", domain.get_name ());
+            try_add_new_domain (source, connection, domain);
+        });
+    }
+}
+
diff --git a/src/vm-creator.vala b/src/vm-creator.vala
index a16bd2e..cf696d3 100644
--- a/src/vm-creator.vala
+++ b/src/vm-creator.vala
@@ -10,7 +10,7 @@ private class Boxes.VMCreator {
 
     public InstallerMedia? install_media { get; private set; }
 
-    private Connection? connection { get { return App.app.default_connection; } }
+    private Connection? connection { owned get { return App.app.default_connection; } }
     private ulong state_changed_id;
 
     private uint num_reboots { get; private set; }


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