[connections/turn-db-object-into-singleton: 2/3] database: Handle connection creation in a singleton




commit 71620f0b661856339300276f86aa36fae7eaabff
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Apr 20 15:02:52 2021 +0200

    database: Handle connection creation in a singleton

 src/application.vala | 19 ++-----------------
 src/database.vala    | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 17 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 5d87ed4..f1880e2 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -108,27 +108,12 @@ namespace Connections {
             return window;
         }
 
-        public void add_connection (string _uri) {
-            Connection? connection = null;
-
-            var uri = Xml.URI.parse (_uri);
-            switch (uri.scheme) {
-                case "vnc":
-                    connection = new VncConnection.from_uri (_uri);
-                    break;
-                case "rdp":
-                    connection = new RdpConnection.from_uri (_uri);
-                    break;
-                default:
-                    debug ("Failed to add '%s': unknown protocol", _uri);
-                    break;
-            }
-
+        public void add_connection (string uri) {
+            var connection = Database.get_default ().add_connection (uri);
             if (connection == null)
                 return;
 
             model.insert (0, connection);
-            connection.save ();
         }
 
         public void add_connection_from_file (string file_path) {
diff --git a/src/database.vala b/src/database.vala
index e130c24..df7b475 100644
--- a/src/database.vala
+++ b/src/database.vala
@@ -60,6 +60,14 @@ namespace Connections {
     }
 
     private class Database : Object {
+        private static Database database;
+        public static Database get_default () {
+            if (database == null)
+                database = new Database ();
+
+            return database;
+        }
+
         protected KeyFile keyfile = new KeyFile ();
         protected string? filename = Path.build_filename (Environment.get_user_config_dir (),
                                                           "connections.db");
@@ -152,5 +160,26 @@ namespace Connections {
 
             return null;
         }
+
+        public Connection add_connection (string _uri) {
+            Connection? connection = null;
+
+            var uri = Xml.URI.parse (_uri);
+            switch (uri.scheme) {
+                case "vnc":
+                    connection = new VncConnection.from_uri (_uri);
+                    break;
+                case "rdp":
+                    connection = new RdpConnection.from_uri (_uri);
+                    break;
+                default:
+                    debug ("Failed to add '%s': unknown protocol", _uri);
+                    break;
+            }
+
+            connection.save ();
+
+            return connection;
+        }
     }
 }


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