From 0b57fe0c5645e8f4210d2566bbd3c2b92f7698d8 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 8 Nov 2012 13:15:51 -0500 Subject: [PATCH] core: simplify nm_device_get_best_auto_connection() implementations Filter out non-autoconnect connections in the generic NMDevice method rather than requiring each subclass to do it. https://bugzilla.gnome.org/show_bug.cgi?id=688284 --- src/modem-manager/nm-modem-cdma.c | 13 ++----------- src/modem-manager/nm-modem-gsm.c | 13 ++----------- src/nm-device-adsl.c | 14 ++------------ src/nm-device-bond.c | 6 +----- src/nm-device-bt.c | 9 +-------- src/nm-device-ethernet.c | 7 +------ src/nm-device-infiniband.c | 6 ------ src/nm-device-vlan.c | 6 +----- src/nm-device-wifi.c | 8 +------- src/nm-device.c | 21 ++++++++++++++++++++- 10 files changed, 31 insertions(+), 72 deletions(-) diff --git a/src/modem-manager/nm-modem-cdma.c b/src/modem-manager/nm-modem-cdma.c index 73c87c2892..6bed8c2a48 100644 --- a/src/modem-manager/nm-modem-cdma.c +++ b/src/modem-manager/nm-modem-cdma.c @@ -201,18 +201,9 @@ get_best_auto_connection (NMModem *modem, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_CDMA_SETTING_NAME)) - continue; - - return connection; + if (nm_connection_is_type (connection, NM_SETTING_CDMA_SETTING_NAME)) + return connection; } return NULL; } diff --git a/src/modem-manager/nm-modem-gsm.c b/src/modem-manager/nm-modem-gsm.c index b6d3c857a7..be793db9c8 100644 --- a/src/modem-manager/nm-modem-gsm.c +++ b/src/modem-manager/nm-modem-gsm.c @@ -475,18 +475,9 @@ get_best_auto_connection (NMModem *modem, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_GSM_SETTING_NAME)) - continue; - - return connection; + if (nm_connection_is_type (connection, NM_SETTING_GSM_SETTING_NAME)) + return connection; } return NULL; } diff --git a/src/nm-device-adsl.c b/src/nm-device-adsl.c index ac10d9cf9a..db678d9f27 100644 --- a/src/nm-device-adsl.c +++ b/src/nm-device-adsl.c @@ -208,22 +208,12 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - NMSettingAdsl *s_adsl; if (!nm_connection_is_type (connection, NM_SETTING_ADSL_SETTING_NAME)) continue; - s_adsl = nm_connection_get_setting_adsl (connection); - if (!s_adsl) - continue; - - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - return connection; + if (nm_connection_get_setting_adsl (connection)) + return connection; } return NULL; } diff --git a/src/nm-device-bond.c b/src/nm-device-bond.c index 0986bc7d11..bcb82fd4e9 100644 --- a/src/nm-device-bond.c +++ b/src/nm-device-bond.c @@ -164,12 +164,8 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if ( nm_setting_connection_get_autoconnect (s_con) - && match_bond_connection (dev, connection, NULL)) + if (match_bond_connection (dev, connection, NULL)) return connection; } return NULL; diff --git a/src/nm-device-bt.c b/src/nm-device-bt.c index 448ec87c23..366e2683f0 100644 --- a/src/nm-device-bt.c +++ b/src/nm-device-bt.c @@ -149,16 +149,9 @@ get_best_auto_connection (NMDevice *device, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; guint32 bt_type; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_BLUETOOTH_SETTING_NAME)) + if (!nm_connection_is_type (connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) continue; bt_type = get_connection_bt_type (connection); diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 385fce564e..cf4541db1b 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -624,13 +624,8 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - - if ( nm_setting_connection_get_autoconnect (s_con) - && match_ethernet_connection (dev, connection, TRUE, NULL)) + if (match_ethernet_connection (dev, connection, TRUE, NULL)) return connection; } diff --git a/src/nm-device-infiniband.c b/src/nm-device-infiniband.c index aa50ef9afd..c7dcf19c87 100644 --- a/src/nm-device-infiniband.c +++ b/src/nm-device-infiniband.c @@ -158,16 +158,10 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; NMSettingInfiniband *s_infiniband; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if (!nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME)) continue; - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; s_infiniband = nm_connection_get_setting_infiniband (connection); if (!s_infiniband) diff --git a/src/nm-device-vlan.c b/src/nm-device-vlan.c index bd6ae3b8e4..d8e1f3c1d4 100644 --- a/src/nm-device-vlan.c +++ b/src/nm-device-vlan.c @@ -300,12 +300,8 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if ( nm_setting_connection_get_autoconnect (s_con) - && match_vlan_connection (NM_DEVICE_VLAN (dev), connection, NULL)) + if (match_vlan_connection (NM_DEVICE_VLAN (dev), connection, NULL)) return connection; } return NULL; diff --git a/src/nm-device-wifi.c b/src/nm-device-wifi.c index 36721b8811..67a10d43f0 100644 --- a/src/nm-device-wifi.c +++ b/src/nm-device-wifi.c @@ -1386,7 +1386,6 @@ get_best_auto_connection (NMDevice *dev, for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; NMSettingWireless *s_wireless; const GByteArray *mac; const GSList *mac_blacklist, *mac_blacklist_iter; @@ -1395,12 +1394,7 @@ get_best_auto_connection (NMDevice *dev, const char *method = NULL; guint64 timestamp = 0; - s_con = nm_connection_get_setting_connection (connection); - if (s_con == NULL) - continue; - if (strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_WIRELESS_SETTING_NAME)) - continue; - if (!nm_setting_connection_get_autoconnect (s_con)) + if (!nm_connection_is_type (connection, NM_SETTING_WIRELESS_SETTING_NAME)) continue; /* Don't autoconnect to networks that have been tried at least once diff --git a/src/nm-device.c b/src/nm-device.c index a8113e30bf..a499db9cc5 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -1161,6 +1161,8 @@ nm_device_get_best_auto_connection (NMDevice *dev, char **specific_object) { guint32 caps; + GSList *iter, *available_conns; + NMConnection *best_connection; g_return_val_if_fail (NM_IS_DEVICE (dev), NULL); g_return_val_if_fail (specific_object != NULL, NULL); @@ -1174,7 +1176,24 @@ nm_device_get_best_auto_connection (NMDevice *dev, if (!NM_DEVICE_GET_CLASS (dev)->get_best_auto_connection) return NULL; - return NM_DEVICE_GET_CLASS (dev)->get_best_auto_connection (dev, connections, specific_object); + available_conns = NULL; + for (iter = connections; iter; iter = iter->next) { + NMConnection *connection = NM_CONNECTION (iter->data); + NMSettingConnection *s_con; + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + if (nm_setting_connection_get_autoconnect (s_con)) + available_conns = g_slist_prepend (available_conns, connection); + } + + if (!available_conns) + return NULL; + + best_connection = NM_DEVICE_GET_CLASS (dev)->get_best_auto_connection (dev, available_conns, specific_object); + + g_slist_free (available_conns); + return best_connection; } gboolean