4 * Copyright (c) 2000 - 2011 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"
57 #ifdef __FEATURE_LOCAL_ACTIVITY__
58 extern int g_local_activity_run;
59 extern int g_save_local_activity_run;
61 static int _emdaemon_check_mail_id(int mail_id, int* err_code);
63 INTERNAL_FUNC int emdaemon_send_mail(int mail_id, email_option_t* sending_option, unsigned* handle, int* err_code)
65 EM_DEBUG_FUNC_BEGIN("mail_id[%d], sending_option[%p], handle[%p], err_code[%p]", mail_id, sending_option, handle, err_code);
67 int ret = false, err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
68 int result_handle = 0, account_id = 0;
69 email_option_t* option = NULL;
70 email_event_t event_data;
71 emstorage_mail_tbl_t* mail_table_data = NULL;
72 emstorage_mailbox_tbl_t* local_mailbox = NULL;
73 int dst_mailbox_id = 0;
76 EM_DEBUG_EXCEPTION("mail_id is not valid");
77 err= EMAIL_ERROR_INVALID_PARAM;
81 if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
82 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
86 if (!mail_table_data || mail_table_data->mailbox_id <= 0 || mail_table_data->account_id <= 0) {
87 if (mail_table_data != NULL)
88 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);
90 *err_code = EMAIL_ERROR_INVALID_MAILBOX;
94 account_id = mail_table_data->account_id;
96 if (sending_option != NULL) {
97 if (!(option = (email_option_t*)em_malloc(sizeof(email_option_t)))) {
98 EM_DEBUG_EXCEPTION("em_malloc for sending_option failed...");
99 err = EMAIL_ERROR_OUT_OF_MEMORY;
102 memcpy(option, sending_option, sizeof(email_option_t));
103 option->display_name_from = EM_SAFE_STRDUP(sending_option->display_name_from);
106 email_account_t* ref_account = emdaemon_get_account_reference(account_id);
109 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", account_id);
110 err = EMAIL_ERROR_INVALID_ACCOUNT;
114 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
115 if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err)) {
116 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
119 dst_mailbox_id = local_mailbox->mailbox_id;
120 if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
121 /* mail is moved to 'OUTBOX' first of all. */
122 if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
123 EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
127 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
129 if(!emstorage_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
130 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
132 /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
134 if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
135 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
140 memset(&event_data, 0x00, sizeof(email_event_t));
141 event_data.type = EMAIL_EVENT_SEND_MAIL;
142 event_data.account_id = account_id;
143 event_data.event_param_data_1 = (char*)option;
144 event_data.event_param_data_4 = mail_id;
145 event_data.event_param_data_5 = mail_table_data->mailbox_id;
147 if (!emcore_insert_event_for_sending_mails(&event_data, &result_handle, &err)) {
148 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
152 #ifdef __FEATURE_LOCAL_ACTIVITY__
153 EM_DEBUG_LOG("Setting g_save_local_activity_run ");
154 g_save_local_activity_run = 1;
158 *handle = result_handle;
164 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
166 if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
167 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
169 EM_SAFE_FREE(event_data.event_param_data_3);
172 EM_SAFE_FREE(option->display_name_from);
173 EM_SAFE_FREE(option);
177 if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
178 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
181 emstorage_free_mailbox(&local_mailbox, 1, NULL);
183 if (err_code != NULL)
186 if(mail_table_data) {
187 emstorage_free_mail(&mail_table_data, 1, &err);
194 INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, email_option_t* sending_option, unsigned* handle, int* err_code)
196 EM_DEBUG_FUNC_BEGIN("account_id[%d], sending_option[%p], handle[%p], err_code[%p]", account_id, sending_option, handle, err_code);
199 int err = EMAIL_ERROR_NONE;
200 email_option_t* option = NULL;
201 email_event_t event_data;
202 char *mailbox_name = NULL;
204 memset(&event_data, 0x00, sizeof(email_event_t));
206 if (account_id <= 0) {
207 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
208 err = EMAIL_ERROR_INVALID_PARAM;
212 if (sending_option) {
213 option = (email_option_t*)em_malloc(sizeof(email_option_t));
215 EM_DEBUG_EXCEPTION("em_malloc failed...");
216 err = EMAIL_ERROR_OUT_OF_MEMORY;
220 memcpy(option, sending_option, sizeof(email_option_t));
223 email_account_t* ref_account = emdaemon_get_account_reference(account_id);
226 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", account_id);
227 err = EMAIL_ERROR_INVALID_ACCOUNT;
232 event_data.type = EMAIL_EVENT_SEND_MAIL_SAVED;
234 if (!emstorage_get_mailbox_name_by_mailbox_type(account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
235 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
238 event_data.event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
239 event_data.account_id = account_id;
240 event_data.event_param_data_1 = (char*)option;
242 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
243 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
251 EM_SAFE_FREE(event_data.event_param_data_3);
252 EM_SAFE_FREE(option);
255 EM_SAFE_FREE(mailbox_name);
264 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)
266 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);
268 int err = EMAIL_ERROR_NONE;
270 email_event_t event_data = { 0 };
272 if (!input_mail_data || input_mail_data->account_id <= 0 ||
273 (input_mail_data->report_status == EMAIL_MAIL_REPORT_MDN && !input_mail_data->full_address_to)) {
274 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
275 err = EMAIL_ERROR_INVALID_PARAM;
279 email_account_t* ref_account = emdaemon_get_account_reference(input_mail_data->account_id);
281 EM_DEBUG_LOG(" emdaemon_get_account_reference failed [%d]", input_mail_data->account_id);
282 err = EMAIL_ERROR_INVALID_ACCOUNT;
286 if ((err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
287 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
291 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
292 if ( input_from_eas == 0) {
293 event_data.type = EMAIL_EVENT_SAVE_MAIL;
294 event_data.account_id = input_mail_data->account_id;
295 event_data.event_param_data_4 = input_mail_data->mail_id;
297 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
298 EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
305 EM_DEBUG_FUNC_END("err [%d]", err);
310 INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
312 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);
314 /* default variable */
316 int err = EMAIL_ERROR_NONE;
318 if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 ) {
320 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
322 err = EMAIL_ERROR_INVALID_PARAM;
326 if (!emcore_add_meeting_request(account_id, input_mailbox_id, meeting_req, &err)) {
327 EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
340 INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbose, int with_attachment, unsigned* handle, int* err_code)
342 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);
344 /* default variable */
346 int err = EMAIL_ERROR_NONE;
347 /* mpNewMail_StopAlertSound(); */
349 if (!_emdaemon_check_mail_id(mail_id, &err)) {
350 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
354 email_event_t event_data;
356 memset(&event_data, 0x00, sizeof(email_event_t));
358 event_data.type = EMAIL_EVENT_DOWNLOAD_BODY;
359 event_data.event_param_data_4 = mail_id;
360 event_data.account_id = account_id;
361 event_data.event_param_data_3 = GINT_TO_POINTER(verbose << 1 | with_attachment);
363 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
364 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
365 err = EMAIL_ERROR_DB_FAILURE;
368 #ifdef __FEATURE_LOCAL_ACTIVITY__
369 EM_DEBUG_LOG("Setting g_local_activity_run ");
370 g_local_activity_run = 1;
381 int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachment, int* err_code)
383 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
385 /* default variable */
387 int err = EMAIL_ERROR_NONE;
389 if (!attachment_id || !attachment) {
390 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%d], attachment[%p]\n", attachment_id, attachment);
391 err = EMAIL_ERROR_INVALID_PARAM;
395 if (!emcore_get_attachment_info(attachment_id, attachment, &err) || !attachment) {
396 EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
409 int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, int* err_code)
411 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
413 /* default variable */
415 int err = EMAIL_ERROR_NONE;
418 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
419 err = EMAIL_ERROR_INVALID_PARAM;
423 if (!_emdaemon_check_mail_id(mail_id, &err)) {
424 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
428 if (!emcore_mail_add_attachment(mail_id, attachment, &err)) {
429 EM_DEBUG_EXCEPTION(" emcore_mail_add_attachment failed [%d]", err);
442 int emdaemon_delete_mail_attachment(int attachment_id, int* err_code)
444 EM_DEBUG_FUNC_BEGIN("attachment_id[%s], err_code[%p]", attachment_id, err_code);
446 /* default variable */
448 int err = EMAIL_ERROR_NONE;
450 if (!attachment_id) {
451 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
452 err = EMAIL_ERROR_INVALID_PARAM;
456 if (!emcore_delete_mail_attachment(attachment_id, &err)) {
457 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
470 INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int nth, unsigned* handle, int* err_code)
472 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);
475 int err = EMAIL_ERROR_NONE;
478 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
479 err = EMAIL_ERROR_INVALID_PARAM;
483 if (!_emdaemon_check_mail_id(mail_id, &err)) {
484 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
488 email_event_t event_data;
490 memset(&event_data, 0x00, sizeof(email_event_t));
492 event_data.type = EMAIL_EVENT_DOWNLOAD_ATTACHMENT;
493 event_data.account_id = account_id;
494 event_data.event_param_data_4 = mail_id;
495 event_data.event_param_data_5 = nth;
497 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
498 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
502 #ifdef __FEATURE_LOCAL_ACTIVITY__
503 EM_DEBUG_LOG("Setting g_local_activity_run ");
504 g_local_activity_run = 1;
516 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
518 EM_DEBUG_FUNC_BEGIN();
519 return emcore_free_attachment_data(atch_data, 1, err_code);
522 void* thread_func_to_delete_mail(void *thread_argument)
524 EM_DEBUG_FUNC_BEGIN();
525 int err = EMAIL_ERROR_NONE;
526 int *mail_id_list = NULL;
527 int mail_id_count = 0;
531 email_event_t *event_data = (email_event_t*)thread_argument;
533 account_id = event_data->account_id;
534 mail_id_list = (int*)event_data->event_param_data_3;
535 mail_id_count = event_data->event_param_data_4;
536 from_server = event_data->event_param_data_5;
538 if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, false, &err)) {
539 EM_DEBUG_EXCEPTION(" emcore_delete_mail falied [%d]", err);
543 if (from_server != EMAIL_DELETE_LOCALLY) {
544 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
545 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
551 /* emcore_free_event(event_data); */ /* all of members will be freed after using in each event handler */
552 EM_SAFE_FREE(event_data);
558 INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, unsigned* handle, int* err_code)
560 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);
563 int err = EMAIL_ERROR_NONE;
564 int* p = NULL, thread_error;
565 email_event_t *event_data = NULL;
566 emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
567 thread_t delete_thread;
569 /* mailbox can be NULL for deleting thread mail. */
570 if (mail_ids_count <= 0) {
571 EM_DEBUG_EXCEPTION("mail_ids_count [%d]", mail_ids_count);
572 err = EMAIL_ERROR_INVALID_PARAM;
576 if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
577 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
581 if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
582 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
583 err = EMAIL_ERROR_OUT_OF_MEMORY;
587 memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
589 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
590 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
591 err = EMAIL_ERROR_OUT_OF_MEMORY;
595 event_data->type = EMAIL_EVENT_DELETE_MAIL;
596 event_data->account_id = mailbox_tbl_data->account_id;
597 event_data->event_param_data_3 = (char*)p;
598 event_data->event_param_data_4 = mail_ids_count;
599 event_data->event_param_data_5 = from_server;
601 THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
602 THREAD_DETACH(delete_thread); /* free resources used for new thread */
607 if (mailbox_tbl_data)
608 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
619 int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, unsigned *output_handle, int *output_err_code)
621 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);
624 int err = EMAIL_ERROR_NONE;
625 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
626 email_event_t event_data = { 0 };
628 if (!input_mailbox_id) {
629 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
630 err = EMAIL_ERROR_INVALID_PARAM;
634 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
635 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
639 if(!emcore_delete_all_mails_of_mailbox(input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
640 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
644 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
645 if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
646 event_data.type = EMAIL_EVENT_DELETE_MAIL_ALL;
647 event_data.account_id = mailbox_tbl->account_id;
648 event_data.event_param_data_4 = input_mailbox_id;
649 event_data.event_param_data_5 = input_from_server;
651 if (!emcore_insert_event(&event_data, (int*)output_handle, &err)) {
652 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
656 #ifdef __FEATURE_LOCAL_ACTIVITY__
657 int i, total = 0 , search_handle = 0;
658 int *mail_ids = NULL;
659 emstorage_activity_tbl_t new_activity;
662 if (false == emcore_get_next_activity_id(&activityid,&err)) {
663 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
666 if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
667 EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
673 mail_ids = em_malloc(sizeof(int) * total);
674 if (mail_ids == NULL) {
675 EM_DEBUG_EXCEPTION(" mailloc failed...");
677 err = EMAIL_ERROR_OUT_OF_MEMORY;
681 for (i = 0; i < total; i++) {
682 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
683 EM_DEBUG_EXCEPTION(" emstorage_mail_search_result failed [%d]", err);
686 EM_SAFE_FREE(mail_ids);
690 new_activity.activity_id = activityid;
691 new_activity.activity_type = ACTIVITY_DELETEMAIL;
692 new_activity.mail_id = mail_ids[i];
693 new_activity.server_mailid = NULL;
694 new_activity.src_mbox = mailbox->mailbox_name;
695 new_activity.dest_mbox = NULL;
696 new_activity.account_id = mailbox->account_id;
698 if (! emcore_add_activity(&new_activity, &err))
699 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
703 EM_SAFE_FREE(mail_ids);
705 EM_DEBUG_LOG("Setting g_local_activity_run ");
706 g_local_activity_run = 1;
709 #endif /* __FEATURE_SYNC_CLIENT_TO_SERVER__ */
716 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
719 *output_err_code = err;
724 void* thread_func_to_move_mail(void *thread_argument)
726 EM_DEBUG_FUNC_BEGIN();
727 int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
729 email_event_t *event_data = (email_event_t*)thread_argument;
730 char *dst_mailbox_name = NULL;
731 int dst_mailbox_id = 0;
733 dst_mailbox_name = (char*)event_data->event_param_data_1;
734 mail_ids = (int*)event_data->event_param_data_3;
735 mail_ids_count = event_data->event_param_data_4;
736 dst_mailbox_id = event_data->event_param_data_5;
737 noti_param_1 = event_data->event_param_data_6;
738 noti_param_2 = event_data->event_param_data_7;
741 if (!emcore_move_mail(mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
742 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
746 if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
747 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
752 /* emcore_free_event(event_data); */ /* all of members will be freed after using in each event handler */
753 EM_SAFE_FREE(event_data);
759 INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailbox_id, int* err_code)
761 EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
764 int err = EMAIL_ERROR_NONE;
771 email_account_t *ref_account = NULL;
772 email_mail_list_item_t *mail_list = NULL;
773 email_event_t *event_data = NULL;
774 thread_t move_thread;
775 emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
776 emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
778 if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0) {
779 EM_DEBUG_EXCEPTION("Invalid Parameter");
780 err = EMAIL_ERROR_INVALID_PARAM;
784 if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
785 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
789 if ((err = emstorage_get_mailbox_by_id(src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
790 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
794 ref_account = emdaemon_get_account_reference(dst_mailbox_tbl->account_id);
797 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
798 err = EMAIL_ERROR_INVALID_ACCOUNT;
802 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)) {
803 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
807 mails = malloc(sizeof(int) * select_num);
810 EM_DEBUG_EXCEPTION("Malloc failed...!");
811 err = EMAIL_ERROR_OUT_OF_MEMORY;
815 memset(mails, 0x00, sizeof(int) * select_num);
817 for(i = 0 ; i < select_num ; i++) {
818 if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
819 mails[num] = mail_list[i].mail_id;
825 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
826 err = EMAIL_ERROR_MAIL_NOT_FOUND;
830 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
831 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
832 err = EMAIL_ERROR_OUT_OF_MEMORY;
836 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
837 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
838 err = EMAIL_ERROR_OUT_OF_MEMORY;
842 memcpy(p, mails, sizeof(int) * num);
844 event_data->account_id = dst_mailbox_tbl->account_id;
845 event_data->type = EMAIL_EVENT_MOVE_MAIL;
846 event_data->event_param_data_3 = (char*)p;
847 event_data->event_param_data_4 = num;
848 event_data->event_param_data_5 = dst_mailbox_id;
849 event_data->event_param_data_8 = src_mailbox_id;
851 #ifdef __FEATURE_LOCAL_ACTIVITY__
852 int i = 0, activityid = 0;
854 if (false == emcore_get_next_activity_id(&activityid,&err))
855 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
857 for (i =0; i < event_data.event_param_data_4; i++) {
858 emstorage_activity_tbl_t new_activity;
859 new_activity.activity_id = activityid;
860 new_activity.activity_type = ACTIVITY_MOVEMAIL;
861 new_activity.account_id = event_data.account_id;
862 new_activity.mail_id = mail_ids[i];
863 new_activity.dest_mbox = event_data.event_param_data_1;
864 new_activity.server_mailid = NULL;
865 new_activity.src_mbox = event_data.event_param_data_2;
867 if (!emcore_add_activity(&new_activity, &err))
868 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
870 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
871 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
872 THREAD_DETACH(move_thread); /* free resources used for new thread */
877 #ifdef __FEATURE_LOCAL_ACTIVITY__
878 EM_DEBUG_LOG("Setting g_local_activity_run ");
879 g_local_activity_run = 1;
880 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
886 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
889 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
892 EM_SAFE_FREE(mail_list);
898 EM_DEBUG_FUNC_END("ret [%d]", ret);
902 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)
904 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
906 /* default variable */
907 int mail_id = 0, *p = NULL, thread_error;
908 int ret = false, err = EMAIL_ERROR_NONE;
909 emstorage_mail_tbl_t* mail_table_data = NULL;
910 email_account_t* ref_account = NULL;
911 email_event_t *event_data = NULL;
912 thread_t move_thread;
913 emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
914 int src_mailbox_id = 0;
916 if (num <= 0 || dst_mailbox_id <= 0) {
917 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
918 err = EMAIL_ERROR_INVALID_PARAM;
922 if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
923 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
927 ref_account = emdaemon_get_account_reference(dest_mailbox_tbl->account_id);
930 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
931 err = EMAIL_ERROR_INVALID_ACCOUNT;
935 /* Getting source mailbox name */
936 mail_id = mail_ids[0];
938 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
939 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
944 if (src_mailbox_id == 0)
945 src_mailbox_id = mail_table_data->mailbox_id;
947 emstorage_free_mail(&mail_table_data, 1, NULL);
949 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
950 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
951 err = EMAIL_ERROR_OUT_OF_MEMORY;
955 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
956 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
957 err = EMAIL_ERROR_OUT_OF_MEMORY;
961 memcpy(p, mail_ids, sizeof(int) * num);
963 event_data->account_id = dest_mailbox_tbl->account_id;
964 event_data->type = EMAIL_EVENT_MOVE_MAIL;
965 event_data->event_param_data_3 = (char*)p;
966 event_data->event_param_data_4 = num;
967 event_data->event_param_data_5 = dst_mailbox_id;
968 event_data->event_param_data_8 = src_mailbox_id;
969 event_data->event_param_data_6 = noti_param_1;
970 event_data->event_param_data_7 = noti_param_2;
972 #ifdef __FEATURE_LOCAL_ACTIVITY__
973 int i = 0, activityid = 0;
975 if (false == emcore_get_next_activity_id(&activityid,&err))
976 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
978 for (i =0; i < event_data.event_param_data_4; i++) {
979 emstorage_activity_tbl_t new_activity;
980 new_activity.activity_id = activityid;
981 new_activity.activity_type = ACTIVITY_MOVEMAIL;
982 new_activity.account_id = event_data.account_id;
983 new_activity.mail_id = mail_ids[i];
984 new_activity.dest_mbox = event_data.event_param_data_1;
985 new_activity.server_mailid = NULL;
986 new_activity.src_mbox = event_data.event_param_data_2;
988 if (!emcore_add_activity(&new_activity, &err))
989 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
991 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
992 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
993 THREAD_DETACH(move_thread); /* free resources used for new thread */
998 #ifdef __FEATURE_LOCAL_ACTIVITY__
999 EM_DEBUG_LOG("Setting g_local_activity_run ");
1000 g_local_activity_run = 1;
1001 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1006 if (dest_mailbox_tbl)
1007 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1009 EM_DEBUG_FUNC_END("ret [%d]", ret);
1013 INTERNAL_FUNC int emdaemon_modify_flag(int mail_id, email_mail_flag_t new_flag, int onserver, int sticky_flag, int* err_code)
1015 EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_flag[%d], onserver[%d], sticky_flag[%d], err_code[%p]", mail_id, new_flag, onserver, sticky_flag, err_code);
1017 /* default variable */
1019 int err = EMAIL_ERROR_NONE;
1021 if (!emcore_modify_flag(mail_id, new_flag, sticky_flag, &err)) {
1022 EM_DEBUG_EXCEPTION(" emcore_modify_flag falled [%d]", err);
1026 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1027 if( onserver == 1) {
1028 email_event_t event_data;
1029 unsigned handle = 0;
1030 memset(&event_data, 0x00, sizeof(email_event_t));
1032 event_data.type = EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER;
1033 event_data.event_param_data_1 = NULL;
1034 event_data.event_param_data_4 = mail_id;
1035 event_data.account_id = 0;
1036 #ifdef __FEATURE_LOCAL_ACTIVITY__
1037 emstorage_mail_tbl_t *mail_table_data = NULL;
1038 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, false, &err) || !mail_table_data) {
1039 EM_DEBUG_LOG(" emstorage_get_mail_field_by_id failed [%d]", err);
1042 event_data.account_id = mail_table_data->account_id;
1043 emstorage_free_mail(&mail_table_data,1,&err);
1045 emstorage_activity_tbl_t new_activity;
1046 new_activity.activity_type = ACTIVITY_MODIFYFLAG;
1047 new_activity.account_id = event_data.account_id;
1048 new_activity.mail_id = event_data.event_param_data_4;
1049 new_activity.dest_mbox = NULL;
1050 new_activity.server_mailid = NULL;
1051 new_activity.src_mbox = NULL;
1053 if (false == emcore_get_next_activity_id(&new_activity.activity_id,&err)) {
1054 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d \n", err);
1057 if (!emcore_add_activity(&new_activity, &err)) {
1058 EM_DEBUG_EXCEPTION(" emcore_add_activity [ACTIVITY_MODIFYFLAG] Failed - %d \n", err);
1061 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1062 if (!emcore_insert_event(&event_data, (int*)&handle, &err)) {
1063 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1067 #ifdef __FEATURE_LOCAL_ACTIVITY__
1068 EM_DEBUG_LOG("Setting g_local_activity_run ");
1069 g_local_activity_run = 1;
1070 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1072 #endif /* __FEATURE_SYNC_CLIENT_TO_SERVER__ */
1079 EM_DEBUG_FUNC_END();
1083 INTERNAL_FUNC int emdaemon_modify_extra_flag(int mail_id, email_extra_flag_t new_flag, int* err_code)
1085 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1088 int err = EMAIL_ERROR_NONE;
1090 if (!_emdaemon_check_mail_id(mail_id, &err)) {
1091 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1095 if (!emcore_modify_extra_flag(mail_id, new_flag, &err)) {
1096 EM_DEBUG_EXCEPTION("engine_mail_modify_extra_flag failed [%d]", err);
1106 EM_DEBUG_FUNC_END("ret [%d]", ret);
1110 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)
1112 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);
1114 int ret = false, err = EMAIL_ERROR_NONE;
1115 emstorage_account_tbl_t *account_tbl = NULL;
1117 if(account_id <= 0 || !mail_ids || num <= 0) {
1118 err = EMAIL_ERROR_INVALID_PARAM;
1119 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1123 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
1124 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
1128 if (!emcore_set_flags_field(account_id, mail_ids, num, field_type, value, &err)) {
1129 EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
1133 if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
1134 int *mail_id_array = NULL;
1135 email_event_t event_data = {0};
1136 unsigned handle = 0;
1138 mail_id_array = em_malloc(sizeof(int) * num);
1140 if (mail_id_array == NULL) {
1141 EM_DEBUG_EXCEPTION("em_malloc failed...");
1142 err = EMAIL_ERROR_OUT_OF_MEMORY;
1146 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1148 event_data.type = EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER;
1149 event_data.account_id = account_id;
1150 event_data.event_param_data_1 = NULL;
1151 event_data.event_param_data_3 = (char*)mail_id_array;
1152 event_data.event_param_data_4 = num;
1153 event_data.event_param_data_5 = field_type;
1154 event_data.event_param_data_6 = value;
1156 if (!emcore_insert_event(&event_data, (int*)&handle, &err)) {
1157 EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1167 emstorage_free_account(&account_tbl, 1, NULL);
1171 EM_DEBUG_FUNC_END();
1176 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)
1178 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);
1180 int err = EMAIL_ERROR_NONE;
1182 email_event_t event_data = { 0 };
1183 email_account_t *ref_account = NULL;
1185 if (!input_mail_data || input_mail_data->account_id <= 0 || input_mail_data->mail_id == 0 ||
1186 (input_mail_data->report_status == EMAIL_MAIL_REPORT_MDN && !input_mail_data->full_address_to)) {
1187 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1188 err = EMAIL_ERROR_INVALID_PARAM;
1192 ref_account = emdaemon_get_account_reference(input_mail_data->account_id);
1194 EM_DEBUG_LOG(" emdaemon_get_account_reference failed [%d]", input_mail_data->account_id);
1195 err = EMAIL_ERROR_INVALID_ACCOUNT;
1199 if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
1200 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1204 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1205 if ( input_from_eas == 0) {
1206 event_data.type = EMAIL_EVENT_UPDATE_MAIL;
1207 event_data.account_id = input_mail_data->account_id;
1208 event_data.event_param_data_1 = (char*)input_mail_data;
1209 event_data.event_param_data_2 = (char*)input_attachment_data_list;
1210 event_data.event_param_data_3 = (char*)input_meeting_request;
1211 event_data.event_param_data_4 = input_attachment_count;
1212 event_data.event_param_data_5 = input_from_eas;
1214 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1215 EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
1216 err = EMAIL_ERROR_NONE;
1225 EM_DEBUG_FUNC_END("err [%d]", err);
1230 int _emdaemon_check_mail_id(int mail_id, int* err_code)
1232 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1235 int err = EMAIL_ERROR_NONE;
1237 emstorage_mail_tbl_t* mail = NULL;
1239 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail, true, &err)) {
1240 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1248 emstorage_free_mail(&mail, 1, NULL);
1250 if (err_code != NULL)
1252 EM_DEBUG_FUNC_END();
1257 INTERNAL_FUNC int emdaemon_send_mail_retry( int mail_id, int timeout_in_sec, int* err_code)
1260 int err = EMAIL_ERROR_NONE;
1261 long nTimerValue = 0;
1262 char mail_id_string[10] = { 0, };
1263 email_mailbox_t mailbox;
1264 emstorage_mail_tbl_t* mail_table_data = NULL;
1267 memset(&opt, 0x00, sizeof(email_option_t));
1268 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
1270 if (!_emdaemon_check_mail_id(mail_id, &err)) {
1271 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1275 if( ! emstorage_get_mail_by_id(mail_id, &mail_table_data, false, &err) && !mail_table_data) {
1276 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1280 mailbox.mailbox_name = EM_SAFE_STRDUP(mail_table_data->mailbox_name);
1282 memcpy(&opt, emcore_get_option(&err), sizeof(email_option_t));
1284 if( mail_table_data ) {
1285 mailbox.account_id = mail_table_data->account_id;
1286 opt.priority = mail_table_data->priority;
1289 if ( timeout_in_sec == 0 ) {
1290 if(!emdaemon_send_mail(mail_id, &opt, NULL, &err)) {
1291 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1295 else if ( timeout_in_sec > 0 ) {
1296 sprintf(mail_id_string,"%d",mail_id);
1297 nTimerValue = timeout_in_sec * 1000;
1298 if ( emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)mail_id_string) <= 0) {
1299 EM_DEBUG_EXCEPTION("Failed to start timer");
1310 emstorage_free_mail(&mail_table_data, 1, NULL);
1312 EM_SAFE_FREE(mailbox.mailbox_name);
1318 INTERNAL_FUNC void _OnMailSendRetryTimerCB( void* data )
1320 EM_DEBUG_FUNC_BEGIN();
1321 int err = EMAIL_ERROR_NONE;
1322 email_mailbox_t mailbox;
1323 emstorage_mail_tbl_t* mail_table_data = NULL;
1327 memset( &opt, 0x00, sizeof(email_option_t));
1328 memset( &mailbox, 0x00, sizeof(email_mailbox_t));
1331 EM_DEBUG_LOG("Invalid param");
1335 mail_id = atoi((char*)data);
1338 if (!_emdaemon_check_mail_id(mail_id, &err)) {
1339 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1343 if( ! emstorage_get_mail_by_id(mail_id, &mail_table_data, false, &err) && !mail_table_data) {
1344 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1348 mailbox.mailbox_name = EM_SAFE_STRDUP(mail_table_data->mailbox_name);
1349 memcpy(&opt, emcore_get_option(&err), sizeof(email_option_t));
1351 if( mail_table_data ) {
1352 opt.priority = mail_table_data->priority;
1353 mailbox.account_id = mail_table_data->account_id;
1356 if(!emdaemon_send_mail(mail_id, &opt, NULL, &err)) {
1357 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1363 emstorage_free_mail(&mail_table_data, 1, NULL);
1365 EM_SAFE_FREE(mailbox.mailbox_name);
1367 EM_DEBUG_FUNC_END();
1371 INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
1373 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);
1375 int err = EMAIL_ERROR_NONE;
1376 int *mail_id_list = NULL, result_count = 0, i, mailbox_count = 0, account_id;
1377 email_mail_list_item_t *mail_list = NULL;
1378 email_mailbox_t *target_mailbox_list = NULL, *target_mailbox = NULL;
1379 char mailbox_id_param_string[10] = {0,};
1381 if (target_mailbox_id == 0) {
1382 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1383 err = EMAIL_ERROR_INVALID_PARAM;
1387 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) {
1388 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1393 mail_id_list = em_malloc(sizeof(int) * result_count);
1395 if (mail_id_list == NULL) {
1396 EM_DEBUG_EXCEPTION("em_malloc failed...");
1397 err = EMAIL_ERROR_OUT_OF_MEMORY;
1401 for(i = 0; i < result_count; i++) {
1402 mail_id_list[i] = mail_list[i].mail_id;
1404 account_id = mail_list[0].account_id;
1407 if (!emcore_get_list(account_id, &target_mailbox_list, &mailbox_count, &err)) {
1408 EM_DEBUG_EXCEPTION("emcore_get_list failed [%d]", err);
1412 for(i = 0; i < mailbox_count; i++) {
1413 EM_DEBUG_LOG("%s %d", target_mailbox_list[i].mailbox_name, target_mailbox_id);
1414 if(target_mailbox_list[i].mailbox_id == target_mailbox_id) {
1415 target_mailbox = (target_mailbox_list + i);
1420 if(!target_mailbox) {
1421 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1425 if (!emdaemon_move_mail(mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
1426 EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
1430 SNPRINTF(mailbox_id_param_string, 10, "%d", target_mailbox->mailbox_id);
1431 if (!emstorage_notify_storage_event(NOTI_THREAD_MOVE, account_id, thread_id, mailbox_id_param_string, move_always_flag))
1432 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1437 emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1438 EM_SAFE_FREE(mail_list);
1439 EM_SAFE_FREE(mail_id_list);
1443 EM_DEBUG_FUNC_END();
1447 INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_flag, unsigned* handle, int *err_code)
1449 EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1451 int err = EMAIL_ERROR_NONE;
1453 int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1454 email_mail_list_item_t *mail_list = NULL;
1456 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) {
1457 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1462 mail_id_list = em_malloc(sizeof(int) * result_count);
1464 if (mail_id_list == NULL) {
1465 EM_DEBUG_EXCEPTION("em_malloc failed...");
1466 err = EMAIL_ERROR_OUT_OF_MEMORY;
1470 for(i = 0; i < result_count; i++) {
1471 mail_id_list[i] = mail_list[i].mail_id;
1474 account_id = mail_list[0].account_id;
1475 mailbox_id = mail_list[0].mailbox_id;
1477 // should remove requiring of mailbox information from this function.
1478 // email-service should find mailboxes itself by its mail id.
1479 if (!emdaemon_delete_mail(mailbox_id, mail_id_list, result_count, false, handle, &err)) {
1480 EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
1484 if (!emstorage_notify_storage_event(NOTI_THREAD_DELETE, account_id, thread_id, NULL, delete_always_flag))
1485 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [NOTI_THREAD_DELETE] >>>> ");
1490 EM_SAFE_FREE(mail_list);
1491 EM_SAFE_FREE(mail_id_list);
1495 EM_DEBUG_FUNC_END();
1499 INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server, unsigned* handle, int *err_code)
1501 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);
1503 int err = EMAIL_ERROR_NONE;
1504 int account_id, *mail_id_list = NULL, result_count = 0, i;
1505 email_mail_list_item_t *mail_list = NULL;
1507 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) {
1508 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1513 mail_id_list = em_malloc(sizeof(int) * result_count);
1515 if (mail_id_list == NULL) {
1516 EM_DEBUG_EXCEPTION("em_malloc failed...");
1517 err = EMAIL_ERROR_OUT_OF_MEMORY;
1521 for(i = 0; i < result_count; i++) {
1522 mail_id_list[i] = mail_list[i].mail_id;
1525 account_id = mail_list[0].account_id;
1527 if (!emdaemon_set_flags_field(account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
1528 EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
1532 if (!emstorage_notify_storage_event(NOTI_THREAD_MODIFY_SEEN_FLAG, account_id, thread_id, NULL, seen_flag))
1533 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1538 EM_SAFE_FREE(mail_list);
1539 EM_SAFE_FREE(mail_id_list);
1543 EM_DEBUG_FUNC_END();
1547 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, unsigned *output_handle)
1549 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
1550 int err = EMAIL_ERROR_NONE;
1552 email_event_t event_data = { 0 };
1553 email_account_t *ref_account = NULL;
1554 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1556 if (input_mailbox_id <= 0) {
1557 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1558 err = EMAIL_ERROR_INVALID_PARAM;
1562 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
1563 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1567 ref_account = emdaemon_get_account_reference(mailbox_tbl->account_id);
1569 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", mailbox_tbl->account_id);
1570 err = EMAIL_ERROR_INVALID_ACCOUNT;
1574 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1575 if(input_on_server) {
1576 event_data.type = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
1577 event_data.account_id = mailbox_tbl->account_id;
1578 event_data.event_param_data_4 = input_mailbox_id;
1580 if (!emcore_insert_event(&event_data, &handle, &err)) {
1581 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1587 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1588 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1595 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1597 EM_DEBUG_FUNC_END("err [%d]", err);