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