Bring over configure imporvements from dhcpcd.
[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-2013 Roy Marples";
31 static const char *authors[] = { "Roy Marples <roy@marples.name>", NULL };
32
33 static void
34 on_pref(_unused GObject *o, gpointer data)
35 {
36
37         dhcpcd_prefs_show((DHCPCD_CONNECTION *)data);
38 }
39
40 static void
41 on_quit(void)
42 {
43
44         gtk_main_quit();
45 }
46
47 #if GTK_MAJOR_VERSION == 2
48 static void
49 url_show(GtkAboutDialog *dialog, const char *url)
50 {
51         GdkScreen *screen;
52
53         screen = gtk_widget_get_screen(GTK_WIDGET(dialog));
54         gtk_show_uri(screen, url, GDK_CURRENT_TIME, NULL);
55 }
56
57 static void
58 email_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
59 {
60         char *address;
61
62         address = g_strdup_printf("mailto:%s", url);
63         url_show(dialog, address);
64         g_free(address);
65 }
66
67
68 static void
69 url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
70 {
71         url_show(dialog, url);
72 }
73 #endif
74
75 static void
76 ssid_hook(_unused GtkMenuItem *item, gpointer data)
77 {
78         DHCPCD_WI_SCAN *scan;
79         WI_SCAN *wi;
80
81         scan = (DHCPCD_WI_SCAN *)data;
82         wi = wi_scan_find(scan);
83         if (wi)
84                 wpa_configure(wi->connection, wi->interface, scan);
85 }
86
87 static void
88 on_about(_unused GtkMenuItem *item)
89 {
90
91         gtk_window_set_default_icon_name("network-transmit-receive");
92 #if GTK_MAJOR_VERSION == 2
93         gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
94         gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
95 #endif
96         gtk_show_about_dialog(NULL,
97             "version", VERSION,
98             "copyright", copyright,
99             "website-label", "dhcpcd Website",
100             "website", "http://roy.marples.name/projects/dhcpcd",
101             "authors", authors,
102             "logo-icon-name", "network-transmit-receive",
103             "comments", "Part of the dhcpcd project",
104             NULL);
105 }
106
107 static GtkWidget *
108 add_scans(WI_SCAN *scan)
109 {
110         DHCPCD_WI_SCAN *wis;
111         GtkWidget *menu, *item, *image, *box, *label, *bar;
112         double perc;
113         int strength;
114         const char *icon;
115         char *tip;
116
117         if (scan->scans == NULL)
118                 return NULL;
119         menu = gtk_menu_new();
120         for (wis = scan->scans; wis; wis = wis->next) {
121                 item = gtk_check_menu_item_new();
122                 gtk_check_menu_item_set_draw_as_radio(
123                         GTK_CHECK_MENU_ITEM(item), true); 
124                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
125                 gtk_container_add(GTK_CONTAINER(item), box); 
126                 label = gtk_label_new(wis->ssid);
127                 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
128
129                 if (g_strcmp0(wis->ssid, scan->interface->ssid) == 0)
130                         gtk_check_menu_item_set_active(
131                                 GTK_CHECK_MENU_ITEM(item), true);
132                 if (wis->flags == NULL)
133                         icon = "network-wireless";
134                 else
135                         icon = "network-wireless-encrypted";
136                 image = gtk_image_new_from_icon_name(icon,
137                     GTK_ICON_SIZE_MENU);
138                 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
139
140                 bar = gtk_progress_bar_new();
141                 gtk_widget_set_size_request(bar, 100, -1);
142                 gtk_box_pack_end(GTK_BOX(box), bar, FALSE, TRUE, 0);
143                 if (wis->quality.value == 0)
144                         strength = wis->level.average;
145                 else
146                         strength = wis->quality.average;
147                 perc = CLAMP(strength, 0, 100) / 100.0;
148                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
149
150                 tip = g_strconcat(wis->bssid, " ", wis->flags, NULL);
151                 gtk_widget_set_tooltip_text(item, tip);
152                 g_free(tip);
153
154                 gtk_widget_show(label);
155                 gtk_widget_show(bar);
156                 gtk_widget_show(image);
157                 gtk_widget_show(box);
158                 g_signal_connect(G_OBJECT(item), "activate",
159                     G_CALLBACK(ssid_hook), wis);
160                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
161         }
162         return menu;
163 }
164
165 static void
166 on_activate(GtkStatusIcon *icon)
167 {
168         WI_SCAN *w;
169         GtkWidget *menu, *item, *image;
170
171         notify_close();
172         if (wi_scans == NULL)
173                 return;
174         if (wi_scans->next) {
175                 menu = gtk_menu_new();
176                 for (w = wi_scans; w; w = w->next) {
177                         item = gtk_image_menu_item_new_with_label(
178                                 w->interface->ifname);
179                         image = gtk_image_new_from_icon_name(
180                                 "network-wireless", GTK_ICON_SIZE_MENU);
181                         gtk_image_menu_item_set_image(
182                                 GTK_IMAGE_MENU_ITEM(item), image);
183                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
184                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
185                             add_scans(w));
186                 }
187         } else
188                 menu = add_scans(wi_scans);
189
190         if (menu) {
191                 gtk_widget_show_all(GTK_WIDGET(menu));
192                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
193                     gtk_status_icon_position_menu, icon,
194                     1, gtk_get_current_event_time());
195         }
196 }
197
198 static void
199 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
200 {
201         DHCPCD_CONNECTION *con;
202         GtkMenu *menu;
203         GtkWidget *item, *image;
204
205         notify_close();
206
207         con = (DHCPCD_CONNECTION *)data;
208         menu = (GtkMenu *)gtk_menu_new();
209
210         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
211         image = gtk_image_new_from_icon_name(GTK_STOCK_PREFERENCES,
212             GTK_ICON_SIZE_MENU);
213         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
214         if (g_strcmp0(dhcpcd_status(con), "down") == 0)
215                 gtk_widget_set_sensitive(item, false);
216         else
217                 g_signal_connect(G_OBJECT(item), "activate",
218                     G_CALLBACK(on_pref), data);
219         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
220
221         item = gtk_separator_menu_item_new();
222         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
223
224         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
225         image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
226             GTK_ICON_SIZE_MENU);
227         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
228         g_signal_connect(G_OBJECT(item), "activate",
229             G_CALLBACK(on_about), icon);
230         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
231
232         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
233         image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
234             GTK_ICON_SIZE_MENU);
235         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
236         g_signal_connect(G_OBJECT(item), "activate",
237             G_CALLBACK(on_quit), icon);
238         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
239
240         gtk_widget_show_all(GTK_WIDGET(menu));
241         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
242             gtk_status_icon_position_menu, icon, button, atime);
243         if (button == 0)
244                 gtk_menu_shell_select_first(GTK_MENU_SHELL(menu), FALSE);
245 }
246
247 void
248 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
249 {
250
251         g_signal_connect(G_OBJECT(icon), "activate",
252             G_CALLBACK(on_activate), con);
253         g_signal_connect(G_OBJECT(icon), "popup_menu",
254             G_CALLBACK(on_popup), con);
255 }
256
257
258 #if GTK_MAJOR_VERSION == 2
259 GtkWidget *
260 gtk_box_new(GtkOrientation o, gint s)
261 {
262
263         if (o == GTK_ORIENTATION_HORIZONTAL)
264                 return gtk_hbox_new(false, s);
265         else
266                 return gtk_vbox_new(false, s);
267 }
268 #endif