libnm-util: add non-failing versions of nm_connection_new_from_hash() and replace...
[NetworkManager.git] / libnm-util / nm-connection.c
index f77e322..51c4802 100644 (file)
@@ -327,19 +327,30 @@ validate_permissions_type (GHashTable *hash, GError **error)
        return TRUE;
 }
 
-static void
-hash_to_connection (NMConnection *connection, GHashTable *new)
+/**
+ * _nm_connection_replace_settings:
+ * @connection: a #NMConnection
+ * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
+ **/
+void
+_nm_connection_replace_settings (NMConnection *connection,
+                                 GHashTable *new_settings)
 {
+       NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
        GHashTableIter iter;
        const char *setting_name;
        GHashTable *setting_hash;
        gboolean changed;
-       NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
+
+       g_return_if_fail (NM_IS_CONNECTION (connection));
+       g_return_if_fail (new_settings != NULL);
+
+       priv = NM_CONNECTION_GET_PRIVATE (connection);
 
        if ((changed = g_hash_table_size (priv->settings) > 0))
                g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
 
-       g_hash_table_iter_init (&iter, new);
+       g_hash_table_iter_init (&iter, new_settings);
        while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting_hash)) {
                GType type = nm_connection_lookup_setting_type (setting_name);
 
@@ -373,13 +384,12 @@ nm_connection_replace_settings (NMConnection *connection,
 {
        g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
        g_return_val_if_fail (new_settings != NULL, FALSE);
-       if (error)
-               g_return_val_if_fail (*error == NULL, FALSE);
+       g_return_val_if_fail (!error || !*error, FALSE);
 
        if (!validate_permissions_type (new_settings, error))
                return FALSE;
 
-       hash_to_connection (connection, new_settings);
+       _nm_connection_replace_settings (connection, new_settings);
        return nm_connection_verify (connection, error);
 }
 
@@ -1440,6 +1450,29 @@ nm_connection_new (void)
        return (NMConnection *) g_object_new (NM_TYPE_CONNECTION, NULL);
 }
 
+/**
+ * _nm_connection_new_from_hash:
+ * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
+ * the connection
+ *
+ * Creates a new #NMConnection from a hash table describing the connection.  See
+ * nm_connection_to_hash() for a description of the expected hash table.
+ *
+ * Returns: the new #NMConnection object, populated with settings created
+ * from the values in the hash table.
+ **/
+NMConnection *
+_nm_connection_new_from_hash (GHashTable *hash)
+{
+       NMConnection *connection;
+
+       g_return_val_if_fail (hash != NULL, NULL);
+
+       connection = nm_connection_new ();
+       _nm_connection_replace_settings (connection, hash);
+       return connection;
+}
+
 /**
  * nm_connection_new_from_hash:
  * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
@@ -1463,8 +1496,7 @@ nm_connection_new_from_hash (GHashTable *hash, GError **error)
        if (!validate_permissions_type (hash, error))
                return NULL;
 
-       connection = nm_connection_new ();
-       hash_to_connection (connection, hash);
+       connection = _nm_connection_new_from_hash (hash);
        if (!nm_connection_verify (connection, error))
                g_clear_object (&connection);
        return connection;