1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation
20 : * Portions created by the Initial Developer are Copyright (C) 2004
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Lars Knoll <knoll@kde.org>
25 : * Zack Rusin <zack@kde.org>
26 : * Steffen Imhof <steffen.imhof@googlemail.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #include "mozilla/dom/ContentChild.h"
43 : #include "nsFilePickerProxy.h"
44 :
45 :
46 0 : NS_IMPL_ISUPPORTS1(nsFilePickerProxy, nsIFilePicker)
47 :
48 0 : nsFilePickerProxy::nsFilePickerProxy()
49 : {
50 0 : }
51 :
52 0 : nsFilePickerProxy::~nsFilePickerProxy()
53 : {
54 0 : }
55 :
56 : NS_IMETHODIMP
57 0 : nsFilePickerProxy::Init(nsIDOMWindow* /*aParent*/, const nsAString& aTitle,
58 : PRInt16 aMode)
59 : {
60 0 : mTitle = aTitle;
61 0 : mMode = aMode;
62 :
63 0 : return NS_OK;
64 : }
65 :
66 0 : void nsFilePickerProxy::InitNative(nsIWidget* aParent, const nsAString& aTitle,
67 : PRInt16 aMode)
68 : {
69 0 : }
70 :
71 :
72 : NS_IMETHODIMP
73 0 : nsFilePickerProxy::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
74 : {
75 0 : mFilters.AppendElement(aFilter);
76 0 : mFilterNames.AppendElement(aTitle);
77 0 : return NS_OK;
78 : }
79 :
80 : NS_IMETHODIMP
81 0 : nsFilePickerProxy::GetDefaultString(nsAString& aDefaultString)
82 : {
83 0 : aDefaultString = mDefault;
84 0 : return NS_OK;
85 : }
86 :
87 : NS_IMETHODIMP
88 0 : nsFilePickerProxy::SetDefaultString(const nsAString& aDefaultString)
89 : {
90 0 : mDefault = aDefaultString;
91 0 : return NS_OK;
92 : }
93 :
94 : NS_IMETHODIMP
95 0 : nsFilePickerProxy::GetDefaultExtension(nsAString& aDefaultExtension)
96 : {
97 0 : aDefaultExtension = mDefaultExtension;
98 0 : return NS_OK;
99 : }
100 :
101 : NS_IMETHODIMP
102 0 : nsFilePickerProxy::SetDefaultExtension(const nsAString& aDefaultExtension)
103 : {
104 0 : mDefaultExtension = aDefaultExtension;
105 0 : return NS_OK;
106 : }
107 :
108 : NS_IMETHODIMP
109 0 : nsFilePickerProxy::GetFilterIndex(PRInt32* aFilterIndex)
110 : {
111 0 : *aFilterIndex = mSelectedType;
112 0 : return NS_OK;
113 : }
114 :
115 : NS_IMETHODIMP
116 0 : nsFilePickerProxy::SetFilterIndex(PRInt32 aFilterIndex)
117 : {
118 0 : mSelectedType = aFilterIndex;
119 0 : return NS_OK;
120 : }
121 :
122 : /* readonly attribute nsILocalFile file; */
123 : NS_IMETHODIMP
124 0 : nsFilePickerProxy::GetFile(nsILocalFile** aFile)
125 : {
126 0 : NS_ENSURE_ARG_POINTER(aFile);
127 :
128 0 : *aFile = nsnull;
129 0 : if (mFile.IsEmpty()) {
130 0 : return NS_OK;
131 : }
132 :
133 0 : nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
134 0 : NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
135 :
136 0 : file->InitWithPath(mFile);
137 :
138 0 : file.forget(aFile);
139 :
140 0 : return NS_OK;
141 : }
142 :
143 : /* readonly attribute nsIFileURL fileURL; */
144 : NS_IMETHODIMP
145 0 : nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
146 : {
147 0 : nsCOMPtr<nsILocalFile> file;
148 0 : GetFile(getter_AddRefs(file));
149 :
150 0 : nsCOMPtr<nsIURI> uri;
151 0 : NS_NewFileURI(getter_AddRefs(uri), file);
152 0 : NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
153 :
154 0 : return CallQueryInterface(uri, aFileURL);
155 : }
156 :
157 : /* readonly attribute nsISimpleEnumerator files; */
158 : NS_IMETHODIMP
159 0 : nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
160 : {
161 0 : NS_ENSURE_ARG_POINTER(aFiles);
162 :
163 0 : if (mMode == nsIFilePicker::modeOpenMultiple) {
164 0 : return NS_NewArrayEnumerator(aFiles, mFiles);
165 : }
166 :
167 0 : return NS_ERROR_FAILURE;
168 : }
169 :
170 0 : NS_IMETHODIMP nsFilePickerProxy::Show(PRInt16* aReturn)
171 : {
172 0 : mozilla::dom::ContentChild *cc = mozilla::dom::ContentChild::GetSingleton();
173 0 : NS_ASSERTION(cc, "Content Protocol is NULL!");
174 :
175 0 : InfallibleTArray<nsString> filePaths;
176 :
177 : nsresult rv;
178 : cc->SendShowFilePicker(mMode, mSelectedType,
179 : mAddToRecentDocs, mTitle,
180 : mDefault, mDefaultExtension,
181 : mFilters, mFilterNames,
182 0 : &filePaths, aReturn, &rv);
183 :
184 0 : NS_ENSURE_SUCCESS(rv, rv);
185 :
186 0 : PRUint32 count = filePaths.Length();
187 :
188 0 : if (mMode == nsIFilePicker::modeOpenMultiple) {
189 0 : for (PRUint32 i = 0; i < count; ++i) {
190 0 : nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
191 0 : NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
192 :
193 0 : file->InitWithPath(filePaths[i]);
194 0 : mFiles.AppendObject(file);
195 : }
196 0 : return NS_OK;
197 : }
198 :
199 0 : NS_ASSERTION(count == 1 || count == 0, "we should only have 1 or 0 files");
200 :
201 0 : if (count == 1)
202 0 : mFile = filePaths[0];
203 :
204 0 : return NS_OK;
205 : }
|