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 JSContactManagerChangeCallbackManager.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #include "JSContactManagerChangeCallbackManager.h"
31 JSContactManagerChangeCallbackManagerPtr JSContactManagerChangeCallbackManager::createObject(JSContextRef context)
33 return JSContactManagerChangeCallbackManagerPtr( new JSContactManagerChangeCallbackManager(context) );
36 JSContactManagerChangeCallbackManager::JSContactManagerChangeCallbackManager(JSContextRef context,
37 JSObjectRef onPersonsAdded,
38 JSObjectRef onPersonsUpdated,
39 JSObjectRef onPersonsDeleted,
40 JSObjectRef onError ) :
41 m_onPersonsAdded(NULL),
42 m_onPersonsUpdated(NULL),
43 m_onPersonsDeleted(NULL),
48 setOnPersonsAdded(onPersonsAdded);
49 setOnPersonsUpdated(onPersonsUpdated);
50 setOnPersonsDeleted(onPersonsDeleted);
54 JSContactManagerChangeCallbackManager::~JSContactManagerChangeCallbackManager()
58 JSValueUnprotect(m_context, m_onPersonsAdded);
61 if(m_onPersonsUpdated)
63 JSValueUnprotect(m_context, m_onPersonsUpdated);
66 if(m_onPersonsDeleted)
68 JSValueUnprotect(m_context, m_onPersonsDeleted);
73 JSValueUnprotect(m_context, m_onError);
77 void JSContactManagerChangeCallbackManager::setOnPersonsAdded( JSValueRef onPersonsAdded )
81 if (m_onPersonsAdded != NULL)
83 JSValueUnprotect(m_context, m_onPersonsAdded);
86 m_onPersonsAdded = JSValueToObject( m_context, onPersonsAdded, NULL );
88 if (m_onPersonsAdded != NULL)
90 JSValueProtect(m_context, m_onPersonsAdded);
95 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsAdded() const
97 return m_onPersonsAdded;
100 void JSContactManagerChangeCallbackManager::setOnPersonsUpdated( JSValueRef onPersonsUpdated )
102 if (onPersonsUpdated)
104 if (m_onPersonsUpdated != NULL)
106 JSValueUnprotect(m_context, m_onPersonsUpdated);
109 m_onPersonsUpdated = JSValueToObject( m_context, onPersonsUpdated, NULL );
111 if (m_onPersonsUpdated != NULL)
113 JSValueProtect(m_context, m_onPersonsUpdated);
118 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsUpdated() const
120 return m_onPersonsUpdated;
123 void JSContactManagerChangeCallbackManager::setOnPersonsDeleted( JSValueRef onPersonsDeleted )
125 if (onPersonsDeleted)
127 if (m_onPersonsDeleted != NULL)
129 JSValueUnprotect(m_context, m_onPersonsDeleted);
132 m_onPersonsDeleted = JSValueToObject( m_context, onPersonsDeleted, NULL );
134 if (m_onPersonsDeleted != NULL)
136 JSValueProtect(m_context, m_onPersonsDeleted);
141 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsDeleted() const
143 return m_onPersonsDeleted;
146 void JSContactManagerChangeCallbackManager::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 JSContactManagerChangeCallbackManager::getOnError() const
169 void JSContactManagerChangeCallbackManager::setContext( JSContextRef context )
174 void JSContactManagerChangeCallbackManager::setObject( JSObjectRef object )
179 JSObjectRef JSContactManagerChangeCallbackManager::getObject() const
184 void JSContactManagerChangeCallbackManager::callOnPersonsAdded( JSValueRef contacts )
186 if ( m_onPersonsAdded == NULL )
188 //LoggerD("onpersonsadded callback is not set");
191 JSValueRef objParam[1] = { contacts };
192 makeCallback( m_context, NULL, m_onPersonsAdded, "onpersonsadded", objParam, 1 );
195 void JSContactManagerChangeCallbackManager::callOnPersonsUpdated( JSValueRef contacts )
197 if ( m_onPersonsUpdated == NULL )
199 //LoggerD("onpersonsupdated callback is not set");
202 JSValueRef objParam[1] = { contacts };
203 makeCallback( m_context, NULL, m_onPersonsUpdated, "onpersonsupdated", objParam, 1 );
206 void JSContactManagerChangeCallbackManager::callOnPersonsDeleted( JSValueRef contactIds )
208 if ( m_onPersonsDeleted == NULL )
210 //LoggerD("onpersonsdeleted callback is not set");
213 JSValueRef objParam[1] = { contactIds };
214 makeCallback( m_context, NULL, m_onPersonsDeleted, "onpersonsdeleted", objParam, 1 );
217 void JSContactManagerChangeCallbackManager::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 JSContactManagerChangeCallbackManager::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);