2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
30 #include "JSCallHistoryEntry.h"
31 #include "JSCallHistory.h"
32 #include "ResponseDispatcher.h"
33 #include "Converter.h"
34 #include "plugin_config.h"
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;
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"
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"
60 JSClassRef JSCallHistory::m_jsClassRef = NULL;
62 JSClassDefinition JSCallHistory::m_classInfo =
65 kJSClassAttributeNone,
83 JSStaticValue JSCallHistory::m_property[] = {
87 JSStaticFunction JSCallHistory::m_function[] =
89 { "find", JSCallHistory::find, kJSPropertyAttributeNone },
90 { "remove", JSCallHistory::remove, kJSPropertyAttributeNone },
91 { "removeBatch", JSCallHistory::removeBatch, kJSPropertyAttributeNone },
95 const JSClassRef JSCallHistory::getClassRef() {
97 m_jsClassRef = JSClassCreate(&m_classInfo);
102 const JSClassDefinition* JSCallHistory::getClassInfo(){
106 void JSCallHistory::initialize(JSContextRef context, JSObjectRef object) {
107 JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
110 ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
111 priv = new JSCallHistoryPriv(context, CallHistory);
112 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
118 void JSCallHistory::finalize(JSObjectRef object) {
119 JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
121 JSObjectSetPrivate(object, NULL);
126 JSObjectRef JSCallHistory::createJSObject(JSContextRef context, JSObjectRef object)
128 ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
129 JSCallHistoryPriv* priv = new JSCallHistoryPriv(context, CallHistory);
131 return JSObjectMake(context, getClassRef(), priv);
134 bool JSCallHistory::hasInstance(JSContextRef context, JSObjectRef constructor,
135 JSValueRef possibleInstance, JSValueRef* exception) {
136 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
139 JSValueRef JSCallHistory::find(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
140 const JSValueRef arguments[], JSValueRef* exception) {
142 if (argumentCount < 1) {
143 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
146 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
147 JSContextRef gContext = priv->getContext();
149 assert(priv && "Invalid private pointer.");
150 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
151 Validator check(context, exception);
154 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
156 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
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 ");
165 cbm->setOnError(arguments[1]);
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 ");
175 cbm->setOnSuccess(arguments[0]);
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");
186 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
188 CALL_HISTORY_FUNCTION_API_FIND);
190 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
192 EventFindCallHistoryPtr event(new EventFindCallHistory());
193 ICallHistoryPtr callHistory(priv->getObject());
194 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
195 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
197 if (argumentCount >= 3) {
198 if (!check.isNullOrUndefined(arguments[2])) {
199 FilterPtr filter = filterConverter->toFilter(arguments[2]);
200 event ->setFilter(filter);
204 if (argumentCount >= 4) {
205 if (!check.isNullOrUndefined(arguments[3])) {
206 event->setSortMode(filterConverter->toSortModeArray(arguments[3]));
210 if (argumentCount >= 5) {
211 if (!check.isNullOrUndefined(arguments[4])) {
212 event->setLimit(converter.toULong(arguments[4]));
216 if (argumentCount >= 6) {
217 if (!check.isNullOrUndefined(arguments[5])) {
218 event->setOffset(converter.toULong(arguments[5]));
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());
233 return JSValueMakeUndefined(context);
236 JSValueRef JSCallHistory::remove(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
237 const JSValueRef arguments[], JSValueRef* exception) {
239 if (argumentCount < 1) {
240 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
243 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
244 JSContextRef gContext = priv->getContext();
246 assert(priv && "Invalid private pointer.");
247 Converter converter(context);
248 Validator check(context, exception);
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");
257 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
259 CALL_HISTORY_FUNCTION_API_REMOVE);
261 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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());
276 return JSValueMakeUndefined(context);
279 JSValueRef JSCallHistory::removeBatch(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
280 const JSValueRef arguments[], JSValueRef* exception) {
282 if (argumentCount < 1) {
283 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
286 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
287 JSContextRef gContext = priv->getContext();
289 assert(priv && "Invalid private pointer.");
290 Converter converter(context);
291 Validator check(context, exception);
293 if (!JSIsArrayValue(context, arguments[0])) {
294 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallHistoryEntry array");
298 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
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");
307 cbm->setOnError(arguments[2]);
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");
316 cbm->setOnSuccess(arguments[1]);
320 AceSecurityStatus status = CALL_HISTORY_CHECK_ACCESS(
322 CALL_HISTORY_FUNCTION_API_REMOVE_BATCH);
324 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
326 EventRemoveBatchPtr event(new EventRemoveBatch());
327 ICallHistoryPtr callHistory(priv->getObject());
329 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
330 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
332 CallHistoryEntryList entryList = converter.toVectorOfCallHistoryEntryProperties(arguments[0]);
334 std::vector<unsigned long> entryIds;
336 if (entryList.size() > 0) {
337 for (unsigned int i = 0; i < entryList.size(); i++) {
338 entryIds.push_back(entryList[i]->getEntryId());
340 event->setEntryIds(entryIds);
341 callHistory->removeBatch(event);
343 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : Entry array is null");
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());
355 return JSValueMakeUndefined(context);