Add tooltip showing BSSID and flags.
[dhcpcd-ui] / 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 #include "menu.h"
29 #include "wpa.h"
30
31 static const char *copyright = "Copyright (c) 2009 Roy Marples";
32
33 static const char *authors[] = {
34         "Roy Marples <roy@marples.name>",
35         NULL
36 };
37
38 static void
39 on_quit(_unused GtkMenuItem *item, _unused gpointer data)
40 {
41         gtk_main_quit();
42 }
43
44 static void
45 on_help(_unused GtkMenuItem *item, _unused gpointer data)
46 {
47 }
48
49 static void
50 url_show(GtkAboutDialog *dialog, const char *url)
51 {
52         GdkScreen *screen;
53
54         screen = gtk_widget_get_screen(GTK_WIDGET(dialog));
55         gtk_show_uri(screen, url, GDK_CURRENT_TIME, NULL);
56 }
57
58 static void
59 email_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
60 {
61         char *address;
62
63         address = g_strdup_printf("mailto:%s", url);
64         url_show(dialog, address);
65         g_free(address);
66 }
67
68 static void
69 url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
70 {
71         url_show(dialog, url);
72 }
73
74 static void
75 ssid_hook(_unused GtkMenuItem *item, gpointer data)
76 {
77         wpa_configure((const struct if_ap *)data);
78 }
79
80 static void
81 on_about(_unused GtkMenuItem *item, _unused gpointer data)
82 {
83         gtk_window_set_default_icon_name(GTK_STOCK_NETWORK);
84         gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
85         gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
86         gtk_show_about_dialog(NULL,
87                               "version", VERSION,
88                               "copyright", copyright,
89                               "website-label", "dhcpcd Website",
90                               "website", "http://roy.marples.name/projects/dhcpcd",
91                               "authors", authors,
92                               "logo-icon-name", GTK_STOCK_NETWORK,
93                               NULL);
94 }
95
96 static void
97 add_scan_results(GtkMenu *menu, const struct if_msg *ifm)
98 {
99         GSList *gl;
100         const struct if_ap *ifa;
101         GtkWidget *item, *image, *box, *label, *bar;
102         double perc;
103         int strength;
104         const char *icon;
105         char *tip;
106
107         for (gl = ifm->scan_results; gl; gl = gl->next) {
108                 ifa = (const struct if_ap *)gl->data;
109                 item = gtk_check_menu_item_new();
110                 gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item), TRUE); 
111                 box = gtk_hbox_new(FALSE, 6);
112                 gtk_container_add(GTK_CONTAINER(item), box); 
113                 label = gtk_label_new(ifa->ssid);
114                 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
115
116                 if (g_strcmp0(ifm->ssid, ifa->ssid) == 0)
117                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
118                 if (ifa->flags == NULL)
119                         icon = "network-wireless";
120                 else
121                         icon = "network-wireless-encrypted";
122                 image = gtk_image_new_from_icon_name(icon,
123                                                      GTK_ICON_SIZE_MENU);
124                 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
125
126                 bar = gtk_progress_bar_new();
127                 gtk_widget_set_size_request(bar, 100, -1);
128                 gtk_box_pack_end(GTK_BOX(box), bar, FALSE, TRUE, 0);
129                 strength = CLAMP(ifa->quality, 0, 100);
130                 perc = strength / 100.0;
131                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
132
133                 tip = g_strconcat(ifa->bssid, " ", ifa->flags, NULL);
134                 gtk_widget_set_tooltip_text(item, tip);
135                 g_free(tip);
136
137                 gtk_widget_show(label);
138                 gtk_widget_show(bar);
139                 gtk_widget_show(image);
140                 gtk_widget_show(box);
141                 g_signal_connect(G_OBJECT(item), "activate",
142                                 G_CALLBACK(ssid_hook), gl->data);
143                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
144         }
145 }
146
147 static void
148 on_activate(GtkStatusIcon *icon, _unused guint button, _unused guint32 atime, _unused gpointer data)
149 {
150         GtkMenu *menu;
151         const struct if_msg *ifm;
152         GList *gl;
153         size_t n;
154
155         notify_close();
156
157         n = 0;
158         for (gl = interfaces; gl; gl = gl->next) {
159                 ifm = (const struct if_msg *)gl->data;
160                 if (ifm->wireless)
161                         if (++n > 1)
162                                 break;
163         }
164         if (n == 0)
165                 return;
166
167         menu = (GtkMenu *)gtk_menu_new();
168
169         for (gl = interfaces; gl; gl = gl->next) {
170                 ifm = (const struct if_msg *)gl->data;
171                 if (!ifm->wireless)
172                         continue;
173                 if (n == 1) {
174                         add_scan_results(menu, ifm);
175                         break;
176                 }
177 #if 0
178                 item = gtk_image_menu_item_new_with_label(ifm->name);
179                 image = gtk_image_new_from_icon_name("network-wireless",
180                                                      GTK_ICON_SIZE_MENU);
181                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
182
183                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
184 #endif
185         }
186         gtk_widget_show_all(GTK_WIDGET(menu));
187         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
188                        gtk_status_icon_position_menu, icon,
189                        1, gtk_get_current_event_time());
190 }
191
192 static void
193 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
194 {
195         GtkMenu *menu;
196         GtkWidget *item, *image;
197
198         notify_close();
199
200         menu = (GtkMenu *)gtk_menu_new();
201
202         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
203         image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
204                                              GTK_ICON_SIZE_MENU);
205         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
206         g_signal_connect(G_OBJECT(item), "activate",
207                          G_CALLBACK(on_quit), icon);
208         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
209
210         item = gtk_separator_menu_item_new();
211         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
212
213         item = gtk_image_menu_item_new_with_mnemonic(_("_Help"));
214         image = gtk_image_new_from_icon_name(GTK_STOCK_HELP,
215                                              GTK_ICON_SIZE_MENU);
216         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
217         g_signal_connect(G_OBJECT(item), "activate",
218                          G_CALLBACK(on_help), icon);
219         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
220
221         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
222         image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
223                                              GTK_ICON_SIZE_MENU);
224         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
225         g_signal_connect(G_OBJECT(item), "activate",
226                          G_CALLBACK(on_about), icon);
227         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
228
229         gtk_widget_show_all(GTK_WIDGET(menu));
230         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
231                        gtk_status_icon_position_menu, data, button, atime);
232         if (button == 0)
233                 gtk_menu_shell_select_first(GTK_MENU_SHELL(menu), FALSE);
234 }
235
236 void
237 menu_init(GtkStatusIcon *icon)
238 {
239         g_signal_connect_object(G_OBJECT(icon), "activate",
240                                 G_CALLBACK(on_activate), icon, 0);
241         g_signal_connect_object(G_OBJECT(icon), "popup_menu",
242                                 G_CALLBACK(on_popup), icon, 0);
243 }