Fix segfault if error getting scans.
[dhcpcd-ui] / src / dhcpcd-gtk / main.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 <locale.h>
28 #include <poll.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 static NotifyNotification *nn;
35 #endif
36
37 #include "dhcpcd-gtk.h"
38
39 static GtkStatusIcon *status_icon;
40 static int ani_timer;
41 static int ani_counter;
42 static bool online;
43 static bool carrier;
44
45 struct watch {
46         struct pollfd pollfd;
47         int eventid;
48         GIOChannel *gio;
49         struct watch *next;
50 };
51 static struct watch *watches;
52
53 WI_SCAN *wi_scans;
54
55 WI_SCAN *
56 wi_scan_find(DHCPCD_WI_SCAN *scan)
57 {
58         WI_SCAN *w;
59         DHCPCD_WI_SCAN *dw;
60
61         for (w = wi_scans; w; w = w->next) {
62                 for (dw = w->scans; dw; dw = dw->next)
63                         if (dw == scan)
64                                 break;
65                 if (dw)
66                         return w;
67         }
68         return NULL;
69 }
70
71 static gboolean
72 animate_carrier(_unused gpointer data)
73 {
74         const char *icon;
75         
76         if (ani_timer == 0)
77                 return false;
78
79         switch(ani_counter++) {
80         case 0:
81                 icon = "network-transmit";
82                 break;
83         case 1:
84                 icon = "network-receive";
85                 break;
86         default:
87                 icon = "network-idle";
88                 ani_counter = 0;
89                 break;
90         }
91         gtk_status_icon_set_from_icon_name(status_icon, icon);
92         return true;
93 }
94
95 static gboolean
96 animate_online(_unused gpointer data)
97 {
98         const char *icon;
99         
100         if (ani_timer == 0)
101                 return false;
102
103         if (ani_counter++ > 6) {
104                 ani_timer = 0;
105                 ani_counter = 0;
106                 return false;
107         }
108
109         if (ani_counter % 2 == 0)
110                 icon = "network-idle";
111         else
112                 icon = "network-transmit-receive";
113         gtk_status_icon_set_from_icon_name(status_icon, icon);
114         return true;
115 }
116
117 static void
118 update_online(DHCPCD_CONNECTION *con, bool showif)
119 {
120         bool ison, iscarrier;
121         char *msg, *msgs, *tmp;
122         DHCPCD_IF *ifs, *i;
123
124         ison = iscarrier = false;
125         msgs = NULL;
126         ifs = dhcpcd_interfaces(con);
127         for (i = ifs; i; i = i->next) {
128                 if (showif)
129                         g_message("%s: %s", i->ifname, i->reason);
130                 if (strcmp(i->reason, "RELEASE") == 0 ||
131                     strcmp(i->reason, "STOP") == 0)
132                         continue;
133                 if (dhcpcd_if_up(i))
134                         ison = iscarrier = true;
135                 if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
136                         iscarrier = true;
137                 msg = dhcpcd_if_message(i);
138                 if (msgs) {
139                         tmp = g_strconcat(msgs, "\n", msg, NULL);
140                         g_free(msgs);
141                         g_free(msg);
142                         msgs = tmp;
143                 } else
144                         msgs = msg;
145         }
146
147         if (online != ison || carrier != iscarrier) {
148                 online = ison;
149                 if (ani_timer != 0) {
150                         g_source_remove(ani_timer);
151                         ani_timer = 0;
152                         ani_counter = 0;
153                 }
154                 if (ison) {
155                         animate_online(NULL);
156                         ani_timer = g_timeout_add(300, animate_online, NULL);
157                 } else if (iscarrier) {
158                         animate_carrier(NULL);
159                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
160                 } else {
161                         gtk_status_icon_set_from_icon_name(status_icon,
162                             "network-offline");
163                 }
164         }
165         gtk_status_icon_set_tooltip(status_icon, msgs);
166         g_free(msgs);
167 }
168
169 void
170 notify_close(void)
171 {
172 #ifdef NOTIFY
173         if (nn != NULL)
174                 notify_notification_close(nn, NULL);
175 #endif
176 }
177
178 static void
179 notify_closed(void)
180 {
181 #ifdef NOTIFY
182         nn = NULL;
183 #endif
184 }
185
186 #ifdef NOTIFY
187 static void
188 notify(const char *title, const char *msg, const char *icon)
189 {
190         char **msgs, **m;
191
192         msgs = g_strsplit(msg, "\n", 0);
193         for (m = msgs; *m; m++)
194                 g_message("%s", *m);
195         g_strfreev(msgs);
196         if (nn != NULL)
197                 notify_notification_close(nn, NULL);
198         if (gtk_status_icon_get_visible(status_icon))
199                 nn = notify_notification_new_with_status_icon(title,
200                     msg, icon, status_icon);
201         else
202                 nn = notify_notification_new(title, msg, icon, NULL);
203         notify_notification_set_timeout(nn, 5000);
204         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
205         notify_notification_show(nn, NULL);
206 }
207 #else
208 #  define notify(a, b, c)
209 #endif
210
211 static void
212 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
213 {
214         char *msg;
215         const char *icon;
216
217         g_message("%s: %s", i->ifname, i->reason);
218         update_online(con, false);
219         
220         /* We should ignore renew and stop so we don't annoy the user */
221         if (g_strcmp0(i->reason, "RENEW") == 0 ||
222             g_strcmp0(i->reason, "STOP") == 0)
223                 return;
224
225         msg = dhcpcd_if_message(i);
226         if (dhcpcd_if_up(i))
227                 icon = "network-transmit-receive";
228         else
229                 icon = "network-transmit";
230         if (dhcpcd_if_down(i))
231                 icon = "network-offline";
232         notify(_("Network event"), msg, icon);
233         g_free(msg);
234 }
235
236 static void
237 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
238 {
239         static char *last = NULL;
240         char *version;
241         const char *msg;
242         bool refresh;
243         WI_SCAN *w;
244
245         g_message("Status changed to %s", status);
246         if (g_strcmp0(status, "down") == 0) {
247                 msg = N_(last ?
248                     "Connection to dhcpcd lost" : "dhcpcd not running");
249                 gtk_status_icon_set_tooltip(status_icon, msg);
250                 notify(_("No network"), msg, "network-offline");
251                 dhcpcd_prefs_abort();
252                 while (wi_scans) {
253                         w = wi_scans->next;
254                         dhcpcd_wi_scans_free(wi_scans->scans);
255                         g_free(wi_scans);
256                         wi_scans = w;
257                 }
258         } else {
259                 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
260                     dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
261                 {
262                         g_message(_("Connected to %s-%s"), "dhcpcd", version);
263                         g_free(version);
264                         refresh = true;
265                 } else
266                         refresh = false;
267                 update_online(con, refresh);
268         }
269         last = g_strdup(status);
270 }
271
272 static void
273 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
274 {
275         WI_SCAN *w;
276         DHCPCD_WI_SCAN *scans, *s1, *s2;
277         char *txt, *t;
278         const char *msg;
279
280         g_message(_("%s: Received scan results"), i->ifname);
281         scans = dhcpcd_wi_scans(con, i);
282         if (scans == NULL) {
283                 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
284                 dhcpcd_error_clear(con);
285         }
286         for (w = wi_scans; w; w = w->next)
287                 if (w->connection == con && w->interface == i)
288                         break;
289         if (w == NULL) {
290                 w = g_malloc(sizeof(*w));
291                 w->connection = con;
292                 w->interface = i;
293                 w->next = wi_scans;
294                 wi_scans = w;
295         } else {
296                 txt = NULL;
297                 msg = N_("New Access Point");
298                 for (s1 = scans; s1; s1 = s1->next) {
299                         for (s2 = w->scans; s2; s2 = s2->next)
300                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
301                                         break;
302                         if (s2 == NULL) {
303                                 if (txt == NULL)
304                                         txt = g_strdup(s1->ssid);
305                                 else {
306                                         msg = N_("New Access Points");
307                                         t = g_strconcat(txt, "\n",
308                                             s1->ssid, NULL);
309                                         g_free(txt);
310                                         txt = t;
311                                 }
312                         }
313                 }
314                 if (txt) {
315                         notify(msg, txt, "network-wireless");
316                         g_free(txt);
317                 }
318                 dhcpcd_wi_scans_free(w->scans);
319         }
320         w->scans = scans;
321 }
322
323 static gboolean
324 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
325 {
326         int fd;
327
328         fd = g_io_channel_unix_get_fd(gio);
329         dhcpcd_dispatch(fd);
330         return true;
331 }
332
333 static void
334 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
335     _unused void *data)
336 {
337         struct watch *w, *l;
338
339         l = NULL;
340         for (w = watches; w; w = w->next) {
341                 if (w->pollfd.fd == fd->fd) {
342                         if (l == NULL)
343                                 watches = w->next;
344                         else
345                                 l->next = w->next;
346                         g_source_remove(w->eventid);
347                         g_io_channel_unref(w->gio);
348                         g_free(w);
349                         break;
350                 }
351         }
352 }
353
354 static void
355 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
356     _unused void *data)
357 {
358         struct watch *w;
359         GIOChannel *gio;
360         int flags, eventid;
361
362         /* Remove any existing watch */
363         delete_watch_cb(con, fd, data);
364         
365         gio = g_io_channel_unix_new(fd->fd);
366         if (gio == NULL) {
367                 g_error(_("Error creating new GIO Channel\n"));
368                 return;
369         }
370         flags = 0;
371         if (fd->events & POLLIN)
372                 flags |= G_IO_IN;
373         if (fd->events & POLLOUT)
374                 flags |= G_IO_OUT;
375         if (fd->events & POLLERR)
376                 flags |= G_IO_ERR;
377         if (fd->events & POLLHUP)
378                 flags |= G_IO_HUP;
379         if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
380                 g_io_channel_unref(gio);
381                 g_error(_("Error creating watch\n"));
382                 return;
383         }
384         w = g_malloc(sizeof(*w));
385         memcpy(&w->pollfd, fd, sizeof(w->pollfd));
386         w->eventid = eventid;
387         w->gio = gio;
388         w->next = watches;
389         watches = w;
390 }
391
392 int
393 main(int argc, char *argv[])
394 {
395         char *error = NULL;
396         char *version = NULL;
397         DHCPCD_CONNECTION *con;
398                 
399         setlocale(LC_ALL, "");
400         bindtextdomain(PACKAGE, NULL);
401         bind_textdomain_codeset(PACKAGE, "UTF-8");
402         textdomain(PACKAGE); 
403
404         gtk_init(&argc, &argv);
405         g_set_application_name("Network Configurator");
406         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
407             ICONDIR);
408         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
409         
410         gtk_status_icon_set_tooltip(status_icon,
411             _("Connecting to dhcpcd ..."));
412         gtk_status_icon_set_visible(status_icon, true);
413
414         notify_init(PACKAGE);
415
416         g_message(_("Connecting ..."));
417         con = dhcpcd_open(&error);
418         if (con ==  NULL) {
419                 g_critical("libdhcpcd: %s", error);
420                 exit(EXIT_FAILURE);
421         }
422
423         gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
424         online = false;
425
426         if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
427                 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
428                 exit(EXIT_FAILURE);
429         }
430         g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
431         g_free(version);
432
433         dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
434         dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
435         if (dhcpcd_error(con))
436                 g_error("libdhcpcd: %s", dhcpcd_error(con));
437
438         menu_init(status_icon, con);
439
440         gtk_main();
441         dhcpcd_close(con);
442         return 0;
443 }