Re-work around dhcpcd-dbus-0.2
authorRoy Marples <roy@marples.name>
Mon, 2 Feb 2009 17:39:48 +0000 (17:39 +0000)
committerRoy Marples <roy@marples.name>
Mon, 2 Feb 2009 17:39:48 +0000 (17:39 +0000)
config.h
dhcpcd-gtk.h
main.c
menu.c

index 737927deb74ba11a326d1d70345344cd9a5d3279..001567f44856c18333272482b95faa9c634bedc0 100644 (file)
--- a/config.h
+++ b/config.h
@@ -28,7 +28,7 @@
 #define CONFIG_H
 
 #define PACKAGE                        "dhcpcd-gtk"
-#define VERSION                        "0.1.4"
+#define VERSION                        "0.2.0"
 
 #define DHCPCD_SERVICE         "name.marples.roy.dhcpcd"
 #define DHCPCD_PATH            "/name/marples/roy/dhcpcd"
index acdc7e35a82d01c8572e2f17b965a1602095c10c..10bf0082f8273c680e4972e39ab0bef168ff7469 100644 (file)
@@ -54,6 +54,8 @@
 struct if_ap {
        char *bssid;
        int freq;
+       int qual;
+       int noise;
        int level;
        char *flags;
        char *ssid;
@@ -73,5 +75,4 @@ extern DBusGProxy *dus;
 extern GList *interfaces;
 
 void notify_close(void);
-GSList *get_scan_results(const char *);
 #endif
diff --git a/main.c b/main.c
index ad4ceb33d51295bd9fc5695cbe179c567bab4be6..d933da692121ca5560c8e0f1c973e920c370fb9f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -77,16 +77,44 @@ ignore_if_msg(const struct if_msg *ifm)
        return FALSE;
 }
 
+static struct if_msg *
+find_if_msg(const char *iface)
+{
+       GList *gl;
+       struct if_msg *ifm;
+
+       for (gl = interfaces; gl; gl = gl->next) {
+               ifm = (struct if_msg *)gl->data;
+               if (g_strcmp0(ifm->name, iface) == 0)
+                       return ifm;
+       }
+       return NULL;
+}
+
 static void
 free_if_msg(struct if_msg *ifm)
 {
+       GSList *gl;
+
        g_free(ifm->name);
        g_free(ifm->reason);
        g_free(ifm->ssid);
+       for (gl = ifm->scan_results; gl; gl = gl->next)
+               g_free(gl->data);
+       g_slist_free(ifm->scan_results);
        g_free(ifm);
 }
 
 static void
+free_if_ap(struct if_ap *ifa)
+{
+       g_free(ifa->bssid);
+       g_free(ifa->flags);
+       g_free(ifa->ssid);
+       g_free(ifa);
+}
+
+static void
 error_exit(const char *msg, GError *error)
 {
        GtkWidget *dialog;
@@ -117,53 +145,39 @@ error_exit(const char *msg, GError *error)
                exit(EXIT_FAILURE);
 }
 
-GSList *
-get_scan_results(const char *iface)
+static GSList *
+get_scan_results(const GPtrArray *array)
 {
-       GType otype;
-       GPtrArray *array = NULL;
-       GValueArray *item;
-       GError *error = NULL;
+       GHashTable *config;
        GSList *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;
-       }
+       GValue *val;
 
-       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));
+               config = g_ptr_array_index(array, i);
+               val = g_hash_table_lookup(config, "BSSID");
+               if (val == NULL)
+                       continue;
+               ifa = g_malloc0(sizeof(*ifa));
+               ifa->bssid = g_strdup(g_value_get_string(val));
+               val = g_hash_table_lookup(config, "Frequency");
+               if (val)
+                       ifa->freq = g_value_get_int(val);
+               val = g_hash_table_lookup(config, "Noise");
+               if (val)
+                       ifa->noise = g_value_get_int(val);
+               val = g_hash_table_lookup(config, "Level");
+               if (val)
+                       ifa->level = g_value_get_int(val);
+               val = g_hash_table_lookup(config, "Flags");
+               if (val)
+                       ifa->flags = g_strdup(g_value_get_string(val));
+               val = g_hash_table_lookup(config, "SSID");
+               if (val)
+                       ifa->ssid = g_strdup(g_value_get_string(val));
                list = g_slist_append(list, ifa);
        }
-       g_ptr_array_free(array, TRUE);
        return list;
 }
 
@@ -200,8 +214,6 @@ 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;
 }
 
@@ -484,7 +496,7 @@ dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
                notify(title, msg, GTK_STOCK_NETWORK);
                g_free(title);
        } else
-               notify("Interface event", msg, GTK_STOCK_NETWORK);
+               notify(N_("Interface event"), msg, GTK_STOCK_NETWORK);
        g_free(msg);
 }
 
@@ -507,6 +519,10 @@ dhcpcd_get_interfaces()
        GError *error = NULL;
        GType otype;
        char *msg;
+       GList *gl;
+       GSList *gsl;
+       GPtrArray *array;
+       struct if_msg *ifm;
 
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
@@ -526,6 +542,27 @@ dhcpcd_get_interfaces()
                               G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
                error_exit("ListInterfaces", error);
        interfaces = g_list_sort(interfaces, if_msg_comparer);
+
+       otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
+       otype = dbus_g_type_get_collection("GPtrArray", otype);
+       for (gl = interfaces; gl; gl = gl->next) {
+               ifm = (struct if_msg *)gl->data;
+               if (!ifm->wireless)
+                       continue;
+               if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
+                                      G_TYPE_STRING, ifm->name, G_TYPE_INVALID,
+                                      otype, &array, G_TYPE_INVALID)) 
+               {
+                       g_message("GetScanResults: %s", error->message);
+                       g_clear_error(&error);
+                       continue;
+               }
+               for (gsl = ifm->scan_results; gsl; gsl = gsl->next)
+                       g_free(gsl->data);
+               g_slist_free(ifm->scan_results);
+               ifm->scan_results = get_scan_results(array);
+       }
+
        msg = NULL;
        update_online(&msg);
        // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
@@ -587,6 +624,22 @@ dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
        check_status(status);
 }
 
+static void
+dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface, GPtrArray *array, _unused void *data)
+{
+       struct if_msg *ifm;
+       GSList *gl;
+
+       ifm = find_if_msg(iface);
+       if (ifm == NULL)
+               return;
+       g_message(_("%s: Received scan results"), ifm->name);
+       for (gl = ifm->scan_results; gl; gl = gl->next)
+               free_if_ap((struct if_ap *)gl->data);
+       g_slist_free(ifm->scan_results);
+       ifm->scan_results = get_scan_results(array);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -657,12 +710,17 @@ main(int argc, char *argv[])
                                otype, G_TYPE_INVALID);
        dbus_g_proxy_connect_signal(dbus, "Event",
                                    G_CALLBACK(dhcpcd_event),
-                                   NULL, NULL);
+                                   bus, NULL);
        dbus_g_proxy_add_signal(dbus, "StatusChanged",
                                G_TYPE_STRING, G_TYPE_INVALID);
        dbus_g_proxy_connect_signal(dbus, "StatusChanged",
                                    G_CALLBACK(dhcpcd_status),
-                                   NULL, NULL);
+                                   bus, NULL);
+       dbus_g_proxy_add_signal(dbus, "ScanResults",
+                               G_TYPE_STRING, G_TYPE_INVALID);
+       dbus_g_proxy_connect_signal(dbus, "ScanResults",
+                                   G_CALLBACK(dhcpcd_scan_results),
+                                   bus, NULL);
 
        gtk_main();
        return 0;
diff --git a/menu.c b/menu.c
index f9e4721cf8dec2e8616b854521634354c74aff08..1a658088277aba983d3c6feafe98b00c4d688f02 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -96,9 +96,16 @@ add_scan_results(GtkMenu *menu, const struct if_msg *ifm)
        for (gl = ifm->scan_results; gl; gl = gl->next) {
                ifa = (const struct if_ap *)gl->data;
                item = gtk_image_menu_item_new_with_label(ifa->ssid);
-               image = gtk_image_new_from_icon_name("network-wireless",
-                                                    GTK_ICON_SIZE_MENU);
-               gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
+               image = NULL;
+               if (g_strcmp0(ifm->ssid, ifa->ssid) == 0)
+                       image = gtk_image_new_from_icon_name("network-wireless",
+                                                            GTK_ICON_SIZE_MENU);
+               if (!image && ifa->flags != NULL)
+                       image = gtk_image_new_from_icon_name("lock",
+                                                            GTK_ICON_SIZE_MENU);
+               if (image)
+                       gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
+                                                     image);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        }
 }