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 * @file JSAddressBookChangeCallbackManager.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
24 #include "JSAddressBookChangeCallbackManager.h"
31 JSAddressBookChangeCallbackManagerPtr JSAddressBookChangeCallbackManager::createObject(JSContextRef context)
33 ////LogDebug("entered");
35 return JSAddressBookChangeCallbackManagerPtr( new JSAddressBookChangeCallbackManager(context) );
38 JSAddressBookChangeCallbackManager::JSAddressBookChangeCallbackManager(JSContextRef context,
39 JSObjectRef onContactsAdded,
40 JSObjectRef onContactsUpdated,
41 JSObjectRef onContactsDeleted,
42 JSObjectRef onAddressBookReset,
43 JSObjectRef onError ) :
44 m_onContactsAdded(NULL),
45 m_onContactsUpdated(NULL),
46 m_onContactsDeleted(NULL),
47 m_onAddressBookReset(NULL),
52 //LogDebug("entered");
54 setOnContactsAdded(onContactsAdded);
55 setOnContactsUpdated(onContactsUpdated);
56 setOnContactsDeleted(onContactsDeleted);
57 setOnAddressBookReset(onAddressBookReset);
61 JSAddressBookChangeCallbackManager::~JSAddressBookChangeCallbackManager()
63 //LogDebug("entered");
67 JSValueUnprotect(m_context, m_onContactsAdded);
70 if(m_onContactsUpdated)
72 JSValueUnprotect(m_context, m_onContactsUpdated);
75 if(m_onContactsDeleted)
77 JSValueUnprotect(m_context, m_onContactsDeleted);
80 if(m_onAddressBookReset)
82 JSValueUnprotect(m_context, m_onAddressBookReset);
87 JSValueUnprotect(m_context, m_onError);
91 void JSAddressBookChangeCallbackManager::setOnContactsAdded( JSValueRef onContactsAdded )
93 //LogDebug("entered");
96 if (m_onContactsAdded != NULL)
98 JSValueUnprotect(m_context, m_onContactsAdded);
101 m_onContactsAdded = JSValueToObject( m_context, onContactsAdded, NULL );
103 if (m_onContactsAdded != NULL)
105 JSValueProtect(m_context, m_onContactsAdded);
110 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsAdded() const
112 //LogDebug("entered");
113 return m_onContactsAdded;
116 void JSAddressBookChangeCallbackManager::setOnContactsUpdated( JSValueRef onContactsUpdated )
118 //LogDebug("entered");
119 if (onContactsUpdated)
121 if (m_onContactsUpdated != NULL)
123 JSValueUnprotect(m_context, m_onContactsUpdated);
126 m_onContactsUpdated = JSValueToObject( m_context, onContactsUpdated, NULL );
128 if (m_onContactsUpdated != NULL)
130 JSValueProtect(m_context, m_onContactsUpdated);
135 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsUpdated() const
137 //LogDebug("entered");
138 return m_onContactsUpdated;
141 void JSAddressBookChangeCallbackManager::setOnContactsDeleted( JSValueRef onContactsDeleted )
143 //LogDebug("entered");
144 if (onContactsDeleted)
146 if (m_onContactsDeleted != NULL)
148 JSValueUnprotect(m_context, m_onContactsDeleted);
151 m_onContactsDeleted = JSValueToObject( m_context, onContactsDeleted, NULL );
153 if (m_onContactsDeleted != NULL)
155 JSValueProtect(m_context, m_onContactsDeleted);
160 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsDeleted() const
162 //LogDebug("entered");
163 return m_onContactsDeleted;
166 void JSAddressBookChangeCallbackManager::setOnAddressBookReset( JSValueRef onAddressBookReset )
168 //LogDebug("entered");
169 if (onAddressBookReset)
171 if (m_onAddressBookReset != NULL)
173 JSValueUnprotect(m_context, m_onAddressBookReset);
176 m_onAddressBookReset = JSValueToObject( m_context, onAddressBookReset, NULL );
178 if (m_onAddressBookReset != NULL)
180 JSValueProtect(m_context, m_onAddressBookReset);
185 JSValueRef JSAddressBookChangeCallbackManager::getOnAddressBookReset() const
187 //LogDebug("entered");
188 return m_onAddressBookReset;
191 void JSAddressBookChangeCallbackManager::setOnError( JSValueRef onError )
193 //LogDebug("entered");
196 if (m_onError != NULL)
198 JSValueUnprotect(m_context, m_onError);
201 m_onError = JSValueToObject( m_context, onError, NULL );
203 if (m_onError != NULL)
205 JSValueProtect(m_context, m_onError);
210 JSValueRef JSAddressBookChangeCallbackManager::getOnError() const
212 //LogDebug("entered");
216 void JSAddressBookChangeCallbackManager::setContext( JSContextRef context )
218 //LogDebug("entered");
222 void JSAddressBookChangeCallbackManager::setObject( JSObjectRef object )
224 //LogDebug("entered");
228 JSObjectRef JSAddressBookChangeCallbackManager::getObject() const
230 //LogDebug("entered");
234 void JSAddressBookChangeCallbackManager::callOnContactsAdded( JSValueRef contacts )
236 //LogDebug("entered");
237 if ( m_onContactsAdded == NULL )
239 //LogDebug("oncontactsadded callback is not set");
242 JSValueRef objParam[1] = { contacts };
243 makeCallback( m_context, NULL, m_onContactsAdded, "oncontactsadded", objParam, 1 );
246 void JSAddressBookChangeCallbackManager::callOnContactsUpdated( JSValueRef contacts )
248 //LogDebug("entered");
249 if ( m_onContactsUpdated == NULL )
251 //LogDebug("oncontactsupdated callback is not set");
254 JSValueRef objParam[1] = { contacts };
255 makeCallback( m_context, NULL, m_onContactsUpdated, "oncontactsupdated", objParam, 1 );
258 void JSAddressBookChangeCallbackManager::callOnContactsDeleted( JSValueRef contactIds )
260 //LogDebug("entered");
261 if ( m_onContactsDeleted == NULL )
263 //LogDebug("oncontactsdeleted callback is not set");
266 JSValueRef objParam[1] = { contactIds };
267 makeCallback( m_context, NULL, m_onContactsDeleted, "oncontactsdeleted", objParam, 1 );
270 void JSAddressBookChangeCallbackManager::callOnAddressBookReset()
272 //LogDebug("entered");
273 if ( m_onAddressBookReset == NULL )
275 //LogDebug("onaddressbookreset callback is not set");
278 makeCallback( m_context, NULL, m_onAddressBookReset, "onaddressbookreset", NULL, 0 );
281 void JSAddressBookChangeCallbackManager::callOnError( JSValueRef error )
283 //LogDebug("entered");
284 if ( m_onError == NULL )
286 //LogDebug("Error callback is not set");
289 JSValueRef objParam[1] = { error };
290 makeCallback( m_context, NULL, m_onError, "onError", objParam, 1 );
293 void JSAddressBookChangeCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
295 //LogDebug("entered");
297 if (callback == NULL)
299 LogError("callback is NULL");
303 if (JSObjectIsFunction(context, callback))
307 //LogDebug("Calling object directly, no arguments");
308 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
312 //LogDebug("Calling object directly, one argument");
313 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);