2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file ReqReceiverMessage.cpp
19 * @author Pawel Misiak (p.misiak@samsung.com)
24 #include <Commons/Exception.h>
25 #include "ReqReceiverMessage.h"
26 #include "IMessaging.h"
29 #include "MessageFilterValidatorFactory.h"
30 #include "ConversationFilterValidatorFactory.h"
31 #include "FolderFilterValidatorFactory.h"
35 using namespace TizenApis::Api::Tizen;
36 using namespace TizenApis::Platform::Messaging;
37 using namespace WrtDeviceApis::Commons;
43 ReqReceiverMessage::ReqReceiverMessage() :
44 EventAddDraftMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
45 EventSendMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
46 EventUpdateMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
47 EventDeleteMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
48 EventDeleteMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
49 EventUpdateMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
50 EventQueryMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
51 EventGetConversationIdReqReceiver(ThreadEnum::MESSAGING_THREAD),
52 EventQueryConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
53 EventDeleteConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
54 EventQueryFoldersReqReceiver(ThreadEnum::MESSAGING_THREAD),
55 EventMessagingServiceReqReceiver(ThreadEnum::MESSAGING_THREAD)
57 LogDebug("create receiver for messaging");
60 ReqReceiverMessage::~ReqReceiverMessage()
64 void ReqReceiverMessage::addDraftMessage(const EventAddDraftMessagePtr& event)
67 EventAddDraftMessageReqReceiver::PostRequest(event);
70 void ReqReceiverMessage::sendMessage(const EventSendMessagePtr& event)
73 EventSendMessageReqReceiver::PostRequest(event);
76 void ReqReceiverMessage::updateMessage(const EventUpdateMessagePtr& event)
79 EventUpdateMessageReqReceiver::PostRequest(event);
82 void ReqReceiverMessage::deleteMessage(const EventDeleteMessagePtr& event)
85 EventDeleteMessageReqReceiver::PostRequest(event);
88 void ReqReceiverMessage::deleteMessages(const EventDeleteMessagesPtr& event)
91 EventDeleteMessagesReqReceiver::PostRequest(event);
94 void ReqReceiverMessage::updateMessages(const EventUpdateMessagesPtr& event)
97 EventUpdateMessagesReqReceiver::PostRequest(event);
100 void ReqReceiverMessage::queryMessages(const EventQueryMessagesPtr& event)
103 EventQueryMessagesReqReceiver::PostRequest(event);
107 void ReqReceiverMessage::getConversationId(const EventGetConversationIdPtr& event)
110 EventGetConversationIdReqReceiver::PostRequest(event);
113 void ReqReceiverMessage::queryConversations(const EventQueryConversationsPtr& event)
116 EventQueryConversationsReqReceiver::PostRequest(event);
119 void ReqReceiverMessage::deleteConversations(const EventDeleteConversationsPtr& event)
122 EventDeleteConversationsReqReceiver::PostRequest(event);
125 void ReqReceiverMessage::queryFolders(const EventQueryFoldersPtr& event)
128 EventQueryFoldersReqReceiver::PostRequest(event);
131 void ReqReceiverMessage::loadMessageBody(const EventMessagingServicePtr& event)
134 EventMessagingServiceReqReceiver::PostRequest(event);
137 void ReqReceiverMessage::loadMessageAttachment(const EventMessagingServicePtr& event)
140 EventMessagingServiceReqReceiver::PostRequest(event);
143 void ReqReceiverMessage::sync(const EventMessagingServicePtr& event)
146 EventMessagingServiceReqReceiver::PostRequest(event);
149 void ReqReceiverMessage::syncFolder(const EventMessagingServicePtr& event)
152 EventMessagingServiceReqReceiver::PostRequest(event);
155 void ReqReceiverMessage::OnRequestReceived(const EventAddDraftMessagePtr& event)
160 IMessagePtr msg = event->msg;
161 if (!event->checkCancelled()) {
162 event->setCancelAllowed(false);
163 msg->addMessageToDraft();
165 event->setCancelAllowed(true);
168 Catch(WrtDeviceApis::Commons::PlatformException) {
169 MsgLogError("platform exception");
170 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
175 void ReqReceiverMessage::OnRequestReceived(const EventSendMessagePtr& event)
180 IMessagePtr msg = event->msg;
181 //this prevents calling BONDI, Tizen callback immidiately after leaving this function
182 //callback will be called after receving platform callback.
183 msg->setRequestReceiver(this);
184 msg->setSendMessageEvent(event);
185 if (!event->checkCancelled()) {
186 event->setCancelAllowed(false);
187 event->switchToManualAnswer();
188 int handle = msg->send();
189 LogDebug("handle : " << handle);
190 if (event->opId && event->m_messagingService)
192 event->m_messagingService->setHandleToOpId(event->opId, handle);
195 event->setCancelAllowed(true);
198 Catch(WrtDeviceApis::Commons::PlatformException) {
199 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
200 WrtDeviceApis::Commons::EventRequestReceiver<EventSendMessage>::ManualAnswer(event);
204 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagePtr& event)
209 IMessagePtr msg = event->msg;
210 if (!event->checkCancelled()) {
211 event->setCancelAllowed(false);
214 event->setCancelAllowed(true);
217 Catch(WrtDeviceApis::Commons::PlatformException) {
218 MsgLogError("platform exception");
219 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
223 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagePtr& event)
228 IMessagePtr msg = event->msg;
231 Catch(WrtDeviceApis::Commons::PlatformException) {
232 MsgLogError("platform exception");
233 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
237 bool ReqReceiverMessage::validateFilter(const Tizen::FilterPtr& filter){
240 bool retBool = false;
243 MessageFilterValidatorPtr validator =
244 MessageFilterValidatorFactory::getMessageFilterValidator();
246 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
247 retBool = filter->validate(filterValidator);
248 LogDebug("retBool:" << retBool);
249 if(retBool == false){
250 MsgLogError(">>> Validate FAIL InvalidArgumentException");
251 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
254 LogDebug("filter is NULL");
258 LogDebug(">>> retBool:" << retBool);
263 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagesPtr& event)
268 Tizen::FilterPtr filter = event->getFilter();
270 bool isValidFilter = validateFilter(filter);
271 if(isValidFilter == false){
272 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
275 event->msgArray = IMessaging::getInstance().findMessages(event->getFilter());
278 std::vector<Api::Messaging::IMessagePtr> msg = event->msgArray;
279 LogDebug("msg.size()" << msg.size());
281 int vecSize = msg.size();
282 for(int i=0; i < vecSize; i++){
283 int type = msg[i]->getMessageType();
284 std::string id = msg[i]->getId();
285 MsgLogError("type :"<<type);
286 MsgLogError("id :"<<id);
287 MsgLogError("body" << msg[i]->getBody());
290 }Catch(WrtDeviceApis::Commons::PlatformException) {
291 MsgLogError("platform exception");
292 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
296 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagesPtr& event)
301 std::vector<Api::Messaging::IMessagePtr> msg = event->msgArray;
302 LogDebug("msg.size()" << msg.size());
303 int vecSize = msg.size();
304 for(int i=0; i < vecSize; i++)
306 // int type = msg[i]->getMessageType();
307 std::string id = msg[i]->getId();
308 msg[i]->updateIsRead();
311 Catch(WrtDeviceApis::Commons::PlatformException) {
312 MsgLogError("platform exception");
313 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
317 void ReqReceiverMessage::OnRequestReceived(const EventQueryMessagesPtr& event)
323 vector<IMessagePtr> msgs;
325 //check filter validation
326 Tizen::FilterPtr filter = event->getFilter();
327 bool isValidFilter = validateFilter(filter);
328 if(isValidFilter == false){
329 MsgLogError("[ERROR]this filter is invalid");
330 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
334 vector<IMessagePtr> tmp = IMessaging::getInstance().findMessages(event->getFilter(), event->getSortMode(), event->getLimit(), event->getOffset());
335 msgs.insert(msgs.end(), tmp.begin(), tmp.end());
338 }Catch(WrtDeviceApis::Commons::PlatformException) {
339 MsgLogError("platform exception");
340 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
341 }Catch(WrtDeviceApis::Commons::InvalidArgumentException){
342 MsgLogError("InvalidArgumentException");
343 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
347 void ReqReceiverMessage::OnRequestReceived(const EventGetConversationIdPtr& event)
352 Api::Messaging::IMessagePtr msg = event->getConversationMsgPtr();
353 Api::Messaging::MessageType msgType = msg->getMessageType();
354 unsigned int msgId = 0;
356 if (msgType == EMAIL)
358 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
359 msgId = email->getUID();
364 msgId = msg->convertId(msg->getId());
367 int conversationId = IMessaging::getInstance().getConversationId(msgId, msgType);
368 LogDebug("Cond Id:" << conversationId);
369 event->setConversationId(conversationId);
371 Catch(WrtDeviceApis::Commons::PlatformException) {
372 MsgLogError("platform exception");
373 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
377 void ReqReceiverMessage::OnRequestReceived(const EventQueryConversationsPtr& event)
383 //check filter validation
384 Tizen::FilterPtr filter = event->getFilter();
386 ConversationFilterValidatorPtr validator =
387 ConversationFilterValidatorFactory::getConversationFilterValidator();
389 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
390 bool isValidFilter = filter->validate(filterValidator);
391 if(isValidFilter == false){
392 MsgLogError("[ERROR]Filter is Invalid");
393 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
397 vector<IConversationPtr> tmp = IMessaging::getInstance().queryConversations(event->getSortMode(), event->getFilter(), event->getLimit(), event->getOffset());
398 event->addConversations(tmp);
401 Catch(WrtDeviceApis::Commons::PlatformException)
403 MsgLogError("platform exception");
404 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
406 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
408 MsgLogError("InvalidArgumentException");
409 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
413 void ReqReceiverMessage::OnRequestReceived(const EventMessagingServicePtr& event)
415 LogDebug("enter : EventMessagingServicePtr");
418 LogDebug("event type :" << event->getEventType() );
419 int MessagingServiceEventType = event->getEventType();
421 if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY)
423 if (!event->checkCancelled()) {
424 IMessagePtr msg = event->m_message;
427 msg->setRequestReceiver(this);
428 msg->setMessagingServiceEvent(event);
430 int type = msg->getMessageType();
431 LogDebug("message type :" << type);
432 if ( msg->getMessageType() == Api::Messaging::EMAIL )
434 IEmailPtr email = MessageFactory::convertToEmail(msg);
435 event->switchToManualAnswer(); //switch to manual answer;
436 int handle = email->downloadBody();
437 LogDebug("Load Message Body handle : " << handle);
438 if (event->opId && event->m_messagingService)
440 event->m_messagingService->setHandleToOpId(event->opId, handle);
445 LogDebug(" invaild message. ");
446 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
452 LogDebug(" Cancel ");
453 event->setCancelAllowed(true);
457 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
459 if (!event->checkCancelled()) {
460 IMessagePtr msg = event->m_message;
463 msg->setRequestReceiver(this);
464 msg->setMessagingServiceEvent(event);
466 int type = msg->getMessageType();
467 LogDebug("message type :" << type);
468 if ( msg->getMessageType() == Api::Messaging::EMAIL )
470 IEmailPtr email = MessageFactory::convertToEmail(msg);
471 event->switchToManualAnswer(); //switch to manual answer;
472 int handle = email->downloadAttachment(event->m_attachment);
473 LogDebug("Load Message Attachment handle : " << handle);
474 if (event->opId && event->m_messagingService)
476 event->m_messagingService->setHandleToOpId(event->opId, handle);
481 LogDebug(" invaild message. ");
482 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
488 LogDebug(" cancel. ");
489 event->setCancelAllowed(true);
494 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC)
496 if (event->m_messagingService)
498 if (!event->checkCancelled()) {
499 int type = event->m_messagingService->getType();
500 LogDebug("messaging service type :" << type);
502 event->m_messagingService->setRequestReceiver(this);
503 event->m_messagingService->setMessagingServiceEvent(event); //
505 if (type == Api::Messaging::EMAIL)
507 event->switchToManualAnswer(); //switch to manual answer;
508 LogDebug("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
510 if ( event->m_messagingService->getAccountID() > 0)
512 int handle = event->m_messagingService->sync( event->m_messagingService, event->m_sync_limit );
513 LogDebug("Sync handle : " << handle);
514 if (event->opId && event->m_messagingService)
516 event->m_messagingService->setHandleToOpId(event->opId, handle);
520 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
524 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
529 LogDebug(" cancel. ");
530 event->setCancelAllowed(true);
535 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
537 if (event->m_messagingService)
539 if (!event->checkCancelled()) {
540 int type = event->m_messagingService->getType();
541 LogDebug("messaging service type :" << type);
543 event->m_messagingService->setRequestReceiver(this);
544 event->m_messagingService->setMessagingServiceEvent(event); //
546 if (type == Api::Messaging::EMAIL)
548 event->switchToManualAnswer(); //switch to manual answer;
549 LogDebug("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
551 if ( event->m_messagingService->getAccountID() > 0)
553 int handle = event->m_messagingService->syncFolder( event->m_messagingService, event->m_folder_name, event->m_sync_limit);
554 LogDebug("Sync Folder handle : " << handle);
555 if (event->opId && event->m_messagingService)
557 event->m_messagingService->setHandleToOpId(event->opId, handle);
561 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
565 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
570 LogDebug(" cancel. ");
571 event->setCancelAllowed(true);
577 LogDebug(" Cancel ");
578 event->setCancelAllowed(true);
582 Catch (WrtDeviceApis::Commons::PlatformException) {
583 MsgLogError("platform exception");
584 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
589 void ReqReceiverMessage::OnRequestReceived(const EventDeleteConversationsPtr& event)
595 if (event->isFilterUsing())
597 LogDebug("filter use");
599 //check filter validation
600 FilterPtr filter = event->getFilter();
603 ConversationFilterValidatorPtr validator =
604 ConversationFilterValidatorFactory::getConversationFilterValidator();
607 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
608 bool isValidFilter = filter->validate(filterValidator);
610 if(isValidFilter == false)
612 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
616 if (IMessaging::getInstance().deleteConversations(event->getSortMode(), event->getFilter()) == false)
618 Throw(WrtDeviceApis::Commons::PlatformException);
620 //.queryConversations(event->getSortMode(), event->getFilter());
624 LogDebug("no filter");
626 if (IMessaging::getInstance().deleteConversations(event->getConversations()) == false)
628 Throw(WrtDeviceApis::Commons::PlatformException);
632 Catch(WrtDeviceApis::Commons::PlatformException)
634 MsgLogError("platform exception");
635 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
637 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
639 MsgLogError("InvalidArgumentException");
640 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
644 void ReqReceiverMessage::OnRequestReceived(const EventQueryFoldersPtr& event)
651 //check filter validation
652 Tizen::FilterPtr filter = event->getFilter();
654 FolderFilterValidatorPtr validator =
655 FolderFilterValidatorFactory::getFolderFilterValidator();
657 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
659 bool isValidFilter = filter->validate(filterValidator);
660 if(isValidFilter == false){
661 MsgLogError("[ERROR]Filter is Invalid");
662 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
667 vector<IMessageFolderPtr> tmp = IMessaging::getInstance().queryFolders(event->getFilter());
668 event->addFolders(tmp);
670 Catch(WrtDeviceApis::Commons::PlatformException)
672 MsgLogError("platform exception");
673 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
675 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
677 MsgLogError("InvalidArgumentException");
678 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);