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) 1999
21 * the Initial Developer. All Rights Reserved.
24 * Stuart Parmenter <pavlov@netscape.com>
25 * Mike Pinkerton <pinkerton@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 "nsPIDOMWindow.h"
43 #include "nsIDocShell.h"
44 #include "nsIDocShellTreeItem.h"
45 #include "nsIInterfaceRequestorUtils.h"
46 #include "nsIBaseWindow.h"
47 #include "nsIContentViewer.h"
48 #include "nsIDocumentViewer.h"
49 #include "nsIWidget.h"
51 #include "nsIStringBundle.h"
52 #include "nsXPIDLString.h"
53 #include "nsIServiceManager.h"
54 #include "nsISupportsArray.h"
55 #include "nsILocalFile.h"
56 #include "nsEnumeratorUtils.h"
58 #include "nsBaseFilePicker.h"
60 #define FILEPICKER_PROPERTIES "chrome://global/locale/filepicker.properties"
62 nsBaseFilePicker::nsBaseFilePicker()
67 nsBaseFilePicker::~nsBaseFilePicker()
72 // XXXdholbert -- this function is duplicated in nsPrintDialogGTK.cpp
73 // and needs to be unified in some generic utility class.
74 nsIWidget
*nsBaseFilePicker::DOMWindowToWidget(nsIDOMWindow
*dw
)
76 nsCOMPtr
<nsIWidget
> widget
;
78 nsCOMPtr
<nsPIDOMWindow
> window
= do_QueryInterface(dw
);
80 nsCOMPtr
<nsIBaseWindow
> baseWin(do_QueryInterface(window
->GetDocShell()));
82 while (!widget
&& baseWin
) {
83 baseWin
->GetParentWidget(getter_AddRefs(widget
));
85 nsCOMPtr
<nsIDocShellTreeItem
> docShellAsItem(do_QueryInterface(baseWin
));
89 nsCOMPtr
<nsIDocShellTreeItem
> parent
;
90 docShellAsItem
->GetSameTypeParent(getter_AddRefs(parent
));
92 window
= do_GetInterface(parent
);
96 baseWin
= do_QueryInterface(window
->GetDocShell());
101 // This will return a pointer that we're about to release, but
102 // that's ok since the docshell (nsIBaseWindow) holds the widget
107 //-------------------------------------------------------------------------
108 NS_IMETHODIMP
nsBaseFilePicker::Init(nsIDOMWindow
*aParent
,
109 const nsAString
& aTitle
,
112 NS_PRECONDITION(aParent
, "Null parent passed to filepicker, no file "
114 nsIWidget
*widget
= DOMWindowToWidget(aParent
);
115 NS_ENSURE_TRUE(widget
, NS_ERROR_FAILURE
);
117 InitNative(widget
, aTitle
, aMode
);
124 nsBaseFilePicker::AppendFilters(PRInt32 aFilterMask
)
127 nsCOMPtr
<nsIStringBundleService
> stringService
= do_GetService(NS_STRINGBUNDLE_CONTRACTID
, &rv
);
128 NS_ENSURE_SUCCESS(rv
, rv
);
130 nsCOMPtr
<nsIStringBundle
> stringBundle
;
132 rv
= stringService
->CreateBundle(FILEPICKER_PROPERTIES
, getter_AddRefs(stringBundle
));
134 return NS_ERROR_FAILURE
;
137 nsXPIDLString filter
;
139 if (aFilterMask
& filterAll
) {
140 stringBundle
->GetStringFromName(NS_LITERAL_STRING("allTitle").get(), getter_Copies(title
));
141 stringBundle
->GetStringFromName(NS_LITERAL_STRING("allFilter").get(), getter_Copies(filter
));
142 AppendFilter(title
,filter
);
144 if (aFilterMask
& filterHTML
) {
145 stringBundle
->GetStringFromName(NS_LITERAL_STRING("htmlTitle").get(), getter_Copies(title
));
146 stringBundle
->GetStringFromName(NS_LITERAL_STRING("htmlFilter").get(), getter_Copies(filter
));
147 AppendFilter(title
,filter
);
149 if (aFilterMask
& filterText
) {
150 stringBundle
->GetStringFromName(NS_LITERAL_STRING("textTitle").get(), getter_Copies(title
));
151 stringBundle
->GetStringFromName(NS_LITERAL_STRING("textFilter").get(), getter_Copies(filter
));
152 AppendFilter(title
,filter
);
154 if (aFilterMask
& filterImages
) {
155 stringBundle
->GetStringFromName(NS_LITERAL_STRING("imageTitle").get(), getter_Copies(title
));
156 stringBundle
->GetStringFromName(NS_LITERAL_STRING("imageFilter").get(), getter_Copies(filter
));
157 AppendFilter(title
,filter
);
159 if (aFilterMask
& filterXML
) {
160 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xmlTitle").get(), getter_Copies(title
));
161 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xmlFilter").get(), getter_Copies(filter
));
162 AppendFilter(title
,filter
);
164 if (aFilterMask
& filterXUL
) {
165 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xulTitle").get(), getter_Copies(title
));
166 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xulFilter").get(), getter_Copies(filter
));
167 AppendFilter(title
, filter
);
169 if (aFilterMask
& filterApps
) {
170 stringBundle
->GetStringFromName(NS_LITERAL_STRING("appsTitle").get(), getter_Copies(title
));
171 // Pass the magic string "..apps" to the platform filepicker, which it
172 // should recognize and do the correct platform behavior for.
173 AppendFilter(title
, NS_LITERAL_STRING("..apps"));
178 //-------------------------------------------------------------------------
180 // Set the filter index
182 //-------------------------------------------------------------------------
183 NS_IMETHODIMP
nsBaseFilePicker::GetFilterIndex(PRInt32
*aFilterIndex
)
189 NS_IMETHODIMP
nsBaseFilePicker::SetFilterIndex(PRInt32 aFilterIndex
)
194 NS_IMETHODIMP
nsBaseFilePicker::GetFiles(nsISimpleEnumerator
**aFiles
)
196 NS_ENSURE_ARG_POINTER(aFiles
);
197 nsCOMPtr
<nsISupportsArray
> files
;
198 nsresult rv
= NS_NewISupportsArray(getter_AddRefs(files
));
199 NS_ENSURE_SUCCESS(rv
,rv
);
201 // if we get into the base class, the platform
202 // doesn't implement GetFiles() yet.
204 nsCOMPtr
<nsILocalFile
> file
;
205 rv
= GetFile(getter_AddRefs(file
));
206 NS_ENSURE_SUCCESS(rv
,rv
);
208 rv
= files
->AppendElement(file
);
209 NS_ENSURE_SUCCESS(rv
,rv
);
211 return NS_NewArrayEnumerator(aFiles
, files
);
214 #ifdef BASEFILEPICKER_HAS_DISPLAYDIRECTORY
215 //-------------------------------------------------------------------------
217 // Set the display directory
219 //-------------------------------------------------------------------------
220 NS_IMETHODIMP
nsBaseFilePicker::SetDisplayDirectory(nsILocalFile
*aDirectory
)
223 mDisplayDirectory
= nsnull
;
226 nsCOMPtr
<nsIFile
> directory
;
227 nsresult rv
= aDirectory
->Clone(getter_AddRefs(directory
));
230 mDisplayDirectory
= do_QueryInterface(directory
, &rv
);
234 //-------------------------------------------------------------------------
236 // Get the display directory
238 //-------------------------------------------------------------------------
239 NS_IMETHODIMP
nsBaseFilePicker::GetDisplayDirectory(nsILocalFile
**aDirectory
)
241 *aDirectory
= nsnull
;
242 if (!mDisplayDirectory
)
244 nsCOMPtr
<nsIFile
> directory
;
245 nsresult rv
= mDisplayDirectory
->Clone(getter_AddRefs(directory
));
248 return CallQueryInterface(directory
, aDirectory
);