[Filesystem] - common errors and findFilesystem function
authorAndrzej Popowski <a.popowski@samsung.com>
Mon, 2 Nov 2015 12:04:15 +0000 (13:04 +0100)
committerAndrzej Popowski <a.popowski@samsung.com>
Tue, 3 Nov 2015 13:53:22 +0000 (14:53 +0100)
Change-Id: Id5f732a8d78b656f6f0bdb17b651575cc3d48265
Signed-off-by: Andrzej Popowski <a.popowski@samsung.com>
src/file/cordova_file_api.js
src/file/js/Errors.js [new file with mode: 0644]
src/file/js/fileSystems-roots.js
src/file/js/rootsUtils.js [new file with mode: 0644]

index 0df4c7a7e2ca6755af444ecd61f8df96aa98b309..d28e754e2c42f6f3293cb83ef73f913cfb0b7819 100755 (executable)
@@ -14,6 +14,8 @@
  *    limitations under the License.
  */
 
+//= require('Errors.js');
+//= require('rootsUtils.js');
 //= require('DirectoryEntry.js');
 //= require('DirectoryReader.js');
 //= require('Entry.js');
diff --git a/src/file/js/Errors.js b/src/file/js/Errors.js
new file mode 100644 (file)
index 0000000..a0e71b0
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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.
+ */
+
+/**
+ * Function converting a tizen error to a cordova error
+ *
+ * {unsigned short} WebAPIError error code
+ */
+function ConvErrorCode(err_code) {
+  switch (err_code) {
+    case WebAPIException.INVALID_VALUES_ERR:
+      return FileError.ENCODING_ERR;
+
+    case WebAPIException.NOT_FOUND_ERR:
+      return FileError.NOT_FOUND_ERR;
+
+    default:
+      return FileError.ENCODING_ERR;
+  }
+}
index 6b85f63fd000f54193e8c1774a24ea9c0f110279..86229f84882840b8418634639607937e43ea09ba 100644 (file)
 cordova.define('cordova-plugin-file.tizen.fileSystems-roots', function(require, exports, module) {
 // TODO: remove -> end
 
-var info = (function() {
-  var roots_to_resolve = [
-    {
-      filesystemName: 'temporary',
-      name: '',
-      fullPath: '',
-      nativeURL: 'wgt-private-tmp'
-    },
-    {
-      filesystemName: 'persistent',
-      name: '',
-      fullPath: '',
-      nativeURL: 'wgt-private'
-    }
-  ];
+var channel = require('cordova/channel');
 
-  var roots = [
-    {
-      filesystemName: 'root',
-      name: '',
-      fullPath: 'file:///',
-      nativeURL: '/'
-    }
-  ];
-
-  function getRoots(successCallback) {
-    if (roots_to_resolve.length > 0) {
-      tizen.filesystem.resolve(roots_to_resolve[0].nativeURL, function(dir) {
-        roots_to_resolve[0].fullPath = dir.toURI();
-        roots_to_resolve[0].name = roots_to_resolve[0].fullPath.replace(/^.*(\\|\/|\:)/, '');  // extract name of the directory
-        roots.push(roots_to_resolve[0]);
-        roots_to_resolve.splice(0, 1); // remove first item
-
-        // we've resolved one root, check if there are any other
-        getRoots(successCallback);
-      }, function(e) {
-        console.error(e);
-        // in case of an error, return the roots we have so far
-        successCallback(roots);
-      });
-    } else {
-      successCallback(roots.slice());
-    }
-  }
-
-  return {
-    getRoots: getRoots
-  };
-})();
+channel.waitForInitialization('onGetRootsReady');
+channel.onCordovaReady.subscribe(function() {
+  rootsUtils.getRoots(function () {
+    channel.initializationComplete('onGetRootsReady');
+  });
+});
 
 module.exports = {
-    requestAllFileSystems: function(successCallback, errorCallback, args) {
-      info.getRoots(successCallback);
+  requestAllFileSystems: function(successCallback, errorCallback, args) {
+    rootsUtils.getRoots(successCallback);
   }
 };
 
diff --git a/src/file/js/rootsUtils.js b/src/file/js/rootsUtils.js
new file mode 100644 (file)
index 0000000..e8eb953
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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.
+ */
+
+var rootsUtils = (function() {
+  var roots_to_resolve = [
+    {
+      filesystemName: 'temporary',
+      name: '',
+      fullPath: '',
+      nativeURL: 'wgt-private-tmp'
+    },
+    {
+      filesystemName: 'persistent',
+      name: '',
+      fullPath: '',
+      nativeURL: 'wgt-private'
+    }
+  ];
+
+  var roots = [
+    {
+      filesystemName: 'root',
+      name: '',
+      fullPath: '/',
+      nativeURL: '/'
+    }
+  ];
+
+  function getRoots(successCallback) {
+    if (roots_to_resolve.length > 0) {
+      tizen.filesystem.resolve(roots_to_resolve[0].nativeURL, function(dir) {
+        roots_to_resolve[0].fullPath = dir.toURI().substr(7);
+        roots_to_resolve[0].name = roots_to_resolve[0].fullPath.replace(/^.*(\\|\/|\:)/, '');  // extract name of the directory
+        roots.push(roots_to_resolve[0]);
+        roots_to_resolve.splice(0, 1); // remove first item
+
+        // we've resolved one root, check if there are any other
+        getRoots(successCallback);
+      }, function(e) {
+        console.error(e);
+        // in case of an error, return the roots we have so far
+        successCallback(roots);
+      });
+    } else {
+      successCallback(roots.slice());
+    }
+  }
+
+  function strncmp(str1, str2, n) {
+    str1 = str1.substring(0, n);
+    str2 = str2.substring(0, n);
+    return ((str1 === str2) ? 0 : ((str1 > str2) ? 1 : -1 ));
+  }
+
+  function findFilesystem(url) {
+    for (var i = roots.length - 1; i >= 0; i--) {
+      if (url.indexOf('file://') == 0) {
+        url = url.substr(7);
+      }
+
+      if (url.charAt(0) == '/') {
+        if (url == roots[i].fullPath) {
+          return roots[i];
+        }
+
+        if (strncmp(url, roots[i].fullPath + '/', roots[i].fullPath.length + 1) == 0) {
+          return roots[i];
+        }
+      } else {
+        if (url == roots[i].nativeURL) {
+          return roots[i];
+        }
+      }
+    }
+
+    return roots[0];  // root filesystem
+  }
+
+  return {
+    getRoots: getRoots,
+    findFilesystem: findFilesystem
+  };
+})();