4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File: email-daemon-mail.c
26 * Desc: email-daemon Mail Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
36 #include <sys/types.h>
43 #include "email-internal-types.h"
44 #include "email-daemon.h"
45 #include "email-core-event.h"
46 #include "email-daemon-account.h"
47 #include "email-debug-log.h"
48 #include "email-storage.h"
49 #include "email-utilities.h"
50 #include "email-core-account.h"
51 #include "email-core-mail.h"
52 #include "email-core-mailbox.h"
53 #include "email-core-utils.h"
54 #include "email-core-smtp.h"
55 #include "email-core-timer.h"
56 #include "email-core-signal.h"
58 #ifdef __FEATURE_LOCAL_ACTIVITY__
59 extern int g_local_activity_run;
60 extern int g_save_local_activity_run;
62 static int _emdaemon_check_mail_id(int mail_id, int* err_code);
64 INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
66 EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p], err_code[%p]", mail_id, handle, err_code);
68 int ret = false, err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
69 int result_handle = 0, account_id = 0;
70 email_event_t event_data = { 0, };
71 emstorage_mail_tbl_t* mail_table_data = NULL;
72 emstorage_mailbox_tbl_t* local_mailbox = NULL;
73 email_account_t* ref_account = NULL;
74 int dst_mailbox_id = 0;
77 EM_DEBUG_EXCEPTION("mail_id is not valid");
78 err= EMAIL_ERROR_INVALID_PARAM;
82 if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
83 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
87 if (!mail_table_data || mail_table_data->mailbox_id <= 0 || mail_table_data->account_id <= 0) {
88 if (mail_table_data != NULL)
89 EM_DEBUG_EXCEPTION(" mail_table_data->mailbox_id[%d], mail_table_data->account_id[%d]", mail_table_data->mailbox_id, mail_table_data->account_id);
91 *err_code = EMAIL_ERROR_INVALID_MAILBOX;
93 emstorage_free_mail(&mail_table_data, 1, &err);
97 account_id = mail_table_data->account_id;
99 ref_account = emcore_get_account_reference(account_id);
102 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
103 err = EMAIL_ERROR_INVALID_ACCOUNT;
107 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
108 if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err)) {
109 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
112 dst_mailbox_id = local_mailbox->mailbox_id;
113 if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
114 /* mail is moved to 'OUTBOX' first of all. */
115 if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
116 EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
120 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
122 if(!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
123 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
125 /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
127 if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
128 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
133 event_data.type = EMAIL_EVENT_SEND_MAIL;
134 event_data.account_id = account_id;
135 event_data.event_param_data_4 = mail_id;
136 event_data.event_param_data_5 = mail_table_data->mailbox_id;
138 if (!emcore_insert_event_for_sending_mails(&event_data, &result_handle, &err)) {
139 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
143 #ifdef __FEATURE_LOCAL_ACTIVITY__
144 EM_DEBUG_LOG("Setting g_save_local_activity_run ");
145 g_save_local_activity_run = 1;
149 *handle = result_handle;
155 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
157 if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
158 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
160 EM_SAFE_FREE(event_data.event_param_data_3);
164 emcore_free_account(ref_account);
165 EM_SAFE_FREE(ref_account);
168 if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
169 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
172 emstorage_free_mailbox(&local_mailbox, 1, NULL);
174 if (err_code != NULL)
178 emstorage_free_mail(&mail_table_data, 1, &err);
184 INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err_code)
186 EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
189 int err = EMAIL_ERROR_NONE;
190 email_event_t event_data = { 0 , };
191 email_account_t* ref_account = NULL;
192 char *mailbox_name = NULL;
194 if (account_id <= 0) {
195 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
196 err = EMAIL_ERROR_INVALID_PARAM;
200 ref_account = emcore_get_account_reference(account_id);
203 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
204 err = EMAIL_ERROR_INVALID_ACCOUNT;
208 if (!emstorage_get_mailbox_name_by_mailbox_type(account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
209 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
213 event_data.type = EMAIL_EVENT_SEND_MAIL_SAVED;
214 event_data.account_id = account_id;
215 event_data.event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
217 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
218 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
226 EM_SAFE_FREE(event_data.event_param_data_3);
230 emcore_free_account(ref_account);
231 EM_SAFE_FREE(ref_account);
234 EM_SAFE_FREE(mailbox_name);
243 INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
245 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
247 int err = EMAIL_ERROR_NONE;
249 email_event_t event_data = { 0 };
250 email_account_t* ref_account = NULL;
252 if (!input_mail_data || input_mail_data->account_id <= 0 ||
253 ( ((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to)) {
254 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
255 err = EMAIL_ERROR_INVALID_PARAM;
259 ref_account = emcore_get_account_reference(input_mail_data->account_id);
261 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
262 err = EMAIL_ERROR_INVALID_ACCOUNT;
266 if ((err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
267 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
271 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
272 if ( input_from_eas == 0 && ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
273 event_data.type = EMAIL_EVENT_SAVE_MAIL;
274 event_data.account_id = input_mail_data->account_id;
275 event_data.event_param_data_4 = input_mail_data->mail_id;
277 if (!emcore_insert_event(&event_data, &handle, &err)) {
278 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
287 emcore_free_account(ref_account);
288 EM_SAFE_FREE(ref_account);
291 EM_DEBUG_FUNC_END("err [%d]", err);
296 INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
298 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
300 /* default variable */
302 int err = EMAIL_ERROR_NONE;
304 if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 ) {
306 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
308 err = EMAIL_ERROR_INVALID_PARAM;
312 if (!emcore_add_meeting_request(account_id, input_mailbox_id, meeting_req, &err)) {
313 EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
326 INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbose, int with_attachment, int *handle, int* err_code)
328 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], verbose[%d], with_attachment[%d], handle[%p], err_code[%p]", account_id, mail_id, verbose, with_attachment, handle, err_code);
330 /* default variable */
332 int err = EMAIL_ERROR_NONE;
333 /* mpNewMail_StopAlertSound(); */
335 if (!_emdaemon_check_mail_id(mail_id, &err)) {
336 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
340 email_event_t event_data;
342 memset(&event_data, 0x00, sizeof(email_event_t));
344 event_data.type = EMAIL_EVENT_DOWNLOAD_BODY;
345 event_data.event_param_data_4 = mail_id;
346 event_data.account_id = account_id;
347 event_data.event_param_data_3 = GINT_TO_POINTER(verbose << 1 | with_attachment);
349 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
350 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
351 err = EMAIL_ERROR_DB_FAILURE;
354 #ifdef __FEATURE_LOCAL_ACTIVITY__
355 EM_DEBUG_LOG("Setting g_local_activity_run ");
356 g_local_activity_run = 1;
367 int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachment, int* err_code)
369 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
371 /* default variable */
373 int err = EMAIL_ERROR_NONE;
375 if (!attachment_id || !attachment) {
376 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%d], attachment[%p]\n", attachment_id, attachment);
377 err = EMAIL_ERROR_INVALID_PARAM;
381 if (!emcore_get_attachment_info(attachment_id, attachment, &err) || !attachment) {
382 EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
395 int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, int* err_code)
397 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
399 /* default variable */
401 int err = EMAIL_ERROR_NONE;
404 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
405 err = EMAIL_ERROR_INVALID_PARAM;
409 if (!_emdaemon_check_mail_id(mail_id, &err)) {
410 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
414 if (!emcore_mail_add_attachment(mail_id, attachment, &err)) {
415 EM_DEBUG_EXCEPTION(" emcore_mail_add_attachment failed [%d]", err);
428 int emdaemon_delete_mail_attachment(int attachment_id, int* err_code)
430 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
432 /* default variable */
434 int err = EMAIL_ERROR_NONE;
436 if (!attachment_id) {
437 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
438 err = EMAIL_ERROR_INVALID_PARAM;
442 if (!emcore_delete_mail_attachment(attachment_id, &err)) {
443 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
456 INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int nth, int *handle, int* err_code)
458 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], handle[%p], err_code[%p]", account_id, mail_id, nth, handle, err_code);
461 int err = EMAIL_ERROR_NONE;
464 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
465 err = EMAIL_ERROR_INVALID_PARAM;
469 if (!_emdaemon_check_mail_id(mail_id, &err)) {
470 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
474 email_event_t event_data;
476 memset(&event_data, 0x00, sizeof(email_event_t));
478 event_data.type = EMAIL_EVENT_DOWNLOAD_ATTACHMENT;
479 event_data.account_id = account_id;
480 event_data.event_param_data_4 = mail_id;
481 event_data.event_param_data_5 = nth;
483 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
484 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
488 #ifdef __FEATURE_LOCAL_ACTIVITY__
489 EM_DEBUG_LOG("Setting g_local_activity_run ");
490 g_local_activity_run = 1;
502 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
504 EM_DEBUG_FUNC_BEGIN();
505 return emcore_free_attachment_data(atch_data, 1, err_code);
508 void* thread_func_to_delete_mail(void *thread_argument)
510 EM_DEBUG_FUNC_BEGIN();
511 int err = EMAIL_ERROR_NONE;
512 int *mail_id_list = NULL;
513 int mail_id_count = 0;
516 int noti_param_2 = 0;
518 email_event_t *event_data = (email_event_t*)thread_argument;
520 account_id = event_data->account_id;
521 mail_id_list = (int*)event_data->event_param_data_3;
522 mail_id_count = event_data->event_param_data_4;
523 from_server = event_data->event_param_data_5;
525 if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
526 EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
530 if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
531 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
532 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
538 /* all of members will be freed after using in each event handler */
540 if (from_server != EMAIL_DELETE_LOCAL_AND_SERVER && from_server != EMAIL_DELETE_FROM_SERVER) {
541 EM_SAFE_FREE(event_data->event_param_data_3);
543 EM_SAFE_FREE(event_data);
549 INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, int *handle, int* err_code)
551 EM_DEBUG_FUNC_BEGIN("mailbox_id[%d], mail_ids[%p], mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]", mailbox_id, mail_ids, mail_ids_count, from_server, handle, err_code);
554 int err = EMAIL_ERROR_NONE;
556 int thread_error = 0;
557 email_account_t *ref_account = NULL;
558 email_event_t *event_data = NULL;
559 emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
560 thread_t delete_thread;
562 /* mailbox can be NULL for deleting thread mail. */
563 if (mail_ids_count <= 0) {
564 EM_DEBUG_EXCEPTION("mail_ids_count [%d]", mail_ids_count);
565 err = EMAIL_ERROR_INVALID_PARAM;
569 if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
570 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
574 if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
575 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
576 err = EMAIL_ERROR_OUT_OF_MEMORY;
580 memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
582 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
583 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
584 err = EMAIL_ERROR_OUT_OF_MEMORY;
588 ref_account = emcore_get_account_reference(mailbox_tbl_data->account_id);
591 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
592 err = EMAIL_ERROR_INVALID_ACCOUNT;
596 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
597 from_server = EMAIL_DELETE_LOCALLY;
600 event_data->type = EMAIL_EVENT_DELETE_MAIL;
601 event_data->account_id = mailbox_tbl_data->account_id;
602 event_data->event_param_data_3 = (char*)p;
603 event_data->event_param_data_4 = mail_ids_count;
604 event_data->event_param_data_5 = from_server;
606 THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
607 THREAD_DETACH(delete_thread); /* free resources used for new thread */
612 if (mailbox_tbl_data)
613 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
616 emcore_free_account(ref_account);
617 EM_SAFE_FREE(ref_account);
620 if (thread_error != 0) {
621 EM_SAFE_FREE(event_data);
631 int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
633 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_from_server[%d], handle[%p], err_code[%p]", input_mailbox_id, input_from_server, output_handle, output_err_code);
636 int err = EMAIL_ERROR_NONE;
637 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
638 email_event_t event_data = { 0 };
640 if (!input_mailbox_id) {
641 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
642 err = EMAIL_ERROR_INVALID_PARAM;
646 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
647 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
651 if(!emcore_delete_all_mails_of_mailbox(mailbox_tbl->account_id, input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
652 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
656 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
657 if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
658 event_data.type = EMAIL_EVENT_DELETE_MAIL_ALL;
659 event_data.account_id = mailbox_tbl->account_id;
660 event_data.event_param_data_4 = input_mailbox_id;
661 event_data.event_param_data_5 = input_from_server;
663 if (!emcore_insert_event(&event_data, (int*)output_handle, &err)) {
664 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
668 #ifdef __FEATURE_LOCAL_ACTIVITY__
669 int i, total = 0 , search_handle = 0;
670 int *mail_ids = NULL;
671 emstorage_activity_tbl_t new_activity;
674 if (false == emcore_get_next_activity_id(&activityid,&err)) {
675 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
678 if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
679 EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
685 mail_ids = em_malloc(sizeof(int) * total);
686 if (mail_ids == NULL) {
687 EM_DEBUG_EXCEPTION(" mailloc failed...");
689 err = EMAIL_ERROR_OUT_OF_MEMORY;
693 for (i = 0; i < total; i++) {
694 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
695 EM_DEBUG_EXCEPTION(" emstorage_mail_search_result failed [%d]", err);
698 EM_SAFE_FREE(mail_ids);
702 new_activity.activity_id = activityid;
703 new_activity.activity_type = ACTIVITY_DELETEMAIL;
704 new_activity.mail_id = mail_ids[i];
705 new_activity.server_mailid = NULL;
706 new_activity.src_mbox = mailbox->mailbox_name;
707 new_activity.dest_mbox = NULL;
708 new_activity.account_id = mailbox->account_id;
710 if (! emcore_add_activity(&new_activity, &err))
711 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
715 EM_SAFE_FREE(mail_ids);
717 EM_DEBUG_LOG("Setting g_local_activity_run ");
718 g_local_activity_run = 1;
721 #endif /* __FEATURE_SYNC_CLIENT_TO_SERVER__ */
728 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
731 *output_err_code = err;
736 void* thread_func_to_move_mail(void *thread_argument)
738 EM_DEBUG_FUNC_BEGIN();
739 int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
741 email_event_t *event_data = (email_event_t*)thread_argument;
742 int dst_mailbox_id = 0;
744 /* dst_mailbox_name = (char*)event_data->event_param_data_1; */ /*prevent 33693*/
745 mail_ids = (int*)event_data->event_param_data_3;
746 mail_ids_count = event_data->event_param_data_4;
747 dst_mailbox_id = event_data->event_param_data_5;
748 noti_param_1 = event_data->event_param_data_6;
749 noti_param_2 = event_data->event_param_data_7;
752 if (!emcore_move_mail(mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
753 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
757 if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
758 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
763 /* emcore_free_event(event_data); */ /* all of members will be freed after using in each event handler */
764 EM_SAFE_FREE(event_data);
770 INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailbox_id, int* err_code)
772 EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
775 int err = EMAIL_ERROR_NONE;
782 email_account_t *ref_account = NULL;
783 email_mail_list_item_t *mail_list = NULL;
784 email_event_t *event_data = NULL;
785 thread_t move_thread;
786 emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
787 emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
789 if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0) {
790 EM_DEBUG_EXCEPTION("Invalid Parameter");
791 err = EMAIL_ERROR_INVALID_PARAM;
795 if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
796 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
800 if ((err = emstorage_get_mailbox_by_id(src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
801 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
805 ref_account = emcore_get_account_reference(dst_mailbox_tbl->account_id);
808 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
809 err = EMAIL_ERROR_INVALID_ACCOUNT;
813 if(!emstorage_get_mail_list(src_mailbox_tbl->account_id, src_mailbox_id, NULL, EMAIL_LIST_TYPE_NORMAL, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, false, &mail_list, &select_num, &err)) {
814 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
818 mails = malloc(sizeof(int) * select_num);
821 EM_DEBUG_EXCEPTION("Malloc failed...!");
822 err = EMAIL_ERROR_OUT_OF_MEMORY;
826 memset(mails, 0x00, sizeof(int) * select_num);
828 for(i = 0 ; i < select_num ; i++) {
829 if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
830 mails[num] = mail_list[i].mail_id;
836 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
837 err = EMAIL_ERROR_MAIL_NOT_FOUND;
841 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
842 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
843 err = EMAIL_ERROR_OUT_OF_MEMORY;
847 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
848 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
849 err = EMAIL_ERROR_OUT_OF_MEMORY;
853 memcpy(p, mails, sizeof(int) * num);
855 event_data->account_id = dst_mailbox_tbl->account_id;
856 event_data->type = EMAIL_EVENT_MOVE_MAIL;
857 event_data->event_param_data_3 = (char*)p;
858 event_data->event_param_data_4 = num;
859 event_data->event_param_data_5 = dst_mailbox_id;
860 event_data->event_param_data_8 = src_mailbox_id;
862 #ifdef __FEATURE_LOCAL_ACTIVITY__
863 int i = 0, activityid = 0;
865 if (false == emcore_get_next_activity_id(&activityid,&err))
866 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
868 for (i =0; i < event_data.event_param_data_4; i++) {
869 emstorage_activity_tbl_t new_activity;
870 new_activity.activity_id = activityid;
871 new_activity.activity_type = ACTIVITY_MOVEMAIL;
872 new_activity.account_id = event_data.account_id;
873 new_activity.mail_id = mail_ids[i];
874 new_activity.dest_mbox = event_data.event_param_data_1;
875 new_activity.server_mailid = NULL;
876 new_activity.src_mbox = event_data.event_param_data_2;
878 if (!emcore_add_activity(&new_activity, &err))
879 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
881 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
882 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
883 THREAD_DETACH(move_thread); /* free resources used for new thread */
888 #ifdef __FEATURE_LOCAL_ACTIVITY__
889 EM_DEBUG_LOG("Setting g_local_activity_run ");
890 g_local_activity_run = 1;
891 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
897 emcore_free_account(ref_account);
898 EM_SAFE_FREE(ref_account);
902 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
905 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
908 EM_SAFE_FREE(mail_list);
914 EM_DEBUG_FUNC_END("ret [%d]", ret);
918 INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
920 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
922 /* default variable */
923 int mail_id = 0, *p = NULL, thread_error;
924 int ret = false, err = EMAIL_ERROR_NONE;
925 emstorage_mail_tbl_t* mail_table_data = NULL;
926 email_account_t* ref_account = NULL;
927 email_event_t *event_data = NULL;
928 thread_t move_thread;
929 emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
930 int src_mailbox_id = 0;
932 if (num <= 0 || dst_mailbox_id <= 0) {
933 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
934 err = EMAIL_ERROR_INVALID_PARAM;
938 if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
939 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
943 ref_account = emcore_get_account_reference(dest_mailbox_tbl->account_id);
946 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
947 err = EMAIL_ERROR_INVALID_ACCOUNT;
951 /* Getting source mailbox name */
952 mail_id = mail_ids[0];
954 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
955 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
960 if (src_mailbox_id == 0)
961 src_mailbox_id = mail_table_data->mailbox_id;
963 emstorage_free_mail(&mail_table_data, 1, NULL);
965 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
966 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
967 err = EMAIL_ERROR_OUT_OF_MEMORY;
971 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
972 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
973 err = EMAIL_ERROR_OUT_OF_MEMORY;
977 memcpy(p, mail_ids, sizeof(int) * num);
979 event_data->account_id = dest_mailbox_tbl->account_id;
980 event_data->type = EMAIL_EVENT_MOVE_MAIL;
981 event_data->event_param_data_3 = (char*)p;
982 event_data->event_param_data_4 = num;
983 event_data->event_param_data_5 = dst_mailbox_id;
984 event_data->event_param_data_8 = src_mailbox_id;
985 event_data->event_param_data_6 = noti_param_1;
986 event_data->event_param_data_7 = noti_param_2;
988 #ifdef __FEATURE_LOCAL_ACTIVITY__
989 int i = 0, activityid = 0;
991 if (false == emcore_get_next_activity_id(&activityid,&err))
992 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
994 for (i =0; i < event_data.event_param_data_4; i++) {
995 emstorage_activity_tbl_t new_activity;
996 new_activity.activity_id = activityid;
997 new_activity.activity_type = ACTIVITY_MOVEMAIL;
998 new_activity.account_id = event_data.account_id;
999 new_activity.mail_id = mail_ids[i];
1000 new_activity.dest_mbox = event_data.event_param_data_1;
1001 new_activity.server_mailid = NULL;
1002 new_activity.src_mbox = event_data.event_param_data_2;
1004 if (!emcore_add_activity(&new_activity, &err))
1005 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
1007 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1008 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
1009 THREAD_DETACH(move_thread); /* free resources used for new thread */
1014 #ifdef __FEATURE_LOCAL_ACTIVITY__
1015 EM_DEBUG_LOG("Setting g_local_activity_run ");
1016 g_local_activity_run = 1;
1017 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1023 emcore_free_account(ref_account);
1024 EM_SAFE_FREE(ref_account);
1027 if (dest_mailbox_tbl)
1028 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1030 EM_DEBUG_FUNC_END("ret [%d]", ret);
1034 INTERNAL_FUNC int emdaemon_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int onserver, int* err_code)
1036 EM_DEBUG_FUNC_BEGIN("mail_ids[%d], num[%d], field_type [%d], value[%d], err_code[%p]", mail_ids[0], num, field_type, value, err_code);
1038 int ret = false, err = EMAIL_ERROR_NONE;
1039 emstorage_account_tbl_t *account_tbl = NULL;
1041 if(account_id <= 0 || !mail_ids || num <= 0) {
1042 err = EMAIL_ERROR_INVALID_PARAM;
1043 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1047 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
1048 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
1052 if (!emcore_set_flags_field(account_id, mail_ids, num, field_type, value, &err)) {
1053 EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
1057 if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
1058 int *mail_id_array = NULL;
1059 email_event_t event_data = {0};
1062 mail_id_array = em_malloc(sizeof(int) * num);
1064 if (mail_id_array == NULL) {
1065 EM_DEBUG_EXCEPTION("em_malloc failed...");
1066 err = EMAIL_ERROR_OUT_OF_MEMORY;
1070 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1072 event_data.type = EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER;
1073 event_data.account_id = account_id;
1074 event_data.event_param_data_1 = NULL;
1075 event_data.event_param_data_3 = (char*)mail_id_array;
1076 event_data.event_param_data_4 = num;
1077 event_data.event_param_data_5 = field_type;
1078 event_data.event_param_data_6 = value;
1080 if (!emcore_insert_event(&event_data, (int*)&handle, &err)) {
1081 EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1091 emstorage_free_account(&account_tbl, 1, NULL);
1095 EM_DEBUG_FUNC_END();
1100 INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
1102 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
1104 int err = EMAIL_ERROR_NONE;
1106 email_event_t event_data = { 0 };
1107 email_account_t *ref_account = NULL;
1109 if (!input_mail_data || input_mail_data->account_id <= 0 || input_mail_data->mail_id == 0 ||
1110 (((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to)) {
1111 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1112 err = EMAIL_ERROR_INVALID_PARAM;
1116 ref_account = emcore_get_account_reference(input_mail_data->account_id);
1118 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
1119 err = EMAIL_ERROR_INVALID_ACCOUNT;
1123 if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
1124 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1128 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1129 if ( input_from_eas == 0) {
1130 event_data.type = EMAIL_EVENT_UPDATE_MAIL;
1131 event_data.account_id = input_mail_data->account_id;
1132 event_data.event_param_data_1 = (char*)input_mail_data;
1133 event_data.event_param_data_2 = (char*)input_attachment_data_list;
1134 event_data.event_param_data_3 = (char*)input_meeting_request;
1135 event_data.event_param_data_4 = input_attachment_count;
1136 event_data.event_param_data_5 = input_from_eas;
1138 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1139 EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
1140 err = EMAIL_ERROR_NONE;
1149 emcore_free_account(ref_account);
1150 EM_SAFE_FREE(ref_account);
1153 EM_DEBUG_FUNC_END("err [%d]", err);
1158 int _emdaemon_check_mail_id(int mail_id, int* err_code)
1160 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1163 int err = EMAIL_ERROR_NONE;
1165 emstorage_mail_tbl_t* mail = NULL;
1167 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail, true, &err)) {
1168 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1176 emstorage_free_mail(&mail, 1, NULL);
1178 if (err_code != NULL)
1180 EM_DEBUG_FUNC_END();
1185 INTERNAL_FUNC int emdaemon_send_mail_retry(int mail_id, int timeout_in_sec, int* err_code)
1188 int err = EMAIL_ERROR_NONE;
1189 long nTimerValue = 0;
1190 char mail_id_string[10] = { 0, };
1192 if (!_emdaemon_check_mail_id(mail_id, &err)) {
1193 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1197 if ( timeout_in_sec == 0 ) {
1198 if(!emdaemon_send_mail(mail_id, NULL, &err)) {
1199 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1203 else if ( timeout_in_sec > 0 ) {
1204 sprintf(mail_id_string,"%d",mail_id);
1205 nTimerValue = timeout_in_sec * 1000;
1206 if ( emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)mail_id_string) <= 0) {
1207 EM_DEBUG_EXCEPTION("Failed to start timer");
1221 INTERNAL_FUNC void _OnMailSendRetryTimerCB( void* data )
1223 EM_DEBUG_FUNC_BEGIN();
1224 int err = EMAIL_ERROR_NONE;
1228 EM_DEBUG_LOG("Invalid param");
1232 mail_id = atoi((char*)data);
1234 if (!_emdaemon_check_mail_id(mail_id, &err)) {
1235 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1239 if(!emdaemon_send_mail(mail_id, NULL, &err)) {
1240 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1246 EM_DEBUG_FUNC_END();
1250 INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
1252 EM_DEBUG_FUNC_BEGIN("thread_id [%d], target_mailbox_id [%d], move_always_flag [%d], err_code [%p]", thread_id, target_mailbox_id, move_always_flag, err_code);
1254 int err = EMAIL_ERROR_NONE;
1255 int *mail_id_list = NULL, result_count = 0, i, mailbox_count = 0, account_id;
1256 email_mail_list_item_t *mail_list = NULL;
1257 email_mailbox_t *target_mailbox_list = NULL, *target_mailbox = NULL;
1258 char mailbox_id_param_string[10] = {0,};
1260 if (target_mailbox_id == 0) {
1261 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1262 err = EMAIL_ERROR_INVALID_PARAM;
1266 if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1267 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1272 mail_id_list = em_malloc(sizeof(int) * result_count);
1274 if (mail_id_list == NULL) {
1275 EM_DEBUG_EXCEPTION("em_malloc failed...");
1276 err = EMAIL_ERROR_OUT_OF_MEMORY;
1280 for(i = 0; i < result_count; i++) {
1281 mail_id_list[i] = mail_list[i].mail_id;
1283 account_id = mail_list[0].account_id;
1286 if (!emcore_get_mailbox_list(account_id, &target_mailbox_list, &mailbox_count, &err)) {
1287 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
1291 for(i = 0; i < mailbox_count; i++) {
1292 EM_DEBUG_LOG("%s %d", target_mailbox_list[i].mailbox_name, target_mailbox_id);
1293 if(target_mailbox_list[i].mailbox_id == target_mailbox_id) {
1294 target_mailbox = (target_mailbox_list + i);
1299 if(!target_mailbox) {
1300 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1304 if (!emdaemon_move_mail(mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
1305 EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
1309 SNPRINTF(mailbox_id_param_string, 10, "%d", target_mailbox->mailbox_id);
1310 if (!emcore_notify_storage_event(NOTI_THREAD_MOVE, account_id, thread_id, mailbox_id_param_string, move_always_flag))
1311 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1316 emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1317 EM_SAFE_FREE(mail_list);
1318 EM_SAFE_FREE(mail_id_list);
1322 EM_DEBUG_FUNC_END();
1326 INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_flag, int *handle, int *err_code)
1328 EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1330 int err = EMAIL_ERROR_NONE;
1332 int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1333 email_mail_list_item_t *mail_list = NULL;
1335 if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_NAME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1336 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1341 mail_id_list = em_malloc(sizeof(int) * result_count);
1343 if (mail_id_list == NULL) {
1344 EM_DEBUG_EXCEPTION("em_malloc failed...");
1345 err = EMAIL_ERROR_OUT_OF_MEMORY;
1349 for(i = 0; i < result_count; i++) {
1350 mail_id_list[i] = mail_list[i].mail_id;
1353 account_id = mail_list[0].account_id;
1354 mailbox_id = mail_list[0].mailbox_id;
1356 // should remove requiring of mailbox information from this function.
1357 // email-service should find mailboxes itself by its mail id.
1358 if (!emdaemon_delete_mail(mailbox_id, mail_id_list, result_count, false, handle, &err)) {
1359 EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
1363 if (!emcore_notify_storage_event(NOTI_THREAD_DELETE, account_id, thread_id, NULL, delete_always_flag))
1364 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_THREAD_DELETE] >>>> ");
1369 EM_SAFE_FREE(mail_list);
1370 EM_SAFE_FREE(mail_id_list);
1374 EM_DEBUG_FUNC_END();
1378 INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
1380 EM_DEBUG_FUNC_BEGIN("thread_id [%d], seen_flag [%d], on_server [%d], handle [%p], err_code [%p]", thread_id, seen_flag, on_server, handle, err_code);
1382 int err = EMAIL_ERROR_NONE;
1383 int account_id, *mail_id_list = NULL, result_count = 0, i;
1384 email_mail_list_item_t *mail_list = NULL;
1386 if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_NAME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1387 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1392 mail_id_list = em_malloc(sizeof(int) * result_count);
1394 if (mail_id_list == NULL) {
1395 EM_DEBUG_EXCEPTION("em_malloc failed...");
1396 err = EMAIL_ERROR_OUT_OF_MEMORY;
1400 for(i = 0; i < result_count; i++) {
1401 mail_id_list[i] = mail_list[i].mail_id;
1404 account_id = mail_list[0].account_id;
1406 if (!emdaemon_set_flags_field(account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
1407 EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
1411 if (!emcore_notify_storage_event(NOTI_THREAD_MODIFY_SEEN_FLAG, account_id, thread_id, NULL, seen_flag))
1412 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1417 EM_SAFE_FREE(mail_list);
1418 EM_SAFE_FREE(mail_id_list);
1422 EM_DEBUG_FUNC_END();
1426 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle)
1428 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
1429 int err = EMAIL_ERROR_NONE;
1431 email_event_t event_data = { 0 };
1432 email_account_t *ref_account = NULL;
1433 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1435 if (input_mailbox_id <= 0) {
1436 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1437 err = EMAIL_ERROR_INVALID_PARAM;
1441 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
1442 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1446 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
1448 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
1449 err = EMAIL_ERROR_INVALID_ACCOUNT;
1453 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1454 if(input_on_server) {
1455 event_data.type = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
1456 event_data.account_id = mailbox_tbl->account_id;
1457 event_data.event_param_data_4 = input_mailbox_id;
1459 if (!emcore_insert_event(&event_data, &handle, &err)) {
1460 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1466 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1467 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1474 emcore_free_account(ref_account);
1475 EM_SAFE_FREE(ref_account);
1479 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1481 EM_DEBUG_FUNC_END("err [%d]", err);