[gnome-boxes] Introducing MediaManager class



commit 8e3ca3269952ca13dc6c1c6fdbcb7b1a7bd4485f
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Sun Feb 5 17:28:27 2012 +0100

    Introducing MediaManager class
    
    Separate out installer media management into a separate class.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669771

 src/Makefile.am               |    1 +
 src/installer-media.vala      |   52 +++++++++++-----------------------------
 src/media-manager.vala        |   53 +++++++++++++++++++++++++++++++++++++++++
 src/unattended-installer.vala |    1 +
 src/vm-configurator.vala      |   10 ++-----
 src/vm-creator.vala           |    5 +--
 src/wizard.vala               |   25 +++++++------------
 7 files changed, 83 insertions(+), 64 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index e2417a5..a952370 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -44,6 +44,7 @@ gnome_boxes_SOURCES =				\
 	libvirt-machine.vala			\
 	machine.vala				\
 	menu-box.vala				\
+	media-manager.vala 			\
 	mini-graph.vala				\
 	main.vala				\
 	os-database.vala 			\
diff --git a/src/installer-media.vala b/src/installer-media.vala
index e7ab93a..5415a02 100644
--- a/src/installer-media.vala
+++ b/src/installer-media.vala
@@ -4,7 +4,8 @@ using Osinfo;
 using GUdev;
 
 private class Boxes.InstallerMedia : Object {
-    public Os os;
+    public Os? os;
+    public Osinfo.Resources? resources;
     public Media os_media;
     public string label;
     public string device_file;
@@ -13,55 +14,26 @@ private class Boxes.InstallerMedia : Object {
 
     public bool live { get { return os_media == null || os_media.live; } }
 
-    public static async InstallerMedia instantiate (string       path,
-                                                    OSDatabase   os_db,
-                                                    Client       client,
-                                                    Cancellable? cancellable) throws GLib.Error {
+    public static async InstallerMedia create_for_path (string       path,
+                                                        MediaManager media_manager,
+                                                        Cancellable? cancellable) throws GLib.Error {
         var media = new InstallerMedia ();
-        yield media.setup_for_path (path, os_db, client, cancellable);
 
-        if (media.os == null)
-            return media;
-
-        switch (media.os.short_id) {
-        case "fedora14":
-        case "fedora15":
-        case "fedora16":
-            media = new FedoraInstaller.copy (media);
-
-            break;
-
-        case "win7":
-        case "win2k8":
-            media = new Win7Installer.copy (media);
-
-            break;
-
-        case "winxp":
-        case "win2k":
-        case "win2k3":
-            media = new WinXPInstaller.copy (media);
-
-            break;
-
-        default:
-            return media;
-        }
+        yield media.setup_for_path (path, media_manager, cancellable);
 
         return media;
     }
 
     private async void setup_for_path (string       path,
-                                       OSDatabase   os_db,
-                                       Client       client,
+                                       MediaManager media_manager,
                                        Cancellable? cancellable) throws GLib.Error {
-        var device = yield get_device_from_path (path, client, cancellable);
+        var device = yield get_device_from_path (path, media_manager.client, cancellable);
 
         if (device != null)
-            get_media_info_from_device (device, os_db);
+            get_media_info_from_device (device, media_manager.os_db);
         else {
             from_image = true;
-            os = yield os_db.guess_os_from_install_media (device_file, out os_media, cancellable);
+            os = yield media_manager.os_db.guess_os_from_install_media (device_file, out os_media, cancellable);
         }
 
         if (os != null)
@@ -69,6 +41,10 @@ private class Boxes.InstallerMedia : Object {
 
         if (label == null)
             label = Path.get_basename (device_file);
+
+        // FIXME: these values could be made editable somehow
+        var architecture = (os_media != null) ? os_media.architecture : "i686";
+        resources = media_manager.os_db.get_resources_for_os (os, architecture);
     }
 
     private async GUdev.Device? get_device_from_path (string path, Client client, Cancellable? cancellable) {
diff --git a/src/media-manager.vala b/src/media-manager.vala
new file mode 100644
index 0000000..80a9ff3
--- /dev/null
+++ b/src/media-manager.vala
@@ -0,0 +1,53 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+using Osinfo;
+using GUdev;
+
+private class Boxes.MediaManager : Object {
+    public OSDatabase os_db { get; private set; }
+    public Client client { get; private set; }
+
+    public MediaManager () {
+        client = new GUdev.Client ({"block"});
+        try {
+            os_db = new OSDatabase ();
+        } catch (GLib.Error error) {
+            critical ("Error fetching default OS database: %s", error.message);
+        }
+    }
+
+    public async InstallerMedia create_installer_media_for_path (string       path,
+                                                                 Cancellable? cancellable) throws GLib.Error {
+        var media = yield InstallerMedia.create_for_path (path, this, cancellable);
+
+        if (media.os == null)
+            return media;
+
+        switch (media.os.short_id) {
+        case "fedora14":
+        case "fedora15":
+        case "fedora16":
+            media = new FedoraInstaller.copy (media);
+
+            break;
+
+        case "win7":
+        case "win2k8":
+            media = new Win7Installer.copy (media);
+
+            break;
+
+        case "winxp":
+        case "win2k":
+        case "win2k3":
+            media = new WinXPInstaller.copy (media);
+
+            break;
+
+        default:
+            return media;
+        }
+
+        return media;
+    }
+}
diff --git a/src/unattended-installer.vala b/src/unattended-installer.vala
index 55f0010..900185c 100644
--- a/src/unattended-installer.vala
+++ b/src/unattended-installer.vala
@@ -73,6 +73,7 @@ private abstract class Boxes.UnattendedInstaller: InstallerMedia {
         device_file = media.device_file;
         from_image = media.from_image;
         mount_point = media.mount_point;
+        resources = media.resources;
 
         disk_path = get_pkgcache (os.short_id + "-unattended.img");
         this.unattended_src_path = unattended_src_path;
diff --git a/src/vm-configurator.vala b/src/vm-configurator.vala
index fe8787c..a51e8e7 100644
--- a/src/vm-configurator.vala
+++ b/src/vm-configurator.vala
@@ -4,15 +4,11 @@ using Osinfo;
 using GVirConfig;
 
 private class Boxes.VMConfigurator {
-    public Domain create_domain_config (InstallerMedia install_media,
-                                        string         name,
-                                        string         target_path,
-                                        Resources      resources) {
-
+    public Domain create_domain_config (InstallerMedia install_media, string name, string target_path) {
         var domain = new Domain ();
         domain.name = name;
-        domain.memory = resources.ram / KIBIBYTES;
-        domain.vcpu = resources.n_cpus;
+        domain.memory = install_media.resources.ram / KIBIBYTES;
+        domain.vcpu = install_media.resources.n_cpus;
         domain.set_virt_type (DomainVirtType.KVM);
 
         set_os_config (domain, install_media);
diff --git a/src/vm-creator.vala b/src/vm-creator.vala
index feb18c8..075d099 100644
--- a/src/vm-creator.vala
+++ b/src/vm-creator.vala
@@ -13,7 +13,6 @@ private class Boxes.VMCreator {
     }
 
     public async Domain create_and_launch_vm (InstallerMedia install_media,
-                                              Resources      resources,
                                               Cancellable?   cancellable) throws GLib.Error {
         if (install_media is UnattendedInstaller)
             yield (install_media as UnattendedInstaller).setup (cancellable);
@@ -28,8 +27,8 @@ private class Boxes.VMCreator {
         for (var i = 1; connection.find_domain_by_name (domain_name) != null; i++)
             domain_name = name + "-" + i.to_string ();
 
-        var volume = yield create_target_volume (name, resources.storage);
-        var config = configurator.create_domain_config (install_media, domain_name, volume.get_path (), resources);
+        var volume = yield create_target_volume (name, install_media.resources.storage);
+        var config = configurator.create_domain_config (install_media, domain_name, volume.get_path ());
 
         Domain domain;
         if (install_media.live)
diff --git a/src/wizard.vala b/src/wizard.vala
index 40d962b..b436457 100644
--- a/src/wizard.vala
+++ b/src/wizard.vala
@@ -26,12 +26,10 @@ private class Boxes.Wizard: Boxes.UI {
     private Gtk.VBox setup_vbox;
     private Gtk.Label review_label;
 
-    private OSDatabase os_db;
+    private MediaManager media_manager;
     private VMCreator vm_creator;
-    private GUdev.Client client;
 
     private InstallerMedia? install_media;
-    private Osinfo.Resources? resources;
 
     private WizardPage _page;
     private WizardPage page {
@@ -133,6 +131,8 @@ private class Boxes.Wizard: Boxes.UI {
         wizard_source.url_entry.activate.connect(() => {
             page = page + 1;
         });
+
+        media_manager = new MediaManager ();
     }
 
     public Wizard (App app) {
@@ -148,7 +148,7 @@ private class Boxes.Wizard: Boxes.UI {
 
             next_button.sensitive = false;
             try {
-                yield vm_creator.create_and_launch_vm (install_media, resources, null);
+                yield vm_creator.create_and_launch_vm (install_media, null);
             } catch (IOError.CANCELLED cancel_error) { // We did this, so ignore!
             } catch (GLib.Error error) {
                 warning (error.message);
@@ -156,7 +156,6 @@ private class Boxes.Wizard: Boxes.UI {
             }
 
             install_media = null;
-            resources = null;
             wizard_source.uri = "";
 
             return true;
@@ -211,25 +210,19 @@ private class Boxes.Wizard: Boxes.UI {
     }
 
     private void prepare_for_installer (string path) throws GLib.Error {
-        if (client == null) {
-            client = new GUdev.Client ({"block"});
-            os_db = new OSDatabase ();
+        if (vm_creator == null) {
             vm_creator = new VMCreator (app);
         }
 
         next_button.sensitive = false;
-        InstallerMedia.instantiate.begin (path, os_db, client, null, on_installer_media_instantiated);
+        media_manager.create_installer_media_for_path.begin (path, null, on_installer_media_instantiated);
     }
 
     private void on_installer_media_instantiated (Object? source_object, AsyncResult result) {
         next_button.sensitive = true;
 
         try {
-            install_media = InstallerMedia.instantiate.end (result);
-            // FIXME: these values could be made editable somehow
-            var architecture = (install_media.os_media != null) ? install_media.os_media.architecture : "i686";
-
-            resources = os_db.get_resources_for_os (install_media.os, architecture);
+            install_media = media_manager.create_installer_media_for_path.end (result);
             prep_progress.fraction = 1.0;
             page = page + 1;
         } catch (IOError.CANCELLED cancel_error) { // We did this, so no warning!
@@ -305,7 +298,7 @@ private class Boxes.Wizard: Boxes.UI {
             }
         } else if (install_media != null) {
             summary.add_property (_("System"), install_media.label);
-            var memory = format_size (resources.ram, FormatSizeFlags.IEC_UNITS);
+            var memory = format_size (install_media.resources.ram, FormatSizeFlags.IEC_UNITS);
 
             if (install_media is UnattendedInstaller) {
                 var media = install_media as UnattendedInstaller;
@@ -316,7 +309,7 @@ private class Boxes.Wizard: Boxes.UI {
             }
 
             summary.add_property (_("Memory"), memory);
-            memory = format_size (resources.storage, FormatSizeFlags.IEC_UNITS);
+            memory = format_size (install_media.resources.storage, FormatSizeFlags.IEC_UNITS);
             summary.add_property (_("Disk"),  _("%s maximum".printf (memory)));
         }
     }



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