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 4ad09fe5767fc21b34d26944b1723fb5576e1671..b6acd2087dc8036c2baab9c925260bbb7eac13f9 100644 (file)
--- a/main.c
+++ b/main.c
  * maybe use network-idle -> network-transmit ->
  * network-receive -> network-transmit-receive */
 
-#include <arpa/inet.h>
-
+#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;
-       gboolean wireless;
-       char *ssid;
-};
+DBusGProxy *dbus = NULL;
+GList *interfaces = NULL;
 
-static DBusGProxy *bus_proxy;
-static GtkStatusIcon *status_icon;
-static GList *interfaces;
+static GtkStatusIcon *status_icon = NULL;
+static NotifyNotification *nn;
 static gboolean online;
 static gboolean carrier;
-static NotifyNotification *nn;
-
 static char **interface_order;
 
 const char *const up_reasons[] = {
@@ -92,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)
 {
@@ -144,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)
 {
@@ -177,6 +198,8 @@ 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;
 }
 
@@ -202,27 +225,27 @@ print_if_msg(const struct if_msg *ifm)
        showip = TRUE;
        showssid = FALSE;
        if (if_up(ifm))
-               reason = "Acquired address";
+               reason = N_("Acquired address");
        else {
                if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
-                       reason = "Failed to renew";
+                       reason = N_("Failed to renew");
                else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
                        if (ifm->wireless) {
-                               reason = "Asssociated with";
+                               reason = N_("Asssociated with");
                                if (ifm->ssid != NULL)
                                        showssid = TRUE;
                        } else
-                               reason = "Cable plugged in";
+                               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 = "Lost association with";
+                                       reason = N_("Lost association with");
                                        showssid = TRUE;
                                } else
-                                   reason = "Not associated";
+                                   reason = N_("Not associated");
                        } else
-                               reason = "Cable unplugged";
+                               reason = N_("Cable unplugged");
                        showip = FALSE;
                }
        }
@@ -388,23 +411,23 @@ dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
        msg = print_if_msg(ifm);
        title = NULL;
        if (if_up(ifm))
-               act = "Connected to ";
+               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);
        }
 
@@ -438,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);
@@ -449,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);
@@ -480,9 +503,9 @@ check_status(const char *status)
                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;
@@ -500,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();
 }
@@ -523,31 +546,37 @@ main(int argc, char *argv[])
        char *version = NULL;
        GType otype;
        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_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_tooltip(status_icon, _("Connecting to dhcpcd ..."));
        gtk_status_icon_set_visible(status_icon, TRUE);
 
        notify_init(PACKAGE);
 
-       g_message("Connecting to dbus ...");
+       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);
 
-       g_message("Connecting to dhcpcd-dbus ...");
+       g_message(_("Connecting to dhcpcd-dbus ..."));
        while (--tries > 0) {
                g_clear_error(&error);
-               if (dbus_g_proxy_call_with_timeout(bus_proxy,
+               if (dbus_g_proxy_call_with_timeout(dbus,
                                                   "GetVersion",
                                                   500,
                                                   &error,
@@ -558,30 +587,30 @@ main(int argc, char *argv[])
                        break;
        }
        if (tries == 0)
-               error_exit("GetVersion", error);
-       g_message("Connected to dhcpcd-dbus-%s", version);
+               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);