Index: src/anjuta-app.c
===================================================================
--- src/anjuta-app.c	(Revision 4861)
+++ src/anjuta-app.c	(Arbeitskopie)
@@ -498,6 +498,8 @@
 	
 	/* Loading accels */
 	anjuta_ui_load_accels (NULL);
+
+	app->save_count = 0;
 }
 
 static void
@@ -768,6 +770,20 @@
 	}
 }
 
+static void
+anjuta_app_saving_push (AnjutaShell* shell)
+{
+	AnjutaApp* app = ANJUTA_APP (shell);
+	app->save_count++;
+}
+
+static void
+anjuta_app_saving_pop (AnjutaShell* shell)
+{
+	AnjutaApp* app = ANJUTA_APP (shell);
+	app->save_count--;
+}
+
 static gboolean
 remove_from_widgets_hash (gpointer name, gpointer hash_widget, gpointer widget)
 {
@@ -1041,6 +1057,8 @@
 	iface->get_preferences = anjuta_app_get_preferences;
 	iface->get_plugin_manager = anjuta_app_get_plugin_manager;
 	iface->get_profile_manager = anjuta_app_get_profile_manager;
+	iface->saving_push = anjuta_app_saving_push;
+	iface->saving_pop = anjuta_app_saving_pop;
 }
 
 ANJUTA_TYPE_BEGIN(AnjutaApp, anjuta_app, GTK_TYPE_WINDOW);
Index: src/anjuta-app.h
===================================================================
--- src/anjuta-app.h	(Revision 4861)
+++ src/anjuta-app.h	(Arbeitskopie)
@@ -58,6 +58,8 @@
 	AnjutaPreferences *preferences;
 	AnjutaPluginManager *plugin_manager;
 	AnjutaProfileManager *profile_manager;
+
+	gint save_count;
 };
 
 struct _AnjutaAppClass
Index: src/anjuta.c
===================================================================
--- src/anjuta.c	(Revision 4861)
+++ src/anjuta.c	(Arbeitskopie)
@@ -79,6 +79,15 @@
 				break;
 		}
 	}
+	/* Wait for files to be really saved (asyncronous operation) */
+	if (app->save_count > 0)
+	{
+		g_message ("Waiting for %d file(s) to be saved!", app->save_count);
+		while (app->save_count > 0)
+		{
+			g_main_context_iteration (NULL, TRUE);
+		}
+	}
 	
 	/* If current active profile is "user", save current session as
 	 * default session
Index: src/main.c
===================================================================
--- src/main.c	(Revision 4861)
+++ src/main.c	(Arbeitskopie)
@@ -184,7 +184,7 @@
 	/* Initialize gnome program */
 	if (!g_option_context_parse (context, &argc, &argv, &error))
 	{
-		DEBUG_PRINT ("Option parsing failed: %s", error);
+		DEBUG_PRINT ("Option parsing failed: %s", error->message);
 		exit(1);
 	}
 	
Index: plugins/sourceview/sourceview-io.c
===================================================================
--- plugins/sourceview/sourceview-io.c	(Revision 4861)
+++ plugins/sourceview/sourceview-io.c	(Arbeitskopie)
@@ -236,14 +236,18 @@
 }
 
 static void
-on_save_finished (GObject* output_stream, GAsyncResult* result, gpointer data)
+on_save_finished (GObject* file, GAsyncResult* result, gpointer data)
 {
 	SourceviewIO* sio = SOURCEVIEW_IO(data);
+	AnjutaShell* shell = ANJUTA_PLUGIN (sio->sv->priv->plugin)->shell;
 	GError* err = NULL;
-	g_output_stream_write_finish (G_OUTPUT_STREAM(output_stream),
-								  result,
-								  &err);
+	gchar* etag;
+	g_file_replace_contents_finish (G_FILE (file),
+	                                result,
+	                                &etag,
+	                                &err);
 	g_free (sio->write_buffer);
+	g_free (etag);
 	sio->write_buffer = NULL;
 	if (err)
 	{
@@ -253,12 +257,11 @@
 	else
 	{
 		set_display_name (sio);
-		g_output_stream_close(G_OUTPUT_STREAM (output_stream), NULL, NULL);
 		setup_monitor (sio);
 		g_signal_emit_by_name (sio, "save-finished");
 	}
-	g_object_unref (output_stream);
 	g_object_unref (sio);
+	anjuta_shell_saving_pop (shell);
 }
 
 void
@@ -279,9 +282,9 @@
 void
 sourceview_io_save_as (SourceviewIO* sio, GFile* file)
 {
-	GFileOutputStream* output_stream;
-	GError* err = NULL;
+	AnjutaShell* shell = ANJUTA_PLUGIN (sio->sv->priv->plugin)->shell;
 	gboolean backup = TRUE;
+	gsize len;
 	
 	g_return_if_fail (file != NULL);
 	
@@ -290,19 +293,11 @@
 	backup = anjuta_preferences_get_int_with_default (sio->sv->priv->prefs,
 													  "sourceview.backup", TRUE);
 	
-	output_stream = g_file_replace (file, NULL, backup, G_FILE_CREATE_NONE,
-									NULL, NULL);
-	if (!output_stream)
-	{
-		g_signal_emit_by_name (sio, "save-failed", err);
-		g_error_free (err);
-		return;
-	}
-	
 	if (sio->last_encoding == NULL)
 	{
 		sio->write_buffer = ianjuta_editor_get_text_all (IANJUTA_EDITOR(sio->sv), 
 														 NULL);
+		len = strlen (sio->write_buffer);
 	}
 	else
 	{
@@ -312,7 +307,7 @@
 		sio->write_buffer = anjuta_convert_from_utf8 (buffer_text,
 													  -1,
 													  sio->last_encoding,
-													  NULL,
+													  &len,
 													  &err);
 		g_free (buffer_text);
 		if (err != NULL)
@@ -323,13 +318,16 @@
 		}
 	}
 	g_cancellable_reset (sio->cancel);
-	g_output_stream_write_async (G_OUTPUT_STREAM (output_stream),
-								 sio->write_buffer,
-								 strlen (sio->write_buffer),
-								 G_PRIORITY_LOW,
-								 sio->cancel,
-								 on_save_finished,
-								 sio);
+	g_file_replace_contents_async (file,
+	                               sio->write_buffer,
+	                               len,
+	                               NULL,
+	                               backup,
+	                               G_FILE_CREATE_NONE,
+	                               sio->cancel,
+	                               on_save_finished,
+	                               sio);
+	anjuta_shell_saving_push (shell);
 	
 	if (sio->file != file)
 	{
Index: ChangeLog
===================================================================
--- ChangeLog	(Revision 4861)
+++ ChangeLog	(Arbeitskopie)
@@ -1,3 +1,25 @@
+2009-03-15  Johannes Schmid <jhs@gnome.org>
+
+	* libanjuta/anjuta-shell.c (anjuta_shell_saving_push),
+	(anjuta_shell_saving_pop):
+	* libanjuta/anjuta-shell.h:
+	* src/anjuta-app.c (anjuta_app_instance_init),
+	(anjuta_app_saving_push), (anjuta_app_saving_pop),
+	(anjuta_shell_iface_init):
+	* src/anjuta-app.h:
+	* src/anjuta.c (on_anjuta_delete_event):
+	Implemented infrastructure to wait for all async save operation before 
+	exiting
+
+	* src/main.c (main):
+	Fix trivial warning (error->message instead of error)
+
+	* plugins/sourceview/sourceview-io.c (on_save_finished),
+	(sourceview_io_save_as):
+	Use saving infrastructure to fix #574376 – Save doesn't always save. Use
+	g_file_replace_contents_async instead of GOutputStream to avoid duplicated
+	code with gio.
+
 2009-03-11  Johannes Schmid  <jhs@gnome.org>
 
 	* configure.in: Fixed "--enable-editor-scintilla"
Index: libanjuta/anjuta-shell.c
===================================================================
--- libanjuta/anjuta-shell.c	(Revision 4861)
+++ libanjuta/anjuta-shell.c	(Arbeitskopie)
@@ -754,6 +754,34 @@
 	return ANJUTA_SHELL_GET_IFACE (shell)->get_profile_manager (shell, error);
 }
 
+/**
+ * anjuta_shell_saving_push:
+ * @shell: A #AnjutaShell interface
+ *
+ * Increase the count of files that need to be saved 
+ *
+ */
+void anjuta_shell_saving_push	    (AnjutaShell* shell)
+{
+	g_return_if_fail (ANJUTA_IS_SHELL (shell));
+	
+	return ANJUTA_SHELL_GET_IFACE (shell)->saving_push (shell);
+}
+
+/**
+ * anjuta_shell_saving_push:
+ * @shell: A #AnjutaShell interface
+ *
+ * Decrease the count of files that need to be saved 
+ *
+ */
+void anjuta_shell_saving_pop	    (AnjutaShell* shell)
+{
+	g_return_if_fail (ANJUTA_IS_SHELL (shell));
+
+	return ANJUTA_SHELL_GET_IFACE (shell)->saving_pop (shell);
+}
+
 void
 anjuta_shell_session_save (AnjutaShell *shell, const gchar *session_directory,
 						   GError **error)
Index: libanjuta/anjuta-shell.h
===================================================================
--- libanjuta/anjuta-shell.h	(Revision 4861)
+++ libanjuta/anjuta-shell.h	(Arbeitskopie)
@@ -108,6 +108,8 @@
 	void (*remove_value)      (AnjutaShell  *shell,
 							   const char   *name,
 							   GError       **error);
+	void (*saving_push)       (AnjutaShell* shell);
+	void (*saving_pop)        (AnjutaShell* shell);
 	GObject* (*get_object)    (AnjutaShell  *shell,
 							   const char   *iface_name,
 							   GError       **error);
@@ -196,7 +198,10 @@
 									 AnjutaSavePrompt *prompt,
 									 GError **error);
 void anjuta_shell_notify_exit       (AnjutaShell *shell, GError **error);
+void anjuta_shell_saving_push	    (AnjutaShell* shell);
+void anjuta_shell_saving_pop	    (AnjutaShell* shell);
 
+
 /**
  * anjuta_shell_get_interface:
  * @shell: A #AnjutaShell object
