2 * Copyright (C) 2010 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include <libgnomevfs/gnome-vfs.h>
19 #include "strongswan-connections.h"
21 /* connections are stored in ~/.config/strongswan/connections */
22 #define CONFIG_DIR_NAME "strongswan"
23 #define CONFIG_FILE_NAME "connections"
25 #define STRONGSWAN_CONNECTIONS_GET_PRIVATE(object) \
26 (G_TYPE_INSTANCE_GET_PRIVATE ((object), STRONGSWAN_TYPE_CONNECTIONS, StrongswanConnectionsPrivate))
28 struct _StrongswanConnectionsPrivate
32 GnomeVFSMonitorHandle
*monitor
;
33 GHashTable
*connections
;
37 G_DEFINE_TYPE (StrongswanConnections
, strongswan_connections
, G_TYPE_OBJECT
);
40 strongswan_connections_load_connections (StrongswanConnections
*connections
)
42 StrongswanConnectionsPrivate
*priv
= connections
->priv
;
48 g_hash_table_remove_all (priv
->connections
);
49 groups
= g_key_file_get_groups (priv
->key_file
, NULL
);
50 for (i
= 0; groups
[i
]; i
++)
52 StrongswanConnection
*conn
;
53 conn
= strongswan_connection_new_from_key_file(priv
->key_file
,
57 g_hash_table_insert (priv
->connections
,
64 gtk_list_store_clear (GTK_LIST_STORE (priv
->model
));
65 g_hash_table_iter_init (&iter
, priv
->connections
);
66 while (g_hash_table_iter_next (&iter
, &key
, &value
))
68 gtk_list_store_insert_with_values (GTK_LIST_STORE (priv
->model
),
77 strongswan_connections_load_config (StrongswanConnections
*connections
)
79 StrongswanConnectionsPrivate
*priv
= connections
->priv
;
84 g_key_file_free (priv
->key_file
);
87 priv
->key_file
= g_key_file_new ();
88 if (!g_key_file_load_from_file (priv
->key_file
,
90 G_KEY_FILE_KEEP_COMMENTS
,
93 if (g_error_matches (error
,
95 G_KEY_FILE_ERROR_PARSE
))
97 g_debug ("Failed to parse config file '%s', treated as empty: %s",
98 priv
->path
, error
->message
);
103 g_debug ("Could not read config file '%s': %s",
104 priv
->path
, error
->message
);
105 g_error_free (error
);
109 strongswan_connections_load_connections (connections
);
113 strongswan_connections_file_changed (GnomeVFSMonitorHandle
*handle
,
114 const gchar
*monitor_uri
,
115 const gchar
*info_uri
,
116 GnomeVFSMonitorEventType event_type
,
117 StrongswanConnections
*connections
)
119 strongswan_connections_load_config (connections
);
124 strongswan_connections_init (StrongswanConnections
*connections
)
126 StrongswanConnectionsPrivate
*priv
;
127 connections
->priv
= STRONGSWAN_CONNECTIONS_GET_PRIVATE (connections
);
128 priv
= connections
->priv
;
130 priv
->path
= g_build_filename (g_get_user_config_dir (),
135 priv
->connections
= g_hash_table_new_full (g_str_hash
,
140 priv
->model
= GTK_TREE_MODEL (gtk_list_store_new (2,
143 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv
->model
),
149 strongswan_connections_constructed (GObject
*object
)
151 StrongswanConnectionsPrivate
*priv
;
152 GnomeVFSResult result
;
154 if (G_OBJECT_CLASS (strongswan_connections_parent_class
)->constructed
)
156 G_OBJECT_CLASS (strongswan_connections_parent_class
)->constructed (object
);
159 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
160 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
162 result
= gnome_vfs_monitor_add (&priv
->monitor
,
164 GNOME_VFS_MONITOR_FILE
,
165 (GnomeVFSMonitorCallback
) strongswan_connections_file_changed
,
167 if (result
!= GNOME_VFS_OK
)
169 g_warning ("Could not monitor '%s': %s",
171 gnome_vfs_result_to_string (result
));
174 strongswan_connections_load_config (STRONGSWAN_CONNECTIONS (object
));
178 strongswan_connections_dispose (GObject
*object
)
180 StrongswanConnectionsPrivate
*priv
;
182 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
183 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
187 priv
->model
= (g_object_unref (priv
->model
), NULL
);
190 G_OBJECT_CLASS (strongswan_connections_parent_class
)->dispose (object
);
194 strongswan_connections_finalize (GObject
*object
)
196 StrongswanConnectionsPrivate
*priv
;
198 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
199 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
201 priv
->path
= (g_free (priv
->path
), NULL
);
202 priv
->monitor
= (gnome_vfs_monitor_cancel (priv
->monitor
), NULL
);
203 priv
->key_file
= (g_key_file_free (priv
->key_file
), NULL
);
204 priv
->connections
= (g_hash_table_destroy (priv
->connections
), NULL
);
206 G_OBJECT_CLASS (strongswan_connections_parent_class
)->finalize (object
);
210 strongswan_connections_class_init (StrongswanConnectionsClass
*klass
)
212 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
214 object_class
->constructed
= strongswan_connections_constructed
;
215 object_class
->dispose
= strongswan_connections_dispose
;
216 object_class
->finalize
= strongswan_connections_finalize
;
218 g_type_class_add_private (klass
, sizeof (StrongswanConnectionsPrivate
));
222 strongswan_connections_get_model (StrongswanConnections
*self
)
224 StrongswanConnectionsPrivate
*priv
= self
->priv
;
225 return g_object_ref (priv
->model
);
229 strongswan_connections_setup_column_renderers (StrongswanConnections
*self
,
230 GtkCellLayout
*layout
)
232 GtkCellRenderer
*renderer
;
233 renderer
= gtk_cell_renderer_text_new ();
234 gtk_cell_layout_pack_start (layout
,
237 gtk_cell_layout_add_attribute (layout
,
242 StrongswanConnections
*
243 strongswan_connections_new (void)
245 StrongswanConnections
*connections
;
246 connections
= g_object_new (STRONGSWAN_TYPE_CONNECTIONS
,
251 StrongswanConnection
*
252 strongswan_connections_get_connection (StrongswanConnections
*self
,
255 StrongswanConnectionsPrivate
*priv
= self
->priv
;
256 g_return_val_if_fail (name
!= NULL
, NULL
);
257 return g_hash_table_lookup (priv
->connections
, name
);
261 strongswan_connections_save_connections (StrongswanConnections
*self
)
263 StrongswanConnectionsPrivate
*priv
= self
->priv
;
266 GError
*error
= NULL
;
268 data
= g_key_file_to_data (priv
->key_file
, &size
, NULL
);
269 if (!g_file_set_contents (priv
->path
, data
, size
, &error
))
271 g_warning ("Failed to save connections to '%s': %s",
272 priv
->path
, error
->message
);
273 g_error_free (error
);
279 strongswan_connections_save_connection (StrongswanConnections
*self
,
280 StrongswanConnection
*conn
)
282 StrongswanConnectionsPrivate
*priv
= self
->priv
;
284 strongswan_connection_save_to_key_file (priv
->key_file
, conn
);
286 strongswan_connections_save_connections (self
);
290 strongswan_connections_remove_connection (StrongswanConnections
*self
,
293 StrongswanConnectionsPrivate
*priv
= self
->priv
;
295 g_key_file_remove_group (priv
->key_file
, name
, NULL
);
297 strongswan_connections_save_connections (self
);