Bug 502516 - Cannot use file picker to browse for folders on Windows CE, r=vlad
[mozilla-central.git] / widget / src / windows / nsFilePicker.cpp
blobbabb4eb4fe0e2711a197b91f31b50003eb4d7ba8
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
14 * License.
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.
23 * Contributor(s):
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 ***** */
41 #include "nsCOMPtr.h"
42 #include "nsGUIEvent.h"
43 #include "nsReadableUtils.h"
44 #include "nsNetUtil.h"
45 #include "nsWindow.h"
46 #include "nsIServiceManager.h"
47 #include "nsIPlatformCharset.h"
48 #include "nsICharsetConverterManager.h"
49 #include "nsFilePicker.h"
50 #include "nsILocalFile.h"
51 #include "nsIURL.h"
52 #include "nsIStringBundle.h"
53 #include "nsEnumeratorUtils.h"
54 #include "nsCRT.h"
55 #include <windows.h>
56 #include <shlobj.h>
58 // commdlg.h and cderr.h are needed to build with WIN32_LEAN_AND_MEAN
59 #include <commdlg.h>
60 #ifndef WINCE
61 #include <cderr.h>
62 #endif
64 #include "nsString.h"
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
74 #ifndef BIF_USENEWUI
75 // BIF_USENEWUI isn't defined in the platform SDK that comes with
76 // MSVC6.0.
77 #define BIF_USENEWUI 0x50
78 #endif
80 //-------------------------------------------------------------------------
82 // nsFilePicker constructor
84 //-------------------------------------------------------------------------
85 nsFilePicker::nsFilePicker()
87 mSelectedType = 1;
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 //-------------------------------------------------------------------------
109 #ifndef WINCE_WINDOWS_MOBILE
110 int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
112 if (uMsg == BFFM_INITIALIZED)
114 PRUnichar * filePath = (PRUnichar *) lpData;
115 if (filePath)
116 ::SendMessageW(hwnd, BFFM_SETSELECTIONW,
117 TRUE /* true because lpData is a path string */,
118 lpData);
120 return 0;
122 #endif
124 NS_IMETHODIMP nsFilePicker::ShowW(PRInt16 *aReturnVal)
126 NS_ENSURE_ARG_POINTER(aReturnVal);
128 // suppress blur events
129 if (mParentWidget) {
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();
153 #ifndef WINCE_WINDOWS_MOBILE
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;
173 else
175 browserInfo.lParam = nsnull;
176 browserInfo.lpfn = nsnull;
178 browserInfo.iImage = nsnull;
180 LPITEMIDLIST list = ::SHBrowseForFolderW(&browserInfo);
181 if (list != NULL) {
182 result = ::SHGetPathFromIDListW(list, (LPWSTR)fileBuffer);
183 if (result) {
184 mUnicodeFile.Assign(fileBuffer);
187 // free PIDL
188 CoTaskMemFree(list);
191 else
192 #endif // WINCE_WINDOWS_MOBILE
195 OPENFILENAMEW ofn;
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();
217 else {
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) {
223 nsAutoString ext;
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();
240 #ifndef WINCE
241 try {
242 #endif
243 if (mMode == modeOpen) {
244 // FILE MUST EXIST!
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
259 ToLowerCase(ext);
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);
266 if (!result) {
267 // Error, find out what kind.
268 if (::GetLastError() == ERROR_INVALID_PARAMETER
269 #ifndef WINCE
270 || ::CommDlgExtendedError() == FNERR_INVALIDFILENAME
271 #endif
273 // probably the default file name is too long or contains illegal characters!
274 // Try again, without a starting file name.
275 ofn.lpstrFile[0] = 0;
276 result = ::GetSaveFileNameW(&ofn);
280 else {
281 NS_ASSERTION(0, "unsupported mode");
283 #ifndef WINCE
285 catch(...) {
286 MessageBoxW(ofn.hwndOwner,
288 L"The filepicker was unexpectedly closed by Windows.",
289 MB_ICONERROR);
290 result = PR_FALSE;
292 #endif
294 if (result == PR_TRUE) {
295 // Remember what filter type the user selected
296 mSelectedType = (PRInt16)ofn.nFilterIndex;
298 // Set user-selected location of file or directory
299 if (mMode == modeOpenMultiple) {
301 // from msdn.microsoft.com, "Open and Save As Dialog Boxes" section:
302 // If you specify OFN_EXPLORER,
303 // The directory and file name strings are NULL separated,
304 // with an extra NULL character after the last file name.
305 // This format enables the Explorer-style dialog boxes
306 // to return long file names that include spaces.
307 PRUnichar *current = fileBuffer;
309 nsAutoString dirName(current);
310 // sometimes dirName contains a trailing slash
311 // and sometimes it doesn't.
312 if (current[dirName.Length() - 1] != '\\')
313 dirName.Append((PRUnichar)'\\');
315 nsresult rv;
316 while (current && *current && *(current + nsCRT::strlen(current) + 1)) {
317 current = current + nsCRT::strlen(current) + 1;
319 nsCOMPtr<nsILocalFile> file = do_CreateInstance("@mozilla.org/file/local;1", &rv);
320 NS_ENSURE_SUCCESS(rv,rv);
322 rv = file->InitWithPath(dirName + nsDependentString(current));
323 NS_ENSURE_SUCCESS(rv,rv);
325 rv = mFiles.AppendObject(file);
326 NS_ENSURE_SUCCESS(rv,rv);
329 // handle the case where the user selected just one
330 // file. according to msdn.microsoft.com:
331 // If you specify OFN_ALLOWMULTISELECT and the user selects
332 // only one file, the lpstrFile string does not have
333 // a separator between the path and file name.
334 if (current && *current && (current == fileBuffer)) {
335 nsCOMPtr<nsILocalFile> file = do_CreateInstance("@mozilla.org/file/local;1", &rv);
336 NS_ENSURE_SUCCESS(rv,rv);
338 rv = file->InitWithPath(nsDependentString(current));
339 NS_ENSURE_SUCCESS(rv,rv);
341 rv = mFiles.AppendObject(file);
342 NS_ENSURE_SUCCESS(rv,rv);
345 else {
346 // I think it also needs a conversion here (to unicode since appending to nsString)
347 // but doing that generates garbage file name, weird.
348 mUnicodeFile.Assign(fileBuffer);
354 if (result) {
355 PRInt16 returnOKorReplace = returnOK;
357 // Remember last used directory.
358 nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
359 NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
361 // XXX InitWithPath() will convert UCS2 to FS path !!! corrupts unicode
362 file->InitWithPath(mUnicodeFile);
363 nsCOMPtr<nsIFile> dir;
364 if (NS_SUCCEEDED(file->GetParent(getter_AddRefs(dir)))) {
365 mDisplayDirectory = do_QueryInterface(dir);
366 if (mDisplayDirectory) {
367 if (mLastUsedUnicodeDirectory) {
368 NS_Free(mLastUsedUnicodeDirectory);
369 mLastUsedUnicodeDirectory = nsnull;
372 nsAutoString newDir;
373 mDisplayDirectory->GetPath(newDir);
374 if(!newDir.IsEmpty())
375 mLastUsedUnicodeDirectory = ToNewUnicode(newDir);
379 if (mMode == modeSave) {
380 // Windows does not return resultReplace,
381 // we must check if file already exists
382 PRBool exists = PR_FALSE;
383 file->Exists(&exists);
385 if (exists)
386 returnOKorReplace = returnReplace;
388 *aReturnVal = returnOKorReplace;
390 else {
391 *aReturnVal = returnCancel;
393 if (mParentWidget) {
394 nsIWidget *tmp = mParentWidget;
395 nsWindow *parent = static_cast<nsWindow *>(tmp);
396 parent->SuppressBlurEvents(PR_FALSE);
399 return NS_OK;
402 NS_IMETHODIMP nsFilePicker::Show(PRInt16 *aReturnVal)
404 return ShowW(aReturnVal);
407 NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile)
409 NS_ENSURE_ARG_POINTER(aFile);
410 *aFile = nsnull;
412 if (mUnicodeFile.IsEmpty())
413 return NS_OK;
415 nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
417 NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
419 file->InitWithPath(mUnicodeFile);
421 NS_ADDREF(*aFile = file);
423 return NS_OK;
426 //-------------------------------------------------------------------------
427 NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL)
429 *aFileURL = nsnull;
430 nsCOMPtr<nsILocalFile> file;
431 nsresult rv = GetFile(getter_AddRefs(file));
432 if (!file)
433 return rv;
435 return NS_NewFileURI(aFileURL, file);
438 NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
440 NS_ENSURE_ARG_POINTER(aFiles);
441 return NS_NewArrayEnumerator(aFiles, mFiles);
444 //-------------------------------------------------------------------------
446 // Get the file + path
448 //-------------------------------------------------------------------------
449 NS_IMETHODIMP nsFilePicker::SetDefaultString(const nsAString& aString)
451 mDefault = aString;
453 //First, make sure the file name is not too long!
454 PRInt32 nameLength;
455 PRInt32 nameIndex = mDefault.RFind("\\");
456 if (nameIndex == kNotFound)
457 nameIndex = 0;
458 else
459 nameIndex ++;
460 nameLength = mDefault.Length() - nameIndex;
462 if (nameLength > MAX_PATH) {
463 PRInt32 extIndex = mDefault.RFind(".");
464 if (extIndex == kNotFound)
465 extIndex = mDefault.Length();
467 //Let's try to shave the needed characters from the name part
468 PRInt32 charsToRemove = nameLength - MAX_PATH;
469 if (extIndex - nameIndex >= charsToRemove) {
470 mDefault.Cut(extIndex - charsToRemove, charsToRemove);
474 //Then, we need to replace illegal characters.
475 //At this stage, we cannot replace the backslash as the string might represent a file path.
476 mDefault.ReplaceChar(FILE_ILLEGAL_CHARACTERS, '-');
478 return NS_OK;
481 NS_IMETHODIMP nsFilePicker::GetDefaultString(nsAString& aString)
483 return NS_ERROR_FAILURE;
486 //-------------------------------------------------------------------------
488 // The default extension to use for files
490 //-------------------------------------------------------------------------
491 NS_IMETHODIMP nsFilePicker::GetDefaultExtension(nsAString& aExtension)
493 aExtension = mDefaultExtension;
494 return NS_OK;
497 NS_IMETHODIMP nsFilePicker::SetDefaultExtension(const nsAString& aExtension)
499 mDefaultExtension = aExtension;
500 return NS_OK;
503 //-------------------------------------------------------------------------
505 // Set the filter index
507 //-------------------------------------------------------------------------
508 NS_IMETHODIMP nsFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
510 // Windows' filter index is 1-based, we use a 0-based system.
511 *aFilterIndex = mSelectedType - 1;
512 return NS_OK;
515 NS_IMETHODIMP nsFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
517 // Windows' filter index is 1-based, we use a 0-based system.
518 mSelectedType = aFilterIndex + 1;
519 return NS_OK;
522 //-------------------------------------------------------------------------
523 void nsFilePicker::InitNative(nsIWidget *aParent,
524 const nsAString& aTitle,
525 PRInt16 aMode)
527 mParentWidget = aParent;
528 mTitle.Assign(aTitle);
529 mMode = aMode;
533 NS_IMETHODIMP
534 nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
536 mFilterList.Append(aTitle);
537 mFilterList.Append(PRUnichar('\0'));
539 if (aFilter.EqualsLiteral("..apps"))
540 mFilterList.AppendLiteral("*.exe;*.com");
541 else
543 nsAutoString filter(aFilter);
544 filter.StripWhitespace();
545 if (filter.EqualsLiteral("*"))
546 filter.AppendLiteral(".*");
547 mFilterList.Append(filter);
550 mFilterList.Append(PRUnichar('\0'));
552 return NS_OK;