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