summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGubolin <gubolin@fantasymail.de>2014-09-09 11:51:31 +0200
committerGubolin <gubolin@fantasymail.de>2014-09-09 11:51:31 +0200
commit478f3d4c387d67cb54a02ba373bfb4655d7a19f7 (patch)
tree73acf4c2b864a18a6634ac6be5db4eca9bea767f
parenta7b3c537edf83b8415716f930fd3fe518638c4db (diff)
downloadsnap-478f3d4c387d67cb54a02ba373bfb4655d7a19f7.tar.gz
snap-478f3d4c387d67cb54a02ba373bfb4655d7a19f7.zip
add file blocks
-rw-r--r--blocks.js12
-rwxr-xr-xmobile.sh1
-rw-r--r--objects.js44
-rw-r--r--threads.js183
4 files changed, 240 insertions, 0 deletions
diff --git a/blocks.js b/blocks.js
index 735d36b..9aec7f4 100644
--- a/blocks.js
+++ b/blocks.js
@@ -844,6 +844,18 @@ SyntaxElementMorph.prototype.labelPart = function (spec) {
);
part.setContents(['date']);
break;
+ case '%fileOrDir':
+ part = new InputSlotMorph(
+ null, // text
+ false, // non-numeric
+ {
+ 'file': ['file'],
+ 'directory': ['directory']
+ },
+ true // read-only
+ );
+ part.setContents(['file']);
+ break;
case '%locations':
part = new InputSlotMorph(
null, // text
diff --git a/mobile.sh b/mobile.sh
index 909f7e5..b0bcc5d 100755
--- a/mobile.sh
+++ b/mobile.sh
@@ -48,6 +48,7 @@ cordova plugin add org.apache.cordova.device-motion
cordova plugin add org.apache.cordova.device-orientation
cordova plugin add org.apache.cordova.geolocation
cordova plugin add de.appplant.cordova.plugin.local-notification
+cordova plugin add org.apache.cordova.file
if [[ $1 == "android" ]]
then
diff --git a/objects.js b/objects.js
index 100bd67..d432dcb 100644
--- a/objects.js
+++ b/objects.js
@@ -883,6 +883,36 @@ SpriteMorph.prototype.initBlocks = function () {
category: 'sensing',
spec: 'current %dates'
},
+ reportFileOrDirectoryContent: {
+ type: 'reporter',
+ category: 'sensing',
+ spec: 'content of the %fileOrDir %s'
+ },
+ doSetFileContent: {
+ type: 'command',
+ category: 'sensing',
+ spec: 'write %s to the file %s'
+ },
+ doCreateFileOrDirectory: {
+ type: 'command',
+ category: 'sensing',
+ spec: 'create %fileOrDir %s'
+ },
+ reportFileOrDirectoryExists: {
+ type: 'predicate',
+ category: 'sensing',
+ spec: '%fileOrDir %s exists'
+ },
+ doDeleteFileOrDirectory: {
+ type: 'command',
+ category: 'sensing',
+ spec: 'delete the %fileOrDir %s'
+ },
+ reportHomeDirectory: {
+ type: 'reporter',
+ category: 'sensing',
+ spec: 'home directory'
+ },
doVibrate: {
type: 'command',
category: 'sensing',
@@ -1927,6 +1957,13 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push('-');
blocks.push(block('reportDate'));
blocks.push('-');
+ blocks.push(block('reportFileOrDirectoryContent'));
+ blocks.push(block('doSetFileContent'));
+ blocks.push(block('doCreateFileOrDirectory'));
+ blocks.push(block('reportFileOrDirectoryExists'));
+ blocks.push(block('doDeleteFileOrDirectory'));
+ blocks.push(block('reportHomeDirectory'));
+ blocks.push('-');
blocks.push(block('reportLanguage'));
blocks.push(block('reportLocation'));
blocks.push('-');
@@ -5049,6 +5086,13 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push('-');
blocks.push(block('reportDate'));
blocks.push('-');
+ blocks.push(block('reportFileOrDirectoryContent'));
+ blocks.push(block('doSetFileContent'));
+ blocks.push(block('doCreateFileOrDirectory'));
+ blocks.push(block('reportFileOrDirectoryExists'));
+ blocks.push(block('doDeleteFileOrDirectory'));
+ blocks.push(block('reportHomeDirectory'));
+ blocks.push('-');
blocks.push(block('reportLanguage'));
blocks.push(block('reportLocation'));
blocks.push('-');
diff --git a/threads.js b/threads.js
index 7616b88..37db5cd 100644
--- a/threads.js
+++ b/threads.js
@@ -2687,6 +2687,189 @@ Process.prototype.reportDate = function (datefn) {
return result;
};
+// Process File access
+Process.prototype.reportFileOrDirectoryContent = function (typeInput, filepath) {
+ var myself = this;
+ var type = myself.inputOption(typeInput);
+
+ if (myself.context.result == null) {
+ myself.context.result = 'wait';
+
+ if (window.resolveLocalFileSystemURL) {
+ window.resolveLocalFileSystemURL(filepath,
+ function (entry) {
+ if (type === 'file') {
+ entry.file(
+ function (file) {
+ var reader = new FileReader();
+
+ reader.onloadend = function (e) {
+ myself.context.result = this.result;
+ };
+
+ reader.readAsText(file);
+ },
+ function (error) {
+ myself.context.result = error.toString();
+ }
+ );
+ } else {
+ var dirReader = entry.createReader();
+ dirReader.readEntries(
+ function (results) {
+ var items = [];
+ results.forEach(
+ function (item) {
+ items.push(item.name);
+ }
+ );
+ myself.context.result = new List(items);
+ },
+ function (error) {
+ myself.context.result = error.toString();
+ }
+ );
+ }
+ },
+ function (error) {
+ myself.context.result = error.toString();
+ }
+ );
+ }
+ } else {
+ if (myself.context.result !== 'wait') {
+ return myself.context.result;
+ }
+ }
+
+ this.pushContext('doYield');
+ this.pushContext();
+};
+
+Process.prototype.doSetFileContent = function (content, filepath) {
+ var dirpath = filepath.substring(0,
+ filepath.lastIndexOf('/'));
+ var filename = filepath.substring(
+ filepath.lastIndexOf('/') + 1);
+
+ if (window.resolveLocalFileSystemURL) {
+ window.resolveLocalFileSystemURL(dirpath,
+ function (dirEntry) {
+ dirEntry.getFile(filename,
+ {create: true, exclusive: false},
+ function (fileEntry) {
+ fileEntry.createWriter(
+ function (fileWriter) {
+ var blob;
+
+ try {
+ blob = new Blob([content],
+ {type: 'text/plain'});
+ // No `new Blob` for Android
+ } catch (e) {
+ window.BlobBuilder =
+ window.BlobBuilder ||
+ window.WebKitBlobBuilder ||
+ window.MozBlobBuilder ||
+ window.MSBlobBuilder;
+
+ if (e.name == 'TypeError'
+ && window.BlobBuilder) {
+ var bb = new BlobBuilder();
+ bb.append(content);
+ blob = bb.getBlob('text/plain');
+ }
+ }
+
+ fileWriter.write(blob);
+ }
+ );
+ }
+ );
+ }
+ );
+ }
+};
+
+Process.prototype.createFileOrDirectory = function (typeInput, filepath) {
+ var myself = this;
+ var type = myself.inputOption(typeInput);
+
+ function createDir(rootDirEntry, folders) {
+ rootDirEntry.getDirectory(folders[0], {create: true},
+ function (dirEntry) {
+ if (folders.length === 1) {
+ if (type === 'file') {
+ dirEntry.getFile(folders.slice(1),
+ {create: true, exclusive: false});
+ return;
+ }
+ }
+
+ if (folders.length) {
+ createDir(dirEntry, folders.slice(1));
+ }
+ }
+ );
+ };
+
+ window.requestFileSystem(window.PERSISTENT, 0,
+ function (fileSystem) {
+ createDir(fileSystem.root, path.split('/'));
+ }
+ );
+};
+
+Process.prototype.reportFileOrDirectoryExists = function (typeInput, filepath) {
+ var myself = this;
+ var type = myself.inputOption(typeInput);
+
+ if (myself.context.result == null) {
+ myself.context.result = 'wait';
+
+ if (window.resolveLocalFileSystemURL) {
+ window.resolveLocalFileSystemURL(filepath,
+ function (entry) {
+ if (type === 'file') {
+ myself.context.result = !entry.isDirectory;
+ } else {
+ myself.context.result = entry.isDirectory;
+ }
+ },
+ function (error) {
+ myself.context.result = false;
+ }
+ );
+ }
+ } else {
+ if (myself.context.result !== 'wait') {
+ return myself.context.result;
+ }
+ }
+
+ this.pushContext('doYield');
+ this.pushContext();
+};
+
+Process.prototype.doDeleteFileOrDirectory = function (typeInput, filepath) {
+ if (window.resolveLocalFileSystemURL) {
+ window.resolveLocalFileSystemURL(filepath,
+ function (entry) {
+ entry.remove();
+ }
+ );
+ }
+};
+
+Process.prototype.reportHomeDirectory = function () {
+ if (cordova) {
+ if (cordova.file) {
+ return cordova.file.dataDirectory;
+ }
+ }
+ return '';
+};
+
Process.prototype.getCompassHeading = function (dest) {
var stage = this.homeContext.receiver.parentThatIsA(StageMorph);