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