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