diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index aeb7c1549..43911fc31 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -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; diff --git a/libnm-util/nm-connection.c b/libnm-util/nm-connection.c index a74a19bb9..dff7546aa 100644 --- a/libnm-util/nm-connection.c +++ b/libnm-util/nm-connection.c @@ -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 diff --git a/libnm-util/nm-connection.h b/libnm-util/nm-connection.h index 4d060cc98..9c36c4045 100644 --- a/libnm-util/nm-connection.h +++ b/libnm-util/nm-connection.h @@ -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); diff --git a/libnm-util/tests/test-general.c b/libnm-util/tests/test-general.c index a18c76ec5..7e494a176 100644 --- a/libnm-util/tests/test-general.c +++ b/libnm-util/tests/test-general.c @@ -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 ();