Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessagingStorage.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 /*
19  * @file        JSPluginTemplete.cpp
20  * @author      Jaehyun Park (jaehyun77.park@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSPluginTemplete class
23  */
24
25 #include "JSMessagingStorage.h"
26 #include "JSMessagingListener.h"
27 #include "ConverterMessage.h"
28 #include "MessagingStorageMultiCallback.h"
29 #include <API/Messaging/ReqReceiverMessage.h>
30 #include <API/Filter/SortMode.h>
31
32 #include <Tizen/Tizen/FilterConverter.h>
33
34 #include <CommonsJavaScript/Converter.h>
35 #include <CommonsJavaScript/Validator.h>
36 #include <CommonsJavaScript/JSUtils.h>
37 #include <CommonsJavaScript/JSCallbackManager.h>
38 #include <CommonsJavaScript/Utils.h>                                    
39 #include <CommonsJavaScript/JSDOMExceptionFactory.h>            
40 #include <CommonsJavaScript/SecurityExceptions.h>
41
42 #include <Tizen/Common/JSTizenExceptionFactory.h>
43 #include <Tizen/Common/JSTizenException.h>
44 #include <Tizen/Common/SecurityExceptions.h>
45
46 #include "MessagingErrorMsg.h"
47 #include "plugin_config.h"
48
49
50 using namespace std;
51 using namespace DPL;
52 using namespace WrtDeviceApis::Commons;
53 using namespace WrtDeviceApis::CommonsJavaScript;
54 using namespace TizenApis::Tizen1_0::Tizen;
55 using namespace TizenApis::Commons;
56
57
58 namespace TizenApis {
59 namespace Tizen1_0 {
60
61         JSClassRef JSMessagingStorage::m_jsClassRef = NULL;
62
63         JSClassDefinition JSMessagingStorage::m_classInfo = {
64                 0,
65                 kJSClassAttributeNone,
66                 "messagingStorage",
67                 NULL,
68                 NULL,
69                 m_function,
70                 initialize,
71                 finalize,
72                 NULL, //hasProperty,
73                 NULL, //getProperty,
74                 NULL, //setProperty,
75                 NULL, //deleteProperty,Geolocation
76                 NULL, //getPropertyNames,
77                 NULL,
78                 NULL,
79                 hasInstance,
80                 NULL
81         };
82
83         JSStaticFunction JSMessagingStorage::m_function[] = {
84                         { "addDraftMessage",                    JSMessagingStorage::addDraftMessage,                                     kJSPropertyAttributeNone },            
85                         { "findMessages",                       JSMessagingStorage::findMessages,                                        kJSPropertyAttributeNone },
86                         { "removeMessages",                     JSMessagingStorage::removeMessages,                      kJSPropertyAttributeNone },
87                         { "updateMessages",                     JSMessagingStorage::updateMessages,                      kJSPropertyAttributeNone },
88                         { "findConversations",                  JSMessagingStorage::findConversations,                   kJSPropertyAttributeNone },
89                         { "removeConversations",                JSMessagingStorage::removeConversations,                         kJSPropertyAttributeNone },
90                         { "findFolders",                                JSMessagingStorage::findFolders,                                 kJSPropertyAttributeNone },
91                         { "addMessagesChangeListener",  JSMessagingStorage::addMessagesChangeListener,   kJSPropertyAttributeNone },
92                         { "addConversationsChangeListener",     JSMessagingStorage::addConversationsChangeListener,      kJSPropertyAttributeNone },
93                         { "addFoldersChangeListener",   JSMessagingStorage::addFoldersChangeListener,    kJSPropertyAttributeNone },                    
94                         { "removeChangeListener",                               JSMessagingStorage::removeChangeListener,                                kJSPropertyAttributeNone },
95                         { 0, 0, 0 }
96         };
97
98         const JSClassRef JSMessagingStorage::getClassRef() {
99                 if (!m_jsClassRef) {
100                         m_jsClassRef = JSClassCreate(&m_classInfo);
101                 }
102                 return m_jsClassRef;
103         }
104
105         void JSMessagingStorage::initialize(JSContextRef context, JSObjectRef object) {
106                 LogDebug("creation messaging instance");
107
108                 JSMessagingListener* priv = new JSMessagingListener(context);
109                 JSObjectSetPrivate(object, static_cast<void*>(priv));
110                 LogDebug("global context=" << priv->getContext());
111
112         }
113
114         void JSMessagingStorage::finalize(JSObjectRef object) {
115                 LogDebug("enter");
116                 JSMessagingListener* priv =
117                         static_cast<JSMessagingListener*>(JSObjectGetPrivate(object));
118                 if (priv) {
119                         // deregister from incoming message notifications
120                         priv->deregisterMessageReceivedEmitter();
121
122                         LogDebug("deleting private object (messaging)");
123                         // for messaging object, private object is shared pointer, so will be deleted automatically
124                         delete priv;
125                         // object will be deleted, but used to show that private object is deleted
126                         JSObjectSetPrivate(object, NULL);
127                 }
128
129         }
130
131         bool JSMessagingStorage::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
132                 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
133         }
134
135         JSValueRef JSMessagingStorage::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
136                 {
137                 LogDebug("<<< ");
138                 JSValueRef retVal = NULL;
139
140                 Try{
141                         Converter convert(context);
142                         if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_INBOX")) {
143                                 LogDebug("FOLDER_INBOX" << ":" << "1");
144                                 retVal = convert.toJSValueRef(Api::Messaging::INBOX);
145                         } else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_OUTBOX")) {
146                                 LogDebug("FOLDER_OUTBOX" << ":" << "2");
147                                 retVal = convert.toJSValueRef(Api::Messaging::OUTBOX);
148                         } else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_DRAFTS")) {
149                                 LogDebug("FOLDER_DRAFTS" << ":" << "3");
150                                 retVal = convert.toJSValueRef(Api::Messaging::DRAFTBOX);
151                         } else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_SENTBOX")) {
152                                 LogDebug("FOLDER_SENTBOX" << ":" << "4");
153                                 retVal = convert.toJSValueRef(Api::Messaging::SENTBOX);
154                         } else{
155                                 retVal = JSValueMakeUndefined(context);
156                 }
157                 }Catch (WrtDeviceApis::Commons::Exception){
158                         LogError("Exception: " << _rethrown_exception.GetMessage());
159                         return JSTizenExceptionFactory::postException(context, exception, 
160                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
161
162         }
163
164                 LogDebug(">>>");
165                 return retVal;
166         }
167
168         JSValueRef JSMessagingStorage::addDraftMessage(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
169                 const JSValueRef arguments[], JSValueRef* exception) {
170
171         LogDebug("<<<");
172
173         JSMessagingListener* priv = static_cast<JSMessagingListener*>
174         (JSObjectGetPrivate(thisObject));
175
176         if (!priv) {
177                 LogError(">>> Null pointer");
178                 return JSTizenExceptionFactory::postException(context, exception, 
179                    JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
180
181         }
182
183         JSContextRef globalContext = priv->getContext();
184
185         if (argumentCount < 1) {
186                 LogError(">>> TypeMismatchException");
187                 return JSTizenExceptionFactory::postException(context, exception, 
188                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
189
190         }
191
192         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
193
194         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
195         Try {
196
197                 //check permission.
198                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
199                         priv->getContext(),
200                         MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_DRAFT_MESSAGE]);
201                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
202
203                 Api::Messaging::EventAddDraftMessagePtr event(new Api::Messaging::EventAddDraftMessage());
204
205                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
206
207                 if (argumentCount > 1) {
208                         callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
209                 }
210                 
211                 if (argumentCount > 2) {
212                         callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
213                 }
214                 
215                 event->msg = converter->toIMessage(arguments[0]);
216
217         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(
218                                   callbackManager));
219         event->setForAsynchronousCall(priv);
220
221         Api::Messaging::ReqReceiverMessageSingleton::Instance().addDraftMessage(event);
222
223         return JSValueMakeNull(context);
224         }Catch(WrtDeviceApis::Commons::ConversionException) {
225                 LogError(">>> TypeMismatchException");
226                 return JSTizenExceptionFactory::postException(context, exception, 
227                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
228         }Catch(WrtDeviceApis::Commons::NullPointerException) {
229                 LogError(">>> TypeMismatchException");
230                 return JSTizenExceptionFactory::postException(context, exception, 
231                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
232         }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
233                 LogError(">>> InvalidValuesException");
234                 if(argumentCount < 3)
235                 {
236                         return JSTizenExceptionFactory::postException(context, exception, 
237                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
238                 }                       
239                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
240                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
241         }Catch(WrtDeviceApis::Commons::PlatformException) {
242                 LogError(">>> UnknownException");
243                 return JSTizenExceptionFactory::postException(context, exception, 
244                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
245         }               
246
247         LogError(">>> JSValueMakeNull");
248         return JSValueMakeNull(context);
249         
250         }
251
252
253                 JSValueRef JSMessagingStorage::findMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
254                         const JSValueRef arguments[], JSValueRef* exception) {
255
256                 LogDebug("<<<");
257
258                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
259                 (JSObjectGetPrivate(thisObject));
260
261                 if (!priv) {
262                         LogError(">>> Null pointer");
263                         return JSTizenExceptionFactory::postException(context, exception, 
264                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
265
266                 }
267
268                 JSContextRef globalContext = priv->getContext();
269
270                 if (argumentCount < 2) {
271                         LogError(">>> TypeMismatchException");
272                         return JSTizenExceptionFactory::postException(context, exception, 
273                                  JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
274
275                 }
276
277                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
278
279                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
280                 Try {
281
282                         //check permission.
283                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
284                                 priv->getContext(),
285                                 MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_MESSAGES]);
286                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
287
288                         Api::Messaging::EventQueryMessagesPtr event(new Api::Messaging::EventQueryMessages());
289
290                                 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
291
292                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);
293                                 event->setFilter(filter);
294
295                         callbackManager->setOnSuccess(converter->toFunction(arguments[1]));     
296                         
297                         if(argumentCount > 2)
298                         {
299                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
300                         }
301
302                         if ((argumentCount >= 3) && !JSValueIsNull(context, arguments[0]) && !JSValueIsUndefined(context, arguments[0])) {
303                                 switch(argumentCount){
304                                         case 6:{
305                                                 long offset = filterConverter->toLong(arguments[5]);
306                                                 LogDebug("offset:" << offset);
307                                                 event->setOffset(offset);
308                                         }
309                                         case 5:{
310                                                 long limit = filterConverter->toLong(arguments[4]);
311                                                 LogDebug("limit:" << limit);
312                                                 event->setLimit(limit);
313                                         }
314                                         case 4:{
315                                                 Api::Tizen::SortModePtr sortMode = filterConverter->toSortMode(arguments[3]);
316                                                 event->setSortMode(sortMode);
317                                                 break;
318                                         }
319                                 }
320                         }
321
322                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
323                         event->setForAsynchronousCall(priv);
324
325                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryMessages(event);
326
327                         LogError(">>>");
328                         return JSValueMakeNull(context);
329                 }Catch(WrtDeviceApis::Commons::ConversionException) {
330                         LogError(">>> TypeMismatchException");
331                         return JSTizenExceptionFactory::postException(context, exception, 
332                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
333                 }Catch(WrtDeviceApis::Commons::NullPointerException) {
334                         LogError(">>> TypeMismatchException");
335                         return JSTizenExceptionFactory::postException(context, exception, 
336                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
337                 }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
338                         LogError(">>> InvalidValuesException");
339                         if(argumentCount < 3)
340                         {
341                                 return JSTizenExceptionFactory::postException(context, exception, 
342                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
343                         }                       
344                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
345                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
346                 }Catch(WrtDeviceApis::Commons::PlatformException) {
347                         LogError(">>> UnknownException");
348                         return JSTizenExceptionFactory::postException(context, exception, 
349                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
350                 }               
351
352                 LogError(">>> JSValueMakeNull");
353                 return JSValueMakeNull(context);
354         }
355
356         JSValueRef JSMessagingStorage::removeMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
357                         const JSValueRef arguments[], JSValueRef* exception) {
358
359                 LogDebug("enter");
360
361                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
362                 (JSObjectGetPrivate(thisObject));
363
364                 if (!priv) {
365                         LogError("Null pointer");
366                         return JSTizenExceptionFactory::postException(context, exception, 
367                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
368
369                 }
370
371                 JSContextRef globalContext = priv->getContext();
372
373                 if (argumentCount < 1) {
374                         return JSTizenExceptionFactory::postException(context, exception, 
375                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
376                 }
377
378                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
379
380                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
381                 Try {
382
383                         //check permission.
384                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
385                                 priv->getContext(),
386                                 MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_MESSAGES]);
387                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
388
389                         Api::Messaging::EventDeleteMessagesPtr event(new Api::Messaging::EventDeleteMessages());
390
391
392                         if (argumentCount > 1) {
393                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
394                         }
395
396                         if (argumentCount > 2) {
397                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
398                         }
399
400                         std::vector<Api::Messaging::IMessagePtr> msgs;
401                                 
402                         JSObjectRef objArg = converter->toJSObjectRef(arguments[0]);
403                         LogDebug("array length "<<JSGetArrayLength(context, objArg));
404                         for (std::size_t i = 0; i < JSGetArrayLength(context, objArg); i++){
405                                 JSValueRef element = JSGetArrayElement(context, objArg, i);
406                                 JSObjectRef arg = JSValueToObject(context, element, NULL);
407                                 Api::Messaging::IMessagePtr msg = converter->toIMessage(arg);
408                                 msgs.push_back(msg);
409                         }
410                                 event->msgArray = msgs;
411
412                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
413                         event->setForAsynchronousCall(priv);
414
415                         Api::Messaging::ReqReceiverMessageSingleton::Instance().deleteMessages(event);
416
417                         return JSValueMakeNull(context);
418                 }Catch(WrtDeviceApis::Commons::ConversionException) {
419                         LogError(">>> TypeMismatchException");
420                         return JSTizenExceptionFactory::postException(context, exception, 
421                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
422                 }Catch(WrtDeviceApis::Commons::NullPointerException) {
423                         LogError(">>> TypeMismatchException");
424                         return JSTizenExceptionFactory::postException(context, exception, 
425                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
426                 }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
427                         LogError(">>> InvalidValuesException");
428                         if(argumentCount < 3)
429                         {
430                                 return JSTizenExceptionFactory::postException(context, exception, 
431                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
432                         }
433                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
434                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
435                 }Catch(WrtDeviceApis::Commons::PlatformException) {
436                         LogError(">>> UnknownException");
437                         return JSTizenExceptionFactory::postException(context, exception, 
438                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
439                 }
440                 
441                 return JSValueMakeNull(context);
442         }
443
444     JSValueRef JSMessagingStorage::updateMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
445                 const JSValueRef arguments[], JSValueRef* exception) {
446
447     LogDebug("enter");
448
449     JSMessagingListener* priv = static_cast<JSMessagingListener*>
450         (JSObjectGetPrivate(thisObject));
451
452     if (!priv) {
453         LogError("Null pointer");
454            return JSTizenExceptionFactory::postException(context, exception, 
455                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
456
457     }
458
459     JSContextRef globalContext = priv->getContext();
460
461     if (argumentCount < 1) {
462            return JSTizenExceptionFactory::postException(context, exception, 
463                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
464     }
465
466     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(
467             globalContext);
468
469     ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
470         
471     Try {
472
473                 //check permission.
474                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
475                         priv->getContext(),
476                         MessagingExportedNames[MESSAGING_FUNCTION_API_UPDATE_MESSAGES]);
477                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
478
479         Api::Messaging::EventUpdateMessagesPtr event(new Api::Messaging::EventUpdateMessages());
480
481                 if (argumentCount > 1) {
482                         callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
483                 }
484                 
485                 if (argumentCount > 2) {
486                         callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
487                 }
488
489
490                 std::vector<Api::Messaging::IMessagePtr> msgs;
491                 
492                 JSObjectRef objArg = converter->toJSObjectRef(arguments[0]);
493                 LogDebug("array length "<<JSGetArrayLength(context, objArg));
494                 for (std::size_t i = 0; i < JSGetArrayLength(context, objArg); i++)
495                 {
496                         JSValueRef element = JSGetArrayElement(context, objArg, i);
497                         JSObjectRef arg = JSValueToObject(context, element, NULL);
498                         Api::Messaging::IMessagePtr msg = converter->toIMessage(arg);
499                         msgs.push_back(msg);
500                 }
501                 event->msgArray = msgs;
502
503         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(
504                                   callbackManager));
505         event->setForAsynchronousCall(priv);
506
507         Api::Messaging::ReqReceiverMessageSingleton::Instance().updateMessages(event);
508
509         return JSValueMakeNull(context);
510         }
511         Catch(WrtDeviceApis::Commons::ConversionException) {
512                 LogError(">>> TypeMismatchException");
513                 return JSTizenExceptionFactory::postException(context, exception, 
514                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
515         }Catch(WrtDeviceApis::Commons::NullPointerException) {
516                 LogError(">>> TypeMismatchException");
517                 return JSTizenExceptionFactory::postException(context, exception, 
518                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
519         }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
520                 LogError(">>> InvalidValuesException");
521                 if(argumentCount < 3)
522                 {
523                         return JSTizenExceptionFactory::postException(context, exception, 
524                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
525                 }               
526                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
527                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
528         }Catch(WrtDeviceApis::Commons::PlatformException) {
529                 LogError(">>> UnknownException");
530                 return JSTizenExceptionFactory::postException(context, exception, 
531                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
532         }
533
534     return JSValueMakeNull(context);
535     }
536
537         JSValueRef JSMessagingStorage::findConversations(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
538                 const JSValueRef arguments[], JSValueRef* exception)
539         {
540
541                 LogDebug("<<<");
542
543                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
544                 (JSObjectGetPrivate(thisObject));
545
546                 if (!priv) {
547                         LogError(">>> Null pointer");
548                         return JSTizenExceptionFactory::postException(context, exception, 
549                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
550                 }
551
552                 JSContextRef globalContext = priv->getContext();
553
554                 if (argumentCount < 2) {
555                         LogError(">>> TypeMismatchException");
556                         return JSTizenExceptionFactory::postException(context, exception, 
557                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
558
559                 }
560
561                 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
562
563                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
564                 Try {
565
566                         //check permission.
567                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
568                                 priv->getContext(),
569                                 MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_CONVERSATIONS]);
570                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
571         
572                         Api::Messaging::EventQueryConversationsPtr event(new Api::Messaging::EventQueryConversations());
573
574                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
575
576                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);
577                         event->setFilter(filter);
578
579                         callbackManager->setOnSuccess(converter->toFunction(arguments[1]));
580                         
581                         if (argumentCount > 2)
582                         {
583                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
584                         }
585
586                         // setup message filter
587                         if ((argumentCount >= 3) && !JSValueIsNull(context, arguments[0]) && !JSValueIsUndefined(context, arguments[0]))
588                         {
589                                 switch(argumentCount)
590                                 {
591                                         case 6:
592                                         {
593                                                 long offset = filterConverter->toLong(arguments[5]);
594                                                 LogDebug("offset:" << offset);
595                                                 event->setOffset(offset);
596                                         }
597                                         case 5:
598                                         {
599                                                 long limit = filterConverter->toLong(arguments[4]);
600                                                 LogDebug("limit:" << limit);
601                                                 event->setLimit(limit);
602                                         }
603                                         case 4:
604                                         {
605                                                 Api::Tizen::SortModePtr sortMode = filterConverter->toSortMode(arguments[3]);
606                                                 event->setSortMode(sortMode);
607                                                 break;
608                                         }
609                                 }
610                         }
611
612                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
613                         event->setForAsynchronousCall(priv);
614
615                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryConversations(event);
616
617                         return JSValueMakeNull(context);
618                 }
619                 Catch(WrtDeviceApis::Commons::ConversionException) {
620                         LogError(">>> TypeMismatchException");
621                         return JSTizenExceptionFactory::postException(context, exception, 
622                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
623                 }Catch(WrtDeviceApis::Commons::NullPointerException) {
624                         LogError(">>> TypeMismatchException");
625                         return JSTizenExceptionFactory::postException(context, exception, 
626                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
627                 }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
628                         LogError(">>> InvalidValuesException");
629                         if(argumentCount < 3)
630                         {
631                                 return JSTizenExceptionFactory::postException(context, exception, 
632                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
633                         }
634                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
635                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));                   
636                 }Catch(WrtDeviceApis::Commons::PlatformException) {
637                         LogError(">>> UnknownException");
638                         return JSTizenExceptionFactory::postException(context, exception, 
639                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
640                 }
641
642                 LogError(">>> JSValueMakeNull");
643                 return JSValueMakeNull(context);
644
645
646         }
647
648         JSValueRef JSMessagingStorage::removeConversations(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
649                 const JSValueRef arguments[], JSValueRef* exception)
650         {
651                 LogDebug("enter");
652
653                 JSMessagingListener* priv = static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
654
655                 if (!priv) {
656                         LogError("Null pointer");
657                         return JSTizenExceptionFactory::postException(context, exception, 
658                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
659                 }
660
661                 JSContextRef globalContext = priv->getContext();
662
663                 if (argumentCount < 1) {
664                         return JSTizenExceptionFactory::postException(context, exception, 
665                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
666                 }
667
668                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(
669                 globalContext);
670
671                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
672
673                 Try {
674
675                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
676                                 priv->getContext(),
677                                 MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_CONVERSATIONS]);
678                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
679
680                         Api::Messaging::EventDeleteConversationsPtr event(new Api::Messaging::EventDeleteConversations());
681
682                         if (argumentCount > 1) {
683                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
684                         }
685
686                         if (argumentCount > 2) {
687                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
688                         }
689                         //check permission.
690
691                         std::vector<Api::Messaging::IConversationPtr> deleteConversationArray = converter->toVectorOfConversation(arguments[0]);
692                         event->setConversations(deleteConversationArray);
693                         event->setFilterUsing(false);
694
695                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
696                         event->setForAsynchronousCall(priv);
697
698                         Api::Messaging::ReqReceiverMessageSingleton::Instance().deleteConversations(event);
699
700                 }
701                 Catch(WrtDeviceApis::Commons::ConversionException) {
702                         LogError(">>> TypeMismatchException");
703                         return JSTizenExceptionFactory::postException(context, exception, 
704                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
705                 }Catch(WrtDeviceApis::Commons::NullPointerException) {
706                         LogError(">>> TypeMismatchException");
707                         return JSTizenExceptionFactory::postException(context, exception, 
708                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
709                 }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
710                         LogError(">>> InvalidValuesException");
711                         if(argumentCount < 3)
712                         {
713                                 return JSTizenExceptionFactory::postException(context, exception, 
714                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
715                         }
716                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
717                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));                   
718                 }Catch(WrtDeviceApis::Commons::PlatformException) {
719                         LogError(">>> UnknownException");
720                         return JSTizenExceptionFactory::postException(context, exception, 
721                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
722                 }
723                 return JSValueMakeNull(context);
724         }
725
726         JSValueRef JSMessagingStorage::findFolders(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
727                 const JSValueRef arguments[], JSValueRef* exception)
728         {
729
730                 LogDebug("enter");
731
732                 JSMessagingListener* priv = static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
733
734                 if (!priv) {
735                         LogError("Null pointer");
736                         return JSTizenExceptionFactory::postException(context, exception, 
737                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
738                 }
739
740                 JSContextRef globalContext = priv->getContext();
741
742                 if (argumentCount < 2) {
743                         return JSTizenExceptionFactory::postException(context, exception, 
744                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
745                 }
746
747                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
748                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
749
750                 // TODO : need to re-implement for supporting filter
751                 Try {
752                         //check permission.
753                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
754                                 priv->getContext(),
755                                 MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_FOLDERS]);
756                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
757                         
758                         Api::Messaging::EventQueryFoldersPtr event(new Api::Messaging::EventQueryFolders());
759         
760                         // setup message filter
761                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
762
763                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);                                                         
764                         event->setFilter(filter);
765
766                         callbackManager->setOnSuccess(converter->toFunction(arguments[1]));     
767                         
768                         if(argumentCount > 2)
769                         {
770                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
771                         }
772
773                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
774                         event->setForAsynchronousCall(priv);
775
776                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryFolders(event);
777
778                         return JSValueMakeNull(context);
779
780         }
781         Catch(WrtDeviceApis::Commons::ConversionException) {
782                 LogError(">>> TypeMismatchException");
783                 return JSTizenExceptionFactory::postException(context, exception, 
784                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
785         }Catch(WrtDeviceApis::Commons::NullPointerException) {
786                 LogError(">>> TypeMismatchException");
787                 return JSTizenExceptionFactory::postException(context, exception, 
788                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
789         }Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
790                 LogError(">>> InvalidValuesException");
791                 if(argumentCount < 3)
792                 {
793                         return JSTizenExceptionFactory::postException(context, exception, 
794                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
795                 }               
796                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
797                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
798         }Catch(WrtDeviceApis::Commons::PlatformException) {
799                 LogError(">>> UnknownException");
800                 return JSTizenExceptionFactory::postException(context, exception, 
801                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
802         }
803
804     return JSValueMakeNull(context);
805     }
806
807         JSValueRef JSMessagingStorage::addMessagesChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
808                 const JSValueRef arguments[], JSValueRef* exception) {
809
810                 LogInfo("<<<");
811                 LogDebug("arumentConunt:" << argumentCount);
812
813                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
814                         (JSObjectGetPrivate(thisObject));
815                         
816                 if (!priv) {
817                         LogError("Null pointer");
818                         return JSTizenExceptionFactory::postException(context, exception, 
819                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
820                 }
821
822                 if (argumentCount < 1 ||
823                         JSValueIsUndefined(context, arguments[0]) ||
824                         JSValueIsNull(context, arguments[0]))
825                 {
826                         LogError("bad argument");
827                         return JSTizenExceptionFactory::postException(context, exception, 
828                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
829                 }
830
831                 JSMessagingListener* listener =
832                         static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
833                 if (!listener) {
834                         LogError("no private object");
835                         return JSValueMakeUndefined(context);
836                 }
837
838     Try
839     {
840
841                 //check permission.
842                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
843                 priv->getContext(),
844                 MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_MESSAGES_CHANGE_LISTNER]);      
845
846                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
847                 
848         ConverterMessageFactory::ConverterType converter =
849             ConverterMessageFactory::getConverter(context);
850         JSContextRef l_globalContext = converter->toJSGlobalContext(thisObject);
851
852                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
853                 WrtDeviceApis::CommonsJavaScript::JSCallbackManager::createObject(l_globalContext,
854                                                                                                                 NULL,
855                                                                                                                 NULL);
856
857                 OnMessagesChanged result = converter->toMessageMultifunctions(arguments[0]);
858                 int funtionIndex = result.functionIndex;
859
860                 callbackManager->setOnSuccess(result.messagesAdded);
861                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesUpdated, NULL);
862                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesRemoved, NULL);
863
864
865                 EventOnMessagingStorageChangesPrivateDataPtr privData(
866                         new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
867
868         //filter
869         if ((argumentCount == 2) && !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]))
870         {
871             FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
872             privData->setFilter(filterConverter->toFilter(arguments[1]));
873         }
874
875                 listener->registerMessageReceivedEmitter(privData);
876
877         // add to map and return index
878         return JSValueMakeNumber(context,
879                                  listener->addIncomingMsgCallback(callbackManager, funtionIndex));
880
881     }
882     Catch(WrtDeviceApis::Commons::ConversionException) {
883         LogError("Error on conversion");
884            return JSTizenExceptionFactory::postException(context, exception, 
885                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
886     }
887     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
888         LogError("Invalid argument exception");
889           return JSTizenExceptionFactory::postException(context, exception, 
890                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
891     }
892     Catch(WrtDeviceApis::Commons::UnsupportedException) {
893         LogError("Unsupported exception");
894                 return JSTizenExceptionFactory::postException(context, exception, 
895                 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
896     }
897     Catch(WrtDeviceApis::Commons::AlreadyInUseException) {
898         LogError("Already in use exception");
899     }
900     Catch(WrtDeviceApis::Commons::PlatformException) {
901         LogError("not registered for incoming message notifications");
902     }
903
904     return JSValueMakeUndefined(context);
905     }
906
907         JSValueRef JSMessagingStorage::addConversationsChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
908                 const JSValueRef arguments[], JSValueRef* exception) {
909
910                 LogInfo("<<<");
911                 LogDebug("arumentConunt:" << argumentCount);
912
913                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
914                         (JSObjectGetPrivate(thisObject));
915                         
916                 if (!priv) {
917                         LogError("Null pointer");
918                         return JSTizenExceptionFactory::postException(context, exception, 
919                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
920                 }
921
922                 if (argumentCount < 1 ||
923                         JSValueIsUndefined(context, arguments[0]) ||
924                         JSValueIsNull(context, arguments[0]))
925                 {
926                         LogError("bad argument");
927                         return JSTizenExceptionFactory::postException(context, exception, 
928                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
929                 }
930
931                 JSMessagingListener* listener =
932                         static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
933                 if (!listener) {
934                         LogError("no private object");
935                         return JSValueMakeUndefined(context);
936                 }
937
938     Try
939     {
940
941                 //check permission.
942                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
943                 priv->getContext(),
944                 MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_CONVERSATIONS_CHANGE_LISTNER]);         
945
946                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
947                 
948         ConverterMessageFactory::ConverterType converter =
949             ConverterMessageFactory::getConverter(context);
950         JSContextRef l_globalContext = converter->toJSGlobalContext(thisObject);
951
952                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
953                 WrtDeviceApis::CommonsJavaScript::JSCallbackManager::createObject(l_globalContext,
954                                                                                                                 NULL,
955                                                                                                                 NULL);
956
957                 OnMessagesChanged result = converter->toMessageMultifunctions(arguments[0]);
958                 int funtionIndex = result.functionIndex;
959
960                 callbackManager->setOnSuccess(result.messagesAdded);
961                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesUpdated, NULL);
962                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesRemoved, NULL);
963
964
965                 EventOnMessagingStorageChangesPrivateDataPtr privData(
966                         new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
967
968         //filter
969         if ((argumentCount == 2) && !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]))
970         {
971             FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
972             privData->setFilter(filterConverter->toFilter(arguments[1]));
973         }
974
975                 listener->registerMessageReceivedEmitter(privData);
976
977         // add to map and return index
978         return JSValueMakeNumber(context,
979                                  listener->addIncomingMsgCallback(callbackManager, funtionIndex));
980
981     }
982     Catch(WrtDeviceApis::Commons::ConversionException) {
983         LogError("Error on conversion");
984            return JSTizenExceptionFactory::postException(context, exception, 
985                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
986     }
987     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
988         LogError("Invalid argument exception");
989           return JSTizenExceptionFactory::postException(context, exception, 
990                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
991     }
992     Catch(WrtDeviceApis::Commons::UnsupportedException) {
993         LogError("Unsupported exception");
994                 return JSTizenExceptionFactory::postException(context, exception, 
995                 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
996     }
997     Catch(WrtDeviceApis::Commons::AlreadyInUseException) {
998         LogError("Already in use exception");
999     }
1000     Catch(WrtDeviceApis::Commons::PlatformException) {
1001         LogError("not registered for incoming message notifications");
1002     }
1003
1004     return JSValueMakeUndefined(context);
1005     }
1006
1007
1008         JSValueRef JSMessagingStorage::addFoldersChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
1009                 const JSValueRef arguments[], JSValueRef* exception) {
1010
1011                 LogInfo("<<<");
1012                 LogDebug("arumentConunt:" << argumentCount);
1013
1014                 JSMessagingListener* priv = static_cast<JSMessagingListener*>
1015                         (JSObjectGetPrivate(thisObject));
1016                         
1017                 if (!priv) {
1018                         LogError("Null pointer");
1019                         return JSTizenExceptionFactory::postException(context, exception, 
1020                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1021                 }
1022
1023                 if (argumentCount < 1 ||
1024                         JSValueIsUndefined(context, arguments[0]) ||
1025                         JSValueIsNull(context, arguments[0]))
1026                 {
1027                         LogError("bad argument");
1028                         return JSTizenExceptionFactory::postException(context, exception, 
1029                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1030                 }
1031
1032                 JSMessagingListener* listener =
1033                         static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
1034                 if (!listener) {
1035                         LogError("no private object");
1036                         return JSValueMakeUndefined(context);
1037                 }
1038
1039     Try
1040     {
1041
1042                 //check permission.
1043                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1044                 priv->getContext(),
1045                 MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_FOLDERS_CHANGE_LISTNER]);       
1046
1047                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1048                 
1049         ConverterMessageFactory::ConverterType converter =
1050             ConverterMessageFactory::getConverter(context);
1051         JSContextRef l_globalContext = converter->toJSGlobalContext(thisObject);
1052
1053                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
1054                 WrtDeviceApis::CommonsJavaScript::JSCallbackManager::createObject(l_globalContext,
1055                                                                                                                 NULL,
1056                                                                                                                 NULL);
1057
1058                 OnMessagesChanged result = converter->toMessageMultifunctions(arguments[0]);
1059                 int funtionIndex = result.functionIndex;
1060
1061                 callbackManager->setOnSuccess(result.messagesAdded);
1062                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesUpdated, NULL);
1063                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm = JSCallbackManager::createObject(listener->getContext(), result.messagesRemoved, NULL);
1064
1065
1066                 EventOnMessagingStorageChangesPrivateDataPtr privData(
1067                         new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
1068
1069         //filter
1070         if ((argumentCount == 2) && !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]))
1071         {
1072             FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1073             privData->setFilter(filterConverter->toFilter(arguments[1]));
1074         }
1075
1076                 listener->registerMessageReceivedEmitter(privData);
1077
1078         // add to map and return index
1079         return JSValueMakeNumber(context,
1080                                  listener->addIncomingMsgCallback(callbackManager, funtionIndex));
1081
1082     }
1083     Catch(WrtDeviceApis::Commons::ConversionException) {
1084         LogError("Error on conversion");
1085            return JSTizenExceptionFactory::postException(context, exception, 
1086                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1087     }
1088     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
1089         LogError("Invalid argument exception");
1090           return JSTizenExceptionFactory::postException(context, exception, 
1091                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1092     }
1093     Catch(WrtDeviceApis::Commons::UnsupportedException) {
1094         LogError("Unsupported exception");
1095                 return JSTizenExceptionFactory::postException(context, exception, 
1096                 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1097     }
1098     Catch(WrtDeviceApis::Commons::AlreadyInUseException) {
1099         LogError("Already in use exception");
1100     }
1101     Catch(WrtDeviceApis::Commons::PlatformException) {
1102         LogError("not registered for incoming message notifications");
1103     }
1104
1105     return JSValueMakeUndefined(context);
1106     }
1107
1108     JSValueRef JSMessagingStorage::removeChangeListener(JSContextRef context,
1109         JSObjectRef function,
1110         JSObjectRef thisObject,
1111         size_t argumentCount,
1112         const JSValueRef arguments[],
1113         JSValueRef* exception)
1114     {
1115     LogInfo("entered");
1116
1117     JSMessagingListener* priv = static_cast<JSMessagingListener*>
1118         (JSObjectGetPrivate(thisObject));
1119
1120     if (!priv) {
1121         LogError("Null pointer");
1122            return JSTizenExceptionFactory::postException(context, exception, 
1123                                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1124     }
1125
1126     if (argumentCount < 1) {
1127         LogError("wrong argument count");
1128                 return JSTizenExceptionFactory::postException(context, exception, 
1129                                  JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1130     }
1131
1132         //check permission.
1133         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1134         priv->getContext(),
1135         MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_CHANGE_LISTENER]);   
1136         
1137         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1138
1139
1140     JSMessagingListener* listener =
1141         static_cast<JSMessagingListener*>(JSObjectGetPrivate(thisObject));
1142     if (!listener) {
1143         LogError("no private object");
1144         return JSValueMakeUndefined(context);
1145     }
1146
1147     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
1148     Try {
1149         double handler = converter.toDouble(arguments[0]);
1150         listener->removeIncomingMsgCallback(handler);
1151     }
1152     Catch(WrtDeviceApis::Commons::ConversionException) {
1153         LogError("conversion error");
1154            return JSTizenExceptionFactory::postException(context, exception, 
1155                                  JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1156     }
1157
1158     return JSValueMakeUndefined(context);
1159     }
1160
1161
1162 }
1163 }