Return-Path: X-Original-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1842B9A46 for ; Thu, 17 May 2012 22:37:09 +0000 (UTC) Received: (qmail 55916 invoked by uid 500); 17 May 2012 22:37:08 -0000 Delivered-To: apmail-incubator-callback-commits-archive@incubator.apache.org Received: (qmail 55862 invoked by uid 500); 17 May 2012 22:37:08 -0000 Mailing-List: contact callback-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-commits@incubator.apache.org Received: (qmail 55678 invoked by uid 99); 17 May 2012 22:37:08 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 17 May 2012 22:37:08 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 8D87A1680F; Thu, 17 May 2012 22:37:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: timkim@apache.org To: callback-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [16/19] js commit: Forgot to add these files Message-Id: <20120517223708.8D87A1680F@tyr.zones.apache.org> Date: Thu, 17 May 2012 22:37:08 +0000 (UTC) Forgot to add these files 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/5e937b29 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/5e937b29 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/5e937b29 Branch: refs/heads/playbookFile Commit: 5e937b29a4e330897dd7e0472bde75d4c83d5468 Parents: 1ed4ec4 Author: Tim Kim Authored: Mon May 7 12:31:49 2012 -0700 Committer: Tim Kim Committed: Mon May 7 12:31:49 2012 -0700 ---------------------------------------------------------------------- lib/playbook/platform.js | 12 +- lib/playbook/plugin/playbook/DirectoryEntry.js | 247 +++++++++++++++ lib/playbook/plugin/playbook/Entry.js | 88 ++++++ lib/playbook/plugin/playbook/File.js | 18 ++ lib/playbook/plugin/playbook/FileEntry.js | 74 +++++ lib/playbook/plugin/playbook/FileReader.js | 255 ++++++++++++++++ lib/playbook/plugin/playbook/FileWriter.js | 255 ++++++++++++++++ lib/playbook/plugin/playbook/requestFileSystem.js | 45 +++ 8 files changed, 991 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/platform.js ---------------------------------------------------------------------- diff --git a/lib/playbook/platform.js b/lib/playbook/platform.js index dd73a54..0ec37a6 100644 --- a/lib/playbook/platform.js +++ b/lib/playbook/platform.js @@ -5,15 +5,21 @@ module.exports = { device: { path: "cordova/plugin/playbook/device" }, - requestFileSystem:{ - path: 'cordova/plugin/playbook/requestFileSystem' + File:{ + path: 'cordova/plugin/playbook/File' + }, + FileEntry:{ + path: 'cordova/plugin/playbook/FileEntry' }, FileReader:{ path: 'cordova/plugin/playbook/FileReader' }, FileWriter:{ path: 'cordova/plugin/playbook/FileWriter' - } + }, + requestFileSystem:{ + path: 'cordova/plugin/playbook/requestFileSystem' + }, }, merges: { navigator: { http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/DirectoryEntry.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/DirectoryEntry.js b/lib/playbook/plugin/playbook/DirectoryEntry.js new file mode 100644 index 0000000..2da98e7 --- /dev/null +++ b/lib/playbook/plugin/playbook/DirectoryEntry.js @@ -0,0 +1,247 @@ +var DirectoryEntry = require('cordova/plugin/DirectoryEntry'), + 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, + // 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 + */ + 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); + } + }; + + 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 file exists + try { + // will return true if path exists AND is a file + 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); + } + 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); + } + } + } + // 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/5e937b29/lib/playbook/plugin/playbook/Entry.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/Entry.js b/lib/playbook/plugin/playbook/Entry.js new file mode 100644 index 0000000..52b1fd5 --- /dev/null +++ b/lib/playbook/plugin/playbook/Entry.js @@ -0,0 +1,88 @@ +var FileError = require('cordova/plugin/FileError'), + LocalFileSystem = require('cordova/plugin/LocalFileSystem'), + 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 + 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 + console.log('entry directory'); + 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) { + fail(FileError.INVALID_MODIFICATION_ERR); + } else { + try { + // delete + blackberry.io.dir.deleteDirectory(path, false); + if (typeof successCallback === "function") { + successCallback(); + } + } catch (eone) { + // permissions don't allow + fail(FileError.NO_MODIFICATION_ALLOWED_ERR); + } + } + } + } + // not found + else { + fail(FileError.NOT_FOUND_ERR); + } + }, + getParent : function(successCallback, errorCallback) { + var that = this; + + try { + // 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) { + if (fileSystem.root.fullPath === that.fullPath) { + if (typeof successCallback === 'function') { + successCallback(fileSystem.root); + } + } else { + resolveLocalFileSystemURI(blackberry.io.dir + .getParentDirectory(that.fullPath), + successCallback, errorCallback); + } + }, errorCallback); + } catch (e) { + if (typeof errorCallback === 'function') { + errorCallback(new FileError(FileError.NOT_FOUND_ERR)); + } + } + } +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/File.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/File.js b/lib/playbook/plugin/playbook/File.js new file mode 100644 index 0000000..1f2a9b6 --- /dev/null +++ b/lib/playbook/plugin/playbook/File.js @@ -0,0 +1,18 @@ +/** + * Constructor. + * name {DOMString} name of the file, without path information + * fullPath {DOMString} the full path of the file, including the name + * type {DOMString} mime type + * lastModifiedDate {Date} last modified date + * size {Number} size of the file in bytes + */ + +var File = function(name, fullPath, type, lastModifiedDate, size){ + this.name = name || ''; + this.fullPath = fullPath || null; + this.type = type || null; + this.lastModifiedDate = lastModifiedDate || null; + this.size = size || 0; +}; + +module.exports = File; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/FileEntry.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/FileEntry.js b/lib/playbook/plugin/playbook/FileEntry.js new file mode 100644 index 0000000..493a0d6 --- /dev/null +++ b/lib/playbook/plugin/playbook/FileEntry.js @@ -0,0 +1,74 @@ +var utils = require('cordova/utils'), + exec = require('cordova/exec'), + Entry = require('cordova/plugin/Entry'), + FileWriter = require('cordova/plugin/FileWriter'), + File = require('cordova/plugin/File'), + FileError = require('cordova/plugin/FileError'); + +/** + * An interface representing a file on the file system. + * + * {boolean} isFile always true (readonly) + * {boolean} isDirectory always false (readonly) + * {DOMString} name of the file, excluding the path leading to it (readonly) + * {DOMString} fullPath the absolute full path to the file (readonly) + * {FileSystem} filesystem on which the file resides (readonly) + */ +var FileEntry = function(name, fullPath) { + FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath]); +}; + +utils.extend(FileEntry, Entry); + +/** + * Creates a new FileWriter associated with the file that this FileEntry represents. + * + * @param {Function} successCallback is called with the new FileWriter + * @param {Function} errorCallback is called with a FileError + */ +FileEntry.prototype.createWriter = function(successCallback, errorCallback) { + this.file(function(filePointer) { + var writer = new FileWriter(filePointer); + + if (writer.fileName === null || writer.fileName === "") { + if (typeof errorCallback === "function") { + errorCallback(new FileError(FileError.INVALID_STATE_ERR)); + } + } else { + if (typeof successCallback === "function") { + successCallback(writer); + } + } + }, errorCallback); +}; + +/** + * Returns a File that represents the current state of the file that this FileEntry represents. + * + * @param {Function} successCallback is called with the new File object + * @param {Function} errorCallback is called with a FileError + */ +FileEntry.prototype.file = function(successCallback, errorCallback) { + var win = typeof successCallback !== 'function' ? null : function(f) { + var file = new File(f.name, f.fullPath, f.type, f.lastModifiedDate, f.size); + successCallback(file); + }; + var fail = typeof errorCallback !== 'function' ? null : function(code) { + errorCallback(new FileError(code)); + }; + console.log('getting file properties'); + + var theFileProperties = blackberry.io.file.getFileProperties(this.fullPath); + var theFile = {}; + console.log(this.fullPath); + console.log(this.name); + //theFile.name = + theFile.fullPath = this.fullPath; + theFile.type = theFileProperties.fileExtension; + theFile.lastModifiedDate = theFileProperties.dateModified; + theFile.size = theFileProperties.size; + //exec(win, fail, "File", "getFileMetadata", [this.fullPath]); +}; + + +module.exports = FileEntry; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/FileReader.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/FileReader.js b/lib/playbook/plugin/playbook/FileReader.js new file mode 100644 index 0000000..da890f9 --- /dev/null +++ b/lib/playbook/plugin/playbook/FileReader.js @@ -0,0 +1,255 @@ +var FileError = require('cordova/plugin/FileError'), + ProgressEvent = require('cordova/plugin/ProgressEvent'); + +/** + * This class reads the mobile device file system. + * + * For Android: + * The root directory is the root of the file system. + * To read from the SD card, the file name is "sdcard/my_file.txt" + * @constructor + */ +var FileReader = function() { + this.fileName = ""; + + this.readyState = 0; // FileReader.EMPTY + + // File data + this.result = null; + + // Error + this.error = null; + + // Event handlers + this.onloadstart = null; // When the read starts. + this.onprogress = null; // While reading (and decoding) file or fileBlob data, and reporting partial file data (progess.loaded/progress.total) + this.onload = null; // When the read has successfully completed. + this.onerror = null; // When the read has failed (see errors). + this.onloadend = null; // When the request has completed (either in success or failure). + this.onabort = null; // When the read has been aborted. For instance, by invoking the abort() method. +}; + +// States +FileReader.EMPTY = 0; +FileReader.LOADING = 1; +FileReader.DONE = 2; + +/** + * Abort reading file. + */ +FileReader.prototype.abort = function() { + this.result = null; + + if (this.readyState == FileReader.DONE || this.readyState == FileReader.EMPTY) { + return; + } + + this.readyState = FileReader.DONE; + + // If abort callback + if (typeof this.onabort === 'function') { + this.onabort(new ProgressEvent('abort', {target:this})); + } + // If load end callback + if (typeof this.onloadend === 'function') { + this.onloadend(new ProgressEvent('loadend', {target:this})); + } +}; + +/** + * Read text file. + * + * @param file {File} File object containing file properties + * @param encoding [Optional] (see http://www.iana.org/assignments/character-sets) + */ +FileReader.prototype.readAsText = function(file, encoding) { + // Figure out pathing + this.fileName = ''; + if (typeof file.fullPath === 'undefined') { + this.fileName = file; + } else { + this.fileName = file.fullPath; + } + + // Already loading something + if (this.readyState == FileReader.LOADING) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + // LOADING state + this.readyState = FileReader.LOADING; + + // If loadstart callback + if (typeof this.onloadstart === "function") { + this.onloadstart(new ProgressEvent("loadstart", {target:this})); + } + + // Default encoding is UTF-8 + var enc = encoding ? encoding : "UTF-8"; + + var me = this; + console.log('read as text'); + if(blackberry.io.file.exists(this.fileName)){ + + } + // Read file + /* + exec( + // Success callback + function(r) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileReader.DONE) { + return; + } + + // Save result + me.result = r; + + // If onload callback + if (typeof me.onload === "function") { + me.onload(new ProgressEvent("load", {target:me})); + } + + // DONE state + me.readyState = FileReader.DONE; + + // If onloadend callback + if (typeof me.onloadend === "function") { + me.onloadend(new ProgressEvent("loadend", {target:me})); + } + }, + // Error callback + function(e) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileReader.DONE) { + return; + } + + // DONE state + me.readyState = FileReader.DONE; + + // null result + me.result = null; + + // Save error + me.error = new FileError(e); + + // If onerror callback + if (typeof me.onerror === "function") { + me.onerror(new ProgressEvent("error", {target:me})); + } + + // If onloadend callback + if (typeof me.onloadend === "function") { + me.onloadend(new ProgressEvent("loadend", {target:me})); + } + }, "File", "readAsText", [this.fileName, enc]); + */ +}; + + +/** + * Read file and return data as a base64 encoded data url. + * A data url is of the form: + * data:[][;base64], + * + * @param file {File} File object containing file properties + */ +FileReader.prototype.readAsDataURL = function(file) { + this.fileName = ""; + if (typeof file.fullPath === "undefined") { + this.fileName = file; + } else { + this.fileName = file.fullPath; + } + + // Already loading something + if (this.readyState == FileReader.LOADING) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + // LOADING state + this.readyState = FileReader.LOADING; + + // If loadstart callback + if (typeof this.onloadstart === "function") { + this.onloadstart(new ProgressEvent("loadstart", {target:this})); + } + + var me = this; + console.log('read as url'); + // Read file + /* + exec( + // Success callback + function(r) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileReader.DONE) { + return; + } + + // DONE state + me.readyState = FileReader.DONE; + + // Save result + me.result = r; + + // If onload callback + if (typeof me.onload === "function") { + me.onload(new ProgressEvent("load", {target:me})); + } + + // If onloadend callback + if (typeof me.onloadend === "function") { + me.onloadend(new ProgressEvent("loadend", {target:me})); + } + }, + // Error callback + function(e) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileReader.DONE) { + return; + } + + // DONE state + me.readyState = FileReader.DONE; + + me.result = null; + + // Save error + me.error = new FileError(e); + + // If onerror callback + if (typeof me.onerror === "function") { + me.onerror(new ProgressEvent("error", {target:me})); + } + + // If onloadend callback + if (typeof me.onloadend === "function") { + me.onloadend(new ProgressEvent("loadend", {target:me})); + } + }, "File", "readAsDataURL", [this.fileName]); + */ +}; + +/** + * Read file and return data as a binary data. + * + * @param file {File} File object containing file properties + */ +FileReader.prototype.readAsBinaryString = function(file) { + // TODO - Can't return binary data to browser. + console.log('method "readAsBinaryString" is not supported at this time.'); +}; + +/** + * Read file and return data as a binary data. + * + * @param file {File} File object containing file properties + */ +FileReader.prototype.readAsArrayBuffer = function(file) { + // TODO - Can't return binary data to browser. + console.log('This method is not supported at this time.'); +}; + +module.exports = FileReader; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/FileWriter.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/FileWriter.js b/lib/playbook/plugin/playbook/FileWriter.js new file mode 100644 index 0000000..ee43231 --- /dev/null +++ b/lib/playbook/plugin/playbook/FileWriter.js @@ -0,0 +1,255 @@ +var FileError = require('cordova/plugin/FileError'), + ProgressEvent = require('cordova/plugin/ProgressEvent'); + +/** + * @constructor + * @param file {File} File object containing file properties + * @param append if true write to the end of the file, otherwise overwrite the file + */ +var FileWriter = function(file) { + this.fileName = ""; + this.length = 0; + if (file) { + this.fileName = file.fullPath || file; + this.length = file.size || 0; + } + // default is to write at the beginning of the file + this.position = 0; + + this.readyState = 0; // EMPTY + + this.result = null; + + // Error + this.error = null; + + // Event handlers + this.onwritestart = null; // When writing starts + this.onprogress = null; // While writing the file, and reporting partial file data + this.onwrite = null; // When the write has successfully completed. + this.onwriteend = null; // When the request has completed (either in success or failure). + this.onabort = null; // When the write has been aborted. For instance, by invoking the abort() method. + this.onerror = null; // When the write has failed (see errors). +}; + +// States +FileWriter.INIT = 0; +FileWriter.WRITING = 1; +FileWriter.DONE = 2; + +/** + * Abort writing file. + */ +FileWriter.prototype.abort = function() { + // check for invalid state + if (this.readyState === FileWriter.DONE || this.readyState === FileWriter.INIT) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + // set error + this.error = new FileError(FileError.ABORT_ERR); + + this.readyState = FileWriter.DONE; + + // If abort callback + if (typeof this.onabort === "function") { + this.onabort(new ProgressEvent("abort", {"target":this})); + } + + // If write end callback + if (typeof this.onwriteend === "function") { + this.onwriteend(new ProgressEvent("writeend", {"target":this})); + } +}; + +/** + * Writes data to the file + * + * @param text to be written + */ +FileWriter.prototype.write = function(text) { + // Throw an exception if we are already writing a file + if (this.readyState === FileWriter.WRITING) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + // WRITING state + this.readyState = FileWriter.WRITING; + + var me = this; + + // If onwritestart callback + if (typeof me.onwritestart === "function") { + me.onwritestart(new ProgressEvent("writestart", {"target":me})); + } + + // Write file + // TODO: Need to think about how to make this asynch + console.log('writing to file'); + var textBlob = blackberry.utils.stringToBlob(text); + blackberry.io.file.saveFile(this.fileName, textBlob); + me.readyState = FileWriter.DONE; + /* + exec( + // Success callback + function(r) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileWriter.DONE) { + return; + } + + // position always increases by bytes written because file would be extended + me.position += r; + // The length of the file is now where we are done writing. + + me.length = me.position; + + // DONE state + me.readyState = FileWriter.DONE; + + // If onwrite callback + if (typeof me.onwrite === "function") { + me.onwrite(new ProgressEvent("write", {"target":me})); + } + + // If onwriteend callback + if (typeof me.onwriteend === "function") { + me.onwriteend(new ProgressEvent("writeend", {"target":me})); + } + }, + // Error callback + function(e) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileWriter.DONE) { + return; + } + + // DONE state + me.readyState = FileWriter.DONE; + + // Save error + me.error = new FileError(e); + + // If onerror callback + if (typeof me.onerror === "function") { + me.onerror(new ProgressEvent("error", {"target":me})); + } + + // If onwriteend callback + if (typeof me.onwriteend === "function") { + me.onwriteend(new ProgressEvent("writeend", {"target":me})); + } + }, "File", "write", [this.fileName, text, this.position]); + */ +}; + +/** + * Moves the file pointer to the location specified. + * + * If the offset is a negative number the position of the file + * pointer is rewound. If the offset is greater than the file + * size the position is set to the end of the file. + * + * @param offset is the location to move the file pointer to. + */ +FileWriter.prototype.seek = function(offset) { + // Throw an exception if we are already writing a file + if (this.readyState === FileWriter.WRITING) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + if (!offset && offset !== 0) { + return; + } + + // See back from end of file. + if (offset < 0) { + this.position = Math.max(offset + this.length, 0); + } + // Offset is bigger then file size so set position + // to the end of the file. + else if (offset > this.length) { + this.position = this.length; + } + // Offset is between 0 and file size so set the position + // to start writing. + else { + this.position = offset; + } +}; + +/** + * Truncates the file to the size specified. + * + * @param size to chop the file at. + */ +FileWriter.prototype.truncate = function(size) { + // Throw an exception if we are already writing a file + if (this.readyState === FileWriter.WRITING) { + throw new FileError(FileError.INVALID_STATE_ERR); + } + + // WRITING state + this.readyState = FileWriter.WRITING; + + var me = this; + + // If onwritestart callback + if (typeof me.onwritestart === "function") { + me.onwritestart(new ProgressEvent("writestart", {"target":this})); + } + + // Write file + /* + exec( + // Success callback + function(r) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileWriter.DONE) { + return; + } + + // DONE state + me.readyState = FileWriter.DONE; + + // Update the length of the file + me.length = r; + me.position = Math.min(me.position, r); + + // If onwrite callback + if (typeof me.onwrite === "function") { + me.onwrite(new ProgressEvent("write", {"target":me})); + } + + // If onwriteend callback + if (typeof me.onwriteend === "function") { + me.onwriteend(new ProgressEvent("writeend", {"target":me})); + } + }, + // Error callback + function(e) { + // If DONE (cancelled), then don't do anything + if (me.readyState === FileWriter.DONE) { + return; + } + + // DONE state + me.readyState = FileWriter.DONE; + + // Save error + me.error = new FileError(e); + + // If onerror callback + if (typeof me.onerror === "function") { + me.onerror(new ProgressEvent("error", {"target":me})); + } + + // If onwriteend callback + if (typeof me.onwriteend === "function") { + me.onwriteend(new ProgressEvent("writeend", {"target":me})); + } + }, "File", "truncate", [this.fileName, size]); + */ +}; + +module.exports = FileWriter; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/5e937b29/lib/playbook/plugin/playbook/requestFileSystem.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/requestFileSystem.js b/lib/playbook/plugin/playbook/requestFileSystem.js new file mode 100644 index 0000000..28c7ab0 --- /dev/null +++ b/lib/playbook/plugin/playbook/requestFileSystem.js @@ -0,0 +1,45 @@ +var DirectoryEntry = require('cordova/plugin/DirectoryEntry'), +FileError = require('cordova/plugin/FileError'), +FileSystem = require('cordova/plugin/FileSystem'); + var exec = require('cordova/exec'); + +/** + * Request a file system in which to store application data. + * @param type local file system type + * @param size indicates how much storage space, in bytes, the application expects to need + * @param successCallback invoked with a FileSystem object + * @param errorCallback invoked if error occurs retrieving file system + */ +var requestFileSystem = function(type, size, successCallback, errorCallback) { + var fail = function(code) { + if (typeof errorCallback === 'function') { + errorCallback(new FileError(code)); + } + }; + + if (type < 0 || type > 3) { + fail(FileError.SYNTAX_ERR); + } else { + // if successful, return a FileSystem object + var success = function(file_system) { + if (file_system) { + if (typeof successCallback === 'function') { + // grab the name and root from the file system object + //var result = new FileSystem(file_system.name, file_system.root); + console.log('file_system.name ' + file_system.name); + console.log('file_system.root ' + file_system.root); + successCallback(file_system); + } + } + else { + // no FileSystem object returned + fail(FileError.NOT_FOUND_ERR); + } + }; + + // TODO: add some try/catch to support win/fail + var theFileSystem = new FileSystem('myApp', new DirectoryEntry('root', blackberry.io.dir.appDirs.app.storage.path)); + success(theFileSystem); + } +}; +module.exports = requestFileSystem;