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].user_display_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 EventDeleteMessagesPtr& event)
306 std::vector<IMessagePtr> msg = event->msgArray;
307 MessageType msgType = UNKNOWN;
309 int vecSize = msg.size();
310 for(int i=0; i < vecSize; i++){
311 int type = msg[i]->getMessageType();
312 std::string id = msg[i]->getId();
318 msgType = msg[vecSize-1]->getMessageType();
321 if (msgType == EMAIL)
323 // removeCheck the last message
324 IMessagePtr lastMsg = msg[vecSize-1];
328 LoggerD("lastMsg ID " << lastMsg->getId());
329 IEmailPtr email = MessageFactory::convertToEmail(lastMsg);
330 if(!email->removeFinishCheck())
332 lastMsg->setRequestReceiver(this);
333 lastMsg->setRemoveMessagesEvent(event);
334 LoggerD("switchToManualAnswer ");
335 event->switchToManualAnswer(); //switch to manual answer;
341 }Catch(WrtDeviceApis::Commons::PlatformException) {
342 MsgLoggerE("platform exception");
343 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
347 void ReqReceiverMessage::OnRequestReceived(const EventUpdateMessagesPtr& event)
352 std::vector<IMessagePtr> msg = event->msgArray;
353 LoggerD("msg.size()" << msg.size());
354 int vecSize = msg.size();
355 for(int i=0; i < vecSize; i++)
357 std::string id = msg[i]->getId();
358 msg[i]->updateIsRead();
359 msg[i]->updateMessage();
362 Catch(WrtDeviceApis::Commons::PlatformException) {
363 MsgLoggerE("platform exception");
364 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
368 void ReqReceiverMessage::OnRequestReceived(const EventQueryMessagesPtr& event)
374 vector<IMessagePtr> msgs;
375 vector<IMessagePtr> tmp;
377 //check filter validation
378 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
379 bool isValidFilter = validateFilter(filter);
380 if(isValidFilter == false){
381 MsgLoggerE("[ERROR]this filter is invalid");
382 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
386 LoggerD("event->getType() = " << event->getType());
387 if(event->getType() > UNKNOWN)
389 tmp = IMessaging::getInstance().findMessages(event->getFilter(), event->getSortMode(), event->getLimit(), event->getOffset(), event->getType());
393 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
396 msgs.insert(msgs.end(), tmp.begin(), tmp.end());
399 }Catch(WrtDeviceApis::Commons::PlatformException) {
400 MsgLoggerE("platform exception");
401 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
402 }Catch(WrtDeviceApis::Commons::InvalidArgumentException){
403 MsgLoggerE("InvalidArgumentException");
404 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
407 MsgLoggerE("platform exception");
408 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
412 void ReqReceiverMessage::OnRequestReceived(const EventGetConversationIdPtr& event)
417 IMessagePtr msg = event->getConversationMsgPtr();
418 MessageType msgType = msg->getMessageType();
419 unsigned int msgId = 0;
421 if (msgType == EMAIL)
423 IEmailPtr email = MessageFactory::convertToEmail(msg);
424 msgId = email->getUID();
429 msgId = msg->convertId(msg->getId());
432 int conversationId = IMessaging::getInstance().getConversationId(msgId, msgType);
433 LoggerD("Cond Id:" << conversationId);
434 event->setConversationId(conversationId);
436 Catch(WrtDeviceApis::Commons::PlatformException) {
437 MsgLoggerE("platform exception");
438 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
442 void ReqReceiverMessage::OnRequestReceived(const EventQueryConversationsPtr& event)
448 //check filter validation
449 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
450 vector<IConversationPtr> tmp;
452 ConversationFilterValidatorPtr validator =
453 ConversationFilterValidatorFactory::getConversationFilterValidator();
455 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
456 bool isValidFilter = filter->validate(filterValidator);
457 if(isValidFilter == false){
458 MsgLoggerE("[ERROR]Filter is Invalid");
459 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
462 LoggerD("event->getType() = "<<event->getType());
463 if(event->getType() > UNKNOWN)
465 tmp = IMessaging::getInstance().queryConversations(event->getFilter(), event->getSortMode(), event->getType(), event->getLimit(), event->getOffset());
469 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
472 event->addConversations(tmp);
475 Catch(WrtDeviceApis::Commons::PlatformException)
477 MsgLoggerE("platform exception " << _rethrown_exception.GetMessage());
478 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
480 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
482 MsgLoggerE("InvalidArgumentException");
483 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
487 void ReqReceiverMessage::OnRequestReceived(const EventMessagingServicePtr& event)
489 LoggerD("enter : EventMessagingServicePtr");
492 LoggerD("event type :" << event->getEventType() );
493 int MessagingServiceEventType = event->getEventType();
495 if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY)
497 if (!event->checkCancelled()) {
498 IMessagePtr msg = event->m_message;
502 msg->setRequestReceiver(this);
503 msg->setMessagingServiceEvent(event);
505 int type = msg->getMessageType();
506 LoggerD("message type :" << type);
507 if ( msg->getMessageType() == EMAIL )
509 IEmailPtr email = MessageFactory::convertToEmail(msg);
510 event->switchToManualAnswer(); //switch to manual answer;
511 int handle = email->downloadBody();
512 LoggerD("Load Message Body handle : " << handle);
513 if (event->opId && event->m_messagingService)
515 event->m_messagingService->setHandleToOpId(event->opId, handle);
519 event->setHandle(handle); //set handle
524 LoggerD(" invaild message. ");
525 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
532 event->setCancelAllowed(true);
536 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
538 if (!event->checkCancelled()) {
539 IMessagePtr msg = event->m_message;
542 msg->setRequestReceiver(this);
543 msg->setMessagingServiceEvent(event);
545 int type = msg->getMessageType();
546 LoggerD("message type :" << type);
547 if ( msg->getMessageType() == EMAIL )
549 IEmailPtr email = MessageFactory::convertToEmail(msg);
550 event->switchToManualAnswer(); //switch to manual answer;
551 int handle = email->downloadAttachment(event->m_attachment);
552 LoggerD("Load Message Attachment handle : " << handle);
553 if (event->opId && event->m_messagingService)
555 event->m_messagingService->setHandleToOpId(event->opId, handle);
559 event->setHandle(handle); //set handle
564 LoggerD(" invaild message. ");
565 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
571 LoggerD(" cancel. ");
572 event->setCancelAllowed(true);
577 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
579 if (event->m_messagingService)
581 if (!event->checkCancelled()) {
582 int type = event->m_messagingService->getType();
583 LoggerD("messaging service type :" << type);
585 event->m_messagingService->setRequestReceiver(this);
586 event->m_messagingService->setMessagingServiceEvent(event); //
590 event->switchToManualAnswer(); //switch to manual answer;
591 LoggerD("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
593 if ( event->m_messagingService->getAccountID() > 0)
595 int handle = event->m_messagingService->sync( event->m_messagingService, event->m_sync_limit );
596 LoggerD("Sync handle : " << handle);
597 if (event->opId && event->m_messagingService)
599 event->m_messagingService->setHandleToOpId(event->opId, handle);
603 event->setHandle(handle); //set handle
608 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
612 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
617 LoggerD(" cancel. ");
618 event->setCancelAllowed(true);
623 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
625 if (event->m_messagingService)
627 if (!event->checkCancelled()) {
628 int type = event->m_messagingService->getType();
629 LoggerD("messaging service type :" << type);
631 event->m_messagingService->setRequestReceiver(this);
632 event->m_messagingService->setMessagingServiceEvent(event);
635 event->switchToManualAnswer(); //switch to manual answer;
636 LoggerD("privateData->getAccountId() :" << event->m_messagingService->getAccountID());
638 if ( event->m_messagingService->getAccountID() > 0)
640 int handle = event->m_messagingService->syncFolder( event->m_messagingService, event->m_sync_folder_id, event->m_sync_limit);
641 LoggerD("Sync Folder handle : " << handle);
642 if (event->opId && event->m_messagingService)
644 event->m_messagingService->setHandleToOpId(event->opId, handle);
648 event->setHandle(handle); //set handle
652 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
656 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
661 LoggerD(" cancel. ");
662 event->setCancelAllowed(true);
666 else if (MessagingServiceEventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
668 IMessagePtr msg = event->m_message;
669 msg->setRequestReceiver(this);
670 msg->setMessagingServiceEvent(event);
671 if (!event->checkCancelled())
674 if (msg->getMessageStatus() == MESSAGE_STATUS_CREATED ) //create message.
677 if (msg->getMessageType() == EMAIL )
679 IEmailPtr email = MessageFactory::convertToEmail(msg);
680 email->setEmailAccount(event->m_messagingService->getCurrentEmailAccount());
683 msg->createSendMessage();
684 LoggerD("Message Status = " << msg->getMessageStatus());
685 LoggerD("Message ID = " << msg->getId());
687 event->switchToManualAnswer();
689 int handle = msg->send();
690 LoggerD("handle : " << handle);
691 if (event->opId && event->m_messagingService)
693 event->m_messagingService->setHandleToOpId(event->opId, handle);
698 event->setHandle(handle); //set handle
707 event->setCancelAllowed(true);
711 Catch (WrtDeviceApis::Commons::PlatformException) {
712 MsgLoggerE("platform exception");
713 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
715 Catch (WrtDeviceApis::Commons::ConversionException) {
716 MsgLoggerE("conversion exception");
717 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::ConversionException);
722 void ReqReceiverMessage::OnRequestReceived(const EventDeleteConversationsPtr& event)
728 if (IMessaging::getInstance().deleteConversations(event->getConversations()) == false)
730 Throw(WrtDeviceApis::Commons::PlatformException);
733 Catch(WrtDeviceApis::Commons::PlatformException)
735 MsgLoggerE("platform exception");
736 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
738 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
740 MsgLoggerE("InvalidArgumentException");
741 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);
745 void ReqReceiverMessage::OnRequestReceived(const EventQueryFoldersPtr& event)
752 //check filter validation
753 DeviceAPI::Tizen::FilterPtr filter = event->getFilter();
755 FolderFilterValidatorPtr validator =
756 FolderFilterValidatorFactory::getFolderFilterValidator();
758 FilterValidatorPtr filterValidator = DPL::StaticPointerCast<FilterValidator>(validator);
760 bool isValidFilter = filter->validate(filterValidator);
761 if(isValidFilter == false){
762 MsgLoggerE("[ERROR]Filter is Invalid");
763 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
768 vector<IMessageFolderPtr> tmp = IMessaging::getInstance().queryFolders(event->getFilter());
769 event->addFolders(tmp);
771 Catch(WrtDeviceApis::Commons::PlatformException)
773 MsgLoggerE("platform exception");
774 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
776 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
778 MsgLoggerE("InvalidArgumentException");
779 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException);