From 54584cc58048745aea75b99683e7985ea875e810 Mon Sep 17 00:00:00 2001 From: "timeless@mozdev.org" Date: Tue, 26 Feb 2008 08:12:17 -0800 Subject: [PATCH] Bug 412822 nsIFilePicker makes it hard to drop in other url systems r=neil a=dsicore --- embedding/browser/gtk/src/EmbedFilePicker.cpp | 24 ++++----- embedding/browser/gtk/src/EmbedFilePicker.h | 2 +- .../filepicker/content/filepicker.js | 29 ++++++++-- widget/public/nsIFilePicker.idl | 9 ++-- widget/src/beos/nsFilePicker.cpp | 20 +++---- widget/src/beos/nsFilePicker.h | 2 +- widget/src/cocoa/nsFilePicker.h | 2 +- widget/src/cocoa/nsFilePicker.mm | 13 +---- widget/src/gtk2/nsFilePicker.cpp | 53 +++++++++++-------- widget/src/gtk2/nsFilePicker.h | 5 +- widget/src/os2/nsFilePicker.cpp | 20 +++---- widget/src/os2/nsFilePicker.h | 2 +- widget/src/photon/nsFilePicker.cpp | 20 +++---- widget/src/photon/nsFilePicker.h | 2 +- widget/src/windows/nsFilePicker.cpp | 21 +++----- widget/src/windows/nsFilePicker.h | 2 +- .../filepicker/src/nsFilePicker.js.in | 18 ++++--- 17 files changed, 120 insertions(+), 124 deletions(-) diff --git a/embedding/browser/gtk/src/EmbedFilePicker.cpp b/embedding/browser/gtk/src/EmbedFilePicker.cpp index d51d484c4758..1f76f31a1f86 100644 --- a/embedding/browser/gtk/src/EmbedFilePicker.cpp +++ b/embedding/browser/gtk/src/EmbedFilePicker.cpp @@ -132,12 +132,11 @@ NS_IMETHODIMP EmbedFilePicker::GetFile(nsILocalFile **aFile) { NS_ENSURE_ARG_POINTER(aFile); - - if (mFilename.IsEmpty()) - return NS_OK; - + *aFile = nsnull; nsCOMPtr baseURI; - nsresult rv = NS_NewURI(getter_AddRefs(baseURI), mFilename); + nsresult rv = GetFileURL(getter_AddRefs(baseURI)); + if (!baseURI) + return rv; nsCOMPtr fileURL(do_QueryInterface(baseURI, &rv)); NS_ENSURE_SUCCESS(rv, rv); @@ -162,19 +161,14 @@ NS_IMETHODIMP EmbedFilePicker::GetFile(nsILocalFile **aFile) } /* readonly attribute nsIFileURL fileURL; */ -NS_IMETHODIMP EmbedFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP EmbedFilePicker::GetFileURL(nsIURI **aFileURL) { NS_ENSURE_ARG_POINTER(aFileURL); *aFileURL = nsnull; - - nsCOMPtr file; - GetFile(getter_AddRefs(file)); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); - nsCOMPtr fileURL = do_CreateInstance(NS_STANDARDURL_CONTRACTID); - NS_ENSURE_TRUE(fileURL, NS_ERROR_OUT_OF_MEMORY); - fileURL->SetFile(file); - NS_ADDREF(*aFileURL = fileURL); + if (mFileURI.IsEmpty()) return NS_OK; + + return NS_NewURI(aFileURL, mFileURI); } /* readonly attribute nsISimpleEnumerator files; */ @@ -204,7 +198,7 @@ NS_IMETHODIMP EmbedFilePicker::Show(PRInt16 *_retval) *_retval = response ? nsIFilePicker::returnOK : nsIFilePicker::returnCancel; - mFilename = retname; + mFileURI = retname; if (retname) NS_Free(retname); diff --git a/embedding/browser/gtk/src/EmbedFilePicker.h b/embedding/browser/gtk/src/EmbedFilePicker.h index 2d18d98b1aee..473bf883ec39 100644 --- a/embedding/browser/gtk/src/EmbedFilePicker.h +++ b/embedding/browser/gtk/src/EmbedFilePicker.h @@ -69,7 +69,7 @@ public: protected: nsIDOMWindow *mParent; PRInt16 mMode; - nsCString mFilename; + nsCString mFileURI; private: ~EmbedFilePicker(); diff --git a/toolkit/components/filepicker/content/filepicker.js b/toolkit/components/filepicker/content/filepicker.js index 9ce4f28c6f81..8353d5fe2daf 100644 --- a/toolkit/components/filepicker/content/filepicker.js +++ b/toolkit/components/filepicker/content/filepicker.js @@ -45,6 +45,7 @@ const nsIFilePicker = Components.interfaces.nsIFilePicker; const nsIProperties = Components.interfaces.nsIProperties; const NS_DIRECTORYSERVICE_CONTRACTID = "@mozilla.org/file/directory_service;1"; +const NS_IOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1"; const nsITreeBoxObject = Components.interfaces.nsITreeBoxObject; const nsIFileView = Components.interfaces.nsIFileView; const NS_FILEVIEW_CONTRACTID = "@mozilla.org/filepicker/fileview;1"; @@ -59,6 +60,7 @@ var retvals; var filePickerMode; var homeDir; var treeView; +var allowURLs; var textInput; var okButton; @@ -91,6 +93,7 @@ function filepickerLoad() { const numFilters = filterTitles.length; document.title = title; + allowURLs = o.allowURLs; if (initialText) { textInput.value = initialText; @@ -235,9 +238,28 @@ function selectOnOK() var isDir = false; var isFile = false; - var fileList = processPath(textInput.value); + retvals.filterIndex = document.getElementById("filterMenuList"); + retvals.fileURL = null; - if (!fileList) { // generic error message, should probably never happen + if (allowURLs) { + try { + var ios = Components.classes[NS_IOSERVICE_CONTRACTID].getService(Components.interfaces.nsIIOService); + retvals.fileURL = ios.newURI(textInput.value, '', null); + var fileList = []; + if (retvals.fileURL instanceof Components.interfaces.nsIFileURL) + fileList.push(retvals.fileURL.nsIFileURL.file); + gFilesEnumerator.mFiles = fileList; + retvals.files = gFilesEnumerator; + retvals.buttonStatus = ret; + + return true; + } catch (e) { + } + } + + var fileList = processPath(textInput.value); + if (!fileList) { + // generic error message, should probably never happen showErrorDialog("errorPathProblemTitle", "errorPathProblemMessage", textInput.value); @@ -377,9 +399,6 @@ function selectOnOK() retvals.files = gFilesEnumerator; retvals.buttonStatus = ret; - - var filterMenuList = document.getElementById("filterMenuList"); - retvals.filterIndex = filterMenuList.selectedIndex; return (ret != nsIFilePicker.returnCancel); } diff --git a/widget/public/nsIFilePicker.idl b/widget/public/nsIFilePicker.idl index 733a097a0453..4899165fc181 100644 --- a/widget/public/nsIFilePicker.idl +++ b/widget/public/nsIFilePicker.idl @@ -40,11 +40,11 @@ #include "nsISupports.idl" interface nsILocalFile; -interface nsIFileURL; +interface nsIURI; interface nsIDOMWindow; interface nsISimpleEnumerator; -[scriptable, uuid(80faf095-c807-4558-a2cc-185ed70754ea)] +[scriptable, uuid(d24ef0aa-d555-4117-84af-9cbbb7406909)] interface nsIFilePicker : nsISupports { const short modeOpen = 0; // Load a file or directory @@ -63,6 +63,7 @@ interface nsIFilePicker : nsISupports const long filterXML = 0x10; // *.xml const long filterXUL = 0x20; // *.xul const long filterApps = 0x40; // Applications (per-platform implementation) + const long filterAllowURLs = 0x80; // Allow URLs /** * Initialize the file picker widget. The file picker is not valid until this @@ -132,11 +133,11 @@ interface nsIFilePicker : nsISupports readonly attribute nsILocalFile file; /** - * Get the nsIFileURL for the file or directory. + * Get the nsIURL for the file or directory. * * @return Returns the file currently selected */ - readonly attribute nsIFileURL fileURL; + readonly attribute nsIURI fileURL; /** * Get the enumerator for the selected files diff --git a/widget/src/beos/nsFilePicker.cpp b/widget/src/beos/nsFilePicker.cpp index cc7d23447bc1..a2620b70f7b4 100644 --- a/widget/src/beos/nsFilePicker.cpp +++ b/widget/src/beos/nsFilePicker.cpp @@ -46,7 +46,6 @@ #include "nsFilePicker.h" #include "nsILocalFile.h" #include "nsIURL.h" -#include "nsIFileURL.h" #include "nsIStringBundle.h" #include "nsReadableUtils.h" #include "nsEscape.h" @@ -292,20 +291,15 @@ NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles) //------------------------------------------------------------------------- -NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL) { - nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); - file->InitWithNativePath(mFile); + *aFileURL = nsnull; + nsCOMPtr file; + nsresult rv = GetFile(getter_AddRefs(file)); + if (!file) + return rv; - nsCOMPtr uri; - NS_NewFileURI(getter_AddRefs(uri), file); - nsCOMPtr fileURL(do_QueryInterface(uri)); - NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE); - - NS_ADDREF(*aFileURL = fileURL); - - return NS_OK; + return NS_NewFileURI(aFileURL, file); } //------------------------------------------------------------------------- diff --git a/widget/src/beos/nsFilePicker.h b/widget/src/beos/nsFilePicker.h index 0bf1d06e9b59..8afc17a99b65 100644 --- a/widget/src/beos/nsFilePicker.h +++ b/widget/src/beos/nsFilePicker.h @@ -82,7 +82,7 @@ public: NS_IMETHOD GetDefaultExtension(nsAString& aDefaultExtension); NS_IMETHOD SetDefaultExtension(const nsAString& aDefaultExtension); NS_IMETHOD GetFile(nsILocalFile * *aFile); - NS_IMETHOD GetFileURL(nsIFileURL * *aFileURL); + NS_IMETHOD GetFileURL(nsIURI * *aFileURL); NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); NS_IMETHOD Show(PRInt16 *_retval); NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter); diff --git a/widget/src/cocoa/nsFilePicker.h b/widget/src/cocoa/nsFilePicker.h index e72305b03eb3..dd802cca808d 100644 --- a/widget/src/cocoa/nsFilePicker.h +++ b/widget/src/cocoa/nsFilePicker.h @@ -73,7 +73,7 @@ public: NS_IMETHOD SetFilterIndex(PRInt32 aFilterIndex); NS_IMETHOD SetDefaultExtension(const nsAString& aDefaultExtension); NS_IMETHOD GetFile(nsILocalFile * *aFile); - NS_IMETHOD GetFileURL(nsIFileURL * *aFileURL); + NS_IMETHOD GetFileURL(nsIURI * *aFileURL); NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); NS_IMETHOD Show(PRInt16 *_retval); NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter); diff --git a/widget/src/cocoa/nsFilePicker.mm b/widget/src/cocoa/nsFilePicker.mm index 7fce5ed5fb29..fe99439a918f 100644 --- a/widget/src/cocoa/nsFilePicker.mm +++ b/widget/src/cocoa/nsFilePicker.mm @@ -56,7 +56,6 @@ #include "nsILocalFile.h" #include "nsILocalFileMac.h" #include "nsIURL.h" -#include "nsIFileURL.h" #include "nsArrayEnumerator.h" #include "nsIStringBundle.h" @@ -540,7 +539,7 @@ NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile) } //------------------------------------------------------------------------- -NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL) { NS_ENSURE_ARG_POINTER(aFileURL); *aFileURL = nsnull; @@ -548,15 +547,7 @@ NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) if (mFiles.Count() == 0) return NS_OK; - nsCOMPtr uri; - nsresult rv = NS_NewFileURI(getter_AddRefs(uri), mFiles.ObjectAt(0)); - if (NS_FAILED(rv)) return rv; - - nsCOMPtr fileURL(do_QueryInterface(uri)); - NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE); - - NS_ADDREF(*aFileURL = fileURL); - return NS_OK; + return NS_NewFileURI(aFileURL, mFiles.ObjectAt(0)); } //------------------------------------------------------------------------- diff --git a/widget/src/gtk2/nsFilePicker.cpp b/widget/src/gtk2/nsFilePicker.cpp index bc115d94e847..c78be3b79232 100644 --- a/widget/src/gtk2/nsFilePicker.cpp +++ b/widget/src/gtk2/nsFilePicker.cpp @@ -88,6 +88,8 @@ typedef enum typedef gchar* (*_gtk_file_chooser_get_filename_fn)(GtkFileChooser *chooser); typedef GSList* (*_gtk_file_chooser_get_filenames_fn)(GtkFileChooser *chooser); +typedef gchar* (*_gtk_file_chooser_get_uri_fn)(GtkFileChooser *chooser); +typedef GSList* (*_gtk_file_chooser_get_uris_fn)(GtkFileChooser *chooser); typedef GtkWidget* (*_gtk_file_chooser_dialog_new_fn)(const gchar *title, GtkWindow *parent, GtkFileChooserAction action, @@ -110,10 +112,12 @@ typedef void (*_gtk_image_set_from_pixbuf_fn)(GtkImage *image, GdkPixbuf *pixbuf typedef void (*_gtk_file_chooser_set_preview_widget_fn)(GtkFileChooser *chooser, GtkWidget *preview_widget); typedef GtkWidget* (*_gtk_image_new_fn)(); typedef void (*_gtk_misc_set_padding_fn)(GtkMisc *misc, gint xpad, gint ypad); - +typedef void (*_gtk_file_chooser_set_local_only_fn)(GtkFileChooser *chooser, gboolean local_only); DECL_FUNC_PTR(gtk_file_chooser_get_filename); DECL_FUNC_PTR(gtk_file_chooser_get_filenames); +DECL_FUNC_PTR(gtk_file_chooser_get_uri); +DECL_FUNC_PTR(gtk_file_chooser_get_uris); DECL_FUNC_PTR(gtk_file_chooser_dialog_new); DECL_FUNC_PTR(gtk_file_chooser_set_select_multiple); DECL_FUNC_PTR(gtk_file_chooser_set_do_overwrite_confirmation); @@ -132,6 +136,7 @@ DECL_FUNC_PTR(gtk_image_set_from_pixbuf); DECL_FUNC_PTR(gtk_file_chooser_set_preview_widget); DECL_FUNC_PTR(gtk_image_new); DECL_FUNC_PTR(gtk_misc_set_padding); +DECL_FUNC_PTR(gtk_file_chooser_set_local_only); static GtkWindow * get_gtk_window_for_nsiwidget(nsIWidget *widget) @@ -204,6 +209,8 @@ nsFilePicker::LoadSymbolsGTK24() } GET_LIBGTK_FUNC(gtk_file_chooser_get_filenames); + GET_LIBGTK_FUNC(gtk_file_chooser_get_uri); + GET_LIBGTK_FUNC(gtk_file_chooser_get_uris); GET_LIBGTK_FUNC(gtk_file_chooser_dialog_new); GET_LIBGTK_FUNC(gtk_file_chooser_set_select_multiple); GET_LIBGTK_FUNC_OPT(gtk_file_chooser_set_do_overwrite_confirmation); @@ -222,6 +229,7 @@ nsFilePicker::LoadSymbolsGTK24() GET_LIBGTK_FUNC(gtk_file_chooser_set_preview_widget); GET_LIBGTK_FUNC(gtk_image_new); GET_LIBGTK_FUNC(gtk_misc_set_padding); + GET_LIBGTK_FUNC(gtk_file_chooser_set_local_only); initialized = PR_TRUE; @@ -340,17 +348,17 @@ ReadMultipleFiles(gpointer filename, gpointer array) void nsFilePicker::ReadValuesFromFileChooser(GtkWidget *file_chooser) { - mFiles.Clear(); + mFilesURL.Clear(); if (mMode == nsIFilePicker::modeOpenMultiple) { - mFile.Truncate(); + mFileURL.Truncate(); GSList *list = _gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(file_chooser)); g_slist_foreach(list, ReadMultipleFiles, static_cast(&mFiles)); g_slist_free(list); } else { - gchar *filename = _gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_chooser)); - mFile.Assign(filename); + gchar *filename = _gtk_file_chooser_get_uri (GTK_FILE_CHOOSER(file_chooser)); + mFileURL.Assign(filename); g_free(filename); } @@ -397,6 +405,7 @@ nsFilePicker::InitNative(nsIWidget *aParent, NS_IMETHODIMP nsFilePicker::AppendFilters(PRInt32 aFilterMask) { + mAllowURLs = !!(aFilterMask & filterAllowURLs); return nsBaseFilePicker::AppendFilters(aFilterMask); } @@ -471,31 +480,26 @@ nsFilePicker::GetFile(nsILocalFile **aFile) NS_ENSURE_ARG_POINTER(aFile); *aFile = nsnull; - if (mFile.IsEmpty()) { - return NS_OK; - } + nsCOMPtr uri; + nsresult rv = GetFileURL(getter_AddRefs(uri)); + if (!uri) + return rv; - nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); + nsCOMPtr fileURL(do_QueryInterface(uri, &rv)); + NS_ENSURE_SUCCESS(rv, rv); - file->InitWithNativePath(mFile); + nsCOMPtr file; + rv = fileURL->GetFile(getter_AddRefs(file)); + NS_ENSURE_SUCCESS(rv, rv); - NS_ADDREF(*aFile = file); - - return NS_OK; + return CallQueryInterface(file, aFile); } NS_IMETHODIMP -nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +nsFilePicker::GetFileURL(nsIURI **aFileURL) { - nsCOMPtr file; - GetFile(getter_AddRefs(file)); - - nsCOMPtr uri; - NS_NewFileURI(getter_AddRefs(uri), file); - NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); - - return CallQueryInterface(uri, aFileURL); + *aFileURL = nsnull; + return NS_NewFileURI(aFileURL, mFileURL); } NS_IMETHODIMP @@ -573,6 +577,9 @@ nsFilePicker::Show(PRInt16 *aReturn) GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, accept_button, GTK_RESPONSE_ACCEPT, NULL); + if (mAllowURLs) { + _gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_chooser), FALSE); + } if (mMode == GTK_FILE_CHOOSER_ACTION_OPEN || mMode == GTK_FILE_CHOOSER_ACTION_SAVE) { GtkWidget *img_preview = _gtk_image_new(); diff --git a/widget/src/gtk2/nsFilePicker.h b/widget/src/gtk2/nsFilePicker.h index d5d9e9350b29..3a7dd615fcc9 100644 --- a/widget/src/gtk2/nsFilePicker.h +++ b/widget/src/gtk2/nsFilePicker.h @@ -68,7 +68,7 @@ public: NS_IMETHODIMP GetFilterIndex(PRInt32 *aFilterIndex); NS_IMETHODIMP SetFilterIndex(PRInt32 aFilterIndex); NS_IMETHODIMP GetFile(nsILocalFile **aFile); - NS_IMETHODIMP GetFileURL(nsIFileURL **aFileURL); + NS_IMETHODIMP GetFileURL(nsIURI **aFileURL); NS_IMETHODIMP GetFiles(nsISimpleEnumerator **aFiles); NS_IMETHODIMP Show(PRInt16 *aReturn); @@ -86,7 +86,8 @@ protected: PRInt16 mMode; PRInt16 mSelectedType; - nsCString mFile; + PRBool mAllowURLs; + nsCString mFileURL; nsString mTitle; nsString mDefault; nsString mDefaultExtension; diff --git a/widget/src/os2/nsFilePicker.cpp b/widget/src/os2/nsFilePicker.cpp index 5cce4db134f5..963f0a87f6a3 100644 --- a/widget/src/os2/nsFilePicker.cpp +++ b/widget/src/os2/nsFilePicker.cpp @@ -51,7 +51,6 @@ #include "nsFilePicker.h" #include "nsILocalFile.h" #include "nsIURL.h" -#include "nsIFileURL.h" #include "nsIStringBundle.h" #include "nsEnumeratorUtils.h" #include "nsCRT.h" @@ -400,20 +399,15 @@ NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile) } //------------------------------------------------------------------------- -NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL) { - nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); - file->InitWithNativePath(mFile); + *aFileURL = nsnull; + nsCOMPtr file; + nsresult rv = GetFile(getter_AddRefs(file)); + if (!file) + return rv; - nsCOMPtr uri; - NS_NewFileURI(getter_AddRefs(uri), file); - nsCOMPtr fileURL(do_QueryInterface(uri)); - NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE); - - NS_ADDREF(*aFileURL = fileURL); - - return NS_OK; + return NS_NewFileURI(aFileURL, file); } NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles) diff --git a/widget/src/os2/nsFilePicker.h b/widget/src/os2/nsFilePicker.h index 011c34482c64..d903bd786745 100644 --- a/widget/src/os2/nsFilePicker.h +++ b/widget/src/os2/nsFilePicker.h @@ -70,7 +70,7 @@ public: NS_IMETHOD GetFilterIndex(PRInt32 *aFilterIndex); NS_IMETHOD SetFilterIndex(PRInt32 aFilterIndex); NS_IMETHOD GetFile(nsILocalFile * *aFile); - NS_IMETHOD GetFileURL(nsIFileURL * *aFileURL); + NS_IMETHOD GetFileURL(nsIURI * *aFileURL); NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); NS_IMETHOD Show(PRInt16 *_retval); NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter); diff --git a/widget/src/photon/nsFilePicker.cpp b/widget/src/photon/nsFilePicker.cpp index 019e0ed613e5..d0c22b75351a 100644 --- a/widget/src/photon/nsFilePicker.cpp +++ b/widget/src/photon/nsFilePicker.cpp @@ -47,7 +47,6 @@ #include "nsFilePicker.h" #include "nsILocalFile.h" #include "nsIURL.h" -#include "nsIFileURL.h" #include "nsIStringBundle.h" #include "nsEnumeratorUtils.h" #include "nsCRT.h" @@ -267,20 +266,15 @@ NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles) } //------------------------------------------------------------------------- -NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL) { - nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); - file->InitWithNativePath(mFile); + *aFileURL = nsnull; + nsCOMPtr file; + nsresult rv = GetFile(getter_AddRefs(file)); + if (!file) + return rv; - nsCOMPtr uri; - NS_NewFileURI(getter_AddRefs(uri), file); - nsCOMPtr fileURL(do_QueryInterface(uri)); - NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE); - - NS_ADDREF(*aFileURL = fileURL); - - return NS_OK; + return NS_NewFileURI(aFileURL, file); } //------------------------------------------------------------------------- diff --git a/widget/src/photon/nsFilePicker.h b/widget/src/photon/nsFilePicker.h index 430a120da47d..8109bb95d8dc 100644 --- a/widget/src/photon/nsFilePicker.h +++ b/widget/src/photon/nsFilePicker.h @@ -68,7 +68,7 @@ public: NS_IMETHOD GetFilterIndex(PRInt32 *aFilterIndex); NS_IMETHOD SetFilterIndex(PRInt32 aFilterIndex); NS_IMETHOD GetFile(nsILocalFile * *aFile); - NS_IMETHOD GetFileURL(nsIFileURL * *aFileURL); + NS_IMETHOD GetFileURL(nsIURI * *aFileURL); NS_IMETHOD Show(PRInt16 *_retval); NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter); NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); diff --git a/widget/src/windows/nsFilePicker.cpp b/widget/src/windows/nsFilePicker.cpp index 831e8e999296..bd68753e9778 100644 --- a/widget/src/windows/nsFilePicker.cpp +++ b/widget/src/windows/nsFilePicker.cpp @@ -49,7 +49,6 @@ #include "nsFilePicker.h" #include "nsILocalFile.h" #include "nsIURL.h" -#include "nsIFileURL.h" #include "nsIStringBundle.h" #include "nsEnumeratorUtils.h" #include "nsCRT.h" @@ -406,6 +405,7 @@ NS_IMETHODIMP nsFilePicker::Show(PRInt16 *aReturnVal) NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile) { NS_ENSURE_ARG_POINTER(aFile); + *aFile = nsnull; if (mUnicodeFile.IsEmpty()) return NS_OK; @@ -422,20 +422,15 @@ NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile) } //------------------------------------------------------------------------- -NS_IMETHODIMP nsFilePicker::GetFileURL(nsIFileURL **aFileURL) +NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL) { - nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); - NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); - file->InitWithPath(mUnicodeFile); + *aFileURL = nsnull; + nsCOMPtr file; + nsresult rv = GetFile(getter_AddRefs(file)); + if (!file) + return rv; - nsCOMPtr uri; - NS_NewFileURI(getter_AddRefs(uri), file); - nsCOMPtr fileURL(do_QueryInterface(uri)); - NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE); - - NS_ADDREF(*aFileURL = fileURL); - - return NS_OK; + return NS_NewFileURI(aFileURL, file); } NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles) diff --git a/widget/src/windows/nsFilePicker.h b/widget/src/windows/nsFilePicker.h index db9ab27d8549..0a2de90e38d9 100644 --- a/widget/src/windows/nsFilePicker.h +++ b/widget/src/windows/nsFilePicker.h @@ -70,7 +70,7 @@ public: NS_IMETHOD GetFilterIndex(PRInt32 *aFilterIndex); NS_IMETHOD SetFilterIndex(PRInt32 aFilterIndex); NS_IMETHOD GetFile(nsILocalFile * *aFile); - NS_IMETHOD GetFileURL(nsIFileURL * *aFileURL); + NS_IMETHOD GetFileURL(nsIURI * *aFileURL); NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); NS_IMETHOD Show(PRInt16 *aReturnVal); NS_IMETHOD ShowW(PRInt16 *aReturnVal); diff --git a/xpfe/components/filepicker/src/nsFilePicker.js.in b/xpfe/components/filepicker/src/nsFilePicker.js.in index 13f91da0ece6..721eeeb12fbb 100644 --- a/xpfe/components/filepicker/src/nsFilePicker.js.in +++ b/xpfe/components/filepicker/src/nsFilePicker.js.in @@ -116,16 +116,19 @@ nsFilePicker.prototype = { set files(a) { throw "readonly property"; }, get files() { return this.mFilesEnumerator; }, - /* readonly attribute nsIFileURL fileURL; */ + /* readonly attribute nsIURI fileURL; */ set fileURL(a) { throw "readonly property"; }, get fileURL() { - if (this.mFilesEnumerator) { + if (this.mFileURL) + return this.mFileURL; + + if (!this.mFilesEnumerator) + return null; + var ioService = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); - var url = ioService.newFileURI(this.file); - return url; - } - return null; + + return this.mFileURL = ioService.newFileURI(this.file); }, /* attribute wstring defaultString; */ @@ -172,6 +175,7 @@ nsFilePicker.prototype = { this.appendFilter(bundle.GetStringFromName("xulTitle"), bundle.GetStringFromName("xulFilter")); } + this.mAllowURLs = !!(filterMask & nsIFilePicker.filterAllowURLs); if (filterMask & nsIFilePicker.filterApps) { // We use "..apps" as a special filter for executable files this.appendFilter(bundle.GetStringFromName("appsTitle"), @@ -207,6 +211,7 @@ nsFilePicker.prototype = { o.filters = new Object(); o.filters.titles = this.mFilterTitles; o.filters.types = this.mFilters; + o.allowURLs = this.mAllowURLs; o.retvals = new Object(); var parent; @@ -247,6 +252,7 @@ nsFilePicker.prototype = { this.mFilterIndex = o.retvals.filterIndex; this.mFilesEnumerator = o.retvals.files; + this.mFileURL = o.retvals.fileURL; lastDirectory = o.retvals.directory; return o.retvals.buttonStatus; } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }