From fbb506227182962746c2f71ff6d2557591860969 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 19 Mar 2008 20:58:21 +0000 Subject: [PATCH] 2008-03-19 Dan Williams * introspection/nm-manager.xml introspection/nm-manager-client.xml - Rename the ActivateDevice method to ActivateConnection to better reflect it's usage; it's arguments get reordered a bit too - Convert GetActiveConnections method return from a struct to a dict * include/NetworkManager.h - Define the dict keys for return value of GetActiveConnections * src/nm-manager.c - impl_manager_activate_device -> impl_manager_activate_connection - (add_one_connection_element): return a populated hash table, not a structure * libnm-glib/nm-client.c libnm-glib/nm-client.h - nm_client_activate_device -> nm_client_activate_connection - nm_client_free_active_connection_element -> nm_client_free_active_connections_element - (nm_client_get_active_connections): return a GSList of GHashTables, instead of the custom structures. Each element of the returned list must be freed with nm_client_free_active_connections_element() git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3482 4912f4e0-d625-0410-9fb7-b9a5a253dbdc --- ChangeLog | 24 +++++ include/NetworkManager.h | 12 +++ introspection/nm-manager-client.xml | 15 +-- introspection/nm-manager.xml | 53 ++------- libnm-glib/nm-client.c | 160 +++++++++++++++++----------- libnm-glib/nm-client.h | 14 +-- src/nm-manager.c | 101 ++++++++++++------ 7 files changed, 216 insertions(+), 163 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41e65f980a..32847b9487 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2008-03-19 Dan Williams + + * introspection/nm-manager.xml + introspection/nm-manager-client.xml + - Rename the ActivateDevice method to ActivateConnection to better + reflect it's usage; it's arguments get reordered a bit too + - Convert GetActiveConnections method return from a struct to a dict + + * include/NetworkManager.h + - Define the dict keys for return value of GetActiveConnections + + * src/nm-manager.c + - impl_manager_activate_device -> impl_manager_activate_connection + - (add_one_connection_element): return a populated hash table, not + a structure + + * libnm-glib/nm-client.c + libnm-glib/nm-client.h + - nm_client_activate_device -> nm_client_activate_connection + - nm_client_free_active_connection_element -> nm_client_free_active_connections_element + - (nm_client_get_active_connections): return a GSList of GHashTables, + instead of the custom structures. Each element of the returned list + must be freed with nm_client_free_active_connections_element() + 2008-03-18 Dan Williams * system-settings/plugins/ifcfg-fedora/parser.c diff --git a/include/NetworkManager.h b/include/NetworkManager.h index 9946a4cc3f..f4a57330cc 100644 --- a/include/NetworkManager.h +++ b/include/NetworkManager.h @@ -139,4 +139,16 @@ typedef enum } NMDeviceState; +/* + * Active Connection dict keys + */ +#define NM_AC_KEY_SERVICE_NAME "service-name" +#define NM_AC_KEY_CONNECTION "connection" +#define NM_AC_KEY_SPECIFIC_OBJECT "specific-object" +#define NM_AC_KEY_SHARED_TO_SERVICE_NAME "shared-to-service-name" +#define NM_AC_KEY_SHARED_TO_CONNECTION "shared-to-connection" +#define NM_AC_KEY_DEVICES "devices" + + #endif /* NETWORK_MANAGER_H */ + diff --git a/introspection/nm-manager-client.xml b/introspection/nm-manager-client.xml index aeec313025..d256a78076 100644 --- a/introspection/nm-manager-client.xml +++ b/introspection/nm-manager-client.xml @@ -19,25 +19,18 @@ object. dbus-glib generates the same bound function names for D-Bus the methods - - + + - + - - + diff --git a/introspection/nm-manager.xml b/introspection/nm-manager.xml index d6f11be7ec..ab3d7046ef 100644 --- a/introspection/nm-manager.xml +++ b/introspection/nm-manager.xml @@ -21,17 +21,12 @@ - - + + - Activate a device using the supplied connection. + Activate a connection using the supplied device. - - - The device to be activated. - - The D-Bus service name of the settings service that provides this connection. @@ -42,6 +37,11 @@ The connection to activate the devices with. + + + The device to be activated. + + The path of a device-type-specific object this activation should use, for example a WiFi access point. @@ -63,16 +63,9 @@ - - + - List of active connections + List of active connections, each described by a property dictionary. @@ -182,32 +175,6 @@ - - - A struct representing an active connection, returned by GetActiveConnections on the NetworkManager interface. - - - - The D-Bus service name of the settings service that provides this connection. - - - - - Object path of the active connection - - - - - Object path of a device-type-specific object this connection uses, for example a specific WiFi access point. - - - - - Array of object paths to the devices this connection is active on. - - - - diff --git a/libnm-glib/nm-client.c b/libnm-glib/nm-client.c index cfb47d9b63..933306bc0f 100644 --- a/libnm-glib/nm-client.c +++ b/libnm-glib/nm-client.c @@ -531,10 +531,10 @@ activate_cb (DBusGProxy *proxy, GError *err, gpointer user_data) } void -nm_client_activate_device (NMClient *client, - NMDevice *device, +nm_client_activate_connection (NMClient *client, const char *service_name, const char *connection_path, + NMDevice *device, const char *specific_object, NMClientActivateDeviceFn callback, gpointer user_data) @@ -557,15 +557,33 @@ nm_client_activate_device (NMClient *client, info->fn = callback; info->user_data = user_data; - org_freedesktop_NetworkManager_activate_device_async (NM_CLIENT_GET_PRIVATE (client)->client_proxy, - nm_object_get_path (NM_OBJECT (device)), + org_freedesktop_NetworkManager_activate_connection_async (NM_CLIENT_GET_PRIVATE (client)->client_proxy, service_name, connection_path, + nm_object_get_path (NM_OBJECT (device)), internal_so, activate_cb, info); } +void +nm_client_free_active_connections_element (GHashTable *item) +{ + GSList *devices, *iter; + + g_free (g_hash_table_lookup (item, NM_AC_KEY_SERVICE_NAME)); + g_free (g_hash_table_lookup (item, NM_AC_KEY_CONNECTION)); + g_free (g_hash_table_lookup (item, NM_AC_KEY_SPECIFIC_OBJECT)); + g_free (g_hash_table_lookup (item, NM_AC_KEY_SHARED_TO_SERVICE_NAME)); + g_free (g_hash_table_lookup (item, NM_AC_KEY_SHARED_TO_CONNECTION)); + + devices = g_hash_table_lookup (item, NM_AC_KEY_DEVICES); + for (iter = devices; iter; iter = g_slist_next (iter)) + g_object_unref (iter->data); + g_slist_free (devices); +} + +#define DBUS_TYPE_G_OBJECT_PATH_ARRAY (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH)) GSList * nm_client_get_active_connections (NMClient *client) @@ -575,7 +593,6 @@ nm_client_get_active_connections (NMClient *client) GPtrArray *array = NULL; GError *err = NULL; int i, j; - static GType type = 0, ao_type = 0; g_return_val_if_fail (NM_IS_CLIENT (client), NULL); @@ -586,83 +603,98 @@ nm_client_get_active_connections (NMClient *client) return NULL; } - /* dbus signature "sooao" */ - if (G_UNLIKELY (ao_type) == 0) - ao_type = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH); - if (G_UNLIKELY (type) == 0) { - type = dbus_g_type_get_struct ("GValueArray", - G_TYPE_STRING, - DBUS_TYPE_G_OBJECT_PATH, - DBUS_TYPE_G_OBJECT_PATH, - ao_type, - G_TYPE_INVALID); - } - for (i = 0; i < array->len; i++) { - NMClientActiveConnection *ac_elt; - GValue val = {0, }; - GPtrArray *devices = NULL; + GHashTable *reply; + GHashTable *active; + GValue *value, *value2; + GPtrArray *devices_array = NULL; + GSList *devices = NULL; + gboolean have_shared_service = TRUE; - ac_elt = g_slice_new0 (NMClientActiveConnection); - if (!ac_elt) { - g_warning ("Error in get_active_connections: not enough memory."); + active = g_hash_table_new (g_str_hash, g_str_equal); + + reply = g_ptr_array_index (array, i); + + /* Service name */ + value = g_hash_table_lookup (reply, NM_AC_KEY_SERVICE_NAME); + if (!value || !G_VALUE_HOLDS_STRING (value)) { + g_warning ("%s: missing item " NM_AC_KEY_SERVICE_NAME, __func__); + nm_client_free_active_connections_element (active); + g_hash_table_destroy (reply); + continue; + } + g_hash_table_insert (active, NM_AC_KEY_SERVICE_NAME, g_value_dup_string (value)); + + /* Connection path */ + value = g_hash_table_lookup (reply, NM_AC_KEY_CONNECTION); + if (!value || !G_VALUE_HOLDS (value, DBUS_TYPE_G_OBJECT_PATH)) { + g_warning ("%s: missing item " NM_AC_KEY_CONNECTION, __func__); + nm_client_free_active_connections_element (active); + g_hash_table_destroy (reply); + continue; + } + g_hash_table_insert (active, NM_AC_KEY_CONNECTION, g_value_dup_boxed (value)); + + /* Specific object path */ + value = g_hash_table_lookup (reply, NM_AC_KEY_SPECIFIC_OBJECT); + if (value && G_VALUE_HOLDS (value, DBUS_TYPE_G_OBJECT_PATH)) + g_hash_table_insert (active, NM_AC_KEY_SPECIFIC_OBJECT, g_value_dup_boxed (value)); + + /* Shared to service name */ + value = g_hash_table_lookup (reply, NM_AC_KEY_SHARED_TO_SERVICE_NAME); + if (!value || !G_VALUE_HOLDS_STRING (value)) + have_shared_service = FALSE; + + value2 = g_hash_table_lookup (reply, NM_AC_KEY_SHARED_TO_CONNECTION); + if (have_shared_service && value2 && G_VALUE_HOLDS (value2, DBUS_TYPE_G_OBJECT_PATH)) { + g_hash_table_insert (active, NM_AC_KEY_SHARED_TO_SERVICE_NAME, g_value_dup_string (value)); + g_hash_table_insert (active, NM_AC_KEY_SHARED_TO_CONNECTION, g_value_dup_boxed (value2)); + } else { + /* Ignore missing shared-to-service _and_ missing shared-to-connection */ + if (have_shared_service) { + g_warning ("%s: missing item " NM_AC_KEY_SHARED_TO_SERVICE_NAME, __func__); + nm_client_free_active_connections_element (active); + g_hash_table_destroy (reply); + continue; + } + } + + /* Device array */ + value = g_hash_table_lookup (reply, NM_AC_KEY_DEVICES); + if (!value || !G_VALUE_HOLDS (value, DBUS_TYPE_G_OBJECT_PATH_ARRAY)) { + g_warning ("%s: missing item " NM_AC_KEY_DEVICES, __func__); + nm_client_free_active_connections_element (active); + g_hash_table_destroy (reply); continue; } - g_value_init (&val, type); - g_value_take_boxed (&val, g_ptr_array_index (array, i)); - dbus_g_type_struct_get (&val, - 0, &(ac_elt->service_name), - 1, &(ac_elt->connection_path), - 2, &(ac_elt->specific_object), - 3, &devices, - G_MAXUINT); - g_value_unset (&val); - - if (devices == NULL || (devices->len == 0)) { - g_warning ("Error in get_active_connections: no devices returned."); - nm_client_free_active_connection_element (ac_elt); + devices_array = g_value_get_boxed (value); + if (!devices_array || (devices_array->len == 0)) { + g_warning ("%s: no devices for this active connection.", __func__); + nm_client_free_active_connections_element (active); + g_hash_table_destroy (reply); continue; } - if (!strcmp (ac_elt->specific_object, "/")) { - g_free (ac_elt->specific_object); - ac_elt->specific_object = NULL; - } - - for (j = 0; j < devices->len; j++) { + for (j = 0; j < devices_array->len; j++) { NMDevice *device; - char *path = g_ptr_array_index (devices, j); + const char *path; - device = get_device (client, (const char *) path, TRUE); - ac_elt->devices = g_slist_append (ac_elt->devices, g_object_ref (device)); - g_free (path); + path = (const char *) g_ptr_array_index (devices_array, j); + device = get_device (client, path, TRUE); + devices = g_slist_append (devices, g_object_ref (device)); } - g_ptr_array_free (devices, TRUE); - connections = g_slist_append (connections, ac_elt); + g_hash_table_insert (active, NM_AC_KEY_DEVICES, devices); + + connections = g_slist_append (connections, active); + g_hash_table_destroy (reply); } g_ptr_array_free (array, TRUE); return connections; } -void -nm_client_free_active_connection_element (NMClientActiveConnection *elt) -{ - g_return_if_fail (elt != NULL); - - g_free (elt->service_name); - g_free (elt->connection_path); - g_free (elt->specific_object); - - g_slist_foreach (elt->devices, (GFunc) g_object_unref, NULL); - g_slist_free (elt->devices); - - g_slice_free (NMClientActiveConnection, elt); -} - - gboolean nm_client_wireless_get_enabled (NMClient *client) { diff --git a/libnm-glib/nm-client.h b/libnm-glib/nm-client.h index 5fc99cf899..6c25abff50 100644 --- a/libnm-glib/nm-client.h +++ b/libnm-glib/nm-client.h @@ -45,23 +45,15 @@ gboolean nm_client_manager_is_running (NMClient *client); GSList *nm_client_get_devices (NMClient *client); NMDevice *nm_client_get_device_by_path (NMClient *client, const char *object_path); - -typedef struct NMClientActiveConnection { - char *service_name; - char *connection_path; - char *specific_object; - GSList *devices; /* list of NMDevice objects */ -} NMClientActiveConnection; - GSList * nm_client_get_active_connections (NMClient *client); -void nm_client_free_active_connection_element (NMClientActiveConnection *elt); +void nm_client_free_active_connections_element (GHashTable *item); typedef void (*NMClientActivateDeviceFn) (gpointer user_data, GError *error); -void nm_client_activate_device (NMClient *client, - NMDevice *device, +void nm_client_activate_connection (NMClient *client, const char *service_name, const char *connection_path, + NMDevice *device, const char *specific_object, NMClientActivateDeviceFn callback, gpointer user_data); diff --git a/src/nm-manager.c b/src/nm-manager.c index 422fbbc720..c45311dfa2 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -15,10 +15,10 @@ #include "nm-marshal.h" static gboolean impl_manager_get_devices (NMManager *manager, GPtrArray **devices, GError **err); -static void impl_manager_activate_device (NMManager *manager, - char *device_path, +static void impl_manager_activate_connection (NMManager *manager, char *service_name, char *connection_path, + char *device_path, char *specific_object_path, DBusGMethodInvocation *context); @@ -1368,10 +1368,10 @@ connection_added_default_handler (NMManager *manager, } static void -impl_manager_activate_device (NMManager *manager, - char *device_path, +impl_manager_activate_connection (NMManager *manager, char *service_name, char *connection_path, + char *device_path, char *specific_object_path, DBusGMethodInvocation *context) { @@ -1456,22 +1456,31 @@ impl_manager_activate_device (NMManager *manager, g_free (real_sop); } -static GValueArray * +static void +destroy_gvalue (gpointer data) +{ + GValue *value = (GValue *) data; + + g_value_unset (value); + g_slice_free (GValue, value); +} + +static GHashTable * add_one_connection_element (NMManager *manager, NMDevice *device) { - static GType type = 0, ao_type = 0; - GValue entry = {0, }; - GPtrArray *dev_array = NULL; + GHashTable *properties; NMActRequest *req; const char *service_name = NULL; NMConnection *connection; const char *specific_object; + GPtrArray *dev_array = NULL; + GValue *value; req = nm_device_get_act_request (device); g_assert (req); - connection = nm_act_request_get_connection (req); + g_assert (connection); switch (nm_connection_get_scope (connection)) { case NM_CONNECTION_SCOPE_USER: @@ -1487,32 +1496,56 @@ add_one_connection_element (NMManager *manager, specific_object = nm_act_request_get_specific_object (req); - /* dbus signature "sooao" */ - if (G_UNLIKELY (ao_type) == 0) - ao_type = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH); - if (G_UNLIKELY (type) == 0) { - type = dbus_g_type_get_struct ("GValueArray", - G_TYPE_STRING, - DBUS_TYPE_G_OBJECT_PATH, - DBUS_TYPE_G_OBJECT_PATH, - ao_type, - G_TYPE_INVALID); + properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_gvalue); + + /* Service name */ + value = g_slice_new0 (GValue); + g_value_init (value, G_TYPE_STRING); + g_value_set_string (value, service_name); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_SERVICE_NAME), value); + + /* Connection path */ + value = g_slice_new0 (GValue); + g_value_init (value, DBUS_TYPE_G_OBJECT_PATH); + g_value_set_boxed (value, nm_connection_get_path (connection)); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_CONNECTION), value); + + /* Specific object */ + if (specific_object) { + value = g_slice_new0 (GValue); + g_value_init (value, DBUS_TYPE_G_OBJECT_PATH); + g_value_set_boxed (value, specific_object); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_SPECIFIC_OBJECT), value); } - dev_array = g_ptr_array_sized_new (1); - if (!dev_array) - return NULL; - g_ptr_array_add (dev_array, g_strdup (nm_device_get_udi (device))); + if (FALSE /* SHARED */ ) { + /* Shared connection service name */ + value = g_slice_new0 (GValue); + g_value_init (value, G_TYPE_STRING); + g_value_set_string (value, service_name); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_SHARED_TO_SERVICE_NAME), value); - g_value_init (&entry, type); - g_value_take_boxed (&entry, dbus_g_type_specialized_construct (type)); - dbus_g_type_struct_set (&entry, - 0, service_name, - 1, nm_connection_get_path (connection), - 2, specific_object ? specific_object : "/", - 3, dev_array, - G_MAXUINT); - return g_value_get_boxed (&entry); + /* Shared connection connection path */ + value = g_slice_new0 (GValue); + g_value_init (value, DBUS_TYPE_G_OBJECT_PATH); + g_value_set_boxed (value, nm_connection_get_path (connection)); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_SHARED_TO_CONNECTION), value); + } + + /* Device list */ + dev_array = g_ptr_array_sized_new (1); + if (!dev_array) { + g_hash_table_destroy (properties); + return NULL; + } + g_ptr_array_add (dev_array, g_object_ref (device)); + + value = g_slice_new0 (GValue); + g_value_init (value, DBUS_TYPE_G_OBJECT_ARRAY); + g_value_take_boxed (value, dev_array); + g_hash_table_insert (properties, g_strdup (NM_AC_KEY_DEVICES), value); + + return properties; } static gboolean @@ -1527,13 +1560,13 @@ impl_manager_get_active_connections (NMManager *manager, priv = NM_MANAGER_GET_PRIVATE (manager); - // GPtrArray of GValueArrays of (gchar * and GPtrArray of gchar *) + /* GPtrArray of GHashTables */ *connections = g_ptr_array_sized_new (1); // FIXME: this assumes one active device per connection for (iter = priv->devices; iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); - GValueArray *item; + GHashTable *item; if ( (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED) && !nm_device_is_activating (dev))