wrt-plugins-tizen_0.4.23
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / ApplicationConverter.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 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <Commons/RegexUtils.h>
22 #include <Commons/Exception.h>
23 #include <JSWebAPIError.h>
24
25 #include "ApplicationConverter.h"
26 #include "JSApplication.h"
27 #include "JSApplicationInformation.h"
28 #include "JSApplicationContext.h"
29 #include "JSApplicationControlData.h"
30 #include "JSApplicationControlData.h"
31 #include "JSApplicationControl.h"
32 #include "JSRequestedApplicationControl.h"
33 #include "JSApplicationCert.h"
34 #include <Logger.h>
35
36 namespace DeviceAPI {
37 namespace Application {
38
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42
43 ApplicationConverter::ApplicationConverter(JSContextRef context) : Converter(context)
44 {
45 }
46
47 ApplicationConverter::~ApplicationConverter()
48 {
49 }
50
51
52 JSValueRef ApplicationConverter::toJSValueRefFromApplication(const ApplicationPtr &arg)
53 {
54         LoggerD("entered to toJSValueRefFromApplication ");
55         if(arg == NULL) {
56                 Throw(Commons::ConversionException);
57         }
58         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplication::getClassRef(), arg);
59 }
60
61
62 JSValueRef ApplicationConverter::toJSValueRefFromApplicationCert(const ApplicationCertPtr &arg)
63 {
64         LoggerD("entered to toJSValueRefFromApplicationCert ");
65         if(arg == NULL) {
66                 Throw(Commons::ConversionException);
67         }
68         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationCert::getClassRef(), arg);
69 }
70
71
72 JSValueRef ApplicationConverter::toJSValueRefFromeApplicationCerts(const ApplicationCertArrayPtr &arg)
73 {
74         if(arg == NULL) {
75                 Throw(Commons::InvalidArgumentException);
76         }
77         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationCert, this);
78 }
79
80
81 JSValueRef ApplicationConverter::toJSValueRefFromApplicationInformation(const ApplicationInformationPtr &arg)
82 {
83         if(arg == NULL) {
84                 Throw(Commons::ConversionException);
85         }
86         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationInformation::getClassRef(), arg);
87 }
88
89 ApplicationInformationPtr ApplicationConverter::toApplicationInformation(const JSValueRef &jsValue)
90 {
91         if(JSApplicationInformation::isObjectOfClass(m_context, jsValue))
92                 return JSApplicationInformation::getApplicationInformation(m_context, jsValue);
93
94         const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_NAME));
95         const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_ID));
96         const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_ICONPATH));
97         const ScopedJSStringRef versionStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_VERSION));
98         const ScopedJSStringRef showStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_SHOW));
99         const ScopedJSStringRef categoriesStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_CATEGORIES));
100     const ScopedJSStringRef installDateStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_INSTALL_DATE));
101         const ScopedJSStringRef installSizeStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE));
102         const ScopedJSStringRef packageIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_PACKAGE_ID));
103
104         JSObjectRef jsObject = toJSObjectRef(jsValue);
105
106         JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
107         JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
108         JSValueRef iconPathData = JSObjectGetProperty(m_context, jsObject, iconPathStr.get(), NULL);
109         JSValueRef versionData = JSObjectGetProperty(m_context, jsObject, versionStr.get(), NULL);      
110         JSValueRef showData = JSObjectGetProperty(m_context, jsObject, showStr.get(), NULL);
111         JSValueRef categoriesData = JSObjectGetProperty(m_context, jsObject, categoriesStr.get(), NULL);
112         JSValueRef installDateData = JSObjectGetProperty(m_context, jsObject, installDateStr.get(), NULL);
113         JSValueRef installSizeData = JSObjectGetProperty(m_context, jsObject, installSizeStr.get(), NULL);
114         JSValueRef pkgIdStrData = JSObjectGetProperty(m_context, jsObject, packageIdStr.get(), NULL);
115
116         std::string name;
117         std::string appid;
118         std::string iconPath;
119         std::string version;
120         bool show;
121         std::vector <std::string> categories;
122         time_t installDate;
123         long installSize;
124         std::string pkgid;
125
126         ApplicationInformationPtr result(new ApplicationInformation());
127         if (!result) {
128                 Throw(Commons::ConversionException);
129         }
130
131         if (!JSValueIsUndefined(m_context, nameData)) {
132                 name = toString(nameData);
133                 result->setName(name);
134         }
135         if (!JSValueIsUndefined(m_context, appIdStrData)) {
136                 appid = toString(appIdStrData);
137                 result->setAppId(appid);
138         }
139         if (!JSValueIsUndefined(m_context, iconPathData)) {
140                 iconPath = toString(iconPathData);
141                 result->setIconPath(iconPath);
142         }
143         if (!JSValueIsUndefined(m_context, versionData)) {
144                 version = toString(versionData);
145                 result->setVersion(version);
146         }
147         if (!JSValueIsUndefined(m_context, showData)) {
148                 show = toBool(showData);
149                 result->setShow(show);
150         }
151         if (!JSValueIsUndefined(m_context, categoriesData)) {
152                 categories = toVectorOfStrings(categoriesData);
153                 result->setCategories(categories);
154         }       
155         if (!JSValueIsUndefined(m_context, installDateData)) {
156                 installDate = toDateTimeT(installDateData);
157                 result->setInstallDate(installDate);
158         }
159         if (!JSValueIsUndefined(m_context, installSizeData)) {
160                 installSize = toDateTimeT(installSizeData);
161                 result->setInstallSize(installSize);
162         }
163         if (!JSValueIsUndefined(m_context, pkgIdStrData)) {
164                 pkgid = toString(pkgIdStrData);
165                 result->setPackageId(pkgid);
166         }       
167         return result;
168 }
169
170 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationInformationArrayPtr &arg)
171 {
172         if(arg == NULL) {
173                 Throw(Commons::InvalidArgumentException);
174         }
175         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationInformation, this);
176 }
177
178 ApplicationInformationArrayPtr ApplicationConverter::toApplicationInformationArray(const JSValueRef &jsValue)
179 {
180         ApplicationInformationArrayPtr result(new ApplicationInformationArray());
181
182         JSObjectRef jsObject = toJSObjectRef(jsValue);
183     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
184         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
185         result->push_back(toApplicationInformation(element));
186     }
187     return result;
188 }
189
190 JSValueRef ApplicationConverter::toJSValueRefFromApplicationContext(const ApplicationContextPtr &arg)
191 {
192         if(arg == NULL) {
193                 Throw(Commons::InvalidArgumentException);
194         }
195         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationContext::getClassRef(), arg);
196 }
197
198 ApplicationContextPtr ApplicationConverter::toApplicationContext(const JSValueRef &jsValue)
199 {
200         if(JSApplicationContext::isObjectOfClass(m_context, jsValue))
201                 return JSApplicationContext::getApplicationContext(m_context, jsValue);
202
203         const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_APP_ID));
204         const ScopedJSStringRef contextIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_ID));
205
206         JSObjectRef jsObject = toJSObjectRef(jsValue);
207
208         JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
209         JSValueRef contextIdStrData = JSObjectGetProperty(m_context, jsObject, contextIdStr.get(), NULL);
210
211         std::string appid;
212         std::string contextid;
213
214         ApplicationContextPtr result(new ApplicationContext());
215         if (!result) {
216                 Throw(Commons::ConversionException);
217         }
218
219         if (!JSValueIsUndefined(m_context, appIdStrData)) {
220                 appid = toString(appIdStrData);
221                 result->setAppId(appid);
222         }
223         if (!JSValueIsUndefined(m_context, contextIdStrData)) {
224                 contextid = toString(contextIdStrData);
225                 result->setContextId(contextid);
226         }
227         
228         return result;
229 }
230
231 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationContextArrayPtr &arg)
232 {
233         if(arg == NULL) {
234                 Throw(Commons::ConversionException);
235         }
236         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationContext, this);
237 }
238
239 ApplicationContextArrayPtr ApplicationConverter::toApplicationContextArray(const JSValueRef &jsValue)
240 {
241         ApplicationContextArrayPtr result(new ApplicationContextArray());
242
243         JSObjectRef jsObject = toJSObjectRef(jsValue);
244     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
245         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
246         result->push_back(toApplicationContext(element));
247     }
248     return result;
249 }
250
251
252 JSValueRef ApplicationConverter::toJSValueRef(const RequestedApplicationControlPtr &arg)
253 {
254         LoggerD("entered to toJSValueRefFromRequestedApplicationControl" );
255         if(arg == NULL) {
256                 Throw(Commons::InvalidArgumentException);
257         }
258         return CommonsJavaScript::JSUtils::makeObject(m_context, JSRequestedApplicationControl::getClassRef(), arg);
259 }
260
261 RequestedApplicationControlPtr ApplicationConverter::toRequestedApplicationControl(const JSValueRef &jsValue)
262 {
263         if(JSRequestedApplicationControl::isObjectOfClass(m_context, jsValue)) {
264                 return JSRequestedApplicationControl::getRequestedApplicationControl(m_context, jsValue);
265         } else {
266             ThrowMsg(Commons::ConversionException, "Wrong parameter type.");
267         }
268 }
269
270
271 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlPtr &arg)
272 {
273         LoggerD("entered");
274         if(arg == NULL) {
275                 Throw(Commons::InvalidArgumentException);
276         }
277         return JSApplicationControl::createJSObject(m_context, arg);
278 }
279
280 ApplicationControlPtr ApplicationConverter::toApplicationControl(const JSValueRef &jsValue)
281 {
282         if(JSApplicationControl::isObjectOfClass(m_context, jsValue)) {
283                 return JSApplicationControl::getApplicationControl(m_context, jsValue);
284         } else {
285             throw TypeMismatchException("not a object of class ApplicationControl");
286         }
287 }
288
289 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlDataPtr &arg)
290 {
291         LoggerD("entered");
292         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationControlData::getClassRef(), arg);
293 }
294
295 ApplicationControlDataPtr ApplicationConverter::toApplicationControlData(const JSValueRef &jsValue)
296 {
297         if(JSApplicationControlData::isObjectOfClass(m_context, jsValue))
298                 return JSApplicationControlData::getApplicationControlData(m_context, jsValue);
299
300         const ScopedJSStringRef appControlDataKeyStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_KEY));
301         const ScopedJSStringRef appControlDataValueStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_VALUE));
302
303         JSObjectRef jsObject = toJSObjectRef(jsValue);
304
305         JSValueRef appControlDataKeyData = JSObjectGetProperty(m_context, jsObject, appControlDataKeyStr.get(), NULL);
306         JSValueRef appControlDataValueData = JSObjectGetProperty(m_context, jsObject, appControlDataValueStr.get(), NULL);
307
308         ApplicationControlDataPtr result(new ApplicationControlData());
309         if (!result) {
310                 Throw(Commons::ConversionException);
311         }
312
313         if (!JSValueIsUndefined(m_context, appControlDataKeyData)) {
314                 result->setKey(toString(appControlDataKeyData));
315         }
316
317         if (!JSValueIsUndefined(m_context, appControlDataValueData)) {
318                 result->setValue(toVectorOfStrings(appControlDataValueData));
319         }
320         return result;
321 }
322
323 JSValueRef ApplicationConverter::toJSValueRef(const std::vector<ApplicationControlDataPtr> &arg)
324 {
325     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
326
327     if (jsResult == NULL) {
328         ThrowMsg(Commons::ConversionException, "Could not create js array object");
329     }
330
331     for (std::size_t i = 0; i < arg.size(); ++i) {
332         JSValueRef tmpVal = toJSValueRef(arg[i]);
333         if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
334             ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
335         }
336     }
337
338     return jsResult;    
339 }
340
341 std::vector<ApplicationControlDataPtr> ApplicationConverter::toApplicationControlDataArray(const JSValueRef &jsValue)
342 {
343     if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
344         return std::vector<ApplicationControlDataPtr>();
345     }
346
347     if (!JSIsArrayValue(m_context, jsValue)) {
348         ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
349     }
350
351     std::vector<ApplicationControlDataPtr> result;
352     JSObjectRef objArg = toJSObjectRef(jsValue);
353     for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
354         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
355         result.push_back(toApplicationControlData(element));
356     }
357     return result;      
358 }
359
360 }
361 }