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"
34 #include <emf-types.h>
39 using namespace TizenApis::Api::Tizen;
40 using namespace TizenApis::Platform::Messaging;
41 using namespace WrtDeviceApis::Commons;
48 ReqReceiverMessage::ReqReceiverMessage() :
49 EventAddDraftMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
50 EventSendMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
51 EventUpdateMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
52 EventDeleteMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
53 EventDeleteMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
54 EventUpdateMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
55 EventQueryMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
56 EventGetConversationIdReqReceiver(ThreadEnum::MESSAGING_THREAD),
57 EventQueryConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
58 EventDeleteConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
59 EventQueryFoldersReqReceiver(ThreadEnum::MESSAGING_THREAD),
60 EventMessagingServiceReqReceiver(ThreadEnum::MESSAGING_THREAD)
62 LogDebug("create receiver for messaging");
65 ReqReceiverMessage::~ReqReceiverMessage()
69 void ReqReceiverMessage::addDraftMessage(const EventAddDraftMessagePtr& event)
72 EventAddDraftMessageReqReceiver::PostRequest(event);
75 void ReqReceiverMessage::sendMessage(const EventSendMessagePtr& event)
78 EventSendMessageReqReceiver::PostRequest(event);
81 void ReqReceiverMessage::updateMessage(const EventUpdateMessagePtr& event)
84 EventUpdateMessageReqReceiver::PostRequest(event);
87 void ReqReceiverMessage::deleteMessage(const EventDeleteMessagePtr& event)
90 EventDeleteMessageReqReceiver::PostRequest(event);
93 void ReqReceiverMessage::deleteMessages(const EventDeleteMessagesPtr& event)
96 EventDeleteMessagesReqReceiver::PostRequest(event);
99 void ReqReceiverMessage::updateMessages(const EventUpdateMessagesPtr& event)
102 EventUpdateMessagesReqReceiver::PostRequest(event);
105 void ReqReceiverMessage::queryMessages(const EventQueryMessagesPtr& event)
108 EventQueryMessagesReqReceiver::PostRequest(event);
112 void ReqReceiverMessage::getConversationId(const EventGetConversationIdPtr& event)
115 EventGetConversationIdReqReceiver::PostRequest(event);
118 void ReqReceiverMessage::queryConversations(const EventQueryConversationsPtr& event)
121 EventQueryConversationsReqReceiver::PostRequest(event);
124 void ReqReceiverMessage::deleteConversations(const EventDeleteConversationsPtr& event)
127 EventDeleteConversationsReqReceiver::PostRequest(event);
130 void ReqReceiverMessage::queryFolders(const EventQueryFoldersPtr& event)
133 EventQueryFoldersReqReceiver::PostRequest(event);
136 void ReqReceiverMessage::loadMessageBody(const EventMessagingServicePtr& event)
139 EventMessagingServiceReqReceiver::PostRequest(event);
142 void ReqReceiverMessage::loadMessageAttachment(const EventMessagingServicePtr& event)
145 EventMessagingServiceReqReceiver::PostRequest(event);
148 void ReqReceiverMessage::sync(const EventMessagingServicePtr& event)
151 EventMessagingServiceReqReceiver::PostRequest(event);
154 void ReqReceiverMessage::syncFolder(const EventMessagingServicePtr& event)
157 EventMessagingServiceReqReceiver::PostRequest(event);
160 void ReqReceiverMessage::OnRequestReceived(const EventAddDraftMessagePtr& event)
165 IMessagePtr msg = event->msg;
166 if (!event->checkCancelled()) {
167 event->setCancelAllowed(false);
168 emf_account_t* accounts = NULL;
170 if (msg->getMessageType() == Api::Messaging::EMAIL )
172 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
176 if (email_get_account_list(&accounts, &count)) {
179 Api::Messaging::EmailAccountInfo account(accounts[0].account_id,
180 accounts[0].user_name,
181 accounts[0].email_addr );
182 email->setEmailAccount(account);
184 if (accounts != NULL) {
185 email_free_account(&accounts, count);
190 Catch(WrtDeviceApis::Commons::PlatformException) {
191 if (accounts != NULL) {
192 email_free_account(&accounts, count);
199 msg->addMessageToDraft();
201 event->setCancelAllowed(true);
204 Catch(WrtDeviceApis::Commons::PlatformException) {
205 MsgLogError("platform exception");
206 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
211 void ReqReceiverMessage::OnRequestReceived(const EventSendMessagePtr& event)
216 IMessagePtr msg = event->msg;
217 //this prevents calling BONDI, Tizen callback immidiately after leaving this function
218 //callback will be called after receving platform callback.
219 msg->setRequestReceiver(this);
220 msg->setSendMessageEvent(event);
221 if (!event->checkCancelled()) {
222 event->setCancelAllowed(false);
223 event->switchToManualAnswer();
224 if (msg->getMessageStatus() == Api::Messaging::MESSAGE_STATUS_CREATED ) //create message.
227 if (msg->getMessageType() == Api::Messaging::EMAIL )
229 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
230 email->setEmailAccount(event->m_messagingService->getCurrentEmailAccount());
233 msg->addMessageToDraft(); //save
235 LogDebug("Message Status = " << msg->getMessageStatus());
236 LogDebug("Message ID = " << msg->getId());
239 int handle = msg->send();
240 LogDebug("handle : " << handle);
241 if (event->opId && event->m_messagingService)
243 event->m_messagingService->setHandleToOpId(event->opId, handle);
246 event->setCancelAllowed(true);
249 Catch(WrtDeviceApis::Commons::PlatformException) {
250 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
251 WrtDeviceApis::Commons::EventRequestReceiver<EventSendMessage>::ManualAnswer(event);
255 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagePtr& event)
260 IMessagePtr msg = event->msg;
261 if (!event->checkCancelled()) {
262 event->setCancelAllowed(false);
265 event->setCancelAllowed(true);
268 Catch(WrtDeviceApis::Commons::PlatformException) {
269 MsgLogError("platform exception");
270 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
274 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagePtr& event)
279 IMessagePtr msg = event->msg;
282 Catch(WrtDeviceApis::Commons::PlatformException) {
283 MsgLogError("platform exception");
284 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
288 bool ReqReceiverMessage::validateFilter(const Tizen::FilterPtr& filter){
291 bool retBool = false;
294 MessageFilterValidatorPtr validator =
295 MessageFilterValidatorFactory::getMessageFilterValidator();
297 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
298 retBool = filter->validate(filterValidator);
299 LogDebug("retBool:" << retBool);
300 if(retBool == false){
301 MsgLogError(">>> Validate FAIL InvalidArgumentException");
302 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
305 LogDebug("filter is NULL");
309 LogDebug(">>> retBool:" << retBool);
314 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagesPtr& event)
319 Tizen::FilterPtr filter = event->getFilter();
321 bool isValidFilter = validateFilter(filter);
322 if(isValidFilter == false){
323 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
326 event->msgArray = IMessaging::getInstance().findMessages(event->getFilter());
329 std::vector<Api::Messaging::IMessagePtr> msg = event->msgArray;
330 LogDebug("msg.size()" << msg.size());
332 int vecSize = msg.size();
333 for(int i=0; i < vecSize; i++){
334 int type = msg[i]->getMessageType();
335 std::string id = msg[i]->getId();
336 MsgLogError("type :"<<type);
337 MsgLogError("id :"<<id);
338 MsgLogError("body" << msg[i]->getBody());
341 }Catch(WrtDeviceApis::Commons::PlatformException) {
342 MsgLogError("platform exception");
343 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
347 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagesPtr& event)
352 std::vector<Api::Messaging::IMessagePtr> msg = event->msgArray;
353 LogDebug("msg.size()" << msg.size());
354 int vecSize = msg.size();
355 for(int i=0; i < vecSize; i++)
357 // int type = msg[i]->getMessageType();
358 std::string id = msg[i]->getId();
359 msg[i]->updateIsRead();
362 Catch(WrtDeviceApis::Commons::PlatformException) {
363 MsgLogError("platform exception");
364 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
368 void ReqReceiverMessage::OnRequestReceived(const EventQueryMessagesPtr& event)
374 vector<IMessagePtr> msgs;
376 //check filter validation
377 Tizen::FilterPtr filter = event->getFilter();
378 bool isValidFilter = validateFilter(filter);
379 if(isValidFilter == false){
380 MsgLogError("[ERROR]this filter is invalid");
381 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
385 vector<IMessagePtr> tmp = IMessaging::getInstance().findMessages(event->getFilter(), event->getSortMode(), event->getLimit(), event->getOffset());
386 msgs.insert(msgs.end(), tmp.begin(), tmp.end());
389 }Catch(WrtDeviceApis::Commons::PlatformException) {
390 MsgLogError("platform exception");
391 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
392 }Catch(WrtDeviceApis::Commons::InvalidArgumentException){
393 MsgLogError("InvalidArgumentException");
394 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
398 void ReqReceiverMessage::OnRequestReceived(const EventGetConversationIdPtr& event)
403 Api::Messaging::IMessagePtr msg = event->getConversationMsgPtr();
404 Api::Messaging::MessageType msgType = msg->getMessageType();
405 unsigned int msgId = 0;
407 if (msgType == EMAIL)
409 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
410 msgId = email->getUID();
415 msgId = msg->convertId(msg->getId());
418 int conversationId = IMessaging::getInstance().getConversationId(msgId, msgType);
419 LogDebug("Cond Id:" << conversationId);
420 event->setConversationId(conversationId);
422 Catch(WrtDeviceApis::Commons::PlatformException) {
423 MsgLogError("platform exception");
424 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
428 void ReqReceiverMessage::OnRequestReceived(const EventQueryConversationsPtr& event)
434 //check filter validation
435 Tizen::FilterPtr filter = event->getFilter();
437 ConversationFilterValidatorPtr validator =
438 ConversationFilterValidatorFactory::getConversationFilterValidator();
440 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
441 bool isValidFilter = filter->validate(filterValidator);
442 if(isValidFilter == false){
443 MsgLogError("[ERROR]Filter is Invalid");
444 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
448 vector<IConversationPtr> tmp = IMessaging::getInstance().queryConversations(event->getSortMode(), event->getFilter(), event->getLimit(), event->getOffset());
449 event->addConversations(tmp);
452 Catch(WrtDeviceApis::Commons::PlatformException)
454 MsgLogError("platform exception");
455 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
457 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
459 MsgLogError("InvalidArgumentException");
460 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
464 void ReqReceiverMessage::OnRequestReceived(const EventMessagingServicePtr& event)
466 LogDebug("enter : EventMessagingServicePtr");
469 LogDebug("event type :" << event->getEventType() );
470 int MessagingServiceEventType = event->getEventType();
472 if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY)
474 if (!event->checkCancelled()) {
475 IMessagePtr msg = event->m_message;
478 msg->setRequestReceiver(this);
479 msg->setMessagingServiceEvent(event);
481 int type = msg->getMessageType();
482 LogDebug("message type :" << type);
483 if ( msg->getMessageType() == Api::Messaging::EMAIL )
485 IEmailPtr email = MessageFactory::convertToEmail(msg);
486 event->switchToManualAnswer(); //switch to manual answer;
487 int handle = email->downloadBody();
488 LogDebug("Load Message Body handle : " << handle);
489 if (event->opId && event->m_messagingService)
491 event->m_messagingService->setHandleToOpId(event->opId, handle);
496 LogDebug(" invaild message. ");
497 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
503 LogDebug(" Cancel ");
504 event->setCancelAllowed(true);
508 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
510 if (!event->checkCancelled()) {
511 IMessagePtr msg = event->m_message;
514 msg->setRequestReceiver(this);
515 msg->setMessagingServiceEvent(event);
517 int type = msg->getMessageType();
518 LogDebug("message type :" << type);
519 if ( msg->getMessageType() == Api::Messaging::EMAIL )
521 IEmailPtr email = MessageFactory::convertToEmail(msg);
522 event->switchToManualAnswer(); //switch to manual answer;
523 int handle = email->downloadAttachment(event->m_attachment);
524 LogDebug("Load Message Attachment handle : " << handle);
525 if (event->opId && event->m_messagingService)
527 event->m_messagingService->setHandleToOpId(event->opId, handle);
532 LogDebug(" invaild message. ");
533 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
539 LogDebug(" cancel. ");
540 event->setCancelAllowed(true);
545 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC)
547 if (event->m_messagingService)
549 if (!event->checkCancelled()) {
550 int type = event->m_messagingService->getType();
551 LogDebug("messaging service type :" << type);
553 event->m_messagingService->setRequestReceiver(this);
554 event->m_messagingService->setMessagingServiceEvent(event); //
556 if (type == Api::Messaging::EMAIL)
558 event->switchToManualAnswer(); //switch to manual answer;
559 LogDebug("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
561 if ( event->m_messagingService->getAccountID() > 0)
563 int handle = event->m_messagingService->sync( event->m_messagingService, event->m_sync_limit );
564 LogDebug("Sync handle : " << handle);
565 if (event->opId && event->m_messagingService)
567 event->m_messagingService->setHandleToOpId(event->opId, handle);
571 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
575 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
580 LogDebug(" cancel. ");
581 event->setCancelAllowed(true);
586 else if (MessagingServiceEventType == EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
588 if (event->m_messagingService)
590 if (!event->checkCancelled()) {
591 int type = event->m_messagingService->getType();
592 LogDebug("messaging service type :" << type);
594 event->m_messagingService->setRequestReceiver(this);
595 event->m_messagingService->setMessagingServiceEvent(event); //
597 if (type == Api::Messaging::EMAIL)
599 event->switchToManualAnswer(); //switch to manual answer;
600 LogDebug("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
602 if ( event->m_messagingService->getAccountID() > 0)
604 int handle = event->m_messagingService->syncFolder( event->m_messagingService, event->m_folder_name, event->m_sync_limit);
605 LogDebug("Sync Folder handle : " << handle);
606 if (event->opId && event->m_messagingService)
608 event->m_messagingService->setHandleToOpId(event->opId, handle);
612 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
616 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
621 LogDebug(" cancel. ");
622 event->setCancelAllowed(true);
628 LogDebug(" Cancel ");
629 event->setCancelAllowed(true);
633 Catch (WrtDeviceApis::Commons::PlatformException) {
634 MsgLogError("platform exception");
635 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
640 void ReqReceiverMessage::OnRequestReceived(const EventDeleteConversationsPtr& event)
646 if (event->isFilterUsing())
648 LogDebug("filter use");
650 //check filter validation
651 FilterPtr filter = event->getFilter();
654 ConversationFilterValidatorPtr validator =
655 ConversationFilterValidatorFactory::getConversationFilterValidator();
658 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
659 bool isValidFilter = filter->validate(filterValidator);
661 if(isValidFilter == false)
663 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
667 if (IMessaging::getInstance().deleteConversations(event->getSortMode(), event->getFilter()) == false)
669 Throw(WrtDeviceApis::Commons::PlatformException);
671 //.queryConversations(event->getSortMode(), event->getFilter());
675 LogDebug("no filter");
677 if (IMessaging::getInstance().deleteConversations(event->getConversations()) == false)
679 Throw(WrtDeviceApis::Commons::PlatformException);
683 Catch(WrtDeviceApis::Commons::PlatformException)
685 MsgLogError("platform exception");
686 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
688 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
690 MsgLogError("InvalidArgumentException");
691 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
695 void ReqReceiverMessage::OnRequestReceived(const EventQueryFoldersPtr& event)
702 //check filter validation
703 Tizen::FilterPtr filter = event->getFilter();
705 FolderFilterValidatorPtr validator =
706 FolderFilterValidatorFactory::getFolderFilterValidator();
708 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
710 bool isValidFilter = filter->validate(filterValidator);
711 if(isValidFilter == false){
712 MsgLogError("[ERROR]Filter is Invalid");
713 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
718 vector<IMessageFolderPtr> tmp = IMessaging::getInstance().queryFolders(event->getFilter());
719 event->addFolders(tmp);
721 Catch(WrtDeviceApis::Commons::PlatformException)
723 MsgLogError("platform exception");
724 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
726 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
728 MsgLogError("InvalidArgumentException");
729 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);