You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by gt...@apache.org on 2013/01/02 19:08:26 UTC

[1/2] js commit: Add initial File API support for BB10

Add initial File API support for BB10


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

Branch: refs/heads/master
Commit: df5c78d69020966aaa57678354d9e271d052ccab
Parents: 2226b09
Author: Bryan Higgins <bh...@rim.com>
Authored: Fri Dec 14 22:14:06 2012 -0500
Committer: Gord Tanner <gt...@gmail.com>
Committed: Wed Jan 2 12:36:52 2013 -0500

----------------------------------------------------------------------
 lib/blackberry/plugin/qnx/file.js                  |  425 +++++++++++++++
 lib/blackberry/plugin/qnx/manager.js               |    1 +
 lib/blackberry/plugin/qnx/platform.js              |    8 -
 lib/blackberry/plugin/qnx/requestFileSystem.js     |   22 -
 .../plugin/qnx/resolveLocalFileSystemURI.js        |   22 -
 test/blackberry/qnx/test.platform.js               |   12 +-
 test/blackberry/qnx/test.requestFileSystem.js      |   32 --
 .../qnx/test.resolveLocalFileSystemURI.js          |   32 --
 8 files changed, 427 insertions(+), 127 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/lib/blackberry/plugin/qnx/file.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/qnx/file.js b/lib/blackberry/plugin/qnx/file.js
new file mode 100644
index 0000000..6317857
--- /dev/null
+++ b/lib/blackberry/plugin/qnx/file.js
@@ -0,0 +1,425 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+/*global WebKitBlobBuilder:false */
+var FileError = require('cordova/plugin/FileError'),
+    DirectoryEntry = require('cordova/plugin/DirectoryEntry'),
+    FileEntry = require('cordova/plugin/FileEntry'),
+    File = require('cordova/plugin/File'),
+    FileSystem = require('cordova/plugin/FileSystem'),
+    FileReader = require('cordova/plugin/FileReader'),
+    nativeRequestFileSystem = window.webkitRequestFileSystem,
+    nativeResolveLocalFileSystemURI = function(uri, success, fail) {
+        if (uri.substring(0,11) !== "filesystem:") {
+            uri = "filesystem:" + uri;
+        }
+        window.webkitResolveLocalFileSystemURL(uri, success, fail);
+    },
+    NativeFileReader = window.FileReader;
+
+window.FileReader = FileReader;
+window.File = File;
+
+function getFileSystemName(nativeFs) {
+    return (nativeFs.name.indexOf("Persistent") != -1) ? "persistent" : "temporary";
+}
+
+function makeEntry(entry) {
+    if (entry.isDirectory) {
+        return new DirectoryEntry(entry.name, decodeURI(entry.toURL()).substring(11));
+    }
+    else {
+        return new FileEntry(entry.name, decodeURI(entry.toURL()).substring(11));
+    }
+}
+
+module.exports = {
+    /* requestFileSystem */
+    requestFileSystem: function(args, successCallback, errorCallback) {
+        var type = args[0],
+            size = args[1];
+
+        nativeRequestFileSystem(type, size, function(nativeFs) {
+            successCallback(new FileSystem(getFileSystemName(nativeFs), makeEntry(nativeFs.root)));
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* resolveLocalFileSystemURI */
+    resolveLocalFileSystemURI: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            successCallback(makeEntry(entry));
+        }, function(error) {
+            var code = error.code;
+            switch (code) {
+                case 5:
+                    code = FileError.NOT_FOUND_ERR;
+                    break;
+
+                case 2:
+                    code = FileError.ENCODING_ERR;
+                    break;
+            }
+            errorCallback(code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* DirectoryReader */
+    readEntries: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(dirEntry) {
+            var reader = dirEntry.createReader();
+            reader.readEntries(function(entries) {
+                var retVal = [];
+                for (var i = 0; i < entries.length; i++) {
+                    retVal.push(makeEntry(entries[i]));
+                }
+                successCallback(retVal);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* Entry */
+    getMetadata: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            entry.getMetadata(function(metaData) {
+                successCallback(metaData.modificationTime);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    moveTo: function(args, successCallback, errorCallback) {
+        var srcUri = args[0],
+            parentUri = args[1],
+            name = args[2];
+
+        nativeResolveLocalFileSystemURI(srcUri, function(source) {
+            nativeResolveLocalFileSystemURI(parentUri, function(parent) {
+                source.moveTo(parent, name, function(entry) {
+                    successCallback(makeEntry(entry));
+                }, function(error) {
+                    errorCallback(error.code);
+                });
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    copyTo: function(args, successCallback, errorCallback) {
+        var srcUri = args[0],
+            parentUri = args[1],
+            name = args[2];
+
+        nativeResolveLocalFileSystemURI(srcUri, function(source) {
+            nativeResolveLocalFileSystemURI(parentUri, function(parent) {
+                source.copyTo(parent, name, function(entry) {
+                    successCallback(makeEntry(entry));
+                }, function(error) {
+                    errorCallback(error.code);
+                });
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    remove: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            if (entry.fullPath === "/") {
+                errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR);
+            } else {
+                entry.remove(
+                    function (success) {
+                        if (successCallback) {
+                            successCallback(success)
+                        }
+                    },
+                    function(error) {
+                        if (errorCallback) {
+                            errorCallback(error.code);
+                        }
+                    }
+                );
+            }
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    getParent: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            entry.getParent(function(entry) {
+                successCallback(makeEntry(entry));
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* FileEntry */
+    getFileMetadata: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            entry.file(function(file) {
+                var retVal = new File(file.name, decodeURI(entry.toURL()), file.type, file.lastModifiedDate, file.size);
+                successCallback(retVal);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* DirectoryEntry */
+    getDirectory: function(args, successCallback, errorCallback) {
+        var uri = args[0],
+            path = args[1],
+            options = args[2];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            entry.getDirectory(path, options, function(entry) {
+                successCallback(makeEntry(entry));
+            }, function(error) {
+                if (error.code === FileError.INVALID_MODIFICATION_ERR) {
+                    if (options.create) {
+                        errorCallback(FileError.PATH_EXISTS_ERR);
+                    } else {
+                        errorCallback(FileError.ENCODING_ERR);
+                    }
+                } else {
+                    errorCallback(error.code);
+                }
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    removeRecursively: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            if (entry.fullPath === "/") {
+                errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR);
+            } else {
+                entry.removeRecursively(
+                    function (success) {
+                        if (successCallback) {
+                            successCallback(success);
+                        }
+                    },
+                    function(error) {
+                        errorCallback(error.code);
+                    }
+                );
+            }
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    getFile: function(args, successCallback, errorCallback) {
+        var uri = args[0],
+            path = args[1],
+            options = args[2];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            entry.getFile(path, options, function(entry) {
+                successCallback(makeEntry(entry));
+            }, function(error) {
+                if (error.code === FileError.INVALID_MODIFICATION_ERR) {
+                    if (options.create) {
+                        errorCallback(FileError.PATH_EXISTS_ERR);
+                    } else {
+                        errorCallback(FileError.NOT_FOUND_ERR);
+                    }
+                } else {
+                    errorCallback(error.code);
+                }
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* FileReader */
+    readAsText: function(args, successCallback, errorCallback) {
+        var uri = args[0],
+            encoding = args[1];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            var onLoadEnd = function(evt) {
+                    if (!evt.target.error) {
+                        successCallback(evt.target.result);
+                    }
+            },
+                onError = function(evt) {
+                    errorCallback(evt.target.error.code);
+            };
+
+            var reader = new NativeFileReader();
+
+            reader.onloadend = onLoadEnd;
+            reader.onerror = onError;
+            entry.file(function(file) {
+                reader.readAsText(file, encoding);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    readAsDataURL: function(args, successCallback, errorCallback) {
+        var uri = args[0];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            var onLoadEnd = function(evt) {
+                    if (!evt.target.error) {
+                        successCallback(evt.target.result);
+                    }
+            },
+                onError = function(evt) {
+                    errorCallback(evt.target.error.code);
+            };
+
+            var reader = new NativeFileReader();
+
+            reader.onloadend = onLoadEnd;
+            reader.onerror = onError;
+            entry.file(function(file) {
+                reader.readAsDataURL(file);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    /* FileWriter */
+    write: function(args, successCallback, errorCallback) {
+        var uri = args[0],
+            text = args[1],
+            position = args[2];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            var onWriteEnd = function(evt) {
+                    if(!evt.target.error) {
+                        successCallback(evt.target.position - position);
+                    } else {
+                        errorCallback(evt.target.error.code);
+                    }
+            },
+                onError = function(evt) {
+                    errorCallback(evt.target.error.code);
+            };
+
+            entry.createWriter(function(writer) {
+                var blob = new WebKitBlobBuilder();
+                blob.append(text);
+
+                writer.onwriteend = onWriteEnd;
+                writer.onerror = onError;
+
+                writer.seek(position);
+                writer.write(blob.getBlob('text/plain'));
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    },
+
+    truncate: function(args, successCallback, errorCallback) {
+        var uri = args[0],
+            size = args[1];
+
+        nativeResolveLocalFileSystemURI(uri, function(entry) {
+            var onWriteEnd = function(evt) {
+                    if(!evt.target.error) {
+                        successCallback(evt.target.length);
+                    } else {
+                        errorCallback(evt.target.error.code);
+                    }
+            },
+                onError = function(evt) {
+                    errorCallback(evt.target.error.code);
+            };
+
+            entry.createWriter(function(writer) {
+                writer.onwriteend = onWriteEnd;
+                writer.onerror = onError;
+
+                writer.truncate(size);
+            }, function(error) {
+                errorCallback(error.code);
+            });
+        }, function(error) {
+            errorCallback(error.code);
+        });
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+    }
+};

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/lib/blackberry/plugin/qnx/manager.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/qnx/manager.js b/lib/blackberry/plugin/qnx/manager.js
index 849d760..ccbefce 100644
--- a/lib/blackberry/plugin/qnx/manager.js
+++ b/lib/blackberry/plugin/qnx/manager.js
@@ -31,6 +31,7 @@ var cordova = require('cordova'),
         'Logger' : require('cordova/plugin/webworks/logger'),
         'Notification' : require('cordova/plugin/webworks/notification'),
         'Media': require('cordova/plugin/webworks/media'),
+        'File' : require('cordova/plugin/qnx/file'),
         'FileTransfer': require('cordova/plugin/qnx/fileTransfer')
     };
 

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/lib/blackberry/plugin/qnx/platform.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/qnx/platform.js b/lib/blackberry/plugin/qnx/platform.js
index 21d050f..753e9dc 100644
--- a/lib/blackberry/plugin/qnx/platform.js
+++ b/lib/blackberry/plugin/qnx/platform.js
@@ -41,14 +41,6 @@ module.exports = {
             });
         });
     },
-    objects: {
-        requestFileSystem:{
-            path: 'cordova/plugin/qnx/requestFileSystem'
-        },
-        resolveLocalFileSystemURI:{
-            path: 'cordova/plugin/qnx/resolveLocalFileSystemURI'
-        }
-    },
     merges: {
         navigator: {
             children: {

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/lib/blackberry/plugin/qnx/requestFileSystem.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/qnx/requestFileSystem.js b/lib/blackberry/plugin/qnx/requestFileSystem.js
deleted file mode 100644
index 36524a0..0000000
--- a/lib/blackberry/plugin/qnx/requestFileSystem.js
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-module.exports = window.webkitRequestFileSystem;

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/lib/blackberry/plugin/qnx/resolveLocalFileSystemURI.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/qnx/resolveLocalFileSystemURI.js b/lib/blackberry/plugin/qnx/resolveLocalFileSystemURI.js
deleted file mode 100644
index f0b29d2..0000000
--- a/lib/blackberry/plugin/qnx/resolveLocalFileSystemURI.js
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-module.exports = window.webkitResolveLocalFileSystemURI;

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/test/blackberry/qnx/test.platform.js
----------------------------------------------------------------------
diff --git a/test/blackberry/qnx/test.platform.js b/test/blackberry/qnx/test.platform.js
index 23a7f0f..87e18a3 100644
--- a/test/blackberry/qnx/test.platform.js
+++ b/test/blackberry/qnx/test.platform.js
@@ -121,20 +121,10 @@ describe("blackberry qnx platform", function () {
 
     });
 
-    describe('export objects', function(){
-        it('should define the requestFileSystem path', function(){
-            expect(platform.objects.requestFileSystem.path).toEqual("cordova/plugin/qnx/requestFileSystem");
-        });
-
-        it('should define the resolveLocalFileSystemURI path', function(){
-            expect(platform.objects.resolveLocalFileSystemURI.path).toEqual("cordova/plugin/qnx/resolveLocalFileSystemURI");
-        });
-    });
-
     describe('export merges', function(){
         it('should define the compass path', function(){
             expect(platform.merges.navigator.children.compass.path).toEqual("cordova/plugin/qnx/compass");
         });
     });
     
-});
\ No newline at end of file
+});

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/test/blackberry/qnx/test.requestFileSystem.js
----------------------------------------------------------------------
diff --git a/test/blackberry/qnx/test.requestFileSystem.js b/test/blackberry/qnx/test.requestFileSystem.js
deleted file mode 100644
index c5c4d0b..0000000
--- a/test/blackberry/qnx/test.requestFileSystem.js
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-describe("blackberry qnx requestFileSystem", function () {
-    var requestFileSystem = require('cordova/plugin/qnx/requestFileSystem');
-
-    describe("webkitRequestFileSystem", function(){
-        it('should call on webkitRequestFileSystem', function(){
-            expect(requestFileSystem).toEqual(window.webkitRequestFileSystem);
-        });
-
-    });
-    
-});

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/df5c78d6/test/blackberry/qnx/test.resolveLocalFileSystemURI.js
----------------------------------------------------------------------
diff --git a/test/blackberry/qnx/test.resolveLocalFileSystemURI.js b/test/blackberry/qnx/test.resolveLocalFileSystemURI.js
deleted file mode 100644
index 2d6a3d5..0000000
--- a/test/blackberry/qnx/test.resolveLocalFileSystemURI.js
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-describe("blackberry qnx resolveLocalFileSystemURI", function () {
-    var resolveLocalFileSystemURI = require('cordova/plugin/qnx/resolveLocalFileSystemURI');
-
-    describe("webkitResolveLocalFileSystemURI", function(){
-        it('should call on webkitResolveLocalFileSystemURI', function(){
-            expect(resolveLocalFileSystemURI).toEqual(window.webkitResolveLocalFileSystemURI);
-        });
-
-    });
-    
-});