network: Replace method with a helper function.

This removes a case where an object was being temporarily created just to use
this mehtod.
This commit is contained in:
Robert Ancell 2019-10-23 14:14:48 +13:00
parent 966cb97d45
commit d7e17ca02f
6 changed files with 32 additions and 46 deletions

View file

@ -127,12 +127,13 @@ device_off_toggled (NetDeviceBluetooth *self)
if (self->updating_device) if (self->updating_device)
return; return;
client = net_object_get_client (NET_OBJECT (self));
connection = net_device_get_find_connection (client, net_device_get_nm_device (NET_DEVICE (self)));
if (connection == NULL)
return;
active = gtk_switch_get_active (self->device_off_switch); active = gtk_switch_get_active (self->device_off_switch);
if (active) { if (active) {
client = net_object_get_client (NET_OBJECT (self));
connection = net_device_get_find_connection (NET_DEVICE (self));
if (connection == NULL)
return;
nm_client_activate_connection_async (client, nm_client_activate_connection_async (client,
connection, connection,
net_device_get_nm_device (NET_DEVICE (self)), net_device_get_nm_device (NET_DEVICE (self)),
@ -140,11 +141,7 @@ device_off_toggled (NetDeviceBluetooth *self)
} else { } else {
const gchar *uuid; const gchar *uuid;
connection = net_device_get_find_connection (NET_DEVICE (self));
if (connection == NULL)
return;
uuid = nm_connection_get_uuid (connection); uuid = nm_connection_get_uuid (connection);
client = net_object_get_client (NET_OBJECT (self));
acs = nm_client_get_active_connections (client); acs = nm_client_get_active_connections (client);
for (i = 0; acs && i < acs->len; i++) { for (i = 0; acs && i < acs->len; i++) {
a = (NMActiveConnection*)acs->pdata[i]; a = (NMActiveConnection*)acs->pdata[i];
@ -164,7 +161,7 @@ edit_connection (NetDeviceBluetooth *self)
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
NMConnection *connection; NMConnection *connection;
connection = net_device_get_find_connection (NET_DEVICE (self)); connection = net_device_get_find_connection (net_object_get_client (NET_OBJECT (self)), net_device_get_nm_device (NET_DEVICE (self)));
uuid = nm_connection_get_uuid (connection); uuid = nm_connection_get_uuid (connection);
cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid); cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid);
g_debug ("Launching '%s'\n", cmdline); g_debug ("Launching '%s'\n", cmdline);

View file

@ -370,7 +370,7 @@ populate_ui (NetDeviceEthernet *self)
} }
g_list_free (children); g_list_free (children);
connections = net_device_get_valid_connections (NET_DEVICE (self)); connections = net_device_get_valid_connections (net_object_get_client (NET_OBJECT (self)), net_device_get_nm_device (NET_DEVICE (self)));
for (l = connections; l; l = l->next) { for (l = connections; l; l = l->next) {
NMConnection *connection = l->data; NMConnection *connection = l->data;
if (!g_hash_table_contains (self->connections, connection)) { if (!g_hash_table_contains (self->connections, connection)) {
@ -461,7 +461,7 @@ device_off_toggled (NetDeviceEthernet *self)
nm_device = net_device_get_nm_device (NET_DEVICE (self)); nm_device = net_device_get_nm_device (NET_DEVICE (self));
if (gtk_switch_get_active (self->device_off_switch)) { if (gtk_switch_get_active (self->device_off_switch)) {
connection = net_device_get_find_connection (NET_DEVICE (self)); connection = net_device_get_find_connection (client, nm_device);
if (connection != NULL) { if (connection != NULL) {
nm_client_activate_connection_async (client, nm_client_activate_connection_async (client,
connection, connection,

View file

@ -210,7 +210,7 @@ device_add_device_connections (NetDeviceMobile *self,
NMConnection *connection; NMConnection *connection;
/* get the list of available connections for this device */ /* get the list of available connections for this device */
list = net_device_get_valid_connections (NET_DEVICE (self)); list = net_device_get_valid_connections (net_object_get_client (NET_OBJECT (self)), nm_device);
gtk_list_store_clear (liststore); gtk_list_store_clear (liststore);
active_connection = nm_device_get_active_connection (nm_device); active_connection = nm_device_get_active_connection (nm_device);
for (l = list; l; l = g_slist_next (l)) { for (l = list; l; l = g_slist_next (l)) {
@ -398,7 +398,7 @@ nm_device_mobile_refresh_ui (NetDeviceMobile *self)
gtk_label_set_label (self->status_label, status); gtk_label_set_label (self->status_label, status);
/* sensitive for other connection types if the device is currently connected */ /* sensitive for other connection types if the device is currently connected */
is_connected = net_device_get_find_connection (NET_DEVICE (self)) != NULL; is_connected = net_device_get_find_connection (net_object_get_client (NET_OBJECT (self)), nm_device) != NULL;
gtk_widget_set_sensitive (GTK_WIDGET (self->options_button), is_connected); gtk_widget_set_sensitive (GTK_WIDGET (self->options_button), is_connected);
caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (nm_device)); caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (nm_device));
@ -495,12 +495,13 @@ device_off_toggled (NetDeviceMobile *self)
if (self->updating_device) if (self->updating_device)
return; return;
client = net_object_get_client (NET_OBJECT (self));
connection = net_device_get_find_connection (client, net_device_get_nm_device (NET_DEVICE (self)));
if (connection == NULL)
return;
active = gtk_switch_get_active (self->device_off_switch); active = gtk_switch_get_active (self->device_off_switch);
if (active) { if (active) {
client = net_object_get_client (NET_OBJECT (self));
connection = net_device_get_find_connection (NET_DEVICE (self));
if (connection == NULL)
return;
nm_client_activate_connection_async (client, nm_client_activate_connection_async (client,
connection, connection,
net_device_get_nm_device (NET_DEVICE (self)), net_device_get_nm_device (NET_DEVICE (self)),
@ -508,11 +509,7 @@ device_off_toggled (NetDeviceMobile *self)
} else { } else {
const gchar *uuid; const gchar *uuid;
connection = net_device_get_find_connection (NET_DEVICE (self));
if (connection == NULL)
return;
uuid = nm_connection_get_uuid (connection); uuid = nm_connection_get_uuid (connection);
client = net_object_get_client (NET_OBJECT (self));
acs = nm_client_get_active_connections (client); acs = nm_client_get_active_connections (client);
for (i = 0; acs && i < acs->len; i++) { for (i = 0; acs && i < acs->len; i++) {
a = (NMActiveConnection*)acs->pdata[i]; a = (NMActiveConnection*)acs->pdata[i];
@ -532,7 +529,7 @@ edit_connection (NetDeviceMobile *self)
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
NMConnection *connection; NMConnection *connection;
connection = net_device_get_find_connection (NET_DEVICE (self)); connection = net_device_get_find_connection (net_object_get_client (NET_OBJECT (self)), net_device_get_nm_device (NET_DEVICE (self)));
uuid = nm_connection_get_uuid (connection); uuid = nm_connection_get_uuid (connection);
cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid); cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid);
g_debug ("Launching '%s'\n", cmdline); g_debug ("Launching '%s'\n", cmdline);

View file

@ -169,17 +169,7 @@ static NMConnection *
find_connection_for_device (NetDeviceWifi *self, find_connection_for_device (NetDeviceWifi *self,
NMDevice *device) NMDevice *device)
{ {
g_autoptr(NetDevice) tmp = NULL; return net_device_get_find_connection (net_object_get_client (NET_OBJECT (self)), device);
NMConnection *connection;
NMClient *client;
client = net_object_get_client (NET_OBJECT (self));
tmp = g_object_new (NET_TYPE_DEVICE,
"client", client,
"nm-device", device,
NULL);
connection = net_device_get_find_connection (tmp);
return connection;
} }
static gboolean static gboolean
@ -726,7 +716,7 @@ net_device_wifi_get_hotspot_connection (NetDeviceWifi *self)
GSList *connections, *l; GSList *connections, *l;
NMConnection *c = NULL; NMConnection *c = NULL;
connections = net_device_get_valid_connections (NET_DEVICE (self)); connections = net_device_get_valid_connections (net_object_get_client (NET_OBJECT (self)), net_device_get_nm_device (NET_DEVICE (self)));
for (l = connections; l; l = l->next) { for (l = connections; l; l = l->next) {
NMConnection *tmp = l->data; NMConnection *tmp = l->data;
if (is_hotspot_connection (tmp)) { if (is_hotspot_connection (tmp)) {

View file

@ -117,20 +117,19 @@ compare_mac_device_with_mac_connection (NMDevice *device,
} }
NMConnection * NMConnection *
net_device_get_find_connection (NetDevice *self) net_device_get_find_connection (NMClient *client, NMDevice *device)
{ {
NetDevicePrivate *priv = net_device_get_instance_private (self);
GSList *list, *iterator; GSList *list, *iterator;
NMConnection *connection = NULL; NMConnection *connection = NULL;
NMActiveConnection *ac; NMActiveConnection *ac;
/* is the device available in a active connection? */ /* is the device available in a active connection? */
ac = nm_device_get_active_connection (priv->nm_device); ac = nm_device_get_active_connection (device);
if (ac) if (ac)
return (NMConnection*) nm_active_connection_get_connection (ac); return (NMConnection*) nm_active_connection_get_connection (ac);
/* not found in active connections - check all available connections */ /* not found in active connections - check all available connections */
list = net_device_get_valid_connections (self); list = net_device_get_valid_connections (client, device);
if (list != NULL) { if (list != NULL) {
/* if list has only one connection, use this connection */ /* if list has only one connection, use this connection */
if (g_slist_length (list) == 1) { if (g_slist_length (list) == 1) {
@ -141,7 +140,7 @@ net_device_get_find_connection (NetDevice *self)
/* is there connection with the MAC address of the device? */ /* is there connection with the MAC address of the device? */
for (iterator = list; iterator; iterator = iterator->next) { for (iterator = list; iterator; iterator = iterator->next) {
connection = iterator->data; connection = iterator->data;
if (compare_mac_device_with_mac_connection (priv->nm_device, if (compare_mac_device_with_mac_connection (device,
connection)) { connection)) {
goto out; goto out;
} }
@ -265,7 +264,7 @@ net_device_init (NetDevice *self)
} }
GSList * GSList *
net_device_get_valid_connections (NetDevice *self) net_device_get_valid_connections (NMClient *client, NMDevice *device)
{ {
GSList *valid; GSList *valid;
NMConnection *connection; NMConnection *connection;
@ -276,10 +275,10 @@ net_device_get_valid_connections (NetDevice *self)
GPtrArray *filtered; GPtrArray *filtered;
guint i; guint i;
all = nm_client_get_connections (net_object_get_client (NET_OBJECT (self))); all = nm_client_get_connections (client);
filtered = nm_device_filter_connections (net_device_get_nm_device (self), all); filtered = nm_device_filter_connections (device, all);
active_connection = nm_device_get_active_connection (net_device_get_nm_device (self)); active_connection = nm_device_get_active_connection (device);
active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL; active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL;
valid = NULL; valid = NULL;

View file

@ -37,9 +37,12 @@ struct _NetDeviceClass
NetObjectClass parent_class; NetObjectClass parent_class;
}; };
NMDevice *net_device_get_nm_device (NetDevice *device); NMDevice *net_device_get_nm_device (NetDevice *device);
NMConnection *net_device_get_find_connection (NetDevice *device);
GSList *net_device_get_valid_connections (NetDevice *device); NMConnection *net_device_get_find_connection (NMClient *client,
NMDevice *device);
GSList *net_device_get_valid_connections (NMClient *client,
NMDevice *device);
G_END_DECLS G_END_DECLS