433ccef7c22af09b09c4567e716ea0111b02ccaa
[framework/web/wrt-plugins-tizen.git] / src / Callhistory / Converter.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 "Converter.h"
20 #include <dpl/log/log.h>
21
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/ScopedJSStringRef.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include "CallHistoryDefine.h"
28 #include "JSCallHistoryEntry.h"
29 #include "JSRemoteParty.h"
30
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34
35 namespace DeviceAPI {
36 namespace CallHistory {
37
38 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
39 {
40         static bool init = initializeAllowedProperties();
41         (void) init;
42 }
43
44 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& jsValue)
45 {
46     return toVectorOfT_(jsValue, &Converter::toULong);
47 }
48
49 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& jsValue)
50 {
51         if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
52                 ThrowMsg(Commons::ConversionException, "Undefined history entry");
53         }
54
55         if (!JSIsArrayValue(m_context, jsValue)) {
56                 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
57         }
58
59         try {
60                 CallHistoryEntryList result;
61                 JSObjectRef objArg = toJSObjectRef(jsValue);
62                 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
63                         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
64                         result.push_back(toCallHistoryEntryProperties(element));
65                 }
66                 return result;
67         } catch (Commons::ConversionException& ex) {
68                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
69         } catch (Commons::InvalidArgumentException& ex) {
70                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
71         } catch (Commons::Exception& ex) {
72                 ThrowMsg(Commons::Exception, ex.GetMessage());
73         }
74
75 }
76
77 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
78 {
79         StringArrayPtr result(new StringArray());
80         try {
81                 if (!JSValueIsNull(m_context, jsValue)) {
82                         JSObjectRef jsObject = toJSObjectRef(jsValue);
83                         
84                         for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
85                                 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
86                                 result->push_back(toString(element));
87                         }
88                 }
89         } catch (Commons::ConversionException& ex) {
90                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
91         } catch (Commons::InvalidArgumentException& ex) {
92                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
93         } catch (Commons::Exception& ex) {
94                 ThrowMsg(Commons::Exception, ex.GetMessage());
95         }
96         return result;
97 }
98
99 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
100 {
101         RemotePartyListPtr result(new RemotePartyList());
102         try {
103                 if (!JSValueIsNull(m_context, jsValue)) {
104                         JSObjectRef jsObject = toJSObjectRef(jsValue);
105                         
106                         for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
107                                 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
108                                 result->push_back(toRemoteParty(element));
109                         }
110                 }
111         } catch (Commons::ConversionException& ex) {
112                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
113         } catch (Commons::InvalidArgumentException& ex) {
114                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
115         } catch (Commons::Exception& ex) {
116                 ThrowMsg(Commons::Exception, ex.GetMessage());
117         }
118         return result;
119 }
120
121 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
122 {
123         try {
124                 return JSRemoteParty::getRemoteParty(m_context, jsValue);
125         } catch (Commons::ConversionException& ex) {
126                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
127         } catch (Commons::Exception& ex) {
128                 ThrowMsg(Commons::Exception, ex.GetMessage());
129         }
130 }
131
132 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
133 {
134         try {
135                 return JSCallHistoryEntry::getCallHistoryEntry(m_context, jsValue);
136         } catch (Commons::ConversionException& ex) {
137                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
138         } catch (Commons::Exception& ex) {
139                 ThrowMsg(Commons::Exception, ex.GetMessage());
140         }
141 }
142
143 EventCallHistoryListenerPrivateDataPtr 
144         Converter::toEventCallHistoryListenerPrivateData(const JSValueRef &jsValue, JSContextRef context)
145 {
146         if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
147                 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback is null or undefined");
148         }
149         if (!JSValueIsObject(m_context, jsValue) || JSObjectIsFunction(m_context, toJSObjectRef(jsValue))) {
150                 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback");
151         }
152
153         JSObjectRef objectCallbacks = toJSObjectRef(jsValue);
154         CallHistoryChangeCB result;
155
156         result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onadded");
157         result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onchanged");
158
159         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context);
160         JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context);
161
162         if (!JSValueIsNull(m_context, result.onAdded) && !JSValueIsUndefined(m_context, result.onAdded)) {
163                 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onAdded))) {
164                         onAddedCbm->setOnSuccess(result.onAdded);
165                 } else {
166                         ThrowMsg(Commons::ConversionException, "Type missmatch error : onadded callback");
167                 }
168         }
169
170         if (!JSValueIsNull(m_context, result.onChanged) && !JSValueIsUndefined(m_context, result.onChanged)) {
171                 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onChanged))) {
172                         onChangedCbm->setOnSuccess(result.onChanged);
173                 } else {                        
174                         ThrowMsg(Commons::ConversionException, "Type missmatch error : onchanged callback");
175                 }
176         }
177
178         return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm));
179 }
180
181 JSValueRef Converter::toJSValueRef(const CallHistoryEntryListPtr& arg, JSContextRef context)
182 {
183         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
184         if (!jsResult) {
185                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
186         }
187         CallHistoryEntryProperties tmpCallEntry;
188
189         try {
190                 for (size_t i = 0; i < (*arg).size(); i++) {
191                         tmpCallEntry = *((*arg)[i]);
192                         JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
193                         if (!jsObject) {
194                                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
195                         }
196
197                         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
198                                 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
199                         }
200                 }
201         } catch (Commons::ConversionException& ex) {
202                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
203         } catch (Commons::InvalidArgumentException& ex) {
204                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
205         } catch (Commons::Exception& ex) {
206                 ThrowMsg(Commons::Exception, ex.GetMessage());
207         }
208         return jsResult;
209 }
210
211 JSValueRef Converter::toJSValueRef(const StringArrayPtr &arg, JSContextRef context)
212 {
213         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
214         if (!jsResult) {
215                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
216         }
217
218         for (size_t i = 0; i < (*arg).size(); i++) {
219                 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
220                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
221                 }
222         }
223
224         return jsResult;
225 }
226
227 JSValueRef Converter::toJSValueRef(const RemotePartyListPtr& arg, JSContextRef context)
228 {
229         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
230         if (!jsResult) {
231                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
232         }
233
234         RemoteParty tmpRemoteParty;
235
236         try {
237                 for (size_t i = 0; i < (*arg).size(); i++) {
238                         tmpRemoteParty = *((*arg)[i]);
239                         JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
240                         if (!jsObject) {
241                                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
242                         }
243
244                         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
245                                 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
246                         }
247                 }
248         } catch (Commons::ConversionException& ex) {
249                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
250         } catch (Commons::InvalidArgumentException& ex) {
251                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
252         } catch (Commons::Exception& ex) {
253                 ThrowMsg(Commons::Exception, ex.GetMessage());
254         }
255         return jsResult;
256 }
257
258 bool Converter::initializeAllowedProperties()
259 {
260         return true;
261 }
262
263 }
264 }
265