Add preliminary support for getting a list of AP's via dbus. This presently doesn...
[dhcpcd-ui] / main.c
diff --git a/main.c b/main.c
index 95d13be05977c952a1a0c82533f252ac10b9d834..b6acd2087dc8036c2baab9c925260bbb7eac13f9 100644 (file)
--- a/main.c
+++ b/main.c
  * SUCH DAMAGE.
  */
 
-#include <arpa/inet.h>
+/* TODO: Animate the icon from carrier -> address
+ * maybe use network-idle -> network-transmit ->
+ * network-receive -> network-transmit-receive */
 
+#include <locale.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <dbus/dbus-glib.h>
-#include <gtk/gtk.h>
 #include <libnotify/notify.h>
 
-#include "config.h"
+#include "dhcpcd-gtk.h"
 #include "menu.h"
 
-/* Work out if we have a private address or not
- * 10/8
- * 172.16/12
- * 192.168/16
- */
-#ifndef IN_PRIVATE
-# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
-                          ((addr & 0xfff00000)    == 0xac100000) || \
-                          ((addr & IN_CLASSB_NET) == 0xc0a80000))
-#endif
-#ifndef IN_LINKLOCAL
-# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == 0xa9fe0000)
-#endif
-
-struct if_msg {
-       char *name;
-       char *reason;
-       struct in_addr ip;
-       unsigned char cidr;
-};
+DBusGProxy *dbus = NULL;
+GList *interfaces = NULL;
 
-static DBusGProxy *bus_proxy;
-static GtkStatusIcon *status_icon;
-static GList *interfaces;
-static gboolean online;
+static GtkStatusIcon *status_icon = NULL;
 static NotifyNotification *nn;
-
+static gboolean online;
+static gboolean carrier;
 static char **interface_order;
 
 const char *const up_reasons[] = {
@@ -85,9 +66,6 @@ const char *const down_reasons[] = {
        NULL
 };
 
-/* Should be in a header */
-void notify_close(void);
-
 static gboolean
 ignore_if_msg(const struct if_msg *ifm)
 {
@@ -102,6 +80,7 @@ free_if_msg(struct if_msg *ifm)
 {
        g_free(ifm->name);
        g_free(ifm->reason);
+       g_free(ifm->ssid);
        g_free(ifm);
 }
 
@@ -136,6 +115,56 @@ error_exit(const char *msg, GError *error)
                exit(EXIT_FAILURE);
 }
 
+GList *
+get_scan_results(const char *iface)
+{
+       GType otype;
+       GPtrArray *array = NULL;
+       GValueArray *item;
+       GError *error = NULL;
+       GList *list = NULL;
+       struct if_ap *ifa;
+       guint i;
+       GValue *v;
+
+       /* Below code causes dbus to go belly up for some reason :/ */
+       return NULL;
+       otype = dbus_g_type_get_struct("GValueArray",
+                                      G_TYPE_STRING,
+                                      G_TYPE_UINT,
+                                      G_TYPE_UINT,
+                                      G_TYPE_STRING,
+                                      G_TYPE_STRING,
+                                      G_TYPE_INVALID);
+       otype = dbus_g_type_get_collection("GPtrArray", otype);
+       if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
+                              G_TYPE_STRING, &iface, G_TYPE_INVALID,
+                              otype, &array, G_TYPE_INVALID)) {
+               g_message("GetScanResults: %s\n", error->message);
+               g_clear_error(&error);
+               return NULL;
+       }
+
+       printf ("got %d\n", array->len);
+       for (i = 0; i < array->len; i++) {
+               ifa = g_malloc(sizeof(*ifa));
+               item = g_ptr_array_index(array, i);
+               v = g_value_array_get_nth(item, 0);
+               ifa->bssid = g_strdup(g_value_get_string(v));
+               v = g_value_array_get_nth(item, 1);
+               ifa->freq = g_value_get_uint(v);
+               v = g_value_array_get_nth(item, 2);
+               ifa->level = g_value_get_uint(v);
+               v = g_value_array_get_nth(item, 3);
+               ifa->flags = g_strdup(g_value_get_string(v));
+               v = g_value_array_get_nth(item, 3);
+               ifa->ssid = g_strdup(g_value_get_string(v));
+               list = g_list_append(list, ifa);
+       }
+       g_ptr_array_free(array, TRUE);
+       return list;
+}
+
 static struct if_msg *
 make_if_msg(GHashTable *config)
 {
@@ -150,6 +179,14 @@ make_if_msg(GHashTable *config)
        val = g_hash_table_lookup(config, "Reason");
        if (val)
                ifm->reason = g_strdup(g_value_get_string(val));
+       val = g_hash_table_lookup(config, "Wireless");
+       if (val)
+               ifm->wireless = g_value_get_boolean(val);
+       if (ifm->wireless) {
+               val = g_hash_table_lookup(config, "SSID");
+               if (val)
+                       ifm->ssid = g_strdup(g_value_get_string(val));
+       }
        val = g_hash_table_lookup(config, "IPAddress");
        if (val)
                ifm->ip.s_addr = g_value_get_uint(val);
@@ -161,28 +198,73 @@ make_if_msg(GHashTable *config)
                g_strfreev(interface_order);
                interface_order = g_strsplit(g_value_get_string(val), " ", 0);
        }
+       if (ifm->wireless)
+               ifm->scan_results = get_scan_results(ifm->name);
        return ifm;
 }
 
+static gboolean
+if_up(const struct if_msg *ifm)
+{
+       const char *const *r;
+
+       for (r = up_reasons; *r; r++)
+               if (g_strcmp0(*r, ifm->reason) == 0)
+                       return TRUE;
+       return FALSE;
+}
+
 static char *
 print_if_msg(const struct if_msg *ifm)
 {
        char *msg, *p;
+       const char *reason = NULL;
        size_t len;
-       gboolean showip;
-
+       gboolean showip, showssid;
+    
+       showip = TRUE;
+       showssid = FALSE;
+       if (if_up(ifm))
+               reason = N_("Acquired address");
+       else {
+               if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
+                       reason = N_("Failed to renew");
+               else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
+                       if (ifm->wireless) {
+                               reason = N_("Asssociated with");
+                               if (ifm->ssid != NULL)
+                                       showssid = TRUE;
+                       } else
+                               reason = N_("Cable plugged in");
+                       showip = FALSE;
+               } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
+                       if (ifm->wireless) {
+                               if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
+                                       reason = N_("Lost association with");
+                                       showssid = TRUE;
+                               } else
+                                   reason = N_("Not associated");
+                       } else
+                               reason = N_("Cable unplugged");
+                       showip = FALSE;
+               }
+       }
+       if (reason == NULL)
+               reason = ifm->reason;
+       
        len = strlen(ifm->name) + 3;
-       len += strlen(ifm->reason) + 1;
+       len += strlen(reason) + 1;
        if (ifm->ip.s_addr != 0) {
                len += 16; /* 000. * 4 */
                if (ifm->cidr != 0)
                        len += 3; /* /32 */
        }
+       if (showssid)
+               len += strlen(ifm->ssid) + 1;
        msg = p = g_malloc(len);
-       p += g_snprintf(msg, len, "%s: %s", ifm->name, ifm->reason);
-       showip = TRUE;
-       if (g_strcmp0(ifm->reason, "NOCARRIER") == 0)
-               showip = FALSE;
+       p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
+       if (showssid)
+               p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
        if (ifm->ip.s_addr != 0 && showip) {
                p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
                if (ifm->cidr != 0)
@@ -211,23 +293,20 @@ if_msg_comparer(gconstpointer a, gconstpointer b)
 static void
 update_online(char **buffer)
 {
-       gboolean ison;
+       gboolean ison, iscarrier;
        char *msg, *msgs, *tmp;
        const char *icon;
-       const char *const *r;
        const GList *gl;
        const struct if_msg *ifm;
 
-       ison = FALSE;
+       ison = iscarrier = FALSE;
        msgs = NULL;
        for (gl = interfaces; gl; gl = gl->next) {
-               ifm = (const struct if_msg *)gl->data;          
-               for (r = up_reasons; *r; r++) {
-                       if (g_strcmp0(*r, ifm->reason) == 0) {
-                               ison = TRUE;
-                               break;
-                       }
-               }
+               ifm = (const struct if_msg *)gl->data;
+               if (if_up(ifm))
+                       ison = iscarrier = TRUE;
+               if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
+                       iscarrier = TRUE;
                msg = print_if_msg(ifm);
                if (msgs) {
                        tmp = g_strconcat(msgs, "\n", msg, NULL);
@@ -238,10 +317,15 @@ update_online(char **buffer)
                        msgs = msg;
        }
 
-       if (online != ison) {
+       if (online != ison || carrier != iscarrier) {
                online = ison;
-               icon = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
-               gtk_status_icon_set_from_stock(status_icon, icon);
+               if (ison)
+                       icon = "network-transmit-receive";
+               else if (iscarrier)
+                       icon = "network-transmit";
+               else
+                       icon = "network-offline";
+               gtk_status_icon_set_from_icon_name(status_icon, icon);
        }
        gtk_status_icon_set_tooltip(status_icon, msgs);
        if (buffer)
@@ -253,7 +337,8 @@ update_online(char **buffer)
 void
 notify_close(void)
 {
-       notify_notification_close(nn, NULL);
+       if (nn != NULL)
+               notify_notification_close(nn, NULL);
 }
 
 static void
@@ -316,34 +401,36 @@ dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
        if (ifp == NULL && !rem)
                interfaces = g_list_prepend(interfaces, ifm);
        interfaces = g_list_sort(interfaces, if_msg_comparer);
+       update_online(NULL);
+
+       /* We should ignore renew and stop so we don't annoy the user */
+       if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
+           g_strcmp0(ifm->reason, "STOP") == 0)
+               return;
 
        msg = print_if_msg(ifm);
-       act = NULL;
        title = NULL;
-       for (r = up_reasons; *r; r++) {
-               if (g_strcmp0(*r, ifm->reason) == 0) {
-                       act = "Connected to ";
-                       break;
-               }
-       }
+       if (if_up(ifm))
+               act = N_("Connected to ");
+       else
+               act = NULL;
        for (r = down_reasons; *r; r++) {
                if (g_strcmp0(*r, ifm->reason) == 0) {
-                       act = "Disconnected from ";
+                       act = N_("Disconnected from ");
                        break;
                }
        }
        if (act && ifm->ip.s_addr) {
                ipn = htonl(ifm->ip.s_addr);
                if (IN_LINKLOCAL(ipn))
-                       net = "private network";
+                       net = N_("private network");
                else if (IN_PRIVATE(ipn))
-                       net = "LAN";
+                       net = N_("LAN");
                else
-                       net = "internet";
+                       net = N_("internet");
                title = g_strconcat(act, net, NULL);
        }
 
-       update_online(NULL);
        if (title) {
                notify(title, msg, GTK_STOCK_NETWORK);
                g_free(title);
@@ -374,7 +461,7 @@ dhcpcd_get_interfaces()
 
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
-       if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
+       if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
                               G_TYPE_INVALID,
                               otype, &ifs, G_TYPE_INVALID))
                error_exit("GetInterfaces", error);
@@ -385,7 +472,7 @@ dhcpcd_get_interfaces()
         * that interface was configured, so get the real order now. */
        g_strfreev(interface_order);
        interface_order = NULL;
-       if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
+       if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
                               G_TYPE_INVALID,
                               G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
                error_exit("ListInterfaces", error);
@@ -409,16 +496,16 @@ check_status(const char *status)
        gboolean refresh;
        GError *error = NULL;
 
-       g_message("status changed to %s", status);
+       g_message("Status changed to %s", status);
        if (g_strcmp0(status, "down") == 0) {
                for (gl = interfaces; gl; gl = gl->next)
                        free_if_msg((struct if_msg *)gl->data);
                g_list_free(interfaces);
                interfaces = NULL;
                update_online(NULL);
-               msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
+               msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
                gtk_status_icon_set_tooltip(status_icon, msg);
-               notify("No network", msg, GTK_STOCK_NETWORK);
+               notify(_("No network"), msg, GTK_STOCK_NETWORK);
        }
 
        refresh = FALSE;
@@ -436,11 +523,11 @@ check_status(const char *status)
 
        if (!refresh)
                return;
-       if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
+       if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
                               G_TYPE_INVALID,
                               G_TYPE_STRING, &version, G_TYPE_INVALID))
-               error_exit("GetDhcpcdVersion", error);
-       g_message("Connected to dhcpcd-%s", version);
+               error_exit(_("GetDhcpcdVersion"), error);
+       g_message(_("Connected to %s-%s"), "dhcpcd", version);
        g_free(version);
        dhcpcd_get_interfaces();
 }
@@ -458,54 +545,72 @@ main(int argc, char *argv[])
        GError *error = NULL;
        char *version = NULL;
        GType otype;
-       
-#if defined(HAVE_GNOME) || defined(HAVE_XFCE)
-       if (!g_thread_supported())
-               g_thread_init(NULL);
-#endif
+       int tries = 5;
+
+               
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, NULL);
+       bind_textdomain_codeset(PACKAGE, "UTF-8");
+       textdomain(PACKAGE); 
 
        gtk_init(&argc, &argv);
        g_set_application_name("dhcpcd Monitor");
-       status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
-       gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
+       status_icon = gtk_status_icon_new_from_icon_name("network-offline");
+       if (status_icon == NULL)
+               status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
+       
+       gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
        gtk_status_icon_set_visible(status_icon, TRUE);
 
        notify_init(PACKAGE);
 
+       g_message(_("Connecting to dbus ..."));
        bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
        if (bus == NULL || error != NULL)
-               error_exit("could not connect to system bus", error);
-       bus_proxy = dbus_g_proxy_new_for_name(bus,
+               error_exit(_("Could not connect to system bus"), error);
+       dbus = dbus_g_proxy_new_for_name(bus,
                                              DHCPCD_SERVICE,
                                              DHCPCD_PATH,
                                              DHCPCD_SERVICE);
-       if (!dbus_g_proxy_call(bus_proxy, "GetVersion", &error,
-                              G_TYPE_INVALID,
-                              G_TYPE_STRING, &version, G_TYPE_INVALID))
-               error_exit("GetVersion", error);
-       g_message("Connected to dhcpcd-dbus-%s", version);
+
+       g_message(_("Connecting to dhcpcd-dbus ..."));
+       while (--tries > 0) {
+               g_clear_error(&error);
+               if (dbus_g_proxy_call_with_timeout(dbus,
+                                                  "GetVersion",
+                                                  500,
+                                                  &error,
+                                                  G_TYPE_INVALID,
+                                                  G_TYPE_STRING,
+                                                  &version,
+                                                  G_TYPE_INVALID))
+                       break;
+       }
+       if (tries == 0)
+               error_exit(_("GetVersion"), error);
+       g_message(_("Connected to %s-%s"), "dhcpcd-dbus", version);
        g_free(version);
 
-       gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
+       gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
        online = FALSE;
        menu_init(status_icon);
 
-       if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
+       if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
                               G_TYPE_INVALID,
                               G_TYPE_STRING, &version, G_TYPE_INVALID))
-               error_exit("GetStatus", error);
+               error_exit(_("GetStatus"), error);
        check_status(version);
        g_free(version);
 
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
-       dbus_g_proxy_add_signal(bus_proxy, "Event",
+       dbus_g_proxy_add_signal(dbus, "Event",
                                otype, G_TYPE_INVALID);
-       dbus_g_proxy_connect_signal(bus_proxy, "Event",
+       dbus_g_proxy_connect_signal(dbus, "Event",
                                    G_CALLBACK(dhcpcd_event),
                                    NULL, NULL);
-       dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
+       dbus_g_proxy_add_signal(dbus, "StatusChanged",
                                G_TYPE_STRING, G_TYPE_INVALID);
-       dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
+       dbus_g_proxy_connect_signal(dbus, "StatusChanged",
                                    G_CALLBACK(dhcpcd_status),
                                    NULL, NULL);