[connections/refactor-storage] connection, database: Refactor how we store data




commit a018a3ccf8be014193205944f3b5a65f728db8cb
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue May 11 15:14:06 2021 +0200

    connection, database: Refactor how we store data
    
    GObject has very convenient and reliable APIs for serializing object
    properties' names and values. This way we can sync up object state
    with a key-file without an extra layer of serialization.
    
    Connection property names become more implicit but it saves us from
    lots of boilerplate code to add a new connection property.

 src/actions-popover.vala       |   2 +-
 src/application.vala           |   2 -
 src/connection.vala            |  63 ++++-------------------
 src/database.vala              | 112 +++++++++++++++++++++++++----------------
 src/properties.vala            |   2 -
 src/rdp-connection.vala        |  22 ++------
 src/topbar.vala                |   2 +-
 src/vnc-connection.vala        |  83 ++----------------------------
 src/vnc-properties-dialog.vala |   4 +-
 9 files changed, 88 insertions(+), 204 deletions(-)
---
diff --git a/src/actions-popover.vala b/src/actions-popover.vala
index c027697..a6c5e5d 100644
--- a/src/actions-popover.vala
+++ b/src/actions-popover.vala
@@ -60,7 +60,7 @@ namespace Connections {
         private void properties_activated () {
             debug ("Launch properties for %s", connection.uri);
 
-            if (connection.protocol == Connection.Protocol.VNC)
+            if (connection.protocol == "vnc")
                 (new VncPropertiesDialog (connection).run ());
             else
                 (new RdpPropertiesDialog (connection).run ());
diff --git a/src/application.vala b/src/application.vala
index 9dc7e5c..43a97b9 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -123,7 +123,6 @@ namespace Connections {
             switch (mime_type) {
                 case "application/x-vnc":
                     connection = new VncConnection.from_vnc_file (file_path);
-                    connection.protocol = Connection.Protocol.VNC;
                     break;
                 default:
                     warning ( _ ("Couldn’t open file of unknown mime type %s".printf (mime_type)));
@@ -134,7 +133,6 @@ namespace Connections {
                 return;
 
             model.insert (0, connection);
-            connection.save ();
         }
 
         public void remove_connection (Connection connection) {
diff --git a/src/connection.vala b/src/connection.vala
index b9b4d06..d5a343a 100644
--- a/src/connection.vala
+++ b/src/connection.vala
@@ -32,53 +32,8 @@ namespace Connections {
 
         public abstract bool scaling { get; set; }
 
-        protected ConnectionConfig config;
-
         public bool connected;
 
-        public enum Protocol {
-            UNKNOWN,
-            VNC,
-            RDP;
-
-            public string to_string () {
-                switch (this) {
-                    case VNC:
-                        return "vnc";
-                    case RDP:
-                        return "rdp";
-                    case UNKNOWN:
-                        return "unknown";
-                    default:
-                        assert_not_reached ();
-                }
-            }
-
-            public Protocol from_string (string protocol) {
-                switch (protocol) {
-                    case "vnc":
-                        return VNC;
-                    case "rdp":
-                        return RDP;
-                    case "unknown":
-                    default:
-                       return UNKNOWN;
-                }
-            }
-
-            public int get_default_port () {
-                switch (this) {
-                    case VNC:
-                        return 5900;
-                    case RDP:
-                        return 3389;
-                    case UNKNOWN:
-                    default:
-                        assert_not_reached ();
-                }
-            }
-        }
-        public Protocol protocol;
 
         public string uri {
             owned get {
@@ -88,14 +43,15 @@ namespace Connections {
             set {
                 var address = Xml.URI.parse (value);
 
-                protocol = protocol.from_string (address.scheme);
+                protocol = address.scheme;
                 host = address.server;
-                port = (address.port != 0) ? address.port : protocol.get_default_port ();
+                port = (address.port != 0) ? address.port : port;
             }
         }
 
-        public string host;
-        public int port;
+        public string host { get; protected set; }
+        public abstract int port { get; protected set; }
+        public string protocol { get; protected set; }
 
         private string _display_name;
         public string display_name {
@@ -193,10 +149,6 @@ namespace Connections {
                                                                                         need_username);
         }
 
-        public void save () {
-            config.save ();
-        }
-
         construct {
             thumbnailer = new Connections.Thumbnailer (this);
 
@@ -205,6 +157,11 @@ namespace Connections {
             notify.connect (save);
         }
 
+        public void save (GLib.ParamSpec? pspec = null) {
+            if (uuid != null && pspec != null)
+                Database.get_default ().save_property (this, pspec.name);
+        }
+
         public abstract void connect_it ();
         public abstract void disconnect_it ();
     }
diff --git a/src/database.vala b/src/database.vala
index 4b3829b..ff494ee 100644
--- a/src/database.vala
+++ b/src/database.vala
@@ -20,35 +20,6 @@
  */
 
 namespace Connections {
-    private class ConnectionConfig : Connections.Database {
-
-        public unowned Connection connection;
-
-        construct {
-            load_keyfile ();
-        }
-
-        public virtual void load () {
-            connection.protocol = connection.protocol.from_string (get_string (connection.uuid, "protocol"));
-            connection.host = get_string (connection.uuid, "host");
-            connection.port = int.parse (get_string (connection.uuid, "port"));
-            connection.display_name = get_string (connection.uuid, "display-name");
-
-            connection.scaling = get_boolean (connection.uuid, "scaling");
-        }
-
-        public virtual void save () {
-            set_string (connection.uuid, "protocol", connection.protocol.to_string ());
-            set_string (connection.uuid, "host", connection.host);
-            set_string (connection.uuid, "port", connection.port.to_string ());
-            set_string (connection.uuid, "display-name", connection.display_name);
-
-            set_boolean (connection.uuid, "scaling", connection.scaling);
-
-            save_keyfile ();
-        }
-    }
-
     private class Database : Object {
         private static Database database;
         public static Database get_default () {
@@ -62,7 +33,7 @@ namespace Connections {
         protected string? filename = Path.build_filename (Environment.get_user_config_dir (),
                                                           "connections.db");
 
-        public void load_keyfile () {
+        private void load_keyfile () {
             try {
                 File file = File.new_for_path (filename);
                 file.create (FileCreateFlags.PRIVATE);
@@ -82,7 +53,7 @@ namespace Connections {
             }
         }
 
-        public bool save_keyfile () {
+        private bool save_keyfile () {
             try {
                 return FileUtils.set_contents (filename, keyfile.to_data (null));
             } catch (GLib.Error error) {
@@ -92,7 +63,7 @@ namespace Connections {
             }
         }
 
-        public bool get_boolean (string group, string key) {
+        private bool get_boolean (string group, string key) {
             try {
                 return keyfile.get_boolean (group, key);
             } catch (GLib.KeyFileError error) {
@@ -102,15 +73,7 @@ namespace Connections {
             }
         }
 
-        public void set_boolean (string group, string key, bool value) {
-            keyfile.set_boolean (group, key, value);
-        }
-
-        public void set_string (string group, string key, string value) {
-            keyfile.set_string (group, key, value);
-        }
-
-        public string get_string (string group, string key) {
+        private string get_string (string group, string key) {
             try {
                 return keyfile.get_string (group, key);
             } catch (GLib.KeyFileError error) {
@@ -120,6 +83,58 @@ namespace Connections {
             }
         }
 
+        private void load_properties (Connection connection) {
+            try {
+                foreach (var property_name in keyfile.get_keys (connection.uuid))
+                    load_property (connection, property_name);
+            } catch (GLib.Error error) {
+                warning ("Failed to load connection properties: %s".printf (error.message));
+            }
+        }
+
+        private void load_property (Connection connection, string property_name) {
+            var property = connection.get_class ().find_property (property_name);
+            if (property == null)
+                return;
+
+            var value = GLib.Value (property.value_type);
+            try {
+                if (value.type () == typeof (string))
+                    value = get_string (connection.uuid, property_name);
+                if (value.type () == typeof (bool))
+                    value = get_boolean (connection.uuid, property_name);
+                if (value.type () == typeof (uint64))
+                    value = keyfile.get_uint64 (connection.uuid, property_name);
+                if (value.type () == typeof (int64))
+                    value = keyfile.get_int64 (connection.uuid, property_name);
+            } catch (GLib.Error error) {
+            }
+
+            connection.set_property (property_name, value);
+        }
+
+        public void save_property (Connection connection, string property_name) {
+            load_keyfile ();
+
+            var value = GLib.Value (connection.get_class ().find_property (property_name).value_type);
+
+            connection.get_property (property_name, ref value);
+
+            if (value.type () == typeof (string))
+                keyfile.set_string (connection.uuid, property_name, value.get_string ());
+            else if (value.type () == typeof (bool))
+                keyfile.set_boolean (connection.uuid, property_name, value.get_boolean ());
+            else if (value.type () == typeof (uint64))
+                keyfile.set_uint64 (connection.uuid, property_name, value.get_uint64 ());
+            else if (value.type () == typeof (int64))
+                keyfile.set_int64 (connection.uuid, property_name, value.get_int64 ());
+            else
+                warning ("Unhadled property %s type, value: %s".printf (property_name,
+                                                                        value.strdup_contents ()));
+
+            save_keyfile ();
+        }
+
         public List<Connections.Connection> get_connections () {
             load_keyfile ();
 
@@ -137,18 +152,27 @@ namespace Connections {
             if (!keyfile.has_group (uuid))
                 return null;
 
+            Connection? connection = null;
             var protocol = get_string (uuid, "protocol");
             switch (protocol) {
                 case "vnc":
-                    return new VncConnection (uuid);
+                    connection = new VncConnection (uuid);
+                    break;
                 case "rdp":
-                    return new RdpConnection (uuid);
+                    connection = new RdpConnection (uuid);
+                    break;
                 default:
                     debug ("Unknown protocol defined for %s", uuid);
                     break;
             }
 
-            return null;
+
+            if (connection == null)
+                return null;
+
+            load_properties (connection);
+
+            return connection;
         }
 
         public Connection add_connection (string _uri) {
diff --git a/src/properties.vala b/src/properties.vala
index 378529d..1f9457e 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -121,8 +121,6 @@ namespace Connections {
                 connection.display_name = connection_name_entry.text;
                 connection_name_label.set_text (connection_name_entry.text);
             }
-
-            connection.save ();
         }
     }
 
diff --git a/src/rdp-connection.vala b/src/rdp-connection.vala
index 57bc07e..80ec63d 100644
--- a/src/rdp-connection.vala
+++ b/src/rdp-connection.vala
@@ -52,6 +52,8 @@ namespace Connections {
             }
         }
 
+        public override int port { get; protected set; default = 3389; }
+
         construct {
             display = new FrdpDisplay ();
             display.bind_property ("username", this, "username", BindingFlags.BIDIRECTIONAL);
@@ -60,23 +62,15 @@ namespace Connections {
             display.rdp_connected.connect (() => { show (); });
             display.rdp_needs_authentication.connect (on_rdp_auth_credential_cb);
             //display.size_allocate.connect (scale);
-
-            config = new RdpConfig () {
-                connection = this
-            };
         }
 
         public RdpConnection (string uuid) {
             this.uuid = uuid;
-
-            config.load ();
         }
 
         public RdpConnection.from_uri (string uri) {
-            this.uri = uri;
-
             this.uuid = Uuid.string_random ();
-            config.save ();
+            this.uri = uri;
         }
 
         ~RdpConnection () {
@@ -130,14 +124,4 @@ namespace Connections {
             add_property (scaling);
         }
     }
-
-    private class RdpConfig : ConnectionConfig {
-        public override void load () {
-            base.load ();
-        }
-
-        public override void save () {
-            base.save ();
-        }
-    }
 }
diff --git a/src/topbar.vala b/src/topbar.vala
index 322de19..a3170d6 100644
--- a/src/topbar.vala
+++ b/src/topbar.vala
@@ -66,7 +66,7 @@ namespace Connections {
         }
 
         private void properties_activated () {
-            if (connection.protocol == Connection.Protocol.VNC)
+            if (connection.protocol == "vnc")
                 (new VncPropertiesDialog (connection).run ());
             else
                 (new RdpPropertiesDialog (connection).run ());
diff --git a/src/vnc-connection.vala b/src/vnc-connection.vala
index 68c67de..e70478c 100644
--- a/src/vnc-connection.vala
+++ b/src/vnc-connection.vala
@@ -79,50 +79,8 @@ namespace Connections {
             }
         }
 
-        public enum Bandwidth {
-            HIGH_QUALITY,
-            FAST_REFRESH;
-
-            public string to_string () {
-                switch (this) {
-                    case FAST_REFRESH:
-                        return "fast-refresh";
-                    case HIGH_QUALITY:
-                    default:
-                        return "high-quality";
-                }
-            }
-
-            public Bandwidth from_string (string bandwidth) {
-                switch (bandwidth) {
-                    case "fast-refresh":
-                        return FAST_REFRESH;
-                    case "high-quality":
-                    default:
-                        return HIGH_QUALITY;
-                }
-            }
-        }
-        private Bandwidth _bandwidth;
-        public Bandwidth bandwidth {
-            set {
-                switch (value) {
-                case FAST_REFRESH:
-                    display.set_depth (DisplayDepthColor.LOW);
-                    break;
-
-                case HIGH_QUALITY:
-                default:
-                    display.set_depth (DisplayDepthColor.FULL);
-                    break;
-                }
-                _bandwidth = value;
-            }
-
-            get {
-                return _bandwidth;
-            }
-        }
+        public override int port { get; protected set; default = 3900; }
+        public string bandwidth { get; set; default = "hight-quality"; }
 
         construct {
             display = new Vnc.Display ();
@@ -139,23 +97,15 @@ namespace Connections {
             display.vnc_auth_credential.connect (on_vnc_auth_credential_cb);
             display.vnc_auth_failure.connect (on_vnc_auth_failure_cb);
             display.size_allocate.connect (scale);
-
-            config = new VncConfig () {
-                connection = this
-            };
         }
 
         public VncConnection (string uuid) {
             this.uuid = uuid;
-
-            config.load ();
         }
 
         public VncConnection.from_uri (string uri) {
-            this.uri = uri;
-
             this.uuid = Uuid.string_random ();
-            config.save ();
+            this.uri = uri;
         }
 
         public VncConnection.from_vnc_file(string file_path) {
@@ -178,7 +128,6 @@ namespace Connections {
                 this.port = key_file.get_integer ("Connection", "Port");
             } catch (GLib.Error e) {
                 info  ( _ ("VNC File is missing key “%s”".printf ("Port")));
-                this.port = Connection.Protocol.VNC.get_default_port();
             }
             try {
                 this.username = key_file.get_string ("Connection", "Username");
@@ -192,7 +141,6 @@ namespace Connections {
             }
 
             this.uuid = Uuid.string_random ();
-            this.config.save();
         }
 
         ~VncConnection () {
@@ -275,29 +223,4 @@ namespace Connections {
             }
         }
     }
-
-    private class VncConfig : ConnectionConfig {
-        public override void load () {
-            base.load ();
-
-            var vnc = connection as VncConnection;
-
-            vnc.view_only = get_boolean (connection.uuid, "view_only");
-            vnc.show_local_pointer = get_boolean (connection.uuid, "show_local_pointer");
-            vnc.bandwidth = vnc.bandwidth.from_string (get_string (connection.uuid, "bandwidth"));
-        }
-
-        public override void save () {
-            if (connection.uuid == null)
-                return;
-
-            var vnc = connection as VncConnection;
-
-            set_boolean (connection.uuid, "view_only", vnc.view_only);
-            set_boolean (connection.uuid, "show_local_pointer", vnc.show_local_pointer);
-            set_string (connection.uuid, "bandwidth", vnc.bandwidth.to_string ());
-
-            base.save ();
-        }
-    }
 }
diff --git a/src/vnc-properties-dialog.vala b/src/vnc-properties-dialog.vala
index 04b50af..6cf7af3 100644
--- a/src/vnc-properties-dialog.vala
+++ b/src/vnc-properties-dialog.vala
@@ -40,13 +40,13 @@ namespace Connections {
             add_property (local_pointer);
 
             var vnc = connection as VncConnection;
-            var bandwidth = new ComboProperty (vnc, "bandwidth", vnc.bandwidth.to_string ()) {
+            var bandwidth = new ComboProperty (vnc, "bandwidth", vnc.bandwidth) {
                 label = _("Bandwidth")
             };
             bandwidth.add_option ("high-quality", _("High quality"));
             bandwidth.add_option ("fast-refresh", _("Fast refresh"));
             bandwidth.changed.connect ((property_value) => {
-               vnc.bandwidth = vnc.bandwidth.from_string (property_value);
+               vnc.bandwidth = property_value;
             });
             add_property (bandwidth);
         }


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