From 4cae0bb0faab5f9a6cae6306d63db172f085760e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 22 Apr 2011 11:55:52 -0500 Subject: [PATCH] settings: add a GetConnectionByUuid method If the client knows the UUID, add a convenience function to get the connection path directly, instead of having to iterate the whole connection list and get each connection's details and then check the UUID. --- introspection/nm-settings.xml | 17 +++++++++++++++ libnm-glib/libnm-glib.ver | 1 + libnm-glib/nm-remote-settings.c | 29 ++++++++++++++++++++++++++ libnm-glib/nm-remote-settings.h | 5 ++++- src/settings/nm-settings.c | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) diff --git a/introspection/nm-settings.xml b/introspection/nm-settings.xml index f816b08706..64163534a5 100644 --- a/introspection/nm-settings.xml +++ b/introspection/nm-settings.xml @@ -18,6 +18,23 @@ + + + Retrieve the object path of a connection, given that connection's UUID. + + + + + The UUID to find the connection object path for. + + + + + The connection's object path. + + + + Add new connection. diff --git a/libnm-glib/libnm-glib.ver b/libnm-glib/libnm-glib.ver index ed4ba4f32e..042e5a1b3f 100644 --- a/libnm-glib/libnm-glib.ver +++ b/libnm-glib/libnm-glib.ver @@ -141,6 +141,7 @@ global: nm_remote_settings_error_get_type; nm_remote_settings_error_quark; nm_remote_settings_get_connection_by_path; + nm_remote_settings_get_connection_by_uuid; nm_remote_settings_get_type; nm_remote_settings_list_connections; nm_remote_settings_new; diff --git a/libnm-glib/nm-remote-settings.c b/libnm-glib/nm-remote-settings.c index e8239d7875..70befd9e4e 100644 --- a/libnm-glib/nm-remote-settings.c +++ b/libnm-glib/nm-remote-settings.c @@ -182,6 +182,35 @@ nm_remote_settings_get_connection_by_path (NMRemoteSettings *settings, const cha return g_hash_table_lookup (NM_REMOTE_SETTINGS_GET_PRIVATE (settings)->connections, path); } +/** + * nm_remote_settings_get_connection_by_uuid: + * @settings: the %NMRemoteSettings + * @uuid: the UUID of the remote connection + * + * Returns the %NMRemoteConnection identified by @uuid. + * + * Returns: (transfer none): the remote connection object on success, or NULL if the object was + * not known + **/ +NMRemoteConnection * +nm_remote_settings_get_connection_by_uuid (NMRemoteSettings *settings, const char *uuid) +{ + GHashTableIter iter; + NMRemoteConnection *candidate; + + g_return_val_if_fail (settings != NULL, NULL); + g_return_val_if_fail (NM_IS_REMOTE_SETTINGS (settings), NULL); + g_return_val_if_fail (uuid != NULL, NULL); + + g_hash_table_iter_init (&iter, NM_REMOTE_SETTINGS_GET_PRIVATE (settings)->connections); + while (g_hash_table_iter_next (&iter, NULL, (gpointer) &candidate)) { + if (g_strcmp0 (uuid, nm_connection_get_uuid (NM_CONNECTION (candidate))) == 0) + return candidate; + } + + return NULL; +} + static void connection_removed_cb (NMRemoteConnection *remote, gpointer user_data) { diff --git a/libnm-glib/nm-remote-settings.h b/libnm-glib/nm-remote-settings.h index f6ea5fbe78..e882e4fa13 100644 --- a/libnm-glib/nm-remote-settings.h +++ b/libnm-glib/nm-remote-settings.h @@ -18,7 +18,7 @@ * Boston, MA 02110-1301 USA. * * Copyright (C) 2008 Novell, Inc. - * Copyright (C) 2009 Red Hat, Inc. + * Copyright (C) 2009 - 2011 Red Hat, Inc. */ #ifndef NM_REMOTE_SETTINGS_H @@ -115,6 +115,9 @@ GSList *nm_remote_settings_list_connections (NMRemoteSettings *settings); NMRemoteConnection * nm_remote_settings_get_connection_by_path (NMRemoteSettings *settings, const char *path); +NMRemoteConnection *nm_remote_settings_get_connection_by_uuid (NMRemoteSettings *settings, + const char *uuid); + gboolean nm_remote_settings_add_connection (NMRemoteSettings *settings, NMConnection *connection, NMRemoteSettingsAddConnectionFunc callback, diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 66aeb65c0b..87fa4b6c9c 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -91,6 +91,11 @@ static gboolean impl_settings_list_connections (NMSettings *self, GPtrArray **connections, GError **error); +static gboolean impl_settings_get_connection_by_uuid (NMSettings *self, + const char *uuid, + char **out_object_path, + GError **error); + static void impl_settings_add_connection (NMSettings *self, GHashTable *settings, DBusGMethodInvocation *context); @@ -222,6 +227,38 @@ impl_settings_list_connections (NMSettings *self, return TRUE; } +static gboolean +impl_settings_get_connection_by_uuid (NMSettings *self, + const char *uuid, + char **out_object_path, + GError **error) +{ + NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); + GHashTableIter iter; + NMConnection *candidate = NULL; + gboolean found = FALSE; + + load_connections (self); + + g_hash_table_iter_init (&iter, priv->connections); + while (g_hash_table_iter_next (&iter, NULL, (gpointer) &candidate)) { + if (g_strcmp0 (uuid, nm_connection_get_uuid (candidate)) == 0) { + *out_object_path = g_strdup (nm_connection_get_path (candidate)); + found = TRUE; + break; + } + } + + if (!found) { + g_set_error_literal (error, + NM_SETTINGS_ERROR, + NM_SETTINGS_ERROR_INVALID_CONNECTION, + "No connection with the UUID was found."); + } + + return found; +} + static int connection_sort (gconstpointer pa, gconstpointer pb) {