Capitalise messages
[dhcpcd-ui] / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009 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 /* TODO: Animate the icon from carrier -> address
28  * maybe use network-idle -> network-transmit ->
29  * network-receive -> network-transmit-receive */
30
31 #include <arpa/inet.h>
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <dbus/dbus-glib.h>
37 #include <gtk/gtk.h>
38 #include <libnotify/notify.h>
39
40 #include "config.h"
41 #include "menu.h"
42
43 /* Work out if we have a private address or not
44  * 10/8
45  * 172.16/12
46  * 192.168/16
47  */
48 #ifndef IN_PRIVATE
49 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
50                            ((addr & 0xfff00000)    == 0xac100000) || \
51                            ((addr & IN_CLASSB_NET) == 0xc0a80000))
52 #endif
53 #ifndef IN_LINKLOCAL
54 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == 0xa9fe0000)
55 #endif
56
57 struct if_msg {
58         char *name;
59         char *reason;
60         struct in_addr ip;
61         unsigned char cidr;
62         gboolean wireless;
63         char *ssid;
64 };
65
66 static DBusGProxy *bus_proxy;
67 static GtkStatusIcon *status_icon;
68 static GList *interfaces;
69 static gboolean online;
70 static gboolean carrier;
71 static NotifyNotification *nn;
72
73 static char **interface_order;
74
75 const char *const up_reasons[] = {
76         "BOUND",
77         "RENEW",
78         "REBIND",
79         "REBOOT",
80         "IPV4LL",
81         "INFORM",
82         "TIMEOUT",
83         NULL
84 };
85
86 const char *const down_reasons[] = {
87         "EXPIRE",
88         "FAIL",
89         "NAK",
90         "NOCARRIER",
91         "STOP",
92         NULL
93 };
94
95 /* Should be in a header */
96 void notify_close(void);
97
98 static gboolean
99 ignore_if_msg(const struct if_msg *ifm)
100 {
101         if (g_strcmp0(ifm->reason, "STOP") == 0 ||
102             g_strcmp0(ifm->reason, "RELEASE") == 0)
103                 return TRUE;
104         return FALSE;
105 }
106
107 static void
108 free_if_msg(struct if_msg *ifm)
109 {
110         g_free(ifm->name);
111         g_free(ifm->reason);
112         g_free(ifm->ssid);
113         g_free(ifm);
114 }
115
116 static void
117 error_exit(const char *msg, GError *error)
118 {
119         GtkWidget *dialog;
120
121         if (error) {
122                 g_critical("%s: %s", msg, error->message);
123                 dialog = gtk_message_dialog_new(NULL,
124                                                 0,
125                                                 GTK_MESSAGE_ERROR,
126                                                 GTK_BUTTONS_CLOSE,
127                                                 "%s: %s",
128                                                 msg,
129                                                 error->message);
130         } else {
131                 g_critical("%s", msg);
132                 dialog = gtk_message_dialog_new(NULL,
133                                                 0,
134                                                 GTK_MESSAGE_ERROR,
135                                                 GTK_BUTTONS_CLOSE,
136                                                 "%s",
137                                                 msg);
138         }
139         gtk_dialog_run(GTK_DIALOG(dialog));
140         gtk_widget_destroy(dialog);
141         if (gtk_main_level())
142                 gtk_main_quit();
143         else
144                 exit(EXIT_FAILURE);
145 }
146
147 static struct if_msg *
148 make_if_msg(GHashTable *config)
149 {
150         GValue *val;
151         struct if_msg *ifm;
152
153         val = g_hash_table_lookup(config, "Interface");
154         if (val == NULL)
155                 return NULL;
156         ifm = g_malloc0(sizeof(*ifm));
157         ifm->name = g_strdup(g_value_get_string(val));
158         val = g_hash_table_lookup(config, "Reason");
159         if (val)
160                 ifm->reason = g_strdup(g_value_get_string(val));
161         val = g_hash_table_lookup(config, "Wireless");
162         if (val)
163                 ifm->wireless = g_value_get_boolean(val);
164         if (ifm->wireless) {
165                 val = g_hash_table_lookup(config, "SSID");
166                 if (val)
167                         ifm->ssid = g_strdup(g_value_get_string(val));
168         }
169         val = g_hash_table_lookup(config, "IPAddress");
170         if (val)
171                 ifm->ip.s_addr = g_value_get_uint(val);
172         val = g_hash_table_lookup(config, "SubnetCIDR");
173         if (val)
174                 ifm->cidr = g_value_get_uchar(val);
175         val = g_hash_table_lookup(config, "InterfaceOrder");
176         if (val) {
177                 g_strfreev(interface_order);
178                 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
179         }
180         return ifm;
181 }
182
183 static gboolean
184 if_up(const struct if_msg *ifm)
185 {
186         const char *const *r;
187
188         for (r = up_reasons; *r; r++)
189                 if (g_strcmp0(*r, ifm->reason) == 0)
190                         return TRUE;
191         return FALSE;
192 }
193
194 static char *
195 print_if_msg(const struct if_msg *ifm)
196 {
197         char *msg, *p;
198         const char *reason = NULL;
199         size_t len;
200         gboolean showip, showssid;
201     
202         showip = TRUE;
203         showssid = FALSE;
204         if (if_up(ifm))
205                 reason = "Acquired address";
206         else {
207                 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
208                         reason = "Failed to renew";
209                 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
210                         if (ifm->wireless) {
211                                 reason = "Asssociated with";
212                                 if (ifm->ssid != NULL)
213                                         showssid = TRUE;
214                         } else
215                                 reason = "Cable plugged in";
216                         showip = FALSE;
217                 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
218                         if (ifm->wireless) {
219                                 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
220                                         reason = "Lost association with";
221                                         showssid = TRUE;
222                                 } else
223                                     reason = "Not associated";
224                         } else
225                                 reason = "Cable unplugged";
226                         showip = FALSE;
227                 }
228         }
229         if (reason == NULL)
230                 reason = ifm->reason;
231         
232         len = strlen(ifm->name) + 3;
233         len += strlen(reason) + 1;
234         if (ifm->ip.s_addr != 0) {
235                 len += 16; /* 000. * 4 */
236                 if (ifm->cidr != 0)
237                         len += 3; /* /32 */
238         }
239         if (showssid)
240                 len += strlen(ifm->ssid) + 1;
241         msg = p = g_malloc(len);
242         p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
243         if (showssid)
244                 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
245         if (ifm->ip.s_addr != 0 && showip) {
246                 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
247                 if (ifm->cidr != 0)
248                         g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
249         }
250         return msg;
251 }
252
253 static gint
254 if_msg_comparer(gconstpointer a, gconstpointer b)
255 {
256         const struct if_msg *ifa, *ifb;
257         const char *const *order;
258
259         ifa = (const struct if_msg *)a;
260         ifb = (const struct if_msg *)b;
261         for (order = (const char *const *)interface_order; *order; order++) {
262                 if (g_strcmp0(*order, ifa->name) == 0)
263                         return -1;
264                 if (g_strcmp0(*order, ifb->name) == 0)
265                         return 1;
266         }
267         return 0;
268 }
269
270 static void
271 update_online(char **buffer)
272 {
273         gboolean ison, iscarrier;
274         char *msg, *msgs, *tmp;
275         const char *icon;
276         const GList *gl;
277         const struct if_msg *ifm;
278
279         ison = iscarrier = FALSE;
280         msgs = NULL;
281         for (gl = interfaces; gl; gl = gl->next) {
282                 ifm = (const struct if_msg *)gl->data;
283                 if (if_up(ifm))
284                         ison = iscarrier = TRUE;
285                 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
286                         iscarrier = TRUE;
287                 msg = print_if_msg(ifm);
288                 if (msgs) {
289                         tmp = g_strconcat(msgs, "\n", msg, NULL);
290                         g_free(msgs);
291                         g_free(msg);
292                         msgs = tmp;
293                 } else
294                         msgs = msg;
295         }
296
297         if (online != ison || carrier != iscarrier) {
298                 online = ison;
299                 if (ison)
300                         icon = "network-transmit-receive";
301                 else if (iscarrier)
302                         icon = "network-transmit";
303                 else
304                         icon = "network-offline";
305                 gtk_status_icon_set_from_icon_name(status_icon, icon);
306         }
307         gtk_status_icon_set_tooltip(status_icon, msgs);
308         if (buffer)
309                 *buffer = msgs;
310         else
311                 g_free(msgs);
312 }
313
314 void
315 notify_close(void)
316 {
317         if (nn != NULL)
318                 notify_notification_close(nn, NULL);
319 }
320
321 static void
322 notify_closed(void)
323 {
324         nn = NULL;
325 }
326
327 static void
328 notify(const char *title, const char *msg, const char *icon)
329 {
330         char **msgs, **m;
331
332         msgs = g_strsplit(msg, "\n", 0);
333         for (m = msgs; *m; m++)
334                 g_message("%s", *m);
335         g_strfreev(msgs);
336         if (nn != NULL)
337                 notify_notification_close(nn, NULL);
338         if (gtk_status_icon_get_visible(status_icon))
339                 nn = notify_notification_new_with_status_icon(title,
340                                                               msg,
341                                                               icon,
342                                                               status_icon);
343         else
344                 nn = notify_notification_new(title, msg, icon, NULL);
345         notify_notification_set_timeout(nn, 5000);
346         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
347         notify_notification_show(nn, NULL);
348 }
349
350 static void
351 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
352 {
353         struct if_msg *ifm, *ifp;
354         gboolean rem;
355         GList *gl;
356         char *msg, *title;
357         const char *act, *net;
358         const char *const *r;
359         in_addr_t ipn;
360
361         ifm = make_if_msg(config);
362         if (ifm == NULL)
363                 return;
364
365         rem = ignore_if_msg(ifm);
366         ifp = NULL;
367         for (gl = interfaces; gl; gl = gl->next) {
368                 ifp = (struct if_msg *)gl->data;
369                 if (g_strcmp0(ifp->name, ifm->name) == 0) {
370                         free_if_msg(ifp);
371                         if (rem)
372                                 interfaces = g_list_delete_link(interfaces, gl);
373                         else
374                                 gl->data = ifm;
375                         break;
376                 }
377         }
378         if (ifp == NULL && !rem)
379                 interfaces = g_list_prepend(interfaces, ifm);
380         interfaces = g_list_sort(interfaces, if_msg_comparer);
381         update_online(NULL);
382
383         /* We should ignore renew and stop so we don't annoy the user */
384         if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
385             g_strcmp0(ifm->reason, "STOP") == 0)
386                 return;
387
388         msg = print_if_msg(ifm);
389         title = NULL;
390         if (if_up(ifm))
391                 act = "Connected to ";
392         else
393                 act = NULL;
394         for (r = down_reasons; *r; r++) {
395                 if (g_strcmp0(*r, ifm->reason) == 0) {
396                         act = "Disconnected from ";
397                         break;
398                 }
399         }
400         if (act && ifm->ip.s_addr) {
401                 ipn = htonl(ifm->ip.s_addr);
402                 if (IN_LINKLOCAL(ipn))
403                         net = "private network";
404                 else if (IN_PRIVATE(ipn))
405                         net = "LAN";
406                 else
407                         net = "internet";
408                 title = g_strconcat(act, net, NULL);
409         }
410
411         if (title) {
412                 notify(title, msg, GTK_STOCK_NETWORK);
413                 g_free(title);
414         } else
415                 notify("Interface event", msg, GTK_STOCK_NETWORK);
416         g_free(msg);
417 }
418
419 static void
420 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
421 {
422         struct if_msg *ifm;
423
424         ifm = make_if_msg((GHashTable *)value);
425         if (ignore_if_msg(ifm))
426                 g_free(ifm);
427         else if (ifm)
428                 interfaces = g_list_prepend(interfaces, ifm);
429 }
430
431 static void
432 dhcpcd_get_interfaces()
433 {
434         GHashTable *ifs;
435         GError *error = NULL;
436         GType otype;
437         char *msg;
438
439         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
440         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
441         if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
442                                G_TYPE_INVALID,
443                                otype, &ifs, G_TYPE_INVALID))
444                 error_exit("GetInterfaces", error);
445         g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
446         g_hash_table_unref(ifs);
447
448         /* Each interface config only remembers the last order when
449          * that interface was configured, so get the real order now. */
450         g_strfreev(interface_order);
451         interface_order = NULL;
452         if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
453                                G_TYPE_INVALID,
454                                G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
455                 error_exit("ListInterfaces", error);
456         interfaces = g_list_sort(interfaces, if_msg_comparer);
457         msg = NULL;
458         update_online(&msg);
459         // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
460         if (msg != NULL) {
461                 notify("Interface status", msg, GTK_STOCK_NETWORK);
462                 g_free(msg);
463         }
464 }
465
466 static void
467 check_status(const char *status)
468 {
469         static char *last = NULL;
470         GList *gl;
471         char *version;
472         const char *msg;
473         gboolean refresh;
474         GError *error = NULL;
475
476         g_message("Status changed to %s", status);
477         if (g_strcmp0(status, "down") == 0) {
478                 for (gl = interfaces; gl; gl = gl->next)
479                         free_if_msg((struct if_msg *)gl->data);
480                 g_list_free(interfaces);
481                 interfaces = NULL;
482                 update_online(NULL);
483                 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
484                 gtk_status_icon_set_tooltip(status_icon, msg);
485                 notify("No network", msg, GTK_STOCK_NETWORK);
486         }
487
488         refresh = FALSE;
489         if (last == NULL) {
490                 if (g_strcmp0(status, "down") != 0)
491                         refresh = TRUE;
492         } else {
493                 if (g_strcmp0(status, last) == 0)
494                         return;
495                 if (g_strcmp0(last, "down") == 0)
496                         refresh = TRUE;
497                 g_free(last);
498         }
499         last = g_strdup(status);
500
501         if (!refresh)
502                 return;
503         if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
504                                G_TYPE_INVALID,
505                                G_TYPE_STRING, &version, G_TYPE_INVALID))
506                 error_exit("GetDhcpcdVersion", error);
507         g_message("Connected to dhcpcd-%s", version);
508         g_free(version);
509         dhcpcd_get_interfaces();
510 }
511
512 static void
513 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
514 {
515         check_status(status);
516 }
517
518 int
519 main(int argc, char *argv[])
520 {
521         DBusGConnection *bus;
522         GError *error = NULL;
523         char *version = NULL;
524         GType otype;
525         int tries = 5;
526         
527         gtk_init(&argc, &argv);
528         g_set_application_name("dhcpcd Monitor");
529         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
530         if (status_icon == NULL)
531                 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
532         
533         gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
534         gtk_status_icon_set_visible(status_icon, TRUE);
535
536         notify_init(PACKAGE);
537
538         g_message("Connecting to dbus ...");
539         bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
540         if (bus == NULL || error != NULL)
541                 error_exit("Could not connect to system bus", error);
542         bus_proxy = dbus_g_proxy_new_for_name(bus,
543                                               DHCPCD_SERVICE,
544                                               DHCPCD_PATH,
545                                               DHCPCD_SERVICE);
546
547         g_message("Connecting to dhcpcd-dbus ...");
548         while (--tries > 0) {
549                 g_clear_error(&error);
550                 if (dbus_g_proxy_call_with_timeout(bus_proxy,
551                                                    "GetVersion",
552                                                    500,
553                                                    &error,
554                                                    G_TYPE_INVALID,
555                                                    G_TYPE_STRING,
556                                                    &version,
557                                                    G_TYPE_INVALID))
558                         break;
559         }
560         if (tries == 0)
561                 error_exit("GetVersion", error);
562         g_message("Connected to dhcpcd-dbus-%s", version);
563         g_free(version);
564
565         gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
566         online = FALSE;
567         menu_init(status_icon);
568
569         if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
570                                G_TYPE_INVALID,
571                                G_TYPE_STRING, &version, G_TYPE_INVALID))
572                 error_exit("GetStatus", error);
573         check_status(version);
574         g_free(version);
575
576         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
577         dbus_g_proxy_add_signal(bus_proxy, "Event",
578                                 otype, G_TYPE_INVALID);
579         dbus_g_proxy_connect_signal(bus_proxy, "Event",
580                                     G_CALLBACK(dhcpcd_event),
581                                     NULL, NULL);
582         dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
583                                 G_TYPE_STRING, G_TYPE_INVALID);
584         dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
585                                     G_CALLBACK(dhcpcd_status),
586                                     NULL, NULL);
587
588         gtk_main();
589         return 0;
590 }