Respect suggested name when downloading files

This commit is contained in:
Dan Williams 2007-03-14 13:15:39 -04:00
parent ca4c680f37
commit b35e6b6f2b

View File

@ -1,6 +1,10 @@
#include <nsCExternalHandlerService.h> #include <nsCExternalHandlerService.h>
#include <nsIFile.h> #include <nsIFile.h>
#include <nsIFactory.h> #include <nsIFactory.h>
#include <nsILocalFile.h>
#include <nsStringAPI.h>
#include <nsComponentManagerUtils.h>
#include "GeckoContentHandler.h" #include "GeckoContentHandler.h"
@ -30,15 +34,13 @@ NS_IMETHODIMP
GeckoContentHandler::Show (nsIHelperAppLauncher *aLauncher, GeckoContentHandler::Show (nsIHelperAppLauncher *aLauncher,
nsISupports *aContext, nsISupports *aContext,
PRUint32 aReason) PRUint32 aReason)
{ {
nsCOMPtr<nsIFile> tmpFile; aLauncher->SaveToDisk(NULL, PR_FALSE);
aLauncher->GetTargetFile(getter_AddRefs(tmpFile));
aLauncher->SaveToDisk (tmpFile, PR_FALSE);
return NS_OK; return NS_OK;
} }
#include <glib.h>
NS_IMETHODIMP NS_IMETHODIMP
GeckoContentHandler::PromptForSaveToFile (nsIHelperAppLauncher *aLauncher, GeckoContentHandler::PromptForSaveToFile (nsIHelperAppLauncher *aLauncher,
nsISupports *aWindowContext, nsISupports *aWindowContext,
@ -46,6 +48,28 @@ GeckoContentHandler::PromptForSaveToFile (nsIHelperAppLauncher *aLauncher,
const PRUnichar *aSuggestedFileExtension, const PRUnichar *aSuggestedFileExtension,
nsILocalFile **_retval) nsILocalFile **_retval)
{ {
char *filename = NULL;
nsCString defaultFile;
NS_UTF16ToCString(nsString(aDefaultFile), NS_CSTRING_ENCODING_UTF8, defaultFile);
nsCOMPtr <nsILocalFile> destFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
NS_ENSURE_TRUE(destFile, NS_ERROR_FAILURE);
const char * suggested = defaultFile.get();
if (strlen(suggested) > 0) {
filename = g_build_path("/", g_get_tmp_dir (), suggested, NULL);
} else {
filename = tempnam(NULL, NULL);
}
if (filename == NULL)
return NS_ERROR_OUT_OF_MEMORY;
destFile->InitWithNativePath(nsCString(filename));
g_free(filename);
NS_IF_ADDREF(*_retval = destFile);
return NS_OK; return NS_OK;
} }