5aec3200398e5e5e415f869dd6928110d13572cd
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCallHistory.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 <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <Tizen/Tizen/FilterConverter.h>
23 #include <API/Call/EventFindCallHistory.h>
24 #include <API/Call/EventRemoveBatch.h>
25 #include <API/Call/CallHistoryFactory.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h> 
28 #include <Tizen/Common/SecurityExceptions.h>
29
30 #include "JSCallHistoryEntry.h"
31 #include "JSCallHistory.h"
32 #include "ResponseDispatcher.h"
33 #include "Converter.h"
34 #include "plugin_config.h"
35
36 using namespace std;
37 using namespace DPL;
38 using namespace TizenApis::Api::Tizen;
39 using namespace TizenApis::Api::Call;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace TizenApis::Tizen1_0::Tizen;
43 using namespace TizenApis::Commons;
44
45 #define STR_CALL_TYPE_UNKNOWN                           "CALL_TYPE_UNKNOWN"
46 #define STR_CALL_TYPE_AUDIO                                     "CALL_TYPE_AUDIO"
47 #define STR_CALL_TYPE_VIDEO                                     "CALL_TYPE_VIDEO"
48
49 #define STR_CALL_STATE_UNKNOWN                          "CALL_STATE_UNKNOWN"
50 #define STR_CALL_STATE_INCOMING                         "CALL_STATE_INCOMING"
51 #define STR_CALL_STATE_OUTGOING                         "CALL_STATE_OUTGOING"
52 #define STR_CALL_STATE_MISSED_CHECKED           "CALL_STATE_MISSED_CHECKED"
53 #define STR_CALL_STATE_MISSED_UNCHECKED         "CALL_STATE_MISSED_UNCHECKED"
54 #define STR_CALL_STATE_REJECTED                         "CALL_STATE_REJECTED"
55 #define STR_CALL_STATE_BLOCKED                          "CALL_STATE_BLOCKED"
56
57 namespace TizenApis {
58 namespace Tizen1_0 {
59
60 JSClassRef JSCallHistory::m_jsClassRef = NULL;
61
62 JSClassDefinition JSCallHistory::m_classInfo =
63 {
64         0,
65         kJSClassAttributeNone,
66         "callhistory",
67         NULL,
68         m_property,
69         m_function,
70         initialize,
71         finalize,
72         NULL,
73         NULL,
74         NULL,
75         NULL,
76         NULL,
77         NULL,
78         NULL,
79         hasInstance,
80         NULL
81 };
82
83 JSStaticValue JSCallHistory::m_property[] = {
84         { 0, 0, 0, 0 }
85 };
86
87 JSStaticFunction JSCallHistory::m_function[] =
88 {
89         { "find", JSCallHistory::find, kJSPropertyAttributeNone },
90         { "remove",      JSCallHistory::remove,      kJSPropertyAttributeNone },
91         { "removeBatch",      JSCallHistory::removeBatch,      kJSPropertyAttributeNone },
92         { 0, 0, 0 }
93 };
94
95 const JSClassRef JSCallHistory::getClassRef() {
96         if (!m_jsClassRef) {
97                 m_jsClassRef = JSClassCreate(&m_classInfo);
98         }
99         return m_jsClassRef;
100 }
101
102 const JSClassDefinition* JSCallHistory::getClassInfo(){
103         return &m_classInfo;
104 }
105
106 void JSCallHistory::initialize(JSContextRef context, JSObjectRef object) {
107         JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
108
109         if (priv == NULL) {
110                 ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
111                 priv = new JSCallHistoryPriv(context, CallHistory);
112                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
113                         delete priv;
114                 }
115         }
116 }
117
118 void JSCallHistory::finalize(JSObjectRef object) {
119         JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
120         if (priv != NULL) {
121                 JSObjectSetPrivate(object, NULL);
122                 delete priv;
123         }
124 }
125
126 JSObjectRef JSCallHistory::createJSObject(JSContextRef context, JSObjectRef object)
127 {
128         ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
129         JSCallHistoryPriv* priv = new JSCallHistoryPriv(context, CallHistory);
130
131         return JSObjectMake(context, getClassRef(), priv);
132 }
133
134 bool JSCallHistory::hasInstance(JSContextRef context, JSObjectRef constructor,
135         JSValueRef possibleInstance, JSValueRef* exception) {
136         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
137 }
138
139 JSValueRef JSCallHistory::find(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
140         const JSValueRef arguments[], JSValueRef* exception) {
141
142         if (argumentCount < 1) {
143                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
144         }
145
146         JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
147         JSContextRef gContext = priv->getContext();
148
149         assert(priv && "Invalid private pointer.");
150         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
151         Validator check(context, exception);
152
153         try {
154                 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
155
156                 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
157                 if (cbm != NULL) {
158                 }
159
160                 if (argumentCount >= 2) {
161                         if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
162                                 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
163                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback ");
164                                 }
165                                 cbm->setOnError(arguments[1]);
166                         }
167                 }
168
169                 if (argumentCount >= 1) {
170                         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
171                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : success callback ");
172                         } else if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[0]))) {
173                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback ");
174                         }
175                         cbm->setOnSuccess(arguments[0]);
176                 }
177
178                 if (argumentCount >= 3) {
179                         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
180                                 if (!JSValueIsObject(context, arguments[2])) {
181                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : filter");
182                                 }
183                         }
184                 }
185
186                 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
187                                 gContext,
188                                 CALL_HISTORY_FUNCTION_API_FIND);
189
190                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
191
192                 EventFindCallHistoryPtr event(new EventFindCallHistory());
193                 ICallHistoryPtr callHistory(priv->getObject());
194                 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
195                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
196
197                 if (argumentCount >= 3) {
198                         if (!check.isNullOrUndefined(arguments[2])) {
199                                 FilterPtr filter = filterConverter->toFilter(arguments[2]);
200                                 event ->setFilter(filter);
201                         }
202                 }
203
204                 if (argumentCount >= 4) {
205                         if (!check.isNullOrUndefined(arguments[3])) {
206                                 event->setSortMode(filterConverter->toSortModeArray(arguments[3]));
207                         }
208                 }
209
210                 if (argumentCount >= 5) {
211                         if (!check.isNullOrUndefined(arguments[4])) {
212                                 event->setLimit(converter.toULong(arguments[4]));
213                         }
214                 }
215
216                 if (argumentCount >= 6) {
217                         if (!check.isNullOrUndefined(arguments[5])) {
218                                 event->setOffset(converter.toULong(arguments[5]));
219                         }
220                 }
221
222                 callHistory->find(event);
223         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
224                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
225         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
226                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
227         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
228                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
229         } catch(const WrtDeviceApis::Commons::Exception& ex) {
230                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
231         }
232
233         return JSValueMakeUndefined(context);
234 }
235
236 JSValueRef JSCallHistory::remove(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
237         const JSValueRef arguments[], JSValueRef* exception) {
238
239         if (argumentCount < 1) {
240                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
241         }
242
243         JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
244         JSContextRef gContext = priv->getContext();
245
246         assert(priv && "Invalid private pointer.");
247         Converter converter(context);
248         Validator check(context, exception);
249
250         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
251                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : CallHistoryEntry");
252         } else if (!JSValueIsObject(context, arguments[0])) {
253                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallHistoryEntry");
254         }
255
256         try {
257                 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
258                                 gContext,
259                                 CALL_HISTORY_FUNCTION_API_REMOVE);
260
261                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
262
263                 ICallHistoryPtr callHistory(priv->getObject());
264                 CallHistoryEntryPropertiesPtr entry = converter.toCallHistoryEntryProperties(arguments[0]);
265                 callHistory->remove(entry->getEntryId());
266         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
267                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
268         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
269                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
270         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
271                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
272         } catch(const WrtDeviceApis::Commons::Exception& ex) {
273                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
274         }
275
276         return JSValueMakeUndefined(context);
277 }
278
279 JSValueRef JSCallHistory::removeBatch(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
280         const JSValueRef arguments[], JSValueRef* exception) {
281
282         if (argumentCount < 1) {
283                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
284         }
285
286         JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
287         JSContextRef gContext = priv->getContext();
288
289         assert(priv && "Invalid private pointer.");
290         Converter converter(context);
291         Validator check(context, exception);
292
293         if (!JSIsArrayValue(context, arguments[0])) {
294                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallHistoryEntry array");
295         }
296
297         try {
298                 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
299                 if (cbm != NULL) {
300                 }
301
302                 if (argumentCount >= 3) {
303                         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
304                                 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[2]))) {
305                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback");
306                                 }
307                                 cbm->setOnError(arguments[2]);
308                         }
309                 }
310
311                 if (argumentCount >= 2) {
312                         if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
313                                 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
314                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback");
315                                 }
316                                 cbm->setOnSuccess(arguments[1]);
317                         }
318                 }
319
320                 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
321                                 gContext,
322                                 CALL_HISTORY_FUNCTION_API_REMOVE_BATCH);
323
324                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
325
326                 EventRemoveBatchPtr event(new EventRemoveBatch());
327                 ICallHistoryPtr callHistory(priv->getObject());
328
329                 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
330                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
331
332                 CallHistoryEntryList entryList = converter.toVectorOfCallHistoryEntryProperties(arguments[0]);
333
334                 std::vector<unsigned long> entryIds;
335
336                 if (entryList.size() > 0) {
337                         for (unsigned int i = 0; i < entryList.size(); i++) {
338                                 entryIds.push_back(entryList[i]->getEntryId());
339                         }
340                         event->setEntryIds(entryIds);
341                         callHistory->removeBatch(event);
342                 } else {
343                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : Entry array is null");
344                 }
345         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
346                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
347         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
348                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
349         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
350                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
351         } catch(const WrtDeviceApis::Commons::Exception& ex) {
352                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
353         }
354
355         return JSValueMakeUndefined(context);
356 }
357
358 }
359 }
360