~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpfe/components/filepicker/src/nsFilePicker.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is mozilla.org code.
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are
 
17
 * Copyright (C) 2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): Stuart Parmenter <pavlov@netscape.com>
 
21
 */
 
22
 
 
23
/*
 
24
 * No magic constructor behaviour, as is de rigeur for XPCOM.
 
25
 * If you must perform some initialization, and it could possibly fail (even
 
26
 * due to an out-of-memory condition), you should use an Init method, which
 
27
 * can convey failure appropriately (thrown exception in JS,
 
28
 * NS_FAILED(nsresult) return in C++).
 
29
 *
 
30
 * In JS, you can actually cheat, because a thrown exception will cause the
 
31
 * CreateInstance call to fail in turn, but not all languages are so lucky.
 
32
 * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
 
33
 * for portability reasons -- and even when you're building completely
 
34
 * platform-specific code, you can't throw across an XPCOM method boundary.)
 
35
 */
 
36
 
 
37
 
 
38
const DEBUG = false; /* set to true to enable debug messages */
 
39
 
 
40
const FILEPICKER_CONTRACTID     = "@mozilla.org/filepicker;1";
 
41
const FILEPICKER_CID        = Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}");
 
42
const LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
 
43
const APPSHELL_SERV_CONTRACTID  = "@mozilla.org/appshell/appShellService;1";
 
44
const STRBUNDLE_SERV_CONTRACTID = "@mozilla.org/intl/stringbundle;1";
 
45
 
 
46
const nsIAppShellService    = Components.interfaces.nsIAppShellService;
 
47
const nsILocalFile          = Components.interfaces.nsILocalFile;
 
48
const nsIFileURL            = Components.interfaces.nsIFileURL;
 
49
const nsISupports           = Components.interfaces.nsISupports;
 
50
const nsIFactory            = Components.interfaces.nsIFactory;
 
51
const nsIFilePicker         = Components.interfaces.nsIFilePicker;
 
52
const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor
 
53
const nsIDOMWindow          = Components.interfaces.nsIDOMWindow;
 
54
const nsIStringBundleService = Components.interfaces.nsIStringBundleService;
 
55
const nsIWebNavigation      = Components.interfaces.nsIWebNavigation;
 
56
const nsIDocShellTreeItem   = Components.interfaces.nsIDocShellTreeItem;
 
57
const nsIBaseWindow         = Components.interfaces.nsIBaseWindow;
 
58
 
 
59
var   bundle                = null;
 
60
var   lastDirectory         = null;
 
61
 
 
62
function nsFilePicker()
 
63
{
 
64
  if (!bundle)
 
65
    bundle = srGetStrBundle("chrome://global/locale/filepicker.properties");
 
66
 
 
67
  /* attributes */
 
68
  this.mDefaultString = "";
 
69
  this.mFilterIndex = 0;
 
70
  if (lastDirectory) {
 
71
    this.mDisplayDirectory = Components.classes[LOCAL_FILE_CONTRACTID].createInstance(nsILocalFile);
 
72
    this.mDisplayDirectory.initWithPath(lastDirectory);
 
73
  } else {
 
74
    this.mDisplayDirectory = null;
 
75
  }
 
76
  this.mFilterTitles = new Array();
 
77
  this.mFilters = new Array();
 
78
}
 
79
 
 
80
nsFilePicker.prototype = {
 
81
 
 
82
  /* attribute nsILocalFile displayDirectory; */
 
83
  set displayDirectory(a) { this.mDisplayDirectory = a; },
 
84
  get displayDirectory()  { return this.mDisplayDirectory; },
 
85
 
 
86
  /* readonly attribute nsILocalFile file; */
 
87
  set file(a) { throw "readonly property"; },
 
88
  get file()  { return this.mFilesEnumerator.mFiles[0]; },
 
89
 
 
90
  /* readonly attribute nsISimpleEnumerator files; */
 
91
  set files(a) { throw "readonly property"; },
 
92
  get files()  { return this.mFilesEnumerator; },
 
93
 
 
94
  /* readonly attribute nsIFileURL fileURL; */
 
95
  set fileURL(a) { throw "readonly property"; },
 
96
  get fileURL()  { 
 
97
    if (this.mFilesEnumerator) {
 
98
      var ioService = Components.classes["@mozilla.org/network/io-service;1"]
 
99
                    .getService(Components.interfaces.nsIIOService);
 
100
      var url       = ioService.newFileURI(this.file);
 
101
      return url;
 
102
    }
 
103
    return null;
 
104
  },
 
105
 
 
106
  /* attribute wstring defaultString; */
 
107
  set defaultString(a) { this.mDefaultString = a; },
 
108
  get defaultString()  { return this.mDefaultString; },
 
109
 
 
110
  /* attribute wstring defaultExtension */
 
111
  set defaultExtension(ext) { },
 
112
  get defaultExtension() { return ""; },
 
113
  
 
114
  /* attribute long filterIndex; */
 
115
  set filterIndex(a) { this.mFilterIndex = a; },
 
116
  get filterIndex() { return this.mFilterIndex; },
 
117
 
 
118
  /* members */
 
119
  mFilesEnumerator: undefined,
 
120
  mParentWindow: null,
 
121
 
 
122
  /* methods */
 
123
  init: function(parent, title, mode) {
 
124
    this.mParentWindow = parent;
 
125
    this.mTitle = title;
 
126
    this.mMode = mode;
 
127
  },
 
128
 
 
129
  appendFilters: function(filterMask) {
 
130
    if (filterMask & nsIFilePicker.filterHTML) {
 
131
      this.appendFilter(bundle.GetStringFromName("htmlTitle"),
 
132
                   bundle.GetStringFromName("htmlFilter"));
 
133
    }
 
134
    if (filterMask & nsIFilePicker.filterText) {
 
135
      this.appendFilter(bundle.GetStringFromName("textTitle"),
 
136
                   bundle.GetStringFromName("textFilter"));
 
137
    }
 
138
    if (filterMask & nsIFilePicker.filterImages) {
 
139
      this.appendFilter(bundle.GetStringFromName("imageTitle"),
 
140
                   bundle.GetStringFromName("imageFilter"));
 
141
    }
 
142
    if (filterMask & nsIFilePicker.filterXML) {
 
143
      this.appendFilter(bundle.GetStringFromName("xmlTitle"),
 
144
                   bundle.GetStringFromName("xmlFilter"));
 
145
    }
 
146
    if (filterMask & nsIFilePicker.filterXUL) {
 
147
      this.appendFilter(bundle.GetStringFromName("xulTitle"),
 
148
                   bundle.GetStringFromName("xulFilter"));
 
149
    }
 
150
    if (filterMask & nsIFilePicker.filterApps) {
 
151
      // We use "..apps" as a special filter for executable files
 
152
      this.appendFilter(bundle.GetStringFromName("appsTitle"),
 
153
                        "..apps");
 
154
    }
 
155
    if (filterMask & nsIFilePicker.filterAll) {
 
156
      this.appendFilter(bundle.GetStringFromName("allTitle"),
 
157
                   bundle.GetStringFromName("allFilter"));
 
158
    }
 
159
  },
 
160
 
 
161
  appendFilter: function(title, extensions) {
 
162
    this.mFilterTitles.push(title);
 
163
    this.mFilters.push(extensions);
 
164
  },
 
165
 
 
166
  QueryInterface: function(iid) {
 
167
    if (!iid.equals(nsIFilePicker) &&
 
168
        !iid.equals(nsISupports))
 
169
        throw Components.results.NS_ERROR_NO_INTERFACE;
 
170
    return this;
 
171
  },
 
172
 
 
173
  show: function() {
 
174
    var o = new Object();
 
175
    o.title = this.mTitle;
 
176
    o.mode = this.mMode;
 
177
    o.displayDirectory = this.mDisplayDirectory;
 
178
    o.defaultString = this.mDefaultString;
 
179
    o.filterIndex = this.mFilterIndex;
 
180
    o.filters = new Object();
 
181
    o.filters.titles = this.mFilterTitles;
 
182
    o.filters.types = this.mFilters;
 
183
    o.retvals = new Object();
 
184
 
 
185
    var parent;
 
186
    if (this.mParentWindow) {
 
187
      parent = this.mParentWindow;
 
188
    } else if (typeof(window) == "object" && window != null) {
 
189
      parent = window;
 
190
    } else {
 
191
      try {
 
192
        var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService);
 
193
        parent = appShellService.hiddenDOMWindow;
 
194
      } catch(ex) {
 
195
        debug("Can't get parent.  xpconnect hates me so we can't get one from the appShellService.\n");
 
196
        debug(ex + "\n");
 
197
      }
 
198
    }
 
199
 
 
200
    var parentWin = null;
 
201
    try {
 
202
      parentWin = parent.QueryInterface(nsIInterfaceRequestor)
 
203
                        .getInterface(nsIWebNavigation)
 
204
                        .QueryInterface(nsIDocShellTreeItem)
 
205
                        .treeOwner
 
206
                        .QueryInterface(nsIInterfaceRequestor)
 
207
                        .getInterface(nsIBaseWindow);
 
208
    } catch(ex) {
 
209
      dump("file picker couldn't get base window\n"+ex+"\n");
 
210
    }
 
211
    try {
 
212
      if (parentWin)
 
213
        parentWin.blurSuppression = true;
 
214
      parent.openDialog("chrome://global/content/filepicker.xul",
 
215
                        "",
 
216
                        "chrome,modal,titlebar,resizable=yes,dependent=yes",
 
217
                        o);
 
218
      if (parentWin)
 
219
        parentWin.blurSuppression = false;
 
220
 
 
221
      this.mFilterIndex = o.retvals.filterIndex;
 
222
      this.mFilesEnumerator = o.retvals.files;
 
223
      lastDirectory = o.retvals.directory;
 
224
      return o.retvals.buttonStatus;
 
225
    } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }
 
226
 
 
227
    return null;
 
228
  }
 
229
}
 
230
 
 
231
if (DEBUG)
 
232
    debug = function (s) { dump("-*- filepicker: " + s + "\n"); }
 
233
else
 
234
    debug = function (s) {}
 
235
 
 
236
/* module foo */
 
237
 
 
238
var filePickerModule = new Object();
 
239
 
 
240
filePickerModule.registerSelf =
 
241
function (compMgr, fileSpec, location, type)
 
242
{
 
243
    debug("registering (all right -- a JavaScript module!)");
 
244
    compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
 
245
 
 
246
    compMgr.registerFactoryLocation(FILEPICKER_CID, 
 
247
                                    "FilePicker JS Component",
 
248
                                    "",
 
249
                                    fileSpec, 
 
250
                                    location,
 
251
                                    type);
 
252
}
 
253
 
 
254
filePickerModule.getClassObject =
 
255
function (compMgr, cid, iid) {
 
256
    if (!cid.equals(FILEPICKER_CID))
 
257
        throw Components.results.NS_ERROR_NO_INTERFACE;
 
258
    
 
259
    if (!iid.equals(Components.interfaces.nsIFactory))
 
260
        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
 
261
    
 
262
    return filePickerFactory;
 
263
}
 
264
 
 
265
filePickerModule.canUnload =
 
266
function(compMgr)
 
267
{
 
268
    debug("Unloading component.");
 
269
    return true;
 
270
}
 
271
    
 
272
/* factory object */
 
273
var filePickerFactory = new Object();
 
274
 
 
275
filePickerFactory.createInstance =
 
276
function (outer, iid) {
 
277
    debug("CI: " + iid);
 
278
    debug("IID:" + nsIFilePicker);
 
279
    if (outer != null)
 
280
        throw Components.results.NS_ERROR_NO_AGGREGATION;
 
281
 
 
282
    return (new nsFilePicker()).QueryInterface(iid);
 
283
}
 
284
 
 
285
/* entrypoint */
 
286
function NSGetModule(compMgr, fileSpec) {
 
287
    return filePickerModule;
 
288
}
 
289
 
 
290
 
 
291
 
 
292
/* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */
 
293
 
 
294
var strBundleService = null;
 
295
 
 
296
function srGetStrBundle(path)
 
297
{
 
298
  var strBundle = null;
 
299
 
 
300
  if (!strBundleService) {
 
301
    try {
 
302
      strBundleService = Components.classes[STRBUNDLE_SERV_CONTRACTID].getService(nsIStringBundleService);
 
303
    } catch (ex) {
 
304
      dump("\n--** strBundleService createInstance failed **--\n");
 
305
      return null;
 
306
    }
 
307
  }
 
308
 
 
309
  strBundle = strBundleService.createBundle(path); 
 
310
  if (!strBundle) {
 
311
        dump("\n--** strBundle createInstance failed **--\n");
 
312
  }
 
313
  return strBundle;
 
314
}
 
315