2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
21 #include <email-types.h>
22 #include <email-api.h>
23 #include <email-api-network.h>
24 #include <email-api-init.h>
25 #include <email-api-account.h>
27 #include <dpl/log/log.h>
28 #include <dpl/assert.h>
29 #include <Commons/Exception.h>
30 #include <Commons/ThreadPool.h>
31 #include <API/Messaging/ReqReceiverMessage.h>
32 #include <API/Messaging/EventMessagingService.h>
33 #include <API/Messaging/IAttachment.h>
36 #include "SyncNetworkStatus.h"
37 #include <API/Messaging/IMessage.h>
40 #include <vconf-keys.h>
43 const char* DBUS_FILTER_NETWORK_STATUS =
44 "type='signal',interface='User.Email.NetworkStatus'";
45 //const char* DBUS_FILTER_EMAIL_RECEIVED =
46 // "type='signal',interface='User.Email.StorageChange'";
53 using namespace WrtDeviceApis::Commons;
54 using namespace WrtDeviceApis::CommonsJavaScript;
56 MailSync& MailSync::getInstance()
58 static MailSync instance;
62 MailSync::MailSync() :
63 m_dbusThread(new DPL::Thread()),
64 m_dbus(new DBus::Connection())
68 m_dbus->setWorkerThread(m_dbusThread.Get());
69 m_dbus->AddListener(this);
70 m_dbus->SwitchAllListenersToThread(
71 ThreadPool::getInstance().getThreadRef(ThreadEnum::MESSAGING_THREAD)
73 m_dbus->addFilter(DBUS_FILTER_NETWORK_STATUS);
74 m_dbus->open(DBUS_BUS_SYSTEM);
77 if (EMF_ERROR_NONE == email_service_begin()) {
78 LogDebug("Email service Begin\n");
79 if (EMF_ERROR_NONE == email_open_db()) {
80 LogDebug("Email open DB success\n");
83 LogDebug("Email open DB failed\n");
87 LogDebug("Email service not started\n");
92 vconf_get_int("db/email/slot_size", &(slot_size));
94 m_default_slot_size = slot_size;
96 LogDebug( "Slot Size : " << m_default_slot_size );
100 MailSync::~MailSync()
102 m_dbus->RemoveListener(this);
105 m_dbusThread->Quit();
107 //close email service
108 if (EMF_ERROR_NONE == email_close_db()) {
109 LogDebug("Email Close DB Success\n");
110 if (EMF_ERROR_NONE == email_service_end())
112 LogDebug("Email service close Success\n");
116 LogDebug("Email service end failed\n");
120 LogDebug("Email Close DB failed\n");
124 int MailSync::downloadBody( const Api::Messaging::IEmailPtr& mail )
128 int mailId = mail->convertId(mail->getIdRef()); // get mail id
129 LogDebug("start attachment, mail Id : " << mail->getIdRef());
130 SyncRequestIterator it;
131 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
133 const Api::Messaging::IEmailPtr& email = it->second.mail;
134 LogDebug ( "Requests mail ID:" << mail->convertId(email->getIdRef()) );
135 if ( mailId == mail->convertId(email->getIdRef()))
137 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
138 "download Body: " << mailId << " already requested to be download Body.");
143 return downloadBodyInternal(mail, mail->getAccountID());
147 void MailSync::cancelDownloadBody(int handle)
151 SyncRequestIterator it = m_SyncRequests.find(handle);
152 if ( m_SyncRequests.end() != it)
154 if ( it->second.messagingService )
155 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
156 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
160 LogDebug("Don't find cancel DownloadBody handle : " << handle);
161 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
166 void MailSync::cancelDownloadAttachment(int handle)
170 SyncRequestIterator it = m_SyncRequests.find(handle);
171 if ( m_SyncRequests.end() != it)
173 if ( it->second.messagingService )
174 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
175 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
179 LogDebug("Don't find cancel DownloadAttachment handle : " << handle);
184 int MailSync::downloadAttachment(const Api::Messaging::IEmailPtr& mail, const Api::Messaging::IAttachmentPtr& attachment)
186 if ( mail && attachment )
188 //int mailId = mail->convertId(mail->getIdRef()); // get mail id
189 LogDebug("start attachment, mail Id : " << mail->getIdRef());
190 SyncRequestIterator it;
191 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
193 const Api::Messaging::IAttachmentPtr& iattachment = it->second.attachment;
194 LogDebug ( "Requests Attachment ID:" << iattachment->getAttachmentID() << " Input Attachment ID: " << attachment->getAttachmentID());
195 if (iattachment->getAttachmentID() == attachment->getAttachmentID())
197 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
198 "Attachment: " << attachment->getAttachmentID() << " already requested to be download Attachment.");
203 return downloadAttachmentInternal(mail, mail->getAccountID(), attachment);
207 int MailSync::syncAccount(const Api::Messaging::IMessagingServicePtr& messagingService, const int limit)
209 LogDebug("syncAccount : " << limit);
210 if (messagingService)
212 int accountId = messagingService->getAccountID();
213 LogDebug("start sync, account Id : " << accountId);
214 SyncRequestIterator it;
215 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
217 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
218 if (messagingService && messagingService->getAccountID() == accountId)
220 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
221 "Sync: " << accountId << " already requested to be Sync Account.");
224 return syncAccountInternal(messagingService, limit);
228 LogDebug("messagingService is NULL");
231 LogDebug("end sync" );
235 int MailSync::syncAccountInternal( const Api::Messaging::IMessagingServicePtr& messagingService, const int limit)
237 LogDebug("syncAccountInternal limit: " << limit);
239 int account_id = messagingService->getAccountID();
241 emf_mailbox_t mailbox ;
242 unsigned emf_handle = 0;
244 memset(&mailbox, 0, sizeof(emf_mailbox_t));
245 mailbox.name = NULL; /* all folders. */
246 mailbox.account_id = account_id; //set account id.
250 slot_size = m_default_slot_size;
254 email_set_mail_slot_size(0, NULL, slot_size);
256 LogDebug("mailbox.account_id " << mailbox.account_id );
257 err = email_sync_header(&mailbox, &emf_handle);
258 LogDebug("emf_handle " << emf_handle);
260 if (err != EMF_ERROR_NONE) {
261 LogDebug("fail to sync all folders - err : " << err);
265 LogDebug("Insert sync request");
266 SyncRequestData data = SyncRequestData(emf_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
267 m_SyncRequests.insert(std::make_pair(emf_handle, data));
273 void MailSync::syncAccountCancel(const int handle)
275 LogDebug("sync cancel");
278 SyncRequestIterator it = m_SyncRequests.find(handle);
279 if ( m_SyncRequests.end() != it)
281 if ( it->second.messagingService )
282 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
286 LogDebug("Don't find cancel Sync handle : " << handle);
287 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
291 int MailSync::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingService, const std::string& folderName, const int limit)
293 LogDebug("sync folder : " << folderName << " limit : " << limit);
295 if (messagingService)
297 int accountId = messagingService->getAccountID();
298 LogDebug("start sync, account Id : " << accountId);
299 SyncRequestIterator it;
300 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
302 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
303 if (messagingService && messagingService->getAccountID() == accountId
304 && it->second.folderName == folderName)
306 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
307 "Sync: " << accountId << " already requested to be Sync Account.");
311 return syncFolderInternal(messagingService, folderName, limit);
315 LogDebug("messagingService is NULL");
322 void MailSync::syncFolderCancel(const int handle)
324 LogDebug("sync Folder Cancel");
327 SyncRequestIterator it = m_SyncRequests.find(handle);
328 if ( m_SyncRequests.end() != it)
330 if ( it->second.messagingService )
331 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
335 LogDebug("Don't find cancel Sync Folder handle : " << handle);
336 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
340 int MailSync::syncFolderInternal(const Api::Messaging::IMessagingServicePtr& messagingService, const std::string& folderName, const int limit)
342 LogDebug("Folder Name=" << folderName << " limit : " << limit);
344 int account_id = messagingService->getAccountID();
346 unsigned emf_handle = 0;
349 emf_mailbox_t* mailbox;
350 char* mailbox_name = strdup(folderName.c_str());
352 err = email_get_mailbox_by_name(account_id, mailbox_name, &mailbox );
353 if (EMF_ERROR_NONE != err) {
354 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
355 "Couldn't add message to mailbox. [" << err << "]");
359 slot_size = m_default_slot_size;
363 err = email_set_mail_slot_size(0, mailbox_name, slot_size);
364 if (EMF_ERROR_NONE != err) {
365 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
366 "Couldn't set mail solt size. [" << err << "]");
369 err = email_sync_header(mailbox, &emf_handle);
370 if (err != EMF_ERROR_NONE) {
371 LogDebug("fail to sync folders - err : " << err);
375 LogDebug("Insert sync request");
377 SyncRequestData data = SyncRequestData(emf_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER, messagingService, folderName);
378 m_SyncRequests.insert(std::make_pair(emf_handle, data));
385 if ( EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
386 LogDebug("fail to email_free_mailbox - err ");
392 // TODO Copied from former solution, refactor it.
393 void MailSync::OnEventReceived(const DBus::MessageEvent& event)
395 LogDebug("OnEventReceived");
396 SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
397 int mailId = syncNetworkStatus->getMailId(); //if email body download mode.
398 int status = syncNetworkStatus->getStatus();
399 int handle = syncNetworkStatus->getHandle();
401 // if Attachment Load
402 if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||
403 status == NOTI_DOWNLOAD_ATTACH_FAIL )
406 LogDebug(" Debus mailID = " << mailId << " , Nth = " << nth );
408 SyncRequestIterator it = m_SyncRequests.begin();
409 for ( ; it != m_SyncRequests.end(); it++ )
411 const Api::Messaging::IEmailPtr& mail = it->second.mail;
413 LogDebug(" request mailID = " << mail->getUID() << " , Nth = " << (it->second.attachment)->getNth() );
414 if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
416 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
417 if ( mail && requestReceiver )
419 Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
421 if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
423 LogDebug(" Attachment Finish " );
424 int acountId = mail->getAccountID();
425 const Api::Messaging::IAttachmentPtr& attachment = it->second.attachment;
428 emf_mailbox_t mailbox ;
429 emf_attachment_info_t *attach_info = NULL;
430 memset(&mailbox, 0, sizeof(emf_mailbox_t));
431 mailbox.account_id = acountId; //set account id.
432 //int attachmentId = attachment->getAttachmentID();
433 std::stringstream stream;
434 stream << attachment->getAttachmentID();
435 LogDebug("Attachment ID = " << stream.str());
437 int err = email_get_attachment_info( &mailbox, mail->getUID(), stream.str().c_str() , &attach_info );
438 if (err == EMF_ERROR_NONE) {
439 LogDebug("attachment Name : " << attach_info->name);
440 LogDebug("attachment ID : " << attach_info->attachment_id);
441 attachment->init(attach_info->savename, false);
445 LogDebug("fail to email_get_attachment_info - err : " << err);
453 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
454 LogDebug(" Attachment Failed " );
457 requestReceiver->ManualAnswer(event);
458 m_SyncRequests.erase( handle );
466 else if ( status == NOTI_DOWNLOAD_ATTACH_START )
468 LogDebug("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
472 SyncRequestIterator it = m_SyncRequests.find(handle);
473 if ( m_SyncRequests.end() != it)
475 int syncType = it->second.syncType;
476 LogDebug(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
480 case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
482 LogDebug(" Sync Account");
483 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
484 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
486 if ( messagingService && requestReceiver)
488 Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
489 if ( status == NOTI_DOWNLOAD_FINISH )
491 LogDebug("Sync Success");
492 requestReceiver->ManualAnswer(event);
493 m_SyncRequests.erase( handle );
495 else if ( status == NOTI_DOWNLOAD_FAIL )
497 LogDebug("Sync Fail");
498 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
499 requestReceiver->ManualAnswer(event);
500 m_SyncRequests.erase( handle );
505 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
510 case MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER:
512 LogDebug("Sync Folder");
513 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
514 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
516 if ( messagingService && requestReceiver)
518 Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
519 if ( status == NOTI_DOWNLOAD_FINISH )
521 LogDebug("Sync Success");
522 requestReceiver->ManualAnswer(event);
523 m_SyncRequests.erase( handle );
525 else if ( status == NOTI_DOWNLOAD_FAIL )
527 LogDebug("Sync Fail");
528 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
529 requestReceiver->ManualAnswer(event);
530 m_SyncRequests.erase( handle );
535 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
539 case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
541 LogDebug(" DownLoad Body");
542 const Api::Messaging::IEmailPtr& mail = it->second.mail; // IEmailPtr
543 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
544 if (mail && requestReceiver) {
545 Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
546 if ( status == NOTI_DOWNLOAD_BODY_FINISH )
548 requestReceiver->ManualAnswer(event);
549 m_SyncRequests.erase( handle );
551 else if ( status == NOTI_DOWNLOAD_BODY_FAIL )
553 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
554 requestReceiver->ManualAnswer(event);
555 m_SyncRequests.erase( handle );
560 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
573 int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int account_id)
576 LogDebug("downloadInternal");
579 int mailId = mail->convertId(mail->getIdRef());
580 unsigned int emf_handle = 0;
582 LogDebug("folder type = " << mail->getCurrentFolder());
584 char *mailbox_name =NULL;
585 err = email_get_mailbox_name_by_mail_id(mailId, &mailbox_name);
586 if (EMF_ERROR_NONE != err) {
587 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
588 "Couldn't add message to mailbox. [" << err << "]");
590 LogDebug("mail ID :" << mailId );
591 LogDebug("mailbox name : " << mailbox_name );
593 emf_mailbox_t* mailbox;
594 err = email_get_mailbox_by_name(mail->getAccountID(), mailbox_name, &mailbox );
595 if (EMF_ERROR_NONE != err) {
596 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
597 "Couldn't add message to mailbox. [" << err << "]");
600 err = email_download_body(mailbox, mailId, 0, &emf_handle);
601 if (err != EMF_ERROR_NONE) {
602 LogDebug("fail to downloadBody - err : " << err);
603 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
607 LogDebug("Insert downloadBody request");
608 LogDebug("handle : " << emf_handle);
609 SyncRequestData data = SyncRequestData(emf_handle, MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
610 m_SyncRequests.insert(std::make_pair(emf_handle, data));
614 if ( EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
615 LogDebug("fail to email_free_mailbox - err ");
621 int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail, int account_id, const Api::Messaging::IAttachmentPtr& attachment)
623 LogDebug("downloadAttachmentInternal");
628 int mailId = mail->convertId(mail->getIdRef());
629 unsigned emf_handle = 0;
631 char *mailbox_name =NULL;
632 err = email_get_mailbox_name_by_mail_id(mailId, &mailbox_name);
633 if (EMF_ERROR_NONE != err) {
634 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
635 "Couldn't add message to mailbox. [" << err << "]");
637 LogDebug("mail ID :" << mailId );
638 LogDebug("mailbox name : " << mailbox_name );
640 emf_mailbox_t* mailbox;
641 err = email_get_mailbox_by_name(mail->getAccountID(), mailbox_name, &mailbox );
642 if (EMF_ERROR_NONE != err) {
643 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
644 "Couldn't add message to mailbox. [" << err << "]");
647 LogDebug("Attachment ID :" << attachment->getAttachmentID());
648 std::stringstream stream;
651 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
652 "Couldn't convert e-mail attachment id");
656 std::vector<Api::Messaging::IAttachmentPtr> attachments = mail->getAttachments();
657 for ( unsigned int i = 0 ; i < attachments.size() ; i ++)
659 LogDebug( "attachment ID is = " << attachments[i]->getAttachmentID());
660 if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
668 LogDebug("nth = " << stream.str());
669 LogDebug("attachment Order Index = " << stream.str());
670 err = email_download_attachment(mailbox, mailId, stream.str().c_str() , &emf_handle);
671 if (err != EMF_ERROR_NONE) {
672 LogDebug("fail to downloadAttachment - err : " << err);
673 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
677 LogDebug("Insert downloadAttachment request");
678 LogDebug("handle : " << emf_handle);
679 SyncRequestData data = SyncRequestData( emf_handle,
680 MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
681 m_SyncRequests.insert(std::make_pair(emf_handle, data));
685 if ( EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
686 LogDebug("fail to email_free_mailbox - err ");
691 void MailSync::cancelEmailJobInternal(int accountId, int handle)
693 LogDebug("cancel Email Job, account : " <<accountId << " handle : " << handle);
695 int error = email_cancel_job(accountId, handle);
696 if (EMF_ERROR_NONE != error) {
697 LogDebug("error code : " << error );
698 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
699 "Couldn't cancel email job handle: " << handle);
702 m_SyncRequests.erase(handle);