diff options
author | Dan Williams <dcbw@redhat.com> | 2014-10-10 14:28:49 -0500 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-12-04 12:16:41 +0100 |
commit | 4db851f852b73a85b9b4a5d617e66461836be210 (patch) | |
tree | 7d11aa0549e986daa4119d6c2829838ddf33b6e3 /libnm | |
parent | e84587183bfa86d92f24deffeba23a4e928cbee7 (diff) | |
download | NetworkManager-4db851f852b73a85b9b4a5d617e66461836be210.tar.gz |
libnm/libnm-glib: add NMClient.get_all_devices() method and AllDevices property
Mirror new NetworkManager API to return both real devices and
device placeholders.
Diffstat (limited to 'libnm')
-rw-r--r-- | libnm/libnm.ver | 2 | ||||
-rw-r--r-- | libnm/nm-client.c | 110 | ||||
-rw-r--r-- | libnm/nm-client.h | 9 | ||||
-rw-r--r-- | libnm/nm-manager.c | 92 | ||||
-rw-r--r-- | libnm/nm-manager.h | 5 |
5 files changed, 198 insertions, 20 deletions
diff --git a/libnm/libnm.ver b/libnm/libnm.ver index ae287695bc..e092a5845a 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -861,6 +861,7 @@ global: nm_connection_get_setting_ip_tunnel; nm_connection_verify_secrets; nm_device_ethernet_get_s390_subchannels; + nm_client_get_all_devices; nm_device_get_lldp_neighbors; nm_device_get_metered; nm_device_ip_tunnel_get_encapsulation_limit; @@ -897,6 +898,7 @@ global: nm_lldp_neighbor_new; nm_lldp_neighbor_ref; nm_lldp_neighbor_unref; + nm_manager_get_all_devices; nm_metered_get_type; nm_setting_802_1x_check_cert_scheme; nm_setting_bridge_get_multicast_snooping; diff --git a/libnm/nm-client.c b/libnm/nm-client.c index e17042eb44..dc6db0aad9 100644 --- a/libnm/nm-client.c +++ b/libnm/nm-client.c @@ -73,6 +73,7 @@ enum { PROP_PRIMARY_CONNECTION, PROP_ACTIVATING_CONNECTION, PROP_DEVICES, + PROP_ALL_DEVICES, PROP_CONNECTIONS, PROP_HOSTNAME, PROP_CAN_MODIFY, @@ -84,6 +85,8 @@ enum { enum { DEVICE_ADDED, DEVICE_REMOVED, + ANY_DEVICE_ADDED, + ANY_DEVICE_REMOVED, PERMISSION_CHANGED, CONNECTION_ADDED, CONNECTION_REMOVED, @@ -720,6 +723,34 @@ nm_client_get_devices (NMClient *client) } /** + * nm_client_get_all_devices: + * @client: a #NMClient + * + * Gets both real devices and device placeholders (eg, software devices which + * do not currently exist, but could be created automatically by NetworkManager + * if one of their NMDevice::ActivatableConnections was activated). Use + * nm_device_is_real() to determine whether each device is a real device or + * a placeholder. + * + * Use nm_device_get_type() or the NM_IS_DEVICE_XXXX() functions to determine + * what kind of device each member of the returned array is, and then you may + * use device-specific methods such as nm_device_ethernet_get_hw_address(). + * + * Returns: (transfer none) (element-type NMDevice): a #GPtrArray + * containing all the #NMDevices. The returned array is owned by the + * #NMClient object and should not be modified. + * + * Since: 1.2 + **/ +const GPtrArray * +nm_client_get_all_devices (NMClient *client) +{ + g_return_val_if_fail (NM_IS_CLIENT (client), NULL); + + return nm_manager_get_all_devices (NM_CLIENT_GET_PRIVATE (client)->manager); +} + +/** * nm_client_get_device_by_path: * @client: a #NMClient * @object_path: the object path to search for @@ -1660,6 +1691,7 @@ manager_device_added (NMManager *manager, { g_signal_emit (client, signals[DEVICE_ADDED], 0, device); } + static void manager_device_removed (NMManager *manager, NMDevice *device, @@ -1669,6 +1701,22 @@ manager_device_removed (NMManager *manager, } static void +manager_any_device_added (NMManager *manager, + NMDevice *device, + gpointer client) +{ + g_signal_emit (client, signals[ANY_DEVICE_ADDED], 0, device); +} + +static void +manager_any_device_removed (NMManager *manager, + NMDevice *device, + gpointer client) +{ + g_signal_emit (client, signals[ANY_DEVICE_REMOVED], 0, device); +} + +static void manager_permission_changed (NMManager *manager, NMClientPermission permission, NMClientPermissionResult result, @@ -1707,6 +1755,10 @@ constructed (GObject *object) G_CALLBACK (manager_device_added), client); g_signal_connect (priv->manager, "device-removed", G_CALLBACK (manager_device_removed), client); + g_signal_connect (priv->manager, "any-device-added", + G_CALLBACK (manager_any_device_added), client); + g_signal_connect (priv->manager, "any-device-removed", + G_CALLBACK (manager_any_device_removed), client); g_signal_connect (priv->manager, "permission-changed", G_CALLBACK (manager_permission_changed), client); @@ -1873,6 +1925,7 @@ get_property (GObject *object, guint prop_id, case PROP_ACTIVATING_CONNECTION: case PROP_DEVICES: case PROP_METERED: + case PROP_ALL_DEVICES: g_object_get_property (G_OBJECT (NM_CLIENT_GET_PRIVATE (object)->manager), pspec->name, value); break; @@ -2092,7 +2145,7 @@ nm_client_class_init (NMClientClass *client_class) /** * NMClient:devices: * - * List of known network devices. + * List of real network devices. Does not include placeholder devices. * * Element-type: NMDevice **/ @@ -2104,6 +2157,21 @@ nm_client_class_init (NMClientClass *client_class) G_PARAM_STATIC_STRINGS)); /** + * NMClient:all-devices: + * + * List of both real devices and device placeholders. + * + * Element-type: NMDevice + * Since: 1.2 + **/ + g_object_class_install_property + (object_class, PROP_ALL_DEVICES, + g_param_spec_boxed (NM_CLIENT_ALL_DEVICES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** * NMClient:connections: * * The list of configured connections that are available to the user. (Note @@ -2166,7 +2234,8 @@ nm_client_class_init (NMClientClass *client_class) * @client: the client that received the signal * @device: (type NMDevice): the new device * - * Notifies that a #NMDevice is added. + * Notifies that a #NMDevice is added. This signal is not emitted for + * placeholder devices. **/ signals[DEVICE_ADDED] = g_signal_new (NM_CLIENT_DEVICE_ADDED, @@ -2182,7 +2251,8 @@ nm_client_class_init (NMClientClass *client_class) * @client: the client that received the signal * @device: (type NMDevice): the removed device * - * Notifies that a #NMDevice is removed. + * Notifies that a #NMDevice is removed. This signal is not emitted for + * placeholder devices. **/ signals[DEVICE_REMOVED] = g_signal_new (NM_CLIENT_DEVICE_REMOVED, @@ -2194,6 +2264,40 @@ nm_client_class_init (NMClientClass *client_class) G_TYPE_OBJECT); /** + * NMClient::any-device-added: + * @client: the client that received the signal + * @device: (type NMDevice): the new device + * + * Notifies that a #NMDevice is added. This signal is emitted for both + * regular devices and placeholder devices. + **/ + signals[ANY_DEVICE_ADDED] = + g_signal_new (NM_CLIENT_ANY_DEVICE_ADDED, + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMClientClass, any_device_added), + NULL, NULL, NULL, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + + /** + * NMClient::any-device-removed: + * @client: the client that received the signal + * @device: (type NMDevice): the removed device + * + * Notifies that a #NMDevice is removed. This signal is emitted for both + * regular devices and placeholder devices. + **/ + signals[ANY_DEVICE_REMOVED] = + g_signal_new (NM_CLIENT_ANY_DEVICE_REMOVED, + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMClientClass, any_device_removed), + NULL, NULL, NULL, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + + /** * NMClient::permission-changed: * @client: the client that received the signal * @permission: a permission from #NMClientPermission diff --git a/libnm/nm-client.h b/libnm/nm-client.h index fc309fa1a0..54d6deeeeb 100644 --- a/libnm/nm-client.h +++ b/libnm/nm-client.h @@ -53,6 +53,7 @@ G_BEGIN_DECLS #define NM_CLIENT_PRIMARY_CONNECTION "primary-connection" #define NM_CLIENT_ACTIVATING_CONNECTION "activating-connection" #define NM_CLIENT_DEVICES "devices" +#define NM_CLIENT_ALL_DEVICES "all-devices" #define NM_CLIENT_CONNECTIONS "connections" #define NM_CLIENT_HOSTNAME "hostname" #define NM_CLIENT_CAN_MODIFY "can-modify" @@ -60,6 +61,8 @@ G_BEGIN_DECLS #define NM_CLIENT_DEVICE_ADDED "device-added" #define NM_CLIENT_DEVICE_REMOVED "device-removed" +#define NM_CLIENT_ANY_DEVICE_ADDED "any-device-added" +#define NM_CLIENT_ANY_DEVICE_REMOVED "any-device-removed" #define NM_CLIENT_PERMISSION_CHANGED "permission-changed" #define NM_CLIENT_CONNECTION_ADDED "connection-added" #define NM_CLIENT_CONNECTION_REMOVED "connection-removed" @@ -164,6 +167,8 @@ typedef struct { /* Signals */ void (*device_added) (NMClient *client, NMDevice *device); void (*device_removed) (NMClient *client, NMDevice *device); + void (*any_device_added) (NMClient *client, NMDevice *device); + void (*any_device_removed) (NMClient *client, NMDevice *device); void (*permission_changed) (NMClient *client, NMClientPermission permission, NMClientPermissionResult result); @@ -171,7 +176,7 @@ typedef struct { void (*connection_removed) (NMClient *client, NMRemoteConnection *connection); /*< private >*/ - gpointer padding[8]; + gpointer padding[6]; } NMClientClass; GType nm_client_get_type (void); @@ -248,6 +253,8 @@ gboolean nm_client_save_hostname_finish (NMClient *client, /* Devices */ const GPtrArray *nm_client_get_devices (NMClient *client); +NM_AVAILABLE_IN_1_2 +const GPtrArray *nm_client_get_all_devices(NMClient *client); NMDevice *nm_client_get_device_by_path (NMClient *client, const char *object_path); NMDevice *nm_client_get_device_by_iface (NMClient *client, const char *iface); diff --git a/libnm/nm-manager.c b/libnm/nm-manager.c index 7ed50e3ced..b4b7e0c7f2 100644 --- a/libnm/nm-manager.c +++ b/libnm/nm-manager.c @@ -60,6 +60,7 @@ typedef struct { NMState state; gboolean startup; GPtrArray *devices; + GPtrArray *all_devices; GPtrArray *active_connections; NMConnectivityState connectivity; NMActiveConnection *primary_connection; @@ -104,6 +105,7 @@ enum { PROP_ACTIVATING_CONNECTION, PROP_DEVICES, PROP_METERED, + PROP_ALL_DEVICES, LAST_PROP }; @@ -111,6 +113,8 @@ enum { enum { DEVICE_ADDED, DEVICE_REMOVED, + ANY_DEVICE_ADDED, + ANY_DEVICE_REMOVED, ACTIVE_CONNECTION_ADDED, ACTIVE_CONNECTION_REMOVED, PERMISSION_CHANGED, @@ -136,6 +140,7 @@ nm_manager_init (NMManager *manager) priv->permissions = g_hash_table_new (g_direct_hash, g_direct_equal); priv->devices = g_ptr_array_new (); + priv->all_devices = g_ptr_array_new (); priv->active_connections = g_ptr_array_new (); } @@ -145,8 +150,8 @@ poke_wireless_devices_with_rf_status (NMManager *manager) NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager); int i; - for (i = 0; i < priv->devices->len; i++) { - NMDevice *device = g_ptr_array_index (priv->devices, i); + for (i = 0; i < priv->all_devices->len; i++) { + NMDevice *device = g_ptr_array_index (priv->all_devices, i); if (NM_IS_DEVICE_WIFI (device)) _nm_device_wifi_set_wireless_enabled (NM_DEVICE_WIFI (device), priv->wireless_enabled); @@ -182,6 +187,7 @@ init_dbus (NMObject *object) { NM_MANAGER_ACTIVATING_CONNECTION, &priv->activating_connection, NULL, NM_TYPE_ACTIVE_CONNECTION }, { NM_MANAGER_DEVICES, &priv->devices, NULL, NM_TYPE_DEVICE, "device" }, { NM_MANAGER_METERED, &priv->metered }, + { NM_MANAGER_ALL_DEVICES, &priv->all_devices, NULL, NM_TYPE_DEVICE, "any-device" }, { NULL }, }; @@ -667,6 +673,14 @@ nm_manager_get_devices (NMManager *manager) return NM_MANAGER_GET_PRIVATE (manager)->devices; } +const GPtrArray * +nm_manager_get_all_devices (NMManager *manager) +{ + g_return_val_if_fail (NM_IS_MANAGER (manager), NULL); + + return NM_MANAGER_GET_PRIVATE (manager)->all_devices; +} + NMDevice * nm_manager_get_device_by_path (NMManager *manager, const char *object_path) { @@ -1180,27 +1194,47 @@ static void free_devices (NMManager *manager, gboolean in_dispose) { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager); - GPtrArray *devices; - NMDevice *device; - int i; + gs_unref_ptrarray GPtrArray *real_devices = NULL; + gs_unref_ptrarray GPtrArray *all_devices = NULL; + GPtrArray *devices = NULL; + guint i, j; - if (!priv->devices) + real_devices = priv->devices; + all_devices = priv->all_devices; + + if (in_dispose) { + priv->devices = NULL; + priv->all_devices = NULL; return; + } - devices = priv->devices; + priv->devices = g_ptr_array_new_with_free_func (g_object_unref); + priv->all_devices = g_ptr_array_new_with_free_func (g_object_unref); - if (in_dispose) - priv->devices = NULL; - else { - priv->devices = g_ptr_array_new (); + if (all_devices && all_devices->len > 0) + devices = all_devices; + else if (devices && devices->len > 0) + devices = real_devices; + + if (real_devices && devices != real_devices) { + for (i = 0; i < real_devices->len; i++) { + NMDevice *d = real_devices->pdata[i]; - for (i = 0; i < devices->len; i++) { - device = devices->pdata[i]; - g_signal_emit (manager, signals[DEVICE_REMOVED], 0, device); + if (all_devices) { + for (j = 0; j < all_devices->len; j++) { + if (d == all_devices->pdata[j]) + goto next; + } + } + g_signal_emit (manager, signals[DEVICE_REMOVED], 0, d); +next: + ; } } - - g_ptr_array_unref (devices); + if (devices) { + for (i = 0; i < devices->len; i++) + g_signal_emit (manager, signals[DEVICE_REMOVED], 0, devices->pdata[i]); + } } static void @@ -1543,6 +1577,9 @@ get_property (GObject *object, case PROP_METERED: g_value_set_uint (value, priv->metered); break; + case PROP_ALL_DEVICES: + g_value_take_boxed (value, _nm_utils_copy_object_array (nm_manager_get_all_devices (self))); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -1689,6 +1726,13 @@ nm_manager_class_init (NMManagerClass *manager_class) G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property + (object_class, PROP_ALL_DEVICES, + g_param_spec_boxed (NM_MANAGER_ALL_DEVICES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + /* signals */ signals[DEVICE_ADDED] = @@ -1707,6 +1751,22 @@ nm_manager_class_init (NMManagerClass *manager_class) NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT); + signals[ANY_DEVICE_ADDED] = + g_signal_new ("any-device-added", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, NULL, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + signals[ANY_DEVICE_REMOVED] = + g_signal_new ("any-device-removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, NULL, NULL, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); signals[ACTIVE_CONNECTION_ADDED] = g_signal_new ("active-connection-added", G_OBJECT_CLASS_TYPE (object_class), diff --git a/libnm/nm-manager.h b/libnm/nm-manager.h index 8d04a060f6..f9e4cd0e28 100644 --- a/libnm/nm-manager.h +++ b/libnm/nm-manager.h @@ -51,6 +51,7 @@ G_BEGIN_DECLS #define NM_MANAGER_ACTIVATING_CONNECTION "activating-connection" #define NM_MANAGER_DEVICES "devices" #define NM_MANAGER_METERED "metered" +#define NM_MANAGER_ALL_DEVICES "all-devices" typedef struct { NMObject parent; @@ -67,6 +68,8 @@ typedef struct { void (*permission_changed) (NMManager *manager, NMClientPermission permission, NMClientPermissionResult result); + + /* Beware: no more slots. Cannot extend struct without breaking ABI. */ } NMManagerClass; GType nm_manager_get_type (void); @@ -121,6 +124,8 @@ NMConnectivityState nm_manager_check_connectivity_finish (NMManager *manager, /* Devices */ const GPtrArray *nm_manager_get_devices (NMManager *manager); +NM_AVAILABLE_IN_1_2 +const GPtrArray *nm_manager_get_all_devices(NMManager *manager); NMDevice *nm_manager_get_device_by_path (NMManager *manager, const char *object_path); NMDevice *nm_manager_get_device_by_iface (NMManager *manager, const char *iface); |