Support both dhcp and manual configurations.
Support realtime status update.

Signed-off-by: Dongxiao Xu <dongxiao...@intel.com>
---
 common/connman-client.c |   92 +++++++++-----
 common/connman-client.h |   16 ++-
 common/connman-dbus.c   |   68 +++-------
 common/connman-dbus.xml |    2 +
 properties/Makefile.am  |    2 +-
 properties/advanced.h   |   13 ++-
 properties/main.c       |   91 ++++++++++----
 properties/wifi.c       |  315 ++++++++++++++++++++++++++++++++++++-----------
 8 files changed, 414 insertions(+), 185 deletions(-)

diff --git a/common/connman-client.c b/common/connman-client.c
index 3c203cb..915931e 100644
--- a/common/connman-client.c
+++ b/common/connman-client.c
@@ -114,10 +114,10 @@ static void connman_client_init(ConnmanClient *client)
                                G_TYPE_STRING,  /* name */
                                G_TYPE_STRING,  /* icon */
                                G_TYPE_UINT,    /* type */
-                               G_TYPE_UINT,    /* state */
+                               G_TYPE_STRING,  /* state */
                                G_TYPE_BOOLEAN, /* favorite */
                                G_TYPE_UINT,    /* strength */
-                               G_TYPE_UINT,    /* security */
+                               G_TYPE_STRING,  /* security */
                                G_TYPE_STRING,  /* passphrase */
                                G_TYPE_STRING,  /* method */
                                G_TYPE_STRING,  /* address */
@@ -256,30 +256,6 @@ GtkTreeModel 
*connman_client_get_device_model(ConnmanClient *client)
        return model;
 }
 
-void connman_client_set_policy(ConnmanClient *client, const gchar *device,
-                                                       const gchar *policy)
-{
-       ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
-       DBusGProxy *proxy;
-       GValue value = { 0 };
-
-       DBG("client %p", client);
-
-       if (device == NULL)
-               return;
-
-       proxy = connman_dbus_get_proxy(priv->store, device);
-       if (proxy == NULL)
-               return;
-
-       g_value_init(&value, G_TYPE_STRING);
-       g_value_set_string(&value, policy);
-
-       connman_set_property(proxy, "Policy", &value, NULL);
-
-       g_object_unref(proxy);
-}
-
 gboolean connman_client_set_ipv4(ConnmanClient *client, const gchar *device,
                                struct ipv4_config *ipv4_config)
 {
@@ -391,18 +367,18 @@ static gboolean network_disconnect(GtkTreeModel *model, 
GtkTreePath *path,
                                        -1);
 
        if (proxy == NULL)
-               return FALSE;
+               return TRUE;
 
        if (g_str_equal(dbus_g_proxy_get_interface(proxy),
                                        CONNMAN_SERVICE_INTERFACE) == FALSE)
-               return FALSE;
+               return TRUE;
 
        if (type == CONNMAN_TYPE_WIFI)
                connman_disconnect(proxy, NULL);
 
        g_object_unref(proxy);
 
-       return TRUE;
+       return FALSE;
 }
 
 void connman_client_connect(ConnmanClient *client, const gchar *network)
@@ -427,6 +403,31 @@ void connman_client_connect(ConnmanClient *client, const 
gchar *network)
        g_object_unref(proxy);
 }
 
+void connman_client_connect_async(ConnmanClient *client, const gchar *network,
+               connman_connect_reply callback, gpointer userdata)
+{
+       ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
+       DBusGProxy *proxy;
+
+       DBG("client %p", client);
+       DBG("network %s", network);
+
+       if (network == NULL)
+               goto done;
+
+       gtk_tree_model_foreach(GTK_TREE_MODEL(priv->store),
+                       network_disconnect, NULL);
+
+       proxy = connman_dbus_get_proxy(priv->store, network);
+       if (proxy == NULL)
+               goto done;
+
+       connman_connect_async(proxy, callback, userdata);
+
+done:
+       return;
+}
+
 static void connman_client_disconnect_all(ConnmanClient *client)
 {
        ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
@@ -456,11 +457,11 @@ void connman_client_disconnect(ConnmanClient *client, 
const gchar *network)
        g_object_unref(proxy);
 }
 
-guint connman_client_get_security(ConnmanClient *client, const gchar *network)
+gchar *connman_client_get_security(ConnmanClient *client, const gchar *network)
 {
        ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
        GtkTreeIter iter;
-       guint security;
+       gchar *security;
 
        DBG("client %p", client);
 
@@ -496,30 +497,34 @@ gchar *connman_client_get_passphrase(ConnmanClient 
*client, const gchar *network
        return passphrase;
 }
 
-void connman_client_set_passphrase(ConnmanClient *client, const gchar *network,
+gboolean connman_client_set_passphrase(ConnmanClient *client, const gchar 
*network,
                                                const gchar *passphrase)
 {
        ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
        DBusGProxy *proxy;
        GValue value = { 0 };
+       gboolean ret = FALSE;
 
        DBG("client %p", client);
 
        if (network == NULL)
-               return;
+               goto done;
 
        proxy = connman_dbus_get_proxy(priv->store, network);
        if (proxy == NULL)
-               return;
+               goto done;
 
        g_value_init(&value, G_TYPE_STRING);
        g_value_set_string(&value, passphrase);
 
-       connman_set_property(proxy, "Passphrase", &value, NULL);
+       ret = connman_set_property(proxy, "Passphrase", &value, NULL);
 
        g_value_unset(&value);
 
        g_object_unref(proxy);
+
+done:
+       return ret;
 }
 
 void connman_client_set_callback(ConnmanClient *client,
@@ -540,3 +545,20 @@ void connman_client_set_callback(ConnmanClient *client,
        if (state != NULL && priv->callback != NULL)
                priv->callback(state, priv->userdata);
 }
+
+void connman_client_remove(ConnmanClient *client, const gchar *network)
+{
+       ConnmanClientPrivate *priv = CONNMAN_CLIENT_GET_PRIVATE(client);
+       DBusGProxy *proxy;
+
+       if (network == NULL)
+               return;
+
+       proxy = connman_dbus_get_proxy(priv->store, network);
+       if (proxy == NULL)
+               return;
+
+       connman_remove(proxy, NULL);
+
+       g_object_unref(proxy);
+}
diff --git a/common/connman-client.h b/common/connman-client.h
index d756619..592da4f 100644
--- a/common/connman-client.h
+++ b/common/connman-client.h
@@ -23,6 +23,7 @@
 #define __CONNMAN_CLIENT_H
 
 #include <gtk/gtk.h>
+#include "connman-dbus-glue.h"
 
 G_BEGIN_DECLS
 
@@ -64,8 +65,6 @@ GtkTreeModel *connman_client_get_model(ConnmanClient *client);
 GtkTreeModel *connman_client_get_device_model(ConnmanClient *client);
 GtkTreeModel *connman_client_get_connection_model(ConnmanClient *client);
 
-void connman_client_set_policy(ConnmanClient *client, const gchar *device,
-                                                       const gchar *policy);
 void connman_client_set_powered(ConnmanClient *client, const gchar *device,
                                                        gboolean powered);
 gboolean connman_client_set_ipv4(ConnmanClient *client, const gchar *device,
@@ -74,10 +73,11 @@ void connman_client_propose_scan(ConnmanClient *client, 
const gchar *device);
 
 void connman_client_connect(ConnmanClient *client, const gchar *network);
 void connman_client_disconnect(ConnmanClient *client, const gchar *network);
-
-guint connman_client_get_security(ConnmanClient *client, const gchar *network);
+gchar *connman_client_get_security(ConnmanClient *client, const gchar 
*network);
+void connman_client_connect_async(ConnmanClient *client, const gchar *network,
+                               connman_connect_reply callback, gpointer 
userdata);
 gchar *connman_client_get_passphrase(ConnmanClient *client, const gchar 
*network);
-void connman_client_set_passphrase(ConnmanClient *client, const gchar *network,
+gboolean connman_client_set_passphrase(ConnmanClient *client, const gchar 
*network,
                                                const gchar *passphrase);
 void connman_client_set_remember(ConnmanClient *client, const gchar *network,
                                                        gboolean remember);
@@ -87,16 +87,18 @@ typedef void (* ConnmanClientCallback) (const char *status, 
void *user_data);
 void connman_client_set_callback(ConnmanClient *client,
                        ConnmanClientCallback callback, gpointer user_data);
 
+void connman_client_remove(ConnmanClient *client, const gchar *network);
+
 enum {
        CONNMAN_COLUMN_PROXY,           /* G_TYPE_OBJECT  */
        CONNMAN_COLUMN_INDEX,           /* G_TYPE_UINT    */
        CONNMAN_COLUMN_NAME,            /* G_TYPE_STRING  */
        CONNMAN_COLUMN_ICON,            /* G_TYPE_STRING  */
        CONNMAN_COLUMN_TYPE,            /* G_TYPE_UINT    */
-       CONNMAN_COLUMN_STATE,           /* G_TYPE_UINT    */
+       CONNMAN_COLUMN_STATE,           /* G_TYPE_STRING  */
        CONNMAN_COLUMN_FAVORITE,        /* G_TYPE_BOOLEAN */
        CONNMAN_COLUMN_STRENGTH,        /* G_TYPE_UINT    */
-       CONNMAN_COLUMN_SECURITY,        /* G_TYPE_UINT    */
+       CONNMAN_COLUMN_SECURITY,        /* G_TYPE_STRING  */
        CONNMAN_COLUMN_PASSPHRASE,      /* G_TYPE_STRING  */
        CONNMAN_COLUMN_METHOD,          /* G_TYPE_STRING  */
        CONNMAN_COLUMN_ADDRESS,         /* G_TYPE_STRING  */
diff --git a/common/connman-dbus.c b/common/connman-dbus.c
index 549c567..2b5be36 100644
--- a/common/connman-dbus.c
+++ b/common/connman-dbus.c
@@ -194,46 +194,6 @@ static const gchar *type2icon(guint type)
        return NULL;
 }
 
-static guint get_state(const GValue *value)
-{
-       const char *state = value ? g_value_get_string(value) : NULL;
-
-       if (state == NULL)
-               return CONNMAN_STATE_UNKNOWN;
-       else if (g_str_equal(state, "idle") == TRUE)
-               return CONNMAN_STATE_IDLE;
-       else if (g_str_equal(state, "carrier") == TRUE)
-               return CONNMAN_STATE_CARRIER;
-       else if (g_str_equal(state, "association") == TRUE)
-               return CONNMAN_STATE_ASSOCIATION;
-       else if (g_str_equal(state, "configuration") == TRUE)
-               return CONNMAN_STATE_CONFIGURATION;
-       else if (g_str_equal(state, "ready") == TRUE)
-               return CONNMAN_STATE_READY;
-       else if (g_str_equal(state, "failure") == TRUE)
-               return CONNMAN_STATE_FAILURE;
-
-       return CONNMAN_STATE_UNKNOWN;
-}
-
-static guint get_security(const GValue *value)
-{
-       const char *security = value ? g_value_get_string(value) : NULL;
-
-       if (security == NULL)
-               return CONNMAN_SECURITY_UNKNOWN;
-       else if (g_str_equal(security, "none") == TRUE)
-               return CONNMAN_SECURITY_NONE;
-       else if (g_str_equal(security, "wep") == TRUE)
-               return CONNMAN_SECURITY_WEP;
-       else if (g_str_equal(security, "wpa") == TRUE)
-               return CONNMAN_SECURITY_WPA;
-       else if (g_str_equal(security, "rsn") == TRUE)
-               return CONNMAN_SECURITY_RSN;
-
-       return CONNMAN_SECURITY_UNKNOWN;
-}
-
 static void service_changed(DBusGProxy *proxy, const char *property,
                                        GValue *value, gpointer user_data)
 {
@@ -244,7 +204,6 @@ static void service_changed(DBusGProxy *proxy, const char 
*property,
        const char *method, *addr, *netmask, *gateway;
        GValue *ipv4_method, *ipv4_address, *ipv4_netmask, *ipv4_gateway;
 
-
        DBG("store %p proxy %p property %s", store, proxy, property);
 
        if (property == NULL || value == NULL)
@@ -277,13 +236,23 @@ static void service_changed(DBusGProxy *proxy, const char 
*property,
                                        CONNMAN_COLUMN_GATEWAY, gateway,
                                        -1);
        } else if (g_str_equal(property, "State") == TRUE) {
-               guint state = get_state(value);
+               const char *state = value ? g_value_get_string(value) : NULL;
                gtk_tree_store_set(store, &iter,
                                        CONNMAN_COLUMN_STATE, state, -1);
        } else if (g_str_equal(property, "Favorite") == TRUE) {
                gboolean favorite = g_value_get_boolean(value);
                gtk_tree_store_set(store, &iter,
                                        CONNMAN_COLUMN_FAVORITE, favorite, -1);
+       } else if (g_str_equal(property, "Security") == TRUE) {
+               const char *security = value ? g_value_get_string(value) : NULL;
+               gtk_tree_store_set(store, &iter,
+                                       CONNMAN_COLUMN_SECURITY, security,
+                                       -1);
+       } else if (g_str_equal(property, "PassPhrase") == TRUE) {
+               const char *passphrase = value ? g_value_get_string(value) : 
NULL;
+               gtk_tree_store_set(store, &iter,
+                                       CONNMAN_COLUMN_PASSPHRASE, passphrase,
+                                       -1);
        } else if (g_str_equal(property, "Strength") == TRUE) {
                guint strength = g_value_get_uchar(value);
                gtk_tree_store_set(store, &iter,
@@ -364,8 +333,8 @@ static void service_properties(DBusGProxy *proxy, 
GHashTable *hash,
 {
        GtkTreeStore *store = user_data;
        GValue *value;
-       const gchar *name, *icon;
-       guint type, state, strength, security;
+       const gchar *name, *icon, *passphrase, *security, *state;
+       guint type, strength;
        gboolean favorite;
        GtkTreeIter iter;
 
@@ -386,16 +355,19 @@ static void service_properties(DBusGProxy *proxy, 
GHashTable *hash,
        icon = type2icon(type);
 
        value = g_hash_table_lookup(hash, "State");
-       state = get_state(value);
+       state = value ? g_value_get_string(value) : NULL;
 
        value = g_hash_table_lookup(hash, "Favorite");
        favorite = value ? g_value_get_boolean(value) : FALSE;
 
        value = g_hash_table_lookup(hash, "Strength");
-       strength= value ? g_value_get_uchar(value) : 0;
+       strength = value ? g_value_get_uchar(value) : 0;
 
        value = g_hash_table_lookup(hash, "Security");
-       security = get_security(value);
+       security = value ? g_value_get_string(value) : NULL;
+
+       value = g_hash_table_lookup(hash, "PassPhrase");
+       passphrase = value ? g_value_get_string(value) : NULL;
 
        DBG("name %s type %d icon %s", name, type, icon);
 
@@ -427,6 +399,7 @@ static void service_properties(DBusGProxy *proxy, 
GHashTable *hash,
                                        CONNMAN_COLUMN_FAVORITE, favorite,
                                        CONNMAN_COLUMN_STRENGTH, strength,
                                        CONNMAN_COLUMN_SECURITY, security,
+                                       CONNMAN_COLUMN_PASSPHRASE, passphrase,
                                        CONNMAN_COLUMN_METHOD, method,
                                        CONNMAN_COLUMN_ADDRESS, addr,
                                        CONNMAN_COLUMN_NETMASK, netmask,
@@ -446,6 +419,7 @@ static void service_properties(DBusGProxy *proxy, 
GHashTable *hash,
                                        CONNMAN_COLUMN_FAVORITE, favorite,
                                        CONNMAN_COLUMN_STRENGTH, strength,
                                        CONNMAN_COLUMN_SECURITY, security,
+                                       CONNMAN_COLUMN_PASSPHRASE, passphrase,
                                        CONNMAN_COLUMN_METHOD, method,
                                        CONNMAN_COLUMN_ADDRESS, addr,
                                        CONNMAN_COLUMN_NETMASK, netmask,
diff --git a/common/connman-dbus.xml b/common/connman-dbus.xml
index 9fefd55..845f1a3 100644
--- a/common/connman-dbus.xml
+++ b/common/connman-dbus.xml
@@ -15,5 +15,7 @@
     </method>
     <method name="Disconnect">
     </method>
+    <method name="Remove">
+    </method>
   </interface>
 </node>
diff --git a/properties/Makefile.am b/properties/Makefile.am
index 38060a3..fc1b8c6 100644
--- a/properties/Makefile.am
+++ b/properties/Makefile.am
@@ -1,7 +1,7 @@
 
 bin_PROGRAMS = connman-properties
 
-connman_properties_SOURCES = main.c advanced.h ethernet.c
+connman_properties_SOURCES = main.c advanced.h ethernet.c wifi.c
 
 connman_properties_LDADD = $(top_builddir)/common/libcommon.a \
                                                @GTK_LIBS@ @DBUS_LIBS@
diff --git a/properties/advanced.h b/properties/advanced.h
index fdf6996..f70da4e 100644
--- a/properties/advanced.h
+++ b/properties/advanced.h
@@ -23,6 +23,7 @@ struct config_data {
        ConnmanClient *client;
 
        GtkWidget *widget;
+       GtkWidget *table;
        GtkWidget *title;
        GtkWidget *label;
        GtkWidget *button;
@@ -47,6 +48,16 @@ struct config_data {
        } ipv4;
 
        struct ipv4_config ipv4_config;
+
+       struct {
+               GtkWidget *name;
+               GtkWidget *security;
+               GtkWidget *strength;
+               GtkWidget *passphrase;
+               GtkWidget *connect_info;
+               GtkWidget *connect;
+               GtkWidget *disconnect;
+       } wifi;
 };
 
 static inline gboolean separator_function(GtkTreeModel *model,
@@ -68,5 +79,5 @@ static inline gboolean separator_function(GtkTreeModel *model,
 void add_ethernet_service(GtkWidget *mainbox, GtkTreeIter *iter, struct 
config_data *data);
 void update_ethernet_ipv4(struct config_data *data, guint policy);
 
-void add_wifi_policy(GtkWidget *mainbox, struct config_data *data);
+void add_wifi_service(GtkWidget *mainbox, GtkTreeIter *iter, struct 
config_data *data);
 void update_wifi_policy(struct config_data *data, guint policy);
diff --git a/properties/main.c b/properties/main.c
index 49bb218..aa99fc6 100644
--- a/properties/main.c
+++ b/properties/main.c
@@ -32,22 +32,44 @@
 
 static ConnmanClient *client;
 static GtkWidget *interface_notebook;
+static struct config_data *current_data;
 
-static void update_config(struct config_data *data)
+static void status_update(GtkTreeModel *model, GtkTreePath  *path,
+               GtkTreeIter  *iter, gpointer user_data)
 {
-       GtkTreeIter iter;
+       struct config_data *data = user_data;
        guint type;
-       gchar *network;
+       const char *name = NULL, *_name = NULL, *state = NULL;
 
-       if (gtk_tree_model_get_iter_from_string(data->model,
-                                               &iter, data->index) == FALSE)
-               return;
+       gtk_tree_model_get(model, iter,
+                       CONNMAN_COLUMN_STATE, &state,
+                       CONNMAN_COLUMN_NAME, &name,
+                       CONNMAN_COLUMN_TYPE, &type,
+                       -1);
 
-       gtk_tree_model_get(data->model, &iter,
-                               CONNMAN_COLUMN_TYPE, &type,
-                               CONNMAN_COLUMN_NAME, &network, -1);
+       if (type == CONNMAN_TYPE_WIFI) {
+               if (data->wifi.name)
+                       _name = gtk_label_get_text(GTK_LABEL(data->wifi.name));
 
-       g_free(network);
+               if (!(name && _name && g_str_equal(name, _name)))
+                       return;
+
+               if (g_str_equal(state, "failure") == TRUE) {
+                       gtk_label_set_text(GTK_LABEL(data->wifi.connect_info),
+                                       _("connection failed"));
+                       gtk_widget_show(data->wifi.connect_info);
+                       gtk_widget_show(data->wifi.connect);
+                       gtk_widget_hide(data->wifi.disconnect);
+               } else if (g_str_equal(state, "idle") == TRUE) {
+                       gtk_widget_hide(data->wifi.connect_info);
+                       gtk_widget_show(data->wifi.connect);
+                       gtk_widget_hide(data->wifi.disconnect);
+               } else {
+                       gtk_widget_hide(data->wifi.connect_info);
+                       gtk_widget_hide(data->wifi.connect);
+                       gtk_widget_show(data->wifi.disconnect);
+               }
+       }
 }
 
 static struct config_data *create_config(GtkTreeModel *model,
@@ -100,7 +122,7 @@ static struct config_data *create_config(GtkTreeModel 
*model,
                add_ethernet_service(mainbox, iter, data);
                break;
        case CONNMAN_TYPE_WIFI:
-//             add_wifi_policy(mainbox, data);
+               add_wifi_service(mainbox, iter, data);
                break;
        default:
                break;
@@ -117,6 +139,9 @@ static struct config_data *create_config(GtkTreeModel 
*model,
 
        gtk_widget_show_all(mainbox);
 
+       g_signal_connect(G_OBJECT(model), "row-changed",
+                       G_CALLBACK(status_update), data);
+
        return data;
 }
 
@@ -135,19 +160,21 @@ static void select_callback(GtkTreeSelection *selection, 
gpointer user_data)
                return;
        }
 
-       if (data == NULL) {
-               data = create_config(model, &iter, user_data);
-               if (data == NULL)
-                       return;
+       if (current_data) {
+               g_signal_handlers_disconnect_by_func(G_OBJECT(model),
+                               G_CALLBACK(status_update), current_data);
+               g_free(current_data);
+       }
+
+       data = create_config(model, &iter, user_data);
+       if (data == NULL)
+               return;
 
-               page = gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
-                                                       data->widget, NULL);
-       } else {
-               update_config(data);
 
-               page = gtk_notebook_page_num(GTK_NOTEBOOK(notebook),
-                                                               data->widget);
-       }
+       current_data = data;
+
+       page = gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
+                               data->widget, NULL);
 
        gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), page);
 
@@ -168,22 +195,36 @@ static void device_to_text(GtkTreeViewColumn *column, 
GtkCellRenderer *cell,
        switch (type) {
        case CONNMAN_TYPE_ETHERNET:
                title = N_("Ethernet");
+               markup = g_strdup_printf("  %s\n", title);
                break;
        case CONNMAN_TYPE_WIFI:
-               title = N_("WIFI");
+               /* Show the AP name */
+               title = N_(name);
+               if (g_str_equal(state, "association") == TRUE)
+                       state = "associating...";
+               else if (g_str_equal(state, "configuration") == TRUE)
+                       state = "configurating...";
+               else if (g_str_equal(state, "ready") == TRUE ||
+                               g_str_equal(state, "online") == TRUE)
+                       state = "connnected";
+               else
+                       state = "";
+               markup = g_strdup_printf("  %s\n  %s", title, state);
                break;
        case CONNMAN_TYPE_WIMAX:
                title = N_("WiMAX");
+               markup = g_strdup_printf("  %s\n", title);
                break;
        case CONNMAN_TYPE_BLUETOOTH:
                title = N_("Bluetooth");
+               markup = g_strdup_printf("  %s\n", title);
                break;
        default:
                title = N_("Unknown");
+               markup = g_strdup_printf("  %s\n", title);
                break;
        }
 
-       markup = g_strdup_printf("  %s\n", title);
        g_object_set(cell, "markup", markup, NULL);
        g_free(markup);
 }
@@ -350,6 +391,8 @@ int main(int argc, char *argv[])
        bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
        textdomain(GETTEXT_PACKAGE);
 
+       current_data = NULL;
+
        gtk_init(&argc, &argv);
 
        gtk_window_set_default_icon_name("network-wireless");
diff --git a/properties/wifi.c b/properties/wifi.c
index b34df51..1fc8121 100644
--- a/properties/wifi.c
+++ b/properties/wifi.c
@@ -23,6 +23,7 @@
 #include <config.h>
 #endif
 
+#include <dbus/dbus-glib.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
@@ -30,108 +31,287 @@
 
 #include "advanced.h"
 
+static void update_wifi_ipv4(struct config_data *data, guint policy);
+
 static void changed_callback(GtkWidget *editable, gpointer user_data)
 {
-#if 0
        struct config_data *data = user_data;
        gint active;
 
        active = gtk_combo_box_get_active(GTK_COMBO_BOX(data->policy.config));
 
-       switch (active) {
-       case 0:
-               connman_client_set_policy(data->client, data->device, "auto");
-               update_wifi_policy(data, CONNMAN_POLICY_AUTO);
-               break;
-       case 1:
-               connman_client_set_policy(data->client, data->device, "manual");
-               update_wifi_policy(data, CONNMAN_POLICY_MANUAL);
-               break;
-       case 3:
-               connman_client_set_policy(data->client, data->device, "off");
-               update_wifi_policy(data, CONNMAN_POLICY_OFF);
-               break;
+       update_wifi_ipv4(data, active);
+}
+
+static void connect_reply_cb(DBusGProxy *proxy, GError *error,
+               gpointer user_data)
+{
+       if (error)
+               g_error_free(error);
+}
+
+static void connect_callback(GtkWidget *editable, gpointer user_data)
+{
+       struct config_data *data = user_data;
+
+       gboolean ret;
+       gint active;
+
+       if (data->wifi.passphrase) {
+               char *passphrase;
+               passphrase = (gchar 
*)gtk_entry_get_text(GTK_ENTRY(data->wifi.passphrase));
+               ret = connman_client_set_passphrase(data->client, data->device,
+                               passphrase);
+
+               if (ret == FALSE) {
+                       return;
+               }
        }
-#endif
+
+       active = gtk_combo_box_get_active(GTK_COMBO_BOX(data->policy.config));
+       data->ipv4_config.method = active ? "manual" : "dhcp";
+       data->ipv4_config.address = active ? 
gtk_entry_get_text(GTK_ENTRY(data->ipv4.entry[0])) : NULL;
+       data->ipv4_config.netmask = active ? 
gtk_entry_get_text(GTK_ENTRY(data->ipv4.entry[1])) : NULL;
+       data->ipv4_config.gateway = active ? 
gtk_entry_get_text(GTK_ENTRY(data->ipv4.entry[2])) : NULL;
+
+       ret = connman_client_set_ipv4(data->client, data->device, 
&data->ipv4_config);
+       if (ret == FALSE) {
+               return;
+       }
+
+       connman_client_connect_async(data->client, data->device, 
connect_reply_cb, data);
+}
+
+
+static void disconnect_callback(GtkWidget *editable, gpointer user_data)
+{
+       struct config_data *data = user_data;
+
+       connman_client_disconnect(data->client, data->device);
+}
+
+static void wifi_ipconfig(GtkWidget *table, struct config_data *data, 
GtkTreeIter *iter)
+{
+       GtkWidget *entry;
+       GtkWidget *label;
+       GtkWidget *combo;
+       DBusGProxy *proxy;
+
+       struct ipv4_config ipv4_config = {
+               .method  = NULL,
+               .address = NULL,
+               .netmask = NULL,
+               .gateway = NULL,
+       };
+
+       gtk_tree_model_get(data->model, iter,
+                       CONNMAN_COLUMN_PROXY, &proxy,
+                       CONNMAN_COLUMN_METHOD, &ipv4_config.method,
+                       CONNMAN_COLUMN_ADDRESS, &ipv4_config.address,
+                       CONNMAN_COLUMN_NETMASK, &ipv4_config.netmask,
+                       CONNMAN_COLUMN_GATEWAY, &ipv4_config.gateway,
+                       -1);
+       label = gtk_label_new(_("Configuration:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 3, 4);
+       data->ipv4.label[0] = label;
+
+       combo = gtk_combo_box_new_text();
+       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "DHCP");
+       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Manual");
+
+       gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combo),
+                       separator_function, NULL, NULL);
+       gtk_table_attach_defaults(GTK_TABLE(table), combo, 2, 4, 3, 4);
+       data->policy.config = combo;
+
+       label = gtk_label_new(_("IP address:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 4, 5);
+       data->ipv4.label[0] = label;
+
+       entry = gtk_entry_new();
+       gtk_entry_set_max_length (GTK_ENTRY (entry), 15);
+       if (ipv4_config.address)
+               gtk_entry_set_text(GTK_ENTRY(entry), ipv4_config.address);
+       gtk_table_attach_defaults(GTK_TABLE(table), entry, 2, 4, 4, 5);
+       data->ipv4.entry[0] = entry;
+
+
+       label = gtk_label_new(_("Netmask:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 5, 6);
+       data->ipv4.label[1] = label;
+
+       entry = gtk_entry_new();
+       gtk_entry_set_max_length (GTK_ENTRY (entry), 15);
+       if (ipv4_config.netmask)
+               gtk_entry_set_text(GTK_ENTRY(entry), ipv4_config.netmask);
+       gtk_table_attach_defaults(GTK_TABLE(table), entry, 2, 4, 5, 6);
+       data->ipv4.entry[1] = entry;
+
+       label = gtk_label_new(_("Gateway:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 6, 7);
+       data->ipv4.label[2] = label;
+
+       entry = gtk_entry_new();
+       gtk_entry_set_max_length (GTK_ENTRY (entry), 15);
+       if (ipv4_config.gateway)
+               gtk_entry_set_text(GTK_ENTRY(entry), ipv4_config.gateway);
+       gtk_table_attach_defaults(GTK_TABLE(table), entry, 2, 4, 6, 7);
+       data->ipv4.entry[2] = entry;
+
+       data->ipv4_config = ipv4_config;
+
+       if (g_str_equal(ipv4_config.method, "dhcp") == TRUE)
+               update_wifi_ipv4(data, CONNMAN_POLICY_DHCP);
+       else
+               update_wifi_ipv4(data, CONNMAN_POLICY_MANUAL);
+
+       g_signal_connect(G_OBJECT(combo), "changed",
+                       G_CALLBACK(changed_callback), data);
+}
+
+static void toggled_callback(GtkWidget *button, gpointer user_data)
+{
+       GtkWidget *entry = user_data;
+       gboolean mode;
+
+       mode = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
+
+       gtk_entry_set_visibility(GTK_ENTRY(entry), mode);
 }
 
-void add_wifi_policy(GtkWidget *mainbox, struct config_data *data)
+void add_wifi_service(GtkWidget *mainbox, GtkTreeIter *iter, struct 
config_data *data)
 {
        GtkWidget *vbox;
        GtkWidget *table;
        GtkWidget *label;
-       GtkWidget *combo;
+       GtkWidget *entry;
+       GtkWidget *button;
+
+       const char *name, *security, *icon, *state;
+       guint strength;
 
-       vbox = gtk_vbox_new(FALSE, 0);
+       gtk_tree_model_get(data->model, iter,
+                       CONNMAN_COLUMN_NAME, &name,
+                       CONNMAN_COLUMN_SECURITY, &security,
+                       CONNMAN_COLUMN_ICON, &icon,
+                       CONNMAN_COLUMN_STATE, &state,
+                       CONNMAN_COLUMN_STRENGTH, &strength,
+                       -1);
+
+       if (g_str_equal(state, "failure") == TRUE)
+               connman_client_remove(data->client, data->device);
+
+       vbox = gtk_vbox_new(TRUE, 0);
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 24);
        gtk_box_pack_start(GTK_BOX(mainbox), vbox, FALSE, FALSE, 0);
 
-       table = gtk_table_new(2, 4, FALSE);
-       gtk_table_set_row_spacings(GTK_TABLE(table), 8);
-       gtk_table_set_col_spacings(GTK_TABLE(table), 8);
+       table = gtk_table_new(10, 5, TRUE);
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
+       data->table = table;
 
-#if 0
-       label = gtk_label_new(_("Network Name:"));
-       gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
-       gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
-       gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
+       label = gtk_label_new(_("Access Point:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
 
-       combo = gtk_combo_box_new_text();
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Guest");
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "");
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Off");
-       gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combo),
-                                       separator_function, NULL, NULL);
-       gtk_table_attach_defaults(GTK_TABLE(table), combo, 1, 4, 0, 1);
-       data->policy.config = combo;
-#endif
-
-       label = gtk_label_new(_("Configuration:"));
-       gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
-       gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
-       gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
+       label = gtk_label_new(_(name));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 4, 0, 1);
+       gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+       gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+       data->wifi.name = label;
 
-       combo = gtk_combo_box_new_text();
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Automatically");
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Manually");
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "");
-       gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Off");
-       gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combo),
-                                       separator_function, NULL, NULL);
-       gtk_table_attach_defaults(GTK_TABLE(table), combo, 1, 4, 0, 1);
-       data->policy.config = combo;
+       label = gtk_label_new(_("Security:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
 
-       label = gtk_label_new(NULL);
-       gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
+       label = gtk_label_new(_(security));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 4, 1, 2);
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+       gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+       data->wifi.security = label;
+
+       label = gtk_label_new(_("Passphrase:"));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
+
+       if (g_str_equal(security, "none") != TRUE &&
+                       g_str_equal(security, "unknown") != TRUE) {
+               entry = gtk_entry_new();
+               gtk_entry_set_max_length (GTK_ENTRY (entry), 64);
+               gtk_table_attach_defaults(GTK_TABLE(table), entry, 2, 4, 2, 3);
+               gtk_entry_set_visibility(GTK_ENTRY(entry), 0);
+               data->wifi.passphrase = entry;
+
+               button = gtk_check_button_new_with_label(_("Show input"));
+               gtk_table_attach_defaults(GTK_TABLE(table), button, 4, 5, 2, 3);
+
+               g_signal_connect(G_OBJECT(button), "toggled",
+                               G_CALLBACK(toggled_callback), entry);
+
+
+       } else {
+               label = gtk_label_new(_("none"));
+               gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 4, 2, 3);
+               gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+               gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+               data->wifi.passphrase = NULL;
+       }
+
+       label = gtk_label_new(_(""));
+       gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 8, 9);
        gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
-       gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 4, 1, 2);
-       gtk_widget_set_size_request(label, 180, -1);
-       data->policy.label = label;
+       gtk_widget_hide(label);
+       data->wifi.connect_info = label;
 
-       g_signal_connect(G_OBJECT(combo), "changed",
-                               G_CALLBACK(changed_callback), data);
+       button = gtk_button_new_with_label(_("Connect"));
+       gtk_table_attach_defaults(GTK_TABLE(table), button, 3, 4, 8, 9);
+       g_signal_connect(G_OBJECT(button), "clicked",
+                       G_CALLBACK(connect_callback), data);
+       gtk_widget_set_no_show_all(button, TRUE);
+       data->wifi.connect = button;
+
+       button = gtk_button_new_with_label(_("Disconnect"));
+       gtk_table_attach_defaults(GTK_TABLE(table), button, 3, 4, 8, 9);
+       g_signal_connect(G_OBJECT(button), "clicked",
+                       G_CALLBACK(disconnect_callback), data);
+       data->wifi.disconnect = button;
+       gtk_widget_set_no_show_all(button, TRUE);
+
+       if (g_str_equal(state, "failure") == TRUE
+                       || g_str_equal(state, "idle") == TRUE) {
+               gtk_widget_show(data->wifi.connect);
+               gtk_widget_hide(data->wifi.disconnect);
+       } else {
+               gtk_widget_hide(data->wifi.connect);
+               gtk_widget_show(data->wifi.disconnect);
+       }
+
+       wifi_ipconfig(table, data, iter);
 }
 
-void update_wifi_policy(struct config_data *data, guint policy)
+void update_wifi_ipv4(struct config_data *data, guint policy)
 {
-#if 0
        GtkWidget *combo = data->policy.config;
-       gchar *info = NULL;
+       GtkWidget *entry[3];
+       int i;
+
+       for (i = 0; i < 3; i++)
+               entry[i] = data->ipv4.entry[i];
+
 
        g_signal_handlers_block_by_func(G_OBJECT(combo),
-                                       G_CALLBACK(changed_callback), data);
+                       G_CALLBACK(changed_callback), data);
 
        switch (policy) {
-       case CONNMAN_POLICY_OFF:
-               gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 3);
+       case CONNMAN_POLICY_DHCP:
+               gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
+               for (i = 0; i < 3; i++) {
+                       gtk_entry_set_editable(GTK_ENTRY(entry[i]), 0);
+                       gtk_widget_set_sensitive(entry[i], 0);
+               }
                break;
        case CONNMAN_POLICY_MANUAL:
                gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 1);
-               break;
-       case CONNMAN_POLICY_AUTO:
-               gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
+               for (i = 0; i < 3; i++) {
+                       gtk_entry_set_editable(GTK_ENTRY(entry[i]), 1);
+                       gtk_widget_set_sensitive(entry[i], 1);
+               }
                break;
        default:
                gtk_combo_box_set_active(GTK_COMBO_BOX(combo), -1);
@@ -140,9 +320,4 @@ void update_wifi_policy(struct config_data *data, guint 
policy)
 
        g_signal_handlers_unblock_by_func(G_OBJECT(combo),
                                        G_CALLBACK(changed_callback), data);
-
-       gtk_label_set_markup(GTK_LABEL(data->policy.label), info);
-
-       g_free(info);
-#endif
 }
-- 
1.7.1

_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to