Remove options line.
[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         if (scan->flags[0] == '\0')
181                 icon = "network-wireless";
182         else
183                 icon = "network-wireless-encrypted";
184         wim->icon = gtk_image_new_from_icon_name(icon,
185             GTK_ICON_SIZE_MENU);
186         gtk_box_pack_start(GTK_BOX(box), wim->icon, FALSE, FALSE, 0);
187
188         wim->bar = gtk_progress_bar_new();
189         gtk_widget_set_size_request(wim->bar, 100, -1);
190         gtk_box_pack_end(GTK_BOX(box), wim->bar, FALSE, TRUE, 0);
191         perc = scan->strength.value / 100.0;
192         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(wim->bar), perc);
193
194         if (scan->flags[0] == '\0')
195                 gtk_widget_set_tooltip_text(wim->menu, scan->bssid);
196         else {
197                 tip = g_strconcat(scan->bssid, " ", scan->flags, NULL);
198                 gtk_widget_set_tooltip_text(wim->menu, tip);
199                 g_free(tip);
200         }
201
202         g_signal_connect(G_OBJECT(wim->menu), "toggled",
203             G_CALLBACK(ssid_hook), NULL);
204         g_object_set_data(G_OBJECT(wim->menu), "dhcpcd_wi_scan", scan);
205         gtk_menu_shell_append(GTK_MENU_SHELL(m), wim->menu);
206
207         return wim;
208 }
209
210 void
211 menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
212 {
213         WI_MENU *wim, *win;
214         DHCPCD_WI_SCAN *s;
215         bool found, added;
216
217         if (menu == NULL) {
218                 dhcpcd_wi_scans_free(wi->scans);
219                 wi->scans = scans;
220                 return;
221         }
222
223         TAILQ_FOREACH_SAFE(wim, &wi->menus, next, win) {
224                 found = false;
225                 for (s = scans; s; s = s->next) {
226                         if (memcmp(wim->scan->bssid, s->bssid,
227                             sizeof(s->bssid)) == 0)
228                         {
229                                 found = true;
230                                 update_item(wi, wim, s);
231                         }
232                 }
233                 if (!found) {
234                         TAILQ_REMOVE(&wi->menus, wim, next);
235                         g_message("removed %s", wim->scan->ssid);
236                         gtk_widget_destroy(wim->menu);
237                         g_free(wim->scan);
238                         g_free(wim);
239                 }
240         }
241
242         added = false;
243         for (s = scans; s; s = s->next) {
244                 found = false;
245                 TAILQ_FOREACH(wim, &wi->menus, next) {
246                         if (memcmp(wim->scan->bssid, s->bssid,
247                             sizeof(s->bssid)) == 0)
248                         {
249                                 found = true;
250                                 break;
251                         }
252                 }
253                 if (!found) {
254                         added = true;
255                         wim = create_menu(wi->ifmenu, wi, s);
256                         TAILQ_INSERT_TAIL(&wi->menus, wim, next);
257                         gtk_widget_show_all(wim->menu);
258                         g_message("added %s", wim->scan->ssid);
259                 }
260         }
261
262         dhcpcd_wi_scans_free(wi->scans);
263         wi->scans = scans;
264
265         if (added && gtk_widget_get_visible(wi->ifmenu)) {
266 //              gtk_widget_hide(wi->ifmenu);
267 //              gtk_menu_popup(GTK_MENU(wi->ifmenu), NULL, NULL,
268 //                  gtk_status_icon_position_menu, sicon,
269 //                  1, gtk_get_current_event_time());
270 //              gtk_widget_show(wi->ifmenu);
271         }
272 }
273
274
275 static GtkWidget *
276 add_scans(WI_SCAN *wi)
277 {
278         GtkWidget *m;
279         DHCPCD_WI_SCAN *wis;
280         WI_MENU *wim;
281
282         if (wi->scans == NULL)
283                 return NULL;
284
285         m = gtk_menu_new();
286         for (wis = wi->scans; wis; wis = wis->next) {
287                 wim = create_menu(m, wi, wis);
288                 TAILQ_INSERT_TAIL(&wi->menus, wim, next);
289         }
290
291         return m;
292 }
293
294 void
295 menu_abort(void)
296 {
297         WI_SCAN *wis;
298         WI_MENU *wim;
299
300         TAILQ_FOREACH(wis, &wi_scans, next) {
301                 wis->ifmenu = NULL;
302                 while ((wim = TAILQ_FIRST(&wis->menus))) {
303                         TAILQ_REMOVE(&wis->menus, wim, next);
304                         g_free(wim);
305                 }
306         }
307
308         if (menu != NULL) {
309                 gtk_widget_destroy(menu);
310                 g_object_ref_sink(menu);
311                 g_object_unref(menu);
312                 menu = NULL;
313         }
314 }
315
316 static void
317 on_activate(GtkStatusIcon *icon)
318 {
319         WI_SCAN *w, *l;
320         GtkWidget *item, *image;
321
322         sicon = icon;
323         notify_close();
324         prefs_abort();
325         menu_abort();
326
327         if ((w = TAILQ_FIRST(&wi_scans)) == NULL)
328                 return;
329
330         if ((l = TAILQ_LAST(&wi_scans, wi_scan_head)) && l != w) {
331                 menu = gtk_menu_new();
332                 ifmenu = true;
333                 TAILQ_FOREACH(w, &wi_scans, next) {
334                         item = gtk_image_menu_item_new_with_label(
335                                 w->interface->ifname);
336                         image = gtk_image_new_from_icon_name(
337                                 "network-wireless", GTK_ICON_SIZE_MENU);
338                         gtk_image_menu_item_set_image(
339                                 GTK_IMAGE_MENU_ITEM(item), image);
340                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
341                         w->ifmenu = add_scans(w);
342                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
343                             w->ifmenu);
344                 }
345         } else {
346                 ifmenu = false;
347                 w->ifmenu = menu = add_scans(w);
348         }
349
350         gtk_widget_show_all(GTK_WIDGET(menu));
351         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
352             gtk_status_icon_position_menu, icon,
353             1, gtk_get_current_event_time());
354 }
355
356 static void
357 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
358 {
359         DHCPCD_CONNECTION *con;
360         GtkMenu *mnu;
361         GtkWidget *item, *image;
362
363         notify_close();
364
365         con = (DHCPCD_CONNECTION *)data;
366         mnu = (GtkMenu *)gtk_menu_new();
367
368         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
369         image = gtk_image_new_from_icon_name("preferences-system-network",
370             GTK_ICON_SIZE_MENU);
371         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
372         if (g_strcmp0(dhcpcd_status(con), "down") == 0)
373                 gtk_widget_set_sensitive(item, false);
374         else
375                 g_signal_connect(G_OBJECT(item), "activate",
376                     G_CALLBACK(on_pref), data);
377         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
378
379         item = gtk_separator_menu_item_new();
380         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
381
382         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
383         image = gtk_image_new_from_icon_name("help-about",
384             GTK_ICON_SIZE_MENU);
385         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
386         g_signal_connect(G_OBJECT(item), "activate",
387             G_CALLBACK(on_about), icon);
388         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
389
390         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
391         image = gtk_image_new_from_icon_name("application-exit",
392             GTK_ICON_SIZE_MENU);
393         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
394         g_signal_connect(G_OBJECT(item), "activate",
395             G_CALLBACK(on_quit), icon);
396         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
397
398         gtk_widget_show_all(GTK_WIDGET(mnu));
399         gtk_menu_popup(GTK_MENU(mnu), NULL, NULL,
400             gtk_status_icon_position_menu, icon, button, atime);
401         if (button == 0)
402                 gtk_menu_shell_select_first(GTK_MENU_SHELL(mnu), FALSE);
403 }
404
405 void
406 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
407 {
408
409         g_signal_connect(G_OBJECT(icon), "activate",
410             G_CALLBACK(on_activate), con);
411         g_signal_connect(G_OBJECT(icon), "popup_menu",
412             G_CALLBACK(on_popup), con);
413 }
414
415
416 #if GTK_MAJOR_VERSION == 2
417 GtkWidget *
418 gtk_box_new(GtkOrientation o, gint s)
419 {
420
421         if (o == GTK_ORIENTATION_HORIZONTAL)
422                 return gtk_hbox_new(false, s);
423         else
424                 return gtk_vbox_new(false, s);
425 }
426 #endif