Update change log and spec for wrt-plugins-tizen_0.4.38
[framework/web/wrt-plugins-tizen.git] / src / Messaging / MessagingListener.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 #include <vector>
19 #include <dpl/assert.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21
22 #include <IFilter.h>
23 #include "StorageChangesMessageGenerator.h"
24 #include "StorageChangesConversationGenerator.h"
25 #include "StorageChangesFolderGenerator.h"
26
27 #include <JSWebAPIErrorFactory.h>
28 #include <SecurityExceptions.h>
29
30 #include "MessagingErrorMsg.h"
31 #include "JSMessagingStorage.h"
32 #include "ConverterMessage.h"
33 #include "EventSendMessagePrivateData.h"
34 #include "MessagingListener.h"
35 #include "MessageAsyncCallbackManager.h"
36 #include <dpl/singleton_safe_impl.h>
37
38 IMPLEMENT_SAFE_SINGLETON(DeviceAPI::Messaging::MessagingListener)
39
40 using namespace std;
41
42 namespace DeviceAPI {
43 namespace Messaging {
44
45
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace DeviceAPI::Messaging;
49 using namespace DeviceAPI::Common;
50
51
52 MessagingListener::MessagingListener() :
53     EventAddDraftMessageAnswerReceiver(ThreadEnum::NULL_THREAD),
54     EventQueryMessagesAnswerReceiver(ThreadEnum::NULL_THREAD),
55     EventDeleteMessagesAnswerReceiver(ThreadEnum::NULL_THREAD),
56     EventUpdateMessagesAnswerReceiver(ThreadEnum::NULL_THREAD), 
57     EventUpdateMessageAnswerReceiver(ThreadEnum::NULL_THREAD),
58     EventMessageReceivedListener(ThreadEnum::NULL_THREAD),
59     EventQueryConversationsAnswerReceiver(ThreadEnum::NULL_THREAD),
60     EventDeleteConversationsAnswerReceiver(ThreadEnum::NULL_THREAD),
61     EventQueryFoldersAnswerReceiver(ThreadEnum::NULL_THREAD),    
62     m_nextHandle(1)
63 //    m_emiterMsgId(EmitterMessageReceived::emptyId),
64 //    m_emiterConvId(EmitterConversationReceived::emptyId),
65 //    m_emiterFolderId(EmitterFolderReceived::emptyId)
66 {
67 }
68
69 MessagingListener::~MessagingListener() {
70 }
71
72 void MessagingListener::OnAnswerReceived(
73         const EventAddDraftMessagePtr& event)
74 {
75     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
76         DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
77     Assert(NULL != callbackManager);
78     LoggerD("thread=" << DPL::Thread::GetCurrentThread());
79
80     JSContextRef context = callbackManager->getContext();
81     Assert(NULL != context);
82
83     MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
84
85     Try
86     {
87         if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
88             callbackManager->callOnSuccess();
89
90         } else {
91             switch (event->getExceptionCode()) {
92             case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
93                 Throw(WrtDeviceApis::Commons::PlatformException);
94             case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
95                 Throw(WrtDeviceApis::Commons::ConversionException);
96             case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
97                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98             default:
99                 Throw(WrtDeviceApis::Commons::UnknownException);
100             }
101         }
102     }
103     Catch(WrtDeviceApis::Commons::PlatformException) {
104         LoggerE("platform error");
105         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
106                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
107     }
108     Catch(WrtDeviceApis::Commons::ConversionException) {
109         LoggerE("conversion error");
110         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
111                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
112     }
113     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
114         LoggerE("InvalidArgument error");
115         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
116                                                 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
117     }
118     Catch(WrtDeviceApis::Commons::UnknownException) {
119         LoggerE("unknown error");
120         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
121                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
122     }
123
124 }
125
126
127
128 void MessagingListener::OnAnswerReceived(
129         const EventQueryMessagesPtr& event)
130 {
131     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
132         DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
133     Assert(NULL != callbackManager);
134     LoggerD("thread=" << DPL::Thread::GetCurrentThread());
135
136     JSContextRef context = callbackManager->getContext();
137     Assert(NULL != context);
138
139     MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
140
141     Try
142     {
143         if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
144             std::vector<IMessagePtr> msgs = event->msgs;
145
146             ConverterMessageFactory::ConverterType converter =
147                 ConverterMessageFactory::getConverter(context);
148
149             callbackManager->callOnSuccess(converter->toJSValueRef(msgs, this));
150
151         } else {
152             switch (event->getExceptionCode()) {
153             case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
154                 Throw(WrtDeviceApis::Commons::PlatformException);
155             case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
156                 Throw(WrtDeviceApis::Commons::ConversionException);
157             case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
158                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
159             default:
160                 Throw(WrtDeviceApis::Commons::UnknownException);
161             }
162         }
163     }
164     Catch(WrtDeviceApis::Commons::PlatformException) {
165         LoggerE("platform error");
166         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
167                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
168     }
169
170     Catch(WrtDeviceApis::Commons::ConversionException) {
171         LoggerE("conversion error");
172         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
173                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
174     }
175
176     Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
177     {
178         LoggerE("conversion error");
179         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
180                                                 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
181     }
182
183     Catch(WrtDeviceApis::Commons::UnknownException) {
184         LoggerE("unknown error");
185         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
186                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
187     }
188 }
189
190 void MessagingListener::OnAnswerReceived(
191         const EventDeleteMessagesPtr& event)
192 {
193     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
194         DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
195     Assert(NULL != callbackManager);
196     LoggerD("thread=" << DPL::Thread::GetCurrentThread());
197
198     JSContextRef context = callbackManager->getContext();
199     Assert(NULL != context);
200
201     MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
202
203     Try
204     {
205         if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
206             callbackManager->callOnSuccess();
207
208         } else {
209             switch (event->getExceptionCode()) {
210             case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
211                 Throw(WrtDeviceApis::Commons::PlatformException);
212             case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
213                 Throw(WrtDeviceApis::Commons::ConversionException);
214             default:
215                 Throw(WrtDeviceApis::Commons::UnknownException);
216             }
217         }
218     }
219
220     Catch(WrtDeviceApis::Commons::PlatformException) {
221         LoggerE("platform error");
222         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
223                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
224     }
225
226     Catch(WrtDeviceApis::Commons::ConversionException) {
227         LoggerE("conversion error");
228         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
229                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
230     }
231
232     Catch(WrtDeviceApis::Commons::UnknownException) {
233         LoggerE("unknown error");
234         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
235                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
236     }
237
238 }
239
240 void MessagingListener::OnAnswerReceived(
241         const EventUpdateMessagesPtr& event)
242 {
243     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
244         DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
245     Assert(NULL != callbackManager);
246     LoggerD("thread=" << DPL::Thread::GetCurrentThread());
247
248     JSContextRef context = callbackManager->getContext();
249     Assert(NULL != context);
250
251     MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
252
253     Try
254     {
255         if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
256             callbackManager->callOnSuccess();
257
258         } else {
259             switch (event->getExceptionCode()) {
260             case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
261                 Throw(WrtDeviceApis::Commons::PlatformException);
262             case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
263                 Throw(WrtDeviceApis::Commons::ConversionException);
264             default:
265                 Throw(WrtDeviceApis::Commons::UnknownException);
266             }
267         }
268     }
269
270     Catch(WrtDeviceApis::Commons::PlatformException) {
271         LoggerE("platform error");
272         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
273                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
274     }
275
276     Catch(WrtDeviceApis::Commons::ConversionException) {
277         LoggerE("conversion error");
278         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
279                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
280     }
281
282     Catch(WrtDeviceApis::Commons::UnknownException) {
283         LoggerE("unknown error");
284         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
285                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
286     }
287
288 }
289
290 void MessagingListener::OnAnswerReceived(
291         const DPL::SharedPtr<EventUpdateMessage> &event)
292 {
293     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
294         DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
295     Assert(NULL != callbackManager);
296     LoggerD("thread=" << DPL::Thread::GetCurrentThread());
297
298     JSContextRef context = callbackManager->getContext();
299     Assert(NULL != context);
300
301     Try
302     {
303         if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
304             ConverterMessageFactory::ConverterType converter =
305                 ConverterMessageFactory::getConverter(context);
306             callbackManager->callOnSuccess(converter->toJSValueRef(event->msg,
307                                                                    this));
308         } else {
309             switch (event->getExceptionCode()) {
310             case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
311                 Throw(WrtDeviceApis::Commons::PlatformException);
312             case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
313                 Throw(WrtDeviceApis::Commons::ConversionException);
314             default:
315                 Throw(WrtDeviceApis::Commons::UnknownException);
316             }
317         }
318     }
319
320     Catch(WrtDeviceApis::Commons::PlatformException) {
321         LoggerE("platform error");
322         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
323                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
324     }
325
326     Catch(WrtDeviceApis::Commons::ConversionException) {
327         LoggerE("conversion error");
328         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
329                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
330     }
331
332     Catch(WrtDeviceApis::Commons::UnknownException) {
333         LoggerE("conversion error");
334         callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
335                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
336     }
337 }
338
339 void MessagingListener::OnAnswerReceived(const EventQueryConversationsPtr& event)
340 {
341         LoggerD("enter");
342
343         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
344
345         JSContextRef context = callbackManager->getContext();
346         Assert(NULL != context);
347
348         MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
349
350         Try
351         {
352                 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) 
353                 {
354                         vector<IConversationPtr> convPtr = event->getConversatioins();
355                         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
356
357                         callbackManager->callOnSuccess(converter->toJSValueRef(convPtr));
358
359                 } 
360                 else 
361                 {
362                         switch (event->getExceptionCode()) 
363                         {
364                                 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
365                                         Throw(WrtDeviceApis::Commons::PlatformException);
366                                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
367                                         Throw(WrtDeviceApis::Commons::ConversionException);
368                                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
369                                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
370                                 default:
371                                         Throw(WrtDeviceApis::Commons::UnknownException);
372                         }
373                 }
374         }
375
376         Catch(WrtDeviceApis::Commons::PlatformException) 
377         {
378                 LoggerE("platform error");
379                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
380                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
381         }
382         Catch(WrtDeviceApis::Commons::ConversionException) 
383         {
384                 LoggerE("conversion error");
385                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
386                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
387         }
388         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
389         {
390                 LoggerE("conversion error");
391                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
392                                                 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
393         }       
394         Catch(WrtDeviceApis::Commons::UnknownException) 
395         {
396                 LoggerE("unknown error");
397                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
398                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
399         }
400
401 }
402
403 void MessagingListener::OnAnswerReceived(const EventDeleteConversationsPtr& event)
404 {
405         LoggerD("enter");
406         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
407
408         JSContextRef context = callbackManager->getContext();
409         Assert(NULL != context);
410
411         MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
412         
413         try 
414         {
415                 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) 
416                 {
417                         callbackManager->callOnSuccess();
418         
419 }
420                 else 
421                 {
422                         switch (event->getExceptionCode()) 
423                         {
424                                 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
425                                         Throw(WrtDeviceApis::Commons::PlatformException);
426                                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
427                                         Throw(WrtDeviceApis::Commons::ConversionException);
428                                 default:
429                                         Throw(WrtDeviceApis::Commons::UnknownException);
430                         }
431                 }
432         }
433         Catch(WrtDeviceApis::Commons::PlatformException) 
434         {
435                 LoggerE("platform error");
436                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
437                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
438         }
439         Catch(WrtDeviceApis::Commons::ConversionException) 
440         {
441                 LoggerE("conversion error");
442                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
443                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
444         }
445         Catch(WrtDeviceApis::Commons::UnknownException) 
446         {
447                 LoggerE("unknown error");
448                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
449                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
450         }
451 }
452
453 void MessagingListener::OnAnswerReceived(const EventQueryFoldersPtr& event)
454 {
455         LoggerD("enter");
456
457         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
458
459         JSContextRef context = callbackManager->getContext();
460         Assert(NULL != context);
461
462         MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
463
464         Try
465         {
466                 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) 
467                 {
468                         vector<IMessageFolderPtr> folderPtr = event->getFolders();
469                         int size = folderPtr.size();
470                         LoggerD(size);
471                         
472                         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
473                         callbackManager->callOnSuccess(converter->toJSValueRef(folderPtr));
474                 } 
475                 else 
476                 {
477                         switch (event->getExceptionCode()) 
478                         {
479                                 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
480                                         Throw(WrtDeviceApis::Commons::PlatformException);
481                                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
482                                         Throw(WrtDeviceApis::Commons::ConversionException);
483                                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
484                                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
485                                 default:
486                                         Throw(WrtDeviceApis::Commons::UnknownException);
487                         }
488                 }
489         }
490
491         Catch(WrtDeviceApis::Commons::PlatformException) 
492         {
493                 LoggerE("platform error");
494                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
495                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
496         }
497         Catch(WrtDeviceApis::Commons::ConversionException) 
498         {
499                 LoggerE("conversion error");
500                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
501                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
502         }
503         Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
504         {
505                 LoggerE("conversion error");
506                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
507                                                 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
508         }       
509         Catch(WrtDeviceApis::Commons::UnknownException) 
510         {
511                 LoggerE("unknown error");
512                 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,             
513                                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
514         }
515
516 }
517
518
519 void MessagingListener::onAnswerReceived(
520         const EventMessageReceivedPtr& event)
521 {
522         LoggerD("<<<");
523
524         if (WrtDeviceApis::Commons::ExceptionCodes::None != event->getExceptionCode()) 
525         {
526                 LoggerE("exception occured: " << event->getExceptionCode());
527                 return;
528         }
529
530         JSValueRef jsMsg = NULL;
531
532         EventOnMessagingStorageChangesPrivateDataPtr multiCallbacks = 
533                 DPL::DynamicPointerCast<EventOnMessagingStorageChangesPrivateData>(event->getPrivateData());
534         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr defaultCbm = multiCallbacks->getMessagesAdded();
535
536     JSContextRef context = defaultCbm->getContext();
537         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
538
539
540         Try
541         {
542                 switch(multiCallbacks->getFunctionIndex())
543                 {
544                         case MESSAGES_MULTI_FUNCTION:
545                         {
546
547                                 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
548                                 IMessagePtr msg = event->getMessage();
549
550                                 if(filter != NULL)
551                                 {
552                                         LoggerD("Filter exist");                        
553                                         StorageChangesMessageGeneratorPtr queryGenerator(new StorageChangesMessageGenerator(event->getMessage(), event->getConversation()));
554                                 
555                                         DeviceAPI::Tizen::IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<DeviceAPI::Tizen::IFilterVisitor>(queryGenerator);
556                                         filter->travel(filterVisitor, 0);
557                                         std::vector<IMessagePtr> msgs; // for msg
558                                         if(queryGenerator->getCompareResult())
559                                         {
560                                                 LoggerD("Message call back");
561                                                 msgs.push_back(msg);
562                                                 jsMsg = converter->toJSValueRef(msgs, this);
563                                         }
564                                         else
565                                         {
566                                                 LoggerD("skip");
567                                                 return;                                 
568                                         }
569                                 }
570                                 else 
571                                 {
572                                         LoggerD("Filter not exist or empty message");                                           
573                                         std::vector<IMessagePtr> msgs; // for msg
574                                         IMessagePtr msg = event->getMessage();
575                                         msgs.push_back(msg);
576                                         jsMsg = converter->toJSValueRef(msgs, this);                    
577                                 }
578                         }
579                         break;
580                         case CONVERSATIONS_MULTI_FUNCTION:
581                         {
582                                 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
583                                 IMessagePtr msg = event->getMessage();
584
585                                 if(filter != NULL && msg->getMessageType() != EMPTY_MESSAGE)
586                                 {
587                                         LoggerD("Filter exist");                        
588                                         StorageChangesConversationGeneratorPtr queryGenerator(new StorageChangesConversationGenerator(event->getConversation()));
589                                 
590                                         DeviceAPI::Tizen::IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<DeviceAPI::Tizen::IFilterVisitor>(queryGenerator);
591                                         filter->travel(filterVisitor, 0);
592                                         std::vector<IConversationPtr> convPtrs; // for conversation
593                                         IConversationPtr conversation = event->getConversation();
594                                         if(queryGenerator->getCompareResult())
595                                         {
596                                                 LoggerD("Message call back");
597                                                 convPtrs.push_back(conversation);
598                                                 jsMsg = converter->toJSValueRef(convPtrs);
599                                         }
600                                         else
601                                         {
602                                                 LoggerD("skip");
603                                                 return;                                 
604                                         }
605                                 }
606                                 else 
607                                 {
608                                         LoggerD("Filter not exist or empty conversation");                                              
609                                         std::vector<IConversationPtr> convPtrs; // for conversation
610                                         IConversationPtr conversation = event->getConversation();
611
612                                         LoggerD("Conversation call back");
613
614                                         convPtrs.push_back(conversation);
615                                         jsMsg = converter->toJSValueRef(convPtrs);
616                                 }
617                         }
618                         break;
619                         case FOLDERS_MULTI_FUNCTION:
620                         {
621                                 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
622                                 if(filter != NULL)
623                                 {
624                                         LoggerD("Filter exist");                        
625                                         StorageChangesFolderGeneratorPtr queryGenerator(new StorageChangesFolderGenerator(event->getMessageFolder()));
626                                 
627                                         DeviceAPI::Tizen::IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<DeviceAPI::Tizen::IFilterVisitor>(queryGenerator);
628                                         filter->travel(filterVisitor, 0);
629                                         std::vector<IMessageFolderPtr> folderPtrs; // for messagefoldeer
630                                         IMessageFolderPtr messagefolder = event->getMessageFolder();
631                                         if(queryGenerator->getCompareResult())
632                                         {
633                                                 LoggerD("Message call back");
634                                                 folderPtrs.push_back(messagefolder);
635                                                 jsMsg = converter->toJSValueRef(folderPtrs);
636                                         }
637                                         else
638                                         {
639                                                 LoggerD("skip");
640                                                 return;                                 
641                                         }
642                                 }
643                                 else 
644                                 {
645                                         LoggerD("Filter not exist");                                            
646                                         std::vector<IMessageFolderPtr> folderPtrs; // for messagefoldeer
647                                         IMessageFolderPtr messagefolder = event->getMessageFolder();
648                         
649                                         LoggerD("MessageFolder call back");
650                         
651                                         folderPtrs.push_back(messagefolder);
652                                         jsMsg = converter->toJSValueRef(folderPtrs);
653                                 }
654                         }
655                         break;
656                 }
657
658                 switch(event->getMsg_Event_Type())
659                 {
660                         case EventMessageReceived::MSG_ADDED:
661                         {
662                                 if(multiCallbacks->getMessagesAdded())
663                                 {
664                                         multiCallbacks->getMessagesAdded()->callOnSuccess(jsMsg);
665                                 }
666                         }
667                         break;
668                         case EventMessageReceived::MSG_UPDATED:
669                         {
670                                 if(multiCallbacks->getMessagesUpdated())
671                                 {
672                                         defaultCbm = multiCallbacks->getMessagesUpdated();
673                                         defaultCbm->callOnSuccess(jsMsg);
674                                 }
675                         }
676                         break;
677                         case EventMessageReceived::MSG_DELETED:
678                         {
679                                 if(multiCallbacks->getMessagesRemoved())
680                                 {
681                                         defaultCbm = multiCallbacks->getMessagesRemoved();
682                                         defaultCbm->callOnSuccess(jsMsg);
683                                 }
684                         }
685                         break;
686                         default:
687                         {
688                                 break;
689                         }
690                                 
691                 }
692         }
693         catch(...) {
694         LoggerW("Exception: occured");
695                 return;
696     }
697 }
698
699 long MessagingListener::addIncomingMsgCallback(const WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr& cbManager, int funtionIndex)
700 {
701         IncomingMsgCB* callbacks = NULL;
702         long handle = 0;
703         
704         switch(funtionIndex)
705         {
706                 case MESSAGES_MULTI_FUNCTION:
707                 {
708                         callbacks = &m_MessageCallbacks;                        
709                         handle = static_cast<long>(m_emiterMsgId.back());
710                         LoggerW("Message Emitter handle : " << handle);                 
711                 }
712                 break;
713                 case CONVERSATIONS_MULTI_FUNCTION:
714                 {
715                         callbacks = &m_ConversationCallbacks;                                           
716                         handle = static_cast<long>(m_emiterConvId.back());
717                         LoggerW("Conversation Emitter handle : " << handle);                    
718                 }
719                 break;
720                 case FOLDERS_MULTI_FUNCTION:
721                 {
722                         callbacks = &m_FolderCallbacks;                                         
723                         handle = static_cast<long>(m_emiterFolderId.back());
724                         LoggerW("Folder Emitter handle : " << handle);                  
725                 }
726                 break;
727                 default:
728                 {
729                         LoggerW("Unexpected state");
730                 }
731                 break;
732         }
733
734         if (NULL == callbacks) {
735             Throw(WrtDeviceApis::Commons::UnsupportedException);
736         }
737
738     // try to insert new callback
739     if (!callbacks->insert(std::make_pair(handle, cbManager)).second) {
740         // callback with this handle already exists!
741         Throw(WrtDeviceApis::Commons::AlreadyInUseException);
742     }
743
744     return handle;
745 }
746
747 void MessagingListener::removeIncomingMsgCallback(long handle, int type, long index)
748 {
749     size_t msgRemoved = 0;
750     size_t convRemoved = 0;
751     size_t folderRemoved = 0;   
752
753     if(!checkMessagingEmitter(handle, type, index))
754     {
755         LoggerW("Try to deregister by unmatched service or unknown id");
756         Throw(WrtDeviceApis::Commons::NotFoundException);
757     }
758                 
759     msgRemoved += m_MessageCallbacks.erase(handle);
760     convRemoved += m_ConversationCallbacks.erase(handle);       
761     folderRemoved += m_FolderCallbacks.erase(handle);
762
763     // there are duplicate handles
764     if (msgRemoved+convRemoved+folderRemoved > 1) {
765         LoggerE("Duplicate callback handles!");
766     }
767     EmitterMessageReceived::IdType temp_handle;
768     temp_handle = static_cast<EmitterMessageReceived::IdType>(handle);
769
770     if (msgRemoved) {
771         LoggerW("EmitterMessageReceived registered");
772         IMessaging::getInstance().removeOnMessageMsgReceived(temp_handle);
773         removeMessagingEmitter(temp_handle);    
774         for(unsigned int i=0; i<m_emiterMsgId.size(); i++)
775         {
776             if(temp_handle == static_cast<long>(m_emiterMsgId[i]))
777             {
778                 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterMsgId[i]) );
779                 m_emiterMsgId.erase(m_emiterMsgId.begin()+i);
780             }
781         }
782     }
783     else if (convRemoved) {
784         LoggerW("EmitterConversationReceived registered");
785         IMessaging::getInstance().removeOnMessageConvReceived(temp_handle);
786         removeMessagingEmitter(temp_handle);
787         for(unsigned int i=0; i<m_emiterConvId.size(); i++)
788         {
789             if(temp_handle == static_cast<long>(m_emiterConvId[i]))
790             {
791                 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterConvId[i]) );
792                 m_emiterConvId.erase(m_emiterConvId.begin()+i);
793             }
794         }
795     }
796     else if (folderRemoved) {
797         LoggerW("EmitterFolderReceived registered");
798         IMessaging::getInstance().removeOnMessageFolderReceived(temp_handle);
799         removeMessagingEmitter(temp_handle);
800         for(unsigned int i=0; i<m_emiterFolderId.size(); i++)
801         {
802             if(temp_handle == static_cast<long>(m_emiterFolderId[i]))
803             {
804                 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterFolderId[i]) );
805                 m_emiterFolderId.erase(m_emiterFolderId.begin()+i);
806             }
807         }
808     }
809         
810 }
811
812 void MessagingListener::registerMessageReceivedEmitter(EventOnMessagingStorageChangesPrivateDataPtr privData)
813 {
814
815     Try
816     {
817         if(privData->getFunctionIndex() == MESSAGES_MULTI_FUNCTION)
818         {
819
820             EmitterMessageReceivedPtr emitter(new EmitterMessageReceived());
821             emitter->setListener(this);
822             emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));// <-registerMessageReceivedEmitter()
823             m_emiterMsgId.push_back(emitter->getId());
824             IMessaging::getInstance().addOnMessageReceived(emitter, privData->getFilter(), privData->getFunctionIndex());               
825             addMessagingEmitter(static_cast<long>(emitter->getId()), privData->getType(), privData->getIndex());                
826         }
827         else if(privData->getFunctionIndex() == CONVERSATIONS_MULTI_FUNCTION)
828         {
829             EmitterConversationReceivedPtr emitter(new EmitterConversationReceived());
830             emitter->setListener(this);
831             emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));// <-registerMessageReceivedEmitter()
832             m_emiterConvId.push_back(emitter->getId());
833             IMessaging::getInstance().addOnMessageReceived(emitter, privData->getFilter(), privData->getFunctionIndex());               
834             addMessagingEmitter(static_cast<long>(emitter->getId()), privData->getType(), privData->getIndex());                
835         }
836         else// if(privData->getFunctionIndex() == FOLDERS_MULTI_FUNCTION)
837         {
838             EmitterFolderReceivedPtr emitter(new EmitterFolderReceived());
839             emitter->setListener(this);
840             emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));// <-registerMessageReceivedEmitter()
841             m_emiterFolderId.push_back(emitter->getId());
842             IMessaging::getInstance().addOnMessageReceived(emitter, privData->getFilter(), privData->getFunctionIndex());               
843             addMessagingEmitter(static_cast<long>(emitter->getId()), privData->getType(), privData->getIndex());
844         }
845     }
846     Catch(WrtDeviceApis::Commons::PlatformException){
847         LoggerD("registerMessageReceivedEmitter failed");                               
848         Throw(WrtDeviceApis::Commons::PlatformException);
849     }
850         
851 }
852
853 void MessagingListener::deregisterMessageReceivedEmitter()
854 {
855     LoggerD("enter");
856     if (m_emiterMsgId.size() > 0) {
857         LoggerW("EmitterMessageReceived registered");
858                 EmitterMessageReceived::IdType  tempId;
859                 while(m_emiterMsgId.size() > 0)
860                 {
861                         tempId = m_emiterMsgId.back();
862                         IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
863                         m_emiterMsgId.pop_back();
864                 }
865     }
866
867         if (m_emiterConvId.size() > 0) {
868                 LoggerW("EmitterConversationReceived registered");
869                 EmitterConversationReceived::IdType     tempId;
870                 while(m_emiterConvId.size() > 0)
871                 {
872                         tempId = m_emiterConvId.back();
873                         IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
874                         m_emiterConvId.pop_back();
875                 }
876         }
877
878         if (m_emiterFolderId.size() > 0) {
879                 LoggerW("EmitterFolderReceived registered");
880                 EmitterFolderReceived::IdType   tempId;
881                 while(m_emiterFolderId.size() > 0)
882                 {
883                         tempId = m_emiterFolderId.back();
884                         IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
885                         m_emiterFolderId.pop_back();
886                 }
887         }
888
889         MessagingEmitterRegistersIterator it = m_requests.begin();
890
891         for (; it != m_requests.end(); it++)
892         {
893                 removeMessagingEmitter(it->first);
894         }
895         LoggerD("finish");
896         return;
897 }
898
899 bool MessagingListener::checkMessagingEmitter(long handle, int type, long index)
900 {
901         int ret = FALSE;
902     MessagingEmitterRegistersIterator it = m_requests.find(handle);
903
904     LoggerD("handle, type, index"<< handle << " " << type << " " << index);
905     LoggerD("it->second.type, it->second.accountId"<< it->second.type << " " << it->second.accountId);
906         
907         if((it->second.type == type) && (it->second.accountId == index))
908         {
909                 ret = TRUE;
910         }
911         return ret;     
912 }
913
914 void MessagingListener::addMessagingEmitter(long handle, int type, long index)
915 {
916     LoggerD("handle, type, index"<< handle << " " << type << " " << index);
917         m_requests.insert(std::make_pair(handle, MessagingEmitterData(type, index)));
918 }
919
920 void MessagingListener::removeMessagingEmitter(long handle)
921 {
922     LoggerD("handle"<< handle );
923         m_requests.erase(handle);
924 }
925
926
927 MessagingListener& MessagingListener::getInstance()
928 {
929         static MessagingListener instance;
930         return instance;
931 }
932
933 }
934 }