You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2012/05/18 00:37:08 UTC

[15/19] js commit: trying to get directory entry stuff working

trying to get directory entry stuff working


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/adf1a2c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/adf1a2c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/adf1a2c9

Branch: refs/heads/playbookFile
Commit: adf1a2c93e266bbbd6f9decdda9ec9f51056a015
Parents: 5e937b2
Author: Tim Kim <ti...@adobe.com>
Authored: Mon May 7 14:07:01 2012 -0700
Committer: Tim Kim <ti...@adobe.com>
Committed: Mon May 7 14:07:01 2012 -0700

----------------------------------------------------------------------
 lib/playbook/platform.js                       |    6 +-
 lib/playbook/plugin/playbook/DirectoryEntry.js |  458 ++++++++++---------
 lib/playbook/plugin/playbook/FileEntry.js      |    1 -
 3 files changed, 245 insertions(+), 220 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/adf1a2c9/lib/playbook/platform.js
----------------------------------------------------------------------
diff --git a/lib/playbook/platform.js b/lib/playbook/platform.js
index 0ec37a6..6cbbef4 100644
--- a/lib/playbook/platform.js
+++ b/lib/playbook/platform.js
@@ -5,6 +5,9 @@ module.exports = {
         device: {
             path: "cordova/plugin/playbook/device"
         },
+        DirectoryEntry: {
+            path: 'cordova/plugin/playbook/DirectoryEntry'
+        },
         File:{
             path: 'cordova/plugin/playbook/File'
         },
@@ -29,9 +32,6 @@ module.exports = {
                 }
             }
         },
-        DirectoryEntry: {
-            path: 'cordova/plugin/playbook/DirectoryEntry'
-        },
         Entry: {
             path: 'cordova/plugin/playbook/Entry'
         }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/adf1a2c9/lib/playbook/plugin/playbook/DirectoryEntry.js
----------------------------------------------------------------------
diff --git a/lib/playbook/plugin/playbook/DirectoryEntry.js b/lib/playbook/plugin/playbook/DirectoryEntry.js
index 2da98e7..9b134de 100644
--- a/lib/playbook/plugin/playbook/DirectoryEntry.js
+++ b/lib/playbook/plugin/playbook/DirectoryEntry.js
@@ -1,247 +1,273 @@
-var DirectoryEntry = require('cordova/plugin/DirectoryEntry'),
+var utils = require('cordova/utils'),
+    Entry = require('cordova/plugin/Entry'),
+    DirectoryReader = require('cordova/plugin/DirectoryReader'),
     FileEntry = require('cordova/plugin/FileEntry'),
-    FileError = require('cordova/plugin/FileError'),
-    exec = require('cordova/exec');
-
-module.exports = {
-    /**
-     * Creates or looks up a directory; override for BlackBerry.
-     *
-     * @param path
-     *            {DOMString} either a relative or absolute path from this
-     *            directory in which to look up or create a directory
-     * @param options
-     *            {Flags} options to create or exclusively create the directory
-     * @param successCallback
-     *            {Function} called with the new DirectoryEntry
-     * @param errorCallback
-     *            {Function} called with a FileError
-     */
-    getDirectory : function(path, options, successCallback, errorCallback) {
-        // create directory if it doesn't exist
-        var create = (options && options.create === true) ? true : false,
+    FileError = require('cordova/plugin/FileError');
+
+
+/**
+ * An interface representing a directory on the file system.
+ *
+ * {boolean} isFile always false (readonly)
+ * {boolean} isDirectory always true (readonly)
+ * {DOMString} name of the directory, excluding the path leading to it (readonly)
+ * {DOMString} fullPath the absolute full path to the directory (readonly)
+ * {FileSystem} filesystem on which the directory resides (readonly)
+ */
+var DirectoryEntry = function(name, fullPath) {
+     DirectoryEntry.__super__.constructor.apply(this, [false, true, name, fullPath]);
+};
+
+utils.extend(DirectoryEntry, Entry);
+
+/**
+ * Creates a new DirectoryReader to read entries from this directory
+ */
+DirectoryEntry.prototype.createReader = function() {
+    return new DirectoryReader(this.fullPath);
+};
+
+/**
+ * Creates or looks up a directory; override for BlackBerry.
+ *
+ * @param path
+ *            {DOMString} either a relative or absolute path from this
+ *            directory in which to look up or create a directory
+ * @param options
+ *            {Flags} options to create or exclusively create the directory
+ * @param successCallback
+ *            {Function} called with the new DirectoryEntry
+ * @param errorCallback
+ *            {Function} called with a FileError
+ */
+DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback) {
+    // create directory if it doesn't exist
+    var create = (options && options.create === true) ? true : false,
+    // if true, causes failure if create is true and path already exists
+    exclusive = (options && options.exclusive === true) ? true : false,
+    // directory exists
+    exists,
+    // create a new DirectoryEntry object and invoke success callback
+    createEntry = function() {
+        var path_parts = path.split('/'),
+            name = path_parts[path_parts.length - 1],
+            dirEntry = new DirectoryEntry(name, path);
+
+        // invoke success callback
+        if (typeof successCallback === 'function') {
+            successCallback(dirEntry);
+        }
+    };
+
+    var fail = function(error) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(error));
+        }
+    };
+
+    // determine if path is relative or absolute
+    if (!path) {
+        fail(FileError.ENCODING_ERR);
+        return;
+    } else if (path.indexOf(this.fullPath) !== 0) {
+        // path does not begin with the fullPath of this directory
+        // therefore, it is relative
+        path = this.fullPath + '/' + path;
+    }
+
+    // determine if directory exists
+    try {
+        // will return true if path exists AND is a directory
+        console.log('exists :' + exists);
+        exists = blackberry.io.dir.exists(path);
+    } catch (e) {
+        // invalid path
+        console.log('invalid path');
+        fail(FileError.ENCODING_ERR);
+        return;
+    }
+
+    // path is a directory
+    if (exists) {
+        if (create && exclusive) {
+            // can't guarantee exclusivity
+            fail(FileError.PATH_EXISTS_ERR);
+        } else {
+            // create entry for existing directory
+            createEntry();
+        }
+    }
+    // will return true if path exists AND is a file
+    else if (blackberry.io.file.exists(path)) {
+        // the path is a file
+        fail(FileError.TYPE_MISMATCH_ERR);
+    }
+    // path does not exist, create it
+    else if (create) {
+        try {
+            // directory path must have trailing slash
+            var dirPath = path;
+            if (dirPath.substr(-1) !== '/') {
+                dirPath += '/';
+            }
+            blackberry.io.dir.createNewDir(dirPath);
+            createEntry();
+        } catch (eone) {
+            // unable to create directory
+            fail(FileError.NOT_FOUND_ERR);
+        }
+    }
+    // path does not exist, don't create
+    else {
+        // directory doesn't exist
+        fail(FileError.NOT_FOUND_ERR);
+    }
+};
+
+/**
+ * Create or look up a file.
+ *
+ * @param path {DOMString}
+ *            either a relative or absolute path from this directory in
+ *            which to look up or create a file
+ * @param options {Flags}
+ *            options to create or exclusively create the file
+ * @param successCallback {Function}
+ *            called with the new FileEntry object
+ * @param errorCallback {Function}
+ *            called with a FileError object if error occurs
+ */
+DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback) {
+    // create file if it doesn't exist
+    var create = (options && options.create === true) ? true : false,
         // if true, causes failure if create is true and path already exists
         exclusive = (options && options.exclusive === true) ? true : false,
-        // directory exists
+        // file exists
         exists,
-        // create a new DirectoryEntry object and invoke success callback
+        // create a new FileEntry object and invoke success callback
         createEntry = function() {
             var path_parts = path.split('/'),
                 name = path_parts[path_parts.length - 1],
-                dirEntry = new DirectoryEntry(name, path);
+                fileEntry = new FileEntry(name, path);
 
             // invoke success callback
             if (typeof successCallback === 'function') {
-                successCallback(dirEntry);
-            }
-        };
-
-        var fail = function(error) {
-            if (typeof errorCallback === 'function') {
-                errorCallback(new FileError(error));
+                successCallback(fileEntry);
             }
         };
 
-        // determine if path is relative or absolute
-        if (!path) {
-            fail(FileError.ENCODING_ERR);
-            return;
-        } else if (path.indexOf(this.fullPath) !== 0) {
-            // path does not begin with the fullPath of this directory
-            // therefore, it is relative
-            path = this.fullPath + '/' + path;
+    var fail = function(error) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(error));
         }
+    };
 
-        // determine if directory exists
-        try {
-            // will return true if path exists AND is a directory
-            console.log('exists :' + exists);
-            exists = blackberry.io.dir.exists(path);
-        } catch (e) {
-            // invalid path
-            console.log('invalid path');
-            fail(FileError.ENCODING_ERR);
-            return;
-        }
+    // determine if path is relative or absolute
+    if (!path) {
+        fail(FileError.ENCODING_ERR);
+        return;
+    }
+    else if (path.indexOf(this.fullPath) !== 0) {
+        // path does not begin with the fullPath of this directory
+        // therefore, it is relative
+        path = this.fullPath + '/' + path;
+    }
 
-        // path is a directory
-        if (exists) {
-            if (create && exclusive) {
-                // can't guarantee exclusivity
-                fail(FileError.PATH_EXISTS_ERR);
-            } else {
-                // create entry for existing directory
-                createEntry();
-            }
-        }
+    // determine if file exists
+    try {
         // will return true if path exists AND is a file
-        else if (blackberry.io.file.exists(path)) {
-            // the path is a file
-            fail(FileError.TYPE_MISMATCH_ERR);
-        }
-        // path does not exist, create it
-        else if (create) {
-            try {
-                // directory path must have trailing slash
-                var dirPath = path;
-                if (dirPath.substr(-1) !== '/') {
-                    dirPath += '/';
-                }
-                blackberry.io.dir.createNewDir(dirPath);
-                createEntry();
-            } catch (eone) {
-                // unable to create directory
-                fail(FileError.NOT_FOUND_ERR);
-            }
+        exists = blackberry.io.file.exists(path);
+    }
+    catch (e) {
+        // invalid path
+        fail(FileError.ENCODING_ERR);
+        return;
+    }
+
+    // path is a file
+    if (exists) {
+        if (create && exclusive) {
+            // can't guarantee exclusivity
+            fail(FileError.PATH_EXISTS_ERR);
         }
-        // path does not exist, don't create
         else {
-            // directory doesn't exist
-            fail(FileError.NOT_FOUND_ERR);
+            // create entry for existing file
+            createEntry();
         }
-    },
-    /**
-     * Create or look up a file.
-     *
-     * @param path {DOMString}
-     *            either a relative or absolute path from this directory in
-     *            which to look up or create a file
-     * @param options {Flags}
-     *            options to create or exclusively create the file
-     * @param successCallback {Function}
-     *            called with the new FileEntry object
-     * @param errorCallback {Function}
-     *            called with a FileError object if error occurs
-     */
-    getFile:function(path, options, successCallback, errorCallback) {
-        // create file if it doesn't exist
-        var create = (options && options.create === true) ? true : false,
-            // if true, causes failure if create is true and path already exists
-            exclusive = (options && options.exclusive === true) ? true : false,
-            // file exists
-            exists,
-            // create a new FileEntry object and invoke success callback
-            createEntry = function() {
-                var path_parts = path.split('/'),
-                    name = path_parts[path_parts.length - 1],
-                    fileEntry = new FileEntry(name, path);
-
-                // invoke success callback
-                if (typeof successCallback === 'function') {
-                    successCallback(fileEntry);
-                }
-            };
+    }
+    // will return true if path exists AND is a directory
+    else if (blackberry.io.dir.exists(path)) {
+        // the path is a directory
+        fail(FileError.TYPE_MISMATCH_ERR);
+    }
+    // path does not exist, create it
+    else if (create) {
+        // create empty file
+        console.log('making file');
+        var emptyBlob = blackberry.utils.stringToBlob('');
+        blackberry.io.file.saveFile(path,emptyBlob);
+        createEntry();
+        /*
+        exec(
+            function(result) {
+                // file created
+                createEntry();
+            },
+            fail, "File", "write", [ path, "", 0 ]);
+        */
+    }
+    // path does not exist, don't create
+    else {
+        // file doesn't exist
+        fail(FileError.NOT_FOUND_ERR);
+    }
+};
 
-        var fail = function(error) {
-            if (typeof errorCallback === 'function') {
-                errorCallback(new FileError(error));
-            }
-        };
+/**
+ * Delete a directory and all of it's contents.
+ *
+ * @param successCallback {Function} called with no parameters
+ * @param errorCallback {Function} called with a FileError
+ */
+DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) {
+    // we're removing THIS directory
+    var path = this.fullPath;
 
-        // determine if path is relative or absolute
-        if (!path) {
-            fail(FileError.ENCODING_ERR);
-            return;
-        }
-        else if (path.indexOf(this.fullPath) !== 0) {
-            // path does not begin with the fullPath of this directory
-            // therefore, it is relative
-            path = this.fullPath + '/' + path;
+    var fail = function(error) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(error));
         }
+    };
 
-        // determine if file exists
-        try {
-            // will return true if path exists AND is a file
-            exists = blackberry.io.file.exists(path);
+    // attempt to delete directory
+    if (blackberry.io.dir.exists(path)) {
+        // it is an error to attempt to remove the file system root
+        //exec(null, null, "File", "isFileSystemRoot", [ path ]) === true
+        if (false) {
+            fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
         }
-        catch (e) {
-            // invalid path
-            fail(FileError.ENCODING_ERR);
-            return;
-        }
-
-        // path is a file
-        if (exists) {
-            if (create && exclusive) {
-                // can't guarantee exclusivity
-                fail(FileError.PATH_EXISTS_ERR);
-            }
-            else {
-                // create entry for existing file
-                createEntry();
-            }
-        }
-        // will return true if path exists AND is a directory
-        else if (blackberry.io.dir.exists(path)) {
-            // the path is a directory
-            fail(FileError.TYPE_MISMATCH_ERR);
-        }
-        // path does not exist, create it
-        else if (create) {
-            // create empty file
-            console.log('making file');
-            var emptyBlob = blackberry.utils.stringToBlob('');
-            blackberry.io.file.saveFile(path,emptyBlob);
-            createEntry();
-            /*
-            exec(
-                function(result) {
-                    // file created
-                    createEntry();
-                },
-                fail, "File", "write", [ path, "", 0 ]);
-            */
-        }
-        // path does not exist, don't create
         else {
-            // file doesn't exist
-            fail(FileError.NOT_FOUND_ERR);
-        }
-    },
-
-    /**
-     * Delete a directory and all of it's contents.
-     *
-     * @param successCallback {Function} called with no parameters
-     * @param errorCallback {Function} called with a FileError
-     */
-    removeRecursively : function(successCallback, errorCallback) {
-        // we're removing THIS directory
-        var path = this.fullPath;
-
-        var fail = function(error) {
-            if (typeof errorCallback === 'function') {
-                errorCallback(new FileError(error));
-            }
-        };
-
-        // attempt to delete directory
-        if (blackberry.io.dir.exists(path)) {
-            // it is an error to attempt to remove the file system root
-            if (exec(null, null, "File", "isFileSystemRoot", [ path ]) === true) {
-                fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
-            }
-            else {
-                try {
-                    // delete the directory, setting recursive flag to true
-                    blackberry.io.dir.deleteDirectory(path, true);
-                    if (typeof successCallback === "function") {
-                        successCallback();
-                    }
-                } catch (e) {
-                    // permissions don't allow deletion
-                    console.log(e);
-                    fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
+            try {
+                // delete the directory, setting recursive flag to true
+                blackberry.io.dir.deleteDirectory(path, true);
+                if (typeof successCallback === "function") {
+                    successCallback();
                 }
+            } catch (e) {
+                // permissions don't allow deletion
+                console.log(e);
+                fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
             }
         }
-        // it's a file, not a directory
-        else if (blackberry.io.file.exists(path)) {
-            fail(FileError.TYPE_MISMATCH_ERR);
-        }
-        // not found
-        else {
-            fail(FileError.NOT_FOUND_ERR);
-        }
+    }
+    // it's a file, not a directory
+    else if (blackberry.io.file.exists(path)) {
+        fail(FileError.TYPE_MISMATCH_ERR);
+    }
+    // not found
+    else {
+        fail(FileError.NOT_FOUND_ERR);
     }
 };
+
+module.exports = new DirectoryEntry();

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/adf1a2c9/lib/playbook/plugin/playbook/FileEntry.js
----------------------------------------------------------------------
diff --git a/lib/playbook/plugin/playbook/FileEntry.js b/lib/playbook/plugin/playbook/FileEntry.js
index 493a0d6..0f04c4d 100644
--- a/lib/playbook/plugin/playbook/FileEntry.js
+++ b/lib/playbook/plugin/playbook/FileEntry.js
@@ -1,5 +1,4 @@
 var utils = require('cordova/utils'),
-    exec = require('cordova/exec'),
     Entry = require('cordova/plugin/Entry'),
     FileWriter = require('cordova/plugin/FileWriter'),
     File = require('cordova/plugin/File'),