* Filters/FilterTiff.cs: Compilation fix for 1.1.14
[beagle.git] / mozilla-extension / content / jslib / io / dir.js
blobc9921b3344b7e85ac37431706a5306058d943959
1 /*** -*- Mode: Javascript; tab-width: 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/
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.
13 The Original Code is Collabnet code.
14 The Initial Developer of the Original Code is Collabnet.
16 Portions created by Collabnet are Copyright (C) 2000 Collabnet.
17 All Rights Reserved.
19 Contributor(s): Pete Collins, Doug Turner, Brendan Eich, Warren Harris,
20 Eric Plaster, Martin Kutschker
23 JS Directory Class API
25 dir.js
27 Function List
29 create(aPermissions); // permissions are optional
31 files(); // returns an array listing all files of a dirs contents
32 dirs(); // returns an array listing all dirs of a dirs contents
33 list(aDirPath); // returns an array listing of a dirs contents
35 // help!
36 help(); // currently dumps a list of available functions
38 Instructions:
43 if (typeof(JS_LIB_LOADED)=='boolean') {
45 /************* INCLUDE FILESYSTEM *****************/
46 if(typeof(JS_FILESYSTEM_LOADED)!='boolean')
47 include(jslib_filesystem);
48 /************* INCLUDE FILESYSTEM *****************/
51 /****************** Globals **********************/
52 const JS_DIR_FILE = "dir.js";
53 const JS_DIR_LOADED = true;
55 const JS_DIR_LOCAL_CID = "@mozilla.org/file/local;1";
56 const JS_DIR_LOCATOR_PROGID = '@mozilla.org/filelocator;1';
57 const JS_DIR_CID = "@mozilla.org/file/directory_service;1";
59 const JS_DIR_I_LOCAL_FILE = "nsILocalFile";
60 const JS_DIR_INIT_W_PATH = "initWithPath";
62 const JS_DIR_PREFS_DIR = 65539;
64 const JS_DIR_DIRECTORY = 0x01; // 1
65 const JS_DIR_OK = true;
67 const JS_DIR_DEFAULT_PERMS = 0766;
69 const JS_DIR_FilePath = new C.Constructor(JS_DIR_LOCAL_CID,
70 JS_DIR_I_LOCAL_FILE,
71 JS_DIR_INIT_W_PATH);
72 /****************** Globals **********************/
74 /****************** Dir Object Class *********************/
75 // constructor
76 function Dir(aPath) {
78 if(!aPath) {
79 jslibError(null,
80 "Please enter a local file path to initialize",
81 "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_DIR_FILE);
82 throw C.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
85 return this.initPath(arguments);
86 } // end constructor
88 Dir.prototype = new FileSystem;
89 Dir.prototype.fileInst = null;
91 /********************* CREATE ****************************/
92 Dir.prototype.create = function(aPermissions)
94 if(!this.mPath) {
95 jslibError(null, "create (no file path defined)", "NS_ERROR_NOT_INITIALIZED");
96 return C.results.NS_ERROR_NOT_INITIALIZED;
99 if(this.exists()) {
100 jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
101 return null;
104 if (typeof(aPermissions) == "number") {
105 var checkPerms = this.validatePermissions(aPermissions);
107 if(!checkPerms) {
108 jslibError(null, "create (invalid permissions)",
109 "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":create");
110 return C.results.NS_ERROR_INVALID_ARG;
112 } else {
113 checkPerms = this.mFileInst.parent.permissions;
116 var rv = null;
118 try {
119 rv=this.mFileInst.create(JS_DIR_DIRECTORY, checkPerms);
120 } catch (e) {
121 jslibError(e, "(unable to create)", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
122 rv=null;
125 return rv;
128 /********************* READDIR **************************/
129 Dir.prototype.readDir = function ()
132 if(!this.exists()) {
133 jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
134 return null;
137 var rv=null;
139 try {
140 if(!this.isDir()) {
141 jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
142 return null;
145 var files = this.mFileInst.directoryEntries;
146 var listings = new Array();
147 var file;
149 if(typeof(JS_FILE_LOADED)!='boolean')
150 include(JS_LIB_PATH+'io/file.js');
152 while(files.hasMoreElements()) {
153 file = files.getNext().QueryInterface(C.interfaces.nsILocalFile);
154 if(file.isFile())
155 listings.push(new File(file.path));
157 if(file.isDirectory())
158 listings.push(new Dir(file.path));
161 rv=listings;
162 } catch(e) {
163 jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILE_FILE+":readDir");
164 rv=null;
167 return rv;
170 /********************* REMOVE *******************************/
171 Dir.prototype.remove = function (aRecursive)
174 if(typeof(aRecursive)!='boolean')
175 aRecursive=false;
177 if(!this.checkInst())
178 throw C.results.NS_ERROR_NOT_INITIALIZED;
180 if(!this.mPath)
182 jslibError(null, "remove (no path defined)",
183 "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":remove");
184 return null;
187 var rv=null
189 try {
190 if(!this.exists()) {
191 jslibError(null, "(directory doesn't exist)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
192 return null;
195 if(!this.isDir()) {
196 jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
197 return null;
200 rv=this.mFileInst.remove(aRecursive);
201 } catch (e) {
202 jslibError(e, "(dir not empty, use 'remove(true)' for recursion)", "NS_ERROR_UNEXPECTED",
203 JS_DIR_FILE+":remove");
204 rv=null;
207 return rv;
210 /********************* HELP *****************************/
211 Dir.prototype.super_help = FileSystem.prototype.help;
213 Dir.prototype.__defineGetter__('help',
214 function() {
215 var help = this.super_help() +
217 " create(aPermissions);\n" +
218 " remove(aRecursive);\n" +
219 " readDir(aDirPath);\n";
221 return help;
224 jslibDebug('*** load: '+JS_DIR_FILE+' OK');
226 } else {
227 dump("JSLIB library not loaded:\n" +
228 " \tTo load use: chrome://jslib/content/jslib.js\n" +
229 " \tThen: include(jslib_dir);\n\n");