58049ebdc96b2cd1412a4a6f68c78dacb3ade122
[dhcpcd-ui] / src / dhcpcd-gtk / menu.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "dhcpcd-gtk.h"
29
30 static const char *copyright = "Copyright (c) 2009-2014 Roy Marples";
31 static const char *authors[] = { "Roy Marples <roy@marples.name>", NULL };
32
33 static GtkStatusIcon *sicon;
34 static GtkWidget *menu;
35 static bool ifmenu;
36
37 static void
38 on_pref(_unused GObject *o, gpointer data)
39 {
40
41         prefs_show((DHCPCD_CONNECTION *)data);
42 }
43
44 static void
45 on_quit(void)
46 {
47
48         wpa_abort();
49         gtk_main_quit();
50 }
51
52 #if GTK_MAJOR_VERSION == 2
53 static void
54 url_show(GtkAboutDialog *dialog, const char *url)
55 {
56         GdkScreen *screen;
57
58         screen = gtk_widget_get_screen(GTK_WIDGET(dialog));
59         gtk_show_uri(screen, url, GDK_CURRENT_TIME, NULL);
60 }
61
62 static void
63 email_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
64 {
65         char *address;
66
67         address = g_strdup_printf("mailto:%s", url);
68         url_show(dialog, address);
69         g_free(address);
70 }
71
72
73 static void
74 url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
75 {
76         url_show(dialog, url);
77 }
78 #endif
79
80 static void
81 ssid_hook(GtkMenuItem *item, _unused gpointer data)
82 {
83         DHCPCD_WI_SCAN *scan;
84         WI_SCAN *wi;
85
86         scan = g_object_get_data(G_OBJECT(item), "dhcpcd_wi_scan");
87         wi = wi_scan_find(scan);
88         if (wi) {
89                 DHCPCD_CONNECTION *con;
90
91                 con = dhcpcd_if_connection(wi->interface);
92                 if (con) {
93                         DHCPCD_WPA *wpa;
94
95                         wpa = dhcpcd_wpa_find(con, wi->interface->ifname);
96                         if (wpa)
97                                 wpa_configure(wpa, scan);
98                 }
99         }
100 }
101
102 static void
103 on_about(_unused GtkMenuItem *item)
104 {
105
106         gtk_window_set_default_icon_name("network-transmit-receive");
107 #if GTK_MAJOR_VERSION == 2
108         gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
109         gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
110 #endif
111         gtk_show_about_dialog(NULL,
112             "version", VERSION,
113             "copyright", copyright,
114             "website-label", "dhcpcd Website",
115             "website", "http://roy.marples.name/projects/dhcpcd",
116             "authors", authors,
117             "logo-icon-name", "network-transmit-receive",
118             "comments", "Part of the dhcpcd project",
119             NULL);
120 }
121
122 static void
123 update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
124 {
125         const char *icon;
126         double perc;
127
128         m->scan = scan;
129
130         g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
131         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(m->menu),
132                 (wi->interface->up &&
133                 g_strcmp0(wi->interface->ssid, scan->ssid)));
134
135         gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
136         if (scan->flags[0] == '\0')
137                 icon = "network-wireless";
138         else
139                 icon = "network-wireless-encrypted";
140         m->icon = gtk_image_new_from_icon_name(icon,
141             GTK_ICON_SIZE_MENU);
142
143         perc = scan->strength.value / 100.0;
144         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(m->bar), perc);
145
146         if (scan->flags[0] == '\0')
147                 gtk_widget_set_tooltip_text(m->menu, scan->bssid);
148         else {
149                 char *tip = g_strconcat(scan->bssid, " ", scan->flags, NULL);
150                 gtk_widget_set_tooltip_text(m->menu, tip);
151                 g_free(tip);
152         }
153
154         g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
155 }
156
157 static WI_MENU *
158 create_menu(GtkWidget *m, WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
159 {
160         WI_MENU *wim;
161         GtkWidget *box;
162         double perc;
163         const char *icon;
164         char *tip;
165
166         wim = g_malloc(sizeof(*wim));
167         wim->scan = scan;
168         wim->menu = gtk_check_menu_item_new();
169         gtk_check_menu_item_set_draw_as_radio(
170             GTK_CHECK_MENU_ITEM(wim->menu), true);
171         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
172         gtk_container_add(GTK_CONTAINER(wim->menu), box);
173         wim->ssid = gtk_label_new(scan->ssid);
174         gtk_box_pack_start(GTK_BOX(box), wim->ssid, TRUE, TRUE, 0);
175
176         if (wis->interface->up &&
177             g_strcmp0(scan->ssid, wis->interface->ssid) == 0)
178                 gtk_check_menu_item_set_active(
179                     GTK_CHECK_MENU_ITEM(wim->menu), true);
180
181         if (scan->flags[0] == '\0')
182                 icon = "network-wireless";
183         else
184                 icon = "network-wireless-encrypted";
185         wim->icon = gtk_image_new_from_icon_name(icon,
186             GTK_ICON_SIZE_MENU);
187
188         gtk_box_pack_start(GTK_BOX(box), wim->icon, FALSE, FALSE, 0);
189
190         wim->bar = gtk_progress_bar_new();
191         gtk_widget_set_size_request(wim->bar, 100, -1);
192         gtk_box_pack_end(GTK_BOX(box), wim->bar, FALSE, TRUE, 0);
193         perc = scan->strength.value / 100.0;
194         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(wim->bar), perc);
195
196         if (scan->flags[0] == '\0')
197                 gtk_widget_set_tooltip_text(wim->menu, scan->bssid);
198         else {
199                 tip = g_strconcat(scan->bssid, " ", scan->flags, NULL);
200                 gtk_widget_set_tooltip_text(wim->menu, tip);
201                 g_free(tip);
202         }
203
204         g_signal_connect(G_OBJECT(wim->menu), "toggled",
205             G_CALLBACK(ssid_hook), NULL);
206         g_object_set_data(G_OBJECT(wim->menu), "dhcpcd_wi_scan", scan);
207         gtk_menu_shell_append(GTK_MENU_SHELL(m), wim->menu);
208
209         return wim;
210 }
211
212 void
213 menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
214 {
215         WI_MENU *wim, *win;
216         DHCPCD_WI_SCAN *s;
217         bool found;
218         int adjust = 0;
219
220         if (menu == NULL) {
221                 dhcpcd_wi_scans_free(wi->scans);
222                 wi->scans = scans;
223                 return;
224         }
225
226         TAILQ_FOREACH_SAFE(wim, &wi->menus, next, win) {
227                 found = false;
228                 for (s = scans; s; s = s->next) {
229                         if (memcmp(wim->scan->bssid, s->bssid,
230                             sizeof(s->bssid)) == 0)
231                         {
232                                 found = true;
233                                 update_item(wi, wim, s);
234                         }
235                 }
236                 if (!found) {
237                         TAILQ_REMOVE(&wi->menus, wim, next);
238                         gtk_widget_destroy(wim->menu);
239                         g_free(wim);
240                         adjust--;
241                 }
242         }
243
244         for (s = scans; s; s = s->next) {
245                 found = false;
246                 TAILQ_FOREACH(wim, &wi->menus, next) {
247                         if (memcmp(wim->scan->bssid, s->bssid,
248                             sizeof(s->bssid)) == 0)
249                         {
250                                 found = true;
251                                 break;
252                         }
253                 }
254                 if (!found) {
255                         wim = create_menu(wi->ifmenu, wi, s);
256                         TAILQ_INSERT_TAIL(&wi->menus, wim, next);
257                         gtk_widget_show_all(wim->menu);
258                         adjust++;
259                 }
260         }
261
262         dhcpcd_wi_scans_free(wi->scans);
263         wi->scans = scans;
264
265         if (adjust && gtk_widget_get_visible(wi->ifmenu))
266                 gtk_menu_reposition(GTK_MENU(wi->ifmenu));
267 }
268
269
270 static GtkWidget *
271 add_scans(WI_SCAN *wi)
272 {
273         GtkWidget *m;
274         DHCPCD_WI_SCAN *wis;
275         WI_MENU *wim;
276
277         if (wi->scans == NULL)
278                 return NULL;
279
280         m = gtk_menu_new();
281         for (wis = wi->scans; wis; wis = wis->next) {
282                 wim = create_menu(m, wi, wis);
283                 TAILQ_INSERT_TAIL(&wi->menus, wim, next);
284         }
285
286         return m;
287 }
288
289 void
290 menu_abort(void)
291 {
292         WI_SCAN *wis;
293         WI_MENU *wim;
294
295         TAILQ_FOREACH(wis, &wi_scans, next) {
296                 wis->ifmenu = NULL;
297                 while ((wim = TAILQ_FIRST(&wis->menus))) {
298                         TAILQ_REMOVE(&wis->menus, wim, next);
299                         g_free(wim);
300                 }
301         }
302
303         if (menu != NULL) {
304                 gtk_widget_destroy(menu);
305                 g_object_ref_sink(menu);
306                 g_object_unref(menu);
307                 menu = NULL;
308         }
309 }
310
311 static void
312 on_activate(GtkStatusIcon *icon)
313 {
314         WI_SCAN *w, *l;
315         GtkWidget *item, *image;
316
317         sicon = icon;
318         notify_close();
319         prefs_abort();
320         menu_abort();
321
322         if ((w = TAILQ_FIRST(&wi_scans)) == NULL)
323                 return;
324
325         if ((l = TAILQ_LAST(&wi_scans, wi_scan_head)) && l != w) {
326                 menu = gtk_menu_new();
327                 ifmenu = true;
328                 TAILQ_FOREACH(w, &wi_scans, next) {
329                         item = gtk_image_menu_item_new_with_label(
330                                 w->interface->ifname);
331                         image = gtk_image_new_from_icon_name(
332                                 "network-wireless", GTK_ICON_SIZE_MENU);
333                         gtk_image_menu_item_set_image(
334                                 GTK_IMAGE_MENU_ITEM(item), image);
335                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
336                         w->ifmenu = add_scans(w);
337                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
338                             w->ifmenu);
339                 }
340         } else {
341                 ifmenu = false;
342                 w->ifmenu = menu = add_scans(w);
343         }
344
345         gtk_widget_show_all(GTK_WIDGET(menu));
346         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
347             gtk_status_icon_position_menu, icon,
348             1, gtk_get_current_event_time());
349 }
350
351 static void
352 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
353 {
354         DHCPCD_CONNECTION *con;
355         GtkMenu *mnu;
356         GtkWidget *item, *image;
357
358         notify_close();
359
360         con = (DHCPCD_CONNECTION *)data;
361         mnu = (GtkMenu *)gtk_menu_new();
362
363         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
364         image = gtk_image_new_from_icon_name("preferences-system-network",
365             GTK_ICON_SIZE_MENU);
366         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
367         if (g_strcmp0(dhcpcd_status(con), "down") == 0)
368                 gtk_widget_set_sensitive(item, false);
369         else
370                 g_signal_connect(G_OBJECT(item), "activate",
371                     G_CALLBACK(on_pref), data);
372         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
373
374         item = gtk_separator_menu_item_new();
375         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
376
377         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
378         image = gtk_image_new_from_icon_name("help-about",
379             GTK_ICON_SIZE_MENU);
380         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
381         g_signal_connect(G_OBJECT(item), "activate",
382             G_CALLBACK(on_about), icon);
383         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
384
385         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
386         image = gtk_image_new_from_icon_name("application-exit",
387             GTK_ICON_SIZE_MENU);
388         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
389         g_signal_connect(G_OBJECT(item), "activate",
390             G_CALLBACK(on_quit), icon);
391         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
392
393         gtk_widget_show_all(GTK_WIDGET(mnu));
394         gtk_menu_popup(GTK_MENU(mnu), NULL, NULL,
395             gtk_status_icon_position_menu, icon, button, atime);
396         if (button == 0)
397                 gtk_menu_shell_select_first(GTK_MENU_SHELL(mnu), FALSE);
398 }
399
400 void
401 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
402 {
403
404         g_signal_connect(G_OBJECT(icon), "activate",
405             G_CALLBACK(on_activate), con);
406         g_signal_connect(G_OBJECT(icon), "popup_menu",
407             G_CALLBACK(on_popup), con);
408 }
409
410
411 #if GTK_MAJOR_VERSION == 2
412 GtkWidget *
413 gtk_box_new(GtkOrientation o, gint s)
414 {
415
416         if (o == GTK_ORIENTATION_HORIZONTAL)
417                 return gtk_hbox_new(false, s);
418         else
419                 return gtk_vbox_new(false, s);
420 }
421 #endif