2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <dpl/assert.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include "StorageChangesMessageGenerator.h"
24 #include "StorageChangesConversationGenerator.h"
25 #include "StorageChangesFolderGenerator.h"
27 #include <JSWebAPIErrorFactory.h>
28 #include <SecurityExceptions.h>
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>
38 IMPLEMENT_SAFE_SINGLETON(DeviceAPI::Messaging::MessagingListener)
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace DeviceAPI::Messaging;
49 using namespace DeviceAPI::Common;
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),
63 // m_emiterMsgId(EmitterMessageReceived::emptyId),
64 // m_emiterConvId(EmitterConversationReceived::emptyId),
65 // m_emiterFolderId(EmitterFolderReceived::emptyId)
69 MessagingListener::~MessagingListener() {
72 void MessagingListener::OnAnswerReceived(
73 const EventAddDraftMessagePtr& event)
75 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
76 DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
77 Assert(NULL != callbackManager);
78 LoggerD("thread=" << DPL::Thread::GetCurrentThread());
80 JSContextRef context = callbackManager->getContext();
81 Assert(NULL != context);
83 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
87 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
88 callbackManager->callOnSuccess();
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);
99 Throw(WrtDeviceApis::Commons::UnknownException);
103 Catch(WrtDeviceApis::Commons::PlatformException) {
104 LoggerE("platform error");
105 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
106 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
108 Catch(WrtDeviceApis::Commons::ConversionException) {
109 LoggerE("conversion error");
110 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
111 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
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));
118 Catch(WrtDeviceApis::Commons::UnknownException) {
119 LoggerE("unknown error");
120 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
121 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
128 void MessagingListener::OnAnswerReceived(
129 const EventQueryMessagesPtr& event)
131 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
132 DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
133 Assert(NULL != callbackManager);
134 LoggerD("thread=" << DPL::Thread::GetCurrentThread());
136 JSContextRef context = callbackManager->getContext();
137 Assert(NULL != context);
139 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
143 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
144 std::vector<IMessagePtr> msgs = event->msgs;
146 ConverterMessageFactory::ConverterType converter =
147 ConverterMessageFactory::getConverter(context);
149 callbackManager->callOnSuccess(converter->toJSValueRef(msgs, this));
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);
160 Throw(WrtDeviceApis::Commons::UnknownException);
164 Catch(WrtDeviceApis::Commons::PlatformException) {
165 LoggerE("platform error");
166 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
167 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
170 Catch(WrtDeviceApis::Commons::ConversionException) {
171 LoggerE("conversion error");
172 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
173 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
176 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
178 LoggerE("conversion error");
179 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
180 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
183 Catch(WrtDeviceApis::Commons::UnknownException) {
184 LoggerE("unknown error");
185 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
186 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
190 void MessagingListener::OnAnswerReceived(
191 const EventDeleteMessagesPtr& event)
193 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
194 DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
195 Assert(NULL != callbackManager);
196 LoggerD("thread=" << DPL::Thread::GetCurrentThread());
198 JSContextRef context = callbackManager->getContext();
199 Assert(NULL != context);
201 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
205 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
206 callbackManager->callOnSuccess();
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);
215 Throw(WrtDeviceApis::Commons::UnknownException);
220 Catch(WrtDeviceApis::Commons::PlatformException) {
221 LoggerE("platform error");
222 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
223 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
226 Catch(WrtDeviceApis::Commons::ConversionException) {
227 LoggerE("conversion error");
228 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
229 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
232 Catch(WrtDeviceApis::Commons::UnknownException) {
233 LoggerE("unknown error");
234 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
235 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
240 void MessagingListener::OnAnswerReceived(
241 const EventUpdateMessagesPtr& event)
243 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
244 DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
245 Assert(NULL != callbackManager);
246 LoggerD("thread=" << DPL::Thread::GetCurrentThread());
248 JSContextRef context = callbackManager->getContext();
249 Assert(NULL != context);
251 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
255 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
256 callbackManager->callOnSuccess();
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);
265 Throw(WrtDeviceApis::Commons::UnknownException);
270 Catch(WrtDeviceApis::Commons::PlatformException) {
271 LoggerE("platform error");
272 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
273 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
276 Catch(WrtDeviceApis::Commons::ConversionException) {
277 LoggerE("conversion error");
278 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
279 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
282 Catch(WrtDeviceApis::Commons::UnknownException) {
283 LoggerE("unknown error");
284 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
285 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
290 void MessagingListener::OnAnswerReceived(
291 const DPL::SharedPtr<EventUpdateMessage> &event)
293 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager =
294 DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
295 Assert(NULL != callbackManager);
296 LoggerD("thread=" << DPL::Thread::GetCurrentThread());
298 JSContextRef context = callbackManager->getContext();
299 Assert(NULL != context);
303 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode()) {
304 ConverterMessageFactory::ConverterType converter =
305 ConverterMessageFactory::getConverter(context);
306 callbackManager->callOnSuccess(converter->toJSValueRef(event->msg,
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);
315 Throw(WrtDeviceApis::Commons::UnknownException);
320 Catch(WrtDeviceApis::Commons::PlatformException) {
321 LoggerE("platform error");
322 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
323 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
326 Catch(WrtDeviceApis::Commons::ConversionException) {
327 LoggerE("conversion error");
328 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
329 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
332 Catch(WrtDeviceApis::Commons::UnknownException) {
333 LoggerE("conversion error");
334 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
335 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
339 void MessagingListener::OnAnswerReceived(const EventQueryConversationsPtr& event)
343 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
345 JSContextRef context = callbackManager->getContext();
346 Assert(NULL != context);
348 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
352 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode())
354 vector<IConversationPtr> convPtr = event->getConversatioins();
355 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
357 callbackManager->callOnSuccess(converter->toJSValueRef(convPtr));
362 switch (event->getExceptionCode())
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);
371 Throw(WrtDeviceApis::Commons::UnknownException);
376 Catch(WrtDeviceApis::Commons::PlatformException)
378 LoggerE("platform error");
379 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
380 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
382 Catch(WrtDeviceApis::Commons::ConversionException)
384 LoggerE("conversion error");
385 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
386 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
388 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
390 LoggerE("conversion error");
391 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
392 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
394 Catch(WrtDeviceApis::Commons::UnknownException)
396 LoggerE("unknown error");
397 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
398 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
403 void MessagingListener::OnAnswerReceived(const EventDeleteConversationsPtr& event)
406 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
408 JSContextRef context = callbackManager->getContext();
409 Assert(NULL != context);
411 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
415 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode())
417 callbackManager->callOnSuccess();
422 switch (event->getExceptionCode())
424 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
425 Throw(WrtDeviceApis::Commons::PlatformException);
426 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
427 Throw(WrtDeviceApis::Commons::ConversionException);
429 Throw(WrtDeviceApis::Commons::UnknownException);
433 Catch(WrtDeviceApis::Commons::PlatformException)
435 LoggerE("platform error");
436 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
437 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
439 Catch(WrtDeviceApis::Commons::ConversionException)
441 LoggerE("conversion error");
442 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
443 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
445 Catch(WrtDeviceApis::Commons::UnknownException)
447 LoggerE("unknown error");
448 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
449 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
453 void MessagingListener::OnAnswerReceived(const EventQueryFoldersPtr& event)
457 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
459 JSContextRef context = callbackManager->getContext();
460 Assert(NULL != context);
462 MessageAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
466 if (WrtDeviceApis::Commons::ExceptionCodes::None == event->getExceptionCode())
468 vector<IMessageFolderPtr> folderPtr = event->getFolders();
469 int size = folderPtr.size();
472 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
473 callbackManager->callOnSuccess(converter->toJSValueRef(folderPtr));
477 switch (event->getExceptionCode())
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);
486 Throw(WrtDeviceApis::Commons::UnknownException);
491 Catch(WrtDeviceApis::Commons::PlatformException)
493 LoggerE("platform error");
494 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
495 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
497 Catch(WrtDeviceApis::Commons::ConversionException)
499 LoggerE("conversion error");
500 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
501 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
503 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
505 LoggerE("conversion error");
506 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
507 JSWebAPIErrorFactory::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
509 Catch(WrtDeviceApis::Commons::UnknownException)
511 LoggerE("unknown error");
512 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context,
513 JSWebAPIErrorFactory::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
519 void MessagingListener::onAnswerReceived(
520 const EventMessageReceivedPtr& event)
524 if (WrtDeviceApis::Commons::ExceptionCodes::None != event->getExceptionCode())
526 LoggerE("exception occured: " << event->getExceptionCode());
530 JSValueRef jsMsg = NULL;
532 EventOnMessagingStorageChangesPrivateDataPtr multiCallbacks =
533 DPL::DynamicPointerCast<EventOnMessagingStorageChangesPrivateData>(event->getPrivateData());
534 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr defaultCbm = multiCallbacks->getMessagesAdded();
536 JSContextRef context = defaultCbm->getContext();
537 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
542 switch(multiCallbacks->getFunctionIndex())
544 case MESSAGES_MULTI_FUNCTION:
547 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
548 IMessagePtr msg = event->getMessage();
552 LoggerD("Filter exist");
553 StorageChangesMessageGeneratorPtr queryGenerator(new StorageChangesMessageGenerator(event->getMessage(), event->getConversation()));
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())
560 LoggerD("Message call back");
562 jsMsg = converter->toJSValueRef(msgs, this);
572 LoggerD("Filter not exist or empty message");
573 std::vector<IMessagePtr> msgs; // for msg
574 IMessagePtr msg = event->getMessage();
576 jsMsg = converter->toJSValueRef(msgs, this);
580 case CONVERSATIONS_MULTI_FUNCTION:
582 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
583 IMessagePtr msg = event->getMessage();
585 if(filter != NULL && msg->getMessageType() != EMPTY_MESSAGE)
587 LoggerD("Filter exist");
588 StorageChangesConversationGeneratorPtr queryGenerator(new StorageChangesConversationGenerator(event->getConversation()));
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())
596 LoggerD("Message call back");
597 convPtrs.push_back(conversation);
598 jsMsg = converter->toJSValueRef(convPtrs);
608 LoggerD("Filter not exist or empty conversation");
609 std::vector<IConversationPtr> convPtrs; // for conversation
610 IConversationPtr conversation = event->getConversation();
612 LoggerD("Conversation call back");
614 convPtrs.push_back(conversation);
615 jsMsg = converter->toJSValueRef(convPtrs);
619 case FOLDERS_MULTI_FUNCTION:
621 DeviceAPI::Tizen::FilterPtr filter = multiCallbacks->getFilter();
624 LoggerD("Filter exist");
625 StorageChangesFolderGeneratorPtr queryGenerator(new StorageChangesFolderGenerator(event->getMessageFolder()));
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())
633 LoggerD("Message call back");
634 folderPtrs.push_back(messagefolder);
635 jsMsg = converter->toJSValueRef(folderPtrs);
645 LoggerD("Filter not exist");
646 std::vector<IMessageFolderPtr> folderPtrs; // for messagefoldeer
647 IMessageFolderPtr messagefolder = event->getMessageFolder();
649 LoggerD("MessageFolder call back");
651 folderPtrs.push_back(messagefolder);
652 jsMsg = converter->toJSValueRef(folderPtrs);
658 switch(event->getMsg_Event_Type())
660 case EventMessageReceived::MSG_ADDED:
662 if(multiCallbacks->getMessagesAdded())
664 multiCallbacks->getMessagesAdded()->callOnSuccess(jsMsg);
668 case EventMessageReceived::MSG_UPDATED:
670 if(multiCallbacks->getMessagesUpdated())
672 defaultCbm = multiCallbacks->getMessagesUpdated();
673 defaultCbm->callOnSuccess(jsMsg);
677 case EventMessageReceived::MSG_DELETED:
679 if(multiCallbacks->getMessagesRemoved())
681 defaultCbm = multiCallbacks->getMessagesRemoved();
682 defaultCbm->callOnSuccess(jsMsg);
694 LoggerW("Exception: occured");
699 long MessagingListener::addIncomingMsgCallback(const WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr& cbManager, int funtionIndex)
701 IncomingMsgCB* callbacks = NULL;
706 case MESSAGES_MULTI_FUNCTION:
708 callbacks = &m_MessageCallbacks;
709 handle = static_cast<long>(m_emiterMsgId.back());
710 LoggerW("Message Emitter handle : " << handle);
713 case CONVERSATIONS_MULTI_FUNCTION:
715 callbacks = &m_ConversationCallbacks;
716 handle = static_cast<long>(m_emiterConvId.back());
717 LoggerW("Conversation Emitter handle : " << handle);
720 case FOLDERS_MULTI_FUNCTION:
722 callbacks = &m_FolderCallbacks;
723 handle = static_cast<long>(m_emiterFolderId.back());
724 LoggerW("Folder Emitter handle : " << handle);
729 LoggerW("Unexpected state");
734 if (NULL == callbacks) {
735 Throw(WrtDeviceApis::Commons::UnsupportedException);
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);
747 void MessagingListener::removeIncomingMsgCallback(long handle, int type, long index)
749 size_t msgRemoved = 0;
750 size_t convRemoved = 0;
751 size_t folderRemoved = 0;
753 if(!checkMessagingEmitter(handle, type, index))
755 LoggerW("Try to deregister by unmatched service or unknown id");
756 Throw(WrtDeviceApis::Commons::NotFoundException);
759 msgRemoved += m_MessageCallbacks.erase(handle);
760 convRemoved += m_ConversationCallbacks.erase(handle);
761 folderRemoved += m_FolderCallbacks.erase(handle);
763 // there are duplicate handles
764 if (msgRemoved+convRemoved+folderRemoved > 1) {
765 LoggerE("Duplicate callback handles!");
767 EmitterMessageReceived::IdType temp_handle;
768 temp_handle = static_cast<EmitterMessageReceived::IdType>(handle);
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++)
776 if(temp_handle == static_cast<long>(m_emiterMsgId[i]))
778 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterMsgId[i]) );
779 m_emiterMsgId.erase(m_emiterMsgId.begin()+i);
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++)
789 if(temp_handle == static_cast<long>(m_emiterConvId[i]))
791 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterConvId[i]) );
792 m_emiterConvId.erase(m_emiterConvId.begin()+i);
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++)
802 if(temp_handle == static_cast<long>(m_emiterFolderId[i]))
804 LoggerW("handle : handle " << temp_handle<< " " << static_cast<long>(m_emiterFolderId[i]) );
805 m_emiterFolderId.erase(m_emiterFolderId.begin()+i);
812 void MessagingListener::registerMessageReceivedEmitter(EventOnMessagingStorageChangesPrivateDataPtr privData)
817 if(privData->getFunctionIndex() == MESSAGES_MULTI_FUNCTION)
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());
827 else if(privData->getFunctionIndex() == CONVERSATIONS_MULTI_FUNCTION)
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());
836 else// if(privData->getFunctionIndex() == FOLDERS_MULTI_FUNCTION)
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());
846 Catch(WrtDeviceApis::Commons::PlatformException){
847 LoggerD("registerMessageReceivedEmitter failed");
848 Throw(WrtDeviceApis::Commons::PlatformException);
853 void MessagingListener::deregisterMessageReceivedEmitter()
856 if (m_emiterMsgId.size() > 0) {
857 LoggerW("EmitterMessageReceived registered");
858 EmitterMessageReceived::IdType tempId;
859 while(m_emiterMsgId.size() > 0)
861 tempId = m_emiterMsgId.back();
862 IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
863 m_emiterMsgId.pop_back();
867 if (m_emiterConvId.size() > 0) {
868 LoggerW("EmitterConversationReceived registered");
869 EmitterConversationReceived::IdType tempId;
870 while(m_emiterConvId.size() > 0)
872 tempId = m_emiterConvId.back();
873 IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
874 m_emiterConvId.pop_back();
878 if (m_emiterFolderId.size() > 0) {
879 LoggerW("EmitterFolderReceived registered");
880 EmitterFolderReceived::IdType tempId;
881 while(m_emiterFolderId.size() > 0)
883 tempId = m_emiterFolderId.back();
884 IMessaging::getInstance().removeOnMessageMsgReceived(tempId);
885 m_emiterFolderId.pop_back();
889 MessagingEmitterRegistersIterator it = m_requests.begin();
891 for (; it != m_requests.end(); it++)
893 removeMessagingEmitter(it->first);
899 bool MessagingListener::checkMessagingEmitter(long handle, int type, long index)
902 MessagingEmitterRegistersIterator it = m_requests.find(handle);
904 LoggerD("handle, type, index"<< handle << " " << type << " " << index);
905 LoggerD("it->second.type, it->second.accountId"<< it->second.type << " " << it->second.accountId);
907 if((it->second.type == type) && (it->second.accountId == index))
914 void MessagingListener::addMessagingEmitter(long handle, int type, long index)
916 LoggerD("handle, type, index"<< handle << " " << type << " " << index);
917 m_requests.insert(std::make_pair(handle, MessagingEmitterData(type, index)));
920 void MessagingListener::removeMessagingEmitter(long handle)
922 LoggerD("handle"<< handle );
923 m_requests.erase(handle);
927 MessagingListener& MessagingListener::getInstance()
929 static MessagingListener instance;