70abfaddbc82ae08a7a6ec04d550429ce026b007
[strongswan.git] / src / charon / plugins / dbus / nm_applet_gui.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <glade/glade.h>
19
20 #define NM_VPN_API_SUBJECT_TO_CHANGE
21 #include <nm-vpn-ui-interface.h>
22
23
24 typedef struct private_nm_applet_gui_t private_nm_applet_gui_t;
25
26 /**
27 * Private data of an nm_applet_gui_t object.
28 */
29 struct private_nm_applet_gui_t {
30
31 /**
32 * Implements NetworkManagerVpnUI interface.
33 */
34 NetworkManagerVpnUI public;
35
36 /**
37 * callback registered by NM to update validity
38 */
39 NetworkManagerVpnUIDialogValidityCallback callback;
40
41 /**
42 * loaded Glade XML interface description
43 */
44 GladeXML *xml;
45
46 /**
47 * root widget to return to druid
48 */
49 GtkWidget *widget;
50
51 /**
52 * name of the connection
53 */
54 GtkEntry *name;
55
56 /**
57 * gateway address
58 */
59 GtkEntry *gateway;
60
61 /**
62 * username
63 */
64 GtkEntry *user;
65 };
66
67 static const char *get_display_name(private_nm_applet_gui_t *this)
68 {
69 return "strongSwan (IPsec/IKEv2)";
70 }
71
72 static const char *get_service_name(private_nm_applet_gui_t *this)
73 {
74 return "org.freedesktop.NetworkManager.strongswan";
75 }
76
77 static GtkWidget *get_widget(private_nm_applet_gui_t *this, GSList *properties,
78 GSList *routes, const char *connection_name)
79 {
80 GSList *i;
81
82 gtk_entry_set_text(this->name, "");
83 gtk_entry_set_text(this->gateway, "");
84 gtk_entry_set_text(this->user, "");
85
86 if (connection_name)
87 {
88 gtk_entry_set_text(this->name, connection_name);
89 }
90
91 while (properties)
92 {
93 const char *key;
94 const char *value;
95
96 key = properties->data;
97 properties = g_slist_next(properties);
98 if (properties)
99 {
100 value = properties->data;
101 if (strcmp(key, "gateway") == 0)
102 {
103 gtk_entry_set_text(this->gateway, value);
104 }
105 if (strcmp(key, "user") == 0)
106 {
107 gtk_entry_set_text(this->user, value);
108 }
109 properties = g_slist_next(properties);
110 }
111 }
112 return this->widget;
113 }
114
115 static GSList *get_properties(private_nm_applet_gui_t *this)
116 {
117 GSList *props = NULL;
118
119 props = g_slist_append(props, g_strdup("gateway"));
120 props = g_slist_append(props, g_strdup(gtk_entry_get_text(this->gateway)));
121 props = g_slist_append(props, g_strdup("user"));
122 props = g_slist_append(props, g_strdup(gtk_entry_get_text(this->user)));
123
124 return props;
125 }
126
127 static GSList *get_routes(private_nm_applet_gui_t *this)
128 {
129 return NULL;
130 }
131
132 static char *get_connection_name(private_nm_applet_gui_t *this)
133 {
134 const char *name;
135
136 name = gtk_entry_get_text(this->name);
137 if (name != NULL)
138 {
139 return g_strdup(name);
140 }
141 return NULL;
142 }
143
144 static gboolean is_valid(private_nm_applet_gui_t *this)
145 {
146 return TRUE;
147 }
148
149 static void set_validity_changed_callback(private_nm_applet_gui_t *this,
150 NetworkManagerVpnUIDialogValidityCallback callback,
151 gpointer user_data)
152 {
153 this->callback = callback;
154 }
155
156 static void get_confirmation_details(private_nm_applet_gui_t *this, gchar **retval)
157 {
158 *retval = g_strdup_printf("connection %s\n", gtk_entry_get_text(this->name));
159 }
160
161 static gboolean can_export(private_nm_applet_gui_t *this)
162 {
163 return FALSE;
164 }
165
166 static gboolean import_file (private_nm_applet_gui_t *this, const char *path)
167 {
168 return FALSE;
169 }
170
171 static gboolean export(private_nm_applet_gui_t *this, GSList *properties,
172 GSList *routes, const char *connection_name)
173 {
174 return FALSE;
175 }
176
177 NetworkManagerVpnUI* nm_vpn_properties_factory(void)
178 {
179 private_nm_applet_gui_t *this = g_new0(private_nm_applet_gui_t, 1);
180
181 this->public.get_display_name = (const char *(*)(NetworkManagerVpnUI *))get_display_name;
182 this->public.get_service_name = (const char *(*) (NetworkManagerVpnUI *))get_service_name;
183 this->public.get_widget = (GtkWidget *(*) (NetworkManagerVpnUI *self, GSList *, GSList *, const char *))get_widget;
184 this->public.get_connection_name = (char *(*) (NetworkManagerVpnUI *))get_connection_name;
185 this->public.get_properties = (GSList *(*) (NetworkManagerVpnUI *))get_properties;
186 this->public.get_routes = (GSList *(*) (NetworkManagerVpnUI *))get_routes;
187 this->public.set_validity_changed_callback = (void (*) (NetworkManagerVpnUI *, NetworkManagerVpnUIDialogValidityCallback, gpointer))set_validity_changed_callback;
188 this->public.is_valid = (gboolean (*) (NetworkManagerVpnUI *))is_valid;
189 this->public.get_confirmation_details = (void (*)(NetworkManagerVpnUI *, gchar **))get_confirmation_details;
190 this->public.can_export = (gboolean (*) (NetworkManagerVpnUI *))can_export;
191 this->public.import_file = (gboolean (*) (NetworkManagerVpnUI *, const char *))import_file;
192 this->public.export = (gboolean (*) (NetworkManagerVpnUI *, GSList *, GSList *, const char *))export;
193 this->public.data = NULL;
194
195 this->callback = NULL;
196 this->xml = glade_xml_new("/home/martin/strongswan/trunk/src/charon/plugins/dbus/nm_applet_gui.xml", NULL, NULL);
197
198 if (this->xml != NULL)
199 {
200 this->widget = glade_xml_get_widget(this->xml, "main");
201 this->name = GTK_ENTRY(glade_xml_get_widget(this->xml, "name"));
202 this->gateway = GTK_ENTRY(glade_xml_get_widget(this->xml, "gateway"));
203 this->user = GTK_ENTRY(glade_xml_get_widget(this->xml, "user"));
204
205 if (this->widget && this->name && this->gateway && this->user)
206 {
207 return &this->public;
208 }
209 }
210 g_free(this);
211 return NULL;
212 }