2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "Converter.h"
19 #include <dpl/log/log.h>
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"
27 #include "Encodings.h"
28 #include "JSStorage.h"
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";
47 const char* ENCODINGS[] = {
48 TizenApis::Tizen1_0::Encodings::UTF8,
49 TizenApis::Tizen1_0::Encodings::ISO88591,
54 using namespace WrtDeviceApis;
55 using namespace WrtDeviceApis::Commons;
56 using namespace WrtDeviceApis::CommonsJavaScript;
60 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
64 JSValueRef Converter::toJSValueRef(
65 const Api::Filesystem::StoragePropertiesPtr &arg,
68 Api::Filesystem::StorageProperties tmpStorage;
70 tmpStorage.setLabel(arg->getLabel());
71 tmpStorage.setType(arg->getType());
73 switch (arg->getState()) {
74 case Api::Filesystem::StorageProperties::STATE_MOUNTED :
75 tmpStorage.setState(JSStorage::STATE_MOUNTED);
77 case Api::Filesystem::StorageProperties::STATE_REMOVED:
78 tmpStorage.setState(JSStorage::STATE_REMOVED);
80 case Api::Filesystem::StorageProperties::STATE_UNMOUNTABLE:
81 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
85 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
87 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
90 return toJSValueRef(jsObject);
93 JSValueRef Converter::toJSValueRef(
94 const std::vector<Api::Filesystem::StoragePropertiesPtr>& arg,
97 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
99 ThrowMsg(Commons::ConversionException, "Could not create js array object");
102 Api::Filesystem::StorageProperties tmpStorage;
104 for (size_t i = 0; i < arg.size(); i++) {
105 tmpStorage.setLabel(arg[i]->getLabel());
106 tmpStorage.setType(arg[i]->getType());
108 switch (arg[i]->getState()) {
109 case Api::Filesystem::StorageProperties::STATE_MOUNTED :
110 tmpStorage.setState(JSStorage::STATE_MOUNTED);
112 case Api::Filesystem::StorageProperties::STATE_REMOVED:
113 tmpStorage.setState(JSStorage::STATE_REMOVED);
115 case Api::Filesystem::StorageProperties::STATE_UNMOUNTABLE:
116 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
120 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
122 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
125 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
126 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
133 JSValueRef Converter::toJSValueRef(const Api::Filesystem::NodeList& arg,
134 JSContextRef context)
136 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
138 ThrowMsg(Commons::ConversionException, "Could not create js array object");
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);
145 delete privateObject;
146 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
148 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
149 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
156 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
158 JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
160 ThrowMsg(Commons::ConversionException, "Could not create array object.");
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.");
173 Api::Filesystem::IPathPtr Converter::toPath(const JSValueRef& arg)
176 std::string path = toString(arg);
177 if (!Utils::isPathValid(path)) {
178 ThrowMsg(Commons::ConversionException, "Invalid path component.");
180 return Api::Filesystem::IPath::create(path);
181 } Catch (Commons::InvalidArgumentException) {
182 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
186 Api::Filesystem::NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
188 JSObjectRef filter = toJSObjectRef(arg);
190 Api::Filesystem::NodeFilterPtr result(new Api::Filesystem::NodeFilter());
191 JSValueRef prop = NULL;
192 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
194 result->setName(toString(prop));
197 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
199 result->setMinCreated(toDateTimeT(prop));
202 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
204 result->setMaxCreated(toDateTimeT(prop));
207 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
209 result->setMinModified(toDateTimeT(prop));
212 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
214 result->setMaxModified(toDateTimeT(prop));
220 Api::Filesystem::AccessMode Converter::toAccessMode(const JSValueRef& arg)
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;
233 ThrowMsg(Commons::InvalidArgumentException, "Invalid mode.");
236 std::string Converter::toEncoding(const JSValueRef& arg)
238 std::string result = toString_(arg);
239 const char** ptr = ENCODINGS;
241 if (result == *ptr) {
246 ThrowMsg(Commons::InvalidArgumentException, "Invalid encoding");
248 JSValueRef Converter::toStorageType(const short type)
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);
259 ThrowMsg(Commons::InvalidArgumentException, "Invalid storage type");
262 JSValueRef Converter::toStorageState(const short state)
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);
271 ThrowMsg(Commons::InvalidArgumentException, "Invalid storage state");