We finally work with PSK/WEPKEY_0 entry and we now save our config.
authorRoy Marples <roy@marples.name>
Wed, 4 Feb 2009 20:40:08 +0000 (20:40 +0000)
committerRoy Marples <roy@marples.name>
Wed, 4 Feb 2009 20:40:08 +0000 (20:40 +0000)
dhcpcd-gtk.h
main.c
menu.c
wpa.c
wpa.h

index 1894d68c9a9308ce86eb49b9f4bae6889c981434..db4b5ce8ae661cd2ddd2fa4487099412e1ada85c 100644 (file)
@@ -52,6 +52,7 @@
 #endif
 
 struct if_ap {
+       char *ifname;
        char *bssid;
        int frequency;
        int quality;
@@ -62,7 +63,7 @@ struct if_ap {
 };
 
 struct if_msg {
-       char *name;
+       char *ifname;
        char *reason;
        struct in_addr ip;
        unsigned char cidr;
diff --git a/main.c b/main.c
index 3f00718ce9316d85511eccad6d711f2bd2ba2b32..edb4db6fedf0d939368a6131a1dfd0abfe57b444 100644 (file)
--- a/main.c
+++ b/main.c
@@ -85,7 +85,7 @@ find_if_msg(const char *iface)
 
        for (gl = interfaces; gl; gl = gl->next) {
                ifm = (struct if_msg *)gl->data;
-               if (g_strcmp0(ifm->name, iface) == 0)
+               if (g_strcmp0(ifm->ifname, iface) == 0)
                        return ifm;
        }
        return NULL;
@@ -94,6 +94,7 @@ find_if_msg(const char *iface)
 static void
 free_if_ap(struct if_ap *ifa)
 {
+       g_free(ifa->ifname);
        g_free(ifa->bssid);
        g_free(ifa->flags);
        g_free(ifa->ssid);
@@ -105,7 +106,7 @@ free_if_msg(struct if_msg *ifm)
 {
        GSList *gl;
 
-       g_free(ifm->name);
+       g_free(ifm->ifname);
        g_free(ifm->reason);
        g_free(ifm->ssid);
        for (gl = ifm->scan_results; gl; gl = gl->next)
@@ -160,10 +161,10 @@ get_scan_results(struct if_msg *ifm)
        otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
        otype = dbus_g_type_get_collection("GPtrArray", otype);
 
-       if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
-                              G_TYPE_STRING, ifm->name, G_TYPE_INVALID,
+       if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
+                              G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
                               otype, &array, G_TYPE_INVALID))
-               error_exit(_("GetScanResults"), error);
+               error_exit(_("ScanResults"), error);
 
        for (i = 0; i < array->len; i++) {
                config = g_ptr_array_index(array, i);
@@ -171,6 +172,7 @@ get_scan_results(struct if_msg *ifm)
                if (val == NULL)
                        continue;
                ifa = g_malloc0(sizeof(*ifa));
+               ifa->ifname = g_strdup(ifm->ifname);
                ifa->bssid = g_strdup(g_value_get_string(val));
                val = g_hash_table_lookup(config, "Frequency");
                if (val)
@@ -205,7 +207,7 @@ make_if_msg(GHashTable *config)
        if (val == NULL)
                return NULL;
        ifm = g_malloc0(sizeof(*ifm));
-       ifm->name = g_strdup(g_value_get_string(val));
+       ifm->ifname = g_strdup(g_value_get_string(val));
        val = g_hash_table_lookup(config, "Reason");
        if (val)
                ifm->reason = g_strdup(g_value_get_string(val));
@@ -280,7 +282,7 @@ print_if_msg(const struct if_msg *ifm)
        if (reason == NULL)
                reason = ifm->reason;
        
-       len = strlen(ifm->name) + 3;
+       len = strlen(ifm->ifname) + 3;
        len += strlen(reason) + 1;
        if (ifm->ip.s_addr != 0) {
                len += 16; /* 000. * 4 */
@@ -290,7 +292,7 @@ print_if_msg(const struct if_msg *ifm)
        if (showssid)
                len += strlen(ifm->ssid) + 1;
        msg = p = g_malloc(len);
-       p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
+       p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
        if (showssid)
                p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
        if (ifm->ip.s_addr != 0 && showip) {
@@ -310,9 +312,9 @@ if_msg_comparer(gconstpointer a, gconstpointer b)
        ifa = (const struct if_msg *)a;
        ifb = (const struct if_msg *)b;
        for (order = (const char *const *)interface_order; *order; order++) {
-               if (g_strcmp0(*order, ifa->name) == 0)
+               if (g_strcmp0(*order, ifa->ifname) == 0)
                        return -1;
-               if (g_strcmp0(*order, ifb->name) == 0)
+               if (g_strcmp0(*order, ifb->ifname) == 0)
                        return 1;
        }
        return 0;
@@ -464,7 +466,7 @@ dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
        ifp = NULL;
        for (gl = interfaces; gl; gl = gl->next) {
                ifp = (struct if_msg *)gl->data;
-               if (g_strcmp0(ifp->name, ifm->name) == 0) {
+               if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
                        ifm->scan_results = ifp->scan_results;
                        ifp->scan_results = NULL;
                        free_if_msg(ifp);
@@ -565,11 +567,11 @@ dhcpcd_get_interfaces()
                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,
+               if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
+                                      G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
                                       otype, &array, G_TYPE_INVALID)) 
                {
-                       g_message("GetScanResults: %s", error->message);
+                       g_message("ScanResults: %s", error->message);
                        g_clear_error(&error);
                        continue;
                }
@@ -651,7 +653,7 @@ dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface, _unused void *
        ifm = find_if_msg(iface);
        if (ifm == NULL)
                return;
-       g_message(_("%s: Received scan results"), ifm->name);
+       g_message(_("%s: Received scan results"), ifm->ifname);
        aps = get_scan_results(ifm);
        txt = NULL;
        for (gl = aps; gl; gl = gl->next) {
diff --git a/menu.c b/menu.c
index 317628def2519d2c2b3e32e12d53c238d5915825..0145890a110cf7f61ed881ce26298f553c8c26f2 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -74,7 +74,7 @@ url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
 static void
 ssid_hook(_unused GtkMenuItem *item, gpointer data)
 {
-       wpa_configure((const char *)data);
+       wpa_configure((const struct if_ap *)data);
 }
 
 static void
@@ -134,7 +134,7 @@ add_scan_results(GtkMenu *menu, const struct if_msg *ifm)
                gtk_widget_show(image);
                gtk_widget_show(box);
                g_signal_connect(G_OBJECT(item), "activate",
-                               G_CALLBACK(ssid_hook), ifa->ssid);
+                               G_CALLBACK(ssid_hook), gl->data);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        }
 }
diff --git a/wpa.c b/wpa.c
index 6482f1edcd7ecde073855a8d1c16c32a2f04bd7c..6a0a0609624a0fff5eea50ac8e8aa3dd2fd9ee48 100644 (file)
--- a/wpa.c
+++ b/wpa.c
@@ -48,11 +48,11 @@ find_network(const char *ifname, const char *ssid)
                        G_TYPE_INVALID);
        otype = dbus_g_type_get_collection("GPtrArray", otype);
        error = NULL;
-       if (!dbus_g_proxy_call(dbus, "GetNetworks", &error,
+       if (!dbus_g_proxy_call(dbus, "ListNetworks", &error,
                               G_TYPE_STRING, ifname, G_TYPE_INVALID,
                               otype, &array, G_TYPE_INVALID))
        {
-               g_warning("GetNetworks: %s", error->message);
+               g_warning("ListNetworks: %s", error->message);
                g_error_free(error);
                return -1;
        }
@@ -77,7 +77,7 @@ find_network(const char *ifname, const char *ssid)
        }
 
        nssid = g_strconcat("\"", ssid, "\"", NULL);
-       if (!dbus_g_proxy_call(dbus, "SetNetworkParameter", &error,
+       if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
                               G_TYPE_STRING, ifname,
                               G_TYPE_INT, id,
                               G_TYPE_STRING, "ssid",
@@ -85,7 +85,7 @@ find_network(const char *ifname, const char *ssid)
                               G_TYPE_INVALID,
                               G_TYPE_INVALID))
        {
-               g_warning("SetNetworkParameter: %s", error->message);
+               g_warning("SetNetwork: %s", error->message);
                g_free(nssid);
                g_error_free(error);
                return -1;
@@ -100,10 +100,12 @@ configure_network(const char *ifname, int id, const char *var, const char *val)
 {
        GError *error;
        char *str;
+       static gboolean warned = FALSE;
+       GtkWidget *dialog;
 
        error = NULL;
        str = g_strconcat("\"", val, "\"", NULL);
-       if (!dbus_g_proxy_call(dbus, "SetNetworkParameter", &error,
+       if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
                               G_TYPE_STRING, ifname,
                               G_TYPE_INT, id,
                               G_TYPE_STRING, var,
@@ -111,7 +113,7 @@ configure_network(const char *ifname, int id, const char *var, const char *val)
                               G_TYPE_INVALID,
                               G_TYPE_INVALID))
        {
-               g_warning("SetNetworkParameter: %s", error->message);
+               g_warning("SetNetwork: %s", error->message);
                g_free(str);
                g_error_free(error);
                return -1;
@@ -129,19 +131,38 @@ configure_network(const char *ifname, int id, const char *var, const char *val)
                return -1;
        }
 
+       if (!dbus_g_proxy_call(dbus, "SaveConfig", &error,
+                              G_TYPE_STRING, ifname,
+                              G_TYPE_INVALID,
+                              G_TYPE_INVALID))
+       {
+               g_warning("SaveConfig: %s", error->message);
+               if (!warned) {
+                       warned = TRUE;
+                       dialog = gtk_message_dialog_new(NULL,
+                                                       GTK_DIALOG_MODAL,
+                                                       GTK_MESSAGE_ERROR,
+                                                       GTK_BUTTONS_OK,
+                                                       _("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.\nThis warning will not appear again until program restarted."));
+                       gtk_window_set_title(GTK_WINDOW(dialog),
+                                            _("Error saving configuration"));
+                       gtk_dialog_run(GTK_DIALOG(dialog));
+                       gtk_widget_destroy(dialog);
+               }
+               g_error_free(error);
+       }
        return 0;
 }
 
 gboolean
-wpa_configure(const char *ssid)
+wpa_configure(const struct if_ap *ifa)
 {
        GtkWidget *dialog, *label, *psk, *vbox, *hbox;
        char *title;
+       const char *var;
        gint result, id;
 
-       const char *ifname = "iwi0";
-
-       title = g_strdup_printf(_("Configuration for %s"), ssid);
+       title = g_strdup_printf(_("Configuration for %s"), ifa->ssid);
        dialog = gtk_dialog_new_with_buttons(title,
                NULL,
                GTK_DIALOG_MODAL,
@@ -149,10 +170,12 @@ wpa_configure(const char *ssid)
                GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                NULL);
        g_free(title);
+       gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
+       gtk_window_set_icon_name(GTK_WINDOW(dialog), "config-users");
        vbox = GTK_DIALOG(dialog)->vbox;
 
        hbox = gtk_hbox_new(FALSE, 2);
-       label = gtk_label_new("Pre Shared Key:");
+       label = gtk_label_new(_("Pre Shared Key:"));
        gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
        psk = gtk_entry_new();
        gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
@@ -164,10 +187,14 @@ wpa_configure(const char *ssid)
        
        id = -1;
        if (result == GTK_RESPONSE_ACCEPT) {
-               id = find_network(ifname, ssid);
+               id = find_network(ifa->ifname, ifa->ssid);
+               if (g_strcmp0(ifa->flags, "[WEP]") == 0)
+                       var = "wep_key0";
+               else
+                       var = "psk";
                if (id != -1)
-                       id = configure_network(ifname, id, "psk",
-                                       gtk_entry_get_text(GTK_ENTRY(psk)));
+                       id = configure_network(ifa->ifname, id, var,
+                                              gtk_entry_get_text(GTK_ENTRY(psk)));
        }
        gtk_widget_destroy(dialog);
        return id == -1 ? FALSE : TRUE;
diff --git a/wpa.h b/wpa.h
index 0e0911f28d72e407f07b446103ec6e44c812b27c..b490aa2fef101b6650f21ebda829ced51b9b967c 100644 (file)
--- a/wpa.h
+++ b/wpa.h
@@ -27,6 +27,8 @@
 #ifndef WPA_H
 #define WPA_H
 
-gboolean wpa_configure(const char *ssid);
+#include "dhcpcd-gtk.h"
+
+gboolean wpa_configure(const struct if_ap *);
 
 #endif