We now have a working preference screen and can rebind the interface!
[dhcpcd-ui] / wpa.c
diff --git a/wpa.c b/wpa.c
index c483837eff8a42d871f7be5e08f9a5c90cbd2f72..27ec8eb50ed2d1ecbda75821b35a71d180478575 100644 (file)
--- a/wpa.c
+++ b/wpa.c
 #include "dhcpcd-gtk.h"
 #include "wpa.h"
 
-static gint
+static int
 find_network(const char *ifname, const char *ssid)
 {
        GType otype;
        GError *error;
-       gint id;
+       int id;
        size_t i;
        GPtrArray *array;
        GValueArray *varray;
@@ -41,16 +41,16 @@ find_network(const char *ifname, const char *ssid)
        char *nssid;
 
        otype = dbus_g_type_get_struct("GValueArray",
-                       G_TYPE_INT,
-                       G_TYPE_STRING,
-                       G_TYPE_STRING,
-                       G_TYPE_STRING,
-                       G_TYPE_INVALID);
+           G_TYPE_INT,
+           G_TYPE_STRING,
+           G_TYPE_STRING,
+           G_TYPE_STRING,
+           G_TYPE_INVALID);
        otype = dbus_g_type_get_collection("GPtrArray", otype);
        error = NULL;
        if (!dbus_g_proxy_call(dbus, "ListNetworks", &error,
-                              G_TYPE_STRING, ifname, G_TYPE_INVALID,
-                              otype, &array, G_TYPE_INVALID))
+               G_TYPE_STRING, ifname, G_TYPE_INVALID,
+               otype, &array, G_TYPE_INVALID))
        {
                g_warning("ListNetworks: %s", error->message);
                g_error_free(error);
@@ -68,8 +68,8 @@ find_network(const char *ifname, const char *ssid)
        }
 
        if (!dbus_g_proxy_call(dbus, "AddNetwork", &error,
-                              G_TYPE_STRING, ifname, G_TYPE_INVALID,
-                              G_TYPE_INT, &id, G_TYPE_INVALID))
+               G_TYPE_STRING, ifname, G_TYPE_INVALID,
+               G_TYPE_INT, &id, G_TYPE_INVALID))
        {
                g_warning("AddNetwork: %s", error->message);
                g_error_free(error);
@@ -78,12 +78,12 @@ find_network(const char *ifname, const char *ssid)
 
        nssid = g_strconcat("\"", ssid, "\"", NULL);
        if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
-                              G_TYPE_STRING, ifname,
-                              G_TYPE_INT, id,
-                              G_TYPE_STRING, "ssid",
-                              G_TYPE_STRING, nssid,
-                              G_TYPE_INVALID,
-                              G_TYPE_INVALID))
+               G_TYPE_STRING, ifname,
+               G_TYPE_INT, id,
+               G_TYPE_STRING, "ssid",
+               G_TYPE_STRING, nssid,
+               G_TYPE_INVALID,
+               G_TYPE_INVALID))
        {
                g_warning("SetNetwork: %s", error->message);
                g_free(nssid);
@@ -96,42 +96,60 @@ find_network(const char *ifname, const char *ssid)
 }
 
 static int
-configure_network(const char *ifname, int id, const char *var, const char *val,
-               gboolean quote)
+configure_network(const char *ifname, int id, const char *mgmt,
+    const char *var, const char *val,
+    bool quote)
 {
        GError *error;
        char *str;
-       static gboolean warned = FALSE;
+       static bool warned = false;
        GtkWidget *dialog;
 
        if (id == -1)
                return -1;
 
+       dbus_g_proxy_call(dbus, "SetNetwork", &error,
+           G_TYPE_STRING, ifname,
+           G_TYPE_INT, id,
+           G_TYPE_STRING, "key_mgmt",
+           G_TYPE_STRING, mgmt,
+           G_TYPE_INVALID,
+           G_TYPE_INVALID);
+
        error = NULL;
        if (quote)
                str = g_strconcat("\"", val, "\"", NULL);
        else
                str = NULL;
        if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
-                              G_TYPE_STRING, ifname,
-                              G_TYPE_INT, id,
-                              G_TYPE_STRING, var,
-                              G_TYPE_STRING, quote ? str : val,
-                              G_TYPE_INVALID,
-                              G_TYPE_INVALID))
+               G_TYPE_STRING, ifname,
+               G_TYPE_INT, id,
+               G_TYPE_STRING, var,
+               G_TYPE_STRING, quote ? str : val,
+               G_TYPE_INVALID,
+               G_TYPE_INVALID))
        {
                g_warning("SetNetwork: %s", error->message);
                g_free(str);
                g_error_free(error);
+               dialog = gtk_message_dialog_new(NULL,
+                   GTK_DIALOG_MODAL,
+                   GTK_MESSAGE_ERROR,
+                   GTK_BUTTONS_OK,
+                   _("Failed to set password, probably too short."));
+               gtk_window_set_title(GTK_WINDOW(dialog),
+                   _("Error setting password"));
+               gtk_dialog_run(GTK_DIALOG(dialog));
+               gtk_widget_destroy(dialog);
                return -1;
        }
        g_free(str);
 
        if (!dbus_g_proxy_call(dbus, "EnableNetwork", &error,
-                              G_TYPE_STRING, ifname,
-                              G_TYPE_INT, id,
-                              G_TYPE_INVALID,
-                              G_TYPE_INVALID))
+               G_TYPE_STRING, ifname,
+               G_TYPE_INT, id,
+               G_TYPE_INVALID,
+               G_TYPE_INVALID))
        {
                g_warning("EnableNetwork: %s", error->message);
                g_error_free(error);
@@ -139,51 +157,79 @@ configure_network(const char *ifname, int id, const char *var, const char *val,
        }
 
        if (!dbus_g_proxy_call(dbus, "SaveConfig", &error,
-                              G_TYPE_STRING, ifname,
-                              G_TYPE_INVALID,
-                              G_TYPE_INVALID))
+               G_TYPE_STRING, ifname,
+               G_TYPE_INVALID,
+               G_TYPE_INVALID))
        {
                g_warning("SaveConfig: %s", error->message);
                if (!warned) {
-                       warned = TRUE;
+                       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_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"));
+                           _("Error saving configuration"));
                        gtk_dialog_run(GTK_DIALOG(dialog));
                        gtk_widget_destroy(dialog);
                }
                g_error_free(error);
        }
+/*
+  if (!dbus_g_proxy_call(dbus, "Disconnect", &error,
+  G_TYPE_STRING, ifname,
+  G_TYPE_INVALID,
+  G_TYPE_INVALID))
+  {
+  g_warning("Disconnect: %s", error->message);
+  g_error_free(error);
+  }
+*/
+       if (!dbus_g_proxy_call(dbus, "Reassociate", &error,
+               G_TYPE_STRING, ifname,
+               G_TYPE_INVALID,
+               G_TYPE_INVALID))
+       {
+               g_warning("Reassociate: %s", error->message);
+               g_error_free(error);
+       }
+       
        return 0;
 }
 
-gboolean
+static void
+onEnter(_unused GtkWidget *widget, gpointer *data)
+{
+       gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
+}
+
+bool
 wpa_configure(const struct if_ap *ifa)
 {
        GtkWidget *dialog, *label, *psk, *vbox, *hbox;
        const char *var, *mgt;
-       gint result, id, retval;
+       int result, id, retval;
 
        dialog = gtk_dialog_new_with_buttons(ifa->ssid,
-               NULL,
-               GTK_DIALOG_MODAL,
-               GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
-               GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
-               NULL);
-       gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
+           NULL,
+           GTK_DIALOG_MODAL,
+           GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+           GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
+           NULL);
+       gtk_window_set_resizable(GTK_WINDOW(dialog), false);
        gtk_window_set_icon_name(GTK_WINDOW(dialog), "config-users");
+       gtk_dialog_set_default_response(GTK_DIALOG(dialog),
+           GTK_RESPONSE_ACCEPT);
        vbox = GTK_DIALOG(dialog)->vbox;
 
-       hbox = gtk_hbox_new(FALSE, 2);
+       hbox = gtk_hbox_new(false, 2);
        label = gtk_label_new(_("Pre Shared Key:"));
-       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 0);
        psk = gtk_entry_new();
        gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
-       gtk_box_pack_start(GTK_BOX(hbox), psk, TRUE, TRUE, 0);
+       g_signal_connect(G_OBJECT(psk), "activate", G_CALLBACK(onEnter), dialog);
+       gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 0);
        gtk_container_add(GTK_CONTAINER(vbox), hbox);
 
        gtk_widget_show_all(dialog);
@@ -201,12 +247,10 @@ wpa_configure(const struct if_ap *ifa)
                        var = "psk";
                }
                if (id != -1) {
-                       retval = configure_network(ifa->ifname, id, "key_mgmt", 
-                                              mgt, FALSE);
-                       retval |= configure_network(ifa->ifname, id, var,
-                                              gtk_entry_get_text(GTK_ENTRY(psk)), TRUE);
+                       retval = configure_network(ifa->ifname, id, mgt, var,
+                           gtk_entry_get_text(GTK_ENTRY(psk)), true);
                }
        }
        gtk_widget_destroy(dialog);
-       return retval == -1 ? FALSE : TRUE;
+       return retval == -1 ? false : true;
 }