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.
22 * @author Pawel Misiak (p.misiak@samsung.com)
26 #include <emf-types.h>
29 #include <Commons/Exception.h>
30 #include <Commons/StringUtils.h>
31 #include <dpl/log/log.h>
32 #include <dpl/scoped_free.h>
33 #include "Messaging.h"
36 #include "BinarySms.h"
38 #include "EmailConverter.h"
40 #include "API/Messaging/log.h"
44 #include "API/Messaging/StorageChangesMessageFilterValidatorFactory.h"
45 #include "API/Messaging/StorageChangesConversationFilterValidatorFactory.h"
46 #include "API/Messaging/StorageChangesFolderFilterValidatorFactory.h"
47 #include "MessageQueryGenerator.h"
49 #include "ConversationQueryGenerator.h"
50 #include "FolderQueryGenerator.h"
52 #include "messageDB/MessageStorageReader.h"
55 #include <MapiStorage.h>
56 #include <MapiControl.h>
57 #include <MsgMmsTypes.h>
58 #include <MapiTransport.h>
59 #include <MapiMessage.h>
62 #include <Commons/ThreadPool.h>
63 #include "Conversation.h"
64 #include "MessageFolder.h"
67 #define LOG_ENTER LogDebug("---> ENTER");
68 #define LOG_EXIT LogDebug("---> EXIT");
72 using namespace TizenApis::Api::Messaging;
73 using namespace TizenApis::Api::Tizen;
74 using namespace WrtDeviceApis::Commons;
77 const char* DBUS_INTERFACE_EMAIL_RECEIVED = "User.Email.StorageChange";
78 const char* DBUS_FILTER_EMAIL_RECEIVED =
79 "type='signal',interface='User.Email.StorageChange'";
81 const int MESSAGE_FIND_LIMIT = 100;
88 int Messaging::m_currentEmailAccountId = 0;
89 DPL::Atomic Messaging::m_objCounter;
91 Messaging& Messaging::getInstance()
93 static Messaging instance;
97 Messaging::Messaging() :
98 m_onMessageReceivedHandleMgr(NULL),
99 m_dbusConnection(new DBus::Connection()),
100 m_dbusWorkerThread(new DPL::Thread())
104 const vector<Api::Messaging::EmailAccountInfo> accounts = getEmailAccounts();
105 LogDebug("Number of emails account=" << accounts.size());
106 if (accounts.size() > 0) {
107 // set default email account - first from the list
108 setCurrentEmailAccount(accounts[0]);
110 LogError("no default email account set");
114 Catch(WrtDeviceApis::Commons::PlatformException) {
115 LogError("No email accounts available");
118 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
119 LogError("No email accounts available");
120 //current email not configured, skipped
123 Catch(WrtDeviceApis::Commons::UnknownException) {
124 LogError("unknown error");
127 // Begin service for email management ?? pmi question if it should be added before email actions
128 if (0 == m_objCounter) {
129 int error = email_service_begin();
130 if (EMF_ERROR_NONE != error) {
131 LogError("email_service_begin() returned error " << error);
133 LogInfo("email_service_begin() executed without error");
138 m_dbusWorkerThread->Run();
139 m_dbusConnection->setWorkerThread(m_dbusWorkerThread);
140 m_dbusConnection->addFilter(DBUS_FILTER_EMAIL_RECEIVED);
141 m_dbusConnection->AddListener(this);
144 Messaging::~Messaging()
146 // Endservice for email management
147 m_dbusConnection->RemoveListener(this);
148 m_dbusConnection->setWorkerThread(NULL);
149 m_dbusWorkerThread->Quit();
150 delete m_dbusWorkerThread;
152 if (!--m_objCounter) {
153 int error = email_service_end();
154 if (EMF_ERROR_NONE != error) {
155 LogError("email_service_end() returned error " << error);
157 LogDebug("email_service_end() executed without error");
162 void Messaging::getNumberOfMessages(MessageType msgType,
167 if (NULL == readed ||
169 LogError("output pointers are NULL");
170 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
174 if (Api::Messaging::SMS == msgType) {
175 getNumberOfSms(folder, readed, unReaded);
176 } else if (Api::Messaging::MMS == msgType) {
177 getNumberOfMms(folder, readed, unReaded);
178 } else if (Api::Messaging::EMAIL == msgType) {
179 getNumberOfEmails(folder, readed, unReaded);
181 LogError("wrong message type");
182 Throw(WrtDeviceApis::Commons::PlatformException);
186 vector<IMessagePtr> Messaging::findMessages(const vector<MessageType>& msgTypes,
187 const string &folder,
188 const Api::Tizen::FilterPtr& filter)
191 vector<IMessagePtr> retVal;
192 back_insert_iterator< vector<IMessagePtr> > biit(retVal);
193 vector<MessageType>::const_iterator it = msgTypes.begin();
194 for (; it != msgTypes.end(); ++it) {
195 LogDebug("Finding messages (" << *it << ") in folder: " << folder);
196 vector<IMessagePtr> result;
201 FolderType folderEnum = Sms::toFolder(folder);
202 result = findSms(folderEnum, filter);
207 FolderType folderEnum = Mms::toFolder(folder);
208 result = findMms(folderEnum, filter);
213 result = findEmail(folder, filter);
217 LogError("message type unknown");
218 Throw(WrtDeviceApis::Commons::PlatformException);
220 LogDebug("Found: " << result.size());
221 copy(result.begin(), result.end(), biit);
227 std::string Messaging::generateFilterSql(const Api::Tizen::FilterPtr& filter){
229 std::string filterSql;
231 MessageQueryGeneratorPtr queryGenerator(new MessageQueryGenerator());
232 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
233 filter->travel(filterVisitor, 0);
234 filterSql = queryGenerator->getQuery();
236 LogDebug(">>> filterSql:[" << filterSql << "]");
240 vector<IMessagePtr> Messaging::findMessages(const vector<MessageType>& msgTypes, FolderType folder,
241 const Api::Tizen::FilterPtr& filter)
244 vector<IMessagePtr> retVal;
245 back_insert_iterator< vector<IMessagePtr> > biit(retVal);
247 std::string filterSql = generateFilterSql(filter);
251 vector<MessageType>::const_iterator it = msgTypes.begin();
252 for (; it != msgTypes.end(); ++it) {
253 LogDebug("Finding messages (" << *it << ") in folder: " << folder);
254 vector<IMessagePtr>(Messaging::*findFnPtr)(Api::Messaging::FolderType folder, const Api::Tizen::FilterPtr& filter) = NULL;
259 findFnPtr = &Messaging::findSms;
264 findFnPtr = &Messaging::findMms;
269 findFnPtr = &Messaging::findEmail;
273 LogError("message type unknown");
274 Throw(WrtDeviceApis::Commons::PlatformException);
277 vector<IMessagePtr> result = (this->*findFnPtr)(folder, filter);
278 LogDebug("Found: " << result.size());
279 copy(result.begin(), result.end(), biit);
285 void Messaging::printErrorMessage(int errorCode){
287 case EMF_ERROR_MAIL_NOT_FOUND:{
288 LogDebug("EMF_ERROR_MAIL_NOT_FOUND");
291 case EMF_ERROR_DB_FAILURE:{
292 LogDebug("EMF_ERROR_DB_FAILURE");
296 LogDebug("other error message:" << errorCode);
301 vector<IMessagePtr> Messaging::createVectorFromeMessageList(const MSG_LIST_S& message_list){
304 vector<IMessagePtr> retVal;
305 LogDebug("message_list.nCount:" << message_list.nCount);
306 for (int i = 0; i < message_list.nCount; i++) {
307 // int msgId = msg_get_message_id(message_list.msgInfo[i]);
309 string addressVal = msg_get_ith_address(message_list.msgInfo[i], 0);
310 LogDebug("<<< addressVal:" << addressVal);
312 int msgType = msg_get_message_type(message_list.msgInfo[i]);
313 MessageType webApiMsgType;
317 webApiMsgType = Api::Messaging::SMS;
321 webApiMsgType = Api::Messaging::MMS;
325 LogError("[ERROR]invalid message type:" << msgType);
330 int l_msgId = msg_get_message_id(message_list.msgInfo[i]);
331 Api::Messaging::IMessagePtr msg;
332 msg = MessageFactory::createMessage(webApiMsgType, l_msgId);
333 retVal.push_back(msg);
340 std::vector<Api::Messaging::IConversationPtr> Messaging::createVectorFromeThreadViewList(const MSG_THREAD_VIEW_LIST_S& threadViewList){
341 vector<IConversationPtr> recVec;
343 if (threadViewList.nCount <= 0) {
344 LogDebug("Empty...");
346 for (int i = 0; i < threadViewList.nCount; i++) {
347 // Api::Messaging::IConversationPtr convPtr(new Conversation(i));
348 Api::Messaging::IConversationPtr convPtr(new Conversation(threadViewList.msgThreadInfo[i]));
350 recVec.push_back(convPtr);
357 vector<IMessagePtr> Messaging::querySmsMmsMessages(const std::string& queryString){
358 LogDebug("<<< queryString:[" << queryString <<"]");
360 vector<IMessagePtr> retVal;
361 MSG_LIST_S message_list = {0, NULL};
364 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
366 MessageStorageReader reader;
367 reader.MsgStoConnectDB();
368 res = reader.queryMessage(queryString, &message_list);
370 if (MSG_SUCCESS != res) {
371 LogError("msg_get_folder_view_list failed " << res);
372 Throw(WrtDeviceApis::Commons::PlatformException);
374 reader.MsgStoDisconnectDB();
376 retVal = createVectorFromeMessageList(message_list);
378 msg_release_message_list(&message_list);
379 }Catch(WrtDeviceApis::Commons::PlatformException) {
380 LogError("Problem with message creation, cleaning");
381 if (message_list.nCount) {
382 msg_release_message_list(&message_list);
391 vector<IConversationPtr> Messaging::querySmsMmsConversation(const std::string& sqlWhereClause){
392 LogDebug("sqlWhereClause:[" << sqlWhereClause << "]");
394 std::vector<Api::Messaging::IConversationPtr> recVec;
395 MSG_THREAD_VIEW_LIST_S threadViewList;
397 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
399 MessageStorageReader reader;
400 reader.MsgStoConnectDB();
401 res = reader.queryConversation(sqlWhereClause, &threadViewList);
403 if (MSG_SUCCESS != res) {
404 LogError("queryConversation failed:" << res);
405 Throw(WrtDeviceApis::Commons::PlatformException);
407 reader.MsgStoDisconnectDB();
409 recVec = createVectorFromeThreadViewList(threadViewList);
411 msg_release_thread_view_list(&threadViewList);
417 vector<IConversationPtr> Messaging::queryEmailConversation(const std::string& sqlWhereClause){
418 LogDebug("sqlWhereClause:[" << sqlWhereClause << "]");
420 std::vector<Api::Messaging::IConversationPtr> emailResultVector;
421 emf_mail_list_item_t* mailList = NULL;
422 int mailListCount = 0;
424 errCode = email_query_message_ex(const_cast<char*>(sqlWhereClause.c_str()), &mailList, &mailListCount);
426 if(errCode != EMF_ERROR_NONE){
427 LogError("[ERROR]email_query_message_ex failed:" << errCode);
428 printErrorMessage(errCode);
429 if (mailList != NULL){
433 LogDebug("message found mailListCount:" << mailListCount);
434 std::map<int, int> checkUnique;
436 for(int i=0; i<mailListCount; i++){
437 if (checkUnique.find(mailList[i].thread_id) == checkUnique.end()){
438 checkUnique[mailList[i].thread_id] = mailList[i].thread_id;
439 Api::Messaging::IConversationPtr convPtr(new Conversation(mailList[i].thread_id, Api::Messaging::EMAIL));
442 LogDebug("mailList[i].thread_id:[" << mailList[i].thread_id << "]");
443 LogDebug("mailList[i].from:[" << mailList[i].from << "]");
444 LogDebug("mailList[i].from_email_address : [" << mailList[i].from_email_address <<"]");
445 LogDebug("mailList[i].datetime:[" << mailList[i].datetime << "]");
446 LogDebug("mailList[i].subject:[" << mailList[i].subject << "]");
447 LogDebug("mailList[i].mailbox_name:[" << mailList[i].mailbox_name << "]");
448 LogDebug("mailList[i].previewBodyText:[" << mailList[i].previewBodyText << "]");
449 LogDebug("mailList[i].flags_seen_field:[" << mailList[i].flags_seen_field << "]");
450 LogDebug("mailList[i].priority:[" << mailList[i].priority<< "]");
452 if (convPtr->getResult() == true)
454 emailResultVector.push_back(convPtr);
460 if (mailList != NULL){
465 return emailResultVector;
468 vector<IMessagePtr> Messaging::queryEmailMessage(const std::string& sqlWhereClause){
469 LogDebug("sqlWhereClause:[" << sqlWhereClause << "]");
471 vector<IMessagePtr> emailResultVector;
473 emf_mail_list_item_t* mailList = NULL;
474 int mailListCount = 0;
476 errCode = email_query_message_ex(const_cast<char*>(sqlWhereClause.c_str()), &mailList, &mailListCount);
478 if(errCode != EMF_ERROR_NONE){
479 LogError("[ERROR]email_query_message_ex failed:" << errCode);
480 printErrorMessage(errCode);
481 if (mailList != NULL) {
485 LogDebug("message found mailListCount:" << mailListCount);
487 for(int i=0; i<mailListCount; i++){
488 //Api::Messaging::IMessagePtr msg = MessageFactory::createMessage(EMAIL, mailList[i].mail_id);
489 Api::Messaging::IMessagePtr msg = MessageFactory::createEmailMessage(mailList[i].account_id, mailList[i].mail_id);
492 LogDebug("mailList[i].from:[" << mailList[i].from << "]");
493 LogDebug("mailList[i].from_email_address : [" << mailList[i].from_email_address <<"]");
494 LogDebug("mailList[i].datetime:[" << mailList[i].datetime << "]");
495 LogDebug("mailList[i].subject:[" << mailList[i].subject << "]");
496 LogDebug("mailList[i].mailbox_name:[" << mailList[i].mailbox_name << "]");
497 LogDebug("mailList[i].previewBodyText:[" << mailList[i].previewBodyText << "]");
498 LogDebug("mailList[i].flags_seen_field:[" << mailList[i].flags_seen_field << "]");
499 LogDebug("mailList[i].priority:[" << mailList[i].priority<< "]");
501 emailResultVector.push_back(msg);
505 if (mailList != NULL) {
510 return emailResultVector;
513 vector<IMessagePtr> Messaging::findMessages(const Api::Tizen::FilterPtr& filter){
516 vector<IMessagePtr> retVal;
517 std::string filterSql;
519 MessageQueryGeneratorPtr queryGenerator(new MessageQueryGenerator());
521 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
522 filter->travel(filterVisitor, 0);
523 filterSql = queryGenerator->getQuery();
524 LogDebug("filterSql:[" << filterSql <<"]");
526 int messageType = queryGenerator->getMessageType();
528 case Api::Messaging::EMAIL:
529 LogDebug("message type is EMAIL:[" << messageType <<"]");
530 retVal = queryEmailMessage(filterSql);
533 case Api::Messaging::SMS:
534 case Api::Messaging::MMS:
535 LogDebug("message type is SMS or MMS:[" << messageType <<"]");
536 queryGenerator->reset(MessageQueryGenerator::MODE_SMS_MMS);
537 filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
538 filter->travel(filterVisitor);
539 filterSql = queryGenerator->getQuery();
540 retVal = querySmsMmsMessages(filterSql);
544 LogError("[ERROR] >>> invlid message type:[" << messageType <<"]");
553 vector<IMessagePtr> Messaging::findMessages(const Api::Tizen::FilterPtr& filter, const Api::Tizen::SortModePtr& sortMode, const long limit, const long offset)
557 vector<IMessagePtr> retVal;
558 std::string filterSql;
560 MessageQueryGeneratorPtr queryGenerator(new MessageQueryGenerator(sortMode, limit, offset));
562 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
563 filter->travel(filterVisitor, 0);
564 filterSql = queryGenerator->getQuery();
565 LogDebug("filterSql:[" << filterSql <<"]");
567 int messageType = queryGenerator->getMessageType();
569 case Api::Messaging::EMAIL:
570 LogDebug("message type is EMAIL:[" << messageType <<"]");
571 retVal = queryEmailMessage(filterSql);
574 case Api::Messaging::SMS:
575 case Api::Messaging::MMS:
576 LogDebug("message type is SMS or MMS:[" << messageType <<"]");
577 queryGenerator->reset(MessageQueryGenerator::MODE_SMS_MMS);
578 filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
579 filter->travel(filterVisitor);
580 filterSql = queryGenerator->getQuery();
581 retVal = querySmsMmsMessages(filterSql);
585 MsgLogError("[ERROR] >>> invlid message type:[" << messageType <<"]");
593 vector<string> Messaging::getMessageIds(MessageType msgType,
598 return getSmsIds(folder);
600 return getMmsIds(folder);
602 return getEmailIds(folder);
604 LogError("not supported message type");
605 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
609 vector<string> Messaging::getSmsIds(FolderType folder)
611 vector<string> retVal;
612 msg_message_t msg = msg_new_message();
613 MSG_LIST_S folder_list_view = { 0, NULL };
617 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
618 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
619 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
620 res = msg_get_folder_view_list(
621 MsgGetCommonHandle(), platformFolder, &sort_rules,
624 if (MSG_SUCCESS != res) {
625 LogError("msg_Get_folder_view_list failed" << res);
626 Throw(WrtDeviceApis::Commons::PlatformException);
629 for (int i = 0; i < folder_list_view.nCount; i++) {
631 msg_get_message_type(folder_list_view.
633 int l_msgId = msg_get_message_id(
634 folder_list_view.msgInfo[i]);
635 ostringstream stream;
637 retVal.push_back(stream.str());
641 msg_release_message_list(&folder_list_view);
644 Catch(WrtDeviceApis::Commons::PlatformException) {
645 LogError("Problem with message creation, cleaning");
646 if (folder_list_view.nCount) {
647 msg_release_message_list(&folder_list_view);
650 msg_release_message(&msg);
658 vector<string> Messaging::getMmsIds(FolderType folder)
660 vector<string> retVal;
661 msg_message_t msg = msg_new_message();
662 MSG_LIST_S folder_list_view = { 0, NULL };
666 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
667 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
668 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
669 res = msg_get_folder_view_list(
670 MsgGetCommonHandle(), platformFolder, &sort_rules,
673 if (MSG_SUCCESS != res) {
674 LogError("msg_Get_folder_view_list failed" << res);
675 Throw(WrtDeviceApis::Commons::PlatformException);
678 for (int i = 0; i < folder_list_view.nCount; i++) {
680 msg_get_message_type(folder_list_view.
682 int l_msgId = msg_get_message_id(
683 folder_list_view.msgInfo[i]);
684 ostringstream stream;
686 retVal.push_back(stream.str());
690 msg_release_message_list(&folder_list_view);
693 Catch(WrtDeviceApis::Commons::PlatformException) {
694 LogError("Problem with message creation, cleaning");
695 if (folder_list_view.nCount) {
696 msg_release_message_list(&folder_list_view);
699 msg_release_message(&msg);
707 vector<string> Messaging::getEmailIds(FolderType folder)
709 vector<string> retVal;
713 Throw(WrtDeviceApis::Commons::UnknownException);
718 vector<EmailAccountInfo> Messaging::getEmailAccounts() const
720 emf_account_t* accounts = NULL;
723 if (!email_get_account_list(&accounts, &count)) {
724 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
725 "Couldn't get e-mail accounts.");
729 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No e-mail account found.");
732 vector<Api::Messaging::EmailAccountInfo> result;
733 for (int i = 0; i < count; ++i) {
734 Api::Messaging::EmailAccountInfo account(accounts[i].account_id,
735 accounts[i].user_name,
736 accounts[i].email_addr);
737 result.push_back(account);
740 if (accounts != NULL) {
741 email_free_account(&accounts, count);
746 Catch(WrtDeviceApis::Commons::PlatformException) {
747 if (accounts != NULL) {
748 email_free_account(&accounts, count);
754 int Messaging::getEmailAccountId(const std::string& account)
757 string tmpAccount = account;
758 emf_account_t *pAccountArray = NULL;
763 if (account.empty()) {
764 tmpAccount = getCurrentEmailAccount().getAddress();
765 if (tmpAccount.empty()) {
767 "current email account is not set, possible that no account created");
768 Throw(WrtDeviceApis::Commons::PlatformException);
772 if (!email_get_account_list(&pAccountArray, &iCount)) {
773 LogError("email_get_account_list error");
774 Throw(WrtDeviceApis::Commons::PlatformException);
778 LogError("no email account exist");
779 Throw(WrtDeviceApis::Commons::PlatformException);
782 for (int i = 0; i < iCount; i++) {
783 string tmp = pAccountArray[i].email_addr;
784 if (tmp == tmpAccount) {
785 m_currentEmailAccountId = pAccountArray[i].account_id;
786 retVal = m_currentEmailAccountId;
791 if (0 == m_currentEmailAccountId) {
792 LogError("wrong email account ID");
793 Throw(WrtDeviceApis::Commons::PlatformException);
796 if (pAccountArray != NULL) {
797 LogDebug("free account, ptr=" << pAccountArray);
798 email_free_account(&pAccountArray, iCount);
802 LogError("no email account created");
803 Throw(WrtDeviceApis::Commons::PlatformException);
807 Catch(WrtDeviceApis::Commons::PlatformException) {
808 LogError("exception catch, platform exception");
809 if (pAccountArray != NULL) {
810 email_free_account(&pAccountArray, iCount);
818 void Messaging::fetchEmailHeaders()
820 emf_mailbox_t *mailbox = NULL;
822 mailbox = static_cast<emf_mailbox_t*>(calloc(sizeof (emf_mailbox_t), 1));
824 LogError("calloc failed");
827 mailbox->account_id = m_currentEmailAccountId;
828 mailbox->name = strdup(EMF_INBOX_NAME);
829 if (EMF_ERROR_NONE != email_sync_header(mailbox, &handle)) {
830 LogError("email_sync_header failed");
832 email_free_mailbox(&mailbox, 1);
835 int Messaging::convertFolderToPlatform(const FolderType folder)
837 MSG_FOLDER_ID_T platfromFolderId;
840 platfromFolderId = MSG_INBOX_ID;
843 platfromFolderId = MSG_DRAFT_ID;
846 platfromFolderId = MSG_OUTBOX_ID;
849 platfromFolderId = MSG_SENTBOX_ID;
852 // intentionally not break in platform is no spambox
854 LogError("Invalid folder: " << folder);
855 Throw(WrtDeviceApis::Commons::PlatformException);
858 return platfromFolderId;
861 int Messaging::convertFolderToPlatform(const std::string &folder)
863 MSG_FOLDER_LIST_S folderList = { 0, NULL };
868 msg_get_folder_list(MsgGetCommonHandle(), &folderList)) {
869 LogError("msg_get_folder_list error");
870 Throw(WrtDeviceApis::Commons::PlatformException);
872 for (int i = 0; i < folderList.nCount; ++i) {
873 if (MSG_FOLDER_TYPE_USER_DEF ==
874 folderList.folderInfo[i].folderType &&
875 NULL != folderList.folderInfo[i].folderName &&
876 folder == folderList.folderInfo[i].folderName) {
877 result = folderList.folderInfo[i].folderId;
882 Catch(WrtDeviceApis::Commons::PlatformException) {
883 if (folderList.nCount) {
884 msg_release_folder_list(&folderList);
888 if (folderList.nCount) {
889 msg_release_folder_list(&folderList);
894 void Messaging::addOnMessageReceived(
895 const Api::Messaging::EmitterMessageReceivedPtr& emitter, const Api::Tizen::FilterPtr& filter, int funtionIndex)
903 LogDebug("funtionIndex = " << funtionIndex);
904 bool isValidFilter = validateFilter(filter, funtionIndex);
905 if(isValidFilter == false){
906 LogError("[ERROR]this filter is invalid");
907 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
910 Catch(WrtDeviceApis::Commons::PlatformException) {
915 LogDebug("filter is NULL");
919 m_onMessageReceived.attach(emitter);
920 EmittersMessageReceived::LockType lock = m_onMessageReceived.getLock();
921 if ((m_onMessageReceived.size() == 1)&&((m_onConversationReceived.size() + m_onFolderReceived.size()) == 0)) {
922 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(
923 new MsgServiceHandleMgr());
926 err = msg_reg_storage_change_callback(
927 m_onMessageReceivedHandleMgr->getHandle(),
928 onMessageStorageChanged,
931 if (err != MSG_SUCCESS) {
932 LogError("Couldn't register on MMS received callback, err=" << err);
935 m_dbusConnection->open(DBUS_BUS_SYSTEM);
939 void Messaging::addOnMessageReceived(
940 const Api::Messaging::EmitterConversationReceivedPtr& emitter, const Api::Tizen::FilterPtr& filter, int funtionIndex)
948 LogDebug("funtionIndex = " << funtionIndex);
949 bool isValidFilter = validateFilter(filter, funtionIndex);
950 if(isValidFilter == false){
951 LogError("[ERROR]this filter is invalid");
952 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
955 Catch(WrtDeviceApis::Commons::PlatformException) {
960 LogDebug("filter is NULL");
964 m_onConversationReceived.attach(emitter);
965 EmittersConversationReceived::LockType lock = m_onConversationReceived.getLock();
966 if ((m_onConversationReceived.size() == 1)&&((m_onMessageReceived.size() + m_onFolderReceived.size()) == 0)) {
967 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(
968 new MsgServiceHandleMgr());
971 err = msg_reg_storage_change_callback(
972 m_onMessageReceivedHandleMgr->getHandle(),
973 onMessageStorageChanged,
976 if (err != MSG_SUCCESS) {
977 LogError("Couldn't register on MMS received callback, err=" << err);
980 m_dbusConnection->open(DBUS_BUS_SYSTEM);
984 void Messaging::addOnMessageReceived(
985 const Api::Messaging::EmitterFolderReceivedPtr& emitter, const Api::Tizen::FilterPtr& filter, int funtionIndex)
993 LogDebug("funtionIndex = " << funtionIndex);
994 bool isValidFilter = validateFilter(filter, funtionIndex);
995 if(isValidFilter == false){
996 LogError("[ERROR]this filter is invalid");
997 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
1000 Catch(WrtDeviceApis::Commons::PlatformException) {
1005 LogDebug("filter is NULL");
1009 m_onFolderReceived.attach(emitter);
1010 EmittersFolderReceived::LockType lock = m_onFolderReceived.getLock();
1011 if ((m_onFolderReceived.size() == 1)&&((m_onMessageReceived.size() + m_onConversationReceived.size()) == 0)) {
1012 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(
1013 new MsgServiceHandleMgr());
1016 err = msg_reg_storage_change_callback(
1017 m_onMessageReceivedHandleMgr->getHandle(),
1018 onMessageStorageChanged,
1021 if (err != MSG_SUCCESS) {
1022 LogError("Couldn't register on MMS received callback, err=" << err);
1025 m_dbusConnection->open(DBUS_BUS_SYSTEM);
1029 bool Messaging::validateFilter(const Api::Tizen::FilterPtr& filter, const int funtionIndex){
1032 bool retBool = false;
1033 LogDebug("funtionIndex = " << funtionIndex);
1036 if(funtionIndex == 0)
1038 Platform::Messaging::StorageChangesMessageFilterValidatorPtr validatorMsg =
1039 Platform::Messaging::StorageChangesMessageFilterValidatorFactory::getStorageChangesMessageFilterValidator();
1041 FilterValidatorPtr filterValidatorMsg = DPL::StaticPointerCast<FilterValidator>(validatorMsg);
1042 retBool = filter->validate(filterValidatorMsg);
1043 }else if(funtionIndex == 1)
1045 Platform::Messaging::StorageChangesConversationFilterValidatorPtr validatorConv =
1046 Platform::Messaging::StorageChangesConversationFilterValidatorFactory::getStorageChangesConversationFilterValidator();
1048 FilterValidatorPtr filterValidatorConv = DPL::StaticPointerCast<FilterValidator>(validatorConv);
1049 retBool = filter->validate(filterValidatorConv);
1050 }else if(funtionIndex == 2)
1052 Platform::Messaging::StorageChangesFolderFilterValidatorPtr validatorFolder =
1053 Platform::Messaging::StorageChangesFolderFilterValidatorFactory::getStorageChangesFolderFilterValidator();
1055 FilterValidatorPtr filterValidatorFolder = DPL::StaticPointerCast<FilterValidator>(validatorFolder);
1056 retBool = filter->validate(filterValidatorFolder);
1059 if(retBool == false){
1060 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
1063 LogDebug("filter is NULL");
1067 LogDebug(">>> retBool:" << retBool);
1073 void Messaging::removeOnMessageMsgReceived(Api::Messaging::EmitterMessageReceived::IdType id)
1076 m_onMessageReceived.detach(id);
1077 EmittersMessageReceived::LockType lock = m_onMessageReceived.getLock();
1078 if ((m_onMessageReceived.size()+m_onConversationReceived.size()+m_onFolderReceived.size()) == 0) {
1079 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(NULL);
1080 m_dbusConnection->close();
1084 void Messaging::removeOnMessageConvReceived(Api::Messaging::EmitterConversationReceived::IdType id)
1087 m_onConversationReceived.detach(id);
1088 EmittersConversationReceived::LockType lock = m_onConversationReceived.getLock();
1089 if ((m_onMessageReceived.size()+m_onConversationReceived.size()+m_onFolderReceived.size()) == 0) {
1090 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(NULL);
1091 m_dbusConnection->close();
1095 void Messaging::removeOnMessageFolderReceived(Api::Messaging::EmitterFolderReceived::IdType id)
1098 m_onFolderReceived.detach(id);
1099 EmittersFolderReceived::LockType lock = m_onFolderReceived.getLock();
1100 if ((m_onMessageReceived.size()+m_onConversationReceived.size()+m_onFolderReceived.size()) == 0) {
1101 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(NULL);
1102 m_dbusConnection->close();
1106 void Messaging::OnEventReceived(const DBus::MessageEvent& event)
1109 EventMessageReceivedPtr jsEvent(new EventMessageReceived());
1110 DBus::MessagePtr message = event.GetArg0();
1114 jsEvent->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::PlatformException);
1115 m_onMessageReceived.emit(jsEvent);
1117 if (message->getInterface() == DBUS_INTERFACE_EMAIL_RECEIVED) {
1118 int account_id = 0, mail_id = 0, thread_id = 0, status = 0;
1120 emf_mailbox_t m_mailboxes;
1121 emf_mailbox_t* mailboxes = NULL;
1123 DBus::Message::ReadIterator it = message->getReadIterator();
1124 for (int i = 0; it->isValid(); it->next(), ++i) {
1125 if ((i == 0) && (it->getArgType() == DBUS_TYPE_INT32)) {
1126 status = it->getInt();
1127 LogInfo("status: " << status);
1128 } else if ((i == 1) && (it->getArgType() == DBUS_TYPE_INT32)) {
1129 account_id = it->getInt();
1130 LogInfo("account_id: " << account_id);
1131 } else if ((i == 2) && (it->getArgType() == DBUS_TYPE_INT32)) {
1132 mail_id = it->getInt();
1133 LogInfo("mail_id: " << mail_id);
1134 } else if ((i == 3) && (it->getArgType() == DBUS_TYPE_STRING)) {
1135 name = it->getString();
1136 LogInfo("name: " << name);
1137 } else if ((i == 4) && (it->getArgType() == DBUS_TYPE_INT32)) {
1138 thread_id = it->getInt();
1139 LogInfo("thread_id: " << thread_id);
1143 if ((mail_id > 0) && (NOTI_MAIL_ADD == status))
1144 { // TODO also RECEIVE_THREAD_ITEM?
1145 LogInfo("Email received. mail_id: " << mail_id);
1147 IMessagePtr msg = MessageFactory::createMessage(EMAIL, mail_id);
1149 // TODO Temporary fix: ignore messages from OUTBOX, SENTBOX & DRAFTBOX -> most probably we added them
1150 Api::Messaging::FolderType folder = msg->getCurrentFolder();
1151 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1153 if (OUTBOX != folder && SENTBOX != folder && DRAFTBOX !=
1155 IEmailPtr email = MessageFactory::convertToEmail(msg);
1156 jsEvent->setMessage(msg);
1157 if(m_onMessageReceived.size() > 0){
1158 m_onMessageReceived.emit(jsEvent);
1161 if(m_onConversationReceived.size() > 0){
1162 m_onConversationReceived.emit(jsEvent);
1167 "New email message in ignored folder: " << folder);
1170 else if ((mail_id > 0) && (NOTI_MAIL_DELETE == status))
1171 { // TODO also RECEIVE_THREAD_ITEM?
1172 LogInfo("Email received. delete type: " << mail_id);
1174 std::vector<std::string> strIds = String::split(name, ',');
1176 std::stringstream stream;
1177 for (std::vector<std::string>::iterator it = strIds.begin(); it != strIds.end(); ++it) {
1178 LogDebug("ID is :" << *it);
1180 if ( (*it).length() > 0 ) //vaild id
1186 IMessagePtr msg = MessageFactory::createMessage(EMAIL, id);
1188 Api::Messaging::FolderType folder = msg->getCurrentFolder();
1189 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_DELETED);
1191 if (true) { // if (OUTBOX != folder && SENTBOX != folder && DRAFTBOX != folder)
1192 IEmailPtr email = MessageFactory::convertToEmail(msg);
1193 jsEvent->setMessage(msg);
1194 if(m_onMessageReceived.size() > 0){
1195 m_onMessageReceived.emit(jsEvent);
1197 if(m_onConversationReceived.size() > 0){
1198 m_onConversationReceived.emit(jsEvent);
1202 "New email message in ignored folder: " << folder);
1210 else if ((mail_id > 0) && (NOTI_MAIL_UPDATE == status))
1211 { // TODO also RECEIVE_THREAD_ITEM?
1212 LogInfo("Email received. mail_id: " << mail_id);
1214 IMessagePtr msg = MessageFactory::createMessage(EMAIL, mail_id);
1216 Api::Messaging::FolderType folder = msg->getCurrentFolder();
1217 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_UPDATED);
1219 if (true) { // if (OUTBOX != folder && SENTBOX != folder && DRAFTBOX != folder)
1220 IEmailPtr email = MessageFactory::convertToEmail(msg);
1221 jsEvent->setMessage(msg);
1222 if(m_onMessageReceived.size() > 0)
1223 m_onMessageReceived.emit(jsEvent);
1224 if(m_onConversationReceived.size() > 0)
1225 m_onConversationReceived.emit(jsEvent);
1228 "New email message in ignored folder: " << folder);
1231 else if (NOTI_MAILBOX_ADD == status)
1233 LogInfo("Emailbox received. account Id: " << account_id);
1234 LogInfo("name Id: " << name);
1235 email_get_mailbox_by_name(account_id, name.c_str(), &mailboxes);
1237 m_mailboxes = *mailboxes;
1239 Api::Messaging::IMessageFolderPtr folderPtr(new MessageFolder(m_mailboxes));
1240 jsEvent->setMessageFolder(folderPtr);
1241 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1242 if(m_onFolderReceived.size() > 0)
1244 m_onFolderReceived.emit(jsEvent);
1248 else if (NOTI_MAILBOX_UPDATE == status)
1250 LogInfo("Emailbox received. account Id: " << account_id);
1251 LogInfo("name Id: " << name);
1252 email_get_mailbox_by_name(account_id, name.c_str(), &mailboxes);
1254 m_mailboxes = *mailboxes;
1256 Api::Messaging::IMessageFolderPtr folderPtr(new MessageFolder(m_mailboxes));
1257 jsEvent->setMessageFolder(folderPtr);
1258 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_UPDATED);
1260 if(m_onFolderReceived.size() > 0)
1262 m_onFolderReceived.emit(jsEvent);
1267 else if (NOTI_MAILBOX_DELETE == status)
1269 LogInfo("Emailbox received. account Id: " << account_id);
1270 LogInfo("name Id: " << name);
1271 email_get_mailbox_by_name(account_id, name.c_str(), &mailboxes);
1273 m_mailboxes = *mailboxes;
1275 Api::Messaging::IMessageFolderPtr folderPtr(new MessageFolder(m_mailboxes));
1276 jsEvent->setMessageFolder(folderPtr);
1277 jsEvent->setMsg_Event_Type(EventMessageReceived::MSG_DELETED);
1279 if(m_onFolderReceived.size() > 0)
1281 m_onFolderReceived.emit(jsEvent);
1287 LogError("Couldn't retrieve message or it has been malformed.");
1290 LogDebug("Wrong DBus interface, skipping it.");
1295 void Messaging::onMessageStorageChanged(MSG_HANDLE_T handle,
1296 MSG_STORAGE_CHANGE_TYPE_T storageChangeType,
1297 MSG_MSGID_LIST_S *pMsgIdList,
1303 MSG_ERROR_T err = MSG_SUCCESS;
1305 msg_message_t msg = msg_new_message();
1306 MSG_SENDINGOPT_S sendOpt = {0, };
1308 err = msg_get_message(handle, *pMsgIdList->msgIdList, msg, &sendOpt);
1310 LogDebug("err" << err);
1313 if (err != MSG_SUCCESS)
1315 LogDebug("Get Message Failed!");
1316 msg_release_message(&msg);
1320 Messaging* this_ = static_cast<Messaging*>(data);
1323 int MsgType = msg_get_message_type(msg);
1325 if((MsgType > MSG_TYPE_INVALID) && ( MsgType <= MSG_TYPE_SMS_REJECT))
1327 if(msg_get_message_type(msg) != MSG_TYPE_MMS_NOTI)
1329 IMessagePtr message = MessageFactory::createMessage(
1333 ISmsPtr sms = MessageFactory::convertToSms(message);
1334 IConversationPtr conversation(new Conversation(message->getId(), SMS));
1335 EventMessageReceivedPtr event(new EventMessageReceived());
1336 event->setMessage(message);
1337 event->setConversation(conversation);
1339 if(storageChangeType == MSG_STORAGE_CHANGE_INSERT)
1341 event->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1343 else if(storageChangeType == MSG_STORAGE_CHANGE_UPDATE)
1345 event->setMsg_Event_Type(EventMessageReceived::MSG_UPDATED);
1347 else if(storageChangeType == MSG_STORAGE_CHANGE_DELETE)
1349 event->setMsg_Event_Type(EventMessageReceived::MSG_DELETED);
1351 else if(storageChangeType == MSG_STORAGE_CHANGE_CONTACT)
1353 event->setMsg_Event_Type(EventMessageReceived::MSG_MULTIPLE);
1356 if(this_->m_onMessageReceived.size() > 0){
1357 this_->m_onMessageReceived.emit(event);
1359 if(this_->m_onConversationReceived.size() > 0){
1360 this_->m_onConversationReceived.emit(event);
1365 LogError("Ignore this sms, this is mms noti.");
1368 else if((MsgType >= MSG_TYPE_MMS) && (MsgType <= MSG_TYPE_MMS_NOTI))
1370 if(msg_get_message_type(msg) != MSG_TYPE_MMS_NOTI)
1372 IMessagePtr message = MessageFactory::createMessage(
1376 IMmsPtr mms = MessageFactory::convertToMms(message);
1377 IConversationPtr conversation(new Conversation(message->getId(), MMS));
1378 EventMessageReceivedPtr event(new EventMessageReceived());
1379 event->setMessage(message);
1380 event->setConversation(conversation);
1382 if(storageChangeType == MSG_STORAGE_CHANGE_INSERT)
1384 event->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1386 else if(storageChangeType == MSG_STORAGE_CHANGE_UPDATE)
1388 event->setMsg_Event_Type(EventMessageReceived::MSG_UPDATED);
1390 else if(storageChangeType == MSG_STORAGE_CHANGE_DELETE)
1392 event->setMsg_Event_Type(EventMessageReceived::MSG_DELETED);
1394 else if(storageChangeType == MSG_STORAGE_CHANGE_CONTACT)
1396 event->setMsg_Event_Type(EventMessageReceived::MSG_MULTIPLE);
1398 if(this_->m_onMessageReceived.size() > 0){
1399 this_->m_onMessageReceived.emit(event);
1401 if(this_->m_onConversationReceived.size() > 0){
1402 this_->m_onConversationReceived.emit(event);
1407 LogError("Ignore this mms, this is mms noti.");
1412 Catch(WrtDeviceApis::Commons::ConversionException) {
1413 LogError("Couldn't convert message to sms.");
1419 void Messaging::onSmsReceived(MSG_HANDLE_T handle,
1424 Messaging* this_ = static_cast<Messaging*>(data);
1427 if(msg_get_message_type(msg) != MSG_TYPE_MMS_NOTI)
1429 IMessagePtr message = MessageFactory::createMessage(
1433 ISmsPtr sms = MessageFactory::convertToSms(message);
1434 IConversationPtr conversation(new Conversation(message->getId(), SMS));
1436 EventMessageReceivedPtr event(new EventMessageReceived());
1437 event->setMessage(message);
1438 event->setConversation(conversation);
1439 event->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1440 this_->m_onMessageReceived.emit(event);
1444 LogError("Ignore this sms, this is mms noti.");
1447 Catch(WrtDeviceApis::Commons::ConversionException) {
1448 LogError("Couldn't convert message to sms.");
1453 void Messaging::onMmsReceived(MSG_HANDLE_T handle,
1458 Messaging* this_ = static_cast<Messaging*>(data);
1461 IMessagePtr message = MessageFactory::createMessage(
1465 IMmsPtr mms = MessageFactory::convertToMms(message);
1466 IConversationPtr conversation(new Conversation(message->getId(), MMS));
1467 EventMessageReceivedPtr event(new EventMessageReceived());
1468 event->setMessage(message);
1469 event->setConversation(conversation);
1470 event->setMsg_Event_Type(EventMessageReceived::MSG_ADDED);
1471 this_->m_onMessageReceived.emit(event);
1473 Catch(WrtDeviceApis::Commons::ConversionException) {
1474 LogError("Couldn't convert message to mms.");
1479 void Messaging::getNumberOfEmails(Api::Messaging::FolderType folder,
1485 emf_mailbox_t mailbox = {};
1486 mailbox.account_id = 0; // means for all accounts
1487 mailbox.name = String::strdup(
1488 EmailConverter::toMailboxName(folder)
1491 int error = email_count_message(&mailbox, read, unread);
1494 if (EMF_ERROR_NONE != error) {
1495 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1496 "Couldn't get number of emails. [" << error << "]");
1502 void Messaging::getNumberOfSms(Api::Messaging::FolderType folder,
1506 getNumberOfSmsMms(folder, read, unread, SMS);
1509 void Messaging::getNumberOfMms(Api::Messaging::FolderType folder,
1513 getNumberOfSmsMms(folder, read, unread, MMS);
1516 void Messaging::getNumberOfSmsMms(Api::Messaging::FolderType folder,
1519 Api::Messaging::MessageType msgType)
1521 MSG_LIST_S folderListView = { 0, NULL };
1528 MSG_MESSAGE_TYPE_T msgTypePlatform;
1529 if (SMS == msgType) {
1530 msgTypePlatform = MSG_TYPE_SMS;
1531 } else if (MMS == msgType) {
1532 msgTypePlatform = MSG_TYPE_MMS;
1534 LogError("no supported message type in this method");
1535 Throw(WrtDeviceApis::Commons::PlatformException);
1538 MSG_FOLDER_ID_T msgFolderId;
1541 msgFolderId = MSG_INBOX_ID;
1544 msgFolderId = MSG_OUTBOX_ID;
1547 msgFolderId = MSG_SPAMBOX_ID;
1550 msgFolderId = MSG_SENTBOX_ID;
1553 msgFolderId = MSG_DRAFT_ID;
1556 LogError("wrong folder type");
1557 Throw(WrtDeviceApis::Commons::PlatformException);
1561 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
1562 if (msg_get_folder_view_list(MsgGetCommonHandle(), msgFolderId,
1564 &folderListView) != MSG_SUCCESS) {
1566 "msg_get_folder_view_list empty or failed, msgFolderId=" <<
1568 Throw(WrtDeviceApis::Commons::PlatformException);
1570 // go thtough all message to check type of message
1571 LogDebug("msgCount=" << (int) folderListView.nCount);
1572 for (int msg_cnt = 0; msg_cnt < folderListView.nCount; msg_cnt++) {
1573 LogDebug("msgMainType=" <<
1574 (int) msg_get_message_type(folderListView.
1577 ", searching for = " << (int) msgTypePlatform);
1578 if (msgTypePlatform ==
1579 msg_get_message_type(folderListView.msgInfo[
1581 if (msg_is_read(folderListView.msgInfo[msg_cnt]))
1589 msg_release_message_list(&folderListView);
1591 LogDebug("readed=" << *read << ", unReaded=" << *unread);
1594 Catch(WrtDeviceApis::Commons::PlatformException) {
1595 if (folderListView.nCount) {
1596 msg_release_message_list(&folderListView);
1602 vector<Api::Messaging::IMessagePtr> Messaging::findSms(Api::Messaging::FolderType folder, const Api::Tizen::FilterPtr& filter)
1606 vector<Api::Messaging::IMessagePtr> retVal;
1607 msg_message_t msg = msg_new_message();
1608 MSG_LIST_S folder_list_view = {0, NULL};
1611 const MSG_SORT_RULE_S sort_rules = {MSG_SORT_BY_DISPLAY_TIME, true};
1612 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
1613 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
1614 res = msg_get_folder_view_list(MsgGetCommonHandle(), platformFolder, &sort_rules, &folder_list_view);
1616 if (MSG_SUCCESS != res) {
1617 LogError("msg_get_folder_view_list failed " << res);
1618 Throw(WrtDeviceApis::Commons::PlatformException);
1621 for (int i = 0; i < folder_list_view.nCount; i++) {
1622 if (MSG_TYPE_SMS == msg_get_message_type(folder_list_view.msgInfo[i])) {
1623 int l_msgId = msg_get_message_id(folder_list_view.msgInfo[i]);
1624 Api::Messaging::IMessagePtr msg = MessageFactory::createMessage(SMS, l_msgId);
1626 // if (!filter || !filter->isValid() ||
1627 // filter->compare(MessageFactory::convertToSms(msg))) {
1628 retVal.push_back(msg);
1633 msg_release_message_list(&folder_list_view);
1634 }Catch(WrtDeviceApis::Commons::PlatformException) {
1635 LogError("Problem with message creation, cleaning");
1636 if (folder_list_view.nCount) {
1637 msg_release_message_list(&folder_list_view);
1640 msg_release_message(&msg);
1649 vector<Api::Messaging::IMessagePtr> Messaging::findMms(Api::Messaging::FolderType folder,
1650 const Api::Tizen::FilterPtr& filter)
1652 vector<Api::Messaging::IMessagePtr> retVal;
1653 msg_message_t msg = msg_new_message();
1654 MSG_LIST_S folder_list_view = {0, NULL};
1658 const MSG_SORT_RULE_S sort_rules = {MSG_SORT_BY_DISPLAY_TIME, true};
1659 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
1660 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
1661 res = msg_get_folder_view_list(
1662 MsgGetCommonHandle(), platformFolder, &sort_rules,
1665 if (MSG_SUCCESS != res) {
1666 LogError("msg_Get_folder_view_list failed" << res);
1667 Throw(WrtDeviceApis::Commons::PlatformException);
1670 for (int i = 0; i < folder_list_view.nCount; i++) {
1672 msg_get_message_type(folder_list_view.
1674 int l_msgId = msg_get_message_id(
1675 folder_list_view.msgInfo[i]);
1676 Api::Messaging::IMessagePtr msg = MessageFactory::createMessage(MMS, l_msgId);
1680 msg_release_message_list(&folder_list_view);
1683 Catch(WrtDeviceApis::Commons::PlatformException) {
1684 LogError("Problem with message creation, cleaning");
1685 if (folder_list_view.nCount) {
1686 msg_release_message_list(&folder_list_view);
1689 msg_release_message(&msg);
1697 void Messaging::createFolder(MessageType msgType,
1698 const string& userFolder)
1701 case Api::Messaging::SMS:
1702 case Api::Messaging::MMS:
1703 createMsgServiceFolder(userFolder);
1705 case Api::Messaging::EMAIL:
1706 createEmailFolder(userFolder);
1709 LogError("msg not supported");
1710 Throw(WrtDeviceApis::Commons::UnknownException);
1714 void Messaging::createMsgServiceFolder(const std::string& userFolder)
1716 if (userFolder.length() > MAX_FOLDER_NAME_SIZE) {
1717 LogError("folder name to long");
1718 Throw(WrtDeviceApis::Commons::PlatformException);
1721 MSG_FOLDER_INFO_S folderInfo;
1723 folderInfo.folderId = 0;
1724 strncpy(folderInfo.folderName, userFolder.c_str(), userFolder.length());
1726 folderInfo.folderType = MSG_FOLDER_TYPE_USER_DEF;
1728 if (msg_add_folder(MsgGetCommonHandle(), &folderInfo) != MSG_SUCCESS) {
1729 LogError("msg_add_folder failed");
1730 Throw(WrtDeviceApis::Commons::PlatformException);
1734 void Messaging::createEmailFolder(const std::string& userFolder)
1738 Throw(WrtDeviceApis::Commons::UnknownException);
1741 void Messaging::deleteFolder(MessageType msgType,
1742 const string& userFolder)
1745 case Api::Messaging::SMS:
1746 case Api::Messaging::MMS:
1747 deleteMsgServiceFolder(userFolder);
1749 case Api::Messaging::EMAIL:
1750 deleteEmailFolder(userFolder);
1753 LogError("msg not supported");
1754 Throw(WrtDeviceApis::Commons::UnknownException);
1758 void Messaging::deleteMsgServiceFolder(const string& userFolder)
1762 int platformFolderId = convertFolderToPlatform(userFolder);
1764 msg_delete_folder(MsgGetCommonHandle(), platformFolderId)) {
1765 LogError("msg_get_folder_list failed");
1766 Throw(WrtDeviceApis::Commons::PlatformException);
1769 Catch(WrtDeviceApis::Commons::PlatformException) {
1774 void Messaging::deleteEmailFolder(const string& userFolder)
1778 Throw(WrtDeviceApis::Commons::UnknownException);
1781 vector<string> Messaging::getFolderNames(MessageType msgType)
1784 case Api::Messaging::SMS:
1785 case Api::Messaging::MMS:
1786 return getFolderNamesMsgService();
1787 case Api::Messaging::EMAIL:
1788 return getFolderNamesEmail();
1790 LogError("msg not supported");
1791 Throw(WrtDeviceApis::Commons::UnknownException);
1795 vector<string> Messaging::getFolderNamesMsgService()
1797 vector<string> retVal;
1798 MSG_FOLDER_LIST_S msgFolderList = { 0, NULL }; // output data
1802 if (msg_get_folder_list(MsgGetCommonHandle(),
1803 &msgFolderList) != MSG_SUCCESS) {
1804 LogError("msg_get_folder_list failed");
1805 Throw(WrtDeviceApis::Commons::PlatformException);
1808 LogDebug("number of folder=" << msgFolderList.nCount);
1809 for (int i = 0; i < msgFolderList.nCount; i++) {
1810 LogDebug("folderName=" << msgFolderList.folderInfo[i].folderName);
1811 LogDebug("folderId=" << (int) msgFolderList.folderInfo[i].folderId);
1814 (int) msgFolderList.folderInfo[i].folderType);
1815 retVal.push_back(msgFolderList.folderInfo[i].folderName);
1817 (void) msg_release_folder_list(&msgFolderList);
1820 Catch(WrtDeviceApis::Commons::PlatformException) {
1821 if (msgFolderList.nCount) {
1822 (void) msg_release_folder_list(&msgFolderList);
1829 vector<string> Messaging::getFolderNamesEmail()
1831 vector<string> retVal;
1834 emf_mailbox_t* mailboxes = NULL;
1836 string emailAccountName;
1837 Api::Messaging::EmailAccountInfo account = getCurrentEmailAccount();
1841 if (!email_get_mailbox_list(account.getIntId(), -1, &mailboxes,
1842 &mailboxesCount) || !mailboxes) {
1843 LogError("error during get mailboxes");
1845 for (int i = 0; i < mailboxesCount; i++) {
1846 retVal.push_back(mailboxes->name);
1847 LogDebug("mailbox found, name=" << mailboxes->name);
1852 Catch(WrtDeviceApis::Commons::PlatformException) {
1853 // nothing to clean, re-throw exception
1860 vector<Api::Messaging::IMessagePtr> Messaging::findEmail(const std::string &folder,
1861 const Api::Tizen::FilterPtr& filter)
1863 vector<Api::Messaging::IMessagePtr> retVal;
1865 Api::Messaging::EmailAccountInfo account = getCurrentEmailAccount();
1866 if (account.getId().empty()) {
1867 LogWarning("No valid email accounts. Skipping");
1871 int accountId = account.getIntId();
1873 // number of found emails
1879 emf_mail_list_item_t* results = NULL;
1880 int err = email_get_mail_list_ex(accountId,
1882 EMF_LIST_TYPE_NORMAL,
1885 EMF_SORT_DATETIME_HIGH,
1888 DPL::ScopedFree<emf_mail_list_item_t> autoFree(results);
1890 if (EMF_ERROR_MAIL_NOT_FOUND == err || results == NULL) {
1891 LogWarning("No emails found in mailbox: " << folder);
1893 } else if (EMF_ERROR_NONE != err) {
1895 "Unable to get mail list for mailbox: " << folder <<
1896 ", Error: " << err);
1897 Throw(WrtDeviceApis::Commons::PlatformException);
1901 for (int i = 0; i < count; ++i) {
1902 Api::Messaging::IMessagePtr msg = MessageFactory::createMessage(EMAIL, results[i].mail_id);
1906 // || filter->compare(MessageFactory::convertToEmail(msg)) //TODO implemnet compare in filter
1908 retVal.push_back(msg);
1912 startIndex += count;
1914 while (count >= MESSAGE_FIND_LIMIT);
1919 vector<Api::Messaging::IMessagePtr> Messaging::findEmail(Api::Messaging::FolderType folder,
1920 const Api::Tizen::FilterPtr& filter)
1922 vector<Api::Messaging::IMessagePtr> result;
1924 Api::Messaging::EmailAccountInfo account = getCurrentEmailAccount();
1925 if (account.getId().empty()) {
1926 LogWarning("No valid email accounts.");
1932 name = EmailConverter::toMailboxName(folder);
1934 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1935 LogWarning(ex.DumpToString());
1939 int accountId = account.getIntId();
1943 emf_mail_list_item_t* messages = NULL;
1944 int error = email_get_mail_list_ex(accountId,
1946 EMF_LIST_TYPE_NORMAL,
1949 EMF_SORT_DATETIME_HIGH,
1952 DPL::ScopedFree<emf_mail_list_item_t> freeGuard(messages);
1953 if ((EMF_ERROR_MAIL_NOT_FOUND == error) || (NULL == messages)) {
1954 LogWarning("No emails found in mailbox: " << name);
1956 } else if (EMF_ERROR_NONE != error) {
1957 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1958 "Couldn't get mail list from mailbox: " << name <<
1959 ". [" << error << "]");
1962 for (int i = 0; i < count; ++i) {
1963 Api::Messaging::IMessagePtr msg =
1964 MessageFactory::createMessage(EMAIL, messages[i].mail_id);
1966 // ||filter->compare(MessageFactory::convertToEmail(msg)) //TODO implement compare in filter
1968 result.push_back(msg);
1972 startIndex += count;
1974 while (count >= MESSAGE_FIND_LIMIT);
1979 int Messaging::getConversationId(const int& msgId, const MessageType& msgtype)
1987 int index = 0, count = 0;
1988 char *mailBox = NULL;
1989 emf_mail_list_item_t *mailList = NULL;
1991 LogDebug("Current Account " << m_currentEmailAccountId);
1995 if (email_get_mailbox_name_by_mail_id(msgId, &mailBox) != EMF_ERROR_NONE)
1997 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get mail box fail" );
2000 if (email_get_mail_list_ex(m_currentEmailAccountId, mailBox, EMF_LIST_TYPE_NORMAL, 0, MESSAGE_FIND_LIMIT,
2001 EMF_SORT_DATETIME_HIGH, &mailList, &count) != EMF_ERROR_NONE)
2003 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get email list fail" );
2009 for (index = 0; index < count; index++)
2011 if (mailList[index].mail_id == (int)msgId)
2013 return mailList[index].thread_id;
2016 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "can not find msg" );
2018 catch (const WrtDeviceApis::Commons::Exception& ex)
2023 LogError("Exception: " << ex.GetMessage());
2026 return MSG_ERR_INVALID_MSG_TYPE;
2032 msg_message_t msg = msg_new_message();
2033 MSG_SENDINGOPT_S sendOpt = {0, };
2034 MSG_HANDLE_T handle = MsgGetCommonHandle();
2035 MSG_ERROR_T err = MSG_SUCCESS;
2037 err = msg_get_message(handle, msgId, msg, &sendOpt);
2040 if( err < MSG_SUCCESS )
2042 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
2043 "can not find msg id(" << msgId << ")");
2046 err = msg_get_ith_thread_id(msg, 0);
2048 if( err < MSG_SUCCESS )
2050 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
2051 "can not find thread with msg id(" << msgId << ")");
2058 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
2059 "Wrong Type (" << msgId << ")");
2060 return MSG_ERR_INVALID_MSG_TYPE;
2065 std::vector<Api::Messaging::IConversationPtr> Messaging::queryConversations(const Api::Tizen::SortModePtr& sortMode,
2066 const Api::Tizen::FilterPtr& filter, long limit, long offset)
2070 std::vector<Api::Messaging::IConversationPtr> result;
2071 std::string filterSql;
2073 ConversationQueryGeneratorPtr queryGenerator(new ConversationQueryGenerator(sortMode, limit, offset));
2074 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
2075 filter->travel(filterVisitor, 0);
2077 LogDebug("filterSql:[" << filterSql <<"]");
2079 int conversationType = queryGenerator->getMessageType();
2080 switch(conversationType){
2081 case Api::Messaging::EMAIL:{
2082 LogDebug("type is EMAIL:[" << conversationType <<"]");
2083 queryGenerator->reset(MessageQueryGenerator::MODE_EMAIL);
2084 filter->travel(filterVisitor, 0);
2085 filterSql = queryGenerator->getQuery();
2086 result = queryEmailConversation(filterSql);
2090 case Api::Messaging::SMS:{
2091 LogDebug("type is SMS:[" << conversationType <<"]");
2092 queryGenerator->reset(MessageQueryGenerator::MODE_SMS_MMS);
2093 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
2094 filter->travel(filterVisitor, 0);
2095 filterSql = queryGenerator->getQuery();
2096 result = querySmsMmsConversation(filterSql);
2100 case Api::Messaging::MMS:{
2101 LogDebug("type is MMS:[" << conversationType <<"]");
2102 result = querySmsMmsConversation(filterSql);
2107 LogError("[ERROR] >>> invlid type:[" << conversationType <<"]");
2115 bool Messaging::deleteConversations(const Api::Tizen::SortModePtr& sortMode, const Api::Tizen::FilterPtr& filter)
2119 std::vector<Api::Messaging::IConversationPtr> conversationsToDelete = queryConversations(sortMode, filter);
2120 return deleteConversations(conversationsToDelete);
2123 bool Messaging::deleteConversations(const std::vector<Api::Messaging::IConversationPtr>& conversations)
2127 if (conversations.size() == 0)
2130 if (conversations[0]->getType() == Api::Messaging::EMAIL)
2132 LogDebug("Enter-Email");
2135 for (std::size_t i = 0; i < conversations.size(); ++i)
2137 threadId = conversations[i]->getId();
2139 if (email_delete_thread(threadId, false) != EMF_ERROR_NONE)
2147 LogDebug("Enter-Msg");
2148 MSG_THREAD_ID_T threadId = 0;
2149 MSG_HANDLE_T handle = MsgGetCommonHandle();
2151 for (std::size_t i = 0; i < conversations.size(); ++i)
2153 threadId = conversations[i]->getId();
2154 if (msg_delete_thread_message_list(handle, threadId) != MSG_SUCCESS)
2163 std::vector<Api::Messaging::IMessageFolderPtr> Messaging::queryFolders(const Api::Tizen::FilterPtr& filter)
2167 emf_mailbox_t* mailboxes = NULL;
2168 emf_mailbox_t m_mailboxes;
2171 std::vector<Api::Messaging::IMessageFolderPtr> result;
2172 std::string folderName;
2175 Api::Messaging::EmailAccountInfo account = getCurrentEmailAccount();
2177 if (account.getId().empty()) {
2178 LogWarning("No valid email accounts. Skipping");
2182 const vector<Api::Messaging::EmailAccountInfo> accounts = getEmailAccounts();
2184 FolderQueryGeneratorPtr queryGenerator(new FolderQueryGenerator());
2186 IFilterVisitorPtr filterVisitor = DPL::StaticPointerCast<IFilterVisitor>(queryGenerator);
2187 queryGenerator->reset();
2188 filter->travel(filterVisitor, 0);
2189 accountId = queryGenerator->getAccountId();
2193 LogWarning("No valid email accounts. accountId : 0");
2197 if(queryGenerator->isFolderPathExist() == 1){
2198 folderName = queryGenerator->getFolderPath();
2200 if(email_get_mailbox_by_name(accountId, folderName.c_str(), &mailboxes) != 1)
2202 LogError("error during get mailboxes");
2206 m_mailboxes = *mailboxes;
2207 Api::Messaging::IMessageFolderPtr folderPtr(new MessageFolder(m_mailboxes));
2208 result.push_back(folderPtr);
2211 if (!email_get_mailbox_list(accountId, -1, &mailboxes, &mailboxesCount) || !mailboxes)
2213 LogError("error during get mailboxes");
2217 if (mailboxesCount <= 0)
2219 LogDebug("Empty...");
2223 for (int i = 0; i < mailboxesCount; i++)
2226 m_mailboxes = *mailboxes;
2227 Api::Messaging::IMessageFolderPtr folderPtr(new MessageFolder(m_mailboxes));
2228 result.push_back(folderPtr);