Fix icon.
[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 <locale.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <libnotify/notify.h>
36
37 #include "dhcpcd-gtk.h"
38 #include "menu.h"
39
40 DBusGProxy *dbus = NULL;
41 GList *interfaces = NULL;
42
43 static GtkStatusIcon *status_icon;
44 static gint ani_timer;
45 static int ani_counter;
46 static gboolean online;
47 static gboolean carrier;
48 static char **interface_order;
49 static NotifyNotification *nn;
50
51 const char *const up_reasons[] = {
52         "BOUND",
53         "RENEW",
54         "REBIND",
55         "REBOOT",
56         "IPV4LL",
57         "INFORM",
58         "TIMEOUT",
59         NULL
60 };
61
62 const char *const down_reasons[] = {
63         "EXPIRE",
64         "FAIL",
65         "NAK",
66         "NOCARRIER",
67         "STOP",
68         NULL
69 };
70
71 static gboolean
72 ignore_if_msg(const struct if_msg *ifm)
73 {
74         if (g_strcmp0(ifm->reason, "STOP") == 0 ||
75             g_strcmp0(ifm->reason, "RELEASE") == 0)
76                 return TRUE;
77         return FALSE;
78 }
79
80 static struct if_msg *
81 find_if_msg(const char *iface)
82 {
83         GList *gl;
84         struct if_msg *ifm;
85
86         for (gl = interfaces; gl; gl = gl->next) {
87                 ifm = (struct if_msg *)gl->data;
88                 if (g_strcmp0(ifm->ifname, iface) == 0)
89                         return ifm;
90         }
91         return NULL;
92 }
93
94 static void
95 free_if_ap(struct if_ap *ifa)
96 {
97         g_free(ifa->ifname);
98         g_free(ifa->bssid);
99         g_free(ifa->flags);
100         g_free(ifa->ssid);
101         g_free(ifa);
102 }
103
104 static void
105 free_if_msg(struct if_msg *ifm)
106 {
107         GSList *gl;
108
109         g_free(ifm->ifname);
110         g_free(ifm->reason);
111         g_free(ifm->ssid);
112         for (gl = ifm->scan_results; gl; gl = gl->next)
113                 free_if_ap((struct if_ap *)gl->data);
114         g_slist_free(ifm->scan_results);
115         g_free(ifm);
116 }
117
118 static void
119 error_exit(const char *msg, GError *error)
120 {
121         GtkWidget *dialog;
122
123         if (error) {
124                 g_critical("%s: %s", msg, error->message);
125                 dialog = gtk_message_dialog_new(NULL, 0,
126                     GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
127                     "%s: %s", msg, error->message);
128         } else {
129                 g_critical("%s", msg);
130                 dialog = gtk_message_dialog_new(NULL, 0,
131                     GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", msg);
132         }
133         gtk_dialog_run(GTK_DIALOG(dialog));
134         gtk_widget_destroy(dialog);
135         if (gtk_main_level())
136                 gtk_main_quit();
137         else
138                 exit(EXIT_FAILURE);
139 }
140
141 static GSList *
142 get_scan_results(struct if_msg *ifm)
143 {
144         GType otype;
145         GError *error;
146         GPtrArray *array;
147         GHashTable *config;
148         GSList *list = NULL;
149         struct if_ap *ifa;
150         guint i;
151         GValue *val;
152
153         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
154         otype = dbus_g_type_get_collection("GPtrArray", otype);
155
156         if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
157                 G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
158                 otype, &array, G_TYPE_INVALID))
159                 error_exit(_("ScanResults"), error);
160
161         for (i = 0; i < array->len; i++) {
162                 config = g_ptr_array_index(array, i);
163                 val = g_hash_table_lookup(config, "BSSID");
164                 if (val == NULL)
165                         continue;
166                 ifa = g_malloc0(sizeof(*ifa));
167                 ifa->ifname = g_strdup(ifm->ifname);
168                 ifa->bssid = g_strdup(g_value_get_string(val));
169                 val = g_hash_table_lookup(config, "Frequency");
170                 if (val)
171                         ifa->frequency = g_value_get_int(val);
172                 val = g_hash_table_lookup(config, "Quality");
173                 if (val)
174                         ifa->quality = g_value_get_int(val);
175                 val = g_hash_table_lookup(config, "Noise");
176                 if (val)
177                         ifa->noise = g_value_get_int(val);
178                 val = g_hash_table_lookup(config, "Level");
179                 if (val)
180                         ifa->level = g_value_get_int(val);
181                 val = g_hash_table_lookup(config, "Flags");
182                 if (val)
183                         ifa->flags = g_strdup(g_value_get_string(val));
184                 val = g_hash_table_lookup(config, "SSID");
185                 if (val)
186                         ifa->ssid = g_strdup(g_value_get_string(val));
187                 list = g_slist_append(list, ifa);
188         }
189         return list;
190 }
191
192 static struct if_msg *
193 make_if_msg(GHashTable *config)
194 {
195         GValue *val;
196         struct if_msg *ifm;
197
198         val = g_hash_table_lookup(config, "Interface");
199         if (val == NULL)
200                 return NULL;
201         ifm = g_malloc0(sizeof(*ifm));
202         ifm->ifname = g_strdup(g_value_get_string(val));
203         val = g_hash_table_lookup(config, "Reason");
204         if (val)
205                 ifm->reason = g_strdup(g_value_get_string(val));
206         val = g_hash_table_lookup(config, "Wireless");
207         if (val)
208                 ifm->wireless = g_value_get_boolean(val);
209         if (ifm->wireless) {
210                 val = g_hash_table_lookup(config, "SSID");
211                 if (val)
212                         ifm->ssid = g_strdup(g_value_get_string(val));
213         }
214         val = g_hash_table_lookup(config, "IPAddress");
215         if (val)
216                 ifm->ip.s_addr = g_value_get_uint(val);
217         val = g_hash_table_lookup(config, "SubnetCIDR");
218         if (val)
219                 ifm->cidr = g_value_get_uchar(val);
220         val = g_hash_table_lookup(config, "InterfaceOrder");
221         if (val) {
222                 g_strfreev(interface_order);
223                 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
224         }
225         return ifm;
226 }
227
228 static gboolean
229 if_up(const struct if_msg *ifm)
230 {
231         const char *const *r;
232
233         for (r = up_reasons; *r; r++)
234                 if (g_strcmp0(*r, ifm->reason) == 0)
235                         return TRUE;
236         return FALSE;
237 }
238
239 static char *
240 print_if_msg(const struct if_msg *ifm)
241 {
242         char *msg, *p;
243         const char *reason = NULL;
244         size_t len;
245         gboolean showip, showssid;
246     
247         showip = TRUE;
248         showssid = FALSE;
249         if (if_up(ifm))
250                 reason = N_("Acquired address");
251         else {
252                 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
253                         reason = N_("Failed to renew");
254                 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
255                         if (ifm->wireless) {
256                                 reason = N_("Asssociated with");
257                                 if (ifm->ssid != NULL)
258                                         showssid = TRUE;
259                         } else
260                                 reason = N_("Cable plugged in");
261                         showip = FALSE;
262                 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
263                         if (ifm->wireless) {
264                                 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
265                                         reason = N_("Disassociated from");
266                                         showssid = TRUE;
267                                 } else
268                                         reason = N_("Not associated");
269                         } else
270                                 reason = N_("Cable unplugged");
271                         showip = FALSE;
272                 }
273         }
274         if (reason == NULL)
275                 reason = ifm->reason;
276         
277         len = strlen(ifm->ifname) + 3;
278         len += strlen(reason) + 1;
279         if (ifm->ip.s_addr != 0) {
280                 len += 16; /* 000. * 4 */
281                 if (ifm->cidr != 0)
282                         len += 3; /* /32 */
283         }
284         if (showssid)
285                 len += strlen(ifm->ssid) + 1;
286         msg = p = g_malloc(len);
287         p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
288         if (showssid)
289                 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
290         if (ifm->ip.s_addr != 0 && showip) {
291                 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
292                 if (ifm->cidr != 0)
293                         g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
294         }
295         return msg;
296 }
297
298 static gint
299 if_msg_comparer(gconstpointer a, gconstpointer b)
300 {
301         const struct if_msg *ifa, *ifb;
302         const char *const *order;
303
304         ifa = (const struct if_msg *)a;
305         ifb = (const struct if_msg *)b;
306         for (order = (const char *const *)interface_order; *order; order++) {
307                 if (g_strcmp0(*order, ifa->ifname) == 0)
308                         return -1;
309                 if (g_strcmp0(*order, ifb->ifname) == 0)
310                         return 1;
311         }
312         return 0;
313 }
314
315 static gboolean
316 animate_carrier(_unused gpointer data)
317 {
318         const char *icon;
319         
320         if (ani_timer == 0)
321                 return FALSE;
322
323         switch(ani_counter++) {
324         case 0:
325                 icon = "network-transmit";
326                 break;
327         case 1:
328                 icon = "network-receive";
329                 break;
330         default:
331                 icon = "network-idle";
332                 ani_counter = 0;
333                 break;
334         }
335         gtk_status_icon_set_from_icon_name(status_icon, icon);
336         return TRUE;
337 }
338
339 static gboolean
340 animate_online(_unused gpointer data)
341 {
342         const char *icon;
343         
344         if (ani_timer == 0)
345                 return FALSE;
346
347         if (ani_counter++ > 6) {
348                 ani_timer = 0;
349                 ani_counter = 0;
350                 return FALSE;
351         }
352
353         if (ani_counter % 2 == 0)
354                 icon = "network-idle";
355         else
356                 icon = "network-transmit-receive";
357         gtk_status_icon_set_from_icon_name(status_icon, icon);
358         return TRUE;
359 }
360
361 static void
362 update_online(void)
363 {
364         gboolean ison, iscarrier;
365         char *msg, *msgs, *tmp;
366         const GList *gl;
367         const struct if_msg *ifm;
368
369         ison = iscarrier = FALSE;
370         msgs = NULL;
371         for (gl = interfaces; gl; gl = gl->next) {
372                 ifm = (const struct if_msg *)gl->data;
373                 if (if_up(ifm))
374                         ison = iscarrier = TRUE;
375                 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
376                         iscarrier = TRUE;
377                 msg = print_if_msg(ifm);
378                 if (msgs) {
379                         tmp = g_strconcat(msgs, "\n", msg, NULL);
380                         g_free(msgs);
381                         g_free(msg);
382                         msgs = tmp;
383                 } else
384                         msgs = msg;
385         }
386
387         if (online != ison || carrier != iscarrier) {
388                 online = ison;
389                 if (ani_timer != 0) {
390                         g_source_remove(ani_timer);
391                         ani_timer = 0;
392                         ani_counter = 0;
393                 }
394                 if (ison) {
395                         animate_online(NULL);
396                         ani_timer = g_timeout_add(300, animate_online, NULL);
397                 } else if (iscarrier) {
398                         animate_carrier(NULL);
399                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
400                 } else {
401                         gtk_status_icon_set_from_icon_name(status_icon,
402                             "network-offline");
403                 }
404         }
405         gtk_status_icon_set_tooltip(status_icon, msgs);
406         g_free(msgs);
407 }
408
409 void
410 notify_close(void)
411 {
412         if (nn != NULL)
413                 notify_notification_close(nn, NULL);
414 }
415
416 static void
417 notify_closed(void)
418 {
419         nn = NULL;
420 }
421
422 static void
423 notify(const char *title, const char *msg, const char *icon)
424 {
425         char **msgs, **m;
426
427         msgs = g_strsplit(msg, "\n", 0);
428         for (m = msgs; *m; m++)
429                 g_message("%s", *m);
430         g_strfreev(msgs);
431         if (nn != NULL)
432                 notify_notification_close(nn, NULL);
433         if (gtk_status_icon_get_visible(status_icon))
434                 nn = notify_notification_new_with_status_icon(title,
435                     msg, icon, status_icon);
436         else
437                 nn = notify_notification_new(title, msg, icon, NULL);
438         notify_notification_set_timeout(nn, 5000);
439         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
440         notify_notification_show(nn, NULL);
441 }
442
443 static void
444 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
445 {
446         struct if_msg *ifm, *ifp;
447         gboolean rem;
448         GList *gl;
449         char *msg, *title;
450         const char *act, *net;
451         const char *const *r;
452         in_addr_t ipn;
453
454         ifm = make_if_msg(config);
455         if (ifm == NULL)
456                 return;
457
458         rem = ignore_if_msg(ifm);
459         ifp = NULL;
460         for (gl = interfaces; gl; gl = gl->next) {
461                 ifp = (struct if_msg *)gl->data;
462                 if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
463                         ifm->scan_results = ifp->scan_results;
464                         ifp->scan_results = NULL;
465                         free_if_msg(ifp);
466                         if (rem)
467                                 interfaces =
468                                         g_list_delete_link(interfaces, gl);
469                         else
470                                 gl->data = ifm;
471                         break;
472                 }
473         }
474         if (ifp == NULL && !rem)
475                 interfaces = g_list_prepend(interfaces, ifm);
476         interfaces = g_list_sort(interfaces, if_msg_comparer);
477         update_online();
478
479         /* We should ignore renew and stop so we don't annoy the user */
480         if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
481             g_strcmp0(ifm->reason, "STOP") == 0)
482                 return;
483
484         msg = print_if_msg(ifm);
485         title = NULL;
486         if (if_up(ifm))
487                 act = N_("Connected to ");
488         else
489                 act = NULL;
490         for (r = down_reasons; *r; r++) {
491                 if (g_strcmp0(*r, ifm->reason) == 0) {
492                         act = N_("Disconnected from ");
493                         break;
494                 }
495         }
496         if (act && ifm->ip.s_addr) {
497                 ipn = htonl(ifm->ip.s_addr);
498                 if (IN_LINKLOCAL(ipn))
499                         net = N_("private network");
500                 else if (IN_PRIVATE(ipn))
501                         net = N_("LAN");
502                 else
503                         net = N_("internet");
504                 title = g_strconcat(act, net, NULL);
505         }
506
507         if (title) {
508                 notify(title, msg, GTK_STOCK_NETWORK);
509                 g_free(title);
510         } else
511                 notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
512         g_free(msg);
513 }
514
515 static void
516 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
517 {
518         struct if_msg *ifm;
519
520         ifm = make_if_msg((GHashTable *)value);
521         if (ignore_if_msg(ifm))
522                 g_free(ifm);
523         else if (ifm)
524                 interfaces = g_list_prepend(interfaces, ifm);
525 }
526
527 static void
528 dhcpcd_get_interfaces()
529 {
530         GHashTable *ifs;
531         GError *error = NULL;
532         GType otype;
533         GList *gl;
534         GSList *gsl;
535         GPtrArray *array;
536         struct if_msg *ifm;
537
538         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
539         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
540         if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
541                 G_TYPE_INVALID,
542                 otype, &ifs, G_TYPE_INVALID))
543                 error_exit("GetInterfaces", error);
544         g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
545         g_hash_table_unref(ifs);
546
547         /* Each interface config only remembers the last order when
548          * that interface was configured, so get the real order now. */
549         g_strfreev(interface_order);
550         interface_order = NULL;
551         if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
552                 G_TYPE_INVALID,
553                 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
554                 error_exit("ListInterfaces", error);
555         interfaces = g_list_sort(interfaces, if_msg_comparer);
556
557         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
558         otype = dbus_g_type_get_collection("GPtrArray", otype);
559         for (gl = interfaces; gl; gl = gl->next) {
560                 ifm = (struct if_msg *)gl->data;
561                 if (!ifm->wireless)
562                         continue;
563                 if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
564                         G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
565                         otype, &array, G_TYPE_INVALID)) 
566                 {
567                         g_message("ScanResults: %s", error->message);
568                         g_clear_error(&error);
569                         continue;
570                 }
571                 for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
572                         g_free(gsl->data);
573                 g_slist_free(ifm->scan_results);
574                 ifm->scan_results = get_scan_results(ifm);
575         }
576
577         update_online();
578 }
579
580 static void
581 check_status(const char *status)
582 {
583         static char *last = NULL;
584         GList *gl;
585         char *version;
586         const char *msg;
587         gboolean refresh;
588         GError *error = NULL;
589
590         g_message("Status changed to %s", status);
591         if (g_strcmp0(status, "down") == 0) {
592                 for (gl = interfaces; gl; gl = gl->next)
593                         free_if_msg((struct if_msg *)gl->data);
594                 g_list_free(interfaces);
595                 interfaces = NULL;
596                 update_online();
597                 msg = N_(last ?
598                     "Connection to dhcpcd lost" : "dhcpcd not running");
599                 gtk_status_icon_set_tooltip(status_icon, msg);
600                 notify(_("No network"), msg, GTK_STOCK_NETWORK);
601         }
602
603         refresh = FALSE;
604         if (last == NULL) {
605                 if (g_strcmp0(status, "down") != 0)
606                         refresh = TRUE;
607         } else {
608                 if (g_strcmp0(status, last) == 0)
609                         return;
610                 if (g_strcmp0(last, "down") == 0)
611                         refresh = TRUE;
612                 g_free(last);
613         }
614         last = g_strdup(status);
615
616         if (!refresh)
617                 return;
618         if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
619                 G_TYPE_INVALID,
620                 G_TYPE_STRING, &version, G_TYPE_INVALID))
621                 error_exit(_("GetDhcpcdVersion"), error);
622         g_message(_("Connected to %s-%s"), "dhcpcd", version);
623         g_free(version);
624         dhcpcd_get_interfaces();
625 }
626
627 static void
628 dhcpcd_status(_unused DBusGProxy *proxy, const char *status,
629     _unused void *data)
630 {
631         check_status(status);
632 }
633
634 static void
635 dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface,
636     _unused void *data)
637 {
638         struct if_msg *ifm;
639         struct if_ap *ifa, *ifa2;
640         GSList *gl, *aps, *l;
641         char *txt, *ntxt;
642
643         ifm = find_if_msg(iface);
644         if (ifm == NULL)
645                 return;
646         g_message(_("%s: Received scan results"), ifm->ifname);
647         aps = get_scan_results(ifm);
648         txt = NULL;
649         for (gl = aps; gl; gl = gl->next) {
650                 ifa = (struct if_ap *)gl->data;
651                 for (l = ifm->scan_results; l; l = l->next) {
652                         ifa2 = (struct if_ap *)l->data;
653                         if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
654                                 break;
655                 }
656                 if (l == NULL) {
657                         if (txt == NULL)
658                                 txt = g_strdup(ifa->ssid);
659                         else {
660                                 ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
661                                 g_free(txt);
662                                 txt = ntxt;
663                         }
664                 }
665         }
666         for (gl = ifm->scan_results; gl; gl = gl->next)
667                 free_if_ap((struct if_ap *)gl->data);
668         g_slist_free(ifm->scan_results);
669         ifm->scan_results = aps;
670         if (txt != NULL) {
671                 notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
672                 g_free(txt);
673         }
674 }
675
676 int
677 main(int argc, char *argv[])
678 {
679         DBusGConnection *bus;
680         GError *error = NULL;
681         char *version = NULL;
682         GType otype;
683         int tries = 5;
684
685                 
686         setlocale(LC_ALL, "");
687         bindtextdomain(PACKAGE, NULL);
688         bind_textdomain_codeset(PACKAGE, "UTF-8");
689         textdomain(PACKAGE); 
690
691         gtk_init(&argc, &argv);
692         g_set_application_name("dhcpcd Monitor");
693         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
694         if (status_icon == NULL)
695                 status_icon =
696                         gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
697         //network_offline = gtk_status_icon_get_pixbuf(status_icon);
698         
699         gtk_status_icon_set_tooltip(status_icon,
700             _("Connecting to dhcpcd ..."));
701         gtk_status_icon_set_visible(status_icon, TRUE);
702
703         notify_init(PACKAGE);
704
705         g_message(_("Connecting to dbus ..."));
706         bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
707         if (bus == NULL || error != NULL)
708                 error_exit(_("Could not connect to system bus"), error);
709         dbus = dbus_g_proxy_new_for_name(bus,
710             DHCPCD_SERVICE,
711             DHCPCD_PATH,
712             DHCPCD_SERVICE);
713
714         g_message(_("Connecting to dhcpcd-dbus ..."));
715         while (--tries > 0) {
716                 g_clear_error(&error);
717                 if (dbus_g_proxy_call_with_timeout(dbus, "GetVersion", 500,
718                         &error, G_TYPE_INVALID,
719                         G_TYPE_STRING, &version, G_TYPE_INVALID))
720                         break;
721         }
722         if (tries == 0)
723                 error_exit(_("GetVersion"), error);
724         g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
725         g_free(version);
726
727         gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
728         online = FALSE;
729         menu_init(status_icon);
730
731         if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
732                 G_TYPE_INVALID,
733                 G_TYPE_STRING, &version, G_TYPE_INVALID))
734                 error_exit(_("GetStatus"), error);
735         check_status(version);
736         g_free(version);
737
738         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
739         dbus_g_proxy_add_signal(dbus, "Event",
740             otype, G_TYPE_INVALID);
741         dbus_g_proxy_connect_signal(dbus, "Event",
742             G_CALLBACK(dhcpcd_event), bus, NULL);
743         dbus_g_proxy_add_signal(dbus, "StatusChanged",
744             G_TYPE_STRING, G_TYPE_INVALID);
745         dbus_g_proxy_connect_signal(dbus, "StatusChanged",
746             G_CALLBACK(dhcpcd_status), bus, NULL);
747         dbus_g_proxy_add_signal(dbus, "ScanResults",
748             G_TYPE_STRING, G_TYPE_INVALID);
749         dbus_g_proxy_connect_signal(dbus, "ScanResults",
750             G_CALLBACK(dhcpcd_scan_results), bus, NULL);
751
752         gtk_main();
753         return 0;
754 }