Debian Codegraph @

src/database.vala

raw
1 /* database.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 namespace Connections {
23 private class Database : Object {
24 private static Database database;
25 public static Database get_default () {
26 if (database == null)
27 database = new Database ();
28
29 return database;
30 }
31
32 protected KeyFile keyfile = new KeyFile ();
33 protected string? filename = Path.build_filename (Environment.get_user_config_dir (),
34 "connections.db");
35
36 private void load_keyfile () {
37 try {
38 File file = File.new_for_path (filename);
39 file.create (FileCreateFlags.PRIVATE);
40 } catch (GLib.IOError.EXISTS error) {
41 // This means that the database exists. Let's use it!
42 } catch (GLib.Error error) {
43 warning ("Failed to load database file: %s", error.message);
44
45 return;
46 }
47
48 try {
49 keyfile.load_from_file (filename,
50 KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
51 } catch (GLib.Error error) {
52 warning ("Failed to load database: %s", error.message);
53 }
54 }
55
56 private bool save_keyfile () {
57 try {
58 return FileUtils.set_contents (filename, keyfile.to_data (null));
59 } catch (GLib.Error error) {
60 warning ("Failed to save database: %s", error.message);
61
62 return false;
63 }
64 }
65
66 private bool get_boolean (string group, string key) {
67 try {
68 return keyfile.get_boolean (group, key);
69 } catch (GLib.KeyFileError error) {
70 debug ("Key %s not found in group %s: %s", key, group, error.message);
71
72 return false; // DEFAULT_VALUE
73 }
74 }
75
76 private string get_string (string group, string key) {
77 try {
78 return keyfile.get_string (group, key);
79 } catch (GLib.KeyFileError error) {
80 debug ("Key %s not found in group %s: %s", key, group, error.message);
81
82 return ""; // DEFAULT_VALUE
83 }
84 }
85
86 private void load_properties (Connection connection) {
87 try {
88 foreach (var property_name in keyfile.get_keys (connection.uuid))
89 load_property (connection, property_name);
90 } catch (GLib.Error error) {
91 warning ("Failed to load connection properties: %s".printf (error.message));
92 }
93 }
94
95 private void load_property (Connection connection, string property_name) {
96 var property = connection.get_class ().find_property (property_name);
97 if (property == null)
98 return;
99
100 var value = GLib.Value (property.value_type);
101 try {
102 if (value.type () == typeof (string))
103 value = get_string (connection.uuid, property_name);
104 if (value.type () == typeof (bool))
105 value = get_boolean (connection.uuid, property_name);
106 if (value.type () == typeof (int))
107 value = keyfile.get_integer (connection.uuid, property_name);
108 if (value.type () == typeof (uint64))
109 value = keyfile.get_uint64 (connection.uuid, property_name);
110 if (value.type () == typeof (int64))
111 value = keyfile.get_int64 (connection.uuid, property_name);
112 } catch (GLib.Error error) {
113 }
114
115 connection.set_property (property_name, value);
116 }
117
118 public void save_property (Connection connection, string property_name) {
119 load_keyfile ();
120
121 var value = GLib.Value (connection.get_class ().find_property (property_name).value_type);
122
123 connection.get_property (property_name, ref value);
124
125 if (value.type () == typeof (string)) {
126 var vstring = (value.get_string () == null ? "" : value.get_string ());
127 keyfile.set_string (connection.uuid, property_name, vstring);
128 } else if (value.type () == typeof (bool))
129 keyfile.set_boolean (connection.uuid, property_name, value.get_boolean ());
130 else if (value.type () == typeof (int))
131 keyfile.set_integer (connection.uuid, property_name, value.get_int ());
132 else if (value.type () == typeof (uint64))
133 keyfile.set_uint64 (connection.uuid, property_name, value.get_uint64 ());
134 else if (value.type () == typeof (int64))
135 keyfile.set_int64 (connection.uuid, property_name, value.get_int64 ());
136 else
137 warning ("Unhadled property %s type, value: %s".printf (property_name,
138 value.strdup_contents ()));
139
140 save_keyfile ();
141 }
142
143 public List<Connections.Connection> get_connections () {
144 load_keyfile ();
145
146 List<Connections.Connection >? connections = new List<Connections.Connection> ();
147 foreach (var uuid in keyfile.get_groups ()) {
148 Connection? connection = get_connection (uuid);
149
150 if (connection != null)
151 connections.append (connection);
152 }
153
154 return connections;
155 }
156
157 public Connection? get_connection (string uuid) {
158 load_keyfile ();
159
160 if (!keyfile.has_group (uuid))
161 return null;
162
163 Connection? connection = null;
164 var protocol = get_string (uuid, "protocol");
165 switch (protocol) {
166 case "vnc":
167 connection = new VncConnection (uuid);
168 break;
169 case "rdp":
170 connection = new RdpConnection (uuid);
171 break;
172 #if HAS_SPICE
173 case "spice":
174 connection = new SpiceConnection (uuid);
175 break;
176 #endif
177 default:
178 debug ("Unknown protocol defined for %s", uuid);
179 break;
180 }
181
182
183 if (connection == null)
184 return null;
185
186 load_properties (connection);
187
188 return connection;
189 }
190
191 public Connection? find_connection_by_uri (string _uri) {
192 try {
193 var address = GLib.Uri.parse (_uri, UriFlags.NONE);
194 var protocol = address.get_scheme ();
195 var host = address.get_host ();
196 var port = address.get_port ();
197
198 foreach (var connection in get_connections ()) {
199 if (connection.protocol == protocol &&
200 connection.host == host &&
201 (port == -1 || connection.port == port)) {
202 return connection;
203 }
204 }
205 } catch (UriError e) {
206 warning ("Failed to parse URI: %s (%s)", _uri, e.message);
207 }
208
209 return null;
210 }
211
212 public Connection add_connection (string _uri) {
213 Connection? connection = null;
214
215 var uri = Xml.URI.parse (_uri);
216 switch (uri.scheme) {
217 case "vnc":
218 connection = new VncConnection.from_uri (_uri);
219 break;
220 case "rdp":
221 connection = new RdpConnection.from_uri (_uri);
222 break;
223 #if HAS_SPICE
224 case "spice":
225 connection = new SpiceConnection.from_uri (_uri);
226 break;
227 #endif
228 default:
229 debug ("Failed to add '%s': unknown protocol", _uri);
230 break;
231 }
232
233 connection.save ();
234
235 return connection;
236 }
237
238 public void delete_connection (Connection connection) {
239 load_keyfile ();
240 try {
241 keyfile.remove_group (connection.uuid);
242 } catch (GLib.Error error) {
243 warning ("Failed to delete '%s': %s", connection.uuid, error.message);
244 }
245
246 save_keyfile ();
247 }
248 }
249 }