merge wrt-plugins-tizen_0.2.0-3
[platform/framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / Call / Converter.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
17  
18 #include "Converter.h"
19 #include <dpl/log/log.h>
20
21 #include <Commons/Exception.h>
22 #include <CommonsJavaScript/Validator.h>
23 #include <CommonsJavaScript/ScopedJSStringRef.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <API/Call/CallDefine.h>
27 #include "JSCallHistoryEntry.h"
28 #include "JSRemoteParty.h"
29 #include "JSCallService.h"
30 #include "JSCellularCallService.h"
31
32 using namespace WrtDeviceApis;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35 using namespace TizenApis::Api::Call;
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 std::vector<std::string> Converter::m_allowedCallHistoryEntryProperties;
40 std::vector<std::string> Converter::m_allowedRemoteParty;
41 std::vector<std::string> Converter::m_allowedCallServiceFilter;
42
43 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
44 {
45         static bool init = initializeAllowedProperties();
46         (void) init;
47 }
48
49 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& arg)
50 {
51     return toVectorOfT_(arg, &Converter::toULong);
52 }
53
54 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& arg)
55 {
56         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
57                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
58         }
59
60         if (!JSIsArrayValue(m_context, arg)) {
61                 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
62         }
63
64         try {
65                 CallHistoryEntryList result;
66                 JSObjectRef objArg = toJSObjectRef(arg);
67                 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
68                         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
69                         result.push_back(toCallHistoryEntryProperties(element));
70                 }
71                 return result;
72         } catch (Commons::ConversionException& ex) {
73                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
74         } catch (Commons::InvalidArgumentException& ex) {
75                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
76         } catch (Commons::Exception& ex) {
77                 ThrowMsg(Commons::Exception, ex.GetMessage());
78         }
79
80 }
81
82 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
83 {
84         StringArrayPtr result(new StringArray());
85         try {
86                 if (!JSValueIsNull(m_context, jsValue)) {
87                         JSObjectRef jsObject = toJSObjectRef(jsValue);
88                         
89                         for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
90                                 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
91                                 result->push_back(toString(element));
92                         }
93                 }
94         } catch (Commons::Exception& ex) {
95                 LogError("Exception: " << ex.GetMessage());
96         }
97         return result;
98 }
99
100 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
101 {
102         RemotePartyListPtr result(new RemotePartyList());
103         try {
104                 if (!JSValueIsNull(m_context, jsValue)) {
105                         JSObjectRef jsObject = toJSObjectRef(jsValue);
106                         
107                         for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
108                                 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
109                                 result->push_back(toRemoteParty(element));
110                         }
111                 }
112         } catch (Commons::Exception& ex) {
113                 LogError("Exception: " << ex.GetMessage());
114         }
115         return result;
116 }
117
118 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
119 {
120         Validator validator(m_context);
121         if (!validator.checkArrayKeys(m_allowedRemoteParty, jsValue)) {
122                 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of RemoteParty");
123         }
124
125         const ScopedJSStringRef remotePartyStr(JSStringCreateWithUTF8CString(STR_REMOTE_PARTY));
126         const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString(STR_DISPLAY_NAME));
127         const ScopedJSStringRef contactIdStr(JSStringCreateWithUTF8CString(STR_CONTACT_ID));
128
129         JSObjectRef jsObject = toJSObjectRef(jsValue);
130
131         JSValueRef jRemoteParty = JSObjectGetProperty(m_context, jsObject, remotePartyStr.get(), NULL);
132         JSValueRef jDisplayName = JSObjectGetProperty(m_context, jsObject, displayNameStr.get(), NULL);
133         JSValueRef jContactId = JSObjectGetProperty(m_context, jsObject, contactIdStr.get(), NULL);
134
135         std::string remoteParty;
136         std::string displayName;
137         std::string contactId;
138
139         RemotePartyPtr result(new RemoteParty());
140
141         if (!result) {
142                 Throw(Commons::ConversionException);
143         }
144
145         if (!JSValueIsUndefined(m_context, jRemoteParty)) {
146                 remoteParty = toString(jRemoteParty);
147                 result->setRemoteParty(remoteParty);
148         }
149
150         if (!JSValueIsUndefined(m_context, jDisplayName)) {
151                 displayName = toString(jDisplayName);
152                 result->setDisplayName(displayName);
153         }
154
155         if (!JSValueIsUndefined(m_context, jContactId)) {
156                 contactId = toString(jContactId);
157                 result->setContactId(contactId);
158         }
159
160         return result;
161 }
162
163 CallServiceFilterPtr Converter::toCallServiceFilter(const JSValueRef &jsValue)
164 {
165         Validator validator(m_context);
166         if (!validator.checkArrayKeys(m_allowedCallServiceFilter, jsValue)) {
167                 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call service filter");
168         }
169
170         const ScopedJSStringRef callTypeStr(JSStringCreateWithUTF8CString(STR_CALL_TYPE));
171         const ScopedJSStringRef tagsStr(JSStringCreateWithUTF8CString(STR_TAGS));
172         const ScopedJSStringRef serviceNameStr(JSStringCreateWithUTF8CString(STR_SERVICE_NAME));
173         const ScopedJSStringRef providerIdStr(JSStringCreateWithUTF8CString(STR_PROVIDER_ID));
174
175         JSObjectRef jsObject = toJSObjectRef(jsValue);
176
177         JSValueRef jCallType = JSObjectGetProperty(m_context, jsObject, callTypeStr.get(), NULL);
178         JSValueRef jTags = JSObjectGetProperty(m_context, jsObject, tagsStr.get(), NULL);
179         JSValueRef jServiceName = JSObjectGetProperty(m_context, jsObject, serviceNameStr.get(), NULL);
180         JSValueRef jProviderId = JSObjectGetProperty(m_context, jsObject, providerIdStr.get(), NULL);
181
182         std::string callType;
183         StringArrayPtr tags(new StringArray());
184         std::string serviceName;
185         std::string providerId;
186
187         CallServiceFilterPtr result(new CallServiceFilter());
188
189         if (!result) {
190                 Throw(Commons::ConversionException);
191         }
192
193         if (!JSValueIsUndefined(m_context, jCallType)) {
194                 callType = toString(jCallType);
195                 result->setCallType(callType);
196         }
197
198         if (!JSValueIsUndefined(m_context, jProviderId)) {
199                 providerId = toString(jProviderId);
200                 result->setProviderId(providerId);
201         }
202
203         return result;
204 }
205
206 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
207 {
208         Validator validator(m_context);
209         if (!validator.checkArrayKeys(m_allowedCallHistoryEntryProperties, jsValue)) {
210                 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call history entry");
211         }
212
213         const ScopedJSStringRef entryIdStr(JSStringCreateWithUTF8CString(STR_ENTRY_ID));
214         const ScopedJSStringRef serviceIdStr(JSStringCreateWithUTF8CString(STR_SERVICE_ID));
215         const ScopedJSStringRef callTypeStr(JSStringCreateWithUTF8CString(STR_CALL_TYPE));
216         const ScopedJSStringRef tagsStr(JSStringCreateWithUTF8CString(STR_TAGS));
217         const ScopedJSStringRef remotePartiesStr(JSStringCreateWithUTF8CString(STR_REMOTE_PARTIES));
218         const ScopedJSStringRef forwardedFromStr(JSStringCreateWithUTF8CString(STR_FORWARDEDFROM));
219         const ScopedJSStringRef startTimeStr(JSStringCreateWithUTF8CString(STR_START_TIME));
220         const ScopedJSStringRef durationStr(JSStringCreateWithUTF8CString(STR_DURATION));
221         const ScopedJSStringRef endReasonStr(JSStringCreateWithUTF8CString(STR_END_REASON));
222         const ScopedJSStringRef directionStr(JSStringCreateWithUTF8CString(STR_DIRECTION));
223         const ScopedJSStringRef recordingStr(JSStringCreateWithUTF8CString(STR_RECORDING));
224         const ScopedJSStringRef costStr(JSStringCreateWithUTF8CString(STR_COST));
225         const ScopedJSStringRef currencyStr(JSStringCreateWithUTF8CString(STR_CURRENCY));
226
227         JSObjectRef jsObject = toJSObjectRef(jsValue);
228
229         JSValueRef jEntryId = JSObjectGetProperty(m_context, jsObject, entryIdStr.get(), NULL);
230         JSValueRef jServiceId = JSObjectGetProperty(m_context, jsObject, serviceIdStr.get(), NULL);
231         JSValueRef jCallType = JSObjectGetProperty(m_context, jsObject, callTypeStr.get(), NULL);
232         JSValueRef jTags = JSObjectGetProperty(m_context, jsObject, tagsStr.get(), NULL);
233         JSValueRef jRemoteParties = JSObjectGetProperty(m_context, jsObject, remotePartiesStr.get(), NULL);
234         JSValueRef jForwardedFrom = JSObjectGetProperty(m_context, jsObject, forwardedFromStr.get(), NULL);
235         JSValueRef jStartTime = JSObjectGetProperty(m_context, jsObject, startTimeStr.get(), NULL);
236         JSValueRef jDuration = JSObjectGetProperty(m_context, jsObject, durationStr.get(), NULL);
237         JSValueRef jEndReason = JSObjectGetProperty(m_context, jsObject, endReasonStr.get(), NULL);
238         JSValueRef jDirection = JSObjectGetProperty(m_context, jsObject, directionStr.get(), NULL);
239         JSValueRef jRecording = JSObjectGetProperty(m_context, jsObject, recordingStr.get(), NULL);
240         JSValueRef jCost = JSObjectGetProperty(m_context, jsObject, costStr.get(), NULL);
241         JSValueRef jCurrency = JSObjectGetProperty(m_context, jsObject, currencyStr.get(), NULL);
242
243         unsigned long entryId;
244         std::string serviceId;
245         std::string callType;
246         StringArrayPtr tags;
247         RemotePartyListPtr remoteParties;
248         RemotePartyPtr forwardedFrom;
249         time_t startTime;
250         unsigned long duration;
251         std::string endReason;
252         std::string direction;
253         StringArrayPtr recording;
254         unsigned long cost;
255         std::string currency;
256
257         CallHistoryEntryPropertiesPtr result = CallHistoryEntryPropertiesPtr(new CallHistoryEntryProperties());
258
259         if (!result) {
260                 Throw(Commons::ConversionException);
261         }
262
263         if (!JSValueIsUndefined(m_context, jEntryId)) {
264                 entryId = toULong(jEntryId);
265                 result->setEntryId(entryId);
266         } else {
267                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
268         }
269
270         if (!JSValueIsUndefined(m_context, jServiceId)) {
271                 serviceId = toString(jServiceId);
272                 result->setServiceId(serviceId);
273         }
274
275         if (!JSValueIsUndefined(m_context, jCallType)) {
276                 callType = toString(jCallType);
277                 result->setCallType(callType);
278         }
279
280         if (!JSValueIsUndefined(m_context, jTags)) {
281                 tags = toStringArray(jTags);
282                 result->setTags(tags);
283         }
284
285         if (!JSValueIsUndefined(m_context, jRemoteParties)) {
286                 remoteParties = toRemotePartyList(jRemoteParties);
287                 result->setRemoteParties(remoteParties);
288         } else {
289                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
290         }
291
292         if (!JSValueIsUndefined(m_context, jForwardedFrom)) {
293                 forwardedFrom = toRemoteParty(jForwardedFrom);
294                 result->setForwardedFrom(forwardedFrom);
295         }
296
297         if (!JSValueIsUndefined(m_context, jStartTime)) {
298                 startTime = toDateTimeT(jStartTime);
299                 result->setStartTime(startTime);
300         }
301
302         if (!JSValueIsUndefined(m_context, jDuration)) {
303                 duration = toULong(jDuration);
304                 result->setDuration(duration);
305         }
306
307         if (!JSValueIsUndefined(m_context, jEndReason)) {
308                 endReason = toString(jEndReason);
309                 result->setEndReason(endReason);
310         }
311
312         if (!JSValueIsUndefined(m_context, jDirection)) {
313                 direction = toString(jDirection);
314                 result->setDirection(direction);
315         }
316
317         if (!JSValueIsUndefined(m_context, jRecording)) {
318                 recording = toStringArray(jRecording);
319                 result->setRecording(recording);
320         }
321
322         if (!JSValueIsUndefined(m_context, jCost)) {
323                 cost = toULong(jCost);
324                 result->setCost(cost);
325         }
326
327         if (!JSValueIsUndefined(m_context, jCurrency)) {
328                 currency = toString(jCurrency);
329                 result->setCurrency(currency);
330         }
331
332         return result;
333 }
334
335 EventCallHistoryListenerPrivateDataPtr 
336         Converter::toEventCallHistoryListenerPrivateData(JSValueRef successParam, JSContextRef context)
337 {
338         if (JSValueIsNull(m_context, successParam) || JSValueIsUndefined(m_context, successParam)
339                         || !JSValueIsObject(m_context, successParam))
340         {
341                 ThrowMsg(Commons::ConversionException, "CB is not a object");
342         }
343
344         JSObjectRef objectCallbacks = toJSObjectRef(successParam);
345         Validator validator(m_context);
346         CallHistoryChangeCB result;
347
348         result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onAdded");
349         result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onChanged");
350                         
351         if ((!validator.isNullOrUndefined(result.onAdded) && !validator.isCallback(result.onAdded)) ||
352                 (!validator.isNullOrUndefined(result.onChanged) && !validator.isCallback(result.onChanged)))
353         {
354                 ThrowMsg(Commons::InvalidArgumentException, "Invalid values error : CallHistoryChangeCB");
355         }
356
357         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context, result.onAdded, NULL);
358         JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context, result.onChanged, NULL);
359
360         return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm));
361 }
362
363 JSValueRef Converter::toJSValueRef(const Api::Call::CallHistoryEntryListPtr& arg, JSContextRef context)
364 {
365         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
366         if (!jsResult) {
367                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
368         }
369         Api::Call::CallHistoryEntryProperties tmpCallEntry;
370
371         for (size_t i = 0; i < (*arg).size(); i++) {
372                 tmpCallEntry = *((*arg)[i]);
373                 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
374                 if (!jsObject) {
375                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
376                 }
377
378                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
379                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
380                 }
381         }
382         return jsResult;
383 }
384
385 JSValueRef Converter::toJSValueRef(const Api::Call::StringArrayPtr &arg, JSContextRef context)
386 {
387         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
388         if (!jsResult) {
389                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
390         }
391
392         for (size_t i = 0; i < (*arg).size(); i++) {
393                 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
394                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
395                 }
396         }
397
398         return jsResult;
399 }
400
401 JSValueRef Converter::toJSValueRef(const Api::Call::RemotePartyListPtr& arg, JSContextRef context)
402 {
403         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
404         if (!jsResult) {
405                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
406         }
407
408         Api::Call::RemoteParty tmpRemoteParty;
409
410         for (size_t i = 0; i < (*arg).size(); i++) {
411                 tmpRemoteParty = *((*arg)[i]);
412                 JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
413                 if (!jsObject) {
414                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
415                 }
416
417                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
418                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
419                 }
420         }
421         return jsResult;
422 }
423
424 JSValueRef Converter::toJSValueRef(const Api::Account::AccountServicesArrayPtr& arg, JSContextRef context)
425 {
426         std::vector<JSValueRef> callServiceList;
427
428         Api::Account::AccountServices tmpService;
429
430         for (size_t i = 0; i < (*arg).size(); i++) {
431                 tmpService = *((*arg)[i]);
432
433                 if (tmpService.getServiceTypeId().compare(STR_TIZEN_TEL) == 0) {
434                         callServiceList.push_back(JSCellularCallService::createJSObject(context, tmpService));
435                 } else {
436                         callServiceList.push_back(JSCallService::createJSObject(context, tmpService));
437                 }
438         }
439
440         return toJSValueRef(callServiceList);
441 }
442
443 bool Converter::initializeAllowedProperties()
444 {
445         m_allowedCallHistoryEntryProperties.push_back(STR_ENTRY_ID);
446         m_allowedCallHistoryEntryProperties.push_back(STR_SERVICE_ID);
447         m_allowedCallHistoryEntryProperties.push_back(STR_CALL_TYPE);
448         m_allowedCallHistoryEntryProperties.push_back(STR_TAGS);
449         m_allowedCallHistoryEntryProperties.push_back(STR_REMOTE_PARTIES);
450         m_allowedCallHistoryEntryProperties.push_back(STR_FORWARDEDFROM);
451         m_allowedCallHistoryEntryProperties.push_back(STR_START_TIME);
452         m_allowedCallHistoryEntryProperties.push_back(STR_DURATION);
453         m_allowedCallHistoryEntryProperties.push_back(STR_END_REASON);
454         m_allowedCallHistoryEntryProperties.push_back(STR_DIRECTION);
455         m_allowedCallHistoryEntryProperties.push_back(STR_RECORDING);
456         m_allowedCallHistoryEntryProperties.push_back(STR_COST);
457         m_allowedCallHistoryEntryProperties.push_back(STR_CURRENCY);
458         m_allowedRemoteParty.push_back(STR_REMOTE_PARTY);
459         m_allowedRemoteParty.push_back(STR_DISPLAY_NAME);
460         m_allowedRemoteParty.push_back(STR_CONTACT_ID);
461         m_allowedCallServiceFilter.push_back(STR_CALL_TYPE);
462         m_allowedCallServiceFilter.push_back(STR_TAGS);
463         m_allowedCallServiceFilter.push_back(STR_SERVICE_NAME);
464         m_allowedCallServiceFilter.push_back(STR_PROVIDER_ID);
465
466         return true;
467 }
468
469 }
470 }
471