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 <API/Call/CallHistoryEntryProperties.h>
26 #include "JSCallHistoryEntry.h"
28 #define CH_ENTRY_ID "entryId"
29 #define CH_ACCOUNT_ID "accountId"
30 #define CH_CALL_TYPE "callType"
31 #define CH_REMOTE_PARTY "remoteParty"
32 #define CH_CONTACT_ID "contactId"
33 #define CH_CONTACT_DETAILS "contactDetails"
34 #define CH_FORWARDEDFROM "forwardedFrom"
35 #define CH_START_TIME "startTime"
36 #define CH_DURATION "duration"
37 #define CH_END_REASON "endReason"
38 #define CH_USED_CAPABILITIES "usedCapabilities"
39 #define CH_KIND "kind"
40 #define CH_TAGS "tags"
41 #define CH_DIRECTION "direction"
42 #define CH_RECORDING "recording"
43 #define CH_COST "cost"
44 #define CH_CURRENCY "currency"
46 using namespace WrtDeviceApis;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49 using namespace TizenApis::Api::Call;
53 std::vector<std::string> Converter::m_allowedCallHistoryEntryProperties;
55 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
57 static bool init = initializeAllowedProperties();
61 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& arg)
63 return toVectorOfT_(arg, &Converter::toULong);
66 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& arg)
68 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
69 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
72 if (!JSIsArrayValue(m_context, arg)) {
73 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
77 CallHistoryEntryList result;
78 JSObjectRef objArg = toJSObjectRef(arg);
79 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
80 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
81 result.push_back(toCallHistoryEntryProperties(element));
84 } catch (Commons::ConversionException& ex) {
85 ThrowMsg(Commons::ConversionException, ex.GetMessage());
86 } catch (Commons::InvalidArgumentException& ex) {
87 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
88 } catch (Commons::Exception& ex) {
89 ThrowMsg(Commons::Exception, ex.GetMessage());
94 stringArrayPtr Converter::toCallHistoryEntryStringArray(const JSValueRef &jsValue)
96 stringArrayPtr result(new stringArray());
98 if (!JSValueIsNull(m_context, jsValue)) {
99 JSObjectRef jsObject = toJSObjectRef(jsValue);
101 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
102 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
103 result->push_back(toString(element));
106 } catch (Commons::Exception& ex) {
107 LogError("Exception: " << ex.GetMessage());
112 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
114 Validator validator(m_context);
115 if (!validator.checkArrayKeys(m_allowedCallHistoryEntryProperties, jsValue)) {
116 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call history entry");
119 const ScopedJSStringRef entryIdStr(JSStringCreateWithUTF8CString(CH_ENTRY_ID));
120 const ScopedJSStringRef accountIdStr(JSStringCreateWithUTF8CString(CH_ACCOUNT_ID));
121 const ScopedJSStringRef callTypeStr(JSStringCreateWithUTF8CString(CH_CALL_TYPE));
122 const ScopedJSStringRef remotePartyStr(JSStringCreateWithUTF8CString(CH_REMOTE_PARTY));
123 const ScopedJSStringRef contactIdStr(JSStringCreateWithUTF8CString(CH_CONTACT_ID));
124 const ScopedJSStringRef contactDetailsStr(JSStringCreateWithUTF8CString(CH_CONTACT_DETAILS));
125 const ScopedJSStringRef forwardedFromStr(JSStringCreateWithUTF8CString(CH_FORWARDEDFROM));
126 const ScopedJSStringRef startTimeStr(JSStringCreateWithUTF8CString(CH_START_TIME));
127 const ScopedJSStringRef durationStr(JSStringCreateWithUTF8CString(CH_DURATION));
128 const ScopedJSStringRef endReasonStr(JSStringCreateWithUTF8CString(CH_END_REASON));
129 const ScopedJSStringRef usedCapabilitiesStr(JSStringCreateWithUTF8CString(CH_USED_CAPABILITIES));
130 const ScopedJSStringRef kindStr(JSStringCreateWithUTF8CString(CH_KIND));
131 const ScopedJSStringRef directionStr(JSStringCreateWithUTF8CString(CH_DIRECTION));
132 const ScopedJSStringRef recordingStr(JSStringCreateWithUTF8CString(CH_RECORDING));
133 const ScopedJSStringRef costStr(JSStringCreateWithUTF8CString(CH_COST));
134 const ScopedJSStringRef currencyStr(JSStringCreateWithUTF8CString(CH_CURRENCY));
136 JSObjectRef jsObject = toJSObjectRef(jsValue);
138 JSValueRef jEntryId = JSObjectGetProperty(m_context, jsObject, entryIdStr.get(), NULL);
139 JSValueRef jAccountId = JSObjectGetProperty(m_context, jsObject, accountIdStr.get(), NULL);
140 JSValueRef jCallType = JSObjectGetProperty(m_context, jsObject, callTypeStr.get(), NULL);
141 JSValueRef jRemoteParty = JSObjectGetProperty(m_context, jsObject, remotePartyStr.get(), NULL);
142 JSValueRef jContactId = JSObjectGetProperty(m_context, jsObject, contactIdStr.get(), NULL);
143 JSValueRef jContactDetails = JSObjectGetProperty(m_context, jsObject, contactDetailsStr.get(), NULL);
144 JSValueRef jForwardedFrom = JSObjectGetProperty(m_context, jsObject, forwardedFromStr.get(), NULL);
145 JSValueRef jStartTime = JSObjectGetProperty(m_context, jsObject, startTimeStr.get(), NULL);
146 JSValueRef jDuration = JSObjectGetProperty(m_context, jsObject, durationStr.get(), NULL);
147 JSValueRef jEndReason = JSObjectGetProperty(m_context, jsObject, endReasonStr.get(), NULL);
148 JSValueRef jUsedCapabilities = JSObjectGetProperty(m_context, jsObject, usedCapabilitiesStr.get(), NULL);
149 JSValueRef jKind = JSObjectGetProperty(m_context, jsObject, kindStr.get(), NULL);
150 JSValueRef jDirection = JSObjectGetProperty(m_context, jsObject, directionStr.get(), NULL);
151 JSValueRef jRecording = JSObjectGetProperty(m_context, jsObject, recordingStr.get(), NULL);
152 JSValueRef jCost = JSObjectGetProperty(m_context, jsObject, costStr.get(), NULL);
153 JSValueRef jCurrency = JSObjectGetProperty(m_context, jsObject, currencyStr.get(), NULL);
155 unsigned long entryId;
156 std::string accountId;
157 std::string callType;
158 std::string remoteParty;
159 std::string contactId;
160 std::string contactDetails;
161 std::string forwardedFrom;
163 unsigned long duration;
164 std::string endReason;
165 stringArrayPtr usedCapabilities;
167 std::string direction;
168 stringArrayPtr recording;
170 std::string currency;
172 CallHistoryEntryPropertiesPtr result = CallHistoryEntryPropertiesPtr(new CallHistoryEntryProperties());
175 Throw(Commons::ConversionException);
178 if (!JSValueIsUndefined(m_context, jEntryId)) {
179 entryId = toULong(jEntryId);
180 result->setEntryId(entryId);
182 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
185 if (!JSValueIsUndefined(m_context, jAccountId)) {
186 accountId = toString(jAccountId);
187 result->setAccountId(accountId);
190 if (!JSValueIsUndefined(m_context, jCallType)) {
191 callType = toString(jCallType);
192 result->setCallType(callType);
195 if (!JSValueIsUndefined(m_context, jRemoteParty)) {
196 remoteParty = toString(jRemoteParty);
197 result->setRemoteParty(remoteParty);
199 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
202 if (!JSValueIsUndefined(m_context, jContactId)) {
203 contactId = toString(jContactId);
204 result->setContactId(contactId);
207 if (!JSValueIsUndefined(m_context, jContactDetails)) {
208 contactDetails = toString(jContactDetails);
209 result->setContactDetails(contactDetails);
212 if (!JSValueIsUndefined(m_context, jForwardedFrom)) {
213 forwardedFrom = toString(jForwardedFrom);
214 result->setForwardedFrom(forwardedFrom);
217 if (!JSValueIsUndefined(m_context, jStartTime)) {
218 startTime = toDateTimeT(jStartTime);
219 result->setStartTime(startTime);
222 if (!JSValueIsUndefined(m_context, jDuration)) {
223 duration = toULong(jDuration);
224 result->setDuration(duration);
227 if (!JSValueIsUndefined(m_context, jEndReason)) {
228 endReason = toString(jEndReason);
229 result->setEndReason(endReason);
232 if (!JSValueIsUndefined(m_context, jUsedCapabilities)) {
233 usedCapabilities = toCallHistoryEntryStringArray(jUsedCapabilities);
234 result->setUsedCapabilities(usedCapabilities);
237 if (!JSValueIsUndefined(m_context, jKind)) {
238 kind = toString(jKind);
239 result->setKind(kind);
242 if (!JSValueIsUndefined(m_context, jDirection)) {
243 direction = toString(jDirection);
244 result->setDirection(direction);
247 if (!JSValueIsUndefined(m_context, jRecording)) {
248 recording = toCallHistoryEntryStringArray(jRecording);
249 result->setRecording(recording);
252 if (!JSValueIsUndefined(m_context, jCost)) {
253 cost = toULong(jCost);
254 result->setCost(cost);
257 if (!JSValueIsUndefined(m_context, jCurrency)) {
258 currency = toString(jCurrency);
259 result->setCurrency(currency);
265 JSValueRef Converter::toJSValueRef(const Api::Call::CallHistoryEntryListPtr& arg, JSContextRef context)
267 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
269 ThrowMsg(Commons::ConversionException, "Could not create js array object");
271 Api::Call::CallHistoryEntryProperties tmpCallEntry;
273 for (size_t i = 0; i < (*arg).size(); i++) {
274 tmpCallEntry = *((*arg)[i]);
275 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
277 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
280 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
281 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
287 JSValueRef Converter::toJSValueRef(const Api::Call::stringArrayPtr &arg, JSContextRef context)
289 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
291 ThrowMsg(Commons::ConversionException, "Could not create js array object");
294 for (size_t i = 0; i < (*arg).size(); i++) {
295 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
296 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
303 bool Converter::initializeAllowedProperties()
305 m_allowedCallHistoryEntryProperties.push_back(CH_ENTRY_ID);
306 m_allowedCallHistoryEntryProperties.push_back(CH_ACCOUNT_ID);
307 m_allowedCallHistoryEntryProperties.push_back(CH_CALL_TYPE);
308 m_allowedCallHistoryEntryProperties.push_back(CH_REMOTE_PARTY);
309 m_allowedCallHistoryEntryProperties.push_back(CH_CONTACT_ID);
310 m_allowedCallHistoryEntryProperties.push_back(CH_CONTACT_DETAILS);
311 m_allowedCallHistoryEntryProperties.push_back(CH_FORWARDEDFROM);
312 m_allowedCallHistoryEntryProperties.push_back(CH_START_TIME);
313 m_allowedCallHistoryEntryProperties.push_back(CH_DURATION);
314 m_allowedCallHistoryEntryProperties.push_back(CH_END_REASON);
315 m_allowedCallHistoryEntryProperties.push_back(CH_USED_CAPABILITIES);
316 m_allowedCallHistoryEntryProperties.push_back(CH_KIND);
317 m_allowedCallHistoryEntryProperties.push_back(CH_DIRECTION);
318 m_allowedCallHistoryEntryProperties.push_back(CH_RECORDING);
319 m_allowedCallHistoryEntryProperties.push_back(CH_COST);
320 m_allowedCallHistoryEntryProperties.push_back(CH_CURRENCY);