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