Update change log and spec for wrt-plugins-tizen_0.4.21
[platform/framework/web/wrt-plugins-tizen.git] / src / Filesystem / Converter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18  
19 #include "Converter.h"
20 #include <dpl/log/log.h>
21
22 #include "IManager.h"
23 #include "IPath.h"
24 #include <Commons/Exception.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include "FilesystemUtils.h"
27 #include "JSFile.h"
28 #include "Encodings.h"
29 #include "JSStorage.h"
30
31 namespace {
32 const char* PROPERTY_FILEFILTER_NAME = "name";
33 const char* PROPERTY_FILEFILTER_START_CREATED = "startCreated";
34 const char* PROPERTY_FILEFILTER_END_CREATED = "endCreated";
35 const char* PROPERTY_FILEFILTER_START_MODIFIED = "startModified";
36 const char* PROPERTY_FILEFILTER_END_MODIFIED = "endModified";
37 const char* ACCESS_MODE_READ = "r";
38 const char* ACCESS_MODE_APPEND = "a";
39 const char* ACCESS_MODE_WRITE = "w";
40 const char* ACCESS_MODE_READ_WRITE = "rw";
41 const char* STORAGE_TYPE_STATE_MOUNTED = "MOUNTED";
42 const char* STORAGE_TYPE_STATE_REMOVED = "REMOVED";
43 const char* STORAGE_TYPE_STATE_UNMOUNTABLE = "REMOVED";
44 const char* STORAGE_TYPE_INTERNAL = "INTERNAL";
45 const char* STORAGE_TYPE_EXTERNAL = "EXTERNAL";
46
47
48 const char* ENCODINGS[] = {
49     DeviceAPI::Filesystem::Encodings::UTF8,
50     DeviceAPI::Filesystem::Encodings::ISO88591,
51     DeviceAPI::Filesystem::Encodings::SJIS,
52     NULL
53 };
54 } // namespace
55
56 using namespace WrtDeviceApis;
57 using namespace WrtDeviceApis::Commons;
58 using namespace WrtDeviceApis::CommonsJavaScript;
59
60 namespace DeviceAPI {
61 namespace Filesystem{
62 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
63 {
64 }
65
66 JSValueRef Converter::toJSValueRef(
67         const NodeList& arg,
68         const JSFile::PrivateObjectDef::PermissionList &parentPermissions,
69         JSContextRef context)
70 {
71     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
72     if (!jsResult) {
73         ThrowMsg(Commons::ConversionException,
74                  "Could not create js array object");
75     }
76
77     for (std::size_t i = 0; i < arg.size(); ++i)
78     {
79         JSFile::PrivateObjectDefPtr privData(
80                 new JSFile::PrivateObjectDef(
81                     arg[i],
82                     parentPermissions));
83         JSFile::PrivateObject* privateObject = new JSFile::PrivateObject(
84                 context,
85                 privData);
86         JSObjectRef jsObject = JSObjectMake(m_context,
87                                             JSFile::getClassRef(),
88                                             privateObject);
89         if (!jsObject) {
90             delete privateObject;
91             ThrowMsg(Commons::ConversionException,
92                      "Could not create JS object.");
93         }
94         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
95             ThrowMsg(Commons::ConversionException,
96                      "Could not insert value into js array");
97         }
98     }
99
100     return jsResult;
101 }
102
103
104 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
105 {
106         JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
107         if (!result) {
108                 ThrowMsg(Commons::ConversionException, "Could not create array object.");
109         }
110
111         for (std::size_t i = 0; i < num; ++i) {
112                 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
113                 if (!JSSetArrayElement(m_context, result, i, value)) {
114                         ThrowMsg(Commons::ConversionException, "Could not fill array.");
115                 }
116         }
117
118         return result;
119 }
120
121 JSValueRef Converter::toJSValueRef(unsigned long long arg)
122 {
123     return JSValueMakeNumber(m_context, arg);
124 }
125
126
127
128 IPathPtr Converter::toPath(const JSValueRef& arg)
129 {
130         Try {
131                 std::string path = toString(arg);
132                 if (!Utils::isPathValid(path)) {
133                         ThrowMsg(Commons::InvalidArgumentException, "Invalid path component.");
134                 }
135                 return IPath::create(path);
136         } Catch (Commons::ConversionException) {
137                 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
138         }
139 }
140
141 NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
142 {
143         JSObjectRef filter = toJSObjectRef(arg);
144
145         NodeFilterPtr result(new NodeFilter());
146         JSValueRef prop = NULL;
147         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
148         if (prop) {
149                 result->setName(toString(prop));
150         }
151
152         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
153         if (prop) {
154                 result->setMinCreated(toDateTimeT(prop));
155         }
156
157         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
158         if (prop) {
159                 result->setMaxCreated(toDateTimeT(prop));
160         }
161
162         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
163         if (prop) {
164                 result->setMinModified(toDateTimeT(prop));
165         }
166
167         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
168         if (prop) {
169                 result->setMaxModified(toDateTimeT(prop));
170         }
171
172         return result;
173 }
174
175 AccessMode Converter::toAccessMode(const JSValueRef& arg)
176 {
177         std::string mode = toString_(arg);
178         if (ACCESS_MODE_READ == mode) {
179                 return AM_READ;
180         } else if (ACCESS_MODE_APPEND == mode) {
181                 return AM_APPEND;
182         } else if (ACCESS_MODE_WRITE == mode) {
183                 return AM_WRITE;
184         } else if (ACCESS_MODE_READ_WRITE == mode) {
185                 return AM_READ_WRITE;
186         }
187         
188         ThrowMsg(Commons::ConversionException, "Invalid mode.");
189 }
190
191 std::string Converter::toEncoding(const JSValueRef& arg)
192 {
193         std::string result = toString_(arg);
194         const char** ptr = ENCODINGS;
195         while (*ptr) {
196                 if (result == *ptr) {
197                         return result;
198                 }
199                 ++ptr;
200         }
201         ThrowMsg(Commons::ConversionException, "Invalid encoding");
202 }
203 JSValueRef Converter::toStorageState(const short type)
204 {
205         switch (type) 
206         {
207                 case JSStorage::STATE_MOUNTED:
208                         return toJSValueRef(STORAGE_TYPE_STATE_MOUNTED);
209                 case JSStorage::STATE_REMOVED:
210                         return toJSValueRef(STORAGE_TYPE_STATE_REMOVED);
211                 case JSStorage::STATE_UNMOUNTABLE:
212                         return toJSValueRef(STORAGE_TYPE_STATE_UNMOUNTABLE);
213         }
214         ThrowMsg(Commons::ConversionException, "Invalid storage type");
215 }
216
217 JSValueRef Converter::toStorageType(const short state)
218 {
219         switch (state) 
220         {
221                 case StorageProperties::TYPE_INTERNAL:
222                         return toJSValueRef(STORAGE_TYPE_INTERNAL);
223                 case StorageProperties::TYPE_EXTERNAL:
224                         return toJSValueRef(STORAGE_TYPE_EXTERNAL);
225         }
226         ThrowMsg(Commons::ConversionException, "Invalid storage state");
227 }
228
229 JSValueRef Converter::toJSValueRef(
230         const StoragePropertiesPtr &arg,
231         JSContextRef context)
232 {
233         StorageProperties tmpStorage;
234
235         tmpStorage.setLabel(arg->getLabel()); 
236         tmpStorage.setType(arg->getType());
237
238         switch (arg->getState()) {
239         case StorageProperties::STATE_MOUNTED :
240         case StorageProperties::STATE_MOUNTED_READONLY :
241                 tmpStorage.setState(JSStorage::STATE_MOUNTED);
242                 break;
243         case StorageProperties::STATE_REMOVED:
244                 tmpStorage.setState(JSStorage::STATE_REMOVED);
245                 break;
246         case StorageProperties::STATE_UNMOUNTABLE:
247                 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
248                 break;
249         }
250
251         JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
252         if (!jsObject) {
253                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
254         }
255
256         return toJSValueRef(jsObject);
257 }
258
259 JSValueRef Converter::toJSValueRef(
260         const std::vector<StoragePropertiesPtr>& arg,
261         JSContextRef context)
262 {
263         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
264         if (!jsResult) {
265                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
266         }
267
268         StorageProperties tmpStorage;
269
270         for (size_t i = 0; i < arg.size(); i++) {
271                 tmpStorage.setLabel(arg[i]->getLabel()); 
272                 tmpStorage.setType(arg[i]->getType());
273
274                 switch (arg[i]->getState()) {
275                 case StorageProperties::STATE_MOUNTED :
276                 case StorageProperties::STATE_MOUNTED_READONLY :
277                         tmpStorage.setState(JSStorage::STATE_MOUNTED);
278                         break;
279                 case StorageProperties::STATE_REMOVED:
280                         tmpStorage.setState(JSStorage::STATE_REMOVED);
281                         break;
282                 case StorageProperties::STATE_UNMOUNTABLE:
283                         tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
284                         break;
285                 }
286
287                 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
288                 if (!jsObject) {
289                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
290                 }
291
292                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
293                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
294                 }
295         }
296
297         return jsResult;
298 }
299 }
300 }