Index: src/eog-list-store.c
===================================================================
--- src/eog-list-store.c	(revision 3478)
+++ src/eog-list-store.c	(working copy)
@@ -47,6 +47,12 @@
 	const gchar *text_uri;
 } MonitorHandleContext;
 
+typedef struct {
+	EogListStore *store;
+	EogListStoreForeachFunc func;
+	gpointer data;
+} EogListStoreForeachContext;
+
 G_DEFINE_TYPE (EogListStore, eog_list_store, GTK_TYPE_LIST_STORE);
 
 gboolean
@@ -688,3 +694,39 @@
 			    EOG_LIST_STORE_THUMB_SET, FALSE,
 			    -1);	
 }
+
+static gboolean
+eog_list_store_foreach_helper (GtkTreeModel *model,
+			       GtkTreePath *path,
+			       GtkTreeIter *iter,
+			       gpointer data)
+{
+	EogImage *image;
+	EogListStoreForeachContext *ctx;
+
+	ctx = (EogListStoreForeachContext *)data;
+	
+	gtk_tree_model_get (GTK_TREE_MODEL (ctx->store), iter, 
+			    EOG_LIST_STORE_EOG_IMAGE, &image,
+			    -1);
+
+	return (*ctx->func) (ctx->store, image, ctx->data);
+}
+
+void
+eog_list_store_foreach (EogListStore *store,
+			EogListStoreForeachFunc func,
+			gpointer data)
+{
+	EogListStoreForeachContext *ctx = g_new0 (EogListStoreForeachContext, 1);
+
+	ctx->store = store;
+	ctx->func  = func;
+	ctx->data  = data;
+	
+	gtk_tree_model_foreach (GTK_TREE_MODEL (store),
+				eog_list_store_foreach_helper,
+				ctx);
+
+	g_free (ctx);
+}
Index: src/eog-list-store.h
===================================================================
--- src/eog-list-store.h	(revision 3478)
+++ src/eog-list-store.h	(working copy)
@@ -68,6 +68,8 @@
 	void (* _eog_reserved4) (void);
 } EogListStoreClass;
 
+typedef gboolean (* EogListStoreForeachFunc) (EogListStore *store, EogImage *image, gpointer data);
+
 GType           eog_list_store_get_type 	     (void);
 
 GtkListStore   *eog_list_store_new 		     (void);
@@ -105,6 +107,10 @@
 void            eog_list_store_thumbnail_unset       (EogListStore *store, 
 						      GtkTreeIter *iter);
 
+void            eog_list_store_foreach               (EogListStore *store,
+						      EogListStoreForeachFunc func,
+						      gpointer data);
+
 #define EOG_LIST_STORE_THUMB_SIZE 90
 
 G_END_DECLS
Index: src/eog-application.c
===================================================================
--- src/eog-application.c	(revision 3478)
+++ src/eog-application.c	(working copy)
@@ -69,6 +69,22 @@
 	return instance;
 }
 
+static gboolean
+eog_application_on_win_delete_event (GtkWidget *widget,
+				     GdkEvent  *event,
+				     gpointer   user_data)
+{
+	gboolean can_close;
+	
+	EogWindow *window = EOG_WINDOW (widget);
+	
+	can_close = eog_window_confirm_close (window);
+
+	/* If we can close, then do not stop other handlers from
+	   being called */
+	return !can_close;
+}
+
 gboolean
 eog_application_open_window (EogApplication  *application,
 			     guint32         timestamp,
@@ -197,6 +213,11 @@
 		new_window = EOG_WINDOW (eog_window_new (flags));
 	}
 
+	g_signal_connect  (new_window,
+			   "delete-event",
+			   G_CALLBACK (eog_application_on_win_delete_event),
+			   application);
+
 	g_signal_connect (new_window, 
 			  "prepared", 
 			  G_CALLBACK (eog_application_show_window), 
@@ -274,4 +295,3 @@
 
 	return windows;
 }
-
Index: src/eog-window.c
===================================================================
--- src/eog-window.c	(revision 3478)
+++ src/eog-window.c	(working copy)
@@ -1611,7 +1611,8 @@
 {
 	g_return_if_fail (EOG_IS_WINDOW (user_data));
 
-	gtk_widget_destroy (GTK_WIDGET (user_data));
+	if (eog_window_confirm_close (EOG_WINDOW (user_data)))
+		gtk_widget_destroy (GTK_WIDGET (user_data));
 }
 
 static void
@@ -2708,7 +2709,8 @@
 		break;
 	case GDK_Q:
 	case GDK_q:
-		gtk_widget_destroy (widget);
+		if (eog_window_confirm_close (EOG_WINDOW (widget)))
+			gtk_widget_destroy (widget);
 		result = TRUE;
 		break;
 	case GDK_Up:
@@ -3089,3 +3091,99 @@
 
         return empty;
 }
+
+static gboolean
+eog_window_confirm_save_image (EogImage *image)
+{
+	GtkWidget *dialog;
+	gint response;
+	gboolean cancel = FALSE;
+
+	dialog = gtk_message_dialog_new (NULL,
+					 GTK_DIALOG_MODAL,
+					 GTK_MESSAGE_QUESTION,
+					 GTK_BUTTONS_NONE,
+					 _("Save the changes to image \"%s\" before closing?"),
+					 eog_image_get_caption (image));
+
+	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+						  _("The image %s has unsaved changes, if you close now they will be lost. "
+						    "Do you want to save the image before closing?"),
+						  eog_image_get_caption (image));
+	
+	gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+				_("Close without saving"), GTK_RESPONSE_NO,
+				GTK_STOCK_SAVE, GTK_RESPONSE_YES,
+				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
+
+	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
+
+	response = gtk_dialog_run (GTK_DIALOG (dialog));
+	switch (response) {
+	case GTK_RESPONSE_NO:
+		break;
+	case GTK_RESPONSE_YES:
+		g_print ("here we are supposed to save\n");
+		break;
+	case GTK_RESPONSE_CANCEL:
+		cancel = TRUE;
+		break;
+	}
+	gtk_widget_destroy (dialog);
+
+	return cancel;
+}
+
+static gboolean
+save_unsaved_images_foreach (EogListStore *store,
+			     EogImage *image,
+			     gpointer data)
+{
+	gboolean *user_cancelled = (gboolean*) data;
+	
+	if (eog_image_is_modified (image)) {
+		*user_cancelled = eog_window_confirm_save_image (image);
+	}
+	
+	return *user_cancelled;
+}
+
+static gboolean
+eog_window_save_unsaved_images (EogWindow *window)
+{
+	EogWindowPrivate *priv;
+	gboolean user_canceled = FALSE;
+	
+	priv = window->priv;
+
+	eog_list_store_foreach (priv->store,
+				save_unsaved_images_foreach,
+				&user_canceled);
+
+	return !user_canceled;
+}
+
+/**
+ * eog_window_confirm_close:
+ * @window: An #EogWindow.
+ * 
+ * Confirms whether the #EogWindow can be safely closed.
+ * 
+ * Returns: #TRUE if window can be safely closed, false otherwise.
+ **/
+
+gboolean
+eog_window_confirm_close (EogWindow *window)
+{
+	EogWindowPrivate *priv;
+
+	g_return_val_if_fail (EOG_IS_WINDOW (window), FALSE);
+	
+	priv = window->priv;
+	
+	if (priv->mode != EOG_WINDOW_MODE_NORMAL) {
+		return TRUE;
+	}
+
+	return eog_window_save_unsaved_images (window);
+}
Index: src/eog-window.h
===================================================================
--- src/eog-window.h	(revision 3478)
+++ src/eog-window.h	(working copy)
@@ -89,6 +89,8 @@
 
 gboolean     eog_window_is_empty 	(EogWindow       *window);
 
+gboolean     eog_window_confirm_close   (EogWindow *window);
+
 G_END_DECLS
 
 #endif
