From 0197654177cb952fb115f7c4da74b9b314e01bb4 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 24 Oct 2019 12:13:40 +1300 Subject: [PATCH] network: Move helper functions into panel-common.c --- panels/network/net-device-bluetooth.c | 7 +- panels/network/net-device-ethernet.c | 17 +-- panels/network/net-device-mobile.c | 7 +- panels/network/net-device-wifi.c | 7 +- panels/network/net-device.c | 169 -------------------------- panels/network/net-device.h | 9 -- panels/network/net-object.c | 8 -- panels/network/net-object.h | 2 - panels/network/panel-common.c | 144 ++++++++++++++++++++++ panels/network/panel-common.h | 6 + 10 files changed, 171 insertions(+), 205 deletions(-) diff --git a/panels/network/net-device-bluetooth.c b/panels/network/net-device-bluetooth.c index dd5b99ff6..1888bd988 100644 --- a/panels/network/net-device-bluetooth.c +++ b/panels/network/net-device-bluetooth.c @@ -109,9 +109,9 @@ nm_device_bluetooth_refresh_ui (NetDeviceBluetooth *self) } static void -device_bluetooth_refresh (NetObject *object) +device_state_changed_cb (NetDeviceBluetooth *self) { - NetDeviceBluetooth *self = NET_DEVICE_BLUETOOTH (object); + net_object_emit_changed (NET_OBJECT (self)); nm_device_bluetooth_refresh_ui (self); } @@ -187,7 +187,6 @@ net_device_bluetooth_class_init (NetDeviceBluetoothClass *klass) object_class->finalize = net_device_bluetooth_finalize; parent_class->get_widget = device_bluetooth_get_widget; - parent_class->refresh = device_bluetooth_refresh; } static void @@ -228,6 +227,8 @@ net_device_bluetooth_new (NMClient *client, NMDevice *device) NULL); self->client = g_object_ref (client); + g_signal_connect_object (device, "state-changed", G_CALLBACK (device_state_changed_cb), self, G_CONNECT_SWAPPED); + nm_device_bluetooth_refresh_ui (self); return self; diff --git a/panels/network/net-device-ethernet.c b/panels/network/net-device-ethernet.c index 0844e51e1..e00983337 100644 --- a/panels/network/net-device-ethernet.c +++ b/panels/network/net-device-ethernet.c @@ -405,6 +405,13 @@ client_connection_added_cb (NetDeviceEthernet *self) device_ethernet_refresh_ui (self); } +static void +device_state_changed_cb (NetDeviceEthernet *self) +{ + net_object_emit_changed (NET_OBJECT (self)); + device_ethernet_refresh_ui (self); +} + static void add_profile (NetDeviceEthernet *self) { @@ -505,20 +512,12 @@ device_ethernet_finalize (GObject *object) G_OBJECT_CLASS (net_device_ethernet_parent_class)->finalize (object); } -static void -device_ethernet_refresh (NetObject *object) -{ - NetDeviceEthernet *self = NET_DEVICE_ETHERNET (object); - device_ethernet_refresh_ui (self); -} - static void net_device_ethernet_class_init (NetDeviceEthernetClass *klass) { NetObjectClass *obj_class = NET_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass); - obj_class->refresh = device_ethernet_refresh; obj_class->get_widget = device_ethernet_get_widget; object_class->finalize = device_ethernet_finalize; } @@ -581,6 +580,8 @@ net_device_ethernet_new (NMClient *client, NMDevice *device) g_signal_connect_object (client, NM_CLIENT_CONNECTION_REMOVED, G_CALLBACK (connection_removed), self, G_CONNECT_SWAPPED); + g_signal_connect_object (device, "state-changed", G_CALLBACK (device_state_changed_cb), self, G_CONNECT_SWAPPED); + g_signal_connect (self, "notify::title", G_CALLBACK (device_title_changed), NULL); device_ethernet_refresh_ui (self); diff --git a/panels/network/net-device-mobile.c b/panels/network/net-device-mobile.c index 93ceccbf4..6a5bbc785 100644 --- a/panels/network/net-device-mobile.c +++ b/panels/network/net-device-mobile.c @@ -470,9 +470,9 @@ nm_device_mobile_refresh_ui (NetDeviceMobile *self) } static void -device_mobile_refresh (NetObject *object) +device_state_changed_cb (NetDeviceMobile *self) { - NetDeviceMobile *self = NET_DEVICE_MOBILE (object); + net_object_emit_changed (NET_OBJECT (self)); nm_device_mobile_refresh_ui (self); } @@ -779,7 +779,6 @@ net_device_mobile_class_init (NetDeviceMobileClass *klass) object_class->dispose = net_device_mobile_dispose; parent_class->get_widget = device_mobile_get_widget; - parent_class->refresh = device_mobile_refresh; } static void @@ -851,6 +850,8 @@ net_device_mobile_new (NMClient *client, NMDevice *device, GDBusObject *modem) NULL); self->client = g_object_ref (client); + g_signal_connect_object (device, "state-changed", G_CALLBACK (device_state_changed_cb), self, G_CONNECT_SWAPPED); + if (modem != NULL) { MMModem3gpp *modem_3gpp; diff --git a/panels/network/net-device-wifi.c b/panels/network/net-device-wifi.c index e4ced1204..8f253259e 100644 --- a/panels/network/net-device-wifi.c +++ b/panels/network/net-device-wifi.c @@ -406,9 +406,9 @@ nm_device_wifi_refresh_ui (NetDeviceWifi *self) } static void -device_wifi_refresh (NetObject *object) +device_state_changed_cb (NetDeviceWifi *self) { - NetDeviceWifi *self = NET_DEVICE_WIFI (object); + net_object_emit_changed (NET_OBJECT (self)); nm_device_wifi_refresh_ui (self); } @@ -903,7 +903,6 @@ net_device_wifi_class_init (NetDeviceWifiClass *klass) object_class->finalize = net_device_wifi_finalize; object_class->get_property = net_device_wifi_get_property; parent_class->get_widget = device_wifi_proxy_get_widget; - parent_class->refresh = device_wifi_refresh; g_object_class_install_property (object_class, PROP_SCANNING, @@ -1325,6 +1324,8 @@ net_device_wifi_new (CcPanel *panel, NMClient *client, NMDevice *device) g_signal_connect_object (client, "notify::wireless-enabled", G_CALLBACK (wireless_enabled_toggled), self, G_CONNECT_SWAPPED); + g_signal_connect_object (device, "state-changed", G_CALLBACK (device_state_changed_cb), self, G_CONNECT_SWAPPED); + list = GTK_WIDGET (cc_wifi_connection_list_new (client, NM_DEVICE_WIFI (device), TRUE, TRUE, FALSE)); gtk_widget_show (list); gtk_container_add (GTK_CONTAINER (self->listbox_box), list); diff --git a/panels/network/net-device.c b/panels/network/net-device.c index 98e2cd076..12b0afa02 100644 --- a/panels/network/net-device.c +++ b/panels/network/net-device.c @@ -33,7 +33,6 @@ typedef struct { NMDevice *nm_device; - guint changed_id; } NetDevicePrivate; enum { @@ -44,123 +43,6 @@ enum { G_DEFINE_TYPE_WITH_PRIVATE (NetDevice, net_device, NET_TYPE_OBJECT) -/* return value must be freed by caller with g_free() */ -static gchar * -get_mac_address_of_connection (NMConnection *connection) -{ - if (!connection) - return NULL; - - /* check the connection type */ - if (nm_connection_is_type (connection, - NM_SETTING_WIRELESS_SETTING_NAME)) { - /* check wireless settings */ - NMSettingWireless *s_wireless = nm_connection_get_setting_wireless (connection); - if (!s_wireless) - return NULL; - return g_strdup (nm_setting_wireless_get_mac_address (s_wireless)); - } else if (nm_connection_is_type (connection, - NM_SETTING_WIRED_SETTING_NAME)) { - /* check wired settings */ - NMSettingWired *s_wired = nm_connection_get_setting_wired (connection); - if (!s_wired) - return NULL; - return g_strdup (nm_setting_wired_get_mac_address (s_wired)); - } - /* no MAC address found */ - return NULL; -} - -/* return value must not be freed! */ -static const gchar * -get_mac_address_of_device (NMDevice *device) -{ - const gchar *mac = NULL; - switch (nm_device_get_device_type (device)) { - case NM_DEVICE_TYPE_WIFI: - { - NMDeviceWifi *device_wifi = NM_DEVICE_WIFI (device); - mac = nm_device_wifi_get_hw_address (device_wifi); - break; - } - case NM_DEVICE_TYPE_ETHERNET: - { - NMDeviceEthernet *device_ethernet = NM_DEVICE_ETHERNET (device); - mac = nm_device_ethernet_get_hw_address (device_ethernet); - break; - } - default: - break; - } - /* no MAC address found */ - return mac; -} - -/* returns TRUE if both MACs are equal */ -static gboolean -compare_mac_device_with_mac_connection (NMDevice *device, - NMConnection *connection) -{ - const gchar *mac_dev = NULL; - g_autofree gchar *mac_conn = NULL; - - mac_dev = get_mac_address_of_device (device); - if (mac_dev == NULL) - return FALSE; - - mac_conn = get_mac_address_of_connection (connection); - if (mac_conn == NULL) - return FALSE; - - /* compare both MACs */ - return g_strcmp0 (mac_dev, mac_conn) == 0; -} - -NMConnection * -net_device_get_find_connection (NMClient *client, NMDevice *device) -{ - GSList *list, *iterator; - NMConnection *connection = NULL; - NMActiveConnection *ac; - - /* is the device available in a active connection? */ - ac = nm_device_get_active_connection (device); - if (ac) - return (NMConnection*) nm_active_connection_get_connection (ac); - - /* not found in active connections - check all available connections */ - list = net_device_get_valid_connections (client, device); - if (list != NULL) { - /* if list has only one connection, use this connection */ - if (g_slist_length (list) == 1) { - connection = list->data; - goto out; - } - - /* is there connection with the MAC address of the device? */ - for (iterator = list; iterator; iterator = iterator->next) { - connection = iterator->data; - if (compare_mac_device_with_mac_connection (device, - connection)) { - goto out; - } - } - } - - /* no connection found for the given device */ - connection = NULL; -out: - g_slist_free (list); - return connection; -} - -static void -state_changed_cb (NetDevice *self) -{ - net_object_emit_changed (NET_OBJECT (self)); - net_object_refresh (NET_OBJECT (self)); -} - NMDevice * net_device_get_nm_device (NetDevice *self) { @@ -208,18 +90,7 @@ net_device_set_property (GObject *object, switch (prop_id) { case PROP_DEVICE: - if (priv->changed_id != 0) { - g_signal_handler_disconnect (priv->nm_device, - priv->changed_id); - } priv->nm_device = g_value_dup_object (value); - if (priv->nm_device) { - priv->changed_id = g_signal_connect_swapped (priv->nm_device, - "state-changed", - G_CALLBACK (state_changed_cb), - net_device); - } else - priv->changed_id = 0; break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -233,10 +104,6 @@ net_device_finalize (GObject *object) NetDevice *self = NET_DEVICE (object); NetDevicePrivate *priv = net_device_get_instance_private (self); - if (priv->changed_id != 0) { - g_signal_handler_disconnect (priv->nm_device, - priv->changed_id); - } g_clear_object (&priv->nm_device); G_OBJECT_CLASS (net_device_parent_class)->finalize (object); @@ -262,39 +129,3 @@ static void net_device_init (NetDevice *self) { } - -GSList * -net_device_get_valid_connections (NMClient *client, NMDevice *device) -{ - GSList *valid; - NMConnection *connection; - NMSettingConnection *s_con; - NMActiveConnection *active_connection; - const char *active_uuid; - const GPtrArray *all; - GPtrArray *filtered; - guint i; - - all = nm_client_get_connections (client); - filtered = nm_device_filter_connections (device, all); - - active_connection = nm_device_get_active_connection (device); - active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL; - - valid = NULL; - for (i = 0; i < filtered->len; i++) { - connection = g_ptr_array_index (filtered, i); - 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_ptr_array_free (filtered, FALSE); - - return g_slist_reverse (valid); -} diff --git a/panels/network/net-device.h b/panels/network/net-device.h index 6760cbdbe..1953d8bae 100644 --- a/panels/network/net-device.h +++ b/panels/network/net-device.h @@ -21,9 +21,6 @@ #pragma once -#include - -#include #include "net-object.h" G_BEGIN_DECLS @@ -39,10 +36,4 @@ struct _NetDeviceClass NMDevice *net_device_get_nm_device (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 diff --git a/panels/network/net-object.c b/panels/network/net-object.c index 00aea2d7c..c5e622ad7 100644 --- a/panels/network/net-object.c +++ b/panels/network/net-object.c @@ -89,14 +89,6 @@ net_object_get_widget (NetObject *self, return klass->get_widget (self, heading_size_group); } -void -net_object_refresh (NetObject *self) -{ - NetObjectClass *klass = NET_OBJECT_GET_CLASS (self); - if (klass->refresh != NULL) - klass->refresh (self); -} - /** * net_object_get_property: **/ diff --git a/panels/network/net-object.h b/panels/network/net-object.h index 94ec05593..56f03dab5 100644 --- a/panels/network/net-object.h +++ b/panels/network/net-object.h @@ -40,7 +40,6 @@ struct _NetObjectClass /* vtable */ GtkWidget *(*get_widget) (NetObject *object, GtkSizeGroup *heading_size_group); - void (*refresh) (NetObject *object); }; const gchar *net_object_get_title (NetObject *object); @@ -48,7 +47,6 @@ void net_object_set_title (NetObject *object, const gchar *title); void net_object_emit_changed (NetObject *object); void net_object_emit_removed (NetObject *object); -void net_object_refresh (NetObject *object); GtkWidget *net_object_get_widget (NetObject *object, GtkSizeGroup *heading_size_group); diff --git a/panels/network/panel-common.c b/panels/network/panel-common.c index 22a31d09b..5f3f75130 100644 --- a/panels/network/panel-common.c +++ b/panels/network/panel-common.c @@ -264,6 +264,76 @@ device_state_reason_to_localized_string (NMDevice *device) return value; } +static gchar * +get_mac_address_of_connection (NMConnection *connection) +{ + if (!connection) + return NULL; + + /* check the connection type */ + if (nm_connection_is_type (connection, + NM_SETTING_WIRELESS_SETTING_NAME)) { + /* check wireless settings */ + NMSettingWireless *s_wireless = nm_connection_get_setting_wireless (connection); + if (!s_wireless) + return NULL; + return g_strdup (nm_setting_wireless_get_mac_address (s_wireless)); + } else if (nm_connection_is_type (connection, + NM_SETTING_WIRED_SETTING_NAME)) { + /* check wired settings */ + NMSettingWired *s_wired = nm_connection_get_setting_wired (connection); + if (!s_wired) + return NULL; + return g_strdup (nm_setting_wired_get_mac_address (s_wired)); + } + /* no MAC address found */ + return NULL; +} + +static const gchar * +get_mac_address_of_device (NMDevice *device) +{ + const gchar *mac = NULL; + switch (nm_device_get_device_type (device)) { + case NM_DEVICE_TYPE_WIFI: + { + NMDeviceWifi *device_wifi = NM_DEVICE_WIFI (device); + mac = nm_device_wifi_get_hw_address (device_wifi); + break; + } + case NM_DEVICE_TYPE_ETHERNET: + { + NMDeviceEthernet *device_ethernet = NM_DEVICE_ETHERNET (device); + mac = nm_device_ethernet_get_hw_address (device_ethernet); + break; + } + default: + break; + } + /* no MAC address found */ + return mac; +} + +/* returns TRUE if both MACs are equal */ +static gboolean +compare_mac_device_with_mac_connection (NMDevice *device, + NMConnection *connection) +{ + const gchar *mac_dev = NULL; + g_autofree gchar *mac_conn = NULL; + + mac_dev = get_mac_address_of_device (device); + if (mac_dev == NULL) + return FALSE; + + mac_conn = get_mac_address_of_connection (connection); + if (mac_conn == NULL) + return FALSE; + + /* compare both MACs */ + return g_strcmp0 (mac_dev, mac_conn) == 0; +} + gchar * panel_device_status_to_localized_string (NMDevice *nm_device, const gchar *speed) @@ -308,3 +378,77 @@ panel_device_status_to_localized_string (NMDevice *nm_device, return g_string_free (string, FALSE); } + +NMConnection * +net_device_get_find_connection (NMClient *client, NMDevice *device) +{ + GSList *list, *iterator; + NMConnection *connection = NULL; + NMActiveConnection *ac; + + /* is the device available in a active connection? */ + ac = nm_device_get_active_connection (device); + if (ac) + return (NMConnection*) nm_active_connection_get_connection (ac); + + /* not found in active connections - check all available connections */ + list = net_device_get_valid_connections (client, device); + if (list != NULL) { + /* if list has only one connection, use this connection */ + if (g_slist_length (list) == 1) { + connection = list->data; + goto out; + } + + /* is there connection with the MAC address of the device? */ + for (iterator = list; iterator; iterator = iterator->next) { + connection = iterator->data; + if (compare_mac_device_with_mac_connection (device, + connection)) { + goto out; + } + } + } + + /* no connection found for the given device */ + connection = NULL; +out: + g_slist_free (list); + return connection; +} + +GSList * +net_device_get_valid_connections (NMClient *client, NMDevice *device) +{ + GSList *valid; + NMConnection *connection; + NMSettingConnection *s_con; + NMActiveConnection *active_connection; + const char *active_uuid; + const GPtrArray *all; + GPtrArray *filtered; + guint i; + + all = nm_client_get_connections (client); + filtered = nm_device_filter_connections (device, all); + + active_connection = nm_device_get_active_connection (device); + active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL; + + valid = NULL; + for (i = 0; i < filtered->len; i++) { + connection = g_ptr_array_index (filtered, i); + 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_ptr_array_free (filtered, FALSE); + + return g_slist_reverse (valid); +} diff --git a/panels/network/panel-common.h b/panels/network/panel-common.h index 0c0715929..07326ad93 100644 --- a/panels/network/panel-common.h +++ b/panels/network/panel-common.h @@ -28,4 +28,10 @@ G_BEGIN_DECLS gchar *panel_device_status_to_localized_string (NMDevice *nm_device, const gchar *speed); +NMConnection *net_device_get_find_connection (NMClient *client, + NMDevice *device); + +GSList *net_device_get_valid_connections (NMClient *client, + NMDevice *device); + G_END_DECLS