123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- /*
- * Copyright 2011 Wolfgang Koller - http://www.gofg.at/
- *
- * Licensed 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.
- */
- /**
- * FileError interface
- * http://www.w3.org/TR/file-system-api/#the-fileerror-interface
- */
- function FileError() {
- }
- FileError.cast = function( p_code ) {
- var fe = new FileError();
- fe.code = p_code;
- return fe;
- }
- FileError.prototype.code = 0;
- FileError.NOT_FOUND_ERR = 1;
- FileError.SECURITY_ERR = 2;
- FileError.ABORT_ERR = 3;
- FileError.NOT_READABLE_ERR = 4;
- FileError.ENCODING_ERR = 5;
- FileError.NO_MODIFICATION_ALLOWED_ERR = 6;
- FileError.INVALID_STATE_ERR = 7;
- FileError.SYNTAX_ERR = 8;
- FileError.INVALID_MODIFICATION_ERR = 9;
- FileError.QUOTA_EXCEEDED_ERR = 10;
- FileError.TYPE_MISMATCH_ERR = 11;
- FileError.PATH_EXISTS_ERR = 12;
- /**
- * FileException interface
- * http://www.w3.org/TR/FileAPI/#dfn-FileException
- */
- //use this one as FileError name conflict
- function FileException() {
- }
- FileException.cast = function( p_code ) {
- var fe = new FileException();
- fe.code = p_code;
- return fe;
- }
- FileException.prototype.code = 0;
- FileException.NOT_FOUND_ERR = 1;
- FileException.SECURITY_ERR = 2;
- FileException.ABORT_ERR = 3;
- FileException.NOT_READABLE_ERR = 4;
- FileException.ENCODING_ERR = 5;
- FileException.NO_MODIFICATION_ALLOWED_ERR = 6;
- FileException.INVALID_STATE_ERR = 7;
- FileException.SYNTAX_ERR = 8;
- FileException.INVALID_MODIFICATION_ERR = 9;
- FileException.QUOTA_EXCEEDED_ERR = 10;
- FileException.TYPE_MISMATCH_ERR = 11;
- FileException.PATH_EXISTS_ERR = 12;
- /**
- * FileMetadata (Metadata) interface
- * http://www.w3.org/TR/file-system-api/#idl-def-Metadata
- */
- function Metadata(p_modificationDate ) {
- this.modificationTime = p_modificationDate || null;
- return this;
- }
- Metadata.cast = function( p_modificationDate ) {
- var md = new Metadata(p_modificationDate);
- return md;
- }
- function Flags(create, exclusive) {
- this.create = create || false;
- this.exclusive = exclusive || false;
- };
- Flags.cast = function( p_modificationDate ) {
- var md = new Metadata(p_modificationDate);
- return md;
- }
- Flags.cast = function(create, exclusive) {
- var that = new Flags(create, exclusive);
- return that;
- };
- /**
- * Entry interface
- * http://www.w3.org/TR/file-system-api/#idl-def-Entry
- */
- function Entry() {
- }
- Entry.prototype.isFile = false;
- Entry.prototype.isDirectory = false;
- Entry.prototype.name = "";
- Entry.prototype.fullPath = "";//fullpath for cordova-test = realFullPath - "/"
- Entry.prototype.filesystem = null;
- Entry.prototype.getMetadata = function( successCallback, errorCallback ) {
- // Get metadata for this entry
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "getMetadata", [this.fullPath]);
- }
- Entry.prototype.setMetadata = function( successCallback, errorCallback ) {
- //Cordova.exec(successCallback, errorCallback, "com.cordova.File", "setMetadata", [this.fullPath]);
- }
- /**
- * Copies or Move a file to a new location
- *
- * @param {Entry} parent the directory to which to copy the entry
- * @param {DOMString} newName the new name of the entry, defaults to the current name
- */
- //Entry.prototype.moveTo = function( parent, newName, successCallback, errorCallback ) {
- // }
- //Entry.prototype.copyTo = function( parent, newName, successCallback, errorCallback ) {
- // }
- Entry.prototype.toURL = function( mimeType ) {
- return "file://" + this.fullPath;
- }
- Entry.prototype.remove = function( successCallback, errorCallback ) {
- // Remove this entry
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "remove", [this.fullPath]);
- }
- Entry.prototype.getParent = function( successCallback, errorCallback ) {
- // Ask the system for our parent
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "getParent", [this.fullPath]);
- }
- /**
- * FileInfo (File) interface
- * (had to call the 'File' object FileInfo since there were name conflicts)
- * http://dev.w3.org/2006/webapi/FileAPI/#dfn-file
- */
- //function FileInfo() {
- //}
- //FileInfo.cast = function( p_name, p_fullPath, p_type, p_lastModifiedDate, p_size ) {
- // var f = new FileInfo();
- // f.name = p_name;
- // f.fullPath = p_fullPath;
- // f.type = p_type;
- // f.lastModifiedDate = p_lastModifiedDate;
- // f.size = p_size;
- // return f;
- // }
- //FileInfo.prototype.name = "";
- //FileInfo.prototype.fullPath = "";
- //FileInfo.prototype.type = "unknown/unknown";
- //FileInfo.prototype.lastModifiedDate = null;
- //FileInfo.prototype.size = 0;
- //Rework, have to overwrite it to pass the test-cast,
- function File(name, fullPath, type, lastModifiedDate, size) {
- this.name = name || null;
- this.fullPath = fullPath || null;
- this.type = type || null;
- this.lastModifiedDate = lastModifiedDate || null;
- this.size = size || 0;
- }
- File.cast = function( p_name, p_fullPath, p_type, p_lastModifiedDate, p_size ) {
- var f = new File(p_name, p_fullPath, p_type, p_lastModifiedDate, p_size);
- return f;
- }
- /**
- * FileSaver interface
- * http://www.w3.org/TR/file-writer-api/#idl-def-FileSaver
- */
- function FileSaver() {
- }
- FileSaver.createEvent = function( p_type, p_target ) {
- var evt = {
- "type": p_type,
- "target": p_target
- };
- return evt;
- }
- FileSaver.prototype.abort = function() {
- if( this.readyState == FileSaver.INIT || this.readyState == FileSaver.DONE ) throw FileException.cast(FileException.INVALID_STATE_ERR);
- this.error = FileError.cast(FileError.ABORT_ERR);
- this.readyState = FileSaver.DONE;
- if( typeof this.onerror === "function" ) this.onerror(FileSaver.createEvent("error", this));
- if( typeof this.onabort === "function" ) this.onabort(FileSaver.createEvent("abort", this));
- if( typeof this.onwriteend === "function" ) this.onwriteend(FileSaver.createEvent("writeend", this));
- }
- FileSaver.INIT = 0;
- FileSaver.WRITING = 1;
- FileSaver.DONE = 2;
- FileSaver.prototype.readyState = FileSaver.INIT;
- FileSaver.prototype.error = new FileError();
- FileSaver.prototype.onwritestart = null;
- FileSaver.prototype.onprogress = null;
- FileSaver.prototype.onwrite = null;
- FileSaver.prototype.onabort = null;
- FileSaver.prototype.onerror = null;
- FileSaver.prototype.onwriteend = null;
- //FileSaver.prototype.fullPath = ""; // Not W3C conform, but we need the path for handling!
- /**
- * FileWriter interface
- * (derives from FileSaver)
- * http://www.w3.org/TR/file-writer-api/#idl-def-FileWriter
- */
- function FileWriter(p_file) {
- this.fullPath = p_file.fullPath || "";
- return this;
- }
- FileWriter.cast = function( p_fullPath, p_length ) {
- var tmpFile = new File(null,p_fullPath,null, null,null);
- var fw = new FileWriter(tmpFile);
- return fw;
- }
- FileWriter.prototype = new FileSaver();
- FileWriter.prototype.fullPath = "";
- FileWriter.prototype.position = 0;
- FileWriter.prototype.length = 0;
- FileWriter.prototype.write = function( data ) {
- //console.log( 'Calling write: ' + this.position + " / " + this.length );
- // Check if we are able to write
- if( this.readyState == FileSaver.WRITING ) throw FileException.cast(FileException.INVALID_STATE_ERR);
- // We are writing now
- this.readyState = FileSaver.WRITING;
- // Emit the start event
- if( typeof this.onwritestart === "function" ) this.onwritestart( FileSaver.createEvent("writestart", this) );
- // Finally do the writing
- var me = this;
- Cordova.exec(function(p_position, p_length) {
- // Update position & length for file
- me.position = p_position;
- me.length = p_length;
- // Update state
- me.readyState = FileSaver.DONE;
- // Dispatch missing events
- if( typeof me.onwrite === "function" ) me.onwrite( FileSaver.createEvent("write", me) );
- if( typeof me.onwriteend === "function" ) me.onwriteend( FileSaver.createEvent("writeend", me) );
- }, function( p_fileError, p_position, p_length ) {
- // Update position & length for file
- me.position = p_position;
- me.length = p_length;
- // Set error object & update state
- me.error = p_fileError;
- me.readyState = FileSaver.DONE;
- // Dispatch missing events
- if( typeof me.onerror === "function" ) me.onerror( FileWriter.createEvent("error", me) );
- if( typeof me.onwriteend === "function" ) me.onwriteend( FileWriter.createEvent("writeend", me) );
- }, "com.cordova.File", "write", [this.fullPath, this.position, data]);
- }
- FileWriter.prototype.seek = function( offset ) {
- //console.log( 'Calling seek: ' + offset + " / " + this.position + " / " + this.length + " / " + this.readyState );
- if( this.readyState == FileSaver.WRITING ) throw FileException.cast(FileException.INVALID_STATE_ERR);
- if( offset < 0 ) {
- this.position = Math.max(offset + this.length, 0);
- }
- else if( offset > this.length ) {
- this.position = this.length;
- }
- else {
- this.position = offset;
- }
- }
- FileWriter.prototype.truncate = function( size ) {
- // Check if we are able to write
- if( this.readyState == FileSaver.WRITING ) throw FileException.cast(FileException.INVALID_STATE_ERR);
- // We are writing now
- this.readyState = FileSaver.WRITING;
- // Emit the start event
- if( typeof this.onwritestart === "function" ) this.onwritestart( FileSaver.createEvent("writestart", this) );
- // Finally do the writing
- var me = this;
- Cordova.exec(function(p_position, p_length) {
- // Update position & length for file
- me.position = p_position;
- me.length = p_length;
- // Update state
- me.readyState = FileSaver.DONE;
- // Dispatch missing events
- if( typeof me.onwrite === "function" ) me.onwrite( FileSaver.createEvent("write", me) );
- if( typeof me.onwriteend === "function" ) me.onwriteend( FileSaver.createEvent("writeend", me) );
- }, function( p_fileError, p_position, p_length ) {
- // Update position & length for file
- me.position = p_position;
- me.length = p_length;
- // Set error object & update state
- me.error = p_fileError;
- me.readyState = FileSaver.DONE;
- // Dispatch missing events
- if( typeof me.onerror === "function" ) me.onerror( FileSaver.createEvent("error", me) );
- if( typeof me.onwriteend === "function" ) me.onwriteend( FileSaver.createEvent("writeend", me) );
- }, "com.cordova.File", "truncate", [this.fullPath, size]);
- }
- /**
- * FileReader interface
- * http://www.w3.org/TR/FileAPI/#dfn-filereader
- */
- function FileReader() {
- }
- FileReader.EMPTY = 0;
- FileReader.LOADING = 1;
- FileReader.DONE = 2;
- FileReader.prototype.readyState = FileReader.EMPTY;
- FileReader.prototype.result = "";
- FileReader.prototype.error = new FileError();
- FileReader.prototype.onloadstart = null;
- FileReader.prototype.onprogress = null;
- FileReader.prototype.onload = null;
- FileReader.prototype.onabort = null;
- FileReader.prototype.onerror = null;
- FileReader.prototype.onloadend = null;
- FileReader.prototype.readAsArrayBuffer = function( file ) {
- }
- FileReader.prototype.readAsBinaryString = function( file ) {
- }
- FileReader.prototype.readAsText = function( file ) {
- this.readyState = FileReader.EMPTY;
- this.result = null;
- this.readyState = FileReader.LOADING;
- if( typeof this.onloadstart === "function" ) this.onloadstart( FileSaver.createEvent( "loadstart", this) );
- var me = this;
- // Lets read the file...
- Cordova.exec(function( p_data ) {
- me.readyState = FileReader.DONE;
- me.result = atob( p_data );
- if( typeof me.onload === "function" ) me.onload( FileSaver.createEvent( "load", me) );
- if( typeof me.onloadend === "function" ) me.onloadend( FileSaver.createEvent( "loadend", me) );
- }, function( p_fileError ) {
- me.readyState = FileReader.DONE;
- me.result = null;
- me.error = p_fileError;
- if( typeof me.onloadend === "function" ) me.onloadend( FileSaver.createEvent( "loadend", me) );
- if( typeof me.onerror === "function" ) me.onerror( FileSaver.createEvent( "error", me) );
- }, "com.cordova.File", "readAsText", [file.fullPath]);
- }
- FileReader.prototype.readAsDataURL = function( file ) {
- this.readyState = FileReader.EMPTY;
- this.result = null;
- this.readyState = FileReader.LOADING;
- if( typeof this.onloadstart === "function" ) this.onloadstart( FileSaver.createEvent( "loadstart", this) );
- var me = this;
- // Lets read the file...
- Cordova.exec(function( p_data ) {
- me.readyState = FileReader.DONE;
- me.result = p_data;
- if( typeof me.onloadend === "function" ) me.onloadend( FileSaver.createEvent( "loadend", me) );
- }, function( p_fileError ) {
- me.readyState = FileReader.DONE;
- me.result = null;
- me.error = p_fileError;
- if( typeof me.onloadend === "function" ) me.onloadend( FileSaver.createEvent( "loadend", me) );
- if( typeof me.onerror === "function" ) me.onerror( FileSaver.createEvent( "error", me) );
- }, "com.cordova.File", "readAsDataURL", [file.fullPath]);
- }
- FileReader.prototype.abort = function() {
- this.readyState = FileReader.DONE;
- this.result = null;
- this.error = FileError.cast( FileError.ABORT_ERR );
- if( typeof this.onerror === "function" ) this.onerror( FileSaver.createEvent( "error", me) ) ;
- if( typeof this.onabort === "function" ) this.onabort( FileSaver.createEvent( "abort", me) ) ;
- if( typeof this.onloadend === "function" ) this.onloadend( FileSaver.createEvent( "loadend", me) ) ;
- }
- /**
- * FileEntry interface
- * (derives from Entry)
- * http://www.w3.org/TR/file-system-api/#the-fileentry-interface
- */
- function FileEntry() {
- this.isFile = true;
- this.isDirectory = false;
- }
- FileEntry.cast = function( filename, path ) {
- var fe = new FileEntry();
- fe.name = filename;
- fe.fullPath = path;
- return fe;
- }
- FileEntry.prototype = new Entry();
- FileEntry.prototype.createWriter = function( successCallback, errorCallback ) {
- this.file( function(p_file) {
- successCallback(FileWriter.cast(p_file.fullPath, p_file.size));
- }, errorCallback);
- }
- FileEntry.prototype.file = function( successCallback, errorCallback ) {
- // Lets get the fileinfo
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "file", [this.fullPath]);
- }
- FileEntry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "copyFile", [this.fullPath, parent.fullPath, newName]);
- };
- FileEntry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "moveFile", [this.fullPath, parent.fullPath, newName]);
- };
- /**
- * DirectoryReader interface
- * http://www.w3.org/TR/file-system-api/#the-directoryreader-interface
- */
- function DirectoryReader() {
- }
- DirectoryReader.cast = function( p_fullPath ) {
- var dr = new DirectoryReader();
- dr.fullPath = p_fullPath;
- return dr;
- }
- DirectoryReader.prototype.fullPath = ""; // Not W3C conform, but required
- DirectoryReader.prototype.readEntries = function( successCallback, errorCallback ) {
- // Get all entries for the directory
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "readEntries", [this.fullPath]);
- }
- /**
- * DirectoryEntry interface
- * (derives from Entry)
- * http://www.w3.org/TR/file-system-api/#the-directoryentry-interface
- */
- function DirectoryEntry() {
- this.isFile = false;
- this.isDirectory = true;
- }
- DirectoryEntry.cast = function( dirname, path ) {
- var de = new DirectoryEntry();
- de.name = dirname;
- de.fullPath = path;
- return de;
- }
- DirectoryEntry.prototype = new Entry();
- DirectoryEntry.prototype.createReader = function() {
- return DirectoryReader.cast(this.fullPath);
- }
- DirectoryEntry.prototype.getFile = function( path, options, successCallback, errorCallback ) {
- var requestPath = path;
- // Check for a relative path
- if( requestPath.charAt(0) != '/' ) requestPath = this.fullPath + "/" + requestPath;
- // Lets get the file
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "getFile", [requestPath, options]);
- }
- DirectoryEntry.prototype.getDirectory = function( path, options, successCallback, errorCallback ) {
- var requestPath = path;
- // Check for a relative path
- if( requestPath.charAt(0) != '/' ) requestPath = this.fullPath + "/" + requestPath;
- // Lets get the directory
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "getDirectory", [requestPath, options]);
- }
- DirectoryEntry.prototype.removeRecursively = function( successCallback, errorCallback ) {
- // Remove the directory
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "removeRecursively", [this.fullPath]);
- }
- DirectoryEntry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "copyDir", [this.fullPath, parent.fullPath, newName]);
- };
- DirectoryEntry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "moveDir", [this.fullPath, parent.fullPath, newName]);
- };
- /**
- * FileSystem interface
- * http://www.w3.org/TR/file-system-api/#the-filesystem-interface
- */
- function FileSystem() {
- }
- FileSystem.cast = function( fsname, dirname, path ) {
- var fs = new FileSystem();
- fs.name = fsname;
- fs.root = DirectoryEntry.cast(dirname, path);
- return fs;
- }
- FileSystem.prototype.name = "";
- FileSystem.prototype.root = null; // Should be a DirectoryEntry
- /**
- * LocalFileSystem interface
- * http://www.w3.org/TR/file-system-api/#using-localfilesystem
- */
- function LocalFileSystem() {
- }
- LocalFileSystem.TEMPORARY = 0;
- LocalFileSystem.PERSISTENT = 1;
- LocalFileSystem.prototype.requestFileSystem = function( type, size, successCallback, errorCallback ) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "requestFileSystem", [type,size]);
- }
- LocalFileSystem.prototype.resolveLocalFileSystemURL = function( url, successCallback, errorCallback ) {
- Cordova.exec(successCallback, errorCallback, "com.cordova.File", "resolveLocalFileSystemURL", [url]);
- }
- /**
- * Let window implement the localfilesystem
- */
- Cordova.addConstructor( "com.cordova.File", function () {
- var localFileSystem = new LocalFileSystem();
- window.requestFileSystem = localFileSystem.requestFileSystem;
- window.resolveLocalFileSystemURI = localFileSystem.resolveLocalFileSystemURL;
- } );
|