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 #ifndef USE_DYNAMIC_TYPES
38 G_DEFINE_TYPE (StrongswanConnections
, strongswan_connections
, G_TYPE_OBJECT
);
40 G_DEFINE_DYNAMIC_TYPE (StrongswanConnections
, strongswan_connections
, G_TYPE_OBJECT
);
41 void strongswan_connections_register (GTypeModule
*type_module
)
43 strongswan_connections_register_type (type_module
);
48 strongswan_connections_load_connections (StrongswanConnections
*connections
)
50 StrongswanConnectionsPrivate
*priv
= connections
->priv
;
56 g_hash_table_remove_all (priv
->connections
);
57 groups
= g_key_file_get_groups (priv
->key_file
, NULL
);
58 for (i
= 0; groups
[i
]; i
++)
60 StrongswanConnection
*conn
;
61 conn
= strongswan_connection_new_from_key_file(priv
->key_file
,
65 g_hash_table_insert (priv
->connections
,
72 gtk_list_store_clear (GTK_LIST_STORE (priv
->model
));
73 g_hash_table_iter_init (&iter
, priv
->connections
);
74 while (g_hash_table_iter_next (&iter
, &key
, &value
))
76 gtk_list_store_insert_with_values (GTK_LIST_STORE (priv
->model
),
85 strongswan_connections_load_config (StrongswanConnections
*connections
)
87 StrongswanConnectionsPrivate
*priv
= connections
->priv
;
92 g_key_file_free (priv
->key_file
);
95 priv
->key_file
= g_key_file_new ();
96 if (!g_key_file_load_from_file (priv
->key_file
,
98 G_KEY_FILE_KEEP_COMMENTS
,
101 if (g_error_matches (error
,
103 G_KEY_FILE_ERROR_PARSE
))
105 g_debug ("Failed to parse config file '%s', treated as empty: %s",
106 priv
->path
, error
->message
);
107 g_error_free (error
);
111 g_debug ("Could not read config file '%s': %s",
112 priv
->path
, error
->message
);
113 g_error_free (error
);
117 strongswan_connections_load_connections (connections
);
121 strongswan_connections_file_changed (GnomeVFSMonitorHandle
*handle
,
122 const gchar
*monitor_uri
,
123 const gchar
*info_uri
,
124 GnomeVFSMonitorEventType event_type
,
125 StrongswanConnections
*connections
)
127 strongswan_connections_load_config (connections
);
132 strongswan_connections_init (StrongswanConnections
*connections
)
134 StrongswanConnectionsPrivate
*priv
;
135 connections
->priv
= STRONGSWAN_CONNECTIONS_GET_PRIVATE (connections
);
136 priv
= connections
->priv
;
138 priv
->path
= g_build_filename (g_get_user_config_dir (),
142 /* ensure that the directory exists */
143 gchar
*dir
= g_path_get_dirname (priv
->path
);
144 g_mkdir_with_parents (dir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
147 priv
->connections
= g_hash_table_new_full (g_str_hash
,
152 priv
->model
= GTK_TREE_MODEL (gtk_list_store_new (2,
155 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv
->model
),
161 strongswan_connections_constructed (GObject
*object
)
163 StrongswanConnectionsPrivate
*priv
;
164 GnomeVFSResult result
;
166 if (G_OBJECT_CLASS (strongswan_connections_parent_class
)->constructed
)
168 G_OBJECT_CLASS (strongswan_connections_parent_class
)->constructed (object
);
171 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
172 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
174 result
= gnome_vfs_monitor_add (&priv
->monitor
,
176 GNOME_VFS_MONITOR_FILE
,
177 (GnomeVFSMonitorCallback
) strongswan_connections_file_changed
,
179 if (result
!= GNOME_VFS_OK
)
181 g_warning ("Could not monitor '%s': %s",
183 gnome_vfs_result_to_string (result
));
186 strongswan_connections_load_config (STRONGSWAN_CONNECTIONS (object
));
190 strongswan_connections_dispose (GObject
*object
)
192 StrongswanConnectionsPrivate
*priv
;
194 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
195 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
199 priv
->model
= (g_object_unref (priv
->model
), NULL
);
202 G_OBJECT_CLASS (strongswan_connections_parent_class
)->dispose (object
);
206 strongswan_connections_finalize (GObject
*object
)
208 StrongswanConnectionsPrivate
*priv
;
210 g_return_if_fail (STRONGSWAN_IS_CONNECTIONS (object
));
211 priv
= STRONGSWAN_CONNECTIONS (object
)->priv
;
213 priv
->path
= (g_free (priv
->path
), NULL
);
214 priv
->monitor
= (gnome_vfs_monitor_cancel (priv
->monitor
), NULL
);
215 priv
->key_file
= (g_key_file_free (priv
->key_file
), NULL
);
216 priv
->connections
= (g_hash_table_destroy (priv
->connections
), NULL
);
218 G_OBJECT_CLASS (strongswan_connections_parent_class
)->finalize (object
);
222 strongswan_connections_class_init (StrongswanConnectionsClass
*klass
)
224 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
226 object_class
->constructed
= strongswan_connections_constructed
;
227 object_class
->dispose
= strongswan_connections_dispose
;
228 object_class
->finalize
= strongswan_connections_finalize
;
230 g_type_class_add_private (klass
, sizeof (StrongswanConnectionsPrivate
));
233 #ifdef USE_DYNAMIC_TYPES
235 strongswan_connections_class_finalize (StrongswanConnectionsClass
*klass
)
241 strongswan_connections_get_model (StrongswanConnections
*self
)
243 StrongswanConnectionsPrivate
*priv
= self
->priv
;
244 return g_object_ref (priv
->model
);
248 strongswan_connections_setup_column_renderers (StrongswanConnections
*self
,
249 GtkCellLayout
*layout
)
251 GtkCellRenderer
*renderer
;
252 renderer
= gtk_cell_renderer_text_new ();
253 gtk_cell_layout_pack_start (layout
,
256 gtk_cell_layout_add_attribute (layout
,
261 StrongswanConnections
*
262 strongswan_connections_new (void)
264 StrongswanConnections
*connections
;
265 connections
= g_object_new (STRONGSWAN_TYPE_CONNECTIONS
,
270 StrongswanConnection
*
271 strongswan_connections_get_connection (StrongswanConnections
*self
,
274 StrongswanConnectionsPrivate
*priv
= self
->priv
;
275 g_return_val_if_fail (name
!= NULL
, NULL
);
276 return g_hash_table_lookup (priv
->connections
, name
);
280 strongswan_connections_save_connections (StrongswanConnections
*self
)
282 StrongswanConnectionsPrivate
*priv
= self
->priv
;
285 GError
*error
= NULL
;
287 data
= g_key_file_to_data (priv
->key_file
, &size
, NULL
);
288 if (!g_file_set_contents (priv
->path
, data
, size
, &error
))
290 g_warning ("Failed to save connections to '%s': %s",
291 priv
->path
, error
->message
);
292 g_error_free (error
);
298 strongswan_connections_save_connection (StrongswanConnections
*self
,
299 StrongswanConnection
*conn
)
301 StrongswanConnectionsPrivate
*priv
= self
->priv
;
303 strongswan_connection_save_to_key_file (priv
->key_file
, conn
);
305 strongswan_connections_save_connections (self
);
309 strongswan_connections_remove_connection (StrongswanConnections
*self
,
312 StrongswanConnectionsPrivate
*priv
= self
->priv
;
314 g_key_file_remove_group (priv
->key_file
, name
, NULL
);
316 strongswan_connections_save_connections (self
);