Don't spam the desktop with a notification on initial launch.
[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, na;
154
155         notify_close();
156
157         n = na =0;
158         for (gl = interfaces; gl; gl = gl->next) {
159                 ifm = (const struct if_msg *)gl->data;
160                 if (ifm->wireless) {
161                         if (ifm->scan_results != NULL)
162                                 ++na;
163                         if (++n > 1 && na != 0)
164                                 break;
165                 }
166         }
167         if (n == 0 || (n == 1 && na == 0))
168                 return;
169
170         menu = (GtkMenu *)gtk_menu_new();
171
172         for (gl = interfaces; gl; gl = gl->next) {
173                 ifm = (const struct if_msg *)gl->data;
174                 if (!ifm->wireless)
175                         continue;
176                 if (n == 1) {
177                         add_scan_results(menu, ifm);
178                         break;
179                 }
180 #if 0
181                 item = gtk_image_menu_item_new_with_label(ifm->name);
182                 image = gtk_image_new_from_icon_name("network-wireless",
183                                                      GTK_ICON_SIZE_MENU);
184                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
185
186                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
187 #endif
188         }
189         gtk_widget_show_all(GTK_WIDGET(menu));
190         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
191                        gtk_status_icon_position_menu, icon,
192                        1, gtk_get_current_event_time());
193 }
194
195 static void
196 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
197 {
198         GtkMenu *menu;
199         GtkWidget *item, *image;
200
201         notify_close();
202
203         menu = (GtkMenu *)gtk_menu_new();
204
205         item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
206         image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
207                                              GTK_ICON_SIZE_MENU);
208         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
209         g_signal_connect(G_OBJECT(item), "activate",
210                          G_CALLBACK(on_quit), icon);
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(_("_Help"));
217         image = gtk_image_new_from_icon_name(GTK_STOCK_HELP,
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_help), icon);
222         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
223
224         item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
225         image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
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_about), 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, data, 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)
241 {
242         g_signal_connect_object(G_OBJECT(icon), "activate",
243                                 G_CALLBACK(on_activate), icon, 0);
244         g_signal_connect_object(G_OBJECT(icon), "popup_menu",
245                                 G_CALLBACK(on_popup), icon, 0);
246 }