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.
This commit is contained in:
Dan Williams 2011-04-22 11:55:52 -05:00
parent bb8e9a0b18
commit 4cae0bb0fa
5 changed files with 88 additions and 1 deletions

View file

@ -18,6 +18,23 @@
</arg>
</method>
<method name="GetConnectionByUuid">
<tp:docstring>
Retrieve the object path of a connection, given that connection's UUID.
</tp:docstring>
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_settings_get_connection_by_uuid"/>
<arg name="uuid" type="s" direction="in">
<tp:docstring>
The UUID to find the connection object path for.
</tp:docstring>
</arg>
<arg name="connection" type="o" direction="out">
<tp:docstring>
The connection's object path.
</tp:docstring>
</arg>
</method>
<method name="AddConnection">
<tp:docstring>
Add new connection.

View file

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

View file

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

View file

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

View file

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