You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by de...@apache.org on 2012/02/27 23:05:06 UTC

git commit: Cleanup BB File API. Delete LocalFileSystem.isFileSystemRoot.

Updated Branches:
  refs/heads/master 73d34c34a -> 0ce751417


Cleanup BB File API. Delete LocalFileSystem.isFileSystemRoot.


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/0ce75141
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/0ce75141
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/0ce75141

Branch: refs/heads/master
Commit: 0ce751417dee8d13d0b35c84a8b099e1a6c77ee1
Parents: 73d34c3
Author: Drew Walters <de...@gmail.com>
Authored: Mon Feb 27 15:56:01 2012 -0600
Committer: Drew Walters <de...@gmail.com>
Committed: Mon Feb 27 15:56:01 2012 -0600

----------------------------------------------------------------------
 lib/platform/blackberry.js              |   10 ++-
 lib/plugin/LocalFileSystem.js           |    5 -
 lib/plugin/blackberry/DirectoryEntry.js |  115 +++++++++++++++++++-------
 lib/plugin/blackberry/Entry.js          |   50 +++++++-----
 4 files changed, 121 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0ce75141/lib/platform/blackberry.js
----------------------------------------------------------------------
diff --git a/lib/platform/blackberry.js b/lib/platform/blackberry.js
index ce2f53d..dd25f07 100644
--- a/lib/platform/blackberry.js
+++ b/lib/platform/blackberry.js
@@ -22,7 +22,7 @@ module.exports = {
                 origLog.call(window.console, msg);
             }
             org.apache.cordova.Logger.log(''+msg);
-        }
+        };
 
         // TODO: is there a better way to do this? build-time
         // convention? how can we save the few bytes from
@@ -37,6 +37,7 @@ module.exports = {
 
         DirectoryEntry.prototype.getDirectory = BB_DirectoryEntry.getDirectory;
         DirectoryEntry.prototype.getFile = BB_DirectoryEntry.getFile;
+        DirectoryEntry.prototype.removeRecursively = BB_DirectoryEntry.removeRecursively;
 
         // Mapping of button events to BlackBerry key identifier.
         var buttonMapping = {
@@ -73,8 +74,8 @@ module.exports = {
                     blackberry.system.event.onHardwareKey(
                             buttonMapping[event], null);
                 }
-            }}
-        }
+            }};
+        };
 
         // Inject listeners for buttons on the document.
         for (var button in buttonMapping) {
@@ -164,6 +165,9 @@ module.exports = {
         },
         device: {
             path: "cordova/plugin/blackberry/device"
+        },
+        File: { // exists natively on BlackBerry OS 7, override
+            path: "cordova/plugin/File"
         }
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0ce75141/lib/plugin/LocalFileSystem.js
----------------------------------------------------------------------
diff --git a/lib/plugin/LocalFileSystem.js b/lib/plugin/LocalFileSystem.js
index 134b9ac..f71ae62 100644
--- a/lib/plugin/LocalFileSystem.js
+++ b/lib/plugin/LocalFileSystem.js
@@ -7,11 +7,6 @@ var LocalFileSystem = function() {
 
 };
 
-// Non-standard function
-LocalFileSystem.prototype.isFileSystemRoot = function(path) {
-    return exec(null, null, "File", "isFileSystemRoot", [path]);
-};
-
 LocalFileSystem.TEMPORARY = 0; //temporary, with no guarantee of persistence
 LocalFileSystem.PERSISTENT = 1; //persistent
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0ce75141/lib/plugin/blackberry/DirectoryEntry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/blackberry/DirectoryEntry.js b/lib/plugin/blackberry/DirectoryEntry.js
index 9334fff..0dbbb8e 100644
--- a/lib/plugin/blackberry/DirectoryEntry.js
+++ b/lib/plugin/blackberry/DirectoryEntry.js
@@ -1,11 +1,12 @@
 var DirectoryEntry = require('cordova/plugin/DirectoryEntry'),
     FileEntry = require('cordova/plugin/FileEntry'),
-    FileError = require('cordova/plugin/FileError');
+    FileError = require('cordova/plugin/FileError'),
+    exec = require('cordova/exec');
 
 var BB_DirectoryEntry = {
 /**
  * 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
@@ -29,24 +30,30 @@ var BB_DirectoryEntry = {
             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) {
-        errorCallback(new FileError(FileError.ENCODING_ERR));
+        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
@@ -54,25 +61,25 @@ var BB_DirectoryEntry = {
     }
     catch (e) {
         // invalid path
-        errorCallback(new FileError(FileError.ENCODING_ERR));
+        fail(FileError.ENCODING_ERR);
         return;
     }
-    
+
     // path is a directory
     if (exists) {
         if (create && exclusive) {
             // can't guarantee exclusivity
-            errorCallback(new FileError(FileError.PATH_EXISTS_ERR));
+            fail(FileError.PATH_EXISTS_ERR);
         }
         else {
             // create entry for existing directory
-            createEntry();                
+            createEntry();
         }
     }
     // will return true if path exists AND is a file
     else if (blackberry.io.file.exists(path)) {
         // the path is a file
-        errorCallback(new FileError(FileError.TYPE_MISMATCH_ERR));
+        fail(FileError.TYPE_MISMATCH_ERR);
     }
     // path does not exist, create it
     else if (create) {
@@ -87,18 +94,18 @@ var BB_DirectoryEntry = {
         }
         catch (eone) {
             // unable to create directory
-            errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+            fail(FileError.NOT_FOUND_ERR);
         }
     }
     // path does not exist, don't create
     else {
         // directory doesn't exist
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
-    }             
+        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
@@ -121,16 +128,22 @@ var BB_DirectoryEntry = {
                 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);
                 }
             };
 
+        var fail = function(error) {
+            if (typeof errorCallback === 'function') {
+                errorCallback(new FileError(error));
+            }
+        };
+
         // determine if path is relative or absolute
         if (!path) {
-            errorCallback(new FileError(FileError.ENCODING_ERR));
+            fail(FileError.ENCODING_ERR);
             return;
         }
         else if (path.indexOf(this.fullPath) !== 0) {
@@ -146,44 +159,86 @@ var BB_DirectoryEntry = {
         }
         catch (e) {
             // invalid path
-            errorCallback(new FileError(FileError.ENCODING_ERR));
+            fail(FileError.ENCODING_ERR);
             return;
         }
-        
+
         // path is a file
         if (exists) {
             if (create && exclusive) {
                 // can't guarantee exclusivity
-                errorCallback(new FileError(FileError.PATH_EXISTS_ERR));
+                fail(FileError.PATH_EXISTS_ERR);
             }
             else {
                 // create entry for existing file
-                createEntry();                
+                createEntry();
             }
         }
         // will return true if path exists AND is a directory
         else if (blackberry.io.dir.exists(path)) {
             // the path is a directory
-            errorCallback(new FileError(FileError.TYPE_MISMATCH_ERR));
+            fail(FileError.TYPE_MISMATCH_ERR);
         }
         // path does not exist, create it
         else if (create) {
             // create empty file
-          // TODO: wtf is this?
-            navigator.fileMgr.write(path, "", 0,
+            exec(
                 function(result) {
                     // file created
                     createEntry();
                 },
-                function(error) {
-                    // unable to create file
-                    errorCallback(new FileError(error));
-                });
+                fail, "File", "write", [ path, "", 0 ]);
         }
         // path does not exist, don't create
         else {
             // file doesn't exist
-            errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+            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);
+                }
+            }
+        }
+        // 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);
         }
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0ce75141/lib/plugin/blackberry/Entry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/blackberry/Entry.js b/lib/plugin/blackberry/Entry.js
index 537bb3b..9d7e0eb 100644
--- a/lib/plugin/blackberry/Entry.js
+++ b/lib/plugin/blackberry/Entry.js
@@ -1,37 +1,44 @@
 var FileError = require('cordova/plugin/FileError'),
     LocalFileSystem = require('cordova/plugin/LocalFileSystem'),
-    resolveLocalFileSystemURI = require('cordova/plugin/resolveLocalFileSystemURI');
+    resolveLocalFileSystemURI = require('cordova/plugin/resolveLocalFileSystemURI'),
+    exec = require('cordova/exec');
 
 module.exports = {
   remove:function(successCallback, errorCallback) {
     var path = this.fullPath,
         // directory contents
         contents = [];
-    
+
+    var fail = function(error) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(error));
+        }
+    };
+
     // file
     if (blackberry.io.file.exists(path)) {
         try {
             blackberry.io.file.deleteFile(path);
             if (typeof successCallback === "function") {
                 successCallback();
-            }                
+            }
         }
         catch (e) {
             // permissions don't allow
-            errorCallback(new FileError(FileError.INVALID_MODIFICATION_ERR));
+            fail(FileError.INVALID_MODIFICATION_ERR);
         }
     }
     // directory
     else if (blackberry.io.dir.exists(path)) {
         // it is an error to attempt to remove the file system root
-        if (LocalFileSystem.isFileSystemRoot(path)) {
-            errorCallback(new FileError(FileError.NO_MODIFICATION_ALLOWED_ERR));
+        if (exec(null, null, "File", "isFileSystemRoot", [ path ]) === true) {
+            fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
         }
         else {
             // check to see if directory is empty
             contents = blackberry.io.dir.listFiles(path);
             if (contents.length !== 0) {
-                errorCallback(new FileError(FileError.INVALID_MODIFICATION_ERR));
+                fail(FileError.INVALID_MODIFICATION_ERR);
             }
             else {
                 try {
@@ -43,44 +50,45 @@ module.exports = {
                 }
                 catch (eone) {
                     // permissions don't allow
-                    errorCallback(new FileError(FileError.NO_MODIFICATION_ALLOWED_ERR));
+                    fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
                 }
             }
         }
     }
     // not found
     else {
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+        fail(FileError.NOT_FOUND_ERR);
     }
   },
   getParent:function(successCallback, errorCallback) {
     var that = this;
-    
+
     try {
-        // On BlackBerry, the TEMPORARY file system is actually a temporary 
+        // On BlackBerry, the TEMPORARY file system is actually a temporary
         // directory that is created on a per-application basis.  This is
         // to help ensure that applications do not share the same temporary
         // space.  So we check to see if this is the TEMPORARY file system
         // (directory).  If it is, we must return this Entry, rather than
         // the Entry for its parent.
         requestFileSystem(LocalFileSystem.TEMPORARY, 0,
-                function(fileSystem) {                        
+                function(fileSystem) {
                     if (fileSystem.root.fullPath === that.fullPath) {
-                        successCallback(fileSystem.root);
+                        if (typeof successCallback === 'function') {
+                            successCallback(fileSystem.root);
+                        }
                     }
                     else {
                         resolveLocalFileSystemURI(
-                                blackberry.io.dir.getParentDirectory(that.fullPath), 
-                                successCallback, 
+                                blackberry.io.dir.getParentDirectory(that.fullPath),
+                                successCallback,
                                 errorCallback);
                     }
-                },
-                function (error) {
-                    errorCallback(error);
-                });
-    } 
+                }, errorCallback);
+    }
     catch (e) {
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+        }
     }
   }
 };