diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2012-09-01 17:30:01 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2013-01-30 16:49:52 +0100 |
commit | e2aab7e53aabbe5793aa14bcc66de2e689172b2a (patch) | |
tree | 14b5f84cf0ac3df8407a3b9b71305413564015c5 | |
parent | b84c9730bf2fda451f2d60b3294f76149027bb5a (diff) |
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
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/app.vala | 84 | ||||
-rw-r--r-- | src/libvirt-broker.vala | 89 | ||||
-rw-r--r-- | 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 c0ffaa8..6c9f9e5 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; @@ -338,89 +339,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 { @@ -436,6 +364,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 40e56b7..4805109 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; } |