2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "Converter.h"
19 #include <dpl/log/log.h>
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"
32 using namespace WrtDeviceApis;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35 using namespace TizenApis::Api::Call;
39 std::vector<std::string> Converter::m_allowedCallHistoryEntryProperties;
40 std::vector<std::string> Converter::m_allowedRemoteParty;
41 std::vector<std::string> Converter::m_allowedCallServiceFilter;
43 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
45 static bool init = initializeAllowedProperties();
49 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& arg)
51 return toVectorOfT_(arg, &Converter::toULong);
54 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& arg)
56 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
57 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
60 if (!JSIsArrayValue(m_context, arg)) {
61 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
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));
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());
82 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
84 StringArrayPtr result(new StringArray());
86 if (!JSValueIsNull(m_context, jsValue)) {
87 JSObjectRef jsObject = toJSObjectRef(jsValue);
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));
94 } catch (Commons::Exception& ex) {
95 LogError("Exception: " << ex.GetMessage());
100 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
102 RemotePartyListPtr result(new RemotePartyList());
104 if (!JSValueIsNull(m_context, jsValue)) {
105 JSObjectRef jsObject = toJSObjectRef(jsValue);
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));
112 } catch (Commons::Exception& ex) {
113 LogError("Exception: " << ex.GetMessage());
118 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
120 Validator validator(m_context);
121 if (!validator.checkArrayKeys(m_allowedRemoteParty, jsValue)) {
122 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of RemoteParty");
125 const ScopedJSStringRef remotePartyStr(JSStringCreateWithUTF8CString(STR_REMOTE_PARTY));
126 const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString(STR_DISPLAY_NAME));
127 const ScopedJSStringRef contactIdStr(JSStringCreateWithUTF8CString(STR_CONTACT_ID));
129 JSObjectRef jsObject = toJSObjectRef(jsValue);
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);
135 std::string remoteParty;
136 std::string displayName;
137 std::string contactId;
139 RemotePartyPtr result(new RemoteParty());
142 Throw(Commons::ConversionException);
145 if (!JSValueIsUndefined(m_context, jRemoteParty)) {
146 remoteParty = toString(jRemoteParty);
147 result->setRemoteParty(remoteParty);
150 if (!JSValueIsUndefined(m_context, jDisplayName)) {
151 displayName = toString(jDisplayName);
152 result->setDisplayName(displayName);
155 if (!JSValueIsUndefined(m_context, jContactId)) {
156 contactId = toString(jContactId);
157 result->setContactId(contactId);
163 CallServiceFilterPtr Converter::toCallServiceFilter(const JSValueRef &jsValue)
165 Validator validator(m_context);
166 if (!validator.checkArrayKeys(m_allowedCallServiceFilter, jsValue)) {
167 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call service filter");
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));
175 JSObjectRef jsObject = toJSObjectRef(jsValue);
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);
182 std::string callType;
183 StringArrayPtr tags(new StringArray());
184 std::string serviceName;
185 std::string providerId;
187 CallServiceFilterPtr result(new CallServiceFilter());
190 Throw(Commons::ConversionException);
193 if (!JSValueIsUndefined(m_context, jCallType)) {
194 callType = toString(jCallType);
195 result->setCallType(callType);
198 if (!JSValueIsUndefined(m_context, jProviderId)) {
199 providerId = toString(jProviderId);
200 result->setProviderId(providerId);
206 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
208 Validator validator(m_context);
209 if (!validator.checkArrayKeys(m_allowedCallHistoryEntryProperties, jsValue)) {
210 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call history entry");
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));
227 JSObjectRef jsObject = toJSObjectRef(jsValue);
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);
243 unsigned long entryId;
244 std::string serviceId;
245 std::string callType;
247 RemotePartyListPtr remoteParties;
248 RemotePartyPtr forwardedFrom;
250 unsigned long duration;
251 std::string endReason;
252 std::string direction;
253 StringArrayPtr recording;
255 std::string currency;
257 CallHistoryEntryPropertiesPtr result = CallHistoryEntryPropertiesPtr(new CallHistoryEntryProperties());
260 Throw(Commons::ConversionException);
263 if (!JSValueIsUndefined(m_context, jEntryId)) {
264 entryId = toULong(jEntryId);
265 result->setEntryId(entryId);
267 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
270 if (!JSValueIsUndefined(m_context, jServiceId)) {
271 serviceId = toString(jServiceId);
272 result->setServiceId(serviceId);
275 if (!JSValueIsUndefined(m_context, jCallType)) {
276 callType = toString(jCallType);
277 result->setCallType(callType);
280 if (!JSValueIsUndefined(m_context, jTags)) {
281 tags = toStringArray(jTags);
282 result->setTags(tags);
285 if (!JSValueIsUndefined(m_context, jRemoteParties)) {
286 remoteParties = toRemotePartyList(jRemoteParties);
287 result->setRemoteParties(remoteParties);
289 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
292 if (!JSValueIsUndefined(m_context, jForwardedFrom)) {
293 forwardedFrom = toRemoteParty(jForwardedFrom);
294 result->setForwardedFrom(forwardedFrom);
297 if (!JSValueIsUndefined(m_context, jStartTime)) {
298 startTime = toDateTimeT(jStartTime);
299 result->setStartTime(startTime);
302 if (!JSValueIsUndefined(m_context, jDuration)) {
303 duration = toULong(jDuration);
304 result->setDuration(duration);
307 if (!JSValueIsUndefined(m_context, jEndReason)) {
308 endReason = toString(jEndReason);
309 result->setEndReason(endReason);
312 if (!JSValueIsUndefined(m_context, jDirection)) {
313 direction = toString(jDirection);
314 result->setDirection(direction);
317 if (!JSValueIsUndefined(m_context, jRecording)) {
318 recording = toStringArray(jRecording);
319 result->setRecording(recording);
322 if (!JSValueIsUndefined(m_context, jCost)) {
323 cost = toULong(jCost);
324 result->setCost(cost);
327 if (!JSValueIsUndefined(m_context, jCurrency)) {
328 currency = toString(jCurrency);
329 result->setCurrency(currency);
335 EventCallHistoryListenerPrivateDataPtr
336 Converter::toEventCallHistoryListenerPrivateData(JSValueRef successParam, JSContextRef context)
338 if (JSValueIsNull(m_context, successParam) || JSValueIsUndefined(m_context, successParam)
339 || !JSValueIsObject(m_context, successParam))
341 ThrowMsg(Commons::ConversionException, "CB is not a object");
344 JSObjectRef objectCallbacks = toJSObjectRef(successParam);
345 Validator validator(m_context);
346 CallHistoryChangeCB result;
348 result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onAdded");
349 result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onChanged");
351 if ((!validator.isNullOrUndefined(result.onAdded) && !validator.isCallback(result.onAdded)) ||
352 (!validator.isNullOrUndefined(result.onChanged) && !validator.isCallback(result.onChanged)))
354 ThrowMsg(Commons::InvalidArgumentException, "Invalid values error : CallHistoryChangeCB");
357 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context, result.onAdded, NULL);
358 JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context, result.onChanged, NULL);
360 return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm));
363 JSValueRef Converter::toJSValueRef(const Api::Call::CallHistoryEntryListPtr& arg, JSContextRef context)
365 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
367 ThrowMsg(Commons::ConversionException, "Could not create js array object");
369 Api::Call::CallHistoryEntryProperties tmpCallEntry;
371 for (size_t i = 0; i < (*arg).size(); i++) {
372 tmpCallEntry = *((*arg)[i]);
373 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
375 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
378 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
379 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
385 JSValueRef Converter::toJSValueRef(const Api::Call::StringArrayPtr &arg, JSContextRef context)
387 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
389 ThrowMsg(Commons::ConversionException, "Could not create js array object");
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");
401 JSValueRef Converter::toJSValueRef(const Api::Call::RemotePartyListPtr& arg, JSContextRef context)
403 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
405 ThrowMsg(Commons::ConversionException, "Could not create js array object");
408 Api::Call::RemoteParty tmpRemoteParty;
410 for (size_t i = 0; i < (*arg).size(); i++) {
411 tmpRemoteParty = *((*arg)[i]);
412 JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
414 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
417 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
418 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
424 JSValueRef Converter::toJSValueRef(const Api::Account::AccountServicesArrayPtr& arg, JSContextRef context)
426 std::vector<JSValueRef> callServiceList;
428 Api::Account::AccountServices tmpService;
430 for (size_t i = 0; i < (*arg).size(); i++) {
431 tmpService = *((*arg)[i]);
433 if (tmpService.getServiceTypeId().compare(STR_TIZEN_TEL) == 0) {
434 callServiceList.push_back(JSCellularCallService::createJSObject(context, tmpService));
436 callServiceList.push_back(JSCallService::createJSObject(context, tmpService));
440 return toJSValueRef(callServiceList);
443 bool Converter::initializeAllowedProperties()
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);