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