Merged the tizen 2.4 47/48447/1
authorSunghyun Kwon <sh0701.kwon@samsung.com>
Mon, 21 Sep 2015 12:45:03 +0000 (21:45 +0900)
committerSunghyun Kwon <sh0701.kwon@samsung.com>
Mon, 21 Sep 2015 12:45:03 +0000 (21:45 +0900)
Change-Id: I3fec60f2d5942aee01d1996a664dc3318b756306

35 files changed:
email-api/email-api-account.c
email-api/email-api-mailbox.c
email-api/email-api-network.c
email-api/email-api-smime.c
email-common-use/email-convert.c
email-common-use/email-utilities.c
email-common-use/include/email-types.h
email-core/email-core-account.c
email-core/email-core-auto-download.c
email-core/email-core-cert.c
email-core/email-core-event.c
email-core/email-core-gmime.c
email-core/email-core-mail.c
email-core/email-core-mailbox-sync.c
email-core/email-core-mailbox.c
email-core/email-core-signal.c
email-core/email-core-smime.c
email-core/email-core-smtp.c
email-core/email-core-task-manager.c
email-core/email-core-tasks.c
email-core/email-core-utils.c
email-core/email-network/email-network.c
email-core/email-storage/email-storage.c
email-core/email-storage/include/email-storage.h
email-core/include/email-core-mail.h
email-core/include/email-core-mailbox-sync.h
email-daemon/email-daemon-account.c
email-daemon/email-daemon-event.c
email-daemon/email-daemon-mail.c
email-daemon/email-daemon-mailbox.c
email-daemon/include/email-daemon.h
email-daemon/main.c
email-ipc/email-ipc-api.c
email-ipc/email-ipc-api/email-ipc-param-list.c
utilities/test-application/testapp-mail.c

index c2e7336..648e17f 100755 (executable)
@@ -469,7 +469,8 @@ EXPORT_API int email_validate_account_ex(email_account_t* account, int *handle)
        int size = 0;
        int err = EMAIL_ERROR_NONE;
 
-       if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL||
+       if (account == NULL || account->user_email_address == NULL || 
+               account->incoming_server_user_name == NULL || account->incoming_server_address == NULL||
                account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                return EMAIL_ERROR_INVALID_PARAM;
index a463b25..e9c8681 100755 (executable)
@@ -454,9 +454,15 @@ FINISH_OFF:
        return err;
 }
 
-EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int *output_handle)
+EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, 
+                                                                               int input_mailbox_id_count, int input_on_server, 
+                                                                               int *output_handle)
 {
-       EM_DEBUG_API_BEGIN ("input_account_id[%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id_array, input_mailbox_id_array, input_mailbox_id_count, input_on_server, output_handle);
+       EM_DEBUG_API_BEGIN ("input_account_id[%d] input_mailbox_id_array[%p] "
+                                               "input_mailbox_id_count[%d] input_on_server[%d] "
+                                               "output_handle[%p]", input_mailbox_id_array, input_mailbox_id_array, 
+                                               input_mailbox_id_count, input_on_server, output_handle);
+
        int err = EMAIL_ERROR_NONE;
     char *multi_user_name = NULL;
        email_account_server_t account_server_type;
@@ -474,7 +480,11 @@ EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_
     }
 
        /*  check account bind type and branch off  */
-       if (em_get_account_server_type_by_account_id(multi_user_name, input_account_id, &account_server_type, false, &err) == false) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, 
+                                                                                                       input_account_id, 
+                                                                                                       &account_server_type, 
+                                                                                                       false, 
+                                                                                                       &err) == false) {
                EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
                err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
                goto FINISH_OFF;
@@ -513,7 +523,8 @@ EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_
                task_parameter.mailbox_id_count  = input_mailbox_id_count;
                task_parameter.on_server         = input_on_server;
 
-               if ((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, &task_parameter)) != EMAIL_ERROR_NONE) {
+               if ((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, 
+                                                                                       &task_parameter)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
                        goto FINISH_OFF;
                }
index 825d872..08ef234 100755 (executable)
@@ -453,7 +453,6 @@ FINISH_OFF:
 EXPORT_API int email_sync_header_for_all_account(int *handle)
 {
        EM_DEBUG_API_BEGIN ("handle[%p]", handle);
-       char* mailbox_stream = NULL;
     char *multi_user_name = NULL;
        int err = EMAIL_ERROR_NONE;     
        HIPC_API hAPI = NULL;
@@ -490,7 +489,7 @@ EXPORT_API int email_sync_header_for_all_account(int *handle)
 
        if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
-               EM_SAFE_FREE(mailbox_stream);
+               EM_SAFE_FREE(multi_user_name);
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
        }
                
@@ -673,7 +672,6 @@ FINISH_OFF:
 EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
 {
        EM_DEBUG_API_BEGIN ("mail_id[%d] nth[%d] handle[%p]", mail_id, nth, handle);
-       char* mailbox_stream = NULL;
        int err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        int account_id = 0;
@@ -760,7 +758,6 @@ EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
                /* Execute API */
                if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
-                       EM_SAFE_FREE(mailbox_stream);
                        EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
                }
 
@@ -1037,9 +1034,16 @@ EXPORT_API int email_sync_imap_mailbox_list(int account_id, int *handle)
        return err;
 }
 
-EXPORT_API int email_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter_list, int input_search_filter_count, int *output_handle)
+EXPORT_API int email_search_mail_on_server(int input_account_id, 
+                                                                               int input_mailbox_id, 
+                                                                               email_search_filter_t *input_search_filter_list, 
+                                                                               int input_search_filter_count, 
+                                                                               int *output_handle)
 {
-       EM_DEBUG_API_BEGIN ("input_account_id[%d] input_mailbox_id[%d] input_search_filter_list[%p] input_search_filter_count[%d] output_handle[%p]", input_account_id, input_mailbox_id, input_search_filter_list, input_search_filter_count, output_handle);
+       EM_DEBUG_API_BEGIN ("input_account_id[%d] input_mailbox_id[%d] input_search_filter_list[%p] "
+                                               "input_search_filter_count[%d] output_handle[%p]", 
+                                               input_account_id, input_mailbox_id, input_search_filter_list, 
+                                               input_search_filter_count, output_handle);
 
        int       err = EMAIL_ERROR_NONE;
        int       return_value = 0;
@@ -1061,15 +1065,19 @@ EXPORT_API int email_search_mail_on_server(int input_account_id, int input_mailb
 
        memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
 
-       if ( em_get_account_server_type_by_account_id(multi_user_name, input_account_id, &account_server_type, true, &err) == false ) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, 
+                                                                                               input_account_id, 
+                                                                                               &account_server_type, 
+                                                                                               true, 
+                                                                                               &err) == false) {
                EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
+       if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                int as_handle = 0;
 
-               if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
+               if (em_get_handle_for_activesync(&as_handle, &err) == false) {
                        EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
                        goto FINISH_OFF;
                }
@@ -1084,13 +1092,13 @@ EXPORT_API int email_search_mail_on_server(int input_account_id, int input_mailb
 
                return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER, &as_noti_data);
 
-               if ( return_value == false ) {
+               if (return_value == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
                        err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
                        goto FINISH_OFF;
                }
 
-               if(output_handle)
+               if (output_handle)
                        *output_handle = as_handle;
        }
        else
@@ -1099,29 +1107,33 @@ EXPORT_API int email_search_mail_on_server(int input_account_id, int input_mailb
 
                EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
 
-               if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
+               if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
                        err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
                        goto FINISH_OFF;
                }
 
-               if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_mailbox_id, sizeof(int))){
+               if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_mailbox_id, sizeof(int))){
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
                        err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
                        goto FINISH_OFF;
                }
 
-               stream_for_search_filter_list = em_convert_search_filter_to_byte_stream(input_search_filter_list, input_search_filter_count, &stream_size_for_search_filter_list);
+               stream_for_search_filter_list = em_convert_search_filter_to_byte_stream(input_search_filter_list, 
+                                                                                                                                                               input_search_filter_count, 
+                                                                                                                                                               &stream_size_for_search_filter_list);
 
                EM_PROXY_IF_NULL_RETURN_VALUE(stream_for_search_filter_list, hAPI, EMAIL_ERROR_NULL_VALUE);
 
-               if(!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, stream_for_search_filter_list, stream_size_for_search_filter_list)) { /*prevent 18950*/
+               if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, 
+                                                                               stream_for_search_filter_list, 
+                                                                               stream_size_for_search_filter_list)) { /*prevent 18950*/
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
                        err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
                        goto FINISH_OFF;
                }
 
-               if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
+               if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
                        EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
                }
@@ -1168,15 +1180,19 @@ EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
 
        memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
 
-       if ( em_get_account_server_type_by_account_id(multi_user_name, input_account_id, &account_server_type, true, &err) == false ) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, 
+                                                                                               input_account_id, 
+                                                                                               &account_server_type, 
+                                                                                               true, 
+                                                                                               &err) == false) {
                EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
+       if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                int as_handle = 0;
 
-               if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
+               if (em_get_handle_for_activesync(&as_handle, &err) == false) {
                        EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
                        goto FINISH_OFF;
                }
@@ -1186,9 +1202,9 @@ EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
                as_noti_data.clear_result_of_search_mail_on_server.account_id          = input_account_id;
                as_noti_data.clear_result_of_search_mail_on_server.multi_user_name     = multi_user_name;
 
-               return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER, &as_noti_data);
-
-               if ( return_value == false ) {
+               return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER, 
+                                                                                                                                       &as_noti_data);
+               if (return_value == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
                        err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
                        goto FINISH_OFF;
@@ -1219,7 +1235,7 @@ EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
        }
 
 FINISH_OFF:
-       if(hAPI) {
+       if (hAPI) {
                emipc_destroy_email_api(hAPI);
                hAPI = NULL;
        }
index 57bdaa1..2072bd5 100755 (executable)
@@ -340,11 +340,11 @@ EXPORT_API int email_verify_signature_ex(email_mail_data_t *input_mail_data, ema
        }
 
        if (input_mail_data->smime_type == EMAIL_SMIME_SIGNED) {
-                emcore_init_openssl_library();
+               emcore_init_openssl_library();
                if (!emcore_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, verify, &err)) 
                        EM_DEBUG_EXCEPTION("emcore_verify_signature failed : [%d]", err);
 
-                emcore_clean_openssl_library();
+               emcore_clean_openssl_library();
        } else if(input_mail_data->smime_type == EMAIL_PGP_SIGNED) {
                if ((err = emcore_pgp_get_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, input_mail_data->digest_type, verify)) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_siganture failed : [%d]", err);
index 3dc30b5..35cc901 100755 (executable)
@@ -748,8 +748,8 @@ static char *convert_format(char *fmt)
         c++;
     }
 
-       EM_DEBUG_LOG("original fmt : [%s]", fmt);
-       EM_DEBUG_LOG("converted_fmt : [%s]", converted_fmt);
+       EM_DEBUG_LOG_DEV("original fmt  : [%s]", fmt);
+       EM_DEBUG_LOG_DEV("converted_fmt : [%s]", converted_fmt);
 
        ret = true;
 
@@ -1410,6 +1410,8 @@ INTERNAL_FUNC char* em_convert_search_filter_to_byte_stream(email_search_filter_
                        case EMAIL_SEARCH_FILTER_TYPE_TO               :
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
                        case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
+                       case EMAIL_SEARCH_FILTER_TYPE_CHARSET          :
+                       case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     :
                                result_stream = append_string_to_stream(result_stream, &stream_size, input_search_filter_list[i].search_filter_key_value.string_type_key_value);
                                break;
 
@@ -1490,6 +1492,8 @@ INTERNAL_FUNC void em_convert_byte_stream_to_search_filter(char *input_stream,
                        case EMAIL_SEARCH_FILTER_TYPE_TO               :
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
                        case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
+                       case EMAIL_SEARCH_FILTER_TYPE_CHARSET          :
+                       case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     :
                                fetch_string_from_stream(input_stream, &stream_offset, &(local_search_filter[i].search_filter_key_value.string_type_key_value));
                                break;
 
index 9c86fb5..ea2f78b 100755 (executable)
@@ -52,10 +52,14 @@ INTERNAL_FUNC void* em_malloc(int len)
                return NULL;
        }
 
-       void *p = calloc(1,len);
-       if (!p)
+       void *p = NULL;
+       p = malloc(len);
+       if (!p) {
                EM_DEBUG_PERROR("malloc failed");
+               return NULL;
+       }
 
+       memset(p, 0x00, len);
        return p;
 }
 
index 3add43d..e379c48 100755 (executable)
@@ -1112,6 +1112,8 @@ typedef enum {
     EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       = 43,  /* string type */     /**< Messages with specified message ID */
     EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  = 50,  /* integer type */    /**< Messages that have a header with the specified priority */
     EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  = 60,  /* string type */     /**< Messages that contain the specified string in attachment name */
+       EMAIL_SEARCH_FILTER_TYPE_CHARSET          = 61,  /* string type */     /**< Messages of encoded type */
+       EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     = 62,  /* string type */     /**< Messages that extend and user defined string */ 
 } email_search_filter_type;
 
 typedef enum {
index 4e2debc..3875206 100755 (executable)
@@ -457,7 +457,8 @@ INTERNAL_FUNC int emcore_delete_account(char *multi_user_name, int account_id, i
                        error_code_from_account_svc = account_delete_from_db_by_id(account_to_be_deleted->account_svc_id);
                        EM_DEBUG_LOG("account_delete_from_db_by_id returns [%d]", error_code_from_account_svc);
 
-                       emcore_unset_join_zone(join_zone);
+                       if (join_zone)
+                               emcore_unset_join_zone(join_zone);
                }
 
                if (account_to_be_deleted) {
@@ -622,7 +623,7 @@ INTERNAL_FUNC int emcore_create_account(char *multi_user_name, email_account_t *
                        if(error_code != ACCOUNT_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
                                err = error_code;
-                               emcore_unset_join_zone(join_zone);
+                               if (join_zone) emcore_unset_join_zone(join_zone);
                                goto FINISH_OFF;
                        }
 
@@ -681,7 +682,7 @@ INTERNAL_FUNC int emcore_create_account(char *multi_user_name, email_account_t *
                        if (account_handle)
                                account_destroy(account_handle);
 
-                       emcore_unset_join_zone(join_zone);
+                       if (join_zone) emcore_unset_join_zone(join_zone);
                }
        }
 
index 7fa8a08..66f7a3e 100755 (executable)
@@ -568,7 +568,12 @@ CHECK_CONTINUE:
                        }
 
                        snprintf(uid_str, sizeof(uid_str), "%ld", event_data->server_mail_id);
-                       if (!emstorage_get_maildata_by_servermailid(event_data->multi_user_name, event_data->mailbox_id, uid_str, &mail, true, &err) || !mail) {
+                       if (!emstorage_get_maildata_by_servermailid(event_data->multi_user_name, 
+                                                                                                               uid_str, 
+                                                                                                               event_data->mailbox_id,
+                                                                                                               &mail, 
+                                                                                                               true, 
+                                                                                                               &err) || !mail) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
                        }
                        else {
@@ -712,8 +717,10 @@ INTERNAL_FUNC int emcore_insert_auto_download_job(char *multi_user_name, int acc
 FINISH_OFF:
 
        if (!event_pushed) {
-               EM_SAFE_FREE(ad_event->multi_user_name);
-               EM_SAFE_FREE(ad_event);
+               if (ad_event) {
+                       EM_SAFE_FREE(ad_event->multi_user_name);
+                       free(ad_event);
+               }
        }
 
        EM_DEBUG_FUNC_END();
index 8f44291..0adb2af 100755 (executable)
@@ -89,7 +89,7 @@ INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X5
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_EXCEPTION("emcore_load_PFX_file - certificate passed : %s", certificate);
+       EM_DEBUG_LOG("emcore_load_PFX_file - certificate passed : %s", certificate);
 
        /* Create instance */
        err = certsvc_instance_new(&cert_instance);
index 8192aca..de6a63f 100755 (executable)
@@ -596,12 +596,12 @@ INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data)
                new_event->event_param_data_3 = EM_SAFE_STRDUP(sync_failed_event_data->event_param_data_3);
        new_event->event_param_data_4 = sync_failed_event_data->event_param_data_4;
        new_event->event_param_data_5 = sync_failed_event_data->event_param_data_5;
+       new_event->multi_user_name = EM_SAFE_STRDUP(sync_failed_event_data->multi_user_name);
 
        *event_data = new_event;
 
        emcore_free_event(sync_failed_event_data);
        EM_SAFE_FREE(sync_failed_event_data);
-       sync_failed_event_data = NULL;
 }
 
 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
@@ -1501,6 +1501,8 @@ INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
                                                case EMAIL_SEARCH_FILTER_TYPE_TO               :
                                                case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
                                                case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
+                                               case EMAIL_SEARCH_FILTER_TYPE_CHARSET          :
+                                               case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     :
                                                        EM_SAFE_FREE(search_filter[i].search_filter_key_value.string_type_key_value);
                                                        break;
                                                default:
index 14ee7d6..ebf2f97 100755 (executable)
@@ -1438,14 +1438,16 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMime
                /*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, &result) == EMAIL_ERROR_NONE && result)) {
+                       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 {
-                               content_disposition_type = ATTACHMENT;
-                               EM_DEBUG_LOG("ATTACHMENT");
-                        }
+                       } else {
+                               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;
@@ -1956,8 +1958,10 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent,
                                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)) {
+                       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) {
@@ -1974,8 +1978,9 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent,
                                        }
                                }
                        } else {
-                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
-                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                               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;
index 15eb117..aae0cd4 100755 (executable)
@@ -3657,9 +3657,15 @@ 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, int input_from_server, int *err_code)
+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 *err_code)
 {
-       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);
+       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;
        int   err = EMAIL_ERROR_NONE;
@@ -3667,22 +3673,45 @@ INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name, int
        int   mail_id_count = 0;
        char  conditional_clause[QUERY_SIZE] = { 0, };
 
-       if (!input_mailbox_id) {
+       if (input_mailbox_id <= 0 && input_mailbox_type <= 0) {
                err = EMAIL_ERROR_INVALID_PARAM;
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                goto FINISH_OFF;
        }
 
        /* Delete all mails in specific mailbox */
+       SNPRINTF(conditional_clause, QUERY_SIZE, " where ");
+
+       if (input_mailbox_id <= 0 && input_mailbox_type > 0) {
+               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE, 
+                                       "mailbox_type = %d ", input_mailbox_type);
+       } else if (input_mailbox_id > 0 && input_mailbox_type <= 0) {
+               SNPRINTF(conditional_clause + strlen(conditional_clause), QUERY_SIZE, 
+                                       "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, QUERY_SIZE, " where mailbox_id = %d ", input_mailbox_id);
+       EM_DEBUG_LOG("command : [%s]", conditional_clause);
 
-       emstorage_query_mail_id_list(multi_user_name, conditional_clause, false, &mail_id_array, &mail_id_count);
+       err = emstorage_query_mail_id_list(multi_user_name, conditional_clause, false, &mail_id_array, &mail_id_count);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list failed : [%d]", err);
+               goto FINISH_OFF;
+       }
 
        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, &err)) {
+               if (!emcore_delete_mail(multi_user_name, 
+                                                               input_account_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;
                }
@@ -3691,6 +3720,7 @@ INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(char *multi_user_name, int
        ret = true;
 
 FINISH_OFF:
+
        EM_SAFE_FREE(mail_id_array);
 
        if (err_code != NULL)
@@ -3743,7 +3773,17 @@ 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, result_mail_list[i].mailbox_id, result_mail_list[i].thread_id, NULL, 0, 0, NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, false, &err)) {
+                       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, 
+                                                                                                               &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -5059,19 +5099,49 @@ INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int ma
                        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, mail_list[i].mailbox_id, mail_list[i].thread_id, NULL, 0, 0, NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, false, &err)) {
+               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, 
+                                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                /* Destination mailbox replace thread id */
                if (p_thread_id == -1) {
-                       if (!emstorage_update_latest_thread_mail(multi_user_name, account_id, p_mail_data->mailbox_id, p_mail_data->mail_id, NULL, p_mail_data->mail_id, 1, NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, false, &err)) {
+                       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, 
+                                                                                                               &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, p_mail_data->mailbox_id, dest_prev_thread_id_list[i], NULL, 0, 0, NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE, false, &err)) {
+                       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, 
+                                                                                                               &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -7084,475 +7154,6 @@ INTERNAL_FUNC void emcore_free_attachment_info(struct attachment_info *attchment
        EM_DEBUG_FUNC_END();
 }
 
-#if 0
-static char *make_time_string_to_time_t(time_t time)
-{
-       char *time_string = NULL;
-       struct tm *struct_time = NULL;
-
-       if (!time) {
-               EM_DEBUG_EXCEPTION("Invalid paramter");
-               return NULL;
-       }
-
-       time_string = em_malloc(MAX_DATETIME_STRING_LENGTH);
-       if (time_string == NULL) {
-               EM_DEBUG_EXCEPTION("em_malloc failed");
-               return NULL;
-       }
-
-       struct_time = localtime(&time);
-       SNPRINTF(time_string, MAX_DATETIME_STRING_LENGTH, "%d/%d/%d", struct_time->tm_mon + 1, struct_time->tm_mday, struct_time->tm_year + 1900);
-
-       EM_DEBUG_LOG("time string = [%s]", time_string);
-       return time_string;
-}
-
-static SEARCHOR *make_all_search_program(email_search_filter_t *search_filter, int search_filter_count, int current_index, int *output_current_index)
-{
-       EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d], current_index : [%d]", search_filter, search_filter_count, current_index);
-
-       int i = 0;
-       char *time_string = NULL;
-       char temp_criteria[STRING_LENGTH_FOR_DISPLAY] = {0,};
-
-       SEARCHOR *p_search_or_program = NULL;
-       SEARCHPGM *child_search_pgm = NULL;
-
-       for (i = current_index; i < search_filter_count; i++) {
-
-               memset(temp_criteria, 0x00 ,sizeof(temp_criteria));
-
-               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);
-                       goto FINISH_OFF;
-
-               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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "ANSWERED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNANSWERED ");
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BCC %s ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       time_string = make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BEFORE %s ", time_string);
-                       EM_SAFE_FREE(time_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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BODY \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "CC \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "DELETED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNDELETED ");
-
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FLAGGED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNFLAGGED ");
-
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FROM \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "KEYWORD \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       break;
-
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW:
-               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);
-                       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);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "RECENT ");
-
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SEEN ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNSEEN ");
-
-                       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);
-                       time_string = make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SINCE %s ", time_string);
-                       EM_SAFE_FREE(time_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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SUBJECT \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TEXT \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TO \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "HEADER x-priority %d ", search_filter[i].search_filter_key_value.integer_type_key_value);
-                       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);
-                       break;
-
-               case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
-               case EMAIL_SEARCH_FILTER_TYPE_UID :
-               case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
-               case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
-               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);
-                       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);
-                       break;
-
-               default :
-                       EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
-                       break;
-               }
-
-               while (true) {
-                       if (!p_search_or_program) {
-                               p_search_or_program = (SEARCHOR *)em_malloc(sizeof(SEARCHOR));
-                       } else {
-                               if (!p_search_or_program->first) {
-                                       p_search_or_program->first = mail_criteria(temp_criteria);
-                                       break;
-                               }
-                               else if (!p_search_or_program->second) {
-                                       p_search_or_program->second = mail_criteria(temp_criteria);
-                                       break;
-                               }
-                               else {
-                                       child_search_pgm = mail_newsearchpgm();
-                                       child_search_pgm->or = p_search_or_program;
-
-                                       p_search_or_program = NULL;
-                                       p_search_or_program = (SEARCHOR *)em_malloc(sizeof(SEARCHOR));
-                                       p_search_or_program->first = child_search_pgm;
-                               }
-                       }
-               }
-       }
-
-FINISH_OFF:
-
-       if (output_current_index)
-               *output_current_index = i;
-
-       EM_SAFE_FREE(time_string);
-
-       EM_DEBUG_FUNC_END();
-
-       return p_search_or_program;
-}
-
-static int make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, SEARCHPGM **output_search_program)
-{
-       EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
-
-       int i = 0;
-       int err = EMAIL_ERROR_NONE;
-       char *criteria = NULL;
-       char *temp_criteria = NULL;
-       char *time_string = NULL;
-       int current_index = 0;
-       int draft = 0;
-       int undraft = 0;
-       unsigned long larger_size = 0;
-       unsigned long smaller_size = 0;
-
-       SEARCHOR  *or_search_program = NULL;
-       SEARCHPGM *p_search_program = NULL;
-
-       if (search_filter == NULL || search_filter_count < 0) {
-               EM_DEBUG_EXCEPTION("Invalid paramter");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY * search_filter_count);
-       if (criteria == NULL) {
-               EM_DEBUG_EXCEPTION("em_malloc failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       temp_criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY);
-       if (temp_criteria == NULL) {
-               EM_DEBUG_EXCEPTION("em_malloc failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       for (i = 0; i < search_filter_count; i++) {
-               EM_DEBUG_LOG("search_filter_type [%d]", search_filter[i].search_filter_type);
-               memset(temp_criteria, 0x00, STRING_LENGTH_FOR_DISPLAY);
-
-               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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "ALL ");
-                       if (!search_filter[i].search_filter_key_value.integer_type_key_value) {
-                               or_search_program = make_all_search_program(search_filter, search_filter_count, i + 1, &current_index);
-                               i = current_index;
-                       }
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "ANSWERED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNANSWERED ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BCC \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, 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);
-                       time_string = make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BEFORE %s ", time_string);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       EM_SAFE_FREE(time_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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "BODY \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "CC \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "DELETED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNDELETED ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FLAGGED ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNFLAGGED ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "FROM \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "KEYWORD \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "NEW ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "OLD ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       time_string = make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "ON %s ", time_string);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       EM_SAFE_FREE(time_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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "RECENT ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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)
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SEEN ");
-                       else
-                               SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "UNSEEN ");
-
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       time_string = make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SINCE %s ", time_string);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       EM_SAFE_FREE(time_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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "SUBJECT \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TEXT \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "TO \"%s\" ", search_filter[i].search_filter_key_value.string_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "HEADER x-priority %d ", search_filter[i].search_filter_key_value.integer_type_key_value);
-                       strncat(criteria, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
-                       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);
-                       larger_size = 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);
-                       smaller_size = 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)
-                               draft = search_filter[i].search_filter_key_value.integer_type_key_value;
-                       else
-                               undraft = search_filter[i].search_filter_key_value.integer_type_key_value;
-
-                       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);
-                       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);
-                       break;
-
-               default :
-                       EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
-                       err = EMAIL_ERROR_INVALID_PARAM;
-                       goto FINISH_OFF;
-               }
-               EM_SAFE_FREE(time_string); /*prevent 26258*/
-       }
-
-       EM_DEBUG_LOG("criteria[%s]", criteria);
-
-       p_search_program = mail_criteria(criteria);
-       p_search_program->or = or_search_program;
-       p_search_program->larger = larger_size;
-       p_search_program->smaller = smaller_size;
-       p_search_program->draft = draft;
-       p_search_program->undraft = undraft;
-
-FINISH_OFF:
-
-       EM_SAFE_FREE(temp_criteria);
-       EM_SAFE_FREE(time_string);
-       EM_SAFE_FREE(criteria);
-
-       if (output_search_program != NULL)
-               *output_search_program = p_search_program;
-
-       EM_DEBUG_FUNC_END();
-       return err;
-}
-#endif
-
 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);
@@ -7600,7 +7201,8 @@ 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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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;
 
@@ -7613,13 +7215,15 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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;
 
@@ -7645,13 +7249,16 @@ 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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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);
                        break;
 
@@ -7704,19 +7311,22 @@ static int get_search_filter_string(email_search_filter_t *search_filter, int se
 
                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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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\" ", search_filter[i].search_filter_key_value.string_type_key_value);
+                       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;
 
@@ -7740,6 +7350,22 @@ 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\"", 
+                                               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);
+                       strncat(filter_string, temp_criteria, STRING_LENGTH_FOR_DISPLAY);
+                       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);
+                       SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "%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_MESSAGE_NO :
@@ -7771,9 +7397,13 @@ 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 *output_uid_count)
+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]", stream, input_search_filter_count, output_uid_list, 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]", 
+                                               stream, input_search_filter_count, output_uid_list, output_uid_count);
 
        int err = EMAIL_ERROR_NONE;
 
@@ -7803,16 +7433,15 @@ 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, &search_filter_string)) != EMAIL_ERROR_NONE) {
+       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;
        }
 
        SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
        SNPRINTF(command, MAX_PREVIEW_TEXT_LENGTH, "%s UID SEARCH %s\015\012", tag, search_filter_string);
-       /*
-       SNPRINTF(command, sizeof(command), "%s UID SEARCH BEFORE %s SINCE %s OR OR SUBJECT \"%s\" FROM \"%s\" BODY \"%s\"\015\012", tag, before_date_string, since_date_string, input_keyword, input_keyword, input_keyword);
-       */
        EM_DEBUG_LOG_SEC("COMMAND [%s] ", command);
 
 #ifdef FEATURE_CORE_DEBUG
@@ -7888,7 +7517,9 @@ 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, &uid_range_string)) != EMAIL_ERROR_NONE) {
+               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;
                }
@@ -7919,7 +7550,11 @@ FINISH_OFF:
 }
 
 
-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 handle_to_be_published)
+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);
 
@@ -7928,19 +7563,15 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
        int mail_id = 0;
        int thread_id = 0;
        int uid_count = 0;
-       //char temp_uid_string[20] = {0,};
        char *uid_range = NULL;
+       char mailbox_id_param_string[10] = {0,};
        emcore_uid_list *uid_list = NULL;
        emcore_uid_list *uid_elem = NULL;
        emstorage_mailbox_tbl_t *search_mailbox = NULL;
        emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
 
-//     SEARCHPGM *search_program = NULL;
        MAILSTREAM *stream = NULL;
-       //MESSAGECACHE *mail_cache_element = NULL;
        ENVELOPE *env = NULL;
-       emstorage_mailbox_tbl_t* local_mailbox = NULL;
-       char mailbox_id_param_string[10] = {0,};
 
        if (account_id < 0 || mailbox_id == 0) {
                EM_DEBUG_EXCEPTION("Invalid parameter");
@@ -7948,51 +7579,29 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                goto FINISH_OFF;
        }
 
-       /*
-       if ((err = make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &search_program)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("make_criteria_to_search_filter failed [%d]", err);
-               goto FINISH_OFF;
-       }
-       */
-
-       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;
-       }
-
-       SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);
-
-       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 (!emnetwork_check_network_status(&err)) {
-               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
-               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string,  0, err))
-                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
-               goto FINISH_OFF;
-       }
+       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)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
-               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
-                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
                goto FINISH_OFF;
        }
 
-       /*
-       if (!mail_search_full(stream, NIL, search_program, 0)) {
-               EM_DEBUG_EXCEPTION("mail_search failed");
-               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
-                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
-               goto FINISH_OFF;
-       }
-       */
+       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, &uid_count)) != EMAIL_ERROR_NONE) {
+       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;
        }
 
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
        uid_elem = uid_list;
 
        if (uid_count > 0) {
@@ -8009,6 +7618,9 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
        }
 
        while (uid_elem) {
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
                EM_DEBUG_LOG("msgno : [%4lu]", uid_elem->msgno);
 #ifdef __FEATURE_HEADER_OPTIMIZATION__
                env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 0);
@@ -8019,10 +7631,14 @@ 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, &err) || !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);
-                       if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
-                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
                        goto FINISH_OFF;
                }
 
@@ -8030,8 +7646,6 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                if (search_mailbox == NULL) {
                        EM_DEBUG_EXCEPTION("em_malloc failed");
                        err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
-                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
                        goto FINISH_OFF;
                }
 
@@ -8040,15 +7654,29 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_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, &thread_id)) != EMAIL_ERROR_NONE) {
+               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);
-                       if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
-                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
                        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,
+                                                                       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_param_string, thread_id)) {
                        EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
                }
@@ -8062,9 +7690,6 @@ INTERNAL_FUNC int emcore_search_on_server(char *multi_user_name, int account_id,
                i++;
        }
 
-       if (err == EMAIL_ERROR_NONE && !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 >>>>>");
-
 FINISH_OFF:
 
        if (stream)
@@ -8072,19 +7697,13 @@ FINISH_OFF:
 
        if (uid_list)
                emcore_free_uids(uid_list, NULL);
-/*
-       if (search_program)
-               mail_free_searchpgm(&search_program);
-*/
+
        if (search_mailbox != NULL)
                emstorage_free_mailbox(&search_mailbox, 1, NULL);
 
        if (new_mail_tbl_data)
                emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
 
-       if (local_mailbox)
-               emstorage_free_mailbox(&local_mailbox, 1, NULL);
-
        EM_DEBUG_FUNC_END();
        return err;
 
index 662c955..23c831e 100755 (executable)
@@ -71,7 +71,6 @@ extern void imap_parse_body_structure (MAILSTREAM *stream, BODY *body, unsigned
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buffer, int item_count);
 static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream, char *tag, IMAPPARSEDREPLY **reply, int input_download_size, int item_count);
-static int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int account_id, int mail_id, char *uid, int mailbox_id, int *err_code);
 #endif
 
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
@@ -1166,10 +1165,19 @@ 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 ,  int *err_code)
+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]", account, input_mailbox_tbl, limit_count, uid_list, err_code);
+       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;
        int err = EMAIL_ERROR_NONE;
@@ -1203,7 +1211,9 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
        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, uid_list)) != EMAIL_ERROR_NONE ) {
+                       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)
                                        goto FINISH_OFF;
@@ -1218,12 +1228,14 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
                }
        }
 
-       if (!emstorage_get_downloaded_list(multi_user_name, input_mailbox_tbl->account_id,
-                                                                       (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ? 0 : input_mailbox_tbl->mailbox_id,
-                                                                       &downloaded_uids, 
-                                                                       &downloaded_uid_count, 
-                                                                       true, 
-                                                                       &err)) {
+       if (!emstorage_get_downloaded_list(multi_user_name, 
+                                                                               input_mailbox_tbl->account_id,
+                                                                               (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ? 
+                                                                               0 : input_mailbox_tbl->mailbox_id,
+                                                                               &downloaded_uids, 
+                                                                               &downloaded_uid_count, 
+                                                                               true, 
+                                                                               &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1232,7 +1244,7 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
        uid_elem = *uid_list;
        uid_count = 0;
 
-       if(!uid_elem) { /* If there is no mail in the input_mailbox_tbl, remove all mails in the input_mailbox_tbl */
+       if (!uid_elem) { /* If there is no mail in the input_mailbox_tbl, remove all mails in the input_mailbox_tbl */
                for (i = 0; i < downloaded_uid_count; i++) {
                        downloaded_uids[i].sync_status = EMAIL_SYNC_STATUS_NOT_EXIST_ON_SERVER;
                }
@@ -1275,7 +1287,8 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
        while (uid_elem) {
                next_uid_elem = uid_elem->next;
 
-               if ((account->retrieval_mode & EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (uid_elem->flag.seen != 0)) {         /*  already seen */
+               if ((account->retrieval_mode & EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (uid_elem->flag.seen != 0)) {
+                       /*  already seen */
                        EM_SAFE_FREE (uid_elem->uid);
                        EM_SAFE_FREE (uid_elem->internaldate);
                        EM_SAFE_FREE (uid_elem);
@@ -1293,7 +1306,8 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
                                                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 */
+                                                       /* 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) {
@@ -1352,36 +1366,30 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
                                }
                        }
 
-                       //EM_DEBUG_LOG("Is uid[%s] datetime[%s] going to be downloaded ? [%d]", uid_elem->uid, uid_elem->internaldate, to_be_downloaded);
-
                        if (to_be_downloaded) {
-                               if (retrieve_mode == EMAIL_SYNC_OLDEST_MAILS_FIRST){
+                               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, add uid elem to end so that latest mails are in front of list */
-                                       if (head_uid_elem == NULL){
+                               } 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;
                                                head_uid_elem = uid_elem;
                                                end = head_uid_elem;
-                                       }
-                                       else{
+                                       } else {
                                                end->next = uid_elem;
                                                uid_elem->next = NULL;
                                                end = uid_elem;
                                        }
                                }
                                uid_to_be_downloaded_count++;
-
-                       }
-                       else {
+                       } else {
                                EM_SAFE_FREE (uid_elem->uid);
                                EM_SAFE_FREE (uid_elem);
                        }
-
                        uid_count++;
                }
-
                uid_elem = next_uid_elem;
        }
 
@@ -1389,18 +1397,30 @@ static int emcore_get_uids_to_download(char *multi_user_name, MAILSTREAM *stream
        EM_PROFILE_BEGIN(emCoreGetUidsDownloadForLoop);
 
        for (i = 0; i < downloaded_uid_count; i++) {
-               /*  EM_DEBUG_LOG("input_mailbox_tbl[%s] sync_status[%d]", input_mailbox_tbl->name, downloaded_uids[i].sync_status); */
                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, input_mailbox_tbl->mailbox_id, downloaded_uids[i].server_uid, &mail, true, &err)){
-                               EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid for uid[%s] Failed [%d] \n ", downloaded_uids[i].server_uid, err);
+                       (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, 
+                                                                                                               input_mailbox_tbl->mailbox_id,
+                                                                                                               &mail, 
+                                                                                                               true, 
+                                                                                                               &err)){
+                               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);
+                               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) {
@@ -1479,7 +1499,14 @@ 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, int *err_code)
+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);
 
@@ -1496,9 +1523,13 @@ static int emcore_add_read_mail_uid(char *multi_user_name, emstorage_mailbox_tbl
        }
 
        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, &err)) {
+               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;
                }
@@ -1582,9 +1613,15 @@ 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, int *output_thread_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, input_new_mail_tbl_data, output_mail_id, 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, 
+                                               input_new_mail_tbl_data, output_mail_id, output_thread_id);
 
        int                   err = EMAIL_ERROR_NONE;
        int                   thread_id = -1;
@@ -1610,20 +1647,24 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name, emstorage_ma
                goto FINISH_OFF;
        }
 
-       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)
+       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.");
 
        if (thread_id == -1){
                input_new_mail_tbl_data->thread_id = input_new_mail_tbl_data->mail_id;
                input_new_mail_tbl_data->thread_item_count = thread_item_count = 1;
-       }
-       else {
+       } else {
                input_new_mail_tbl_data->thread_id = thread_id;
                thread_item_count++;
        }
 
-        EM_DEBUG_LOG("MULTI USER NAME: [%s]", multi_user_name);
-        input_new_mail_tbl_data->user_name = EM_SAFE_STRDUP(multi_user_name);
+       EM_DEBUG_LOG("MULTI USER NAME: [%s]", multi_user_name);
+       input_new_mail_tbl_data->user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emstorage_add_mail(multi_user_name, input_new_mail_tbl_data, 0, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
@@ -1632,20 +1673,44 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name, emstorage_ma
        }
 
        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, input_new_mail_tbl_data->thread_id, NULL, -1, -1, NOTI_THREAD_ID_CHANGED_BY_ADD, false, &err)) {
+               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, 
+                                                                                                       &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, input_new_mail_tbl_data->mailbox_id, input_new_mail_tbl_data->thread_id, NULL, 0, 0, NOTI_THREAD_ID_CHANGED_BY_ADD, false, &err)) {
+               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, 
+                                                                                                       &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_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)
+       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.");
 
        if (output_thread_id)
@@ -1654,7 +1719,14 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(char *multi_user_name, emstorage_ma
        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, &err)) {
+       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);
                goto FINISH_OFF;
@@ -2035,7 +2107,9 @@ 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]", input_mailbox_tbl, input_uid_list, err_code);
+       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) {
                EM_DEBUG_EXCEPTION ("NULL stream");
                if (err_code)
@@ -2046,10 +2120,11 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
        int ret = false;
        int err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
        int download_limit_count;
-       email_account_t      *account_ref = NULL;
+       email_session_t *session = NULL;
+       email_account_t *account_ref = NULL;
        emstorage_rule_tbl_t *rule = NULL;
-       emcore_uid_list      *uid_list = NULL;
-       emcore_uid_list      *uid_elem = NULL;
+       emcore_uid_list *uid_list = NULL;
+       emcore_uid_list *uid_elem = NULL;
        emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
        emstorage_mailbox_tbl_t *destination_mailbox = NULL;
 
@@ -2080,7 +2155,6 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                goto FINISH_OFF;
        }
 
-
        if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && input_mailbox_tbl->local_yn == 1) {
                /* mailbox recovery starts */
                if (input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) {
@@ -2090,8 +2164,9 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
                        }
                }
                else {
-                       EM_DEBUG_EXCEPTION("because mailbox_name[%s] mailbox_type[%d] of account[%d] is a local box, it cant be synced",
-                              input_mailbox_tbl->mailbox_name, input_mailbox_tbl->mailbox_type, account_id);
+                       EM_DEBUG_EXCEPTION("because mailbox_name[%s] mailbox_type[%d] of account[%d] is "
+                                                               "a local box, it cant be synced",
+                                                               input_mailbox_tbl->mailbox_name, input_mailbox_tbl->mailbox_type, account_id);
                        err = EMAIL_ERROR_INVALID_MAILBOX;
                        goto FINISH_OFF;
                }
@@ -2108,7 +2183,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, &err) || !*stream) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, 
+                                                                                       account_id, 
+                                                                                       input_mailbox_tbl->mailbox_id, 
+                                                                                       (void **)stream, 
+                                                                                       &err) || !*stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
                goto FINISH_OFF;
        }
@@ -2116,320 +2195,367 @@ INTERNAL_FUNC int emcore_sync_header (char *multi_user_name,
        if (cancellable)
                FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-       /*  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, &err)){
-               EM_DEBUG_EXCEPTION("emstorage_update_mailbox_total_count failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       /* if (((MAILSTREAM *)stream)->nmsgs > 0) */
-       {
-               email_option_t *opt_ref = &account_ref->options;
-               EM_DEBUG_LOG_SEC("block_address = %d, block_subject = %d", opt_ref->block_address, opt_ref->block_subject);
 
-               if (opt_ref->block_address || opt_ref->block_subject) {
-                       int is_completed = false;
-                       int type = 0;
+       /* check the connection broken */
+       EM_DEBUG_LOG("((MAILSTREAM *)stream)->nmsgs [%ld]", ((MAILSTREAM *)*stream)->nmsgs);
 
-                       if (!opt_ref->block_address)
-                               type = EMAIL_FILTER_SUBJECT;
-                       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, &err) || !rule) {
-                               if ( err != EMAIL_ERROR_FILTER_NOT_FOUND )
-                                       EM_DEBUG_EXCEPTION ("emstorage_get_rule error [%d]", err);
-                       }
+       unsigned long nmsgs_count = ((MAILSTREAM *)*stream)->nmsgs;
+       if (!nmsgs_count) {
+               EM_DEBUG_LOG("MAILSTREAM");
+               if (!emcore_get_current_session(&session)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_current_session failed");
+                       err = EMAIL_ERROR_SESSION_NOT_FOUND;
+                       goto FINISH_OFF;
                }
 
-               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, &err)){
-                       EM_DEBUG_EXCEPTION("emcore_get_uids_to_download failed [%d]", err);
+               if (session->error == EMAIL_ERROR_CONNECTION_BROKEN) {
+                       EM_DEBUG_EXCEPTION("session error : [%d]", session->error);
+                       err = session->error;
                        goto FINISH_OFF;
                }
+       }
 
-               if (cancellable)
-                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+       /*  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, 
+                                                                                               &err)){
+               EM_DEBUG_EXCEPTION("emstorage_update_mailbox_total_count failed [%d]", err);
+               goto FINISH_OFF;
+       }
 
-               if (input_uid_list && *input_uid_list){
-                       emcore_free_uids(*input_uid_list, NULL);
-                       *input_uid_list = uid_list;
+       email_option_t *opt_ref = &account_ref->options;
+       EM_DEBUG_LOG_SEC("block_address = %d, block_subject = %d", opt_ref->block_address, opt_ref->block_subject);
+
+       if (opt_ref->block_address || opt_ref->block_subject) {
+               int is_completed = false;
+               int type = 0;
+
+               if (!opt_ref->block_address)
+                       type = EMAIL_FILTER_SUBJECT;
+               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, 
+                                                               &err) || !rule) {
+                       if ( err != EMAIL_ERROR_FILTER_NOT_FOUND )
+                               EM_DEBUG_EXCEPTION ("emstorage_get_rule error [%d]", err);
                }
-               uid_elem = uid_list;
-               i = 0;
-               EM_PROFILE_BEGIN(emCoreSyncHeaderwhileloop);
+       }
+
+       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, 
+                                                                               &err)) {
+               EM_DEBUG_EXCEPTION("emcore_get_uids_to_download failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+
+       if (input_uid_list && *input_uid_list){
+               emcore_free_uids(*input_uid_list, NULL);
+               *input_uid_list = uid_list;
+       }
+       uid_elem = uid_list;
+       i = 0;
+       EM_PROFILE_BEGIN(emCoreSyncHeaderwhileloop);
 
 #ifdef  __FEATURE_HEADER_OPTIMIZATION__
-               /* g.shyamakshi@samsung.com : Bulk fetch of headers only if the receiving server type is IMAP */
+       /* g.shyamakshi@samsung.com : Bulk fetch of headers only if the receiving server type is IMAP */
 
-               EM_DEBUG_LOG("((MAILSTREAM *)stream)->nmsgs [%d]", ((MAILSTREAM *)*stream)->nmsgs);
-               EM_DEBUG_LOG("uid_list [%p]", uid_list);
+       EM_DEBUG_LOG("uid_list [%p]", uid_list);
 
-               if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list  != NULL) {
+       if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list  != NULL) {
 
-                       if ((err = emcore_make_uid_range_string(uid_list, total, &uid_range)) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_make_uid_range_string failed [%d]", err);
-                               goto FINISH_OFF;
-                       }
+               if ((err = emcore_make_uid_range_string(uid_list, total, &uid_range)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_make_uid_range_string failed [%d]", err);
+                       goto FINISH_OFF;
+               }
 
-                       if (cancellable)
-                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-                       uid_elem = uid_list;
-                       if (*stream && uid_elem){
-                               EM_DEBUG_LOG("msgno : %d", uid_elem->msgno);
-                               ((MAILSTREAM *)*stream)->nmsgs = uid_elem->msgno;
-                       }
-                       else{
-                               EM_DEBUG_LOG("Uid List Null");
-                       }
-                       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);
+               uid_elem = uid_list;
+               if (*stream && uid_elem){
+                       EM_DEBUG_LOG("msgno : %d", uid_elem->msgno);
+                       ((MAILSTREAM *)*stream)->nmsgs = uid_elem->msgno;
+               }
+               else{
+                       EM_DEBUG_LOG("Uid List Null");
                }
+               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);
+       }
 #endif
 
-               /*  h.gahlaut@samsung.com :  Clear the event queue of partial body download thread before starting fetching new headers  */
+       /*  h.gahlaut@samsung.com :  Clear the event queue of partial body download thread before starting fetching new headers  */
 #ifndef __FEATURE_PARTIAL_BODY_FOR_POP3__
-               if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
+       if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
 #endif /*  __FEATURE_PARTIAL_BODY_FOR_POP3__ */
-                       /*  Partial body download feature is only for IMAP accounts  */
-                       if (false == emcore_clear_partial_body_thd_event_que(&err))
-                               EM_DEBUG_LOG("emcore_clear_partial_body_thd_event_que failed [%d]", err);
+               /*  Partial body download feature is only for IMAP accounts  */
+               if (false == emcore_clear_partial_body_thd_event_que(&err))
+                       EM_DEBUG_LOG("emcore_clear_partial_body_thd_event_que failed [%d]", err);
 #ifndef __FEATURE_PARTIAL_BODY_FOR_POP3__
-               }
+       }
 #endif /*  __FEATURE_PARTIAL_BODY_FOR_POP3__ */
-               while (uid_elem) {
-                       EM_PROFILE_BEGIN(emCoreSyncHeaderEachMail);
-                       EM_DEBUG_LOG("mail_fetchstructure_full  :  uid_elem->msgno[%d]", uid_elem->msgno);
+       while (uid_elem) {
+               EM_PROFILE_BEGIN(emCoreSyncHeaderEachMail);
+               EM_DEBUG_LOG("mail_fetchstructure_full  :  uid_elem->msgno[%d]", uid_elem->msgno);
 
-                       env = NULL;
+               env = NULL;
 
-                       if (uid_elem->msgno > ((MAILSTREAM *)*stream)->nmsgs) {
-                               EM_DEBUG_LOG ("WARN: msgno[%d] can't be greater than nmsgs[%d].",
-                                               uid_elem->msgno, ((MAILSTREAM *)*stream)->nmsgs);
-                       }
-                       else{
+               if (uid_elem->msgno > ((MAILSTREAM *)*stream)->nmsgs) {
+                       EM_DEBUG_LOG ("WARN: msgno[%d] can't be greater than nmsgs[%d].",
+                                       uid_elem->msgno, ((MAILSTREAM *)*stream)->nmsgs);
+               }
+               else{
 #ifdef __FEATURE_HEADER_OPTIMIZATION__
-                               if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* Fetch env from cache in case of IMAP */
-                                       env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK, 0);
-                               else /* Fetch header from network in case of POP */
-                                       env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK, 1);
+                       if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* Fetch env from cache in case of IMAP */
+                               env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK, 0);
+                       else /* Fetch header from network in case of POP */
+                               env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK, 1);
 #else
-                               env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK);
+                       env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK);
 #endif
-                       }
-
-                       if (cancellable)
-                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+               }
 
-                       if (env != NULL) {
-                               int searched_mail_id = 0;
-                               int blocked = false;
-                               int priority_sender = false;
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-                               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;
-                               }
+               if (env != NULL) {
+                       int searched_mail_id = 0;
+                       int blocked = false;
+                       int priority_sender = false;
 
-                               /* 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, 
-                                                                                                                                                        &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, 
-                                                                                               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, 
-                                                                                                       new_mail_tbl_data, true, &err)) {
-                                               EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed : [%d]", err);
-                                               goto CONTINUE_NEXT;
-                                       }
+                       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, 
+                                                                                                                                                &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, 
+                                                                                       0, &err)) {
+                                       EM_DEBUG_EXCEPTION("emcore_add_read_mail_uid failed : [%d]", err);
                                        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, &err)) {
-                                       EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
+                       
+                               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;
                                }
 
-                               if (destination_mailbox) /* cleanup before reusing */
-                                       emstorage_free_mailbox(&destination_mailbox, 1, NULL);
+                               goto CONTINUE_NEXT;
+                       }
 
-                               if (priority_sender) {
-                                       new_mail_tbl_data->tag_id = PRIORITY_SENDER_TAG_ID;
-                                       vip_total ++;
-                               }
+                       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);
+                       }
 
-                               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, 
-                                                                &err)) {
-                                                       EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
-                                               }
-                                       }
-                                       else
-                                               blocked = 0;
-                               }
+                       if (destination_mailbox) /* cleanup before reusing */
+                               emstorage_free_mailbox(&destination_mailbox, 1, NULL);
 
-                               /* Set the noti waited */
-                               new_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_NOTI_WAITED;
+                       if (priority_sender) {
+                               new_mail_tbl_data->tag_id = PRIORITY_SENDER_TAG_ID;
+                               vip_total ++;
+                       }
 
-                               if (destination_mailbox) {
-                                       if (destination_mailbox->local_yn == 0 || account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-                                               if ((err = emcore_move_mail_on_server_by_server_mail_id((void*)*stream, new_mail_tbl_data->server_mail_id, destination_mailbox->mailbox_name)) != EMAIL_ERROR_NONE){
-                                                       EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_by_server_mail_id falied [%d]", err);
-                                                       goto CONTINUE_NEXT;
-                                               }
+                       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, 
+                                                                                                                       &err)) {
+                                               EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                        }
-                                       else { /* local mailbox */
-                                               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;
-                                               }
+                               }
+                               else
+                                       blocked = 0;
+                       }
 
+                       /* Set the noti waited */
+                       new_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_NOTI_WAITED;
+
+                       if (destination_mailbox) {
+                               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, 
+                                                                                                                                                       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;
                                        }
-                                       SNPRINTF(mailbox_id_param_string, 10, "%d", destination_mailbox->mailbox_id);
-                               } 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, 
-                                                        &thread_id)) != EMAIL_ERROR_NONE) {
+                               } else { /* local mailbox */
+                                       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;
                                        }
 
+                               }
+                               SNPRINTF(mailbox_id_param_string, 10, "%d", destination_mailbox->mailbox_id);
+                       } 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, 
+                                                                                                       &thread_id)) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
+                                       goto CONTINUE_NEXT;
+                               }
+
 #ifndef __FEATURE_PARTIAL_BODY_FOR_POP3__
-                                       if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
+                               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, 
-                                                            uid_elem->uid,
-                                                            input_mailbox_tbl->mailbox_id, 
-                                                            &err))
-                                                               EM_DEBUG_LOG("Partial body download initiation failed [%d]", err);
-                                               }
-#ifndef __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, 
+                                                                                                               uid_elem->uid,
+                                                                                                               input_mailbox_tbl->mailbox_id,
+                                                                                                               input_mailbox_tbl->mailbox_name,
+                                                                                                               &err))
+                                                       EM_DEBUG_LOG("Partial body download initiation failed [%d]", err);
                                        }
+#ifndef __FEATURE_PARTIAL_BODY_FOR_POP3__
+                               }
 #endif /*  __FEATURE_PARTIAL_BODY_FOR_POP3__ */
 
 #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, 
-                                                            &err))
-                                                       EM_DEBUG_LOG("emcore_insert_auto_download_job failed [%d]", err);
-                                       }
+                               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, 
+                                                                                                               &err))
+                                               EM_DEBUG_LOG("emcore_insert_auto_download_job failed [%d]", err);
+                               }
 #endif
 /*
-                                       if (!uid_elem->flag.seen && input_mailbox_tbl->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX)
-                                               emcore_add_notification_for_unread_mail(new_mail_tbl_data);
+                               if (!uid_elem->flag.seen && input_mailbox_tbl->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX)
+                                       emcore_add_notification_for_unread_mail(new_mail_tbl_data);
 */
-                                       if (cancellable)
-                                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+                               if (cancellable)
+                                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-                                       if (!uid_elem->flag.seen) {
-                                               unread++;
-                                               if (priority_sender) {
-                                                       vip_unread++;
-                                               }
+                               if (!uid_elem->flag.seen) {
+                                       unread++;
+                                       if (priority_sender) {
+                                               vip_unread++;
                                        }
-
-                                       percentage = ((i+1) * 100) / total ;
-                                       EM_DEBUG_LOG("Header Percentage Completed [%d] : [%d/%d]  mail_id [%d]", percentage, i+1,
-                                                                                                        total, mail_id);
-                                       SNPRINTF(mailbox_id_param_string, 10, "%d", input_mailbox_tbl->mailbox_id);
                                }
 
-                               if (!emcore_notify_storage_event (NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id))
-                                       EM_DEBUG_EXCEPTION ("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
+                               percentage = ((i+1) * 100) / total ;
+                               EM_DEBUG_LOG("Header Percentage Completed [%d] : [%d/%d]  mail_id [%d]", percentage, i+1,
+                                                                                                                                                                                                       total, mail_id);
+                               SNPRINTF(mailbox_id_param_string, 10, "%d", input_mailbox_tbl->mailbox_id);
+                       }
 
-#ifdef __FEATURE_BLOCKING_MODE__
-                               /* Check the blocking mode */
-                               int blocking_mode = false;
-
-                               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, 
-                                                                                &blocking_mode)) != EMAIL_ERROR_NONE) {
-                                               EM_DEBUG_EXCEPTION ("emcore_check_blocking_mode failed : [%d]", err);
-                                       }
+                       if (!emcore_notify_storage_event (NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id))
+                               EM_DEBUG_EXCEPTION ("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
 
-                                       if (blocking_mode)
-                                               emcore_set_blocking_mode_status (blocking_mode);
+#ifdef __FEATURE_BLOCKING_MODE__
+                       /* Check the blocking mode */
+                       int blocking_mode = false;
+
+                       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, 
+                                                                                                                                                       &blocking_mode)) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION ("emcore_check_blocking_mode failed : [%d]", err);
                                }
+
+                               if (blocking_mode)
+                                       emcore_set_blocking_mode_status (blocking_mode);
+                       }
 #endif /* __FEATURE_BLOCKING_MODE__ */
 
-                               /* Set contact log */
-                               switch (input_mailbox_tbl->mailbox_type) {
-                               case EMAIL_MAILBOX_TYPE_INBOX :
-                                       if ((err = emcore_set_received_contacts_log(multi_user_name, new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
-                                               EM_DEBUG_EXCEPTION("emcore_set_received_contacts_log failed : [%d]", err);
-                                       }
-                                       break;
-                               case EMAIL_MAILBOX_TYPE_SENTBOX:
-                               case EMAIL_MAILBOX_TYPE_OUTBOX:
-                                       if ((err = emcore_set_sent_contacts_log(multi_user_name, new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
-                                               EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
-                                       }
-                                       break;
-                               default:
-                                       EM_DEBUG_LOG("Mailbox type : [%d]", input_mailbox_tbl->mailbox_type);
-                                       break;
+                       /* Set contact log */
+                       switch (input_mailbox_tbl->mailbox_type) {
+                       case EMAIL_MAILBOX_TYPE_INBOX :
+                               if ((err = emcore_set_received_contacts_log(multi_user_name, new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_set_received_contacts_log failed : [%d]", err);
                                }
+                               break;
+                       case EMAIL_MAILBOX_TYPE_SENTBOX:
+                       case EMAIL_MAILBOX_TYPE_OUTBOX:
+                               if ((err = emcore_set_sent_contacts_log(multi_user_name, new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
+                               }
+                               break;
+                       default:
+                               EM_DEBUG_LOG("Mailbox type : [%d]", input_mailbox_tbl->mailbox_type);
+                               break;
+                       }
 
-                               /*  Release for envelope is not required and it may cause crash. Don't free the memory for envelope here. */
-                               /*  Envelope data will be freed by garbage collector in mail_close_full */
+                       /*  Release for envelope is not required and it may cause crash. Don't free the memory for envelope here. */
+                       /*  Envelope data will be freed by garbage collector in mail_close_full */
 
-                               if (cancellable)
-                                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
-                       }
+                       if (cancellable)
+                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
+               }
 
 CONTINUE_NEXT:
-                       if (new_mail_tbl_data){
-                               emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
-                               new_mail_tbl_data = NULL;
-                       }
-                       uid_elem = uid_elem->next;
-                       i++;
-                       EM_PROFILE_END(emCoreSyncHeaderEachMail);
+               if (new_mail_tbl_data){
+                       emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
+                       new_mail_tbl_data = NULL;
                }
-               EM_PROFILE_END(emCoreSyncHeaderwhileloop);
+               uid_elem = uid_elem->next;
+               i++;
+               EM_PROFILE_END(emCoreSyncHeaderEachMail);
        }
+       EM_PROFILE_END(emCoreSyncHeaderwhileloop);
 
        ret = true;
+
 FINISH_OFF:
 
        if (mail_count != NULL)
@@ -2569,7 +2695,6 @@ int emcore_download_uid_all(char *multi_user_name, MAILSTREAM *mail_stream, emai
 
                if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
                        EM_DEBUG_EXCEPTION("net_sout failed...");
-
                        err = EMAIL_ERROR_CONNECTION_BROKEN;
                        goto FINISH_OFF;
                }
@@ -2672,7 +2797,6 @@ int emcore_download_uid_all(char *multi_user_name, MAILSTREAM *mail_stream, emai
 #endif
                if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
                        EM_DEBUG_EXCEPTION("net_sout failed...");
-
                        err = EMAIL_ERROR_CONNECTION_BROKEN;
                        goto FINISH_OFF;
                }
@@ -3422,14 +3546,16 @@ FINISH_OFF:
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-static int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int account_id, int mail_id, char *uid, int input_maibox_id, int *err_code)
+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]", account_id, mail_id, uid, input_maibox_id);
+       EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_id[%d], uid[%p], input_maibox_id[%d], input_mailbox_name[%s]",
+                                               account_id, mail_id, uid, input_maibox_id, input_mailbox_name);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account_ref = NULL;
-       emstorage_mailbox_tbl_t* mailbox = NULL;
 
        if (account_id < FIRST_ACCOUNT_ID || mail_id < 0 || NULL == uid || 0 == input_maibox_id){
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -3438,9 +3564,19 @@ static int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int ac
        }
 
        account_ref = emcore_get_account_reference(multi_user_name, account_id, false);
+       if (account_ref == NULL) {
+               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
 
-       email_event_partial_body_thd pbd_event;
+       if (account_ref->auto_download_size == 0) {
+               EM_DEBUG_LOG("Header only download");
+               ret = true;
+               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;
@@ -3449,20 +3585,16 @@ static int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int ac
        else
                pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_IMAP4;
 
-       if ( (err = emstorage_get_mailbox_by_id(multi_user_name, input_maibox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
-               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       pbd_event.mailbox_id      = input_maibox_id;
        pbd_event.mail_id         = mail_id;
        pbd_event.server_mail_id  = strtoul(uid, NULL, 0);
-       pbd_event.mailbox_name    = EM_SAFE_STRDUP(mailbox->mailbox_name);
+       pbd_event.mailbox_id      = input_maibox_id;
+       pbd_event.mailbox_name    = EM_SAFE_STRDUP(input_mailbox_name);
        pbd_event.multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
        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]", pbd_event.account_id, pbd_event.mail_id , pbd_event.server_mail_id);
+       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)){
                EM_DEBUG_EXCEPTION("Inserting Partial Body Download activity failed with error[%d]", err);
@@ -3495,10 +3627,6 @@ static int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, int ac
 
 FINISH_OFF:
 
-       if (mailbox) {
-               emstorage_free_mailbox(&mailbox, 1, &err);
-       }
-
        if (account_ref) {
                emcore_free_account(account_ref);
                EM_SAFE_FREE(account_ref);
@@ -3522,7 +3650,8 @@ INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name,
                                                                                                                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]", cnt_info, account_id, mail_id, mailbox_id);
+       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;
 
        if (!cnt_info || !account_id || !mail_id || !mailbox_id) {
@@ -3614,6 +3743,7 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
        IMAPLOCAL *imaplocal = NULL;
        IMAPPARSEDREPLY *reply_from_server = NULL;
        emstorage_mail_tbl_t *mail = NULL;
+       emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
        email_partial_buffer *imap_response = NULL;
        BODY *body = NULL;
        struct _m_content_info *cnt_info = NULL;
@@ -3710,12 +3840,22 @@ 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, pbd_event[temp_count].mailbox_id, uid_string, &mail, true, &err) || !mail) {
+               if (!emstorage_get_maildata_by_servermailid(pbd_event[temp_count].multi_user_name, 
+                                                                                                       uid_string, 
+                                                                                                       pbd_event[temp_count].mailbox_id,
+                                                                                                       &mail, 
+                                                                                                       false, 
+                                                                                                       &err) || !mail) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND || !mail)
                                goto FINISH_OFF;
                }
 
+               if (mailbox_tbl) {
+                       emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
+                       mailbox_tbl = NULL;
+               }
+
                if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
                        EM_DEBUG_LOG("Downloaded mail");
                        continue;
@@ -4009,7 +4149,11 @@ 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, &err))
+                       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, 
@@ -4118,10 +4262,14 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                                while (temp_file) {
                                        if (temp_file->type == INLINE_ATTACHMENT && temp_file->save_status == 1) {
                                                inline_download_count++;
-                    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, &err))
+                                               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, 
+                                                                                                       &err))
                                                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
                                                if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
@@ -4154,7 +4302,11 @@ 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, &err))
+                                                       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);
                                                }
                                        }
@@ -4167,11 +4319,16 @@ 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) && (inline_download_count == inline_attachment_num))?
-                               EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED: EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED);
+               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_PARTIALLY_DOWNLOADED);
 
-               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]", total_mail_size, total_attachment_size, input_download_size);
+               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]", 
+                                               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 */
@@ -4183,7 +4340,12 @@ static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int
                        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, &err)) {
+               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;
                }
@@ -4191,11 +4353,18 @@ 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, &err) || stripped_text == NULL) {
+               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, &err) || !mail_text) {
+               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;
                }
@@ -4203,7 +4372,11 @@ 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, &err)) {
+               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;
                }
@@ -4215,7 +4388,11 @@ 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, &err)){
+               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;
                }
@@ -4272,6 +4449,9 @@ FINISH_OFF:
        if (mail)
                emstorage_free_mail(&mail, 1, NULL);
 
+       if (mailbox_tbl)
+               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
+
        if (mail_text)
                emstorage_free_mail_text(&mail_text, 1, NULL);
 
@@ -4322,8 +4502,6 @@ FINISH_OFF:
        return ret;
 }
 
-
-
 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, email_event_partial_body_thd *pbd_event, int item_count, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("stream [%p], pbd_event [%p], item_count [%d], error [%p]", stream, pbd_event, item_count, error);
@@ -4342,7 +4520,7 @@ INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, ema
                goto FINISH_OFF;
        }
 
-       auto_download_size = (pbd_account_tbl->auto_download_size<4096)?4096:pbd_account_tbl->auto_download_size;
+       auto_download_size = (pbd_account_tbl->auto_download_size < 4096) ? 4096 : pbd_account_tbl->auto_download_size;
 
        switch (pbd_account_tbl->incoming_server_type){
                case EMAIL_SERVER_TYPE_IMAP4:
index b9d22d9..3c9b5ab 100755 (executable)
@@ -625,9 +625,16 @@ INTERNAL_FUNC int emcore_delete_mailbox(char *multi_user_name, int input_mailbox
                                EM_DEBUG_LOG("Delete the mailbox in server : success");
                }
 
-               if (!emcore_delete_all_mails_of_mailbox(multi_user_name, target_mailbox_array[i].account_id, target_mailbox_array[i].mailbox_id, false, &err))  {
-                       EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
-                       goto FINISH_OFF;
+               if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
+                                                                                               target_mailbox_array[i].account_id, 
+                                                                                               target_mailbox_array[i].mailbox_id,
+                                                                                               0,
+                                                                                               false, 
+                                                                                               &err))  {
+                       if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
+                               EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
                }
 
                if (!emstorage_delete_mailbox(multi_user_name, target_mailbox_array[i].account_id, -1, target_mailbox_array[i].mailbox_id, true, &err))  {
@@ -693,10 +700,16 @@ INTERNAL_FUNC int emcore_delete_mailbox_all(char *multi_user_name, email_mailbox
                goto FINISH_OFF;
        }
        
-       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, mailbox->account_id, mailbox->mailbox_id, 0, /*NULL, */ &err)) {
-               EM_DEBUG_EXCEPTION(" emcore_delete_all_mails_of_mailbox failed - %d", err);
-
-               goto FINISH_OFF;
+       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
+                                                                                       mailbox->account_id, 
+                                                                                       mailbox->mailbox_id,
+                                                                                       0,
+                                                                                       0, /*NULL, */ 
+                                                                                       &err)) {
+               if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
+                       EM_DEBUG_EXCEPTION(" emcore_delete_all_mails_of_mailbox failed - %d", err);
+                       goto FINISH_OFF;
+               }
        }
 
        if (!emstorage_delete_mailbox(multi_user_name, mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
@@ -886,9 +899,11 @@ 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, email_account_t *account, 
-                                        int input_mailbox_id, void **result_stream, /*either MAILSTREAM or SENDSTREAM*/ 
-                                        int *err_code)
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *multi_user_name, 
+                                                                                                                                               email_account_t *account, 
+                                                                                                                                               int input_mailbox_id, 
+                                                                       /*either MAILSTREAM or SENDSTREAM*/ void **result_stream, 
+                                                                                                                                               int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreMailboxOpen);
        EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account, input_mailbox_id,
@@ -938,16 +953,15 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
                EM_DEBUG_LOG("Stream reuse desired");
 #endif
 
+       /* set current network error as EMAIL_ERROR_NONE before network operation */
        session->error = EMAIL_ERROR_NONE;
-       emcore_set_network_error (EMAIL_ERROR_NONE);    /* set current network error as EMAIL_ERROR_NONE before network operation */
-
+       emcore_set_network_error(EMAIL_ERROR_NONE);             
+       
        if (input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
                mailbox_name = EM_SAFE_STRDUP(ENCODED_PATH_SMTP);
-       }
-       else if (input_mailbox_id == 0) {
+       } else if (input_mailbox_id == 0) {
                mailbox_name = NULL;
-       }
-       else {
+       } else {
                if ((error = emstorage_get_mailbox_by_id (multi_user_name, input_mailbox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
                        goto FINISH_OFF;
@@ -957,7 +971,12 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
 
        if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
                /*  open pop3/imap server */
-               if (!emcore_get_long_encoded_path_with_account_info (multi_user_name, account, mailbox_name, '/', &mbox_path, &error)) {
+               if (!emcore_get_long_encoded_path_with_account_info(multi_user_name, 
+                                                                                                                       account, 
+                                                                                                                       mailbox_name, 
+                                                                                                                       '/', 
+                                                                                                                       &mbox_path, 
+                                                                                                                       &error)) {
                        EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed - %d", error);
                        session->error = error;
                        goto FINISH_OFF;
@@ -967,32 +986,39 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
 
                session->auth = 0; /*  ref_account->receiving_auth ? 1  :  0 */
 
-               if (!(*result_stream = mail_open (*result_stream, mbox_path, IMAP_2004_LOG))) {
-                       EM_DEBUG_EXCEPTION("mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
-                       *result_stream = mail_close (*result_stream);
+               if (!(*result_stream = mail_open(*result_stream, mbox_path, IMAP_2004_LOG))) {
+                       EM_DEBUG_EXCEPTION("mail_open failed. session->error[%d], session->network[%d]", 
+                                                               session->error, session->network);
+                       *result_stream = mail_close(*result_stream);
+
+                       if (account->account_id > 0 && (session->network == EMAIL_ERROR_XOAUTH_BAD_REQUEST || 
+                               session->network == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED)) {
+                               error = emcore_refresh_xoauth2_access_token (multi_user_name, account->account_id);
 
-                       if(account->account_id > 0 && (session->network == EMAIL_ERROR_XOAUTH_BAD_REQUEST || session->network == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED)) {
-                               if((error = emcore_refresh_xoauth2_access_token (multi_user_name, account->account_id)) != EMAIL_ERROR_NONE) {
+                               if (error != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emcore_refresh_xoauth2_access_token failed. [%d]", error);
-                               }
-                               else {
+                               } else {
                                        while (*result_stream == NULL && connection_retry_count < 3) {
 
                                                sleep(3); /* wait for updating access token */
-                                               if (!(*result_stream = mail_open (*result_stream, mbox_path, IMAP_2004_LOG))) {
-                                                       EM_DEBUG_LOG("mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
+                                               if (!(*result_stream = mail_open(*result_stream, mbox_path, IMAP_2004_LOG))) {
+                                                       EM_DEBUG_LOG("mail_open failed. session->error[%d], session->network[%d]", 
+                                                                                       session->error, session->network);
                                                }
                                                connection_retry_count++;
                                                EM_DEBUG_LOG ("connection_retry_count [%d]", connection_retry_count);
                                        }
                                }
-                       } else if ((session->error == EMAIL_ERROR_TLS_SSL_FAILURE || session->error == EMAIL_ERROR_CANNOT_NEGOTIATE_TLS)) {
+                       } else if ((session->error == EMAIL_ERROR_TLS_SSL_FAILURE || 
+                                               session->error == EMAIL_ERROR_CANNOT_NEGOTIATE_TLS)) {
                                char *replaced_mbox_path = NULL;
                                replaced_mbox_path = em_replace_string(mbox_path, "}", "/force_tls_v1_0}");
 
-                               if (replaced_mbox_path != NULL && !(*result_stream = mail_open (*result_stream, replaced_mbox_path, IMAP_2004_LOG))) {
-                                       EM_DEBUG_EXCEPTION("retry --> mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
-                                       *result_stream = mail_close (*result_stream);
+                               if (replaced_mbox_path != NULL && 
+                                       !(*result_stream = mail_open(*result_stream, replaced_mbox_path, IMAP_2004_LOG))) {
+                                       EM_DEBUG_EXCEPTION("retry --> mail_open failed. session->error[%d], session->network[%d]", 
+                                                                               session->error, session->network);
+                                       *result_stream = mail_close(*result_stream);
                                }
 
                                EM_SAFE_FREE(replaced_mbox_path);
@@ -1005,8 +1031,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
                                goto FINISH_OFF;
                        }
                }
-       }
-       else {
+       } else {
                /*  open smtp server */
                char *host_list[2] = {NULL, NULL};
 
@@ -1026,7 +1051,12 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
                }
 #endif
                if (!*result_stream) {
-                       if (!emcore_get_long_encoded_path_with_account_info (multi_user_name, account, mailbox_name, 0, &mbox_path, &error)) {
+                       if (!emcore_get_long_encoded_path_with_account_info(multi_user_name, 
+                                                                                                                               account, 
+                                                                                                                               mailbox_name, 
+                                                                                                                               0, 
+                                                                                                                               &mbox_path, 
+                                                                                                                               &error)) {
                                EM_DEBUG_EXCEPTION(" emcore_get_long_encoded_path failed - %d", error);
                                session->error = error;
                                goto FINISH_OFF;
@@ -1040,13 +1070,16 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
 
                        if (!(*result_stream = smtp_open (host_list, 1))) {
                                EM_DEBUG_EXCEPTION_SEC("smtp_open error : outgoing_server_secure_connection[%d] "
-                                               "session->error[%d] session->network[%d]",
-                                          account->outgoing_server_secure_connection, session->error, session->network);
+                                                                          "session->error[%d] session->network[%d]",
+                                                                                 account->outgoing_server_secure_connection, 
+                                                                                 session->error, session->network);
 
                                if (account->account_id > 0 && (session->network == EMAIL_ERROR_XOAUTH_BAD_REQUEST ||
-                                                         session->network == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED)) {
-                                       *result_stream = smtp_close (*result_stream);
-                                       if((error = emcore_refresh_xoauth2_access_token (multi_user_name, account->account_id)) != EMAIL_ERROR_NONE) {
+                                        session->network == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED)) {
+                                       *result_stream = smtp_close(*result_stream);
+
+                                       error = emcore_refresh_xoauth2_access_token(multi_user_name, account->account_id);
+                                       if (error != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION ("emcore_refresh_xoauth2_access_token failed. [%d]", error);
                                                if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
                                                        session->error = EMAIL_ERROR_CONNECTION_FAILURE;
@@ -1055,8 +1088,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
                                        }
 
                                        sleep(2); /* wait for updating access token */
-
-                                       if (!(*result_stream = smtp_open (host_list, 1))) {
+                                       if (!(*result_stream = smtp_open(host_list, 1))) {
                                                if (session->network != EMAIL_ERROR_NONE)
                                                        session->error = session->network;
                                                if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
@@ -1065,8 +1097,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (char *mult
                                                error = session->error;
                                                goto FINISH_OFF;
                                        }
-                               }
-                               else {
+                               } else {
                                        error = EMAIL_ERROR_CONNECTION_FAILURE;
                                        goto FINISH_OFF;
                                }
@@ -1152,6 +1183,7 @@ FINISH_OFF:
 
        if (err_code)
                *err_code = error;
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
index c19567c..b9756b2 100755 (executable)
@@ -317,6 +317,8 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                                        case EMAIL_SEARCH_FILTER_TYPE_TO               :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_CHARSET          :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     :\r
                                                dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.string_type_key_value), DBUS_TYPE_INVALID);\r
                                                break;\r
 \r
index 0ea3df8..82e397b 100755 (executable)
@@ -317,7 +317,8 @@ INTERNAL_FUNC int emcore_smime_set_signed_message(char *certificate,
 
        signed_message = PKCS7_sign(NULL, NULL, other_certs, bio_mime_entity, flags);
        if (!signed_message) {
-               EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -326,19 +327,22 @@ INTERNAL_FUNC int emcore_smime_set_signed_message(char *certificate,
        digest = emcore_get_digest_algorithm(digest_type);
 
        if (!PKCS7_sign_add_signer(signed_message, cert, private_key, digest, flags)) {
-               EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
        if (!PKCS7_final(signed_message, bio_mime_entity, flags)) {
-               EM_DEBUG_EXCEPTION("PKCS7_final failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("PKCS7_final failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
        if (!i2d_PKCS7_bio_stream(smime_attachment, signed_message, bio_mime_entity, flags)) {
-               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -412,13 +416,15 @@ INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *multi_user_name,
        
        encrypt_message = PKCS7_encrypt(recipient_certs, bio_mime_entity, cipher, flags);
        if (encrypt_message == NULL) {
-               EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld]", ERR_get_error());
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
        if (!i2d_PKCS7_bio_stream(smime_attachment, encrypt_message, bio_mime_entity, flags)) {
-               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -503,7 +509,8 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_n
        /* 3. signing */
        signed_message = PKCS7_sign(NULL, NULL, other_certs, bio_mime_entity, flags);
        if (!signed_message) {
-               EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -513,7 +520,8 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_n
 
        /* 5. Apply a digest algorithm */
        if (!PKCS7_sign_add_signer(signed_message, cert, private_key, digest, flags)) {
-               EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -529,8 +537,10 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_n
                goto FINISH_OFF;
        }
 
-       if (!SMIME_write_PKCS7(bio_signed_message, signed_message, bio_mime_entity, flags | SMIME_OLDMIME | SMIME_CRLFEOL)) {
-               EM_DEBUG_EXCEPTION("SMIME_write_PKCS7 error");
+       if (!SMIME_write_PKCS7(bio_signed_message, signed_message, bio_mime_entity, 
+                                               flags | SMIME_OLDMIME | SMIME_CRLFEOL)) {
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("SMIME_write_PKCS7 error : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -559,14 +569,16 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_n
 
        encrypt_message = PKCS7_encrypt(recipient_certs, bio_signed_message, cipher, flags);
        if (encrypt_message == NULL) {
-               EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld]", ERR_get_error());
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
        
        /* 4. Write the encrypt message in file */
        if (!i2d_PKCS7_bio_stream(smime_attachment, encrypt_message, bio_mime_entity, flags)) {
-               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -649,7 +661,8 @@ INTERNAL_FUNC int emcore_smime_get_decrypt_message(char *encrypt_message,
        }
 
        if (!PKCS7_decrypt(p7_encrypt_message, private_key, cert, out_buf, 0)) {
-               EM_DEBUG_EXCEPTION("Decrpyt failed");
+               unsigned long error = ERR_get_error();
+               EM_DEBUG_EXCEPTION("Decrpyt failed : [%ld][%s]", error, ERR_error_string(error, NULL));
                err = EMAIL_ERROR_DECRYPT_FAILED;
                goto FINISH_OFF;
        }
index 449bb32..78cf724 100755 (executable)
@@ -55,6 +55,7 @@
 #include "email-debug-log.h"
 #include "email-core-gmime.h"
 #include "email-core-container.h"
+#include "email-network.h"
 
 #undef min
 
@@ -415,8 +416,6 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
        if (body->encoding == ENCQUOTEDPRINTABLE || body->encoding == ENCBASE64)
                fprintf(fp, CRLF_STRING);
 
-       fprintf(fp, CRLF_STRING);
-
 FINISH_OFF: /* prevent 34226 */
 
        /* cleanup local vars */
@@ -463,26 +462,27 @@ static int emcore_write_rfc822_body(BODY *body, BODY *root_body, FILE *fp, int *
 
                                rfc822_write_body_header(&p, &part->body);
 
-                               fprintf(fp, "--%s"CRLF_STRING, bndry);
+                               fprintf(fp, CRLF_STRING"--%s"CRLF_STRING, bndry);
                                if (body->subtype && (body->subtype[0] == 'S' || body->subtype[0] == 's')) {
                                        if ((error = emcore_write_body (body, root_body, fp)) != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
                                                return false;
                                        }
-                                       fprintf(fp, "--%s"CRLF_STRING, bndry);
+                                       fprintf(fp, CRLF_STRING"--%s"CRLF_STRING, bndry);
                                }
 
-                               fprintf(fp, "%s"CRLF_STRING, buf);
-
+                               fprintf(fp, "%s", buf);
                                emcore_write_rfc822_body(&part->body, root_body, fp, err_code);
+
                        } while ((part = part->next));
 
-                       fprintf(fp, "--%s--"CRLF_STRING, bndry);
+                       fprintf(fp, CRLF_STRING"--%s--"CRLF_STRING, bndry);
                        break;
 
                default:
                        EM_DEBUG_LOG_DEV("body->type is not TYPEMULTIPART");
 
+                       fprintf(fp, CRLF_STRING);
                        if ((error = emcore_write_body (body, root_body, fp)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emcore_write_body failed : [%d]", error);
                                return false;
@@ -553,7 +553,6 @@ static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t
        rfc822_output_header(&buf, env, body, NIL, NIL);                /*  Excluding BCC */
 
        EM_DEBUG_LOG("header_buffer [%d]", strlen(header_buffer));
-
        {
                gchar **tokens = g_strsplit(header_buffer, "CHARSET=X-UNKNOWN", 2);
 
@@ -634,8 +633,7 @@ static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t
                }
        }
 
-       SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), CRLF_STRING);
-
+//     SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), CRLF_STRING);
        if (data && EM_SAFE_STRLEN(*data) > 0) {
                fname = EM_SAFE_STRDUP(*data);
                file_exist = 1;
@@ -1123,7 +1121,17 @@ 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, mail_data->mailbox_id, converted_mail_tbl->thread_id, &updated_thread_id, 0, 0, NOTI_THREAD_ID_CHANGED_BY_ADD, false, &err)) {
+               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, 
+                                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
                        emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
@@ -1779,6 +1787,10 @@ INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_
        dst_mailbox_id = local_mailbox->mailbox_id;
        emstorage_free_mailbox(&local_mailbox, 1, NULL);
 
+       if (!emnetwork_check_network_status(&err)) {
+               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
+               goto FINISH_OFF;
+       }
 
        send_stream = emcore_get_smtp_stream (multi_user_name, account_id, &err);
        if (!send_stream) {
@@ -1936,7 +1948,12 @@ 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, local_mailbox->mailbox_id, "0", &temp_mail, false, &err)) {
+                               if (!emstorage_get_maildata_by_servermailid(multi_user_name, 
+                                                                                                                       "0", 
+                                                                                                                       local_mailbox->mailbox_id,
+                                                                                                                       &temp_mail, 
+                                                                                                                       false, 
+                                                                                                                       &err)) {
                                        if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
                                                EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid failed : [%d]", err);
                                                goto FINISH_OFF;
@@ -2090,7 +2107,7 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
        int err2 = EMAIL_ERROR_NONE;
        int status = EMAIL_SEND_FAIL;
        int *mail_ids = NULL;
-       DB_STMT handle;
+       DB_STMT handle = NULL;
        int i = 0;
        int total = 0;
        int attachment_tbl_count = 0;
@@ -2122,6 +2139,11 @@ INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id,
 /*don't delete the comment. several threads including event thread call this func */
 /*     FINISH_OFF_IF_CANCELED; */
 
+       if (!emnetwork_check_network_status(&err)) {
+               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
        opt = &(ref_account->options);
 
        if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, input_mailbox_name, &local_mailbox, false, &err)) {
@@ -2502,7 +2524,6 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
                        recipients++;
        }
 
-
        if (send_err) {
                EM_DEBUG_EXCEPTION("One or more recipients failed...");
                err = EMAIL_ERROR_INVALID_ADDRESS;
@@ -2573,14 +2594,13 @@ static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVE
                }
 
                while (total) {
-#if 0
                        /* Cancel the sending event */
                        if (!emcore_check_send_mail_thread_status()) {
                                EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
                                err = EMAIL_ERROR_CANCELLED;
                                goto FINISH_OFF;
                        }
-#endif
+
                        if (total  < allocSize)
                                dataSize = total;
                        else
@@ -3436,8 +3456,9 @@ static int emcore_make_envelope_from_mail(char *multi_user_name, emstorage_mail_
        emcore_encode_rfc2047_address(envelope->cc, &error);
        emcore_encode_rfc2047_address(envelope->bcc, &error);
 
-       if (input_mail_tbl_data->subject)
+       if (input_mail_tbl_data->subject) {
                envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
+       }
 
        char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
        rfc822_date(rfc822_date_string);
@@ -4657,7 +4678,7 @@ INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(char *multi_user_name, int
 
                trigger_at_time = current_time + input_alarm_interval_in_second;
 
-               if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_AUTO_RESEND, 0, emcore_auto_resend_cb, (void *)multi_user_name)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_AUTO_RESEND, 0, emcore_auto_resend_cb, NULL)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
                        goto FINISH_OFF;
                }
index 2d2bc0a..bf0e57b 100755 (executable)
@@ -163,9 +163,8 @@ INTERNAL_FUNC void* emcore_default_async_task_handler(void *input_param)
        }
 
 FINISH_OFF:
-       emcore_finalize_async_task_handler(task, err);
 
-       EM_SAFE_FREE(decoded_task_parameter);
+       emcore_finalize_async_task_handler(task, err);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return NULL;
@@ -198,8 +197,6 @@ INTERNAL_FUNC int emcore_default_sync_task_handler(void *intput_param)
                free(return_err);
        }
 
-       EM_SAFE_FREE(decoded_task_parameter);
-
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
index 065154d..ca7a67e 100755 (executable)
@@ -556,13 +556,26 @@ FINISH_OFF:
        if(tn)
                tpl_free(tn);
 
+       if (err != EMAIL_ERROR_NONE) {
+               EM_SAFE_FREE(result_data);
+                       
+               if (task_parameter) {
+                       EM_SAFE_FREE(task_parameter->multi_user_name);
+                       EM_SAFE_FREE(task_parameter->mail_id_array);
+                       free(task_parameter);
+               }
+       }
+
        EM_DEBUG_FUNC_END("err [%d]", err);
-       return EMAIL_ERROR_NONE;
+       return err;
 }
 
-INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct)
+INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(char *input_byte_stream, 
+                                                                                                                                                       int input_stream_size, 
+                                                                                                                                                       void **output_task_parameter_struct)
 {
-       EM_DEBUG_FUNC_BEGIN("input_byte_stream [%p] input_stream_size [%d] output_task_parameter_struct [%p]", input_byte_stream, input_stream_size, output_task_parameter_struct);
+       EM_DEBUG_FUNC_BEGIN("input_byte_stream [%p] input_stream_size [%d] output_task_parameter_struct [%p]", 
+                                               input_byte_stream, input_stream_size, output_task_parameter_struct);
        int err = EMAIL_ERROR_NONE;
        tpl_node *tn = NULL;
        tpl_bin tb_mail_id_array;
@@ -577,7 +590,6 @@ INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(c
        }
 
        task_parameter = em_malloc(sizeof(task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE));
-
        if(task_parameter == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -601,14 +613,16 @@ INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(c
        tpl_unpack(tn, 0);
 
        if(task_parameter->mail_id_count <= 0 || tb_mail_id_array.addr == NULL) {
-               EM_DEBUG_LOG("No mail id list. mail_id_count[%d] addr[%p]", task_parameter->mail_id_count, tb_mail_id_array.addr);
+               EM_DEBUG_LOG("No mail id list. mail_id_count[%d] addr[%p]", 
+                                               task_parameter->mail_id_count, tb_mail_id_array.addr);
        }
        else {
                task_parameter->mail_id_array = tb_mail_id_array.addr;
        }
 
        if(tb_value.addr) {
-               if ((err = emcore_get_mail_attribute_value_type(task_parameter->attribute_type, &attribute_value_type)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_get_mail_attribute_value_type(task_parameter->attribute_type, 
+                                                                                                               &attribute_value_type)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_attribute_value_type failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -641,11 +655,19 @@ INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(c
 
 FINISH_OFF:
 
-       if(tn)
+       if (tn)
                tpl_free(tn);
 
+       if (err != EMAIL_ERROR_NONE) {
+               if (task_parameter) {
+                       EM_SAFE_FREE(task_parameter->multi_user_name);
+                       EM_SAFE_FREE(task_parameter->mail_id_array);
+                       free(task_parameter);
+               }
+       }
+
        EM_DEBUG_FUNC_END("err [%d]", err);
-       return EMAIL_ERROR_NONE;
+       return err;
 }
 
 INTERNAL_FUNC void *task_handler_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(void *input_param)
index 2c2f66d..01522ef 100755 (executable)
@@ -251,15 +251,15 @@ int emcore_get_long_encoded_path_with_account_info(char *multi_user_name, email_
             SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d%s%s",
                 account->incoming_server_address,
                 account->incoming_server_port_number,
-                account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", 
-                account->account_id, 
-                TOKEN_FOR_MULTI_USER, 
+                account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap",
+                account->account_id,
+                TOKEN_FOR_MULTI_USER,
                 multi_user_name);
         } else {
             SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d",
                 account->incoming_server_address,
                 account->incoming_server_port_number,
-                account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", 
+                account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap",
                 account->account_id);
         }
 
@@ -298,12 +298,12 @@ int emcore_get_long_encoded_path_with_account_info(char *multi_user_name, email_
 
                if (account->outgoing_server_need_authentication > 0) {
             if (multi_user_name) {
-                           SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d%s%s", 
-                                                                                        account->account_id, 
-                                                                                        TOKEN_FOR_MULTI_USER, 
+                           SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d%s%s",
+                                                                                        account->account_id,
+                                                                                        TOKEN_FOR_MULTI_USER,
                                                                                         multi_user_name);
             } else {
-                           SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", 
+                           SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d",
                                                                                         account->account_id);
             }
 
@@ -1012,9 +1012,9 @@ void emcore_display_unread_in_badge(void *data)
        int total_mail_count = 0;
        int unseen = 0;
 
-       if (!emcore_get_mail_count_by_query((char *)data, ALL_ACCOUNT, 
-                                                                               EMAIL_MAILBOX_TYPE_INBOX, 0, 
-                                                                               &total_mail_count, &total_unread_count, 
+       if (!emcore_get_mail_count_by_query((char *)data, ALL_ACCOUNT,
+                                                                               EMAIL_MAILBOX_TYPE_INBOX, 0,
+                                                                               &total_mail_count, &total_unread_count,
                                                                                &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
                goto FINISH_OFF;
@@ -2020,8 +2020,8 @@ int emcore_get_storage_status(void)
        EM_DEBUG_FUNC_BEGIN();
        int storage_status = 0, nError = 0;
 
-#if !GLIB_CHECK_VERSION(2, 36, 0) 
-       g_type_init(); 
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
 #endif
 
 #ifdef STORAGE_STATUS
@@ -2156,7 +2156,7 @@ char *emcore_get_alias_of_mailbox(const char *mailbox_path)
 
 
        mailbox = g_strdup(mailbox_path);
-       token_list = g_strsplit_set(mailbox, "/", -1);
+       token_list = g_strsplit_set(mailbox, "/\\", -1);
 
        if(token_list == NULL) {
                EM_DEBUG_LOG("g_strsplit_set failed.");
@@ -2652,7 +2652,7 @@ int emcore_get_preview_text_from_file(char *multi_user_name, const char *input_p
        } else {
                prefix_path = strdup("");
        }
-       
+
        if (input_html_path) { /*prevent 26249*/
                /*      get preview text from html file */
                char result_buffer[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
@@ -4117,7 +4117,7 @@ static int emcore_get_next_peak_start_time(emstorage_account_tbl_t *input_accoun
        if (time_info == NULL) {
                EM_DEBUG_EXCEPTION("localtime failed");
                err = EMAIL_ERROR_SYSTEM_FAILURE;
-               goto FINISH_OFF;                
+               goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("input time and date: %s", asctime(time_info));
@@ -4191,7 +4191,7 @@ static int emcore_check_time_in_peak_schedule(emstorage_account_tbl_t *input_acc
        if (time_info == NULL) {
                EM_DEBUG_EXCEPTION("localtime failed");
                err = EMAIL_ERROR_SYSTEM_FAILURE;
-               goto FINISH_OFF;                
+               goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("input time and date: %s", asctime(time_info));
@@ -4344,7 +4344,7 @@ int emcore_get_mail_contact_info(char *multi_user_name, email_mail_contact_info_
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        if (!emcore_get_mail_contact_info_with_update(multi_user_name, contact_info, full_address, 0, &err))
                EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
        else
@@ -4627,10 +4627,10 @@ FINISH_OFF:
 }
 
 int emcore_set_contacts_log (char *multi_user_name,
-                                int   account_id, 
-                                char *email_address, 
-                                char *subject, 
-                                time_t date_time, 
+                                int   account_id,
+                                char *email_address,
+                                char *subject,
+                                time_t date_time,
                                 email_action_t action)
 {
        /* arg shall be destroyed in emcore_set_contacts_log_internal */
@@ -4669,11 +4669,11 @@ INTERNAL_FUNC int emcore_set_sent_contacts_log(char *multi_user_name, emstorage_
                        rfc822_parse_adrlist(&addr, address_array[i], NULL);
                        for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
                                SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
-                               if ((err = emcore_set_contacts_log(multi_user_name, 
-                                                    input_mail_data->account_id, 
-                                                    email_address, 
-                                                    input_mail_data->subject, 
-                                                    input_mail_data->date_time, 
+                               if ((err = emcore_set_contacts_log(multi_user_name,
+                                                    input_mail_data->account_id,
+                                                    email_address,
+                                                    input_mail_data->subject,
+                                                    input_mail_data->date_time,
                                                     EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
                                        goto FINISH_OFF;
@@ -4702,11 +4702,11 @@ INTERNAL_FUNC int emcore_set_received_contacts_log(char *multi_user_name, emstor
        EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
        int err = EMAIL_ERROR_NONE;
 
-       if ((err = emcore_set_contacts_log(multi_user_name, 
-                                        input_mail_data->account_id, 
-                                        input_mail_data->email_address_sender, 
-                                        input_mail_data->subject, 
-                                        input_mail_data->date_time, 
+       if ((err = emcore_set_contacts_log(multi_user_name,
+                                        input_mail_data->account_id,
+                                        input_mail_data->email_address_sender,
+                                        input_mail_data->subject,
+                                        input_mail_data->date_time,
                                         EMAIL_ACTION_SYNC_HEADER)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
        }
@@ -4724,7 +4724,7 @@ typedef struct _contacts_delete_data
 gboolean emcore_delete_contacts_log_internal(void* arg)
 {
        EM_DEBUG_FUNC_BEGIN();
-    
+
     contacts_delete_data *data = (contacts_delete_data *)arg;
        int contacts_error = CONTACTS_ERROR_NONE;
 
@@ -4768,7 +4768,7 @@ INTERNAL_FUNC int emcore_delete_contacts_log(char *multi_user_name, int account_
 
        g_main_context_invoke (NULL, emcore_delete_contacts_log_internal, (void*) data);
 
-       return EMAIL_ERROR_NONE;                
+       return EMAIL_ERROR_NONE;
 }
 
 INTERNAL_FUNC int emcore_get_mail_display_name (char *multi_user_name, char *email_address, char **contact_display_name)
@@ -4783,8 +4783,8 @@ INTERNAL_FUNC int emcore_get_mail_display_name (char *multi_user_name, char *ema
        GVariant *result = NULL;
        int ret = EMAIL_ERROR_NONE;
 
-#if !GLIB_CHECK_VERSION(2, 36, 0) 
-       g_type_init(); 
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
 #endif
 
        GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
@@ -4802,17 +4802,17 @@ INTERNAL_FUNC int emcore_get_mail_display_name (char *multi_user_name, char *ema
                goto FINISH_OFF;
        }
 
-       result = g_dbus_proxy_call_sync (bproxy, 
-                                      "GetDisplayName", 
-                                      g_variant_new ("(ss)", email_address, multi_user_name), 
-                                      G_DBUS_CALL_FLAGS_NONE, 
-                                      -1, 
-                                      NULL, 
+       result = g_dbus_proxy_call_sync (bproxy,
+                                      "GetDisplayName",
+                                      g_variant_new ("(ss)", email_address, multi_user_name),
+                                      G_DBUS_CALL_FLAGS_NONE,
+                                      -1,
+                                      NULL,
                                       &gerror);
 
 
        if (!result) {
-               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'GetDisplayName' error [%s]", 
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'GetDisplayName' error [%s]",
                                  gerror->message);
                ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
                goto FINISH_OFF;
@@ -4837,8 +4837,8 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_get_mail_display_name_internal (char *multi_user_name, 
-                                                        char *email_address, 
+INTERNAL_FUNC int emcore_get_mail_display_name_internal (char *multi_user_name,
+                                                        char *email_address,
                                                         char **contact_display_name)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
@@ -4915,7 +4915,7 @@ INTERNAL_FUNC int emcore_connect_contacts_service(char *multi_user_name)
     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
                if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
-                       return err;                     
+                       return err;
                }
     }
 
@@ -4943,7 +4943,7 @@ INTERNAL_FUNC int emcore_disconnect_contacts_service(char *multi_user_name)
        if (EM_SAFE_STRLEN(multi_user_name) > 0) {
                if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
-                       return err;                     
+                       return err;
                }
     }
 
@@ -5001,8 +5001,8 @@ INTERNAL_FUNC int emcore_check_blocking_mode (char *multi_user_name, char *sende
        GVariant *result = NULL;
        int ret = EMAIL_ERROR_NONE;
 
-#if !GLIB_CHECK_VERSION(2, 36, 0) 
-       g_type_init(); 
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       g_type_init();
 #endif
 
        GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
@@ -5020,17 +5020,17 @@ INTERNAL_FUNC int emcore_check_blocking_mode (char *multi_user_name, char *sende
                goto FINISH_OFF;
        }
 
-       result = g_dbus_proxy_call_sync (bproxy, 
-                                      "CheckBlockingMode", 
-                                      g_variant_new ("(ss)", sender_address, multi_user_name), 
-                                      G_DBUS_CALL_FLAGS_NONE, 
-                                      -1, 
-                                      NULL, 
+       result = g_dbus_proxy_call_sync (bproxy,
+                                      "CheckBlockingMode",
+                                      g_variant_new ("(ss)", sender_address, multi_user_name),
+                                      G_DBUS_CALL_FLAGS_NONE,
+                                      -1,
+                                      NULL,
                                       &gerror);
 
 
        if (!result) {
-               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'CheckBlockingMode' error [%s]", 
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'CheckBlockingMode' error [%s]",
                                  gerror->message);
                ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
                goto FINISH_OFF;
index 04d44e8..ce47151 100755 (executable)
@@ -154,10 +154,10 @@ INTERNAL_FUNC int emnetwork_check_network_status(int *err_code)
        int err            = EMAIL_ERROR_NONE;
        int ret            = false;
 
-       if(network_status == 0) {
+       if (network_status == 0) {
                EM_DEBUG_LOG("VCONFKEY_NETWORK_STATUS is 0");
 
-               if ( (err = _get_flight_mode(&flight_mode)) != EMAIL_ERROR_NONE) {
+               if ((err = _get_flight_mode(&flight_mode)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("_get_flight_mode failed : [%d]", err);
                        goto FINISH_OFF;
                }
@@ -168,7 +168,7 @@ INTERNAL_FUNC int emnetwork_check_network_status(int *err_code)
                        goto FINISH_OFF;                        
                }
 
-               if ( (err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
+               if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
                        goto FINISH_OFF;
                }
index e99646a..ca19a70 100755 (executable)
@@ -63,6 +63,7 @@
 #include "email-core-signal.h"
 #include "email-core-event.h"
 #include "email-core-container.h"
+#include "email-core-gmime.h"
 #include "email-core-key-manager.h"
 
 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
@@ -216,20 +217,33 @@ sqlite3 *emstorage_get_db_handle(char *multi_user_name)
        pthread_t current_thread_id = THREAD_SELF();
        sqlite3 *result_db_handle = NULL;
 
-       ENTER_CRITICAL_SECTION(_db_handle_lock);
-       for (i = 0; i < MAX_DB_CLIENT; i++) {
-        if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id))
-        {
-            if (_db_handle_list[i].user_name) {
-                if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name ? multi_user_name : "root"))
-                    continue;
-            }
-                       EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
-                       result_db_handle = _db_handle_list[i].db_handle;
-                       break;
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               ENTER_CRITICAL_SECTION(_db_handle_lock);
+               for (i = 0; i < MAX_DB_CLIENT; i++) {
+                       if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
+                               continue;
+
+                       if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
+                               EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
+                               result_db_handle = _db_handle_list[i].db_handle;
+                               break;
+                       }
+               }
+               LEAVE_CRITICAL_SECTION(_db_handle_lock);
+       } else {
+               ENTER_CRITICAL_SECTION(_db_handle_lock);
+               for (i = 0; i < MAX_DB_CLIENT; i++) {
+                       if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
+                               continue;
+
+                       if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
+                               EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
+                               result_db_handle = _db_handle_list[i].db_handle;
+                               break;
+                       }
                }
+               LEAVE_CRITICAL_SECTION(_db_handle_lock);
        }
-       LEAVE_CRITICAL_SECTION(_db_handle_lock);
 
        if (!result_db_handle)
                EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
@@ -250,7 +264,7 @@ int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
                        _db_handle_list[i].thread_id = current_thread_id;
                        _db_handle_list[i].db_handle = db_handle;
             /* Only distinguished container and host  */
-            _db_handle_list[i].user_name = strdup(multi_user_name ? multi_user_name : "root");
+            _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
                        EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
                        error_code =  EMAIL_ERROR_NONE;
                        break;
@@ -1633,18 +1647,20 @@ FINISH_OFF:
 
  /*  pData : a parameter which is registered when busy handler is registerd */
  /*  count : retry count */
-#define EMAIL_STORAGE_MAX_RETRY_COUNT 20
-
-static int _callback_sqlite_busy_handler(void *pData, int retry)
-{
-       if (retry < EMAIL_STORAGE_MAX_RETRY_COUNT) {
-               EM_DEBUG_LOG("Busy handler called!!: [%d]", retry);
-               sqlite3_sleep(200);
+static int _callback_sqlite_busy_handler(void *pData, int count)
+{
+       if (10 - count > 0) {
+               struct timespec time = {
+                       .tv_sec = 0,
+                       .tv_nsec = (count + 1) * 100 * 1000 * 1000
+               };
+               EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
+               nanosleep(&time, NULL);
                return 1;
+       } else {
+               EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
+               return 0;
        }
-
-       EM_DEBUG_EXCEPTION("Invalid Busy handler called:: [%d]", retry);
-       return 0;
 }
 
 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
@@ -1829,8 +1845,8 @@ INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *e
                        goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
        /* register busy handler */
+       EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
        rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
        if (SQLITE_OK != rc) {
                EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
@@ -3653,9 +3669,15 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, int mailbox_id, char *server_mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
+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]", mailbox_id, server_mail_id, mail, transaction, err_code);
+       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;
        char conditional_clause[QUERY_SIZE] = {0, };
@@ -3667,14 +3689,19 @@ INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
                goto FINISH_OFF;
        }
 
-       if (mailbox_id == 0)
-               SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
-       else
-               SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND mailbox_id = %d", server_mail_id, mailbox_id);
+       SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) = UPPER('%s')", 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 (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
+       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;
        }
@@ -3682,6 +3709,7 @@ INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
        ret = true;
 
 FINISH_OFF:
+
        if (ret == true)
                *mail = p_data_tbl;
 
@@ -5543,7 +5571,9 @@ 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, "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
+       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;
        }
@@ -5788,21 +5818,29 @@ 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, int *err_code)
+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]", account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
+                                               "transaction[%d], err_code[%p]", 
+                                               account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
+
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
        if (account_id <= 0 || input_mailbox_id <= 0)  {
                EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
-
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                EM_DEBUG_FUNC_END("ret [%d]", ret);
                return false;
        }
+
        sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
@@ -5826,6 +5864,7 @@ INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name, in
        ret = true;
 
 FINISH_OFF:
+
        EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
@@ -12755,10 +12794,17 @@ 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, int *thread_item_count)
+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]", mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
+       EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%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);
+
        int      rc = 0, query_size = 0, query_size_account = 0;
        int      account_id = 0;
        int      err_code = EMAIL_ERROR_NONE;
@@ -12792,7 +12838,9 @@ 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, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)  {
+       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;
                result_thread_id = -1;
@@ -12806,7 +12854,8 @@ 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_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 (account_id > 0)     {
                query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
@@ -15011,9 +15060,22 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, int account_id, int mailbox_id, 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, thread_item_count, err_code);
+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 *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, thread_item_count, err_code);
 
        int rc = -1, ret = false;
        int err = EMAIL_ERROR_NONE;
@@ -15059,8 +15121,7 @@ INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, int
 
        EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
 
-       if (thread_item_count < 0)
-       {
+       if (thread_item_count < 0) {
                memset(sql_query_string, 0, QUERY_SIZE);
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
                EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
@@ -16679,10 +16740,12 @@ FINISH_OFF:
                        *output_task_list = task_item_from_tbl;
                *output_task_count = count;
        } else {
-               for (i = 0; i < count; i++) {
-                       EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
+               if (task_item_from_tbl) {
+                       for (i = 0; i < count; i++) 
+                               EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
+                       
+                       free(task_item_from_tbl);
                }
-               EM_SAFE_FREE(task_item_from_tbl);
        }
 
        if (hStmt != NULL)  {
index da88623..abe181b 100755 (executable)
@@ -1006,7 +1006,12 @@ INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int
 
 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t **mail_list,  int *result_count,  int *err_code);
 
-INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, int mailbox_id, char *server_mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
+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);
 
 INTERNAL_FUNC int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code);
 
@@ -1499,7 +1504,17 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
 
 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code);
 
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int *updated_thread_id, int latest_mail_id, int thread_item_count, int noti_type, int transaction, int *err_code);
+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 *err_code);
 
 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count);
 
index 52b41d1..78312db 100755 (executable)
@@ -326,7 +326,12 @@ 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, int input_from_server, int *err_code);
+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 *err_code);
 
 INTERNAL_FUNC void  emcore_free_mail_data_list(email_mail_data_t **mail_list, int count);
 INTERNAL_FUNC void  emcore_free_mail_data(email_mail_data_t *mail);
@@ -363,12 +368,19 @@ INTERNAL_FUNC int   emcore_delete_rule(char *multi_user_name, int filter_id);
  * @param[in] mailbox_id                Specifies the id of mailbox
  * @param[in] input_search_filter       Specifies the filter list for searching field
  * @param[in] input_search_filter_count Specifies the filter count
+ * @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 
  */
 
-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 handle_to_be_published);
+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);
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 51ecb4a..52d4165 100755 (executable)
@@ -189,7 +189,15 @@ INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name,
                                                         int *output_inline_attachment_count);
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
-INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, email_event_partial_body_thd *pbd_event, int count, int *error);
+INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, 
+                                                                                                               email_event_partial_body_thd *pbd_event, 
+                                                                                                               int count, 
+                                                                                                               int *error);
+
+INTERNAL_FUNC int emcore_initiate_pbd(char *multi_user_name, MAILSTREAM *stream, 
+                                                                               int account_id, int mail_id, char *uid, 
+                                                                               int input_mailbox_id, char *input_mailbox_name, 
+                                                                               int *err_code);
 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
 
 
index 742e918..f1f0d42 100755 (executable)
@@ -400,8 +400,7 @@ INTERNAL_FUNC int emdaemon_validate_account_and_update(char *multi_user_name, in
        email_event_t *event_data = NULL;
 
        event_data = em_malloc(sizeof(email_event_t));
-
-       if(!event_data) { /*prevent 53094*/
+       if (!event_data) { /*prevent 53094*/
                EM_DEBUG_EXCEPTION("em_malloc failed");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
index 51d0f00..5d78990 100755 (executable)
@@ -449,7 +449,11 @@ 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, &err);
+                                                       event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data->multi_user_name, 
+                                                                                                                                                                       event_data->account_id, 
+                                                                                                                                                                       account, 
+                                                                                                                                                                       handle_to_be_published, 
+                                                                                                                                                                       &err);
                                                }
                                                break;
 
@@ -489,19 +493,25 @@ static void* worker_event_queue(void *arg)
                                }
                        }
 
-                       if ((event_data->type == EMAIL_EVENT_SYNC_HEADER || event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX) && (err != EMAIL_ERROR_NONE)) {
+                       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) {
+                                       EM_DEBUG_LOG("Unsupported account");
+                                       goto FINISH_OFF;
+                               }
+
                                /* for the retry syncing */
                                if (sync_failed_event_data) {
                                        emcore_free_event(sync_failed_event_data);
                                        EM_SAFE_FREE(sync_failed_event_data);
-                                       sync_failed_event_data = NULL;
                                }
 
                                sync_failed_event_data = em_malloc(sizeof(email_event_t));
                                if (sync_failed_event_data == NULL) {
                                        EM_DEBUG_EXCEPTION("em_malloc failed");
                                        err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
                                }
 
                                if (event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX && sync_failed_event_data) {
@@ -509,6 +519,7 @@ static void* worker_event_queue(void *arg)
                                        sync_failed_event_data->account_id         = event_data->account_id;
                                        sync_failed_event_data->status             = EMAIL_EVENT_STATUS_WAIT;
                                        sync_failed_event_data->event_param_data_3 = EM_SAFE_STRDUP(event_data->event_param_data_3);
+                                       sync_failed_event_data->multi_user_name    = EM_SAFE_STRDUP(event_data->multi_user_name);
                                }
 
                                if (event_data->type == EMAIL_EVENT_SYNC_HEADER && sync_failed_event_data) {
@@ -517,20 +528,18 @@ static void* worker_event_queue(void *arg)
                                        sync_failed_event_data->status             = EMAIL_EVENT_STATUS_WAIT;
                                        sync_failed_event_data->event_param_data_5 = event_data->event_param_data_5;
                                        sync_failed_event_data->event_param_data_4 = event_data->event_param_data_4;
+                                       sync_failed_event_data->multi_user_name    = EM_SAFE_STRDUP(event_data->multi_user_name);
                                }
                        }
-
-                       /* free internals in event */
-                       emcore_free_event(event_data); /*detected by valgrind*/
+FINISH_OFF:
+                       if (!emcore_notify_response_to_api(event_data->type, handle_to_be_published, err))
+                               EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");
 
                        if (account_tbl) {
                                emstorage_free_account(&account_tbl, 1, NULL);
                                account_tbl = NULL;
                        }
 
-                       if (!emcore_notify_response_to_api(event_data->type, handle_to_be_published, err))
-                               EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");
-
                        /* free event itself */
                        ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
                        started_event = g_queue_pop_head(g_event_que);
@@ -539,7 +548,9 @@ static void* worker_event_queue(void *arg)
                                EM_DEBUG_EXCEPTION("Failed to g_queue_pop_head");
                        }
                        else {
+                               /* freed event_data : event_data is started event */
                                emcore_return_handle(started_event->handle);
+                               emcore_free_event(started_event); /*detected by valgrind*/
                                EM_SAFE_FREE(started_event);
                        }
                }
@@ -1597,7 +1608,12 @@ 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, input_from_server, &err))
+       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
+                                                                                       input_account_id, 
+                                                                                       input_mailbox_id, 
+                                                                                       0,
+                                                                                       input_from_server, 
+                                                                                       &err))
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
 
        if (error)
@@ -1663,17 +1679,74 @@ 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, int input_search_filter_count, int handle_to_be_published)
+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, 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, handle_to_be_published);
+
        int err = EMAIL_ERROR_NONE;
-       
-       if (err == EMAIL_ERROR_NONE) {
-               if ((err = emcore_search_on_server(multi_user_name, account_id, mailbox_id, input_search_filter, input_search_filter_count, handle_to_be_published)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emcore_search_on_server failed [%d]", err);
-               }
+       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, 
+                                                                                       &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
+               goto FINISH_OFF;
        }
 
+       SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);
+
+       if (!emnetwork_check_network_status(&err)) {
+               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
+               if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, 0, err))
+                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
+               goto FINISH_OFF;
+       }
+
+       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) {
+               EM_DEBUG_EXCEPTION("emcore_search_on_server failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+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, 
+                                                                                       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, 
+                                                                                                                               0))
+                       EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");
+       }
+
+       if (local_mailbox)
+               emstorage_free_mailbox(&local_mailbox, 1, NULL);
+
        EM_DEBUG_FUNC_END();
        return err;
 }
index cc7ef17..8a1fbcb 100755 (executable)
@@ -42,6 +42,7 @@
 
 #include "email-internal-types.h"
 #include "email-daemon.h"
+#include "email-network.h"
 #include "email-core-event.h"
 #include "email-daemon-account.h"
 #include "email-debug-log.h"
@@ -103,13 +104,19 @@ INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *ha
                goto FINISH_OFF;
        }
 
+       if (!emnetwork_check_network_status(&err)) {
+               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
        if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
+
        dst_mailbox_id = local_mailbox->mailbox_id;
-       if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
+       if (mail_table_data->mailbox_id != dst_mailbox_id) {
                        /*  mail is moved to 'OUTBOX' first of all. */
                if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
@@ -118,7 +125,7 @@ INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *ha
        }
 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
 
-       if(!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
+       if (!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
 
        /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
@@ -629,9 +636,16 @@ FINISH_OFF:
        return SUCCESS;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, int *handle, int* err_code)
+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, 
+                                                                               int* err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox_id[%d], mail_ids[%p], mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]", mailbox_id, mail_ids, mail_ids_count, from_server, handle, err_code);
+       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);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
@@ -639,7 +653,6 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, in
        int thread_error = 0;
        email_account_t *ref_account = NULL;
        email_event_t *event_data = NULL;
-       emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
        thread_t delete_thread;
 
        /* mailbox can be NULL for deleting thread mail. */
@@ -649,11 +662,6 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, in
                goto FINISH_OFF;
        }
 
-       if ( (err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
-               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;
@@ -668,7 +676,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, in
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl_data->account_id, false);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -680,7 +688,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, in
        }
 
        event_data->type                   = EMAIL_EVENT_DELETE_MAIL;
-       event_data->account_id             = mailbox_tbl_data->account_id;
+       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;
@@ -692,9 +700,6 @@ INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, in
 
 FINISH_OFF:
 
-       if (mailbox_tbl_data)
-               emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
-
        if (ref_account) {
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
@@ -732,7 +737,12 @@ 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, input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
+       if (!emcore_delete_all_mails_of_mailbox(multi_user_name, 
+                                                                                       mailbox_tbl->account_id, 
+                                                                                       input_mailbox_id,
+                                                                                       0,
+                                                                                       EMAIL_DELETE_LOCALLY, 
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1153,9 +1163,12 @@ FINISH_OFF:
        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, int onserver, int* err_code)
+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]", mail_ids, num, field_type, value, err_code); /*prevent 27460*/
+       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;
        emstorage_account_tbl_t *account_tbl = NULL;
@@ -1169,7 +1182,12 @@ 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, &err)) {
+       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;
        }
@@ -1179,9 +1197,8 @@ INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id
                goto FINISH_OFF;
        }
 
-       if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
+       if (onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
                mail_id_array = em_malloc(sizeof(int) * num);
-
                if (mail_id_array == NULL)  {
                        EM_DEBUG_EXCEPTION("em_malloc failed...");
                        err = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -1226,14 +1243,20 @@ FINISH_OFF:
 
        if (err_code)
                *err_code = err;
+
        EM_DEBUG_FUNC_END();
        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, email_meeting_request_t *input_meeting_request, int input_from_eas)
+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_attachment_count, input_meeting_request, 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);
+
        int err = EMAIL_ERROR_NONE;
        /*email_event_t *event_data = NULL;*/
        email_account_t *ref_account = NULL;
@@ -1488,16 +1511,28 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int account_id = 0;
-       int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
+       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, &err) || !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;
        }
 
        mail_id_list = em_malloc(sizeof(int) * result_count);
-
        if (mail_id_list == NULL) {
                EM_DEBUG_EXCEPTION("em_malloc failed...");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -1509,11 +1544,10 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_
        }
 
        account_id = mail_list[0].account_id;
-       mailbox_id = mail_list[0].mailbox_id;
 
        // 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, mailbox_id, mail_id_list, result_count, false, handle, &err)) {
+       if (!emdaemon_delete_mail(multi_user_name, account_id, mail_id_list, result_count, false, handle, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
index fe07b99..350f0ad 100755 (executable)
 extern int g_local_activity_run;
 #endif
 
-INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(char *multi_user_name, int account_id, char* mailbox, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(char *multi_user_name, 
+                                                                                                       int account_id, 
+                                                                                                       char* mailbox, 
+                                                                                                       int *handle, 
+                                                                                                       int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d] mailbox[%p] err_code[%p]", account_id, mailbox, err_code);
 
@@ -75,7 +79,7 @@ INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(char *multi_user_name, int acco
        event_data->event_param_data_3 = EM_SAFE_STRDUP(mailbox);
        event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
-       if (!emcore_insert_event(event_data, (int*)handle, &err)) {
+       if (!emcore_insert_event(event_data, handle, &err)) {
                EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
                goto FINISH_OFF;
        }
index 09c32a6..865efc7 100755 (executable)
@@ -273,7 +273,7 @@ INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int accoun
 /**
  * Delete a mail or multiple mails.
  *
- * @param[in] mailbox_id               Specifies the mailbox.
+ * @param[in] account_id               Specifies the account.
  * @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 +284,7 @@ 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 mailbox_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 mail_id[], int num, int from_server,  int *handle, int* err_code);
 
 /**
  * Delete all mail from a mailbox.
index b4ddc9e..50101a0 100755 (executable)
@@ -1013,6 +1013,7 @@ void stb_delete_mail(HIPC_API a_hAPI)
        int *mail_ids = NULL;
     int nAPPID = emipc_get_app_id(a_hAPI);
     char *multi_user_name = NULL;
+       emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
@@ -1046,18 +1047,28 @@ void stb_delete_mail(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &from_server);
        EM_DEBUG_LOG("from_server [%d]", from_server);
 
-       emdaemon_delete_mail(multi_user_name, mailbox_id, mail_ids, num, from_server, NULL, &err);
+       err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       emdaemon_delete_mail(multi_user_name, mailbox_tbl->account_id, mail_ids, num, from_server, NULL, &err);
 
 FINISH_OFF:
+
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
-       EM_SAFE_FREE(mail_ids);
+       if (mailbox_tbl)
+               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
 
+       EM_SAFE_FREE(mail_ids);
     EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END();
 }
 
@@ -2248,7 +2259,7 @@ void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
 
-       /*need to check: why err value is changed? */
+       /* need to check: why err value is changed? */
        if(emdaemon_get_imap_mailbox_list(multi_user_name, account_id, "", &handle, &err))
                err = EMAIL_ERROR_NONE;
 
@@ -2547,17 +2558,23 @@ void stb_search_mail_on_server(HIPC_API a_hAPI)
                }
 
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, stream_for_search_filter_list);
-               em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, &search_filter_list, &search_filter_count);
+               em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, 
+                                                                                               &search_filter_list, 
+                                                                                               &search_filter_count);
                EM_SAFE_FREE(stream_for_search_filter_list);
        }
 
-       if(!emdaemon_search_mail_on_server(multi_user_name, account_id ,mailbox_id, search_filter_list, search_filter_count, &job_handle, &err))
+       if (!emdaemon_search_mail_on_server(multi_user_name, account_id ,mailbox_id, 
+                                                                               search_filter_list, 
+                                                                               search_filter_count, 
+                                                                               &job_handle, 
+                                                                               &err))
                EM_DEBUG_LOG("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 failed");
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
+       if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
 
        if (!emipc_execute_stub_api(a_hAPI))
@@ -2570,11 +2587,17 @@ void stb_search_mail_on_server(HIPC_API a_hAPI)
 void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int                      err = EMAIL_ERROR_NONE;
-       int                      account_id = 0;
-       emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
+
+       int err = EMAIL_ERROR_NONE;
+       int account_id = 0;
+       int handle = 0;
+       int *mail_id_list = NULL;
+       int mail_id_count = 0;
     int nAPPID = emipc_get_app_id(a_hAPI);
+
+       char conditional_clause[QUERY_SIZE] = {0, };
     char *multi_user_name = NULL;
+       emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
@@ -2586,19 +2609,48 @@ void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
 
        EM_DEBUG_LOG("account_id [%d]", account_id);
 
-       if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, EMAIL_SEARCH_RESULT_MAILBOX_NAME, &mailbox_tbl, false, &err)) {
-               EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed [%d]", err);
+       if (!emstorage_get_mailbox_by_name(multi_user_name, 
+                                                                               account_id, 
+                                                                               -1, 
+                                                                               EMAIL_SEARCH_RESULT_MAILBOX_NAME, 
+                                                                               &mailbox_tbl, 
+                                                                               false, 
+                                                                               &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_delete_mail_by_mailbox(multi_user_name, mailbox_tbl, true, &err))
-               EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
+       /* Create the conditional_clause */
+       SNPRINTF(conditional_clause, QUERY_SIZE, " where mailbox_type = %d ", mailbox_tbl->mailbox_type);
 
-FINISH_OFF:
+       /* Get mail ids */
+       err = emstorage_query_mail_id_list(multi_user_name,
+                                                                               conditional_clause,
+                                                                               false,
+                                                                               &mail_id_list,
+                                                                               &mail_id_count);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list failed : [%d]", err);
+               if (err == EMAIL_ERROR_MAIL_NOT_FOUND) err = EMAIL_ERROR_NONE;
+               goto FINISH_OFF;
+       }
 
-       if (mailbox_tbl) {
-               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
+       /* Remove the searched mails */
+       if (!emdaemon_delete_mail(multi_user_name, 
+                                                               account_id, 
+                                                               mail_id_list, 
+                                                               mail_id_count, 
+                                                               EMAIL_DELETE_LOCALLY, 
+                                                               &handle,
+                                                               &err)) {
+               EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed : [%d]", err);
+               goto FINISH_OFF;
        }
+/*
+       if (!emstorage_delete_mail_by_mailbox(multi_user_name, mailbox_tbl, true, &err))
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
+*/
+FINISH_OFF:
 
        if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
@@ -2606,7 +2658,12 @@ FINISH_OFF:
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+       if (mailbox_tbl) 
+               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
+
+       EM_SAFE_FREE(mail_id_list);
        EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END();
 }
 
@@ -2855,11 +2912,11 @@ void stb_get_task_information(HIPC_API a_hAPI)
 
        err = emcore_get_task_information(&task_information, &task_information_count);
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
+       if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_LOG("emipc_add_parameter failed ");
 
        /* email_task_information_t */
-       if(task_information_count) {
+       if (task_information_count) {
                task_information_stream = em_convert_task_information_to_byte_stream(task_information, task_information_count, &stream_length);
 
                if((stream_length > 0) && !emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, task_information_stream, stream_length)) {
@@ -3990,7 +4047,7 @@ INTERNAL_FUNC int kill_daemon_if_no_account()
                goto FINISH_OFF;
     }
 
-    if (g_list_length(zone_name_list) == 1) {
+    if (g_list_length(zone_name_list) <= 1) {
         if ((err = emcore_get_account_reference_list(NULL, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
             EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
         }
index 184930d..165de25 100755 (executable)
@@ -70,7 +70,7 @@ EXPORT_API void emipc_destroy_email_api(HIPC_API api)
 EXPORT_API long emipc_get_api_id(HIPC_API api)
 {
        EM_DEBUG_FUNC_BEGIN();
-       emipc_email_api_info *api_info = (emipc_email_api_info*)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;
 }
@@ -79,6 +79,7 @@ EXPORT_API long emipc_get_app_id(HIPC_API api)
 {
        EM_DEBUG_FUNC_BEGIN();
        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;
 }
 
@@ -135,14 +136,12 @@ EXPORT_API int emipc_get_parameter(HIPC_API input_api_handle, EPARAMETER_DIRECTI
        }
 
        parameters = emipc_get_api_parameters(input_api_handle, input_parameter_direction);
-
        if (parameters == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
                return EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
        }
 
        local_buffer = emipc_get_param_of_param_list(parameters, input_parameter_index);
-
        if (local_buffer == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
                return EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
@@ -167,14 +166,12 @@ EXPORT_API void* emipc_get_nth_parameter_data(HIPC_API api_handle, EPARAMETER_DI
        void *buf = NULL;
 
        parameters = emipc_get_api_parameters(api_handle, direction);
-
        if (parameters == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
                return NULL;
        }
 
        buf = emipc_get_param_of_param_list(parameters, param_index);
-
        if (!buf) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
                return NULL;
@@ -190,7 +187,7 @@ EXPORT_API int emipc_get_parameter_length(HIPC_API api, EPARAMETER_DIRECTION dir
        EM_DEBUG_FUNC_BEGIN();
        emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
        if (parameters) {
-               EM_DEBUG_FUNC_END("Suceeded");
+               EM_DEBUG_LOG("Suceeded");
                return emipc_get_param_len_of_param_list(parameters, parameter_index);
        }
        EM_DEBUG_FUNC_END("Failed");
@@ -202,7 +199,7 @@ EXPORT_API int emipc_get_nth_parameter_length(HIPC_API api, EPARAMETER_DIRECTION
        EM_DEBUG_FUNC_BEGIN();
        emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
        if (parameters) {
-               EM_DEBUG_FUNC_END("Suceeded");
+               EM_DEBUG_LOG("Suceeded");
                return emipc_get_param_len_of_param_list(parameters, parameter_index);
        }
        EM_DEBUG_FUNC_END("Failed");
index 708b955..bef1353 100755 (executable)
@@ -229,7 +229,7 @@ EXPORT_API void *emipc_get_param_of_param_list(emipc_param_list *param_list, int
 {
        EM_DEBUG_FUNC_BEGIN("index [%d]", index);
        if (index < 0 || index >= param_list->param_count) {
-               EM_DEBUG_EXCEPTION("Index value is not valid");
+//             EM_DEBUG_EXCEPTION("Index value is not valid");
                return NULL;
        }
        return emipc_get_data(param_list->params[index]);
@@ -239,7 +239,7 @@ EXPORT_API int emipc_get_param_len_of_param_list(emipc_param_list *param_list, i
 {
        EM_DEBUG_FUNC_BEGIN();
        if (index < 0 || index >= param_list->param_count) {
-               EM_DEBUG_EXCEPTION("Index valud is not valid");
+//             EM_DEBUG_EXCEPTION("Index value is not valid");
                return 0;
        }
        return emipc_get_length(param_list->params[index]);
index 32219c3..af7f296 100755 (executable)
@@ -2031,7 +2031,7 @@ static gboolean testapp_test_search_mail_on_server()
        email_search_filter_t search_filter[10];
        int handle = 0;
        time_t current_time = 0;
-       char search_key_value_string[MAX_EMAIL_ADDRESS_LENGTH];
+       char search_key_value_string[256];
 
        testapp_print("input account id : ");
        if (0 >= scanf("%d",&account_id))
@@ -2047,13 +2047,13 @@ static gboolean testapp_test_search_mail_on_server()
                testapp_print(
                        "       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       =  1,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_UID              =  2,  ( integer type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_ALL              =  3,  ( integer type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_ALL              =  3,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_BCC              =  7,  ( string type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_BODY             =  8,  ( string type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_BODY             =  8,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_CC               =  9,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FROM             = 10,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_KEYWORD          = 11,  ( string type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_TEXT             = 12,  ( string type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_TEXT             = 12,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_SUBJECT          = 13,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_TO               = 15,  ( string type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      = 16,  ( integet type ) \n"
@@ -2062,16 +2062,18 @@ static gboolean testapp_test_search_mail_on_server()
                        "       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     = 21,  ( time type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  = 22,  ( time type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   = 26,  ( integer type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        = 27,  ( integer type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        = 27,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    = 28,  ( integer type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        = 29,  ( integer type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        = 29,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      = 30,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     = 32,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     = 34,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       = 36,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       = 43,  ( string type ) \n"
-                       "       EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  = 50,  ( integer type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  = 50,  ( integer type ) \n"
                        "       EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  = 60,  ( string type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_CHARSET          = 61,  ( string type ) \n"
+                       "   EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     = 62,  ( string type ) \n"
                        "       END                                       = 0 \n");
 
                testapp_print("input search filter type : ");
@@ -2111,10 +2113,14 @@ static gboolean testapp_test_search_mail_on_server()
                        case EMAIL_SEARCH_FILTER_TYPE_TO               :
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
                        case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
+                       case EMAIL_SEARCH_FILTER_TYPE_CHARSET          :
+                       case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED     :
                                testapp_print("input search filter key value : ");
-                               if (0 >= scanf("%s", search_key_value_string))
+                               int readn = read(0, search_key_value_string, 256);
+                               if (readn < 0)
                                        testapp_print("Invalid input. ");
-                               search_filter[search_filter_count].search_filter_key_value.string_type_key_value = strdup(search_key_value_string);
+
+                               search_filter[search_filter_count].search_filter_key_value.string_type_key_value = strndup(search_key_value_string, readn - 1);
                                break;
 
                        case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :