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
);
128 nsCOMPtr
<nsIStringBundle
> stringBundle
;
130 rv
= stringService
->CreateBundle(FILEPICKER_PROPERTIES
, getter_AddRefs(stringBundle
));
132 return NS_ERROR_FAILURE
;
135 nsXPIDLString filter
;
137 if (aFilterMask
& filterAll
) {
138 stringBundle
->GetStringFromName(NS_LITERAL_STRING("allTitle").get(), getter_Copies(title
));
139 stringBundle
->GetStringFromName(NS_LITERAL_STRING("allFilter").get(), getter_Copies(filter
));
140 AppendFilter(title
,filter
);
142 if (aFilterMask
& filterHTML
) {
143 stringBundle
->GetStringFromName(NS_LITERAL_STRING("htmlTitle").get(), getter_Copies(title
));
144 stringBundle
->GetStringFromName(NS_LITERAL_STRING("htmlFilter").get(), getter_Copies(filter
));
145 AppendFilter(title
,filter
);
147 if (aFilterMask
& filterText
) {
148 stringBundle
->GetStringFromName(NS_LITERAL_STRING("textTitle").get(), getter_Copies(title
));
149 stringBundle
->GetStringFromName(NS_LITERAL_STRING("textFilter").get(), getter_Copies(filter
));
150 AppendFilter(title
,filter
);
152 if (aFilterMask
& filterImages
) {
153 stringBundle
->GetStringFromName(NS_LITERAL_STRING("imageTitle").get(), getter_Copies(title
));
154 stringBundle
->GetStringFromName(NS_LITERAL_STRING("imageFilter").get(), getter_Copies(filter
));
155 AppendFilter(title
,filter
);
157 if (aFilterMask
& filterXML
) {
158 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xmlTitle").get(), getter_Copies(title
));
159 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xmlFilter").get(), getter_Copies(filter
));
160 AppendFilter(title
,filter
);
162 if (aFilterMask
& filterXUL
) {
163 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xulTitle").get(), getter_Copies(title
));
164 stringBundle
->GetStringFromName(NS_LITERAL_STRING("xulFilter").get(), getter_Copies(filter
));
165 AppendFilter(title
, filter
);
167 if (aFilterMask
& filterApps
) {
168 stringBundle
->GetStringFromName(NS_LITERAL_STRING("appsTitle").get(), getter_Copies(title
));
169 // Pass the magic string "..apps" to the platform filepicker, which it
170 // should recognize and do the correct platform behavior for.
171 AppendFilter(title
, NS_LITERAL_STRING("..apps"));
176 //-------------------------------------------------------------------------
178 // Set the filter index
180 //-------------------------------------------------------------------------
181 NS_IMETHODIMP
nsBaseFilePicker::GetFilterIndex(PRInt32
*aFilterIndex
)
187 NS_IMETHODIMP
nsBaseFilePicker::SetFilterIndex(PRInt32 aFilterIndex
)
192 NS_IMETHODIMP
nsBaseFilePicker::GetFiles(nsISimpleEnumerator
**aFiles
)
194 NS_ENSURE_ARG_POINTER(aFiles
);
195 nsCOMPtr
<nsISupportsArray
> files
;
196 nsresult rv
= NS_NewISupportsArray(getter_AddRefs(files
));
197 NS_ENSURE_SUCCESS(rv
,rv
);
199 // if we get into the base class, the platform
200 // doesn't implement GetFiles() yet.
202 nsCOMPtr
<nsILocalFile
> file
;
203 rv
= GetFile(getter_AddRefs(file
));
204 NS_ENSURE_SUCCESS(rv
,rv
);
206 rv
= files
->AppendElement(file
);
207 NS_ENSURE_SUCCESS(rv
,rv
);
209 return NS_NewArrayEnumerator(aFiles
, files
);
212 #ifdef BASEFILEPICKER_HAS_DISPLAYDIRECTORY
213 //-------------------------------------------------------------------------
215 // Set the display directory
217 //-------------------------------------------------------------------------
218 NS_IMETHODIMP
nsBaseFilePicker::SetDisplayDirectory(nsILocalFile
*aDirectory
)
221 mDisplayDirectory
= nsnull
;
224 nsCOMPtr
<nsIFile
> directory
;
225 nsresult rv
= aDirectory
->Clone(getter_AddRefs(directory
));
228 mDisplayDirectory
= do_QueryInterface(directory
, &rv
);
232 //-------------------------------------------------------------------------
234 // Get the display directory
236 //-------------------------------------------------------------------------
237 NS_IMETHODIMP
nsBaseFilePicker::GetDisplayDirectory(nsILocalFile
**aDirectory
)
239 *aDirectory
= nsnull
;
240 if (!mDisplayDirectory
)
242 nsCOMPtr
<nsIFile
> directory
;
243 nsresult rv
= mDisplayDirectory
->Clone(getter_AddRefs(directory
));
246 return CallQueryInterface(directory
, aDirectory
);