1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Stuart Parmenter <pavlov@netscape.com>
25 : * Mike Pinkerton <pinkerton@netscape.com>
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsCOMPtr.h"
42 : #include "nsPIDOMWindow.h"
43 : #include "nsIDocShell.h"
44 : #include "nsIDocShellTreeItem.h"
45 : #include "nsIInterfaceRequestorUtils.h"
46 : #include "nsIBaseWindow.h"
47 : #include "nsIWidget.h"
48 :
49 : #include "nsIStringBundle.h"
50 : #include "nsXPIDLString.h"
51 : #include "nsIServiceManager.h"
52 : #include "nsCOMArray.h"
53 : #include "nsILocalFile.h"
54 : #include "nsEnumeratorUtils.h"
55 : #include "mozilla/Services.h"
56 : #include "WidgetUtils.h"
57 :
58 : #include "nsBaseFilePicker.h"
59 :
60 : using namespace mozilla::widget;
61 :
62 : #define FILEPICKER_TITLES "chrome://global/locale/filepicker.properties"
63 : #define FILEPICKER_FILTERS "chrome://global/content/filepicker.properties"
64 :
65 0 : nsBaseFilePicker::nsBaseFilePicker() :
66 0 : mAddToRecentDocs(true)
67 : {
68 :
69 0 : }
70 :
71 0 : nsBaseFilePicker::~nsBaseFilePicker()
72 : {
73 :
74 0 : }
75 :
76 0 : NS_IMETHODIMP nsBaseFilePicker::Init(nsIDOMWindow *aParent,
77 : const nsAString& aTitle,
78 : PRInt16 aMode)
79 : {
80 0 : NS_PRECONDITION(aParent, "Null parent passed to filepicker, no file "
81 : "picker for you!");
82 0 : nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
83 0 : NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
84 :
85 0 : InitNative(widget, aTitle, aMode);
86 :
87 0 : return NS_OK;
88 : }
89 :
90 :
91 : NS_IMETHODIMP
92 0 : nsBaseFilePicker::AppendFilters(PRInt32 aFilterMask)
93 : {
94 : nsCOMPtr<nsIStringBundleService> stringService =
95 0 : mozilla::services::GetStringBundleService();
96 0 : if (!stringService)
97 0 : return NS_ERROR_FAILURE;
98 :
99 0 : nsCOMPtr<nsIStringBundle> titleBundle, filterBundle;
100 :
101 0 : nsresult rv = stringService->CreateBundle(FILEPICKER_TITLES,
102 0 : getter_AddRefs(titleBundle));
103 0 : if (NS_FAILED(rv))
104 0 : return NS_ERROR_FAILURE;
105 :
106 0 : rv = stringService->CreateBundle(FILEPICKER_FILTERS, getter_AddRefs(filterBundle));
107 0 : if (NS_FAILED(rv))
108 0 : return NS_ERROR_FAILURE;
109 :
110 0 : nsXPIDLString title;
111 0 : nsXPIDLString filter;
112 :
113 0 : if (aFilterMask & filterAll) {
114 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("allTitle").get(), getter_Copies(title));
115 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("allFilter").get(), getter_Copies(filter));
116 0 : AppendFilter(title,filter);
117 : }
118 0 : if (aFilterMask & filterHTML) {
119 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("htmlTitle").get(), getter_Copies(title));
120 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("htmlFilter").get(), getter_Copies(filter));
121 0 : AppendFilter(title,filter);
122 : }
123 0 : if (aFilterMask & filterText) {
124 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("textTitle").get(), getter_Copies(title));
125 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("textFilter").get(), getter_Copies(filter));
126 0 : AppendFilter(title,filter);
127 : }
128 0 : if (aFilterMask & filterImages) {
129 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("imageTitle").get(), getter_Copies(title));
130 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("imageFilter").get(), getter_Copies(filter));
131 0 : AppendFilter(title,filter);
132 : }
133 0 : if (aFilterMask & filterAudio) {
134 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("audioTitle").get(), getter_Copies(title));
135 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("audioFilter").get(), getter_Copies(filter));
136 0 : AppendFilter(title,filter);
137 : }
138 0 : if (aFilterMask & filterVideo) {
139 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("videoTitle").get(), getter_Copies(title));
140 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("videoFilter").get(), getter_Copies(filter));
141 0 : AppendFilter(title,filter);
142 : }
143 0 : if (aFilterMask & filterXML) {
144 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("xmlTitle").get(), getter_Copies(title));
145 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("xmlFilter").get(), getter_Copies(filter));
146 0 : AppendFilter(title,filter);
147 : }
148 0 : if (aFilterMask & filterXUL) {
149 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("xulTitle").get(), getter_Copies(title));
150 0 : filterBundle->GetStringFromName(NS_LITERAL_STRING("xulFilter").get(), getter_Copies(filter));
151 0 : AppendFilter(title, filter);
152 : }
153 0 : if (aFilterMask & filterApps) {
154 0 : titleBundle->GetStringFromName(NS_LITERAL_STRING("appsTitle").get(), getter_Copies(title));
155 : // Pass the magic string "..apps" to the platform filepicker, which it
156 : // should recognize and do the correct platform behavior for.
157 0 : AppendFilter(title, NS_LITERAL_STRING("..apps"));
158 : }
159 0 : return NS_OK;
160 : }
161 :
162 : // Set the filter index
163 0 : NS_IMETHODIMP nsBaseFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
164 : {
165 0 : *aFilterIndex = 0;
166 0 : return NS_OK;
167 : }
168 :
169 0 : NS_IMETHODIMP nsBaseFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
170 : {
171 0 : return NS_OK;
172 : }
173 :
174 0 : NS_IMETHODIMP nsBaseFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
175 : {
176 0 : NS_ENSURE_ARG_POINTER(aFiles);
177 0 : nsCOMArray <nsILocalFile> files;
178 : nsresult rv;
179 :
180 : // if we get into the base class, the platform
181 : // doesn't implement GetFiles() yet.
182 : // so we fake it.
183 0 : nsCOMPtr <nsILocalFile> file;
184 0 : rv = GetFile(getter_AddRefs(file));
185 0 : NS_ENSURE_SUCCESS(rv,rv);
186 :
187 0 : rv = files.AppendObject(file);
188 0 : NS_ENSURE_SUCCESS(rv,rv);
189 :
190 0 : return NS_NewArrayEnumerator(aFiles, files);
191 : }
192 :
193 : #ifdef BASEFILEPICKER_HAS_DISPLAYDIRECTORY
194 :
195 : // Set the display directory
196 0 : NS_IMETHODIMP nsBaseFilePicker::SetDisplayDirectory(nsILocalFile *aDirectory)
197 : {
198 0 : if (!aDirectory) {
199 0 : mDisplayDirectory = nsnull;
200 0 : return NS_OK;
201 : }
202 0 : nsCOMPtr<nsIFile> directory;
203 0 : nsresult rv = aDirectory->Clone(getter_AddRefs(directory));
204 0 : if (NS_FAILED(rv))
205 0 : return rv;
206 0 : mDisplayDirectory = do_QueryInterface(directory, &rv);
207 0 : return rv;
208 : }
209 :
210 : // Get the display directory
211 0 : NS_IMETHODIMP nsBaseFilePicker::GetDisplayDirectory(nsILocalFile **aDirectory)
212 : {
213 0 : *aDirectory = nsnull;
214 0 : if (!mDisplayDirectory)
215 0 : return NS_OK;
216 0 : nsCOMPtr<nsIFile> directory;
217 0 : nsresult rv = mDisplayDirectory->Clone(getter_AddRefs(directory));
218 0 : if (NS_FAILED(rv))
219 0 : return rv;
220 0 : return CallQueryInterface(directory, aDirectory);
221 : }
222 : #endif
223 :
224 : NS_IMETHODIMP
225 0 : nsBaseFilePicker::GetAddToRecentDocs(bool *aFlag)
226 : {
227 0 : *aFlag = mAddToRecentDocs;
228 0 : return NS_OK;
229 : }
230 :
231 : NS_IMETHODIMP
232 0 : nsBaseFilePicker::SetAddToRecentDocs(bool aFlag)
233 : {
234 0 : mAddToRecentDocs = aFlag;
235 0 : return NS_OK;
236 : }
|