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