Disconnect when same SSID selected (#1)
[dhcpcd-ui] / src / dhcpcd-gtk / menu.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2015 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-2015 Roy Marples";
31
32 static GtkStatusIcon *sicon;
33 static GtkWidget *menu;
34 static GtkAboutDialog *about;
35 static guint bgscan_timer;
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 static void
53 ssid_hook(GtkMenuItem *item, _unused gpointer data)
54 {
55         DHCPCD_WI_SCAN *scan;
56         WI_SCAN *wi;
57
58         scan = g_object_get_data(G_OBJECT(item), "dhcpcd_wi_scan");
59         wi = wi_scan_find(scan);
60         if (wi) {
61                 DHCPCD_CONNECTION *con;
62
63                 con = dhcpcd_if_connection(wi->interface);
64                 if (con) {
65                         DHCPCD_WPA *wpa;
66
67                         wpa = dhcpcd_wpa_find(con, wi->interface->ifname);
68                         if (wpa)
69                                 wpa_configure(wpa, scan);
70                 }
71         }
72 }
73
74 static void
75 on_about(_unused GtkMenuItem *item)
76 {
77
78         if (about == NULL) {
79                 about = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
80                 gtk_about_dialog_set_version(about, VERSION);
81                 gtk_about_dialog_set_copyright(about, copyright);
82                 gtk_about_dialog_set_website_label(about, "dhcpcd Website");
83                 gtk_about_dialog_set_website(about,
84                     "http://roy.marples.name/projects/dhcpcd");
85                 gtk_about_dialog_set_logo_icon_name(about,
86                     "network-transmit-receive");
87                 gtk_about_dialog_set_comments(about,
88                     "Part of the dhcpcd project");
89         }
90         gtk_window_set_position(GTK_WINDOW(about), GTK_WIN_POS_MOUSE);
91         gtk_window_present(GTK_WINDOW(about));
92         gtk_dialog_run(GTK_DIALOG(about));
93         gtk_widget_hide(GTK_WIDGET(about));
94 }
95
96 static bool
97 is_associated(WI_SCAN *wi, DHCPCD_WI_SCAN *scan)
98 {
99
100         return dhcpcd_wi_associated(wi->interface, scan);
101 }
102
103 static bool
104 get_security_icon(unsigned int flags, const char **icon)
105 {
106         bool active;
107
108         active = true;
109         if (flags & WSF_SECURE) {
110                 if (flags & WSF_PSK)
111                         *icon = "network-wireless-encrypted";
112                 else {
113                         *icon = "network-error";        
114                         active = false;
115                 }
116         } else
117                 *icon = "dialog-warning";
118         return active;
119 }
120
121 static void
122 update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
123 {
124         const char *icon;
125         GtkWidget *sel;
126         bool active;
127 #if 0
128         char tip[256];
129 #endif
130
131         m->scan = scan;
132
133         g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
134
135         m->associated = is_associated(wi, scan);
136         if (m->associated)
137                 sel = gtk_image_new_from_icon_name("dialog-ok-apply",
138                     GTK_ICON_SIZE_MENU);
139         else
140                 sel = NULL;
141         gtk_image_menu_item_set_image(
142             GTK_IMAGE_MENU_ITEM(m->menu), sel);
143
144         if (m->associated) {
145                 gchar *lbl;
146
147                 lbl = g_markup_printf_escaped("<b>%s</b>",
148                     scan->ssid);
149                 gtk_label_set_markup(GTK_LABEL(m->ssid), lbl);
150                 g_free(lbl);
151         } else
152                 gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
153         
154         active = get_security_icon(scan->flags, &icon);
155         m->icon = gtk_image_new_from_icon_name(icon,
156             GTK_ICON_SIZE_MENU);
157         if (gtk_widget_get_sensitive(GTK_WIDGET(m->menu)) != active)
158                 gtk_widget_set_sensitive(GTK_WIDGET(m->menu), active);
159
160         icon = get_strength_icon_name(scan->strength.value);
161         m->strength = gtk_image_new_from_icon_name(icon,
162                 GTK_ICON_SIZE_MENU);
163
164 #if 0
165         dhcpcd_wi_print_tooltip(tip, sizeof(tip), scan, 0);
166         gtk_widget_set_tooltip_text(m->menu, tip);
167 #endif
168
169         g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
170 }
171
172 static WI_MENU *
173 create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
174 {
175         WI_MENU *wim;
176         GtkWidget *box;
177         const char *icon;
178
179         wim = g_malloc(sizeof(*wim));
180         wim->scan = scan;
181         wim->menu = gtk_image_menu_item_new();
182         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
183         gtk_container_add(GTK_CONTAINER(wim->menu), box);
184
185         wim->ssid = gtk_label_new(NULL);
186         gtk_misc_set_alignment(GTK_MISC(wim->ssid), 0.0, 0.5);
187         gtk_box_pack_start(GTK_BOX(box), wim->ssid, TRUE, TRUE, 0);
188
189         get_security_icon(scan->flags, &icon);
190         wim->icon = gtk_image_new_from_icon_name(icon,
191             GTK_ICON_SIZE_MENU);
192         gtk_box_pack_start(GTK_BOX(box), wim->icon, FALSE, FALSE, 0);
193
194         icon = get_strength_icon_name(scan->strength.value);
195         wim->strength = gtk_image_new_from_icon_name(icon,
196                 GTK_ICON_SIZE_MENU);
197         gtk_box_pack_start(GTK_BOX(box), wim->strength, FALSE, FALSE, 0);
198
199 #if 0
200         if (scan->wpa_flags[0] == '\0')
201                 gtk_widget_set_tooltip_text(wim->menu, scan->bssid);
202         else {
203                 char *tip;
204
205                 tip = g_strconcat(scan->bssid, " ", scan->wpa_flags, NULL);
206                 gtk_widget_set_tooltip_text(wim->menu, tip);
207                 g_free(tip);
208         }
209 #endif
210         update_item(wis, wim, scan);
211
212         g_signal_connect(G_OBJECT(wim->menu), "activate",
213             G_CALLBACK(ssid_hook), NULL);
214         g_object_set_data(G_OBJECT(wim->menu), "dhcpcd_wi_scan", scan);
215
216         return wim;
217 }
218
219 void
220 menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
221 {
222         WI_MENU *wim, *win;
223         DHCPCD_WI_SCAN *s;
224         bool found;
225         int position;
226
227         if (wi->ifmenu == NULL) {
228                 dhcpcd_wi_scans_free(wi->scans);
229                 wi->scans = scans;
230                 return;
231         }
232
233         TAILQ_FOREACH_SAFE(wim, &wi->menus, next, win) {
234                 found = false;
235                 for (s = scans; s; s = s->next) {
236                         if (strcmp(wim->scan->ssid, s->ssid) == 0) {
237                                 /* If assoication changes, we
238                                  * need to remove the item to replace it */
239                                 if (wim->associated ==
240                                     is_associated(wi, s))
241                                 {
242                                         found = true;
243                                         update_item(wi, wim, s);
244                                 }
245                                 break;
246                         }
247                 }
248                 if (!found) {
249                         TAILQ_REMOVE(&wi->menus, wim, next);
250                         gtk_widget_destroy(wim->menu);
251                         g_free(wim);
252                 }
253         }
254
255         for (s = scans; s; s = s->next) {
256                 found = false;
257                 position = 0;
258                 TAILQ_FOREACH(wim, &wi->menus, next) {
259                         if (strcmp(wim->scan->ssid, s->ssid) == 0) {
260                                 found = true;
261                                 break;
262                         }
263                         /* Assoicated scans are always first */
264                         if (!is_associated(wi, s) &&
265                             dhcpcd_wi_scan_compare(wim->scan, s) < 0)
266                                 position++;
267                 }
268                 if (!found) {
269                         wim = create_menu(wi, s);
270                         TAILQ_INSERT_TAIL(&wi->menus, wim, next);
271                         gtk_menu_shell_insert(GTK_MENU_SHELL(wi->ifmenu),
272                             wim->menu, position);
273                         gtk_widget_show_all(wim->menu);
274                 }
275         }
276
277         dhcpcd_wi_scans_free(wi->scans);
278         wi->scans = scans;
279
280         if (gtk_widget_get_visible(wi->ifmenu))
281                 gtk_menu_reposition(GTK_MENU(wi->ifmenu));
282 }
283
284 void
285 menu_remove_if(WI_SCAN *wi)
286 {
287         WI_MENU *wim;
288
289         if (wi->ifmenu == NULL)
290                 return;
291
292         if (wi->ifmenu == menu)
293                 menu = NULL;
294
295         gtk_widget_destroy(wi->ifmenu);
296         wi->ifmenu = NULL;
297         while ((wim = TAILQ_FIRST(&wi->menus))) {
298                 TAILQ_REMOVE(&wi->menus, wim, next);
299                 g_free(wim);
300         }
301
302         if (menu && gtk_widget_get_visible(menu))
303                 gtk_menu_reposition(GTK_MENU(menu));
304 }
305
306 static GtkWidget *
307 add_scans(WI_SCAN *wi)
308 {
309         GtkWidget *m;
310         DHCPCD_WI_SCAN *wis;
311         WI_MENU *wim;
312         int position;
313
314         if (wi->scans == NULL)
315                 return NULL;
316
317         m = gtk_menu_new();
318         position = 0;
319         for (wis = wi->scans; wis; wis = wis->next) {
320                 wim = create_menu(wi, wis);
321                 TAILQ_INSERT_TAIL(&wi->menus, wim, next);
322                 gtk_menu_shell_insert(GTK_MENU_SHELL(m),
323                     wim->menu, is_associated(wi, wis) ? 0 : position);
324                 position++;
325         }
326
327         return m;
328 }
329
330 void
331 menu_abort(void)
332 {
333         WI_SCAN *wis;
334         WI_MENU *wim;
335
336         if (bgscan_timer) {
337                 g_source_remove(bgscan_timer);
338                 bgscan_timer = 0;
339         }
340
341         TAILQ_FOREACH(wis, &wi_scans, next) {
342                 wis->ifmenu = NULL;
343                 while ((wim = TAILQ_FIRST(&wis->menus))) {
344                         TAILQ_REMOVE(&wis->menus, wim, next);
345                         g_free(wim);
346                 }
347         }
348
349         if (menu != NULL) {
350                 gtk_widget_destroy(menu);
351                 g_object_ref_sink(menu);
352                 g_object_unref(menu);
353                 menu = NULL;
354         }
355 }
356
357 static gboolean
358 menu_bgscan(gpointer data)
359 {
360         WI_SCAN *w;
361         DHCPCD_CONNECTION *con;
362         DHCPCD_WPA *wpa;
363
364         if (menu == NULL || !gtk_widget_get_visible(menu)) {
365                 bgscan_timer = 0;
366                 return FALSE;
367         }
368
369         con = (DHCPCD_CONNECTION *)data;
370         TAILQ_FOREACH(w, &wi_scans, next) {
371                 if (w->interface->wireless && w->interface->up) {
372                         wpa = dhcpcd_wpa_find(con, w->interface->ifname);
373                         if (wpa &&
374                             (!w->interface->up ||
375                             dhcpcd_wpa_can_background_scan(wpa)))
376                                 dhcpcd_wpa_scan(wpa);
377                 }
378         }
379
380         return TRUE;
381 }
382
383 static void
384 on_activate(GtkStatusIcon *icon)
385 {
386         WI_SCAN *w, *l;
387         GtkWidget *item, *image;
388
389         sicon = icon;
390         notify_close();
391         prefs_abort();
392         menu_abort();
393
394         if ((w = TAILQ_FIRST(&wi_scans)) == NULL)
395                 return;
396
397         if ((l = TAILQ_LAST(&wi_scans, wi_scan_head)) && l != w) {
398                 menu = gtk_menu_new();
399                 TAILQ_FOREACH(l, &wi_scans, next) {
400                         item = gtk_image_menu_item_new_with_label(
401                                 l->interface->ifname);
402                         image = gtk_image_new_from_icon_name(
403                                 "network-wireless", GTK_ICON_SIZE_MENU);
404                         gtk_image_menu_item_set_image(
405                                 GTK_IMAGE_MENU_ITEM(item), image);
406                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
407                         l->ifmenu = add_scans(l);
408                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
409                             l->ifmenu);
410                 }
411         } else {
412                 w->ifmenu = menu = add_scans(w);
413         }
414
415         if (menu) {
416                 gtk_widget_show_all(GTK_WIDGET(menu));
417                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
418                     gtk_status_icon_position_menu, icon,
419                     1, gtk_get_current_event_time());
420
421                 bgscan_timer = g_timeout_add(DHCPCD_WPA_SCAN_SHORT,
422                     menu_bgscan, dhcpcd_if_connection(w->interface));
423         }
424 }
425
426 static void
427 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
428 {
429         DHCPCD_CONNECTION *con;
430         GtkMenu *mnu;
431         GtkWidget *item, *image;
432
433         notify_close();
434
435         con = (DHCPCD_CONNECTION *)data;
436         mnu = (GtkMenu *)gtk_menu_new();
437
438         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
439         image = gtk_image_new_from_icon_name("preferences-system-network",
440             GTK_ICON_SIZE_MENU);
441         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
442         if (dhcpcd_status(con, NULL) == DHC_DOWN)
443                 gtk_widget_set_sensitive(item, false);
444         else
445                 g_signal_connect(G_OBJECT(item), "activate",
446                     G_CALLBACK(on_pref), data);
447         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
448
449         item = gtk_separator_menu_item_new();
450         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
451
452         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
453         image = gtk_image_new_from_icon_name("help-about",
454             GTK_ICON_SIZE_MENU);
455         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
456         g_signal_connect(G_OBJECT(item), "activate",
457             G_CALLBACK(on_about), icon);
458         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
459
460         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
461         image = gtk_image_new_from_icon_name("application-exit",
462             GTK_ICON_SIZE_MENU);
463         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
464         g_signal_connect(G_OBJECT(item), "activate",
465             G_CALLBACK(on_quit), icon);
466         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
467
468         gtk_widget_show_all(GTK_WIDGET(mnu));
469         gtk_menu_popup(GTK_MENU(mnu), NULL, NULL,
470             gtk_status_icon_position_menu, icon, button, atime);
471         if (button == 0)
472                 gtk_menu_shell_select_first(GTK_MENU_SHELL(mnu), FALSE);
473 }
474
475 void
476 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
477 {
478
479         g_signal_connect(G_OBJECT(icon), "activate",
480             G_CALLBACK(on_activate), con);
481         g_signal_connect(G_OBJECT(icon), "popup_menu",
482             G_CALLBACK(on_popup), con);
483 }
484
485
486 #if GTK_MAJOR_VERSION == 2
487 GtkWidget *
488 gtk_box_new(GtkOrientation o, gint s)
489 {
490
491         if (o == GTK_ORIENTATION_HORIZONTAL)
492                 return gtk_hbox_new(false, s);
493         else
494                 return gtk_vbox_new(false, s);
495 }
496 #endif