2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSAddressBookChangeCallbackManager.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #include "JSAddressBookChangeCallbackManager.h"
31 JSAddressBookChangeCallbackManagerPtr JSAddressBookChangeCallbackManager::createObject(JSContextRef context)
33 return JSAddressBookChangeCallbackManagerPtr( new JSAddressBookChangeCallbackManager(context) );
36 JSAddressBookChangeCallbackManager::JSAddressBookChangeCallbackManager(JSContextRef context,
37 JSObjectRef onContactsAdded,
38 JSObjectRef onContactsUpdated,
39 JSObjectRef onContactsDeleted,
40 JSObjectRef onError ) :
41 m_onContactsAdded(NULL),
42 m_onContactsUpdated(NULL),
43 m_onContactsDeleted(NULL),
48 setOnContactsAdded(onContactsAdded);
49 setOnContactsUpdated(onContactsUpdated);
50 setOnContactsDeleted(onContactsDeleted);
54 JSAddressBookChangeCallbackManager::~JSAddressBookChangeCallbackManager()
58 JSValueUnprotect(m_context, m_onContactsAdded);
61 if(m_onContactsUpdated)
63 JSValueUnprotect(m_context, m_onContactsUpdated);
66 if(m_onContactsDeleted)
68 JSValueUnprotect(m_context, m_onContactsDeleted);
73 JSValueUnprotect(m_context, m_onError);
77 void JSAddressBookChangeCallbackManager::setOnContactsAdded( JSValueRef onContactsAdded )
81 if (m_onContactsAdded != NULL)
83 JSValueUnprotect(m_context, m_onContactsAdded);
86 m_onContactsAdded = JSValueToObject( m_context, onContactsAdded, NULL );
88 if (m_onContactsAdded != NULL)
90 JSValueProtect(m_context, m_onContactsAdded);
95 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsAdded() const
97 return m_onContactsAdded;
100 void JSAddressBookChangeCallbackManager::setOnContactsUpdated( JSValueRef onContactsUpdated )
102 if (onContactsUpdated)
104 if (m_onContactsUpdated != NULL)
106 JSValueUnprotect(m_context, m_onContactsUpdated);
109 m_onContactsUpdated = JSValueToObject( m_context, onContactsUpdated, NULL );
111 if (m_onContactsUpdated != NULL)
113 JSValueProtect(m_context, m_onContactsUpdated);
118 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsUpdated() const
120 return m_onContactsUpdated;
123 void JSAddressBookChangeCallbackManager::setOnContactsDeleted( JSValueRef onContactsDeleted )
125 if (onContactsDeleted)
127 if (m_onContactsDeleted != NULL)
129 JSValueUnprotect(m_context, m_onContactsDeleted);
132 m_onContactsDeleted = JSValueToObject( m_context, onContactsDeleted, NULL );
134 if (m_onContactsDeleted != NULL)
136 JSValueProtect(m_context, m_onContactsDeleted);
141 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsDeleted() const
143 return m_onContactsDeleted;
146 void JSAddressBookChangeCallbackManager::setOnError( JSValueRef onError )
150 if (m_onError != NULL)
152 JSValueUnprotect(m_context, m_onError);
155 m_onError = JSValueToObject( m_context, onError, NULL );
157 if (m_onError != NULL)
159 JSValueProtect(m_context, m_onError);
164 JSValueRef JSAddressBookChangeCallbackManager::getOnError() const
169 void JSAddressBookChangeCallbackManager::setContext( JSContextRef context )
174 void JSAddressBookChangeCallbackManager::setObject( JSObjectRef object )
179 JSObjectRef JSAddressBookChangeCallbackManager::getObject() const
184 void JSAddressBookChangeCallbackManager::callOnContactsAdded( JSValueRef contacts )
186 if ( m_onContactsAdded == NULL )
188 //LoggerD("oncontactsadded callback is not set");
191 JSValueRef objParam[1] = { contacts };
192 makeCallback( m_context, NULL, m_onContactsAdded, "oncontactsadded", objParam, 1 );
195 void JSAddressBookChangeCallbackManager::callOnContactsUpdated( JSValueRef contacts )
197 if ( m_onContactsUpdated == NULL )
199 //LoggerD("oncontactsupdated callback is not set");
202 JSValueRef objParam[1] = { contacts };
203 makeCallback( m_context, NULL, m_onContactsUpdated, "oncontactsupdated", objParam, 1 );
206 void JSAddressBookChangeCallbackManager::callOnContactsDeleted( JSValueRef contactIds )
208 if ( m_onContactsDeleted == NULL )
210 //LoggerD("oncontactsdeleted callback is not set");
213 JSValueRef objParam[1] = { contactIds };
214 makeCallback( m_context, NULL, m_onContactsDeleted, "oncontactsdeleted", objParam, 1 );
217 void JSAddressBookChangeCallbackManager::callOnError( JSValueRef error )
219 if ( m_onError == NULL )
221 //LoggerD("Error callback is not set");
224 JSValueRef objParam[1] = { error };
225 makeCallback( m_context, NULL, m_onError, "onError", objParam, 1 );
228 void JSAddressBookChangeCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
231 if (callback == NULL)
233 LoggerE("callback is NULL");
237 if (JSObjectIsFunction(context, callback))
241 //LoggerD("Calling object directly, no arguments");
242 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
246 //LoggerD("Calling object directly, one argument");
247 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);