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