libnm: merge saved and unsaved connection methods
authorDan Winship <danw@gnome.org>
Thu, 11 Sep 2014 20:12:40 +0000 (16:12 -0400)
committerDan Winship <danw@gnome.org>
Thu, 25 Sep 2014 13:29:20 +0000 (09:29 -0400)
Merge nm_remote_settings_add_connection() and
nm_remote_settings_add_connection_unsaved(), and likewise
nm_remote_connection_commit_changes() and
nm_remote_connection_commit_changes_unsaved(), by adding a boolean
flag to each saying whether to save to disk.

clients/cli/connections.c
clients/tui/nmt-editor.c
examples/C/glib/add-connection-libnm.c
libnm/libnm.ver
libnm/nm-remote-connection.c
libnm/nm-remote-connection.h
libnm/nm-remote-settings.c
libnm/nm-remote-settings.h
libnm/tests/test-remote-settings-client.c
libnm/tests/test-secret-agent.c

index 28fa8f7..985336e 100644 (file)
@@ -4896,10 +4896,7 @@ add_new_connection (gboolean persistent,
                     NMRemoteSettingsAddConnectionFunc callback,
                     gpointer user_data)
 {
-       if (persistent)
-               return nm_remote_settings_add_connection (settings, connection, callback, user_data);
-       else
-               return nm_remote_settings_add_connection_unsaved (settings, connection, callback, user_data);
+       return nm_remote_settings_add_connection (settings, connection, persistent, callback, user_data);
 }
 
 static void
@@ -4908,10 +4905,7 @@ update_connection (gboolean persistent,
                    NMRemoteConnectionResultFunc callback,
                    gpointer user_data)
 {
-       if (persistent)
-               nm_remote_connection_commit_changes (connection, callback, user_data);
-       else
-               nm_remote_connection_commit_changes_unsaved (connection, callback, user_data);
+       nm_remote_connection_commit_changes (connection, persistent, callback, user_data);
 }
 
 static char *
index 0f35f48..46710ef 100644 (file)
@@ -133,7 +133,7 @@ save_connection_and_exit (NmtNewtButton *button,
 
        nmt_sync_op_init (&op);
        if (NM_IS_REMOTE_CONNECTION (priv->orig_connection)) {
-               nm_remote_connection_commit_changes (NM_REMOTE_CONNECTION (priv->orig_connection),
+               nm_remote_connection_commit_changes (NM_REMOTE_CONNECTION (priv->orig_connection), TRUE,
                                                     connection_updated, &op);
                if (!nmt_sync_op_wait_boolean (&op, &error)) {
                        nmt_newt_message_dialog (_("Unable to save connection: %s"),
@@ -147,7 +147,7 @@ save_connection_and_exit (NmtNewtButton *button,
                 */
                nm_connection_clear_secrets (priv->orig_connection);
        } else {
-               nm_remote_settings_add_connection (nm_settings, priv->orig_connection,
+               nm_remote_settings_add_connection (nm_settings, priv->orig_connection, TRUE,
                                                   connection_added, &op);
                if (!nmt_sync_op_wait_boolean (&op, &error)) {
                        nmt_newt_message_dialog (_("Unable to add new connection: %s"),
index 2b0143d..5963295 100644 (file)
@@ -89,7 +89,7 @@ add_connection (NMRemoteSettings *settings, GMainLoop *loop, const char *con_nam
        /* Ask the settings service to add the new connection; we'll quit the
         * mainloop and exit when the callback is called.
         */
-       success = nm_remote_settings_add_connection (settings, connection, added_cb, loop);
+       success = nm_remote_settings_add_connection (settings, connection, TRUE, added_cb, loop);
        if (!success)
                g_print ("Error adding connection\n");
 
index c9329cc..3fe5429 100644 (file)
@@ -337,7 +337,6 @@ global:
        nm_object_get_path;
        nm_object_get_type;
        nm_remote_connection_commit_changes;
-       nm_remote_connection_commit_changes_unsaved;
        nm_remote_connection_delete;
        nm_remote_connection_error_get_type;
        nm_remote_connection_error_quark;
@@ -347,7 +346,6 @@ global:
        nm_remote_connection_get_visible;
        nm_remote_connection_save;
        nm_remote_settings_add_connection;
-       nm_remote_settings_add_connection_unsaved;
        nm_remote_settings_error_get_type;
        nm_remote_settings_error_quark;
        nm_remote_settings_get_connection_by_id;
index 9c58192..6da5e18 100644 (file)
@@ -63,6 +63,7 @@ struct RemoteCall {
        RemoteCallFetchResultCb fetch_result_cb;
        GFunc callback;
        gpointer user_data;
+       gpointer call_data;
 };
 
 typedef struct {
@@ -131,9 +132,13 @@ update_result_cb (RemoteCall *call, GAsyncResult *result)
 {
        NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (call->self);
        NMRemoteConnectionResultFunc func = (NMRemoteConnectionResultFunc) call->callback;
+       gboolean save_to_disk = GPOINTER_TO_INT (call->call_data);
        GError *error = NULL;
 
-       nmdbus_settings_connection_call_update_finish (priv->proxy, result, &error);
+       if (save_to_disk)
+               nmdbus_settings_connection_call_update_finish (priv->proxy, result, &error);
+       else
+               nmdbus_settings_connection_call_update_unsaved_finish (priv->proxy, result, &error);
        if (func)
                (*func) (call->self, error, call->user_data);
        g_clear_error (&error);
@@ -142,15 +147,21 @@ update_result_cb (RemoteCall *call, GAsyncResult *result)
 /**
  * nm_remote_connection_commit_changes:
  * @connection: the #NMRemoteConnection
+ * @save_to_disk: whether to persist the changes to disk
  * @callback: (scope async) (allow-none): a function to be called when the
  * commit completes
  * @user_data: (closure): caller-specific data to be passed to @callback
  *
  * Send any local changes to the settings and properties of this connection to
- * NetworkManager, which will immediately save them to disk.
+ * NetworkManager.
+ *
+ * If @save_to_disk is %TRUE, the changes will immediately be saved to disk.
+ * If %FALSE, then only the in-memory version is changed. (It can be saved to
+ * disk later with nm_remote_connection_save().)
  **/
 void
 nm_remote_connection_commit_changes (NMRemoteConnection *self,
+                                     gboolean save_to_disk,
                                      NMRemoteConnectionResultFunc callback,
                                      gpointer user_data)
 {
@@ -165,61 +176,21 @@ nm_remote_connection_commit_changes (NMRemoteConnection *self,
        call = remote_call_new (self, update_result_cb, (GFunc) callback, user_data);
        if (!call)
                return;
+       call->call_data = GINT_TO_POINTER (save_to_disk);
 
        settings = nm_connection_to_dbus (NM_CONNECTION (self), NM_CONNECTION_SERIALIZE_ALL);
-       nmdbus_settings_connection_call_update (priv->proxy,
-                                               settings,
-                                               NULL,
-                                               remote_call_dbus_cb, call);
-}
-
-static void
-update_unsaved_result_cb (RemoteCall *call, GAsyncResult *result)
-{
-       NMRemoteConnectionPrivate *priv = NM_REMOTE_CONNECTION_GET_PRIVATE (call->self);
-       NMRemoteConnectionResultFunc func = (NMRemoteConnectionResultFunc) call->callback;
-       GError *error = NULL;
-
-       nmdbus_settings_connection_call_update_unsaved_finish (priv->proxy, result, &error);
-       if (func)
-               (*func) (call->self, error, call->user_data);
-       g_clear_error (&error);
-}
 
-/**
- * nm_remote_connection_commit_changes_unsaved:
- * @connection: the #NMRemoteConnection
- * @callback: (scope async) (allow-none): a function to be called when the
- * commit completes
- * @user_data: (closure): caller-specific data to be passed to @callback
- *
- * Send any local changes to the settings and properties of this connection to
- * NetworkManager.  The changes are not saved to disk until either
- * nm_remote_connection_save() or nm_remote_connection_commit_changes() is
- * called.
- **/
-void
-nm_remote_connection_commit_changes_unsaved (NMRemoteConnection *connection,
-                                             NMRemoteConnectionResultFunc callback,
-                                             gpointer user_data)
-{
-       NMRemoteConnectionPrivate *priv;
-       GVariant *settings;
-       RemoteCall *call;
-
-       g_return_if_fail (NM_IS_REMOTE_CONNECTION (connection));
-
-       priv = NM_REMOTE_CONNECTION_GET_PRIVATE (connection);
-
-       call = remote_call_new (connection, update_unsaved_result_cb, (GFunc) callback, user_data);
-       if (!call)
-               return;
-
-       settings = nm_connection_to_dbus (NM_CONNECTION (connection), NM_CONNECTION_SERIALIZE_ALL);
-       nmdbus_settings_connection_call_update_unsaved (priv->proxy,
-                                                       settings,
-                                                       NULL,
-                                                       remote_call_dbus_cb, call);
+       if (save_to_disk) {
+               nmdbus_settings_connection_call_update (priv->proxy,
+                                                       settings,
+                                                       NULL,
+                                                       remote_call_dbus_cb, call);
+       } else {
+               nmdbus_settings_connection_call_update_unsaved (priv->proxy,
+                                                               settings,
+                                                               NULL,
+                                                               remote_call_dbus_cb, call);
+       }
 }
 
 static void
index fc5b87d..5c6bebf 100644 (file)
@@ -105,13 +105,10 @@ typedef void (*NMRemoteConnectionGetSecretsFunc) (NMRemoteConnection *connection
 GType nm_remote_connection_get_type (void);
 
 void nm_remote_connection_commit_changes (NMRemoteConnection *connection,
+                                          gboolean save_to_disk,
                                           NMRemoteConnectionResultFunc callback,
                                           gpointer user_data);
 
-void nm_remote_connection_commit_changes_unsaved (NMRemoteConnection *connection,
-                                                  NMRemoteConnectionResultFunc callback,
-                                                  gpointer user_data);
-
 void nm_remote_connection_save (NMRemoteConnection *connection,
                                 NMRemoteConnectionResultFunc callback,
                                 gpointer user_data);
index 8c9eb09..a867f63 100644 (file)
@@ -181,6 +181,7 @@ typedef struct {
        NMRemoteSettingsAddConnectionFunc callback;
        gpointer callback_data;
        char *path;
+       gboolean saved;
 } AddConnectionInfo;
 
 static AddConnectionInfo *
@@ -430,14 +431,24 @@ add_connection_done (GObject *proxy, GAsyncResult *result, gpointer user_data)
        AddConnectionInfo *info = user_data;
        GError *error = NULL;
 
-       if (nmdbus_settings_call_add_connection_finish (NMDBUS_SETTINGS (proxy),
-                                                       &info->path,
-                                                       result, &error)) {
-               /* Wait until this connection is fully initialized before calling the callback */
+       if (info->saved) {
+               nmdbus_settings_call_add_connection_finish (NMDBUS_SETTINGS (proxy),
+                                                           &info->path,
+                                                           result, &error);
        } else {
+               nmdbus_settings_call_add_connection_unsaved_finish (NMDBUS_SETTINGS (proxy),
+                                                                   &info->path,
+                                                                   result, &error);
+       }
+
+       if (error) {
                add_connection_info_complete (info->self, info, NULL, error);
                g_clear_error (&error);
        }
+
+       /* On success, we still have to wait until the connection is fully
+        * initialized before calling the callback.
+        */
 }
 
 /**
@@ -445,14 +456,20 @@ add_connection_done (GObject *proxy, GAsyncResult *result, gpointer user_data)
  * @settings: the %NMRemoteSettings
  * @connection: the connection to add. Note that this object's settings will be
  *   added, not the object itself
+ * @save_to_disk: whether to immediately save the connection to disk
  * @callback: (scope async): callback to be called when the add operation completes
  * @user_data: (closure): caller-specific data passed to @callback
  *
  * Requests that the remote settings service add the given settings to a new
- * connection.  The connection is immediately written to disk.  @connection is
- * untouched by this function and only serves as a template of the settings to
- * add.  The #NMRemoteConnection object that represents what NetworkManager
- * actually added is returned to @callback when the addition operation is complete.
+ * connection.  If @save_to_disk is %TRUE, the connection is immediately written
+ * to disk; otherwise it is initially only stored in memory, but may be saved
+ * later by calling the connection's nm_remote_connection_commit_changes()
+ * method.
+ *
+ * @connection is untouched by this function and only serves as a template of
+ * the settings to add.  The #NMRemoteConnection object that represents what
+ * NetworkManager actually added is returned to @callback when the addition
+ * operation is complete.
  *
  * Note that the #NMRemoteConnection returned in @callback may not contain
  * identical settings to @connection as NetworkManager may perform automatic
@@ -463,6 +480,7 @@ add_connection_done (GObject *proxy, GAsyncResult *result, gpointer user_data)
 gboolean
 nm_remote_settings_add_connection (NMRemoteSettings *settings,
                                    NMConnection *connection,
+                                   gboolean save_to_disk,
                                    NMRemoteSettingsAddConnectionFunc callback,
                                    gpointer user_data)
 {
@@ -483,68 +501,28 @@ nm_remote_settings_add_connection (NMRemoteSettings *settings,
        info->self = settings;
        info->callback = callback;
        info->callback_data = user_data;
+       info->saved = save_to_disk;
 
        new_settings = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
-       nmdbus_settings_call_add_connection (priv->proxy,
-                                            new_settings,
-                                            NULL,
-                                            add_connection_done, info);
 
-       priv->add_list = g_slist_append (priv->add_list, info);
-
-       return TRUE;
-}
-
-/**
- * nm_remote_settings_add_connection_unsaved:
- * @settings: the %NMRemoteSettings
- * @connection: the connection to add. Note that this object's settings will be
- *   added, not the object itself
- * @callback: (scope async): callback to be called when the add operation completes
- * @user_data: (closure): caller-specific data passed to @callback
- *
- * Requests that the remote settings service add the given settings to a new
- * connection.  The connection is not written to disk, which may be done at
- * a later time by calling the connection's nm_remote_connection_commit_changes()
- * method.
- *
- * Returns: %TRUE if the request was successful, %FALSE if it failed
- **/
-gboolean
-nm_remote_settings_add_connection_unsaved (NMRemoteSettings *settings,
-                                           NMConnection *connection,
-                                           NMRemoteSettingsAddConnectionFunc callback,
-                                           gpointer user_data)
-{
-       NMRemoteSettingsPrivate *priv;
-       AddConnectionInfo *info;
-       GVariant *new_settings;
-
-       g_return_val_if_fail (NM_IS_REMOTE_SETTINGS (settings), FALSE);
-       g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
-       g_return_val_if_fail (callback != NULL, FALSE);
-
-       priv = NM_REMOTE_SETTINGS_GET_PRIVATE (settings);
-
-       if (!_nm_object_get_nm_running (NM_OBJECT (settings)))
-               return FALSE;
-
-       info = g_malloc0 (sizeof (AddConnectionInfo));
-       info->self = settings;
-       info->callback = callback;
-       info->callback_data = user_data;
-
-       new_settings = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
-       nmdbus_settings_call_add_connection_unsaved (priv->proxy,
-                                                    new_settings,
-                                                    NULL,
-                                                    add_connection_done, info);
+       if (save_to_disk) {
+               nmdbus_settings_call_add_connection (priv->proxy,
+                                                    new_settings,
+                                                    NULL,
+                                                    add_connection_done, info);
+       } else {
+               nmdbus_settings_call_add_connection_unsaved (priv->proxy,
+                                                            new_settings,
+                                                            NULL,
+                                                            add_connection_done, info);
+       }
 
        priv->add_list = g_slist_append (priv->add_list, info);
 
        return TRUE;
 }
 
+
 /**
  * nm_remote_settings_load_connections:
  * @settings: the %NMRemoteSettings
index afda606..11c4955 100644 (file)
@@ -131,14 +131,10 @@ NMRemoteConnection *nm_remote_settings_get_connection_by_uuid (NMRemoteSettings
 
 gboolean nm_remote_settings_add_connection (NMRemoteSettings *settings,
                                             NMConnection *connection,
+                                            gboolean save_to_disk,
                                             NMRemoteSettingsAddConnectionFunc callback,
                                             gpointer user_data);
 
-gboolean nm_remote_settings_add_connection_unsaved (NMRemoteSettings *settings,
-                                                    NMConnection *connection,
-                                                    NMRemoteSettingsAddConnectionFunc callback,
-                                                    gpointer user_data);
-
 gboolean nm_remote_settings_load_connections (NMRemoteSettings *settings,
                                               char **filenames,
                                               char ***failures,
index 7543d2c..4f8d873 100644 (file)
@@ -63,6 +63,7 @@ test_add_connection (void)
 
        success = nm_remote_settings_add_connection (settings,
                                                     connection,
+                                                    TRUE,
                                                     add_cb,
                                                     &done);
        g_assert (success == TRUE);
@@ -387,6 +388,7 @@ test_add_remove_connection (void)
        connection = nmtst_create_minimal_connection (TEST_ADD_REMOVE_ID, NULL, NM_SETTING_WIRED_SETTING_NAME, NULL);
        success = nm_remote_settings_add_connection (settings,
                                                     connection,
+                                                    TRUE,
                                                     add_remove_cb,
                                                     &done);
        g_assert (success == TRUE);
index 27b844d..4bb99a7 100644 (file)
@@ -316,6 +316,7 @@ test_setup (TestSecretAgentData *sadata, gconstpointer test_data)
 
        success = nm_remote_settings_add_connection (sadata->settings,
                                                     connection,
+                                                    TRUE,
                                                     connection_added_cb,
                                                     sadata);
        g_assert (success == TRUE);