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