network: Move helper functions into panel-common.c

This commit is contained in:
Robert Ancell 2019-10-24 12:13:40 +13:00
parent 1a0b9b628a
commit 0197654177
10 changed files with 171 additions and 205 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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);
}

View file

@ -21,9 +21,6 @@
#pragma once
#include <glib-object.h>
#include <NetworkManager.h>
#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

View file

@ -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:
**/

View file

@ -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);

View file

@ -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);
}

View file

@ -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