[gnome-control-center/wip/networking3: 17/17] network: add net_device_get_valid_connections() and use it everywhere



commit 7f3512a50b2313f4f4a689c3a8a2df9990919f48
Author: Dan Winship <danw gnome org>
Date:   Mon Jan 28 16:27:47 2013 -0500

    network: add net_device_get_valid_connections() and use it everywhere
    
    In addition to simplifying a bunch of places that were calling
    nm_remote_settings_list_connections() +
    nm_device_filter_connections(), this also ensures we filter out slave
    connections everywhere (except when they are the active connection).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677145

 panels/network/net-device-ethernet.c |   35 +------------------
 panels/network/net-device-mobile.c   |   10 +----
 panels/network/net-device-wifi.c     |   41 +++++----------------
 panels/network/net-device.c          |   64 ++++++++++++++++++----------------
 panels/network/net-device.h          |    2 +
 5 files changed, 49 insertions(+), 103 deletions(-)
---
diff --git a/panels/network/net-device-ethernet.c b/panels/network/net-device-ethernet.c
index d86baee..762448a 100644
--- a/panels/network/net-device-ethernet.c
+++ b/panels/network/net-device-ethernet.c
@@ -56,35 +56,6 @@ device_ethernet_get_speed (NetDeviceSimple *device_simple)
                 return NULL;
 }
 
-static GSList *
-valid_connections_for_device (NMRemoteSettings *remote_settings,
-                              NetDevice        *device)
-{
-        GSList *all, *filtered, *iterator, *valid;
-        NMConnection *connection;
-        NMSettingConnection *s_con;
-
-        all = nm_remote_settings_list_connections (remote_settings);
-        filtered = nm_device_filter_connections (net_device_get_nm_device (device), all);
-        g_slist_free (all);
-
-        valid = NULL;
-        for (iterator = filtered; iterator; iterator = iterator->next) {
-                connection = iterator->data;
-                s_con = nm_connection_get_setting_connection (connection);
-                if (!s_con)
-                        continue;
-
-                if (nm_setting_connection_get_master (s_con))
-                        continue;
-
-                valid = g_slist_prepend (valid, connection);
-        }
-        g_slist_free (filtered);
-
-        return g_slist_reverse (valid);
-}
-
 static GtkWidget *
 device_ethernet_add_to_notebook (NetObject    *object,
                                  GtkNotebook  *notebook,
@@ -391,7 +362,6 @@ connection_removed (NMRemoteConnection *connection,
 static void
 populate_ui (NetDeviceEthernet *device)
 {
-        NMRemoteSettings *settings;
         GList *children, *c;
         GSList *connections, *l;
         NMConnection *connection;
@@ -409,10 +379,7 @@ populate_ui (NetDeviceEthernet *device)
         }
         g_list_free (children);
 
-        settings = net_object_get_remote_settings (NET_OBJECT (device));
-        connections = valid_connections_for_device (settings, NET_DEVICE (device));
-
-
+        connections = net_device_get_valid_connections (NET_DEVICE (device));
         for (l = connections; l; l = l->next) {
                 NMConnection *connection = l->data;
                 if (!g_object_get_data (G_OBJECT (connection), "removed_signal_handler")) {
diff --git a/panels/network/net-device-mobile.c b/panels/network/net-device-mobile.c
index 1952718..1ca2532 100644
--- a/panels/network/net-device-mobile.c
+++ b/panels/network/net-device-mobile.c
@@ -184,21 +184,16 @@ device_add_device_connections (NetDeviceMobile *device_mobile,
                                GtkComboBox *combobox)
 {
         NetDeviceMobilePrivate *priv = device_mobile->priv;
-        GSList *filtered;
         GSList *list, *l;
         GtkTreeIter treeiter;
         NMActiveConnection *active_connection;
         NMConnection *connection;
-        NMRemoteSettings *remote_settings;
 
         /* get the list of available connections for this device */
-        remote_settings = net_object_get_remote_settings (NET_OBJECT (device_mobile));
-        g_assert (remote_settings != NULL);
-        list = nm_remote_settings_list_connections (remote_settings);
-        filtered = nm_device_filter_connections (nm_device, list);
+        list = net_device_get_valid_connections (NET_DEVICE (device_mobile));
         gtk_list_store_clear (liststore);
         active_connection = nm_device_get_active_connection (nm_device);
-        for (l = filtered; l; l = g_slist_next (l)) {
+        for (l = list; l; l = g_slist_next (l)) {
                 connection = NM_CONNECTION (l->data);
                 gtk_list_store_append (liststore, &treeiter);
                 gtk_list_store_set (liststore,
@@ -226,7 +221,6 @@ device_add_device_connections (NetDeviceMobile *device_mobile,
                             -1);
 
         g_slist_free (list);
-        g_slist_free (filtered);
 }
 
 static void
diff --git a/panels/network/net-device-wifi.c b/panels/network/net-device-wifi.c
index aedf2d3..e08fa0a 100644
--- a/panels/network/net-device-wifi.c
+++ b/panels/network/net-device-wifi.c
@@ -700,11 +700,9 @@ wireless_try_to_connect (NetDeviceWifi *device_wifi,
         const GByteArray *ssid;
         const gchar *ssid_tmp;
         GSList *list, *l;
-        GSList *filtered;
         NMConnection *connection_activate = NULL;
         NMDevice *device;
         NMSettingWireless *setting_wireless;
-        NMRemoteSettings *remote_settings;
         NMClient *client;
 
         if (device_wifi->priv->updating_device)
@@ -721,12 +719,9 @@ wireless_try_to_connect (NetDeviceWifi *device_wifi,
                  ssid_target, ap_object_path);
 
         /* look for an existing connection we can use */
-        remote_settings = net_object_get_remote_settings (NET_OBJECT (device_wifi));
-        list = nm_remote_settings_list_connections (remote_settings);
-        g_debug ("%i existing remote connections available", g_slist_length (list));
-        filtered = nm_device_filter_connections (device, list);
-        g_debug ("%i suitable remote connections to check", g_slist_length (filtered));
-        for (l = filtered; l; l = g_slist_next (l)) {
+        list = net_device_get_valid_connections (NET_DEVICE (device_wifi));
+        g_debug ("%i suitable remote connections to check", g_slist_length (list));
+        for (l = list; l; l = g_slist_next (l)) {
                 NMConnection *connection;
 
                 connection = NM_CONNECTION (l->data);
@@ -746,7 +741,6 @@ wireless_try_to_connect (NetDeviceWifi *device_wifi,
         }
 
         g_slist_free (list);
-        g_slist_free (filtered);
 
         /* activate the connection */
         client = net_object_get_client (NET_OBJECT (device_wifi));
@@ -974,27 +968,22 @@ start_shared_connection (NetDeviceWifi *device_wifi)
         const gchar *str_mac;
         struct ether_addr *bin_mac;
         GSList *connections;
-        GSList *filtered;
         GSList *l;
         NMClient *client;
-        NMRemoteSettings *remote_settings;
 
         device = net_device_get_nm_device (NET_DEVICE (device_wifi));
         g_assert (nm_device_get_device_type (device) == NM_DEVICE_TYPE_WIFI);
 
-        remote_settings = net_object_get_remote_settings (NET_OBJECT (device_wifi));
-        connections = nm_remote_settings_list_connections (remote_settings);
-        filtered = nm_device_filter_connections (device, connections);
-        g_slist_free (connections);
+        connections = net_device_get_valid_connections (NET_DEVICE (device_wifi));
         c = NULL;
-        for (l = filtered; l; l = l->next) {
+        for (l = connections; l; l = l->next) {
                 tmp = l->data;
                 if (is_hotspot_connection (tmp)) {
                         c = tmp;
                         break;
                 }
         }
-        g_slist_free (filtered);
+        g_slist_free (connections);
 
         client = net_object_get_client (NET_OBJECT (device_wifi));
         if (c != NULL) {
@@ -1680,9 +1669,7 @@ open_history (NetDeviceWifi *device_wifi)
         GtkWidget *button;
         GtkWidget *forget;
         GtkWidget *swin;
-        NMRemoteSettings *settings;
         GSList *connections;
-        GSList *filtered;
         GSList *l;
         const GPtrArray *aps;
         GPtrArray *aps_unique = NULL;
@@ -1743,15 +1730,13 @@ open_history (NetDeviceWifi *device_wifi)
 
         nm_device = net_device_get_nm_device (NET_DEVICE (device_wifi));
 
-        settings = net_object_get_remote_settings (NET_OBJECT (device_wifi));
-        connections = nm_remote_settings_list_connections (settings);
-        filtered = nm_device_filter_connections (nm_device, connections);
+        connections = net_device_get_valid_connections (NET_DEVICE (device_wifi));
 
         aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nm_device));
         aps_unique = panel_get_strongest_unique_aps (aps);
         active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (nm_device));
 
-        for (l = filtered; l; l = l->next) {
+        for (l = connections; l; l = l->next) {
                 NMConnection *connection = l->data;
                 NMAccessPoint *ap = NULL;
                 NMSetting *setting;
@@ -1779,7 +1764,6 @@ open_history (NetDeviceWifi *device_wifi)
                 }
         }
         g_slist_free (connections);
-        g_slist_free (filtered);
 
         gtk_window_present (GTK_WINDOW (dialog));
 }
@@ -1792,9 +1776,7 @@ populate_ap_list (NetDeviceWifi *device_wifi)
         GtkSizeGroup *rows;
         GtkSizeGroup *icons;
         NMDevice *nm_device;
-        NMRemoteSettings *settings;
         GSList *connections;
-        GSList *filtered;
         GSList *l;
         const GPtrArray *aps;
         GPtrArray *aps_unique = NULL;
@@ -1819,9 +1801,7 @@ populate_ap_list (NetDeviceWifi *device_wifi)
 
         nm_device = net_device_get_nm_device (NET_DEVICE (device_wifi));
 
-        settings = net_object_get_remote_settings (NET_OBJECT (device_wifi));
-        connections = nm_remote_settings_list_connections (settings);
-        filtered = nm_device_filter_connections (nm_device, connections);
+        connections = net_device_get_valid_connections (NET_DEVICE (device_wifi));
 
         aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nm_device));
         aps_unique = panel_get_strongest_unique_aps (aps);
@@ -1833,7 +1813,7 @@ populate_ap_list (NetDeviceWifi *device_wifi)
                 NMConnection *connection = NULL;
                 ap = NM_ACCESS_POINT (g_ptr_array_index (aps_unique, i));
                 ssid_ap = nm_access_point_get_ssid (ap);
-                for (l = filtered; l; l = l->next) {
+                for (l = connections; l; l = l->next) {
                         connection = l->data;
                         NMSetting *setting;
                         const GByteArray *ssid;
@@ -1858,7 +1838,6 @@ populate_ap_list (NetDeviceWifi *device_wifi)
         }
 
         g_slist_free (connections);
-        g_slist_free (filtered);
 }
 
 static void
diff --git a/panels/network/net-device.c b/panels/network/net-device.c
index 453946f..ec8be06 100644
--- a/panels/network/net-device.c
+++ b/panels/network/net-device.c
@@ -165,35 +165,6 @@ compare_mac_device_with_mac_connection (NMDevice *device,
         return FALSE;
 }
 
-static GSList *
-valid_connections_for_device (NMRemoteSettings *remote_settings,
-                              NetDevice *device)
-{
-        GSList *all, *filtered, *iterator, *valid;
-        NMConnection *connection;
-        NMSettingConnection *s_con;
-
-        all = nm_remote_settings_list_connections (remote_settings);
-        filtered = nm_device_filter_connections (device->priv->nm_device, all);
-        g_slist_free (all);
-
-        valid = NULL;
-        for (iterator = filtered; iterator; iterator = iterator->next) {
-                connection = iterator->data;
-                s_con = nm_connection_get_setting_connection (connection);
-                if (!s_con)
-                        continue;
-
-                if (nm_setting_connection_get_master (s_con))
-                        continue;
-
-                valid = g_slist_prepend (valid, connection);
-        }
-        g_slist_free (filtered);
-
-        return g_slist_reverse (valid);
-}
-
 static NMConnection *
 net_device_real_get_find_connection (NetDevice *device)
 {
@@ -211,7 +182,7 @@ net_device_real_get_find_connection (NetDevice *device)
         }
 
         /* not found in active connections - check all available connections */
-        list = valid_connections_for_device (remote_settings, device);
+        list = net_device_get_valid_connections (device);
         if (list != NULL) {
                 /* if list has only one connection, use this connection */
                 if (g_slist_length (list) == 1) {
@@ -387,3 +358,36 @@ net_device_new (void)
         return NET_DEVICE (device);
 }
 
+GSList *
+net_device_get_valid_connections (NetDevice *device)
+{
+        GSList *all, *filtered, *iterator, *valid;
+        NMConnection *connection;
+        NMSettingConnection *s_con;
+        NMActiveConnection *active_connection;
+        const char *active_uuid;
+
+        all = nm_remote_settings_list_connections (net_object_get_remote_settings (NET_OBJECT (device)));
+        filtered = nm_device_filter_connections (net_device_get_nm_device (device), all);
+        g_slist_free (all);
+
+        active_connection = nm_device_get_active_connection (net_device_get_nm_device (device));
+        active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL;
+
+        valid = NULL;
+        for (iterator = filtered; iterator; iterator = iterator->next) {
+                connection = iterator->data;
+                s_con = nm_connection_get_setting_connection (connection);
+                if (!s_con)
+                        continue;
+
+                if (nm_setting_connection_get_master (s_con) &&
+                    g_strcmp0 (nm_setting_connection_get_uuid (s_con), active_uuid) != 0)
+                        continue;
+
+                valid = g_slist_prepend (valid, connection);
+        }
+        g_slist_free (filtered);
+
+        return g_slist_reverse (valid);
+}
diff --git a/panels/network/net-device.h b/panels/network/net-device.h
index f8e9dd0..03fde5e 100644
--- a/panels/network/net-device.h
+++ b/panels/network/net-device.h
@@ -58,6 +58,8 @@ NetDevice       *net_device_new                         (void);
 NMDevice        *net_device_get_nm_device               (NetDevice      *device);
 NMConnection    *net_device_get_find_connection         (NetDevice      *device);
 
+GSList          *net_device_get_valid_connections       (NetDevice      *device);
+
 G_END_DECLS
 
 #endif /* __NET_DEVICE_H */



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