Merged from tizen_2.4 to tizen_3.0 70/50470/1 accepted/tizen/mobile/20151029.085946 accepted/tizen/tv/20151029.085956 accepted/tizen/wearable/20151029.090008 submit/tizen/20151029.065142
authorSunghyun Kwon <sh0701.kwon@samsung.com>
Thu, 29 Oct 2015 05:47:25 +0000 (14:47 +0900)
committerSunghyun Kwon <sh0701.kwon@samsung.com>
Thu, 29 Oct 2015 05:47:25 +0000 (14:47 +0900)
Change-Id: I45fc8920cfadae1384fe13c540329140edce3c4d

33 files changed:
email-api/email-api-mail.c
email-api/email-api-smime.c
email-common-use/CMakeLists.txt
email-common-use/email-convert.c
email-core/email-core-account.c
email-core/email-core-cynara.c [changed mode: 0644->0755]
email-core/email-core-event.c
email-core/email-core-gmime.c
email-core/email-core-imap-idle.c
email-core/email-core-imap-mailbox.c
email-core/email-core-mail.c
email-core/email-core-mailbox-sync.c
email-core/email-core-mailbox.c
email-core/email-core-smtp.c
email-core/email-core-utils.c
email-core/email-storage/email-storage.c
email-core/email-storage/include/email-storage.h
email-core/include/email-core-cynara.h [changed mode: 0644->0755]
email-core/include/email-core-mail.h
email-core/include/email-core-mailbox-sync.h
email-core/include/email-core-mailbox.h
email-core/include/email-core-utils.h
email-daemon/email-daemon-event.c
email-daemon/email-daemon-mail.c
email-daemon/include/email-daemon.h
email-daemon/main.c
email-ipc/email-ipc-api.c
email-ipc/email-ipc-api/email-ipc-api-info.c
email-ipc/email-ipc-api/email-ipc-param-list.c
email-ipc/email-ipc-api/include/email-ipc-api-info.h
email-ipc/email-ipc-api/include/email-ipc-param-list.h
email-ipc/email-stub/email-stub-socket.c
email-ipc/include/email-ipc.h

index 524da69..29f6af7 100755 (executable)
@@ -2034,6 +2034,7 @@ EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int inp
        }
 
 FINISH_OFF:
+
        if(hAPI)
                emipc_destroy_email_api(hAPI);
 
index 2072bd5..70d85ee 100755 (executable)
@@ -339,6 +339,12 @@ EXPORT_API int email_verify_signature_ex(email_mail_data_t *input_mail_data, ema
                        break;
        }
 
+       if (count == input_attachment_count) {
+               EM_DEBUG_LOG("No have the signed attachment");
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+
        if (input_mail_data->smime_type == EMAIL_SMIME_SIGNED) {
                emcore_init_openssl_library();
                if (!emcore_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, verify, &err)) 
index b29ebee..060c5a8 100755 (executable)
@@ -29,7 +29,7 @@ INCLUDE_DIRECTORIES(
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(common_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf contacts-service2 uw-imap-toolkit sqlite3
+pkg_check_modules(common_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf contacts-service2 uw-imap-toolkit) 
 
 FOREACH(flag ${common_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index 35cc901..cc06df9 100755 (executable)
@@ -227,7 +227,6 @@ INTERNAL_FUNC int em_convert_mailbox_to_mailbox_tbl(email_mailbox_t *mailbox, em
                memcpy(mailbox_tbl->eas_data, mailbox->eas_data, mailbox->eas_data_length);
        }
 
-
        EM_DEBUG_FUNC_END();
        return ret;
 }
@@ -360,7 +359,6 @@ FINISH_OFF:
                emcore_free_mail_data_list(&temp_mail_data, item_count);
        }
 
-
        if(error)
                *error = err_code;
 
@@ -449,7 +447,7 @@ INTERNAL_FUNC int   em_convert_mail_data_to_mail_tbl(email_mail_data_t *mail_dat
                                /*prevent 44357*/
                                if (i > 0)
                                        emstorage_free_mail(&temp_mail_tbl, i, NULL);
-                               else 
+                               else
                                        EM_SAFE_FREE(temp_mail_tbl);
                                goto FINISH_OFF;
                        }
@@ -739,7 +737,7 @@ static char *convert_format(char *fmt)
                                } else {
                                        EM_DEBUG_LOG("size : [%d]", size);
                                }
-                               
+
                                break;
             default:
                 EM_DEBUG_EXCEPTION("unsupported option %c\n", *c);
@@ -949,12 +947,12 @@ INTERNAL_FUNC char* em_convert_attachment_data_to_byte_stream(email_attachment_d
                return NULL;
        }
        EM_SAFE_FREE(converted_fmt);
-       
+
        /* if attachment_count is zero, for loop is skipped */
        int i=0;
        for( ; (i < attachment_count) && (attachment+i) ; i++ ) {
                memcpy(&cur, attachment + i, sizeof(email_attachment_data_t));
-               tpl_pack(tn, 1);                       
+               tpl_pack(tn, 1);
        }
 
        /* write data to buffer */
@@ -974,15 +972,15 @@ INTERNAL_FUNC char* em_convert_attachment_data_to_byte_stream(email_attachment_d
 INTERNAL_FUNC void em_convert_byte_stream_to_attachment_data(char *stream, int stream_len, email_attachment_data_t **attachment_data, int *attachment_count)
 {
        EM_DEBUG_FUNC_BEGIN();
+
        EM_NULL_CHECK_FOR_VOID(stream);
        EM_NULL_CHECK_FOR_VOID(attachment_data);
        EM_NULL_CHECK_FOR_VOID(attachment_count);
 
-
        email_attachment_data_t cur = {0};
        tpl_node *tn = NULL;
        char *converted_fmt = NULL;
-       
+
        converted_fmt = convert_format(EMAIL_ATTACHMENT_DATA_FMT);
        if (converted_fmt == NULL) {
                EM_DEBUG_EXCEPTION("converting failed");
@@ -1008,7 +1006,7 @@ INTERNAL_FUNC void em_convert_byte_stream_to_attachment_data(char *stream, int s
                attached = (email_attachment_data_t*) em_malloc(sizeof(email_attachment_data_t)*num_element);
 
        int i = 0;
-       whiletpl_unpack(tn, 1) > 0) {
+       while (tpl_unpack(tn, 1) > 0) {
                if (!(attached+i)) {
                        EM_DEBUG_EXCEPTION ("num element mismatched [%d] vs [%d]", num_element, i);
                        num_element = i;
@@ -1020,6 +1018,10 @@ INTERNAL_FUNC void em_convert_byte_stream_to_attachment_data(char *stream, int s
                pdata->attachment_path          = EM_SAFE_STRDUP(cur.attachment_path);
                pdata->content_id               = EM_SAFE_STRDUP(cur.content_id);
                pdata->attachment_mime_type     = EM_SAFE_STRDUP(cur.attachment_mime_type);
+               EM_SAFE_FREE(cur.attachment_name);
+               EM_SAFE_FREE(cur.attachment_path);
+               EM_SAFE_FREE(cur.content_id);
+               EM_SAFE_FREE(cur.attachment_mime_type);
                memset(&cur, 0, sizeof(email_attachment_data_t));    /* initialize variable, used for unpacking */
                i++;
        }
index 3875206..4fa62b0 100755 (executable)
@@ -229,7 +229,12 @@ INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_nam
 
        FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
-       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
+       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, 
+                                                                                                                       account, 
+                                                                                                                       0, 
+                                                                                                                       true,
+                                                                                                                       (void **)&tmp_stream, 
+                                                                                                                       &err) || !tmp_stream) {
                EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
                if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) {      /*  wrong password or etc */
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
@@ -248,7 +253,12 @@ INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_nam
 
        FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
-       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
+       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, 
+                                                                                                                       account, 
+                                                                                                                       EMAIL_CONNECT_FOR_SENDING, 
+                                                                                                                       true,
+                                                                                                                       (void **)&stream, 
+                                                                                                                       &err) || !stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
                goto FINISH_OFF;
@@ -260,7 +270,12 @@ INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_nam
                        EM_DEBUG_LOG("Retry with TLS");
                        account->outgoing_server_secure_connection = 0x02;      /*  0x02 == TLS */
 
-                       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
+                       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, 
+                                                                                                                                       account, 
+                                                                                                                                       EMAIL_CONNECT_FOR_SENDING, 
+                                                                                                                                       true,
+                                                                                                                                       (void **)&stream, 
+                                                                                                                                       &err) || !stream) {
                                EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
                                err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
                                account->outgoing_server_secure_connection = 0x01;      /*  restore to the previous value */
@@ -512,7 +527,7 @@ INTERNAL_FUNC int emcore_delete_account(char *multi_user_name, int account_id, i
        }
 
        /*  delete local imap sync mailbox from imap mailbox table */
-       if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, NULL, NULL, false, &err)) {
+       if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, 0, NULL, NULL, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
                goto FINISH_OFF;
        }
old mode 100644 (file)
new mode 100755 (executable)
index 42aa047..879f5de
@@ -125,7 +125,7 @@ INTERNAL_FUNC void emcore_finish_cynara()
        EM_DEBUG_FUNC_END();
 }
 
-INTERNAL_FUNC int emcore_check_privilege(int socket_fd)
+INTERNAL_FUNC int emcore_check_privilege(unsigned int socket_fd)
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = CYNARA_API_SUCCESS;
index de6a63f..c35bc88 100755 (executable)
@@ -383,6 +383,9 @@ INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, in
        int error = EMAIL_ERROR_NONE;
        int q_length = 0;
        int new_handle = 0;
+       char *multi_user_name = NULL;
+
+       multi_user_name = EM_SAFE_STRDUP(event_data->multi_user_name);
 
        ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
 
@@ -443,10 +446,9 @@ INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, in
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
                {
                        int is_local_activity_event_inserted = false;
-                       emcore_partial_body_thd_local_activity_sync (
-                                                 event_data->multi_user_name, 
-                                                 &is_local_activity_event_inserted, 
-                                                 &error);
+                       emcore_partial_body_thd_local_activity_sync(multi_user_name,
+                                                                                                                &is_local_activity_event_inserted, 
+                                                                                                                &error);
                        if (error != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
                        }
@@ -459,6 +461,8 @@ INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, in
                break;
        }
 
+       EM_SAFE_FREE(multi_user_name);
+
        if (err_code) {
                EM_DEBUG_LOG("ERR [%d]", error);
                *err_code = error;
@@ -2013,7 +2017,12 @@ static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code
        MAILSTREAM *stream = NULL;
        void *tmp_stream = NULL;
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       g_partial_body_bulk_dwd_que[0].account_id, 
+                                                                                       g_partial_body_bulk_dwd_que[0].mailbox_id, 
+                                                                                       true,
+                                                                                       (void **)&tmp_stream, 
+                                                                                       &error) || (NULL == tmp_stream)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -2125,7 +2134,12 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                                int k = 0;
                                int activity_count = 0;
 
-                               if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name, account_list[m], mailbox_list[i], (void **)&stream, &error)) {
+                               if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name, 
+                                                                                                               account_list[m], 
+                                                                                                               mailbox_list[i], 
+                                                                                                               true,
+                                                                                                               (void **)&stream, 
+                                                                                                               &error)) {
                                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
                                        stream = mail_close (stream);
                                        goto FINISH_OFF;
index ebf2f97..c5d167a 100755 (executable)
@@ -176,9 +176,9 @@ INTERNAL_FUNC int emcore_gmime_pop3_parse_mime(char *eml_path, struct _m_content
 }
 
 
-INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path, 
-                                                                                       struct _rfc822header *rfc822_header, 
-                                                                                       struct _m_content_info *cnt_info, 
+INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path,
+                                                                                       struct _rfc822header *rfc822_header,
+                                                                                       struct _m_content_info *cnt_info,
                                                                                        int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("cnt_info[%p], err_code[%p]", cnt_info, err_code);
@@ -397,6 +397,20 @@ static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject
                char *content_location = (char *)g_mime_part_get_content_location(leaf_part);
                EM_DEBUG_LOG_SEC("Location:%s", content_location);
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+               /* Parent content type - END */
+
                GMimeObject *mobject = (GMimeObject *)part;
 
                /*Content ID*/
@@ -491,21 +505,39 @@ static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject
                /*Content - END*/
 
                /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
-               int result = false;
-               if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0))) {
                        content_disposition_type = INLINE_ATTACHMENT;
                        EM_DEBUG_LOG("INLINE_ATTACHMENT");
-               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
-                       content_disposition_type = ATTACHMENT;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
-                       content_disposition_type = ATTACHMENT;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+               } else if (parent_ctype_subtype && g_ascii_strcasecmp(parent_ctype_subtype, "mixed") == 0) {
                        content_disposition_type = ATTACHMENT;
                        EM_DEBUG_LOG("ATTACHMENT");
                } else {
-                       EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
+                       int result = false;
+                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html,
+                                                                                                                               content_id,
+                                                                                                                               NULL,
+                                                                                                                               &result) == EMAIL_ERROR_NONE && result)) {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) ||
+                                               disposition_filename || ctype_name) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
+                       }
                }
 
                if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
@@ -930,6 +962,20 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject *
 
                GMimeObject *mobject = (GMimeObject *)part;
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+               /* Parent Content Type - END */
+
                /*Content ID*/
                char *content_id = (char *)g_mime_object_get_content_id(mobject);
 
@@ -1039,28 +1085,50 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject *
                        EM_DEBUG_LOG("save_status : [%d]", save_status);
                }
 
-               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
-               int result = false;
                cnt_info->total_mail_size += content_size;
-               if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       cnt_info->total_body_size += content_size;
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0))) {
                        content_disposition_type = INLINE_ATTACHMENT;
                        cnt_info->total_body_size += content_size;
                        EM_DEBUG_LOG("INLINE_ATTACHMENT");
-               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
-                       content_disposition_type = ATTACHMENT;
-                       cnt_info->total_attachment_size += content_size;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
-                       content_disposition_type = ATTACHMENT;
-                       cnt_info->total_attachment_size += content_size;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
                        content_disposition_type = ATTACHMENT;
                        cnt_info->total_attachment_size += content_size;
                        EM_DEBUG_LOG("ATTACHMENT");
                } else {
-                       cnt_info->total_body_size += content_size;
-                       EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
+                       int result = false;
+                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html,
+                                                                                                                               content_id,
+                                                                                                                               NULL,
+                                                                                                                               &result) == EMAIL_ERROR_NONE && result)) {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               cnt_info->total_body_size += content_size;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) ||
+                                               disposition_filename || ctype_name) {
+                               content_disposition_type = ATTACHMENT;
+                               cnt_info->total_attachment_size += content_size;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               cnt_info->total_attachment_size += content_size;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               cnt_info->total_attachment_size += content_size;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               cnt_info->total_body_size += content_size;
+                               EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
+                       }
                }
 
                if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
@@ -1397,6 +1465,19 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMime
                GMimeObject *mobject = (GMimeObject *)part;
                leaf_part = (GMimePart *)part;
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+
                /*Content Type*/
                ctype = g_mime_object_get_content_type(mobject);
                ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
@@ -1436,63 +1517,42 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMime
                EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
 
                /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
-               int result = false;
-               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
-                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html, 
-                                                                                                                               content_id, 
-                                                                                                                               NULL, 
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0))) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else {
+                       int result = false;
+                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html,
+                                                                                                                               content_id,
+                                                                                                                               NULL,
                                                                                                                                &result) == EMAIL_ERROR_NONE && result)) {
                                content_disposition_type = INLINE_ATTACHMENT;
                                EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       } else {
+                       } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) ||
+                                               disposition_filename || ctype_name) {
                                content_disposition_type = ATTACHMENT;
                                EM_DEBUG_LOG("ATTACHMENT");
-                       }
-               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
-                       if (cnt_info->attachment_only) {
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
                                content_disposition_type = ATTACHMENT;
                                EM_DEBUG_LOG("ATTACHMENT");
                        } else {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       }
-               } else {
-                       if (content_id &&
-                                       (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       } else if (content_id || content_location) {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       if (cnt_info->attachment_only) {
-                                               content_disposition_type = ATTACHMENT;
-                                               EM_DEBUG_LOG("ATTACHMENT");
-                                       } else {
-                                               content_disposition_type = INLINE_ATTACHMENT;
-                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                                       }
-                               }
-                       } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       content_disposition_type = ATTACHMENT;
-                                       EM_DEBUG_LOG("ATTACHMENT");
-                               }
+                               EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
                        }
                }
 
-               /*if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
-                       content_disposition_type = INLINE_ATTACHMENT;
-                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
-               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
-                       content_disposition_type = ATTACHMENT;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               }*/
-
                if (!emcore_get_temp_file_name(&content_path, &error))  {
                        EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
                        goto FINISH_OFF;
@@ -1911,6 +1971,19 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent,
                GMimeObject *mobject = (GMimeObject *)part;
                leaf_part = (GMimePart *)part;
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+
                /*Content Type*/
                ctype = g_mime_object_get_content_type(mobject);
                ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
@@ -1943,60 +2016,43 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent,
                content_location = (char *)g_mime_part_get_content_location(leaf_part);
                EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
 
-
                /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
-               int result = false;
-               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0)))     {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
                        content_disposition_type = ATTACHMENT;
                        EM_DEBUG_LOG("ATTACHMENT");
-               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
-                       if (cnt_info->attachment_only) {
-                               content_disposition_type = ATTACHMENT;
-                               EM_DEBUG_LOG("ATTACHMENT");
-                       } else {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       }
                } else {
-                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html, 
-                                                                                                                               content_id, 
-                                                                                                                               NULL, 
+                       int result = false;
+                       if (content_id && (emcore_search_string_from_file(cnt_info->text.html,
+                                                                                                                               content_id,
+                                                                                                                               NULL,
                                                                                                                                &result) == EMAIL_ERROR_NONE && result)) {
                                content_disposition_type = INLINE_ATTACHMENT;
                                EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       } else if (content_id || content_location) {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       if (cnt_info->attachment_only) {
-                                               content_disposition_type = ATTACHMENT;
-                                               EM_DEBUG_LOG("ATTACHMENT");
-                                       } else {
-                                               content_disposition_type = INLINE_ATTACHMENT;
-                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                                       }
-                               }
+                       } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) ||
+                                               disposition_filename || ctype_name) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
                        } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 && 
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
-                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       content_disposition_type = ATTACHMENT;
-                                       EM_DEBUG_LOG("ATTACHMENT");
-                               }
+                               EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
                        }
                }
 
-               /*if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
-                       content_disposition_type = INLINE_ATTACHMENT;
-                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
-               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
-                       content_disposition_type = ATTACHMENT;
-                       EM_DEBUG_LOG("ATTACHMENT");
-               }*/
-
                /*Content*/
                if (!emcore_get_temp_file_name(&content_path, &error)) {
                        EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
@@ -2367,6 +2423,20 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject
                GMimeObject *mobject = (GMimeObject *)part;
                leaf_part = (GMimePart *)part;
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+               /* Parent content type - END */
+
                /*Content Type*/
                ctype = g_mime_object_get_content_type(mobject);
                ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
@@ -2404,49 +2474,67 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject
                EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
 
                /*Figure out TEXT or ATTACHMENT(INLINE) */
-               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       cnt_info->total_body_size += content_size;
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0)))     {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       cnt_info->total_body_size += content_size;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
                        content_disposition_type = ATTACHMENT;
                        cnt_info->total_attachment_size += content_size;
                        EM_DEBUG_LOG("ATTACHMENT");
-               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
-                       if (cnt_info->attachment_only) {
+               } else {
+                       if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
                                content_disposition_type = ATTACHMENT;
                                cnt_info->total_attachment_size += content_size;
                                EM_DEBUG_LOG("ATTACHMENT");
-                       } else {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               cnt_info->total_body_size += content_size;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       }
-               } else {
-                       if (content_id || content_location) {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                               if (cnt_info->attachment_only) {
+                                       content_disposition_type = ATTACHMENT;
+                                       cnt_info->total_attachment_size += content_size;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               } else {
+                                       content_disposition_type = INLINE_ATTACHMENT;
                                        cnt_info->total_body_size += content_size;
-                                       EM_DEBUG_LOG("TEXT");
+                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                               }
+                       } else {
+                               if (content_id || content_location) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               cnt_info->total_body_size += content_size;
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
+                                               if (cnt_info->attachment_only) {
+                                                       content_disposition_type = ATTACHMENT;
+                                                       cnt_info->total_attachment_size += content_size;
+                                                       EM_DEBUG_LOG("ATTACHMENT");
+                                               } else {
+                                                       content_disposition_type = INLINE_ATTACHMENT;
+                                                       cnt_info->total_body_size += content_size;
+                                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                               }
+                                       }
                                } else {
-                                       if (cnt_info->attachment_only) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               cnt_info->total_body_size += content_size;
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
                                                content_disposition_type = ATTACHMENT;
                                                cnt_info->total_attachment_size += content_size;
                                                EM_DEBUG_LOG("ATTACHMENT");
-                                       } else {
-                                               content_disposition_type = INLINE_ATTACHMENT;
-                                               cnt_info->total_body_size += content_size;
-                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
                                        }
                                }
-                       } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       cnt_info->total_body_size += content_size;
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       content_disposition_type = ATTACHMENT;
-                                       cnt_info->total_attachment_size += content_size;
-                                       EM_DEBUG_LOG("ATTACHMENT");
-                               }
                        }
                }
 
@@ -2487,8 +2575,8 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent, 
-                                                                                                                       GMimeObject *part, 
+INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent,
+                                                                                                                       GMimeObject *part,
                                                                                                                        gpointer user_data)
 {
        EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
@@ -2585,6 +2673,20 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent
                GMimeObject *mobject = (GMimeObject *)part;
                leaf_part = (GMimePart *)part;
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+               /* Parent content type - END */
+
                /*Content Type*/
                ctype = g_mime_object_get_content_type(mobject);
                ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
@@ -2600,7 +2702,7 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent
                        goto FINISH_OFF;
                }
                /*Content Type - END*/
-               
+
                /*Content Disposition*/
                disposition = g_mime_object_get_content_disposition(mobject);
                if (disposition) {
@@ -2621,41 +2723,56 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent
                EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
 
                /*Figure out TEXT or ATTACHMENT(INLINE) */
-               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0))) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
                        content_disposition_type = ATTACHMENT;
                        EM_DEBUG_LOG("ATTACHMENT");
-               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
-                       if (cnt_info->attachment_only) {
+               } else {
+                       if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
                                content_disposition_type = ATTACHMENT;
                                EM_DEBUG_LOG("ATTACHMENT");
+                       } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                               if (cnt_info->attachment_only) {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               } else {
+                                       content_disposition_type = INLINE_ATTACHMENT;
+                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                               }
                        } else {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       }
-               } else {
-                       if (content_id || content_location) {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
+                               if (content_id || content_location) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
+                                               if (cnt_info->attachment_only) {
+                                                       content_disposition_type = ATTACHMENT;
+                                                       EM_DEBUG_LOG("ATTACHMENT");
+                                               } else {
+                                                       content_disposition_type = INLINE_ATTACHMENT;
+                                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                               }
+                                       }
                                } else {
-                                       if (cnt_info->attachment_only) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
                                                content_disposition_type = ATTACHMENT;
                                                EM_DEBUG_LOG("ATTACHMENT");
-                                       } else {
-                                               content_disposition_type = INLINE_ATTACHMENT;
-                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
                                        }
                                }
-                       } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       content_disposition_type = ATTACHMENT;
-                                       EM_DEBUG_LOG("ATTACHMENT");
-                               }
                        }
                }
 
@@ -2667,7 +2784,7 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent
                }
                else {
                        snprintf(sections, sizeof(sections), "BODY.PEEK[%s.MIME] BODY.PEEK[%s]", ctype_section, ctype_section);
-                       
+
                        if (cnt_info->sections) {
                                /* Signed message : Did not need sections */
                                if (cnt_info->content_type == 1) {
@@ -2696,8 +2813,8 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent, 
-                                                                                                                               GMimeObject *part, 
+INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent,
+                                                                                                                               GMimeObject *part,
                                                                                                                                gpointer user_data)
 {
        EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
@@ -2785,6 +2902,20 @@ INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *p
                        goto FINISH_OFF;
                }
 
+               /* Parent object */
+               /* because of distinguished the inline or attachment */
+               /* multipart/related : inline */
+               /* multipart/mixed : attachment */
+               char *parent_ctype_type = NULL;
+               char *parent_ctype_subtype = NULL;
+               GMimeContentType *parent_ctype = NULL;
+
+               parent_ctype = g_mime_object_get_content_type(parent);
+               parent_ctype_type = (char *)g_mime_content_type_get_media_type(parent_ctype);
+               parent_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(parent_ctype);
+               EM_DEBUG_LOG("Content-type[%s/%s]", parent_ctype_type, parent_ctype_subtype);
+               /* Parent content type - END */
+
                /*Content Type*/
                ctype = g_mime_object_get_content_type(mobject);
                ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
@@ -2821,41 +2952,56 @@ INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *p
                EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
 
                /*Figure out TEXT or ATTACHMENT(INLINE) */
-               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+               if ((!disposition_str && g_ascii_strcasecmp(ctype_type, "text") == 0) &&
+                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                       EM_DEBUG_LOG("TEXT");
+               } else if (parent_ctype_subtype &&
+                                       ((g_ascii_strncasecmp(parent_ctype_subtype, "related", strlen("related")) == 0) ||
+                                        (g_ascii_strncasecmp(parent_ctype_subtype, "relative", strlen("relative")) == 0))) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if (parent_ctype_subtype &&
+                                       g_ascii_strncasecmp(parent_ctype_subtype, "mixed", strlen("mixed")) == 0) {
                        content_disposition_type = ATTACHMENT;
                        EM_DEBUG_LOG("ATTACHMENT");
-               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
-                       if (cnt_info->attachment_only) {
+               } else {
+                       if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
                                content_disposition_type = ATTACHMENT;
                                EM_DEBUG_LOG("ATTACHMENT");
+                       } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                               if (cnt_info->attachment_only) {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               } else {
+                                       content_disposition_type = INLINE_ATTACHMENT;
+                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                               }
                        } else {
-                               content_disposition_type = INLINE_ATTACHMENT;
-                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
-                       }
-               } else {
-                       if (content_id || content_location) {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
+                               if (content_id || content_location) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
+                                               if (cnt_info->attachment_only) {
+                                                       content_disposition_type = ATTACHMENT;
+                                                       EM_DEBUG_LOG("ATTACHMENT");
+                                               } else {
+                                                       content_disposition_type = INLINE_ATTACHMENT;
+                                                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                               }
+                                       }
                                } else {
-                                       if (cnt_info->attachment_only) {
+                                       if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                                       (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 ||
+                                                        g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                               EM_DEBUG_LOG("TEXT");
+                                       } else {
                                                content_disposition_type = ATTACHMENT;
                                                EM_DEBUG_LOG("ATTACHMENT");
-                                       } else {
-                                               content_disposition_type = INLINE_ATTACHMENT;
-                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
                                        }
                                }
-                       } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || 
-                                                g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
-                                       EM_DEBUG_LOG("TEXT");
-                               } else {
-                                       content_disposition_type = ATTACHMENT;
-                                       EM_DEBUG_LOG("ATTACHMENT");
-                               }
                        }
                }
 
@@ -2956,8 +3102,8 @@ INTERNAL_FUNC void emcore_gmime_get_mime_entity_cb(GMimeObject *parent, GMimeObj
 }
 
 INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart,
-                                                                                                       BODY *body, 
-                                                                                                       const char *spec, 
+                                                                                                       BODY *body,
+                                                                                                       const char *spec,
                                                                                                        int *total_mail_size)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -3001,7 +3147,7 @@ INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart,
                if (EM_SAFE_STRLEN(subspec) > 2)
                        section = EM_SAFE_STRDUP(subspec+2);
 
-               EM_DEBUG_LOG("constructing a %s/%s part (%s/%s)", body_types[part->body.type], part->body.subtype, 
+               EM_DEBUG_LOG("constructing a %s/%s part (%s/%s)", body_types[part->body.type], part->body.subtype,
                                                                                                                subspec, section);
 
                if (part->body.type == TYPEMULTIPART) {
@@ -3172,8 +3318,8 @@ INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart,
 }
 
 INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part,
-                                                                                               BODY *body, 
-                                                                                               const char *spec, 
+                                                                                               BODY *body,
+                                                                                               const char *spec,
                                                                                                int *total_mail_size)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -3199,7 +3345,7 @@ INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part,
        ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
 
        /* Type-Subtype */
-       if (g_strcmp0(ctype_type, "text") == 0 && g_strcmp0(ctype_subtype, "plain") == 0 
+       if (g_strcmp0(ctype_type, "text") == 0 && g_strcmp0(ctype_subtype, "plain") == 0
                        && body->subtype) {
                GMimeContentType *content_type = NULL;
                char *type = g_ascii_strdown(body_types[body->type], -1);
@@ -3423,7 +3569,7 @@ FINISH_OFF:
 }
 
 INTERNAL_FUNC int emcore_gmime_get_body_sections_from_message(GMimeMessage *message,
-                                                                                                                       struct _m_content_info *cnt_info, 
+                                                                                                                       struct _m_content_info *cnt_info,
                                                                                                                        char **sections_to_fetch)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -3522,8 +3668,8 @@ FINISH_OFF:
 }
 
 INTERNAL_FUNC int emcore_gmime_get_attachment_section_from_message(GMimeMessage *message,
-                                                                                                                                       struct _m_content_info *cnt_info, 
-                                                                                                                                       int nth, 
+                                                                                                                                       struct _m_content_info *cnt_info,
+                                                                                                                                       int nth,
                                                                                                                                        char **section_to_fetch)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -3660,8 +3806,8 @@ static int emcore_gmime_get_section_n_bodysize(char *response, char *section, in
 }
 
 INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int msg_uid, int mail_id,
-                                                                                                               struct _m_content_info *cnt_info, 
-                                                                                                               GMimeMessage *message, int event_handle, 
+                                                                                                               struct _m_content_info *cnt_info,
+                                                                                                               GMimeMessage *message, int event_handle,
                                                                                                                int auto_download, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], cnt_info[%p], err_code[%p]",
@@ -3739,7 +3885,7 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
 
        if (GMIME_IS_MULTIPART_SIGNED(message->mime_part)) {
                EM_DEBUG_LOG("Multi Part Signed");
-               signed_message = 1;     
+               signed_message = 1;
        }
 
        while (imaplocal->netstream) {
@@ -3845,7 +3991,7 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
                                EM_DEBUG_EXCEPTION("invalid mime part type");
                                goto FINISH_OFF;
                        }
-               
+
                        if (!part_header) {
                                if (!emcore_get_temp_file_name(&buf, &err) || !buf) {
                                        EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
@@ -3862,10 +4008,10 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
                                        if (cnt_info->total_body_size > body_size) {
                                                EM_DEBUG_LOG("Multipart body size is [%d]", cnt_info->total_body_size);
                                                if (!auto_download) {
-                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, 
-                                                                                                                       mail_id, 
-                                                                                                                       buf, 
-                                                                                                                       cnt_info->total_body_size, 
+                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY,
+                                                                                                                       mail_id,
+                                                                                                                       buf,
+                                                                                                                       cnt_info->total_body_size,
                                                                                                                        0))
                                                                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
                                                }
@@ -3886,10 +4032,10 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
 
                                if (cnt_info->grab_type == (GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT)) {
                                        if (!auto_download) {
-                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, 
-                                                                                                               mail_id, 
-                                                                                                               buf, 
-                                                                                                               cnt_info->total_mail_size, 
+                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY,
+                                                                                                               mail_id,
+                                                                                                               buf,
+                                                                                                               cnt_info->total_mail_size,
                                                                                                                0))
                                                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
                                        }
@@ -3910,16 +4056,16 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
                                        }
 
                                        if (signed_message) {
-                                               content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, 
+                                               content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded,
                                                                                                                                                        EM_SAFE_STRLEN(encoded));
                                                entity_parser = g_mime_parser_new_with_stream(content_stream);
                                                if (content_stream) g_object_unref(content_stream);
 
                                                entity_part = g_mime_parser_construct_part(entity_parser);
                                                if (entity_parser) g_object_unref(entity_parser);
-                                       } 
+                                       }
                                        else if (GMIME_IS_PART(mime_object) && mime_part) {
-                                               content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, 
+                                               content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded,
                                                                                                                                                        EM_SAFE_STRLEN(encoded));
                                                //parser = g_mime_parser_new_with_stream(content_stream);
                                                content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
@@ -4111,7 +4257,7 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
                                                if (content_stream) g_object_unref (content_stream);
                                                g_mime_part_set_content_object(mime_part, content);
                                                if (content) g_object_unref(content);
-                                       }       
+                                       }
                                }
                        } else {
                                EM_DEBUG_LOG("MIME header");
@@ -4155,7 +4301,7 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int
 
                                        /* Replace the file name (Becase the server sometimes send the invalid name in bodystructure) */
                                        if (mime_part && file_name) g_mime_part_set_filename(mime_part, file_name);
-                               } 
+                               }
 
                                if (object_header) g_object_unref(object_header);
                                if (ctype_header) g_object_unref(ctype_header);
@@ -4937,7 +5083,7 @@ INTERNAL_FUNC char *emcore_gmime_get_decoding_text(const char *text)
 INTERNAL_FUNC char *emcore_gmime_get_mime_entity_signed_message(GMimeObject *multipart)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        int entity_fd = 0;
        int error = EMAIL_ERROR_NONE;
        char *mime_entity_path = NULL;
@@ -4997,8 +5143,8 @@ FINISH_OFF:
 INTERNAL_FUNC char *emcore_gmime_get_mime_entity(int fd)
 {
        EM_DEBUG_FUNC_BEGIN();
+
        char *output_path = NULL;
-       
        GMimeStream *stream = NULL;
        GMimeParser *parser = NULL;
        GMimeObject *part = NULL;
index 7f5e4f2..edce282 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,9 +24,9 @@
  * This file contains functionality related to IMAP IDLE.
  * to interact with email-service.
  * @file               em_core-imap-idle.c
- * @author     
+ * @author
  * @version    0.1
- * @brief              This file contains functionality to provide IMAP IDLE support in email-service. 
+ * @brief              This file contains functionality to provide IMAP IDLE support in email-service.
  */
 
 #include <email-internal-types.h>
@@ -39,7 +39,7 @@
 #include "lnx_inc.h"
 #include "email-core-imap-idle.h"
 #include "email-debug-log.h"
-#include "email-storage.h" 
+#include "email-storage.h"
 #include "email-network.h"
 #include "email-core-utils.h"
 #include "email-core-mailbox.h"
@@ -74,9 +74,12 @@ typedef struct _email_imap_idle_connection_info_t {
 int       imap_idle_pipe_fd[2];
 thread_t  imap_idle_thread_id;
 
-static int emcore_get_connection_info_by_socket_fd(GList *input_imap_idle_task_list, int input_socket_fd, email_imap_idle_connection_info_t **output_connection_info)
+static int emcore_get_connection_info_by_socket_fd(GList *input_imap_idle_task_list,
+                                                                                                       int input_socket_fd,
+                                                                                                       email_imap_idle_connection_info_t **output_connection_info)
 {
-       EM_DEBUG_FUNC_BEGIN("input_imap_idle_task_list[%p] input_socket_fd[%d] output_connection_info[%p]", input_imap_idle_task_list, input_socket_fd, output_connection_info);
+       EM_DEBUG_FUNC_BEGIN("input_imap_idle_task_list[%p] input_socket_fd[%d] output_connection_info[%p]",
+                                               input_imap_idle_task_list, input_socket_fd, output_connection_info);
        int err = EMAIL_ERROR_NONE;
        email_imap_idle_connection_info_t *connection_info = NULL;
        GList *index = NULL;
@@ -89,27 +92,29 @@ static int emcore_get_connection_info_by_socket_fd(GList *input_imap_idle_task_l
 
        index = g_list_first(input_imap_idle_task_list);
 
-       while(index) {
+       while (index) {
                connection_info = index->data;
-               if(connection_info && connection_info->socket_fd == input_socket_fd) {
+               if (connection_info && connection_info->socket_fd == input_socket_fd) {
                        break;
                }
                index = g_list_next(index);
        }
 
-       if(connection_info)
+       if (connection_info)
                *output_connection_info = connection_info;
        else
                err = EMAIL_ERROR_DATA_NOT_FOUND;
 
 FINISH_OFF:
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
 static int emcore_clear_old_connections(int input_epoll_fd, GList **input_imap_idle_task_list)
 {
-       EM_DEBUG_FUNC_BEGIN("input_epoll_fd[%d] input_imap_idle_task_list[%p] ", input_epoll_fd, input_imap_idle_task_list);
+       EM_DEBUG_FUNC_BEGIN("input_epoll_fd[%d] input_imap_idle_task_list[%p]", input_epoll_fd, input_imap_idle_task_list);
+
        int err = EMAIL_ERROR_NONE;
        email_imap_idle_connection_info_t *connection_info = NULL;
        struct epoll_event ev = {0};
@@ -122,38 +127,44 @@ static int emcore_clear_old_connections(int input_epoll_fd, GList **input_imap_i
                goto FINISH_OFF;
        }
 
-       index = g_list_first(*input_imap_idle_task_list);
+       index = *input_imap_idle_task_list;
 
-       while(index) {
+       while (index) {
                connection_info = index->data;
+               if (connection_info) {
+                       /* Removes all fd from epoll_fd */
+                       ev.events  = EPOLLIN;
+                       ev.data.fd = connection_info->socket_fd;
 
-               /* Removes all fd from epoll_fd */
-               ev.events  = EPOLLIN;
-               ev.data.fd = connection_info->socket_fd;
-
-               if (epoll_ctl(input_epoll_fd, EPOLL_CTL_DEL, connection_info->socket_fd, &ev) == -1) {
-                       EM_DEBUG_LOG("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-               }
+                       if (epoll_ctl(input_epoll_fd, EPOLL_CTL_DEL, connection_info->socket_fd, &ev) == -1) {
+                               EM_DEBUG_LOG("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+                       }
 
-               /* Close connection */
-               if (connection_info->mail_stream)
-                       connection_info->mail_stream = mail_close(connection_info->mail_stream);
+                       /* Close connection */
+                       if (connection_info->mail_stream)
+                               connection_info->mail_stream = mail_close(connection_info->mail_stream);
 
-               EM_SAFE_FREE(connection_info->multi_user_name);
-               EM_SAFE_FREE(connection_info);
+                       g_free(connection_info->multi_user_name);
+                       g_free(connection_info);
+               }
 
-               index = g_list_next(index);
+               EM_DEBUG_LOG("Delete the index list");
+               *input_imap_idle_task_list = g_list_delete_link(*input_imap_idle_task_list, index);
+               index = *input_imap_idle_task_list;
        }
 
-       g_list_free(*input_imap_idle_task_list);
        *input_imap_idle_task_list = NULL;
 
 FINISH_OFF:
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-static int emcore_imap_idle_insert_sync_event(char *multi_user_name, int input_account_id, int input_mailbox_id, int *err_code)
+static int emcore_imap_idle_insert_sync_event(char *multi_user_name,
+                                                                                               int input_account_id,
+                                                                                               int input_mailbox_id,
+                                                                                               int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -201,9 +212,15 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_parse_imap_idle_response(char *multi_user_name, int input_account_id, int input_mailbox_id, MAILSTREAM *input_mailstream, int input_socket_fd)
+static int emcore_parse_imap_idle_response(char *multi_user_name,
+                                                                                       int input_account_id,
+                                                                                       int input_mailbox_id,
+                                                                                       MAILSTREAM *input_mailstream,
+                                                                                       int input_socket_fd)
 {
-       EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id [%d] input_mailstream[%p] input_socket_fd[%d]", input_account_id, input_mailbox_id, input_mailstream, input_socket_fd);
+       EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id [%d] input_mailstream[%p] input_socket_fd[%d]",
+                                               input_account_id, input_mailbox_id, input_mailstream, input_socket_fd);
+
        int err = EMAIL_ERROR_NONE;
        char *p = NULL;
        IMAPLOCAL *imap_local = NULL;
@@ -262,9 +279,17 @@ FINISH_OFF:
 }
 
 /* connects to given mailbox. send idle and returns socket id */
-static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **input_connection_list, email_account_t *input_account, email_mailbox_t *input_mailbox, MAILSTREAM **output_mailstream, int *output_socket_fd)
+static int emcore_connect_and_idle_on_mailbox(char *multi_user_name,
+                                                                                               GList **input_connection_list,
+                                                                                               email_account_t *input_account,
+                                                                                               email_mailbox_t *input_mailbox,
+                                                                                               MAILSTREAM **output_mailstream,
+                                                                                               int *output_socket_fd)
 {
-       EM_DEBUG_FUNC_BEGIN("input_connection_list [%p] input_account [%p] input_mailbox[%p] output_mailstream [%p] output_socket_fd [%p]", input_connection_list, input_account, input_mailbox, output_mailstream, output_socket_fd);
+       EM_DEBUG_FUNC_BEGIN("input_connection_list [%p] input_account [%p] input_mailbox[%p] "
+                                               "output_mailstream [%p] output_socket_fd [%p]",
+                                               input_connection_list, input_account, input_mailbox, output_mailstream, output_socket_fd);
+
        void            *mail_stream = NULL;
        char             cmd[128] = { 0, };
        char             tag[32] = { 0, };
@@ -278,7 +303,8 @@ static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **inp
        GList           *imap_idle_connection_list = NULL;
        email_imap_idle_connection_info_t *connection_info = NULL;
 
-       if (input_connection_list == NULL || input_account == NULL || input_mailbox == NULL || output_mailstream == NULL || output_socket_fd == NULL) {
+       if (input_connection_list == NULL || input_account == NULL || input_mailbox == NULL ||
+               output_mailstream == NULL || output_socket_fd == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
@@ -290,7 +316,12 @@ static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **inp
                EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
 
        /* Open connection to mailbox */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_mailbox->account_id, input_mailbox->mailbox_id, (void **)&mail_stream, &err) || !mail_stream) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       input_mailbox->account_id,
+                                                                                       input_mailbox->mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&mail_stream,
+                                                                                       &err) || !mail_stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -299,7 +330,6 @@ static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **inp
        net_stream = imap_local->netstream;
 
        /* check if ssl option is enabled on this account - shasikala.p */
-
        if (input_account->incoming_server_secure_connection){
                SSLSTREAM *ssl_stream = net_stream->stream;
                tcp_stream = ssl_stream->tcpstream;
@@ -397,18 +427,21 @@ static int emcore_refresh_alarm_for_imap_idle(char *multi_user_name)
        email_account_t *account_ref_list = NULL;
 
        /* Check whether the alarm is already existing */
-       if(emcore_check_alarm_by_class_id(EMAIL_ALARM_CLASS_IMAP_IDLE) == EMAIL_ERROR_NONE) {
+       if (emcore_check_alarm_by_class_id(EMAIL_ALARM_CLASS_IMAP_IDLE) == EMAIL_ERROR_NONE) {
                EM_DEBUG_LOG("Already exist. Remove old one.");
                emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_IMAP_IDLE, 0);
        }
 
-       if ((err = emcore_get_account_reference_list(multi_user_name, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_get_account_reference_list(multi_user_name,
+                                                                                                       &account_ref_list,
+                                                                                                       &account_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
        for (i = 0; i < account_count; i++) {
-               if (account_ref_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && (account_ref_list[i].check_interval == 0 || account_ref_list[i].peak_interval == 0)) {
+               if (account_ref_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 &&
+                       (account_ref_list[i].check_interval == 0 || account_ref_list[i].peak_interval == 0)) {
                        auto_sync_account_count++;
                }
        }
@@ -420,7 +453,12 @@ static int emcore_refresh_alarm_for_imap_idle(char *multi_user_name)
                time(&current_time);
                trigger_at_time = current_time + (29 * 60);
 
-               if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_IMAP_IDLE, 0, emcore_imap_idle_cb, NULL)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_add_alarm(multi_user_name,
+                                                                       trigger_at_time,
+                                                                       EMAIL_ALARM_CLASS_IMAP_IDLE,
+                                                                       0,
+                                                                       emcore_imap_idle_cb,
+                                                                       NULL)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
                }
        }
@@ -491,11 +529,14 @@ void* emcore_imap_idle_worker(void* thread_user_data)
                        }
 
                        if (g_list_length(zone_name_list) <= 1) {
-                               if ((err = emcore_get_account_reference_list(NULL, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) 
+                               if ((err = emcore_get_account_reference_list(NULL,
+                                                                                                                               &account_ref_list,
+                                                                                                                               &account_count)) != EMAIL_ERROR_NONE)
                                        EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
 
                                for (i = 0; i < account_count; i++) {
-                                       if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
+                                       if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 ||
+                                               (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
                                                EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id);
                                                continue;
                                        }
@@ -504,13 +545,22 @@ void* emcore_imap_idle_worker(void* thread_user_data)
 
                                        memset(&inbox_mailbox, 0, sizeof(email_mailbox_t));
 
-                                       if (!emcore_get_mailbox_by_type(NULL, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) {
+                                       if (!emcore_get_mailbox_by_type(NULL,
+                                                                                                       account_ref_list[i].account_id,
+                                                                                                       EMAIL_MAILBOX_TYPE_INBOX,
+                                                                                                       &inbox_mailbox,
+                                                                                                       &err)) {
                                                EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err);
                                                continue;
                                        }
 
                                        EM_DEBUG_LOG("Connect to IMAP server");
-                                       err = emcore_connect_and_idle_on_mailbox(NULL, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd);
+                                       err = emcore_connect_and_idle_on_mailbox(NULL,
+                                                                                                                               &imap_idle_connection_list,
+                                                                                                                               &(account_ref_list[i]),
+                                                                                                                               &inbox_mailbox,
+                                                                                                                               &mail_stream,
+                                                                                                                               &socket_fd);
 
                                        emcore_free_mailbox(&inbox_mailbox);
 
@@ -544,12 +594,15 @@ void* emcore_imap_idle_worker(void* thread_user_data)
 
                                        EM_DEBUG_LOG("Data name of node : [%s]", node->data);
 
-                                       if ((err = emcore_get_account_reference_list(node->data, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) {
+                                       if ((err = emcore_get_account_reference_list(node->data,
+                                                                                                                                       &account_ref_list,
+                                                                                                                                       &account_count)) != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
                                        }
 
                                        for (i = 0; i < account_count; i++) {
-                                               if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
+                                               if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 ||
+                                                       (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
                                                        EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id);
                                                        continue;
                                                }
@@ -558,13 +611,22 @@ void* emcore_imap_idle_worker(void* thread_user_data)
 
                                                memset(&inbox_mailbox, 0, sizeof(email_mailbox_t));
 
-                                               if (!emcore_get_mailbox_by_type(node->data, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) {
+                                               if (!emcore_get_mailbox_by_type(node->data,
+                                                                                                               account_ref_list[i].account_id,
+                                                                                                               EMAIL_MAILBOX_TYPE_INBOX,
+                                                                                                               &inbox_mailbox,
+                                                                                                               &err)) {
                                                        EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err);
                                                        continue;
                                                }
 
                                                EM_DEBUG_LOG("Connect to IMAP server");
-                                               err = emcore_connect_and_idle_on_mailbox(node->data, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd);
+                                               err = emcore_connect_and_idle_on_mailbox(node->data,
+                                                                                                                                       &imap_idle_connection_list,
+                                                                                                                                       &(account_ref_list[i]),
+                                                                                                                                       &inbox_mailbox,
+                                                                                                                                       &mail_stream,
+                                                                                                                                       &socket_fd);
 
                                                emcore_free_mailbox(&inbox_mailbox);
 
@@ -619,15 +681,18 @@ void* emcore_imap_idle_worker(void* thread_user_data)
                                        }
                                }
                                else {
-                                       if ((err = emcore_get_connection_info_by_socket_fd(imap_idle_connection_list, event_fd, &connection_info)) != EMAIL_ERROR_NONE) {
-                                               EM_DEBUG_LOG("emcore_get_connection_info_by_socket_fd couldn't find proper connection info. [%d]", err);
+                                       if ((err = emcore_get_connection_info_by_socket_fd(imap_idle_connection_list,
+                                                                                                                                               event_fd,
+                                                                                                                                               &connection_info)) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_LOG("emcore_get_connection_info_by_socket_fd "
+                                                                               "couldn't find proper connection info. [%d]", err);
                                                continue;
                                        }
 
-                                       if ((err = emcore_parse_imap_idle_response(connection_info->multi_user_name, 
-                                                                                                                       connection_info->account_id, 
-                                                                                                                       connection_info->mailbox_id, 
-                                                                                                                       connection_info->mail_stream, 
+                                       if ((err = emcore_parse_imap_idle_response(connection_info->multi_user_name,
+                                                                                                                       connection_info->account_id,
+                                                                                                                       connection_info->mailbox_id,
+                                                                                                                       connection_info->mail_stream,
                                                                                                                        connection_info->socket_fd)) != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION("emcore_parse_imap_idle_response failed. [%d] ", err);
                                                refresh_connection_flag = 1;
index 839d497..2dbe405 100755 (executable)
@@ -84,23 +84,25 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name, emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
+INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name, 
+                                                                                                       emstorage_mailbox_tbl_t *input_mailbox_tbl, 
+                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
+       EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], err_code[%p]", input_mailbox_tbl, err_code);
 
        int ret = false;
        int *mail_id_list = NULL, mail_id_list_count = 0;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account_ref = NULL;
 
-       if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
-               if (intput_mailbox_tbl)
-               EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
+       if (!input_mailbox_tbl || input_mailbox_tbl->account_id < 1) {
+               if (input_mailbox_tbl)
+               EM_DEBUG_EXCEPTION("Invalid Parameter. input_mailbox_tbl->account_id [%d]", input_mailbox_tbl->account_id);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(multi_user_name, intput_mailbox_tbl->account_id, false);
+       account_ref = emcore_get_account_reference(multi_user_name, input_mailbox_tbl->account_id, false);
        if (account_ref) {
                if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
@@ -109,26 +111,42 @@ INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name, emstorag
                }
        }
 
-       if (!emstorage_get_overflowed_mail_id_list(multi_user_name, intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_id, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
+       if (!emstorage_get_overflowed_mail_id_list(multi_user_name, 
+                                                                                               input_mailbox_tbl->account_id, 
+                                                                                               input_mailbox_tbl->mailbox_id, 
+                                                                                               input_mailbox_tbl->mail_slot_size, 
+                                                                                               &mail_id_list, 
+                                                                                               &mail_id_list_count, 
+                                                                                               false, 
+                                                                                               &err)) {
                if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
-                       EM_DEBUG_LOG_SEC("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
+                       EM_DEBUG_LOG_SEC("There are enough slot in input_mailbox_tbl [%s]", input_mailbox_tbl->mailbox_name);
                        err = EMAIL_ERROR_NONE;
                        ret = true;
                }
                else
                        EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
+
                goto FINISH_OFF;
        }
 
        if (mail_id_list) {
-               if (!emcore_delete_mail(multi_user_name, intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
+               if (!emcore_delete_mails_from_local_storage(multi_user_name, 
+                                                                                                       input_mailbox_tbl->account_id,
+                                                                                                       mail_id_list, 
+                                                                                                       mail_id_list_count, 
+                                                                                                       EMAIL_DELETE_LOCALLY, 
+                                                                                                       EMAIL_DELETED_BY_COMMAND, 
+                                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
        ret = true;
+
 FINISH_OFF:
+
        EM_SAFE_FREE(mail_id_list);
 
        if (account_ref) {
@@ -433,7 +451,12 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id
        FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&tmp_stream, 
+                                                                                       &err) || !tmp_stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
 
                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
@@ -757,7 +780,12 @@ INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbo
 
        /* connect mail server */
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       mailbox->account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&tmp_stream, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -853,7 +881,12 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_ma
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       mailbox_tbl->account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&tmp_stream, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -931,7 +964,12 @@ INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, in
 
        /* connect mail server */
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       input_account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&tmp_stream, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -1035,7 +1073,12 @@ INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resour
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       mailbox_tbl->account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&stream, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1070,7 +1113,12 @@ INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root,
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       mailbox_tbl->account_id, 
+                                                                                       0, 
+                                                                                       true,
+                                                                                       (void **)&stream, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
index aae0cd4..fa270b7 100755 (executable)
@@ -414,10 +414,10 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
 
                EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
 
-               if (!emstorage_update_server_uid(g_multi_user_name, old_server_uid_char, new_server_uid_char, &err)) {
+               if (!emstorage_update_server_uid(g_multi_user_name, 0, old_server_uid_char, new_server_uid_char, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
                }
-       
+
                if (!emstorage_update_read_mail_uid_by_server_uid(g_multi_user_name, old_server_uid_char, new_server_uid_char, mailbox, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
                }
@@ -450,7 +450,13 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
        EM_SAFE_FREE(new_server_uid);
 }
 
-INTERNAL_FUNC int emcore_move_mail_on_server_ex(char *multi_user_name, int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code)
+INTERNAL_FUNC int emcore_move_mail_on_server_ex(char *multi_user_name,
+                                                                                               int account_id,
+                                                                                               int src_mailbox_id,
+                                                                                               int mail_ids[],
+                                                                                               int num,
+                                                                                               int dest_mailbox_id,
+                                                                                               int *error_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        MAILSTREAM *stream = NULL;
@@ -479,10 +485,8 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(char *multi_user_name, int accou
        }
 
        ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
-       
        if (NULL == ref_account) {
                EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
-
                *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
@@ -492,9 +496,13 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(char *multi_user_name, int accou
                goto FINISH_OFF;
        }
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, src_mailbox_id, (void **)&stream, &err_code)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       src_mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err_code)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
-
                goto FINISH_OFF;
        }
 
@@ -600,7 +608,6 @@ FINISH_OFF:
                                EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
                        }
                }
-
        }
 #endif
        if (dest_mailbox) {
@@ -614,7 +621,13 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[], int num, int from_server, int *err_code)
+int emcore_delete_mails_from_imap4_server(char *multi_user_name,
+                                                                                       int account_id,
+                                                                                       int mailbox_id,
+                                                                                       int mail_ids[],
+                                                                                       int num,
+                                                                                       int from_server,
+                                                                                       int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -633,46 +646,36 @@ int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[],
        email_uid_range_set *uid_range_node = NULL;
        char **string_list = NULL;
        int string_count = 0;
-       emstorage_mail_tbl_t *mail_tbl_data = NULL;
 
-       if (num <= 0 || !mail_ids) {
+       if (num <= 0 || !mail_ids || account_id <= 0 || mailbox_id <= 0) {
                EM_DEBUG_EXCEPTION(" Invalid parameter ");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       for(i = 0; i < num; i++) {
-               if (!emstorage_get_downloaded_mail(multi_user_name, mail_ids[i], &mail_tbl_data, false, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
-
-                       if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {        /* not server mail */
-                               continue;
-                       }
-                       else
-                               break;
-               }
-       }
-
-       if (!mail_tbl_data || mail_tbl_data->account_id <= 0 || mail_tbl_data->mailbox_id <= 0) {
-               if(mail_tbl_data)
-                       EM_DEBUG_EXCEPTION("mail_tbl_data->account_id[%d] mail_tbl_data->mailbox_id[%d]", mail_tbl_data->account_id, mail_tbl_data->mailbox_id);
-               else
-                       EM_DEBUG_EXCEPTION("mail_tbl_data [%p]", mail_tbl_data);
-
-               goto FINISH_OFF;
-       }
-
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
-       /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
-       /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, server_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by server_uid  */
-       /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
-
-       if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
+       /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
+       /* Length is decided on the basis of remaining keywords in the Query */
+       /* to be formed later on in emstorage_get_id_set_from_mail_ids  */
+       /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, server_uid */
+       /* from mail_read_mail_uid_tbl where local_uid in (....) ORDER by server_uid  */
+       /* So length of comma separated strings which will be filled in (.....) in above query */
+       /* can be maximum QUERY_SIZE - 90  */
+       if (false == emcore_form_comma_separated_strings(mail_ids,
+                                                                                                               num,
+                                                                                                               QUERY_SIZE - 90,
+                                                                                                               &string_list,
+                                                                                                               &string_count,
+                                                                                                               &err)) {
                EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -681,16 +684,24 @@ int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[],
 
        for (i = 0; i < string_count; ++i) {
                /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order  */
-               if (false == emstorage_get_id_set_from_mail_ids(multi_user_name, string_list[i], &id_set, &id_set_count, &err)) {
+               if (false == emstorage_get_id_set_from_mail_ids(multi_user_name,
+                                                                                                               string_list[i],
+                                                                                                               &id_set,
+                                                                                                               &id_set_count,
+                                                                                                               &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H  */
-
-               len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*   1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag.  */
-
-               if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
+               /* Convert the sorted sequence of server mail ids to range sequences of given length. */
+               /* A range sequence will be like A : B, C, D: E, H  */
+               /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag.  */
+               len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;
+               if (false == emcore_convert_to_uid_range_set(id_set,
+                                                                                                               id_set_count,
+                                                                                                               &uid_range_set,
+                                                                                                               len_of_each_range,
+                                                                                                               &err)) {
                        EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -699,7 +710,6 @@ int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[],
 
                while (uid_range_node != NULL) {
                        /*  Remove comma from end of uid_range  */
-
                        uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
 
                        if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
@@ -823,7 +833,13 @@ int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[],
                                                                        if (mail[index]) {
                                                                                /* Clear entry from mail_read_mail_uid_tbl */
                                                                                if (mail[index]->server_mail_id != NULL) {
-                                                                                       if (!emstorage_remove_downloaded_mail(multi_user_name, mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err))  {
+                                                                                       if (!emstorage_remove_downloaded_mail(multi_user_name,
+                                                                                                                                                                       mail[index]->account_id,
+                                                                                                                                                                       mail[index]->mailbox_id,
+                                                                                                                                                                       mail[index]->mailbox_name,
+                                                                                                                                                                       mail[index]->server_mail_id,
+                                                                                                                                                                       true,
+                                                                                                                                                                       &err))  {
                                                                                                EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
                                                                                        }
                                                                                }
@@ -901,12 +917,12 @@ int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[],
        ret = true;
 
 FINISH_OFF:
+
        EM_SAFE_FREE(p);
        EM_SAFE_FREE(id_set); /*prevent 17954*/
 
-       if (stream)  {
+       if (stream)
                stream = mail_close (stream);
-       }
 
        emcore_free_comma_separated_strings(&string_list, &string_count);
 
@@ -1181,8 +1197,8 @@ int emcore_get_mail_contact_info_with_update(char *multi_user_name, email_mail_c
 
                is_searched = false;
                EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
-       
-               err = emcore_get_mail_display_name (multi_user_name, email_address,  
+
+               err = emcore_get_mail_display_name (multi_user_name, email_address,
                                             &contact_display_name_from_contact_info);
                if ( err == EMAIL_ERROR_NONE) {
                        contact_display_name = contact_display_name_from_contact_info;
@@ -1451,7 +1467,7 @@ int emcore_sync_contact_info(char *multi_user_name, int mail_id, int *err_code)
        }
 
        /*  Update DB */
-       if (!emstorage_change_mail_field(multi_user_name, mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {                                      
+       if (!emstorage_change_mail_field(multi_user_name, mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1557,7 +1573,7 @@ static int emcore_sync_address_info(char *multi_user_name, email_address_type_t
                EM_DEBUG_LOG_SEC("Search a contact  :  address[%s]", email_address);
 
                is_search = false;
-               error = emcore_get_mail_display_name (multi_user_name, email_address, 
+               error = emcore_get_mail_display_name (multi_user_name, email_address,
                                             &contact_display_name_from_contact_info);
                if (error == EMAIL_ERROR_NONE) {
                        EM_DEBUG_LOG_SEC(">>> contact display name[%s]", contact_display_name_from_contact_info);
@@ -2118,7 +2134,12 @@ INTERNAL_FUNC int emcore_gmime_download_attachment(char *multi_user_name, int ma
 
        /* open mail server. */
        if (!auto_download) {
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               server_mbox_id,
+                                                                                               true,
+                                                                                               (void **)&tmp_stream,
+                                                                                               &err) || !tmp_stream) {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                        if(err == EMAIL_ERROR_NO_SUCH_HOST)
                                err = EMAIL_ERROR_CONNECTION_FAILURE;
@@ -2344,7 +2365,7 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
        email_file_list *pFileList = NULL;
 #endif /*  SUPPORT_EXTERNAL_MEMORY */
        int decoded_attachment_size = 0;
-       
+
        memset(move_buf, 0x00, 512);
        memset(path_buf, 0x00, 512);
 
@@ -2408,7 +2429,12 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       server_mbox,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err) || !tmp_stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
 
                status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
@@ -2541,15 +2567,15 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 }
 #endif
 
-INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name, 
+INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
                                                                                                                void *mail_stream,
-                                                                                                               int account_id, 
-                                                                                                               int mail_id, 
-                                                                                                               int with_attach, 
+                                                                                                               int account_id,
+                                                                                                               int mail_id,
+                                                                                                               int with_attach,
                                                                                                                int limited_size,
-                                                                                                               int event_handle, 
-                                                                                                               int cancellable, 
-                                                                                                               int auto_download, 
+                                                                                                               int event_handle,
+                                                                                                               int cancellable,
+                                                                                                               int auto_download,
                                                                                                                int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], "
@@ -2642,7 +2668,12 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
        if (!mail_stream) {
 #if 0
                MAILSTREAM *tmp_stream = NULL;
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               mail->mailbox_id,
+                                                                                               true,
+                                                                                               (void **)&tmp_stream,
+                                                                                               &err) || !tmp_stream)  {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -2811,7 +2842,7 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
 
                /* FETCH sections and set content to message */
                if (!emcore_gmime_fetch_imap_body_sections(stream, uid, mail_id,
-                                                                                               cnt_info, message1, event_handle, 
+                                                                                               cnt_info, message1, event_handle,
                                                                                                auto_download, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_gmime_get_imap_sections failed");
                        goto FINISH_OFF;
@@ -2875,8 +2906,8 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, 
-                                                                       charset_plain_text, move_buf, path_buf, 
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0,
+                                                                       charset_plain_text, move_buf, path_buf,
                                                                        sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
@@ -2933,8 +2964,8 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
         memset(move_buf, 0x00, sizeof(move_buf));
         memset(path_buf, 0x00, sizeof(path_buf));
 
-               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 
-                                                                       0, "mime_entity", move_buf, path_buf, 
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id,
+                                                                       0, "mime_entity", move_buf, path_buf,
                                                                        sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
@@ -2950,10 +2981,10 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
        }
 
        /* Update local_preview_text */
-       if ((err = emcore_get_preview_text_from_file (multi_user_name, 
-                                                    mail->file_path_plain, 
-                                                    mail->file_path_html, 
-                                                    MAX_PREVIEW_TEXT_LENGTH, 
+       if ((err = emcore_get_preview_text_from_file (multi_user_name,
+                                                    mail->file_path_plain,
+                                                    mail->file_path_html,
+                                                    MAX_PREVIEW_TEXT_LENGTH,
                                                     &mail->preview_text)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
        }
@@ -2972,12 +3003,12 @@ INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name,
                int attachment_num_from_cnt_info;
                int inline_attachment_num_from_cnt_info;
 
-               if ((err = emcore_update_attachment_except_inline(multi_user_name, 
-                                                cnt_info, 
+               if ((err = emcore_update_attachment_except_inline(multi_user_name,
+                                                cnt_info,
                                                 mail->account_id,
-                                                mail->mail_id, 
-                                                mail->mailbox_id, 
-                                                &total_attachment_size,        
+                                                mail->mail_id,
+                                                mail->mailbox_id,
+                                                &total_attachment_size,
                                                 &attachment_num_from_cnt_info,
                                                 &inline_attachment_num_from_cnt_info)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
@@ -3372,20 +3403,28 @@ void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
        sprintf(g_new_server_uid, "%ld", destset->first);
        EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
 
-       if (!emstorage_update_server_uid(NULL, old_server_uid, g_new_server_uid, NULL)) {
+       if (!emstorage_update_server_uid(NULL, 0, old_server_uid, g_new_server_uid, NULL)) {
                EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
        }
 }
 
-static int emcore_delete_mails_from_remote_server(char *multi_user_name, int input_account_id, int input_mail_ids[], int input_mail_id_count, int input_delete_option)
+static int emcore_delete_mails_from_remote_server(char *multi_user_name,
+                                                                                                       int input_account_id,
+                                                                                                       int input_mailbox_id,
+                                                                                                       int input_mail_ids[],
+                                                                                                       int input_mail_id_count,
+                                                                                                       int input_delete_option)
 {
-       EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mail_ids[%p], input_mail_id_count[%d], input_delete_option [%d]", input_account_id, input_mail_ids, input_mail_id_count, input_delete_option);
+       EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mailbox_id[%d], input_mail_ids[%p], "
+                                               "input_mail_id_count[%d], input_delete_option [%d]",
+                                               input_account_id, input_mailbox_id, input_mail_ids, input_mail_id_count, input_delete_option);
 
        int err = EMAIL_ERROR_NONE;
        int i = 0;
-       email_account_t *account = NULL;
        char *noti_param_string = NULL;
        char mail_id_string[10] = { 0, };
+       email_account_t *account = NULL;
+       emstorage_mail_tbl_t *mail_tbl_data = NULL;
 #ifdef         __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
        int bulk_flag = false;
 #endif
@@ -3405,14 +3444,13 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
 
        /* Sending Notification */
        noti_param_string = em_malloc(sizeof(char) * 10 * input_mail_id_count);
-
-       if(!noti_param_string) {
+       if (!noti_param_string) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
-       for(i = 0; i < input_mail_id_count; i++) {
+       for (i = 0; i < input_mail_id_count; i++) {
                memset(mail_id_string, 0, sizeof(mail_id_string));
                SNPRINTF(mail_id_string, sizeof(mail_id_string), "%d,", input_mail_ids[i]);
                strcat(noti_param_string, mail_id_string);
@@ -3423,7 +3461,13 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_DELETE_MAIL_START ] >>>> ");
 
        if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-               if (!bulk_flag && !emcore_delete_mails_from_imap4_server(multi_user_name, input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
+               if (!bulk_flag && !emcore_delete_mails_from_imap4_server(multi_user_name,
+                                                                                                                                       input_account_id,
+                                                                                                                                       input_mailbox_id,
+                                                                                                                                       input_mail_ids,
+                                                                                                                                       input_mail_id_count,
+                                                                                                                                       input_delete_option,
+                                                                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mails_from_imap4_server failed [%d]", err);
                        if (err == EMAIL_ERROR_IMAP4_STORE_FAILURE)
                                err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
@@ -3434,7 +3478,11 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
 
 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
                        for(i = 0; i < input_mail_id_count; i++) {
-                               if (!emcore_delete_auto_download_activity(multi_user_name, input_account_id, input_mail_ids[i], 0, &err)) {
+                               if (!emcore_delete_auto_download_activity(multi_user_name,
+                                                                                                                       input_account_id,
+                                                                                                                       input_mail_ids[i],
+                                                                                                                       0,
+                                                                                                                       &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
                                }
                        }
@@ -3449,7 +3497,11 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
                else {
                        for(i = 0; i < input_mail_id_count; i++) {
-                               if (!emcore_delete_auto_download_activity(multi_user_name, input_account_id, input_mail_ids[i], 0, &err)) {
+                               if (!emcore_delete_auto_download_activity(multi_user_name,
+                                                                                                                       input_account_id,
+                                                                                                                       input_mail_ids[i],
+                                                                                                                       0,
+                                                                                                                       &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
                                }
                        }
@@ -3479,7 +3531,13 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
                        }
 
                        /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
-                       if (!emstorage_remove_downloaded_mail(multi_user_name, input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))  {
+                       if (!emstorage_remove_downloaded_mail(multi_user_name,
+                                                                                                       input_account_id,
+                                                                                                       mail->mailbox_id,
+                                                                                                       mail->server_mailbox_name,
+                                                                                                       mail->server_mail_id,
+                                                                                                       true,
+                                                                                                       &err))  {
                                EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
                        }
                }
@@ -3487,6 +3545,33 @@ static int emcore_delete_mails_from_remote_server(char *multi_user_name, int inp
 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
        }
 
+       for (i = 0; i < input_mail_id_count; i++) {
+               if (!emstorage_get_downloaded_mail(multi_user_name,
+                                                                                       input_mail_ids[i],
+                                                                                       &mail_tbl_data,
+                                                                                       false,
+                                                                                       &err)) {
+                       /* not server mail */
+                       EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
+                       continue;
+               }
+
+               if (!emstorage_remove_downloaded_mail(multi_user_name,
+                                                                                               mail_tbl_data->account_id,
+                                                                                               mail_tbl_data->mailbox_id,
+                                                                                               mail_tbl_data->server_mailbox_name,
+                                                                                               mail_tbl_data->server_mail_id,
+                                                                                               true,
+                                                                                               &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail feiled : [%d]", err);
+               }
+
+               if (mail_tbl_data)
+                       emstorage_free_mail(&mail_tbl_data, 1, NULL);
+
+               mail_tbl_data = NULL;
+       }
+
 FINISH_OFF:
 
        if (err == EMAIL_ERROR_NONE) {
@@ -3498,6 +3583,9 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event failed [ NOTI_DELETE_MAIL_FAIL ] >>>> ");
        }
 
+       if (mail_tbl_data)
+               emstorage_free_mail(&mail_tbl_data, 1, NULL);
+
        EM_SAFE_FREE(noti_param_string);
 
        if (account) {
@@ -3509,16 +3597,27 @@ FINISH_OFF:
        return err;
 }
 
-int emcore_delete_mail(char *multi_user_name, int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
+int emcore_delete_mail(char *multi_user_name,
+                                               int account_id,
+                                               int mailbox_id,
+                                               int mail_ids[],
+                                               int num,
+                                               int from_server,
+                                               int noti_param_1,
+                                               int noti_param_2,
+                                               int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num[%d], from_server[%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], mail_ids[%p], num[%d], from_server[%d], "
+                                               "noti_param_1 [%d], noti_param_2 [%d], err_code[%p]",
+                                               account_id, mailbox_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account = NULL;
 
        if (!account_id || !mail_ids || !num)  {
-               EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
+               EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]",
+                                                       account_id, mail_ids, num, from_server);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
@@ -3533,17 +3632,28 @@ int emcore_delete_mail(char *multi_user_name, int account_id, int mail_ids[], in
        /* FINISH_OFF_IF_CANCELED; */
 
        if (from_server == EMAIL_DELETE_LOCALLY) /* Delete mails from local storage*/ {
-               emcore_delete_mails_from_local_storage(multi_user_name, account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
+               emcore_delete_mails_from_local_storage(multi_user_name,
+                                                                                               account_id,
+                                                                                               mail_ids,
+                                                                                               num,
+                                                                                               noti_param_1,
+                                                                                               noti_param_2,
+                                                                                               err_code);
                emcore_display_unread_in_badge(multi_user_name);
        }
        else {   /* Delete mails from server*/
-               emcore_delete_mails_from_remote_server(multi_user_name, account_id, mail_ids, num, from_server);
+               emcore_delete_mails_from_remote_server(multi_user_name,
+                                                                                               account_id,
+                                                                                               mailbox_id,
+                                                                                               mail_ids,
+                                                                                               num,
+                                                                                               from_server);
        }
 
-
        ret = true;
 
 FINISH_OFF:
+
        if (from_server)
                emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0  :  err);
 
@@ -3657,14 +3767,14 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name, 
-                                                                                                               int input_account_id, 
-                                                                                                               int input_mailbox_id, 
+INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name,
+                                                                                                               int input_account_id,
+                                                                                                               int input_mailbox_id,
                                                                                                                int input_mailbox_type,
-                                                                                                               int input_from_server, 
+                                                                                                               int input_from_server,
                                                                                                                int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id[%d] input_from_server[%d] err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id[%d] input_from_server[%d] err_code[%p]",
                                                        input_account_id, input_mailbox_id, input_from_server, err_code);
 
        int   ret = false;
@@ -3683,14 +3793,18 @@ INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name,
        SNPRINTF(conditional_clause, QUERY_SIZE, " where ");
 
        if (input_mailbox_id <= 0 && input_mailbox_type > 0) {
-               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE, 
+               SNPRINTF(conditional_clause + strlen(conditional_clause),
+                                       QUERY_SIZE - strlen(conditional_clause),
                                        "mailbox_type = %d ", input_mailbox_type);
        } else if (input_mailbox_id > 0 && input_mailbox_type <= 0) {
-               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE, 
+               SNPRINTF(conditional_clause + strlen(conditional_clause),
+                                       QUERY_SIZE - strlen(conditional_clause),
                                        "mailbox_id = %d ", input_mailbox_id);
        } else {
-               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE,
-                                       "mailbox_type = %d and mailbox_id = %d ", input_mailbox_type, input_mailbox_id);
+               SNPRINTF(conditional_clause + strlen(conditional_clause),
+                                       QUERY_SIZE - strlen(conditional_clause),
+                                       "mailbox_type = %d and mailbox_id = %d ",
+                                       input_mailbox_type, input_mailbox_id);
        }
 
        EM_DEBUG_LOG("command : [%s]", conditional_clause);
@@ -3704,13 +3818,14 @@ INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name,
        EM_DEBUG_LOG("emstorage_query_mail_id_list returns [%d]", mail_id_count);
 
        if (mail_id_count > 0) {
-               if (!emcore_delete_mail(multi_user_name, 
-                                                               input_account_id, 
-                                                               mail_id_array, 
-                                                               mail_id_count, 
-                                                               input_from_server, 
-                                                               EMAIL_DELETED_BY_COMMAND, 
-                                                               false, 
+               if (!emcore_delete_mail(multi_user_name,
+                                                               input_account_id,
+                                                               input_mailbox_id,
+                                                               mail_id_array,
+                                                               mail_id_count,
+                                                               input_from_server,
+                                                               EMAIL_DELETED_BY_COMMAND,
+                                                               false,
                                                                &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                        goto FINISH_OFF;
@@ -3730,9 +3845,18 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name, int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
+INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
+                                                                                                                       int account_id,
+                                                                                                                       int *mail_ids,
+                                                                                                                       int num,
+                                                                                                                       int noti_param_1,
+                                                                                                                       int noti_param_2,
+                                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], "
+                                               "noti_param_1 [%d], noti_param_2 [%d], err_code[%p]",
+                                               account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
+
        int ret = false, err = EMAIL_ERROR_NONE, i;
        emstorage_mail_tbl_t *result_mail_list = NULL;
        char mail_id_string[10], *noti_param_string = NULL;
@@ -3740,27 +3864,31 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
     char path_buf[512] = {0,};
 
        /* Getting mail list by using select mail_id [in] */
-       if(!emstorage_get_mail_field_by_multiple_mail_id(multi_user_name, mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
+       if (!emstorage_get_mail_field_by_multiple_mail_id(multi_user_name,
+                                                                                                               mail_ids,
+                                                                                                               num,
+                                                                                                               RETRIEVE_SUMMARY,
+                                                                                                               &result_mail_list, false,
+                                                                                                               &err) || !result_mail_list) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Deleting mails by using select mail_id [in] */
-       if(!emstorage_delete_multiple_mails(multi_user_name, mail_ids, num, true, &err)) {
+       if (!emstorage_delete_multiple_mails(multi_user_name, mail_ids, num, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Sending Notification */
        noti_param_string = em_malloc(sizeof(char) * 10 * num);
-
-       if(!noti_param_string) {
+       if (!noti_param_string) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
-       for(i = 0; i < num; i++) {
+       for (i = 0; i < num; i++) {
                memset(mail_id_string, 0, sizeof(mail_id_string));
                SNPRINTF(mail_id_string, sizeof(mail_id_string), "%d,", mail_ids[i]);
                strcat(noti_param_string, mail_id_string);
@@ -3773,16 +3901,16 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
        /* Updating Thread informations */
        for(i = 0; i < num; i++) {
                if (result_mail_list[i].thread_id != 0) {
-                       if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                               account_id, 
+                       if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                               account_id,
                                                                                                                result_mail_list[i].mailbox_id,
                                                                                                                result_mail_list[i].mailbox_type,
-                                                                                                               result_mail_list[i].thread_id, 
-                                                                                                               NULL, 
-                                                                                                               0, 
-                                                                                                               0, 
-                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, 
-                                                                                                               false, 
+                                                                                                               result_mail_list[i].thread_id,
+                                                                                                               NULL,
+                                                                                                               0,
+                                                                                                               0,
+                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE,
+                                                                                                               false,
                                                                                                                &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                                goto FINISH_OFF;
@@ -3791,7 +3919,11 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
        }
 
        /* Thread information should be updated as soon as possible. */
-       if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
+       if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FINISH,
+                                                                       account_id,
+                                                                       noti_param_1,
+                                                                       noti_param_string,
+                                                                       noti_param_2))
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
 
        for(i = 0; i < num; i++) {
@@ -3804,7 +3936,15 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
 
                /* Deleting Directories */
                /*  delete mail contents from filesystem */
-               if (!emstorage_get_save_name(multi_user_name, account_id, result_mail_list[i].mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err)) {
+               if (!emstorage_get_save_name(multi_user_name,
+                                                                       account_id,
+                                                                       result_mail_list[i].mail_id,
+                                                                       0,
+                                                                       NULL,
+                                                                       move_buf,
+                                                                       path_buf,
+                                                                       sizeof(path_buf),
+                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -3814,7 +3954,12 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
                }
 
                /* Deleting Meeting Request */
-               if (!emstorage_delete_meeting_request(multi_user_name, account_id, result_mail_list[i].mail_id, 0, false, &err)) {
+               if (!emstorage_delete_meeting_request(multi_user_name,
+                                                                                       account_id,
+                                                                                       result_mail_list[i].mail_id,
+                                                                                       0,
+                                                                                       false,
+                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
                        if (err != EMAIL_ERROR_CONTACT_NOT_FOUND) {
                                goto FINISH_OFF;
@@ -3822,9 +3967,17 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
                }
 
                /* Deleting mail_read_mail_uid_tbl */
-               if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, result_mail_list[i].server_mailbox_name, result_mail_list[i].server_mail_id, true, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed [%d]", err);
-                       goto FINISH_OFF;
+               if (noti_param_1 != EMAIL_DELETE_LOCAL_AND_SERVER) {
+                       if (!emstorage_remove_downloaded_mail(multi_user_name,
+                                                                                                       account_id,
+                                                                                                       result_mail_list[i].mailbox_id,
+                                                                                                       result_mail_list[i].server_mailbox_name,
+                                                                                                       result_mail_list[i].server_mail_id,
+                                                                                                       false,
+                                                                                                       &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
                }
        }
 
@@ -3840,8 +3993,12 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
 
 FINISH_OFF:
 
-       if(ret == false) {
-               if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FAIL, account_id, noti_param_1, noti_param_string, noti_param_2))
+       if (ret == false) {
+               if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FAIL,
+                                                                               account_id,
+                                                                               noti_param_1,
+                                                                               noti_param_string,
+                                                                               noti_param_2))
                        EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FAIL ] >>>> ");
        }
 
@@ -3891,7 +4048,12 @@ static int emcore_delete_mails_from_pop3_server(char *multi_user_name, email_acc
                }
 
                if (stream == NULL)  {
-                       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_account->account_id, 0, (void **)&stream, &err))  {
+                       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                                       input_account->account_id,
+                                                                                                       0,
+                                                                                                       true,
+                                                                                                       (void **)&stream,
+                                                                                                       &err))  {
                                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -3922,7 +4084,13 @@ static int emcore_delete_mails_from_pop3_server(char *multi_user_name, email_acc
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_remove_downloaded_mail(multi_user_name, input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
+               if (!emstorage_remove_downloaded_mail(multi_user_name,
+                                                                                               input_account->account_id,
+                                                                                               mail_tbl_data->mailbox_id,
+                                                                                               mail_tbl_data->server_mailbox_name,
+                                                                                               mail_tbl_data->server_mail_id,
+                                                                                               true,
+                                                                                               &err))
                        EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
 
 NOT_FOUND_ON_SERVER :
@@ -4061,12 +4229,22 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(char *
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if ((err = emstorage_query_mail_id_list(multi_user_name, conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_mail_id_list(multi_user_name,
+                                                                                       conditional_clause_string,
+                                                                                       false,
+                                                                                       &result_mail_id_list,
+                                                                                       &result_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mails_from_local_storage(multi_user_name, mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
+       if (!emcore_delete_mails_from_local_storage(multi_user_name,
+                                                                                               mailbox_tbl->account_id,
+                                                                                               result_mail_id_list,
+                                                                                               result_count,
+                                                                                               EMAIL_DELETE_LOCALLY,
+                                                                                               EMAIL_DELETED_BY_COMMAND,
+                                                                                               &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage [%d]", err);
                goto FINISH_OFF;
        }
@@ -4136,7 +4314,15 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mail(multi_user_name, input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
+       if (!emcore_delete_mail(multi_user_name,
+                                                       input_account_id,
+                                                       input_mailbox_id,
+                                                       result_mail_id_list,
+                                                       result_count,
+                                                       EMAIL_DELETE_FROM_SERVER,
+                                                       1,
+                                                       EMAIL_DELETED_BY_COMMAND,
+                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail [%d]", err);
                goto FINISH_OFF;
        }
@@ -4426,8 +4612,9 @@ int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int
                return false;
        }
 
-       int ret = true;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
+       int before_tr_begin = 0;
        char attachment_folder_path[MAX_PATH] = {0, };
        emstorage_attachment_tbl_t *attachment_tbl = NULL;
 
@@ -4439,6 +4626,7 @@ int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int
        /*  BEGIN TRANSACTION; */
        if(!emstorage_begin_transaction(multi_user_name, NULL, NULL, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", error);
+               before_tr_begin = 1;
                goto FINISH_OFF;
        }
 
@@ -4447,7 +4635,6 @@ int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int
                if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        error = EMAIL_ERROR_DB_FAILURE;
 
-               ret = false;
                goto FINISH_OFF;
        }
 
@@ -4463,12 +4650,21 @@ int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int
                }
        }
 
-       if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
-               error = EMAIL_ERROR_DB_FAILURE;
-               ret = false;
-       }
+       ret = true;
 
 FINISH_OFF:
+
+       if (ret == true) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
+                       error = EMAIL_ERROR_DB_FAILURE;
+                       ret = false;
+               }
+       } else {
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) {
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
        if (attachment_tbl)
                emstorage_free_attachment(&attachment_tbl, 1, NULL);
 
@@ -4521,7 +4717,7 @@ static int emcore_mail_update_attachment_data(char *multi_user_name, int input_m
                }
                attachment_id = attachment_tbl.attachment_id;
        }
-       
+
        if (!emstorage_get_save_name(multi_user_name, attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                goto FINISH_OFF;
@@ -4742,14 +4938,14 @@ INTERNAL_FUNC int emcore_mail_copy(char *multi_user_name, int mail_id, email_mai
         memset(real_file_path, 0x00, sizeof(real_file_path));
         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_html);
 
-               if (!emstorage_get_save_name(multi_user_name, 
-                                    dst_mailbox->account_id, 
-                                    mail->mail_id, 
-                                    0, 
-                                    filename, 
-                                    real_path, 
+               if (!emstorage_get_save_name(multi_user_name,
+                                    dst_mailbox->account_id,
+                                    mail->mail_id,
+                                    0,
+                                    filename,
+                                    real_path,
                                     virtual_path,
-                                    sizeof(virtual_path), 
+                                    sizeof(virtual_path),
                                     &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        g_free(filename);
@@ -4824,14 +5020,14 @@ INTERNAL_FUNC int emcore_mail_copy(char *multi_user_name, int mail_id, email_mai
             memset(real_file_path, 0x00, sizeof(real_file_path));
             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, atch_list[i].attachment_path);
 
-                       if (!emstorage_get_save_name(multi_user_name, 
-                                        dst_mailbox->account_id, 
-                                        mail->mail_id, 
-                                        i+1, 
-                                        atch_list[i].attachment_name, 
-                                        real_path, 
-                                        virtual_path, 
-                                        sizeof(virtual_path), 
+                       if (!emstorage_get_save_name(multi_user_name,
+                                        dst_mailbox->account_id,
+                                        mail->mail_id,
+                                        i+1,
+                                        atch_list[i].attachment_name,
+                                        real_path,
+                                        virtual_path,
+                                        sizeof(virtual_path),
                                         &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                break;
@@ -5017,23 +5213,33 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
                dest_prev_thread_id_list[i] = dest_prev_thread_id;
        }
 
-       if(!emstorage_move_multiple_mails_on_db(multi_user_name, account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
+       if (!emstorage_move_multiple_mails_on_db(multi_user_name,
+                                                                                               account_id,
+                                                                                               dst_mailbox_id,
+                                                                                               mail_ids,
+                                                                                               mail_ids_count,
+                                                                                               true,
+                                                                                               &err)) {
                EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails_on_db failed [%d]", err);
                goto FINISH_OFF;
        }
-       
+
        for (i = 0; i < mail_ids_count; i++) {
-               if (mail_list[i].subject == NULL) 
+               if (mail_list[i].subject == NULL)
                        continue;
+
                if (dest_prev_thread_id_list[i] == -1) {
-                       if (em_find_pos_stripped_subject_for_thread_view(mail_list[i].subject, stripped_subject, sizeof(stripped_subject)) != EMAIL_ERROR_NONE) {
+                       if (em_find_pos_stripped_subject_for_thread_view(mail_list[i].subject,
+                                                                                                                               stripped_subject,
+                                                                                                                               sizeof(stripped_subject)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
                        } else {
                                EM_DEBUG_LOG_SEC("subject: [%s]", mail_list[i].subject);
                                if (EM_SAFE_STRLEN(stripped_subject) >= 2) {
                                        find_striped_subject = 1;
                                }
-                               EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
+                               EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
+                                                                       EM_SAFE_STRLEN(stripped_subject), stripped_subject);
                        }
 
                        if (find_striped_subject) {
@@ -5043,6 +5249,7 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
                                                break;
                                        }
                                }
+
                                if (j == i)
                                        dest_prev_thread_id_list[i] = mail_ids[i];
                        } else {
@@ -5050,7 +5257,14 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
                        }
                }
 
-               if (!emstorage_update_thread_id_of_mail(multi_user_name, account_id, dst_mailbox_id, mail_ids[i], dest_prev_thread_id_list[i], 0, false, &err)) {
+               if (!emstorage_update_thread_id_of_mail(multi_user_name,
+                                                                                               account_id,
+                                                                                               dst_mailbox_id,
+                                                                                               mail_ids[i],
+                                                                                               dest_prev_thread_id_list[i],
+                                                                                               0,
+                                                                                               false,
+                                                                                               &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -5094,21 +5308,21 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
                        goto FINISH_OFF;
                }
 
-               /* Get the thread_id of moved mail */   
+               /* Get the thread_id of moved mail */
                if (emstorage_get_thread_id_of_thread_mails(multi_user_name, p_mail_data, &p_thread_id, &p_lastest_mail_id, &p_thread_item_count) != EMAIL_ERROR_NONE)
                        EM_DEBUG_LOG("emstorage_get_thread_id_of_thread_mails is failed.");
 
                /* Original mailbox replace thread id */
-               if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                       account_id, 
+               if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                       account_id,
                                                                                                        mail_list[i].mailbox_id,
                                                                                                        mail_list[i].mailbox_type,
-                                                                                                       mail_list[i].thread_id, 
-                                                                                                       NULL, 
-                                                                                                       0, 
-                                                                                                       0, 
-                                                                                                       NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, 
-                                                                                                       false, 
+                                                                                                       mail_list[i].thread_id,
+                                                                                                       NULL,
+                                                                                                       0,
+                                                                                                       0,
+                                                                                                       NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE,
+                                                                                                       false,
                                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        goto FINISH_OFF;
@@ -5116,31 +5330,31 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
 
                /* Destination mailbox replace thread id */
                if (p_thread_id == -1) {
-                       if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                               account_id, 
+                       if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                               account_id,
                                                                                                                p_mail_data->mailbox_id,
                                                                                                                p_mail_data->mailbox_type,
-                                                                                                               p_mail_data->mail_id, 
-                                                                                                               NULL, 
-                                                                                                               p_mail_data->mail_id, 
-                                                                                                               1, 
-                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, 
-                                                                                                               false, 
+                                                                                                               p_mail_data->mail_id,
+                                                                                                               NULL,
+                                                                                                               p_mail_data->mail_id,
+                                                                                                               1,
+                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE,
+                                                                                                               false,
                                                                                                                &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                                goto FINISH_OFF;
                        }
                } else {
-                       if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                               account_id, 
+                       if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                               account_id,
                                                                                                                p_mail_data->mailbox_id,
                                                                                                                p_mail_data->mailbox_type,
-                                                                                                               dest_prev_thread_id_list[i], 
-                                                                                                               NULL, 
-                                                                                                               0, 
-                                                                                                               0, 
-                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, 
-                                                                                                               false, 
+                                                                                                               dest_prev_thread_id_list[i],
+                                                                                                               NULL,
+                                                                                                               0,
+                                                                                                               0,
+                                                                                                               NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE,
+                                                                                                               false,
                                                                                                                &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                                goto FINISH_OFF;
@@ -5187,7 +5401,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(char *multi_user_name, int account_
        int i = 0;
 
        mail_id = mail_ids[0];
-       
+
        ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed  :  get account reference[%d]", account_id);
@@ -5221,7 +5435,12 @@ INTERNAL_FUNC int emcore_move_mail_on_server(char *multi_user_name, int account_
                        goto FINISH_OFF;
                }
 
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, src_mailbox_id, (void **)&stream, &err_code))                /* faizan.h@samsung.com mail_move_fix_07042009 */ {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               src_mailbox_id,
+                                                                                               true,
+                                                                                               (void **)&stream,
+                                                                                               &err_code))             /* faizan.h@samsung.com mail_move_fix_07042009 */ {
                        EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err_code);
 
                        ret = 0;
@@ -5344,15 +5563,15 @@ static int emcore_copy_mail_to_another_account_on_local_storeage(char *multi_use
        /* Check download status */
        if(!(mail_data->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED)) {
                /* If not downloaded, download fully */
-               if (!emcore_gmime_download_body_sections(multi_user_name, 
-                                       NULL, 
+               if (!emcore_gmime_download_body_sections(multi_user_name,
+                                       NULL,
                                        input_source_mailbox->account_id,
-                                       input_mail_id, 
-                                       (mail_data->attachment_count > 0)?1:0, 
-                                       NO_LIMITATION, 
-                                       input_task_id, 
-                                       0, 
-                                       0, 
+                                       input_mail_id,
+                                       (mail_data->attachment_count > 0) ? 1 : 0,
+                                       NO_LIMITATION,
+                                       input_task_id,
+                                       0,
+                                       0,
                                        &err)) {
                        EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
                        goto FINISH_OFF;
@@ -5451,18 +5670,34 @@ INTERNAL_FUNC int emcore_move_mail_to_another_account(char *multi_user_name, int
                        EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed [%d]", err);
 
                        /* if append is failed, restore source mail and delete copied mail. */
-                       if(!emcore_set_flags_field(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
+                       if (!emcore_set_flags_field(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
                                EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if(!emcore_delete_mail(multi_user_name, target_mailbox->account_id, &moved_mail_id, 1, false, 0, 0, &err_for_delete_mail))
+                       if (!emcore_delete_mail(multi_user_name,
+                                                                       target_mailbox->account_id,
+                                                                       target_mailbox->mailbox_id,
+                                                                       &moved_mail_id,
+                                                                       1,
+                                                                       EMAIL_DELETE_LOCALLY,
+                                                                       EMAIL_DELETED_BY_COMMAND,
+                                                                       0,
+                                                                       &err_for_delete_mail))
                                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err_for_delete_mail);
                        goto FINISH_OFF;
                }
        }
 
-       if(!emcore_delete_mail(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, true, 0, 0, &err)) {
+       if (!emcore_delete_mail(multi_user_name,
+                                                       source_mailbox->account_id,
+                                                       source_mailbox->mailbox_id,
+                                                       &input_mail_id,
+                                                       1,
+                                                       EMAIL_DELETE_FROM_SERVER,
+                                                       EMAIL_DELETED_BY_COMMAND,
+                                                       0,
+                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -5489,11 +5724,11 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_save_mail_file(char *multi_user_name, int account_id, int mail_id, int attachment_id, 
-                                                                               char *src_file_path, char *file_name, char *full_path, char *virtual_path, 
+INTERNAL_FUNC int emcore_save_mail_file(char *multi_user_name, int account_id, int mail_id, int attachment_id,
+                                                                               char *src_file_path, char *file_name, char *full_path, char *virtual_path,
                                                                                int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d], file_name[%p], full_path[%p], err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d], file_name[%p], full_path[%p], err_code[%p]",
                                                account_id, mail_id, attachment_id, file_name, full_path, err_code);
 
        int err = EMAIL_ERROR_NONE;
@@ -5508,14 +5743,14 @@ INTERNAL_FUNC int emcore_save_mail_file(char *multi_user_name, int account_id, i
                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_get_save_name(multi_user_name, 
-                                                               account_id, 
-                                                               mail_id, 
-                                                               attachment_id, 
-                                                               file_name, 
-                                                               full_path, 
-                                                               virtual_path, 
+
+       if (!emstorage_get_save_name(multi_user_name,
+                                                               account_id,
+                                                               mail_id,
+                                                               attachment_id,
+                                                               file_name,
+                                                               full_path,
+                                                               virtual_path,
                                                                MAX_PATH,
                                                                &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
@@ -5537,15 +5772,15 @@ FINISH_OFF:
 }
 
 /* description : update mail information */
-INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, 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, 
+INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, 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)
 {
-       EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], " 
-                                               "input_meeting_request[%p], input_from_eas[%d]", 
-                                               input_mail_data, input_attachment_data_list, input_attachment_count, 
+       EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], "
+                                               "input_meeting_request[%p], input_from_eas[%d]",
+                                               input_mail_data, input_attachment_data_list, input_attachment_count,
                                                input_meeting_request, input_from_eas);
 
        char                   filename_buf[1024]          = {0, };
@@ -5564,7 +5799,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
     char                   *prefix_path                = NULL;
     char                   real_file_path[MAX_PATH]    = {0};
 
-       if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || 
+       if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) ||
                (!input_attachment_count &&input_attachment_data_list)) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -5592,7 +5827,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                                goto FINISH_OFF2;
                        }
                }
-               
+
                if (input_mail_data->file_path_html)  {
             memset(real_file_path, 0x00, sizeof(real_file_path));
             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_html);
@@ -5603,7 +5838,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                                goto FINISH_OFF2;
                        }
                }
-               
+
                if (input_attachment_count && input_attachment_data_list)  {
                        for (i = 0; i < input_attachment_count; i++)  {
                                if (input_attachment_data_list[i].save_status) {
@@ -5624,7 +5859,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                 emcore_calc_mail_size(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
        }
 
-       if (input_mail_data->file_path_plain)  {   
+       if (input_mail_data->file_path_plain)  {
                /* Save plain text body. */
                if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
@@ -5632,14 +5867,14 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                        goto FINISH_OFF2;
                }
 
-               if (!emcore_save_mail_file(multi_user_name, 
-                                    input_mail_data->account_id, 
-                                    input_mail_data->mail_id, 
-                                                                       0, 
-                                    input_mail_data->file_path_plain, 
-                                    body_text_file_name, 
-                                    filename_buf, 
-                                    virtual_path, 
+               if (!emcore_save_mail_file(multi_user_name,
+                                    input_mail_data->account_id,
+                                    input_mail_data->mail_id,
+                                                                       0,
+                                    input_mail_data->file_path_plain,
+                                    body_text_file_name,
+                                    filename_buf,
+                                    virtual_path,
                                     &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                        goto FINISH_OFF2;
@@ -5648,7 +5883,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                input_mail_data->file_path_plain = EM_SAFE_STRDUP(virtual_path);
        }
 
-       if (input_mail_data->file_path_html)  {   
+       if (input_mail_data->file_path_html)  {
                /*  Save HTML text body. */
                EM_SAFE_FREE(body_text_file_name);
                if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
@@ -5657,14 +5892,14 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                        goto FINISH_OFF2;
                }
 
-               if (!emcore_save_mail_file(multi_user_name, 
-                                    input_mail_data->account_id, 
-                                    input_mail_data->mail_id, 
-                                                                       0, 
-                                    input_mail_data->file_path_html, 
-                                    body_text_file_name, 
-                                    filename_buf, 
-                                    virtual_path, 
+               if (!emcore_save_mail_file(multi_user_name,
+                                    input_mail_data->account_id,
+                                    input_mail_data->mail_id,
+                                                                       0,
+                                    input_mail_data->file_path_html,
+                                    body_text_file_name,
+                                    filename_buf,
+                                    virtual_path,
                                     &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                        goto FINISH_OFF2;
@@ -5673,15 +5908,15 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                input_mail_data->file_path_html = EM_SAFE_STRDUP(virtual_path);
        }
 
-       if (input_mail_data->file_path_mime_entity)  {   
+       if (input_mail_data->file_path_mime_entity)  {
                /*  Save mime entity. */
-               if (!emcore_save_mail_file(multi_user_name, 
-                                    input_mail_data->account_id, 
-                                    input_mail_data->mail_id, 
-                                                                       0, 
-                                    input_mail_data->file_path_mime_entity, 
-                                    "mime_entity", 
-                                    filename_buf, 
+               if (!emcore_save_mail_file(multi_user_name,
+                                    input_mail_data->account_id,
+                                    input_mail_data->mail_id,
+                                                                       0,
+                                    input_mail_data->file_path_mime_entity,
+                                    "mime_entity",
+                                    filename_buf,
                                     virtual_path,
                                     &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
@@ -5696,7 +5931,7 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
                int j = 0;
                int compare_result = 1;
                email_attachment_data_t *temp_attachment_data = NULL;
-                               
+
                if ((err = emcore_get_attachment_data_list(multi_user_name, input_mail_data->mail_id, &ori_attachment_data_list, &ori_attachment_count)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed : [%d]", err);
                }
@@ -5799,10 +6034,10 @@ INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *i
 
        EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
        if (input_mail_data->preview_text == NULL) {
-               if ( (err =emcore_get_preview_text_from_file(multi_user_name, 
-                                                    input_mail_data->file_path_plain, 
-                                                    input_mail_data->file_path_html, 
-                                                    MAX_PREVIEW_TEXT_LENGTH, 
+               if ( (err =emcore_get_preview_text_from_file(multi_user_name,
+                                                    input_mail_data->file_path_plain,
+                                                    input_mail_data->file_path_html,
+                                                    MAX_PREVIEW_TEXT_LENGTH,
                                                     &(input_mail_data->preview_text))) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
                        if (err != EMAIL_ERROR_EMPTY_FILE)
@@ -6130,7 +6365,12 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(char *multi_user_name, int mail_i
        FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mail->mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -6258,7 +6498,12 @@ INTERNAL_FUNC int emcore_sync_seen_flag_with_server(char *multi_user_name, int m
        }
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mail->mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -6417,7 +6662,7 @@ INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachme
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-INTERNAL_FUNC int emcore_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code) 
+INTERNAL_FUNC int emcore_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
 
@@ -6570,8 +6815,13 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(char *multi_user_name, int
                err = EMAIL_ERROR_NOT_SUPPORTED;
                goto FINISH_OFF;
        }
-       
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mail->mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -6749,7 +6999,7 @@ static int emcore_mail_move_by_filter_rule(char *multi_user_name, int account_id
                return err;
        }
 
-    if (filter_info->rule_id > 0 && !emstorage_update_tag_id(multi_user_name, filter_info->rule_id, 0, &err)) 
+    if (filter_info->rule_id > 0 && !emstorage_update_tag_id(multi_user_name, filter_info->rule_id, 0, &err))
             EM_DEBUG_EXCEPTION("emstorage_update_tag_id failed : [%d]", err);
 
        if (!emstorage_filter_mails_by_rule(multi_user_name, account_id, mailbox_id, mailbox_type, false, filter_info, &filter_mail_id_list, &filter_mail_id_count, &err)) {
@@ -7154,13 +7404,502 @@ INTERNAL_FUNC void emcore_free_attachment_info(struct attachment_info *attchment
        EM_DEBUG_FUNC_END();
 }
 
+static char *get_charset_in_search_filter_string(email_search_filter_t *search_filter, int search_filter_count)
+{
+       EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
+
+       int i = 0;
+       char *charset = NULL;
+
+       if (search_filter == NULL || search_filter_count <= 0) {
+               EM_DEBUG_EXCEPTION("Invalid paramter");
+               return NULL;
+       }
+
+       for (i = 0; i < search_filter_count; i++) {
+               EM_DEBUG_LOG("current : [%d]", i);
+               EM_DEBUG_LOG("search_filter_type : [%d]", search_filter[i].search_filter_type);
+
+               switch (search_filter[i].search_filter_type) {
+               case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
+                       EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       charset = g_strdup(search_filter[i].search_filter_key_value.string_type_key_value);
+                       break;
+
+               default:
+                       break;
+               }
+       }
+
+       return charset;
+}
+
+static int create_searchpgm_from_filter_string(email_search_filter_t *search_filter,
+                                                                                               int start_index,
+                                                                                               int end_index,
+                                                                                               int *current_index,
+                                                                                               SEARCHPGM **output_searchpgm)
+{
+       EM_DEBUG_FUNC_BEGIN("search_filter:[%p], start_index[%d], end_index[%d]",
+                                               search_filter, start_index, end_index);
+
+       int err = EMAIL_ERROR_NONE;
+
+       if (search_filter == NULL || output_searchpgm == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid paramter");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       EM_DEBUG_LOG("start index : [%d]", start_index);
+
+       int i = 0;
+       int p_current_index = 0;
+       char *p = NULL;
+       char *temp_string = NULL;
+
+       SEARCHPGM *search_program = NULL;
+       SEARCHOR *temp_or_program = NULL;
+
+       search_program = mail_newsearchpgm();
+       if (search_program == NULL) {
+               EM_DEBUG_EXCEPTION("mail_newsearchpgm failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       for (i = start_index; i < end_index; i++) {
+               p_current_index = i;
+               EM_DEBUG_LOG("search_filter_type : [%d]", search_filter[i].search_filter_type);
+
+               switch (search_filter[i].search_filter_type) {
+               case EMAIL_SEARCH_FILTER_TYPE_ALL:
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value == 0) {
+                               temp_or_program = mail_newsearchor();
+                               if (temp_or_program == NULL) {
+                                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
+                               }
+
+                               p_current_index++;
+                               err = create_searchpgm_from_filter_string(search_filter,
+                                                                                                                       p_current_index,
+                                                                                                                       p_current_index + 1,
+                                                                                                                       &p_current_index,
+                                                                                                                       &temp_or_program->first);
+                               if (err != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("create_searchpgm_from_filter_string failed : [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               p_current_index++;
+                               err = create_searchpgm_from_filter_string(search_filter,
+                                                                                                                       p_current_index,
+                                                                                                                       p_current_index + 1,
+                                                                                                                       &p_current_index,
+                                                                                                                       &temp_or_program->second);
+                               if (err != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("create_searchpgm_from_filter_string failed : [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               i = p_current_index;
+                       }
+
+                       if (search_program->or) {
+                               SEARCHOR *temp_or = search_program->or;
+                               while (temp_or->next)
+                                       temp_or = temp_or->next;
+
+                               temp_or->next = temp_or_program;
+                       } else {
+                               search_program->or = temp_or_program;
+                       }
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->answered = 1;
+                       else
+                               search_program->unanswered = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_BCC :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("bcc %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
+                       EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
+
+                       err = emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                                               &temp_string);
+                       if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_make_date_string_for_search failed : [%d]", err);
+                               goto FINISH_OFF;
+                       }
+
+                       p = temp_string;
+                       if (!mail_criteria_date(&(search_program->before), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_date failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_BODY :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       p = search_filter[i].search_filter_key_value.string_type_key_value;
+                       if (!mail_criteria_string(&(search_program->body), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_CC :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("cc %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->deleted = 1;
+                       else
+                               search_program->undeleted = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->flagged = 1;
+                       else
+                               search_program->unflagged = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FROM :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("from %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       p = search_filter[i].search_filter_key_value.string_type_key_value;
+                       if (!mail_criteria_string(&(search_program->keyword), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW:
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value) {
+                               search_program->unseen = 1;
+                               search_program->recent = 1;
+                       }
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD:
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->old = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
+                       EM_DEBUG_LOG("time_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.time_type_key_value);
+
+                       err = emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                                               &temp_string);
+                       if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_make_date_string_for_search failed : [%d]", err);
+                               goto FINISH_OFF;
+                       }
+
+                       p = temp_string;
+                       if (!mail_criteria_date(&(search_program->on), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_date failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->recent = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->seen = 1;
+                       else
+                               search_program->unseen = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
+                       EM_DEBUG_LOG("time_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.time_type_key_value);
+
+                       err = emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                                               &temp_string);
+                       if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_make_date_string_for_search failed : [%d]", err);
+                               goto FINISH_OFF;
+                       }
+
+                       p = temp_string;
+                       if (!mail_criteria_date(&(search_program->since), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_date failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
+                       EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("subject %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_TEXT :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       p = search_filter[i].search_filter_key_value.string_type_key_value;
+                       if (!mail_criteria_string(&(search_program->text), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_TO :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("to %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       temp_string = g_strdup_printf("x-priority %d",
+                                                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       search_program->larger = search_filter[i].search_filter_key_value.integer_type_key_value;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       search_program->smaller = search_filter[i].search_filter_key_value.integer_type_key_value;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+
+                       if (search_filter[i].search_filter_key_value.integer_type_key_value)
+                               search_program->draft = 1;
+                       else
+                               search_program->undraft = 1;
+
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME :
+                   EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+#ifdef __FEATURE_GMIME_SEARCH_EXTENTION__
+                       p = search_filter[i].search_filter_key_value.string_type_key_value;
+                       if (!mail_criteria_string(&(search_program->attachment_name), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+#endif /* __FEATURE_GMIME_SEARCH_EXTENTION__ */
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
+                       EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED:
+                       EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
+               case EMAIL_SEARCH_FILTER_TYPE_UID :
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
+                       break;
+
+               case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
+                       EM_DEBUG_LOG("string_type_key_value [%s]",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+
+                       temp_string = g_strdup_printf("message-id %s",
+                                                       search_filter[i].search_filter_key_value.string_type_key_value);
+                       p = temp_string;
+                       if (!mail_criteria_header_string(&(search_program->header), &p)) {
+                               EM_DEBUG_EXCEPTION("mail_criteria_header_string failed");
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(temp_string);
+                       break;
+
+               default :
+                       EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
+                       err = EMAIL_ERROR_INVALID_PARAM;
+                       break;
+               }
+       }
+
+       if (current_index) {
+               EM_DEBUG_LOG("swap current_index[%d], p_current_index[%d]", *current_index, p_current_index);
+               *current_index = p_current_index;
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(temp_string);
+
+       if (err != EMAIL_ERROR_NONE) {
+               if (search_program)
+                       mail_free_searchpgm(&search_program);
+       } else {
+               *output_searchpgm = search_program;
+       }
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
 static int get_search_filter_string(email_search_filter_t *search_filter, int search_filter_count, char **output_filter_string)
 {
        EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
 
        int i = 0, j = 0;
        int err = EMAIL_ERROR_NONE;
-       char time_string[20];
+       char *time_string = NULL;
        char filter_string[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
        char temp_criteria[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
 
@@ -7176,7 +7915,8 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                switch (search_filter[i].search_filter_type) {
                case EMAIL_SEARCH_FILTER_TYPE_ALL:
-                       EM_DEBUG_LOG("integer_type_key_value [%d]", search_filter[i].search_filter_key_value.integer_type_key_value);
+                       EM_DEBUG_LOG("integer_type_key_value [%d]",
+                                                       search_filter[i].search_filter_key_value.integer_type_key_value);
                        if (search_filter[i].search_filter_key_value.integer_type_key_value == 0) {
                                for (j = i + 1; j < search_filter_count; j++) {
                                        if (search_filter[j + 1].search_filter_type == EMAIL_SEARCH_FILTER_TYPE_ALL &&
@@ -7201,28 +7941,30 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                case EMAIL_SEARCH_FILTER_TYPE_BCC :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BCC \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BCC \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
                        EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
-                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value, time_string);
+                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                               &time_string);
                        SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BEFORE %s ", time_string);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
+                       EM_SAFE_FREE(time_string);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_BODY :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BODY \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BODY \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_CC :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "CC \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "CC \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
@@ -7249,14 +7991,14 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                case EMAIL_SEARCH_FILTER_TYPE_FROM :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FROM \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FROM \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "KEYWORD \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "KEYWORD \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
 
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
@@ -7280,9 +8022,11 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
                        EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
-                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value, time_string);
+                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                               &time_string);
                        SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "ON %s ", time_string);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
+                       EM_SAFE_FREE(time_string);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
@@ -7304,28 +8048,30 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
                        EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
-                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value, time_string);
+                       emcore_make_date_string_for_search(search_filter[i].search_filter_key_value.time_type_key_value,
+                                                                                               &time_string);
                        SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SINCE %s ", time_string);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
+                       EM_SAFE_FREE(time_string);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
                        EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SUBJECT \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SUBJECT \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_TEXT :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TEXT \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TEXT \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_TO :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TO \"%s\" ", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TO \"%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
@@ -7350,14 +8096,14 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME :
                    EM_DEBUG_LOG_SEC("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "X-GM-RAW \"has:attachment filename:%s\"", 
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "X-GM-RAW \"has:attachment filename:%s\" ",
                                                search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
                case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
                        EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "charset %s", search_filter[i].search_filter_key_value.string_type_key_value);
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "charset %s ", search_filter[i].search_filter_key_value.string_type_key_value);
                        strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
                        break;
 
@@ -7397,12 +8143,12 @@ FINISH_OFF:
        return err;
 }
 
-int emcore_search_mail_and_uids(MAILSTREAM *stream, email_search_filter_t *input_search_filter, 
-                                                               int input_search_filter_count, emcore_uid_list** output_uid_list, 
+int emcore_search_mail_and_uids(MAILSTREAM *stream, email_search_filter_t *input_search_filter,
+                                                               int input_search_filter_count, emcore_uid_list** output_uid_list,
                                                                int *output_uid_count)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p] input_search_filter[%p] input_search_filter_count[%d] "
-                                               "output_uid_list[%p] output_uid_count[%p]", 
+                                               "output_uid_list[%p] output_uid_count[%p]",
                                                stream, input_search_filter_count, output_uid_list, output_uid_count);
 
        int err = EMAIL_ERROR_NONE;
@@ -7433,8 +8179,8 @@ int emcore_search_mail_and_uids(MAILSTREAM *stream, email_search_filter_t *input
        memset(tag, 0x00, sizeof(tag));
        memset(command, 0x00, sizeof(command));
 
-       if ((err = get_search_filter_string(input_search_filter, 
-                                                                               input_search_filter_count, 
+       if ((err = get_search_filter_string(input_search_filter,
+                                                                               input_search_filter_count,
                                                                                &search_filter_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("get_search_filter_string failed [%d]", err);
                goto FINISH_OFF;
@@ -7517,8 +8263,8 @@ int emcore_search_mail_and_uids(MAILSTREAM *stream, email_search_filter_t *input
 
        EM_DEBUG_LOG("uid_count [%d] ", uid_count);
        if (uid_count > 0) {
-               if ((err = emcore_make_uid_range_string(uid_list_for_listing, 
-                                                                                               uid_count, 
+               if ((err = emcore_make_uid_range_string(uid_list_for_listing,
+                                                                                               uid_count,
                                                                                                &uid_range_string)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_make_uid_range_string failed [%d]", err);
                        goto FINISH_OFF;
@@ -7549,17 +8295,248 @@ FINISH_OFF:
        return err;
 }
 
+INTERNAL_FUNC int emcore_search_on_server_ex(char *multi_user_name,
+                                                                                               int account_id,
+                                                                                               int mailbox_id,
+                                                                                               email_search_filter_t *input_search_filter,
+                                                                                               int input_search_filter_count,
+                                                                                               int cancellable,
+                                                                                               int event_handle)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d]", account_id, mailbox_id);
+       int err = EMAIL_ERROR_NONE;
+
+       if (account_id <= 0 || mailbox_id <= 0 || input_search_filter_count <= 0 || input_search_filter == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       int uid_count = 0;
+       int mail_id = 0;
+       int thread_id = 0;
+       long message_count = 0, i = 0;
+       char *charset = NULL;
+       char *uid_range = NULL;
+       char *mailbox_id_string = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       emcore_uid_list *uid_elem = NULL;
+       emcore_uid_list *uid_list = NULL;
+       emstorage_mailbox_tbl_t *search_mailbox = NULL;
+       emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
+
+       ENVELOPE *env = NULL;
+       MAILSTREAM *stream = NULL;
+       SEARCHPGM *search_program = NULL;
+       MESSAGECACHE *search_message_cache = NULL;
+
+       /* Get charset */
+       charset = get_charset_in_search_filter_string(input_search_filter, input_search_filter_count);
+
+       /* Get the search program from search_filter */
+       err = create_searchpgm_from_filter_string(input_search_filter,
+                                                                                               0,
+                                                                                               input_search_filter_count,
+                                                                                               NULL,
+                                                                                               &search_program);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("create_searchpgm_from_filter_string failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
+       /* Create new stream.
+          This stream is certainly freed. */
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mailbox_id,
+                                                                                       false,
+                                                                                       (void **)&stream,
+                                                                                       &err)) {
+               EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("nmsgs : [%ld]", stream->nmsgs);
+       message_count = stream->nmsgs;
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
+       if (!mail_search_full(stream, charset, search_program, 0)) {
+               EM_DEBUG_EXCEPTION("mail_search_full failed");
+               goto FINISH_OFF;
+       }
+
+       for (i = 1 ; i <= message_count; i++) {
+               search_message_cache = mail_elt(stream, i);
+               if (!search_message_cache->searched)
+                       continue;
+
+               uid_elem = em_malloc(sizeof(emcore_uid_list));
+               if (uid_elem == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               uid_count++;
+               uid_elem->uid = g_strdup_printf("%lu", mail_uid(stream, i));
+               uid_elem->msgno = i;
+
+               if (uid_list != NULL)
+                       uid_elem->next = uid_list;
+
+               uid_list = uid_elem;
+       }
+
+       uid_elem = NULL;
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
+       EM_DEBUG_LOG("uid_count : [%d]", uid_count);
+       if (uid_count > 0) {
+               err = emcore_make_uid_range_string(uid_list, uid_count, &uid_range);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_make_uid_range_string failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG("Calling ... mail_fetch_fast. uid_range [%s]", uid_range);
+               mail_fetch_fast(stream, uid_range, FT_UID | FT_PEEK | FT_NEEDENV);
+               EM_SAFE_FREE(uid_range);
+       }
+
+       /* Create the mailbox information for adding */
+       search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
+       if (search_mailbox == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       search_mailbox->account_id = account_id;
+       search_mailbox->mailbox_id = mailbox_id;
+       search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
+       search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
+
+       mailbox_id_string = g_strdup_printf("%d", mailbox_id);
+       if (mailbox_id_string == NULL) {
+               EM_DEBUG_EXCEPTION("g_strdup_printf failed : [%d][%s]", errno, EM_STRERROR(errno_buf));
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       /* Add the mail */
+       uid_elem = uid_list;
+       while (uid_elem) {
+               search_message_cache = NULL;
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
+#ifdef __FEATURE_HEADER_OPTIMIZATION__
+               env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 0);
+#else
+               env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK);
+#endif
+               search_message_cache = mail_elt(stream, uid_elem->msgno);
+               if (search_message_cache) {
+                       uid_elem->flag.seen = search_message_cache->seen;
+                       uid_elem->flag.draft = search_message_cache->draft;
+                       uid_elem->flag.flagged = search_message_cache->flagged;
+               }
+
+               if (!emcore_make_mail_tbl_data_from_envelope(multi_user_name,
+                                                                                                       account_id,
+                                                                                                       stream,
+                                                                                                       env,
+                                                                                                       uid_elem,
+                                                                                                       &new_mail_tbl_data,
+                                                                                                       &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+
+               if ((err = emcore_add_mail_to_mailbox(multi_user_name,
+                                                                                               search_mailbox,
+                                                                                               new_mail_tbl_data,
+                                                                                               &mail_id,
+                                                                                               &thread_id)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emcore_initiate_pbd(multi_user_name,
+                                                                       stream,
+                                                                       account_id,
+                                                                       mail_id,
+                                                                       uid_elem->uid,
+                                                                       search_mailbox->mailbox_id,
+                                                                       search_mailbox->mailbox_name,
+                                                                       &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_initiate_pbd failed : [%d]", err);
+               }
+
+               if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_string, thread_id)) {
+                       EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
+               }
+
+               if (new_mail_tbl_data) {
+                       emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
+                       new_mail_tbl_data = NULL;
+               }
+
+               uid_elem = uid_elem->next;
+       }
+
+FINISH_OFF:
+
+       if (stream)
+               stream = mail_close(stream);
 
-INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id, int mailbox_id, 
-                                                                               email_search_filter_t *input_search_filter, 
-                                                                               int input_search_filter_count, 
+       if (search_program)
+               mail_free_searchpgm(&search_program);
+
+       if (new_mail_tbl_data) {
+               emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
+               new_mail_tbl_data = NULL;
+       }
+
+       if (uid_list)
+               emcore_free_uids(uid_list, NULL);
+
+       if (search_mailbox)
+               emstorage_free_mailbox(&search_mailbox, 1, NULL);
+
+       EM_SAFE_FREE(charset);
+       EM_SAFE_FREE(uid_range);
+       EM_SAFE_FREE(mailbox_id_string);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+/*
+       It is fast. because this api search directly on server.
+       but do not search encoded words.
+ */
+INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id, int mailbox_id,
+                                                                               email_search_filter_t *input_search_filter,
+                                                                               int input_search_filter_count,
                                                                                int cancellable,
                                                                                int event_handle)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id : [%d], mailbox_id : [%d]", account_id, mailbox_id);
 
        int err = EMAIL_ERROR_NONE;
-       int i = 0;
        int mail_id = 0;
        int thread_id = 0;
        int uid_count = 0;
@@ -7582,18 +8559,25 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
        if (cancellable)
                FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mailbox_id, (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       mailbox_id,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
                goto FINISH_OFF;
        }
 
+       EM_DEBUG_LOG("nmsgs : [%ld]", stream->nmsgs);
+
        if (cancellable)
                FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-       if ((err = emcore_search_mail_and_uids(stream, 
-                                                                                       input_search_filter, 
-                                                                                       input_search_filter_count, 
-                                                                                       &uid_list, 
+       if ((err = emcore_search_mail_and_uids(stream,
+                                                                                       input_search_filter,
+                                                                                       input_search_filter_count,
+                                                                                       &uid_list,
                                                                                        &uid_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_search_mail_and_uids failed [%d]", err);
                goto FINISH_OFF;
@@ -7617,6 +8601,21 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                EM_DEBUG_LOG("stream->nmsgs : [%d]", stream->nmsgs);
        }
 
+       search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
+       if (search_mailbox == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       search_mailbox->account_id = account_id;
+       search_mailbox->mailbox_id = mailbox_id;
+       search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
+       search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
+
+       memset(mailbox_id_param_string, 0, 10);
+       SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
+
        while (uid_elem) {
                if (cancellable)
                        FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
@@ -7631,41 +8630,26 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                if (env)
                        EM_DEBUG_LOG("message_id[%s]", env->message_id);
 
-               if (!emcore_make_mail_tbl_data_from_envelope(multi_user_name, 
-                                                                                                       account_id, 
-                                                                                                       stream, 
-                                                                                                       env, 
-                                                                                                       uid_elem, 
-                                                                                                       &new_mail_tbl_data, 
+               if (!emcore_make_mail_tbl_data_from_envelope(multi_user_name,
+                                                                                                       account_id,
+                                                                                                       stream,
+                                                                                                       env,
+                                                                                                       uid_elem,
+                                                                                                       &new_mail_tbl_data,
                                                                                                        &err) || !new_mail_tbl_data) {
                        EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
-               if (search_mailbox == NULL) {
-                       EM_DEBUG_EXCEPTION("em_malloc failed");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-
-               search_mailbox->account_id = account_id;
-               search_mailbox->mailbox_id = mailbox_id;
-               search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
-               search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
-
-               if ((err = emcore_add_mail_to_mailbox(multi_user_name, 
-                                                                                               search_mailbox, 
-                                                                                               new_mail_tbl_data, 
-                                                                                               &mail_id, 
+               if ((err = emcore_add_mail_to_mailbox(multi_user_name,
+                                                                                               search_mailbox,
+                                                                                               new_mail_tbl_data,
+                                                                                               &mail_id,
                                                                                                &thread_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               memset(mailbox_id_param_string, 0, 10);
-               SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
-
                if (!emcore_initiate_pbd(multi_user_name,
                                                                        stream,
                                                                        account_id,
@@ -7687,7 +8671,6 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                }
 
                uid_elem = uid_elem->next;
-               i++;
        }
 
 FINISH_OFF:
index 23c831e..736fe57 100755 (executable)
@@ -563,10 +563,10 @@ int emcore_get_uids_order_by_datetime_from_imap_server(MAILSTREAM *stream, int c
        char delims[] = " ";
        char *result = NULL;
        int  uid_count = 0;
-       time_t         RawTime = 0;
-       char           since_date_string[20];
-       char           before_date_string[20];
-       char          *uid_range_string = NULL;
+       time_t RawTime = 0;
+       char before_date_string[20] = {0};
+       char *since_date_string = NULL;
+       char *uid_range_string = NULL;
        emcore_uid_list *uid_list_for_listing = NULL;
 
        if (!stream || !output_uid_list) {
@@ -587,7 +587,11 @@ int emcore_get_uids_order_by_datetime_from_imap_server(MAILSTREAM *stream, int c
        while (uid_count <= count_to_download) {
                RawTime = RawTime - (604800 * 4); //4 Weeks Before
 
-               emcore_make_date_string_for_search(RawTime, since_date_string);
+               err = emcore_make_date_string_for_search(RawTime, &since_date_string);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_make_date_string_for_search failed");
+                       goto FINISH_OFF;
+               }
 
                memset(tag, 0x00, sizeof(tag));
                memset(command, 0x00, sizeof(command));
@@ -599,6 +603,8 @@ int emcore_get_uids_order_by_datetime_from_imap_server(MAILSTREAM *stream, int c
                        SNPRINTF(command, sizeof(command), "%s UID SEARCH 1:* SINCE %s\015\012", tag, since_date_string);
                EM_DEBUG_LOG("COMMAND [%s] ", command);
 
+               EM_SAFE_STRCPY(before_date_string, since_date_string);
+               EM_SAFE_FREE(since_date_string);
 #ifdef FEATURE_CORE_DEBUG
                EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
 #endif
@@ -670,7 +676,6 @@ int emcore_get_uids_order_by_datetime_from_imap_server(MAILSTREAM *stream, int c
                        }
                }
 
-               EM_SAFE_STRCPY(before_date_string, since_date_string);
                EM_DEBUG_LOG("uid_count [%d] ", uid_count);
        }
 
@@ -1092,8 +1097,7 @@ static GDateTime *emcore_convert_strftime(char *time_str)
        char *ar_month[13] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
 
        token_list = g_strsplit_set(time_str, "-: ", -1);
-
-       if(token_list == NULL) {
+       if (token_list == NULL) {
                EM_DEBUG_LOG("g_strsplit_set failed.");
                return NULL;
        }
@@ -1165,18 +1169,18 @@ static gint emcore_compare_uid_elem(gconstpointer a, gconstpointer b)
        return ret;
 }
 
-static int emcore_get_uids_to_download(char *multi_user_name, 
-                                                                               MAILSTREAM *stream, 
-                                                                               email_account_t *account, 
-                                                                               emstorage_mailbox_tbl_t *input_mailbox_tbl, 
-                                                                               int limit_count, 
-                                                                               emcore_uid_list** uid_list, 
-                                                                               int *uids, 
-                                                                               int retrieve_mode, 
+static int emcore_get_uids_to_download(char *multi_user_name,
+                                                                               MAILSTREAM *stream,
+                                                                               email_account_t *account,
+                                                                               emstorage_mailbox_tbl_t *input_mailbox_tbl,
+                                                                               int limit_count,
+                                                                               emcore_uid_list** uid_list,
+                                                                               int *uids,
+                                                                               int retrieve_mode,
                                                                                int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreGetUidsDownload);
-       EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_tbl[%p], limit_count[%d], uid_list[%p], err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_tbl[%p], limit_count[%d], uid_list[%p], err_code[%p]",
                                                        account, input_mailbox_tbl, limit_count, uid_list, err_code);
 
        int ret = false;
@@ -1211,8 +1215,8 @@ static int emcore_get_uids_to_download(char *multi_user_name,
        else {  /*  EMAIL_SERVER_TYPE_IMAP4 */
                EM_DEBUG_LOG("nmsgs[%d]", ((MAILSTREAM *)stream)->nmsgs);
                if ((limit_count < ((MAILSTREAM *)stream)->nmsgs)) {
-                       if ((err = emcore_get_uids_order_by_datetime_from_imap_server(stream, 
-                                                                                                                                                       limit_count, 
+                       if ((err = emcore_get_uids_order_by_datetime_from_imap_server(stream,
+                                                                                                                                                       limit_count,
                                                                                                                                                        uid_list)) != EMAIL_ERROR_NONE ) {
                                EM_DEBUG_EXCEPTION("emcore_get_uids_order_by_datetime_from_imap_server failed [%d]", err);
                                if (err  != EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
@@ -1228,13 +1232,18 @@ static int emcore_get_uids_to_download(char *multi_user_name,
                }
        }
 
-       if (!emstorage_get_downloaded_list(multi_user_name, 
+       /* After syncing, if there is moving request, inbox syncing do */
+       /* So checking all downloaded mail */
+       if (!emstorage_get_downloaded_list(multi_user_name,
                                                                                input_mailbox_tbl->account_id,
-                                                                               (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ? 
+                                                                               0,
+/*
+                                                                               (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ?
                                                                                0 : input_mailbox_tbl->mailbox_id,
-                                                                               &downloaded_uids, 
-                                                                               &downloaded_uid_count, 
-                                                                               true, 
+*/
+                                                                               &downloaded_uids,
+                                                                               &downloaded_uid_count,
+                                                                               true,
                                                                                &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
                goto FINISH_OFF;
@@ -1306,58 +1315,62 @@ static int emcore_get_uids_to_download(char *multi_user_name,
                                                if (downloaded_uids[i - 1].mailbox_id != input_mailbox_tbl->mailbox_id) {
                                                        uid_count--;
                                                } else {
-                                                       /* The mail exists on server and local storage, 
-                                                          so it should not be downloaded, just check seen flag */
-                                                       if (downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen ||
-                                                                       downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
-                                                               if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen) {
-                                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
-                                                                       downloaded_uids[i - 1].flags_seen_field = uid_elem->flag.seen;
+                                                       if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
+                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_EXIST_ON_SERVER;
+                                                       } else if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
+                                                               /* The mail exists on server and local storage,
+                                                                  so it should not be downloaded, just check seen flag */
+                                                               if (downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen ||
+                                                                               downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
+                                                                       if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen) {
+                                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
+                                                                               downloaded_uids[i - 1].flags_seen_field = uid_elem->flag.seen;
+                                                                       }
+
+                                                                       if (downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
+                                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
+                                                                               downloaded_uids[i - 1].flags_flagged_field = uid_elem->flag.flagged;
+                                                                       }
+                                                               } else {
+                                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_EXIST_ON_SERVER;
                                                                }
 
-                                                               if (downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
-                                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
-                                                                       downloaded_uids[i - 1].flags_flagged_field = uid_elem->flag.flagged;
+                                                               if (!emstorage_get_mail_by_id(multi_user_name,
+                                                                                                                               downloaded_uids[i-1].local_uid,
+                                                                                                                               &mail,
+                                                                                                                               false,
+                                                                                                                               &err)) {
+                                                                       EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
+                                                                       continue;
                                                                }
-                                                       } else {
-                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_EXIST_ON_SERVER;
-                                                       }
 
-                                                       if (!emstorage_get_mail_by_id(multi_user_name, 
-                                                                                                                       downloaded_uids[i-1].local_uid, 
-                                                                                                                       &mail,
-                                                                                                                       false,
-                                                                                                                       &err)) {
-                                                               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
-                                                               continue;
-                                                       }
-
-                                                       if (mail->flags_answered_field != uid_elem->flag.answered) {
-                                                               if (!emcore_set_flags_field(multi_user_name,
-                                                                                                                       downloaded_uids[i-1].account_id,
-                                                                                                                       &(downloaded_uids[i-1].local_uid),
-                                                                                                                       1,
-                                                                                                                       EMAIL_FLAGS_ANSWERED_FIELD,
-                                                                                                                       uid_elem->flag.answered,
-                                                                                                                       &err)) {
-                                                                       EM_DEBUG_EXCEPTION("emcore_set_flags_field failed : [%d]", err);
+                                                               if (mail->flags_answered_field != uid_elem->flag.answered) {
+                                                                       if (!emcore_set_flags_field(multi_user_name,
+                                                                                                                               downloaded_uids[i-1].account_id,
+                                                                                                                               &(downloaded_uids[i-1].local_uid),
+                                                                                                                               1,
+                                                                                                                               EMAIL_FLAGS_ANSWERED_FIELD,
+                                                                                                                               uid_elem->flag.answered,
+                                                                                                                               &err)) {
+                                                                               EM_DEBUG_EXCEPTION("emcore_set_flags_field failed : [%d]", err);
+                                                                       }
                                                                }
-                                                       }
 
-                                                       if (mail->flags_forwarded_field != uid_elem->flag.forwarded) {
-                                                               if (!emcore_set_flags_field(multi_user_name,
-                                                                                                                       downloaded_uids[i-1].account_id,
-                                                                                                                       &(downloaded_uids[i-1].local_uid),
-                                                                                                                       1,
-                                                                                                                       EMAIL_FLAGS_FORWARDED_FIELD,
-                                                                                                                       uid_elem->flag.forwarded,
-                                                                                                                       &err)) {
-                                                                       EM_DEBUG_EXCEPTION("emcore_set_flags_field failed : [%d]", err);
+                                                               if (mail->flags_forwarded_field != uid_elem->flag.forwarded) {
+                                                                       if (!emcore_set_flags_field(multi_user_name,
+                                                                                                                               downloaded_uids[i-1].account_id,
+                                                                                                                               &(downloaded_uids[i-1].local_uid),
+                                                                                                                               1,
+                                                                                                                               EMAIL_FLAGS_FORWARDED_FIELD,
+                                                                                                                               uid_elem->flag.forwarded,
+                                                                                                                               &err)) {
+                                                                               EM_DEBUG_EXCEPTION("emcore_set_flags_field failed : [%d]", err);
+                                                                       }
                                                                }
-                                                       }
 
-                                                       emstorage_free_mail(&mail, 1, NULL);
-                                                       mail = NULL;
+                                                               emstorage_free_mail(&mail, 1, NULL);
+                                                               mail = NULL;
+                                                       }
                                                }
 
                                                to_be_downloaded = 0;
@@ -1370,8 +1383,8 @@ static int emcore_get_uids_to_download(char *multi_user_name,
                                if (retrieve_mode == EMAIL_SYNC_OLDEST_MAILS_FIRST) {
                                        uid_elem->next = head_uid_elem;
                                        head_uid_elem = uid_elem;
-                               } else {        
-                                       /* if retrieve_mode is EMAIL_SYNC_LATEST_MAILS_FIRST, 
+                               } else {
+                                       /* if retrieve_mode is EMAIL_SYNC_LATEST_MAILS_FIRST,
                                           add uid elem to end so that latest mails are in front of list */
                                        if (head_uid_elem == NULL) {
                                                uid_elem->next = head_uid_elem;
@@ -1400,65 +1413,53 @@ static int emcore_get_uids_to_download(char *multi_user_name,
                if ((downloaded_uids[i].sync_status == EMAIL_SYNC_STATUS_NOT_EXIST_ON_SERVER) &&
                        (EM_SAFE_STRCMP(downloaded_uids[i].mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) != 0)) {
                        /*  deleted on server */
-                       if (!emstorage_get_maildata_by_servermailid(multi_user_name, 
-                                                                                                               downloaded_uids[i].server_uid, 
+                       if (!emstorage_get_maildata_by_servermailid(multi_user_name,
+                                                                                                               downloaded_uids[i].server_uid,
                                                                                                                input_mailbox_tbl->mailbox_id,
-                                                                                                               &mail, 
-                                                                                                               true, 
+                                                                                                               &mail,
+                                                                                                               true,
                                                                                                                &err)){
-                               EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid for uid[%s] Failed [%d] \n ", 
+                               EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid for uid[%s] Failed [%d] \n ",
                                                                        downloaded_uids[i].server_uid, err);
                                if (err == EMAIL_ERROR_MAIL_NOT_FOUND){
                                        continue;
                                }
                        }
 
-                       if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-                               ret = emcore_delete_mails_from_local_storage(multi_user_name, 
-                                                                                                                       input_mailbox_tbl->account_id, 
-                                                                                                                       &(mail->mail_id), 
-                                                                                                                       1, 
-                                                                                                                       EMAIL_DELETED_FROM_SERVER, 
-                                                                                                                       false, 
-                                                                                                                       &err);
-                               /* Update badge count */
-                               emcore_display_unread_in_badge(multi_user_name);
-                               if (!ret) {
-                                       EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage falied - %d", err);
-                                       goto FINISH_OFF;
-                               }
-                       }
-
-                       if (!emstorage_remove_downloaded_mail(multi_user_name, 
-                                                                                               input_mailbox_tbl->account_id, 
-                                                                                               input_mailbox_tbl->mailbox_name, 
-                                                                                               downloaded_uids[i].server_uid, 
-                                                                                               true, 
-                                                                                               &err)) {   /*  remove uid from uid list */
-                               EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
-                               /* goto FINISH_OFF; */
+                       ret = emcore_delete_mails_from_local_storage(multi_user_name,
+                                                                                                               input_mailbox_tbl->account_id,
+                                                                                                               &(mail->mail_id),
+                                                                                                               1,
+                                                                                                               EMAIL_DELETED_FROM_SERVER,
+                                                                                                               false,
+                                                                                                               &err);
+                       if (!ret) {
+                               EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage falied - %d", err);
+                               goto FINISH_OFF;
                        }
 
+                       /* Update badge count */
+                       emcore_display_unread_in_badge(multi_user_name);
                }
                else if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
                        /* set seen flag of downloaded mail */
                        if (downloaded_uids[i].sync_status == EMAIL_SYNC_STATUS_FLAG_CHANGED) {
-                               if (!emcore_set_flags_field(multi_user_name, 
-                                                                                       downloaded_uids[i].account_id, 
-                                                                                       &(downloaded_uids[i].local_uid), 
-                                                                                       1, 
-                                                                                       EMAIL_FLAGS_FLAGGED_FIELD, 
-                                                                                       downloaded_uids[i].flags_flagged_field, 
+                               if (!emcore_set_flags_field(multi_user_name,
+                                                                                       downloaded_uids[i].account_id,
+                                                                                       &(downloaded_uids[i].local_uid),
+                                                                                       1,
+                                                                                       EMAIL_FLAGS_FLAGGED_FIELD,
+                                                                                       downloaded_uids[i].flags_flagged_field,
                                                                                        &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                }
 
-                               if (!emcore_set_flags_field(multi_user_name, 
-                                                                                       downloaded_uids[i].account_id, 
-                                                                                       &(downloaded_uids[i].local_uid), 
-                                                                                       1, 
-                                                                                       EMAIL_FLAGS_SEEN_FIELD, 
-                                                                                       downloaded_uids[i].flags_seen_field, 
+                               if (!emcore_set_flags_field(multi_user_name,
+                                                                                       downloaded_uids[i].account_id,
+                                                                                       &(downloaded_uids[i].local_uid),
+                                                                                       1,
+                                                                                       EMAIL_FLAGS_SEEN_FIELD,
+                                                                                       downloaded_uids[i].flags_seen_field,
                                                                                        &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                }
@@ -1499,13 +1500,13 @@ FINISH_OFF:
 }
 
 /* insert received mail UID to read mail uid table */
-static int emcore_add_read_mail_uid(char *multi_user_name, 
-                                                                       emstorage_mailbox_tbl_t *input_maibox_data, 
-                                                                       char *server_mailbox_name, 
-                                                                       int mail_id, 
-                                                                       char *uid, 
-                                                                       int rfc822_size, 
-                                                                       int rule_id, 
+static int emcore_add_read_mail_uid(char *multi_user_name,
+                                                                       emstorage_mailbox_tbl_t *input_maibox_data,
+                                                                       char *server_mailbox_name,
+                                                                       int mail_id,
+                                                                       char *uid,
+                                                                       int rfc822_size,
+                                                                       int rule_id,
                                                                        int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("input_maibox_data[%p], server_mailbox_name[%s], uid[%s], rfc822_size[%d], rule_id[%d], err_code[%p]", input_maibox_data, server_mailbox_name, uid, rfc822_size, rule_id, err_code);
@@ -1524,11 +1525,11 @@ static int emcore_add_read_mail_uid(char *multi_user_name,
 
        read_mail_uid.account_id = input_maibox_data->account_id;
        if (!(input_maibox_data->mailbox_id) || !(server_mailbox_name)) {
-               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, 
-                                                                                                       input_maibox_data->account_id, 
-                                                                                                       EMAIL_MAILBOX_TYPE_INBOX, 
-                                                                                                       &mailbox_tbl, 
-                                                                                                       false, 
+               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                       input_maibox_data->account_id,
+                                                                                                       EMAIL_MAILBOX_TYPE_INBOX,
+                                                                                                       &mailbox_tbl,
+                                                                                                       false,
                                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                        goto FINISH_OFF;
@@ -1613,20 +1614,21 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name, 
-                                                                                               emstorage_mailbox_tbl_t *input_maibox_data, 
-                                                                                               emstorage_mail_tbl_t *input_new_mail_tbl_data, 
-                                                                                               int *output_mail_id, 
+INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
+                                                                                               emstorage_mailbox_tbl_t *input_maibox_data,
+                                                                                               emstorage_mail_tbl_t *input_new_mail_tbl_data,
+                                                                                               int *output_mail_id,
                                                                                                int *output_thread_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], input_new_mail_tbl_data[%p], "
-                                               "uid_elem[%p], output_mail_id[%p], output_thread_id[%p]", mail_stream, input_maibox_data, 
+                                               "uid_elem[%p], output_mail_id[%p], output_thread_id[%p]", mail_stream, input_maibox_data,
                                                input_new_mail_tbl_data, output_mail_id, output_thread_id);
 
-       int                   err = EMAIL_ERROR_NONE;
-       int                   thread_id = -1;
-       int                   thread_item_count = 0;
-       int                   latest_mail_id_in_thread = -1;
+       int err = EMAIL_ERROR_NONE;
+       int thread_id = -1;
+       int before_tr_begin = 0;
+       int thread_item_count = 0;
+       int latest_mail_id_in_thread = -1;
 
        if (!input_maibox_data || !input_new_mail_tbl_data) {
                EM_DEBUG_EXCEPTION("Invalid Parameter");
@@ -1638,7 +1640,11 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
        input_new_mail_tbl_data->mailbox_id            = input_maibox_data->mailbox_id;
        input_new_mail_tbl_data->mailbox_type          = input_maibox_data->mailbox_type;
 
-       emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL);
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
+               before_tr_begin = 1;
+               goto FINISH_OFF;
+       }
 
        /* Get the Mail_id */
        if (!emstorage_increase_mail_id(multi_user_name, &(input_new_mail_tbl_data->mail_id), false, &err)) {
@@ -1647,10 +1653,10 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       err = emstorage_get_thread_id_of_thread_mails(multi_user_name, 
-                                                                                                       input_new_mail_tbl_data, 
-                                                                                                       &thread_id, 
-                                                                                                       &latest_mail_id_in_thread, 
+       err = emstorage_get_thread_id_of_thread_mails(multi_user_name,
+                                                                                                       input_new_mail_tbl_data,
+                                                                                                       &thread_id,
+                                                                                                       &latest_mail_id_in_thread,
                                                                                                        &thread_item_count);
        if (err != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
@@ -1673,32 +1679,32 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
        }
 
        if (thread_item_count > 1) {
-               if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                       input_new_mail_tbl_data->account_id, 
-                                                                                                       input_new_mail_tbl_data->mailbox_id, 
+               if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                       input_new_mail_tbl_data->account_id,
+                                                                                                       input_new_mail_tbl_data->mailbox_id,
                                                                                                        input_new_mail_tbl_data->mailbox_type,
                                                                                                        input_new_mail_tbl_data->thread_id,
-                                                                                                       NULL, 
-                                                                                                       -1, 
-                                                                                                       -1, 
-                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD, 
-                                                                                                       false, 
+                                                                                                       NULL,
+                                                                                                       -1,
+                                                                                                       -1,
+                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD,
+                                                                                                       false,
                                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                       input_new_mail_tbl_data->account_id, 
+               if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                       input_new_mail_tbl_data->account_id,
                                                                                                        input_new_mail_tbl_data->mailbox_id,
                                                                                                        input_new_mail_tbl_data->mailbox_type,
-                                                                                                       input_new_mail_tbl_data->thread_id, 
-                                                                                                       NULL, 
-                                                                                                       0, 
-                                                                                                       0, 
-                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD, 
-                                                                                                       false, 
+                                                                                                       input_new_mail_tbl_data->thread_id,
+                                                                                                       NULL,
+                                                                                                       0,
+                                                                                                       0,
+                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD,
+                                                                                                       false,
                                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
@@ -1706,10 +1712,10 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
                }
        }
 
-       if (emstorage_get_thread_id_of_thread_mails(multi_user_name, 
-                                                                                               input_new_mail_tbl_data, 
-                                                                                               &thread_id, 
-                                                                                               &latest_mail_id_in_thread, 
+       if (emstorage_get_thread_id_of_thread_mails(multi_user_name,
+                                                                                               input_new_mail_tbl_data,
+                                                                                               &thread_id,
+                                                                                               &latest_mail_id_in_thread,
                                                                                                &thread_item_count) != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
 
@@ -1719,13 +1725,13 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
        if (output_mail_id != NULL)
                *output_mail_id = input_new_mail_tbl_data->mail_id;
 
-       if (!emcore_add_read_mail_uid(multi_user_name, 
-                                                                       input_maibox_data, 
-                                                                       input_maibox_data->mailbox_name, 
-                                                                       input_new_mail_tbl_data->mail_id, 
-                                                                       input_new_mail_tbl_data->server_mail_id, 
-                                                                       input_new_mail_tbl_data->mail_size, 
-                                                                       0, 
+       if (!emcore_add_read_mail_uid(multi_user_name,
+                                                                       input_maibox_data,
+                                                                       input_maibox_data->mailbox_name,
+                                                                       input_new_mail_tbl_data->mail_id,
+                                                                       input_new_mail_tbl_data->server_mail_id,
+                                                                       input_new_mail_tbl_data->mail_size,
+                                                                       0,
                                                                        &err)) {
                EM_DEBUG_EXCEPTION("emcore_add_read_mail_uid failed [%d]", err);
                emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
@@ -1740,10 +1746,16 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name,
        }
 #endif
 
-       emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL);
-
 FINISH_OFF:
 
+       if (err != EMAIL_ERROR_NONE) {
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
+                       err = EMAIL_ERROR_DB_FAILURE;
+       } else {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false)
+                       err = EMAIL_ERROR_DB_FAILURE;
+       }
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
@@ -1877,10 +1889,17 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(char *multi_user_name, int account_id, MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code)
+INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(char *multi_user_name,
+                                                                                                                       int account_id,
+                                                                                                                       MAILSTREAM *mail_stream,
+                                                                                                                       ENVELOPE *input_envelope,
+                                                                                                                       emcore_uid_list *input_uid_elem,
+                                                                                                                       emstorage_mail_tbl_t **output_mail_tbl_data,
+                                                                                                                       int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreParseEnvelope);
-       EM_DEBUG_FUNC_BEGIN("input_envelope[%p], input_uid_elem [%p], output_mail_tbl_data[%p],  err_code[%p]", input_envelope, input_uid_elem, output_mail_tbl_data, err_code);
+       EM_DEBUG_FUNC_BEGIN("input_envelope[%p], input_uid_elem [%p], output_mail_tbl_data[%p], err_code[%p]",
+                                               input_envelope, input_uid_elem, output_mail_tbl_data, err_code);
 
        int zone_hour = 0;
        int ret = false;
@@ -2094,11 +2113,11 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_sync_header (char *multi_user_name, 
-                                       emstorage_mailbox_tbl_t *input_mailbox_tbl, 
-                                       void **stream, 
-                                       emcore_uid_list **input_uid_list, 
-                                       int *mail_count, 
+INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
+                                       emstorage_mailbox_tbl_t *input_mailbox_tbl,
+                                       void **stream,
+                                       emcore_uid_list **input_uid_list,
+                                       int *mail_count,
                                        int *unread_mail,
                                        int *vip_mail_count,
                                        int *vip_unread_mail,
@@ -2107,7 +2126,7 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                                        int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreSyncHeader);
-       EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], input_uid_list [%p], err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], input_uid_list [%p], err_code[%p]",
                                                        input_mailbox_tbl, input_uid_list, err_code);
 
        if (!stream) {
@@ -2183,10 +2202,11 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
        }
 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
-                                                                                       account_id, 
-                                                                                       input_mailbox_tbl->mailbox_id, 
-                                                                                       (void **)stream, 
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       input_mailbox_tbl->mailbox_id,
+                                                                                       true,
+                                                                                       (void **)stream,
                                                                                        &err) || !*stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
                goto FINISH_OFF;
@@ -2216,11 +2236,11 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
        }
 
        /*  save total mail count on server to DB */
-       if (!emstorage_update_mailbox_total_count(multi_user_name, 
-                                                                                               account_id, 
-                                                                                               input_mailbox_tbl->mailbox_id, 
-                                                                                               ((MAILSTREAM *)*stream)->nmsgs, 
-                                                                                               1, 
+       if (!emstorage_update_mailbox_total_count(multi_user_name,
+                                                                                               account_id,
+                                                                                               input_mailbox_tbl->mailbox_id,
+                                                                                               ((MAILSTREAM *)*stream)->nmsgs,
+                                                                                               1,
                                                                                                &err)){
                EM_DEBUG_EXCEPTION("emstorage_update_mailbox_total_count failed [%d]", err);
                goto FINISH_OFF;
@@ -2238,14 +2258,14 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                else if (!opt_ref->block_subject)
                        type = EMAIL_FILTER_FROM;
 
-               if (!emstorage_get_rule(multi_user_name, 
-                                                               ALL_ACCOUNT, 
-                                                               type, 
-                                                               0, 
-                                                               &rule_count, 
-                                                               &is_completed, 
-                                                               &rule, 
-                                                               true, 
+               if (!emstorage_get_rule(multi_user_name,
+                                                               ALL_ACCOUNT,
+                                                               type,
+                                                               0,
+                                                               &rule_count,
+                                                               &is_completed,
+                                                               &rule,
+                                                               true,
                                                                &err) || !rule) {
                        if ( err != EMAIL_ERROR_FILTER_NOT_FOUND )
                                EM_DEBUG_EXCEPTION ("emstorage_get_rule error [%d]", err);
@@ -2253,14 +2273,14 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
        }
 
        download_limit_count = input_mailbox_tbl->mail_slot_size;
-       if (!emcore_get_uids_to_download(multi_user_name, 
-                                                                               *stream, 
-                                                                               account_ref, 
-                                                                               input_mailbox_tbl, 
-                                                                               download_limit_count, 
-                                                                               &uid_list, 
-                                                                               &total, 
-                                                                               EMAIL_SYNC_LATEST_MAILS_FIRST, 
+       if (!emcore_get_uids_to_download(multi_user_name,
+                                                                               *stream,
+                                                                               account_ref,
+                                                                               input_mailbox_tbl,
+                                                                               download_limit_count,
+                                                                               &uid_list,
+                                                                               &total,
+                                                                               EMAIL_SYNC_LATEST_MAILS_FIRST,
                                                                                &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_uids_to_download failed [%d]", err);
                goto FINISH_OFF;
@@ -2345,31 +2365,31 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                        int blocked = false;
                        int priority_sender = false;
 
-                       if (!emcore_make_mail_tbl_data_from_envelope (multi_user_name, account_id, *stream, env, uid_elem, 
+                       if (!emcore_make_mail_tbl_data_from_envelope (multi_user_name, account_id, *stream, env, uid_elem,
                                                                                                                         &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
                                EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
                                goto CONTINUE_NEXT;
                        }
 
                        /* Check message_id for duplicated mail in sentbox */
-                       if ((input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_SENTBOX) && 
-                                       (emstorage_check_and_update_server_uid_by_message_id(multi_user_name, 
-                                                                                                                                                new_mail_tbl_data->account_id, 
-                                                                                                                                                input_mailbox_tbl->mailbox_type, 
-                                                                                                                                                new_mail_tbl_data->message_id, 
-                                                                                                                                                new_mail_tbl_data->server_mail_id, 
+                       if ((input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_SENTBOX) &&
+                                       (emstorage_check_and_update_server_uid_by_message_id(multi_user_name,
+                                                                                                                                                new_mail_tbl_data->account_id,
+                                                                                                                                                input_mailbox_tbl->mailbox_type,
+                                                                                                                                                new_mail_tbl_data->message_id,
+                                                                                                                                                new_mail_tbl_data->server_mail_id,
                                                                                                                                                 &searched_mail_id) == EMAIL_ERROR_NONE)) {
                                EM_DEBUG_LOG("Existed the duplicated mail : message_id[%s]", new_mail_tbl_data->message_id);
 
-                               if (!emcore_add_read_mail_uid(multi_user_name, input_mailbox_tbl, 
-                                                                                       input_mailbox_tbl->mailbox_name, searched_mail_id, 
-                                                                                       new_mail_tbl_data->server_mail_id, new_mail_tbl_data->mail_size, 
+                               if (!emcore_add_read_mail_uid(multi_user_name, input_mailbox_tbl,
+                                                                                       input_mailbox_tbl->mailbox_name, searched_mail_id,
+                                                                                       new_mail_tbl_data->server_mail_id, new_mail_tbl_data->mail_size,
                                                                                        0, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_add_read_mail_uid failed : [%d]", err);
                                        goto CONTINUE_NEXT;
                                }
-                       
-                               if (!emstorage_change_mail_field(multi_user_name, searched_mail_id, UPDATE_DATETIME, 
+
+                               if (!emstorage_change_mail_field(multi_user_name, searched_mail_id, UPDATE_DATETIME,
                                                                                                new_mail_tbl_data, true, &err)) {
                                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed : [%d]", err);
                                        goto CONTINUE_NEXT;
@@ -2378,12 +2398,12 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                                goto CONTINUE_NEXT;
                        }
 
-                       if (rule && !emcore_check_rule(new_mail_tbl_data->full_address_from, 
-                                                                                       new_mail_tbl_data->subject, 
-                                                                                       rule, 
-                                                                                       rule_count, 
-                                                                                       &priority_sender, 
-                                                                                       &blocked, 
+                       if (rule && !emcore_check_rule(new_mail_tbl_data->full_address_from,
+                                                                                       new_mail_tbl_data->subject,
+                                                                                       rule,
+                                                                                       rule_count,
+                                                                                       &priority_sender,
+                                                                                       &blocked,
                                                                                        &err)) {
                                EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
                        }
@@ -2399,11 +2419,11 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                        if (blocked && (input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)) {
                                if (input_mailbox_tbl->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX) {
                                        EM_DEBUG_LOG("This mail would be added to spambox");
-                                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, 
-                                                                                                                       account_id, 
-                                                                                                                       EMAIL_MAILBOX_TYPE_SPAMBOX, 
-                                                                                                                       &destination_mailbox, 
-                                                                                                                       false, 
+                                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                                       account_id,
+                                                                                                                       EMAIL_MAILBOX_TYPE_SPAMBOX,
+                                                                                                                       &destination_mailbox,
+                                                                                                                       false,
                                                                                                                        &err)) {
                                                EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                        }
@@ -2416,20 +2436,20 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                        new_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_NOTI_WAITED;
 
                        if (destination_mailbox) {
-                               if (destination_mailbox->local_yn == 0 || 
+                               if (destination_mailbox->local_yn == 0 ||
                                        account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-                                       err = emcore_move_mail_on_server_by_server_mail_id((void*)*stream, 
-                                                                                                                                                       new_mail_tbl_data->server_mail_id, 
+                                       err = emcore_move_mail_on_server_by_server_mail_id((void*)*stream,
+                                                                                                                                                       new_mail_tbl_data->server_mail_id,
                                                                                                                                                        destination_mailbox->mailbox_name);
                                        if (err != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_by_server_mail_id falied [%d]", err);
                                                goto CONTINUE_NEXT;
                                        }
                                } else { /* local mailbox */
-                                       if ((err = emcore_add_mail_to_mailbox(multi_user_name, 
-                                                                                                               destination_mailbox, 
-                                                                                                               new_mail_tbl_data, 
-                                                                                                               &mail_id, 
+                                       if ((err = emcore_add_mail_to_mailbox(multi_user_name,
+                                                                                                               destination_mailbox,
+                                                                                                               new_mail_tbl_data,
+                                                                                                               &mail_id,
                                                                                                                &thread_id)) != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
                                                goto CONTINUE_NEXT;
@@ -2440,10 +2460,10 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                        } else {
                                /*  add mails to specified mail box */
                                EM_DEBUG_LOG_SEC("mail[%d] moved to input_mailbox_tbl [%s]", mail_id, input_mailbox_tbl->mailbox_name);
-                               if ((err = emcore_add_mail_to_mailbox(multi_user_name, 
-                                                                                                       input_mailbox_tbl, 
-                                                                                                       new_mail_tbl_data, 
-                                                                                                       &mail_id, 
+                               if ((err = emcore_add_mail_to_mailbox(multi_user_name,
+                                                                                                       input_mailbox_tbl,
+                                                                                                       new_mail_tbl_data,
+                                                                                                       &mail_id,
                                                                                                        &thread_id)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
                                        goto CONTINUE_NEXT;
@@ -2453,10 +2473,10 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                                if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
 #endif /*  __FEATURE_PARTIAL_BODY_FOR_POP3__ */
                                        if (account_ref->auto_download_size != 0) {
-                                               if (false == emcore_initiate_pbd(multi_user_name, 
-                                                                                                               *stream, 
-                                                                                                               account_id, 
-                                                                                                               mail_id, 
+                                               if (false == emcore_initiate_pbd(multi_user_name,
+                                                                                                               *stream,
+                                                                                                               account_id,
+                                                                                                               mail_id,
                                                                                                                uid_elem->uid,
                                                                                                                input_mailbox_tbl->mailbox_id,
                                                                                                                input_mailbox_tbl->mailbox_name,
@@ -2469,12 +2489,12 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
 
 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
                                if (input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) {
-                                       if (!emcore_insert_auto_download_job(multi_user_name, 
-                                                                                                               account_id, 
-                                                                                                               input_mailbox_tbl->mailbox_id, 
-                                                                                                               mail_id, 
-                                                                                                               /*account_ref->wifi_auto_download*/ 1, 
-                                                                                                               uid_elem->uid, 
+                                       if (!emcore_insert_auto_download_job(multi_user_name,
+                                                                                                               account_id,
+                                                                                                               input_mailbox_tbl->mailbox_id,
+                                                                                                               mail_id,
+                                                                                                               /*account_ref->wifi_auto_download*/ 1,
+                                                                                                               uid_elem->uid,
                                                                                                                &err))
                                                EM_DEBUG_LOG("emcore_insert_auto_download_job failed [%d]", err);
                                }
@@ -2508,7 +2528,7 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
 
                        blocking_mode = emcore_get_blocking_mode_status();
                        if (!blocking_mode) {
-                               if ((err = emcore_check_blocking_mode (multi_user_name, new_mail_tbl_data->email_address_sender, 
+                               if ((err = emcore_check_blocking_mode (multi_user_name, new_mail_tbl_data->email_address_sender,
                                                                                                                                                        &blocking_mode)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION ("emcore_check_blocking_mode failed : [%d]", err);
                                }
@@ -2665,7 +2685,12 @@ int emcore_download_uid_all(char *multi_user_name, MAILSTREAM *mail_stream, emai
        }
 
        if (!mail_stream) {
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)){
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               mailbox->account_id,
+                                                                                               mailbox->mailbox_id,
+                                                                                               true,
+                                                                                               (void **)&tmp_stream,
+                                                                                               &err)){
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed...");
 
                        goto FINISH_OFF;
@@ -2965,7 +2990,12 @@ int emcore_download_imap_msgno(char *multi_user_name, email_internal_mailbox_t *
        }
 
        if (!mailbox->mail_stream) {
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               mailbox->account_id,
+                                                                                               mailbox->mailbox_id,
+                                                                                               true,
+                                                                                               (void **)&tmp_stream,
+                                                                                               &err)) {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
 
                        goto FINISH_OFF;
@@ -3255,6 +3285,134 @@ INTERNAL_FUNC char *emcore_guess_charset(char *multi_user_name, char *source_str
 }
 
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
+INTERNAL_FUNC int emcore_sync_mail_by_message_id(char *multi_user_name,
+                                                                                                       int mail_id,
+                                                                                                       int mailbox_id,
+                                                                                                       char **output_server_uid)
+{
+       EM_DEBUG_FUNC_BEGIN("mail_id : [%d], mailbox_id : [%d]", mail_id, mailbox_id);
+
+       int err = EMAIL_ERROR_NONE;
+       char *tag = NULL;
+       char *command = NULL;
+       char *response = NULL;
+       char *server_uid = NULL;
+       emstorage_mail_tbl_t *mail_data_t = NULL;
+
+       MAILSTREAM *stream = NULL;
+       IMAPLOCAL *imaplocal = NULL;
+
+       if (mail_id <= 0 || output_server_uid == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_data_t, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       mail_data_t->account_id,
+                                                                                       mailbox_id,
+                                                                                       false,
+                                                                                       (void **)&stream,
+                                                                                       &err)){
+           EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err);
+           goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("nmsgs : [%lu]", stream->nmsgs);
+
+       if (!(imaplocal = stream->local) || !imaplocal->netstream) {
+               EM_DEBUG_EXCEPTION(" invalid IMAP4 stream detected...");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       tag = g_strdup_printf("%08lx", 0xffffffff & (stream->gensym++));
+       if (tag == NULL) {
+               EM_DEBUG_EXCEPTION("[tag] g_strdup_printf failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       command = g_strdup_printf("%s UID SEARCH header message-id %s\015\012", tag, mail_data_t->message_id);
+       if (command == NULL) {
+               EM_DEBUG_EXCEPTION("[COMMAND] g_strdup_printf failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
+
+       if (!net_sout(imaplocal->netstream, command, EM_SAFE_STRLEN(command))) {
+               EM_DEBUG_EXCEPTION("net_sout failed...");
+               err = EMAIL_ERROR_CONNECTION_BROKEN;
+               goto FINISH_OFF;
+       }
+
+       while (imaplocal->netstream) {
+               char *p = NULL;
+
+               /*  receive response */
+               response = net_getline(imaplocal->netstream);
+               if (response == NULL) {
+                       EM_DEBUG_EXCEPTION("net_getline failed...");
+                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
+
+               if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
+                       if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
+                               EM_SAFE_FREE (response);
+                               break;
+                       } else {        /*  'NO' or 'BAD' */
+                               err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;              /* EMAIL_ERROR_INVALID_RESPONSE; */
+                               goto FINISH_OFF;
+                       }
+               }
+
+               if ((p = strstr(response, " SEARCH "))){
+                       *p = '\0'; p  += strlen(" SEARCH ");
+
+                       server_uid = g_strdup(p);
+
+                       EM_SAFE_FREE(response);
+                       continue;
+               } else if ((p = strstr(response, " SEARCH"))){
+                       /* there is no mail */
+                       continue;
+               } else {
+                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                       goto FINISH_OFF;
+               }
+       }
+
+FINISH_OFF:
+
+       if (stream)
+               stream = mail_close(stream);
+
+       if (mail_data_t)
+               emstorage_free_mail(&mail_data_t, 1, NULL);
+
+       EM_SAFE_FREE(tag);
+       EM_SAFE_FREE(command);
+
+       if (err != EMAIL_ERROR_NONE) {
+               EM_SAFE_FREE(server_uid);
+       } else {
+               *output_server_uid = server_uid;
+       }
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
 /* callback for GET_APPENDUID - shasikala.p */
 void mail_appenduid(char *mailbox, unsigned long uidvalidity, SEARCHSET *set)
 {
@@ -3366,7 +3524,12 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(char *multi_user_name,
 
        rewind(fp);
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, mail_table_data->account_id, 0, (void **)&stream, &err)){
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       mail_table_data->account_id,
+                                                                                       0,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err)){
            EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err);
            goto FINISH_OFF;
        }
@@ -3475,14 +3638,25 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(char *multi_user_name,
        }
 
        /* Update read_mail_uid tbl */
-       if (!emcore_add_read_mail_uid(multi_user_name, mailbox_tbl, mailbox_tbl->mailbox_name, mail_table_data->mail_id, g_append_uid_rsp, mail_table_data->mail_size, 0, &err)) {
+       if (!emcore_add_read_mail_uid(multi_user_name,
+                                                                       mailbox_tbl,
+                                                                       mailbox_tbl->mailbox_name,
+                                                                       mail_table_data->mail_id,
+                                                                       g_append_uid_rsp,
+                                                                       mail_table_data->mail_size,
+                                                                       0,
+                                                                       &err)) {
                EM_DEBUG_EXCEPTION(" emcore_add_read_mail_uid failed [%d]", err);
                emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
        /* Update mail_data tbl */
-       if (!emstorage_update_server_uid(multi_user_name, mail_table_data->server_mail_id, g_append_uid_rsp, &err)) {
+       if (!emstorage_update_server_uid(multi_user_name,
+                                                                               mail_table_data->mail_id,
+                                                                               mail_table_data->server_mail_id,
+                                                                               g_append_uid_rsp,
+                                                                               &err)) {
                EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed : [%d]", err);
                emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
@@ -3546,8 +3720,8 @@ FINISH_OFF:
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int account_id, 
-                                                                               int mail_id, char *uid, int input_maibox_id, 
+INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int account_id,
+                                                                               int mail_id, char *uid, int input_maibox_id,
                                                                                char *input_mailbox_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_id[%d], uid[%p], input_maibox_id[%d], input_mailbox_name[%s]",
@@ -3556,6 +3730,7 @@ INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream,
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account_ref = NULL;
+       email_event_partial_body_thd pbd_event = {0, };
 
        if (account_id < FIRST_ACCOUNT_ID || mail_id < 0 || NULL == uid || 0 == input_maibox_id){
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -3576,7 +3751,6 @@ INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream,
                goto FINISH_OFF;
        }
 
-       email_event_partial_body_thd pbd_event;
        memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
 
        pbd_event.account_id = account_id;
@@ -3593,7 +3767,7 @@ INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream,
 
        EM_DEBUG_LOG("input_mailbox_name name [%d]", pbd_event.mailbox_id);
        EM_DEBUG_LOG("uid [%s]", uid);
-       EM_DEBUG_LOG("pbd_event.account_id[%d], pbd_event.mail_id[%d], pbd_event.server_mail_id [%d]", 
+       EM_DEBUG_LOG("pbd_event.account_id[%d], pbd_event.mail_id[%d], pbd_event.server_mail_id [%d]",
                                        pbd_event.account_id, pbd_event.mail_id , pbd_event.server_mail_id);
 
        if (!emcore_insert_pbd_activity(&pbd_event, &pbd_event.activity_id, &err)){
@@ -3641,16 +3815,16 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name, 
-                                                                                                               struct _m_content_info *cnt_info, 
-                                                                                                               int account_id, 
-                                                                                                               int mail_id, 
-                                                                                                               int mailbox_id, 
-                                                                                                               int *output_total_attachment_size, 
-                                                                                                               int *output_attachment_count, 
+INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name,
+                                                                                                               struct _m_content_info *cnt_info,
+                                                                                                               int account_id,
+                                                                                                               int mail_id,
+                                                                                                               int mailbox_id,
+                                                                                                               int *output_total_attachment_size,
+                                                                                                               int *output_attachment_count,
                                                                                                                int *output_inline_attachment_count)
 {
-       EM_DEBUG_FUNC_BEGIN("cnt_info : [%p], account_id : [%d], mail_id : [%d], mailbox_id : [%d]", 
+       EM_DEBUG_FUNC_BEGIN("cnt_info : [%p], account_id : [%d], mail_id : [%d], mailbox_id : [%d]",
                                                cnt_info, account_id, mail_id, mailbox_id);
        int err = EMAIL_ERROR_NONE;
 
@@ -3840,11 +4014,11 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
 
                /* Check the body download status and body size */
                SNPRINTF(uid_string, sizeof(uid_string), "%ld", imap_response[i].uid_no);
-               if (!emstorage_get_maildata_by_servermailid(pbd_event[temp_count].multi_user_name, 
-                                                                                                       uid_string, 
+               if (!emstorage_get_maildata_by_servermailid(pbd_event[temp_count].multi_user_name,
+                                                                                                       uid_string,
                                                                                                        pbd_event[temp_count].mailbox_id,
-                                                                                                       &mail, 
-                                                                                                       false, 
+                                                                                                       &mail,
+                                                                                                       false,
                                                                                                        &err) || !mail) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND || !mail)
@@ -4063,11 +4237,11 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                }
 
                if (!strcasecmp(body->subtype, "pkcs7-mime")) {
-                       if (emcore_get_attribute_value_of_body_part(body->parameter, 
-                                                                                                               "PROTOCOL", 
-                                                                                                               rfc822_protocol, 
-                                                                                                               TEMP_STRING_LENGTH, 
-                                                                                                               false, 
+                       if (emcore_get_attribute_value_of_body_part(body->parameter,
+                                                                                                               "PROTOCOL",
+                                                                                                               rfc822_protocol,
+                                                                                                               TEMP_STRING_LENGTH,
+                                                                                                               false,
                                                                                                                &err)) {
                                if (strcasestr(rfc822_protocol, "enveloped-data"))
                                        mail->smime_type = EMAIL_SMIME_ENCRYPTED;
@@ -4077,20 +4251,20 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                } else if (!strcasecmp(body->subtype, "encrypted")) {
                        mail->smime_type = EMAIL_PGP_ENCRYPTED;
                } else if (!strcasecmp(body->subtype, "signed")) {
-                       if (emcore_get_attribute_value_of_body_part(body->parameter, 
-                                                                                                               "MICALG", 
-                                                                                                               rfc822_micalg, 
-                                                                                                               TEMP_STRING_LENGTH, 
-                                                                                                               false, 
+                       if (emcore_get_attribute_value_of_body_part(body->parameter,
+                                                                                                               "MICALG",
+                                                                                                               rfc822_micalg,
+                                                                                                               TEMP_STRING_LENGTH,
+                                                                                                               false,
                                                                                                                &err)) {
                                mail->digest_type = emcore_get_digest_type(rfc822_micalg);
                        }
 
-                       if (emcore_get_attribute_value_of_body_part(body->parameter, 
-                                                                                                               "PROTOCOL", 
-                                                                                                               rfc822_protocol, 
-                                                                                                               TEMP_STRING_LENGTH, 
-                                                                                                               false, 
+                       if (emcore_get_attribute_value_of_body_part(body->parameter,
+                                                                                                               "PROTOCOL",
+                                                                                                               rfc822_protocol,
+                                                                                                               TEMP_STRING_LENGTH,
+                                                                                                               false,
                                                                                                                &err)) {
                                if (strcasestr(rfc822_protocol, "pkcs7-signature"))
                                        mail->smime_type = EMAIL_SMIME_SIGNED;
@@ -4112,13 +4286,13 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                }
 
                /* Update the attachment info except inline attachment */
-               if ((err = emcore_update_attachment_except_inline(pbd_event[temp_count].multi_user_name, 
-                                                        cnt_info, 
-                                                        pbd_event[temp_count].account_id, 
-                                                        mail->mail_id, 
-                                                        pbd_event[temp_count].mailbox_id, 
-                                                        &total_attachment_size, 
-                                                        &attachment_num, 
+               if ((err = emcore_update_attachment_except_inline(pbd_event[temp_count].multi_user_name,
+                                                        cnt_info,
+                                                        pbd_event[temp_count].account_id,
+                                                        mail->mail_id,
+                                                        pbd_event[temp_count].mailbox_id,
+                                                        &total_attachment_size,
+                                                        &attachment_num,
                                                         &inline_attachment_num)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
                        goto FINISH_OFF;
@@ -4149,20 +4323,20 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                                }
                        }
 
-                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name, 
-                                                                               pbd_event[temp_count].account_id, 
-                                                                               mail->mail_id, 
-                                                                               0, 
+                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name,
+                                                                               pbd_event[temp_count].account_id,
+                                                                               mail->mail_id,
+                                                                               0,
                                                                                &err))
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
-                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
-                                        pbd_event[temp_count].account_id, 
-                                        mail->mail_id, 
-                                        0, 
-                                        charset_plain_text, 
-                                        move_buf, 
-                                        path_buf, 
+                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name,
+                                        pbd_event[temp_count].account_id,
+                                        mail->mail_id,
+                                        0,
+                                        charset_plain_text,
+                                        move_buf,
+                                        path_buf,
                                         sizeof(path_buf),
                                         &err))
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
@@ -4194,21 +4368,21 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
 
                        charset_html_text = g_strconcat(charset_html_text, HTML_EXTENSION_STRING, NULL);
 
-                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name, 
-                                                                       pbd_event[temp_count].account_id, 
-                                                                       mail->mail_id, 
-                                                                       0, 
+                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name,
+                                                                       pbd_event[temp_count].account_id,
+                                                                       mail->mail_id,
+                                                                       0,
                                                                        &err))
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
-                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
-                                        pbd_event[temp_count].account_id, 
-                                        mail->mail_id, 
-                                        0, 
-                                        charset_html_text, 
-                                        move_buf, 
-                                        path_buf, 
-                                        sizeof(path_buf), 
+                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name,
+                                        pbd_event[temp_count].account_id,
+                                        mail->mail_id,
+                                        0,
+                                        charset_html_text,
+                                        move_buf,
+                                        path_buf,
+                                        sizeof(path_buf),
                                         &err))
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
 
@@ -4233,14 +4407,14 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                                                                        &err))
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
-                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
-                                        pbd_event[temp_count].account_id, 
-                                        mail->mail_id, 
-                                        0, 
-                                        "mime_entity", 
-                                        move_buf, 
-                                        path_buf, 
-                                        sizeof(path_buf), 
+                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name,
+                                        pbd_event[temp_count].account_id,
+                                        mail->mail_id,
+                                        0,
+                                        "mime_entity",
+                                        move_buf,
+                                        path_buf,
+                                        sizeof(path_buf),
                                         &err))
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
 
@@ -4265,21 +4439,21 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                                                memset(path_buf, 0x00, sizeof(path_buf));
                                                memset(move_buf, 0x00, sizeof(move_buf));
 
-                                               if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name, 
-                                                                                                       pbd_event[temp_count].account_id, 
-                                                                                                       mail->mail_id, 
-                                                                                                       0, 
+                                               if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name,
+                                                                                                       pbd_event[temp_count].account_id,
+                                                                                                       mail->mail_id,
+                                                                                                       0,
                                                                                                        &err))
                                                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
-                                               if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
-                                                    pbd_event[temp_count].account_id, 
-                                                    mail->mail_id, 
-                                                    0, 
-                                                    temp_file->name, 
-                                                    move_buf, 
-                                                    path_buf, 
-                                                    sizeof(path_buf), 
+                                               if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name,
+                                                    pbd_event[temp_count].account_id,
+                                                    mail->mail_id,
+                                                    0,
+                                                    temp_file->name,
+                                                    move_buf,
+                                                    path_buf,
+                                                    sizeof(path_buf),
                                                     &err))
                                                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
 
@@ -4302,10 +4476,10 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                                                        attachment_tbl.content_id                       = temp_file->content_id;
                                                        EM_DEBUG_LOG("mime_type : [%s]", temp_file->attachment_mime_type);
 
-                                                       if (!emstorage_add_attachment(pbd_event[temp_count].multi_user_name, 
-                                                                                                                       &attachment_tbl, 
-                                                                                                                       false, 
-                                                                                                                       false, 
+                                                       if (!emstorage_add_attachment(pbd_event[temp_count].multi_user_name,
+                                                                                                                       &attachment_tbl,
+                                                                                                                       false,
+                                                                                                                       false,
                                                                                                                        &err))
                                                                EM_DEBUG_EXCEPTION("emstorage_add_attachment failed - %d", err);
                                                }
@@ -4319,32 +4493,32 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                        }
                }
 
-               mail->body_download_status = (mail->body_download_status & ~0x00000003) | 
-                                                                       (((total_mail_size - total_attachment_size <= input_download_size) && 
+               mail->body_download_status = (mail->body_download_status & ~0x00000003) |
+                                                                       (((total_mail_size - total_attachment_size <= input_download_size) &&
                                                                   (inline_download_count == inline_attachment_num)) ?
-                                                                       EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED : 
+                                                                       EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED :
                                                                        EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED);
 
-               EM_DEBUG_LOG("inline_download_count[%d] inline_attachment_num[%d]", 
+               EM_DEBUG_LOG("inline_download_count[%d] inline_attachment_num[%d]",
                                                inline_download_count, inline_attachment_num);
-               EM_DEBUG_LOG("total_mail_size[%d] total_attachment_size[%d] input_download_size[%d]", 
+               EM_DEBUG_LOG("total_mail_size[%d] total_attachment_size[%d] input_download_size[%d]",
                                                total_mail_size, total_attachment_size, input_download_size);
                EM_DEBUG_LOG("mail_id [%d] body_download_status [%d]", mail->mail_id, mail->body_download_status);
 
                /* Get preview text */
-               if ( (err = emcore_get_preview_text_from_file(pbd_event[temp_count].multi_user_name, 
-                                                        mail->file_path_plain, 
-                                                        mail->file_path_html, 
-                                                        MAX_PREVIEW_TEXT_LENGTH, 
+               if ( (err = emcore_get_preview_text_from_file(pbd_event[temp_count].multi_user_name,
+                                                        mail->file_path_plain,
+                                                        mail->file_path_html,
+                                                        MAX_PREVIEW_TEXT_LENGTH,
                                                         &(mail->preview_text))) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file() failed[%d]", err);
 
                /* Update body contents */
-               if (!emstorage_change_mail_field(pbd_event[temp_count].multi_user_name, 
-                                                                                       mail->mail_id, 
-                                                                                       UPDATE_PARTIAL_BODY_DOWNLOAD, 
-                                                                                       mail, 
-                                                                                       true, 
+               if (!emstorage_change_mail_field(pbd_event[temp_count].multi_user_name,
+                                                                                       mail->mail_id,
+                                                                                       UPDATE_PARTIAL_BODY_DOWNLOAD,
+                                                                                       mail,
+                                                                                       true,
                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed - %d", err);
                        goto FINISH_OFF;
@@ -4353,17 +4527,17 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
 #ifdef __FEATURE_BODY_SEARCH__
                /* strip html content and save into mail_text_tbl */
                char *stripped_text = NULL;
-               if (!emcore_strip_mail_body_from_file(pbd_event[temp_count].multi_user_name, 
-                                                                                               mail, 
-                                                                                               &stripped_text, 
+               if (!emcore_strip_mail_body_from_file(pbd_event[temp_count].multi_user_name,
+                                                                                               mail,
+                                                                                               &stripped_text,
                                                                                                &err) || stripped_text == NULL) {
                        EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
                }
 
-               if (!emstorage_get_mail_text_by_id(pbd_event[temp_count].multi_user_name, 
-                                                                                       mail->mail_id, 
-                                                                                       &mail_text, 
-                                                                                       true, 
+               if (!emstorage_get_mail_text_by_id(pbd_event[temp_count].multi_user_name,
+                                                                                       mail->mail_id,
+                                                                                       &mail_text,
+                                                                                       true,
                                                                                        &err) || !mail_text) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_text_by_id failed [%d]", err);
                        goto FINISH_OFF;
@@ -4372,10 +4546,10 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                EM_SAFE_FREE(mail_text->body_text);
                mail_text->body_text = stripped_text;
 
-               if (!emstorage_change_mail_text_field(pbd_event[temp_count].multi_user_name, 
-                                                                                               mail->mail_id, 
-                                                                                               mail_text, 
-                                                                                               false, 
+               if (!emstorage_change_mail_text_field(pbd_event[temp_count].multi_user_name,
+                                                                                               mail->mail_id,
+                                                                                               mail_text,
+                                                                                               false,
                                                                                                &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_text_field failed [%d]", err);
                        goto FINISH_OFF;
@@ -4388,10 +4562,10 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                if (mail)
                        emstorage_free_mail(&mail, 1, NULL);
 
-               if (false == emcore_delete_pbd_activity(pbd_event[temp_count].multi_user_name, 
-                                                                                               pbd_event[temp_count].account_id, 
-                                                                                               pbd_event[temp_count].mail_id, 
-                                                                                               pbd_event[temp_count].activity_id, 
+               if (false == emcore_delete_pbd_activity(pbd_event[temp_count].multi_user_name,
+                                                                                               pbd_event[temp_count].account_id,
+                                                                                               pbd_event[temp_count].mail_id,
+                                                                                               pbd_event[temp_count].activity_id,
                                                                                                &err)){
                        EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
                        goto FINISH_OFF;
@@ -4631,7 +4805,7 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
                                        goto FINISH_OFF;
                                }
 
-                               if (net_getbuffer(nstream, server_response[count].rfc822header_len, 
+                               if (net_getbuffer(nstream, server_response[count].rfc822header_len,
                                                                server_response[count].rfc822header) <= 0) {
                                        EM_DEBUG_EXCEPTION("net_getbuffer failed");
                                        goto FINISH_OFF;
index 3c9b5ab..546f53a 100755 (executable)
@@ -257,7 +257,12 @@ INTERNAL_FUNC MAILSTREAM** emcore_get_recv_stream (char *multi_user_name, int ac
                goto FINISH_OFF;
        }
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mailbox_id, (void **)ret, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       account_id, 
+                                                                                       mailbox_id, 
+                                                                                       true,
+                                                                                       (void **)ret, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                EM_SAFE_FREE(ret);
                goto FINISH_OFF;
@@ -899,9 +904,10 @@ FINISH_OFF:
 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 
-INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *multi_user_name, 
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info(char *multi_user_name, 
                                                                                                                                                email_account_t *account, 
                                                                                                                                                int input_mailbox_id, 
+                                                                                                                                               int reusable,
                                                                        /*either MAILSTREAM or SENDSTREAM*/ void **result_stream, 
                                                                                                                                                int *err_code)
 {
@@ -938,7 +944,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
 #ifdef __FEATURE_KEEP_CONNECTION__
        email_connection_info_t *connection_info = emcore_get_connection_info_by_account_id(account->account_id);
 
-       if(connection_info) {
+       if (connection_info && reusable) {
                if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
                        if(connection_info->receiving_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
                                *result_stream = connection_info->receiving_server_stream;
@@ -1157,9 +1163,15 @@ emcore_close_mailbox uses mail_close inside it.
 
 mail_close is only used in emcore_connect_to_remote_mailbox and emcore_reset_streams as an exception to above rule*/
 
-INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, char *mailbox, void **mail_stream, int *err_code)
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, 
+                                                                                                       int account_id, 
+                                                                                                       char *mailbox, 
+                                                                                                       int reusable,
+                                                                                                       void **mail_stream, 
+                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], mail_stream[%p], err_code[%p]", account_id, mailbox, mail_stream, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], reusable[%d], mail_stream[%p], err_code[%p]", 
+                                               account_id, mailbox, reusable, mail_stream, err_code);
 
        int ret = false;
        int error = EMAIL_ERROR_NONE;
@@ -1172,7 +1184,12 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int ac
                goto FINISH_OFF;
        }
        
-       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, ref_account, mailbox, mail_stream, &error);
+       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, 
+                                                                                                                               ref_account, 
+                                                                                                                               mailbox, 
+                                                                                                                               reusable,
+                                                                                                                               mail_stream, 
+                                                                                                                               &error);
 
 FINISH_OFF:
 
@@ -1228,9 +1245,15 @@ INTERNAL_FUNC void emcore_reset_streams()
 
 #else /*  __FEATURE_KEEP_CONNECTION__ */
 
-INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, 
+                                                                                                       int account_id, 
+                                                                                                       int input_mailbox_id, 
+                                                                                                       int reusable,
+                                                                                                       void **mail_stream, 
+                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account_id, input_mailbox_id, mail_stream, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], reusable[%d], mail_stream[%p], err_code[%p]", 
+                                               account_id, input_mailbox_id, reusable, mail_stream, err_code);
 
        int ret = false;
        int error = EMAIL_ERROR_NONE;
@@ -1261,9 +1284,14 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int ac
                goto FINISH_OFF; */
        }
 
-       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, ref_account, input_mailbox_id, mail_stream, &error);
-
-       EM_DEBUG_LOG("ret[%d] incoming_server_type[%d] input_mailbox_id[%d]", ret, ref_account->incoming_server_type, input_mailbox_id);
+       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, 
+                                                                                                                               ref_account, 
+                                                                                                                               input_mailbox_id, 
+                                                                                                                               reusable,
+                                                                                                                               mail_stream, 
+                                                                                                                               &error);
+       EM_DEBUG_LOG("ret[%d] incoming_server_type[%d] input_mailbox_id[%d]", 
+                                       ret, ref_account->incoming_server_type, input_mailbox_id);
 
        if (ret == EMAIL_ERROR_NONE && input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
                SENDSTREAM *send_stream = (SENDSTREAM*)*mail_stream;
index 78cf724..2865855 100755 (executable)
@@ -441,7 +441,7 @@ static int emcore_write_rfc822_body(BODY *body, BODY *root_body, FILE *fp, int *
 
        PARAMETER *param = NULL;
        PART *part = NULL;
-       char *p = NULL, *bndry = NULL, buf[1025];
+       char *p = NULL, *bndry = NULL, buf[SENDBUFLEN];
        int error = EMAIL_ERROR_NONE;
 
        switch (body->type)  {
@@ -947,8 +947,8 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
                                EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH);
                        */
 
-                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 
-                                                                               0, body_text_file_name, name_buf, path_buf, 
+                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
+                                                                               0, body_text_file_name, name_buf, path_buf,
                                                                                sizeof(path_buf), &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                goto FINISH_OFF;
@@ -988,8 +988,8 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
                                EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH);
                        */
 
-                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 
-                                                                               0, body_text_file_name, name_buf, path_buf, 
+                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
+                                                                               0, body_text_file_name, name_buf, path_buf,
                                                                                sizeof(path_buf), &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                goto FINISH_OFF;
@@ -1016,8 +1016,8 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
 
                EM_DEBUG_LOG_SEC("mail_data->file_path_mime_entity [%s]", mail_data->file_path_mime_entity);
 
-               if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 
-                                                                       0, "mime_entity", name_buf, path_buf, 
+               if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id,
+                                                                       0, "mime_entity", name_buf, path_buf,
                                                                        sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
@@ -1065,10 +1065,10 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
 
        EM_DEBUG_LOG("preview_text[%p]", mail_data->preview_text);
        if (mail_data->preview_text == NULL) {
-               if ( (err = emcore_get_preview_text_from_file(multi_user_name, 
-                                                    mail_data->file_path_plain, 
-                                                    mail_data->file_path_html, 
-                                                    MAX_PREVIEW_TEXT_LENGTH, 
+               if ( (err = emcore_get_preview_text_from_file(multi_user_name,
+                                                    mail_data->file_path_plain,
+                                                    mail_data->file_path_html,
+                                                    MAX_PREVIEW_TEXT_LENGTH,
                                                     &(mail_data->preview_text))) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
 
@@ -1107,7 +1107,11 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
 
        mail_data->thread_id = converted_mail_tbl->thread_id;
        converted_mail_tbl->user_name = EM_SAFE_STRDUP(account_tbl_item->user_name);
-       emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL);
+
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL)) {
+               EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]");
+               goto FINISH_OFF;
+       }
 
        /*  insert mail to mail table */
        if (!emstorage_add_mail(multi_user_name, converted_mail_tbl, 0, false, &err))  {
@@ -1121,16 +1125,16 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
        EM_DEBUG_LOG("thread_item_count [%d]", thread_item_count);
 
        if (thread_item_count > 1) {
-               if (!emstorage_update_latest_thread_mail(multi_user_name, 
-                                                                                                       mail_data->account_id, 
+               if (!emstorage_update_latest_thread_mail(multi_user_name,
+                                                                                                       mail_data->account_id,
                                                                                                        mail_data->mailbox_id,
                                                                                                        mail_data->mailbox_type,
-                                                                                                       converted_mail_tbl->thread_id, 
-                                                                                                       &updated_thread_id, 
-                                                                                                       0, 
-                                                                                                       0, 
-                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD, 
-                                                                                                       false, 
+                                                                                                       converted_mail_tbl->thread_id,
+                                                                                                       &updated_thread_id,
+                                                                                                       0,
+                                                                                                       0,
+                                                                                                       NOTI_THREAD_ID_CHANGED_BY_ADD,
+                                                                                                       false,
                                                                                                        &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
@@ -1170,13 +1174,13 @@ INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *inpu
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_save_name(multi_user_name, 
-                                        mail_data->account_id, 
-                                        mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, 
-                                        attachment_data_list[i].attachment_name, 
-                                        name_buf, 
+               if (!emstorage_get_save_name(multi_user_name,
+                                        mail_data->account_id,
+                                        mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id,
+                                        attachment_data_list[i].attachment_name,
+                                        name_buf,
                                         path_buf,
-                                        sizeof(path_buf), 
+                                        sizeof(path_buf),
                                         &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
@@ -1498,13 +1502,23 @@ INTERNAL_FUNC int emcore_query_mail_size_limit(char *multi_user_name, int accoun
        }
 
        if (ref_account->pop_before_smtp != FALSE) {
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err)) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               0,
+                                                                                               true,
+                                                                                               (void **)&mail_stream,
+                                                                                               &err)) {
                        EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       EMAIL_CONNECT_FOR_SENDING,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1623,7 +1637,12 @@ INTERNAL_FUNC SENDSTREAM** emcore_get_smtp_stream (char *multi_user_name, int ac
                goto FINISH_OFF;
        }
 
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)ret, &err))  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       EMAIL_CONNECT_FOR_SENDING,
+                                                                                       true,
+                                                                                       (void **)ret,
+                                                                                       &err))  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
 
                EM_SAFE_FREE(ret);
@@ -1679,9 +1698,11 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
        email_option_t *opt = NULL;
        char *fpath = NULL;
        emstorage_mailbox_tbl_t* local_mailbox = NULL;
+       emstorage_read_mail_uid_tbl_t *downloaded_mail = NULL;
        int dst_mailbox_id = 0;
        int total_mail_size = 0;
        int sent_flag = 0;
+       char *server_uid = NULL;
        MAILSTREAM *mail_stream = NULL;
 
        if (!mail_id)  {
@@ -1772,7 +1793,12 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
        }
 
        if (ref_account->pop_before_smtp != FALSE)  {
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err))  {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               0,
+                                                                                               true,
+                                                                                               (void **)&mail_stream,
+                                                                                               &err))  {
                        EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
                        if (err == EMAIL_ERROR_CONNECTION_BROKEN)
                                err = EMAIL_ERROR_CANCELLED;
@@ -1801,7 +1827,12 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
        }
 
 #if 0
-       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err))  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                       account_id,
+                                                                                       EMAIL_CONNECT_FOR_SENDING,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err))  {
                EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
 
                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
@@ -1877,7 +1908,12 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
 
        /*  sent mail is moved to 'SENT' box or deleted. */
        if (opt->keep_local_copy)  {
-               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
+               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                       account_id,
+                                                                                                       EMAIL_MAILBOX_TYPE_SENTBOX,
+                                                                                                       &local_mailbox,
+                                                                                                       true,
+                                                                                                       &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -1933,14 +1969,19 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
                        emstorage_mailbox_tbl_t* src_mailbox = NULL;
 //                     emstorage_mail_tbl_t *temp_mail = NULL;
 
-                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &src_mailbox, true, &err))  {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                               account_id,
+                                                                                                               EMAIL_MAILBOX_TYPE_OUTBOX,
+                                                                                                               &src_mailbox,
+                                                                                                               true,
+                                                                                                               &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
                        EM_DEBUG_LOG("local_yn:[%d]", src_mailbox->local_yn);
-                       if (src_mailbox->local_yn) { 
-                               /* This is syncing operation in sent box 
+                       if (src_mailbox->local_yn) {
+                               /* This is syncing operation in sent box
                                   but it slowed operation */
                                /*
                                void *local_stream = NULL;
@@ -1948,11 +1989,11 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
                                        EM_DEBUG_EXCEPTION("emcore_sync_header failed");
                                }
                                mail_close (local_stream);
-                               if (!emstorage_get_maildata_by_servermailid(multi_user_name, 
-                                                                                                                       "0", 
+                               if (!emstorage_get_maildata_by_servermailid(multi_user_name,
+                                                                                                                       "0",
                                                                                                                        local_mailbox->mailbox_id,
-                                                                                                                       &temp_mail, 
-                                                                                                                       false, 
+                                                                                                                       &temp_mail,
+                                                                                                                       false,
                                                                                                                        &err)) {
                                        if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
                                                EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid failed : [%d]", err);
@@ -1962,18 +2003,65 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
 
                                if (temp_mail) {
                                        emcore_sync_mail_from_client_to_server(multi_user_name, mail_id);
-                                       emstorage_free_mail(&temp_mail, 1, NULL);       
+                                       emstorage_free_mail(&temp_mail, 1, NULL);
                                }
                                */
 
                                /* sent box exception list : gmail (After the mail sent, moved the sent box) */
                                if (ref_account->outgoing_server_address) {
-                                       if (!strcasestr(ref_account->outgoing_server_address, "gmail"))
+                                       if (!strcasestr(ref_account->outgoing_server_address, "gmail")) {
                                                emcore_sync_mail_from_client_to_server(multi_user_name, mail_id);
+                                       } else {
+                                               err = emcore_sync_mail_by_message_id(multi_user_name,
+                                                                                                                               mail_id,
+                                                                                                                               dst_mailbox_id,
+                                                                                                                               &server_uid);
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION("emcore_sync_mail_by_message_id failed : [%d]", err);
+                                               }
+
+                                               EM_DEBUG_LOG("server_uid : [%s]", server_uid);
+
+                                               if (server_uid) {
+                                                       if (!emstorage_update_server_uid(multi_user_name,
+                                                                                                                               mail_id,
+                                                                                                                               NULL,
+                                                                                                                               server_uid,
+                                                                                                                               &err)) {
+                                                               EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed : [%d]", err);
+                                                       }
+
+                                                       downloaded_mail = em_malloc(sizeof(emstorage_read_mail_uid_tbl_t));
+                                                       if (downloaded_mail == NULL) {
+                                                               EM_DEBUG_EXCEPTION("em_malloc failed");
+                                                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       downloaded_mail->account_id = account_id;
+                                                       downloaded_mail->mailbox_id = dst_mailbox_id;
+                                                       downloaded_mail->local_uid = mail_id;
+                                                       downloaded_mail->mailbox_name = g_strdup(local_mailbox->mailbox_name);
+                                                       downloaded_mail->server_uid = g_strdup(server_uid);
+                                                       downloaded_mail->rfc822_size = mail_tbl_data->mail_size;
+
+                                                       if (!emstorage_add_downloaded_mail(multi_user_name,
+                                                                                                                               downloaded_mail,
+                                                                                                                               true,
+                                                                                                                               &err)) {
+                                                               EM_DEBUG_EXCEPTION("emstorage_add_downloaded_mail failed : [%d]", err);
+                                                       }
+                                               }
+                                       }
                                }
-                       }
-                       else {
-                               if (!emcore_move_mail_on_server(multi_user_name, account_id, src_mailbox->mailbox_id, &mail_id, 1, local_mailbox->mailbox_name, &err)) {
+                       } else {
+                               if (!emcore_move_mail_on_server(multi_user_name,
+                                                                                               account_id,
+                                                                                               src_mailbox->mailbox_id,
+                                                                                               &mail_id,
+                                                                                               1,
+                                                                                               local_mailbox->mailbox_name,
+                                                                                               &err)) {
                                        EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
                                }
                        }
@@ -1984,11 +2072,25 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
                /* On Successful Mail sent remove the Draft flag */
                mail_tbl_data->flags_draft_field = 0;
 
-               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "flags_draft_field", mail_tbl_data->flags_draft_field, true, &err))
+               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name,
+                                                                                                                       account_id,
+                                                                                                                       &mail_id,
+                                                                                                                       1,
+                                                                                                                       "flags_draft_field",
+                                                                                                                       mail_tbl_data->flags_draft_field,
+                                                                                                                       true,
+                                                                                                                       &err))
                        EM_DEBUG_EXCEPTION("Failed to modify extra flag [%d]", err);
-       }
-       else  {
-               if (!emcore_delete_mail(multi_user_name, account_id, &mail_id, 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
+       } else {
+               if (!emcore_delete_mail(multi_user_name,
+                                                               account_id,
+                                                               0,
+                                                               &mail_id,
+                                                               1,
+                                                               EMAIL_DELETE_LOCALLY,
+                                                               EMAIL_DELETED_AFTER_SENDING,
+                                                               false,
+                                                               &err))
                        EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
        }
 
@@ -1999,7 +2101,14 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
 
        /*Update save_status */
        mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
-       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name,
+                                                                                                               account_id,
+                                                                                                               &mail_id,
+                                                                                                               1,
+                                                                                                               "save_status",
+                                                                                                               mail_tbl_data->save_status,
+                                                                                                               true,
+                                                                                                               &err))
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
 
        if (!emcore_delete_transaction_info_by_mailId(mail_id))
@@ -2008,6 +2117,9 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
        ret = true;
 
 FINISH_OFF:
+
+       EM_SAFE_FREE(server_uid);
+
        if (ret == false && sent_flag == 0) {
                emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, err);
        }
@@ -2023,7 +2135,14 @@ FINISH_OFF:
                                EM_DEBUG_LOG("EMAIL_MAIL_STATUS_SEND_CANCELED Already set for ");
                        else {
                                mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
-                               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
+                               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name,
+                                                                                                                                       account_id,
+                                                                                                                                       &mail_id,
+                                                                                                                                       1,
+                                                                                                                                       "save_status",
+                                                                                                                                       mail_tbl_data->save_status,
+                                                                                                                                       true,
+                                                                                                                                       &err2))
                                        EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
                        }
                }
@@ -2041,6 +2160,9 @@ FINISH_OFF:
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 #endif
 
+       if (downloaded_mail)
+               emstorage_free_read_mail_uid(&downloaded_mail, 1, NULL);
+
        if (mail_stream)
                mail_stream = mail_close (mail_stream);
 
@@ -2059,7 +2181,7 @@ FINISH_OFF:
                EM_SAFE_FREE (fpath);
        }
 
-       if(local_mailbox)
+       if (local_mailbox)
                emstorage_free_mailbox(&local_mailbox, 1, NULL);
 
        if (ret == true) {
@@ -2231,7 +2353,12 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
                        }
 
                        stream = NULL;
-                       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, &tmp_stream, &err) || !tmp_stream)  {
+                       if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                                       account_id,
+                                                                                                       EMAIL_CONNECT_FOR_SENDING,
+                                                                                                       true,
+                                                                                                       &tmp_stream,
+                                                                                                       &err) || !tmp_stream)  {
                                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
 
                                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
@@ -2278,7 +2405,12 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
                        if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err2))
                                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
 
-                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                               account_id,
+                                                                                                               EMAIL_MAILBOX_TYPE_OUTBOX,
+                                                                                                               &local_mailbox,
+                                                                                                               false,
+                                                                                                               &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -2295,27 +2427,50 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
                searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
 
                /*  update mail status to sent mail. */
-               if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
+               if (!emstorage_change_mail_field(multi_user_name,
+                                                                                       mail_ids[i],
+                                                                                       UPDATE_EXTRA_FLAG,
+                                                                                       searched_mail_tbl_data,
+                                                                                       true,
+                                                                                       &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                /*  sent mail is moved to 'SENT' box or deleted. */
-               if (opt->keep_local_copy)  {
-                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
+               if (opt->keep_local_copy) {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name,
+                                                                                                               account_id,
+                                                                                                               EMAIL_MAILBOX_TYPE_SENTBOX,
+                                                                                                               &local_mailbox,
+                                                                                                               false,
+                                                                                                               &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
-                       dst_mailbox_id = local_mailbox->mailbox_id;
 
-                       if (!emcore_move_mail(multi_user_name, &mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
+                       dst_mailbox_id = local_mailbox->mailbox_id;
+                       if (!emcore_move_mail(multi_user_name,
+                                                                       &mail_ids[i],
+                                                                       1,
+                                                                       dst_mailbox_id,
+                                                                       EMAIL_MOVED_AFTER_SENDING,
+                                                                       0,
+                                                                       &err))
                                EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
 
                        if(local_mailbox)
                                emstorage_free_mailbox(&local_mailbox, 1, NULL);
-               }
-               else  {
-                       if (!emcore_delete_mail(multi_user_name, account_id, &mail_ids[i], 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
+               } else {
+                       if (!emcore_delete_mail(multi_user_name,
+                                                                       account_id,
+                                                                       0,
+                                                                       &mail_ids[i],
+                                                                       1,
+                                                                       EMAIL_DELETE_LOCALLY,
+                                                                       EMAIL_DELETED_AFTER_SENDING,
+                                                                       false,
+                                                                       &err))
                                EM_DEBUG_EXCEPTION("emcore_delete_mail falied [%d]", err);
                }
 
@@ -2324,7 +2479,7 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
                        EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
                }
 
-               if(searched_mail_tbl_data) {
+               if (searched_mail_tbl_data) {
                        emstorage_free_mail(&searched_mail_tbl_data, 1, NULL);
                        searched_mail_tbl_data = NULL;
                }
@@ -2395,9 +2550,16 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
+static int emcore_send_mail_smtp(char *multi_user_name,
+                                                                       SENDSTREAM *stream,
+                                                                       ENVELOPE *env,
+                                                                       char *data_file,
+                                                                       int account_id,
+                                                                       int mail_id,
+                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("stream[%p], env[%p], data_file[%s], account_id[%d], mail_id[%d], err_code[%p]", stream, env, data_file, account_id, mail_id, err_code);
+       EM_DEBUG_FUNC_BEGIN_SEC("stream[%p], env[%p], data_file[%s], account_id[%d], mail_id[%d], err_code[%p]",
+                                                               stream, env, data_file, account_id, mail_id, err_code);
        EM_PROFILE_BEGIN(profile_emcore_send_mail_smtp);
 
        int ret = false;
@@ -2444,9 +2606,12 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
 
                if (stream->protocol.esmtp.dsn.ok && stream->protocol.esmtp.dsn.want) {
                        EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is required");
-                       strncat (buf, stream->protocol.esmtp.dsn.full ? " RET=FULL" : " RET=HDRS", sizeof(buf)-EM_SAFE_STRLEN(buf)-1);
+                       strncat(buf, stream->protocol.esmtp.dsn.full ? " RET=FULL" : " RET=HDRS",
+                                       sizeof(buf)-EM_SAFE_STRLEN(buf)-1);
+
                        if (stream->protocol.esmtp.dsn.envid)
-                               SNPRINTF (buf + EM_SAFE_STRLEN (buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)), " ENVID=%.100s", stream->protocol.esmtp.dsn.envid);
+                               SNPRINTF (buf + EM_SAFE_STRLEN (buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)),
+                                                       " ENVID=%.100s", stream->protocol.esmtp.dsn.envid);
                }
                else
                        EM_DEBUG_LOG("stream->protocol.esmtp.dsn.want is not required or DSN is not supported");
@@ -2565,9 +2730,7 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
                }
                EM_PROFILE_END(profile_open_file);
 
-
 #ifdef __FEATURE_SEND_OPTMIZATION__
-       {
                char *data = NULL;
                int read_size, allocSize, dataSize, gMaxAllocSize = 40960; /*  40KB */
                int total_fixed = 0;
@@ -2596,12 +2759,13 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
                while (total) {
                        /* Cancel the sending event */
                        if (!emcore_check_send_mail_thread_status()) {
+                               EM_SAFE_FREE(data);
                                EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
                                err = EMAIL_ERROR_CANCELLED;
                                goto FINISH_OFF;
                        }
 
-                       if (total  < allocSize)
+                       if (total < allocSize)
                                dataSize = total;
                        else
                                dataSize = allocSize;
@@ -2634,7 +2798,8 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
 
                                        emcore_update_notification_for_send(account_id, mail_id, progress);
                                }
-                               EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d]", sent, total);
+                               EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d] datasize[%d]",
+                                                               sent, total, dataSize);
                        }
                        total -= dataSize;
                }
@@ -2665,13 +2830,13 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
                        }
                }
 
-#endif
                if (!send_ret) {
                        EM_DEBUG_EXCEPTION("smtp_soutr failed - %ld", send_ret);
                        err = EMAIL_ERROR_SMTP_SEND_FAILURE;
                        goto FINISH_OFF;
                }
        }
+#endif
 
        send_ret = smtp_send(stream, ".", 0);
        EM_DEBUG_LOG("[SMTP] . --------> %s", stream->reply);
@@ -2684,6 +2849,7 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
        ret = true;
 
 FINISH_OFF:
+
        if (ret == false)
                smtp_send(stream, "RSET", 0);
 
@@ -2697,6 +2863,7 @@ FINISH_OFF:
 
        if (fp)
                fclose(fp);
+
        EM_PROFILE_END(profile_emcore_send_mail_smtp);
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
@@ -3695,7 +3862,7 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
                        protocol_param->value       = cpystr("application/pkcs7-signature");
                        protocol_param->next        = NULL;
                        param->next                 = protocol_param;
-               } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED || input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
+               } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_ENCRYPTED || input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
                        root_body->type    = TYPEAPPLICATION;
                        root_body->subtype = strdup("PKCS7-MIME");
 
@@ -3755,7 +3922,7 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
 
                        if (strlen(input_mail_tbl_data->file_path_plain) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
-                
+
                 memset(real_file_path, 0x00, sizeof(real_file_path));
                 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
 
@@ -3783,7 +3950,7 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
                                           (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)) {
                        if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
-                
+
                 memset(real_file_path, 0x00, sizeof(real_file_path));
                 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
 
@@ -3795,7 +3962,7 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
 
                        if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
-                               if (input_mail_tbl_data->inline_content_count > 0 && 
+                               if (input_mail_tbl_data->inline_content_count > 0 &&
                                                (root_body->subtype && (strcasecmp(root_body->subtype, "RELATED") != 0))) {
                                        part_for_related = attach_multipart_with_sub_type(root_body, "RELATED", &error);
                                        if (!part_for_related) {
@@ -3808,13 +3975,13 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
                 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
 
                                if (part_for_related) {
-                                       if (!attach_part(&(part_for_related->body), (unsigned char *)real_file_path, 0, 
+                                       if (!attach_part(&(part_for_related->body), (unsigned char *)real_file_path, 0,
                                                                                          NULL, "html", false, &error)) {
                                                EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                                goto FINISH_OFF;
                                        }
                                } else {
-                                       if (!attach_part (root_body, (unsigned char *)real_file_path, 0, 
+                                       if (!attach_part (root_body, (unsigned char *)real_file_path, 0,
                                                                                          NULL, "html", false, &error)) {
                                                EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                                goto FINISH_OFF;
@@ -3872,11 +4039,11 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emsto
                 SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, temp_attachment_tbl->attachment_path);
 
                                if (!attach_part(body_to_attach,
-                                               (unsigned char *)real_file_path, 
-                                               0, 
-                                               name, 
-                                               temp_attachment_tbl->attachment_mime_type, 
-                                               temp_attachment_tbl->attachment_inline_content_status, 
+                                               (unsigned char *)real_file_path,
+                                               0,
+                                               name,
+                                               temp_attachment_tbl->attachment_mime_type,
+                                               temp_attachment_tbl->attachment_inline_content_status,
                                                &error))
                                {
                                        EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
@@ -4042,8 +4209,8 @@ INTERNAL_FUNC int emcore_make_rfc822_file(char *multi_user_name, email_mail_data
        }
 
        modified_mail_data->attachment_count = attachment_count;
-       modified_mail_data->inline_content_count = inline_content_count;        
-       
+       modified_mail_data->inline_content_count = inline_content_count;
+
        if (!emcore_make_rfc822_file_from_mail(multi_user_name, modified_mail_data, modified_attachment_data, input_attachment_count, NULL, file_path, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
                goto FINISH_OFF;
@@ -4330,14 +4497,14 @@ static int emcore_copy_attachment_from_original_mail(char *multi_user_name, int
 
                                EM_DEBUG_LOG("attachment_inline_content_status [%d] attachment_id[%d]", target_attach->attachment_inline_content_status, attachment_id);
 
-                               if(!emcore_save_mail_file(multi_user_name, 
-                                            target_attach->account_id, 
-                                            target_attach->mail_id, 
-                                            attachment_id, 
-                                            original_mail_attachment_array[i].attachment_path, 
-                                            original_mail_attachment_array[i].attachment_name, 
-                                            output_file_path, 
-                                            virtual_file_path,                  
+                               if(!emcore_save_mail_file(multi_user_name,
+                                            target_attach->account_id,
+                                            target_attach->mail_id,
+                                            attachment_id,
+                                            original_mail_attachment_array[i].attachment_path,
+                                            original_mail_attachment_array[i].attachment_name,
+                                            output_file_path,
+                                            virtual_file_path,
                                             &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                                        goto FINISH_OFF;
index 01522ef..f1cf6e7 100755 (executable)
 #define EMAIL_CH_SQUARE_BRACKET_S '['
 #define EMAIL_CH_SQUARE_BRACKET_E ']'
 #define EMAIL_CH_SPACE            ' '
-#define EMAIL_NOTI_ICON_PATH                "/usr/apps/org.tizen.quickpanel/shared/res/noti_icons/noti_email.png"
+#define EMAIL_NOTI_ICON_PATH                "/usr/apps/org.tizen.quickpanel/shared/res/noti_icons/E-mail/noti_email.png"
 #define EMAIL_NOTI_INDICATOR_ICON_PATH      "/usr/apps/org.tizen.indicator/res/icons/Event/B03_Event_email.png"
 #define EMAIL_NOTI_MAX_MAIL_ID   100
 
@@ -150,7 +150,7 @@ INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
                return NULL;
        }
-       
+
        for (; *cursor; ++cursor)
                switch (*cursor) {
                        case '+':
@@ -1633,7 +1633,7 @@ INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int ac
 {
        EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, action:%d", account_id, mail_id, action );
        int err = EMAIL_ERROR_NONE;
-#ifdef __FEATURE_NOTIFICATION_ENABLE__  
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
        int private_id = 0;
        void *join_zone = NULL;
        char *mailbox_name = NULL;
@@ -1754,25 +1754,25 @@ INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int ac
                        break;
                }
 
-               if ((noti_err = notification_set_layout(noti, 
+               if ((noti_err = notification_set_layout(noti,
                                                                                                NOTIFICATION_LY_NOTI_EVENT_SINGLE)) != NOTIFICATION_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("notification_set_layout failed [%d]", noti_err);
                        err = EMAIL_ERROR_NOTI;
                        goto FINISH_OFF;
                }
 
-               if ((noti_err = notification_set_image(noti, 
-                                                                                               NOTIFICATION_IMAGE_TYPE_ICON, 
+               if ((noti_err = notification_set_image(noti,
+                                                                                               NOTIFICATION_IMAGE_TYPE_ICON,
                                                                                                EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
                        err = EMAIL_ERROR_NOTI;
                        goto FINISH_OFF;
                }
 
-               if ((noti_err = notification_set_text(noti, 
+               if ((noti_err = notification_set_text(noti,
                                                                                        NOTIFICATION_TEXT_TYPE_TITLE,
-                                                                                       p_mail_data->alias_recipient,           
-                                                                                       NULL, 
+                                                                                       p_mail_data->alias_recipient,
+                                                                                       NULL,
                                                                                        NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
                        err = EMAIL_ERROR_NOTI;
@@ -1803,9 +1803,9 @@ INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int ac
                        }
                        break;
                default:
-                       if ((noti_err = notification_set_text(noti, 
-                                                                                               NOTIFICATION_TEXT_TYPE_CONTENT, 
-                                                                                               "Sending failed", 
+                       if ((noti_err = notification_set_text(noti,
+                                                                                               NOTIFICATION_TEXT_TYPE_CONTENT,
+                                                                                               "Sending failed",
                                                                                                dgettext_string,
                                                                                                NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
@@ -1816,10 +1816,10 @@ INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int ac
                }
 
                if (sending_error == EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE) {
-                       if ((noti_err = notification_set_text(noti, 
-                                                                                               NOTIFICATION_TEXT_TYPE_INFO_1, 
-                                                                                               dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_POP_THE_EMAIL_IS_TOO_LARGE"), 
-                                                                                               NULL, 
+                       if ((noti_err = notification_set_text(noti,
+                                                                                               NOTIFICATION_TEXT_TYPE_INFO_1,
+                                                                                               dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_POP_THE_EMAIL_IS_TOO_LARGE"),
+                                                                                               NULL,
                                                                                                NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
                                err = EMAIL_ERROR_NOTI;
@@ -1827,7 +1827,7 @@ INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int ac
                        }
                }
 
-               if ((noti_err = notification_set_display_applist(noti, 
+               if ((noti_err = notification_set_display_applist(noti,
                                                                                                NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_TICKER)) != NOTIFICATION_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
                        err = EMAIL_ERROR_NOTI;
@@ -2125,7 +2125,7 @@ int emcore_calc_mail_size(char *multi_user_name, email_mail_data_t *input_mail_d
         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_attachment_data_list[i].attachment_path);
 
                if (stat(real_file_path, &st_buf) < 0)  {
-                       EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
+                       EM_DEBUG_EXCEPTION_SEC("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
                        err = EMAIL_ERROR_FILE_NOT_FOUND;
                        goto FINISH_OFF;
                }
@@ -2978,12 +2978,12 @@ INTERNAL_FUNC char *reg_replace_new(char *input_source_text, char *input_old_pat
                goto FINISH_OFF;
        }
 
-       replaced_str = g_regex_replace_literal(regex, 
-                                                                                       input_source_text, 
-                                                                                       strlen(input_source_text), 
-                                                                                       0, 
-                                                                                       input_new_string, 
-                                                                                       0, 
+       replaced_str = g_regex_replace_literal(regex,
+                                                                                       input_source_text,
+                                                                                       strlen(input_source_text),
+                                                                                       0,
+                                                                                       input_new_string,
+                                                                                       0,
                                                                                        &error);
        if (replaced_str == NULL) {
                EM_DEBUG_EXCEPTION("g_regex_replace_literal failed : [%s][%d]", error->message, error->code);
@@ -2991,7 +2991,7 @@ INTERNAL_FUNC char *reg_replace_new(char *input_source_text, char *input_old_pat
        }
 
 FINISH_OFF:
-       
+
        if (regex) g_regex_unref(regex);
 
        return replaced_str;
@@ -5471,12 +5471,13 @@ INTERNAL_FUNC char *__em_get_month_in_string(int month)
        return mon;
 }
 
-INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *output_date_string)
+INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char **output_date_string)
 {
        EM_DEBUG_FUNC_BEGIN("input_time[%p] output_date_string[%p]", input_time, output_date_string);
        int err = EMAIL_ERROR_NONE;
        struct tm   *timeinfo = NULL;
        char *mon = NULL;
+       char *temp_date_string = NULL;
 
        EM_DEBUG_LOG("RawTime Info [%lu]", input_time);
 
@@ -5489,15 +5490,18 @@ INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *ou
 
        EM_DEBUG_LOG(">>>>>Time %d %d %d %d %d %d", 1900+timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
 
-       memset(output_date_string, 0x00, 20);
-
        mon = __em_get_month_in_string(timeinfo->tm_mon);
        if (mon) {
-               snprintf(output_date_string, 16, "%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
-               EM_DEBUG_LOG("DATE IS [ %s ] ", output_date_string);
+               temp_date_string = g_strdup_printf("%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
+               EM_DEBUG_LOG("DATE IS [ %s ] ", temp_date_string);
                EM_SAFE_FREE(mon);
        }
 
+       if (output_date_string)
+               *output_date_string = temp_date_string;
+       else 
+               EM_SAFE_FREE(temp_date_string);
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
@@ -5521,7 +5525,6 @@ INTERNAL_FUNC int emcore_make_uid_range_string(emcore_uid_list *uid_list, int to
 
        EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
        uid_range = malloc(sizeof(char) * uid_range_size);
-
        if (uid_range == NULL){
                EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
                err  = EMAIL_ERROR_OUT_OF_MEMORY;
index ca19a70..509a56a 100755 (executable)
@@ -1578,7 +1578,7 @@ INTERNAL_FUNC int emstorage_close(int *err_code)
        int ret = false;
        int error = EMAIL_ERROR_NONE;
 
-       if (!emstorage_db_close(NULL, &error)) 
+       if (!emstorage_db_close(NULL, &error))
 
        if (--_open_counter == 0)
                _emstorage_close_once();
@@ -1637,7 +1637,7 @@ static void *_emstorage_open_once(char *multi_user_name, int *err_code)
        }
 
 FINISH_OFF:
-       
+
 
        if (err_code != NULL)
                *err_code = error;
@@ -2845,11 +2845,11 @@ INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *c
        EM_PROFILE_END(emstorage_query_mail_list_performing_query);
 
        if (!base_count)
-               base_count = ({ 
-                               int i=0; 
-                               char *tmp = NULL; 
+               base_count = ({
+                               int i=0;
+                               char *tmp = NULL;
                                for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
-                               i; 
+                               i;
                                });
 
        col_index = base_count;
@@ -3606,8 +3606,8 @@ INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_
                        EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
 
                        /*  get password file name */
-                       error = _get_password_file_name (multi_user_name, p_data_tbl[i].account_id, 
-                                                   recv_password_file_name, 
+                       error = _get_password_file_name (multi_user_name, p_data_tbl[i].account_id,
+                                                   recv_password_file_name,
                                                    send_password_file_name);
                        if (error != EMAIL_ERROR_NONE){
                                EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
@@ -3669,14 +3669,14 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, 
-                                                                                                                       char *server_mail_id, 
-                                                                                                                       int mailbox_id, 
-                                                                                                                       emstorage_mail_tbl_t **mail, 
-                                                                                                                       int transaction, 
+INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
+                                                                                                                       char *server_mail_id,
+                                                                                                                       int mailbox_id,
+                                                                                                                       emstorage_mail_tbl_t **mail,
+                                                                                                                       int transaction,
                                                                                                                        int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
                                                        mailbox_id, server_mail_id, mail, transaction, err_code);
 
        int ret = false, error = EMAIL_ERROR_NONE, result_count;
@@ -3689,18 +3689,21 @@ INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) = UPPER('%s')", server_mail_id);
+       sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
 
-       if (mailbox_id > 0) 
-               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE, " AND mailbox_id = %d", mailbox_id);
-       
-       EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
+       if (mailbox_id > 0) {
+               SNPRINTF(conditional_clause + strlen(conditional_clause),
+                                       QUERY_SIZE - strlen(conditional_clause),
+                                       " AND mailbox_id = %d",
+                                       mailbox_id);
+       }
 
-       if (!emstorage_query_mail_tbl(multi_user_name, 
-                                                                       conditional_clause, 
-                                                                       transaction, 
-                                                                       &p_data_tbl, 
-                                                                       &result_count, 
+       EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
+       if (!emstorage_query_mail_tbl(multi_user_name,
+                                                                       conditional_clause,
+                                                                       transaction,
+                                                                       &p_data_tbl,
+                                                                       &result_count,
                                                                        &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
                goto FINISH_OFF;
@@ -5571,8 +5574,8 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_m
 
        EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
 
-       if ((ret = emstorage_query_mailbox_tbl(multi_user_name, 
-                                                                                       conditional_clause_string, 
+       if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
+                                                                                       conditional_clause_string,
                                                                                        "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
                goto FINISH_OFF;
@@ -5818,15 +5821,15 @@ FINISH_OFF:
 
 }
 
-INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name, 
-                                                                                                               int account_id, 
-                                                                                                               int input_mailbox_id, 
-                                                                                                               int total_count_on_server, 
-                                                                                                               int transaction, 
+INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
+                                                                                                               int account_id,
+                                                                                                               int input_mailbox_id,
+                                                                                                               int total_count_on_server,
+                                                                                                               int transaction,
                                                                                                                int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
-                                               "transaction[%d], err_code[%p]", 
+                                               "transaction[%d], err_code[%p]",
                                                account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
 
        int ret = false;
@@ -6683,6 +6686,7 @@ INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_
        ret = true;
 
 FINISH_OFF:
+
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
@@ -7154,9 +7158,17 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
+                                                                                                       int account_id,
+                                                                                                       int mailbox_id,
+                                                                                                       char *mailbox_name,
+                                                                                                       char *uid,
+                                                                                                       int transaction,
+                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
+       EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
+                                                       "uid[%s], transaction[%d], err_code[%p]",
+                                                       account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
 
        if (account_id < FIRST_ACCOUNT_ID)  {
                EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
@@ -7174,7 +7186,14 @@ INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int ac
 
        EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
+       SNPRINTF(sql_query_string, sizeof(sql_query_string),
+                               "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
+
+       if (mailbox_id > 0) {
+               SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
+                                       sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
+                                       "AND mailbox_id = %d ", mailbox_id);
+       }
 
        if (mailbox_name) {             /*  NULL means all mailbox_name */
                if (strstr(mailbox_name, "'")) {
@@ -7183,11 +7202,15 @@ INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int ac
                        replaced_mailbox_name = strdup(mailbox_name);
                }
 
-               SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_name = '%s' ", replaced_mailbox_name);
+               SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
+                                       sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
+                                       "AND mailbox_name = '%s' ", replaced_mailbox_name);
        }
 
        if (uid) {              /*  NULL means all mail */
-               SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND server_uid='%s' ", uid);
+               sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
+                                                sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
+                                                uid);
        }
 
        error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
@@ -8361,19 +8384,19 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name, 
-                                                                                       emstorage_search_filter_t *search, 
-                                                                                       int account_id, 
-                                                                                       int mailbox_id, 
-                                                                                       int sorting, 
-                                                                                       DB_STMT *search_handle, 
-                                                                                       int *searched, 
-                                                                                       int transaction, 
+INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
+                                                                                       emstorage_search_filter_t *search,
+                                                                                       int account_id,
+                                                                                       int mailbox_id,
+                                                                                       int sorting,
+                                                                                       DB_STMT *search_handle,
+                                                                                       int *searched,
+                                                                                       int transaction,
                                                                                        int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], " 
-                                               "search_handle[%p], searched[%p], transaction[%d], err_code[%p]", 
-                                               search, account_id, mailbox_id, sorting, search_handle, 
+       EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
+                                               "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
+                                               search, account_id, mailbox_id, sorting, search_handle,
                                                searched, transaction, err_code);
 
        if (!search_handle || !searched)  {
@@ -9685,7 +9708,7 @@ INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t
                                                    NULL),
                                     rc);
        if (rc != SQLITE_OK) {
-               EM_DEBUG_EXCEPTION ("sqlite3_prepare error [%d] [%s] SQL(%s) ", 
+               EM_DEBUG_EXCEPTION ("sqlite3_prepare error [%d] [%s] SQL(%s) ",
                              rc, sql_query_string, sqlite3_errmsg(local_db_handle));
                error = EMAIL_ERROR_DB_FAILURE;
                goto FINISH_OFF;
@@ -11511,7 +11534,7 @@ INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name
        return result;
 }
 
-INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id, 
+INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
                                                                                        char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
@@ -11681,10 +11704,10 @@ INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, in
        }
 
        if (account_id >= FIRST_ACCOUNT_ID)  {
-               SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path, 
-                                                                                               DIR_SEPERATOR, 
-                                                                                               MAILHOME, 
-                                                                                               DIR_SEPERATOR, 
+               SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
+                                                                                               DIR_SEPERATOR,
+                                                                                               MAILHOME,
+                                                                                               DIR_SEPERATOR,
                                                                                                account_id);
 
                if (stat(buf, &sbuf) == 0) {
@@ -12292,15 +12315,22 @@ INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
 }
 
 /* faizan.h@samsung.com */
-INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code)
+INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
+                                                                                               int mail_id,
+                                                                                               char *old_server_uid,
+                                                                                               char *new_server_uid,
+                                                                                               int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
        int ret = false;
        int error = EMAIL_ERROR_NONE;
-       char sql_query_string[QUERY_SIZE] = {0, };
        int transaction = true;
+       int temp_strlen = 0;
+       int and_operation = 0;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       char conditional_clause_string[QUERY_SIZE] = {0};
 
-       if (!old_server_uid || !new_server_uid) {
+       if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
                EM_DEBUG_EXCEPTION("Invalid parameters");
                if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
@@ -12308,21 +12338,41 @@ INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_s
        }
 
        sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
-
        EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
-       SNPRINTF(sql_query_string, sizeof(sql_query_string),
-                "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
+       SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
+
+       if (mail_id > 0) {
+               temp_strlen = strlen(conditional_clause_string);
+               SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
+                                       "mail_id = %d ", mail_id);
+               and_operation = 1;
+       }
+
+       if (old_server_uid) {
+               temp_strlen = strlen(conditional_clause_string);
+               if (!and_operation) {
+                       sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
+                                                       "server_mail_id = '%q'", old_server_uid);
+               } else {
+                       sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
+                                                       "and server_mail_id = '%q'", old_server_uid);
+               }
+       }
+
+       sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
+                                               "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
 
        error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
        if (error != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
-                       goto FINISH_OFF;
+               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+               goto FINISH_OFF;
        }
 
        ret = true;
 
 FINISH_OFF:
+
        EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
@@ -12794,14 +12844,14 @@ INTERNAL_FUNC int emstorage_get_max_mail_count()
 
 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
 
-INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name, 
-                                                                                                                       emstorage_mail_tbl_t *mail_tbl, 
-                                                                                                                       int *thread_id, 
-                                                                                                                       int *result_latest_mail_id_in_thread, 
+INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
+                                                                                                                       emstorage_mail_tbl_t *mail_tbl,
+                                                                                                                       int *thread_id,
+                                                                                                                       int *result_latest_mail_id_in_thread,
                                                                                                                        int *thread_item_count)
 {
        EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
-                                               "result_latest_mail_id_in_thread [%p], thread_item_count [%p]", 
+                                               "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
                                                mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
        EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
 
@@ -12838,8 +12888,8 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       if (em_find_pos_stripped_subject_for_thread_view(subject, 
-                                                                                                               stripped_subject, 
+       if (em_find_pos_stripped_subject_for_thread_view(subject,
+                                                                                                               stripped_subject,
                                                                                                                STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
                EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
                err_code =  EMAIL_ERROR_UNKNOWN;
@@ -12854,7 +12904,7 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", 
+       EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
                                                EM_SAFE_STRLEN(stripped_subject), stripped_subject);
 
        if (account_id > 0)     {
@@ -14082,7 +14132,11 @@ FINISH_OFF:
  *                                                     SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
+INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
+                                                                                                               char *mail_ids,
+                                                                                                               email_id_set_t** idset,
+                                                                                                               int *id_set_count,
+                                                                                                               int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
@@ -14102,7 +14156,8 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char
 
 
        if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
-               EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
+               EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
+                                                       mail_ids, idset, id_set_count);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
@@ -14110,14 +14165,17 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char
 
        sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid", mail_ids);
+       SNPRINTF(sql_query_string, space_left_in_query_buffer,
+                       "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
+                       mail_ids);
 
        EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
 
        /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
-       EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
-               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);
+                                               goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
+                                               sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
        EM_DEBUG_LOG(" Count of mails [%d ]", count);
 
@@ -14127,7 +14185,6 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char
                goto FINISH_OFF;
        }
 
-
        if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
                EM_DEBUG_EXCEPTION(" em_malloc failed...");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -15060,21 +15117,21 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, 
-                                                                                                               int account_id, 
+INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
+                                                                                                               int account_id,
                                                                                                                int mailbox_id,
                                                                                                                int mailbox_type,
-                                                                                                               int thread_id, 
-                                                                                                               int *updated_thread_id, 
-                                                                                                               int latest_mail_id, 
-                                                                                                               int thread_item_count, 
-                                                                                                               int noti_type, 
-                                                                                                               int transaction, 
+                                                                                                               int thread_id,
+                                                                                                               int *updated_thread_id,
+                                                                                                               int latest_mail_id,
+                                                                                                               int thread_item_count,
+                                                                                                               int noti_type,
+                                                                                                               int transaction,
                                                                                                                int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
-                                               "latest_mail_id [%d], thread_item_count[%d], err_code[%p]", 
-                                               account_id, mailbox_id, thread_id, updated_thread_id, 
+                                               "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
+                                               account_id, mailbox_id, thread_id, updated_thread_id,
                                                latest_mail_id, thread_item_count, err_code);
 
        int rc = -1, ret = false;
@@ -16645,14 +16702,14 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emstorage_query_task(char *multi_user_name, 
-                                                                       const char *input_conditional_clause, 
-                                                                       const char *input_ordering_clause, 
-                                                                       email_task_t **output_task_list, 
+INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
+                                                                       const char *input_conditional_clause,
+                                                                       const char *input_ordering_clause,
+                                                                       email_task_t **output_task_list,
                                                                        int *output_task_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
-                                               "output_task_list[%p], output_task_count[%d]", 
+                                               "output_task_list[%p], output_task_count[%d]",
                                                input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
        int i = 0, count = 0, rc = -1;
        int cur_query = 0;
@@ -16670,7 +16727,7 @@ INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
 
        local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, 
+       SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
                                        "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
        EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
 
@@ -16689,7 +16746,7 @@ INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, 
+       SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
                                        "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
        EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
 
@@ -16741,9 +16798,9 @@ FINISH_OFF:
                *output_task_count = count;
        } else {
                if (task_item_from_tbl) {
-                       for (i = 0; i < count; i++) 
+                       for (i = 0; i < count; i++)
                                EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
-                       
+
                        free(task_item_from_tbl);
                }
        }
index abe181b..38d0c25 100755 (executable)
@@ -780,7 +780,13 @@ INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int acco
  * arguments :
  * return  :
  */
-INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, 
+                                                                                                       int account_id, 
+                                                                                                       int mailbox_id,
+                                                                                                       char *mailbox_name, 
+                                                                                                       char *uid, 
+                                                                                                       int transaction, 
+                                                                                                       int *err_code);
 
 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code);
 
@@ -1126,7 +1132,11 @@ INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emst
 
 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code);
 
-INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code);
+INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, 
+                                                                                               int mail_id,
+                                                                                               char *old_server_uid, 
+                                                                                               char *new_server_uid, 
+                                                                                               int *err_code);
 
 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code);
 
old mode 100644 (file)
new mode 100755 (executable)
index 2f844c3..fd33155
@@ -4,7 +4,7 @@
 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Sunghyun Kwon <sh0701.kwon@samsung.com>, Minsoo Kim <minnsoo.kim@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
  * @file       email-core-cynara.h
  * @author     sh0701.kwon@samsung.com
  * @version    0.1
- * @brief      This file contains functionality to provide cynara support in email-service. 
+ * @brief      This file contains functionality to provide cynara support in email-service.
  */
 
 INTERNAL_FUNC int emcore_init_cynara();
 
 INTERNAL_FUNC void emcore_finish_cynara();
 
-INTERNAL_FUNC int emcore_check_privilege(int socket_fd);
+INTERNAL_FUNC int emcore_check_privilege(unsigned int socket_fd);
index 78312db..3ff9400 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,9 +24,9 @@
  * File :  email-core-mail.h
  * Desc :  Mail Operation Header
  *
- * Auth : 
+ * Auth :
  *
- * History : 
+ * History :
  *    2006.08.16  :  created
  *****************************************************************************/
 #ifndef __EMAIL_CORE_MESSAGE_H__
@@ -40,7 +40,7 @@ extern "C" {
 
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
 
-#define MAX_SUBSET_STRING_SIZE 260     
+#define MAX_SUBSET_STRING_SIZE 260
 #define MAX_IMAP_COMMAND_LENGTH 1000
 #define MAX_TAG_SIZE 16
 
@@ -50,9 +50,7 @@ typedef struct _emf_uid_range_set
        char *uid_range;
        unsigned long lowest_uid;
        unsigned long highest_uid;
-       
        struct _emf_uid_range_set *next;
-       
 } email_uid_range_set;
 
 #endif
@@ -206,7 +204,7 @@ struct _m_mesg {
        int                      size;            /* text size if not contain multipart */
 };
 
-struct _m_content_info 
+struct _m_content_info
 {
        int grab_type;  /*  1 :  download text and get attachment names (no saving attachment) -
                                                        #define GRAB_TYPE_TEXT retrieve text and attachment names */
@@ -220,9 +218,9 @@ struct _m_content_info
        int content_type; /* 1 : signed */
        char *sections;
 
-       struct text_data 
+       struct text_data
        {
-               int   plain_save_status; 
+               int   plain_save_status;
                char *plain;             /*  body plain text */
                char *plain_charset;     /*  charset of body text */
                int   html_save_status;
@@ -275,7 +273,7 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int nu
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code) ;
 INTERNAL_FUNC int emcore_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code);
-#endif 
+#endif
 
 INTERNAL_FUNC int emcore_get_mail_contact_info(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int *err_code);
 INTERNAL_FUNC int emcore_get_mail_contact_info_with_update(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code);
@@ -295,7 +293,13 @@ INTERNAL_FUNC int emcore_get_mail_data(char *multi_user_name, int input_mail_id,
 
 INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, 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 sync_server);
 
-INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name, int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code);
+INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name,
+                                                                                                                       int account_id,
+                                                                                                                       int *mail_ids,
+                                                                                                                       int num,
+                                                                                                                       int noti_param_1,
+                                                                                                                       int noti_param_2,
+                                                                                                                       int *err_code);
 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code);
 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(char *multi_user_name, int input_mailbox_id);
 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *multi_user_name, int input_account_id, int input_mailbox_id);
@@ -304,6 +308,7 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *
  * Delete mails.
  *
  * @param[in] account_id      Specifies the account id.
+ * @param[in] mailbox_id      Specifies the mailbox id.
  * @param[in] mail_id         Specifies the array for mail id.
  * @param[in] num             Specifies the number of id.
  * @param[in] from_server     Specifies whether mails is deleted from server.
@@ -314,7 +319,15 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emcore_delete_mail(char *multi_user_name, int account_id, int mail_id[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code);
+INTERNAL_FUNC int emcore_delete_mail(char *multi_user_name,
+                                                                       int account_id,
+                                                                       int mailbox_id,
+                                                                       int mail_id[],
+                                                                       int num,
+                                                                       int from_server,
+                                                                       int noti_param_1,
+                                                                       int noti_param_2,
+                                                                       int *err_code);
 
 /**
  * Delete mails.
@@ -326,11 +339,11 @@ INTERNAL_FUNC int emcore_delete_mail(char *multi_user_name, int account_id, int
  * @return This function returns true on success or false on failure.
  */
 INTERNAL_FUNC int   emcore_delete_all_mails_of_acount(char *multi_user_name, int input_account_id);
-INTERNAL_FUNC int   emcore_delete_all_mails_of_mailbox(char *multi_user_name, 
-                                                                                                               int input_account_id, 
-                                                                                                               int input_mailbox_id, 
+INTERNAL_FUNC int   emcore_delete_all_mails_of_mailbox(char *multi_user_name,
+                                                                                                               int input_account_id,
+                                                                                                               int input_mailbox_id,
                                                                                                                int input_mailbox_type,
-                                                                                                               int input_from_server, 
+                                                                                                               int input_from_server,
                                                                                                                int *err_code);
 
 INTERNAL_FUNC void  emcore_free_mail_data_list(email_mail_data_t **mail_list, int count);
@@ -371,16 +384,25 @@ INTERNAL_FUNC int   emcore_delete_rule(char *multi_user_name, int filter_id);
  * @param[in] cancellable               Specifies the cancellable
  * @param[in] handle                    Specifies the handle for searching mails
  * @remarks N/A
- * @return EMAIL_ERROR_NONE on success or an error code (refer to EMAIL_ERROR_XXX) on failure 
+ * @return EMAIL_ERROR_NONE on success or an error code (refer to EMAIL_ERROR_XXX) on failure
  */
 
-INTERNAL_FUNC int   emcore_search_on_server(char *multi_user_name, 
-                                                                                       int account_id, 
-                                                                                       int mailbox_id, 
-                                                                                       email_search_filter_t *input_search_filter, 
-                                                                                       int input_search_filter_count, 
+INTERNAL_FUNC int   emcore_search_on_server(char *multi_user_name,
+                                                                                       int account_id,
+                                                                                       int mailbox_id,
+                                                                                       email_search_filter_t *input_search_filter,
+                                                                                       int input_search_filter_count,
                                                                                        int cancellable,
                                                                                        int event_handle);
+
+INTERNAL_FUNC int   emcore_search_on_server_ex(char *multi_user_name, 
+                                                                                               int account_id, 
+                                                                                               int mailbox_id, 
+                                                                                               email_search_filter_t *input_search_filter, 
+                                                                                               int input_search_filter_count, 
+                                                                                               int cancellable,
+                                                                                               int event_handle);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 52d4165..e5b5e22 100755 (executable)
@@ -177,7 +177,6 @@ int emcore_get_uid(emcore_uid_list *uid_list, int msgno, char **uid, int *err_co
  * @return This function returns true on success or false on failure.
  */
 int emcore_free_uids(emcore_uid_list *uid_list, int *err_code);
-
 INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(char *multi_user_name, int mail_id);
 INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name, 
                                                         struct _m_content_info *cnt_info, 
@@ -188,6 +187,10 @@ INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name,
                                                         int *output_attachment_count, 
                                                         int *output_inline_attachment_count);
 
+INTERNAL_FUNC int emcore_sync_mail_by_message_id(char *multi_user_name, 
+                                                                                                       int mail_id, 
+                                                                                                       int mailbox_id, 
+                                                                                                       char **output_server_uid);
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, 
                                                                                                                email_event_partial_body_thd *pbd_event, 
index 3b8aea7..ebb7f7d 100755 (executable)
@@ -53,8 +53,19 @@ INTERNAL_FUNC int emcore_remove_connection_info(int account_id);
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 /*  in SMTP case, path argument must be (ENCODED_PATH_SMTP) */
 /*  ex) emcore_connect_to_remote_mailbox(xxx, (char *)ENCODED_PATH_SMTP, xxx, xxx); */
-INTERNAL_FUNC int  emcore_connect_to_remote_mailbox_with_account_info(char *multi_user_name, email_account_t *ref_account, int input_mailbox_id, void **mail_stream, int *err_code);
-INTERNAL_FUNC int  emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, void **mail_stream, int *err_code);
+INTERNAL_FUNC int  emcore_connect_to_remote_mailbox_with_account_info(char *multi_user_name, 
+                                                                                                                                               email_account_t *ref_account, 
+                                                                                                                                               int input_mailbox_id, 
+                                                                                                                                               int reusable,
+                                                                                                                                               void **mail_stream, 
+                                                                                                                                               int *err_code);
+
+INTERNAL_FUNC int  emcore_connect_to_remote_mailbox(char *multi_user_name, 
+                                                                                                       int account_id, 
+                                                                                                       int input_mailbox_id,
+                                                                                                       int reusable,
+                                                                                                       void **mail_stream, 
+                                                                                                       int *err_code);
 INTERNAL_FUNC int  emcore_close_mailbox(int account_id, void *mail_stream);
 #ifdef __FEATURE_KEEP_CONNECTION__
 INTERNAL_FUNC void emcore_close_mailbox_receiving_stream();
index e78a144..e4c7e77 100755 (executable)
@@ -235,7 +235,7 @@ INTERNAL_FUNC int emcore_update_db_table_schema(char *multi_user_name);
 #endif /* #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
 INTERNAL_FUNC int emcore_unescape_from_url(char *input_url, char **output_url);
 INTERNAL_FUNC char *__em_get_month_in_string(int month);
-INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *output_date_string);
+INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char **output_date_string);
 INTERNAL_FUNC int emcore_make_uid_range_string(emcore_uid_list *uid_list, int total, char **output_uid_range_string);
 
 INTERNAL_FUNC char* emcore_convert_mutf7_to_utf8(char *mailbox_name); 
index 5d78990..bfa2a3a 100755 (executable)
@@ -73,7 +73,13 @@ static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, i
 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id);
 static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int account_id, int mail_id, int option, int handle_to_be_published, int *error);
 static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name, int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error);
+static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name,
+                                                                                                                               int account_id,
+                                                                                                                               int mail_ids[],
+                                                                                                                               int num,
+                                                                                                                               email_flags_field_type field_type,
+                                                                                                                               int value,
+                                                                                                                               int *error);
 static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(char *multi_user_name, int account_id, int handle_to_be_published, int *error);
 static int event_handler_EMAIL_EVENT_UPDATE_MAIL(char *multi_user_name, 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, int handle_to_be_published);
 static int event_handler_EMAIL_EVENT_SAVE_MAIL(char *multi_user_name, int input_account_id, int input_mail_id, int input_handle_to_be_published);
@@ -82,7 +88,15 @@ static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(char *multi_user_name, int m
 static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(char *multi_user_name, email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published);
 static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(char *multi_user_name, int mail_id, int event_handle, int *error);
 static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int input_account_id, int input_mailbox_id, int input_from_server, int *error);
-static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error);
+
+static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name,
+                                                                                                       int account_id,
+                                                                                                       int mailbox_id,
+                                                                                                       int *mail_id_list,
+                                                                                                       int mail_id_count,
+                                                                                                       int from_server,
+                                                                                                       int *error);
+
 static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int account_id, char *maibox_name, int handle_to_be_published, int *error);
 static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int account_id, int mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, int handle_to_be_published);
 static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published);
@@ -399,11 +413,23 @@ static void* worker_event_queue(void *arg)
                                                break;
 
                                        case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
-                                               event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(event_data->multi_user_name, (int*)event_data->event_param_data_3, event_data->event_param_data_4 , event_data->event_param_data_5, event_data->event_param_data_6, &err);
+                                               event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(event_data->multi_user_name,
+                                                                                                                                                       event_data->account_id,
+                                                                                                                                                       (int*)event_data->event_param_data_3,
+                                                                                                                                                       event_data->event_param_data_4 ,
+                                                                                                                                                       event_data->event_param_data_5,
+                                                                                                                                                       event_data->event_param_data_6,
+                                                                                                                                                       &err);
                                                break;
 
                                        case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
-                                               event_handler_EMAIL_EVENT_DELETE_MAIL(event_data->multi_user_name, event_data->account_id, (int*)event_data->event_param_data_3, event_data->event_param_data_4, event_data->event_param_data_5, &err);
+                                               event_handler_EMAIL_EVENT_DELETE_MAIL(event_data->multi_user_name,
+                                                                                                                               event_data->account_id,
+                                                                                                                               event_data->event_param_data_6,
+                                                                                                                               (int*)event_data->event_param_data_3,
+                                                                                                                               event_data->event_param_data_4,
+                                                                                                                               event_data->event_param_data_5,
+                                                                                                                               &err);
                                                break;
 
                                        case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
@@ -415,7 +441,7 @@ static void* worker_event_queue(void *arg)
                                                break;
 #endif
 
-                                       case EMAIL_EVENT_CREATE_MAILBOX: 
+                                       case EMAIL_EVENT_CREATE_MAILBOX:
                                                err = event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data->multi_user_name, (email_mailbox_t *)event_data->event_param_data_1, event_data->event_param_data_4, handle_to_be_published);
                                                break;
 
@@ -449,10 +475,10 @@ static void* worker_event_queue(void *arg)
 
                                        case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
                                                        email_account_t *account = (email_account_t *)event_data->event_param_data_1;
-                                                       event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data->multi_user_name, 
-                                                                                                                                                                       event_data->account_id, 
-                                                                                                                                                                       account, 
-                                                                                                                                                                       handle_to_be_published, 
+                                                       event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data->multi_user_name,
+                                                                                                                                                                       event_data->account_id,
+                                                                                                                                                                       account,
+                                                                                                                                                                       handle_to_be_published,
                                                                                                                                                                        &err);
                                                }
                                                break;
@@ -493,7 +519,7 @@ static void* worker_event_queue(void *arg)
                                }
                        }
 
-                       if ((event_data->type == EMAIL_EVENT_SYNC_HEADER || event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX) && 
+                       if ((event_data->type == EMAIL_EVENT_SYNC_HEADER || event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX) &&
                                (err != EMAIL_ERROR_NONE)) {
                                EM_DEBUG_LOG("retry syncing");
                                if (event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX && err == EMAIL_ERROR_INVALID_ACCOUNT) {
@@ -564,10 +590,10 @@ FINISH_OFF:
        return SUCCESS;
 }
 
-static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int input_account_id, int input_mailbox_id, 
+static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int input_account_id, int input_mailbox_id,
                                                                        int handle_to_be_published, int *error)
 {
-       EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", 
+       EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]",
                                                    input_account_id, input_mailbox_id, handle_to_be_published, error);
 
        int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
@@ -629,14 +655,14 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
 
        if (sync_type != EMAIL_SYNC_ALL_MAILBOX) {      /* Sync only particular mailbox */
                EM_DEBUG_LOG_SEC ("sync start: account_id [%d] alias [%s]", input_account_id, mailbox_tbl_target->alias);
-               if ((err = emcore_update_sync_status_of_account(multi_user_name, 
-                                                                                                               input_account_id, 
-                                                                                                               SET_TYPE_UNION, 
+               if ((err = emcore_update_sync_status_of_account(multi_user_name,
+                                                                                                               input_account_id,
+                                                                                                               SET_TYPE_UNION,
                                                                                                                SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
 
                if (!emcore_sync_header (multi_user_name, mailbox_tbl_target, (void**) &stream,
-                                                                  &uid_list, &mail_count, &unread, &vip_mail_count, 
+                                                                  &uid_list, &mail_count, &unread, &vip_mail_count,
                                                                   &vip_unread, 1, handle_to_be_published, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
                        if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
@@ -715,11 +741,11 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
                                continue;
                        }
 /* folder sync is also necessary */
-                       if (!emstorage_get_mailbox_list (multi_user_name, account_tbl_array[account_index].account_id, 0, 
-                                  EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || 
+                       if (!emstorage_get_mailbox_list (multi_user_name, account_tbl_array[account_index].account_id, 0,
+                                  EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) ||
                                                                                                   mailbox_count <= 0) {
                                EM_DEBUG_EXCEPTION ("emstorage_get_mailbox error [%d]", err);
-                               if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, 
+                               if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id,
                                                                      input_mailbox_id_str, handle_to_be_published, err))
                                        EM_DEBUG_EXCEPTION ("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] error >>>> ");
                                continue;
@@ -732,8 +758,12 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
                        if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
                                memset(mailbox_id_param_string, 0, 10);
                                SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
-                               if (!emcore_connect_to_remote_mailbox (multi_user_name, account_tbl_array[account_index].account_id, 
-                                                      mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err))  {
+                               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                                               account_tbl_array[account_index].account_id,
+                                                                                                               mailbox_tbl_list[0].mailbox_id,
+                                                                                                               true,
+                                                                                                               (void **)&stream,
+                                                                                                               &err))  {
                                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox error [%d]", err);
                                        if (err == EMAIL_ERROR_LOGIN_FAILURE)
                                                EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
@@ -762,32 +792,32 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
                                        EM_DEBUG_LOG_SEC("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
 #ifdef __FEATURE_KEEP_CONNECTION__
                                        if (!emcore_sync_header (multi_user_name,
-                                      (mailbox_tbl_list + counter), 
-                                      (void **)&stream, 
-                                      &uid_list, 
-                                      &mail_count, 
+                                      (mailbox_tbl_list + counter),
+                                      (void **)&stream,
+                                      &uid_list,
+                                      &mail_count,
                                       &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
 #else /*  __FEATURE_KEEP_CONNECTION__ */
-                                       if (!emcore_sync_header (multi_user_name, 
-                                      (mailbox_tbl_list + counter), 
-                                      (void **)&stream, 
-                                      &uid_list, 
-                                      &mail_count, 
+                                       if (!emcore_sync_header (multi_user_name,
+                                      (mailbox_tbl_list + counter),
+                                      (void **)&stream,
+                                      &uid_list,
+                                      &mail_count,
                                       &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
 #endif /*  __FEATURE_KEEP_CONNECTION__ */
-                                       {       
-                                               EM_DEBUG_EXCEPTION_SEC ("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", 
+                                       {
+                                               EM_DEBUG_EXCEPTION_SEC ("emcore_sync_header for %s(mailbox_id = %d) failed [%d]",
                                                        mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, err);
 
 #ifndef __FEATURE_KEEP_CONNECTION__
-                                               if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || 
+                                               if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST ||
                                                                                  err == EMAIL_ERROR_SOCKET_FAILURE)
-                                                       stream = mail_close (stream);    
+                                                       stream = mail_close (stream);
 #endif /*  __FEATURE_KEEP_CONNECTION__ */
                                                memset(mailbox_id_param_string, 0, 10);
                                                SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
-                                               if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL, 
-                                                                           account_tbl_array[account_index].account_id, 
+                                               if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL,
+                                                                           account_tbl_array[account_index].account_id,
                                                                            mailbox_id_param_string,
                                                                            handle_to_be_published, err))
                                                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
@@ -795,7 +825,7 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
                                                break;
                                        }
                                }
-                               EM_DEBUG_LOG_SEC("---mailbox %s has unread %d / %d", mailbox_tbl_list[counter].mailbox_name, 
+                               EM_DEBUG_LOG_SEC("---mailbox %s has unread %d / %d", mailbox_tbl_list[counter].mailbox_name,
                                                                                                 unread, mail_count);
                                total_unread     += unread;
                                vip_total_unread += vip_unread;
@@ -826,7 +856,7 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int inp
                        }
 
 #ifndef __FEATURE_KEEP_CONNECTION__
-                       if (stream)  
+                       if (stream)
                                stream = mail_close (stream);
 #endif
                        if (mailbox_tbl_list) {
@@ -1151,17 +1181,22 @@ static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, i
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id)
+static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name,
+                                                                                                                                       int input_account_id,
+                                                                                                                                       int input_mailbox_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
        int err = EMAIL_ERROR_NONE;
 
-       if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(multi_user_name, input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_expunge_mails_deleted_flagged_from_remote_server(multi_user_name,
+                                                                                                                                               input_account_id,
+                                                                                                                                               input_mailbox_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name,
+                                                                                                                                               input_mailbox_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1236,27 +1271,29 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(char *multi_user_name, int a
                                                                switch (local_activity[k-1].activity_type) {
                                                                        case ACTIVITY_DELETEMAIL:  {
                                                                                if (!emcore_delete_mail(multi_user_name,
-                                                                                                                                               local_activity[k-1].account_id,
-                                                                                                                                               mail_id_list,
-                                                                                                                                               j,
-                                                                                                                                               EMAIL_DELETE_LOCAL_AND_SERVER,
-                                                                                                                                               EMAIL_DELETED_BY_COMMAND,
-                                                                                                                                               false,
-                                                                                                                                               &err))
+                                                                                                                               local_activity[k-1].account_id,
+                                                                                                                               mail_id_list,
+                                                                                                                               j,
+                                                                                                                               EMAIL_DELETE_LOCAL_AND_SERVER,
+                                                                                                                               EMAIL_DELETED_BY_COMMAND,
+                                                                                                                               false,
+                                                                                                                               &err))
                                                                                        EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
                                                                        }
                                                                        break;
 
                                                                        case ACTIVITY_MOVEMAIL:  {
-                                                                               if (!emcore_move_mail_on_server_ex(multi_user_name, local_activity[k-1].account_id ,
-                                                                                                                                                  local_activity[k-1].src_mbox,
-                                                                                                                                                  mail_id_list,
-                                                                                                                                                  j,
-                                                                                                                                                  local_activity[k-1].dest_mbox,
-                                                                                                                                                  &err))
+                                                                               if (!emcore_move_mail_on_server_ex(multi_user_name,
+                                                                                                                                                       local_activity[k-1].account_id ,
+                                                                                                                                                   local_activity[k-1].src_mbox,
+                                                                                                                                                   mail_id_list,
+                                                                                                                                                   j,
+                                                                                                                                                   local_activity[k-1].dest_mbox,
+                                                                                                                                                   &err))
                                                                                        EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
                                                                        }
                                                                        break;
+
                                                                        case ACTIVITY_MODIFYSEENFLAG:  {
                                                                                int seen_flag = atoi(local_activity[0].src_mbox);
                                                                                if (!emcore_sync_seen_flag_with_server_ex(multi_user_name, mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
@@ -1325,8 +1362,12 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int ac
                        goto FINISH_OFF;
                }
 
-
-               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name,
+                                                                                               account_id,
+                                                                                               mail->mailbox_id,
+                                                                                               true,
+                                                                                               (void **)&tmp_stream,
+                                                                                               &err) || !tmp_stream)  {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                        if(err == EMAIL_ERROR_NO_SUCH_HOST)
                                err = EMAIL_ERROR_CONNECTION_FAILURE;
@@ -1391,7 +1432,13 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name,
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
+static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name,
+                                                                                                                               int account_id,
+                                                                                                                               int mail_ids[],
+                                                                                                                               int num,
+                                                                                                                               email_flags_field_type field_type,
+                                                                                                                               int value,
+                                                                                                                               int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1402,6 +1449,18 @@ static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user
        else if (!emcore_sync_flags_field_with_server(multi_user_name, mail_ids, num, field_type, value, &err))
                EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
 
+       /* timing issue : sending mail (to me) -> sync header -> enter the viewer */
+       /* viewer is changed the DB -> While syncing update again */
+       /* So in event update again the DB field */
+       if (err == EMAIL_ERROR_NONE) {
+               if (!emcore_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
+               }
+       } else {
+               /* If the emcore_sync_flags_field_with_server is failed, rollback the db */
+               emcore_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value ? 0 : 1, NULL);
+       }
+
        if (error)
                *error = err;
 
@@ -1608,11 +1667,11 @@ static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id [%d], input_from_server [%d], error [%p]", input_account_id, input_mailbox_id, input_from_server, error);
        int err = EMAIL_ERROR_NONE;
 
-       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
-                                                                                       input_account_id, 
-                                                                                       input_mailbox_id, 
+       if (!emcore_delete_all_mails_of_mailbox(multi_user_name,
+                                                                                       input_account_id,
+                                                                                       input_mailbox_id,
                                                                                        0,
-                                                                                       input_from_server, 
+                                                                                       input_from_server,
                                                                                        &err))
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
 
@@ -1623,7 +1682,13 @@ static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error)
+static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name,
+                                                                                                       int account_id,
+                                                                                                       int mailbox_id,
+                                                                                                       int *mail_id_list,
+                                                                                                       int mail_id_count,
+                                                                                                       int from_server,
+                                                                                                       int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
@@ -1636,12 +1701,21 @@ static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int acco
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, mail_id_count, from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
+       if (!emcore_delete_mail(multi_user_name,
+                                                       account_id,
+                                                       mailbox_id,
+                                                       mail_id_list,
+                                                       mail_id_count,
+                                                       from_server,
+                                                       EMAIL_DELETED_BY_COMMAND,
+                                                       false,
+                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
 
        ret = true;
+
 FINISH_OFF:
 
        if (account_ref) {
@@ -1679,21 +1753,21 @@ static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int account_id, int mailbox_id, 
-                                                                                                               email_search_filter_t *input_search_filter, 
+static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int account_id, int mailbox_id,
+                                                                                                               email_search_filter_t *input_search_filter,
                                                                                                                int input_search_filter_count, int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], input_search_filter : [%p], "
-                                               "input_search_filter_count : [%d], handle_to_be_published [%d]", 
-                                               account_id, mailbox_id, input_search_filter, 
+                                               "input_search_filter_count : [%d], handle_to_be_published [%d]",
+                                               account_id, mailbox_id, input_search_filter,
                                                input_search_filter_count, handle_to_be_published);
 
        int err = EMAIL_ERROR_NONE;
        char mailbox_id_param_string[10] = {0,};
        emstorage_mailbox_tbl_t *local_mailbox = NULL;
 
-       if ((err = emstorage_get_mailbox_by_id(multi_user_name, 
-                                                                                       mailbox_id, 
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name,
+                                                                                       mailbox_id,
                                                                                        &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
@@ -1708,20 +1782,20 @@ static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int
                goto FINISH_OFF;
        }
 
-       if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_START, 
-                                                                               account_id, 
-                                                                               mailbox_id_param_string, 
-                                                                               handle_to_be_published, 
+       if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_START,
+                                                                               account_id,
+                                                                               mailbox_id_param_string,
+                                                                               handle_to_be_published,
                                                                                0))
                EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_START] failed >>>>");
 
-       if ((err = emcore_search_on_server(multi_user_name, 
-                                                                               account_id, 
-                                                                               mailbox_id, 
-                                                                               input_search_filter, 
-                                                                               input_search_filter_count, 
-                                                                               true,
-                                                                               handle_to_be_published)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_search_on_server_ex(multi_user_name,
+                                                                                       account_id,
+                                                                                       mailbox_id,
+                                                                                       input_search_filter,
+                                                                                       input_search_filter_count,
+                                                                                       true,
+                                                                                       handle_to_be_published)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_search_on_server failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1729,17 +1803,17 @@ static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int
 FINISH_OFF:
 
        if (err != EMAIL_ERROR_NONE) {
-               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, 
-                                                                                       account_id, 
-                                                                                       mailbox_id_param_string, 
-                                                                                       handle_to_be_published, 
+               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL,
+                                                                                       account_id,
+                                                                                       mailbox_id_param_string,
+                                                                                       handle_to_be_published,
                                                                                        0))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAILED] failed >>>>");
        } else {
-               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH, 
-                                                                                                                               account_id, 
-                                                                                                                               NULL, 
-                                                                                                                               handle_to_be_published, 
+               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH,
+                                                                                                                               account_id,
+                                                                                                                               NULL,
+                                                                                                                               handle_to_be_published,
                                                                                                                                0))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");
        }
@@ -1751,12 +1825,12 @@ FINISH_OFF:
        return err;
 }
 
-static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, 
-                                                                                                                               int input_account_id, 
-                                                                                                                               int input_mailbox_id, 
-                                                                                                                               char *input_old_mailbox_path, 
-                                                                                                                               char *input_new_mailbox_path, 
-                                                                                                                               char *input_new_mailbox_alias, 
+static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name,
+                                                                                                                               int input_account_id,
+                                                                                                                               int input_mailbox_id,
+                                                                                                                               char *input_old_mailbox_path,
+                                                                                                                               char *input_new_mailbox_path,
+                                                                                                                               char *input_new_mailbox_alias,
                                                                                                                                int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_path [%s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, input_new_mailbox_alias, handle_to_be_published);
@@ -1804,7 +1878,7 @@ static void* worker_send_event_queue(void *arg)
                        send_thread_run = 0;
 
                        emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_SENDING_WORKER, true, NULL);
-                       
+
 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
                        int wifi_status = 0;
                        if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
@@ -1869,7 +1943,7 @@ static void* worker_send_event_queue(void *arg)
 
                                                                        case ACTIVITY_DELETEMAIL_SEND:                          /* New Activity Type Added for Race Condition and Crash Fix */ {
                                                                                if (!emcore_delete_mail(local_activity[0].multi_user_name,
-                                                                                                                                local_activity[0].account_id,
+                                                                                                                               local_activity[0].account_id,
                                                                                                                                &local_activity[0].mail_id,
                                                                                                                                EMAIL_DELETE_FOR_SEND_THREAD,
                                                                                                                                true,
@@ -2060,7 +2134,7 @@ static gpointer partial_body_download_thread(gpointer data)
                                }
 
                                EM_DEBUG_LOG_DEV(" Partial Body Thread is going to sleep");
-                               
+
                                /* finalize sync */
                                account_count = 0;
                                account_list = NULL;
@@ -2075,7 +2149,7 @@ static gpointer partial_body_download_thread(gpointer data)
 
                                if (account_list)
                                        emstorage_free_account(&account_list, account_count, NULL);
-       
+
                                emcore_set_pbd_thd_state(false);
 
 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
index 8a1fbcb..32d8781 100755 (executable)
@@ -264,11 +264,11 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name, 
-                                        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, 
+INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name,
+                                        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)
 {
        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);
@@ -592,67 +592,64 @@ void* thread_func_to_delete_mail(void *thread_argument)
        int *mail_id_list = NULL;
        int  mail_id_count = 0;
        int  account_id = 0;
-       int  from_server = 0;
+       int  mailbox_id = 0;
        int  noti_param_2 = 0;
-       int  handle = 0;
        char *multi_user_name = NULL;
        email_event_t *event_data = (email_event_t*)thread_argument;
 
+       if (event_data == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               return NULL;
+       }
+
        account_id         = event_data->account_id;
        mail_id_list       = (int*)event_data->event_param_data_3;
        mail_id_count      = event_data->event_param_data_4;
-       from_server        = event_data->event_param_data_5;
+       mailbox_id         = event_data->event_param_data_6;
        multi_user_name    = event_data->multi_user_name;
 
-       if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, 
-                                                       mail_id_count, EMAIL_DELETE_LOCALLY, 
-                                                       EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
+       if (!emcore_delete_mail(multi_user_name,
+                                                       account_id,
+                                                       mailbox_id,
+                                                       mail_id_list,
+                                                       mail_id_count,
+                                                       EMAIL_DELETE_LOCALLY,
+                                                       EMAIL_DELETE_LOCAL_AND_SERVER,
+                                                       noti_param_2,
+                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
-               emcore_free_event(event_data); /* prevent 17922 */
-               EM_SAFE_FREE(event_data);
                goto FINISH_OFF;
        }
 
-       if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
-               if (!emcore_insert_event(event_data, &handle, &err)) {
-                       EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
-/*                     if (from_server != EMAIL_DELETE_LOCAL_AND_SERVER && from_server != EMAIL_DELETE_FROM_SERVER) {
-                               EM_SAFE_FREE(event_data->event_param_data_3);
-                       } */
-                       emcore_free_event(event_data); /* prevent 17922 */
-                       EM_SAFE_FREE(event_data);
-                       goto FINISH_OFF;
-               }
-       }
-       else {
-               emcore_free_event(event_data); /* prevent 17922 */
-               EM_SAFE_FREE(event_data);
-       }
-
 FINISH_OFF:
-       /* Don't free event_data as if the data is destined to be passed to event handler through emcore_insert_event.*/
+
+       emcore_free_event(event_data);
+       EM_SAFE_FREE(event_data);
 
        EM_DEBUG_FUNC_END();
        return SUCCESS;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, 
-                                                                               int account_id, 
-                                                                               int mail_ids[], 
-                                                                               int mail_ids_count, 
-                                                                               int from_server, 
-                                                                               int *handle, 
+INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name,
+                                                                               int account_id,
+                                                                               int mailbox_id,
+                                                                               int mail_ids[],
+                                                                               int mail_ids_count,
+                                                                               int from_server,
+                                                                               int *handle,
                                                                                int* err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]", 
-                                                       account_id, mail_ids, mail_ids_count, from_server, handle, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], mail_ids[%p], "
+                                               "mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]",
+                                               account_id, mailbox_id, mail_ids, mail_ids_count, from_server, handle, err_code);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int *p = NULL;
+       int *p = NULL, *p2 = NULL;
        int thread_error = 0;
-       email_account_t *ref_account = NULL;
        email_event_t *event_data = NULL;
+       email_event_t *thread_func_event_data = NULL;
+       email_account_t *ref_account = NULL;
        thread_t delete_thread;
 
        /* mailbox can be NULL for deleting thread mail. */
@@ -662,20 +659,6 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
-               EM_DEBUG_EXCEPTION("em_malloc for p failed...");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
-
-       if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
-               EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
        ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
@@ -687,30 +670,89 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name,
                from_server = EMAIL_DELETE_LOCALLY;
        }
 
-       event_data->type                   = EMAIL_EVENT_DELETE_MAIL;
-       event_data->account_id             = account_id;
-       event_data->event_param_data_3     = (char*)p;
-       event_data->event_param_data_4     = mail_ids_count;
-       event_data->event_param_data_5     = from_server;
-       event_data->multi_user_name        = EM_SAFE_STRDUP(multi_user_name);
+       if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
+               if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc for p failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
+
+               if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               event_data->type                   = EMAIL_EVENT_DELETE_MAIL;
+               event_data->account_id             = account_id;
+               event_data->event_param_data_3     = (char*)p;
+               event_data->event_param_data_4     = mail_ids_count;
+               event_data->event_param_data_5     = from_server;
+               event_data->event_param_data_6     = mailbox_id;
+               event_data->multi_user_name        = EM_SAFE_STRDUP(multi_user_name);
+
+               if (!emcore_insert_event(event_data, handle, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_insert_event failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+FINISH_OFF:
+
+               if (err != EMAIL_ERROR_NONE) {
+                       if (event_data) {
+                               emcore_free_event(event_data);
+                               EM_SAFE_FREE(event_data);
+                       }
+                       EM_SAFE_FREE(p);
+                       goto FINISH_OFF2;
+               }
+       }
+
+       thread_func_event_data = em_malloc(sizeof(email_event_t));
+       if (thread_func_event_data == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       p2 = em_malloc(sizeof(int) * mail_ids_count);
+       if (p2 == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       memcpy(p2, mail_ids, sizeof(int) * mail_ids_count);
 
-       THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
+       thread_func_event_data->type = EMAIL_EVENT_DELETE_MAIL;
+       thread_func_event_data->account_id = account_id;
+       thread_func_event_data->event_param_data_3 = (char *)p2;
+       thread_func_event_data->event_param_data_4 = mail_ids_count;
+       thread_func_event_data->event_param_data_5 = from_server;
+       thread_func_event_data->event_param_data_6 = mailbox_id;
+       thread_func_event_data->multi_user_name = g_strdup(multi_user_name);
+
+       THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void *)thread_func_event_data, thread_error);
        THREAD_DETACH(delete_thread); /* free resources used for new thread */
        ret = true;
 
-FINISH_OFF:
+FINISH_OFF2:
+
+       if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+               if (thread_func_event_data) {
+                   emcore_free_event(thread_func_event_data);
+                       EM_SAFE_FREE(thread_func_event_data);
+               }
+               EM_SAFE_FREE(p2);
+       }
 
        if (ref_account) {
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
        }
 
-       if (err != EMAIL_ERROR_NONE || thread_error != 0) {
-           emcore_free_event(event_data);
-               EM_SAFE_FREE(event_data);
-               EM_SAFE_FREE(p);
-       }
-
        if (err_code)
                *err_code = err;
 
@@ -737,18 +779,18 @@ int emdaemon_delete_mail_all(char *multi_user_name, int input_mailbox_id, int in
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
-                                                                                       mailbox_tbl->account_id, 
+       if (!emcore_delete_all_mails_of_mailbox(multi_user_name,
+                                                                                       mailbox_tbl->account_id,
                                                                                        input_mailbox_id,
                                                                                        0,
-                                                                                       EMAIL_DELETE_LOCALLY, 
+                                                                                       EMAIL_DELETE_LOCALLY,
                                                                                        &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
 
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
-       if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
+       if (input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
                event_data = em_malloc(sizeof(email_event_t));
                if (event_data == NULL) {
                        EM_DEBUG_EXCEPTION("em_malloc failed");
@@ -842,19 +884,16 @@ void* thread_func_to_move_mail(void *thread_argument)
 {
        EM_DEBUG_FUNC_BEGIN();
 
-       int ret = false;
        int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
-       int handle = 0;
        email_event_t *event_data = (email_event_t*)thread_argument;
        int dst_mailbox_id = 0;
        char *multi_user_name = NULL;
 
-       if(!event_data) { /*prevent 53096*/
+       if (!event_data) { /*prevent 53096*/
                EM_DEBUG_EXCEPTION("INVALID_PARMAETER");
                return NULL;
        }
 
-
 /*     dst_mailbox_name   = (char*)event_data->event_param_data_1; */ /*prevent 33693*/
        mail_ids           = (int*)event_data->event_param_data_3;
        mail_ids_count     = event_data->event_param_data_4;
@@ -863,24 +902,21 @@ void* thread_func_to_move_mail(void *thread_argument)
        noti_param_2       = event_data->event_param_data_7;
        multi_user_name    = event_data->multi_user_name;
 
-       if (!emcore_move_mail(multi_user_name, mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
+       if (!emcore_move_mail(multi_user_name,
+                                                       mail_ids,
+                                                       mail_ids_count,
+                                                       dst_mailbox_id,
+                                                       noti_param_1,
+                                                       noti_param_2,
+                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       ret = true;
-
 FINISH_OFF:
 
-       if (ret == false && event_data) {
-               emcore_free_event(event_data);
-               EM_SAFE_FREE(event_data);
-       }
+       emcore_free_event(event_data);
+       EM_SAFE_FREE(event_data);
 
        EM_DEBUG_FUNC_END();
        return SUCCESS;
@@ -895,28 +931,35 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_ma
        int select_num = 0;
        int *mails = NULL;
        int *p = NULL;
+       int *p2 = NULL;
        int i = 0;
        int num = 0;
+       int handle = 0;
        int thread_error = 0;
        email_account_t *ref_account = NULL;
        email_mail_list_item_t *mail_list = NULL;
        email_event_t *event_data = NULL;
+       email_event_t *thread_func_event_data = NULL;
        thread_t move_thread;
        emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
        emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
 
-       if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0)  {
+       if (dst_mailbox_id <= 0 || src_mailbox_id <= 0)  {
                EM_DEBUG_EXCEPTION("Invalid Parameter");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name,
+                                                                                       dst_mailbox_id,
+                                                                                       &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(multi_user_name, src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name,
+                                                                                       src_mailbox_id,
+                                                                                       &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -928,14 +971,26 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_ma
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_list(multi_user_name, 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)) {
+       if (!emstorage_get_mail_list(multi_user_name,
+                                                                       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)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
                goto FINISH_OFF;
        }
 
        mails = malloc(sizeof(int) * select_num);
-
-       if( !mails ) {
+       if (!mails) {
                EM_DEBUG_EXCEPTION("Malloc failed...!");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
@@ -943,14 +998,14 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_ma
 
        memset(mails, 0x00, sizeof(int) * select_num);
 
-       for(i = 0 ; i < select_num ; i++) {
-               if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
+       for (i = 0 ; i < select_num ; i++) {
+               if (mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING) {
                        mails[num] = mail_list[i].mail_id;
                        num++;
                }
        }
 
-       ifnum <= 0) {
+       if (num <= 0) {
                EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
                err = EMAIL_ERROR_MAIL_NOT_FOUND;
                goto FINISH_OFF;
@@ -972,12 +1027,28 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_ma
 
        event_data->account_id         = dst_mailbox_tbl->account_id;
        event_data->type               = EMAIL_EVENT_MOVE_MAIL;
-       event_data->event_param_data_3 = (char*)p;
+       event_data->event_param_data_3 = (char *)p;
        event_data->event_param_data_4 = num;
        event_data->event_param_data_5 = dst_mailbox_id;
        event_data->event_param_data_8 = src_mailbox_id;
        event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
+       if (!emcore_insert_event(event_data, &handle, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+    if (err != EMAIL_ERROR_NONE) {
+               if (event_data) {
+                       emcore_free_event(event_data);
+                       EM_SAFE_FREE(event_data);
+               }
+        EM_SAFE_FREE(p);
+               goto FINISH_OFF2;
+    }
+
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        int i = 0, activityid = 0;
 
@@ -998,12 +1069,46 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_ma
                        EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
        }
 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
-       THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
+
+       /* seperated the event because of timing issue */
+       thread_func_event_data = (email_event_t *)em_malloc(sizeof(email_event_t));
+       if (thread_func_event_data == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       p2 = em_malloc(sizeof(int) * num);
+       if (p2 == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc for p2 failed...");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       memcpy(p2, mails, sizeof(int) * num);
+
+       thread_func_event_data->type = EMAIL_EVENT_MOVE_MAIL;
+       thread_func_event_data->account_id = dst_mailbox_tbl->account_id;
+       thread_func_event_data->event_param_data_3 = (char *)p2;
+       thread_func_event_data->event_param_data_4 = num;
+       thread_func_event_data->event_param_data_5 = dst_mailbox_id;
+       thread_func_event_data->event_param_data_8 = src_mailbox_id;
+       thread_func_event_data->multi_user_name = g_strdup(multi_user_name);
+
+       THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)thread_func_event_data, thread_error);
        THREAD_DETACH(move_thread); /* free resources used for new thread */
        EM_DEBUG_LOG("thread_error [%d]", thread_error);
        ret = true;
 
-FINISH_OFF:
+FINISH_OFF2:
+
+       if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+               if (thread_func_event_data) {
+                       emcore_free_event(thread_func_event_data);
+                       EM_SAFE_FREE(thread_func_event_data);
+               }
+               EM_SAFE_FREE(p2);
+       }
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        EM_DEBUG_LOG("Setting g_local_activity_run ");
@@ -1024,18 +1129,12 @@ FINISH_OFF:
        if (src_mailbox_tbl)
                emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
 
-       if(mail_list)
+       if (mail_list)
                EM_SAFE_FREE(mail_list);
 
-       if(mails != NULL )
+       if (mails != NULL )
                EM_SAFE_FREE(mails);
 
-    if (err != EMAIL_ERROR_NONE || thread_error != 0) {
-        emcore_free_event(event_data);
-        EM_SAFE_FREE(event_data);
-        EM_SAFE_FREE(p);
-    }
-
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
@@ -1045,11 +1144,14 @@ INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
 
        /*  default variable */
-       int mail_id = 0, *p = NULL, thread_error = 0;
+       int handle = 0;
+       int mail_id = 0, thread_error = 0;
+       int *p = NULL, *p2 = NULL;
        int ret = false, err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        email_account_t* ref_account = NULL;
        email_event_t *event_data = NULL;
+       email_event_t *thread_func_event_data = NULL;
        thread_t move_thread;
        emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
        int src_mailbox_id = 0;
@@ -1100,8 +1202,8 @@ INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int
 
        memcpy(p, mail_ids, sizeof(int) * num);
 
-       event_data->account_id        = dest_mailbox_tbl->account_id;
        event_data->type               = EMAIL_EVENT_MOVE_MAIL;
+       event_data->account_id         = dest_mailbox_tbl->account_id;
        event_data->event_param_data_3 = (char*)p;
        event_data->event_param_data_4 = num;
        event_data->event_param_data_5 = dst_mailbox_id;
@@ -1110,6 +1212,22 @@ INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int
        event_data->event_param_data_7 = noti_param_2;
        event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
+       if (!emcore_insert_event(event_data, &handle, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       if (err != EMAIL_ERROR_NONE) {
+               if (event_data) {
+                       emcore_free_event(event_data);
+                       EM_SAFE_FREE(event_data);
+               }
+               EM_SAFE_FREE(p);
+               goto FINISH_OFF2;
+       }
+
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        int i = 0, activityid = 0;
 
@@ -1130,12 +1248,46 @@ INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int
                        EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
        }
 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
-       THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
+       thread_func_event_data = (email_event_t *)em_malloc(sizeof(email_event_t));
+       if (thread_func_event_data == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       p2 = em_malloc(sizeof(int) * num);
+       if (p2 == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF2;
+       }
+
+       memcpy(p2, mail_ids, sizeof(int) * num);
+
+       thread_func_event_data->type = EMAIL_EVENT_MOVE_MAIL;
+       thread_func_event_data->account_id = dest_mailbox_tbl->account_id;
+       thread_func_event_data->event_param_data_3 = (char*)p2;
+       thread_func_event_data->event_param_data_4 = num;
+       thread_func_event_data->event_param_data_5 = dst_mailbox_id;
+       thread_func_event_data->event_param_data_8 = src_mailbox_id;
+       thread_func_event_data->event_param_data_6 = noti_param_1;
+       thread_func_event_data->event_param_data_7 = noti_param_2;
+       thread_func_event_data->multi_user_name = g_strdup(multi_user_name);
+
+       THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)thread_func_event_data, thread_error);
        THREAD_DETACH(move_thread); /* free resources used for new thread */
        EM_DEBUG_LOG("thread_error [%d]", thread_error);
        ret = true;
 
-FINISH_OFF:
+FINISH_OFF2:
+
+    if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+               if (thread_func_event_data) {
+                       emcore_free_event(thread_func_event_data);
+                       EM_SAFE_FREE(thread_func_event_data);
+               }
+        EM_SAFE_FREE(p2);
+    }
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        EM_DEBUG_LOG("Setting g_local_activity_run ");
@@ -1153,21 +1305,15 @@ FINISH_OFF:
        if (dest_mailbox_tbl)
                emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
 
-    if (err != EMAIL_ERROR_NONE || thread_error != 0) {
-        emcore_free_event(event_data);
-        EM_SAFE_FREE(event_data);
-        EM_SAFE_FREE(p);
-    }
-
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id, int mail_ids[], 
-                                                                                       int num, email_flags_field_type field_type, int value, 
+INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id, int mail_ids[],
+                                                                                       int num, email_flags_field_type field_type, int value,
                                                                                        int onserver, int* err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], field_type [%d], value[%d], err_code[%p]", 
+       EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], field_type [%d], value[%d], err_code[%p]",
                                                mail_ids, num, field_type, value, err_code); /*prevent 27460*/
 
        int ret = false, err = EMAIL_ERROR_NONE;
@@ -1182,11 +1328,11 @@ INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(multi_user_name, 
-                                                                       account_id, 
-                                                                       EMAIL_ACC_GET_OPT_DEFAULT, 
-                                                                       &account_tbl, 
-                                                                       false, 
+       if (!emstorage_get_account_by_id(multi_user_name,
+                                                                       account_id,
+                                                                       EMAIL_ACC_GET_OPT_DEFAULT,
+                                                                       &account_tbl,
+                                                                       false,
                                                                        &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
                goto FINISH_OFF;
@@ -1248,13 +1394,13 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_update_mail(char *multi_user_name, email_mail_data_t *input_mail_data, 
-                                                                       email_attachment_data_t *input_attachment_data_list, int input_attachment_count, 
+INTERNAL_FUNC int emdaemon_update_mail(char *multi_user_name, 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)
 {
        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_from_eas[%d]", input_mail_data, input_attachment_data_list,
                                                input_attachment_count, input_meeting_request, input_from_eas);
 
        int err = EMAIL_ERROR_NONE;
@@ -1514,19 +1660,19 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_
        int *mail_id_list = NULL, result_count = 0, i;
        email_mail_list_item_t *mail_list = NULL;
 
-       if (!emstorage_get_mail_list(multi_user_name, 
-                                                                       0, 
-                                                                       0, 
-                                                                       NULL, 
-                                                                       thread_id, 
-                                                                       -1, 
-                                                                       -1, 
-                                                                       0, 
-                                                                       NULL, 
-                                                                       EMAIL_SORT_MAILBOX_ID_HIGH, 
-                                                                       true, 
-                                                                       &mail_list, 
-                                                                       &result_count, 
+       if (!emstorage_get_mail_list(multi_user_name,
+                                                                       0,
+                                                                       0,
+                                                                       NULL,
+                                                                       thread_id,
+                                                                       -1,
+                                                                       -1,
+                                                                       0,
+                                                                       NULL,
+                                                                       EMAIL_SORT_MAILBOX_ID_HIGH,
+                                                                       true,
+                                                                       &mail_list,
+                                                                       &result_count,
                                                                        &err) || !mail_list || !result_count) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
                goto FINISH_OFF;
@@ -1547,7 +1693,14 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_
 
        // should remove requiring of mailbox information from this function.
        // email-service should find mailboxes itself by its mail id.
-       if (!emdaemon_delete_mail(multi_user_name, account_id, mail_id_list, result_count, false, handle, &err)) {
+       if (!emdaemon_delete_mail(multi_user_name,
+                                                               account_id,
+                                                               0,
+                                                               mail_id_list,
+                                                               result_count,
+                                                               EMAIL_DELETE_LOCALLY,
+                                                               handle,
+                                                               &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1558,6 +1711,7 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_
        ret = true;
 
 FINISH_OFF:
+
        EM_SAFE_FREE(mail_list);
        EM_SAFE_FREE(mail_id_list);
 
@@ -1645,7 +1799,7 @@ INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name,
        }
 
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
-       if(input_on_server) {
+       if (input_on_server) {
                event_data = em_malloc(sizeof(email_event_t));
                if (event_data == NULL) {
                        EM_DEBUG_EXCEPTION("em_malloc failed");
@@ -1666,11 +1820,13 @@ INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name,
        }
        else
 #endif
-               if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
+       {
+               if ((err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name,
+                                                                                                                                                       input_mailbox_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
                        goto FINISH_OFF;
                }
-
+       }
 FINISH_OFF:
 
        if (event_insert == false && event_data) {
index 865efc7..37d5034 100755 (executable)
@@ -274,6 +274,7 @@ INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int accoun
  * Delete a mail or multiple mails.
  *
  * @param[in] account_id               Specifies the account.
+ * @param[in] mailbox_id        Specifies the mailbox id.
  * @param[in] mail_id                  Specifies the arrary of mail id.
  * @param[in] num                              Specifies the number of mail id.
  * @param[in] from_server          Specifies whether mails are deleted from server.
@@ -284,7 +285,14 @@ INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int accoun
  * @return This function returns true on success or false on failure.
  */
 
-INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int account_id, int mail_id[], int num, int from_server,  int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, 
+                                                                               int account_id, 
+                                                                               int mailbox_id,
+                                                                               int mail_id[], 
+                                                                               int num, 
+                                                                               int from_server, 
+                                                                               int *handle, 
+                                                                               int* err_code);
 
 /**
  * Delete all mail from a mailbox.
index 50101a0..f3b07af 100755 (executable)
@@ -1053,7 +1053,14 @@ void stb_delete_mail(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
-       emdaemon_delete_mail(multi_user_name, mailbox_tbl->account_id, mail_ids, num, from_server, NULL, &err);
+       emdaemon_delete_mail(multi_user_name, 
+                                                       mailbox_tbl->account_id, 
+                                                       mailbox_id, 
+                                                       mail_ids, 
+                                                       num, 
+                                                       from_server, 
+                                                       NULL, 
+                                                       &err);
 
 FINISH_OFF:
 
@@ -1975,14 +1982,17 @@ void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
        EM_DEBUG_LOG("on_server [%d]", on_server);
 
-       if( (err = emdaemon_expunge_mails_deleted_flagged(multi_user_name, mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
+       if ((err = emdaemon_expunge_mails_deleted_flagged(multi_user_name, 
+                                                                                                               mailbox_id, 
+                                                                                                               on_server, 
+                                                                                                               &handle)) != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
+       if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
                EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
        }
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
+       if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
                EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
        }
 
@@ -2638,6 +2648,7 @@ void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
        /* Remove the searched mails */
        if (!emdaemon_delete_mail(multi_user_name, 
                                                                account_id, 
+                                                               0,
                                                                mail_id_list, 
                                                                mail_id_count, 
                                                                EMAIL_DELETE_LOCALLY, 
@@ -3734,8 +3745,8 @@ void stb_API_mapper(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
-       int nAPIID = emipc_get_api_id(a_hAPI);
-       int client_fd = emipc_get_response_id(a_hAPI);
+       unsigned int nAPIID = emipc_get_api_id(a_hAPI);
+       unsigned int client_fd = emipc_get_response_id(a_hAPI);
 
        err = emcore_check_privilege(client_fd);
        if (err != EMAIL_ERROR_NONE) {
index 165de25..57f480f 100755 (executable)
@@ -47,7 +47,7 @@ EXPORT_API HIPC_API emipc_create_email_api(long api_id)
 
        emipc_email_api_info *api_info = (emipc_email_api_info *)calloc(1, sizeof(emipc_email_api_info));
        if(api_info == NULL) {
-               EM_DEBUG_EXCEPTION("Malloc failed");
+               EM_DEBUG_EXCEPTION("calloc failed");
                return NULL;
        }
 
@@ -67,27 +67,28 @@ EXPORT_API void emipc_destroy_email_api(HIPC_API api)
        EM_SAFE_FREE(api_info);
 }
 
-EXPORT_API long emipc_get_api_id(HIPC_API api)
+EXPORT_API unsigned int emipc_get_api_id(HIPC_API api)
 {
-       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_FUNC_BEGIN("API = %p", api);
        emipc_email_api_info *api_info = (emipc_email_api_info *)api;
        EM_DEBUG_FUNC_END("api_id [%d]", api_info->api_id);
        return api_info->api_id;
 }
 
-EXPORT_API long emipc_get_app_id(HIPC_API api)
+EXPORT_API unsigned int emipc_get_response_id(HIPC_API api)
 {
-       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_FUNC_BEGIN("API = %p", api);
        emipc_email_api_info *api_info = (emipc_email_api_info *)api;
-       EM_DEBUG_FUNC_END("app_id [%d]", api_info->app_id);
-       return api_info->app_id;
+       EM_DEBUG_FUNC_END("response_id [%d]", api_info->response_id);
+       return api_info->response_id;
 }
 
-EXPORT_API long emipc_get_response_id(HIPC_API api)
+EXPORT_API int emipc_get_app_id(HIPC_API api)
 {
-       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_FUNC_BEGIN("API = %p", api);
        emipc_email_api_info *api_info = (emipc_email_api_info *)api;
-       return api_info->response_id;
+       EM_DEBUG_FUNC_END("app_id [%d]", api_info->app_id);
+       return api_info->app_id;
 }
 
 /* note: there incurs additional cost (malloc & memcpy). */
index de8e88f..5d4a52e 100755 (executable)
@@ -44,16 +44,16 @@ EXPORT_API bool emipc_deserialize_api_info(emipc_email_api_info *api_info, EPARA
        if (api_info->params[direction] == NULL) {
                api_info->params[direction] = emipc_create_param_list();
                if (api_info->params[direction] == NULL) {
-                       EM_DEBUG_EXCEPTION("Malloc failed");
+                       EM_DEBUG_EXCEPTION("emipc_create_param_list failed");
                        return false;
                }
        }
 
-       api_info->api_id = *((long *)stream + eSTREAM_APIID);
-       api_info->app_id = *((long *)stream + eSTREAM_APPID);
-       api_info->response_id = *((long *)stream + eSTREAM_RESID);
+       memcpy(&(api_info->api_id), stream + (sizeof(int) * eSTREAM_APIID), sizeof(api_info->api_id));
+       memcpy(&(api_info->response_id), stream + (sizeof(int) * eSTREAM_RESID), sizeof(api_info->response_id));
+       memcpy(&(api_info->app_id), stream + (sizeof(int) * eSTREAM_APPID), sizeof(api_info->api_id));
 
-       return emipc_parse_stream_of_param_list(api_info->params[direction], stream);
+       return emipc_parse_stream_of_param_list(stream, api_info->params[direction]);
 }
 
 EXPORT_API unsigned char *emipc_serialize_api_info(emipc_email_api_info *api_info, EPARAMETER_DIRECTION direction, int *stream_len)
@@ -69,17 +69,18 @@ EXPORT_API unsigned char *emipc_serialize_api_info(emipc_email_api_info *api_inf
        if (api_info->params[direction] == NULL) {
                api_info->params[direction] = emipc_create_param_list();
                if (api_info->params[direction] == NULL) {
-                       EM_DEBUG_EXCEPTION("Malloc failed");
+                       EM_DEBUG_EXCEPTION("emipc_create_param_list failed");
                        return NULL;
                }
        }
 
        stream = emipc_serialize_param_list(api_info->params[direction], stream_len);
        if (stream != NULL) {
-               memcpy(stream, &(api_info->api_id), sizeof(api_info->api_id));
-               memcpy(stream+(sizeof(long)*eSTREAM_RESID), &(api_info->response_id), sizeof(api_info->response_id));
-               memcpy(stream+(sizeof(long)*eSTREAM_APPID), &(api_info->app_id), sizeof(api_info->app_id));
+               memcpy(stream + (sizeof(int) * eSTREAM_APIID), &(api_info->api_id), sizeof(api_info->api_id));
+               memcpy(stream + (sizeof(int) * eSTREAM_RESID), &(api_info->response_id), sizeof(api_info->response_id));
+               memcpy(stream + (sizeof(int) * eSTREAM_APPID), &(api_info->app_id), sizeof(api_info->app_id));
        }
+
        EM_DEBUG_FUNC_END();
        return stream;
 }
index bef1353..70b3c98 100755 (executable)
@@ -40,7 +40,7 @@ EXPORT_API emipc_param_list *emipc_create_param_list()
 {
        emipc_param_list *new_param_list = NULL;
 
-       new_param_list = (emipc_param_list *) em_malloc (sizeof(emipc_param_list));
+       new_param_list = (emipc_param_list *)em_malloc(sizeof(emipc_param_list));
        if (new_param_list == NULL) {
                EM_DEBUG_EXCEPTION("em_malloc failed.");
                return NULL;
@@ -66,11 +66,13 @@ EXPORT_API void emipc_destroy_param_list(emipc_param_list *param_list)
 }
 
 /* making stream into param length and param data */
-EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, void *stream)
+EXPORT_API bool emipc_parse_stream_of_param_list(void *stream, emipc_param_list *param_list)
 {
        EM_DEBUG_FUNC_BEGIN();
-       long parameter_count = *((long *)stream + eSTREAM_COUNT);
+       int parameter_count = 0;
 
+       /* Get the parameter count */
+       memcpy(&parameter_count, stream + (sizeof(int) * eSTREAM_COUNT), sizeof(int));
        if (parameter_count < 0) {
                EM_DEBUG_LOG("INVALID_PARAM : count %d", parameter_count);
                return false;
@@ -82,10 +84,10 @@ EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, v
        }
 
        int stream_len = malloc_usable_size(stream);
-       int remain_len = stream_len - (sizeof(long) * eSTREAM_DATA);
+       int remain_len = stream_len - (sizeof(int) * eSTREAM_DATA);
        EM_DEBUG_LOG_DEV("Allocated stream size : %dbyte", stream_len);
 
-       unsigned char* cur = ((unsigned char *)stream) + (sizeof(long) * eSTREAM_DATA);
+       unsigned char* cur = ((unsigned char *)stream) + (sizeof(int) * eSTREAM_DATA);
 
        int i = 0;
        /* stream is composed of data type which is encoded into length and data field */
@@ -133,17 +135,19 @@ EXPORT_API unsigned char *emipc_serialize_param_list(emipc_param_list *param_lis
 
        EM_SAFE_FREE (param_list->byte_stream);
 
-       int stream_len = emipc_sum_param_list_length (param_list);
-
+       int stream_len = emipc_sum_param_list_length(param_list);
        if (stream_len <= 0) {
                EM_DEBUG_EXCEPTION("stream_len error %d", stream_len);
-               EM_SAFE_FREE(param_list->byte_stream);
                goto FINISH_OFF;
        }
 
        param_list->byte_stream = (unsigned char*)calloc(1, stream_len);
-       int pos = sizeof(long)*eSTREAM_COUNT;
+       if (param_list->byte_stream == NULL) {
+               EM_DEBUG_EXCEPTION("calloc failed : [%d]", errno);
+               goto FINISH_OFF;
+       }
 
+       int pos = sizeof(int) * eSTREAM_COUNT;
        if (pos + (int)sizeof(param_list->param_count) > stream_len ) {
                EM_DEBUG_EXCEPTION("%d > stream_len", pos + sizeof(param_list->param_count));
                EM_SAFE_FREE(param_list->byte_stream);
@@ -153,7 +157,7 @@ EXPORT_API unsigned char *emipc_serialize_param_list(emipc_param_list *param_lis
        memcpy((param_list->byte_stream + pos), &param_list->param_count, sizeof(param_list->param_count));
 
        /* Add param count */
-       pos += sizeof(long);
+       pos += sizeof(int);
        int index = 0, length = 0;
 
        /* stream format */
@@ -197,7 +201,7 @@ FINISH_OFF:
 
 EXPORT_API int emipc_sum_param_list_length(emipc_param_list *param_list)
 {
-       int length = sizeof(long) * eSTREAM_DATA;
+       int length = sizeof(int) * eSTREAM_DATA;
        int index;
        for (index = 0; index < param_list->param_count; index++) {
                length += sizeof(int);
index 1ff6cc3..2f98f42 100755 (executable)
@@ -29,9 +29,9 @@
 #include "email-ipc-param-list.h"
 
 typedef struct {
-       long api_id;
-       long response_id;
-       long app_id;
+       unsigned int api_id;
+       unsigned int response_id;
+       int app_id;
        emipc_param_list *params[2];
 } emipc_email_api_info;
 
index dab2e0b..54a65cd 100755 (executable)
@@ -36,7 +36,7 @@ typedef enum {
 }IPC_STREAM_INFO;
 
 typedef struct {
-       long param_count;
+       int param_count;
        emipc_param params[10];
        unsigned char *byte_stream;
 } emipc_param_list;
@@ -45,7 +45,7 @@ EXPORT_API emipc_param_list *emipc_create_param_list();
 
 EXPORT_API void emipc_destroy_param_list(emipc_param_list *param_list);
 
-EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, void *stream_data);
+EXPORT_API bool emipc_parse_stream_of_param_list(void *stream_data, emipc_param_list *param_list);
 
 EXPORT_API unsigned char *emipc_serialize_param_list(emipc_param_list *param_list, int *stream_length);
 
index 5aadb73..cae9d30 100755 (executable)
@@ -186,7 +186,7 @@ EXPORT_API void emipc_wait_for_ipc_request()
                                                EM_DEBUG_LOG("[IPCLib]Stub Socket Recv [Socket ID = %d], [recv_len = %d]", event_fd, recv_len);
 
                                                /* IPC request stream is at least 16byte */
-                                               if (recv_len >= sizeof(long) * eSTREAM_DATA) {
+                                               if (recv_len >= sizeof(int) * eSTREAM_DATA) {
                                                        int ret = 0;
                                                        ret = emipc_create_task((unsigned char *)sz_buf, event_fd);
                                                        if (ret == EMAIL_ERROR_PERMISSION_DENIED) {
index 0a574b9..4abf15c 100755 (executable)
@@ -71,9 +71,9 @@ EXPORT_API bool emipc_execute_stub_api(HIPC_API input_api_handle);
 EXPORT_API HIPC_API emipc_create_email_api(long api_id);
 EXPORT_API void emipc_destroy_email_api(HIPC_API input_api_handle);
 
-EXPORT_API long emipc_get_api_id(HIPC_API input_api_handle);
-EXPORT_API long emipc_get_app_id(HIPC_API input_api_handle);
-EXPORT_API long emipc_get_response_id(HIPC_API input_api_handle);
+EXPORT_API unsigned int emipc_get_api_id(HIPC_API input_api_handle);
+EXPORT_API unsigned int emipc_get_response_id(HIPC_API input_api_handle);
+EXPORT_API int emipc_get_app_id(HIPC_API input_api_handle);
 
 EXPORT_API bool emipc_add_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length);
 EXPORT_API bool emipc_add_dynamic_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length);