Commit bafd0d55 authored by Dan Williams's avatar Dan Williams
Browse files

libnm-util: add nm_connection_replace_settings_from_connection() (bgo #696387)

Convenience function to replace settings in one conneciton with settings
from another, without having to go through the nm_connection_to_hash()
steps, which are just inefficient and kinda pointless.
parent cb3a5998
......@@ -48,6 +48,7 @@ global:
nm_connection_new_from_hash;
nm_connection_remove_setting;
nm_connection_replace_settings;
nm_connection_replace_settings_from_connection;
nm_connection_set_path;
nm_connection_to_hash;
nm_connection_update_secrets;
......
......@@ -19,7 +19,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* (C) Copyright 2007 - 2011 Red Hat, Inc.
* (C) Copyright 2007 - 2013 Red Hat, Inc.
* (C) Copyright 2007 - 2008 Novell, Inc.
*/
......@@ -479,6 +479,45 @@ nm_connection_replace_settings (NMConnection *connection,
return hash_to_connection (connection, new_settings, error);
}
/**
* nm_connection_replace_settings_from_connection:
* @connection: a #NMConnection
* @new_connection: a #NMConnection to replace the settings of @connection with
* @error: location to store error, or %NULL
*
* Deep-copies the settings of @new_conenction and replaces the settings of @connection
* with the copied settings.
*
* Returns: %TRUE if the settings were valid and added to the connection, %FALSE
* if they were not
*
* Since: 0.9.10
**/
gboolean
nm_connection_replace_settings_from_connection (NMConnection *connection,
NMConnection *new_connection,
GError **error)
{
GHashTableIter iter;
NMSetting *setting;
g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
g_return_val_if_fail (NM_IS_CONNECTION (new_connection), FALSE);
if (error)
g_return_val_if_fail (*error == NULL, FALSE);
/* No need to validate permissions like nm_connection_replace_settings()
* since we're dealing with an NMConnection which has already done that.
*/
g_hash_table_remove_all (NM_CONNECTION_GET_PRIVATE (connection)->settings);
g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (new_connection)->settings);
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
nm_connection_add_setting (connection, nm_setting_duplicate (setting));
return nm_connection_verify (connection, error);
}
/**
* nm_connection_compare:
* @a: a #NMConnection
......
......@@ -19,7 +19,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* (C) Copyright 2007 - 2008 Red Hat, Inc.
* (C) Copyright 2007 - 2013 Red Hat, Inc.
* (C) Copyright 2007 - 2008 Novell, Inc.
*/
......@@ -135,6 +135,10 @@ gboolean nm_connection_replace_settings (NMConnection *connection,
GHashTable *new_settings,
GError **error);
gboolean nm_connection_replace_settings_from_connection (NMConnection *connection,
NMConnection *new_connection,
GError **error);
gboolean nm_connection_compare (NMConnection *a,
NMConnection *b,
NMSettingCompareFlags flags);
......
......@@ -742,6 +742,68 @@ test_connection_replace_settings ()
g_object_unref (connection);
}
static void
test_connection_replace_settings_from_connection ()
{
NMConnection *connection, *replacement;
GError *error = NULL;
gboolean success;
NMSettingConnection *s_con;
NMSetting *setting;
GByteArray *ssid;
char *uuid = NULL;
const char *expected_id = "Awesome connection";
connection = new_test_connection ();
g_assert (connection);
replacement = nm_connection_new ();
g_assert (replacement);
/* New connection setting */
setting = nm_setting_connection_new ();
g_assert (setting);
uuid = nm_utils_uuid_generate ();
g_object_set (setting,
NM_SETTING_CONNECTION_ID, expected_id,
NM_SETTING_CONNECTION_UUID, uuid,
NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
NULL);
nm_connection_add_setting (replacement, setting);
/* New wifi setting */
setting = nm_setting_wireless_new ();
g_assert (setting);
ssid = g_byte_array_new ();
g_byte_array_append (ssid, (const guint8 *) "1234567", 7);
g_object_set (setting,
NM_SETTING_WIRELESS_SSID, ssid,
NM_SETTING_WIRELESS_MODE, "infrastructure",
NULL);
g_byte_array_free (ssid, TRUE);
nm_connection_add_setting (replacement, setting);
/* Replace settings and test */
success = nm_connection_replace_settings_from_connection (connection, replacement, &error);
g_assert_no_error (error);
g_assert (success);
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id);
g_assert_cmpstr (nm_setting_connection_get_uuid (s_con), ==, uuid);
g_assert (!nm_connection_get_setting_wired (connection));
g_assert (!nm_connection_get_setting_ip6_config (connection));
g_assert (nm_connection_get_setting_wireless (connection));
g_free (uuid);
g_object_unref (replacement);
g_object_unref (connection);
}
static void
test_connection_new_from_hash ()
{
......@@ -1634,6 +1696,7 @@ int main (int argc, char **argv)
test_connection_to_hash_setting_name ();
test_setting_new_from_hash ();
test_connection_replace_settings ();
test_connection_replace_settings_from_connection ();
test_connection_new_from_hash ();
test_setting_connection_permissions_helpers ();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment