f2c7bc9fbbaccbba91197b95a08748342e5eaf95
[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 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                 DHCPCD_CONNECTION *con;
85
86                 con = dhcpcd_if_connection(wi->interface);
87                 if (con) {
88                         DHCPCD_WPA *wpa;
89
90                         wpa = dhcpcd_wpa_find(con, wi->interface->ifname);
91                         if (wpa)
92                                 wpa_configure(wpa, scan);
93                 }
94         }
95 }
96
97 static void
98 on_about(_unused GtkMenuItem *item)
99 {
100
101         gtk_window_set_default_icon_name("network-transmit-receive");
102 #if GTK_MAJOR_VERSION == 2
103         gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
104         gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
105 #endif
106         gtk_show_about_dialog(NULL,
107             "version", VERSION,
108             "copyright", copyright,
109             "website-label", "dhcpcd Website",
110             "website", "http://roy.marples.name/projects/dhcpcd",
111             "authors", authors,
112             "logo-icon-name", "network-transmit-receive",
113             "comments", "Part of the dhcpcd project",
114             NULL);
115 }
116
117 static GtkWidget *
118 add_scans(WI_SCAN *scan)
119 {
120         DHCPCD_WI_SCAN *wis;
121         GtkWidget *menu, *item, *image, *box, *label, *bar;
122         double perc;
123         const char *icon;
124         char *tip;
125
126         if (scan->scans == NULL)
127                 return NULL;
128         menu = gtk_menu_new();
129         for (wis = scan->scans; wis; wis = wis->next) {
130                 item = gtk_check_menu_item_new();
131                 gtk_check_menu_item_set_draw_as_radio(
132                         GTK_CHECK_MENU_ITEM(item), true); 
133                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
134                 gtk_container_add(GTK_CONTAINER(item), box); 
135                 label = gtk_label_new(wis->ssid);
136                 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
137
138                 if (g_strcmp0(wis->ssid, scan->interface->ssid) == 0)
139                         gtk_check_menu_item_set_active(
140                                 GTK_CHECK_MENU_ITEM(item), true);
141                 if (wis->flags[0] == '\0')
142                         icon = "network-wireless";
143                 else
144                         icon = "network-wireless-encrypted";
145                 image = gtk_image_new_from_icon_name(icon,
146                     GTK_ICON_SIZE_MENU);
147                 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
148
149                 bar = gtk_progress_bar_new();
150                 gtk_widget_set_size_request(bar, 100, -1);
151                 gtk_box_pack_end(GTK_BOX(box), bar, FALSE, TRUE, 0);
152                 perc = wis->strength.value / 100.0;
153                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
154
155                 if (wis->flags[0] == '\0')
156                         gtk_widget_set_tooltip_text(item, wis->bssid);
157                 else {
158                         tip = g_strconcat(wis->bssid, " ", wis->flags, NULL);
159                         gtk_widget_set_tooltip_text(item, tip);
160                         g_free(tip);
161                 }
162
163                 gtk_widget_show(label);
164                 gtk_widget_show(bar);
165                 gtk_widget_show(image);
166                 gtk_widget_show(box);
167                 g_signal_connect(G_OBJECT(item), "activate",
168                     G_CALLBACK(ssid_hook), wis);
169                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
170         }
171         return menu;
172 }
173
174 static void
175 on_activate(GtkStatusIcon *icon)
176 {
177         WI_SCAN *w;
178         GtkWidget *menu, *item, *image;
179
180         notify_close();
181         if (wi_scans == NULL)
182                 return;
183         if (wi_scans->next) {
184                 menu = gtk_menu_new();
185                 for (w = wi_scans; w; w = w->next) {
186                         item = gtk_image_menu_item_new_with_label(
187                                 w->interface->ifname);
188                         image = gtk_image_new_from_icon_name(
189                                 "network-wireless", GTK_ICON_SIZE_MENU);
190                         gtk_image_menu_item_set_image(
191                                 GTK_IMAGE_MENU_ITEM(item), image);
192                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
193                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
194                             add_scans(w));
195                 }
196         } else
197                 menu = add_scans(wi_scans);
198
199         if (menu) {
200                 gtk_widget_show_all(GTK_WIDGET(menu));
201                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
202                     gtk_status_icon_position_menu, icon,
203                     1, gtk_get_current_event_time());
204         }
205 }
206
207 static void
208 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
209 {
210         DHCPCD_CONNECTION *con;
211         GtkMenu *menu;
212         GtkWidget *item, *image;
213
214         notify_close();
215
216         con = (DHCPCD_CONNECTION *)data;
217         menu = (GtkMenu *)gtk_menu_new();
218
219         item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
220         image = gtk_image_new_from_icon_name(GTK_STOCK_PREFERENCES,
221             GTK_ICON_SIZE_MENU);
222         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
223         if (g_strcmp0(dhcpcd_status(con), "down") == 0)
224                 gtk_widget_set_sensitive(item, false);
225         else
226                 g_signal_connect(G_OBJECT(item), "activate",
227                     G_CALLBACK(on_pref), data);
228         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
229
230         item = gtk_separator_menu_item_new();
231         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
232
233         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
234         image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
235             GTK_ICON_SIZE_MENU);
236         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
237         g_signal_connect(G_OBJECT(item), "activate",
238             G_CALLBACK(on_about), icon);
239         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
240
241         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
242         image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
243             GTK_ICON_SIZE_MENU);
244         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
245         g_signal_connect(G_OBJECT(item), "activate",
246             G_CALLBACK(on_quit), icon);
247         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
248
249         gtk_widget_show_all(GTK_WIDGET(menu));
250         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
251             gtk_status_icon_position_menu, icon, button, atime);
252         if (button == 0)
253                 gtk_menu_shell_select_first(GTK_MENU_SHELL(menu), FALSE);
254 }
255
256 void
257 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
258 {
259
260         g_signal_connect(G_OBJECT(icon), "activate",
261             G_CALLBACK(on_activate), con);
262         g_signal_connect(G_OBJECT(icon), "popup_menu",
263             G_CALLBACK(on_popup), con);
264 }
265
266
267 #if GTK_MAJOR_VERSION == 2
268 GtkWidget *
269 gtk_box_new(GtkOrientation o, gint s)
270 {
271
272         if (o == GTK_ORIENTATION_HORIZONTAL)
273                 return gtk_hbox_new(false, s);
274         else
275                 return gtk_vbox_new(false, s);
276 }
277 #endif