Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Filesystem / Converter.cpp
1 /*
2  * Copyright (c) 2011 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  
18 #include "Converter.h"
19 #include <dpl/log/log.h>
20
21 #include <API/Filesystem/IManager.h>
22 #include <API/Filesystem/IPath.h>
23 #include <Commons/Exception.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include "FilesystemUtils.h"
26 #include "JSFile.h"
27 #include "Encodings.h"
28 #include "JSStorage.h"
29
30 namespace {
31 const char* PROPERTY_FILEFILTER_NAME = "name";
32 const char* PROPERTY_FILEFILTER_START_CREATED = "startCreated";
33 const char* PROPERTY_FILEFILTER_END_CREATED = "endCreated";
34 const char* PROPERTY_FILEFILTER_START_MODIFIED = "startModified";
35 const char* PROPERTY_FILEFILTER_END_MODIFIED = "endModified";
36 const char* ACCESS_MODE_READ = "r";
37 const char* ACCESS_MODE_APPEND = "a";
38 const char* ACCESS_MODE_WRITE = "w";
39 const char* ACCESS_MODE_READ_WRITE = "rw";
40 const char* STORAGE_TYPE_STATE_MOUNTED = "MOUNTED";
41 const char* STORAGE_TYPE_STATE_REMOVED = "REMOVED";
42 const char* STORAGE_TYPE_STATE_UNMOUNTABLE = "REMOVED";
43 const char* STORAGE_TYPE_INTERNAL = "INTERNAL";
44 const char* STORAGE_TYPE_EXTERNAL = "EXTERNAL";
45
46
47 const char* ENCODINGS[] = {
48     TizenApis::Tizen1_0::Encodings::UTF8,
49     TizenApis::Tizen1_0::Encodings::ISO88591,
50     NULL
51 };
52 } // namespace
53
54 using namespace WrtDeviceApis;
55 using namespace WrtDeviceApis::Commons;
56 using namespace WrtDeviceApis::CommonsJavaScript;
57
58 namespace TizenApis {
59 namespace Tizen1_0{
60 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
61 {
62 }
63
64 JSValueRef Converter::toJSValueRef(
65         const Api::Filesystem::StoragePropertiesPtr &arg,
66         JSContextRef context)
67 {
68         Api::Filesystem::StorageProperties tmpStorage;
69
70         tmpStorage.setLabel(arg->getLabel()); 
71         tmpStorage.setType(arg->getType());
72
73         switch (arg->getState()) {
74         case Api::Filesystem::StorageProperties::STATE_MOUNTED :
75                 tmpStorage.setState(JSStorage::STATE_MOUNTED);
76                 break;
77         case Api::Filesystem::StorageProperties::STATE_REMOVED:
78                 tmpStorage.setState(JSStorage::STATE_REMOVED);
79                 break;
80         case Api::Filesystem::StorageProperties::STATE_UNMOUNTABLE:
81                 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
82                 break;
83         }
84
85         JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
86         if (!jsObject) {
87                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
88         }
89
90         return toJSValueRef(jsObject);
91 }
92
93 JSValueRef Converter::toJSValueRef(
94         const std::vector<Api::Filesystem::StoragePropertiesPtr>& arg,
95         JSContextRef context)
96 {
97         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
98         if (!jsResult) {
99                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
100         }
101
102         Api::Filesystem::StorageProperties tmpStorage;
103
104         for (size_t i = 0; i < arg.size(); i++) {
105                 tmpStorage.setLabel(arg[i]->getLabel()); 
106                 tmpStorage.setType(arg[i]->getType());
107
108                 switch (arg[i]->getState()) {
109                 case Api::Filesystem::StorageProperties::STATE_MOUNTED :
110                         tmpStorage.setState(JSStorage::STATE_MOUNTED);
111                         break;
112                 case Api::Filesystem::StorageProperties::STATE_REMOVED:
113                         tmpStorage.setState(JSStorage::STATE_REMOVED);
114                         break;
115                 case Api::Filesystem::StorageProperties::STATE_UNMOUNTABLE:
116                         tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
117                         break;
118                 }
119
120                 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
121                 if (!jsObject) {
122                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
123                 }
124
125                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
126                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
127                 }
128         }
129
130         return jsResult;
131 }
132
133 JSValueRef Converter::toJSValueRef(const Api::Filesystem::NodeList& arg,
134         JSContextRef context)
135 {
136         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
137         if (!jsResult) {
138                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
139         }
140
141         for (std::size_t i = 0; i < arg.size(); ++i) {
142                 JSFile::PrivateObject* privateObject = new JSFile::PrivateObject(context, arg[i]);
143                 JSObjectRef jsObject = JSObjectMake(m_context, JSFile::getClassRef(), privateObject);
144                 if (!jsObject) {
145                         delete privateObject;
146                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
147                 }
148                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
149                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
150                 }
151         }
152
153         return jsResult;
154 }
155
156 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
157 {
158         JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
159         if (!result) {
160                 ThrowMsg(Commons::ConversionException, "Could not create array object.");
161         }
162
163         for (std::size_t i = 0; i < num; ++i) {
164                 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
165                 if (!JSSetArrayElement(m_context, result, i, value)) {
166                         ThrowMsg(Commons::ConversionException, "Could not fill array.");
167                 }
168         }
169
170         return result;
171 }
172
173 Api::Filesystem::IPathPtr Converter::toPath(const JSValueRef& arg)
174 {
175         Try {
176                 std::string path = toString(arg);
177                 if (!Utils::isPathValid(path)) {
178                         ThrowMsg(Commons::ConversionException, "Invalid path component.");
179                 }
180                 return Api::Filesystem::IPath::create(path);
181         } Catch (Commons::InvalidArgumentException) {
182                 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
183         }
184 }
185
186 Api::Filesystem::NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
187 {
188         JSObjectRef filter = toJSObjectRef(arg);
189
190         Api::Filesystem::NodeFilterPtr result(new Api::Filesystem::NodeFilter());
191         JSValueRef prop = NULL;
192         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
193         if (prop) {
194                 result->setName(toString(prop));
195         }
196
197         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
198         if (prop) {
199                 result->setMinCreated(toDateTimeT(prop));
200         }
201
202         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
203         if (prop) {
204                 result->setMaxCreated(toDateTimeT(prop));
205         }
206
207         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
208         if (prop) {
209                 result->setMinModified(toDateTimeT(prop));
210         }
211
212         prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
213         if (prop) {
214                 result->setMaxModified(toDateTimeT(prop));
215         }
216
217         return result;
218 }
219
220 Api::Filesystem::AccessMode Converter::toAccessMode(const JSValueRef& arg)
221 {
222         std::string mode = toString_(arg);
223         if (ACCESS_MODE_READ == mode) {
224                 return Api::Filesystem::AM_READ;
225         } else if (ACCESS_MODE_APPEND == mode) {
226                 return Api::Filesystem::AM_APPEND;
227         } else if (ACCESS_MODE_WRITE == mode) {
228                 return Api::Filesystem::AM_WRITE;
229         } else if (ACCESS_MODE_READ_WRITE == mode) {
230                 return Api::Filesystem::AM_WRITE;
231         }
232         
233         ThrowMsg(Commons::InvalidArgumentException, "Invalid mode.");
234 }
235
236 std::string Converter::toEncoding(const JSValueRef& arg)
237 {
238         std::string result = toString_(arg);
239         const char** ptr = ENCODINGS;
240         while (*ptr) {
241                 if (result == *ptr) {
242                         return result;
243                 }
244                 ++ptr;
245         }
246         ThrowMsg(Commons::InvalidArgumentException, "Invalid encoding");
247 }
248 JSValueRef Converter::toStorageType(const short type)
249 {
250         switch (type) 
251         {
252                 case Api::Filesystem::StorageProperties::STATE_MOUNTED:
253                         return toJSValueRef(STORAGE_TYPE_STATE_MOUNTED);
254                 case Api::Filesystem::StorageProperties::STATE_REMOVED:
255                         return toJSValueRef(STORAGE_TYPE_STATE_REMOVED);
256                 case Api::Filesystem::StorageProperties::STATE_UNMOUNTABLE:
257                         return toJSValueRef(STORAGE_TYPE_STATE_UNMOUNTABLE);
258         }
259         ThrowMsg(Commons::InvalidArgumentException, "Invalid storage type");
260 }
261
262 JSValueRef Converter::toStorageState(const short state)
263 {
264         switch (state) 
265         {
266                 case Api::Filesystem::StorageProperties::TYPE_INTERNAL:
267                         return toJSValueRef(STORAGE_TYPE_INTERNAL);
268                 case Api::Filesystem::StorageProperties::TYPE_EXTERNAL:
269                         return toJSValueRef(STORAGE_TYPE_EXTERNAL);
270         }
271         ThrowMsg(Commons::InvalidArgumentException, "Invalid storage state");
272 }
273
274
275 }
276 }