Update change log and spec for wrt-plugins-tizen_0.4.70
[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
21 #include "IManager.h"
22 #include "IPath.h"
23 #include <Commons/Exception.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include "FilesystemUtils.h"
27 #include "JSFile.h"
28 #include "Encodings.h"
29 #include "JSStorage.h"
30 #include <Logger.h>
31
32 namespace {
33 const char* PROPERTY_FILEFILTER_NAME = "name";
34 const char* PROPERTY_FILEFILTER_START_CREATED = "startCreated";
35 const char* PROPERTY_FILEFILTER_END_CREATED = "endCreated";
36 const char* PROPERTY_FILEFILTER_START_MODIFIED = "startModified";
37 const char* PROPERTY_FILEFILTER_END_MODIFIED = "endModified";
38 const char* ACCESS_MODE_READ = "r";
39 const char* ACCESS_MODE_APPEND = "a";
40 const char* ACCESS_MODE_WRITE = "w";
41 const char* ACCESS_MODE_READ_WRITE = "rw";
42 const char* STORAGE_TYPE_STATE_MOUNTED = "MOUNTED";
43 const char* STORAGE_TYPE_STATE_REMOVED = "REMOVED";
44 const char* STORAGE_TYPE_STATE_UNMOUNTABLE = "REMOVED";
45 const char* STORAGE_TYPE_INTERNAL = "INTERNAL";
46 const char* STORAGE_TYPE_EXTERNAL = "EXTERNAL";
47
48
49 const char* ENCODINGS[] = {
50     DeviceAPI::Filesystem::Encodings::UTF8,
51     DeviceAPI::Filesystem::Encodings::ISO88591,
52     DeviceAPI::Filesystem::Encodings::SJIS,
53     NULL
54 };
55 } // namespace
56
57 using namespace WrtDeviceApis;
58 using namespace WrtDeviceApis::Commons;
59 using namespace WrtDeviceApis::CommonsJavaScript;
60
61 namespace DeviceAPI {
62 namespace Filesystem{
63 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
64 {
65 }
66
67 JSValueRef Converter::toJSValueRef(
68         const NodeList& arg,
69         const JSFile::PrivateObjectDef::PermissionList &parentPermissions,
70         JSContextRef context, DeviceAPI::Common::SecurityAccessor *securityAccessor)
71 {
72     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
73     if (!jsResult) {
74         ThrowMsg(Commons::ConversionException,
75                  "Could not create js array object");
76     }
77
78     for (std::size_t i = 0; i < arg.size(); ++i)
79     {
80         JSFile::PrivateObjectDefPtr privData(
81                 new JSFile::PrivateObjectDef(
82                     arg[i],
83                     parentPermissions));
84         JSFile::PrivateObject* privateObject = new JSFile::PrivateObject(
85                 context,
86                 privData);
87                 
88                 if (securityAccessor)
89                         privateObject->copyAceCheckAccessFunction(securityAccessor);
90                 
91         JSObjectRef jsObject = JSObjectMake(m_context,
92                                             JSFile::getClassRef(),
93                                             privateObject);
94         if (!jsObject) {
95             delete privateObject;
96             ThrowMsg(Commons::ConversionException,
97                      "Could not create JS object.");
98         }
99         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
100             ThrowMsg(Commons::ConversionException,
101                      "Could not insert value into js array");
102         }
103     }
104
105     return jsResult;
106 }
107
108
109 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
110 {
111         JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
112         if (!result) {
113                 ThrowMsg(Commons::ConversionException, "Could not create array object.");
114         }
115
116         for (std::size_t i = 0; i < num; ++i) {
117                 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
118                 if (!JSSetArrayElement(m_context, result, i, value)) {
119                         ThrowMsg(Commons::ConversionException, "Could not fill array.");
120                 }
121         }
122
123         return result;
124 }
125
126 JSValueRef Converter::toJSValueRef(unsigned long long arg)
127 {
128     return JSValueMakeNumber(m_context, arg);
129 }
130
131
132
133 IPathPtr Converter::toPath(const JSValueRef& arg)
134 {
135         Try {
136                 std::string path = toString(arg);
137                 if (!Utils::isPathValid(path)) {
138                         ThrowMsg(Commons::InvalidArgumentException, "Invalid path component.");
139                 }
140                 return IPath::create(path);
141         } Catch (Commons::ConversionException) {
142                 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
143         }
144 }
145 std::string Converter::checkPercentSign(std::string& arg)
146 {       
147         size_t pos = 0;
148
149         pos = arg.find("%");
150
151         if (pos == std::string::npos) 
152         {
153                 return arg;
154         }
155         else if (arg[arg.size() - 1] == '%') 
156         {
157                 arg[arg.size() - 1] = ')';      
158
159                 if (arg[0] == '%') 
160                 {
161                         arg[0] = '(';
162                 }
163                 else 
164                 {
165                         arg.insert(0, "^");
166                         arg.insert(1, "(");
167                 }
168
169                 return arg;
170         }
171         ThrowMsg(Commons::ConversionException, "Invalid Filter");
172 }
173
174 NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
175 {
176         JSObjectRef filter = toJSObjectRef(arg);
177
178         NodeFilterPtr result(new NodeFilter());
179         JSValueRef prop = NULL;
180         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
181         if (prop) {
182                 std::string nameFilter = toString(prop);
183                 result->setName(checkPercentSign(nameFilter));
184         }
185
186         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
187         if (prop) {
188                 result->setMinCreated(toDateTimeT(prop));
189         }
190
191         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
192         if (prop) {
193                 result->setMaxCreated(toDateTimeT(prop));
194         }
195
196         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
197         if (prop) {
198                 result->setMinModified(toDateTimeT(prop));
199         }
200
201         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
202         if (prop) {
203                 result->setMaxModified(toDateTimeT(prop));
204         }
205
206         return result;
207 }
208
209 AccessMode Converter::toAccessMode(const JSValueRef& arg)
210 {
211         std::string mode = toString_(arg);
212         if (ACCESS_MODE_READ == mode) {
213                 return AM_READ;
214         } else if (ACCESS_MODE_APPEND == mode) {
215                 return AM_APPEND;
216         } else if (ACCESS_MODE_WRITE == mode) {
217                 return AM_WRITE;
218         } else if (ACCESS_MODE_READ_WRITE == mode) {
219                 return AM_READ_WRITE;
220         }
221         
222         ThrowMsg(Commons::ConversionException, "Invalid mode.");
223 }
224
225 std::string Converter::toEncoding(const JSValueRef& arg)
226 {
227         std::string result = toString_(arg);
228         const char** ptr = ENCODINGS;
229         while (*ptr) {
230                 if (result == *ptr) {
231                         return result;
232                 }
233                 ++ptr;
234         }
235         ThrowMsg(Commons::ConversionException, "Invalid encoding");
236 }
237 JSValueRef Converter::toStorageState(const short type)
238 {
239         switch (type) 
240         {
241                 case JSStorage::STATE_MOUNTED:
242                         return toJSValueRef(STORAGE_TYPE_STATE_MOUNTED);
243                 case JSStorage::STATE_REMOVED:
244                         return toJSValueRef(STORAGE_TYPE_STATE_REMOVED);
245                 case JSStorage::STATE_UNMOUNTABLE:
246                         return toJSValueRef(STORAGE_TYPE_STATE_UNMOUNTABLE);
247         }
248         ThrowMsg(Commons::ConversionException, "Invalid storage type");
249 }
250
251 JSValueRef Converter::toStorageType(const short state)
252 {
253         switch (state) 
254         {
255                 case StorageProperties::TYPE_INTERNAL:
256                         return toJSValueRef(STORAGE_TYPE_INTERNAL);
257                 case StorageProperties::TYPE_EXTERNAL:
258                         return toJSValueRef(STORAGE_TYPE_EXTERNAL);
259         }
260         ThrowMsg(Commons::ConversionException, "Invalid storage state");
261 }
262
263 JSValueRef Converter::toJSValueRef(
264         const StoragePropertiesPtr &arg,
265         JSContextRef context)
266 {
267         StorageProperties tmpStorage;
268
269         tmpStorage.setLabel(arg->getLabel()); 
270         tmpStorage.setType(arg->getType());
271
272         switch (arg->getState()) {
273         case StorageProperties::STATE_MOUNTED :
274         case StorageProperties::STATE_MOUNTED_READONLY :
275                 tmpStorage.setState(JSStorage::STATE_MOUNTED);
276                 break;
277         case StorageProperties::STATE_REMOVED:
278                 tmpStorage.setState(JSStorage::STATE_REMOVED);
279                 break;
280         case StorageProperties::STATE_UNMOUNTABLE:
281                 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
282                 break;
283         }
284
285         JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
286         if (!jsObject) {
287                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
288         }
289
290         return toJSValueRef(jsObject);
291 }
292
293 JSValueRef Converter::toJSValueRef(
294         const std::vector<StoragePropertiesPtr>& arg,
295         JSContextRef context)
296 {
297         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
298         if (!jsResult) {
299                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
300         }
301
302         StorageProperties tmpStorage;
303
304         for (size_t i = 0; i < arg.size(); i++) {
305                 tmpStorage.setLabel(arg[i]->getLabel()); 
306                 tmpStorage.setType(arg[i]->getType());
307
308                 switch (arg[i]->getState()) {
309                 case StorageProperties::STATE_MOUNTED :
310                 case StorageProperties::STATE_MOUNTED_READONLY :
311                         tmpStorage.setState(JSStorage::STATE_MOUNTED);
312                         break;
313                 case StorageProperties::STATE_REMOVED:
314                         tmpStorage.setState(JSStorage::STATE_REMOVED);
315                         break;
316                 case StorageProperties::STATE_UNMOUNTABLE:
317                         tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
318                         break;
319                 }
320
321                 JSObjectRef jsObject = JSObjectMake(m_context, NULL, NULL);
322                 const ScopedJSStringRef labelStr(JSStringCreateWithUTF8CString("label"));
323                 const ScopedJSStringRef typeStr(JSStringCreateWithUTF8CString("type"));
324                 const ScopedJSStringRef stateStr(JSStringCreateWithUTF8CString("state"));
325
326                 JSObjectSetProperty(m_context, jsObject, labelStr.get(), toJSValueRef(tmpStorage.getLabel()), kJSPropertyAttributeReadOnly, NULL);
327                 JSObjectSetProperty(m_context, jsObject, typeStr.get(), toStorageType(tmpStorage.getType()), kJSPropertyAttributeReadOnly, NULL);
328                 JSObjectSetProperty(m_context, jsObject, stateStr.get(), toStorageState(tmpStorage.getState()), kJSPropertyAttributeReadOnly, NULL);
329                 
330                 if (!jsObject) {
331                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
332                 }
333
334                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
335                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
336                 }
337         }
338
339         return jsResult;
340 }
341 }
342 }