2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <email-types.h>
20 #include <email-api.h>
21 #include <email-api-network.h>
22 #include <email-api-init.h>
23 #include <email-api-account.h>
25 #include <dpl/assert.h>
26 #include <Commons/Exception.h>
27 #include <Commons/ThreadPool.h>
28 #include "ReqReceiverMessage.h"
29 #include "EventMessagingService.h"
30 #include "IAttachment.h"
33 #include "SyncNetworkStatus.h"
37 #include <vconf-keys.h>
42 const char* DBUS_FILTER_NETWORK_STATUS =
43 "type='signal',interface='User.Email.NetworkStatus'";
44 //const char* DBUS_FILTER_EMAIL_RECEIVED =
45 // "type='signal',interface='User.Email.StorageChange'";
51 using namespace WrtDeviceApis::Commons;
53 MailSync& MailSync::getInstance()
55 static MailSync instance;
59 MailSync::MailSync() :
60 m_dbusThread(new DPL::Thread()),
61 m_dbus(new DBus::Connection())
65 m_dbus->setWorkerThread(m_dbusThread.Get());
66 m_dbus->AddListener(this);
67 m_dbus->SwitchAllListenersToThread(
68 ThreadPool::getInstance().getThreadRef(ThreadEnum::MESSAGING_THREAD)
70 m_dbus->addFilter(DBUS_FILTER_NETWORK_STATUS);
71 m_dbus->open(DBUS_BUS_SYSTEM);
74 int error = email_service_begin();
75 if (error == EMAIL_ERROR_NONE) {
76 LoggerD("Email service Begin\n");
77 if (EMAIL_ERROR_NONE == email_open_db()) {
78 LoggerD("Email open DB success\n");
81 LoggerD("Email open DB failed\n");
85 LoggerD("Email service not started\n");
86 LoggerD("Error : " << error);
91 vconf_get_int("db/private/email-service/slot_size", &(slot_size));
93 m_default_slot_size = slot_size;
95 LoggerD( "Slot Size : " << m_default_slot_size );
101 m_dbus->RemoveListener(this);
104 m_dbusThread->Quit();
106 //close email service
107 if (EMAIL_ERROR_NONE == email_close_db()) {
108 LoggerD("Email Close DB Success\n");
109 if (EMAIL_ERROR_NONE == email_service_end())
111 LoggerD("Email service close Success\n");
115 LoggerD("Email service end failed\n");
119 LoggerD("Email Close DB failed\n");
123 int MailSync::downloadBody( const IEmailPtr& mail )
127 int mailId = mail->convertId(mail->getIdRef()); // get mail id
128 LoggerD("start attachment, mail Id : " << mail->getIdRef());
129 SyncRequestIterator it;
131 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
133 synType = it->second.syncType;
134 LoggerD("synType : " << synType);
136 if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY )
141 const IEmailPtr& email = it->second.mail;
142 LoggerD ( "Requests mail ID:" << mail->convertId(email->getIdRef()) );
143 if ( mailId == mail->convertId(email->getIdRef()))
145 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
146 "download Body: " << mailId << " already requested to be download Body.");
151 return downloadBodyInternal(mail, mail->getAccountID());
155 void MailSync::cancelDownloadBody(int handle)
159 SyncRequestIterator it = m_SyncRequests.find(handle);
160 if ( m_SyncRequests.end() != it)
162 if ( it->second.messagingService )
163 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
164 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
168 LoggerD("Don't find cancel DownloadBody handle : " << handle);
169 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
174 void MailSync::cancelDownloadAttachment(int handle)
178 SyncRequestIterator it = m_SyncRequests.find(handle);
179 if ( m_SyncRequests.end() != it)
181 if ( it->second.messagingService )
182 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
183 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
187 LoggerD("Don't find cancel DownloadAttachment handle : " << handle);
192 int MailSync::downloadAttachment(const IEmailPtr& mail, const IAttachmentPtr& attachment)
194 if ( mail && attachment )
196 //int mailId = mail->convertId(mail->getIdRef()); // get mail id
197 LoggerD("start attachment, mail Id : " << mail->getIdRef());
198 SyncRequestIterator it;
200 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
202 synType = it->second.syncType;
203 LoggerD("synType : " << synType);
205 if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT )
211 const IAttachmentPtr& iattachment = it->second.attachment;
212 LoggerD ( "Requests Attachment ID:" << iattachment->getAttachmentID() << " Input Attachment ID: " << attachment->getAttachmentID());
213 if (iattachment->getAttachmentID() == attachment->getAttachmentID())
215 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
216 "Attachment: " << attachment->getAttachmentID() << " already requested to be download Attachment.");
221 return downloadAttachmentInternal(mail, mail->getAccountID(), attachment);
225 int MailSync::syncAccount(const IMessagingServicePtr& messagingService, const int limit)
227 LoggerD("syncAccount : " << limit);
228 if (messagingService)
230 int accountId = messagingService->getAccountID();
231 LoggerD("start sync, account Id : " << accountId);
232 SyncRequestIterator it;
234 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
236 synType = it->second.syncType;
237 LoggerD("synType : " << synType);
239 if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC )
245 const IMessagingServicePtr& messagingService = it->second.messagingService;
246 if (messagingService && messagingService->getAccountID() == accountId)
248 LoggerD("accountId is sync requested already");
249 // ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
252 return syncAccountInternal(messagingService, limit);
256 LoggerD("messagingService is NULL");
259 LoggerD("end sync" );
263 int MailSync::syncAccountInternal( const IMessagingServicePtr& messagingService, const int limit)
265 LoggerD("syncAccountInternal limit: " << limit);
267 int account_id = messagingService->getAccountID();
269 // email_mailbox_t mailbox ;
270 int email_handle = 0;
272 // memset(&mailbox, 0, sizeof(email_mailbox_t));
273 // mailbox.mailbox_name = NULL; /* all folders. */
274 // mailbox.account_id = account_id; //set account id.
278 slot_size = m_default_slot_size;
282 email_set_mail_slot_size(0, 0, slot_size);
284 // LoggerD("mailbox.account_id " << mailbox.account_id );
285 // err = email_sync_header(&mailbox, &email_handle);
286 // LoggerD("emf_handle " << emf_handle);
288 err = email_sync_header(account_id, 0, &email_handle);
289 LoggerD("email_handle " << email_handle);
291 if (err != EMAIL_ERROR_NONE) {
292 LoggerD("fail to sync all folders - err : " << err);
296 LoggerD("Insert sync request");
297 SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
298 m_SyncRequests.insert(std::make_pair(email_handle, data));
304 void MailSync::syncAccountCancel(const int handle)
306 LoggerD("sync cancel");
309 SyncRequestIterator it = m_SyncRequests.find(handle);
310 if ( m_SyncRequests.end() != it)
312 if ( it->second.messagingService )
313 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
317 LoggerD("Don't find cancel Sync handle : " << handle);
318 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
322 int MailSync::syncFolder(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
324 LoggerD("sync folder : " << folder_id << " limit : " << limit);
326 if (messagingService)
328 int accountId = messagingService->getAccountID();
329 LoggerD("start sync, account Id : " << accountId);
330 SyncRequestIterator it;
332 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
334 synType = it->second.syncType;
335 LoggerD("synType : " << synType);
337 if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER )
343 const IMessagingServicePtr& messagingService = it->second.messagingService;
344 if (messagingService && messagingService->getAccountID() == accountId
345 && it->second.folderId == folder_id)
347 LoggerD("accountId is syncFolder requested already");
348 // ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
352 return syncFolderInternal(messagingService, folder_id, limit);
356 LoggerD("messagingService is NULL");
363 void MailSync::syncFolderCancel(const int handle)
365 LoggerD("sync Folder Cancel");
368 SyncRequestIterator it = m_SyncRequests.find(handle);
369 if ( m_SyncRequests.end() != it)
371 if ( it->second.messagingService )
372 cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
376 LoggerD("Don't find cancel Sync Folder handle : " << handle);
377 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
381 int MailSync::syncFolderInternal(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
383 LoggerD("Folder Id=" << folder_id << " limit : " << limit);
385 int account_id = messagingService->getAccountID();
387 int email_handle = 0;
390 email_mailbox_t* mailbox = NULL;
392 // char* mailbox_name = strdup(folderName.c_str());
394 err = email_get_mailbox_by_mailbox_id(folder_id, &mailbox);
395 if (EMAIL_ERROR_NONE != err) {
396 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
397 "Couldn't set mail solt size. [" << err << "]");
400 if (mailbox == NULL) {
401 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't get mailbox");
404 // char* mailbox_name = strdup(mailbox->mailbox_name);
407 slot_size = m_default_slot_size;
411 err = email_set_mail_slot_size(0, mailbox->mailbox_id, slot_size);
412 if (EMAIL_ERROR_NONE != err) {
413 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
414 "Couldn't set mail solt size. [" << err << "]");
417 // err = email_sync_header(mailbox, &email_handle);
418 err = email_sync_header(account_id, mailbox->mailbox_id, &email_handle);
419 if (err != EMAIL_ERROR_NONE) {
420 LoggerD("fail to sync folders - err : " << err);
424 LoggerD("Insert sync request");
426 SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER, messagingService, folder_id);
427 m_SyncRequests.insert(std::make_pair(email_handle, data));
431 // free(mailbox_name);
433 if ( mailbox != NULL )
435 if ( EMAIL_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
436 LoggerD("fail to email_free_mailbox - err ");
444 // TODO Copied from former solution, refactor it.
445 void MailSync::OnEventReceived(const DBus::MessageEvent& event)
447 LoggerD("OnEventReceived");
448 SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
449 int mailId = syncNetworkStatus->getMailId(); //if email body download mode.
450 int status = syncNetworkStatus->getStatus();
451 int handle = syncNetworkStatus->getHandle();
453 LoggerD("mailId : " << mailId << " status : " << status << " handle : " << handle);
454 // if Attachment Load
455 if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||status == NOTI_DOWNLOAD_ATTACH_FAIL )
458 LoggerD(" Debus mailID = " << mailId << " , Nth = " << nth );
460 SyncRequestIterator it = m_SyncRequests.begin();
461 for ( ; it != m_SyncRequests.end(); it++ )
463 const IEmailPtr& mail = it->second.mail;
465 LoggerD(" request mailID = " << mail->getUID() << " , Nth = " << (it->second.attachment)->getNth() );
466 if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
468 EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
469 if ( mail && requestReceiver )
471 EventMessagingServicePtr event = mail->getMessagingServiceEvent();
472 // EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
474 if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
476 LoggerD(" Attachment Finish " );
477 const IAttachmentPtr& attachment = it->second.attachment;
481 email_attachment_data_t* attachment_data = NULL;
482 int attachmentId = attachment->getAttachmentID();
483 LoggerD("Attachment ID = " << attachmentId);
485 int err = email_get_attachment_data(attachmentId, &attachment_data);
486 if (err == EMAIL_ERROR_NONE) {
487 LoggerD("attachment Name : " << attachment_data->attachment_name);
488 LoggerD("attachment ID : " << attachment_data->attachment_id);
489 attachment->init(attachment_data->attachment_path, false);
493 LoggerD("fail to email_get_attachment_data - err : " << err);
496 if ( attachment_data != NULL )
498 email_free_attachment_data(&attachment_data, 1);
506 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
507 LoggerD(" Attachment Failed " );
510 LoggerD("remove downloadAttachment request");
511 LoggerD("handle : " << it->first );
512 requestReceiver->ManualAnswer(event);
513 m_SyncRequests.erase(it->first);
520 else if ( status == NOTI_DOWNLOAD_ATTACH_START )
522 LoggerD("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
526 SyncRequestIterator it = m_SyncRequests.find(handle);
527 if ( m_SyncRequests.end() != it)
529 int syncType = it->second.syncType;
530 LoggerD(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
534 case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
536 LoggerD(" Sync Account");
537 const IMessagingServicePtr& messagingService = it->second.messagingService;
538 EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
540 if ( messagingService && requestReceiver)
542 EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
543 // get event using handle from m_opRequests;
546 if ( status == NOTI_DOWNLOAD_FINISH )
548 LoggerD("Sync Success");
549 requestReceiver->ManualAnswer(event);
550 m_SyncRequests.erase( handle );
552 else if ( status == NOTI_DOWNLOAD_FAIL )
554 LoggerD("Sync Fail");
555 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
556 requestReceiver->ManualAnswer(event);
557 m_SyncRequests.erase( handle );
563 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
567 case MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER:
569 LoggerD("Sync Folder");
570 const IMessagingServicePtr& messagingService = it->second.messagingService;
571 EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
573 if ( messagingService && requestReceiver)
575 EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
578 if ( status == NOTI_DOWNLOAD_FINISH )
580 LoggerD("Sync Success");
581 requestReceiver->ManualAnswer(event);
582 m_SyncRequests.erase( handle );
584 else if ( status == NOTI_DOWNLOAD_FAIL )
586 LoggerD("Sync Fail");
587 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
588 requestReceiver->ManualAnswer(event);
589 m_SyncRequests.erase( handle );
595 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
599 case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
601 LoggerD(" DownLoad Body");
602 const IEmailPtr& mail = it->second.mail; // IEmailPtr
603 EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
604 if (mail && requestReceiver) {
605 EventMessagingServicePtr event = mail->getMessagingServiceEvent();
606 // EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
608 if ( status == NOTI_DOWNLOAD_BODY_FINISH )
610 event->m_message->readAllData();
611 requestReceiver->ManualAnswer(event);
612 m_SyncRequests.erase( handle );
614 else if ( status == NOTI_DOWNLOAD_BODY_FAIL )
616 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
617 requestReceiver->ManualAnswer(event);
618 m_SyncRequests.erase( handle );
623 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
631 int MailSync::downloadBodyInternal( const IEmailPtr& mail, int account_id)
634 LoggerD("downloadInternal");
637 int mailId = mail->convertId(mail->getIdRef());
638 int email_handle = 0;
640 LoggerD("folder type = " << mail->getCurrentFolder());
641 LoggerD("mail ID :" << mailId );
643 err = email_download_body(mailId, 0, &email_handle);
644 if (err != EMAIL_ERROR_NONE) {
645 LoggerD("fail to downloadBody - err : " << err);
646 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
650 LoggerD("Insert downloadBody request");
651 LoggerD("handle : " << email_handle);
652 SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
653 m_SyncRequests.insert(std::make_pair(email_handle, data));
660 int MailSync::downloadAttachmentInternal(const IEmailPtr& mail, int account_id, const IAttachmentPtr& attachment)
662 LoggerD("downloadAttachmentInternal");
667 int mailId = mail->convertId(mail->getIdRef());
668 int email_handle = 0;
670 email_mail_data_t* result = NULL;
673 error = email_get_mail_data(mailId, &result);
674 if (EMAIL_ERROR_NONE != error) {
675 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
676 "Couldn't find message " << mailId << ". [" << error << "]");
679 LoggerD("Attachment ID :" << attachment->getAttachmentID());
682 std::vector<IAttachmentPtr> attachments = mail->getAttachments();
683 std::vector<IAttachmentPtr> inlineAttachments = mail->getInlineAttachments();
684 unsigned int attachmentSize = attachments.size();
685 unsigned int inlineAttachmentSize = inlineAttachments.size();
687 LoggerD( "attachment sizes = " << attachmentSize << ",inline attachment size : " << inlineAttachmentSize);
688 for ( unsigned int i = 0 ; i < attachmentSize ; i ++)
690 LoggerD( "attachment ID is = " << attachments[i]->getAttachmentID());
691 if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
694 nth = attachments[i]->getNth();
698 for ( unsigned int i = 0 ; i < inlineAttachmentSize ; i ++)
700 LoggerD( "inline attachment ID is = " << inlineAttachments[i]->getAttachmentID());
701 if (inlineAttachments[i]->getAttachmentID() == attachment->getAttachmentID())
704 nth = inlineAttachments[i]->getNth();
709 LoggerD("nth = " << nth);
710 LoggerD("attachment Order Index = " << nth);
711 err = email_download_attachment(mailId, nth , &email_handle);
712 if (err != EMAIL_ERROR_NONE) {
713 LoggerD("fail to downloadAttachment - err : " << err);
714 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
718 LoggerD("Insert downloadAttachment request");
719 LoggerD("handle : " << email_handle);
720 SyncRequestData data = SyncRequestData( email_handle,
721 MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
722 m_SyncRequests.insert(std::make_pair(email_handle, data));
726 if ( EMAIL_ERROR_NONE != email_free_mail_data( &result , 1) )
727 LoggerD("fail to email_free_mail_data - err ");
732 void MailSync::cancelEmailJobInternal(int accountId, int handle)
734 LoggerD("cancel Email Job, account : " <<accountId << " handle : " << handle);
736 int error = email_cancel_job(accountId, handle,EMAIL_CANCELED_BY_USER);
737 if (EMAIL_ERROR_NONE != error) {
738 LoggerD("error code : " << error );
739 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
740 "Couldn't cancel email job handle: " << handle);
743 m_SyncRequests.erase(handle);