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 <Commons/Exception.h>
20 #include "ReqReceiverMessage.h"
21 #include "IMessaging.h"
24 #include "MessageFilterValidatorFactory.h"
25 #include "ConversationFilterValidatorFactory.h"
26 #include "FolderFilterValidatorFactory.h"
29 #include <email-types.h>
30 #include <email-api.h>
31 #include <dpl/singleton_safe_impl.h>
34 IMPLEMENT_SAFE_SINGLETON(DeviceAPI::Messaging::ReqReceiverMessage)
37 using namespace DeviceAPI::Tizen;
38 using namespace DeviceAPI::Messaging;
39 using namespace WrtDeviceApis::Commons;
45 ReqReceiverMessage::ReqReceiverMessage() :
46 EventAddDraftMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
47 // EventSendMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
48 EventUpdateMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
49 // EventDeleteMessageReqReceiver(ThreadEnum::MESSAGING_THREAD),
50 EventDeleteMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
51 EventUpdateMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
52 EventQueryMessagesReqReceiver(ThreadEnum::MESSAGING_THREAD),
53 EventGetConversationIdReqReceiver(ThreadEnum::MESSAGING_THREAD),
54 EventQueryConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
55 EventDeleteConversationsReqReceiver(ThreadEnum::MESSAGING_THREAD),
56 EventQueryFoldersReqReceiver(ThreadEnum::MESSAGING_THREAD),
57 EventMessagingServiceReqReceiver(ThreadEnum::MESSAGING_THREAD)
59 LoggerD("create receiver for messaging");
62 ReqReceiverMessage::~ReqReceiverMessage()
66 void ReqReceiverMessage::addDraftMessage(const EventAddDraftMessagePtr& event)
69 EventAddDraftMessageReqReceiver::PostRequest(event);
72 void ReqReceiverMessage::sendMessage(const EventSendMessagePtr& event)
75 EventSendMessageReqReceiver::PostRequest(event);
78 void ReqReceiverMessage::sendMessage(const EventMessagingServicePtr& event)
81 EventMessagingServiceReqReceiver::PostRequest(event);
84 void ReqReceiverMessage::updateMessage(const EventUpdateMessagePtr& event)
87 EventUpdateMessageReqReceiver::PostRequest(event);
91 void ReqReceiverMessage::deleteMessage(const EventDeleteMessagePtr& event)
94 EventDeleteMessageReqReceiver::PostRequest(event);
98 void ReqReceiverMessage::deleteMessages(const EventDeleteMessagesPtr& event)
101 EventDeleteMessagesReqReceiver::PostRequest(event);
104 void ReqReceiverMessage::updateMessages(const EventUpdateMessagesPtr& event)
107 EventUpdateMessagesReqReceiver::PostRequest(event);
110 void ReqReceiverMessage::queryMessages(const EventQueryMessagesPtr& event)
113 EventQueryMessagesReqReceiver::PostRequest(event);
117 void ReqReceiverMessage::getConversationId(const EventGetConversationIdPtr& event)
120 EventGetConversationIdReqReceiver::PostRequest(event);
123 void ReqReceiverMessage::queryConversations(const EventQueryConversationsPtr& event)
126 EventQueryConversationsReqReceiver::PostRequest(event);
129 void ReqReceiverMessage::deleteConversations(const EventDeleteConversationsPtr& event)
132 EventDeleteConversationsReqReceiver::PostRequest(event);
135 void ReqReceiverMessage::queryFolders(const EventQueryFoldersPtr& event)
138 EventQueryFoldersReqReceiver::PostRequest(event);
141 void ReqReceiverMessage::loadMessageBody(const EventMessagingServicePtr& event)
144 EventMessagingServiceReqReceiver::PostRequest(event);
147 void ReqReceiverMessage::loadMessageAttachment(const EventMessagingServicePtr& event)
150 EventMessagingServiceReqReceiver::PostRequest(event);
153 void ReqReceiverMessage::sync(const EventMessagingServicePtr& event)
156 EventMessagingServiceReqReceiver::PostRequest(event);
159 void ReqReceiverMessage::syncFolder(const EventMessagingServicePtr& event)
163 if (event && event->m_messagingService && event->m_messagingService->getAccountID() > 0)
166 email_account_t* accounts = NULL;
167 if (email_get_account( event->m_messagingService->getAccountID(), GET_FULL_DATA, &accounts))
170 if (accounts == NULL) {
171 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't get accounts");
174 LoggerD("accounts[0].incoming_server_type = " << accounts[0].incoming_server_type);
175 LoggerD("m_folder_name = " << event->m_folder_name);
176 if ( accounts[0].incoming_server_type == EMAIL_SERVER_TYPE_POP3 && event->m_folder_name != "INBOX" )
178 LoggerD("Can't Sync Folder because POP3 spec.");
179 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Operation Type is mismatched");
182 if (accounts != NULL)
184 email_free_account(&accounts, 1);
188 EventMessagingServiceReqReceiver::PostRequest(event);
192 bool ReqReceiverMessage::validateFilter(const DeviceAPI::Tizen::FilterPtr& filter){
195 bool retBool = false;
198 MessageFilterValidatorPtr validator =
199 MessageFilterValidatorFactory::getMessageFilterValidator();
201 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
202 retBool = filter->validate(filterValidator);
203 LoggerD("retBool:" << retBool);
204 if(retBool == false){
205 MsgLoggerE(">>> Validate FAIL InvalidArgumentException");
206 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
209 LoggerD("filter is NULL");
213 LoggerD(">>> retBool:" << retBool);
218 void ReqReceiverMessage::OnRequestReceived(const EventAddDraftMessagePtr& event)
223 IMessagePtr msg = event->msg;
224 if(event->getType() != msg->getMessageType())
226 LoggerD("msg type and serviceType is not match");
227 LoggerD("service type : "<< event->getType());
228 LoggerD("msg type : "<< msg->getMessageType());
229 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
231 if (!event->checkCancelled())
233 event->setCancelAllowed(false);
234 email_account_t* accounts = NULL;
237 if (msg->getMessageType() == EMAIL )
240 IEmailPtr email = MessageFactory::convertToEmail(msg);
243 if (email_get_account_list(&accounts, &count)) {
246 int index = (int)event->getIndex();
247 EmailAccountInfo account(accounts[index-1].account_id,
248 accounts[index-1].incoming_server_user_name,
249 accounts[index-1].user_email_address );
250 email->setEmailAccount(account);
253 if (accounts != NULL) {
254 email_free_account(&accounts, count);
257 Catch(WrtDeviceApis::Commons::PlatformException) {
258 if (accounts != NULL) {
259 email_free_account(&accounts, count);
261 MsgLoggerE("platform exception");
262 Throw(WrtDeviceApis::Commons::PlatformException);
265 msg->addMessageToDraft();
269 event->setCancelAllowed(true);
272 Catch(WrtDeviceApis::Commons::PlatformException) {
273 MsgLoggerE("platform exception");
274 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
276 Catch(WrtDeviceApis::Commons::InvalidArgumentException){
277 MsgLoggerE("InvalidArgumentException");
278 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
282 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagePtr& event)
287 IMessagePtr msg = event->msg;
288 if (!event->checkCancelled()) {
289 event->setCancelAllowed(false);
292 event->setCancelAllowed(true);
295 Catch(WrtDeviceApis::Commons::PlatformException) {
296 MsgLoggerE("platform exception");
297 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
301 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagePtr& event)
306 IMessagePtr msg = event->msg;
309 Catch(WrtDeviceApis::Commons::PlatformException) {
310 MsgLoggerE("platform exception");
311 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
317 void ReqReceiverMessage::OnRequestReceived(const EventDeleteMessagesPtr& event)
323 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
325 bool isValidFilter = validateFilter(filter);
326 if(isValidFilter == false){
327 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
330 event->msgArray = IMessaging::getInstance().findMessages(event->getFilter());
333 std::vector<IMessagePtr> msg = event->msgArray;
334 LoggerD("msg.size()" << msg.size());
336 int vecSize = msg.size();
337 for(int i=0; i < vecSize; i++){
338 int type = msg[i]->getMessageType();
339 std::string id = msg[i]->getId();
340 MsgLoggerE("type :"<<type);
341 MsgLoggerE("id :"<<id);
342 MsgLoggerE("body" << msg[i]->getBody());
345 }Catch(WrtDeviceApis::Commons::PlatformException) {
346 MsgLoggerE("platform exception");
347 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
351 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagesPtr& event)
356 std::vector<IMessagePtr> msg = event->msgArray;
357 LoggerD("msg.size()" << msg.size());
358 int vecSize = msg.size();
359 for(int i=0; i < vecSize; i++)
361 // int type = msg[i]->getMessageType();
362 std::string id = msg[i]->getId();
363 msg[i]->updateMessage();
364 // msg[i]->updateIsRead();
367 Catch(WrtDeviceApis::Commons::PlatformException) {
368 MsgLoggerE("platform exception");
369 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
373 void ReqReceiverMessage::OnRequestReceived(const EventQueryMessagesPtr& event)
379 vector<IMessagePtr> msgs;
380 vector<IMessagePtr> tmp;
382 //check filter validation
383 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
384 bool isValidFilter = validateFilter(filter);
385 if(isValidFilter == false){
386 MsgLoggerE("[ERROR]this filter is invalid");
387 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
391 LoggerD("event->getType() = " << event->getType());
392 if(event->getType() > UNKNOWN)
394 tmp = IMessaging::getInstance().findMessages(event->getFilter(), event->getSortMode(), event->getLimit(), event->getOffset(), event->getType());
398 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
401 msgs.insert(msgs.end(), tmp.begin(), tmp.end());
404 }Catch(WrtDeviceApis::Commons::PlatformException) {
405 MsgLoggerE("platform exception");
406 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
407 }Catch(WrtDeviceApis::Commons::InvalidArgumentException){
408 MsgLoggerE("InvalidArgumentException");
409 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
412 MsgLoggerE("platform exception");
413 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
417 void ReqReceiverMessage::OnRequestReceived(const EventGetConversationIdPtr& event)
422 IMessagePtr msg = event->getConversationMsgPtr();
423 MessageType msgType = msg->getMessageType();
424 unsigned int msgId = 0;
426 if (msgType == EMAIL)
428 IEmailPtr email = MessageFactory::convertToEmail(msg);
429 msgId = email->getUID();
434 msgId = msg->convertId(msg->getId());
437 int conversationId = IMessaging::getInstance().getConversationId(msgId, msgType);
438 LoggerD("Cond Id:" << conversationId);
439 event->setConversationId(conversationId);
441 Catch(WrtDeviceApis::Commons::PlatformException) {
442 MsgLoggerE("platform exception");
443 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
447 void ReqReceiverMessage::OnRequestReceived(const EventQueryConversationsPtr& event)
453 //check filter validation
454 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
455 vector<IConversationPtr> tmp;
457 ConversationFilterValidatorPtr validator =
458 ConversationFilterValidatorFactory::getConversationFilterValidator();
460 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
461 bool isValidFilter = filter->validate(filterValidator);
462 if(isValidFilter == false){
463 MsgLoggerE("[ERROR]Filter is Invalid");
464 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
467 LoggerD("event->getType() = "<<event->getType());
468 if(event->getType() > UNKNOWN)
470 tmp = IMessaging::getInstance().queryConversations(event->getFilter(), event->getSortMode(), event->getType(), event->getLimit(), event->getOffset());
475 // tmp = IMessaging::getInstance().queryConversations(event->getSortMode(), event->getFilter(), event->getLimit(), event->getOffset());
476 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
479 event->addConversations(tmp);
482 Catch(WrtDeviceApis::Commons::PlatformException)
484 MsgLoggerE("platform exception");
485 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
487 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
489 MsgLoggerE("InvalidArgumentException");
490 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
494 void ReqReceiverMessage::OnRequestReceived(const EventMessagingServicePtr& event)
496 LoggerD("enter : EventMessagingServicePtr");
499 LoggerD("event type :" << event->getEventType() );
500 int MessagingServiceEventType = event->getEventType();
502 if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY)
504 if (!event->checkCancelled()) {
505 IMessagePtr msg = event->m_message;
509 msg->setRequestReceiver(this);
510 msg->setMessagingServiceEvent(event);
512 int type = msg->getMessageType();
513 LoggerD("message type :" << type);
514 if ( msg->getMessageType() == EMAIL )
516 IEmailPtr email = MessageFactory::convertToEmail(msg);
517 event->switchToManualAnswer(); //switch to manual answer;
518 int handle = email->downloadBody();
519 LoggerD("Load Message Body handle : " << handle);
520 if (event->opId && event->m_messagingService)
522 event->m_messagingService->setHandleToOpId(event->opId, handle);
526 event->setHandle(handle); //set handle
531 LoggerD(" invaild message. ");
532 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
539 event->setCancelAllowed(true);
543 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
545 if (!event->checkCancelled()) {
546 IMessagePtr msg = event->m_message;
549 msg->setRequestReceiver(this);
550 msg->setMessagingServiceEvent(event);
552 int type = msg->getMessageType();
553 LoggerD("message type :" << type);
554 if ( msg->getMessageType() == EMAIL )
556 IEmailPtr email = MessageFactory::convertToEmail(msg);
557 event->switchToManualAnswer(); //switch to manual answer;
558 int handle = email->downloadAttachment(event->m_attachment);
559 LoggerD("Load Message Attachment handle : " << handle);
560 if (event->opId && event->m_messagingService)
562 event->m_messagingService->setHandleToOpId(event->opId, handle);
566 event->setHandle(handle); //set handle
571 LoggerD(" invaild message. ");
572 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
578 LoggerD(" cancel. ");
579 event->setCancelAllowed(true);
584 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
586 if (event->m_messagingService)
588 if (!event->checkCancelled()) {
589 int type = event->m_messagingService->getType();
590 LoggerD("messaging service type :" << type);
592 event->m_messagingService->setRequestReceiver(this);
593 event->m_messagingService->setMessagingServiceEvent(event); //
597 event->switchToManualAnswer(); //switch to manual answer;
598 LoggerD("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
600 if ( event->m_messagingService->getAccountID() > 0)
602 int handle = event->m_messagingService->sync( event->m_messagingService, event->m_sync_limit );
603 LoggerD("Sync handle : " << handle);
604 if (event->opId && event->m_messagingService)
606 event->m_messagingService->setHandleToOpId(event->opId, handle);
610 event->setHandle(handle); //set handle
615 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
619 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
624 LoggerD(" cancel. ");
625 event->setCancelAllowed(true);
630 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
632 if (event->m_messagingService)
634 if (!event->checkCancelled()) {
635 int type = event->m_messagingService->getType();
636 LoggerD("messaging service type :" << type);
638 event->m_messagingService->setRequestReceiver(this);
639 event->m_messagingService->setMessagingServiceEvent(event);
642 event->switchToManualAnswer(); //switch to manual answer;
643 LoggerD("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
645 if ( event->m_messagingService->getAccountID() > 0)
647 int handle = event->m_messagingService->syncFolder( event->m_messagingService, event->m_sync_folder_id, event->m_sync_limit);
648 LoggerD("Sync Folder handle : " << handle);
649 if (event->opId && event->m_messagingService)
651 event->m_messagingService->setHandleToOpId(event->opId, handle);
655 event->setHandle(handle); //set handle
659 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
663 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
668 LoggerD(" cancel. ");
669 event->setCancelAllowed(true);
673 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
675 IMessagePtr msg = event->m_message;
676 msg->setRequestReceiver(this);
677 msg->setMessagingServiceEvent(event);
678 if (!event->checkCancelled())
681 if (msg->getMessageStatus() == MESSAGE_STATUS_CREATED ) //create message.
684 if (msg->getMessageType() == EMAIL )
686 IEmailPtr email = MessageFactory::convertToEmail(msg);
687 email->setEmailAccount(event->m_messagingService->getCurrentEmailAccount());
690 msg->createSendMessage();
691 LoggerD("Message Status = " << msg->getMessageStatus());
692 LoggerD("Message ID = " << msg->getId());
694 event->switchToManualAnswer();
696 int handle = msg->send();
697 LoggerD("handle : " << handle);
698 if (event->opId && event->m_messagingService)
700 event->m_messagingService->setHandleToOpId(event->opId, handle);
705 event->setHandle(handle); //set handle
714 event->setCancelAllowed(true);
718 Catch (WrtDeviceApis::Commons::PlatformException) {
719 MsgLoggerE("platform exception");
720 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
722 Catch (WrtDeviceApis::Commons::ConversionException) {
723 MsgLoggerE("conversion exception");
724 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::ConversionException);
729 void ReqReceiverMessage::OnRequestReceived(const EventDeleteConversationsPtr& event)
735 if (event->isFilterUsing())
737 LoggerD("filter use");
739 //check filter validation
740 FilterPtr filter = event->getFilter();
743 ConversationFilterValidatorPtr validator =
744 ConversationFilterValidatorFactory::getConversationFilterValidator();
747 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
748 bool isValidFilter = filter->validate(filterValidator);
750 if(isValidFilter == false)
752 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
756 if (IMessaging::getInstance().deleteConversations(event->getSortMode(), event->getFilter()) == false)
758 Throw(WrtDeviceApis::Commons::PlatformException);
760 //.queryConversations(event->getSortMode(), event->getFilter());
764 LoggerD("no filter");
766 if (IMessaging::getInstance().deleteConversations(event->getConversations()) == false)
768 Throw(WrtDeviceApis::Commons::PlatformException);
772 Catch(WrtDeviceApis::Commons::PlatformException)
774 MsgLoggerE("platform exception");
775 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
777 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
779 MsgLoggerE("InvalidArgumentException");
780 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
784 void ReqReceiverMessage::OnRequestReceived(const EventQueryFoldersPtr& event)
791 //check filter validation
792 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
794 FolderFilterValidatorPtr validator =
795 FolderFilterValidatorFactory::getFolderFilterValidator();
797 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
799 bool isValidFilter = filter->validate(filterValidator);
800 if(isValidFilter == false){
801 MsgLoggerE("[ERROR]Filter is Invalid");
802 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
807 vector<IMessageFolderPtr> tmp = IMessaging::getInstance().queryFolders(event->getFilter());
808 event->addFolders(tmp);
810 Catch(WrtDeviceApis::Commons::PlatformException)
812 MsgLoggerE("platform exception");
813 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
815 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
817 MsgLoggerE("InvalidArgumentException");
818 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);