9ee597c0ec8efbf57c690d0772b8870c7873b90a
[profile/ivi/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 <API/Call/CallHistoryEntryProperties.h>
26 #include "JSCallHistoryEntry.h"
27
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"
45
46 using namespace WrtDeviceApis;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49 using namespace TizenApis::Api::Call;
50
51 namespace TizenApis {
52 namespace Tizen1_0 {
53 std::vector<std::string> Converter::m_allowedCallHistoryEntryProperties;
54
55 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
56 {
57         static bool init = initializeAllowedProperties();
58         (void) init;
59 }
60
61 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& arg)
62 {
63     return toVectorOfT_(arg, &Converter::toULong);
64 }
65
66 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& arg)
67 {
68         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
69                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
70         }
71
72         if (!JSIsArrayValue(m_context, arg)) {
73                 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
74         }
75
76         try {
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));
82                 }
83                 return result;
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());
90         }
91
92 }
93
94 stringArrayPtr Converter::toCallHistoryEntryStringArray(const JSValueRef &jsValue)
95 {
96         stringArrayPtr result(new stringArray());
97         try {
98                 if (!JSValueIsNull(m_context, jsValue)) {
99                         JSObjectRef jsObject = toJSObjectRef(jsValue);
100                         
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));
104                         }
105                 }
106         } catch (Commons::Exception& ex) {
107                 LogError("Exception: " << ex.GetMessage());
108         }
109         return result;
110 }
111
112 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
113 {
114         Validator validator(m_context);
115         if (!validator.checkArrayKeys(m_allowedCallHistoryEntryProperties, jsValue)) {
116                 ThrowMsg(Commons::InvalidArgumentException, "Wrong attribute of call history entry");
117         }
118
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));
135
136         JSObjectRef jsObject = toJSObjectRef(jsValue);
137
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);
154
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;
162         time_t startTime;
163         unsigned long duration;
164         std::string endReason;
165         stringArrayPtr usedCapabilities;
166         std::string kind;
167         std::string direction;
168         stringArrayPtr recording;
169         unsigned long cost;
170         std::string currency;
171
172         CallHistoryEntryPropertiesPtr result = CallHistoryEntryPropertiesPtr(new CallHistoryEntryProperties());
173
174         if (!result) {
175                 Throw(Commons::ConversionException);
176         }
177
178         if (!JSValueIsUndefined(m_context, jEntryId)) {
179                 entryId = toULong(jEntryId);
180                 result->setEntryId(entryId);
181         } else {
182                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
183         }
184
185         if (!JSValueIsUndefined(m_context, jAccountId)) {
186                 accountId = toString(jAccountId);
187                 result->setAccountId(accountId);
188         }
189
190         if (!JSValueIsUndefined(m_context, jCallType)) {
191                 callType = toString(jCallType);
192                 result->setCallType(callType);
193         }
194
195         if (!JSValueIsUndefined(m_context, jRemoteParty)) {
196                 remoteParty = toString(jRemoteParty);
197                 result->setRemoteParty(remoteParty);
198         } else {
199                 ThrowMsg(Commons::InvalidArgumentException, "Undefined history entry");
200         }
201
202         if (!JSValueIsUndefined(m_context, jContactId)) {
203                 contactId = toString(jContactId);
204                 result->setContactId(contactId);
205         }
206
207         if (!JSValueIsUndefined(m_context, jContactDetails)) {
208                 contactDetails = toString(jContactDetails);
209                 result->setContactDetails(contactDetails);
210         }
211
212         if (!JSValueIsUndefined(m_context, jForwardedFrom)) {
213                 forwardedFrom = toString(jForwardedFrom);
214                 result->setForwardedFrom(forwardedFrom);
215         }
216
217         if (!JSValueIsUndefined(m_context, jStartTime)) {
218                 startTime = toDateTimeT(jStartTime);
219                 result->setStartTime(startTime);
220         }
221
222         if (!JSValueIsUndefined(m_context, jDuration)) {
223                 duration = toULong(jDuration);
224                 result->setDuration(duration);
225         }
226
227         if (!JSValueIsUndefined(m_context, jEndReason)) {
228                 endReason = toString(jEndReason);
229                 result->setEndReason(endReason);
230         }
231
232         if (!JSValueIsUndefined(m_context, jUsedCapabilities)) {
233                 usedCapabilities = toCallHistoryEntryStringArray(jUsedCapabilities);
234                 result->setUsedCapabilities(usedCapabilities);
235         }
236
237         if (!JSValueIsUndefined(m_context, jKind)) {
238                 kind = toString(jKind);
239                 result->setKind(kind);
240         }
241
242         if (!JSValueIsUndefined(m_context, jDirection)) {
243                 direction = toString(jDirection);
244                 result->setDirection(direction);
245         }
246
247         if (!JSValueIsUndefined(m_context, jRecording)) {
248                 recording = toCallHistoryEntryStringArray(jRecording);
249                 result->setRecording(recording);
250         }
251
252         if (!JSValueIsUndefined(m_context, jCost)) {
253                 cost = toULong(jCost);
254                 result->setCost(cost);
255         }
256
257         if (!JSValueIsUndefined(m_context, jCurrency)) {
258                 currency = toString(jCurrency);
259                 result->setCurrency(currency);
260         }
261
262         return result;
263 }
264
265 JSValueRef Converter::toJSValueRef(const Api::Call::CallHistoryEntryListPtr& arg, JSContextRef context)
266 {
267         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
268         if (!jsResult) {
269                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
270         }
271         Api::Call::CallHistoryEntryProperties tmpCallEntry;
272
273         for (size_t i = 0; i < (*arg).size(); i++) {
274                 tmpCallEntry = *((*arg)[i]);
275                 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
276                 if (!jsObject) {
277                         ThrowMsg(Commons::ConversionException, "Could not create JS object.");
278                 }
279
280                 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
281                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
282                 }
283         }
284         return jsResult;
285 }
286
287 JSValueRef Converter::toJSValueRef(const Api::Call::stringArrayPtr &arg, JSContextRef context)
288 {
289         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
290         if (!jsResult) {
291                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
292         }
293
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");
297                 }
298         }
299
300         return jsResult;
301 }
302
303 bool Converter::initializeAllowedProperties()
304 {
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);
321
322         return true;
323
324 }
325
326 } // Tizen1_0
327 } // TizenApis
328