[gnome-boxes] Split DisplayConfig out of CollectionSource



commit e3f5654ac900a2cc9cbc2a1b28c6855ef9483dd2
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Thu Nov 3 20:41:26 2011 +0100

    Split DisplayConfig out of CollectionSource

 src/Makefile.am            |   25 ++++----
 src/collection-source.vala |  131 ++++++++++++++++++--------------------------
 src/collection-view.vala   |    2 +-
 src/display-config.vala    |   63 +++++++++++++++++++++
 src/display.vala           |   10 ++--
 src/machine.vala           |    1 +
 src/remote-machine.vala    |    5 +-
 src/spice-display.vala     |   10 ++--
 src/vnc-display.vala       |    6 +-
 9 files changed, 147 insertions(+), 106 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index c2ea7a2..b586636 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,46 +16,47 @@ AM_VALAFLAGS =						\
 	--pkg config					\
 	--pkg gdk-pixbuf-2.0				\
 	--pkg glib-2.0					\
-	--pkg libvirt-gobject-1.0			\
+	--pkg gtk-vnc-2.0				\
+	--pkg gudev-1.0					\
+	--pkg libosinfo-1.0				\
 	--pkg libvirt-gconfig-1.0			\
+	--pkg libvirt-gobject-1.0			\
 	--pkg libxml-2.0				\
 	--pkg posix					\
 	--pkg spice-client-gtk-3.0			\
-	--pkg gtk-vnc-2.0				\
-	--pkg gudev-1.0					\
-	--pkg libosinfo-1.0				\
 	$(NULL)
 
 bin_PROGRAMS = gnome-boxes
 
 gnome_boxes_SOURCES =				\
 	app.vala				\
-	main.vala				\
 	collection-source.vala			\
 	collection-view.vala			\
 	collection.vala				\
+	display-config.vala			\
 	display-page.vala			\
 	display.vala				\
 	editable-entry.vala			\
+	fedora-installer.vala 			\
+	installer-media.vala 			\
 	libvirt-machine.vala			\
 	machine.vala				\
+	main.vala				\
+	os-database.vala 			\
 	properties.vala				\
 	remote-machine.vala			\
 	sidebar.vala				\
 	spice-display.vala			\
 	topbar.vala				\
 	ui.vala					\
+	unattended-installer.vala 		\
 	util.vala				\
+	vm-creator.vala 			\
 	vnc-display.vala			\
-	wizard-source.vala			\
-	wizard.vala				\
-	installer-media.vala 			\
-	unattended-installer.vala 		\
-	fedora-installer.vala 			\
 	win7-installer.vala 			\
 	winxp-installer.vala 			\
-	os-database.vala 			\
-	vm-creator.vala 			\
+	wizard-source.vala			\
+	wizard.vala				\
 	$(NULL)
 
 gnome_boxes_LDADD = $(BOXES_LIBS)
diff --git a/src/collection-source.vala b/src/collection-source.vala
index 5c66dbc..fb2f0f2 100644
--- a/src/collection-source.vala
+++ b/src/collection-source.vala
@@ -1,7 +1,57 @@
 // This file is part of GNOME Boxes. License: LGPLv2+
 
-private class Boxes.CollectionSource: GLib.Object {
-    private KeyFile keyfile;
+private interface Boxes.IConfig {
+
+    protected abstract KeyFile keyfile { get; }
+    public abstract string? filename { get; set; }
+    protected abstract bool has_file { get; set; }
+
+    public void save () {
+        keyfile_save (keyfile, get_pkgconfig_source (filename), has_file);
+        has_file = true;
+    }
+
+    protected void load () throws GLib.Error {
+        if (!has_file)
+            throw new Boxes.Error.INVALID ("has_file is false");
+
+        keyfile.load_from_file (get_pkgconfig_source (filename),
+                                KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
+    }
+
+    protected string? get_string (string group, string key) {
+        try {
+            return keyfile.get_string (group, key);
+        } catch (GLib.KeyFileError error) {
+            return null;
+        }
+    }
+
+    protected string[]? get_string_list (string group, string key) {
+        try {
+            return keyfile.get_string_list (group, key);
+        } catch (GLib.KeyFileError error) {
+            return null;
+        }
+    }
+}
+
+private class Boxes.CollectionSource: GLib.Object, Boxes.IConfig {
+    private KeyFile _keyfile;
+    private KeyFile keyfile { get { return _keyfile; } }
+
+    private bool has_file { get; set; }
+
+    private string? _filename;
+    public string? filename {
+        get {
+            if (_filename == null)
+                _filename = make_filename (name);
+            return _filename;
+        }
+        set { _filename = value; }
+    }
+
     public string? name {
         owned get { return get_string ("source", "name"); }
         set {
@@ -20,25 +70,9 @@ private class Boxes.CollectionSource: GLib.Object {
         owned get { return get_string ("source", "uri"); }
         set { keyfile.set_string ("source", "uri", value); }
     }
-    public string[]? categories {
-        owned get { return get_string_list ("display", "categories"); }
-        set { keyfile.set_string_list ("display", "categories", value); }
-    }
-
-    private string? _filename;
-    public string? filename {
-        get {
-            if (_filename == null)
-                _filename = make_filename (name);
-            return _filename;
-        }
-        set { _filename = value; }
-    }
-
-    private bool has_file;
 
     construct {
-        keyfile = new KeyFile ();
+        _keyfile = new KeyFile ();
     }
 
     public CollectionSource (string name, string source_type, string uri) {
@@ -52,63 +86,4 @@ private class Boxes.CollectionSource: GLib.Object {
         has_file = true;
         load ();
     }
-
-    private void load () throws GLib.Error {
-        keyfile.load_from_file (get_pkgconfig_source (filename),
-                                KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
-    }
-
-    public void save () {
-        keyfile_save (keyfile, get_pkgconfig_source (filename), has_file);
-        has_file = true;
-    }
-
-    private string? get_string (string group, string key) {
-        try {
-            return keyfile.get_string (group, key);
-        } catch (GLib.KeyFileError error) {
-            return null;
-        }
-    }
-
-    private string[]? get_string_list (string group, string key) {
-        try {
-            return keyfile.get_string_list (group, key);
-        } catch (GLib.KeyFileError error) {
-            return null;
-        }
-    }
-
-    public void save_display_property (Object display, string property_name) {
-        var group = "display";
-        var value = Value (display.get_class ().find_property (property_name).value_type);
-
-        display.get_property (property_name, ref value);
-
-        if (value.type () == typeof (string))
-            keyfile.set_string (group, property_name, value.get_string ());
-        else if (value.type () == typeof (bool))
-            keyfile.set_boolean (group, property_name, value.get_boolean ());
-        else
-            warning ("unhandled property %s type, value: %s".printf (
-                         property_name, value.strdup_contents ()));
-
-        save ();
-    }
-
-    public void load_display_property (Object display, string property_name, Value default_value) {
-        var group = "display";
-        var value = Value (display.get_class ().find_property (property_name).value_type);
-
-        try {
-            if (value.type () == typeof (string))
-                value = keyfile.get_string (group, property_name);
-            if (value.type () == typeof (bool))
-                value = keyfile.get_boolean (group, property_name);
-        } catch (GLib.Error err) {
-            value = default_value;
-        }
-
-        display.set_property (property_name, value);
-    }
 }
diff --git a/src/collection-view.vala b/src/collection-view.vala
index 4d73384..1274cb7 100644
--- a/src/collection-view.vala
+++ b/src/collection-view.vala
@@ -121,7 +121,7 @@ private class Boxes.CollectionView: Boxes.UI {
 
             switch (category.kind) {
             case Category.Kind.USER:
-                visible = category.name in machine.source.categories;
+                visible = category.name in machine.config.categories;
                 break;
             case Category.Kind.NEW:
                 visible = true;
diff --git a/src/display-config.vala b/src/display-config.vala
new file mode 100644
index 0000000..560e4d1
--- /dev/null
+++ b/src/display-config.vala
@@ -0,0 +1,63 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+private class Boxes.DisplayConfig: GLib.Object, Boxes.IConfig {
+    private CollectionSource source;
+
+    private bool has_file {
+        get { return source.has_file; }
+        set { source.has_file = value; }
+    }
+    private string? filename {
+        get { return source.filename; }
+        set { warning ("not allowed to change filename"); }
+    }
+    private KeyFile keyfile {
+        get { return source.keyfile; }
+    }
+
+    private string group;
+
+    public string[]? categories {
+        owned get { return get_string_list (group, "categories"); }
+        set { keyfile.set_string_list (group, "categories", value); }
+    }
+
+    public DisplayConfig (CollectionSource source, string? subgroup = null) {
+        this.source = source;
+
+        this.group = "display";
+        if (subgroup != null)
+            this.group += " " + subgroup;
+    }
+
+    public void save_display_property (Object display, string property_name) {
+        var value = Value (display.get_class ().find_property (property_name).value_type);
+
+        display.get_property (property_name, ref value);
+
+        if (value.type () == typeof (string))
+            keyfile.set_string (group, property_name, value.get_string ());
+        else if (value.type () == typeof (bool))
+            keyfile.set_boolean (group, property_name, value.get_boolean ());
+        else
+            warning ("unhandled property %s type, value: %s".printf (
+                         property_name, value.strdup_contents ()));
+
+        save ();
+    }
+
+    public void load_display_property (Object display, string property_name, Value default_value) {
+        var value = Value (display.get_class ().find_property (property_name).value_type);
+
+        try {
+            if (value.type () == typeof (string))
+                value = keyfile.get_string (group, property_name);
+            if (value.type () == typeof (bool))
+                value = keyfile.get_boolean (group, property_name);
+        } catch (GLib.Error err) {
+            value = default_value;
+        }
+
+        display.set_property (property_name, value);
+    }
+}
diff --git a/src/display.vala b/src/display.vala
index 1bfc0c6..a8ed177 100644
--- a/src/display.vala
+++ b/src/display.vala
@@ -32,19 +32,19 @@ private abstract class Boxes.Display: GLib.Object, Boxes.IProperties {
         displays = new HashTable<int, Gtk.Widget> (direct_hash, direct_equal);
     }
 
-    public CollectionSource? source { get; set; }
+    public DisplayConfig? config { get; set; }
 
-    public void sync_source_with_display (Object display, SavedProperty[] saved_properties) {
-        if (source == null)
+    public void sync_config_with_display (Object display, SavedProperty[] saved_properties) {
+        if (config == null)
             return;
 
         foreach (var prop in saved_properties)
-            source.load_display_property (display, prop.name, prop.default_value);
+            config.load_display_property (display, prop.name, prop.default_value);
 
         display.notify.connect ((pspec) => {
             foreach (var prop in saved_properties)
                 if (pspec.name == prop.name) {
-                    source.save_display_property (display, pspec.name);
+                    config.save_display_property (display, pspec.name);
                     break;
                 }
         });
diff --git a/src/machine.vala b/src/machine.vala
index 1861407..11759b4 100644
--- a/src/machine.vala
+++ b/src/machine.vala
@@ -8,6 +8,7 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IProperties {
     public Boxes.App app;
     public MachineActor machine_actor;
     public Boxes.CollectionSource source;
+    public Boxes.DisplayConfig config;
 
     private ulong show_id;
     private ulong hide_id;
diff --git a/src/remote-machine.vala b/src/remote-machine.vala
index fd7e49d..213aa17 100644
--- a/src/remote-machine.vala
+++ b/src/remote-machine.vala
@@ -6,6 +6,7 @@ private class Boxes.RemoteMachine: Boxes.Machine, Boxes.IProperties {
     public RemoteMachine (CollectionSource source, Boxes.App app) {
         base (source, app, source.name);
 
+        config = new DisplayConfig (source);
         source.bind_property ("name", this, "name", BindingFlags.DEFAULT);
         update_screenshot.begin ();
     }
@@ -16,9 +17,9 @@ private class Boxes.RemoteMachine: Boxes.Machine, Boxes.IProperties {
 
         try {
             if (source.source_type == "spice")
-                display = new SpiceDisplay.with_uri (source, source.uri);
+                display = new SpiceDisplay.with_uri (config, source.uri);
             else if (source.source_type == "vnc")
-                display = new VncDisplay.with_uri (source, source.uri);
+                display = new VncDisplay.with_uri (config, source.uri);
 
             display.connect_it ();
         } catch (GLib.Error error) {
diff --git a/src/spice-display.vala b/src/spice-display.vala
index f858884..d242b80 100644
--- a/src/spice-display.vala
+++ b/src/spice-display.vala
@@ -27,8 +27,8 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IProperties {
         session = new Session ();
         gtk_session = GtkSession.get (session);
 
-        this.notify["source"].connect (() => {
-            sync_source_with_display (gtk_session, gtk_session_saved_properties);
+        this.notify["config"].connect (() => {
+            sync_config_with_display (gtk_session, gtk_session_saved_properties);
         });
     }
 
@@ -37,8 +37,8 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IProperties {
         session.host = host;
     }
 
-    public SpiceDisplay.with_uri (CollectionSource source, string uri) {
-        this.source = source;
+    public SpiceDisplay.with_uri (DisplayConfig config, string uri) {
+        this.config = config;
 
         session.uri = uri;
     }
@@ -52,7 +52,7 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IProperties {
             if (display == null)
                 throw new Boxes.Error.INVALID ("invalid display");
 
-            sync_source_with_display (display, display_saved_properties);
+            sync_config_with_display (display, display_saved_properties);
 
             display.scaling = true;
 
diff --git a/src/vnc-display.vala b/src/vnc-display.vala
index c1144b0..ebf8fb6 100644
--- a/src/vnc-display.vala
+++ b/src/vnc-display.vala
@@ -75,9 +75,9 @@ private class Boxes.VncDisplay: Boxes.Display {
         this.port = port;
     }
 
-    public VncDisplay.with_uri (CollectionSource source, string _uri) throws Boxes.Error {
-        this.source = source;
-        sync_source_with_display (display, saved_properties);
+    public VncDisplay.with_uri (DisplayConfig config, string _uri) throws Boxes.Error {
+        this.config = config;
+        sync_config_with_display (display, saved_properties);
 
         var uri = Xml.URI.parse (_uri);
 



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