Update change log and spec for wrt-plugins-tizen_0.4.13
[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
147         LogDebug("enter");
148
149         if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
150                 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback is null or undefined");
151         }
152         if (!JSValueIsObject(m_context, jsValue) || JSObjectIsFunction(m_context, toJSObjectRef(jsValue))) {
153                 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback");
154         }
155
156         JSObjectRef objectCallbacks = toJSObjectRef(jsValue);
157         CallHistoryChangeCB result;
158
159         result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onadded");
160         result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onchanged");
161         result.onRemoved = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onremoved");
162
163         JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context);
164         JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context);
165         JSCallbackManagerPtr onRemovedCbm = JSCallbackManager::createObject(context);
166
167         if (!JSValueIsNull(m_context, result.onAdded) && !JSValueIsUndefined(m_context, result.onAdded)) {
168                 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onAdded))) {
169                         onAddedCbm->setOnSuccess(result.onAdded);
170                 } else {
171                         ThrowMsg(Commons::ConversionException, "Type missmatch error : onadded callback");
172                 }
173         }
174
175         if (!JSValueIsNull(m_context, result.onChanged) && !JSValueIsUndefined(m_context, result.onChanged)) {
176                 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onChanged))) {
177                         onChangedCbm->setOnSuccess(result.onChanged);
178                 } else {                        
179                         ThrowMsg(Commons::ConversionException, "Type missmatch error : onchanged callback");
180                 }
181         }
182
183         if (!JSValueIsNull(m_context, result.onRemoved) && !JSValueIsUndefined(m_context, result.onRemoved)) {
184                 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onRemoved))) {
185                         onRemovedCbm->setOnSuccess(result.onRemoved);
186                         LogDebug("onRemoved register");
187                 } else {
188                         
189                         ThrowMsg(Commons::ConversionException, "Type missmatch error : onremoved callback");
190                 }
191         }
192
193
194         return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm, onRemovedCbm));
195 }
196
197 JSValueRef Converter::toJSValueRef(const CallHistoryEntryListPtr& arg, JSContextRef context)
198 {
199         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
200         if (!jsResult) {
201                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
202         }
203         CallHistoryEntryProperties tmpCallEntry;
204
205         try {
206                 for (size_t i = 0; i < (*arg).size(); i++) {
207                         tmpCallEntry = *((*arg)[i]);
208                         JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
209                         if (!jsObject) {
210                                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
211                         }
212
213                         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
214                                 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
215                         }
216                 }
217         } catch (Commons::ConversionException& ex) {
218                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
219         } catch (Commons::InvalidArgumentException& ex) {
220                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
221         } catch (Commons::Exception& ex) {
222                 ThrowMsg(Commons::Exception, ex.GetMessage());
223         }
224         return jsResult;
225 }
226
227 JSValueRef Converter::toJSValueRef(const StringArrayPtr &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         for (size_t i = 0; i < (*arg).size(); i++) {
235                 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
236                         ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
237                 }
238         }
239
240         return jsResult;
241 }
242
243 JSValueRef Converter::toJSValueRef(const RemotePartyListPtr& arg, JSContextRef context)
244 {
245         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
246         if (!jsResult) {
247                 ThrowMsg(Commons::ConversionException, "Could not create js array object");
248         }
249
250         RemoteParty tmpRemoteParty;
251
252         try {
253                 for (size_t i = 0; i < (*arg).size(); i++) {
254                         tmpRemoteParty = *((*arg)[i]);
255                         JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
256                         if (!jsObject) {
257                                 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
258                         }
259
260                         if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
261                                 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
262                         }
263                 }
264         } catch (Commons::ConversionException& ex) {
265                 ThrowMsg(Commons::ConversionException, ex.GetMessage());
266         } catch (Commons::InvalidArgumentException& ex) {
267                 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
268         } catch (Commons::Exception& ex) {
269                 ThrowMsg(Commons::Exception, ex.GetMessage());
270         }
271         return jsResult;
272 }
273
274 bool Converter::initializeAllowedProperties()
275 {
276         return true;
277 }
278
279 }
280 }
281