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.
20 * @author Pawel Misiak (p.misiak@samsung.com)
24 #include <emf-types.h>
27 #include <Commons/Exception.h>
28 #include <Commons/StringUtils.h>
29 #include <dpl/log/log.h>
30 #include <dpl/scoped_free.h>
31 #include "Messaging.h"
34 #include "BinarySms.h"
36 #include "EmailConverter.h"
37 #include "EmailService.h"
38 #include "NetworkStatus.h"
41 #include <MapiStorage.h>
42 #include <MapiControl.h>
43 #include <MsgMmsTypes.h>
44 #include <MapiTransport.h>
45 #include <MapiMessage.h>
48 #include <Commons/ThreadPool.h>
50 #define LOG_ENTER LogDebug("---> ENTER");
51 #define LOG_EXIT LogDebug("---> EXIT");
53 using namespace WrtDeviceApis::Messaging;
54 using namespace WrtDeviceApis::Messaging::Api;
58 const int MESSAGE_FIND_LIMIT = 100;
61 namespace WrtDeviceApis {
64 int Messaging::m_currentEmailAccountId = 0;
65 DPL::Atomic Messaging::m_objCounter;
67 Messaging& Messaging::getInstance()
69 static Messaging instance;
73 Messaging::Messaging() :
74 m_onMessageReceivedHandleMgr(NULL),
75 m_mailNotifier(new MailNotifier())
79 const vector<Api::EmailAccountInfo> accounts = getEmailAccounts();
80 LogDebug("Number of emails account=" << accounts.size());
81 if (accounts.size() > 0) {
82 // set default email account - first from the list
83 setCurrentEmailAccount(accounts[0]);
85 LogError("no default email account set");
89 Catch(Commons::PlatformException) {
90 LogError("No email accounts available");
93 Catch(Commons::InvalidArgumentException) {
94 LogError("No email accounts available");
95 //current email not configured, skipped
98 Catch(Commons::UnknownException) {
99 LogError("unknown error");
102 // Begin service for email management ?? pmi question if it should be added before email actions
103 if (0 == m_objCounter) {
104 int error = email_service_begin();
105 if (EMF_ERROR_NONE != error) {
106 LogError("email_service_begin() returned error " << error);
108 LogInfo("email_service_begin() executed without error");
113 m_mailNotifier->AddListener(this);
116 Messaging::~Messaging()
118 m_mailNotifier->RemoveListener(this);
120 if (!--m_objCounter) {
121 int error = email_service_end();
122 if (EMF_ERROR_NONE != error) {
123 LogError("email_service_end() returned error " << error);
125 LogDebug("email_service_end() executed without error");
130 void Messaging::getNumberOfMessages(MessageType msgType,
135 if (NULL == readed ||
137 LogError("output pointers are NULL");
138 Throw(Commons::InvalidArgumentException);
142 if (Api::SMS == msgType) {
143 getNumberOfSms(folder, readed, unReaded);
144 } else if (Api::MMS == msgType) {
145 getNumberOfMms(folder, readed, unReaded);
146 } else if (Api::EMAIL == msgType) {
147 getNumberOfEmails(folder, readed, unReaded);
149 LogError("wrong message type");
150 Throw(Commons::PlatformException);
154 vector<IMessagePtr> Messaging::findMessages(const vector<MessageType>& msgTypes,
155 const string &folder,
156 const MessageFilterPtr& filter)
159 vector<IMessagePtr> retVal;
160 back_insert_iterator< vector<IMessagePtr> > biit(retVal);
161 vector<MessageType>::const_iterator it = msgTypes.begin();
162 for (; it != msgTypes.end(); ++it) {
163 LogDebug("Finding messages (" << *it << ") in folder: " << folder);
164 vector<IMessagePtr> result;
169 FolderType folderEnum = Sms::toFolder(folder);
170 result = findSms(folderEnum, filter);
175 FolderType folderEnum = Mms::toFolder(folder);
176 result = findMms(folderEnum, filter);
181 result = findEmail(folder, filter);
185 LogError("message type unknown");
186 Throw(Commons::PlatformException);
188 LogDebug("Found: " << result.size());
189 copy(result.begin(), result.end(), biit);
195 vector<IMessagePtr> Messaging::findMessages(const vector<MessageType>& msgTypes,
197 const Api::MessageFilterPtr& filter)
200 vector<IMessagePtr> retVal;
201 back_insert_iterator< vector<IMessagePtr> > biit(retVal);
202 vector<MessageType>::const_iterator it = msgTypes.begin();
203 for (; it != msgTypes.end(); ++it) {
204 LogDebug("Finding messages (" << *it << ") in folder: " << folder);
205 vector<IMessagePtr>(Messaging::*findFnPtr)(Api::FolderType folder,
206 const Api::MessageFilterPtr
212 findFnPtr = &Messaging::findSms;
217 findFnPtr = &Messaging::findMms;
222 findFnPtr = &Messaging::findEmail;
226 LogError("message type unknown");
227 Throw(Commons::PlatformException);
229 vector<IMessagePtr> result = (this->*findFnPtr)(folder, filter);
230 LogDebug("Found: " << result.size());
231 copy(result.begin(), result.end(), biit);
237 vector<string> Messaging::getMessageIds(MessageType msgType,
242 return getSmsIds(folder);
244 return getMmsIds(folder);
246 return getEmailIds(folder);
248 LogError("not supported message type");
249 Throw(Commons::InvalidArgumentException);
253 vector<string> Messaging::getSmsIds(FolderType folder)
255 vector<string> retVal;
256 msg_message_t msg = msg_new_message();
257 MSG_LIST_S folder_list_view = { 0, NULL };
261 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
262 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
263 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
264 res = msg_get_folder_view_list(
265 MsgGetCommonHandle(), platformFolder, &sort_rules,
268 if (MSG_SUCCESS != res) {
269 LogError("msg_Get_folder_view_list failed" << res);
270 Throw(Commons::PlatformException);
273 for (int i = 0; i < folder_list_view.nCount; i++) {
275 msg_get_message_type(folder_list_view.msgInfo[i])) {
276 int l_msgId = msg_get_message_id(folder_list_view.msgInfo[i]);
277 ostringstream stream;
279 retVal.push_back(stream.str());
283 msg_release_message_list(&folder_list_view);
286 Catch(Commons::PlatformException) {
287 LogError("Problem with message creation, cleaning");
288 if (folder_list_view.nCount) {
289 msg_release_message_list(&folder_list_view);
292 msg_release_message(&msg);
300 vector<string> Messaging::getMmsIds(FolderType folder)
302 vector<string> retVal;
303 msg_message_t msg = msg_new_message();
304 MSG_LIST_S folder_list_view = { 0, NULL };
308 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
309 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
310 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
311 res = msg_get_folder_view_list(
312 MsgGetCommonHandle(), platformFolder, &sort_rules,
315 if (MSG_SUCCESS != res) {
316 LogError("msg_Get_folder_view_list failed" << res);
317 Throw(Commons::PlatformException);
320 for (int i = 0; i < folder_list_view.nCount; i++) {
322 msg_get_message_type(folder_list_view.msgInfo[i])) {
323 int l_msgId = msg_get_message_id(folder_list_view.msgInfo[i]);
324 ostringstream stream;
326 retVal.push_back(stream.str());
330 msg_release_message_list(&folder_list_view);
333 Catch(Commons::PlatformException) {
334 LogError("Problem with message creation, cleaning");
335 if (folder_list_view.nCount) {
336 msg_release_message_list(&folder_list_view);
339 msg_release_message(&msg);
347 vector<string> Messaging::getEmailIds(FolderType /*folder*/)
349 vector<string> retVal;
353 Throw(Commons::UnknownException);
358 vector<EmailAccountInfo> Messaging::getEmailAccounts() const
360 emf_account_t* accounts = NULL;
363 if (!email_get_account_list(&accounts, &count)) {
364 ThrowMsg(Commons::PlatformException,
365 "Couldn't get e-mail accounts.");
369 ThrowMsg(Commons::PlatformException, "No e-mail account found.");
372 vector<Api::EmailAccountInfo> result;
373 for (int i = 0; i < count; ++i) {
374 Api::EmailAccountInfo account(accounts[i].account_id,
375 accounts[i].user_name,
376 accounts[i].email_addr);
377 result.push_back(account);
380 if (accounts != NULL) {
381 email_free_account(&accounts, count);
386 Catch(Commons::PlatformException) {
387 if (accounts != NULL) {
388 email_free_account(&accounts, count);
394 int Messaging::getEmailAccountId(const std::string& account)
397 string tmpAccount = account;
398 emf_account_t *pAccountArray = NULL;
403 if (account.empty()) {
404 tmpAccount = getCurrentEmailAccount().getAddress();
405 if (tmpAccount.empty()) {
407 "current email account is not set, possible that no account created");
408 Throw(Commons::PlatformException);
412 if (!email_get_account_list(&pAccountArray, &iCount)) {
413 LogError("email_get_account_list error");
414 Throw(Commons::PlatformException);
418 LogError("no email account exist");
419 Throw(Commons::PlatformException);
422 for (int i = 0; i < iCount; i++) {
423 string tmp = pAccountArray[i].email_addr;
424 if (tmp == tmpAccount) {
425 m_currentEmailAccountId = pAccountArray[i].account_id;
426 retVal = m_currentEmailAccountId;
431 if (0 == m_currentEmailAccountId) {
432 LogError("wrong email account ID");
433 Throw(Commons::PlatformException);
436 if (pAccountArray != NULL) {
437 LogDebug("free account, ptr=" << pAccountArray);
438 email_free_account(&pAccountArray, iCount);
442 LogError("no email account created");
443 Throw(Commons::PlatformException);
447 Catch(Commons::PlatformException) {
448 LogError("exception catch, platform exception");
449 if (pAccountArray != NULL) {
450 email_free_account(&pAccountArray, iCount);
458 void Messaging::fetchEmailHeaders()
460 emf_mailbox_t *mailbox = NULL;
462 mailbox = static_cast<emf_mailbox_t*>(calloc(sizeof (emf_mailbox_t), 1));
464 LogError("calloc failed");
467 mailbox->account_id = m_currentEmailAccountId;
468 mailbox->name = strdup(EMF_INBOX_NAME);
469 if (EMF_ERROR_NONE != email_sync_header(mailbox, &handle)) {
470 LogError("email_sync_header failed");
472 email_free_mailbox(&mailbox, 1);
475 int Messaging::convertFolderToPlatform(const FolderType folder)
477 MSG_FOLDER_ID_T platfromFolderId;
480 platfromFolderId = MSG_INBOX_ID;
483 platfromFolderId = MSG_DRAFT_ID;
486 platfromFolderId = MSG_OUTBOX_ID;
489 platfromFolderId = MSG_SENTBOX_ID;
494 LogError("Invalid folder: " << folder);
495 Throw(Commons::PlatformException);
498 return platfromFolderId;
501 int Messaging::convertFolderToPlatform(const std::string &folder)
503 MSG_FOLDER_LIST_S folderList = { 0, NULL };
508 msg_get_folder_list(MsgGetCommonHandle(), &folderList)) {
509 LogError("msg_get_folder_list error");
510 Throw(Commons::PlatformException);
512 for (int i = 0; i < folderList.nCount; ++i) {
513 if (MSG_FOLDER_TYPE_USER_DEF ==
514 folderList.folderInfo[i].folderType &&
515 NULL != folderList.folderInfo[i].folderName &&
516 folder == folderList.folderInfo[i].folderName) {
517 result = folderList.folderInfo[i].folderId;
522 Catch(Commons::PlatformException) {
523 if (folderList.nCount) {
524 msg_release_folder_list(&folderList);
528 if (folderList.nCount) {
529 msg_release_folder_list(&folderList);
534 void Messaging::addOnMessageReceived(
535 const Api::EmitterMessageReceivedPtr& emitter)
538 m_onMessageReceived.attach(emitter);
539 EmittersMessageReceived::LockType lock = m_onMessageReceived.getLock();
540 if (m_onMessageReceived.size() == 1) {
541 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(
542 new MsgServiceHandleMgr());
545 err = msg_reg_sms_message_callback(
546 m_onMessageReceivedHandleMgr->getHandle(),
550 if (err != MSG_SUCCESS) {
551 LogError("Couldn't register on SMS received callback, err=" << err);
553 err = msg_reg_mms_conf_message_callback(
554 m_onMessageReceivedHandleMgr->getHandle(),
558 if (err != MSG_SUCCESS) {
559 LogError("Couldn't register on MMS received callback, err=" << err);
564 void Messaging::removeOnMessageReceived(Api::EmitterMessageReceived::IdType id)
567 m_onMessageReceived.detach(id);
568 EmittersMessageReceived::LockType lock = m_onMessageReceived.getLock();
569 if (m_onMessageReceived.size() == 0) {
570 m_onMessageReceivedHandleMgr = MsgServiceHandleMgrPtr(NULL);
574 void Messaging::OnEventReceived(const EmailReceivedEvent& event)
576 LogDebug("Received email: " << event.GetArg0());
577 EventMessageReceivedPtr jsEvent(new EventMessageReceived());
578 IMessagePtr msg = MessageFactory::createMessage(EMAIL, event.GetArg0());
580 IEmailPtr email = MessageFactory::convertToEmail(msg);
581 jsEvent->setMessage(msg);
582 m_onMessageReceived.emitIf(jsEvent,
583 MessageFilterMatcher<Api::IEmailPtr>(email));
586 void Messaging::onSmsReceived(MSG_HANDLE_T /*handle*/,
591 Messaging* this_ = static_cast<Messaging*>(data);
594 IMessagePtr message = MessageFactory::createMessage(
598 ISmsPtr sms = MessageFactory::convertToSms(message);
599 EventMessageReceivedPtr event(new EventMessageReceived());
600 event->setMessage(message);
601 this_->m_onMessageReceived.emitIf(event,
602 MessageFilterMatcher<Api::ISmsPtr>(
605 Catch(Commons::ConversionException) {
606 LogError("Couldn't convert message to sms.");
611 void Messaging::onMmsReceived(MSG_HANDLE_T /*handle*/,
616 Messaging* this_ = static_cast<Messaging*>(data);
619 IMessagePtr message = MessageFactory::createMessage(
623 IMmsPtr mms = MessageFactory::convertToMms(message);
624 EventMessageReceivedPtr event(new EventMessageReceived());
625 event->setMessage(message);
626 this_->m_onMessageReceived.emitIf(event,
627 MessageFilterMatcher<Api::IMmsPtr>(
630 Catch(Commons::ConversionException) {
631 LogError("Couldn't convert message to mms.");
636 void Messaging::getNumberOfEmails(Api::FolderType folder,
642 emf_mailbox_t mailbox;
643 memset(&mailbox, 0, sizeof(emf_mailbox_t));
644 mailbox.account_id = 0; // means for all accounts
645 mailbox.name = Commons::String::strdup(
646 EmailConverter::toMailboxName(folder)
649 int error = email_count_message(&mailbox, read, unread);
652 if (EMF_ERROR_NONE != error) {
653 ThrowMsg(Commons::PlatformException,
654 "Couldn't get number of emails. [" << error << "]");
660 void Messaging::getNumberOfSms(Api::FolderType folder,
664 getNumberOfSmsMms(folder, read, unread, SMS);
667 void Messaging::getNumberOfMms(Api::FolderType folder,
671 getNumberOfSmsMms(folder, read, unread, MMS);
674 void Messaging::getNumberOfSmsMms(Api::FolderType folder,
677 Api::MessageType msgType)
679 MSG_LIST_S folderListView = { 0, NULL };
686 MSG_MESSAGE_TYPE_T msgTypePlatform;
687 if (SMS == msgType) {
688 msgTypePlatform = MSG_TYPE_SMS;
689 } else if (MMS == msgType) {
690 msgTypePlatform = MSG_TYPE_MMS;
692 LogError("no supported message type in this method");
693 Throw(Commons::PlatformException);
696 MSG_FOLDER_ID_T msgFolderId;
699 msgFolderId = MSG_INBOX_ID;
702 msgFolderId = MSG_OUTBOX_ID;
705 msgFolderId = MSG_SPAMBOX_ID;
708 msgFolderId = MSG_SENTBOX_ID;
711 msgFolderId = MSG_DRAFT_ID;
714 LogError("wrong folder type");
715 Throw(Commons::PlatformException);
719 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
720 if (msg_get_folder_view_list(MsgGetCommonHandle(), msgFolderId,
722 &folderListView) != MSG_SUCCESS) {
723 LogDebug("msg_get_folder_view_list empty or failed, msgFolderId=" <<
725 Throw(Commons::PlatformException);
727 // go thtough all message to check type of message
728 LogDebug("msgCount=" << (int) folderListView.nCount);
729 for (int msg_cnt = 0; msg_cnt < folderListView.nCount; msg_cnt++) {
730 LogDebug("msgMainType=" <<
731 msg_get_message_type(folderListView.msgInfo[msg_cnt]) <<
732 ", searching for = " << (int) msgTypePlatform);
733 if (msgTypePlatform == msg_get_message_type(
734 folderListView.msgInfo[msg_cnt])) {
735 if (msg_is_read(folderListView.msgInfo[msg_cnt])) {
742 msg_release_message_list(&folderListView);
744 LogDebug("readed=" << *read << ", unReaded=" << *unread);
747 Catch(Commons::PlatformException) {
748 if (folderListView.nCount) {
749 msg_release_message_list(&folderListView);
755 vector<Api::IMessagePtr> Messaging::findSms(Api::FolderType folder,
756 const Api::MessageFilterPtr& filter)
758 vector<Api::IMessagePtr> retVal;
759 msg_message_t msg = msg_new_message();
760 MSG_LIST_S folder_list_view = { 0, NULL };
764 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
765 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
766 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
767 res = msg_get_folder_view_list(
768 MsgGetCommonHandle(), platformFolder, &sort_rules,
771 if (MSG_SUCCESS != res) {
772 LogError("msg_get_folder_view_list failed " << res);
773 Throw(Commons::PlatformException);
776 for (int i = 0; i < folder_list_view.nCount; i++) {
778 msg_get_message_type(folder_list_view.msgInfo[i])) {
779 int l_msgId = msg_get_message_id(folder_list_view.msgInfo[i]);
780 Api::IMessagePtr msg = MessageFactory::createMessage(SMS,
782 // invalid or null filter = empty filter
783 if (!filter || !filter->isValid() ||
784 filter->compare(MessageFactory::convertToSms(msg))) {
785 retVal.push_back(msg);
790 msg_release_message_list(&folder_list_view);
793 Catch(Commons::PlatformException) {
794 LogError("Problem with message creation, cleaning");
795 if (folder_list_view.nCount) {
796 msg_release_message_list(&folder_list_view);
799 msg_release_message(&msg);
807 vector<Api::IMessagePtr> Messaging::findMms(Api::FolderType folder,
808 const Api::MessageFilterPtr& filter)
810 vector<Api::IMessagePtr> retVal;
811 msg_message_t msg = msg_new_message();
812 MSG_LIST_S folder_list_view = { 0, NULL };
816 const MSG_SORT_RULE_S sort_rules = { MSG_SORT_BY_DISPLAY_TIME, true };
817 MSG_ERROR_T res = MSG_ERR_UNKNOWN;
818 const MSG_FOLDER_ID_T platformFolder = convertFolderToPlatform(folder);
819 res = msg_get_folder_view_list(
820 MsgGetCommonHandle(), platformFolder, &sort_rules,
823 if (MSG_SUCCESS != res) {
824 LogError("msg_Get_folder_view_list failed" << res);
825 Throw(Commons::PlatformException);
828 for (int i = 0; i < folder_list_view.nCount; i++) {
830 msg_get_message_type(folder_list_view.msgInfo[i])) {
831 int l_msgId = msg_get_message_id(folder_list_view.msgInfo[i]);
832 Api::IMessagePtr msg =
833 MessageFactory::createMessage(MMS, l_msgId);
834 // invalid or null filter = empty filter
835 if (!filter || !filter->isValid() ||
836 filter->compare(MessageFactory::convertToMms(msg))) {
837 retVal.push_back(msg);
842 msg_release_message_list(&folder_list_view);
845 Catch(Commons::PlatformException) {
846 LogError("Problem with message creation, cleaning");
847 if (folder_list_view.nCount) {
848 msg_release_message_list(&folder_list_view);
851 msg_release_message(&msg);
859 void Messaging::createFolder(MessageType msgType,
860 const string& userFolder)
865 createMsgServiceFolder(userFolder);
868 createEmailFolder(userFolder);
871 LogError("msg not supported");
872 Throw(Commons::UnknownException);
876 void Messaging::createMsgServiceFolder(const std::string& userFolder)
878 if (userFolder.length() > MAX_FOLDER_NAME_SIZE) {
879 LogError("folder name to long");
880 Throw(Commons::PlatformException);
883 MSG_FOLDER_INFO_S folderInfo;
885 folderInfo.folderId = 0;
886 strncpy(folderInfo.folderName, userFolder.c_str(), userFolder.length());
888 folderInfo.folderType = MSG_FOLDER_TYPE_USER_DEF;
890 if (msg_add_folder(MsgGetCommonHandle(), &folderInfo) != MSG_SUCCESS) {
891 LogError("msg_add_folder failed");
892 Throw(Commons::PlatformException);
896 void Messaging::createEmailFolder(const std::string& /*userFolder*/)
900 Throw(Commons::UnknownException);
903 void Messaging::deleteFolder(MessageType msgType,
904 const string& userFolder)
909 deleteMsgServiceFolder(userFolder);
912 deleteEmailFolder(userFolder);
915 LogError("msg not supported");
916 Throw(Commons::UnknownException);
920 void Messaging::deleteMsgServiceFolder(const string& userFolder)
924 int platformFolderId = convertFolderToPlatform(userFolder);
926 msg_delete_folder(MsgGetCommonHandle(), platformFolderId)) {
927 LogError("msg_get_folder_list failed");
928 Throw(Commons::PlatformException);
931 Catch(Commons::PlatformException) {
936 void Messaging::deleteEmailFolder(const string& /*userFolder*/)
940 Throw(Commons::UnknownException);
943 vector<string> Messaging::getFolderNames(MessageType msgType)
948 return getFolderNamesMsgService();
950 return getFolderNamesEmail();
952 LogError("msg not supported");
953 Throw(Commons::UnknownException);
957 vector<string> Messaging::getFolderNamesMsgService()
959 vector<string> retVal;
960 MSG_FOLDER_LIST_S msgFolderList = { 0, NULL }; // output data
964 if (msg_get_folder_list(MsgGetCommonHandle(),
965 &msgFolderList) != MSG_SUCCESS) {
966 LogError("msg_get_folder_list failed");
967 Throw(Commons::PlatformException);
970 LogDebug("number of folder=" << msgFolderList.nCount);
971 for (int i = 0; i < msgFolderList.nCount; i++) {
972 LogDebug("folderName=" << msgFolderList.folderInfo[i].folderName);
973 LogDebug("folderId=" << (int) msgFolderList.folderInfo[i].folderId);
976 (int) msgFolderList.folderInfo[i].folderType);
977 retVal.push_back(msgFolderList.folderInfo[i].folderName);
979 (void) msg_release_folder_list(&msgFolderList);
982 Catch(Commons::PlatformException) {
983 if (msgFolderList.nCount) {
984 (void) msg_release_folder_list(&msgFolderList);
991 vector<string> Messaging::getFolderNamesEmail()
993 vector<string> retVal;
996 emf_mailbox_t* mailboxes = NULL;
998 string emailAccountName;
999 Api::EmailAccountInfo account = getCurrentEmailAccount();
1003 if (!email_get_mailbox_list(account.getIntId(), -1, &mailboxes,
1004 &mailboxesCount) || !mailboxes) {
1005 LogError("error during get mailboxes");
1007 for (int i = 0; i < mailboxesCount; i++) {
1008 retVal.push_back(mailboxes->name);
1009 LogDebug("mailbox found, name=" << mailboxes->name);
1014 Catch(Commons::PlatformException) {
1015 // nothing to clean, re-throw exception
1022 vector<Api::IMessagePtr> Messaging::findEmail(const std::string &folder,
1023 const Api::MessageFilterPtr& filter)
1025 vector<Api::IMessagePtr> retVal;
1027 Api::EmailAccountInfo account = getCurrentEmailAccount();
1028 if (account.getId().empty()) {
1029 LogWarning("No valid email accounts. Skipping");
1033 int accountId = account.getIntId();
1035 // number of found emails
1041 emf_mail_list_item_t* results = NULL;
1042 int err = email_get_mail_list_ex(accountId,
1044 EMF_LIST_TYPE_NORMAL,
1047 EMF_SORT_DATETIME_HIGH,
1050 DPL::ScopedFree<emf_mail_list_item_t> autoFree(results);
1052 if (EMF_ERROR_MAIL_NOT_FOUND == err || results == NULL) {
1053 LogWarning("No emails found in mailbox: " << folder);
1055 } else if (EMF_ERROR_NONE != err) {
1057 "Unable to get mail list for mailbox: " << folder <<
1058 ", Error: " << err);
1059 Throw(Commons::PlatformException);
1063 for (int i = 0; i < count; ++i) {
1064 Api::IMessagePtr msg = MessageFactory::createMessage(
1069 filter->compare(MessageFactory::convertToEmail(msg))) {
1070 retVal.push_back(msg);
1074 startIndex += count;
1076 while (count >= MESSAGE_FIND_LIMIT);
1081 vector<Api::IMessagePtr> Messaging::findEmail(Api::FolderType folder,
1082 const Api::MessageFilterPtr& filter)
1084 vector<Api::IMessagePtr> result;
1086 Api::EmailAccountInfo account = getCurrentEmailAccount();
1087 if (account.getId().empty()) {
1088 LogWarning("No valid email accounts.");
1094 name = EmailConverter::toMailboxName(folder);
1096 catch (const Commons::PlatformException& ex) {
1097 LogWarning(ex.DumpToString());
1101 int accountId = account.getIntId();
1105 emf_mail_list_item_t* messages = NULL;
1106 int error = email_get_mail_list_ex(accountId,
1108 EMF_LIST_TYPE_NORMAL,
1111 EMF_SORT_DATETIME_HIGH,
1114 DPL::ScopedFree<emf_mail_list_item_t> freeGuard(messages);
1115 if ((EMF_ERROR_MAIL_NOT_FOUND == error) || (NULL == messages)) {
1116 LogWarning("No emails found in mailbox: " << name);
1118 } else if (EMF_ERROR_NONE != error) {
1119 ThrowMsg(Commons::PlatformException,
1120 "Couldn't get mail list from mailbox: " << name <<
1121 ". [" << error << "]");
1124 for (int i = 0; i < count; ++i) {
1125 Api::IMessagePtr msg =
1126 MessageFactory::createMessage(EMAIL, messages[i].mail_id);
1128 filter->compare(MessageFactory::convertToEmail(msg))) {
1129 result.push_back(msg);
1133 startIndex += count;
1135 while (count >= MESSAGE_FIND_LIMIT);