Index: embed/mozilla/ContentHandler.cpp
===================================================================
--- embed/mozilla/ContentHandler.cpp	(revisione 7334)
+++ embed/mozilla/ContentHandler.cpp	(copia locale)
@@ -137,13 +137,16 @@ NS_IMETHODIMP GContentHandler::PromptFor
 	int response;
 	char *filename = NULL;
 	nsCString defaultFile;
+	gboolean show_dialog = FALSE;
 
 	NS_UTF16ToCString (nsString (aDefaultFile),
 			   NS_CSTRING_ENCODING_UTF8, defaultFile);
 
 	if (mAction != CONTENT_ACTION_SAVEAS)
 	{
-		return BuildDownloadPath (defaultFile.get(), _retval);
+		nsresult rv;
+		rv = BuildDownloadPath (defaultFile.get(), _retval, &show_dialog);
+		if (show_dialog == FALSE) return rv;
 	}
 
 	nsresult rv;
Index: embed/mozilla/EphyHeaderSniffer.h
===================================================================
--- embed/mozilla/EphyHeaderSniffer.h	(revisione 7334)
+++ embed/mozilla/EphyHeaderSniffer.h	(copia locale)
@@ -77,6 +77,7 @@ public:
 protected:
 	nsresult PerformSave (nsIURI* inOriginalURI);
 	nsresult HandleContent ();
+	void ShowSaveDialog (const char* key, char* filename);
 
 private:
 	nsIWebBrowserPersist*      mPersist; /* Weak. It owns us as a listener. */
Index: embed/mozilla/MozDownload.h
===================================================================
--- embed/mozilla/MozDownload.h	(revisione 7334)
+++ embed/mozilla/MozDownload.h	(copia locale)
@@ -90,7 +90,7 @@ nsresult InitiateMozillaDownload (nsIDOM
 				  nsIURI* inOriginalURI, MozillaEmbedPersist *embedPersist,
 				  nsIInputStream *postData, nsISupports *aCacheKey,
 				  PRInt64 aMaxSize);
-nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval);
+nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval, gboolean *show_dialog);
 
 class MozDownload : public nsITransfer,
 		    public nsIInterfaceRequestor
Index: embed/mozilla/EphyHeaderSniffer.cpp
===================================================================
--- embed/mozilla/EphyHeaderSniffer.cpp	(revisione 7334)
+++ embed/mozilla/EphyHeaderSniffer.cpp	(copia locale)
@@ -278,6 +278,41 @@ filechooser_response_cb (GtkWidget *dial
 	gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
+void EphyHeaderSniffer::ShowSaveDialog (const char* key, char* filename)
+{
+	EphyFileChooser *dialog;
+	GtkWindow *window;
+	const char *title;
+	guint32 user_time;
+	
+	title = ephy_embed_persist_get_fc_title (EPHY_EMBED_PERSIST (mEmbedPersist));
+	window = ephy_embed_persist_get_fc_parent (EPHY_EMBED_PERSIST (mEmbedPersist));
+
+	user_time = ephy_embed_persist_get_user_time (EPHY_EMBED_PERSIST (mEmbedPersist));
+	if (user_time == 0)
+	{
+		g_warning ("EphyHeaderSniffer::PerformSave without valid user time!\n");
+	}
+	
+	dialog = ephy_file_chooser_new (title ? title: _("Save"),
+					GTK_WIDGET (window),
+					GTK_FILE_CHOOSER_ACTION_SAVE,
+					key ? key : CONF_STATE_SAVE_DIR,
+					EPHY_FILE_FILTER_ALL_SUPPORTED);
+	gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
+
+	gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
+                                       filename);
+
+	g_signal_connect (dialog, "response",
+				  G_CALLBACK (filechooser_response_cb), this);
+
+	ephy_gui_window_update_user_time (GTK_WIDGET (dialog), user_time);
+	gtk_widget_show (GTK_WIDGET (dialog));
+
+    g_free (filename);
+}
+
 nsresult EphyHeaderSniffer::PerformSave (nsIURI* inOriginalURI)
 {
 	nsresult rv;
@@ -391,47 +426,25 @@ nsresult EphyHeaderSniffer::PerformSave 
 
 	if (askDownloadDest)
 	{
-		EphyFileChooser *dialog;
-		GtkWindow *window;
-		const char *title;
-		guint32 user_time;
-
-		title = ephy_embed_persist_get_fc_title (EPHY_EMBED_PERSIST (mEmbedPersist));
-		window = ephy_embed_persist_get_fc_parent (EPHY_EMBED_PERSIST (mEmbedPersist));
-
-		user_time = ephy_embed_persist_get_user_time (EPHY_EMBED_PERSIST (mEmbedPersist));
-		if (user_time == 0)
-		{
-			g_warning ("EphyHeaderSniffer::PerformSave without valid user time!\n");
-		}
-
-		dialog = ephy_file_chooser_new (title ? title: _("Save"),
-						GTK_WIDGET (window),
-						GTK_FILE_CHOOSER_ACTION_SAVE,
-						key ? key : CONF_STATE_SAVE_DIR,
-						EPHY_FILE_FILTER_ALL_SUPPORTED);
-		gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
-
-		gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
-                                                   filename);
-
-		g_signal_connect (dialog, "response",
-				  G_CALLBACK (filechooser_response_cb), this);
-
-		ephy_gui_window_update_user_time (GTK_WIDGET (dialog), user_time);
-		gtk_widget_show (GTK_WIDGET (dialog));
-
-                g_free (filename);
+		ShowSaveDialog (key, filename);
 		return NS_OK;
 	}
 	/* FIXME: how to inform user of failed save ? */
 
 	nsCOMPtr<nsILocalFile> destFile;
-	BuildDownloadPath (filename, getter_AddRefs (destFile));
-        g_free (filename);
-	NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);
-
-	return InitiateDownload (destFile);
+	gboolean show_dialog = FALSE;
+	
+	BuildDownloadPath (filename, getter_AddRefs (destFile), &show_dialog);
+	if (show_dialog)
+	{
+		ShowSaveDialog (key, filename);
+	}
+	else 
+	{
+		g_free (filename);
+	}
+	
+	return NS_OK;
 }
 
 nsresult EphyHeaderSniffer::InitiateDownload (nsILocalFile *aDestFile)
Index: embed/mozilla/MozDownload.cpp
===================================================================
--- embed/mozilla/MozDownload.cpp	(revisione 7334)
+++ embed/mozilla/MozDownload.cpp	(copia locale)
@@ -47,6 +47,9 @@
 #include <stdlib.h>
 
 #include <glib/gi18n.h>
+#include <gtk/gtkdialog.h>
+#include <gtk/gtkmessagedialog.h>
+#include <gtk/gtkstock.h>
 
 #include <nsStringAPI.h>
 
@@ -85,6 +88,11 @@
 
 const char* const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
 
+enum
+{
+    MOZ_DOWNLOAD_OVERWRITE = 1
+};
+
 MozDownload::MozDownload() :
 	mTotalProgress(-1),
 	mCurrentProgress(0),
@@ -714,102 +722,73 @@ GetFilePath (const char *filename)
 	return path;
 }
 
-static const char*
-file_is_compressed (const char *filename)
+nsresult StartDownload (char *path, nsILocalFile **_retval)
 {
-        int i;
-        static const char * const compression[] = {".gz", ".bz2", ".Z", ".lz", NULL};
-
-        for (i = 0; compression[i] != NULL; i++)
-        {
-                if (g_str_has_suffix (filename, compression[i]))
-                        return compression[i];
-        }
+    nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
+   	NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);
 
-        return NULL;
+   	destFile->InitWithNativePath (nsCString (path));
+    g_free (path);
+    NS_IF_ADDREF (*_retval = destFile); 
+    
+    return NS_OK;
 }
 
-static const char*
-parse_extension (const char *filename)
+nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval, gboolean *show_dialog)
 {
-        const char *compression;
-
-        compression = file_is_compressed (filename);
+	char *path;
 
-        /* If the file is compressed we might have a double extension */
-        if (compression != NULL)
+	path = GetFilePath (defaultFileName);
+    
+    if (g_file_test (path, G_FILE_TEST_EXISTS))
+    {
+        GtkWidget *dialog;
+        int response;
+        
+        dialog = gtk_message_dialog_new
+            (NULL,
+             GTK_DIALOG_MODAL,
+             GTK_MESSAGE_QUESTION,
+             GTK_BUTTONS_NONE,
+             _("A file named \"%s\" already exists. "
+             "Do you want to replace it?"),
+             defaultFileName);
+        
+       	gtk_message_dialog_format_secondary_text
+            (GTK_MESSAGE_DIALOG (dialog),
+             _("The file already exists in \"%s\". "
+               "Replacing it will overwrite its contents."),
+             g_path_get_basename (ephy_file_get_downloads_dir ()));
+        
+        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                _("Overwrite"), MOZ_DOWNLOAD_OVERWRITE,
+                                GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT, NULL);
+        
+       	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
+        
+        response = gtk_dialog_run (GTK_DIALOG (dialog));
+        gtk_widget_destroy (dialog);
+        switch (response)
         {
-                int i;
-                static const char * const extensions[] = {"tar", "ps", "xcf", "dvi", "txt", "text", NULL};
-
-                for (i = 0; extensions[i] != NULL; i++)
+            case GTK_RESPONSE_ACCEPT:
                 {
-                        char *suffix;
-                        suffix = g_strdup_printf (".%s%s", extensions[i],
-                                                  compression);
-
-                        if (g_str_has_suffix (filename, suffix))
-                        {
-                                char *p;
-
-                                p = g_strrstr (filename, suffix);
-                                g_free (suffix);
-
-                                return p;
-                        }
-
-                        g_free (suffix);
+                    *show_dialog = TRUE;
+                    return NS_OK;
                 }
+                break;
+            case MOZ_DOWNLOAD_OVERWRITE:
+                return StartDownload (path, _retval);
+                break;
+            default:
+                return NS_ERROR_FAILURE;
+                break;
         }
-        
-        /* default case */
-        return g_strrstr (filename, ".");
-}
-
-nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval)
-{
-	char *path;
-
-	path = GetFilePath (defaultFileName);
-	
-	if (g_file_test (path, G_FILE_TEST_EXISTS))
-	{
-		int i = 1;
-		const char *dot_pos;
-                char *serial = NULL;
-		GString *tmp_path;
-		gssize position;
-
-                dot_pos = parse_extension (defaultFileName);
-		if (dot_pos)
-		{
-			position = dot_pos - defaultFileName;
-		}
-		else
-		{
-			position = strlen (defaultFileName);
-		}
-		tmp_path = g_string_new (NULL);
+    }   
+    else
+    {
+        return StartDownload (path, _retval);
+    }
 
-		do {
-			g_free (path);
-			g_string_assign (tmp_path, defaultFileName);
-			serial = g_strdup_printf ("(%d)", i++);
-			g_string_insert (tmp_path, position, serial);
-			g_free (serial);
-			path = GetFilePath (tmp_path->str);
-			
-		} while (g_file_test (path, G_FILE_TEST_EXISTS));
-		
-		g_string_free (tmp_path, TRUE);
-	}
-
-	nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
-	NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);
-
-	destFile->InitWithNativePath (nsCString (path));
-	g_free (path);
-
-	NS_IF_ADDREF (*_retval = destFile);
-	return NS_OK;
+    return NS_ERROR_UNEXPECTED;
 }
