[gnome-control-center] network: Copy shared functions into the two cases it is used



commit f75affbfe05f8f9a1c2e1031749c544a4d526d01
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Nov 12 09:28:00 2019 +1300

    network: Copy shared functions into the two cases it is used
    
    This does increase duplication, but should be reduced in following refactoring.

 .../network/wireless-security/wireless-security.c  | 203 --------------------
 .../network/wireless-security/wireless-security.h  |  13 --
 panels/network/wireless-security/ws-dynamic-wep.c  | 200 ++++++++++++++++++--
 panels/network/wireless-security/ws-wpa-eap.c      | 205 ++++++++++++++++++++-
 4 files changed, 382 insertions(+), 239 deletions(-)
---
diff --git a/panels/network/wireless-security/wireless-security.c 
b/panels/network/wireless-security/wireless-security.c
index 1a0738b95..47a92605b 100644
--- a/panels/network/wireless-security/wireless-security.c
+++ b/panels/network/wireless-security/wireless-security.c
@@ -26,13 +26,6 @@
 
 #include "wireless-security.h"
 #include "wireless-security-resources.h"
-#include "eap-method.h"
-#include "eap-method-fast.h"
-#include "eap-method-leap.h"
-#include "eap-method-peap.h"
-#include "eap-method-simple.h"
-#include "eap-method-tls.h"
-#include "eap-method-ttls.h"
 #include "utils.h"
 
 typedef struct  {
@@ -49,9 +42,6 @@ enum {
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-#define AUTH_NAME_COLUMN   0
-#define AUTH_METHOD_COLUMN 1
-
 static void
 wireless_security_dispose (GObject *object)
 {
@@ -247,196 +237,3 @@ wireless_security_clear_ciphers (NMConnection *connection)
        nm_setting_wireless_security_clear_pairwise (s_wireless_sec);
        nm_setting_wireless_security_clear_groups (s_wireless_sec);
 }
-
-EAPMethod *
-ws_802_1x_auth_combo_get_eap (GtkComboBox *combo)
-{
-       GtkTreeModel *model;
-       GtkTreeIter iter;
-       g_autoptr(EAPMethod) eap = NULL;
-
-       model = gtk_combo_box_get_model (combo);
-       if (!gtk_combo_box_get_active_iter (combo, &iter))
-               return NULL;
-
-       gtk_tree_model_get (model, &iter, AUTH_METHOD_COLUMN, &eap, -1);
-       return eap;
-}
-
-void
-ws_802_1x_auth_combo_init (WirelessSecurity *self,
-                           GtkComboBox *combo,
-                           NMConnection *connection,
-                           gboolean is_editor,
-                           gboolean secrets_only)
-{
-       const gchar *user = NULL, *password = NULL;
-       gboolean always_ask = FALSE;
-       g_autoptr(GtkListStore) auth_model = NULL;
-       GtkTreeIter iter;
-       g_autoptr(EAPMethodTLS) em_tls = NULL;
-       g_autoptr(EAPMethodSimple) em_pwd = NULL;
-       g_autoptr(EAPMethodFAST) em_fast = NULL;
-       g_autoptr(EAPMethodTTLS) em_ttls = NULL;
-       g_autoptr(EAPMethodPEAP) em_peap = NULL;
-       const char *default_method = NULL, *ctype = NULL;
-       int active = -1, item = 0;
-       gboolean wired = FALSE;
-       EAPMethodSimpleFlags simple_flags = EAP_METHOD_SIMPLE_FLAG_NONE;
-
-       /* Grab the default EAP method out of the security object */
-       if (connection) {
-               NMSettingConnection *s_con;
-               NMSetting8021x *s_8021x;
-
-               s_con = nm_connection_get_setting_connection (connection);
-               if (s_con)
-                       ctype = nm_setting_connection_get_connection_type (s_con);
-               if (   (g_strcmp0 (ctype, NM_SETTING_WIRED_SETTING_NAME) == 0)
-                   || nm_connection_get_setting_wired (connection))
-                       wired = TRUE;
-
-               s_8021x = nm_connection_get_setting_802_1x (connection);
-               if (s_8021x && nm_setting_802_1x_get_num_eap_methods (s_8021x))
-                       default_method = nm_setting_802_1x_get_eap_method (s_8021x, 0);
-       }
-
-       /* initialize WirelessSecurity userpass from connection (clear if no connection) */
-       if (connection) {
-               NMSetting8021x *setting;
-
-               setting = nm_connection_get_setting_802_1x (connection);
-               if (setting) {
-                       NMSettingSecretFlags flags;
-
-                       user = nm_setting_802_1x_get_identity (setting);
-                       password = nm_setting_802_1x_get_password (setting);
-
-                       if (nm_setting_get_secret_flags (NM_SETTING (setting), NM_SETTING_802_1X_PASSWORD, 
&flags, NULL))
-                               always_ask = !!(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED);
-               }
-       }
-       wireless_security_set_username (self, user);
-       wireless_security_set_password (self, password);
-       wireless_security_set_always_ask (self, always_ask);
-       wireless_security_set_show_password (self, FALSE);
-
-       auth_model = gtk_list_store_new (2, G_TYPE_STRING, eap_method_get_type ());
-
-       if (is_editor)
-               simple_flags |= EAP_METHOD_SIMPLE_FLAG_IS_EDITOR;
-       if (secrets_only)
-               simple_flags |= EAP_METHOD_SIMPLE_FLAG_SECRETS_ONLY;
-
-       if (wired) {
-               g_autoptr(EAPMethodSimple) em_md5 = NULL;
-
-               em_md5 = eap_method_simple_new (self, connection, EAP_METHOD_SIMPLE_TYPE_MD5, simple_flags);
-               gtk_list_store_append (auth_model, &iter);
-               gtk_list_store_set (auth_model, &iter,
-                                       AUTH_NAME_COLUMN, _("MD5"),
-                                       AUTH_METHOD_COLUMN, em_md5,
-                                       -1);
-               if (default_method && (active < 0) && !strcmp (default_method, "md5"))
-                       active = item;
-               item++;
-       }
-
-       em_tls = eap_method_tls_new (self, connection, FALSE, secrets_only);
-       gtk_list_store_append (auth_model, &iter);
-       gtk_list_store_set (auth_model, &iter,
-                           AUTH_NAME_COLUMN, _("TLS"),
-                           AUTH_METHOD_COLUMN, em_tls,
-                           -1);
-       if (default_method && (active < 0) && !strcmp (default_method, "tls"))
-               active = item;
-       item++;
-
-       if (!wired) {
-               g_autoptr(EAPMethodLEAP) em_leap = NULL;
-
-               em_leap = eap_method_leap_new (self, connection, secrets_only);
-               gtk_list_store_append (auth_model, &iter);
-               gtk_list_store_set (auth_model, &iter,
-                                   AUTH_NAME_COLUMN, _("LEAP"),
-                                   AUTH_METHOD_COLUMN, em_leap,
-                                   -1);
-               if (default_method && (active < 0) && !strcmp (default_method, "leap"))
-                       active = item;
-               item++;
-       }
-
-       em_pwd = eap_method_simple_new (self, connection, EAP_METHOD_SIMPLE_TYPE_PWD, simple_flags);
-       gtk_list_store_append (auth_model, &iter);
-       gtk_list_store_set (auth_model, &iter,
-                           AUTH_NAME_COLUMN, _("PWD"),
-                           AUTH_METHOD_COLUMN, em_pwd,
-                           -1);
-       if (default_method && (active < 0) && !strcmp (default_method, "pwd"))
-               active = item;
-       item++;
-
-       em_fast = eap_method_fast_new (self, connection, is_editor, secrets_only);
-       gtk_list_store_append (auth_model, &iter);
-       gtk_list_store_set (auth_model, &iter,
-                           AUTH_NAME_COLUMN, _("FAST"),
-                           AUTH_METHOD_COLUMN, em_fast,
-                           -1);
-       if (default_method && (active < 0) && !strcmp (default_method, "fast"))
-               active = item;
-       item++;
-
-       em_ttls = eap_method_ttls_new (self, connection, is_editor, secrets_only);
-       gtk_list_store_append (auth_model, &iter);
-       gtk_list_store_set (auth_model, &iter,
-                           AUTH_NAME_COLUMN, _("Tunneled TLS"),
-                           AUTH_METHOD_COLUMN, em_ttls,
-                           -1);
-       if (default_method && (active < 0) && !strcmp (default_method, "ttls"))
-               active = item;
-       item++;
-
-       em_peap = eap_method_peap_new (self, connection, is_editor, secrets_only);
-       gtk_list_store_append (auth_model, &iter);
-       gtk_list_store_set (auth_model, &iter,
-                           AUTH_NAME_COLUMN, _("Protected EAP (PEAP)"),
-                           AUTH_METHOD_COLUMN, em_peap,
-                           -1);
-       if (default_method && (active < 0) && !strcmp (default_method, "peap"))
-               active = item;
-       item++;
-
-       gtk_combo_box_set_model (combo, GTK_TREE_MODEL (auth_model));
-       gtk_combo_box_set_active (combo, active < 0 ? 0 : (guint32) active);
-}
-
-void
-ws_802_1x_fill_connection (GtkComboBox *combo,
-                           NMConnection *connection)
-{
-       NMSettingWirelessSecurity *s_wireless_sec;
-       NMSetting8021x *s_8021x;
-       NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE;
-       EAPMethod *eap;
-
-       /* Get the EAPMethod object */
-       eap = ws_802_1x_auth_combo_get_eap (combo);
-       g_assert (eap);
-
-       /* Get previous pasword flags, if any. Otherwise default to agent-owned secrets */
-       s_8021x = nm_connection_get_setting_802_1x (connection);
-       if (s_8021x)
-               nm_setting_get_secret_flags (NM_SETTING (s_8021x), eap_method_get_password_flags_name (eap), 
&secret_flags, NULL);
-       else
-               secret_flags = NM_SETTING_SECRET_FLAG_AGENT_OWNED;
-
-       /* Blow away the old wireless security setting by adding a clear one */
-       s_wireless_sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
-       nm_connection_add_setting (connection, (NMSetting *) s_wireless_sec);
-
-       /* Blow away the old 802.1x setting by adding a clear one */
-       s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
-       nm_connection_add_setting (connection, (NMSetting *) s_8021x);
-
-       eap_method_fill_connection (eap, connection, secret_flags);
-}
diff --git a/panels/network/wireless-security/wireless-security.h 
b/panels/network/wireless-security/wireless-security.h
index 55f51c393..501ec611e 100644
--- a/panels/network/wireless-security/wireless-security.h
+++ b/panels/network/wireless-security/wireless-security.h
@@ -72,17 +72,4 @@ void wireless_security_notify_changed (WirelessSecurity *sec);
 
 void wireless_security_clear_ciphers (NMConnection *connection);
 
-void ws_802_1x_auth_combo_init (WirelessSecurity *sec,
-                                GtkComboBox *combo,
-                                NMConnection *connection,
-                                gboolean is_editor,
-                                gboolean secrets_only);
-
-#include "eap-method.h"
-
-EAPMethod *ws_802_1x_auth_combo_get_eap (GtkComboBox *combo);
-
-void ws_802_1x_fill_connection (GtkComboBox *combo,
-                                NMConnection *connection);
-
 G_END_DECLS
diff --git a/panels/network/wireless-security/ws-dynamic-wep.c 
b/panels/network/wireless-security/ws-dynamic-wep.c
index 923767c09..49f171c44 100644
--- a/panels/network/wireless-security/ws-dynamic-wep.c
+++ b/panels/network/wireless-security/ws-dynamic-wep.c
@@ -25,9 +25,15 @@
 #include <ctype.h>
 #include <string.h>
 
-#include "ws-dynamic-wep.h"
-#include "wireless-security.h"
 #include "eap-method.h"
+#include "eap-method-fast.h"
+#include "eap-method-leap.h"
+#include "eap-method-peap.h"
+#include "eap-method-simple.h"
+#include "eap-method-tls.h"
+#include "eap-method-ttls.h"
+#include "wireless-security.h"
+#include "ws-dynamic-wep.h"
 
 struct _WirelessSecurityDynamicWEP {
        WirelessSecurity parent;
@@ -43,6 +49,24 @@ struct _WirelessSecurityDynamicWEP {
 
 G_DEFINE_TYPE (WirelessSecurityDynamicWEP, ws_dynamic_wep, wireless_security_get_type ())
 
+#define AUTH_NAME_COLUMN   0
+#define AUTH_METHOD_COLUMN 1
+
+static EAPMethod *
+get_eap (WirelessSecurityDynamicWEP *self)
+{
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       g_autoptr(EAPMethod) eap = NULL;
+
+       model = gtk_combo_box_get_model (self->auth_combo);
+       if (!gtk_combo_box_get_active_iter (self->auth_combo, &iter))
+               return NULL;
+
+       gtk_tree_model_get (model, &iter, AUTH_METHOD_COLUMN, &eap, -1);
+       return eap;
+}
+
 static void
 ws_dynamic_wep_dispose (GObject *object)
 {
@@ -65,7 +89,7 @@ static gboolean
 validate (WirelessSecurity *security, GError **error)
 {
        WirelessSecurityDynamicWEP *self = WS_DYNAMIC_WEP (security);
-       return eap_method_validate (ws_802_1x_auth_combo_get_eap (self->auth_combo), error);
+       return eap_method_validate (get_eap (self), error);
 }
 
 static void
@@ -77,7 +101,7 @@ add_to_size_group (WirelessSecurity *security, GtkSizeGroup *group)
        self->size_group = g_object_ref (group);
 
        gtk_size_group_add_widget (self->size_group, GTK_WIDGET (self->auth_label));
-       eap_method_add_to_size_group (ws_802_1x_auth_combo_get_eap (self->auth_combo), self->size_group);
+       eap_method_add_to_size_group (get_eap (self), self->size_group);
 }
 
 static void
@@ -85,11 +109,29 @@ fill_connection (WirelessSecurity *security, NMConnection *connection)
 {
        WirelessSecurityDynamicWEP *self = WS_DYNAMIC_WEP (security);
        NMSettingWirelessSecurity *s_wireless_sec;
+       NMSetting8021x *s_8021x;
+       NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE;
+       EAPMethod *eap;
+
+       /* Get the EAPMethod object */
+       eap = get_eap (self);
 
-       ws_802_1x_fill_connection (self->auth_combo, connection);
+       /* Get previous pasword flags, if any. Otherwise default to agent-owned secrets */
+       s_8021x = nm_connection_get_setting_802_1x (connection);
+       if (s_8021x)
+               nm_setting_get_secret_flags (NM_SETTING (s_8021x), eap_method_get_password_flags_name (eap), 
&secret_flags, NULL);
+       else
+               secret_flags = NM_SETTING_SECRET_FLAG_AGENT_OWNED;
 
-       s_wireless_sec = nm_connection_get_setting_wireless_security (connection);
-       g_assert (s_wireless_sec);
+       /* Blow away the old wireless security setting by adding a clear one */
+       s_wireless_sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
+       nm_connection_add_setting (connection, (NMSetting *) s_wireless_sec);
+
+       /* Blow away the old 802.1x setting by adding a clear one */
+       s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
+       nm_connection_add_setting (connection, (NMSetting *) s_8021x);
+
+       eap_method_fill_connection (eap, connection, secret_flags);
 
        g_object_set (s_wireless_sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", NULL);
 }
@@ -112,7 +154,7 @@ auth_combo_changed_cb (WirelessSecurityDynamicWEP *self)
        for (elt = children; elt; elt = g_list_next (elt))
                gtk_container_remove (GTK_CONTAINER (self->method_box), GTK_WIDGET (elt->data));
 
-       eap = ws_802_1x_auth_combo_get_eap (self->auth_combo);
+       eap = get_eap (self);
        g_assert (eap);
 
        gtk_widget_unparent (GTK_WIDGET (eap));
@@ -154,6 +196,19 @@ ws_dynamic_wep_new (NMConnection *connection,
                     gboolean secrets_only)
 {
        WirelessSecurityDynamicWEP *self;
+       const gchar *user = NULL, *password = NULL;
+       gboolean always_ask = FALSE;
+       g_autoptr(GtkListStore) auth_model = NULL;
+       GtkTreeIter iter;
+       g_autoptr(EAPMethodTLS) em_tls = NULL;
+       g_autoptr(EAPMethodSimple) em_pwd = NULL;
+       g_autoptr(EAPMethodFAST) em_fast = NULL;
+       g_autoptr(EAPMethodTTLS) em_ttls = NULL;
+       g_autoptr(EAPMethodPEAP) em_peap = NULL;
+       const char *default_method = NULL, *ctype = NULL;
+       int active = -1, item = 0;
+       gboolean wired = FALSE;
+       EAPMethodSimpleFlags simple_flags = EAP_METHOD_SIMPLE_FLAG_NONE;
        g_autoptr(GError) error = NULL;
 
        self = g_object_new (ws_dynamic_wep_get_type (), NULL);
@@ -169,11 +224,130 @@ ws_dynamic_wep_new (NMConnection *connection,
        self->grid = GTK_GRID (gtk_builder_get_object (self->builder, "grid"));
        self->method_box = GTK_BOX (gtk_builder_get_object (self->builder, "method_box"));
 
-       ws_802_1x_auth_combo_init (WIRELESS_SECURITY (self),
-                                  self->auth_combo,
-                                  connection,
-                                  is_editor,
-                                  secrets_only);
+       /* Grab the default EAP method out of the security object */
+       if (connection) {
+               NMSettingConnection *s_con;
+               NMSetting8021x *s_8021x;
+
+               s_con = nm_connection_get_setting_connection (connection);
+               if (s_con)
+                       ctype = nm_setting_connection_get_connection_type (s_con);
+               if (   (g_strcmp0 (ctype, NM_SETTING_WIRED_SETTING_NAME) == 0)
+                   || nm_connection_get_setting_wired (connection))
+                       wired = TRUE;
+
+               s_8021x = nm_connection_get_setting_802_1x (connection);
+               if (s_8021x && nm_setting_802_1x_get_num_eap_methods (s_8021x))
+                       default_method = nm_setting_802_1x_get_eap_method (s_8021x, 0);
+       }
+
+       /* initialize WirelessSecurity userpass from connection (clear if no connection) */
+       if (connection) {
+               NMSetting8021x *setting;
+
+               setting = nm_connection_get_setting_802_1x (connection);
+               if (setting) {
+                       NMSettingSecretFlags flags;
+
+                       user = nm_setting_802_1x_get_identity (setting);
+                       password = nm_setting_802_1x_get_password (setting);
+
+                       if (nm_setting_get_secret_flags (NM_SETTING (setting), NM_SETTING_802_1X_PASSWORD, 
&flags, NULL))
+                               always_ask = !!(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED);
+               }
+       }
+       wireless_security_set_username (WIRELESS_SECURITY (self), user);
+       wireless_security_set_password (WIRELESS_SECURITY (self), password);
+       wireless_security_set_always_ask (WIRELESS_SECURITY (self), always_ask);
+       wireless_security_set_show_password (WIRELESS_SECURITY (self), FALSE);
+
+       auth_model = gtk_list_store_new (2, G_TYPE_STRING, eap_method_get_type ());
+
+       if (is_editor)
+               simple_flags |= EAP_METHOD_SIMPLE_FLAG_IS_EDITOR;
+       if (secrets_only)
+               simple_flags |= EAP_METHOD_SIMPLE_FLAG_SECRETS_ONLY;
+
+       if (wired) {
+               g_autoptr(EAPMethodSimple) em_md5 = NULL;
+
+               em_md5 = eap_method_simple_new (WIRELESS_SECURITY (self), connection, 
EAP_METHOD_SIMPLE_TYPE_MD5, simple_flags);
+               gtk_list_store_append (auth_model, &iter);
+               gtk_list_store_set (auth_model, &iter,
+                                       AUTH_NAME_COLUMN, _("MD5"),
+                                       AUTH_METHOD_COLUMN, em_md5,
+                                       -1);
+               if (default_method && (active < 0) && !strcmp (default_method, "md5"))
+                       active = item;
+               item++;
+       }
+
+       em_tls = eap_method_tls_new (WIRELESS_SECURITY (self), connection, FALSE, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("TLS"),
+                           AUTH_METHOD_COLUMN, em_tls,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "tls"))
+               active = item;
+       item++;
+
+       if (!wired) {
+               g_autoptr(EAPMethodLEAP) em_leap = NULL;
+
+               em_leap = eap_method_leap_new (WIRELESS_SECURITY (self), connection, secrets_only);
+               gtk_list_store_append (auth_model, &iter);
+               gtk_list_store_set (auth_model, &iter,
+                                   AUTH_NAME_COLUMN, _("LEAP"),
+                                   AUTH_METHOD_COLUMN, em_leap,
+                                   -1);
+               if (default_method && (active < 0) && !strcmp (default_method, "leap"))
+                       active = item;
+               item++;
+       }
+
+       em_pwd = eap_method_simple_new (WIRELESS_SECURITY (self), connection, EAP_METHOD_SIMPLE_TYPE_PWD, 
simple_flags);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("PWD"),
+                           AUTH_METHOD_COLUMN, em_pwd,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "pwd"))
+               active = item;
+       item++;
+
+       em_fast = eap_method_fast_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("FAST"),
+                           AUTH_METHOD_COLUMN, em_fast,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "fast"))
+               active = item;
+       item++;
+
+       em_ttls = eap_method_ttls_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("Tunneled TLS"),
+                           AUTH_METHOD_COLUMN, em_ttls,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "ttls"))
+               active = item;
+       item++;
+
+       em_peap = eap_method_peap_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("Protected EAP (PEAP)"),
+                           AUTH_METHOD_COLUMN, em_peap,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "peap"))
+               active = item;
+       item++;
+
+       gtk_combo_box_set_model (self->auth_combo, GTK_TREE_MODEL (auth_model));
+       gtk_combo_box_set_active (self->auth_combo, active < 0 ? 0 : (guint32) active);
 
        if (secrets_only) {
                gtk_widget_hide (GTK_WIDGET (self->auth_combo));
diff --git a/panels/network/wireless-security/ws-wpa-eap.c b/panels/network/wireless-security/ws-wpa-eap.c
index a70cafee0..6c7aed573 100644
--- a/panels/network/wireless-security/ws-wpa-eap.c
+++ b/panels/network/wireless-security/ws-wpa-eap.c
@@ -28,6 +28,12 @@
 #include "ws-wpa-eap.h"
 #include "wireless-security.h"
 #include "eap-method.h"
+#include "eap-method-fast.h"
+#include "eap-method-leap.h"
+#include "eap-method-peap.h"
+#include "eap-method-simple.h"
+#include "eap-method-tls.h"
+#include "eap-method-ttls.h"
 
 struct _WirelessSecurityWPAEAP {
        WirelessSecurity parent;
@@ -43,6 +49,24 @@ struct _WirelessSecurityWPAEAP {
 
 G_DEFINE_TYPE (WirelessSecurityWPAEAP, ws_wpa_eap, wireless_security_get_type ())
 
+#define AUTH_NAME_COLUMN   0
+#define AUTH_METHOD_COLUMN 1
+
+static EAPMethod *
+get_eap (WirelessSecurityWPAEAP *self)
+{
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       g_autoptr(EAPMethod) eap = NULL;
+
+       model = gtk_combo_box_get_model (self->auth_combo);
+       if (!gtk_combo_box_get_active_iter (self->auth_combo, &iter))
+               return NULL;
+
+       gtk_tree_model_get (model, &iter, AUTH_METHOD_COLUMN, &eap, -1);
+       return eap;
+}
+
 static void
 ws_wpa_eap_dispose (GObject *object)
 {
@@ -65,7 +89,7 @@ static gboolean
 validate (WirelessSecurity *security, GError **error)
 {
        WirelessSecurityWPAEAP *self = WS_WPA_EAP (security);
-       return eap_method_validate (ws_802_1x_auth_combo_get_eap (self->auth_combo), error);
+       return eap_method_validate (get_eap (self), error);
 }
 
 static void
@@ -77,7 +101,36 @@ add_to_size_group (WirelessSecurity *security, GtkSizeGroup *group)
        self->size_group = g_object_ref (group);
 
        gtk_size_group_add_widget (self->size_group, GTK_WIDGET (self->auth_label));
-       eap_method_add_to_size_group (ws_802_1x_auth_combo_get_eap (self->auth_combo), self->size_group);
+       eap_method_add_to_size_group (get_eap (self), self->size_group);
+}
+
+static void
+ws_802_1x_fill_connection (WirelessSecurityWPAEAP *self, NMConnection *connection)
+{
+       NMSettingWirelessSecurity *s_wireless_sec;
+       NMSetting8021x *s_8021x;
+       NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE;
+       EAPMethod *eap;
+
+       /* Get the EAPMethod object */
+       eap = get_eap (self);
+
+       /* Get previous pasword flags, if any. Otherwise default to agent-owned secrets */
+       s_8021x = nm_connection_get_setting_802_1x (connection);
+       if (s_8021x)
+               nm_setting_get_secret_flags (NM_SETTING (s_8021x), eap_method_get_password_flags_name (eap), 
&secret_flags, NULL);
+       else
+               secret_flags = NM_SETTING_SECRET_FLAG_AGENT_OWNED;
+
+       /* Blow away the old wireless security setting by adding a clear one */
+       s_wireless_sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
+       nm_connection_add_setting (connection, (NMSetting *) s_wireless_sec);
+
+       /* Blow away the old 802.1x setting by adding a clear one */
+       s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
+       nm_connection_add_setting (connection, (NMSetting *) s_8021x);
+
+       eap_method_fill_connection (eap, connection, secret_flags);  
 }
 
 static void
@@ -86,7 +139,7 @@ fill_connection (WirelessSecurity *security, NMConnection *connection)
        WirelessSecurityWPAEAP *self = WS_WPA_EAP (security);
        NMSettingWirelessSecurity *s_wireless_sec;
 
-       ws_802_1x_fill_connection (self->auth_combo, connection);
+       ws_802_1x_fill_connection (self, connection);
 
        s_wireless_sec = nm_connection_get_setting_wireless_security (connection);
        g_assert (s_wireless_sec);
@@ -112,7 +165,7 @@ auth_combo_changed_cb (WirelessSecurityWPAEAP *self)
        for (elt = children; elt; elt = g_list_next (elt))
                gtk_container_remove (GTK_CONTAINER (self->method_box), GTK_WIDGET (elt->data));
 
-       eap = ws_802_1x_auth_combo_get_eap (self->auth_combo);
+       eap = get_eap (self);
        g_assert (eap);
 
        gtk_widget_unparent (GTK_WIDGET (eap));
@@ -154,6 +207,19 @@ ws_wpa_eap_new (NMConnection *connection,
                 gboolean secrets_only)
 {
        WirelessSecurityWPAEAP *self;
+       const gchar *user = NULL, *password = NULL;
+       gboolean always_ask = FALSE;
+       g_autoptr(GtkListStore) auth_model = NULL;
+       GtkTreeIter iter;
+       g_autoptr(EAPMethodTLS) em_tls = NULL;
+       g_autoptr(EAPMethodSimple) em_pwd = NULL;
+       g_autoptr(EAPMethodFAST) em_fast = NULL;
+       g_autoptr(EAPMethodTTLS) em_ttls = NULL;
+       g_autoptr(EAPMethodPEAP) em_peap = NULL;
+       const char *default_method = NULL, *ctype = NULL;
+       int active = -1, item = 0;
+       gboolean wired = FALSE;
+       EAPMethodSimpleFlags simple_flags = EAP_METHOD_SIMPLE_FLAG_NONE;
        g_autoptr(GError) error = NULL;
 
        self = g_object_new (ws_wpa_eap_get_type (), NULL);
@@ -169,11 +235,130 @@ ws_wpa_eap_new (NMConnection *connection,
        self->grid = GTK_GRID (gtk_builder_get_object (self->builder, "grid"));
        self->method_box = GTK_BOX (gtk_builder_get_object (self->builder, "method_box"));
 
-       ws_802_1x_auth_combo_init (WIRELESS_SECURITY (self),
-                                  self->auth_combo,
-                                  connection,
-                                  is_editor,
-                                  secrets_only);
+       /* Grab the default EAP method out of the security object */
+       if (connection) {
+               NMSettingConnection *s_con;
+               NMSetting8021x *s_8021x;
+
+               s_con = nm_connection_get_setting_connection (connection);
+               if (s_con)
+                       ctype = nm_setting_connection_get_connection_type (s_con);
+               if (   (g_strcmp0 (ctype, NM_SETTING_WIRED_SETTING_NAME) == 0)
+                   || nm_connection_get_setting_wired (connection))
+                       wired = TRUE;
+
+               s_8021x = nm_connection_get_setting_802_1x (connection);
+               if (s_8021x && nm_setting_802_1x_get_num_eap_methods (s_8021x))
+                       default_method = nm_setting_802_1x_get_eap_method (s_8021x, 0);
+       }
+
+       /* initialize WirelessSecurity userpass from connection (clear if no connection) */
+       if (connection) {
+               NMSetting8021x *setting;
+
+               setting = nm_connection_get_setting_802_1x (connection);
+               if (setting) {
+                       NMSettingSecretFlags flags;
+
+                       user = nm_setting_802_1x_get_identity (setting);
+                       password = nm_setting_802_1x_get_password (setting);
+
+                       if (nm_setting_get_secret_flags (NM_SETTING (setting), NM_SETTING_802_1X_PASSWORD, 
&flags, NULL))
+                               always_ask = !!(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED);
+               }
+       }
+       wireless_security_set_username (WIRELESS_SECURITY (self), user);
+       wireless_security_set_password (WIRELESS_SECURITY (self), password);
+       wireless_security_set_always_ask (WIRELESS_SECURITY (self), always_ask);
+       wireless_security_set_show_password (WIRELESS_SECURITY (self), FALSE);
+
+       auth_model = gtk_list_store_new (2, G_TYPE_STRING, eap_method_get_type ());
+
+       if (is_editor)
+               simple_flags |= EAP_METHOD_SIMPLE_FLAG_IS_EDITOR;
+       if (secrets_only)
+               simple_flags |= EAP_METHOD_SIMPLE_FLAG_SECRETS_ONLY;
+
+       if (wired) {
+               g_autoptr(EAPMethodSimple) em_md5 = NULL;
+
+               em_md5 = eap_method_simple_new (WIRELESS_SECURITY (self), connection, 
EAP_METHOD_SIMPLE_TYPE_MD5, simple_flags);
+               gtk_list_store_append (auth_model, &iter);
+               gtk_list_store_set (auth_model, &iter,
+                                       AUTH_NAME_COLUMN, _("MD5"),
+                                       AUTH_METHOD_COLUMN, em_md5,
+                                       -1);
+               if (default_method && (active < 0) && !strcmp (default_method, "md5"))
+                       active = item;
+               item++;
+       }
+
+       em_tls = eap_method_tls_new (WIRELESS_SECURITY (self), connection, FALSE, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("TLS"),
+                           AUTH_METHOD_COLUMN, em_tls,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "tls"))
+               active = item;
+       item++;
+
+       if (!wired) {
+               g_autoptr(EAPMethodLEAP) em_leap = NULL;
+
+               em_leap = eap_method_leap_new (WIRELESS_SECURITY (self), connection, secrets_only);
+               gtk_list_store_append (auth_model, &iter);
+               gtk_list_store_set (auth_model, &iter,
+                                   AUTH_NAME_COLUMN, _("LEAP"),
+                                   AUTH_METHOD_COLUMN, em_leap,
+                                   -1);
+               if (default_method && (active < 0) && !strcmp (default_method, "leap"))
+                       active = item;
+               item++;
+       }
+
+       em_pwd = eap_method_simple_new (WIRELESS_SECURITY (self), connection, EAP_METHOD_SIMPLE_TYPE_PWD, 
simple_flags);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("PWD"),
+                           AUTH_METHOD_COLUMN, em_pwd,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "pwd"))
+               active = item;
+       item++;
+
+       em_fast = eap_method_fast_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("FAST"),
+                           AUTH_METHOD_COLUMN, em_fast,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "fast"))
+               active = item;
+       item++;
+
+       em_ttls = eap_method_ttls_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("Tunneled TLS"),
+                           AUTH_METHOD_COLUMN, em_ttls,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "ttls"))
+               active = item;
+       item++;
+
+       em_peap = eap_method_peap_new (WIRELESS_SECURITY (self), connection, is_editor, secrets_only);
+       gtk_list_store_append (auth_model, &iter);
+       gtk_list_store_set (auth_model, &iter,
+                           AUTH_NAME_COLUMN, _("Protected EAP (PEAP)"),
+                           AUTH_METHOD_COLUMN, em_peap,
+                           -1);
+       if (default_method && (active < 0) && !strcmp (default_method, "peap"))
+               active = item;
+       item++;
+
+       gtk_combo_box_set_model (self->auth_combo, GTK_TREE_MODEL (auth_model));
+       gtk_combo_box_set_active (self->auth_combo, active < 0 ? 0 : (guint32) active);
 
        if (secrets_only) {
                gtk_widget_hide (GTK_WIDGET (self->auth_combo));
@@ -189,5 +374,5 @@ ws_wpa_eap_new (NMConnection *connection,
 void
 ws_wpa_eap_fill_connection (WirelessSecurityWPAEAP *self, NMConnection *connection)
 {
-        ws_802_1x_fill_connection (self->auth_combo, connection);
+        ws_802_1x_fill_connection (self, connection);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]