1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2000
21 * the Initial Developer. All Rights Reserved.
24 * Stuart Parmenter <pavlov@netscape.com>
25 * Seth Spitzer <sspitzer@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 #include "nsGUIEvent.h"
43 #include "nsReadableUtils.h"
44 #include "nsNetUtil.h"
46 #include "nsIServiceManager.h"
47 #include "nsIPlatformCharset.h"
48 #include "nsICharsetConverterManager.h"
49 #include "nsFilePicker.h"
50 #include "nsILocalFile.h"
52 #include "nsIStringBundle.h"
53 #include "nsEnumeratorUtils.h"
58 // commdlg.h and cderr.h are needed to build with WIN32_LEAN_AND_MEAN
65 #include "nsToolkit.h"
67 NS_IMPL_ISUPPORTS1(nsFilePicker
, nsIFilePicker
)
69 PRUnichar
*nsFilePicker::mLastUsedUnicodeDirectory
;
70 char nsFilePicker::mLastUsedDirectory
[MAX_PATH
+1] = { 0 };
72 #define MAX_EXTENSION_LENGTH 10
75 // BIF_USENEWUI isn't defined in the platform SDK that comes with
77 #define BIF_USENEWUI 0x50
80 //-------------------------------------------------------------------------
82 // nsFilePicker constructor
84 //-------------------------------------------------------------------------
85 nsFilePicker::nsFilePicker()
90 //-------------------------------------------------------------------------
92 // nsFilePicker destructor
94 //-------------------------------------------------------------------------
95 nsFilePicker::~nsFilePicker()
97 if (mLastUsedUnicodeDirectory
) {
98 NS_Free(mLastUsedUnicodeDirectory
);
99 mLastUsedUnicodeDirectory
= nsnull
;
103 //-------------------------------------------------------------------------
105 // Show - Display the file dialog
107 //-------------------------------------------------------------------------
110 int CALLBACK
BrowseCallbackProc(HWND hwnd
, UINT uMsg
, LPARAM lParam
, LPARAM lpData
)
112 if (uMsg
== BFFM_INITIALIZED
)
114 PRUnichar
* filePath
= (PRUnichar
*) lpData
;
116 ::SendMessageW(hwnd
, BFFM_SETSELECTIONW
,
117 TRUE
/* true because lpData is a path string */,
124 NS_IMETHODIMP
nsFilePicker::ShowW(PRInt16
*aReturnVal
)
126 NS_ENSURE_ARG_POINTER(aReturnVal
);
128 // suppress blur events
130 nsIWidget
*tmp
= mParentWidget
;
131 nsWindow
*parent
= static_cast<nsWindow
*>(tmp
);
132 parent
->SuppressBlurEvents(PR_TRUE
);
135 PRBool result
= PR_FALSE
;
136 PRUnichar fileBuffer
[FILE_BUFFER_SIZE
+1];
137 wcsncpy(fileBuffer
, mDefault
.get(), FILE_BUFFER_SIZE
);
138 fileBuffer
[FILE_BUFFER_SIZE
] = '\0'; // null terminate in case copy truncated
140 NS_NAMED_LITERAL_STRING(htmExt
, "html");
141 nsAutoString initialDir
;
142 if (mDisplayDirectory
)
143 mDisplayDirectory
->GetPath(initialDir
);
145 // If no display directory, re-use the last one.
146 if(initialDir
.IsEmpty()) {
147 // Allocate copy of last used dir.
148 initialDir
= mLastUsedUnicodeDirectory
;
151 mUnicodeFile
.Truncate();
155 if (mMode
== modeGetFolder
) {
156 PRUnichar dirBuffer
[MAX_PATH
+1];
157 wcsncpy(dirBuffer
, initialDir
.get(), MAX_PATH
);
159 BROWSEINFOW browserInfo
;
160 browserInfo
.hwndOwner
= (HWND
)
161 (mParentWidget
.get() ? mParentWidget
->GetNativeData(NS_NATIVE_WINDOW
) : 0);
162 browserInfo
.pidlRoot
= nsnull
;
163 browserInfo
.pszDisplayName
= (LPWSTR
)dirBuffer
;
164 browserInfo
.lpszTitle
= mTitle
.get();
165 browserInfo
.ulFlags
= BIF_USENEWUI
| BIF_RETURNONLYFSDIRS
;
166 if (initialDir
.Length())
168 // the dialog is modal so that |initialDir.get()| will be valid in
169 // BrowserCallbackProc. Thus, we don't need to clone it.
170 browserInfo
.lParam
= (LPARAM
) initialDir
.get();
171 browserInfo
.lpfn
= &BrowseCallbackProc
;
175 browserInfo
.lParam
= nsnull
;
176 browserInfo
.lpfn
= nsnull
;
178 browserInfo
.iImage
= nsnull
;
180 LPITEMIDLIST list
= ::SHBrowseForFolderW(&browserInfo
);
182 result
= ::SHGetPathFromIDListW(list
, (LPWSTR
)fileBuffer
);
184 mUnicodeFile
.Assign(fileBuffer
);
196 memset(&ofn
, 0, sizeof(ofn
));
197 ofn
.lStructSize
= sizeof(ofn
);
198 nsString filterBuffer
= mFilterList
;
200 if (!initialDir
.IsEmpty()) {
201 ofn
.lpstrInitialDir
= initialDir
.get();
204 ofn
.lpstrTitle
= (LPCWSTR
)mTitle
.get();
205 ofn
.lpstrFilter
= (LPCWSTR
)filterBuffer
.get();
206 ofn
.nFilterIndex
= mSelectedType
;
207 ofn
.hwndOwner
= (HWND
)
208 (mParentWidget
.get() ? mParentWidget
->GetNativeData(NS_NATIVE_WINDOW
) : 0);
209 ofn
.lpstrFile
= fileBuffer
;
210 ofn
.nMaxFile
= FILE_BUFFER_SIZE
;
212 ofn
.Flags
= OFN_NOCHANGEDIR
| OFN_SHAREAWARE
| OFN_LONGNAMES
| OFN_OVERWRITEPROMPT
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
214 if (!mDefaultExtension
.IsEmpty()) {
215 ofn
.lpstrDefExt
= mDefaultExtension
.get();
218 // Get file extension from suggested filename
219 // to detect if we are saving an html file
220 //XXX: nsIFile SHOULD HAVE A GetExtension() METHOD!
221 PRInt32 extIndex
= mDefault
.RFind(".");
222 if ( extIndex
>= 0) {
224 mDefault
.Right(ext
, mDefault
.Length() - extIndex
);
225 // Should we test for ".cgi", ".asp", ".jsp" and other
226 // "generated" html pages?
228 if ( ext
.LowerCaseEqualsLiteral(".htm") ||
229 ext
.LowerCaseEqualsLiteral(".html") ||
230 ext
.LowerCaseEqualsLiteral(".shtml") ) {
231 // This is supposed to append ".htm" if user doesn't supply an extension
232 //XXX Actually, behavior is sort of weird:
233 // often appends ".html" even if you have an extension
234 // It obeys your extension if you put quotes around name
235 ofn
.lpstrDefExt
= htmExt
.get();
243 if (mMode
== modeOpen
) {
245 ofn
.Flags
|= OFN_FILEMUSTEXIST
;
246 result
= ::GetOpenFileNameW(&ofn
);
248 else if (mMode
== modeOpenMultiple
) {
249 ofn
.Flags
|= OFN_FILEMUSTEXIST
| OFN_ALLOWMULTISELECT
| OFN_EXPLORER
;
250 result
= ::GetOpenFileNameW(&ofn
);
252 else if (mMode
== modeSave
) {
253 ofn
.Flags
|= OFN_NOREADONLYRETURN
;
255 // Don't follow shortcuts when saving a shortcut, this can be used
256 // to trick users (bug 271732)
257 NS_ConvertUTF16toUTF8
ext(mDefault
);
258 ext
.Trim(" .", PR_FALSE
, PR_TRUE
); // watch out for trailing space and dots
260 if (StringEndsWith(ext
, NS_LITERAL_CSTRING(".lnk")) ||
261 StringEndsWith(ext
, NS_LITERAL_CSTRING(".pif")) ||
262 StringEndsWith(ext
, NS_LITERAL_CSTRING(".url")))
263 ofn
.Flags
|= OFN_NODEREFERENCELINKS
;
265 result
= ::GetSaveFileNameW(&ofn
);
267 // Error, find out what kind.
268 if (::GetLastError() == ERROR_INVALID_PARAMETER
||
269 ::CommDlgExtendedError() == FNERR_INVALIDFILENAME
) {
270 // probably the default file name is too long or contains illegal characters!
271 // Try again, without a starting file name.
272 ofn
.lpstrFile
[0] = 0;
273 result
= ::GetSaveFileNameW(&ofn
);
278 NS_ASSERTION(0, "unsupported mode");
283 MessageBoxW(ofn
.hwndOwner
,
285 L
"The filepicker was unexpectedly closed by Windows.",
291 if (result
== PR_TRUE
) {
292 // Remember what filter type the user selected
293 mSelectedType
= (PRInt16
)ofn
.nFilterIndex
;
295 // Set user-selected location of file or directory
296 if (mMode
== modeOpenMultiple
) {
297 nsresult rv
= NS_NewISupportsArray(getter_AddRefs(mFiles
));
298 NS_ENSURE_SUCCESS(rv
,rv
);
300 // from msdn.microsoft.com, "Open and Save As Dialog Boxes" section:
301 // If you specify OFN_EXPLORER,
302 // The directory and file name strings are NULL separated,
303 // with an extra NULL character after the last file name.
304 // This format enables the Explorer-style dialog boxes
305 // to return long file names that include spaces.
306 PRUnichar
*current
= fileBuffer
;
308 nsAutoString
dirName(current
);
309 // sometimes dirName contains a trailing slash
310 // and sometimes it doesn't.
311 if (current
[dirName
.Length() - 1] != '\\')
312 dirName
.Append((PRUnichar
)'\\');
314 while (current
&& *current
&& *(current
+ nsCRT::strlen(current
) + 1)) {
315 current
= current
+ nsCRT::strlen(current
) + 1;
317 nsCOMPtr
<nsILocalFile
> file
= do_CreateInstance("@mozilla.org/file/local;1", &rv
);
318 NS_ENSURE_SUCCESS(rv
,rv
);
320 rv
= file
->InitWithPath(dirName
+ nsDependentString(current
));
321 NS_ENSURE_SUCCESS(rv
,rv
);
323 rv
= mFiles
->AppendElement(file
);
324 NS_ENSURE_SUCCESS(rv
,rv
);
327 // handle the case where the user selected just one
328 // file. according to msdn.microsoft.com:
329 // If you specify OFN_ALLOWMULTISELECT and the user selects
330 // only one file, the lpstrFile string does not have
331 // a separator between the path and file name.
332 if (current
&& *current
&& (current
== fileBuffer
)) {
333 nsCOMPtr
<nsILocalFile
> file
= do_CreateInstance("@mozilla.org/file/local;1", &rv
);
334 NS_ENSURE_SUCCESS(rv
,rv
);
336 rv
= file
->InitWithPath(nsDependentString(current
));
337 NS_ENSURE_SUCCESS(rv
,rv
);
339 rv
= mFiles
->AppendElement(file
);
340 NS_ENSURE_SUCCESS(rv
,rv
);
344 // I think it also needs a conversion here (to unicode since appending to nsString)
345 // but doing that generates garbage file name, weird.
346 mUnicodeFile
.Assign(fileBuffer
);
353 PRInt16 returnOKorReplace
= returnOK
;
355 // Remember last used directory.
356 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance("@mozilla.org/file/local;1"));
357 NS_ENSURE_TRUE(file
, NS_ERROR_FAILURE
);
359 // XXX InitWithPath() will convert UCS2 to FS path !!! corrupts unicode
360 file
->InitWithPath(mUnicodeFile
);
361 nsCOMPtr
<nsIFile
> dir
;
362 if (NS_SUCCEEDED(file
->GetParent(getter_AddRefs(dir
)))) {
363 mDisplayDirectory
= do_QueryInterface(dir
);
364 if (mDisplayDirectory
) {
365 if (mLastUsedUnicodeDirectory
) {
366 NS_Free(mLastUsedUnicodeDirectory
);
367 mLastUsedUnicodeDirectory
= nsnull
;
371 mDisplayDirectory
->GetPath(newDir
);
372 if(!newDir
.IsEmpty())
373 mLastUsedUnicodeDirectory
= ToNewUnicode(newDir
);
377 if (mMode
== modeSave
) {
378 // Windows does not return resultReplace,
379 // we must check if file already exists
380 PRBool exists
= PR_FALSE
;
381 file
->Exists(&exists
);
384 returnOKorReplace
= returnReplace
;
386 *aReturnVal
= returnOKorReplace
;
389 *aReturnVal
= returnCancel
;
392 nsIWidget
*tmp
= mParentWidget
;
393 nsWindow
*parent
= static_cast<nsWindow
*>(tmp
);
394 parent
->SuppressBlurEvents(PR_FALSE
);
400 NS_IMETHODIMP
nsFilePicker::Show(PRInt16
*aReturnVal
)
402 return ShowW(aReturnVal
);
405 NS_IMETHODIMP
nsFilePicker::GetFile(nsILocalFile
**aFile
)
407 NS_ENSURE_ARG_POINTER(aFile
);
410 if (mUnicodeFile
.IsEmpty())
413 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance("@mozilla.org/file/local;1"));
415 NS_ENSURE_TRUE(file
, NS_ERROR_FAILURE
);
417 file
->InitWithPath(mUnicodeFile
);
419 NS_ADDREF(*aFile
= file
);
424 //-------------------------------------------------------------------------
425 NS_IMETHODIMP
nsFilePicker::GetFileURL(nsIURI
**aFileURL
)
428 nsCOMPtr
<nsILocalFile
> file
;
429 nsresult rv
= GetFile(getter_AddRefs(file
));
433 return NS_NewFileURI(aFileURL
, file
);
436 NS_IMETHODIMP
nsFilePicker::GetFiles(nsISimpleEnumerator
**aFiles
)
438 NS_ENSURE_ARG_POINTER(aFiles
);
439 return NS_NewArrayEnumerator(aFiles
, mFiles
);
442 //-------------------------------------------------------------------------
444 // Get the file + path
446 //-------------------------------------------------------------------------
447 NS_IMETHODIMP
nsFilePicker::SetDefaultString(const nsAString
& aString
)
451 //First, make sure the file name is not too long!
453 PRInt32 nameIndex
= mDefault
.RFind("\\");
454 if (nameIndex
== kNotFound
)
458 nameLength
= mDefault
.Length() - nameIndex
;
460 if (nameLength
> _MAX_FNAME
) {
461 PRInt32 extIndex
= mDefault
.RFind(".");
462 if (extIndex
== kNotFound
)
463 extIndex
= mDefault
.Length();
465 //Let's try to shave the needed characters from the name part
466 PRInt32 charsToRemove
= nameLength
- _MAX_FNAME
;
467 if (extIndex
- nameIndex
>= charsToRemove
) {
468 mDefault
.Cut(extIndex
- charsToRemove
, charsToRemove
);
472 //Then, we need to replace illegal characters.
473 //At this stage, we cannot replace the backslash as the string might represent a file path.
474 mDefault
.ReplaceChar(FILE_ILLEGAL_CHARACTERS
, '-');
479 NS_IMETHODIMP
nsFilePicker::GetDefaultString(nsAString
& aString
)
481 return NS_ERROR_FAILURE
;
484 //-------------------------------------------------------------------------
486 // The default extension to use for files
488 //-------------------------------------------------------------------------
489 NS_IMETHODIMP
nsFilePicker::GetDefaultExtension(nsAString
& aExtension
)
491 aExtension
= mDefaultExtension
;
495 NS_IMETHODIMP
nsFilePicker::SetDefaultExtension(const nsAString
& aExtension
)
497 mDefaultExtension
= aExtension
;
501 //-------------------------------------------------------------------------
503 // Set the filter index
505 //-------------------------------------------------------------------------
506 NS_IMETHODIMP
nsFilePicker::GetFilterIndex(PRInt32
*aFilterIndex
)
508 // Windows' filter index is 1-based, we use a 0-based system.
509 *aFilterIndex
= mSelectedType
- 1;
513 NS_IMETHODIMP
nsFilePicker::SetFilterIndex(PRInt32 aFilterIndex
)
515 // Windows' filter index is 1-based, we use a 0-based system.
516 mSelectedType
= aFilterIndex
+ 1;
520 //-------------------------------------------------------------------------
521 void nsFilePicker::InitNative(nsIWidget
*aParent
,
522 const nsAString
& aTitle
,
525 mParentWidget
= aParent
;
526 mTitle
.Assign(aTitle
);
532 nsFilePicker::AppendFilter(const nsAString
& aTitle
, const nsAString
& aFilter
)
534 mFilterList
.Append(aTitle
);
535 mFilterList
.Append(PRUnichar('\0'));
537 if (aFilter
.EqualsLiteral("..apps"))
538 mFilterList
.AppendLiteral("*.exe;*.com");
541 nsAutoString
filter(aFilter
);
542 filter
.StripWhitespace();
543 if (filter
.EqualsLiteral("*"))
544 filter
.AppendLiteral(".*");
545 mFilterList
.Append(filter
);
548 mFilterList
.Append(PRUnichar('\0'));