Work with a seperate interface for Router Advertisements
[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;
125         char *msg, *msgs, *tmp;
126         DHCPCD_IF *ifs, *i;
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 (i->up)
138                         ison = iscarrier = true;
139                 if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
140                         iscarrier = true;
141                 msg = dhcpcd_if_message(i);
142                 if (msgs) {
143                         tmp = g_strconcat(msgs, "\n", msg, NULL);
144                         g_free(msgs);
145                         g_free(msg);
146                         msgs = tmp;
147                 } else
148                         msgs = msg;
149         }
150
151         if (online != ison || carrier != iscarrier) {
152                 online = ison;
153                 if (ani_timer != 0) {
154                         g_source_remove(ani_timer);
155                         ani_timer = 0;
156                         ani_counter = 0;
157                 }
158                 if (ison) {
159                         animate_online(NULL);
160                         ani_timer = g_timeout_add(300, animate_online, NULL);
161                 } else if (iscarrier) {
162                         animate_carrier(NULL);
163                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
164                 } else {
165                         gtk_status_icon_set_from_icon_name(status_icon,
166                             "network-offline");
167                 }
168         }
169         gtk_status_icon_set_tooltip(status_icon, msgs);
170         g_free(msgs);
171 }
172
173 void
174 notify_close(void)
175 {
176 #ifdef NOTIFY
177         if (nn != NULL)
178                 notify_notification_close(nn, NULL);
179 #endif
180 }
181
182 #ifdef NOTIFY
183 static char *notify_last_msg;
184
185 static void
186 notify_closed(void)
187 {
188         nn = NULL;
189 }
190
191 static void
192 notify(const char *title, const char *msg, const char *icon)
193 {
194         char **msgs, **m;
195
196         /* Don't spam the same message */
197         if (notify_last_msg) {
198                 if (strcmp(msg, notify_last_msg) == 0)
199                         return;
200                 g_free(notify_last_msg);
201         }
202         notify_last_msg = g_strdup(msg);
203
204         msgs = g_strsplit(msg, "\n", 0);
205         for (m = msgs; *m; m++)
206                 g_message("%s", *m);
207         g_strfreev(msgs);
208
209 #if NOTIFY_CHECK_VERSION(0,7,0)
210         nn = nn = notify_notification_new(title, msg, icon);
211 #else
212         if (nn != NULL)
213                 notify_notification_close(nn, NULL);
214         if (gtk_status_icon_get_visible(status_icon))
215                 nn = notify_notification_new_with_status_icon(title,
216                     msg, icon, status_icon);
217         else
218                 nn = notify_notification_new(title, msg, icon, NULL);
219 #endif
220
221         notify_notification_set_timeout(nn, 5000);
222         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
223         notify_notification_show(nn, NULL);
224 }
225 #else
226 #  define notify(a, b, c)
227 #endif
228
229 static void
230 event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
231 {
232         char *msg;
233         const char *icon;
234
235         g_message("%s: %s", i->ifname, i->reason);
236         update_online(con, false);
237         
238         /* We should ignore renew and stop so we don't annoy the user */
239         if (g_strcmp0(i->reason, "RENEW") == 0 ||
240             g_strcmp0(i->reason, "STOP") == 0)
241                 return;
242
243         msg = dhcpcd_if_message(i);
244         if (i->up)
245                 icon = "network-transmit-receive";
246         //else
247         //      icon = "network-transmit";
248         if (!i->up)
249                 icon = "network-offline";
250         notify(_("Network event"), msg, icon);
251         g_free(msg);
252 }
253
254 static void
255 status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
256 {
257         static char *last = NULL;
258         char *version;
259         const char *msg;
260         bool refresh;
261         WI_SCAN *w;
262
263         g_message("Status changed to %s", status);
264         if (g_strcmp0(status, "down") == 0) {
265                 msg = N_(last ?
266                     "Connection to dhcpcd lost" : "dhcpcd not running");
267                 if (ani_timer != 0) {
268                         g_source_remove(ani_timer);
269                         ani_timer = 0;
270                         ani_counter = 0;
271                 }
272                 gtk_status_icon_set_from_icon_name(status_icon,
273                     "network-offline");
274                 gtk_status_icon_set_tooltip(status_icon, msg);
275                 notify(_("No network"), msg, "network-offline");
276                 dhcpcd_prefs_abort();
277                 while (wi_scans) {
278                         w = wi_scans->next;
279                         dhcpcd_wi_scans_free(wi_scans->scans);
280                         g_free(wi_scans);
281                         wi_scans = w;
282                 }
283         } else {
284                 if ((last == NULL || g_strcmp0(last, "down") == 0) &&
285                     dhcpcd_command(con, "GetDhcpcdVersion", NULL, &version))
286                 {
287                         g_message(_("Connected to %s-%s"), "dhcpcd", version);
288                         g_free(version);
289                         refresh = true;
290                 } else
291                         refresh = false;
292                 update_online(con, refresh);
293         }
294         last = g_strdup(status);
295 }
296
297 static void
298 scan_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
299 {
300         WI_SCAN *w;
301         DHCPCD_WI_SCAN *scans, *s1, *s2;
302         char *txt, *t;
303         const char *msg;
304
305         g_message(_("%s: Received scan results"), i->ifname);
306         scans = dhcpcd_wi_scans(con, i);
307         if (scans == NULL && dhcpcd_error(con) != NULL) {
308                 g_warning("%s: %s", i->ifname, dhcpcd_error(con));
309                 dhcpcd_error_clear(con);
310         }
311         for (w = wi_scans; w; w = w->next)
312                 if (w->connection == con && w->interface == i)
313                         break;
314         if (w == NULL) {
315                 w = g_malloc(sizeof(*w));
316                 w->connection = con;
317                 w->interface = i;
318                 w->next = wi_scans;
319                 wi_scans = w;
320         } else {
321                 txt = NULL;
322                 msg = N_("New Access Point");
323                 for (s1 = scans; s1; s1 = s1->next) {
324                         for (s2 = w->scans; s2; s2 = s2->next)
325                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
326                                         break;
327                         if (s2 == NULL) {
328                                 if (txt == NULL)
329                                         txt = g_strdup(s1->ssid);
330                                 else {
331                                         msg = N_("New Access Points");
332                                         t = g_strconcat(txt, "\n",
333                                             s1->ssid, NULL);
334                                         g_free(txt);
335                                         txt = t;
336                                 }
337                         }
338                 }
339                 if (txt) {
340                         notify(msg, txt, "network-wireless");
341                         g_free(txt);
342                 }
343                 dhcpcd_wi_scans_free(w->scans);
344         }
345         w->scans = scans;
346 }
347
348 static gboolean
349 gio_callback(GIOChannel *gio, _unused GIOCondition c, _unused gpointer d)
350 {
351         int fd;
352
353         fd = g_io_channel_unix_get_fd(gio);
354         dhcpcd_dispatch(fd);
355         return true;
356 }
357
358 static void
359 delete_watch_cb(_unused DHCPCD_CONNECTION *con, const struct pollfd *fd,
360     _unused void *data)
361 {
362         struct watch *w, *l;
363
364         l = NULL;
365         for (w = watches; w; w = w->next) {
366                 if (w->pollfd.fd == fd->fd) {
367                         if (l == NULL)
368                                 watches = w->next;
369                         else
370                                 l->next = w->next;
371                         g_source_remove(w->eventid);
372                         g_io_channel_unref(w->gio);
373                         g_free(w);
374                         break;
375                 }
376         }
377 }
378
379 static void
380 add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
381     _unused void *data)
382 {
383         struct watch *w;
384         GIOChannel *gio;
385         int flags, eventid;
386
387         /* Remove any existing watch */
388         delete_watch_cb(con, fd, data);
389         
390         gio = g_io_channel_unix_new(fd->fd);
391         if (gio == NULL) {
392                 g_warning(_("Error creating new GIO Channel\n"));
393                 return;
394         }
395         flags = 0;
396         if (fd->events & POLLIN)
397                 flags |= G_IO_IN;
398         if (fd->events & POLLOUT)
399                 flags |= G_IO_OUT;
400         if (fd->events & POLLERR)
401                 flags |= G_IO_ERR;
402         if (fd->events & POLLHUP)
403                 flags |= G_IO_HUP;
404         if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
405                 g_io_channel_unref(gio);
406                 g_warning(_("Error creating watch\n"));
407                 return;
408         }
409         w = g_malloc(sizeof(*w));
410         memcpy(&w->pollfd, fd, sizeof(w->pollfd));
411         w->eventid = eventid;
412         w->gio = gio;
413         w->next = watches;
414         watches = w;
415 }
416
417 int
418 main(int argc, char *argv[])
419 {
420         char *error = NULL;
421         char *version = NULL;
422         DHCPCD_CONNECTION *con;
423                 
424         setlocale(LC_ALL, "");
425         bindtextdomain(PACKAGE, NULL);
426         bind_textdomain_codeset(PACKAGE, "UTF-8");
427         textdomain(PACKAGE); 
428
429         gtk_init(&argc, &argv);
430         g_set_application_name("Network Configurator");
431         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
432             ICONDIR);
433         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
434         
435         gtk_status_icon_set_tooltip(status_icon,
436             _("Connecting to dhcpcd ..."));
437         gtk_status_icon_set_visible(status_icon, true);
438
439 #ifdef NOTIFY
440         notify_init(PACKAGE);
441 #endif
442
443         g_message(_("Connecting ..."));
444         con = dhcpcd_open(&error);
445         if (con ==  NULL) {
446                 g_critical("libdhcpcd: %s", error);
447                 exit(EXIT_FAILURE);
448         }
449
450         gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
451         online = false;
452
453         if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
454                 g_critical("libdhcpcd: GetVersion: %s", dhcpcd_error(con));
455                 exit(EXIT_FAILURE);
456         }
457         g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
458         g_free(version);
459
460         dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
461         dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
462         if (dhcpcd_error(con)) {
463                 g_critical("libdhcpcd: %s", dhcpcd_error(con));
464                 exit(EXIT_FAILURE);
465         }
466
467         menu_init(status_icon, con);
468
469         gtk_main();
470         dhcpcd_close(con);
471         return 0;
472 }