wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactManagerChangeCallbackManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        JSContactManagerChangeCallbackManager.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "JSContactManagerChangeCallbackManager.h"
26 #include <Logger.h>
27
28 namespace DeviceAPI {
29 namespace Contact {
30
31 JSContactManagerChangeCallbackManagerPtr JSContactManagerChangeCallbackManager::createObject(JSContextRef context)
32 {
33         return JSContactManagerChangeCallbackManagerPtr( new JSContactManagerChangeCallbackManager(context) );
34 }
35
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),
44                 m_onError(NULL),
45                 m_context(context),
46                 m_object(NULL)
47 {
48         setOnPersonsAdded(onPersonsAdded);
49         setOnPersonsUpdated(onPersonsUpdated);
50         setOnPersonsDeleted(onPersonsDeleted);
51     setOnError(onError);
52 }
53
54 JSContactManagerChangeCallbackManager::~JSContactManagerChangeCallbackManager()
55 {
56         if(m_onPersonsAdded)
57         {
58                 JSValueUnprotect(m_context, m_onPersonsAdded);
59         }
60
61         if(m_onPersonsUpdated)
62         {
63                 JSValueUnprotect(m_context, m_onPersonsUpdated);
64         }
65
66         if(m_onPersonsDeleted)
67         {
68                 JSValueUnprotect(m_context, m_onPersonsDeleted);
69         }
70
71     if(m_onError)
72     {
73         JSValueUnprotect(m_context, m_onError);
74     }
75 }
76
77 void JSContactManagerChangeCallbackManager::setOnPersonsAdded( JSValueRef onPersonsAdded )
78 {
79         if (onPersonsAdded)
80         {
81                 if (m_onPersonsAdded != NULL)
82                 {
83                         JSValueUnprotect(m_context, m_onPersonsAdded);
84                 }
85
86                 m_onPersonsAdded = JSValueToObject( m_context, onPersonsAdded, NULL );
87
88                 if (m_onPersonsAdded != NULL)
89                 {
90                         JSValueProtect(m_context, m_onPersonsAdded);
91                 }
92         }
93 }
94
95 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsAdded() const
96 {
97         return m_onPersonsAdded;
98 }
99
100 void JSContactManagerChangeCallbackManager::setOnPersonsUpdated( JSValueRef onPersonsUpdated )
101 {
102         if (onPersonsUpdated)
103         {
104                 if (m_onPersonsUpdated != NULL)
105                 {
106                         JSValueUnprotect(m_context, m_onPersonsUpdated);
107                 }
108
109                 m_onPersonsUpdated = JSValueToObject( m_context, onPersonsUpdated, NULL );
110
111                 if (m_onPersonsUpdated != NULL)
112                 {
113                         JSValueProtect(m_context, m_onPersonsUpdated);
114                 }
115         }
116 }
117
118 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsUpdated() const
119 {
120         return m_onPersonsUpdated;
121 }
122
123 void JSContactManagerChangeCallbackManager::setOnPersonsDeleted( JSValueRef onPersonsDeleted )
124 {
125         if (onPersonsDeleted)
126         {
127                 if (m_onPersonsDeleted != NULL)
128                 {
129                         JSValueUnprotect(m_context, m_onPersonsDeleted);
130                 }
131
132                 m_onPersonsDeleted = JSValueToObject( m_context, onPersonsDeleted, NULL );
133
134                 if (m_onPersonsDeleted != NULL)
135                 {
136                         JSValueProtect(m_context, m_onPersonsDeleted);
137                 }
138         }
139 }
140
141 JSValueRef JSContactManagerChangeCallbackManager::getOnPersonsDeleted() const
142 {
143         return m_onPersonsDeleted;
144 }
145
146 void JSContactManagerChangeCallbackManager::setOnError( JSValueRef onError )
147 {
148     if (onError)
149     {
150         if (m_onError != NULL)
151         {
152             JSValueUnprotect(m_context, m_onError);
153         }
154
155         m_onError = JSValueToObject( m_context, onError, NULL );
156
157         if (m_onError != NULL)
158         {
159             JSValueProtect(m_context, m_onError);
160         }
161     }
162 }
163
164 JSValueRef JSContactManagerChangeCallbackManager::getOnError() const
165 {
166     return m_onError;
167 }
168
169 void JSContactManagerChangeCallbackManager::setContext( JSContextRef context )
170 {
171     m_context = context;
172 }
173
174 void JSContactManagerChangeCallbackManager::setObject( JSObjectRef object )
175 {
176     m_object = object;
177 }
178
179 JSObjectRef JSContactManagerChangeCallbackManager::getObject() const
180 {
181     return m_object;
182 }
183
184 void JSContactManagerChangeCallbackManager::callOnPersonsAdded( JSValueRef contacts )
185 {
186     if ( m_onPersonsAdded == NULL )
187     {
188         //LoggerD("onpersonsadded callback is not set");
189         return;
190     }
191     JSValueRef objParam[1] = { contacts };
192     makeCallback( m_context, NULL, m_onPersonsAdded, "onpersonsadded", objParam, 1 );
193 }
194
195 void JSContactManagerChangeCallbackManager::callOnPersonsUpdated( JSValueRef contacts )
196 {
197     if ( m_onPersonsUpdated == NULL )
198     {
199         //LoggerD("onpersonsupdated callback is not set");
200         return;
201     }
202     JSValueRef objParam[1] = { contacts };
203     makeCallback( m_context, NULL, m_onPersonsUpdated, "onpersonsupdated", objParam, 1 );
204 }
205
206 void JSContactManagerChangeCallbackManager::callOnPersonsDeleted( JSValueRef contactIds )
207 {
208     if ( m_onPersonsDeleted == NULL )
209     {
210         //LoggerD("onpersonsdeleted callback is not set");
211         return;
212     }
213     JSValueRef objParam[1] = { contactIds };
214     makeCallback( m_context, NULL, m_onPersonsDeleted, "onpersonsdeleted", objParam, 1 );
215 }
216
217 void JSContactManagerChangeCallbackManager::callOnError( JSValueRef error )
218 {
219     if ( m_onError == NULL )
220     {
221         //LoggerD("Error callback is not set");
222         return;
223     }
224     JSValueRef objParam[1] = { error };
225     makeCallback( m_context, NULL, m_onError, "onError", objParam, 1 );
226 }
227
228 void JSContactManagerChangeCallbackManager::makeCallback(JSContextRef context, JSObjectRef object, JSObjectRef callback, const char *szName, JSValueRef argv[], unsigned argc)
229 {
230
231     if (callback == NULL)
232     {
233         LoggerE("callback is NULL");
234         return;
235     }
236
237     if (JSObjectIsFunction(context, callback))
238     {
239         if (argc == 0)
240         {
241                 //LoggerD("Calling object directly, no arguments");
242                 JSObjectCallAsFunction(context, callback, object, 0, NULL, NULL);
243         }
244         else
245         {
246                 //LoggerD("Calling object directly, one argument");
247                 JSObjectCallAsFunction(context, callback, object, argc, argv, NULL);
248         }
249         return;
250     }
251 }
252
253 } // Contact
254 } // DeviceAPI