2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "Converter.h"
20 #include <dpl/log/log.h>
24 #include <Commons/Exception.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include "FilesystemUtils.h"
28 #include "Encodings.h"
29 #include "JSStorage.h"
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";
48 const char* ENCODINGS[] = {
49 DeviceAPI::Filesystem::Encodings::UTF8,
50 DeviceAPI::Filesystem::Encodings::ISO88591,
51 DeviceAPI::Filesystem::Encodings::SJIS,
56 using namespace WrtDeviceApis;
57 using namespace WrtDeviceApis::Commons;
58 using namespace WrtDeviceApis::CommonsJavaScript;
62 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
66 JSValueRef Converter::toJSValueRef(
68 const JSFile::PrivateObjectDef::PermissionList &parentPermissions,
71 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
73 ThrowMsg(Commons::ConversionException,
74 "Could not create js array object");
77 for (std::size_t i = 0; i < arg.size(); ++i)
79 JSFile::PrivateObjectDefPtr privData(
80 new JSFile::PrivateObjectDef(
83 JSFile::PrivateObject* privateObject = new JSFile::PrivateObject(
86 JSObjectRef jsObject = JSObjectMake(m_context,
87 JSFile::getClassRef(),
91 ThrowMsg(Commons::ConversionException,
92 "Could not create JS object.");
94 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
95 ThrowMsg(Commons::ConversionException,
96 "Could not insert value into js array");
104 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
106 JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
108 ThrowMsg(Commons::ConversionException, "Could not create array object.");
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.");
121 JSValueRef Converter::toJSValueRef(unsigned long long arg)
123 return JSValueMakeNumber(m_context, arg);
128 IPathPtr Converter::toPath(const JSValueRef& arg)
131 std::string path = toString(arg);
132 if (!Utils::isPathValid(path)) {
133 ThrowMsg(Commons::InvalidArgumentException, "Invalid path component.");
135 return IPath::create(path);
136 } Catch (Commons::ConversionException) {
137 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
141 NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
143 JSObjectRef filter = toJSObjectRef(arg);
145 NodeFilterPtr result(new NodeFilter());
146 JSValueRef prop = NULL;
147 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
149 result->setName(toString(prop));
152 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
154 result->setMinCreated(toDateTimeT(prop));
157 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
159 result->setMaxCreated(toDateTimeT(prop));
162 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
164 result->setMinModified(toDateTimeT(prop));
167 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
169 result->setMaxModified(toDateTimeT(prop));
175 AccessMode Converter::toAccessMode(const JSValueRef& arg)
177 std::string mode = toString_(arg);
178 if (ACCESS_MODE_READ == mode) {
180 } else if (ACCESS_MODE_APPEND == mode) {
182 } else if (ACCESS_MODE_WRITE == mode) {
184 } else if (ACCESS_MODE_READ_WRITE == mode) {
185 return AM_READ_WRITE;
188 ThrowMsg(Commons::ConversionException, "Invalid mode.");
191 std::string Converter::toEncoding(const JSValueRef& arg)
193 std::string result = toString_(arg);
194 const char** ptr = ENCODINGS;
196 if (result == *ptr) {
201 ThrowMsg(Commons::ConversionException, "Invalid encoding");
203 JSValueRef Converter::toStorageState(const short type)
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);
214 ThrowMsg(Commons::ConversionException, "Invalid storage type");
217 JSValueRef Converter::toStorageType(const short state)
221 case StorageProperties::TYPE_INTERNAL:
222 return toJSValueRef(STORAGE_TYPE_INTERNAL);
223 case StorageProperties::TYPE_EXTERNAL:
224 return toJSValueRef(STORAGE_TYPE_EXTERNAL);
226 ThrowMsg(Commons::ConversionException, "Invalid storage state");
229 JSValueRef Converter::toJSValueRef(
230 const StoragePropertiesPtr &arg,
231 JSContextRef context)
233 StorageProperties tmpStorage;
235 tmpStorage.setLabel(arg->getLabel());
236 tmpStorage.setType(arg->getType());
238 switch (arg->getState()) {
239 case StorageProperties::STATE_MOUNTED :
240 case StorageProperties::STATE_MOUNTED_READONLY :
241 tmpStorage.setState(JSStorage::STATE_MOUNTED);
243 case StorageProperties::STATE_REMOVED:
244 tmpStorage.setState(JSStorage::STATE_REMOVED);
246 case StorageProperties::STATE_UNMOUNTABLE:
247 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
251 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
253 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
256 return toJSValueRef(jsObject);
259 JSValueRef Converter::toJSValueRef(
260 const std::vector<StoragePropertiesPtr>& arg,
261 JSContextRef context)
263 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
265 ThrowMsg(Commons::ConversionException, "Could not create js array object");
268 StorageProperties tmpStorage;
270 for (size_t i = 0; i < arg.size(); i++) {
271 tmpStorage.setLabel(arg[i]->getLabel());
272 tmpStorage.setType(arg[i]->getType());
274 switch (arg[i]->getState()) {
275 case StorageProperties::STATE_MOUNTED :
276 case StorageProperties::STATE_MOUNTED_READONLY :
277 tmpStorage.setState(JSStorage::STATE_MOUNTED);
279 case StorageProperties::STATE_REMOVED:
280 tmpStorage.setState(JSStorage::STATE_REMOVED);
282 case StorageProperties::STATE_UNMOUNTABLE:
283 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
287 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
289 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
292 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
293 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");