[network-manager-applet] editor: ensure that canceled changes get thrown away



commit 55ee94609d12b669f27c77d39b95c3d37fd95f36
Author: Dan Williams <dcbw redhat com>
Date:   Sat Sep 19 11:25:23 2009 -0700

    editor: ensure that canceled changes get thrown away
    
    The editor modifies ->connection in-place; so work on a copy
    and allow callers to update the real connection from the modified
    copy only when the user actually hits OK.

 src/connection-editor/nm-connection-editor.c |   29 +++++++++++++++++++++++--
 src/connection-editor/nm-connection-editor.h |    2 +
 src/connection-editor/nm-connection-list.c   |   25 +++++++++++++++++----
 3 files changed, 48 insertions(+), 8 deletions(-)
---
diff --git a/src/connection-editor/nm-connection-editor.c b/src/connection-editor/nm-connection-editor.c
index f1eec5f..d757dc1 100644
--- a/src/connection-editor/nm-connection-editor.c
+++ b/src/connection-editor/nm-connection-editor.c
@@ -311,6 +311,10 @@ dispose (GObject *object)
 		g_object_unref (editor->connection);
 		editor->connection = NULL;
 	}
+	if (editor->orig_connection) {
+		g_object_unref (editor->orig_connection);
+		editor->orig_connection = NULL;
+	}
 	if (editor->window) {
 		gtk_widget_destroy (editor->window);
 		editor->window = NULL;
@@ -406,7 +410,24 @@ nm_connection_editor_get_connection (NMConnectionEditor *editor)
 {
 	g_return_val_if_fail (NM_IS_CONNECTION_EDITOR (editor), NULL);
 
-	return editor->connection;
+	return editor->orig_connection;
+}
+
+gboolean
+nm_connection_editor_update_connection (NMConnectionEditor *editor, GError **error)
+{
+	GHashTable *settings;
+
+	g_return_val_if_fail (NM_IS_CONNECTION_EDITOR (editor), FALSE);
+
+	if (!nm_connection_verify (editor->connection, error))
+		return FALSE;
+
+	/* Copy the modified connection to the original connection */
+	settings = nm_connection_to_hash (editor->connection);
+	nm_connection_replace_settings (editor->orig_connection, settings, NULL);
+	g_hash_table_destroy (settings);
+	return TRUE;
 }
 
 static void
@@ -541,8 +562,10 @@ nm_connection_editor_set_connection (NMConnectionEditor *editor,
 	if (editor->connection)
 		g_object_unref (editor->connection);
 
-	editor->connection = g_object_ref (connection);
-	editor->orig_scope = nm_connection_get_scope (connection);
+	editor->connection = nm_connection_duplicate (connection);
+
+	editor->orig_connection = g_object_ref (connection);
+	editor->orig_scope = nm_connection_get_scope (editor->connection);
 	nm_connection_editor_update_title (editor);
 
 	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (editor->connection, NM_TYPE_SETTING_CONNECTION));
diff --git a/src/connection-editor/nm-connection-editor.h b/src/connection-editor/nm-connection-editor.h
index e5bce34..ff6a77f 100644
--- a/src/connection-editor/nm-connection-editor.h
+++ b/src/connection-editor/nm-connection-editor.h
@@ -39,6 +39,7 @@ typedef struct {
 
 	/* private data */
 	NMConnection *connection;
+	NMConnection *orig_connection;
 	gboolean initialized;
 
 	NMConnectionScope orig_scope;
@@ -69,6 +70,7 @@ void                nm_connection_editor_present (NMConnectionEditor *editor);
 void                nm_connection_editor_run (NMConnectionEditor *editor);
 void                nm_connection_editor_save_vpn_secrets (NMConnectionEditor *editor);
 NMConnection *      nm_connection_editor_get_connection (NMConnectionEditor *editor);
+gboolean            nm_connection_editor_update_connection (NMConnectionEditor *editor, GError **error);
 GtkWindow *         nm_connection_editor_get_window (NMConnectionEditor *editor);
 
 #endif
diff --git a/src/connection-editor/nm-connection-list.c b/src/connection-editor/nm-connection-list.c
index 62d6bdf..4730784 100644
--- a/src/connection-editor/nm-connection-list.c
+++ b/src/connection-editor/nm-connection-list.c
@@ -591,12 +591,27 @@ static void
 add_response_cb (NMConnectionEditor *editor, gint response, GError *error, gpointer user_data)
 {
 	ActionInfo *info = (ActionInfo *) user_data;
-	const char *message = _("An unknown error ocurred.");
+	GError *add_error = NULL;
 
 	if (response == GTK_RESPONSE_OK) {
-		add_connection (info->list, editor, add_finished_cb, editor);
-		return;
+		/* Verify and commit user changes */
+		if (nm_connection_editor_update_connection (editor, &add_error)) {
+			/* Yay we can try to add the connection; it'll get removed from
+			 * list->editors when the add finishes.
+			 */
+			add_connection (info->list, editor, add_finished_cb, editor);
+			return;
+		} else {
+			error_dialog (GTK_WINDOW (editor->window),
+			              _("Error editing connection: property '%s' / '%s' invalid: %d"),
+			              g_type_name (nm_connection_lookup_setting_type_by_quark (add_error->domain)),
+			              (add_error && add_error->message) ? add_error->message : "(unknown)",
+			              add_error ? add_error->code : -1);
+			g_clear_error (&add_error);
+		}
 	} else if (response == GTK_RESPONSE_NONE) {
+		const char *message = _("An unknown error ocurred.");
+
 		if (error && error->message)
 			message = error->message;
 		error_dialog (GTK_WINDOW (editor->window),
@@ -720,9 +735,9 @@ edit_done_cb (NMConnectionEditor *editor, gint response, GError *error, gpointer
 
 	switch (response) {
 	case GTK_RESPONSE_OK:
-		/* Make sure the connection is valid */
+		/* Verify and commit user changes */
 		utils_fill_connection_certs (connection);
-		success = nm_connection_verify (connection, &edit_error);
+		success = nm_connection_editor_update_connection (editor, &edit_error);
 		utils_clear_filled_connection_certs (connection);
 
 		if (success) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]