Dialog to initiate or terminate connections added.
[strongswan.git] / src / frontends / maemo / src / strongswan-status.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
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
16 #include <hildon/hildon.h>
17
18 #include "strongswan-status.h"
19 #include "strongswan-connections.h"
20
21 #define STRONGSWAN_STATUS_GET_PRIVATE(object) \
22 (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
23 STRONGSWAN_TYPE_STATUS, \
24 StrongswanStatusPrivate))
25
26 #define ICON_SIZE_STATUS 18
27 #define ICON_SIZE_BUTTON 48
28
29 struct _StrongswanStatusPrivate
30 {
31 struct {
32 GdkPixbuf *status_open;
33 GdkPixbuf *status_close;
34 GdkPixbuf *button_open;
35 GdkPixbuf *button_close;
36 } icons;
37
38 GtkWidget *dialog;
39 GtkWidget *button;
40 GtkWidget *image;
41 GtkWidget *selector;
42 GtkWidget *box;
43
44 StrongswanConnections *conns;
45
46 gchar *current;
47 };
48
49 HD_DEFINE_PLUGIN_MODULE_EXTENDED (StrongswanStatus, strongswan_status, \
50 HD_TYPE_STATUS_MENU_ITEM, {}, { \
51 strongswan_connection_register (G_TYPE_MODULE (plugin)); \
52 strongswan_connections_register (G_TYPE_MODULE (plugin)); }, {});
53
54 static void
55 dialog_response (GtkDialog *dialog, gint response_id, StrongswanStatus *plugin)
56 {
57 StrongswanStatusPrivate *priv = plugin->priv;
58 g_object_unref (priv->dialog);
59 priv->dialog = NULL;
60 }
61
62 static void
63 connect_clicked (HildonButton *button, StrongswanStatus *plugin)
64 {
65 StrongswanStatusPrivate *priv = plugin->priv;
66 gtk_dialog_response (GTK_DIALOG (priv->dialog), GTK_RESPONSE_OK);
67 }
68
69 static void
70 disconnect_clicked (HildonButton *button, StrongswanStatus *plugin)
71 {
72 StrongswanStatusPrivate *priv = plugin->priv;
73 gtk_dialog_response (GTK_DIALOG (priv->dialog), GTK_RESPONSE_OK);
74 }
75
76 static void
77 button_clicked (HildonButton *button, StrongswanStatus *plugin)
78 {
79 StrongswanStatusPrivate *priv = plugin->priv;
80
81 priv->dialog = gtk_dialog_new ();
82 gtk_window_set_title (GTK_WINDOW (priv->dialog), "strongSwan VPN");
83 g_signal_connect (priv->dialog, "response",
84 G_CALLBACK (dialog_response), plugin);
85
86 GtkWidget *vbox = GTK_DIALOG (priv->dialog)->vbox;
87
88 if (priv->current)
89 {
90 /* connected case */
91 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
92 priv->box = hbox;
93 GtkWidget *button = hildon_button_new_with_text (
94 HILDON_SIZE_FINGER_HEIGHT |
95 HILDON_SIZE_AUTO_WIDTH,
96 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
97 "Disconnect", priv->current);
98 hildon_button_set_style (HILDON_BUTTON (button),
99 HILDON_BUTTON_STYLE_PICKER);
100 g_signal_connect (button, "clicked", G_CALLBACK (disconnect_clicked),
101 plugin);
102 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
103 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
104 }
105 else
106 {
107 /* unconnected case */
108 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
109 priv->box = hbox;
110 GtkWidget *button = hildon_picker_button_new (
111 HILDON_SIZE_FINGER_HEIGHT |
112 HILDON_SIZE_AUTO_WIDTH,
113 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
114 hildon_button_set_title (HILDON_BUTTON (button), "Connection:");
115 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
116
117 GtkWidget *selector = hildon_touch_selector_new ();
118 priv->selector = selector;
119 GtkTreeModel *model = strongswan_connections_get_model (priv->conns);
120 hildon_touch_selector_append_text_column (
121 HILDON_TOUCH_SELECTOR (selector),
122 model,
123 TRUE);
124 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
125 HILDON_TOUCH_SELECTOR (selector));
126 g_object_unref (model);
127
128 button = hildon_button_new_with_text (
129 HILDON_SIZE_FINGER_HEIGHT |
130 HILDON_SIZE_AUTO_WIDTH,
131 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
132 "Connect", NULL);
133 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
134 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
135 g_signal_connect (button, "clicked", G_CALLBACK (connect_clicked),
136 plugin);
137 }
138
139 gtk_widget_show_all (priv->dialog);
140 }
141
142 static void
143 load_icons (StrongswanStatusPrivate *priv)
144 {
145 GtkIconTheme *theme = gtk_icon_theme_get_default ();
146 priv->icons.status_open = gtk_icon_theme_load_icon (theme,
147 "strongswan_lock_open",
148 ICON_SIZE_STATUS, GTK_ICON_LOOKUP_NO_SVG, NULL);
149 priv->icons.status_close = gtk_icon_theme_load_icon (theme,
150 "strongswan_lock_close",
151 ICON_SIZE_STATUS, GTK_ICON_LOOKUP_NO_SVG, NULL);
152 priv->icons.button_open = gtk_icon_theme_load_icon (theme,
153 "strongswan_lock_open",
154 ICON_SIZE_BUTTON, GTK_ICON_LOOKUP_NO_SVG, NULL);
155 priv->icons.button_close = gtk_icon_theme_load_icon (theme,
156 "strongswan_lock_close",
157 ICON_SIZE_BUTTON, GTK_ICON_LOOKUP_NO_SVG, NULL);
158 }
159
160 static void
161 strongswan_status_init (StrongswanStatus *plugin)
162 {
163 StrongswanStatusPrivate *priv = STRONGSWAN_STATUS_GET_PRIVATE (plugin);
164 plugin->priv = priv;
165
166 priv->conns = strongswan_connections_new ();
167
168 load_icons(priv);
169
170 hd_status_plugin_item_set_status_area_icon (HD_STATUS_PLUGIN_ITEM (plugin),
171 priv->icons.status_open);
172
173 GtkWidget *button = hildon_button_new_with_text (
174 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
175 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
176 "strongSwan VPN", "Not connected");
177 hildon_button_set_style (HILDON_BUTTON (button),
178 HILDON_BUTTON_STYLE_PICKER);
179 priv->button = button;
180 gtk_container_add (GTK_CONTAINER (plugin), button);
181
182 GtkWidget *image = gtk_image_new_from_pixbuf (priv->icons.button_open);
183 priv->image = image;
184 hildon_button_set_image (HILDON_BUTTON (button), image);
185
186 gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
187
188 g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), plugin);
189
190 gtk_widget_show_all (GTK_WIDGET (plugin));
191 }
192
193 static void
194 strongswan_status_dispose (GObject *object)
195 {
196 StrongswanStatusPrivate *priv = STRONGSWAN_STATUS (object)->priv;
197 if (priv->conns)
198 {
199 priv->conns = (g_object_unref (priv->conns), NULL);
200 }
201 if (priv->dialog)
202 {
203 priv->dialog = (g_object_unref (priv->dialog), NULL);
204 }
205 if (priv->icons.status_open)
206 {
207 g_object_unref (priv->icons.status_open);
208 priv->icons.status_open = NULL;
209 }
210 if (priv->icons.status_close)
211 {
212 g_object_unref (priv->icons.status_close);
213 priv->icons.status_close = NULL;
214 }
215 if (priv->icons.button_open)
216 {
217 g_object_unref (priv->icons.button_open);
218 priv->icons.button_open = NULL;
219 }
220 if (priv->icons.button_close)
221 {
222 g_object_unref (priv->icons.button_close);
223 priv->icons.button_close = NULL;
224 }
225 G_OBJECT_CLASS (strongswan_status_parent_class)->dispose (object);
226 }
227
228 static void
229 strongswan_status_finalize (GObject *object)
230 {
231 StrongswanStatusPrivate *priv = STRONGSWAN_STATUS (object)->priv;
232 priv->current = (g_free (priv->current), NULL);
233 G_OBJECT_CLASS (strongswan_status_parent_class)->finalize (object);
234 }
235
236 static void
237 strongswan_status_class_finalize (StrongswanStatusClass *klass)
238 {
239 }
240
241 static void
242 strongswan_status_class_init (StrongswanStatusClass *klass)
243 {
244 GObjectClass *object_class = G_OBJECT_CLASS (klass);
245
246 object_class->dispose = strongswan_status_dispose;
247 object_class->finalize = strongswan_status_finalize;
248
249 g_type_class_add_private (klass, sizeof (StrongswanStatusPrivate));
250 }