Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSAddressBookChangeCallbackManager.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  * @file        JSAddressBookChangeCallbackManager.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include "JSAddressBookChangeCallbackManager.h"
25 #include <dpl/log.h>
26
27 namespace TizenApis {
28 namespace Tizen1_0 {
29 namespace Contact {
30
31 JSAddressBookChangeCallbackManagerPtr JSAddressBookChangeCallbackManager::createObject(JSContextRef context)
32 {
33         ////LogDebug("entered");
34
35         return JSAddressBookChangeCallbackManagerPtr( new JSAddressBookChangeCallbackManager(context) );
36 }
37
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),
48                 m_onError(NULL),
49                 m_context(context),
50                 m_object(NULL)
51 {
52     //LogDebug("entered");
53
54         setOnContactsAdded(onContactsAdded);
55         setOnContactsUpdated(onContactsUpdated);
56         setOnContactsDeleted(onContactsDeleted);
57         setOnAddressBookReset(onAddressBookReset);
58     setOnError(onError);
59 }
60
61 JSAddressBookChangeCallbackManager::~JSAddressBookChangeCallbackManager()
62 {
63     //LogDebug("entered");
64
65         if(m_onContactsAdded)
66         {
67                 JSValueUnprotect(m_context, m_onContactsAdded);
68         }
69
70         if(m_onContactsUpdated)
71         {
72                 JSValueUnprotect(m_context, m_onContactsUpdated);
73         }
74
75         if(m_onContactsDeleted)
76         {
77                 JSValueUnprotect(m_context, m_onContactsDeleted);
78         }
79
80         if(m_onAddressBookReset)
81         {
82                 JSValueUnprotect(m_context, m_onAddressBookReset);
83         }
84
85     if(m_onError)
86     {
87         JSValueUnprotect(m_context, m_onError);
88     }
89 }
90
91 void JSAddressBookChangeCallbackManager::setOnContactsAdded( JSValueRef onContactsAdded )
92 {
93         //LogDebug("entered");
94         if (onContactsAdded)
95         {
96                 if (m_onContactsAdded != NULL)
97                 {
98                         JSValueUnprotect(m_context, m_onContactsAdded);
99                 }
100
101                 m_onContactsAdded = JSValueToObject( m_context, onContactsAdded, NULL );
102
103                 if (m_onContactsAdded != NULL)
104                 {
105                         JSValueProtect(m_context, m_onContactsAdded);
106                 }
107         }
108 }
109
110 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsAdded() const
111 {
112         //LogDebug("entered");
113         return m_onContactsAdded;
114 }
115
116 void JSAddressBookChangeCallbackManager::setOnContactsUpdated( JSValueRef onContactsUpdated )
117 {
118         //LogDebug("entered");
119         if (onContactsUpdated)
120         {
121                 if (m_onContactsUpdated != NULL)
122                 {
123                         JSValueUnprotect(m_context, m_onContactsUpdated);
124                 }
125
126                 m_onContactsUpdated = JSValueToObject( m_context, onContactsUpdated, NULL );
127
128                 if (m_onContactsUpdated != NULL)
129                 {
130                         JSValueProtect(m_context, m_onContactsUpdated);
131                 }
132         }
133 }
134
135 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsUpdated() const
136 {
137         //LogDebug("entered");
138         return m_onContactsUpdated;
139 }
140
141 void JSAddressBookChangeCallbackManager::setOnContactsDeleted( JSValueRef onContactsDeleted )
142 {
143         //LogDebug("entered");
144         if (onContactsDeleted)
145         {
146                 if (m_onContactsDeleted != NULL)
147                 {
148                         JSValueUnprotect(m_context, m_onContactsDeleted);
149                 }
150
151                 m_onContactsDeleted = JSValueToObject( m_context, onContactsDeleted, NULL );
152
153                 if (m_onContactsDeleted != NULL)
154                 {
155                         JSValueProtect(m_context, m_onContactsDeleted);
156                 }
157         }
158 }
159
160 JSValueRef JSAddressBookChangeCallbackManager::getOnContactsDeleted() const
161 {
162         //LogDebug("entered");
163         return m_onContactsDeleted;
164 }
165
166 void JSAddressBookChangeCallbackManager::setOnAddressBookReset( JSValueRef onAddressBookReset )
167 {
168         //LogDebug("entered");
169         if (onAddressBookReset)
170         {
171                 if (m_onAddressBookReset != NULL)
172                 {
173                         JSValueUnprotect(m_context, m_onAddressBookReset);
174                 }
175
176                 m_onAddressBookReset = JSValueToObject( m_context, onAddressBookReset, NULL );
177
178                 if (m_onAddressBookReset != NULL)
179                 {
180                         JSValueProtect(m_context, m_onAddressBookReset);
181                 }
182         }
183 }
184
185 JSValueRef JSAddressBookChangeCallbackManager::getOnAddressBookReset() const
186 {
187         //LogDebug("entered");
188         return m_onAddressBookReset;
189 }
190
191 void JSAddressBookChangeCallbackManager::setOnError( JSValueRef onError )
192 {
193     //LogDebug("entered");
194     if (onError)
195     {
196         if (m_onError != NULL)
197         {
198             JSValueUnprotect(m_context, m_onError);
199         }
200
201         m_onError = JSValueToObject( m_context, onError, NULL );
202
203         if (m_onError != NULL)
204         {
205             JSValueProtect(m_context, m_onError);
206         }
207     }
208 }
209
210 JSValueRef JSAddressBookChangeCallbackManager::getOnError() const
211 {
212     //LogDebug("entered");
213     return m_onError;
214 }
215
216 void JSAddressBookChangeCallbackManager::setContext( JSContextRef context )
217 {
218     //LogDebug("entered");
219     m_context = context;
220 }
221
222 void JSAddressBookChangeCallbackManager::setObject( JSObjectRef object )
223 {
224     //LogDebug("entered");
225     m_object = object;
226 }
227
228 JSObjectRef JSAddressBookChangeCallbackManager::getObject() const
229 {
230     //LogDebug("entered");
231     return m_object;
232 }
233
234 void JSAddressBookChangeCallbackManager::callOnContactsAdded( JSValueRef contacts )
235 {
236     //LogDebug("entered");
237     if ( m_onContactsAdded == NULL )
238     {
239         //LogDebug("oncontactsadded callback is not set");
240         return;
241     }
242     JSValueRef objParam[1] = { contacts };
243     makeCallback( m_context, NULL, m_onContactsAdded, "oncontactsadded", objParam, 1 );
244 }
245
246 void JSAddressBookChangeCallbackManager::callOnContactsUpdated( JSValueRef contacts )
247 {
248     //LogDebug("entered");
249     if ( m_onContactsUpdated == NULL )
250     {
251         //LogDebug("oncontactsupdated callback is not set");
252         return;
253     }
254     JSValueRef objParam[1] = { contacts };
255     makeCallback( m_context, NULL, m_onContactsUpdated, "oncontactsupdated", objParam, 1 );
256 }
257
258 void JSAddressBookChangeCallbackManager::callOnContactsDeleted( JSValueRef contactIds )
259 {
260     //LogDebug("entered");
261     if ( m_onContactsDeleted == NULL )
262     {
263         //LogDebug("oncontactsdeleted callback is not set");
264         return;
265     }
266     JSValueRef objParam[1] = { contactIds };
267     makeCallback( m_context, NULL, m_onContactsDeleted, "oncontactsdeleted", objParam, 1 );
268 }
269
270 void JSAddressBookChangeCallbackManager::callOnAddressBookReset()
271 {
272     //LogDebug("entered");
273     if ( m_onAddressBookReset == NULL )
274     {
275         //LogDebug("onaddressbookreset callback is not set");
276         return;
277     }
278     makeCallback( m_context, NULL, m_onAddressBookReset, "onaddressbookreset", NULL, 0 );
279 }
280
281 void JSAddressBookChangeCallbackManager::callOnError( JSValueRef error )
282 {
283     //LogDebug("entered");
284     if ( m_onError == NULL )
285     {
286         //LogDebug("Error callback is not set");
287         return;
288     }
289     JSValueRef objParam[1] = { error };
290     makeCallback( m_context, NULL, m_onError, "onError", objParam, 1 );
291 }
292
293 void JSAddressBookChangeCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
294 {
295     //LogDebug("entered");
296
297     if (callback == NULL)
298     {
299         LogError("callback is NULL");
300         return;
301     }
302
303     if (JSObjectIsFunction(context, callback))
304     {
305         if (argc == 0)
306         {
307                 //LogDebug("Calling object directly, no arguments");
308                 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
309         }
310         else
311         {
312                 //LogDebug("Calling object directly, one argument");
313                 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
314         }
315         return;
316     }
317 }
318
319 } // Contact
320 } // Tizen1_0
321 } // TizenApis