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