Make the about dialogs more similar.
[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
32 static GtkStatusIcon *sicon;
33 static GtkWidget *menu;
34 static GtkAboutDialog *about;
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 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 const char *
97 get_strength_icon_name(int strength)
98 {
99
100         if (strength > 80)
101                 return "network-wireless-connected-100";
102         else if (strength > 55)
103                 return "network-wireless-connected-75";
104         else if (strength > 30)
105                 return "network-wireless-connected-50";
106         else if (strength > 5)
107                 return "network-wireless-connected-25";
108         else
109                 return "network-wireless-connected-00";
110 }
111
112 static void
113 update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
114 {
115         const char *icon;
116         GtkWidget *sel;
117
118         m->scan = scan;
119
120         g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
121
122         if (wi->interface->up &&
123             g_strcmp0(scan->ssid, wi->interface->ssid) == 0)
124                 sel = gtk_image_new_from_icon_name("dialog-ok-apply",
125                     GTK_ICON_SIZE_MENU);
126         else
127                 sel = NULL;
128         gtk_image_menu_item_set_image(
129             GTK_IMAGE_MENU_ITEM(m->menu), sel);
130
131         gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
132         if (scan->flags[0] == '\0')
133                 icon = "network-wireless";
134         else
135                 icon = "network-wireless-encrypted";
136         m->icon = gtk_image_new_from_icon_name(icon,
137             GTK_ICON_SIZE_MENU);
138
139         icon = get_strength_icon_name(scan->strength.value);
140         m->strength = gtk_image_new_from_icon_name(icon,
141                 GTK_ICON_SIZE_MENU);
142
143 #if 0
144         if (scan->flags[0] == '\0')
145                 gtk_widget_set_tooltip_text(m->menu, scan->bssid);
146         else {
147                 char *tip = g_strconcat(scan->bssid, " ", scan->flags, 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(GtkWidget *m, WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
158 {
159         WI_MENU *wim;
160         GtkWidget *box, *sel;
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         if (wis->interface->up &&
170             g_strcmp0(scan->ssid, wis->interface->ssid) == 0)
171                 sel = gtk_image_new_from_icon_name("dialog-ok-apply",
172                     GTK_ICON_SIZE_MENU);
173         else
174                 sel = NULL;
175         gtk_image_menu_item_set_image(
176             GTK_IMAGE_MENU_ITEM(wim->menu), sel);
177
178         wim->ssid = gtk_label_new(scan->ssid);
179         gtk_misc_set_alignment(GTK_MISC(wim->ssid), 0.0, 0.5);
180         gtk_box_pack_start(GTK_BOX(box), wim->ssid, TRUE, TRUE, 0);
181
182         if (scan->flags[0] == '\0')
183                 icon = "network-wireless";
184         else
185                 icon = "network-wireless-encrypted";
186         wim->icon = gtk_image_new_from_icon_name(icon,
187             GTK_ICON_SIZE_MENU);
188         gtk_box_pack_start(GTK_BOX(box), wim->icon, FALSE, FALSE, 0);
189
190         icon = get_strength_icon_name(scan->strength.value);
191         wim->strength = gtk_image_new_from_icon_name(icon,
192                 GTK_ICON_SIZE_MENU);
193         gtk_box_pack_start(GTK_BOX(box), wim->strength, FALSE, FALSE, 0);
194
195 #if 0
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 #endif
204
205         g_signal_connect(G_OBJECT(wim->menu), "activate",
206             G_CALLBACK(ssid_hook), NULL);
207         g_object_set_data(G_OBJECT(wim->menu), "dhcpcd_wi_scan", scan);
208         gtk_menu_shell_append(GTK_MENU_SHELL(m), wim->menu);
209
210         return wim;
211 }
212
213 void
214 menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
215 {
216         WI_MENU *wim, *win;
217         DHCPCD_WI_SCAN *s;
218         bool found;
219
220         if (wi->ifmenu == 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                 }
241         }
242
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                         wim = create_menu(wi->ifmenu, wi, s);
255                         TAILQ_INSERT_TAIL(&wi->menus, wim, next);
256                         gtk_widget_show_all(wim->menu);
257                 }
258         }
259
260         dhcpcd_wi_scans_free(wi->scans);
261         wi->scans = scans;
262
263         if (gtk_widget_get_visible(wi->ifmenu))
264                 gtk_menu_reposition(GTK_MENU(wi->ifmenu));
265 }
266
267 void
268 menu_remove_if(WI_SCAN *wi)
269 {
270         WI_MENU *wim;
271
272         if (wi->ifmenu == NULL)
273                 return;
274
275         if (wi->ifmenu == menu)
276                 menu = NULL;
277
278         gtk_widget_destroy(wi->ifmenu);
279         wi->ifmenu = NULL;
280         while ((wim = TAILQ_FIRST(&wi->menus))) {
281                 TAILQ_REMOVE(&wi->menus, wim, next);
282                 g_free(wim);
283         }
284
285         if (menu && gtk_widget_get_visible(menu))
286                 gtk_menu_reposition(GTK_MENU(menu));
287 }
288
289 static GtkWidget *
290 add_scans(WI_SCAN *wi)
291 {
292         GtkWidget *m;
293         DHCPCD_WI_SCAN *wis;
294         WI_MENU *wim;
295
296         if (wi->scans == NULL)
297                 return NULL;
298
299         m = gtk_menu_new();
300         for (wis = wi->scans; wis; wis = wis->next) {
301                 wim = create_menu(m, wi, wis);
302                 TAILQ_INSERT_TAIL(&wi->menus, wim, next);
303         }
304
305         return m;
306 }
307
308 void
309 menu_abort(void)
310 {
311         WI_SCAN *wis;
312         WI_MENU *wim;
313
314         TAILQ_FOREACH(wis, &wi_scans, next) {
315                 wis->ifmenu = NULL;
316                 while ((wim = TAILQ_FIRST(&wis->menus))) {
317                         TAILQ_REMOVE(&wis->menus, wim, next);
318                         g_free(wim);
319                 }
320         }
321
322         if (menu != NULL) {
323                 gtk_widget_destroy(menu);
324                 g_object_ref_sink(menu);
325                 g_object_unref(menu);
326                 menu = NULL;
327         }
328 }
329
330 static void
331 on_activate(GtkStatusIcon *icon)
332 {
333         WI_SCAN *w, *l;
334         GtkWidget *item, *image;
335
336         sicon = icon;
337         notify_close();
338         prefs_abort();
339         menu_abort();
340
341         if ((w = TAILQ_FIRST(&wi_scans)) == NULL)
342                 return;
343
344         if ((l = TAILQ_LAST(&wi_scans, wi_scan_head)) && l != w) {
345                 menu = gtk_menu_new();
346                 ifmenu = true;
347                 TAILQ_FOREACH(w, &wi_scans, next) {
348                         item = gtk_image_menu_item_new_with_label(
349                                 w->interface->ifname);
350                         image = gtk_image_new_from_icon_name(
351                                 "network-wireless", GTK_ICON_SIZE_MENU);
352                         gtk_image_menu_item_set_image(
353                                 GTK_IMAGE_MENU_ITEM(item), image);
354                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
355                         w->ifmenu = add_scans(w);
356                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
357                             w->ifmenu);
358                 }
359         } else {
360                 ifmenu = false;
361                 w->ifmenu = menu = add_scans(w);
362         }
363
364         gtk_widget_show_all(GTK_WIDGET(menu));
365         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
366             gtk_status_icon_position_menu, icon,
367             1, gtk_get_current_event_time());
368 }
369
370 static void
371 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
372 {
373         DHCPCD_CONNECTION *con;
374         GtkMenu *mnu;
375         GtkWidget *item, *image;
376
377         notify_close();
378
379         con = (DHCPCD_CONNECTION *)data;
380         mnu = (GtkMenu *)gtk_menu_new();
381
382         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
383         image = gtk_image_new_from_icon_name("preferences-system-network",
384             GTK_ICON_SIZE_MENU);
385         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
386         if (g_strcmp0(dhcpcd_status(con), "down") == 0)
387                 gtk_widget_set_sensitive(item, false);
388         else
389                 g_signal_connect(G_OBJECT(item), "activate",
390                     G_CALLBACK(on_pref), data);
391         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
392
393         item = gtk_separator_menu_item_new();
394         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
395
396         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
397         image = gtk_image_new_from_icon_name("help-about",
398             GTK_ICON_SIZE_MENU);
399         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
400         g_signal_connect(G_OBJECT(item), "activate",
401             G_CALLBACK(on_about), icon);
402         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
403
404         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
405         image = gtk_image_new_from_icon_name("application-exit",
406             GTK_ICON_SIZE_MENU);
407         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
408         g_signal_connect(G_OBJECT(item), "activate",
409             G_CALLBACK(on_quit), icon);
410         gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
411
412         gtk_widget_show_all(GTK_WIDGET(mnu));
413         gtk_menu_popup(GTK_MENU(mnu), NULL, NULL,
414             gtk_status_icon_position_menu, icon, button, atime);
415         if (button == 0)
416                 gtk_menu_shell_select_first(GTK_MENU_SHELL(mnu), FALSE);
417 }
418
419 void
420 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
421 {
422
423         g_signal_connect(G_OBJECT(icon), "activate",
424             G_CALLBACK(on_activate), con);
425         g_signal_connect(G_OBJECT(icon), "popup_menu",
426             G_CALLBACK(on_popup), con);
427 }
428
429
430 #if GTK_MAJOR_VERSION == 2
431 GtkWidget *
432 gtk_box_new(GtkOrientation o, gint s)
433 {
434
435         if (o == GTK_ORIENTATION_HORIZONTAL)
436                 return gtk_hbox_new(false, s);
437         else
438                 return gtk_vbox_new(false, s);
439 }
440 #endif