ec1bb69f4958de24aae9b8b9852f3800599433d1
[test/tct/web/api.git] /
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 // TODO: remove when added to public cordova repository -> begin
18 cordova.define('cordova-plugin-file.tizen.fileSystemPaths', function(require, exports, module) {
19 // TODO: remove -> end
20
21 var pathsPrefix = {
22   sharedDirectory: 'file:///opt/usr/media/'
23 };
24
25 function setExternalStorage(callback) {
26   var label = '';
27
28   var onError = function(error) {
29     console.error('Failed to get external storage: ' + error.message);
30     callback(pathsPrefix);
31   }
32
33   var onSuccess = function(storages) {
34     for (var i = 0; i < storages.length; ++i) {
35       if (storages[i].type === 'EXTERNAL' && storages[i].state === 'MOUNTED') {
36         label = storages[i].label;
37         break;
38       }
39     }
40
41     var onSuccessStorage = function(storage) {
42       pathsPrefix.externalRootDirectory = 'file://' + storage.fullPath + '/';
43       callback(pathsPrefix);
44     }
45
46     try {
47       if (label) {
48         tizen.filesystem.resolve(label, onSuccessStorage, onError);
49       } else {
50         callback(pathsPrefix);
51       }
52     } catch(error) {
53       console.error('Failed to resolve external storage: ' + error.message);
54       callback(pathsPrefix);
55     }
56   }
57
58   try {
59     tizen.filesystem.listStorages(onSuccess, onError);
60   } catch(error) {
61     console.error('Failed to list storages: ' + error.message);
62     callback(pathsPrefix);
63   }
64 }
65
66 function setApplicationStorageDirectory(callback) {
67   var onError = function (error) {
68     console.error('Failed to get directory: ' + error.message);
69     callback(pathsPrefix);
70   };
71
72   try {
73     tizen.filesystem.resolve('wgt-package', function(appDir) {
74       pathsPrefix.applicationDirectory = appDir.toURI() + '/';
75
76       tizen.filesystem.resolve('wgt-private', function(dataDir) {
77         pathsPrefix.applicationStorageDirectory = dataDir.toURI() + '/';
78         pathsPrefix.dataDirectory = dataDir.toURI() + '/';
79
80         tizen.filesystem.resolve('wgt-private-tmp', function(cacheDir) {
81           pathsPrefix.cacheDirectory = cacheDir.toURI() + '/';
82
83           setExternalStorage(callback);
84         }, onError, 'r');
85       }, onError, 'r');
86     }, onError, 'r');
87   } catch(error) {
88     console.error('Failed to get current application: ' + error.message);
89     callback(pathsPrefix);
90   }
91 }
92
93 module.exports = {
94   requestAllPaths: function(successCallback, errorCallback, args) {
95     // we ignore errorCallback here, as we're always reporting as much
96     // information as we currently have
97     setApplicationStorageDirectory(function(r) {
98       successCallback && successCallback(r);
99     });
100   }
101 };
102
103 //TODO: remove when added to public cordova repository -> begin
104 });
105 //TODO: remove -> end