[gnome-boxes] Move the properties sync code to DisplayConfig



commit 887316aa7d0b1d7a96bb86490698d371b61a5728
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Wed Oct 24 22:26:15 2012 +0200

    Move the properties sync code to DisplayConfig
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680826

 src/display-config.vala     |   30 ++++++++++++++++++++++++------
 src/display-properties.vala |   37 +++++++------------------------------
 src/spice-display.vala      |   18 +++++++++---------
 src/vnc-display.vala        |    8 ++++----
 4 files changed, 44 insertions(+), 49 deletions(-)
---
diff --git a/src/display-config.vala b/src/display-config.vala
index fef01e8..4857e44 100644
--- a/src/display-config.vala
+++ b/src/display-config.vala
@@ -1,6 +1,11 @@
 // This file is part of GNOME Boxes. License: LGPLv2+
 
 public class Boxes.DisplayConfig: GLib.Object, Boxes.IConfig {
+    public struct SyncProperty {
+        string name;
+        Value default_value;
+    }
+
     private CollectionSource source;
 
     private bool has_file {
@@ -77,10 +82,10 @@ public class Boxes.DisplayConfig: GLib.Object, Boxes.IConfig {
         save ();
     }
 
-    public void save_display_property (Object display, string property_name) {
-        var value = Value (display.get_class ().find_property (property_name).value_type);
+    public void save_property (Object object, string property_name) {
+        var value = Value (object.get_class ().find_property (property_name).value_type);
 
-        display.get_property (property_name, ref value);
+        object.get_property (property_name, ref value);
 
         if (value.type () == typeof (string))
             keyfile.set_string (group, property_name, value.get_string ());
@@ -97,8 +102,8 @@ public class Boxes.DisplayConfig: GLib.Object, Boxes.IConfig {
         save ();
     }
 
-    public void load_display_property (Object display, string property_name, Value default_value) {
-        var property = display.get_class ().find_property (property_name);
+    public void load_property (Object object, string property_name, Value default_value) {
+        var property = object.get_class ().find_property (property_name);
         if (property == null) {
             debug ("You forgot the property '%s' needs to have public getter!", property_name);
         }
@@ -118,6 +123,19 @@ public class Boxes.DisplayConfig: GLib.Object, Boxes.IConfig {
             value = default_value;
         }
 
-        display.set_property (property_name, value);
+        object.set_property (property_name, value);
+    }
+
+    public void sync_properties (Object object, SyncProperty[] properties) {
+        foreach (var prop in properties)
+            load_property (object, prop.name, prop.default_value);
+
+        object.notify.connect ((pspec) => {
+            foreach (var prop in properties)
+                if (pspec.name == prop.name) {
+                    save_property (object, pspec.name);
+                    break;
+                }
+        });
     }
 }
diff --git a/src/display-properties.vala b/src/display-properties.vala
index 534347c..201c351 100644
--- a/src/display-properties.vala
+++ b/src/display-properties.vala
@@ -2,11 +2,6 @@
 
 // too bad we can't make it just a mixin
 public class Boxes.DisplayProperties: GLib.Object {
-    protected struct SavedProperty {
-        string name;
-        Value default_value;
-    }
-
     private int64 started_time;
     protected void access_start () {
         if (started_time != 0)
@@ -35,19 +30,17 @@ public class Boxes.DisplayProperties: GLib.Object {
     public int64 access_first_time { set; get; }
     public int64 access_total_time { set; get; } // in seconds
     public int64 access_ntimes { set; get; }
-    private SavedProperty[] access_saved_properties;
+    private DisplayConfig.SyncProperty[] access_properties;
 
     construct {
-        access_saved_properties = {
-            SavedProperty () { name = "access-last-time", default_value = (int64)(-1) },
-            SavedProperty () { name = "access-first-time", default_value = (int64)(-1) },
-            SavedProperty () { name = "access-total-time", default_value = (int64)(-1) },
-            SavedProperty () { name = "access-ntimes", default_value = (uint64)0 }
+        access_properties = {
+            DisplayConfig.SyncProperty () { name = "access-last-time", default_value = (int64)(-1) },
+            DisplayConfig.SyncProperty () { name = "access-first-time", default_value = (int64)(-1) },
+            DisplayConfig.SyncProperty () { name = "access-total-time", default_value = (int64)(-1) },
+            DisplayConfig.SyncProperty () { name = "access-ntimes", default_value = (uint64)0 }
         };
 
-        this.notify["config"].connect (() => {
-            sync_config_with_display (this, access_saved_properties);
-        });
+        config.sync_properties (this, access_properties);
     }
 
     public DisplayProperties.with_config (DisplayConfig config) {
@@ -62,22 +55,6 @@ public class Boxes.DisplayProperties: GLib.Object {
 
     public DisplayConfig? config { get; set; }
 
-    public void sync_config_with_display (Object display, SavedProperty[] saved_properties) {
-        if (config == null)
-            return;
-
-        foreach (var prop in saved_properties)
-            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) {
-                    config.save_display_property (display, pspec.name);
-                    break;
-                }
-        });
-    }
-
     private string filter_data;
 
     private void update_filter_data () {
diff --git a/src/spice-display.vala b/src/spice-display.vala
index e41ca63..7ba5389 100644
--- a/src/spice-display.vala
+++ b/src/spice-display.vala
@@ -11,8 +11,8 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
     private unowned Spice.Audio audio;
     private ulong channel_new_id;
     private ulong channel_destroy_id;
-    private DisplayProperties.SavedProperty[] display_saved_properties;
-    private DisplayProperties.SavedProperty[] gtk_session_saved_properties;
+    private DisplayConfig.SyncProperty[] display_sync_properties;
+    private DisplayConfig.SyncProperty[] gtk_session_sync_properties;
     private bool connected;
 
     public bool resize_guest { get; set; }
@@ -27,13 +27,13 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
     }
 
     construct {
-        display_saved_properties = {
-            SavedProperty () { name = "resize-guest", default_value = true }
+        display_sync_properties = {
+            DisplayConfig.SyncProperty () { name = "resize-guest", default_value = true }
         };
 
-        gtk_session_saved_properties = {
-            SavedProperty () { name = "auto-clipboard", default_value = true },
-            SavedProperty () { name = "auto-usbredir", default_value = false }
+        gtk_session_sync_properties = {
+            DisplayConfig.SyncProperty () { name = "auto-clipboard", default_value = true },
+            DisplayConfig.SyncProperty () { name = "auto-usbredir", default_value = false }
         };
 
         need_password = false;
@@ -42,7 +42,7 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
         gtk_session = GtkSession.get (session);
 
         this.notify["config"].connect (() => {
-            sync_config_with_display (gtk_session, gtk_session_saved_properties);
+            config.sync_properties (gtk_session, gtk_session_sync_properties);
         });
 
         App.app.notify["ui-state"].connect (ui_state_changed);
@@ -90,7 +90,7 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
             display.mouse_grab.connect((status) => {
                 mouse_grabbed = status != 0;
             });
-            sync_config_with_display (this, display_saved_properties);
+            config.sync_properties (this, display_sync_properties);
             display.scaling = true;
             if (display.get_class ().find_property ("only-downscale") != null)
                 display.set ("only-downscale", true);
diff --git a/src/vnc-display.vala b/src/vnc-display.vala
index d8716a8..9a9da52 100644
--- a/src/vnc-display.vala
+++ b/src/vnc-display.vala
@@ -9,12 +9,12 @@ private class Boxes.VncDisplay: Boxes.Display {
     private string host;
     private int port;
     private Gtk.Window window;
-    private DisplayProperties.SavedProperty[] saved_properties;
+    private DisplayConfig.SyncProperty[] sync_properties;
     private bool connected;
 
     construct {
-        saved_properties = {
-            SavedProperty () { name = "read-only", default_value = false }
+        sync_properties = {
+            DisplayConfig.SyncProperty () { name = "read-only", default_value = false }
         };
         need_password = false;
 
@@ -72,7 +72,7 @@ private class Boxes.VncDisplay: Boxes.Display {
         });
 
         this.notify["config"].connect (() => {
-            sync_config_with_display (display, saved_properties);
+            config.sync_properties (display, sync_properties);
         });
     }
 



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