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