Merge the tizen-2.4 54/38054/1
authorSunghyun Kwon <sh0701.kwon@samsung.com>
Fri, 10 Apr 2015 08:41:02 +0000 (17:41 +0900)
committerSunghyun Kwon <sh0701.kwon@samsung.com>
Fri, 10 Apr 2015 08:41:02 +0000 (17:41 +0900)
Change-Id: Iba2ade0baf1983a7e3fc7568aa497516b2334ef2

134 files changed:
CMakeLists.txt
LICENSE [moved from LICENSE.APLv2 with 90% similarity, mode: 0755]
LICENSE.BSD [deleted file]
email-api/CMakeLists.txt
email-api/doc/email-service_doc.h [new file with mode: 0755]
email-api/email-api-account.c
email-api/email-api-etc.c
email-api/email-api-init.c
email-api/email-api-mail.c
email-api/email-api-mailbox.c
email-api/email-api-network.c
email-api/email-api-rule.c
email-api/email-api-smime.c
email-api/include/email-api-account.h
email-api/include/email-api-etc.h
email-api/include/email-api-init.h
email-api/include/email-api-mail.h
email-api/include/email-api-mailbox.h
email-api/include/email-api-network.h
email-api/include/email-api-rule.h
email-api/include/email-api-smime.h
email-api/include/email-api.h
email-common-use/CMakeLists.txt
email-common-use/email-convert.c
email-common-use/email-utilities.c
email-common-use/include/email-debug-log.h
email-common-use/include/email-errors.h
email-common-use/include/email-internal-types.h
email-common-use/include/email-types.h
email-common-use/include/email-utilities.h
email-core/CMakeLists.txt
email-core/email-core-account.c
email-core/email-core-alarm.c
email-core/email-core-auto-download.c [new file with mode: 0755]
email-core/email-core-cert.c
email-core/email-core-container.c [new file with mode: 0644]
email-core/email-core-event.c
email-core/email-core-gmime.c [new file with mode: 0755]
email-core/email-core-imap-idle.c
email-core/email-core-imap-mailbox.c
email-core/email-core-mail.c
email-core/email-core-mailbox-sync.c
email-core/email-core-mailbox.c
email-core/email-core-mime.c
email-core/email-core-mm-callbacks.c
email-core/email-core-pgp.c [new file with mode: 0644]
email-core/email-core-signal.c
email-core/email-core-smime.c
email-core/email-core-smtp.c
email-core/email-core-sound.c [deleted file]
email-core/email-core-task-manager.c
email-core/email-core-tasks.c
email-core/email-core-timer.c
email-core/email-core-utils.c
email-core/email-device/email-device.c
email-core/email-device/include/email-device.h
email-core/email-network/email-network.c
email-core/email-network/include/email-network.h
email-core/email-storage/email-storage.c
email-core/email-storage/include/email-storage.h
email-core/include/email-core-account.h
email-core/include/email-core-alarm.h
email-core/include/email-core-auto-download.h [new file with mode: 0755]
email-core/include/email-core-cert.h [changed mode: 0644->0755]
email-core/include/email-core-container.h [new file with mode: 0644]
email-core/include/email-core-event.h
email-core/include/email-core-gmime.h [new file with mode: 0644]
email-core/include/email-core-imap-idle.h
email-core/include/email-core-imap-mailbox.h
email-core/include/email-core-mail.h
email-core/include/email-core-mailbox-sync.h
email-core/include/email-core-mailbox.h
email-core/include/email-core-mime.h
email-core/include/email-core-pgp.h [new file with mode: 0644]
email-core/include/email-core-smime.h
email-core/include/email-core-smtp.h
email-core/include/email-core-sound.h [deleted file]
email-core/include/email-core-task-manager.h
email-core/include/email-core-tasks.h
email-core/include/email-core-utils.h
email-daemon/CMakeLists.txt
email-daemon/email-daemon-account.c
email-daemon/email-daemon-auto-poll.c
email-daemon/email-daemon-emn.c
email-daemon/email-daemon-etc.c
email-daemon/email-daemon-event.c
email-daemon/email-daemon-init.c
email-daemon/email-daemon-mail.c
email-daemon/email-daemon-mailbox.c
email-daemon/include/email-daemon-auto-poll.h
email-daemon/include/email-daemon-emn.h
email-daemon/include/email-daemon-event.h
email-daemon/include/email-daemon-init.h
email-daemon/include/email-daemon.h
email-daemon/main.c
email-ipc/CMakeLists.txt
email-ipc/email-activation/email-dbus-activation.c
email-ipc/email-activation/email-service.service
email-ipc/email-activation/include/email-dbus-activation.h
email-ipc/email-ipc-api.c
email-ipc/email-ipc-api/email-ipc-api-info.c
email-ipc/email-ipc-api/email-ipc-param-list.c
email-ipc/email-ipc-api/include/email-ipc-api-info.h
email-ipc/email-ipc-api/include/email-ipc-param-list.h
email-ipc/email-ipc-proxy.c
email-ipc/email-proxy/email-proxy-main.c
email-ipc/email-proxy/email-proxy-socket.c [changed mode: 0644->0755]
email-ipc/email-proxy/include/email-proxy-socket.h
email-ipc/email-socket/email-ipc-socket.c
email-ipc/email-socket/include/email-ipc-socket.h
email-ipc/email-stub/email-stub-main.c
email-ipc/email-stub/email-stub-socket.c
email-ipc/email-stub/email-stub-task-manager.c
email-ipc/email-stub/email-stub-task.c
email-ipc/email-stub/include/email-stub-task-manager.h
email-ipc/include/email-ipc.h
email-service.pc.in
email-service_PG.h
packaging/email-service.manifest [changed mode: 0644->0755]
packaging/email-service.service [new file with mode: 0755]
packaging/email-service.spec
packaging/email.service
res/email-service.sql
utilities/CMakeLists.txt
utilities/test-application/include/testapp-gmime.h [new file with mode: 0755]
utilities/test-application/main.c
utilities/test-application/testapp-account.c
utilities/test-application/testapp-gmime.c [new file with mode: 0644]
utilities/test-application/testapp-mail.c
utilities/test-application/testapp-mailbox.c
utilities/test-application/testapp-others.c
utilities/test-application/testapp-rule.c
utilities/test-application/testapp-thread.c
utilities/test-application/testapp-utility.c

index f699d9b..c5af06b 100755 (executable)
@@ -27,19 +27,16 @@ ADD_SUBDIRECTORY(email-ipc)
 ##########################################################
 # Define STORAGE, NETWORK, CORE lib
 ##########################################################
-
 ADD_SUBDIRECTORY(email-core)
 
 ##########################################################
 # Define Email API   
 ##########################################################
-
 ADD_SUBDIRECTORY(email-api)
 
 ##########################################################
 # Define Execute File
 ##########################################################
-
 ADD_SUBDIRECTORY(email-daemon)
 
 ##########################################################
@@ -59,7 +56,7 @@ INSTALL(FILES ${CMAKE_BINARY_DIR}/email-service.pc DESTINATION ${LIB_INSTALL_DIR
 INSTALL(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/email-service DESTINATION /etc/rc.d/init.d/)
 
 # LICENSE file
-INSTALL(FILES ${CMAKE_SOURCE_DIR}/LICENSE.BSD DESTINATION /usr/share/license RENAME email-service)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/LICENSE DESTINATION /usr/share/license/email-service)
 
 INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/res/ DESTINATION ${TZ_SYS_DATA}/email/res/image FILES_MATCHING PATTERN "*.png")
 INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/res/ DESTINATION ${TZ_SYS_DATA}/email/res FILES_MATCHING PATTERN "*.sql")
old mode 100644 (file)
new mode 100755 (executable)
similarity index 90%
rename from LICENSE.APLv2
rename to LICENSE
index 74f474e..7d6a5a6
+++ b/LICENSE
@@ -201,3 +201,27 @@ Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+-------------------------------------------------------------------------------
+Copyright (c) 2005-2010, Troy D. Hanson     http://tpl.sourceforge.net
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
diff --git a/LICENSE.BSD b/LICENSE.BSD
deleted file mode 100644 (file)
index b68368e..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-Copyright (c) 2005-2010, Troy D. Hanson     http://tpl.sourceforge.net
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
-    * Redistributions of source code must retain the above copyright
-      notice, this list of conditions and the following disclaimer.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
-IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
-OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
index 4697005..15ad244 100755 (executable)
@@ -14,6 +14,7 @@ MESSAGE(">>> Build type: ${CMAKE_BUILD_TYPE}")
 
 SET(VISIBILITY "-DEXPORT_API=\"__attribute__((visibility(\\\"default\\\")))\"")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VISIBILITY} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
 
 ##########################################################
 # Define Email API   
@@ -35,15 +36,14 @@ INCLUDE_DIRECTORIES(
        ${CMAKE_SOURCE_DIR}/email-api/include
        ${CMAKE_SOURCE_DIR}/email-api/socket/include
        ${CMAKE_SOURCE_DIR}/email-ipc/include
+       ${CMAKE_SOURCE_DIR}/email-ipc/email-activation/include
        ${CMAKE_SOURCE_DIR}/email-core/include
        ${CMAKE_SOURCE_DIR}/email-core/email-storage/include
        ${CMAKE_SOURCE_DIR}/email-common-use/include
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(api_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf db-util uw-imap-toolkit) 
-
-#pkg_check_modules(api_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf db-util contacts-service2 uw-imap-toolkit) 
+pkg_check_modules(api_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf contacts-service2 uw-imap-toolkit gmime-2.6) 
 
 FOREACH(flag ${api_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
diff --git a/email-api/doc/email-service_doc.h b/email-api/doc/email-service_doc.h
new file mode 100755 (executable)
index 0000000..0625199
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef __TIZEN_EMAIL_SERVICE_DOC_H__
+#define __TIZEN_EMAIL_SERVICE_DOC_H__
+
+/**
+ * @internal
+ * @ingroup CAPI_MESSAGING_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_FRAMEWORK email-service
+ * @brief The Platform API for email functionalities.
+ * @internal
+ *
+ * @addtogroup EMAIL_SERVICE_FRAMEWORK
+ *
+ * @section EMAIL_SERVICE_FRAMEWORK_HEADER Required Header
+ *   \#include <email-api.h>
+ *
+ * @section EMAIL_SERVICE_FRAMEWORK_OVERVIEW Overview
+ * Electronic mail, most commonly abbreviated email or e-mail, is a method of exchanging digital messages. 
+ * E-mail systems are based on a store-and-forward model in which e-mail server computer systems accept, 
+ * forward, deliver and store messages on behalf of users, who only need to connect to the e-mail infrastructure, 
+ * typically an e-mail server, with a network-enabled device for the duration of message submission or retrieval.
+ * The email-service API is implemented to provide functionalities to applications that make use of email service.
+ * The email-service API can be utilized by any component in the application layer which facilitates the end user 
+ * to perform the email realted operations such as save, send, download email message and others.
+ * For Example, the email-service API’s shall be invoked by 1) Multimedia when user opts to send media file through email 
+ * 2) Email application when user tries to send an email message etc.
+ *
+ * <table>
+ * <tr>
+ *      <th>Feature</th>
+ *      <th>Description</th>
+ * </tr>
+ * <tr>
+ *      <td>Account API</td>
+ *      <td>Account API is a set of operations to manage email accounts like add, update, delete or get account related details.</td>
+ * </tr>
+ * <tr>
+ *      <td>Other API</td>
+ *      <td>Various API set for initializing and MIME operations and verifying email address. </td>
+ * </tr>
+ *  <tr>
+ *      <td>Mail API</td>
+ *      <td>Mail API is a set of operations to manage mail like add, update, delete or get mail related details.</td>
+ * </tr>
+ * <tr>
+ *      <td>Mailbox API</td>
+ *      <td>Mailbox API is a set of operations to manage email mailboxes like add, update, delete or get mailbox related details.</td>
+ * </tr>
+ * <tr>
+ *      <td>Network API</td>
+ *      <td>Network API is a set of operations to manage email send, receive and cancel related details.</td>
+ * </tr>
+ * <tr>
+ *      <td>Rule API</td>
+ *      <td>Rule API is a set of operations to manage email rules like add, get, delete or update rule related details.</td>
+ * </tr>
+ * <tr>
+ *      <td>SMIME API</td>
+ *      <td>SMIME API is a set of operations to handle SMIME data for secured email. </td>
+ * </tr>
+ * </table>
+ */
+
+#endif /* __TIZEN_EMAIL_SERVICE_DOC_H__ */
+
+
+
+
+
index 5bc0ebc..59f8b18 100755 (executable)
@@ -29,7 +29,6 @@
  *                     email-service .
  */
 
-#include "email-api.h"
 #include "string.h"
 #include "email-convert.h"
 #include "email-api-account.h"
 #include "email-core-utils.h"
 #include "email-utilities.h"
 #include "email-ipc.h"
+#include "email-dbus-activation.h"
 
 /* API - Adds the Email Account */
 EXPORT_API int email_add_account(email_account_t* account)
 {
        EM_DEBUG_API_BEGIN ("account[%p]", account);
-       char* local_account_stream = NULL;
        int size = 0;
        int err = EMAIL_ERROR_NONE;
        int ret_from_ipc = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
+       char* local_account_stream = NULL;
        HIPC_API hAPI = NULL;
 
        if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL||
@@ -56,7 +57,12 @@ EXPORT_API int email_add_account(email_account_t* account)
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if(emstorage_check_duplicated_account(account, true, &err) == false) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if (!emstorage_check_duplicated_account(multi_user_name, account, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_check_duplicated_account failed (%d) ", err);
                return err;
        }
@@ -68,26 +74,26 @@ EXPORT_API int email_add_account(email_account_t* account)
        local_account_stream = em_convert_account_to_byte_stream(account, &size);
        EM_PROXY_IF_NULL_RETURN_VALUE(local_account_stream, hAPI, EMAIL_ERROR_NULL_VALUE);
 
-       if(!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, local_account_stream, size)) {
+       if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, local_account_stream, size)) {
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
        }
        EM_DEBUG_LOG("APPID[%d], APIID [%d]", emipc_get_app_id(hAPI), emipc_get_api_id(hAPI));
 
        /* passing account information to service */
-       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);
        }
 
        /* get result from service */
-       if(ret_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err)) != EMAIL_ERROR_NONE) {
+       if ((ret_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emipc_get_parameter failed [%d]", ret_from_ipc);
                err = ret_from_ipc;
                goto FINISH_OFF;
        }
 
-       if(err == EMAIL_ERROR_NONE) {
+       if (err == EMAIL_ERROR_NONE) {
                emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &account->account_id);
        }
        else {  /*  get error code */
@@ -96,9 +102,11 @@ EXPORT_API int email_add_account(email_account_t* account)
 
 FINISH_OFF:
 
-       if(hAPI)
+       if (hAPI)
                emipc_destroy_email_api(hAPI);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -133,9 +141,11 @@ EXPORT_API int email_free_account(email_account_t** account_list, int count)
                        EM_SAFE_FREE(p[i].return_address);
                        EM_SAFE_FREE(p[i].logo_icon_path);
                        EM_SAFE_FREE(p[i].user_data);
+                       EM_SAFE_FREE(p[i].certificate_path);
                        p[i].user_data_length = 0;
                        EM_SAFE_FREE(p[i].options.display_name_from);
                        EM_SAFE_FREE(p[i].options.signature);
+                       EM_SAFE_FREE(p[i].options.alert_ringtone_path);
                }
                free(p); *account_list = NULL;
        }
@@ -179,6 +189,7 @@ EXPORT_API int email_delete_account(int account_id)
        if(ret != EMAIL_ERROR_NONE) {   /*  get error code */
                emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &err);
        }
+
        emipc_destroy_email_api(hAPI);
 
        hAPI = NULL;
@@ -191,7 +202,6 @@ EXPORT_API int email_update_account(int account_id, email_account_t* new_account
 {
        EM_DEBUG_API_BEGIN ("account_id[%d] new_account[%p]", account_id, new_account);
 
-       int ret = 0;
        int size = 0;
        int err = EMAIL_ERROR_NONE;
        int with_validation = false;
@@ -231,18 +241,14 @@ EXPORT_API int email_update_account(int account_id, email_account_t* new_account
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
        }
 
-       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &ret);
-       if(ret != EMAIL_ERROR_NONE) {
-               /*  get error code */
-               emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &err);
-       }
+       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
 
 FINISH_OFF:
 
        emipc_destroy_email_api(hAPI);
        hAPI = NULL;
 
-       EM_DEBUG_API_END ("ret[%d] err[%d]", ret, err);
+       EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
 
@@ -251,7 +257,6 @@ EXPORT_API int email_update_account_with_validation(int account_id, email_accoun
 {
        EM_DEBUG_API_BEGIN ("account_id[%d] new_account[%p]", account_id, new_account);
 
-       int ret = 0;
        int size = 0;
        int err = EMAIL_ERROR_NONE;
        int with_validation = true;
@@ -293,15 +298,12 @@ EXPORT_API int email_update_account_with_validation(int account_id, email_accoun
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
        }
 
-       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &ret);
-       if(ret != EMAIL_ERROR_NONE) {   /*  get error code */
-               emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &err);
-       }
+       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
 
        emipc_destroy_email_api(hAPI);
        hAPI = NULL;
 
-       EM_DEBUG_API_END ("ret[%d] err[%d]", ret, err);
+       EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
 
@@ -310,25 +312,22 @@ EXPORT_API int email_update_account_with_validation(int account_id, email_accoun
 EXPORT_API int email_get_account(int account_id, int pulloption, email_account_t** account)
 {
        EM_DEBUG_FUNC_BEGIN ("account_id[%d] pulloption[%d]", account_id, pulloption);
-       int ret = 0;
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_account_tbl_t *account_tbl = NULL;
 
        EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(account, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
        if (pulloption == GET_FULL_DATA)
                pulloption = EMAIL_ACC_GET_OPT_FULL_DATA;
 
-       if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, pulloption, &account_tbl, true, &err)) {
                if (err != EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", err);
                        goto FINISH_OFF;
@@ -339,7 +338,7 @@ EXPORT_API int email_get_account(int account_id, int pulloption, email_account_t
                        EM_DEBUG_LOG("change pulloption : disable password");
                }
 
-               if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
+               if (!emstorage_get_account_by_id(multi_user_name, account_id, pulloption, &account_tbl, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", err);
                        goto FINISH_OFF;
                }
@@ -353,13 +352,13 @@ EXPORT_API int email_get_account(int account_id, int pulloption, email_account_t
        memset((void*)*account, 0, sizeof(email_account_t));
        em_convert_account_tbl_to_account(account_tbl, *account);
 
-
-       ret = true;
-
 FINISH_OFF:
+
        if (account_tbl)
                emstorage_free_account(&account_tbl, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
 }
@@ -369,22 +368,20 @@ EXPORT_API int email_get_account_list(email_account_t** account_list, int* count
        EM_DEBUG_FUNC_BEGIN ();
 
        int err = EMAIL_ERROR_NONE, i = 0;
-       emstorage_account_tbl_t *temp_account_tbl = NULL;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(account_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+       emstorage_account_tbl_t *temp_account_tbl = NULL;
 
-       if (!emstorage_get_account_list(count, &temp_account_tbl , true, false, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
+       if (!emstorage_get_account_list(multi_user_name, count, &temp_account_tbl , true, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
@@ -395,6 +392,7 @@ EXPORT_API int email_get_account_list(email_account_t** account_list, int* count
                        err = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
+
                memset((void*)*account_list, 0, sizeof(email_account_t) * (*count));
                for(i = 0; i < (*count); i++)
                        em_convert_account_tbl_to_account(temp_account_tbl + i, (*account_list) + i);
@@ -402,9 +400,11 @@ EXPORT_API int email_get_account_list(email_account_t** account_list, int* count
 
 FINISH_OFF:
 
-
        if(temp_account_tbl)
                emstorage_free_account(&temp_account_tbl, (*count), NULL);
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
 
@@ -504,6 +504,7 @@ EXPORT_API int email_add_account_with_validation(email_account_t* account, int *
        int ret = -1;
        int size = 0;
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = 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)  {
@@ -511,7 +512,12 @@ EXPORT_API int email_add_account_with_validation(email_account_t* account, int *
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if(emstorage_check_duplicated_account(account, true, &err) == false) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
+
+       if(emstorage_check_duplicated_account(multi_user_name, account, true, &err) == false) {
                EM_DEBUG_EXCEPTION("emstorage_check_duplicated_account failed (%d) ", err);
                return err;
        }
@@ -596,30 +602,26 @@ EXPORT_API int email_restore_accounts_from_secure_storage(const char * file_name
        int ret = 0;
        int err = EMAIL_ERROR_NONE;
 
-       if (!file_name)  {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               return err;
-       }
-
        HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_RESTORE_ACCOUNTS);
 
        EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
 
        /* file_name */
-       if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)file_name, EM_SAFE_STRLEN(file_name)+1)) {
-               EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed ");
-               EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
+       if (file_name) {
+               if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)file_name, EM_SAFE_STRLEN(file_name)+1)) {
+                       EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed ");
+                       EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
+               }
        }
 
-       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);
        }
 
        emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &ret);
 
-       if(0==ret) {    /*  get error code */
+       if (0 == ret) { /* get error code */
                emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &err);
        }
 
@@ -630,12 +632,11 @@ EXPORT_API int email_restore_accounts_from_secure_storage(const char * file_name
        return err;
 }
 
-EXPORT_API int email_get_password_length_of_account(const int account_id, int *password_length)
+EXPORT_API int email_get_password_length_of_account(int account_id, email_get_password_length_type password_type, int *password_length)
 {
        EM_DEBUG_API_BEGIN ("account_id[%d] password_length[%p]", account_id, password_length);
        int ret = 0;
        int err = EMAIL_ERROR_NONE;
-       int password_type = 1;
 
        if (account_id <= 0 || password_length == NULL)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -675,47 +676,10 @@ EXPORT_API int email_get_password_length_of_account(const int account_id, int *p
        return err;
 }
 
-EXPORT_API int email_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
-{
-       EM_DEBUG_API_BEGIN ("domain_name[%p] result_server_info[%p]", domain_name, result_server_info);
-       int err = EMAIL_ERROR_NONE;
-
-       if (!domain_name || !result_server_info)  {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               return err;
-       }
-
-       err = emcore_query_server_info(domain_name, result_server_info);
-
-       EM_DEBUG_API_END ("err[%d]", err);
-       return err;
-}
-
-
-EXPORT_API int email_free_server_info(email_server_info_t **result_server_info)
-{
-       EM_DEBUG_API_BEGIN ("result_server_info[%p]", result_server_info);
-       int err = EMAIL_ERROR_NONE;
-
-       if (!result_server_info)  {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               return err;
-       }
-
-       err = emcore_free_server_info(result_server_info);
-
-       EM_DEBUG_API_END ("ret[%d]", err);
-       return err;
-}
-
-EXPORT_API int email_update_notification_bar(int account_id)
+EXPORT_API int email_update_notification_bar(int account_id, int total_mail_count, int unread_mail_count, int input_from_eas)
 {
-       EM_DEBUG_API_BEGIN("account_id [%d]", account_id);
+       EM_DEBUG_API_BEGIN ("account_id[%d] total_mail_count[%d] unread_mail_count[%d]", account_id, total_mail_count, unread_mail_count);
        int err = EMAIL_ERROR_NONE;
-       int total_mail_count = 0;
-       int unread_mail_count = 0;
 
        if (account_id == 0)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -744,6 +708,14 @@ EXPORT_API int email_update_notification_bar(int account_id)
                EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed");
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
        }
+
+       /* input_from_eas */
+       if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_eas, sizeof(int))) {
+               EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed");
+               EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
+       }
+
+
        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);
@@ -757,15 +729,65 @@ EXPORT_API int email_update_notification_bar(int account_id)
        return err;
 }
 
-
 EXPORT_API int email_clear_all_notification_bar()
 {
-       EM_DEBUG_FUNC_BEGIN ();
+    EM_DEBUG_API_BEGIN ();
        int err = EMAIL_ERROR_NONE;
+       int account_id = ALL_ACCOUNT;
 
-       err = emcore_clear_all_notifications();
+       HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_NOTIFICATION_BAR);
 
-       EM_DEBUG_FUNC_END ("ret[%d]", err);
+       EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
+
+       /* account_id */
+       if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
+               EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed");
+               EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
+       }
+
+       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);
+       }
+
+       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
+       emipc_destroy_email_api(hAPI);
+
+       hAPI = NULL;
+       EM_DEBUG_API_END ("ret[%d]", err);
+       return err;
+}
+
+EXPORT_API int email_clear_notification_bar(int account_id)
+{
+       EM_DEBUG_API_BEGIN ();
+       int err = EMAIL_ERROR_NONE;
+
+       if (account_id < ALL_ACCOUNT) {
+               EM_DEBUG_EXCEPTION ("EMAIL_ERROR_INVALID_PARAM");
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+
+       HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_NOTIFICATION_BAR);
+
+       EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
+
+       /* account_id */
+       if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
+               EM_DEBUG_EXCEPTION(" emipc_add_parameter account_id  failed");
+               EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
+       }
+
+       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);
+       }
+
+       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
+       emipc_destroy_email_api(hAPI);
+
+       hAPI = NULL;
+       EM_DEBUG_API_END ("ret[%d]", err);
        return err;
 }
 
@@ -774,8 +796,21 @@ EXPORT_API int email_save_default_account_id(int input_account_id)
        EM_DEBUG_API_BEGIN ("input_account_id [%d]", input_account_id);
        int err = EMAIL_ERROR_NONE;
 
-       err = emcore_save_default_account_id(input_account_id);
+       if (input_account_id < ALL_ACCOUNT) {
+               EM_DEBUG_EXCEPTION ("EMAIL_ERROR_INVALID_PARAM");
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+       
+       char *multi_user_name = NULL;
+
+       if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
+
+       err = emcore_save_default_account_id(multi_user_name, input_account_id);
 
+       EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("ret[%d]", err);
        return err;
 }
@@ -784,8 +819,16 @@ EXPORT_API int email_load_default_account_id(int *output_account_id)
 {
        EM_DEBUG_FUNC_BEGIN ("output_account_id[%p]", output_account_id);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
+
+       err = emcore_load_default_account_id(multi_user_name, output_account_id);
 
-       err = emcore_load_default_account_id(output_account_id);
+    EM_SAFE_FREE(multi_user_name);
 
        EM_DEBUG_FUNC_END ("ret[%d]", err);
        return err;
index 4c5a1cd..2c71bf2 100755 (executable)
@@ -28,7 +28,6 @@
  * @brief              This file contains the data structures and interfaces of functionalities provided by email-service .
  */
 
-#include "email-api.h"
 #include "email-types.h"
 #include "email-ipc.h"
 #include "email-debug-log.h"
@@ -37,6 +36,7 @@
 #include "email-core-mime.h"
 #include "email-convert.h"
 #include "email-utilities.h"
+#include "email-core-gmime.h"
 
 EXPORT_API int email_show_user_message(int id, email_action_t action, int error_code)
 {
@@ -100,12 +100,8 @@ EXPORT_API int email_write_mime_file(email_mail_data_t *input_mail_data, email_a
 {
        EM_DEBUG_API_BEGIN ("input_mail_data[%p] input_attachment_data[%p] input_attachment_count[%d]", input_mail_data, input_attachment_data, input_attachment_count);
 
-       int   err = EMAIL_ERROR_NONE;
-       int   size = 0;
-       int  *ret_nth_value = 0;
-       char *p_output_file_path = NULL;
-       char *mail_data_stream = NULL;
-       char *attachment_data_list_stream = NULL;
+       int err = EMAIL_ERROR_NONE;
+       int *ret_nth_value = NULL;
        HIPC_API hAPI = NULL;
 
        EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
@@ -118,75 +114,27 @@ EXPORT_API int email_write_mime_file(email_mail_data_t *input_mail_data, email_a
                goto FINISH_OFF;
        }
 
-       mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
-       if (!mail_data_stream) {
-               EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
-               err = EMAIL_ERROR_NULL_VALUE;
-               goto FINISH_OFF;
-       }
-
-       if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size)) {
-               EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data, input_attachment_count, &size);
-       if ((size > 0) && !emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
-               EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       if (*output_file_path && (size = EM_SAFE_STRLEN(*output_file_path)) > 0) {
-               EM_DEBUG_LOG_SEC("output_file_path : [%s] size : [%d]", *output_file_path, size);
-               size = size + 1;
-       } else {
-               size = 0;
-       }
-
-       if (!emipc_add_parameter(hAPI, ePARAMETER_IN, *output_file_path, size)) {
-               EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }       
-
        if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
                err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
                goto FINISH_OFF;
        }
 
-       if ((ret_nth_value = (int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 0))) {
+       if (*(ret_nth_value = (int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 0)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("email_write_mime_file failed:[%d]", *ret_nth_value);
                err = *ret_nth_value;
-
-               if (err == EMAIL_ERROR_NONE) {
-                       size = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 1);
-                       if (size > 0) {
-                               p_output_file_path = (char *)em_malloc(size);
-                               if (p_output_file_path == NULL) {
-                                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                                       goto FINISH_OFF;
-                               }
-
-                               if ((err = emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, size, p_output_file_path)) != EMAIL_ERROR_NONE) {
-                                       EM_DEBUG_EXCEPTION("emipc_get_parameter failed : [%d]", err);
-                                       goto FINISH_OFF;
-                               }
-                       }
-               }
+               goto FINISH_OFF;
        }
 
-       EM_SAFE_FREE(*output_file_path);
-       *output_file_path = EM_SAFE_STRDUP(p_output_file_path); 
+       if (!emcore_make_rfc822_file(NULL, input_mail_data, input_attachment_data, input_attachment_count, false, output_file_path,  &err)) {
+               EM_DEBUG_EXCEPTION("emcore_make_rfc822_file failed : [%d]", err);
+       }
 
 FINISH_OFF:
 
        if (hAPI)
                emipc_destroy_email_api(hAPI);
 
-       EM_SAFE_FREE(p_output_file_path);
-
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -195,12 +143,20 @@ EXPORT_API int email_delete_parsed_data(email_mail_data_t *input_mail_data)
 {
        EM_DEBUG_API_BEGIN ("mail_data[%p]", input_mail_data);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
 
-       if (!emcore_delete_parsed_data(input_mail_data, &err))
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
+
+       if (!emcore_delete_parsed_data(multi_user_name, input_mail_data, &err))
                EM_DEBUG_EXCEPTION("emcore_delete_parsed_data failed [%d]", err);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -219,4 +175,39 @@ EXPORT_API int email_get_mime_entity(char *mime_path, char **mime_entity)
        return err;
 }
 
+EXPORT_API int email_verify_email_address(char *input_email_address)
+{
+       EM_DEBUG_API_BEGIN ("input_email_address[%p]", input_email_address);
+       int err = EMAIL_ERROR_NONE;
+
+       EM_IF_NULL_RETURN_VALUE(input_email_address, EMAIL_ERROR_INVALID_PARAM);
 
+       if ((err = em_verify_email_address (input_email_address)) != EMAIL_ERROR_NONE)
+               EM_DEBUG_EXCEPTION("em_verify_email_address failed [%d]", err);
+
+       EM_DEBUG_API_END("err [%d]", err);
+       return err;
+}
+
+EXPORT_API int email_convert_mutf7_to_utf8(const char *mutf7_str, char **utf8_str)
+{
+       EM_DEBUG_API_BEGIN ("mutf7_str[%p]", mutf7_str);
+       int err = EMAIL_ERROR_NONE;
+
+       EM_IF_NULL_RETURN_VALUE(mutf7_str, EMAIL_ERROR_INVALID_PARAM);
+
+       emcore_gmime_init();
+
+       char *tmp_mutf7_str = g_strdup(mutf7_str);
+       *utf8_str = emcore_convert_mutf7_to_utf8(tmp_mutf7_str);
+       if (!(*utf8_str) || EM_SAFE_STRLEN(*utf8_str) == 0) {
+               EM_DEBUG_EXCEPTION("emcore_convert_mutf7_to_utf8 failed");
+               err = EMAIL_ERROR_UNKNOWN;
+       }
+       EM_SAFE_FREE(tmp_mutf7_str);
+
+       emcore_gmime_shutdown();
+
+       EM_DEBUG_API_END ("ret[%d]", err);
+       return err;
+}
index cfb2aa9..2adb5fb 100755 (executable)
  *                     email-service . 
  */
  
-#include "email-api.h"
 #include "string.h"
 #include "email-convert.h"
 #include "email-storage.h"
 #include "email-ipc.h"
 #include "email-core-task-manager.h"
+#include "email-core-account.h"
 #include "email-utilities.h"
+#include "email-dbus-activation.h"
+#include "email-core-container.h"
 #include <sqlite3.h>
-
+#include <gio/gio.h>
 
 EXPORT_API int email_open_db(void)
 {
        EM_DEBUG_API_BEGIN ();
        int error  = EMAIL_ERROR_NONE;
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       error = em_check_db_privilege_by_pid(getpid());
-       if (error == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_EXCEPTION ("permission denied");
-               return error;
-       }
-#endif
+    char *multi_user_name = NULL;
 
-       if (emstorage_db_open(&error) == NULL)
+    if ((error = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", error);
+        return error;
+    }
+
+       if (emstorage_db_open(multi_user_name, &error) == NULL)
                EM_DEBUG_EXCEPTION("emstorage_db_open failed [%d]", error);
-       
-       EM_DEBUG_API_END ("err[%d]", error);
 
+    EM_SAFE_FREE(multi_user_name);
+
+       EM_DEBUG_API_END ("error[%d]", error);
        return error;   
 }
 
@@ -65,11 +66,19 @@ EXPORT_API int email_close_db(void)
 {
        EM_DEBUG_API_BEGIN ();
        int error  = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
+
+    if ((error = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", error);
+        return error;
+    }
 
-       if ( !emstorage_db_close(&error)) 
+       if (!emstorage_db_close(multi_user_name, &error)) 
                EM_DEBUG_EXCEPTION("emstorage_db_close failed [%d]", error);
 
-       EM_DEBUG_API_END ("err[%d]", error);
+    EM_SAFE_FREE(multi_user_name);
+
+       EM_DEBUG_API_END ("error[%d]", error);
        return error;   
 }
 
@@ -89,12 +98,18 @@ EXPORT_API int email_service_begin(void)
        return ret;
 }
 
+extern GCancellable *cancel;
 
 EXPORT_API int email_service_end(void)
 {
        EM_DEBUG_API_BEGIN ();
        int ret = -1;
-       
+
+       if (cancel) {
+               g_cancellable_cancel (cancel);
+               while (cancel) usleep(1000000);
+       }
+
        ret = emipc_finalize_proxy();
 
        EM_DEBUG_API_END ("err[%d]", ret);
@@ -109,20 +124,20 @@ EXPORT_API int email_init_storage(void)
 {
        EM_DEBUG_API_BEGIN ();
        int error  = EMAIL_ERROR_NONE;
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       error = em_check_db_privilege_by_pid(getpid());
-       if (error == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return error;
-       }
-#endif
+    char *multi_user_name = NULL;
+
+    if ((error = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", error);
+        return error;
+    }
 
-       if (!emstorage_create_table(EMAIL_CREATE_DB_CHECK, &error))  {
+       if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error))  {
                EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
        }
 
-       EM_DEBUG_API_END ("err[%d]", error);
+    EM_SAFE_FREE(multi_user_name);
+
+       EM_DEBUG_API_END ("error[%d]", error);
        return error;
 }
 
index 579f082..4302f2d 100755 (executable)
@@ -33,8 +33,9 @@
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
-#include "email-api.h"
 #include "email-ipc.h"
+#include "email-api-init.h"
+#include "email-api-mailbox.h"
 #include "email-convert.h"
 #include "email-core-tasks.h"
 #include "email-core-utils.h"
@@ -46,7 +47,6 @@
 #include "email-core-signal.h"
 #include "email-utilities.h"
 #include "email-core-smime.h"
-#include "db-util.h"
 
 #define  DIR_SEPERATOR_CH '/'
 
@@ -71,14 +71,20 @@ EXPORT_API int email_add_mail(email_mail_data_t *input_mail_data, email_attachme
        int      *ret_nth_value = NULL;
        int       size = 0;
        char     *rfc822_file = NULL;
+    char     *multi_user_name = NULL;
        char     *mail_data_stream = NULL;
        char     *attachment_data_list_stream  = NULL;
        char     *meeting_request_stream = NULL;
        HIPC_API  hAPI = NULL;
-       
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if(input_from_eas == 0) { /* native emails */
                if ((input_mail_data->smime_type != EMAIL_SMIME_NONE) && (input_mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_DRAFT)) {
-                       if (!emcore_make_rfc822_file(input_mail_data, input_attachment_data_list, input_attachment_count, &rfc822_file, &err)) {
+                       if (!emcore_make_rfc822_file(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, true, &rfc822_file, &err)) {
                                EM_DEBUG_EXCEPTION("emcore_make_rfc822_file failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -166,7 +172,7 @@ EXPORT_API int email_add_mail(email_mail_data_t *input_mail_data, email_attachme
                        goto FINISH_OFF;
                }
        } else { /* take care of mails from eas */
-               err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false);
+               err = emcore_add_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false);
                if(err != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
                        goto FINISH_OFF;
@@ -174,9 +180,11 @@ EXPORT_API int email_add_mail(email_mail_data_t *input_mail_data, email_attachme
        }
 
 FINISH_OFF:
+
        if(hAPI) 
                emipc_destroy_email_api(hAPI);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 
@@ -224,7 +232,7 @@ EXPORT_API int email_add_read_receipt(int input_read_mail_id, int *output_receip
 
 
 #define TMP_BODY_PATH "/tmp/UTF-8"
-int email_create_db_full()
+EXPORT_API int email_create_db_full()
 {
        int mailbox_index, mail_index, mailbox_count, mail_slot_size;
        int account_id = 0;
@@ -239,15 +247,14 @@ int email_create_db_full()
                return err;
        }
 
-       if ((err = emcore_load_default_account_id(&account_id)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_load_default_account_id(NULL, &account_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_load_default_account_id failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       body_file = fopen(TMP_BODY_PATH, "w");
-       if (body_file == NULL) {
-               EM_DEBUG_EXCEPTION("fopen failed");
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(TMP_BODY_PATH, "w", &body_file);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed : [%s][%d]", TMP_BODY_PATH, err);
                goto FINISH_OFF;
        }
 
@@ -275,7 +282,8 @@ int email_create_db_full()
                        sprintf(mail_table_data.subject, "Subject #%d",mail_index);
                        mail_table_data.mailbox_id   = mailbox_list[mailbox_index].mailbox_id;
                        mail_table_data.mailbox_type = mailbox_list[mailbox_index].mailbox_type;
-                       if( !emstorage_add_mail(&mail_table_data, 1, true, &err))  {
+
+                       if( !emstorage_add_mail(NULL, &mail_table_data, 1, true, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]",err);
                
                                goto FINISH_OFF;
@@ -296,6 +304,7 @@ FINISH_OFF:
        EM_SAFE_FREE(mail_table_data.subject);
        EM_SAFE_FREE(mail_table_data.full_address_from);
        EM_SAFE_FREE(mail_table_data.full_address_to);
+       EM_SAFE_FREE(mail_table_data.file_path_plain);
 
        return err;
 }
@@ -391,7 +400,7 @@ EXPORT_API int email_update_mail(email_mail_data_t *input_mail_data, email_attac
                }
        } 
        else {
-               if( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
+               if( (err = emcore_update_mail(NULL, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -478,6 +487,7 @@ EXPORT_API int email_count_mail(email_list_filter_t *input_filter_list, int inpu
        int total_count = 0;
        int unread = 0;
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        char *conditional_clause_string = NULL;
 
        EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
@@ -485,20 +495,17 @@ EXPORT_API int email_count_mail(email_list_filter_t *input_filter_list, int inpu
        EM_IF_NULL_RETURN_VALUE(output_total_mail_count, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_unseen_mail_count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(input_filter_list, input_filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, input_filter_list, input_filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_query_mail_count(conditional_clause_string, true, &total_count, &unread)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_mail_count(multi_user_name, conditional_clause_string, true, &total_count, &unread)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -508,6 +515,8 @@ EXPORT_API int email_count_mail(email_list_filter_t *input_filter_list, int inpu
 
 FINISH_OFF:
 
+    EM_SAFE_FREE(multi_user_name);
+       EM_SAFE_FREE (conditional_clause_string); /* detected by valgrind */
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -751,22 +760,19 @@ EXPORT_API int email_query_mails(char *conditional_clause_string, email_mail_dat
        EM_DEBUG_API_BEGIN ("conditional_clause_string[%s] mail_list[%p] result_count[%p]", conditional_clause_string, mail_list, result_count);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t *result_mail_tbl = NULL;
        
        EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed :[%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, result_count, &err)) {
+       if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, true, &result_mail_tbl, result_count, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -776,9 +782,12 @@ EXPORT_API int email_query_mails(char *conditional_clause_string, email_mail_dat
        }
 
 FINISH_OFF:    
+
        if(result_mail_tbl && !emstorage_free_mail(&result_mail_tbl, *result_count, NULL))
                EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -788,24 +797,24 @@ EXPORT_API int email_query_mail_list(char *input_conditional_clause_string, emai
        EM_DEBUG_API_BEGIN ("input_conditional_clause_string[%s] output_mail_list[%p] output_result_count[%p]", input_conditional_clause_string, output_mail_list, output_result_count);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        
        EM_IF_NULL_RETURN_VALUE(input_conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed :[%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_query_mail_list(input_conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
+       if (!emstorage_query_mail_list(multi_user_name, input_conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:    
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err [%d]", err);
        return err;
 }
@@ -886,20 +895,19 @@ FINISH_OFF:
 EXPORT_API int email_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
 {
        EM_DEBUG_API_BEGIN ("input_mail_id[%d] output_attachment_data[%p] output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
-       int err = EMAIL_ERROR_NONE;
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+       int   err             = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
 
-       if((err = emcore_get_attachment_data_list(input_mail_id, output_attachment_data, output_attachment_count)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_attachment_data_list(multi_user_name, input_mail_id, output_attachment_data, output_attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -921,34 +929,34 @@ EXPORT_API int email_get_mail_list_ex(email_list_filter_t *input_filter_list, in
        EM_DEBUG_FUNC_BEGIN ("input_filter_list[%p] input_filter_count[%d] input_sorting_rule_list[%p] input_sorting_rule_count[%d] input_start_index[%d] input_limit_count[%d] output_mail_list[%p] output_result_count[%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_mail_list, output_result_count);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        char *conditional_clause_string = NULL;
 
        EM_IF_NULL_RETURN_VALUE(output_mail_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG_DEV ("conditional_clause_string[%s].", conditional_clause_string);
 
-       if(!emstorage_query_mail_list(conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
+       if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
                if (err != EMAIL_ERROR_MAIL_NOT_FOUND) /* there is no mail and it is definitely common */
                        EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:
+
        EM_SAFE_FREE(conditional_clause_string);
+    EM_SAFE_FREE(multi_user_name);
 
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
@@ -972,6 +980,7 @@ EXPORT_API int email_get_mails(int account_id , int mailbox_id, int thread_id, i
        EM_DEBUG_API_BEGIN ("account_id[%d] mailbox_id[%d] thread_id[%d]", account_id, mailbox_id, thread_id);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t *mail_tbl_list = NULL;
        EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
 
@@ -981,17 +990,13 @@ EXPORT_API int email_get_mails(int account_id , int mailbox_id, int thread_id, i
                goto FINISH_OFF;
        }
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mails(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, sorting, true, &mail_tbl_list, result_count, &err))  {
+       if (!emstorage_get_mails(multi_user_name, account_id, mailbox_id, NULL, thread_id, start_index, limit_count, sorting, true, &mail_tbl_list, result_count, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mails failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -1001,9 +1006,12 @@ EXPORT_API int email_get_mails(int account_id , int mailbox_id, int thread_id, i
        }
 
 FINISH_OFF:    
+
        if(mail_tbl_list && !emstorage_free_mail(&mail_tbl_list, *result_count, NULL))
                EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1013,6 +1021,7 @@ EXPORT_API int email_get_mail_list(int account_id , int mailbox_id, int thread_i
        EM_DEBUG_API_BEGIN ("account_id[%d] mailbox_id[%d] thread_id[%d]", account_id, mailbox_id, thread_id);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        
        EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
 
@@ -1021,21 +1030,20 @@ EXPORT_API int email_get_mail_list(int account_id , int mailbox_id, int thread_i
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, 0, NULL, sorting, true, mail_list, result_count, &err))  {
+       if (!emstorage_get_mail_list(multi_user_name, account_id, mailbox_id, NULL, thread_id, start_index, limit_count, 0, NULL, sorting, true, mail_list, result_count, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
 FINISH_OFF:    
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1045,7 +1053,7 @@ EXPORT_API int email_get_mail_by_address(int account_id , int mailbox_id, email_
 {
        EM_DEBUG_API_BEGIN ("account_id[%d] mailbox_id[%d]", account_id, mailbox_id);
        int err = EMAIL_ERROR_NONE;
-
+    char *multi_user_name = NULL;
        email_mail_list_item_t* mail_list_item = NULL;
        
        EM_IF_NULL_RETURN_VALUE(mail_list, EMAIL_ERROR_INVALID_PARAM);
@@ -1057,23 +1065,22 @@ EXPORT_API int email_get_mail_by_address(int account_id , int mailbox_id, email_
                return err;
        }
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mail_list(account_id, mailbox_id, addr_list, EMAIL_LIST_TYPE_NORMAL, start_index, limit_count, search_type, search_value, sorting, true, &mail_list_item, result_count, &err)) {
+       if (!emstorage_get_mail_list(multi_user_name, account_id, mailbox_id, addr_list, EMAIL_LIST_TYPE_NORMAL, start_index, limit_count, search_type, search_value, sorting, true, &mail_list_item, result_count, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
        *mail_list = mail_list_item;
 
 FINISH_OFF:
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1082,19 +1089,17 @@ EXPORT_API int email_get_thread_information_by_thread_id(int thread_id, email_ma
 {
        EM_DEBUG_API_BEGIN ("thread_id[%d]", thread_id);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t *mail_table_data = NULL;
        
        EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_thread_information(thread_id, &mail_table_data , true, &err)) {
+       if (!emstorage_get_thread_information(multi_user_name, thread_id, &mail_table_data , true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_thread_information  failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1109,6 +1114,8 @@ FINISH_OFF:
        if(mail_table_data && !emstorage_free_mail(&mail_table_data, 1, NULL))
                EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1117,20 +1124,18 @@ EXPORT_API int email_get_thread_information_ex(int thread_id, email_mail_list_it
 {
        EM_DEBUG_API_BEGIN ("thread_id[%d]", thread_id);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t *mail_table_data = NULL;
        email_mail_list_item_t *temp_thread_info = NULL;
        
        EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_thread_information(thread_id, &mail_table_data , true, &err)) {
+       if (!emstorage_get_thread_information(multi_user_name, thread_id, &mail_table_data , true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_thread_information -- failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1172,6 +1177,8 @@ FINISH_OFF:
        if(mail_table_data)
                emstorage_free_mail(&mail_table_data, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1180,18 +1187,18 @@ EXPORT_API int email_get_mail_data(int input_mail_id, email_mail_data_t **output
 {
        EM_DEBUG_API_BEGIN ("input_mail_id[%d]", input_mail_id);
        int err = EMAIL_ERROR_NONE;
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+    char *multi_user_name = NULL;
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
 
-       if ( ((err = emcore_get_mail_data(input_mail_id, output_mail_data)) != EMAIL_ERROR_NONE) || !output_mail_data) 
+       if ( ((err = emcore_get_mail_data(multi_user_name, input_mail_id, output_mail_data)) != EMAIL_ERROR_NONE) || !output_mail_data) 
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);    
-               
+       
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);      
        return err;
 }
@@ -1444,6 +1451,9 @@ EXPORT_API int email_free_mail_data(email_mail_data_t** mail_list, int count)
 {
        EM_DEBUG_FUNC_BEGIN ("mail_list[%p] count[%d]", mail_list, count);
        int err = EMAIL_ERROR_NONE;
+
+       EM_IF_NULL_RETURN_VALUE(mail_list, EMAIL_ERROR_INVALID_PARAM);
+
        emcore_free_mail_data_list(mail_list, count);
        EM_DEBUG_FUNC_END ("err[%d]", err);     
        return err;
@@ -1459,12 +1469,17 @@ EXPORT_API int email_cancel_sending_mail(int mail_id)
        
        int err = EMAIL_ERROR_NONE;
        int account_id = 0;
+    char *multi_user_name = NULL;
        email_mail_data_t* mail_data = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_container_name error : [%d]", err);
+        goto FINISH_OFF;
+    }
        
-       if (((err = emcore_get_mail_data(mail_id, &mail_data)) != EMAIL_ERROR_NONE) || mail_data == NULL)  {
+       if (((err = emcore_get_mail_data(multi_user_name, mail_id, &mail_data)) != EMAIL_ERROR_NONE) || mail_data == NULL)  {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1472,7 +1487,7 @@ EXPORT_API int email_cancel_sending_mail(int mail_id)
        account_id = mail_data->account_id;
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, 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;
@@ -1489,9 +1504,10 @@ EXPORT_API int email_cancel_sending_mail(int mail_id)
                }
 
                /*  noti to active sync */
-               as_noti_data.cancel_sending_mail.handle     = as_handle;
-               as_noti_data.cancel_sending_mail.mail_id    = mail_id;
-               as_noti_data.cancel_sending_mail.account_id = account_id;
+               as_noti_data.cancel_sending_mail.handle          = as_handle;
+               as_noti_data.cancel_sending_mail.mail_id         = mail_id;
+               as_noti_data.cancel_sending_mail.account_id      = account_id;
+               as_noti_data.cancel_sending_mail.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -1634,6 +1650,7 @@ EXPORT_API int email_get_address_info_list(int mail_id, email_address_info_list_
        EM_DEBUG_FUNC_BEGIN ("mail_id[%d] address_info_list[%p]", mail_id, address_info_list);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
 
        email_address_info_list_t *temp_address_info_list = NULL;
 
@@ -1643,29 +1660,28 @@ EXPORT_API int email_get_address_info_list(int mail_id, email_address_info_list_
                err = EMAIL_ERROR_INVALID_PARAM ;
                return err;
        }
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
 
-       if ( !emcore_get_mail_address_info_list(mail_id, &temp_address_info_list, &err) ) {
-               EM_DEBUG_EXCEPTION("emcore_get_mail_address_info_list failed [%d]", err);
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
+       if (!emcore_get_mail_address_info_list(multi_user_name, mail_id, &temp_address_info_list, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_get_mail_address_info_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( address_info_list ) {
+       if (address_info_list) {
                *address_info_list = temp_address_info_list;
                temp_address_info_list = NULL;
        }
 
 FINISH_OFF:
-       if ( temp_address_info_list )
+
+       if (temp_address_info_list)
                emstorage_free_address_info_list(&temp_address_info_list);
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
 }
@@ -1683,33 +1699,26 @@ EXPORT_API int email_free_address_info_list(email_address_info_list_t **address_
        return err;
 }
 
-EXPORT_API int email_get_structure(const char*encoded_string, void **struct_var, email_convert_struct_type_e type)
-{
-       EM_DEBUG_API_BEGIN ("encoded_string[%s] struct_var[%p] type[%d]", encoded_string, struct_var, type);
-       EM_DEBUG_API_END ("err[%d]", EMAIL_ERROR_NOT_IMPLEMENTED);
-       return EMAIL_ERROR_NOT_IMPLEMENTED;
-}
-
 EXPORT_API int email_query_meeting_request(char *input_conditional_clause_string, email_meeting_request_t **output_meeting_req, int *output_count)
 {
        EM_DEBUG_API_BEGIN ("input_conditional_clause_string[%s] output_meeting_req[%p] output_count[%p]", input_conditional_clause_string, output_meeting_req, output_count);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(input_conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
 
-       if ((err = emstorage_query_meeting_request(input_conditional_clause_string, output_meeting_req, output_count, true)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_meeting_request(multi_user_name, input_conditional_clause_string, output_meeting_req, output_count, true)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed [%d]", err);
        }
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1719,34 +1728,33 @@ EXPORT_API int email_get_meeting_request(int mail_id, email_meeting_request_t **
        EM_DEBUG_API_BEGIN ("mail_id[%d] meeting_req[%p]", mail_id, meeting_req);
 
        int err = EMAIL_ERROR_NONE;
-
+    char *multi_user_name = NULL;
        email_meeting_request_t *temp_meeting_req = NULL;
 
        EM_IF_NULL_RETURN_VALUE(meeting_req, EMAIL_ERROR_INVALID_PARAM);
-       if( mail_id <= 0 ) {
+       if (mail_id <= 0) {
                EM_DEBUG_EXCEPTION(" Invalid Mail Id Param ");
                err = EMAIL_ERROR_INVALID_PARAM ;
                return err;
        }
-       
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
 
-       if ( !emstorage_get_meeting_request(mail_id, &temp_meeting_req, 1, &err) ) {
-               EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed[%d]", err);
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
 
+       if (!emstorage_get_meeting_request(multi_user_name, mail_id, &temp_meeting_req, 1, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed[%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( meeting_req )
+       if (meeting_req)
                *meeting_req = temp_meeting_req;
 
 FINISH_OFF:
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -1828,6 +1836,12 @@ EXPORT_API int email_delete_thread(int thread_id, int delete_always_flag)
        EM_DEBUG_API_BEGIN ("thread_id[%d] delete_always_flag[%d]", thread_id, delete_always_flag);
        int err = EMAIL_ERROR_NONE;
 
+       if (thread_id <= 0) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+                return err;
+       }
+
        HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_THREAD);
 
        if(!hAPI) {
@@ -1860,6 +1874,7 @@ EXPORT_API int email_delete_thread(int thread_id, int delete_always_flag)
        emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
 
 FINISH_OFF:
+
        if(hAPI)
                emipc_destroy_email_api(hAPI);
 
@@ -1871,6 +1886,13 @@ EXPORT_API int email_modify_seen_flag_of_thread(int thread_id, int seen_flag, in
 {
        EM_DEBUG_API_BEGIN ("thread_id[%d] seen_flag[%d] on_server[%d]", thread_id, seen_flag, on_server);
        int err = EMAIL_ERROR_NONE;
+
+       if (thread_id <= 0) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+                return err;
+       }
+
        HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD);
 
        if(!hAPI) {
@@ -1921,10 +1943,16 @@ EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int inp
        EM_DEBUG_API_BEGIN ("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
        int err = EMAIL_ERROR_NONE;
        int return_value = 0;
+    char *multi_user_name = NULL;
        HIPC_API hAPI = NULL;
        emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
 
-       if( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if( (err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
                goto FINISH_OFF;
        }
@@ -1934,7 +1962,7 @@ EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int inp
 
        memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
 
-       if ( em_get_account_server_type_by_account_id(mailbox_tbl_data->account_id, &account_server_type, true, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mailbox_tbl_data->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;
        }
@@ -1948,10 +1976,11 @@ EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int inp
                }
 
                /*  noti to active sync */
-               as_noti_data.expunge_mails_deleted_flagged.handle        = as_handle;
-               as_noti_data.expunge_mails_deleted_flagged.account_id    = mailbox_tbl_data->account_id;
-               as_noti_data.expunge_mails_deleted_flagged.mailbox_id    = input_mailbox_id;
-               as_noti_data.expunge_mails_deleted_flagged.on_server     = input_on_server;
+               as_noti_data.expunge_mails_deleted_flagged.handle          = as_handle;
+               as_noti_data.expunge_mails_deleted_flagged.account_id      = mailbox_tbl_data->account_id;
+               as_noti_data.expunge_mails_deleted_flagged.mailbox_id      = input_mailbox_id;
+               as_noti_data.expunge_mails_deleted_flagged.on_server       = input_on_server;
+               as_noti_data.expunge_mails_deleted_flagged.multi_user_name = multi_user_name;
 
                return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED, &as_noti_data);
 
@@ -2010,6 +2039,7 @@ FINISH_OFF:
                emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
index ccd01cf..2c55ff6 100755 (executable)
@@ -30,7 +30,6 @@
  *                     email-service . 
  */
  
-#include "email-api.h"
 #include "string.h"
 #include "email-convert.h"
 #include "email-storage.h"
@@ -38,7 +37,6 @@
 #include "email-core-signal.h"
 #include "email-utilities.h"
 #include "email-ipc.h"
-#include "db-util.h"
 
 /* API - Create a mailbox */
 
@@ -49,16 +47,22 @@ EXPORT_API int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, in
        int size = 0;
        int err = EMAIL_ERROR_NONE;
        char* local_mailbox_stream = NULL;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
        EM_IF_NULL_RETURN_VALUE(new_mailbox, EMAIL_ERROR_INVALID_PARAM);
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(new_mailbox->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, new_mailbox->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;
@@ -74,12 +78,13 @@ EXPORT_API int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, in
                }
 
                /*  noti to active sync */
-               as_noti_data.add_mailbox.handle        = as_handle;
-               as_noti_data.add_mailbox.account_id    = new_mailbox->account_id;
-               as_noti_data.add_mailbox.mailbox_alias = new_mailbox->alias;
-               as_noti_data.add_mailbox.mailbox_path  = new_mailbox->mailbox_name;
-               as_noti_data.add_mailbox.eas_data      = new_mailbox->eas_data;
+               as_noti_data.add_mailbox.handle          = as_handle;
+               as_noti_data.add_mailbox.account_id      = new_mailbox->account_id;
+               as_noti_data.add_mailbox.mailbox_alias   = new_mailbox->alias;
+               as_noti_data.add_mailbox.mailbox_path    = new_mailbox->mailbox_name;
+               as_noti_data.add_mailbox.eas_data        = new_mailbox->eas_data;
                as_noti_data.add_mailbox.eas_data_length = new_mailbox->eas_data_length;
+               as_noti_data.add_mailbox.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_ADD_MAILBOX, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -131,6 +136,7 @@ EXPORT_API int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, in
 
 FINISH_OFF:
        EM_SAFE_FREE(local_mailbox_stream);
+    EM_SAFE_FREE(multi_user_name);
 
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
@@ -141,6 +147,7 @@ EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_na
        EM_DEBUG_API_BEGIN ("input_mailbox_id[%d] input_mailbox_name[%p] input_mailbox_alias[%p] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_mailbox_name, input_mailbox_alias, input_on_server, output_handle);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
@@ -150,13 +157,18 @@ EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_na
        EM_IF_NULL_RETURN_VALUE(input_mailbox_name,  EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(input_mailbox_alias, EMAIL_ERROR_INVALID_PARAM);
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mailbox_tbl->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;
@@ -172,13 +184,14 @@ EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_na
                }
 
                /*  noti to active sync */
-               as_noti_data.rename_mailbox.handle        = as_handle;
-               as_noti_data.rename_mailbox.account_id    = mailbox_tbl->account_id;
-               as_noti_data.rename_mailbox.mailbox_id    = input_mailbox_id;
-               as_noti_data.rename_mailbox.mailbox_name  = input_mailbox_name;
-               as_noti_data.rename_mailbox.mailbox_alias = input_mailbox_alias;
+               as_noti_data.rename_mailbox.handle          = as_handle;
+               as_noti_data.rename_mailbox.account_id      = mailbox_tbl->account_id;
+               as_noti_data.rename_mailbox.mailbox_id      = input_mailbox_id;
+               as_noti_data.rename_mailbox.mailbox_name    = input_mailbox_name;
+               as_noti_data.rename_mailbox.mailbox_alias   = input_mailbox_alias;
                as_noti_data.rename_mailbox.eas_data        = NULL;
                as_noti_data.rename_mailbox.eas_data_length = 0;
+               as_noti_data.rename_mailbox.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RENAME_MAILBOX, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -186,7 +199,7 @@ EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_na
                        goto FINISH_OFF;
                }
 
-               if(output_handle)
+               if (output_handle)
                        *output_handle = as_handle;
        }
        else {
@@ -228,6 +241,7 @@ FINISH_OFF:
        if (mailbox_tbl)
                emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -237,6 +251,7 @@ EXPORT_API int email_rename_mailbox_ex(int input_mailbox_id, char *input_mailbox
        EM_DEBUG_API_BEGIN ("input_mailbox_id[%d] input_mailbox_name[%p] input_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_mailbox_name, input_mailbox_alias, input_eas_data, input_eas_data_length, input_on_server, output_handle);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
@@ -246,13 +261,18 @@ EXPORT_API int email_rename_mailbox_ex(int input_mailbox_id, char *input_mailbox
        EM_IF_NULL_RETURN_VALUE(input_mailbox_name,  EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(input_mailbox_alias, EMAIL_ERROR_INVALID_PARAM);
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mailbox_tbl->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;
@@ -275,6 +295,7 @@ EXPORT_API int email_rename_mailbox_ex(int input_mailbox_id, char *input_mailbox
                as_noti_data.rename_mailbox.mailbox_alias   = input_mailbox_alias;
                as_noti_data.rename_mailbox.eas_data        = input_eas_data;
                as_noti_data.rename_mailbox.eas_data_length = input_eas_data_length;
+        as_noti_data.rename_mailbox.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RENAME_MAILBOX, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -329,6 +350,7 @@ FINISH_OFF:
        if (mailbox_tbl)
                emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -341,6 +363,7 @@ EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, i
        EM_DEBUG_API_BEGIN ("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
        
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
        HIPC_API hAPI = NULL;
@@ -348,13 +371,18 @@ EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, i
        
        EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
 
-       if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if ( (err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
                goto FINISH_OFF;
        }
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mailbox_tbl->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;
@@ -370,9 +398,10 @@ EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, i
                }
 
                /*  noti to active sync */
-               as_noti_data.delete_mailbox.handle        = as_handle;
-               as_noti_data.delete_mailbox.account_id    = mailbox_tbl->account_id;
-               as_noti_data.delete_mailbox.mailbox_id    = input_mailbox_id;
+               as_noti_data.delete_mailbox.handle          = as_handle;
+               as_noti_data.delete_mailbox.account_id      = mailbox_tbl->account_id;
+               as_noti_data.delete_mailbox.mailbox_id      = input_mailbox_id;
+        as_noti_data.delete_mailbox.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -419,6 +448,7 @@ FINISH_OFF:
                emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -427,6 +457,7 @@ EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_
 {
        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;
        task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX task_parameter;
 
@@ -436,8 +467,13 @@ EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_
                goto FINISH_OFF;
        }
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(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;
@@ -459,6 +495,7 @@ EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_
                as_noti_data.delete_mailbox_ex.mailbox_id_array  = input_mailbox_id_array;
                as_noti_data.delete_mailbox_ex.mailbox_id_count  = input_mailbox_id_count;
                as_noti_data.delete_mailbox_ex.on_server         = input_on_server;
+               as_noti_data.delete_mailbox_ex.multi_user_name   = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -563,16 +600,20 @@ EXPORT_API int email_get_sync_mailbox_list(int account_id, email_mailbox_t** mai
        int mailbox_count = 0;
        int err = EMAIL_ERROR_NONE ;
        int i = 0;
+    char *multi_user_name = NULL;
        emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL;
 
        EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-       if (!emstorage_get_mailbox_list(account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
-
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
+       if (!emstorage_get_mailbox_list(multi_user_name, account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
                goto FINISH_OFF;
        } else
                err = EMAIL_ERROR_NONE;
@@ -590,9 +631,12 @@ EXPORT_API int email_get_sync_mailbox_list(int account_id, email_mailbox_t** mai
        
        *count = mailbox_count;
        
-       FINISH_OFF:
+FINISH_OFF:
+
        if (mailbox_tbl_list != NULL)
                emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -606,22 +650,19 @@ EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, ema
        emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
        int err =EMAIL_ERROR_NONE;
        int i;
+    char *multi_user_name = NULL;
        
        EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mailbox_list(account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {
+       if (!emstorage_get_mailbox_list(multi_user_name, account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
-
                goto FINISH_OFF;
        } else
                err = EMAIL_ERROR_NONE;
@@ -641,15 +682,16 @@ EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, ema
        *count = mailbox_count;
 
 FINISH_OFF:
+
        if (mailbox_tbl_list != NULL)
                emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err [%d]", err);
        return err;
 }
 
-
-
 EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type, int with_count, email_mailbox_t** mailbox_list, int* count)
 {
        EM_DEBUG_FUNC_BEGIN (); 
@@ -658,22 +700,19 @@ EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type,
        emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
        int err =EMAIL_ERROR_NONE;
        int i;
-       
+    char *multi_user_name = NULL;
+
        EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mailbox_list_ex(account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {        
+       if (!emstorage_get_mailbox_list_ex(multi_user_name, account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {       
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list_ex failed [%d]", err);
-
                goto FINISH_OFF;
        } else
                err = EMAIL_ERROR_NONE;
@@ -695,9 +734,12 @@ EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type,
                *count = mailbox_count;
 
 FINISH_OFF:
+
        if (mailbox_tbl_list != NULL)
                emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
 }
@@ -710,19 +752,17 @@ EXPORT_API int email_get_mailbox_list_by_keyword(int account_id, char *keyword,
        emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL;
        int err = EMAIL_ERROR_NONE;
        int i = 0;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_mailbox_by_keyword(account_id, keyword, &mailbox_tbl_list, &mailbox_count, true, &err)) {
+       if (!emstorage_get_mailbox_by_keyword(multi_user_name, account_id, keyword, &mailbox_tbl_list, &mailbox_count, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_keyword failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -747,6 +787,8 @@ FINISH_OFF:
        if (mailbox_tbl_list != NULL)
                emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -756,26 +798,23 @@ EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_t
        EM_DEBUG_FUNC_BEGIN ();
        
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_mailbox_t* curr_mailbox = NULL;
        emstorage_mailbox_tbl_t* local_mailbox = NULL;
 
        EM_IF_NULL_RETURN_VALUE(mailbox, EMAIL_ERROR_INVALID_PARAM);    
        EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM)  ;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if(mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED)
                return EMAIL_ERROR_INVALID_PARAM;
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
-
-       if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &local_mailbox, true, &err))  {
+       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, mailbox_type, &local_mailbox, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
-
                goto FINISH_OFF;
        } else {
                err = EMAIL_ERROR_NONE;
@@ -785,10 +824,14 @@ EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_t
        }
 
        *mailbox = curr_mailbox;        
+
 FINISH_OFF:
 
        if(local_mailbox)
                emstorage_free_mailbox(&local_mailbox, 1, NULL);
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END ("err[%d]", err);
        return err;
 }
@@ -798,20 +841,18 @@ EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailb
        EM_DEBUG_API_BEGIN ();
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_mailbox_t* curr_mailbox = NULL;
        emstorage_mailbox_tbl_t* local_mailbox = NULL;
 
        EM_IF_NULL_RETURN_VALUE(output_mailbox, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
 
-       if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                return err;
        } else {
@@ -826,6 +867,8 @@ EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailb
 
        emstorage_free_mailbox(&local_mailbox, 1, &err);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -886,18 +929,18 @@ EXPORT_API int email_stamp_sync_time_of_mailbox(int input_mailbox_id)
        EM_DEBUG_API_BEGIN ("input_mailbox_id[%d]", input_mailbox_id);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        return err;
+    }
+
+       err = emstorage_stamp_last_sync_time_of_mailbox(multi_user_name, input_mailbox_id, 1);
 
-       err = emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_id, 1);
+    EM_SAFE_FREE(multi_user_name);
 
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
@@ -909,6 +952,8 @@ EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count)
        int err = EMAIL_ERROR_NONE;
 
        if (count <= 0 || !mailbox_list || !*mailbox_list) {
+               EM_DEBUG_LOG("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
index 1054660..6ce2afc 100755 (executable)
@@ -30,7 +30,6 @@
  *                     email-service . 
  */
  
-#include "email-api.h"
 #include "string.h"
 #include "email-convert.h"
 #include "email-api-mailbox.h"
@@ -46,18 +45,24 @@ EXPORT_API int email_send_mail(int mail_id, int *handle)
        EM_DEBUG_API_BEGIN ("mail_id[%d] handle[%p]", mail_id, handle);
        
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if(mail_id <= 0) {
                EM_DEBUG_EXCEPTION("mail_id is not valid");
-               err= EMAIL_ERROR_INVALID_PARAM;
+               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data) {
+       if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
@@ -73,7 +78,7 @@ EXPORT_API int email_send_mail(int mail_id, int *handle)
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mail_table_data->account_id, &account_server_type, false, &err) == false ) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->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;
@@ -88,9 +93,10 @@ EXPORT_API int email_send_mail(int mail_id, int *handle)
                }
                
                /*  noti to active sync */
-               as_noti_data.send_mail.handle     = as_handle;
-               as_noti_data.send_mail.account_id = mail_table_data->account_id;
-               as_noti_data.send_mail.mail_id    = mail_id;
+               as_noti_data.send_mail.handle          = as_handle;
+               as_noti_data.send_mail.account_id      = mail_table_data->account_id;
+               as_noti_data.send_mail.mail_id         = mail_id;
+        as_noti_data.send_mail.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -136,6 +142,8 @@ FINISH_OFF:
 
        emstorage_free_mail(&mail_table_data, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;     
 }
@@ -145,18 +153,24 @@ EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int
        EM_DEBUG_API_BEGIN ("input_mail_id[%d] output_handle[%p]", input_mail_id, output_handle);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL task_parameter;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if(input_mail_id <= 0) {
                EM_DEBUG_EXCEPTION("mail_id is not valid");
                err= EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
+       if(!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
@@ -170,7 +184,7 @@ EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int
        EM_DEBUG_LOG("mail_table_data->account_id[%d], mail_table_data->mailbox_id[%d]", mail_table_data->account_id, mail_table_data->mailbox_id);
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mail_table_data->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->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;
@@ -189,9 +203,10 @@ EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int
                }
 
                /*  noti to active sync */
-               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.handle     = as_handle;
-               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.mail_id    = input_mail_id;
-               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.account_id = mail_table_data->account_id;
+               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.handle          = as_handle;
+               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.mail_id         = input_mail_id;
+               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.account_id      = mail_table_data->account_id;
+               as_noti_data.send_mail_with_downloading_attachment_of_original_mail.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -217,6 +232,8 @@ FINISH_OFF:
 
        emstorage_free_mail(&mail_table_data, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -226,24 +243,30 @@ EXPORT_API int email_schedule_sending_mail(int input_mail_id, time_t input_sched
        EM_DEBUG_API_BEGIN ("mail_id[%d] input_time[%d]", input_mail_id, input_scheduled_time);
 
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL task_parameter;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if(input_mail_id <= 0) {
                EM_DEBUG_EXCEPTION("mail_id is not valid");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
+       if(!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
 
        /*  check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(mail_table_data->account_id, &account_server_type, false, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->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;
@@ -262,10 +285,11 @@ EXPORT_API int email_schedule_sending_mail(int input_mail_id, time_t input_sched
                }
 
                /*  noti to active sync */
-               as_noti_data.schedule_sending_mail.handle         = as_handle;
-               as_noti_data.schedule_sending_mail.account_id     = mail_table_data->account_id;
-               as_noti_data.schedule_sending_mail.mail_id        = input_mail_id;
-               as_noti_data.schedule_sending_mail.scheduled_time = input_scheduled_time;
+               as_noti_data.schedule_sending_mail.handle          = as_handle;
+               as_noti_data.schedule_sending_mail.account_id      = mail_table_data->account_id;
+               as_noti_data.schedule_sending_mail.mail_id         = input_mail_id;
+               as_noti_data.schedule_sending_mail.scheduled_time  = input_scheduled_time;
+        as_noti_data.schedule_sending_mail.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -289,6 +313,8 @@ FINISH_OFF:
 
        emstorage_free_mail(&mail_table_data, 1, NULL);
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -334,6 +360,7 @@ EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int
 {
        EM_DEBUG_API_BEGIN ("input_account_id[%d] input_mailbox_id[%d] handle[%p]", input_account_id, input_mailbox_id, handle);
        int err = EMAIL_ERROR_NONE;     
+    char *multi_user_name = NULL;
        /* int total_count = 0; */
        
        EM_IF_ACCOUNT_ID_NULL(input_account_id, EMAIL_ERROR_INVALID_PARAM);
@@ -343,8 +370,13 @@ EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int
        ASNotiData as_noti_data;
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        /*  2010/02/12 ch715.lee : check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(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;
        }
@@ -357,18 +389,19 @@ EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int
                }
                
                /*  noti to active sync */
-               as_noti_data.sync_header.handle = as_handle;
-               as_noti_data.sync_header.account_id = input_account_id;
+               as_noti_data.sync_header.handle          = as_handle;
+               as_noti_data.sync_header.account_id      = input_account_id;
                /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
-               as_noti_data.sync_header.mailbox_id = input_mailbox_id;
+               as_noti_data.sync_header.mailbox_id      = input_mailbox_id;
+        as_noti_data.sync_header.multi_user_name = multi_user_name;
 
-               if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
+               if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
                        err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
                        goto FINISH_OFF;
                }
 
-               if(handle)
+               if (handle)
                        *handle = as_handle;
 
        }
@@ -406,9 +439,12 @@ EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int
        }
 
 FINISH_OFF:
+
        emipc_destroy_email_api(hAPI);
        hAPI = NULL;
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;
 }
@@ -418,6 +454,7 @@ 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;
        int return_handle;
@@ -428,6 +465,11 @@ EXPORT_API int email_sync_header_for_all_account(int *handle)
        int input_account_id = ALL_ACCOUNT;
        int input_mailbox_id = 0; /* all case */
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);  
 
        EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
@@ -457,14 +499,13 @@ EXPORT_API int email_sync_header_for_all_account(int *handle)
         if (err != EMAIL_ERROR_NONE)
                 goto FINISH_OFF;
 
-          emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &return_handle);
+    emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &return_handle);
 
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
        /*  Get all accounts for sending notification to active sync engine. */
-       if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &as_err)) {
+       if (!emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_array , true, false, &as_err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ]  ", as_err);
-
                goto FINISH_OFF;
        }
 
@@ -480,10 +521,11 @@ EXPORT_API int email_sync_header_for_all_account(int *handle)
                        */
                        
                        /*  noti to active sync */
-                       as_noti_data.sync_header.handle = return_handle;
-                       as_noti_data.sync_header.account_id = account_tbl_array[i].account_id;
+                       as_noti_data.sync_header.handle          = return_handle;
+                       as_noti_data.sync_header.account_id      = account_tbl_array[i].account_id;
                        /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
-                       as_noti_data.sync_header.mailbox_id = 0;
+                       as_noti_data.sync_header.mailbox_id      = 0;
+            as_noti_data.sync_header.multi_user_name = multi_user_name;
 
                        if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
                                EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -503,7 +545,8 @@ FINISH_OFF:
 
        if ( account_tbl_array )
                emstorage_free_account(&account_tbl_array, account_count, NULL);
-       
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;
 }
@@ -517,6 +560,13 @@ EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
+    char *multi_user_name = NULL;
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
        if(mail_id <= 0) {
@@ -525,7 +575,7 @@ EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
+       if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
@@ -538,7 +588,7 @@ EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle
        account_id = mail_table_data->account_id;
                
        /*  2010/02/12 ch715.lee : check account bind type and branch off  */
-       if ( em_get_account_server_type_by_account_id(account_id, &account_server_type, true, &err) == false ) {
+       if ( em_get_account_server_type_by_account_id(multi_user_name, 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;
        }
@@ -551,10 +601,11 @@ EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle
                }
                
                /*  noti to active sync */
-               as_noti_data.download_body.handle = as_handle;
-               as_noti_data.download_body.account_id = account_id;
-               as_noti_data.download_body.mail_id = mail_id;
+               as_noti_data.download_body.handle          = as_handle;
+               as_noti_data.download_body.account_id      = account_id;
+               as_noti_data.download_body.mail_id         = mail_id;
                as_noti_data.download_body.with_attachment = with_attachment;
+        as_noti_data.download_body.multi_user_name = multi_user_name;
 
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_BODY, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -595,11 +646,9 @@ EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle
                 if (err != EMAIL_ERROR_NONE)           
                         goto FINISH_OFF;
                 
-                if(handle)     
-                {
+                if(handle)     {
                        emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
                        EM_DEBUG_LOG("RETURN VALUE : %d  handle %d", err, *handle);
-
                 }
        }
 
@@ -611,6 +660,8 @@ FINISH_OFF:
                emstorage_free_mail(&mail_table_data, 1, &err);
        }
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;
 
@@ -629,6 +680,13 @@ EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
        ASNotiData as_noti_data;
+    char *multi_user_name = NULL;
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
        if(mail_id <= 0) {
@@ -637,7 +695,7 @@ EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
+       if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
@@ -648,8 +706,8 @@ EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
        }
 
        account_id = mail_table_data->account_id;
-       
-       if ( em_get_account_server_type_by_account_id(account_id, &account_server_type, true, &err) == false ) {
+
+       if ( em_get_account_server_type_by_account_id(multi_user_name, 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;
        }
@@ -662,10 +720,12 @@ EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
                }
                
                /*  noti to active sync */
-               as_noti_data.download_attachment.handle = as_handle;
-               as_noti_data.download_attachment.account_id = account_id;
-               as_noti_data.download_attachment.mail_id = mail_id;
+               as_noti_data.download_attachment.handle           = as_handle;
+               as_noti_data.download_attachment.account_id       = account_id;
+               as_noti_data.download_attachment.mail_id          = mail_id;
                as_noti_data.download_attachment.attachment_order = nth;
+        as_noti_data.download_attachment.multi_user_name  = multi_user_name;
+
                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT, &as_noti_data) == false) {
                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
                        err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
@@ -721,6 +781,8 @@ FINISH_OFF:
                emstorage_free_mail(&mail_table_data, 1, &err);
        }
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;
        
@@ -736,30 +798,36 @@ EXPORT_API int email_cancel_job(int input_account_id, int input_handle, email_ca
        ASNotiData as_noti_data;
        emstorage_account_tbl_t *account_list = NULL;
        int i, account_count = 0;
+    char *multi_user_name = NULL;
 
        if(input_account_id < 0)
                return EMAIL_ERROR_INVALID_PARAM;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        if ( input_account_id == ALL_ACCOUNT ) {
                /*  this means that job is executed with all account */
                /*  Get all accounts for sending notification to active sync engine. */
-               if (!emstorage_get_account_list(&account_count, &account_list , true, false, &err)) {
+               if (!emstorage_get_account_list(multi_user_name, &account_count, &account_list , true, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                for(i = 0; i < account_count; i++) {
-                       if ( em_get_account_server_type_by_account_id(account_list[i].account_id, &account_server_type, true, &err) == false ) {
+                       if ( em_get_account_server_type_by_account_id(multi_user_name, account_list[i].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 ) {
                                memset(&as_noti_data, 0x00, sizeof(ASNotiData));
-                               as_noti_data.cancel_job.account_id  = account_list[i].account_id;
-                               as_noti_data.cancel_job.handle      = input_handle;
-                               as_noti_data.cancel_job.cancel_type = input_cancel_type;
-
+                               as_noti_data.cancel_job.account_id      = account_list[i].account_id;
+                               as_noti_data.cancel_job.handle          = input_handle;
+                               as_noti_data.cancel_job.cancel_type     = input_cancel_type;
+                as_noti_data.cancel_job.multi_user_name = multi_user_name;
 
                                if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
                                        EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -802,15 +870,16 @@ EXPORT_API int email_cancel_job(int input_account_id, int input_handle, email_ca
                hAPI = NULL;
        }
        else {
-               if ( em_get_account_server_type_by_account_id(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 ) {
                        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
-                       as_noti_data.cancel_job.account_id = input_account_id;
-                       as_noti_data.cancel_job.handle = input_handle;
+                       as_noti_data.cancel_job.account_id      = input_account_id;
+                       as_noti_data.cancel_job.handle          = input_handle;
+            as_noti_data.cancel_job.multi_user_name = multi_user_name;
 
                        if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
                                EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
@@ -848,7 +917,8 @@ FINISH_OFF:
        hAPI = NULL;
        if (account_list)
                emstorage_free_account(&account_list, account_count, NULL);
-               
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);  
        return err;
 }
@@ -921,6 +991,9 @@ EXPORT_API int email_get_task_information(email_task_information_t **output_task
                }
        }
 
+        if (err == EMAIL_ERROR_DATA_NOT_FOUND)
+            err = EMAIL_ERROR_NONE;
+
 FINISH_OFF:
        if(hAPI)
                emipc_destroy_email_api(hAPI);
@@ -964,186 +1037,11 @@ 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)
-{
-       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;
-       int       stream_size_for_search_filter_list = 0;
-       char     *stream_for_search_filter_list = NULL;
-       HIPC_API  hAPI = NULL;
-       email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
-       ASNotiData as_noti_data;
-
-       EM_IF_NULL_RETURN_VALUE(input_account_id,         EMAIL_ERROR_INVALID_PARAM);
-       EM_IF_NULL_RETURN_VALUE(input_mailbox_id,         EMAIL_ERROR_INVALID_PARAM);
-       EM_IF_NULL_RETURN_VALUE(input_search_filter_list, EMAIL_ERROR_INVALID_PARAM);
-
-       memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
-
-       if ( em_get_account_server_type_by_account_id(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 ) {
-               int as_handle = 0;
-
-               if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
-                       EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
-                       goto FINISH_OFF;
-               }
-
-               /*  noti to active sync */
-               as_noti_data.search_mail_on_server.handle              = as_handle;
-               as_noti_data.search_mail_on_server.account_id          = input_account_id;
-               as_noti_data.search_mail_on_server.mailbox_id          = input_mailbox_id;
-               as_noti_data.search_mail_on_server.search_filter_list  = input_search_filter_list;
-               as_noti_data.search_mail_on_server.search_filter_count = input_search_filter_count;
-
-               return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_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;
-               }
-
-               if(output_handle)
-                       *output_handle = as_handle;
-       }
-       else
-       {
-               hAPI = emipc_create_email_api(_EMAIL_API_SEARCH_MAIL_ON_SERVER);
-
-               EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
-
-               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))){
-                       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);
-
-               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*/
-                       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) {
-                       EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
-                       EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
-               }
-
-               emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
-
-               if (err != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("_EMAIL_API_SEARCH_MAIL_ON_SERVER failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-
-               if(output_handle)
-                       emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
-       }
-
-FINISH_OFF:
-       if(hAPI) {
-               emipc_destroy_email_api(hAPI);
-               hAPI = NULL;
-       }
-
-       EM_DEBUG_API_END ("err[%d]", err);
-       return err;
-}
-
-EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
-{
-       EM_DEBUG_API_BEGIN ("input_account_id[%d]", input_account_id);
-
-       int       err = EMAIL_ERROR_NONE;
-       int       return_value = 0;
-       HIPC_API  hAPI = NULL;
-       email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
-       ASNotiData as_noti_data;
-
-       EM_IF_NULL_RETURN_VALUE(input_account_id,         EMAIL_ERROR_INVALID_PARAM);
-
-       memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
-
-       if ( em_get_account_server_type_by_account_id(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 ) {
-               int as_handle = 0;
-
-               if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
-                       EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
-                       goto FINISH_OFF;
-               }
-
-               /*  noti to active sync */
-               as_noti_data.clear_result_of_search_mail_on_server.handle              = as_handle;
-               as_noti_data.clear_result_of_search_mail_on_server.account_id          = input_account_id;
-
-               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;
-               }
-       }
-       else {
-               hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER);
-
-               EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
-
-               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_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);
-               }
-
-               emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
-
-               if (err != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-       }
-
-FINISH_OFF:
-       if(hAPI) {
-               emipc_destroy_email_api(hAPI);
-               hAPI = NULL;
-       }
-
-       EM_DEBUG_API_END ("err[%d]", err);
-       return err;
-}
-
 EXPORT_API int email_query_smtp_mail_size_limit(int account_id, int *handle)
 {
        EM_DEBUG_API_BEGIN ("account_id[%d] handle[%p]", account_id, handle);
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        HIPC_API hAPI = NULL;
 
@@ -1153,7 +1051,12 @@ EXPORT_API int email_query_smtp_mail_size_limit(int account_id, int *handle)
                goto FINISH_OFF;
        }
 
-       if (em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err) == false ) {
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false ) {
                EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
                goto FINISH_OFF;
        }
@@ -1188,6 +1091,7 @@ FINISH_OFF:
        emipc_destroy_email_api(hAPI);
        hAPI = (HIPC_API)NULL;
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
index 34a61f7..4bd92c0 100755 (executable)
  *                     email-service . 
  */
 
-#include "email-api.h"
 #include "string.h"
 #include "email-convert.h"
 #include "email-storage.h"
 #include "email-utilities.h"
 #include "email-ipc.h"
+#include "email-core-utils.h"
 
 EXPORT_API int email_get_rule(int filter_id, email_rule_t** filtering_set)
 {
        EM_DEBUG_API_BEGIN ("filter_id[%d] filtering_set[%p]", filter_id, filtering_set);
 
        int err = 0;
+    char *multi_user_name = NULL;
 
        EM_IF_NULL_RETURN_VALUE(filtering_set, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(filter_id, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
-       if (!emstorage_get_rule_by_id(filter_id, (emstorage_rule_tbl_t**)filtering_set, true, &err))  {
+       if (!emstorage_get_rule_by_id(multi_user_name, filter_id, (emstorage_rule_tbl_t**)filtering_set, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed [%d]", err);
-
                goto FINISH_OFF;
        } else
                err = EMAIL_ERROR_NONE;
 
 FINISH_OFF:
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -73,29 +73,28 @@ EXPORT_API int email_get_rule_list(email_rule_t** filtering_set, int* count)
        
        int err = EMAIL_ERROR_NONE;
        int is_completed = 0;
+    char *multi_user_name = NULL;
        
        EM_IF_NULL_RETURN_VALUE(filtering_set, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
        *count = 1000;
-       
-       if (!emstorage_get_rule(0, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filtering_set, true, &err))  {
-               EM_DEBUG_EXCEPTION("emstorage_get_rule failed [%d]", err);
-
 
+       if (!emstorage_get_rule(multi_user_name, 0, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filtering_set, true, &err))  {
+               EM_DEBUG_EXCEPTION("emstorage_get_rule failed [%d]", err);
                goto FINISH_OFF;
        } else
                err = EMAIL_ERROR_NONE;
 
 FINISH_OFF:
+
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 
@@ -245,11 +244,11 @@ EXPORT_API int email_free_rule (email_rule_t** filtering_set, int count)
        if (count > 0)  {
                email_rule_t* p = *filtering_set;
                
-               for (i = 0; i < count; i++) {
-                       EM_SAFE_FREE(p[i].value);
-               }
+               for (i = 0; i < count; i++)
+                       emcore_free_rule(p + i);
                
-               EM_SAFE_FREE(p); *filtering_set = NULL;
+               EM_SAFE_FREE(p);
+               *filtering_set = NULL;
        }
        
        EM_DEBUG_FUNC_END ("err[%d]", err);
index 96708dc..6fc3de4 100755 (executable)
@@ -30,8 +30,8 @@
  *                     Email Engine . 
  */
 
-#include "email-api.h"
 #include "string.h"
+#include "email-api-mail.h"
 #include "email-convert.h"
 #include "email-api-account.h"
 #include "email-storage.h"
@@ -41,6 +41,7 @@
 #include "email-core-account.h"
 #include "email-core-cert.h"
 #include "email-core-smime.h"
+#include "email-core-pgp.h"
 #include "email-core-signal.h"
 #include "email-ipc.h"
 
@@ -149,31 +150,33 @@ EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **
        EM_DEBUG_API_BEGIN ();
        int err = EMAIL_ERROR_NONE;
        char temp_email_address[130] = {0, };
+    char *multi_user_name = NULL;
        emstorage_certificate_tbl_t *cert = NULL;
        
        EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(certificate, EMAIL_ERROR_INVALID_PARAM);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return err;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
 
        SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
-       
-       if (!emstorage_get_certificate_by_email_address(temp_email_address, &cert, false, 0, &err)) {
+
+       if (!emstorage_get_certificate_by_email_address(multi_user_name, temp_email_address, &cert, false, 0, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_certificate_by_index failed - %d", err);
-               return err;
+        goto FINISH_OFF;
        }
 
        if (!em_convert_certificate_tbl_to_certificate(cert, certificate, &err)) {
                EM_DEBUG_EXCEPTION("em_convert_certificate_tbl_to_certificate failed");
-               return err;
+        goto FINISH_OFF;
        }       
-       
+
+FINISH_OFF:
+    
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -183,9 +186,11 @@ EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output
        EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
        int err = EMAIL_ERROR_NONE;
        int p_output_attachment_count = 0;
-        int i = 0;
+    int i = 0;
+       int verify = 0;
        char *decrypt_filepath = NULL;
-        char *search = NULL;
+    char *search = NULL;
+    char *multi_user_name = NULL;
        email_mail_data_t *p_output_mail_data = NULL;
        email_attachment_data_t *p_output_attachment_data = NULL;
        emstorage_account_tbl_t *p_account_tbl = NULL;
@@ -198,25 +203,22 @@ EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output
                goto FINISH_OFF;
        }
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       err = em_check_db_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               goto FINISH_OFF;
-       }
-#endif
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]");
+        goto FINISH_OFF;
+    }
 
-       if ((err = emcore_get_mail_data(mail_id, &p_output_mail_data)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_get_mail_data(multi_user_name, mail_id, &p_output_mail_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(p_output_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
+       if (!emstorage_get_account_by_id(multi_user_name, p_output_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ((err = emcore_get_attachment_data_list(mail_id, &p_output_attachment_data, &p_output_attachment_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_get_attachment_data_list(multi_user_name, mail_id, &p_output_attachment_data, &p_output_attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed");
                goto FINISH_OFF;
        }
@@ -226,7 +228,10 @@ EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output
                 if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
                         EM_DEBUG_LOG("Found the encrypt file");
                         break;
-                }
+                } else if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "octet-stream"))) {
+                       EM_DEBUG_LOG("Found the encrypt file");
+                       break;
+               }
         }
 
         if (!search) {
@@ -235,8 +240,27 @@ EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output
                 goto FINISH_OFF;
         }
 
-       if (!emcore_smime_set_decrypt_message(p_output_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_smime_set_decrypt_message failed");
+       if (p_output_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || p_output_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
+                emcore_init_openssl_library();
+               if (!emcore_smime_get_decrypt_message(p_output_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
+                        emcore_clean_openssl_library();
+                       goto FINISH_OFF;
+               }
+                emcore_clean_openssl_library();
+       } else if (p_output_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
+               if ((err = emcore_pgp_get_decrypted_message(p_output_attachment_data[i].attachment_path, p_output_mail_data->pgp_password, false, &decrypt_filepath, &verify)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else if (p_output_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
+               if ((err = emcore_pgp_get_decrypted_message(p_output_attachment_data[i].attachment_path, p_output_mail_data->pgp_password, true, &decrypt_filepath, &verify)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               EM_DEBUG_LOG("Invalid encrypted mail");
+               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
@@ -256,9 +280,12 @@ EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output
        (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(p_output_mail_data->full_address_to);
        (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(p_output_mail_data->full_address_cc);
        (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(p_output_mail_data->full_address_bcc);
+       (*output_mail_data)->flags_flagged_field     = p_output_mail_data->flags_flagged_field;
 
 FINISH_OFF:
 
+    EM_SAFE_FREE(decrypt_filepath);
+
        if (p_account_tbl)
                emstorage_free_account(&p_account_tbl, 1, NULL);
 
@@ -268,6 +295,109 @@ FINISH_OFF:
        if (p_output_attachment_data)
                email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
 
+    EM_SAFE_FREE(multi_user_name);
+
+       EM_DEBUG_API_END ("err[%d]", err);
+       return err;
+}
+
+EXPORT_API int email_get_decrypt_message_ex(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count,
+                                            email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
+{
+       EM_DEBUG_API_BEGIN ();
+       int err = EMAIL_ERROR_NONE;
+    int i = 0;
+       int verify = 0;
+       char *decrypt_filepath = NULL;
+    char *search = NULL;
+    char *multi_user_name = NULL;
+       emstorage_account_tbl_t *p_account_tbl = NULL;
+
+       EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
+
+       if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+       if (!emstorage_get_account_by_id(multi_user_name, input_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+        for (i = 0; i < input_attachment_count; i++) {
+                EM_DEBUG_LOG("mime_type : [%s]", input_attachment_data[i].attachment_mime_type);
+                if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
+                        EM_DEBUG_LOG("Found the encrypt file");
+                        break;
+                } else if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "octet-stream"))) {
+                       EM_DEBUG_LOG("Found the encrypt file");
+                       break;
+               }
+        }
+
+        if (!search) {
+                EM_DEBUG_EXCEPTION("No have a decrypt file");
+                err = EMAIL_ERROR_INVALID_PARAM;
+                goto FINISH_OFF;
+        }
+
+       if (input_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || input_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
+                emcore_init_openssl_library();
+               if (!emcore_smime_get_decrypt_message(input_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
+                        emcore_clean_openssl_library();
+                       goto FINISH_OFF;
+               }
+                emcore_clean_openssl_library();
+       } else if (input_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
+               if ((err = emcore_pgp_get_decrypted_message(input_attachment_data[i].attachment_path, input_mail_data->pgp_password, false, &decrypt_filepath, &verify)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else if (input_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
+               if ((err = emcore_pgp_get_decrypted_message(input_attachment_data[i].attachment_path, input_mail_data->pgp_password, true, &decrypt_filepath, &verify)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               EM_DEBUG_LOG("Invalid encrypted mail");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       /* Change decrpyt_message to mail_data_t */
+       if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       (*output_mail_data)->subject                 = EM_SAFE_STRDUP(input_mail_data->subject);
+       (*output_mail_data)->date_time               = input_mail_data->date_time;
+       (*output_mail_data)->full_address_return     = EM_SAFE_STRDUP(input_mail_data->full_address_return);
+       (*output_mail_data)->email_address_recipient = EM_SAFE_STRDUP(input_mail_data->email_address_recipient);
+       (*output_mail_data)->email_address_sender    = EM_SAFE_STRDUP(input_mail_data->email_address_sender);
+       (*output_mail_data)->full_address_reply      = EM_SAFE_STRDUP(input_mail_data->full_address_reply);
+       (*output_mail_data)->full_address_from       = EM_SAFE_STRDUP(input_mail_data->full_address_from);
+       (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(input_mail_data->full_address_to);
+       (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(input_mail_data->full_address_cc);
+       (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(input_mail_data->full_address_bcc);
+       (*output_mail_data)->flags_flagged_field     = input_mail_data->flags_flagged_field;
+
+FINISH_OFF:
+
+    EM_SAFE_FREE(decrypt_filepath);
+    EM_SAFE_FREE(multi_user_name);
+
+       if (p_account_tbl)
+               emstorage_free_account(&p_account_tbl, 1, NULL);
+
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -279,7 +409,7 @@ EXPORT_API int email_verify_signature(int mail_id, int *verify)
        int err = EMAIL_ERROR_NONE;
        int p_verify = 0;
 
-       EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);    
+       EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
 
        HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
        if (hAPI == NULL) {
@@ -336,8 +466,20 @@ EXPORT_API int email_verify_signature_ex(email_mail_data_t *input_mail_data, ema
                        break;
        }
 
-       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");
+       if (input_mail_data->smime_type == EMAIL_SMIME_SIGNED) {
+                emcore_init_openssl_library();
+               if (!emcore_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, verify, &err)) 
+                       EM_DEBUG_EXCEPTION("emcore_verify_signature failed : [%d]", err);
+
+                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);
+       } else {
+               EM_DEBUG_LOG("Invalid signed mail : mime_type[%d]", input_mail_data->smime_type);
+               err = EMAIL_ERROR_INVALID_PARAM;
+       }
+
 
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
@@ -423,10 +565,10 @@ EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url,
                EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
        }
 
-       emipc_get_paramter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
+       emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
        if (err == EMAIL_ERROR_NONE) {
                if (handle)
-                       emipc_get_paramter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
+                       emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
        }
 }
 */
@@ -440,12 +582,18 @@ EXPORT_API int email_validate_certificate(int account_id, char *email_address, u
 
        int err = EMAIL_ERROR_NONE;
        int as_handle = 0;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        ASNotiData as_noti_data;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
-       if (em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err)     == false) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, 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;
@@ -465,7 +613,8 @@ EXPORT_API int email_validate_certificate(int account_id, char *email_address, u
 
        as_noti_data.validate_certificate.handle = as_handle;
        as_noti_data.validate_certificate.account_id = account_id;
-       as_noti_data.validate_certificate.email_address = strdup(email_address);
+       as_noti_data.validate_certificate.email_address = email_address;
+    as_noti_data.validate_certificate.multi_user_name = multi_user_name;
 
        if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE, &as_noti_data) == false) {
                EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
@@ -478,6 +627,7 @@ EXPORT_API int email_validate_certificate(int account_id, char *email_address, u
 
 FINISH_OFF:
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
@@ -492,12 +642,18 @@ EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address,
 
        int err = EMAIL_ERROR_NONE;
        int as_handle = 0;
+    char *multi_user_name = NULL;
        email_account_server_t account_server_type;
        ASNotiData as_noti_data;
 
+    if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
        memset(&as_noti_data, 0x00, sizeof(ASNotiData));
 
-       if (em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err)     == false) {
+       if (em_get_account_server_type_by_account_id(multi_user_name, 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;
@@ -515,9 +671,10 @@ EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address,
                goto FINISH_OFF;                
        }
 
-       as_noti_data.get_resolve_recipients.handle = as_handle;
-       as_noti_data.get_resolve_recipients.account_id = account_id;
-       as_noti_data.get_resolve_recipients.email_address = strdup(email_address);
+       as_noti_data.get_resolve_recipients.handle          = as_handle;
+       as_noti_data.get_resolve_recipients.account_id      = account_id;
+       as_noti_data.get_resolve_recipients.email_address   = email_address;
+    as_noti_data.get_resolve_recipients.multi_user_name = multi_user_name;
 
        if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT, &as_noti_data) == false) {
                EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
@@ -530,6 +687,7 @@ EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address,
 
 FINISH_OFF:
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_API_END ("err[%d]", err);
        return err;
 }
index 98c545c..6da101a 100755 (executable)
 #include "email-types.h"
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_ACCOUNT Email Account API
-* @{
-*/
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_ACCOUNT_MODULE Account API
+ * @brief Account API is a set of operations to manage email accounts like add, update, delete or get account related details.
+ *
+ * @section EMAIL_SERVICE_ACCOUNT_MODULE_HEADER Required Header
+ *   \#include <email-api.h>
+ *
+ * @section EMAIL_SERVICE_ACCOUNT_MODULE_OVERVIEW Overview
+ * Account API is a set of operations to manage email accounts like add, update, delete or get account related details.
+ */
 
 /**
+ * @file email-api-account.h
+ * @brief This file contains the data structures and interfaces of Accounts provided by email-service.
+ * @{
+ * @code
+ *   #include "email-api-account.h"
+ *    bool
+ *       other_app_invoke_uniform_api_sample(int* error_code)
+ *   {
+ *   email_account_t *account = NULL;
+ *   email_account_t *new_account = NULL;
  *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api-account.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief              This file contains the data structures and interfaces of Accounts provided by
- *                     email-service .
+ *   account = malloc(sizeof(email_account_t));
+ *   memset(account, 0x00, sizeof(email_account_t));
  *
-* @{
-
-* @code
-*      #include "email-api-account.h"
-*       bool
-*              other_app_invoke_uniform_api_sample(int* error_code)
-*      {
-*      email_account_t *account = NULL;
-*      email_account_t *new_account = NULL;
-*
-*      account = malloc(sizeof(email_account_t));
-*      memset(account, 0x00, sizeof(email_account_t));
-*
-*      account->retrieval_mode         = 1;
-*      account->incoming_server_secure_connection           = 1;
-*      account->outgoing_server_type    = EMAIL_SERVER_TYPE_SMTP;
-*      account->outgoing_server_port_number       = EMAIL_SMTP_PORT;
-*      account->outgoing_server_need_authentication           = 1;
-*      account->account_name           = strdup("gmail");
-*      account->display_name           = strdup("Tom");
-*      account->user_email_address             = strdup("tom@gmail.com");
-*      account->reply_to_addr          = strdup("tom@gmail.com");
-*      account->return_addr            = strdup("tom@gmail.com");
-*      account->incoming_server_type  = EMAIL_SERVER_TYPE_POP3;
-*      account->incoming_server_address  = strdup("pop3.gmail.com");
-*      account->incoming_server_port_number               = 995;
-*      account->incoming_server_secure_connection           = 1;
-*      account->retrieval_mode         = EMAIL_IMAP4_RETRIEVAL_MODE_ALL;
-*      account->incoming_server_user_name              = strdup("tom");
-*      account->password               = strdup("password");
-*      account->outgoing_server_type    = EMAIL_SERVER_TYPE_SMTP;
-*      account->outgoing_server_address    = strdup("smtp.gmail.com");
-*      account->outgoing_server_port_number       = 587;
-*      account->outgoing_server_secure_connection       = 0x02;
-*      account->outgoing_server_need_authentication           = 1;
-*      account->outgoing_server_user_name           = strdup("tom@gmail.com");
-*      account->sending_password       = strdup("password");
-*      account->auto_resend_times        = 0;
-*      account->pop_before_smtp        = 0;
-*      account->incoming_server_requires_apop                   = 0;
-*      account->incoming_server_authentication_method                   = 0;
-*      account->is_preset_account         = 1;
-*      account->logo_icon_path         = strdup("Logo Icon Path");
-*      account->options.priority = 3;
-*      account->options.keep_local_copy = 0;
-*      account->options.req_delivery_receipt = 0;
-*      account->options.req_read_receipt = 0;
-*      account->options.download_limit = 0;
-*      account->options.block_address = 0;
-*      account->options.block_subject = 0;
-*      account->options.display_name_from = strdup("Display name from");
-*      account->options.reply_with_body = 0;
-*      account->options.forward_with_files = 0;
-*      account->options.add_myname_card = 0;
-*      account->options.add_signature = 0;
-*      account->options.signature= strdup("Signature");
-*      account->check_interval = 0;
-*      // Add account
-*      if(EMAIL_ERROR_NONE != email_add_account(account))
-*              //failure
-*      //else
-*      {
-*              //success
-*              if(account_id)
-*                      *account_id = account->account_id;
-*      }
-*      if(EMAIL_ERROR_NONE != email_validate_account(account_id,&account_handle))
-*              //failure
-*      else
-*            //success
-*      if(EMAIL_ERROR_NONE != email_delete_account(account_id))
-*              //failure
-*      else
-*              //success
-*      new_account = malloc(sizeof(email_account_t));
-*      memset(new_account, 0x00, sizeof(email_account_t));
-*      new_account->flag1                   = 1;
-*      new_account->account_name            = strdup("samsung001");
-*      new_account->display_name            = strdup("Tom001");
-*      new_account->options.keep_local_copy = 1;
-*      new_account->check_interval          = 55;
-*      // Update account
-*      if(EMAIL_ERROR_NONE != email_update_account(acount_id,new_account))
-*              //failure
-*      else
-*              //success
-*      // Get account
-*      if(EMAIL_ERROR_NONE != email_get_account(account_id,GET_FULL_DATA,&account))
-*              //failure
-*      else
-*              //success
-*      // Get list of accounts
-*      if(EMAIL_ERROR_NONE != email_get_account_list(&account_list,&count))
-*              //failure
-*      else
-*              //success
-*      // free account
-*      email_free_account(&account, 1);
-*      }
+ *   account->retrieval_mode         = 1;
+ *   account->incoming_server_secure_connection           = 1;
+ *   account->outgoing_server_type    = EMAIL_SERVER_TYPE_SMTP;
+ *   account->outgoing_server_port_number       = EMAIL_SMTP_PORT;
+ *   account->outgoing_server_need_authentication           = 1;
+ *   account->account_name           = strdup("gmail");
+ *   account->display_name           = strdup("Tom");
+ *   account->user_email_address             = strdup("tom@gmail.com");
+ *   account->reply_to_addr          = strdup("tom@gmail.com");
+ *   account->return_addr            = strdup("tom@gmail.com");
+ *   account->incoming_server_type  = EMAIL_SERVER_TYPE_POP3;
+ *   account->incoming_server_address  = strdup("pop3.gmail.com");
+ *   account->incoming_server_port_number               = 995;
+ *   account->incoming_server_secure_connection           = 1;
+ *   account->retrieval_mode         = EMAIL_IMAP4_RETRIEVAL_MODE_ALL;
+ *   account->incoming_server_user_name              = strdup("tom");
+ *   account->password               = strdup("password");
+ *   account->outgoing_server_type    = EMAIL_SERVER_TYPE_SMTP;
+ *   account->outgoing_server_address    = strdup("smtp.gmail.com");
+ *   account->outgoing_server_port_number       = 587;
+ *   account->outgoing_server_secure_connection       = 0x02;
+ *   account->outgoing_server_need_authentication           = 1;
+ *   account->outgoing_server_user_name           = strdup("tom@gmail.com");
+ *   account->sending_password       = strdup("password");
+ *   account->auto_resend_times        = 0;
+ *   account->pop_before_smtp        = 0;
+ *   account->incoming_server_requires_apop                   = 0;
+ *   account->incoming_server_authentication_method                   = 0;
+ *   account->is_preset_account         = 1;
+ *   account->logo_icon_path         = strdup("Logo Icon Path");
+ *   account->options.priority = 3;
+ *   account->options.keep_local_copy = 0;
+ *   account->options.req_delivery_receipt = 0;
+ *   account->options.req_read_receipt = 0;
+ *   account->options.download_limit = 0;
+ *   account->options.block_address = 0;
+ *   account->options.block_subject = 0;
+ *   account->options.display_name_from = strdup("Display name from");
+ *   account->options.reply_with_body = 0;
+ *   account->options.forward_with_files = 0;
+ *   account->options.add_myname_card = 0;
+ *       account->options.add_signature = 0;
+ *   account->options.signature= strdup("Signature");
+ *   account->check_interval = 0;
+ *   // Add account
+ *   if(EMAIL_ERROR_NONE != email_add_account(account))
+ *       //failure
+ *   //else
+ *   {
+ *       //success
+ *       if(account_id)
+ *           *account_id = account->account_id;
+ *   }
+ *   if(EMAIL_ERROR_NONE != email_validate_account(account_id,&account_handle))
+ *       //failure
+ *   else
+ *         //success
+ *   if(EMAIL_ERROR_NONE != email_delete_account(account_id))
+ *       //failure
+ *   else
+ *       //success
+ *   new_account = malloc(sizeof(email_account_t));
+ *   memset(new_account, 0x00, sizeof(email_account_t));
+ *   new_account->flag1                   = 1;
+ *   new_account->account_name            = strdup("tizen001");
+ *   new_account->display_name            = strdup("Tom001");
+ *   new_account->options.keep_local_copy = 1;
+ *   new_account->check_interval          = 55;
+ *   // Update account
+ *   if(EMAIL_ERROR_NONE != email_update_account(acount_id,new_account))
+ *       //failure
+ *   else
+ *       //success
+ *   // Get account
+ *   if(EMAIL_ERROR_NONE != email_get_account(account_id,GET_FULL_DATA,&account))
+ *       //failure
+ *   else
+ *       //success
+ *   // Get list of accounts
+ *   if(EMAIL_ERROR_NONE != email_get_account_list(&account_list,&count))
+ *       //failure
+ *   else
+ *       //success
+ *   // free account
+ *   email_free_account(&account, 1);
+ *   }
  *
  * @endcode
  * @}
  */
 
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_ACCOUNT_MODULE
+ * @{
+ */
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
 /**
- * @fn email_add_account(email_account_t* account)
- * @brief      Create a new email account.This function is invoked when user wants to add new email account
+ * @brief Creates a new email account.
+ * @details This function is invoked when the user wants to add a new email account.
  *
- * @param[in] account  Specifies the structure pointer of account.
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see         email_account_t
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ * @param[in] account  The structure pointer of an account
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_add_account(email_account_t* account);
 
 /**
- * @fn email_delete_account(int account_id)
- * @brief       Delete a email account.This function is invoked when user wants to delete an existing email account
+ * @brief Deletes an email account.
+ * @details This function is invoked when the user wants to delete an existing email account.
  *
- * @param[in] account_id       Specifies the account ID.
- * @exception  EMAIL_ERROR_INVALID_PARAM               -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id  The account ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
  */
 EXPORT_API int email_delete_account(int account_id);
 
 /**
- * @fn email_update_account(int account_id, email_account_t* new_account)
- * @brief      Change the information of a email account.This function is getting invoked when user wants to change some information of existing email account.
- *
- * @param[in] account_id       Specifies the orignal account ID.
- * @param[in] new_account      Specifies the information of new account.
- * @param[in] with_validation  If this is 1, email-service will validate the account before updating. If this is 0, email-service will update the account without validation.
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see email_account_t
- * @remarks N/A
+ * @brief Changes the information of an email account.
+ * @details This function is invoked when the user wants to change some information of the existing email account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] account_id       The original account ID
+ * @param[in] new_account      The information of new account
+ * @param[in] with_validation  The validation flag \n
+ *                             If this is @c 1, email-service will validate the account before updating. 
+ *                             If this is @c 0, email-service will update the account without validation.
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_update_account(int account_id, email_account_t* new_account);
 
 /**
- * @fn email_update_account_with_validation(int account_id, email_account_t* new_account)
- * @brief      Change the information of a email account.This function is getting invoked when user wants to change some information of existing email account.
- *
- * @param[in] account_id       Specifies the orignal account ID.
- * @param[in] new_account      Specifies the information of new account.
- * @param[in] with_validation  If this is 1, email-service will validate the account before updating. If this is 0, email-service will update the account without validation.
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see email_account_t
- * @remarks N/A
+ * @brief Changes the information of an email account.
+ * @details This function is invoked when the user wants to change some information of the existing email account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] account_id       The original account ID
+ * @param[in] new_account      The information of new account
+ * @param[in] with_validation  The validation tag \n
+ *                             If this is @c 1, email-service will validate the account before updating. 
+ *                             If this is @c 0, email-service will update the account without validation.
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_update_account_with_validation(int account_id, email_account_t* new_account);
 
 /**
- * @fn  email_get_account(int account_id, int pulloption, email_account_t** account)
- * @brief      Get an email account by ID. This function is getting invoked when user wants to get the account informantion based on account id and option (GET_FULL_DATA/WITHOUT_OPTION/ONLY_OPTION).<br>
- *                     Memory for account information will be allocated to 3rd param(account). The allocated memory should be freed by email_free_account().
- *
- * @param[in] account_id       Specifies the account ID.This function is invoked when user
- * @param[in] pulloption       Option to specify to get full details or partial, see definition of EMAIL_ACC_GET_OPT_XXX
- * @param[out] account         The returned account is saved here.
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see email_account_t
- * @remarks N/A
+ * @brief Gets an email account by ID.
+ * @details This function is invoked when the user wants to get the account information based on account ID and option (GET_FULL_DATA/WITHOUT_OPTION/ONLY_OPTION). 
+ *          Memory for account information will be allocated to the 3rd param (@a account). 
+ *          You must free the allocated memory using email_free_account().
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id  The account ID
+ * @param[in]  pulloption  The option to specify to get full details or partial \n 
+ *                         See definition of #EMAIL_ACC_GET_OPT_XXX.
+ * @param[out] account     The returned account is saved here
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_get_account(int account_id, int pulloption, email_account_t** account);
 
 /**
- * @fn email_get_account_list(email_account_t** account_list, int* count);
- * @brief      Get Account List.This function is getting invoked when user wants to get all account information based on the count of accounts provided by user.<br>
- *                     Memory for account information will be allocated to 3rd param(account). The allocated memory should be freed by email_free_account().
- *
- * @param[in] account_list     Specifies the structure pointer of account.
- * @param[out] count                   Specifies the count of accounts.
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see email_account_t
- * @remarks N/A
+ * @brief Gets an account list.
+ * @details This function is invoked when the user wants to get all account information based on the count of accounts provided by user. 
+ *          Memory for account information will be allocated to 3rd param (@a account). 
+ *          You must free the allocated memory using email_free_account().
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_list  The structure pointer of an account
+ * @param[out] count         The count of accounts
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_get_account_list(email_account_t** account_list, int* count);
 
 /**
- * @fn   email_free_account(email_account_t** account_list, int count);
- * @brief      Free allocated memory.This function is getting invoked when user wants to delete all account information.
- *
- * @param[in] account_list     Specifies the structure pointer of account.
- * @param[out] count                   Specifies the count of accounts.
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see email_account_t
- * @remarks N/A
+ * @brief Frees allocated memory.
+ * @details This function is invoked when the user wants to delete all account information.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in]  account_list  The structure pointer of an account
+ * @param[out] count         The count of accounts
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM     Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_free_account(email_account_t** account_list, int count);
 
-
-/**
- * @fn email_validate_account(int account_id, int *handle)
- * @brief      Validate account.This function is getting invoked  after adding one account to validate it.If account is not validated then user should retry once again to add the account .
- *
- * @param[in] account_id              Specifies the account Id to validate.
- * @param[out] handle          Specifies the sending handle.
- * @remarks N/A
- * @exception EMAIL_ERROR_INVALID_PARAM        -Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see                none
- * @remarks N/A
- */
 EXPORT_API int email_validate_account(int account_id, int *handle) DEPRECATED; /* Will be replaced with email_validate_account_ex */
 
 /**
- * @fn email_validate_account_ex(email_account_t* account, int *handle)
- * @brief      Validate account.This function is getting invoked after adding one account to validate it.If account is not validated then user should retry once again to add the account .
+ * @brief Validates an account.
+ * @details This function is invoked after adding one account to validate it.
+ *          If the account is not validated then t user should retry once again to add the account.
  *
- * @param[in] account  Specifies the account structure
- * @param[out] handle  Specifies the sending handle.
- * @remarks N/A
- * @exception EMAIL_ERROR_INVALID_PARAM - Invalid argument
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see                email_account_t
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account  The account structure
+ * @param[out] handle   The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_account_t
  */
 EXPORT_API int email_validate_account_ex(email_account_t* account, int *handle);
 
 /**
- * @fn email_add_account_with_validation(email_account_t* account, int *handle)
- * @brief      Add an account when the account is validated. This function is getting invoked when user want to validate an account. If account is not validated then user should retry once again to add the account.<br>
- *              Validation is executed without saving an account to DB
+ * @brief Adds an account when the account is validated.
+ * @details This function is invoked when a user wants to validate an account. 
+ *          If the account is not validated then user should retry once again to add the account. 
+ *          Validation is executed without saving an account to DB.
  *
- * @param[in] account      Specifies the structure pointer of account.
- * @param[out] handle          Specifies the sending handle.
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account  The structure pointer of an account
+ * @param[out] handle   The sending handle
+ *
+ * @return  @c true on success,
+ *          otherwise @c false on failure
  */
 EXPORT_API int email_add_account_with_validation(email_account_t* account, int *handle);
 
 
 /**
- * @fn email_backup_accounts_into_secure_storage(const char *file_name)
- * @brief      Back up information of all accounts into secure storage.
- *          This function is getting invoked when user want to backup account information safely.
+ * @brief Backs up information of all accounts into the secure storage.
+ * @details This function is invoked when a user wants to backup account information safely.
  *
- * @param[in] file_name            Specifies the file name in secure storage
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @param[in]  file_name  The file name in secure storage
+ *
+ * @return  @c true on success,
+ *          otherwise @c false on failure
  */
 EXPORT_API int email_backup_accounts_into_secure_storage(const char *file_name);
 
 /**
- * @fn email_restore_accounts_from_secure_storage(const char *file_name)
- * @brief      Restore accounts from stored file in secure storage.
- *          This function is getting invoked when user want to restore accounts.
+ * @brief Restores accounts from a file stored in the secure storage.
+ * @details This function is invoked when a user wants to restore accounts.
  *
- * @param[in] file_name            Specifies the file name in secure storage
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  file_name  The file name in the secure storage
+ *
+ * @return  @c true on success,
+ *          otherwise @c false on failure
  */
 EXPORT_API int email_restore_accounts_from_secure_storage(const char * file_name);
 
 /**
- * @fn email_get_password_length_of_account(const int account_id, int *password_length)
- * @brief      Get password length of an account.
- *          This function is getting invoked when user want to know the length of an account.
+ * @brief Gets the password length of an account.
+ * @details This function is invoked when a user wants to know the length of an account.
  *
- * @param[in] account_id    Specifies the account id
- * @param[in] password_type Specifies the password type
- * @param[out] password_length         Specifies the password length.
- * @remarks N/A
- * @return This function returns true on success or false on failure.
- */
-EXPORT_API int email_get_password_length_of_account(int account_id, int *password_length);
-
-
-/**
- * @fn email_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
- * @brief      Query email server information.
- *          This function is getting invoked when user want to get email server information.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @param[in] domain_name              Specifies the domain name of server
- * @param[out] result_server_info      Specifies the information of email server.
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @param[in]  account_id       The account ID
+ * @param[in]  password_type    The password type
+ * @param[out] password_length  The password length
+ *
+ * @return  @c true on success,
+ *          otherwise @c false on failure
  */
-EXPORT_API int email_query_server_info(const char* domain_name, email_server_info_t **result_server_info);
+EXPORT_API int email_get_password_length_of_account(int account_id, email_get_password_length_type password_type, int *password_length);
 
 /**
- * @fn email_free_server_info(email_server_info_t **result_server_info)
- * @brief      Free email_server_info_t.
- *          This function is getting invoked when user want to free email_server_info_t.
+ * @brief Updates notifications on the notification bar.
+ * @details This function is invoked when user want to update notification bar.
  *
- * @param[in] result_server_info       Specifies the pointer of  in secure storage
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @param[in] account_id         The account ID
+ * @param[in] total_mail_count   The total number of synced mail
+ * @param[in] unread_mail_count  The unread number of synced mail
+ * @param[in] input_from_eas     The flag that specifies whether the mail is from EAS
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
-EXPORT_API int email_free_server_info(email_server_info_t **result_server_info);
+EXPORT_API int email_update_notification_bar(int account_id, int total_mail_count, int unread_mail_count, int input_from_eas);
 
 /**
- * @fn email_update_notification_bar(int account_id)
- * @brief      Update notifications on notification bar.
- *          This function is getting invoked when user want to update notification bar.
+ * @brief Clears all notification on the notification bar.
+ * @details This function is invoked when a user wants to clear notification bar.
  *
- * @param[in] account_id               Specifies the id of account.
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
-EXPORT_API int email_update_notification_bar(int account_id);
+EXPORT_API int email_clear_all_notification_bar();
 
 /**
- * @fn email_clear_all_notification_bar()
- * @brief      Clear all notification on notification bar.
- *          This function is getting invoked when user want to clear notification bar.
+ * @fn email_clear_notification_bar(int account_id)
+ * @brief  Clear notification of account on notification bar.
+ *         This function is getting invoked when user want to clear notification bar.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
  * @remarks N/A
- * @return This function returns true on success or false on failure.
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
-EXPORT_API int email_clear_all_notification_bar();
-
+EXPORT_API int email_clear_notification_bar(int account_id);
 
 /**
- * @fn email_save_default_account_id()
- * @brief      Save default account id to vconf storage.
- *          This function is getting invoked when user want to save default account id.
+ * @brief Saves the default account ID to the vconf storage.
+ * @details This function is invoked when a user wants to save a default account ID.
  *
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_save_default_account_id(int input_account_id);
 
 /**
- * @fn email_load_default_account_id()
- * @brief      Load default account id to vconf storage.
- *          This function is getting invoked when user want to load default account id.
+ * @brief Loads the default account ID to the vconf storage.
+ * @details This function is invoked when a user wants to load a default account ID.
  *
- * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  @c true on success,
+ *          otherwise @c false on failure
  */
 EXPORT_API int email_load_default_account_id(int *output_account_id);
 
@@ -399,8 +457,8 @@ EXPORT_API int email_load_default_account_id(int *output_account_id);
 #endif /* __cplusplus */
 
 /**
-* @} @}
-*/
+ * @}
+ */
 
 #endif /* __EMAIL_API_ACCOUNT_H__ */
 
index 4d6d2e5..a3f3d5f 100755 (executable)
 */
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
+ * @file email-api-etc.h
+ * @brief This file contains the data structures and interfaces of etc APIs provided by email-service.
+ */
 
 /**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_ETC Email API
-* @{
-*/
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_ETC_MODULE Other API
+ * @brief Various API set for initializing and MIME operations and verifying email address.
+ *
+ * @section EMAIL_SERVICE_ETC_MODULE_HEADER Required Header
+ *   \#include <email-api-etc.h>
+ *   \#include <email-api-init.h>
+ *
+ * @section EMAIL_SERVICE_ETC_MODULE_OVERVIEW Overview
+ */
 
 /**
- *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file       email-api-etc.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief      This file contains the data structures and interfaces of Accounts provided by
- *                     email-service .
+ * @internal
+ * @addtogroup EMAIL_SERVICE_FRAMEWORK
+ * @{
  */
 
 #ifndef __EMAIL_API_ETC_H__
@@ -52,67 +53,117 @@ extern "C" {
 #include "email-types.h"
 
 /**
- * @fn email_show_user_message
- * @brief      This function show user message.
+ * @brief Shows a user message.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  id          (need to be updated)
+ * @param[in]  action      (need to be updated)
+ * @param[in]  error_code  (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see        email_action_t
- * @remarks N/A
+ * @see #email_action_t
  */
 EXPORT_API int email_show_user_message(int id, email_action_t action, int error_code);
 
 /**
- * @fn email_parse_mime_file
- * @brief      This function parse mime file
- * @param[in]  eml_file_path
- * @param[out] output_mail_data
- * @param[out] output_attachment_data
- * @param[out] output_attachment_count
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see
- * @remarks N/A
+ * @brief Parses a MIME file.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   eml_file_path            (need to be updated)
+ * @param[out]  output_mail_data         (need to be updated)
+ * @param[out]  output_attachment_data   (need to be updated)
+ * @param[out]  output_attachment_count  (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_parse_mime_file(char *eml_file_path, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
 
 /**
- * @fn email_write_mime_file
- * @brief      This function create mime file from input data
- * @param[in]  input_mail_data
- * @param[in]  input_attachment_data
- * @param[in]  input_attachment_count
- * @param[out] output_file_path
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see
- * @remarks N/A
+ * @brief Creates a MIME file from input data.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   input_mail_data         (need to be updated)
+ * @param[in]   input_attachment_data   (need to be updated)
+ * @param[in]   input_attachment_count  (need to be updated)
+ * @param[out]  output_file_path        (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_write_mime_file(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count, char **output_file_path);
 
 /**
- * @fn email_delete_parsed_data
- * @brief      This function delete parsed files of mime
- * @param[in]  input_mail_data
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see
- * @remarks N/A
+ * @brief Deletes the parsed files of MIME.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_mail_data  (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_delete_parsed_data(email_mail_data_t *input_mail_data);
 
 
 /**
- * @fn email_get_mime_entity
- * @brief      This function get mime entity
- * @param[in]  mime_path
- * @param[out] mime_entity
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see
- * @remarks N/A
+ * @brief Gets a MIME entity.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   mime_path    (need to be updated)
+ * @param[out]  mime_entity  (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_get_mime_entity(char *mime_path, char **mime_entity);
+
+/**
+ * @brief Validates email address.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_email_address  The email address string   (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ */
+EXPORT_API int email_verify_email_address(char *input_email_address);
+
+/**
+ * @brief   Convert mutf7 string to utf8 string.
+ *
+ * @since_tizen 2.4
+ *
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mutf7_str     The original mutf7 string  (need to be updated)
+ * @param[out] utf8_str     Thr utf8 string converted  (need to be updated)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ */
+EXPORT_API int email_convert_mutf7_to_utf8(const char *mutf7_str, char **utf8_str);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
@@ -120,5 +171,5 @@ EXPORT_API int email_get_mime_entity(char *mime_path, char **mime_entity);
 #endif /* __EMAIL_API_ETC_H__ */
 
 /**
-* @} @}
-*/
+ * @}
+ */
index 6e2ffe1..6e15f3f 100755 (executable)
 #include "email-types.h"
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_INIT Email Initialization API
-* @{
-*/
-
-/**
+ * @internal
+ * @file email-api-init.h
+ * @brief This file contains the data structures and interfaces of Email FW Initialization provided by email-service.
+ *
+ * @{
+ * @code
+ *
+ *  #include "email-api-init.h"
+ *
+ *  bool
+ *  other_app_invoke_uniform_api_sample(int* error_code)
+ *  {
+ *       int err = EMAIL_ERROR_NONE;
+ *
+ *      // Opens connections to email-service and DB
+ *      // The connections will be maintain throughout application's execution
+ *      if(EMAIL_ERROR_NONE == email_service_begin())
+ *      {
+ *          If(EMAIL_ERROR_NONE != email_open_db())
+ *          {
+ *              return false;
+ *          }
+ *
+ *          // Executes email_init_storage() if and only if there is no db file.
+ *              // This function will create db file and tables for email service
+ *          If(EMAIL_ERROR_NONE !=email_init_storage())
+ *          {
+ *              return false;
+ *          }
+ *      }
+ *
+ *      ......
+ *
+ *      // Work with calling MAPI functions
  *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api-init.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief      This file contains the data structures and interfaces of Email FW Initialization provided by
- *                     email-service .
- *@{
- *@code
- *
- *     #include "email-api-init.h"
- *
- *     bool
- *     other_app_invoke_uniform_api_sample(int* error_code)
- *     {
- *              int err = EMAIL_ERROR_NONE;
- *
- *             // Open connections to email-service and DB
- *             // The connections will be maintain throughout application's execution
- *             if(EMAIL_ERROR_NONE == email_service_begin())
- *             {
- *                     If(EMAIL_ERROR_NONE != email_open_db())
- *                     {
- *                             return false;
- *                     }
- *
- *                     // Execute email_init_storage() if and only if there is no db file.
- *                     // This fuction will create db file and tables for email service
- *                     If(EMAIL_ERROR_NONE !=email_init_storage())
- *                     {
- *                             return false;
- *                     }
- *             }
- *
- *             ......
- *
- *             // Work with calling MAPI functions
- *
- *             ......
- *
- *             // Close the connections to email-service and DB after all email job is finished. (ex. close an email application)
- *             // DO NOT have to call these funtions until the connections is not needed any more.
- *             err =email_close_db();
- *             err =email_service_end();
- *      }
+ *      ......
+ *
+ *      // Closes the connections to email-service and DB after all email jobs are finished. (ex. close an email application)
+ *      // DO NOT have to call these funtions until the connections are not needed any more.
+ *      err =email_close_db();
+ *      err =email_service_end();
+ *   }
  *
  * @endcode
  * @}
  */
 
-
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_ETC_MODULE
+ * @{
+ */
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
 /**
- * @fn email_init_storage(void)
- * @brief      Create all tables for email.
+ * @brief Creates all tables for an email.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @exception none
- * @see none
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_init_storage(void);
 
 /**
- * @fn email_open_db(void)
- * @brief This function Open the email DB and register busy handler
+ * @brief Opens the email DB and registers a busy handler.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception none
- * @see        none
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_open_db(void);
 
 
 /**
- * @fn email_close_db(void)
- * @brief      This function closes the connection of  the email DB
+ * @brief Closes the connection to the email DB.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception none
- * @see none
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_close_db(void);
 
 /**
- * @fn email_service_begin(void)
- * @brief      Does the IPC Proxy Initialization by the Application which used the Email FW API's
+ * @brief Initializes IPC Proxy by an application which used the Email FW API's.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception none
- * @see        none
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_service_begin(void);
 
 /**
- * @fn email_service_end(void)
- * @brief      This function does the IPC Proxy Finaization by the Application which used the Email FW API's
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see        none
- * @remarks N/A
+ * @brief Finalizes IPC Proxy by an application which used the Email FW API's.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_service_end(void);
 
 /**
- * @fn email_ping_service(void)
- * @brief      This function check email-service process is running or not
+ * @brief Checks whether the email-service process is running.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception  none
- * @see        none
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_ping_service(void);
 
@@ -168,8 +161,8 @@ EXPORT_API int email_ping_service(void);
 #endif /* __cplusplus */
 
 /**
-* @} @}
-*/
+ * @}
+ */
 
 
 #endif  /* __EMAIL_API_INTI_H__ */
index 212cd81..b2cdf1a 100755 (executable)
 #include <unistd.h>
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_MAIL Email Mail API
-* @{
-*/
+ * @file email-api-mail.h
+ */
 
 /**
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_MAIL_MODULE Mail API
+ * @brief Mail API is a set of operations to manage mail like add, update, delete or get mail related details.
+ *
+ * @section EMAIL_SERVICE_MAIL_MODULE_HEADER Required Header
+ *   \#include <email-api-mail.h>
  *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file       email-api-mail.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief      This file contains the data structures and interfaces of Messages provided by
- *                     email-service .
+ * @section EMAIL_SERVICE_MAIL_MODULE_OVERVIEW Overview
+ * Mail API is a set of operations to manage mail like add, update, delete or get mail related details.
  */
 
-
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_MAIL_MODULE
+ * @{
+ */
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
 /**
- * @fn email_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas)
- * @brief      Save a mail. This function is invoked when user wants to add a mail.
- *             If the option from_eas is 1 then this will save the message on server as well as on locally.
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_eas value will be 0
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_eas value will be 1/0
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_data  Specifies the saving mail.
- * @param[in] input_attachment_data_list       Specifies the mailbox structure for saving email.
- * @param[in] input_attachment_count   Specifies if the mail comes from composer.
- * @param[in] input_meeting_request    Specifies if the mail comes from composer.
- * @param[in] input_from_eas   Specifies if the mail will be saved on server.
-
- * @exception  none
- * @see email_mail_data_t and email_mailbox_t
- * @remarks N/A
- * @code
- * #include "email-api-mail.h"
- * int _test_add_mail ()
- * {
- *     int                    i = 0;
- *     int                    account_id = 0;
- *     int                    from_eas = 0;
- *     int                    attachment_count = 0;
- *     int                    err = EMAIL_ERROR_NONE;
- *     char                   arg[50] = { 0 , };
- *     char                  *body_file_path = MAILHOME"/tmp/mail.txt";
- *     email_mailbox_t         *mailbox_data = NULL;
- *     email_mail_data_t       *test_mail_data = NULL;
- *     email_attachment_data_t *attachment_data = NULL;
- *     email_meeting_request_t *meeting_req = NULL;
- *     FILE                  *body_file;
- *
- *     printf("\n > Enter account id : ");
- *     scanf("%d", &account_id);
- *
- *
- *     memset(arg, 0x00, 50);
- *     printf("\n > Enter mailbox name : ");
- *     scanf("%s", arg);
- *
- *     email_get_mailbox_by_name(account_id, arg, &mailbox_data);
- *
- *     test_mail_data = malloc(sizeof(email_mail_data_t));
- *     memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
- *
- *     printf("\n From EAS? [0/1]> ");
- *     scanf("%d", &from_eas);
- *
- *     test_mail_data->account_id        = account_id;
- *     test_mail_data->save_status       = 1;
- *     test_mail_data->flags_seen_field  = 1;
- *     test_mail_data->file_path_plain   = strdup(body_file_path);
- *     test_mail_data->mailbox_id        = mailbox_data->mailbox_id;
- *     test_mail_data->mailbox_type      = mailbox_data->mailbox_type;
- *     test_mail_data->full_address_from = strdup("<test1@test.com>");
- *     test_mail_data->full_address_to   = strdup("<test2@test.com>");
- *     test_mail_data->full_address_cc   = strdup("<test3@test.com>");
- *     test_mail_data->full_address_bcc  = strdup("<test4@test.com>");
- *     test_mail_data->subject           = strdup("Meeting request mail");
- *
- *     body_file = fopen(body_file_path, "w");
- *
- *     for(i = 0; i < 500; i++)
- *             fprintf(body_file, "X2 X2 X2 X2 X2 X2 X2");
- *     fflush(body_file);
- *   fclose(body_file);
- *
- *     printf(" > Attach file? [0/1] : ");
- *     scanf("%d",&attachment_count);
- *
- *     if ( attachment_count )  {
- *             memset(arg, 0x00, 50);
- *             printf("\n > Enter attachment name : ");
- *             scanf("%s", arg);
- *
- *             attachment_data = malloc(sizeof(email_attachment_data_t));
- *
- *             attachment_data->attachment_name  = strdup(arg);
- *
- *             memset(arg, 0x00, 50);
- *             printf("\n > Enter attachment absolute path : ");
- *             scanf("%s",arg);
- *
- *             attachment_data->attachment_path  = strdup(arg);
- *             attachment_data->save_status      = 1;
- *             test_mail_data->attachment_count  = attachment_count;
- *     }
+ * @brief Saves a mail.
+ * @details This function is invoked when a user wants to add a mail.\n
+ *          If the option from_eas is 1 then this will save the message on server as well as on locally.\n
+ *          If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_eas value will be 0.\n
+ *          If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_eas value will be 1/0.
  *
- *     printf("\n > Meeting Request? [0: no, 1: yes (request from server), 2: yes (response from local)]");
- *     scanf("%d", &(test_mail_data->meeting_request_status));
- *
- *     if ( test_mail_data->meeting_request_status == 1
- *             || test_mail_data->meeting_request_status == 2 ) {
- *             time_t current_time;
- *             meeting_req = malloc(sizeof(email_meeting_request_t));
- *             memset(meeting_req, 0x00, sizeof(email_meeting_request_t));
- *
- *             meeting_req->meeting_response     = 1;
- *             current_time = time(NULL);
- *             gmtime_r(&current_time, &(meeting_req->start_time));
- *             gmtime_r(&current_time, &(meeting_req->end_time));
- *             meeting_req->location = malloc(strlen("Seoul") + 1);
- *             memset(meeting_req->location, 0x00, strlen("Seoul") + 1);
- *             strcpy(meeting_req->location, "Seoul");
- *             strcpy(meeting_req->global_object_id, "abcdef12345");
- *
- *             meeting_req->time_zone.offset_from_GMT = 9;
- *             strcpy(meeting_req->time_zone.standard_name, "STANDARD_NAME");
- *             gmtime_r(&current_time, &(meeting_req->time_zone.standard_time_start_date));
- *             meeting_req->time_zone.standard_bias = 3;
- *
- *             strcpy(meeting_req->time_zone.daylight_name, "DAYLIGHT_NAME");
- *             gmtime_r(&current_time, &(meeting_req->time_zone.daylight_time_start_date));
- *             meeting_req->time_zone.daylight_bias = 7;
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- *     }
+ * @param[in] input_mail_data             The mail to be saved
+ * @param[in] input_attachment_data_list  The mailbox structure for saving email
+ * @param[in] input_attachment_count      The mail attachment count
+ * @param[in] input_meeting_request       Specifies if the mail comes from composer
+ * @param[in] input_from_eas              Specifies if the mail will be saved on server
  *
- *     if((err = email_add_mail(test_mail_data, attachment_data, attachment_count, meeting_req, from_eas)) != EMAIL_ERROR_NONE)
- *             printf("email_add_mail failed. [%d]\n", err);
- *     else
- *             printf("email_add_mail success.\n");
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- *     if(attachment_data)
- *             email_free_attachment_data(&attachment_data, attachment_count);
+ * @see #email_mail_data_t and #email_mailbox_t
+ *
+ * @code
+ * #include "email-api-mail.h"
+ * int _test_add_mail ()
+ * {
+ *  int                    i = 0;
+ *  int                    account_id = 0;
+ *  int                    from_eas = 0;
+ *  int                    attachment_count = 0;
+ *  int                    err = EMAIL_ERROR_NONE;
+ *  char                   arg[50] = { 0 , };
+ *  char                  *body_file_path = MAILHOME"/tmp/mail.txt";
+ *  email_mailbox_t         *mailbox_data = NULL;
+ *  email_mail_data_t       *test_mail_data = NULL;
+ *  email_attachment_data_t *attachment_data = NULL;
+ *  email_meeting_request_t *meeting_req = NULL;
+ *  FILE                  *body_file;
+ *
+ *  printf("\n > Enter account id : ");
+ *  scanf("%d", &account_id);
+ *
+ *
+ *  memset(arg, 0x00, 50);
+ *  printf("\n > Enter mailbox name : ");
+ *  scanf("%s", arg);
+ *
+ *  email_get_mailbox_by_name(account_id, arg, &mailbox_data);
+ *
+ *  test_mail_data = malloc(sizeof(email_mail_data_t));
+ *  memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
+ *
+ *  printf("\n From EAS? [0/1]> ");
+ *  scanf("%d", &from_eas);
+ *
+ *  test_mail_data->account_id        = account_id;
+ *  test_mail_data->save_status       = 1;
+ *  test_mail_data->flags_seen_field  = 1;
+ *  test_mail_data->file_path_plain   = strdup(body_file_path);
+ *  test_mail_data->mailbox_id        = mailbox_data->mailbox_id;
+ *  test_mail_data->mailbox_type      = mailbox_data->mailbox_type;
+ *  test_mail_data->full_address_from = strdup("<test1@test.com>");
+ *  test_mail_data->full_address_to   = strdup("<test2@test.com>");
+ *  test_mail_data->full_address_cc   = strdup("<test3@test.com>");
+ *  test_mail_data->full_address_bcc  = strdup("<test4@test.com>");
+ *  test_mail_data->subject           = strdup("Meeting request mail");
+ *
+ *  body_file = fopen(body_file_path, "w");
+ *
+ *  for(i = 0; i < 500; i++)
+ *      fprintf(body_file, "X2 X2 X2 X2 X2 X2 X2");
+ *  fflush(body_file);
+ *  fclose(body_file);
+ *
+ *  printf(" > Attach file? [0/1] : ");
+ *  scanf("%d",&attachment_count);
+ *
+ *  if ( attachment_count )  {
+ *      memset(arg, 0x00, 50);
+ *      printf("\n > Enter attachment name : ");
+ *      scanf("%s", arg);
+ *
+ *      attachment_data = malloc(sizeof(email_attachment_data_t));
+ *
+ *      attachment_data->attachment_name  = strdup(arg);
+ *
+ *      memset(arg, 0x00, 50);
+ *      printf("\n > Enter attachment absolute path : ");
+ *      scanf("%s",arg);
+ *
+ *      attachment_data->attachment_path  = strdup(arg);
+ *      attachment_data->save_status      = 1;
+ *      test_mail_data->attachment_count  = attachment_count;
+ *  }
+ *
+ *  printf("\n > Meeting Request? [0: no, 1: yes (request from server), 2: yes (response from local)]");
+ *  scanf("%d", &(test_mail_data->meeting_request_status));
+ *
+ *  if ( test_mail_data->meeting_request_status == 1
+ *      || test_mail_data->meeting_request_status == 2 ) {
+ *      time_t current_time;
+ *      meeting_req = malloc(sizeof(email_meeting_request_t));
+ *      memset(meeting_req, 0x00, sizeof(email_meeting_request_t));
+ *
+ *      meeting_req->meeting_response     = 1;
+ *      current_time = time(NULL);
+ *      gmtime_r(&current_time, &(meeting_req->start_time));
+ *      gmtime_r(&current_time, &(meeting_req->end_time));
+ *      meeting_req->location = malloc(strlen("Seoul") + 1);
+ *      memset(meeting_req->location, 0x00, strlen("Seoul") + 1);
+ *      strcpy(meeting_req->location, "Seoul");
+ *      strcpy(meeting_req->global_object_id, "abcdef12345");
+ *
+ *      meeting_req->time_zone.offset_from_GMT = 9;
+ *      strcpy(meeting_req->time_zone.standard_name, "STANDARD_NAME");
+ *      gmtime_r(&current_time, &(meeting_req->time_zone.standard_time_start_date));
+ *      meeting_req->time_zone.standard_bias = 3;
+ *
+ *      strcpy(meeting_req->time_zone.daylight_name, "DAYLIGHT_NAME");
+ *      gmtime_r(&current_time, &(meeting_req->time_zone.daylight_time_start_date));
+ *      meeting_req->time_zone.daylight_bias = 7;
+ *
+ *  }
+ *
+ *  if((err = email_add_mail(test_mail_data, attachment_data, attachment_count, meeting_req, from_eas)) != EMAIL_ERROR_NONE)
+ *      printf("email_add_mail failed. [%d]\n", err);
+ *  else
+ *      printf("email_add_mail success.\n");
+ *
+ *  if(attachment_data)
+ *      email_free_attachment_data(&attachment_data, attachment_count);
  *
- *     if(meeting_req)
- *             email_free_meeting_request(&meeting_req, 1);
+ *  if(meeting_req)
+ *      email_free_meeting_request(&meeting_req, 1);
  *
- *     email_free_mail_data(&test_mail_data, 1);
- *     email_free_mailbox(&mailbox_data, 1);
+ *  email_free_mail_data(&test_mail_data, 1);
+ *  email_free_mailbox(&mailbox_data, 1);
  *
- *     printf("saved mail id = [%d]\n", test_mail_data->mail_id);
+ *  printf("saved mail id = [%d]\n", test_mail_data->mail_id);
  *
- *     return 0;
+ *  return 0;
  * }
  * @endcode
  */
 EXPORT_API int email_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas);
 
 /**
- * @fn email_add_read_receipt(int input_read_mail_id,  int *output_handle)
- * @brief      Add a read receipt mail. This function is invoked when user receives a mail with read report enable and wants to send a read report for the same.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in]  input_read_mail_id      Specifies the id of mail which has been read.
- * @param[out] output_receipt_mail_id  Specifies the receipt mail id .
- * @exception none
- * @see
- * @remarks N/A
+ * @brief Adds a read receipt mail.
+ * @details This function is invoked when a user receives a mail with read report enable and wants to send a read report for the same.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   input_read_mail_id      The ID of mail which has been read
+ * @param[out]  output_receipt_mail_id  The receipt mail ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id);
 
 /**
- * @fn email_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_composer)
- * @brief      Update a existing email information. This function is invoked when user wants to change some existing email information with new email information.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_data          Specifies the mail ID.
- * @param[in] input_attachment_data_list       Specifies the pointer of attachment data.
- * @param[in] input_attachment_count   Specifies the number of attachment data.
- * @param[in] input_meeting_request    Specifies the meeting request data.
- * @param[in] input_from_eas Specifies whether sync server.
-
- * @exception  none
- * @see        email_mail_data_t
+ * @brief Updates an existing email information.
+ * @details This function is invoked when a user wants to change some existing email information with new email information.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] input_mail_data             The mail ID
+ * @param[in] input_attachment_data_list  The pointer of attachment data
+ * @param[in] input_attachment_count      The number of attachment data
+ * @param[in] input_meeting_request       The meeting request data
+ * @param[in] input_from_eas              Specifies whether sync server
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_data_t
+ *
  * @code
  * #include "email-api-account.h"
  * int email_test_update_mail()
  * {
- *     int                    mail_id = 0;
- *     int                    err = EMAIL_ERROR_NONE;
- *     int                    test_attachment_data_count = 0;
- *     char                   arg[50];
- *     email_mail_data_t       *test_mail_data = NULL;
- *     email_attachment_data_t *test_attachment_data_list = NULL;
- *     email_meeting_request_t *meeting_req = NULL;
+ *  int                    mail_id = 0;
+ *  int                    err = EMAIL_ERROR_NONE;
+ *  int                    test_attachment_data_count = 0;
+ *  char                   arg[50];
+ *  email_mail_data_t       *test_mail_data = NULL;
+ *  email_attachment_data_t *test_attachment_data_list = NULL;
+ *  email_meeting_request_t *meeting_req = NULL;
  *
- *     printf("\n > Enter mail id : ");
- *     scanf("%d", &mail_id);
+ *  printf("\n > Enter mail id : ");
+ *  scanf("%d", &mail_id);
  *
- *     email_get_mail_data(mail_id, &test_mail_data);
+ *  email_get_mail_data(mail_id, &test_mail_data);
  *
- *     printf("\n > Enter Subject: ");
- *     scanf("%s", arg);
+ *  printf("\n > Enter Subject: ");
+ *  scanf("%s", arg);
  *
- *     test_mail_data->subject= strdup(arg);
+ *  test_mail_data->subject= strdup(arg);
  *
  *
  *
- *     if (test_mail_data->attachment_count > 0) {
- *             if ( (err = email_get_attachment_data_list(mail_id, &test_attachment_data_list, &test_attachment_data_count)) != EMAIL_ERROR_NONE ) {
- *                     printf("email_get_meeting_request() failed [%d]\n", err);
- *                     return -1;
- *             }
- *     }
+ *  if (test_mail_data->attachment_count > 0) {
+ *      if ( (err = email_get_attachment_data_list(mail_id, &test_attachment_data_list, &test_attachment_data_count)) != EMAIL_ERROR_NONE ) {
+ *          printf("email_get_meeting_request() failed [%d]\n", err);
+ *          return -1;
+ *      }
+ *  }
  *
- *     if ( test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
- *             || test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
- *             || test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
+ *  if ( test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
+ *      || test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
+ *      || test_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
  *
- *             if ( (err = email_get_meeting_request(mail_id, &meeting_req)) != EMAIL_ERROR_NONE ) {
- *                     printf("email_get_meeting_request() failed [%d]\n", err);
- *                     return -1;
- *             }
+ *      if ( (err = email_get_meeting_request(mail_id, &meeting_req)) != EMAIL_ERROR_NONE ) {
+ *          printf("email_get_meeting_request() failed [%d]\n", err);
+ *          return -1;
+ *      }
  *
- *             printf("\n > Enter meeting response: ");
- *             scanf("%d", (int*)&(meeting_req->meeting_response));
- *     }
+ *      printf("\n > Enter meeting response: ");
+ *      scanf("%d", (int*)&(meeting_req->meeting_response));
+ *  }
  *
- *     if ( (err = email_update_mail(test_mail_data, test_attachment_data_list, test_attachment_data_count, meeting_req, 0)) != EMAIL_ERROR_NONE)
- *                     printf("email_update_mail failed.[%d]\n", err);
- *             else
- *                     printf("email_update_mail success\n");
+ *  if ( (err = email_update_mail(test_mail_data, test_attachment_data_list, test_attachment_data_count, meeting_req, 0)) != EMAIL_ERROR_NONE)
+ *          printf("email_update_mail failed.[%d]\n", err);
+ *      else
+ *          printf("email_update_mail success\n");
  *
- *     if(test_mail_data)
- *             email_free_mail_data(&test_mail_data, 1);
+ *  if(test_mail_data)
+ *      email_free_mail_data(&test_mail_data, 1);
  *
- *     if(test_attachment_data_list)
- *             email_free_attachment_data(&test_attachment_data_list, test_attachment_data_count);
+ *  if(test_attachment_data_list)
+ *      email_free_attachment_data(&test_attachment_data_list, test_attachment_data_count);
  *
- *     if(meeting_req)
- *             email_free_meeting_request(&meeting_req, 1);
+ *  if(meeting_req)
+ *      email_free_meeting_request(&meeting_req, 1);
  *
- *     return 0;
+ *  return 0;
  * }
 
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas);
 
 /**
- * @fn email_update_mail_attribute(int input_account_id, int *input_mail_id_array, int input_mail_id_count, email_mail_attribute_type input_attribute_type, email_mail_attribute_value_t input_value)
- * @brief      This function update individual attribute of mail data.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in]  input_account_id        Specifies the id of account.
- * @param[in]  input_mail_id_array Specify the array list if mail id
- * @param[in]  input_mail_id_count Specify the count of mail id array
- * @param[in]  input_attribute_type Specify the attribute type which to update
- * @param[in]  input_value Specify the value of attribute
- * @exception none
- * @see
- * @remarks N/A
+ * @brief Updates an individual attribute of the mail data.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_account_id      The ID of account
+ * @param[in]  input_mail_id_array   The array list of mail IDs
+ * @param[in]  input_mail_id_count   The count of mail ID array
+ * @param[in]  input_attribute_type  The attribute type to update
+ * @param[in]  input_value           The value of attribute
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_update_mail_attribute(int input_account_id, int *input_mail_id_array, int input_mail_id_count, email_mail_attribute_type input_attribute_type, email_mail_attribute_value_t input_value);
 
 /**
- * @fn email_count_mail(email_mailbox_t* mailbox, int* total, int* unseen)
- * @brief      Get mail count from mailbox.This function is invoked when user wants to know how many toatl mails and out of that
- *             how many unseen mails are there in a given mailbox.
+ * @brief Gets the mail count.
+ * @details This function is invoked when a user wants to know how many total mails and out of that
+ *          how many unseen mails are there in a given mailbox.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  mailbox  The pointer of mailbox structure
+ * @param[out] total    The total email count
+ * @param[out] unseen   The unread email count
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mailbox  Specifies the pointer of mailbox structure.
- * @param[out] total   Total email count is saved here.
- * @param[out] unseen  Unread email count is saved here.
- * @exception  none
- * @see        email_mailbox_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_count_mail()
- *     {
- *             int total = 0;
- *             int unseen = 0;
- *             email_list_filter_t *filter_list = NULL;
- *             int err = EMAIL_ERROR_NONE;
- *             int i = 0;
- *             filter_list = malloc(sizeof(email_list_filter_t) * 3);
- *             memset(filter_list, 0 , sizeof(email_list_filter_t) * 3);
- *
- *             filter_list[0].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
- *             filter_list[0].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SUBJECT;
- *             filter_list[0].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
- *             filter_list[0].list_filter_item.rule.key_value.string_type_value   = strdup("RE");
- *             filter_list[0].list_filter_item.rule.case_sensitivity              = false;
- *
- *             filter_list[1].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
- *             filter_list[1].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_OR;
- *
- *             filter_list[2].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
- *             filter_list[2].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_TO;
- *             filter_list[2].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
- *             filter_list[2].list_filter_item.rule.key_value.string_type_value   = strdup("RE");
- *             filter_list[2].list_filter_item.rule.case_sensitivity              = false;
- *
- *             if(EMAIL_ERROR_NONE == email_count_mail(filter_list, 3, &total, &unseen))
- *                     printf("\n Total: %d, Unseen: %d \n", total, unseen);
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_count_mail()
+ *  {
+ *      int total = 0;
+ *      int unseen = 0;
+ *      email_list_filter_t *filter_list = NULL;
+ *      int err = EMAIL_ERROR_NONE;
+ *      int i = 0;
+ *      filter_list = malloc(sizeof(email_list_filter_t) * 3);
+ *      memset(filter_list, 0 , sizeof(email_list_filter_t) * 3);
+ *
+ *      filter_list[0].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
+ *      filter_list[0].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SUBJECT;
+ *      filter_list[0].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
+ *      filter_list[0].list_filter_item.rule.key_value.string_type_value   = strdup("RE");
+ *      filter_list[0].list_filter_item.rule.case_sensitivity              = false;
+ *
+ *      filter_list[1].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
+ *      filter_list[1].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_OR;
+ *
+ *      filter_list[2].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
+ *      filter_list[2].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_TO;
+ *      filter_list[2].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
+ *      filter_list[2].list_filter_item.rule.key_value.string_type_value   = strdup("RE");
+ *      filter_list[2].list_filter_item.rule.case_sensitivity              = false;
+ *
+ *      if(EMAIL_ERROR_NONE == email_count_mail(filter_list, 3, &total, &unseen))
+ *          printf("\n Total: %d, Unseen: %d \n", total, unseen);
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_count_mail(email_list_filter_t *input_filter_list, int input_filter_count, int *output_total_mail_count, int *output_unseen_mail_count);
 
 /**
- * @fn email_get_max_mail_count(int *Count)
- * @brief      Gets the max count of mails which can be supported by email-service
+ * @brief Gets the max count of mails which can be supported by email-service.
+ *
+ * @param[out]  count  The max count of mails which can be supported by email-service
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[out] Count   max count of mails which can be supported by email-service
- * @exception          none
  * @code
- *     #include "email-api-account.h"
- *             bool
- *             _api_sample_get_max_mail_count()
- *             {
- *                     int max_count = -1;
- *
- *                     if(EMAIL_ERROR_NONE == email_get_max_mail_count(&max_count))
- *                             printf("\n\t>>>>> email_get_max_mail_count() return [%d]\n\n", max_count);
- *     }
+ *      #include "email-api-account.h"
+ *          bool
+ *          _api_sample_get_max_mail_count()
+ *          {
+ *              int max_count = -1;
+ *
+ *              if(EMAIL_ERROR_NONE == email_get_max_mail_count(&max_count))
+ *                  printf("\n\t>>>>> email_get_max_mail_count() returns [%d]\n\n", max_count);
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_max_mail_count(int *count);
 
 /**
- * @fn         email_delete_mail(email_mailbox_t* mailbox, int *mail_ids, int num, int from_server)
- * @brief      Delete a mail or multiple mails.Based on from_server value this function will delte a mail or multiple mails from server or loaclly.
- * @param[in] mailbox                  Reserved.
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_server value will be 0
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_server value will be 1/0
- *
- * @param[in] mail_ids[]       Specifies the array of mail id.
- * @param[in] num                      Specifies the number of mail id.
- * @param[in] from_server      Specifies whether mails are deleted from server.
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception none
+ * @brief Deletes a mail or multiple mails.
+ * @details Based on from_server value, this function will delete a mail or multiple mails from the server or locally.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks If the incoming_server_type is #EMAIL_SERVER_TYPE_POP3 then from_server value will be @c 0. \n
+ *          If the incoming_server_type is #EMAIL_SERVER_TYPE_IMAP4 then from_server value will be 1/0.
+ *
+ * @param[in] mailbox      Reserved
+ * @param[in] mail_ids[]   The array of mail IDs
+ * @param[in] num          The number of mail IDs
+ * @param[in] from_server  The flag that specifies whether mails are deleted from server
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_delete_mail()
- *     {
- *             int count, i, mail_id=0, mailbox_id =0;
- *
- *             printf("\n > Enter Mail_id: ");
- *             scanf("%d",&mail_id);
- *             printf("\n > Enter Mailbox ID: ");
- *             scanf("%d",&mailbox_id);
- *             if(EMAIL_ERROR_NONE == email_delete_mail(mailbox_id, &mail_id, 1, 1))
- *                     printf("\n email_delete_mail success");
- *             else
- *                     printf("\n email_delete_mail failed");
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_delete_mail()
+ *  {
+ *      int count, i, mail_id=0, mailbox_id =0;
+ *
+ *      printf("\n > Enter Mail_id: ");
+ *      scanf("%d",&mail_id);
+ *      printf("\n > Enter Mailbox ID: ");
+ *      scanf("%d",&mailbox_id);
+ *      if(EMAIL_ERROR_NONE == email_delete_mail(mailbox_id, &mail_id, 1, 1))
+ *          printf("\n email_delete_mail success");
+ *      else
+ *          printf("\n email_delete_mail failed");
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_delete_mail(int input_mailbox_id, int *input_mail_ids, int input_num, int input_from_server);
 
 
 /**
- * @fn email_delete_all_mails_in_mailbox(int input_mailbox_id, int input_from_server)
- * @brief      Delete all mail from a mailbox.
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_server value will be 0
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_server value will be 1/0
+ * @brief Deletes all mails from a mailbox.
+ * @details  If the incoming_server_type is #EMAIL_SERVER_TYPE_POP3 then @a from_server value will be @c 0. \n
+ *           If the incoming_server_type is #EMAIL_SERVER_TYPE_IMAP4 then @a from_server value will be 1/0.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mailbox                  Specifies the structure of mailbox.
- * @param[in] from_server              Specifies whether mails are also deleted from server.
- * @exception          none
- * @see                email_mailbox_t
-
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mailbox      The structure of mailbox
+ * @param[in] from_server  The flag that specifies whether mails are also deleted from server
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_delete_all_mails_in_mailbox()
- *     {
- *             int count, mailbox_id =0;
- *
- *             printf("\n > Enter mailbox_id: ");
- *             scanf("%d",&mailbox_id);
- *
- *             if (EMAIL_ERROR_NONE != email_delete_all_mails_in_mailbox(mailbox_id, 0))
- *                     printf("email_delete_all_mails_in_mailbox failed");
- *             else
- *                     printf("email_delete_all_mails_in_mailbox Success");
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_delete_all_mails_in_mailbox()
+ *  {
+ *      int count, mailbox_id =0;
+ *
+ *      printf("\n > Enter mailbox_id: ");
+ *      scanf("%d",&mailbox_id);
+ *
+ *      if (EMAIL_ERROR_NONE != email_delete_all_mails_in_mailbox(mailbox_id, 0))
+ *          printf("email_delete_all_mails_in_mailbox failed");
+ *      else
+ *          printf("email_delete_all_mails_in_mailbox Success");
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_delete_all_mails_in_mailbox(int input_mailbox_id, int input_from_server);
 
 /**
- *  @fn email_clear_mail_data()
- * @brief      delete email data from storage. This API will be used by the Settings Application
+ * @brief Deletes email data from the storage.
+ * @details This API will be used by Settings Application.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @return #EMAIL_ERROR_NONE on success,
+ *         otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception          none
- * @see                none
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_clear_mail_data()
- *     {
- *             if(EMAIL_ERROR_NONE == email_clear_mail_data())
- *                     //success
- *             else
- *                     //failure
- *
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_clear_mail_data()
+ *  {
+ *      if(EMAIL_ERROR_NONE == email_clear_mail_data())
+ *          //success
+ *      else
+ *          //failure
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int  email_clear_mail_data();
 
 
 /**
- * @fn email_add_attachment(int mail_id, email_attachment_data_t* attachment)
- * @brief      Append a attachment to email.This function is invoked when user wants to add attachment to existing mail.
+ * @brief Appends an attachment to an email.
+ * @details This function is invoked when a user wants to add attachment to an existing mail.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id     The mail ID
+ * @param[in] attachment  The structure of attachment
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_attachment_data_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the mail ID.
- * @param[in] attachment       Specifies the structure of attachment.
- * @exception          none
- * @see email_attachment_data_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_mail_add_attachment()
- *     {
- *             int mail_id = 0;
- *             email_attachment_data_t attachment;
- *
- *             printf("\n > Enter Mail Id: ");
- *             scanf("%d", &mail_id);
- *             memset(&attachment, 0x00, sizeof(email_attachment_data_t));
- *             printf("\n > Enter attachment name: ");
- *             attachment.name = strdup("Test");
- *             printf("\n > Enter attachment absolute path: ");
- *             attachment.savename = strdup("/tmp/test.txt");
- *             attachment.next = NULL;
- *             if(EMAIL_ERROR_NONE != email_add_attachment(mail_id, &attachment))
- *                     printf("email_add_attachment failed\n");
- *             else
- *                     printf(email_add_attachment success\n");
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_mail_add_attachment()
+ *  {
+ *      int mail_id = 0;
+ *      email_attachment_data_t attachment;
+ *
+ *      printf("\n > Enter Mail Id: ");
+ *      scanf("%d", &mail_id);
+ *      memset(&attachment, 0x00, sizeof(email_attachment_data_t));
+ *      printf("\n > Enter attachment name: ");
+ *      attachment.name = strdup("Test");
+ *      printf("\n > Enter attachment absolute path: ");
+ *      attachment.savename = strdup("/tmp/test.txt");
+ *      attachment.next = NULL;
+ *      if(EMAIL_ERROR_NONE != email_add_attachment(mail_id, &attachment))
+ *          printf("email_add_attachment failed\n");
+ *      else
+ *          printf(email_add_attachment success\n");
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_add_attachment(int mail_id, email_attachment_data_t* attachment);
 
 
 /**
- * @fn email_delete_attachment(int mail_id, const char * attachment_id)
- * @brief      delete a attachment from email.This function is invoked when user wants to delete a attachment from existing mailbased on mail Id and attachment Id
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id                  Specifies the mail ID.
- * @param[in] attachment_id            Specifies the attachment id.
- * @exception          none
- * @see                email_mailbox_t
+ * @brief Deletes an attachment from email.
+ * @details This function is invoked when a user wants to delete a attachment from an existing mail based on mail ID and attachment ID.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id        The mail ID
+ * @param[in] attachment_id  The attachment ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_mail_delete_attachment()
- *     {
- *
- *             if(EMAIL_ERROR_NONE != email_delete_attachment(1))
- *                     //failure
- *             else
- *                     //success
- *     }
+ *  #include "email-api-account.h"
+ *  bool
+ *  _api_sample_mail_delete_attachment()
+ *  {
+ *
+ *      if(EMAIL_ERROR_NONE != email_delete_attachment(1))
+ *          //failure
+ *      else
+ *          //success
+ *  }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_delete_attachment(int attachment_id);
 
 /**
- * @fn email_get_attachment_data(int mail_id, const char* attachment_id, email_attachment_data_t** attachment)
- * @brief      Get a mail attachment.This function is invoked when user wants to get the attachment information based on attachment id for the specified mail Id.
+ * @brief Gets a mail attachment.
+ * @details This function is invoked when a user wants to get the attachment information based on an attachment ID for the specified mail ID.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  mail_id        The mail ID
+ * @param[in]  attachment_id  The buffer that an attachment ID is saved
+ * @param[out] attachment     The returned attachment
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t and #email_mail_attachment_info_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the mail ID.
- * @param[in] attachment_id    Specifies the buffer that a attachment ID been saved.
- * @param[out] attachment      The returned attachment is save here.
- * @exception          none
- * @see                email_mailbox_t and email_mail_attachment_info_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_mail_get_attachment_info()
- *     {
- *             email_mail_attachment_info_t *mail_attach_info = NULL;
- *             int mail_id = 0;
- *             char arg[10];
- *             int err = EMAIL_ERROR_NONE;
- *
- *
- *             printf("\n > Enter Mail Id: ");
- *             scanf("%d", &mail_id);
- *             printf("> attachment Id\n");
- *             scanf("%s",arg);
- *             if (EMAIL_ERROR_NONE != email_get_attachment_data(mail_id, &mail_attach_info))
- *                     printf("email_get_attachment_data failed\n");
- *             else
- *             {
- *                     printf("email_get_attachment_data SUCCESS\n");
- *                     //do something
- *                     email_free_attachment_data(&mail_attach_info,1);
- *             }
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_mail_get_attachment_info()
+ *      {
+ *      email_mail_attachment_info_t *mail_attach_info = NULL;
+ *      int mail_id = 0;
+ *      char arg[10];
+ *      int err = EMAIL_ERROR_NONE;
+ *
+ *
+ *      printf("\n > Enter Mail Id: ");
+ *      scanf("%d", &mail_id);
+ *      printf("> attachment Id\n");
+ *      scanf("%s",arg);
+ *      if (EMAIL_ERROR_NONE != email_get_attachment_data(mail_id, &mail_attach_info))
+ *          printf("email_get_attachment_data failed\n");
+ *      else
+ *      {
+ *          printf("email_get_attachment_data SUCCESS\n");
+ *          //do something
+ *          email_free_attachment_data(&mail_attach_info,1);
+ *      }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_attachment_data(int attachment_id, email_attachment_data_t** attachment);
 
 /**
- * @fn email_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
- * @brief      Get a list of mail attachment.This function is invoked when user wants to get the information of attachment list based on mail id.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_id            Specifies the mail ID.
- * @param[out] output_attachment_data  The returned attachment list is save here.
- * @param[out] output_attachment_count The returned count of attachment list.
- * @exception  none
- * @see                email_attachment_data_t
- * @remarks N/A
+ * @brief Gets a list of mail attachments.
+ * @details This function is invoked when a user wants to get the the attachment list information based on the mail ID.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_mail_id            The mail ID
+ * @param[out] output_attachment_data   The returned attachment list is save here
+ * @param[out] output_attachment_count  The returned count of attachment list
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_attachment_data_t
  */
 EXPORT_API int email_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
 
 /**
- * @fn email_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count)
- * @brief      Free allocated memroy for email attachment.
+ * @brief Frees the allocated memory for email attachments.
+ *
+ * @since_tizen 2.3
+ *
+ * @param[in] atch_info  The pointer of mail attachment structure pointer
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_attachment_info_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] atch_info        Specifies the pointer of mail attachment structure pointer.
- * @exception          none
- * @see                email_mail_attachment_info_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_mail_free_attachment_info()
- *     {
- *             email_mailbox_t mailbox;
- *             email_mail_attachment_info_t *mail_attach_info = NULL;
- *             int mail_id = 0,account_id = 0;
- *             char arg[10];
- *             int err = EMAIL_ERROR_NONE;
- *
- *             memset(&mailbox, 0x00, sizeof(email_mailbox_t));
- *
- *             printf("\n > Enter Mail Id: ");
- *             scanf("%d", &mail_id);
- *             printf("\n > Enter account Id: ");
- *             scanf("%d", &account_id);
- *             printf("> attachment Id\n");
- *             scanf("%s",arg);
- *             mailbox.account_id = account_id;
- *             if (EMAIL_ERROR_NONE != email_get_attachment_data(&mailbox, mail_id, &mail_attach_info))
- *                     printf("email_get_attachment_data failed\n");
- *             else
- *             {
- *                     printf("email_get_attachment_data SUCCESS\n");
- *                     //do something
- *                     email_free_attachment_info(&mail_attach_info,1);
- *             }
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_mail_free_attachment_info()
+ *      {
+ *      email_mailbox_t mailbox;
+ *      email_mail_attachment_info_t *mail_attach_info = NULL;
+ *      int mail_id = 0,account_id = 0;
+ *      char arg[10];
+ *      int err = EMAIL_ERROR_NONE;
+ *
+ *      memset(&mailbox, 0x00, sizeof(email_mailbox_t));
+ *
+ *      printf("\n > Enter Mail Id: ");
+ *      scanf("%d", &mail_id);
+ *      printf("\n > Enter account Id: ");
+ *      scanf("%d", &account_id);
+ *      printf("> attachment Id\n");
+ *      scanf("%s",arg);
+ *      mailbox.account_id = account_id;
+ *      if (EMAIL_ERROR_NONE != email_get_attachment_data(&mailbox, mail_id, &mail_attach_info))
+ *          printf("email_get_attachment_data failed\n");
+ *      else
+ *      {
+ *          printf("email_get_attachment_data SUCCESS\n");
+ *          //do something
+ *          email_free_attachment_info(&mail_attach_info,1);
+ *      }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count);
 
 /*-----------------------------------------------------------
-                                       Mail Information API
+                    Mail Information API
   -----------------------------------------------------------*/
 
 
 /**
- * @fn email_query_mails(char *conditional_clause_string, email_mail_data_t** mail_list,  int *result_count)
- * @brief                          Query the mail list information from DB based on the mailbox name.
+ * @brief Queries the mail list information from the DB based on the mailbox name.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]      conditional_clause_string  The where clause string
+ * @param[in/out]  mail_list                  The pointer to the structure #email_mail_data_t
+ * @param[in/out]  result_count               The number of mails returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_list_item_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] conditional_clause_string  Specifies the where clause string.
- * @param[in/out] mail_list        Specifies the pointer to the structure email_mail_data_t.
- * @param[in/out] result_count     Specifies the number of mails returned.
- * @exception                      None
- * @see                            email_mail_list_item_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_query_mail()
- *     {
- *             email_mail_data_t *mail_list = NULL;
- *             char conditional_clause_string[500];
- *             int result_count = 0;
- *
- *             memset(conditional_clause_string, 0x00, 10);
- *             printf("\n > Enter where clause: ");
- *             scanf("%s", conditional_clause_string);
- *
- *
- *             if (EMAIL_ERROR_NONE != email_query_mails(conditional_clause_string, &mail_list, &result_count))
- *                     printf("email_query_mails failed \n");
- *             else {
- *                     printf("Success\n");
- *                     //do something
- *                     free(mail_list);
- *             }
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_query_mail()
+ *      {
+ *      email_mail_data_t *mail_list = NULL;
+ *      char conditional_clause_string[500];
+ *      int result_count = 0;
+ *
+ *      memset(conditional_clause_string, 0x00, 10);
+ *      printf("\n > Enter where clause: ");
+ *      scanf("%s", conditional_clause_string);
+ *
+ *
+ *      if (EMAIL_ERROR_NONE != email_query_mails(conditional_clause_string, &mail_list, &result_count))
+ *          printf("email_query_mails failed \n");
+ *      else {
+ *          printf("Success\n");
+ *          //do something
+ *          free(mail_list);
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_query_mails(char *conditional_clause_string, email_mail_data_t** mail_list,  int *result_count);
 
 /**
- * @fn email_query_mail_list(char *input_conditional_clause_string, email_mail_list_item_t** output_mail_list,  int *output_result_count)
- * @brief Query the mail list information from DB.
+ * @brief Queries the mail list information from the DB.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]      input_conditional_clause_string  The where clause string
+ * @param[in/out]  output_mail_list                 The pointer to the structure #email_mail_list_item_t
+ * @param[in/out]  output_result_count              The number of mails returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_list_item_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_conditional_clause_string  Specifies the where clause string.
- * @param[in/out] output_mail_list                             Specifies the pointer to the structure email_mail_list_item_t.
- * @param[in/out] output_result_count                  Specifies the number of mails returned.
- * @exception  None
- * @see                        email_mail_list_item_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_query_mail_list()
- *     {
- *             email_mail_list_item_t *mail_list = NULL;
- *             char conditional_clause_string[500];
- *             int result_count = 0;
- *
- *             memset(conditional_clause_string, 0x00, 10);
- *             printf("\n > Enter where clause: ");
- *             scanf("%s", conditional_clause_string);
- *
- *
- *             if (EMAIL_ERROR_NONE != email_query_mail_list(conditional_clause_string, &mail_list, &result_count))
- *                     printf("email_query_mail_list failed \n");
- *             else {
- *                     printf("Success\n");
- *                     //do something
- *                     free(mail_list);
- *             }
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_query_mail_list()
+ *      {
+ *      email_mail_list_item_t *mail_list = NULL;
+ *      char conditional_clause_string[500];
+ *      int result_count = 0;
+ *
+ *      memset(conditional_clause_string, 0x00, 10);
+ *      printf("\n > Enter where clause: ");
+ *      scanf("%s", conditional_clause_string);
+ *
+ *
+ *      if (EMAIL_ERROR_NONE != email_query_mail_list(conditional_clause_string, &mail_list, &result_count))
+ *          printf("email_query_mail_list failed \n");
+ *      else {
+ *          printf("Success\n");
+ *          //do something
+ *          free(mail_list);
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_query_mail_list(char *input_conditional_clause_string, email_mail_list_item_t** output_mail_list,  int *output_result_count);
 
 /**
- * @fn email_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
- * @brief      Get a mail by its mail id. This function is invoked when user wants to get a mail based on mail id existing in DB.
+ * @brief Gets a mail by its mail ID.
+ * @details This function is invoked when a user wants to get a mail based on mail ID existing in the DB.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_mail_id     The mail ID
+ * @param[out] output_mail_data  The returned mail is save here
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_data_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_id    specifies the mail id.
- * @param[out] output_mail_data                The returned mail is save here.
- * @exception          none
- * @see        email_mail_data_t
-
  * @code
  *    #include "email-api-account.h"
- *     bool
- *     _api_sample_get_mail_data()
- *     {
- *             email_mail_data_t *mail = NULL;
- *             int mail_id = 0 ;
- *             int err = EMAIL_ERROR_NONE;
- *
- *             printf("\n > Enter mail id: ");
- *             scanf("%d", &mail_id);
- *
- *             if (EMAIL_ERROR_NONE != email_get_mail_data(mail_id, &mail))
- *                     printf("email_get_mail_data failed\n");
- *             else
- *             {
- *                     printf("email_get_mail_data SUCCESS\n");
- *                     //do something
- *                     email_free_mail_data(&mail,1);
- *             }
- *     }
+ *      bool
+ *      _api_sample_get_mail_data()
+ *      {
+ *      email_mail_data_t *mail = NULL;
+ *      int mail_id = 0 ;
+ *      int err = EMAIL_ERROR_NONE;
+ *
+ *      printf("\n > Enter mail id: ");
+ *      scanf("%d", &mail_id);
+ *
+ *      if (EMAIL_ERROR_NONE != email_get_mail_data(mail_id, &mail))
+ *          printf("email_get_mail_data failed\n");
+ *      else
+ *      {
+ *          printf("email_get_mail_data SUCCESS\n");
+ *          //do something
+ *          email_free_mail_data(&mail,1);
+ *      }
+ *      }
  * @endcode
- * @remarks N/A
  */
 
 EXPORT_API int email_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data);
 
 
 /**
- * @fn email_free_mail_data(email_mail_data_t** mail_list, int count)
- * @brief      Free allocated memroy for emails.
+ * @brief Frees the allocated memory for emails.
+ *
+ * @since_tizen 2.3
+ *
+ * @param[in] mail_list  The pointer of mail structure pointer
+ * @param[in] count      The count of mails
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_data_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_list        Specifies the pointer of mail structure pointer.
- * @param[in] count            Specifies the count of mails.
- * @exception          none
- * @see                 email_mail_data_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_free_mail()
- *     {
- *             email_mail_data_t *mail;
- *
- *             //fill the mail structure
- *             //count - number of mail structure user want to free
- *              if(EMAIL_ERROR_NONE == email_free_mail_data(&mail,count))
- *                     //success
- *              else
- *                     //failure
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_free_mail()
+ *      {
+ *      email_mail_data_t *mail;
+ *
+ *      //fill the mail structure
+ *      //count - number of mail structure a user want to free
+ *      if(EMAIL_ERROR_NONE == email_free_mail_data(&mail,count))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_free_mail_data(email_mail_data_t** mail_list, int count);
 
 /*-----------------------------------------------------------
-                                       Mail Flag API
+                     Mail Flag API
   -----------------------------------------------------------*/
 
 /**
- * @fn email_set_flags_field(int *mail_ids, int num, email_flags_field_type field_type, int value, int onserver)
- * @brief      Change email flags field.
- *             If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_server value will be 0
- *                     If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_server value will be 1/0
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id  Specifies account ID.
- * @param[in] mail_ids         Specifies the array of mail ID.
- * @param[in] num              Specifies the number of mail ID.
- * @param[in] field_type  Specifies the field type what you want to set. Refer email_flags_field_type.
- * @param[in] value          Specifies the value what you want to set.
- * @param[in] onserver         Specifies whether mail Flag updation in server
- * @exception          none
- * @see                none
+ * @brief Changes an email flags field.
+ * @details If the incoming_server_type is EMAIL_SERVER_TYPE_POP3 then from_server value will be 0. \n
+ *          If the incoming_server_type is EMAIL_SERVER_TYPE_IMAP4 then from_server value will be 1/0.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] account_id  The account ID
+ * @param[in] mail_ids    The array of mail IDs
+ * @param[in] num         The number of mail IDs
+ * @param[in] field_type  The field type to be set \n 
+ *                        See #email_flags_field_type.
+ * @param[in] value       The value to be set
+ * @param[in] onserver    The flag indicating whether mail flag updating is in server
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_set_flags_field()
- *     {
- *             int account_id = 0;
- *                     int mail_id = 0;
- *                     int err = EMAIL_ERROR_NONE;
- *
- *                     printf("\n > Enter account id: ");
- *                     scanf("%d", &account_id);
- *
- *                     printf("\n > Enter mail id: ");
- *                     scanf("%d", &mail_id);
- *                     if (EMAIL_ERROR_NONE != email_set_flags_field(&account_id, &mail_id, EMAIL_FLAGS_SEEN_FIELD, 1, 0))
- *                             printf("email_set_flags_field failed\n");
- *                     else
- *                     {
- *                             printf("email_set_flags_field succeed\n");
- *                             //do something
- *                     }
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_set_flags_field()
+ *      {
+ *          int account_id = 0;
+ *          int mail_id = 0;
+ *          int err = EMAIL_ERROR_NONE;
+ *
+ *          printf("\n > Enter account id: ");
+ *          scanf("%d", &account_id);
+ *
+ *          printf("\n > Enter mail id: ");
+ *          scanf("%d", &mail_id);
+ *          if (EMAIL_ERROR_NONE != email_set_flags_field(&account_id, &mail_id, EMAIL_FLAGS_SEEN_FIELD, 1, 0))
+ *              printf("email_set_flags_field failed\n");
+ *          else
+ *          {
+ *              printf("email_set_flags_field succeed\n");
+ *              //do something
+ *          }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_set_flags_field(int account_id, int *mail_ids, int num, email_flags_field_type field_type, int value, int onserver);
 
 /* -----------------------------------------------------------
-                                       Mail Move API
+                    Mail Move API
    -----------------------------------------------------------*/
 
 /**
- * @fn email_move_mail_to_mailbox(int *mail_ids, int num, int input_target_mailbox_id)
- * @brief      Move a email to another mailbox.
+ * @brief Moves an email to another mailbox.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id                  The array of mail ID
+ * @param[in] num                      The count of mail IDs
+ * @param[in] input_target_mailbox_id  The mailbox ID for moving email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id                                  Specifies the array of mail ID.
- * @param[in] num                                              Specifies the count of mail IDs.
- * @param[in] input_target_mailbox_id  Specifies the mailbox ID for moving email.
- * @exception          none
- * @see                email_mailbox_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_move_mail_to_mailbox()
- *     {
- *             int mail_id = 0;
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_move_mail_to_mailbox()
+ *      {
+ *      int mail_id = 0;
  *      int mailbox_id = 0;
- *             int err = EMAIL_ERROR_NONE;
+ *      int err = EMAIL_ERROR_NONE;
 
- *             printf("\n > Enter mail_id: ");
- *             scanf("%d",&mail_id);
+ *      printf("\n > Enter mail_id: ");
+ *      scanf("%d",&mail_id);
  *
- *             printf("\n > Enter target mailbox_id: ");
- *             scanf("%d",&mailbox_id);
+ *      printf("\n > Enter target mailbox_id: ");
+ *      scanf("%d",&mailbox_id);
  *
- *             if(EMAIL_ERROR_NONE == email_move_mail_to_mailbox(&mail_id,     1, mailbox_id))
- *                     printf("Success\n");
+ *      if(EMAIL_ERROR_NONE == email_move_mail_to_mailbox(&mail_id,    1, mailbox_id))
+ *         printf("Success\n");
  *
- *     }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_move_mail_to_mailbox(int *mail_ids, int num, int input_target_mailbox_id);
 
 /**
- * @fn email_move_all_mails_to_mailbox(int input_source_mailbox_id, int input_target_mailbox_id)
- * @brief      Move all email to another mailbox.
+ * @brief Moves all emails to another mailbox.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] input_source_mailbox_id  The source mailbox ID for moving email
+ * @param[in] input_target_mailbox_id  The destination mailbox ID for moving email
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_source_mailbox_id  Specifies the source mailbox ID for moving email.
- * @param[in] input_target_mailbox_id  Specifies the destination mailbox ID for moving email.
- * @exception          none
- * @see                email_mailbox_t
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_move_all_mails_to_mailbox()
- *     {
- *     int src_mailbox_id;
- *     int dst_mailbox_id;
- *             int total_count;
- *             int err = EMAIL_ERROR_NONE;
- *             char temp[128];
- *
- *
- *             // input mailbox information : need  account_id and name (src & dest)
- *             printf("src mail maibox id> ");
- *             scanf("%d", &src_mailbox_id);
- *
- *             // Destination mailbox
- *             printf("dest mailbox id> ");
- *             scanf("%d", &dst_mailbox_id);
- *
- *             if( EMAIL_ERROR_NONE == email_move_all_mails_to_mailbox(src_mailbox_id, dst_mailbox_id))
- *             {
- *                     printf("  fail email_move_all_mails_to_mailbox: \n");
- *             }
- *             else
- *                     //success
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_move_all_mails_to_mailbox()
+ *      {
+ *      int src_mailbox_id;
+ *      int dst_mailbox_id;
+ *      int total_count;
+ *      int err = EMAIL_ERROR_NONE;
+ *      char temp[128];
+ *
+ *
+ *      // input mailbox information : need  account_id and name (src & dest)
+ *      printf("src mail maibox id> ");
+ *      scanf("%d", &src_mailbox_id);
+ *
+ *      // Destination mailbox
+ *      printf("dest mailbox id> ");
+ *      scanf("%d", &dst_mailbox_id);
+ *
+ *      if( EMAIL_ERROR_NONE == email_move_all_mails_to_mailbox(src_mailbox_id, dst_mailbox_id))
+ *      {
+ *          printf("  fail email_move_all_mails_to_mailbox: \n");
+ *      }
+ *      else
+ *          //success
  * }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_move_all_mails_to_mailbox(int input_source_mailbox_id, int input_target_mailbox_id);
 
 /**
- * @fn email_move_mails_to_mailbox_of_another_account(int input_source_mailbox_id, int *mail_id_array, int mail_id_count, int input_target_mailbox_id, int *output_task_id)
- * @brief      Move mails to a mailbox of another account.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_source_mailbox_id  Specifies the source mailbox ID for moving email.
- * @param[in] mail_id_array    Specifies the source mail id array for moving email.
- * @param[in] mail_id_count    Specifies the count of source mail for moving email.
- * @param[in] input_target_mailbox_id  Specifies the destination mailbox ID for moving email.
- * @param[out] output_task_id  Specifies the task id for handling the task.
- * @exception          none
- * @remarks N/A
+ * @brief Moves mails to the mailbox of an another account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_source_mailbox_id  The source mailbox ID for moving email
+ * @param[in]  mail_id_array            The source mail ID array for moving email
+ * @param[in]  mail_id_count            The count of source mail for moving email
+ * @param[in]  input_target_mailbox_id  The destination mailbox ID for moving email
+ * @param[out] output_task_id           The Task ID for handling the task
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          or an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_move_mails_to_mailbox_of_another_account(int input_source_mailbox_id, int *mail_id_array, int mail_id_count, int input_target_mailbox_id, int *output_task_id);
 
 
 /**
- * @fn email_get_mail_list_ex(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, email_mail_list_item_t** output_mail_list, int *output_result_count)
- * @brief      Get the Mail List information from DB.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_filter_list                        Specifies the filter list
- * @param[in] input_filter_count               Specifies the number of filter
- * @param[in] input_sorting_rule_list  Specifies the sorting rule list.
- * @param[in] input_sorting_rule_count Specifies the number of sorting rule
- * @param[in] input_start_index                        Specifies start index for LIMIT clause of SQL query.
- * @param[in] input_limit_count                        Specifies limit count for LIMIT clause of SQL query.
- * @param[in/out] output_mail_list             Specifies the pointer to the structure email_mail_list_item_t.
- * @param[in/out] output_result_count  Specifies the number of mails returned.
- * @exception          none
- * @see             email_mail_list_item_t
+ * @brief Gets the Mail List information from the DB.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     input_filter_list         The filter list
+ * @param[in]     input_filter_count        The number of filter
+ * @param[in]     input_sorting_rule_list   The sorting rule list
+ * @param[in]     input_sorting_rule_count  The number of sorting rule
+ * @param[in]     input_start_index         The start index for LIMIT clause of SQL query
+ * @param[in]     input_limit_count         The limit count for LIMIT clause of SQL query
+ * @param[in/out] output_mail_list          The pointer to the structure #email_mail_list_item_t
+ * @param[in/out] output_result_count       The number of mails returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_list_item_t
+ *
  * @code
  * email_list_filter_t *filter_list = NULL;
  * email_list_sorting_rule_t *sorting_rule_list = NULL;
@@ -1015,482 +1102,543 @@ EXPORT_API int email_move_mails_to_mailbox_of_another_account(int input_source_m
  *
  * return FALSE;
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_mail_list_ex(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, email_mail_list_item_t** output_mail_list, int *output_result_count);
 
 /**
- * @fn email_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
- * @brief      Free allocated memroy for filers.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_filter_list        Specifies the pointer of filter structure.
- * @param[in] input_filter_count       Specifies the count of filter.
- * @exception          none
- * @see                        email_list_filter_t
- * @remarks N/A
+ * @brief Frees the allocated memory for filters.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in] input_filter_list   The pointer of filter structure
+ * @param[in] input_filter_count  The count of filter
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_list_filter_t
  */
 EXPORT_API int email_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count);
 
 /**
- * @fn email_get_mails(int account_id , int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_data_t** mail_list,  int* result_count)
- * @brief      Get the Mail List information from DB based on the mailbox name.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account ID
- * @param[in] mailbox_id               Specifies the mailbox id
- * @param[in] thread_id                Specifies the thread id. It can be EMAIL_LIST_TYPE_THREAD, EMAIL_LIST_TYPE_NORMAL or thread id.
- * @param[in] start_index      Specifies start index for LIMIT clause of SQL query.
- * @param[in] limit_count      Specifies limit count for LIMIT clause of SQL query.
- * @param[in] sorting          Specifies the sorting type.
- * @param[in/out] mail_list            Specifies the pointer to the structure email_mail_data_t.
- * @param[in/out] result_count Specifies the number of mails returned.
- * @exception          none
- * @see                 email_mail_data_t
-
+ * @brief Gets the Mail List information from the DB based on the mailbox name.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     account_id    The account ID
+ * @param[in]     mailbox_id    The mailbox ID
+ * @param[in]     thread_id     The thread ID \n
+ *                              It can be #EMAIL_LIST_TYPE_THREAD, #EMAIL_LIST_TYPE_NORMAL or thread ID.
+ * @param[in]     start_index   The start index for LIMIT clause of SQL query
+ * @param[in]     limit_count   The limit count for LIMIT clause of SQL query
+ * @param[in]     sorting       The sorting type
+ * @param[in/out] mail_list     The pointer to the structure #email_mail_data_t
+ * @param[in/out] result_count  The number of mails returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_data_t
+ *
  * @code
- *    #include "email-api-account.h"
- *     bool
- *     _api_sample_get_mail()
- *     {
- *             email_mail_data_t *mail_list = NULL;
- *             int mailbox_id;
- *             int result_count = 0;
- *             int account_id = 0;
- *             int err_code = EMAIL_ERROR_NONE;
- *
- *             printf("\n > Enter Mailbox id: ");
- *             scanf("%d", mailbox_id);
- *
- *             printf("\n > Enter Account_id: ");
- *             scanf("%d",&account_id);
- *
- *             if (EMAIL_ERROR_NONE != email_get_mails(account_id, mailbox_id, EMAIL_LIST_TYPE_NORMAL, 0, 100, EMAIL_SORT_DATETIME_HIGH,  &mail_list, &result_count)) {
- *                     printf("email_get_mails failed \n");
- *             }
- *             else {
- *                     printf("Success\n");
- *                     //do something
- *                     free(mail_list);
- *             }
- *
- *     }
+ *     #include "email-api-account.h"
+ *      bool
+ *      _api_sample_get_mail()
+ *      {
+ *      email_mail_data_t *mail_list = NULL;
+ *      int mailbox_id;
+ *      int result_count = 0;
+ *      int account_id = 0;
+ *      int err_code = EMAIL_ERROR_NONE;
+ *
+ *      printf("\n > Enter Mailbox id: ");
+ *      scanf("%d", mailbox_id);
+ *
+ *      printf("\n > Enter Account_id: ");
+ *      scanf("%d",&account_id);
+ *
+ *      if (EMAIL_ERROR_NONE != email_get_mails(account_id, mailbox_id, EMAIL_LIST_TYPE_NORMAL, 0, 100, EMAIL_SORT_DATETIME_HIGH,  &mail_list, &result_count)) {
+ *          printf("email_get_mails failed \n");
+ *      }
+ *      else {
+ *          printf("Success\n");
+ *          //do something
+ *          free(mail_list);
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 
 EXPORT_API int email_get_mails(int account_id , int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_data_t** mail_list,  int* result_count);
 
 /**
- * @fn email_get_mail_list(int account_id, int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count)
- * @brief      Get the Mail List information from DB based on the mailbox name.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account id
- * @param[in] mailbox_id               Specifies the mailbox id
- * @param[in] thread_id                Specifies the thread id. It can be EMAIL_LIST_TYPE_THREAD, EMAIL_LIST_TYPE_NORMAL or thread id.
- * @param[in] start_index      Specifies start index for LIMIT clause of SQL query.
- * @param[in] limit_count      Specifies limit count for LIMIT clause of SQL query.
- * @param[in] sorting          Specifies the sorting type.
- * @param[in/out] mail_list            Specifies the pointer to the structure email_mail_list_item_t.
- * @param[in/out] result_count Specifies the number of mails returned.
- * @exception          none
- * @see                 email_mail_list_item_t
-
+ * @brief Gets the Mail List information from the DB based on the mailbox name.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     account_id    The account ID
+ * @param[in]     mailbox_id    The mailbox ID
+ * @param[in]     thread_id     The thread ID \n
+ *                              It can be #EMAIL_LIST_TYPE_THREAD, #EMAIL_LIST_TYPE_NORMAL or thread ID.
+ * @param[in]     start_index   The start index for LIMIT clause of SQL query
+ * @param[in]     limit_count   The limit count for LIMIT clause of SQL query
+ * @param[in]     sorting       The sorting type
+ * @param[in/out] mail_list     The pointer to the structure #email_mail_list_item_t
+ * @param[in/out] result_count  The number of mails returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_list_item_t
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_get_mail()
- *     {
- *             email_mail_list_item_t *mail_list = NULL;
- *             int mailbox_id;
- *             int result_count = 0;
- *             int account_id = 0;
- *             int err_code = EMAIL_ERROR_NONE;
- *
- *             printf("\n > Enter Mailbox id: ");
- *             scanf("%d", mailbox_id);
- *
- *             printf("\n > Enter Account_id: ");
- *             scanf("%d",&account_id);
- *
- *             if (EMAIL_ERROR_NONE != email_get_mail_list(account_id, mailbox_id, EMAIL_LIST_TYPE_NORMAL, 0, 100, EMAIL_SORT_DATETIME_HIGH,  &mail_list, &result_count))
- *             {
- *                     printf("email_get_mail_list_ex failed \n");
- *             }
- *             else
- *             {
- *                     printf("Success\n");
- *                     //do something
- *                     free(mail_list);
- *             }
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_get_mail()
+ *      {
+ *      email_mail_list_item_t *mail_list = NULL;
+ *      int mailbox_id;
+ *      int result_count = 0;
+ *      int account_id = 0;
+ *      int err_code = EMAIL_ERROR_NONE;
+ *
+ *      printf("\n > Enter Mailbox id: ");
+ *      scanf("%d", mailbox_id);
+ *
+ *      printf("\n > Enter Account_id: ");
+ *      scanf("%d",&account_id);
+ *
+ *      if (EMAIL_ERROR_NONE != email_get_mail_list(account_id, mailbox_id, EMAIL_LIST_TYPE_NORMAL, 0, 100, EMAIL_SORT_DATETIME_HIGH,  &mail_list, &result_count))
+ *      {
+ *          printf("email_get_mail_list_ex failed \n");
+ *      }
+ *      else
+ *      {
+ *          printf("Success\n");
+ *          //do something
+ *          free(mail_list);
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_mail_list(int account_id, int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count);
 
 /**
- * @fn         email_get_mail_by_address(int account_id, int mailbox_id, email_email_address_list_t* addr_list,
-                                                                       int start_index, int limit_count, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count)
- * @brief      Get the Mail List information from the DB based on the mailbox name account_id and sender address.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id       Specifies the Account ID
- * @param[in] mailbox_id       Specifies the Mailbox ID
- * @param[in] addr_list                Specifies the addresses of senders. see email_email_address_list_t
- * @param[in] start_index              Specifies the first mail index of searched mail. This function will return mails whose index in the result list are from start_index to start_index + limit_count
- * @param[in] limit_count              Specifies the max number of returned mails.
- * @param[in] search_type   Specifies the search type.
- * @param[in] search_value  Specifies the search value.
- * @param[in] sorting                   Specifies the sorting order. see email_sort_type_t
- * @param[in/out] mail_list    Specifies the Pointer to the structure email_mail_list_item_t.
- * @param[in/out] result_count         Specifies the Number of searched Mails
- * @exception          none
- * @see                 email_email_address_list_t, email_sort_type_t, email_mail_list_item_t
- * @code
- * @endcode
- * @remarks N/A
+ * @brief Gets the Mail List information from the DB based on the mailbox name account_id and sender addresses.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     account_id    The Account ID
+ * @param[in]     mailbox_id    The Mailbox ID
+ * @param[in]     addr_list     The addresses of senders \n
+ *                              See #email_email_address_list_t.
+ * @param[in]     start_index   The first mail index of searched mail \n
+ *                              This function will return mails whose index in the result list are from @a start_index to @a start_index + @a limit_count.
+ * @param[in]     limit_count   The max number of returned mails
+ * @param[in]     search_type   The search type
+ * @param[in]     search_value  The search value
+ * @param[in]     sorting       The sorting order \n 
+ *                              See #email_sort_type_t.
+ * @param[in/out] mail_list     The Pointer to the structure #email_mail_list_item_t
+ * @param[in/out] result_count  The number of searched mails
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_email_address_list_t, #email_sort_type_t, #email_mail_list_item_t
  */
 EXPORT_API int email_get_mail_by_address(int account_id , int mailbox_id, email_email_address_list_t* addr_list,
-                                                                       int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count);
+                                         int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count);
 
 /**
- * @fn email_get_thread_information_by_thread_id(int thread_id, email_mail_data_t** thread_info)
- * @brief      Get the thread information for specific thread from DB based on the mailbox name.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] thread_id            Specifies the thread_id
- * @param[in/out] thread_info  Specifies the Pointer to the structure email_mail_data_t.
- * @exception          none
- * @see             email_mail_data_t
+ * @brief Gets thread information for a specific thread from DB based on the mailbox name.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     thread_id    The thread ID
+ * @param[in/out] thread_info  The pointer to the structure #email_mail_data_t
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_data_t
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_get_thread_information()
- *     {
- *             email_mail_data_t *thread_info = NULL;
- *             int thread_id = 0;
- *             int err_code = EMAIL_ERROR_NONE;
- *
- *             printf("\n > Enter thread_id: ");
- *             scanf("%d",&thread_id);
- *
- *             if ( EMAIL_ERROR_NONE != email_get_thread_information_by_thread_id(thread_id, &thread_info))
- *             {
- *                     printf("email_get_thread_information_by_thread_id failed :\n"); *
- *             }
- *             else
- *             {
- *                     printf("Success\n");
- *                     //do something
- *             }
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_get_thread_information()
+ *      {
+ *      email_mail_data_t *thread_info = NULL;
+ *      int thread_id = 0;
+ *      int err_code = EMAIL_ERROR_NONE;
+ *
+ *      printf("\n > Enter thread_id: ");
+ *      scanf("%d",&thread_id);
+ *
+ *      if ( EMAIL_ERROR_NONE != email_get_thread_information_by_thread_id(thread_id, &thread_info))
+ *      {
+ *          printf("email_get_thread_information_by_thread_id failed :\n"); *
+ *      }
+ *      else
+ *      {
+ *          printf("Success\n");
+ *          //do something
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_thread_information_by_thread_id(int thread_id, email_mail_data_t** thread_info);
 
 /**
- * @fn email_get_thread_information_ex(int thread_id, email_mail_list_item_t** thread_info)
- * @brief      Get the Mail List information for specific thread from DB based on the mailbox name.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] thread_id            Specifies the thread_id
- * @param[in/out] thread_info  Specifies the Pointer to the structure email_mail_list_item_t.
- * @exception          none
- * @see             email_mail_list_item_t
+ * @brief Gets Mail List information for a specific thread from the DB based on the mailbox name.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     thread_id    The thread ID
+ * @param[in/out] thread_info  The pointer to the structure #email_mail_list_item_t
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mail_list_item_t
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_get_thread_information_ex()
- *     {
- *             email_mail_list_item_t *thread_info = NULL;
- *             int thread_id = 0;
- *             int err_code = EMAIL_ERROR_NONE;
- *
- *             printf("\n > Enter thread_id: ");
- *             scanf("%d",&thread_id);
- *
- *             if ( EMAIL_ERROR_NONE != email_get_thread_information_ex(thread_id, &thread_info))
- *             {
- *                     printf("email_get_mail_list_of_thread failed :\n"); *
- *             }
- *             else
- *             {
- *                     printf("Success\n");
- *                     //do something
- *             }
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_get_thread_information_ex()
+ *      {
+ *      email_mail_list_item_t *thread_info = NULL;
+ *      int thread_id = 0;
+ *      int err_code = EMAIL_ERROR_NONE;
+ *
+ *      printf("\n > Enter thread_id: ");
+ *      scanf("%d",&thread_id);
+ *
+ *      if ( EMAIL_ERROR_NONE != email_get_thread_information_ex(thread_id, &thread_info))
+ *      {
+ *          printf("email_get_mail_list_of_thread failed :\n"); *
+ *      }
+ *      else
+ *      {
+ *          printf("Success\n");
+ *          //do something
+ *      }
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_thread_information_ex(int thread_id, email_mail_list_item_t** thread_info);
 
 /**
- * @fn email_retry_sending_mail( int mail_id, int timeout_in_sec)
- * @brief      Retry mail send
+ * @brief Retries to send a mail.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id         The mail ID for the mail to resend
+ * @param[in] timeout_in_sec  The timeout value in seconds
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id                  Specifies the mail id for the mail to resend
- * @param[in] timeout_in_sec   Specifies the timeout value in seconds
- * @exception          none
- * @see                 none
-
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_retry_send_mail()
- *     {
- *             int mail_id = 1;
- *             int timeout_in_sec = 2;
- *
- *              if(EMAIL_ERROR_NONE == email_retry_sending_mail(mail_id,timeout_in_sec))
- *                     //success
- *              else
- *                     //failure
- *
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_retry_send_mail()
+ *      {
+ *      int mail_id = 1;
+ *      int timeout_in_sec = 2;
+ *
+ *      if(EMAIL_ERROR_NONE == email_retry_sending_mail(mail_id,timeout_in_sec))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_retry_sending_mail(int mail_id, int timeout_in_sec);
 
 /**
- * @fn email_create_db_full()
- * @brief      Create db and fill with dummy data
+ * @brief Creates a DB and fill it with dummy data.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @exception          none
- * @see                                none
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_create_db_full();
 
 /**
- * @fn email_cancel_sending_mail( int mail_id)
- * @brief      Callback function for cm popup. We set the status as EMAIL_MAIL_STATUS_SEND_CANCELED
+ * @brief Callback function for cm popup. 
+ *        We set the status as EMAIL_MAIL_STATUS_SEND_CANCELED <<need to be updated>>.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id  The mail ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the mailID
- * @exception          none
- * @see                 none
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_cancel_send_mail()
- *     {
- *             int mail_id = 10;
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_cancel_send_mail()
+ *      {
+ *          int mail_id = 10;
  *
  *
- *              if(EMAIL_ERROR_NONE == email_cancel_sending_mail(mail_id,))
- *                     //success
- *              else
- *                     //failure
+ *       if(EMAIL_ERROR_NONE == email_cancel_sending_mail(mail_id,))
+ *          //success
+ *       else
+ *          //failure
  *
- *     }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_cancel_sending_mail(int mail_id) ;
 
 /**
- * @fn email_get_disk_space_usage(unsigned long *total_size)
- * @brief      Gets the total disk usage of emails in KB.
+ * @brief Gets the total disk usage of emails in KB.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[out] total_size  The total disk usage of emails
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an  error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[out] total_size      total disk usage of emails
- * @exception          none
  * @code
- *     #include "email-api-account.h"
- *             bool
- *             _api_sample_get_disk_space_usage()
- *             {
- *                     unsigned long total_size = 0;
- *
- *                     if ( EMAIL_ERROR_NONE  != email_get_disk_space_usage(&total_size))
- *                             printf("email_get_disk_space_usage failed err : %d\n",err_code);
- *                     else
- *                             printf("email_get_disk_space_usage SUCCESS, total disk usage in KB : %ld \n", total_size);
- *             }
+ *     #include "email-api-account.h"
+ *     bool
+ *     _api_sample_get_disk_space_usage()
+ *     {
+ *           unsigned long total_size = 0;
+ *
+ *           if ( EMAIL_ERROR_NONE  != email_get_disk_space_usage(&total_size))
+ *               printf("email_get_disk_space_usage failed err : %d\n",err_code);
+ *           else
+ *               printf("email_get_disk_space_usage SUCCESS, total disk usage in KB : %ld \n", total_size);
+ *     }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_disk_space_usage(unsigned long *total_size);
 
 /**
- * @fn         email_get_address_info_list(int mail_id, email_address_info_list_t** address_info_list)
- * @brief      Get the address info list. The address info list contains from, to, cc, bcc addresses and their display name, contact id and etc. (see email_address_info_list_t)
- *                     Each GList (from, to, cc, bcc) is the list of email_address_info_t data.
- *                     "data" variable of GList structure contains email_address_info_t data. <br>
- *                     To get email_address_info_t data from GList, Use type casting from GList node.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the mail id to get the list.
- * @param[out] address_info_list       Specifies the Pointer to the structure email_address_info_list_t. Memory for a new address info list will be allocated to this. Call email_free_address_info_list() to free the memory allocated to this.
- * @exception          none
- * @see                 email_address_info_list_t, email_address_info_t, email_free_address_info_list()
+ * @brief Gets the address info list.
+ * @details The address info list contains From, To, CC, BCC addresses and their display name, contact ID and etc (see #email_address_info_list_t).\n
+ *          Each GList (From, To, CC, BCC) is the list of #email_address_info_t data. \n
+ *          "data" variable of GList structure contains #email_address_info_t data. \n
+ *          To get #email_address_info_t data from GList, use type casting from GList node.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  mail_id            The mail ID to get the list
+ * @param[out] address_info_list  The pointer to the structure #email_address_info_list_t \n
+ *                                Memory for a new address info list will be allocated to this. 
+ *                                You must call email_free_address_info_list() to free the memory allocated to this.
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_address_info_list_t, #email_address_info_t, email_free_address_info_list()
+ *
  * @code
- *     #include "email-api-account.h"
- *     bool
- *     _api_sample_get_address_info_list()
- *     {
- *             email_address_info_list_t *p_address_info_list= NULL;
- *             email_address_info_t *p_address_info = NULL;
- *             int mai_id;
- *             int err_code = EMAIL_ERROR_NONE;
- *
- *             mail_id = 1;
- *
- *             if ( EMAIL_ERROR_NONE != (err_code = email_get_address_info_list(mail_id, &p_address_info_list)) )
- *             {
- *                     printf("email_get_address_info_list failed :\n"); *
- *                     return false;
- *             }
- *             else
- *             {
- *                     printf("Success\n");
- *                     //do something with p_address_info_list
- *                     GList *list = p_address_info_list->from;
- *                     GList *node = g_list_first(list);
- *                     while ( node != NULL )
- *                     {
- *                             p_address_info = (email_address_info_t*)node->data;
- *                             printf("%d,  %s, %s, %d\n", p_address_info->address_type, p_address_info->address, p_address_info->display_name, p_address_info->contact_id);
- *
- *                             node = g_list_next(node);
- *                     }
- *             }
- *
- *             // Free sender list
- *             if ( p_address_info_list )
- *             {
- *                     email_free_address_info_list(&p_address_info_list);
- *             }
- *     }
+ *      #include "email-api-account.h"
+ *      bool
+ *      _api_sample_get_address_info_list()
+ *      {
+ *      email_address_info_list_t *p_address_info_list= NULL;
+ *      email_address_info_t *p_address_info = NULL;
+ *      int mai_id;
+ *      int err_code = EMAIL_ERROR_NONE;
+ *
+ *      mail_id = 1;
+ *
+ *      if ( EMAIL_ERROR_NONE != (err_code = email_get_address_info_list(mail_id, &p_address_info_list)) )
+ *      {
+ *          printf("email_get_address_info_list failed :\n"); *
+ *          return false;
+ *      }
+ *      else
+ *      {
+ *          printf("Success\n");
+ *          //do something with p_address_info_list
+ *          GList *list = p_address_info_list->from;
+ *          GList *node = g_list_first(list);
+ *          while ( node != NULL )
+ *          {
+ *              p_address_info = (email_address_info_t*)node->data;
+ *              printf("%d,  %s, %s, %d\n", p_address_info->address_type, p_address_info->address, p_address_info->display_name, p_address_info->contact_id);
+ *
+ *              node = g_list_next(node);
+ *          }
+ *      }
+ *
+ *      // Free sender list
+ *      if ( p_address_info_list )
+ *      {
+ *          email_free_address_info_list(&p_address_info_list);
+ *      }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_address_info_list(int mail_id, email_address_info_list_t** address_info_list);
 
 /**
- * @fn email_free_address_info_list(email_address_info_list_t **address_info_list)
- * @brief      Free the address info list allocated by email_get_address_info_list(). This function will free the memory which is allocated to address_info_list itself.
+ * @brief Frees the address info list allocated by email_get_address_info_list().
+ * @details This function will free the memory which is allocated to address_info_list itself.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in]  address_info_list  The pointer to the structure #email_address_info_list_t to be freed
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] address_info_list        Specifies the Pointer to the structure email_address_info_list_t to be freed.
- * @exception          none
- * @see                 email_address_info_list_t, email_get_address_info_list()
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_address_info_list_t, email_get_address_info_list()
  */
 EXPORT_API int email_free_address_info_list(email_address_info_list_t **address_info_list);
 
 /**
- * @fn email_get_structure(const char*encoded_string, void **struct_var, email_convert_struct_type_e type)
- * @brief      This function returns the structure of the type which is indicated by 'type' variable. This function will allocate new memory to 'struct_var' for structure.<br>
- *                     Some notifications such as NOTI_DOWNLOAD_NEW_MAIL are published with string parameter. The string contains various values that might be divided by delimiter.<br>
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] encoded_string           Specifies the Pointer to the string from notification.
- * @param[out] struct_var                      Specifies the Pointer to the structure to be returned. If success, new memory for structure will be allocated.
- * @param[in] type                             Specifies the converting type. see email_convert_struct_type_e
- * @exception          none
- * @see                 email_convert_struct_type_e
- */
-EXPORT_API int email_get_structure(const char*encoded_string, void **struct_var, email_convert_struct_type_e type);
-
-/**
-
- * @fn email_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req)
- * @brief      Query the information of meeting request.
- *                     The information of meeting request is corresponding to only one mail.
- *                     For this reason, the information of meeting request can be added by using email_add_message_with_meeting_request() with a matched mail information.
- *
- * @return This function returns EMAIL_ERROR_NONE on success. This function returns EMAIL_ERROR_DATA_NOT_FOUND if there isn't a matched mail. Otherwise it returns error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_conditional_clause_string  Specifies the where clause string.
- * @param[out] output_meeting_req      Specifies the Pointer to the structure email_meeting_request_t.
- * @param[out] output_count    Specifies the number of meeting request returned.
- * @exception  none
- * @see        email_meeting_request_t
+ * @brief Queries the information of a meeting request.
+ * @details The information of a meeting request is corresponding to only one mail. \n
+ *          For this reason, the information of a meeting request can be added by using email_add_message_with_meeting_request() with the matched mail information.
+ *
+ * @param[in]  input_conditional_clause_string  The where clause string
+ * @param[out] output_meeting_req               The pointer to the structure #email_meeting_request_t
+ * @param[out] output_count                     The number of meeting request returned
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          #EMAIL_ERROR_DATA_NOT_FOUND if there is no matched mail, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_meeting_request_t
  */
 EXPORT_API int email_query_meeting_request(char *input_conditional_clause_string, email_meeting_request_t **output_meeting_req, int *output_count);
 
 /**
- * @fn email_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req)
- * @brief      Get the information of meeting request.  The information of meeting request is based on Mail Id. <br>
- *                     The information of meeting request is corresponding to only one mail.
- *                     For this reason, the information of meeting request can be added by using email_add_message_with_meeting_request() with a matched mail information.
- *
- * @return This function returns EMAIL_ERROR_NONE on success. This function returns EMAIL_ERROR_DATA_NOT_FOUND if there isn't a matched mail. Otherwise it returns error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the Mail id for which  meeting request details need to be fetched
- * @param[in/out] meeting_req  Specifies the Pointer to the structure email_meeting_request_t.
- * @exception          none
- * @see                 email_meeting_request_t
+ * @brief Gets the information of a meeting request.
+ * @details The information of a meeting request is based on the Mail ID. \n
+ *          The information of the meeting request is corresponding to only one mail. \n
+ *          For this reason, the meeting request information can be added by using email_add_message_with_meeting_request() with the matched mail information.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]     mail_id      The mail ID for which  meeting request details need to be fetched
+ * @param[in/out] meeting_req  The pointer to the structure #email_meeting_request_t
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          #EMAIL_ERROR_DATA_NOT_FOUND if there is no matched mail, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_meeting_request_t
  */
 EXPORT_API int email_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req);
 
 /**
- * @fn email_free_meeting_request(email_meeting_request_t** meeting_req, int count)
- * @brief      Free the meeting request allocated by email_get_meeting_request() or alloacted to add. This function will free the memory which is allocated to meeting_req (= *meeting_req) itself.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] email_meeting_request_t  Specifies the Pointer to the structure email_meeting_request_t to be freed.
- * @param[in] count    Specifies the number of elements in meeting_req array. this is usually 1.
- * @exception          none
- * @see                 email_meeting_request_t, email_get_meeting_request()
+ * @brief Frees a meeting request allocated by email_get_meeting_request() or allocated to add.
+ * @details This function will free the memory which is allocated to meeting_req (= *meeting_req) itself.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in] email_meeting_request_t  The pointer to the structure #email_meeting_request_t to be freed
+ * @param[in] count                    The number of elements in #meeting_req array (usually @c 1)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_meeting_request_t, email_get_meeting_request()
  */
 EXPORT_API int email_free_meeting_request(email_meeting_request_t** meeting_req, int count);
 
 /**
- * @fn email_move_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag)
- * @brief      Move a thread of mails to target mailbox.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] thread_id        Specifies the thread id to move
- * @param[in] target_mailbox_id        Specifies the mailbox id which move in
- * @param[in] move_always_flag Specify move always flag
- * @exception none
- * @see
+ * @brief Moves a thread of mails to the target mailbox.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] thread_id          The thread ID to move
+ * @param[in] target_mailbox_id  The mailbox ID in which to move
+ * @param[in] move_always_flag   The move always flag
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_move_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag);
 
 /**
- * @fn email_delete_thread(int thread_id, int delete_always_flag)
- * @brief      delete a thread of mails.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] thread_id        Specifies the thread id to delete
- * @param[in] delete_always_flag Specify delete always flag
- * @exception none
- * @see
+ * @brief Deletes a thread of mails.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] thread_id           The thread ID to delete
+ * @param[in] delete_always_flag  The delete always flag
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_delete_thread(int thread_id, int delete_always_flag);
 
 /**
- * @fn email_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server)
- * @brief      Modify seen flags of thread
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] thread_id        Specifies the thread id to modify
- * @param[in] seen_flag Specify seem flag
- * @param[in] on_server Specify to sync with server
- * @exception none
- * @see
+ * @brief Modifies seen flags of the thread.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] thread_id  The thread ID to modify
+ * @param[in] seen_flag  The seen flag
+ * @param[in] on_server  The flag to sync with server
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server);
 
 /**
- * @fn email_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle)
- * @brief      Delete mails which flagged to delete
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id Specifies the id of mailbox
- * @param[in] input_on_server Specify to sync with server
- * @param[out] output_handle Specify the handle of task
- * @exception none
- * @see
+ * @brief Deletes mails flagged to "delete".
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_mailbox_id  The mailbox ID
+ * @param[in]  input_on_server   The flag to sync with server
+ * @param[out] output_handle     The handle of task
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle);
 
@@ -1499,8 +1647,8 @@ EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int inp
 #endif /* __cplusplus */
 
 /**
-* @} @}
-*/
+ * @}
+ */
 
 
 #endif /* __EMAIL_API_MAIL_H__ */
index 0a7b7bb..60ceeea 100755 (executable)
 #include "email-types.h"
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_MAILBOX Email Mailbox API
-* @{
-*/
-
-/**
- *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api-mailbox.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief              This file contains the data structures and interfaces of mailbox provided by
- *                     email-service .
+ * @file email-api-mailbox.h
+ * @brief This file contains the data structures and interfaces of mailbox provided by email-service.
  * @{
  * @code
  *
  *  #include "email_api_mailbox.h"
  *
  *  bool other_app_invoke_uniform_api_sample(int *error_code)
- *     {
- *             email_mailbox_t mailbox;
- *             email_mailbox_t *new_mailbox =NULL;
- *             email_mailbox_t *mailbox_list = NULL;
- *             int count = 0;
- *             int mailbox_type;
- *             int handle = 0;
- *             char *pMaiboxName;
- *             char *pParentMailbox;
- *
- *             memset(&mailbox,0x00,sizeof(email_mailbox_t));
- *             mailbox.mailbox_name = strdup("test");
- *             mailbox.alias = strdup("Personal");
- *             mailbox.account_id = 1;
- *             printf("Enter local_yn(1/0)");
- *             scanf("%d",&local_yn);
- *             mailbox.local=local_yn;
- *             mailbox.mailbox_type = 7;
- *
- *             //create new mailbox
- *
- *             if(EMAIL_ERR_NONE != email_add_mailbox(&mailbox,local_yn,&handle))
- *                     printf("email_add_mailbox failed\n");
- *             else
- *                     printf("email_add_mailbox success");
- *
- *             //delete mailbox
- *
- *             if(EMAIL_ERROR_NONE != email_delete_mailbox(mailbox,local_yn,&handle))
- *                     printf("email_delete_mailbox failed\n");
- *             else
- *                     printf("email_delete_mailbox success\n");
- *
- *             //free mailbox
- *             email_free_mailbox("new_mailbox,1");
- *
- *             //Get mailbox list
- *             if(EMAIL_ERROR_NONE != email_get_mailbox_list(account_id,local_yn,&mailbox_list,&count))
- *                     //failure
- *             else
- *                     //success
- *
- *             //Get mailbox by name
- *             pMailboxName = strdup("test");
- *             if(EMAIL_ERROR_NONE != email_get_mailbox_by_name(account_id,pMailboxName,&mailbox_list))
- *                     //failure
- *             else
- *                     //success
- *
- *             //Get child mailbox list
- *             pParentMailbox = strdup("test");
- *             if(EMAIL_ERROR_NONE != email_get_child_mailbox_list(account_id, paerent_mailbox,&mailbox_list,&count))
- *                     //failure
- *             else
- *                     //success
- *
- *             //Get mailbox by mailbox_type
- *             printf("Enter mailbox_type\n");
- *             scanf("%d",&mailbox_type);
- *             if(EMAIL_ERROR_NONE != email_get_mailbox_by_mailbox_type(account_id,mailbox_type,&mailbox_list))
- *                     //failure
- *             else
- *                     //success
- *
- *     }
+ *  {
+ *      email_mailbox_t mailbox;
+ *      email_mailbox_t *new_mailbox =NULL;
+ *      email_mailbox_t *mailbox_list = NULL;
+ *      int count = 0;
+ *      int mailbox_type;
+ *      int handle = 0;
+ *      char *pMaiboxName;
+ *      char *pParentMailbox;
+ *
+ *      memset(&mailbox,0x00,sizeof(email_mailbox_t));
+ *      mailbox.mailbox_name = strdup("test");
+ *      mailbox.alias = strdup("Personal");
+ *      mailbox.account_id = 1;
+ *      printf("Enter local_yn(1/0)");
+ *      scanf("%d",&local_yn);
+ *      mailbox.local=local_yn;
+ *      mailbox.mailbox_type = 7;
+ *
+ *      //create new mailbox
+ *
+ *      if(EMAIL_ERR_NONE != email_add_mailbox(&mailbox,local_yn,&handle))
+ *          printf("email_add_mailbox failed\n");
+ *      else
+ *          printf("email_add_mailbox success");
+ *
+ *      //delete mailbox
+ *
+ *      if(EMAIL_ERROR_NONE != email_delete_mailbox(mailbox,local_yn,&handle))
+ *          printf("email_delete_mailbox failed\n");
+ *      else
+ *          printf("email_delete_mailbox success\n");
+ *
+ *      //free mailbox
+ *      email_free_mailbox("new_mailbox,1");
+ *
+ *      //Get mailbox list
+ *      if(EMAIL_ERROR_NONE != email_get_mailbox_list(account_id,local_yn,&mailbox_list,&count))
+ *          //failure
+ *      else
+ *          //success
+ *
+ *      //Get mailbox by name
+ *      pMailboxName = strdup("test");
+ *      if(EMAIL_ERROR_NONE != email_get_mailbox_by_name(account_id,pMailboxName,&mailbox_list))
+ *          //failure
+ *      else
+ *          //success
+ *
+ *      //Get child mailbox list
+ *      pParentMailbox = strdup("test");
+ *      if(EMAIL_ERROR_NONE != email_get_child_mailbox_list(account_id, paerent_mailbox,&mailbox_list,&count))
+ *          //failure
+ *      else
+ *          //success
+ *
+ *      //Get mailbox by mailbox_type
+ *      printf("Enter mailbox_type\n");
+ *      scanf("%d",&mailbox_type);
+ *      if(EMAIL_ERROR_NONE != email_get_mailbox_by_mailbox_type(account_id,mailbox_type,&mailbox_list))
+ *          //failure
+ *      else
+ *          //success
+ *
+ *  }
  *
  * @endcode
  * @}
  */
 
+/**
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_MAILBOX_MODULE Mailbox API
+ * @brief Mailbox API is a set of operations to manage email mailboxes like add, update, delete or get mailbox related details.
+ *
+ * @section EMAIL_SERVICE_MAILBOX_MODULE_HEADER Required Header
+ *   \#include <email-api-mailbox.h>
+ *
+ * @section EMAIL_SERVICE_MAILBOX_MODULE_OVERVIEW Overview
+ * Mailbox API is a set of operations to manage email mailboxes like add, update, delete or get mailbox related details.
+ */
 
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_MAILBOX_MODULE
+ * @{
+ */
 
 #ifdef __cplusplus
 extern "C"
@@ -132,269 +130,347 @@ extern "C"
 
 
 /**
- * @fn int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle)
- * @brief      Create a new mailbox or mailbox.This function is invoked when user wants to create a new mailbox for the specified account.
- *             If On_server is true then it will create the mailbox on server as well as in local also.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] new_mailbox      Specifies the pointer of creating mailbox information.
-*  @param[in] on_server                Specifies the creating mailbox on server.
- * @param[out] handle          Specifies the sending handle.
- * @exception  none
- * @see        email_mailbox_t
-  * @remarks N/A
+ * @brief Creates a new mailbox.
+ * @details This function is invoked when a user wants to create a new mailbox for the specified account.\n
+ *          If on_server is true, it will create the mailbox on the server as well as locally.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  new_mailbox  The pointer of creating mailbox information
+ * @param[in]  on_server    The creating mailbox on server
+ * @param[out] handle       The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_add_mailbox(email_mailbox_t *new_mailbox, int on_server, int *handle);
 
 /**
- * @fn int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias)
- * @brief      Change mailbox name. This function is invoked when user wants to change the name of existing mail box.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id Specifies the id of the mailbox.
- * @param[in] input_mailbox_name       Specifies the name of the mailbox.
- * @param[in] input_mailbox_alias      Specifies the alias of the mailbox.
- * @param[in] input_on_server  Specifies the moving mailbox on server.
- * @param[out] output_handle   Specifies the handle to manage tasks.
- *
- * @exception see email-errors.h
- * @see        email_mailbox_t, email_mailbox_type_e
- * @remarks N/A
+ * @brief Changes the name of a mailbox.
+ * @details This function is invoked when a user wants to change the name of an existing mail box.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in]  input_mailbox_id     The mailbox ID
+ * @param[in]  input_mailbox_name   The name of the mailbox
+ * @param[in]  input_mailbox_alias  The alias of the mailbox
+ * @param[in]  input_on_server      The moving mailbox on server
+ * @param[out] output_handle        The handle to manage tasks
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t, #email_mailbox_type_e
  */
 EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias, int input_on_server, int *output_handle);
 
 /**
- * @fn int email_rename_mailbox_ex(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle);
- * @brief      Change mailbox name. This function is invoked when user wants to change the name of existing mail box.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id Specifies the id of the mailbox.
- * @param[in] input_mailbox_name       Specifies the name of the mailbox.
- * @param[in] input_mailbox_alias      Specifies the alias of the mailbox.
- * @param[in] input_eas_data           Specifies the eas data.
- * @param[in] input_eas_data_length    Specifies the length of eas data.
- * @param[in] input_on_server  Specifies the moving mailbox on server.
- * @param[out] output_handle   Specifies the handle to manage tasks.
- *
- * @exception see email-errors.h
- * @see        email_mailbox_t, email_mailbox_type_e
- * @remarks N/A
+ * @brief Changes the name of a mailbox.
+ * @details This function is invoked when a user wants to change the name of an existing mail box.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in]  input_mailbox_id       The mailbox ID
+ * @param[in]  input_mailbox_name     The name of the mailbox
+ * @param[in]  input_mailbox_alias    The alias of the mailbox
+ * @param[in]  input_eas_data         The EAS data
+ * @param[in]  input_eas_data_length  The length of EAS data
+ * @param[in]  input_on_server        The moving mailbox on server
+ * @param[out] output_handle          The handle to manage tasks
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t, #email_mailbox_type_e
  */
 EXPORT_API int email_rename_mailbox_ex(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle);
 
 /**
- * @fn int email_delete_mailbox(int input_mailbox_id, int input_on_server, int *output_handle)
- * @brief      Delete a mailbox or mailbox.This function deletes the existing mailbox for specified account based on the option on_server.
- *             If the on_server is true then it deletes mailbox from server as well as locally.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id Specifies the id of target mailbox .
- * @param[in] input_on_server  Specifies the deleting mailbox on server.
- * @param[out] output_handle   Specifies the sending handle.
- * @exception  see email-errors.h
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Deletes a mailbox.
+ * @details This function deletes the existing mailbox for the specified account based on the on_server option. \n
+ *          If on_server is true, it deletes the mailbox from the server as well as locally.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in]  input_mailbox_id  The target mailbox ID
+ * @param[in]  input_on_server   The deleting mailbox on server
+ * @param[out] output_handle     The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, int *output_handle);
 
 /**
- * @fn 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);
- * @brief      Delete a mailbox or mailbox.This function deletes the existing mailbox for specified account based on the option on_server.
- *             If the on_server is true then it deletes mailbox from server as well as locally.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_account_id         Specifies the id of account.
- * @param[in] input_mailbox_id_array   Specifies the mailbox array for deleting.
- * @param[in] input_mailbox_id_count   Specifies the count of mailbox for deleting.
- * @param[in] input_on_server  Specifies the deleting mailbox on server.
- * @param[out] output_handle   Specifies the sending handle.
- * @exception  see email-errors.h
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Deletes a mailbox.
+ * @details This function deletes the existing mailbox for the specified account based on the option on_server option. \n
+ *          If on_server is true, it deletes the mailbox from the server as well as locally.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in]  input_account_id        The account ID
+ * @param[in]  input_mailbox_id_array  The mailbox array for deleting
+ * @param[in]  input_mailbox_id_count  The count of mailbox for deleting
+ * @param[in]  input_on_server         The deleting mailbox on server
+ * @param[out] output_handle           The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 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);
 
 /**
- * @fn int email_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
- * @brief      Change the mailbox type. This function is invoked when user wants to change the mailbox type.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id         Specifies the id of the mailbox.
- * @param[in] input_mailbox_type       Specifies the mailbox type.
- * @exception see email-errors.h
- * @see        email_mailbox_type_e
- * @remarks N/A
+ * @brief Changes the mailbox type.
+ * @details This function is invoked when a user wants to change the mailbox type.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in] input_mailbox_id    The mailbox ID
+ * @param[in] input_mailbox_type  The mailbox type
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code(see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_type_e
  */
 EXPORT_API int email_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type);
 
 
 /**
- * @fn int email_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox)
- * @brief      Change the attribute 'local' of email_mailbox_t. This function is invoked when user wants to change the attribute 'local'.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id                 Specifies the id of the mailbox.
- * @param[in] input_is_local_mailbox   Specifies the value of the attribute 'local' of email_mailbox_t.
- * @exception see email-errors.h
- * @see                none
- * @remarks N/A
+ * @brief Changes the 'local' attribute of #email_mailbox_t.
+ * @details This function is invoked when a user wants to change the 'local' attribute.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @remarks See email-errors.h for exception.
+ *
+ * @param[in] input_mailbox_id        The mailbox ID
+ * @param[in] input_is_local_mailbox  The value of the attribute 'local' of #email_mailbox_t
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox);
 
 /**
- * @fn email_get_mailbox_list(int account_id, int mailbox_sync_type, email_mailbox_t** mailbox_list, int* count)
- * @brief      Get all mailboxes from account.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account ID.
- * @param[in] mailbox_sync_type                Specifies the sync type.
- * @param[out] mailbox_list    Specifies the pointer of mailbox structure pointer.(possibly NULL)
- * @param[out] count                   The mailbox count is saved here.(possibly 0)
- * @exception          none
- * @see        email_mailbox_t
-
+ * @brief Gets all mailboxes from account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id         The account ID
+ * @param[in]  mailbox_sync_type  The sync type
+ * @param[out] mailbox_list       The pointer of mailbox structure pointer (possibly @c NULL)
+ * @param[out] count              The mailbox count (possibly @c 0)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
+ *
  * @code
- *     #include "email-api-mailbox.h"
- *     bool
- *     _api_sample_get_mailbox_list()
- *     {
- *             int account_id =0,count = 0;
- *             int mailbox_sync_type;
- *             int error_code = EMAIL_ERROR_NONE;
- *             email_mailbox_t *mailbox_list=NULL;
- *
- *             printf("\n > Enter account id: ");
- *             scanf("%d", &account_id);
- *             printf("\n > Enter mailbox_sync_type: ");
- *             scanf("%d", &mailbox_sync_type);
- *
- *             if((EMAIL_ERROR_NONE != email_get_mailbox_list(account_id, mailbox_sync_type, &mailbox_list, &count)))
- *             {
- *                     printf(" Error\n");
- *             }
- *             else
- *             {
- *                     printf("Success\n");
- *                     email_free_mailbox(&mailbox_list,count);
- *             }
- *     }
+ *      #include "email-api-mailbox.h"
+ *      bool
+ *      _api_sample_get_mailbox_list()
+ *      {
+ *          int account_id =0,count = 0;
+ *          int mailbox_sync_type;
+ *          int error_code = EMAIL_ERROR_NONE;
+ *          email_mailbox_t *mailbox_list=NULL;
+ *
+ *          printf("\n > Enter account id: ");
+ *          scanf("%d", &account_id);
+ *          printf("\n > Enter mailbox_sync_type: ");
+ *          scanf("%d", &mailbox_sync_type);
+ *
+ *          if((EMAIL_ERROR_NONE != email_get_mailbox_list(account_id, mailbox_sync_type, &mailbox_list, &count)))
+ *          {
+ *              printf(" Error\n");
+ *          }
+ *          else
+ *          {
+ *              printf("Success\n");
+ *              email_free_mailbox(&mailbox_list,count);
+ *          }
+ *      }
  * @endcode
- * @remarks N/A
  */
 EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, email_mailbox_t** mailbox_list, int* count);
 
 /**
- * @fn email_get_mailbox_list_ex(int account_id, int mailbox_sync_type, int with_count, email_mailbox_t** mailbox_list, int* count)
- * @brief      Extend the email_get_mailbox_list_ex function.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account ID.
- * @param[in] mailbox_sync_type                Specifies the sync type.
- * @param[in] with_count                specifies the count of mailbox.
- * @param[out] mailbox_list    Specifies the pointer of mailbox structure pointer.(possibly NULL)
- * @param[out] count                   The mailbox count is saved here.(possibly 0)
- * @exception          none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Extends the email_get_mailbox_list_ex() function.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id         The account ID
+ * @param[in]  mailbox_sync_type  The sync type
+ * @param[in]  with_count         The count of mailbox
+ * @param[out] mailbox_list       The pointer of mailbox structure pointer (possibly @c NULL)
+ * @param[out] count              The mailbox count is saved here (possibly @c 0)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type, int with_count, email_mailbox_t** mailbox_list, int* count);
 
 /**
- * @fn email_get_mailbox_list_by_keyworkd(int account_id, char *keyword , email_mailbox_t** mailbox_list, int* count)
- * @brief      Get the mailbox list to using keywork.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account ID.
- * @param[in] keywork          Specifies the specified keyworkd for searching.
- * @param[out] mailbox_list    Specifies the pointer of mailbox structure pointer.(possibly NULL)
- * @param[out] count                   The mailbox count is saved here.(possibly 0)
- * @exception          none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Gets the mailbox list based on a keyword.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id    The account ID
+ * @param[in]  keyword       The specified keyword for searching
+ * @param[out] mailbox_list  The pointer of mailbox structure pointer (possibly @c NULL)
+ * @param[out] count         The mailbox count is saved here (possibly @c 0)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_get_mailbox_list_by_keyword(int account_id, char *keyword, email_mailbox_t** mailbox_list, int* count);
 
 /**
- * @fn email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type,  email_mailbox_t** mailbox)
- * @brief      Get mailbox by mailbox_type.This function is invoked when user wants to know the mailbox information by mailbox_type for the given account.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id               Specifies the account ID.
- * @param[in] mailbox_type             Specifies the mailbox type.
- * @param[out] mailbox         Specifies the pointer of mailbox structure pointer.(possibly NULL)
- * @exception none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Gets a mailbox by mailbox type.
+ * @details This function is invoked when a user wants to know the mailbox information by @a mailbox_type for the given account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id    The account ID
+ * @param[in]  mailbox_type  The mailbox type
+ * @param[out] mailbox       The pointer of mailbox structure pointer (possibly @c NULL)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type,  email_mailbox_t** mailbox);
 
 /**
- * @fn email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailbox_t** output_mailbox)
- * @brief      Get mailbox by mailbox_id. This function is invoked when user wants to know the mailbox information by mailbox id.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in]  input_mailbox_id        Specifies the mailbox id.
- * @param[out] output_mailbox  Specifies the pointer of mailbox structure pointer.(possibly NULL)
- * @exception none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Gets a mailbox by mailbox ID.
+ * @details This function is invoked when a user wants to know the mailbox information by mailbox ID.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_mailbox_id  The mailbox ID
+ * @param[out] output_mailbox    The pointer of mailbox structure pointer (possibly @c NULL)
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailbox_t** output_mailbox);
 
 /**
- * @fn email_set_mail_slot_size(int input_account_id, int input_mailbox_id, int input_new_slot_size)
- * @brief      Set mail slot size.This function is invoked when user wants to set the size of mail slot.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_account_id         Specifies the account ID.
- * @param[in] input_mailbox_id         Specifies the mailbox id.
- * @param[in] input_new_slot_size      Specifies the mail slot size.
- * @exception none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Sets a mail slot size.
+ * @details This function is invoked when a user wants to set the size of mail slot.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] input_account_id     The account ID
+ * @param[in] input_mailbox_id     The mailbox ID
+ * @param[in] input_new_slot_size  The mail slot size
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_set_mail_slot_size(int input_account_id, int input_mailbox_id, int input_new_slot_size);
 
 /**
- * @fn email_stamp_sync_time_of_mailbox(int input_mailbox_id)
- * @brief      Stamp sync time of mailbox. This function is invoked when user wants to set the sync time of the mailbox.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code(refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mailbox_id         Specifies the mailbox id.
- * @exception none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Sets the sync time of a mailbox.
+ * @details This function is invoked when a user wants to set the sync time of the mailbox.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] input_mailbox_id  The mailbox ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_stamp_sync_time_of_mailbox(int input_mailbox_id);
 
 
 /**
- * @fn email_free_mailbox(email_mailbox_t** mailbox_list, int count)
- * @brief      Free allocated memory for mailbox information.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mailbox_list     Specifies the pointer for searching mailbox structure pointer.
- * @param[in] count                    Specifies the count of mailboxes.
- * @exception          none
- * @see                 email_mailbox_t
-
+ * @brief Frees the memory allocated for the mailbox information.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in] mailbox_list  The pointer for searching mailbox structure pointer
+ * @param[in] count         The count of mailboxes
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
+ *
  * @code
- *     #include "email-api-mailbox.h"
- *     bool
- *     _api_sample_free_mailbox_info()
- *     {
- *             email_mailbox_t *mailbox;
- *
- *             //fill the mailbox structure
- *             //count - number of mailbox structure user want to free
- *              if(EMAIL_ERROR_NONE == email_free_mailbox(&mailbox,count))
- *                     //success
- *              else
- *                     //failure
- *
- *     }
+ *      #include "email-api-mailbox.h"
+ *      bool
+ *      _api_sample_free_mailbox_info()
+ *      {
+ *      email_mailbox_t *mailbox;
+ *
+ *      //fill the mailbox structure
+ *      //count - number of mailbox structure user want to free
+ *      if(EMAIL_ERROR_NONE == email_free_mailbox(&mailbox,count))
+ *           //success
+ *      else
+ *           //failure
+ *
+ *      }
  * @endcode
- * @remarks N/A
  */
 
 EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count);
@@ -404,8 +480,8 @@ EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count);
 #endif /* __cplusplus */
 
 /**
-* @} @}
-*/
+ * @}
+ */
 
 #endif /* __EMAIL_API_MAILBOX_H__ */
 
index 77f3e16..cbce793 100755 (executable)
 #include "email-types.h"
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_NETWORK Email Network API
-* @{
-*/
-
-/**
- *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api-network.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief              This file contains the data structures and interfaces of Network related Functionality provided by
- *                     email-service .
+ * @internal
+ * @file email-api-network.h
+ * @brief This file contains the data structures and interfaces of Network related Functionality provided by
+ *        email-service.
  *
  * @{
-
  * @code
 
- *     #include "email-api.h"
- *
- *     bool
- *     other_app_invoke_uniform_api_sample(int* error_code)
- *     {
- *
- *             // Send a mail
- *             email_attachment_data_t attachment;
- *             int account_id = 1;
- *             int mailbox_id = 0;
- *             int attachment_id = 0;
- *             int err = EMAIL_ERROR_NONE;
- *             int mail_id = 0;
- *             int action = -1;
- *
- *             printf("Enter mail id\n");
- *             scanf("%d",&mail_id);
- *
- *             if(EMAIL_ERROR_NONE == email_send_mail(mail_id, &handle))
- *                     //success
- *             else
- *                     //failure
- *
- *             // Download header of new emails from mail server
- *             int handle = 0;
- *
- *             mailbox.account_id = account_id;
- *             printf("Enter mailbox id\n");
- *             scanf("%d",&mailbox_id);
- *             if(EMAIL_ERROR_NONE == email_sync_header (account_id, mailbox_id, &handle))
- *                     //success
- *             else
- *                     //failure
- *
- *             //Sync mail header for all accounts
- *             if(EMAIL_ERROR_NONE == email_sync_header_for_all_account(&handle))
- *                     //success
- *             else
- *                     //failure
- *
- *             //Download email body from server
- *
- *             if(EMAIL_ERROR_NONE == email_download_body (mail_id,0,&handle))
- *                     //success
- *             else
- *                     //failure
- *
- *             //Download a email nth-attachment from server
- *             prinf("Enter attachment number\n");
- *             scanf("%d",&attachment_id);
- *             if(EMAIL_ERROR_NONE == email_download_attachment(mail_id, attachment_id, &handle))
- *                     //success
- *             else
- *                     //failure
- *
- *             //Cancel job
- *             if(EMAIL_ERROR_NONE == email_cancel_job(account_id,handle))//canceling download email nth attachment from server job.
- *                                                                     //so this handle contains the value return by the email_download_attachment()
- *                     //success
- *             else
- *                     //failure
- *             //Get pending job list for an account
- *
- *             printf( " Enter Action \n SEND_MAIL = 0 \n SYNC_HEADER = 1 \n" \
- *                         " DOWNLOAD_BODY,= 2 \n DOWNLOAD_ATTACHMENT = 3 \n" \
- *                         " DELETE_MAIL = 4 \n SEARCH_MAIL = 5 \n SAVE_MAIL = 6 \n" \
- *                         " NUM = 7 \n");
- *             scanf("%d",&action);
- *             if(EMAIL_ERROR_NONE == email_get_pending_job(action,account_id,mail_id,&status))
- *                     //success
- *             else
- *                     //error
- *
- *             //Get Network status
- *             if(EMAIL_ERROR_NONE == email_get_network_status(&sending,&receiving))
- *                     //success
- *             else
- *                     //failure
- *
- *             //Send read report
- *             if(EMAIL_ERROR_NONE == email_send_report(mail ,&handle))
- *                     //success
- *             else
- *                     //failure
- *             //Save and send
- *
- *
- *             if(EMAIL_ERROR_NONE  == email_add_mail(mail,NULL,0, NULL, 0))
- *             {
- *                     if(EMAIL_ERROR_NONE == email_send_saved(account_id,&option,&handle))
- *                             //success
- *                     else
- *                             //failure
- *             }
- *             //Get Imap mailbox list
- *             if(EMAIL_ERROR_NONE == email_sync_imap_mailbox_list(account_id,  &handle))
- *                     //success
- *             else
- *                     //failure
+ *  #include "email-api.h"
+ *
+ *  bool
+ *  other_app_invoke_uniform_api_sample(int* error_code)
+ *  {
+ *
+ *      // Sends a mail
+ *      email_attachment_data_t attachment;
+ *      int account_id = 1;
+ *      int mailbox_id = 0;
+ *      int attachment_id = 0;
+ *      int err = EMAIL_ERROR_NONE;
+ *      int mail_id = 0;
+ *      int action = -1;
+ *
+ *      printf("Enter mail id\n");
+ *      scanf("%d",&mail_id);
+ *
+ *      if(EMAIL_ERROR_NONE == email_send_mail(mail_id, &handle))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      // Downloads header of new emails from mail server
+ *      int handle = 0;
+ *
+ *      mailbox.account_id = account_id;
+ *      printf("Enter mailbox id\n");
+ *      scanf("%d",&mailbox_id);
+ *      if(EMAIL_ERROR_NONE == email_sync_header (account_id, mailbox_id, &handle))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      //Syncs mail header for all accounts
+ *      if(EMAIL_ERROR_NONE == email_sync_header_for_all_account(&handle))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      //Downloads email body from server
+ *
+ *      if(EMAIL_ERROR_NONE == email_download_body (mail_id,0,&handle))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      //Downloads an email nth-attachment from server
+ *      prinf("Enter attachment number\n");
+ *      scanf("%d",&attachment_id);
+ *      if(EMAIL_ERROR_NONE == email_download_attachment(mail_id, attachment_id, &handle))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      //Cancel job
+ *      if(EMAIL_ERROR_NONE == email_cancel_job(account_id,handle))//canceling download email nth attachment from server job.
+ *                                  //so this handle contains the value return by the email_download_attachment()
+ *          //success
+ *      else
+ *          //failure
+ *      //Gets pending job list for an account
+ *
+ *      printf( " Enter Action \n SEND_MAIL = 0 \n SYNC_HEADER = 1 \n" \
+ *              " DOWNLOAD_BODY,= 2 \n DOWNLOAD_ATTACHMENT = 3 \n" \
+ *              " DELETE_MAIL = 4 \n SEARCH_MAIL = 5 \n SAVE_MAIL = 6 \n" \
+ *              " NUM = 7 \n");
+ *          scanf("%d",&action);
+ *      if(EMAIL_ERROR_NONE == email_get_pending_job(action,account_id,mail_id,&status))
+ *          //success
+ *      else
+ *          //error
+ *
+ *      //Gets Network status
+ *      if(EMAIL_ERROR_NONE == email_get_network_status(&sending,&receiving))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      //Sends read report
+ *      if(EMAIL_ERROR_NONE == email_send_report(mail ,&handle))
+ *          //success
+ *      else
+ *          //failure
+ *      //Saves and sends
+ *
+ *
+ *      if(EMAIL_ERROR_NONE  == email_add_mail(mail,NULL,0, NULL, 0))
+ *      {
+ *          if(EMAIL_ERROR_NONE == email_send_saved(account_id,&option,&handle))
+ *              //success
+ *          else
+ *              //failure
+ *      }
+ *      //Get Imap mailbox list
+ *      if(EMAIL_ERROR_NONE == email_sync_imap_mailbox_list(account_id,  &handle))
+ *          //success
+ *      else
+ *          //failure
  *
  * }
  *
  * @endcode
  * @}
-
  */
 
+/**
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_NETWORK_MODULE Network API
+ * @brief Network API is a set of operations to manage email send, receive and cancel related details.
+ *
+ * @section EMAIL_SERVICE_NETWORK_MODULE_HEADER Required Header
+ *   \#include <email-api-network.h>
+ *
+ * @section EMAIL_SERVICE_NETWORK_MODULE_OVERVIEW Overview
+ * Network API is a set of operations to manage email send, receive and cancel related details.
+ */
 
+/**
+ * @internal
+* @addtogroup EMAIL_SERVICE_NETWORK_MODULE
+* @{
+*/
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
 /**
- * @fn email_send_mail(int mail_id,    int *handle)
- * @brief      Send a mail.This function is invoked when user wants to send a composed mail.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id                  Specifies the mail ID.
- * @param[out] handle          Specifies the sending handle.
- * @exception  none
- * @see        email_mailbox_t and email_option_t
- * @remarks N/A
+ * @brief Sends a mail.
+ * @details This function is invoked when a user wants to send a composed mail.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   mail_id  The mail ID
+ * @param[out]  handle   The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t and #email_option_t
  */
 EXPORT_API int email_send_mail(int mail_id,    int *handle);
 
 /**
- * @fn email_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id, int *output_handle)
- * @brief      Send a mail. This function is invoked when user wants to send the mail, not been downloaded the attachment.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_id    Specifies the mail ID.
- * @param[out] handle          Specifies the sending handle.
- * @exception  none
- * @see        email_mailbox_t and email_option_t
- * @remarks N/A
+ * @brief Sends a mail.
+ * @details This function is invoked when a user wants to send the mail, not been downloaded the attachment.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   input_mail_id  The mail ID
+ * @param[out]  handle         The sending handle
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t and #email_option_t
  */
 EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id, int *output_handle);
 
 /**
- * @fn email_schedule_sending_mail(int input_mail_id, time_t input_time)
- * @brief      Send a mail. This function is invoked when user wants to send the scheduled mail.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_mail_id    Specifies the mail ID.
- * @param[out] input_time      Specifies the scheduled time.
- * @exception  none
- * @see        email_mailbox_t and email_option_t
- * @remarks N/A
+ * @brief Sends a mail.
+ * @details This function is invoked when a user wants to send the scheduled mail.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   input_mail_id  The mail ID
+ * @param[out]  input_time     The scheduled time
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (refer to #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t and #email_option_t
  */
 EXPORT_API int email_schedule_sending_mail(int input_mail_id, time_t input_time);
 
 
 /**
- * @fn email_sync_header(int input_account_id, int input_mailbox_id, int *handle)
- * @brief      Download header of new emails from mail server.This function is invoked when user wants to download only header of new mails.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_account_id         Specifies the account ID.
- * @param[in] input_mailbox_id         Specifies the mailbox ID.
- * @param[out] handle          Specifies the handle for stopping downloading.
- * @exception  none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Downloads headers of new emails from the mail server.
+ * @details This function is invoked when a user wants to download only the headers of new mails.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  input_account_id  The account ID
+ * @param[in]  input_mailbox_id  The mailbox ID
+ * @param[out] handle            The handle for stopping downloading
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int *handle);
 
 
 /**
- * @fn email_sync_header_for_all_account(int *handle)
- * @brief      Download header of new emails from mail server for all emails.This function is invoked when user wants to download header of new mails for all accounts.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[out] handle          Specifies the handle for stopping downloading.
- * @exception none
- * @see        none
- * @remarks N/A
+ * @brief Downloads headers of new emails from the mail server for all emails.
+ * @details This function is invoked when a user wants to download headers of new mails for all accounts.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[out]  handle  The handle for stopping downloading
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_sync_header_for_all_account(int *handle);
 
 
 /**
-
- * @fn email_download_body(int mail_id, int with_attachment, int *handle)
- * @brief      Download email body from server.This function is invoked when user wants to download email body with/without attachment based on the option with_attachment
- *             from the server.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id                  Specifies the mail ID.
- * @param[in] with_attachment  Specifies the whether attachment is there or not.
- * @param[out] handle          Specifies the handle for stopping downloading.
- * @exception none
- * @see email_mailbox_t
- * @remarks N/A
+ * @brief Downloads an email body from the server.
+ * @details This function is invoked when a user wants to download email body with/without attachment based on the @a with_attachment option
+ *          from the server.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   mail_id          The mail ID
+ * @param[in]   with_attachment  The flag indicating whether there is an attachment
+ * @param[out]  handle           The handle for stopping downloading
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle);
 
 
-
-
-
-
 /**
- * @fn email_download_attachment(int mail_id, const char* nth, int *handle);
- * @brief      Download a email nth-attachment from server.This function is invoked if user wants to download only specific attachment of a mail whose body is already downloaded.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] mail_id          Specifies the mail ID.
- * @param[in] nth                      Specifies the attachment number been saved. the minimum number is "1".
- * @param[out] handle          Specifies the handle for stopping downloading.
- * @exception none
- * @see        email_mailbox_t
- * @remarks N/A
+ * @brief Downloads an email nth-attachment from the server.
+ * @details This function is invoked if a user wants to download only specific attachment of a mail whose body is already downloaded.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  mail_id  The mail ID
+ * @param[in]  nth      The attachment number been saved (The minimum number is "1")
+ * @param[out] handle   The handle for stopping downloading
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_mailbox_t
  */
 EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle);
 
 
 /**
- * @fn email_cancel_job(int account_id, int handle);
- * @brief      cancel the ongoing job.This function is invoked if user wants to cancel any ongoing job of a specified account.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] input_account_id Specifies the account ID.
- * @param[in] input_handle             Specifies the handle for stopping the operation.
- * @param[in] input_cancel_type        Specifies the type of cancellation.
- * @exception   none
- * @see        none
- * @remarks N/A
+ * @brief Cancels the ongoing job.
+ * @details This function is invoked if a user wants to cancel any ongoing job of a specified account.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] input_account_id   The account ID
+ * @param[in] input_handle       The handle for stopping the operation
+ * @param[in] input_cancel_type  The type of cancellation
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_cancel_job(int input_account_id, int input_handle, email_cancelation_type input_cancel_type);
 
@@ -287,90 +317,67 @@ EXPORT_API int email_get_pending_job(email_action_t action, int account_id, int
 EXPORT_API int email_get_network_status(int* on_sending, int* on_receiving) DEPRECATED;
 
 /**
- * @fn email_get_task_information(email_task_information_t *output_task_information)
- * @brief      This function gives the current job information.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[out] output_task_information The array of job information.
- * @param[out] output_task_information_count   The count of job information.
- * @exception  none
- * @see        none
- * @remarks N/A
+ * @brief Gives the current job information.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[out]  output_task_information        The array of job information
+ * @param[out]  output_task_information_count  The count of job information
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count);
 
 /**
- * @fn email_send_saved(int account_id, int *handle)
- * @brief      Send all mails to been saved in Offline-mode.This function is invoked when user wants to send an email and after saving it.
- *             This will save the email in draft mailbox and then sends.
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id                       Specifies the account ID.
- * @param[out] handle                          Specifies the handle for stopping sending.
- * @exception none
- * @see email_option_t
- * @remarks N/A
+ * @brief Sends all mails to be saved in offline-mode.
+ * @details This function is invoked when a user wants to send an email and saving it afterwards.
+ *          This will save the email in draft mailbox and then send it.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id  The account ID
+ * @param[out] handle      The handle for stopping sending
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @see #email_option_t
  */
 EXPORT_API int email_send_saved(int account_id, int *handle);
 
 /**
-
- * @fn email_sync_imap_mailbox_list(int account_id, int *handle)
- *  @brief     fetch all the mailbox names from server and store the non-existing mailboxes in DB.This function is invoked when user wants to download all server mailboxes from IMAP server
- *
- * @return     This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id                       Specifies the account ID.
- * @param[out] handle                  Specifies the handle for stopping Network operation.
- * @exception  none
- * @see        none
- * @remarks N/A
+ * @brief Fetches all mailbox names from the server and stores the non-existing mailboxes in the DB.
+ * @details This function is invoked when a user wants to download all server mailboxes from IMAP server.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id  The account ID
+ * @param[out] handle      The handle for stopping Network operation
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_sync_imap_mailbox_list(int account_id, int *handle);
 
 /**
-
- * @fn 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)
- * @brief      Search the mails on server.
- *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id       Specifies the Account ID
- * @param[in] mailbox_id       Specifies the Mailbox ID
- * @param[in] search_type      Specifies the searching type(EMAIL_SEARCH_FILTER_SUBJECT,  EMAIL_SEARCH_FILTER_SENDER, EMAIL_SEARCH_FILTER_RECIPIENT, EMAIL_SEARCH_FILTER_ALL)
- * @param[in] search_value     Specifies the value to use for searching. (ex : Subject, email address, display name)
- * @exception          none
- * @see email_search_filter_t,
- * @code
- * @endcode
- * @remarks N/A
- */
-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);
-
-/**
-
- * @fn email_clear_result_of_search_mail_on_server(int input_account_id)
- * @brief      Delete the temporarily downloaded mails on local storage.
+ * @brief Queries the maximum mail size limit from the SMTP server.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id       Specifies the Account ID
- * @exception          none
- * @see email_search_filter_t,
- * @code
- * @endcode
- * @remarks N/A
- */
-EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id);
-
-/**
-
- * @fn email_query_smtp_mail_size_limit(int account_id)
- * @brief      query maximum mail size limit from smtp server
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] account_id       Specifies the Account ID
- * @exception          none
- * @code
- * @endcode
- * @remarks N/A
+ * @param[in] account_id  The Account ID
+ * @param[out] handle     The handle for stopping Network operation
+ *
+ * @return #EMAIL_ERROR_NONE on success,
+ *         otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_query_smtp_mail_size_limit(int account_id, int *handle);
 
@@ -379,8 +386,8 @@ EXPORT_API int email_query_smtp_mail_size_limit(int account_id, int *handle);
 #endif /* __cplusplus */
 
 /**
-* @}
-*/
+ * @}
+ */
 
 
 #endif /* __EMAIL_API_NETWORK_H__ */
index befcbc3..68b5644 100755 (executable)
 #include "email-types.h"
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_API_RULE Email Rule API
-* @{
-*/
-
- /**
+ * @file email-api-rule.h
+ * @brief This file contains the data structures and interfaces of Rule related Functionality provided by email-service.
  *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api-rule.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief              This file contains the data structures and interfaces of Rule related Functionality provided by
- *                     email-service .
  * @{
-
  * @code
- *     #include "email-api-rule.h"
- *     bool
- *     other_app_invoke_uniform_api_sample(int* error_code)
- *     {
- *             int err = EMAIL_ERROR_NONE;
- *             email_rule_t*  rule = NULL;
- *             int filter_id = 1;
- *             int count = 0;
- *
- *             // Get a information of filtering
- *             printf("Enter filter Id:\n");
- *             scanf("%d",&filter_id);
- *
- *             if(EMAIL_ERROR_NONE == email_get_rule (filter_id,&rule))
- *                     //success
- *             else
- *                     //failure
- *
- *             // Get all filterings
- *             if(EMAIL_ERROR_NONE == email_get_rule_list(&rule,&count))
- *                     //success
- *             else
- *                     //failure
- *
- *
- *             // Add a filter information
- *             if(EMAIL_ERROR_NONE == email_add_rule (rule))
- *                     //success
- *             else
- *                     //failure
- *             err = email_free_rule (&rule,1);
- *
- *             // Change a filter information
- *             if(EMAIL_ERROR_NONE == email_update_rule (filter_id,rule))
- *                     //success
- *             else
- *                     //failure
- *             err = email_free_rule (&rule,1);
- *
- *             // Delete a filter information
- *             printf("Enter filter Id:\n");
- *             scanf("%d",&filter_id);
- *
- *             if(EMAIL_ERROR_NONE == email_delete_rule (filter_id))
- *                     //success
- *             else
- *                     //failure
- *
- *             // Free allocated memory
- *             if(EMAIL_ERROR_NONE == email_free_rule (&rule,1))
- *                     //success
- *             else
- *                     //failure
- *
- *     }
+ *  #include "email-api-rule.h"
+ *  bool
+ *  other_app_invoke_uniform_api_sample(int* error_code)
+ *  {
+ *      int err = EMAIL_ERROR_NONE;
+ *      email_rule_t*  rule = NULL;
+ *      int filter_id = 1;
+ *      int count = 0;
+ *
+ *      // Gets information of filtering
+ *      printf("Enter filter Id:\n");
+ *      scanf("%d",&filter_id);
+ *
+ *      if(EMAIL_ERROR_NONE == email_get_rule (filter_id,&rule))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      // Get all filterings
+ *      if(EMAIL_ERROR_NONE == email_get_rule_list(&rule,&count))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *
+ *      // Adds a filter information
+ *      if(EMAIL_ERROR_NONE == email_add_rule (rule))
+ *          //success
+ *      else
+ *          //failure
+ *      err = email_free_rule (&rule,1);
+ *
+ *      // Changes a filter information
+ *      if(EMAIL_ERROR_NONE == email_update_rule (filter_id,rule))
+ *          //success
+ *      else
+ *          //failure
+ *      err = email_free_rule (&rule,1);
+ *
+ *      // Deletes a filter information
+ *      printf("Enter filter Id:\n");
+ *      scanf("%d",&filter_id);
+ *
+ *      if(EMAIL_ERROR_NONE == email_delete_rule (filter_id))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *      // Free allocated memory
+ *      if(EMAIL_ERROR_NONE == email_free_rule (&rule,1))
+ *          //success
+ *      else
+ *          //failure
+ *
+ *  }
  *
  * @endcode
  * @}
  */
 
+/**
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_RULE_MOUDLE Rule API
+ * @brief Rule API is a set of operations to manage email rules like add, get, delete or update rule related details.
+ *
+ * @section EMAIL_SERVICE_RULE_MOUDLE_HEADER Required Header
+ *   \#include <email-api-rule.h>
+ *
+ * @section EMAIL_SERVICE_RULE_MOUDLE_OVERVIEW Overview
+ * Rule API is a set of operations to manage email rules like add, get, delete or update rule related details.
+ */
 
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_RULE_MOUDLE
+ * @{
+ */
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
 /**
-
-
- * @fn email_get_rule(int filter_id, email_rule_t** filtering_set)
- * @brief      Get a information of filtering.
+ * @brief Gets a filter rule.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  filter_id      The filter ID
+ * @param[out] filtering_set  The filter rule
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @param[in] filter_id                        Specifies the filter ID.
- * @param[out] filtering_set   The returned information of filter are saved here.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        email_rule_t
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  #EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_rule_t
  */
 EXPORT_API int email_get_rule(int filter_id, email_rule_t** filtering_set);
 
 /**
-
-
- * @fn email_get_rule_list(email_rule_t** filtering_set, int* count)
- * @brief      Get all filterings.This function gives all the filter rules already set before by user.
- *             This will provide total number of filter rules available and information of all rules.
+ * @brief Gets all filter rules.
+ * @details This function gives all the filter rules already set before by user.
+ *          This will provide total number of filter rules available and information of all rules.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[out] filtering_set  The filtering rules (possibly @c NULL)
+ * @param[out] count          The count of returned filters (possibly @c 0)
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @param[out] filtering_set           The returned filterings are saved here.(possibly NULL)
- * @param[out] count                           The count of returned filters is saved here.(possibly 0)
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        email_rule_t
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_rule_t
  */
 EXPORT_API int email_get_rule_list(email_rule_t** filtering_set, int* count);
 
 /**
-
-
- * @fn email_add_rule(email_rule_t* filtering_set)
- * @brief      Add a filter information.This function is invoked if user wants to add a new filter rule.
+ * @brief Adds a filter rule.
+ * @details This function is invoked if a user wants to add a new filter rule.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  filtering_set  The pointer of adding a filter structure
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] filtering_set            Specifies the pointer of adding filter structure.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        email_rule_t
- * @remarks N/A
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_rule_t
  */
 EXPORT_API int email_add_rule(email_rule_t* filtering_set);
 
 /**
-
-
- * @fn email_update_rule(int filter_id, email_rule_t* new_set)
- * @brief      Change a filter information.This function will update the existing filter rule with new information.
+ * @brief Changes a filter rule.
+ * @details This function will update the existing filter rule with new information.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @param[in] filter_id        Specifies the original filter ID.
- * @param[in] new_set  Specifies the information of new filter.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        email_rule_t
- * @remarks N/A
+ * @param[in]  filter_id  The original filter ID
+ * @param[in]  new_set    The information of new filter
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_rule_t
  */
 EXPORT_API int email_update_rule(int filter_id, email_rule_t* new_set);
 
 /**
-
- * @fn email_delete_rule(int filter_id)
- * @brief      Delete a filter information.This function will delete the exsting filter information by specified filter Id.
+ * @brief Deletes a filter rule.
+ * @details This function will delete the existing filter information by the specified filter ID.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  filter_id  The filter ID
+ *
+ * @return #EMAIL_ERROR_NONE on success,
+ *         otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @param[in] filter_id        Specifies the filter ID.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        none
- * @remarks N/A
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
  */
 EXPORT_API int email_delete_rule(int filter_id);
 
 /**
-
- * @fn email_free_rule (email_rule_t** filtering_set, int count)
- * @brief      Free allocated memory.
+ * @brief Frees allocated memory.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in] filtering_set  The pointer of pointer of filter structure for memory freeing
+ * @param[in] count          The count of filter
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @param[in] filtering_set    Specifies the pointer of pointer of filter structure for memory free.
- * @param[in] count                    Specifies the count of filter.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        email_rule_t
- * @remarks N/A
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
+ *
+ * @see #email_rule_t
  */
 EXPORT_API int email_free_rule (email_rule_t** filtering_set, int count);
 
 /**
-
- * @fn email_apply_rule(int filter_id)
- * @brief      Delete a filter information.This function will delete the exsting filter information by specified filter Id.
+ * @brief Deletes a filter rule.
+ * @details This function will delete the existing filter information by the specified filter ID.
  *
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure
- * @param[in] filter_id        Specifies the filter ID.
- * @exception  EMAIL_ERROR_INVALID_PARAM       -Invalid argument
- * @see        none
- * @remarks N/A
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  filter_id  The filter ID
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ *
+ * @retval  EMAIL_ERROR_INVALID_PARAM  Invalid argument
  */
 EXPORT_API int email_apply_rule(int filter_id);
 
@@ -220,8 +246,8 @@ EXPORT_API int email_apply_rule(int filter_id);
 #endif /* __cplusplus */
 
 /**
-* @} @}
-*/
+ * @}
+ */
 
 
 #endif /* __EMAIL_API_RULE_H__ */
index fc66834..a9d27d8 100755 (executable)
@@ -29,136 +29,198 @@ extern "C" {
 #endif /* __cplusplus */
 
 /**
+ * @file email-api-smime.h
+ */
 
- * @fn email_add_certificate(char *certificate_path, char *email_address)
- * @brief       Store infomations of public certificate in database.
+/**
+ * @internal
+ * @ingroup EMAIL_SERVICE_FRAMEWORK
+ * @defgroup EMAIL_SERVICE_SMIME_MODULE SMIME API
+ * @brief SMIME API is a set of operations to handle SMIME data for secured email.
  *
- * @param[in] certificate_path         File path of public certificate.
- * @param[in] email_address            Keyword for searching the certificate information
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @section EMAIL_SERVICE_SMIME_MODULE_HEADER Required Header
+ *   \#include <email-api-smime.h>
+ *
+ * @section EMAIL_SERVICE_SMIME_MODULE_OVERVIEW Overview
+ * SMIME API is a set of operations to handle SMIME data for secured email.
  */
-EXPORT_API int email_add_certificate(char *certificate_path, char *email_address);
 
 /**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_SMIME_MODULE
+ * @{
+ */
+
 
- * @fn email_delete_certificate(char *email_address)
- * @brief       Delete infomations of public certificate in database.
+/**
+ * @brief Stores a public certificate information in the database.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @param[in] email_address            Keyword for deleting the certificate information
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @param[in] certificate_path  The file path of public certificate
+ * @param[in] email_address     The keyword for searching the certificate information
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
-EXPORT_API int email_delete_certificate(char *email_address);
+EXPORT_API int email_add_certificate(char *certificate_path, char *email_address);
 
 /**
+ * @brief Deletes a public certificate information from the database.
+ *
+ * @param[in]  email_address  The keyword for deleting the certificate information
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ */
+EXPORT_API int email_delete_certificate(char *email_address);
 
- * @fn email_get_certificate(char *email_address, email_certificate_t **certificate)
- * @brief       Get infomations of public certificate in database.
+/**
+ * @brief Gets the the public certificate information from the database.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
  *
- * @param[in] email_address            Keyword for geting the certificate information
- * @param[out] certificate              Specifies the certificate
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @param[in]  email_address  The keyword for getting the certificate information
+ * @param[out] certificate    The certificate
+ *
+ * @return  #EMAIL_ERROR_NONE on success, 
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **certificate);
 
 /**
+ * @brief Gets a decrypted message.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  mail_id                  The mail ID
+ * @param[out] output_mail_data         The mail data
+ * @param[out] output_attachment_data   The mail attachment data
+ * @param[out] output_attachment_count  The count of attachment
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+ */
+EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
 
- * @fn email_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
-
- * @brief       Get the decrypted message 
- * @param[in] mail_id                  Specifies the mail_id
- * @param[out] output_mail_data                Specifies the mail_data 
+/**
+ * @brief Gets a decrypted message.
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ * @param[in] input_mail_data          Specifies the signed mail data
+ * @param[in] input_attachment_data    Specifies the attachment of signed mail
+ * @param[in] input_attachment_count   Specifies the attachment count of signed mail
+ * @param[out] output_mail_data                Specifies the mail_data
  * @param[out] output_attachment_data  Specifies the mail_attachment_data
  * @param[out] output_attachment_count Specifies the count of attachment
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @return EMAIL_ERROR_NONE on success or an error code (refer to EMAIL_ERROR_XXX) on failure
  */
-EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
-
+EXPORT_API int email_get_decrypt_message_ex(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count,
+                                            email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
 /**
-
- * @fn email_verify_signature(int mail_id, int *verify);
-
- * @brief       Verify the signed mail 
- * @param[in] mail_id          Specifies the mail_id
- * @param[out] verify          Specifies verify [false : failed verify, true : success the verification]
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @brief Verifies a signed mail.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in] mail_id  The mail ID
+ * @param[out] verify  The verification state \n
+ *                     [false : failed verification, true : verification successful]
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_verify_signature(int mail_id, int *verify);
 
+/**
+* @brief Verifies a signed mail.
+* @since_tizen 2.3
+*
+* @param[in]  input_mail_data         The signed mail data
+* @param[in]  input_attachment_data   The attachment of signed mail
+* @param[in]  input_attachment_count  The attachment count of signed mail
+* @param[out] verify                  The verification status \n
+*                                     false : failed verification, true : verification successful
+*
+* @return  #EMAIL_ERROR_NONE on success,
+*          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
+*/
 EXPORT_API int email_verify_signature_ex(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count, int *verify);
 
 /**
-
- * @fn email_verify_certificate(char *certificate_path, int *verify);
-
- * @brief       Verify the certificate 
- * @param[in] certificate_path Specifies the path of certificate
- * @param[out] verify          Specifies verify [false : failed verify, true : success the verification]
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @brief Verifies a certificate.
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]   certificate_path  The path of the certificate
+ * @param[out]  verify            The verification status \n
+ *                                false : failed verification, true : verification successful
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_verify_certificate(char *certificate_path, int *verify);
 
 /**
-
- * @fn email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle);
-
- * @brief       Get the certificate at the server [Using exchange server] 
- * @param[in] account_id       Specifies an account_id
- * @param[in] email_address    Specifies email address for getting certificate
- * @param[out] handle          Specifies the handle for stopping 
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @brief Gets the certificate from the server (using exchange server).
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id     The account ID
+ * @param[in]  email_address  The email address that gets a certificate
+ * @param[out] handle         The handle for stopping
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle);
 
 /**
-
- * @fn email_validate_certificate(int account_id, char *email_address, unsigned *handle);
-
- * @brief       Verfiy the certificate to the server [Using exchange server] 
- * @param[in] account_id       Specifies an account_id
- * @param[in] email_address    Specifies email address for validating certificate
- * @param[out] handle          Specifies the handle for stopping 
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @brief Verifies the certificate to the server (using exchange server).
+ *
+ * @since_tizen 2.3
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/email
+ *
+ * @param[in]  account_id     The account ID
+ * @param[in]  email_address  The email address that validates a certificate
+ * @param[out] handle         The handle for stopping
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise an error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle);
 
 /**
-
- * @fn email_free_certificate(email_certificate_t **certificate, int count);
-
- * @brief       Free the memory of certificate 
- * @param[in] certificate      Specifies the certificate
- * @param[in] count            Specifies the count of certificates
- * @exception  none
- * @return This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR_XXX) on failure.
- * @see
- * @remarks N/A
+ * @brief Frees the memory of the certificate.
+ *
+ * @since_tizen 2.3
+ * @privlevel N/P
+ *
+ * @param[in] certificate  The certificate
+ * @param[in] count        The count of certificates
+ *
+ * @return  #EMAIL_ERROR_NONE on success,
+ *          otherwise error code (see #EMAIL_ERROR_XXX) on failure
  */
 EXPORT_API int email_free_certificate(email_certificate_t **certificate, int count);
 
+/**
+ * @}
+ */
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 8da1b26..e3841e9 100755 (executable)
 #include "email-api-smime.h"
 #include "email-api-etc.h"
 
-
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
-/**
-* @{
-*/
-
-/**
- *
- * This file contains the data structures and interfaces needed for application,
- * to interact with email-service.
- * @file               email-api.h
- * @author     Kyuho Jo <kyuho.jo@samsung.com>
- * @author     Sunghyun Kwon <sh0701.kwon@samsung.com>
- * @version    0.1
- * @brief              This file contains the data structures and interfaces provided by
- *                     email-service.
+ * @file email-api.h
+ * @brief This file contains the data structures and interfaces provided by email-service.
  */
 
 
@@ -67,9 +50,6 @@ extern "C" {
 }
 #endif
 
-/**
-* @} @}
-*/
 
 #endif /* __EMAIL_MAPI_H__ */
 
index 0f9ebe5..060c5a8 100755 (executable)
@@ -12,6 +12,7 @@ MESSAGE(">>> current directory: ${CMAKE_CURRENT_SOURCE_DIR}")
 MESSAGE(">>> Build type: ${CMAKE_BUILD_TYPE}")
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VISIBILITY} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
 
 SET(COMMON-USE-LIB "email-common-use")
 SET(COMMON-USE-SRCS 
@@ -28,9 +29,7 @@ INCLUDE_DIRECTORIES(
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(common_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf uw-imap-toolkit security-server libtzplatform-config)
-
-#pkg_check_modules(common_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf contacts-service2 uw-imap-toolkit security-server libtzplatform-config)
+pkg_check_modules(common_pkgs REQUIRED glib-2.0 dlog dbus-1 vconf contacts-service2 uw-imap-toolkit) 
 
 FOREACH(flag ${common_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index f9437e5..45373c2 100755 (executable)
@@ -45,71 +45,79 @@ INTERNAL_FUNC int em_convert_account_to_account_tbl(email_account_t *account, em
        EM_DEBUG_FUNC_BEGIN("account[%p], account_tbl[%p]", account, account_tbl);
        int ret = 1;
 
-       account_tbl->account_name                             = EM_SAFE_STRDUP(account->account_name);
-       account_tbl->incoming_server_type                     = account->incoming_server_type;
-       account_tbl->incoming_server_address                  = EM_SAFE_STRDUP(account->incoming_server_address);
-       account_tbl->user_email_address                       = EM_SAFE_STRDUP(account->user_email_address);
-       account_tbl->incoming_server_user_name                = EM_SAFE_STRDUP(account->incoming_server_user_name);
-       account_tbl->incoming_server_password                 = EM_SAFE_STRDUP(account->incoming_server_password);
-       account_tbl->retrieval_mode                           = account->retrieval_mode;
-       account_tbl->incoming_server_port_number              = account->incoming_server_port_number;
-       account_tbl->incoming_server_secure_connection        = account->incoming_server_secure_connection;
-       account_tbl->outgoing_server_type                     = account->outgoing_server_type;
-       account_tbl->outgoing_server_address                  = EM_SAFE_STRDUP(account->outgoing_server_address);
-       account_tbl->outgoing_server_port_number              = account->outgoing_server_port_number;
-       account_tbl->outgoing_server_need_authentication      = account->outgoing_server_need_authentication;
-       account_tbl->outgoing_server_secure_connection        = account->outgoing_server_secure_connection;
-       account_tbl->outgoing_server_user_name                = EM_SAFE_STRDUP(account->outgoing_server_user_name);
-       account_tbl->outgoing_server_password                 = EM_SAFE_STRDUP(account->outgoing_server_password);
-       account_tbl->user_display_name                        = EM_SAFE_STRDUP(account->user_display_name);
-       account_tbl->reply_to_address                         = EM_SAFE_STRDUP(account->reply_to_address);
-       account_tbl->return_address                           = EM_SAFE_STRDUP(account->return_address);
-       account_tbl->account_id                               = account->account_id;
-       account_tbl->keep_mails_on_pop_server_after_download  = account->keep_mails_on_pop_server_after_download;
-       account_tbl->auto_download_size                       = account->auto_download_size;
-       account_tbl->peak_interval                            = account->peak_interval;
-       account_tbl->peak_days                                = account->peak_days;
-       account_tbl->peak_start_time                          = account->peak_start_time;
-       account_tbl->peak_end_time                            = account->peak_end_time;
-
-
-       account_tbl->outgoing_server_use_same_authenticator   = account->outgoing_server_use_same_authenticator;
-       account_tbl->auto_resend_times                        = account->auto_resend_times;
-       account_tbl->pop_before_smtp                          = account->pop_before_smtp;
-       account_tbl->incoming_server_requires_apop            = account->incoming_server_requires_apop;
-       account_tbl->incoming_server_authentication_method    = account->incoming_server_authentication_method;
-       account_tbl->logo_icon_path                           = EM_SAFE_STRDUP(account->logo_icon_path);
-
-       account_tbl->user_data                                = em_memdup(account->user_data, account->user_data_length);
-       account_tbl->user_data_length                         = account->user_data_length;
-       account_tbl->roaming_option                           = account->roaming_option;
-
-       account_tbl->options.priority                         = account->options.priority;
-       account_tbl->options.keep_local_copy                  = account->options.keep_local_copy;
-       account_tbl->options.req_delivery_receipt             = account->options.req_delivery_receipt;
-       account_tbl->options.req_read_receipt                 = account->options.req_read_receipt;
-       account_tbl->options.download_limit                   = account->options.download_limit;
-       account_tbl->options.block_address                    = account->options.block_address;
-       account_tbl->options.block_subject                    = account->options.block_subject;
-       account_tbl->options.display_name_from                = EM_SAFE_STRDUP(account->options.display_name_from);
-       account_tbl->options.reply_with_body                  = account->options.reply_with_body;
-       account_tbl->options.forward_with_files               = account->options.forward_with_files;
-       account_tbl->options.add_myname_card                  = account->options.add_myname_card;
-       account_tbl->options.add_signature                    = account->options.add_signature;
-       account_tbl->options.signature                        = EM_SAFE_STRDUP(account->options.signature);
-       account_tbl->options.add_my_address_to_bcc            = account->options.add_my_address_to_bcc;
-       account_tbl->check_interval                           = account->check_interval;
-       account_tbl->account_svc_id                           = account->account_svc_id;
-       account_tbl->sync_status                              = account->sync_status;
-       account_tbl->sync_disabled                            = account->sync_disabled;
-       account_tbl->default_mail_slot_size                   = account->default_mail_slot_size;
-       account_tbl->smime_type                               = account->smime_type;
-       account_tbl->certificate_path                         = EM_SAFE_STRDUP(account->certificate_path);
-       account_tbl->cipher_type                              = account->cipher_type;
-       account_tbl->digest_type                              = account->digest_type;
-       account_tbl->outgoing_server_size_limit               = account->outgoing_server_size_limit;
-
-
+       account_tbl->account_name                                  = EM_SAFE_STRDUP(account->account_name);
+       account_tbl->incoming_server_type                          = account->incoming_server_type;
+       account_tbl->incoming_server_address                       = EM_SAFE_STRDUP(account->incoming_server_address);
+       account_tbl->user_email_address                            = EM_SAFE_STRDUP(account->user_email_address);
+       account_tbl->incoming_server_user_name                     = EM_SAFE_STRDUP(account->incoming_server_user_name);
+       account_tbl->incoming_server_password                      = EM_SAFE_STRDUP(account->incoming_server_password);
+       account_tbl->retrieval_mode                                = account->retrieval_mode;
+       account_tbl->incoming_server_port_number                   = account->incoming_server_port_number;
+       account_tbl->incoming_server_secure_connection             = account->incoming_server_secure_connection;
+       account_tbl->outgoing_server_type                          = account->outgoing_server_type;
+       account_tbl->outgoing_server_address                       = EM_SAFE_STRDUP(account->outgoing_server_address);
+       account_tbl->outgoing_server_port_number                   = account->outgoing_server_port_number;
+       account_tbl->outgoing_server_need_authentication           = account->outgoing_server_need_authentication;
+       account_tbl->outgoing_server_secure_connection             = account->outgoing_server_secure_connection;
+       account_tbl->outgoing_server_user_name                     = EM_SAFE_STRDUP(account->outgoing_server_user_name);
+       account_tbl->outgoing_server_password                      = EM_SAFE_STRDUP(account->outgoing_server_password);
+       account_tbl->user_display_name                             = EM_SAFE_STRDUP(account->user_display_name);
+       account_tbl->reply_to_address                              = EM_SAFE_STRDUP(account->reply_to_address);
+       account_tbl->return_address                                = EM_SAFE_STRDUP(account->return_address);
+       account_tbl->account_id                                    = account->account_id;
+       account_tbl->keep_mails_on_pop_server_after_download       = account->keep_mails_on_pop_server_after_download;
+       account_tbl->auto_download_size                            = account->auto_download_size;
+       account_tbl->peak_interval                                 = account->peak_interval;
+       account_tbl->peak_days                                     = account->peak_days;
+       account_tbl->peak_start_time                               = account->peak_start_time;
+       account_tbl->peak_end_time                                 = account->peak_end_time;
+
+
+       account_tbl->outgoing_server_use_same_authenticator        = account->outgoing_server_use_same_authenticator;
+       account_tbl->auto_resend_times                             = account->auto_resend_times;
+       account_tbl->pop_before_smtp                               = account->pop_before_smtp;
+       account_tbl->incoming_server_requires_apop                 = account->incoming_server_requires_apop;
+       account_tbl->incoming_server_authentication_method         = account->incoming_server_authentication_method;
+       account_tbl->logo_icon_path                                = EM_SAFE_STRDUP(account->logo_icon_path);
+
+       account_tbl->roaming_option                                = account->roaming_option;
+       account_tbl->color_label                                   = account->color_label;
+       account_tbl->user_data                                     = em_memdup(account->user_data, account->user_data_length);
+       account_tbl->user_data_length                              = account->user_data_length;
+
+       account_tbl->options.priority                              = account->options.priority;
+       account_tbl->options.keep_local_copy                       = account->options.keep_local_copy;
+       account_tbl->options.req_delivery_receipt                  = account->options.req_delivery_receipt;
+       account_tbl->options.req_read_receipt                      = account->options.req_read_receipt;
+       account_tbl->options.download_limit                        = account->options.download_limit;
+       account_tbl->options.block_address                         = account->options.block_address;
+       account_tbl->options.block_subject                         = account->options.block_subject;
+       account_tbl->options.display_name_from                     = EM_SAFE_STRDUP(account->options.display_name_from);
+       account_tbl->options.reply_with_body                       = account->options.reply_with_body;
+       account_tbl->options.forward_with_files                    = account->options.forward_with_files;
+       account_tbl->options.add_myname_card                       = account->options.add_myname_card;
+       account_tbl->options.add_signature                         = account->options.add_signature;
+       account_tbl->options.signature                             = EM_SAFE_STRDUP(account->options.signature);
+       account_tbl->options.add_my_address_to_bcc                 = account->options.add_my_address_to_bcc;
+       account_tbl->options.notification_status                   = account->options.notification_status;
+       account_tbl->options.vibrate_status                        = account->options.vibrate_status;
+       account_tbl->options.display_content_status                = account->options.display_content_status;
+       account_tbl->options.default_ringtone_status               = account->options.default_ringtone_status;
+       account_tbl->options.alert_ringtone_path                   = EM_SAFE_STRDUP(account->options.alert_ringtone_path);
+
+       account_tbl->check_interval                                = account->check_interval;
+       account_tbl->account_svc_id                                = account->account_svc_id;
+       account_tbl->sync_status                                   = account->sync_status;
+       account_tbl->sync_disabled                                 = account->sync_disabled;
+       account_tbl->default_mail_slot_size                        = account->default_mail_slot_size;
+       account_tbl->smime_type                                    = account->smime_type;
+       account_tbl->certificate_path                              = EM_SAFE_STRDUP(account->certificate_path);
+       account_tbl->cipher_type                                   = account->cipher_type;
+       account_tbl->digest_type                                   = account->digest_type;
+       account_tbl->outgoing_server_size_limit                    = account->outgoing_server_size_limit;
+       account_tbl->wifi_auto_download                            = account->wifi_auto_download;
+
+    account_tbl->user_name                                     = EM_SAFE_STRDUP(account->user_name);
        EM_DEBUG_FUNC_END();
        return ret;
 }
@@ -119,65 +127,73 @@ INTERNAL_FUNC int em_convert_account_tbl_to_account(emstorage_account_tbl_t *acc
        EM_DEBUG_FUNC_BEGIN("account_tbl[%p], account[%p]", account_tbl, account);
        int ret = 1;
 
-       account->account_name                             = EM_SAFE_STRDUP(account_tbl->account_name);
-       account->incoming_server_type                     = account_tbl->incoming_server_type;
-       account->incoming_server_address                  = EM_SAFE_STRDUP(account_tbl->incoming_server_address);
-       account->user_email_address                       = EM_SAFE_STRDUP(account_tbl->user_email_address);
-       account->incoming_server_user_name                = EM_SAFE_STRDUP(account_tbl->incoming_server_user_name);
-       account->incoming_server_password                 = EM_SAFE_STRDUP(account_tbl->incoming_server_password);
-       account->retrieval_mode                           = account_tbl->retrieval_mode;
-       account->incoming_server_port_number              = account_tbl->incoming_server_port_number;
-       account->incoming_server_secure_connection        = account_tbl->incoming_server_secure_connection;
-       account->outgoing_server_type                     = account_tbl->outgoing_server_type;
-       account->outgoing_server_address                  = EM_SAFE_STRDUP(account_tbl->outgoing_server_address);
-       account->outgoing_server_port_number              = account_tbl->outgoing_server_port_number;
-       account->outgoing_server_need_authentication      = account_tbl->outgoing_server_need_authentication;
-       account->outgoing_server_secure_connection        = account_tbl->outgoing_server_secure_connection;
-       account->outgoing_server_user_name                = EM_SAFE_STRDUP(account_tbl->outgoing_server_user_name);
-       account->outgoing_server_password                 = EM_SAFE_STRDUP(account_tbl->outgoing_server_password);
-       account->user_display_name                        = EM_SAFE_STRDUP(account_tbl->user_display_name);
-       account->reply_to_address                         = EM_SAFE_STRDUP(account_tbl->reply_to_address);
-       account->return_address                           = EM_SAFE_STRDUP(account_tbl->return_address);
-       account->account_id                               = account_tbl->account_id;
-       account->keep_mails_on_pop_server_after_download  = account_tbl->keep_mails_on_pop_server_after_download;
-       account->auto_download_size                       = account_tbl->auto_download_size;
-       account->peak_interval                            = account_tbl->peak_interval;
-       account->peak_days                                = account_tbl->peak_days;
-       account->peak_start_time                          = account_tbl->peak_start_time;
-       account->peak_end_time                            = account_tbl->peak_end_time;
-       account->outgoing_server_use_same_authenticator   = account_tbl->outgoing_server_use_same_authenticator;
-       account->pop_before_smtp                          = account_tbl->pop_before_smtp;
-       account->auto_resend_times                        = account_tbl->auto_resend_times;
-       account->incoming_server_requires_apop            = account_tbl->incoming_server_requires_apop;
-       account->incoming_server_authentication_method    = account_tbl->incoming_server_authentication_method;
-       account->logo_icon_path                           = EM_SAFE_STRDUP(account_tbl->logo_icon_path);
-       account->user_data                                = em_memdup(account_tbl->user_data, account_tbl->user_data_length);
-       account->user_data_length                         = account_tbl->user_data_length;
-       account->roaming_option                           = account_tbl->roaming_option;
-       account->options.priority                         = account_tbl->options.priority;
-       account->options.keep_local_copy                  = account_tbl->options.keep_local_copy;
-       account->options.req_delivery_receipt             = account_tbl->options.req_delivery_receipt;
-       account->options.req_read_receipt                 = account_tbl->options.req_read_receipt;
-       account->options.download_limit                   = account_tbl->options.download_limit;
-       account->options.block_address                    = account_tbl->options.block_address;
-       account->options.block_subject                    = account_tbl->options.block_subject;
-       account->options.display_name_from                = EM_SAFE_STRDUP(account_tbl->options.display_name_from);
-       account->options.reply_with_body                  = account_tbl->options.reply_with_body;
-       account->options.forward_with_files               = account_tbl->options.forward_with_files;
-       account->options.add_myname_card                  = account_tbl->options.add_myname_card;
-       account->options.add_signature                    = account_tbl->options.add_signature;
-       account->options.signature                        = EM_SAFE_STRDUP(account_tbl->options.signature);
-       account->options.add_my_address_to_bcc            = account_tbl->options.add_my_address_to_bcc;
-       account->check_interval                           = account_tbl->check_interval;
-       account->account_svc_id                           = account_tbl->account_svc_id;
-       account->sync_status                              = account_tbl->sync_status;
-       account->sync_disabled                            = account_tbl->sync_disabled;
-       account->default_mail_slot_size                   = account_tbl->default_mail_slot_size;
-       account->smime_type                               = account_tbl->smime_type;
-       account->certificate_path                         = EM_SAFE_STRDUP(account_tbl->certificate_path);
-       account->cipher_type                              = account_tbl->cipher_type;
-       account->digest_type                              = account_tbl->digest_type;
-       account->outgoing_server_size_limit               = account_tbl->outgoing_server_size_limit;
+       account->account_name                                      = EM_SAFE_STRDUP(account_tbl->account_name);
+       account->incoming_server_type                              = account_tbl->incoming_server_type;
+       account->incoming_server_address                           = EM_SAFE_STRDUP(account_tbl->incoming_server_address);
+       account->user_email_address                                = EM_SAFE_STRDUP(account_tbl->user_email_address);
+       account->incoming_server_user_name                         = EM_SAFE_STRDUP(account_tbl->incoming_server_user_name);
+       account->incoming_server_password                          = EM_SAFE_STRDUP(account_tbl->incoming_server_password);
+       account->retrieval_mode                                    = account_tbl->retrieval_mode;
+       account->incoming_server_port_number                       = account_tbl->incoming_server_port_number;
+       account->incoming_server_secure_connection                 = account_tbl->incoming_server_secure_connection;
+       account->outgoing_server_type                              = account_tbl->outgoing_server_type;
+       account->outgoing_server_address                           = EM_SAFE_STRDUP(account_tbl->outgoing_server_address);
+       account->outgoing_server_port_number                       = account_tbl->outgoing_server_port_number;
+       account->outgoing_server_need_authentication               = account_tbl->outgoing_server_need_authentication;
+       account->outgoing_server_secure_connection                 = account_tbl->outgoing_server_secure_connection;
+       account->outgoing_server_user_name                         = EM_SAFE_STRDUP(account_tbl->outgoing_server_user_name);
+       account->outgoing_server_password                          = EM_SAFE_STRDUP(account_tbl->outgoing_server_password);
+       account->user_display_name                                 = EM_SAFE_STRDUP(account_tbl->user_display_name);
+       account->reply_to_address                                  = EM_SAFE_STRDUP(account_tbl->reply_to_address);
+       account->return_address                                    = EM_SAFE_STRDUP(account_tbl->return_address);
+       account->account_id                                        = account_tbl->account_id;
+       account->keep_mails_on_pop_server_after_download           = account_tbl->keep_mails_on_pop_server_after_download;
+       account->auto_download_size                                = account_tbl->auto_download_size;
+       account->peak_interval                                     = account_tbl->peak_interval;
+       account->peak_days                                         = account_tbl->peak_days;
+       account->peak_start_time                                   = account_tbl->peak_start_time;
+       account->peak_end_time                                     = account_tbl->peak_end_time;
+       account->outgoing_server_use_same_authenticator            = account_tbl->outgoing_server_use_same_authenticator;
+       account->pop_before_smtp                                   = account_tbl->pop_before_smtp;
+       account->auto_resend_times                                 = account_tbl->auto_resend_times;
+       account->incoming_server_requires_apop                     = account_tbl->incoming_server_requires_apop;
+       account->incoming_server_authentication_method             = account_tbl->incoming_server_authentication_method;
+       account->logo_icon_path                                    = EM_SAFE_STRDUP(account_tbl->logo_icon_path);
+       account->user_data                                         = em_memdup(account_tbl->user_data, account_tbl->user_data_length);
+       account->user_data_length                                  = account_tbl->user_data_length;
+       account->roaming_option                                    = account_tbl->roaming_option;
+       account->color_label                                       = account_tbl->color_label;
+       account->options.priority                                  = account_tbl->options.priority;
+       account->options.keep_local_copy                           = account_tbl->options.keep_local_copy;
+       account->options.req_delivery_receipt                      = account_tbl->options.req_delivery_receipt;
+       account->options.req_read_receipt                          = account_tbl->options.req_read_receipt;
+       account->options.download_limit                            = account_tbl->options.download_limit;
+       account->options.block_address                             = account_tbl->options.block_address;
+       account->options.block_subject                             = account_tbl->options.block_subject;
+       account->options.display_name_from                         = EM_SAFE_STRDUP(account_tbl->options.display_name_from);
+       account->options.reply_with_body                           = account_tbl->options.reply_with_body;
+       account->options.forward_with_files                        = account_tbl->options.forward_with_files;
+       account->options.add_myname_card                           = account_tbl->options.add_myname_card;
+       account->options.add_signature                             = account_tbl->options.add_signature;
+       account->options.signature                                 = EM_SAFE_STRDUP(account_tbl->options.signature);
+       account->options.add_my_address_to_bcc                     = account_tbl->options.add_my_address_to_bcc;
+       account->options.notification_status                       = account_tbl->options.notification_status;
+       account->options.vibrate_status                            = account_tbl->options.vibrate_status;
+       account->options.display_content_status                    = account_tbl->options.display_content_status;
+       account->options.default_ringtone_status                   = account_tbl->options.default_ringtone_status;
+       account->options.alert_ringtone_path                       = EM_SAFE_STRDUP(account_tbl->options.alert_ringtone_path);
+       account->check_interval                                    = account_tbl->check_interval;
+       account->account_svc_id                                    = account_tbl->account_svc_id;
+       account->sync_status                                       = account_tbl->sync_status;
+       account->sync_disabled                                     = account_tbl->sync_disabled;
+       account->default_mail_slot_size                            = account_tbl->default_mail_slot_size;
+       account->smime_type                                        = account_tbl->smime_type;
+       account->certificate_path                                  = EM_SAFE_STRDUP(account_tbl->certificate_path);
+       account->cipher_type                                       = account_tbl->cipher_type;
+       account->digest_type                                       = account_tbl->digest_type;
+       account->outgoing_server_size_limit                        = account_tbl->outgoing_server_size_limit;
+       account->wifi_auto_download                                = account_tbl->wifi_auto_download;
+       account->user_name                                         = EM_SAFE_STRDUP(account_tbl->user_name);
 
        EM_DEBUG_FUNC_END();
        return ret;
@@ -331,6 +347,8 @@ INTERNAL_FUNC int em_convert_mail_tbl_to_mail_data(emstorage_mail_tbl_t *mail_ta
                        }
                }
                memcpy(temp_mail_data[i].eas_data, mail_table_data[i].eas_data, mail_table_data[i].eas_data_length);
+               temp_mail_data[i].pgp_password            = EM_SAFE_STRDUP(mail_table_data[i].pgp_password);
+               temp_mail_data[i].user_name               = EM_SAFE_STRDUP(mail_table_data[i].user_name);
        }
 
        *mail_data = temp_mail_data;
@@ -437,6 +455,8 @@ INTERNAL_FUNC int   em_convert_mail_data_to_mail_tbl(email_mail_data_t *mail_dat
                        }
                }
                memcpy(temp_mail_tbl[i].eas_data, mail_data[i].eas_data, mail_data[i].eas_data_length);
+               temp_mail_tbl[i].pgp_password             = EM_SAFE_STRDUP(mail_data[i].pgp_password);
+               temp_mail_tbl[i].user_name                = EM_SAFE_STRDUP(mail_data[i].user_name);
        }
 
        *mail_table_data = temp_mail_tbl;
@@ -660,8 +680,8 @@ static int fetch_string_from_stream(char *input_stream, int *input_output_stream
        return EMAIL_ERROR_NONE;
 }
                                     /* divide struct at binary field (void* user_data)*/
-#define EMAIL_ACCOUNT_FMT   "S(" "isiii" "isi" ")" "B" "S(" "issss"  "isiss" "iiiii" "iiiii" "isiss" "iii"\
-                                 "$(" "iiiii" "iisii" "iisi" ")" "iiiiisii" ")"
+#define EMAIL_ACCOUNT_FMT   "S(" "isiii" "isii" ")" "B" "S(" "issss"  "isiss" "iiiii" "iiiii" "isiss" "iii"\
+                                 "$(" "iiiii" "iisii" "iisi" "iiiis" ")" "iiiiiisiis" ")"
 
 
 INTERNAL_FUNC char* em_convert_account_to_byte_stream(email_account_t* account, int *stream_len)
@@ -718,7 +738,7 @@ INTERNAL_FUNC void em_convert_byte_stream_to_account(char *stream, int stream_le
 }
 
 #define EMAIL_MAIL_DATA_FMT  "S(" "iiiis" "iisss" "issss" "sssss" "sisss"\
-                            "icccc" "cccii" "iiiii" "iisii" "iiiii" "iii)B"
+                            "icccc" "cccii" "iiiii" "iisii" "iiiii" "iissi" ")B"
 
 INTERNAL_FUNC char* em_convert_mail_data_to_byte_stream(email_mail_data_t *mail_data, int *stream_len)
 {
@@ -784,17 +804,12 @@ INTERNAL_FUNC void em_convert_byte_stream_to_mail_data(char *stream, int stream_
 }
 
 
-#define EMAIL_ATTACHMENT_DATA_FMT "A(S(" "issii" "iciii" "s" "))"
+#define EMAIL_ATTACHMENT_DATA_FMT "A(S(" "isssi" "iicii" "is" "))"
 
 INTERNAL_FUNC char* em_convert_attachment_data_to_byte_stream(email_attachment_data_t *attachment, int attachment_count, int* stream_len)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
-       if(!attachment) {
-               EM_DEBUG_LOG("no attachment to be included");
-               *stream_len = 0;
-               return NULL;
-       }
 
        email_attachment_data_t cur = {0};
        tpl_node *tn = NULL;
@@ -806,8 +821,10 @@ INTERNAL_FUNC char* em_convert_attachment_data_to_byte_stream(email_attachment_d
                EM_DEBUG_EXCEPTION("tpl_map failed");
                return NULL;
        }
+       
+       /* if attachment_count is zero, for loop is skipped */
        int i=0;
-       for( ; i < attachment_count ; i++ ) {
+       for( ; (i < attachment_count) && (attachment+i) ; i++ ) {
                memcpy(&cur, attachment + i, sizeof(email_attachment_data_t));
                tpl_pack(tn, 1);                       
        }
@@ -846,36 +863,31 @@ INTERNAL_FUNC void em_convert_byte_stream_to_attachment_data(char *stream, int s
        /* tpl does not return the size of variable-length array, but we need variable-length array */
        /* so, make list and get list count in the first phase, */
        /* and then copy list to var array after allocating memory */
-       GList *head = NULL;
-       int count = 0;
+
+       int num_element = tpl_Alen (tn, 1);
+       email_attachment_data_t *attached = NULL;
+       if (num_element > 0)
+               attached = (email_attachment_data_t*) em_malloc(sizeof(email_attachment_data_t)*num_element);
+
+       int i = 0;
        while( tpl_unpack(tn, 1) > 0) {
-               email_attachment_data_t* pdata = (email_attachment_data_t*) em_malloc(sizeof(email_attachment_data_t));
+               if (!(attached+i)) {
+                       EM_DEBUG_EXCEPTION ("num element mismatched [%d] vs [%d]", num_element, i);
+                       num_element = i;
+                       break;
+               }
+               email_attachment_data_t* pdata = attached+i;
                memcpy(pdata, &cur, sizeof(email_attachment_data_t)); /* copy unpacked data to list item */
                pdata->attachment_name          = EM_SAFE_STRDUP(cur.attachment_name);
                pdata->attachment_path          = EM_SAFE_STRDUP(cur.attachment_path);
+               pdata->content_id               = EM_SAFE_STRDUP(cur.content_id);
                pdata->attachment_mime_type     = EM_SAFE_STRDUP(cur.attachment_mime_type);
-               head = g_list_prepend(head, pdata);                   /* add it to list */
                memset(&cur, 0, sizeof(email_attachment_data_t));    /* initialize variable, used for unpacking */
-               count++;
+               i++;
        }
        tpl_free(tn);
 
-       /*finally we get the list count and allocate var length array */
-       email_attachment_data_t *attached = (email_attachment_data_t*) em_malloc(sizeof(email_attachment_data_t)*count);
-
-       /*write glist item into variable array*/
-       head = g_list_reverse(head);
-       GList *p = g_list_first(head);
-       int i=0;
-       for( ; p ; p = g_list_next(p), i++ ) {
-               email_attachment_data_t* pdata = (email_attachment_data_t*) g_list_nth_data(p, 0);
-               memcpy( attached+i, pdata, sizeof(email_attachment_data_t));
-               EM_SAFE_FREE(pdata);    /*now, list item is useless */
-       }
-
-       g_list_free(head);
-
-       *attachment_count = count;
+       *attachment_count = num_element;
        *attachment_data = attached;
        EM_DEBUG_FUNC_END();
 }
@@ -1155,38 +1167,45 @@ INTERNAL_FUNC char* em_convert_search_filter_to_byte_stream(email_search_filter_
        result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_count), sizeof(int));
 
        for( i = 0; i < input_search_filter_count; i++) {
-               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list->search_filter_type), sizeof(int));
-               switch(input_search_filter_list->search_filter_type) {
+               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_type), sizeof(int));
+               switch(input_search_filter_list[i].search_filter_type) {
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
                        case EMAIL_SEARCH_FILTER_TYPE_UID              :
+                       case EMAIL_SEARCH_FILTER_TYPE_ALL              :
                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
+                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
+                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
-                               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list->search_filter_key_value.integer_type_key_value), sizeof(int));
+                       case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  :
+                               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_key_value.integer_type_key_value), sizeof(int));
                                break;
 
                        case EMAIL_SEARCH_FILTER_TYPE_BCC              :
+                       case EMAIL_SEARCH_FILTER_TYPE_BODY             :
                        case EMAIL_SEARCH_FILTER_TYPE_CC               :
                        case EMAIL_SEARCH_FILTER_TYPE_FROM             :
                        case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
+                       case EMAIL_SEARCH_FILTER_TYPE_TEXT             :
                        case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
                        case EMAIL_SEARCH_FILTER_TYPE_TO               :
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
-                               result_stream = append_string_to_stream(result_stream, &stream_size, input_search_filter_list->search_filter_key_value.string_type_key_value);
+                       case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
+                               result_stream = append_string_to_stream(result_stream, &stream_size, input_search_filter_list[i].search_filter_key_value.string_type_key_value);
                                break;
 
                        case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
                        case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
                        case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  :
-                               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list->search_filter_key_value.time_type_key_value), sizeof(struct tm));
+                               result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_key_value.time_type_key_value), sizeof(time_t));
                                break;
                        default :
-                               EM_DEBUG_EXCEPTION("Invalid filter type [%d]", input_search_filter_list->search_filter_type);
+                               EM_DEBUG_EXCEPTION("Invalid filter type [%d]", input_search_filter_list[i].search_filter_type);
                                break;
                }
        }
@@ -1232,24 +1251,31 @@ INTERNAL_FUNC void em_convert_byte_stream_to_search_filter(char *input_stream,
                switch(local_search_filter[i].search_filter_type) {
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
                        case EMAIL_SEARCH_FILTER_TYPE_UID              :
+                       case EMAIL_SEARCH_FILTER_TYPE_ALL              :
                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
+                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
+                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
+                       case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  :
                                fetch_sized_data_from_stream(input_stream, &stream_offset, sizeof(int), (char*)&(local_search_filter[i].search_filter_key_value.integer_type_key_value));
                                break;
 
                        case EMAIL_SEARCH_FILTER_TYPE_BCC              :
+                       case EMAIL_SEARCH_FILTER_TYPE_BODY             :
                        case EMAIL_SEARCH_FILTER_TYPE_CC               :
                        case EMAIL_SEARCH_FILTER_TYPE_FROM             :
                        case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
+                       case EMAIL_SEARCH_FILTER_TYPE_TEXT             :
                        case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
                        case EMAIL_SEARCH_FILTER_TYPE_TO               :
                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
+                       case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
                                fetch_string_from_stream(input_stream, &stream_offset, &(local_search_filter[i].search_filter_key_value.string_type_key_value));
                                break;
 
@@ -1316,8 +1342,6 @@ INTERNAL_FUNC void em_convert_byte_stream_to_task_information(char *input_stream
        email_task_information_t cur = {0};
        tpl_node *tn = NULL;
        int i = 0;
-       int count = 0;
-       GList *head = NULL;
 
        tn = tpl_map(EMAIL_JOB_INFORMATION_FMT, &cur);
        if (!tn) {
@@ -1325,30 +1349,26 @@ INTERNAL_FUNC void em_convert_byte_stream_to_task_information(char *input_stream
                return;
        }
        tpl_load(tn, TPL_MEM, input_stream, input_stream_len);
+       int num_element = tpl_Alen (tn, 1);
+       email_task_information_t* deserialized = NULL;
+       if (num_element > 0)
+               deserialized = (email_task_information_t*) em_malloc(sizeof(email_task_information_t)*num_element);
 
        while( tpl_unpack(tn, 1) > 0) {
-               email_task_information_t* pdata = (email_task_information_t*) em_malloc(sizeof(email_task_information_t));
+               if (!(deserialized+i)) {
+                       EM_DEBUG_EXCEPTION ("num element mismatched [%d] vs [%d]", num_element, i);
+                       num_element = i;
+                       break;
+               }
+               email_task_information_t* pdata = deserialized+i;
                memcpy(pdata, &cur, sizeof(email_task_information_t));
-               head = g_list_prepend(head, pdata);
                memset(&cur, 0, sizeof(email_task_information_t));
-               count++;
+               i++;
        }
        tpl_free(tn);
 
-       email_task_information_t *deserialized = (email_task_information_t*) em_malloc(sizeof(email_task_information_t)*count);
-
-       head = g_list_reverse(head);
-       GList *p = g_list_first(head);
 
-       for( ; p ; p = g_list_next(p), i++ ) {
-               email_task_information_t* pdata = (email_task_information_t*) g_list_nth_data(p, 0);
-               memcpy( deserialized+i, pdata, sizeof(email_task_information_t));
-               EM_SAFE_FREE(pdata);
-       }
-
-       g_list_free(head);
-
-       *output_task_information_count = count;
+       *output_task_information_count = num_element;
        *output_task_information       = deserialized;
 
        EM_DEBUG_FUNC_END();
@@ -1359,7 +1379,7 @@ INTERNAL_FUNC int em_convert_certificate_tbl_to_certificate(emstorage_certificat
        EM_DEBUG_FUNC_BEGIN("certficate_tbl[%p], certificate[%p]", certificate_tbl, certificate);
 
        int err_code = EMAIL_ERROR_NONE;
-       int ret = false;
+
        email_certificate_t *temp_certificate = NULL;
 
        if (!certificate_tbl || !certificate)  {
@@ -1389,7 +1409,6 @@ INTERNAL_FUNC int em_convert_certificate_tbl_to_certificate(emstorage_certificat
 
        *certificate = temp_certificate;
 
-       ret = true;
 FINISH_OFF:
        if (error)
                *error = err_code;
index b5f36f5..511a181 100755 (executable)
@@ -36,7 +36,7 @@
 #include <malloc.h>
 #include <pthread.h>
 #include <regex.h>
-#include <security-server.h>
+#include <locale.h>
 
 #include "c-client.h"
 
@@ -182,6 +182,56 @@ INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
        EM_DEBUG_FUNC_END("ptr[%s]", ptr);
 }
 
+INTERNAL_FUNC void em_skip_whitespace_without_alias(char *addr_str, char **pAddr)
+{
+       EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
+
+       if (!addr_str)
+               return ;
+       char *str = addr_str;
+       char ptr[EM_SAFE_STRLEN(addr_str) + 1];
+       int i = 0, j = 0;
+       char *first_qu = NULL;
+       char *last_qu = NULL;
+       char *first_c = NULL;
+       char *last_c = NULL;
+
+       // find first and last quatation
+       for (i = 0; str[i] != '\0'; i++) {
+               if (!first_qu && str[i] == '\"')
+                       first_qu = str + i;
+               if (str[i] == '\"')
+                       last_qu = str + i;
+       }
+
+       if (!first_qu || !last_qu) {
+               // if there is no qutation
+               for (i = 0; str[i] != NULL_CHAR ; i++) {
+                       if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
+                               ptr[j++] = str[i];
+               }
+       } else {
+               // find first and last character except for space
+               for (first_c = first_qu + 1; *first_c == ' '; first_c++);
+               for (last_c = last_qu - 1; *last_c == ' '; last_c--);
+               for (i = 0; str[i] != '\0'; i++) {
+                       if (str + i <= first_qu || str + i >= last_qu) {
+                               if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
+                                       ptr[j++] = str[i];
+                       } else if (first_qu < str + i && str + i < last_qu) {
+                               if (str + i < first_c || str + i > last_c)
+                                       continue;
+                               else
+                                       ptr[j++] = str[i];
+                       }
+               }
+       }
+       ptr[j++] = NULL_CHAR;
+
+       *pAddr = EM_SAFE_STRDUP(ptr);
+       EM_DEBUG_FUNC_END("ptr[%s]", ptr);
+}
+
 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
 {
        EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
@@ -299,6 +349,83 @@ INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, cha
        return result_buffer;
 }
 
+INTERNAL_FUNC int em_replace_string_ex(char **input_source_string, char *input_old_string, char *input_new_string)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int   err = 0;
+       int   buffer_length = 0;
+       int   old_string_length = 0;
+       int   new_string_length = 0;
+       int   match_count = 0;
+
+       char *cursor_of_source_string = NULL;
+       char *cursor_of_result_buffer = NULL;
+       char *result_buffer = NULL;
+       char *source_string = NULL;
+       char *found_pos = NULL;
+
+       EM_IF_NULL_RETURN_VALUE(input_source_string, EMAIL_ERROR_INVALID_PARAM);
+       EM_IF_NULL_RETURN_VALUE(input_old_string, EMAIL_ERROR_INVALID_PARAM);
+       EM_IF_NULL_RETURN_VALUE(input_new_string, EMAIL_ERROR_INVALID_PARAM);
+
+       source_string = *input_source_string;
+
+       found_pos = strstr(source_string, input_old_string);
+
+       if (found_pos == NULL) {
+               err = EMAIL_ERROR_DATA_NOT_FOUND;
+               goto FINISH_OFF;
+       }
+
+       old_string_length = EM_SAFE_STRLEN(input_old_string);
+       new_string_length = EM_SAFE_STRLEN(input_new_string);
+
+       while (found_pos) {
+               match_count++;
+               found_pos++;
+               found_pos = strstr(found_pos, input_old_string);
+       }
+
+       buffer_length  = EM_SAFE_STRLEN(source_string) + ((new_string_length - old_string_length) * match_count) + 50;
+
+       result_buffer  = (char*)malloc(buffer_length);
+
+       if (!result_buffer) {
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+       memset(result_buffer, 0 , buffer_length);
+
+       cursor_of_source_string = source_string;
+       cursor_of_result_buffer = result_buffer;
+       found_pos = strstr(source_string, input_old_string);
+
+       while (found_pos) {
+               memcpy(cursor_of_result_buffer, cursor_of_source_string, found_pos - cursor_of_source_string);
+
+               cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
+               cursor_of_source_string = found_pos + old_string_length;
+
+               memcpy(cursor_of_result_buffer, input_new_string, new_string_length);
+
+               cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
+
+               found_pos++;
+               found_pos = strstr(found_pos, input_old_string);
+       }
+
+       EM_SAFE_STRCAT(result_buffer, cursor_of_source_string);
+
+       EM_SAFE_FREE(*input_source_string);
+       *input_source_string = result_buffer;
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err[%d]", err);
+       return err;
+}
+
+
 /* Memory clean up */
 #include <sys/mman.h>
 
@@ -410,7 +537,7 @@ INTERNAL_FUNC int em_get_file_name_and_extension_from_file_path(char *input_sour
        char  file_name_string[MAX_PATH] = { 0, };
        char  extension_string[MAX_PATH] = { 0, };
 
-       if (!input_source_file_path || !output_file_name || !output_extension) {
+       if (!input_source_file_path || !output_file_name || !output_extension || EM_SAFE_STRLEN(input_source_file_path) <= 0) {
                EM_DEBUG_EXCEPTION("Invalid Parameter");
                err  = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
@@ -467,10 +594,12 @@ INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int
 
        pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
 
-       while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
+       while(pos_on_string >= 0 && source_file_path[pos_on_string] != '.') {
+               pos_on_string--;
+       }
 
-       if(pos_on_string > 0)
-               extension = source_file_path + pos_on_string + 2;
+       if (pos_on_string >= 0 && pos_on_string < EM_SAFE_STRLEN(source_file_path) - 1)
+               extension = source_file_path + pos_on_string + 1;
 
        EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
 
@@ -481,6 +610,61 @@ FINISH_OFF:
        return extension;
 }
 
+INTERNAL_FUNC char *em_shrink_filename(char *fname, int size_limit)
+{
+       EM_DEBUG_FUNC_BEGIN("fname[%s], size_limit[%d]", fname, size_limit);
+
+       char *modified_name = NULL;
+       char *extension = NULL;
+
+       modified_name = em_malloc(sizeof(char)*size_limit);
+       if (!modified_name) {
+               return NULL;
+       }
+
+       extension = em_get_extension_from_file_path(fname, NULL);
+
+       if (extension && EM_SAFE_STRLEN(extension) > 0) {
+               int ext_len = EM_SAFE_STRLEN(extension);
+               int name_len = EM_SAFE_STRLEN(fname) - EM_SAFE_STRLEN(extension) - 1;
+               int name_strip_len = size_limit - EM_SAFE_STRLEN(extension) - 2;
+
+               char *tmp_ext = NULL;
+               char *tmp_name = NULL;
+               char *tmp_name_strip = NULL;
+               tmp_ext = em_malloc(sizeof(char)*(ext_len+1));
+               tmp_name = em_malloc(sizeof(char)*(name_len+1));
+               tmp_name_strip = em_malloc(sizeof(char)*name_strip_len);
+
+               snprintf(tmp_ext, sizeof(char)*(ext_len+1), "%s", extension);
+               snprintf(tmp_name, sizeof(char)*(name_len+1), "%s", fname);
+
+               EM_DEBUG_LOG(">>>>> extention [%s]", tmp_ext);
+               EM_DEBUG_LOG(">>>>> name [%s]", tmp_name);
+
+               if (EM_SAFE_STRLEN(extension) > EM_SAFE_STRLEN(fname) - EM_SAFE_STRLEN(extension)) {
+                       snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
+               } else {
+                       if (tmp_name_strip && name_strip_len > 1) {
+                               snprintf(tmp_name_strip, sizeof(char)*name_strip_len, "%s", tmp_name);
+                               snprintf(modified_name, sizeof(char)*size_limit, "%s.%s", tmp_name_strip, tmp_ext);
+                       } else {
+                               snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
+                       }
+               }
+
+               EM_SAFE_FREE(tmp_ext);
+               EM_SAFE_FREE(tmp_name);
+               EM_SAFE_FREE(tmp_name_strip);
+       } else {
+               snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
+       }
+
+       EM_DEBUG_FUNC_END();
+
+       return modified_name;
+}
+
 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
 {
        EM_DEBUG_FUNC_BEGIN("input_file_path[%d], output_encoding_type[%p]", input_file_path, output_encoding_type);
@@ -489,7 +673,6 @@ INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_pat
        int   pos_of_dot = 0;
        int   enf_of_string = 0;
        int   result_string_length = 0;
-       char *filename = NULL;
        char *result_encoding_type = NULL;
 
        if (!input_file_path || !output_encoding_type) {
@@ -500,7 +683,7 @@ INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_pat
 
        enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
 
-       while(pos_of_filename >= 0 && input_file_path[pos_of_filename--] != '/') {
+       while(pos_of_filename > 0 && input_file_path[pos_of_filename--] != '/') {
                if(input_file_path[pos_of_filename] == '.')
                        pos_of_dot = pos_of_filename;
        }
@@ -508,16 +691,12 @@ INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_pat
        if(pos_of_filename != 0)
                pos_of_filename += 2;
 
-       filename = (char*)input_file_path + pos_of_filename;
-
        if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
                result_string_length = pos_of_dot - pos_of_filename;
        else
                result_string_length = enf_of_string - pos_of_filename;
 
-       EM_DEBUG_LOG("pos_of_dot [%d], pos_of_filename [%d], enf_of_string[%d],result_string_length [%d]", pos_of_dot, pos_of_filename, enf_of_string, result_string_length);
-
-       if( !(result_encoding_type =    em_malloc(sizeof(char) * (result_string_length + 1))) ) {
+       if( !(result_encoding_type = em_malloc(sizeof(char) * (result_string_length + 1))) ) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
                err  = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
@@ -525,7 +704,7 @@ INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_pat
 
        memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
 
-       EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
+       EM_DEBUG_LOG("result_encoding_type [%s]", result_encoding_type);
 
        *output_encoding_type = result_encoding_type;
 
@@ -538,7 +717,7 @@ INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extensio
 {
        EM_DEBUG_FUNC_BEGIN("extension_string[%s]", extension_string);
        int i = 0, err = EMAIL_ERROR_NONE, result_content_type = TYPEAPPLICATION;
-       char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , "p7s", "p7m", NULL};
+       char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , "p7s", "p7m", "asc", NULL};
 
        if (!extension_string) {
                EM_DEBUG_EXCEPTION("Invalid Parameter");
@@ -572,6 +751,9 @@ INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extensio
        case EXTENSION_P7M:
                result_content_type = TYPEPKCS7_MIME;
                break;
+       case EXTENSION_ASC:
+               result_content_type = TYPEPGP;
+               break;
        default:
                break;
        }
@@ -583,20 +765,28 @@ FINISH_OFF:
        return result_content_type;
 }
 
-#define EMAIL_ACCOUNT_RGEX                     "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
-#define EMAIL_DOMAIN_RGEX                      "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
+#define EMAIL_ATOM                             "([^]()<>@,;:\\\".[\x20\x01-\x1f\x7f])+"  // x20: space,
+#define EMAIL_QTEXT                            "[^\"\\\x0d]" /* " \ CR */
+#define EMAIL_DTEXT                            "[^][\\\x0d]" /* [ ] \ CR */
+#define EMAIL_QUOTED_PAIR                      "([\\].)" // first char :\ second char : anything (.)
+#define EMAIL_QUOTED_STRING                    "[\"](" EMAIL_QTEXT "|" EMAIL_QUOTED_PAIR ")*[\"]"
+#define EMAIL_WORD                             "(" EMAIL_ATOM "|" EMAIL_QUOTED_STRING ")"
+#define EMAIL_PHRASE                           "(" EMAIL_ATOM "|" EMAIL_QUOTED_STRING ")"
+
+#define EMAIL_DOMAIN_LITERAL                   "\\[(" EMAIL_DTEXT "|" EMAIL_QUOTED_PAIR ")*\\]" /* literal match for "[" and "]"*/
+#define EMAIL_SUB_DOMAIN                       "(" EMAIL_ATOM "|" EMAIL_DOMAIN_LITERAL ")"
+
+#define EMAIL_LOCAL_PART                       "(" EMAIL_WORD "(\\." EMAIL_WORD ")*)"
+#define EMAIL_DOMAIN                           "(" EMAIL_SUB_DOMAIN "(\\." EMAIL_SUB_DOMAIN ")*)"
+#define EMAIL_ADDR_SPEC                        "(" EMAIL_LOCAL_PART "@" EMAIL_DOMAIN ")"
 
-#define EMAIL_ADDR_RGEX                        "[[:space:]]*<"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX">[[:space:]]*"
-#define EMAIL_ALIAS_RGEX                       "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_RGEX
-#define EMAIL_ALIAS_LIST_RGEX                  "^("EMAIL_ALIAS_RGEX"[;,])*"EMAIL_ALIAS_RGEX"[;,]?[[:space:]]*$"
+#define EMAIL_MAILBOX                          "("EMAIL_ADDR_SPEC "|" EMAIL_PHRASE "[[:space:]]*" "<" EMAIL_ADDR_SPEC ">|<" EMAIL_ADDR_SPEC ">)"
+#define EMAIL_ADDRESS                          "^([:blank:]*" EMAIL_MAILBOX "[,;[:blank:]]*([,;][,;[:blank:]]*" EMAIL_MAILBOX "[,;[:blank:]]*)*)$"
 
-#define EMAIL_ADDR_WITHOUT_BRACKET_RGEX        "[[:space:]]*"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX"[[:space:]]*"
-#define EMAIL_ALIAS_WITHOUT_BRACKET_RGEX       "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_WITHOUT_BRACKET_RGEX
-#define EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX  "("EMAIL_ALIAS_WITHOUT_BRACKET_RGEX"[;,])*"EMAIL_ADDR_WITHOUT_BRACKET_RGEX"[;,]?[[:space:]]*$"
 
-INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket)
+static int em_verify_email_address_by_using_regex(char *address)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("address[%s] without_bracket[%d]", address, without_bracket);
+       EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
 
        /*  this following code verfies the email alias string using reg. exp. */
        regex_t alias_list_regex = {0};
@@ -610,10 +800,7 @@ INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket)
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if(without_bracket)
-               reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
-       else
-               reg_rule = EMAIL_ALIAS_LIST_RGEX;
+       reg_rule = EMAIL_ADDRESS;
 
        if (regcomp (&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED) != 0) {
                EM_DEBUG_EXCEPTION("email alias regex unrecognized");
@@ -642,16 +829,152 @@ FINISH_OFF:
        return error;
 }
 
-INTERNAL_FUNC int em_verify_email_address_of_mail_data (email_mail_data_t *mail_data, int without_bracket)
+static int em_verify_email_address_without_regex(char *address)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
+       char *local_address = NULL;
+       char *address_start = NULL;
+       char *cur = NULL;
+       char *local_part = NULL;
+       char *domain = NULL;
+       char *saveptr = NULL;
+       char currunt_char;
+       int address_length = 0;
+       int i = 0;
+       int error = EMAIL_ERROR_NONE;
+       int occur = 0;
+
+       EM_DEBUG_LOG_SEC("address [%s]", address);
+
+        if (address == NULL) {
+            EM_DEBUG_EXCEPTION("Invalid parameter");
+            error = EMAIL_ERROR_INVALID_PARAM;
+            return error;
+        }
+
+       local_address = strdup(address);
+
+       address_start = local_address;
+
+       while ((cur = strchr(address_start, '\"'))) {
+               address_start = cur + 1;
+               if (local_address >= cur - 1 || *(cur - 1) != '\\')
+                       occur++;
+       }
+
+       if (occur % 2) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+               goto FINISH_OFF;
+       }
+
+       if ((cur = strchr(address_start, '<'))) {
+               char *close_pos = NULL;
+
+               address_start = cur + 1;
+               close_pos = address_start;
+
+               while ((cur = strchr(close_pos, '>'))) {
+                       close_pos = cur + 1;
+               }
+
+               if (address_start == close_pos) {
+                       error = EMAIL_ERROR_INVALID_ADDRESS;
+                       EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+                       goto FINISH_OFF;
+               }
+
+               address_start[close_pos - 1 - address_start] = '\0';
+       }
+
+       EM_DEBUG_LOG_SEC("address_start [%s]", address_start);
+
+       address_length = EM_SAFE_STRLEN(address_start);
+
+       for (i = 0; i < address_length; i++) {
+               currunt_char = address_start[i];
+               if (!isalpha(currunt_char) && !isdigit(currunt_char) && currunt_char != '_' && currunt_char != '.' && currunt_char != '@') {
+                       error = EMAIL_ERROR_INVALID_ADDRESS;
+                       EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (strstr(address_start, "..") || strstr(address_start, ".@") || strstr(address_start, "@.") || strstr(address_start, "._.")) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+               goto FINISH_OFF;
+       }
+
+       local_part = strtok_r(address_start, "@", &saveptr);
+
+       EM_DEBUG_LOG("local_part [%s]", local_part);
+
+       if (local_part == NULL || EM_SAFE_STRLEN(local_part) == 0) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               goto FINISH_OFF;
+       }
+
+       domain = strtok_r(NULL, "@", &saveptr);
+
+       EM_DEBUG_LOG("domain [%s]", domain);
+
+       if (domain == NULL || EM_SAFE_STRLEN(domain) < 3) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+               goto FINISH_OFF;
+       }
+
+       if (strchr(domain, '.') == NULL) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+               goto FINISH_OFF;
+       }
+
+       if (!isalpha(local_part[0])) {
+               error = EMAIL_ERROR_INVALID_ADDRESS;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+       EM_SAFE_FREE(local_address);
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+
+INTERNAL_FUNC int em_verify_email_address(char *address)
 {
-       EM_DEBUG_FUNC_BEGIN("mail_data[%p] without_bracket[%d]", mail_data, without_bracket);
+       EM_DEBUG_FUNC_BEGIN("address[%p]", address);
+       int error = EMAIL_ERROR_NONE;
+       char *result_locale = NULL;
+
+       setlocale(LC_ALL, "");
+
+       result_locale = setlocale(LC_ALL, NULL);
+
+       EM_DEBUG_LOG("LC_ALL[%s]" , result_locale);
+
+       if ( EM_SAFE_STRCMP(result_locale, "or_IN.UTF-8") == 0)
+               error = em_verify_email_address_without_regex(address);
+       else
+               error = em_verify_email_address_by_using_regex(address);
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+
+INTERNAL_FUNC int em_verify_email_address_of_mail_data (email_mail_data_t *mail_data)
+{
+       EM_DEBUG_FUNC_BEGIN("mail_data[%p]", mail_data);
        char *address_array[4] = { mail_data->full_address_from, mail_data->full_address_to, mail_data->full_address_cc, mail_data->full_address_bcc};
        int  err = EMAIL_ERROR_NONE, i;
 
        /* check for email_address validation */
        for (i = 0; i < 4; i++) {
                if (address_array[i] && address_array[i][0] != 0) {
-                       err = em_verify_email_address (address_array[i] , without_bracket);
+                       err = em_verify_email_address (address_array[i]);
                        if (err != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION_SEC("em_verify_email_address error[%d] idx[%d] addr[%s]", err, i, address_array[i]);
                                goto FINISH_OFF;
@@ -663,16 +986,16 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
+INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl)
 {
-       EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p] input_without_bracket[%d]", input_mail_tbl, input_without_bracket);
+       EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p]", input_mail_tbl);
        char *address_array[4] = { input_mail_tbl->full_address_to, input_mail_tbl->full_address_cc, input_mail_tbl->full_address_bcc, input_mail_tbl->full_address_from};
        int  err = EMAIL_ERROR_NONE, i;
 
        /* check for email_address validation */
        for (i = 0; i < 4; i++) {
                if (address_array[i] && address_array[i][0] != 0) {
-                       if ((err = em_verify_email_address (address_array[i] , input_without_bracket)) != EMAIL_ERROR_NONE) {
+                       if ((err = em_verify_email_address (address_array[i])) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION_SEC("em_verify_email_address error[%d] idx[%d] addr[%s]", err, i, address_array[i]);
                                goto FINISH_OFF;
                        }
@@ -703,7 +1026,7 @@ INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
        }
 
        em_upper_string(copy_of_subject);
-       EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
+       EM_DEBUG_LOG_SEC("em_upper_string result : %s\n", copy_of_subject);
 
        if (strstr(copy_of_subject, "RE:") == NULL) {
                if (strstr(copy_of_subject, "FWD:") == NULL) {
@@ -744,21 +1067,21 @@ INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, ch
        em_upper_string(copy_of_subject);
        curpos = copy_of_subject;
 
-       EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
+
 
        while ((result = strstr(curpos, "RE:")) != NULL) {
                curpos = result + 3;
-               EM_DEBUG_LOG("RE result : %s", curpos);
+               EM_DEBUG_LOG_SEC("RE result : %s", curpos);
        }
 
        while ((result = strstr(curpos, "FWD:")) != NULL) {
                curpos = result + 4;
-               EM_DEBUG_LOG("FWD result : %s", curpos);
+               EM_DEBUG_LOG_SEC("FWD result : %s", curpos);
        }
 
        while ((result = strstr(curpos, "FW:")) != NULL) {
                curpos = result + 3;
-               EM_DEBUG_LOG("FW result : %s", curpos);
+               EM_DEBUG_LOG_SEC("FW result : %s", curpos);
        }
 
        while (curpos != NULL && *curpos == ' ') {
@@ -772,9 +1095,6 @@ INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, ch
 FINISH_OFF:
        EM_SAFE_FREE(copy_of_subject);
 
-       if (error_code == EMAIL_ERROR_NONE && stripped_subject)
-               EM_DEBUG_LOG("result[%s]", stripped_subject);
-
        EM_DEBUG_FUNC_END("error_code[%d]", error_code);
        return error_code;
 }
@@ -841,7 +1161,7 @@ INTERNAL_FUNC int em_decode_base64(unsigned char *enc_text, unsigned long enc_le
     return ret;
 }
 
-INTERNAL_FUNC int em_get_account_server_type_by_account_id(int account_id, email_account_server_t* account_server_type, int flag, int *error)
+INTERNAL_FUNC int em_get_account_server_type_by_account_id(char *multi_user_name, int account_id, email_account_server_t* account_server_type, int flag, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
        emstorage_account_tbl_t *account_tbl_data = NULL;
@@ -855,7 +1175,7 @@ INTERNAL_FUNC int em_get_account_server_type_by_account_id(int account_id, email
                goto FINISH_OFF;
        }
 
-       if( !emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
+       if( !emstorage_get_account_by_id(multi_user_name, account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
                EM_DEBUG_EXCEPTION ("emstorage_get_account_by_id failed [%d] ", err);
                ret = false;
                goto FINISH_OFF;
@@ -933,42 +1253,11 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int em_check_socket_privilege_by_pid(int pid)
-{
-       EM_DEBUG_FUNC_BEGIN("pid [%d]", pid);
-       int smack_ret = 0;
-
-       smack_ret = security_server_check_privilege_by_pid(pid, "email-service::write", "rw");
-       if (smack_ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
-               EM_DEBUG_EXCEPTION("SECURITY_SERVER_API_ERROR_ACCESS_DENIED");
-               return EMAIL_ERROR_PERMISSION_DENIED;
-       }
-
-       EM_DEBUG_FUNC_END();
-       return EMAIL_ERROR_NONE;
-}
-
-INTERNAL_FUNC int em_check_db_privilege_by_pid(int pid)
-{
-       EM_DEBUG_FUNC_BEGIN("pid [%d]", pid);
-       int smack_ret = 0;
-
-       smack_ret = security_server_check_privilege_by_pid(pid, "email-service::db", "rw");
-       if (smack_ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
-               EM_DEBUG_EXCEPTION("SECURITY_SERVER_API_ERROR_ACCESS_DENIED");
-               return EMAIL_ERROR_PERMISSION_DENIED;
-       }
-
-       EM_DEBUG_FUNC_END();
-       return EMAIL_ERROR_NONE;
-}
-
-
 /* thread with task queue generic functions */
 
 pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
 
-email_thread_handle_t* em_thread_create (void *(*thread_exit)(void*), void *arg)
+email_thread_handle_t* em_thread_create(void *(*thread_exit)(void*), void *arg)
 {
        pthread_mutex_lock(&g_mu);
 
@@ -1049,7 +1338,7 @@ static void* worker_func (void* arg)
        do {
                /* running thread main function */
                if (warg->thread_func)
-                       (warg->thread_func) (warg->arg); 
+                       (warg->thread_func) (warg->arg);
                if (warg->destroy)
                        (warg->destroy) (warg->arg);
                EM_SAFE_FREE (warg);
@@ -1082,6 +1371,7 @@ void em_thread_run (email_thread_handle_t *thd_handle, void *(*thread_func)(void
                return;
        }
        worker_handle->thread_func = thread_func;
+       worker_handle->destroy = destroy;
        worker_handle->arg  = arg;
        worker_handle->thd_handle = thd_handle;
 
@@ -1089,7 +1379,7 @@ void em_thread_run (email_thread_handle_t *thd_handle, void *(*thread_func)(void
 
        /* adding task to queue */
        if (thd_handle->running) {
-               g_queue_push_tail (thd_handle->q, worker_handle);               
+               g_queue_push_tail (thd_handle->q, worker_handle);
        }
        else {
                thd_handle->running = 1;
@@ -1110,4 +1400,73 @@ void em_thread_join (email_thread_handle_t *thd_handle)
        }
 }
 
+INTERNAL_FUNC int em_fopen(const char *filename, const char *mode, FILE **fp)
+{
+       EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
+
+       int err = EMAIL_ERROR_NONE;
+
+       if (!filename) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       FILE *temp_fp = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
 
+       temp_fp = fopen(filename, mode);
+       if (temp_fp == NULL) {
+               EM_DEBUG_EXCEPTION("fopen failed : [%s][%d]", EM_STRERROR(errno_buf), errno);
+               if (errno == EACCES || errno == EPERM)
+                       err = EMAIL_ERROR_PERMISSION_DENIED;
+               else if (errno == ENOSPC)
+                       err = EMAIL_ERROR_MAIL_MEMORY_FULL;
+               else
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+       }
+
+       if (fp)
+               *fp = temp_fp;
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC int em_open(const char *filename, int oflags, mode_t mode, int *handle)
+{
+       EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
+       int err = EMAIL_ERROR_NONE;
+
+       if (!filename) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       int temp_handle = -1;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       if (mode)
+               temp_handle = open(filename, oflags, mode);
+       else
+               temp_handle = open(filename, oflags);
+
+       if (temp_handle < 0) {
+               EM_DEBUG_EXCEPTION("open failed : [%s][%d]", EM_STRERROR(errno_buf), errno);
+               if (errno == EACCES || errno == EPERM)
+                       err = EMAIL_ERROR_PERMISSION_DENIED;
+               else if (errno == ENOSPC)
+                       err = EMAIL_ERROR_MAIL_MEMORY_FULL;
+               else
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+       }
+
+       if (handle)
+               *handle = temp_handle;
+       else
+               if (temp_handle >= 0) close(temp_handle);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
index 6d34bf3..759b2de 100755 (executable)
@@ -45,8 +45,9 @@ extern "C"
 #include <errno.h>
 
 #define __FEATURE_DEBUG_LOG__
-#define __FEATURE_LOG_FOR_LIFE_CYCLE_OF_FUNCTION__
-#define __FEATURE_LOG_FOR_DEBUG_LOG_DEV__
+/* #define __FEATURE_LOG_FOR_LIFE_CYCLE_OF_FUNCTION__ */
+/* #define __FEATURE_LOG_FOR_DEBUG_LOG_DEV__ */
+/* #define FEATURE_CORE_DEBUG */
 
 #ifdef  __FEATURE_DEBUG_LOG__
 
@@ -60,19 +61,9 @@ extern "C"
 #define        EM_DEBUG_LOG(format, arg...)        SLOGD(format, ##arg)
 #define        EM_DEBUG_EXCEPTION(format, arg...)  SLOGE("[EXCEPTION!] " format "\n", ##arg)
 
-#ifdef _SECURE_LOG
-#undef _SECURE_LOG
-#endif
-
-#ifdef _SECURE_LOG
 #define        EM_DEBUG_LOG_SEC(format, arg...)        SECURE_SLOGD(format, ##arg)
 #define        EM_DEBUG_EXCEPTION_SEC(format, arg...)  SECURE_SLOGE("[EXCEPTION!] " format "\n", ##arg)
 #define        EM_DEBUG_FUNC_BEGIN_SEC(format, arg...) EM_DEBUG_LOG_SEC("BEGIN - "format, ##arg)
-#else
-#define        EM_DEBUG_LOG_SEC(format, arg...)        SLOGD(format, ##arg)
-#define        EM_DEBUG_EXCEPTION_SEC(format, arg...)  SLOGE("[EXCEPTION!] " format "\n", ##arg)
-#define        EM_DEBUG_FUNC_BEGIN_SEC(format, arg...) EM_DEBUG_LOG("BEGIN - "format, ##arg)
-#endif
 
 #ifdef  _DEBUG_MIME_PARSE_
 #define EM_DEBUG_LOG_MIME(format, arg...)   EM_DEBUG_LOG_SEC(format, ##arg)
@@ -97,7 +88,7 @@ extern "C"
 #endif
 
 #define        EM_DEBUG_LINE                       EM_DEBUG_LOG("FUNC[%s : %d]", __FUNCTION__, __LINE__)
-#define EM_DEBUG_DB_EXEC(eval, expr, X)     if (eval) { EM_DEBUG_LOG X; expr;} else {;}
+#define EM_DEBUG_DB_EXEC(eval, expr, X)     if (eval) { EM_DEBUG_EXCEPTION X; expr;} else {;}
 
 #define EM_DEBUG_ERROR_FILE_PATH            tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/.critical_error.log")
 #define EM_DEBUG_CRITICAL_EXCEPTION(format, arg...)   \
@@ -203,16 +194,16 @@ extern "C"
 #define EM_NULL_CHECK_FOR_VOID(expr)   \
        {\
                if (!expr) {\
-                       EM_DEBUG_EXCEPTION ("INVALID PARAM: "#expr" NULL ");\
+                       EM_DEBUG_LOG ("NULL_PARAM: ["#expr"]");\
                        return;\
                }\
        }
 
-#define EM_IF_NULL_RETURN_VALUE(expr, val) \
+#define EM_IF_NULL_RETURN_VALUE(expr, ret) \
        {\
                if (!expr ) {\
-                       EM_DEBUG_EXCEPTION ("INVALID PARAM: "#expr" NULL ");\
-                       return val;     \
+                       EM_DEBUG_LOG("NULL_PARAM: ["#expr"]");\
+                       return ret;     \
                }\
        }
 
@@ -237,6 +228,17 @@ extern "C"
                }\
        })
 
+#define EM_SAFE_CLOSE(fd)       \
+       ({\
+               int err = 0;\
+               if (fd >=0) {\
+                       err = close (fd);\
+                       if (err < 0)\
+                               EM_DEBUG_EXCEPTION ("close errno [%d]", errno);\
+                       fd = 0;\
+               }\
+       })
+
 #define EM_SAFE_STRDUP(s) \
        ({\
                char* _s = (char*)s;\
@@ -250,6 +252,13 @@ extern "C"
                ((_src) && (_dest))? strcmp(_dest, _src) : -1;\
        })
 
+#define EM_SAFE_STRCASECMP(dest, src) \
+       ({\
+               char* _dest = dest;\
+               char* _src = src;\
+               ((_src) && (_dest))? strcasecmp(_dest, _src) : -1;\
+       })
+
 #define EM_SAFE_STRCPY(dest, src) \
        ({\
                char* _dest = dest;\
index 2a5247e..2f35c69 100755 (executable)
 #ifndef __EMAIL_ERRORS_H__
 #define __EMAIL_ERRORS_H__
 
+#include <tizen_error.h>
+/**
+ * @file email-errors.h
+ */
+
+/**
+ * @internal
+ * @addtogroup EMAIL_SERVICE_FRAMEWORK
+ * @{
+ */
+
 /*****************************************************************************/
 /*  Errors                                                                   */
 /*****************************************************************************/
-#define EMAIL_ERROR_NONE                                 1       /*  There is no error */
+#define EMAIL_ERROR_NONE                                 1       /**<  There is no error */
 
 /* Error codes for invalid input */
-#define EMAIL_ERROR_INVALID_PARAM                       -1001    /*  invalid parameter was given. - Invalid input parameter */
-#define EMAIL_ERROR_INVALID_ACCOUNT                     -1002    /*  invalid account information was given. - Unsupported account */
-#define EMAIL_ERROR_INVALID_SERVER                      -1005    /*  invalid server information was given. - Server unavailable */
-#define EMAIL_ERROR_INVALID_MAIL                        -1006    /*  invalid mail information was given */
-#define EMAIL_ERROR_INVALID_ADDRESS                     -1007    /*  invalid address information was given. - Incorrect address */
-#define EMAIL_ERROR_INVALID_ATTACHMENT                  -1008    /*  invalid attachment information was given */
-#define EMAIL_ERROR_INVALID_MAILBOX                     -1009    /*  invalid mailbox information was given */
-#define EMAIL_ERROR_INVALID_FILTER                      -1010    /*  invalid filter information was given */
-#define EMAIL_ERROR_INVALID_DATA                        -1012    /*  invalid data */
-#define EMAIL_ERROR_INVALID_RESPONSE                    -1013    /*  unexpected network response was given. - Invalid server response */
-#define EMAIL_ERROR_NO_RECIPIENT                        -1062    /*  no recipients information was found */
-#define EMAIL_ERROR_INVALID_FILE_PATH                   -4101    /*  invalid file path was given */
-#define EMAIL_ERROR_INVALID_REFERENCE_MAIL              -4102    /*  invalid reference mail was given */
+#define EMAIL_ERROR_INVALID_PARAM                       -1001    /**<  Invalid parameter was given. - Invalid input parameter */
+#define EMAIL_ERROR_INVALID_ACCOUNT                     -1002    /**<  Invalid account information was given. - Unsupported account */
+#define EMAIL_ERROR_INVALID_SERVER                      -1005    /**<  Invalid server information was given. - Server unavailable */
+#define EMAIL_ERROR_INVALID_MAIL                        -1006    /**<  Invalid mail information was given */
+#define EMAIL_ERROR_INVALID_ADDRESS                     -1007    /**<  Invalid address information was given. - Incorrect address */
+#define EMAIL_ERROR_INVALID_ATTACHMENT                  -1008    /**<  Invalid attachment information was given */
+#define EMAIL_ERROR_INVALID_MAILBOX                     -1009    /**<  Invalid mailbox information was given */
+#define EMAIL_ERROR_INVALID_FILTER                      -1010    /**<  Invalid filter information was given */
+#define EMAIL_ERROR_INVALID_DATA                        -1012    /**<  Invalid data */
+#define EMAIL_ERROR_INVALID_RESPONSE                    -1013    /**<  Unexpected network response was given. - Invalid server response */
+#define EMAIL_ERROR_NO_RECIPIENT                        -1062    /**<  No recipients information was found */
+#define EMAIL_ERROR_INVALID_FILE_PATH                   -4101    /**<  Invalid file path was given */
+#define EMAIL_ERROR_INVALID_REFERENCE_MAIL              -4102    /**<  Invalid reference mail was given */
 
 /* Error codes for missing data */
-#define EMAIL_ERROR_ACCOUNT_NOT_FOUND                   -1014    /*  no matched account was found */
-#define EMAIL_ERROR_MAIL_NOT_FOUND                      -1015    /*  no matched mail was found */
-#define EMAIL_ERROR_MAILBOX_NOT_FOUND                   -1016    /*  no matched mailbox was found */
-#define EMAIL_ERROR_ATTACHMENT_NOT_FOUND                -1017    /*  no matched attachment was found */
-#define EMAIL_ERROR_FILTER_NOT_FOUND                    -1018    /*  no matched filter was found */
-#define EMAIL_ERROR_CONTACT_NOT_FOUND                   -1019    /*  no matched contact was found */
-#define EMAIL_ERROR_FILE_NOT_FOUND                      -1020    /*  no matched file was found */
-#define EMAIL_ERROR_DATA_NOT_FOUND                      -1021    /*  no matched data was found */
-#define EMAIL_ERROR_TASK_BINDER_NOT_FOUND               -1023    /*  no matched task binder was found */
-#define EMAIL_ERROR_TASK_NOT_FOUND                      -1168    /*  no matched task was found */
-#define EMAIL_ERROR_HANDLE_NOT_FOUND                    -1301
-#define EMAIL_ERROR_ALARM_DATA_NOT_FOUND                -1933
+#define EMAIL_ERROR_ACCOUNT_NOT_FOUND                   -1014    /**<  No matched account was found */
+#define EMAIL_ERROR_MAIL_NOT_FOUND                      -1015    /**<  No matched mail was found */
+#define EMAIL_ERROR_MAILBOX_NOT_FOUND                   -1016    /**<  No matched mailbox was found */
+#define EMAIL_ERROR_ATTACHMENT_NOT_FOUND                -1017    /**<  No matched attachment was found */
+#define EMAIL_ERROR_FILTER_NOT_FOUND                    -1018    /**<  No matched filter was found */
+#define EMAIL_ERROR_CONTACT_NOT_FOUND                   -1019    /**<  No matched contact was found */
+#define EMAIL_ERROR_FILE_NOT_FOUND                      -1020    /**<  No matched file was found */
+#define EMAIL_ERROR_DATA_NOT_FOUND                      -1021    /**<  No matched data was found */
+#define EMAIL_ERROR_TASK_BINDER_NOT_FOUND               -1023    /**<  No matched task binder was found */
+#define EMAIL_ERROR_TASK_NOT_FOUND                      -1168    /**<  No matched task was found */
+#define EMAIL_ERROR_HANDLE_NOT_FOUND                    -1169    /**<  No matched handle was found */
+#define EMAIL_ERROR_ALARM_DATA_NOT_FOUND                -1933    /**<  No matched alarm data was found */
 
 /* Error codes for specification for maximum data */
-#define EMAIL_ERROR_NO_MORE_DATA                        -1022    /*  No more data available */
-#define EMAIL_ERROR_MAX_EXCEEDED                        -1024    /*  Can't handle more data */
-#define EMAIL_ERROR_OUT_OF_MEMORY                       -1028    /*  There is not enough memory */
-#define EMAIL_ERROR_ACCOUNT_MAX_COUNT                   -1053    /*  There is too many account */
-#define EMAIL_ERROR_MAIL_MEMORY_FULL                    -1054    /*  There is no more storage */
-#define EMAIL_ERROR_DATA_TOO_LONG                       -1025    /*  Data is too long */
-#define EMAIL_ERROR_MAXIMUM_DEVICES_LIMIT_REACHED       -1530    /*  EAS - Maximum devices limit reached */
+#define EMAIL_ERROR_NO_MORE_DATA                        -1022    /**<  No more data available */
+#define EMAIL_ERROR_MAX_EXCEEDED                        -1024    /**<  Cannot handle more data */
+#define EMAIL_ERROR_OUT_OF_MEMORY                       -1028    /**<  There is not enough memory */
+#define EMAIL_ERROR_ACCOUNT_MAX_COUNT                   -1053    /**<  There is too many account */
+#define EMAIL_ERROR_MAIL_MEMORY_FULL                    -1054    /**<  There is no more storage */
+#define EMAIL_ERROR_DATA_TOO_LONG                       -1025    /**<  Data is too long */
+#define EMAIL_ERROR_MAXIMUM_DEVICES_LIMIT_REACHED       -1530    /**<  EAS - Maximum devices limit reached */
 
 /* Error codes for storage */
-#define EMAIL_ERROR_DB_FAILURE                          -1029    /*  database operation failed */
-#define EMAIL_ERROR_SECURED_STORAGE_FAILURE             -2100    /*  Error from secured storage */
-#define EMAIL_ERROR_GCONF_FAILURE                       -1058    /*  The error occurred on accessing Gconf */
-#define EMAIL_ERROR_FILE                                -1059    /* file related error */
+#define EMAIL_ERROR_DB_FAILURE                          -1029    /**<  Database operation failed */
+#define EMAIL_ERROR_SECURED_STORAGE_FAILURE             -2100    /**<  Error from secured storage */
+#define EMAIL_ERROR_GCONF_FAILURE                       -1058    /**<  The error occurred on accessing Gconf */
+#define EMAIL_ERROR_FILE                                -1059    /**<  File related error */
 
 /* Error codes for network */
-#define EMAIL_ERROR_SOCKET_FAILURE                      -1031    /*  socket operation failed */
-#define EMAIL_ERROR_CONNECTION_FAILURE                  -1032    /*  network connection failed */
-#define EMAIL_ERROR_CONNECTION_BROKEN                   -1033    /*  network connection was broken */
-#define EMAIL_ERROR_NO_SUCH_HOST                        -1059    /*  no such host was found */
-#define EMAIL_ERROR_NETWORK_NOT_AVAILABLE               -1800    /*  WIFI not available*/
+#define EMAIL_ERROR_SOCKET_FAILURE                      -1031    /**<  Socket operation failed */
+#define EMAIL_ERROR_CONNECTION_FAILURE                  -1032    /**<  Network connection failed */
+#define EMAIL_ERROR_CONNECTION_BROKEN                   -1033    /**<  Network connection was broken */
+#define EMAIL_ERROR_NO_SUCH_HOST                        -1802    /**<  No such host was found */
+#define EMAIL_ERROR_NETWORK_NOT_AVAILABLE               -1800    /**<  WIFI not available*/
 #define EMAIL_ERROR_INVALID_STREAM                      -1068
+#define EMAIL_ERROR_FLIGHT_MODE_ENABLE                  -1801    /**<  Flight mode enable : network not available */
 
 /* Error codes for SSL/TLS */
-#define EMAIL_ERROR_STARTLS                             -1401    /*  "STARTLS" */
-#define EMAIL_ERROR_TLS_NOT_SUPPORTED                   -1040    /*  The server doesn't support TLS */
-#define EMAIL_ERROR_TLS_SSL_FAILURE                     -1041    /*  The agent failed TLS/SSL */
-#define EMAIL_ERROR_CANNOT_NEGOTIATE_TLS                -1400    /*  "Cannot negotiate TLS" */
+#define EMAIL_ERROR_STARTLS                             -1401    /**<  "STARTLS" */
+#define EMAIL_ERROR_TLS_NOT_SUPPORTED                   -1040    /**<  The server doesn't support TLS */
+#define EMAIL_ERROR_TLS_SSL_FAILURE                     -1041    /**<  The agent failed TLS/SSL */
+#define EMAIL_ERROR_CANNOT_NEGOTIATE_TLS                -1400    /**<  "Cannot negotiate TLS" */
 
-#define EMAIL_ERROR_NO_RESPONSE                         -1036    /*  There is no server response */
+#define EMAIL_ERROR_NO_RESPONSE                         -1036    /**<  There is no server response */
 
 /* Error codes for authentication */
-#define EMAIL_ERROR_AUTH_NOT_SUPPORTED                  -1038    /*  The server doesn't support authentication */
-#define EMAIL_ERROR_AUTHENTICATE                        -1039    /*  The server failed to authenticate user */
-#define EMAIL_ERROR_AUTH_REQUIRED                       -1069    /*  SMTP Authentication needed */
-#define EMAIL_ERROR_LOGIN_FAILURE                       -1035    /*  login failed */
-#define EMAIL_ERROR_LOGIN_ALLOWED_EVERY_15_MINS         -1600    /*  "login allowed only every 15 minutes" */
-#define EMAIL_ERROR_TOO_MANY_LOGIN_FAILURE              -1601    /*  "Too many login failure" */
-#define EMAIL_ERROR_XOAUTH_BAD_REQUEST                  -1602    /*  "{"status":"400"..." */
-#define EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED         -1603    /*  "{"status":"401"..." */
-#define EMAIL_ERROR_XOAUTH_INVALID_GRANT                -1604    /*  "error" : "invalid_grant" */
+#define EMAIL_ERROR_AUTH_NOT_SUPPORTED                  -1038    /**<  The server does not support authentication */
+#define EMAIL_ERROR_AUTHENTICATE                        -1039    /**<  The server failed to authenticate user */
+#define EMAIL_ERROR_AUTH_REQUIRED                       -1069    /**<  SMTP Authentication needed */
+#define EMAIL_ERROR_LOGIN_FAILURE                       -1035    /**<  Login failed */
+#define EMAIL_ERROR_LOGIN_ALLOWED_EVERY_15_MINS         -1600    /**<  "login allowed only every 15 minutes" */
+#define EMAIL_ERROR_TOO_MANY_LOGIN_FAILURE              -1601    /**<  "Too many login failure" */
+#define EMAIL_ERROR_XOAUTH_BAD_REQUEST                  -1602    /**<  "{"status":"400"..." */
+#define EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED         -1603    /**<  "{"status":"401"..." */
+#define EMAIL_ERROR_XOAUTH_INVALID_GRANT                -1604    /**<  "error" : "invalid_grant" */
 
 
 /* Error codes for functionality */
-#define EMAIL_ERROR_NOT_IMPLEMENTED                     -1047    /*  The function is not implemented yet*/
-#define EMAIL_ERROR_NOT_SUPPORTED                       -1048    /*  The function is not supported */
-#define EMAIL_ERROR_SERVER_NOT_SUPPORT_FUNCTION         -1823
+#define EMAIL_ERROR_NOT_IMPLEMENTED                     -1047    /**<  The function is not implemented yet */
+#define EMAIL_ERROR_NOT_SUPPORTED                       TIZEN_ERROR_PERMISSION_DENIED /**<  The function is not supported */
+#define EMAIL_ERROR_SERVER_NOT_SUPPORT_FUNCTION         -1823    /**<  The function is not supported in server */
 
 /* Error codes for from system */
-#define EMAIL_ERROR_NO_SIM_INSERTED                     -1205
-#define EMAIL_ERROR_BADGE_API_FAILED                    -3004
+#define EMAIL_ERROR_NO_SIM_INSERTED                     -1205    /**<  The SIM card did not insert */
+#define EMAIL_ERROR_BADGE_API_FAILED                    -3004    /**<  The badge API returned false */
 
 /* Error codes for event handling */
-#define EMAIL_ERROR_EVENT_QUEUE_FULL                    -1060    /*  event queue is full */
-#define EMAIL_ERROR_EVENT_QUEUE_EMPTY                   -1061    /*  event queue is empty */
-#define EMAIL_ERROR_SESSION_NOT_FOUND                   -1067    /*  no matched session was found */
+#define EMAIL_ERROR_EVENT_QUEUE_FULL                    -1060    /**<  Event queue is full */
+#define EMAIL_ERROR_EVENT_QUEUE_EMPTY                   -1061    /**<  Event queue is empty */
+#define EMAIL_ERROR_SESSION_NOT_FOUND                   -1067    /**<  No matched session was found */
 #define EMAIL_ERROR_CANNOT_STOP_THREAD                  -2000
-#define EMAIL_ERROR_CANCELLED                           -1046    /*  The job was canceled by user */
-#define EMAIL_NO_AVAILABLE_TASK_SLOT                    -2656    /*  There is no available task slot */
+#define EMAIL_ERROR_CANCELLED                           -1046    /**<  The job was canceled by user */
+#define EMAIL_NO_AVAILABLE_TASK_SLOT                    -2656    /**<  There is no available task slot */
 
 /* Error codes for IPC*/
-#define EMAIL_ERROR_IPC_CRASH                           -1500
-#define EMAIL_ERROR_IPC_CONNECTION_FAILURE              -1501
-#define EMAIL_ERROR_IPC_SOCKET_FAILURE                  -1502
-#define EMAIL_ERROR_IPC_PROTOCOL_FAILURE                -1503
-#define EMAIL_ERROR_IPC_ALREADY_INITIALIZED             -1504
-#define EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE            -1300
+#define EMAIL_ERROR_IPC_CRASH                           -1500    /**<  The IPC connection is broken */
+#define EMAIL_ERROR_IPC_CONNECTION_FAILURE              -1501    /**<  The server(daemon) and client(app) did not connet */
+#define EMAIL_ERROR_IPC_SOCKET_FAILURE                  -1502    /**<  The IPC socket failed read and write */
+#define EMAIL_ERROR_IPC_PROTOCOL_FAILURE                -1503    /**<  The IPC protocol failed */
+#define EMAIL_ERROR_IPC_ALREADY_INITIALIZED             -1504    /**<  The IPC already is initialized */
+#define EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE            -1300    /**<  The active sync noti failed */
 
 /* Error codes for error from server */
-#define EMAIL_ERROR_COMMAND_NOT_SUPPORTED               -1043    /* The server doesn't support this command */
-#define EMAIL_ERROR_ANNONYM_NOT_SUPPORTED               -1044    /* The server doesn't support anonymous user */
-#define EMAIL_ERROR_SCAN_NOT_SUPPORTED                  -1037    /* The server doesn't support 'scan mailbox' */
-
-#define EMAIL_ERROR_SMTP_SEND_FAILURE                   -1063    /* SMTP send failed */
-#define EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE       -1073    /* SMTP send failed by too large mail size*/
-
-#define EMAIL_ERROR_POP3_DELE_FAILURE                   -1100    /* Failed to run the command 'Dele' on POP server */
-#define EMAIL_ERROR_POP3_UIDL_FAILURE                   -1101    /* Failed to run the command 'Uidl' on POP server */
-#define EMAIL_ERROR_POP3_LIST_FAILURE                   -1102    /* Failed to run the command 'List' on POP server */
-
-#define EMAIL_ERROR_IMAP4_APPEND_FAILURE                -1042    /* Failed to run the command 'Append' on IMAP server */
-#define EMAIL_ERROR_IMAP4_STORE_FAILURE                 -1200    /* Failed to run the command 'Store' on IMAP server */
-#define EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE               -1201    /* Failed to run the command 'Expunge' on IMAP server */
-#define EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE             -1202    /* Failed to run the command 'Fetch UID' on IMAP server */
-#define EMAIL_ERROR_IMAP4_FETCH_SIZE_FAILURE            -1203    /* Failed to run the command 'Fetch SIZE' on IMAP server */
-#define EMAIL_ERROR_IMAP4_IDLE_FAILURE                  -1204    /* Failed to run the command 'Idle' on IMAP server */
-#define EMAIL_ERROR_IMAP4_CREATE_FAILURE                -1210    /* Failed to run the command 'Create' on IMAP server */
-#define EMAIL_ERROR_IMAP4_DELETE_FAILURE                -1211    /* Failed to run the command 'Delete' on IMAP server */
-#define EMAIL_ERROR_IMAP4_RENAME_FAILURE                -1212    /* Failed to run the command 'Rename' on IMAP server */
-
-#define EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME        -1301
-
-#define EMAIL_ERROR_DOMAIN_LOOKUP_FAILED                -1423    /* The domain name provided was not found */
+#define EMAIL_ERROR_COMMAND_NOT_SUPPORTED               -1043    /**< The server does not support this command */
+#define EMAIL_ERROR_ANNONYM_NOT_SUPPORTED               -1044    /**< The server does not support anonymous user */
+#define EMAIL_ERROR_SCAN_NOT_SUPPORTED                  -1037    /**< The server does not support 'scan mailbox' */
+
+#define EMAIL_ERROR_SMTP_SEND_FAILURE                   -1063    /**< SMTP send failed */
+#define EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE       -1073    /**< SMTP send failed by too large mail size*/
+
+#define EMAIL_ERROR_POP3_DELE_FAILURE                   -1100    /**< Failed to run the command 'Dele' on POP server */
+#define EMAIL_ERROR_POP3_UIDL_FAILURE                   -1101    /**< Failed to run the command 'Uidl' on POP server */
+#define EMAIL_ERROR_POP3_LIST_FAILURE                   -1102    /**< Failed to run the command 'List' on POP server */
+#define EMAIL_ERROR_POP3_NOOP_FAILURE                   -1103    /**< Failed to run the command 'NOOP' on POP server */
+
+#define EMAIL_ERROR_IMAP4_APPEND_FAILURE                -1042    /**< Failed to run the command 'Append' on IMAP server */
+#define EMAIL_ERROR_IMAP4_STORE_FAILURE                 -1200    /**< Failed to run the command 'Store' on IMAP server */
+#define EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE               -1201    /**< Failed to run the command 'Expunge' on IMAP server */
+#define EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE             -1202    /**< Failed to run the command 'Fetch UID' on IMAP server */
+#define EMAIL_ERROR_IMAP4_FETCH_SIZE_FAILURE            -1203    /**< Failed to run the command 'Fetch SIZE' on IMAP server */
+#define EMAIL_ERROR_IMAP4_IDLE_FAILURE                  -1204    /**< Failed to run the command 'Idle' on IMAP server */
+#define EMAIL_ERROR_IMAP4_CREATE_FAILURE                -1210    /**< Failed to run the command 'Create' on IMAP server */
+#define EMAIL_ERROR_IMAP4_DELETE_FAILURE                -1211    /**< Failed to run the command 'Delete' on IMAP server */
+#define EMAIL_ERROR_IMAP4_RENAME_FAILURE                -1212    /**< Failed to run the command 'Rename' on IMAP server */
+#define EMAIL_ERROR_IMAP4_COPY_FAILURE                  -1213    /**< Failed to run the command 'Copy' on IMAP server */
+#define EMAIL_ERROR_IMAP4_NOOP_FAILURE                  -1214    /**< Failed to run the command 'NOOP' on IMAP server */
+
+#define EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME        -1301    /**< Invalid attachment save name */
+
+#define EMAIL_ERROR_DOMAIN_LOOKUP_FAILED                -1423    /**< The domain name provided was not found */
 
 /* Error codes for certificate */
-#define EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE            -3001    /*  Cannot load the certificate */
-#define EMAIL_ERROR_INVALID_CERTIFICATE                 -3002    /*  invalid certificate */
-#define EMAIL_ERROR_DECRYPT_FAILED                      -3003    /*  Cannot decipher the encrypt mail */
-#define EMAIL_ERROR_CERTIFICATE_FAILURE                 -1045    /*  certificate failure - Invalid server certificate */
+#define EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE            -3001    /**<  Cannot load the certificate */
+#define EMAIL_ERROR_INVALID_CERTIFICATE                 -3002    /**<  Invalid certificate */
+#define EMAIL_ERROR_DECRYPT_FAILED                      -3003    /**<  Cannot decipher the encrypt mail */
+#define EMAIL_ERROR_CERTIFICATE_FAILURE                 -1045    /**<  Certificate failure - Invalid server certificate */
 
 /* Error codes for account */
-#define EMAIL_ERROR_ACCOUNT_IS_QUARANTINED              -5001
-#define EMAIL_ERROR_ACCOUNT_IS_BLOCKED                  -5002
-#define EMAIL_ERROR_ACCOUNT_SYNC_IS_DISALBED            -5003
+#define EMAIL_ERROR_ACCOUNT_IS_QUARANTINED              -5001    /**< Account is quarantined */
+#define EMAIL_ERROR_ACCOUNT_IS_BLOCKED                  -5002    /**< Account is blocked */
+#define EMAIL_ERROR_ACCOUNT_SYNC_IS_DISALBED            -5003    /**< Account sync is disabled */
 
 /* Error codes for mails */
-#define EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER            -1055    /*  The expected mail is not found in server */
-#define EMAIL_ERROR_MAIL_IS_NOT_DOWNLOADED              -1095    /*  The mail is not downloaded */
-#define EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED          -1456    /*  The mail is already downloaded */
+#define EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER            -1055    /**<  The expected mail is not found in server */
+#define EMAIL_ERROR_MAIL_IS_NOT_DOWNLOADED              -1095    /**<  The mail is not downloaded */
+#define EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED          -1456    /**<  The mail is already downloaded */
 
 /* Error codes for attachment */
-#define EMAIL_ERROR_ATTACHMENT_SIZE_EXCEED_POLICY_LIMIT -7001
+#define EMAIL_ERROR_ATTACHMENT_SIZE_EXCEED_POLICY_LIMIT -7001    /**<  The attachment size is exceeded the policy value */
 
 /* Error codes for Other Module */
-#define EMAIL_ERROR_MDM_SERVICE_FAILURE                 -7100
-#define EMAIL_ERROR_MDM_RESTRICTED_MODE                 -7101
-#define EMAIL_ERROR_NOTI                                -7110
+#define EMAIL_ERROR_MDM_SERVICE_FAILURE                 -7100    /**<  The MDM service did not work */
+#define EMAIL_ERROR_MDM_RESTRICTED_MODE                 -7101    /**<  The MDM service is in restricted mode */
+#define EMAIL_ERROR_NOTI                                -7110    /**<  The Notification API returned the error */
 
 /* Etc */
-#define EMAIL_ERROR_ALREADY_INITIALIZED                 -7321
-#define EMAIL_ERROR_NOT_INITIALIZED                     -7322
-#define EMAIL_ERROR_UNKNOWN                             -8000    /*  unknown error */
+#define EMAIL_ERROR_ALREADY_INITIALIZED                 -7321    /**<  The thread is already intialized */
+#define EMAIL_ERROR_NOT_INITIALIZED                     -7322    /**<  The thread is not intialized */
+#define EMAIL_ERROR_UNKNOWN                             -8000    /**<  Unknown error */
 
 /* Should be replaced with proper name */
-#define EMAIL_ERROR_LOAD_ENGINE_FAILURE                 -1056    /*  loading engine failed */
-#define EMAIL_ERROR_CLOSE_FAILURE                       -1057    /*  engine is still used */
-#define EMAIL_ERROR_NULL_VALUE                          -1302
-#define EMAIL_ERROR_EMPTY_FILE                          -1304
+#define EMAIL_ERROR_LOAD_ENGINE_FAILURE                 -1056    /**<  Loading engine failed */
+#define EMAIL_ERROR_CLOSE_FAILURE                       -1057    /**<  Engine is still used */
+#define EMAIL_ERROR_NULL_VALUE                          -1302    /**<  The value is null */
+#define EMAIL_ERROR_EMPTY_FILE                          -1304    /**<  The file did not exist */
 
 /* Should be classified */
-#define EMAIL_ERROR_SYSTEM_FAILURE                      -1050    /*  There is a system error */
-#define EMAIL_ERROR_ON_PARSING                          -1700
-#define EMAIL_ERROR_ALREADY_EXISTS                      -1023    /*  data duplicated */
-#define EMAIL_ERROR_INPROPER_RESPONSE_FROM_MSG_SERVICE  -1323
+#define EMAIL_ERROR_SYSTEM_FAILURE                      -1050    /**<  There is a system error */
+#define EMAIL_ERROR_ON_PARSING                          -1700    /**<  MIME parsering is failed */
+#define EMAIL_ERROR_ALREADY_EXISTS                      -1322    /**<  Data duplicated */
+#define EMAIL_ERROR_INPROPER_RESPONSE_FROM_MSG_SERVICE  -1323    /**<  Returned the error from msg server */
 
 /* smack */
-#define EMAIL_ERROR_NO_SMACK_RULE                       -1710    /*  No smack rule exist for file access */
-#define EMAIL_ERROR_PERMISSION_DENIED                   -1720    /*  Failed to check privilege */
+#define EMAIL_ERROR_NO_SMACK_RULE                       -1710    /**<  No smack rule exist for file access */
+#define EMAIL_ERROR_PERMISSION_DENIED                   TIZEN_ERROR_PERMISSION_DENIED   /**<  Failed to check privilege */
 
 /* Not used */
-#define EMAIL_ERROR_INVALID_USER                        -1003    /*  invalid user ID was given. - Invalid user or password */
-#define EMAIL_ERROR_INVALID_PASSWORD                    -1004    /*  invalid password was given. - Invalid user or password */
-#define EMAIL_ERROR_INVALID_PATH                        -1011    /*  invalid flle path was given */
-#define EMAIL_ERROR_MAIL_MAX_COUNT                      -1052    /*  The mailbox is full */
-#define EMAIL_ERROR_DATA_TOO_SMALL                      -1026    /*  Data is too small */
-#define EMAIL_ERROR_PROFILE_FAILURE                     -1030    /*  no proper profile was found */
-#define EMAIL_ERROR_NO_MMC_INSERTED                     -1209
-#define EMAIL_ERROR_VALIDATE_ACCOUNT                    -1208
-#define EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP            -1215
-#define EMAIL_ERROR_NETWORK_TOO_BUSY                    -1027    /*  Network is busy */
-#define EMAIL_ERROR_DISCONNECTED                        -1034    /*  connection was disconnected */
-#define EMAIL_ERROR_MAIL_LOCKED                         -1049    /*  The mail was locked */
-#define EMAIL_ERROR_RETRIEVE_HEADER_DATA_FAILURE        -1065    /*  retrieving header failed */
-#define EMAIL_ERROR_MAILBOX_OPEN_FAILURE                -1064    /*  accessing mailbox failed */
-#define EMAIL_ERROR_XML_PARSER_FAILURE                  -1066    /*  XML parsing failed */
-#define EMAIL_ERROR_FAILED_BY_SECURITY_POLICY           -1303
-#define EMAIL_ERROR_FLIGHT_MODE                         -1206
-
+#define EMAIL_ERROR_INVALID_USER                        -1003    /**<  Invalid user ID was given. - Invalid user or password */
+#define EMAIL_ERROR_INVALID_PASSWORD                    -1004    /**<  Invalid password was given. - Invalid user or password */
+#define EMAIL_ERROR_INVALID_PATH                        -1011    /**<  Invalid flle path was given */
+#define EMAIL_ERROR_MAIL_MAX_COUNT                      -1052    /**<  The mailbox is full */
+#define EMAIL_ERROR_DATA_TOO_SMALL                      -1026    /**<  Data is too small */
+#define EMAIL_ERROR_PROFILE_FAILURE                     -1030    /**<  No proper profile was found */
+#define EMAIL_ERROR_NO_MMC_INSERTED                     -1209    /**<  The MMC card did not exist */
+#define EMAIL_ERROR_VALIDATE_ACCOUNT                    -1208    /**<  Account validation failed */
+#define EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP            -1215    /**<  Account validation of SMTP failed */
+#define EMAIL_ERROR_NETWORK_TOO_BUSY                    -1027    /**<  Network is busy */
+#define EMAIL_ERROR_DISCONNECTED                        -1034    /**<  Connection was disconnected */
+#define EMAIL_ERROR_MAIL_LOCKED                         -1049    /**<  The mail was locked */
+#define EMAIL_ERROR_RETRIEVE_HEADER_DATA_FAILURE        -1065    /**<  Retrieving header failed */
+#define EMAIL_ERROR_MAILBOX_OPEN_FAILURE                -1064    /**<  Accessing mailbox failed */
+#define EMAIL_ERROR_XML_PARSER_FAILURE                  -1066    /**<  XML parsing failed */
+#define EMAIL_ERROR_FAILED_BY_SECURITY_POLICY           -1303    /**<  Failed by security policy */
+#define EMAIL_ERROR_FLIGHT_MODE                         -1206    /**<  The network is flight mode */
+
+/* Container : KNOX */
+#define EMAIL_ERROR_CONTAINER_CREATE_FAILED             -2001    /**<  Failed to create the container (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_GET_DOMAIN                -2002    /**<  Failed to get the domain information (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_NOT_FOUND                 -2003    /**<  Failed to found the container (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_SET_LINK                  -2004    /**<  Failed to set shared socket link (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_ITERATE_DOMAIN            -2005    /**<  Failed to set interate domain (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION        -2006    /**<  Not created the container yet (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_JOIN_ZONE_FAILED          -2007    /**<  Failed to join zone in the container (Since 2.4) */
+#define EMAIL_ERROR_CONTAINER_LOOKUP_ZONE_FAILED        -2008    /**<  Failed to lookup the zone (Since 2.4) */
+ /**
+ * @}
+ */
 #endif /* __EMAIL_ERRORS_H__ */
index fe280f1..42ead9a 100755 (executable)
@@ -50,9 +50,8 @@ extern "C"
 #define __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__
 #define __FEATURE_BACKUP_ACCOUNT__
 #define __FEATURE_MOVE_TO_OUTBOX_FIRST__
-/*  #define __FEATURE_PARTIAL_BODY_FOR_POP3__*/
+#define __FEATURE_PARTIAL_BODY_FOR_POP3__
 /*  #define __FEATURE_KEEP_CONNECTION__  */
-/*  #define __FEATURE_DRM__ */
 #define __FEATURE_PARTIAL_BODY_DOWNLOAD__
 #define __FEATURE_HEADER_OPTIMIZATION__
 #define __FEATURE_SEND_OPTMIZATION__
@@ -68,30 +67,40 @@ extern "C"
 #define __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
 #define __FEATURE_SUPPORT_VALIDATION_SYSTEM__
 #define __FEATURE_PROGRESS_IN_OUTBOX__
-#define __FEATURE_OMA_EMN__
+
 /*  #define __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
-/*  #define __FEATURE_IMAP_IDLE__ */
+#define __FEATURE_IMAP_IDLE__
 #define __FEATURE_DRIVING_MODE__
 #define __FEATURE_DELETE_MAILBOX_RECURSIVELY__
 #define __FEATURE_RENAME_MAILBOX_RECURSIVELY__
 #define __FEATURE_AUTO_RETRY_SEND__
-/*  #define __FEATURE_SMTP_VALIDATION__  */
-/*  #define FEATURE_CORE_DEBUG  */
-/*  #define FEATURE_USE_GMIME  */
+#define __FEATURE_SMTP_VALIDATION__
+#define __FEATURE_USE_GMIME__
+#define __FEATURE_WIFI_AUTO_DOWNLOAD__
+
 /* #define __FEATURE_BLOCKING_MODE__ */
 #define __FEATURE_BODY_SEARCH__
-/* #define __FEATURE_USE_APPSYNC__ */
-/* #define __FEATURE_ACCESS_CONTROL__ */
+#define __FEATURE_ACCESS_CONTROL__
+#define __FEATURE_UPDATE_DB_TABLE_SCHEMA__ 
+#define __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
+/* #define __FEATURE_COMPARE_DOMAIN__ */
+/* #define __FEATURE_FORK_FOR_CURL__ */
+/* #define __FEATURE_USE_DRM_API__ */
+#define __FEATURE_SECURE_PGP__ 
+#define __FEATURE_SYNC_STATUS__
+/* #define __FEATURE_CONTAINER_ENABLE__ */
+/* #define __FEATURE_NOTIFICATION_ENABLE__ */
+/* #define __FEATURE_CONTACTS_ENABLE__ */
+/* #define __FEATURE_FEEDBACK_TYPE_LED_ENABLE__ */
 /* #define __FEATURE_IMAP_QUOTA__ */
 
-
 /* ----------------------------------------------------------------------------- */
 /*  Macro */
 #ifndef NULL
 #define NULL (char *)0
 #endif
 
-#define SESSION_MAX                            10
+#define SESSION_MAX                            50
 #define        IMAP_2004_LOG                       1
 #define TEXT_SIZE                           161 
 #define MAILBOX_COUNT                       6
@@ -107,6 +116,7 @@ extern "C"
 #define DOWNLOAD_NOTI_INTERVAL_PERCENT      5         /*  notify every 5% */
 #define DOWNLOAD_NOTI_INTERVAL_SIZE         51200     /*  notify every 50k */
 #define MAX_PATH                            4096      /* /usr/src/linux-2.4.20-8/include/linux/limits.h */
+#define MAX_FILENAME                        255
 #define DATETIME_LENGTH                     16
 #define MAIL_ID_STRING_LENGTH               10
 #define MAILBOX_ID_STRING_LENGTH            10
@@ -136,7 +146,7 @@ extern "C"
 
 #define ACCOUNT_PASSWORD_SS_GROUP_ID        "secure-storage::email-service"
 
-#define NATIVE_EMAIL_APPLICATION_PKG        "com.samsung.email"
+#define NATIVE_EMAIL_APPLICATION_PKG        "org.tizen.email"
 #define NATIVE_EMAIL_DOMAIN                 "email"
 
 #define IMAP_ID_OS                          "TIZEN"
@@ -167,8 +177,7 @@ extern "C"
 #define SAVE_TYPE_BUFFER                    2        /*  save content to buffer */
 #define SAVE_TYPE_FILE                      3        /*  save content to temporary file */
 
-#define FINISH_OFF_IF_CANCELED              if (!emcore_check_thread_status()) { err = EMAIL_ERROR_CANCELLED; goto FINISH_OFF; }
-#define CHECK_JOB_CANCELED()                {if (!emcore_check_thread_status()) goto JOB_CANCEL; }
+#define TOKEN_FOR_MULTI_USER                 "_"
 
 #define SNPRINTF(buff, size, format, args...)  snprintf(buff, size, format, ##args)
 #define SNPRINTF_OFFSET(base_buf, offset, base_size, format, args...) \
@@ -217,7 +226,7 @@ typedef pthread_t thread_t;
 #define VCONF_KEY_DEFAULT_ACCOUNT_ID    "db/private/email-service/default_account_id"
 #define VCONF_KEY_NOTI_PRIVATE_ID       "db/private/email-service/noti_private_id"
 
-#define VCONF_KEY_TOPMOST_WINDOW        "db/private/com.samsung.email/is_topmost_window"
+#define VCONF_KEY_TOPMOST_WINDOW        "db/private/org.tizen.email/is_topmost_window"
 
 #define OUTMODE  "wb"
 #define INMODE   "rb"
@@ -226,6 +235,7 @@ typedef pthread_t thread_t;
 
 #define TYPEPKCS7_SIGN 10      
 #define TYPEPKCS7_MIME 11
+#define TYPEPGP        12
 
 #define INLINE_ATTACHMENT    1
 #define ATTACHMENT           2
@@ -295,14 +305,16 @@ enum
        EXTENSION_TIF    = 7,
        EXTENSION_WBMP   = 8,
        EXTENSION_P7S    = 9,
-       EXTENSION_P7M    = 10
+       EXTENSION_P7M    = 10,
+       EXTENSION_ASC    = 11
 };
 
 typedef enum {
        EMAIL_ALARM_CLASS_SCHEDULED_SENDING   = 1,
        EMAIL_ALARM_CLASS_NEW_MAIL_ALERT      = 2,
        EMAIL_ALARM_CLASS_AUTO_POLLING        = 3,
-       EMAIL_ALARM_CLASS_AUTO_RESEND         = 4
+       EMAIL_ALARM_CLASS_AUTO_RESEND         = 4,
+       EMAIL_ALARM_CLASS_IMAP_IDLE           = 5,
 } email_alarm_class_t;
 
 
@@ -313,6 +325,7 @@ typedef struct
        int                        handle;
        email_event_type_t         type;
        email_event_status_type_t  status;
+       char                      *multi_user_name;
        char                      *event_param_data_1; /*  in general, mailbox name (exception in emcore_send_mail, emcore_send_saved_mail it is email_option_t **/
        char                      *event_param_data_2;
        char                      *event_param_data_3;
@@ -339,15 +352,11 @@ struct email_search_key_t
        email_search_key_t *next;
 };
 
-typedef struct
-{
-       int                  tid;
-       email_protocol_type_t  protocol;
-       void                *stream;
+/* the type is used to get uw-imap-toolkit error with thread local storage */
+typedef struct {
        int                  auth;
        int                  network;
        int                  error;
-       int                  status;
 } email_session_t;
 
 typedef struct
@@ -393,7 +402,7 @@ typedef struct
 } email_mail_contact_info_t;
 
 /*  global account list */
-typedef struct email_account_list {
+typedef struct  email_account_list {
     email_account_t *account;
     struct email_account_list *next;
 } email_account_list_t;
@@ -404,6 +413,14 @@ typedef struct {
        thread_t           thread_id;
 } email_active_task_t;
 
+typedef struct emcore_uid_elem {
+       int msgno;
+       char *uid;
+       char *internaldate;
+       email_mail_flag_t flag;
+       struct emcore_uid_elem *next;
+} emcore_uid_list;
+
 typedef void (*email_event_callback)(int total, int done, int status, int account_id, int mail_id, int handle, void *user_data, int error);
 
 /* ----------------------------------------------------------------------------- */
@@ -418,18 +435,27 @@ typedef enum
 
 typedef struct 
 {
-    int account_id;
-    int mail_id;
-    unsigned long server_mail_id;
-    int activity_id;
-    int mailbox_id;
-    char *mailbox_name;
-    email_event_type_t event_type;   /*  Event Type Null means event is created from local activitys    */
-    int activity_type;             /*  Activity Type Null means event is created from event queue */
-
+        int account_id;
+        int mail_id;
+        unsigned long server_mail_id;
+        int activity_id;
+        int mailbox_id;
+        char *mailbox_name;
+        char *multi_user_name;
+        email_event_type_t event_type;   /*  Event Type Null means event is created from local activitys    */
+        int activity_type;             /*  Activity Type Null means event is created from event queue */
 } email_event_partial_body_thd;
 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
 
+typedef enum
+{
+       EMAIL_ALERT_TYPE_MELODY, 
+       EMAIL_ALERT_TYPE_VIB, 
+       EMAIL_ALERT_TYPE_MELODY_AND_VIB, 
+       EMAIL_ALERT_TYPE_MUTE, 
+       EMAIL_ALERT_TYPE_NONE,
+} EMAIL_ALERT_TYPE;
+
 #ifdef __cplusplus
 }
 #endif /*  __cplusplus */
index 0ffecde..780483a 100755 (executable)
 #define __EMAIL_TYPES_H__
 
 /**
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
+ * @internal
+ * @file email-types.h
+ * @brief This file is the header file of Email Framework library.
+ */
 
 /**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_TYPES Email Types
-* @{
-*/
-/**
- * This file defines structures and enums of Email Framework.
- * @file       email-types.h
- * @author     Kyu-ho Jo(kyuho.jo@samsung.com)
- * @author     Choongho Lee(ch715.lee@samsung.com)
- * @version    0.1
- * @brief      This file is the header file of Email Framework library.
+ * @internal
+ * @addtogroup EMAIL_SERVICE_FRAMEWORK
+ * @{
  */
 
-
 #ifdef __cplusplus
 extern "C"
 {
@@ -61,7 +53,7 @@ extern "C"
 #define MAX_EMAIL_ADDRESS_LENGTH          254                                    /* RFC5322, RFC3696 */
 #define MAX_USER_NAME_LENGTH              64
 #define MAX_DATETIME_STRING_LENGTH        20
-#define MAX_PREVIEW_TEXT_LENGTH           512
+#define MAX_PREVIEW_TEXT_LENGTH           1000
 #define STRING_LENGTH_FOR_DISPLAY         100
 #define MEETING_REQ_OBJECT_ID_LENGTH      256
 #define EMAIL_NO_LIMITED_RETRY_COUNT      -1
@@ -78,43 +70,122 @@ extern "C"
 
 #define EMAIL_SUCCESS                     0                                      /*  we need to modify the success return value */
 
-#define EMAIL_ACC_GET_OPT_DEFAULT         0x01                                   /**< Default values without account name */
-#define EMAIL_ACC_GET_OPT_ACCOUNT_NAME    0x02                                   /**< Account name */
-#define EMAIL_ACC_GET_OPT_PASSWORD        0x04                                   /**< With password */
-#define EMAIL_ACC_GET_OPT_OPTIONS         0x08                                   /**< Account options : email_option_t */
-#define EMAIL_ACC_GET_OPT_FULL_DATA       0xFF                                   /**< With all data of account */
+/** @brief Definition for default values without account name.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACC_GET_OPT_DEFAULT         0x01
+
+/** @brief Definition for account name.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACC_GET_OPT_ACCOUNT_NAME    0x02
+
+/** @brief Definition for account with password.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACC_GET_OPT_PASSWORD        0x04
+
+/** @brief Definition for account with options: #email_option_t.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACC_GET_OPT_OPTIONS         0x08
+
+/** @brief Definition for account with all data of account.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACC_GET_OPT_FULL_DATA       0xFF
+
 
 #define GET_FULL_DATA                     (EMAIL_ACC_GET_OPT_FULL_DATA)
 #define GET_FULL_DATA_WITHOUT_PASSWORD    (EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_ACCOUNT_NAME | EMAIL_ACC_GET_OPT_OPTIONS )
 #define WITHOUT_OPTION                    (EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_ACCOUNT_NAME )
 #define ONLY_OPTION                       (EMAIL_ACC_GET_OPT_OPTIONS)
 
-#define THREAD_TYPE_RECEIVING             0                                      /**< for function 'email_activate_pdp' */
-#define THREAD_TYPE_SENDING               1                                      /**< for function 'email_activate_pdp' */
-
-#define EMAIL_IMAP_PORT                   143                                    /**< Specifies the default IMAP port.*/
-#define EMAIL_POP3_PORT                   110                                    /**< Specifies the default POP3 port.*/
-#define EMAIL_SMTP_PORT                   25                                     /**< Specifies the default SMTP port.*/
-#define EMAIL_IMAPS_PORT                  993                                    /**< Specifies the default IMAP SSL port.*/
-#define EMAIL_POP3S_PORT                  995                                    /**< Specifies the default POP3 SSL port.*/
-#define EMAIL_SMTPS_PORT                  465                                    /**< Specifies the default SMTP SSL port.*/
-#define EMAIL_ACCOUNT_MAX                 10                                     /**< Specifies the MAX account.*/
-
-#define EMAIL_INBOX_NAME                  "INBOX"                                /**< Specifies the name of inbox.*/
-#define EMAIL_DRAFTBOX_NAME               "DRAFTBOX"                             /**< Specifies the name of draftbox.*/
-#define EMAIL_OUTBOX_NAME                 "OUTBOX"                               /**< Specifies the name of outbox.*/
-#define EMAIL_SENTBOX_NAME                "SENTBOX"                              /**< Specifies the name of sentbox.*/
-#define EMAIL_TRASH_NAME                  "TRASH"                                /**< Specifies the name of trash.*/
-#define EMAIL_SPAMBOX_NAME                "SPAMBOX"                              /**< Specifies the name of spambox.*/
-
-#define EMAIL_INBOX_DISPLAY_NAME          "Inbox"                                /**< Specifies the display name of inbox.*/
-#define EMAIL_DRAFTBOX_DISPLAY_NAME       "Draftbox"                             /**< Specifies the display name of draftbox.*/
-#define EMAIL_OUTBOX_DISPLAY_NAME         "Outbox"                               /**< Specifies the display name of outbox.*/
-#define EMAIL_SENTBOX_DISPLAY_NAME        "Sentbox"                              /**< Specifies the display name of sentbox.*/
-#define EMAIL_TRASH_DISPLAY_NAME          "Trash"                                /**< Specifies the display name of sentbox.*/
-#define EMAIL_SPAMBOX_DISPLAY_NAME        "Spambox"                              /**< Specifies the display name of spambox.*/
-
-#define EMAIL_SEARCH_RESULT_MAILBOX_NAME  "_`S1!E2@A3#R4$C5^H6&R7*E8(S9)U0-L=T_" /**< Specifies the name of search result mailbox.*/
+
+/** @brief Definition for the function 'email_activate_pdp'.
+ *  @since_tizen 2.3 */
+#define THREAD_TYPE_RECEIVING             0
+
+/** @brief Definition for the function 'email_activate_pdp'.
+ *  @since_tizen 2.3 */
+#define THREAD_TYPE_SENDING               1
+
+
+/** @brief Definition for the default IMAP port.
+ *  @since_tizen 2.3 */
+#define EMAIL_IMAP_PORT                   143
+
+/** @brief Definition for the default POP3 port.
+ *  @since_tizen 2.3 */
+#define EMAIL_POP3_PORT                   110
+
+/** @brief Definition for the default SMTP port.
+ *  @since_tizen 2.3 */
+#define EMAIL_SMTP_PORT                   25
+
+/** @brief Definition for the default IMAP SSL port.
+ *  @since_tizen 2.3 */
+#define EMAIL_IMAPS_PORT                  993
+
+/** @brief Definition for the default POP3 SSL port.
+ *  @since_tizen 2.3 */
+#define EMAIL_POP3S_PORT                  995
+
+/** @brief Definition for the default SMTP SSL port.
+ *  @since_tizen 2.3 */
+#define EMAIL_SMTPS_PORT                  465
+
+/** @brief Definition for the MAX account.
+ *  @since_tizen 2.3 */
+#define EMAIL_ACCOUNT_MAX                 10
+
+/** @brief Definition for the name of inbox.
+ *  @since_tizen 2.3 */
+#define EMAIL_INBOX_NAME                  "INBOX"
+
+/** @brief Definition for the name of draftbox.
+ * @since_tizen 2.3 */
+#define EMAIL_DRAFTBOX_NAME               "DRAFTBOX"
+
+/** @brief Definition for the name of outbox.
+ * @since_tizen 2.3 */
+#define EMAIL_OUTBOX_NAME                 "OUTBOX"
+
+/** @brief Definition for the name of sentbox.
+ * @since_tizen 2.3 */
+#define EMAIL_SENTBOX_NAME                "SENTBOX"
+
+/** @brief Definition for the name of trash.
+ * @since_tizen 2.3 */
+#define EMAIL_TRASH_NAME                  "TRASH"
+
+/** @brief Definition for the name of spambox.
+ * @since_tizen 2.3 */
+#define EMAIL_SPAMBOX_NAME                "SPAMBOX"
+
+/** @brief Definition for the display name of inbox.
+ * @since_tizen 2.3 */
+#define EMAIL_INBOX_DISPLAY_NAME          "Inbox"
+
+/** @brief Definition for the display name of draftbox.
+ * @since_tizen 2.3 */
+#define EMAIL_DRAFTBOX_DISPLAY_NAME       "Draftbox"
+
+/** @brief Definition for the display name of outbox.
+ * @since_tizen 2.3 */
+#define EMAIL_OUTBOX_DISPLAY_NAME         "Outbox"
+
+/** @brief Definition for the display name of sentbox.
+ * @since_tizen 2.3 */
+#define EMAIL_SENTBOX_DISPLAY_NAME        "Sentbox"
+
+/** @brief Definition for the display name of trash.
+ * @since_tizen 2.3 */
+#define EMAIL_TRASH_DISPLAY_NAME          "Trash"
+
+/** @brief Definition for the display name of spambox.
+ * @since_tizen 2.3 */
+#define EMAIL_SPAMBOX_DISPLAY_NAME        "Spambox"
+
+
+/** @brief Definition for the name of search result mailbox.
+ * @since_tizen 2.3 */
+#define EMAIL_SEARCH_RESULT_MAILBOX_NAME  "_`S1!E2@A3#R4$C5^H6&R7*E8(S9)U0-L=T_"
 
 #define SYNC_STATUS_FINISHED              0                                      /* BIN 00000000 */
 #define SYNC_STATUS_SYNCING               1                                      /* BIN 00000001 */
@@ -162,1031 +233,1142 @@ extern "C"
 /*****************************************************************************/
 
 enum {
-       /* Account */
-       _EMAIL_API_ADD_ACCOUNT                               = 0x01000000,
-       _EMAIL_API_DELETE_ACCOUNT                            = 0x01000001,
-       _EMAIL_API_UPDATE_ACCOUNT                            = 0x01000002,
-       _EMAIL_API_GET_ACCOUNT                               = 0x01000003,
-       _EMAIL_API_GET_ACCOUNT_LIST                          = 0x01000005,
-       _EMAIL_API_GET_MAILBOX_COUNT                         = 0x01000007,
-       _EMAIL_API_VALIDATE_ACCOUNT                          = 0x01000008,
-       _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION               = 0x01000009,
-       _EMAIL_API_BACKUP_ACCOUNTS                           = 0x0100000A,
-       _EMAIL_API_RESTORE_ACCOUNTS                          = 0x0100000B,
-       _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT            = 0x0100000C,
-       _EMAIL_API_VALIDATE_ACCOUNT_EX                       = 0x0100000D,
-
-       /* Mail */
-       _EMAIL_API_DELETE_MAIL                               = 0x01100002,
-       _EMAIL_API_DELETE_ALL_MAIL                           = 0x01100004,
-       _EMAIL_API_GET_MAILBOX_LIST                          = 0x01100006,
-       _EMAIL_API_GET_SUBMAILBOX_LIST                       = 0x01100007,
-       _EMAIL_API_CLEAR_DATA                                = 0x01100009,
-       _EMAIL_API_MOVE_MAIL                                 = 0x0110000A,
-       _EMAIL_API_MOVE_ALL_MAIL                             = 0x0110000B,
-       _EMAIL_API_ADD_ATTACHMENT                            = 0x0110000C,
-       _EMAIL_API_GET_ATTACHMENT                            = 0x0110000D,
-       _EMAIL_API_DELETE_ATTACHMENT                         = 0x0110000E,
-       _EMAIL_API_MODIFY_MAIL_FLAG                          = 0x0110000F,
-       _EMAIL_API_MODIFY_MAIL_EXTRA_FLAG                    = 0x01100011,
-       _EMAIL_API_SET_FLAGS_FIELD                           = 0x01100016,
-       _EMAIL_API_ADD_MAIL                                  = 0x01100017,
-       _EMAIL_API_UPDATE_MAIL                               = 0x01100018,
-       _EMAIL_API_ADD_READ_RECEIPT                          = 0x01100019,
-       _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED             = 0x0110001A,
-
-       /* Thread */
-       _EMAIL_API_MOVE_THREAD_TO_MAILBOX                    = 0x01110000,
-       _EMAIL_API_DELETE_THREAD                             = 0x01110001,
-       _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD                = 0x01110002,
-
-       /* Mailbox */
-       _EMAIL_API_ADD_MAILBOX                               = 0x01200000,
-       _EMAIL_API_DELETE_MAILBOX                            = 0x01200001,
-       _EMAIL_API_SET_MAIL_SLOT_SIZE                        = 0x01200007,
-       _EMAIL_API_RENAME_MAILBOX                            = 0x01200008,
-       _EMAIL_API_RENAME_MAILBOX_EX                         = 0x0120000B,
-       _EMAIL_API_SET_MAILBOX_TYPE                          = 0x01200009,
-       _EMAIL_API_SET_LOCAL_MAILBOX                         = 0x0120000A,
-
-       /* Network */
-       _EMAIL_API_SEND_MAIL                                 = 0x01300000,
-       _EMAIL_API_SYNC_HEADER                               = 0x01300001,
-       _EMAIL_API_DOWNLOAD_BODY                             = 0x01300002,
-       _EMAIL_API_DOWNLOAD_ATTACHMENT                       = 0x01300003,
-       _EMAIL_API_SEND_SAVED                                = 0x01300005,
-       _EMAIL_API_DELETE_EMAIL                              = 0x01300007,
-       _EMAIL_API_DELETE_EMAIL_ALL                          = 0x01300008,
-       _EMAIL_API_GET_IMAP_MAILBOX_LIST                     = 0x01300015,
-       _EMAIL_API_SEND_MAIL_CANCEL_JOB                      = 0x01300017,
-       _EMAIL_API_SEARCH_MAIL_ON_SERVER                     = 0x01300019,
-       _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER     = 0x0130001A,
-       _EMAIL_API_QUERY_SMTP_MAIL_SIZE_LIMIT                = 0x0130001B,
-
-       /* Rule */
-       _EMAIL_API_ADD_RULE                                  = 0x01400000,
-       _EMAIL_API_GET_RULE                                  = 0x01400001,
-       _EMAIL_API_GET_RULE_LIST                             = 0x01400002,
-       _EMAIL_API_FIND_RULE                                 = 0x01400003,
-       _EMAIL_API_DELETE_RULE                               = 0x01400004,
-       _EMAIL_API_UPDATE_RULE                               = 0x01400005,
-       _EMAIL_API_APPLY_RULE                                = 0x01400006,
-       _EMAIL_API_CANCEL_JOB                                = 0x01400007,
-       _EMAIL_API_SEND_RETRY                                = 0x01400009,
-       _EMAIL_API_UPDATE_ACTIVITY                           = 0x0140000A,
-       _EMAIL_API_SYNC_LOCAL_ACTIVITY                       = 0x0140000B,
+    /* Account */
+    _EMAIL_API_ADD_ACCOUNT                               = 0x01000000,    /**< IPC API ID for email_add_account */
+    _EMAIL_API_DELETE_ACCOUNT                            = 0x01000001,    /**< IPC API ID for email_delete_account */
+    _EMAIL_API_UPDATE_ACCOUNT                            = 0x01000002,    /**< IPC API ID for email_update_account */
+    _EMAIL_API_GET_ACCOUNT                               = 0x01000003,    /**< IPC API ID for email_get_account */
+    _EMAIL_API_GET_ACCOUNT_LIST                          = 0x01000005,    /**< IPC API ID for email_get_account_list */
+    _EMAIL_API_GET_MAILBOX_COUNT                         = 0x01000007,    /**< IPC API ID for email_get_mailbox_count */
+    _EMAIL_API_VALIDATE_ACCOUNT                          = 0x01000008,    /**< IPC API ID for email_validate_account */
+    _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION               = 0x01000009,    /**< IPC API ID for email_add_account_with_validation */
+    _EMAIL_API_BACKUP_ACCOUNTS                           = 0x0100000A,    /**< IPC API ID for email_backup_accounts */
+    _EMAIL_API_RESTORE_ACCOUNTS                          = 0x0100000B,    /**< IPC API ID for email_restore_accounts */
+    _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT            = 0x0100000C,    /**< IPC API ID for email_get_password_legnth_of_account */
+    _EMAIL_API_VALIDATE_ACCOUNT_EX                       = 0x0100000D,    /**< IPC API ID for email_validate_account_ex */
+
+    /* Mail */
+    _EMAIL_API_DELETE_MAIL                               = 0x01100002,    /**< IPC API ID for email_delete_mail */
+    _EMAIL_API_DELETE_ALL_MAIL                           = 0x01100004,    /**< IPC API ID for email_delete_mail_all */
+    _EMAIL_API_GET_MAILBOX_LIST                          = 0x01100006,    /**< IPC API ID for email_get_mailbox_list */
+    _EMAIL_API_GET_SUBMAILBOX_LIST                       = 0x01100007,    /**< IPC API ID for email_get_submailbox_list */
+    _EMAIL_API_CLEAR_DATA                                = 0x01100009,    /**< IPC API ID for email_clear_data */
+    _EMAIL_API_MOVE_MAIL                                 = 0x0110000A,    /**< IPC API ID for email_move_mail */
+    _EMAIL_API_MOVE_ALL_MAIL                             = 0x0110000B,    /**< IPC API ID for email_move_all_mail */
+    _EMAIL_API_ADD_ATTACHMENT                            = 0x0110000C,    /**< IPC API ID for email_move_add_attachment */
+    _EMAIL_API_GET_ATTACHMENT                            = 0x0110000D,    /**< IPC API ID for email_get_attachment */
+    _EMAIL_API_DELETE_ATTACHMENT                         = 0x0110000E,    /**< IPC API ID for email_delete_attachment */
+    _EMAIL_API_MODIFY_MAIL_FLAG                          = 0x0110000F,    /**< IPC API ID for email_modify_mail_flag */
+    _EMAIL_API_MODIFY_MAIL_EXTRA_FLAG                    = 0x01100011,    /**< IPC API ID for email_modify_mail_extra_flag */
+    _EMAIL_API_SET_FLAGS_FIELD                           = 0x01100016,    /**< IPC API ID for email_set_flags_field */
+    _EMAIL_API_ADD_MAIL                                  = 0x01100017,    /**< IPC API ID for email_add_mail */
+    _EMAIL_API_UPDATE_MAIL                               = 0x01100018,    /**< IPC API ID for email_update_mail */
+    _EMAIL_API_ADD_READ_RECEIPT                          = 0x01100019,    /**< IPC API ID for email_add_read_receipt */
+    _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED             = 0x0110001A,    /**< IPC API ID for email_expunge_mails_deleted_flagged */
+
+    /* Thread */
+    _EMAIL_API_MOVE_THREAD_TO_MAILBOX                    = 0x01110000,    /**< IPC API ID for email_move_thread_to_mailbox */
+    _EMAIL_API_DELETE_THREAD                             = 0x01110001,    /**< IPC API ID for email_delete_thread */
+    _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD                = 0x01110002,    /**< IPC API ID for email_modify_seen_flag_of_thread */
+
+    /* Mailbox */
+    _EMAIL_API_ADD_MAILBOX                               = 0x01200000,    /**< IPC API ID for email_add_mailbox */
+    _EMAIL_API_DELETE_MAILBOX                            = 0x01200001,    /**< IPC API ID for email_delete mailbox */
+    _EMAIL_API_SET_MAIL_SLOT_SIZE                        = 0x01200007,    /**< IPC API ID for email_set_mail_slot_size */
+    _EMAIL_API_RENAME_MAILBOX                            = 0x01200008,    /**< IPC API ID for email_rename_mailbox */
+    _EMAIL_API_RENAME_MAILBOX_EX                         = 0x0120000B,    /**< IPC API ID for email_rename_mailbox_ex */
+    _EMAIL_API_SET_MAILBOX_TYPE                          = 0x01200009,    /**< IPC API ID for email_set_mailbox_type */
+    _EMAIL_API_SET_LOCAL_MAILBOX                         = 0x0120000A,    /**< IPC API ID for email_set_local_mailbox */
+
+    /* Network */
+    _EMAIL_API_SEND_MAIL                                 = 0x01300000,    /**< IPC API ID for email_send_mail */
+    _EMAIL_API_SYNC_HEADER                               = 0x01300001,    /**< IPC API ID for email_sycn_header */
+    _EMAIL_API_DOWNLOAD_BODY                             = 0x01300002,    /**< IPC API ID for email_download_body */
+    _EMAIL_API_DOWNLOAD_ATTACHMENT                       = 0x01300003,    /**< IPC API ID for email_download_attachment */
+    _EMAIL_API_SEND_SAVED                                = 0x01300005,    /**< IPC API ID for email_send_saved */
+    _EMAIL_API_DELETE_EMAIL                              = 0x01300007,    /**< IPC API ID for email_delete_email */
+    _EMAIL_API_DELETE_EMAIL_ALL                          = 0x01300008,    /**< IPC API ID for email_delete_email_all */
+    _EMAIL_API_GET_IMAP_MAILBOX_LIST                     = 0x01300015,    /**< IPC API ID for email_get_imap_mailbox_list */
+    _EMAIL_API_SEND_MAIL_CANCEL_JOB                      = 0x01300017,    /**< IPC API ID for email_send_mail_cancel_job */
+    _EMAIL_API_QUERY_SMTP_MAIL_SIZE_LIMIT                = 0x0130001B,    /**< IPC API ID for email_query_smtp_mail_size_limit */
+
+    /* Rule */
+    _EMAIL_API_ADD_RULE                                  = 0x01400000,    /**< IPC API ID for email_add_rule */
+    _EMAIL_API_GET_RULE                                  = 0x01400001,    /**< IPC API ID for email_get_rule */
+    _EMAIL_API_GET_RULE_LIST                             = 0x01400002,    /**< IPC API ID for email_get_rule_list */
+    _EMAIL_API_FIND_RULE                                 = 0x01400003,    /**< IPC API ID for email_find_rule */
+    _EMAIL_API_DELETE_RULE                               = 0x01400004,    /**< IPC API ID for email_delete_rule */
+    _EMAIL_API_UPDATE_RULE                               = 0x01400005,    /**< IPC API ID for email_update_rule */
+    _EMAIL_API_APPLY_RULE                                = 0x01400006,    /**< IPC API ID for email_apply_rule */
+    _EMAIL_API_CANCEL_JOB                                = 0x01400007,    /**< IPC API ID for email_cancel_job */
+    _EMAIL_API_SEND_RETRY                                = 0x01400009,    /**< IPC API ID for email_send_retry */
+    _EMAIL_API_UPDATE_ACTIVITY                           = 0x0140000A,    /**< IPC API ID for email_update_activity */
+    _EMAIL_API_SYNC_LOCAL_ACTIVITY                       = 0x0140000B,    /**< IPC API ID for email_sync_local_activity */
 
        /* Etc */
-       _EMAIL_API_PING_SERVICE                              = 0x01500000,
-       _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL   = 0x01500001,
-       _EMAIL_API_SHOW_USER_MESSAGE                         = 0x01500002,
-       _EMAIL_API_WRITE_MIME_FILE                           = 0x01500003,
-       _EMAIL_API_GET_TASK_INFORMATION                      = 0x01500004,
-
-       /* Smime */
-       _EMAIL_API_ADD_CERTIFICATE                           = 0x01600000,
-       _EMAIL_API_DELETE_CERTIFICATE                        = 0x01600001,
-       _EMAIL_API_VERIFY_SIGNATURE                          = 0x01600002,
-       _EMAIL_API_VERIFY_CERTIFICATE                        = 0x01600003,
+       _EMAIL_API_PING_SERVICE                              = 0x01500000,    /**< IPC API ID for email_ping_service */
+       _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL   = 0x01500001,    /**< IPC API ID for email_update_notification_bar_for_unread_mail */
+       _EMAIL_API_SHOW_USER_MESSAGE                         = 0x01500002,    /**< IPC API ID for email_show_user_message */
+       _EMAIL_API_WRITE_MIME_FILE                           = 0x01500003,    /**< IPC API ID for email_write_mime_file */
+       _EMAIL_API_GET_TASK_INFORMATION                      = 0x01500004,    /**< IPC API ID for email_get_task_information */
+       _EMAIL_API_CLEAR_NOTIFICATION_BAR                    = 0x01500005,
+       _EMAIL_API_GET_USER_NAME                             = 0x01500006,
+
+    /* Smime */
+    _EMAIL_API_ADD_CERTIFICATE                           = 0x01600000,    /**< IPC API ID for email_add_certificate */
+    _EMAIL_API_DELETE_CERTIFICATE                        = 0x01600001,    /**< IPC API ID for email_delete_certificate */
+    _EMAIL_API_VERIFY_SIGNATURE                          = 0x01600002,    /**< IPC API ID for email_verify_signature */
+    _EMAIL_API_VERIFY_CERTIFICATE                        = 0x01600003,    /**< IPC API ID for email_verify_certificate */
 };
 
 typedef enum
 {
-       EMAIL_DELETE_LOCALLY                     = 0,  /**< Specifies Mail Delete local only */
-       EMAIL_DELETE_LOCAL_AND_SERVER,                 /**< Specifies Mail Delete local & server */
-       EMAIL_DELETE_FOR_SEND_THREAD,                  /**< Created to check which activity to delete in send thread */
-       EMAIL_DELETE_FROM_SERVER,
-       EMAIL_DELETE_MAIL_AND_MEETING_FOR_EAS,         /**< Delete mails and meetings on an EAS account. */
+    EMAIL_DELETE_LOCALLY                     = 0,  /**< Delete mail locally only */
+    EMAIL_DELETE_LOCAL_AND_SERVER,                 /**< Delete mail locally and on server */
+    EMAIL_DELETE_FOR_SEND_THREAD,                  /**< Check which activity to delete in send thread */
+    EMAIL_DELETE_FROM_SERVER,                      /**< Delete mail on server */
+    EMAIL_DELETE_MAIL_AND_MEETING_FOR_EAS,         /**< Delete mails and meetings on an EAS account */
 } email_delete_option_t;
 
+/**
+ * @brief Enumeration for the notification of changes on storage.
+ * @since_tizen 2.3
+ */
 typedef enum
 {
-       NOTI_MAIL_ADD                            = 10000,
-       NOTI_MAIL_DELETE                         = 10001,
-       NOTI_MAIL_DELETE_ALL                     = 10002,
-       NOTI_MAIL_DELETE_WITH_ACCOUNT            = 10003,
-       NOTI_MAIL_DELETE_FAIL                    = 10007,
-       NOTI_MAIL_DELETE_FINISH                  = 10008,
-
-       NOTI_MAIL_UPDATE                         = 10004,
-       NOTI_MAIL_FIELD_UPDATE                   = 10020,
-
-       NOTI_MAIL_MOVE                           = 10005,
-       NOTI_MAIL_MOVE_FAIL                      = 10010,
-       NOTI_MAIL_MOVE_FINISH                    = 10006,
-
-       NOTI_THREAD_MOVE                         = 11000,
-       NOTI_THREAD_DELETE                       = 11001,
-       NOTI_THREAD_MODIFY_SEEN_FLAG             = 11002,
-
-       NOTI_ACCOUNT_ADD                         = 20000,
-       NOTI_ACCOUNT_DELETE                      = 20001,
-       NOTI_ACCOUNT_DELETE_FAIL                 = 20003,
-       NOTI_ACCOUNT_UPDATE                      = 20002,
-       NOTI_ACCOUNT_UPDATE_SYNC_STATUS          = 20010,
-
-       NOTI_MAILBOX_ADD                         = 40000,
-       NOTI_MAILBOX_DELETE                      = 40001,
-       NOTI_MAILBOX_UPDATE                      = 40002,
-       NOTI_MAILBOX_FIELD_UPDATE                = 40003,
-
-       NOTI_MAILBOX_RENAME                      = 40010,
-       NOTI_MAILBOX_RENAME_FAIL                 = 40011,
-
-       NOTI_CERTIFICATE_ADD                     = 50000,
-       NOTI_CERTIFICATE_DELETE                  = 50001,
-       NOTI_CERTIFICATE_UPDATE                  = 50002,
-       /* To be added more */
+    NOTI_MAIL_ADD                            = 10000,    /**< A mail is added */
+    NOTI_MAIL_DELETE                         = 10001,    /**< Some mails are removed */
+    NOTI_MAIL_DELETE_ALL                     = 10002,    /**< All mails in mailbox are removed */
+    NOTI_MAIL_DELETE_WITH_ACCOUNT            = 10003,    /**< All mails of an account removed */
+    NOTI_MAIL_DELETE_FAIL                    = 10007,    /**< Removing mails failed */
+    NOTI_MAIL_DELETE_FINISH                  = 10008,    /**< Removing mails finished */
+
+    NOTI_MAIL_UPDATE                         = 10004,    /**< Some fields of mail are updated */
+    NOTI_MAIL_FIELD_UPDATE                   = 10020,    /**< A field of some mails is updated */
+
+    NOTI_MAIL_MOVE                           = 10005,    /**< Some mail are moved */
+    NOTI_MAIL_MOVE_FAIL                      = 10010,    /**< Moving mails failed */
+    NOTI_MAIL_MOVE_FINISH                    = 10006,    /**< Moving mails finished */
+
+    NOTI_THREAD_MOVE                         = 11000,    /**< A mail thread is moved */
+    NOTI_THREAD_DELETE                       = 11001,    /**< A mail thread is removed */
+    NOTI_THREAD_MODIFY_SEEN_FLAG             = 11002,    /**< Seen flag of a mail thread is modified */
+    NOTI_THREAD_ID_CHANGED_BY_ADD            = 11003,    /**< The thread ID is changed by adding a new mail */
+    NOTI_THREAD_ID_CHANGED_BY_MOVE_OR_DELETE = 11004,    /**< The thread ID is changed by moving or removing mails */
+
+    NOTI_ACCOUNT_ADD                         = 20000,    /**< An account is added */
+    NOTI_ACCOUNT_ADD_FINISH                  = 20005,    /**< An account is added and account reference updated */
+    NOTI_ACCOUNT_ADD_FAIL                    = 20007,    /**< An account adding failed */
+    NOTI_ACCOUNT_DELETE                      = 20001,    /**< An account is removed */
+    NOTI_ACCOUNT_DELETE_FAIL                 = 20003,    /**< Removing an account failed */
+    NOTI_ACCOUNT_UPDATE                      = 20002,    /**< An account is updated */
+    NOTI_ACCOUNT_UPDATE_SYNC_STATUS          = 20010,    /**< Sync status of an account is updated */
+
+    NOTI_MAILBOX_ADD                         = 40000,    /**< A mailbox is added */
+    NOTI_MAILBOX_DELETE                      = 40001,    /**< A mailbox is removed */
+    NOTI_MAILBOX_UPDATE                      = 40002,    /**< A mailbox is updated */
+    NOTI_MAILBOX_FIELD_UPDATE                = 40003,    /**< Some fields of a mailbox are updated */
+
+    NOTI_MAILBOX_RENAME                      = 40010,    /**< A mailbox is renamed */
+    NOTI_MAILBOX_RENAME_FAIL                 = 40011,    /**< Renaming a mailbox failed */
+
+    NOTI_CERTIFICATE_ADD                     = 50000,    /**< A certificate is added */
+    NOTI_CERTIFICATE_DELETE                  = 50001,    /**< A certificate is removed */
+    NOTI_CERTIFICATE_UPDATE                  = 50002,    /**< A certificate is updated */
+
+    NOTI_ACCOUNT_RESTORE_START               = 60001,    /**< Start of restoring accounts */
+    NOTI_ACCOUNT_RESTORE_FINISH              = 60002,    /**< Finish of restoring accounts */
+    NOTI_ACCOUNT_RESTORE_FAIL                = 60003,    /**< Failure of restoring accounts */
+
+    NOTI_RULE_ADD                            = 70000,    /**< A rule is added in DB */
+    NOTI_RULE_APPLY                          = 70001,    /**< A rule is applied */
+    NOTI_RULE_DELETE                         = 70002,    /**< A rule is removed in DB */
+    NOTI_RULE_UPDATE                         = 70003,    /**< A rule is updated in DB */
+    
+    /* To be added more */
 } email_noti_on_storage_event;
 
+/**
+ * @brief Enumeration for the notification of network event.
+ * @since_tizen 2.3
+ */
 typedef enum
 {
-       NOTI_SEND_START                          = 1002,
-       NOTI_SEND_FINISH                         = 1004,
-       NOTI_SEND_FAIL                           = 1005,
-       NOTI_SEND_CANCEL                         = 1003,
-
-       NOTI_DOWNLOAD_START                      = 2000,
-       NOTI_DOWNLOAD_FINISH,
-       NOTI_DOWNLOAD_FAIL,
-       NOTI_DOWNLOAD_CANCEL                     = 2004,
-       NOTI_DOWNLOAD_NEW_MAIL                   = 2003,
-
-       NOTI_DOWNLOAD_BODY_START                 = 3000,
-       NOTI_DOWNLOAD_BODY_FINISH                = 3002,
-       NOTI_DOWNLOAD_BODY_FAIL                  = 3004,
-       NOTI_DOWNLOAD_BODY_CANCEL                = 3003,
-       NOTI_DOWNLOAD_MULTIPART_BODY             = 3001,
-
-       NOTI_DOWNLOAD_ATTACH_START               = 4000,
-       NOTI_DOWNLOAD_ATTACH_FINISH,
-       NOTI_DOWNLOAD_ATTACH_FAIL,
-       NOTI_DOWNLOAD_ATTACH_CANCEL,
-
-       NOTI_MAIL_DELETE_ON_SERVER_FAIL          = 5000,
-       NOTI_MAIL_MOVE_ON_SERVER_FAIL,
-
-       NOTI_SEARCH_ON_SERVER_START              = 6000,
-       NOTI_SEARCH_ON_SERVER_FINISH             = 6001,
-       NOTI_SEARCH_ON_SERVER_FAIL               = 6002,
-       NOTI_SEARCH_ON_SERVER_CANCEL             = 6003,
-
-       NOTI_VALIDATE_ACCOUNT_FINISH             = 7000,
-       NOTI_VALIDATE_ACCOUNT_FAIL,
-       NOTI_VALIDATE_ACCOUNT_CANCEL,
-
-       NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH  = 8000,
-       NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL,
-       NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL,
-
-       NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH  = 9000,
-       NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL,
-       NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL,
-
-       NOTI_VALIDATE_CERTIFICATE_FINISH         = 10000,
-       NOTI_VALIDATE_CERTIFICATE_FAIL           = 10001,
-       NOTI_VALIDATE_CERTIFICATE_CANCEL         = 10002,
-
-       NOTI_RESOLVE_RECIPIENT_START             = 11000,
-       NOTI_RESOLVE_RECIPIENT_FINISH,
-       NOTI_RESOLVE_RECIPIENT_FAIL,
-       NOTI_RESOLVE_RECIPIENT_CANCEL,
-
-       NOTI_RENAME_MAILBOX_START                = 12000,
-       NOTI_RENAME_MAILBOX_FINISH,
-       NOTI_RENAME_MAILBOX_FAIL,
-       NOTI_RENAME_MAILBOX_CANCEL,
-
-       NOTI_ADD_MAILBOX_START                   = 12100,
-       NOTI_ADD_MAILBOX_FINISH,
-       NOTI_ADD_MAILBOX_FAIL,
-       NOTI_ADD_MAILBOX_CANCEL,
-
-       NOTI_DELETE_MAILBOX_START                = 12200,
-       NOTI_DELETE_MAILBOX_FINISH,
-       NOTI_DELETE_MAILBOX_FAIL,
-       NOTI_DELETE_MAILBOX_CANCEL,
-
-       NOTI_SYNC_IMAP_MAILBOX_LIST_START        = 12300,
-       NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH,
-       NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL,
-       NOTI_SYNC_IMAP_MAILBOX_LIST_CANCEL,
-
-       NOTI_DELETE_MAIL_START        = 12400,
-       NOTI_DELETE_MAIL_FINISH,
-       NOTI_DELETE_MAIL_FAIL,
-       NOTI_DELETE_MAIL_CANCEL,
-
-       NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FINISH  = 12500,
-       NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FAIL,
-
-       /* To be added more */
+    NOTI_SEND_START                          = 1002,    /**< Sending a mail started */
+    NOTI_SEND_FINISH                         = 1004,    /**< Sending a mail finished */
+    NOTI_SEND_FAIL                           = 1005,    /**< Sending a mail failed */
+    NOTI_SEND_CANCEL                         = 1003,    /**< Sending a mail canceled */
+
+    NOTI_DOWNLOAD_START                      = 2000,    /**< Syncing header started */
+    NOTI_DOWNLOAD_FINISH,                               /**< Syncing header finished */
+    NOTI_DOWNLOAD_FAIL,                                 /**< Syncing header failed */
+    NOTI_DOWNLOAD_CANCEL                     = 2004,    /**< Syncing header canceled */
+    NOTI_DOWNLOAD_NEW_MAIL                   = 2003,    /**< Deprecated */
+
+    NOTI_DOWNLOAD_BODY_START                 = 3000,    /**< Downloading mail body started */
+    NOTI_DOWNLOAD_BODY_FINISH                = 3002,    /**< Downloading mail body finished */
+    NOTI_DOWNLOAD_BODY_FAIL                  = 3004,    /**< Downloading mail body failed */
+    NOTI_DOWNLOAD_BODY_CANCEL                = 3003,    /**< Downloading mail body canceled */
+    NOTI_DOWNLOAD_MULTIPART_BODY             = 3001,    /**< Downloading multipart body is in progress */
+
+    NOTI_DOWNLOAD_ATTACH_START               = 4000,    /**< Downloading attachment started */
+    NOTI_DOWNLOAD_ATTACH_FINISH,                        /**< Downloading attachment finished */
+    NOTI_DOWNLOAD_ATTACH_FAIL,                          /**< Downloading attachment failed */
+    NOTI_DOWNLOAD_ATTACH_CANCEL,                        /**< Downloading attachment canceled */
+
+    NOTI_MAIL_DELETE_ON_SERVER_FAIL          = 5000,    /**< Deleting mails on server failed */
+    NOTI_MAIL_MOVE_ON_SERVER_FAIL,                      /**< Moving mails on server failed */
+
+    NOTI_SEARCH_ON_SERVER_START              = 6000,    /**< Searching mails on server started */
+    NOTI_SEARCH_ON_SERVER_FINISH             = 6001,    /**< Searching mails on server finished */
+    NOTI_SEARCH_ON_SERVER_FAIL               = 6002,    /**< Searching mails on server failed */
+    NOTI_SEARCH_ON_SERVER_CANCEL             = 6003,    /**< Searching mails on server canceled */
+
+    NOTI_VALIDATE_ACCOUNT_FINISH             = 7000,    /**< Validating an account finished */
+    NOTI_VALIDATE_ACCOUNT_FAIL,                         /**< Validating an account failed */
+    NOTI_VALIDATE_ACCOUNT_CANCEL,                       /**< Validating an account canceled */
+
+    NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH  = 8000,    /**< Validating and creating an account finished */
+    NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL,              /**< Validating and creating an account failed */
+    NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL,            /**< Validating and creating an account canceled */
+
+    NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH  = 9000,    /**< Validating and updating an account finished */
+    NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL,              /**< Validating and updating an account failed */
+    NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL,            /**< Validating and updating an account canceled */
+
+    NOTI_VALIDATE_CERTIFICATE_FINISH         = 10000,   /**< Validating a certificate finished */
+    NOTI_VALIDATE_CERTIFICATE_FAIL           = 10001,   /**< Validating a certificate failed */
+    NOTI_VALIDATE_CERTIFICATE_CANCEL         = 10002,   /**< Validating a certificate canceled */
+
+    NOTI_RESOLVE_RECIPIENT_START             = 11000,   /**< Resolving recipients started */
+    NOTI_RESOLVE_RECIPIENT_FINISH,                      /**< Resolving recipients finished */
+    NOTI_RESOLVE_RECIPIENT_FAIL,                        /**< Resolving recipients failed */
+    NOTI_RESOLVE_RECIPIENT_CANCEL,                      /**< Resolving recipients canceled */
+
+    NOTI_RENAME_MAILBOX_START                = 12000,   /**< Renaming a mailbox started */
+    NOTI_RENAME_MAILBOX_FINISH,                         /**< Renaming a mailbox finished */
+    NOTI_RENAME_MAILBOX_FAIL,                           /**< Renaming a mailbox failed */
+    NOTI_RENAME_MAILBOX_CANCEL,                         /**< Renaming a mailbox canceled */
+
+    NOTI_ADD_MAILBOX_START                   = 12100,   /**< Adding a mailbox started */
+    NOTI_ADD_MAILBOX_FINISH,                            /**< Adding a mailbox finished */
+    NOTI_ADD_MAILBOX_FAIL,                              /**< Adding a mailbox failed */
+    NOTI_ADD_MAILBOX_CANCEL,                            /**< Adding a mailbox canceled */
+
+    NOTI_DELETE_MAILBOX_START                = 12200,   /**< Removing a mailbox started */
+    NOTI_DELETE_MAILBOX_FINISH,                         /**< Removing a mailbox finished */
+    NOTI_DELETE_MAILBOX_FAIL,                           /**< Removing a mailbox failed */
+    NOTI_DELETE_MAILBOX_CANCEL,                         /**< Removing a mailbox canceled */
+
+    NOTI_SYNC_IMAP_MAILBOX_LIST_START        = 12300,   /**< Syncing mailbox list started */
+    NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH,                 /**< Syncing mailbox list finished */
+    NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL,                   /**< Syncing mailbox list failed */
+    NOTI_SYNC_IMAP_MAILBOX_LIST_CANCEL,                 /**< Syncing mailbox list canceled */
+
+    NOTI_DELETE_MAIL_START                   = 12400,   /**< Removing mails started */
+    NOTI_DELETE_MAIL_FINISH,                            /**< Removing mails finished */
+    NOTI_DELETE_MAIL_FAIL,                              /**< Removing mails failed */
+    NOTI_DELETE_MAIL_CANCEL,                            /**< Removing mails canceled */
+
+    NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FINISH  = 12500,    /**< Querying limitation of mail size to SMTP finished */
+    NOTI_QUERY_SMTP_MAIL_SIZE_LIMIT_FAIL,               /**< Querying limitation of mail size to SMTP failed */
+
+    /* To be added more */
 } email_noti_on_network_event;
 
+/**
+ * @brief Enumeration for the response to request.
+ * @since_tizen 2.3
+ */
 typedef enum
 {
-       RESPONSE_SUCCEEDED                       = 0,
-       RESPONSE_FAILED                          = 1,
-       RESPONSE_CANCELED                        = 2
-       /* To be added more */
+    RESPONSE_SUCCEEDED                       = 0,    /**< The request succeeded */
+    RESPONSE_FAILED                          = 1,    /**< The request failed */
+    RESPONSE_CANCELED                        = 2     /**< The request is canceled */
+    /* To be added more */
 } email_response_to_request;
 
 /**
- * This enumeration specifies the mail type of account.
+ * @brief Enumeration for the account mail type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_BIND_TYPE_DISABLE          = 0,          /**< Specifies the bind type for Disabled account.*/
-       EMAIL_BIND_TYPE_EM_CORE          = 1,          /**< Specifies the bind type for email-service .*/
+    EMAIL_BIND_TYPE_DISABLE          = 0,          /**< The bind type for Disabled account */
+    EMAIL_BIND_TYPE_EM_CORE          = 1,          /**< The bind type for email-service */
 } email_account_bind_t DEPRECATED;
 
 /**
- * This enumeration specifies the server type of account.
+ * @brief Enumeration for the account server type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_SERVER_TYPE_POP3           = 1,          /**< Specifies the POP3 Server.*/
-       EMAIL_SERVER_TYPE_IMAP4,                       /**< Specifies the IMAP4 Server.*/
-       EMAIL_SERVER_TYPE_SMTP,                        /**< Specifies the SMTP Server.*/
-       EMAIL_SERVER_TYPE_NONE,                        /**< Specifies the Local.*/
-       EMAIL_SERVER_TYPE_ACTIVE_SYNC,                 /** < Specifies the Active Sync.  */
+    EMAIL_SERVER_TYPE_POP3           = 1,          /**< The POP3 Server */
+    EMAIL_SERVER_TYPE_IMAP4,                       /**< The IMAP4 Server */
+    EMAIL_SERVER_TYPE_SMTP,                        /**< The SMTP Server */
+    EMAIL_SERVER_TYPE_NONE,                        /**< The local account */
+    EMAIL_SERVER_TYPE_ACTIVE_SYNC,                 /**< The Active Sync */
 } email_account_server_t;
 
 /**
- * This enumeration specifies the mode of retrieval.
+ * @brief Enumeration for the retrieval mode.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_IMAP4_RETRIEVAL_MODE_NEW   = 0,          /**< Specifies the retrieval mode for new email.*/
-       EMAIL_IMAP4_RETRIEVAL_MODE_ALL,                /**< Specifies the retrieval mode for all email.*/
+    EMAIL_IMAP4_RETRIEVAL_MODE_NEW   = 1,          /**< Retrieval mode for new email */
+    EMAIL_IMAP4_RETRIEVAL_MODE_ALL   = 2,          /**< Retrieval mode for all email */
+    EMAIL_IMAP4_IDLE_SUPPORTED       = 0x00100000  /**< Support for feature 'imap idle' */
 } email_imap4_retrieval_mode_t;
 
 /**
- * This enumeration specifies the filtering type.
+ * @brief Enumeration for the filtering type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_FILTER_FROM                = 1,          /**< Specifies the filtering of sender.*/
-       EMAIL_FILTER_SUBJECT             = 2,          /**< Specifies the filtering of email subject.*/
-       EMAIL_FILTER_BODY                = 4,          /**< Specifies the filterinf of email body.*/
-       EMAIL_PRIORITY_SENDER            = 8,          /**< Specifies the priority sender of email. */
+    EMAIL_FILTER_FROM                = 1,          /**< Filtering of sender */
+    EMAIL_FILTER_SUBJECT             = 2,          /**< Filtering of email subject */
+    EMAIL_FILTER_BODY                = 4,          /**< Filtering of email body */
+    EMAIL_PRIORITY_SENDER            = 8,          /**< Priority sender of email */
 } email_rule_type_t;
 
 
 /**
- * This enumeration specifies the rules for filtering type.
+ * @brief Enumeration for the rules for filtering type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       RULE_TYPE_INCLUDES             = 1,          /**< Specifies the filtering rule for includes.*/
-       RULE_TYPE_EXACTLY,                           /**< Specifies the filtering rule for Exactly same as.*/
+    RULE_TYPE_INCLUDES             = 1,          /**< Filtering rule for includes */
+    RULE_TYPE_EXACTLY,                           /**< Filtering rule for Exactly same as */
 } email_filtering_type_t;
 
 
 /**
- * This enumeration specifies the action for filtering type.
+ * @brief Enumeration for the action for filtering type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_FILTER_MOVE                = 1,          /**< Specifies the move of email.*/
-       EMAIL_FILTER_BLOCK               = 2,          /**< Specifies the block of email.*/
-       EMAIL_FILTER_DELETE              = 3,          /**< Specifies delete email.*/
+    EMAIL_FILTER_MOVE                = 1,          /**< Filter action is moving email */
+    EMAIL_FILTER_BLOCK               = 2,          /**< Filtering action is blocking email */
+    EMAIL_FILTER_DELETE              = 3,          /**< Filtering action is deleting email */
 } email_rule_action_t;
 
 /**
- * This enumeration specifies the email status.
+ * @brief Enumeration for the email status.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MAIL_STATUS_NONE           = 0,          /**< The Mail is in No Operation state */
-       EMAIL_MAIL_STATUS_RECEIVED,                    /**< The mail is a received mail.*/
-       EMAIL_MAIL_STATUS_SENT,                        /**< The mail is a sent mail.*/
-       EMAIL_MAIL_STATUS_SAVED,                       /**< The mail is a saved mail.*/
-       EMAIL_MAIL_STATUS_SAVED_OFFLINE,               /**< The mail is a saved mail in off-line mode.*/
-       EMAIL_MAIL_STATUS_SENDING,                     /**< The mail is being sent.*/
-       EMAIL_MAIL_STATUS_SEND_FAILURE,                /**< The mail is a mail to been failed to send.*/
-       EMAIL_MAIL_STATUS_SEND_CANCELED,               /**< The mail is a canceled mail.*/
-       EMAIL_MAIL_STATUS_SEND_WAIT,                   /**< The mail is a mail to be send .*/
-       EMAIL_MAIL_STATUS_SEND_SCHEDULED,              /**< The mail is a scheduled mail to be send later.*/
-       EMAIL_MAIL_STATUS_SEND_DELAYED,                /**< The mail is a delayed mail to be send later.*/
+    EMAIL_MAIL_STATUS_NONE           = 0,          /**< The Mail is in No Operation state */
+    EMAIL_MAIL_STATUS_RECEIVED,                    /**< The mail is a received mail */
+    EMAIL_MAIL_STATUS_SENT,                        /**< The mail is a sent mail */
+    EMAIL_MAIL_STATUS_SAVED,                       /**< The mail is a saved mail */
+    EMAIL_MAIL_STATUS_SAVED_OFFLINE,               /**< The mail is a saved mail in off-line mode */
+    EMAIL_MAIL_STATUS_SENDING,                     /**< The mail is being sent */
+    EMAIL_MAIL_STATUS_SEND_FAILURE,                /**< The mail is a sending failed mail */
+    EMAIL_MAIL_STATUS_SEND_CANCELED,               /**< The mail is a canceled mail */
+    EMAIL_MAIL_STATUS_SEND_WAIT,                   /**< The mail is waiting to be sent */
+    EMAIL_MAIL_STATUS_SEND_SCHEDULED,              /**< The mail is a scheduled mail to be sent later.*/
+    EMAIL_MAIL_STATUS_SEND_DELAYED,                /**< The mail is a delayed mail to be sent later */
+    EMAIL_MAIL_STATUS_NOTI_WAITED,                 /**< The mail is a waited notification */
 } email_mail_status_t;
 
 /**
- * This enumeration specifies the email priority.
+ * @brief Enumeration for the email priority.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MAIL_PRIORITY_HIGH         = 1,          /**< The priority is high.*/
-       EMAIL_MAIL_PRIORITY_NORMAL       = 3,          /**< The priority is normal.*/
-       EMAIL_MAIL_PRIORITY_LOW          = 5,          /**< The priority is low.*/
+    EMAIL_MAIL_PRIORITY_HIGH         = 1,          /**< The priority is high */
+    EMAIL_MAIL_PRIORITY_NORMAL       = 3,          /**< The priority is normal */
+    EMAIL_MAIL_PRIORITY_LOW          = 5,          /**< The priority is low */
 } email_mail_priority_t;
 
 /**
- * This enumeration specifies the email status.
+ * @brief Enumeration for the email status.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MAIL_REPORT_NONE           = 0x00,       /**< The mail isn't report mail.*/
-       EMAIL_MAIL_REPORT_REQUEST        = 0x03,       /* Deprecated */
-       EMAIL_MAIL_REPORT_DSN            = 0x04,       /**< The mail is a Delivery Status Notifications mail.*/
-       EMAIL_MAIL_REPORT_MDN            = 0x08,       /**< The mail is a Message Disposition Notifications mail.*/
-       EMAIL_MAIL_REQUEST_DSN           = 0x10,       /**< The mail requires Delivery Status Notifications.*/
-       EMAIL_MAIL_REQUEST_MDN           = 0x20,       /**< The mail requires Message Disposition Notifications.*/
+    EMAIL_MAIL_REPORT_NONE           = 0x00,       /**< The mail is not report mail */
+    EMAIL_MAIL_REPORT_REQUEST        = 0x03,       /**< The mail is to request report mail */
+    EMAIL_MAIL_REPORT_DSN            = 0x04,       /**< The mail is a Delivery Status Notifications mail */
+    EMAIL_MAIL_REPORT_MDN            = 0x08,       /**< The mail is a Message Disposition Notifications mail */
+    EMAIL_MAIL_REQUEST_DSN           = 0x10,       /**< The mail requires Delivery Status Notifications */
+    EMAIL_MAIL_REQUEST_MDN           = 0x20,       /**< The mail requires Message Disposition Notifications */
 } email_mail_report_t;
 
 /**
- * This enumeration specifies the DRM type
+ * @brief Enumeration for the DRM type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_ATTACHMENT_DRM_NONE        = 0,          /**< The mail isn't DRM file.*/
-       EMAIL_ATTACHMENT_DRM_OBJECT,                   /**< The mail is a DRM object.*/
-       EMAIL_ATTACHMENT_DRM_RIGHTS,                   /**< The mail is a DRM rights as XML format.*/
-       EMAIL_ATTACHMENT_DRM_DCF,                      /**< The mail is a DRM DCF.*/
+    EMAIL_ATTACHMENT_DRM_NONE        = 0,          /**< The mail is not DRM file */
+    EMAIL_ATTACHMENT_DRM_OBJECT,                   /**< The mail is a DRM object */
+    EMAIL_ATTACHMENT_DRM_RIGHTS,                   /**< The mail is a DRM rights as XML format */
+    EMAIL_ATTACHMENT_DRM_DCF,                      /**< The mail is a DRM DCF */
 } email_attachment_drm_t;
 
 /**
- * This enumeration specifies the mail type
+ * @brief Enumeration for the mail type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MAIL_TYPE_NORMAL                     = 0, /**< NOT a meeting request mail. A Normal mail */
-       EMAIL_MAIL_TYPE_MEETING_REQUEST            = 1, /**< a meeting request mail from a serve */
-       EMAIL_MAIL_TYPE_MEETING_RESPONSE           = 2, /**< a response mail about meeting request */
-       EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST = 3  /**< a originating mail about meeting request */
+    EMAIL_MAIL_TYPE_NORMAL                     = 0, /**< NOT a meeting request mail. A Normal mail */
+    EMAIL_MAIL_TYPE_MEETING_REQUEST            = 1, /**< A meeting request mail */
+    EMAIL_MAIL_TYPE_MEETING_RESPONSE           = 2, /**< A response mail about meeting request */
+    EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST = 3  /**< An originating mail about meeting request */
 } email_mail_type_t;
 
 /**
- * This enumeration specifies the meeting response type
+ * @brief Enumeration for the meeting response type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MEETING_RESPONSE_NONE                = 0, /**< NOT response */
-       EMAIL_MEETING_RESPONSE_ACCEPT              = 1, /**< The response is acceptance */
-       EMAIL_MEETING_RESPONSE_TENTATIVE           = 2, /**< The response is tentative */
-       EMAIL_MEETING_RESPONSE_DECLINE             = 3, /**< The response is decline */
-       EMAIL_MEETING_RESPONSE_REQUEST             = 4, /**< The response is request */
-       EMAIL_MEETING_RESPONSE_CANCEL              = 5, /**< The response is cancellation */
-       EMAIL_MEETING_RESPONSE_PROPOSE_NEW_TIME_TENTATIVE,
-       EMAIL_MEETING_RESPONSE_PROPOSE_NEW_TIME_DECLINE
+    EMAIL_MEETING_RESPONSE_NONE                = 0,     /**< NOT a response */
+    EMAIL_MEETING_RESPONSE_ACCEPT              = 1,     /**< The response is acceptance */
+    EMAIL_MEETING_RESPONSE_TENTATIVE           = 2,     /**< The response is tentative */
+    EMAIL_MEETING_RESPONSE_DECLINE             = 3,     /**< The response is decline */
+    EMAIL_MEETING_RESPONSE_REQUEST             = 4,     /**< The response is request */
+    EMAIL_MEETING_RESPONSE_CANCEL              = 5,     /**< The response is cancellation */
+    EMAIL_MEETING_RESPONSE_PROPOSE_NEW_TIME_TENTATIVE,  /**< The response proposes new time tentative */
+    EMAIL_MEETING_RESPONSE_PROPOSE_NEW_TIME_DECLINE     /**< The response proposes new time decline */
 } email_meeting_response_t;
 
 typedef enum
 {
-       EMAIL_ACTION_SEND_MAIL                     =  0,
-       EMAIL_ACTION_SYNC_HEADER                   =  1,
-       EMAIL_ACTION_DOWNLOAD_BODY                 =  2,
-       EMAIL_ACTION_DOWNLOAD_ATTACHMENT           =  3,
-       EMAIL_ACTION_DELETE_MAIL                   =  4,
-       EMAIL_ACTION_SEARCH_MAIL                   =  5,
-       EMAIL_ACTION_SAVE_MAIL                     =  6,
-       EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER      =  7,
-       EMAIL_ACTION_SYNC_FLAGS_FIELD_TO_SERVER    =  8,
-       EMAIL_ACTION_MOVE_MAIL                     =  9,
-       EMAIL_ACTION_CREATE_MAILBOX                = 10,
-       EMAIL_ACTION_DELETE_MAILBOX                = 11,
-       EMAIL_ACTION_SYNC_HEADER_OMA               = 12,
-       EMAIL_ACTION_VALIDATE_ACCOUNT              = 13,
-       EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT   = 14,
-       EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT   = 15,
-       EMAIL_ACTION_VALIDATE_ACCOUNT_EX           = 16,
-       EMAIL_ACTION_UPDATE_MAIL                   = 30,
-       EMAIL_ACTION_SET_MAIL_SLOT_SIZE            = 31,
-       EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED = 32,
-       EMAIL_ACTION_SEARCH_ON_SERVER              = 33,
-       EMAIL_ACTION_MOVE_MAILBOX                  = 34,
-       EMAIL_ACTION_SENDING_MAIL                  = 35,
-       EMAIL_ACTION_NUM,
+    EMAIL_ACTION_SEND_MAIL                     =  0,    /**< Action type for sending mail */
+    EMAIL_ACTION_SYNC_HEADER                   =  1,    /**< Action type for syncing header */
+    EMAIL_ACTION_DOWNLOAD_BODY                 =  2,    /**< Action type for downloading body */
+    EMAIL_ACTION_DOWNLOAD_ATTACHMENT           =  3,    /**< Action type for downloading attachment */
+    EMAIL_ACTION_DELETE_MAIL                   =  4,    /**< Action type for deleting mail */
+    EMAIL_ACTION_SEARCH_MAIL                   =  5,    /**< Action type for searching mail */
+    EMAIL_ACTION_SAVE_MAIL                     =  6,    /**< Action type for saving mail */
+    EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER      =  7,    /**< Action type for syncing mail flag */
+    EMAIL_ACTION_SYNC_FLAGS_FIELD_TO_SERVER    =  8,    /**< Action type for syncing flags field */
+    EMAIL_ACTION_MOVE_MAIL                     =  9,    /**< Action type for moving mail */
+    EMAIL_ACTION_CREATE_MAILBOX                = 10,    /**< Action type for creating mailbox */
+    EMAIL_ACTION_DELETE_MAILBOX                = 11,    /**< Action type for deleting mailbox */
+    EMAIL_ACTION_SYNC_HEADER_OMA               = 12,    /**< Action type for syncing header by oma-emn */
+    EMAIL_ACTION_VALIDATE_ACCOUNT              = 13,    /**< Action type for validating account */
+    EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT   = 14,    /**< Action type for validating and creating account */
+    EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT   = 15,    /**< Action type for validating and updating account */
+    EMAIL_ACTION_VALIDATE_ACCOUNT_EX           = 16,    /**< Action type for validating account */
+    EMAIL_ACTION_UPDATE_MAIL                   = 30,    /**< Action type for updating account */
+    EMAIL_ACTION_SET_MAIL_SLOT_SIZE            = 31,    /**< Action type for setting mail slot size */
+    EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED = 32,    /**< Action type for expunge mails deleted flagged */
+    EMAIL_ACTION_SEARCH_ON_SERVER              = 33,    /**< Action type for searching on server */
+    EMAIL_ACTION_MOVE_MAILBOX                  = 34,    /**< Action type for moving mailbox */
+    EMAIL_ACTION_SENDING_MAIL                  = 35,    /**< Action type for sending mail */
+    EMAIL_ACTION_NUM,                                   /**< end of email_action_t */
 } email_action_t;
 
 /**
- * This enumeration specifies the status of getting envelope list.
+ * @brief Enumeration for the status of getting an envelope list.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_LIST_NONE                  = 0,
-       EMAIL_LIST_WAITING,
-       EMAIL_LIST_PREPARE,                            /**< Specifies the preparation.*/
-       EMAIL_LIST_CONNECTION_START,                   /**< Specifies the connection start.*/
-       EMAIL_LIST_CONNECTION_SUCCEED,                 /**< Specifies the success of connection.*/
-       EMAIL_LIST_CONNECTION_FINISH,                  /**< Specifies the connection finish.*/
-       EMAIL_LIST_CONNECTION_FAIL,                    /**< Specifies the connection failure.*/
-       EMAIL_LIST_START,                              /**< Specifies the getting start.*/
-       EMAIL_LIST_PROGRESS,                           /**< Specifies the status of getting.*/
-       EMAIL_LIST_FINISH,                             /**< Specifies the getting complete.*/
-       EMAIL_LIST_FAIL,                               /**< Specifies the download failure.*/
+    EMAIL_LIST_NONE                  = 0,          /**< Initial status */
+    EMAIL_LIST_WAITING,                            /**< Waiting status */
+    EMAIL_LIST_PREPARE,                            /**< Preparation status */
+    EMAIL_LIST_CONNECTION_START,                   /**< Connection start */
+    EMAIL_LIST_CONNECTION_SUCCEED,                 /**< Connection success */
+    EMAIL_LIST_CONNECTION_FINISH,                  /**< Connection finish */
+    EMAIL_LIST_CONNECTION_FAIL,                    /**< Connection failure */
+    EMAIL_LIST_START,                              /**< Getting the list started */
+    EMAIL_LIST_PROGRESS,                           /**< The progress status of getting */
+    EMAIL_LIST_FINISH,                             /**< Getting the list completed */
+    EMAIL_LIST_FAIL,                               /**< Getting the list failed */
 } email_envelope_list_status_t;
 
 /**
- * This enumeration specifies the downloaded status of email.
+ * @brief Enumeration for the downloaded status of an email.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_DOWNLOAD_NONE              = 0,
-       EMAIL_DOWNLOAD_WAITING,
-       EMAIL_DOWNLOAD_PREPARE,                        /**< Specifies the preparation.*/
-       EMAIL_DOWNLOAD_CONNECTION_START,               /**< Specifies the connection start.*/
-       EMAIL_DOWNLOAD_CONNECTION_SUCCEED,             /**< Specifies the success of connection.*/
-       EMAIL_DOWNLOAD_CONNECTION_FINISH,              /**< Specifies the connection finish.*/
-       EMAIL_DOWNLOAD_CONNECTION_FAIL,                /**< Specifies the connection failure.*/
-       EMAIL_DOWNLOAD_START,                          /**< Specifies the download start.*/
-       EMAIL_DOWNLOAD_PROGRESS,                       /**< Specifies the status of download.*/
-       EMAIL_DOWNLOAD_FINISH,                         /**< Specifies the download complete.*/
-       EMAIL_DOWNLOAD_FAIL,                           /**< Specifies the download failure.*/
+    EMAIL_DOWNLOAD_NONE              = 0,          /**< Initial status */
+    EMAIL_DOWNLOAD_WAITING,                        /**< Download is waiting */
+    EMAIL_DOWNLOAD_PREPARE,                        /**< Preparing for download */
+    EMAIL_DOWNLOAD_CONNECTION_START,               /**< Connection start */
+    EMAIL_DOWNLOAD_CONNECTION_SUCCEED,             /**< Connection success */
+    EMAIL_DOWNLOAD_CONNECTION_FINISH,              /**< Connection finish */
+    EMAIL_DOWNLOAD_CONNECTION_FAIL,                /**< Connection failure */
+    EMAIL_DOWNLOAD_START,                          /**< Download start */
+    EMAIL_DOWNLOAD_PROGRESS,                       /**< Progress of download */
+    EMAIL_DOWNLOAD_FINISH,                         /**< Download complete */
+    EMAIL_DOWNLOAD_FAIL,                           /**< Download failure */
 } email_download_status_t;
 
 /**
- * This enumeration specifies the status of sending email.
+ * @brief Enumeration for the status of sending an email.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_SEND_NONE                  = 0,
-       EMAIL_SEND_WAITING,
-       EMAIL_SEND_PREPARE,                            /**< Specifies the preparation.*/
-       EMAIL_SEND_CONNECTION_START,                   /**< Specifies the connection start.*/
-       EMAIL_SEND_CONNECTION_SUCCEED,                 /**< Specifies the success of connection.*/
-       EMAIL_SEND_CONNECTION_FINISH,                  /**< Specifies the connection finish.*/
-       EMAIL_SEND_CONNECTION_FAIL,                    /**< Specifies the connection failure.*/
-       EMAIL_SEND_START,                              /**< Specifies the sending start.*/
-       EMAIL_SEND_PROGRESS,                           /**< Specifies the status of sending.*/
-       EMAIL_SEND_FINISH,                             /**< Specifies the sending complete.*/
-       EMAIL_SEND_FAIL,                               /**< Specifies the sending failure.*/
-       EMAIL_SAVE_WAITING,                            /**< Specfies the Waiting of Sync */
+    EMAIL_SEND_NONE                  = 0,          /**< Initial status */
+    EMAIL_SEND_WAITING,                            /**< Waiting to send */
+    EMAIL_SEND_PREPARE,                            /**< Preparing to send */
+    EMAIL_SEND_CONNECTION_START,                   /**< Starting the send connection */
+    EMAIL_SEND_CONNECTION_SUCCEED,                 /**< Send connection success */
+    EMAIL_SEND_CONNECTION_FINISH,                  /**< Send connection finish */
+    EMAIL_SEND_CONNECTION_FAIL,                    /**< Send connection failure */
+    EMAIL_SEND_START,                              /**< Start sending */
+    EMAIL_SEND_PROGRESS,                           /**< Sending status */
+    EMAIL_SEND_FINISH,                             /**< Sending complete */
+    EMAIL_SEND_FAIL,                               /**< Sending failure */
+    EMAIL_SAVE_WAITING,                            /**< Waiting to save */
 } email_send_status_t;
 
 typedef enum
 {
-       EMAIL_SYNC_NONE                  = 0,
-       EMAIL_SYNC_WAITING,
-       EMAIL_SYNC_PREPARE,
-       EMAIL_SYNC_CONNECTION_START,
-       EMAIL_SYNC_CONNECTION_SUCCEED,
-       EMAIL_SYNC_CONNECTION_FINISH,
-       EMAIL_SYNC_CONNECTION_FAIL,
-       EMAIL_SYNC_START,
-       EMAIL_SYNC_PROGRESS,
-       EMAIL_SYNC_FINISH,
-       EMAIL_SYNC_FAIL,
+    EMAIL_SYNC_NONE                  = 0,          /**< Initial status */
+    EMAIL_SYNC_WAITING,                            /**< Waiting to sync */
+    EMAIL_SYNC_PREPARE,                            /**< Preparing to sync */
+    EMAIL_SYNC_CONNECTION_START,                   /**< Starting sync connection */
+    EMAIL_SYNC_CONNECTION_SUCCEED,                 /**< Sync connection success */
+    EMAIL_SYNC_CONNECTION_FINISH,                  /**< Sync connection finish */
+    EMAIL_SYNC_CONNECTION_FAIL,                    /**< Sync connection failure*/
+    EMAIL_SYNC_START,                              /**< Start syncing */
+    EMAIL_SYNC_PROGRESS,                           /**< Sync status */
+    EMAIL_SYNC_FINISH,                             /**< Sync complete */
+    EMAIL_SYNC_FAIL,                               /**< Sync failure */
 } email_sync_status_t;
 
 /**
-* This enumeration specifies the deleting status of email.
+* @brief Enumeration for the deleting status of an email.
+* @since_tizen 2.3
 */
 typedef enum
 {
-       EMAIL_DELETE_NONE                = 0,
-       EMAIL_DELETE_WAITING,
-       EMAIL_DELETE_PREPARE,                          /**< Specifies the preparation.*/
-       EMAIL_DELETE_CONNECTION_START,                 /**< Specifies the connection start.*/
-       EMAIL_DELETE_CONNECTION_SUCCEED,               /**< Specifies the success of connection.*/
-       EMAIL_DELETE_CONNECTION_FINISH,                /**< Specifies the connection finish.*/
-       EMAIL_DELETE_CONNECTION_FAIL,                  /**< Specifies the connection failure.*/
-       EMAIL_DELETE_START,                            /**< Specifies the deletion start.*/
-       EMAIL_DELETE_PROGRESS,                         /**< Specifies the status of deleting.*/
-       EMAIL_DELETE_SERVER_PROGRESS,                  /**< Specifies the status of server deleting.*/
-       EMAIL_DELETE_LOCAL_PROGRESS,                   /**< Specifies the status of local deleting.*/
-       EMAIL_DELETE_FINISH,                           /**< Specifies the deletion complete.*/
-       EMAIL_DELETE_FAIL,                             /**< Specifies the deletion failure.*/
+    EMAIL_DELETE_NONE                = 0,          /**< Initial status */
+    EMAIL_DELETE_WAITING,                          /**< Waiting to delete */
+    EMAIL_DELETE_PREPARE,                          /**< Preparing to delete */
+    EMAIL_DELETE_CONNECTION_START,                 /**< Starting delete connection */
+    EMAIL_DELETE_CONNECTION_SUCCEED,               /**< Delete connection success */
+    EMAIL_DELETE_CONNECTION_FINISH,                /**< Delete connection finish */
+    EMAIL_DELETE_CONNECTION_FAIL,                  /**< Delete connection failure */
+    EMAIL_DELETE_START,                            /**< Deletion start */
+    EMAIL_DELETE_PROGRESS,                         /**< Delete status */
+    EMAIL_DELETE_SERVER_PROGRESS,                  /**< Server deleting status */
+    EMAIL_DELETE_LOCAL_PROGRESS,                   /**< Local deleting status*/
+    EMAIL_DELETE_FINISH,                           /**< Deletion complete */
+    EMAIL_DELETE_FAIL,                             /**< Deletion failure */
 } email_delete_status_t;
 
 /**
-* This enumeration specifies the status of validating account
+* @brief Enumeration for the status of validating an account.
+* @since_tizen 2.3
 */
 typedef enum
 {
-       EMAIL_VALIDATE_ACCOUNT_NONE = 0,
-       EMAIL_VALIDATE_ACCOUNT_WAITING,
-       EMAIL_VALIDATE_ACCOUNT_PREPARE,                /**< Specifies the preparation.*/
-       EMAIL_VALIDATE_ACCOUNT_CONNECTION_START,       /**< Specifies the connection start.*/
-       EMAIL_VALIDATE_ACCOUNT_CONNECTION_SUCCEED,     /**< Specifies the success of connection.*/
-       EMAIL_VALIDATE_ACCOUNT_CONNECTION_FINISH,      /**< Specifies the connection finish.*/
-       EMAIL_VALIDATE_ACCOUNT_CONNECTION_FAIL,        /**< Specifies the connection failure.*/
-       EMAIL_VALIDATE_ACCOUNT_START,                  /**< Specifies the getting start.*/
-       EMAIL_VALIDATE_ACCOUNT_PROGRESS,               /**< Specifies the status of getting.*/
-       EMAIL_VALIDATE_ACCOUNT_FINISH,                 /**< Specifies the getting complete.*/
-       EMAIL_VALIDATE_ACCOUNT_FAIL,                   /**< Specifies the validation failure.*/
+    EMAIL_VALIDATE_ACCOUNT_NONE = 0,               /**< Initial status */
+    EMAIL_VALIDATE_ACCOUNT_WAITING,                /**< Waiting to validate account */
+    EMAIL_VALIDATE_ACCOUNT_PREPARE,                /**< Preparing to validate account */
+    EMAIL_VALIDATE_ACCOUNT_CONNECTION_START,       /**< Starting validate account connection */
+    EMAIL_VALIDATE_ACCOUNT_CONNECTION_SUCCEED,     /**< Validate account connection success */
+    EMAIL_VALIDATE_ACCOUNT_CONNECTION_FINISH,      /**< Validate account connection finish */
+    EMAIL_VALIDATE_ACCOUNT_CONNECTION_FAIL,        /**< Validate account connection failure */
+    EMAIL_VALIDATE_ACCOUNT_START,                  /**< Start validating account */
+    EMAIL_VALIDATE_ACCOUNT_PROGRESS,               /**< Account validation status */
+    EMAIL_VALIDATE_ACCOUNT_FINISH,                 /**< Account validation complete */
+    EMAIL_VALIDATE_ACCOUNT_FAIL,                   /**< Account validation failure.*/
 } email_validate_account_status_t;
 
+/**
+* @brief Enumeration for the status of setting slot size.
+* @since_tizen 2.3
+*/
 typedef enum
 {
-       EMAIL_SET_SLOT_SIZE_NONE         = 0,
-       EMAIL_SET_SLOT_SIZE_WAITING,
-       EMAIL_SET_SLOT_SIZE_START,
-       EMAIL_SET_SLOT_SIZE_FINISH,
-       EMAIL_SET_SLOT_SIZE_FAIL,
+    EMAIL_SET_SLOT_SIZE_NONE         = 0,          /**< Initial status */
+    EMAIL_SET_SLOT_SIZE_WAITING,                   /**< Waiting status*/
+    EMAIL_SET_SLOT_SIZE_START,                     /**< Task started  */
+    EMAIL_SET_SLOT_SIZE_FINISH,                    /**< Task finished */
+    EMAIL_SET_SLOT_SIZE_FAIL,                      /**< Task failed */
 }email_set_slot_size_status_e;
 
+/**
+* @brief Enumeration for the status of expunging mails.
+* @since_tizen 2.3
+*/
 typedef enum
 {
-       EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_NONE         = 0,
-       EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_WAITING,
-       EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_START,
-       EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FINISH,
-       EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL,
+    EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_NONE         = 0,    /**< Initial status */
+    EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_WAITING,             /**< Waiting status*/
+    EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_START,               /**< Task started  */
+    EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FINISH,              /**< Task finished */
+    EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL,                /**< Task failed */
 }email_expunge_mails_deleted_flagged_status_e;
 
+/**
+* @brief Enumeration for the status of moving mails.
+* @since_tizen 2.3
+*/
 typedef enum
 {
-       EMAIL_SEARCH_ON_SERVER_NONE         = 0,
-       EMAIL_SEARCH_ON_SERVER_WAITING,
-       EMAIL_SEARCH_ON_SERVER_START,
-       EMAIL_SEARCH_ON_SERVER_FINISH,
-       EMAIL_SEARCH_ON_SERVER_FAIL,
-}email_search_on_server_status_e;
-
-typedef enum
-{
-       EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_NONE         = 0,
-       EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_WAITING,
-       EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_START,
-       EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FINISH,
-       EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL,
+    EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_NONE         = 0,  /**< Initial status */
+    EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_WAITING,           /**< Waiting status*/
+    EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_START,             /**< Task started  */
+    EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FINISH,            /**< Task finished */
+    EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL,              /**< Task failed */
 }email_move_mailbox_status_e;
 
+/**
+* @brief Enumeration for the status of updating mails.
+* @since_tizen 2.3
+*/
 typedef enum
 {
-       EMAIL_UPDATE_MAIL_NONE         = 0,
-       EMAIL_UPDATE_MAIL_WAITING,
-       EMAIL_UPDATE_MAIL_START,
-       EMAIL_UPDATE_MAIL_FINISH,
-       EMAIL_UPDATE_MAIL_FAIL,
+    EMAIL_UPDATE_MAIL_NONE         = 0,    /**< Initial status */
+    EMAIL_UPDATE_MAIL_WAITING,             /**< Waiting status*/
+    EMAIL_UPDATE_MAIL_START,               /**< Task started  */
+    EMAIL_UPDATE_MAIL_FINISH,              /**< Task finished */
+    EMAIL_UPDATE_MAIL_FAIL,                /**< Task failed */
 }email_update_mail_status_e;
 
 /**
-* This enumeration specifies the type of mailbox
+* @brief Enumeration for the mailbox type.
+* @since_tizen 2.3
 */
 typedef enum
 {
-       EMAIL_MAILBOX_TYPE_NONE          = 0,         /**< Unspecified mailbox type*/
-       EMAIL_MAILBOX_TYPE_INBOX         = 1,         /**< Specified inbox type*/
-       EMAIL_MAILBOX_TYPE_SENTBOX       = 2,         /**< Specified sent box type*/
-       EMAIL_MAILBOX_TYPE_TRASH         = 3,         /**< Specified trash type*/
-       EMAIL_MAILBOX_TYPE_DRAFT         = 4,         /**< Specified draft box type*/
-       EMAIL_MAILBOX_TYPE_SPAMBOX       = 5,         /**< Specified spam box type*/
-       EMAIL_MAILBOX_TYPE_OUTBOX        = 6,         /**< Specified outbox type*/
-       EMAIL_MAILBOX_TYPE_ALL_EMAILS    = 7,         /**< Specified all emails type of gmail*/
-       EMAIL_MAILBOX_TYPE_SEARCH_RESULT = 8,         /**< Specified mailbox type for result of search on server */
-       EMAIL_MAILBOX_TYPE_FLAGGED       = 9,         /**< Specified flagged mailbox type on gmail */
-       EMAIL_MAILBOX_TYPE_USER_DEFINED  = 0xFF,      /**< Specified mailbox type for all other mailboxes */
+    EMAIL_MAILBOX_TYPE_NONE          = 0,         /**< Unspecified mailbox type */
+    EMAIL_MAILBOX_TYPE_INBOX         = 1,         /**< Specified inbox type */
+    EMAIL_MAILBOX_TYPE_SENTBOX       = 2,         /**< Specified sent box type */
+    EMAIL_MAILBOX_TYPE_TRASH         = 3,         /**< Specified trash type */
+    EMAIL_MAILBOX_TYPE_DRAFT         = 4,         /**< Specified draft box type */
+    EMAIL_MAILBOX_TYPE_SPAMBOX       = 5,         /**< Specified spam box type */
+    EMAIL_MAILBOX_TYPE_OUTBOX        = 6,         /**< Specified outbox type */
+    EMAIL_MAILBOX_TYPE_ALL_EMAILS    = 7,         /**< Specified all emails type of gmail */
+    EMAIL_MAILBOX_TYPE_SEARCH_RESULT = 8,         /**< Specified mailbox type for result of search on server */
+    EMAIL_MAILBOX_TYPE_FLAGGED       = 9,         /**< Specified flagged mailbox type on gmail */
+    EMAIL_MAILBOX_TYPE_USER_DEFINED  = 0xFF,      /**< Specified mailbox type for all other mailboxes */
 }email_mailbox_type_e;
 
+
+/** @brief Enumeration for the sync order.
+ * @since_tizen 2.3 */
+
+
 typedef enum
 {
-       EMAIL_SYNC_LATEST_MAILS_FIRST    = 0,
-       EMAIL_SYNC_OLDEST_MAILS_FIRST,
-       EMAIL_SYNC_ALL_MAILBOX_50_MAILS,
+    EMAIL_SYNC_LATEST_MAILS_FIRST    = 0,    /**< Download latest mails first */
+    EMAIL_SYNC_OLDEST_MAILS_FIRST,           /**< Download oldest mails first */
+    EMAIL_SYNC_ALL_MAILBOX_50_MAILS,         /**< Download latest 50 mails only */
 } EMAIL_RETRIEVE_MODE;
 
-/*  event type */
+/** @brief Enumeration for the event type.
+ * @since_tizen 2.3 */
 typedef enum
 {
-       EMAIL_EVENT_NONE                            =  0,
-       EMAIL_EVENT_SYNC_HEADER                     =  1,          /*  synchronize mail headers with server (network used) */
-       EMAIL_EVENT_DOWNLOAD_BODY                   =  2,          /*  download mail body from server (network used)*/
-       EMAIL_EVENT_DOWNLOAD_ATTACHMENT             =  3,          /*  download mail attachment from server (network used) */
-       EMAIL_EVENT_SEND_MAIL                       =  4,          /*  send a mail (network used) */
-       EMAIL_EVENT_SEND_MAIL_SAVED                 =  5,          /*  send all mails in 'outbox' box (network used) */
-       EMAIL_EVENT_SYNC_IMAP_MAILBOX               =  6,          /*  download imap mailboxes from server (network used) */
-       EMAIL_EVENT_DELETE_MAIL                     =  7,          /*  delete mails (network unused) */
-       EMAIL_EVENT_DELETE_MAIL_ALL                 =  8,          /*  delete all mails (network unused) */
-       EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER        =  9,          /*  sync mail flag to server */
-       EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER      = 10,          /*  sync a field of flags to server */
-       EMAIL_EVENT_SAVE_MAIL                       = 11,          /*  add mail on server */
-       EMAIL_EVENT_MOVE_MAIL                       = 12,          /*  move mails to specific mailbox on server */
-       EMAIL_EVENT_CREATE_MAILBOX                  = 13,
-       EMAIL_EVENT_UPDATE_MAILBOX                  = 14,
-       EMAIL_EVENT_DELETE_MAILBOX                  = 15,
-       EMAIL_EVENT_ISSUE_IDLE                      = 16,
-       EMAIL_EVENT_SYNC_HEADER_OMA                 = 17,
-       EMAIL_EVENT_VALIDATE_ACCOUNT                = 18,
-       EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT     = 19,
-       EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT     = 20,
-       EMAIL_EVENT_SEARCH_ON_SERVER                = 21,
-       EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER   = 22,
-       EMAIL_EVENT_VALIDATE_ACCOUNT_EX             = 23,
-       EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT      = 24,
-
-       EMAIL_EVENT_ADD_MAIL                        = 10001,       /*  Deprecated */
-       EMAIL_EVENT_UPDATE_MAIL_OLD                 = 10002,       /*  Deprecated */
-       EMAIL_EVENT_UPDATE_MAIL                     = 10003,
-       EMAIL_EVENT_SET_MAIL_SLOT_SIZE              = 20000,
-       EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED   = 20001,
-
-/*     EMAIL_EVENT_LOCAL_ACTIVITY,                     __LOCAL_ACTIVITY_ */
-
-       EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD      = 20002,       /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ supported */
-       EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD    = 20003,       /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ supported */
-       EMAIL_EVENT_GET_PASSWORD_LENGTH             = 20004,       /*  get password length of an account */
+    EMAIL_EVENT_NONE                            =  0,          /**< Initial value of #email_event_type_t */
+    EMAIL_EVENT_SYNC_HEADER                     =  1,          /**< Synchronize mail headers with server (network used) */
+    EMAIL_EVENT_DOWNLOAD_BODY                   =  2,          /**< Download mail body from server (network used)*/
+    EMAIL_EVENT_DOWNLOAD_ATTACHMENT             =  3,          /**< Download mail attachment from server (network used) */
+    EMAIL_EVENT_SEND_MAIL                       =  4,          /**< Send a mail (network used) */
+    EMAIL_EVENT_SEND_MAIL_SAVED                 =  5,          /**< Send all mails in 'outbox' (network used) */
+    EMAIL_EVENT_SYNC_IMAP_MAILBOX               =  6,          /**< Download IMAP mailboxes from server (network used) */
+    EMAIL_EVENT_DELETE_MAIL                     =  7,          /**< Delete mails (network not used) */
+    EMAIL_EVENT_DELETE_MAIL_ALL                 =  8,          /**< Delete all mails (network not used) */
+    EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER        =  9,          /**< Sync mail flag to server */
+    EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER      = 10,          /**< Sync a field of flags to server */
+    EMAIL_EVENT_SAVE_MAIL                       = 11,          /**< Add mail on server */
+    EMAIL_EVENT_MOVE_MAIL                       = 12,          /**< Move mails to specific mailbox on server */
+    EMAIL_EVENT_CREATE_MAILBOX                  = 13,          /**< Create a mailbox */
+    EMAIL_EVENT_UPDATE_MAILBOX                  = 14,          /**< Update a mailbox */
+    EMAIL_EVENT_DELETE_MAILBOX                  = 15,          /**< Delete a mailbox */
+    EMAIL_EVENT_ISSUE_IDLE                      = 16,          /**< Deprecated */
+    EMAIL_EVENT_SYNC_HEADER_OMA                 = 17,          /**< Sync mail headers by OMA-EMN */
+    EMAIL_EVENT_VALIDATE_ACCOUNT                = 18,          /**< Validate account */
+    EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT     = 19,          /**< Validate and create account */
+    EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT     = 20,          /**< Validate and update account */
+    EMAIL_EVENT_SEARCH_ON_SERVER                = 21,          /**< Search mails on server */
+    EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER   = 22,          /**< Rename a mailbox on imap server */
+    EMAIL_EVENT_VALIDATE_ACCOUNT_EX             = 23,          /**< Validate account */
+    EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT      = 24,          /**< Query limitation of mail size to SMTP server */
+
+    EMAIL_EVENT_ADD_MAIL                        = 10001,       /**< Deprecated */
+    EMAIL_EVENT_UPDATE_MAIL_OLD                 = 10002,       /**< Deprecated */
+    EMAIL_EVENT_UPDATE_MAIL                     = 10003,       /**< Update a mail */
+    EMAIL_EVENT_SET_MAIL_SLOT_SIZE              = 20000,       /**< Set mail slot size */
+    EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED   = 20001,       /**< Expunge mails deleted flagged */
+
+/*  EMAIL_EVENT_LOCAL_ACTIVITY,                     __LOCAL_ACTIVITY_ */
+
+    EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD      = 20002,       /**< Download body partially */
+    EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD    = 20003,       /**< Deprecated */
+    EMAIL_EVENT_GET_PASSWORD_LENGTH             = 20004,       /**< Get password length of an account */
 } email_event_type_t;
 
-/*  event status */
+/** @brief Enumeration for the event status.
+ * @since_tizen 2.3 */
 typedef enum
 {
-       EMAIL_EVENT_STATUS_UNUSED        = 0,
-       EMAIL_EVENT_STATUS_WAIT,
-       EMAIL_EVENT_STATUS_STARTED,
-       EMAIL_EVENT_STATUS_CANCELED,
+    EMAIL_EVENT_STATUS_UNUSED        = 0,    /**< Initial status of event */
+    EMAIL_EVENT_STATUS_WAIT,                 /**< Waiting status */
+    EMAIL_EVENT_STATUS_STARTED,              /**< Event handling is started */
+    EMAIL_EVENT_STATUS_CANCELED,             /**< Event handling is canceled */
 } email_event_status_type_t;
 
 
-/* sorting_orde */
+/** @brief Enumeration for the srting type.
+ * @since_tizen 2.3 */
 typedef enum
 {
-       EMAIL_SORT_DATETIME_HIGH         = 0,
-       EMAIL_SORT_DATETIME_LOW,
-       EMAIL_SORT_SENDER_HIGH,
-       EMAIL_SORT_SENDER_LOW,
-       EMAIL_SORT_RCPT_HIGH,
-       EMAIL_SORT_RCPT_LOW,
-       EMAIL_SORT_SUBJECT_HIGH,
-       EMAIL_SORT_SUBJECT_LOW,
-       EMAIL_SORT_PRIORITY_HIGH,
-       EMAIL_SORT_PRIORITY_LOW,
-       EMAIL_SORT_ATTACHMENT_HIGH,
-       EMAIL_SORT_ATTACHMENT_LOW,
-       EMAIL_SORT_FAVORITE_HIGH,
-       EMAIL_SORT_FAVORITE_LOW,
-       EMAIL_SORT_MAILBOX_ID_HIGH,
-       EMAIL_SORT_MAILBOX_ID_LOW,
-       EMAIL_SORT_FLAGGED_FLAG_HIGH,
-       EMAIL_SORT_FLAGGED_FLAG_LOW,
-       EMAIL_SORT_SEEN_FLAG_HIGH,
-       EMAIL_SORT_SEEN_FLAG_LOW,
-       EMAIL_SORT_END,
+    EMAIL_SORT_DATETIME_HIGH         = 0,    /**< Sort mails by datetime ascending order */
+    EMAIL_SORT_DATETIME_LOW,                 /**< Sort mails by datetime descending order */
+    EMAIL_SORT_SENDER_HIGH,                  /**< Sort mails by sender ascending order */
+    EMAIL_SORT_SENDER_LOW,                   /**< Sort mails by sender descending order */
+    EMAIL_SORT_RCPT_HIGH,                    /**< Sort mails by recipient ascending order */
+    EMAIL_SORT_RCPT_LOW,                     /**< Sort mails by recipient descending order */
+    EMAIL_SORT_SUBJECT_HIGH,                 /**< Sort mails by subject ascending order */
+    EMAIL_SORT_SUBJECT_LOW,                  /**< Sort mails by subject descending order */
+    EMAIL_SORT_PRIORITY_HIGH,                /**< Sort mails by priority ascending order */
+    EMAIL_SORT_PRIORITY_LOW,                 /**< Sort mails by priority descending order */
+    EMAIL_SORT_ATTACHMENT_HIGH,              /**< Sort mails by attachment ascending order */
+    EMAIL_SORT_ATTACHMENT_LOW,               /**< Sort mails by attachment descending order */
+    EMAIL_SORT_FAVORITE_HIGH,                /**< Sort mails by favorite ascending order */
+    EMAIL_SORT_FAVORITE_LOW,                 /**< Sort mails by favorite descending order */
+    EMAIL_SORT_MAILBOX_ID_HIGH,              /**< Sort mails by mailbox ID ascending order */
+    EMAIL_SORT_MAILBOX_ID_LOW,               /**< Sort mails by mailbox ID descending order */
+    EMAIL_SORT_FLAGGED_FLAG_HIGH,            /**< Sort mails by flagged flag ID ascending order */
+    EMAIL_SORT_FLAGGED_FLAG_LOW,             /**< Sort mails by flagged flag ID descending order */
+    EMAIL_SORT_SEEN_FLAG_HIGH,               /**< Sort mails by seen flag ID ascending order */
+    EMAIL_SORT_SEEN_FLAG_LOW,                /**< Sort mails by seen flag ID descending order */
+    EMAIL_SORT_END,                          /**< end of #email_sort_type_t */
 }email_sort_type_t;
 
 typedef enum
 {
-       EMAIL_MAILBOX_SORT_BY_NAME_ASC  = 0,
-       EMAIL_MAILBOX_SORT_BY_NAME_DSC,
-       EMAIL_MAILBOX_SORT_BY_TYPE_ASC,
-       EMAIL_MAILBOX_SORT_BY_TYPE_DSC,
+    EMAIL_MAILBOX_SORT_BY_NAME_ASC  = 0,  /**< Sort mailbox by name ascending order */
+    EMAIL_MAILBOX_SORT_BY_NAME_DSC,       /**< Sort mailbox by name descending order */
+    EMAIL_MAILBOX_SORT_BY_TYPE_ASC,       /**< Sort mailbox by type ascending order */
+    EMAIL_MAILBOX_SORT_BY_TYPE_DSC,       /**< Sort mailbox by type descending order */
 } email_mailbox_sort_type_t;
 
 
 /**
-* This enumeration specifies the priority.
+* @brief Enumeration for the priority.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_PRIORITY_HIGH       = 1,          /**< Specifies the high priority.*/
-       EMAIL_OPTION_PRIORITY_NORMAL     = 3,          /**< Specifies the normal priority*/
-       EMAIL_OPTION_PRIORITY_LOW        = 5,          /**< Specifies the low priority.*/
+    EMAIL_OPTION_PRIORITY_HIGH       = 1,          /**< High priority */
+    EMAIL_OPTION_PRIORITY_NORMAL     = 3,          /**< Normal priority */
+    EMAIL_OPTION_PRIORITY_LOW        = 5,          /**< Low priority */
 };
 
 /**
-* This enumeration specifies the saving save a copy after sending.
+* @brief Enumeration for the saving a copy after sending.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_KEEP_LOCAL_COPY_OFF = 0,          /**< Specifies off the keeping local copy.*/
-       EMAIL_OPTION_KEEP_LOCAL_COPY_ON  = 1,          /**< Specifies on the keeping local copy.*/
+    EMAIL_OPTION_KEEP_LOCAL_COPY_OFF = 0,          /**< Keeping local copy is not enabled */
+    EMAIL_OPTION_KEEP_LOCAL_COPY_ON  = 1,          /**< Keeping local copy is enabled */
 };
 
 /**
-* This enumeration specifies the request of delivery report.
+* @brief Enumeration for the request of a delivery report.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_REQ_DELIVERY_RECEIPT_OFF = 0,     /**< Specifies off the requesting delivery receipt.*/
-       EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON  = 1,     /**< Specifies on the requesting delivery receipt.*/
+    EMAIL_OPTION_REQ_DELIVERY_RECEIPT_OFF = 0,     /**< Requesting delivery receipt is not enabled */
+    EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON  = 1,     /**< Requesting delivery receipt is enabled */
 };
 
 /**
-* This enumeration specifies the request of read receipt.
+* @brief Enumeration for the request of a read receipt.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_REQ_READ_RECEIPT_OFF = 0,         /**< Specifies off the requesting read receipt.*/
-       EMAIL_OPTION_REQ_READ_RECEIPT_ON  = 1,         /**< Specifies on the requesting read receipt.*/
+    EMAIL_OPTION_REQ_READ_RECEIPT_OFF = 0,         /**< Requesting read receipt is not enabled */
+    EMAIL_OPTION_REQ_READ_RECEIPT_ON  = 1,         /**< Requesting read receipt is enabled */
 };
 
 /**
-* This enumeration specifies the blocking of address.
+* @brief Enumeration for the blocking of an address.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_BLOCK_ADDRESS_OFF   = 0,          /**< Specifies off the blocking by address.*/
-       EMAIL_OPTION_BLOCK_ADDRESS_ON    = 1,          /**< Specifies on the blocking by address.*/
+    EMAIL_OPTION_BLOCK_ADDRESS_OFF   = 0,          /**< Blocking an address is not enabled */
+    EMAIL_OPTION_BLOCK_ADDRESS_ON    = 1,          /**< Blocking an address is enabled */
 };
 
 /**
-* This enumeration specifies the blocking of subject.
+* @brief Enumeration for the blocking of a subject.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_OPTION_BLOCK_SUBJECT_OFF   = 0,          /**< Specifies off the blocking by subject.*/
-       EMAIL_OPTION_BLOCK_SUBJECT_ON    = 1,          /**< Specifies on the blocking by subject.*/
+    EMAIL_OPTION_BLOCK_SUBJECT_OFF   = 0,          /**< Blocking by subject is not enabled */
+    EMAIL_OPTION_BLOCK_SUBJECT_ON    = 1,          /**< Blocking by subject is enabled */
 };
 
 enum
 {
-       EMAIL_LIST_TYPE_UNREAD           = -3,
-       EMAIL_LIST_TYPE_LOCAL            = -2,
-       EMAIL_LIST_TYPE_THREAD           = -1,
-       EMAIL_LIST_TYPE_NORMAL           = 0
+    EMAIL_LIST_TYPE_UNREAD           = -3,        /**< List unread mails */
+    EMAIL_LIST_TYPE_LOCAL            = -2,        /**< List local mails */
+    EMAIL_LIST_TYPE_THREAD           = -1,        /**< List thread */
+    EMAIL_LIST_TYPE_NORMAL           = 0          /**< List all mails */
 };
 
 /**
-* This enumeration specifies the mailbox sync type.
+* @brief Enumeration for the mailbox sync type.
+* @since_tizen 2.3
 */
 enum
 {
-       EMAIL_MAILBOX_ALL                = -1,         /**< All mailboxes.*/
-       EMAIL_MAILBOX_FROM_SERVER        = 0,          /**< Mailboxes from server. */
-       EMAIL_MAILBOX_FROM_LOCAL         = 1,          /**< Mailboxes from local. */
+    EMAIL_MAILBOX_ALL                = -1,         /**< All mailboxes */
+    EMAIL_MAILBOX_FROM_SERVER        = 0,          /**< Mailboxes from server */
+    EMAIL_MAILBOX_FROM_LOCAL         = 1,          /**< Mailboxes from local */
 };
 
+/**
+* @brief Enumeration for the mail change type.
+* @since_tizen 2.3
+*/
 typedef enum
 {
-       APPEND_BODY                    = 1,
-       UPDATE_MAILBOX,
-       UPDATE_ATTACHMENT_INFO,
-       UPDATE_FLAG,
-       UPDATE_SAVENAME,
-       UPDATE_EXTRA_FLAG,
-       UPDATE_MAIL,
-       UPDATE_DATETIME,
-       UPDATE_FROM_CONTACT_INFO,
-       UPDATE_TO_CONTACT_INFO,
-       UPDATE_ALL_CONTACT_NAME,
-       UPDATE_ALL_CONTACT_INFO,
-       UPDATE_STICKY_EXTRA_FLAG,
-       UPDATE_PARTIAL_BODY_DOWNLOAD,
-       UPDATE_MEETING,
-       UPDATE_SEEN_FLAG_OF_THREAD,
-       UPDATE_FILE_PATH,
-} email_mail_change_type_t; // Should be moved intenal types */
+    APPEND_BODY                    = 1,    /**< Body is appended */
+    UPDATE_MAILBOX,                        /**< Mailbox is updated */
+    UPDATE_ATTACHMENT_INFO,                /**< Attachment info is updated */
+    UPDATE_FLAG,                           /**< Flag is updated */
+    UPDATE_SAVENAME,                       /**< Savename is updated */
+    UPDATE_EXTRA_FLAG,                     /**< Extra flag is updated */
+    UPDATE_MAIL,                           /**< Mail information is updated */
+    UPDATE_DATETIME,                       /**< Datetime is updated */
+    UPDATE_FROM_CONTACT_INFO,              /**< From contact info is updated */
+    UPDATE_TO_CONTACT_INFO,                /**< To contact info is updated */
+    UPDATE_ALL_CONTACT_NAME,               /**< All contact name is updated */
+    UPDATE_ALL_CONTACT_INFO,               /**< All contact info is updated */
+    UPDATE_STICKY_EXTRA_FLAG,              /**< Sticky extra flag is updated */
+    UPDATE_PARTIAL_BODY_DOWNLOAD,          /**< Partial body download is updated */
+    UPDATE_MEETING,                        /**< Meeting is updated */
+    UPDATE_SEEN_FLAG_OF_THREAD,            /**< Seen flag of thread is updated */
+    UPDATE_FILE_PATH,                      /**< File path is updated */
+} email_mail_change_type_t;
 
 
 /**
-* This enumeration specifies the address type.
+* @brief Enumeration for the address type.
+* @since_tizen 2.3
 */
 typedef enum
 {
-       EMAIL_ADDRESS_TYPE_FROM          = 1,          /**< Specifies the from address.*/
-       EMAIL_ADDRESS_TYPE_TO,                         /**< Specifies the to recipient address.*/
-       EMAIL_ADDRESS_TYPE_CC,                         /**< Specifies the cc recipient address.*/
-       EMAIL_ADDRESS_TYPE_BCC,                        /**< Specifies the bcc recipient address.*/
-       EMAIL_ADDRESS_TYPE_REPLY,                      /**< Specifies the reply recipient address.*/
-       EMAIL_ADDRESS_TYPE_RETURN,                     /**< Specifies the return recipient address.*/
+    EMAIL_ADDRESS_TYPE_FROM          = 1,          /**< From address */
+    EMAIL_ADDRESS_TYPE_TO,                         /**< To recipient address */
+    EMAIL_ADDRESS_TYPE_CC,                         /**< CC recipient address */
+    EMAIL_ADDRESS_TYPE_BCC,                        /**< BCC recipient address */
+    EMAIL_ADDRESS_TYPE_REPLY,                      /**< Reply recipient address */
+    EMAIL_ADDRESS_TYPE_RETURN,                     /**< Return recipient address */
 } email_address_type_t;
 
 /**
- * This enumeration specifies the search type for searching mailbox.
+ * @brief Enumeration for the search type for searching a mailbox.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MAILBOX_SEARCH_KEY_TYPE_SUBJECT,         /**< The search key is for searching subject.*/
-       EMAIL_MAILBOX_SEARCH_KEY_TYPE_FROM,            /**< The search key is for searching sender address.*/
-       EMAIL_MAILBOX_SEARCH_KEY_TYPE_BODY,            /**< The search key is for searching body.*/
-       EMAIL_MAILBOX_SEARCH_KEY_TYPE_TO,              /**< The search key is for searching recipient address.*/
+    EMAIL_MAILBOX_SEARCH_KEY_TYPE_SUBJECT,         /**< The search key for searching subject */
+    EMAIL_MAILBOX_SEARCH_KEY_TYPE_FROM,            /**< The search key for searching sender address */
+    EMAIL_MAILBOX_SEARCH_KEY_TYPE_BODY,            /**< The search key for searching body */
+    EMAIL_MAILBOX_SEARCH_KEY_TYPE_TO,              /**< The search key for searching recipient address */
 } email_mailbox_search_key_t;
 
 /**
- * This enumeration specifies the download status of mail body.
+ * @brief Enumeration for the download status of a mail body.
+ * @since_tizen 2.3
  */
 
 typedef enum
 {
-       EMAIL_BODY_DOWNLOAD_STATUS_NONE = 0,
-       EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED = 1,
-       EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED = 2,
+    EMAIL_BODY_DOWNLOAD_STATUS_NONE = 0,                  /**< The mail is not downloaded yet */
+    EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED = 1,      /**< The mail is fully downloaded */
+    EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED = 2,  /**< The mail is partially downloaded */
 } email_body_download_status_t;
 
 /**
- * This enumeration specifies the moving type.
+ * @brief Enumeration for the download status of a mail body.
+ * @since_tizen 2.3
+ */
+
+typedef enum
+{
+    EMAIL_PART_DOWNLOAD_STATUS_NONE = 0,                   /**< The part is not downloaded yet */
+    EMAIL_PART_DOWNLOAD_STATUS_FULLY_DOWNLOADED = 1,       /**< The part is fully downloaded */
+    EMAIL_PART_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED = 2,   /**< The part is partially downloaded */
+} email_part_download_status_t;
+
+/**
+ * @brief Enumeration for the moving type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_MOVED_BY_COMMAND = 0,
-       EMAIL_MOVED_BY_MOVING_THREAD,
-       EMAIL_MOVED_AFTER_SENDING,
-       EMAIL_MOVED_CANCELATION_MAIL
+    EMAIL_MOVED_BY_COMMAND = 0,      /**< The mails are moved by user */
+    EMAIL_MOVED_BY_MOVING_THREAD,    /**< The mails are moved with moving thread mails*/
+    EMAIL_MOVED_AFTER_SENDING,       /**< The mails are moved by 'move after sending' option */
+    EMAIL_MOVED_CANCELATION_MAIL     /**< The mails are moved by cancellation of meeting request */
 } email_move_type;
 
 /**
- * This enumeration specifies the deletion type.
+ * @brief Enumeration for the deletion type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_DELETED_BY_COMMAND = 0,
-       EMAIL_DELETED_BY_OVERFLOW,
-       EMAIL_DELETED_BY_DELETING_THREAD,
-       EMAIL_DELETED_BY_MOVING_TO_OTHER_ACCOUNT,
-       EMAIL_DELETED_AFTER_SENDING,
-       EMAIL_DELETED_FROM_SERVER,
+    EMAIL_DELETED_BY_COMMAND = 0,                /**< The mails are deleted by user */
+    EMAIL_DELETED_BY_OVERFLOW,                   /**< The mails are deleted by overflow */
+    EMAIL_DELETED_BY_DELETING_THREAD,            /**< The mails are deleted with removing thread mails */
+    EMAIL_DELETED_BY_MOVING_TO_OTHER_ACCOUNT,    /**< The mails are deleted by moving to other account */
+    EMAIL_DELETED_AFTER_SENDING,                 /**< The mails are deleted by 'delete after sending' option */
+    EMAIL_DELETED_FROM_SERVER,                   /**< The mails are deleted because the mails are deleted on server */
 } email_delete_type;
 
 /**
- * This enumeration specifies the status field type.
+ * @brief Enumeration for the status field type.
+ * @since_tizen 2.3
  */
 typedef enum
 {
-       EMAIL_FLAGS_SEEN_FIELD = 0,
-       EMAIL_FLAGS_DELETED_FIELD,
-       EMAIL_FLAGS_FLAGGED_FIELD,
-       EMAIL_FLAGS_ANSWERED_FIELD,
-       EMAIL_FLAGS_RECENT_FIELD,
-       EMAIL_FLAGS_DRAFT_FIELD,
-       EMAIL_FLAGS_FORWARDED_FIELD,
-       EMAIL_FLAGS_FIELD_COUNT,
+    EMAIL_FLAGS_SEEN_FIELD = 0,    /**< mail flag : \Seen */
+    EMAIL_FLAGS_DELETED_FIELD,     /**< mail flag : \Deleted */
+    EMAIL_FLAGS_FLAGGED_FIELD,     /**< mail flag : \Flagged */
+    EMAIL_FLAGS_ANSWERED_FIELD,    /**< mail flag : \Answered */
+    EMAIL_FLAGS_RECENT_FIELD,      /**< mail flag : \Recent */
+    EMAIL_FLAGS_DRAFT_FIELD,       /**< mail flag : \Draft */
+    EMAIL_FLAGS_FORWARDED_FIELD,   /**< mail flag : \Forwarded */
+    EMAIL_FLAGS_FIELD_COUNT,       /**< field count */
 } email_flags_field_type;
 
 typedef enum {
-       EMAIL_FLAG_NONE                  = 0,
-       EMAIL_FLAG_FLAGED                = 1,
-       EMAIL_FLAG_TASK_STATUS_CLEAR     = 2,
-       EMAIL_FLAG_TASK_STATUS_COMPLETE  = 3,
-       EMAIL_FLAG_TASK_STATUS_ACTIVE    = 4,
+    EMAIL_FLAG_NONE                  = 0,    /**< No flag */
+    EMAIL_FLAG_FLAGED                = 1,    /**< Flagged */
+    EMAIL_FLAG_TASK_STATUS_CLEAR     = 2,    /**< For EAS task management : No task */
+    EMAIL_FLAG_TASK_STATUS_COMPLETE  = 3,    /**< For EAS task management : Completed task */
+    EMAIL_FLAG_TASK_STATUS_ACTIVE    = 4,    /**< For EAS task management : Active task */
 } email_flagged_type;
 
 typedef enum {
-       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       =  1,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_UID              =  2,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_BCC              =  7,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_CC               =  9,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_FROM             = 10,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_KEYWORD          = 11,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_SUBJECT          = 13,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_TO               = 15,  /* string type */
-       EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      = 16,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     = 17,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE = 20,  /* time type */
-       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     = 21,  /* time type */
-       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  = 22,  /* time type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   = 26,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    = 28,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      = 30,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     = 32,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     = 34,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       = 36,  /* integer type */
-       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       = 43,  /* string type */
+    EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       =  1,  /* integer type */    /**< Messages with specified message no */
+    EMAIL_SEARCH_FILTER_TYPE_UID              =  2,  /* integer type */    /**< Messages with unique identifiers corresponding to the specified unique identifier set */
+    EMAIL_SEARCH_FILTER_TYPE_ALL              =  3,  /* integer type */    /**< All messages in the mailbox; the default initial key for ANDing */
+    EMAIL_SEARCH_FILTER_TYPE_BCC              =  7,  /* string type */     /**< Messages that contain the specified string in the envelope structure's BCC field */
+    EMAIL_SEARCH_FILTER_TYPE_BODY             =  8,  /* string type */     /**< Messages that contain the specified string in the body of the message */
+    EMAIL_SEARCH_FILTER_TYPE_CC               =  9,  /* string type */     /**< Messages that contain the specified string in the envelope structure's CC field */
+    EMAIL_SEARCH_FILTER_TYPE_FROM             = 10,  /* string type */     /**< Messages that contain the specified string in the envelope structure's FROM field */
+    EMAIL_SEARCH_FILTER_TYPE_KEYWORD          = 11,  /* string type */     /**< Messages with the specified keyword set */
+    EMAIL_SEARCH_FILTER_TYPE_TEXT             = 12,  /* string type */     /**< Messages that contain the specified string in the header or body of the message */
+    EMAIL_SEARCH_FILTER_TYPE_SUBJECT          = 13,  /* string type */     /**< Messages that contain the specified string in the envelope structure's SUBJECT field */
+    EMAIL_SEARCH_FILTER_TYPE_TO               = 15,  /* string type */     /**< Messages that contain the specified string in the envelope structure's TO field */
+    EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      = 16,  /* integer type */    /**< Messages with a size larger than the specified number of octets */
+    EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     = 17,  /* integer type */    /**< Messages with a size smaller than the specified number of octets */
+    EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE = 20,  /* time type */       /**< Messages whose Date: header is earlier than the specified date */
+    EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     = 21,  /* time type */       /**< Messages whose Date: header is within the specified date */
+    EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  = 22,  /* time type */       /**< Messages whose Date: header is within or later than the specified date */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   = 26,  /* integer type */    /**< Messages with the \Answered flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        = 27,  /* integer type */    /**< Messages that have the \Recent flag set but not the \Seen flag */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    = 28,  /* integer type */    /**< Messages with the \Deleted flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        = 29,  /* integer type */    /**< Messages that do not have the \Recent flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      = 30,  /* integer type */    /**< Messages with the \Draft flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     = 32,  /* integer type */    /**< Messages with the \Flagged flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     = 34,  /* integer type */    /**< Messages that have the \Recent flag set */
+    EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       = 36,  /* integer type */    /**< Messages that have the \Seen flag set */
+    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;
 
 typedef enum {
-       EMAIL_DRM_TYPE_NONE                       = 0,
-       EMAIL_DRM_TYPE_OBJECT                     = 1,
-       EMAIL_DRM_TYPE_RIGHT                      = 2,
-       EMAIL_DRM_TYPE_DCF                        = 3
+    EMAIL_DRM_TYPE_NONE                       = 0,    /**< No DRM type */
+    EMAIL_DRM_TYPE_OBJECT                     = 1,    /**< DRM object */
+    EMAIL_DRM_TYPE_RIGHT                      = 2,    /**< DRM right */
+    EMAIL_DRM_TYPE_DCF                        = 3     /**< DRM Content Format */
 } email_drm_type;
 
 typedef enum {
-       EMAIL_DRM_METHOD_NONE                     = 0,
-       EMAIL_DRM_METHOD_FL                       = 1,
-       EMAIL_DRM_METHOD_CD                       = 2,
-       EMAIL_DRM_METHOD_SSD                      = 3,
-       EMAIL_DRM_METHOD_SD                       = 4
+    EMAIL_DRM_METHOD_NONE                     = 0,    /**< No DRM method */
+    EMAIL_DRM_METHOD_FL                       = 1,    /**< Forward lock method */
+    EMAIL_DRM_METHOD_CD                       = 2,    /**< Combined Delivery method */
+    EMAIL_DRM_METHOD_SSD                      = 3,    /**< Deprecated */
+    EMAIL_DRM_METHOD_SD                       = 4     /**< Separated Delivery method */
 } email_drm_method;
 
 typedef enum {
-       EMAIL_CANCELED_BY_USER                    = 0,
-       EMAIL_CANCELED_BY_MDM                     = 1,
+    EMAIL_CANCELED_BY_USER                    = 0,    /**< Canceled by user */
+    EMAIL_CANCELED_BY_MDM                     = 1,    /**< Canceled by MDM policy */
 } email_cancelation_type;
 
 typedef enum {
-       EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 =  0,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              =  1,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              =  2,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            =  3,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            =  4,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_SUBJECT                 =  5,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_DATE_TIME               =  6,  /* datetime type */
-       EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      =  7,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     =  8,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          =  9,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              = 10,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       = 11,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FROM                    = 12,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_TO                      = 13,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_CC                      = 14,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_BCC                     = 15,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    = 16,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE               = 17,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         = 18,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          = 19,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               = 20,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        = 21,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     = 22,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     = 23,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    = 24,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      = 25,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       = 26,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   = 27,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              = 28,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_PRIORITY                = 29,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             = 30,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             = 31,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           = 32,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        = 33,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    = 34,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_THREAD_ID               = 35,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       = 36,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            = 37,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  = 38,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           = 39,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             = 40,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              = 41,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME  = 42,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES  = 43,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_TAG_ID                  = 44,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME            = 45,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME          = 46,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_RECIPIENT_ADDRESS       = 47,  /* string type */
-       EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE    = 48,  /* integer type */
-       EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE           = 49,  /* binary type */
-       EMAIL_MAIL_ATTRIBUTE_END                         
+    EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 =  0,  /* integer type */    /**< @a mail_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              =  1,  /* integer type */    /**< @a account_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              =  2,  /* integer type */    /**< @a mailbox_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            =  3,  /* string type */     /**< @a mailbox_name field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            =  4,  /* integer type */    /**< @a mailbox_type field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SUBJECT                 =  5,  /* string type */     /**< @a subject field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_DATE_TIME               =  6,  /* datetime type */   /**< @a date_time field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      =  7,  /* integer type */    /**< @a server_mail_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     =  8,  /* string type */     /**< @a server_mailbox_name field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          =  9,  /* string type */     /**< @a server_mail_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              = 10,  /* string type */     /**< @a message_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       = 11,  /* integer type */    /**< @a reference_mail_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FROM                    = 12,  /* string type */     /**< @a full_address_from field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_TO                      = 13,  /* string type */     /**< @a full_address_to field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_CC                      = 14,  /* string type */     /**< @a full_address_cc field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_BCC                     = 15,  /* string type */     /**< @a full_address_bcc field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    = 16,  /* integer type */    /**< @a body_download_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE               = 17,  /* integer type */    /**< @a mail_size field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         = 18,  /* string type */     /**< @a file_path_plain field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          = 19,  /* string type */     /**< @a file_path_html field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               = 20,  /* integer type */    /* Deprecated */  /**< @a file_size field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        = 21,  /* integer type */    /**< @a flags_seen_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     = 22,  /* integer type */    /**< @a flags_deleted_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     = 23,  /* integer type */    /**< @a flags_flagged_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    = 24,  /* integer type */    /**< @a flags_answered_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      = 25,  /* integer type */    /**< @a flags_recent_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       = 26,  /* integer type */    /**< @a flags_draft_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   = 27,  /* integer type */    /**< @a flags_forwarded_field field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              = 28,  /* integer type */    /**< @a drm_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_PRIORITY                = 29,  /* integer type */    /**< @a priority field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             = 30,  /* integer type */    /**< @a save_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             = 31,  /* integer type */    /**< @a lock_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           = 32,  /* integer type */    /**< @a report_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        = 33,  /* integer type */    /**< @a attachment_count field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    = 34,  /* integer type */    /**< @a inline_content_count field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_THREAD_ID               = 35,  /* integer type */    /**< @a thread_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       = 36,  /* integer type */    /**< @a thread_item_count field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            = 37,  /* string type */     /**< @a preview_text field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  = 38,  /* integer type */    /**< @a meeting_request_status field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           = 39,  /* integer type */    /**< @a message_class field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             = 40,  /* integer type */    /**< @a digest_type field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              = 41,  /* integer type */    /**< @a smime_type field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME  = 42,  /* integer type */    /**< @a scheduled_sending_time field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES  = 43,  /* integer type */    /**< @a remaining_resend_times field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_TAG_ID                  = 44,  /* integer type */    /**< @a tag_id field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME            = 45,  /* integer type */    /**< @a replied_time field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME          = 46,  /* integer type */    /**< @a forwared_time field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_RECIPIENT_ADDRESS       = 47,  /* string type */     /**< @a recipient_address field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE    = 48,  /* integer type */    /**< @a eas_data_length_type field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE           = 49,  /* binary type */     /**< @a eas_data_type field of email_mail_data_t */
+    EMAIL_MAIL_ATTRIBUTE_END                            /**< The end of attribute */
 } email_mail_attribute_type;
 
 typedef enum {
-       EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT = 1,
-       EMAIL_MAIL_TEXT_ATTRIBUTE_END
+    EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT = 1,    /**< Full text attribute of email_mail_text */
+    EMAIL_MAIL_TEXT_ATTRIBUTE_END               /**< The end of email_mail_text_attribute */
 } email_mail_text_attribute_type;
 
 typedef enum {
-       EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME = 1,
-       EMAIL_MAIL_ATTCH_ATTRIBUTE_END
+    EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME = 1,    /**< Attachment name */
+    EMAIL_MAIL_ATTCH_ATTRIBUTE_END                      /**< The end of email_mail_attach_attribute_type */
 } email_mail_attach_attribute_type;
 
 typedef enum {
-       EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE         = 0,
-       EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER      = 1,
-       EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING       = 2,
-       EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME         = 3,
-       EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_BINARY       = 4
+    EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE         = 0,    /**< The attribute type is none */
+    EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER      = 1,    /**< The attribute type is integer */
+    EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING       = 2,    /**< The attribute type is string */
+    EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME         = 3,    /**< The attribute type is time */
+    EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_BINARY       = 4     /**< The attribute type is binary */
 } email_mail_attribute_value_type;
 
 typedef enum {
-       EMAIL_ADD_MY_ADDRESS_OPTION_DO_NOT_ADD            = 0,
-       EMAIL_ADD_MY_ADDRESS_OPTION_ALWAYS_ADD_TO_CC      = 1,
-       EMAIL_ADD_MY_ADDRESS_OPTION_ALWAYS_ADD_TO_BCC     = 2,
+    EMAIL_ADD_MY_ADDRESS_OPTION_DO_NOT_ADD            = 0,    /**< Address option type is not added */
+    EMAIL_ADD_MY_ADDRESS_OPTION_ALWAYS_ADD_TO_CC      = 1,    /**< Address option type is added to cc */
+    EMAIL_ADD_MY_ADDRESS_OPTION_ALWAYS_ADD_TO_BCC     = 2,    /**< Address option type is added to bcc */
 } email_add_my_address_option_type;
 
 typedef enum {
-       EMAIL_MESSAGE_CLASS_UNSPECIFIED,
-       EMAIL_MESSAGE_CLASS_UNKNOWN,
-       EMAIL_MESSAGE_CLASS_NOTE,
-       EMAIL_MESSAGE_CLASS_NOTE_RULES_OF_TEMPLATE_MICROSOFT,
-       EMAIL_MESSAGE_CLASS_NOTE_SMIME,
-       EMAIL_MESSAGE_CLASS_NOTE_SMIME_MULTIPART_SIGNED,
-       EMAIL_MESSAGE_CLASS_NOTIFICATION_MEETING,
-       EMAIL_MESSAGE_CLASS_OCTEL_VOICE,
-       EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_REQUEST,
-       EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_CANCELED,
-       EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_POS,
-       EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_TENT,
-       EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_NEG,
-       EMAIL_MESSAGE_CLASS_POST,
-       EMAIL_MESSAGE_CLASS_INFO_PATH_FORM,
-       EMAIL_MESSAGE_CLASS_VOICE_NOTES,
-       EMAIL_MESSAGE_CLASS_SHARING,
-       EMAIL_MESSAGE_CLASS_NOTE_EXCHANGE_ACTIVE_SYNC_REMOTE_WIPE_CONFIRMATION,
-       EMAIL_MESSAGE_CLASS_VOICE_MAIL,
-       EMAIL_MESSAGE_CLASS_SMS,
-       EMAIL_MESSAGE_CLASS_IRM_MESSAGE                                         = 0x00100000,
-       EMAIL_MESSAGE_CLASS_SMART_REPLY                                         = 0x01000000,
-       EMAIL_MESSAGE_CLASS_SMART_FORWARD                                       = 0x02000000,
-       EMAIL_MESSAGE_CLASS_REPORT_NOT_READ_REPORT                              = 0x10000000,
-       EMAIL_MESSAGE_CLASS_REPORT_READ_REPORT                                  = 0x20000000,
-       EMAIL_MESSAGE_CLASS_REPORT_NON_DELIVERY_RECEIPT                         = 0x40000000,
-       EMAIL_MESSAGE_CLASS_REPORT_DELIVERY_RECEIPT                             = 0x80000000,
-       EMAIL_MESSAGE_CLASS_CALENDAR_FORWARD                                                        = 0x04000000
+    EMAIL_MESSAGE_CLASS_UNSPECIFIED,                                           /**< The message class is unspecified */
+    EMAIL_MESSAGE_CLASS_UNKNOWN,                                               /**< The message class is unknown */
+    EMAIL_MESSAGE_CLASS_NOTE,                                                  /**< The message class is note */
+    EMAIL_MESSAGE_CLASS_NOTE_RULES_OF_TEMPLATE_MICROSOFT,                      /**< The message class is note_rule_of_template_microsoft */
+    EMAIL_MESSAGE_CLASS_NOTE_SMIME,                                            /**< The message class is note_smime */
+    EMAIL_MESSAGE_CLASS_NOTE_SMIME_MULTIPART_SIGNED,                           /**< The message class is note_smime_multipart_signed */
+    EMAIL_MESSAGE_CLASS_NOTIFICATION_MEETING,                                  /**< The message class is notification meeting */ 
+    EMAIL_MESSAGE_CLASS_OCTEL_VOICE,                                           /**< The message class is octel voice */ 
+    EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_REQUEST,                              /**< The message class is meeting request */
+    EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_CANCELED,                             /**< The message class is meeting canceled */
+    EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_POS,                             /**< The message class is meeting resp pos */
+    EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_TENT,                            /**< The message class is meeting resp tent */
+    EMAIL_MESSAGE_CLASS_SCHEDULE_MEETING_RESP_NEG,                             /**< The message class is meeting resp neg */
+    EMAIL_MESSAGE_CLASS_POST,                                                  /**< The message class is post */
+    EMAIL_MESSAGE_CLASS_INFO_PATH_FORM,                                        /**< The message class is info path form */
+    EMAIL_MESSAGE_CLASS_VOICE_NOTES,                                           /**< The message class is voice notes */
+    EMAIL_MESSAGE_CLASS_SHARING,                                               /**< The message class is sharing */
+    EMAIL_MESSAGE_CLASS_NOTE_EXCHANGE_ACTIVE_SYNC_REMOTE_WIPE_CONFIRMATION,    /**< The message class is note exchange active sync remote wipe confirmation */
+    EMAIL_MESSAGE_CLASS_VOICE_MAIL,                                            /**< The message class is voice mail */
+    EMAIL_MESSAGE_CLASS_SMS,                                                   /**< The message class is SMS */
+    EMAIL_MESSAGE_CLASS_IRM_MESSAGE                           = 0x00010000,    /**< The message class is IRM message */
+    EMAIL_MESSAGE_CLASS_SMART_REPLY                           = 0x00100000,    /**< The message class is smart reply */
+    EMAIL_MESSAGE_CLASS_SMART_FORWARD                         = 0x00200000,    /**< The message class is smart forward */
+    EMAIL_MESSAGE_CLASS_REPORT_NOT_READ_REPORT                = 0x01000000,    /**< The message class is report not read report */
+    EMAIL_MESSAGE_CLASS_REPORT_READ_REPORT                    = 0x02000000,    /**< The message class is report read report */
+    EMAIL_MESSAGE_CLASS_REPORT_NON_DELIVERY_RECEIPT           = 0x04000000,    /**< The message class is report non delivery receipt */
+    EMAIL_MESSAGE_CLASS_REPORT_DELIVERY_RECEIPT               = 0x08000000,    /**< The message class is report delivery receipt */
+    EMAIL_MESSAGE_CLASS_CALENDAR_FORWARD                      = 0x00400000     /**< The message class is calendar forward */
 } email_message_class; 
 
 typedef enum{
-       EMAIL_SMIME_NONE                          = 0,   /* Not use smime */
-       EMAIL_SMIME_SIGNED,                               
-       EMAIL_SMIME_ENCRYPTED,
-       EMAIL_SMIME_SIGNED_AND_ENCRYPTED,
+    EMAIL_SMIME_NONE                          = 0,   /**< Not using smime : Normal mail */ 
+    EMAIL_SMIME_SIGNED,                              /**< Signed mail of smime */
+    EMAIL_SMIME_ENCRYPTED,                           /**< Encrypted mail of smime */
+    EMAIL_SMIME_SIGNED_AND_ENCRYPTED,                /**< Signed/encrypted mail of smime */
+    EMAIL_PGP_SIGNED,                                /**< Signed mail of pgp */
+    EMAIL_PGP_ENCRYPTED,                             /**< Encrypted mail of pgp */
+    EMAIL_PGP_SIGNED_AND_ENCRYPTED                   /**< Signed/encrypted mail of pgp */
 } email_smime_type;
 
 typedef enum {
-       CIPHER_TYPE_DES3                          = 0,
-       CIPHER_TYPE_DES,
-       CIPHER_TYPE_RC2_128,
-       CIPHER_TYPE_RC2_64,
-       CIPHER_TYPE_RC2_40,                       
+    CIPHER_TYPE_NONE                          = 0,   /**< None of cipher type */
+    CIPHER_TYPE_DES3,                                /**< DES3 of cipher type */
+    CIPHER_TYPE_DES,                                 /**< DES of cipher type */
+    CIPHER_TYPE_RC2_128,                             /**< RC2 128 of cipher type */
+    CIPHER_TYPE_RC2_64,                              /**< RC2 64 of cipher type */
+    CIPHER_TYPE_RC2_40,                              /**< RC2 40 of cipher type */
 } email_cipher_type;
 
 typedef enum {
-       DIGEST_TYPE_SHA1                          = 0,
-       DIGEST_TYPE_MD5,
+    DIGEST_TYPE_NONE                          = 0,    /**< None of digest type */
+    DIGEST_TYPE_SHA1                          = 1,    /**< SHA1 of digest type */
+    DIGEST_TYPE_MD5                           = 2,    /**< MD5 of digest type */
+    DIGEST_TYPE_RIPEMD160                     = 3,    /**< RIPEMD160 of digest type */
+    DIGEST_TYPE_MD2                           = 4,    /**< MD2 of digest type */
+    DIGEST_TYPE_TIGER192                      = 5,    /**< TIGER192 of digest type */
+    DIGEST_TYPE_HAVAL5160                     = 6,    /**< HAVAL5160 of digest type */
+    DIGEST_TYPE_SHA256                        = 7,    /**< SHA256 of digest type */
+    DIGEST_TYPE_SHA384                        = 8,    /**< SHA384 of digest type */
+    DIGEST_TYPE_SHA512                        = 9,    /**< SHA512 of digest type */
+    DIGEST_TYPE_SHA224                        = 10,   /**< SHA224 of digest type */
+    DIGEST_TYPE_MD4                           = 11,   /**< MD4 of digest type */                   
 } email_digest_type;
 
 typedef enum {
-       EMAIL_AUTHENTICATION_METHOD_NO_AUTH          = 0,
-       EMAIL_AUTHENTICATION_METHOD_DEFAULT          = 1,
-       EMAIL_AUTHENTICATION_METHOD_XOAUTH2          = 2,
+    EMAIL_AUTHENTICATION_METHOD_NO_AUTH          = 0,    /**< The authentication method is no auth */
+    EMAIL_AUTHENTICATION_METHOD_DEFAULT          = 1,    /**< The authentication method is default(SSL/TLS) */
+    EMAIL_AUTHENTICATION_METHOD_XOAUTH2          = 2,    /**< The authentication method is xoauth2 */
 } email_authentication_method_t;
 
 typedef enum {
-       EMAIL_ROAMING_OPTION_RESTRICTED_BACKGROUND_TASK  = 0,
-       EMAIL_ROAMING_OPTION_UNRESTRICTED                = 1,
+    EMAIL_ROAMING_OPTION_RESTRICTED_BACKGROUND_TASK  = 0,    /**< The roaming option is restricted background task */
+    EMAIL_ROAMING_OPTION_UNRESTRICTED                = 1,    /**< The roaming option is unrestricted */
 } email_roaming_option_t;
 
 typedef enum {
-       EMAIL_GET_INCOMING_PASSWORD_LENGTH = 1,
-       EMAIL_GET_OUTGOING_PASSWORD_LENGTH
+    EMAIL_GET_INCOMING_PASSWORD_LENGTH = 1,    /**< Length of receiving password */
+    EMAIL_GET_OUTGOING_PASSWORD_LENGTH         /**< Length of SMTP password */
 } email_get_password_length_type;
 
 /*****************************************************************************/
@@ -1194,539 +1376,599 @@ typedef enum {
 /*****************************************************************************/
 
 /**
- * This structure is used to save mail time.
+ * @brief The structure type to save the mail time.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       unsigned short year;                         /**< Specifies the Year.*/
-       unsigned short month;                        /**< Specifies the Month.*/
-       unsigned short day;                          /**< Specifies the Day.*/
-       unsigned short hour;                         /**< Specifies the Hour.*/
-       unsigned short minute;                       /**< Specifies the Minute.*/
-       unsigned short second;                       /**< Specifies the Second.*/
+    unsigned short year;                         /**< The Year */
+    unsigned short month;                        /**< The Month */
+    unsigned short day;                          /**< The Day */
+    unsigned short hour;                         /**< The Hour */
+    unsigned short minute;                       /**< The Minute */
+    unsigned short second;                       /**< The Second */
 } email_datetime_t DEPRECATED;
 
 /**
- * This structure is used to save the options.
+ * @brief The structure type to save the options.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       email_mail_priority_t              priority;               /**< Specifies the prority. 1=high 3=normal 5=low.*/
-       int                                keep_local_copy;        /**< Specifies the saving save a copy after sending.*/
-       int                                req_delivery_receipt;   /**< Specifies the request of delivery report. 0=off 1=on*/
-       int                                req_read_receipt;       /**< Specifies the request of read receipt. 0=off 1=on*/
-       int                                download_limit;         /**< Specifies the limit of size for downloading.*/
-       int                                block_address;          /**< Specifies the blocking of address. 0=off 1=on*/
-       int                                block_subject;          /**< Specifies the blocking of subject. 0=off 1=on*/
-       char                              *display_name_from;      /**< Specifies the display name of from.*/
-       int                                reply_with_body;        /**< Specifies the replying with body 0=off 1=on*/
-       int                                forward_with_files;     /**< Specifies the fowarding with files 0=off 1=on*/
-       int                                add_myname_card;        /**< Specifies the adding name card 0=off 1=on*/
-       int                                add_signature;          /**< Specifies the adding signature 0=off 1=on*/
-       char                              *signature;              /**< Specifies the signature*/
-       email_add_my_address_option_type   add_my_address_to_bcc;  /**< Specifies whether cc or bcc field should be always filled with my address. */
+    email_mail_priority_t              priority;                /**< The priority. 1=high 3=normal 5=low */
+    int                                keep_local_copy;         /**< Saves a copy after sending */
+    int                                req_delivery_receipt;    /**< The request of delivery report. 0=off 1=on */
+    int                                req_read_receipt;        /**< The request of read receipt. 0=off 1=on */
+    int                                download_limit;          /**< The limit of size for downloading */
+    int                                block_address;           /**< Specifies the blocking of address. 0=off 1=on */
+    int                                block_subject;           /**< The blocking of subject. 0=off 1=on */
+    char                              *display_name_from;       /**< The display name of from */
+    int                                reply_with_body;         /**< The replying with body 0=off 1=on */
+    int                                forward_with_files;      /**< The forwarding with files 0=off 1=on */
+    int                                add_myname_card;         /**< The adding name card 0=off 1=on */
+    int                                add_signature;           /**< The adding signature 0=off 1=on */
+    char                              *signature;               /**< The signature */
+    email_add_my_address_option_type   add_my_address_to_bcc;   /**< The flag specifying whether cc or bcc field should be always filled with my address */
+    int                                notification_status;     /**< The notification status. 1 = ON, 0 = OFF */
+    int                                vibrate_status;          /**< The repetition type */
+    int                                display_content_status;  /**< The display_content status. 1 = ON, 0 = OFF */
+    int                                default_ringtone_status; /**< The badge status.  1 = ON, 0 = OFF */
+    char                               *alert_ringtone_path;    /**< The ringtone path */
 } email_option_t;
 
 /**
- * This structure is used to save the information of email account.
+ * @brief The structure type to save the information of an email account.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       /* General account information */
-       int                             account_id;                               /**< Account id  */
-       char                           *account_name;                             /**< Account name */
-       int                             account_svc_id;                           /**< AccountSvc priv data: Specifies id from account-svc */
-       int                             sync_status;                              /**< Sync Status. SYNC_STATUS_FINISHED, SYNC_STATUS_SYNCING, SYNC_STATUS_HAVE_NEW_MAILS */
-       int                             sync_disabled;                            /**< If this attriube is set as true, email-service will not synchronize this account. */
-       int                             default_mail_slot_size;
-       char                           *logo_icon_path;                           /**< account logo icon (used by account svc and email app) */
-       email_roaming_option_t          roaming_option;                           /**< roaming option */
-       void                           *user_data;                                /**< binary user data */
-       int                                             user_data_length;                         /**< user data length */
-
-       /* User information */
-       char                           *user_display_name;                        /**< User's display */
-       char                           *user_email_address;                       /**< User's email address */
-       char                           *reply_to_address;                         /**< Email address for reply */
-       char                           *return_address;                           /**< Email address for error from server*/
-
-       /* Configuration for incoming server */
-       email_account_server_t          incoming_server_type;                     /**< Incoming server type */
-       char                           *incoming_server_address;                  /**< Incoming server address */
-       int                             incoming_server_port_number;              /**< Incoming server port number */
-       char                           *incoming_server_user_name;                /**< Incoming server user name */
-       char                           *incoming_server_password;                 /**< Incoming server password */
-       int                             incoming_server_secure_connection;        /**< Does incoming server requires secured connection? */
-       email_authentication_method_t   incoming_server_authentication_method;    /**< Incoming server authentication method */
-
-       /* Options for incoming server */
-       email_imap4_retrieval_mode_t    retrieval_mode;                           /**< Retrieval mode : EMAIL_IMAP4_RETRIEVAL_MODE_NEW or EMAIL_IMAP4_RETRIEVAL_MODE_ALL */
-       int                             keep_mails_on_pop_server_after_download;  /**< Keep mails on POP server after download */
-       int                             check_interval;                           /**< Specifies the interval for checking new mail periodically */
-       int                             auto_download_size;                       /**< Specifies the size for auto download in bytes. -1 means entire mails body */
-       int                             peak_interval;                            /**< Specifies the interval for checking new mail periodically of peak schedule */
-       int                             peak_days;                                /**< Specifies the weekdays of peak schedule */
-       int                             peak_start_time;                          /**< Specifies the start time of peak schedule */
-       int                             peak_end_time;                            /**< Specifies the end time of peak schedule */
-
-       /* Configuration for outgoing server */
-       email_account_server_t          outgoing_server_type;                     /**< Outgoing server type */
-       char                                       *outgoing_server_address;                  /**< Outgoing server address */
-       int                                                 outgoing_server_port_number;              /**< Outgoing server port number */
-       char                                       *outgoing_server_user_name;                /**< Outgoing server user name */
-       char                                       *outgoing_server_password;                 /**< Outgoing server password */
-       int                                         outgoing_server_secure_connection;        /**< Does outgoing server requires secured connection? */
-       int                                         outgoing_server_need_authentication;      /**< Does outgoing server requires authentication? */
-       int                                         outgoing_server_use_same_authenticator;   /**< Use same authenticator for outgoing server - email_authentication_method_t - */
-
-
-       /* Options for outgoing server */
-       email_option_t                      options;
-       int                             auto_resend_times;                        /**< Auto retry count for sending a email */
-       int                            outgoing_server_size_limit;                /** Mail size limitation for SMTP sending*/
-
-       /* Authentication Options */
-       int                                 pop_before_smtp;                          /**< POP before SMTP Authentication */
-       int                             incoming_server_requires_apop;            /**< APOP authentication */
-
-       /* S/MIME Options */
-       email_smime_type                smime_type;                               /**< Specifies the smime type 0=Normal 1=Clear signed 2=encrypted 3=Signed + encrypted */
-       char                           *certificate_path;                         /**< Specifies the certificate path of private*/
-       email_cipher_type               cipher_type;                              /**< Specifies the encryption algorithm*/
-       email_digest_type               digest_type;                              /**< Specifies the digest algorithm*/
+    /* General account information */
+    int                             account_id;                               /**< Account ID */
+    char                           *account_name;                             /**< Account name */
+    int                             account_svc_id;                           /**< AccountSvc priv data: Specifies ID from account-svc */
+    int                             sync_status;                              /**< Sync Status. SYNC_STATUS_FINISHED, SYNC_STATUS_SYNCING, SYNC_STATUS_HAVE_NEW_MAILS */
+    int                             sync_disabled;                            /**< Flag to enable or disable syncing. If this attribute is set as true, email-service will not synchronize this account. */
+    int                             default_mail_slot_size;                   /**< Synced mail count in mailbox */
+    char                           *logo_icon_path;                           /**< Account logo icon (used by account svc and email app) */
+    email_roaming_option_t          roaming_option;                           /**< Roaming option */
+    int                             color_label;                              /**< Account color label */
+    void                           *user_data;                                /**< Binary user data */
+    int                             user_data_length;                         /**< User data length */
+
+    /* User information */
+    char                           *user_display_name;                        /**< User's display */
+    char                           *user_email_address;                       /**< User's email address */
+    char                           *reply_to_address;                         /**< Email address for reply */
+    char                           *return_address;                           /**< Email address for error from server*/
+
+    /* Configuration for incoming server */
+    email_account_server_t          incoming_server_type;                     /**< Incoming server type */
+    char                           *incoming_server_address;                  /**< Incoming server address */
+    int                             incoming_server_port_number;              /**< Incoming server port number */
+    char                           *incoming_server_user_name;                /**< Incoming server user name */
+    char                           *incoming_server_password;                 /**< Incoming server password */
+    int                             incoming_server_secure_connection;        /**< Flag indicating whether incoming server requires secured connection */
+    email_authentication_method_t   incoming_server_authentication_method;    /**< Incoming server authentication method */
+
+    /* Options for incoming server */
+    email_imap4_retrieval_mode_t    retrieval_mode;                           /**< Retrieval mode : EMAIL_IMAP4_RETRIEVAL_MODE_NEW or EMAIL_IMAP4_RETRIEVAL_MODE_ALL */
+    int                             keep_mails_on_pop_server_after_download;  /**< Keep mails on POP server after download */
+    int                             check_interval;                           /**< The interval for checking new mail periodically */
+    int                             auto_download_size;                       /**< The size for auto download in bytes. @c -1 means entire mails body */
+    int                             peak_interval;                            /**< The interval for checking new mail periodically of peak schedule */
+    int                             peak_days;                                /**< The weekdays of peak schedule */
+    int                             peak_start_time;                          /**< The start time of peak schedule */
+    int                             peak_end_time;                            /**< The end time of peak schedule */
+
+    /* Configuration for outgoing server */
+    email_account_server_t          outgoing_server_type;                     /**< Outgoing server type */
+    char                           *outgoing_server_address;                  /**< Outgoing server address */
+    int                             outgoing_server_port_number;              /**< Outgoing server port number */
+    char                           *outgoing_server_user_name;                /**< Outgoing server user name */
+    char                           *outgoing_server_password;                 /**< Outgoing server password */
+    int                             outgoing_server_secure_connection;        /**< Flag indicating whether outgoing server requires secured connection */
+    int                             outgoing_server_need_authentication;      /**< Flag indicating whether outgoing server requires authentication */
+    int                             outgoing_server_use_same_authenticator;   /**< Use same authenticator for outgoing server - email_authentication_method_t - */
+
+
+    /* Options for outgoing server */
+    email_option_t                  options;                                  /**< Account options for setting */
+    int                             auto_resend_times;                        /**< Auto retry count for sending a email */
+    int                             outgoing_server_size_limit;               /**< Mail size limitation for SMTP sending */
+
+    /* Auto download */
+    int                             wifi_auto_download;                        /**< Auto attachment download in WiFi connection */
+
+    /* Authentication Options */
+    int                             pop_before_smtp;                          /**< POP before SMTP Authentication */
+    int                             incoming_server_requires_apop;            /**< APOP authentication */
+
+    /* S/MIME Options */
+    email_smime_type                smime_type;                               /**< The smime type 0=Normal 1=Clear signed 2=encrypted 3=Signed + encrypted */
+    char                           *certificate_path;                         /**< The certificate path of private */
+    email_cipher_type               cipher_type;                              /**< The encryption algorithm */
+    email_digest_type               digest_type;                              /**< The digest algorithm */
+       char                           *user_name;                                /**< The user name for multi user (Since 2.4) */   
 } email_account_t;
 
 /**
- * This structure is used to save the information of certificate
+ * @brief The structure type to save the certificate information.
+ * @since_tizen 2.3
  */
 
 typedef struct 
 {
-       int certificate_id;
-       int issue_year;
-       int issue_month;
-       int issue_day;
-       int expiration_year;
-       int expiration_month;
-       int expiration_day;
-       char *issue_organization_name;
-       char *email_address;
-       char *subject_str;
-       char *filepath;
+    int certificate_id;               /**< The saved certificate ID */
+    int issue_year;                   /**< The issue year information of certificate */
+    int issue_month;                  /**< The issue month information of certificate */
+    int issue_day;                    /**< The issue day information of certificate */
+    int expiration_year;              /**< The expiration year information of certificate */
+    int expiration_month;             /**< The expiration month information of certificate */
+    int expiration_day;               /**< The expiration day information of certificate */
+    char *issue_organization_name;    /**< The issue organization information of certificate */
+    char *email_address;              /**< The email address of certificate */
+    char *subject_str;                /**< The subject information of certificate */
+    char *filepath;                   /**< The saved path of certificate */
 } email_certificate_t;
 
 /**
- * This structure is used to save the information of email server.
+ * @brief The structure type to save the email server information.
+ * @since_tizen 2.3
  */
 
 typedef struct
 {
-       int                         configuration_id;      /**< Specifies the id of configuration.*/
-       email_account_server_t      protocol_type;         /**< Specifies the type of configuration.*/
-       char                       *server_addr;           /**< Specifies the address of configuration.*/
-       int                         port_number;           /**< Specifies the port number of configuration.*/
-       int                         security_type;         /**< Specifies the security such as SSL.*/
-       int                         auth_type;             /**< Specifies the authentication type of configuration.*/
+    int                         configuration_id;      /**< The configuration ID */
+    email_account_server_t      protocol_type;         /**< The type of configuration */
+    char                       *server_addr;           /**< The address of configuration */
+    int                         port_number;           /**< The port number of configuration */
+    int                         security_type;         /**< The security such as SSL */
+    int                         auth_type;             /**< The authentication type of configuration */
 } email_protocol_config_t;
 
 typedef struct
 {
-       char                       *service_name;           /**< Specifies the name of service.*/
-       int                         authname_format;        /**< Specifies the type of user name for authentication.*/
-       int                         protocol_conf_count;    /**< Specifies count of protocol configurations.*/
-       email_protocol_config_t    *protocol_config_array;  /**< Specifies array of protocol configurations.*/
+    char                       *service_name;           /**< The name of service */
+    int                         authname_format;        /**< The type of user name for authentication */
+    int                         protocol_conf_count;    /**< The count of protocol configurations */
+    email_protocol_config_t    *protocol_config_array;  /**< The array of protocol configurations */
 } email_server_info_t;
 
 typedef struct
 {
-       int   mailbox_type;
-       char  mailbox_name[MAILBOX_NAME_LENGTH];
+    int   mailbox_type;                         /**< The mailbox type  */
+    char  mailbox_name[MAILBOX_NAME_LENGTH];    /**< The mailbox name */
 } email_mailbox_type_item_t;
 
 /**
- * This structure is contains the Mail information.
+ * @brief The structure type which contains the Mail information.
+ * @since_tizen 2.3
  */
 
 typedef struct
 {
-       int                   mail_id;                 /**< Specifies the Mail ID.*/
-       int                   account_id;              /**< Specifies the Account ID.*/
-       int                   mailbox_id;              /**< Specifies the Mailbox ID.*/
-       email_mailbox_type_e  mailbox_type;            /**< Specifies the mailbox type of the mail. */
-       char                 *subject;                 /**< Specifies the subject.*/
-       time_t                date_time;               /**< Specifies the Date time.*/
-       int                   server_mail_status;      /**< Specifies the Whether sever mail or not.*/
-       char                 *server_mailbox_name;     /**< Specifies the server mailbox.*/
-       char                 *server_mail_id;          /**< Specifies the Server Mail ID.*/
-       char                 *message_id;              /**< Specifies the message id */
-       int                   reference_mail_id;       /**< Specifies the reference mail id */
-       char                 *full_address_from;       /**< Specifies the From address.*/
-       char                 *full_address_reply;      /**< Specifies the Reply to address */
-       char                 *full_address_to;         /**< Specifies the To address.*/
-       char                 *full_address_cc;         /**< Specifies the CC address.*/
-       char                 *full_address_bcc;        /**< Specifies the BCC address*/
-       char                 *full_address_return;     /**< Specifies the return Path*/
-       char                 *email_address_sender;    /**< Specifies the email address of sender.*/
-       char                 *email_address_recipient; /**< Specifies the email address of recipients.*/
-       char                 *alias_sender;            /**< Specifies the alias of sender. */
-       char                 *alias_recipient;         /**< Specifies the alias of recipients. */
-       int                   body_download_status;    /**< Specifies the Text downloaded or not.*/
-       char                 *file_path_plain;         /**< Specifies the path of text mail body.*/
-       char                 *file_path_html;          /**< Specifies the path of HTML mail body.*/
-       char                 *file_path_mime_entity;   /**< Specifies the path of MIME entity. */
-       int                   mail_size;               /**< Specifies the Mail Size.*/
-       char                  flags_seen_field;        /**< Specifies the seen flags*/
-       char                  flags_deleted_field;     /**< Specifies the deleted flags*/
-       char                  flags_flagged_field;     /**< Specifies the flagged flags*/
-       char                  flags_answered_field;    /**< Specifies the answered flags*/
-       char                  flags_recent_field;      /**< Specifies the recent flags*/
-       char                  flags_draft_field;       /**< Specifies the draft flags*/
-       char                  flags_forwarded_field;   /**< Specifies the forwarded flags*/
-       int                   DRM_status;              /**< Has the mail DRM content? (1 : true, 0 : false) */
-       email_mail_priority_t priority;                /**< Specifies the priority of the mail.*/
-       email_mail_status_t   save_status;             /**< Specifies the save status*/
-       int                   lock_status;             /**< Specifies the mail is locked*/
-       email_mail_report_t   report_status;           /**< Specifies the Mail Report.*/
-       int                   attachment_count;        /**< Specifies the attachment count. */
-       int                   inline_content_count;    /**< Specifies the inline content count. */
-       int                   thread_id;               /**< Specifies the thread id for thread view. */
-       int                   thread_item_count;       /**< Specifies the item count of specific thread. */
-       char                 *preview_text;            /**< Specifies the preview body. */
-       email_mail_type_t     meeting_request_status;  /**< Specifies the status of meeting request. */
-       int                   message_class;           /**< Specifies the class of message for EAS. */ /* email_message_class */
-       email_digest_type     digest_type;             /**< Specifies the digest algorithm*/
-       email_smime_type      smime_type;              /**< Specifies the SMIME type. */
-       time_t                scheduled_sending_time;  /**< Specifies the scheduled sending time.*/
-       int                   remaining_resend_times;  /**< Specifies the remaining resend times.*/
-       int                   tag_id;                  /**< Specifies the data for filtering. */
-       time_t                replied_time;            /**< Specifies the time of replied. */
-       time_t                forwarded_time;          /**< Specifies the time of forwarded. */
-       int                   eas_data_length;         /**< Specifies the length of eas_data. */
-       void                 *eas_data;                /**< Extended Application Specific data. */
+    int                   mail_id;                 /**< The Mail ID */
+    int                   account_id;              /**< The Account ID */
+    int                   mailbox_id;              /**< The mailbox ID */
+    email_mailbox_type_e  mailbox_type;            /**< The mailbox type of the mail */
+    char                 *subject;                 /**< The subject */
+    time_t                date_time;               /**< The Date time */
+    int                   server_mail_status;      /**< The flag indicating whether sever mail or not */
+    char                 *server_mailbox_name;     /**< The server mailbox */
+    char                 *server_mail_id;          /**< The Server Mail ID */
+    char                 *message_id;              /**< The message ID */
+    int                   reference_mail_id;       /**< The reference mail ID */
+    char                 *full_address_from;       /**< The From address */
+    char                 *full_address_reply;      /**< The Reply to address */
+    char                 *full_address_to;         /**< The To address */
+    char                 *full_address_cc;         /**< The CC address */
+    char                 *full_address_bcc;        /**< The BCC address */
+    char                 *full_address_return;     /**< The return path */
+    char                 *email_address_sender;    /**< The email address of sender */
+    char                 *email_address_recipient; /**< The email address of recipients */
+    char                 *alias_sender;            /**< The alias of sender */
+    char                 *alias_recipient;         /**< The alias of recipients */
+    int                   body_download_status;    /**< The text download status */
+    char                 *file_path_plain;         /**< The path of text mail body */
+    char                 *file_path_html;          /**< The path of HTML mail body */
+    char                 *file_path_mime_entity;   /**< The path of MIME entity */
+    int                   mail_size;               /**< The mail size */
+    char                  flags_seen_field;        /**< The seen flags */
+    char                  flags_deleted_field;     /**< The deleted flags */
+    char                  flags_flagged_field;     /**< The flagged flags */
+    char                  flags_answered_field;    /**< The answered flags */
+    char                  flags_recent_field;      /**< The recent flags */
+    char                  flags_draft_field;       /**< The draft flags */
+    char                  flags_forwarded_field;   /**< The forwarded flags */
+    int                   DRM_status;              /**< The flag indicating whether the mail has DRM content (1 : true, 0 : false) */
+    email_mail_priority_t priority;                /**< The priority of the mail */
+    email_mail_status_t   save_status;             /**< The save status */
+    int                   lock_status;             /**< The mail is locked */
+    email_mail_report_t   report_status;           /**< The mail report */
+    int                   attachment_count;        /**< The attachment count */
+    int                   inline_content_count;    /**< The inline content count */
+    int                   thread_id;               /**< The thread ID for thread view */
+    int                   thread_item_count;       /**< The item count of specific thread */
+    char                 *preview_text;            /**< The preview body */
+    email_mail_type_t     meeting_request_status;  /**< The status of meeting request */
+    int                   message_class;           /**< The class of message for EAS */ /* email_message_class */
+    email_digest_type     digest_type;             /**< The digest algorithm */
+    email_smime_type      smime_type;              /**< The SMIME type */
+    time_t                scheduled_sending_time;  /**< The scheduled sending time */
+    int                   remaining_resend_times;  /**< The remaining resend times */
+    int                   tag_id;                  /**< The data for filtering */
+    time_t                replied_time;            /**< The time of replied */
+    time_t                forwarded_time;          /**< The time of forwarded */
+    char                 *pgp_password;            /**< The password of PGP. */
+    int                   eas_data_length;         /**< The length of eas_data */
+    void                 *eas_data;                /**< Extended Application Specific data */
+    char                 *user_name;               /**< The user information for multi user (Since 2.4) */
 } email_mail_data_t;
 
 /**
- * This structure is contains information for displaying mail list.
+ * @brief The structure type which contains information for displaying a mail list.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       int                   mail_id;                                            /**< Specifies the mail id.*/
-       int                   account_id;                                         /**< Specifies the account id.*/
-       int                   mailbox_id;                                         /**< Specifies the mailbox id.*/
-       email_mailbox_type_e  mailbox_type;                                       /**< Specifies the mailbox type of the mail. */
-       char                  full_address_from[STRING_LENGTH_FOR_DISPLAY];       /**< Specifies the full from email address.*/
-       char                  email_address_sender[MAX_EMAIL_ADDRESS_LENGTH];     /**< Specifies the sender email address.*/
-       char                  email_address_recipient[STRING_LENGTH_FOR_DISPLAY]; /**< Specifies the recipients email address.*/
-       char                  subject[STRING_LENGTH_FOR_DISPLAY];                 /**< Specifies the subject.*/
-       int                   body_download_status;                               /**< Specifies the text donwloaded or not.*/
-       int                   mail_size;                                          /**< Specifies the mail size. */
-       time_t                date_time;                                          /**< Specifies the date time.*/
-       char                  flags_seen_field;                                   /**< Specifies the seen flags*/
-       char                  flags_deleted_field;                                /**< Specifies the deleted flags*/
-       char                  flags_flagged_field;                                /**< Specifies the flagged flags*/
-       char                  flags_answered_field;                               /**< Specifies the answered flags*/
-       char                  flags_recent_field;                                 /**< Specifies the recent flags*/
-       char                  flags_draft_field;                                  /**< Specifies the draft flags*/
-       char                  flags_forwarded_field;                              /**< Specifies the forwarded flags*/
-       int                   DRM_status;                                         /**< Has the mail DRM content? (1 : true, 0 : false) */
-       email_mail_priority_t priority;                                           /**< Specifies the priority of Mails.*/ /* email_mail_priority_t*/
-       email_mail_status_t   save_status;                                        /**< Specifies the save status*/ /* email_mail_status_t */
-       int                   lock_status;                                        /**< Specifies the locked*/
-       email_mail_report_t   report_status;                                      /**< Specifies the mail report.*/ /* email_mail_report_t */
-       int                   attachment_count;                                   /**< Specifies the attachment count. */
-       int                   inline_content_count;                               /**< Specifies the inline content count. */
-       char                  preview_text[MAX_PREVIEW_TEXT_LENGTH];              /**< text for preview body*/
-       int                   thread_id;                                          /**< thread id for thread view*/
-       int                   thread_item_count;                                  /**< item count of specific thread */
-       email_mail_type_t     meeting_request_status;                             /**< Whether the mail is a meeting request or not */ /* email_mail_type_t */
-       int                   message_class;                                      /**< Specifies the message class */ /* email_message_class */
-       email_smime_type      smime_type;                                         /**< Specifies the smime type */ /* email_smime_type */
-       time_t                scheduled_sending_time;                             /**< Specifies the scheduled sending time.*/
-       int                   remaining_resend_times;                             /**< Specifies the remaining resend times.*/
-       int                   tag_id;                                             /**< Specifies the data for filtering */
-       int                   eas_data_length;                                    /**< Specifies the length of eas_data. */
-       void                 *eas_data;                                           /**< Extended Application Specific data. */
+    int                   mail_id;                                            /**< The mail ID */
+    int                   account_id;                                         /**< The account ID */
+    int                   mailbox_id;                                         /**< The mailbox ID */
+    email_mailbox_type_e  mailbox_type;                                       /**< The mailbox type of the mail */
+    char                  full_address_from[STRING_LENGTH_FOR_DISPLAY];       /**< The full from email address */
+    char                  email_address_sender[MAX_EMAIL_ADDRESS_LENGTH];     /**< The sender email address */
+    char                  email_address_recipient[STRING_LENGTH_FOR_DISPLAY]; /**< The recipients email address */
+    char                  subject[STRING_LENGTH_FOR_DISPLAY];                 /**< The subject */
+    int                   body_download_status;                               /**< The text download status */
+    int                   mail_size;                                          /**< The mail size */
+    time_t                date_time;                                          /**< The date time */
+    char                  flags_seen_field;                                   /**< The seen flags */
+    char                  flags_deleted_field;                                /**< The deleted flags */
+    char                  flags_flagged_field;                                /**< The flagged flags */
+    char                  flags_answered_field;                               /**< The answered flags */
+    char                  flags_recent_field;                                 /**< The recent flags */
+    char                  flags_draft_field;                                  /**< The draft flags */
+    char                  flags_forwarded_field;                              /**< The forwarded flags */
+    int                   DRM_status;                                         /**< The flag indicating whether the has mail DRM content (1 : true, 0 : false) */
+    email_mail_priority_t priority;                                           /**< The priority of mails */ /* email_mail_priority_t*/
+    email_mail_status_t   save_status;                                        /**< The save status */ /* email_mail_status_t */
+    int                   lock_status;                                        /**< The lock status */
+    email_mail_report_t   report_status;                                      /**< The mail report */ /* email_mail_report_t */
+    int                   attachment_count;                                   /**< The attachment count */
+    int                   inline_content_count;                               /**< The inline content count */
+    char                  preview_text[MAX_PREVIEW_TEXT_LENGTH];              /**< The text for preview body */
+    int                   thread_id;                                          /**< The thread ID for thread view */
+    int                   thread_item_count;                                  /**< The item count of specific thread */
+    email_mail_type_t     meeting_request_status;                             /**< The flag indicating whether the mail is a meeting request or not */ /* email_mail_type_t */
+    int                   message_class;                                      /**< The message class */ /* email_message_class */
+    email_smime_type      smime_type;                                         /**< The smime type */ /* email_smime_type */
+    time_t                scheduled_sending_time;                             /**< The scheduled sending time */
+    int                   remaining_resend_times;                             /**< The remaining resend times */
+    int                   tag_id;                                             /**< The data for filtering */
+    int                   eas_data_length;                                    /**< The length of eas_data */
+    void                 *eas_data;                                           /**< Extended Application Specific data */
 } email_mail_list_item_t;
 
 /**
- * This structure is used to save the filtering structure.
+ * @brief The structure type used to save the filtering structure.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       int                  account_id;          /**< Specifies the account ID.*/
-       int                  filter_id;           /**< Specifies the filtering ID.*/
-       char                *filter_name;         /**< Specifies the filtering name. */
-       email_rule_type_t    type;                /**< Specifies the filtering type.*/
-       char                *value;               /**< Specifies the filtering value : subject. */
-       char                *value2;              /**< Specifies the filtering value2 : sender address. */
-       email_rule_action_t  faction;             /**< Specifies the action type for filtering.*/
-       int                  target_mailbox_id;   /**< Specifies the mail box if action type means move.*/
-       int                  flag1;               /**< Specifies the activation.*/
-       int                  flag2;               /**< Reserved.*/
+    int                  account_id;          /**< The account ID */
+    int                  filter_id;           /**< The filtering ID */
+    char                *filter_name;         /**< The filtering name */
+    email_rule_type_t    type;                /**< The filtering type */
+    char                *value;               /**< The filtering value : subject */
+    char                *value2;              /**< The filtering value2 : sender address */
+    email_rule_action_t  faction;             /**< The action type for filtering */
+    int                  target_mailbox_id;   /**< The mail box if action type means move */
+    int                  flag1;               /**< The activation */
+    int                  flag2;               /**< Reserved */
 } email_rule_t;
 
 /**
- * This structure is used to save the information of mail flag.
+ * @brief The structure type used to save the information of a mail flag.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       unsigned int  seen           : 1; /**< Specifies the read email.*/
-       unsigned int  deleted        : 1; /**< Reserved.*/
-       unsigned int  flagged        : 1; /**< Specifies the flagged email.*/
-       unsigned int  answered       : 1; /**< Reserved.*/
-       unsigned int  recent         : 1; /**< Reserved.*/
-       unsigned int  draft          : 1; /**< Specifies the draft email.*/
-       unsigned int  attachment_count : 1; /**< Reserved.*/
-       unsigned int  forwarded      : 1; /**< Reserved.*/
-       unsigned int  sticky         : 1; /**< Sticky flagged mails cannot be deleted */
+    unsigned int  seen           : 1;    /**< The read email */
+    unsigned int  deleted        : 1;    /**< The deleted email */
+    unsigned int  flagged        : 1;    /**< The flagged email */
+    unsigned int  answered       : 1;    /**< The answered email */
+    unsigned int  recent         : 1;    /**< The recent email */
+    unsigned int  draft          : 1;    /**< The draft email */
+    unsigned int  attachment_count : 1;  /**< The attachment count */
+    unsigned int  forwarded      : 1;    /**< The forwarded email. */
+    unsigned int  sticky         : 1;    /**< The sticky flagged mails cannot be deleted */
 } email_mail_flag_t /* DEPRECATED */;
 
 
 /**
- * This structure is used to save the information of mail extra flag.
+ * @brief The structure type used to save the information of a mail extra flag.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       unsigned int  priority         : 3; /**< Specifies the mail priority.
-                                           The value is greater than or equal to EMAIL_MAIL_PRIORITY_HIGH.
-                                           The value is less than or equal to EMAIL_MAIL_PRIORITY_LOW.*/
-       unsigned int  status           : 4; /**< Specifies the mail status.
-                                              The value is a value of enumeration email_mail_status_t.*/
-       unsigned int  noti             : 1; /**< Specifies the notified mail.*/
-       unsigned int  lock             : 1; /**< Specifies the locked mail.*/
-       unsigned int  report           : 2; /**< Specifies the MDN/DSN mail. The value is a value of enumeration email_mail_report_t.*/
-       unsigned int  drm              : 1; /**< Specifies the drm mail.*/
-       unsigned int  text_download_yn : 2; /**< body download y/n*/
+    unsigned int  priority         : 3; /**< The mail priority \n
+                                             The value is greater than or equal to EMAIL_MAIL_PRIORITY_HIGH.
+                                             The value is less than or equal to EMAIL_MAIL_PRIORITY_LOW.*/
+    unsigned int  status           : 4; /**< The mail status \n
+                                             The value is a value of enumeration email_mail_status_t.*/
+    unsigned int  noti             : 1; /**< The notified mail */
+    unsigned int  lock             : 1; /**< The locked mail */
+    unsigned int  report           : 2; /**< The MDN/DSN mail \n 
+                                             The value is a value of enumeration email_mail_report_t.*/
+    unsigned int  drm              : 1; /**< The DRM mail */
+    unsigned int  text_download_yn : 2; /**< The body download y/n */
 } email_extra_flag_t DEPRECATED;
 
 /**
- * This structure is used to save the information of attachment.
+ * @brief The structure type used to save the information of an attachment.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       int   attachment_id;
-       char *attachment_name;
-       char *attachment_path;
-       int   attachment_size;
-       int   mail_id;
-       int   account_id;
-       char  mailbox_id;
-       int   save_status;
-       int   drm_status;
-       int   inline_content_status;
-       char *attachment_mime_type; /**< Specifies the context mime type.*/
+    int   attachment_id;         /**< The attachment ID */
+    char *attachment_name;       /**< The attachment name */
+    char *attachment_path;       /**< The path of a downloaded attachment */
+    char *content_id;            /**< The content ID string of an attachment */
+    int   attachment_size;       /**< The size of an attachment */
+    int   mail_id;               /**< The mail ID of an included attachment */
+    int   account_id;            /**< The account ID of an included attachment */
+    char  mailbox_id;            /**< The mailbox ID of an include attachment */
+    int   save_status;           /**< The save status of an attachment */
+    int   drm_status;            /**< The DRM status of an attachment */
+    int   inline_content_status; /**< The distinguished inline attachment and normal attachment */
+    char *attachment_mime_type;  /**< The context MIME type */
 } email_attachment_data_t;
 
 typedef struct
 {
-       int        offset_from_GMT;
-       char       standard_name[32];
-       struct tm  standard_time_start_date;
-       int        standard_bias;
-       char       daylight_name[32];
-       struct tm  daylight_time_start_date;
-       int        daylight_bias;
+    int        offset_from_GMT;             /**< The time from base GMT  */
+    char       standard_name[32];           /**< The name of time zone */
+    struct tm  standard_time_start_date;    /**< The start date of time zone */
+    int        standard_bias;               /**< The bias of time zone */
+    char       daylight_name[32];           /**< The daylight name of time zone */
+    struct tm  daylight_time_start_date;    /**< The daylight start date of time zone */
+    int        daylight_bias;               /**< The daylight bias of time zone */
 } email_time_zone_t;
 
+/**
+ * @brief Structure used to save the information of a meeting request
+ * @since_tizen 2.3
+ */
 typedef struct
 {
-       int                        mail_id;                                        /**< Specifies the mail id of meeting request on DB. This is the primary key. */
-       email_meeting_response_t   meeting_response;                               /**< Specifies the meeting response. */
-       struct tm                  start_time;
-       struct tm                  end_time;
-       char                      *location;                                       /**< Specifies the location of meeting. Maximum length of this string is 32768 */
-       char                      *global_object_id;                               /**< Specifies the object id. */
-       email_time_zone_t          time_zone;
+    int                        mail_id;                                        /**< The mail ID of meeting request on DB <br> 
+                                                                                    This is the primary key. */
+    email_meeting_response_t   meeting_response;                               /**< The meeting response */
+    struct tm                  start_time;                                     /**< The meeting start time */
+    struct tm                  end_time;                                       /**< The meeting end time */
+    char                      *location;                                       /**< The location of meeting <br> 
+                                                                                    Maximum length of this string is 32768. */
+    char                      *global_object_id;                               /**< The object ID */
+    email_time_zone_t          time_zone;                                      /**< The time zone of meeting */
 } email_meeting_request_t;
 
 /**
- * This structure is used to save the information of sender list with unread/total mail counts
+ * @brief The structure type used to save the information of a sender list with unread/total mail counts.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       char *address;         /**< Specifies the address of a sender.*/
-       char *display_name;    /**< Specifies a display name. This may be one of contact name, alias in original mail and email address of sender. (Priority order : contact name, alias, email address) */
-       int   unread_count;    /**< Specifies the number of unread mails received from sender address*/
-       int   total_count;     /**< Specifies the total number of  mails which are received from sender address*/
+    char *address;         /**< The address of a sender */
+    char *display_name;    /**< The display name <br> 
+                                This may be one of contact name, alias in original mail and email address of sender. 
+                                (Priority order : contact name, alias, email address) */
+    int   unread_count;    /**< The number of unread mails received from sender address */
+    int   total_count;     /**< The total number of  mails which are received from sender address */
 } email_sender_list_t /* DEPRECATED */;
 
 
 /**
- * This structure is used to save the information of mailbox.
+ * @brief The structure type used to save the information of a mailbox.
+ * @since_tizen 2.3
  */
 typedef struct
 {
-       int                   mailbox_id;                 /**< Unique id on mailbox table.*/
-       char                 *mailbox_name;               /**< Specifies the path of mailbox.*/
-       email_mailbox_type_e  mailbox_type;               /**< Specifies the type of mailbox */
-       char                 *alias;                      /**< Specifies the display name of mailbox.*/
-       int                   unread_count;               /**< Specifies the unread mail count in the mailbox.*/
-       int                   total_mail_count_on_local;  /**< Specifies the total number of mails in the mailbox in the local DB.*/
-       int                   total_mail_count_on_server; /**< Specifies the total number of mails in the mailbox in the mail server.*/
-       int                   local;                      /**< Specifies the local mailbox.*/
-       int                   account_id;                 /**< Specifies the account ID for mailbox.*/
-       int                   mail_slot_size;             /**< Specifies how many mails can be stored in local mailbox.*/
-       int                   no_select;                  /**< Specifies the 'no_select' attribute from xlist.*/
-       time_t                last_sync_time;
-       int                   deleted_flag;               /**< Specifies whether mailbox is deleted.*/
-       int                   eas_data_length;            /**< Specifies the length of eas_data. */
-       void                 *eas_data;                   /**< Specifies the data for eas engine. */
+    int                   mailbox_id;                 /**< The unique ID on mailbox table */
+    char                 *mailbox_name;               /**< The path of mailbox */
+    email_mailbox_type_e  mailbox_type;               /**< The type of mailbox */
+    char                 *alias;                      /**< The display name of mailbox */
+    int                   unread_count;               /**< The unread mail count in the mailbox */
+    int                   total_mail_count_on_local;  /**< The total number of mails in the mailbox in the local DB */
+    int                   total_mail_count_on_server; /**< The total number of mails in the mailbox in the mail server */
+    int                   local;                      /**< The local mailbox */
+    int                   account_id;                 /**< The account ID for mailbox */
+    int                   mail_slot_size;             /**< The number of mails that can be stored in local mailbox */
+    int                   no_select;                  /**< The 'no_select' attribute from xlist */
+    time_t                last_sync_time;             /**< The last sync time of the mailbox */
+    int                   deleted_flag;               /**< The flag specifying whether mailbox is deleted */
+    int                   eas_data_length;            /**< The length of eas_data */
+    void                 *eas_data;                   /**< The data for eas engine. */
 } email_mailbox_t;
 
+/**
+ * @brief Structure used to save the information of email a address.
+ * @since_tizen 2.3
+ */
+
 typedef struct
 {
-       email_address_type_t  address_type;
-       char                 *address;
-       char                 *display_name;
-       int                   storage_type;
-       int                   contact_id;
+    email_address_type_t  address_type;    /**< The address type using the email (TO, CC, BCC, etc..) */
+    char                 *address;         /**< The Email address */
+    char                 *display_name;    /**< The alias */
+    int                   storage_type;    /**< The type of saved storage on contact-service */
+    int                   contact_id;      /**< The ID of saved storage on contact-service */
 } email_address_info_t;
 
+/**
+ * @brief Structure used to save the set of email addresses
+ * @since_tizen 2.3
+ */
 typedef struct
 {
-       GList *from;
-       GList *to;
-       GList *cc;
-       GList *bcc;
+    GList *from;    /**< The From address list */
+    GList *to;      /**< The TO address list */
+    GList *cc;      /**< The CC address list */
+    GList *bcc;     /**< The BCC address list */
 } email_address_info_list_t;
 
+/**
+ * @brief Structure used to save the list of email addresses
+ * @since_tizen 2.3
+ */
 typedef struct
 {
-       int     address_type;           /* type of mail (sender : 0, recipient : 1)*/
-       int     address_count;  /*  The number of email addresses */
-       char  **address_list;   /*  strings of email addresses */
+    int address_type;               /**< The type of mail (sender : 0, recipient : 1)*/
+    int     address_count;          /**< The number of email addresses */
+    char  **address_list;           /**< The strings of email addresses */
 } email_email_address_list_t;
 
 
+/**
+ * @brief Structure used to save the information of a search filter
+ * @since_tizen 2.3
+ */
 typedef struct _email_search_filter_t {
-       email_search_filter_type search_filter_type; /* type of search filter */
-       union {
-               int            integer_type_key_value;
-               time_t         time_type_key_value;
-               char          *string_type_key_value;
-       } search_filter_key_value;
+    email_search_filter_type search_filter_type;      /**< The type of search filter */
+    union {
+        int            integer_type_key_value;        /**< The integer value for searching */
+        time_t         time_type_key_value;           /**< The time value for searching */
+        char          *string_type_key_value;         /**< The string value for searching */
+    } search_filter_key_value;                        /**< The key value of search filter */
 } email_search_filter_t;
 
 typedef enum {
-       EMAIL_LIST_FILTER_RULE_EQUAL                  = 0,
-       EMAIL_LIST_FILTER_RULE_NOT_EQUAL              = 1,
-       EMAIL_LIST_FILTER_RULE_LESS_THAN              = 2,
-       EMAIL_LIST_FILTER_RULE_GREATER_THAN           = 3,
-       EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL     = 4,
-       EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL  = 5,
-       EMAIL_LIST_FILTER_RULE_INCLUDE                = 6,
-       EMAIL_LIST_FILTER_RULE_MATCH                  = 7,
-       EMAIL_LIST_FILTER_RULE_IN                     = 8,
-       EMAIL_LIST_FILTER_RULE_NOT_IN                 = 9
+    EMAIL_LIST_FILTER_RULE_EQUAL                  = 0,    /**< EQUAL(=) of filter type */
+    EMAIL_LIST_FILTER_RULE_NOT_EQUAL              = 1,    /**< NOT EQUAL(!=) of filter type */
+    EMAIL_LIST_FILTER_RULE_LESS_THAN              = 2,    /**< LESS THAN(<) of filter type */
+    EMAIL_LIST_FILTER_RULE_GREATER_THAN           = 3,    /**< GREATER THAN(>) of filter type */
+    EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL     = 4,    /**< LESS THAN OR EQUAL(<=) of filter type */
+    EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL  = 5,    /**< GREATER THAN OR EQUAL(>=) of filter type */
+    EMAIL_LIST_FILTER_RULE_INCLUDE                = 6,    /**< LIKE operator in SQL statements */
+    EMAIL_LIST_FILTER_RULE_MATCH                  = 7,    /**< MATCH operator in SQL statements */
+    EMAIL_LIST_FILTER_RULE_IN                     = 8,    /**< IN operator in SQL statements */
+    EMAIL_LIST_FILTER_RULE_NOT_IN                 = 9     /**< NOT IN operator in SQL statements */
 } email_list_filter_rule_type_t;
 
 typedef enum {
-       EMAIL_CASE_SENSITIVE                          = 0,
-       EMAIL_CASE_INSENSITIVE                        = 1,
+    EMAIL_CASE_SENSITIVE                          = 0,    /**< Case sensitive search */
+    EMAIL_CASE_INSENSITIVE                        = 1,    /**< Case insensitive search */
 } email_list_filter_case_sensitivity_t;
 
 typedef union {
-       int                                    integer_type_value;
-       char                                  *string_type_value;
-       time_t                                 datetime_type_value;
+    int                                    integer_type_value;    /**< Container for integer type value */
+    char                                  *string_type_value;     /**< Container for string type value  */
+    time_t                                 datetime_type_value;   /**< Container for datetime type value  */
 } email_mail_attribute_value_t;
 
 typedef struct {
-       email_list_filter_rule_type_t          rule_type;
-       email_mail_attribute_type              target_attribute;
-       email_mail_attribute_value_t           key_value;
-       email_list_filter_case_sensitivity_t   case_sensitivity;
+    email_list_filter_rule_type_t          rule_type;             /**< Operator type of the filter rule */
+    email_mail_attribute_type              target_attribute;      /**< Target attribute type of the filter rule */
+    email_mail_attribute_value_t           key_value;             /**< Target attribute value of the filter rule */
+    email_list_filter_case_sensitivity_t   case_sensitivity;      /**< Specifies case sensitivity of the filter rule */
 } email_list_filter_rule_t;
 
 typedef struct {
-       email_list_filter_rule_type_t          rule_type;
-       email_mail_text_attribute_type         target_attribute;
-       email_mail_attribute_value_t           key_value;
+    email_list_filter_rule_type_t          rule_type;             /**< Operator type of the filter rule for full text search */
+    email_mail_text_attribute_type         target_attribute;      /**< Target attribute type of the filter rule */
+    email_mail_attribute_value_t           key_value;             /**< Target attribute value of the filter rule */
 } email_list_filter_rule_fts_t;
 
 typedef struct {
-       email_list_filter_rule_type_t          rule_type;
-       email_mail_attach_attribute_type       target_attribute;
-       email_mail_attribute_value_t           key_value;
-       email_list_filter_case_sensitivity_t   case_sensitivity;
+    email_list_filter_rule_type_t          rule_type;             /**< Operator type of the filter rule for searching attachment */
+    email_mail_attach_attribute_type       target_attribute;      /**< Target attribute type of the filter rule */
+    email_mail_attribute_value_t           key_value;             /**< Target attribute value of the filter rule */
+    email_list_filter_case_sensitivity_t   case_sensitivity;      /**< Specifies case sensitivity of the filter rule */
 } email_list_filter_rule_attach_t;
 
 typedef enum {
-       EMAIL_LIST_FILTER_ITEM_RULE                     = 0,
-       EMAIL_LIST_FILTER_ITEM_RULE_FTS                 = 1,
-       EMAIL_LIST_FILTER_ITEM_RULE_ATTACH              = 2,
-       EMAIL_LIST_FILTER_ITEM_OPERATOR                 = 3,
+    EMAIL_LIST_FILTER_ITEM_RULE                     = 0,    /**< Normal search rule */
+    EMAIL_LIST_FILTER_ITEM_RULE_FTS                 = 1,    /**< Full text search rule */
+    EMAIL_LIST_FILTER_ITEM_RULE_ATTACH              = 2,    /**< Searching attachment rule */
+    EMAIL_LIST_FILTER_ITEM_OPERATOR                 = 3,    /**< Operator */
 } email_list_filter_item_type_t;
 
 typedef enum {
-       EMAIL_LIST_FILTER_OPERATOR_AND                  = 0,
-       EMAIL_LIST_FILTER_OPERATOR_OR                   = 1,
-       EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS     = 2,
-       EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS    = 3
+    EMAIL_LIST_FILTER_OPERATOR_AND                  = 0,    /**< AND operator */
+    EMAIL_LIST_FILTER_OPERATOR_OR                   = 1,    /**< OR operator */
+    EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS     = 2,    /**< ( operator */
+    EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS    = 3     /**< ) operator */
 } email_list_filter_operator_type_t;
 
+/**
+ * @brief Structure used to save the information of a list filter
+ * @since_tizen 2.3
+ */
 typedef struct {
-       email_list_filter_item_type_t          list_filter_item_type;
+    email_list_filter_item_type_t          list_filter_item_type;    /**< Filter item type */
 
-       union {
-               email_list_filter_rule_t           rule;
-               email_list_filter_rule_fts_t       rule_fts;
-               email_list_filter_rule_attach_t    rule_attach;
-               email_list_filter_operator_type_t  operator_type;
-       } list_filter_item;
+    union {
+        email_list_filter_rule_t           rule;           /**< Rule type */
+        email_list_filter_rule_fts_t       rule_fts;       /**< Rule type for full text search */
+        email_list_filter_rule_attach_t    rule_attach;    /**< Rule type for searching attachment */
+        email_list_filter_operator_type_t  operator_type;  /**< Operator type */
+    } list_filter_item;    /**< filter item of list */
 
 } email_list_filter_t;
 
 typedef enum {
-       EMAIL_SORT_ORDER_ASCEND                             = 0,
-       EMAIL_SORT_ORDER_DESCEND                            = 1,
-       EMAIL_SORT_ORDER_TO_CCBCC                           = 2,
-       EMAIL_SORT_ORDER_TO_CC_BCC                          = 3,
-       EMAIL_SORT_ORDER_TO_CCBCC_ALL                       = 4
+    EMAIL_SORT_ORDER_ASCEND                             = 0,    /**< Ascending order sorting */
+    EMAIL_SORT_ORDER_DESCEND                            = 1,    /**< Descending order sorting */
+    EMAIL_SORT_ORDER_TO_CCBCC                           = 2,    /**< full_address_to sorting */
+    EMAIL_SORT_ORDER_TO_CC_BCC                          = 3,    /**< full_address_to, full_address_cc sorting */
+    EMAIL_SORT_ORDER_TO_CCBCC_ALL                       = 4,    /**< full_address_to of all account sorting */
+    EMAIL_SORT_ORDER_NOCASE_ASCEND                      = 5,    /**< Ascending order with COLLATE NOCASE option*/
+    EMAIL_SORT_ORDER_NOCASE_DESCEND                     = 6,    /**< Descending order with COLLATE NOCASE option */
+    EMAIL_SORT_ORDER_LOCALIZE_ASCEND                    = 7,    /**< Localize ascending order sorting */
+    EMAIL_SORT_ORDER_LOCALIZE_DESCEND                   = 8     /**< Localize descending order sorting */
 } email_list_filter_sort_order_t;
 
 
 typedef struct {
-       email_mail_attribute_type              target_attribute;
-       email_mail_attribute_value_t           key_value;
-       bool                                   force_boolean_check;
-       email_list_filter_sort_order_t         sort_order;
+    email_mail_attribute_type              target_attribute;    /**< The attribute type of mail for sorting*/
+    email_mail_attribute_value_t           key_value;           /**< The attribute value of mail for sorting*/
+    bool                                   force_boolean_check; /**< The distinguished true/false for sorting  */
+    email_list_filter_sort_order_t         sort_order;          /**< The ordering rule for sorting */
 } email_list_sorting_rule_t;
 
 
 typedef struct {
-       int                                    handle;
-       int                                    account_id;
-       email_event_type_t                     type;
-       email_event_status_type_t              status;
+    int                                    handle;        /**< The job handle to be canceled */
+    int                                    account_id;    /**< The account ID for task information */
+    email_event_type_t                     type;          /**< The type for task information */
+    email_event_status_type_t              status;        /**< The status for task information */
 } email_task_information_t;
 
 typedef enum {
-       EMAIL_MESSAGE_CONTEXT_VOICE_MESSAGE                 = 1,
-       EMAIL_MESSAGE_CONTEXT_VIDEO_MESSAGE                 = 2,
-       EMAIL_MESSAGE_CONTEXT_FAX_MESSAGE                   = 3,
-       EMAIL_MESSAGE_CONTEXT_X_EMPTY_CALL_CAPTURE_MESSAGE  = 4,
-       EMAIL_MESSAGE_CONTEXT_X_NUMBER_MESSAGE              = 5,
-       EMAIL_MESSAGE_CONTEXT_X_VOICE_INFOTAINMENT_MESSAGE  = 6
+    EMAIL_MESSAGE_CONTEXT_VOICE_MESSAGE                 = 1,    /**< Voice message context */
+    EMAIL_MESSAGE_CONTEXT_VIDEO_MESSAGE                 = 2,    /**< Video message context */
+    EMAIL_MESSAGE_CONTEXT_FAX_MESSAGE                   = 3,    /**< Fax message context */
+    EMAIL_MESSAGE_CONTEXT_X_EMPTY_CALL_CAPTURE_MESSAGE  = 4,    /**< X empty call capture message context */
+    EMAIL_MESSAGE_CONTEXT_X_NUMBER_MESSAGE              = 5,    /**< X number message context */
+    EMAIL_MESSAGE_CONTEXT_X_VOICE_INFOTAINMENT_MESSAGE  = 6     /**< X voice infotainment message context */
 } email_message_context_t;
 
 typedef enum {
-       EMAIL_MESSAGE_SENSITIVITY_PRIVATE                   = 1,
-       EMAIL_MESSAGE_SENSITIVITY_CONFIDENTIAL              = 2,
-       EMAIL_MESSAGE_SENSITIVITY_PERSONAL                  = 3
+    EMAIL_MESSAGE_SENSITIVITY_PRIVATE                   = 1,    /**< Private sensitivity of message */
+    EMAIL_MESSAGE_SENSITIVITY_CONFIDENTIAL              = 2,    /**< Confidential sensitivity of message */
+    EMAIL_MESSAGE_SENSITIVITY_PERSONAL                  = 3     /**< Personal sensitivity of message */
 } email_message_sensitivity_t;
 
 typedef struct {
-       email_message_context_t                message_context;
-       int                                    content_length;     /* Content-Duration for audio/video or X-Content-Pages for fax */
-       email_message_sensitivity_t            sensitivity;
+    email_message_context_t                message_context;    /**< Message context for visual voice mail */
+    int                                    content_length;     /**< Content-Duration for audio/video or X-Content-Pages for fax */
+    email_message_sensitivity_t            sensitivity;        /**< Sensitivity of message for visual voice mail */
 } email_vvm_specific_data_t;
 
 typedef struct {
-       char *quota_root;
-       char *resource_name;
-       int   usage;
-       int   limits;
+    char *quota_root;       /**< Quota root for IMAP quota */
+    char *resource_name;    /**< Resource name for IMAP quota */
+    int   usage;            /**< Usage for IMAP quota */
+    int   limits;           /**< Limit for IMAP quota */
 } email_quota_resource_t;
 
 /*****************************************************************************/
@@ -1738,294 +1980,316 @@ typedef struct {
 
 typedef enum
 {
-       ACTIVE_SYNC_NOTI_SEND_MAIL,                                   /*  a sending notification to ASE (active sync engine */
-       ACTIVE_SYNC_NOTI_SEND_SAVED,                                  /*  a sending notification to ASE (active sync engine), All saved mails should be sent */
-       ACTIVE_SYNC_NOTI_SEND_REPORT,                                 /*  a sending notification to ASE (active sync engine), report should be sen */
-       ACTIVE_SYNC_NOTI_SYNC_HEADER,                                 /*  a sync header - download mails from server. */
-                                                                  /*  It is depended on account/s flag1 field whether it excutes downloading header only or downloading header + body */
-                                                                  /*  downloading option : 0 is subject only, 1 is text body, 2 is normal */
-       ACTIVE_SYNC_NOTI_DOWNLOAD_BODY,                               /*  a downloading body notification to AS */
-       ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT,                         /*  a downloading attachment notification to AS */
-       ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT,                            /*  a account validating notification to AS */
-       ACTIVE_SYNC_NOTI_CANCEL_JOB,                                  /*  a cancling job notification to AS */
-       ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER,                            /*  a searching on server notification to AS */
-       ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER,            /*  a notification for clearing result of search on server to AS */
-       ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED,               /*  a notification to expunge deleted flagged mails */
-       ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT,                           /*  a notification to get the resolve recipients */
-       ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE,                        /*  a notification to validate certificate */
-       ACTIVE_SYNC_NOTI_ADD_MAILBOX,                                 /*  a notification to add mailbox */
-       ACTIVE_SYNC_NOTI_RENAME_MAILBOX,                              /*  a notification to rename mailbox */
-       ACTIVE_SYNC_NOTI_DELETE_MAILBOX,                              /*  a notification to delete mailbox */
-       ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL,                         /*  a notification to cancel a sending mail */
-       ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX,                           /*  a notification to delete multiple mailboxes */
-       ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL, /*  a notification to send a mail with downloading attachment of original mail */
-       ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL,                       /*  a notification to schedule a mail to send later*/
-}      eactivesync_noti_t;
+    ACTIVE_SYNC_NOTI_SEND_MAIL,                                   /**<  Sending notification to ASE (active sync engine) */
+    ACTIVE_SYNC_NOTI_SEND_SAVED,                                  /**<  Sending notification to ASE (active sync engine), all saved mails should be sent */
+    ACTIVE_SYNC_NOTI_SEND_REPORT,                                 /**<  Sending notification to ASE (active sync engine), report should be sent */
+    ACTIVE_SYNC_NOTI_SYNC_HEADER,                                 /**<  Sync header - download mails from server <br>
+                                                                        It depends on account/s flag1 field whether it executes downloading header only or downloading header + body
+                                                                        downloading option : 0 is subject only, 1 is text body, 2 is normal */
+    ACTIVE_SYNC_NOTI_DOWNLOAD_BODY,                               /**<  Downloading body notification to AS */
+    ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT,                         /**<  Downloading attachment notification to AS */
+    ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT,                            /**<  Account validating notification to AS */
+    ACTIVE_SYNC_NOTI_CANCEL_JOB,                                  /**<  Canceling job notification to AS */
+    ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER,                            /**<  Searching on server notification to AS */
+    ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER,            /**<  Notification for clearing result of search on server to AS */
+    ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED,               /**<  Notification to expunge deleted flagged mails */
+    ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT,                           /**<  Notification to get the resolve recipients */
+    ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE,                        /**<  Notification to validate certificate */
+    ACTIVE_SYNC_NOTI_ADD_MAILBOX,                                 /**<  Notification to add mailbox */
+    ACTIVE_SYNC_NOTI_RENAME_MAILBOX,                              /**<  Notification to rename mailbox */
+    ACTIVE_SYNC_NOTI_DELETE_MAILBOX,                              /**<  Notification to delete mailbox */
+    ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL,                         /**<  Notification to cancel a sending mail */
+    ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX,                           /**<  Notification to delete multiple mailboxes */
+    ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL, /**<  Notification to send a mail with downloading attachment of original mail */
+    ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL,                       /**<  Notification to schedule a mail to send later*/
+}   eactivesync_noti_t;
 
 typedef union
 {
-       struct _send_mail
-       {
-               int             handle;
-               int             account_id;
-               int             mail_id;
-       } send_mail;
-
-       struct _send_mail_saved
-       {/*  not defined ye */
-               int             handle;
-               int             account_id;
-       } send_mail_saved;
-
-       struct _send_report
-       {/*  not defined ye */
-               int             handle;
-               int             account_id;
-       } send_report;
-
-       struct _sync_header
-       {
-               int             handle;
-               int             account_id;
-               int             mailbox_id;
-       } sync_header;
-
-       struct _download_body
-       {
-               int             handle;
-               int             account_id;
-               int             mail_id;
-               int             with_attachment;      /*  0: without attachments, 1: with attachment */
-       } download_body;
-
-       struct _download_attachment
-       {
-               int             handle;
-               int             account_id;
-               int             mail_id;
-               int             attachment_order;
-       } download_attachment;
-
-       struct _cancel_job
-       {
-               int             account_id;
-               int             handle;               /*  job handle to be canceled. this value is issued by email-service. */
-               int             cancel_type;
-       } cancel_job;
-
-       struct _validate_account
-       {/*  not defined yet */
-               int             handle;
-               int             account_id;
-       } validate_account;
-
-       struct _search_mail_on_server
-       {
-               int                    handle;
-               int                    account_id;
-               int                                        mailbox_id;
-               email_search_filter_t *search_filter_list;
-               int                    search_filter_count;
-       } search_mail_on_server;
-
-       struct _clear_result_of_search_mail_on_server
-       {
-               int                    handle;
-               int                    account_id;
-       } clear_result_of_search_mail_on_server;
-
-       struct _expunge_mails_deleted_flagged
-       {
-               int                    handle;
-               int                    account_id;
-               int                    mailbox_id;
-               int                    on_server;
-       } expunge_mails_deleted_flagged;
-
-       struct _get_resolve_recipients
-       {
-               int                     handle;
-               int                     account_id;
-               char                   *email_address;
-       } get_resolve_recipients;
-
-       struct _validate_certificate
-       {
-               int                     handle;
-               int                     account_id;
-               char                   *email_address;
-       } validate_certificate;
-
-       struct _add_mailbox
-       {
-               int                     handle;
-               int                     account_id;
-               char                   *mailbox_path;
-               char                   *mailbox_alias;
-               void                   *eas_data;
-               int                     eas_data_length;
-       } add_mailbox;
-
-       struct _rename_mailbox
-       {
-               int                     handle;
-               int                     account_id;
-               int                     mailbox_id;
-               char                   *mailbox_name;
-               char                   *mailbox_alias;
-               void                   *eas_data;
-               int                     eas_data_length;
-       } rename_mailbox;
-
-       struct _delete_mailbox
-       {
-               int                     handle;
-               int                     account_id;
-               int                     mailbox_id;
-       } delete_mailbox;
-
-       struct _cancel_sending_mail
-       {
-               int                     handle;
-               int                     account_id;
-               int                     mail_id;
-       } cancel_sending_mail;
-
-       struct _delete_mailbox_ex
-       {
-               int                     handle;
-               int                     account_id;
-               int                    *mailbox_id_array;
-               int                     mailbox_id_count;
-               int                     on_server;
-       } delete_mailbox_ex;
-
-       struct _send_mail_with_downloading_attachment_of_original_mail
-       {
-               int                     handle;
-               int                     account_id;
-               int                     mail_id;
-       } send_mail_with_downloading_attachment_of_original_mail;
-
-       struct _schedule_sending_mail
-       {
-               int                     handle;
-               int                     account_id;
-               int                     mail_id;
-               time_t                  scheduled_time;
-       } schedule_sending_mail;
+    struct _send_mail
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for sending the mail */
+        int                     mail_id;           /**< The mail ID for sending the mail */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */    
+       } send_mail;    /**< Noti data for sending the mail */
+
+    struct _send_mail_saved
+    {/*  not defined ye */
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for sending the saved mail */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */    
+       } send_mail_saved;    /**< Noti data for sending the saved mail */
+
+    struct _send_report
+    {/*  not defined ye */
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for sending the report */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } send_report;    /**< Noti data for sending the report */
+
+    struct _sync_header
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for syncing the header */
+        int                     mailbox_id;        /**< The mailbox ID for syncing the header */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } sync_header;    /**< Noti data for syncing the header */
+
+    struct _download_body
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for downloading the body */
+        int                     mail_id;           /**< The mail ID for downloading the body */
+        int                     with_attachment;   /**< 0: without attachments, 1: with attachment */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } download_body;    /**< Noti data for downloading the body */
+
+    struct _download_attachment
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for downloading the attachment */
+        int                     mail_id;           /**< The mail ID for downloading the attachment */
+        int                     attachment_order;  /**< The ordered attachment for downloading the attachment */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } download_attachment;    /**< Noti data for downloading the attachment */
+
+    struct _cancel_job
+    {
+        int                     account_id;        /**< The account ID for canceling the job */
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     cancel_type;       /**< The canceling type for canceling the job */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } cancel_job;    /**< Noti data for canceling the job */
+
+    struct _validate_account
+    {/*  not defined yet */
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for validating the account */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } validate_account;    /**< Noti data for validating the account */
+
+    struct _search_mail_on_server
+    {
+        int                     handle;              /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;          /**< The account ID for searching the mail on server */
+        int                     mailbox_id;          /**< The mailbox ID for searching the mail on server */
+        email_search_filter_t  *search_filter_list;  /**< The list of search filter for searching the mail on server */
+        int                     search_filter_count; /**< The count of search filter for searching the mail on server */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } search_mail_on_server;    /**< Noti data for searching the mail on server */
+
+    struct _clear_result_of_search_mail_on_server
+    { 
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for clearing the result of search mail on server */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } clear_result_of_search_mail_on_server;    /**< Noti data for clearing the result of search mail on server */
+
+    struct _expunge_mails_deleted_flagged
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for expunging the mails flagged for deleting */
+        int                     mailbox_id;        /**< The mailbox ID for expunging the mails flagged for deleting */
+        int                     on_server;         /**< The flag indicating whether the mail is on server for expunging the mails flagged for deleting */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } expunge_mails_deleted_flagged;    /**< Noti data for expunging the mails flagged for deleting */
+
+    struct _get_resolve_recipients
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for getting the resolve recipients */
+        char                   *email_address;     /**< The email address for getting the resolve recipients */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } get_resolve_recipients;    /**< Noti data for getting the resolve recipients */
+
+    struct _validate_certificate
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for validating the certificate */
+        char                   *email_address;     /**< The email address for validating the certifiate */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } validate_certificate;    /**< Noti data for validating the certificate */
+
+    struct _add_mailbox
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for adding the mailbox */
+        char                   *mailbox_path;      /**< The path of mailbox for adding the mailbox */
+        char                   *mailbox_alias;     /**< The alias of mailbox for adding the mailbox */
+        void                   *eas_data;          /**< The eas-data for adding the mailbox */
+        int                     eas_data_length;   /**< The length of eas-data for adding the mailbox */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } add_mailbox;    /**< Noti data for adding the mailbox */
+
+    struct _rename_mailbox
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for renaming the mailbox */
+        int                     mailbox_id;        /**< The mailbox ID for renaming the mailbox */
+        char                   *mailbox_name;      /**< The mailbox name for renaming the mailbox */
+        char                   *mailbox_alias;     /**< The alias of mailbox for renaming the mailbox */
+        void                   *eas_data;          /**< The eas-data for renaming the mailbox */
+        int                     eas_data_length;   /**< The length of eas-data for renaming the mailbox */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } rename_mailbox;    /**< Noti data for renaming the mailbox */
+
+    struct _delete_mailbox
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for deleting mailbox */
+        int                     mailbox_id;        /**< The mailbox ID for deleting mailbox */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } delete_mailbox;    /**< Noti data for deleting mailbox */
+
+    struct _cancel_sending_mail
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for canceled sending mail */
+        int                     mail_id;           /**< The mail ID for canceled sending mail */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } cancel_sending_mail;    /**< Noti data for canceled sending mail */
+
+    struct _delete_mailbox_ex
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for deleting mailbox (extention) */
+        int                    *mailbox_id_array;  /**< The mailbox ID array for deleting mailbox (extention) */
+        int                     mailbox_id_count;  /**< The mailbox ID count for deleting mailbox (extention) */
+        int                     on_server;         /**< The on server for deleting mailbox (extention) */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } delete_mailbox_ex;    /**< Noti data for deleting mailbox (extention) */
+
+    struct _send_mail_with_downloading_attachment_of_original_mail
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for downloading attachment */
+        int                     mail_id;           /**< The mail ID for download attachment */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } send_mail_with_downloading_attachment_of_original_mail;    /**< Noti data for send mail with downloading attachment of original mail */
+
+    struct _schedule_sending_mail
+    {
+        int                     handle;            /**< The job handle to be canceled. This value is issued by email-service. */
+        int                     account_id;        /**< The account ID for scheduled sending mail */
+        int                     mail_id;           /**< The mail ID for scheduled sending mail */
+        time_t                  scheduled_time;    /**< The scheduled time for scheduled sending mail */
+        char                   *multi_user_name;   /**< Speicifes the supporting for multi user (Since 2.4) */
+    } schedule_sending_mail;    /**< Noti data for schedule sending mail */
 } ASNotiData;
 
-/*  types for noti string */
+/** @brief Enumeration for noti string types.
+ * @since_tizen 2.3 */
 typedef enum
 {
-       EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM,      /** specifies email_mail_list_t */
+    EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM,      /**< specifies email_mail_list_t */
 } email_convert_struct_type_e;
 
-/* Tasks */
+/** @brief Enumeration for task types.
+ * @since_tizen 2.3 */
 typedef enum {
-       /* Sync tasks */
-       EMAIL_SYNC_TASK_BOUNDARY_START                                          = 11000,
-       /* Sync tasks for account - from 11000 */
-       EMAIL_SYNC_TASK_ADD_ACCOUNT                                             = 11010,
-       EMAIL_SYNC_TASK_DELETE_ACCOUNT                                          = 11020,
-       EMAIL_SYNC_TASK_UPDATE_ACCOUNT                                          = 11030,
-       EMAIL_SYNC_TASK_GET_ACCOUNT                                             = 11040,
-       EMAIL_SYNC_TASK_GET_ACCOUNT_LIST                                        = 11050,
-       EMAIL_SYNC_TASK_BACKUP_ACCOUNTS                                         = 11060,
-       EMAIL_SYNC_TASK_RESTORE_ACCOUNTS                                        = 11070,
-       EMAIL_SYNC_TASK_GET_PASSWORD_LENGTH_OF_ACCOUNT                          = 11090,
-
-       /* Sync tasks for mailbox - from 12000 */
-       EMAIL_SYNC_TASK_GET_MAILBOX_COUNT                                       = 12010,
-       EMAIL_SYNC_TASK_GET_MAILBOX_LIST                                        = 12020,
-       EMAIL_SYNC_TASK_GET_SUB_MAILBOX_LIST                                    = 12030,
-       EMAIL_SYNC_TASK_SET_MAIL_SLOT_SIZE                                      = 12040,
-       EMAIL_SYNC_TASK_SET_MAILBOX_TYPE                                        = 12050,
-       EMAIL_SYNC_TASK_SET_LOCAL_MAILBOX                                       = 12060,
-
-       /* Sync tasks for mail - from 13000 */
-       EMAIL_SYNC_TASK_GET_ATTACHMENT                                          = 13010,
-       EMAIL_SYNC_TASK_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER                   = 13020,
-       EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL                                   = 13030,
-       EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE                                        = 13040,
-
-       /* Sync tasks for mail thread - from 14000 */
-
-       /* Sync tasks for rule - from 15000 */
-
-       /* Sync tasks for etc - from 16000 */
-       EMAIL_SYNC_TASK_BOUNDARY_END                                            = 59999,
-       /* Async tasks */
-       EMAIL_ASYNC_TASK_BOUNDARY_START                                         = 60000,
-       /* Async tasks for account - from 61000 */
-       EMAIL_ASYNC_TASK_VALIDATE_ACCOUNT                                       = 61010,
-       EMAIL_ASYNC_TASK_ADD_ACCOUNT_WITH_VALIDATION                            = 61020,
-
-       /* Async tasks for mailbox - from 62000 */
-       EMAIL_ASYNC_TASK_ADD_MAILBOX                                            = 62010,
-       EMAIL_ASYNC_TASK_DELETE_MAILBOX                                         = 62020,
-       EMAIL_ASYNC_TASK_RENAME_MAILBOX                                         = 62030,
-       EMAIL_ASYNC_TASK_DOWNLOAD_IMAP_MAILBOX_LIST                             = 62040,
-       EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX                                      = 62050,
-
-       /* Async tasks for mail - from 63000 */
-       EMAIL_ASYNC_TASK_ADD_MAIL                                               = 63010,
-       EMAIL_ASYNC_TASK_ADD_READ_RECEIPT                                       = 63020,
-
-       EMAIL_ASYNC_TASK_UPDATE_MAIL                                            = 63030,
-
-       EMAIL_ASYNC_TASK_DELETE_MAIL                                            = 63040,
-       EMAIL_ASYNC_TASK_DELETE_ALL_MAIL                                        = 63050,
-       EMAIL_ASYNC_TASK_EXPUNGE_MAILS_DELETED_FLAGGED                          = 63060,
-
-       EMAIL_ASYNC_TASK_MOVE_MAIL                                              = 63070,
-       EMAIL_ASYNC_TASK_MOVE_ALL_MAIL                                          = 63080,
-       EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT               = 63090,
-
-       EMAIL_ASYNC_TASK_SET_FLAGS_FIELD                                        = 63300,
-
-       EMAIL_ASYNC_TASK_DOWNLOAD_MAIL_LIST                                     = 63400,
-       EMAIL_ASYNC_TASK_DOWNLOAD_BODY                                          = 63410,
-       EMAIL_ASYNC_TASK_DOWNLOAD_ATTACHMENT                                    = 63420,
-
-       EMAIL_ASYNC_TASK_SEND_MAIL                                              = 63500,
-       EMAIL_ASYNC_TASK_SEND_SAVED                                             = 63510,
-       EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL = 63520,
-
-       EMAIL_ASYNC_TASK_SEARCH_MAIL_ON_SERVER                                  = 63600,
-
-       /* Async tasks for mail thread - from 64000 */
-       EMAIL_ASYNC_TASK_MOVE_THREAD_TO_MAILBOX                                 = 64010,
-       EMAIL_ASYNC_TASK_DELETE_THREAD                                          = 64020,
-       EMAIL_ASYNC_TASK_MODIFY_SEEN_FLAG_OF_THREAD                             = 64030,
-
-       /* Async tasks for rule - from 65000 */
-
-       /* Async tasks for etc - from 66000 */
-       EMAIL_ASYNC_TASK_BOUNDARY_END                                           = 99999,
+    /* Sync tasks */
+    EMAIL_SYNC_TASK_BOUNDARY_START                                          = 11000,    /**< Sync task for boundary start */
+    /* Sync tasks for account - from 11000 */
+    EMAIL_SYNC_TASK_ADD_ACCOUNT                                             = 11010,    /**< Sync task for adding the account */
+    EMAIL_SYNC_TASK_DELETE_ACCOUNT                                          = 11020,    /**< Sync task for deleting the account */
+    EMAIL_SYNC_TASK_UPDATE_ACCOUNT                                          = 11030,    /**< Sync task for updating the account */
+    EMAIL_SYNC_TASK_GET_ACCOUNT                                             = 11040,    /**< Sync task for getting the account */
+    EMAIL_SYNC_TASK_GET_ACCOUNT_LIST                                        = 11050,    /**< Sync task for getting the account list */
+    EMAIL_SYNC_TASK_BACKUP_ACCOUNTS                                         = 11060,    /**< Sync task for backup the account */
+    EMAIL_SYNC_TASK_RESTORE_ACCOUNTS                                        = 11070,    /**< Sync task for restoring the account */
+    EMAIL_SYNC_TASK_GET_PASSWORD_LENGTH_OF_ACCOUNT                          = 11090,    /**< Sync task for getting the password length of account */
+
+    /* Sync tasks for mailbox - from 12000 */
+    EMAIL_SYNC_TASK_GET_MAILBOX_COUNT                                       = 12010,    /**< Sync task for getting the mailbox count */
+    EMAIL_SYNC_TASK_GET_MAILBOX_LIST                                        = 12020,    /**< Sync task for getting the mailbox list */
+    EMAIL_SYNC_TASK_GET_SUB_MAILBOX_LIST                                    = 12030,    /**< Sync task for getting the sub-mailbox list */
+    EMAIL_SYNC_TASK_SET_MAIL_SLOT_SIZE                                      = 12040,    /**< Sync task for setting the mail slot size */
+    EMAIL_SYNC_TASK_SET_MAILBOX_TYPE                                        = 12050,    /**< Sync task for setting the mailbox type */
+    EMAIL_SYNC_TASK_SET_LOCAL_MAILBOX                                       = 12060,    /**< Sync task for setting the local mailbox */
+
+    /* Sync tasks for mail - from 13000 */
+    EMAIL_SYNC_TASK_GET_ATTACHMENT                                          = 13010,    /**< Sync task for getting the attachment */
+    EMAIL_SYNC_TASK_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER                   = 13020,    /**< Sync task for clearing the result of search mail on server */
+    EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL                                   = 13030,    /**< Sync task for scheduling the sending mail */
+    EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE                                        = 13040,    /**< Sync task for updating the attribute */
+
+    /* Sync tasks for mail thread - from 14000 */
+
+    /* Sync tasks for rule - from 15000 */
+
+    /* Sync tasks for etc - from 16000 */
+    EMAIL_SYNC_TASK_BOUNDARY_END                                            = 59999,    /**< Sync task for boundary end */
+    /* Async tasks */
+    EMAIL_ASYNC_TASK_BOUNDARY_START                                         = 60000,    /**< Async task for boundary start */
+    /* Async tasks for account - from 61000 */
+    EMAIL_ASYNC_TASK_VALIDATE_ACCOUNT                                       = 61010,    /**< Async task for validating the account */
+    EMAIL_ASYNC_TASK_ADD_ACCOUNT_WITH_VALIDATION                            = 61020,    /**< Async task for adding the account with validation */
+
+    /* Async tasks for mailbox - from 62000 */
+    EMAIL_ASYNC_TASK_ADD_MAILBOX                                            = 62010,    /**< Async task for adding the mailbox */
+    EMAIL_ASYNC_TASK_DELETE_MAILBOX                                         = 62020,    /**< Async task for deleting the mailbox */
+    EMAIL_ASYNC_TASK_RENAME_MAILBOX                                         = 62030,    /**< Async task for renaming the mailbox */
+    EMAIL_ASYNC_TASK_DOWNLOAD_IMAP_MAILBOX_LIST                             = 62040,    /**< Async task for downloading the imap mailbox list */
+    EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX                                      = 62050,    /**< Async task for deleting the mailbox : extentiong version */
+
+    /* Async tasks for mail - from 63000 */
+    EMAIL_ASYNC_TASK_ADD_MAIL                                               = 63010,    /**< Async task for adding the mail */
+    EMAIL_ASYNC_TASK_ADD_READ_RECEIPT                                       = 63020,    /**< Async task for adding the read receipt */
+
+    EMAIL_ASYNC_TASK_UPDATE_MAIL                                            = 63030,    /**< Async task for updating the mail */
+
+    EMAIL_ASYNC_TASK_DELETE_MAIL                                            = 63040,    /**< Async task for deleting the mail */
+    EMAIL_ASYNC_TASK_DELETE_ALL_MAIL                                        = 63050,    /**< Async task for deleting the all mails */
+    EMAIL_ASYNC_TASK_EXPUNGE_MAILS_DELETED_FLAGGED                          = 63060,    /**< Async task for expunging the deleted flagged mails */
+
+    EMAIL_ASYNC_TASK_MOVE_MAIL                                              = 63070,    /**< Async task for moving the mail */
+    EMAIL_ASYNC_TASK_MOVE_ALL_MAIL                                          = 63080,    /**< Async task for moving the all mails */
+    EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT               = 63090,    /**< Async task for moving the mails to mailbox of another account */
+
+    EMAIL_ASYNC_TASK_SET_FLAGS_FIELD                                        = 63300,    /**< Async task for setting the flags field */
+
+    EMAIL_ASYNC_TASK_DOWNLOAD_MAIL_LIST                                     = 63400,    /**< Async task for downloading the mail list */
+    EMAIL_ASYNC_TASK_DOWNLOAD_BODY                                          = 63410,    /**< Async taks for downloading the body */
+    EMAIL_ASYNC_TASK_DOWNLOAD_ATTACHMENT                                    = 63420,    /**< Async task for downloading the attachment */
+
+    EMAIL_ASYNC_TASK_SEND_MAIL                                              = 63500,    /**< Async task for sending the mail */
+    EMAIL_ASYNC_TASK_SEND_SAVED                                             = 63510,    /**< Async task for sending the saved */
+    EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL = 63520,    /**< Async task for sending the mail with downloading attachment of original mail */
+
+    EMAIL_ASYNC_TASK_SEARCH_MAIL_ON_SERVER                                  = 63600,    /**< Async task for searching the mail on server */
+
+    /* Async tasks for mail thread - from 64000 */
+    EMAIL_ASYNC_TASK_MOVE_THREAD_TO_MAILBOX                                 = 64010,    /**< Async task for moving the thread to mailbox */
+    EMAIL_ASYNC_TASK_DELETE_THREAD                                          = 64020,    /**< Async task for deleting the thread */
+    EMAIL_ASYNC_TASK_MODIFY_SEEN_FLAG_OF_THREAD                             = 64030,    /**< Async task for modified the seen flag of thread */
+
+    /* Async tasks for rule - from 65000 */
+
+    /* Async tasks for etc - from 66000 */
+    EMAIL_ASYNC_TASK_BOUNDARY_END                                           = 99999,    /**< Async tasks for boundary end*/
 } email_task_type_t;
 
 typedef enum
 {
-       EMAIL_TASK_STATUS_UNUSED                   = 0,
-       EMAIL_TASK_STATUS_WAIT                     = 1,
-       EMAIL_TASK_STATUS_STARTED                  = 2,
-       EMAIL_TASK_STATUS_IN_PROGRESS              = 3,
-       EMAIL_TASK_STATUS_FINISHED                 = 4,
-       EMAIL_TASK_STATUS_FAILED                   = 5,
-       EMAIL_TASK_STATUS_CANCELED                 = 6,
-       EMAIL_TASK_STATUS_SCHEDULED                = 7,
+    EMAIL_TASK_STATUS_UNUSED                   = 0,    /**< The status of task is unused */
+    EMAIL_TASK_STATUS_WAIT                     = 1,    /**< The status of task is waited */
+    EMAIL_TASK_STATUS_STARTED                  = 2,    /**< The status of task is started */
+    EMAIL_TASK_STATUS_IN_PROGRESS              = 3,    /**< The status of task is in progress */
+    EMAIL_TASK_STATUS_FINISHED                 = 4,    /**< The status of task is finished */
+    EMAIL_TASK_STATUS_FAILED                   = 5,    /**< The status of task is failed */
+    EMAIL_TASK_STATUS_CANCELED                 = 6,    /**< The status of task is canceled */
+    EMAIL_TASK_STATUS_SCHEDULED                = 7,    /**< The status of task is scheduled */
 } email_task_status_type_t;
 
 typedef enum
 {
-       EMAIL_TASK_PRIORITY_UNUSED                 = 0,
-       EMAIL_TASK_PRIORITY_SCHEDULED              = 1,
-       EMAIL_TASK_PRIORITY_LOW                    = 3,
-       EMAIL_TASK_PRIORITY_MID                    = 5,
-       EMAIL_TASK_PRIORITY_HIGH                   = 7,
-       EMAIL_TASK_PRIORITY_BACK_GROUND_TASK       = 9,
+    EMAIL_TASK_PRIORITY_UNUSED                 = 0,    /**< Unused the priority task*/
+    EMAIL_TASK_PRIORITY_SCHEDULED              = 1,    /**< Scheduled the priority task */
+    EMAIL_TASK_PRIORITY_LOW                    = 3,    /**< The priority task is low */
+    EMAIL_TASK_PRIORITY_MID                    = 5,    /**< The priority task is MID */
+    EMAIL_TASK_PRIORITY_HIGH                   = 7,    /**< The priority task is HIGH */
+    EMAIL_TASK_PRIORITY_BACK_GROUND_TASK       = 9,    /**< The priority task is back ground */
 } email_task_priority_t;
 /* Tasks */
 
@@ -2034,7 +2298,7 @@ typedef enum
 #endif
 
 /**
-* @} @}
+* @}
 */
 
 #endif /* __EMAIL_TYPES_H__ */
index 9a9ebf5..ad3640c 100755 (executable)
@@ -44,18 +44,21 @@ INTERNAL_FUNC int   em_upper_path(char *path);
 
 INTERNAL_FUNC void  em_skip_whitespace(char *addr_str , char **pAddr);
 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string);
+INTERNAL_FUNC void  em_skip_whitespace_without_alias(char *addr_str, char **pAddr);
 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string);
 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string);
+INTERNAL_FUNC int   em_replace_string_ex(char **input_source_string, char *input_old_string, char *input_new_string);
 INTERNAL_FUNC void  em_flush_memory();
 INTERNAL_FUNC int   em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name);
 INTERNAL_FUNC int   em_get_file_name_and_extension_from_file_path(char *input_source_file_path, char **output_file_name, char **output_extention);
 INTERNAL_FUNC char* em_get_extension_from_file_path(char *source_file_path, int *err_code);
 INTERNAL_FUNC int   em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type);
 INTERNAL_FUNC int   em_get_content_type_from_extension_string(const char *extension_string, int *err_code);
+INTERNAL_FUNC char *em_shrink_filename(char *fname, int size_limit);
 
-INTERNAL_FUNC int   em_verify_email_address(char *address, int without_bracket);
-INTERNAL_FUNC int   em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket);
-INTERNAL_FUNC int   em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket);
+INTERNAL_FUNC int   em_verify_email_address(char *address);
+INTERNAL_FUNC int   em_verify_email_address_of_mail_data(email_mail_data_t *mail_data);
+INTERNAL_FUNC int   em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl);
 
 INTERNAL_FUNC int   em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size);
 INTERNAL_FUNC int   em_find_tag_for_thread_view(char *subject, int *result);
@@ -65,11 +68,9 @@ INTERNAL_FUNC int   em_decode_base64(unsigned char *enc_text, unsigned long enc_
 
 extern        char* strcasestr(__const char *__haystack, __const char *__needle) __THROW __attribute_pure__ __nonnull ((1, 2));
 
-INTERNAL_FUNC int   em_get_account_server_type_by_account_id(int account_id, email_account_server_t* account_server_type, int flag, int *error);
+INTERNAL_FUNC int   em_get_account_server_type_by_account_id(char *multi_user_name, int account_id, email_account_server_t* account_server_type, int flag, int *error);
 
 INTERNAL_FUNC int   em_get_handle_for_activesync(int *handle, int *error);
-INTERNAL_FUNC int   em_check_socket_privilege_by_pid(int pid);
-INTERNAL_FUNC int   em_check_db_privilege_by_pid(int pid);
 
 /* thread handle definition */
 typedef struct {
@@ -82,10 +83,11 @@ typedef struct {
        void *thread_exit_arg;
 } email_thread_handle_t;
 
-INTERNAL_FUNC email_thread_handle_t* em_thread_create ();
+INTERNAL_FUNC email_thread_handle_t* em_thread_create(void *(*thread_exit)(void*), void *arg);
 INTERNAL_FUNC void em_thread_destroy (email_thread_handle_t* th);
 INTERNAL_FUNC void em_thread_run (email_thread_handle_t *th, void *(*thread_func)(void*), void *(*destroy)(void*), void* arg);
 INTERNAL_FUNC void em_thread_join (email_thread_handle_t *th);
-
+INTERNAL_FUNC int  em_fopen(const char *filename, const char *mode, FILE **fp);
+INTERNAL_FUNC int  em_open(const char *filename, int oflags, mode_t mode, int *handle);
 
 #endif /* __EMAIL_UTILITIES_H__ */
index e07534a..4221266 100755 (executable)
@@ -13,7 +13,7 @@ MESSAGE(">>> Build type: ${CMAKE_BUILD_TYPE}")
 
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VISIBILITY} -fvisibility=hidden")
-
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
 
 ##########################################################
 # Define device lib
@@ -29,9 +29,8 @@ INCLUDE_DIRECTORIES(
        ${CMAKE_SOURCE_DIR}/email-core/email-device/include
 )
 
-
 INCLUDE(FindPkgConfig)
-pkg_check_modules(device_pkgs REQUIRED glib-2.0 dlog vconf pmapi dbus-1)
+pkg_check_modules(device_pkgs REQUIRED glib-2.0 dlog vconf capi-system-device dbus-1)
 
 set(EXTRA_CFLAGS "")
 FOREACH(flag ${device_pkgs_CFLAGS})
@@ -47,7 +46,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 ADD_LIBRARY(${DEVICE-LIB} SHARED ${DEVICE-SRCS})
-TARGET_LINK_LIBRARIES(${DEVICE-LIB} ${device_pkgs_LDFLAGS} ${BASE-LIB})
+TARGET_LINK_LIBRARIES(${DEVICE-LIB} ${device_pkgs_LDFLAGS})
 SET_TARGET_PROPERTIES(${DEVICE-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
 SET_TARGET_PROPERTIES(${DEVICE-LIB} PROPERTIES VERSION ${VERSION})
 
@@ -72,9 +71,7 @@ INCLUDE_DIRECTORIES(
 
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(storage_pkgs REQUIRED drm-client glib-2.0 dlog db-util openssl vconf secure-storage dbus-1 libtzplatform-config)
-
-#pkg_check_modules(storage_pkgs REQUIRED drm-client glib-2.0 dlog db-util openssl vconf secure-storage dbus-1 contacts-service2 libtzplatform-config)
+pkg_check_modules(storage_pkgs REQUIRED glib-2.0 dlog openssl vconf secure-storage dbus-1 contacts-service2)
 
 set(EXTRA_CFLAGS "")
 FOREACH(flag ${storage_pkgs_CFLAGS})
@@ -90,7 +87,6 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 ADD_LIBRARY(${STORAGE-LIB} SHARED ${STORAGE-SRCS})
-TARGET_LINK_LIBRARIES(${STORAGE-LIB} ${storage_LDFLAGS} ${BASE-LIB} ss-client)
 SET_TARGET_PROPERTIES(${STORAGE-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
 SET_TARGET_PROPERTIES(${STORAGE-LIB} PROPERTIES VERSION ${VERSION})
 
@@ -127,55 +123,80 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 ADD_LIBRARY(${NETWORK-LIB} SHARED ${NETWORK-SRCS})
-TARGET_LINK_LIBRARIES(${NETWORK-LIB} ${network_pkgs_LDFLAGS} ${BASE-LIB} ${STORAGE-LIB} dl)
+TARGET_LINK_LIBRARIES(${NETWORK-LIB} ${network_pkgs_LDFLAGS} ${STORAGE-LIB} dl)
 SET_TARGET_PROPERTIES(${NETWORK-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
 SET_TARGET_PROPERTIES(${NETWORK-LIB} PROPERTIES VERSION ${VERSION})
 
 INSTALL(TARGETS ${NETWORK-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries)
 
 ##########################################################
-# Define Core-sound lib
+# Define container lib                                                  
 ##########################################################
 
-SET(CORE-SOUND-LIB "email-core-sound")
-SET(CORE-SOUND-SRCS 
-       ${CMAKE_SOURCE_DIR}/email-core/email-core-sound.c
+SET(CONTAINER-LIB "email-container")
+SET(CONTAINER-SRCS 
+    ${CMAKE_SOURCE_DIR}/email-core/email-core-container.c
 )
 
 INCLUDE_DIRECTORIES(
        ${CMAKE_SOURCE_DIR}/email-common-use/include
-       ${CMAKE_SOURCE_DIR}/email-daemon/include/
-       ${CMAKE_SOURCE_DIR}/email-core/include
-       ${CMAKE_SOURCE_DIR}/email-core/email-storage/include
 )
 
-
 INCLUDE(FindPkgConfig)
-pkg_check_modules(core_sound_pkgs REQUIRED glib-2.0 dlog dbus-1 gthread-2.0 
-                       vconf vconf-internal-keys mm-player mm-session feedback 
-                       alarm-service notification badge)
+SET(PKG_MODULES glib-2.0)
 
-set(EXTRA_CFLAGS "")
-FOREACH(flag ${core_sound_pkgs_CFLAGS})
+# __FEATURE_CONTAINER_ENABLE__
+#SET(PKG_MODULES glib-2.0 vasum)
+
+pkg_check_modules(container_pkgs REQUIRED ${PKG_MODULES})
+
+FOREACH(flag ${container_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 
-#message(">> core extra raw: ${core_sound_pkgs_CFLAGS}")
-#message(">> core extra : ${EXTRA_CFLAGS}")
-#message(">> core result: ${CMAKE_C_FLAGS}")
-
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
-ADD_LIBRARY(${CORE-SOUND-LIB} SHARED ${CORE-SOUND-SRCS})
+ADD_LIBRARY(${CONTAINER-LIB} SHARED ${CONTAINER-SRCS})
+TARGET_LINK_LIBRARIES(${CONTAINER-LIB} ${container_pkgs_LDFLAGS} dl)
+SET_TARGET_PROPERTIES(${CONTAINER-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
+SET_TARGET_PROPERTIES(${CONTAINER-LIB} PROPERTIES VERSION ${VERSION})
 
-TARGET_LINK_LIBRARIES(${CORE-SOUND-LIB} ${core_sound_pkgs_LDFLAGS} ${STORAGE-LIB} ${CORE-LIB})
+INSTALL(TARGETS ${CONTAINER-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries)
 
-SET_TARGET_PROPERTIES(${CORE-SOUND-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
-SET_TARGET_PROPERTIES(${CORE-SOUND-LIB} PROPERTIES VERSION ${VERSION})
+##########################################################
+# Define cynara lib                                                  
+##########################################################
+
+#SET(CYNARA-LIB "email-cynara")
+#SET(CYNARA-SRCS 
+#    ${CMAKE_SOURCE_DIR}/email-core/email-core-cynara.c
+#)
+
+#INCLUDE_DIRECTORIES(
+#      ${CMAKE_SOURCE_DIR}/email-common-use/include
+#)
+
+#INCLUDE(FindPkgConfig)
+#SET(PKG_MODULES cynara-client cynara-creds-socket)
+
+#pkg_check_modules(cynara_pkgs REQUIRED ${PKG_MODULES})
 
-INSTALL(TARGETS ${CORE-SOUND-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries)
+#FOREACH(flag ${cynara_pkgs_CFLAGS})
+#      SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+#ENDFOREACH(flag)
+
+#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+#SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
+
+#ADD_LIBRARY(${CYNARA-LIB} SHARED ${CYNARA-SRCS})
+#TARGET_LINK_LIBRARIES(${CYNARA-LIB} ${cynara_pkgs_LDFLAGS} dl)
+#SET_TARGET_PROPERTIES(${CYNARA-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
+#SET_TARGET_PROPERTIES(${CYNARA-LIB} PROPERTIES VERSION ${VERSION})
+
+#INSTALL(TARGETS ${CYNARA-LIB} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries)
 
 ##########################################################
 # Define Core lib
@@ -196,11 +217,12 @@ SET(CORE-SRCS
        ${CMAKE_SOURCE_DIR}/email-core/email-core-global.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-account.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-mime.c
-#      ${CMAKE_SOURCE_DIR}/email-core/email-core-sound.c
+       ${CMAKE_SOURCE_DIR}/email-core/email-core-gmime.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-task-manager.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-tasks.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-signal.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-alarm.c
+       ${CMAKE_SOURCE_DIR}/email-core/email-core-auto-download.c
 )
 
 INCLUDE_DIRECTORIES(
@@ -209,14 +231,17 @@ INCLUDE_DIRECTORIES(
        ${CMAKE_SOURCE_DIR}/email-core/email-storage/include
        ${CMAKE_SOURCE_DIR}/email-core/email-network/include
        ${CMAKE_SOURCE_DIR}/email-core/email-device/include
+       ${CMAKE_SOURCE_DIR}/email-ipc/email-activation/include/
 )
 
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(core_pkgs REQUIRED glib-2.0 drm-client dlog dbus-1 gthread-2.0 uw-imap-toolkit vconf vconf-internal-keys secure-storage openssl accounts-svc mm-player mm-session feedback alarm-service notification libcurl libxml-2.0 cert-svc cert-svc-vcore badge capi-appfw-application icu-i18n libtzplatform-config)
-
+SET(PKG_MODULES glib-2.0 dlog dbus-1 gthread-2.0
+                       uw-imap-toolkit vconf vconf-internal-keys contacts-service2 secure-storage
+                       openssl accounts-svc alarm-service notification libcurl libxml-2.0 feedback
+                       cert-svc cert-svc-vcore badge capi-appfw-application icu-i18n gmime-2.6 storage)
 
-#pkg_check_modules(core_pkgs REQUIRED glib-2.0 drm-client dlog dbus-1 gthread-2.0 uw-imap-toolkit vconf vconf-internal-keys contacts-service2 secure-storage openssl accounts-svc mm-player mm-session feedback alarm-service notification libcurl libxml-2.0 cert-svc cert-svc-vcore badge capi-appfw-application icu-i18n libtzplatform-config)
+pkg_check_modules(core_pkgs REQUIRED ${PKG_MODULES})
 
 set(EXTRA_CFLAGS "")
 FOREACH(flag ${core_pkgs_CFLAGS})
@@ -248,7 +273,9 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 ADD_LIBRARY(${CORE-LIB} SHARED ${CORE-SRCS})
 
-TARGET_LINK_LIBRARIES(${CORE-LIB} ${core_pkgs_LDFLAGS} ${STORAGE-LIB} ${NETWORK-LIB} ${DEVICE-LIB} ${CORE-SOUND-LIB})
+TARGET_LINK_LIBRARIES(${CORE-LIB} ${core_pkgs_LDFLAGS} ${STORAGE-LIB} ${NETWORK-LIB} ${DEVICE-LIB} ${CONTAINER-LIB})
+
+#TARGET_LINK_LIBRARIES(${CORE-LIB} ${core_pkgs_LDFLAGS} ${STORAGE-LIB} ${NETWORK-LIB} ${DEVICE-LIB} ${CONTAINER-LIB} ${CYNARA-LIB})
 
 SET_TARGET_PROPERTIES(${CORE-LIB} PROPERTIES SOVERSION ${VERSION_MAJOR})
 SET_TARGET_PROPERTIES(${CORE-LIB} PROPERTIES VERSION ${VERSION})
@@ -263,6 +290,7 @@ SET(SMIME-LIB "email-smime")
 SET(SMIME-SRCS 
        ${CMAKE_SOURCE_DIR}/email-core/email-core-smime.c
        ${CMAKE_SOURCE_DIR}/email-core/email-core-cert.c
+       ${CMAKE_SOURCE_DIR}/email-core/email-core-pgp.c
 )
 
 INCLUDE_DIRECTORIES(
index 31149dc..8c4722f 100755 (executable)
 #include <string.h>
 #include <time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <vconf.h>
 #include <curl/curl.h>
+#include <sys/shm.h>
 
 #include "email-convert.h"
 #include "email-types.h"
 #include "email-core-signal.h"
 #include "email-core-imap-mailbox.h"
 
-#if defined(__FEATURE_USING_ACCOUNT_SVC_FOR_ACCOUNT_MANAGEMENT__) || defined(__FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__)
+#include "email-core-container.h"
+
+#include "imap4r1.h"
+
 #include "account.h"
 #include "account-types.h"
-#endif /*  __FEATURE_USING_ACCOUNT_SVC_FOR_ACCOUNT_MANAGEMENT__ */
 
 char *g_default_mbox_alias[MAILBOX_COUNT] =
 {
@@ -87,71 +91,119 @@ email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] =
        EMAIL_MAILBOX_TYPE_SPAMBOX,
 };
 
-static email_account_list_t *g_account_list = NULL;
-static int g_account_num = 0;
-static pthread_mutex_t _account_ref_lock = PTHREAD_MUTEX_INITIALIZER;
-
-INTERNAL_FUNC email_account_t* emcore_get_account_reference(int account_id)
+INTERNAL_FUNC email_account_t* emcore_get_account_reference(char *multi_user_name, int account_id)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
-       email_account_list_t **p;
-       email_account_t *result_account = NULL;
-
-       if (account_id < 0){
-               emcore_get_account_from_unvalidated_account_list(account_id, &result_account);
-               return result_account;
-       }
-       else if (account_id > 0)  {
-               ENTER_CRITICAL_SECTION(_account_ref_lock);
-               p = &g_account_list;
-               while (*p)  {
-                       if ((*p)->account->account_id == account_id) {
-                               emcore_duplicate_account((*p)->account, &result_account, NULL);
-                               break;
-                       }
-                       p = &(*p)->next;
-               }
-               LEAVE_CRITICAL_SECTION(_account_ref_lock);
+        int err = EMAIL_ERROR_NONE;
+        email_account_t *result_account = NULL;
+        emstorage_account_tbl_t *p_result_account = NULL;
+
+        if (account_id < 0){
+               emcore_get_account_from_unvalidated_account_list(account_id, &result_account);
+               return result_account;
+        } else if (account_id > 0) {
+                if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &p_result_account, false, &err)) {
+                    EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
+                    return NULL;
+                }
+
+                result_account = (email_account_t *)em_malloc(sizeof(email_account_t));
+                if (result_account == NULL) {
+                    EM_DEBUG_EXCEPTION("em_malloc failed");
+                    return NULL;
+                }
+
+                em_convert_account_tbl_to_account(p_result_account, result_account);
+
+                if (p_result_account)
+                    emstorage_free_account(&p_result_account, 1, NULL);
+        }
 
-               if (result_account)
-                       goto FINISH_OFF;
+       EM_DEBUG_FUNC_END("[%p]", result_account);
+       return result_account;
+}
 
-               /*  refresh and check once again */
-               emcore_init_account_reference();
-               ENTER_CRITICAL_SECTION(_account_ref_lock);
-               if (g_account_num > 0 && g_account_list) {
-                       p = &g_account_list;
-                       while (*p)  {
-                               if ((*p)->account->account_id == account_id) {
-                                       emcore_duplicate_account((*p)->account, &result_account, NULL);
-                                       break;
-                               }
-                               p = &(*p)->next;
-                       }
-               }
-               LEAVE_CRITICAL_SECTION(_account_ref_lock);
-       }
+INTERNAL_FUNC int emcore_get_account_reference_list(char *multi_user_name, email_account_t **output_account_list, int *output_count)
+{
+       EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", output_account_list, output_count, err_code);
+    int err = EMAIL_ERROR_NONE;
+    int i = 0;
+    int account_count = 0;
+    email_account_t *account_list = NULL;
+    emstorage_account_tbl_t *account_list_tbl = NULL;
+
+    if (!emstorage_get_account_list(multi_user_name, &account_count, &account_list_tbl, false, true, &err)) {
+        EM_DEBUG_EXCEPTION("emstorage_get_account_list failed : [%d]", err);
+        goto FINISH_OFF;
+    }
+
+    if (account_count > 0) {
+        account_list = em_malloc(sizeof(email_account_t) * (account_count));
+        if (account_list == NULL) {
+            EM_DEBUG_EXCEPTION("em_malloc failed");
+            err = EMAIL_ERROR_OUT_OF_MEMORY;
+            goto FINISH_OFF;
+        }
+    }
+
+    for (i = 0; i < account_count; i++) {
+        em_convert_account_tbl_to_account(&account_list_tbl[i], &account_list[i]);    
+    }
 
 FINISH_OFF:
-       EM_DEBUG_FUNC_END("[%p]", result_account);
-       return result_account;
+
+    if (account_list_tbl)
+        emstorage_free_account(&account_list_tbl, account_count, NULL);
+
+    if (output_account_list)
+        *output_account_list = account_list;
+
+    if (output_count)
+        *output_count = account_count;
+
+       EM_DEBUG_FUNC_END();
+       return err;
 }
 
+static char *emcore_get_imap_capability_string(MAILSTREAM *input_stream)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("input_stream[%p]", input_stream);
+       char *result_string = NULL;
+       IMAPCAP *imap_capability = NULL;
+       char capability_string[512] = { 0, };
+
+       if ((imap_capability = imap_cap(input_stream))) {
+               if (imap_capability->idle)
+                       EM_SAFE_STRCAT(capability_string, "IDLE ");
+               if (imap_capability->quota)
+                       EM_SAFE_STRCAT(capability_string, "QUOTA ");
+               if (imap_capability->starttls)
+                       EM_SAFE_STRCAT(capability_string, "STARTTLS ");
+#ifdef __FEATURE_XLIST_SUPPORT__
+               if (imap_capability->xlist)
+                       EM_SAFE_STRCAT(capability_string, "XLIST ");
+#endif /* __FEATURE_XLIST_SUPPORT__ */
+               result_string = EM_SAFE_STRDUP(capability_string);
+       }
+
+       EM_DEBUG_FUNC_END("[%s]", result_string);
+       return result_string;
+}
 
-INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, email_event_type_t event_type, int *err_code)
+INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_name, email_account_t *account, email_event_type_t event_type, char **output_server_capability_string, int event_handle, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("account[%p], err_code[%p], incoming_server_address [%s]", account, err_code, account->incoming_server_address);
+       EM_DEBUG_FUNC_BEGIN_SEC("account[%p] output_server_capability_string[%p] err_code[%p] incoming_server_address [%s]", account, output_server_capability_string, err_code, account->incoming_server_address);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
+       int server_capability_string_length = 0;
        email_session_t *session = NULL;
+       char *imap_capability_string = NULL;
+       char smtp_capability_string[128] = { 0, };
        SENDSTREAM *stream = NULL;
        MAILSTREAM *tmp_stream = NULL;
 
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        if (!emnetwork_check_network_status(&err)) {
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
@@ -159,92 +211,102 @@ INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *acc
        }
        EM_DEBUG_LOG("Network available");
 
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        if (!emcore_get_empty_session(&session)) {
                EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
-               err = EMAIL_ERROR_SESSION_NOT_FOUND;
-               goto FINISH_OFF;
+/*             err = EMAIL_ERROR_SESSION_NOT_FOUND;
+               goto FINISH_OFF; */
        }
 
        /* validate connection for pop3/imap */
        EM_DEBUG_LOG("Validate connection for POP3/IMAP4");
-       if (EMAIL_ERROR_NONE == err) {
-               if (!emcore_check_thread_status()) {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
 
-               if (!emcore_connect_to_remote_mailbox_with_account_info(account, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
-                       EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
-                       if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) {      /*  wrong password or etc */
-                               EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
-                       } else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
-                               /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
-                       }
-                       goto FINISH_OFF;
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
+               EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
+               if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) {      /*  wrong password or etc */
+                       EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
+               } else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
+                       /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
                }
+               goto FINISH_OFF;
        }
 
+       if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)
+               imap_capability_string = emcore_get_imap_capability_string(tmp_stream);
+
 #ifdef __FEATURE_SMTP_VALIDATION__
        /* validate connection for SMTP */
        EM_DEBUG_LOG("Validate connection for SMTP server");
 
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
-       if (!emcore_connect_to_remote_mailbox_with_account_info(account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
-               EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
-               if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) {
-                       /* Wrong password or etc */
-                       EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
-                       err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
-                       goto FINISH_OFF;
-               }
+       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
+               EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+               err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
+               goto FINISH_OFF;
 
+#if 0
                if (account->outgoing_server_secure_connection == 0x01) /*  0x01 == SSL */ {
-                       if (!emcore_check_thread_status()) {
-                               err = EMAIL_ERROR_CANCELLED;
-                               goto FINISH_OFF;
-                       }
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                        EM_DEBUG_LOG("Retry with TLS");
                        account->outgoing_server_secure_connection = 0x02;      /*  0x02 == TLS */
 
-                       if (!emcore_connect_to_remote_mailbox_with_account_info(account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
+                       if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, account, EMAIL_CONNECT_FOR_SENDING, (void **)&stream, &err) || !stream) {
                                EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
                                err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
                                account->outgoing_server_secure_connection = 0x01;      /*  restore to the previous value */
                                goto FINISH_OFF;
                        }
 
-                       if (!emcore_check_thread_status()) {
-                               err = EMAIL_ERROR_CANCELLED;
-                               goto FINISH_OFF;
-                       }
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
                }
                else {
-                       if (EMAIL_ERROR_CONNECTION_FAILURE != err)
-                               err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
+                       err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
                        goto FINISH_OFF;
                }
+#endif
+       }
+       if (stream && stream->protocol.esmtp.ok) {
+               if (stream->protocol.esmtp.size.ok && stream->protocol.esmtp.size.limit > 0) {
+                       account->outgoing_server_size_limit = stream->protocol.esmtp.size.limit;
+                       SNPRINTF(smtp_capability_string, 128, "SMTP_MAIL_SIZE_LIMIT=%d ", account->outgoing_server_size_limit);
+                       EM_DEBUG_LOG("%s", smtp_capability_string);
+               }
        }
 #endif /* __FEATURE_SMTP_VALIDATION__ */
 
-       if (!emcore_check_thread_status()) {
+       int dummy = 0;
+       if (!emcore_check_event_thread_status(&dummy, event_handle)) {
+               EM_DEBUG_LOG ("canceled event: [%d]", dummy);
                if (event_type == EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT || event_type == EMAIL_EVENT_VALIDATE_ACCOUNT_EX) {
-                       if (!emcore_delete_account(account->account_id, NULL))
+                       if (!emcore_delete_account(multi_user_name, account->account_id, false, NULL))
                                EM_DEBUG_EXCEPTION("emdaemon_delete_account failed [%d]", account->account_id);
                }
                err = EMAIL_ERROR_CANCELLED;
                goto FINISH_OFF;
        }
 
+       if (output_server_capability_string) {
+               server_capability_string_length = EM_SAFE_STRLEN(imap_capability_string) + EM_SAFE_STRLEN(smtp_capability_string);
+               if (server_capability_string_length) {
+                       *output_server_capability_string = em_malloc(server_capability_string_length + 1);
+
+                       if      (*output_server_capability_string == NULL) {
+                               EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
+                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_STRCAT(*output_server_capability_string, smtp_capability_string);
+                       EM_SAFE_STRCAT(*output_server_capability_string, imap_capability_string);
+                       EM_DEBUG_LOG("%s", *output_server_capability_string);
+               }
+       }
+
        ret = true;
 
 FINISH_OFF:
@@ -257,6 +319,8 @@ FINISH_OFF:
        if (err_code != NULL)
                *err_code = err;
 
+       EM_SAFE_FREE(imap_capability_string);
+
        emcore_clear_session(session);
 
        EM_DEBUG_FUNC_END();
@@ -264,7 +328,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
+INTERNAL_FUNC int emcore_validate_account(char *multi_user_name, int account_id, int handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
 
@@ -278,9 +342,9 @@ INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
-       if (ref_account && emcore_validate_account_with_account_info(ref_account, EMAIL_EVENT_VALIDATE_ACCOUNT, &err) == false) {
+       if (ref_account && emcore_validate_account_with_account_info(multi_user_name, ref_account, EMAIL_EVENT_VALIDATE_ACCOUNT, NULL, handle, &err) == false) {
                EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed (%d)", err);
                goto FINISH_OFF;
        }
@@ -302,10 +366,31 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
+key_t del_account_key = 4511; /* name of the segment d/4 e/5 l/11 */
+
+INTERNAL_FUNC int emcore_delete_account(char *multi_user_name, int account_id, int input_delete_from_account_svc, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
 
+       int shmid = 0;
+       int *del_account_id = NULL;
+
+       EM_DEBUG_LOG ("begin account_id [%d]", account_id);
+       /* worker thread is single instance, so multiple accounts cant be deleted concurrently */
+       if ((shmid = shmget (del_account_key, sizeof (int), IPC_CREAT | 0666)) != -1) {
+               /* attaching the segment to the current process space */
+               if ((del_account_id = (int*) shmat (shmid, NULL, 0)) != (int*) -1) {
+                       /* write it */
+                       *del_account_id = account_id;
+                       EM_DEBUG_LOG ("recorded account_id [%d]", *del_account_id);
+               }
+               else
+                       EM_DEBUG_EXCEPTION ("shmget error[%d]", errno);
+
+       }
+       else
+               EM_DEBUG_EXCEPTION ("shmget error[%d]", errno);
+
        /*  default variabl */
        int ret = false;
        int err = EMAIL_ERROR_NONE;
@@ -314,20 +399,20 @@ INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
        if (account_id < FIRST_ACCOUNT_ID)  {
                EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
                err = EMAIL_ERROR_INVALID_PARAM;
+               before_tr_begin = 1;
                goto FINISH_OFF;
        }
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        /* Delete all local activities of previous account */
-       
-
        emstorage_activity_tbl_t activity;
        memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
        activity.account_id = account_id;
+    activity.multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_delete_activity(&activity, &err)) {
                EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
-
+               before_tr_begin = 1;
                goto FINISH_OFF;
        }
 #endif
@@ -336,25 +421,43 @@ INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
        if (false == emcore_clear_partial_body_thd_event_que(&err))
                EM_DEBUG_EXCEPTION(" emcore_clear_partial_body_thd_event_que [%d]", err);
 
-       if (false == emstorage_delete_full_pbd_activity_data(account_id, true, &err))
+       if (false == emstorage_delete_full_pbd_activity_data(multi_user_name, account_id, true, &err))
                EM_DEBUG_EXCEPTION("emstorage_delete_full_pbd_activity_data failed [%d]", err);
 
 #endif
 
-#ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_ACCOUNT_MANAGEMENT__
-       {
-               int error_code;
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       if (!emcore_clear_auto_download_queue())
+               EM_DEBUG_EXCEPTION("emcore_clear_auto_download_queue failed");
+
+       if (!emstorage_delete_all_auto_download_activity(multi_user_name, account_id, true, &err))
+               EM_DEBUG_EXCEPTION("emstorage_delete_all_auto_download_activity failed [%d]", err);
+#endif
+
+       if (input_delete_from_account_svc == true) {
+               int error_code_from_account_svc = 0;
                email_account_t *account_to_be_deleted;
+               vsm_zone_h join_zone = NULL;
 
-               account_to_be_deleted = emcore_get_account_reference(account_id);
+               account_to_be_deleted = emcore_get_account_reference(multi_user_name, account_id);
                if (account_to_be_deleted && account_to_be_deleted->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        EM_DEBUG_LOG("Calling account_svc_delete with account_svc_id[%d]", account_to_be_deleted->account_svc_id);
-                       error_code = account_connect();
-                       EM_DEBUG_LOG("account_connect returns [%d]", error_code);
-                       error_code = 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);
-                       error_code = account_disconnect();
-                       EM_DEBUG_LOG("account_disconnect returns [%d]", error_code);
+                       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);
+                                       goto FINISH_OFF;
+                               }
+                       }
+                       
+                       error_code_from_account_svc = account_connect();
+
+                       EM_DEBUG_LOG("account_connect returns [%d]", error_code_from_account_svc);
+                       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);
+                       error_code_from_account_svc = account_disconnect();
+                       EM_DEBUG_LOG("account_disconnect returns [%d]", error_code_from_account_svc);
+
+                       emcore_unset_join_zone(join_zone);
                }
 
                if (account_to_be_deleted) {
@@ -362,26 +465,27 @@ INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
                        EM_SAFE_FREE(account_to_be_deleted);
                }
        }
-#endif
-       if (emcore_cancel_all_threads_of_an_account(account_id) < EMAIL_ERROR_NONE) {
+
+       if (emcore_cancel_all_threads_of_an_account(multi_user_name, account_id) < EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("There are some remaining jobs. I couldn't stop them.");
                err = EMAIL_ERROR_CANNOT_STOP_THREAD;
+               before_tr_begin = 1;
                goto FINISH_OFF;
        }
 
        /* Delete contact log */
-       if ( ((err = emcore_delete_contacts_log(account_id)) != EMAIL_ERROR_NONE) && (err != EMAIL_ERROR_DATA_NOT_FOUND) ) {
-                       EM_DEBUG_EXCEPTION("emcore_delete_contacts_log failed : [%d]", err);
+       if (((err = emcore_delete_contacts_log(multi_user_name, account_id)) != EMAIL_ERROR_NONE) && (err != EMAIL_ERROR_DATA_NOT_FOUND)) {
+        EM_DEBUG_EXCEPTION("emcore_delete_contacts_log failed : [%d]", err);
        }
 
        /*  BEGIN TRANSACTION;           */
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_delete_account(account_id, false, &err))  {
+
+       if (!emstorage_delete_account(multi_user_name, account_id, false, &err)) {
                if(err != EMAIL_ERROR_SYSTEM_FAILURE) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
                        goto FINISH_OFF;
@@ -393,13 +497,13 @@ INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
        emcore_remove_connection_info(account_id);
 #endif
        
-       if ((err = emcore_delete_all_mails_of_acount(account_id)) != EMAIL_ERROR_NONE)  {
+       if ((err = emcore_delete_all_mails_of_acount(multi_user_name, account_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_acount failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /*  delete all mailboxes */
-       if (!emstorage_delete_mailbox(account_id, -1, 0, false, &err))  {
+       if (!emstorage_delete_mailbox(multi_user_name, account_id, -1, 0, false, &err)) {
                if(err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_mailbox failed - %d", err);
                        goto FINISH_OFF;
@@ -407,20 +511,19 @@ INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
        }
 
        /*  delete local imap sync mailbox from imap mailbox table */
-       if (!emstorage_remove_downloaded_mail(account_id, NULL, NULL, false, &err))  {
+       if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, NULL, NULL, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
                goto FINISH_OFF;
        }
 
-       emcore_display_unread_in_badge();
-       emcore_delete_notification_by_account(account_id, true);
-       emcore_init_account_reference();
+       emcore_display_unread_in_badge(NULL);
+       emcore_delete_notification_by_account(multi_user_name, account_id, true);
 
        ret = true;
 
 FINISH_OFF:
        if (ret == true) {      /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                        ret = false;
                }
@@ -429,7 +532,7 @@ FINISH_OFF:
 
        }
        else {  /*  ROLLBACK TRANSACTION; */
-               if (!before_tr_begin && emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
                if (!emcore_notify_storage_event (NOTI_ACCOUNT_DELETE_FAIL, account_id, err, NULL, 0))
                        EM_DEBUG_EXCEPTION (" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
@@ -438,14 +541,19 @@ FINISH_OFF:
        if (err_code)
                *err_code = err;
 
+       /* del the segment */
+       if (shmid != -1) {
+               shmctl (shmid, IPC_RMID, NULL);
+       }
+
        EM_DEBUG_FUNC_END();
 
        return ret;
 }
 
-INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
+INTERNAL_FUNC int emcore_create_account(char *multi_user_name, email_account_t *account, int add_account_to_account_svc, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p]", account, err_code);
+       EM_DEBUG_FUNC_BEGIN("account[%p] add_account_to_account_svc [%d] err_code[%p]", account, add_account_to_account_svc, err_code);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
@@ -461,7 +569,7 @@ INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_count(&count, true, &err))  {
+       if (!emstorage_get_account_count(multi_user_name, &count, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
                goto FINISH_OFF;
        }
@@ -485,100 +593,117 @@ INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
        /* check for email address validation */
        EM_DEBUG_LOG_SEC("account->user_email_address[%s]", account->user_email_address);
        if (account->user_email_address) {
-               if ((err = em_verify_email_address (account->user_email_address, true)) != EMAIL_ERROR_NONE) {
+               if ((err = em_verify_email_address (account->user_email_address)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_verify_email_address error [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
-#ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_ACCOUNT_MANAGEMENT__
-       if (account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
-               int account_svc_id = 0;
-               int error_code;
-               account_h account_handle = NULL;
+       if (EM_SAFE_STRLEN(account->options.alert_ringtone_path) == 0) {
+               account->options.alert_ringtone_path = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR));
+       }
 
-               error_code = account_connect();
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("account_connect failed [%d]", error_code);
-                       err = error_code;
-                       goto FINISH_OFF;
-               }
+       if (add_account_to_account_svc) {
+               if (account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
+                       int account_svc_id = 0;
+                       int error_code = 0;
+                       account_h account_handle = NULL;
+                       vsm_zone_h join_zone = NULL;
 
-               error_code = account_create(&account_handle);
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
-                       err = error_code;
-                       account_disconnect();
-                       goto FINISH_OFF;
-               }
+                       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+                               err = emcore_set_join_zone(multi_user_name, &join_zone);
+                               if (err != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+                       }
 
-               error_code = account_set_user_name(account_handle, account->incoming_server_user_name);
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_user_name failed [%d]", error_code);
-               }
+                       error_code = account_connect();
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("account_connect_to_container failed [%d]", error_code);
+                               err = error_code;
+                               emcore_unset_join_zone(join_zone);
+                               goto FINISH_OFF;
+                       }
 
-               error_code = account_set_domain_name(account_handle, account->account_name);
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_domain_name failed [%d]", error_code);
-               }
+                       error_code = account_create(&account_handle);
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
+                               err = error_code;
+                               account_disconnect();
+                               emcore_unset_join_zone(join_zone);
+                               goto FINISH_OFF;
+                       }
 
-               error_code = account_set_email_address(account_handle,  account->user_email_address);
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_email_address failed [%d]", error_code);
-               }
+                       error_code = account_set_user_name(account_handle, account->incoming_server_user_name);
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_user_name failed [%d]", error_code);
+                       }
 
-               error_code = account_set_source(account_handle, "SLP EMAIL");
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_source failed [%d]", error_code);
-               }
+                       error_code = account_set_domain_name(account_handle, account->account_name);
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_domain_name failed [%d]", error_code);
+                       }
 
-               error_code = account_set_package_name(account_handle, "email-setting-efl");
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_package_name failed [%d]", error_code);
-               }
+                       error_code = account_set_email_address(account_handle,  account->user_email_address);
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_email_address failed [%d]", error_code);
+                       }
 
-               /* account_set_capability(account_handle , ACCOUNT_CAPABILITY_EMAIL, ACCOUNT_CAPABILITY_ENABLED); OLD API */
-               error_code = account_set_capability(account_handle , ACCOUNT_SUPPORTS_CAPABILITY_EMAIL , ACCOUNT_CAPABILITY_ENABLED);
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_capability failed [%d]", error_code);
-               }
+                       error_code = account_set_source(account_handle, "SLP EMAIL");
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_source failed [%d]", error_code);
+                       }
 
-               error_code = account_set_sync_support(account_handle, ACCOUNT_SYNC_STATUS_IDLE); /* This means "The account is supporting 'sync' and initialized as idle status" */
-               if(error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_LOG("account_set_sync_support failed [%d]", error_code);
-               }
+                       error_code = account_set_package_name(account_handle, "email-setting-efl");
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_package_name failed [%d]", error_code);
+                       }
 
-               if (account->logo_icon_path) {
-                       error_code = account_set_icon_path(account_handle, account->logo_icon_path);
+                       /* account_set_capability(account_handle , ACCOUNT_CAPABILITY_EMAIL, ACCOUNT_CAPABILITY_ENABLED); OLD API */
+                       error_code = account_set_capability(account_handle , ACCOUNT_SUPPORTS_CAPABILITY_EMAIL , ACCOUNT_CAPABILITY_ENABLED);
                        if(error_code != ACCOUNT_ERROR_NONE) {
-                               EM_DEBUG_LOG("account_set_icon_path failed [%d]", error_code);
+                               EM_DEBUG_LOG("account_set_capability failed [%d]", error_code);
                        }
-               }
 
-               error_code = account_insert_to_db(account_handle, &account_svc_id); 
-               if (error_code != ACCOUNT_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("account_insert_to_db failed [%d]", error_code);
-               }
-               else {
-                       account->account_svc_id = account_svc_id;
-                       EM_DEBUG_LOG("account_insert_to_db succeed");
-               }
+                       error_code = account_set_sync_support(account_handle, ACCOUNT_SYNC_STATUS_IDLE); /* This means "The account is supporting 'sync' and initialized as idle status" */
+                       if(error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_LOG("account_set_sync_support failed [%d]", error_code);
+                       }
 
-               if (account_handle)
-                       account_destroy(account_handle);
+                       if (account->logo_icon_path) {
+                               error_code = account_set_icon_path(account_handle, account->logo_icon_path);
+                               if(error_code != ACCOUNT_ERROR_NONE) {
+                                       EM_DEBUG_LOG("account_set_icon_path failed [%d]", error_code);
+                               }
+                       }
 
-               account_disconnect();
+                       error_code = account_insert_to_db(account_handle, &account_svc_id);
+                       if (error_code != ACCOUNT_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("account_insert_to_db failed [%d]", error_code);
+                       }
+                       else {
+                               account->account_svc_id = account_svc_id;
+                               EM_DEBUG_LOG("account_insert_to_db succeed");
+                       }
+
+                       if (account_handle)
+                               account_destroy(account_handle);
+
+                       account_disconnect();
+                       emcore_unset_join_zone(join_zone);
+               }
        }
-#endif  /*  __FEATURE_USING_ACCOUNT_SVC_FOR_ACCOUNT_MANAGEMENT__ */
 
        temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
        if (!temp_account_tbl) {
                EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
                goto FINISH_OFF;
        }
+
        em_convert_account_to_account_tbl(account, temp_account_tbl);
 
-       if (!emstorage_add_account(temp_account_tbl, true, &err))  {
+       if (!emstorage_add_account(multi_user_name, temp_account_tbl, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
                goto FINISH_OFF;
        }
@@ -592,7 +717,7 @@ INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
                        local_mailbox.account_id      = temp_account_tbl->account_id;
                        local_mailbox.mailbox_name    = EM_SAFE_STRDUP(g_default_mbox_name[i]);
                        local_mailbox.alias           = EM_SAFE_STRDUP(g_default_mbox_alias[i]);
-                       local_mailbox.mailbox_type        = g_default_mbox_type[i];
+                       local_mailbox.mailbox_type    = g_default_mbox_type[i];
                        local_mailbox.mail_slot_size  = temp_account_tbl->default_mail_slot_size;
                        local_mailbox.eas_data        = NULL;
                        local_mailbox.eas_data_length = 0;
@@ -602,17 +727,17 @@ INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
                        else
                                local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
 
-                       if (!emcore_create_mailbox(&local_mailbox, 0, &err))  {
+                       if (!emcore_create_mailbox(multi_user_name, &local_mailbox, 0, account->incoming_server_type, account->default_mail_slot_size, &err)) {
                                EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
                                emcore_free_mailbox(&local_mailbox);
                                goto FINISH_OFF;
                        }
-                       
+
                        emcore_free_mailbox(&local_mailbox);
                }
        }
 
-       /* Initialize the noti private id */    
+       /* Initialize the noti private id */
        SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account->account_id);
        if (vconf_get_int(vconf_private_id, &private_id) != 0) {
                EM_DEBUG_EXCEPTION("vconf_get_int failed");
@@ -621,18 +746,18 @@ INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
                        EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
                }
        }
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (temp_account_tbl)
                emstorage_free_account(&temp_account_tbl, 1, NULL);
-       
+
        if (ret == false && account != NULL)  {
-               if (!emcore_delete_account(account->account_id, NULL))
+               if (!emcore_delete_account(multi_user_name, account->account_id, false, NULL))
                        EM_DEBUG_EXCEPTION("emdaemon_delete_account Failed [%d]", account->account_id);
        }
-       
+
        if (err_code)
                *err_code = err;
 
@@ -640,112 +765,29 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emcore_init_account_reference()
-{
-       EM_DEBUG_FUNC_BEGIN();
-       
-       int err = EMAIL_ERROR_NONE;
-       
-       email_account_list_t *account_list = NULL;
-       email_account_list_t **p = NULL;
-       email_account_t *account = NULL;
-       emstorage_account_tbl_t *account_tbl_array = NULL;
-       int count = 0;          
-       int i = 0;
-       
-       /* free account reference if any */
-       emcore_free_account_reference();
-
-       if (!emstorage_get_account_list(&count, &account_tbl_array, true, true, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       for (p = &account_list, i = 0; i < count; i++) {
-               account = em_malloc(sizeof(email_account_t));
-               if (!account) {
-                       EM_DEBUG_EXCEPTION("malloc failed...");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-               
-               em_convert_account_tbl_to_account(account_tbl_array + i, account);
-
-               *p = (email_account_list_t*) em_malloc(sizeof(email_account_list_t));
-               if (!(*p)) {
-                       EM_DEBUG_EXCEPTION("malloc failed...");
-                       emcore_free_account(account);
-                       EM_SAFE_FREE(account);
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-
-               (*p)->account = account;
-
-               p = &(*p)->next;
-       }
-
-       ENTER_CRITICAL_SECTION(_account_ref_lock);
-       g_account_num = count;
-       g_account_list = account_list;
-       LEAVE_CRITICAL_SECTION(_account_ref_lock);
-
-FINISH_OFF: 
-       if (account_tbl_array)
-               emstorage_free_account(&account_tbl_array, count, NULL);
-
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
-}
-
-INTERNAL_FUNC int emcore_free_account_reference()
-{
-       EM_DEBUG_FUNC_BEGIN();
-       ENTER_CRITICAL_SECTION(_account_ref_lock);
-
-       email_account_list_t *p = g_account_list;
-       email_account_list_t *p_next = NULL;
-       while (p) {
-               emcore_free_account(p->account);
-               EM_SAFE_FREE(p->account);
-               
-               p_next = p->next;
-               EM_SAFE_FREE(p);
-               p = p_next;
-       }
-
-       g_account_num = 0;
-       g_account_list = NULL;
-
-       LEAVE_CRITICAL_SECTION(_account_ref_lock);
-       EM_DEBUG_FUNC_END();
-       return true;
-}
-
 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        if (count <= 0 || !account_list || !*account_list) {
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-               
+
        email_account_t *p = *account_list;
        int i;
        for (i = 0; i < count; i++)
                emcore_free_account(p+i);
-               
+
        EM_SAFE_FREE(p);
        *account_list = NULL;
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (err_code)
                *err_code = err;
        EM_DEBUG_FUNC_END();
@@ -764,22 +806,21 @@ INTERNAL_FUNC void emcore_free_account(email_account_t *account)
        if(!account) return;
 
        EM_SAFE_FREE(account->account_name);
-       EM_SAFE_FREE(account->incoming_server_address);
+       EM_SAFE_FREE(account->logo_icon_path);
+       EM_SAFE_FREE(account->user_data);
+       account->user_data_length = 0;
+       EM_SAFE_FREE(account->user_display_name);
        EM_SAFE_FREE(account->user_email_address);
+       EM_SAFE_FREE(account->reply_to_address);
+       EM_SAFE_FREE(account->return_address);
+       EM_SAFE_FREE(account->incoming_server_address);
        EM_SAFE_FREE(account->incoming_server_user_name);
        EM_SAFE_FREE(account->incoming_server_password);
        EM_SAFE_FREE(account->outgoing_server_address);
        EM_SAFE_FREE(account->outgoing_server_user_name);
        EM_SAFE_FREE(account->outgoing_server_password);
-       EM_SAFE_FREE(account->user_display_name);
-       EM_SAFE_FREE(account->reply_to_address);
-       EM_SAFE_FREE(account->return_address);
-       EM_SAFE_FREE(account->logo_icon_path);
-       EM_SAFE_FREE(account->certificate_path);
-       EM_SAFE_FREE(account->user_data);
-       account->user_data_length = 0;
        emcore_free_option(&account->options);
-
+       EM_SAFE_FREE(account->certificate_path);
 
        EM_DEBUG_FUNC_END();
 }
@@ -815,7 +856,7 @@ INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, emai
 
        if(!account || !account_dup) { /*prevent 40514*/
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               if(err_code) *err_code = EMAIL_ERROR_INVALID_PARAM; 
+               if(err_code) *err_code = EMAIL_ERROR_INVALID_PARAM;
                return;
        }
 
@@ -834,66 +875,6 @@ INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, emai
        EM_DEBUG_FUNC_END();
 }
 
-INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code)
-{
-       EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
-       int i, countOfAccounts = 0;
-       int ret = false;
-       int err = EMAIL_ERROR_NONE;
-       email_account_t *accountRef;
-       email_account_list_t *p;
-
-       ENTER_CRITICAL_SECTION(_account_ref_lock);
-
-       if (!account_list || !count)  {
-               EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
-               err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       p = g_account_list;
-
-       while (p) {
-               countOfAccounts++;
-               p = p->next;
-       }
-
-       EM_DEBUG_LOG("Result count[%d]", countOfAccounts);
-       *count = countOfAccounts;
-
-       if (countOfAccounts > 0) {
-               *account_list = malloc(sizeof(email_account_t) * countOfAccounts);
-               if (!*account_list)  {
-                       EM_DEBUG_LOG("malloc failed...");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-       }
-
-       p = g_account_list;
-       for (i = 0; i < countOfAccounts; i++)  {
-               accountRef = (*account_list) + i;
-               emcore_duplicate_account_members(p->account, accountRef);
-               EM_DEBUG_LOG_SEC("Result account id[%d], name[%s]", accountRef->account_id, accountRef->account_name);
-               p = p->next;
-       }
-
-       ret = true;
-
-FINISH_OFF:
-       if (ret == false) {
-               if (account_list && *account_list)
-                       EM_SAFE_FREE(*account_list);
-       }
-
-       LEAVE_CRITICAL_SECTION(_account_ref_lock);
-
-       if (err_code != NULL)
-               *err_code = err;
-       EM_DEBUG_FUNC_END();
-       return ret;
-}
-
 #ifdef __FEATURE_BACKUP_ACCOUNT__
 #include <ss_manager.h>
 
@@ -928,7 +909,7 @@ static int append_data_into_buffer(char **target_buffer, int *target_buffer_lent
 
        ret_code = true;
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (error_code)
                *error_code = local_error_code;
@@ -948,7 +929,7 @@ static int emcore_write_account_into_buffer(char **target_buffer, int *target_bu
        if (em_convert_account_tbl_to_account(account_tbl_ptr, &temp_account)) {
                byte_stream = em_convert_account_to_byte_stream(&temp_account, &stream_length);
                EM_DEBUG_LOG("stream_length [%d]", stream_length);
-               /*  EM_DEBUG_LOG("incoming_server_password [%s]", temp_account->password) */
+               /*  EM_DEBUG_LOG_SEC("incoming_server_password [%s]", temp_account->password) */
 
                if (byte_stream) {
                        if (!append_data_into_buffer(target_buffer, target_buffer_lenth, (char *)&stream_length, sizeof(int), &local_error_code)) {
@@ -971,7 +952,7 @@ static int emcore_write_account_into_buffer(char **target_buffer, int *target_bu
        }
 
        ret_code = true;
-FINISH_OFF: 
+FINISH_OFF:
        emcore_free_account(&temp_account);
        if (error_code)
                *error_code = local_error_code;
@@ -982,30 +963,46 @@ FINISH_OFF:
        return ret_code;
 }
 
-INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
+INTERNAL_FUNC int emcore_backup_accounts(char *multi_user_name, const char *file_path, int *error_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("file_path [%s], error_code [%p]", file_path, error_code);
        int local_error_code = EMAIL_ERROR_NONE, local_error_code_2 = EMAIL_ERROR_NONE, ret_code = false;
        int select_num, i, target_buff_length = 0;
+       int normal_email_account_count = 0;
        char *target_buffer = NULL;
        emstorage_account_tbl_t *account_list = NULL;
+       int fd = 0;
 
        if (!file_path) {
                local_error_code = EMAIL_ERROR_INVALID_PARAM;
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               goto FINISH_OFF;        
+               goto FINISH_OFF;
        }
 
        select_num = 1000;
-       
-       if (!emstorage_get_account_list(&select_num, &account_list, true, true, &local_error_code)) {
+
+       if (!emstorage_get_account_list(multi_user_name, &select_num, &account_list, true, true, &local_error_code)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", local_error_code);
-               goto FINISH_OFF;        
+               goto FINISH_OFF;
        }
-       
+
        EM_DEBUG_LOG("select_num [%d]", select_num);
-       
+
        if (account_list) {
+               for (i = 0; i < select_num; i++) {
+                       if (account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_POP3 || account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)
+                               normal_email_account_count++;
+               }
+       }
+
+       EM_DEBUG_LOG("normal_email_account_count [%d]", normal_email_account_count);
+
+       if (normal_email_account_count == 0) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_ACCOUNT_NOT_FOUND");
+               local_error_code = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
+               goto FINISH_OFF;
+       }
+       else {
                target_buffer = em_malloc(sizeof(int));
                if (!target_buffer)  {
                        EM_DEBUG_EXCEPTION("malloc failed");
@@ -1013,13 +1010,15 @@ INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
                        goto FINISH_OFF;
                }
 
-               memcpy(target_buffer, (char *)&select_num, sizeof(int));
+               memcpy(target_buffer, (char *)&normal_email_account_count, sizeof(int));
                target_buff_length = sizeof(int);
 
                for (i = 0; i < select_num; i++) {
-                       if (!emcore_write_account_into_buffer(&target_buffer, &target_buff_length, account_list + i, &local_error_code)) {
-                               EM_DEBUG_EXCEPTION("emcore_write_account_into_buffer failed [%d]", local_error_code);
-                               goto FINISH_OFF;        
+                       if (account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_POP3 || account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
+                               if (!emcore_write_account_into_buffer(&target_buffer, &target_buff_length, account_list + i, &local_error_code)) {
+                                       EM_DEBUG_EXCEPTION("emcore_write_account_into_buffer failed [%d]", local_error_code);
+                                       goto FINISH_OFF;
+                               }
                        }
                }
 
@@ -1030,15 +1029,16 @@ INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
                if (ssm_write_buffer(target_buffer, target_buff_length, file_path, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
                        EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d]", local_error_code);
                        local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;        
+                       goto FINISH_OFF;
                }
-               
        }
 
        ret_code = true;        
 FINISH_OFF: 
 
+       EM_SAFE_CLOSE (fd);
        EM_SAFE_FREE(target_buffer);
+
        if (account_list)
                emstorage_free_account(&account_list, select_num, &local_error_code_2);
 
@@ -1049,29 +1049,33 @@ FINISH_OFF:
        return ret_code;
 }
 
-INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code)
+INTERNAL_FUNC int emcore_restore_accounts(char *multi_user_name, const char *file_path)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("file_path [%s], error_code [%p]", file_path, error_code);
-       int local_error_code = EMAIL_ERROR_NONE, ret_code = false, buffer_length = 0, read_length = 0;
+       EM_DEBUG_FUNC_BEGIN_SEC("file_path [%s]", file_path);
+       int err = EMAIL_ERROR_NONE, buffer_length = 0, read_length = 0;
        int account_count = 0, i = 0, account_stream_length = 0;
        char *temp_buffer = NULL, *account_stream = NULL, *buffer_ptr = NULL;
        email_account_t temp_account = {0};
        email_account_t *account_list = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+       char *encrypt_buffer = NULL;
 
        ssm_file_info_t sfi;
 
+       if (!emcore_notify_storage_event (NOTI_ACCOUNT_RESTORE_START, 0, 0, NULL, 0))
+               EM_DEBUG_EXCEPTION ("emcore_notify_storage_event [NOTI_ACCOUNT_RESTORE_START] failed");
+
        if (!file_path) {
-               local_error_code = EMAIL_ERROR_INVALID_PARAM;
+               err = EMAIL_ERROR_INVALID_PARAM;
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               goto FINISH_OFF;        
+               goto FINISH_OFF;
        }
 
-       if (emcore_get_account_reference_list(&account_list, &account_count, &ret_code)) {
+       if ((err = emcore_get_account_reference_list(multi_user_name, &account_list, &account_count)) == EMAIL_ERROR_NONE) {
                for (i = 0; i < account_count; i++) {
                        if (account_list[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
-                               if (!emcore_delete_account(account_list[i].account_id, &ret_code)) {
-                                       local_error_code = EMAIL_ERROR_INVALID_ACCOUNT;
-                                       EM_DEBUG_EXCEPTION("emcore_delete_account failed");
+                               if (!emcore_delete_account(multi_user_name, account_list[i].account_id, true, &err)) {
+                                       EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
                                        goto FINISH_OFF;
                                }
                        }
@@ -1085,21 +1089,21 @@ INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code
 
        if (ssm_getinfo(file_path, &sfi, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
                EM_DEBUG_EXCEPTION("ssm_getinfo() failed.");
-               ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
        buffer_length = sfi.originSize;
        EM_DEBUG_LOG("account buffer_length[%d]", buffer_length);
-       if ((temp_buffer = (char *)em_malloc(buffer_length + 1)) == NULL) {
+       if ((encrypt_buffer = (char *)em_malloc(buffer_length + 1)) == NULL) {
                EM_DEBUG_EXCEPTION("em_malloc failed...");
-               ret_code = EMAIL_ERROR_OUT_OF_MEMORY;
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
        if (ssm_read(file_path, temp_buffer, buffer_length, (size_t *)&read_length, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
                EM_DEBUG_EXCEPTION("ssm_read() failed.");
-               ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
@@ -1109,7 +1113,7 @@ INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code
                memcpy((void *)&account_count, temp_buffer, sizeof(int));
                buffer_ptr = temp_buffer + sizeof(int);
 
-               EM_DEBUG_LOG("account_count[%d]", account_count);               
+               EM_DEBUG_LOG("account_count[%d]", account_count);
 
                for (i = 0; i < account_count; i++) {
                        memcpy((void *)&account_stream_length, buffer_ptr, sizeof(int));
@@ -1119,16 +1123,25 @@ INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code
                                account_stream = em_malloc(account_stream_length);
                                if (!account_stream) {
                                        EM_DEBUG_EXCEPTION("em_malloc() failed.");
-                                       ret_code = EMAIL_ERROR_OUT_OF_MEMORY ;
+                                       err = EMAIL_ERROR_OUT_OF_MEMORY ;
                                        goto FINISH_OFF;
                                }
                                memcpy(account_stream, buffer_ptr, account_stream_length);
 
                                em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
                                EM_SAFE_FREE(account_stream);
-                       
-                               if (!emcore_create_account(&temp_account, &ret_code)) {
-                                       EM_DEBUG_EXCEPTION("emcore_create_account() failed.");
+
+                               if ((emstorage_check_duplicated_account(multi_user_name, &temp_account, true, &err) == false) && (err = EMAIL_ERROR_ALREADY_EXISTS)) {
+                                       EM_DEBUG_EXCEPTION("An account is duplicated. ");
+                                       buffer_ptr += account_stream_length;
+                                       account_stream_length = 0;
+                                       emcore_free_account(&temp_account);
+                                       err = EMAIL_ERROR_NONE;
+                                       continue;
+                               }
+
+                               if (!emcore_create_account(multi_user_name, &temp_account, true, &err)) {
+                                       EM_DEBUG_EXCEPTION("emcore_create_account() failed. [%d]", err);
                                        goto FINISH_OFF;
                                }
 
@@ -1139,11 +1152,24 @@ INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code
                }
        } else {
                EM_DEBUG_EXCEPTION("ssm_read() failed.");
-               ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
-       ret_code = true;        
-FINISH_OFF: 
+
+FINISH_OFF:
+#ifdef FEATURE_SSS_ENABLE
+       EM_SAFE_CLOSE (fd);
+#endif
+
+       if (err == EMAIL_ERROR_NONE) {
+               if (!emcore_notify_storage_event (NOTI_ACCOUNT_RESTORE_FINISH, 0, 0, NULL, err))
+                       EM_DEBUG_EXCEPTION ("emcore_notify_storage_event [NOTI_ACCOUNT_RESTORE_FINISH] failed");
+       }
+       else {
+               if (!emcore_notify_storage_event (NOTI_ACCOUNT_RESTORE_FAIL, 0, 0, NULL, err))
+                       EM_DEBUG_EXCEPTION ("emcore_notify_storage_event [NOTI_ACCOUNT_RESTORE_FAIL] failed");
+       }
+
        if(account_list) {
                emcore_free_account_list(&account_list, account_count, NULL);
                account_list = NULL;
@@ -1151,45 +1177,15 @@ FINISH_OFF:
        emcore_free_account(&temp_account);
        EM_SAFE_FREE(account_stream);
        EM_SAFE_FREE(temp_buffer);
+       EM_SAFE_FREE(encrypt_buffer);
 
-       EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
-       return ret_code;
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
 #endif /*  __FEATURE_BACKUP_ACCOUNT_ */
 
-
-
-INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
-{
-       EM_DEBUG_FUNC_BEGIN_SEC("domain_name [%s], result_server_info [%p]", domain_name, result_server_info);
-       int ret_code = EMAIL_ERROR_NONE;
-
-       EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
-       return ret_code;
-}
-
-
-INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info)
-{
-       EM_DEBUG_FUNC_BEGIN("result_server_info [%p]", target_server_info);
-       int i, ret_code = EMAIL_ERROR_NONE;
-       email_server_info_t *server_info = NULL;
-
-       if(target_server_info && *target_server_info) {
-               server_info = *target_server_info;
-               EM_SAFE_FREE(server_info->service_name);
-               for(i = 0; i < server_info->protocol_conf_count; i++) {
-                       EM_SAFE_FREE(server_info->protocol_config_array[i].server_addr);
-               }
-               EM_SAFE_FREE(server_info->protocol_config_array);
-               EM_SAFE_FREE(server_info);
-       }
-       EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
-       return ret_code;        
-}
-
-INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
+INTERNAL_FUNC int emcore_save_default_account_id(char *multi_user_name, int input_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
        int ret_code = EMAIL_ERROR_NONE, result_value = 0;
@@ -1201,10 +1197,10 @@ INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
        }
 
        EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
-       return ret_code;        
+       return ret_code;
 }
 
-static int _recover_from_invalid_default_account_id(int *output_account_id)
+static int _recover_from_invalid_default_account_id(char *multi_user_name, int *output_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
        int ret_code = EMAIL_ERROR_NONE;
@@ -1216,9 +1212,9 @@ static int _recover_from_invalid_default_account_id(int *output_account_id)
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_account_list(&account_count, &result_account_list, false, false, &ret_code) || !result_account_list) {
+       if(!emstorage_get_account_list(multi_user_name, &account_count, &result_account_list, false, false, &ret_code) || !result_account_list) {
                if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
-                       EM_DEBUG_LOG ("no account found");                      
+                       EM_DEBUG_LOG ("no account found");
                else
                        EM_DEBUG_EXCEPTION ("emstorage_get_account_list() failed [%d]", ret_code);
                *output_account_id = 0;
@@ -1240,13 +1236,13 @@ FINISH_OFF:
        return ret_code;
 }
 
-INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
+INTERNAL_FUNC int emcore_load_default_account_id(char *multi_user_name, int *output_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
        int ret_code = EMAIL_ERROR_NONE;
        int result_value = 0;
        emstorage_account_tbl_t *result_account = NULL;
-       
+
        if (output_account_id == NULL) {
                ret_code = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
@@ -1261,7 +1257,7 @@ INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
        }
 
        if (*output_account_id != 0) {
-               if (!emstorage_get_account_by_id(*output_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &result_account, false, &ret_code)) {
+               if (!emstorage_get_account_by_id(multi_user_name, *output_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &result_account, false, &ret_code)) {
                        if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND) {
                                EM_DEBUG_LOG ("no account found account_id[%d]", *output_account_id);
                                *output_account_id = 0;
@@ -1274,10 +1270,10 @@ INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
        }
 
        if (*output_account_id == 0) {
-               if ((ret_code = _recover_from_invalid_default_account_id (output_account_id)) != EMAIL_ERROR_NONE) {
+               if ((ret_code = _recover_from_invalid_default_account_id (multi_user_name, output_account_id)) != EMAIL_ERROR_NONE) {
                        if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
                                EM_DEBUG_LOG ("no account found");
-                       else 
+                       else
                                EM_DEBUG_EXCEPTION("_recover_from_invalid_default_account() failed [%d]", ret_code);
                        *output_account_id = 0;
                }
@@ -1286,12 +1282,12 @@ INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
 FINISH_OFF:
        if (result_account)
                emstorage_free_account(&result_account, 1, NULL);
-       
+
        EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
-       return ret_code;        
+       return ret_code;
 }
 
-INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
+INTERNAL_FUNC int emcore_recover_from_secured_storage_failure(char *multi_user_name)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
@@ -1300,15 +1296,15 @@ INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
        emstorage_account_tbl_t *temp_account_tbl_list = NULL;
        emstorage_account_tbl_t *temp_account_tbl      = NULL;
 
-       if (!emstorage_get_account_list(&account_count, &temp_account_tbl_list, true, false, &err)) {
+       if (!emstorage_get_account_list(multi_user_name, &account_count, &temp_account_tbl_list, true, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
        for (i = 0; i < account_count; i++) {
-               if(!emstorage_get_account_by_id(temp_account_tbl_list[i].account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_PASSWORD, &temp_account_tbl, true, &err)) {
+               if(!emstorage_get_account_by_id(multi_user_name, temp_account_tbl_list[i].account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_PASSWORD, &temp_account_tbl, true, &err)) {
                        if(err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
-                               if(!emcore_delete_account(temp_account_tbl_list[i].account_id, &err)) {
+                               if(!emcore_delete_account(multi_user_name, temp_account_tbl_list[i].account_id, true, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
                                        goto FINISH_OFF;
                                }
@@ -1325,7 +1321,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, email_set_type_t input_set_operator, int input_sync_status)
+INTERNAL_FUNC int emcore_update_sync_status_of_account(char *multi_user_name, int input_account_id, email_set_type_t input_set_operator, int input_sync_status)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_set_operator [%d], input_sync_status [%d]", input_account_id, input_set_operator, input_sync_status);
        int err = EMAIL_ERROR_NONE;
@@ -1333,12 +1329,22 @@ INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, ema
 #ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__
        int err_from_account_svc = 0;
        emstorage_account_tbl_t *account_tbl_data = NULL;
+       vsm_zone_h join_zone = NULL;
+
        if (input_account_id != ALL_ACCOUNT && (input_sync_status == SYNC_STATUS_SYNCING)) {
-               if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_data, true, &err)) {
+               if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_data, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
                        goto FINISH_OFF;
                }
 
+               if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+                       err = emcore_set_join_zone(multi_user_name, &join_zone);
+                       if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
+                               goto FINISH_OFF;
+                       }
+               }
+
                err_from_account_svc = account_connect();
                EM_DEBUG_LOG_DEV("account_connect returns [%d]", err_from_account_svc);
 
@@ -1347,14 +1353,15 @@ INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, ema
                else if(input_set_operator == SET_TYPE_MINUS)
                        err_from_account_svc = account_update_sync_status_by_id(account_tbl_data->account_svc_id, ACCOUNT_SYNC_STATUS_IDLE);
 
-               EM_DEBUG_LOG("account_update_sync_status_by_id returns [%d] by id[%d]", err_from_account_svc, account_tbl_data->account_svc_id); 
+               EM_DEBUG_LOG("account_update_sync_status_by_id returns [%d] by id[%d]", err_from_account_svc, account_tbl_data->account_svc_id);
 
                err_from_account_svc = account_disconnect();
-               EM_DEBUG_LOG_DEV("account_disconnect returns [%d]", err_from_account_svc); 
+               EM_DEBUG_LOG_DEV("account_disconnect returns [%d]", err_from_account_svc);
+               emcore_unset_join_zone(join_zone);
        }
 #endif /* __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__ */
 
-       if (!emstorage_update_sync_status_of_account(input_account_id, input_set_operator, input_sync_status, true, &err))
+       if (!emstorage_update_sync_status_of_account(multi_user_name, input_account_id, input_set_operator, input_sync_status, true, &err))
                EM_DEBUG_EXCEPTION("emstorage_update_sync_status_of_account failed [%d]", err);
 
 #ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__
@@ -1404,7 +1411,7 @@ size_t emcore_curl_write_callback(char* data, size_t size, size_t nmemb, void* t
        }
 
        memcpy(*result_string + last_pos, data, received_length);
-       /*EM_DEBUG_LOG("result_string[%s]", *result_string);*/
+       /*EM_DEBUG_LOG_SEC("result_string[%s]", *result_string);*/
 
 FINISH_OFF:
        EM_DEBUG_FUNC_END("received_length [%d]", received_length);
@@ -1461,7 +1468,7 @@ static int emcore_get_xoauth2_access_token(char *input_refresh_token, char **out
 
        curl_ressult_code = curl_easy_perform(curl);
 
-       EM_DEBUG_LOG("CURLcode: %d (%s)", curl_ressult_code, curl_easy_strerror(curl_ressult_code));
+       EM_DEBUG_LOG_SEC("CURLcode: %d (%s)", curl_ressult_code, curl_easy_strerror(curl_ressult_code));
 
        if (0 == curl_ressult_code) {
                curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
@@ -1519,7 +1526,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(int input_account_id)
+INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(char *multi_user_name, int input_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d]", input_account_id);
        int err = EMAIL_ERROR_NONE;
@@ -1532,7 +1539,7 @@ INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(int input_account_id)
        email_account_t *ref_account = NULL;
 
 
-       ref_account = emcore_get_account_reference(input_account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, input_account_id);
 
        if(ref_account == NULL) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference() failed");
@@ -1576,13 +1583,11 @@ INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(int input_account_id)
 
        SNPRINTF(new_password_string, new_password_string_length, "%s\001%s", new_access_token, refresh_token);
 
-       if((err = emstorage_update_account_password(input_account_id, new_password_string, new_password_string)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_update_account_password(multi_user_name, input_account_id, new_password_string, new_password_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_update_account_password() failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       emcore_init_account_reference();
-
 FINISH_OFF:
 
        EM_SAFE_FREE(access_token);
@@ -1594,7 +1599,7 @@ FINISH_OFF:
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
        }
-       
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
index 4181890..0876dce 100755 (executable)
 #include "email-core-global.h"
 #include "email-core-alarm.h"
 
-#ifdef __FEATURE_USE_APPSYNC__
-#include <appsync.h>
-#endif
-
 
 INTERNAL_FUNC GList *alarm_data_list = NULL;
+static pthread_mutex_t alarm_data_lock = PTHREAD_MUTEX_INITIALIZER;
 
 INTERNAL_FUNC int emcore_get_alarm_data_by_alarm_id(alarm_id_t input_alarm_id, email_alarm_data_t **output_alarm_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] output_alarm_data [%p]", input_alarm_id, output_alarm_data);
        int err = EMAIL_ERROR_NONE;
-       GList *index = g_list_first(alarm_data_list);
+       GList *index = NULL;
        email_alarm_data_t *alarm_data = NULL;
 
        if (output_alarm_data == NULL) {
@@ -70,6 +67,8 @@ INTERNAL_FUNC int emcore_get_alarm_data_by_alarm_id(alarm_id_t input_alarm_id, e
                goto FINISH_OFF;
        }
 
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
+       index = g_list_first(alarm_data_list);
        while(index) {
                alarm_data = index->data;
                if(alarm_data->alarm_id == input_alarm_id) {
@@ -77,6 +76,7 @@ INTERNAL_FUNC int emcore_get_alarm_data_by_alarm_id(alarm_id_t input_alarm_id, e
                }
                index = g_list_next(index);
        }
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
 
        if(alarm_data)
                *output_alarm_data = alarm_data;
@@ -93,7 +93,7 @@ INTERNAL_FUNC int emcore_get_alarm_data_by_reference_id(email_alarm_class_t inpu
 {
        EM_DEBUG_FUNC_BEGIN("input_class_id [%d] input_reference_id [%d] output_alarm_data [%p]", input_class_id, input_reference_id, output_alarm_data);
        int err = EMAIL_ERROR_NONE;
-       GList *index = g_list_first(alarm_data_list);
+       GList *index = NULL;
        email_alarm_data_t *alarm_data = NULL;
 
        if (output_alarm_data == NULL) {
@@ -102,6 +102,10 @@ INTERNAL_FUNC int emcore_get_alarm_data_by_reference_id(email_alarm_class_t inpu
                goto FINISH_OFF;
        }
 
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
+
+       index = g_list_first(alarm_data_list);
+
        while(index) {
                alarm_data = index->data;
                if(alarm_data->class_id == input_class_id && alarm_data->reference_id == input_reference_id) {
@@ -111,6 +115,8 @@ INTERNAL_FUNC int emcore_get_alarm_data_by_reference_id(email_alarm_class_t inpu
                index = g_list_next(index);
        }
 
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
+
        if(index)
                *output_alarm_data = alarm_data;
        else
@@ -126,9 +132,11 @@ INTERNAL_FUNC int emcore_check_alarm_by_class_id(email_alarm_class_t input_class
 {
        EM_DEBUG_FUNC_BEGIN("input_class_id [%d]", input_class_id);
        int err = EMAIL_ERROR_NONE;
-       GList *index = g_list_first(alarm_data_list);
+       GList *index = NULL;
        email_alarm_data_t *alarm_data = NULL;
 
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
+       index = g_list_first(alarm_data_list);
        while(index) {
                alarm_data = index->data;
                if(alarm_data->class_id == input_class_id) {
@@ -137,6 +145,7 @@ INTERNAL_FUNC int emcore_check_alarm_by_class_id(email_alarm_class_t input_class
                }
                index = g_list_next(index);
        }
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
 
        if(!index)
                err = EMAIL_ERROR_ALARM_DATA_NOT_FOUND;
@@ -145,7 +154,7 @@ INTERNAL_FUNC int emcore_check_alarm_by_class_id(email_alarm_class_t input_class
        return err;
 }
 
-static int emcore_add_alarm_data_to_alarm_data_list(alarm_id_t input_alarm_id, email_alarm_class_t input_class_id, int input_reference_id, time_t input_trigger_at_time, int (*input_alarm_callback)(int, void *), void *user_data)
+static int emcore_add_alarm_data_to_alarm_data_list(char *multi_user_name, alarm_id_t input_alarm_id, email_alarm_class_t input_class_id, int input_reference_id, time_t input_trigger_at_time, int (*input_alarm_callback)(email_alarm_data_t*, void *), void *user_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] input_class_id[%d] input_reference_id[%d] input_trigger_at_time[%d] input_alarm_callback [%p] user_data[%p]", input_alarm_id, input_class_id, input_reference_id, input_trigger_at_time, input_alarm_callback, user_data);
        int err = EMAIL_ERROR_NONE;
@@ -164,19 +173,23 @@ static int emcore_add_alarm_data_to_alarm_data_list(alarm_id_t input_alarm_id, e
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
+
        alarm_data->alarm_id        = input_alarm_id;
        alarm_data->class_id        = input_class_id;
        alarm_data->reference_id    = input_reference_id;
        alarm_data->trigger_at_time = input_trigger_at_time;
        alarm_data->alarm_callback  = input_alarm_callback;
        alarm_data->user_data       = user_data;
+       alarm_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
        alarm_data_list = g_list_append(alarm_data_list, (gpointer)alarm_data);
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
 
 FINISH_OFF:
 
-       EM_DEBUG_FUNC_END("err [%d]", EMAIL_ERROR_NONE);
-       return EMAIL_ERROR_NONE;
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
 static int emcore_remove_alarm(email_alarm_data_t *input_alarm_data)
@@ -186,11 +199,14 @@ static int emcore_remove_alarm(email_alarm_data_t *input_alarm_data)
        int ret = ALARMMGR_RESULT_SUCCESS;
        int err = EMAIL_ERROR_NONE;
 
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
        EM_DEBUG_LOG("alarm_id [%d]", input_alarm_data->alarm_id);
        if ((ret = alarmmgr_remove_alarm(input_alarm_data->alarm_id)) != ALARMMGR_RESULT_SUCCESS) {
                EM_DEBUG_EXCEPTION ("alarmmgr_remove_alarm failed [%d]", ret);
                err = EMAIL_ERROR_SYSTEM_FAILURE;
        }
+       EM_SAFE_FREE(input_alarm_data->multi_user_name);
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
@@ -201,11 +217,26 @@ INTERNAL_FUNC int emcore_delete_alram_data_from_alarm_data_list(email_alarm_data
        EM_DEBUG_FUNC_BEGIN("input_alarm_data [%p]", input_alarm_data);
        int err = EMAIL_ERROR_NONE;
 
+       if (input_alarm_data == NULL ) {
+               EM_DEBUG_EXCEPTION ("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
        if ((err = emcore_remove_alarm(input_alarm_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION ("emcore_remove_alarm failed [%d]", err);
        }
 
-       alarm_data_list = g_list_remove(alarm_data_list, input_alarm_data);
+       ENTER_CRITICAL_SECTION(alarm_data_lock);
+       alarm_data_list = g_list_remove_all(alarm_data_list, input_alarm_data);
+
+       if (g_list_length(alarm_data_list) == 0) {
+               g_list_free (alarm_data_list);
+               alarm_data_list = NULL;
+       }
+       LEAVE_CRITICAL_SECTION(alarm_data_lock);
+
+FINISH_OFF:
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
@@ -218,7 +249,7 @@ INTERNAL_FUNC int emcore_delete_alram_data_by_reference_id(email_alarm_class_t i
        email_alarm_data_t *alarm_data = NULL;
 
        if ((err = emcore_get_alarm_data_by_reference_id(input_class_id, input_reference_id, &alarm_data)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_LOG ("emcore_remove_alarm return [%d]", err);
+               EM_DEBUG_LOG ("emcore_get_alarm_data_by_reference_id return [%d]", err);
                goto FINISH_OFF;
        }
 
@@ -231,7 +262,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_add_alarm(time_t input_trigger_at_time, email_alarm_class_t input_class_id, int input_reference_id, int (*input_alarm_callback)(int, void *), void *input_user_data)
+INTERNAL_FUNC int emcore_add_alarm(char *multi_user_name, time_t input_trigger_at_time, email_alarm_class_t input_class_id, int input_reference_id, int (*input_alarm_callback)(email_alarm_data_t*, void *), void *input_user_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_trigger_at_time[%d] input_class_id[%d] input_reference_id[%d] input_alarm_callback[%p] input_user_data[%p]", input_trigger_at_time, input_class_id, input_reference_id, input_alarm_callback, input_user_data);
 
@@ -247,25 +278,13 @@ INTERNAL_FUNC int emcore_add_alarm(time_t input_trigger_at_time, email_alarm_cla
 
        EM_DEBUG_ALARM_LOG("alarm_interval [%d]", (int)alarm_interval);
 
-#ifdef __FEATURE_USE_APPSYNC__
-       if(input_class_id == EMAIL_ALARM_CLASS_AUTO_POLLING) {
-               if ((ret = appsync_daemon_schedule_after_delay(alarm_interval, 0, &alarm_id)) != APPSYNC_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("appsync_daemon_schedule_after_delay failed [%d]",ret);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
-       }
-       else
-#endif
-       {
-               if ((ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, alarm_interval, ALARM_REPEAT_MODE_ONCE, EMAIL_ALARM_DESTINATION, &alarm_id)) != ALARMMGR_RESULT_SUCCESS) {
-                       EM_DEBUG_EXCEPTION("alarmmgr_add_alarm failed [%d]",ret);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
+       if ((ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, alarm_interval, ALARM_REPEAT_MODE_ONCE, EMAIL_ALARM_DESTINATION, &alarm_id)) != ALARMMGR_RESULT_SUCCESS) {
+               EM_DEBUG_EXCEPTION("alarmmgr_add_alarm failed [%d]",ret);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
        }
 
-       if((err = emcore_add_alarm_data_to_alarm_data_list(alarm_id, input_class_id, input_reference_id, input_trigger_at_time, input_alarm_callback, input_user_data)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_add_alarm_data_to_alarm_data_list(multi_user_name, alarm_id, input_class_id, input_reference_id, input_trigger_at_time, input_alarm_callback, input_user_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_alarm_data_to_alarm_data_list failed [%d]", err);
                goto FINISH_OFF;
        }
diff --git a/email-core/email-core-auto-download.c b/email-core/email-core-auto-download.c
new file mode 100755 (executable)
index 0000000..447191b
--- /dev/null
@@ -0,0 +1,818 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Minsoo Kim <minnsoo.kim@samsung.com>, Kyuho Jo <kyuho.jo@samsung.com>,
+* Sunghyun Kwon <sh0701.kwon@samsung.com>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+
+#include <stdio.h>
+#include <glib.h>
+#include <malloc.h>
+#include <pthread.h>
+#include <vconf.h>
+#include "email-storage.h"
+#include "email-utilities.h"
+#include "email-daemon.h"
+#include "email-network.h"
+#include "email-core-global.h"
+#include "email-core-account.h"
+#include "email-core-event.h"
+#include "email-core-utils.h"
+#include "email-core-mailbox.h"
+#include "email-core-imap-mailbox.h"
+#include "email-core-mail.h"
+#include "email-core-mailbox-sync.h"
+#include "email-core-smtp.h"
+#include "email-core-utils.h"
+#include "email-debug-log.h"
+#include "email-types.h"
+#include "email-internal-types.h"
+#include "email-core-auto-download.h"
+
+/*-----------------------------------------------------------------------------
+ * Auto Download Event Queue
+ *---------------------------------------------------------------------------*/
+INTERNAL_FUNC thread_t g_auto_download_thread;
+INTERNAL_FUNC pthread_cond_t  _auto_downalod_available_signal = PTHREAD_COND_INITIALIZER;
+INTERNAL_FUNC pthread_mutex_t *_auto_download_queue_lock = NULL;
+
+INTERNAL_FUNC GQueue *g_auto_download_que = NULL;
+INTERNAL_FUNC int g_auto_download_loop = 1;
+INTERNAL_FUNC int auto_download_thread_run = 0;
+
+#define AUTO_DOWNLOAD_QUEUE_MAX 100
+
+static void* worker_auto_download_queue(void *arg);
+
+INTERNAL_FUNC int emcore_start_auto_download_loop(int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int thread_error;
+
+       if (err_code != NULL)
+               *err_code = EMAIL_ERROR_NONE;
+
+       if (g_auto_download_thread) {
+               EM_DEBUG_EXCEPTION("auto downlaod thread is already running...");
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_UNKNOWN;
+               return true;
+       }
+
+       g_auto_download_que = g_queue_new();
+       g_queue_init(g_auto_download_que);
+       g_auto_download_loop = 1;
+
+       /* initialize lock */
+       INITIALIZE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       /* create thread */
+       THREAD_CREATE(g_auto_download_thread, worker_auto_download_queue, NULL, thread_error);
+
+       if (thread_error != 0) {
+               EM_DEBUG_EXCEPTION("cannot create thread");
+               g_auto_download_loop = 0;
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
+               return false;
+       }
+
+       if (err_code != NULL)
+               *err_code = EMAIL_ERROR_NONE;
+
+       return true;
+}
+
+
+INTERNAL_FUNC int emcore_auto_download_loop_continue(void)
+{
+       return g_auto_download_loop;
+}
+
+
+INTERNAL_FUNC int emcore_stop_auto_download_loop(int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       if (err_code != NULL)
+               *err_code = EMAIL_ERROR_NONE;
+
+       if (!g_auto_download_thread) {
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_UNKNOWN;
+               return false;
+       }
+
+       /* stop event_data loop */
+       g_auto_download_loop = 0;
+
+       WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+
+       /* wait for thread finished */
+       THREAD_JOIN(g_auto_download_thread);
+
+       g_queue_free(g_auto_download_que);
+
+       g_auto_download_thread = 0;
+
+       DELETE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+       DELETE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+
+       if (err_code != NULL)
+               *err_code = EMAIL_ERROR_NONE;
+       EM_DEBUG_FUNC_END();
+       return true;
+}
+
+
+INTERNAL_FUNC int emcore_insert_auto_download_event(email_event_auto_download *event_data, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
+
+       if (!event_data) {
+               EM_DEBUG_EXCEPTION("Invalid Parameter");
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       if (!g_auto_download_thread) {
+               EM_DEBUG_EXCEPTION("g_auto_download_thread is not ready");
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
+               return false;
+       }
+
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       int q_length = 0;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       if (g_auto_download_que)
+               q_length = g_queue_get_length(g_auto_download_que);
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+
+       if (q_length > AUTO_DOWNLOAD_QUEUE_MAX) {
+               EM_DEBUG_EXCEPTION("auto download que is full...");
+               error = EMAIL_ERROR_EVENT_QUEUE_FULL;
+               ret = false;
+       } else {
+               event_data->status = EMAIL_EVENT_STATUS_WAIT;
+               g_queue_push_tail(g_auto_download_que, event_data);
+               //WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+               ret = true;
+       }
+
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       if (err_code) {
+               EM_DEBUG_LOG("ERR [%d]", error);
+               *err_code = error;
+       }
+
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_retrieve_auto_download_event(email_event_auto_download **event_data, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
+
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       int q_length = 0;
+       //email_event_auto_download *poped = NULL;
+       email_event_auto_download *head_event = NULL;
+
+       if (g_auto_download_que)
+               q_length = g_queue_get_length(g_auto_download_que);
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+
+       if (!q_length) {
+               error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
+               EM_DEBUG_LOG("QUEUE is empty");
+               goto FINISH_OFF;
+       }
+
+       while (1) {
+               head_event = (email_event_auto_download *)g_queue_peek_head(g_auto_download_que);
+               if (!head_event) {
+                       error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
+                       EM_DEBUG_LOG_DEV ("QUEUE is empty");
+                       break;
+               }
+
+               /*if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
+                       EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
+                       poped = g_queue_pop_head(g_auto_download_que);
+                       if (poped) {
+                               EM_SAFE_FREE(poped);
+                       } else {
+                               error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
+                               EM_DEBUG_LOG("QUEUE is empty");
+                               break;
+                       }
+               } else*/
+               {
+                       head_event->status = EMAIL_EVENT_STATUS_STARTED;
+                       *event_data = head_event;
+                       ret = true;
+                       break;
+               }
+       }
+
+FINISH_OFF:
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_is_auto_download_queue_empty(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int ret = false;
+       int q_length = 0;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       if (g_auto_download_que)
+               q_length = g_queue_get_length(g_auto_download_que);
+
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+
+       if (q_length <= 0) {
+               EM_DEBUG_LOG("auto downlaod que is empty...");
+               ret = true;
+       }
+
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_is_auto_download_queue_full(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int ret = false;
+       int q_length = 0;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       if (g_auto_download_que)
+               q_length = g_queue_get_length(g_auto_download_que);
+
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+
+       if (q_length > AUTO_DOWNLOAD_QUEUE_MAX) {
+               EM_DEBUG_LOG("auto downlaod que is full...");
+               ret = true;
+       }
+
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_clear_auto_download_queue(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int ret = false;
+       int i = 0;
+       int q_length = 0;
+       email_event_auto_download *pop_elm = NULL;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+       q_length = g_auto_download_que? g_queue_get_length (g_auto_download_que): 0;
+
+       for (i = 0; i < q_length; i++) {
+               pop_elm = (email_event_auto_download *)g_queue_peek_nth(g_auto_download_que, i);
+
+               if (pop_elm) {
+                       EM_SAFE_FREE(pop_elm);
+               }
+       }
+
+       g_queue_clear(g_auto_download_que);
+       ret = true;
+
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+
+static void* worker_auto_download_queue(void *arg)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+
+       int ai = 0;
+       int mi = 0;
+       int di = 0;
+       int account_count = 0;
+       int mailbox_count = 0;
+       int activity_count = 0;
+       int activity_list_count = 0;
+       int *account_list = NULL;
+       int *mailbox_list = NULL;
+       email_event_auto_download *event_data = NULL;
+       email_event_auto_download *started_event = NULL;
+       email_event_auto_download *activity_list = NULL;
+
+
+       if (!emstorage_open(NULL, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
+               return false;
+       }
+
+       /* check that event_data loop is continuous */
+       while (emcore_auto_download_loop_continue()) {
+
+               ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+               if (!emcore_retrieve_auto_download_event(&event_data, &err)) {
+                       /* no event_data pending */
+                       if (err != EMAIL_ERROR_EVENT_QUEUE_EMPTY) {
+                               LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                               continue;
+                       }
+
+                       activity_count = 0;
+                       if (!emstorage_get_auto_download_activity_count(NULL, &activity_count, false, &err)) {
+                               EM_DEBUG_LOG("emstorage_get_auto_download_activity_count failed [%d]", err);
+                               goto CHECK_CONTINUE;
+                       }
+
+                       if (activity_count <= 0) {
+                               EM_DEBUG_LOG("auto download activity count is 0");
+                               goto CHECK_CONTINUE;
+                       }
+
+                       account_count = 0;
+                       EM_SAFE_FREE(account_list);
+                       if (!emstorage_get_auto_download_account_list(NULL, &account_list, &account_count, false, &err)) {
+                               EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_account_list failed.. [%d]", err);
+                               goto CHECK_CONTINUE;
+                       }
+
+                       if (account_count <= 0 || !account_list) {
+                               EM_DEBUG_LOG("auto download account count is 0");
+                               goto CHECK_CONTINUE;
+                       }
+
+                       for (ai = 0; ai < account_count; ai++) {
+
+                               email_account_t *account_ref = NULL;
+                               if (!(account_ref = emcore_get_account_reference(NULL, account_list[ai]))) {
+                                       EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_list[ai]);
+                                       err = EMAIL_ERROR_INVALID_ACCOUNT;
+                                       continue;
+                               }
+
+                               if (!(account_ref->wifi_auto_download)) {
+                                       if (account_ref) {
+                                               emcore_free_account(account_ref);
+                                               EM_SAFE_FREE(account_ref);
+                                       }
+
+                                       continue;
+                               }
+
+                               if (account_ref) {
+                                       emcore_free_account(account_ref);
+                                       EM_SAFE_FREE(account_ref);
+                               }
+
+                               mailbox_count = 0;
+                               EM_SAFE_FREE(mailbox_list);
+                               if (!emstorage_get_auto_download_mailbox_list(NULL, account_list[ai], &mailbox_list, &mailbox_count, false, &err)) {
+                                       EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_mailbox_list failed.. [%d]", err);
+                                       goto CHECK_CONTINUE;
+                               }
+
+                               if (mailbox_count <= 0 || !mailbox_list) {
+                                       EM_DEBUG_LOG("auto download mailbox count is 0");
+                                       goto CHECK_CONTINUE;
+                               }
+
+                               for (mi = 0; mi < mailbox_count; mi++) {
+                                       emstorage_mailbox_tbl_t *target_mailbox = NULL;
+
+                                       activity_list_count = 0;
+                                       EM_SAFE_FREE(activity_list);
+
+                                       if ((err = emstorage_get_mailbox_by_id(NULL, mailbox_list[mi], &target_mailbox)) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
+                                       }
+
+                                       if (target_mailbox) {
+                                               if (target_mailbox->mailbox_type != EMAIL_MAILBOX_TYPE_INBOX) {
+                                                       EM_DEBUG_LOG("Not INBOX mail, skip to next");
+                                                       emstorage_free_mailbox(&target_mailbox, 1, NULL);
+                                                       continue;
+                                               }
+                                               emstorage_free_mailbox(&target_mailbox, 1, NULL);
+                                       }
+
+                                       if (!emstorage_get_auto_download_activity(NULL, account_list[ai], mailbox_list[mi], &activity_list, &activity_list_count, false, &err)) {
+                                               EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_activity failed.. [%d]", err);
+                                               goto CHECK_CONTINUE;
+                                       }
+
+                                       if (activity_list_count <= 0 || !activity_list) {
+                                               EM_DEBUG_LOG("auto download activity count is 0");
+                                               goto CHECK_CONTINUE;
+                                       }
+
+                                       for (di = 0; di < activity_list_count; di++) {
+                                               email_event_auto_download *activity = NULL;
+                                               activity = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download));
+                                               if (!activity) {
+                                                       EM_DEBUG_EXCEPTION("Malloc failed");
+                                                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                                       goto CHECK_CONTINUE;
+                                               }
+
+                                               activity->activity_id = activity_list[di].activity_id;
+                                               activity->status = 0;
+                                               activity->account_id = activity_list[di].account_id;
+                                               activity->mail_id = activity_list[di].mail_id;
+                                               activity->server_mail_id = activity_list[di].server_mail_id;
+                                               activity->mailbox_id = activity_list[di].mailbox_id;
+
+                                               if (!emcore_insert_auto_download_event(activity, &err)) {
+                                                       EM_DEBUG_EXCEPTION("emcore_insert_auto_download_event is failed [%d]", err);
+                                                       EM_SAFE_FREE(activity);
+                                                       if (err == EMAIL_ERROR_EVENT_QUEUE_FULL) goto CHECK_CONTINUE;
+                                               }
+                                       }
+                               }
+                       }
+
+CHECK_CONTINUE:
+
+                       EM_SAFE_FREE(account_list);
+                       EM_SAFE_FREE(mailbox_list);
+                       EM_SAFE_FREE(activity_list);
+
+                       if (!emcore_is_auto_download_queue_empty()) {
+                               LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                               continue;
+                       }
+
+                       auto_download_thread_run = 0;
+                       //go to sleep when queue is empty
+                       SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
+                       EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
+                       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+               }
+               else {
+                       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                       EM_DEBUG_LOG_DEV(">>>>>>>>>>>>>>> Got auto download event_data !!! <<<<<<<<<<<<<<<");
+                       int state1 = 0, state2 = 0, state3 = 0, wifi_status = 0;
+                       char uid_str[50] = {0, };
+                       emstorage_mail_tbl_t *mail = NULL;
+                       emstorage_mailbox_tbl_t *target_mailbox = NULL;
+                       email_account_t *account_ref = NULL;
+                       auto_download_thread_run = 1;
+
+                       if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
+                       }
+
+                       EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
+
+                       if ((state1 = emcore_get_pbd_thd_state()) == true ||
+                                       (state2 = emcore_is_event_queue_empty()) == false ||
+                                       (state3 = emcore_is_send_event_queue_empty()) == false ||
+                                       (wifi_status <= 1)) {
+
+                               EM_DEBUG_LOG("Auto Download Thread going to SLEEP");
+
+                               ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                               auto_download_thread_run = 0;
+                               SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
+                               EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
+                               LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+                               auto_download_thread_run = 1;
+                       }
+
+                       if (!(account_ref = emcore_get_account_reference(event_data->multi_user_name, event_data->account_id))) {
+                               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", event_data->account_id);
+                               err = EMAIL_ERROR_INVALID_ACCOUNT;
+                               goto POP_HEAD;
+                       }
+
+                       if (!(account_ref->wifi_auto_download)) {
+                               EM_DEBUG_LOG("ACCOUNT[%d] AUTO DOWNLOAD MODE OFF", event_data->account_id);
+                               goto POP_HEAD;
+                       }
+
+                       if (account_ref) {
+                               emcore_free_account(account_ref);
+                               EM_SAFE_FREE(account_ref);
+                       }
+
+                       if ((err = emstorage_get_mailbox_by_id(event_data->multi_user_name, event_data->mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
+                       }
+
+                       if (target_mailbox) {
+                               if (target_mailbox->mailbox_type != EMAIL_MAILBOX_TYPE_INBOX) {
+                                       EM_DEBUG_LOG("Not INBOX mail, skip to next");
+                                       emstorage_free_mailbox(&target_mailbox, 1, NULL);
+                                       goto DELETE_ACTIVITY;
+                               }
+
+                               emstorage_free_mailbox(&target_mailbox, 1, NULL);
+                       }
+
+                       /* Handling storage full */
+                       if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
+                               EM_DEBUG_EXCEPTION("Storage is full");
+
+                               ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                               auto_download_thread_run = 0;
+                               SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
+                               EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
+                               LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+
+                               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) {
+                               EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
+                       }
+                       else {
+                               if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
+                                       EM_DEBUG_LOG("fully downloaded mail");
+                               }
+                               else {
+                                       EM_DEBUG_LOG("#####AUTO DOWNLOAD BODY: ACCOUNT_ID[%d] MAILBOX_ID[%d] MAIL_ID[%d] UID[%d] ACTIVITY[%d]#####",
+                                                       event_data->account_id, event_data->mailbox_id,
+                                                       event_data->mail_id, event_data->server_mail_id, event_data->activity_id);
+
+                                       if (!emcore_gmime_download_body_sections(event_data->multi_user_name, NULL, event_data->account_id, event_data->mail_id,
+                                               1, NO_LIMITATION, -1, 0, 1, &err))
+                                               EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
+                               }
+
+                               if (mail->attachment_count > 0) {
+                                       int i = 0;
+                                       int j = 0;
+                                       int attachment_count = 0;
+                                       emstorage_attachment_tbl_t *attachment_list = NULL;
+
+                                       if ((err = emstorage_get_attachment_list(event_data->multi_user_name, event_data->mail_id, true, &attachment_list, &attachment_count)) != EMAIL_ERROR_NONE ) {
+                                               EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
+                                       } else {
+                                               for (i = 0; i < attachment_count; i++) {
+                                                       if (attachment_list[i].attachment_inline_content_status)
+                                                               continue;
+
+                                                       j++;
+
+                                                       if (attachment_list[i].attachment_save_status)
+                                                               continue;
+
+                                                       EM_DEBUG_LOG("#####AUTO DOWNLOAD ATTACHMENT[%d]: ACCOUNT_ID[%d] MAILBOX_ID[%d] MAIL_ID[%d] UID[%d] ACTIVITY[%d]#####",
+                                                                       j, event_data->account_id, event_data->mailbox_id,
+                                                                       event_data->mail_id, event_data->server_mail_id, event_data->activity_id);
+
+                                                       if (!emcore_gmime_download_attachment(event_data->multi_user_name, event_data->mail_id, j, 0, -1, 1, &err))
+                                                               EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
+                                               }
+
+                                               if (attachment_list)
+                                                       emstorage_free_attachment(&attachment_list, attachment_count, NULL);
+                                       }
+                               }
+
+                               if (mail)
+                                       emstorage_free_mail(&mail, 1, NULL);
+                       }
+
+DELETE_ACTIVITY:
+                       if (!emcore_delete_auto_download_activity(event_data->multi_user_name, event_data->account_id, event_data->mail_id, event_data->activity_id, &err)) {
+                               EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
+                       }
+
+
+POP_HEAD:
+                       /* free event itself */
+                       ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                       started_event = g_queue_pop_head(g_auto_download_que);
+                       LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
+                       if (!started_event) {
+                               EM_DEBUG_EXCEPTION("Failed to g_queue_pop_head");
+                       }
+                       else {
+                               EM_SAFE_FREE(started_event->multi_user_name);
+                               EM_SAFE_FREE(started_event);
+                       }
+
+                       if (account_ref) {
+                               emcore_free_account(account_ref);
+                               EM_SAFE_FREE(account_ref);
+                       }
+
+                       em_flush_memory();
+               }
+       }
+
+       if (!emstorage_close(&err))
+               EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
+
+       emcore_close_recv_stream_list();
+
+       EM_DEBUG_FUNC_END();
+       return SUCCESS;
+}
+
+
+INTERNAL_FUNC int emcore_insert_auto_download_job(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int auto_download_on, char *uid, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id [%d], maibox_id[%d], mail_id[%d], uid[%p]", account_id, mailbox_id, mail_id, uid);
+
+       int ret = false;
+       int err = EMAIL_ERROR_NONE;
+       int event_pushed = 0;
+       email_event_auto_download *ad_event = NULL;
+
+       if (account_id < FIRST_ACCOUNT_ID || mailbox_id <= 0 || mail_id < 0 || !uid) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       ad_event = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download));
+
+       if (!ad_event) {
+               EM_DEBUG_EXCEPTION("em_malloc failed...");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       ad_event->status = 0;
+       ad_event->account_id = account_id;
+       ad_event->mail_id = mail_id;
+       ad_event->server_mail_id = strtoul(uid, NULL, 0);
+       ad_event->mailbox_id = mailbox_id;
+       ad_event->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
+       if (!emcore_insert_auto_download_activity(ad_event, &(ad_event->activity_id), &err)) {
+               EM_DEBUG_EXCEPTION("Inserting auto download activity failed with error[%d]", err);
+               goto FINISH_OFF;
+       }
+       else {
+               ret = true;
+
+               if (!auto_download_on) {
+                       goto FINISH_OFF;
+               }
+
+               if (emcore_is_auto_download_queue_full()) {
+                       EM_DEBUG_LOG("Activity inserted only in DB .. Queue is Full");
+               }
+               else {
+                       if (!emcore_insert_auto_download_event(ad_event, &err)) {
+                               EM_DEBUG_EXCEPTION("emcore_insert_auto_download_event is failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
+                       event_pushed = 1;
+               }
+       }
+
+FINISH_OFF:
+
+       if (!event_pushed)
+               EM_SAFE_FREE(ad_event);
+
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_insert_auto_download_activity(email_event_auto_download *local_activity, int *activity_id, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
+
+       if (!local_activity || !activity_id) {
+               EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int ret = false;
+       int err = EMAIL_ERROR_NONE;
+       int before_tr_begin = 0;
+
+       if (!emstorage_begin_transaction(local_activity->multi_user_name, NULL, NULL, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
+               before_tr_begin = 1;
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_add_auto_download_activity(local_activity->multi_user_name, local_activity, activity_id, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_add_auto_download_activity failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       if (ret == true) {      /*  COMMIT TRANSACTION; */
+               if (emstorage_commit_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false) {
+                       err = EMAIL_ERROR_DB_FAILURE;
+                       ret = false;
+               }
+       } else {        /*  ROLLBACK TRANSACTION; */
+               if (!before_tr_begin && emstorage_rollback_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false)
+                       err = EMAIL_ERROR_DB_FAILURE;
+       }
+
+       if (err_code != NULL)
+               *err_code = err;
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
+               EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int ret = false;
+       int err = EMAIL_ERROR_NONE;
+       int before_tr_begin = 0;
+
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
+               before_tr_begin = 1;
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_delete_auto_download_activity(multi_user_name, account_id, mail_id, activity_id, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_delete_auto_download_activity failed [%d]", err);
+               goto FINISH_OFF;
+       }
+       else if (err == EMAIL_ERROR_DATA_NOT_FOUND)
+               err = EMAIL_ERROR_NONE;
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (ret == true) {      /*  COMMIT TRANSACTION; */
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
+                       err = EMAIL_ERROR_DB_FAILURE;
+                       ret = false;
+               }
+       } else {        /*  ROLLBACK TRANSACTION; */
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
+                       err = EMAIL_ERROR_DB_FAILURE;
+       }
+
+       if (err_code != NULL)
+               *err_code = err;
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+
index 7247b4e..42a6f90 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,9 +24,9 @@
  * File :  email-core-cert.h
  * Desc :  Certificate API
  *
- * Auth : 
+ * Auth :
  *
- * History : 
+ * History :
  *    2006.08.16  :  created
  *****************************************************************************/
 #include <openssl/pkcs7.h>
@@ -55,7 +55,7 @@
 
 typedef enum {
        CERT_TYPE_ETC          = 0,
-       CERT_TYPE_PKCS12,       
+       CERT_TYPE_PKCS12,
        CERT_TYPE_PKCS7,
        CERT_TYPE_P7S
 } cert_type;
@@ -73,7 +73,7 @@ static int emcore_get_certificate_type(char *extension, int *err_code)
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        while(supported_file_type[index]) {
                EM_DEBUG_LOG_SEC("certificate extension[%d]:[%s]", index, supported_file_type[index]);
                if (strcasecmp(extension, supported_file_type[index]) == 0) {
@@ -95,7 +95,7 @@ static int emcore_get_certificate_type(char *extension, int *err_code)
                        default:
                                type = CERT_TYPE_ETC;
                                err = EMAIL_ERROR_INVALID_CERTIFICATE;
-                               break;                  
+                               break;
                        }
                }
                index++;
@@ -110,7 +110,7 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("File type is [%d]", type);
        return type;
 }
-/*     
+/*
 static GList *emcore_make_glist_from_string(char *email_address_list)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("email_address list : [%s]", email_address_list);
@@ -127,11 +127,11 @@ static GList *emcore_make_glist_from_string(char *email_address_list)
                email_list = g_list_append(email_list, token_list[index]);
                index++;
        }
-       
+
        if (p_email_address_list)
                g_free(p_email_address_list);
 
-       return email_list;      
+       return email_list;
 }
 
 static char *emcore_store_public_certificate(STACK_OF(X509) *certificates, char *email_address, int *err_code)
@@ -161,7 +161,7 @@ static char *emcore_store_public_certificate(STACK_OF(X509) *certificates, char
                EM_DEBUG_LOG("Write the certificate in pem file : [%d]", index);
                PEM_write_bio_X509(outfile, sk_X509_value(certificates, index));
        }
-       
+
 FINISH_OFF:
 
        if (outfile)
@@ -182,14 +182,12 @@ INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, char *password, EVP_PK
        FILE *fp = NULL;
        PKCS12 *p12 = NULL;
 
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-       if (!(fp = fopen(certificate, "rb"))) {
-               EM_DEBUG_EXCEPTION_SEC("fopen failed : [%s]", certificate);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(certificate, "rb", &fp);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("em_fopen failed : [%s] [%d]", certificate, err);
                goto FINISH_OFF;
        }
-       
+
        p12 = d2i_PKCS12_fp(fp, NULL);
        if (!p12) {
                EM_DEBUG_EXCEPTION("d2i_PKCS12_fp failed");
@@ -216,7 +214,7 @@ FINISH_OFF:
        if (err_code)
                *err_code = err;
 
-       return ret;     
+       return ret;
 }
 #endif
 
@@ -232,7 +230,7 @@ INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X5
        X509 *t_cert = NULL;
        BIO *bio_mem = NULL;
 //     STACK_OF(X509) *t_ca = NULL;
-       
+
        /* Variable for private key */
        EVP_PKEY *t_pri_key = NULL;
 
@@ -319,7 +317,7 @@ INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X5
        }
 
        ret = true;
-       
+
 FINISH_OFF:
 
        if (bio_mem)
@@ -345,10 +343,107 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *save_name, int *err_code)
+INTERNAL_FUNC int emcore_load_PFX_file_from_string(char *certificate, char **key_string, int *key_size, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("certificate : [%s]", certificate);
+       int err = EMAIL_ERROR_NONE;
+       int ret = false;
+       size_t p_key_size = 0;
+       char *private_key = NULL;
+
+       /* Variable for certificate */
+       X509 *t_cert = NULL;
+//     STACK_OF(X509) *t_ca = NULL;
+
+       /* Variable for private key */
+       EVP_PKEY *t_pri_key = NULL;
+
+       CertSvcString csstring;
+       CertSvcInstance cert_instance;
+       CertSvcCertificate csc_cert;
+       CertSvcCertificateList certificate_list;
+
+       if (certificate == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       /* Create instance */
+       err = certsvc_instance_new(&cert_instance);
+       if (err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_instance_new failed : [%d]", err);
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Make the pfxID string */
+       err = certsvc_string_new(cert_instance, certificate, EM_SAFE_STRLEN(certificate), &csstring);
+       if (err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_string_new failed : [%d]", err);
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Load the certificate list of pkcs12 type */
+       err = certsvc_pkcs12_load_certificate_list(cert_instance, csstring, &certificate_list);
+       if (err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_pkcs12_load_certificate_list failed : [%d]", err);
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get a certificate */
+       err = certsvc_certificate_list_get_one(certificate_list, 0, &csc_cert);
+       if (err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_certificate_list_get_one failed : [%d]", err);
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Convert from char to X509 */
+       err = certsvc_certificate_dup_x509(csc_cert, &t_cert);
+       if (t_cert == NULL || err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_certificate_dup_x509 failed");
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the private key */
+       err = certsvc_pkcs12_private_key_dup(cert_instance, csstring, &private_key, &p_key_size);
+       if (err != CERTSVC_SUCCESS) {
+               EM_DEBUG_EXCEPTION("certsvc_pkcs12_private_key_dup failed : [%d]", err);
+               err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG_DEV("key_size : [%d], private_key : [%s]", p_key_size, private_key);
+       ret = true;
+
+FINISH_OFF:
+
+       if (true) {
+               if (key_string)
+                       *key_string = private_key;
+
+               if (key_size)
+                       *key_size = p_key_size;
+       } else {
+               X509_free(t_cert);
+               EVP_PKEY_free(t_pri_key);
+               EM_SAFE_FREE(private_key);
+       }
+
+       if (err_code)
+               *err_code = err;
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_add_public_certificate(char *multi_user_name, char *public_cert_path, char *save_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("Path [%s], filename [%s]", public_cert_path, save_name);
-       int err = EMAIL_ERROR_NONE;     
+       int err = EMAIL_ERROR_NONE;
        int ret = false;
        int validity = 0;
        int cert_type = 0;
@@ -362,9 +457,9 @@ INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *sa
        if (public_cert_path == NULL || save_name == NULL) {
                EM_DEBUG_EXCEPTION("Invalid parameter");
                err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;                
+               goto FINISH_OFF;
        }
-       
+
        /* Initilize the structure of certificate */
        context = cert_svc_cert_context_init();
 
@@ -387,15 +482,15 @@ INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *sa
        if (cert_type == CERT_TYPE_P7S) {
                extension = "der";
        }
-       
+
        SNPRINTF(temp_file, sizeof(temp_file), "%s%s%s.%s", MAILTEMP, DIR_SEPERATOR, save_name, extension);
-       EM_DEBUG_LOG("temp cert path : [%s]", temp_file);
+       EM_DEBUG_LOG_SEC("temp cert path : [%s]", temp_file);
 
        if (!emstorage_copy_file(public_cert_path, temp_file, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
                goto FINISH_OFF;
        }
-       
+
        /* Load the public certificate */
        err = cert_svc_load_file_to_context(context, temp_file);
        if (err != CERT_SVC_ERR_NO_ERROR) {
@@ -409,7 +504,7 @@ INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *sa
                EM_DEBUG_EXCEPTION("cert_svc_verify_certificate failed");
 //             err = EMAIL_ERROR_INVALID_CERTIFICATE;
 //             goto FINISH_OFF;
-       } 
+       }
 
        if (validity <= 0) {
                EM_DEBUG_LOG("Invalid certificate");
@@ -443,16 +538,16 @@ INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *sa
        cert->issue_year = context->certDesc->info.validPeriod.firstYear;
        cert->issue_year = context->certDesc->info.validPeriod.firstYear;
        cert->issue_month = context->certDesc->info.validPeriod.firstMonth;
-       cert->issue_day = context->certDesc->info.validPeriod.firstDay;         
-       cert->expiration_year= context->certDesc->info.validPeriod.secondYear;          
-       cert->expiration_month = context->certDesc->info.validPeriod.secondMonth;               
-       cert->expiration_day = context->certDesc->info.validPeriod.secondDay;                           
+       cert->issue_day = context->certDesc->info.validPeriod.firstDay;
+       cert->expiration_year= context->certDesc->info.validPeriod.secondYear;
+       cert->expiration_month = context->certDesc->info.validPeriod.secondMonth;
+       cert->expiration_day = context->certDesc->info.validPeriod.secondDay;
        cert->issue_organization_name = EM_SAFE_STRDUP(context->certDesc->info.issuer.organizationName);
        cert->email_address = EM_SAFE_STRDUP(temp_save_name);
-       cert->subject_str = EM_SAFE_STRDUP(context->certDesc->info.issuerStr);          
-       cert->filepath = EM_SAFE_STRDUP(filepath);                              
+       cert->subject_str = EM_SAFE_STRDUP(context->certDesc->info.issuerStr);
+       cert->filepath = EM_SAFE_STRDUP(filepath);
 
-       if (emstorage_add_certificate(cert, true, &err)) {
+       if (emstorage_add_certificate(multi_user_name, cert, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_add_certificate failed");
                goto FINISH_OFF;
        }
@@ -467,7 +562,7 @@ INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *sa
 FINISH_OFF:
 
        emstorage_delete_file(temp_file, NULL);
-       
+
        emstorage_free_certificate(&cert, 1, NULL);
 
        cert_svc_cert_context_final(context);
@@ -482,7 +577,7 @@ FINISH_OFF:
 
 }
 
-INTERNAL_FUNC int emcore_delete_public_certificate(char *email_address, int *err_code)
+INTERNAL_FUNC int emcore_delete_public_certificate(char *multi_user_name, char *email_address, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = false;
@@ -495,7 +590,7 @@ INTERNAL_FUNC int emcore_delete_public_certificate(char *email_address, int *err
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_certificate_by_email_address(email_address, &certificate, false, 0, &err)) {
+       if (!emstorage_get_certificate_by_email_address(multi_user_name, email_address, &certificate, false, 0, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_certificate failed");
                goto FINISH_OFF;
        }
@@ -505,7 +600,7 @@ INTERNAL_FUNC int emcore_delete_public_certificate(char *email_address, int *err
                goto FINISH_OFF;
        }
 
-       if (!emstorage_delete_certificate(certificate->certificate_id, true, &err)) {
+       if (!emstorage_delete_certificate(multi_user_name, certificate->certificate_id, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_delete_certificate failed");
                goto FINISH_OFF;
        }
@@ -538,9 +633,6 @@ INTERNAL_FUNC int emcore_verify_signature(char *p7s_file_path, char *mime_entity
 
        PKCS7 *pkcs7_p7s = NULL;
 
-       /* Initialize */
-       OpenSSL_add_all_algorithms();
-
        /* Open p7s file */
        bio_p7s = BIO_new_file(p7s_file_path, INMODE);
        if (!bio_p7s) {
@@ -567,7 +659,6 @@ INTERNAL_FUNC int emcore_verify_signature(char *p7s_file_path, char *mime_entity
 
        if (!PKCS7_verify(pkcs7_p7s, NULL, NULL, bio_indata, NULL, flags)) {
                EM_DEBUG_EXCEPTION("PKCS7_verify failed");
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
@@ -577,8 +668,6 @@ INTERNAL_FUNC int emcore_verify_signature(char *p7s_file_path, char *mime_entity
 
 FINISH_OFF:
 
-       EVP_cleanup();
-
        if (pkcs7_p7s)
                PKCS7_free(pkcs7_p7s);
 
@@ -606,7 +695,7 @@ INTERNAL_FUNC int emcore_verify_certificate(char *certificate, int *validity, in
        int p_validity = 0;
 
        CERT_CONTEXT *context = NULL;
-       
+
        context = cert_svc_cert_context_init();
 
        err = cert_svc_load_file_to_context(context, certificate);
@@ -625,7 +714,7 @@ INTERNAL_FUNC int emcore_verify_certificate(char *certificate, int *validity, in
 
 FINISH_OFF:
 
-       if (validity != NULL) 
+       if (validity != NULL)
                *validity = p_validity;
 
        if (err_code != NULL) {
@@ -641,17 +730,17 @@ FINISH_OFF:
 INTERNAL_FUNC int emcore_free_certificate(email_certificate_t **certificate, int count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("certificate [%p], count [%d]", certificate, count);
-       
+
        if (count <= 0 || !certificate || !*certificate) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               if (err_code)   
+               if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
 
        email_certificate_t *p_certificate = *certificate;
        int i;
-       
+
        for (i=0;i<count;i++) {
                EM_SAFE_FREE(p_certificate[i].issue_organization_name);
                EM_SAFE_FREE(p_certificate[i].email_address);
diff --git a/email-core/email-core-container.c b/email-core/email-core-container.c
new file mode 100644 (file)
index 0000000..1f4341a
--- /dev/null
@@ -0,0 +1,405 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+/**
+ *
+ * This file contains functionality related to KNOX
+ * to interact with email-service.
+ * @file               email-core-container.c
+ * @author     
+ * @version    0.1
+ * @brief              This file contains functionality to provide KNOX support in email-service. 
+ */
+
+#include <email-utilities.h>
+
+#include "email-core-container.h"
+#include "email-debug-log.h"
+
+#ifdef _FEATURE_CONTAINER_ENABLE__
+int vsm_state_handle = 0;
+int vsm_event_handle = 0;
+vsm_context_h container;
+
+
+static int container_state_listener(vsm_zone_h zone, vsm_zone_state_t state, void *user_data)
+{
+    EM_DEBUG_FUNC_BEGIN();
+    EM_DEBUG_LOG("Container name : [%s]", vsm_get_zone_name(zone));
+    EM_DEBUG_LOG("Container state : [%d]", state);
+    EM_DEBUG_FUNC_END();
+    return 0;
+}
+
+static int container_event_listener(vsm_zone_h zone, vsm_zone_event_t event, void *user_data)
+{
+    EM_DEBUG_FUNC_BEGIN();
+    EM_DEBUG_LOG("Container name : [%s]", vsm_get_zone_name(zone));
+    EM_DEBUG_LOG("Container event : [%d]", event);
+    EM_DEBUG_FUNC_END();
+    return 0;
+}
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+INTERNAL_FUNC void emcore_create_container()
+{
+    EM_DEBUG_FUNC_BEGIN();
+#ifdef __FEATURE_CONTAINER_ENABLE__
+    container = NULL;
+
+    container = vsm_create_context();
+    if (container == NULL) {
+        EM_DEBUG_EXCEPTION("vsm_create_context failed");
+        return;
+    }
+
+    vsm_state_handle = vsm_add_state_changed_callback(container, container_state_listener, NULL);
+
+       vsm_event_handle = vsm_add_event_callback(container, container_event_listener, NULL);
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+    EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_destroy_container()
+{
+    EM_DEBUG_FUNC_BEGIN();
+#ifdef __FEATURE_CONTAINER_ENABLE__
+
+    if (container) {
+        vsm_del_state_changed_callback(container, vsm_state_handle);
+
+               vsm_del_event_callback(container, vsm_event_handle);
+
+        vsm_cleanup_context(container);
+
+        container = NULL;
+    }
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+    EM_DEBUG_FUNC_END();
+}
+
+static gboolean mainloop_callback(GIOChannel *channel, GIOCondition condition, void *data) 
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+    vsm_context_h ctx = (vsm_context_h)data;
+
+    EM_DEBUG_LOG("Enter event loop");
+    vsm_enter_eventloop(ctx, 0, 0);
+    EM_DEBUG_LOG("Finish event loop");
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+    EM_DEBUG_FUNC_END();
+    return TRUE;
+}
+
+INTERNAL_FUNC void emcore_bind_vsm_context()
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+       int fd;
+
+       GIOChannel *domain_channel = NULL;
+
+       fd = vsm_get_poll_fd(container);
+
+       domain_channel = g_io_channel_unix_new(fd);
+       if (domain_channel == NULL) {
+               EM_DEBUG_EXCEPTION("Channel allocation failed\n");
+               return;
+       }
+
+       g_io_add_watch(domain_channel, G_IO_IN, mainloop_callback, container);
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emcore_get_container_path(char *multi_user, char **container_path)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+
+       if (multi_user == NULL) {
+               EM_DEBUG_LOG("user_name is NULL");
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+       if (container == NULL) {
+               EM_DEBUG_LOG("Container not intialize");
+               return EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION;
+       }
+
+       vsm_zone_h zone = NULL;
+       char *p_container_path = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       EM_DEBUG_LOG("name : [%s], container : [%p]", multi_user, container);
+       zone = vsm_lookup_zone_by_name(container, multi_user);
+       if (zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
+               err = EMAIL_ERROR_CONTAINER_GET_DOMAIN;
+               goto FINISH_OFF;
+       }
+
+       p_container_path = (char *)vsm_get_zone_rootpath(zone);
+
+FINISH_OFF:
+
+       if (container_path)
+               *container_path = EM_SAFE_STRDUP(p_container_path);
+
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC int emcore_get_user_name(int pid, char **multi_user)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int err = EMAIL_ERROR_NONE;
+
+       if (pid <= 0) {
+                       EM_DEBUG_EXCEPTION("Invalid parameter");
+                       return EMAIL_ERROR_INVALID_PARAM;
+       }
+#ifdef __FEATURE_CONTAINER_ENABLE__
+
+       char *user_name = NULL;
+       vsm_zone_h zone = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       EM_DEBUG_LOG("pid : [%d], container : [%p]", pid, container);
+       zone = vsm_lookup_zone_by_pid(container, pid);
+       if (zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
+               err = EMAIL_ERROR_CONTAINER_GET_DOMAIN;
+               goto FINISH_OFF;
+       }
+
+       user_name = (char *)vsm_get_zone_name(zone);
+       EM_DEBUG_LOG("user_name : [%s]", user_name);
+
+FINISH_OFF:
+
+       if (multi_user)
+               *multi_user = EM_SAFE_STRDUP(user_name);
+
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC int emcore_lookup_zone_by_name(char *user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int ret = false;
+
+       if (EM_SAFE_STRLEN(user_name) == 0) {
+               EM_DEBUG_LOG("user_name is NULL");
+               return ret;
+       }
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+       if (container == NULL) {
+               EM_DEBUG_LOG("Container not intialize");
+               return ret;
+       }
+
+       vsm_zone_h zone = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       EM_DEBUG_LOG("name : [%s], container : [%p]", user_name, container);
+       zone = vsm_lookup_zone_by_name(container, user_name);
+       if (zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s, %d", EM_STRERROR(errno_buf), errno);
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+INTERNAL_FUNC void emcore_set_declare_link(const char *file_path)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+    if (container == NULL) {
+        EM_DEBUG_EXCEPTION("Not initialize");
+        return;
+    }
+
+    int ret = 0;
+    if ((ret = vsm_declare_link(container, file_path /* src path */, file_path /* dst path */)) != 0) {
+        EM_DEBUG_EXCEPTION("vsm_declare_link failed : [%s] [%d]", file_path, ret);
+    }
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+    EM_DEBUG_FUNC_END();
+}
+
+void iterate_callback(vsm_zone_h zone, void *user_data)
+{
+    EM_DEBUG_FUNC_BEGIN();
+#ifdef __FEATURE_CONTAINER_ENABLE__
+    char *zone_name = NULL;
+    GList *zone_name_list = (GList *)user_data;
+
+    zone_name = EM_SAFE_STRDUP(vsm_get_zone_name(zone));
+    zone_name_list = g_list_append(zone_name_list, zone_name);
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+    
+    EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emcore_get_zone_name_list(GList **output_name_list)
+{
+    EM_DEBUG_FUNC_BEGIN();
+    int err = EMAIL_ERROR_NONE;
+    int ret = 0;
+    GList *zone_name_list = NULL;
+    GList *node = NULL;
+
+    if (output_name_list == NULL) {
+        EM_DEBUG_EXCEPTION("Invalid parameter");
+        err = EMAIL_ERROR_INVALID_PARAM;
+        goto FINISH_OFF;
+    }
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+    if (container == NULL) {
+        EM_DEBUG_EXCEPTION("Not initialize");
+        err = EMAIL_ERROR_CONTAINER_CREATE_FAILED;
+        goto FINISH_OFF;
+    }
+
+    zone_name_list = g_list_alloc();
+    if (zone_name_list == NULL) {
+        EM_DEBUG_EXCEPTION("g_list_alloc failed");
+        err = EMAIL_ERROR_OUT_OF_MEMORY;
+        goto FINISH_OFF;
+    }
+
+    if ((ret = vsm_iterate_zone(container, iterate_callback, (void *)zone_name_list)) < 0) {
+        EM_DEBUG_EXCEPTION("vsm_iterate_domain failed : [%d]", ret);
+        err = EMAIL_ERROR_CONTAINER_ITERATE_DOMAIN;
+        goto FINISH_OFF;
+    }
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+FINISH_OFF:
+
+    if (err != EMAIL_ERROR_NONE) {
+        node = g_list_first(zone_name_list);
+        while (node != NULL) {
+            /* Free the domain name */
+            EM_SAFE_FREE(node->data);                   
+            node = g_list_next(node);
+        }
+        g_list_free(zone_name_list);
+        zone_name_list = NULL;
+    }
+
+    if (output_name_list)
+        *output_name_list = zone_name_list;
+
+    EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC int emcore_get_canonicalize_path(char *db_path, char **output_path)
+{
+    EM_DEBUG_FUNC_BEGIN();
+    int err = EMAIL_ERROR_NONE;
+
+#ifdef __FEATURE_CONTAINER_ENABLE__
+       int ret = 0;
+       ret = vsm_canonicalize_path(db_path, output_path);
+       if (ret < 0) {
+               EM_DEBUG_EXCEPTION("vsm_canonicalize_path failed : [%d]", ret);
+               err = EMAIL_ERROR_FILE_NOT_FOUND;
+       }
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+    EM_DEBUG_FUNC_END();
+    return err;
+}
+
+INTERNAL_FUNC int emcore_set_join_zone(char *multi_user_name, vsm_zone_h *join_zone)
+{
+    EM_DEBUG_FUNC_BEGIN();
+    int err = EMAIL_ERROR_NONE;
+#ifdef __FEATURE_CONTAINER_ENABLE__
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+       vsm_zone_h temp_zone = NULL;
+       vsm_zone_h p_join_zone = NULL;
+
+       temp_zone = vsm_lookup_zone_by_name(container, multi_user_name);
+       if (temp_zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
+               err = EMAIL_ERROR_CONTAINER_LOOKUP_ZONE_FAILED;
+               goto FINISH_OFF;
+       }
+
+       p_join_zone = vsm_join_zone(temp_zone);
+       if (p_join_zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
+               err = EMAIL_ERROR_CONTAINER_JOIN_ZONE_FAILED;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       if (join_zone)
+               *join_zone = p_join_zone;
+
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+    EM_DEBUG_FUNC_END();
+    return err;
+}
+
+INTERNAL_FUNC void emcore_unset_join_zone(vsm_zone_h join_zone)
+{
+    EM_DEBUG_FUNC_BEGIN();
+#ifdef __FEATURE_CONTAINER_ENABLE__
+
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+       vsm_zone_h temp_zone = NULL;
+
+       temp_zone = vsm_join_zone(join_zone);
+       if (temp_zone == NULL) {
+               EM_DEBUG_EXCEPTION("NULL returned : %s %d", EM_STRERROR(errno_buf), errno);
+       }
+
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+    EM_DEBUG_FUNC_END();
+}
index 39659aa..6dfdd15 100755 (executable)
@@ -25,7 +25,8 @@
 #include <pthread.h>
 #include <vconf.h>
 #include <signal.h>
-//#include <contacts.h>
+#include <contacts.h>
+#include <contacts_internal.h>
 #include "c-client.h"
 #include "email-convert.h"
 #include "email-storage.h"
@@ -43,7 +44,6 @@
 #include "email-core-mailbox-sync.h"
 #include "email-core-smtp.h"
 #include "email-core-utils.h"
-#include "email-core-sound.h"
 #include "email-core-signal.h"
 #include "email-debug-log.h"
 
@@ -56,7 +56,7 @@ INTERNAL_FUNC pthread_cond_t  _event_available_signal = PTHREAD_COND_INITIALIZER
 INTERNAL_FUNC pthread_mutex_t *_event_queue_lock = NULL;
 INTERNAL_FUNC pthread_mutex_t *_event_handle_map_lock = NULL;
 
-INTERNAL_FUNC GQueue *g_event_que;
+INTERNAL_FUNC GQueue *g_event_que = NULL;
 INTERNAL_FUNC int g_event_loop = 1;
 INTERNAL_FUNC int handle_map[EVENT_QUEUE_MAX] = {0,};
 INTERNAL_FUNC int recv_thread_run = 0;
@@ -73,7 +73,7 @@ INTERNAL_FUNC pthread_cond_t  _send_event_available_signal = PTHREAD_COND_INITIA
 INTERNAL_FUNC pthread_mutex_t *_send_event_queue_lock = NULL;
 INTERNAL_FUNC pthread_mutex_t *_send_event_handle_map_lock = NULL;
 
-INTERNAL_FUNC GQueue *g_send_event_que;
+INTERNAL_FUNC GQueue *g_send_event_que = NULL;
 INTERNAL_FUNC int g_send_event_loop = 1;
 INTERNAL_FUNC int send_handle_map[EVENT_QUEUE_MAX] = {0,};
 INTERNAL_FUNC int send_thread_run = 0;
@@ -93,7 +93,7 @@ INTERNAL_FUNC pthread_mutex_t _state_variables_lock;
 
 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
 INTERNAL_FUNC int g_partial_body_thd_next_event_idx = 0;                       /* Index of Next Event to be processed in the queue*/
-INTERNAL_FUNC int g_partial_body_thd_loop = 1;                                         /* Variable to make a continuos while loop */
+//INTERNAL_FUNC int g_partial_body_thd_loop = 1;                                               /* Variable to make a continuos while loop */
 INTERNAL_FUNC int g_partial_body_thd_queue_empty = true;                       /* Variable to determine if event queue is empty.True means empty*/
 INTERNAL_FUNC int g_partial_body_thd_queue_full = false;                       /* Variable to determine if event queue is full. True means full*/
 INTERNAL_FUNC int g_pb_thd_local_activity_continue = true;                     /* Variable to control local activity sync */
@@ -106,6 +106,8 @@ INTERNAL_FUNC int g_partial_body_bulk_dwd_queue_empty = true;
 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
 static int emcore_clear_bulk_pbd_que(int *err_code);
 
+INTERNAL_FUNC email_event_t *sync_failed_event_data = NULL;
+
 #endif
 
 /*-----------------------------------------------------------------------------
@@ -158,72 +160,67 @@ static void fail_status_notify(email_event_t *event_data, int error)
                case EMAIL_EVENT_SEND_MAIL:
                        /* case EMAIL_EVENT_SEND_MAIL_SAVED:  */
                        /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
-                       emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, error);
+                       emcore_show_user_message(event_data->multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, error);
                        break;
 
                case EMAIL_EVENT_SYNC_HEADER:
                        emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_SYNC_HEADER, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SYNC_HEADER, error);
                        break;
 
                case EMAIL_EVENT_DOWNLOAD_BODY:
                        emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
                        break;
 
                case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
                        emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
                        break;
 
                case EMAIL_EVENT_DELETE_MAIL:
                case EMAIL_EVENT_DELETE_MAIL_ALL:
                        emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, error);
                        break;
 
                case EMAIL_EVENT_VALIDATE_ACCOUNT:
                        emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
                        break;
 
                case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
                        emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
                        break;
 
                case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
                        emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
                        break;
 
                case EMAIL_EVENT_CREATE_MAILBOX:
                        emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
                        break;
 
                case EMAIL_EVENT_DELETE_MAILBOX:
                        emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
                        break;
 
                case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
                        emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
                        break;
 
                case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
                        emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
                        break;
 
-               case EMAIL_EVENT_SEARCH_ON_SERVER:
-                       emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
-                       break;
-
                case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                        emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
-                       emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
+                       emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
                        break;
 
                case EMAIL_EVENT_UPDATE_MAIL:
@@ -403,6 +400,12 @@ INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, in
                        WAKE_CONDITION_VARIABLE(_event_available_signal);
                        ret = true;
                }
+               else {
+                       EM_DEBUG_EXCEPTION("event queue is full...");
+                       error = EMAIL_ERROR_EVENT_QUEUE_FULL;
+                       ret = false;
+               }
+
        }
 
        LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
@@ -427,22 +430,25 @@ INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, in
                case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
                case EMAIL_EVENT_UPDATE_MAIL:
                case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
-               case EMAIL_EVENT_SEARCH_ON_SERVER:
                case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                        break;
 
                default:
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
-       {
-               int is_local_activity_event_inserted = false;
-
-               if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &error))
-                       EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
-               else {
-                       if (true == is_local_activity_event_inserted)
-                               emcore_pb_thd_set_local_activity_continue(false);
+               {
+                       int is_local_activity_event_inserted = false;
+                       emcore_partial_body_thd_local_activity_sync (
+                                                 event_data->multi_user_name, 
+                                                 &is_local_activity_event_inserted, 
+                                                 &error);
+                       if (error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
+                       }
+                       else {
+                               if (true == is_local_activity_event_inserted)
+                                       emcore_pb_thd_set_local_activity_continue (false);
+                       }
                }
-       }
 #endif
                break;
        }
@@ -519,6 +525,74 @@ INTERNAL_FUNC int emcore_event_loop_continue(void)
        return g_event_loop;
 }
 
+INTERNAL_FUNC int emcore_is_event_queue_empty(void)
+{
+       int q_length = 0;
+       int ret = false;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
+       if (g_event_que)
+               q_length = g_queue_get_length(g_event_que);
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+       if (q_length > 0) {
+               ret = false;
+       }
+       else {
+               EM_DEBUG_LOG("event que is empty");
+               ret = true;
+       }
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
+
+       return ret;
+}
+
+
+INTERNAL_FUNC int emcore_is_send_event_queue_empty(void)
+{
+       int q_length = 0;
+       int ret = false;
+
+       ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
+       if (g_send_event_que)
+               q_length = g_queue_get_length(g_send_event_que);
+       EM_DEBUG_LOG("Q Length : [%d]", q_length);
+       if (q_length > 0) {
+               ret = false;
+       }
+       else {
+               EM_DEBUG_LOG("send event que is empty");
+               ret = true;
+       }
+       LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
+
+       return ret;
+}
+
+
+INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data) 
+{
+       if (!sync_failed_event_data) {
+               EM_DEBUG_EXCEPTION("sync_failed_event_data is NULL");
+               return;
+       }
+
+       email_event_t *new_event = NULL;
+
+       new_event = em_malloc(sizeof(email_event_t));
+       new_event->account_id = sync_failed_event_data->account_id;
+       new_event->type = sync_failed_event_data->type;
+       if (sync_failed_event_data->event_param_data_3)
+               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;
+
+       *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)
 {
        EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
@@ -588,8 +662,8 @@ INTERNAL_FUNC int emcore_retrieve_send_event(email_event_t **event_data, int *er
        email_event_t *poped = NULL;
        email_event_t *head_event = NULL;
 
-       if (g_send_event_que);
-               q_length = g_queue_get_length(g_send_event_que);
+       /* g_queue_get_length is aborted when param is null */
+       q_length = g_send_event_que? g_queue_get_length (g_send_event_que): 0; /*prevent 35141*/
 
        EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
 
@@ -713,10 +787,34 @@ INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
        return true;
 }
 
-/* check thread status
+/* check event thread status (worker_event_queue)
 * 0 : stop job 1 : continue job
+event list handled by event thread :
+case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
+case EMAIL_EVENT_SYNC_HEADER:
+case EMAIL_EVENT_SYNC_HEADER_OMA:
+case EMAIL_EVENT_DOWNLOAD_BODY:
+case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
+case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
+case EMAIL_EVENT_DELETE_MAIL:
+case EMAIL_EVENT_DELETE_MAIL_ALL:
+case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
+case EMAIL_EVENT_CREATE_MAILBOX:
+case EMAIL_EVENT_DELETE_MAILBOX:
+case EMAIL_EVENT_SAVE_MAIL:
+case EMAIL_EVENT_MOVE_MAIL:
+case EMAIL_EVENT_VALIDATE_ACCOUNT:
+case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
+case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
+case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
+case EMAIL_EVENT_UPDATE_MAIL:
+case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
+case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
+case EMAIL_EVENT_LOCAL_ACTIVITY:
+case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
+case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
 */
-INTERNAL_FUNC int emcore_check_thread_status()
+INTERNAL_FUNC int emcore_check_event_thread_status(int *event_type, int handle)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -732,10 +830,16 @@ INTERNAL_FUNC int emcore_check_thread_status()
        if (q_length) {
                active_event = (email_event_t *)g_queue_peek_head(g_event_que);
                if (active_event) {
-                       if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
+                       if (active_event->handle == handle) {
+                               if (event_type)
+                                       *event_type = active_event->type;
+                               if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
+                                       ret = true;
+                               else
+                                       ret = false;
+                       } else {
                                ret = true;
-                       else
-                               ret = false;
+                       }
                }
        } else {
                EM_DEBUG_LOG("Rcv Queue is empty [%d]", q_length);
@@ -820,7 +924,6 @@ INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
                                case EMAIL_EVENT_CREATE_MAILBOX:
                                case EMAIL_EVENT_DELETE_MAILBOX:
                                case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
-                               case EMAIL_EVENT_SEARCH_ON_SERVER:
                                case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                                        EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
                                        break;
@@ -837,14 +940,15 @@ INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
                        } else {
                                EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
                        }
-               } else {
+               }
+               else {
                        switch (found_elm->type) {
 
                                case EMAIL_EVENT_SYNC_HEADER:
                                        EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER");
                                        if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
                                                EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
-                                       if ((err = emcore_update_sync_status_of_account(found_elm->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+                                       if ((err = emcore_update_sync_status_of_account(found_elm->multi_user_name, found_elm->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                                                EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
                                        break;
 
@@ -884,8 +988,8 @@ INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
 
        ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
 
-       if (g_event_que);
-               q_length = g_queue_get_length(g_event_que);
+       /* g_queue_get_length is aborted when param is null */
+       q_length = g_event_que? g_queue_get_length (g_event_que): 0; /*prevent 35142 */
 
        for (i = 0; i < q_length; i++) {
                pop_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
@@ -931,7 +1035,6 @@ INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
                                case EMAIL_EVENT_CREATE_MAILBOX:
                                case EMAIL_EVENT_DELETE_MAILBOX:
                                case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
-                               case EMAIL_EVENT_SEARCH_ON_SERVER:
                                case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                                        EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
                                        break;
@@ -950,7 +1053,8 @@ INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
                        if (g_event_que)
                                q_length = g_queue_get_length(g_event_que);
 
-               } else {
+               }
+               else {
                        pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
                }
        }
@@ -998,7 +1102,7 @@ INTERNAL_FUNC int emcore_check_send_mail_thread_status(void)
        return ret;
 }
 
-INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
+INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(char *multi_user_name, int account_id)
 {
        EM_DEBUG_FUNC_BEGIN();
        int error_code = EMAIL_ERROR_NONE;
@@ -1014,7 +1118,8 @@ INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
 
        for (i = 0; i < q_length; i++) {
                found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
-               if (found_elm->account_id == account_id || found_elm->account_id == ALL_ACCOUNT) {
+               if ((found_elm->account_id == account_id || found_elm->account_id == ALL_ACCOUNT) 
+                && (!EM_SAFE_STRCASECMP(found_elm->multi_user_name, multi_user_name) || (!found_elm && !multi_user_name))) {
                        EM_DEBUG_LOG("Found Queue element[%d]", i);
 
                        if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
@@ -1057,7 +1162,6 @@ INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
                                        case EMAIL_EVENT_CREATE_MAILBOX:
                                        case EMAIL_EVENT_DELETE_MAILBOX:
                                        case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
-                                       case EMAIL_EVENT_SEARCH_ON_SERVER:
                                        case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                                                EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
                                                break;
@@ -1078,7 +1182,8 @@ INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
                                if (g_event_que)
                                        q_length = g_queue_get_length(g_event_que);
 
-                       } else {
+                       }
+                       else {
                                found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
                        }
                }
@@ -1360,10 +1465,6 @@ INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
                        break;
 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
 
-               case EMAIL_EVENT_SEARCH_ON_SERVER:
-                       EM_SAFE_FREE(event_data->event_param_data_1);
-                       break;
-
                case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
                        EM_SAFE_FREE(event_data->event_param_data_1);
                        EM_SAFE_FREE(event_data->event_param_data_2);
@@ -1390,6 +1491,7 @@ INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
                        EM_DEBUG_EXCEPTION("event %d is NOT freed, possibly memory leaks", event_data->type);   
        }
 
+       EM_SAFE_FREE(event_data->multi_user_name);
        event_data->event_param_data_1 = event_data->event_param_data_2 = event_data->event_param_data_3 = NULL;
 
        EM_DEBUG_FUNC_END();
@@ -1678,8 +1780,8 @@ static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src,
        dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
        dest->activity_type = src->activity_type;
        dest->event_type = src->event_type;
-
-       EM_DEBUG_LOG("dest->account_id[%d], dest->mail_id[%d], dest->server_mail_id [%lu]", dest->account_id, dest->mail_id , dest->server_mail_id);
+       dest->multi_user_name = EM_SAFE_STRDUP(src->multi_user_name);
+       
 
        ret = true;
 
@@ -1718,12 +1820,15 @@ INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_th
 
        /*Free character pointers in event_data cell */
        EM_SAFE_FREE(pbd_event->mailbox_name);
+       EM_SAFE_FREE(pbd_event->multi_user_name);
        memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
        EM_DEBUG_FUNC_END();
        return true;
 }
 
-INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
+INTERNAL_FUNC int emcore_insert_partial_body_thread_event (
+                              email_event_partial_body_thd *partial_body_thd_event, 
+                              int                          *error_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1745,49 +1850,40 @@ INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_bo
        ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
 
        /* find a cell in queue which is empty */
-
-       for (count = 0, index =  g_partial_body_thd_next_event_idx; count < TOTAL_PARTIAL_BODY_EVENTS;) {
-               if  (g_partial_body_thd_event_que[index].event_type) {
-                       ++index;
-                       ++count;
-
-                       if (index == TOTAL_PARTIAL_BODY_EVENTS) {
-                               index = 0;
-                       }
-               }
-               else {
-                       /*Found empty Cell*/
-
-                       empty_cell_index =       index;
+       index = g_partial_body_thd_next_event_idx;
+       for (count = 0; count < TOTAL_PARTIAL_BODY_EVENTS; count++) {
+               /*Found empty Cell*/
+               if (g_partial_body_thd_event_que[index].event_type == EMAIL_EVENT_NONE) {               
+                       empty_cell_index = index;
                        break;
                }
+               index++;
+               index = index % TOTAL_PARTIAL_BODY_EVENTS;
        }
 
-       if (-1 != empty_cell_index) {
-               if (false == emcore_copy_partial_body_thd_event(partial_body_thd_event, g_partial_body_thd_event_que+empty_cell_index , &error)) {
+       if (empty_cell_index != -1) {
+               emcore_copy_partial_body_thd_event (partial_body_thd_event, 
+                                 g_partial_body_thd_event_que + empty_cell_index, 
+                                 &error);
+               if (error != EMAIL_ERROR_NONE) {
                        EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
                }
                else {
                        g_partial_body_thd_queue_empty = false;
-
                        if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
                                /*This is the last event_data inserted in queue after its insertion, queue is full */
                                g_partial_body_thd_queue_full = true;
-
                        }
-
                        WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
-
                        ret = true;
                }
        }
        else {
-               EM_DEBUG_LOG(" partial body thread event_data queue is full ");
+               EM_DEBUG_LOG("partial body thread event_data queue is full ");
                error = EMAIL_ERROR_EVENT_QUEUE_FULL;
 
                g_partial_body_thd_queue_full = true;
                g_partial_body_thd_queue_empty = false;
-
        }
 
        LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
@@ -1860,7 +1956,7 @@ INTERNAL_FUNC int emcore_retrieve_partial_body_thread_event(email_event_partial_
 }
 
 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
-static int emcore_partial_body_bulk_flush(int *error_code)
+static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int error = EMAIL_ERROR_NONE;
@@ -1868,7 +1964,7 @@ static int emcore_partial_body_bulk_flush(int *error_code)
        MAILSTREAM *stream = NULL;
        void *tmp_stream = NULL;
 
-       if (!emcore_connect_to_remote_mailbox(g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -1929,7 +2025,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                        EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
                        EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
                        if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
-                               if (false == emcore_partial_body_bulk_flush(&error)) {
+                               if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
                                        EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
                                        goto FINISH_OFF;
                                }
@@ -1940,7 +2036,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                        EM_DEBUG_LOG("Flush the current que if not empty");
                        EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
                        if (!g_partial_body_bulk_dwd_queue_empty) {
-                               if (false == emcore_partial_body_bulk_flush(&error)) {
+                               if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
                                        EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
                                        goto FINISH_OFF;
                                }
@@ -1962,7 +2058,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
 
                EM_DEBUG_LOG("Event is coming from local activity.");
                /* Get all the accounts for which local activities are pending */
-               if (false == emstorage_get_pbd_account_list(&account_list, &account_count, false, &error)) {
+               if (false == emstorage_get_pbd_account_list(pbd_event->multi_user_name, &account_list, &account_count, false, &error)) {
                                EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
                                error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
                                goto FINISH_OFF;
@@ -1970,7 +2066,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
 
                for (m = 0; m < account_count; ++m) {
                        /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
-                       if (false == emstorage_get_pbd_mailbox_list(account_list[m], &mailbox_list, &count, false, &error)) {
+                       if (false == emstorage_get_pbd_mailbox_list(pbd_event->multi_user_name, account_list[m], &mailbox_list, &count, false, &error)) {
                                        EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
                                        error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
                                        goto FINISH_OFF;
@@ -1980,17 +2076,17 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                                int k = 0;
                                int activity_count = 0;
 
-                               if (!emcore_connect_to_remote_mailbox(account_list[m], mailbox_list[i], (void **)&stream, &error)) {
+                               if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name, account_list[m], mailbox_list[i], (void **)&stream, &error)) {
                                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
                                        stream = mail_close (stream);
                                        goto FINISH_OFF;
                                }
 
-                               if (false == emstorage_get_pbd_activity_data(account_list[m], mailbox_list[i], &activity_data_list, &activity_count,  false, &error))
+                               if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[m], mailbox_list[i], &activity_data_list, &activity_count,  false, &error))
                                        EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
 
                                /* it is duplicated with emstorage_get_pbd_activity_data
-                               if (false == emstorage_get_mailbox_pbd_activity_count(account_list[m], mailbox_list[i], &activity_count, false, &error)) {
+                               if (false == emstorage_get_mailbox_pbd_activity_count(pbd_evnet->multi_user_name, account_list[m], mailbox_list[i], &activity_count, false, &error)) {
                                        EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
                                        continue;
                                }
@@ -2004,7 +2100,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                                        int index = 0;
 
 /*
-                                       if (false == emstorage_get_pbd_activity_data(account_list[j], mailbox_list[i], &activity_data_list, &num_activity,  false, &error))
+                                       if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[j], mailbox_list[i], &activity_data_list, &num_activity,  false, &error))
                                                EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
 
                                        if (NULL == activity_data_list) {
@@ -2035,6 +2131,7 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
 
                                        for (k = 0; k < activity_count; k++)
                                                emcore_free_partial_body_thd_event(activity_data_list + k, &error);
+
                                        EM_SAFE_FREE(activity_data_list);
 
                                        /*check: empty check required?*/
@@ -2043,8 +2140,8 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                                                goto FINISH_OFF;                /* Stop Local Activity Sync */
                                        }
                                }
+                               stream = mail_close (stream);
                        }
-                       stream = mail_close (stream);
                }
 
                /* After completing one cycle of local activity sync ,
@@ -2059,7 +2156,8 @@ INTERNAL_FUNC int emcore_mail_partial_body_download (email_event_partial_body_th
                        ret = true;
                        goto FINISH_OFF;                /* Stop Local Activity Sync */
                }
-               if (false == emcore_partial_body_bulk_flush(&error)) {
+
+               if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
                        EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
                        goto FINISH_OFF;
                }
diff --git a/email-core/email-core-gmime.c b/email-core/email-core-gmime.c
new file mode 100755 (executable)
index 0000000..2925481
--- /dev/null
@@ -0,0 +1,4744 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Minsoo Kim <minnsoo.kim@samsung.com>, Kyuho Jo <kyuho.jo@samsung.com>,
+* Sunghyun Kwon <sh0701.kwon@samsung.com>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+#include <glib.h>
+#include <gmime/gmime.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <vconf.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "email-internal-types.h"
+#include "email-utilities.h"
+#include "email-core-global.h"
+#include "email-core-utils.h"
+#include "email-core-mail.h"
+#include "email-core-mime.h"
+#include "email-core-gmime.h"
+#include "email-storage.h"
+#include "email-core-event.h"
+#include "email-core-account.h"
+#include "email-core-signal.h"
+#include "email-core-mailbox-sync.h"
+#include "email-debug-log.h"
+
+static int multipart_status = 0;
+
+static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+static int emcore_gmime_parse_mime_header(GMimeMessage *message, struct _rfc822header *rfc822_header);
+
+
+INTERNAL_FUNC void emcore_gmime_init(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       g_mime_init(0);
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_shutdown(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       g_mime_shutdown();
+
+       EM_DEBUG_FUNC_END();
+}
+
+
+INTERNAL_FUNC int emcore_gmime_imap_parse_mime_partial(char *rfc822header_str, char *bodytext_str, struct _m_content_info *cnt_info)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       EM_DEBUG_LOG_DEV("RFC822H:%s", rfc822header_str);
+       EM_DEBUG_LOG_DEV("BODYTEXT:%s", bodytext_str);
+
+       GMimeStream *stream = NULL;
+       GMimeMessage *message = NULL;
+       GMimeParser *parser = NULL;
+       char *fulltext = NULL;
+
+       fulltext = g_strconcat(rfc822header_str, "\r\n\r\n", bodytext_str, NULL);
+
+       stream = g_mime_stream_mem_new_with_buffer(fulltext, EM_SAFE_STRLEN(fulltext));
+
+       parser = g_mime_parser_new_with_stream(stream);
+       if (stream) g_object_unref(stream);
+
+       message = g_mime_parser_construct_message(parser);
+       if (parser) g_object_unref(parser);
+       if (!message) { /* prevent null check for message */
+               EM_DEBUG_EXCEPTION("g_mime_parser_construct_message error");
+               EM_SAFE_FREE(fulltext);
+               return false;
+       }
+
+       EM_DEBUG_LOG_DEV("Sender:%s", g_mime_message_get_sender(message));
+       EM_DEBUG_LOG_DEV("Reply To:%s", g_mime_message_get_reply_to(message));
+       EM_DEBUG_LOG_DEV("Subject:%s", g_mime_message_get_subject(message));
+       EM_DEBUG_LOG_DEV("Date:%s", g_mime_message_get_date_as_string(message));
+       EM_DEBUG_LOG_DEV("Message ID:%s", g_mime_message_get_message_id(message));
+
+       g_mime_message_foreach(message, emcore_gmime_imap_parse_foreach_cb, (gpointer)cnt_info);
+
+       EM_SAFE_FREE(fulltext);
+
+       if (message) g_object_unref (message);
+
+       EM_DEBUG_FUNC_END();
+       return true;
+}
+
+INTERNAL_FUNC int emcore_gmime_pop3_parse_mime(char *eml_path, struct _m_content_info *cnt_info, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("cnt_info[%p], err_code[%p]", cnt_info, err_code);
+       EM_DEBUG_LOG_SEC("eml_path[%s]", eml_path);
+
+       GMimeStream *stream = NULL;
+       GMimeMessage *message = NULL;
+       GMimeParser *parser = NULL;
+       int fd = 0;
+       int err = EMAIL_ERROR_NONE;
+
+       err = em_open(eml_path, O_RDONLY, 0, &fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+               if (err_code)
+                       *err_code = err;
+               return false; /*prevent 34936*/
+       }
+
+       stream = g_mime_stream_fs_new(fd);
+
+       parser = g_mime_parser_new_with_stream(stream);
+       if (stream) g_object_unref(stream);
+
+       message = g_mime_parser_construct_message(parser);
+       if (parser) g_object_unref(parser);
+       if (!message) { /* prevent null check for message */
+               EM_DEBUG_EXCEPTION("g_mime_parser_construct_message error");
+               return false;
+       }
+
+       EM_DEBUG_LOG_DEV("Sender:%s", g_mime_message_get_sender(message));
+       EM_DEBUG_LOG_DEV("Reply To:%s", g_mime_message_get_reply_to(message));
+       EM_DEBUG_LOG_DEV("Subject:%s", g_mime_message_get_subject(message));
+       EM_DEBUG_LOG_DEV("Date:%s", g_mime_message_get_date_as_string(message));
+       EM_DEBUG_LOG_DEV("Message ID:%s", g_mime_message_get_message_id(message));
+
+       if (g_strrstr(g_mime_message_get_sender(message), "mmsc.plusnet.pl") != NULL ||
+               g_strrstr(g_mime_message_get_sender(message), "mms.t-mobile.pl") != NULL) {
+               cnt_info->attachment_only = 1;
+       }
+
+       g_mime_message_foreach(message, emcore_gmime_pop3_parse_foreach_cb, (gpointer)cnt_info);
+
+       EM_SAFE_CLOSE (fd);
+
+       if (message) g_object_unref (message);
+
+       EM_DEBUG_FUNC_END();
+       return true;
+}
+
+
+INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path, struct _rfc822header *rfc822_header, struct _m_content_info *cnt_info, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("cnt_info[%p], err_code[%p]", cnt_info, err_code);
+       EM_DEBUG_LOG_SEC("eml_path[%s]", eml_path);
+
+       GMimeStream *stream = NULL;
+       GMimeMessage *message = NULL;
+       GMimeParser *parser = NULL;
+       int fd = 0;
+       int err = EMAIL_ERROR_NONE;
+
+       err = em_open(eml_path, O_RDONLY, 0, &fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+               if (err_code)
+                       *err_code = err;
+               return false; /*prevent 34936*/
+       }
+
+       multipart_status = 0;
+
+       stream = g_mime_stream_fs_new(fd);
+
+       parser = g_mime_parser_new_with_stream(stream);
+       if (stream) g_object_unref(stream);
+
+       message = g_mime_parser_construct_message(parser);
+       if (parser) g_object_unref(parser);
+       if (!message) { /* prevent null check for message */
+               EM_DEBUG_EXCEPTION("g_mime_parser_construct_message error");
+               return false;
+       }
+
+       EM_DEBUG_LOG_DEV("Sender:%s", g_mime_message_get_sender(message));
+       EM_DEBUG_LOG_DEV("Reply To:%s", g_mime_message_get_reply_to(message));
+       EM_DEBUG_LOG_DEV("Subject:%s", g_mime_message_get_subject(message));
+       EM_DEBUG_LOG_DEV("Date:%s", g_mime_message_get_date_as_string(message));
+       EM_DEBUG_LOG_DEV("Message ID:%s", g_mime_message_get_message_id(message));
+
+       emcore_gmime_parse_mime_header(message, rfc822_header);
+
+       g_mime_message_foreach(message, emcore_gmime_eml_parse_foreach_cb, (gpointer)cnt_info);
+
+       EM_SAFE_CLOSE (fd);
+
+       if (message) g_object_unref (message);
+
+       EM_DEBUG_FUNC_END();
+       return true;
+}
+
+
+static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       int error = EMAIL_ERROR_NONE;
+       char *msg_tmp_content_path = NULL;
+       char *content_path = NULL;
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+
+               EM_DEBUG_LOG("Message Part");
+               GMimeMessage *message = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               GMimeStream *out_stream;
+               char *msg_ctype_type = NULL;
+               char *msg_ctype_subtype = NULL;
+               char *msg_ctype_name = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_disposition_filename = NULL;
+               char *msg_content_id = NULL;
+               int real_size = 0;
+               int msg_fd = 0;
+
+               if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT) &&
+                               cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) {
+                       goto FINISH_OFF;
+               }
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_type = (char *)g_mime_content_type_get_media_type(msg_ctype);
+               msg_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(msg_ctype);
+               msg_ctype_name = (char *)g_mime_content_type_get_parameter(msg_ctype, "name");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", msg_ctype_type, msg_ctype_subtype);
+               EM_DEBUG_LOG_SEC("RFC822/Message Content-Type-Name[%s]", msg_ctype_name);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+                       msg_disposition_filename = (char *)g_mime_content_disposition_get_parameter(msg_disposition, "filename");
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               EM_DEBUG_LOG_SEC("RFC822/Message Disposition-Filename[%s]", msg_disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               msg_content_id = (char *)g_mime_object_get_content_id(part);
+               EM_DEBUG_LOG("RFC822/Message Content-ID:%s", msg_content_id);
+
+               /*save message content to tmp file*/
+               if (!emcore_get_temp_file_name(&msg_tmp_content_path, &error)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", msg_tmp_content_path);
+
+               msg_fd = open(msg_tmp_content_path, O_WRONLY|O_CREAT, 0644);
+               if (msg_fd < 0) {
+                       EM_DEBUG_EXCEPTION("open failed");
+                       goto FINISH_OFF;
+               }
+
+               out_stream = g_mime_stream_fs_new(msg_fd);
+               real_size = g_mime_object_write_to_stream(GMIME_OBJECT(message), out_stream);
+               if (out_stream) g_object_unref(out_stream);
+
+               if (real_size <= 0) {
+                       EM_DEBUG_EXCEPTION("g_mime_object_write_to_stream failed");
+                       goto FINISH_OFF;
+               }
+
+               if (msg_disposition_str && g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       EM_DEBUG_LOG("RFC822/Message is ATTACHMENT");
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->file;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = ATTACHMENT;
+                       if (msg_disposition_filename) file->name = g_strdup(msg_disposition_filename);
+                       else if (msg_ctype_name) file->name = g_strdup(msg_ctype_name);
+                       else if (msg_content_id) file->name = g_strdup(msg_content_id);
+                       else file->name = g_strdup("unknown");
+
+                       if (msg_content_id) file->content_id = g_strdup(msg_content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       if(msg_ctype_type && msg_ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", msg_ctype_type, msg_ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(msg_tmp_content_path);
+                       file->size = real_size;
+                       file->save_status = 1;
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->file = file;
+                       else
+                               temp_file->next = file;
+               }
+
+               //g_mime_message_foreach(message, emcore_gmime_pop3_parse_foreach_cb, user_data);
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+               //TODO
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+               GMimeMultipart *multi_part = NULL;
+               multi_part = (GMimeMultipart *)part;
+
+               int multi_count = g_mime_multipart_get_count(multi_part);
+               EM_DEBUG_LOG("Multi Part Count:%d", multi_count);
+               EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part));
+
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_size = 0;
+
+               GMimePart *leaf_part = NULL;
+               leaf_part = (GMimePart *)part;
+
+               EM_DEBUG_LOG("Content ID:%s", g_mime_part_get_content_id(leaf_part));
+               EM_DEBUG_LOG_SEC("Description:%s", g_mime_part_get_content_description(leaf_part));
+               EM_DEBUG_LOG_SEC("MD5:%s", g_mime_part_get_content_md5(leaf_part));
+
+               int content_disposition_type = 0;
+               char *content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Location:%s", content_location);
+
+               GMimeObject *mobject = (GMimeObject *)part;
+
+               /*Content ID*/
+               char *content_id = (char *)g_mime_object_get_content_id(mobject);
+
+               /*Content Disposition*/
+               GMimeContentDisposition *disposition = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content Type*/
+               GMimeContentType *ctype = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_charset = NULL;
+               char *ctype_name = NULL;
+
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_charset = (char *)g_mime_content_type_get_parameter(ctype, "charset");
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", ctype_type, ctype_subtype);
+               EM_DEBUG_LOG("Content-Type-Charset[%s]", ctype_charset);
+               EM_DEBUG_LOG("Content-Type-Name[%s]", ctype_name);
+               /*Content Type - END*/
+
+               /*Content*/
+               if (!emcore_get_temp_file_name(&content_path, &error)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", content_path);
+
+
+               GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
+               switch(enc) {
+               case GMIME_CONTENT_ENCODING_DEFAULT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_DEFAULT");
+                       break;
+               case GMIME_CONTENT_ENCODING_7BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_7BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_8BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_8BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_BINARY:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BINARY");
+                       break;
+               case GMIME_CONTENT_ENCODING_BASE64:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BASE64");
+                       break;
+               case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_QUOTEDPRINTABLE");
+                       break;
+               case GMIME_CONTENT_ENCODING_UUENCODE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_UUENCODE");
+                       break;
+
+               default:
+                       break;
+               }
+
+               GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
+               if (data) {
+                       int fd;
+                       fd = open(content_path, O_WRONLY|O_CREAT, 0644);
+                       if (fd < 0) {
+                               EM_DEBUG_EXCEPTION("open failed");
+                               goto FINISH_OFF;
+                       }
+
+                       GMimeStream *out_stream;
+                       out_stream = g_mime_stream_fs_new(fd);
+
+                       //g_mime_data_wrapper_set_stream(data, out_stream);
+                       //g_mime_data_wrapper_set_encoding(data, enc);
+                       g_mime_data_wrapper_write_to_stream(data, out_stream);
+                       if (out_stream) g_object_unref(out_stream);
+                       emcore_get_file_size(content_path, &content_size, NULL);
+               }
+               /*Content - END*/
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
+               int result = false;
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id &&
+                                       (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       } else if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
+                       /*TEXT*/
+                       EM_DEBUG_LOG("TEXT");
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "text") == 0) {
+                               if (!ctype_charset || g_ascii_strcasecmp(ctype_charset, "X-UNKNOWN") == 0) {
+                                       ctype_charset = "UTF-8";
+                               }
+
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "plain") == 0) {
+                                       EM_DEBUG_LOG("TEXT/PLAIN");
+
+                                       char *file_content = NULL;
+                                       int content_size = 0;
+
+                                       if (emcore_get_content_from_file(content_path, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                                       }
+
+                                       if (file_content && content_size > 0) {
+                                               char escape = 0x1b;
+                                               char detector[25] = {0,};
+                                               snprintf(detector, sizeof(detector), "%c$B", escape);
+                                               if (g_strrstr(ctype_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                                                       ctype_charset = "ISO-2022-JP";
+                                               }
+                                       }
+
+                                       EM_SAFE_FREE(file_content);
+
+                                       cnt_info->text.plain_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.plain = g_strdup(content_path);
+                               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "html") == 0) {
+                                       EM_DEBUG_LOG("TEXT/HTML");
+                                       cnt_info->text.html_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.html = g_strdup(content_path);
+                               }
+                       }
+               } else {
+                       /*ATTACHMENT*/
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = NULL;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       if (content_disposition_type == ATTACHMENT) {
+                               EM_DEBUG_LOG("ATTACHMENT");
+                               temp_file = cnt_info->file;
+                       }
+                       else if (content_disposition_type == INLINE_ATTACHMENT) {
+                               EM_DEBUG_LOG("INLINE ATTACHMENT");
+                               temp_file = cnt_info->inline_file;
+                       }
+                       else {
+                               EM_DEBUG_EXCEPTION("Invalid content_disposition_type");
+                               goto FINISH_OFF;
+                       }
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       file->content_id = g_strdup(content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       /* check inline name duplication */
+                       if (content_disposition_type == INLINE_ATTACHMENT) {
+                               char *modified_name = NULL;
+                               if (emcore_gmime_check_filename_duplication(file->name, cnt_info)) {
+                                       modified_name = emcore_gmime_get_modified_filename_in_duplication(file->name);
+                                       EM_SAFE_FREE(file->name);
+                                       file->name = modified_name;
+                                       modified_name = NULL;
+                               }
+                               emcore_unescape_from_url(file->name, &modified_name);
+                               EM_DEBUG_LOG_SEC("file->name[%s] modified_name[%s]", file->name, modified_name);
+                               EM_SAFE_FREE(file->name);
+                               file->name = modified_name;
+                       }
+
+                       /*cid replacement for inline attachment*/
+                       if (content_disposition_type == INLINE_ATTACHMENT) {
+                               char *file_content = NULL;
+                               int content_size = 0;
+
+                               if (emcore_get_content_from_file(cnt_info->text.html, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                               }
+
+                               if (file_content && content_size > 0) {
+                                       em_replace_string_ex(&file_content, "cid:", "");
+                                       em_replace_string_ex(&file_content, file->content_id, file->name);
+
+                                       content_size = EM_SAFE_STRLEN(file_content);
+                                       if (emcore_set_content_to_file(file_content, cnt_info->text.html, content_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_set_content_to_file failed");
+                                       }
+                               }
+                               g_free(file_content); /* prevent 39110 */
+                       }
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(content_path);
+                       file->size = content_size;
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "APPLICATION") == 0) {
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_OBJECT) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_OBJECT;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_RIGHTS) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_RIGHTS;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_DCF) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_DCF;
+                       }
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL) {
+                               if (content_disposition_type == ATTACHMENT) {
+                                       cnt_info->file = file;
+                               } else {
+                                       cnt_info->inline_file = file;
+                               }
+                       }
+                       else
+                               temp_file->next = file;
+               }
+
+               EM_SAFE_FREE(content_path);
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(msg_tmp_content_path);
+       EM_SAFE_FREE(content_path);
+       EM_DEBUG_FUNC_END();
+}
+
+
+static int emcore_gmime_parse_mime_header(GMimeMessage *message, struct _rfc822header *rfc822_header)
+{
+       EM_DEBUG_FUNC_BEGIN("message[%p], rfc822header[%p]", message, rfc822_header);
+
+       int err = EMAIL_ERROR_NONE;
+
+       if (!message || !rfc822_header) {
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       rfc822_header->reply_to     = EM_SAFE_STRDUP(g_mime_message_get_reply_to(message));
+       EM_DEBUG_LOG_SEC("reply_to : [%s]", rfc822_header->reply_to);
+
+       rfc822_header->date         = EM_SAFE_STRDUP(g_mime_message_get_date_as_string(message));
+       EM_DEBUG_LOG_SEC("date : [%s]", rfc822_header->date);
+
+       rfc822_header->subject      = EM_SAFE_STRDUP(g_mime_message_get_subject(message));
+       EM_DEBUG_LOG_SEC("subject : [%s]", rfc822_header->subject);
+
+       rfc822_header->sender       = EM_SAFE_STRDUP(g_mime_message_get_sender(message));
+       EM_DEBUG_LOG_SEC("sender : [%s]", rfc822_header->sender);
+
+       rfc822_header->to           = EM_SAFE_STRDUP(internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_TO), false));
+       EM_DEBUG_LOG_SEC("to : [%s]", rfc822_header->to);
+
+       rfc822_header->cc           = EM_SAFE_STRDUP(internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_CC), false));
+       EM_DEBUG_LOG_SEC("cc : [%s]", rfc822_header->cc);
+
+       rfc822_header->bcc          = EM_SAFE_STRDUP(internet_address_list_to_string(g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_BCC), false));
+       EM_DEBUG_LOG_SEC("bcc : [%s]", rfc822_header->bcc);
+
+       rfc822_header->message_id   = EM_SAFE_STRDUP(g_mime_message_get_message_id(message));
+       EM_DEBUG_LOG("message_id : [%s]", rfc822_header->message_id);
+
+       rfc822_header->content_type = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "content-type"));
+       EM_DEBUG_LOG("content_type : [%s]", rfc822_header->content_type);
+
+       rfc822_header->from         = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "from"));
+       EM_DEBUG_LOG_SEC("from : [%s]", rfc822_header->from);
+
+       rfc822_header->received     = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "received"));
+       EM_DEBUG_LOG_SEC("received : [%s]", rfc822_header->received);
+
+       rfc822_header->return_path  = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "return-path"));
+       EM_DEBUG_LOG_SEC("return_path : [%s]", rfc822_header->return_path);
+
+       rfc822_header->priority     = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "x-priority"));
+       EM_DEBUG_LOG("priority : [%s]", rfc822_header->priority);
+
+       rfc822_header->ms_priority  = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "x-msmail-priority"));
+       EM_DEBUG_LOG("ms_priority : [%s]", rfc822_header->ms_priority);
+
+       rfc822_header->dsp_noti_to  = EM_SAFE_STRDUP(g_mime_object_get_header((GMimeObject *)message, "disposition-notification-to"));
+       EM_DEBUG_LOG("dsp_noti_to : [%s]", rfc822_header->dsp_noti_to);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       int error = EMAIL_ERROR_NONE;
+       int save_status = EMAIL_PART_DOWNLOAD_STATUS_NONE;
+       char *msg_tmp_content_path = NULL;
+       char *content_path = NULL;
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               EM_DEBUG_LOG("Message Part");
+               GMimeMessage *message = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               GMimeStream *out_stream;
+               char *msg_ctype_type = NULL;
+               char *msg_ctype_subtype = NULL;
+               char *msg_ctype_name = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_disposition_filename = NULL;
+               char *msg_content_id = NULL;
+               int real_size = 0;
+               int msg_fd = 0;
+
+               save_status = EMAIL_PART_DOWNLOAD_STATUS_NONE;
+
+               if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT) &&
+                               cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) {
+                       goto FINISH_OFF;
+               }
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_type = (char *)g_mime_content_type_get_media_type(msg_ctype);
+               msg_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(msg_ctype);
+               msg_ctype_name = (char *)g_mime_content_type_get_parameter(msg_ctype, "name");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", msg_ctype_type, msg_ctype_subtype);
+               EM_DEBUG_LOG("RFC822/Message Content-Type-Name[%s]", msg_ctype_name);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+                       msg_disposition_filename = (char *)g_mime_content_disposition_get_parameter(msg_disposition, "filename");
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               EM_DEBUG_LOG_SEC("RFC822/Message Disposition-Filename[%s]", msg_disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               msg_content_id = (char *)g_mime_object_get_content_id(part);
+               EM_DEBUG_LOG("RFC822/Message Content-ID:%s", msg_content_id);
+
+               /*save message content to tmp file*/
+               if (!emcore_get_temp_file_name(&msg_tmp_content_path, &error)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", msg_tmp_content_path);
+
+               msg_fd = open(msg_tmp_content_path, O_WRONLY|O_CREAT, 0644);
+               if (msg_fd < 0) {
+                       EM_DEBUG_EXCEPTION("open failed");
+                       goto FINISH_OFF;
+               }
+
+               out_stream = g_mime_stream_fs_new(msg_fd);
+               real_size = g_mime_object_write_to_stream(GMIME_OBJECT(message), out_stream);
+               if (out_stream) g_object_unref(out_stream);
+
+               if (real_size <= 0) {
+                       EM_DEBUG_EXCEPTION("g_mime_object_write_to_stream failed");
+                       goto FINISH_OFF;
+               }
+
+               /* rfc822/message type is always saving to the attachment */
+               EM_DEBUG_LOG("RFC822/Message is ATTACHMENT");
+
+               struct attachment_info *file = NULL;
+               struct attachment_info *temp_file = cnt_info->file;
+               char *utf8_text = NULL;
+               int err = EMAIL_ERROR_NONE;
+
+               file = em_malloc(sizeof(struct attachment_info));
+               if (file == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed...");
+                       goto FINISH_OFF;
+               }
+
+               file->type = ATTACHMENT;
+               if (msg_disposition_filename) file->name = g_strdup(msg_disposition_filename);
+               else if (msg_ctype_name) file->name = g_strdup(msg_ctype_name);
+               else if (msg_content_id) file->name = g_strdup(msg_content_id);
+               else file->name = g_strdup("unknown");
+
+               if (msg_content_id) file->content_id = g_strdup(msg_content_id);
+
+               if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                       EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+               }
+               EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+               if (utf8_text) {
+                       EM_SAFE_FREE(file->name);
+                       file->name = EM_SAFE_STRDUP(utf8_text);
+               }
+               EM_SAFE_FREE(utf8_text);
+
+               if(msg_ctype_type && msg_ctype_subtype) {
+                       char mime_type_buffer[128] = {0,};
+                       snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", msg_ctype_type, msg_ctype_subtype);
+                       file->attachment_mime_type = g_strdup(mime_type_buffer);
+               }
+
+               file->save = g_strdup(msg_tmp_content_path);
+               file->size = real_size;
+
+               while (temp_file && temp_file->next)
+                       temp_file = temp_file->next;
+
+               if (temp_file == NULL)
+                       cnt_info->file = file;
+               else
+                       temp_file->next = file;
+
+               /* check the partial status */
+               int save_status = EMAIL_BODY_DOWNLOAD_STATUS_NONE;
+               struct _m_content_info *temp_cnt_info = NULL;
+               struct attachment_info *ai = NULL;
+
+               temp_cnt_info = (struct _m_content_info *)em_malloc(sizeof(struct _m_content_info));
+               if (temp_cnt_info == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               g_mime_message_foreach(message, emcore_gmime_eml_parse_foreach_cb, temp_cnt_info);
+
+               save_status = temp_cnt_info->text.plain_save_status;
+               save_status = temp_cnt_info->text.html_save_status;
+
+               for (ai = temp_cnt_info->file; ai; ai = ai->next) {
+                       save_status = ai->save_status;
+               }
+
+               for (ai = temp_cnt_info->inline_file; ai; ai = ai->next) {
+                       save_status = ai->save_status;
+               }
+
+               file->save_status = save_status;
+               EM_DEBUG_LOG("save_status : [%d], [%d]", file->save_status, save_status);
+
+               if (temp_cnt_info) {
+                       emcore_free_content_info(temp_cnt_info);
+                       EM_SAFE_FREE(temp_cnt_info);
+               }
+
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+               //TODO
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+               GMimeMultipart *multi_part = NULL;
+               multi_part = (GMimeMultipart *)part;
+
+               multipart_status = 1;
+
+               int multi_count = g_mime_multipart_get_count(multi_part);
+               EM_DEBUG_LOG("Multi Part Count:%d", multi_count);
+               EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part));
+
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_size = 0;
+
+               save_status = EMAIL_PART_DOWNLOAD_STATUS_NONE;
+
+               GMimePart *leaf_part = NULL;
+               leaf_part = (GMimePart *)part;
+
+               EM_DEBUG_LOG("Content ID:%s", g_mime_part_get_content_id(leaf_part));
+               EM_DEBUG_LOG("Description:%s", g_mime_part_get_content_description(leaf_part));
+               EM_DEBUG_LOG("MD5:%s", g_mime_part_get_content_md5(leaf_part));
+
+               int content_disposition_type = 0;
+               char *content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Location:%s", content_location);
+
+               GMimeObject *mobject = (GMimeObject *)part;
+
+               /*Content ID*/
+               char *content_id = (char *)g_mime_object_get_content_id(mobject);
+
+               /*Content Disposition*/
+               GMimeContentDisposition *disposition = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content Type*/
+               GMimeContentType *ctype = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_charset = NULL;
+               char *ctype_name = NULL;
+
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_charset = (char *)g_mime_content_type_get_parameter(ctype, "charset");
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", ctype_type, ctype_subtype);
+               EM_DEBUG_LOG("Content-Type-Charset[%s]", ctype_charset);
+               EM_DEBUG_LOG("Content-Type-Name[%s]", ctype_name);
+               /*Content Type - END*/
+
+               /*Content*/
+               if (!emcore_get_temp_file_name(&content_path, &error)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", content_path);
+
+
+               GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
+               switch(enc) {
+               case GMIME_CONTENT_ENCODING_DEFAULT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_DEFAULT");
+                       break;
+               case GMIME_CONTENT_ENCODING_7BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_7BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_8BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_8BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_BINARY:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BINARY");
+                       break;
+               case GMIME_CONTENT_ENCODING_BASE64:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BASE64");
+                       break;
+               case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_QUOTEDPRINTABLE");
+                       break;
+               case GMIME_CONTENT_ENCODING_UUENCODE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_UUENCODE");
+                       break;
+
+               default:
+                       break;
+               }
+
+               GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
+               if (data) {
+                       int fd;
+                       fd = open(content_path, O_WRONLY|O_CREAT, 0644);
+                       if (fd < 0) {
+                               EM_DEBUG_EXCEPTION("open failed");
+                               goto FINISH_OFF;
+                       }
+
+                       GMimeStream *out_stream;
+                       out_stream = g_mime_stream_fs_new(fd);
+
+                       //g_mime_data_wrapper_set_stream(data, out_stream);
+                       //g_mime_data_wrapper_set_encoding(data, enc);
+                       g_mime_data_wrapper_write_to_stream(data, out_stream);
+                       if (out_stream) g_object_unref(out_stream);
+                       emcore_get_file_size(content_path, &content_size, NULL);
+               } else {
+                       EM_DEBUG_LOG("Data is NULL");
+                       goto FINISH_OFF;
+               }
+               /*Content - END*/
+
+               /* Set the partial body */
+               GMimeStream *part_stream = g_mime_data_wrapper_get_stream(data);
+               if (part_stream) {
+                       EM_DEBUG_LOG("part_stream->bound_end : [%lld]", part_stream->bound_end);
+                       EM_DEBUG_LOG("super_stream->position : [%lld]", part_stream->super_stream->position);
+                       EM_DEBUG_LOG("multipart_status : [%d]", multipart_status);
+                       if (multipart_status && part_stream->super_stream->position <= part_stream->bound_end) {
+                               save_status = EMAIL_PART_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
+                       } else {
+                               save_status = EMAIL_PART_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+                       }
+
+                       EM_DEBUG_LOG("save_status : [%d]", save_status);
+               }
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
+               int result = false;
+               cnt_info->total_mail_size += content_size;
+               if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       cnt_info->total_body_size += content_size;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
+                       content_disposition_type = ATTACHMENT;
+                       cnt_info->total_attachment_size += content_size;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       cnt_info->total_attachment_size += content_size;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       cnt_info->total_attachment_size += content_size;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else {
+                       cnt_info->total_body_size += content_size;
+                       EM_DEBUG_LOG("Not INLINE or ATTACHMENT");
+               }
+
+               if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
+                       /*TEXT*/
+                       EM_DEBUG_LOG("TEXT");
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "text") == 0) {
+                               if (!ctype_charset || g_ascii_strcasecmp(ctype_charset, "X-UNKNOWN") == 0) {
+                                       ctype_charset = "UTF-8";
+                               }
+
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "plain") == 0) {
+                                       EM_DEBUG_LOG("TEXT/PLAIN");
+
+                                       char *file_content = NULL;
+                                       int content_size = 0;
+
+                                       if (emcore_get_content_from_file(content_path, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                                       }
+
+                                       if (file_content && content_size > 0) {
+                                               char escape = 0x1b;
+                                               char detector[25] = {0,};
+                                               snprintf(detector, sizeof(detector), "%c$B", escape);
+                                               if (g_strrstr(ctype_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                                                       ctype_charset = "ISO-2022-JP";
+                                               }
+                                       }
+
+                                       EM_SAFE_FREE(file_content);
+
+                                       cnt_info->text.plain_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.plain = g_strdup(content_path);
+                                       cnt_info->text.plain_save_status = save_status;
+                               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "html") == 0) {
+                                       EM_DEBUG_LOG("TEXT/HTML");
+                                       cnt_info->text.html_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.html = g_strdup(content_path);
+                                       cnt_info->text.html_save_status = save_status;
+                               }
+                       }
+               } else {
+                       /*ATTACHMENT*/
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = NULL;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       if (content_disposition_type == ATTACHMENT) {
+                               EM_DEBUG_LOG("ATTACHMENT");
+                               temp_file = cnt_info->file;
+                       }
+                       else if (content_disposition_type == INLINE_ATTACHMENT) {
+                               EM_DEBUG_LOG("INLINE ATTACHMENT");
+                               temp_file = cnt_info->inline_file;
+                       }
+                       else {
+                               EM_DEBUG_EXCEPTION("Invalid content_disposition_type");
+                               goto FINISH_OFF;
+                       }
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) file->name = g_strdup("invite.vcs");
+                       else file->name = g_strdup("delivery-status");
+                       file->content_id = g_strdup(content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       /* check inline name duplication */
+                       if (content_disposition_type == INLINE_ATTACHMENT) {
+                               if (emcore_gmime_check_filename_duplication(file->name, cnt_info)) {
+                                       char *modified_name= NULL;
+                                       modified_name = emcore_gmime_get_modified_filename_in_duplication(file->name);
+                                       EM_SAFE_FREE(file->name);
+                                       file->name = modified_name;
+                               }
+                       }
+
+                       /*cid replacement for inline attachment*/
+                       if (content_disposition_type == INLINE_ATTACHMENT) {
+                               char *file_content = NULL;
+                               char *encoding_file_name = NULL;
+                               int html_size = 0;
+                               iconv_t cd;
+
+                               if (emcore_get_content_from_file(cnt_info->text.html, &file_content, &html_size) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                               }
+
+                               EM_DEBUG_LOG_SEC("html_charset : [%s]", cnt_info->text.html_charset);
+                               if (strcasecmp(cnt_info->text.html_charset, "UTF-8") != 0) {
+                                       cd = g_mime_iconv_open(cnt_info->text.html_charset, "UTF-8");
+                                       if (cd) {
+                                               encoding_file_name = g_mime_iconv_strdup(cd, file->name);
+                                       }
+
+                                       if (cd)
+                                               g_mime_iconv_close(cd);
+
+                               } else {
+                                       encoding_file_name = g_strdup(file->name);
+                               }
+
+                               EM_DEBUG_LOG_SEC("File name : [%s], encoding file name : [%s]", file->name, encoding_file_name);
+
+                               if (file_content && html_size > 0) {
+                                       em_replace_string_ex(&file_content, "cid:", "");
+                                       em_replace_string_ex(&file_content, file->content_id, encoding_file_name);
+
+                                       html_size = EM_SAFE_STRLEN(file_content);
+                                       if (emcore_set_content_to_file(file_content, cnt_info->text.html, html_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_set_content_to_file failed");
+                                       }
+                               }
+
+                               g_free(file_content); /* prevent 39110 */
+                               g_free(encoding_file_name);
+                       }
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save_status = save_status;
+                       file->save        = g_strdup(content_path);
+                       file->size        = content_size;
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "APPLICATION") == 0) {
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_OBJECT) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_OBJECT;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_RIGHTS) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_RIGHTS;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_DCF) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_DCF;
+                       }
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL) {
+                               if (content_disposition_type == ATTACHMENT) {
+                                       cnt_info->file = file;
+                               } else {
+                                       cnt_info->inline_file = file;
+                               }
+                       }
+                       else
+                               temp_file->next = file;
+               }
+
+               EM_SAFE_FREE(content_path);
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(msg_tmp_content_path);
+       EM_SAFE_FREE(content_path);
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       int error = EMAIL_ERROR_NONE;
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+       char *content_path = NULL;
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               /* message/rfc822 or message/news */
+               GMimeMessage *message = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               char *msg_ctype_type = NULL;
+               char *msg_ctype_subtype = NULL;
+               char *msg_ctype_name = NULL;
+               char *msg_ctype_size = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_disposition_filename = NULL;
+               char *msg_content_id = NULL;
+               unsigned long msg_content_size = 0;
+               EM_DEBUG_LOG("Message Part");
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_type = (char *)g_mime_content_type_get_media_type(msg_ctype);
+               msg_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(msg_ctype);
+               msg_ctype_name = (char *)g_mime_content_type_get_parameter(msg_ctype, "name");
+               msg_ctype_size = (char *)g_mime_content_type_get_parameter(msg_ctype, "message_size");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", msg_ctype_type, msg_ctype_subtype);
+               EM_DEBUG_LOG("RFC822/Message Content-Type-Name[%s]", msg_ctype_name);
+               EM_DEBUG_LOG("Part.size.bytes[%s]", msg_ctype_size);
+
+               if (msg_ctype_size) msg_content_size = atol(msg_ctype_size);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+                       msg_disposition_filename = (char *)g_mime_content_disposition_get_parameter(msg_disposition, "filename");
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               EM_DEBUG_LOG_SEC("RFC822/Message Disposition-Filename[%s]", msg_disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               msg_content_id = (char *)g_mime_object_get_content_id(part);
+               EM_DEBUG_LOG("RFC822/Message Content-ID:%s", msg_content_id);
+
+               if (msg_disposition_str && g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       EM_DEBUG_LOG("RFC822/Message is ATTACHMENT");
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->file;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = ATTACHMENT;
+                       if (msg_disposition_filename) file->name = g_strdup(msg_disposition_filename);
+                       else if (msg_ctype_name) file->name = g_strdup(msg_ctype_name);
+                       else if (msg_content_id) file->name = g_strdup(msg_content_id);
+                       else file->name = g_strdup("unknown");
+
+                       if (msg_content_id) file->content_id = g_strdup(msg_content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       if(msg_ctype_type && msg_ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", msg_ctype_type, msg_ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = NULL;
+                       file->size = msg_content_size;
+                       file->save_status = 0;
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->file = file;
+                       else
+                               temp_file->next = file;
+               }
+
+               //g_mime_message_foreach(message, emcore_gmime_imap_parse_foreach_cb, user_data);
+
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               /* message/partial */
+
+               EM_DEBUG_LOG("Partial Part");
+               //TODO
+       } else if (GMIME_IS_MULTIPART(part)) {
+               /* multipart/mixed, multipart/alternative,
+                * multipart/related, multipart/signed,
+                * multipart/encrypted, etc... */
+               EM_DEBUG_LOG("Multi Part");
+               GMimeMultipart *multi_part = NULL;
+               multi_part = (GMimeMultipart *)part;
+
+               int multi_count = g_mime_multipart_get_count(multi_part);
+               EM_DEBUG_LOG("Multi Part Count:%d", multi_count);
+               EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part));
+
+       } else if (GMIME_IS_PART(part)) {
+               /* a normal leaf part, could be text/plain or
+                * image/jpeg etc */
+
+               EM_DEBUG_LOG("Part");
+               int download_status = 0;
+               int content_disposition_type = 0;
+               char *content_id = NULL;
+               char *content_location = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               int disposition_size = 0;
+               char *disposition_size_str = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_charset = NULL;
+               char *ctype_name = NULL;
+               char *ctype_size = NULL;
+               unsigned long content_size = 0;
+               int real_size = 0;
+
+               GMimeContentType *ctype = NULL;
+               GMimeContentDisposition *disposition = NULL;
+               GMimePart *leaf_part = NULL;
+               GMimeObject *mobject = (GMimeObject *)part;
+               leaf_part = (GMimePart *)part;
+
+               /*Content Type*/
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_charset = (char *)g_mime_content_type_get_parameter(ctype, "charset");
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               ctype_size = (char *)g_mime_content_type_get_parameter(ctype, "part_size");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", ctype_type, ctype_subtype);
+               EM_DEBUG_LOG("Content-Type-Charset[%s]", ctype_charset);
+               EM_DEBUG_LOG_SEC("Content-Type-Name[%s]", ctype_name);
+               EM_DEBUG_LOG("Part.size.bytes[%s]", ctype_size);
+
+               if (ctype_size) content_size = atol(ctype_size);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+                       disposition_size_str = (char *)g_mime_content_disposition_get_parameter(disposition, "size");
+                       if (disposition_size_str) disposition_size = atoi(disposition_size_str);
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               EM_DEBUG_LOG("Disposition size[%d]", disposition_size);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               content_id = (char *)g_mime_object_get_content_id(mobject);
+               EM_DEBUG_LOG_SEC("Content-ID:%s", content_id);
+
+               /*Content Location*/
+               content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
+               int result = false;
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       if (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 if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id &&
+                                       (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       } else if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               /*if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               }*/
+
+               if (!emcore_get_temp_file_name(&content_path, &error))  {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", content_path);
+
+               if (content_disposition_type != ATTACHMENT) {
+                       /*Content*/
+                       GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
+                       switch(enc) {
+                       case GMIME_CONTENT_ENCODING_DEFAULT:
+                               EM_DEBUG_LOG("Encoding:ENCODING_DEFAULT");
+                               break;
+                       case GMIME_CONTENT_ENCODING_7BIT:
+                               EM_DEBUG_LOG("Encoding:ENCODING_7BIT");
+                               break;
+                       case GMIME_CONTENT_ENCODING_8BIT:
+                               EM_DEBUG_LOG("Encoding:ENCODING_8BIT");
+                               break;
+                       case GMIME_CONTENT_ENCODING_BINARY:
+                               EM_DEBUG_LOG("Encoding:ENCODING_BINARY");
+                               break;
+                       case GMIME_CONTENT_ENCODING_BASE64:
+                               EM_DEBUG_LOG("Encoding:ENCODING_BASE64");
+                               break;
+                       case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
+                               EM_DEBUG_LOG("Encoding:ENCODING_QUOTEDPRINTABLE");
+                               break;
+                       case GMIME_CONTENT_ENCODING_UUENCODE:
+                               EM_DEBUG_LOG("Encoding:ENCODING_UUENCODE");
+                               break;
+
+                       default:
+                               break;
+                       }
+
+                       GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
+                       if (data) {
+                               EM_DEBUG_LOG_DEV("DataWrapper/ref-cnt[%d]", data->parent_object.ref_count);
+                               int fd = 0;
+                               int src_length = 0;
+                               GMimeStream *out_stream = NULL;
+                               GMimeStream *src_stream = NULL;
+
+                               fd = open(content_path, O_WRONLY|O_CREAT, 0644);
+                               if (fd < 0) {
+                                       EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+                                       goto FINISH_OFF;
+                               }
+
+                               out_stream = g_mime_stream_fs_new(fd);
+                               src_stream = g_mime_data_wrapper_get_stream(data);
+                               if (src_stream) src_length = g_mime_stream_length(src_stream);
+                               EM_DEBUG_LOG_DEV("Data length [%d]", src_length);
+
+                               if (src_length >= content_size) /* fully downloaded */
+                                       download_status = EMAIL_PART_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+                               else /* partialy downloaded */
+                                       download_status = EMAIL_PART_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
+
+                               /*g_mime_data_wrapper_set_stream(data, out_stream);
+                               g_mime_data_wrapper_set_encoding(data, enc);*/
+
+                               g_mime_data_wrapper_write_to_stream(data, out_stream);
+                               if (out_stream) g_object_unref(out_stream);
+                               emcore_get_file_size(content_path, &real_size, NULL);
+                       }
+                       /*Content - END*/
+               }
+
+               if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
+                       /*TEXT*/
+                       EM_DEBUG_LOG("TEXT");
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "text") == 0) {
+                               if (!ctype_charset || g_ascii_strcasecmp(ctype_charset, "X-UNKNOWN") == 0) {
+                                       ctype_charset = "UTF-8";
+                               }
+
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "plain") == 0) {
+                                       EM_DEBUG_LOG("TEXT/PLAIN");
+
+                                       char *file_content = NULL;
+                                       int content_size = 0;
+
+                                       if (emcore_get_content_from_file(content_path, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                                       }
+
+                                       if (file_content && content_size > 0) {
+                                               char escape = 0x1b;
+                                               char detector[25] = {0,};
+                                               snprintf(detector, sizeof(detector), "%c$B", escape);
+                                               if (g_strrstr(ctype_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                                                       ctype_charset = "ISO-2022-JP";
+                                               }
+                                       }
+
+                                       EM_SAFE_FREE(file_content);
+
+                                       cnt_info->text.plain_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.plain = g_strdup(content_path);
+                               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "html") == 0) {
+                                       EM_DEBUG_LOG("TEXT/HTML");
+                                       cnt_info->text.html_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.html = g_strdup(content_path);
+                               }
+                       }
+               } else if (content_disposition_type == INLINE_ATTACHMENT) {
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->inline_file;
+                       char *utf8_text = NULL;
+                       char *file_content = NULL;
+                       int file_size = 0;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       file->content_id = g_strdup(content_id);
+
+                       if (file->name && g_strrstr(file->name, "/") != NULL) {
+                               char *tmp_ptr = file->name;
+                               int tmp_len = EM_SAFE_STRLEN(file->name);
+                               int tmpi = 0;
+                               for (tmpi=0; tmpi<tmp_len; tmpi++) {
+                                       if (*(tmp_ptr+tmpi) == '/') {
+                                               *(tmp_ptr+tmpi) = '_';
+                                       }
+                               }
+                               EM_DEBUG_LOG_SEC("file->name[%s]", file->name);
+                       }
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       /* check inline name duplication */
+                       char *modified_name = NULL;
+                       if (emcore_gmime_check_filename_duplication(file->name, cnt_info)) {
+                               modified_name = emcore_gmime_get_modified_filename_in_duplication(file->name);
+                               EM_SAFE_FREE(file->name);
+                               file->name = modified_name;
+                               modified_name = NULL;
+                       }
+
+                       emcore_unescape_from_url(file->name, &modified_name);
+                       EM_DEBUG_LOG_SEC("file->name[%s] modified_name[%s]", file->name, modified_name);
+                       EM_SAFE_FREE(file->name);
+                       file->name = modified_name;
+
+                       /*cid replacement for inline attachment*/
+                       if (emcore_get_content_from_file(cnt_info->text.html, &file_content, &file_size) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                       }
+
+                       if (file_content && file_size > 0) {
+                               em_replace_string_ex(&file_content, "cid:", "");
+                               em_replace_string_ex(&file_content, file->content_id, file->name);
+
+                               file_size = EM_SAFE_STRLEN(file_content);
+                               if (emcore_set_content_to_file(file_content, cnt_info->text.html, file_size) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_set_content_to_file failed");
+                               }
+                       }
+                       g_free(file_content); /* prevent 39110 */
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(content_path);
+                       file->size = disposition_size ? disposition_size : content_size;
+                       if (download_status == EMAIL_PART_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
+                               file->save_status = 1;
+                               file->size = real_size;
+                       }
+                       else
+                               file->save_status = 0;
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->inline_file = file;
+                       else
+                               temp_file->next = file;
+               } else if (content_disposition_type == ATTACHMENT) {
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->file;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       else file->name = g_strdup("unknown-attachment");
+
+                       file->content_id = g_strdup(content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(content_path);
+                       file->size = disposition_size ? disposition_size : content_size;
+                       file->save_status = 0;
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "APPLICATION") == 0) {
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_OBJECT) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_OBJECT;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_RIGHTS) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_RIGHTS;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_DCF) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_DCF;
+                       }
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->file = file;
+                       else
+                               temp_file->next = file;
+               }
+
+               EM_SAFE_FREE(content_path);
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(content_path);
+
+       EM_DEBUG_FUNC_END();
+}
+
+
+INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       int error = EMAIL_ERROR_NONE;
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+       char *content_path = NULL;
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               EM_DEBUG_LOG("Message Part");
+               GMimeContentType *msg_ctype = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               char *msg_ctype_type = NULL;
+               char *msg_ctype_subtype = NULL;
+               char *msg_ctype_name = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_disposition_filename = NULL;
+               char *msg_content_id = NULL;
+               char *msg_tmp_content_path = NULL;
+               int real_size = 0;
+
+               if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT) &&
+                               cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) {
+                       goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_type = (char *)g_mime_content_type_get_media_type(msg_ctype);
+               msg_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(msg_ctype);
+               msg_ctype_name = (char *)g_mime_content_type_get_parameter(msg_ctype, "name");
+               msg_tmp_content_path = (char *)g_mime_content_type_get_parameter(msg_ctype, "tmp_content_path");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", msg_ctype_type, msg_ctype_subtype);
+               EM_DEBUG_LOG_SEC("RFC822/Message Content-Type-Name[%s]", msg_ctype_name);
+               EM_DEBUG_LOG_SEC("RFC822/Message Content-Type-tmp-path[%s]", msg_tmp_content_path);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+                       msg_disposition_filename = (char *)g_mime_content_disposition_get_parameter(msg_disposition, "filename");
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               EM_DEBUG_LOG_SEC("RFC822/Message Disposition-Filename[%s]", msg_disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               msg_content_id = (char *)g_mime_object_get_content_id(part);
+               EM_DEBUG_LOG("RFC822/Message Content-ID:%s", msg_content_id);
+
+               emcore_get_file_size(msg_tmp_content_path, &real_size, NULL);
+               if (real_size <= 0) {
+                       EM_DEBUG_EXCEPTION("tmp content file is not valid");
+                       goto FINISH_OFF;
+               }
+
+               if (msg_disposition_str && g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       EM_DEBUG_LOG("RFC822/Message is ATTACHMENT");
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->file;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = ATTACHMENT;
+                       if (msg_disposition_filename) file->name = g_strdup(msg_disposition_filename);
+                       else if (msg_ctype_name) file->name = g_strdup(msg_ctype_name);
+                       else if (msg_content_id) file->name = g_strdup(msg_content_id);
+                       else file->name = g_strdup("unknown");
+
+                       if (msg_content_id) file->content_id = g_strdup(msg_content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       if(msg_ctype_type && msg_ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", msg_ctype_type, msg_ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(msg_tmp_content_path);
+                       file->size = real_size;
+                       file->save_status = 1;
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->file = file;
+                       else
+                               temp_file->next = file;
+               }
+
+               //g_mime_message_foreach(message, emcore_gmime_imap_parse_full_foreach_cb, user_data);
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+               GMimeMultipart *multi_part = NULL;
+               multi_part = (GMimeMultipart *)part;
+
+               int multi_count = g_mime_multipart_get_count(multi_part);
+               EM_DEBUG_LOG("Multi Part Count:%d", multi_count);
+               EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part));
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_disposition_type = 0;
+               int real_size = 0;
+               char *content_id = NULL;
+               char *content_location = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_charset = NULL;
+               char *ctype_name = NULL;
+               char *ctype_size = NULL;
+               char *tmp_path = NULL;
+
+               GMimeContentType *ctype = NULL;
+               GMimeContentDisposition *disposition = NULL;
+               GMimePart *leaf_part = NULL;
+               GMimeObject *mobject = (GMimeObject *)part;
+               leaf_part = (GMimePart *)part;
+
+               /*Content Type*/
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_charset = (char *)g_mime_content_type_get_parameter(ctype, "charset");
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               ctype_size = (char *)g_mime_content_type_get_parameter(ctype, "part_size");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", ctype_type, ctype_subtype);
+               EM_DEBUG_LOG("Content-Type-Charset[%s]", ctype_charset);
+               EM_DEBUG_LOG_SEC("Content-Type-Name[%s]", ctype_name);
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               content_id = (char *)g_mime_object_get_content_id(mobject);
+               EM_DEBUG_LOG_SEC("Content-ID:%s", content_id);
+
+               /*Content Location*/
+               content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
+
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) ?*/
+               int result = false;
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id &&
+                                       (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       } else if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               /*if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) {
+                       content_disposition_type = INLINE_ATTACHMENT;
+                       EM_DEBUG_LOG("INLINE_ATTACHMENT");
+               } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               }*/
+
+               /*Content*/
+               if (!emcore_get_temp_file_name(&content_path, &error)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                       goto FINISH_OFF;
+               }
+               EM_DEBUG_LOG_SEC("Temporary Content Path[%s]", content_path);
+
+               GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
+               switch(enc) {
+               case GMIME_CONTENT_ENCODING_DEFAULT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_DEFAULT");
+                       break;
+               case GMIME_CONTENT_ENCODING_7BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_7BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_8BIT:
+                       EM_DEBUG_LOG("Encoding:ENCODING_8BIT");
+                       break;
+               case GMIME_CONTENT_ENCODING_BINARY:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BINARY");
+                       break;
+               case GMIME_CONTENT_ENCODING_BASE64:
+                       EM_DEBUG_LOG("Encoding:ENCODING_BASE64");
+                       break;
+               case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_QUOTEDPRINTABLE");
+                       break;
+               case GMIME_CONTENT_ENCODING_UUENCODE:
+                       EM_DEBUG_LOG("Encoding:ENCODING_UUENCODE");
+                       break;
+
+               default:
+                       break;
+               }
+
+               GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
+               if (data) {
+                       int fd = 0;
+                       GMimeStream *out_stream = NULL;
+                       GMimeStream *src_stream = NULL;
+
+                       fd = open(content_path, O_WRONLY|O_CREAT, 0644);
+                       if (fd < 0) {
+                               EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+                               goto FINISH_OFF;
+                       }
+
+                       out_stream = g_mime_stream_fs_new(fd);
+                       src_stream = g_mime_data_wrapper_get_stream(data);
+
+                       if (src_stream)
+                               EM_DEBUG_LOG_DEV("Data length [%d]", g_mime_stream_length(src_stream));
+
+                       /*g_mime_data_wrapper_set_stream(data, out_stream);
+                       g_mime_data_wrapper_set_encoding(data, enc);*/
+
+                       g_mime_data_wrapper_write_to_stream(data, out_stream);
+                       if (out_stream) g_object_unref(out_stream);
+                       emcore_get_file_size(content_path, &real_size, NULL);
+               }
+               else
+               {
+                       int fd = 0;
+                       fd = open(content_path, O_WRONLY|O_CREAT, 0644);
+                       if (fd < 0) {
+                               EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+                               goto FINISH_OFF;
+                       }
+                       close(fd);
+               }
+               /*Content - END*/
+
+               ctype = g_mime_object_get_content_type(mobject);
+               tmp_path = (char *)g_mime_content_type_get_parameter(ctype, "tmp_content_path");
+               if (tmp_path) {
+                       g_remove(tmp_path);
+               }
+
+               if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
+                       /*TEXT*/
+                       EM_DEBUG_LOG("TEXT");
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "text") == 0) {
+                               if (!ctype_charset || g_ascii_strcasecmp(ctype_charset, "X-UNKNOWN") == 0) {
+                                       ctype_charset = "UTF-8";
+                               }
+
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "plain") == 0) {
+                                       EM_DEBUG_LOG("TEXT/PLAIN");
+
+                                       char *file_content = NULL;
+                                       int content_size = 0;
+
+                                       if (emcore_get_content_from_file(content_path, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                                       }
+
+                                       if (file_content && content_size > 0) {
+                                               char escape = 0x1b;
+                                               char detector[25] = {0,};
+                                               snprintf(detector, sizeof(detector), "%c$B", escape);
+                                               if (g_strrstr(ctype_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                                                       ctype_charset = "ISO-2022-JP";
+                                               }
+                                       }
+
+                                       EM_SAFE_FREE(file_content);
+                                       EM_SAFE_FREE(cnt_info->text.plain_charset);
+                                       EM_SAFE_FREE(cnt_info->text.plain);
+
+                                       cnt_info->text.plain_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.plain = g_strdup(content_path);
+                               } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "html") == 0) {
+                                       EM_DEBUG_LOG("TEXT/HTML");
+
+                                       EM_SAFE_FREE(cnt_info->text.html_charset);
+                                       EM_SAFE_FREE(cnt_info->text.html);
+
+                                       cnt_info->text.html_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.html = g_strdup(content_path);
+                               }
+                       }
+               } else if (content_disposition_type == INLINE_ATTACHMENT) {
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->inline_file;
+                       char *utf8_text = NULL;
+                       char *file_content = NULL;
+                       int file_size = 0;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       file->content_id = g_strdup(content_id);
+
+                       if (file->name && g_strrstr(file->name, "/") != NULL) {
+                               char *tmp_ptr = file->name;
+                               int tmp_len = EM_SAFE_STRLEN(file->name);
+                               int tmpi = 0;
+                               for (tmpi=0; tmpi<tmp_len; tmpi++) {
+                                       if (*(tmp_ptr+tmpi) == '/') {
+                                               *(tmp_ptr+tmpi) = '_';
+                                       }
+                               }
+                               EM_DEBUG_LOG_SEC("file->name[%s]", file->name);
+                       }
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       /* check inline name duplication */
+                       char *modified_name= NULL;
+                       if (emcore_gmime_check_filename_duplication(file->name, cnt_info)) {
+                               modified_name = emcore_gmime_get_modified_filename_in_duplication(file->name);
+                               EM_SAFE_FREE(file->name);
+                               file->name = modified_name;
+                       }
+
+                       emcore_unescape_from_url(file->name, &modified_name);
+                       EM_DEBUG_LOG_SEC("file->name[%s] modified_name[%s]", file->name, modified_name);
+                       EM_SAFE_FREE(file->name);
+                       file->name = modified_name;
+
+                       /* cid replacement for inline attachment */
+                       if (emcore_get_content_from_file(cnt_info->text.html, &file_content, &file_size) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+                       }
+
+                       if (file_content && file_size > 0) {
+                               EM_DEBUG_LOG_SEC("content_id[%s] name[%s]", file->content_id, file->name);
+                               em_replace_string_ex(&file_content, "cid:", "");
+                               em_replace_string_ex(&file_content, file->content_id, file->name);
+
+                               file_size = EM_SAFE_STRLEN(file_content);
+                               if (emcore_set_content_to_file(file_content, cnt_info->text.html, file_size) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emcore_set_content_to_file failed");
+                               }
+                       }
+                       g_free(file_content); /* prevent 39110 */
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(content_path);
+                       file->size = real_size;
+                       file->save_status = 1;
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->inline_file = file;
+                       else
+                               temp_file->next = file;
+               } else if (content_disposition_type == ATTACHMENT) {
+
+                       struct attachment_info *file = NULL;
+                       struct attachment_info *temp_file = cnt_info->file;
+                       char *utf8_text = NULL;
+                       int err = EMAIL_ERROR_NONE;
+
+                       file = em_malloc(sizeof(struct attachment_info));
+                       if (file == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               goto FINISH_OFF;
+                       }
+
+                       file->type = content_disposition_type;
+                       if (disposition_filename) file->name = g_strdup(disposition_filename);
+                       else if (ctype_name) file->name = g_strdup(ctype_name);
+                       else if (content_id) file->name = g_strdup(content_id);
+                       else file->name = g_strdup("unknown-attachment");
+
+                       file->content_id = g_strdup(content_id);
+
+                       if (!(utf8_text = g_mime_utils_header_decode_text(file->name))) {
+                               EM_DEBUG_EXCEPTION("g_mime_utils_header_decode_text failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
+
+                       if (utf8_text) {
+                               EM_SAFE_FREE(file->name);
+                               file->name = EM_SAFE_STRDUP(utf8_text);
+                       }
+                       EM_SAFE_FREE(utf8_text);
+
+                       if(ctype_type && ctype_subtype) {
+                               char mime_type_buffer[128] = {0,};
+                               snprintf(mime_type_buffer, sizeof(mime_type_buffer), "%s/%s", ctype_type, ctype_subtype);
+                               file->attachment_mime_type = g_strdup(mime_type_buffer);
+                       }
+
+                       file->save = g_strdup(content_path);
+                       if (real_size == 0 && ctype_size)
+                               file->size = atoi(ctype_size);
+                       else
+                               file->size = real_size;
+                       file->save_status = 1;
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "APPLICATION") == 0) {
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_OBJECT) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_OBJECT;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_RIGHTS) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_RIGHTS;
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, MIME_SUBTYPE_DRM_DCF) == 0)
+                                       file->drm = EMAIL_ATTACHMENT_DRM_DCF;
+                       }
+
+                       while (temp_file && temp_file->next)
+                               temp_file = temp_file->next;
+
+                       if (temp_file == NULL)
+                               cnt_info->file = file;
+                       else
+                               temp_file->next = file;
+               }
+
+               EM_SAFE_FREE(content_path);
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(content_path);
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       int error = EMAIL_ERROR_NONE;
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               GMimeMessage *message = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               char *msg_ctype_type = NULL;
+               char *msg_ctype_subtype = NULL;
+               char *msg_ctype_size = NULL;
+               char *msg_disposition_str = NULL;
+               unsigned long msg_content_size = 0;
+               EM_DEBUG_LOG("Message Part");
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       //goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_type = (char *)g_mime_content_type_get_media_type(msg_ctype);
+               msg_ctype_subtype = (char *)g_mime_content_type_get_media_subtype(msg_ctype);
+               msg_ctype_size = (char *)g_mime_content_type_get_parameter(msg_ctype, "message_size");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", msg_ctype_type, msg_ctype_subtype);
+               EM_DEBUG_LOG("Part.size.bytes[%s]", msg_ctype_size);
+
+               if (msg_ctype_size) msg_content_size = atol(msg_ctype_size);
+               cnt_info->total_mail_size += msg_content_size;
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               /*Content Disposition - END*/
+
+               if (msg_disposition_str && g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       EM_DEBUG_LOG("RFC822/Message is ATTACHMENT");
+                       cnt_info->total_attachment_size += msg_content_size;
+               }
+
+               //g_mime_message_foreach(message, emcore_gmime_imap_parse_bodystructure_foreach_cb, user_data);
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+               //TODO
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+               GMimeMultipart *multi_part = NULL;
+               multi_part = (GMimeMultipart *)part;
+
+               int multi_count = g_mime_multipart_get_count(multi_part);
+               EM_DEBUG_LOG("Multi Part Count:%d", multi_count);
+               EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part));
+
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_disposition_type = 0;
+               char *content_id = NULL;
+               char *content_location = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_charset = NULL;
+               char *ctype_name = NULL;
+               char *ctype_size = NULL;
+               unsigned long content_size = 0;
+
+               GMimeContentType *ctype = NULL;
+               GMimeContentDisposition *disposition = NULL;
+               GMimePart *leaf_part = NULL;
+               GMimeObject *mobject = (GMimeObject *)part;
+               leaf_part = (GMimePart *)part;
+
+               /*Content Type*/
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_charset = (char *)g_mime_content_type_get_parameter(ctype, "charset");
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               ctype_size = (char *)g_mime_content_type_get_parameter(ctype, "part_size");
+               EM_DEBUG_LOG("Content-Type[%s/%s]", ctype_type, ctype_subtype);
+               EM_DEBUG_LOG("Content-Type-Charset[%s]", ctype_charset);
+               EM_DEBUG_LOG("Content-Type-Name[%s]", ctype_name);
+               EM_DEBUG_LOG("Part.size.bytes[%s]", ctype_size);
+
+               if (ctype_size) content_size = atol(ctype_size);
+               cnt_info->total_mail_size += content_size;
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               content_id = (char *)g_mime_object_get_content_id(mobject);
+               EM_DEBUG_LOG_SEC("Content-ID:%s", content_id);
+
+               /*Content Location*/
+               content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) */
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       cnt_info->total_attachment_size += content_size;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               cnt_info->total_attachment_size += content_size;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               cnt_info->total_body_size += content_size;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       cnt_info->total_body_size += content_size;
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               cnt_info->total_attachment_size += content_size;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               cnt_info->total_body_size += content_size;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       cnt_info->total_body_size += content_size;
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       cnt_info->total_attachment_size += content_size;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) {
+                       /*TEXT*/
+                       EM_DEBUG_LOG("TEXT");
+
+                       if (ctype_type && g_ascii_strcasecmp(ctype_type, "text") == 0) {
+                               char *tmp_file = NULL;
+
+                               if (!ctype_charset || g_ascii_strcasecmp(ctype_charset, "X-UNKNOWN") == 0) {
+                                       ctype_charset = "UTF-8";
+                               }
+
+                               if (!emcore_get_temp_file_name(&tmp_file, &error) || !tmp_file) {
+                                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", error);
+                                       EM_SAFE_FREE(tmp_file);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "plain") == 0) {
+                                       EM_DEBUG_LOG("TEXT/PLAIN");
+                                       cnt_info->text.plain_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.plain = tmp_file;
+                               }
+                               else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "html") == 0) {
+                                       EM_DEBUG_LOG("TEXT/HTML");
+                                       cnt_info->text.html_charset = g_strdup(ctype_charset);
+                                       cnt_info->text.html = tmp_file;
+                               }
+                       }
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END();
+}
+
+
+INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+       char sections[IMAP_MAX_COMMAND_LENGTH] = {0,};
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               EM_DEBUG_LOG("Message Part");
+
+               if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT)) {
+                       EM_DEBUG_LOG("grab_type is not GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT");
+                       goto FINISH_OFF;
+               }
+
+               GMimeMessage *message = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_ctype_section = NULL;
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       //goto FINISH_OFF;
+               }
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               /*Content Disposition - END*/
+
+               if (!msg_disposition_str || g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) != 0) {
+                       goto FINISH_OFF;
+               }
+
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_section = (char *)g_mime_content_type_get_parameter(msg_ctype, "section");
+               EM_DEBUG_LOG("section[%s]", msg_ctype_section);
+
+               if (!msg_ctype_section) {
+                       EM_DEBUG_LOG("section is NULL");
+                       goto FINISH_OFF;
+               }
+
+               snprintf(sections, sizeof(sections), "BODY.PEEK[%s]", msg_ctype_section);
+
+               EM_DEBUG_LOG("sections <%s>", sections);
+
+               if (cnt_info->sections) {
+                       char *tmp_str = NULL;
+                       tmp_str = g_strconcat(cnt_info->sections, " ", sections, NULL);
+
+                       if (tmp_str) {
+                               EM_SAFE_FREE(cnt_info->sections);
+                               cnt_info->sections = tmp_str;
+                       }
+               }
+               else {
+                       cnt_info->sections = EM_SAFE_STRDUP(sections);
+               }
+
+               EM_DEBUG_LOG("sections <%s>", cnt_info->sections);
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_disposition_type = 0;
+               char *content_id = NULL;
+               char *content_location = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_name = NULL;
+               char *ctype_section = NULL;
+
+               GMimeContentType *ctype = NULL;
+               GMimeContentDisposition *disposition = NULL;
+               GMimePart *leaf_part = NULL;
+               GMimeObject *mobject = (GMimeObject *)part;
+               leaf_part = (GMimePart *)part;
+
+               /*Content Type*/
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               ctype_section = (char *)g_mime_content_type_get_parameter(ctype, "section");
+               EM_DEBUG_LOG("Content-Type-Name[%s]", ctype_name);
+               EM_DEBUG_LOG("Content-Type-Section[%s]", ctype_section);
+
+               if (!ctype_section) {
+                       EM_DEBUG_LOG("section is NULL");
+                       goto FINISH_OFF;
+               }
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG_SEC("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               content_id = (char *)g_mime_object_get_content_id(mobject);
+               EM_DEBUG_LOG_SEC("Content-ID:%s", content_id);
+
+               /*Content Location*/
+               content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) */
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               if (content_disposition_type == ATTACHMENT) {
+                       EM_DEBUG_LOG("ATTACHMENT");
+
+                       if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT))
+                               goto FINISH_OFF;
+               }
+               else {
+                       snprintf(sections, sizeof(sections), "BODY.PEEK[%s.MIME] BODY.PEEK[%s]", ctype_section, ctype_section);
+
+                       if (cnt_info->sections) {
+                               char *tmp_str = NULL;
+                               tmp_str = g_strconcat(cnt_info->sections, " ", sections, NULL);
+
+                               if (tmp_str) {
+                                       EM_SAFE_FREE(cnt_info->sections);
+                                       cnt_info->sections = tmp_str;
+                               }
+                       }
+                       else {
+                               cnt_info->sections = EM_SAFE_STRDUP(sections);
+                       }
+
+                       EM_DEBUG_LOG("sections <%s>", cnt_info->sections);
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END();
+}
+
+
+INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data);
+
+       struct _m_content_info *cnt_info = (struct _m_content_info *)user_data;
+       char sections[IMAP_MAX_COMMAND_LENGTH] = {0,};
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               EM_DEBUG_LOG("Message Part");
+
+               if (cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) {
+                       EM_DEBUG_LOG("grab_type is not GRAB_TYPE_ATTACHMENT");
+                       goto FINISH_OFF;
+               }
+
+               GMimeMessage *message = NULL;
+               GMimeContentDisposition *msg_disposition = NULL;
+               GMimeContentType *msg_ctype = NULL;
+               char *msg_disposition_str = NULL;
+               char *msg_ctype_section = NULL;
+
+               message = g_mime_message_part_get_message((GMimeMessagePart *)part);
+               if (!message) {
+                       EM_DEBUG_EXCEPTION("Message is NULL");
+                       //goto FINISH_OFF;
+               }
+
+               /*Content Disposition*/
+               msg_disposition = g_mime_object_get_content_disposition(part);
+               if (msg_disposition) {
+                       msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition);
+               }
+               EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str);
+               /*Content Disposition - END*/
+
+               if (!msg_disposition_str || g_ascii_strcasecmp(msg_disposition_str, GMIME_DISPOSITION_ATTACHMENT) != 0) {
+                       goto FINISH_OFF;
+               }
+
+               if (--cnt_info->file_no != 0)
+                       goto FINISH_OFF;
+
+               msg_ctype = g_mime_object_get_content_type(part);
+               msg_ctype_section = (char *)g_mime_content_type_get_parameter(msg_ctype, "section");
+               EM_DEBUG_LOG("section[%s]", msg_ctype_section);
+
+               if (!msg_ctype_section) {
+                       EM_DEBUG_LOG("section is NULL");
+                       goto FINISH_OFF;
+               }
+
+               snprintf(sections, sizeof(sections), "%s", msg_ctype_section);
+
+               EM_DEBUG_LOG("sections <%s>", sections);
+
+               if (!cnt_info->sections) {
+                       cnt_info->sections = EM_SAFE_STRDUP(sections);
+               }
+
+               EM_DEBUG_LOG("sections <%s>", cnt_info->sections);
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+               int content_disposition_type = 0;
+               char *content_id = NULL;
+               char *content_location = NULL;
+               char *disposition_str = NULL;
+               char *disposition_filename = NULL;
+               char *ctype_type = NULL;
+               char *ctype_subtype = NULL;
+               char *ctype_name = NULL;
+               char *ctype_section = NULL;
+
+               GMimeContentType *ctype = NULL;
+               GMimeContentDisposition *disposition = NULL;
+               GMimePart *leaf_part = NULL;
+               GMimeObject *mobject = (GMimeObject *)part;
+               leaf_part = (GMimePart *)part;
+
+               if (cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) {
+                       EM_DEBUG_LOG("grab_type is not GRAB_TYPE_ATTACHMENT");
+                       goto FINISH_OFF;
+               }
+
+               /*Content Type*/
+               ctype = g_mime_object_get_content_type(mobject);
+               ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+               ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+               ctype_name = (char *)g_mime_content_type_get_parameter(ctype, "name");
+               ctype_section = (char *)g_mime_content_type_get_parameter(ctype, "section");
+               EM_DEBUG_LOG("Content-Type-Name[%s]", ctype_name);
+               EM_DEBUG_LOG("Content-Type-Section[%s]", ctype_section);
+
+               if (!ctype_section) {
+                       EM_DEBUG_LOG("section is NULL");
+                       goto FINISH_OFF;
+               }
+               /*Content Type - END*/
+
+               /*Content Disposition*/
+               disposition = g_mime_object_get_content_disposition(mobject);
+               if (disposition) {
+                       disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       disposition_filename = (char *)g_mime_content_disposition_get_parameter(disposition, "filename");
+               }
+               EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+               EM_DEBUG_LOG("Disposition-Filename[%s]", disposition_filename);
+               /*Content Disposition - END*/
+
+               /*Content ID*/
+               content_id = (char *)g_mime_object_get_content_id(mobject);
+               EM_DEBUG_LOG_SEC("Content-ID:%s", content_id);
+
+               /*Content Location*/
+               content_location = (char *)g_mime_part_get_content_location(leaf_part);
+               EM_DEBUG_LOG_SEC("Content-Location:%s", content_location);
+
+               /*Figure out TEXT or ATTACHMENT(INLINE) */
+               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                       content_disposition_type = ATTACHMENT;
+                       EM_DEBUG_LOG("ATTACHMENT");
+               } else if ((content_id || content_location) && (ctype_name || disposition_filename)) {
+                       if (cnt_info->attachment_only) {
+                               content_disposition_type = ATTACHMENT;
+                               EM_DEBUG_LOG("ATTACHMENT");
+                       } else {
+                               content_disposition_type = INLINE_ATTACHMENT;
+                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                       }
+               } else {
+                       if (content_id || content_location) {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       if (cnt_info->attachment_only) {
+                                               content_disposition_type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       } else {
+                                               content_disposition_type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       }
+                               }
+                       } else {
+                               if (g_ascii_strcasecmp(ctype_type, "text") == 0 &&
+                                               (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) {
+                                       EM_DEBUG_LOG("TEXT");
+                               } else {
+                                       content_disposition_type = ATTACHMENT;
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               }
+                       }
+               }
+
+               if (content_disposition_type == ATTACHMENT) {
+                       EM_DEBUG_LOG("ATTACHMENT");
+
+                       if (--cnt_info->file_no != 0)
+                               goto FINISH_OFF;
+
+                       snprintf(sections, sizeof(sections), "%s", ctype_section);
+
+                       if (!cnt_info->sections) {
+                               cnt_info->sections = EM_SAFE_STRDUP(sections);
+                       }
+
+                       EM_DEBUG_LOG("sections <%s>", cnt_info->sections);
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_search_section_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       search_section *search_info = (search_section *)user_data;
+       GMimeContentType *ctype = NULL;
+        char buf[255] = {0};
+       char *ctype_section = NULL;
+
+       if (!search_info) {
+               EM_DEBUG_LOG("search_info is NULL");
+               goto FINISH_OFF;
+       }
+
+       if (!part) {
+               EM_DEBUG_LOG("part is NULL");
+               goto FINISH_OFF;
+       }
+
+       if (GMIME_IS_MESSAGE_PART(part)) {
+               EM_DEBUG_LOG("Message Part");
+       } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
+               EM_DEBUG_LOG("Partial Part");
+       } else if (GMIME_IS_MULTIPART(part)) {
+               EM_DEBUG_LOG("Multi Part");
+       } else if (GMIME_IS_PART(part)) {
+               EM_DEBUG_LOG("Part");
+       }
+
+       ctype = g_mime_object_get_content_type(part);
+       ctype_section = (char *)g_mime_content_type_get_parameter(ctype, "section");
+       EM_DEBUG_LOG("section[%s]", ctype_section);
+
+       if (!ctype_section) {
+               EM_DEBUG_LOG("section is NULL");
+               goto FINISH_OFF;
+       }
+
+        SNPRINTF(buf, sizeof(buf), "%s.MIME", ctype_section);
+
+       if (g_ascii_strcasecmp(ctype_section, search_info->section) == 0) {
+               EM_DEBUG_LOG("found section");
+               if (!(search_info->section_object)) search_info->section_object = part;
+        } else if (g_ascii_strcasecmp(search_info->section, buf) == 0) {
+                EM_DEBUG_LOG("Mime header");
+                if (!(search_info->section_object)) search_info->section_object = part;
+        }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart,
+               BODY *body, const char *spec, int *total_mail_size)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       PART *part = NULL;
+       GMimeObject *subpart = NULL;
+       GMimeMultipart *sub_multipart = NULL;
+       GMimeMessagePart *sub_messagepart = NULL;
+       GMimeMessage *sub_message = NULL;
+       GMimePart *sub_part = NULL;
+       char *subspec = NULL;
+       char *id = NULL;
+       char *section = NULL;
+       int i = 1;
+
+       if (!body || !multipart || !spec) {
+               EM_DEBUG_EXCEPTION("body[%p], multipart[%p], spec[%p]", body, multipart, spec);
+               return;
+       }
+
+       subspec = g_alloca (strlen(spec) + 14);
+       id = g_stpcpy(subspec, spec);
+       *id++ = '.';
+
+       EM_DEBUG_LOG("constructing a %s/%s part (%s)", body_types[body->type],
+               body->subtype, spec);
+
+       /* checkout boundary */
+       if (body->parameter) {
+               PARAMETER *param = body->parameter;
+               while(param) {
+                       EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                       param = param->next;
+               }
+       }
+
+       part = body->nested.part;
+
+       while (part != NULL) {
+               sprintf (id, "%d", i++);
+
+               if (EM_SAFE_STRLEN(subspec) > 2)
+                       section = EM_SAFE_STRDUP(subspec+2);
+
+               EM_DEBUG_LOG("constructing a %s/%s part (%s/%s)", body_types[part->body.type],
+                       part->body.subtype, subspec, section);
+
+               if (part->body.type == TYPEMULTIPART) {
+                       /*multipart*/
+                       char *subtype = g_ascii_strdown(part->body.subtype, -1);
+                       sub_multipart = g_mime_multipart_new_with_subtype(subtype);
+                       EM_SAFE_FREE(subtype);
+                       emcore_gmime_construct_multipart(sub_multipart, &(part->body), subspec, total_mail_size);
+
+                       subpart = GMIME_OBJECT(sub_multipart);
+               } else if (part->body.type == TYPEMESSAGE) {
+                       /*message/rfc822 or message/news*/
+                       BODY *nested_body = NULL;
+                       MESSAGE *nested_message = NULL;
+                       char *subtype = g_ascii_strdown(part->body.subtype, -1);
+                       sub_messagepart = g_mime_message_part_new(subtype);
+                       EM_SAFE_FREE(subtype);
+
+                       subpart = GMIME_OBJECT(sub_messagepart);
+
+                       if (part->body.size.bytes > 0) {
+                               char size_str[sizeof(unsigned long)*8+1] = {0,};
+                               snprintf(size_str, sizeof(size_str), "%lu", part->body.size.bytes);
+                               g_mime_object_set_content_type_parameter(subpart, "message_size", size_str);
+                       }
+
+                       nested_message = part->body.nested.msg;
+                       if (nested_message) nested_body = nested_message->body;
+
+                       if (!emcore_gmime_construct_mime_part_with_bodystructure(nested_body, &sub_message, subspec, NULL)) {
+                               EM_DEBUG_EXCEPTION("emcore_gmime_construct_mime_part_with_bodystructure failed");
+                       } else {
+                               g_mime_message_part_set_message(sub_messagepart, sub_message);
+                               if (sub_message) g_object_unref(sub_message);
+                       }
+               } else {
+                       /*other parts*/
+                       char *type = g_ascii_strdown(body_types[part->body.type], -1);
+                       char *subtype = g_ascii_strdown(part->body.subtype, -1);
+                       sub_part = g_mime_part_new_with_type(type, subtype);
+                       EM_SAFE_FREE(type);
+                       EM_SAFE_FREE(subtype);
+                       subpart = GMIME_OBJECT(sub_part);
+
+                       if (total_mail_size != NULL) {
+                               *total_mail_size = *total_mail_size + (int)part->body.size.bytes;
+                               EM_DEBUG_LOG("body.size.bytes [%d]", part->body.size.bytes);
+                               EM_DEBUG_LOG("total_mail_size [%d]", *total_mail_size);
+                       }
+
+                       /* encoding */
+                       if (part->body.encoding <= 5) {
+                               EM_DEBUG_LOG("Content Encoding: %s", body_encodings[part->body.encoding]);
+                               GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_DEFAULT;
+                               switch(part->body.encoding) {
+                               case ENC7BIT:
+                                       encoding = GMIME_CONTENT_ENCODING_7BIT;
+                                       break;
+                               case ENC8BIT:
+                                       encoding = GMIME_CONTENT_ENCODING_8BIT;
+                                       break;
+                               case ENCBINARY:
+                                       encoding = GMIME_CONTENT_ENCODING_BINARY;
+                                       break;
+                               case ENCBASE64:
+                                       encoding = GMIME_CONTENT_ENCODING_BASE64;
+                                       break;
+                               case ENCQUOTEDPRINTABLE:
+                                       encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
+                                       break;
+                               case ENCOTHER:
+                                       encoding = GMIME_CONTENT_ENCODING_DEFAULT;
+                                       break;
+
+                               default:
+                                       break;
+                               }
+                               g_mime_part_set_content_encoding(sub_part, encoding);
+                       }
+
+                       /* content description */
+                       if (part->body.description) {
+                               EM_DEBUG_LOG("Content-Description: %s", part->body.description);
+                               g_mime_part_set_content_description(sub_part, part->body.description);
+                       }
+
+                       /* content location */
+                       if (part->body.location) {
+                               EM_DEBUG_LOG_SEC("Content-Location: %s", part->body.location);
+                               g_mime_part_set_content_location(sub_part, part->body.location);
+                       }
+
+                       /* md5 */
+                       if (part->body.md5) {
+                               EM_DEBUG_LOG("Content-MD5: %s", part->body.md5);
+                               g_mime_part_set_content_md5(sub_part, part->body.md5);
+                       }
+
+                       if (part->body.size.bytes > 0) {
+                               char size_str[sizeof(unsigned long)*8+1] = {0,};
+                               snprintf(size_str, sizeof(size_str), "%lu", part->body.size.bytes);
+                               g_mime_object_set_content_type_parameter(subpart, "part_size", size_str);
+                       }
+               }
+
+               /* content type */
+               if (part->body.parameter) {
+                       PARAMETER *param = part->body.parameter;
+                       while(param) {
+                           EM_DEBUG_LOG_SEC("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                               if (param->attribute || param->value) 
+                                       g_mime_object_set_content_type_parameter(subpart, param->attribute, param->value);
+                               param = param->next;
+                       }
+               }
+               g_mime_object_set_content_type_parameter(subpart, "section", section);
+
+               /* content disposition */
+               if (part->body.disposition.type) {
+                       EM_DEBUG_LOG("Content-Disposition: %s", part->body.disposition.type);
+                       char *disposition_type = g_ascii_strdown(part->body.disposition.type, -1);
+                       g_mime_object_set_disposition(subpart, disposition_type);
+                       g_free(disposition_type);
+               }
+
+               if (part->body.disposition.parameter) {
+                       PARAMETER *param = part->body.disposition.parameter;
+                       while(param) {
+                           EM_DEBUG_LOG_SEC("Content-Disposition Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                               if (param->attribute || param->value)
+                                       g_mime_object_set_content_disposition_parameter(subpart, param->attribute, param->value);
+                               param = param->next;
+                       }
+               }
+
+               /* content id */
+               if (part->body.id) {
+                       EM_DEBUG_LOG_SEC("Content-ID: %s", part->body.id);
+                       int i = 0;
+                       char *cid = EM_SAFE_STRDUP(part->body.id);
+                       g_strstrip(cid);
+
+                       while (strlen(cid) > 0 && cid[i] != '\0') {
+                               if (cid[i] == '<' || cid[i] == '>')
+                                       cid[i] = ' ';
+                               i++;
+                       }
+
+                       g_strstrip(cid);
+                       EM_DEBUG_LOG_DEV("Content-ID stripped: %s", cid);
+                       g_mime_object_set_content_id(subpart, cid);
+                       EM_SAFE_FREE(cid);
+               }
+
+               g_mime_multipart_add (multipart, subpart);
+
+               if (subpart) {
+                       g_object_unref (subpart);
+                       subpart = NULL;
+               }
+
+               EM_SAFE_FREE(section);
+
+               part = part->next;
+       }
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part,
+               BODY *body, const char *spec, int *total_mail_size)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       GMimeObject *part_object = NULL;
+
+       if (!body || !part || !spec) {
+               EM_DEBUG_EXCEPTION("body[%p], part[%p] spec[%p]", body, part, spec);
+               return;
+       }
+
+       part_object = GMIME_OBJECT(part);
+
+       if (total_mail_size != NULL) {
+               *total_mail_size = (int)(body->size.bytes);
+               EM_DEBUG_LOG("total_mail_size [%d]", *total_mail_size);
+       }
+
+       GMimeContentType *ctype = NULL;
+       char *ctype_type = NULL;
+       char *ctype_subtype = NULL;
+       ctype = g_mime_object_get_content_type(GMIME_OBJECT(part));
+       ctype_type = (char *)g_mime_content_type_get_media_type(ctype);
+       ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype);
+
+       /* Type-Subtype */
+       if (g_strcmp0(ctype_type, "text") == 0 && g_strcmp0(ctype_subtype, "plain") == 0 &&
+                       body->type >= 0 && body->subtype) {
+               GMimeContentType *content_type = NULL;
+               char *type = g_ascii_strdown(body_types[body->type], -1);
+               char *subtype = g_ascii_strdown(body->subtype, -1);
+               EM_DEBUG_LOG("Content Type: [%s/%s]", type, subtype);
+
+               content_type = g_mime_content_type_new(type, subtype);
+               g_mime_object_set_content_type(GMIME_OBJECT(part), content_type);
+               g_object_unref(content_type);
+
+               EM_SAFE_FREE(type);
+               EM_SAFE_FREE(subtype);
+       }
+
+       /* encoding */
+       if (body->encoding <= 5) {
+               EM_DEBUG_LOG("Content Encoding: %s", body_encodings[body->encoding]);
+               GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_DEFAULT;
+               switch(body->encoding) {
+               case ENC7BIT:
+                       encoding = GMIME_CONTENT_ENCODING_7BIT;
+                       break;
+               case ENC8BIT:
+                       encoding = GMIME_CONTENT_ENCODING_8BIT;
+                       break;
+               case ENCBINARY:
+                       encoding = GMIME_CONTENT_ENCODING_BINARY;
+                       break;
+               case ENCBASE64:
+                       encoding = GMIME_CONTENT_ENCODING_BASE64;
+                       break;
+               case ENCQUOTEDPRINTABLE:
+                       encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
+                       break;
+               case ENCOTHER:
+                       encoding = GMIME_CONTENT_ENCODING_DEFAULT;
+                       break;
+
+               default:
+                       break;
+               }
+               g_mime_part_set_content_encoding(part, encoding);
+       }
+
+       /* content description */
+       if (body->description) {
+               EM_DEBUG_LOG("Content-Description: %s", body->description);
+               g_mime_part_set_content_description(part, body->description);
+       }
+
+       /* content location */
+       if (body->location) {
+               EM_DEBUG_LOG_SEC("Content-Location: %s", body->location);
+               g_mime_part_set_content_location(part, body->location);
+       }
+
+       /* md5 */
+       if (body->md5) {
+               EM_DEBUG_LOG("Content-MD5: %s", body->md5);
+               g_mime_part_set_content_md5(part, body->md5);
+       }
+
+       /* content type */
+       if (body->parameter) {
+               PARAMETER *param = body->parameter;
+               while(param) {
+                       EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                       if (param->attribute || param->value)
+                               g_mime_object_set_content_type_parameter(part_object, param->attribute, param->value);
+                       param = param->next;
+               }
+       }
+
+       if (body->size.bytes > 0) {
+               char size_str[sizeof(unsigned long)*8+1] = {0,};
+               snprintf(size_str, sizeof(size_str), "%lu", body->size.bytes);
+               g_mime_object_set_content_type_parameter(part_object, "part_size", size_str);
+       }
+
+       if (g_strcmp0(spec, "1") == 0)
+               g_mime_object_set_content_type_parameter(part_object, "section", spec);
+       else {
+               if (EM_SAFE_STRLEN(spec) > 2) {
+                       char *tmp = EM_SAFE_STRDUP(spec+2);
+                       g_mime_object_set_content_type_parameter(part_object, "section", tmp);
+                       EM_SAFE_FREE(tmp);
+               }
+       }
+
+       /* content disposition */
+       if (body->disposition.type) {
+               EM_DEBUG_LOG("Content-Disposition: %s", body->disposition.type);
+               char *disposition_type = g_ascii_strdown(body->disposition.type, -1);
+               g_mime_object_set_disposition(part_object, disposition_type);
+               g_free(disposition_type);
+       }
+
+       if (body->disposition.parameter) {
+               PARAMETER *param = body->disposition.parameter;
+               while(param) {
+                       EM_DEBUG_LOG("Content-Disposition Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                       if (param->attribute || param->value)
+                               g_mime_object_set_content_disposition_parameter(part_object, param->attribute, param->value);
+                       param = param->next;
+               }
+       }
+
+       /* content id */
+       if (body->id) {
+               EM_DEBUG_LOG("Content-ID: %s", body->id);
+               int i = 0;
+               char *cid = EM_SAFE_STRDUP(body->id);
+               g_strstrip(cid);
+
+               while (strlen(cid) > 0 && cid[i] != '\0') {
+                       if (cid[i] == '<' || cid[i] == '>')
+                               cid[i] = ' ';
+                       i++;
+               }
+
+               g_strstrip(cid);
+               EM_DEBUG_LOG_DEV("Content-ID stripped: %s", cid);
+               g_mime_object_set_content_id(part_object, cid);
+               EM_SAFE_FREE(cid);
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emcore_gmime_construct_mime_part_with_bodystructure(BODY *mbody,
+               GMimeMessage **message, const char *spec, int *total_mail_size)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       GMimeMessage *message1 = NULL;
+       GMimeObject *mime_part = NULL;
+       GMimeMultipart *multipart = NULL;
+       GMimePart *singlepart = NULL;
+       int total_size = 0;
+       int ret = FALSE;
+
+       if (!mbody || !message) {
+               EM_DEBUG_EXCEPTION("body[%p], message[%p]", mbody, message);
+               return ret;
+       }
+
+       message1 = g_mime_message_new(FALSE);
+
+       /* Construct mime_part of GMimeMessage */
+       if (g_ascii_strcasecmp(body_types[mbody->type], "multipart") == 0) {
+               /* checkout boundary */
+               int boundary_ok = 0;
+               if (mbody->parameter) {
+                       PARAMETER *param = mbody->parameter;
+                       while(param) {
+                               EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value);
+                               if (g_ascii_strcasecmp(param->attribute, "boundary") == 0 && param->value)
+                                       boundary_ok = 1;
+                               param = param->next;
+                       }
+               }
+
+               if (!boundary_ok) {
+                       EM_DEBUG_EXCEPTION("boundary is not exist in bodystructure");
+                       goto FINISH_OFF;
+               }
+
+               char *subtype = g_ascii_strdown(mbody->subtype, -1);
+               multipart = g_mime_multipart_new_with_subtype(subtype);
+
+               /* Fill up mime part of message1 using bodystructure info */
+               emcore_gmime_construct_multipart(multipart, mbody, spec, &total_size);
+
+               mime_part = GMIME_OBJECT(multipart);
+               EM_SAFE_FREE(subtype);
+       }
+       else {
+               char *type = g_ascii_strdown(body_types[mbody->type], -1);
+               char *subtype = g_ascii_strdown(mbody->subtype, -1);
+               singlepart = g_mime_part_new_with_type(type, subtype);
+
+               emcore_gmime_construct_part(singlepart, mbody, spec, &total_size);
+
+               mime_part = GMIME_OBJECT(singlepart);
+               EM_SAFE_FREE(type);
+               EM_SAFE_FREE(subtype);
+       }
+
+       g_mime_message_set_mime_part(message1, mime_part);
+       if (mime_part) g_object_unref(mime_part);
+
+       ret = TRUE;
+
+FINISH_OFF:
+
+       if (total_mail_size)
+               *total_mail_size = total_size;
+
+       if (message)
+               *message = message1;
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_get_body_sections_from_message(GMimeMessage *message,
+               struct _m_content_info *cnt_info, char **sections_to_fetch)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int ret = FALSE;
+       GMimePartIter *iter1 = NULL;
+       GMimeObject *part_tmp = NULL;
+       char *part_path = NULL;
+       char sections[IMAP_MAX_COMMAND_LENGTH] = {0,};
+
+       iter1 = g_mime_part_iter_new((GMimeObject *)message);
+
+       if (!g_mime_part_iter_is_valid(iter1)) {
+               EM_DEBUG_EXCEPTION("Part iterator is not valid");
+               goto FINISH_OFF;
+       }
+
+       do {
+               part_tmp = g_mime_part_iter_get_current(iter1);
+               if (part_tmp && GMIME_IS_PART(part_tmp)) {
+                       GMimeObject *mobject = (GMimeObject *)part_tmp;
+                       GMimeContentDisposition *disposition = NULL;
+                       char *disposition_str = NULL;
+
+                       /*Content Disposition*/
+                       disposition = g_mime_object_get_content_disposition(mobject);
+                       if (disposition) {
+                               disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       }
+                       EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+                       /*Content Disposition - END*/
+
+                       part_path = g_mime_part_iter_get_path(iter1);
+
+                       if (cnt_info->grab_type == (GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT)) {
+                               char t[100] = {0,};
+                               snprintf(t, sizeof(t), "BODY.PEEK[%s] ", part_path);
+                               if (EM_SAFE_STRLEN(sections) + EM_SAFE_STRLEN(t) < sizeof(sections) - 1) {
+                                       strcat(sections, t);
+                               }
+                               else {
+                                       EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
+                                       goto FINISH_OFF;
+                               }
+                       }
+                       else if (cnt_info->grab_type == GRAB_TYPE_TEXT) {
+                               if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                                       EM_DEBUG_LOG("ATTACHMENT");
+                               } else {
+                                       char t[100] = {0,};
+                                       snprintf(t, sizeof(t), "BODY.PEEK[%s] ", part_path);
+                                       if (EM_SAFE_STRLEN(sections) + EM_SAFE_STRLEN(t) < sizeof(sections) - 1) {
+                                               strcat(sections, t);
+                                       }
+                                       else {
+                                               EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
+                                               goto FINISH_OFF;
+                                       }
+                               }
+                       }
+
+                       EM_SAFE_FREE(part_path);
+               }
+       } while (g_mime_part_iter_next(iter1));
+
+       if (iter1) {
+               g_mime_part_iter_free(iter1);
+               iter1 = NULL;
+       }
+
+       if (EM_SAFE_STRLEN(sections) <= 0 || EM_SAFE_STRLEN(sections) >= IMAP_MAX_COMMAND_LENGTH-1) {
+               EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
+               goto FINISH_OFF;
+       }
+
+       if (sections[EM_SAFE_STRLEN(sections)-1] == ' ')
+               sections[EM_SAFE_STRLEN(sections)-1] = '\0';
+
+       EM_DEBUG_LOG("sections <%s>", sections);
+
+       if (g_strlcpy(*sections_to_fetch, sections, IMAP_MAX_COMMAND_LENGTH) > 0)
+               ret = TRUE;
+
+FINISH_OFF:
+
+       if (iter1) {
+               g_mime_part_iter_free(iter1);
+               iter1 = NULL;
+       }
+
+       EM_SAFE_FREE(part_path);
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_get_attachment_section_from_message(GMimeMessage *message,
+               struct _m_content_info *cnt_info, int nth, char **section_to_fetch)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int ret = FALSE;
+       int count = nth;
+       GMimePartIter *iter1 = NULL;
+       GMimeObject *part_tmp = NULL;
+       char *part_path = NULL;
+       char sections[IMAP_MAX_COMMAND_LENGTH] = {0,};
+
+       if (nth <= 0) {
+               EM_DEBUG_EXCEPTION("parameter nth is not valid");
+               goto FINISH_OFF;
+       }
+
+       iter1 = g_mime_part_iter_new((GMimeObject *)message);
+
+       if (!g_mime_part_iter_is_valid(iter1)) {
+               EM_DEBUG_EXCEPTION("Part iterator is not valid");
+               goto FINISH_OFF;
+       }
+
+       do {
+               part_tmp = g_mime_part_iter_get_current(iter1);
+               if (part_tmp && GMIME_IS_PART(part_tmp)) {
+                       GMimeObject *mobject = (GMimeObject *)part_tmp;
+                       GMimeContentDisposition *disposition = NULL;
+                       char *disposition_str = NULL;
+
+                       /*Content Disposition*/
+                       disposition = g_mime_object_get_content_disposition(mobject);
+                       if (disposition) {
+                               disposition_str = (char *)g_mime_content_disposition_get_disposition(disposition);
+                       }
+                       EM_DEBUG_LOG("Disposition[%s]", disposition_str);
+                       /*Content Disposition - END*/
+
+                       part_path = g_mime_part_iter_get_path(iter1);
+
+                       if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) {
+                               EM_DEBUG_LOG("ATTACHMENT");
+                               count--;
+
+                               if (count == 0) {
+                                       char t[100] = {0,};
+                                       snprintf(t, sizeof(t), "%s", part_path);
+                                       if (EM_SAFE_STRLEN(sections) + EM_SAFE_STRLEN(t) < sizeof(sections) - 1) {
+                                               strcat(sections, t);
+                                       }
+                                       else {
+                                               EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
+                                               goto FINISH_OFF;
+                                       }
+                               }
+                       }
+
+                       EM_SAFE_FREE(part_path);
+               }
+       } while (g_mime_part_iter_next(iter1));
+
+       if (iter1) {
+               g_mime_part_iter_free(iter1);
+               iter1 = NULL;
+       }
+
+       EM_DEBUG_LOG("sections <%s>", sections);
+
+       if (g_strlcpy(*section_to_fetch, sections, IMAP_MAX_COMMAND_LENGTH) > 0)
+               ret = TRUE;
+
+FINISH_OFF:
+
+       if (iter1) {
+               g_mime_part_iter_free(iter1);
+               iter1 = NULL;
+       }
+
+       EM_SAFE_FREE(part_path);
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+static int emcore_gmime_get_section_n_bodysize(char *response, char *section, int *body_size)
+{
+       char *p = NULL;
+       char *s = NULL;
+       int size = 0;
+
+       if ((p = strstr(response, "BODY[")) /* || (p = strstr(s + 1, "BODY["))*/) {
+
+               p += strlen("BODY[");
+               s = p;
+
+               while (*s != ']')
+                       s++;
+
+               *s = '\0';
+               strcpy(section, p);
+
+               /* if (strcmp(section, p)) {
+                                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                                       goto FINISH_OFF;
+               }*/
+
+               p = strstr(s+1, " {");
+               if (p) {
+                       p += strlen(" {");
+                       s = p;
+
+                       while (isdigit(*s))
+                               s++;
+
+                       *s = '\0';
+
+                       size = atoi(p);
+                       *body_size = size;
+
+                       /* sending progress noti to application.
+                       1. mail_id
+                       2. file_id
+                       3. bodysize
+                       */
+               } else {
+                       return FALSE;
+               }
+       } else {
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int msg_uid, int mail_id,
+               struct _m_content_info *cnt_info, GMimeMessage *message, int event_handle, int auto_download, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], cnt_info[%p], err_code[%p]",
+                       stream, msg_uid, cnt_info, err_code);
+
+       int err = EMAIL_ERROR_NONE;
+       int ret = FALSE;
+
+       IMAPLOCAL *imaplocal = NULL;
+
+       char tag[16] = {0,};
+       char command[IMAP_MAX_COMMAND_LENGTH+100] = {0,};
+       char section[16] = {0,};
+       char *response = NULL;
+
+       int server_response_yn = 0;
+       int body_size = 0;
+
+       char *buf = NULL;
+
+       unsigned char encoded[DOWNLOAD_MAX_BUFFER_SIZE] = {0,};
+       unsigned char test_buffer[LOCAL_MAX_BUFFER_SIZE] = {0,};
+
+       int total = 0;
+       int flag_first_write = 1;
+       char *tag_position = NULL;
+
+       int part_header = 0;
+       int download_interval = 0;
+       int download_total_size = 0;
+       int downloaded_size = 0;
+       int download_progress = 0;
+       int last_notified_download_size = 0;
+
+       GMimeDataWrapper *content = NULL;
+       GMimeStream *content_stream = NULL;
+       GMimeObject *mime_object = NULL;
+       GMimePart *mime_part = NULL;
+       GMimeMessagePart *mime_message_part = NULL;
+       //GMimePartIter *mime_iter = NULL;
+       search_section *search_info = NULL;
+
+       imaplocal = stream->local;
+
+       if (!imaplocal || !imaplocal->netstream) {
+               EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       if (!cnt_info || !cnt_info->sections) {
+               EM_DEBUG_EXCEPTION("invalid parameter detected...");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
+       SNPRINTF(command, sizeof(command), "%s UID FETCH %d (%s)\015\012", tag, msg_uid, cnt_info->sections);
+       EM_DEBUG_LOG("command <%s>", command);
+
+       /* send command : get msgno/uid for all message */
+       if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
+               EM_DEBUG_EXCEPTION("net_sout failed...");
+               err = EMAIL_ERROR_CONNECTION_BROKEN;
+               goto FINISH_OFF;
+       }
+
+       while (imaplocal->netstream) {
+
+               /*  receive response */
+               if (!(response = net_getline(imaplocal->netstream))) {
+                       EM_DEBUG_EXCEPTION("net_getline failed...");
+                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG("response :%s", response);
+
+               if (strstr(response, "BODY[")) {
+
+                       if (!server_response_yn) { /* start of response */
+                               if (response[0] != '*') {
+                                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                                       EM_DEBUG_EXCEPTION("Start of response doesn't contain *");
+                                       goto FINISH_OFF;
+                               }
+                               server_response_yn = 1;
+                       }
+
+                       part_header = 0;
+                       flag_first_write = 1;
+                       total = 0;
+                       memset(encoded, 0x00, sizeof(encoded));
+
+                       if (!emcore_gmime_get_section_n_bodysize(response, section, &body_size)) {
+                               EM_DEBUG_EXCEPTION("emcore_get_section_body_size failed [%d]", err);
+                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                               goto FINISH_OFF;
+                       }
+                       EM_DEBUG_LOG("section :%s, body_size :%d", section, body_size);
+
+                       /*if (GMIME_IS_MULTIPART (message->mime_part)) {
+                               mime_iter = g_mime_part_iter_new((GMimeObject *)message);
+
+                               if (!g_mime_part_iter_is_valid(mime_iter)) {
+                                       EM_DEBUG_EXCEPTION("Part iterator is not valid");
+                                       goto FINISH_OFF;
+                               }
+
+                               if (g_mime_part_iter_jump_to(mime_iter, section)) {
+                                       EM_DEBUG_LOG("g_mime_part_iter_jump_to: %s", section);
+                                       mime_object = g_mime_part_iter_get_current(mime_iter);
+                                       if (!mime_object) {
+                                               EM_DEBUG_EXCEPTION("g_mime_part_iter_get_current failed");
+                                               goto FINISH_OFF;
+                                       }
+                                       mime_part = GMIME_PART(mime_object);
+                               } else {
+                                       EM_DEBUG_LOG("g_mime_part_iter_jump_to: failed to jump to %s", section);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (mime_iter) {
+                                       g_mime_part_iter_free(mime_iter);
+                                       mime_iter = NULL;
+                               }
+                       } else if (GMIME_IS_PART (message->mime_part)) {
+                               mime_object = message->mime_part;
+                               mime_part = GMIME_PART(mime_object);
+                       }*/
+
+                       if (search_info) {
+                               EM_SAFE_FREE(search_info->section);
+                               EM_SAFE_FREE(search_info);
+                       }
+
+                       if (!(search_info = em_malloc(sizeof(search_section)))) { /* prevent */
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
+                       }
+
+                       search_info->section = EM_SAFE_STRDUP(section);
+                       g_mime_message_foreach(message, emcore_gmime_search_section_foreach_cb, (gpointer)search_info);
+
+                       if (!(search_info->section_object)) {
+                               EM_DEBUG_EXCEPTION("section search failed");
+                               goto FINISH_OFF;
+                       }
+
+                       mime_message_part = NULL;
+                       mime_part = NULL;
+                       mime_object = search_info->section_object;
+                       if (GMIME_IS_MESSAGE_PART(mime_object)) {
+                               if (strcasestr(section, "MIME"))
+                                       part_header = 1;
+
+                       mime_message_part = GMIME_MESSAGE_PART(mime_object);
+                       } else if (GMIME_IS_PART(mime_object)) {
+                               if (strcasestr(section, "MIME"))
+                                       part_header = 1;
+
+                               mime_part = GMIME_PART(mime_object);
+                       } else {
+                               EM_DEBUG_EXCEPTION("invalid mime part type");
+                               goto FINISH_OFF;
+                       }
+
+                       if (!part_header) {
+                               if (!emcore_get_temp_file_name(&buf, &err) || !buf) {
+                                       EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               g_mime_object_set_content_type_parameter(mime_object, "tmp_content_path", buf);
+
+                               if (event_handle > 0)
+                                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+                               if (cnt_info->grab_type == GRAB_TYPE_TEXT) {
+                                       if (cnt_info->total_body_size > body_size) {
+                                               EM_DEBUG_LOG("Multipart body size is [%d]", cnt_info->total_body_size);
+                                               if (!auto_download) {
+                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, cnt_info->total_body_size, 0))
+                                                               EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
+                                               }
+
+                                               download_interval =  cnt_info->total_body_size * DOWNLOAD_NOTI_INTERVAL_PERCENT / 100;
+                                               download_total_size = cnt_info->total_body_size;
+                                       }
+                                       else {
+                                               if (!auto_download) {
+                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, buf, body_size, 0))
+                                                               EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
+                                               }
+
+                                               download_interval =  body_size * DOWNLOAD_NOTI_INTERVAL_PERCENT / 100;
+                                               download_total_size = body_size;
+                                       }
+                               }
+
+                               if (cnt_info->grab_type == (GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT)) {
+                                       if (!auto_download) {
+                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, cnt_info->total_mail_size, 0))
+                                                       EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
+                                       }
+
+                                       download_interval =  cnt_info->total_mail_size * DOWNLOAD_NOTI_INTERVAL_PERCENT / 100;
+                                       download_total_size = cnt_info->total_mail_size;
+                               }
+
+                               if (download_interval > DOWNLOAD_NOTI_INTERVAL_SIZE) {
+                                       download_interval = DOWNLOAD_NOTI_INTERVAL_SIZE;
+                               }
+
+                               if (body_size < DOWNLOAD_MAX_BUFFER_SIZE) {
+                                       if (net_getbuffer(imaplocal->netstream, (long)body_size, (char *)encoded) <= 0) {
+                                               EM_DEBUG_EXCEPTION("net_getbuffer failed...");
+                                               err = EMAIL_ERROR_NO_RESPONSE;
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (GMIME_IS_PART(mime_object) && mime_part) {
+                                               content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded));
+                                               //parser = g_mime_parser_new_with_stream(content_stream);
+                                               content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
+                                               if (content_stream) g_object_unref (content_stream);
+                                               g_mime_part_set_content_object(mime_part, content);
+                                               if (content) g_object_unref(content);
+                                       }
+                                       else if (GMIME_IS_MESSAGE_PART(mime_object) && mime_message_part) {
+                                               FILE *fp = NULL;
+                                               int encoded_len = EM_SAFE_STRLEN((char *)encoded);
+
+                                               err = em_fopen(buf, "wb+", &fp);
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", buf);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (encoded_len > 0 && fwrite(encoded, encoded_len, 1, fp) == 0) {
+                                                       EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite() failed");
+                                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                                       fclose(fp);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (fp != NULL)
+                                                       fclose(fp);
+                                       }
+
+                                       downloaded_size += EM_SAFE_STRLEN((char *)encoded);
+                                       EM_DEBUG_LOG("IMAP4 downloaded body size [%d]", downloaded_size);
+                                       EM_DEBUG_LOG("IMAP4 total body size [%d]", download_total_size);
+                                       EM_DEBUG_LOG("IMAP4 download interval [%d]", download_interval);
+
+                                       /* In some situation, total_encoded_len includes the length of dummy bytes.
+                                        * So it might be greater than body_size */
+
+                                       download_progress = 100 * downloaded_size / download_total_size;
+
+                                       EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY:Total[%d]/[%d] = %d %% Completed",
+                                                       downloaded_size, download_total_size, download_progress);
+
+                                       if (cnt_info->grab_type == GRAB_TYPE_TEXT && !auto_download) {
+                                               if (cnt_info->total_body_size > body_size) {
+                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, download_total_size, downloaded_size))
+                                                               EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_MULTIPART_BODY] Failed >>>>");
+                                               }
+                                               else {
+                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, buf, download_total_size, downloaded_size))
+                                                               EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                               }
+                                       }
+                               } else {
+                                       int remain_body_size = body_size;
+                                       int x = 0;
+                                       int nsize = 0;
+                                       int fd = 0;
+                                       total = 0;
+
+                                       if (mime_part && mime_part->encoding == GMIME_CONTENT_ENCODING_BASE64)
+                                               x = (sizeof(encoded)/78)*78; /* to solve base64 decoding pro */
+                                       else
+                                               x = sizeof(encoded)-1;
+
+                                       memset(test_buffer, 0x00, sizeof(test_buffer));
+                                       while (remain_body_size && (total <body_size)) {
+
+                                               memset(test_buffer, 0x00, sizeof(test_buffer));
+                                               while ((total != body_size) && remain_body_size && ((EM_SAFE_STRLEN((char *)test_buffer) + x) < sizeof(test_buffer))) {
+                                                       memset(encoded, 0x00, sizeof(encoded));
+
+                                                       if (net_getbuffer (imaplocal->netstream, (long)x, (char *)encoded) <= 0) {
+                                                               EM_DEBUG_EXCEPTION("net_getbuffer failed...");
+                                                               err = EMAIL_ERROR_NO_RESPONSE;
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       nsize = EM_SAFE_STRLEN((char *)encoded);
+                                                       remain_body_size = remain_body_size - nsize;
+                                                       strncat((char *)test_buffer, (char *)encoded, nsize);
+                                                       total = total + nsize;
+                                                       downloaded_size += nsize;
+
+                                                       EM_DEBUG_LOG("total/body_size [%d/%d]", total, body_size);
+
+                                                       if (!(remain_body_size/x) && remain_body_size%x)
+                                                               x = remain_body_size%x;
+
+                                                       //EM_DEBUG_LOG("IMAP4  - %d ", _imap4_last_notified_body_size);
+                                                       EM_DEBUG_LOG("IMAP4 download interval [%d]", download_interval);
+                                                       EM_DEBUG_LOG("IMAP4 downloaded size [%d]", downloaded_size);
+                                                       EM_DEBUG_LOG("IMAP4 total body size [%d]", download_total_size);
+
+                                                       if (((last_notified_download_size + download_interval) <= downloaded_size)
+                                                               || (downloaded_size >= download_total_size)) {
+
+                                                               /*  In some situation, total_encoded_len includes the length of dummy bytes.
+                                                                * So it might be greater than body_size */
+
+                                                               if (downloaded_size > download_total_size)
+                                                                       downloaded_size = download_total_size;
+
+                                                               last_notified_download_size = downloaded_size;
+
+                                                               download_progress = 100*downloaded_size/download_total_size;
+                                                               EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY :Total[%d] / [%d] = %d %% Completed.",
+                                                                               downloaded_size, download_total_size, download_progress);
+
+                                                               if (!auto_download) {
+                                                                       if (cnt_info->total_body_size > body_size) {
+                                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, download_total_size, downloaded_size))
+                                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_MULTIPART_BODY] Failed >>>>");
+                                                                       } else {
+                                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, buf, download_total_size, downloaded_size))
+                                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+
+                                               if (flag_first_write == 1) {
+                                                       FILE *fp = NULL;
+                                                       int encoded_len = EM_SAFE_STRLEN((char *)test_buffer);
+
+                                                       err = em_fopen(buf, "wb+", &fp);
+                                                       if (err != EMAIL_ERROR_NONE) {
+                                                               EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", buf);
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       if (encoded_len > 0 && fwrite(test_buffer, encoded_len, 1, fp) == 0) {
+                                                               EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite(\"%s\") failed", test_buffer);
+                                                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                                               fclose (fp); /* prevent */
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       if (fp != NULL)
+                                                               fclose(fp);
+
+                                                       flag_first_write = 0;
+                                               } else {
+                                                       FILE *fp = NULL;
+                                                       int encoded_len = EM_SAFE_STRLEN((char *)test_buffer);
+
+                                                       err = em_fopen(buf, "ab+", &fp);
+                                                       if (err != EMAIL_ERROR_NONE) {
+                                                               EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", buf);
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       if (encoded_len > 0 && fwrite(test_buffer, encoded_len, 1, fp) == 0) {
+                                                               EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite(\"%s\") failed", test_buffer);
+                                                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                                               fclose (fp); /* prevent */
+                                                               goto FINISH_OFF;
+                                                       }
+
+                                                       if (fp != NULL)
+                                                               fclose(fp);
+                                               }
+                                               EM_DEBUG_LOG("%d has been written", EM_SAFE_STRLEN((char *)test_buffer));
+                                       }
+
+                                       if (GMIME_IS_PART(mime_object) && mime_part) {
+                                               err = em_open(buf, O_RDONLY, 0, &fd);
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION("holder open failed : holder is a filename that will be saved.");
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               content_stream = g_mime_stream_fs_new(fd);
+                                               //parser = g_mime_parser_new_with_stream(content_stream);
+                                               content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
+                                               if (content_stream) g_object_unref (content_stream);
+                                               g_mime_part_set_content_object(mime_part, content);
+                                               if (content) g_object_unref(content);
+                                       }
+                               }
+                       } else {
+                                EM_DEBUG_LOG("MIME header");
+
+                                char *file_name = NULL;
+
+                                GMimeObject *object_header = NULL;
+                                GMimeParser *parser_header = NULL;
+                                GMimeContentType *ctype_header = NULL;
+                                GMimeContentDisposition *disposition_header = NULL;
+
+                                if (net_getbuffer(imaplocal->netstream, (long)body_size, (char *)encoded) <= 0) {
+                                        EM_DEBUG_EXCEPTION("net_getbuffer failed...");
+                                        err = EMAIL_ERROR_NO_RESPONSE;
+                                        goto FINISH_OFF;
+                                }
+
+                                EM_DEBUG_LOG_DEV("Data : [%s]", encoded);
+
+                                content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded));
+
+                                parser_header = g_mime_parser_new_with_stream(content_stream);
+                                if (content_stream) g_object_unref (content_stream);
+
+                                object_header = g_mime_parser_construct_part(parser_header);
+                                if (parser_header) g_object_unref(parser_header);
+
+                                /* Content type */
+                                ctype_header = g_mime_object_get_content_type(object_header);
+                                file_name = (char *)g_mime_content_type_get_parameter(ctype_header, "name");
+                                EM_DEBUG_LOG_DEV("Content name : [%s]", file_name);
+
+                                if (file_name == NULL) {
+                                        /* Content Disposition */
+                                        disposition_header = g_mime_object_get_content_disposition(object_header);
+                                        file_name = (char *)g_mime_content_disposition_get_parameter(disposition_header, "filename");
+                                        EM_DEBUG_LOG_DEV("Disposition name : [%s]", file_name);
+                                }
+
+                                /* Replace the file name (Becase the server sometimes send the invalid name in bodystructure) */
+                                if (mime_part && file_name) g_mime_part_set_filename(mime_part, file_name);
+
+                                if (object_header) g_object_unref(object_header);
+                                if (ctype_header) g_object_unref(ctype_header);
+                                if (disposition_header) g_object_unref(disposition_header);
+                        }
+
+                       EM_SAFE_FREE(buf);
+               }
+               else if ((tag_position = g_strrstr(response, tag))) /*  end of response */ {
+                       if (!strncmp(tag_position + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
+                               EM_SAFE_FREE(response);
+                       }
+                       else {          /* 'NO' or 'BAD */
+                               err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;
+                               goto FINISH_OFF;
+                       }
+                       break;
+               }
+               else if (!strcmp(response, ")")) {
+               }
+
+               EM_SAFE_FREE (response);
+       }
+
+       ret = TRUE;
+
+FINISH_OFF:
+
+       /*if (mime_iter) {
+               g_mime_part_iter_free(mime_iter);
+               mime_iter = NULL;
+       }*/
+
+       if (search_info) {
+               EM_SAFE_FREE(search_info->section);
+               EM_SAFE_FREE(search_info);
+       }
+
+       EM_SAFE_FREE(buf);
+
+       EM_SAFE_FREE(response);
+
+       if (err_code)
+               *err_code = err;
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_fetch_imap_attachment_section(MAILSTREAM *stream,
+               int mail_id, int uid, int nth, struct _m_content_info *cnt_info,
+               GMimeMessage *message, int auto_download, int event_handle, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("stream[%p], uid[%d], err_code[%p]", stream, uid, err_code);
+
+       int ret = FALSE;
+       int err = EMAIL_ERROR_NONE;
+
+       IMAPLOCAL *imaplocal = NULL;
+
+       char tag[16] = {0,};
+       char command[64] = {0,};
+       char *response = NULL;
+       unsigned char encoded[DOWNLOAD_MAX_BUFFER_SIZE] = {0, };
+       unsigned char test_buffer[LOCAL_MAX_BUFFER_SIZE] = {0, };
+
+       int flag_first_write = TRUE;
+       int preline_len = 0;
+       int nskip = 0;
+       char *concat_encoded = NULL;
+       char *new_response = NULL;
+       char *tag_position = NULL;
+       char *tmp_file = NULL;
+
+       int body_size = 0;
+       int total = 0;
+       int server_response_yn = 0;
+       int download_noti_interval = 0;
+       int download_total_size = 0;
+       int downloaded_size = 0;
+       int download_progress = 0;
+       int last_notified_download_size = 0;
+
+       GMimeDataWrapper *content = NULL;
+       GMimeStream *content_stream = NULL;
+       GMimeObject *mime_object = NULL;
+       GMimePart *mime_part = NULL;
+       //GMimePartIter *mime_iter = NULL;
+       GMimeMessagePart *mime_message_part = NULL;
+       search_section *search_info = NULL;
+
+       if (!stream || !cnt_info || !message) {
+               EM_DEBUG_EXCEPTION_SEC("stream[%p], section[%s], cnt_info[%p], message[%p]",
+                               stream, cnt_info, message);
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       if (!cnt_info->sections) {
+               EM_DEBUG_EXCEPTION("section is NULL");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       imaplocal = stream->local;
+
+       if (!imaplocal->netstream) {
+               EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected... %p", imaplocal->netstream);
+               err = EMAIL_ERROR_INVALID_STREAM;
+               goto FINISH_OFF;
+       }
+
+       memset(tag, 0x00, sizeof(tag));
+       memset(command, 0x00, sizeof(command));
+
+       SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
+       SNPRINTF(command, sizeof(command), "%s UID FETCH %d BODY.PEEK[%s]\015\012", tag, uid, cnt_info->sections);
+       EM_DEBUG_LOG("[IMAP4] >>> [%s]", command);
+
+       /* send command : get msgno/uid for all message */
+       if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
+               EM_DEBUG_EXCEPTION("net_sout failed...");
+               err = EMAIL_ERROR_CONNECTION_BROKEN;
+               goto FINISH_OFF;
+       }
+
+       char *p_stream = NULL;
+       char *p_content = NULL;
+
+       while (imaplocal->netstream) {
+
+               /*don't delete the comment. several threads including event thread call this func
+               if (!emcore_check_thread_status()) {
+                       EM_DEBUG_LOG("Canceled...");
+                       imaplocal->netstream = NULL;
+                       err = EMAIL_ERROR_CANCELLED;
+                       goto FINISH_OFF;
+               }*/
+
+               /* receive response */
+               if (!(response = net_getline(imaplocal->netstream))) {
+                       EM_DEBUG_EXCEPTION("net_getline failed...");
+                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                       goto FINISH_OFF;
+               }
+
+#ifdef FEATURE_CORE_DEBUG
+               EM_DEBUG_LOG("recv[%s]", response);
+#endif
+
+               /* start of response */
+               if (response[0] == '*' && !server_response_yn) {
+
+                       p_stream = strstr(response, "BODY[");
+
+                       if (!p_stream) {
+                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                               goto FINISH_OFF;
+                       }
+
+                       server_response_yn = 1;
+                       p_stream += strlen("BODY[");
+                       p_content = p_stream;
+
+                       while (*p_content != ']')
+                               p_content++;
+
+                       *p_content = '\0';
+
+                       /* check correct section */
+                       if (g_strcmp0(cnt_info->sections, p_stream) != 0) {
+                               EM_DEBUG_LOG("Invalid response of section");
+                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                               goto FINISH_OFF;
+                       }
+
+                       /* get body size */
+                       p_stream = strstr(p_content+1, " {");
+                       if (p_stream) {
+                               p_stream += strlen(" {");
+                               p_content = p_stream;
+
+                               while (isdigit(*p_content))
+                                       p_content++;
+
+                               *p_content = '\0';
+
+                               body_size = atoi(p_stream);
+                       } else {        /* no body length is replied */
+                               /* seek the termination of double quot */
+                               p_stream = strstr(p_content+1, " \"");
+                               if (p_stream) {
+                                       char *t = NULL;
+                                       p_stream += strlen(" \"");
+                                       t = strstr(p_stream, "\"");
+                                       if (t) {
+                                               body_size = t - p_stream;
+                                               *t = '\0';
+                                               EM_DEBUG_LOG("Body : start[%p] end[%p] : body[%s]", p_stream, t, p_stream);
+                                               /* need to decode */
+                                               EM_SAFE_FREE(response);
+                                               response = EM_SAFE_STRDUP(p_stream);
+                                       } else {
+                                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                                               goto FINISH_OFF;
+                                       }
+                               } else {
+                                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       /*if (GMIME_IS_MULTIPART (message->mime_part)) {
+                               mime_iter = g_mime_part_iter_new((GMimeObject *)message);
+
+                               if (!g_mime_part_iter_is_valid(mime_iter)) {
+                                       EM_DEBUG_EXCEPTION("Part iterator is not valid");
+                                       goto FINISH_OFF;
+                               }
+
+                               if (g_mime_part_iter_jump_to (mime_iter, section)) {
+                                       EM_DEBUG_LOG("g_mime_part_iter_jump_to: %s", section);
+                                       mime_object = g_mime_part_iter_get_current (mime_iter);
+                                       if (!mime_object) {
+                                               EM_DEBUG_EXCEPTION("g_mime_part_iter_get_current failed");
+                                               goto FINISH_OFF;
+                                       }
+                                       mime_part = GMIME_PART(mime_object);
+                               } else {
+                                       EM_DEBUG_LOG("g_mime_part_iter_jump_to: failed to jump to %s", section);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (mime_iter) {
+                                       g_mime_part_iter_free(mime_iter);
+                                       mime_iter = NULL;
+                               }
+                       } else if (GMIME_IS_PART (message->mime_part)) {
+                               mime_object = message->mime_part;
+                               mime_part = GMIME_PART(mime_object);
+                       }*/
+
+                       if (search_info) {
+                               EM_SAFE_FREE(search_info->section);
+                               EM_SAFE_FREE(search_info);
+                       }
+
+                       if (!(search_info = em_malloc(sizeof(search_section)))) { /* prevent */
+                               EM_DEBUG_EXCEPTION("em_malloc failed...");
+                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
+                       }
+
+                       search_info->section = EM_SAFE_STRDUP(cnt_info->sections);
+                       g_mime_message_foreach(message, emcore_gmime_search_section_foreach_cb, (gpointer)search_info);
+
+                       if (!(search_info->section_object)) {
+                               EM_DEBUG_EXCEPTION("section search failed");
+                               goto FINISH_OFF;
+                       }
+
+                       mime_message_part = NULL;
+                       mime_part = NULL;
+                       mime_object = search_info->section_object;
+                       if (GMIME_IS_MESSAGE_PART(mime_object)) {
+                               mime_message_part = GMIME_MESSAGE_PART(mime_object);
+                       } else if (GMIME_IS_PART(mime_object)) {
+                               mime_part = GMIME_PART(mime_object);
+                       } else {
+                               EM_DEBUG_EXCEPTION("invalid mime part type");
+                               goto FINISH_OFF;
+                       }
+
+                       if (!emcore_get_temp_file_name(&tmp_file, &err) || !tmp_file) {
+                               EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
+
+                       g_mime_object_set_content_type_parameter(mime_object, "tmp_content_path", tmp_file);
+
+                       EM_DEBUG_LOG("Attachment number [%d]", nth);
+                       if (!auto_download) {
+                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, tmp_file, nth, 0))
+                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+                       }
+
+                       download_noti_interval = body_size * DOWNLOAD_NOTI_INTERVAL_PERCENT / 100;
+                       download_total_size = body_size;
+
+                       if (download_noti_interval > DOWNLOAD_NOTI_INTERVAL_SIZE) {
+                               download_noti_interval = DOWNLOAD_NOTI_INTERVAL_SIZE;
+                       }
+
+                       /* remove new lines */
+                       do {
+                               EM_SAFE_FREE(response);
+                               if (!(response = net_getline(imaplocal->netstream))) {
+                                       EM_DEBUG_EXCEPTION("net_getline failed...");
+                                       err = EMAIL_ERROR_INVALID_RESPONSE;
+                                       goto FINISH_OFF;
+                               }
+
+                               EM_DEBUG_LOG("response:%s", response);
+                               if (EM_SAFE_STRLEN(response) == 0) {
+                                       EM_DEBUG_LOG("Skip newline !!");
+                                       nskip++;
+                               } else {
+                                       EM_SAFE_FREE(new_response); /* detected by valgrind */
+                                       new_response = g_strconcat(response, "\r\n", NULL);
+                                       EM_SAFE_FREE(response);
+                                       break;
+                               }
+                       } while(1);
+
+                       preline_len = EM_SAFE_STRLEN(new_response);
+                       EM_DEBUG_LOG("preline_len : %d", preline_len);
+
+                       if (body_size <= 0 || preline_len <= 0)
+                               continue;
+
+                       if ((body_size - preline_len - nskip*2) <= 0) {
+                               /* 1 line content */
+
+                               if (GMIME_IS_PART(mime_object) && mime_part) {
+                                       char *tmp_str = NULL;
+                                       tmp_str = g_strndup(new_response, body_size);
+                                       if (tmp_str == NULL) tmp_str = g_strdup(new_response);
+
+                                       content_stream = g_mime_stream_mem_new_with_buffer((const char *)tmp_str, EM_SAFE_STRLEN(tmp_str));
+                                       //parser = g_mime_parser_new_with_stream(content_stream);
+                                       content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
+                                       if (content_stream) g_object_unref (content_stream);
+                                       g_mime_part_set_content_object(mime_part, content);
+                                       if (content) g_object_unref(content);
+                                       EM_SAFE_FREE(tmp_str);
+                               }
+                               else if (GMIME_IS_MESSAGE_PART(mime_object) && mime_message_part) {
+                                       FILE *fp = NULL;
+                                       int response_len = 0;
+                                       char *tmp_str = NULL;
+
+                                       tmp_str = g_strndup(new_response, body_size);
+                                       if (tmp_str == NULL) tmp_str = g_strdup(new_response);
+
+                                       response_len = EM_SAFE_STRLEN((char *)tmp_str);
+
+                                       err = em_fopen(tmp_file, "wb+", &fp);
+                                       if (err != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", tmp_file);
+                                               EM_SAFE_FREE(tmp_str);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (response_len > 0 && fwrite(tmp_str, response_len, 1, fp) == 0) {
+                                               EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite() failed");
+                                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                               fclose(fp);
+                                               EM_SAFE_FREE(tmp_str);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (fp != NULL)
+                                               fclose(fp);
+
+                                       EM_SAFE_FREE(tmp_str);
+                               }
+
+                               EM_SAFE_FREE(new_response);
+
+                               total = body_size;
+                               EM_DEBUG_LOG("current total = %d", total);
+                               downloaded_size += total;
+
+                               EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : received[%d] / total_size[%d] = %d %% Completed",
+                                               downloaded_size, download_total_size, (int)((float)downloaded_size / (float)download_total_size * 100.0));
+
+                               if (((last_notified_download_size + download_noti_interval) <= downloaded_size) || (downloaded_size >= download_total_size)) {
+
+                                       if (downloaded_size > download_total_size)
+                                               downloaded_size = download_total_size;
+
+                                       last_notified_download_size = downloaded_size;
+
+                                       download_progress = (int)((float)downloaded_size / (float)download_total_size * 100.0);
+
+                                       if (!auto_download) {
+                                               if (download_total_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, tmp_file, nth, download_progress))
+                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+                                       }
+                               }
+                       }
+                       else if ((body_size < DOWNLOAD_MAX_BUFFER_SIZE) && (body_size - preline_len - nskip*2 < DOWNLOAD_MAX_BUFFER_SIZE)) {
+                               memset(encoded, 0x00, sizeof(encoded));
+                               if (net_getbuffer(imaplocal->netstream, body_size - preline_len - nskip*2, (char *)encoded) <= 0) {
+                                       EM_DEBUG_EXCEPTION("net_getbuffer failed...");
+                                       err = EMAIL_ERROR_NO_RESPONSE;
+                                       goto FINISH_OFF;
+                               }
+
+                               concat_encoded = g_strconcat(new_response, encoded, NULL);
+                               memset(encoded, 0x00, sizeof(encoded));
+                               memcpy(encoded, concat_encoded, EM_SAFE_STRLEN(concat_encoded));
+                               EM_SAFE_FREE(concat_encoded);
+                               EM_SAFE_FREE(new_response);
+
+                               if (GMIME_IS_PART(mime_object) && mime_part) {
+                                       content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded));
+                                       //parser = g_mime_parser_new_with_stream(content_stream);
+                                       content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
+                                       if (content_stream) g_object_unref (content_stream);
+                                       g_mime_part_set_content_object(mime_part, content);
+                                       if (content) g_object_unref(content);
+                               }
+                               else if (GMIME_IS_MESSAGE_PART(mime_object) && mime_message_part) {
+                                       FILE *fp = NULL;
+                                       int encoded_len = EM_SAFE_STRLEN((char *)encoded);
+
+                                       err = em_fopen(tmp_file, "wb+", &fp);
+                                       if (err != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", tmp_file);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (encoded_len > 0 && fwrite(encoded, encoded_len, 1, fp) == 0) {
+                                               EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite() failed");
+                                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                               fclose(fp);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (fp != NULL)
+                                               fclose(fp);
+                               }
+
+                               total = EM_SAFE_STRLEN((char *)encoded);
+                               EM_DEBUG_LOG("total = %d", total);
+                               downloaded_size += total;
+
+                               EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : received[%d] / total_size[%d] = %d %% Completed",
+                                               downloaded_size, download_total_size, (int)((float)downloaded_size / (float)download_total_size * 100.0));
+
+                               if (((last_notified_download_size + download_noti_interval) <= downloaded_size) || (downloaded_size >= download_total_size)) {
+
+                                       if (downloaded_size > download_total_size)
+                                               downloaded_size = download_total_size;
+
+                                       last_notified_download_size = downloaded_size;
+
+                                       download_progress = (int)((float)downloaded_size / (float)download_total_size * 100.0);
+
+                                       if (!auto_download) {
+                                               if (download_total_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, tmp_file, nth, download_progress))
+                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+                                       }
+                               }
+                       }
+                       else {
+                               int remain_body_size = body_size - preline_len - nskip*2;
+                               int x = 0;
+                               int nsize = 0;
+                               int fd = 0;
+                               total += preline_len + nskip*2;
+                               downloaded_size += preline_len + nskip*2;
+
+                               memset(test_buffer, 0x00, sizeof(test_buffer));
+
+                               while (remain_body_size > 0 && (total < body_size)) {
+
+                                       memset(test_buffer, 0x00, sizeof(test_buffer));
+
+                                        if (remain_body_size < DOWNLOAD_MAX_BUFFER_SIZE)
+                                                x = remain_body_size;
+                                        else if (mime_part && mime_part->encoding == GMIME_CONTENT_ENCODING_BASE64)
+                                               x = (sizeof(encoded)/preline_len)*preline_len; /* to solve base64 decoding pro */
+                                       else
+                                               x = sizeof(encoded)-1;
+
+                                       if (new_response) {
+                                               strncat((char *)test_buffer, (char *)new_response, preline_len);
+                                               EM_SAFE_FREE(new_response);
+                                       }
+
+                                       while (remain_body_size > 0 && (total < body_size) && ((EM_SAFE_STRLEN((char *)test_buffer) + x) < sizeof(test_buffer))) {
+                                               if (event_handle > 0)
+                                                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+                                               memset(encoded, 0x00, sizeof(encoded));
+
+                                               if (net_getbuffer(imaplocal->netstream, (long)x, (char *)encoded) <= 0) {
+                                                       EM_DEBUG_EXCEPTION("net_getbuffer failed...");
+                                                       err = EMAIL_ERROR_NO_RESPONSE;
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               nsize = EM_SAFE_STRLEN((char *)encoded);
+                                               remain_body_size = remain_body_size - nsize;
+                                               strncat((char *)test_buffer, (char *)encoded, nsize);
+                                               total = total + nsize;
+                                               downloaded_size += nsize;
+                                               EM_DEBUG_LOG("nsize : %d", nsize);
+                                               EM_DEBUG_LOG("remain_body_size : %d", remain_body_size);
+                                               EM_DEBUG_LOG("total : %d", total);
+                                               EM_DEBUG_LOG("imap_received_body_size : %d", downloaded_size);
+
+                                               if (!(remain_body_size/x) && remain_body_size%x)
+                                                       x = remain_body_size%x;
+
+                                               EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : received[%d] / total_size[%d] = %d %% Completed",
+                                                               downloaded_size, download_total_size, (int)((float)downloaded_size / (float)download_total_size * 100.0));
+
+                                               if (((last_notified_download_size + download_noti_interval) <= downloaded_size) || (downloaded_size >= download_total_size)) {
+
+                                                       if (downloaded_size > download_total_size)
+                                                               downloaded_size = download_total_size;
+
+                                                       last_notified_download_size = downloaded_size;
+
+                                                       download_progress = (int)((float)downloaded_size / (float)download_total_size * 100.0);
+
+                                                       if (!auto_download) {
+                                                               if (download_total_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, tmp_file, nth, download_progress))
+                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+                                                       }
+                                               }
+                                       }
+
+                                       if (flag_first_write == true) {
+
+                                               FILE *fp = NULL;
+                                               int encoded_len = EM_SAFE_STRLEN((char *)test_buffer);
+
+                                               err = em_fopen(tmp_file, "wb+", &fp);
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", tmp_file);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (encoded_len > 0 && fwrite(test_buffer, encoded_len, 1, fp) == 0) {
+                                                       EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite(\"%s\") failed", test_buffer);
+                                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                                       fclose(fp); /* prevent */
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (fp != NULL)
+                                                       fclose(fp);
+
+                                               flag_first_write = false;
+                                       } else {
+                                               FILE *fp = NULL;
+                                               int encoded_len = EM_SAFE_STRLEN((char *)test_buffer);
+
+                                               err = em_fopen(tmp_file, "ab+", &fp);
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", tmp_file);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (encoded_len > 0 && fwrite(test_buffer, encoded_len, 1, fp) == 0) {
+                                                       EM_DEBUG_EXCEPTION("Error Occured while writing. fwrite(\"%s\") failed", test_buffer);
+                                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                                       fclose(fp); /* prevent */
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (fp != NULL)
+                                                       fclose(fp);
+                                       }
+                                       EM_DEBUG_LOG("%d has been written", EM_SAFE_STRLEN((char *)test_buffer));
+                               }
+
+                               if (GMIME_IS_PART(mime_object) && mime_part) {
+                                       err = em_open(tmp_file, O_RDONLY, 0, &fd);
+                                       if (err != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+                                               if (fd) close(fd);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       content_stream = g_mime_stream_fs_new(fd);
+                                       //parser = g_mime_parser_new_with_stream(content_stream);
+                                       content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding);
+                                       if (content_stream) g_object_unref (content_stream);
+                                       g_mime_part_set_content_object(mime_part, content);
+                                       if (content) g_object_unref(content);
+                               }
+                       }
+
+                       EM_SAFE_FREE(tmp_file);
+               }
+               else if ((tag_position = g_strrstr(response, tag))) /*  end of response */ {
+                       if (!strncmp(tag_position + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
+                               EM_SAFE_FREE(response);
+                       } else  {               /* 'NO' or 'BAD */
+                               err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;
+                               goto FINISH_OFF;
+                       }
+                       break;
+               }
+               else if (!strcmp(response, ")")) {
+                       /*  The end of response which contains body information */
+               }
+       }       /*  while (imaplocal->netstream)  */
+
+       ret = TRUE;
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(tmp_file);
+       EM_SAFE_FREE(response);
+       EM_SAFE_FREE (new_response);
+
+       if (search_info) {
+               EM_SAFE_FREE(search_info->section);
+               EM_SAFE_FREE(search_info);
+       }
+
+       /*if (mime_iter) {
+               g_mime_part_iter_free(mime_iter);
+               mime_iter = NULL;
+       }*/
+
+       if (err_code != NULL)
+               *err_code = err;
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_check_filename_duplication(char *source_filename, struct _m_content_info *cnt_info)
+{
+       EM_DEBUG_FUNC_BEGIN("source_file_name [%s], content_info [%p]", source_filename, cnt_info);
+
+       if (!source_filename || !cnt_info || strlen(source_filename) <= 0) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               return FALSE;
+       }
+
+       struct attachment_info *cur_attachment_info = NULL;
+       int ret = FALSE;
+
+       cur_attachment_info = cnt_info->inline_file;
+
+       while (cur_attachment_info) {
+
+               if(g_strcmp0(source_filename, cur_attachment_info->name) == 0) {
+                       ret = TRUE;
+                       break;
+               }
+
+               cur_attachment_info = cur_attachment_info->next;
+       }
+
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+INTERNAL_FUNC char *emcore_gmime_get_modified_filename_in_duplication(char *source_filename)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       struct timeval tv;
+       char temp_filename[MAX_PATH] = {0,};
+       //char *filename = NULL;
+       //char *extension = NULL;
+
+       if (!source_filename) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               return NULL;
+       }
+
+       /*if ((err = em_get_file_name_and_extension_from_file_path(source_filename, &filename, &extension)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_get_file_name_and_extension_from_file_path failed [%d]", err);
+               return NULL;
+       }*/
+
+       gettimeofday(&tv, NULL);
+       srand(tv.tv_usec);
+
+       snprintf(temp_filename, MAX_PATH, "%d_%s", rand(), source_filename);
+       EM_DEBUG_LOG_SEC("temp_file_name [%s]", temp_filename);
+
+       EM_DEBUG_FUNC_END();
+       return EM_SAFE_STRDUP(temp_filename);
+}
+
+INTERNAL_FUNC char *emcore_gmime_get_encoding_to_utf8(const char *text)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+    if (text == NULL) {
+        EM_DEBUG_EXCEPTION("Invalid parameter");
+        return NULL;
+    }
+
+    char *encoded_text = NULL;
+
+    encoded_text = g_mime_utils_header_encode_text(text);
+    if (encoded_text == NULL) {
+        EM_DEBUG_EXCEPTION("g_mime_utils_header_encode_text failed : [%s]", text);
+        return NULL;
+    }
+
+    EM_DEBUG_LOG_SEC("encoded_text : [%s]", encoded_text);
+
+    return encoded_text;
+}
+
+INTERNAL_FUNC char *emcore_gmime_get_decoding_text(const char *text)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+    if (text == NULL) {
+        EM_DEBUG_EXCEPTION("Invalid parameter");
+        return NULL;
+    }
+
+    char *decoded_text = NULL;
+
+    decoded_text = g_mime_utils_header_decode_text(text);
+    if (decoded_text == NULL) {
+        EM_DEBUG_EXCEPTION("g_mime_utils_header_encode_text failed : [%s]", text);
+        return NULL;
+    }
+
+    EM_DEBUG_LOG("decoded_text : [%s]", decoded_text);
+
+    return decoded_text;
+}
index fdbc3fe..96b48cd 100755 (executable)
@@ -35,6 +35,7 @@
 #include <glib.h>
 #include <openssl/ssl.h>
 #include "c-client.h"
+#include <sys/epoll.h>
 #include "lnx_inc.h"
 #include "email-core-imap-idle.h"
 #include "email-debug-log.h"
 #include "email-core-mailbox.h"
 #include "email-core-event.h"
 #include "email-core-account.h"
-
+#include "email-core-alarm.h"
+#include "email-utilities.h"
+#include "email-core-container.h"
 
 /*Definitions copied temporarily from ssl_unix.c */
 #define SSLBUFLEN 8192
+#define MAX_EPOLL_EVENT 100
 
 typedef struct ssl_stream {
   TCPSTREAM *tcpstream; /* TCP stream */
@@ -59,290 +63,115 @@ typedef struct ssl_stream {
 } SSLSTREAM;
 /*Definitions copied temporarily from ssl_unix.c - end*/
 
-thread_t imap_idle_thread;
-int g_imap_idle_thread_alive = 0;
-
-void* emcore_imap_idle_run(void* thread_user_data);
-static int emcore_imap_idle_parse_response_stream(email_mailbox_t *mailbox, int *err_code);
-static int emcore_imap_idle_connect_and_idle_on_mailbox(email_mailbox_t *mailbox, int *err_code);
-
-int emcore_create_imap_idle_thread(int account_id, int *err_code)
-{
-       EM_DEBUG_FUNC_BEGIN("account_id [%d], err_code [%p]", account_id, err_code);
-       int ret = false;
-       int err = EMAIL_ERROR_NONE;
-       int thread_error;
-
-       g_imap_idle_thread_alive = 1;
-       THREAD_CREATE(imap_idle_thread, emcore_imap_idle_run, NULL, thread_error);
-
-       if (thread_error != 0) {
-               EM_DEBUG_EXCEPTION("cannot make IMAP IDLE thread...");
-               err = EMAIL_ERROR_UNKNOWN;
-               g_imap_idle_thread_alive = 0;
-               goto FINISH_OFF;
-       }
-
-       ret = true;
-
-FINISH_OFF:
-       if (err_code != NULL)
-               *err_code = err;
+typedef struct _email_imap_idle_connection_info_t {
+       int          account_id;
+       int          mailbox_id;
+       MAILSTREAM  *mail_stream;
+       int          socket_fd;
+       char        *multi_user_name;
+} email_imap_idle_connection_info_t;
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
-}
-
-/*
-Need to identify various scenarios where thread needs to be killed
-1. After the timer set to 10 min expires.
-2. When No SIM, thread is created and emnetwork_check_network_status() fails.
-*/
-int emcore_kill_imap_idle_thread(int *err_code)
-{
-       EM_DEBUG_FUNC_BEGIN("err_code [%p]", err_code);
-       int ret = true;
-       int err = EMAIL_ERROR_NONE;
-
-       EM_DEBUG_LOG("killing thread");
-
-       /* kill the thread */
-       EM_DEBUG_LOG("Before g_thread_exit");
-       g_imap_idle_thread_alive = 0;
-       EM_DEBUG_LOG("After g_thread_exit");
-
-       EM_DEBUG_LOG("Making imap idle NULL");
-       imap_idle_thread = 0;
-       EM_DEBUG_LOG("killed IMAP IDLE");
-
-       if (err_code)
-               *err_code = err;
-
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
-}
+int       imap_idle_pipe_fd[2];
+thread_t  imap_idle_thread_id;
 
-int emcore_imap_idle_loop_start(email_mailbox_t *mailbox_list,  int num, int *err_code)
+static int emcore_get_connection_info_by_socket_fd(GList *input_imap_idle_task_list, int input_socket_fd, email_imap_idle_connection_info_t **output_connection_info)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], num[%d]", mailbox_list, num);
-       fd_set readfds;
-       int maxfd = 0;
+       EM_DEBUG_FUNC_BEGIN("input_imap_idle_task_list[%p] input_socket_fd[%d] output_connection_info[%p]", input_imap_idle_task_list, input_socket_fd, output_connection_info);
        int err = EMAIL_ERROR_NONE;
-       int counter = 0;
-       int select_result = 0;
-       int ret = false;
-       email_mailbox_t *temp = NULL;
-       struct timeval timeout;
+       email_imap_idle_connection_info_t *connection_info = NULL;
+       GList *index = NULL;
 
-       EM_DEBUG_EXCEPTION(">>>>>>> emcore_imap_idle_loop_start start ");
-       if (!mailbox_list || !num) {
+       if (input_imap_idle_task_list == NULL || output_connection_info == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       /* set timeout value to 10min */
-       timeout.tv_sec = 10 * 60;
-       timeout.tv_usec = 0;
-
-       /* IMAP IDLE LOOP */
-       while (1){
-               EM_DEBUG_EXCEPTION(">>>>>>>>>>>IDLING>>>>>>>>>>>>");
-               FD_ZERO(&readfds);
-               temp = mailbox_list;
-               for (counter = 0; counter < num; counter++) {
-                       FD_SET(temp->hold_connection, &readfds);
-                       if (temp->hold_connection > maxfd)
-                               maxfd = temp->hold_connection; 
-                       temp = temp->next;
-               }
-               maxfd++;
-               temp = mailbox_list;
-               
-               select_result = select(maxfd, &readfds, NULL, NULL, &timeout);
-
-               if (select_result > 0) /* Data coming on some socket */ {
-                       EM_DEBUG_EXCEPTION(">>>> Data Coming from Socket ");
-                       for (counter = 0; counter < num; counter++) {
-                               if (temp && FD_ISSET(temp->hold_connection, &readfds)) {
-                                       if (!emcore_imap_idle_parse_response_stream(temp, &err)) {
-                                               EM_DEBUG_EXCEPTION(">>>> emcore_imap_idle_loop_start 6 ");
-                                               temp->mail_stream = mail_close (temp->mail_stream);
-                                               EM_DEBUG_EXCEPTION(">>>> emcore_imap_idle_loop_start 7 ");
-                                               goto FINISH_OFF;
-                                       }
-                                       break; /* break for now - check if it is possible to get data on two sockets - shasikala.p */
-                               }
-                               temp = temp->next;
-                       }
-               }
+       index = g_list_first(input_imap_idle_task_list);
 
-               else if (select_result == 0) /* Timeout occurred */ {
-                       EM_DEBUG_EXCEPTION(">>>> emcore_imap_idle_loop_start 8 ");
-                       IMAPLOCAL *imap_local = NULL;
-                       char cmd[128], tag[32];
-                       char *p = NULL;
-                       /* send DONE Command */
-                       /* free all data here */
-                       for (counter = 0; counter < num; counter++) {
-                               EM_DEBUG_LOG(">>>> emcore_imap_idle_loop_start 9 ");
-                               if (temp && temp->mail_stream) {
-                                       imap_local = ((MAILSTREAM *)temp->mail_stream)->local;
-
-                                       sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)temp->mail_stream)->gensym++));
-                                       sprintf(cmd, "%s DONE\015\012", tag);
-
-                                       if (!imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
-                                               EM_DEBUG_EXCEPTION_SEC("network error - failed to DONE on Mailbox - %s ", temp->name);
-                                       }
-                                       else {
-                                               while (imap_local->netstream) {
-                                                       p = net_getline(imap_local->netstream);
-                                                       EM_DEBUG_EXCEPTION("p =[%s]", p);
-                                                       temp->mail_stream = mail_close (temp->mail_stream);
-                                                       break;
-                                           }
-                                       }
-                               }
-                               temp = temp->next;
-                       }
-                       
-                       
-                       /* kill idle thread */
-                       emcore_kill_imap_idle_thread(&err);
+       while(index) {
+               connection_info = index->data;
+               if(connection_info && connection_info->socket_fd == input_socket_fd) {
                        break;
                }
-
-               else {
-                       /*
-                       might happen that a socket descriptor passed to select got closed
-                       check which got closed and make hold_connection 0
-                       */
-                       EM_DEBUG_EXCEPTION(">>>>>> socket descriptor error :  No Data ");
-                       break;
-               }
-
-               select_result = 0;
+               index = g_list_next(index);
        }
 
-       ret = true;
-
-       EM_DEBUG_LOG(">>>> emcore_imap_idle_loop_start 17  ");
-
-FINISH_OFF: 
-
-       if (err_code)
-               *err_code = err;
+       if(connection_info)
+               *output_connection_info = connection_info;
+       else
+               err = EMAIL_ERROR_DATA_NOT_FOUND;
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+FINISH_OFF:
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
-void* emcore_imap_idle_run(void* thread_user_data)
+static int emcore_clear_old_connections(int input_epoll_fd, GList **input_imap_idle_task_list)
 {
-       EM_DEBUG_FUNC_BEGIN("thread_user_data [%p]", thread_user_data);
-       char              *mailbox_list[3];
-       int                mailbox_num = 0;
-       int                err = EMAIL_ERROR_NONE;
-       int                flag = true;
-       int                num = 0;
-       int                counter = 0;
-       int                accountID = (int)thread_user_data;
-       email_mailbox_t     *mail_box_list = NULL;
-       email_mailbox_t     *curr_mailbox = NULL;
-       email_mailbox_t     *prev_mailbox = NULL;
-       emstorage_mailbox_tbl_t *local_mailbox = NULL;
-       email_session_t     *session = NULL;
-
-       if ( !emnetwork_check_network_status(&err)) {
-               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
-               goto FINISH_OFF;
-       }
-       /* Connect to DB */
-       if (!emstorage_open(&err)) {
-               EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
+       EM_DEBUG_FUNC_BEGIN("input_epoll_fd[%d] input_imap_idle_task_list[%p] ", input_epoll_fd, input_imap_idle_task_list);
+       int err = EMAIL_ERROR_NONE;
+       email_imap_idle_connection_info_t *connection_info = NULL;
+       struct epoll_event ev = {0};
+       GList *index = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       if (input_imap_idle_task_list == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_empty_session(&session))
-               EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
+       index = g_list_first(*input_imap_idle_task_list);
 
-       /* get the list of mailbox name on which IDLE notifications are required. */
-       /* currently all INBOXES of all accounts need to be notified */
-       /* Dependent on GetMyIdentities for account names */
+       while(index) {
+               connection_info = index->data;
 
-       /* For testing - mailbox_num and mailbox_list are hardcoded here */
-       mailbox_num     = 1;
-       mailbox_list[0] = strdup("INBOX");
+               /* Removes all fd from epoll_fd */
+               ev.events  = EPOLLIN;
+               ev.data.fd = connection_info->socket_fd;
 
-       /* make a list of mailboxes IDLING */
-       for (counter = 0; counter < mailbox_num; counter++){
-               EM_DEBUG_EXCEPTION(">>>> emcore_imap_idle_run 4 ");
-               if (!emstorage_get_mailbox_by_name(accountID, 0,  mailbox_list[counter], &local_mailbox, true, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
+               if (epoll_ctl(input_epoll_fd, EPOLL_CTL_DEL, connection_info->socket_fd, &ev) == -1) {
+                       EM_DEBUG_LOG("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
                }
-               else {
-                       curr_mailbox             = em_malloc(sizeof(email_mailbox_t));
-                       curr_mailbox->account_id = local_mailbox->account_id;
-                       curr_mailbox->mailbox_name       = EM_SAFE_STRDUP(local_mailbox->mailbox_name);
-                       curr_mailbox->local      = local_mailbox->local_yn;
-                       if (!emcore_imap_idle_connect_and_idle_on_mailbox(curr_mailbox, &err)) {
-                               EM_DEBUG_EXCEPTION("emcore_imap_idle_connect_and_idle_on_mailbox failed [%d]", err);
-                               emcore_free_mailbox(curr_mailbox);
-                               EM_SAFE_FREE(curr_mailbox);
-                       }
-                       else {
-                               if (flag) {
-                                       mail_box_list = curr_mailbox;
-                                       prev_mailbox = curr_mailbox;
-                                       flag = false;
-                                       num++;
-                               }
-                               else {
-                                       prev_mailbox->next = curr_mailbox;
-                                       prev_mailbox = curr_mailbox;
-                                       num++;
-                               }
-                       }
-               }
-               if (local_mailbox != NULL)
-                       emstorage_free_mailbox(&local_mailbox, 1, NULL);
-       }
-
-       emcore_clear_session(session);
-       emcore_imap_idle_loop_start(mail_box_list, num, NULL);
 
-FINISH_OFF:
+               /* Close connection */
+               connection_info->mail_stream = mail_close(connection_info->mail_stream);
+               EM_SAFE_FREE(connection_info->multi_user_name);
+               EM_SAFE_FREE(connection_info);
 
-       if (!emstorage_close(&err)) {
-               EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
+               index = g_list_next(index);
        }
 
-       EM_DEBUG_FUNC_END();
-       return NULL;
+       g_list_free(*input_imap_idle_task_list);
+       *input_imap_idle_task_list = NULL;
+
+FINISH_OFF:
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
-int emcore_imap_idle_insert_sync_event(email_mailbox_t *mailbox, int *err_code)
+static int emcore_imap_idle_insert_sync_event(char *multi_user_name, int input_account_id, int input_mailbox_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int handle;
        email_event_t *event_data = NULL;
-       
-       if (!mailbox || mailbox->account_id <= 0) {
+
+       if (input_account_id <= 0) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       event_data = em_malloc(sizeof(email_event_t));
+       event_data                     = em_malloc(sizeof(email_event_t));
        event_data->type               = EMAIL_EVENT_SYNC_HEADER;
-       event_data->event_param_data_1 = mailbox ? EM_SAFE_STRDUP(mailbox->mailbox_name) : NULL;
-       event_data->event_param_data_3 = NULL;
-       event_data->account_id         = mailbox->account_id;
-                       
+       event_data->event_param_data_5 = input_mailbox_id;
+       event_data->account_id         = input_account_id;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
+
        if (!emcore_insert_event(event_data, &handle, &err)) {
                EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
                goto FINISH_OFF;
@@ -364,48 +193,106 @@ FINISH_OFF:
        return ret;
 }
 
-/* connects to given mailbox. send idle and returns socket id */
-static int emcore_imap_idle_connect_and_idle_on_mailbox(email_mailbox_t *mailbox, int *err_code)
+static int emcore_parse_imap_idle_response(char *multi_user_name, int input_account_id, int input_mailbox_id, MAILSTREAM *input_mailstream, int input_socket_fd)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox [%p], err_code [%p]", mailbox, err_code);
-       void          *mail_stream = NULL;
-       char           cmd[128] = { 0, };
-       char           tag[32] = { 0, };
-       char          *p = NULL;
-       int            socket_id = 0;
-       int            ret = 0;
-       int            err = EMAIL_ERROR_NONE;
-       email_account_t *ref_account = NULL;
-       IMAPLOCAL     *imap_local = NULL;
-       NETSTREAM     *net_stream = NULL;
-       TCPSTREAM     *tcp_stream = NULL;
-
-       /* NULL param check */
-       if (!mailbox) {
+       EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id [%d] input_mailstream[%p] input_socket_fd[%d]", input_account_id, input_mailbox_id, input_mailstream, input_socket_fd);
+       int err = EMAIL_ERROR_NONE;
+       char *p = NULL;
+       IMAPLOCAL *imap_local = NULL;
+
+       if (!input_mailstream) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox->account_id);
-       if (!ref_account) {
-               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed. account_id[%d]", mailbox->account_id);
-               err = EMAIL_ERROR_INVALID_ACCOUNT;
+       imap_local = input_mailstream->local;
+
+       if (!imap_local){
+               EM_DEBUG_EXCEPTION("imap_local is NULL");
+               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
+       while (imap_local->netstream){
+               p = net_getline(imap_local->netstream);
+               if (p && !strncmp(p, "*", 1)) {
+                       EM_DEBUG_LOG("p is [%s]", p);
+                       if (p+1 && p+2 && p+3 && p+4 && !strncmp(p+2, "BYE", 3)) {
+                               EM_DEBUG_LOG("BYE connection from server");
+                               EM_SAFE_FREE(p);
+                               err = EMAIL_ERROR_IMAP4_IDLE_FAILURE;
+                               goto FINISH_OFF;
+                       }
+                       else  {
+                               if (strstr(p, "EXIST") != NULL) {
+                                       if (!emcore_imap_idle_insert_sync_event(multi_user_name, input_account_id, input_mailbox_id, &err))
+                                               EM_DEBUG_EXCEPTION_SEC("Syncing mailbox[%d] failed with err_code [%d]", input_mailbox_id, err);
+                               }
+                               else
+                                       EM_DEBUG_LOG("Skipped this message");
+                               EM_SAFE_FREE(p);
+                               break;
+                       }
+               }
+               else if (p && (!strncmp(p, "+", 1))) {
+                       /* Bad response from server */
+                       EM_DEBUG_LOG("p is [%s]", p);
+                       EM_SAFE_FREE(p);
+                       break;
+               }
+               else {
+                       EM_DEBUG_LOG("In else part");
+                       break;
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+/* connects to given mailbox. send idle and returns socket id */
+static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **input_connection_list, email_account_t *input_account, email_mailbox_t *input_mailbox, MAILSTREAM **output_mailstream, int *output_socket_fd)
+{
+       EM_DEBUG_FUNC_BEGIN("input_connection_list [%p] input_account [%p] input_mailbox[%p] output_mailstream [%p] output_socket_fd [%p]", input_connection_list, input_account, input_mailbox, output_mailstream, output_socket_fd);
+       void            *mail_stream = NULL;
+       char             cmd[128] = { 0, };
+       char             tag[32] = { 0, };
+       char            *p = NULL;
+       int              socket_fd = 0;
+       int              err = EMAIL_ERROR_NONE;
+       email_session_t *session = NULL;
+       IMAPLOCAL       *imap_local = NULL;
+       NETSTREAM       *net_stream = NULL;
+       TCPSTREAM       *tcp_stream = NULL;
+       GList           *imap_idle_connection_list = NULL;
+       email_imap_idle_connection_info_t *connection_info = NULL;
+
+       if (input_connection_list == NULL || input_account == NULL || input_mailbox == NULL || output_mailstream == NULL || output_socket_fd == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       imap_idle_connection_list = *input_connection_list;
+
+       if (!emcore_get_empty_session(&session))
+               EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
+
        /* Open connection to mailbox */
-       if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_name, (void **)&mail_stream, &err) || !mail_stream) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_mailbox->account_id, input_mailbox->mailbox_id, (void **)&mail_stream, &err) || !mail_stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
 
        imap_local = ((MAILSTREAM *)mail_stream)->local;
        net_stream = imap_local->netstream;
-       
+
        /* check if ssl option is enabled on this account - shasikala.p */
 
-       if (ref_account->incoming_server_secure_connection){
+       if (input_account->incoming_server_secure_connection){
                SSLSTREAM *ssl_stream = net_stream->stream;
                tcp_stream = ssl_stream->tcpstream;
        }
@@ -413,14 +300,14 @@ static int emcore_imap_idle_connect_and_idle_on_mailbox(email_mailbox_t *mailbox
                tcp_stream = net_stream->stream;
 
        /* Get Socket ID */
-       socket_id = ((TCPSTREAM *)tcp_stream)->tcpsi;
+       socket_fd = ((TCPSTREAM *)tcp_stream)->tcpsi;
 
        sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)mail_stream)->gensym++));
        sprintf(cmd, "%s IDLE\015\012", tag);
 
        /* Send IDLE command */
        if (!imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
-               EM_DEBUG_EXCEPTION_SEC("network error - failed to IDLE on Mailbox - %s ", mailbox->mailbox_name);
+               EM_DEBUG_EXCEPTION_SEC("network error - failed to IDLE on Mailbox - %s ", input_mailbox->mailbox_name);
                err = EMAIL_ERROR_IMAP4_IDLE_FAILURE;
                goto FINISH_OFF;
        }
@@ -428,102 +315,370 @@ static int emcore_imap_idle_connect_and_idle_on_mailbox(email_mailbox_t *mailbox
        /* Get the response for IDLE command */
        while (imap_local->netstream){
                p = net_getline(imap_local->netstream);
+
                EM_DEBUG_LOG("p =[%s]", p);
-               if (!strncmp(p, "+", 1)) {
-                       ret = 1;
+
+               if (p && !strncmp(p, "+", 1)) {
+                       EM_DEBUG_LOG("OK. Go.");
                        break;
                }
-               else if (!strncmp(p, "*", 1)) {
+               else if (p && !strncmp(p, "*", 1)) {
                        EM_SAFE_FREE(p);
                        continue;
                }
                else {
-                       ret = 0;
+                       EM_DEBUG_LOG("Unsuspected response.");
+                       err = EMAIL_ERROR_IMAP4_IDLE_FAILURE;
                        break;
                }
     }
        EM_SAFE_FREE(p);
 
-FINISH_OFF: 
-       
-       if (ret) {
+       connection_info = em_malloc(sizeof(email_imap_idle_connection_info_t));
+       if (connection_info == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       connection_info->account_id      = input_account->account_id;
+       connection_info->mailbox_id      = input_mailbox->mailbox_id;
+       connection_info->mail_stream     = mail_stream;
+       connection_info->socket_fd       = socket_fd;
+       connection_info->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
+       imap_idle_connection_list = g_list_append(imap_idle_connection_list, (gpointer)connection_info);
+
+FINISH_OFF:
+
+       if (err == EMAIL_ERROR_NONE) {
                /* IMAP IDLE - SUCCESS */
-               mailbox->mail_stream     = mail_stream;
-               mailbox->hold_connection = socket_id; /* holds connection continuously on the given socket_id */
+//             *output_mailstream     = mail_stream;
+               *output_socket_fd      = socket_fd;
+               *input_connection_list = imap_idle_connection_list;
        }
        else if (mail_stream)
                mail_stream = mail_close (mail_stream);
 
-       if (ref_account) {
-               emcore_free_account(ref_account);
-               EM_SAFE_FREE(ref_account);
+       emcore_clear_session(session);
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+static int emcore_imap_idle_cb(email_alarm_data_t *alarm_data, void *user_parameter)
+{
+       EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_parameter [%p]", alarm_data, user_parameter);
+       int err = EMAIL_ERROR_NONE;
+
+       emcore_refresh_imap_idle_thread();
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+static int emcore_refresh_alarm_for_imap_idle(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int i = 0;
+       int account_count = 0;
+       int auto_sync_account_count = 0;
+       time_t current_time;
+       time_t trigger_at_time;
+       email_account_t *account_ref_list = NULL;
+
+       /* Check whether the alarm is already existing */
+       if(emcore_check_alarm_by_class_id(EMAIL_ALARM_CLASS_IMAP_IDLE) == EMAIL_ERROR_NONE) {
+               EM_DEBUG_LOG("Already exist. Remove old one.");
+               emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_IMAP_IDLE, 0);
        }
 
-       if (err_code)
-               *err_code = err;
+       if ((err = emcore_get_account_reference_list(multi_user_name, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
+               goto FINISH_OFF;
+       }
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+       for (i = 0; i < account_count; i++) {
+               if (account_ref_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && (account_ref_list[i].check_interval == 0 || account_ref_list[i].peak_interval == 0)) {
+                       auto_sync_account_count++;
+               }
+       }
+
+       if(account_ref_list)
+               emcore_free_account_list(&account_ref_list, account_count, NULL);
+
+       if (auto_sync_account_count) {
+               time(&current_time);
+               trigger_at_time = current_time + (29 * 60);
+
+               if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_IMAP_IDLE, 0, emcore_imap_idle_cb, NULL)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
-static int emcore_imap_idle_parse_response_stream(email_mailbox_t *mailbox, int *err_code)
+void* emcore_imap_idle_worker(void* thread_user_data)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox [%p], err_code [%p]", mailbox, err_code);
+       EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
-       char *p = NULL;
-       int ret = false;
-       IMAPLOCAL *imap_local = NULL;
-       
-       if (!mailbox || !mailbox->mail_stream) {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
+       int i = 0, j = 0;
+       int event_num = 0;
+       int epoll_fd = 0;
+       int socket_fd = 0;
+       int signal_from_pipe = 0;
+       int refresh_connection_flag = 1;
+       int account_count = 0;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+       struct epoll_event ev = {0};
+       struct epoll_event events[MAX_EPOLL_EVENT] = {{0}, };
+       MAILSTREAM *mail_stream = NULL;
+       email_mailbox_t inbox_mailbox = {0};
+       email_account_t *account_ref_list = NULL;
+       GList *imap_idle_connection_list = NULL;
+       GList *zone_name_list = NULL;
+       GList *node = NULL;
+       email_imap_idle_connection_info_t *connection_info = NULL;
+
+       EM_DEBUG_LOG("emcore_imap_idle_worker start ");
+
+       if (pipe(imap_idle_pipe_fd) == -1) {
+               EM_DEBUG_EXCEPTION("pipe failed");
+               err = EMAIL_ERROR_UNKNOWN;
                goto FINISH_OFF;
        }
 
-       imap_local = ((MAILSTREAM *)mailbox->mail_stream)->local;
+       epoll_fd = epoll_create(MAX_EPOLL_EVENT);
 
-       if (!imap_local){
-               EM_DEBUG_EXCEPTION("imap_local is NULL");
-               err = EMAIL_ERROR_INVALID_PARAM;
+       if (epoll_fd < 0) {
+               EM_DEBUG_CRITICAL_EXCEPTION("epoll_create failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+               err = EMAIL_ERROR_IMAP4_IDLE_FAILURE;
                goto FINISH_OFF;
        }
 
-       while (imap_local->netstream){
-               p = net_getline(imap_local->netstream);
-               if (p && !strncmp(p, "*", 1)) {
-                       EM_DEBUG_LOG("p is [%s]", p);
-                       if (p+1 && p+2 && p+3 && p+4 && !strncmp(p+2, "BYE", 3)) {
-                               EM_DEBUG_LOG("BYE connection from server");
-                               EM_SAFE_FREE(p);
-                               goto FINISH_OFF;
+       ev.events  = EPOLLIN;
+       ev.data.fd = imap_idle_pipe_fd[0];
+
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, imap_idle_pipe_fd[0], &ev) == -1) {
+               EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+       }
+
+       EM_DEBUG_LOG("Enter imap idle loop");
+       while (1) {
+               if (refresh_connection_flag) {
+
+                       EM_DEBUG_LOG("Clear old connections");
+                       emcore_clear_old_connections(epoll_fd, &imap_idle_connection_list);
+
+                       EM_DEBUG_LOG("Getting account list");
+                       if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
                        }
-                       else  { 
-                               if (!emcore_imap_idle_insert_sync_event(mailbox, &err))
-                                       EM_DEBUG_EXCEPTION_SEC("Syncing mailbox %s failed with err_code [%d]", mailbox->mailbox_name, err);
-                               EM_SAFE_FREE(p);        
-                               break;
+
+                       if (err != EMAIL_ERROR_NONE) {
+                               if ((err = emcore_get_account_reference_list(NULL, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) 
+                                       EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
+
+                               for (i = 0; i < account_count; i++) {
+                                       if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
+                                               EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id);
+                                               continue;
+                                       }
+
+                                       /* TODO: peak schedule handling */
+
+                                       memset(&inbox_mailbox, 0, sizeof(email_mailbox_t));
+
+                                       if (!emcore_get_mailbox_by_type(NULL, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) {
+                                               EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err);
+                                               continue;
+                                       }
+
+                                       EM_DEBUG_LOG("Connect to IMAP server");
+                                       err = emcore_connect_and_idle_on_mailbox(NULL, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd);
+
+                                       emcore_free_mailbox(&inbox_mailbox);
+
+                                       if (err != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_connect_and_idle_on_mailbox failed [%d]", err);
+                                               continue;
+                                       }
+
+                                       ev.events  = EPOLLIN;
+                                       ev.data.fd = socket_fd;
+
+                                       EM_DEBUG_LOG("Add listener for socket buffer");
+                                       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev) == -1) {
+                                               EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+                                       }
+                               }
+
+                               if(account_ref_list)
+                                       emcore_free_account_list(&account_ref_list, account_count, NULL);
+
+                               EM_DEBUG_LOG(" Delete old an alarm and add an alarm to refresh connections every 29min");
+                               emcore_refresh_alarm_for_imap_idle(NULL);
+
+                               refresh_connection_flag = 0;
+                       } else {
+                               node = g_list_first(zone_name_list);
+                               while (node != NULL) {
+                                       if (!node->data)
+                                               node = g_list_next(node);
+
+                                       EM_DEBUG_LOG("Data name of node : [%s]", node->data);
+
+                                       if ((err = emcore_get_account_reference_list(node->data, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
+                                       }
+
+                                       for (i = 0; i < account_count; i++) {
+                                               if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) {
+                                                       EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id);
+                                                       continue;
+                                               }
+
+                                               /* TODO: peak schedule handling */
+
+                                               memset(&inbox_mailbox, 0, sizeof(email_mailbox_t));
+
+                                               if (!emcore_get_mailbox_by_type(node->data, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) {
+                                                       EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err);
+                                                       continue;
+                                               }
+
+                                               EM_DEBUG_LOG("Connect to IMAP server");
+                                               err = emcore_connect_and_idle_on_mailbox(node->data, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd);
+
+                                               emcore_free_mailbox(&inbox_mailbox);
+
+                                               if (err != EMAIL_ERROR_NONE) {
+                                                       EM_DEBUG_EXCEPTION("emcore_connect_and_idle_on_mailbox failed [%d]", err);
+                                                       continue;
+                                               }
+
+                                               ev.events  = EPOLLIN;
+                                               ev.data.fd = socket_fd;
+
+                                               EM_DEBUG_LOG("Add listener for socket buffer");
+                                               if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev) == -1) {
+                                                       EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+                                               }
+                                       }
+
+                                       if(account_ref_list)
+                                               emcore_free_account_list(&account_ref_list, account_count, NULL);
+
+                                       EM_DEBUG_LOG(" Delete old an alarm and add an alarm to refresh connections every 29min");
+                                       emcore_refresh_alarm_for_imap_idle(node->data);
+
+                                       EM_SAFE_FREE(node->data);
+                                       node = g_list_next(node);
+                               }
+                               g_list_free(zone_name_list);
+
+                               refresh_connection_flag = 0;
                        }
                }
-               else if (p && (!strncmp(p, "+", 1))) {
-                       /* Bad response from server */
-                       EM_DEBUG_LOG("p is [%s]", p);
-                       EM_SAFE_FREE(p);
-                       break;
-               }
-               else {
-                       EM_DEBUG_LOG("In else part");
-                       break;
+
+               EM_DEBUG_LOG("Waiting.......");
+               event_num = epoll_wait(epoll_fd, events, MAX_EPOLL_EVENT, -1);
+               EM_DEBUG_LOG("epoll_wait returns [%d]", event_num);
+
+               if (event_num > 0)  {
+                       EM_DEBUG_LOG(">>>> Data coming from socket or pipe ");
+                       for (j = 0; j < event_num; j++) {
+                               int event_fd = events[j].data.fd;
+
+                               EM_DEBUG_LOG("event_fd [%d]", event_fd);
+
+                               if (event_fd == imap_idle_pipe_fd[0]) {
+                                       EM_DEBUG_LOG(">>>> Data coming from pipe ");
+                                       if (read(imap_idle_pipe_fd[0], (void*)&signal_from_pipe, sizeof(signal_from_pipe)) != -1) {
+                                               EM_DEBUG_LOG("Refresh IMAP connections");
+                                               refresh_connection_flag = 1;
+                                               continue;
+                                       }
+                               }
+                               else {
+                                       if ((err = emcore_get_connection_info_by_socket_fd(imap_idle_connection_list, event_fd, &connection_info)) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_LOG("emcore_get_connection_info_by_socket_fd couldn't find proper connection info. [%d]", err);
+                                               continue;
+                                       }
+
+                                       if ((err = emcore_parse_imap_idle_response(connection_info->multi_user_name, 
+                                                                                                                       connection_info->account_id, 
+                                                                                                                       connection_info->mailbox_id, 
+                                                                                                                       connection_info->mail_stream, 
+                                                                                                                       connection_info->socket_fd)) != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION("emcore_parse_imap_idle_response failed. [%d] ", err);
+                                               refresh_connection_flag = 1;
+//                                             mail_stream = mail_close (mail_stream);
+                                               continue;
+                                       }
+                               }
+                       }
                }
        }
 
-       ret = true;
+       EM_DEBUG_LOG("Exit imap idle loop");
 
 FINISH_OFF:
+       emcore_free_mailbox(&inbox_mailbox);
 
-       if (err_code)
-               *err_code = err;
+       EM_DEBUG_LOG("Close pipes");
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+       close(imap_idle_pipe_fd[0]);
+       close(imap_idle_pipe_fd[1]);
+
+       imap_idle_thread_id = 0;
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return NULL;
+}
+
+
+INTERNAL_FUNC int emcore_create_imap_idle_thread()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int thread_error;
+
+       if (imap_idle_thread_id) {
+               EM_DEBUG_EXCEPTION("IMAP IDLE thread already exists.");
+               err = EMAIL_ERROR_ALREADY_EXISTS;
+               goto FINISH_OFF;
+
+       }
+
+       THREAD_CREATE(imap_idle_thread_id, emcore_imap_idle_worker, NULL, thread_error);
+
+       if (thread_error != 0) {
+               EM_DEBUG_EXCEPTION("cannot make IMAP IDLE thread...");
+               err = EMAIL_ERROR_UNKNOWN;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+INTERNAL_FUNC int emcore_refresh_imap_idle_thread()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int signal = 1;
+
+       write(imap_idle_pipe_fd[1], (char*) &signal, sizeof (signal));
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 #endif /*  __FEATURE_IMAP_IDLE__ */
index aa3b5c8..bc98d41 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -25,9 +25,9 @@
  * File :  email-core-imap_folder.c
  * Desc :  Mail IMAP mailbox
  *
- * Auth : 
+ * Auth :
  *
- * History : 
+ * History :
  *    2006.08.01  :  created
  *****************************************************************************/
 #include <stdio.h>
@@ -42,8 +42,9 @@
 #include "email-core-event.h"
 #include "email-core-mailbox.h"
 #include "email-core-imap-mailbox.h"
+#include "email-core-imap-idle.h"
 #include "email-core-mailbox-sync.h"
-#include "email-core-account.h" 
+#include "email-core-account.h"
 #include "email-core-signal.h"
 
 #include "lnx_inc.h"
@@ -51,7 +52,7 @@
 
 #include "email-debug-log.h"
 
-INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *output_count)
+INTERNAL_FUNC int emcore_get_default_mail_slot_count(char *multi_user_name, int input_account_id, int *output_count)
 {      
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d] output_count[%p]", input_account_id, output_count);
 
@@ -64,17 +65,17 @@ INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(input_account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, input_account_id);
        if (account_ref)
                default_mail_slot_count = account_ref->default_mail_slot_size;
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (account_ref) {
                emcore_free_account(account_ref);
                EM_SAFE_FREE(account_ref);
        }
-       
+
        if (output_count)
                *output_count = default_mail_slot_count;
 
@@ -83,15 +84,15 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
+INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name, emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
 
-       int ret = false; 
+       int ret = false;
        int *mail_id_list = NULL, mail_id_list_count = 0;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account_ref = NULL;
-       
+
        if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
                if (intput_mailbox_tbl)
                EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
@@ -99,7 +100,7 @@ INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, intput_mailbox_tbl->account_id);
        if (account_ref) {
                if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
@@ -107,8 +108,8 @@ INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput
                        goto FINISH_OFF;
                }
        }
-       
-       if (!emstorage_get_overflowed_mail_id_list(intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_id, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
+
+       if (!emstorage_get_overflowed_mail_id_list(multi_user_name, intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_id, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
                if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
                        EM_DEBUG_LOG_SEC("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
                        err = EMAIL_ERROR_NONE;
@@ -120,14 +121,14 @@ INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput
        }
 
        if (mail_id_list) {
-               if (!emcore_delete_mail(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
+               if (!emcore_delete_mail(multi_user_name, intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
-       
+
        ret = true;
-FINISH_OFF: 
+FINISH_OFF:
        EM_SAFE_FREE(mail_id_list);
 
        if (account_ref) {
@@ -143,11 +144,11 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
+INTERNAL_FUNC int emcore_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
 
-       int ret = false, err = EMAIL_ERROR_NONE; 
+       int ret = false, err = EMAIL_ERROR_NONE;
        int i = 0;
        int account_count = 100;
        int mailbox_count = 0;
@@ -156,7 +157,7 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
        emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
 
        if (account_id > ALL_ACCOUNT) {
-               account_ref = emcore_get_account_reference(account_id);
+               account_ref = emcore_get_account_reference(multi_user_name, account_id);
                if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
                        ret = true;
@@ -167,7 +168,7 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
                        goto FINISH_OFF;
                }
                if (mailbox_id == 0) {
-                       if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
+                       if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -175,12 +176,12 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
        }
        else {
                if (mailbox_id == 0) {
-                       if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
+                       if ( !emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_list, false, false, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                                goto FINISH_OFF;
                        }
                        for ( i = 0; i < account_count; i++) {
-                               if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
+                               if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
                                        goto FINISH_OFF;
                                }
@@ -188,8 +189,7 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
                }
        }
 
-
-       if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
+       if (!emstorage_set_mail_slot_size(multi_user_name, account_id, mailbox_id, new_slot_size, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -204,25 +204,25 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
                        }
                        mailbox_tbl_list->account_id = account_id;
                        mailbox_tbl_list->mailbox_id = mailbox_id;
-                       mailbox_tbl_list->mail_slot_size = new_slot_size;     
+                       mailbox_tbl_list->mail_slot_size = new_slot_size;
                }
                else   {        /*  read information from DB */
-                       if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
+                       if ((err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                                goto FINISH_OFF;
                        }
-                       
+
                }
        }
        else {
-               if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
+               if (!emstorage_get_mailbox_list(multi_user_name, account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
        for (i = 0; i < mailbox_count; i++) {
-               if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
+               if (!emcore_remove_overflowed_mails(multi_user_name, mailbox_tbl_list + i, &err)) {
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
                                err = EMAIL_ERROR_NONE;
                        else
@@ -231,8 +231,8 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
        }
 
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
 
        if (account_ref) {
                emcore_free_account(account_ref);
@@ -251,7 +251,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
+static int emcore_get_mailbox_connection_path(char *multi_user_name, int account_id, char *mailbox_name, char **path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_name[%s], err_code[%p]", account_id, mailbox_name, err_code);
        email_account_t *ref_account = NULL;
@@ -259,7 +259,7 @@ static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name
        int ret = false;
        int err = EMAIL_ERROR_NONE;
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        if (!ref_account)        {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
                goto FINISH_OFF;
@@ -310,14 +310,17 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
+       if (err_code)
+               *err_code = err;
+
        if (!ret)
                return 0;
 
        return 1;
 }
 
-static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_mailbox_t *remote_box_list, 
-                                               int remote_box_count, emstorage_mailbox_tbl_t *local_box_list, int local_box_count, 
+static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_mailbox_t *remote_box_list,
+                                               int remote_box_count, emstorage_mailbox_tbl_t *local_box_list, int local_box_count,
                                                GList** remote_box_only, GList** local_box_only)
 {
        if ( !remote_box_only || !local_box_only ) {
@@ -357,7 +360,7 @@ static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_ma
                        EM_DEBUG_LOG_DEV ("vs local [%s]", local_box->mailbox_name);
                        if (!EM_SAFE_STRCMP (remote_box->mailbox_name, local_box->mailbox_name)) {
                                /* It is unnecessary to compare the matched box in the next iteration, so remove it from local_box*/
-                               local_head = g_list_delete_link (local_head, local_p);                          
+                               local_head = g_list_delete_link (local_head, local_p);
                                matched = true;
                                break;
                        }
@@ -371,15 +374,16 @@ static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_ma
 
        /* local_head contains unmatched local box */
        *local_box_only = local_head;
+
+       if (remote_head) g_list_free(remote_head);
 }
 
-INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
+INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id, char *mailbox_name, int event_handle, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
-       
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, event_handle, err_code);
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int status = EMAIL_DOWNLOAD_FAIL;
        MAILSTREAM *stream = NULL;
        email_internal_mailbox_t *mailbox_list = NULL;   /* mailbox list from imap server */
        /* mailbox list from DB */
@@ -392,68 +396,59 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
        char *mbox_path = NULL;
        char *mailbox_name_for_mailbox_type = NULL;
        int   i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
-       
-       if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
+       int   inbox_added = 0;
+
+       if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, event_handle, 0))
                EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        if (!emnetwork_check_network_status(&err)) {
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
                goto FINISH_OFF;
        }
        
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
+
        /* if not imap4 mail, return */
        if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("unsupported account...");
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
+
        /*  get mail server path */
        /*  mbox_path is not used. the below func might be unnecessary */
-       if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path)  {
+       if (!emcore_get_mailbox_connection_path(multi_user_name, account_id, NULL, &mbox_path, &err) || !mbox_path)  {
                EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
                goto FINISH_OFF;
        }
-       
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+
+
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
-               
+
                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
                        err = EMAIL_ERROR_CANCELLED;
                else
                        err = EMAIL_ERROR_CONNECTION_FAILURE;
-               
-               status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
                goto FINISH_OFF;
        }
-       
+
        EM_SAFE_FREE(mbox_path);
-       
+
        stream = (MAILSTREAM *)tmp_stream;
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
+
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        /*  download mailbox list */
        if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err))  {
                EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed [%d]", err);
@@ -461,7 +456,7 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
        }
 
        /* get all mailboxes which is previously synced */
-       if (!emstorage_get_mailbox_list (account_id, EMAIL_MAILBOX_FROM_SERVER, EMAIL_MAILBOX_SORT_BY_NAME_ASC,\
+       if (!emstorage_get_mailbox_list (multi_user_name, account_id, EMAIL_MAILBOX_FROM_SERVER, EMAIL_MAILBOX_SORT_BY_NAME_ASC,\
                                                         &local_mailbox_count, &local_mailbox_list, 1, &err)) {
                if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list error [%d]", err);
@@ -470,11 +465,8 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
                        EM_DEBUG_LOG ("mailbox not found");
        }
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        emcore_find_mailbox_diff_between_local_and_remote (mailbox_list, count, local_mailbox_list, local_mailbox_count,
                                                                                        &remote_box_only, &local_box_only);
 
@@ -483,11 +475,7 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
        for (; p; p = g_list_next (p)) {
                email_internal_mailbox_t *new_mailbox = (email_internal_mailbox_t *) g_list_nth_data (p, 0);
 
-               if (!emcore_check_thread_status())  {
-                       EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                if (!new_mailbox->mailbox_name) {
                        continue;
@@ -525,13 +513,16 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
 
                if (new_mailbox->alias) {
                        mailbox_tbl.alias = new_mailbox->alias;
-                       mailbox_tbl.modifiable_yn = 1; 
+                       mailbox_tbl.modifiable_yn = 1;
                        mailbox_tbl.total_mail_count_on_server = 0;
-                               
-                       if (!emstorage_add_mailbox (&mailbox_tbl, true, &err)) {
+
+                       if (!emstorage_add_mailbox (multi_user_name, &mailbox_tbl, true, &err)) {
                                EM_DEBUG_EXCEPTION ("emstorage_add_mailbox error [%d]", err);
                                goto FINISH_OFF;
                        }
+
+                       if (mailbox_tbl.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
+                               inbox_added = 1;
                        EM_DEBUG_LOG_SEC ("MAILBOX ADDED: mailbox_name [%s] alias [%s] mailbox_type [%d]", new_mailbox->mailbox_name,\
                                                                new_mailbox->alias, mailbox_tbl.mailbox_type);
                }
@@ -541,14 +532,22 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
        p = local_box_only;
        for (; p; p = g_list_next (p)) {
                emstorage_mailbox_tbl_t *del_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (p, 0);
-               if (!emstorage_delete_mail_by_mailbox (del_box->account_id, del_box->mailbox_id, 1, &err)) {
+
+               if (!emstorage_delete_mail_by_mailbox (multi_user_name, del_box, 1, &err)) {
                        EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
                                                        err, del_box->account_id, del_box->mailbox_name);
                }
-               if (!emstorage_delete_mailbox (del_box->account_id, EMAIL_MAILBOX_FROM_SERVER, del_box->mailbox_id, 1, &err)) {
-                       EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
+
+               if (!emstorage_delete_mailbox (multi_user_name, del_box->account_id, EMAIL_MAILBOX_FROM_SERVER, del_box->mailbox_id, 1, &err)) {
+                       EM_DEBUG_EXCEPTION ("emstorage_delete_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
                                                        err, del_box->account_id, del_box->mailbox_name);
                }
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+               if (!emstorage_delete_auto_download_activity_by_mailbox(multi_user_name, del_box->account_id, del_box->mailbox_id, 1, &err))
+                       EM_DEBUG_EXCEPTION("emstorage_delete_auto_download_activity_by_mailbox failed");
+#endif
+
                EM_DEBUG_LOG_SEC ("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
        }
 
@@ -558,7 +557,7 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
                        emstorage_mailbox_tbl_t mailbox_tbl;
                        emstorage_mailbox_tbl_t *result_mailbox_tbl = NULL;
 
-                       if(emstorage_get_mailbox_by_mailbox_type(account_id, counter, &result_mailbox_tbl, true, &err2)) {
+                       if(emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, counter, &result_mailbox_tbl, true, &err2)) {
                                if(result_mailbox_tbl) {
                                        emstorage_free_mailbox(&result_mailbox_tbl, 1, NULL);
                                        continue;
@@ -608,37 +607,37 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
                                        break;
                        }
 
-                       if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
+                       if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
                                goto FINISH_OFF;
                        }
                }
                EM_SAFE_FREE(mailbox_name_for_mailbox_type);
        }
-       if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
+
+       if (!emstorage_set_all_mailbox_modifiable_yn(multi_user_name, account_id, 0, true, &err)) {
                EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
                goto FINISH_OFF;
        }
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
+
+       if (inbox_added)
+               emcore_refresh_imap_idle_thread();
+
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        for (i = 0; i < count; i++)
                mailbox_list[i].account_id = account_id;
-       
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
 
        if (err == EMAIL_ERROR_NONE) {
-               if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
+               if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, event_handle, err))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
        }
        else {
-               if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
+               if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, event_handle, err))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
        }
        EM_SAFE_FREE(mailbox_name_for_mailbox_type);
@@ -649,16 +648,16 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
-       if (stream) 
+       if (stream)
                stream = mail_close (stream);
-       
-       if (mailbox_list) 
+
+       if (mailbox_list)
                emcore_free_internal_mailbox (&mailbox_list, count, NULL);
 
-       if (local_mailbox_list) 
+       if (local_mailbox_list)
                emstorage_free_mailbox (&local_mailbox_list, local_mailbox_count, NULL);
 
-       if (local_box_only) 
+       if (local_box_only)
                g_list_free (local_box_only);
 
        if (remote_box_only)
@@ -670,10 +669,10 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_download_mailbox_list(void *mail_stream, 
+int emcore_download_mailbox_list(void *mail_stream,
                                                                                char *mailbox_name,
                                                                                email_internal_mailbox_t **mailbox_list,
-                                                                               int *count, 
+                                                                               int *count,
                                                                                int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_stream [%p], mailbox_name [%p], mailbox_list [%p], count [%p], err_code [%p]", mail_stream, mailbox_name, mailbox_list, count, err_code);
@@ -689,7 +688,7 @@ int emcore_download_mailbox_list(void *mail_stream,
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        memset(&holder, 0x00, sizeof(holder));
 
     /*  reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
@@ -722,7 +721,7 @@ int emcore_download_mailbox_list(void *mail_stream,
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (err_code)
                *err_code = err;
 
@@ -737,13 +736,14 @@ FINISH_OFF:
  *    succeed  :  1
  *    fail  :  0
  */
-INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
+INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
 {
        MAILSTREAM *stream = NULL;
        char *long_enc_path = NULL;
        void *tmp_stream = NULL;
        int ret = false;
        int err = EMAIL_ERROR_NONE;
+       email_session_t *session = NULL;
 
        EM_DEBUG_FUNC_BEGIN();
 
@@ -752,9 +752,12 @@ INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_
                goto FINISH_OFF;
        }
 
+       if (!emcore_get_empty_session(&session))
+               EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
+
        /* connect mail server */
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -762,7 +765,7 @@ INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_
        stream = (MAILSTREAM *) tmp_stream;
 
        /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
-       if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
+       if (!emcore_get_long_encoded_path(multi_user_name, mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -770,7 +773,18 @@ INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_
        /* create mailbox */
        if (!mail_create(stream, long_enc_path)) {
                EM_DEBUG_EXCEPTION("mail_create failed");
-               err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
+
+               if (!emcore_get_current_session(&session)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
+                       err = EMAIL_ERROR_SESSION_NOT_FOUND;
+                       goto FINISH_OFF;
+               }
+
+               if (session->error == EMAIL_ERROR_ALREADY_EXISTS)
+                       err = session->error;
+               else
+                       err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
+
                goto FINISH_OFF;
        }
 
@@ -794,6 +808,8 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
        }
 
+       emcore_clear_session(session);
+
        if (err_code)
                *err_code = err;
 
@@ -809,7 +825,7 @@ FINISH_OFF:
  *    succeed  :  1
  *    fail  :  0
  */
-INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
+INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -824,12 +840,12 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code
        if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
                EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
 
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
@@ -838,7 +854,7 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -846,7 +862,7 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code
        stream = (MAILSTREAM *)tmp_stream;
 
        /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
-       if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
+       if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -891,7 +907,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
+INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p] handle_to_be_published[%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, handle_to_be_published);
        MAILSTREAM *stream = NULL;
@@ -907,7 +923,7 @@ INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(input_account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, input_account_id);
 
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
@@ -917,7 +933,7 @@ INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int
 
        /* connect mail server */
        stream = NULL;
-       if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_account_id, 0, (void **)&tmp_stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -925,13 +941,13 @@ INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int
        stream = (MAILSTREAM *)tmp_stream;
 
        /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
-       if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
+       if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
                goto FINISH_OFF;
        }
 
        /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
-       if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
+       if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -1008,12 +1024,12 @@ INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resour
        email_account_t *ref_account = NULL;
        emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
 
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
@@ -1022,7 +1038,7 @@ INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resour
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1043,12 +1059,12 @@ INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root,
        email_account_t *ref_account = NULL;
        emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
 
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
@@ -1057,7 +1073,7 @@ INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root,
        }
 
        /* connect mail server */
-       if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
index 3c7eab1..4ed6f8d 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -25,9 +25,9 @@
  * File :  email-core-mail.c
  * Desc :  Mail Operation
  *
- * Auth : 
+ * Auth :
  *
- * History : 
+ * History :
  *    2006.08.16  :  created
  *****************************************************************************/
 #undef close
@@ -45,8 +45,9 @@
 #include <glib.h>
 #include <glib/gstdio.h>
 
-#include <vconf.h> 
-//#include <contacts.h>
+#include <vconf.h>
+#include <contacts.h>
+#include <contacts_internal.h>
 
 #include "email-internal-types.h"
 #include "c-client.h"
 #include "email-utilities.h"
 #include "email-core-global.h"
 #include "email-core-utils.h"
-#include "email-core-mail.h"
 #include "email-core-mime.h"
 #include "email-core-mailbox.h"
 #include "email-storage.h"
 #include "email-network.h"
 #include "email-core-mailbox-sync.h"
 #include "email-core-event.h"
-#include "email-core-account.h" 
+#include "email-core-account.h"
 #include "email-core-signal.h"
 #include "email-core-smtp.h"
+#include "email-core-container.h"
 
 #include "email-convert.h"
 #include "email-debug-log.h"
 #include "email-core-gmime.h"
 #endif /* __FEATURE_USE_GMIME__ */
 
-#ifdef __FEATURE_DRM__
-#include <drm_client.h>
-#endif /* __FEATURE_DRM__ */
 
 #define ST_SILENT   (long) 0x2 /* don't return results */
 #define ST_SET      (long) 0x4 /* set vs. clear */
 
 static char g_new_server_uid[129];
+static int g_copyuid_account_id = 0;
+static char *g_multi_user_name = NULL;
 
 bool only_body_download = false;
 
@@ -95,7 +95,7 @@ int _imap4_last_notified_body_size = 0;
 int _imap4_total_body_size = 0;
 int _imap4_download_noti_interval_value = 0;
 
-static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count);
+static int emcore_delete_mails_from_pop3_server(char *multi_user_name, email_account_t *input_account, int input_mail_ids[], int input_mail_id_count);
 static int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *total_body_size, int *err_code);
 
 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
@@ -133,9 +133,9 @@ static char *_getEncoding(int encoding)
 static void _print_parameter(PARAMETER *param)
 {
        while (param != NULL) {
-               EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
-               EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
-               
+               EM_DEBUG_LOG("param->attribute[%s]", param->attribute);
+               EM_DEBUG_LOG("param->value[%s]", param->value);
+
                param = param->next;
        }
 }
@@ -145,7 +145,7 @@ static void _print_stringlist(STRINGLIST *stringlist)
        while (stringlist != NULL) {
                EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
                EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
-               
+
                stringlist = stringlist->next;
        }
 }
@@ -153,56 +153,56 @@ static void _print_stringlist(STRINGLIST *stringlist)
 void _print_body(BODY *body, int recursive)
 {
        EM_DEBUG_LOG(" ========================================================== ");
-       
+
        if (body != NULL) {
                EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
                EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
                EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
-               
+
                EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
-               
+
                _print_parameter(body->parameter);
-               
+
                EM_DEBUG_LOG_SEC("body->id[%s]", body->id);
                EM_DEBUG_LOG("body->description[%s]", body->description);
-               
+
                EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
                EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
-               
+
                _print_parameter(body->disposition.parameter);
-               
+
                EM_DEBUG_LOG("body->language[%p]", body->language);
-               
+
                _print_stringlist(body->language);
-               
+
                EM_DEBUG_LOG_SEC("body->location[%s]", body->location);
-       
+
                EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
                EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
                EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
-               
+
                EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
                EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
                EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
-       
+
                EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
-               
+
                EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
                EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
-               
+
                EM_DEBUG_LOG("body->md5[%s]", body->md5);
                EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
-               
+
                if (recursive)  {
                        PART *part = body->nested.part;
-                       
+
                        while (part != NULL)  {
                                _print_body(&(part->body), recursive);
                                part = part->next;
                        }
                }
        }
-       
+
        EM_DEBUG_LOG(" ========================================================== ");
 }
 #endif /*  FEATURE_CORE_DEBUG */
@@ -210,48 +210,48 @@ void _print_body(BODY *body, int recursive)
 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        POP3LOCAL *pop3local = NULL;
        char cmd[64];
        char *p = NULL;
-       
+
        if (!stream)  {
                EM_DEBUG_EXCEPTION("stream[%p]", stream);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        if (!(pop3local = stream->local) || !pop3local->netstream) {
                EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
                err = EMAIL_ERROR_UNKNOWN;
                goto FINISH_OFF;
        }
-       
+
        memset(cmd, 0x00, sizeof(cmd));
-       
+
        SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
-       
+
 #ifdef FEATURE_CORE_DEBUG
        EM_DEBUG_LOG("[POP3] >>> %s", cmd);
 #endif
-       
+
        /*  send command  :  delete specified mail */
        if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
                EM_DEBUG_EXCEPTION("net_sout failed...");
                err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN; */
                goto FINISH_OFF;
        }
-       
+
        /*  receive response */
        if (!(p = net_getline(pop3local->netstream))) {
                EM_DEBUG_EXCEPTION("net_getline failed...");
                err = EMAIL_ERROR_INVALID_RESPONSE;
                goto FINISH_OFF;
        }
-       
+
 #ifdef FEATURE_CORE_DEBUG
        EM_DEBUG_LOG("[POP3] <<< %s", p);
 #endif
@@ -260,17 +260,17 @@ static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
                err = EMAIL_ERROR_POP3_DELE_FAILURE;            /* EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
                goto FINISH_OFF;
        }
-       
+
        if (*p != '+') {                /*  '+OK' ... */
                err = EMAIL_ERROR_INVALID_RESPONSE;
                goto FINISH_OFF;
        }
-       
+
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        EM_SAFE_FREE(p);
-       
+
        if (err_code)
                *err_code = err;
 
@@ -281,16 +281,16 @@ FINISH_OFF:
 
 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
 {
-       
+
        EM_DEBUG_FUNC_BEGIN();
 
        int index = -1;
        int i = 0;
        int err = EMAIL_ERROR_NONE;
-       
+
        unsigned long  first_uid = 0;
        unsigned long last_uid = 0;
-       
+
        unsigned long *old_server_uid = NULL;
        unsigned long *new_server_uid = NULL;
        int count = 0;
@@ -298,13 +298,14 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
 
        char old_server_uid_char[129];
        char new_server_uid_char[129];
+       emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
        if (NULL == sourceset || NULL == destset) {
-               /* 
-               sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids  
+               /*
+               sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids
                if sourceset is NULL then corresponding dest set will be NULL
                */
-               
+
                EM_DEBUG_LOG("emcore_mail_copyuid_ex failed :  Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
                return;
        }
@@ -326,19 +327,16 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
                                }
                        }
                }
-               
+
                temp = temp->next;
        }
 
 
-       
-
        EM_DEBUG_LOG("Count of mails copied [%d]", count);
        old_server_uid = em_malloc(count * sizeof(unsigned long));
        new_server_uid = em_malloc(count * sizeof(unsigned long));
 
        /* While loop below will collect all old server uid from sourceset into old_server_uid array */
-       
        while (sourceset) {
                if (sourceset->first > 0) {
                        first_uid = sourceset->first;
@@ -357,7 +355,7 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
                                }
                        }
                }
-               
+
                sourceset = sourceset->next;
        }
 
@@ -365,7 +363,7 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
 
        index = -1;
        first_uid = last_uid = 0;
-       
+
        while (destset) {
                if (destset->first > 0) {
                        first_uid = destset->first;
@@ -384,47 +382,67 @@ static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned l
                                }
                        }
                }
-               
+
                destset = destset->next;
        }
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       if (!emstorage_get_mailbox_by_name(g_multi_user_name, g_copyuid_account_id, -1, mailbox, &mailbox_tbl, false, &err))  {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d", err);
+       }
+#endif
+
        /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
-       
+
        for (i = 0; i <= index; ++i) {
 
                memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
                sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
 
                EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
-               
+
                memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
                sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
 
                EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
-       
-               if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
+
+               if (!emstorage_update_server_uid(g_multi_user_name, old_server_uid_char, new_server_uid_char, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
                }
-               
-               if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
+       
+               if (!emstorage_update_read_mail_uid_by_server_uid(g_multi_user_name, old_server_uid_char, new_server_uid_char, mailbox, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
                }
-               
-#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-               if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
+#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
+               if (false == emstorage_update_pbd_activity(g_multi_user_name, old_server_uid_char, new_server_uid_char, mailbox, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
                }
-               
+
+#endif
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+               if (mailbox_tbl) {
+                       if (!emstorage_update_auto_download_activity(g_multi_user_name, old_server_uid_char, new_server_uid_char, NULL, mailbox_tbl->mailbox_id, &err))
+                               EM_DEBUG_EXCEPTION("emstorage_update_auto_download_activity failed : [%d]", err);
+               }
 #endif
+
        }
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       if (mailbox_tbl)
+               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
+
+       g_copyuid_account_id = 0;
+    EM_SAFE_FREE(g_multi_user_name);
+#endif
+
        EM_SAFE_FREE(old_server_uid);
        EM_SAFE_FREE(new_server_uid);
-       
 }
 
-INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code)
+INTERNAL_FUNC int emcore_move_mail_on_server_ex(char *multi_user_name, int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        MAILSTREAM *stream = NULL;
@@ -440,10 +458,10 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_
 
        email_uid_range_set *uid_range_node = NULL;
 
-       char **string_list = NULL;      
+       char **string_list = NULL;
        int string_count = 0;
        emstorage_mailbox_tbl_t* dest_mailbox = NULL;
-       
+
        if (num <= 0  || account_id <= 0 || src_mailbox_id <= 0 || dest_mailbox_id <= 0 || NULL == mail_ids) {
                if (error_code != NULL) {
                        *error_code = EMAIL_ERROR_INVALID_PARAM;
@@ -452,11 +470,11 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_
                return false;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        
        if (NULL == ref_account) {
                EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
-               
+
                *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
@@ -466,27 +484,28 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_
                goto FINISH_OFF;
        }
 
-
-       if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, src_mailbox_id, (void **)&stream, &err_code)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
-               
+
                goto FINISH_OFF;
        }
 
        if (NULL != stream) {
+               g_copyuid_account_id = account_id;
+        g_multi_user_name = EM_SAFE_STRDUP(multi_user_name);
                mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
                EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
                /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
                /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
                /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, server_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by server_uid  */
                /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
-               
+
                if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code))   {
                        EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
                        goto FINISH_OFF;
                }
-               
-               if ( (err_code = emstorage_get_mailbox_by_id(dest_mailbox_id, &dest_mailbox)) != EMAIL_ERROR_NONE || !dest_mailbox) {
+
+               if ( (err_code = emstorage_get_mailbox_by_id(multi_user_name, dest_mailbox_id, &dest_mailbox)) != EMAIL_ERROR_NONE || !dest_mailbox) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err_code);
                        goto FINISH_OFF;
                }
@@ -495,23 +514,22 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_
 
                for (i = 0; i < string_count; ++i) {
                        /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
-               
-                       if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
+                       if (false == emstorage_get_id_set_from_mail_ids(multi_user_name, string_list[i], &id_set, &id_set_count, &err_code)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
                                goto FINISH_OFF;
                        }
-                       
+
                        /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
-                       
+
                        len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
-                       
+
                        if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
                                EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
                                goto FINISH_OFF;
                        }
-               
+
                        uid_range_node = uid_range_set;
-               
+
                        while (uid_range_node != NULL) {
                                /* Remove comma from end of uid_range */
                                uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
@@ -529,27 +547,27 @@ INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_
                                else {
                                        EM_DEBUG_LOG("Mail MOVE SUCCESS ");
                                }
-                               
+
                                uid_range_node = uid_range_node->next;
-                       }       
+                       }
 
                        emcore_free_uid_range_set(&uid_range_set);
 
                        EM_SAFE_FREE(id_set);
-                       
+
                        id_set_count = 0;
                }
-       
+
        }
        else {
                EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
                goto FINISH_OFF;
        }
-       
+
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        emcore_free_comma_separated_strings(&string_list, &string_count); /*prevent 17958*/
 
        stream = mail_close (stream);
@@ -562,14 +580,14 @@ FINISH_OFF:
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        if (ret || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
                emstorage_activity_tbl_t new_activity;
-               for (i = 0; i<num ; i++) {              
+               for (i = 0; i<num ; i++) {
                        memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
                        new_activity.activity_type = ACTIVITY_MOVEMAIL;
                        new_activity.account_id    = account_id;
                        new_activity.mail_id       = mail_ids[i];
                        new_activity.src_mbox      = src_mailbox;
                        new_activity.dest_mbox     = dest_mailbox;
-               
+
                        if (!emcore_delete_activity(&new_activity, &err_code)) {
                                EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
                        }
@@ -588,10 +606,10 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_server, int *err_code)
+int emcore_delete_mails_from_imap4_server(char *multi_user_name, int mail_ids[], int num, int from_server, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        IMAPLOCAL *imaplocal = NULL;
@@ -605,11 +623,10 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
        email_uid_range_set *uid_range_set = NULL;
        int len_of_each_range = 0;
        email_uid_range_set *uid_range_node = NULL;
-       char **string_list = NULL;      
+       char **string_list = NULL;
        int string_count = 0;
-       int delete_success = false;
        emstorage_mail_tbl_t *mail_tbl_data = NULL;
-               
+
        if (num <= 0 || !mail_ids) {
                EM_DEBUG_EXCEPTION(" Invalid parameter ");
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -617,7 +634,7 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
        }
 
        for(i = 0; i < num; i++) {
-               if (!emstorage_get_downloaded_mail(mail_ids[i], &mail_tbl_data, false, &err)) {
+               if (!emstorage_get_downloaded_mail(multi_user_name, mail_ids[i], &mail_tbl_data, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
 
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {        /* not server mail */
@@ -637,7 +654,7 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                goto FINISH_OFF;
        }
 
-       if (!emcore_connect_to_remote_mailbox(mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -653,19 +670,18 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
        }
 
        /*  Now execute one by one each comma separated string of mail_ids  */
-       
+
        for (i = 0; i < string_count; ++i) {
                /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order  */
-
-               if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
+               if (false == emstorage_get_id_set_from_mail_ids(multi_user_name, string_list[i], &id_set, &id_set_count, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
                        goto FINISH_OFF;
                }
-               
+
                /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H  */
-               
+
                len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*   1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag.  */
-               
+
                if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
                        goto FINISH_OFF;
@@ -680,43 +696,43 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
 
                        if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
                                EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
-               
+
                                err = EMAIL_ERROR_CONNECTION_BROKEN;
                                goto FINISH_OFF;
                        }
 
                        memset(tag, 0x00, sizeof(tag));
                        memset(cmd, 0x00, sizeof(cmd));
-                       
+
                        SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
                        SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
-                       
+
                        EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
 
-                       
+
                        /* send command  :  set deleted flag */
                        if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd)))  {
                                EM_DEBUG_EXCEPTION("net_sout failed...");
-                               
+
                                err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN */
                                goto FINISH_OFF;
                        }
 
-                       
+
                        while (imaplocal->netstream)  {
                                /* receive response */
                                if (!(p = net_getline(imaplocal->netstream))) {
                                        EM_DEBUG_EXCEPTION("net_getline failed...");
-                                       
+
                                        err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
                                        goto FINISH_OFF;
                                }
-                               
+
 
                                EM_DEBUG_LOG("[IMAP4] <<< %s", p);
 
                                /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
-                               /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl 
+                               /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl
                                if (strstr(p, "FETCH") != NULL)  {
                                        EM_DEBUG_LOG(" FETCH Response recieved ");
                                        delete_success = true;
@@ -724,41 +740,39 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                                        break;
                                }
                                */
-               
-                       
+
+
                                if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
                                        if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
                                                /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
                                                EM_DEBUG_LOG(" OK Response recieved ");
-                                               delete_success = true;
                                                EM_SAFE_FREE(p);
                                                break;
-                                       }                                       
+                                       }
                                        else  {
                                                /*  'NO' or 'BAD' */
-                                               delete_success = false;
                                                err = EMAIL_ERROR_IMAP4_STORE_FAILURE;          /* EMAIL_ERROR_INVALID_RESPONSE; */
                                                goto FINISH_OFF;
                                        }
                                }
-                       
+
                                EM_SAFE_FREE(p);
                                }
-                               
+
                                memset(tag, 0x00, sizeof(tag));
                                memset(cmd, 0x00, sizeof(cmd));
 
                                EM_DEBUG_LOG("Calling Expunge");
-                               
+
                                SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
                                SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
-                               
+
                                EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
-                               
+
                                /* send command  :   EXPUNGE */
                                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;
                                }
@@ -767,19 +781,19 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                                        /* receive response */
                                        if (!(p = net_getline(imaplocal->netstream))) {
                                                EM_DEBUG_EXCEPTION("net_getline failed...");
-                                               
+
                                                err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
                                                goto FINISH_OFF;
                                        }
-                                       
+
                                        EM_DEBUG_LOG("[IMAP4] <<< %s", p);
-                                       
+
                                        if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
                                                if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
 #ifdef __FEATURE_LOCAL_ACTIVITY__
                                                        int index = 0;
                                                        emstorage_mail_tbl_t **mail = NULL;
-                                                       
+
                                                        mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
                                                        if (!mail) {
                                                                EM_DEBUG_EXCEPTION("em_malloc failed");
@@ -789,23 +803,23 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
 
                                                        if (delete_success) {
                                                                for (index = 0 ; index < num; index++) {
-                                                                       if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err))  {
+                                                                       if (!emstorage_get_downloaded_mail(multi_user_name, mail_ids[index], &mail[index], false, &err))  {
                                                                                EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
-                                                                               
-                                                                               if (err == EMAIL_ERROR_MAIL_NOT_FOUND)  {               
+
+                                                                               if (err == EMAIL_ERROR_MAIL_NOT_FOUND)  {
                                                                                        EM_DEBUG_LOG("EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER  : ");
                                                                                        continue;
                                                                                }
-                                                                       }               
-                                                               
-                                                                       if (mail[index]) {                              
+                                                                       }
+
+                                                                       if (mail[index]) {
                                                                                /* Clear entry from mail_read_mail_uid_tbl */
                                                                                if (mail[index]->server_mail_id != NULL) {
-                                                                                       if (!emstorage_remove_downloaded_mail(mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err))  {
+                                                                                       if (!emstorage_remove_downloaded_mail(multi_user_name, mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err))  {
                                                                                                EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
                                                                                        }
                                                                                }
-                                                                               
+
                                                                                /* Delete local activity */
                                                                                emstorage_activity_tbl_t new_activity;
                                                                                memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
@@ -821,14 +835,14 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                                                                                new_activity.server_mailid      = NULL;
                                                                                new_activity.src_mbox           = NULL;
                                                                                new_activity.dest_mbox          = NULL;
-                                                                                                               
+
                                                                                if (!emcore_delete_activity(&new_activity, &err)) {
                                                                                        EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
                                                                                }
                                                                        }
                                                                        else {
                                                                                /* Fix for crash seen while deleting Outbox mails which are moved to Trash. Outbox mails do not have server mail id and are not updated in mail_read_mail_uid_tbl.
-                                                                                 * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted. 
+                                                                                 * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted.
                                                                                */
                                                                                /* Delete local activity */
                                                                                emstorage_activity_tbl_t new_activity;
@@ -838,7 +852,7 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                                                                                new_activity.server_mailid      = NULL;
                                                                                new_activity.src_mbox           = NULL;
                                                                                new_activity.dest_mbox          = NULL;
-                                                                                                               
+
                                                                                if (!emcore_delete_activity(&new_activity, &err)) {
                                                                                        EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
                                                                                }
@@ -863,35 +877,35 @@ int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_serv
                                                        goto FINISH_OFF;
                                                }
                                        }
-                                       
+
                                        EM_SAFE_FREE(p);
                                }
                                uid_range_node = uid_range_node->next;
-               }       
+               }
 
                emcore_free_uid_range_set(&uid_range_set);
 
                EM_SAFE_FREE(id_set);
-               
+
                id_set_count = 0;
        }
-       
+
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        EM_SAFE_FREE(p);
        EM_SAFE_FREE(id_set); /*prevent 17954*/
-       
+
        if (stream)  {
                stream = mail_close (stream);
        }
-       
+
        emcore_free_comma_separated_strings(&string_list, &string_count);
 
        if (false == ret) {
                emcore_free_uid_range_set(&uid_range_set);
        }
-       
+
        if (err_code) {
                *err_code = err;
        }
@@ -902,46 +916,48 @@ FINISH_OFF:
 
 #endif
 
-typedef enum {
-       IMAP4_CMD_EXPUNGE
-} imap4_cmd_t;
-
-static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
+INTERNAL_FUNC int emcore_imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        IMAPLOCAL *imaplocal = NULL;
        char tag[16], cmd[64];
        char *p = NULL;
-       
+
        if (!(imaplocal = stream->local) || !imaplocal->netstream) {
                EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
-               
+
                err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN */
                goto FINISH_OFF;
        }
-       
+
        memset(tag, 0x00, sizeof(tag));
        memset(cmd, 0x00, sizeof(cmd));
-       
+
        SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
-       SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
+
+       if (cmd_type == IMAP4_CMD_EXPUNGE) {
+               SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
+       }
+       else if (cmd_type == IMAP4_CMD_NOOP) {
+               SNPRINTF(cmd, sizeof(cmd), "%s NOOP\015\012", tag);
+       }
 
 #ifdef FEATURE_CORE_DEBUG
        EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
 #endif
-       
+
        /*  send command  :  delete flaged mail */
        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;
        }
-       
+
        while (imaplocal->netstream) {
                /*  receive response */
                if (!(p = net_getline(imaplocal->netstream))) {
@@ -949,60 +965,137 @@ static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err
                        err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
                        goto FINISH_OFF;
                }
-               
+
 #ifdef FEATURE_CORE_DEBUG
                EM_DEBUG_LOG("[IMAP4] <<< %s", p);
 #endif
-               
+
                if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
                        if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
                                EM_SAFE_FREE(p);
                                break;
                        }
                        else {          /*  'NO' or 'BAD' */
-                               err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE;                /* EMAIL_ERROR_INVALID_RESPONSE; */
+                               if (cmd_type == IMAP4_CMD_EXPUNGE) {
+                                       err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE;                /* EMAIL_ERROR_INVALID_RESPONSE; */
+                               }
+                               else if (cmd_type == IMAP4_CMD_NOOP) {
+                                       err = EMAIL_ERROR_IMAP4_NOOP_FAILURE;
+                               }
                                goto FINISH_OFF;
                        }
                }
-               
+
                EM_SAFE_FREE(p);
        }
-       
+
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        EM_SAFE_FREE(p);
-       
+
        if (err_code)
                *err_code = err;
 
        return ret;
 }
 
-int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
+INTERNAL_FUNC int emcore_pop3_send_command(MAILSTREAM *stream, pop3_cmd_t cmd_type, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
-       
+       EM_DEBUG_FUNC_BEGIN("stream[%p], err_code[%p]", stream, err_code);
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       ADDRESS *addr = NULL;
-       char *address = NULL;
-       char *temp_emailaddr = NULL;
-       char *alias = NULL;
-       int is_searched = false;
-       int address_length = 0;
-       char *email_address = NULL;
-       int contact_name_len = 0;
-       char temp_string[1024] = { 0 , };
-       int is_saved = 0;
-       char *contact_display_name = NULL;
-       char *contact_display_name_from_contact_info = NULL;
-       int contact_display_name_len = 0;
-       int i = 0;
-       int contact_name_buffer_size = 0;
-       char *contact_name = NULL;
 
-       if (!contact_info)  {
+       POP3LOCAL *pop3local = NULL;
+       char cmd[64];
+       char *p = NULL;
+
+       if (!stream) {
+               EM_DEBUG_EXCEPTION("stream[%p]", stream);
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       if (!(pop3local = stream->local) || !pop3local->netstream) {
+               EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
+               err = EMAIL_ERROR_UNKNOWN;
+               goto FINISH_OFF;
+       }
+
+       memset(cmd, 0x00, sizeof(cmd));
+
+       if (cmd_type == POP3_CMD_NOOP)
+               SNPRINTF(cmd, sizeof(cmd), "NOOP\015\012");
+
+#ifdef FEATURE_CORE_DEBUG
+       EM_DEBUG_LOG("[POP3] >>> %s", cmd);
+#endif
+
+       /*  send command  :  delete specified mail */
+       if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
+               EM_DEBUG_EXCEPTION("net_sout failed...");
+               err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN; */
+               goto FINISH_OFF;
+       }
+
+       /*  receive response */
+       if (!(p = net_getline(pop3local->netstream))) {
+               EM_DEBUG_EXCEPTION("net_getline failed...");
+               err = EMAIL_ERROR_INVALID_RESPONSE;
+               goto FINISH_OFF;
+       }
+
+#ifdef FEATURE_CORE_DEBUG
+       EM_DEBUG_LOG("[POP3] <<< %s", p);
+#endif
+
+       if (*p == '-') {                /*  '-ERR' */
+               if (cmd_type == POP3_CMD_NOOP)
+                       err = EMAIL_ERROR_POP3_NOOP_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (*p != '+') {                /*  '+OK' ... */
+               err = EMAIL_ERROR_INVALID_RESPONSE;
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       EM_SAFE_FREE(p);
+
+       if (err_code)
+               *err_code = err;
+
+       return ret;
+}
+
+int emcore_get_mail_contact_info_with_update(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
+
+       int ret = false;
+       int err = EMAIL_ERROR_NONE;
+       ADDRESS *addr = NULL;
+       char *address = NULL;
+       char *temp_emailaddr = NULL;
+       char *alias = NULL;
+       int is_searched = false;
+       int address_length = 0;
+       char *email_address = NULL;
+       int contact_name_len = 0;
+       char temp_string[1024] = { 0 , };
+       int is_saved = 0;
+       char *contact_display_name = NULL;
+       char *contact_display_name_from_contact_info = NULL;
+       int contact_display_name_len = 0;
+       int i = 0;
+       int contact_name_buffer_size = 0;
+       char *contact_name = NULL;
+
+       if (!contact_info)  {
                EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
@@ -1015,7 +1108,7 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
        }
        else {
                address_length = 2 * EM_SAFE_STRLEN(full_address);
-               temp_emailaddr = (char  *)calloc(1, address_length); 
+               temp_emailaddr = (char  *)calloc(1, address_length);
        }
 
        em_skip_whitespace(full_address , &address);
@@ -1025,13 +1118,13 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
        /*  ',' -> "%2C" */
        gchar **tokens = g_strsplit(address, ", ", -1);
        char *p = g_strjoinv("%2C", tokens);
-       
+
        g_strfreev(tokens);
-       
+
        /*  ';' -> ',' */
-       while (p && p[i] != '\0') 
+       while (p && p[i] != '\0')
        {
-               if (p[i] == ';') 
+               if (p[i] == ';')
                        p[i] = ',';
                i++;
        }
@@ -1056,9 +1149,9 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
-       
+
        while (addr != NULL)  {
-               if (addr->mailbox && addr->host) {      
+               if (addr->mailbox && addr->host) {
                        if (!strncmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS")) || !strncmp(addr->mailbox , "INVALID_ADDRESS", strlen("INVALID_ADDRESS")) || !strncmp(addr->host , ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR.")))
                {
                                EM_DEBUG_LOG("Invalid address ");
@@ -1074,14 +1167,16 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
 
                EM_SAFE_FREE(email_address);
                email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
-               
+
                EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
                EM_DEBUG_LOG_SEC(" email_address[%s]", email_address);
-       
+
                is_searched = false;
                EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
        
-               if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &err) && err == EMAIL_ERROR_NONE) {
+               err = emcore_get_mail_display_name (multi_user_name, email_address,  
+                                            &contact_display_name_from_contact_info);
+               if ( err == EMAIL_ERROR_NONE) {
                        contact_display_name = contact_display_name_from_contact_info;
 
                        EM_DEBUG_LOG_SEC(">>> contact_name[%s]", contact_display_name);
@@ -1093,7 +1188,7 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                                if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
                                        char *temp = contact_name;
                                        contact_name_buffer_size += contact_name_buffer_size;
-                                       contact_name = (char  *)calloc(1, contact_name_buffer_size); 
+                                       contact_name = (char  *)calloc(1, contact_name_buffer_size);
                                        if (contact_name == NULL) {
                                                EM_DEBUG_EXCEPTION("Memory allocation failed.");
                                                EM_SAFE_FREE(temp);
@@ -1109,13 +1204,13 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                                }
                                else {
                                        snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
-                               }                                       
+                               }
 
                                contact_display_name_len = EM_SAFE_STRLEN(temp_string);
                                if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
                                        char *temp = contact_name;
                                        contact_name_buffer_size += contact_name_buffer_size;
-                                       contact_name = (char  *)calloc(1, contact_name_buffer_size); 
+                                       contact_name = (char  *)calloc(1, contact_name_buffer_size);
                                        if (contact_name == NULL) {
                                                EM_DEBUG_EXCEPTION("Memory allocation failed.");
                                                EM_SAFE_FREE(temp);
@@ -1138,11 +1233,11 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                if (addr->personal != NULL) {
                        /*  "%2C" -> ',' */
                        tokens = g_strsplit(addr->personal, "%2C", -1);
-                       
+
                        EM_SAFE_FREE(addr->personal);
-                       
+
                        addr->personal = g_strjoinv(", ", tokens);
-                       
+
                        g_strfreev(tokens);
                        /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
                        alias = addr->personal;
@@ -1171,13 +1266,13 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                                else {
                                        snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
                                }
-                               EM_DEBUG_LOG("temp_string[%s]", temp_string);
+                               EM_DEBUG_LOG_SEC("temp_string[%s]", temp_string);
 
                                contact_display_name_len = EM_SAFE_STRLEN(temp_string);
                                if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
                                        char *temp = contact_name;
                                        contact_name_buffer_size += contact_name_buffer_size;
-                                       contact_name = (char  *)calloc(1, contact_name_buffer_size); 
+                                       contact_name = (char  *)calloc(1, contact_name_buffer_size);
                                        if (contact_name == NULL) {
                                                EM_DEBUG_EXCEPTION("Memory allocation failed.");
                                                EM_SAFE_FREE(temp);
@@ -1187,14 +1282,14 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
                                        snprintf(contact_name, contact_name_buffer_size, "%s", temp);
                                        EM_SAFE_FREE(temp);
                                }
-                               
+
                                snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
                                contact_name_len += contact_display_name_len;
                                EM_DEBUG_LOG_SEC("new contact_name >>>>> %s ", contact_name);
                        }
                }
 
-               if (temp_emailaddr && email_address) {  
+               if (temp_emailaddr && email_address) {
                        if (mail_id == 0) {     /*  if mail_id is 0, return only contact info without saving contact info to DB */
                                /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
                                EM_SAFE_STRCAT(temp_emailaddr, email_address);
@@ -1222,7 +1317,7 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
        } /*  while (addr != NULL) */
 
        if (temp_emailaddr) {
-               EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
+               EM_DEBUG_LOG_SEC(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
                contact_info->email_address = temp_emailaddr;
                temp_emailaddr = NULL;
        }
@@ -1231,51 +1326,51 @@ int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_
        contact_info->contact_name = g_strdup(contact_name); /*prevent 40020*/
 
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
 
        EM_SAFE_FREE(email_address);
        EM_SAFE_FREE(address);
        EM_SAFE_FREE(temp_emailaddr);
-       EM_SAFE_FREE(contact_name);             
+       EM_SAFE_FREE(contact_name);
        EM_SAFE_FREE(contact_display_name_from_contact_info);
-       
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 }
 
 int emcore_free_contact_info(email_mail_contact_info_t *contact_info, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        if (!contact_info)  {
                EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        EM_SAFE_FREE(contact_info->contact_name);
        EM_SAFE_FREE(contact_info->email_address);
        EM_SAFE_FREE(contact_info->alias);
-       
+
        contact_info->storage_type = -1;
        contact_info->contact_id = -1;
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (err_code != NULL)
                *err_code = err;
        EM_DEBUG_FUNC_END();
        return ret;
 }
 
-int emcore_sync_contact_info(int mail_id, int *err_code)
+int emcore_sync_contact_info(char *multi_user_name, int mail_id, int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreMailContactSync);
        EM_DEBUG_FUNC_BEGIN();
@@ -1289,43 +1384,42 @@ int emcore_sync_contact_info(int mail_id, int *err_code)
        email_mail_contact_info_t contact_info_to;
        email_mail_contact_info_t contact_info_cc;
        email_mail_contact_info_t contact_info_bcc;
-       
+
        EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
-       
-       memset(&contact_info_from, 0x00, sizeof(email_mail_contact_info_t));    
+
+       memset(&contact_info_from, 0x00, sizeof(email_mail_contact_info_t));
        memset(&contact_info_to, 0x00, sizeof(email_mail_contact_info_t));
        memset(&contact_info_cc, 0x00, sizeof(email_mail_contact_info_t));
-       memset(&contact_info_bcc, 0x00, sizeof(email_mail_contact_info_t));     
+       memset(&contact_info_bcc, 0x00, sizeof(email_mail_contact_info_t));
 
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
        if (mail->full_address_from != NULL) {
-               if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err))  {
+               if (!emcore_get_mail_contact_info_with_update(multi_user_name, &contact_info_from, mail->full_address_from, mail_id, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
                }
        }
 
        if (mail->full_address_to != NULL)  {
-               if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err))  {
+               if (!emcore_get_mail_contact_info_with_update(multi_user_name, &contact_info_to, mail->full_address_to, mail_id, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
                }
        }
 
        if (mail->full_address_cc != NULL)  {
-               if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err))  {
+               if (!emcore_get_mail_contact_info_with_update(multi_user_name, &contact_info_cc, mail->full_address_cc, mail_id, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
                }
        }
 
        if (mail->full_address_bcc != NULL)  {
-               if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err))  {
+               if (!emcore_get_mail_contact_info_with_update(multi_user_name, &contact_info_bcc, mail->full_address_bcc, mail_id, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
                }
-       }       
+       }
 
        EM_SAFE_FREE(mail->email_address_sender);
        mail->email_address_sender = contact_info_from.email_address;
@@ -1347,24 +1441,23 @@ int emcore_sync_contact_info(int mail_id, int *err_code)
                contact_info_bcc.contact_name  = NULL;
                contact_info_bcc.email_address = NULL;
        }
-       
+
        /*  Update DB */
-       if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {                                       
+       if (!emstorage_change_mail_field(multi_user_name, mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {                                      
                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
-
                goto FINISH_OFF;
-       }       
+       }
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (mail != NULL)
                emstorage_free_mail(&mail, 1, NULL);
 
        emcore_free_contact_info(&contact_info_from, NULL);
        emcore_free_contact_info(&contact_info_to, NULL);
        emcore_free_contact_info(&contact_info_cc, NULL);
-       emcore_free_contact_info(&contact_info_bcc, NULL);      
+       emcore_free_contact_info(&contact_info_bcc, NULL);
 
        if (err_code != NULL)
                *err_code = err;
@@ -1376,7 +1469,7 @@ FINISH_OFF:
 /*  1. parsing  :  alias and address */
 /*  2. sync with contact */
 /*  3. make glist of address info */
-static int emcore_sync_address_info(email_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
+static int emcore_sync_address_info(char *multi_user_name, email_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
 
@@ -1390,7 +1483,7 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
        char email_address[MAX_EMAIL_ADDRESS_LENGTH];
        email_address_info_t *p_address_info = NULL;
        ADDRESS *addr = NULL;
-       
+
        if (address_info_list == NULL) {
                EM_DEBUG_EXCEPTION("Invalid param : address_info_list is NULL");
                error = EMAIL_ERROR_INVALID_PARAM;
@@ -1404,29 +1497,29 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
        gchar **tokens = g_strsplit(address, ", ", -1);
        char *p = g_strjoinv("%2C", tokens);
        int i = 0;
-       
+
        g_strfreev(tokens);
-       
+
        /*  ';' -> ',' */
        while (p && p[i] != '\0')  {
-               if (p[i] == ';') 
+               if (p[i] == ';')
                        p[i] = ',';
                i++;
        }
 
        rfc822_parse_adrlist(&addr, p, NULL);
 
-       EM_SAFE_FREE(p);        
+       EM_SAFE_FREE(p);
 
        if (!addr)  {
                EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
-               error = EMAIL_ERROR_INVALID_PARAM;              
+               error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
        /*  Get a contact name */
        while (addr != NULL)  {
-               if (addr->mailbox && addr->host) {      
+               if (addr->mailbox && addr->host) {
                        if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
                                EM_DEBUG_LOG("Invalid address ");
                                addr = addr->next;
@@ -1448,26 +1541,26 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
                        EM_DEBUG_EXCEPTION("malloc failed...");
                        error = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
-               }       
-               memset(p_address_info, 0x00, sizeof(email_address_info_t)); 
+               }
+               memset(p_address_info, 0x00, sizeof(email_address_info_t));
 
                SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
+
                EM_DEBUG_LOG_SEC("Search a contact  :  address[%s]", email_address);
 
                is_search = false;
-
-               if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &error) && error == EMAIL_ERROR_NONE) {
+               error = emcore_get_mail_display_name (multi_user_name, email_address, 
+                                            &contact_display_name_from_contact_info);
+               if (error == EMAIL_ERROR_NONE) {
                        EM_DEBUG_LOG_SEC(">>> contact display name[%s]", contact_display_name_from_contact_info);
-
                        is_search = true;
                }
                else if (error != EMAIL_ERROR_DATA_NOT_FOUND)
                        EM_DEBUG_EXCEPTION("emcore_get_mail_display_name - Not found contact record(if err is -203) or error [%d]", error);
 
                if (is_search == true) {
-                       p_address_info->contact_id = contact_index;             
-                       p_address_info->storage_type = -1;                              
+                       p_address_info->contact_id = contact_index;
+                       p_address_info->storage_type = -1;
                        p_address_info->display_name = contact_display_name_from_contact_info;
                        EM_DEBUG_LOG_SEC("display_name from contact[%s]", p_address_info->display_name);
                }
@@ -1476,19 +1569,19 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
                        if (addr->personal != NULL) {
                                /*  "%2C" -> ',' */
                                tokens = g_strsplit(addr->personal, "%2C", -1);
-                               
+
                                EM_SAFE_FREE(addr->personal);
-                               
+
                                addr->personal = g_strjoinv(", ", tokens);
-                               
+
                                g_strfreev(tokens);
                                alias = addr->personal;
                        }
                        else {
                                alias = NULL;
-                       }               
+                       }
                        p_address_info->contact_id = -1;
-                       p_address_info->storage_type = -1;      
+                       p_address_info->storage_type = -1;
                        /*  Use an alias or an email address as a display name */
                        if (alias == NULL)
                                p_address_info->display_name = EM_SAFE_STRDUP(email_address);
@@ -1497,7 +1590,7 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
 
                        EM_DEBUG_LOG_SEC("display_name from email [%s]", p_address_info->display_name);
                }
-               
+
                p_address_info->address = EM_SAFE_STRDUP(email_address);
                p_address_info->address_type = address_type;
 
@@ -1517,17 +1610,17 @@ static int emcore_sync_address_info(email_address_type_t address_type, char *ful
        }
 
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
 
        EM_SAFE_FREE(address);
 
        if (addr) {
                mail_free_address(&addr);
        }
-       
+
        if (err_code != NULL)
-               *err_code = error;      
+               *err_code = error;
        EM_DEBUG_FUNC_END();
        return ret;
 }
@@ -1536,13 +1629,13 @@ static gint address_compare(gconstpointer a, gconstpointer b)
 {
        EM_DEBUG_FUNC_BEGIN();
        email_sender_list_t *recipients_list1 = (email_sender_list_t *)a;
-       email_sender_list_t *recipients_list2 = (email_sender_list_t *)b;       
+       email_sender_list_t *recipients_list2 = (email_sender_list_t *)b;
 
-       EM_DEBUG_FUNC_END();    
+       EM_DEBUG_FUNC_END();
        return strcmp(recipients_list1->address, recipients_list2->address);
 }
 
-INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
+INTERNAL_FUNC GList *emcore_get_recipients_list(char *multi_user_name, GList *old_recipients_list, char *full_address, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1558,7 +1651,7 @@ INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char
 
        email_sender_list_t *temp_recipients_list = NULL;
        email_sender_list_t *old_recipients_list_t = NULL;
-       
+
        if (full_address == NULL || EM_SAFE_STRLEN(full_address) == 0) {
                EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -1573,7 +1666,7 @@ INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char
        g_strfreev(tokens);
 
        while (p && p[i] != '\0') {
-               if (p[i] == ';') 
+               if (p[i] == ';')
                        p[i] = ',';
                i++;
        }
@@ -1594,25 +1687,26 @@ INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char
                                EM_DEBUG_LOG("Invalid address ");
                                addr = addr->next;
                                continue;
-                       } 
+                       }
                } else {
                        EM_DEBUG_LOG("Error in parsing..! ");
                        addr = addr->next;
                        continue;
-               }                       
-       
+               }
+
                if ((temp_recipients_list = g_new0(email_sender_list_t, 1)) == NULL) {
                        EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
                        err = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
 
-               
+
                SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
 
                EM_DEBUG_LOG_SEC("Search a contact : address[%s]", email_address);
 
-               if (emcore_get_mail_display_name(email_address, &display_name, &err) && err == EMAIL_ERROR_NONE) {
+               err = emcore_get_mail_display_name(multi_user_name, email_address, &display_name);
+               if ( err == EMAIL_ERROR_NONE) {
                        EM_DEBUG_LOG_SEC(">>> contact display name[%s]", display_name);
                        is_search = true;
                } else {
@@ -1675,16 +1769,15 @@ FINISH_OFF:
                *err_code = err;
 
        EM_DEBUG_FUNC_END();
-       return new_recipients_list;                     
+       return new_recipients_list;
 }
 
-INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_info_list_t **address_info_list, int *err_code)
+INTERNAL_FUNC int emcore_get_mail_address_info_list(char *multi_user_name, int mail_id, email_address_info_list_t **address_info_list, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
 
        int ret = false, err = EMAIL_ERROR_NONE;
        int failed = true;
-       int contact_error;
 
        emstorage_mail_tbl_t *mail = NULL;
        email_address_info_list_t *p_address_info_list = NULL;
@@ -1694,12 +1787,10 @@ INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_i
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        /* get mail from mail table */
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
-               
-
                goto FINISH_OFF;
        }
 
@@ -1707,32 +1798,32 @@ INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_i
                EM_DEBUG_EXCEPTION("malloc failed...");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
-       }       
-       memset(p_address_info_list, 0x00, sizeof(email_address_info_list_t));   
-#if 0
-       if ((contact_error = contacts_connect2 ()) != CONTACTS_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("contacts_connect2 failed [%d]", contact_error);             
-               err = EMAIL_ERROR_DB_FAILURE;
+       }
+       memset(p_address_info_list, 0x00, sizeof(email_address_info_list_t));
+
+       if ((err = emcore_connect_contacts_service(multi_user_name)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_connect_contacts_service error : [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (mail->full_address_from && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
+       if (mail->full_address_from && emcore_sync_address_info(multi_user_name, EMAIL_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
                failed = false;
-       if (mail->full_address_to && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
+       if (mail->full_address_to && emcore_sync_address_info(multi_user_name, EMAIL_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
                failed = false;
-       if (mail->full_address_cc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
+       if (mail->full_address_cc && emcore_sync_address_info(multi_user_name, EMAIL_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
                failed = false;
-       if (mail->full_address_bcc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
+       if (mail->full_address_bcc && emcore_sync_address_info(multi_user_name, EMAIL_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
                failed = false;
 
-       if ((contact_error = contacts_disconnect2 ()) != CONTACTS_ERROR_NONE)
-               EM_DEBUG_EXCEPTION("contacts_connect2 failed [%d]", contact_error);
-#endif
+       if ((err = emcore_disconnect_contacts_service(multi_user_name)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_disconnect_contacts_service failed : [%d]", err);
+       }
+
        if (failed == false)
                ret = true;
 
-FINISH_OFF: 
-       if (ret == true) 
+FINISH_OFF:
+       if (ret == true)
                *address_info_list = p_address_info_list;
        else if (p_address_info_list != NULL)
                emstorage_free_address_info_list(&p_address_info_list);
@@ -1748,22 +1839,22 @@ FINISH_OFF:
 
 /* description
  *    get a mail data
- * arguments  
+ * arguments
  *    input_mail_id     : [in]  mail id
  *    output_mail_data  : [out] double pointer to hold mail data.
- * return  
+ * return
  *    succeed  :  EMAIL_ERROR_NONE
  *    fail     :  error code
  */
-INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
+INTERNAL_FUNC int emcore_get_mail_data(char *multi_user_name, int input_mail_id, email_mail_data_t **output_mail_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
-       
+
        int             error = EMAIL_ERROR_NONE;
        int             result_mail_count = 0;
        char            conditional_clause_string[QUERY_SIZE] = { 0, };
        emstorage_mail_tbl_t *result_mail_tbl = NULL;
-       
+
        if (input_mail_id == 0 || !output_mail_data)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                error = EMAIL_ERROR_INVALID_PARAM;
@@ -1771,8 +1862,8 @@ INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **ou
        }
 
        SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
-       
-       if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
+
+       if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
                goto FINISH_OFF;
        }
@@ -1781,8 +1872,8 @@ INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **ou
                EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
                goto FINISH_OFF;
        }
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (result_mail_tbl)
                emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
 
@@ -1795,31 +1886,6 @@ int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = 0;
-#ifdef __FEATURE_DRM__
-       drm_bool_type_e drm_file = DRM_UNKNOWN;
-       drm_file_info_s drm_file_info; 
-
-       if (input_attachment_tb_data == NULL)
-               return ret;
-
-       ret = drm_is_drm_file(input_attachment_tb_data->attachment_path, &drm_file);
-
-       if (ret == DRM_RETURN_SUCCESS && drm_file == DRM_TRUE) {
-               if (drm_get_file_info (input_attachment_tb_data->attachment_path, &drm_file_info) == DRM_RETURN_SUCCESS) {
-                       input_attachment_tb_data->attachment_drm_type = 0;
-                       EM_DEBUG_LOG ("fileInfo is [%d]", drm_file_info.oma_info.method);
-                       if (drm_file_info.oma_info.method != DRM_METHOD_TYPE_UNDEFINED) {
-                               input_attachment_tb_data->attachment_drm_type = drm_file_info.oma_info.method;
-                               ret = 1;
-                       }
-               }
-       }
-       else {
-               EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
-               input_attachment_tb_data->attachment_drm_type = 0;
-               ret = 0;
-       }
-#endif
        EM_DEBUG_FUNC_END();
        return ret;
 }
@@ -1827,16 +1893,16 @@ int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
 
 /* description
  *    get mail attachment from local mailbox
- * arguments  
+ * arguments
  *    mailbox  :  server mailbox
  *    mail_id  :  mai id to own attachment
  *    attachment  :  the number string to be downloaded
  *    callback  :  function callback. if NULL, ignored.
- * return  
+ * return
  *     succeed  :  1
  *     fail  :  0
  */
-INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment_data_t **attachment, int *err_code)
+INTERNAL_FUNC int emcore_get_attachment_info(char *multi_user_name, int attachment_id, email_attachment_data_t **attachment, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
 
@@ -1846,27 +1912,27 @@ INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        emstorage_attachment_tbl_t *attachment_tbl = NULL;
-       
+
        /* get attachment from attachment tbl */
-       if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &err) || !attachment_tbl)  {
+       if (!emstorage_get_attachment(multi_user_name, attachment_id, &attachment_tbl, true, &err) || !attachment_tbl)  {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
-
                goto FINISH_OFF;
        }
-       
+
        *attachment = em_malloc(sizeof(email_attachment_data_t));
        if (!*attachment)  {
                EM_DEBUG_EXCEPTION("malloc failed...");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
-       
+
        (*attachment)->attachment_id         = attachment_id;
        (*attachment)->attachment_name       = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
+       (*attachment)->content_id            = attachment_tbl->content_id; attachment_tbl->content_id = NULL;
        (*attachment)->attachment_size       = attachment_tbl->attachment_size;
        (*attachment)->save_status           = attachment_tbl->attachment_save_status;
        (*attachment)->attachment_path       = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
@@ -1876,30 +1942,30 @@ INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (attachment_tbl)
                emstorage_free_attachment(&attachment_tbl, 1, NULL);
 
        if (err_code)
                *err_code = err;
-       
+
        return ret;
 }
 
 /* description
  *    get mail attachment
- * arguments  
+ * arguments
  *    input_mail_id           :  mail id to own attachment
  *    output_attachment_data  :  result attahchment data
  *    output_attachment_count :  result attahchment count
- * return  
+ * return
  *     succeed : EMAIL_ERROR_NONE
  *     fail    : error code
  */
-INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
+INTERNAL_FUNC int emcore_get_attachment_data_list(char *multi_user_name, int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_attachment_data[%p], output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
-       
+
        if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                return EMAIL_ERROR_INVALID_PARAM;
@@ -1910,13 +1976,12 @@ INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attac
        int                        attachment_tbl_count = 0;
        emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
        email_attachment_data_t     *temp_attachment_data = NULL;
-       
+
        /* get attachment from attachment tbl */
-       if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE ){
+       if ( (err = emstorage_get_attachment_list(multi_user_name, input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE ){
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
-
                goto FINISH_OFF;
-       } 
+       }
 
        if (attachment_tbl_count)  {
                EM_DEBUG_LOG("attachment count %d", attachment_tbl_count);
@@ -1935,6 +2000,7 @@ INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attac
                        temp_attachment_data->attachment_id         = attachment_tbl_list[i].attachment_id;
                        temp_attachment_data->attachment_name       = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
                        temp_attachment_data->attachment_path       = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
+                       temp_attachment_data->content_id            = attachment_tbl_list[i].content_id; attachment_tbl_list[i].content_id = NULL;
                        temp_attachment_data->attachment_size       = attachment_tbl_list[i].attachment_size;
                        temp_attachment_data->mail_id               = attachment_tbl_list[i].mail_id;
                        temp_attachment_data->account_id            = attachment_tbl_list[i].account_id;
@@ -1945,25 +2011,26 @@ INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attac
                        temp_attachment_data->attachment_mime_type  = attachment_tbl_list[i].attachment_mime_type; attachment_tbl_list[i].attachment_mime_type = NULL;
                }
        }
-       
-FINISH_OFF: 
-       
+
+FINISH_OFF:
+
        *output_attachment_count = attachment_tbl_count;
 
        if (attachment_tbl_list)
                emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
-       
+
        return err;
 }
 
 
-INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nth, int *err_code)
+INTERNAL_FUNC int emcore_download_attachment (char *multi_user_name, int account_id, int mail_id, int nth, 
+                                               int cancellable, int event_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
 
        int err = EMAIL_ERROR_NONE;
 
-       if (mail_id < 1)  {
+       if (mail_id < 1) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
 
@@ -1975,7 +2042,6 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
        }
 
        int ret = false;
-       int status = EMAIL_DOWNLOAD_FAIL;
        MAILSTREAM *stream = NULL;
        BODY *mbody = NULL;
        emstorage_mail_tbl_t *mail = NULL;
@@ -1983,7 +2049,7 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
        struct attachment_info *ai = NULL;
        struct _m_content_info *cnt_info = NULL;
        void *tmp_stream = NULL;
-       char *server_uid = NULL, buf[1024];
+       char *server_uid = NULL, move_buf[512], path_buf[512];
        int msg_no = 0;
        emstorage_attachment_tbl_t *attachment_list = NULL;
        int current_attachment_no = 0;
@@ -1992,15 +2058,13 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
        int server_mbox_id = 0;
        int decoded_attachment_size = 0;
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        only_body_download = false;
-       
+
        /*  get mail from mail table. */
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -2010,29 +2074,26 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
                err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
                goto FINISH_OFF;
        }
-       
+
        if (nth == 0) { /*  download all attachments, nth starts from 1, not zero */
                /*  get attachment list from db */
                attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
-               if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE ){
+               if ( (err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE ){
                        EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
        else {  /*  download only nth attachment */
                attachment_count_to_be_downloaded = 1;
-               if (!emstorage_get_attachment_nth(mail_id, nth, &attachment_list, true, &err) || !attachment_list)  {
+               if (!emstorage_get_attachment_nth(multi_user_name, mail_id, nth, &attachment_list, true, &err) || !attachment_list)  {
                        EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
-
                        goto FINISH_OFF;
                }
        }
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        account_id      = mail->account_id;
        server_uid      = EM_SAFE_STRDUP(mail->server_mail_id);
        server_mbox_id  = mail->mailbox_id;
@@ -2043,16 +2104,15 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
        }
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                if(err == EMAIL_ERROR_NO_SUCH_HOST)
                        err = EMAIL_ERROR_CONNECTION_FAILURE;
-               status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
                goto FINISH_OFF;
        }
-       
+
        stream = (MAILSTREAM *)tmp_stream;
-       
+
        for (i = 0; i < attachment_count_to_be_downloaded; i++) {
                EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
 
@@ -2062,23 +2122,21 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
                else                                                                            /*  download only nth attachment */
                        current_attachment_no = nth;    /*  attachment no */
 
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
-               
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
                /*free cnt_info before reusing*/
                if (cnt_info)  {
                        emcore_free_content_info(cnt_info);
                        EM_SAFE_FREE(cnt_info);
                }
-               
+
                if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
                        EM_DEBUG_EXCEPTION("malloc failed...");
                        err = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
-               
+
                cnt_info->grab_type = GRAB_TYPE_ATTACHMENT;     /*  attachment */
                cnt_info->file_no   = current_attachment_no;    /*  attachment no */
 
@@ -2090,11 +2148,9 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
 
                /*  set sparep(member of BODY) memory free function. */
                mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sparep);
-               
-               if (!emcore_check_thread_status()) {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                msg_no = server_uid? atoi(server_uid): 0;
 
@@ -2105,17 +2161,15 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
                        goto FINISH_OFF;
                }
 
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                /*  set body fetch section. */
                if (emcore_set_fetch_body_section(mbody, false, NULL, NULL, &err) < 0) {
                        EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
                        goto FINISH_OFF;
                }
-       
+
                /*  download attachment. */
                _imap4_received_body_size = 0;
                _imap4_last_notified_body_size = 0;
@@ -2123,38 +2177,42 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
                _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
 
                EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
-               if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0)  {
+               if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, NULL, cnt_info, &err) < 0)  {
                        EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                /*  select target attachment information. */
                for (ai = cnt_info->file ; ai; ai = ai->next) {
-                       EM_DEBUG_LOG_SEC("[in loop] name[%s] save[%s] no[%d]", ai->save, ai->name, cnt_info->file_no);
+                       EM_DEBUG_LOG_SEC("[in loop] save[%s] name[%s] no[%d]", ai->save, ai->name, cnt_info->file_no);
+
+                       if (ai->type == INLINE_ATTACHMENT)
+                               continue;
+
                        if (--cnt_info->file_no == 0)
                                break;
                }
-
                EM_DEBUG_LOG("selected cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
-               
+
                if (cnt_info->file_no == 0 && ai) {
                        /*  rename temporary file to real file. */
-                       if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err))  {
+            memset(move_buf, 0x00, sizeof(move_buf));
+            memset(path_buf, 0x00, sizeof(path_buf));
+
+                       if (!emstorage_create_dir(multi_user_name, account_id, mail_id, current_attachment_no, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err))  {
+                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, current_attachment_no, ai->name, move_buf, path_buf, sizeof(path_buf), &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (!emstorage_move_file(ai->save, buf, false, &err))  {
+                       if (!emstorage_move_file(ai->save, move_buf, false, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                                err = EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME;
                                goto FINISH_OFF;
@@ -2162,42 +2220,43 @@ INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nt
 
                        EM_SAFE_FREE(ai->save);
 
-                       emcore_get_file_size(buf, &decoded_attachment_size, NULL);
+                       emcore_get_file_size(move_buf, &decoded_attachment_size, NULL);
                        EM_DEBUG_LOG("decoded_attachment_size [%d]", decoded_attachment_size);
-                       attachment->attachment_size = decoded_attachment_size;
-                       attachment->attachment_path = EM_SAFE_STRDUP(buf);
+                       attachment->attachment_size        = decoded_attachment_size;
+                       attachment->attachment_path        = EM_SAFE_STRDUP(path_buf);
+                       attachment->content_id             = EM_SAFE_STRDUP(ai->content_id);
+                       attachment->attachment_save_status = 1;
 
                        /*  update attachment information. */
-                       if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err))  {
+                       if (!emstorage_change_attachment_field(multi_user_name, mail_id, UPDATE_SAVENAME, attachment, true, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
                                /*  delete created file. */
-                               remove(buf);
-
+                               remove(move_buf);
                                goto FINISH_OFF;
                        }
-
-#ifdef __FEATURE_DRM__
-                       if (emcore_check_drm(attachment))  {    /*  has drm attachment ? */
-                               if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, (void *)attachment->attachment_path, NULL) == DRM_RETURN_SUCCESS)
-                                       EM_DEBUG_LOG("drm_svc_register_file success");
-                               else
-                                       EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
-                               mail->DRM_status = attachment->attachment_drm_type;
-                       }
-#endif /* __FEATURE_DRM__ */
                }
                else  {
                        EM_DEBUG_EXCEPTION("invalid attachment sequence...");
                        err = EMAIL_ERROR_INVALID_ATTACHMENT;
                        goto FINISH_OFF;
                }
-       
+
                EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
        }
 
        ret = true;
-       
+
 FINISH_OFF:
+
+       if (ret == true)
+               emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
+       else {
+               if (err != EMAIL_ERROR_CANCELLED)
+                       emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
+       }
+
+       EM_SAFE_FREE(server_uid);
+
        if (stream)  {
                stream = mail_close (stream);
        }
@@ -2208,27 +2267,301 @@ FINISH_OFF:
                emcore_free_content_info(cnt_info);
                EM_SAFE_FREE(cnt_info);
        }
+
        if (mail)
                emstorage_free_mail(&mail, 1, NULL);
 
-       EM_SAFE_FREE(server_uid);
+       if (err_code != NULL)
+               *err_code = err;
 
-       if (ret == true)
-               emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
+       EM_DEBUG_FUNC_END();
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_download_attachment(char *multi_user_name, int mail_id, int nth,
+               int cancellable, int event_handle, int auto_download, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], err_code[%p]", mail_id, nth, err_code);
+
+       int err = EMAIL_ERROR_NONE;
+
+       if (mail_id < 1) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+
+               if (err_code != NULL)
+                       *err_code = err;
+
+               if (!auto_download)
+                       emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, nth, err);
+               return FALSE;
+       }
+
+       int ret = FALSE;
+       MAILSTREAM *stream = NULL;
+       BODY *mbody = NULL;
+       emstorage_mail_tbl_t *mail = NULL;
+       emstorage_attachment_tbl_t *attachment = NULL;
+       emstorage_attachment_tbl_t *attachment_list = NULL;
+
+       struct attachment_info *ai = NULL;
+       struct _m_content_info *cnt_info = NULL;
+       void *tmp_stream = NULL;
+       char *server_uid = NULL;
+       char move_buf[512], path_buf[512];
+
+       int msg_no = 0;
+       int account_id = 0;
+       int server_mbox_id = 0;
+
+       int current_attachment_no = 0;
+       int attachment_count = 0;
+       int decoded_attachment_size = 0;
+       int i = 0;
+
+       GMimeMessage *message = NULL;
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       /*  get mail from mail table. */
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (!mail->server_mail_status) {
+               EM_DEBUG_EXCEPTION("not synchronous mail...");
+               err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
+               goto FINISH_OFF;
+       }
+
+       if (nth == 0) { /* download all attachments, nth starts from 1, not zero */
+               if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_list, &attachment_count)) != EMAIL_ERROR_NONE ) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+       else {  /* download only nth attachment */
+               attachment_count = 1;
+               if (!emstorage_get_attachment_nth(multi_user_name, mail_id, nth, &attachment_list, true, &err) || !attachment_list) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       account_id = mail->account_id;
+       server_uid = EM_SAFE_STRDUP(mail->server_mail_id);
+       server_mbox_id = mail->mailbox_id;
+
+       if (attachment_count == 1 && attachment_list) {
+               if (!auto_download) {
+                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, attachment_list[0].attachment_name, nth, 0))
+                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+               }
+       }
+
+       /* open mail server. */
+       if (!auto_download) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
+                       EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+                       if(err == EMAIL_ERROR_NO_SUCH_HOST)
+                               err = EMAIL_ERROR_CONNECTION_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               stream = (MAILSTREAM *)tmp_stream;
+       }
        else {
-               if (err != EMAIL_ERROR_CANCELLED)
-                       emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
+               MAILSTREAM **mstream = NULL;
+               mstream = emcore_get_recv_stream (multi_user_name, account_id, server_mbox_id, &err);
+
+               if (!mstream) {
+                       EM_DEBUG_EXCEPTION("emcore_get_recv_stream failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+               stream = (MAILSTREAM *)*mstream;
+       }
+
+       for (i = 0; i < attachment_count; i++) {
+               EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count);
+
+               attachment = attachment_list + i;
+
+               if (nth == 0)                                           /*  download all attachments, nth starts from 1, not zero */
+                       current_attachment_no = i + 1;  /*  attachment no */
+               else                                                            /*  download only nth attachment */
+                       current_attachment_no = nth;    /*  attachment no */
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               /*free cnt_info before reusing*/
+               if (cnt_info) {
+                       emcore_free_content_info(cnt_info);
+                       EM_SAFE_FREE(cnt_info);
+               }
+
+               if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
+                       EM_DEBUG_EXCEPTION("malloc failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               cnt_info->grab_type = GRAB_TYPE_ATTACHMENT;     /*  attachment */
+               cnt_info->file_no   = current_attachment_no;    /*  attachment no */
+
+               /* set sparep(member of BODY) memory free function. */
+               mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sparep);
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               msg_no = server_uid? atoi(server_uid): 0;
+
+               /*  get body structure. */
+               /*  don't free mbody because mbody is freed in closing mail_stream. */
+               if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0) {
+                       EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               if (!emcore_gmime_construct_mime_part_with_bodystructure(mbody, &message, "1", NULL)) {
+                       EM_DEBUG_EXCEPTION("emcore_gmime_construct_mime_part_with_bodystructure failed");
+                       goto FINISH_OFF;
+               }
+
+               if (g_strrstr(mail->full_address_from, "mmsc.plusnet.pl") != NULL ||
+                       g_strrstr(mail->full_address_from, "mms.t-mobile.pl") != NULL) {
+                       cnt_info->attachment_only = 1;
+               }
+
+               /* fill up section list */
+               g_mime_message_foreach(message, emcore_gmime_get_attachment_section_foreach_cb, (gpointer)cnt_info);
+               cnt_info->file_no = current_attachment_no;
+
+               /* FETCH sections */
+               if (!emcore_gmime_fetch_imap_attachment_section(stream, mail_id, msg_no,
+                               current_attachment_no, cnt_info, message, auto_download, event_handle, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_gmime_fetch_imap_attachment_section failed");
+                       goto FINISH_OFF;
+               }
+
+               /* decode contents */
+               g_mime_message_foreach(message, emcore_gmime_imap_parse_full_foreach_cb, (gpointer)cnt_info);
+
+               if (message) {
+                       g_object_unref(message);
+                       message = NULL;
+               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               /* select target attachment information. */
+               for (ai = cnt_info->file ; ai; ai = ai->next) {
+                       EM_DEBUG_LOG_SEC("[in loop] save[%s] name[%s] no[%d]", ai->save, ai->name, cnt_info->file_no);
+
+                       if (--cnt_info->file_no == 0)
+                               break;
+               }
+               EM_DEBUG_LOG("selected cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
+
+               if (cnt_info->file_no != 0 || ai == NULL) {
+                       EM_DEBUG_EXCEPTION("invalid attachment sequence...");
+                       err = EMAIL_ERROR_INVALID_ATTACHMENT;
+                       goto FINISH_OFF;
+               }
+
+               /* rename temporary file to real file. */
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, current_attachment_no, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, current_attachment_no, ai->name, move_buf, path_buf, sizeof(path_buf), &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                       err = EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME;
+                       goto FINISH_OFF;
+               }
+
+               emcore_get_file_size(move_buf, &decoded_attachment_size, NULL);
+               EM_DEBUG_LOG("decoded_attachment_size [%d]", decoded_attachment_size);
+               attachment->attachment_size        = decoded_attachment_size;
+               attachment->attachment_path        = EM_SAFE_STRDUP(path_buf);
+               attachment->content_id             = EM_SAFE_STRDUP(ai->content_id);
+               attachment->attachment_save_status = 1;
+
+               /*  update attachment information. */
+               if (!emstorage_change_attachment_field(multi_user_name, mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
+                       /*  delete created file. */
+                       g_remove(move_buf);
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG(">>>>>> Attachment Downloading [%d/%d] completed", i+1, attachment_count);
+       }
+
+       ret = TRUE;
+
+FINISH_OFF:
+
+       if (!auto_download) {
+               if (ret == TRUE)
+                       emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
+               else {
+                       if (err != EMAIL_ERROR_CANCELLED)
+                               emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
+               }
+       }
+
+       EM_SAFE_FREE(server_uid);
+
+       if (message) {
+               g_object_unref(message);
+               message = NULL;
        }
 
+       if (stream && !auto_download) {
+               stream = mail_close(stream);
+       }
+
+       if (attachment_list)
+               emstorage_free_attachment(&attachment_list, attachment_count, NULL);
+
+       if (cnt_info) {
+               emcore_free_content_info(cnt_info);
+               EM_SAFE_FREE(cnt_info);
+       }
+
+       if (mail)
+               emstorage_free_mail(&mail, 1, NULL);
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        EM_DEBUG_FUNC_END();
        return ret;
 }
 
+
 #ifdef __ATTACHMENT_OPTI__
-INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, int nth, int *err_code)
+INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, int nth, int event_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
 
@@ -2239,7 +2572,7 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
        emstorage_mail_tbl_t *mail = NULL;
        emstorage_attachment_tbl_t *attachment = NULL;
        void *tmp_stream = NULL;
-       char *s_uid = NULL, *server_mbox = NULL, buf[512];
+       char *s_uid = NULL, *server_mbox = NULL, move_buf[512], path_buf[512];
        emstorage_attachment_tbl_t *attachment_list = NULL;
        int current_attachment_no = 0;
        int attachment_count_to_be_downloaded = 0;              /*  how many attachments should be downloaded */
@@ -2260,7 +2593,9 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 #endif /*  SUPPORT_EXTERNAL_MEMORY */
        int decoded_attachment_size = 0;
        
-       memset(buf, 0x00, 512);
+       memset(move_buf, 0x00, 512);
+       memset(path_buf, 0x00, 512);
+
        /* CID FIX 31230 */
        if (mail_id < 1 || !nth) {
                EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
@@ -2277,56 +2612,42 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 
                return false;
        }
-       
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        only_body_download = false;
-       
+
        attachment_no = atoi(nth);
 
        if (attachment_no == 0) {
                /*  download all attachments, nth starts from 1, not zero */
                /*  get attachment list from db */
                attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
-               if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE || !attachment_list){
+               if ( (err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE || !attachment_list){
                        EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
        }
        else {  /*  download only nth attachment */
                attachment_count_to_be_downloaded = 1;
-               if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
+               if (!emstorage_get_attachment_nth(multi_user_name, mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
                        EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
-
-
                        goto FINISH_OFF;
                }
        }
 
 
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
-       
        /*  get mail from mail table. */
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
-               
-
                goto FINISH_OFF;
        }
 
        /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /*  faizan.h@samsung.com */
 
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        account_id = mail->account_id;
        s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
@@ -2335,7 +2656,7 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
 
                status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
@@ -2344,12 +2665,7 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 
        stream = (MAILSTREAM *)tmp_stream;
 
-
-       if (!emcore_check_thread_status()) {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        for (i = 0; i < attachment_count_to_be_downloaded; i++) {
                EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
@@ -2364,17 +2680,14 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
                        current_attachment_no = attachment_no;          /*  attachment no */
                }
 
-               if (!emcore_check_thread_status()) {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                _imap4_received_body_size = 0;
                _imap4_last_notified_body_size = 0;
                _imap4_total_body_size = 0;                             /*  This will be assigned in imap_mail_write_body_to_file() */
                _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
 
-               
+
                EM_SAFE_FREE(savefile);
                if (!emcore_get_temp_file_name(&savefile, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
@@ -2402,7 +2715,7 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
                        EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
                        goto FINISH_OFF;
                }
-               
+
                if (bIs_full) {
                        /* If external memory not present, return error */
                        if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
@@ -2413,48 +2726,33 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
                }
 #endif /*  SUPPORT_EXTERNAL_MEMORY */
 
-               if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, attachment_no, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
+               if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, move_buf, path_buf, sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_move_file(savefile, buf, false, &err)) {
+               if (!emstorage_move_file(savefile, move_buf, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
-
                        goto FINISH_OFF;
                }
 
-               emcore_get_file_size(buf, &decoded_attachment_size, NULL);
+               emcore_get_file_size(move_buf, &decoded_attachment_size, NULL);
                EM_DEBUG_LOG("decoded_attachment_size [%d]", decoded_attachment_size);
-               attachment->attachment_size = decoded_attachment_size;
-               attachment->attachment_path = EM_SAFE_STRDUP(buf);
+               attachment->attachment_size        = decoded_attachment_size;
+               attachment->attachment_path        = EM_SAFE_STRDUP(path_buf);
+               attachment->attachment_save_status = 1;
                /*  update attachment information. */
-               if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
+               if (!emstorage_change_attachment_field(multi_user_name, mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
-
                        goto FINISH_OFF;
                }
 
-#ifdef __FEATURE_DRM__
-               if (emcore_check_drm(attachment)) {
-                       /*  is drm */
-                       if (drm_svc_register_file(attachment->attachment_path) == DRM_RESULT_SUCCESS)
-                               EM_DEBUG_LOG("drm_svc_register_file success");
-                       else
-                               EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
-                       mail->flag3 = attachment->flag2;
-               }
-#endif /* __FEATURE_DRM__ */
-
-               if (!emcore_check_thread_status()) {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
        }
@@ -2469,10 +2767,10 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
                stream = mail_close (stream);
        }
 
-       if (attachment_list) 
+       if (attachment_list)
                emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
 
-       if (mail) 
+       if (mail)
                emstorage_free_mail(&mail, 1, NULL);
 
        EM_SAFE_FREE(s_uid);
@@ -2491,17 +2789,32 @@ INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, i
 }
 #endif
 
-INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, int account_id, 
-                         int mail_id, int verbose, int with_attach, int limited_size, int event_handle , int *err_code)
+/*
+Three thread can call this function :
+   event worker thread,
+   partial body thread,
+   thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT
+   CANCEL should be revised, later
+*/
+INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (char *multi_user_name, 
+                                                         void *mail_stream, 
+                                                         int account_id, 
+                                                         int mail_id, 
+                                                         int verbose, 
+                                                         int with_attach, 
+                                                         int limited_size, 
+                                                         int event_handle, 
+                                                         int cancellable, 
+                                                         int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d], event_handle [%d]", 
+       EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d], event_handle [%d]",
                                                  mail_stream, account_id, mail_id, verbose, with_attach, event_handle);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int status = EMAIL_DOWNLOAD_FAIL;
        int pop3_body_size = 0;
        int pop3_downloaded_size = 0;
+       int p_with_attach = with_attach;
        MAILSTREAM *stream = NULL;
        BODY *mbody = NULL;
        PARTLIST *section_list = NULL;
@@ -2511,9 +2824,9 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
        email_account_t *ref_account = NULL;
        struct attachment_info *ai = NULL;
        struct _m_content_info *cnt_info = NULL;
-       void *tmp_stream = NULL; 
        char *s_uid = NULL; 
-       char buf[512] = {0};
+       char move_buf[512] = {0};
+    char path_buf[512] = {0};
        int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
        int iActualSize = 0;
        char html_body[MAX_PATH] = {0};
@@ -2523,7 +2836,7 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
 #endif
 
        if (mail_id < 1)  {
-               EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d]", 
+               EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d]",
                                                               mail_stream, account_id, mail_id, verbose, with_attach);
                err = EMAIL_ERROR_INVALID_PARAM;
                if (err_code != NULL)
@@ -2532,50 +2845,75 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
                return false;
        }
-       
-       FINISH_OFF_IF_CANCELED;
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        only_body_download = true;
 
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+       /* looking for mail by mail_id in DB*/
+       if (!emstorage_get_mail_by_id (multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
-       
-       if (1 == mail->body_download_status)  {
-               EM_DEBUG_EXCEPTION("not synchronous mail...");
-               err = EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED;
+
+       /* if mail is downloaded */
+       if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED)  {
+               /* after entering viewer, the mail has been just downloaded */
+               if (!emcore_notify_network_event (NOTI_DOWNLOAD_BODY_START,
+                                           mail_id,
+                                           "dummy-file",   /* ?? */
+                                           mail->mail_size, /*_pop3_total_body_size*/
+                                           0))
+                       EM_DEBUG_EXCEPTION ("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] error >>>> ");
+               else
+                       EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)",
+                                           0, mail->mail_size);
+
+               err = EMAIL_ERROR_NONE; //EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED;
+               ret = true;
                goto FINISH_OFF;
        }
-       
+
        s_uid                             = EM_SAFE_STRDUP(mail->server_mail_id);
        attachment.account_id             = mail->account_id;
        attachment.mail_id                = mail->mail_id;
        attachment.mailbox_id             = mail->mailbox_id;
        attachment.attachment_save_status = 0;
        
-       if (!(ref_account = emcore_get_account_reference(account_id)))   {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))   {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       FINISH_OFF_IF_CANCELED;
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        /*  open mail server. */
-       if (!mail_stream)  {
-               if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+       if (!mail_stream) {
+#if 0
+               MAILSTREAM *tmp_stream = NULL;
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
-                       status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
                        goto FINISH_OFF;
                }
+
                stream = (MAILSTREAM *)tmp_stream;
+#endif
+               MAILSTREAM **mstream = NULL;
+               mstream = emcore_get_recv_stream (multi_user_name, account_id, mail->mailbox_id, &err);
+               if (!mstream) {
+                       EM_DEBUG_EXCEPTION("emcore_get_recv_stream failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+               stream = (MAILSTREAM *)*mstream;
        }
        else
                stream = (MAILSTREAM *)mail_stream;
-       
-       FINISH_OFF_IF_CANCELED;
-       
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
                EM_DEBUG_EXCEPTION("em_malloc failed...");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -2586,12 +2924,11 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                /*  POP3 */
                /*  in POP3 case, both text and attachment are downloaded in this call. */
                cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
-               attachment.attachment_save_status = 1;          /*  all attachments should be downloaded in the case of POP3 */
 
                mailbox.account_id = account_id;
 
                /*  download all uids from server. */
-               if (!emcore_download_uid_all (stream, &mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err))  {
+               if (!emcore_download_uid_all (multi_user_name, stream, &mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -2603,16 +2940,14 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                        goto FINISH_OFF;
                }
 
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                _pop3_received_body_size = 0;
                _pop3_total_body_size = 0;
                _pop3_last_notified_body_size = 0;
                _pop3_receiving_mail_id = mail_id;
-               
+
                /*  send read mail commnad. */
                if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
@@ -2626,8 +2961,9 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                else
                        EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
 
-               FINISH_OFF_IF_CANCELED;
-               
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
                /*  save message into tempfile */
                /*  parsing mime from stream. */
 #ifdef __FEATURE_USE_GMIME__
@@ -2635,19 +2971,30 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                int fd = 0;
                char *tmp_path = emcore_mime_get_save_file_name(&err);
                EM_DEBUG_LOG ("tmp_path[%s]", tmp_path);
-               fd = open(tmp_path, O_WRONLY|O_CREAT, 0644);
-               if (fd < 0) {
-                       EM_DEBUG_EXCEPTION("open error [%d]:  holder is a filename that will be saved.", errno);
+               err = em_open(tmp_path, O_WRONLY | O_CREAT, 0644, &fd);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("open error [%d]:  holder is a filename that will be saved.", err);
                }
 
                while(emcore_mime_get_line_from_sock((void *)stream, sock_buf, 1024, &err)) {
                        if (write(fd, sock_buf, EM_SAFE_STRLEN(sock_buf)) != EM_SAFE_STRLEN(sock_buf)) {
                                EM_DEBUG_EXCEPTION("write failed");
                        }
+
+                       if (cancellable) {
+                               int type = 0;
+                               if (!emcore_check_event_thread_status(&type, event_handle)) {
+                                       EM_DEBUG_LOG ("CANCELED EVENT: type [%d]", type);
+                                       err = EMAIL_ERROR_CANCELLED;
+                                       EM_SAFE_CLOSE (fd);
+                                       g_remove(tmp_path);
+                                       goto FINISH_OFF;
+                               }
+                       }
                }
-               close(fd);
+               EM_SAFE_CLOSE (fd);
 
-               emcore_gmime_parse_mime(tmp_path, cnt_info, &err);
+               emcore_gmime_pop3_parse_mime(tmp_path, cnt_info, &err);
 
                g_remove(tmp_path);
 #else
@@ -2656,11 +3003,13 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                        goto FINISH_OFF;
                }
 #endif /* __FEATURE_USE_GMIME__ */
-               FINISH_OFF_IF_CANCELED;
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
        }
        else  { /*  in IMAP case, both text and attachment list are downloaded in this call. */
                /*  This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
-               if (with_attach > 0)
+               if (p_with_attach > 0)
                        cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
                else
                        cnt_info->grab_type = GRAB_TYPE_TEXT;
@@ -2678,7 +3027,8 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                        goto FINISH_OFF;
                }
 
-               FINISH_OFF_IF_CANCELED;
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                if (mbody->type == TYPEMULTIPART) {
                        EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
@@ -2741,6 +3091,7 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
 
                _imap4_received_body_size = 0;
                _imap4_last_notified_body_size = 0;
+
                if (multi_part_body_size > 0) { /*  download multiparts */
                        _imap4_total_body_size = multi_part_body_size;
                        _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
@@ -2756,39 +3107,76 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                        EM_DEBUG_EXCEPTION("emcore_get_body_part_list_full failed [%d]", err);
                        goto FINISH_OFF;
                }
-               FINISH_OFF_IF_CANCELED;
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
        }
 
-       if (cnt_info->text.plain)  {
-               EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
+       if (cnt_info->text.plain) {
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               char *charset_plain_text = NULL;
+               EM_DEBUG_LOG_SEC("cnt_info->text.plain [%s]", cnt_info->text.plain);
+
+               char *file_content = NULL;
+               int content_size = 0;
+
+               if (emcore_get_content_from_file(cnt_info->text.plain, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+               }
+
+               if (file_content && content_size > 0) {
+                       char escape = 0x1b;
+                       char detector[25] = {0,};
+                       snprintf(detector, sizeof(detector), "%c$B", escape);
+                       if (g_strrstr(cnt_info->text.plain_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                               cnt_info->text.plain_charset = "ISO-2022-JP";
+                       }
+               }
+
+               EM_SAFE_FREE(file_content);
+
+               if (cnt_info->text.plain_charset)
+                       charset_plain_text = cnt_info->text.plain_charset;
+               else {
+                       if (mail->default_charset) {
+                               charset_plain_text = mail->default_charset;
+                       } else {
+                               charset_plain_text = "UTF-8";
+                       }
+               }
+               EM_DEBUG_LOG("PLAIN DEFAULT CHARSET : %s", charset_plain_text);
 
-               if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-
-               if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset  :  "UTF-8", buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, charset_plain_text, move_buf, path_buf, sizeof(path_buf), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err))  {
+               if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                EM_SAFE_FREE(mail->file_path_plain);
-               mail->file_path_plain = EM_SAFE_STRDUP(buf);
+               mail->file_path_plain = EM_SAFE_STRDUP(path_buf);
                EM_DEBUG_LOG_SEC("mail->file_path_plain [%s]", mail->file_path_plain);
        }
        
-       if (cnt_info->text.html)  {
-               if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
+       if (cnt_info->text.html) {
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
-               
+
                if (cnt_info->text.html_charset != NULL) {
                        memcpy(html_body, cnt_info->text.html_charset, EM_SAFE_STRLEN(cnt_info->text.html_charset));
                        strcat(html_body, HTML_EXTENSION_STRING);
@@ -2800,231 +3188,1018 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                else {
                        memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
                }
-               if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, html_body, move_buf, path_buf, sizeof(path_buf), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_move_file(cnt_info->text.html, buf, false, &err))  {
+               if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
                }
                EM_SAFE_FREE(mail->file_path_html);
-               mail->file_path_html = EM_SAFE_STRDUP(buf);
+               mail->file_path_html = EM_SAFE_STRDUP(path_buf);
        }
-       
-       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && 
-                                                                                     limited_size < pop3_body_size)
-               mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
-       else
-               mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
 
-       /* Update local_preview_text */
-       if ((err = emcore_get_preview_text_from_file (mail->file_path_plain, mail->file_path_html, 
-                                             MAX_PREVIEW_TEXT_LENGTH, &mail->preview_text)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
+       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION &&
+                                                                                     limited_size < pop3_body_size) {
+               mail->body_download_status        = (mail->body_download_status & ~0x00000003) | EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
+       }
+       else {
+               mail->body_download_status        = (mail->body_download_status & ~0x00000003) | EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+               p_with_attach = true;
+       }
+
+       /* Update local_preview_text */
+       if ((err = emcore_get_preview_text_from_file (multi_user_name, 
+                                                    mail->file_path_plain, 
+                                                    mail->file_path_html, 
+                                                    MAX_PREVIEW_TEXT_LENGTH, 
+                                                    &mail->preview_text)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
+       }
+
+#ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
+       if (html_changed) mail->flag2 = 1;
+#endif
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++)  {
+
+               if (ai->type == 1)
+                       local_inline_content_count++;
+               else
+                       local_attachment_count++;
+
+               if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && only_body_download && ai->type != INLINE_ATTACHMENT)
+                       continue;
+
+               attachment.attachment_id                    = attachment_num;
+               attachment.attachment_size                  = ai->size;
+               attachment.attachment_path                  = ai->save;
+               attachment.content_id                       = ai->content_id;
+               attachment.attachment_name                  = ai->name;
+               attachment.attachment_drm_type              = ai->drm;
+               attachment.attachment_inline_content_status = ai->type == 1;
+               attachment.attachment_mime_type             = ai->attachment_mime_type;
+
+               if (p_with_attach)
+                       attachment.attachment_save_status       = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+               else
+                       attachment.attachment_save_status       = (ai->type == 1) ? EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED:EMAIL_BODY_DOWNLOAD_STATUS_NONE;
+
+#ifdef __ATTACHMENT_OPTI__
+               attachment.encoding                         = ai->encoding;
+               attachment.section                          = ai->section;
+#endif
+               EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
+               EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
+               EM_DEBUG_LOG_SEC("attachment.attachment_path[%s]", attachment.attachment_path);
+               EM_DEBUG_LOG("attachment.content_id[%s]", attachment.content_id);
+               EM_DEBUG_LOG_SEC("attachment.attachment_name[%s]", attachment.attachment_name);
+               EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
+               EM_DEBUG_LOG("attachment.attachment_save_status[%d]", attachment.attachment_save_status);
+               EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
+
+               if (ai->save)  {
+            memset(move_buf, 0x00, sizeof(move_buf));
+            memset(path_buf, 0x00, sizeof(path_buf));
+
+                       /*  in POP3 case, rename temporary file to real file. */
+                       if (ai->type == 1)  {           /*  it is inline content */
+                               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, attachment.attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+                       }
+                       else  {
+                               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, attachment_num, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, attachment_num, attachment.attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       if (!emstorage_move_file(ai->save, move_buf, false, &err))  {
+                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+
+                               /*  delete all created files. */
+                               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
+                                       /* goto FINISH_OFF; */
+                               }
+
+                               if (!emstorage_delete_dir(move_buf, NULL)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
+                                       /* goto FINISH_OFF; */
+                               }
+
+
+                               goto FINISH_OFF;
+                       }
+
+                       EM_SAFE_FREE(ai->save);
+                       ai->save = EM_SAFE_STRDUP(path_buf);
+
+                       attachment.attachment_path = ai->save;
+
+               }
+
+#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
+               /*  Information :  Attachment info already saved if partial body is dowloaded. */
+               if (ai->type)  {
+                       emstorage_attachment_tbl_t *attch_info = NULL;
+                       /*  Get attachment details  */
+                       if (!emstorage_get_attachment_nth(multi_user_name, mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info)  {
+                               EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
+                               if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) {   /*  save only attachment file. */
+                                       if (!emstorage_add_attachment(multi_user_name, &attachment, 0, false, &err)) {
+                                               EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
+                                               if (attch_info)
+                                                       emstorage_free_attachment(&attch_info, 1, NULL);
+
+                                               /*  delete all created files. */
+                                               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               if (!emstorage_delete_dir(move_buf, &err))  {
+                                                       EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
+                                                       goto FINISH_OFF;
+                                               }
+
+                                               /*  ROLLBACK TRANSACTION; */
+                                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+
+
+                                               goto FINISH_OFF;
+                                       }
+                               }
+                       }
+                       else {
+                               EM_DEBUG_LOG("Attachment info already exists...!");
+                               /* Update attachment size */
+                               EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
+                               attch_info->attachment_size = ai->size;
+                               attch_info->attachment_save_status = attachment.attachment_save_status;
+
+                               if (!emstorage_update_attachment(multi_user_name, attch_info, true, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
+                                       emstorage_free_attachment(&attch_info, 1, NULL); /*prevent 17956*/
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       if (attch_info)
+                               emstorage_free_attachment(&attch_info, 1, NULL);
+               }
+
+#else
+
+               if (ai->type)  {
+                       mail->attachment_yn = 1;
+                       /*  save only attachment file. */
+                       if (!emstorage_add_attachment(multi_user_name, &attachment, 0, false, &err))  {
+                               EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
+                               if (bIsAdd_to_mmc)  {
+                                       if (attachment.attachment) {
+                                       }
+                               }
+                               else  {
+                                       /*  delete all created files. */
+                                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (!emstorage_delete_dir(move_buf, &err))  {
+                                               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       /*  ROLLBACK TRANSACTION; */
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+                                       goto FINISH_OFF;
+                               }
+                       }
+           }
+#endif /*  End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
+
+       }
+
+       mail->attachment_count = local_attachment_count;
+       mail->inline_content_count = local_inline_content_count;
+
+       /*  change mail's information. */
+       if (!emstorage_change_mail_field(multi_user_name, mail_id, APPEND_BODY, mail, false, &err))  {
+               EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
+               goto FINISH_OFF;
+       }
+
+#ifdef __FEATURE_BODY_SEARCH__
+       /* strip html content and save into mail_text_tbl */
+       char *stripped_text = NULL;
+       if (!emcore_strip_mail_body_from_file(multi_user_name, mail, &stripped_text, &err) || stripped_text == NULL) {
+               EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
+       }
+
+       emstorage_mail_text_tbl_t *mail_text;
+       if (!emstorage_get_mail_text_by_id(multi_user_name, mail_id, &mail_text, true, &err) || !mail_text) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_text_by_id failed [%d]", err);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
+               goto FINISH_OFF;
+       }
+
+       EM_SAFE_FREE(mail_text->body_text);
+       mail_text->body_text = stripped_text;
+
+       if (!emstorage_change_mail_text_field(multi_user_name, mail_id, mail_text, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_change_mail_text_field failed [%d]", err);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
+               emstorage_free_mail_text(&mail_text, 1, NULL); /*prevent 17957*/
+               goto FINISH_OFF;
+       }
+
+       if (mail_text)
+               emstorage_free_mail_text(&mail_text, 1, NULL);
+#endif
+
+       EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
+
+       /*  in pop3 mail case, the mail is deleted from server after being downloaded. */
+       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
+#ifdef DELETE_AFTER_DOWNLOADING
+               char delmsg[24];
+
+               SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
+
+               if (!ref_account->keep_mails_on_pop_server_after_download)  {
+                       if (!emcore_delete_mails_from_pop3_server(multi_user_name, &mbox, delmsg, &err))
+                               EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server failed [%d]", err);
+               }
+#endif
+
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (ref_account) {
+               emcore_free_account(ref_account);
+               EM_SAFE_FREE(ref_account);
+       }
+
+#if 0
+       /* note that local stream should be freed here*/
+       if (mail_stream == NULL && stream != NULL)  {
+               stream = mail_close (stream);
+       }
+#endif
+
+       multi_part_body_size = 0;
+       _pop3_received_body_size = 0;
+       _pop3_last_notified_body_size = 0;
+       _pop3_total_body_size = 0;
+       _pop3_receiving_mail_id = 0;
+
+       _imap4_received_body_size = 0;
+       _imap4_last_notified_body_size = 0;
+       _imap4_total_body_size = 0;
+       _imap4_download_noti_interval_value = 0;
+
+       if (cnt_info) {
+               emcore_free_content_info(cnt_info);
+               EM_SAFE_FREE(cnt_info);
+       }
+       if (mail)
+               emstorage_free_mail(&mail, 1, NULL);
+       EM_SAFE_FREE(s_uid);
+
+       multi_part_body_size = 0;
+
+       if (ret == true)
+               emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
+       else
+               emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
+
+       if (err_code != NULL)
+               *err_code = err;
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name, void *mail_stream,
+               int account_id, int mail_id, int with_attach, int limited_size,
+               int event_handle, int cancellable, int auto_download, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], "
+                       "with_attach[%d], event_handle [%d]", mail_stream, account_id, mail_id,
+                       with_attach, event_handle);
+
+       int ret = FALSE;
+       int err = EMAIL_ERROR_NONE;
+       int b_with_attach = with_attach;
+       int need_update_attachment = 0;
+
+       BODY *mbody = NULL;
+       MAILSTREAM *stream = NULL;
+
+       email_account_t *ref_account = NULL;
+       emstorage_mail_tbl_t *mail = NULL;
+       emstorage_attachment_tbl_t attachment = {0,};
+       emstorage_attachment_tbl_t *attch_info = NULL;
+
+       struct _m_content_info *cnt_info = NULL;
+       struct attachment_info *ai = NULL;
+
+       char *s_uid = NULL;
+       char move_buf[512] = {0};
+    char path_buf[512] = {0};
+
+       int msgno = 0;
+       int attachment_num = 1;
+       int local_attachment_count = 0;
+       int local_inline_content_count = 0;
+
+       char html_body[MAX_PATH] = {0,};
+
+       emcore_uid_list *uid_list = NULL;
+
+       GMimeMessage *message1 = NULL;
+
+       if (mail_id < 1) {
+               EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d],"
+                               "with_attach[%d]", mail_stream, account_id, mail_id, with_attach);
+
+               err = EMAIL_ERROR_INVALID_PARAM;
+               if (err_code != NULL)
+                       *err_code = err;
+
+               if (!auto_download)
+                       emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
+               return ret;
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       /* looking for mail by mail_id in DB*/
+       if (!emstorage_get_mail_by_id (multi_user_name, mail_id, &mail, true, &err) || !mail) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       /* if mail is downloaded */
+       if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
+               /* after entering viewer, the mail has been just downloaded */
+               if (!auto_download) {
+                       if (!emcore_notify_network_event (NOTI_DOWNLOAD_BODY_START, mail_id,
+                                       "dummy-file", mail->mail_size, 0))
+                               EM_DEBUG_EXCEPTION ("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] error >>>>");
+                       else
+                               EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, mail->mail_size);
+               }
+
+               err = EMAIL_ERROR_NONE; //EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED;
+               ret = TRUE;
+               goto FINISH_OFF;
+       }
+       else if (!mail->body_download_status)
+               need_update_attachment = 1;
+
+       s_uid = EM_SAFE_STRDUP(mail->server_mail_id);
+
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
+               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
+               err = EMAIL_ERROR_INVALID_ACCOUNT;
+               goto FINISH_OFF;
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       /*  open mail server. */
+       if (!mail_stream) {
+#if 0
+               MAILSTREAM *tmp_stream = NULL;
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+                       EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+               stream = (MAILSTREAM *)tmp_stream;
+#endif
+
+               MAILSTREAM **mstream = NULL;
+               mstream = emcore_get_recv_stream (multi_user_name, account_id, mail->mailbox_id, &err);
+
+               if (!mstream) {
+                       EM_DEBUG_EXCEPTION("emcore_get_recv_stream failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+               stream = (MAILSTREAM *)*mstream;
+       }
+       else
+               stream = (MAILSTREAM *)mail_stream;
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
+               EM_DEBUG_EXCEPTION("em_malloc failed...");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       /* POP3 */
+       /* in POP3 case, both text and attachment are downloaded in this call. */
+       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
+               int fd = 0;
+               int pop3_total_body_size = 0;
+               int pop3_downloaded_size = 0;
+               char sock_buf[1024] = {0,};
+               char *tmp_path = NULL;
+               email_internal_mailbox_t mailbox = {0,};
+
+               cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
+               mailbox.account_id = account_id;
+
+               /* download all uids from server. */
+               if (!emcore_download_uid_all (multi_user_name, stream, &mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               /* get mesg number to be related to last download mail from uid list file */
+               if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
+                       err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
+                       goto FINISH_OFF;
+               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               _pop3_received_body_size = 0;
+               _pop3_total_body_size = 0;
+               _pop3_last_notified_body_size = 0;
+               _pop3_receiving_mail_id = mail_id;
+
+               /* send read mail commnad. */
+               if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_total_body_size, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               _pop3_total_body_size = pop3_total_body_size;
+
+               if (!auto_download) {
+                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
+                               EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
+                       else
+                               EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
+               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               tmp_path = emcore_mime_get_save_file_name(&err);
+               EM_DEBUG_LOG ("tmp_path[%s]", tmp_path);
+
+               err = em_open(tmp_path, O_WRONLY | O_CREAT, 0644, &fd);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_open error [%d]", err);
+                       EM_SAFE_FREE(tmp_path);
+                       goto FINISH_OFF;
+               }
+
+               while(emcore_mime_get_line_from_sock((void *)stream, sock_buf, 1024, &err)) {
+                       if (write(fd, sock_buf, EM_SAFE_STRLEN(sock_buf)) != EM_SAFE_STRLEN(sock_buf)) {
+                               EM_DEBUG_EXCEPTION("write failed");
+                       }
+
+                       if (cancellable) {
+                               int type = 0;
+                               if (!emcore_check_event_thread_status(&type, event_handle)) {
+                                       EM_DEBUG_LOG ("CANCELED EVENT: type [%d]", type);
+                                       err = EMAIL_ERROR_CANCELLED;
+                                       EM_SAFE_CLOSE(fd);
+                                       g_remove(tmp_path);
+                                       EM_SAFE_FREE(tmp_path);
+                                       goto FINISH_OFF;
+                               }
+                       }
+               }
+               EM_SAFE_CLOSE(fd);
+
+               emcore_gmime_pop3_parse_mime(tmp_path, cnt_info, &err);
+
+               g_remove(tmp_path);
+               EM_SAFE_FREE(tmp_path);
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               if (limited_size != NO_LIMITATION && limited_size < pop3_total_body_size) {
+                       mail->body_download_status = (mail->body_download_status & ~0x00000003) | EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
+               } else {
+                       mail->body_download_status = (mail->body_download_status & ~0x00000003) | EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+                       b_with_attach = 1;
+               }
+       }
+       /* IMAP */
+       else if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
+               int total_mail_size = 0;
+
+               if (b_with_attach > 0 || need_update_attachment)
+                       cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
+               else
+                       cnt_info->grab_type = GRAB_TYPE_TEXT;
+
+               int uid = s_uid? atoi(s_uid):0;
+
+               /*  set sparep(member of BODY) memory free function  */
+               mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sparep);
+
+               /*  get body strucutre. */
+               /*  don't free mbody because mbody is freed in closing mail_stream. */
+               if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL))  {
+                       EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
+                       err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
+                       goto FINISH_OFF;
+               }
+
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               /* construct GMimeMessage from bodystructure*/
+               if (!emcore_gmime_construct_mime_part_with_bodystructure(mbody, &message1, "1", &total_mail_size)) {
+                       EM_DEBUG_EXCEPTION("emcore_gmime_construct_mime_part_with_bodystructure failed");
+                       err = EMAIL_ERROR_ON_PARSING;
+                       goto FINISH_OFF;
+               }
+
+               if (g_strrstr(mail->full_address_from, "mmsc.plusnet.pl") != NULL ||
+                       g_strrstr(mail->full_address_from, "mms.t-mobile.pl") != NULL) {
+                       cnt_info->attachment_only = 1;
+               }
+
+               /* fill up cnt_info */
+               g_mime_message_foreach(message1, emcore_gmime_imap_parse_bodystructure_foreach_cb, (gpointer)cnt_info);
+
+               /* fill up section list */
+               g_mime_message_foreach(message1, emcore_gmime_get_body_sections_foreach_cb, (gpointer)cnt_info);
+
+               /* FETCH sections and set content to message */
+               if (!emcore_gmime_fetch_imap_body_sections(stream, uid, mail_id,
+                               cnt_info, message1, event_handle, auto_download, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_gmime_get_imap_sections failed");
+                       goto FINISH_OFF;
+               }
+
+               /* decoding contents */
+               g_mime_message_foreach(message1, emcore_gmime_imap_parse_full_foreach_cb, (gpointer)cnt_info);
+
+               /* free resources */
+               if (message1) {
+                       g_object_unref(message1);
+                       message1 = NULL;
+               }
+
+               mail->body_download_status = (mail->body_download_status & ~0x00000003) | EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+       }
+
+       /* text/plain */
+       if (cnt_info->text.plain) {
+               char *charset_plain_text = NULL;
+               char *file_content = NULL;
+               int content_size = 0;
+
+               EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
+
+               if (emcore_get_content_from_file(cnt_info->text.plain, &file_content, &content_size) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed");
+               }
+
+               if (file_content && content_size > 0) {
+                       char escape = 0x1b;
+                       char detector[25] = {0,};
+                       snprintf(detector, sizeof(detector), "%c$B", escape);
+                       if (g_strrstr(cnt_info->text.plain_charset, "UTF-8") && g_strrstr(file_content, detector)) {
+                               cnt_info->text.plain_charset = "ISO-2022-JP";
+                       }
+               }
+
+               EM_SAFE_FREE(file_content);
+
+               if (cnt_info->text.plain_charset)
+                       charset_plain_text = cnt_info->text.plain_charset;
+               else {
+                       if (mail->default_charset) {
+                               charset_plain_text = mail->default_charset;
+                       } else {
+                               charset_plain_text = "UTF-8";
+                       }
+               }
+               EM_DEBUG_LOG("PLAIN DEFAULT CHARSET : %s", charset_plain_text);
+
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, charset_plain_text, move_buf, path_buf, sizeof(path_buf), &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               EM_SAFE_FREE(mail->file_path_plain);
+               mail->file_path_plain = EM_SAFE_STRDUP(path_buf);
+               EM_DEBUG_LOG_SEC("mail->file_path_plain [%s]", mail->file_path_plain);
+       }
+
+       /* text/html */
+       if (cnt_info->text.html) {
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (cnt_info->text.html_charset != NULL) {
+                       memcpy(html_body, cnt_info->text.html_charset, EM_SAFE_STRLEN(cnt_info->text.html_charset));
+                       strcat(html_body, HTML_EXTENSION_STRING);
+               }
+               else if (cnt_info->text.plain_charset != NULL) {
+                       memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
+                       strcat(html_body, HTML_EXTENSION_STRING);
+               }
+               else {
+                       memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
+               }
+
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, html_body, move_buf, path_buf, sizeof(path_buf), &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               EM_SAFE_FREE(mail->file_path_html);
+               mail->file_path_html = EM_SAFE_STRDUP(path_buf);
+       }
+
+       /* Update local_preview_text */
+       if ((err = emcore_get_preview_text_from_file (multi_user_name, 
+                                                    mail->file_path_plain, 
+                                                    mail->file_path_html, 
+                                                    MAX_PREVIEW_TEXT_LENGTH, 
+                                                    &mail->preview_text)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       attachment.account_id             = mail->account_id;
+       attachment.mail_id                = mail->mail_id;
+       attachment.mailbox_id             = mail->mailbox_id;
+       attachment.attachment_save_status = 0;
+
+       /* attachments */
+       if (need_update_attachment) {
+               int total_attachment_size;
+               int attachment_num_from_cnt_info;
+               int inline_attachment_num_from_cnt_info;
+
+               if ((err = emcore_update_attachment_except_inline(multi_user_name, 
+                                                cnt_info, 
+                                                mail->account_id,
+                                                mail->mail_id, 
+                                                mail->mailbox_id, 
+                                                &total_attachment_size,        
+                                                &attachment_num_from_cnt_info,
+                                                &inline_attachment_num_from_cnt_info)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+
+       ai = cnt_info->file;
+
+       while (ai) {
+               local_attachment_count++;
+
+               if (b_with_attach == 0) {
+                       attachment_num++;
+                       ai = ai->next;
+                       continue;
+               }
+
+               attachment.attachment_id                    = attachment_num;
+               attachment.attachment_size                  = ai->size;
+               attachment.attachment_path                  = ai->save;
+               attachment.content_id                       = ai->content_id;
+               attachment.attachment_name                  = ai->name;
+               attachment.attachment_drm_type              = ai->drm;
+               attachment.attachment_inline_content_status = 0;
+               attachment.attachment_mime_type             = ai->attachment_mime_type;
+               attachment.attachment_save_status           = 1;
+
+               EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
+               EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
+               EM_DEBUG_LOG_SEC("attachment.attachment_path[%s]", attachment.attachment_path);
+               EM_DEBUG_LOG("attachment.content_id[%s]", attachment.content_id);
+               EM_DEBUG_LOG_SEC("attachment.attachment_name[%s]", attachment.attachment_name);
+               EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
+               EM_DEBUG_LOG("attachment.attachment_save_status[%d]", attachment.attachment_save_status);
+               EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
+
+               if (!ai->save) {
+                       attachment_num++;
+                       ai = ai->next;
+                       continue;
+               }
+
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, attachment_num, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, attachment_num, attachment.attachment_name, move_buf, path_buf, sizeof(path_buf), &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+
+                       /*  delete all created files. */
+                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
+                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
+                               /* goto FINISH_OFF; */
+                       }
+
+                       if (!emstorage_delete_dir(move_buf, NULL)) {
+                               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
+                               /* goto FINISH_OFF; */
+                       }
+
+                       goto FINISH_OFF;
+               }
+
+               EM_SAFE_FREE(ai->save);
+               ai->save = EM_SAFE_STRDUP(path_buf);
+               attachment.attachment_path = ai->save;
+
+               /*  Get attachment details  */
+               if (!emstorage_get_attachment_nth(multi_user_name, mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
+
+                       if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) { /*  save only attachment file. */
+                               if (!emstorage_add_attachment(multi_user_name, &attachment, 0, false, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
+
+                                       if (attch_info)
+                                               emstorage_free_attachment(&attch_info, 1, NULL);
+
+                                       /*  delete all created files. */
+                                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       if (!emstorage_delete_dir(move_buf, &err))  {
+                                               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       /*  ROLLBACK TRANSACTION; */
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+
+                                       goto FINISH_OFF;
+                               }
+                       }
+               }
+               else {
+                       EM_DEBUG_LOG("Attachment info already exists...!");
+                       /* Update attachment size */
+                       EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
+                       EM_SAFE_FREE(attch_info->attachment_name);
+                       EM_SAFE_FREE(attch_info->attachment_path);
+                       EM_SAFE_FREE(attch_info->content_id);
+                       EM_SAFE_FREE(attch_info->attachment_mime_type);
+
+                       attch_info->attachment_size = ai->size;
+                       attch_info->attachment_save_status = attachment.attachment_save_status;
+                       attch_info->attachment_path = EM_SAFE_STRDUP(attachment.attachment_path);
+                       attch_info->content_id = EM_SAFE_STRDUP(attachment.content_id);
+                       attch_info->attachment_name = EM_SAFE_STRDUP(attachment.attachment_name);
+                       attch_info->attachment_drm_type = attachment.attachment_drm_type;
+                       attch_info->attachment_inline_content_status = 0;
+                       attch_info->attachment_mime_type = EM_SAFE_STRDUP(attachment.attachment_mime_type);
+
+                       if (!emstorage_update_attachment(multi_user_name, attch_info, true, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
+                               emstorage_free_attachment(&attch_info, 1, NULL); /*prevent 17956*/
+                               goto FINISH_OFF;
+                       }
+               }
+
+               if (attch_info)
+                       emstorage_free_attachment(&attch_info, 1, NULL);
+
+               attachment_num++;
+               ai = ai->next;
        }
-       
-#ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
-       if (html_changed) mail->flag2 = 1;
-#endif
-       
-       FINISH_OFF_IF_CANCELED;
-       
-       for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++)  {
+
+       /* inline attachments */
+       ai = cnt_info->inline_file;
+       while (ai) {
+               local_inline_content_count++;
+
                attachment.attachment_id                    = attachment_num;
                attachment.attachment_size                  = ai->size;
                attachment.attachment_path                  = ai->save;
+               attachment.content_id                       = ai->content_id;
                attachment.attachment_name                  = ai->name;
                attachment.attachment_drm_type              = ai->drm;
-               attachment.attachment_inline_content_status = ai->type == 1;
-               attachment.attachment_save_status           = 0;
+               attachment.attachment_inline_content_status = 1;
                attachment.attachment_mime_type             = ai->attachment_mime_type;
-#ifdef __ATTACHMENT_OPTI__
-               attachment.encoding                         = ai->encoding;
-               attachment.section                          = ai->section;
-#endif
+               attachment.attachment_save_status           = 1;
+
                EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
                EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
-               EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
+               EM_DEBUG_LOG_SEC("attachment.attachment_path[%s]", attachment.attachment_path);
+               EM_DEBUG_LOG("attachment.content_id[%s]", attachment.content_id);
                EM_DEBUG_LOG_SEC("attachment.attachment_name[%s]", attachment.attachment_name);
                EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
+               EM_DEBUG_LOG("attachment.attachment_save_status[%d]", attachment.attachment_save_status);
                EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
 
-               if (ai->type == 1)
-                       local_inline_content_count++;
-               else
-                       local_attachment_count++;
-
-               if (ai->save)  {
-                       /*  in POP3 case, rename temporary file to real file. */
-                       attachment.attachment_save_status = 1;
-                       if (ai->type == 1)  {           /*  it is inline content */
-                               if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
-                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-                                       goto FINISH_OFF;
-                               }
-                               if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err))  {
-                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                                       goto FINISH_OFF;
-                               }
-                       }
-                       else  {
-                               if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
-                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-                                       goto FINISH_OFF;
-                               }
-
-                               if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err))  {
-                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                                       goto FINISH_OFF;
-                               }
-                       }
+               if (!ai->save) {
+                       attachment_num++;
+                       ai = ai->next;
+                       continue;
+               }
 
-                       if (!emstorage_move_file(ai->save, buf, false, &err))  {
-                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
 
-                               /*  delete all created files. */
-                               if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
-                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
-                                       /* goto FINISH_OFF; */
-                               }
+               if (!emstorage_create_dir(multi_user_name, account_id, mail_id, 0, &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                       goto FINISH_OFF;
+               }
 
-                               if (!emstorage_delete_dir(buf, NULL)) {
-                                       EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
-                                       /* goto FINISH_OFF; */
-                               }
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, attachment.attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                       goto FINISH_OFF;
+               }
 
+               if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
 
-                               goto FINISH_OFF;
+                       /*  delete all created files. */
+                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
+                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
+                               /* goto FINISH_OFF; */
                        }
 
-                       EM_SAFE_FREE(ai->save); 
-                       ai->save = EM_SAFE_STRDUP(buf);
-
-                       attachment.attachment_path = ai->save;
-
-#ifdef __FEATURE_DRM__
-                       if (emcore_check_drm(&attachment)) /*  is drm content ?*/ {
-                               if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, attachment.attachment_path, NULL) 
-                                                                                              != DRM_RETURN_SUCCESS)
-                                       EM_DEBUG_EXCEPTION("drm_process_request : register file fail");
-                               mail->DRM_status = attachment.attachment_drm_type;
+                       if (!emstorage_delete_dir(move_buf, NULL)) {
+                               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
+                               /* goto FINISH_OFF; */
                        }
-#endif/*  __FEATURE_DRM__ */
-               }
-
-#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
-               /*  Information :  Attachment info already saved if partial body is dowloaded. */
-               if (ai->type)  {
-                       emstorage_attachment_tbl_t *attch_info = NULL;
-                       /*  Get attachment details  */
-                       if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info)  {
-                               EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
-                               if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) {   /*  save only attachment file. */
-                                       if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
-                                               EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
-                                               if (attch_info)
-                                                       emstorage_free_attachment(&attch_info, 1, NULL);
-                                               /*  delete all created files. */
-                                               if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
-                                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                                                       goto FINISH_OFF;
-                                               }
 
-                                               if (!emstorage_delete_dir(buf, &err))  {
-                                                       EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
-                                                       goto FINISH_OFF;
-                                               }
+                       goto FINISH_OFF;
+               }
 
-                                               /*  ROLLBACK TRANSACTION; */
-                                               emstorage_rollback_transaction(NULL, NULL, NULL);
+               EM_SAFE_FREE(ai->save);
+               ai->save = EM_SAFE_STRDUP(path_buf);
+               attachment.attachment_path = ai->save;
 
+               /*  Get attachment details  */
+               if (!emstorage_get_attachment_nth(multi_user_name, mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
 
-                                               goto FINISH_OFF;
-                                       }
-                               }
-                       }
-                       else {
-                               EM_DEBUG_LOG("Attachment info already exists...!");
-                               /* Update attachment size */
-                               EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
-                               attch_info->attachment_size = ai->size;
-                               if (!emstorage_update_attachment(attch_info, true, &err)) {
-                                       EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
-                                       emstorage_free_attachment(&attch_info, 1, NULL); /*prevent 17956*/
-                                       goto FINISH_OFF;
-                               }
-                       }
+                       if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) { /*  save only attachment file. */
+                               if (!emstorage_add_attachment(multi_user_name, &attachment, 0, false, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
 
-                       if (attch_info)
-                               emstorage_free_attachment(&attch_info, 1, NULL);
-               }
+                                       if (attch_info)
+                                               emstorage_free_attachment(&attch_info, 1, NULL);
 
-#else
-               
-               if (ai->type)  {
-                       mail->attachment_yn = 1;
-                       /*  save only attachment file. */
-                       if (!emstorage_add_attachment(&attachment, 0, false, &err))  {
-                               EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
-                               if (bIsAdd_to_mmc)  {
-                                       if (attachment.attachment) {
-                                       }
-                               }
-                               else  {
                                        /*  delete all created files. */
-                                       if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
+                                       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err))  {
                                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                                goto FINISH_OFF;
                                        }
 
-                                       if (!emstorage_delete_dir(buf, &err))  {
+                                       if (!emstorage_delete_dir(move_buf, &err)) {
                                                EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
                                                goto FINISH_OFF;
                                        }
 
                                        /*  ROLLBACK TRANSACTION; */
-                                       emstorage_rollback_transaction(NULL, NULL, NULL);
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+
                                        goto FINISH_OFF;
                                }
                        }
-           }
-#endif /*  End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
+               }
+               else {
+                       EM_DEBUG_LOG("Attachment info already exists...!");
+                       /* Update attachment size */
+                       EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
+                       EM_SAFE_FREE(attch_info->attachment_name);
+                       EM_SAFE_FREE(attch_info->attachment_path);
+                       EM_SAFE_FREE(attch_info->content_id);
+                       EM_SAFE_FREE(attch_info->attachment_mime_type);
+
+                       attch_info->attachment_size = ai->size;
+                       attch_info->attachment_save_status = attachment.attachment_save_status;
+                       attch_info->attachment_path = EM_SAFE_STRDUP(attachment.attachment_path);
+                       attch_info->content_id = EM_SAFE_STRDUP(attachment.content_id);
+                       attch_info->attachment_name = EM_SAFE_STRDUP(attachment.attachment_name);
+                       attch_info->attachment_drm_type = attachment.attachment_drm_type;
+                       attch_info->attachment_inline_content_status = 1;
+                       attch_info->attachment_mime_type = EM_SAFE_STRDUP(attachment.attachment_mime_type);
+
+                       if (!emstorage_update_attachment(multi_user_name, attch_info, true, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
+                               emstorage_free_attachment(&attch_info, 1, NULL); /*prevent 17956*/
+                               goto FINISH_OFF;
+                       }
+               }
+
+               if (attch_info)
+                       emstorage_free_attachment(&attch_info, 1, NULL);
 
+               attachment_num++;
+               ai = ai->next;
        }
 
        mail->attachment_count = local_attachment_count;
        mail->inline_content_count = local_inline_content_count;
 
-       /*  change mail's information. */
-       if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err))  {
+       /* change mail's information. */
+       if (!emstorage_change_mail_field(multi_user_name, mail_id, APPEND_BODY, mail, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
-
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
                goto FINISH_OFF;
        }
 
 #ifdef __FEATURE_BODY_SEARCH__
        /* strip html content and save into mail_text_tbl */
        char *stripped_text = NULL;
-       if (!emcore_strip_mail_body_from_file(mail, &stripped_text, &err) || stripped_text == NULL) {
+       if (!emcore_strip_mail_body_from_file(multi_user_name, mail, &stripped_text, &err) || stripped_text == NULL) {
                EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
        }
 
        emstorage_mail_text_tbl_t *mail_text;
-       if (!emstorage_get_mail_text_by_id(mail_id, &mail_text, true, &err) || !mail_text) {
+       if (!emstorage_get_mail_text_by_id(multi_user_name, mail_id, &mail_text, true, &err) || !mail_text) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_text_by_id failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
                goto FINISH_OFF;
        }
 
        EM_SAFE_FREE(mail_text->body_text);
        mail_text->body_text = stripped_text;
 
-       if (!emstorage_change_mail_text_field(mail_id, mail_text, false, &err)) {
+       if (!emstorage_change_mail_text_field(multi_user_name, mail_id, mail_text, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_change_mail_text_field failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
                emstorage_free_mail_text(&mail_text, 1, NULL); /*prevent 17957*/
                goto FINISH_OFF;
        }
@@ -3033,26 +4208,26 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk (void *mail_stream, i
                emstorage_free_mail_text(&mail_text, 1, NULL);
 #endif
 
-       EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
-       
+       EM_DEBUG_LOG_SEC("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
+
        /*  in pop3 mail case, the mail is deleted from server after being downloaded. */
-       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
+       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
 #ifdef DELETE_AFTER_DOWNLOADING
                char delmsg[24];
 
                SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
 
                if (!ref_account->keep_mails_on_pop_server_after_download)  {
-                       if (!emcore_delete_mails_from_pop3_server(&mbox, delmsg, &err))
+                       if (!emcore_delete_mails_from_pop3_server(multi_user_name, &mbox, delmsg, &err))
                                EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server failed [%d]", err);
                }
 #endif
-               
        }
 
-       FINISH_OFF_IF_CANCELED;
-       
-       ret = true;
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       ret = TRUE;
 
 FINISH_OFF:
 
@@ -3061,45 +4236,47 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
+#if 0
        /* note that local stream should be freed here*/
-       if (mail_stream == NULL && stream != NULL)  {
+       if (mail_stream == NULL && stream != NULL) {
                stream = mail_close (stream);
        }
+#endif
 
-       multi_part_body_size = 0;
        _pop3_received_body_size = 0;
        _pop3_last_notified_body_size = 0;
        _pop3_total_body_size = 0;
        _pop3_receiving_mail_id = 0;
-       
-       _imap4_received_body_size = 0;
-       _imap4_last_notified_body_size = 0;
-       _imap4_total_body_size = 0;
-       _imap4_download_noti_interval_value = 0;
-       
+
+       EM_SAFE_FREE(s_uid);
+
        if (cnt_info) {
                emcore_free_content_info(cnt_info);
                EM_SAFE_FREE(cnt_info);
        }
+
        if (mail)
                emstorage_free_mail(&mail, 1, NULL);
-       EM_SAFE_FREE(s_uid);
 
-       multi_part_body_size = 0;
-       
-       if (ret == true)
-               emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
-       else
-               emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
+       if (message1) {
+               g_object_unref(message1);
+               message1 = NULL;
+       }
+
+       if (!auto_download) {
+               if (ret == TRUE)
+                       emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
+               else
+                       emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
+       }
 
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 }
 
 
-
 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
                           unsigned long uidvalidity, SEARCHSET *sourceset,
                           SEARCHSET *destset)
@@ -3123,12 +4300,12 @@ void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
        sprintf(g_new_server_uid, "%ld", destset->first);
        EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
 
-       if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
+       if (!emstorage_update_server_uid(NULL, old_server_uid, g_new_server_uid, NULL)) {
                EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
        }
 }
 
-static int emcore_delete_mails_from_remote_server(int input_account_id, int input_mail_ids[], int input_mail_id_count, int input_delete_option)
+static int emcore_delete_mails_from_remote_server(char *multi_user_name, int input_account_id, int input_mail_ids[], int input_mail_id_count, int input_delete_option)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mail_ids[%p], input_mail_id_count[%d], input_delete_option [%d]", input_account_id, input_mail_ids, input_mail_id_count, input_delete_option);
 
@@ -3141,7 +4318,7 @@ static int emcore_delete_mails_from_remote_server(int input_account_id, int inpu
        int bulk_flag = false;
 #endif
 
-       if (!(account = emcore_get_account_reference(input_account_id)))  {
+       if (!(account = emcore_get_account_reference(multi_user_name, input_account_id)))  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -3151,8 +4328,8 @@ static int emcore_delete_mails_from_remote_server(int input_account_id, int inpu
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
                goto FINISH_OFF;
        }
-
-       FINISH_OFF_IF_CANCELED;
+       /* don't delete this comment, several threads including event thread call it */
+       /* FINISH_OFF_IF_CANCELED; */
 
        /* Sending Notification */
        noti_param_string = em_malloc(sizeof(char) * 10 * input_mail_id_count);
@@ -3174,20 +4351,39 @@ static int emcore_delete_mails_from_remote_server(int input_account_id, int inpu
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_DELETE_MAIL_START ] >>>> ");
 
        if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-               if (!bulk_flag && !emcore_delete_mails_from_imap4_server(input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
+               if (!bulk_flag && !emcore_delete_mails_from_imap4_server(multi_user_name, input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mails_from_imap4_server failed [%d]", err);
                        if (err == EMAIL_ERROR_IMAP4_STORE_FAILURE)
                                err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
                        goto FINISH_OFF;
                }
-               else
+               else {
                        bulk_flag = true;
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                       for(i = 0; i < input_mail_id_count; i++) {
+                               if (!emcore_delete_auto_download_activity(multi_user_name, input_account_id, input_mail_ids[i], 0, &err)) {
+                                       EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
+                               }
+                       }
+#endif
+               }
        }
        else if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
-               if (!emcore_delete_mails_from_pop3_server(account, input_mail_ids, input_mail_id_count))  {
+               if (!emcore_delete_mails_from_pop3_server(multi_user_name, account, input_mail_ids, input_mail_id_count))  {
                        EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server falied [%d]", err);
                        goto FINISH_OFF;
                }
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+               else {
+                       for(i = 0; i < input_mail_id_count; i++) {
+                               if (!emcore_delete_auto_download_activity(multi_user_name, input_account_id, input_mail_ids[i], 0, &err)) {
+                                       EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
+                               }
+                       }
+               }
+#endif
+
 #ifdef __FEATURE_LOCAL_ACTIVITY__
                else {
                        /* Remove local activity */
@@ -3211,7 +4407,7 @@ static int emcore_delete_mails_from_remote_server(int input_account_id, int inpu
                        }
 
                        /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
-                       if (!emstorage_remove_downloaded_mail(input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))  {
+                       if (!emstorage_remove_downloaded_mail(multi_user_name, input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))  {
                                EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
                        }
                }
@@ -3241,7 +4437,7 @@ FINISH_OFF:
        return err;
 }
 
-int emcore_delete_mail(int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
+int emcore_delete_mail(char *multi_user_name, int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num[%d], from_server[%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
 
@@ -3255,28 +4451,29 @@ int emcore_delete_mail(int account_id, int mail_ids[], int num, int from_server,
                goto FINISH_OFF;
        }
 
-       if (!(account = emcore_get_account_reference(account_id)))  {
+       if (!(account = emcore_get_account_reference(multi_user_name, account_id)))  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       FINISH_OFF_IF_CANCELED;
+       /* don't delete this comment, several threads including event thread call it */
+       /* FINISH_OFF_IF_CANCELED; */
 
        if (from_server == EMAIL_DELETE_LOCALLY) /* Delete mails from local storage*/ {
-               emcore_delete_mails_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
-               emcore_display_unread_in_badge();
+               emcore_delete_mails_from_local_storage(multi_user_name, account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
+               emcore_display_unread_in_badge(NULL);
        }
        else {   /* Delete mails from server*/
-               emcore_delete_mails_from_remote_server(account_id, mail_ids, num, from_server);
+               emcore_delete_mails_from_remote_server(multi_user_name, account_id, mail_ids, num, from_server);
        }
 
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (from_server)
-               emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0  :  err);
+               emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0  :  err);
 
        if (account) {
                emcore_free_account(account);
@@ -3336,22 +4533,23 @@ INTERNAL_FUNC void emcore_send_signal_for_del_account (int signal)
        pthread_mutex_unlock (&mu_del_account);
 }
 
-int emcore_delete_all_mails_of_acount(int input_account_id)
+int emcore_delete_all_mails_of_acount(char *multi_user_name, int input_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d]");
 
        int   err = EMAIL_ERROR_NONE;
-       char*  buf = NULL;
+       char* move_buf = NULL;
+    char path_buf[512] = {0};
 
        /* emstorage_delete_mail_by_account is available only locally */
-       if (!emstorage_delete_mail_by_account(input_account_id, false, &err))  {
+       if (!emstorage_delete_mail_by_account(multi_user_name, input_account_id, false, &err))  {
                if(err != EMAIL_ERROR_MAIL_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
-       if (!emstorage_delete_attachment_all_on_db(input_account_id, NULL, false, &err))  {
+       if (!emstorage_delete_attachment_all_on_db(multi_user_name, input_account_id, NULL, false, &err))  {
                if(err != EMAIL_ERROR_MAIL_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
                        goto FINISH_OFF;
@@ -3359,34 +4557,35 @@ int emcore_delete_all_mails_of_acount(int input_account_id)
        }
 
        /*  delete mail contents from filesystem */
-       buf = em_malloc (512);
-       if (!buf) {
+       move_buf = em_malloc (512);
+       if (!move_buf) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_save_name(input_account_id, 0, 0, NULL, buf, &err))  {
+       if (!emstorage_get_save_name(multi_user_name, input_account_id, 0, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!del_thd) 
+       if (!del_thd)
                del_thd = em_thread_create (del_exit, NULL);
 
-       em_thread_run (del_thd, del_dir, free_buf, buf);
+       em_thread_run (del_thd, del_dir, free_buf, move_buf);
 
        /*  delete meeting request */
-       if (!emstorage_delete_meeting_request(input_account_id, 0, 0, false, &err))  {
+       if (!emstorage_delete_meeting_request(multi_user_name, input_account_id, 0, 0, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:
+
        EM_DEBUG_FUNC_END("err [%d]",err);
        return err;
 }
 
-INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(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_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);
 
@@ -3406,17 +4605,17 @@ INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(int input_account_id, int i
 
        SNPRINTF(conditional_clause, QUERY_SIZE, " where mailbox_id = %d ", input_mailbox_id);
 
-       emstorage_query_mail_id_list(conditional_clause, false, &mail_id_array, &mail_id_count);
+       emstorage_query_mail_id_list(multi_user_name, conditional_clause, false, &mail_id_array, &mail_id_count);
 
        EM_DEBUG_LOG("emstorage_query_mail_id_list returns [%d]", mail_id_count);
 
        if (mail_id_count > 0) {
-               if (!emcore_delete_mail(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;
                }
        }
-       
+
        ret = true;
 
 FINISH_OFF:
@@ -3429,21 +4628,23 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
+INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name, int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
        int ret = false, err = EMAIL_ERROR_NONE, i;
        emstorage_mail_tbl_t *result_mail_list = NULL;
-       char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
+       char mail_id_string[10], *noti_param_string = NULL;
+    char move_buf[512] = {0, };
+    char path_buf[512] = {0,};
 
        /* Getting mail list by using select mail_id [in] */
-       if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
+       if(!emstorage_get_mail_field_by_multiple_mail_id(multi_user_name, mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Deleting mails by using select mail_id [in] */
-       if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
+       if(!emstorage_delete_multiple_mails(multi_user_name, mail_ids, num, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -3468,19 +4669,22 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *ma
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
 
        /* Updating Thread informations */
-       /* Thread information should be updated as soon as possible. */
        for(i = 0; i < num; i++) {
-               if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false,  &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
-                       goto FINISH_OFF;
+               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)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
                }
        }
+
+       /* Thread information should be updated as soon as possible. */
        if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
 
        for(i = 0; i < num; i++) {
                /* Deleting attachments */
-               if (!emstorage_delete_all_attachments_of_mail(result_mail_list[i].mail_id, false, &err)) {
+               if (!emstorage_delete_all_attachments_of_mail(multi_user_name, result_mail_list[i].mail_id, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_all_attachments_of_mail failed [%d]", err);
                        if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
                                err = EMAIL_ERROR_NONE;
@@ -3488,23 +4692,37 @@ INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *ma
 
                /* Deleting Directories */
                /*  delete mail contents from filesystem */
-               if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
+               if (!emstorage_get_save_name(multi_user_name, account_id, result_mail_list[i].mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_delete_dir(buf, &err)) {
+               if (!emstorage_delete_dir(move_buf, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
                }
-               
+
                /* Deleting Meeting Request */
-               if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, 0, false, &err)) {
+               if (!emstorage_delete_meeting_request(multi_user_name, account_id, result_mail_list[i].mail_id, 0, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
                        if (err != EMAIL_ERROR_CONTACT_NOT_FOUND) {
                                goto FINISH_OFF;
                        }
                }
+
+               /* Deleting mail_read_mail_uid_tbl */
+               if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, result_mail_list[i].server_mailbox_name, result_mail_list[i].server_mail_id, true, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       for(i = 0; i < num; i++) {
+               if (!emcore_delete_auto_download_activity(multi_user_name, account_id, mail_ids[i], 0, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
+               }
        }
+#endif
 
        ret = true;
 
@@ -3527,7 +4745,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count)
+static int emcore_delete_mails_from_pop3_server(char *multi_user_name, email_account_t *input_account, int input_mail_ids[], int input_mail_id_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_account[%p], input_mail_ids[%p], input_mail_id_count[%d]", input_account, input_mail_ids, input_mail_id_count);
 
@@ -3538,7 +4756,7 @@ static int emcore_delete_mails_from_pop3_server(email_account_t *input_account,
        void *stream = NULL;
        email_internal_mailbox_t mailbox_data = { 0, };
        emstorage_mail_tbl_t    *mail_tbl_data = NULL;
-       
+
        if (!input_account || !input_mail_ids) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -3546,13 +4764,13 @@ static int emcore_delete_mails_from_pop3_server(email_account_t *input_account,
        }
 
        for (i = 0; i < input_mail_id_count; i++)  {
-               FINISH_OFF_IF_CANCELED;
+               /*several threads calls this function. don't use this statement */
+               /* FINISH_OFF_IF_CANCELED; */
 
                mail_id = input_mail_ids[i];
 
-               if (!emstorage_get_downloaded_mail(mail_id, &mail_tbl_data, false, &err) || !mail_tbl_data)  {
+               if (!emstorage_get_downloaded_mail(multi_user_name, mail_id, &mail_tbl_data, false, &err) || !mail_tbl_data)  {
                        EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
-
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {        /* not server mail */
                                continue;
                        }
@@ -3561,7 +4779,7 @@ static int emcore_delete_mails_from_pop3_server(email_account_t *input_account,
                }
 
                if (stream == NULL)  {
-                       if (!emcore_connect_to_remote_mailbox(input_account->account_id, 0, (void **)&stream, &err))  {
+                       if (!emcore_connect_to_remote_mailbox(multi_user_name, input_account->account_id, 0, (void **)&stream, &err))  {
                                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -3586,13 +4804,13 @@ static int emcore_delete_mails_from_pop3_server(email_account_t *input_account,
 
                if (!pop3_mail_delete(mailbox_data.mail_stream, msgno, &err)) {
                        EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
-       
+
                        if (err == EMAIL_ERROR_POP3_DELE_FAILURE)
                                err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_remove_downloaded_mail(input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
+               if (!emstorage_remove_downloaded_mail(multi_user_name, input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
                        EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
 
 NOT_FOUND_ON_SERVER :
@@ -3601,7 +4819,7 @@ NOT_FOUND_ON_SERVER :
                        emstorage_free_mail(&mail_tbl_data, 1, NULL);
        }
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (mail_tbl_data != NULL)
                emstorage_free_mail(&mail_tbl_data, 1, NULL);
@@ -3625,17 +4843,17 @@ INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_i
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        emcore_uid_list *uid_list = NULL;
-       
+
        if (!account || !mailbox || !uid || !msgno)  {
                EM_DEBUG_EXCEPTION_SEC("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        uid_list = mailbox->user_data;
-       
+
        if (uid_list == NULL)  {
                if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
                        if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
@@ -3644,7 +4862,7 @@ INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_i
                        }
                }
                else  {         /*  EMAIL_SERVER_TYPE_IMAP4 */
-                       if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
+                       if (!imap4_mailbox_get_uids(mailbox->mail_stream, NULL, &uid_list, &err))  {
                                EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -3662,10 +4880,10 @@ INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_i
                EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
                uid_list = uid_list->next;
        }
-       
+
        err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (err_code != NULL)
                *err_code = err;
 
@@ -3679,7 +4897,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int input_mailbox_id)
+INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(char *multi_user_name, int input_mailbox_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d]", input_mailbox_id);
        int   err = EMAIL_ERROR_NONE;
@@ -3696,7 +4914,7 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int in
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -3724,19 +4942,19 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int in
        filter_list[2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
        filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
 
-       if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_mail_id_list(multi_user_name, conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mails_from_local_storage(mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
+       if (!emcore_delete_mails_from_local_storage(multi_user_name, mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage [%d]", err);
                goto FINISH_OFF;
        }
@@ -3756,7 +4974,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int input_account_id, int input_mailbox_id)
+INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *multi_user_name, int input_account_id, int input_mailbox_id)
 {
        int   err = EMAIL_ERROR_NONE;
        char *conditional_clause_string = NULL;
@@ -3794,19 +5012,19 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int in
        filter_list[2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
        filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
 
-       if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_mail_id_list(multi_user_name, conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mail(input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
+       if (!emcore_delete_mail(multi_user_name, input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail [%d]", err);
                goto FINISH_OFF;
        }
@@ -3824,42 +5042,40 @@ FINISH_OFF:
        return err;
 }
 
-/* description 
+/* description
  *    add a attachment to mail.
- * arguments  
+ * arguments
  *    mailbox  :  mail box
  *    mail_id  :  mail id
  *    attachment  :  attachment to be added
- * return  
+ * return
  *    succeed  :  1
  *    fail  :  0
  */
-INTERNAL_FUNC int emcore_add_attachment(int mail_id, email_attachment_data_t *attachment, int *err_code)
+INTERNAL_FUNC int emcore_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t *attachment, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
-       
+
        if (attachment == NULL)  {
                EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
                if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        int ret = false, err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t *mail_table_data = NULL;
        int attachment_id = 0;
        int before_tr_begin = 0;
 
-       
-       if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
+       if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
-
                goto FINISH_OFF2;
        }
-       
+
        int account_id = mail_table_data->account_id;
        emstorage_attachment_tbl_t attachment_tbl;
-       
+
        memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
 
        mail_table_data->attachment_count               = mail_table_data->attachment_count + 1;
@@ -3872,97 +5088,96 @@ INTERNAL_FUNC int emcore_add_attachment(int mail_id, email_attachment_data_t *at
        attachment_tbl.attachment_drm_type              = attachment->drm_status;
        attachment_tbl.attachment_inline_content_status = attachment->inline_content_status;
        attachment_tbl.attachment_mime_type             = attachment->attachment_mime_type;
+       attachment_tbl.content_id                       = attachment->content_id;
 
        /*  BEGIN TRANSACTION; */
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
        }
 
-       if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
+       if (!emstorage_add_attachment(multi_user_name, &attachment_tbl, 0, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
-
                goto FINISH_OFF;
        }
-       
+
        attachment->attachment_id = attachment_tbl.attachment_id;
 
        if (attachment->attachment_path)  {
-               char buf[512];
+               char move_buf[512] = {0};
+               char path_buf[512] = {0};
 
                if (!attachment->inline_content_status) {
-                       if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err))  {
+                       if (!emstorage_create_dir(multi_user_name, account_id, mail_id, attachment_tbl.attachment_id, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                                goto FINISH_OFF;
                        }
                        attachment_id = attachment_tbl.attachment_id;
                }
 
-               if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->attachment_name, buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, attachment_id, attachment->attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
-        attachment_tbl.attachment_path = buf;
 
-               if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
-                       EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
+           attachment_tbl.attachment_path = path_buf;
 
+               if (!emstorage_change_attachment_field(multi_user_name, mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
+               if (!emstorage_change_mail_field(multi_user_name, mail_id, APPEND_BODY, mail_table_data, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
 
                if (attachment->save_status) {
-                       if (!emstorage_move_file(attachment->attachment_path, buf, false, &err))  {
+                       if (!emstorage_move_file(attachment->attachment_path, move_buf, false, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
-
                                goto FINISH_OFF;
                        }
                }
 
                /* Here only filename is being updated. Since first add is being done there will not be any old files.
                    So no need to check for old files in this update case */
-               if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
+               if (!emstorage_change_attachment_field(multi_user_name, mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
 
                EM_SAFE_FREE(attachment->attachment_path);
-               attachment->attachment_path = EM_SAFE_STRDUP(buf);
+               attachment->attachment_path = EM_SAFE_STRDUP(path_buf);
        }
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (ret == true) {      /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                        ret = false;
                }
        }
        else {  /*  ROLLBACK TRANSACTION; */
-               if (!before_tr_begin && emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
        }
 
-FINISH_OFF2: 
+FINISH_OFF2:
        if (mail_table_data != NULL)
                emstorage_free_mail(&mail_table_data, 1, NULL);
 
        if (err_code)
                *err_code = err;
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return ret;
 }
 
 
-INTERNAL_FUNC int emcore_add_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
+INTERNAL_FUNC int emcore_add_attachment_data(char *multi_user_name, int input_mail_id, email_attachment_data_t *input_attachment_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
 
@@ -3970,16 +5185,18 @@ INTERNAL_FUNC int emcore_add_attachment_data(int input_mail_id, email_attachment
        int attachment_id = 0;
        int ret = false;
        int before_tr_begin = 0;
-       char buf[512] = { 0, };
+       char move_buf[512] = { 0, };
+       char path_buf[512] = { 0, };
+
        emstorage_mail_tbl_t *mail_table_data = NULL;
        emstorage_attachment_tbl_t attachment_tbl  = { 0 };
-       
+
        if (input_attachment_data == NULL)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
+       if (!emstorage_get_mail_field_by_id(multi_user_name, input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
@@ -3995,84 +5212,81 @@ INTERNAL_FUNC int emcore_add_attachment_data(int input_mail_id, email_attachment
        attachment_tbl.attachment_drm_type              = input_attachment_data->drm_status;
        attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
        attachment_tbl.attachment_mime_type             = input_attachment_data->attachment_mime_type;
-       
+       attachment_tbl.content_id                       = input_attachment_data->content_id;
+
        /*  BEGIN TRANSACTION; */
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
        }
 
-       if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
+       if (!emstorage_add_attachment(multi_user_name, &attachment_tbl, 0, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
                goto FINISH_OFF;
        }
-       
+
        input_attachment_data->attachment_id = attachment_tbl.attachment_id;
 
        if (input_attachment_data->attachment_path)  {
                if (!input_attachment_data->inline_content_status) {
-                       if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
+                       if (!emstorage_create_dir(multi_user_name, mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                                goto FINISH_OFF;
                        }
                        attachment_id = attachment_tbl.attachment_id;
                }
 
-               if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               attachment_tbl.attachment_path = buf;
+               attachment_tbl.attachment_path = path_buf;
 
-               if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
+               if (!emstorage_change_attachment_field(multi_user_name, input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
-
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
+               if (!emstorage_change_mail_field(multi_user_name, input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
 
                if (input_attachment_data->save_status) {
-                       if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err))  {
+                       if (!emstorage_copy_file(input_attachment_data->attachment_path, move_buf, false, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
-
                                goto FINISH_OFF;
                        }
                }
-               
+
                /* Here only filename is being updated. Since first add is being done there will not be any old files.
                        So no need to check for old files in this update case */
-               if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
+               if (!emstorage_change_attachment_field(multi_user_name, input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
-               
+
                EM_SAFE_FREE(input_attachment_data->attachment_path);
-               input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
+               input_attachment_data->attachment_path = EM_SAFE_STRDUP(path_buf);
        }
-       
+
        ret = true;
 
 FINISH_OFF:
        if (ret) { /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                }
        } else { /*  ROLLBACK TRANSACTION; */
-               if (!before_tr_begin && emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
        }
-       
+
        if (mail_table_data != NULL)
                emstorage_free_mail(&mail_table_data, 1, NULL);
-       
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
@@ -4089,10 +5303,10 @@ FINISH_OFF:
  *    succeed  :  1
  *    fail  :  0
  */
-int emcore_delete_mail_attachment(int attachment_id, int *err_code)
+int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
-       
+
        if (attachment_id == 0)  {
                EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
                if (err_code != NULL)
@@ -4104,35 +5318,40 @@ int emcore_delete_mail_attachment(int attachment_id, int *err_code)
        int error = EMAIL_ERROR_NONE;
        char attachment_folder_path[MAX_PATH] = {0, };
        emstorage_attachment_tbl_t *attachment_tbl = NULL;
-       
-       if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &error)) {
+
+       if (!emstorage_get_attachment(multi_user_name, attachment_id, &attachment_tbl, true, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment failed");
                return false;
        }
 
        /*  BEGIN TRANSACTION; */
-       if(!emstorage_begin_transaction(NULL, NULL, &error)) {
+       if(!emstorage_begin_transaction(multi_user_name, NULL, NULL, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", error);
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_delete_attachment_on_db(attachment_id, false, &error))  {
-               EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
 
-               if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+       if (!emstorage_delete_attachment_on_db(multi_user_name, attachment_id, false, &error))  {
+               EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
+               if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        error = EMAIL_ERROR_DB_FAILURE;
 
                ret = false;
                goto FINISH_OFF;
        }
 
-       SNPRINTF(attachment_folder_path, sizeof(attachment_folder_path), "%s/%d/%d/%d", MAILHOME, attachment_tbl->account_id, attachment_tbl->mail_id, attachment_id);
+       if (attachment_tbl->attachment_inline_content_status != INLINE_ATTACHMENT) {
+               SNPRINTF(attachment_folder_path, sizeof(attachment_folder_path), "%s/%d/%d/%d", MAILHOME, attachment_tbl->account_id, attachment_tbl->mail_id, attachment_id);
 
-       if (!emstorage_delete_dir(attachment_folder_path, NULL)) {
-               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
+               if (!emstorage_delete_dir(attachment_folder_path, NULL)) {
+                       EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
+               }
+       } else {
+               if (!emstorage_delete_file(attachment_tbl->attachment_path, NULL)) {
+                       EM_DEBUG_EXCEPTION("emstorage_delete_file failed");
+               }
        }
 
-       if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+       if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                error = EMAIL_ERROR_DB_FAILURE;
                ret = false;
        }
@@ -4148,13 +5367,14 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_mail_update_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
+static int emcore_mail_update_attachment_data(char *multi_user_name, int input_mail_id, email_attachment_data_t *input_attachment_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
 
        int err = EMAIL_ERROR_NONE;
        int attachment_id = 0;
-       char buf[512] = { 0 , };
+       char move_buf[512] = { 0 , };
+       char path_buf[512] = {0,};
        emstorage_attachment_tbl_t *existing_attachment_info = NULL;
        emstorage_attachment_tbl_t  attachment_tbl = { 0 };
 
@@ -4164,12 +5384,11 @@ static int emcore_mail_update_attachment_data(int input_mail_id, email_attachmen
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_attachment(input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
+       if (!emstorage_get_attachment(multi_user_name, input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
-
                goto FINISH_OFF;
        }
-       
+
        attachment_tbl.mail_id                          = input_mail_id;
        attachment_tbl.account_id                       = existing_attachment_info->account_id;
        attachment_tbl.mailbox_id                       = existing_attachment_info->mailbox_id;
@@ -4181,27 +5400,27 @@ static int emcore_mail_update_attachment_data(int input_mail_id, email_attachmen
        attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
        attachment_tbl.attachment_mime_type             = input_attachment_data->attachment_mime_type;
        attachment_tbl.attachment_id                    = input_attachment_data->attachment_id;
+       attachment_tbl.content_id                       = input_attachment_data->content_id;
 
        if (!input_attachment_data->inline_content_status) {
-               if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
+               if (!emstorage_create_dir(multi_user_name, attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
                attachment_id = attachment_tbl.attachment_id;
        }
        
-       if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
+       if (!emstorage_get_save_name(multi_user_name, attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       attachment_tbl.attachment_path = buf;
+       attachment_tbl.attachment_path = path_buf;
 
-       EM_DEBUG_LOG("downloaded [%d], savename [%s], attachment_path [%s]", input_attachment_data->save_status, input_attachment_data->attachment_path, existing_attachment_info->attachment_path);
+       EM_DEBUG_LOG_SEC("downloaded [%d], savename [%s], attachment_path [%s]", input_attachment_data->save_status, input_attachment_data->attachment_path, existing_attachment_info->attachment_path);
        if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
-               if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err))  {
+               if (!emstorage_move_file(input_attachment_data->attachment_path, move_buf, false ,&err))  {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
-
                        goto FINISH_OFF;
                }
        }
@@ -4209,23 +5428,23 @@ static int emcore_mail_update_attachment_data(int input_mail_id, email_attachmen
                EM_DEBUG_LOG("no need to move");
 
        EM_SAFE_FREE(input_attachment_data->attachment_path);
-       input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
+       input_attachment_data->attachment_path = EM_SAFE_STRDUP(path_buf);
 
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_update_attachment(&attachment_tbl, false, &err))  {
+       if (!emstorage_update_attachment(multi_user_name, &attachment_tbl, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
        }
-       
+
        if (err == EMAIL_ERROR_NONE) {  /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                }
        } else {        /*  ROLLBACK TRANSACTION; */
-               if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
        }
 
@@ -4238,7 +5457,7 @@ FINISH_OFF:
 }
 
 
-static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, int input_attachment_a_id, email_attachment_data_t *input_attachment_b_data, int *result)
+static int emcore_mail_compare_filename_of_attachment_data(char *multi_user_name, int input_mail_id, int input_attachment_a_id, email_attachment_data_t *input_attachment_b_data, int *result)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_a_id[%d], input_attachment_b_data[%p], result[%p]", input_mail_id, input_attachment_a_id, input_attachment_b_data, result);
 
@@ -4247,9 +5466,38 @@ static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, in
        }
 
        int err = EMAIL_ERROR_NONE;
+       ssize_t ret_readlink;
+       char *linkpath = NULL;
+       struct stat st_buf;
+
        emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
 
-       if (!emstorage_get_attachment(input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
+       if (input_attachment_b_data->attachment_path && (stat(input_attachment_b_data->attachment_path, &st_buf) == 0)) {
+               linkpath = em_malloc(st_buf.st_size + 1);
+               if (linkpath == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       goto FINISH_OFF;
+               }
+
+               ret_readlink = readlink(input_attachment_b_data->attachment_path, linkpath, st_buf.st_size + 1);
+               if (ret_readlink > 0) {
+                       linkpath[st_buf.st_size] = '\0';
+                       EM_DEBUG_LOG("symbolic link path : [%s]", linkpath);
+
+                       if (emstorage_get_attachment_by_attachment_path(multi_user_name, linkpath, &attachment_a_tbl, false, &err)) {
+                               if (attachment_a_tbl->mail_id == input_mail_id) {
+                                       input_attachment_b_data->attachment_id = attachment_a_tbl->attachment_id;
+                                       *result = 2;
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       if (attachment_a_tbl)
+                               emstorage_free_attachment(&attachment_a_tbl, 1, NULL);
+               }
+       }
+
+       if (!emstorage_get_attachment(multi_user_name, input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
                if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
                        EM_DEBUG_LOG ("no attachment found");
                else
@@ -4260,95 +5508,115 @@ static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, in
                goto FINISH_OFF;
        }
 
-       if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
-               EM_DEBUG_LOG_SEC("attachment_a_tbl->attachment_name [%s], input_attachment_b_data->name [%s]", attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
-               *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
+       if (attachment_a_tbl->attachment_path && input_attachment_b_data->attachment_path) {
+               EM_DEBUG_LOG_SEC("attachment_a_tbl->attachment_path [%s], input_attachment_b_data->attachment_path [%s]", attachment_a_tbl->attachment_path, input_attachment_b_data->attachment_path);
+               if (strcmp(attachment_a_tbl->attachment_path, input_attachment_b_data->attachment_path) == 0)
+                       *result = 0;
+               else
+                       *result = 1;
        }
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (attachment_a_tbl)
                emstorage_free_attachment(&attachment_a_tbl, 1, NULL);
+
+       EM_SAFE_FREE(linkpath);
+
        EM_DEBUG_FUNC_END("*result [%d]", *result);
        return err;
 }
 
 
-/* description 
+/* description
  *    copy a mail to mail box
- * arguments  
+ * arguments
  *    src_mailbox  :  source mail box
  *    msgno  :  mail sequence
  *    dst_mailbox  :  target mail box
- * return  
+ * return
  *    succeed  :  1
  *    fail  :  0
  */
-INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, int *err_code)
+INTERNAL_FUNC int emcore_mail_copy(char *multi_user_name, int mail_id, email_mailbox_t *dst_mailbox, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int i;
        emstorage_mail_tbl_t *mail = NULL;
        emstorage_attachment_tbl_t *atch_list = NULL;
-       char buf[512];
        int count = EMAIL_ATTACHMENT_MAX_COUNT;
        char *mailbox_name = NULL;
        char *stripped_text = NULL;
+    char *prefix_path = NULL;
+       char virtual_path[512];
+    char real_path[512];
+    char real_file_path[512];
 
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
 
                goto FINISH_OFF;
        }
 
-       if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMAIL_ERROR_NONE ){
+       if ( (err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &atch_list, &count)) != EMAIL_ERROR_NONE ){
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
        /*  get increased uid. */
-       if (!emstorage_increase_mail_id(&mail->mail_id, true, &err))  {
+       if (!emstorage_increase_mail_id(multi_user_name, &mail->mail_id, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        /*  copy mail body(text) file */
        if (mail->file_path_plain)  {
-               if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
+               if (!emstorage_create_dir(multi_user_name, dst_mailbox->account_id, mail->mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-
                        goto FINISH_OFF;
                }
 
                gchar *filename = g_path_get_basename(mail->file_path_plain);
 
-               if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
+        memset(virtual_path, 0x00, sizeof(virtual_path));
+        memset(real_path, 0x00, sizeof(virtual_path));
+
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_plain);
+
+               if (!emstorage_get_save_name(multi_user_name, dst_mailbox->account_id, mail->mail_id, 0, filename, real_path, virtual_path, sizeof(virtual_path), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        g_free(filename);
-
                        goto FINISH_OFF;
                }
 
                g_free(filename);
 
-               if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err))  {
+               if (!emstorage_copy_file(real_file_path, real_path, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
-
                        goto FINISH_OFF;
                }
 
                EM_SAFE_FREE(mail->file_path_plain);
-               mail->file_path_plain = EM_SAFE_STRDUP(buf);
+               mail->file_path_plain = EM_SAFE_STRDUP(virtual_path);
        }
 
        /*  copy mail body(html) file */
        if (mail->file_path_html)  {
-               if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
+               if (!emstorage_create_dir(multi_user_name, dst_mailbox->account_id, mail->mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
                        goto FINISH_OFF;
@@ -4356,58 +5624,76 @@ INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, in
 
                gchar *filename = g_path_get_basename(mail->file_path_html);
 
-               if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
+        memset(virtual_path, 0x00, sizeof(virtual_path));
+        memset(real_path, 0x00, sizeof(virtual_path));
+
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_html);
+
+               if (!emstorage_get_save_name(multi_user_name, 
+                                    dst_mailbox->account_id, 
+                                    mail->mail_id, 
+                                    0, 
+                                    filename, 
+                                    real_path, 
+                                    virtual_path,
+                                    sizeof(virtual_path), 
+                                    &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        g_free(filename);
-
                        goto FINISH_OFF;
                }
 
                g_free(filename);
 
-               if (!emstorage_copy_file(mail->file_path_html, buf, false, &err))  {
+               if (!emstorage_copy_file(real_file_path, real_path, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
-       
                        goto FINISH_OFF;
                }
 
                EM_SAFE_FREE(mail->file_path_html); /*valgrind*/
-               mail->file_path_html = EM_SAFE_STRDUP(buf);
+               mail->file_path_html = EM_SAFE_STRDUP(virtual_path);
        }
 
        /*  BEGIN TRANSACTION; */
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /*  insert mail data */
-       
+
        mail->account_id   = dst_mailbox->account_id;
        mail->mailbox_id   = dst_mailbox->mailbox_id;
        mail->mailbox_type = dst_mailbox->mailbox_type;
-       
-       if (!emstorage_add_mail(mail, 0, false, &err))  {
+
+       if (!emstorage_add_mail(multi_user_name, mail, 0, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
 
                if (mail->file_path_plain)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_plain);
+
                        if (!emstorage_delete_file(mail->file_path_plain, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
+                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                goto FINISH_OFF;
                        }
                }
+
                if (mail->file_path_html) {
-                       if (!emstorage_delete_file(mail->file_path_html, &err))  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_html);
+
+                       if (!emstorage_delete_file(real_file_path, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
+                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                goto FINISH_OFF;
                        }
                }
 
                /*  ROLLBACK TRANSACTION; */
-               emstorage_rollback_transaction(NULL, NULL, NULL);
-
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
 
                goto FINISH_OFF;
        }
@@ -4415,33 +5701,45 @@ INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, in
        /*  copy attachment file */
        for (i = 0; i<count; i++)  {
                if (atch_list[i].attachment_path)  {
-                       if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err))  {
+                       if (!emstorage_create_dir(multi_user_name, dst_mailbox->account_id, mail->mail_id, i+1, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                                break;
                        }
-                       
-                       if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err))  {
+
+            memset(virtual_path, 0x00, sizeof(virtual_path));
+            memset(real_path, 0x00, sizeof(virtual_path));
+
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, atch_list[i].attachment_path);
+
+                       if (!emstorage_get_save_name(multi_user_name, 
+                                        dst_mailbox->account_id, 
+                                        mail->mail_id, 
+                                        i+1, 
+                                        atch_list[i].attachment_name, 
+                                        real_path, 
+                                        virtual_path, 
+                                        sizeof(virtual_path), 
+                                        &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                break;
                        }
 
-                       if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err))  {
+                       if (!emstorage_copy_file(real_file_path, real_path, false, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
                                break;
                        }
 
                        EM_SAFE_FREE(atch_list[i].attachment_path);
-
-                       atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
+                       atch_list[i].attachment_path = EM_SAFE_STRDUP(virtual_path);
                }
 
                atch_list[i].account_id = dst_mailbox->account_id;
                atch_list[i].mail_id = mail->mail_id;
                atch_list[i].mailbox_id = mail->mailbox_id;
 
-               if (!emstorage_add_attachment(&atch_list[i], 0, false, &err))  {
+               if (!emstorage_add_attachment(multi_user_name, &atch_list[i], 0, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
-
                        break;
                }
        }
@@ -4450,77 +5748,86 @@ INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, in
        if (i && i != count)  {
                for (;i >= 0; i--)  {
                        if (atch_list[i].attachment_path)  {
-                       
-                               if (!emstorage_delete_file(atch_list[i].attachment_path, &err))  {
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, atch_list[i].attachment_path);
+
+                               if (!emstorage_delete_file(real_file_path, &err))  {
                                        EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
-                                       emstorage_rollback_transaction(NULL, NULL, NULL);
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                        goto FINISH_OFF;
                                }
                        }
                }
 
                if (mail->file_path_plain)  {
-                       if (!emstorage_delete_file(mail->file_path_plain, &err))  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_plain);
+
+                       if (!emstorage_delete_file(real_file_path, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
+                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                goto FINISH_OFF;
                        }
                }
-               if (mail->file_path_html)  {
-                       if (!emstorage_delete_file(mail->file_path_html, &err))  {
+
+        if (mail->file_path_html)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail->file_path_html);
+
+                       if (!emstorage_delete_file(real_file_path, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
+                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                goto FINISH_OFF;
                        }
                }
 
                /*  ROLLBACK TRANSACTION; */
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
 #ifdef __FEATURE_BODY_SEARCH__
        /* Insert mail_text to DB */
        emstorage_mailbox_tbl_t *output_mailbox;
-       if (!emcore_strip_mail_body_from_file(mail, &stripped_text, &err) || stripped_text == NULL) {
+       if (!emcore_strip_mail_body_from_file(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_mailbox_by_id(dst_mailbox->mailbox_id, &output_mailbox) != EMAIL_ERROR_NONE) {
+       if (emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox->mailbox_id, &output_mailbox) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed");
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
-       if (!emcore_add_mail_text(output_mailbox, mail, stripped_text, &err)) {
+       if (!emcore_add_mail_text(multi_user_name, output_mailbox, mail, stripped_text, &err)) {
                EM_DEBUG_EXCEPTION("emcore_add_mail_text failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
        if (output_mailbox != NULL)
                emstorage_free_mailbox(&output_mailbox, 1, NULL);
-       
+
 #endif
 
-       if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+       if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                err = EMAIL_ERROR_DB_FAILURE;
-               if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mailbox_name_by_mailbox_type(dst_mailbox->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err))  {
+       if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, dst_mailbox->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
 
        if (!strcmp(dst_mailbox->mailbox_name, mailbox_name) && !(mail->flags_seen_field))
-               emcore_display_unread_in_badge();
+               emcore_display_unread_in_badge(NULL);
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (atch_list != NULL)
                emstorage_free_attachment(&atch_list, count, NULL);
 
@@ -4531,6 +5838,7 @@ FINISH_OFF:
        EM_SAFE_FREE(stripped_text);
 #endif
        EM_SAFE_FREE(mailbox_name);
+       EM_SAFE_FREE(prefix_path);
 
        if (err_code != NULL)
                *err_code = err;
@@ -4548,35 +5856,94 @@ FINISH_OFF:
  *    succeed  :  1
  *    fail  :  0
  */
-INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int mail_ids_count, int dst_mailbox_id, int noti_param_1, int noti_param_2 ,int *err_code)
+INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int mail_ids_count, int dst_mailbox_id, int noti_param_1, int noti_param_2 ,int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], mail_ids_count[%d], dst_mailbox_id[%d], noti_param [%d], err_code[%p]", mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, err_code);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t *mail_list = NULL;
+       emstorage_mail_tbl_t *p_mail_data = NULL;
        int account_id = 0;
-       int i = 0, parameter_string_length = 0;
+       int i = 0, j = 0, parameter_string_length = 0;
+       int p_thread_id = 0;
+       int p_thread_item_count = 0;
+       int p_lastest_mail_id = 0;
        char *parameter_string = NULL, mail_id_string[10];
 
-       if ( dst_mailbox_id <= 0  && mail_ids_count < 1) {
+       int *dest_prev_thread_id_list = NULL;
+       int dest_prev_thread_id = 0;
+       int dest_prev_thread_item_count = 0;
+       int find_striped_subject = 0;
+       char stripped_subject[4086];
+
+       if (dst_mailbox_id <= 0  && mail_ids_count < 1) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
+       if (!emstorage_get_mail_field_by_multiple_mail_id(multi_user_name, mail_ids, mail_ids_count, RETRIEVE_SUMMARY, &mail_list, true, &err) || !mail_list) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        account_id = mail_list[0].account_id;
 
-       if(!emstorage_move_multiple_mails_on_db(account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
+       dest_prev_thread_id_list = em_malloc(sizeof(int) * mail_ids_count);
+       if (dest_prev_thread_id_list == NULL) {
+               EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       for (i = 0; i < mail_ids_count; i++) {
+               /* Get the thread_id before move */
+               if (emstorage_get_thread_id_from_mailbox(multi_user_name, account_id, dst_mailbox_id, mail_list[i].subject, &dest_prev_thread_id, &dest_prev_thread_item_count) != EMAIL_ERROR_NONE)
+                       EM_DEBUG_LOG("emstorage_get_thread_id_of_thread_mails is failed.");
+
+               dest_prev_thread_id_list[i] = dest_prev_thread_id;
+       }
+
+       if(!emstorage_move_multiple_mails_on_db(multi_user_name, account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails_on_db failed [%d]", err);
                goto FINISH_OFF;
        }
        
+       for (i = 0; i < mail_ids_count; i++) {
+               if (mail_list[i].subject == NULL) 
+                       continue;
+               if (dest_prev_thread_id_list[i] == -1) {
+                       if (em_find_pos_stripped_subject_for_thread_view(mail_list[i].subject, stripped_subject, sizeof(stripped_subject)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
+                       } else {
+                               EM_DEBUG_LOG_SEC("subject: [%s]", mail_list[i].subject);
+                               if (EM_SAFE_STRLEN(stripped_subject) >= 2) {
+                                       find_striped_subject = 1;
+                               }
+                               EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
+                       }
+
+                       if (find_striped_subject) {
+                               for (j = 0; j < i; j++) {
+                                       if (g_strrstr(mail_list[j].subject, stripped_subject)) {
+                                               dest_prev_thread_id_list[i] = mail_ids[j];
+                                               break;
+                                       }
+                               }
+                               if (j == i)
+                                       dest_prev_thread_id_list[i] = mail_ids[i];
+                       } else {
+                               dest_prev_thread_id_list[i] = mail_ids[i];
+                       }
+               }
+
+               if (!emstorage_update_thread_id_of_mail(multi_user_name, account_id, dst_mailbox_id, mail_ids[i], dest_prev_thread_id_list[i], 0, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+
        /* Sending a notification */
        parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
        parameter_string = em_malloc(parameter_string_length);
@@ -4595,7 +5962,7 @@ INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int mail_ids_count, int dst_m
                SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
                strcat(parameter_string, mail_id_string);
        }
-       
+
        EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
 
        if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
@@ -4603,14 +5970,47 @@ INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int mail_ids_count, int dst_m
 
 
        for (i = 0; i < mail_ids_count; i++) {
-               if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, false, &err))
+               if (mail_list[i].subject == NULL)
+                       continue;
+               p_thread_id = -1;
+               p_thread_item_count = 0;
+               p_lastest_mail_id = -1;
+
+               /* Get the information of moved mail */
+               if (!emstorage_get_mail_by_id(multi_user_name, mail_ids[i], &p_mail_data, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               /* Get the thread_id of moved mail */   
+               if (emstorage_get_thread_id_of_thread_mails(multi_user_name, p_mail_data, &p_thread_id, &p_lastest_mail_id, &p_thread_item_count) != EMAIL_ERROR_NONE)
+                       EM_DEBUG_LOG("emstorage_get_thread_id_of_thread_mails is failed.");
+
+               /* Original mailbox replace thread id */
+               if (!emstorage_update_latest_thread_mail(multi_user_name, account_id, mail_list[i].mailbox_id, 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)) {
+                               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)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
+               }
+               emstorage_free_mail(&p_mail_data, 1, NULL);
        }
 
        if (!emcore_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
                EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
 
-       emcore_display_unread_in_badge();
+       emcore_display_unread_in_badge(NULL);
 
        ret = true;
 
@@ -4620,9 +6020,11 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_MOVE_FAIL ] >>>> ");
        }
 
+       emstorage_free_mail(&p_mail_data, 1, NULL);
        emstorage_free_mail(&mail_list, mail_ids_count, NULL);
 
        EM_SAFE_FREE(parameter_string);
+       EM_SAFE_FREE(dest_prev_thread_id_list); /*prevent 38972*/
 
        if (err_code != NULL)
                *err_code = err;
@@ -4631,7 +6033,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,  int mail_ids[], int num, char *dest_mailbox, int *error_code)
+INTERNAL_FUNC int emcore_move_mail_on_server(char *multi_user_name, int account_id, int src_mailbox_id,  int mail_ids[], int num, char *dest_mailbox, int *error_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        MAILSTREAM *stream = NULL;
@@ -4641,10 +6043,10 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
        int ret = 1;
        int mail_id = 0;
        int i = 0;
-       
+
        mail_id = mail_ids[0];
        
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed  :  get account reference[%d]", account_id);
                *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -4662,7 +6064,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
        for (i = 0; i < num; i++)  {
                mail_id = mail_ids[i];
 
-               if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail)  {
+               if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, false, &err_code) || !mail)  {
                        EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id  :  emstorage_get_downloaded_mail failed [%d]", err_code);
                        mail = NULL;
                        if (err_code == EMAIL_ERROR_MAIL_NOT_FOUND)  {  /*  not server mail */
@@ -4677,7 +6079,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
                        goto FINISH_OFF;
                }
 
-               if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code))         /* faizan.h@samsung.com mail_move_fix_07042009 */ {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, src_mailbox_id, (void **)&stream, &err_code))                /* faizan.h@samsung.com mail_move_fix_07042009 */ {
                        EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err_code);
 
                        ret = 0;
@@ -4689,7 +6091,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
                        mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
 
                        EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
-                       
+
                        if (mail->server_mail_id) {
                                if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
                                        EM_DEBUG_EXCEPTION("emcore_move_mail_on_server :   Mail cannot be moved failed");
@@ -4697,7 +6099,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
                                }
                                else {
                                        /*  send EXPUNGE command */
-                                       if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
+                                       if (!emcore_imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
                                                EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
 
                                                if (err_code == EMAIL_ERROR_IMAP4_STORE_FAILURE)
@@ -4705,7 +6107,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
                                                goto FINISH_OFF;
                                        }
 
-                                       if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->server_mailbox_name, &err_code)) {
+                                       if (!emstorage_update_read_mail_uid(multi_user_name, mail_id, g_new_server_uid, mail->server_mailbox_name, &err_code)) {
                                                EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
                                                goto FINISH_OFF;
                                        }
@@ -4725,7 +6127,7 @@ INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,
        }
 
 FINISH_OFF:
-       if (stream) 
+       if (stream)
                stream = mail_close (stream);
 
        if (ref_account) {
@@ -4739,8 +6141,44 @@ FINISH_OFF:
        return ret;
 }
 
+INTERNAL_FUNC int emcore_move_mail_on_server_by_server_mail_id(void *mail_stream, char *server_mail_id, char *dest_mailbox_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err_code = EMAIL_ERROR_NONE;
+
+       if (mail_stream == NULL || server_mail_id == 0 || dest_mailbox_name == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err_code = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       /* set callback for COPY_UID */
+       mail_parameters((MAILSTREAM*)mail_stream, SET_COPYUID, emcore_mail_copyuid);
+
+       EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
+
+       if (!mail_copy_full((MAILSTREAM*)mail_stream, server_mail_id, dest_mailbox_name, CP_UID | CP_MOVE)) {
+               EM_DEBUG_EXCEPTION("emcore_move_mail_on_server :   Mail cannot be moved failed");
+               err_code = EMAIL_ERROR_IMAP4_COPY_FAILURE;
+       }
+       else {
+               /*  send EXPUNGE command */
+               if (!emcore_imap4_send_command((MAILSTREAM*)mail_stream, IMAP4_CMD_EXPUNGE, &err_code)) {
+                       EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
+
+                       if (err_code == EMAIL_ERROR_IMAP4_STORE_FAILURE)
+                               err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
+                       goto FINISH_OFF;
+               }
+       }
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err_code [%d]", err_code);
+       return err_code;
+}
 
-static int emcore_copy_mail_to_another_account_on_local_storeage(int input_mail_id, emstorage_mailbox_tbl_t *input_source_mailbox, emstorage_mailbox_tbl_t *input_target_mailbox, int input_task_id, int *output_mail_id)
+static int emcore_copy_mail_to_another_account_on_local_storeage(char *multi_user_name, int input_mail_id, emstorage_mailbox_tbl_t *input_source_mailbox, emstorage_mailbox_tbl_t *input_target_mailbox, int input_task_id, int *output_mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox[%p] input_target_mailbox[%p] input_task_id [%d] output_mail_id[%p]", input_mail_id, input_source_mailbox, input_target_mailbox, input_task_id, output_mail_id);
 
@@ -4755,30 +6193,34 @@ static int emcore_copy_mail_to_another_account_on_local_storeage(int input_mail_
                goto FINISH_OFF;
        }
 
-       if((err = emcore_get_mail_data(input_mail_id, &mail_data)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_mail_data(multi_user_name, input_mail_id, &mail_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
                err = EMAIL_ERROR_MAIL_NOT_FOUND;
                goto FINISH_OFF;
        }
 
        /* Check download status */
-       if(mail_data->body_download_status != 1) {
+       if(!(mail_data->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED)) {
                /* If not downloaded, download fully */
-               if (!emcore_download_body_multi_sections_bulk(NULL,
+#ifdef __FEATURE_USE_GMIME__
+               if (!emcore_gmime_download_body_sections(multi_user_name, 
+                                       NULL, 
                                        input_source_mailbox->account_id,
-                                       input_mail_id,
-                                       0,
-                                       (mail_data->attachment_count > 0)?1:0,
-                                       NO_LIMITATION,
-                                       input_task_id,
+                                       input_mail_id, 
+                                       (mail_data->attachment_count > 0)?1:0, 
+                                       NO_LIMITATION, 
+                                       input_task_id, 
+                                       0, 
+                                       0, 
                                        &err)) {
-                       EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed [%d]", err);
+                       EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
                        goto FINISH_OFF;
                }
+#endif
        }
 
        /* Get attachments */
-       if((err = emcore_get_attachment_data_list(input_mail_id, &attachment_data, &attachment_count)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_attachment_data_list(multi_user_name, input_mail_id, &attachment_data, &attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4790,7 +6232,7 @@ static int emcore_copy_mail_to_another_account_on_local_storeage(int input_mail_
        mail_data->thread_id         = 0;
        mail_data->thread_item_count = 0;
 
-       if((err = emcore_add_mail(mail_data, attachment_data, attachment_count, NULL, false, true)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_add_mail(multi_user_name, mail_data, attachment_data, attachment_count, NULL, false, true)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4810,7 +6252,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id)
+INTERNAL_FUNC int emcore_move_mail_to_another_account(char *multi_user_name, int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox_id[%d] input_target_mailbox_id[%d] result_mail_id[%p] input_task_id [%d]", input_mail_id, input_source_mailbox_id, input_target_mailbox_id, input_task_id);
        int err = EMAIL_ERROR_NONE;
@@ -4821,12 +6263,12 @@ INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int inp
        email_account_t *source_account_ref = NULL;
        email_account_t *target_account_ref = NULL;
 
-       if((err = emstorage_get_mailbox_by_id(input_source_mailbox_id, &source_mailbox)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_get_mailbox_by_id(multi_user_name, input_source_mailbox_id, &source_mailbox)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for source_mailbox [%d]", err);
                goto FINISH_OFF;
        }
 
-       if((err = emstorage_get_mailbox_by_id(input_target_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_get_mailbox_by_id(multi_user_name, input_target_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for target_mailbox [%d]", err);
                goto FINISH_OFF;
        }
@@ -4837,7 +6279,7 @@ INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int inp
        /* EAS  -> X    impossible */
        /* X    -> EAS  impossible */
 
-       source_account_ref = emcore_get_account_reference(source_mailbox->account_id);
+       source_account_ref = emcore_get_account_reference(multi_user_name, source_mailbox->account_id);
 
        if(source_account_ref == NULL || source_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                EM_DEBUG_EXCEPTION("Invalid account");
@@ -4845,7 +6287,7 @@ INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int inp
                goto FINISH_OFF;
        }
 
-       target_account_ref = emcore_get_account_reference(target_mailbox->account_id);
+       target_account_ref = emcore_get_account_reference(multi_user_name, target_mailbox->account_id);
 
        if(target_account_ref == NULL || target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                EM_DEBUG_EXCEPTION("Invalid account");
@@ -4854,33 +6296,33 @@ INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int inp
        }
 
 
-       if((err = emcore_copy_mail_to_another_account_on_local_storeage(input_mail_id, source_mailbox, target_mailbox, input_task_id, &moved_mail_id)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_copy_mail_to_another_account_on_local_storeage(multi_user_name, input_mail_id, source_mailbox, target_mailbox, input_task_id, &moved_mail_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_copy_mail_to_another_account_on_local_storeage failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 1 , &err)) {
+       if(!emcore_set_flags_field(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 1 , &err)) {
                EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                goto FINISH_OFF;
        }
 
        if(target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-               if((err = emcore_sync_mail_from_client_to_server(moved_mail_id)) != EMAIL_ERROR_NONE) {
+               if((err = emcore_sync_mail_from_client_to_server(multi_user_name, moved_mail_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed [%d]", err);
 
                        /* if append is failed, restore source mail and delete copied mail. */
-                       if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
+                       if(!emcore_set_flags_field(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
                                EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if(!emcore_delete_mail(target_mailbox->account_id, &moved_mail_id, 1, false, 0, 0, &err_for_delete_mail))
+                       if(!emcore_delete_mail(multi_user_name, target_mailbox->account_id, &moved_mail_id, 1, false, 0, 0, &err_for_delete_mail))
                                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err_for_delete_mail);
                        goto FINISH_OFF;
                }
        }
 
-       if(!emcore_delete_mail(source_mailbox->account_id, &input_mail_id, 1, true, 0, 0, &err)) {
+       if(!emcore_delete_mail(multi_user_name, source_mailbox->account_id, &input_mail_id, 1, true, 0, 0, &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4907,11 +6349,11 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code)
+INTERNAL_FUNC int emcore_save_mail_file(char *multi_user_name, int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, char *virtual_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d] , file_name[%p] , full_path[%p] , err_code[%p]", account_id, mail_id, attachment_id, file_name, full_path, err_code);
 
-       int ret = false, err = EMAIL_ERROR_NONE;
+       int err = EMAIL_ERROR_NONE;
 
        if (!file_name || !full_path || !src_file_path) {
                EM_DEBUG_EXCEPTION("Invalid paramter");
@@ -4919,12 +6361,12 @@ INTERNAL_FUNC int emcore_save_mail_file(int account_id, int mail_id, int attachm
                goto FINISH_OFF;
        }
 
-       if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err))  {
+       if (!emstorage_create_dir(multi_user_name, account_id, mail_id, attachment_id, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                goto FINISH_OFF;
        }
        
-       if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err))  {
+       if (!emstorage_get_save_name(multi_user_name, account_id, mail_id, attachment_id, file_name, full_path, virtual_path, sizeof(virtual_path), &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4936,20 +6378,20 @@ INTERNAL_FUNC int emcore_save_mail_file(int account_id, int mail_id, int attachm
                }
        }
 
-       ret = true;
-
 FINISH_OFF:
        if (err_code)
                *err_code = err;
+
        return 1;
 }
 
 /* description : update mail information */
-INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas)
+INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
 
        char                   filename_buf[1024]          = {0, };
+       char                   virtual_path[1024]          = {0, };
        char                   mailbox_id_param_string[10] = {0, };
        char                  *body_text_file_name         = NULL;
        int                    i                           = 0;
@@ -4961,7 +6403,8 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
        int ori_attachment_count                           = 0;
        int *temp_attachment_id_array                      = NULL;
        email_attachment_data_t *ori_attachment_data_list  = NULL;
-
+    char                   *prefix_path                = NULL;
+    char                   real_file_path[MAX_PATH]    = {0};
 
        if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list))  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -4969,78 +6412,121 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
                goto FINISH_OFF2;
        }
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF2;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        if(input_from_eas == 0) {
                if (input_mail_data->file_path_plain)  {
-                       if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_plain);
+
+                       if (stat(real_file_path, &st_buf) < 0)  {
                                EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
                                err = EMAIL_ERROR_FILE_NOT_FOUND;
-                               goto FINISH_OFF;
+                               goto FINISH_OFF2;
                        }
                }
                
                if (input_mail_data->file_path_html)  {
-                       if (stat(input_mail_data->file_path_html, &st_buf) < 0)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_html);
+
+                       if (stat(real_file_path, &st_buf) < 0)  {
                                EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
                                err = EMAIL_ERROR_FILE_NOT_FOUND;
-                               goto FINISH_OFF;
+                               goto FINISH_OFF2;
                        }
                }
                
                if (input_attachment_count && input_attachment_data_list)  {
                        for (i = 0; i < input_attachment_count; i++)  {
                                if (input_attachment_data_list[i].save_status) {
-                                       if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
+                    memset(real_file_path, 0x00, sizeof(real_file_path));
+                    SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_attachment_data_list[i].attachment_path);
+
+                                       if (!input_attachment_data_list[i].attachment_path || stat(real_file_path, &st_buf) < 0)  {
                                                EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
                                                err = EMAIL_ERROR_FILE_NOT_FOUND;
-                                               goto FINISH_OFF;
+                                               goto FINISH_OFF2;
                                        }
                                }
                        }
                }
        }
-       
+
        if(input_mail_data->mail_size == 0) {
-                emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
+                emcore_calc_mail_size(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
        }
 
        if (input_mail_data->file_path_plain)  {   /*  Save plain text body. */
                if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
                        err = EMAIL_ERROR_INVALID_FILE_PATH;
-                       goto FINISH_OFF;
+                       goto FINISH_OFF2;
                }
 
-               if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, body_text_file_name, filename_buf, &err)) {
+               if (!emcore_save_mail_file(multi_user_name, 
+                                    input_mail_data->account_id, 
+                                    input_mail_data->mail_id, 
+                                             0, 
+                                    input_mail_data->file_path_plain, 
+                                    body_text_file_name, 
+                                    filename_buf, 
+                                    virtual_path, 
+                                    &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                        goto FINISH_OFF2;
                }
                EM_SAFE_FREE(input_mail_data->file_path_plain);
-               input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
+               input_mail_data->file_path_plain = EM_SAFE_STRDUP(virtual_path);
        }
-       
+
        if (input_mail_data->file_path_html)  {   /*  Save HTML text body. */
                EM_SAFE_FREE(body_text_file_name);
                if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
                        err = EMAIL_ERROR_INVALID_FILE_PATH;
-                       goto FINISH_OFF;
+                       goto FINISH_OFF2;
                }
 
-               if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, body_text_file_name, filename_buf, &err)) {
+               if (!emcore_save_mail_file(multi_user_name, 
+                                    input_mail_data->account_id, 
+                                    input_mail_data->mail_id, 
+                                             0, 
+                                    input_mail_data->file_path_html, 
+                                    body_text_file_name, 
+                                    filename_buf, 
+                                    virtual_path, 
+                                    &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                        goto FINISH_OFF2;
                }
                EM_SAFE_FREE(input_mail_data->file_path_html);
-               input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
+               input_mail_data->file_path_html = EM_SAFE_STRDUP(virtual_path);
        }
 
        if (input_mail_data->file_path_mime_entity)  {   /*  Save mime entity. */
-               if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_mime_entity, "mime_entity", filename_buf, &err)) {
+               if (!emcore_save_mail_file(multi_user_name, 
+                                    input_mail_data->account_id, 
+                                    input_mail_data->mail_id, 
+                                                                                       0, 
+                                    input_mail_data->file_path_mime_entity, 
+                                    "mime_entity", 
+                                    filename_buf, 
+                                    virtual_path,
+                                    &err)) {
                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                        goto FINISH_OFF2;
                }
                EM_SAFE_FREE(input_mail_data->file_path_mime_entity);
-               input_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(filename_buf);
+               input_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(virtual_path);
        }
 
        if (input_attachment_data_list && input_attachment_count)  {
@@ -5049,49 +6535,49 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
                int compare_result = 1;
                email_attachment_data_t *temp_attachment_data = NULL;
                                
-               if ((err = emcore_get_attachment_data_list(input_mail_data->mail_id, &ori_attachment_data_list, &ori_attachment_count)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_get_attachment_data_list(multi_user_name, input_mail_data->mail_id, &ori_attachment_data_list, &ori_attachment_count)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed : [%d]", err);
                }
 
-               if (ori_attachment_count > 0) { /* prevent 33415 */
-                       temp_attachment_id_array = em_malloc (sizeof(int) * ori_attachment_count);
-                       if (temp_attachment_id_array == NULL) {
-                               EM_DEBUG_EXCEPTION("em_malloc failed");
-                               err = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF2;
-                       }
+               temp_attachment_id_array = em_malloc (sizeof(int) * input_attachment_count);
+               if (temp_attachment_id_array == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF2;
                }
-       
+
                for(i = 0; i < input_attachment_count; i++) {
                        temp_attachment_data = input_attachment_data_list + i;
-                       if ( (err = emcore_mail_compare_filename_of_attachment_data(input_mail_data->mail_id, \
+                       if ( (err = emcore_mail_compare_filename_of_attachment_data(multi_user_name, input_mail_data->mail_id, \
                                temp_attachment_data->attachment_id, temp_attachment_data, &compare_result)) != EMAIL_ERROR_NONE) {
-                               if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)                                            
+                               if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
                                        EM_DEBUG_LOG ("no attachment found");
                                else
                                        EM_DEBUG_EXCEPTION ("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
                        }
-       
-                       if (compare_result == 0) {
+
+                       switch (compare_result) {
+                       case 0 :
                                EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
-                               if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data))  {
+                               if (!emcore_mail_update_attachment_data(multi_user_name, input_mail_data->mail_id, temp_attachment_data))  {
                                        EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
                                        goto FINISH_OFF2;
                                }
                                temp_attachment_id_array[i] = temp_attachment_data->attachment_id;
-                       }
-                       else {
+                               break;
+                       case 1 :
                                EM_DEBUG_LOG("save names are different");
-                               if(temp_attachment_data->attachment_id > 0) {
-                                       if (!emcore_delete_mail_attachment(temp_attachment_data->attachment_id, &err)) {
-                                               EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
-                                       }
-                               }
-
-                               if ( (err = emcore_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMAIL_ERROR_NONE)  {
+                               if ( (err = emcore_add_attachment_data(multi_user_name, input_mail_data->mail_id, temp_attachment_data)) != EMAIL_ERROR_NONE)  {
                                        EM_DEBUG_EXCEPTION("emcore_add_attachment failed [%d]", err);
                                        goto FINISH_OFF2;
                                }
+
+                               temp_attachment_id_array[i] = temp_attachment_data->attachment_id;
+                               break;
+                       case 2 :
+                               EM_DEBUG_LOG("No chagned the attachment info");
+                               temp_attachment_id_array[i] = temp_attachment_data->attachment_id;
+                               break;
                        }
 
                        if (temp_attachment_data->inline_content_status)
@@ -5099,23 +6585,48 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
                }
 
                for (i = 0; i < ori_attachment_count; i++) {
+                       emstorage_attachment_tbl_t *temp_attachment_tbl_t = NULL;
+
                        temp_attachment_data = ori_attachment_data_list + i;
                        compare_result = 0;
+
                        for (j = 0; j < input_attachment_count; j++) {
-                               if (temp_attachment_id_array[j] != temp_attachment_data->attachment_id) 
-                                       continue;               
-                               
-                               compare_result = 1;                     
+                               if (!emstorage_get_attachment(multi_user_name, temp_attachment_id_array[j], &temp_attachment_tbl_t, false, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_attachment failed : [%d]", err);
+                                       continue;
+                               }
+
+                               if (temp_attachment_id_array[j] == temp_attachment_data->attachment_id) {
+                                       compare_result = 1;
+                                       break;
+                               }
+
+                               if ((temp_attachment_data->inline_content_status == INLINE_ATTACHMENT) && (strcmp(temp_attachment_tbl_t->attachment_name, temp_attachment_data->attachment_name) == 0))
+                                       compare_result = 2;
+
+                               emstorage_free_attachment(&temp_attachment_tbl_t, 1, NULL);
+
                        }
 
-                       if (!compare_result) {
-                               if (!emcore_delete_mail_attachment(temp_attachment_data->attachment_id, &err)) {
+                       switch (compare_result) {
+                       case 0 : /* Delete the attachment on db and file */
+                               if (!emcore_delete_mail_attachment(multi_user_name, temp_attachment_data->attachment_id, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
                                }
+                               break;
+                       case 2 : /* Delete the attachment on db */
+                               if (!emstorage_delete_attachment_on_db(multi_user_name, temp_attachment_data->attachment_id, true, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed : [%d]", err);
+                               }
+                               break;
+                       case 1:
+                               break;
                        }
+
+                       emstorage_free_attachment(&temp_attachment_tbl_t, 1, NULL);
                }
        }
-       
+
        input_mail_data->attachment_count     = input_attachment_count - local_inline_content_count;
        input_mail_data->inline_content_count = local_inline_content_count;
 
@@ -5126,7 +6637,11 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
 
        EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
        if (input_mail_data->preview_text == NULL) {
-               if ( (err =emcore_get_preview_text_from_file(input_mail_data->file_path_plain, input_mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(input_mail_data->preview_text))) != EMAIL_ERROR_NONE) {
+               if ( (err =emcore_get_preview_text_from_file(multi_user_name, 
+                                                    input_mail_data->file_path_plain, 
+                                                    input_mail_data->file_path_html, 
+                                                    MAX_PREVIEW_TEXT_LENGTH, 
+                                                    &(input_mail_data->preview_text))) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
                        if (err != EMAIL_ERROR_EMPTY_FILE)
                                goto FINISH_OFF2;
@@ -5137,14 +6652,14 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
                EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
                goto FINISH_OFF2;
        }
-       
+
        /*  BEGIN TRANSACTION; */
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                goto FINISH_OFF2;
        }
-       
-       if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err)) {
+
+       if (!emstorage_change_mail_field(multi_user_name, input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -5152,20 +6667,22 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
 #ifdef __FEATURE_BODY_SEARCH__
        /* strip html content and save into mail_text_tbl */
        char *stripped_text = NULL;
-       if (!emcore_strip_mail_body_from_file(converted_mail_tbl_data, &stripped_text, &err) || stripped_text == NULL) {
+       if (!emcore_strip_mail_body_from_file(multi_user_name, converted_mail_tbl_data, &stripped_text, &err) || stripped_text == NULL) {
                EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
        }
 
        emstorage_mail_text_tbl_t *mail_text;
-       if (!emstorage_get_mail_text_by_id(input_mail_data->mail_id, &mail_text, true, &err) || !mail_text) {
+
+       if (!emstorage_get_mail_text_by_id(multi_user_name, input_mail_data->mail_id, &mail_text, true, &err) || !mail_text) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_text_by_id failed [%d]", err);
+               EM_SAFE_FREE(stripped_text);
                goto FINISH_OFF;
        }
 
        EM_SAFE_FREE(mail_text->body_text);
        mail_text->body_text = stripped_text;
 
-       if (!emstorage_change_mail_text_field(input_mail_data->mail_id, mail_text, false, &err)) {
+       if (!emstorage_change_mail_text_field(multi_user_name, input_mail_data->mail_id, mail_text, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_change_mail_text_field failed [%d]", err);
                emstorage_free_mail_text(&mail_text, 1, NULL); /*prevent 17955*/
                goto FINISH_OFF;
@@ -5174,46 +6691,45 @@ INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_a
        if (mail_text)
                emstorage_free_mail_text(&mail_text, 1, NULL);
 #endif
-       
+
        if (input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
                || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
                || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
                /*  check where there is a meeting request in DB */
-               if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMAIL_ERROR_DATA_NOT_FOUND) {
+               if (!emstorage_get_meeting_request(multi_user_name, input_mail_data->mail_id, &meeting_req, false, &err) && err != EMAIL_ERROR_DATA_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
                        goto FINISH_OFF;
                }
-               EM_SAFE_FREE(meeting_req);
+
                if (err == EMAIL_ERROR_DATA_NOT_FOUND) {        /*  insert */
                        emstorage_mail_tbl_t *original_mail = NULL;
 
-                       if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
+                       if (!emstorage_get_mail_by_id(multi_user_name, input_mail_data->mail_id, &original_mail, false, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (original_mail)       {
-                       if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_id, input_meeting_request, false, &err))  {
-                               EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
-
-                               goto FINISH_OFF;
+                       if (original_mail)      {
+                               if (!emstorage_add_meeting_request(multi_user_name, input_mail_data->account_id, original_mail->mailbox_id, input_meeting_request, false, &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
+                                       emstorage_free_mail(&original_mail, 1, NULL);
+                                       goto FINISH_OFF;
+                               }
+                                       emstorage_free_mail(&original_mail, 1, NULL);
                        }
-                               emstorage_free_mail(&original_mail, 1, NULL);
-               }
                }
                else {  /*  update */
-                       if (!emstorage_update_meeting_request(input_meeting_request, false, &err))  {
+                       if (!emstorage_update_meeting_request(multi_user_name, input_meeting_request, false, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
-
                                goto FINISH_OFF;
                        }
                }
        }
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (err == EMAIL_ERROR_NONE) {
                /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                }
 
@@ -5224,20 +6740,23 @@ FINISH_OFF:
        }
        else {
                /*  ROLLBACK TRANSACTION; */
-               if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
        }
-       
+
 FINISH_OFF2:
 
        EM_SAFE_FREE(body_text_file_name);
        EM_SAFE_FREE(temp_attachment_id_array);
+       EM_SAFE_FREE(prefix_path);
 
        if (ori_attachment_data_list)
                emcore_free_attachment_data(&ori_attachment_data_list, ori_attachment_count, NULL);
 
-       if(meeting_req)
+       if(meeting_req) {
                emstorage_free_meeting_request(meeting_req);
+               EM_SAFE_FREE(meeting_req);
+       }
 
        if(converted_mail_tbl_data)
                emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
@@ -5247,7 +6766,7 @@ FINISH_OFF2:
 }
 
 
-INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
+INTERNAL_FUNC int emcore_set_flags_field(char *multi_user_name, int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_ids[%p], num [%d], field_type [%d], value[%d], err_code[%p]", account_id, mail_ids, num, field_type, value, err_code);
 
@@ -5263,42 +6782,42 @@ INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num
                goto  FINISH_OFF;
        }
 
-       if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
                goto FINISH_OFF;
        }
 
        if(field_type == EMAIL_FLAGS_SEEN_FIELD)
-               emcore_display_unread_in_badge();
+               emcore_display_unread_in_badge(NULL);
 
        ret = true;
-       
-FINISH_OFF: 
-       
+
+FINISH_OFF:
+
        if (err_code)
                *err_code = err;
 
-       EM_DEBUG_FUNC_END("err [%d]", err);     
+       EM_DEBUG_FUNC_END("err [%d]", err);
        return ret;
 }
 
 int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *result_total_body_size, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int total_body_size = 0;
        char command[32];
        char *response = NULL;
        POP3LOCAL *pop3local;
-       
+
        if (!stream || !result_total_body_size)  {
                EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
 
        if (!pop3local  || !pop3local->netstream)  {
@@ -5313,7 +6832,7 @@ int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, in
        EM_DEBUG_LOG("[POP3] >>> [%s]", command);
 #endif
 
-       
+
        /*  send command  :  LIST [msgno] - to get the size of the mail */
        if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command)))  {
                EM_DEBUG_EXCEPTION("net_sout failed...");
@@ -5382,7 +6901,7 @@ int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, in
                EM_DEBUG_EXCEPTION("pop3_send failed...");
 
                email_session_t *session = NULL;
-               
+
                if (!emcore_get_current_session(&session))  {
                        EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
                        err = EMAIL_ERROR_SESSION_NOT_FOUND;
@@ -5398,30 +6917,29 @@ int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, in
        }
 
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        EM_SAFE_FREE(response);
-       
+
        if (err_code != NULL)
                *err_code = err;
-       EM_DEBUG_FUNC_END("err [%d]", err);     
+       EM_DEBUG_FUNC_END("err [%d]", err);
        return ret;
 }
 
 
-INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
+INTERNAL_FUNC int emcore_sync_flag_with_server(char *multi_user_name, int mail_id, int event_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
-               
+
        if (mail_id < 1)  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int status = EMAIL_DOWNLOAD_FAIL;
        MAILSTREAM *stream = NULL;
        email_internal_mailbox_t mailbox = {0};
        emstorage_mail_tbl_t *mail = NULL;
@@ -5431,42 +6949,31 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
        char set_flags[100] = { 0, };
        char clear_flags[100] = { 0, };
        char tmp[100] = { 0, };
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
-               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
 
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        account_id = mail->account_id;
 
-       if (!(ref_account = emcore_get_account_reference(account_id)))   {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))   {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
-               status = EMAIL_LIST_CONNECTION_FAIL;
                goto FINISH_OFF;
        }
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        mailbox.mailbox_id = mail->mailbox_id;
        mailbox.account_id = account_id;
@@ -5477,7 +6984,7 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
                EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
                goto FINISH_OFF;
        }
-               
+
        sprintf (tmp, "%d", msgno);
 
        if (mail->flags_seen_field)
@@ -5489,7 +6996,7 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
                sprintf(set_flags, "%s \\Answered", set_flags);
        else
                sprintf(clear_flags, "%s \\Answered", clear_flags);
-               
+
        if (mail->flags_flagged_field)
                sprintf(set_flags, "%s \\Flagged", set_flags);
        else
@@ -5511,7 +7018,7 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
                mail_clearflag(stream, tmp, clear_flags);
                EM_DEBUG_LOG(">>>> End mail_clearflag ");
        }
-               
+
        if (mail->lock_status) {
                memset(set_flags, 0x00, 100);
                sprintf(set_flags, "Sticky");
@@ -5524,15 +7031,12 @@ INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
 
        EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
+       FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
        ret = true;
 
-FINISH_OFF: 
-       
+FINISH_OFF:
+
        if (stream)
                stream = mail_close (stream);
 
@@ -5546,11 +7050,11 @@ FINISH_OFF:
 
        if (err_code != NULL)
                *err_code = err;
-       EM_DEBUG_FUNC_END("err [%d]", err);     
+       EM_DEBUG_FUNC_END("err [%d]", err);
        return ret;
 }
 
-INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
+INTERNAL_FUNC int emcore_sync_seen_flag_with_server(char *multi_user_name, int mail_ids[], int num, int event_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
 
@@ -5562,7 +7066,6 @@ INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int status = EMAIL_DOWNLOAD_FAIL;
        MAILSTREAM *stream = NULL;
        email_internal_mailbox_t mailbox;
        emstorage_mail_tbl_t *mail = NULL;
@@ -5579,56 +7082,47 @@ INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int
 
        mail_id = mail_ids[0];
 
-       if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
        account_id = mail->account_id;
 
-       if (!(ref_account = emcore_get_account_reference(account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
+
        /*  open mail server. */
-       if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
-               status = EMAIL_LIST_CONNECTION_FAIL;
                goto FINISH_OFF;
        }
 
        mailbox.mailbox_id = mail->mailbox_id;
        mailbox.account_id = account_id;
        mailbox.mail_stream = stream;
-       
+
        for (i = 0; i < num; i++)  {
                mail_id = mail_ids[i];
-       
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
 
-               if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
+               if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail, true, &err) || !mail)  {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
-       
-                       goto FINISH_OFF;
-               }
-               
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
                        goto FINISH_OFF;
                }
 
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
+
                /*  download message number from server. */
                if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err))  {
                        EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
                        goto FINISH_OFF;
                }
-               
+
                memset(tmp, 0x00, 100);
                sprintf (tmp, "%d", msgno);
 
@@ -5654,18 +7148,15 @@ INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int
 
                EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
 
-               if (!emcore_check_thread_status())  {
-                       err = EMAIL_ERROR_CANCELLED;
-                       goto FINISH_OFF;
-               }
+               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
        }
        ret = true;
 
 FINISH_OFF:
-       
-       if (stream) 
+
+       if (stream)
                stream = mail_close (stream);
-       if (mail) 
+       if (mail)
                emstorage_free_mail(&mail, 1, NULL);
 
        if (ref_account) {
@@ -5682,13 +7173,13 @@ FINISH_OFF:
 INTERNAL_FUNC void emcore_free_mail_data_list(email_mail_data_t **mail_list, int count)
 {
        EM_DEBUG_FUNC_BEGIN("count[%d]", count);
-       
-       if (count <= 0 || !mail_list || !*mail_list) 
+
+       if (count <= 0 || !mail_list || !*mail_list)
                return;
-               
+
        email_mail_data_t* p = *mail_list;
        int i;
-       
+
        for (i = 0; i < count; i++)
                emcore_free_mail_data( p+i);
 
@@ -5696,7 +7187,7 @@ INTERNAL_FUNC void emcore_free_mail_data_list(email_mail_data_t **mail_list, int
 
        EM_DEBUG_FUNC_END();
 }
-       
+
 INTERNAL_FUNC void emcore_free_mail_data(email_mail_data_t *mail_data)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -5724,37 +7215,39 @@ INTERNAL_FUNC void emcore_free_mail_data(email_mail_data_t *mail_data)
        EM_SAFE_FREE(mail_data->file_path_html);
        EM_SAFE_FREE(mail_data->file_path_mime_entity);
        EM_SAFE_FREE(mail_data->preview_text);
+       EM_SAFE_FREE(mail_data->pgp_password);
        EM_SAFE_FREE(mail_data->eas_data);
 
-       EM_DEBUG_FUNC_END();    
+       EM_DEBUG_FUNC_END();
 }
 
 
 INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);       
-       
+       EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);
+
        if (!attachment_data_list || !*attachment_data_list)  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        email_attachment_data_t* p = *attachment_data_list;
        int i = 0;
-       
+
        for (i = 0; i < attachment_data_count; i++) {
                EM_SAFE_FREE(p[i].attachment_name);
                EM_SAFE_FREE(p[i].attachment_path);
                EM_SAFE_FREE(p[i].attachment_mime_type);
+               EM_SAFE_FREE(p[i].content_id);
        }
 
        EM_SAFE_FREE(p); *attachment_data_list = NULL;
 
        if(err_code)
                *err_code = EMAIL_ERROR_NONE;
-       
-       EM_DEBUG_FUNC_END();    
+
+       EM_DEBUG_FUNC_END();
        return true;
 }
 
@@ -5762,7 +7255,7 @@ INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachme
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code) 
+INTERNAL_FUNC int emcore_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code) 
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
 
@@ -5776,38 +7269,38 @@ INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int ac
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int before_tr_begin = 0;
-       
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+
+       if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err))  {
+
+       if (!emstorage_delete_pbd_activity(multi_user_name, account_id, mail_id, activity_id, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
                goto FINISH_OFF;
        }
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (ret == true) {      /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                        ret = false;
                }
        } else {        /*  ROLLBACK TRANSACTION; */
-               if (!before_tr_begin && emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
-       }       
-       
+       }
+
        if (err_code != NULL)
                *err_code = err;
        EM_DEBUG_FUNC_END();
        return ret;
 }
 
-INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code) 
+INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
 
@@ -5817,37 +7310,38 @@ INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        int before_tr_begin = 0;
-       
-       if (!emstorage_begin_transaction(NULL, NULL, &err)) {
+
+       if (!emstorage_begin_transaction(local_activity->multi_user_name, NULL, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
                before_tr_begin = 1;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err))  {
+
+       if (!emstorage_add_pbd_activity(local_activity->multi_user_name, local_activity, activity_id, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
                goto FINISH_OFF;
        }
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (ret == true) {      /*  COMMIT TRANSACTION; */
-               if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
+               if (emstorage_commit_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false) {
                        err = EMAIL_ERROR_DB_FAILURE;
                        ret = false;
                }
        } else {        /*  ROLLBACK TRANSACTION; */
-               if (!before_tr_begin && emstorage_rollback_transaction(NULL, NULL, NULL) == false)
+               if (!before_tr_begin && emstorage_rollback_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false)
                        err = EMAIL_ERROR_DB_FAILURE;
-       }       
-       
+       }
+
        if (err_code != NULL)
                *err_code = err;
+
        EM_DEBUG_FUNC_END();
        return ret;
 }
@@ -5858,10 +7352,10 @@ FINISH_OFF:
 
 /* API to set or unset a field of flags on server in single IMAP request to server */
 
-INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
+INTERNAL_FUNC int emcore_sync_flags_field_with_server(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, 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);
-               
+
        if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT)  {
                        EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                if (err_code != NULL) {
@@ -5869,16 +7363,15 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, e
                }
                return false;
        }
-       
+
        MAILSTREAM *stream = NULL;
        IMAPLOCAL *imaplocal = NULL;
        char tag[MAX_TAG_SIZE] = {0, };
        char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
        char *p = NULL;
-       char **string_list = NULL;      
+       char **string_list = NULL;
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int command_success = false;
        int account_id = 0;
        int mail_id = 0;
        int i = 0;
@@ -5892,33 +7385,32 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, e
        email_uid_range_set *uid_range_node = NULL;
        char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "\\Seen"
                , "\\Deleted", "\\Flagged", "\\Answered"
-               , "\\Recent", "\\Draft", "$Forwarded" }; 
-       
+               , "\\Recent", "\\Draft", "$Forwarded" };
+
        mail_id = mail_ids[0];
-       
-       if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)                /*To DO :  This is a existing bug. on mail deletion before this call it will fail always */ {
-               EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
 
+       if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)               /*To DO :  This is a existing bug. on mail deletion before this call it will fail always */ {
+               EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        account_id = mail[0].account_id;
 
-       temp_account = emcore_get_account_reference(account_id);
+       temp_account = emcore_get_account_reference(multi_user_name, account_id);
 
        if (!temp_account)   {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
+
        if (temp_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->incoming_server_type);
                err = EMAIL_ERROR_NOT_SUPPORTED;
                goto FINISH_OFF;
        }
        
-       if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -5942,16 +7434,15 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, e
 
        for (i = 0; i < string_count; ++i) {
                /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
-
-               if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
+               if (false == emstorage_get_id_set_from_mail_ids(multi_user_name, string_list[i], &id_set, &id_set_count, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
                        goto FINISH_OFF;
                }
-               
+
                /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
-               
+
                len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
-               
+
                if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
                        goto FINISH_OFF;
@@ -5963,7 +7454,7 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, e
                        /* Remove comma from end of uid_range */
 
                        uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
-                       
+
                        /* Form the IMAP command */
 
                        SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
@@ -5979,74 +7470,69 @@ INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, e
 
                        if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
                                EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
-                               
-                               err = EMAIL_ERROR_UNKNOWN;              
+
+                               err = EMAIL_ERROR_UNKNOWN;
                                goto FINISH_OFF;
                        }
-               
+
                        if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
                                EM_DEBUG_EXCEPTION("net_sout failed...");
-                               err = EMAIL_ERROR_CONNECTION_BROKEN;            
+                               err = EMAIL_ERROR_CONNECTION_BROKEN;
                                goto FINISH_OFF;
                        }
 
                        /* Receive Response */
 
-                       command_success = false;
-                       
-                       while (imaplocal->netstream) {  
+                       while (imaplocal->netstream) {
                                if (!(p = net_getline(imaplocal->netstream)))  {
                                        EM_DEBUG_EXCEPTION("net_getline failed...");
-                               
-                                       err = EMAIL_ERROR_INVALID_RESPONSE;     
+
+                                       err = EMAIL_ERROR_INVALID_RESPONSE;
                                        goto FINISH_OFF;
                                }
-                       
+
                                EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
-                               
+
                                if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
                                        if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
                                                /*Delete all local activities */
-                                               command_success = true;
-
-                                               EM_SAFE_FREE(p);        
+                                               EM_SAFE_FREE(p);
                                                break;
                                        }
                                        else  {
                                                /* 'NO' or 'BAD' */
-                                               command_success = false;
-                                               err = EMAIL_ERROR_IMAP4_STORE_FAILURE;          
+                                               err = EMAIL_ERROR_IMAP4_STORE_FAILURE;
                                                EM_SAFE_FREE(p);
                                                goto FINISH_OFF;
-                                       }               
+                                       }
                                }
-                               
-                               EM_SAFE_FREE(p);                
+
+                               EM_SAFE_FREE(p);
                        }
 
                        uid_range_node = uid_range_node->next;
-               }       
+               }
 
                emcore_free_uid_range_set(&uid_range_set);
 
                EM_SAFE_FREE(id_set);
-               
+
                id_set_count = 0;
        }
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        if (ret) {
                emstorage_activity_tbl_t new_activity;
-               for (i = 0; i<num ; i++) {              
+               for (i = 0; i<num ; i++) {
                        memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
                        new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
                        new_activity.account_id    = account_id;
                        new_activity.mail_id       = mail_ids[i];
-               
+
                        if (!emcore_delete_activity(&new_activity, &err))
                                EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
                }
@@ -6055,10 +7541,10 @@ FINISH_OFF:
 #endif
 
        if (NULL != mail) {
-               if (false == emstorage_free_mail(&mail, 1, &err))                       
+               if (false == emstorage_free_mail(&mail, 1, &err))
                        EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
        }
-       
+
        emcore_free_comma_separated_strings(&string_list, &string_count);
 
        if (false == ret)
@@ -6079,7 +7565,7 @@ FINISH_OFF:
 }
 #endif
 
-static int emcore_mail_move_by_filter_rule(int account_id, int mailbox_id, int mailbox_type, emstorage_rule_tbl_t *filter_info)
+static int emcore_mail_move_by_filter_rule(char *multi_user_name, int account_id, int mailbox_id, int mailbox_type, emstorage_rule_tbl_t *filter_info)
 {
        EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_id:[%d], filter_info:[%p]", account_id, mailbox_id, filter_info);
        int err = EMAIL_ERROR_NONE;
@@ -6092,18 +7578,17 @@ static int emcore_mail_move_by_filter_rule(int account_id, int mailbox_id, int m
        emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
        emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
        emstorage_mail_tbl_t *mail_tbl = NULL;
-       
+
        if ((account_id < 0) && !filter_info) {
                EM_DEBUG_EXCEPTION("Invalid parameter");
                err = EMAIL_ERROR_INVALID_PARAM;
                return err;
        }
 
-        if (filter_info->rule_id > 0 && !emstorage_update_tag_id(filter_info->rule_id, 0, &err)) {
-                EM_DEBUG_EXCEPTION("emstorage_update_tag_id failed : [%d]", err);
-        }
+    if (filter_info->rule_id > 0 && !emstorage_update_tag_id(multi_user_name, filter_info->rule_id, 0, &err)) 
+            EM_DEBUG_EXCEPTION("emstorage_update_tag_id failed : [%d]", err);
 
-       if (!emstorage_filter_mails_by_rule(account_id, mailbox_id, mailbox_type, false, filter_info, &filter_mail_id_list, &filter_mail_id_count, &err)) {
+       if (!emstorage_filter_mails_by_rule(multi_user_name, account_id, mailbox_id, mailbox_type, false, filter_info, &filter_mail_id_list, &filter_mail_id_count, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -6117,50 +7602,60 @@ static int emcore_mail_move_by_filter_rule(int account_id, int mailbox_id, int m
                        goto FINISH_OFF;
                }
 
-               SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%d%c", 0x01, mailbox_id, 0x01);
-               
                for (mail_id_index = 0; mail_id_index < filter_mail_id_count; mail_id_index++) {
                        memset(mail_id_string, 0, 10);
-                       SNPRINTF(mail_id_string, 10, "%d", filter_mail_id_list[mail_id_index]);
+                       if ((filter_mail_id_count - 1) == mail_id_index)
+                               SNPRINTF(mail_id_string, 10, "%d", filter_mail_id_list[mail_id_index]);
+                       else
+                               SNPRINTF(mail_id_string, 10, "%d,", filter_mail_id_list[mail_id_index]);
                        strcat(parameter_string, mail_id_string);
-                       strcat(parameter_string, ",");
                }
 
                EM_DEBUG_LOG("filtered_mail_id_count [%d]", filter_mail_id_count);
                EM_DEBUG_LOG("param string [%s]", parameter_string);
 
-               if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_id, 0, parameter_string, 0)) 
-                       EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
+               if (filter_info->type == EMAIL_PRIORITY_SENDER) {
+                       if (!emcore_notify_storage_event(NOTI_RULE_APPLY, account_id, filter_info->rule_id, parameter_string, 0))
+                               EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
+               } else {
+                       if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_id, 0, parameter_string, 0))
+                               EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
+               }
        }
 
        if (filter_info->action_type != EMAIL_FILTER_MOVE) {
                /* Move the mails on server */
-               if ((err = emstorage_get_mailbox_by_id(mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
+               if ((err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed : [%d]", err);
                        goto FINISH_OFF;
                }
 
                for (mail_id_index = 0; mail_id_index < filter_mail_id_count; mail_id_index++) {
-                       if (!emstorage_get_mail_by_id(filter_mail_id_list[mail_id_index], &mail_tbl, false, &err)) {
+                       if (!emstorage_get_mail_by_id(multi_user_name, filter_mail_id_list[mail_id_index], &mail_tbl, false, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if ((err = emstorage_get_mailbox_by_id(mail_tbl->mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE) {
+                       if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail_tbl->mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed : [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (!emcore_move_mail_on_server(account_id, src_mailbox_tbl->mailbox_id, filter_mail_id_list, filter_mail_id_count, dst_mailbox_tbl->mailbox_name, &err)) {
+                       if (!emcore_move_mail_on_server(multi_user_name, account_id, src_mailbox_tbl->mailbox_id, filter_mail_id_list, filter_mail_id_count, dst_mailbox_tbl->mailbox_name, &err)) {
                                EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : [%d]", err);
                                goto FINISH_OFF;
                        }
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                       if (!emstorage_update_auto_download_activity(multi_user_name, mail_tbl->server_mail_id, g_new_server_uid, NULL, dst_mailbox_tbl->mailbox_id, &err))
+                               EM_DEBUG_EXCEPTION("emstorage_update_auto_download_activity failed : [%d]", err);
+#endif
+
                        emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL); /*prevent 46750*/
                }
        }
 
-FINISH_OFF:    
+FINISH_OFF:
 
        if (src_mailbox_tbl)
                emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
@@ -6176,7 +7671,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err_code)
+INTERNAL_FUNC int emcore_mail_filter_by_rule(char *multi_user_name, email_rule_t *filter_info, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
 
@@ -6196,13 +7691,13 @@ INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err
                if (filter_info->type != EMAIL_PRIORITY_SENDER)
                        break;
 
-               if ((err = emcore_mail_move_by_filter_rule(filter_info->account_id, 0, 0, (emstorage_rule_tbl_t *)filter_info)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_mail_move_by_filter_rule(multi_user_name, filter_info->account_id, 0, 0, (emstorage_rule_tbl_t *)filter_info)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_mail_move_by_filter_rule failed : [%d]", err);
                        goto FINISH_OFF;
                }
-               break;  
+               break;
        case EMAIL_FILTER_BLOCK :
-               if (!emstorage_get_account_list(&account_count, &account_tbl_t_list, false, false, &err)) {
+               if (!emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_t_list, false, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -6210,17 +7705,17 @@ INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err
                for (account_index = 0; account_index < account_count; account_index++) {
                        account_tbl_t = account_tbl_t_list + account_index;
 
-                       if (!emstorage_get_mailbox_by_mailbox_type(account_tbl_t->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err)) {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_tbl_t->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed : [%d]", account_tbl_t->account_id, err);
                                continue;
                        }
 
-                       if ((err = emcore_mail_move_by_filter_rule(account_tbl_t->account_id, spam_mailbox->mailbox_id, spam_mailbox->mailbox_type, (emstorage_rule_tbl_t *)filter_info)) != EMAIL_ERROR_NONE) {
+                       if ((err = emcore_mail_move_by_filter_rule(multi_user_name, account_tbl_t->account_id, spam_mailbox->mailbox_id, spam_mailbox->mailbox_type, (emstorage_rule_tbl_t *)filter_info)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emcore_mail_move_by_filter_rule failed : [%d]", err);
                                goto FINISH_OFF;
                        }
-               
-                       
+
+
                }
                break;
        case EMAIL_FILTER_DELETE :
@@ -6229,12 +7724,12 @@ INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err
                break;
        }
 
-       emcore_display_unread_in_badge();
+       emcore_display_unread_in_badge(NULL);
 
        ret = true;
 
-FINISH_OFF: 
-       
+FINISH_OFF:
+
        if (account_tbl_t_list)
                emstorage_free_account(&account_tbl_t_list, account_count, NULL);
 
@@ -6247,3 +7742,250 @@ FINISH_OFF:
        return ret;
 }
 
+INTERNAL_FUNC int emcore_add_rule(char *multi_user_name, email_rule_t *filter_info)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+
+       if (!emstorage_find_rule(multi_user_name, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
+               goto FINISH_OFF;
+       } else {
+               if (err != EMAIL_ERROR_FILTER_NOT_FOUND) {
+                       EM_DEBUG_LOG("filter already exist");
+                       err = EMAIL_ERROR_ALREADY_EXISTS;
+                       goto FINISH_OFF;
+               }
+       }
+
+       switch (filter_info->faction) {
+       case EMAIL_FILTER_MOVE :
+               if (filter_info->account_id < 0) {
+                       EM_DEBUG_EXCEPTION("Invalid Param : target_mailbox_id[%d], account_id[%d]", filter_info->target_mailbox_id, filter_info->account_id);
+                       err = EMAIL_ERROR_INVALID_PARAM;
+                       goto FINISH_OFF;
+               }
+               break;
+       case EMAIL_FILTER_BLOCK :
+               filter_info->account_id = ALL_ACCOUNT;
+               break;
+       case EMAIL_FILTER_DELETE :
+       default:
+               EM_DEBUG_LOG("filter_faction : [%d]", filter_info->faction);
+               break;
+       }
+
+       if (!emstorage_add_rule(multi_user_name, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_add_rule failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("filter_id : [%d]", filter_info->filter_id);
+
+       if (!emcore_notify_storage_event (NOTI_RULE_ADD, filter_info->account_id, filter_info->filter_id, NULL, 0))
+               EM_DEBUG_EXCEPTION ("emcore_notify_storage_event failed [NOTI_RULE_ADD]");
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC int emcore_update_rule(char *multi_user_name, int filter_id, email_rule_t *filter_info)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+    int err = EMAIL_ERROR_NONE;
+    int filter_mail_id_count = 0;
+    int mail_id_index = 0;
+    int *filter_mail_id_list = NULL;
+    int parameter_string_length = 0;
+    char mail_id_string[10] = { 0x00, };
+    char *parameter_string = NULL;
+
+    emstorage_rule_tbl_t *old_filter_info = NULL;
+
+    if (filter_id <= 0 || !filter_info) {
+        EM_DEBUG_EXCEPTION("filter_id [%d], filter_info[%p]", filter_id, filter_info);
+        err = EMAIL_ERROR_INVALID_PARAM;
+        return err;
+    }
+
+    if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &old_filter_info, false, &err)) {
+            EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed : [%d]", err);
+            goto FINISH_OFF;
+    }
+
+    if (!emstorage_filter_mails_by_rule(multi_user_name, old_filter_info->account_id, old_filter_info->target_mailbox_id, false, true, old_filter_info, &filter_mail_id_list, &filter_mail_id_count, &err)) {
+            EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed : [%d]", err);
+            goto FINISH_OFF;
+    }
+
+    if (filter_mail_id_count) {
+            parameter_string_length = 10 /*mailbox_id length*/ + 7 + (10 * filter_mail_id_count);
+            parameter_string = em_malloc(sizeof(char) * parameter_string_length);
+            if (parameter_string == NULL) {
+                    err = EMAIL_ERROR_OUT_OF_MEMORY;
+                    EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
+                    goto FINISH_OFF;
+            }
+
+            for (mail_id_index = 0; mail_id_index < filter_mail_id_count; mail_id_index++) {
+                    memset(mail_id_string, 0, 10);
+                    if ((filter_mail_id_count - 1) == mail_id_index)
+                            SNPRINTF(mail_id_string, 10, "%d", filter_mail_id_list[mail_id_index]);
+                    else
+                            SNPRINTF(mail_id_string, 10, "%d,", filter_mail_id_list[mail_id_index]);
+                    strcat(parameter_string, mail_id_string);
+            }
+
+            EM_DEBUG_LOG("filtered_mail_id_count [%d]", filter_mail_id_count);
+            EM_DEBUG_LOG("param string [%s]", parameter_string);
+    }
+
+    if (!emstorage_change_rule(multi_user_name, filter_id, (emstorage_rule_tbl_t *)filter_info, true, &err)) {
+        EM_DEBUG_EXCEPTION("emstorage_change_rule failed [%d]", err);
+        goto FINISH_OFF;
+    }
+
+    if (!emcore_notify_storage_event(NOTI_RULE_UPDATE, filter_info->account_id, filter_info->filter_id, parameter_string, 0))
+        EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [NOTI_RULE_UPDATE]");
+
+FINISH_OFF:
+
+    if (old_filter_info)
+        emstorage_free_rule(&old_filter_info, 1, NULL);
+
+    EM_SAFE_FREE(filter_mail_id_list);
+    EM_SAFE_FREE(parameter_string);
+
+    EM_DEBUG_FUNC_END();
+    return err;
+}
+
+INTERNAL_FUNC int emcore_delete_rule(char *multi_user_name, int filter_id)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int filter_mail_id_count = 0;
+       int mail_id_index = 0;
+       int *filter_mail_id_list = NULL;
+       int parameter_string_length = 0;
+       char mail_id_string[10] = { 0x00, };
+       char *parameter_string = NULL;
+
+       emstorage_rule_tbl_t *filter_info = NULL;
+
+       if (filter_id <= 0)  {
+               EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &filter_info, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_filter_mails_by_rule(multi_user_name, filter_info->account_id, filter_info->target_mailbox_id, false, true, filter_info, &filter_mail_id_list, &filter_mail_id_count, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (filter_mail_id_count) {
+               parameter_string_length = 10 /*mailbox_id length*/ + 7 + (10 * filter_mail_id_count);
+               parameter_string = em_malloc(sizeof(char) * parameter_string_length);
+               if (parameter_string == NULL) {
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
+                       goto FINISH_OFF;
+               }
+
+               for (mail_id_index = 0; mail_id_index < filter_mail_id_count; mail_id_index++) {
+                       memset(mail_id_string, 0, 10);
+                       if ((filter_mail_id_count - 1) == mail_id_index)
+                               SNPRINTF(mail_id_string, 10, "%d", filter_mail_id_list[mail_id_index]);
+                       else
+                               SNPRINTF(mail_id_string, 10, "%d,", filter_mail_id_list[mail_id_index]);
+                       strcat(parameter_string, mail_id_string);
+               }
+
+               EM_DEBUG_LOG("filtered_mail_id_count [%d]", filter_mail_id_count);
+               EM_DEBUG_LOG("param string [%s]", parameter_string);
+       }
+
+       if (!emstorage_delete_rule(multi_user_name, filter_id, true, &err)) {
+               EM_DEBUG_EXCEPTION(" emstorage_delete_rule failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (!emcore_notify_storage_event (NOTI_RULE_DELETE, 0, filter_id, parameter_string, 0))
+               EM_DEBUG_EXCEPTION ("emcore_notify_storage_event failed [NOTI_RULE_DELETE]");
+
+FINISH_OFF:
+
+       if (filter_info)
+               emstorage_free_rule(&filter_info, 1, NULL);
+
+       EM_SAFE_FREE(filter_mail_id_list);
+       EM_SAFE_FREE(parameter_string);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+INTERNAL_FUNC void emcore_free_content_info(struct _m_content_info *cnt_info)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       struct attachment_info *p;
+
+       if (!cnt_info) return ;
+       EM_SAFE_FREE(cnt_info->text.plain);
+       EM_SAFE_FREE(cnt_info->text.plain_charset);
+       EM_SAFE_FREE(cnt_info->text.html);
+       EM_SAFE_FREE(cnt_info->text.html_charset);
+       EM_SAFE_FREE(cnt_info->sections);
+
+       while (cnt_info->file) {
+               p = cnt_info->file->next;
+               EM_SAFE_FREE(cnt_info->file->name);
+               EM_SAFE_FREE(cnt_info->file->save);
+               EM_SAFE_FREE(cnt_info->file->attachment_mime_type);
+               EM_SAFE_FREE(cnt_info->file->content_id);
+               EM_SAFE_FREE(cnt_info->file);
+               cnt_info->file = p;
+       }
+
+       while (cnt_info->inline_file) {
+               p = cnt_info->inline_file->next;
+               EM_SAFE_FREE(cnt_info->inline_file->name);
+               EM_SAFE_FREE(cnt_info->inline_file->save);
+               EM_SAFE_FREE(cnt_info->inline_file->attachment_mime_type);
+               EM_SAFE_FREE(cnt_info->inline_file->content_id);
+               EM_SAFE_FREE(cnt_info->inline_file);
+               cnt_info->inline_file = p;
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emcore_free_attachment_info(struct attachment_info *attchment)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       struct attachment_info *p;
+
+       if (!attchment) return;
+
+       while (attchment) {
+               p = attchment->next;
+               EM_SAFE_FREE(attchment->name);
+               EM_SAFE_FREE(attchment->save);
+               EM_SAFE_FREE(attchment->attachment_mime_type);
+               EM_SAFE_FREE(attchment->content_id);
+               EM_SAFE_FREE(attchment);
+               attchment = p;
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
index a9c3ae0..d63f918 100755 (executable)
@@ -32,6 +32,8 @@
 #include <ctype.h>
 #include <vconf.h>
 #include <unicode/ucsdet.h>
+#include <glib.h>
+#include <glib/gstdio.h>
 
 #include "email-internal-types.h"
 
@@ -45,7 +47,6 @@
 #include "email-utilities.h"
 #include "email-convert.h"
 #include "email-core-mailbox-sync.h"
-#include "email-core-global.h"
 #include "email-core-imap-mailbox.h"
 #include "email-core-event.h"
 #include "email-core-mailbox.h"
@@ -56,6 +57,7 @@
 #include "email-core-account.h"
 #include "email-storage.h"
 #include "email-core-signal.h"
+#include "email-core-gmime.h"
 #include "flstring.h"
 #include "email-debug-log.h"
 
@@ -69,13 +71,36 @@ 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(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int mailbox_id, int *err_code);
+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__
 static char g_append_uid_rsp[129]; /* added for getting server response  */
 #endif
 
+static struct {
+       const char *charset;
+       const char *tm_zone;
+} known_zone_charset[] = {
+       {"euc-kr",      "kst"},
+        {"euc-jp",      "jst"}
+#if 0
+        {"Big5",        "zh" },
+        {"BIG5HKSCS",   "zh" },
+        {"gb2312",      "zh" },
+        {"gb18030",     "zh" },
+        {"gbk",         "zh" },
+        {"euc-tw",      "zh" },
+        {"iso-2022-jp", "ja" },
+        {"Shift-JIS",   "ja" },
+        {"sjis",        "ja" },
+        {"ujis",        "ja" },
+        {"eucJP",       "ja" },
+        {"euc-kr",      "ko" },
+        {"koi8-r",      "ru" },
+        {"koi8-u",      "uk" }
+#endif
+};
 
 int pop3_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code)
 {
@@ -356,16 +381,17 @@ FINISH_OFF:
        return ret;
 }
 
-int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
+int imap4_mailbox_get_uids(MAILSTREAM *stream, char *input_target_uid_string, emcore_uid_list** uid_list, int *err_code)
 {
        EM_PROFILE_BEGIN(ImapMailboxGetUids);
-       EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
+       EM_DEBUG_FUNC_BEGIN("stream[%p] input_target_uid_string[%p] uid_list[%p] err_code[%p]", stream, input_target_uid_string, uid_list, err_code);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-
+       int command_length = 0;
        IMAPLOCAL *imaplocal = NULL;
-       char tag[16], command[64];
+       char tag[16];
+       char *command= NULL;
        char *response = NULL;
        emcore_uid_list *uid_elem = NULL;
 
@@ -385,11 +411,17 @@ int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *
                err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
                goto FINISH_OFF;
        }
+
+       command_length = sizeof(char) * (EM_SAFE_STRLEN(input_target_uid_string) + sizeof(tag) + 100);
+       command = em_malloc(command_length);
        memset(tag, 0x00, sizeof(tag));
-       memset(command, 0x00, sizeof(command));
+       memset(command, 0x00, command_length);
 
        SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
-       SNPRINTF(command, sizeof(command), "%s FETCH 1:* (FLAGS UID)\015\012", tag);
+       if (input_target_uid_string)
+               SNPRINTF(command, command_length, "%s UID FETCH %s (FLAGS UID INTERNALDATE)\015\012", tag, input_target_uid_string);
+       else
+               SNPRINTF(command, command_length, "%s FETCH 1:* (FLAGS UID INTERNALDATE)\015\012", tag);
        EM_DEBUG_LOG("COMMAND [%s] \n", command);
 #ifdef FEATURE_CORE_DEBUG
        EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
@@ -406,6 +438,7 @@ int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *
 
        while (imaplocal->netstream) {
                char *p = NULL;
+               char *p_date = NULL;
                char *s = NULL;
                int seen = 0;
                int forwarded = 0;
@@ -443,6 +476,19 @@ int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *
                                forwarded = strstr(p, "$Forwarded") ? 1  :  0;
 
                                if ((p = strstr(p, "UID "))) {
+
+                                       /* get INTERNALDATE */
+                                       char internaldate[100] = {0,};
+                                       if ((p_date = strstr(p, "INTERNALDATE \""))) {
+                                               int i = 0;
+                                               p_date += strlen("INTERNALDATE \"");
+                                               while (*p_date != '"' && i < 99) {
+                                                       internaldate[i] = *p_date;
+                                                       i++;
+                                                       p_date++;
+                                               }
+                                       }
+
                                        s = p + strlen("UID ");
 
                                        while (isdigit(*s))
@@ -462,6 +508,7 @@ int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *
                                        uid_elem->flag.draft = draft;
                                        uid_elem->flag.forwarded = forwarded;
                                        uid_elem->flag.flagged = flagged;
+                                       uid_elem->internaldate = EM_SAFE_STRDUP(internaldate);
                                        if (*uid_list  != NULL)
                                                uid_elem->next = *uid_list;             /*  prepend new data to list */
 
@@ -478,13 +525,14 @@ int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *
                        goto FINISH_OFF;
                }
 
-               EM_SAFE_FREE(response);;
+               EM_SAFE_FREE(response);
        }
 
        ret = true;
 
 FINISH_OFF:
        EM_SAFE_FREE(response);
+       EM_SAFE_FREE(command);
 
        if (err_code  != NULL)
                *err_code = err;
@@ -494,53 +542,160 @@ FINISH_OFF:
        return ret;
 }
 
-static char *__em_get_month_in_string(int month)
+int emcore_get_uids_order_by_datetime_from_imap_server(MAILSTREAM *stream, int count_to_download, emcore_uid_list** output_uid_list)
 {
-       EM_DEBUG_FUNC_BEGIN("month [%d]", month);
+       EM_PROFILE_BEGIN(emcore_get_uids_order_by_datetime_from_imap_server);
+       EM_DEBUG_FUNC_BEGIN("stream[%p] count_to_download [%d] output_uid_list[%p]", stream, count_to_download, output_uid_list);
 
-       char *mon = NULL;
+       int err = EMAIL_ERROR_NONE;
+
+       IMAPLOCAL *imaplocal = NULL;
+       char tag[16], command[64];
+       char *response = NULL;
+       emcore_uid_list *uid_elem = NULL;
+       char delims[] = " ";
+       char *result = NULL;
+       int  uid_count = 0;
+       time_t         RawTime = 0;
+       char           since_date_string[20];
+       char           before_date_string[20];
+       char          *uid_range_string = NULL;
+       emcore_uid_list *uid_list_for_listing = NULL;
+
+       if (!stream || !output_uid_list) {
+               EM_DEBUG_EXCEPTION(" stream[%p], output_uid_list[%p]", stream, output_uid_list);
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       if (!(imaplocal = stream->local) || !imaplocal->netstream) {
+               EM_DEBUG_EXCEPTION(" invalid IMAP4 stream detected...");
+               err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN */
+               goto FINISH_OFF;
+       }
+
+       /* Fetch the System time and Retrieve the a Week before time */
+       time(&RawTime);
+
+       while (uid_count <= count_to_download) {
+               RawTime = RawTime - (604800 * 4); //4 Weeks Before
+
+               emcore_make_date_string_for_search(RawTime, since_date_string);
+
+               memset(tag, 0x00, sizeof(tag));
+               memset(command, 0x00, sizeof(command));
+
+               SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
+               if (EM_SAFE_STRLEN(before_date_string) > 0)
+                       SNPRINTF(command, sizeof(command), "%s UID SEARCH 1:* SINCE %s BEFORE %s\015\012", tag, since_date_string, before_date_string);
+               else
+                       SNPRINTF(command, sizeof(command), "%s UID SEARCH 1:* SINCE %s\015\012", tag, since_date_string);
+               EM_DEBUG_LOG("COMMAND [%s] ", command);
+
+#ifdef FEATURE_CORE_DEBUG
+               EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
+#endif
+
+               /*  send command  :  get msgno/uid for all message */
+               if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
+                       EM_DEBUG_EXCEPTION(" net_sout failed...");
+                       err = EMAIL_ERROR_CONNECTION_BROKEN;
+                       goto FINISH_OFF;
+               }
+
+               while (imaplocal->netstream) {
+                       char *p = NULL;
+                       /*  receive response */
+                       if (!(response = net_getline(imaplocal->netstream))) {
+                               EM_DEBUG_EXCEPTION(" net_getline failed...");
+                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                               goto FINISH_OFF;
+                       }
+#ifdef FEATURE_CORE_DEBUG
+                       EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
+#endif
+
+                       if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
+                               if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
+                                       EM_SAFE_FREE (response);
+                                       break;
+                               }
+                               else {  /*  'NO' or 'BAD' */
+                                       err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;              /* EMAIL_ERROR_INVALID_RESPONSE; */
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       if ((p = strstr(response, " SEARCH "))) {
+                               *p = '\0'; p  += strlen(" SEARCH ");
+
+                               result = strtok(p, delims);
 
-       switch (month){
-           case 0:
-                       mon = strdup("jan");
-               break;
-           case 1:
-                       mon = strdup("feb");
-               break;
-           case 2:
-                       mon = strdup("mar");
-               break;
-           case 3:
-                       mon = strdup("apr");
-               break;
-           case 4:
-                       mon = strdup("may");
-               break;
-           case 5:
-                       mon = strdup("jun");
-               break;
-           case 6:
-                       mon = strdup("jul");
-               break;
-           case 7:
-                       mon = strdup("aug");
-               break;
-           case 8:
-                       mon = strdup("sep");
-               break;
-           case 9:
-                       mon = strdup("oct");
-               break;
-           case 10:
-                       mon = strdup("nov");
-               break;
-           case 11:
-                       mon = strdup("dec");
-               break;
-       }
-       return mon;
+                               while (result  != NULL)
+                               {
+                                       EM_DEBUG_LOG("UID VALUE DEEP is [%s]", result);
+
+                                       if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
+                                               EM_DEBUG_EXCEPTION(" malloc failed...");
+                                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                               goto FINISH_OFF;
+                                       }
+
+                                       uid_elem->uid = EM_SAFE_STRDUP(result);
+
+                                       if (uid_list_for_listing != NULL)
+                                               uid_elem->next = uid_list_for_listing;
+                                       uid_list_for_listing = uid_elem;
+                                       result = strtok(NULL, delims);
+                                       uid_count++;
+                               }
+
+                               EM_SAFE_FREE(response);
+                               continue;
+                       }
+                       else if ((p = strstr(response, " SEARCH"))){
+                               /* there is no mail */
+                               continue;
+                       }
+                       else {
+                               err = EMAIL_ERROR_INVALID_RESPONSE;
+                               goto FINISH_OFF;
+                       }
+               }
+
+               EM_SAFE_STRCPY(before_date_string, since_date_string);
+               EM_DEBUG_LOG("uid_count [%d] ", uid_count);
+       }
+
+       EM_DEBUG_LOG("uid_count [%d] ", uid_count);
+
+       if ((err = emcore_make_uid_range_string(uid_list_for_listing, uid_count, &uid_range_string)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_make_uid_range_string failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("uid_range_string [%s] ", uid_range_string);
+
+       /* Get uids with flags */
+       if(!imap4_mailbox_get_uids(stream, uid_range_string, output_uid_list, &err)) {
+               EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       if (uid_list_for_listing)
+               emcore_free_uids(uid_list_for_listing, NULL);
+
+       EM_SAFE_FREE (response);
+        EM_SAFE_FREE (uid_range_string);
+
+       EM_PROFILE_END(emcore_get_uids_order_by_datetime_from_imap_server);
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
+
 int imap4_mailbox_get_uids_by_timestamp(MAILSTREAM *stream, emcore_uid_list** uid_list,  int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreMailboxuidsbystamp);
@@ -706,7 +861,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
        int len, i, j;
        int subject_flag = 0;
 
-       EM_DEBUG_LOG("Buffer length [%d]", PARSE_BUFFER_LENGTH);
+
 
        *priority = 3;
 
@@ -726,7 +881,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
                        EM_DEBUG_LOG_DEV("buf:%s", buf);
 
                        /*  disposition_notification_to */
-                       if (buf[0] == 'D' && buf[11] == '-' && buf[12] == 'N' && buf[24] == '-' && buf[25] == 'T') {
+                       if ((buf[0] == 'D' || buf[0] == 'd') && buf[11] == '-' && (buf[12] == 'N' || buf[12] == 'n') && buf[24] == '-' && (buf[25] == 'T' || buf[25] == 't')) {
                                em_upper_string(buf);
                                if (req_read_receipt)
                                        *req_read_receipt = 1;
@@ -735,7 +890,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
                        }
 
                        /*  x-priority */
-                       if (buf[0] == 'X' && buf[2] == 'P' && buf[9] == 'Y'){
+                       if ((buf[0] == 'X' || buf[0] == 'x') && (buf[2] == 'P' || buf[2] == 'p') && (buf[9] == 'Y' || buf[9] == 'y')) {
                                em_upper_string(buf);
                                size_t len_2 = EM_SAFE_STRLEN(buf);
                                if (len_2 >= 12){
@@ -747,25 +902,36 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
                        }
 
                        /*  x-msmail-priority */
-                       if (buf[0] == 'X' && buf[2] == 'M' && buf[9] == 'P' && buf[16] == 'Y'){
+                       if ((buf[0] == 'X' || buf[0] == 'x') && (buf[2] == 'M' || buf[2] == 'm') && (buf[9] == 'P' || buf[9] == 'p') && (buf[16] == 'Y' || buf[16] == 'y')) {
                                em_upper_string(buf);
-                               if (strstr(buf, "HIGH"))
+                               if (strcasestr(buf, "HIGH"))
                                        *priority = 1;
-                               if (strstr(buf, "NORMAL"))
+                               if (strcasestr(buf, "NORMAL"))
                                        *priority = 3;
-                               if (strstr(buf, "LOW"))
+                               if (strcasestr(buf, "LOW"))
                                        *priority = 5;
                                memset(buf, 0x00, PARSE_BUFFER_LENGTH);
                                continue;
                        }
 
+                       /* Importance */
+                       if ((buf[0] == 'I' || buf[0] == 'i') && (buf[2] == 'P' || buf[2] == 'p') && (buf[6] == 'A' || buf[6] == 'a')) {
+                               em_upper_string(buf);
+                               if (strcasestr(buf, "HIGH"))
+                                       *priority = 1;
+                               if (strcasestr(buf, "NORMAL"))
+                                       *priority = 3;
+                               if (strcasestr(buf, "LOW"))
+                                       *priority = 5;
+                       }
+
                        /* subject */
                        if (subject) {
                                if (buf[0] != ' ' && buf[0] != '\t') {
                                        subject_flag = 0;
                                }
 
-                               if (buf[0] == 'S' && buf[1] == 'u' && buf[2] == 'b') {
+                               if ((buf[0] == 'S' || buf[0] == 's') && (buf[1] == 'u' || buf[1] == 'U') && (buf[2] == 'b' || buf[2] == 'B')) {
                                        char *deli = NULL;
                                        char *r = NULL;
 
@@ -774,6 +940,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
                                        if (r) *r = '\0';
 
                                        if (deli) {
+                                               if (*subject) EM_SAFE_FREE(*subject);
                                                *subject = g_strdup(deli+1);
                                                EM_DEBUG_LOG_DEV("subject:%s", *subject);
                                        }
@@ -802,7 +969,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
 
                        /* From */
                        if (from) {
-                               if (buf[0] == 'F' && buf[1] == 'r' && buf[2] == 'o') {
+                               if ((buf[0] == 'F' || buf[0] == 'f') && (buf[1] == 'r' || buf[1] == 'R') && (buf[2] == 'o' || buf[2] == 'O')) {
                                        char *deli = NULL;
                                        char *r = NULL;
 
@@ -811,6 +978,7 @@ static int emcore_parse_header(char *rfc822_header, char **subject, char **from,
                                        if (r) *r = '\0';
 
                                        if (deli) {
+                                               if (*from) EM_SAFE_FREE(*from);
                                                *from = g_strdup(deli+1);
                                                EM_DEBUG_LOG_DEV("from:%s", *from);
                                        }
@@ -850,7 +1018,7 @@ static int emcore_get_mail_extra_info(MAILSTREAM *stream, int msgno, char **subj
 #ifdef __FEATURE_HEADER_OPTIMIZATION__
        /* Check if header already available in cache */
        if (stream && stream->cache && stream->cache[msgno-1]->private.msg.header.text.data){
-               EM_DEBUG_LOG("I found the header in stream->cache!!");
+
                rfc822_header = (char *) stream->cache[msgno-1]->private.msg.header.text.data;
        }
        else{
@@ -891,7 +1059,96 @@ FINISH_OFF:
 #define EMAIL_SYNC_STATUS_SEEN_FLAG_CHANGED    2
 #define EMAIL_SYNC_STATUS_FLAG_CHANGED         3
 
-static int emcore_get_uids_to_download(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 GDateTime *emcore_convert_strftime(char *time_str)
+{
+       GDateTime *datetime = NULL;
+       gchar **token_list = NULL;
+       int i = 0;
+       int index = 0;
+       int day = 0;
+       int month = 0;
+       int year = 0;
+       int hh = 0;
+       int mm = 0;
+       int ss = 0;
+       char *month_str = NULL;
+       char *ar_month[13] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
+
+       token_list = g_strsplit_set(time_str, "-: ", -1);
+
+       if(token_list == NULL) {
+               EM_DEBUG_LOG("g_strsplit_set failed.");
+               return NULL;
+       }
+
+       while (token_list[index] != NULL) {
+               index++;
+       }
+
+       for (i = 0; i < index; i++) {
+               switch (i) {
+               case 0:         //Day
+                       day = atoi(token_list[i]);
+                       break;
+               case 1:         //Month
+                       month_str = g_strdup(token_list[i]);
+                       break;
+               case 2:         //Year
+                       year = atoi(token_list[i]);
+                       break;
+               case 3:         //HH
+                       hh = atoi(token_list[i]);
+                       break;
+               case 4:         //MM
+                       mm = atoi(token_list[i]);
+                       break;
+               case 5:         //SS
+                       ss = atoi(token_list[i]);
+                       break;
+               }
+       }
+
+       if (month_str) {
+               i = 0;
+               while (ar_month[i]) {
+                       if (g_ascii_strcasecmp(month_str, ar_month[i]) == 0) {
+                               month = i+1;
+                               break;
+                       }
+                       i++;
+               }
+       }
+
+       EM_DEBUG_LOG_DEV("%d/%d/%d %d:%d:%d", day, month, year, hh, mm, ss);
+
+       datetime = g_date_time_new_utc(year, month, day, hh, mm, ss);
+
+       g_strfreev(token_list);
+       g_free(month_str);
+
+       return datetime;
+}
+
+static gint emcore_compare_uid_elem(gconstpointer a, gconstpointer b)
+{
+       emcore_uid_list *item_a = NULL;
+       emcore_uid_list *item_b = NULL;
+       GDateTime *datetime_a = NULL;
+       GDateTime *datetime_b = NULL;
+       int ret = 0;
+
+       item_a = (emcore_uid_list *)a;
+       item_b = (emcore_uid_list *)b;
+
+       datetime_a = emcore_convert_strftime(item_a->internaldate);
+       datetime_b = emcore_convert_strftime(item_b->internaldate);
+
+       ret = g_date_time_compare(datetime_b, datetime_a);
+
+       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)
 {
        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);
@@ -906,6 +1163,11 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
        emcore_uid_list *next_uid_elem = NULL;
        emstorage_mail_tbl_t *mail = NULL;
 
+       emcore_uid_list *elem_item = NULL;
+       GList *item_list = NULL;
+       GList *item_iter = NULL;
+       GList *item_next = NULL;
+
        if (!account || !input_mailbox_tbl || !uid_list) {
                EM_DEBUG_EXCEPTION("account[%p], input_mailbox_tbl[%p], uid_list[%p]", account, input_mailbox_tbl, uid_list);
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -921,17 +1183,24 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
                }
        }
        else {  /*  EMAIL_SERVER_TYPE_IMAP4 */
-               /*  sowmya.kr commented , since imap4_mailbox_get_uids_by_timestamp will fetch mails since last week and not all mails  */
-
-               EM_DEBUG_LOG("calling imap4_mailbox_get_uids");
-               if (!imap4_mailbox_get_uids(stream, uid_list, &err)) {
-                       EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
-                       if (err  != EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
-                               goto FINISH_OFF;
+               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 ) {
+                               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;
+                       }
+               }
+               else {
+                       if(!imap4_mailbox_get_uids(stream, NULL, uid_list, &err)) {
+                               EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
+                               if (err  != EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
+                                       goto FINISH_OFF;
+                       }
                }
        }
 
-       if (!emstorage_get_downloaded_list(input_mailbox_tbl->account_id,
+       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);
@@ -950,36 +1219,72 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
 
        EM_PROFILE_BEGIN(emCoreGetUidsDownloadWhilwLoop);
 
+       /* Sort uid_elem list based on datetime */
+       elem_item = uid_elem;
+
+       while (elem_item) {
+               item_list = g_list_append(item_list, elem_item);
+               elem_item = elem_item->next;
+       }
+
+       if (item_list) {
+               item_list = g_list_sort(item_list, emcore_compare_uid_elem);
+               item_iter = g_list_first(item_list);
+
+               while (item_iter) {
+                       item_next = g_list_next(item_iter);
+                       if (item_next) {
+                               elem_item = item_iter->data;
+                               elem_item->next = (emcore_uid_list *)item_next->data;
+                       } else {
+                               elem_item = item_iter->data;
+                               elem_item->next = NULL;
+                       }
+
+                       item_iter = g_list_next(item_iter);
+               }
+
+               g_list_free(item_list);
+       }
+       /* Sorting completed */
+
        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);
                }
                else {
                        int to_be_downloaded = 1;
 
                        if (limit_count > 0 && uid_count >= limit_count){
-                               /*  EM_DEBUG_LOG("hit the limit[%d] for [%s]", limit_count, uid_elem->uid);              */
+                               /* EM_DEBUG_LOG("hit the limit[%d] for [%s]", limit_count, uid_elem->uid); */
                                to_be_downloaded = 0;
                        }
                        else{
                                for (i = downloaded_uid_count; i > 0; i--) {
-                                       if (downloaded_uids[i - 1].sync_status == EMAIL_SYNC_STATUS_NOT_EXIST_ON_SERVER && !EM_SAFE_STRCMP(uid_elem->uid, downloaded_uids[i - 1].server_uid)) {
-                                               /* The mail exists on server and local storage, so it should not be downloaded, just check seen flag */
-                                               if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen ||
-                                                               downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
-                                                       if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen) {
-                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
-                                                               downloaded_uids[i - 1].flags_seen_field = uid_elem->flag.seen;
-                                                       }
-                                                       if (downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
-                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
-                                                               downloaded_uids[i - 1].flags_flagged_field = uid_elem->flag.flagged;
+                                       if (downloaded_uids[i - 1].sync_status == EMAIL_SYNC_STATUS_NOT_EXIST_ON_SERVER
+                                                       && !EM_SAFE_STRCMP(uid_elem->uid, downloaded_uids[i - 1].server_uid)) {
+                                               if (downloaded_uids[i - 1].mailbox_id != input_mailbox_tbl->mailbox_id) {
+                                                       uid_count--;
+                                               }
+                                               else {
+                                                       /* The mail exists on server and local storage, so it should not be downloaded, just check seen flag */
+                                                       if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen ||
+                                                                       downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
+                                                               if(downloaded_uids[i - 1].flags_seen_field != uid_elem->flag.seen) {
+                                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
+                                                                       downloaded_uids[i - 1].flags_seen_field = uid_elem->flag.seen;
+                                                               }
+                                                               if (downloaded_uids[i - 1].flags_flagged_field != uid_elem->flag.flagged) {
+                                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_FLAG_CHANGED;
+                                                                       downloaded_uids[i - 1].flags_flagged_field = uid_elem->flag.flagged;
+                                                               }
+                                                       } else {
+                                                               downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_EXIST_ON_SERVER;
                                                        }
-                                               } else {
-                                                       downloaded_uids[i - 1].sync_status = EMAIL_SYNC_STATUS_EXIST_ON_SERVER;
                                                }
                                                to_be_downloaded = 0;
                                                break;
@@ -987,7 +1292,7 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
                                }
                        }
 
-                       /*  EM_DEBUG_LOG("Is uid[%s] going to be downloded ? [%d]", uid_elem->uid, to_be_downloaded); */
+                       //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){
@@ -1025,8 +1330,9 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
 
        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) {          /*  deleted on server */
-                       if (!emstorage_get_maildata_by_servermailid(input_mailbox_tbl->mailbox_id, downloaded_uids[i].server_uid, &mail, true, &err)){
+               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);
                                if (err == EMAIL_ERROR_MAIL_NOT_FOUND){
                                        continue;
@@ -1034,15 +1340,16 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
                        }
 
                        if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-                               if (!emcore_delete_mails_from_local_storage(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(NULL);
+                               if (!ret) {
                                        EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage falied - %d", err);
                                        goto FINISH_OFF;
                                }
-                               /* emcore_delete_notification_for_read_mail(mail->mail_id); */
-                               emcore_display_unread_in_badge();
                        }
 
-                       if (!emstorage_remove_downloaded_mail(input_mailbox_tbl->account_id, input_mailbox_tbl->mailbox_name, downloaded_uids[i].server_uid, true, &err)) {   /*  remove uid from uid list */
+                       if (!emstorage_remove_downloaded_mail(multi_user_name, input_mailbox_tbl->account_id, input_mailbox_tbl->mailbox_name, downloaded_uids[i].server_uid, true, &err)) {   /*  remove uid from uid list */
                                EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
                                /* goto FINISH_OFF; */
                        }
@@ -1051,15 +1358,15 @@ static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *acco
                else if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
                        /* set seen flag of downloaded mail */
                        if (downloaded_uids[i].sync_status == EMAIL_SYNC_STATUS_FLAG_CHANGED) {
-                               if (!emcore_set_flags_field(downloaded_uids[i].account_id, &(downloaded_uids[i].local_uid), 1, EMAIL_FLAGS_FLAGGED_FIELD, downloaded_uids[i].flags_flagged_field, &err)) {
+                               if (!emcore_set_flags_field(multi_user_name, downloaded_uids[i].account_id, &(downloaded_uids[i].local_uid), 1, EMAIL_FLAGS_FLAGGED_FIELD, downloaded_uids[i].flags_flagged_field, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                }
 
-                               if (!emcore_set_flags_field(downloaded_uids[i].account_id, &(downloaded_uids[i].local_uid), 1, EMAIL_FLAGS_SEEN_FIELD, downloaded_uids[i].flags_seen_field, &err)) {
+                               if (!emcore_set_flags_field(multi_user_name, downloaded_uids[i].account_id, &(downloaded_uids[i].local_uid), 1, EMAIL_FLAGS_SEEN_FIELD, downloaded_uids[i].flags_seen_field, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
                                }
                                else
-                                       emcore_display_unread_in_badge();
+                                       emcore_display_unread_in_badge(NULL);
                        }
 
 
@@ -1096,7 +1403,7 @@ FINISH_OFF:
 }
 
 /* insert received mail UID to read mail uid table */
-static int emcore_add_read_mail_uid(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);
 
@@ -1114,8 +1421,8 @@ static int emcore_add_read_mail_uid(emstorage_mailbox_tbl_t *input_maibox_data,
 
        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(input_maibox_data->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl, false, &err)) {
+       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)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -1127,7 +1434,7 @@ static int emcore_add_read_mail_uid(emstorage_mailbox_tbl_t *input_maibox_data,
                read_mail_uid.mailbox_id = mailbox_tbl->mailbox_id;
 
        read_mail_uid.local_uid = mail_id;
-       EM_DEBUG_LOG("MAIL ID [%d] LOCAL_UID [%d]", mail_id, read_mail_uid.local_uid);
+
 
        if (server_mailbox_name)
                read_mail_uid.mailbox_name = server_mailbox_name;
@@ -1137,9 +1444,8 @@ static int emcore_add_read_mail_uid(emstorage_mailbox_tbl_t *input_maibox_data,
        read_mail_uid.server_uid = uid;
        read_mail_uid.rfc822_size = rfc822_size;
 
-       if (!emstorage_add_downloaded_mail(&read_mail_uid, false, &err)) {
+       if (!emstorage_add_downloaded_mail(multi_user_name, &read_mail_uid, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_add_downloaded_mail failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -1157,7 +1463,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-int emcore_add_mail_text(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, char *stripped_text, int *err_code)
+int emcore_add_mail_text(char *multi_user_name, emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, char *stripped_text, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], input_new_mail_tbl_data[%p], err_code[%p]", input_maibox_data, input_new_mail_tbl_data, err_code);
 
@@ -1181,7 +1487,7 @@ int emcore_add_mail_text(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_m
        else
                mail_text.body_text = NULL;
 
-       if (!emstorage_add_mail_text(&mail_text, false, &err)) {
+       if (!emstorage_add_mail_text(multi_user_name, &mail_text, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_add_mail_text failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1200,7 +1506,7 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emcore_add_mail_to_mailbox(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);
 
@@ -1219,15 +1525,16 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(emstorage_mailbox_tbl_t *input_maib
        input_new_mail_tbl_data->mailbox_id            = input_maibox_data->mailbox_id;
        input_new_mail_tbl_data->mailbox_type          = input_maibox_data->mailbox_type;
 
-       emstorage_begin_transaction(NULL, NULL, NULL);
+       emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL);
 
        /* Get the Mail_id */
-       if (!emstorage_increase_mail_id(&(input_new_mail_tbl_data->mail_id), false, &err)) {
+       if (!emstorage_increase_mail_id(multi_user_name, &(input_new_mail_tbl_data->mail_id), false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
-       if (emstorage_get_thread_id_of_thread_mails(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 (thread_id == -1){
@@ -1239,43 +1546,53 @@ INTERNAL_FUNC int emcore_add_mail_to_mailbox(emstorage_mailbox_tbl_t *input_maib
                thread_item_count++;
        }
 
-       if (!emstorage_add_mail(input_new_mail_tbl_data, 0, false, &err)) {
+        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);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
-       if (thread_item_count > 1){
-               if (!emstorage_update_latest_thread_mail(input_new_mail_tbl_data->account_id, input_new_mail_tbl_data->thread_id, 0, 0, false, &err)) {
+       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)) {
+                       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)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
-                       emstorage_rollback_transaction(NULL, NULL, NULL);
+                       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)
+               EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
+
        if (output_thread_id)
-               *output_thread_id = input_new_mail_tbl_data->thread_id;
+               *output_thread_id = thread_id;
 
        if (output_mail_id != NULL)
                *output_mail_id = input_new_mail_tbl_data->mail_id;
 
-       EM_DEBUG_LOG("mail_table_data.mail_id [%d]", input_new_mail_tbl_data->mail_id);
-       EM_DEBUG_LOG("mail_table_data.thread_id [%d]", input_new_mail_tbl_data->thread_id);
-
-       if (!emcore_add_read_mail_uid(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(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
 #ifdef __FEATURE_BODY_SEARCH__
-       if (!emcore_add_mail_text(input_maibox_data, input_new_mail_tbl_data, NULL, &err)) {
+       if (!emcore_add_mail_text(multi_user_name, input_maibox_data, input_new_mail_tbl_data, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emcore_add_mail_text failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 #endif
 
-       emstorage_commit_transaction(NULL, NULL, NULL);
+       emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL);
 
 FINISH_OFF:
 
@@ -1349,7 +1666,7 @@ int emcore_check_rule(const char *input_full_address_from, const char *input_sub
                                        else if (RULE_TYPE_EXACTLY == rule[i].flag2) {
                                                if (!strcasecmp(input_subject, rule[i].value))
                                                        *blocked = true;
-                                       }      
+                                       }
                                }
                        }
 
@@ -1363,9 +1680,17 @@ int emcore_check_rule(const char *input_full_address_from, const char *input_sub
                                                if (!strcasecmp(from_address, rule[i].value2))
                                                        *blocked = true;
                                        }
+#ifdef __FEATURE_COMPARE_DOMAIN__
+                                       else if (RULE_TYPE_COMPARE_DOMAIN == rule[i].flag2) {
+                                               char *domain_start = NULL;
+                                               domain_start = strchr(from_address, '@');
+                                               if (domain_start && strncmp(domain_start, rule[i].value2, strlen(rule[i].value2)) == 0)
+                                                       *blocked = true;
+                                       }
+#endif /* __FEATURE_COMPARE_DOMAIN__ */
                                }
                        }
-               
+
                        break;
 
                default:
@@ -1376,11 +1701,11 @@ int emcore_check_rule(const char *input_full_address_from, const char *input_sub
                                if (RULE_TYPE_INCLUDES == rule[i].flag2) {
                                        if (strcasestr(from_address, rule[i].value2))
                                                *priority_sender = 1;
-                               }      
+                               }
                                else if (RULE_TYPE_EXACTLY == rule[i].flag2) {
-                                       if (!strcasecmp(from_address, rule[i].value2)) 
+                                       if (!strcasecmp(from_address, rule[i].value2))
                                                *priority_sender = 1;
-                               }       
+                               }
                        }
                        break;
                }
@@ -1404,7 +1729,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code)
+INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(char *multi_user_name, int account_id, MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code)
 {
        EM_PROFILE_BEGIN(emCoreParseEnvelope);
        EM_DEBUG_FUNC_BEGIN("input_envelope[%p], input_uid_elem [%p], output_mail_tbl_data[%p],  err_code[%p]", input_envelope, input_uid_elem, output_mail_tbl_data, err_code);
@@ -1421,7 +1746,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
        char *rfc822_subject = NULL;
        char *rfc822_from = NULL;
 
-       account_ref = emcore_get_account_reference(account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, account_id);
        if (!account_ref) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -1467,7 +1792,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
        if (rfc822_subject) {
                EM_SAFE_FREE(input_envelope->subject);
                input_envelope->subject = g_strdup(rfc822_subject);
-               EM_DEBUG_LOG("rfc822_subject:%s", input_envelope->subject);
+               EM_DEBUG_LOG_SEC("rfc822_subject:%s", input_envelope->subject);
                EM_SAFE_FREE(rfc822_subject);
        }
 
@@ -1475,14 +1800,31 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
                if (input_envelope->from) {
                        EM_SAFE_FREE(input_envelope->from->personal);
                        input_envelope->from->personal = g_strdup(rfc822_from);
-                       EM_DEBUG_LOG("rfc822_from:%s", input_envelope->from->personal);
+                       EM_DEBUG_LOG_SEC("rfc822_from:%s", input_envelope->from->personal);
                }
                EM_SAFE_FREE(rfc822_from);
        }
 
        if (input_envelope->subject) {
-               temp_mail_tbl_data->subject = emcore_decode_rfc2047_text(input_envelope->subject, &err);
-               EM_DEBUG_LOG("subject[%s]", temp_mail_tbl_data->subject);
+               temp_mail_tbl_data->subject = emcore_gmime_get_decoding_text(input_envelope->subject);
+               em_trim_left(temp_mail_tbl_data->subject);
+
+               char *charset = NULL;
+               char *charset_end = NULL;
+               char *charset_start = NULL;
+               if(g_str_has_prefix(input_envelope->subject, "=?")) {
+                       charset_start = input_envelope->subject + 2;
+                       charset_end = strstr(charset_start, "?");
+                       if (charset_end) {
+                               charset = g_strndup(charset_start, charset_end - charset_start);
+                       }
+               }
+
+               EM_DEBUG_LOG("DEFAULT CHARSET : %s", charset);
+               if (charset) {
+                       temp_mail_tbl_data->default_charset = g_ascii_strup(charset, -1);
+                       EM_SAFE_FREE(charset);
+               }
        }
 
        if (input_envelope->from) {
@@ -1491,7 +1833,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
                        goto FINISH_OFF;
                }
 
-               EM_DEBUG_LOG_SEC("full_address_from[%s]", temp_mail_tbl_data->full_address_from);
+
        }
 
        if (input_envelope->to) {
@@ -1500,7 +1842,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
                        goto FINISH_OFF;
                }
 
-               EM_DEBUG_LOG_SEC("full_address_to[%s]", temp_mail_tbl_data->full_address_to);
+
        }
 
        if (input_envelope->cc) {
@@ -1542,7 +1884,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
 
        memset((void*)&temp_time_info, 0,  sizeof(struct tm));
 
-       EM_DEBUG_LOG("tm_hour[%d] zhours [%d] tm_min[%d] zminutes[%d] zoocident[%d]", mail_cache_element->hours, mail_cache_element->zhours,  mail_cache_element->minutes, mail_cache_element->zminutes, mail_cache_element->zoccident);
+
 
        zone_hour = mail_cache_element->zhours * (mail_cache_element->zoccident?-1:1);
 
@@ -1579,7 +1921,7 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
        temp_mail_tbl_data->eas_data              = NULL;
 
        temp_mail_tbl_data->account_id            = account_id;
-       emcore_fill_address_information_of_mail_tbl(temp_mail_tbl_data);
+       emcore_fill_address_information_of_mail_tbl(multi_user_name, temp_mail_tbl_data);
 
        *output_mail_tbl_data = temp_mail_tbl_data;
        temp_mail_tbl_data = NULL;
@@ -1588,8 +1930,12 @@ INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILST
 
 FINISH_OFF:
 
-       if (ret != true)
-               EM_SAFE_FREE(temp_mail_tbl_data);
+       if (ret != true) {
+               if (temp_mail_tbl_data) {
+                       emstorage_free_mail(&temp_mail_tbl_data, 1, NULL);
+                       temp_mail_tbl_data = NULL;
+               }
+       }
 
        if (account_ref) {
                emcore_free_account(account_ref);
@@ -1604,8 +1950,17 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl, void **stream, 
-                                     emcore_uid_list **input_uid_list, int *mail_count, int *unread_mail, int *err_code)
+INTERNAL_FUNC int emcore_sync_header (char *multi_user_name, 
+                                       emstorage_mailbox_tbl_t *input_mailbox_tbl, 
+                                       void **stream, 
+                                       emcore_uid_list **input_uid_list, 
+                                       int *mail_count, 
+                                       int *unread_mail,
+                                       int *vip_mail_count,
+                                       int *vip_unread_mail,
+                                       int  cancellable, /*0: excluding event thd*/
+                                       int  event_handle,
+                                       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);
@@ -1619,7 +1974,6 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
        int ret = false;
        int err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
        int err_from_vconf = 0;
-       int status = EMAIL_LIST_FAIL;
        int download_limit_count;
        email_account_t      *account_ref = NULL;
        emstorage_rule_tbl_t *rule = NULL;
@@ -1629,7 +1983,8 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
        emstorage_mailbox_tbl_t *destination_mailbox = NULL;
 
        ENVELOPE *env = NULL;
-       int account_id = 0, mail_id = 0, rule_count = 1000, total = 0, unread = 0, i = 0, percentage  = 0, thread_id = -1;
+       int account_id = 0, mail_id = 0, rule_count = 1000, i = 0, percentage  = 0, thread_id = -1;
+       int total = 0, unread = 0, vip_total = 0, vip_unread = 0;
        char *uid_range = NULL;
        char mailbox_id_param_string[10] = {0,};
 
@@ -1641,47 +1996,63 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
 
        account_id = input_mailbox_tbl->account_id;
        g_current_sync_account_id = account_id;
-       account_ref = emcore_get_account_reference(account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, account_id);
        if (!account_ref) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if(account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && input_mailbox_tbl->local_yn == 1) {
-               EM_DEBUG_EXCEPTION("local_yn flag is false for this mailbox");
-               err = EMAIL_ERROR_INVALID_MAILBOX;
+       if (account_ref->sync_disabled == 1) {
+               err = EMAIL_ERROR_ACCOUNT_SYNC_IS_DISALBED;
+               EM_DEBUG_LOG("Sync disabled for this account. Do not sync.");
                goto FINISH_OFF;
        }
 
-       FINISH_OFF_IF_CANCELED;
+
+       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) {
+                       emcore_sync_mailbox_list (multi_user_name, account_id, "", event_handle, &err);
+                       if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list error [%d] account_id [%d]", err, account_id);
+                       }
+               }
+               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 );
+                       err = EMAIL_ERROR_INVALID_MAILBOX;
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
        if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 1)) != 0 ) {
-               EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
+               EM_DEBUG_EXCEPTION("vconf_set_int failed (sync state) [%d]", err_from_vconf);
        }
 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
 
-       if (!emcore_connect_to_remote_mailbox(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);
-               status = EMAIL_LIST_CONNECTION_FAIL;
-               err = EMAIL_ERROR_CONNECTION_FAILURE;
                goto FINISH_OFF;
        }
 
-       FINISH_OFF_IF_CANCELED;
+       if (cancellable)
+               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
        /*  save total mail count on server to DB */
-       if (!emstorage_update_mailbox_total_count(account_id, input_mailbox_tbl->mailbox_id, ((MAILSTREAM *)*stream)->nmsgs, 1, &err)){
+       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("block_address = %d, block_subject = %d", opt_ref->block_address, opt_ref->block_subject);
+               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;
@@ -1692,20 +2063,21 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
                        else if (!opt_ref->block_subject)
                                type = EMAIL_FILTER_FROM;
 
-                       if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err) || !rule) {
+                       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);
                        }
                }
+
                download_limit_count = input_mailbox_tbl->mail_slot_size;
-               if (!emcore_get_uids_to_download(*stream, account_ref, input_mailbox_tbl, download_limit_count, &uid_list, 
+               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);
-                       uid_list = NULL;
                        goto FINISH_OFF;
                }
 
-               FINISH_OFF_IF_CANCELED;
+               if (cancellable)
+                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
                if (input_uid_list && *input_uid_list){
                        emcore_free_uids(*input_uid_list, NULL);
@@ -1716,76 +2088,20 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
                EM_PROFILE_BEGIN(emCoreSyncHeaderwhileloop);
 
 #ifdef  __FEATURE_HEADER_OPTIMIZATION__
-               /* g.shyamakshi@samsung.com : Bulk fetch of headers only if the recieving 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);
-               if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list  != NULL){
-                       emcore_uid_list *uid_list_prev = NULL;
-                       emcore_uid_list *uid_list_fast = uid_list;
-                       int index = 0;
-                       int msg_count = total;
-                       int uid_range_size = msg_count * 8 + 1000;
-
-                       EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
-                       uid_range = malloc(sizeof(char) * uid_range_size);
-
-                       if (uid_range == NULL){
-                               EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
-                               err  = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
-
-                       uid_list_prev = uid_list_fast;
 
-                       if (uid_list_fast->next == NULL){
-                               /* Single list entry */
-                               snprintf(uid_range, uid_range_size, "%d", atoi(uid_list_fast->uid));
-                       }
-                       else{
-                               /* forming range of uids to be passed */
-                               while (uid_list_fast  != NULL){
-                                       /* uid_list_fast = uid_list_fast->next; */
-
-                                       if ((uid_list_fast->next != NULL) && (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1)){
-                                               index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
-
-                                               uid_list_fast = uid_list_fast->next;
-                                               uid_list_prev = uid_list_fast;
-
-                                               /* to make UID range string "abc, XX : YY" */
-                                               while (uid_list_fast  != NULL){
-                                                       if (uid_list_fast->next == NULL)
-                                                               break;
-                                                       if (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1){
-                                                               uid_list_fast = uid_list_fast->next;
-                                                               uid_list_prev = uid_list_fast;
-                                                       }
-                                                       else
-                                                               break;
-                                               }
-                                               if ((uid_list_fast  != NULL) && (uid_list_fast->next  != NULL))
-                                                       index  += snprintf(uid_range+index, uid_range_size, ":%d,", atoi(uid_list_prev->uid));
-                                               else
-                                                       index  += snprintf(uid_range+index, uid_range_size, ":%d", atoi(uid_list_prev->uid));
+               if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list  != NULL) {
 
-                                               uid_list_fast = uid_list_fast->next;
-                                               uid_list_prev = uid_list_fast;
-                                       }
-                                       else{
-                                               if (uid_list_fast->next  != NULL)
-                                                       index  += snprintf(uid_range+index, uid_range_size, "%d,", atoi(uid_list_prev->uid));
-                                               else
-                                                       index  += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
-                                               uid_list_fast = uid_list_fast->next;
-                                               uid_list_prev = uid_list_fast;
-                                       }
-                               }
+                       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;
                        }
 
-                       FINISH_OFF_IF_CANCELED;
-
-                       EM_DEBUG_LOG("index [%d]", index);
+                       if (cancellable)
+                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
                        uid_elem = uid_list;
                        if (*stream && uid_elem){
@@ -1817,11 +2133,11 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
 
                        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);
+                       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);
@@ -1831,24 +2147,32 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
                                env = mail_fetchstructure_full (*stream, uid_elem->msgno, NULL, FT_PEEK);
 #endif
                        }
-                       FINISH_OFF_IF_CANCELED;
+
+                       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 (!emcore_make_mail_tbl_data_from_envelope (account_id, *stream, env, uid_elem, 
+                               if (!emcore_make_mail_tbl_data_from_envelope (multi_user_name, account_id, *stream, env, uid_elem, 
                                                                      &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
                                        EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
                                        goto CONTINUE_NEXT;
                                }
 
                                /* Check message_id for duplicated mail in sentbox */
-                               if ((input_mailbox_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_SENTBOX) && (emstorage_check_and_update_server_uid_by_message_id(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)) {
+                               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(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)) {
+                                       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");
                                        }
 
@@ -1862,97 +2186,139 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
                                if(destination_mailbox) /* cleanup before reusing */
                                        emstorage_free_mailbox(&destination_mailbox, 1, NULL);
 
-                               if (priority_sender)
+                               if (priority_sender) {
                                        new_mail_tbl_data->tag_id = PRIORITY_SENDER_TAG_ID;
+                                       vip_total ++;
+                               }
 
-                               if (blocked) {
-                                       EM_DEBUG_LOG("mail[%d] added to spambox", mail_id);
-                                       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, 
-                                                                                  &destination_mailbox, false, &err)) {
-                                               EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type 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;
                                }
 
+                               /* Set the noti waited */
+                               new_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_NOTI_WAITED;
+
                                if (destination_mailbox) {
-                                       if ((err = emcore_add_mail_to_mailbox(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;
+                                       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 (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
-                                               if (!emcore_move_mail_on_server(account_id, input_mailbox_tbl->mailbox_id,  &mail_id, 
-                                                                           1, destination_mailbox->mailbox_name, &err)){
-                                                       EM_DEBUG_EXCEPTION("emcore_move_mail_on_server falied [%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;
                                                }
+
                                        }
+                                       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 (input_mailbox_tbl, new_mail_tbl_data, 
-                                                                          &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
+                                       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;
                                        }
 
-                                       /*h.gahlaut :  Start partial body dowload using partial body thread only for IMAP accounts*/
 #ifndef __FEATURE_PARTIAL_BODY_FOR_POP3__
                                        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 (*stream, account_id, mail_id, uid_elem->uid, 
-                                                                                  input_mailbox_tbl->mailbox_id, &err))
+                                                       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__
                                        }
 #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);
+                                       }
+#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);
 */
-                                       FINISH_OFF_IF_CANCELED;
+                                       if (cancellable)
+                                               FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-                                       if (!uid_elem->flag.seen)
+                                       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, 
+                                       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");
                                }
 
+                               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");
+
 #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 (new_mail_tbl_data->email_address_sender, 
+                                       if ((err = emcore_check_blocking_mode (multi_user_name, new_mail_tbl_data->email_address_sender, 
                                                                                 &blocking_mode)) != EMAIL_ERROR_NONE) {
                                                EM_DEBUG_EXCEPTION ("emcore_check_blocking_mode failed : [%d]", err);
                                        }
 
                                        if (blocking_mode)
-                                               emcore_set_blocking_mode_status (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(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
+                                       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(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
+                                       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;
@@ -1964,7 +2330,8 @@ INTERNAL_FUNC int emcore_sync_header (emstorage_mailbox_tbl_t *input_mailbox_tbl
                                /*  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 */
 
-                               FINISH_OFF_IF_CANCELED;
+                               if (cancellable)
+                                       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
                        }
 
 CONTINUE_NEXT:
@@ -1988,20 +2355,26 @@ FINISH_OFF:
        if (unread_mail != NULL)
                *unread_mail = unread;
 
+       if (vip_mail_count != NULL)
+               *vip_mail_count = vip_total;
+
+       if (vip_unread_mail != NULL)
+               *vip_unread_mail = vip_unread;
+
        if (account_ref) {
                emcore_free_account(account_ref);
                EM_SAFE_FREE(account_ref);
        }
 
-       if (!emcore_remove_overflowed_mails(input_mailbox_tbl, &err_2))
+       if (!emcore_remove_overflowed_mails(multi_user_name, input_mailbox_tbl, &err_2))
                EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed - %d", err_2);
 
        if (ret && input_mailbox_tbl)
-               emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_tbl->mailbox_id, 1);
+               emstorage_stamp_last_sync_time_of_mailbox(multi_user_name, input_mailbox_tbl->mailbox_id, 1);
 
 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
        if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 0)) != 0 ) {
-               EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
+               EM_DEBUG_EXCEPTION("vconf_set_int failed (sync state) [%d]", err_from_vconf);
        }
 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
 
@@ -2053,7 +2426,7 @@ emcore_uid_list *__ReverseList(emcore_uid_list *uid_list)
 
 
 
-int emcore_download_uid_all(MAILSTREAM *mail_stream, email_internal_mailbox_t *mailbox, emcore_uid_list** uid_list, int *total, emstorage_read_mail_uid_tbl_t *downloaded_uids, int count, emcore_get_uids_for_delete_t for_delete, int *err_code)
+int emcore_download_uid_all(char *multi_user_name, MAILSTREAM *mail_stream, email_internal_mailbox_t *mailbox, emcore_uid_list** uid_list, int *total, emstorage_read_mail_uid_tbl_t *downloaded_uids, int count, emcore_get_uids_for_delete_t for_delete, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid_list[%p], total[%p], downloaded_uids[%p], count[%d], for_delete[%d], err_code[%p]", mailbox, uid_list, total, downloaded_uids, count, for_delete, err_code);
 
@@ -2075,7 +2448,7 @@ int emcore_download_uid_all(MAILSTREAM *mail_stream, email_internal_mailbox_t *m
                goto FINISH_OFF;
        }
 
-       if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", mailbox->account_id);
 
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -2083,7 +2456,7 @@ int emcore_download_uid_all(MAILSTREAM *mail_stream, email_internal_mailbox_t *m
        }
 
        if (!mail_stream) {
-               if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)){
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)){
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed...");
 
                        goto FINISH_OFF;
@@ -2145,7 +2518,7 @@ int emcore_download_uid_all(MAILSTREAM *mail_stream, email_internal_mailbox_t *m
                        if (*p == '.')
                                break;
 
-                       /* EM_DEBUG_LOG("UID list [%s]", p); */
+                       /*EM_DEBUG_LOG("UID list [%s]", p);*/
 
                        uid_elem = (emcore_uid_list *)malloc(sizeof(emcore_uid_list));
                        if (!uid_elem) {
@@ -2240,7 +2613,7 @@ int emcore_download_uid_all(MAILSTREAM *mail_stream, email_internal_mailbox_t *m
                        }
 
                        if (for_delete == EM_CORE_GET_UIDS_FOR_NO_DELETE) {
-                               if ((ref_account->retrieval_mode == EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (strstr(p, "\\Seen"))) {
+                               if ((ref_account->retrieval_mode & EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (strstr(p, "\\Seen"))) {
                                        EM_SAFE_FREE (p);
                                        continue;
                                }
@@ -2352,7 +2725,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
+int emcore_download_imap_msgno(char *multi_user_name, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid[%p], msgno[%p], err_code[%p]", mailbox, uid, msgno, err_code);
 
@@ -2372,7 +2745,7 @@ int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int
                goto FINISH_OFF;
        }
 
-       if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
 
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -2385,7 +2758,7 @@ int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int
        }
 
        if (!mailbox->mail_stream) {
-               if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
 
                        goto FINISH_OFF;
@@ -2557,6 +2930,7 @@ int emcore_free_uids(emcore_uid_list *uid_list, int *err_code)
                p = uid_list;
                uid_list = uid_list->next;
                EM_SAFE_FREE (p->uid);
+               EM_SAFE_FREE (p->internaldate);
                EM_SAFE_FREE (p);
        }
 
@@ -2570,7 +2944,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC char *emcore_guess_charset(char *source_string)
+INTERNAL_FUNC char *emcore_guess_charset(char *multi_user_name, char *source_string)
 {
        EM_DEBUG_FUNC_BEGIN("source - %s", source_string);
 
@@ -2581,8 +2955,10 @@ INTERNAL_FUNC char *emcore_guess_charset(char *source_string)
        int i = 0;
        int confidence = 0;
        int most_confidence = 0;
-       const char *detected_charset = NULL;
+       char *temp_charset = NULL;
        char *uscdet_result_charset = NULL;
+       const char *charset = NULL;
+       const char *detected_charset = NULL;
 
        char *ret = NULL;
        email_account_t *account_ref = NULL;
@@ -2593,10 +2969,19 @@ INTERNAL_FUNC char *emcore_guess_charset(char *source_string)
        result_charset   = (CHARSET*)utf8_infercharset(&source_text);
 
        if(result_charset) {
-               EM_DEBUG_LOG_SEC("return_charset->name [%s]", result_charset->name);
                return EM_SAFE_STRDUP(result_charset->name);
        }
 
+       time_t t = time(0);
+       struct tm *data = localtime(&t);
+
+       for (i = 0 ; i < G_N_ELEMENTS(known_zone_charset); i++) {
+               if (!g_ascii_strcasecmp(known_zone_charset[i].tm_zone, data->tm_zone)) {
+                       temp_charset = strdup(known_zone_charset[i].charset);
+                       break;
+               }
+       }
+
        detector = ucsdet_open(&err);
        if(U_FAILURE(err))
                EM_DEBUG_LOG("ucsdet_open failed");
@@ -2614,13 +2999,18 @@ INTERNAL_FUNC char *emcore_guess_charset(char *source_string)
                if(U_FAILURE(err))
                        EM_DEBUG_LOG("ucsdet_getConfidence failed");
 
+               charset = ucsdet_getName(match[i], &err);
                if(U_FAILURE(err))
                        EM_DEBUG_LOG("ucsdet_getName failed");
 
                if (most_confidence < confidence) {
                        most_confidence = confidence;
-                       detected_charset = ucsdet_getName(match[i], &err);
+                       detected_charset = charset;
+               } else if (most_confidence == confidence) {
+                       if (temp_charset && !strcasecmp(charset, temp_charset))
+                               detected_charset = charset;
                }
+
                EM_DEBUG_LOG_DEV("UCSDET DETECTED CHARSET:%s, %d", ucsdet_getName(match[i], &err), confidence);
        }
 
@@ -2629,10 +3019,12 @@ INTERNAL_FUNC char *emcore_guess_charset(char *source_string)
 
        ucsdet_close(detector);
 
+       EM_SAFE_FREE(temp_charset);
+
        if (uscdet_result_charset)
                return uscdet_result_charset;
 
-       account_ref = emcore_get_account_reference(g_current_sync_account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, g_current_sync_account_id);
        if (!account_ref) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d",
                                g_current_sync_account_id);
@@ -2664,7 +3056,7 @@ void mail_appenduid(char *mailbox, unsigned long uidvalidity, SEARCHSET *set)
     EM_DEBUG_LOG("append uid - %s", g_append_uid_rsp);
 }
 
-INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
+INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(char *multi_user_name, int mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id [%d]", mail_id);
 
@@ -2676,22 +3068,26 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
        char                       *long_enc_path        = NULL;
        char                       *data                 = NULL;
        char                        set_flags[100]       = {0,};
+       char                        message_size[100]    = {0,};
        ENVELOPE                   *envelope             = NULL;
        FILE                       *fp                   = NULL;
        STRING                     str;
+       STRING                     str_data;
        MAILSTREAM                 *stream               = NULL;
        email_account_t            *account_ref          = NULL;
        emstorage_mail_tbl_t       *mail_table_data      = NULL;
        emstorage_attachment_tbl_t *attachment_tbl_data  = NULL;
        emstorage_mailbox_tbl_t    *mailbox_tbl          = NULL;
+       int max_alloc_size = 40960; /*40K*/
+       int alloc_size = 0;
 
        /*  get a mail from mail table */
-       if (!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(mail_table_data->account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, mail_table_data->account_id);
 
        if(account_ref == NULL || account_ref->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("This account doesn't support sync");
@@ -2699,13 +3095,14 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_attachment_list(mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("mailbox_id [%d]", mail_table_data->mailbox_id);
-       if ((err = emstorage_get_mailbox_by_id(mail_table_data->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl){
+
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail_table_data->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl){
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -2716,7 +3113,7 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
+       if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
                EM_DEBUG_EXCEPTION(">>emcore_get_long_encoded_path  :  Failed [%d] ", err);
                goto FINISH_OFF;
        }
@@ -2726,15 +3123,15 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
            goto FINISH_OFF;
        }
 
-       if (!emcore_make_rfc822_file_from_mail(mail_table_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fname, NULL, &err)){
+       if (!emcore_make_rfc822_file_from_mail(multi_user_name, mail_table_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fname, NULL, &err)){
            EM_DEBUG_EXCEPTION(" emcore_make_rfc822_file_from_mail failed [%d]", err);
            goto FINISH_OFF;
        }
 
-       if (fname){
-           if (!(fp = fopen(fname, "a+"))) {
-               EM_DEBUG_EXCEPTION("fopen failed - %s", fname);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+       if (fname) {
+           err = em_fopen(fname, "a+", &fp);
+           if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed - %s", fname);
                goto FINISH_OFF;
            }
        }
@@ -2759,7 +3156,7 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
 
        rewind(fp);
 
-       if (!emcore_connect_to_remote_mailbox(mail_table_data->account_id, 0, (void **)&stream, &err)){
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, mail_table_data->account_id, 0, (void **)&stream, &err)){
            EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err);
            goto FINISH_OFF;
        }
@@ -2767,27 +3164,100 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
        /* added for copying server UID to DB */
        mail_parameters(stream, SET_APPENDUID, mail_appenduid);
 
-       data = (char *)malloc(len + 1);
-       /* copy data from file to data */
-       read_size = fread(data, sizeof (char), len, fp);
-       if (read_size  != len){
-           /* read faiil. */
-           EM_DEBUG_EXCEPTION("Read from file failed");
-       }
+       if (len < max_alloc_size)
+               alloc_size = len;
+       else
+               alloc_size = max_alloc_size;
+
+       data = (char *)malloc(alloc_size + 1);
+
 
-       INIT(&str, mail_string, data, len);
+       snprintf(message_size, sizeof(message_size), "%d", len);
+       INIT(&str, mail_string, message_size, EM_SAFE_STRLEN(message_size));
 
        sprintf(set_flags, "\\Seen");
 
+       int total_size = len;
+       int data_size = 0;
+       int sent_size = 0;
+
        if (mail_table_data->flags_seen_field){
-               if (!mail_append_full(stream, long_enc_path, set_flags, NULL, &str)) {
-                   EM_DEBUG_EXCEPTION("mail_append  failed -");
-                   err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
-                   goto FINISH_OFF;
+               if (!mail_append_command(stream, long_enc_path, set_flags, NULL, &str)) {
+                       EM_DEBUG_EXCEPTION("mail_append  failed -");
+                       err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               while (total_size > 0) {
+                       if (total_size < max_alloc_size)
+                               data_size = total_size;
+                       else
+                               data_size = max_alloc_size;
+
+                       memset(data, 0x0, data_size+1);
+                       read_size = fread(data, sizeof(char), data_size, fp);
+
+                       if (read_size != data_size) {
+                               /* read fail. */
+                               EM_SAFE_FREE(data);
+                               EM_DEBUG_EXCEPTION("Read from file failed");
+                       }
+                       sent_size += read_size;
+
+                       INIT(&str_data, mail_string, data, read_size);
+                       if (!mail_append_message(stream, long_enc_path, &str_data)) {
+                               EM_DEBUG_EXCEPTION("mail_append  failed -");
+                               err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
+                               goto FINISH_OFF;
+                       } else {
+                               EM_DEBUG_LOG("Sent data Successfully. sent[%d] total[%d]", sent_size, total_size);
+                       }
+                       total_size -= data_size;
+               }
+               EM_SAFE_FREE(data);
+
+               if (!mail_append_end(stream, long_enc_path)) {
+                       EM_DEBUG_EXCEPTION("mail_append  failed -");
+                       err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
+                       goto FINISH_OFF;
                }
        }
        else{
-               if (!mail_append_full(stream, long_enc_path, NULL, NULL, &str)) {
+               if (!mail_append_command(stream, long_enc_path, NULL, NULL, &str)) {
+                       EM_DEBUG_EXCEPTION("mail_append  failed -");
+                       err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               while (total_size > 0) {
+                       if (total_size < max_alloc_size)
+                               data_size = total_size;
+                       else
+                               data_size = max_alloc_size;
+
+                       memset(data, 0x0, data_size+1);
+                       read_size = fread(data, sizeof(char), data_size, fp);
+
+                       if (read_size != data_size) {
+                               /* read fail. */
+                               EM_SAFE_FREE(data);
+                               EM_DEBUG_EXCEPTION("Read from file failed");
+                       }
+                       sent_size += read_size;
+
+                       INIT(&str_data, mail_string, data, read_size);
+                       if (!mail_append_message(stream, long_enc_path, &str_data)) {
+                               EM_DEBUG_EXCEPTION("mail_append  failed -");
+                               err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
+                               goto FINISH_OFF;
+                       } else {
+                               EM_DEBUG_LOG_DEV("Sent data Successfully. sent[%d] total[%d]", sent_size, len);
+                       }
+                       total_size -= data_size;
+               }
+               EM_SAFE_FREE(data);
+
+               if (!mail_append_end(stream, long_enc_path)) {
                        EM_DEBUG_EXCEPTION("mail_append  failed -");
                        err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
                        goto FINISH_OFF;
@@ -2795,16 +3265,16 @@ INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
        }
 
        /* Update read_mail_uid tbl */
-       if (!emcore_add_read_mail_uid(mailbox_tbl, mailbox_tbl->mailbox_name, mail_table_data->mail_id, g_append_uid_rsp, mail_table_data->mail_size, 0, &err)) {
+       if (!emcore_add_read_mail_uid(multi_user_name, mailbox_tbl, mailbox_tbl->mailbox_name, mail_table_data->mail_id, g_append_uid_rsp, mail_table_data->mail_size, 0, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_add_read_mail_uid failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
        /* Update mail_data tbl */
-       if (!emstorage_update_server_uid(mail_table_data->server_mail_id, g_append_uid_rsp, &err)) {
+       if (!emstorage_update_server_uid(multi_user_name, mail_table_data->server_mail_id, g_append_uid_rsp, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed : [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
@@ -2866,7 +3336,7 @@ FINISH_OFF:
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int input_maibox_id, int *err_code)
+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)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_id[%d], uid[%p], input_maibox_id[%d]", account_id, mail_id, uid, input_maibox_id);
 
@@ -2881,7 +3351,7 @@ static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id,
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, account_id);
 
        email_event_partial_body_thd pbd_event;
 
@@ -2893,15 +3363,16 @@ static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id,
        else
                pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_IMAP4;
 
-       if ( (err = emstorage_get_mailbox_by_id(input_maibox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
+       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.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.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);
@@ -2947,6 +3418,9 @@ FINISH_OFF:
                EM_SAFE_FREE(account_ref);
        }
 
+       if (pbd_event.mailbox_name)
+               EM_SAFE_FREE(pbd_event.mailbox_name);
+
        if (NULL  != err_code)
                *err_code = err;
 
@@ -2954,14 +3428,16 @@ FINISH_OFF:
        return ret;
 }
 
+#ifndef __FEATURE_USE_GMIME__
 static int emcore_parse_bodystructure(void *stream, IMAPPARSEDREPLY *reply_from_server, char *bodystructure, BODY **body, struct _m_content_info **cnt_info, int *total_mail_size)
 {
        EM_DEBUG_FUNC_BEGIN("stream:[%p], reply_from_server:[%p], bodystructure:[%p]", stream, reply_from_server, bodystructure);
 
        int err = EMAIL_ERROR_NONE;
+       char* ptr = NULL;
 
        if (!stream || !reply_from_server || !bodystructure || !cnt_info || !body || !total_mail_size) {
-               EM_DEBUG_EXCEPTION("Invalid paramter stream[%p] reply_from_server[%p] bodystructure[%p] cnt_info[%p] body[%p] total_mail_size[%p]", 
+               EM_DEBUG_EXCEPTION("Invalid paramter stream[%p] reply_from_server[%p] bodystructure[%p] cnt_info[%p] body[%p] total_mail_size[%p]",
                                        stream, reply_from_server, bodystructure, cnt_info, body, total_mail_size);
                err = EMAIL_ERROR_INVALID_PARAM;
                return err;
@@ -2982,8 +3458,7 @@ static int emcore_parse_bodystructure(void *stream, IMAPPARSEDREPLY *reply_from_
        }
 
        bodystructure_start = bodystructure_start + strlen("BODYSTRUCTURE");
-       bodystructure_string = strdup(bodystructure_start);
-       char* ptr = bodystructure_string;
+       ptr = bodystructure_string = strdup(bodystructure_start);
 
        EM_DEBUG_LOG_DEV("BODYSTRUCTURE:%s", bodystructure_string);
 
@@ -3014,7 +3489,7 @@ static int emcore_parse_bodystructure(void *stream, IMAPPARSEDREPLY *reply_from_
                goto FINISH_OFF;
        }
 
-       if (emcore_get_body(stream, 0, 0, 0, p_body, p_cnt_info, &err) < 0 || !p_cnt_info) {
+       if (emcore_get_body(stream, 0, 0, 0, p_body, NULL, p_cnt_info, &err) < 0 || !p_cnt_info) {
                EM_DEBUG_EXCEPTION("emcore_get_body failed[%d]", err);
                err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;
                goto FINISH_OFF;
@@ -3040,8 +3515,16 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("Err:[%d]", err);
        return err;
 }
+#endif
 
-static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_info, int account_id, int mail_id, int mailbox_id, int *output_total_attachment_size, int *output_attachment_count, int *output_inline_attachment_count)
+INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name, 
+                                                                                                                                       struct _m_content_info *cnt_info, 
+                                                                                                                                       int account_id, 
+                                                                                                                                       int mail_id, 
+                                                                                                                                       int mailbox_id, 
+                                                                                                                                       int *output_total_attachment_size, 
+                                                                                                                                       int *output_attachment_count, 
+                                                                                                                                       int *output_inline_attachment_count)
 {
        EM_DEBUG_FUNC_BEGIN("cnt_info : [%p], account_id : [%d], mail_id : [%d], mailbox_id : [%d]", cnt_info, account_id, mail_id, mailbox_id);
        int err = EMAIL_ERROR_NONE;
@@ -3053,11 +3536,6 @@ static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_in
 
        }
 
-       if (!cnt_info->file || !cnt_info->file->name) {
-               EM_DEBUG_LOG("Not include attachment");
-               return err;
-       }
-
        int attachment_count = 0;
        int total_attach_size = 0;
        int inline_attachment_count = 0;
@@ -3069,25 +3547,35 @@ static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_in
        attachment_tbl.mailbox_id = mailbox_id;
        attachment_tbl.attachment_save_status = 0;
 
+#ifdef __FEATURE_USE_GMIME__
+       for (inline_attachment_count = 0, attach_info = cnt_info->inline_file; attach_info; attach_info = attach_info->next) {
+               if (attach_info->type == INLINE_ATTACHMENT) {
+                       inline_attachment_count++;
+               }
+       }
+#endif
+
        for (attachment_count = 0, attach_info = cnt_info->file; attach_info; attach_info = attach_info->next, attachment_count++) {
+
+#ifndef __FEATURE_USE_GMIME__
                if (attach_info->type == INLINE_ATTACHMENT) {
                        EM_DEBUG_LOG("INLINE ATTACHMENT");
                        inline_attachment_count++;
                        continue;
                }
-
+#endif
                total_attach_size                              += attach_info->size;
                attachment_tbl.attachment_size                  = attach_info->size;
                attachment_tbl.attachment_path                  = attach_info->save;
                attachment_tbl.attachment_name                  = attach_info->name;
+               attachment_tbl.content_id                       = attach_info->content_id;
                attachment_tbl.attachment_drm_type              = attach_info->drm;
                attachment_tbl.attachment_mime_type             = attach_info->attachment_mime_type;
 #ifdef __ATTACHMENT_OPTI__
                attachment_tbl.encoding                         = attach_info->encoding;
                attachment_tbl.section                          = attach_info->section;
 #endif
-
-               if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
+               if (!emstorage_add_attachment(multi_user_name, &attachment_tbl, 0, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_add_attachment failed : [%d]", err);
                        goto FINISH_OFF;
                }
@@ -3099,7 +3587,11 @@ static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_in
 FINISH_OFF:
 
        if (output_attachment_count)
+#ifdef __FEATURE_USE_GMIME__
+               *output_attachment_count = attachment_count;
+#else
                *output_attachment_count = attachment_count - inline_attachment_count;
+#endif
 
        if (output_inline_attachment_count)
                *output_inline_attachment_count = inline_attachment_count;
@@ -3110,17 +3602,20 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("err : [%d]", err);
        return err;
 }
-#define UID_RANGE_STRING_LENGTH 3000
-#define TEMP_STRING_LENGTH             50
 
+#define UID_RANGE_STRING_LENGTH          3000
+#define MULTIPART_BOUNDARY_LENGTH        1024
+#define TEMP_STRING_LENGTH                      50
+#define CONTENT_TRANSFER_ENCODING_STRING "Content-Transfer-Encoding"
+
+#ifndef __FEATURE_USE_GMIME__
 static int emcore_parse_html_part_for_partial_body(char *start_header, char *boundary_string, char *bufsendforparse, char *text_html, int body_size)
 {
        EM_DEBUG_FUNC_BEGIN("start_header [%p], boundary_string [%s], bufsendforparse [%s], text_html [%s], body_size [%d]", start_header, boundary_string, bufsendforparse, text_html, body_size);
 
        int   err = EMAIL_ERROR_NONE;
-       int   html_uidno = 0;
        int   iEncodingHeader = 0;
-       int   enc_type = ENCOTHER, dec_len = 0, html_length = 0;
+       int   enc_type = ENCOTHER, dec_len = 0;
        char  EncodingHeader[40] = {0};
        char  Encoding[30] = {0};
        char *pEncodingHeaderEnd = NULL;
@@ -3131,6 +3626,11 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
 
        EM_DEBUG_LOG("Content-Type : text/html or message/rfc822 or text/rfc822-headers");
 
+       if(start_header == NULL) { /*prevent 27448*/
+               EM_DEBUG_EXCEPTION("start_header NULL");
+               return false;
+       }
+
        pHeaderStart = start_header;
        pHeaderStart = pHeaderStart-2;
        do{
@@ -3139,29 +3639,36 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
 
        pHeaderStart++;
 
-       memcpy(EncodingHeader, pHeaderStart, 25);
+       /* Check string size */
+       if (EM_SAFE_STRLEN(pHeaderStart) < strlen(CONTENT_TRANSFER_ENCODING_STRING)) {
+               EM_DEBUG_EXCEPTION("pHeaderStart is truncated.");
+               return false;
+       }
 
-       if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
+       memcpy(EncodingHeader, pHeaderStart, strlen(CONTENT_TRANSFER_ENCODING_STRING));
+
+       if (strcasecmp(EncodingHeader, CONTENT_TRANSFER_ENCODING_STRING) == 0){
                pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
+
+               if ((pEncodingHeaderEnd - (pHeaderStart+27)) > 30) {
+                       EM_DEBUG_EXCEPTION("Encoding is too long");
+                       return false;
+               }
                memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart+27));
                iEncodingHeader = 1;
        }
 
        /* HTML Content found */
-
-       if(!start_header) { /*prevent 27448*/
-               EM_DEBUG_EXCEPTION("start_header NULL");
-               return false;
-       }
        txt_html = start_header;
        txt_html = strstr(txt_html, CRLF_STRING CRLF_STRING);
 
        if (txt_html != NULL){
                txt_html += 4; /*  txt_html points at html content */
                start = txt_html;
-               char multipart_boundary[1600] = {0};
+               char multipart_boundary[MULTIPART_BOUNDARY_LENGTH] = {0};
                char *multipart_related_boundry = NULL;
                char *multipart_related_boundry_end = NULL;
+
                if (iEncodingHeader == 1)
                        multipart_related_boundry = pHeaderStart;
                else
@@ -3173,8 +3680,10 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
                while (bufsendforparse < multipart_related_boundry && *multipart_related_boundry != LF && *multipart_related_boundry != NULL_CHAR)
                        multipart_related_boundry -= 1;
 
-               if ((multipart_related_boundry_end - multipart_related_boundry) > 1600)
+               if ((multipart_related_boundry_end - multipart_related_boundry) > MULTIPART_BOUNDARY_LENGTH)    {
+                       EM_DEBUG_EXCEPTION("Encoding is too long");
                        return false;
+               }
 
                memcpy(multipart_boundary, multipart_related_boundry, multipart_related_boundry_end - multipart_related_boundry);
 
@@ -3187,9 +3696,7 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
 
                if (end == NULL) {
                        EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
-
                        end = txt_html + body_size - (txt_html - bufsendforparse);
-                       html_uidno = 1;
                }
                else if(end == txt_html) { /* empty multipart */
                        EM_DEBUG_LOG("Emtpy HTML multipart");
@@ -3204,7 +3711,6 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
                        else
                                EM_DEBUG_EXCEPTION(" Content not per as grammar.");
                }
-
                EM_DEBUG_LOG("iEncodingHeader [%d]", iEncodingHeader);
 
                if (iEncodingHeader == 1){
@@ -3225,7 +3731,7 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
                        if (temp_enc1)
                                start_header = temp_enc1;
 
-                       start_header += strlen("Content-Transfer-Encoding:");
+                       start_header += strlen(CONTENT_TRANSFER_ENCODING_STRING);
                        start_header = em_skip_whitespace_without_strdup(start_header);
 
                        if (!start_header)
@@ -3243,14 +3749,12 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
 
                                if (emcore_decode_body_text(text_html, end - txt_html, enc_type , &dec_len, &err) < 0)
                                        EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
-                               html_length = dec_len;
                        }
 
                        EM_DEBUG_LOG("Decoded length = %d", dec_len);
                }
                else{
                        memcpy(text_html, start, end-txt_html);
-                       html_length = (end-txt_html);
                }
 
                /* EM_DEBUG_LOG(" Content-Type:  text/html [%s]\n", text_html);                                                          */
@@ -3272,8 +3776,6 @@ static int emcore_parse_html_part_for_partial_body(char *start_header, char *bou
 *Message-ID:  <11512468.945901271910226702.JavaMail.weblogic@epml03>
 */
 
-#define CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding"
-
 static int emcore_parse_plain_part_for_partial_body(char *header_start_string, char *start_header, char *boundary_string, char *bufsendforparse, char *text_plain, int body_size)
 {
        EM_DEBUG_FUNC_BEGIN("header_start_string[%p], start_header[%p], boundary_string [%p], bufsendforparse [%p], text_plain [%p]", header_start_string, start_header, boundary_string, bufsendforparse, text_plain);
@@ -3282,17 +3784,15 @@ static int emcore_parse_plain_part_for_partial_body(char *header_start_string, c
        char *pHeaderStart = NULL, *pEncodingHeaderEnd = NULL;
        char  EncodingHeader[40] = {0, };
        char  Encoding[30] = {0, };
-       char *temp_text_boundary = NULL;
        char *start = NULL, *end = NULL, *txt_plain = NULL, *temp_enc1 = NULL;
 
        EM_DEBUG_LOG("Content-Type : text/plain");
 
        pHeaderStart = header_start_string;
-       temp_text_boundary = start_header;
 
        memcpy(EncodingHeader, pHeaderStart, 25);
 
-       if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
+       if (strcasecmp(EncodingHeader, CONTENT_TRANSFER_ENCODING_STRING) == 0){
                pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
                memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart + 27));
                iEncodingHeader = 1;
@@ -3352,7 +3852,7 @@ static int emcore_parse_plain_part_for_partial_body(char *header_start_string, c
                                if (temp_enc1)
                                        start_header = temp_enc1;
 
-                               start_header += strlen("Content-Transfer-Encoding:");
+                               start_header += strlen(CONTENT_TRANSFER_ENCODING_STRING);
                                start_header = em_skip_whitespace_without_strdup(start_header);
 
                                if (!start_header)
@@ -3428,8 +3928,8 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
        char *image_boundary = NULL;
        char *image_boundary_end = NULL;
        char *temp_image_boundary = NULL;
-       int   i = 0, ch_image = 0, cidno = 0;
-       int   enc_type = ENCOTHER, dec_len = 0, image_length = 0;
+       int   i = 0, ch_image = 0;
+       int   enc_type = ENCOTHER;
        char *p = header_start_string;
        char *start = NULL, *end = NULL, *txt_image = NULL;
        char *temp_image = NULL;
@@ -3465,7 +3965,8 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
        if (image_boundary  != NULL && image_boundary_end != NULL)
                memcpy(temp_image_boundary, image_boundary, image_boundary_end-image_boundary);
 
-       if ((char *)strcasestr((const char *)temp_image_boundary, "Content-type:") == NULL)
+
+       if (((char *)strcasestr((const char *)temp_image_boundary, "Content-type:") == NULL) && (temp_image_boundary[0] == '-'))
                p_boundary_string = strdup(temp_image_boundary);
        else
                p_boundary_string = EM_SAFE_STRDUP(boundary_string);
@@ -3481,19 +3982,17 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                emcore_get_content_type_from_mime_string(start_header, &(image_data[i].mime_type));
                EM_DEBUG_LOG("image_data[i].mime_type : [%s]", image_data[i].mime_type);
 
-               if ((strcasestr(p, "Content-Disposition: attachment")) || (!strcasestr(p, "Content-ID: <"))){
+               if ((strcasestr(p, "Content-Disposition: attachment")) || (!strcasestr(p, "Content-ID: <"))) {
                        EM_DEBUG_LOG("Body has attachment no need to parse ");
                        end = NULL;
                        multiple_image = NULL;
                }
                else {  /*  HTML Content found */
                        ch_image = 0;
-                       cidno = 0;
-                       int   boundarylen = -1;
                        char *cid = NULL;
                        char *temp_name = NULL;
                        char *decoded_filename = NULL;
-                       
+
                        image_data[i].image_file_name = NULL;
 
                        if(!start_header) { /*prevent 27449*/
@@ -3509,30 +4008,31 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                        if (temp_name  != NULL){
                                temp_image = temp_name;
                                if (*(temp_image + 5) == '"') {
-                                       temp_image = temp_image + 5;
+                                       temp_image = temp_image + 6;
                                        temp_name = temp_name + 6 ;
                                } else {
                                        temp_image = temp_image + 5;
                                        temp_name = temp_name + 5;
                                }
 
-                               while (*temp_image  != CR){
+                               while (*temp_image != CR){
                                        temp_image++;
                                        ch_image++;
                                }
-                               
+
                                if (ch_image > 0) {
-                                       image_data[i].image_file_name = em_malloc(ch_image);
+                                       image_data[i].image_file_name = em_malloc(ch_image+3);
                                        if (image_data[i].image_file_name == NULL) {
                                                EM_DEBUG_EXCEPTION("em_malloc failed");
+                                               EM_SAFE_FREE(p_boundary_string);
                                                return false;
                                        }
                                        if (*(temp_image - 1) == '"') {
                                                memcpy(image_data[i].image_file_name, temp_name, ch_image - 2);
                                        } else {
-                                               memcpy(image_data[i].image_file_name, temp_name, ch_image - 1);
+                                               memcpy(image_data[i].image_file_name, temp_name, ch_image);
                                        }
-                               }                               
+                               }
 
                                decoded_filename = emcore_decode_rfc2047_text(image_data[i].image_file_name, &err);
                                if(decoded_filename) {
@@ -3563,6 +4063,7 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                                        image_data[i].image_file_name = em_malloc((cid_end - cid) + 1);
                                        if (image_data[i].image_file_name == NULL) {
                                                EM_DEBUG_EXCEPTION("em_malloc failed");
+                                               EM_SAFE_FREE(p_boundary_string);
                                                return false;
                                        }
                                        memcpy(image_data[i].image_file_name, cid, cid_end - cid);
@@ -3576,7 +4077,6 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                                start = txt_image;
                                end = strstr(txt_image, p_boundary_string);
 
-
                                if (end == NULL){
                                        EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
                                        /*  end points to end of partial body data */
@@ -3584,7 +4084,6 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                                }
                                else{
                                        EM_DEBUG_LOG("This image is fully downloaded");
-                                       boundarylen = EM_SAFE_STRLEN(p_boundary_string);
                                        end -= 2;
                                        image_data[i].fully_downloaded = 1;
                                }
@@ -3593,7 +4092,7 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                                        if (temp_enc1)
                                                start_header = temp_enc1;
 
-                                       start_header  += strlen("Content-Transfer-Encoding:");
+                                       start_header  += strlen(CONTENT_TRANSFER_ENCODING_STRING);
                                        start_header = em_skip_whitespace_without_strdup(start_header);
 
                                        if (!start_header)
@@ -3612,14 +4111,12 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
                                                        memcpy(image_data[i].text_image, start, end-txt_image);
                                                        if (emcore_decode_body_text(image_data[i].text_image, end-txt_image, enc_type , &(image_data[i].dec_len), &err) < 0)
                                                                EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
-                                                       else
-                                                               image_length = image_data[i].dec_len;
                                                }
                                                else
                                                        EM_DEBUG_EXCEPTION("em_malloc() failed");
                                        }
 
-                                       EM_DEBUG_LOG("Decoded length [%d]", dec_len);
+                                       EM_DEBUG_LOG("Decoded length [%d]", image_data[i].dec_len);
                                }
                                else{
                                        image_data[i].text_image = (char *)em_malloc(end-txt_image);
@@ -3642,7 +4139,7 @@ static int emcore_parse_image_part_for_partial_body(char *header_start_string, c
        } while (multiple_image != NULL && donot_parse_next_image != 1 && (i < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
 
        EM_SAFE_FREE(p_boundary_string);
-       
+
        EM_DEBUG_FUNC_END();
        return 1;
 }
@@ -3670,7 +4167,7 @@ static int emcore_find_boundary_string_of_the_part(const char *whole_string, con
                boundary_cur++;
 
                if(boundary_end > boundary_cur && boundary_cur >= whole_string) {
-                       EM_DEBUG_LOG("boundary_end - boundary_cur + 15 [%d]", boundary_end - boundary_cur + 15);
+
                        boundary_string = em_malloc(boundary_end - boundary_cur + 15);
                        if(!boundary_string) {
                                EM_DEBUG_EXCEPTION("em_malloc failed");
@@ -3734,6 +4231,10 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
        char *temp_alternative_plain_header = NULL;
        char *temp_content_type = NULL;
 
+       char *local_encoding_str = NULL;
+       char *local_boundary_str = NULL;
+       char *local_body_str = NULL;
+
        p_body_str = EM_SAFE_STRDUP(body_str);
 
        text_plain = em_malloc(body_size + 1);
@@ -3745,29 +4246,9 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
                goto FINISH_OFF;
        }
 
-       if (!strcasestr(p_body_str, "Content-Type: ") && (cnt_info->text.plain || cnt_info->text.html)) {
-               /*  Encoded Content-Type: text/html or Content-Type: text/plain  */
-               /*  No Partial body has No headers with Content-Type: text/html or Content-Type: text/plain  */
-
-               EM_DEBUG_LOG("mbody->encoding [%d] ", encoding_type);
-
-               if (emcore_decode_body_text(p_body_str, EM_SAFE_STRLEN(p_body_str), encoding_type, &dec_len, &err) < 0) {
-                       EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-
-               EM_DEBUG_LOG("Decoded length [%d]", dec_len);
-
-               if (dec_len > 0) {
-                       if (cnt_info->text.plain) {
-                               memcpy(text_plain, p_body_str, dec_len);
-                       }
-
-                       if (cnt_info->text.html) {
-                               memcpy(text_html, p_body_str, dec_len);
-                       }
-               }
-       } else {   /*  Partial body has headers with Content-Type: text/html or Content-Type: text/plain */
+       if ((cnt_info->text.plain && cnt_info->text.html) || cnt_info->file) {
+               /* Start multipart parsing */
+               /*  Partial body has headers with Content-Type: text/html or Content-Type: text/plain */
                no_alternative_part_flag = 0;
                if (((temp_alternative_plain_header = (char *)strcasestr(p_body_str, "Content-type: multipart/alternative")) != NULL)) {        /*  Found 'alternative' */
                        if (((temp_content_type = (char *)strcasestr(p_body_str, "Content-type: text/plain")) != NULL)) {
@@ -3779,8 +4260,8 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
                                        EM_DEBUG_LOG(" Content-type: multipart/alternative ");
                                        boundary_start = strstr(temp_alternative_plain_header, "--");
                                        if(!boundary_start) { /*prevent 37946 */
-                                               err = EMAIL_ERROR_INVALID_DATA; 
-                                               goto FINISH_OFF; 
+                                               err = EMAIL_ERROR_INVALID_DATA;
+                                               goto FINISH_OFF;
                                        }
                                        boundary_end = strcasestr(boundary_start, "Content-type:");
                                        if(!boundary_end) { /*prevent 37946 */
@@ -3804,13 +4285,13 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
                if (no_alternative_part_flag) {
                        boundary_start = strstr(p_body_str, "--");
                        if(!boundary_start) { /*prevent 37946 */
-                               err = EMAIL_ERROR_INVALID_DATA; 
-                               goto FINISH_OFF; 
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
                        }
-                       boundary_end = strcasestr(boundary_start, "Content-type:");
+                       boundary_end = strcasestr(boundary_start, "\r\n");
                        if(!boundary_end) { /*prevent 37946 */
-                               err = EMAIL_ERROR_INVALID_DATA; 
-                               goto FINISH_OFF; 
+                               err = EMAIL_ERROR_INVALID_DATA;
+                               goto FINISH_OFF;
                        }
 
                        boundary_string = em_malloc(boundary_end - (boundary_start + strlen("--")));
@@ -3825,11 +4306,6 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
 
                if (boundary_string && boundary_end) { /*prevent 37946 */
                        /* EM_DEBUG_LOG("boundary_string : [%s]", boundary_string); */
-                       char *next_boundary_start = NULL;
-                       next_boundary_start = (char *)strcasestr(boundary_end, boundary_string);
-
-                       /*if (next_boundary_start)
-                               boundary_end = next_boundary_start;*/
 
                        if (((start_header = (char *)strcasestr(boundary_end, "Content-Type: text/html"))  != NULL) && (no_html  != 1) &&(((char *)strcasestr(boundary_end, "Content-Type: message/rfc822")) == NULL) &&
                                (((char *)strcasestr(boundary_end, "Content-Type: text/rfc822-headers")) == NULL))
@@ -3864,11 +4340,93 @@ static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_c
                        }
                }
        }
+       else {
+               /*  Encoded Content-Type: text/html or Content-Type: text/plain  */
+               /*  No Partial body has No headers with Content-Type: text/html or Content-Type: text/plain  */
+               char *start_of_body = NULL;
+               char *end_of_body = NULL;
+               char *local_encoding = NULL;
+               char *local_encoding_end = NULL;
+               char *local_boundary_end = NULL;
+               int local_encoding_len = 0;
+               int local_boundary_len = 0;
+               int enc_type = encoding_type;
+               EM_DEBUG_LOG_DEV("mbody->encoding [%d] ", enc_type);
+
+               if (EM_SAFE_STRLEN(p_body_str) > 2 && p_body_str[0] == '-' && p_body_str[1] == '-') {
+
+                       local_boundary_end = strstr(p_body_str, "\r\n");
+                       if (local_boundary_end) {
+                               local_boundary_len = local_boundary_end - p_body_str;
+                               local_boundary_str = em_malloc(local_boundary_len + 2);
+                               memcpy(local_boundary_str, p_body_str+2, local_boundary_len-2);
+                               EM_DEBUG_LOG_DEV("local_boundary_str : %s", local_boundary_str);
+                               end_of_body = strstr(local_boundary_end, local_boundary_str);
+                               if (end_of_body) end_of_body -= 2;
+                       }
+
+                       local_encoding = strcasestr(p_body_str, "Content-Transfer-Encoding: ");
+                       if (local_encoding) {
+                               local_encoding_end = strstr(local_encoding, "\r\n");
+
+                               if (local_encoding && (local_encoding_end > local_encoding)) {
+                                       local_encoding_len = local_encoding_end - local_encoding;
+                                       local_encoding_str = em_malloc(local_encoding_len + 2);
+
+                                       if (local_encoding_str) {
+                                               memcpy(local_encoding_str, local_encoding + EM_SAFE_STRLEN("Content-Transfer-Encoding: "), local_encoding_len);
+
+                                               enc_type = ENCOTHER;
+                                               if (strncasecmp(local_encoding_str, "base64", strlen("base64")) == 0)
+                                                       enc_type = ENCBASE64;
+                                               else if (strncasecmp(local_encoding_str, "quoted-printable", strlen("quoted-printable")) == 0)
+                                                       enc_type = ENCQUOTEDPRINTABLE;
+
+                                               EM_DEBUG_LOG_DEV("local enc_type [%d]", enc_type);
+                                       }
+                               }
+                       }
+
+                       start_of_body = strstr(p_body_str, "\r\n\r\n");
+                       if (start_of_body) {
+                               start_of_body = start_of_body+strlen("\r\n\r\n");
+                               if (end_of_body && (end_of_body > start_of_body)) {
+                                       local_body_str = em_malloc(end_of_body - start_of_body + 2);
+                                       memcpy(local_body_str, start_of_body, end_of_body - start_of_body);
+                               } else {
+                                       local_body_str = EM_SAFE_STRDUP(start_of_body);
+                               }
+                       }
+               }
+
+               if (!local_body_str)
+                       local_body_str = EM_SAFE_STRDUP(p_body_str);
+
+               if (emcore_decode_body_text(local_body_str, EM_SAFE_STRLEN(local_body_str), enc_type, &dec_len, &err) < 0) {
+                       EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG("Decoded length [%d]", dec_len);
+
+               if (dec_len > 0) {
+                       if (cnt_info->text.plain) {
+                               memcpy(text_plain, local_body_str, dec_len);
+                       }
+
+                       if (cnt_info->text.html) {
+                               memcpy(text_html, local_body_str, dec_len);
+                       }
+               }
+       }
 
 FINISH_OFF:
 
        EM_SAFE_FREE(p_body_str);
        EM_SAFE_FREE(boundary_string);
+       EM_SAFE_FREE(local_boundary_str);
+       EM_SAFE_FREE(local_encoding_str);
+       EM_SAFE_FREE(local_body_str);
 
        if (err != EMAIL_ERROR_NONE) {
                EM_SAFE_FREE(text_plain);
@@ -3885,10 +4443,12 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("err : [%d]", err);
        return err;
 }
+#endif
 
 #define TAG_LENGTH 16
 #define COMMAND_LENGTH 2000
-static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
+#ifdef __FEATURE_USE_GMIME__
+static int emcore_gmime_download_imap_partial_mail_body(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("stream [%p], input_download_size[%d], pbd_event [%p], item_count [%d], error [%p]", stream, input_download_size, pbd_event, item_count, error);
 
@@ -3897,14 +4457,16 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
        int i32_index = 0;
        int total_mail_size = 0, total_attachment_size = 0;
        int temp_count = 0, attachment_num = 0, inline_attachment_num = 0;
-       char buf[512] = {0, };
-       char *text_plain    = NULL;
-       char *text_html     = NULL;
-       char uid_range_string_to_be_downloaded[UID_RANGE_STRING_LENGTH] = {0, };
+       int inline_download_count = 0;
+       char path_buf[512] = {0,};
+       char move_buf[512] = {0,};
+       char uid_range_string_to_be_downloaded[UID_RANGE_STRING_LENGTH] = {0,};
        char imap_tag[TAG_LENGTH] = {0, };
        char command[COMMAND_LENGTH] = {0, };
-       char *temp_data_html = NULL;
        char uid_string[TEMP_STRING_LENGTH] = {0, };
+       char rfc822_micalg[TEMP_STRING_LENGTH] = {0, };
+       char rfc822_protocol[TEMP_STRING_LENGTH] = {0, };
+       const char *sender = NULL;
        IMAPLOCAL *imaplocal = NULL;
        IMAPPARSEDREPLY *reply_from_server = NULL;
        emstorage_mail_tbl_t *mail = NULL;
@@ -3913,7 +4475,25 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
        struct _m_content_info *cnt_info = NULL;
        emstorage_attachment_tbl_t attachment_tbl;
        email_event_partial_body_thd *stSectionNo = NULL;
-       email_image_data  *image_data = NULL;
+       emstorage_mail_text_tbl_t *mail_text = NULL; /* prevent */
+
+       GMimeMessage *message1 = NULL;
+       GMimeParser *parser1 = NULL;
+       GMimeStream *stream1 = NULL;
+       char *bodystructure_start = NULL;
+       char *bodystructure_buf = NULL;
+       char *bodystructure_ptr = NULL;
+
+       GMimeStream *stream2 = NULL;
+       GMimeMessage *message2 = NULL;
+       GMimeParser *parser2 = NULL;
+       char *fulltext = NULL;
+
+       GMimePartIter *iter1 = NULL;
+       GMimePartIter *iter2 = NULL;
+       GMimeObject *part_tmp1 = NULL;
+       GMimeObject *part_tmp2 = NULL;
+       char *part_path = NULL;
 
        if (!(stream) || !(imaplocal = stream->local) || !imaplocal->netstream || !pbd_event) {
                EM_DEBUG_EXCEPTION("invalid parameter");
@@ -3927,8 +4507,7 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
        /* For constructing UID list which is having 10 UID or less at a time */
        for (j = 0, stSectionNo = pbd_event; (stSectionNo  != NULL && j < item_count); j++)
        {
-               EM_DEBUG_LOG_SEC("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
-                       j, stSectionNo[j].account_id, stSectionNo[j].mail_id, stSectionNo[j].server_mail_id, stSectionNo[j].activity_id);
+               /* delete log before uploading master branch. it is flooding */
 
                if (i32_index >= UID_RANGE_STRING_LENGTH){
                        EM_DEBUG_EXCEPTION("String length exceeded its limitation!");
@@ -3936,13 +4515,16 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
                }
 
                if (j == item_count - 1)
-                       i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu", stSectionNo[j].server_mail_id);
+                       i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index,
+                                       UID_RANGE_STRING_LENGTH - i32_index, "%lu", stSectionNo[j].server_mail_id);
                else
-                       i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu,", stSectionNo[j].server_mail_id);
+                       i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index,
+                                       UID_RANGE_STRING_LENGTH - i32_index, "%lu,", stSectionNo[j].server_mail_id);
        }
 
        SNPRINTF(imap_tag, TAG_LENGTH, "%08lx", 0xffffffff & (stream->gensym++));
-       SNPRINTF(command, COMMAND_LENGTH, "%s UID FETCH %s (BODYSTRUCTURE BODY.PEEK[TEXT]<0.%d>)\015\012", imap_tag, uid_range_string_to_be_downloaded, input_download_size);
+       SNPRINTF(command, COMMAND_LENGTH, "%s UID FETCH %s (BODYSTRUCTURE RFC822.HEADER BODY.PEEK[TEXT]<0.%d>)\015\012",
+                       imap_tag, uid_range_string_to_be_downloaded, input_download_size);
 
        EM_DEBUG_LOG("command : %s", command);
 
@@ -3954,39 +4536,21 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
        }
 
        /*  responce from the server */
-       imap_response = emcore_get_response_from_server(imaplocal->netstream, imap_tag, &reply_from_server, input_download_size, item_count);
+       imap_response = emcore_get_response_from_server(imaplocal->netstream,
+                       imap_tag, &reply_from_server, input_download_size, item_count);
 
-       if (!imap_response || !reply_from_server ){
+       if (!imap_response || !reply_from_server ) {
                EM_DEBUG_EXCEPTION(" Invalid response from emcore_get_response_from_server");
-               for( i = 0 ; i< item_count ; i++ )
-                       emcore_delete_pbd_activity(pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].activity_id, &err);
-
                goto FINISH_OFF;
        }
 
        for (i = 0; i < item_count ; i++) {
+
                total_mail_size = 0;
                total_attachment_size = 0;
                attachment_num = 0;
 
-               if( !(imap_response[i].header) ) continue;
-
-               if (body) {
-                       mail_free_body(&body);
-                       body = NULL;
-               }
-
-               if (cnt_info) {
-                       emcore_free_content_info(cnt_info);
-                       EM_SAFE_FREE(cnt_info);
-               }
-
-               err = emcore_parse_bodystructure(stream, reply_from_server, imap_response[i].header, &body, &cnt_info, &total_mail_size);
-               if (err != EMAIL_ERROR_NONE || !body) {
-                       EM_DEBUG_EXCEPTION("emcore_parse_bodystructure failed : [%d]", err);
-                       err = EMAIL_ERROR_ON_PARSING;
-                       goto FINISH_OFF;
-               }
+               if(!(imap_response[i].bodystructure) || imap_response[i].bodystructure_len <= 0) continue;
 
                /* Search the account id of pbd_event */
                for (temp_count = 0; temp_count <= item_count && pbd_event[temp_count].server_mail_id != imap_response[i].uid_no; temp_count++)
@@ -4002,175 +4566,423 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
 
                /* 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].mailbox_id, uid_string, &mail, true, &err) || !mail) {
+               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) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND || !mail)
                                goto FINISH_OFF;
                }
 
-               if (mail->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
+               if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
                        EM_DEBUG_LOG("Downloaded mail");
                        continue;
                }
 
-               /* Update the attachment info except inline attachment */
-               if ((err = emcore_update_attachment_except_inline(cnt_info, pbd_event[temp_count].account_id, mail->mail_id, pbd_event[temp_count].mailbox_id, &total_attachment_size, &attachment_num, &inline_attachment_num)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
-                       goto FINISH_OFF;
+               if (cnt_info) {
+                       emcore_free_content_info(cnt_info);
+                       EM_SAFE_FREE(cnt_info);
                }
 
-               EM_DEBUG_LOG("total_mail_size:[%d], total_attachment_size:[%d], attachment_num:[%d]", total_mail_size, total_attachment_size, attachment_num);
-               mail->mail_size            = total_mail_size;
-               mail->attachment_count     = attachment_num;
-               mail->inline_content_count = inline_attachment_num;
-
-               if (imap_response[i].body_len == 0) {
-                       EM_DEBUG_LOG("BODY size is zero");
-                       continue;
+               if (body) {
+                       mail_free_body(&body);
+                       body = NULL;
                }
 
-               /*free the plain, html, and image_data before reusing*/
-               if (image_data) 
-                       emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
+               EM_SAFE_FREE(fulltext);
 
-               EM_SAFE_FREE(text_plain);
-               EM_SAFE_FREE(text_html);
+               if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
 
-               if ((err = emcore_parse_body_for_imap(imap_response[i].body, imap_response[i].body_len, cnt_info, body->encoding, &text_plain, &text_html, &image_data)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emcore_parse_body_for_imap failed");
+               body = mail_newbody();
+               if (body == NULL) {
+                       EM_DEBUG_EXCEPTION("New body creationg failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
 
-               int text_plain_len = EM_SAFE_STRLEN(text_plain);
-               if (text_plain_len > 0) {
-                       memset(buf, 0x00, sizeof(buf));
+               /* Get the body strcuture string */
+               bodystructure_start = strstr(imap_response[i].bodystructure, "BODYSTRUCTURE (");
+               if (!bodystructure_start) {
+                       EM_DEBUG_EXCEPTION("Invalid bodystructure :[%s]", imap_response[i].bodystructure);
+                       err = EMAIL_ERROR_INVALID_PARAM;
+                       goto FINISH_OFF;
+               }
 
-                       if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
-                               EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+               bodystructure_start = bodystructure_start + strlen("BODYSTRUCTURE");
+               bodystructure_ptr = bodystructure_buf = EM_SAFE_STRDUP(bodystructure_start);
+               EM_DEBUG_LOG_DEV("GMIME BODYSTRUCTURE:%s", bodystructure_buf);
 
-                       if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err))
-                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+               mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sparep);
+               imap_parse_body_structure(stream, body,
+                               (unsigned char **)&bodystructure_buf, reply_from_server);
 
-                       if (!emstorage_create_file(text_plain, text_plain_len, buf, &err))
-                               EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
+               EM_SAFE_FREE(bodystructure_ptr);
 
-                       mail->file_path_plain = EM_SAFE_STRDUP(buf);
-                       EM_DEBUG_LOG_SEC("mail->file_path_plain [%s]", mail->file_path_plain);
-               }
+               /* Construct message1(fake gmime message object) with rfc822 header */
+               stream1 = g_mime_stream_mem_new_with_buffer(imap_response[i].rfc822header, imap_response[i].rfc822header_len);
+               parser1 = g_mime_parser_new_with_stream(stream1);
+               if (stream1) g_object_unref(stream1);
 
-               if (image_data != NULL && image_data[0].text_image != NULL && image_data[0].text_image[0] != NULL_CHAR) {
-                       char *result_string_of_replacing = NULL;
-                       int store_file = 0;
-                       int content_index = 0;
+               message1 = g_mime_parser_construct_message(parser1);
+               if (parser1) g_object_unref(parser1);
 
-                       temp_data_html = em_malloc(imap_response[i].body_len + 1);
-                       if(!temp_data_html) {
-                               EM_DEBUG_EXCEPTION("em_malloc failed");
-                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               /* message1 is multipart? */
+               if (GMIME_IS_MULTIPART (message1->mime_part)) {
+
+                       /* Fill up mime part of message1 using bodystructure info */
+                       emcore_gmime_construct_multipart((GMimeMultipart *)message1->mime_part, body, "1", &total_mail_size);
+
+                       /* Construct message2 with partial body text */
+                       EM_DEBUG_LOG_DEV("RFC822H:%s", imap_response[i].rfc822header);
+                       EM_DEBUG_LOG_DEV("BODYTEXT:%s", imap_response[i].bodytext);
+                       fulltext = g_strconcat(imap_response[i].rfc822header, "\r\n\r\n", imap_response[i].bodytext, NULL);
+
+                       stream2 = g_mime_stream_mem_new_with_buffer(fulltext, EM_SAFE_STRLEN(fulltext));
+                       parser2 = g_mime_parser_new_with_stream(stream2);
+                       if (stream2) g_object_unref(stream2);
+                       EM_SAFE_FREE(fulltext);
+
+                       message2 = g_mime_parser_construct_message(parser2);
+                       if (parser2) g_object_unref(parser2);
+
+                       /* Merge message2 with message1 to make complete gmime message object */
+
+                       iter1 = g_mime_part_iter_new((GMimeObject *)message1);
+                       iter2 = g_mime_part_iter_new((GMimeObject *)message2);
+
+                       if (!g_mime_part_iter_is_valid(iter1) || !g_mime_part_iter_is_valid(iter2)) {
+                               EM_DEBUG_EXCEPTION("Part iterator is not valid");
                                goto FINISH_OFF;
                        }
 
-                       memset(buf, 0x00, sizeof(buf));
-                       if (text_html != NULL)
-                               memcpy(temp_data_html, text_html, EM_SAFE_STRLEN(text_html));
-                               /* EM_SAFE_STRNCPY(temp_data_html, text_html, text_html); */
-
                        do {
-                               if(image_data[store_file].fully_downloaded == 1) {
-                                       if (!emstorage_create_dir(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].account_id, mail->mail_id, 0, image_data[store_file].image_file_name, buf, &err))
-                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-
-                                       if (image_data[store_file].dec_len>0)
-                                               if (!emstorage_create_file(image_data[store_file].text_image, image_data[store_file].dec_len, buf, &err))
-                                                       EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
-
-                                       if (mail->body_download_status != EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED){
-                                               memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
-                                               attachment_tbl.mail_id                          = mail->mail_id;
-                                               attachment_tbl.account_id                       = pbd_event[temp_count].account_id;
-                                               attachment_tbl.mailbox_id                       = pbd_event[temp_count].mailbox_id;
-                                               attachment_tbl.attachment_name                  = image_data[store_file].image_file_name;
-                                               attachment_tbl.attachment_size                  = image_data[store_file].dec_len;
-                                               attachment_tbl.attachment_path                  = buf;
-                                               attachment_tbl.attachment_save_status           = 1;
-                                               attachment_tbl.attachment_inline_content_status = 1; /* set to 1 for inline image */
-                                               attachment_tbl.attachment_mime_type             = image_data[store_file].mime_type;
-                                               EM_DEBUG_LOG("mime_type : [%s]", image_data[store_file].mime_type);
-                                               if (!emstorage_add_attachment (&attachment_tbl, false, false, &err))
-                                                       EM_DEBUG_EXCEPTION("emstorage_add_attachment failed - %d", err);
-                                       }
-                               }
+                               part_tmp2 = g_mime_part_iter_get_current(iter2);
+                               if (part_tmp2 && GMIME_IS_PART(part_tmp2)) {
+                                       part_path = g_mime_part_iter_get_path(iter2);
+
+                                       if (g_mime_part_iter_jump_to(iter1, part_path)) {
+                                               EM_DEBUG_LOG_DEV("g_mime_part_iter_jump_to: %s", part_path);
+                                               part_tmp1 = g_mime_part_iter_get_current(iter1);
+                                               if (part_tmp1 && GMIME_IS_PART(part_tmp1)) {
+                                                       GMimeContentType *ctype_tmp1 = NULL;
+                                                       GMimeContentType *ctype_tmp2 = NULL;
+                                                       char *ctype_str1 = NULL;
+                                                       char *ctype_str2 = NULL;
+                                                       ctype_tmp1 = g_mime_object_get_content_type(part_tmp1);
+                                                       ctype_tmp2 = g_mime_object_get_content_type(part_tmp2);
+                                                       ctype_str1 = g_mime_content_type_to_string(ctype_tmp1);
+                                                       ctype_str2 = g_mime_content_type_to_string(ctype_tmp2);
+                                                       EM_DEBUG_LOG_DEV("%s", ctype_str1);
+                                                       EM_DEBUG_LOG_DEV("%s", ctype_str2);
+
+                                                       if (g_ascii_strcasecmp(ctype_str1, ctype_str2) != 0) {
+                                                               EM_DEBUG_EXCEPTION("PART of fake_message is different from message");
+                                                       } else {
+                                                               char *ctype_size = NULL;
+                                                               GMimeDataWrapper *content_tmp2 = NULL;
+
+                                                               ctype_size = (char *)g_mime_content_type_get_parameter(ctype_tmp1, "part_size");
+                                                               EM_DEBUG_LOG("Part.size.bytes[%s]", ctype_size);
+                                                               if (ctype_size)
+                                                                       g_mime_object_set_content_type_parameter(part_tmp2, "part_size", ctype_size);
+
+                                                               content_tmp2 = g_mime_part_get_content_object(GMIME_PART(part_tmp2));
+                                                               if (content_tmp2 && part_tmp1 && GMIME_IS_PART(part_tmp1)) {
+                                                                       g_mime_part_set_content_object(GMIME_PART(part_tmp1), content_tmp2);
+                                                               }
+
+                                                               /*part_idx = g_mime_multipart_index_of(GMIME_MULTIPART(message1->mime_part), part_tmp1);
+                                                               replaced = g_mime_multipart_replace(GMIME_MULTIPART(message1->mime_part), part_idx, part_tmp2);
+                                                               if (!replaced) {
+                                                                       EM_DEBUG_EXCEPTION("g_mime_multipart_replace failed");
+                                                               }
+                                                               if (replaced) g_object_unref(replaced);*/
+
+                                                               /*content_tmp2 = g_mime_part_get_content_object(GMIME_PART(part_tmp2));
+                                                               part_tmp1 = g_mime_part_iter_get_current(iter1);
+                                                               if (content_tmp2 && part_tmp1 && GMIME_IS_PART(part_tmp1)) {
+                                                                       g_mime_part_set_content_object(GMIME_PART(part_tmp1), content_tmp2);
+                                                                       g_object_unref(content_tmp2);
+                                                               }*/
+
+                                                       }
 
-                               store_file++;
-                       } while (image_data[store_file].text_image != NULL && image_data[store_file].text_image[0] != NULL_CHAR && (store_file < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
-
-                       while (image_data[content_index].text_image  != NULL && image_data[content_index].text_image[0]  != NULL_CHAR &&
-                                 image_data[content_index].content_id && image_data[content_index].content_id[0]  != NULL_CHAR && (content_index < IMAGE_DISPLAY_PARTIAL_BODY_COUNT)){
-                               /*  Finding CID in HTML and replacing with image name. */
-                               if(image_data[content_index].fully_downloaded == 1) {
-                                       result_string_of_replacing = em_replace_string((char *)temp_data_html, (char *)image_data[content_index].content_id, (char *)image_data[content_index].image_file_name);
-                                       if (result_string_of_replacing) {
-                                               memset(temp_data_html, 0x00, imap_response[i].body_len + 1);
-                                               strncpy(temp_data_html, result_string_of_replacing, strlen(result_string_of_replacing));
-                                               EM_SAFE_FREE (result_string_of_replacing);
+                                                       EM_SAFE_FREE(ctype_str1);
+                                                       EM_SAFE_FREE(ctype_str2);
+                                               }
+                                       } else {
+                                               EM_DEBUG_LOG_SEC("g_mime_part_iter_jump_to: failed to jump to %s", part_path);
+                                               EM_SAFE_FREE(part_path);
+                                               goto FINISH_OFF;
                                        }
 
-                                       if (strstr(temp_data_html, image_data[content_index].content_id)  != NULL)
-                                               continue; /*  Replace content id on HTML with same file name one more time. */
+                                       EM_SAFE_FREE(part_path);
                                }
-                               content_index++;
+                       } while (g_mime_part_iter_next(iter2));
+
+                       if (iter1) {
+                               g_mime_part_iter_free(iter1);
+                               iter1 = NULL;
                        }
 
-                       memset(text_html, 0, imap_response[i].body_len + 1);
+                       if (iter2) {
+                               g_mime_part_iter_free(iter2);
+                               iter2 = NULL;
+                       }
 
-                       if (temp_data_html != NULL)
-                               memcpy(text_html, temp_data_html, EM_SAFE_STRLEN(temp_data_html));
+                       if (message2) {
+                               g_object_unref(message2);
+                               message2 = NULL;
+                       }
 
-                       EM_SAFE_FREE(temp_data_html);
+                       if (g_strrstr(g_mime_message_get_sender(message1), "mmsc.plusnet.pl") != NULL ||
+                               g_strrstr(g_mime_message_get_sender(message1), "mms.t-mobile.pl") != NULL) {
+                               cnt_info->attachment_only = 1;
+                       }
+
+                       g_mime_message_foreach(message1, emcore_gmime_imap_parse_foreach_cb, (gpointer)cnt_info);
+
+               } else if (GMIME_IS_PART (message1->mime_part)) {
+                       GMimeDataWrapper *content = NULL;
+                       GMimePart *single_part = GMIME_PART(message1->mime_part);
+
+                       EM_DEBUG_LOG("constructing a %s/%s part", body_types[body->type], body->subtype);
+                       emcore_gmime_construct_part(single_part, body, "1", &total_mail_size);
+
+                       stream1 = g_mime_stream_mem_new_with_buffer(imap_response[i].bodytext, imap_response[i].bodytext_len);
+                       //parser1 = g_mime_parser_new_with_stream(stream1);
+                       content = g_mime_data_wrapper_new_with_stream(stream1, single_part->encoding);
+                       if (stream1) g_object_unref (stream1);
+
+                       g_mime_part_set_content_object(single_part, content);
+                       if (content) g_object_unref(content);
+
+                       sender = g_mime_message_get_sender(message1);
+
+                       if (sender) {
+                               if (g_strrstr(sender, "mmsc.plusnet.pl") != NULL ||
+                                       g_strrstr(sender, "mms.t-mobile.pl") != NULL) {
+                                       cnt_info->attachment_only = 1;
+                               }
+                       }
+
+                       g_mime_message_foreach(message1, emcore_gmime_imap_parse_foreach_cb, (gpointer)cnt_info);
                }
 
-               if (EM_SAFE_STRLEN(text_html) > 0) {
-                       memset(buf, 0x00, sizeof(buf));
-                       char html_body[MAX_CHARSET_VALUE] = {0x00, };
-                       if (cnt_info->text.plain_charset != NULL){
-                               if (EM_SAFE_STRLEN(cnt_info->text.plain_charset) < MAX_CHARSET_VALUE)
-                                       memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
+               if (!strcasecmp(body->subtype, "pkcs7-mime")) {
+                       if (emcore_get_attribute_value_of_body_part(body->parameter, "PROTOCOL", rfc822_protocol, TEMP_STRING_LENGTH, false, &err)) {
+                               if (strcasestr(rfc822_protocol, "enveloped-data"))
+                                       mail->smime_type = EMAIL_SMIME_ENCRYPTED;
                                else
-                                       memcpy(html_body, "UTF-8", strlen("UTF-8"));
+                                       mail->smime_type = EMAIL_SMIME_SIGNED_AND_ENCRYPTED;
+                       }
+               } else if (!strcasecmp(body->subtype, "encrypted")) {
+                       mail->smime_type = EMAIL_PGP_ENCRYPTED;
+               } else if (!strcasecmp(body->subtype, "signed")) {
+                       if (emcore_get_attribute_value_of_body_part(body->parameter, "MICALG", rfc822_micalg, TEMP_STRING_LENGTH, false, &err)) {
+                               mail->digest_type = emcore_get_digest_type(rfc822_micalg);
+                       }
+
+                       if (emcore_get_attribute_value_of_body_part(body->parameter, "PROTOCOL", rfc822_protocol, TEMP_STRING_LENGTH, false, &err)) {
+                               if (strcasestr(rfc822_protocol, "pkcs7-signature"))
+                                       mail->smime_type = EMAIL_SMIME_SIGNED;
+                               else
+                                       mail->smime_type = EMAIL_PGP_SIGNED;
+                       }
+               } else {
+                       mail->smime_type = EMAIL_SMIME_NONE;
+               }
+
+               if (body) {
+                       mail_free_body(&body);
+                       body = NULL;
+               }
+
+               if (message1) {
+                       g_object_unref(message1);
+                       message1 = NULL;
+               }
+
+               /* Update the attachment info except inline attachment */
+               if ((err = emcore_update_attachment_except_inline(pbd_event[temp_count].multi_user_name, 
+                                                        cnt_info, 
+                                                        pbd_event[temp_count].account_id, 
+                                                        mail->mail_id, 
+                                                        pbd_event[temp_count].mailbox_id, 
+                                                        &total_attachment_size, 
+                                                        &attachment_num, 
+                                                        &inline_attachment_num)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               mail->mail_size            = total_mail_size;
+               mail->attachment_count     = attachment_num;
+               mail->inline_content_count = inline_attachment_num;
+
+               if (imap_response[i].bodytext_len == 0) {
+                       EM_DEBUG_LOG("BODY size is zero");
+                       continue;
+               }
+
+               /* text/plain */
+               if (cnt_info->text.plain) {
+                       char *charset_plain_text = NULL;
+                       memset(path_buf, 0x00, sizeof(path_buf));
+                       memset(move_buf, 0x00, sizeof(move_buf));
+
+                       if (cnt_info->text.plain_charset)
+                               charset_plain_text = cnt_info->text.plain_charset;
+                       else {
+                               if (mail->default_charset) {
+                                       charset_plain_text = mail->default_charset;
+                               } else {
+                                       charset_plain_text = "UTF-8";
+                               }
+                       }
+
+                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name, pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
+                               EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+
+                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
+                                        pbd_event[temp_count].account_id, 
+                                        mail->mail_id, 
+                                        0, 
+                                        charset_plain_text, 
+                                        move_buf, 
+                                        path_buf, 
+                                        sizeof(path_buf),
+                                        &err))
+                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+
+                       if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err))  {
+                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                               mail->file_path_plain = NULL;
                        }
-                       if (html_body[0]  != NULL_CHAR)
-                               strcat(html_body, HTML_EXTENSION_STRING);
                        else
-                               memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
+                               mail->file_path_plain = EM_SAFE_STRDUP(path_buf);
+                       EM_DEBUG_LOG_SEC("mail->file_path_plain [%s]", mail->file_path_plain);
+               }
 
-                       if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
+               /* text/html */
+               if (cnt_info->text.html) {
+                       char *charset_html_text = NULL;
+                       memset(path_buf, 0x00, sizeof(path_buf));
+                       memset(move_buf, 0x00, sizeof(move_buf));
+
+                       if (cnt_info->text.html_charset)
+                               charset_html_text = cnt_info->text.html_charset;
+                       else {
+                               if (mail->default_charset) {
+                                       charset_html_text = mail->default_charset;
+                               } else {
+                                       charset_html_text = "UTF-8";
+                               }
+                       }
+
+                       charset_html_text = g_strconcat(charset_html_text, HTML_EXTENSION_STRING, NULL);
+
+                       if (!emstorage_create_dir(pbd_event[temp_count].multi_user_name, pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
                                EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
 
-                       if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, html_body, buf, &err))
+                       if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
+                                        pbd_event[temp_count].account_id, 
+                                        mail->mail_id, 
+                                        0, 
+                                        charset_html_text, 
+                                        move_buf, 
+                                        path_buf, 
+                                        sizeof(path_buf), 
+                                        &err))
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
 
-                       int text_html_len = EM_SAFE_STRLEN(text_html);
-                       if (text_html_len>0)
-                               if (!emstorage_create_file(text_html, EM_SAFE_STRLEN(text_html), buf, &err))
-                                       EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
+                       if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                               mail->file_path_html = NULL;
+                       }
+                       else
+                               mail->file_path_html = EM_SAFE_STRDUP(path_buf);
+                       g_free(charset_html_text);
+               }
 
-                       mail->file_path_html = EM_SAFE_STRDUP(buf);
+               /* inline attachment */
+               inline_download_count = 0;
+               if (cnt_info->inline_file) {
+                       struct attachment_info *temp_file = NULL;
+
+                       if (inline_attachment_num > 0) {
+                               temp_file = cnt_info->inline_file;
+                               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))
+                                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+
+                                               if (!emstorage_get_save_name(pbd_event[temp_count].multi_user_name, 
+                                                    pbd_event[temp_count].account_id, 
+                                                    mail->mail_id, 
+                                                    0, 
+                                                    temp_file->name, 
+                                                    move_buf, 
+                                                    path_buf, 
+                                                    sizeof(path_buf), 
+                                                    &err))
+                                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+
+                                               if (!emstorage_move_file(temp_file->save, move_buf, false, &err)) {
+                                                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                                                       goto FINISH_OFF;
+                                               }
 
+                                               if (!(mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED)) {
+                                                       memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
+                                                       attachment_tbl.mail_id                          = mail->mail_id;
+                                                       attachment_tbl.account_id                       = pbd_event[temp_count].account_id;
+                                                       attachment_tbl.mailbox_id                       = pbd_event[temp_count].mailbox_id;
+                                                       attachment_tbl.attachment_name                  = temp_file->name;
+                                                       attachment_tbl.attachment_size                  = temp_file->size;
+                                                       attachment_tbl.attachment_path                  = path_buf;
+                                                       attachment_tbl.attachment_save_status           = 1;
+                                                       attachment_tbl.attachment_inline_content_status = 1; /* set to 1 for inline image */
+                                                       attachment_tbl.attachment_mime_type             = temp_file->attachment_mime_type;
+                                                       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))
+                                                               EM_DEBUG_EXCEPTION("emstorage_add_attachment failed - %d", err);
+                                               }
+                                       }
+                                       else {
+                                               if (temp_file->save)
+                                                       g_remove(temp_file->save);
+                                       }
+                                       temp_file = temp_file->next;
+                               }
+                       }
                }
 
-               mail->body_download_status = (total_mail_size - total_attachment_size < input_download_size) ? 1 : 2;
+               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("mail_id [%d] body_download_status [%d]", mail->mail_id, mail->body_download_status);
 
                /* Get preview text */
-               if ( (err = emcore_get_preview_text_from_file(mail->file_path_plain, mail->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail->preview_text))) != EMAIL_ERROR_NONE)
+               if ( (err = emcore_get_preview_text_from_file(pbd_event[temp_count].multi_user_name, 
+                                                        mail->file_path_plain, 
+                                                        mail->file_path_html, 
+                                                        MAX_PREVIEW_TEXT_LENGTH, 
+                                                        &(mail->preview_text))) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file() failed[%d]", err);
 
                /* Update body contents */
-               if (!emstorage_change_mail_field(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;
                }
@@ -4178,19 +4990,19 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
 #ifdef __FEATURE_BODY_SEARCH__
                /* strip html content and save into mail_text_tbl */
                char *stripped_text = NULL;
-               if (!emcore_strip_mail_body_from_file(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);
                }
 
-               emstorage_mail_text_tbl_t *mail_text;
-               if (!emstorage_get_mail_text_by_id(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;
                }
 
                EM_SAFE_FREE(mail_text->body_text);
                mail_text->body_text = stripped_text;
-               if (!emstorage_change_mail_text_field(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;
                }
@@ -4202,7 +5014,7 @@ static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, i
                if (mail)
                        emstorage_free_mail(&mail, 1, NULL);
 
-               if (false == emcore_delete_pbd_activity(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;
                }
@@ -4226,29 +5038,51 @@ FINISH_OFF:
                EM_SAFE_FREE(reply_from_server);
        }
 
-       EM_SAFE_FREE(text_plain);
-       EM_SAFE_FREE(text_html);
+       if (message1) {
+               g_object_unref(message1);
+               message1 = NULL;
+       }
 
-       if (image_data)
-               emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
+       if (message2) {
+               g_object_unref(message2);
+               message2 = NULL;
+       }
+
+       if (iter1) {
+               g_mime_part_iter_free(iter1);
+               iter1 = NULL;
+       }
+
+       if (iter2) {
+               g_mime_part_iter_free(iter2);
+               iter2 = NULL;
+       }
 
        if (cnt_info) {
                emcore_free_content_info(cnt_info);
                EM_SAFE_FREE(cnt_info);
        }
 
+       EM_SAFE_FREE(fulltext);
+
        if (body)
                mail_free_body(&body);
 
        if (mail)
                emstorage_free_mail(&mail, 1, NULL);
 
+       if (mail_text)
+               emstorage_free_mail_text(&mail_text, 1, NULL);
+
        if (imap_response)
                emcore_free_email_partial_buffer(&imap_response, item_count);
 
+       EM_SAFE_FREE(bodystructure_ptr);
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
+#endif
 
 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body_for_pop3(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
 {
@@ -4266,12 +5100,28 @@ INTERNAL_FUNC int emcore_download_bulk_partial_mail_body_for_pop3(MAILSTREAM *st
                EM_DEBUG_LOG_SEC("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
                        i, pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].server_mail_id, pbd_event[i].activity_id);
 
-               if (!emcore_download_body_multi_sections_bulk(stream, pbd_event[i].account_id, pbd_event[i].mail_id, false, false, input_download_size, 0 , &err)){
+#ifdef __FEATURE_USE_GMIME__
+               if (!emcore_gmime_download_body_sections(pbd_event[i].multi_user_name, stream, pbd_event[i].account_id,
+                               pbd_event[i].mail_id, 0, input_download_size, -1, 0, 0, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
+                       goto FINISH_OFF;
+               }
+#else
+               if (!emcore_download_body_multi_sections_bulk (stream,
+                                                   pbd_event[i].account_id,
+                                                   pbd_event[i].mail_id,
+                                                   false,
+                                                   false,
+                                                   input_download_size,
+                                                   -1,
+                                                   0,
+                                                   &err)) {
                        EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed");
                        goto FINISH_OFF;
                }
+#endif
 
-               if (false == emcore_delete_pbd_activity(pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].activity_id, &err)){
+               if (false == emcore_delete_pbd_activity(pbd_event[i].multi_user_name, pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].activity_id, &err)){
                        EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -4302,7 +5152,7 @@ INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, ema
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(pbd_event[0].account_id, EMAIL_ACC_GET_OPT_DEFAULT, &pbd_account_tbl, true, &err)) {
+       if (!emstorage_get_account_by_id(pbd_event[0].multi_user_name, pbd_event[0].account_id, EMAIL_ACC_GET_OPT_DEFAULT, &pbd_account_tbl, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4311,11 +5161,15 @@ INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, ema
 
        switch (pbd_account_tbl->incoming_server_type){
                case EMAIL_SERVER_TYPE_IMAP4:
-                       ret = emcore_download_bulk_partial_mail_body_for_imap(stream, auto_download_size, pbd_event, item_count, &err);
+#ifdef __FEATURE_USE_GMIME__
+                       ret = emcore_gmime_download_imap_partial_mail_body(stream, auto_download_size, pbd_event, item_count, &err);
+#endif
                        break;
+
                case EMAIL_SERVER_TYPE_POP3:
                        ret = emcore_download_bulk_partial_mail_body_for_pop3(stream, auto_download_size, pbd_event, item_count, &err);
                        break;
+
                default:
                        err = EMAIL_ERROR_NOT_SUPPORTED;
                        ret = false;
@@ -4345,8 +5199,9 @@ static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buff
        int i;
 
        for (i = 0; i < item_count ; i++, p++) {
-               EM_SAFE_FREE(p->header);
-               EM_SAFE_FREE(p->body);
+               EM_SAFE_FREE(p->bodystructure);
+               EM_SAFE_FREE(p->bodytext);
+               EM_SAFE_FREE(p->rfc822header);
        }
 
        EM_SAFE_FREE(*partial_buffer);
@@ -4369,6 +5224,7 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
        char *full_line = NULL;
        char *tmp = NULL;
 
+       int rfc822header_size = 0;
        int body_size = 0;
        int count = 0;
        int ret = false;
@@ -4377,6 +5233,7 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
        char *p_bodystructure = NULL;
        char *p_bodystructure_end = NULL;
        char *p_body_text = NULL;
+       char *p_rfc822header = NULL;
 
        server_response = (email_partial_buffer *)em_malloc(sizeof(email_partial_buffer) * item_count);
        if (NULL == server_response) {
@@ -4401,6 +5258,27 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
                        EM_SAFE_FREE(cur_line);
                        EM_SAFE_FREE(tmp);
 
+                       p_rfc822header = strcasestr(full_line, "RFC822.HEADER {");
+                       if (p_rfc822header && server_response[count].rfc822header == NULL) {
+                               p_rfc822header += strlen("RFC822.HEADER {");
+                               rfc822header_size = atoi(p_rfc822header);
+
+                               server_response[count].rfc822header_len = rfc822header_size;
+                               server_response[count].rfc822header = em_malloc(server_response[count].rfc822header_len + 1);
+                               if (server_response[count].rfc822header == NULL) {
+                                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                                       goto FINISH_OFF;
+                               }
+
+                               if (net_getbuffer(nstream, server_response[count].rfc822header_len, server_response[count].rfc822header) <= 0) {
+                                       EM_DEBUG_EXCEPTION("net_getbuffer failed");
+                                       goto FINISH_OFF;
+                               }
+
+                               p_rfc822header = NULL;
+                               continue;
+                       }
+
                        p_bodystructure = strcasestr(full_line, "BODYSTRUCTURE");
                        p_bodystructure_end = strcasestr(full_line, "BODY[TEXT]");
 
@@ -4414,13 +5292,13 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
                                }
 
                                /* get BODYSTRUCTURE */
-                               server_response[count].header_len = p_bodystructure_end - p_bodystructure;
-                               server_response[count].header = em_malloc(server_response[count].header_len + 1);
-                               if (server_response[count].header == NULL) {
+                               server_response[count].bodystructure_len = p_bodystructure_end - p_bodystructure;
+                               server_response[count].bodystructure = em_malloc(server_response[count].bodystructure_len + 1);
+                               if (server_response[count].bodystructure == NULL) {
                                        EM_DEBUG_EXCEPTION("em_malloc failed");
                                        goto FINISH_OFF;
                                }
-                               SNPRINTF(server_response[count].header, server_response[count].header_len, "%s", p_bodystructure);
+                               SNPRINTF(server_response[count].bodystructure, server_response[count].bodystructure_len, "%s", p_bodystructure);
                                /*EM_DEBUG_LOG("BODYSTRUCTURE(%d)[%s]", server_response[count].header_len, server_response[count].header);*/
 
                                /* get BODY size & text */
@@ -4432,14 +5310,14 @@ static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream
 
                                p_body_text += strlen("BODY[TEXT]<0> {");
                                body_size = atoi(p_body_text);
-                               server_response[count].body_len = (body_size > input_download_size) ? input_download_size : body_size;
-                               server_response[count].body = em_malloc(server_response[count].body_len + 1);
-                               if (server_response[count].body == NULL) {
+                               server_response[count].bodytext_len = (body_size > input_download_size) ? input_download_size : body_size;
+                               server_response[count].bodytext = em_malloc(server_response[count].bodytext_len + 1);
+                               if (server_response[count].bodytext == NULL) {
                                        EM_DEBUG_EXCEPTION("em_malloc failed");
                                        goto FINISH_OFF;
                                }
 
-                               if (net_getbuffer(nstream, server_response[count].body_len, server_response[count].body) <= 0) {
+                               if (net_getbuffer(nstream, server_response[count].bodytext_len, server_response[count].bodytext) <= 0) {
                                        EM_DEBUG_EXCEPTION("net_getbuffer failed");
                                        goto FINISH_OFF;
                                }
index 9525bcd..aa4edb8 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,7 +24,7 @@
  * File :  email-core-mailbox.c
  * Desc :  Local Mailbox Management
  *
- * Auth : 
+ * Auth :
  *
  *****************************************************************************/
 #include <stdio.h>
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <time.h>
 #include <sys/types.h>
+
 #include "email-types.h"
 #include "email-utilities.h"
 #include "email-convert.h"
 #include "email-core-event.h"
 #include "email-network.h"
 #include "email-core-mail.h"
-#include "email-core-imap-mailbox.h"   
+#include "email-core-imap-mailbox.h"
 #include "email-storage.h"
-#include "email-core-account.h" 
+#include "email-core-account.h"
+
+#include "imap4r1.h"
 
 #ifdef __FEATURE_KEEP_CONNECTION__
 static void *g_receiving_thd_stream = NULL;                    /* Stores the recv thd stream for next time reuse */
@@ -59,26 +62,36 @@ __thread email_connection_info_t *g_connection_info_list = NULL;
 static pthread_mutex_t _close_stream_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect closing stream */
 #endif /*  __FEATURE_KEEP_CONNECTION__ */
 
+int emcore_imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code);
+int emcore_pop3_send_command(MAILSTREAM *stream, pop3_cmd_t cmd_type, int *err_code);
+
+/* thread local variable for stream reuse */
+__thread GList* g_recv_stream_list = NULL;
+typedef struct {
+       int account_id;
+       int mailbox_id;
+       MAILSTREAM **mail_stream;
+} email_recv_stream_list_t;
 
 /*  Binding IMAP mailbox with its function */
 static email_mailbox_type_item_t  g_mailbox_type[MAX_MAILBOX_TYPE] = {
                                {EMAIL_MAILBOX_TYPE_INBOX,   "INBOX" },
                                /*  Naver */
                                {EMAIL_MAILBOX_TYPE_INBOX,   "Inbox" },
-                               {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent Messages"} , 
-                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTOLpUx3zVaA-"} , 
+                               {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent Messages"} ,
+                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTOLpUx3zVaA-"} ,
                                {EMAIL_MAILBOX_TYPE_DRAFT,   "Drafts"} ,
                                {EMAIL_MAILBOX_TYPE_TRASH,   "Deleted Messages" } ,
                                /*  AOL */
-                               {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent"} , 
-                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "Spam" }, 
+                               {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent"} ,
+                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "Spam" },
                                {EMAIL_MAILBOX_TYPE_DRAFT,   "Drafts"} ,
                                {EMAIL_MAILBOX_TYPE_TRASH,   "Trash"},
                                /* DAUM */
                                {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTONO4ycDVaA-"},
                                /* ETC */
-                               {EMAIL_MAILBOX_TYPE_SENTBOX, "mail/sent-mail"}, 
-                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "mail/spam-mail" }, 
+                               {EMAIL_MAILBOX_TYPE_SENTBOX, "mail/sent-mail"},
+                               {EMAIL_MAILBOX_TYPE_SPAMBOX, "mail/spam-mail" },
                                {EMAIL_MAILBOX_TYPE_DRAFT,   "mail/saved-drafts"} ,
                                {EMAIL_MAILBOX_TYPE_TRASH,   "mail/mail-trash"},
 };
@@ -94,7 +107,7 @@ email_connection_info_t* emcore_get_connection_info_by_account_id(int account_id
                        break;
                connection_info = connection_info->next;
        }
-       
+
        EM_DEBUG_FUNC_END("connection_info [%p]", connection_info);
        return connection_info;
 }
@@ -127,7 +140,7 @@ int emcore_append_connection_info(email_connection_info_t *new_connection_info)
                new_connection_info->next = NULL;
                g_connection_info_list = new_connection_info;
        }
-       
+
        EM_DEBUG_FUNC_END("EMAIL_ERROR_NONE");
        return EMAIL_ERROR_NONE;
 }
@@ -151,7 +164,7 @@ INTERNAL_FUNC int emcore_remove_connection_info(int account_id)
                prev_connection_info = connection_info;
                connection_info = connection_info->next;
        }
-       
+
        EM_DEBUG_FUNC_END("");
        return EMAIL_ERROR_NONE;
 }
@@ -159,49 +172,167 @@ INTERNAL_FUNC int emcore_remove_connection_info(int account_id)
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 
 
+INTERNAL_FUNC void emcore_close_recv_stream_list (void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       GList* cur = g_recv_stream_list;
+       email_recv_stream_list_t* data = NULL;
+       for ( ; cur; cur = g_list_next(cur)) {
+               data = cur->data;
+               *(data->mail_stream) = mail_close (*(data->mail_stream));
+               g_recv_stream_list = g_list_delete_link (g_recv_stream_list, cur);
+               g_free (data);
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC MAILSTREAM** emcore_get_recv_stream (char *multi_user_name, int account_id, int mailbox_id, int *error)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
+       GList* cur = g_recv_stream_list;
+       email_recv_stream_list_t* data = NULL;
+       MAILSTREAM** ret = NULL;
+       int err = EMAIL_ERROR_NONE;
+       email_account_t *ref_account = NULL;
+
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))   {
+               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
+               err = EMAIL_ERROR_INVALID_ACCOUNT;
+               goto FINISH_OFF;
+       }
+
+       for ( ; cur; cur = g_list_next(cur)) {
+               data = cur->data;
+               if (data->account_id == account_id && data->mailbox_id == mailbox_id) {
+                       if (data->mail_stream == NULL || *(data->mail_stream) == NULL) {
+                               EM_DEBUG_LOG ("mail_stream was closed before");
+                               g_recv_stream_list = g_list_delete_link (g_recv_stream_list, cur);
+                               g_free (data);
+                               break;
+                       }
+
+                       if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
+                               /*  send NOOP command */
+                               if (!emcore_imap4_send_command(*(data->mail_stream), IMAP4_CMD_NOOP, &err)) {
+                                       EM_DEBUG_LOG("imap4_send_command failed [%d]", err);
+                                       EM_DEBUG_LOG ("mail_stream is not reusable");
+                                       *(data->mail_stream) = mail_close (*(data->mail_stream));
+                                       g_recv_stream_list = g_list_delete_link (g_recv_stream_list, cur);
+                                       g_free (data);
+                                       break;
+                               }
+                               else {
+                                       EM_DEBUG_LOG ("reusable mail_stream found");
+                                       emcore_free_account(ref_account); /* prevent */
+                                       EM_SAFE_FREE(ref_account);
+                                       return data->mail_stream;
+                               }
+                       }
+                       else if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
+                               /*  send NOOP command */
+                               if (!emcore_pop3_send_command(*(data->mail_stream), POP3_CMD_NOOP, &err)) {
+                                       EM_DEBUG_LOG("emcore_pop3_send_command failed [%d]", err);
+                                       EM_DEBUG_LOG ("mail_stream is not reusable");
+                                       *(data->mail_stream) = mail_close (*(data->mail_stream));
+                                       g_recv_stream_list = g_list_delete_link (g_recv_stream_list, cur);
+                                       g_free (data);
+                                       break;
+                               }
+                               else {
+                                       EM_DEBUG_LOG ("reusable mail_stream found");
+                                       emcore_free_account(ref_account); /* prevent */
+                                       EM_SAFE_FREE(ref_account);
+                                       return data->mail_stream;
+                               }
+                       }
+               }
+       }
+
+       ret = em_malloc (sizeof(MAILSTREAM*));
+       if (!ret) {
+               EM_DEBUG_EXCEPTION("em_malloc error");
+               goto FINISH_OFF;
+       }
+
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mailbox_id, (void **)ret, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+               EM_SAFE_FREE(ret);
+               goto FINISH_OFF;
+       }
+
+       email_recv_stream_list_t *node = em_malloc (sizeof(email_recv_stream_list_t));
+       if (!node) {
+               EM_DEBUG_EXCEPTION ("em_malloc error");
+               *ret = mail_close (*ret);
+               EM_SAFE_FREE(ret);
+               goto FINISH_OFF;
+       }
+
+       node->account_id = account_id;
+       node->mailbox_id = mailbox_id;
+       node->mail_stream = ret;
+
+       g_recv_stream_list = g_list_prepend (g_recv_stream_list, node);
+
+FINISH_OFF:
+
+       if (error)
+               *error = err;
+
+       if (ref_account) {
+               emcore_free_account(ref_account);
+               EM_SAFE_FREE(ref_account);
+       }
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+
 /* description
  *    get local mailbox list
  */
-INTERNAL_FUNC int emcore_get_mailbox_list(int account_id, email_mailbox_t **mailbox_list,      int *p_count, int *err_code)
+INTERNAL_FUNC int emcore_get_mailbox_list(char *multi_user_name, int account_id, email_mailbox_t **mailbox_list,       int *p_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
-       
+
        if (account_id <= 0 || !mailbox_list || !p_count)  {
                EM_DEBUG_EXCEPTION("PARAM Failed account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       
+
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
        email_account_t *ref_account = NULL;
        int i, count = 512;
-       
+
        /* get mailbox list from mailbox table */
        
-       if (!(ref_account = emcore_get_account_reference(account_id)))  {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))  {
                EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed - %d", account_id);
                error = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_get_mailbox_list(ref_account->account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &count, &local_mailbox_list, true, &error))  {      
+
+       if (!emstorage_get_mailbox_list(multi_user_name, ref_account->account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &count, &local_mailbox_list, true, &error))  {     
                EM_DEBUG_EXCEPTION(" emstorage_get_mailbox failed - %d", error);
-       
                goto FINISH_OFF;
        }
-       
+
        if (count > 0)  {
                if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * count)))  {
                        EM_DEBUG_EXCEPTION(" mailloc failed...");
                        error = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
-               
+
                memset(*mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
-               
+
                for (i = 0; i < count; i++)  {
                        em_convert_mailbox_tbl_to_mailbox(local_mailbox_list + i, (*mailbox_list) + i);
                }
@@ -213,7 +344,7 @@ INTERNAL_FUNC int emcore_get_mailbox_list(int account_id, email_mailbox_t **mail
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
        if (local_mailbox_list != NULL)
                emstorage_free_mailbox(&local_mailbox_list, count, NULL);
 
@@ -221,7 +352,7 @@ FINISH_OFF:
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
        }
-       
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -232,10 +363,10 @@ FINISH_OFF:
 /* description
  *    get imap sync mailbox list
  */
-int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
+int emcore_get_mailbox_list_to_be_sync(char *multi_user_name, int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
-       
+
        if (account_id <= 0 || !mailbox_list || !p_count)  {
                EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
                if (err_code != NULL)
@@ -249,29 +380,28 @@ int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox
        emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
        email_account_t *ref_account = NULL;
        int i, count = 512;
-       
+
        /* get mailbox list from mailbox table */
-       if (!(ref_account = emcore_get_account_reference(account_id)))  {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
                error = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_get_mailbox_list(ref_account->account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &count, &mailbox_tbl_list, true, &error))  {        
+
+       if (!emstorage_get_mailbox_list(multi_user_name, ref_account->account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &count, &mailbox_tbl_list, true, &error))  {       
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed - %d", error);
-       
                goto FINISH_OFF;
        }
-       
+
        if (count > 0)  {
                if (!(tmp_mailbox_list = em_malloc(sizeof(email_mailbox_t) * count)))  {
                        EM_DEBUG_EXCEPTION("malloc failed...");
                        error = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
-               
+
                memset(tmp_mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
-               
+
                for (i = 0; i < count; i++)  {
                        em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, tmp_mailbox_list + i);
                }
@@ -280,55 +410,55 @@ int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox
                tmp_mailbox_list = NULL;
        *p_count = count;
        ret = true;
-       
-FINISH_OFF: 
-       
+
+FINISH_OFF:
+
        *mailbox_list = tmp_mailbox_list;
-       
+
        if (ref_account) {
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
        }
-       
+
        if (mailbox_tbl_list != NULL)
                emstorage_free_mailbox(&mailbox_tbl_list, count, NULL);
-       
+
        if (err_code != NULL)
                *err_code = error;
        EM_DEBUG_FUNC_END("error [%d]", error);
        return ret;
 }
 
-INTERNAL_FUNC int emcore_get_mail_count(email_mailbox_t *mailbox, int *total, int *unseen, int *err_code)
+INTERNAL_FUNC int emcore_get_mail_count(char *multi_user_name, email_mailbox_t *mailbox, int *total, int *unseen, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+
        if (!mailbox)  {
                EM_DEBUG_EXCEPTION(" mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
-       if (!emstorage_get_mail_count(mailbox->account_id, mailbox->mailbox_id, total, unseen, true, &err))  {
-               EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
 
+       if (!emstorage_get_mail_count(multi_user_name, mailbox->account_id, mailbox->mailbox_id, total, unseen, true, &err))  {
+               EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
                goto FINISH_OFF;
        }
 
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 }
 
-INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_server, int *err_code)
+INTERNAL_FUNC int emcore_create_mailbox(char *multi_user_name, email_mailbox_t *new_mailbox, int on_server, int server_type, int slot_size, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
        int ret = false;
@@ -336,22 +466,35 @@ INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_ser
        emstorage_mailbox_tbl_t *local_mailbox = NULL;
        email_account_t *account_ref = NULL;
        char *enc_mailbox_name = NULL;
-       
+       int incomming_server_type = 0;
+       int mail_slot_size = 25;
+
        if (new_mailbox == NULL || new_mailbox->mailbox_name == NULL)  {
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(new_mailbox->account_id);
-       if (!account_ref) {
-               EM_DEBUG_EXCEPTION("Invalid account_id [%d]", new_mailbox->account_id);
-               err = EMAIL_ERROR_INVALID_ACCOUNT;
-               goto FINISH_OFF;
+       if (server_type > 0 && slot_size > 0) {
+               incomming_server_type = server_type;
+               mail_slot_size = slot_size;
        }
+       else {
+               account_ref = emcore_get_account_reference(multi_user_name, new_mailbox->account_id);
+               if (!account_ref) {
+                       EM_DEBUG_EXCEPTION("Invalid account_id [%d]", new_mailbox->account_id);
+                       err = EMAIL_ERROR_INVALID_ACCOUNT;
+                       goto FINISH_OFF;
+               }
+
+               incomming_server_type = account_ref->incoming_server_type;
+               mail_slot_size = account_ref->default_mail_slot_size;
 
+               emcore_free_account(account_ref);
+               EM_SAFE_FREE(account_ref);
+       }
 
        /* converting UTF-8 to UTF-7 except EAS */
-       if (account_ref->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
+       if (incomming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                if (!emcore_get_encoded_mailbox_name(new_mailbox->mailbox_name, &enc_mailbox_name, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_get_encoded_mailbox_name failed [%d]", err);
                        goto FINISH_OFF;
@@ -362,7 +505,7 @@ INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_ser
 
        if (on_server) {
                /* Create a mailbox from Sever */
-               if (!emcore_create_imap_mailbox(new_mailbox, &err)) {
+               if (!emcore_create_imap_mailbox(multi_user_name, new_mailbox, &err)) {
                        EM_DEBUG_EXCEPTION("Creating a mailbox on server failed.");
                        goto FINISH_OFF;
                }
@@ -379,9 +522,9 @@ INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_ser
 
        em_convert_mailbox_to_mailbox_tbl(new_mailbox, local_mailbox);
 
-       emcore_get_default_mail_slot_count(local_mailbox->account_id, &(local_mailbox->mail_slot_size));
+       local_mailbox->mail_slot_size = mail_slot_size;
 
-       if (!emstorage_add_mailbox(local_mailbox, true, &err))  {
+       if (!emstorage_add_mailbox(multi_user_name, local_mailbox, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -396,17 +539,14 @@ FINISH_OFF:
 
        if (err_code)
                *err_code = err;
-       
-       emcore_free_account(account_ref);
-       EM_SAFE_FREE(account_ref);
 
        return ret;
 }
 
-INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int input_on_server, int input_recursive)
+INTERNAL_FUNC int emcore_delete_mailbox(char *multi_user_name, int input_mailbox_id, int input_on_server, int input_recursive)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_on_server[%d] input_recursive[%d]", input_mailbox_id, input_on_server, input_recursive);
-       
+
        int err = EMAIL_ERROR_NONE;
        int i = 0;
        int mailbox_count = 0;
@@ -418,8 +558,8 @@ INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int input_on_serve
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE || !target_mailbox) {
+
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE || !target_mailbox) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -427,13 +567,13 @@ INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int input_on_serve
 #ifdef __FEATURE_DELETE_MAILBOX_RECURSIVELY__
        if(input_recursive) {
                /* Getting children mailbox list */
-               if(!emstorage_get_child_mailbox_list(target_mailbox->account_id, target_mailbox->mailbox_name, &mailbox_count, &target_mailbox_array, false,&err)) {
+               if(!emstorage_get_child_mailbox_list(multi_user_name, target_mailbox->account_id, target_mailbox->mailbox_name, &mailbox_count, &target_mailbox_array, false,&err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_child_mailbox_list failed. [%d]", err);
                        goto FINISH_OFF;
                }
 
                if (target_mailbox)
-                       emstorage_free_mailbox(&target_mailbox, 1, NULL);
+               emstorage_free_mailbox(&target_mailbox, 1, NULL);
                target_mailbox = NULL;
        }
        else
@@ -449,18 +589,20 @@ INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int input_on_serve
                EM_DEBUG_LOG("Deleting mailbox_id [%d]", target_mailbox_array[i].mailbox_id);
                if (input_on_server) {
                        EM_DEBUG_LOG("Delete the mailbox in Sever >>> ");
-                       if  (!emcore_delete_imap_mailbox(target_mailbox_array[i].mailbox_id, &err))
+                       if  (!emcore_delete_imap_mailbox(multi_user_name, target_mailbox_array[i].mailbox_id, &err)) {
                                EM_DEBUG_EXCEPTION("Delete the mailbox in server : failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
                        else
                                EM_DEBUG_LOG("Delete the mailbox in server : success");
                }
 
-               if (!emcore_delete_all_mails_of_mailbox(target_mailbox_array[i].account_id, target_mailbox_array[i].mailbox_id, false, &err))  {
+               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 (!emstorage_delete_mailbox(target_mailbox_array[i].account_id, -1, target_mailbox_array[i].mailbox_id, true, &err))  {
+               if (!emstorage_delete_mailbox(multi_user_name, target_mailbox_array[i].account_id, -1, target_mailbox_array[i].mailbox_id, true, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_delete_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -478,7 +620,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int input_recursive)
+INTERNAL_FUNC int emcore_delete_mailbox_ex(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int input_recursive)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_on_server[%d] input_recursive[%d]", input_mailbox_id_array, input_mailbox_id_array, input_mailbox_id_count, input_on_server, input_recursive);
        int err = EMAIL_ERROR_NONE;
@@ -490,13 +632,13 @@ INTERNAL_FUNC int emcore_delete_mailbox_ex(int input_account_id, int *input_mail
                goto FINISH_OFF;
        }
 
-       if((err = emstorage_set_field_of_mailbox_with_integer_value(input_account_id, input_mailbox_id_array, input_mailbox_id_count, "deleted_flag", 1, true)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_set_field_of_mailbox_with_integer_value(multi_user_name, input_account_id, input_mailbox_id_array, input_mailbox_id_count, "deleted_flag", 1, true)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mailbox_with_integer_value failed[%d]", err);
                goto FINISH_OFF;
        }
 
        for(i = 0; i < input_mailbox_id_count; i++) {
-               if((err = emcore_delete_mailbox(input_mailbox_id_array[i] , input_on_server, input_recursive)) != EMAIL_ERROR_NONE) {
+               if((err = emcore_delete_mailbox(multi_user_name, input_mailbox_id_array[i] , input_on_server, input_recursive)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -507,10 +649,10 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_code)
+INTERNAL_FUNC int emcore_delete_mailbox_all(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN(" mailbox[%p], err_code[%p]", mailbox, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
 
@@ -520,29 +662,27 @@ INTERNAL_FUNC int emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_c
                goto FINISH_OFF;
        }
        
-       if (!emcore_delete_all_mails_of_mailbox(mailbox->account_id, mailbox->mailbox_id, 0, /*NULL, */ &err)) {
+       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 (!emstorage_delete_mailbox(mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
-               EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
-               
 
+       if (!emstorage_delete_mailbox(multi_user_name, mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
+               EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
                goto FINISH_OFF;
        }
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
        if (err_code != NULL)
                *err_code = err;
        EM_DEBUG_FUNC_END("err[%d]", err);
        return ret;
 }
 
-INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int input_recursive, int handle_to_be_published)
+INTERNAL_FUNC int emcore_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int input_recursive, int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_on_server[%d] input_recursive[%d] handle_to_be_published[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_on_server, input_recursive, handle_to_be_published);
 
@@ -561,12 +701,12 @@ INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_ma
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE || !target_mailbox) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE || !target_mailbox) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(target_mailbox->account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, target_mailbox->account_id);
 
        if (account_ref == NULL) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
@@ -579,7 +719,7 @@ INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_ma
        if (input_on_server) {
                EM_DEBUG_LOG("Rename the mailbox in Sever >>> ");
 
-               if ((err = emcore_rename_mailbox_on_imap_server(target_mailbox->account_id, target_mailbox->mailbox_id, target_mailbox->mailbox_name, input_new_mailbox_name, handle_to_be_published)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_rename_mailbox_on_imap_server(multi_user_name, target_mailbox->account_id, target_mailbox->mailbox_id, target_mailbox->mailbox_name, input_new_mailbox_name, handle_to_be_published)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_rename_mailbox_on_imap_server failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -590,7 +730,7 @@ INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_ma
 #ifdef __FEATURE_RENAME_MAILBOX_RECURSIVELY__
        if(account_ref->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_recursive) {
                /* Getting children mailbox list */
-               if(!emstorage_get_child_mailbox_list(target_mailbox->account_id, target_mailbox->mailbox_name, &mailbox_count, &target_mailbox_array, false,&err)) {
+               if(!emstorage_get_child_mailbox_list(multi_user_name, target_mailbox->account_id, target_mailbox->mailbox_name, &mailbox_count, &target_mailbox_array, false,&err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_child_mailbox_list failed. [%d]", err);
                        goto FINISH_OFF;
                }
@@ -612,7 +752,7 @@ INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_ma
                EM_DEBUG_LOG_SEC("Rename mailbox_id [%d] mailbox_name [%s]", target_mailbox_array[i].mailbox_id, target_mailbox_array[i].mailbox_name);
 
                if(input_mailbox_id == target_mailbox_array[i].mailbox_id) {
-                       if ((err = emstorage_rename_mailbox(target_mailbox_array[i].mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, true)) != EMAIL_ERROR_NONE) {
+                       if ((err = emstorage_rename_mailbox(multi_user_name, target_mailbox_array[i].mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, true)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -622,7 +762,7 @@ INTERNAL_FUNC int emcore_rename_mailbox(int input_mailbox_id, char *input_new_ma
                        renamed_mailbox_name = em_replace_string(target_mailbox_array[i].mailbox_name, old_mailbox_name, input_new_mailbox_name);
                        EM_DEBUG_LOG_SEC("renamed_mailbox_name[%s]", renamed_mailbox_name);
 
-                       if ((err = emstorage_rename_mailbox(target_mailbox_array[i].mailbox_id, renamed_mailbox_name, target_mailbox_array[i].alias, NULL, 0, true)) != EMAIL_ERROR_NONE) {
+                       if ((err = emstorage_rename_mailbox(multi_user_name, target_mailbox_array[i].mailbox_id, renamed_mailbox_name, target_mailbox_array[i].alias, NULL, 0, true)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -653,14 +793,14 @@ 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 (email_account_t *account, 
+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)
 {
        EM_PROFILE_BEGIN(emCoreMailboxOpen);
        EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account, input_mailbox_id,
                                                                                               result_stream, err_code);
-       
+
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        email_session_t *session = NULL;
@@ -675,7 +815,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        if (!emcore_get_current_session(&session)) {
                EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
                error = EMAIL_ERROR_SESSION_NOT_FOUND;
@@ -684,7 +824,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
 
        if (input_mailbox_id == 0 || input_mailbox_id != EMAIL_CONNECT_FOR_SENDING)
                is_connection_for = _SERVICE_THREAD_TYPE_RECEIVING;
-       else 
+       else
                is_connection_for = _SERVICE_THREAD_TYPE_SENDING;
 
 #ifdef __FEATURE_KEEP_CONNECTION__
@@ -700,14 +840,14 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                                *result_stream = connection_info->sending_server_stream;
                }
        }
-       
+
        if (*result_stream)
                EM_DEBUG_LOG("Stream reuse desired");
 #endif
 
        session->error = EMAIL_ERROR_NONE;
        emcore_set_network_error (EMAIL_ERROR_NONE);    /* set current network error as EMAIL_ERROR_NONE before network operation */
-       
+
        if (input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
                mailbox_name = EM_SAFE_STRDUP(ENCODED_PATH_SMTP);
        }
@@ -715,35 +855,36 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                mailbox_name = NULL;
        }
        else {
-               if ((error = emstorage_get_mailbox_by_id (input_mailbox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
+               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;
                }
                mailbox_name = EM_SAFE_STRDUP (mailbox->mailbox_name);
        }
 
-       if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {      
+       if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
                /*  open pop3/imap server */
-               if (!emcore_get_long_encoded_path_with_account_info (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;
                }
-               
-               EM_DEBUG_LOG("open mail connection to mbox_path [%s]", mbox_path);
-               
+
+               EM_DEBUG_LOG_SEC("open mail connection to mbox_path [%s]", mbox_path);
+
                session->auth = 0; /*  ref_account->receiving_auth ? 1  :  0 */
 
-               if (!(*result_stream = mail_open (*result_stream, mbox_path, IMAP_2004_LOG))) { 
+               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)) {
-                               if((error = emcore_refresh_xoauth2_access_token (account->account_id)) != EMAIL_ERROR_NONE) {
+                               if((error = emcore_refresh_xoauth2_access_token (multi_user_name, account->account_id)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emcore_refresh_xoauth2_access_token failed. [%d]", error);
                                }
                                else {
-                                       while (*result_stream == NULL && connection_retry_count < 5) {
+                                       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);
@@ -752,8 +893,18 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                                                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)) {
+                               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);
+                               }
+
+                               EM_SAFE_FREE(replaced_mbox_path);
                        }
-                       
+
                        if (*result_stream == NULL) { /* Finally, connection failed */
                                if (session->error == EMAIL_ERROR_UNKNOWN || session->error == EMAIL_ERROR_NONE)
                                        session->error = EMAIL_ERROR_CONNECTION_FAILURE;
@@ -762,7 +913,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                        }
                }
        }
-       else {  
+       else {
                /*  open smtp server */
                char *host_list[2] = {NULL, NULL};
 
@@ -773,7 +924,7 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                        send_stream = *result_stream;
                        /*
                        send_ret = smtp_send(send_stream, "RSET", 0);
-                       
+
                        if (send_ret != SMTP_RESPONSE_OK) {
                                EM_DEBUG_EXCEPTION("[SMTP] RSET --> [%s]", send_stream->reply);
                                send_stream = NULL;
@@ -782,16 +933,16 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                }
 #endif
                if (!*result_stream) {
-                       if (!emcore_get_long_encoded_path_with_account_info (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;
                        }
-                               
-                       EM_DEBUG_LOG("open SMTP connection to mbox_path [%s]", mbox_path);
-                       
+
+                       EM_DEBUG_LOG_SEC("open SMTP connection to mbox_path [%s]", mbox_path);
+
                        session->auth = account->outgoing_server_need_authentication ? 1  :  0;
-                       
+
                        host_list[0] = mbox_path;
 
                        if (!(*result_stream = smtp_open (host_list, 1))) {
@@ -799,10 +950,10 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                                                "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 || 
+                               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 (account->account_id)) != EMAIL_ERROR_NONE) {
+                                       if((error = emcore_refresh_xoauth2_access_token (multi_user_name, account->account_id)) != 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;
@@ -829,17 +980,17 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info (email_acco
                        }
                }
        }
-       
+
        ret = true;
-       
-FINISH_OFF: 
+
+FINISH_OFF:
 
 #ifdef __FEATURE_KEEP_CONNECTION__
        if (ret == true) {
                if(!connection_info) {
                        connection_info = em_malloc(sizeof(email_connection_info_t));
                        connection_info->account_id = account->account_id;
-                       if(!connection_info) 
+                       if(!connection_info)
                                EM_DEBUG_EXCEPTION("em_malloc for connection_info failed.");
                        else
                                emcore_append_connection_info(connection_info);
@@ -869,7 +1020,7 @@ FINISH_OFF:
        if (err_code != NULL)
                *err_code = error;
        EM_PROFILE_END(emCoreMailboxOpen);
-       EM_DEBUG_FUNC_END("ret [%d]", ret);     
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
@@ -882,23 +1033,23 @@ emcore_close_mailbox uses mail_close inside it.
 
 mail_close is only used in emcore_connect_to_remote_mailbox and emcore_reset_streams as an exception to above rule*/
 
-INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, char *mailbox, void **mail_stream, int *err_code)
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, char *mailbox, void **mail_stream, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], mail_stream[%p], err_code[%p]", account_id, mailbox, mail_stream, err_code);
-       
+
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        email_account_t *ref_account = NULL;
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
-       if (!ref_account) {             
+       if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
-               error = EMAIL_ERROR_INVALID_ACCOUNT;            
+               error = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
        
-       ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, mailbox, mail_stream, &error);
+       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, ref_account, mailbox, mail_stream, &error);
 
 FINISH_OFF:
 
@@ -936,8 +1087,8 @@ INTERNAL_FUNC void emcore_close_mailbox_partial_body_stream()
        EM_DEBUG_FUNC_END();
 }
 
-/* h.gahlaut@samsung.com :  21-10-2010 - 
-emcore_reset_stream() function is used to reset globally stored partial body thread and receiving thread streams 
+/* h.gahlaut@samsung.com :  21-10-2010 -
+emcore_reset_stream() function is used to reset globally stored partial body thread and receiving thread streams
 on account deletion and pdp deactivation */
 
 INTERNAL_FUNC void emcore_reset_streams()
@@ -946,35 +1097,36 @@ INTERNAL_FUNC void emcore_reset_streams()
 
        emcore_close_mailbox_receiving_stream();
        emcore_close_mailbox_partial_body_stream();
-       
+
        EM_DEBUG_FUNC_END();
        return;
 }
 
 #else /*  __FEATURE_KEEP_CONNECTION__ */
 
-INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
+INTERNAL_FUNC int emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account_id, input_mailbox_id, mail_stream, err_code);
-       
+
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        email_session_t *session = NULL;
        email_account_t *ref_account = NULL;
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
-       if (!ref_account)  {            
+       if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
-               error = EMAIL_ERROR_INVALID_ACCOUNT;            
+               error = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
+/*    Several threads call it, so check event status is disabled
        if (!emcore_check_thread_status()) {
                error = EMAIL_ERROR_CANCELLED;
                goto FINISH_OFF;
        }
-
+*/
        if (!emnetwork_check_network_status(&error)) {
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", error);
                goto FINISH_OFF;
@@ -982,13 +1134,46 @@ INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, int input_mai
 
        if (!emcore_get_empty_session(&session)) {
                EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
-               error = EMAIL_ERROR_SESSION_NOT_FOUND;
-               goto FINISH_OFF;
+/*             error = EMAIL_ERROR_SESSION_NOT_FOUND;
+               goto FINISH_OFF; */
+       }
+
+       ret = emcore_connect_to_remote_mailbox_with_account_info(multi_user_name, ref_account, input_mailbox_id, mail_stream, &error);
+
+       EM_DEBUG_LOG("ret[%d] incoming_server_type[%d] input_mailbox_id[%d]", ret, ref_account->incoming_server_type, input_mailbox_id);
+
+       if (ret == EMAIL_ERROR_NONE && input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
+               SENDSTREAM *send_stream = (SENDSTREAM*)*mail_stream;
+
+               if (send_stream && send_stream->protocol.esmtp.ok) {
+                       if (send_stream->protocol.esmtp.size.ok && send_stream->protocol.esmtp.size.limit > 0) {
+                               EM_DEBUG_LOG("Server size limit : %ld", send_stream->protocol.esmtp.size.limit);
+                               if (send_stream->protocol.esmtp.size.limit != ref_account->outgoing_server_size_limit) {
+                                       emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_id, "outgoing_server_size_limit", send_stream->protocol.esmtp.size.limit, true);
+                               }
+                       }
+               }
+       }
+       else if (ret == EMAIL_ERROR_NONE && ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4
+                       && input_mailbox_id != EMAIL_CONNECT_FOR_SENDING) {
+               IMAPCAP *capability = NULL;
+               /* check capability changes */
+               capability = imap_cap((MAILSTREAM*)*mail_stream);
+               EM_DEBUG_LOG("capability [%p]", capability);
+               if (capability) {
+                       EM_DEBUG_LOG("idle [%d] retrieval_mode[%d]", capability->idle, ref_account->retrieval_mode);
+                       if (capability->idle != ((ref_account->retrieval_mode & EMAIL_IMAP4_IDLE_SUPPORTED) == EMAIL_IMAP4_IDLE_SUPPORTED)) {
+                               if (capability->idle)
+                                       ref_account->retrieval_mode += EMAIL_IMAP4_IDLE_SUPPORTED;
+                               else
+                                       ref_account->retrieval_mode -= EMAIL_IMAP4_IDLE_SUPPORTED;
+                               emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_id, "retrieval_mode", ref_account->retrieval_mode, true);
+                       }
+               }
        }
 
-       ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, input_mailbox_id, mail_stream, &error);
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (ref_account) {
                emcore_free_account(ref_account);
@@ -996,10 +1181,10 @@ FINISH_OFF:
        }
 
        emcore_clear_session(session);
-       
+
        if (err_code)
                *err_code = error;
-       
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
@@ -1008,11 +1193,11 @@ FINISH_OFF:
 INTERNAL_FUNC int emcore_close_mailbox(int account_id, void *mail_stream)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_stream[%p]", account_id, mail_stream);
-       
+
        if (!mail_stream)  {
                return false;
        }
-       
+
 #ifdef __FEATURE_KEEP_CONNECTION__
        thread_t thread_id = THREAD_SELF();
 
@@ -1037,15 +1222,15 @@ INTERNAL_FUNC int emcore_close_mailbox(int account_id, void *mail_stream)
 INTERNAL_FUNC void emcore_free_mailbox_list(email_mailbox_t **mailbox_list, int count)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
-       
+
        if (count <= 0 || !mailbox_list || !*mailbox_list)  {
                EM_DEBUG_EXCEPTION("INVALID_PARAM: mailbox_list[%p], count[%d]", mailbox_list, count);
                return;
        }
-               
+
        email_mailbox_t *p = *mailbox_list;
        int i;
-               
+
        for (i = 0; i < count; i++)
                emcore_free_mailbox(p+i);
 
@@ -1065,7 +1250,7 @@ INTERNAL_FUNC void emcore_free_mailbox(email_mailbox_t *mailbox)
        EM_SAFE_FREE(mailbox->mailbox_name);
        EM_SAFE_FREE(mailbox->alias);
        EM_SAFE_FREE(mailbox->eas_data);
-       
+
        EM_DEBUG_FUNC_END();
 }
 
@@ -1115,7 +1300,7 @@ INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_li
        int i = 0;
        int bIsNotUserMailbox = false;
        email_mailbox_type_item_t   *pMailboxType1 = NULL ;
-       
+
        for (i = 0 ; i < MAX_MAILBOX_TYPE ; i++) {
                pMailboxType1 = g_mailbox_type + i;
                if (0 == EM_SAFE_STRCMP(pMailboxType1->mailbox_name, mailbox_list->mailbox_name)) { /*prevent 24662*/
@@ -1135,15 +1320,15 @@ INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_li
 INTERNAL_FUNC int  emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id , int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       int handle; 
+       int handle;
        email_event_t *event_data = NULL;
 
        if (!mailbox || mailbox->account_id <= 0) {
                EM_DEBUG_LOG(" mailbox[%p]", mailbox);
-               
+
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
@@ -1154,7 +1339,7 @@ INTERNAL_FUNC int  emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id
        event_data->event_param_data_4 = mail_id;
        event_data->event_param_data_1 = NULL;
        event_data->event_param_data_5 = mailbox->mailbox_id;
-                       
+
        if (!emcore_insert_event_for_sending_mails(event_data, &handle, &err))  {
                EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
                goto FINISH_OFF;
@@ -1163,7 +1348,7 @@ INTERNAL_FUNC int  emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id
 
        ret = true;
 
-FINISH_OFF: 
+FINISH_OFF:
 
        if (ret == false && event_data) {
                emcore_free_event(event_data);
@@ -1172,18 +1357,18 @@ FINISH_OFF:
 
        if (err_code)
                *err_code = err;
-       
+
        return ret;
 }
 
-INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inserted, int *err_code)
+INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(char *multi_user_name, int *is_event_inserted, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int activity_count = 0;
        int ret = false;
        int error = EMAIL_ERROR_NONE;
 
-       if (false == emstorage_get_pbd_activity_count(&activity_count, false, &error)) {
+       if (false == emstorage_get_pbd_activity_count(multi_user_name, &activity_count, false, &error)) {
                EM_DEBUG_LOG("emstorage_get_pbd_activity_count failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -1192,7 +1377,7 @@ INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inse
 
                email_event_partial_body_thd pbd_event;
 
-               /* Carefully initialise the event */ 
+               /* Carefully initialise the event */
                memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
 
                pbd_event.event_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
@@ -1206,15 +1391,15 @@ INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inse
                        /*Not checking for NULL here because is_event_inserted is never NULL. */
                        *is_event_inserted = true;
                }
-               
+
        }
        else {
-               *is_event_inserted = false;     
+               *is_event_inserted = false;
        }
 
        ret = true;
-       
-       FINISH_OFF: 
+
+FINISH_OFF:
 
        if (NULL != err_code) {
                *err_code = error;
@@ -1223,35 +1408,35 @@ INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inse
        return ret;
 }
 
-INTERNAL_FUNC int emcore_get_mailbox_by_type(int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *result_mailbox, int *err_code)
+INTERNAL_FUNC int emcore_get_mailbox_by_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *result_mailbox, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], result_mailbox [%p], err_code [%p]", account_id, result_mailbox, err_code);
        int ret = false, err = EMAIL_ERROR_NONE;
        emstorage_mailbox_tbl_t *mail_box_tbl_spam = NULL;
 
-       if (result_mailbox == NULL)     {       
+       if (result_mailbox == NULL)     {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &mail_box_tbl_spam, false, &err)) {
-
+       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, mailbox_type, &mail_box_tbl_spam, false, &err)) {
                EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed - %d", err);
        }
-       else {  
+       else {
                if (mail_box_tbl_spam) {
                        result_mailbox->mailbox_type = mail_box_tbl_spam->mailbox_type;
                        result_mailbox->mailbox_name = EM_SAFE_STRDUP(mail_box_tbl_spam->mailbox_name);
                        result_mailbox->account_id = mail_box_tbl_spam->account_id;
+                       result_mailbox->mailbox_id = mail_box_tbl_spam->mailbox_id;
                        result_mailbox->mail_slot_size = mail_box_tbl_spam->mail_slot_size;
                        if (!emstorage_free_mailbox(&mail_box_tbl_spam, 1, &err))
                                EM_DEBUG_EXCEPTION(" emstorage_free_mailbox Failed [%d]", err);
-                       ret = true;     
+                       ret = true;
                }
        }
 
-FINISH_OFF: 
+FINISH_OFF:
        if (err_code)
                *err_code = err;
        EM_DEBUG_FUNC_END();
@@ -1264,7 +1449,7 @@ INTERNAL_FUNC int emcore_local_activity_sync(int account_id, int *err_code)
        EM_DEBUG_FUNC_BEGIN();
 
        EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
-       
+
        int *activity_id_list = NULL;
        int activity_count = 0;
        int err         = 0;
@@ -1295,7 +1480,7 @@ INTERNAL_FUNC int emcore_local_activity_sync(int account_id, int *err_code)
                        EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
                        goto FINISH_OFF;
                }
-               
+
                ret = true;
        }
 
@@ -1307,11 +1492,11 @@ FINISH_OFF:
        }
 
        if (activity_id_list)
-               emstorage_free_activity_id_list(activity_id_list, &err); 
-       
+               emstorage_free_activity_id_list(activity_id_list, &err);
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 
 }
@@ -1321,7 +1506,7 @@ INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
        EM_DEBUG_FUNC_BEGIN();
 
        EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
-       
+
        emstorage_activity_tbl_t *local_activity = NULL;
        int *activity_id_list = NULL;
        int activity_count = 0;
@@ -1343,7 +1528,7 @@ INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
                goto FINISH_OFF;
        }
 
-       
+
        if (activity_count > 0) {
                event_data = em_malloc(sizeof(email_event_t));
                event_data->type = EMAIL_EVENT_LOCAL_ACTIVITY;
@@ -1351,8 +1536,8 @@ INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
                if (!emcore_insert_event_for_sending_mails(event_data, &handle, &err)) {
                        EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
                        goto FINISH_OFF;
-               }       
-               
+               }
+
                ret = true;
        }
 
@@ -1364,14 +1549,14 @@ FINISH_OFF:
        }
 
        if (local_activity)
-               emstorage_free_local_activity(&local_activity, activity_count, NULL); 
+               emstorage_free_local_activity(&local_activity, activity_count, NULL);
 
        if (activity_id_list)
-               emstorage_free_activity_id_list(activity_id_list, &err);        
+               emstorage_free_activity_id_list(activity_id_list, &err);
 
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 }
 
index 5c0b09e..6310798 100755 (executable)
 #include "email-internal-types.h"
 #include "lnx_inc.h"
 #include "email-utilities.h"
-#include "email-core-global.h"
 #include "email-core-utils.h"
 #include "email-core-mail.h"
 #include "email-core-mime.h"
+#include "email-core-gmime.h"
 #include "email-storage.h"
 #include "email-core-event.h"
 #include "email-core-account.h"
 #include "email-core-signal.h"
 #include "email-core-mailbox-sync.h"
+#include "email-core-container.h"
 #include "email-debug-log.h"
 
 #define MIME_MESG_IS_SOCKET
 
 #define EML_FOLDER           20 /*  save eml content to temporary folder */
 
-/*
-       MIME Structure Example
-
-       (part 0)        multipart/mixed
-       (part 1)                multipart/alternative
-       (part 1.1)                      text/plain              <- text message
-       (part 1.2)                      text/html               <- html message
-       (part 2)                text/plain                      <- text attachment
-
-
-       (part 0)        multipart/related
-       (part 1)                multipart/alternative
-       (part 1.1)                      text/plain                      <- text message
-       (part 1.2)                      text/html                       <- html message
-       (part 2)                image/png                               <- inline image
-       (part 2)                image/png                               <- inline image
-
-
-       (part 0)        multipart/mixed
-       (part 1.1)              multipart/related
-       (part 2.1)                      multipart/alternative
-       (part 3.1)                              text/plain(body)  <- text message
-       (part 3.2)                              text/html(body) <- html message
-       (part 2.2)                      image/png(related)      <- inline image
-       (part 1.2)              image/png(attachment)   <- image attachment
-*/
-
-/*  Text and Attachment Holde */
-/* struct _m_content_info  */
-/*     int grab_type;  */              /*  1 :  text and attachment list */
-                                               /*  2 :  attachmen */
-/*     int file_no; */                 /*  attachment sequence to be downloaded (min : 1 */
-/*     struct text_data  */
-/*             char *plain;    */      /*  body plain tex */
-/*             char *plain_charset */ /*  charset of plai */
-/*             char *html; */ /*  body html tex */
-/*     } text */
-
-/*     struct attachment_info  */
-/*             int   type;     */              /*  1 : inline 2 : attachmen */
-/*             char *name;     */              /*  attachment filenam */
-/*             int   size;             */      /*  attachment siz */
-/*             char *save;     */              /*  content saving filenam */
-/*             struct attachment_info *next */
-/*     } *file */
-/* } */
-
-/* --------------------- MIME Structure --------------------------------- */
-/*  MIME Header Parameter (Content-Type, Content-Disposition, ... */
-struct _parameter {
-       char                            *name;                  /*  parameter name */
-       char                            *value;                 /*  parameter value */
-       struct _parameter       *next;                  /*  next paramete */
-};
-
-/*  Content-Disposition */
-struct _disposition {
-       char                            *type;                  /*  "inline" "attachment */
-       struct _parameter       *parameter;             /*  "filename", .. */
-};
-
-/*  RFC822 Header */
-struct _rfc822header {
-       char                            *return_path;   /*  error return path */
-       char                            *received;
-       char                            *date;
-       char                            *from;
-       char                            *subject;
-       char                            *sender;
-       char                            *to;
-       char                            *cc;
-       char                            *bcc;
-       char                            *reply_to;
-       char                            *priority;
-       char                            *ms_priority;
-       char                            *dsp_noti_to;
-       char                            *others;
-};
-
-/*  MIME Part Header */
-struct _m_part_header {
-       char                            *type;                      /* text, image, audio, video, application, multipart, message */
-       char                            *subtype;                   /* plain, html, jpeg, .. */
-       char                            *encoding;                  /* encoding typ */
-       struct _parameter       *parameter;                 /* content-type parameter  :  "boundary" "charset" .. */
-       char                            *desc;                      /* description */
-       char                            *disp_type;                 /* disposition type  :  "inline" "attachment", */
-       struct _parameter       *disp_parameter;    /* disposition parameter  :  "filename", .. */
-       char                            *content_id;        /* content id  :  it is inline  */
-       char                            *content_location;      /* content location  :  "inline" location  */
-};
-
-/*  MIME Message Header */
-struct _m_mesg_header {
-       char                               *version;          /* MIME Version */
-       struct _m_part_header  *part_header;      /* MIME Part Header */
-       /* char                   *message_context; */  /* Message-Context : Voice-message, Video-message, Fax-message... */
-       /* int                     content_duration; */ /* Content-Duration */
-       /* int                     x_content_pages;  */ /* X-Content-Pages */
-       /* char                   *sensitivity; */     /* Sensitivity */
-};
-
-/*  MIME Multipart Body linked list */
-typedef struct _m_body _m_body_t;
-struct _m_part{
-       _m_body_t              *body;             /* part body */
-       struct _m_part         *next;             /* the next found part */
-};
-
-/*  MIME Multipart Body */
-struct _m_body {
-       struct _m_part_header  *part_header;      /* MIME Part Header */
-       struct _m_part          nested;           /* nested structure if contain multipart */
-       char                   *text;             /* text if not contain multipart */
-       int                     size;             /* text size if not contain multipart */
-       char                   *holdingfile;
-};
 
-/*  MIME Message */
-struct _m_mesg {
-       struct _rfc822header    *rfc822header;    /* RFC822 Header */
-       struct _m_mesg_header   *header;          /* MIME Message Header */
-       struct _m_part           nested;          /* nested structure if contain multipart */
-       char                    *text;            /* text if not contain multipart */
-       int                      size;            /* text size if not contain multipart */
-};
 /* ---------------------------------------------------------------------- */
 /*  Global variable */
 static int  eml_data_count = 0;
@@ -288,7 +164,6 @@ void emcore_mime_free_mime(struct _m_mesg *mmsg);
 /*         3 :  save content to file (holder is file name */
 static int emcore_mime_get_content_data(void *stream,
                                        int  is_file,
-                       int   is_text,
                                        char *boundary_str,
                                        char *content_encoding,
                                        int *end_of_parsing,
@@ -298,8 +173,8 @@ static int emcore_mime_get_content_data(void *stream,
                                        void *callback,
                                        int *err_code);
 
-int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int encoding, int mode, int is_text, int fd, char **holder, int *end_of_parsing, int *size);
-int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int encoding, int mode, int is_text, int fd, char **holder, int *end_of_parsing, int *size);
+int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int encoding, int mode, int fd, char **holder, int *end_of_parsing, int *size);
+int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int encoding, int mode, int fd, char **holder, int *end_of_parsing, int *size);
 /*  skip content data to boundary_str or end of fil */
 int emcore_mime_skip_content_data(void *stream,
                                                        int is_file,
@@ -466,13 +341,14 @@ int emcore_mime_flush_receiving_buffer(void *stream, int is_file, char *boundary
        }
 
        while (TRUE)  {
+/* Several threads including event thd call it, don't delete comment
                if (!emcore_check_thread_status()) {
                        EM_DEBUG_FUNC_END("EMAIL_ERROR_CANCELLED");
                        if (err_code)
                                *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
                }
-
+*/
                if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, &err)) ||
                        (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, &err))) {
                        if (err != EMAIL_ERROR_NO_MORE_DATA) {
@@ -480,7 +356,7 @@ int emcore_mime_flush_receiving_buffer(void *stream, int is_file, char *boundary
                                local_end_of_parsing = 0;
                                break;
                        }
-       
+
                        local_end_of_parsing = 1;
                        break;
                }
@@ -526,13 +402,14 @@ int emcore_parse_mime(void *stream, int is_file, struct _m_content_info *cnt_inf
                return false;
        }
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_CANCELLED;
                emcore_mime_free_mime(mmsg);
                return false;
        }
-
+*/
        /*  2. parse body */
        EM_DEBUG_LOG(">>>>>> 2. parse body");
        if (mmsg && mmsg->header && mmsg->header->part_header && mmsg->header->part_header->parameter)  {
@@ -570,11 +447,13 @@ int emcore_mime_parse_header(void *stream, int is_file, struct _rfc822header **r
        char *pTemp = NULL;
        int  is_longheader;
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
        }
+*/
 
        if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))  {
@@ -588,11 +467,13 @@ int emcore_mime_parse_header(void *stream, int is_file, struct _rfc822header **r
                return false;
        }
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
                }
+*/
 
        while (TRUE)  {
                EM_DEBUG_LOG("buf[%s]", buf);
@@ -669,11 +550,13 @@ int emcore_mime_parse_header(void *stream, int is_file, struct _rfc822header **r
                                emcore_mime_set_rfc822_header_value(&tmp_rfc822header, name, value, err_code);
                }
 
+/* Several threads including event thd call it, don't delete comment
                if (!emcore_check_thread_status())  {
                        if (err_code != NULL)
                                *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
                }
+*/
 
                if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                        (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))  {
@@ -711,14 +594,16 @@ int emcore_mime_parse_part_header(void *stream, int is_file, struct _m_part_head
        char *p = NULL;
        int is_longheader = false;
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                        if (err_code != NULL)
                                *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
        }
+*/
 
        if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
-       (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))
+            (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))
                return false;
 
        tmp_header = em_malloc(sizeof(struct _m_part_header));
@@ -753,11 +638,13 @@ int emcore_mime_parse_part_header(void *stream, int is_file, struct _m_part_head
 
                emcore_mime_set_part_header_value(&tmp_header, name, value, err_code);
 
+/* Several threads including event thd call it, don't delete comment
                if (!emcore_check_thread_status())  {
                        if (err_code != NULL)
                                *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
                }
+*/
 
                if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))  {
@@ -784,17 +671,19 @@ int emcore_mime_parse_body(void *stream, int is_file, struct _m_mesg *mmsg, stru
                EM_DEBUG_EXCEPTION("Invalid paramter");
                *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
-       } 
-       
+       }
+
        char *content_type = NULL, *content_encoding = NULL, *holder = NULL, *attachment_name, *t = NULL;
        int type = 0, end_of_parsing = 0, size;
        int err = EMAIL_ERROR_NONE;
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_CANCELLED;
                return false;
        }
+*/
 
        if (mmsg->header)
                content_type = emcore_mime_get_header_value(mmsg->header->part_header, CONTENT_TYPE, err_code);
@@ -843,7 +732,7 @@ int emcore_mime_parse_body(void *stream, int is_file, struct _m_mesg *mmsg, stru
                        if (mmsg->header && emcore_mime_get_header_value(mmsg->header->part_header, CONTENT_DISPOSITION, err_code)) {
                                attachment_name = emcore_mime_get_header_value(mmsg->header->part_header, CONTENT_FILENAME, err_code);
                                /* if (!attachment_name) attachment_name = "unknown" */
-                               if (attachment_name) EM_DEBUG_LOG(" attachment = [%s]", attachment_name);
+                               if (attachment_name) EM_DEBUG_LOG_SEC(" attachment = [%s]", attachment_name);
                        }
 
                        if (strcasestr(content_type, "PKCS7-MIME")) {
@@ -858,13 +747,13 @@ int emcore_mime_parse_body(void *stream, int is_file, struct _m_mesg *mmsg, stru
                                if (cnt_info->file) {
                                        cnt_info->file->type = ATTACHMENT;
                                        cnt_info->file->name = EM_SAFE_STRDUP(attachment_name);
-                                       if (!emcore_mime_get_content_data(stream, is_file, false, NULL, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
+                                       if (!emcore_mime_get_content_data(stream, is_file, NULL, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
                                                EM_DEBUG_EXCEPTION("emcore_mime_get_content_data failed : [%d]", err_code);
                                                return false;
                                        }
                                        cnt_info->file->save = holder;
                                        cnt_info->file->size = size;
-                               cnt_info->file->attachment_mime_type = EM_SAFE_STRDUP(content_type);
+                                       cnt_info->file->attachment_mime_type = EM_SAFE_STRDUP(content_type);
                                        cnt_info->file->next = NULL;
                                }
                                break;
@@ -873,7 +762,7 @@ int emcore_mime_parse_body(void *stream, int is_file, struct _m_mesg *mmsg, stru
                        if (cnt_info->grab_type & GRAB_TYPE_TEXT)  {
                                /* EM_DEBUG_LINE */
                                /*  get content data. content data is saved in file */
-                               if (!emcore_mime_get_content_data(stream, is_file, true, NULL, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
+                               if (!emcore_mime_get_content_data(stream, is_file, NULL, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
                                        EM_DEBUG_EXCEPTION("emcore_mime_get_content_data failed : [%d]", err_code);
                                        return false;
                                }
@@ -934,7 +823,6 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
        int content_disposition_type = 0;
        int end_of_parsing = 0;
        int size = 0, local_err_code = EMAIL_ERROR_NONE;
-       int is_skip = false;
        int result = 0;
        int inline_attach_index = 0;
 
@@ -949,11 +837,13 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
        /*  goto the first found useful mime dat */
        EM_DEBUG_LOG("Before first loop");
        while (true)  {
+/* Several threads including event thd call it, don't delete comment
                if (!emcore_check_thread_status())  {
                        if (err_code != NULL)
                                *err_code = EMAIL_ERROR_CANCELLED;
                        return false;
                }
+*/
                if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code))) {
                        EM_DEBUG_EXCEPTION("emcore_mime_get_line_from_sock failed.");
@@ -1060,22 +950,11 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
 
                        default:
                                EM_DEBUG_LOG("default");
+                               content_disposition_type = 0;
                                attachment_name = NULL;
                                content_id = NULL;
                                content_disposition = NULL;
 
-                               if (type == TYPE_MESSAGE)
-                                       is_skip = true;
-
-                               if (is_skip == true)  {
-                                       if (!emcore_mime_skip_content_data(stream, is_file, boundary_str, &end_of_parsing, &size, NULL, err_code))
-                                               EM_DEBUG_EXCEPTION("emcore_mime_skip_content_data failed...");
-
-                                       emcore_mime_free_part_body(tmp_body);
-                                       EM_DEBUG_LOG_MIME("break");
-                                       break;
-                               }
-
                                /*  first check inline content */
                                /*  if the content id or content location exis */
                                content_disposition = emcore_mime_get_header_value(tmp_body->part_header, CONTENT_DISPOSITION, err_code);
@@ -1088,13 +967,14 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        if (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result) {
 
                                                SNPRINTF(temp_inline_attachment_name, sizeof(temp_inline_attachment_name), "image_%d", ++inline_attach_index);
-                                               attachment_name = temp_inline_attachment_name;  
+                                               attachment_name = temp_inline_attachment_name;
 
                                                if (emcore_search_string_from_file(cnt_info->text.html, content_id, attachment_name, &result) != EMAIL_ERROR_NONE) {
                                                        EM_DEBUG_EXCEPTION("emcore_search_string_from file failed");
                                                }
-                                               
+
                                                content_disposition_type = INLINE_ATTACHMENT;
+
                                        } else if (!strcasecmp(content_disposition ? content_disposition : "", "attachment")) {
                                                attachment_name = emcore_mime_get_header_value(tmp_body->part_header, CONTENT_NAME, err_code);
 
@@ -1103,6 +983,14 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
 
                                                content_disposition_type = ATTACHMENT;
 
+                                       } else if (!strcasecmp(content_disposition ? content_disposition : "", "inline")) {
+                                               attachment_name = emcore_mime_get_header_value(tmp_body->part_header, CONTENT_NAME, err_code);
+
+                                               if (!attachment_name)
+                                                       attachment_name = emcore_mime_get_header_value(tmp_body->part_header, CONTENT_FILENAME, err_code);
+
+                                               content_disposition_type = INLINE_ATTACHMENT;
+
                                        } else {
                                                EM_DEBUG_EXCEPTION("Unknown mime type");
                                        }
@@ -1113,12 +1001,15 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                                if (!attachment_name)
                                                        attachment_name = emcore_mime_get_header_value(tmp_body->part_header, CONTENT_FILENAME, err_code);
 
+                                               if (!attachment_name && type == TYPE_MESSAGE)
+                                                       attachment_name = strdup("unknown_name");
+
                                                content_disposition_type = ATTACHMENT;
 
                                        } else if (!strcasecmp(content_disposition ? content_disposition : "", "inline")) {
 
                                                SNPRINTF(temp_inline_attachment_name, sizeof(temp_inline_attachment_name), "image_%d", ++inline_attach_index);
-                                               attachment_name = temp_inline_attachment_name;  
+                                               attachment_name = temp_inline_attachment_name;
 
                                                if (emcore_search_string_from_file(cnt_info->text.html, content_id, attachment_name, &result) != EMAIL_ERROR_NONE) {
                                                        EM_DEBUG_EXCEPTION("emcore_search_string_from file failed");
@@ -1136,6 +1027,7 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        }
                                }
 
+/* Several threads including event thd call it, don't delete comment
                                if (!emcore_check_thread_status())  {
                                        if (err_code != NULL)
                                                *err_code = EMAIL_ERROR_CANCELLED;
@@ -1144,13 +1036,14 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        EM_DEBUG_FUNC_END("false");
                                        return false;
                                }
+*/
 
                                EM_DEBUG_LOG_SEC("attachment_name : [%s]", attachment_name);
                                /*  get content and content information */
                                if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT)  { /*  text */
                                        /*  get content by buffer */
                                        EM_DEBUG_LOG_MIME("attachment_name is NULL. It's a text message");
-                                       if (!emcore_mime_get_content_data(stream, is_file, true, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
+                                       if (!emcore_mime_get_content_data(stream, is_file, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
                                                EM_DEBUG_EXCEPTION("emcore_mime_get_content_data failed [%d]", err_code);
                                                emcore_mime_free_part_body(tmp_body);
                                                return false;
@@ -1182,7 +1075,7 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                                        EM_DEBUG_LOG(" cnt_info->text.plain [%s]", cnt_info->text.plain);
                                                }
                                        } else {
-                                               EM_SAFE_FREE (holder);                                          
+                                               EM_SAFE_FREE (holder);
                                        }
                                } else {                /*  attachment */
                                        EM_DEBUG_LOG("attachment_name is not NULL. It's a attachment");
@@ -1204,13 +1097,13 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        file->name                 = EM_SAFE_STRDUP(attachment_name);
                                        file->content_id           = EM_SAFE_STRDUP(tmp_body->part_header->content_id);
                                        file->attachment_mime_type = EM_SAFE_STRDUP(content_type);
-                                       
+
 
                                        /*  check if the current file is target file */
                                        if ((cnt_info->grab_type & GRAB_TYPE_ATTACHMENT) || file->type == INLINE_ATTACHMENT)  {
                                                /*  get content by file */
                                                EM_DEBUG_LOG_MIME("Trying to get content");
-                                               if (!emcore_mime_get_content_data(stream, is_file, false, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
+                                               if (!emcore_mime_get_content_data(stream, is_file, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_FILE, &holder, &size, NULL, err_code)) {
                                                        EM_DEBUG_EXCEPTION("emcore_mime_get_content_data failed [%d]", err_code);
                                                        emcore_mime_free_part_body(tmp_body);
                                                        emcore_free_attachment_info(file);
@@ -1222,7 +1115,7 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        } else {
                                                /*  only get content size */
                                                EM_DEBUG_LOG_MIME("Pass downloading");
-                                               if (!emcore_mime_get_content_data(stream, is_file, false, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_SIZE, NULL, &size, NULL, err_code)) {
+                                               if (!emcore_mime_get_content_data(stream, is_file, boundary_str, content_encoding, &end_of_parsing, SAVE_TYPE_SIZE, NULL, &size, NULL, err_code)) {
                                                        EM_DEBUG_EXCEPTION("emcore_mime_get_content_data failed [%d]", err_code);
                                                        emcore_mime_free_part_body(tmp_body);
                                                        emcore_free_attachment_info(file);
@@ -1238,7 +1131,18 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
 
                                        file->size = size;
                                         file->attachment_mime_type = strdup(content_type);
-                                        file->save_status = end_of_parsing;
+
+                                       switch (end_of_parsing) {
+                                       case 0:
+                                       case 1:
+                                               /* Downloaded */
+                                               file->save_status = 1;
+                                               break;
+                                       case 2:
+                                               file->save_status = 2;
+                                               break;
+                                       }
+
 
                                        if (strstr(content_type, APPLICATION_STR))  {
                                                pTemp = content_type + EM_SAFE_STRLEN(APPLICATION_STR);
@@ -1260,6 +1164,7 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                                temp_file->next = file;
                                }
 
+/* Several threads including event thd call it, don't delete comment
                                if (!emcore_check_thread_status())  {
                                        if (err_code != NULL)
                                                *err_code = EMAIL_ERROR_CANCELLED;
@@ -1268,6 +1173,7 @@ int emcore_mime_parse_part(void *stream, int is_file, struct _m_part_header *par
                                        EM_DEBUG_FUNC_END("false");
                                        return false;
                                }
+*/
 
                                if (!nested->body)
                                        nested->body = tmp_body;
@@ -1372,6 +1278,7 @@ int emcore_mime_set_rfc822_header_value(struct _rfc822header **header, char *nam
                *p = t;
        }
 
+       EM_DEBUG_FUNC_END();
        return true;
 }
 
@@ -1388,6 +1295,7 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
        }
 
        struct _parameter *p = NULL;
+       struct _parameter *tmp = NULL;
        char *p_val = NULL;
 
        if (!*header)  {
@@ -1399,16 +1307,17 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
        }
 
        em_upper_string(name);
-       
+
        em_trim_left(value);
        em_trim_right(value);
 
+/* Several threads including event thd call it, don't delete comment
        if (!emcore_check_thread_status())  {
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_CANCELLED;
                return false;
        }
-
+*/
        if (name) {
                if (strncmp(name, "CONTENT-TYPE", strlen("CONTENT-TYPE")) == 0)  {
                        p_val = strtok(value, ";");
@@ -1421,8 +1330,16 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
 
                                /*  Content-Type Parameter (format :  "name =value" */
                                do {
-                                       if (emcore_mime_get_param_from_str(p_val, &p, err_code))
+                                       if (emcore_mime_get_param_from_str(p_val, &p, err_code)) {
+                                               /* Save the start node */
+                                               tmp = (*header)->parameter;
+                                               /* insert node */
                                                emcore_mime_add_param_to_list(&((*header)->parameter), p, err_code);
+                                               if (tmp != NULL) {
+                                                       (*header)->parameter = tmp;
+                                                       tmp = NULL;
+                                               }
+                                       }
                                        else /* name= CRLF value */ {
                                                struct _parameter *t = (*header)->parameter;
                                                while (t && t->next) {
@@ -1435,38 +1352,48 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
                                                        char *pointer = NULL;
 
                                                        if (EM_SAFE_STRLEN(p_val) > 0)  {
-                                                               if ((pointer = strchr(p_val, '\"')))  {
-                                                                       p_val = pointer + 1;
-                                                                       if (!*p_val) return false;
+                                                               /* "=?charset?B?value?= */
+                                                               /* "=?charset?B?value.... */
+                                                               /* =?charset?B?value"     */
+                                                               if ((pointer = strchr(p_val, '\"'))) {
+                                                                       if (!*(pointer + 1)) { /* line end '"' */
+                                                                               if (*p_val == *pointer) /* Example : '"' */
+                                                                                       return true;
+                                                                               else
+                                                                                       *pointer = '\0'; /* Example : 'abcde"' */
+                                                                       }
+                                                                       else { /* line start '"' */
+                                                                               p_val = pointer + 1;
+                                                                               if ((pointer = strchr(p_val, '\"')))
+                                                                                       *pointer = '\0';
+                                                                       }
                                                                }
-                                                               if ((pointer = strchr(p_val, '\"')))
-                                                                       *pointer = '\0';
 
                                                                /*  = ? ENCODING_TYPE ? B(Q) ? ENCODED_STRING ? */
-                                                               int err = EMAIL_ERROR_NONE;
                                                                char *utf8_text = NULL;
 
-                                                               if (!(utf8_text = emcore_decode_rfc2047_text(p_val, &err))) {
-                                                                       EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_text failed [%d]", err);
+                                                               if (!(utf8_text = emcore_gmime_get_decoding_text(p_val))) {
+                                                                       EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed");
                                                                        continue; /*prevent 53050*/
                                                                }
-                                                               EM_DEBUG_LOG("utf8_text : [%s]", utf8_text);
+                                                               EM_DEBUG_LOG_SEC("utf8_text : [%s]", utf8_text);
 
                                                                if (t->value == NULL) {
                                                                        EM_DEBUG_LOG("value is NULL");
                                                                        t->value = EM_SAFE_STRDUP(utf8_text);
                                                                } else {
                                                                        EM_DEBUG_LOG("value is not NULL");
-                                                                       t->value = realloc(t->value, strlen(t->value) + EM_SAFE_STRLEN(utf8_text) + 1); 
-                                                                       strcpy((t->value + strlen(utf8_text)), utf8_text);
+                                                                       t->value = realloc(t->value, strlen(t->value) + EM_SAFE_STRLEN(utf8_text) + 1);
+                                                                       strcpy((t->value + strlen(t->value)), utf8_text);
+                                                                       EM_DEBUG_LOG("long name : [%s]", t->value);
                                                                }
-               
-                                                               EM_DEBUG_LOG("value : [%s]", t->value); 
+
+                                                               EM_DEBUG_LOG("value : [%s]", t->value);
                                                                EM_SAFE_FREE (utf8_text);
                                                        }
                                                }
                                        }
-                               } while ((p_val = strtok(NULL, ";"))); 
+                               } while ((p_val = strtok(NULL, ";")));
                        }
                }
                else if (strncmp(name, "CONTENT-TRANSFER-ENCODING", strlen("CONTENT-TRANSFER-ENCODING")) == 0)  {
@@ -1485,16 +1412,73 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
                                        em_upper_string(p_val);
                                        (*header)->disp_type = EM_SAFE_STRDUP(p_val);
                                }
-                               else  { /*  Content-Disposition parameter (format :  "name =value" */
-                                       if (emcore_mime_get_param_from_str(p_val, &p, err_code))
+                               /*  Content-Disposition Parameter (format :  "name =value" */
+                               do {
+                                       if (emcore_mime_get_param_from_str(p_val, &p, err_code)) {
+                                               /* Save the start node */
+                                               tmp = (*header)->disp_parameter;
+                                               /* insert node */
                                                emcore_mime_add_param_to_list(&((*header)->disp_parameter), p, err_code);
-                               }
+                                               if (tmp != NULL) {
+                                                       (*header)->disp_parameter = tmp;
+                                                       tmp = NULL;
+                                               }
+                                       }
+                                       else /* name= CRLF value */ {
+                                               struct _parameter *t = (*header)->disp_parameter;
+                                               while (t && t->next) {
+                                                       EM_DEBUG_LOG_SEC("name : [%s]", t->name);
+                                                       EM_DEBUG_LOG("value : [%s]", t->value);
+                                                       t = t->next;
+                                               }
 
-                               /*  repeatedly get paramete */
-                               while ((p_val = strtok(NULL, ";")))  {
-                                       if (emcore_mime_get_param_from_str(p_val, &p, err_code))
-                                               emcore_mime_add_param_to_list(&((*header)->disp_parameter), p, err_code);
-                               }
+                                               if (t && t->name) {
+                                                       char *pointer = NULL;
+
+                                                       if (EM_SAFE_STRLEN(p_val) > 0)  {
+                                                               /* "=?charset?B?value?= */
+                                                               /* "=?charset?B?value.... */
+                                                               /* =?charset?B?value"     */
+                                                               if ((pointer = strchr(p_val, '\"'))) {
+                                                                       if (!*(pointer + 1)) { /* line end '"' */
+                                                                               if (*p_val == *pointer) /* Example : '"' */
+                                                                                       return true;
+                                                                               else
+                                                                                       *pointer = '\0'; /* Example : 'abcde"' */
+                                                                       }
+                                                                       else { /* line start '"' */
+                                                                               p_val = pointer + 1;
+                                                                               if ((pointer = strchr(p_val, '\"')))
+                                                                                       *pointer = '\0';
+                                                                       }
+                                                               }
+
+                                                               /*  = ? ENCODING_TYPE ? B(Q) ? ENCODED_STRING ? */
+                                                               int err = EMAIL_ERROR_NONE;
+                                                               char *utf8_text = NULL;
+
+                                                               if (!(utf8_text = emcore_gmime_get_decoding_text(p_val))) {
+                                                                       EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed [%d]", err);
+                                                                       continue; /*prevent 53050*/
+                                                               }
+                                                               EM_DEBUG_LOG("utf8_text : [%s]", utf8_text);
+
+                                                               if (t->value == NULL) {
+                                                                       EM_DEBUG_LOG("value is NULL");
+                                                                       t->value = EM_SAFE_STRDUP(utf8_text);
+                                                               } else {
+                                                                       EM_DEBUG_LOG("value is not NULL");
+                                                                       t->value = realloc(t->value, strlen(t->value) + EM_SAFE_STRLEN(utf8_text) + 1);
+                                                                       strcpy((t->value + strlen(t->value)), utf8_text);
+                                                                       EM_DEBUG_LOG("long name : [%s]", t->value);
+                                                               }
+
+                                                               EM_DEBUG_LOG_SEC("value : [%s]", t->value);
+                                                               EM_SAFE_FREE (utf8_text);
+                                                       }
+                                               }
+                                       }
+                               } while ((p_val = strtok(NULL, ";")));
                        }
                }
                else if (strncmp(name, "CONTENT-ID", strlen("CONTENT-ID")) == 0)  {
@@ -1512,9 +1496,17 @@ int emcore_mime_set_part_header_value(struct _m_part_header **header, char *name
 
                        (*header)->content_id = EM_SAFE_STRDUP(value);
                }
-               else if (strncmp(name, "CONTENT-LOCATION", strlen("CONTENT-LOCATION")) == 0)
+               else if (strncmp(name, "CONTENT-LOCATION", strlen("CONTENT-LOCATION")) == 0) {
                        (*header)->content_location = EM_SAFE_STRDUP(value);
+               }
+               else if (strncmp(name, "X-PRIORITY", strlen("X-PRIORITY")) == 0) {
+                       (*header)->priority = EM_SAFE_STRDUP(value);
+               }
+               else if (strncmp(name, "X-MSMAIL-PRIORITY", strlen("X-MSMAIL-PRIORITY")) == 0) {
+                       (*header)->ms_priority = EM_SAFE_STRDUP(value);
+               }
        }
+
        EM_DEBUG_FUNC_END();
        return true;
 }
@@ -1579,8 +1571,8 @@ int emcore_mime_get_param_from_str(char *str, struct _parameter **param, int *er
                int err = EMAIL_ERROR_NONE;
                char *utf8_text = NULL;
 
-               if (!(utf8_text = emcore_decode_rfc2047_text(p_val, &err)))
-                       EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_text failed [%d]", err);
+               if (!(utf8_text = emcore_gmime_get_decoding_text(p_val)))
+                       EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed [%d]", err);
                (*param)->value = utf8_text;
        }
        EM_DEBUG_FUNC_END();
@@ -1590,6 +1582,7 @@ int emcore_mime_get_param_from_str(char *str, struct _parameter **param, int *er
 /*  add a parameter to parameter lis */
 int emcore_mime_add_param_to_list(struct _parameter **param_list, struct _parameter *param, int *err_code)
 {
+       EM_DEBUG_FUNC_BEGIN("param_list[%p], param[%p]", param_list, *param_list);
        struct _parameter **t = param_list;
 
        while (*t && (*t)->next)
@@ -1702,12 +1695,12 @@ INTERNAL_FUNC int emcore_decode_body_text(char *enc_buf, int enc_len, int enc_ty
 
        switch (enc_type)  {
                case ENCQUOTEDPRINTABLE:
-                       EM_DEBUG_LOG("ENCQUOTEDPRINTABLE");
+
                        content = rfc822_qprint((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
                        break;
 
                case ENCBASE64:
-                       EM_DEBUG_LOG("ENCBASE64");
+
                        content = rfc822_base64((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
                        break;
 
@@ -1743,7 +1736,7 @@ INTERNAL_FUNC int emcore_decode_body_text(char *enc_buf, int enc_len, int enc_ty
 /*  mode - SAVE_TYPE_SIZE   :  no saving (only hold content size */
 /*         SAVE_TYPE_BUFFER :  content is saved to buffer (holder is buffer */
 /*         SAVE_TYPE_FILE   :  content is saved to temporary file (holder is file name */
-int emcore_mime_get_content_data(void *stream, int is_file, int is_text, char *boundary_str, char *content_encoding, int *end_of_parsing, int mode, char **holder, int *size, void *callback, int *err_code)
+int emcore_mime_get_content_data(void *stream, int is_file, char *boundary_str, char *content_encoding, int *end_of_parsing, int mode, char **holder, int *size, void *callback, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("stream[%p], is_file[%d], boundary_str[%s], content_encoding[%s], end_of_parsing[%p], mode[%d], holder[%p], size[%p], callback[%p], err_code[%p]", stream, is_file, boundary_str, content_encoding, end_of_parsing, mode, holder, size, callback, err_code);
 
@@ -1797,16 +1790,16 @@ int emcore_mime_get_content_data(void *stream, int is_file, int is_text, char *b
 
                EM_DEBUG_LOG("holder[%s]", *holder);
 
-               fd = open(*holder, O_WRONLY|O_CREAT, 0644);
-               if (fd < 0)  {
-                       EM_DEBUG_EXCEPTION("holder open failed :  holder is a filename that will be saved.");
+               error = em_open(*holder, O_WRONLY|O_CREAT, 0644, &fd);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("holder em_open failed :  holder is a filename that will be saved.");
                        goto FINISH_OFF;
                }
        }
 
        if (is_file) {
                EM_DEBUG_LOG("from file");
-               error = emcore_decode_body_text_from_file((FILE *)stream, boundary_str, encoding, mode, is_text, fd, holder, end_of_parsing, &sz);
+               error = emcore_decode_body_text_from_file((FILE *)stream, boundary_str, encoding, mode, fd, holder, end_of_parsing, &sz);
                if (error != EMAIL_ERROR_NONE) {
                        if (error != EMAIL_ERROR_NO_MORE_DATA) {
                                EM_DEBUG_EXCEPTION("emcore_decode_body_text_from_file failed : [%d]", error);
@@ -1815,7 +1808,7 @@ int emcore_mime_get_content_data(void *stream, int is_file, int is_text, char *b
                }
        } else {
                EM_DEBUG_LOG("from sock");
-               error = emcore_decode_body_text_from_sock(stream, boundary_str, encoding, mode, is_text, fd, holder, end_of_parsing, &sz);
+               error = emcore_decode_body_text_from_sock(stream, boundary_str, encoding, mode, fd, holder, end_of_parsing, &sz);
                if (error != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_decode_body_text_from_sock failed : [%d]", error);
                        goto FINISH_OFF;
@@ -1827,7 +1820,7 @@ FINISH_OFF:
        if (err_code != NULL)
                *err_code = error;
 
-       if(fd>0) close(fd); /*prevent 32736*/
+       EM_SAFE_CLOSE (fd); /*prevent 32736*/
 
        if (ret) {
                if (size)
@@ -1858,12 +1851,13 @@ int emcore_mime_skip_content_data(void *stream,
 
        if (!boundary_str)  {   /*  if no boundary, this content is from current line to end of all multipart */
                while (TRUE)  {
-
+/* Several threads including event thd call it, don't delete comment
                        if (!emcore_check_thread_status())  {
                                if (err_code != NULL)
                                        *err_code = EMAIL_ERROR_CANCELLED;
                                return false;
                        }
+*/
                        if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                        (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))  {
                                *end_of_parsing = 1;
@@ -1882,12 +1876,13 @@ int emcore_mime_skip_content_data(void *stream,
                SNPRINTF(boundary_end, BOUNDARY_LEN, "--%s%s", boundary_str, "--\r\n");
 
                while (TRUE)  {
-
+/* Several threads including event thd call it, don't delete comment
                        if (!emcore_check_thread_status())  {
                                if (err_code != NULL)
                                        *err_code = EMAIL_ERROR_CANCELLED;
                                return false;
                        }
+*/
                        if ((is_file == 0 && !emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, err_code)) ||
                        (is_file == 1 && !emcore_get_line_from_file(stream, buf, MIME_LINE_LEN, err_code)))  {
                                /*  end of fil */
@@ -2059,6 +2054,8 @@ void emcore_mime_free_part_header(struct _m_part_header *header)
        EM_SAFE_FREE (header->disp_type);
        EM_SAFE_FREE (header->content_id);
        EM_SAFE_FREE (header->content_location);
+       EM_SAFE_FREE (header->priority);
+       EM_SAFE_FREE (header->ms_priority);
        if (header->disp_parameter) emcore_mime_free_param(header->disp_parameter);
        EM_SAFE_FREE (header);
        EM_DEBUG_FUNC_END();
@@ -2128,49 +2125,6 @@ void emcore_mime_free_mime(struct _m_mesg *mmsg)
        EM_DEBUG_FUNC_END();
 }
 
-void emcore_free_content_info(struct _m_content_info *cnt_info)
-{
-       EM_DEBUG_FUNC_BEGIN();
-       struct attachment_info *p;
-
-       if (!cnt_info) return ;
-       EM_SAFE_FREE(cnt_info->text.plain);
-       EM_SAFE_FREE(cnt_info->text.plain_charset);
-       EM_SAFE_FREE(cnt_info->text.html);
-       EM_SAFE_FREE(cnt_info->text.html_charset);
-       while (cnt_info->file) {
-               p = cnt_info->file->next;
-               EM_SAFE_FREE(cnt_info->file->name);
-               EM_SAFE_FREE(cnt_info->file->save);
-               EM_SAFE_FREE(cnt_info->file->attachment_mime_type);
-               EM_SAFE_FREE(cnt_info->file->content_id);
-               EM_SAFE_FREE(cnt_info->file); 
-               cnt_info->file = p;
-       }
-
-       EM_DEBUG_FUNC_END();
-}
-
-void emcore_free_attachment_info(struct attachment_info *attchment)
-{
-       EM_DEBUG_FUNC_BEGIN();
-       struct attachment_info *p;
-
-       if (!attchment) return;
-
-       while (attchment) {
-               p = attchment->next;
-               EM_SAFE_FREE(attchment->name);
-               EM_SAFE_FREE(attchment->save);
-               EM_SAFE_FREE(attchment->attachment_mime_type);
-               EM_SAFE_FREE(attchment->content_id);
-               EM_SAFE_FREE(attchment);
-               attchment = p;
-       }
-
-       EM_DEBUG_FUNC_END();
-}
-
 /* get body-part in nested part */
 static PARTLIST *emcore_get_allnested_part_full(MAILSTREAM *stream, int msg_uid, BODY *body, struct _m_content_info *cnt_info, int *err_code, PARTLIST *section_list)
 {
@@ -2258,7 +2212,7 @@ PARTLIST* emcore_get_body_full(MAILSTREAM *stream, int msg_uid, BODY *body, stru
        EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], body[%p], cnt_info[%p], err_code[%p]", stream, msg_uid, body, cnt_info, err_code);
        char content_type_buffer[512] = { 0, };
 
-       if (!stream || !body || !cnt_info)  {
+       if (!stream || !body || !cnt_info) {
                EM_DEBUG_EXCEPTION("stream[%p], msg_uid[%d], body[%p], cnt_info[%p]", stream, msg_uid, body, cnt_info);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
@@ -2266,7 +2220,7 @@ PARTLIST* emcore_get_body_full(MAILSTREAM *stream, int msg_uid, BODY *body, stru
                return NULL;
        }
 
-       switch (body->type)  {
+       switch (body->type) {
                case TYPEMULTIPART:
                        section_list = emcore_get_multi_part_full(stream, msg_uid, body, cnt_info, err_code, section_list);
                        EM_DEBUG_FUNC_END("section_list [%p]", section_list);
@@ -2307,10 +2261,14 @@ PARTLIST* emcore_get_body_full(MAILSTREAM *stream, int msg_uid, BODY *body, stru
                                                return NULL;
                                        }
 
-                                       if ((body->id) || (body->location) || ((body->disposition.type != NULL) && ((body->disposition.type[0] == 'i') || (body->disposition.type[0] == 'I'))))
-                                               ai->type = 1; /* inline contents */
-                                       else
-                                               ai->type = 2; /* attachment */
+                                       if (body->disposition.type != NULL && ((body->disposition.type[0] == 'a') || (body->disposition.type[0] == 'A'))) {
+                                               ai->type = ATTACHMENT; /* attachment */
+                                       } else {
+                                               if ((body->id) || (body->location) || ((body->disposition.type != NULL) && ((body->disposition.type[0] == 'i') || (body->disposition.type[0] == 'I'))))
+                                                       ai->type = INLINE_ATTACHMENT; /* inline contents */
+                                               else
+                                                       ai->type = ATTACHMENT; /* attachment */
+                                       }
 
                                        ai->name                 = EM_SAFE_STRDUP(filename);
                                        ai->size                 = body->size.bytes;
@@ -2343,9 +2301,9 @@ PARTLIST* emcore_get_body_full(MAILSTREAM *stream, int msg_uid, BODY *body, stru
                                        } else {
                                                while(current_ai->next != NULL) {
                                                        if (ai->type == ATTACHMENT) {
-                                                               if (current_ai->type == INLINE_ATTACHMENT) 
+                                                               if (current_ai->type == INLINE_ATTACHMENT)
                                                                        break;
-                                                       } 
+                                                       }
 
                                                        prev_ai = current_ai;
                                                        current_ai = current_ai->next;
@@ -2466,8 +2424,6 @@ INTERNAL_FUNC int emcore_get_body_part_list_full(MAILSTREAM *stream, int msg_uid
 static int emcore_write_response_into_file(char *filename, char *write_mode, char *encoded, int encoding_type, char *subtype, int account_id, int mail_id, int *err)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int temp_decoded_len = 0;
-       int inline_support = 0;
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        int not_found = true;
@@ -2531,9 +2487,9 @@ static int emcore_write_response_into_file(char *filename, char *write_mode, cha
                EM_DEBUG_LOG("Decoded Length [%d] " , decoded_len);
                EM_DEBUG_LOG_SEC("filename [%s] " , filename);
 
-               if (!(fp = fopen(filename, write_mode)))  {
-                       EM_DEBUG_EXCEPTION_SEC("fopen failed - %s", filename);
-                       error = EMAIL_ERROR_SYSTEM_FAILURE; /*prevent 28347*/
+               error = em_fopen(filename, write_mode, &fp);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s", filename);
                        goto FINISH_OFF;
                }
 
@@ -2566,6 +2522,8 @@ static int emcore_write_response_into_file(char *filename, char *write_mode, cha
                                                        else
                                                                memcpy(body_inline_id, body_inline->id , EM_SAFE_STRLEN(body_inline->id));
 
+                                                       /* Handle the 'space__content_id(example : (yahoo : content_id: __abcdefgh__ijkl...) */
+                                                       em_trim_left(body_inline_id);
                                                        EM_DEBUG_LOG_SEC("Inline body_inline_id [%s]  ", body_inline_id);
 
                                                        if ((param || param1) && 0 == strncmp(body_inline_id , decoded_content_id + strlen("cid:"), EM_SAFE_STRLEN(body_inline_id))) {
@@ -2587,7 +2545,6 @@ static int emcore_write_response_into_file(char *filename, char *write_mode, cha
                                                                                decoded = decoded_temp;
                                                                                decoded_len = EM_SAFE_STRLEN(decoded);
                                                                                EM_DEBUG_LOG("Decoded Length [%d] ", decoded_len);
-                                                                               inline_support = 1;
                                                                                not_found = false;
                                                                                /* only_body_download = false */
                                                                                break;
@@ -2608,10 +2565,8 @@ static int emcore_write_response_into_file(char *filename, char *write_mode, cha
                                                /* EM_DEBUG_LOG(">>>> decoded_temp 2 [ %s ] ", decoded_temp) */
                                                EM_SAFE_FREE(decoded);
                                                decoded = decoded_temp;
-                                               temp_decoded_len = EM_SAFE_STRLEN(body_inline_id);
                                                decoded_len = EM_SAFE_STRLEN(decoded);
                                                EM_DEBUG_LOG("Decoded Length [%d] ", decoded_len);
-                                               inline_support = 1;
                                        }
                                }
                        }
@@ -2637,7 +2592,7 @@ static int emcore_write_response_into_file(char *filename, char *write_mode, cha
        ret = true;
 
 FINISH_OFF:
-       
+
        if (err)
                *err = error;
 
@@ -2703,11 +2658,13 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                goto FINISH_OFF;
        }
 
+/* don't delete the comment. several threads including event thread call it
        FINISH_OFF_IF_CANCELED;
+*/
 
-       if (!(fp = fopen(filepath, "wb+"))) {
-               EM_DEBUG_EXCEPTION_SEC("fopen failed - %s", filepath);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(filepath, "wb+", &fp);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("fopen failed - %s : [%d]", filepath, err);
                goto FINISH_OFF;
        }
 
@@ -2738,17 +2695,14 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
 
        while (imaplocal->netstream) {
 
+/* don't delete the comment. several threads including event thread call this func
                if (!emcore_check_thread_status()) {
                        EM_DEBUG_LOG("Canceled...");
-                       /* Is it realy required ? It might cause crashes.
-                       if (imaplocal->netstream)
-                               net_close (imaplocal->netstream);
-                       */
                        imaplocal->netstream = NULL;
                        err = EMAIL_ERROR_CANCELLED;
                        goto FINISH_OFF;
                }
-
+*/
                /* receive response */
                if (!(response = net_getline(imaplocal->netstream))) {
                        EM_DEBUG_EXCEPTION("net_getline failed...");
@@ -2831,13 +2785,14 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                EM_DEBUG_LOG("server_uid [%s]", server_uid);
                                EM_DEBUG_LOG("mail_id [%d]", mail_id);
 
+/* don't delete the comment. several threads including event thread call it
                                if (!emcore_check_thread_status()) {
                                        EM_DEBUG_LOG("Canceled...");
                                        imaplocal->netstream = NULL;
                                        err = EMAIL_ERROR_CANCELLED;
                                        goto FINISH_OFF;
                                }
-
+*/
                                if (is_attachment) {
                                        EM_DEBUG_LOG("Attachment number [%d]", is_attachment);
                                        if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 0))
@@ -2860,10 +2815,6 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                        }
                                }
 
-                               if (_imap4_download_noti_interval_value > DOWNLOAD_NOTI_INTERVAL_SIZE) {
-                                       _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_SIZE;
-                               }
-
                                /* remove new lines */
                                do {
                                        EM_SAFE_FREE(response);
@@ -2878,6 +2829,7 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                                EM_DEBUG_LOG("Skip newline !!");
                                                nskip++;
                                        } else {
+                                               EM_SAFE_FREE (new_response); /* detected by valgrind */
                                                new_response = g_strconcat(response, "\r\n", NULL);
                                                EM_SAFE_FREE(response);
                                                break;
@@ -2887,10 +2839,46 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                preline_len = EM_SAFE_STRLEN(new_response);
                                EM_DEBUG_LOG("preline_len : %d", preline_len);
 
-                               if (body_size - preline_len - nskip*2 <= 0)
+                               if (body_size - preline_len - nskip*2 < 0)
                                        continue;
 
-                               if (body_size - preline_len - nskip*2 < DOWNLOAD_MAX_BUFFER_SIZE) {
+                               if ((body_size - preline_len -nskip*2) == 0) {
+                                       /* 1 line content */
+                                       if (!emcore_write_response_into_file(filepath, "wb+", new_response, encoding, section_subtype, account_id, mail_id, &err)) {
+                                               EM_DEBUG_EXCEPTION("write_response_into_file failed [%d]", err);
+                                               goto FINISH_OFF;
+                                       }
+
+                                       total = preline_len;
+                                       EM_DEBUG_LOG("total = %d", total);
+                                       EM_DEBUG_LOG_SEC("write_response_into_file successful %s.....", filename);
+
+                                       /*  In some situation, total_encoded_len includes the length of dummy bytes. So it might be greater than body_size */
+
+                                       _imap4_received_body_size += total;
+                                       EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : received_body[%d] / total_body[%d] = %d %% Completed.",
+                                                       _imap4_received_body_size, _imap4_total_body_size, 100*_imap4_received_body_size/_imap4_total_body_size);
+
+                                       if (((_imap4_last_notified_body_size + _imap4_download_noti_interval_value) <= _imap4_received_body_size)
+                                                                                                       || (_imap4_received_body_size >= _imap4_total_body_size)) {
+
+                                               _imap4_last_notified_body_size = _imap4_received_body_size;
+
+                                               if (is_attachment) {
+                                                       if (_imap4_total_body_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 100 *total/body_size))
+                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
+                                               } else {
+                                                       if (multi_part_body_size) {
+                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                       } else {
+                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                       }
+                                               }
+                                       }
+                               }
+                               else if (body_size - preline_len - nskip*2 < DOWNLOAD_MAX_BUFFER_SIZE) {
                                        memset(encoded, 0x00, sizeof(encoded));
                                        if (net_getbuffer(imaplocal->netstream, body_size - preline_len - nskip*2, (char *)encoded) <= 0) {
                                                EM_DEBUG_EXCEPTION("net_getbuffer failed...");
@@ -2915,24 +2903,30 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                        /*  In some situation, total_encoded_len includes the length of dummy bytes. So it might be greater than body_size */
 
                                        _imap4_received_body_size += total;
-                                       _imap4_last_notified_body_size = _imap4_received_body_size;
 
                                        EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : received_body[%d] / total_body[%d] = %d %% Completed.",
                                                        _imap4_received_body_size, _imap4_total_body_size, 100*_imap4_received_body_size/_imap4_total_body_size);
 
-                                       if (is_attachment) {
-                                               if (_imap4_total_body_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 100 *total/body_size))
-                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
-                                       } else {
-                                               if (multi_part_body_size) {
-                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
-                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                       if (((_imap4_last_notified_body_size + _imap4_download_noti_interval_value) <= _imap4_received_body_size)
+                                                                                                       || (_imap4_received_body_size >= _imap4_total_body_size)) {
+
+                                               _imap4_last_notified_body_size = _imap4_received_body_size;
+
+                                               if (is_attachment) {
+                                                       if (_imap4_total_body_size && !emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 100 *total/body_size))
+                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
                                                } else {
-                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
-                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                       if (multi_part_body_size) {
+                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                       } else {
+                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                       }
                                                }
                                        }
-                               } else {
+                               }
+                               else {
                                        int remain_body_size = body_size - preline_len - nskip*2;
                                        int x = 0;
                                        int nsize = 0;
@@ -2987,8 +2981,8 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                                                if (total > body_size)
                                                                        _imap4_received_body_size = body_size;
 
-                                                               _imap4_last_notified_body_size = _imap4_received_body_size;
 
+/* don't delete the comment. it can be called by several threads
                                                                if (!emcore_check_thread_status()) {
                                                                        EM_DEBUG_LOG("Canceled...");
                                                                        imaplocal->netstream = NULL;
@@ -2996,19 +2990,25 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
                                                                        goto FINISH_OFF;
                                                                }
 
-                                                               EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : Total[%d] / [%d] = %d %% Completed.\n", total, body_size, 100*total/body_size);
+*/                                                             EM_DEBUG_LOG("DOWNLOADING STATUS NOTIFY : Total[%d] / [%d] = %d %% Completed.\n", total, body_size, 100*total/body_size);
 
-                                                               if (is_attachment) {
-                                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 100*total/body_size))
-                                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
-                                                               } else {
-                                                                       if (multi_part_body_size) {
-                                                                               /* EM_DEBUG_LOG("DOWNLOADING..........  :  Multipart body size is [%d]", multi_part_body_size) */
-                                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
-                                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                               if (((_imap4_last_notified_body_size + _imap4_download_noti_interval_value) <= _imap4_received_body_size)
+                                                                                                       || (_imap4_received_body_size >= _imap4_total_body_size)) {
+
+                                                                       _imap4_last_notified_body_size = _imap4_received_body_size;
+
+                                                                       if (is_attachment) {
+                                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, filename, is_attachment, 100*total/body_size))
+                                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_ATTACH_START] Failed >>>>");
                                                                        } else {
-                                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
-                                                                                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                                               if (multi_part_body_size) {
+                                                                                       /* EM_DEBUG_LOG("DOWNLOADING..........  :  Multipart body size is [%d]", multi_part_body_size) */
+                                                                               if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                                               } else {
+                                                                                       if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, filename, _imap4_total_body_size, _imap4_received_body_size))
+                                                                                               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>>");
+                                                                               }
                                                                        }
                                                                }
                                                        }
@@ -3056,6 +3056,8 @@ static int imap_mail_write_body_to_file(MAILSTREAM *stream, int account_id, int
 FINISH_OFF:
        EM_SAFE_FREE(decoded);
        EM_SAFE_FREE(response);
+       EM_SAFE_FREE (file_id);         /* detected by valgrind */
+       EM_SAFE_FREE (new_response); /* detected by valgrind */
 
        if (fp != NULL)
                fclose(fp);
@@ -3108,39 +3110,37 @@ static int emcore_get_body_part_imap_full(MAILSTREAM *stream, int msg_uid, int a
                goto FINISH_OFF;
        }
 
-       if (section_list != NULL) {
-               PARTLIST *temp = section_list;
+       PARTLIST *temp = section_list;
 
-               if (cnt_info->grab_type == GRAB_TYPE_ATTACHMENT) {
-                       /*  to download attachment */
-                       body = temp->body;
-                       if (body->sparep != NULL)  {
-                               snprintf(sections, sizeof(sections), "BODY.PEEK[%s]", (char *)body->sparep);
-                       } else {
-                               EM_DEBUG_EXCEPTION("body->sparep can not be null. ");
-                               return_value = -1;
-                               goto FINISH_OFF;
-                       }
+       if (cnt_info->grab_type == GRAB_TYPE_ATTACHMENT) {
+               /*  to download attachment */
+               body = temp->body;
+               if (body->sparep != NULL)  {
+                       snprintf(sections, sizeof(sections), "BODY.PEEK[%s]", (char *)body->sparep);
                } else {
-                       while (temp != NULL) {
-                               char t[64] = {0,};
-                               body = temp->body;
-
-                               if ((body->type == TYPETEXT) || (body->id != NULL) || ((body->disposition.type != NULL) && ((body->disposition.type[0] == 'i') || (body->disposition.type[0] == 'I')))) {
-                                       snprintf(t, sizeof(t), "BODY.PEEK[%s] ", (char *)body->sparep);         /* body parts seperated by period */
-                                       if (EM_SAFE_STRLEN(sections) + EM_SAFE_STRLEN(t) < sizeof(sections))
-                                               strcat(sections, t);
-                                       else {
-                                               EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
-                                               break;
-                                       }
+                       EM_DEBUG_EXCEPTION("body->sparep can not be null. ");
+                       return_value = -1;
+                       goto FINISH_OFF;
+               }
+       } else {
+               while (temp != NULL) {
+                       char t[64] = {0,};
+                       body = temp->body;
+
+                       if ((body->type == TYPETEXT) || (body->id != NULL) || ((body->disposition.type != NULL) && ((body->disposition.type[0] == 'i') || (body->disposition.type[0] == 'I')))) {
+                               snprintf(t, sizeof(t), "BODY.PEEK[%s] ", (char *)body->sparep);         /* body parts seperated by period */
+                               if (EM_SAFE_STRLEN(sections) + EM_SAFE_STRLEN(t) < sizeof(sections))
+                                       strcat(sections, t);
+                               else {
+                                       EM_DEBUG_EXCEPTION("Too many body parts. IMAP command may cross 2000bytes.");
+                                       break;
                                }
-                               temp = (PARTLIST *)temp->next;
                        }
+                       temp = (PARTLIST *)temp->next;
                }
        }
 
-       if (sections[EM_SAFE_STRLEN(sections)-1] == ' ') {
+       if (EM_SAFE_STRLEN (sections) > 0 && sections[EM_SAFE_STRLEN(sections)-1] == ' ') { /*prevent 32317*/
                sections[EM_SAFE_STRLEN(sections)-1] = '\0';
        }
 
@@ -3212,7 +3212,7 @@ static int emcore_get_body_part_imap_full(MAILSTREAM *stream, int msg_uid, int a
                                goto FINISH_OFF;
                        }
 
-                       EM_DEBUG_LOG("temp filename :%s", buf);
+                       EM_DEBUG_LOG_SEC("temp filename :%s", buf);
 
                        /*  notifying UI start */
                        /*  parse_file_path_to_filename(buf, &file_id);            */
@@ -3229,6 +3229,7 @@ static int emcore_get_body_part_imap_full(MAILSTREAM *stream, int msg_uid, int a
 
                                while (param) {
                                        if (!strcasecmp(param->attribute, "CHARSET")) {
+                                               EM_SAFE_FREE (cnt_info->text.plain_charset); /* detected by valgrind */
                                                cnt_info->text.plain_charset = EM_SAFE_STRDUP(param->value);
                                                break;
                                        }
@@ -3256,7 +3257,8 @@ static int emcore_get_body_part_imap_full(MAILSTREAM *stream, int msg_uid, int a
                                }
                        }
 
-                       FINISH_OFF_IF_CANCELED;
+                       if (event_handle > 0)
+                               FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
 
                        if (cnt_info->grab_type == GRAB_TYPE_ATTACHMENT) {
                                if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_START, mail_id, buf, cnt_info->file_no, 0))
@@ -3459,12 +3461,12 @@ static int emcore_get_body_part_imap_full(MAILSTREAM *stream, int msg_uid, int a
 
 static int _find_duplicated_inline_content_file(char *input_source_file_name, struct _m_content_info *input_content_info, bool *output_result)
 {
-       EM_DEBUG_FUNC_BEGIN("input_source_file_name [%p], input_content_info [%p], output_result [%p]", input_source_file_name,input_content_info, output_result);
+       EM_DEBUG_FUNC_BEGIN("input_source_file_name [%s], input_content_info [%p], output_result [%p]", input_source_file_name,input_content_info, output_result);
        struct attachment_info *cur_attachment_info = NULL;
        int  err = EMAIL_ERROR_NONE;
        bool result = false;
 
-       if(!input_source_file_name || !input_content_info || !output_result) {
+       if(!input_source_file_name || !input_content_info || !output_result || strlen(input_source_file_name) <= 0) {
                EM_DEBUG_EXCEPTION("Invalid parameter");
                return EMAIL_ERROR_INVALID_PARAM;
        }
@@ -3487,7 +3489,7 @@ static int _find_duplicated_inline_content_file(char *input_source_file_name, st
 
 static int _modify_file_name_string_for_duplicated_inline_content(char *input_source_file_name, struct _m_content_info *input_content_info)
 {
-       EM_DEBUG_FUNC_BEGIN("input_source_file_name [%p], input_content_info [%p]", input_source_file_name,input_content_info);
+       EM_DEBUG_FUNC_BEGIN("input_source_file_name [%s], input_content_info [%p]", input_source_file_name,input_content_info);
        int   err = EMAIL_ERROR_NONE;
        char  temp_file_name[MAX_PATH] = { 0, };
        char *file_name = NULL;
@@ -3594,7 +3596,7 @@ static int emcore_get_file_pointer(BODY *body, bool input_check_duplicated_file_
 
                        if ((len > 1) && (attachment_file_name[len-1] == '>'))
                                attachment_file_name[len - 1] = '\0';
-                       decoded_filename = emcore_decode_rfc2047_text(attachment_file_name, &error);
+                       decoded_filename = emcore_gmime_get_decoding_text(attachment_file_name);
                }
                EM_DEBUG_LOG_SEC("attachment_file_name [%s]", attachment_file_name);
                if (decoded_filename != NULL)
@@ -3840,37 +3842,35 @@ INTERNAL_FUNC int emcore_get_utf8_address(char **dest, ADDRESS *address, int *er
        char *nickname = NULL;
 
        while (address)  {
-               EM_DEBUG_LOG_SEC("address->mailbox[%s], address->host[%s]", address->mailbox, address->host);
-               if (!address->mailbox || !address->host) {
-                       address = address->next;
-                       continue;
-               }
-               EM_DEBUG_LOG_DEV("address->mailbox[%p]", address->personal); 
                if (address->personal)  {
-                       if (!(nickname = emcore_decode_rfc2047_text(address->personal, &err)))  {
-                               EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_text failed - %d", err);
+                       if (!(nickname = emcore_gmime_get_decoding_text(address->personal)))  {
+                               EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed - %d", err);
                                goto FINISH_OFF;
                        }
                        EM_DEBUG_LOG_DEV("nickname[%s]", nickname);
                        if (*nickname != '\0')
-                               utf8_address = g_strdup_printf("\"%s\" <%s@%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? address->host : "");
+                               utf8_address = g_strdup_printf("\"%s\" <%s%s%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? "@": "", address->host ? address->host : "");
                        else
-                               utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
+                               utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
 
                        EM_SAFE_FREE(nickname);
                }
-               else
-                       utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
+               else {
+                       if (address->mailbox || address->host)
+                               utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
+               }
 
                EM_DEBUG_LOG_DEV("utf8_address[%s]", utf8_address);
 
-               if (concatenated != NULL)  {
-                       temp = concatenated;
-                       concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
-                       g_free(temp);
+               if (utf8_address) {
+                       if ((concatenated != NULL))  {
+                               temp = concatenated;
+                               concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
+                               g_free(temp);
+                       }
+                       else
+                               concatenated = g_strdup(utf8_address);
                }
-               else
-                       concatenated = g_strdup(utf8_address);
 
                g_free(utf8_address);
                utf8_address = NULL;
@@ -3967,11 +3967,9 @@ INTERNAL_FUNC int emcore_get_attribute_value_of_body_part(PARAMETER *input_param
                if (!strcasecmp(temp_param->attribute, atribute_name))  {
                        if (temp_param->value) {
                                if (with_rfc2047_text) {
-                                       decoded_value = emcore_decode_rfc2047_text(temp_param->value, &err);
-                                       if (decoded_value)
-                                               result_value = decoded_value;
-                                       else
-                                               result_value = decoded_value;
+                                       decoded_value = emcore_gmime_get_decoding_text(temp_param->value);
+                result_value = EM_SAFE_STRDUP(decoded_value);
+                EM_SAFE_FREE(decoded_value);
                                }
                                else
                                        result_value = temp_param->value;
@@ -4014,7 +4012,7 @@ FINISH_OFF:
 /*
  *download body part of imap mail (body-text and attachment)
  */
-static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, struct _m_content_info *cnt_info, int *err_code)
+static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, char *part_body_type, struct _m_content_info *cnt_info, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], body[%p], cnt_info[%p], err_code[%p]", stream, msg_uid, body, cnt_info, err_code);
 
@@ -4027,6 +4025,7 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
        char filename[MAX_PATH + 1] = { 0, };
        char *decoded_filename = NULL;
        int is_attachment = 0;
+       int attachment_type = 0;
        int o_data_len = 0;
        char charset_value_buffer[512] = { 0, };
        char content_type_buffer[512] = { 0, };
@@ -4034,14 +4033,13 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
        PART *part = NULL;
        int dec_len;
        int i = 0;
-       char *sparep = NULL;
-       unsigned short encode = 0;
        int section_plain = 0;
        int section_html = 0;
        int is_pbd = (account_id == 0 && mail_id == 0) ? true  :  false;
+       int search_result = false;
 /*   } is_pb */
 
-       EM_DEBUG_LOG("Grab Type [ %d ] ", cnt_info->grab_type);
+
 
        /* unknown type */
        if (body->type > TYPEOTHER)  {  /* unknown type */
@@ -4068,17 +4066,17 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                return FAILURE;
                        }
 
-                       if (body->subtype[0] == 'H')    /*  HTM */
-                               cnt_info->text.html = o_data;
+                       if (body->subtype[0] == 'H' || body->subtype[0] == 'h')
+                               cnt_info->text.html = EM_SAFE_STRDUP(o_data);
                        else
-                               cnt_info->text.plain = o_data;
+                               cnt_info->text.plain = EM_SAFE_STRDUP(o_data);
 
                        memset(charset_value_buffer, 0, 512);
 
                        if (emcore_get_attribute_value_of_body_part(body->parameter, "CHARSET", charset_value_buffer, 512, true, &err)) {
                                EM_SAFE_FREE(cnt_info->text.plain_charset);
                                cnt_info->text.plain_charset = EM_SAFE_STRDUP(charset_value_buffer);
-                               EM_DEBUG_LOG_SEC(">>>>> CHARSET [%s] ", filename);
+
                        }
                }
        } /*  is_pbd */
@@ -4095,8 +4093,8 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                        char *extcheck = NULL;
 
                                        if (param1->value) {
-                                               decoded_filename = emcore_decode_rfc2047_text(param1->value, &err);
-                                               strncpy(filename, decoded_filename, MAX_PATH);
+                                               decoded_filename = emcore_gmime_get_decoding_text(param1->value);
+                                               strncpy(filename, decoded_filename, MAX_PATH);
                                                EM_SAFE_FREE(decoded_filename);
                                        }
                                        EM_DEBUG_LOG_SEC(">>>>> FILENAME [%s] ", filename);
@@ -4130,7 +4128,7 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
 
                                /*  attribute is "filename" */
                                if (!strcasecmp(param->attribute, "filename"))  {
-                                       decoded_filename = emcore_decode_rfc2047_text(param->value, &err);
+                                       decoded_filename = emcore_gmime_get_decoding_text(param->value);
                                        strncpy(filename, decoded_filename, MAX_PATH);
                                        EM_SAFE_FREE(decoded_filename);
                                        EM_DEBUG_LOG_SEC(">>>>> FILENAME [%s] ", filename);
@@ -4151,24 +4149,20 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                        if ((len > 1) && (filename[len-1] == '>'))
                                filename[len-1] = '\0';
                }
-               /* is_attachment = 1; */
-               is_attachment = 0;
        }
        else if (body->location) {
                EM_DEBUG_LOG("body->location exist");
-               is_attachment = 1;
-               decoded_filename = emcore_decode_rfc2047_text(body->location, &err);
+               decoded_filename = emcore_gmime_get_decoding_text(body->location);
                strncpy(filename, decoded_filename, MAX_PATH);
                EM_SAFE_FREE(decoded_filename);
                EM_DEBUG_LOG_SEC("body->location [%s]", body->location);
        }
        else if (is_pbd && (strncmp(body->subtype, "RFC822", strlen("RFC822")) == 0) && (cnt_info->grab_type == 0 || (cnt_info->grab_type & GRAB_TYPE_ATTACHMENT))) {
                EM_DEBUG_LOG("Beause subtype is RFC822. This is ttachment");
-               is_attachment = 1;
 
                if (cnt_info->grab_type == 0) {
                        if ((body->nested.msg != NULL) && (body->nested.msg->env != NULL) && (body->nested.msg->env->subject != NULL)) {
-                               decoded_filename = emcore_decode_rfc2047_text(body->nested.msg->env->subject, &err);
+                               decoded_filename = emcore_gmime_get_decoding_text(body->nested.msg->env->subject);
                                strncpy(filename, decoded_filename, MAX_PATH);
                                EM_SAFE_FREE(decoded_filename);
                        }
@@ -4222,24 +4216,17 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                        section_plain = 1;
                                else if (temp_body->subtype[0] == 'H')
                                        section_html = 1;
-
-                               sparep = temp_body->sparep;
-                               encode = temp_body->encoding;
                        }
-
                }
        }
        else if (body->disposition.type) /*  if disposition exists, get filename from disposition parameter */ {        /*  "attachment" or "inline" or etc.. */
                EM_DEBUG_LOG("body->disposition.type exist");
-               is_attachment = 1;
 
                if (emcore_get_attribute_value_of_body_part(body->disposition.parameter, "filename", filename, MAX_PATH, true, &err))
                        EM_DEBUG_LOG_SEC(">>>>> FILENAME [%s] ", filename);
 
                if (!*filename)  {      /*  If the part has no filename, it may be report ms */
-                       if ((body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I') && body->parameter && body->parameter->attribute && strcasecmp(body->parameter->attribute, "NAME"))
-                               is_attachment = 0;
-                       else if (body->parameter) /* Fix for the MMS attachment File name as unknown */ {
+                       if (body->parameter) /* Fix for the MMS attachment File name as unknown */ {
                                char *extcheck = NULL;
 
                                if (emcore_get_attribute_value_of_body_part(body->parameter, "NAME", filename, MAX_PATH, true, &err))
@@ -4265,10 +4252,6 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                        else
                                strncpy(filename, "unknown", MAX_PATH);
                }
-               else {
-                       if ((body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I'))
-                               is_attachment = 0;
-               }
        }
        else {
                char *attr_upper = NULL;
@@ -4279,9 +4262,10 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                EM_DEBUG_LOG_DEV("BODY-PARAM-ATTRIBUTE:%s", param->attribute);
                                EM_DEBUG_LOG_DEV("BODY-PARAM-VALUE:%s", param->value);
 
+                               EM_SAFE_FREE(attr_upper); /* valgrind */
                                attr_upper = g_ascii_strup(param->attribute, -1);
                                if (g_strcmp0(attr_upper, "NAME") == 0) {
-                                       decoded_value = emcore_decode_rfc2047_text(param->value, &err);
+                                       decoded_value = emcore_gmime_get_decoding_text(param->value);
                                        if (decoded_value) strncpy(filename, decoded_value, MAX_PATH);
                                        break;
                                }
@@ -4299,21 +4283,32 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
        /* if (!is_pbd) */ {
                EM_DEBUG_LOG_SEC("filename [%s]", filename);
                if (*filename)  {
-                       decoded_filename = emcore_decode_rfc2047_text(filename, &err);
+                       decoded_filename = emcore_gmime_get_decoding_text(filename);
                        strncpy(filename, decoded_filename, MAX_PATH);
                        EM_SAFE_FREE(decoded_filename);
-                       EM_DEBUG_LOG_SEC("filename [%s]", filename);
+
+               }
+       }
+
+       /* the subtype of content-type is related and the dispostion type is attachment or inline, It is inline attachment */
+       if (part_body_type != NULL) {
+               if ((part_body_type[0] == 'R' || part_body_type[0] == 'r') && body->disposition.type) {
+                       attachment_type = INLINE_ATTACHMENT;
+                       is_attachment = 1;
+               } else if ((part_body_type[0] == 'M' || part_body_type[0] == 'm') && body->disposition.type) {
+               /* the subtype of content-type is mixed and the dispostion type is attachment , It is attachment */
+                       attachment_type = ATTACHMENT;
+                       is_attachment = 1;
                }
        }
-       EM_DEBUG_LOG("is_attachment [%d]", is_attachment);
 
        if (!is_attachment)  {  /*  Text or RFC822 Message */
-               EM_DEBUG_LOG("Multipart is not attachment, body->type = %d", body->type);
-               EM_DEBUG_LOG("grab_type : [%d]", cnt_info->grab_type);
                if (((cnt_info->grab_type & GRAB_TYPE_TEXT) && (body->type == TYPEMESSAGE || body->type == TYPETEXT || body->type == TYPEIMAGE)) ||
                         (cnt_info->grab_type & GRAB_TYPE_ATTACHMENT))  {
-                       if (is_pbd)
+                       if (is_pbd) {
+                                EM_SAFE_FREE(o_data);
                                return SUCCESS;
+                        }
                        else {  /*  fetch body */
                                if (!emcore_get_temp_file_name(&o_data, &err))  {
                                        EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
@@ -4334,11 +4329,11 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
 
                switch (body->type)  {
                        case TYPETEXT:
-                               EM_DEBUG_LOG("TYPETEXT");
+
                                if (body->subtype[0] == 'H')
-                                       cnt_info->text.html = o_data;
+                                       cnt_info->text.html = EM_SAFE_STRDUP(o_data);
                                else  {
-                                       cnt_info->text.plain = o_data;
+                                       cnt_info->text.plain = EM_SAFE_STRDUP(o_data);
                                        memset(charset_value_buffer, 0, 512);
                                        /*check: cnt_info->text.plain_charset is malloc'd twice */
                                        if (emcore_get_attribute_value_of_body_part(body->parameter, "CHARSET", charset_value_buffer, 512, true, &err)) {
@@ -4355,7 +4350,7 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                        /*      if (!is_pbd) */ {
                                        EM_DEBUG_LOG("TYPEIMAGE or TYPEAPPLICATION  :  inline content");
                                        ai = &(cnt_info->file);
-                                       
+
                                        while(*ai != NULL)
                                                ai = &(*ai)->next;
 
@@ -4374,16 +4369,24 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                                return FAILURE;
                                        }
 
-                                       if (((body->id) || (body->location)) && body->type == TYPEIMAGE)
-                                               (*ai)->type = 1;                        /*  inline */
+                                       if (((body->id) || (body->location)) && (body->type == TYPEIMAGE || body->type == TYPEAPPLICATION)) /* Some composer writes a content type of gif as application/octet-stream */
+                                               (*ai)->type = INLINE_ATTACHMENT;                        /*  inline */
                                        else
-                                               (*ai)->type = 2;                        /*  attachment */
+                                               (*ai)->type = ATTACHMENT;                       /*  attachment */
 
                                        (*ai)->name                 = EM_SAFE_STRDUP(filename);
                                        (*ai)->size                 = body->size.bytes;
-                                       (*ai)->save                 = o_data;
+                                       (*ai)->save                 = EM_SAFE_STRDUP(o_data);
                                        (*ai)->content_id           = EM_SAFE_STRDUP(body->id);
 
+                                       if (body->id && (emcore_search_string_from_file(cnt_info->text.html, body->id, NULL, &search_result) == EMAIL_ERROR_NONE && search_result)) {
+                                               (*ai)->type = INLINE_ATTACHMENT;
+                                               EM_DEBUG_LOG("INLINE_ATTACHMENT");
+                                       } else if ((body->disposition.type && g_ascii_strcasecmp(body->disposition.type, "ATTACHMENT") == 0)) {
+                                               (*ai)->type = ATTACHMENT;
+                                               EM_DEBUG_LOG("ATTACHMENT");
+                                       }
+
                                        memset(content_type_buffer, 0, 512);
                                        if ( (err = emcore_get_content_type_from_mail_bodystruct(body, 512, content_type_buffer) ) == EMAIL_ERROR_NONE)
                                                (*ai)->attachment_mime_type = EM_SAFE_STRDUP(content_type_buffer);
@@ -4417,7 +4420,7 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                ai = &cnt_info->file;
                EM_DEBUG_LOG(" ai - %p ", (*ai));
 
-               if ((body->id) || (body->location)) {
+               if (attachment_type == INLINE_ATTACHMENT) {
                        /*  For Inline content append to the end */
                        for (i = 1; *ai; ai = &(*ai)->next)
                                i++;
@@ -4447,16 +4450,11 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                /*  meaningless code */
                dec_len = body->size.bytes;
 
-               if (body->id)
-                       EM_DEBUG_LOG_SEC("BODY ID [ %s ]", body->id);
-               else
-                       EM_DEBUG_LOG("BODY ID IS NULL");
-
                EM_DEBUG_LOG("i : %d, cnt_info->file_no : %d", i, cnt_info->file_no);
 
                if (
                        ((cnt_info->grab_type & GRAB_TYPE_ATTACHMENT) && i == cnt_info->file_no) ||   /*  Is it correct attachment  */
-                       (((body->id) || (body->location)) && (cnt_info->grab_type & GRAB_TYPE_TEXT))  /*  Is it inline contents  */
+                       ((attachment_type == INLINE_ATTACHMENT) && (cnt_info->grab_type & GRAB_TYPE_TEXT))  /*  Is it inline contents  */
                )  {
                        /*  fetch attachment */
                        EM_DEBUG_LOG_SEC("attachment (enc)  :  %s %ld bytes", filename, body->size.bytes);
@@ -4468,7 +4466,7 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                                        goto FINISH_OFF;
                                }
 
-                               if (!is_pbd) {
+                               if (!is_pbd && attachment_type == ATTACHMENT) {
                                        if (!imap_mail_write_body_to_file(stream, account_id, mail_id, cnt_info->file_no, savefile, msg_uid, body->sparep, body->encoding, &dec_len, body->subtype, &err))  {
                                                EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
                                                goto FINISH_OFF;
@@ -4486,11 +4484,6 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                        goto FINISH_OFF;
                }
 
-               if ((body->id) || (body->location))
-                       (*ai)->type = 1;
-               else
-                       (*ai)->type = 2;
-
                if (is_pbd) {
                        if (savefile != NULL) {
                                if (section_plain == 1)
@@ -4503,8 +4496,9 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
                } /*  is_pbd */
                (*ai)->name                 = EM_SAFE_STRDUP(filename);
                (*ai)->size                 = dec_len;
-               (*ai)->save                 = savefile;
+               (*ai)->save                 = EM_SAFE_STRDUP(savefile);
                (*ai)->content_id           = EM_SAFE_STRDUP(body->id);
+               (*ai)->type                 = attachment_type;
 
                memset(content_type_buffer, 0, 512);
                if ( (err = emcore_get_content_type_from_mail_bodystruct(body, 512, content_type_buffer) ) == EMAIL_ERROR_NONE)
@@ -4544,6 +4538,8 @@ static int emcore_get_body_part_imap(MAILSTREAM *stream, int account_id, int mai
 FINISH_OFF:
        if (err_code)
                *err_code = err;
+       EM_SAFE_FREE(savefile);
+        EM_SAFE_FREE(o_data);
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
@@ -4555,7 +4551,7 @@ static int emcore_get_allnested_part(MAILSTREAM *stream, int account_id, int mai
        PART *part_child = body->nested.part;
 
        while (part_child)  {
-               if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, cnt_info, err_code) < 0)
+               if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, body->subtype, cnt_info, err_code) < 0)
                        return FAILURE;
 
                part_child = part_child->next;
@@ -4574,7 +4570,7 @@ static int emcore_get_alternative_multi_part(MAILSTREAM *stream, int account_id,
 
        /* find the best sub part we can show */
        while (part_child)  {
-               if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, cnt_info, err_code) < 0)
+               if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, body->subtype, cnt_info, err_code) < 0)
                        return FAILURE;
 
                part_child = part_child->next;
@@ -4592,7 +4588,7 @@ static int emcore_get_signed_multi_part(MAILSTREAM *stream, int account_id, int
 
         /* find the best sub part we can show */
         while (part_child)  {
-                if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, cnt_info, err_code) < 0)
+                if (emcore_get_body(stream, account_id, mail_id, msg_uid, &part_child->body, body->subtype, cnt_info, err_code) < 0)
                        return FAILURE;
 
                 part_child = part_child->next;
@@ -4644,7 +4640,7 @@ static int emcore_get_multi_part(MAILSTREAM *stream, int account_id, int mail_id
 
 /*  get body data by body structure */
 /*  if POP3, ignored */
-INTERNAL_FUNC int emcore_get_body(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, struct _m_content_info *cnt_info, int *err_code)
+INTERNAL_FUNC int emcore_get_body(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, char *part_body_type, struct _m_content_info *cnt_info, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], body[%p], cnt_info[%p], err_code[%p]", stream, msg_uid, body, cnt_info, err_code);
 
@@ -4656,7 +4652,7 @@ INTERNAL_FUNC int emcore_get_body(MAILSTREAM *stream, int account_id, int mail_i
                return FAILURE;
        }
 
-       EM_DEBUG_LOG("body->type [%d]", body->type);
+
 
        switch (body->type)  {
                case TYPEMULTIPART:
@@ -4664,7 +4660,7 @@ INTERNAL_FUNC int emcore_get_body(MAILSTREAM *stream, int account_id, int mail_i
 
                case TYPEMESSAGE:  /*  not support */
                        if (strcasecmp(body->subtype, "RFC822") == 0)
-                               return emcore_get_body_part_imap(stream, account_id, mail_id, msg_uid, body, cnt_info, err_code);
+                               return emcore_get_body_part_imap(stream, account_id, mail_id, msg_uid, body, part_body_type, cnt_info, err_code);
                        break;
 
                case TYPETEXT:
@@ -4675,7 +4671,7 @@ INTERNAL_FUNC int emcore_get_body(MAILSTREAM *stream, int account_id, int mail_i
                case TYPEMODEL:
                case TYPEOTHER:
                        /*  exactly, get a pure body part (text and attachment */
-                       return emcore_get_body_part_imap(stream, account_id, mail_id, msg_uid, body, cnt_info, err_code);
+                       return emcore_get_body_part_imap(stream, account_id, mail_id, msg_uid, body, part_body_type, cnt_info, err_code);
 
                default:
                        break;
@@ -4829,13 +4825,11 @@ static void parse_file_path_to_filename(char *src_string, char **out_string)
        char *filepath = NULL;
        char *str = NULL;
        char *prev1 = NULL;
-       char *prev2 = NULL;
 
        filepath = EM_SAFE_STRDUP(src_string);
        token = strtok_r(filepath, "/", &str);
 
        do {
-               prev2 = prev1;
                prev1 = token;
        } while ((token = strtok_r(NULL , "/", &str)));
 
@@ -4843,323 +4837,110 @@ static void parse_file_path_to_filename(char *src_string, char **out_string)
        EM_SAFE_FREE(filepath);
 }
 
-static char *emcore_decode_rfc2047_word(char *encoded_word, int *err_code)
+static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, struct _m_mesg *mmsg)
 {
-       EM_DEBUG_FUNC_BEGIN("encoded_word[%p], err_code[%p]", encoded_word, err_code);
+        EM_DEBUG_FUNC_BEGIN();
+        int err = EMAIL_ERROR_NONE;
 
-       int err = EMAIL_ERROR_NONE;
-       int base64_encoded = false, length = 0;
-       SIZEDTEXT src = { NULL, 0 };
-       SIZEDTEXT dst = { NULL, 0 };
-       gchar *charset = NULL, *encoded_text = NULL;
-       char *decoded_text = NULL, *decoded_word = NULL;
-       char *current = NULL, *start = NULL, *end = NULL;
-       char *buffer = (char*) em_malloc(EM_SAFE_STRLEN(encoded_word) * 2 + 1);
-
-       if (buffer == NULL) {
-               EM_DEBUG_EXCEPTION("Memory allocation fail");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
+        if (!mmsg || !dst_mail_data) {
+                EM_DEBUG_EXCEPTION("Invalid parameter");
+                err = EMAIL_ERROR_INVALID_PARAM;
+                return err;
+        }
 
-       strcpy(buffer, "");
+       char *encoded_subject = NULL;
+       char *first_address = NULL;
+       char *first_alias = NULL;
+       struct tm temp_time_info;
+        ADDRESS *from = NULL;
+        ADDRESS *to = NULL;
+        ADDRESS *cc = NULL;
+        ADDRESS *bcc = NULL;
+       ADDRESS *return_path = NULL;
+       ADDRESS *reply_to = NULL;
+       MESSAGECACHE mail_cache_element = {0, };
 
-       /*  encoding format : =?charset?encoding?encoded-text ?=  */
-       /*  charset : UTF-8, EUC-KR, ... */
-       /*  encoding : b/B (BASE64), q/Q (QUOTED-PRINTABLE) */
-       current = encoded_word;
+       memset(&mail_cache_element, 0x00, sizeof(MESSAGECACHE));
+       memset((void *)&temp_time_info, 0, sizeof(struct tm));
 
-       while (*current != NULL_CHAR) {
-               /* search next */
-               start = strstr(current, "=?");          /*  start of encoding */
-               end = strstr(current, "?=");            /*  end of encoding */
+       /* Set the smime_type */
+        if (mmsg->rfc822header && mmsg->rfc822header->content_type) {
+                if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-mime")) {
+                       if (strcasestr(mmsg->rfc822header->content_type, "enveloped-data"))
+                               dst_mail_data->smime_type = EMAIL_SMIME_ENCRYPTED;
+                       else
+                               dst_mail_data->smime_type = EMAIL_SMIME_SIGNED_AND_ENCRYPTED;
+               } else if (strcasestr(mmsg->rfc822header->content_type, "encrypted")) {
+                        dst_mail_data->smime_type = EMAIL_PGP_ENCRYPTED;
+               } else if (strcasestr(mmsg->rfc822header->content_type, "signed")) {
+                       if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-signature"))
+                               dst_mail_data->smime_type = EMAIL_SMIME_SIGNED;
+                       else
+                               dst_mail_data->smime_type = EMAIL_PGP_SIGNED;
 
-#ifdef FEATURE_CORE_DEBUG
-               EM_DEBUG_LOG("current[%p][%s], start[%p][%s], end[%p][%s]", current, current, start, start, end, end);
-#endif
-               if (start != NULL) {
-                       if (current != start) { /*  copy the string between current and start to buffer */
-                               strncat(buffer, current, start - current);
-                               current = start;
-#ifdef FEATURE_CORE_DEBUG
-                               EM_DEBUG_LOG("1 - Buffer[%s]", buffer);
-#endif
-                       }
-
-                       if (end) {      /*  decode text between start and end */
-                               char *p = strstr(start, "?b?");
-
-                               if (p || (p = strstr(start, "?B?")))            /*  BASE64 */
-                                       base64_encoded = true;
-                               else {
-                                       p = strstr(start, "?q?");
-
-                                       if (p || (p = strstr(start, "?Q?")))            /*  QUOTED-PRINTABLE */
-                                               base64_encoded = false;
-                                       else {
-                                               EM_DEBUG_EXCEPTION("unknown encoding found...");
-
-                                               err = EMAIL_ERROR_UNKNOWN;
-                                               goto FINISH_OFF;
-                                       }
-                               }
-
-                               if (base64_encoded) {   /*  BASE64 */
-                                       charset = g_strndup(start + 2, p - (start + 2));
-                                       encoded_text = g_strndup(p + 3, end - (p + 3));
-                               }
-                               else {  /*  QUOTED-PRINTABLE */
-                                       charset = g_strndup(start + 2, p - (start + 2));
-                                       if (*(p+3) == '=') {    /*  encoded text might start with '='. ex) '?Q?=E0' */
-                                               end = strstr(p+3, "?=");                /*  find new end flag */
-                                               if (end) {
-                                                       encoded_text = g_strndup(p + 3, end - (p + 3));
-                                               }
-                                               else {  /*  end flag is not found */
-                                                       EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_word decoding error : '?=' is not found...");
-
-                                                       err = EMAIL_ERROR_UNKNOWN;
-                                                       goto FINISH_OFF;
-                                               }
-                                       }
-                                       else {
-                                               encoded_text = g_strndup(p + 3, end - (p + 3));
-                                       }
-                               }
-
-#ifdef FEATURE_CORE_DEBUG
-                               EM_DEBUG_LOG("\t >>>>>>>>>>>>>>> CHARSET[%s]", charset);
-                               EM_DEBUG_LOG("\t >>>>>>>>>>>>>>> ENCODED_TEXT[%s]", encoded_text);
-#endif
-
-                               unsigned long len = 0;
-                               if (encoded_text != NULL) {
-                               if (base64_encoded == true) {
-                                       if (!(decoded_text = (char *)rfc822_base64((unsigned char *)encoded_text, EM_SAFE_STRLEN(encoded_text), &len))) {
-                                               EM_DEBUG_EXCEPTION("rfc822_base64 falied...");
-                                               goto FINISH_OFF;
-                                       }
-                               }
-                               else  {
-                                       g_strdelimit(encoded_text, "_", ' ');
-
-                                       if (!(decoded_text = (char *)rfc822_qprint((unsigned char *)encoded_text, EM_SAFE_STRLEN(encoded_text), &len))) {
-                                               EM_DEBUG_EXCEPTION("rfc822_base64 falied...");
-                                               goto FINISH_OFF;
-                                       }
-                               }
-
-                               src.data = (unsigned char *)decoded_text;
-                               src.size = EM_SAFE_STRLEN(decoded_text);
-
-                               if (!utf8_text(&src, charset, &dst, 0))  {
-                                       EM_DEBUG_EXCEPTION("utf8_text falied...");
-                                       if (src.data)
-                                               strncat(buffer, (char *)src.data, src.size); /* Eventhough failed to decode, downloading should go on. Kyuho Jo */
-                               } else {
-                                       if (dst.data)
-                                               strncat(buffer, (char *)dst.data, dst.size);
-                               }
-#ifdef FEATURE_CORE_DEBUG
-                               EM_DEBUG_LOG("2 - Buffer[%s]", buffer);
-#endif
-
-                               /*  free all of the temp variables */
-                               if (dst.data != NULL && dst.data != src.data)
-                                       EM_SAFE_FREE(dst.data);
-
-                               EM_SAFE_FREE(decoded_text);
-
-                                       g_free(encoded_text);
-                                       encoded_text = NULL;
-                               }
-                               if (charset != NULL) {
-                                       g_free(charset);
-                                       charset = NULL;
-                               }
-
-                               if( *(end + 2) == ' ')
-                                       current = end + 3;      /*  skip '?= ' */
-                               else
-                                       current = end + 2;      /*  skip '?=' */
-                       }
-                       else {
-                               /*  unencoded text      */
-                               length = EM_SAFE_STRLEN(start);
-                               strncat(buffer, start, length);
-                               current = start + length;
-#ifdef FEATURE_CORE_DEBUG
-                               EM_DEBUG_LOG("3 - Buffer[%s]", buffer);
-#endif
-                       }
+                       dst_mail_data->digest_type = emcore_get_digest_type(mmsg->rfc822header->content_type);
+               } else {
+                       dst_mail_data->smime_type = EMAIL_SMIME_NONE;
                }
-               else {
-                       /*  unencoded text      */
-                       char *tmp_buffer = NULL;
-                       char *charset_guess = emcore_guess_charset(current);
-                       length = EM_SAFE_STRLEN(current);
-
-                       if (charset_guess) {
-                               SIZEDTEXT extra_src = { NULL, 0 };
-                               SIZEDTEXT extra_dst = { NULL, 0 };
-                               extra_src.data = (unsigned char *)current;
-                               extra_src.size = length;
-                               if (!utf8_text(&extra_src, charset_guess, &extra_dst, 0)) {
-                                       EM_DEBUG_EXCEPTION("utf8_text falied...");
-                                       if (extra_src.data) {
-                                               tmp_buffer = buffer;
-                                               buffer = g_strconcat(tmp_buffer, (char *)extra_src.data, NULL);
-                                               EM_SAFE_FREE(tmp_buffer);
-                                       }
-                               } else {
-                                       if (extra_dst.data) {
-                                               tmp_buffer = buffer;
-                                               buffer = g_strconcat(tmp_buffer, (char *)extra_dst.data, NULL);
-                                               EM_SAFE_FREE(tmp_buffer);
-                                       }
-                               }
+        }
 
-                               EM_SAFE_FREE(charset_guess);
-                       } else {
-                               tmp_buffer = buffer;
-                               buffer = g_strconcat(tmp_buffer, current, NULL);
-                               EM_SAFE_FREE(tmp_buffer);
+       /* Set the priority */
+       if (mmsg->rfc822header) {
+               if (mmsg->rfc822header->priority) {
+                       switch(atoi(mmsg->rfc822header->priority)) {
+                       case 1:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
+                               break;
+                       case 5:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
+                               break;
+                       case 3:
+                       default:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
+                               break;
                        }
-
-                       current = current + length;
-#ifdef FEATURE_CORE_DEBUG
-                       EM_DEBUG_LOG("4 - Buffer[%s]", buffer);
-#endif
+               } else if (mmsg->rfc822header->ms_priority) {
+                       if (strcasestr(mmsg->rfc822header->ms_priority, "HIGH"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
+                       else if (strcasestr(mmsg->rfc822header->ms_priority, "NORMAL"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
+                       else if (strcasestr(mmsg->rfc822header->ms_priority, "LOW"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
+                       else
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
                }
-       }
-
-       decoded_word = EM_SAFE_STRDUP(buffer);
-
-#ifdef FEATURE_CORE_DEBUG
-       EM_DEBUG_LOG(">>>>>>>>>>>>>>> DECODED_WORD[%s]", decoded_word);
-#endif
-
-FINISH_OFF:
-       if (dst.data != NULL && dst.data != src.data)
-               EM_SAFE_FREE(dst.data);
-       EM_SAFE_FREE(decoded_text);
-       EM_SAFE_FREE(buffer);
-
-       if (encoded_text != NULL)
-               g_free(encoded_text);
-       if (charset != NULL)
-               g_free(charset);
-
-       if (err_code != NULL)
-               *err_code = err;
-
-       EM_DEBUG_FUNC_END();
-       return decoded_word;
-}
-
-INTERNAL_FUNC char *emcore_decode_rfc2047_text(char *rfc2047_text, int *err_code)
-{
-       EM_DEBUG_FUNC_BEGIN("rfc2047_text[%s], err_code[%p]", rfc2047_text, err_code);
-
-       int ret = false;
-       int err = EMAIL_ERROR_NONE;
-
-       if (!rfc2047_text)  {
-               EM_DEBUG_EXCEPTION("rfc2047_text[%p]", rfc2047_text);
-               if (err_code != NULL)
-                       *err_code = EMAIL_ERROR_INVALID_PARAM;
-               return NULL;
-       }
-
-       char *text = NULL;
-
-       gchar **encoded_words = g_strsplit_set(rfc2047_text, "\t\r\n", -1);
-       gchar **decoded_words = g_new0(char *, g_strv_length(encoded_words) + 1);
-
-       EM_DEBUG_LOG_DEV("rfc2047_text [%s]", rfc2047_text);
-       EM_DEBUG_LOG_DEV("g_strv_length(encoded_words) [%d]", g_strv_length(encoded_words));
-
-       if (encoded_words != NULL)  {
-               int i = 0;
-
-               while (encoded_words[i] != NULL)  {
-                       EM_DEBUG_LOG_DEV("encoded_words[%d] [%s]", i, encoded_words[i]);
-                       if (!(decoded_words[i] = emcore_decode_rfc2047_word(encoded_words[i], &err)))  {
-                               EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_word falied [%d]", err);
-                               goto FINISH_OFF;
+       } else if (mmsg->nested.body) {
+               if (mmsg->nested.body->part_header->priority) {
+                       switch(atoi(mmsg->nested.body->part_header->priority)) {
+                       case 1:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
+                               break;
+                       case 5:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
+                               break;
+                       case 3:
+                       default:
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
+                               break;
                        }
-                       EM_DEBUG_LOG_DEV("decoded_words[%d] [%s]", i, decoded_words[i]);
-                       i++;
-               }
-
-               text = g_strjoinv("", decoded_words);
-       }
-       else
-               text = EM_SAFE_STRDUP(rfc2047_text);
-
-#ifdef FEATURE_CORE_DEBUG
-       EM_DEBUG_LOG(">>>>>>>>>>>>>>>>> TEXT[%s]", text);
-#endif /*  FEATURE_CORE_DEBUG */
-
-       ret = true;
-
-FINISH_OFF:
-       g_strfreev(decoded_words);
-       g_strfreev(encoded_words);
-
-       if (err_code != NULL)
-               *err_code = err;
-       EM_DEBUG_FUNC_END();
-       return text;
-}
-
-static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, struct _m_mesg *mmsg)
-{
-        EM_DEBUG_FUNC_BEGIN();
-        int err = EMAIL_ERROR_NONE;
-
-        if (!mmsg || !dst_mail_data) {
-                EM_DEBUG_EXCEPTION("Invalid parameter");
-                err = EMAIL_ERROR_INVALID_PARAM;
-                return err;
-        }
-       
-       char *content_type = NULL;
-       char *mime_type = NULL;
-       char *encoded_subject = NULL;
-       struct tm temp_time_info;
-        ADDRESS *from = NULL;
-        ADDRESS *to = NULL;
-        ADDRESS *cc = NULL;
-        ADDRESS *bcc = NULL;
-       MESSAGECACHE mail_cache_element = {0, };
-
-       memset(&mail_cache_element, 0x00, sizeof(MESSAGECACHE));
-       memset((void *)&temp_time_info, 0, sizeof(struct tm));
-
-       if (mmsg->header)
-               content_type = emcore_mime_get_header_value(mmsg->header->part_header, CONTENT_TYPE, &err);
-
-        if (content_type) {
-                if (strcasestr(content_type, "PKCS7-MIME")) {
-                       mime_type = emcore_mime_get_header_value(mmsg->header->part_header, CONTENT_SMIME_TYPE, &err);
-
-                       if (mime_type && strcasestr(mime_type, "enveloped-data")) 
-                               dst_mail_data->smime_type = EMAIL_SMIME_ENCRYPTED;
+               } else if (mmsg->nested.body->part_header->ms_priority) {
+                       if (strcasestr(mmsg->nested.body->part_header->ms_priority, "HIGH"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
+                       else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "NORMAL"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
+                       else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "LOW"))
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
                        else
-                               dst_mail_data->smime_type = EMAIL_SMIME_SIGNED_AND_ENCRYPTED;
-               } else if (strcasestr(content_type, "SIGNED")) {
-                        dst_mail_data->smime_type = EMAIL_SMIME_SIGNED;
-               } else {
-                        dst_mail_data->smime_type = EMAIL_SMIME_NONE;
+                               dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
                }
-        }
+       }
 
        if (!mmsg->rfc822header) {
                 EM_DEBUG_LOG("This mail did not have envelop");
                 return err;
         }
 
+       /* Set the date */
         if (mmsg->rfc822header->date) {
                 EM_DEBUG_LOG("date : [%s]", mmsg->rfc822header->date);
                 mail_parse_date(&mail_cache_element, (unsigned char *)mmsg->rfc822header->date);
@@ -5180,43 +4961,65 @@ static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, s
 
         dst_mail_data->date_time                   = timegm(&temp_time_info);
 
-        encoded_subject = emcore_decode_rfc2047_text(mmsg->rfc822header->subject, NULL);
+
+       /* Set the subject */
+        encoded_subject = emcore_gmime_get_decoding_text(mmsg->rfc822header->subject);
         dst_mail_data->subject                     = EM_SAFE_STRDUP(encoded_subject);
 
-        dst_mail_data->full_address_return         = EM_SAFE_STRDUP(mmsg->rfc822header->return_path);
+       /* Set the email address(from, to, cc, bcc, received ...) */
         dst_mail_data->email_address_recipient     = EM_SAFE_STRDUP(mmsg->rfc822header->received);
-        dst_mail_data->email_address_sender        = EM_SAFE_STRDUP(mmsg->rfc822header->sender);
-        dst_mail_data->full_address_reply          = EM_SAFE_STRDUP(mmsg->rfc822header->reply_to);
 
         if (mmsg->rfc822header->from) {
                 rfc822_parse_adrlist(&from, mmsg->rfc822header->from, NULL);
                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_from, from, &err)) {
                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->from);
-                }    
-        }                    
+                }
+        }
 
         if (mmsg->rfc822header->to) {
                 rfc822_parse_adrlist(&to, mmsg->rfc822header->to, NULL);
                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_to, to, &err)) {
                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->to);
-                }    
-        }                    
+                }
+        }
 
         if (mmsg->rfc822header->cc) {
                 rfc822_parse_adrlist(&cc, mmsg->rfc822header->cc, NULL);
                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_cc, cc, &err)) {
                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->cc);
-                }    
-        }                    
+                }
+        }
 
         if (mmsg->rfc822header->bcc) {
                 rfc822_parse_adrlist(&bcc, mmsg->rfc822header->bcc, NULL);
                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_bcc, bcc, &err)) {
                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->bcc);
-                }    
-        }     
+                }
+        }
+
+        if (mmsg->rfc822header->return_path) {
+                rfc822_parse_adrlist(&return_path, mmsg->rfc822header->return_path, NULL);
+                if (!emcore_get_utf8_address(&dst_mail_data->full_address_return, return_path, &err)) {
+                        EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->return_path);
+                }
+        }
+
+        if (mmsg->rfc822header->reply_to) {
+                rfc822_parse_adrlist(&reply_to, mmsg->rfc822header->reply_to, NULL);
+                if (!emcore_get_utf8_address(&dst_mail_data->full_address_reply, reply_to, &err)) {
+                        EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->reply_to);
+                }
+        }
+
+       if (emcore_get_first_address(dst_mail_data->full_address_from, &first_alias, &first_address) == true) {
+               dst_mail_data->alias_sender = EM_SAFE_STRDUP(first_alias);
+               dst_mail_data->email_address_sender = EM_SAFE_STRDUP(first_address);
+       }
+
 
        EM_SAFE_FREE(encoded_subject);
+       EM_SAFE_FREE(first_alias);
+       EM_SAFE_FREE(first_address);
 
        if (from)
                mail_free_address(&from);
@@ -5230,6 +5033,12 @@ static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, s
        if (bcc)
                mail_free_address(&bcc);
 
+       if (return_path)
+               mail_free_address(&return_path);
+
+       if (reply_to)
+               mail_free_address(&reply_to);
+
         EM_DEBUG_FUNC_END("err : [%d]", err);
         return err;
 }
@@ -5240,13 +5049,16 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
        int i = 0;
        int ret = false;
         int eml_mail_id = 0;
-       int attachment_num = 0;
        int err = EMAIL_ERROR_NONE;
+       int save_status = EMAIL_BODY_DOWNLOAD_STATUS_NONE;
+       int attachment_num = 0;
        int local_attachment_count = 0;
        int local_inline_content_count = 0;
-       char buf[512];
+       char move_buf[512];
+       char path_buf[512];
        char html_body[MAX_PATH] = {0, };
-        struct timeval tv;
+    char *multi_user_name = NULL;
+    struct timeval tv;
        struct attachment_info *ai = NULL;
        email_attachment_data_t *attachment = NULL;
        email_mail_data_t *p_mail_data = NULL;
@@ -5271,7 +5083,7 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
 
        p_mail_data->mail_id = eml_mail_id;
        p_mail_data->account_id = EML_FOLDER;
-       p_mail_data->body_download_status = (*err_code != EMAIL_ERROR_NO_MORE_DATA) ? EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED : EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
+       p_mail_data->mail_size = cnt_info->total_body_size;
 
         if ((err = emcore_make_mail_data_from_m_mesg(p_mail_data, mmsg)) != EMAIL_ERROR_NONE) {
                 EM_DEBUG_EXCEPTION("emcore_make_mail_data_from_m_mesg failed : [%d]", err);
@@ -5280,27 +5092,34 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
        EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
 
        if (cnt_info->text.plain) {
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
                EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
-               if (!emstorage_create_dir(EML_FOLDER, eml_mail_id, 0, &err)) {
+               if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_save_name(EML_FOLDER, eml_mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : UNKNOWN_CHARSET_PLAIN_TEXT_FILE, buf, &err)) {
+               if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : UNKNOWN_CHARSET_PLAIN_TEXT_FILE, move_buf, path_buf, sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err)) {
+               if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               p_mail_data->file_path_plain = EM_SAFE_STRDUP(buf);
+               p_mail_data->file_path_plain = EM_SAFE_STRDUP(path_buf);
+               save_status = cnt_info->text.plain_save_status;
        }
 
        if (cnt_info->text.html) {
-               if (!emstorage_create_dir(EML_FOLDER, eml_mail_id, 0, &err))  {
+        memset(move_buf, 0x00, sizeof(move_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+               if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -5311,21 +5130,34 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
                        strcpy(html_body, UNKNOWN_CHARSET_HTML_TEXT_FILE);
                }
 
-               if (!emstorage_get_save_name(EML_FOLDER, eml_mail_id, 0, html_body, buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0, html_body, move_buf, path_buf, sizeof(path_buf), &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_move_file(cnt_info->text.html, buf, false, &err))  {
+               if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               p_mail_data->file_path_html = EM_SAFE_STRDUP(buf);
+               p_mail_data->file_path_html = EM_SAFE_STRDUP(path_buf);
+               save_status = cnt_info->text.html_save_status;
        }
 
-       for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++) {}
-       EM_DEBUG_LOG("attachment_num : [%d]", attachment_num);
+       if ((err = emcore_get_preview_text_from_file(multi_user_name, 
+                                                p_mail_data->file_path_plain, 
+                                                p_mail_data->file_path_html, 
+                                                MAX_PREVIEW_TEXT_LENGTH, 
+                                                &p_mail_data->preview_text)) != EMAIL_ERROR_NONE)
+               EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
+
+       for (ai = cnt_info->file; ai; ai = ai->next, local_attachment_count++) {}
+       EM_DEBUG_LOG("local_attachment_count : [%d]", local_attachment_count);
+
+       for (ai = cnt_info->inline_file; ai; ai = ai->next, local_inline_content_count++) {}
+       EM_DEBUG_LOG("local_inline_content_count : [%d]", local_inline_content_count);
+
+       attachment_num = local_attachment_count + local_inline_content_count;
 
        if (attachment_num > 0) {
                attachment = (email_attachment_data_t *)em_malloc(sizeof(email_attachment_data_t) * attachment_num);
@@ -5339,9 +5171,10 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
                        attachment[i].attachment_id          = i + 1;
                        attachment[i].attachment_size        = ai->size;
                        attachment[i].attachment_name        = EM_SAFE_STRDUP(ai->name);
+                       attachment[i].content_id             = EM_SAFE_STRDUP(ai->content_id);
                        attachment[i].drm_status             = ai->drm;
-                       attachment[i].save_status            = 0;
-                       attachment[i].inline_content_status  = ai->type == 1;
+                       attachment[i].save_status            = ai->save_status;
+                       attachment[i].inline_content_status  = 0;
                        attachment[i].attachment_mime_type   = EM_SAFE_STRDUP(ai->attachment_mime_type);
 #ifdef __ATTACHMENT_OPTI__
                        attachment[i].encoding               = ai->encoding;
@@ -5354,46 +5187,89 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
                        EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
                         EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
 
-                       if (ai->type == 1)
-                               local_inline_content_count ++;
-                       else
-                               local_attachment_count++;
-
                        if (ai->save) {
-             attachment[i].save_status = ai->save_status == EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED ? EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED : EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
+               memset(move_buf, 0x00, sizeof(move_buf));
+            memset(path_buf, 0x00, sizeof(path_buf));
 
-                               if (ai->type == 1)  {           /*  it is inline content */
-                                       if (!emstorage_create_dir(EML_FOLDER, eml_mail_id, 0, &err))  {
-                                               EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-                                               goto FINISH_OFF;
-                                       }
-                                       if (!emstorage_get_save_name(EML_FOLDER, eml_mail_id, 0, attachment[i].attachment_name, buf, &err))  {
-                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                                               goto FINISH_OFF;
-                                       }
+                       if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, i + 1, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                                       goto FINISH_OFF;
                                }
-                               else  {
-                                       if (!emstorage_create_dir(EML_FOLDER, eml_mail_id, i + 1, &err)) {
-                                               EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-                                               goto FINISH_OFF;
+
+                               if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, i + 1, attachment[i].attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (!emstorage_move_file(ai->save, move_buf, false, &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+
+                                       /*  delete all created files. */
+                                       if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
+                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
+                                               /* goto FINISH_OFF; */
                                        }
 
-                                       if (!emstorage_get_save_name(EML_FOLDER, eml_mail_id, i + 1, attachment[i].attachment_name, buf, &err))  {
-                                               EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                                               goto FINISH_OFF;
+                                       if (!emstorage_delete_dir(move_buf, NULL)) {
+                                               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
+                                               /* goto FINISH_OFF; */
                                        }
+
+
+                                       goto FINISH_OFF;
+                               }
+
+                               attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
+                       }
+
+                       EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
+                       save_status = ai->save_status;
+               }
+
+               for (ai = cnt_info->inline_file; ai; ai = ai->next, i++) {
+                       attachment[i].attachment_id          = i + 1;
+                       attachment[i].attachment_size        = ai->size;
+                       attachment[i].attachment_name        = EM_SAFE_STRDUP(ai->name);
+                       attachment[i].content_id             = EM_SAFE_STRDUP(ai->content_id);
+                       attachment[i].drm_status             = ai->drm;
+                       attachment[i].save_status            = ai->save_status;
+                       attachment[i].inline_content_status  = INLINE_ATTACHMENT;
+                       attachment[i].attachment_mime_type   = EM_SAFE_STRDUP(ai->attachment_mime_type);
+#ifdef __ATTACHMENT_OPTI__
+                       attachment[i].encoding               = ai->encoding;
+                       attachment[i].section                = ai->section;
+#endif
+                       EM_DEBUG_LOG("attachment[%d].attachment_id[%d]", i, attachment[i].attachment_id);
+                       EM_DEBUG_LOG("attachment[%d].attachment_size[%d]", i, attachment[i].attachment_size);
+                       EM_DEBUG_LOG_SEC("attachment[%d].attachment_name[%s]", i, attachment[i].attachment_name);
+                       EM_DEBUG_LOG("attachment[%d].drm_status[%d]", i, attachment[i].drm_status);
+                       EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
+                        EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
+
+                       if (ai->save) {
+                memset(move_buf, 0x00, sizeof(move_buf));
+                memset(path_buf, 0x00, sizeof(path_buf));
+
+                               if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
+                                       goto FINISH_OFF;
                                }
 
-                               if (!emstorage_move_file(ai->save, buf, false, &err))  {
+                               if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0, attachment[i].attachment_name, move_buf, path_buf, sizeof(path_buf), &err))  {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
+                                       goto FINISH_OFF;
+                               }
+
+                               if (!emstorage_move_file(ai->save, move_buf, false, &err))  {
                                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
 
                                        /*  delete all created files. */
-                                       if (!emstorage_get_save_name(EML_FOLDER, eml_mail_id, 0, NULL, buf, NULL)) {
+                                       if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0, NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
                                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
                                                /* goto FINISH_OFF; */
                                        }
 
-                                       if (!emstorage_delete_dir(buf, NULL)) {
+                                       if (!emstorage_delete_dir(move_buf, NULL)) {
                                                EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
                                                /* goto FINISH_OFF; */
                                        }
@@ -5402,17 +5278,19 @@ INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg, str
                                        goto FINISH_OFF;
                                }
 
-                               attachment[i].attachment_path = EM_SAFE_STRDUP(buf);
-
+                               attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
                        }
 
-                       EM_DEBUG_LOG("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
+                       EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
+                       save_status = ai->save_status;
                }
        }
        EM_DEBUG_LOG("Check #1");
+       EM_DEBUG_LOG("save_status : [%d]", save_status);
 
        p_mail_data->attachment_count = local_attachment_count;
        p_mail_data->inline_content_count = local_inline_content_count;
+       p_mail_data->body_download_status = (save_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) ? 1 : 0;
 
        eml_data_count += 1;
        ret = true;
@@ -5452,10 +5330,10 @@ INTERNAL_FUNC int emcore_parse_mime_file_to_mail(char *eml_file_path, email_mail
 
        int err = EMAIL_ERROR_NONE;
        int ret = false;
-       int is_file = 1;
-       char buf[512];
+       char move_buf[512];
+    char path_buf[512];
        char *mime_entity = NULL;
-       FILE *eml_fp = NULL;
+    char *multi_user_name = NULL;
        struct _m_content_info *cnt_info = NULL;
        struct _m_mesg *mmsg = NULL;
 
@@ -5482,32 +5360,21 @@ INTERNAL_FUNC int emcore_parse_mime_file_to_mail(char *eml_file_path, email_mail
                goto FINISH_OFF;
        }
 
-       eml_fp = fopen(eml_file_path, "r");
-       if (eml_fp == NULL) {
-               EM_DEBUG_EXCEPTION("file open failed");
-               err = EMAIL_ERROR_ON_PARSING;
-               goto FINISH_OFF;
-       }
-
-       if (!emcore_mime_parse_header(eml_fp, is_file, &mmsg->rfc822header, &mmsg->header, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_mime_parse_header failed : [%d]", err);
-               err = EMAIL_ERROR_INVALID_DATA;
+       mmsg->rfc822header = (struct _rfc822header *)em_malloc(sizeof(struct _rfc822header));
+       if (mmsg->rfc822header == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
-       if (!mmsg->header->part_header) {
-               EM_DEBUG_EXCEPTION("Invalid eml format");
+       emcore_gmime_init();
+       if (!emcore_gmime_eml_parse_mime(eml_file_path, mmsg->rfc822header, cnt_info, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_gmime_parse_mime failed : [%d]", err);
                err = EMAIL_ERROR_INVALID_DATA;
+               emcore_gmime_shutdown();
                goto FINISH_OFF;
        }
-
-       if (!emcore_mime_parse_body(eml_fp, is_file, mmsg, cnt_info, NULL, &err)) {
-               if (err != EMAIL_ERROR_NO_MORE_DATA) {
-                       EM_DEBUG_EXCEPTION("emcore_mime_parse_body failed : [%d]", err);
-                       err = EMAIL_ERROR_INVALID_DATA;
-                       goto FINISH_OFF;
-               }
-       }
+       emcore_gmime_shutdown();
 
        if (!emcore_make_mail_data_from_mime_data(mmsg, cnt_info, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
                EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_mime failed : [%d]", err);
@@ -5515,32 +5382,32 @@ INTERNAL_FUNC int emcore_parse_mime_file_to_mail(char *eml_file_path, email_mail
 
        }
 
-       if ((*output_mail_data)->smime_type == EMAIL_SMIME_SIGNED) {
+    memset(move_buf, 0x00, sizeof(move_buf));
+    memset(path_buf, 0x00, sizeof(path_buf));
+
+       if ((*output_mail_data)->smime_type == EMAIL_SMIME_SIGNED || (*output_mail_data)->smime_type == EMAIL_PGP_SIGNED) {
                if (!emcore_get_mime_entity(eml_file_path, &mime_entity, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_get_mime_entity failed: [%d]", err);
                        goto FINISH_OFF;
                }
-               
-               if (!emstorage_get_save_name((*output_mail_data)->account_id, (*output_mail_data)->mail_id, 0, "mime_entity", buf, &err)) {
+
+               if (!emstorage_get_save_name(multi_user_name, (*output_mail_data)->account_id, (*output_mail_data)->mail_id, 0, "mime_entity", move_buf, path_buf, sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed : [%d]", err);
                        goto FINISH_OFF;
                }
-       
-               if (!emstorage_move_file(mime_entity, buf, false, &err)) {
+
+               if (!emstorage_move_file(mime_entity, move_buf, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
-               }       
+               }
 
-               (*output_mail_data)->file_path_mime_entity = EM_SAFE_STRDUP(buf);
+               (*output_mail_data)->file_path_mime_entity = EM_SAFE_STRDUP(path_buf);
        }
 
        ret = true;
 
 FINISH_OFF:
 
-       if (eml_fp)
-               fclose(eml_fp);
-
        if (mmsg)
                emcore_mime_free_mime(mmsg);
 
@@ -5558,12 +5425,13 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_delete_parsed_data(email_mail_data_t *input_mail_data, int *err_code)
+INTERNAL_FUNC int emcore_delete_parsed_data(char *multi_user_name, email_mail_data_t *input_mail_data, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
        int err = EMAIL_ERROR_NONE;
        int ret = false;
-       char buf[512];
+       char buf[MAX_PATH] = {0};
+    char *prefix_path = NULL;
 
        if (!input_mail_data) {
                EM_DEBUG_EXCEPTION("Invliad parameter");
@@ -5577,25 +5445,41 @@ INTERNAL_FUNC int emcore_delete_parsed_data(email_mail_data_t *input_mail_data,
                goto FINISH_OFF;
        }
 
-       eml_data_count = eml_data_count - 1;
-
-       if (eml_data_count == 0) {
-               SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, input_mail_data->account_id);
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
+                       goto FINISH_OFF;
+               }
        } else {
-               SNPRINTF(buf, sizeof(buf), "%s%s%d%s%d", MAILHOME, DIR_SEPERATOR, input_mail_data->account_id, DIR_SEPERATOR, input_mail_data->mail_id);
+               prefix_path = strdup("");
        }
 
-       EM_DEBUG_LOG("Directory : [%s]", buf);
+       if (!input_mail_data->mail_id) {
+               SNPRINTF(buf, sizeof(buf), "%s%s%s%d", prefix_path, MAILHOME, DIR_SEPERATOR, input_mail_data->account_id);
+       } else {
+               SNPRINTF(buf, sizeof(buf), "%s%s%s%d%s%d", prefix_path, MAILHOME, DIR_SEPERATOR, input_mail_data->account_id, DIR_SEPERATOR, input_mail_data->mail_id);
+       }
 
        if (!emstorage_delete_dir(buf, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
-               goto FINISH_OFF;
+               EM_DEBUG_LOG("emstorage_delete_dir failed : buf[%s]", buf);
+                       memset(buf, 0x00, sizeof(buf));
+               SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, input_mail_data->account_id);
+
+                       if (!emstorage_delete_dir(buf, &err)) {
+                          EM_DEBUG_EXCEPTION("emstorage_delete_dir failed : buf[%s]", buf);
+                               goto FINISH_OFF;
+                       }
+
+               err = EMAIL_ERROR_NONE;
        }
 
        ret = true;
 
 FINISH_OFF:
 
+       EM_SAFE_FREE(prefix_path);
+
        if (err_code)
                *err_code = err;
 
@@ -5636,10 +5520,9 @@ INTERNAL_FUNC int emcore_get_mime_entity(char *mime_path, char **output_path, in
                goto FINISH_OFF;
        }
 
-       fp_read = fopen(mime_path, "r");
-       if (fp_read == NULL) {
-               EM_DEBUG_EXCEPTION_SEC("File open(read) is failed : filename [%s]", mime_path);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(mime_path, "r", &fp_read);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("File em_fopen(read) is failed : filename [%s][%d]", mime_path, err);
                goto FINISH_OFF;
        }
 
@@ -5665,10 +5548,9 @@ INTERNAL_FUNC int emcore_get_mime_entity(char *mime_path, char **output_path, in
        }
        EM_DEBUG_LOG("mime_entity_path = %s", mime_entity_path);
 
-       fp_write = fopen(mime_entity_path, "w");
-       if (fp_write == NULL) {
-               EM_DEBUG_EXCEPTION_SEC("File open(write) is failed : filename [%s]", mime_entity_path);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(mime_entity_path, "w", &fp_write);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("File em_fopen(write) is failed : filename [%s][%d]", mime_entity_path, err);
                goto FINISH_OFF;
        }
 
@@ -5746,7 +5628,8 @@ FINISH_OFF:
                emcore_mime_free_mime(mmsg);
 
        if (output_path)
-               *output_path = mime_entity_path;
+               *output_path = EM_SAFE_STRDUP(mime_entity_path);
+       EM_SAFE_FREE(mime_entity_path);
 
        if (err_code)
                *err_code = err;
@@ -5757,7 +5640,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int encoding, int mode, int is_text, int fd, char **holder, int *end_of_parsing, int *size)
+int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int encoding, int mode, int fd, char **holder, int *end_of_parsing, int *size)
 {
        EM_DEBUG_FUNC_BEGIN();
        int error = EMAIL_ERROR_NONE;
@@ -5780,7 +5663,7 @@ int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int enco
                memset(boundary_end, 0x00, BOUNDARY_LEN);
 
                SNPRINTF(boundary, BOUNDARY_LEN, "--%s%s", boundary_str, CRLF_STRING);
-               SNPRINTF(boundary_end, BOUNDARY_LEN, "--%s%s", boundary_str, "--\r\n");
+               SNPRINTF(boundary_end, BOUNDARY_LEN, "--%s%s", boundary_str, "--");
        }
 
        start_location = ftell(stream);
@@ -5797,10 +5680,6 @@ int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int enco
                                error = EMAIL_ERROR_SYSTEM_FAILURE;
                                goto FINISH_OFF;
                        }
-
-                       partial_body = 1;
-                       *end_of_parsing = 2;
-                       break;
                }
 
                if (boundary_str)  {
@@ -5808,13 +5687,20 @@ int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int enco
                                *end_of_parsing = 0;
                                break;
                        }
-                       else if (!strcmp(buf, boundary_end))  { /*  if ending boundary, the parsing of other multipart will be started */
+                       else if (strcasestr(buf, boundary_end))  {      /*  if ending boundary, the parsing of other multipart will be started */
                                *end_of_parsing = 1;
                                break;
                        }
                }
+
+               if (error != EMAIL_ERROR_NONE) {
+                       partial_body = 1;
+                       *end_of_parsing = 2;
+                       break;
+               }
        }
 
+       EM_DEBUG_LOG("partial_body : [%d], buf:[%s]", partial_body, buf);
        end_location = ftell(stream);
        if(end_location < 0 ) { /*prevent 35555*/
                error = EMAIL_ERROR_INVALID_PARAM;
@@ -5844,26 +5730,20 @@ int emcore_decode_body_text_from_file(FILE *stream, char *boundary_str, int enco
        if (mode > SAVE_TYPE_SIZE) {    /*  decode content */
                emcore_decode_body_text(body, p_size, encoding, &dec_len, &error);
 
-               if (is_text) {
-                       modified_body = em_replace_all_string(body, "cid:", "");
-                       modified_body_size = EM_SAFE_STRLEN(modified_body); /*prevent 35585 */
-               }
-
+               modified_body = em_malloc(dec_len + 1);
                if (modified_body == NULL) {
-                       modified_body = em_malloc(dec_len + 1);
-                       if (modified_body == NULL) {
-                               EM_DEBUG_EXCEPTION("em_malloc failed");
-                               error = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
-
-                       memcpy(modified_body, body, dec_len);  /*prevent 35585 */
-                       modified_body_size = dec_len;
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       error = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
                }
 
+               memcpy(modified_body, body, dec_len);  /*prevent 35585 */
+               modified_body_size = dec_len;
+
                if (mode == SAVE_TYPE_BUFFER)  {   /*  save content to buffer */
                        *holder = EM_SAFE_STRDUP(modified_body);
                } else if (mode == SAVE_TYPE_FILE)  {   /*  save content to file */
+                       EM_DEBUG_LOG("Before write : size[%d]", modified_body_size);
                        if (write(fd, modified_body, modified_body_size) != modified_body_size)  {
                                EM_DEBUG_EXCEPTION("write failed");
                                error = EMAIL_ERROR_SYSTEM_FAILURE;
@@ -5884,7 +5764,7 @@ FINISH_OFF:
        return error;
 }
 
-int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int encoding, int mode, int is_text, int fd, char **holder, int *end_of_parsing, int *size)
+int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int encoding, int mode, int fd, char **holder, int *end_of_parsing, int *size)
 {
        EM_DEBUG_FUNC_BEGIN();
        int error = EMAIL_ERROR_NONE;
@@ -5907,12 +5787,13 @@ int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int enco
        }
 
        while (TRUE) {
+/* don't delete the comment. several threads including event thread call it
                if (!emcore_check_thread_status())  {
                        EM_DEBUG_EXCEPTION("EMAIL_ERROR_CANCELLED");
                        error = EMAIL_ERROR_CANCELLED;
                        goto FINISH_OFF;
                }
-
+*/
                if (!emcore_mime_get_line_from_sock(stream, buf, MIME_LINE_LEN, &error)) {
                        if (error != EMAIL_ERROR_NO_MORE_DATA) {
                                EM_DEBUG_EXCEPTION("emcore_mime_get_line_from_sock failed");
@@ -5959,24 +5840,16 @@ int emcore_decode_body_text_from_sock(void *stream, char *boundary_str, int enco
                if (mode > SAVE_TYPE_SIZE) {    /*  decode content */
                        emcore_decode_body_text(buf, dec_len, encoding, &dec_len, &error);
 
-                       if (is_text) {
-                               result_buffer = em_replace_string(buf, "cid:", "");
-                               if (result_buffer)
-                                       result_buffer_size = EM_SAFE_STRLEN(result_buffer);
-                       }
-
+                       result_buffer = em_malloc(dec_len + 1);
                        if (result_buffer == NULL) {
-                               result_buffer = em_malloc(dec_len + 1);
-                               if (result_buffer == NULL) {
-                                       EM_DEBUG_EXCEPTION("em_malloc failed");
-                                       error = EMAIL_ERROR_OUT_OF_MEMORY;
-                                       goto FINISH_OFF;
-                               }
-
-                               memcpy(result_buffer, buf, dec_len); /*prevent 35499*/
-                               result_buffer_size = dec_len;
+                               EM_DEBUG_EXCEPTION("em_malloc failed");
+                               error = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
                        }
 
+                       memcpy(result_buffer, buf, dec_len); /*prevent 35499*/
+                       result_buffer_size = dec_len;
+
                        if (mode == SAVE_TYPE_BUFFER)  {   /*  save content to buffer */
                                pTemp = realloc(*holder, sz + result_buffer_size + 2);
                                if (!pTemp)  {
@@ -6018,3 +5891,39 @@ FINISH_OFF:
        return error;
 }
 
+INTERNAL_FUNC int emcore_get_digest_type(char *micalg_value)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int digest_type = DIGEST_TYPE_NONE;
+
+       if (micalg_value == NULL)
+               return DIGEST_TYPE_NONE;
+
+       if (strcasestr(micalg_value, "sha1"))
+               digest_type = DIGEST_TYPE_SHA1;
+       else if (strcasestr(micalg_value, "md5"))
+               digest_type = DIGEST_TYPE_MD5;
+       else if (strcasestr(micalg_value, "ripemd160"))
+               digest_type = DIGEST_TYPE_RIPEMD160;
+       else if (strcasestr(micalg_value, "md2"))
+               digest_type = DIGEST_TYPE_MD2;
+       else if (strcasestr(micalg_value, "tiger192"))
+               digest_type = DIGEST_TYPE_TIGER192;
+       else if (strcasestr(micalg_value, "haval5160"))
+               digest_type = DIGEST_TYPE_HAVAL5160;
+       else if (strcasestr(micalg_value, "sha256"))
+               digest_type = DIGEST_TYPE_SHA256;
+       else if (strcasestr(micalg_value, "sha384"))
+               digest_type = DIGEST_TYPE_SHA384;
+       else if (strcasestr(micalg_value, "sha512"))
+               digest_type = DIGEST_TYPE_SHA512;
+       else if (strcasestr(micalg_value, "sha224"))
+               digest_type = DIGEST_TYPE_SHA224;
+       else if (strcasestr(micalg_value, "md4"))
+               digest_type = DIGEST_TYPE_MD4;
+       else
+               digest_type = DIGEST_TYPE_NONE;
+
+       EM_DEBUG_FUNC_END();
+       return digest_type;
+}
index ed1c71c..59643ca 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,9 +24,9 @@
  * File :  email-core-mm_callbacks.c
  * Desc :  mm_callbacks for IMAP-2004g
  *
- * Auth :  
+ * Auth :
  *
- * History : 
+ * History :
  * 2006.08.22  :  created
  *****************************************************************************/
 #include <stdio.h>
@@ -60,7 +60,7 @@ INTERNAL_FUNC void mm_lsub(MAILSTREAM *stream, int delimiter, char *mailbox, lon
 
        /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
        enc_path = strchr(mailbox, '}');
-       if (enc_path)   
+       if (enc_path)
                enc_path += 1;
        else {
                emcore_free_mailbox_list(&p, count+1);
@@ -113,7 +113,7 @@ INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, lon
 
        /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
        enc_path = strchr(mailbox, '}');
-       if (enc_path)   
+       if (enc_path)
                enc_path += 1;
        else {
                emcore_free_internal_mailbox(&p, count+1, NULL);
@@ -121,8 +121,8 @@ INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, lon
        }
 
        /* convert directory delimiter to '/' */
-       for (s = enc_path;*s;s++) 
-               if (*s == (char)delimiter) 
+       for (s = enc_path;*s;s++)
+               if (*s == (char)delimiter)
                        *s = '/';
 
        /* copy string */
@@ -140,7 +140,7 @@ INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, lon
        else if(attributes & LATT_XLIST_JUNK)
                p[count].mailbox_type = EMAIL_MAILBOX_TYPE_SPAMBOX;
        else if(attributes & LATT_XLIST_FLAGGED)
-               p[count].mailbox_type = EMAIL_MAILBOX_TYPE_FLAGGED;
+               p[count].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;                //EMAIL_MAILBOX_TYPE_FLAGGED; P141122-00523 sync starred folder as Inbox sync
        else if(attributes & LATT_XLIST_TRASH)
                p[count].mailbox_type = EMAIL_MAILBOX_TYPE_TRASH;
 #endif /* __FEATURE_XLIST_SUPPORT__ */
@@ -190,9 +190,9 @@ INTERNAL_FUNC void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS* stat
        email_callback_holder_t *p = stream->sparep;
 
        EM_DEBUG_FUNC_BEGIN();
-       if (status->flags & SA_MESSAGES) 
+       if (status->flags & SA_MESSAGES)
                p->num = status->messages;
-       if (status->flags & SA_UNSEEN) 
+       if (status->flags & SA_UNSEEN)
                p->data = (void *)status->unseen;
        EM_DEBUG_FUNC_END();
 }
@@ -210,16 +210,26 @@ INTERNAL_FUNC void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
        char *password = NULL;
        char *token    = NULL;
        char *save_ptr = NULL;
+       char *user_info = NULL;
+    char *temp = NULL;
+       char *multi_user_name = NULL;
 
        if (!mb->user[0])  {
                EM_DEBUG_EXCEPTION("invalid account_id...");
                goto FINISH_OFF;
        }
-       
-       account_id = atoi(mb->user);
 
-       ref_account = emcore_get_account_reference(account_id);
+       user_info = EM_SAFE_STRDUP(mb->user);
+
+       token = strtok_r(user_info, TOKEN_FOR_MULTI_USER, &temp);
+       EM_DEBUG_LOG_SEC("Token : [%s], multi_user_name:[%s][%d]", token, temp, EM_SAFE_STRLEN(temp));
+       account_id = atoi(token);
+       token = NULL;
 
+    if (temp != NULL && EM_SAFE_STRLEN(temp) > 0)
+        multi_user_name = EM_SAFE_STRDUP(temp);
+
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
                goto FINISH_OFF;
@@ -263,8 +273,10 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
+       EM_SAFE_FREE(user_info);
        EM_SAFE_FREE(username);
        EM_SAFE_FREE(password);
+    EM_SAFE_FREE(multi_user_name);
 
        EM_DEBUG_FUNC_END();
 }
@@ -279,43 +291,44 @@ INTERNAL_FUNC void mm_dlog(char *string)
 
 INTERNAL_FUNC void mm_log(char *string, long errflg)
 {
-       
+
        switch ((short)errflg)  {
                case NIL:
-                       EM_DEBUG_LOG("IMAP_TOOLKIT_LOG NIL [%s]", string);
+                       EM_DEBUG_LOG("IMAP_TOOLKIT_LOG [%s]", string);
                        break;
-                       
+
                case WARN:
-                       EM_DEBUG_LOG("IMAP_TOOLKIT_LOG WARN [%s]", string);
+                       EM_DEBUG_EXCEPTION ("IMAP_TOOLKIT_LOG WARN [%s]", string);
                        break;
-                       
+
                case PARSE:
                        EM_DEBUG_LOG("IMAP_TOOLKIT_LOG PARSE [%s]", string);
                        break;
-                       
+
                case BYE:
                        EM_DEBUG_LOG("IMAP_TOOLKIT_LOG BYE [%s]", string);
                        break;
-                       
+
                case TCPDEBUG:
-                       EM_DEBUG_LOG("IMAP_TOOLKIT_LOG TCPDEBUG [%s]", string);
+                       EM_DEBUG_LOG_SEC("IMAP_TOOLKIT_LOG TCPDEBUG [%s]", string);
                        break;
-                       
+
                case ERROR: {
                        email_session_t *session = NULL;
-                       
+
                        EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%s]", string);
 
                        emcore_get_current_session(&session);
-                       
+
                        if (session) {
                                mm_get_error(string, &session->error);
                                EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%d]", session->error);
-                               if(session->error == EMAIL_ERROR_XOAUTH_BAD_REQUEST || session->error == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED) {
+                               if (session->error == EMAIL_ERROR_XOAUTH_BAD_REQUEST ||
+                            session->error == EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED) {
                                        session->network = session->error;
                                }
                        }
-                       
+
                        break;
                }
        }
@@ -348,7 +361,9 @@ INTERNAL_FUNC void mm_flags(MAILSTREAM *stream, unsigned long number)
 INTERNAL_FUNC void mm_notify(MAILSTREAM *stream, char *string, long errflg)
 {
        EM_DEBUG_FUNC_BEGIN();
+#ifdef FEATURE_CORE_DEBUG
        mm_log(string, errflg);
+#endif /* FEATURE_CORE_DEBUG */
        EM_DEBUG_FUNC_END();
 }
 
@@ -440,6 +455,8 @@ INTERNAL_FUNC void mm_get_error(char *string, int *err_code)
                *err_code = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
        else if (strstr(string, "\"status\":\"401"))
                *err_code = EMAIL_ERROR_XOAUTH_INVALID_UNAUTHORIZED;
+       else if (strstr(string, "ALREADYEXISTS"))
+               *err_code = EMAIL_ERROR_ALREADY_EXISTS;
        else
                *err_code = EMAIL_ERROR_UNKNOWN;
 }
@@ -449,7 +466,6 @@ INTERNAL_FUNC void mm_imap_id (char **id_string)
 {
        EM_DEBUG_FUNC_BEGIN("id_string [%p]", id_string);
 
-       int   err = EMAIL_ERROR_NONE;
        /*
        char *result_string = NULL;
        char *tag_string = "ID (\"os\" \"" IMAP_ID_OS "\" \"os-version\" \"" IMAP_ID_OS_VERSION "\" \"vendor\" \"" IMAP_ID_VENDOR "\" \"device\" \"" IMAP_ID_DEVICE_NAME "\" \"AGUID\" \"" IMAP_ID_AGUID "\" \"ACLID\" \"" IMAP_ID_ACLID "\"";
@@ -458,7 +474,6 @@ INTERNAL_FUNC void mm_imap_id (char **id_string)
 
        if (id_string == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
@@ -470,15 +485,13 @@ INTERNAL_FUNC void mm_imap_id (char **id_string)
 
        if(result_string == NULL) {
                EM_DEBUG_EXCEPTION("malloc failed");
-               err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
        *id_string = result_string;
        */
-
 FINISH_OFF:
-       EM_DEBUG_FUNC_END("err [%d]", err);
+       return ;
 }
 #endif /* __FEATURE_SUPPORT_IMAP_ID__ */
 /* EOF */
diff --git a/email-core/email-core-pgp.c b/email-core/email-core-pgp.c
new file mode 100644 (file)
index 0000000..9d2a238
--- /dev/null
@@ -0,0 +1,744 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+
+
+/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***
+ *File :  email-core-smime.c
+ *Desc :  MIME Operation
+ *
+ *Auth :
+ *
+ *History :
+ *   2011.04.14  :  created
+ ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***/
+#undef close
+
+#include <glib.h>
+#include <gmime/gmime.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <vconf.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "email-utilities.h"
+#include "email-core-global.h"
+#include "email-core-utils.h"
+#include "email-core-mail.h"
+#include "email-core-smtp.h"
+#include "email-storage.h"
+#include "email-core-cert.h"
+#include "email-debug-log.h"
+
+#define PGP_SIGNED_FILE     "signature.asc"
+#define PGP_ENCRYPTED_FILE  "encrypted.asc"
+#define DECRYPTED_TEMP_FILE "decrypted_temp_file.eml"
+
+static char *passphrase = NULL;
+
+static gboolean request_passwd(GMimeCryptoContext *ctx, const char *user_id, const char *prompt_ctx, gboolean reprompt, GMimeStream *response, GError **err)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_LOG("passpharse : [%s]", passphrase);
+        if (g_mime_stream_write_string (response, passphrase) == -1 ||
+            g_mime_stream_write (response, "\n", 1) == -1) {
+                g_set_error (err, GMIME_ERROR, errno, "%s", g_strerror (errno));
+                return false;
+        }
+
+       EM_SAFE_FREE(passphrase);
+
+       EM_DEBUG_FUNC_END();
+       return true;
+}
+
+static int emcore_pgp_get_gmime_digest_algo(email_digest_type digest_type)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int gmime_digest_algo = GMIME_DIGEST_ALGO_DEFAULT;
+
+       switch (digest_type) {
+       case DIGEST_TYPE_SHA1:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_SHA1;
+               break;
+       case DIGEST_TYPE_MD5:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_MD5;
+               break;
+       case DIGEST_TYPE_RIPEMD160:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_RIPEMD160;
+               break;
+       case DIGEST_TYPE_MD2:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_MD2;
+               break;
+       case DIGEST_TYPE_TIGER192:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_TIGER192;
+               break;
+       case DIGEST_TYPE_HAVAL5160:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_HAVAL5160;
+               break;
+       case DIGEST_TYPE_SHA256:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_SHA256;
+               break;
+       case DIGEST_TYPE_SHA384:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_SHA384;
+               break;
+       case DIGEST_TYPE_SHA512:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_SHA512;
+               break;
+       case DIGEST_TYPE_SHA224:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_SHA224;
+               break;
+       case DIGEST_TYPE_MD4:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_MD4;
+               break;
+       default:
+               gmime_digest_algo = GMIME_DIGEST_ALGO_DEFAULT;
+               break;
+       }
+
+       EM_DEBUG_FUNC_END();
+       return gmime_digest_algo;
+}
+
+static int get_stack_of_recipients(char *recipients, GPtrArray **output_recipients_array) {
+       EM_DEBUG_FUNC_BEGIN();
+       int err                       = EMAIL_ERROR_NONE;
+       int i, j                      = 0;
+       char *temp_recipients         = NULL;
+       char *email_address           = NULL;
+
+       ADDRESS *token_address        = NULL;
+       GPtrArray *p_recipients_array = NULL;
+
+       p_recipients_array = g_ptr_array_new();
+       if (p_recipients_array == NULL) {
+               EM_DEBUG_EXCEPTION("g_ptr_array_new failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       temp_recipients = EM_SAFE_STRDUP(recipients);
+       for (i = 0, j = EM_SAFE_STRLEN(temp_recipients); i < j ; i++)
+               if (temp_recipients[i] == ';') temp_recipients[i] = ',';
+
+       rfc822_parse_adrlist(&token_address, temp_recipients, NULL);
+
+       while (token_address) {
+               email_address = NULL;
+               email_address = g_strdup_printf("%s@%s", token_address->mailbox, token_address->host);
+               
+               g_ptr_array_add(p_recipients_array, email_address);
+               token_address = token_address->next;
+       }
+       
+FINISH_OFF:
+
+       if (err != EMAIL_ERROR_NONE)
+               g_ptr_array_free(p_recipients_array, true);
+       else 
+               *output_recipients_array = p_recipients_array;
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+static GMimeSignatureStatus get_signature_status(GMimeSignatureList *signatures)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+        int i = 0;
+
+        GMimeSignature *sig = NULL;
+        GMimeSignatureStatus status = GMIME_SIGNATURE_STATUS_GOOD;
+     
+        if (!signatures || signatures->array->len == 0)
+                return GMIME_SIGNATURE_STATUS_ERROR;
+     
+        for (i = 0; i < g_mime_signature_list_length(signatures); i++) {
+                sig = g_mime_signature_list_get_signature(signatures, i); 
+                status = MAX(status, sig->status);
+        }   
+
+       EM_DEBUG_FUNC_END();
+
+       return status;
+}
+
+INTERNAL_FUNC int emcore_pgp_set_signed_message(char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("Certificate path : [%s], mime_entity : [%s]", certificate, mime_entity);
+
+#ifdef __FEATURE_SECURE_PGP__
+       int err = EMAIL_ERROR_NONE;
+       int clear_fd = 0;
+       int gpg_fd = 0;
+       int p_digest_type = 0;
+       char temp_pgp_filepath[512] = {0, };
+
+       GMimeCryptoContext *ctx  = NULL;
+       GMimeStream *clear_text  = NULL;
+       GMimeStream *signed_text = NULL;
+       GError *g_err            = NULL;
+
+       if (!password || !mime_entity || !user_id) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       /* Initialized the output stream (signed stream) */
+       EM_SAFE_FREE(passphrase);
+       passphrase = EM_SAFE_STRDUP(password);
+
+       g_mime_init(0);
+
+       SNPRINTF(temp_pgp_filepath, sizeof(temp_pgp_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, PGP_SIGNED_FILE);
+       EM_DEBUG_LOG_SEC("attachment file path of pgp : [%s]", temp_pgp_filepath);
+
+       err = em_open(temp_pgp_filepath, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH, &gpg_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", temp_pgp_filepath, err);
+               goto FINISH_OFF;
+       }
+
+       signed_text = g_mime_stream_fs_new(gpg_fd);
+       if (g_mime_stream_reset(signed_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset signed_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       ctx = g_mime_gpg_context_new(request_passwd, "/usr/bin/gpg");
+       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *)ctx, true);
+
+       /* Initialized the input stream (clear text stream) */
+       EM_DEBUG_LOG("mime_entity : [%s]", mime_entity);
+       err = em_open(mime_entity, O_RDONLY, 0, &clear_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", mime_entity, err);
+               goto FINISH_OFF;
+       }
+
+       clear_text = g_mime_stream_fs_new(clear_fd);
+       if (g_mime_stream_reset(clear_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset clear_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the digest type of gmime */
+       p_digest_type = emcore_pgp_get_gmime_digest_algo(digest_type);
+
+       /* Set the signed message */
+       if ((g_mime_crypto_context_sign(ctx, user_id, p_digest_type, clear_text, signed_text, &g_err) < 0) && (g_err != NULL)) {
+               EM_DEBUG_EXCEPTION("g_mime_crypto_context_sign failed : [%s]", g_err->message);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       if (g_err)
+               g_error_free(g_err);
+
+       if (clear_text)
+               g_object_unref(clear_text);
+
+       if (signed_text)
+               g_object_unref(signed_text);
+
+       if (ctx)
+               g_object_unref(ctx);
+
+       g_mime_shutdown();
+
+       close(clear_fd);
+       close(gpg_fd);
+
+       if (file_path)
+               *file_path = EM_SAFE_STRDUP(temp_pgp_filepath);
+
+       return err;     
+#else /* __FEATURE_SECURE_PGP__ */
+
+       return EMAIL_ERROR_NOT_SUPPORTED;
+
+#endif
+}
+
+INTERNAL_FUNC int emcore_pgp_set_encrypted_message(char *recipient_list, char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("Certificate path : [%s], mime_entity : [%s]", certificate, mime_entity);
+
+#ifdef __FEATURE_SECURE_PGP__
+       int err = EMAIL_ERROR_NONE;
+       int clear_fd = 0;
+       int gpg_fd = 0;
+       int p_digest_type = 0;
+       char temp_pgp_filepath[512] = {0, };
+
+       GPtrArray *recipients = NULL;
+
+       GMimeCryptoContext *ctx     = NULL;
+       GMimeStream *clear_text     = NULL;
+       GMimeStream *encrypted_text = NULL;
+       GError *g_err               = NULL;
+
+       if (!recipient_list || !password || !mime_entity || !user_id) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       /* Initialized the output stream (signed stream) */
+       EM_SAFE_FREE(passphrase);
+       passphrase = EM_SAFE_STRDUP(password);
+
+       g_mime_init(0);
+
+       SNPRINTF(temp_pgp_filepath, sizeof(temp_pgp_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, PGP_ENCRYPTED_FILE);
+       EM_DEBUG_LOG_SEC("attachment file path of pgp : [%s]", temp_pgp_filepath);
+
+       err = em_open(temp_pgp_filepath, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH, &gpg_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", temp_pgp_filepath, err);
+               goto FINISH_OFF;
+       }
+
+       encrypted_text = g_mime_stream_fs_new(gpg_fd);
+       if (g_mime_stream_reset(encrypted_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset encrypted_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       ctx = g_mime_gpg_context_new(request_passwd, "/usr/bin/gpg");
+       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *)ctx, true);
+
+       /* Initialized the input stream (clear text stream) */
+       EM_DEBUG_LOG("mime_entity : [%s]", mime_entity);
+       err = em_open(mime_entity, O_RDONLY, 0, &clear_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", mime_entity, err);
+               goto FINISH_OFF;
+       }
+
+       clear_text = g_mime_stream_fs_new(clear_fd);
+       if (g_mime_stream_reset(clear_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset clear_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the digest type of gmime */
+       p_digest_type = emcore_pgp_get_gmime_digest_algo(digest_type);
+
+       /* Set the recipients list */
+       if ((err = get_stack_of_recipients(recipient_list, &recipients)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("get_stack_of_recipients failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       /* Set the signed message */
+       if ((g_mime_crypto_context_encrypt(ctx, false, user_id, p_digest_type, recipients, clear_text, encrypted_text, &g_err) < 0) && (g_err != NULL)) {
+               EM_DEBUG_EXCEPTION("NO signature : g_mime_crypto_context_encrypt failed : [%s]", g_err->message);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       g_ptr_array_free(recipients, true);
+
+       if (g_err)
+               g_error_free(g_err);
+
+       if (clear_text)
+               g_object_unref(clear_text);
+
+       if (encrypted_text)
+               g_object_unref(encrypted_text);
+
+       if (ctx)
+               g_object_unref(ctx);
+
+       g_mime_shutdown();
+
+       close(clear_fd);
+       close(gpg_fd);
+
+       if (file_path)
+               *file_path = EM_SAFE_STRDUP(temp_pgp_filepath);
+
+       return err;     
+#else /* __FEATURE_SECURE_PGP__ */
+
+       return EMAIL_ERROR_NOT_SUPPORTED;
+
+#endif
+
+}
+
+INTERNAL_FUNC int emcore_pgp_set_signed_and_encrypted_message(char *recipient_list, char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("mime_entity : [%s]", mime_entity);
+
+#ifdef __FEATURE_SECURE_PGP__
+       int err                     = EMAIL_ERROR_NONE;
+       int clear_fd                = 0;
+       int gpg_fd                  = 0;
+       int p_digest_type           = 0;
+       char temp_pgp_filepath[512] = {0, };
+
+       GPtrArray *recipients       = NULL;
+       
+       GMimeCryptoContext *ctx     = NULL;
+       GMimeStream *clear_text     = NULL;
+       GMimeStream *encrypted_text = NULL;
+       GError *g_err               = NULL;
+
+       if (!recipient_list || !password || !mime_entity || !user_id) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       /* Initialized the output stream (signed stream) */
+       EM_SAFE_FREE(passphrase);
+       passphrase = strdup(password);
+
+       g_mime_init(0);
+
+       SNPRINTF(temp_pgp_filepath, sizeof(temp_pgp_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, PGP_ENCRYPTED_FILE);
+       EM_DEBUG_LOG_SEC("attachment file path of pgp : [%s]", temp_pgp_filepath);
+
+       err = em_open(temp_pgp_filepath, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH, &gpg_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s], [%d]", temp_pgp_filepath, err);
+               goto FINISH_OFF;
+       }
+
+       encrypted_text = g_mime_stream_fs_new(gpg_fd);
+       if (g_mime_stream_reset(encrypted_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset encrypted_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       ctx = g_mime_gpg_context_new(request_passwd, "/usr/bin/gpg");
+       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *)ctx, true);
+
+       /* Initialized the input stream (clear text stream) */
+       EM_DEBUG_LOG("mime_entity : [%s]", mime_entity);
+       err = em_open(mime_entity, O_RDONLY, 0, &clear_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", mime_entity, err);
+               goto FINISH_OFF;
+       }
+
+       clear_text = g_mime_stream_fs_new(clear_fd);
+       if (g_mime_stream_reset(clear_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset clear_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the digest type of gmime */
+       p_digest_type = emcore_pgp_get_gmime_digest_algo(digest_type);
+
+       /* Set the recipients list */
+       if ((err = get_stack_of_recipients(recipient_list, &recipients)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("get_stack_of_recipients failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       /* Set the signed message */
+       if ((g_mime_crypto_context_encrypt(ctx, true, user_id, p_digest_type, recipients, clear_text, encrypted_text, &g_err) < 0) && (g_err != NULL)) {
+               EM_DEBUG_EXCEPTION("Signature : g_mime_crypto_context_encrypt failed : [%s]", g_err->message);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       g_ptr_array_free(recipients, true);
+
+       if (g_err)
+               g_error_free(g_err);
+
+       if (clear_text)
+               g_object_unref(clear_text);
+
+       if (encrypted_text)
+               g_object_unref(encrypted_text);
+
+       if (ctx)
+               g_object_unref(ctx);
+
+       g_mime_shutdown();
+
+       close(clear_fd);
+       close(gpg_fd);
+
+       if (file_path)
+               *file_path = EM_SAFE_STRDUP(temp_pgp_filepath);
+
+       return err;     
+#else /* __FEATURE_SECURE_PGP__ */
+
+       return EMAIL_ERROR_NOT_SUPPORTED;
+
+#endif
+
+}
+
+INTERNAL_FUNC int emcore_pgp_get_verify_signature(char *signature_path, char *mime_entity, email_digest_type digest_type, int *verify)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("signature path : [%s], mime_entity : [%s]", signature_path, mime_entity);
+
+#ifdef __FEATURE_SECURE_PGP__
+       int err = EMAIL_ERROR_NONE;
+       int clear_fd = 0;
+       int signed_fd = 0;
+       int p_digest_type = 0;
+       int p_verify = false;
+
+       GMimeCryptoContext *ctx        = NULL;
+       GMimeStream *clear_text        = NULL;
+       GMimeStream *signed_text       = NULL;
+       GMimeSignatureList *signatures = NULL;
+       GError *g_err                  = NULL;
+
+       if (!signature_path || !mime_entity) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       /* Initialized the Context */
+       g_mime_init(0);
+
+       ctx = g_mime_gpg_context_new(request_passwd, "/usr/bin/gpg");
+       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *)ctx, true);
+
+       /* Initialized the input stream (clear text stream) */
+       EM_DEBUG_LOG("mime_entity : [%s]", mime_entity);
+       err = em_open(mime_entity, O_RDONLY, 0, &clear_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", mime_entity, err);
+               goto FINISH_OFF;
+       }
+
+       clear_text = g_mime_stream_fs_new(clear_fd);
+       if (g_mime_stream_reset(clear_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset clear_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Initialized the output stream (signed stream) */
+       EM_DEBUG_LOG("signature_path : [%s]", signature_path);
+       err = em_open(signature_path, O_RDONLY, 0, &signed_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", signature_path, err);
+               goto FINISH_OFF;
+       }
+       
+       signed_text = g_mime_stream_fs_new(signed_fd);
+       if (g_mime_stream_reset(signed_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset signed_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the digest type of gmime */
+       p_digest_type = emcore_pgp_get_gmime_digest_algo(digest_type);
+
+       /* Verify the signature */
+       signatures = g_mime_crypto_context_verify(ctx, p_digest_type, clear_text, signed_text, &g_err);
+       if (signatures == NULL) {
+               EM_DEBUG_EXCEPTION("g_mime_crypto_context_verify failed : [%s]", g_err->message);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (get_signature_status(signatures) != GMIME_SIGNATURE_STATUS_GOOD) {
+               EM_DEBUG_LOG("Invalid the signature");
+               goto FINISH_OFF;
+       }
+
+       p_verify = true;
+
+FINISH_OFF:
+
+       if (g_err)
+               g_error_free(g_err);
+
+       if (signatures)
+               g_object_unref(signatures);
+
+       if (clear_text)
+               g_object_unref(clear_text);
+
+       if (signed_text)
+               g_object_unref(signed_text);
+
+       if (ctx)
+               g_object_unref(ctx);
+
+       g_mime_shutdown();
+
+       close(clear_fd);
+       close(signed_fd);
+
+       if (verify)
+               *verify = p_verify;
+
+       return err;     
+#else /* __FEATURE_SECURE_PGP__ */
+
+       return EMAIL_ERROR_NOT_SUPPORTED;
+
+#endif
+
+}
+
+INTERNAL_FUNC int emcore_pgp_get_decrypted_message(char *encrypted_message, char *password, int sign, char **decrypted_file, int *verify)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("Encrypted message : [%s], password : [%s]", encrypted_message, password);
+
+#ifdef __FEATURE_SECURE_PGP__
+       int err                         = EMAIL_ERROR_NONE;
+       int p_verify                    = false;
+       int decrypted_fd                = 0;
+       int encrypted_fd                = 0;
+       char temp_decrypt_filepath[512] = {0, };
+
+       GError *g_err                   = NULL;
+       GMimeCryptoContext *ctx         = NULL;
+       GMimeDecryptResult *result      = NULL;
+       GMimeStream *encrypted_text     = NULL;
+       GMimeStream *decrypted_text     = NULL;
+
+       if (!encrypted_message && !password) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               return err;
+       }
+
+       EM_SAFE_FREE(passphrase);
+       passphrase = EM_SAFE_STRDUP(password);
+
+       g_mime_init(0);
+
+       /* Initialized the context */
+       ctx = g_mime_gpg_context_new(request_passwd, "/usr/bin/gpg");
+       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *)ctx, true);
+
+       /* Initialized the input stream (clear text stream) */
+       err = em_open(encrypted_message, O_RDONLY, 0, &encrypted_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", encrypted_message, err);
+               goto FINISH_OFF;
+       }
+
+       encrypted_text = g_mime_stream_fs_new(encrypted_fd);
+       if (g_mime_stream_reset(encrypted_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset encrypted_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Initialized the output stream (signed stream) */
+       SNPRINTF(temp_decrypt_filepath, sizeof(temp_decrypt_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, DECRYPTED_TEMP_FILE);
+       EM_DEBUG_LOG_SEC("tmp decrypt file path : [%s]", temp_decrypt_filepath);
+
+       err = em_open(temp_decrypt_filepath, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH, &decrypted_fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open failed : [%s] [%d]", temp_decrypt_filepath, err);
+               goto FINISH_OFF;
+       }
+
+       decrypted_text = g_mime_stream_fs_new(decrypted_fd);
+       if (g_mime_stream_reset(decrypted_text) == -1) {
+               EM_DEBUG_EXCEPTION("g_mime_stream_reset decrypted_text failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       /* Get the decrypt message */
+       result = g_mime_crypto_context_decrypt(ctx, encrypted_text, decrypted_text, &g_err);
+       if (result == NULL) {
+               EM_DEBUG_EXCEPTION("g_mime_crypto_context_decrypt failed : [%s]", g_err->message);
+               err = EMAIL_ERROR_DECRYPT_FAILED;
+               goto FINISH_OFF;
+       }
+
+        if (sign) {
+                if (!result->signatures || get_signature_status(result->signatures) != GMIME_SIGNATURE_STATUS_GOOD)
+                       p_verify = false;
+               else
+                       p_verify = true;
+        } 
+
+FINISH_OFF:
+
+       if (g_err)
+               g_error_free(g_err);
+
+       if (ctx)
+               g_object_unref(ctx);
+
+       if (encrypted_text)
+               g_object_unref(encrypted_text);
+
+       if (decrypted_text)
+               g_object_unref(decrypted_text);
+
+       if (result)
+               g_object_unref(result);
+
+       g_mime_shutdown();
+
+       close(encrypted_fd);
+       close(decrypted_fd);
+
+       if (verify)
+               *verify         = p_verify;
+       
+       if (decrypted_file)
+               *decrypted_file = EM_SAFE_STRDUP(temp_decrypt_filepath);
+
+       return err;     
+#else /* __FEATURE_SECURE_PGP__ */
+
+       return EMAIL_ERROR_NOT_SUPPORTED;
+
+#endif
+}
+
index eaf9818..c19567c 100755 (executable)
@@ -163,8 +163,6 @@ FINISH_OFF:
        return ret;\r
 }\r
 \r
-\r
-\r
 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)\r
 {\r
        EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);\r
@@ -175,6 +173,7 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
        int                 i = 0;\r
        dbus_uint32_t   error = 0;\r
        int ret = true;\r
+       const char *null_string = "";\r
 \r
        ENTER_CRITICAL_SECTION(_dbus_noti_lock);\r
 \r
@@ -199,6 +198,11 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.handle), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.account_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.mail_id), DBUS_TYPE_INVALID);\r
+                       if (data->send_mail.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
                case ACTIVE_SYNC_NOTI_SEND_SAVED:                               /*  publish a send notification to ASE (active sync engine) */\r
                        EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_SAVED]", subType);\r
@@ -214,6 +218,11 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.handle ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.account_id ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.mailbox_id ), DBUS_TYPE_INVALID);\r
+                       if (data->sync_header.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->sync_header.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
                case ACTIVE_SYNC_NOTI_DOWNLOAD_BODY:                    /*  publish a download body notification to ASE */\r
                        EM_DEBUG_LOG("handle:[%d]", data->download_body.handle);\r
@@ -225,6 +234,13 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.account_id  ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.mail_id ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.with_attachment  ), DBUS_TYPE_INVALID);\r
+                       if (data->download_body.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->download_body.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
+                       break;\r
+\r
                        break;\r
                case ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT:\r
                        EM_DEBUG_LOG("handle:[%d]", data->download_attachment.handle);\r
@@ -236,10 +252,17 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.account_id  ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.mail_id ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.attachment_order), DBUS_TYPE_INVALID);\r
+                       if (data->download_attachment.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->download_attachment.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
+\r
                case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:\r
                        EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);\r
                        break;\r
+\r
                case ACTIVE_SYNC_NOTI_CANCEL_JOB:\r
                        EM_DEBUG_LOG("account_id:[%d]",       data->cancel_job.account_id );\r
                        EM_DEBUG_LOG("handle to cancel:[%d]", data->cancel_job.handle);\r
@@ -248,7 +271,13 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.account_id  ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.handle  ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.cancel_type  ), DBUS_TYPE_INVALID);\r
+                       if (data->cancel_job.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->cancel_job.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
+\r
                case ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER:\r
                        EM_DEBUG_LOG("account_id:[%d]",          data->search_mail_on_server.account_id );\r
                        EM_DEBUG_LOG("mailbox_id:[%d]",        data->search_mail_on_server.mailbox_id );\r
@@ -263,24 +292,31 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                                switch(data->search_mail_on_server.search_filter_list[i].search_filter_type) {\r
                                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_UID              :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_ALL              :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  :\r
                                                dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.integer_type_key_value), DBUS_TYPE_INVALID);\r
                                                break;\r
 \r
                                        case EMAIL_SEARCH_FILTER_TYPE_BCC              :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_BODY             :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_CC               :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_FROM             :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_TEXT             :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_TO               :\r
                                        case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :\r
+                                       case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :\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
@@ -295,11 +331,21 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                                }\r
                        }\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);\r
+                       if (data->search_mail_on_server.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER :\r
                        EM_DEBUG_LOG("account_id:[%d]",          data->clear_result_of_search_mail_on_server.account_id );\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);\r
+                       if (data->search_mail_on_server.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED :\r
@@ -307,18 +353,33 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.mailbox_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.on_server), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.handle), DBUS_TYPE_INVALID);\r
+                       if (data->expunge_mails_deleted_flagged.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->expunge_mails_deleted_flagged.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT :\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.account_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->get_resolve_recipients.email_address), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.handle), DBUS_TYPE_INVALID);\r
+                       if (data->get_resolve_recipients.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->get_resolve_recipients.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE :\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.account_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->validate_certificate.email_address), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.handle), DBUS_TYPE_INVALID);\r
+                       if (data->validate_certificate.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->validate_certificate.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_ADD_MAILBOX :\r
@@ -333,6 +394,11 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                                dbus_message_append_args (signal, DBUS_TYPE_ARRAY,  DBUS_TYPE_INT32, &(data->add_mailbox.eas_data),\\r
                                                                                                data->add_mailbox.eas_data_length,DBUS_TYPE_INVALID);\r
                        } */\r
+                       if (data->add_mailbox.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_RENAME_MAILBOX :\r
@@ -348,12 +414,22 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                                dbus_message_append_args (signal, DBUS_TYPE_ARRAY,  DBUS_TYPE_INT32, &(data->rename_mailbox.eas_data),\\r
                                                                                           data->rename_mailbox.eas_data_length, DBUS_TYPE_INVALID);\r
                        }*/\r
+                       if (data->rename_mailbox.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_DELETE_MAILBOX :\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.account_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.mailbox_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.handle), DBUS_TYPE_INVALID);\r
+                       if (data->delete_mailbox.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->delete_mailbox.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX :\r
@@ -362,12 +438,22 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        for(i = 0; i <data->delete_mailbox_ex.mailbox_id_count; i++)\r
                                dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.mailbox_id_array[i]), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox_ex.handle), DBUS_TYPE_INVALID);\r
+                       if (data->delete_mailbox_ex.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->delete_mailbox_ex.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL:\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.handle), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.mail_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.account_id), DBUS_TYPE_INVALID);\r
+                       if (data->send_mail_with_downloading_attachment_of_original_mail.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail_with_downloading_attachment_of_original_mail.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL:\r
@@ -375,12 +461,22 @@ INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNoti
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.account_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.mail_id), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.scheduled_time), DBUS_TYPE_INVALID);\r
+                       if (data->schedule_sending_mail.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->schedule_sending_mail.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                case ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL:\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.handle), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.account_id  ), DBUS_TYPE_INVALID);\r
                        dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.mail_id), DBUS_TYPE_INVALID);\r
+                       if (data->cancel_sending_mail.multi_user_name)\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->cancel_sending_mail.multi_user_name), DBUS_TYPE_INVALID);\r
+                       else\r
+                               dbus_message_append_args(signal, DBUS_TYPE_STRING, &(null_string), DBUS_TYPE_INVALID);\r
+\r
                        break;\r
 \r
                default:\r
index bb9103a..a32669a 100755 (executable)
@@ -37,6 +37,7 @@
 #include <openssl/buffer.h>
 #include <openssl/pem.h>
 #include <openssl/err.h>
+#include <openssl/ssl.h>
 
 #include "email-utilities.h"
 #include "email-core-global.h"
@@ -45,6 +46,7 @@
 #include "email-core-smtp.h"
 #include "email-storage.h"
 #include "email-core-smime.h"
+#include "email-core-pgp.h"
 #include "email-core-cert.h"
 #include "email-debug-log.h"
 
@@ -124,7 +126,7 @@ static const EVP_CIPHER *emcore_get_cipher_algorithm(email_cipher_type cipher_ty
        return cipher;
 }
 
-static int get_x509_stack_of_recipient_certs(char *recipients, STACK_OF(X509) **output_recipient_certs, int *err_code)
+static int get_x509_stack_of_recipient_certs(char *multi_user_name, char *recipients, STACK_OF(X509) **output_recipient_certs, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("recipients : [%s], STACK_OF(X509) : [%p]", recipients, output_recipient_certs);
 
@@ -168,10 +170,10 @@ static int get_x509_stack_of_recipient_certs(char *recipients, STACK_OF(X509) **
                        goto FINISH_OFF;                        
                }
 
-               EM_DEBUG_LOG("email_address_mailbox : [%s], email_address_host : [%s]", token_address->mailbox, token_address->host);
+               EM_DEBUG_LOG_SEC("email_address_mailbox : [%s], email_address_host : [%s]", token_address->mailbox, token_address->host);
 
                email_address = g_strdup_printf("<%s@%s>", token_address->mailbox, token_address->host);
-               if (!emstorage_get_certificate_by_email_address(email_address, &cert, false, 0, &err)) {
+               if (!emstorage_get_certificate_by_email_address(multi_user_name, email_address, &cert, false, 0, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_certificate_by_email_address failed : [%d]", err);
                        goto FINISH_OFF;
                }
@@ -319,9 +321,6 @@ INTERNAL_FUNC int emcore_smime_set_signed_message(char *certificate, char *mime_
        PKCS7 *signed_message = NULL;
        int flags = PKCS7_DETACHED | PKCS7_PARTIAL;
 
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-
        SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_SIGNED_FILE);
        EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_smime_filepath);
 
@@ -389,7 +388,6 @@ FINISH_OFF:
        BIO_free(bio_prikey);
        BIO_free_all(smime_attachment);
 
-       EVP_cleanup();
        if (err_code != NULL)
                *err_code = err;
 
@@ -397,7 +395,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *recipient_list, char *mime_entity, email_cipher_type cipher_type, char **file_path, int *err_code)
+INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *multi_user_name, char *recipient_list, char *mime_entity, email_cipher_type cipher_type, char **file_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("certificate path : [%p], mime_entity : [%p]", recipient_list, mime_entity);
        char temp_smime_filepath[512];
@@ -413,9 +411,6 @@ INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *recipient_list, char *m
        PKCS7 *encrypt_message = NULL;
        const EVP_CIPHER *cipher = NULL;
 
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-
        loaded_cert = cert_svc_cert_context_init();
 
        SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_ENCRYPT_FILE);
@@ -428,7 +423,7 @@ INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *recipient_list, char *m
                goto FINISH_OFF;
        }
 
-       if (!get_x509_stack_of_recipient_certs(recipient_list, &recipient_certs, &err)) {
+       if (!get_x509_stack_of_recipient_certs(multi_user_name, recipient_list, &recipient_certs, &err)) {
                EM_DEBUG_EXCEPTION("get_x509_stack_of_recipient_certs failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -472,7 +467,6 @@ FINISH_OFF:
        BIO_free_all(smime_attachment);
 
        cert_svc_cert_context_final(loaded_cert);
-       EVP_cleanup();
        if (err_code != NULL)
                *err_code = err;
 
@@ -480,7 +474,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *recipient_list, char *certificate, char *mime_entity, email_cipher_type cipher_type, email_digest_type digest_type, char **file_path, int *err_code)
+INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_name, char *recipient_list, char *certificate, char *mime_entity, email_cipher_type cipher_type, email_digest_type digest_type, char **file_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("certificate path : [%s], mime_entity : [%s]", recipient_list, mime_entity);
        char temp_smime_filepath[512];
@@ -502,9 +496,6 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *recipient_li
        EVP_PKEY *private_key = NULL;
        X509 *cert = NULL;
 
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-
        SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_ENCRYPT_FILE);
        EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_smime_filepath);
 
@@ -569,7 +560,7 @@ INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *recipient_li
 
        /* Encrypting the mail */
        /* 1. Get the recipient certs */
-       if (!get_x509_stack_of_recipient_certs(recipient_list, &recipient_certs, &err)) {
+       if (!get_x509_stack_of_recipient_certs(multi_user_name, recipient_list, &recipient_certs, &err)) {
                EM_DEBUG_EXCEPTION("get_x509_stack_of_recipient_certs failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -623,7 +614,6 @@ FINISH_OFF:
        BIO_free(bio_signed_message);
        BIO_free_all(smime_attachment);
 
-       EVP_cleanup();
        if (err_code != NULL)
                *err_code = err;
 
@@ -631,9 +621,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-
-INTERNAL_FUNC int emcore_smime_set_decrypt_message(char *encrypt_message, char *certificate, char **decrypt_message, int *err_code)
+INTERNAL_FUNC int emcore_smime_get_decrypt_message(char *encrypt_message, char *certificate, char **decrypt_message, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("encrypt_file : [%s], certificate : [%s]", encrypt_message, certificate);
        int ret = false;
@@ -646,8 +634,6 @@ INTERNAL_FUNC int emcore_smime_set_decrypt_message(char *encrypt_message, char *
        PKCS7 *p7_encrypt_message = NULL;
        STACK_OF(X509) *recipient_certs = NULL;
 
-       OpenSSL_add_all_algorithms();
-
        /* Load the encrypted message */
        infile = BIO_new_file(encrypt_message, INMODE);
        if (infile == NULL) {
@@ -693,7 +679,6 @@ FINISH_OFF:
        if (decrypt_message && ret)
                *decrypt_message = EM_SAFE_STRDUP(temp_decrypt_filepath);
 
-       EVP_cleanup();
        X509_free(cert);
        EVP_PKEY_free(private_key);
        BIO_free(out_buf);
@@ -718,9 +703,6 @@ INTERNAL_FUNC int emcore_smime_verify_signed_message(char *signed_message, char
        X509_LOOKUP *lookup = NULL;
        PKCS7 *p7 = NULL;
 
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-
        if (BIO_write(indata, signed_message, sizeof(signed_message)) <= 0) {
                EM_DEBUG_EXCEPTION("Char to Bio failed");
                goto FINISH_OFF;
@@ -786,16 +768,15 @@ FINISH_OFF:
                *verify = temp_verify;
 
        ERR_clear_error();
-       EVP_cleanup();
        return ret;
 }
 */
 
-INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t *account_tbl_item, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data_list, int *output_attachment_count)
+INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(char *multi_user_name, emstorage_account_tbl_t *account_tbl_item, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data_list, int *output_attachment_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list [%p], input_attachment_count [%d], output_mail_data [%p], output_attachment_data_list [%p]", input_mail_data, input_attachment_data_list, input_attachment_count, output_mail_data, output_attachment_data_list);
 
-       int i = 0, ret = false;
+       int i = 0;
        int err = EMAIL_ERROR_NONE;
        int smime_type = EMAIL_SMIME_NONE;
        int address_length = 0;
@@ -812,7 +793,7 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
                || !output_attachment_count || !output_mail_data || !output_attachment_data_list ) { /*prevent#53051*/
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
-               return ret;
+               return err;
        }
 
        smime_type = input_mail_data->smime_type;
@@ -844,7 +825,7 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
                
                SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);
 
-               if (!emcore_smime_set_encrypt_message(other_certificate_list, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, &smime_file_path, &err)) {
+               if (!emcore_smime_set_encrypt_message(multi_user_name, other_certificate_list, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, &smime_file_path, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_smime_set_encrypt_message is failed : [%d]", err);
                        goto FINISH_OFF;
                }
@@ -859,14 +840,14 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
                attachment_count = 1;
 
                break;
-       default:                        /* Signed and Encryption message */
+       case EMAIL_SMIME_SIGNED_AND_ENCRYPTED:                  /* Signed and Encryption message */
                address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);
 
                other_certificate_list = em_malloc(address_length + 4);
                
                SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);
 
-               if (!emcore_smime_set_signed_and_encrypt_message(other_certificate_list, account_tbl_item->certificate_path, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, account_tbl_item->digest_type, &smime_file_path, &err)) {
+               if (!emcore_smime_set_signed_and_encrypt_message(multi_user_name, other_certificate_list, account_tbl_item->certificate_path, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, account_tbl_item->digest_type, &smime_file_path, &err)) {
                        EM_DEBUG_EXCEPTION("em_core_smime_set_signed_and_encrypt_message is failed : [%d]", err);
                        goto FINISH_OFF;
                }
@@ -881,6 +862,67 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
                attachment_count = 1;
 
                break;
+       case EMAIL_PGP_SIGNED:
+               if ((err = emcore_pgp_set_signed_message(NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, account_tbl_item->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_core_smime_set_clear_signed_message is failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       
+               EM_DEBUG_LOG_SEC("smime_file_path : %s", smime_file_path);
+               name = strrchr(smime_file_path, '/');
+
+               new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
+               new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
+               new_attachment_data.attachment_mime_type = strdup("pgp-signature");
+
+               attachment_count += 1;
+
+               break;
+       case EMAIL_PGP_ENCRYPTED:
+               address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);
+
+               other_certificate_list = em_malloc(address_length + 4);
+               
+               SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);
+
+               if ((err = emcore_pgp_set_encrypted_message(other_certificate_list, NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, input_mail_data->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_set_encrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               name = strrchr(smime_file_path, '/');
+
+               new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
+               new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
+               new_attachment_data.attachment_mime_type = strdup("octet-stream");
+
+               attachment_count = 1;
+
+               break;
+       case EMAIL_PGP_SIGNED_AND_ENCRYPTED:
+               address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);
+
+               other_certificate_list = em_malloc(address_length + 4);
+               
+               SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);
+
+               if ((err = emcore_pgp_set_signed_and_encrypted_message(other_certificate_list, NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, input_mail_data->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_set_encrypted_message failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               name = strrchr(smime_file_path, '/');
+
+               new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
+               new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
+               new_attachment_data.attachment_mime_type = strdup("octet-stream");
+
+               attachment_count = 1;
+
+               break;
+       default:
+               EM_DEBUG_LOG("MIME none");
+               break;
        }
 
        if (!emcore_get_file_size(smime_file_path, &file_size, NULL)) {
@@ -894,6 +936,7 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
        new_attachment_list = (email_attachment_data_t *)em_malloc(sizeof(email_attachment_data_t) * attachment_count);
        if (new_attachment_list == NULL) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
@@ -902,6 +945,7 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
                        new_attachment_list[i].attachment_id         = input_attachment_data_list[i].attachment_id;
                        new_attachment_list[i].attachment_name       = EM_SAFE_STRDUP(input_attachment_data_list[i].attachment_name);
                        new_attachment_list[i].attachment_path       = EM_SAFE_STRDUP(input_attachment_data_list[i].attachment_path);
+                       new_attachment_list[i].content_id            = EM_SAFE_STRDUP(input_attachment_data_list[i].content_id);
                        new_attachment_list[i].attachment_size       = input_attachment_data_list[i].attachment_size;
                        new_attachment_list[i].mail_id               = input_attachment_data_list[i].mail_id;
                        new_attachment_list[i].account_id            = input_attachment_data_list[i].account_id;
@@ -916,6 +960,7 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
        new_attachment_list[attachment_count - 1].attachment_id         = new_attachment_data.attachment_id;
        new_attachment_list[attachment_count - 1].attachment_name       = EM_SAFE_STRDUP(new_attachment_data.attachment_name);
        new_attachment_list[attachment_count - 1].attachment_path       = EM_SAFE_STRDUP(new_attachment_data.attachment_path);
+       new_attachment_list[attachment_count - 1].content_id            = EM_SAFE_STRDUP(new_attachment_data.content_id);
        new_attachment_list[attachment_count - 1].attachment_size       = new_attachment_data.attachment_size;
        new_attachment_list[attachment_count - 1].mail_id               = new_attachment_data.mail_id;
        new_attachment_list[attachment_count - 1].account_id            = new_attachment_data.account_id;
@@ -928,11 +973,10 @@ INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t
        input_mail_data->smime_type = smime_type;
        input_mail_data->digest_type = account_tbl_item->digest_type;
 
-       ret = true;
-
 FINISH_OFF:    
 
        EM_SAFE_FREE(other_certificate_list);
+       EM_SAFE_FREE(smime_file_path);
 
        *output_attachment_count = attachment_count;
        
@@ -940,7 +984,27 @@ FINISH_OFF:
 
        *output_mail_data = input_mail_data;
 
-       return ret;                             
+    EM_SAFE_FREE(new_attachment_data.attachment_name);
+    EM_SAFE_FREE(new_attachment_data.attachment_path);
+    EM_SAFE_FREE(new_attachment_data.attachment_mime_type);
+    EM_SAFE_FREE(new_attachment_data.content_id);
+
+    EM_DEBUG_LOG("err : [%d]", err);
+       return err;                             
 }
 
+INTERNAL_FUNC void emcore_init_openssl_library()
+{
+        EM_DEBUG_FUNC_BEGIN();
+        SSL_library_init();
+        ERR_load_crypto_strings();
+        EM_DEBUG_FUNC_END();
+}
 
+INTERNAL_FUNC void emcore_clean_openssl_library()
+{
+        EM_DEBUG_FUNC_BEGIN();
+        ERR_free_strings();
+        EVP_cleanup();
+        EM_DEBUG_FUNC_END();
+}
index 7414588..b38003b 100755 (executable)
@@ -32,6 +32,7 @@
 #include <alarm.h>
 #include <dlfcn.h>
 #include <ctype.h>
+#include <sys/shm.h>
 
 #include "email-internal-types.h"
 #include "c-client.h"
 #include "email-utilities.h"
 #include "email-convert.h"
 #include "email-debug-log.h"
+#include "email-core-gmime.h"
+#include "email-core-container.h"
 
 #undef min
 
 
 
 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
-static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code);
+static int emcore_get_report_mail_body(char *multi_user_name, ENVELOPE *envelope, BODY **multipart_body, int *err_code);
 #endif
-static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope);
-static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id,  int *err_code);
+static int emcore_make_envelope_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope);
+static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id,  int *err_code);
 char *emcore_generate_content_id_string(const char *hostname, int *err);
 
 /* Functions from uw-imap-toolkit */
@@ -162,8 +165,7 @@ static char *emcore_replace_inline_image_path_with_content_id(char *source_strin
 
        cur_body = root_body;
        if (root_body->type != TYPEMULTIPART) {
-               EM_DEBUG_EXCEPTION("Invalid related type : type[%d]", root_body->type);
-               err = EMAIL_ERROR_INVALID_DATA;
+               EM_DEBUG_LOG("The body is not multipart : type[%d]", root_body->type);
                goto FINISH_OFF;
        }
 
@@ -197,7 +199,7 @@ static char *emcore_replace_inline_image_path_with_content_id(char *source_strin
                                if (result_string) {
                                        EM_SAFE_FREE(input_string);
                                        input_string = result_string;
-                                       result_string = NULL;
+                                       result_string = NULL; /* prevent 34868 */
                                }
 
                                cur_part = cur_part->next;
@@ -206,7 +208,7 @@ static char *emcore_replace_inline_image_path_with_content_id(char *source_strin
 
                if (cur_part)
                        cur_body = &(cur_part->body);
-               else 
+               else
                        cur_body = NULL;
 
        } while (cur_body);
@@ -247,7 +249,6 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
                EM_DEBUG_LOG("There is no file path");
                switch (body->encoding) {
                        case 0:
-                               break;
                        default:
                                p = cpystr((const char *)body->contents.text.data);
                                len = body->contents.text.size;
@@ -278,9 +279,9 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
                        goto FINISH_OFF;
                }
 
-               if (!(fp_html = fopen (file_path, "r"))) {
-                       EM_DEBUG_EXCEPTION("fopen [%s] error [%d] ", file_path, errno);
-                       error = EMAIL_ERROR_FILE ;
+               error = em_fopen(file_path, "r", &fp_html);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_fopen [%s] error [%d] ", file_path, error);
                        goto FINISH_OFF;
                }
 
@@ -326,10 +327,9 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
                                        }
                                        EM_DEBUG_LOG_DEV("tmp file path : %s", tmp_file_path);
 
-                                       fp_write = fopen (tmp_file_path, "w");
-                                       if (fp_write == NULL) {
-                                               EM_DEBUG_EXCEPTION_SEC ("fopen [%s] error [%d]", tmp_file_path, errno);
-                                               error = EMAIL_ERROR_FILE ;
+                                       error = em_fopen(tmp_file_path, "w", &fp_write);
+                                       if (error != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION_SEC ("em_fopen [%s] error [%d]", tmp_file_path, error);
                                                goto FINISH_OFF;
                                        }
 
@@ -344,7 +344,7 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
                                                error = EMAIL_ERROR_FILE;
                                                goto FINISH_OFF;
                                        }
-                       
+
                                        file_path = tmp_file_path;
                                        fclose(fp_write);
                                        fp_write = NULL;
@@ -355,10 +355,9 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
 
        EM_DEBUG_LOG_DEV("Opening a file[%s]", file_path);
 
-       fd = open (file_path, O_RDONLY);
-       if (fd < 0) {
-               EM_DEBUG_EXCEPTION_SEC("open[%s] error [%d]", file_path, errno);
-               error = EMAIL_ERROR_OUT_OF_MEMORY;
+       error = em_open(file_path, O_RDONLY, 0, &fd);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("open[%s] error [%d]", file_path, error);
                goto FINISH_OFF;
        }
 
@@ -374,7 +373,7 @@ static int emcore_write_body (BODY *body, BODY *root_body, FILE *fp)
                        EM_DEBUG_EXCEPTION ("read [%s] error [%d]", file_path, errno);
                        error = EMAIL_ERROR_FILE;
                        break;
-               } 
+               }
 
                len = nread;
 
@@ -408,13 +407,14 @@ FINISH_OFF: /* prevent 34226 */
 
        /* cleanup local vars */
        EM_SAFE_FREE (body->sparep);
-       close(fd);
+       EM_SAFE_CLOSE (fd); /*prevent 34498*/
        EM_SAFE_FREE (p);
-       if (tmp_file_path) 
+       if (tmp_file_path)
                g_remove(tmp_file_path);
-       if (fp_html) 
+       EM_SAFE_FREE(tmp_file_path);
+       if (fp_html)
                fclose (fp_html);
-       if (fp_write) 
+       if (fp_write)
                fclose (fp_write);
        EM_SAFE_FREE(full_buf);
        EM_SAFE_FREE (replaced_string);
@@ -466,7 +466,7 @@ static int emcore_write_rfc822_body(BODY *body, BODY *root_body, FILE *fp, int *
                        fprintf(fp, "--%s--"CRLF_STRING, bndry);
                        break;
 
-               default: 
+               default:
                        EM_DEBUG_LOG_DEV("body->type is not TYPEMULTIPART");
 
                        if ((error = emcore_write_body (body, root_body, fp)) != EMAIL_ERROR_NONE) {
@@ -480,51 +480,68 @@ static int emcore_write_rfc822_body(BODY *body, BODY *root_body, FILE *fp, int *
        return true;
 }
 
-static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t input_priority, 
+static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t input_priority,
                                                      email_mail_report_t input_report_flag, char **data)
 {
        EM_DEBUG_FUNC_BEGIN("env[%p], body[%p], data[%p]", env, body, data);
 
        int error = EMAIL_ERROR_NONE;
-
        FILE *fp = NULL;
        char *fname = NULL;
-       char *p = NULL;
-       size_t p_len = 0;
+       int file_exist = 0;
+       char  *header_buffer = NULL;
+       size_t header_buffer_lenth = 0;
+       RFC822BUFFER buf;
+       int  address_count = 0;
+       ADDRESS *index = NULL;
 
        if (!env || !data)  {
                EM_DEBUG_EXCEPTION("Invalid Parameters");
                error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
-       
+
        srand(time(NULL));
 
        rfc822_encode_body_7bit(env, body); /*  if contents.text.data isn't NULL, the data will be encoded. */
 
-       /*  FIXME : create memory map for this file */
-       p_len = (env->subject ? EM_SAFE_STRLEN(env->subject) : 0) + 8192;
+       index = env->to;
+       while(index) {
+               address_count++;
+               index = index->next;
+       }
+
+       index = env->cc;
+       while(index) {
+               address_count++;
+               index = index->next;
+       }
+
+       header_buffer_lenth = (env->subject ? EM_SAFE_STRLEN(env->subject) : 0);
+       header_buffer_lenth += address_count * MAX_EMAIL_ADDRESS_LENGTH;
+       header_buffer_lenth += 8192;
 
-       if (!(p = em_malloc(p_len)))  {         /* (env->subject ? EM_SAFE_STRLEN(env->subject) : 0) + 8192))) */
+       EM_DEBUG_LOG("header_buffer_lenth [%d]", header_buffer_lenth);
+
+       if (!(header_buffer = em_malloc(header_buffer_lenth)))  {
                EM_DEBUG_EXCEPTION(" malloc failed...");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
 
-       RFC822BUFFER buf;
-
        /* write at start of buffer */
-       buf.end = (buf.beg = buf.cur = p) + p_len - 1;
-       /* buf.f = NIL; */
-       buf.f = buf_flush;
-       buf.s = NIL;
+       buf.beg = buf.cur = header_buffer;
+       buf.end = header_buffer + header_buffer_lenth - 1;
+       buf.f   = buf_flush;
+       buf.s   = NULL;
 
        /*  rfc822_output_header(&buf, env, body, NIL, T); */           /*  including BCC  */
        rfc822_output_header(&buf, env, body, NIL, NIL);                /*  Excluding BCC */
 
-       *buf.cur = '\0';                /* tie off buffer */
+       EM_DEBUG_LOG("header_buffer [%d]", strlen(header_buffer));
+
        {
-               gchar **tokens = g_strsplit(p, "CHARSET=X-UNKNOWN", 2);
+               gchar **tokens = g_strsplit(header_buffer, "CHARSET=X-UNKNOWN", 2);
 
                if (g_strv_length(tokens) > 1)  {
                        gchar *charset;
@@ -537,7 +554,7 @@ static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t
                                                charset[pHtml-charset] = '\0';
                                }
 
-                               SNPRINTF(p, p_len, "%sCHARSET=%s%s", tokens[0], charset, tokens[1]);
+                               SNPRINTF(header_buffer, header_buffer_lenth, "%sCHARSET=%s%s", tokens[0], charset, tokens[1]);
                                g_free(charset);
                        }
                        else
@@ -548,21 +565,21 @@ static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t
        }
 
        {
-               gchar **tokens = g_strsplit(p, "To: undisclosed recipients: ;\015\012", 2);
+               gchar **tokens = g_strsplit(header_buffer, "To: undisclosed recipients: ;\015\012", 2);
                if (g_strv_length(tokens) > 1)
-                       SNPRINTF(p, p_len, "%s%s", tokens[0], tokens[1]);
+                       SNPRINTF(header_buffer, header_buffer_lenth, "%s%s", tokens[0], tokens[1]);
                g_strfreev(tokens);
        }
 
        EM_DEBUG_LOG_DEV(" =============================================================================== "
                LF_STRING"%s"LF_STRING
-               " =============================================================================== ", p);
+               " =============================================================================== ", header_buffer);
 
-       if (EM_SAFE_STRLEN(p) > 2)
-               *(p + EM_SAFE_STRLEN(p) - 2) = '\0';
+       if (EM_SAFE_STRLEN(header_buffer) > 2)
+               *(header_buffer + EM_SAFE_STRLEN(header_buffer) - 2) = '\0';
 
        if (input_report_flag)  {
-               char buf[512] = {0x00, };
+               char string_buf[512] = {0x00, };
 
                if(input_report_flag & EMAIL_MAIL_REPORT_DSN) {
                        /*  DSN (delivery status) */
@@ -581,42 +598,48 @@ static int emcore_write_rfc822 (ENVELOPE *env, BODY *body, email_mail_priority_t
 
                if(input_report_flag & EMAIL_MAIL_REQUEST_MDN) {
                        /*  require read status */
-                       rfc822_address(buf, env->from);
-                       if (EM_SAFE_STRLEN(buf))
-                               SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "Disposition-Notification-To: %s"CRLF_STRING, buf);
+                       rfc822_address(string_buf, env->from);
+                       if (EM_SAFE_STRLEN(string_buf))
+                               SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth - EM_SAFE_STRLEN(header_buffer), "Disposition-Notification-To: %s"CRLF_STRING, string_buf);
                }
        }
 
        if (input_priority)  {          /*  priority (1:high 3:normal 5:low) */
-               SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-Priority: %d"CRLF_STRING, input_priority);
+               SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-Priority: %d"CRLF_STRING, input_priority);
 
                switch (input_priority)  {
                        case EMAIL_MAIL_PRIORITY_HIGH:
-                               SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: HIgh"CRLF_STRING);
+                               SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: High"CRLF_STRING);
                                break;
                        case EMAIL_MAIL_PRIORITY_NORMAL:
-                               SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: Normal"CRLF_STRING);
+                               SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: Normal"CRLF_STRING);
                                break;
                        case EMAIL_MAIL_PRIORITY_LOW:
-                               SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), "X-MSMail-Priority: Low"CRLF_STRING);
+                               SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), "X-MSMail-Priority: Low"CRLF_STRING);
                                break;
                }
        }
 
-       SNPRINTF(p + EM_SAFE_STRLEN(p), p_len-(EM_SAFE_STRLEN(p)), CRLF_STRING);
+       SNPRINTF(header_buffer + EM_SAFE_STRLEN(header_buffer), header_buffer_lenth-(EM_SAFE_STRLEN(header_buffer)), CRLF_STRING);
 
-       if (!emcore_get_temp_file_name(&fname, &error))  {
-               EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed[%d]", error);
-               goto FINISH_OFF;
+       if (data && EM_SAFE_STRLEN(*data) > 0) {
+               fname = EM_SAFE_STRDUP(*data);
+               file_exist = 1;
+       }
+       else {
+               if (!emcore_get_temp_file_name(&fname, &error))  {
+                       EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed[%d]", error);
+                       goto FINISH_OFF;
+               }
        }
 
-       if (!(fp = fopen(fname, "w+")))  {
-               EM_DEBUG_EXCEPTION("fopen failed[%s]", fname);
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
+       error = em_fopen(fname, "w+", &fp);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed[%s] [%d]", fname, error);
                goto FINISH_OFF;
        }
 
-       fprintf(fp, "%s", p);
+       fprintf(fp, "%s", header_buffer);
 
        if (body)  {
                if (!emcore_write_rfc822_body(body, body, fp, &error))  {
@@ -633,33 +656,56 @@ FINISH_OFF:
        emstorage_copy_file(fname, "/tmp/phone2pc.eml", false, NULL);
 #endif
 
-       if (error == EMAIL_ERROR_NONE)
-               *data = fname;
+       if (error == EMAIL_ERROR_NONE) {
+               if (!file_exist)
+                       *data = EM_SAFE_STRDUP(fname);
+       }
        else if (fname != NULL) {
                remove(fname);
-               EM_SAFE_FREE(fname);
        }
 
-       EM_SAFE_FREE(p);
+       EM_SAFE_FREE(fname);
+       EM_SAFE_FREE(header_buffer);
 
        EM_DEBUG_FUNC_END();
        return error;
 }
 
-INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas, int move_flag)
+INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas, int move_flag)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list [%p], input_attachment_count [%d], input_meeting_request [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
 
+       /* IF an account is being deleted, there is no need to add the mail of the account */
+       int shmid;
+       extern key_t del_account_key; /* in emcore_delete_account */
+       int *del_account_id = NULL;
+       /* get the segment.*/
+
+       if ((shmid = shmget (del_account_key, sizeof (int), 0666)) != -1) {
+               /* attach the segment to current process space */
+               if ((del_account_id = (int*) shmat (shmid, NULL, 0)) != (int*) -1) {
+                       /* compare two account ids */
+                       EM_DEBUG_LOG ("del_id[%d] account_id[%d]",*del_account_id, input_mail_data->account_id);
+                       if (*del_account_id == input_mail_data->account_id) {
+                               EM_DEBUG_LOG ("SKIP adding mail: the account is being deleted");
+                               return EMAIL_ERROR_ACCOUNT_NOT_FOUND;
+                       }
+               }
+       }
+       EM_DEBUG_LOG ("read del_id, account_id [%d]", (del_account_id? *del_account_id:-100),input_mail_data->account_id);
+
+
        int err = EMAIL_ERROR_NONE;
        int attachment_id = 0, thread_id = -1, thread_item_count = 0, latest_mail_id_in_thread = -1;
        int i = 0, rule_len, priority_sender = 0, blocked = 0, local_attachment_count = 0, local_inline_content_count = 0;
        int mailbox_id_spam = 0, mailbox_id_target = 0;
        int mail_smime_flag = 0;
        char name_buf[MAX_PATH] = {0x00, };
+    char path_buf[MAX_PATH] = {0, };
        char *body_text_file_name = NULL;
        char *dl_error = NULL;
        void *dl_handle = NULL;
-       int (*convert_mail_data_to_smime)(emstorage_account_tbl_t*, email_mail_data_t*, email_attachment_data_t*, int, email_mail_data_t**, email_attachment_data_t**, int*);
+       int (*convert_mail_data_to_smime)(char*, emstorage_account_tbl_t*, email_mail_data_t*, email_attachment_data_t*, int, email_mail_data_t**, email_attachment_data_t**, int*);
 
        int attachment_count = 0;
        email_mail_data_t *mail_data = NULL;
@@ -672,6 +718,9 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        struct stat st_buf = { 0 };
        char mailbox_id_param_string[10] = {0,};
        char errno_buf[ERRNO_BUF_SIZE] = {0};
+       int updated_thread_id = 0;
+    char *prefix_path = NULL;
+    char real_file_path[MAX_PATH] = {0};
 
 #ifdef __FEATURE_BODY_SEARCH__
        char *stripped_text = NULL;
@@ -684,12 +733,12 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                goto FINISH_OFF;
        }
 
-       if (emcore_is_storage_full(&err) == true) {
+       if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
                EM_DEBUG_EXCEPTION("Storage is full");
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(input_mail_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_item, true, &err)) {
+       if (!emstorage_get_account_by_id(multi_user_name, input_mail_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_item, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed. account_id[%d] err[%d]", input_mail_data->account_id, err);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -702,7 +751,7 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                        err = EMAIL_ERROR_INVALID_PATH;
                        goto FINISH_OFF;
                }
-               
+
                dlerror();
                convert_mail_data_to_smime = dlsym(dl_handle, "emcore_convert_mail_data_to_smime_data");
                if ((dl_error = dlerror()) != NULL) {
@@ -711,11 +760,11 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                        goto FINISH_OFF;
                }
 
-               if (!convert_mail_data_to_smime(account_tbl_item, input_mail_data, input_attachment_data_list, input_attachment_count, &mail_data, &attachment_data_list, &attachment_count)) {
+               if ((err = convert_mail_data_to_smime(multi_user_name, account_tbl_item, input_mail_data, input_attachment_data_list, input_attachment_count, &mail_data, &attachment_data_list, &attachment_count)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("S/MIME failed");
                        goto FINISH_OFF;
                }
-               
+
                mail_smime_flag = 1;
        } else {
                mail_data = input_mail_data;
@@ -725,11 +774,24 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
 
        mailbox_id_target = mail_data->mailbox_id;
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        if (input_from_eas == 0 &&
                        !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_REPLY) &&
                        !(input_mail_data->message_class & EMAIL_MESSAGE_CLASS_SMART_FORWARD) ) {
                if (mail_data->file_path_plain)  {
-                       if (stat(mail_data->file_path_plain, &st_buf) < 0)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_plain);
+
+                       if (stat(real_file_path, &st_buf) < 0)  {
                                EM_DEBUG_EXCEPTION_SEC("mail_data->file_path_plain, stat(\"%s\") failed...", mail_data->file_path_plain);
                                EM_DEBUG_EXCEPTION("%s", EM_STRERROR(errno_buf));
                                err = EMAIL_ERROR_INVALID_MAIL;
@@ -738,7 +800,10 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                }
 
                if (mail_data->file_path_html)  {
-                       if (stat(mail_data->file_path_html, &st_buf) < 0)  {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_html);
+
+                       if (stat(real_file_path, &st_buf) < 0)  {
                                EM_DEBUG_EXCEPTION_SEC("mail_data->file_path_html, stat(\"%s\") failed...", mail_data->file_path_html);
                                EM_DEBUG_EXCEPTION("%s", EM_STRERROR(errno_buf) );
                                err = EMAIL_ERROR_INVALID_MAIL;
@@ -749,7 +814,10 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                if (attachment_count && attachment_data_list)  {
                        for (i = 0; i < attachment_count; i++)  {
                                if (attachment_data_list[i].save_status) {
-                                       if (!attachment_data_list[i].attachment_path || stat(attachment_data_list[i].attachment_path, &st_buf) < 0)  {
+                    memset(real_file_path, 0x00, sizeof(real_file_path));
+                    SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment_data_list[i].attachment_path);
+
+                                       if (!attachment_data_list[i].attachment_path || stat(real_file_path, &st_buf) < 0)  {
                                                EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", attachment_data_list[i].attachment_path);
                                                err = EMAIL_ERROR_INVALID_ATTACHMENT;
                                                goto FINISH_OFF;
@@ -762,11 +830,11 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                        input_mail_data->full_address_from = EM_SAFE_STRDUP(account_tbl_item->user_email_address);
 
                /* check for email_address validation */
-               if ((err = em_verify_email_address_of_mail_data (mail_data, false)) != EMAIL_ERROR_NONE) {
+               if ((err = em_verify_email_address_of_mail_data (mail_data)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_data failed [%d]", err);
                        goto FINISH_OFF;
                }
-               
+
                if (mail_data->report_status & EMAIL_MAIL_REPORT_MDN)  {
                        /* check read-report mail */
                        if(!mail_data->full_address_to) { /* A report mail should have 'to' address */
@@ -776,7 +844,7 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                        }
                        /* Create report mail body */
                        /*
-                       if (!emcore_make_rfc822_file_from_mail(mail_src, NULL, NULL, NULL, &err))  {
+                       if (!emcore_make_rfc822_file_from_mail(multi_user_name, mail_src, NULL, NULL, NULL, &err))  {
                                EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
                                goto FINISH_OFF;
                        }
@@ -784,49 +852,50 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                }
        }
        else {  /*  For Spam handling */
-               if (mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_OUTBOX) {
-                       email_option_t *opt = &account_tbl_item->options;
-                       EM_DEBUG_LOG("block_address [%d], block_subject [%d]", opt->block_address, opt->block_subject);
-
-                       if (opt->block_address || opt->block_subject)  {
-                               int is_completed = false;
-                               int type = 0;
-
-                               if (!opt->block_address)
-                                       type = EMAIL_FILTER_SUBJECT;
-                               else if (!opt->block_subject)
-                                       type = EMAIL_FILTER_FROM;
-
-                               if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
-                                       EM_DEBUG_LOG("No proper rules. emstorage_get_rule returns [%d]", err);
-                       
-                               if (rule && !emcore_check_rule(mail_data->full_address_from, mail_data->subject, rule, rule_len, &priority_sender, &blocked, &err)) {
-                                       EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
-                               }
-                       
-                               if (priority_sender)
-                                       mail_data->tag_id = PRIORITY_SENDER_TAG_ID;
-               
-                               if (blocked) {
-                                       EM_DEBUG_LOG("mail[%d] added to spambox", mail_data->mail_id);
-                                       if (!emstorage_get_mailbox_id_by_mailbox_type(mail_data->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_id_spam, false, &err))  {
-                                               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
-                                               mailbox_id_spam = 0;
-                                       }
+               email_option_t *opt = &account_tbl_item->options;
+               EM_DEBUG_LOG_SEC("block_address [%d], block_subject [%d]", opt->block_address, opt->block_subject);
 
-                                       if (mailbox_id_spam)
-                                               mailbox_id_target = mailbox_id_spam;
-                               }
-                       }
-               } 
+        /* For eas moving from spambox to other mailbox */
+        if (mail_data->save_status != EMAIL_MAIL_STATUS_SAVED) {
+            if (opt->block_address || opt->block_subject)  {
+                int is_completed = false;
+                int type = 0;
+
+                if (!opt->block_address)
+                    type = EMAIL_FILTER_SUBJECT;
+                else if (!opt->block_subject)
+                    type = EMAIL_FILTER_FROM;
+
+                if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
+                    EM_DEBUG_LOG("No proper rules. emstorage_get_rule returns [%d]", err);
+
+                if (rule && !emcore_check_rule(mail_data->full_address_from, mail_data->subject, rule, rule_len, &priority_sender, &blocked, &err)) {
+                    EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
+                }
+
+                if (priority_sender)
+                    mail_data->tag_id = PRIORITY_SENDER_TAG_ID;
+
+                if (blocked && (mail_data->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)) {
+                    EM_DEBUG_LOG("mail[%d] added to spambox", mail_data->mail_id);
+                    if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail_data->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_id_spam, false, &err))  {
+                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
+                        mailbox_id_spam = 0;
+                    }
+
+                    if (mailbox_id_spam)
+                        mailbox_id_target = mailbox_id_spam;
+                }
+            }
+        }
        }
 
-       if ((err = emstorage_get_mailbox_by_id(mailbox_id_target, (emstorage_mailbox_tbl_t**)&mailbox_tbl)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id_target, (emstorage_mailbox_tbl_t**)&mailbox_tbl)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_increase_mail_id(&mail_data->mail_id, true, &err)) {
+       if (!emstorage_increase_mail_id(multi_user_name, &mail_data->mail_id, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -834,21 +903,26 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        EM_DEBUG_LOG("mail_data->mail_size [%d]", mail_data->mail_size);
 
        if(mail_data->mail_size == 0)
-               emcore_calc_mail_size(mail_data, attachment_data_list, attachment_count, &(mail_data->mail_size)); /*  Getting file size before file moved.  */
+               emcore_calc_mail_size(multi_user_name, mail_data, attachment_data_list, attachment_count, &(mail_data->mail_size)); /*  Getting file size before file moved.  */
 
        EM_DEBUG_LOG("input_from_eas [%d] mail_data->body_download_status [%d]", input_from_eas, mail_data->body_download_status);
 
        if (input_from_eas == 0|| mail_data->body_download_status) {
-               if (!emstorage_create_dir(mail_data->account_id, mail_data->mail_id, 0, &err))  {
+               if (!emstorage_create_dir(multi_user_name, mail_data->account_id, mail_data->mail_id, 0, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                if (mail_data->file_path_plain) {
+            memset(name_buf, 0x00, sizeof(name_buf));
+            memset(path_buf, 0x00, sizeof(path_buf));
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_plain);
+
                        EM_DEBUG_LOG_SEC("mail_data->file_path_plain [%s]", mail_data->file_path_plain);
                        /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH); */
 
-                       if ( (err = em_get_file_name_from_file_path(mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
+                       if ( (err = em_get_file_name_from_file_path(real_file_path, &body_text_file_name)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
                                err = EMAIL_ERROR_INVALID_FILE_PATH;
                                goto FINISH_OFF;
@@ -861,29 +935,34 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                                EM_SAFE_STRNCPY(body_text_file_name, "UTF-8", MAX_PATH);
                        */
 
-                       if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, &err))  {
+                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, path_buf, sizeof(path_buf), &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (!emstorage_copy_file(mail_data->file_path_plain, name_buf, input_from_eas, &err)) {
-                               EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
+                       if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                                goto FINISH_OFF;
                        }
-                       if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
+                       if (!mail_data->body_download_status)
                                mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
 
                        EM_SAFE_FREE(mail_data->file_path_plain);
-                       mail_data->file_path_plain = EM_SAFE_STRDUP(name_buf);
+                       mail_data->file_path_plain = EM_SAFE_STRDUP(path_buf);
                }
 
                if (mail_data->file_path_html) {
+            memset(name_buf, 0x00, sizeof(name_buf));
+            memset(path_buf, 0x00, sizeof(path_buf));
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_html);
+
                        EM_DEBUG_LOG_SEC("mail_data->file_path_html [%s]", mail_data->file_path_html);
                        /* EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH); */
 
                        EM_SAFE_FREE(body_text_file_name);
 
-                       if ( (err = em_get_file_name_from_file_path(mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
+                       if ( (err = em_get_file_name_from_file_path(real_file_path, &body_text_file_name)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
                                err = EMAIL_ERROR_INVALID_FILE_PATH;
                                goto FINISH_OFF;
@@ -895,39 +974,44 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                                EM_SAFE_STRNCPY(body_text_file_name, "UTF-8.htm", MAX_PATH);
                        */
 
-                       if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, &err))  {
+                       if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 0, body_text_file_name, name_buf, path_buf, sizeof(path_buf), &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (!emstorage_copy_file(mail_data->file_path_html, name_buf, input_from_eas, &err))  {
-                               EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
+                       if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err))  {
+                               EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
-                       if (mail_data->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_NONE)
+                       if (!mail_data->body_download_status)
                                mail_data->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
 
                        EM_SAFE_FREE(mail_data->file_path_html);
-                       mail_data->file_path_html = EM_SAFE_STRDUP(name_buf);
+                       mail_data->file_path_html = EM_SAFE_STRDUP(path_buf);
                }
        }
 
        if (mail_data->file_path_mime_entity) {
+        memset(name_buf, 0x00, sizeof(name_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, mail_data->file_path_mime_entity);
+
                EM_DEBUG_LOG_SEC("mail_data->file_path_mime_entity [%s]", mail_data->file_path_mime_entity);
 
-               if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, 0, "mime_entity", name_buf, &err)) {
+               if (!emstorage_get_save_name(multi_user_name, mail_data->account_id, mail_data->mail_id, 0, "mime_entity", name_buf, path_buf, sizeof(path_buf), &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_copy_file(mail_data->file_path_mime_entity, name_buf, input_from_eas, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
+               if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                EM_SAFE_FREE(mail_data->file_path_mime_entity);
-               mail_data->file_path_mime_entity = EM_SAFE_STRDUP(name_buf);
+               mail_data->file_path_mime_entity = EM_SAFE_STRDUP(path_buf);
        }
 
        if (!mail_data->date_time)  {
@@ -937,7 +1021,7 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
 
        /* Generate message_id */
        if (!input_from_eas) {
-               mail_data->message_id     = strdup(emcore_generate_content_id_string("com.samsung.slp.email", NULL));
+               mail_data->message_id     = emcore_generate_content_id_string("com.samsung.slp.email", NULL);
                mail_data->server_mail_id = strdup("0");
        }
 
@@ -963,7 +1047,11 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
 
        EM_DEBUG_LOG("preview_text[%p]", mail_data->preview_text);
        if (mail_data->preview_text == NULL) {
-               if ( (err = emcore_get_preview_text_from_file(mail_data->file_path_plain, mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail_data->preview_text))) != EMAIL_ERROR_NONE) {
+               if ( (err = emcore_get_preview_text_from_file(multi_user_name, 
+                                                    mail_data->file_path_plain, 
+                                                    mail_data->file_path_html, 
+                                                    MAX_PREVIEW_TEXT_LENGTH, 
+                                                    &(mail_data->preview_text))) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
 
                        if (err != EMAIL_ERROR_EMPTY_FILE)
@@ -979,11 +1067,11 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        converted_mail_tbl->mailbox_id        = mailbox_tbl->mailbox_id;
 
        /* Fill address information */
-       emcore_fill_address_information_of_mail_tbl(converted_mail_tbl);
+       emcore_fill_address_information_of_mail_tbl(multi_user_name, converted_mail_tbl);
 
        /* Fill thread id */
        if(mail_data->thread_id == 0) {
-               if (emstorage_get_thread_id_of_thread_mails(converted_mail_tbl, &thread_id, &latest_mail_id_in_thread, &thread_item_count) != EMAIL_ERROR_NONE)
+               if (emstorage_get_thread_id_of_thread_mails(multi_user_name, converted_mail_tbl, &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 (thread_id == -1) {
@@ -1000,15 +1088,14 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        }
 
        mail_data->thread_id = converted_mail_tbl->thread_id;
-
-       emstorage_begin_transaction(NULL, NULL, NULL);
+       converted_mail_tbl->user_name = EM_SAFE_STRDUP(account_tbl_item->user_name);
+       emstorage_begin_transaction(multi_user_name, NULL, NULL, NULL);
 
        /*  insert mail to mail table */
-       if (!emstorage_add_mail(converted_mail_tbl, 0, false, &err))  {
+       if (!emstorage_add_mail(multi_user_name, converted_mail_tbl, 0, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
                /*  ROLLBACK TRANSACTION; */
-               emstorage_rollback_transaction(NULL, NULL, NULL);
-
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 
@@ -1016,54 +1103,77 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        EM_DEBUG_LOG("thread_item_count [%d]", thread_item_count);
 
        if (thread_item_count > 1) {
-               if (!emstorage_update_latest_thread_mail(mail_data->account_id, converted_mail_tbl->thread_id, 0, 0, false, &err)) {
+               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)) {
                        EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
-                       emstorage_rollback_transaction(NULL, NULL, NULL);
-
+                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
+
+               if (updated_thread_id > 0)
+                       input_mail_data->thread_id = updated_thread_id;
        }
 
        /*  Insert attachment information to DB */
 
        for (i = 0; i < attachment_count; i++) {
+        memset(name_buf, 0x00, sizeof(name_buf));
+        memset(path_buf, 0x00, sizeof(path_buf));
+
+        /* set attachment size */
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment_data_list[i].attachment_path);
+
                if (attachment_data_list[i].attachment_size == 0) {
-                       /* set attachment size */
-                       if(attachment_data_list[i].attachment_path && stat(attachment_data_list[i].attachment_path, &st_buf) < 0)
+                       if(attachment_data_list[i].attachment_path && stat(real_file_path, &st_buf) < 0)
                                attachment_data_list[i].attachment_size = st_buf.st_size;
                }
 
                if (!attachment_data_list[i].inline_content_status) {
-                       if (!emstorage_get_new_attachment_no(&attachment_id, &err)) {
+                       if (!emstorage_get_new_attachment_no(multi_user_name, &attachment_id, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_new_attachment_no failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
+                               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                                goto FINISH_OFF;
                        }
                }
 
-               if (!emstorage_create_dir(mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, &err))  {
+               if (!emstorage_create_dir(multi_user_name, mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
-                       emstorage_rollback_transaction(NULL, NULL, NULL);
+                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_save_name(mail_data->account_id, mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, attachment_data_list[i].attachment_name, name_buf, &err))  {
+               if (!emstorage_get_save_name(multi_user_name, 
+                                        mail_data->account_id, 
+                                        mail_data->mail_id, attachment_data_list[i].inline_content_status ? 0  :  attachment_id, 
+                                        attachment_data_list[i].attachment_name, 
+                                        name_buf, 
+                                        path_buf,
+                                        sizeof(path_buf), 
+                                        &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
-                       emstorage_rollback_transaction(NULL, NULL, NULL);
+                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
                /* if (input_from_eas == 0 || attachment_data_list[i].save_status) { */
                if (attachment_data_list[i].save_status) {
-                       if (!emstorage_copy_file(attachment_data_list[i].attachment_path, name_buf, input_from_eas, &err)) {
-                               EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
-                               emstorage_rollback_transaction(NULL, NULL, NULL);
-                               goto FINISH_OFF;
+                       if (attachment_data_list[i].attachment_mime_type && strcasestr(attachment_data_list[i].attachment_mime_type, "PKCS7")) {
+                               if (!emstorage_move_file(real_file_path, name_buf, input_from_eas, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+                                       goto FINISH_OFF;
+                               }
+                       } else {
+                               if (!emstorage_copy_file(real_file_path, name_buf, input_from_eas, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
+                                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
+                                       goto FINISH_OFF;
+                               }
                        }
                }
 
                memset(&attachment_tbl, 0, sizeof(emstorage_attachment_tbl_t));
                attachment_tbl.attachment_name                  = attachment_data_list[i].attachment_name;
-               attachment_tbl.attachment_path                  = name_buf;
+               attachment_tbl.attachment_path                  = path_buf;
                attachment_tbl.attachment_size                  = attachment_data_list[i].attachment_size;
                attachment_tbl.mail_id                          = mail_data->mail_id;
                attachment_tbl.account_id                       = mail_data->account_id;
@@ -1072,11 +1182,11 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                attachment_tbl.attachment_drm_type              = attachment_data_list[i].drm_status;
                attachment_tbl.attachment_inline_content_status = attachment_data_list[i].inline_content_status;
                attachment_tbl.attachment_mime_type             = attachment_data_list[i].attachment_mime_type;
+               attachment_tbl.content_id                       = attachment_data_list[i].content_id;
 
-               if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
+               if (!emstorage_add_attachment(multi_user_name, &attachment_tbl, 0, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
-                       emstorage_rollback_transaction(NULL, NULL, NULL);
-
+                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
 
@@ -1087,13 +1197,13 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
 
 #ifdef __FEATURE_BODY_SEARCH__
        /* Insert mail_text to DB */
-       if (!emcore_strip_mail_body_from_file(converted_mail_tbl, &stripped_text, &err) || stripped_text == NULL) {
+       if (!emcore_strip_mail_body_from_file(multi_user_name, converted_mail_tbl, &stripped_text, &err) || stripped_text == NULL) {
                EM_DEBUG_EXCEPTION("emcore_strip_mail_body_from_file failed [%d]", err);
        }
 
-       if (!emcore_add_mail_text(mailbox_tbl, converted_mail_tbl, stripped_text, &err)) {
+       if (!emcore_add_mail_text(multi_user_name, mailbox_tbl, converted_mail_tbl, stripped_text, &err)) {
                EM_DEBUG_EXCEPTION("emcore_add_mail_text failed [%d]", err);
-               emstorage_rollback_transaction(NULL, NULL, NULL);
+               emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                goto FINISH_OFF;
        }
 #endif
@@ -1104,21 +1214,25 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
                || mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
                EM_DEBUG_LOG("This mail has the meeting request");
                input_meeting_request->mail_id = mail_data->mail_id;
-               if (!emstorage_add_meeting_request(mail_data->account_id, mailbox_tbl->mailbox_id, input_meeting_request, false, &err))  {
-                       EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
 
+               if (!emstorage_add_meeting_request(multi_user_name, mail_data->account_id, mailbox_tbl->mailbox_id, input_meeting_request, false, &err))  {
+                       EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
+                       emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL);
                        goto FINISH_OFF;
                }
        }
 
-       emstorage_commit_transaction(NULL, NULL, NULL);
+       emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL);
+
+       if (emstorage_get_thread_id_of_thread_mails(multi_user_name, converted_mail_tbl, &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.");
 
        SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl->mailbox_id);
-       if (!emcore_notify_storage_event(NOTI_MAIL_ADD, converted_mail_tbl->account_id, converted_mail_tbl->mail_id, mailbox_id_param_string, converted_mail_tbl->thread_id))
+       if (!emcore_notify_storage_event(NOTI_MAIL_ADD, converted_mail_tbl->account_id, converted_mail_tbl->mail_id, mailbox_id_param_string, thread_id))
                EM_DEBUG_LOG("emcore_notify_storage_event [NOTI_MAIL_ADD] failed.");
 
        if (account_tbl_item->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC && !move_flag) {
-               if (!emcore_remove_overflowed_mails(mailbox_tbl, &err)) {
+               if (!emcore_remove_overflowed_mails(multi_user_name, mailbox_tbl, &err)) {
                        if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
                                err = EMAIL_ERROR_NONE;
                        else
@@ -1129,10 +1243,8 @@ INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_atta
        if ( input_from_eas && (mail_data->flags_seen_field == 0)) {
 //                             && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_TRASH
 //                             && mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX) {
-               if ((err = emcore_update_sync_status_of_account(mail_data->account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING | SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
+               if ((err = emcore_update_sync_status_of_account(multi_user_name, mail_data->account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING | SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
                                EM_DEBUG_LOG("emcore_update_sync_status_of_account failed [%d]", err);
-//             emcore_add_notification_for_unread_mail(converted_mail_tbl);
-               emcore_display_unread_in_badge();
        }
 
 FINISH_OFF:
@@ -1140,6 +1252,7 @@ FINISH_OFF:
                dlclose(dl_handle);
 
        EM_SAFE_FREE(body_text_file_name);
+       EM_SAFE_FREE(prefix_path);
 
 #ifdef __FEATURE_BODY_SEARCH__
        EM_SAFE_FREE(stripped_text);
@@ -1161,7 +1274,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id)
+INTERNAL_FUNC int emcore_add_read_receipt(char *multi_user_name, int input_read_mail_id, int *output_receipt_mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_read_mail_id [%d], output_receipt_mail_id [%p]", input_read_mail_id, output_receipt_mail_id);
        int                      err = EMAIL_ERROR_NONE;
@@ -1174,7 +1287,7 @@ INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_re
        emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
        BODY                    *root_body = NULL;
 
-       if( (err = emcore_get_mail_data(input_read_mail_id, &read_mail_data)) != EMAIL_ERROR_NONE) {
+       if( (err = emcore_get_mail_data(multi_user_name, input_read_mail_id, &read_mail_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1203,7 +1316,7 @@ INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_re
                SNPRINTF(receipt_mail_data->subject, EM_SAFE_STRLEN(read_mail_data->subject) + 7,  "Read: %s", read_mail_data->subject);
        }
 
-       if (!emstorage_get_mailbox_by_mailbox_type(receipt_mail_data->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_tbl, true, &err))  {
+       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, receipt_mail_data->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_tbl, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1221,14 +1334,14 @@ INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_re
                goto FINISH_OFF;
        }
 
-       if ( (err = emcore_make_envelope_from_mail(receipt_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
+       if ( (err = emcore_make_envelope_from_mail(multi_user_name, receipt_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", err);
                goto FINISH_OFF;
        }
 
        envelope->references = EM_SAFE_STRDUP(read_mail_data->message_id);
 
-       if (!emcore_get_report_mail_body(envelope, &root_body, &err))  {
+       if (!emcore_get_report_mail_body(multi_user_name, envelope, &root_body, &err))  {
                EM_DEBUG_EXCEPTION("emcore_get_report_mail_body failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1262,7 +1375,7 @@ INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_re
        attachment_data->attachment_name = cpystr(p);
        */
 
-       if ( (err = emcore_add_mail(receipt_mail_data, attachment_data, attachment_count, NULL, 0, false)) != EMAIL_ERROR_NONE) {
+       if ( (err = emcore_add_mail(multi_user_name, receipt_mail_data, attachment_data, attachment_count, NULL, 0, false)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1293,7 +1406,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code)
+INTERNAL_FUNC int emcore_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
        int ret = false;
@@ -1307,9 +1420,8 @@ INTERNAL_FUNC int emcore_add_meeting_request(int account_id, int input_mailbox_i
                goto FINISH_OFF;
        }
 
-       if (!emstorage_add_meeting_request(account_id, input_mailbox_id, meeting_req, 1, &err))  {
+       if (!emstorage_add_meeting_request(multi_user_name, account_id, input_mailbox_id, meeting_req, 1, &err))  {
                EM_DEBUG_EXCEPTION(" emstorage_add_meeting_request failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -1324,7 +1436,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_query_mail_size_limit(int account_id, int handle, int *err_code)
+INTERNAL_FUNC int emcore_query_mail_size_limit(char *multi_user_name, int account_id, int handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
        int ret = false;
@@ -1334,6 +1446,7 @@ INTERNAL_FUNC int emcore_query_mail_size_limit(int account_id, int handle, int *
        int mail_size_limit = -1;
        email_account_t *ref_account = NULL;
        sslstart_t stls = NULL;
+       MAILSTREAM *mail_stream = NULL;
 
        if (account_id <= 0) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -1341,7 +1454,7 @@ INTERNAL_FUNC int emcore_query_mail_size_limit(int account_id, int handle, int *
                goto FINISH_OFF;
        }
 
-       if (!(ref_account = emcore_get_account_reference(account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -1354,13 +1467,13 @@ INTERNAL_FUNC int emcore_query_mail_size_limit(int account_id, int handle, int *
        }
 
        if (ref_account->pop_before_smtp != FALSE) {
-               if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err)) {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err)) {
                        EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
-       if (!emcore_connect_to_remote_mailbox(account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err)) {
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1388,6 +1501,9 @@ FINISH_OFF:
                smtp_close(stream);
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 
+       if (mail_stream)
+               mail_stream = mail_close (mail_stream);
+
        if (stls)
                mail_parameters(NULL, SET_SSLSTART, (void  *)stls);
 
@@ -1406,31 +1522,135 @@ FINISH_OFF:
        return ret;
 }
 
-/*  send a mail */
-INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
+/* thread local variable for reuse */
+__thread GList* g_send_stream_list = NULL;
+typedef struct {
+       int account_id;
+       SENDSTREAM **send_stream;
+} email_send_stream_list_t;
+
+/*
+stmp stream should be closed when threads exit, otherwise memory leaks
+*/
+INTERNAL_FUNC void emcore_close_smtp_stream_list ()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       GList* cur = g_send_stream_list;
+       email_send_stream_list_t* data = NULL;
+       for ( ; cur ; cur = g_list_next(cur) ) {
+               data = cur->data;
+               *(data->send_stream) = smtp_close (*(data->send_stream));
+               g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
+               g_free (data);
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+/*
+if threads exit after calling the function, emcore_close_smtp_stream_list should be called.
+Otherwise, memory leaks
+*/
+INTERNAL_FUNC SENDSTREAM** emcore_get_smtp_stream (char *multi_user_name, int account_id, int *error)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
+       GList* cur = g_send_stream_list;
+       email_send_stream_list_t* data = NULL;
+       SENDSTREAM** ret = NULL;
+       int err = EMAIL_ERROR_NONE;
+
+       for ( ; cur ; cur = g_list_next(cur) ) {
+               data = cur->data;
+               if (data->account_id == account_id) {
+                       if (data->send_stream == NULL || *(data->send_stream) == NULL) {
+                               EM_DEBUG_LOG ("smtp_stream was closed before");
+                               g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
+                               g_free (data);
+                               break;
+                       }
+
+                       int reply = smtp_send ( *(data->send_stream), "NOOP", NULL);
+                       if (reply/100 == 2) { /* 2xx means a success */
+                               EM_DEBUG_LOG ("reusable smtp_stream found");
+                               return data->send_stream;
+                       }
+                       else {
+                               EM_DEBUG_LOG ("smtp_stream is not reusable");
+                               *(data->send_stream) = smtp_close (*(data->send_stream));
+                               g_send_stream_list = g_list_delete_link (g_send_stream_list, cur);
+                               g_free (data->send_stream);
+                               break;
+                       }
+               }
+       }
+
+       ret = em_malloc (sizeof(SENDSTREAM*));
+       if (!ret) {
+               EM_DEBUG_EXCEPTION("em_malloc error");
+               goto FINISH_OFF;
+       }
+
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)ret, &err))  {
+               EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+
+               EM_SAFE_FREE(ret);
+               goto FINISH_OFF;
+       }
+
+       email_send_stream_list_t *node = em_malloc (sizeof(email_send_stream_list_t));
+       if (!node) {
+               EM_DEBUG_EXCEPTION ("em_malloc error");
+               *ret = smtp_close (*ret);
+               EM_SAFE_FREE(ret);
+               goto FINISH_OFF;
+       }
+
+       node->account_id = account_id;
+       node->send_stream = ret;
+
+       g_send_stream_list = g_list_prepend (g_send_stream_list, node);
+
+FINISH_OFF:
+
+       if (error)
+               *error = err;
+
+       EM_DEBUG_FUNC_END();
+
+       return ret;
+}
+
+
+/*
+send a mail
+3 threads call this function :
+worker_send_event_queue
+mainloop (by alarm),
+thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL,
+*/
+
+INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
        EM_PROFILE_BEGIN(profile_emcore_send_mail);
        int ret = false;
        int err = EMAIL_ERROR_NONE, err2 = EMAIL_ERROR_NONE;
-       int status = EMAIL_SEND_FAIL;
        int attachment_tbl_count = 0;
        int i = 0;
        int account_id = 0;
-       SENDSTREAM *stream = NULL;
+       SENDSTREAM** send_stream = NULL;
        ENVELOPE *envelope = NULL;
        sslstart_t stls = NULL;
        emstorage_mail_tbl_t       *mail_tbl_data = NULL;
        emstorage_attachment_tbl_t *attachment_tbl_data = NULL;
        email_account_t *ref_account = NULL;
        email_option_t *opt = NULL;
-       void *tmp_stream = NULL;
        char *fpath = NULL;
-       int sent_box = 0;
        emstorage_mailbox_tbl_t* local_mailbox = NULL;
        int dst_mailbox_id = 0;
        int total_mail_size = 0;
        int sent_flag = 0;
+       MAILSTREAM *mail_stream = NULL;
 
        if (!mail_id)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -1439,20 +1659,20 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
        }
 
        /*  get mail to send */
-       if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err) || err != EMAIL_ERROR_NONE) {
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_tbl_data, false, &err) || err != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        account_id = mail_tbl_data->account_id;
 
-       if (!(ref_account = emcore_get_account_reference(account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_attachment_list(mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1469,7 +1689,7 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                goto FINISH_OFF;
        }
        else {
-               if ((err = em_verify_email_address_of_mail_tbl(mail_tbl_data, false)) != EMAIL_ERROR_NONE) {
+               if ((err = em_verify_email_address_of_mail_tbl(mail_tbl_data)) != EMAIL_ERROR_NONE) {
                        err = EMAIL_ERROR_INVALID_ADDRESS;
                        EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
                        goto FINISH_OFF;
@@ -1495,7 +1715,7 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
        /*Update status flag to DB*/
 
        /*  get rfc822 data */
-       if (!emcore_make_rfc822_file_from_mail(mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err)) {
+       if (!emcore_make_rfc822_file_from_mail(multi_user_name, mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err)) {
                EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1519,15 +1739,15 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
        }
 
        if (ref_account->pop_before_smtp != FALSE)  {
-               if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err))  {
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&mail_stream, &err))  {
                        EM_DEBUG_EXCEPTION(" POP before SMTP Authentication failed [%d]", err);
-                       status = EMAIL_LIST_CONNECTION_FAIL;
                        if (err == EMAIL_ERROR_CONNECTION_BROKEN)
                                err = EMAIL_ERROR_CANCELLED;
                        goto FINISH_OFF;
                }
        }
-       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_DRAFT, &local_mailbox, false, &err))  {
+
+       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_DRAFT, &local_mailbox, false, &err))  {
                EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1535,16 +1755,25 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
        emstorage_free_mailbox(&local_mailbox, 1, NULL);
 
 
-       if (!emcore_connect_to_remote_mailbox(account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err))  {
+       send_stream = emcore_get_smtp_stream (multi_user_name, account_id, &err);
+       if (!send_stream) {
+               EM_DEBUG_EXCEPTION(" emcore_get_smtp_stream failed [%d]", err);
+               if (err == EMAIL_ERROR_CONNECTION_BROKEN)
+                       err = EMAIL_ERROR_CANCELLED;
+               goto FINISH_OFF;
+       }
+
+#if 0
+       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, (void **)&tmp_stream, &err))  {
                EM_DEBUG_EXCEPTION(" emcore_connect_to_remote_mailbox failed [%d]", err);
 
                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
                        err = EMAIL_ERROR_CANCELLED;
-               status = EMAIL_SEND_CONNECTION_FAIL;
                goto FINISH_OFF;
        }
 
        stream = (SENDSTREAM *)tmp_stream;
+#endif
 
        if (!emcore_check_send_mail_thread_status()) {
                EM_DEBUG_EXCEPTION(" emcore_check_send_mail_thread_status failed...");
@@ -1552,12 +1781,12 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                goto FINISH_OFF;
        }
 
-       if (stream && stream->protocol.esmtp.ok) {
-               if (stream->protocol.esmtp.size.ok && stream->protocol.esmtp.size.limit > 0) {
-                       EM_DEBUG_LOG("Server size limit : %ld", stream->protocol.esmtp.size.limit);
+       if (*send_stream && (*send_stream)->protocol.esmtp.ok) {
+               if ((*send_stream)->protocol.esmtp.size.ok && (*send_stream)->protocol.esmtp.size.limit > 0) {
+                       EM_DEBUG_LOG("Server size limit : %ld", (*send_stream)->protocol.esmtp.size.limit);
                        emcore_get_file_size(fpath, &total_mail_size, NULL);
                        EM_DEBUG_LOG("mail size : %d", total_mail_size);
-                       if (total_mail_size > stream->protocol.esmtp.size.limit) {
+                       if (total_mail_size > (*send_stream)->protocol.esmtp.size.limit) {
                                err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
                                goto FINISH_OFF;
                        }
@@ -1570,27 +1799,27 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
 
        if (opt->req_delivery_receipt == EMAIL_OPTION_REQ_DELIVERY_RECEIPT_ON || (mail_tbl_data->report_status & EMAIL_MAIL_REQUEST_DSN))  {
                EM_DEBUG_LOG("DSN is required.");
-               stream->protocol.esmtp.dsn.want = 1;
-               stream->protocol.esmtp.dsn.full = 0;
-               stream->protocol.esmtp.dsn.notify.failure = 1;
-               stream->protocol.esmtp.dsn.notify.success = 1;
+               (*send_stream)->protocol.esmtp.dsn.want = 1;
+               (*send_stream)->protocol.esmtp.dsn.full = 0;
+               (*send_stream)->protocol.esmtp.dsn.notify.failure = 1;
+               (*send_stream)->protocol.esmtp.dsn.notify.success = 1;
        }
 
        mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
-       emcore_show_user_message(mail_id, EMAIL_ACTION_SENDING_MAIL, EMAIL_ERROR_NONE);
+       emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SENDING_MAIL, EMAIL_ERROR_NONE);
 
        /*Update status save_status to DB*/
-       if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
 
        /*  send mail to server. */
-       if (!emcore_send_mail_smtp(stream, envelope, fpath, account_id, mail_id, &err)) {
+       if (!emcore_send_mail_smtp(multi_user_name, *send_stream, envelope, fpath, account_id, mail_id, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_send_mail_smtp failed [%d]", err);
                if (err == SMTP_RESPONSE_EXCEED_SIZE_LIMIT)
                        err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
 
 #ifndef __FEATURE_MOVE_TO_OUTBOX_FIRST__
-               if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err))  {
+               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -1598,7 +1827,7 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                emstorage_free_mailbox(&local_mailbox, 1, NULL);
 
                /*  unsent mail is moved to 'OUTBOX'. */
-               if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, NULL))
+               if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, NULL))
                        EM_DEBUG_EXCEPTION(" emcore_mail_move falied...");
 #endif
                if (err > 0)
@@ -1606,15 +1835,18 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                goto FINISH_OFF;
        }
 
+       emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, err);
+       sent_flag = 1;
+
        /*  sent mail is moved to 'SENT' box or deleted. */
        if (opt->keep_local_copy)  {
-               if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
+               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                        goto FINISH_OFF;
                }
                dst_mailbox_id = local_mailbox->mailbox_id;
 
-               if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
+               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);
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
 #ifdef __FEATURE_LOCAL_ACTIVITY__
@@ -1639,7 +1871,7 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                                EM_DEBUG_EXCEPTION(" emcore_add_activity Failed [%d] ", err);
                        }
 
-                       if (!emcore_move_mail_on_server(dest_mbox.account_id, dst_mailbox_id, &mail_id, 1, dest_mbox.name, &err)) {
+                       if (!emcore_move_mail_on_server(multi_user_name, dest_mbox.account_id, dst_mailbox_id, &mail_id, 1, dest_mbox.name, &err)) {
                                EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
                        }
                        else {
@@ -1664,18 +1896,19 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                        emstorage_mailbox_tbl_t* src_mailbox = NULL;
                        emstorage_mail_tbl_t *temp_mail = NULL;
 
-                       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &src_mailbox, true, &err))  {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &src_mailbox, true, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
 
                        EM_DEBUG_LOG("local_yn:[%d]", src_mailbox->local_yn);
                        if (src_mailbox->local_yn) {
-                               if (!emcore_sync_header(local_mailbox, NULL, NULL, NULL, NULL, &err)) {
+                               void *local_stream = NULL;
+                               if (!emcore_sync_header (multi_user_name, local_mailbox, &local_stream, NULL, NULL, NULL, NULL, NULL, 0, -1, &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_sync_header failed");
                                }
-
-                               if (!emstorage_get_maildata_by_servermailid(local_mailbox->mailbox_id, "0", &temp_mail, false, &err)) {
+                               mail_close (local_stream);
+                               if (!emstorage_get_maildata_by_servermailid(multi_user_name, local_mailbox->mailbox_id, "0", &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;
@@ -1683,12 +1916,12 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                                }
 
                                if (temp_mail) {
-                                       emcore_sync_mail_from_client_to_server(mail_id);
+                                       emcore_sync_mail_from_client_to_server(multi_user_name, mail_id);
                                        emstorage_free_mail(&temp_mail, 1, NULL);       
                                }
                        }
                        else {
-                               if (!emcore_move_mail_on_server(account_id, src_mailbox->mailbox_id, &mail_id, 1, local_mailbox->mailbox_name, &err)) {
+                               if (!emcore_move_mail_on_server(multi_user_name, account_id, src_mailbox->mailbox_id, &mail_id, 1, local_mailbox->mailbox_name, &err)) {
                                        EM_DEBUG_EXCEPTION(" emcore_move_mail_on_server falied [%d]", err);
                                }
                        }
@@ -1699,24 +1932,22 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
                /* On Successful Mail sent remove the Draft flag */
                mail_tbl_data->flags_draft_field = 0;
 
-               if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "flags_draft_field", mail_tbl_data->flags_draft_field, true, &err))
+               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "flags_draft_field", mail_tbl_data->flags_draft_field, true, &err))
                        EM_DEBUG_EXCEPTION("Failed to modify extra flag [%d]", err);
-
-               sent_box = 1;
        }
        else  {
-               if (!emcore_delete_mail(account_id, &mail_id, 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
+               if (!emcore_delete_mail(multi_user_name, account_id, &mail_id, 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
                        EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
        }
 
        /* Set the phone log */
-       if ((err = emcore_set_sent_contacts_log(mail_tbl_data)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_set_sent_contacts_log(multi_user_name, mail_tbl_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
        }
 
        /*Update save_status */
        mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
-       if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err))
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
 
        if (!emcore_delete_transaction_info_by_mailId(mail_id))
@@ -1726,13 +1957,13 @@ INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code)
 
 FINISH_OFF:
        if (ret == false && sent_flag == 0) {
-               emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, err);
+               emcore_show_user_message(multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, err);
        }
 
        if (ret == false && err != EMAIL_ERROR_INVALID_PARAM && mail_tbl_data)  {
                if (err != EMAIL_ERROR_CANCELLED) {
                        mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
-                       if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
+                       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
                }
                else {
@@ -1740,7 +1971,7 @@ FINISH_OFF:
                                EM_DEBUG_LOG("EMAIL_MAIL_STATUS_SEND_CANCELED Already set for ");
                        else {
                                mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
-                               if (!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
+                               if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", mail_tbl_data->save_status, true, &err2))
                                        EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err2);
                        }
                }
@@ -1751,10 +1982,16 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
+#if 0
 #ifndef __FEATURE_KEEP_CONNECTION__
        if (stream)
                smtp_close(stream);
 #endif /* __FEATURE_KEEP_CONNECTION__ */
+#endif
+
+       if (mail_stream)
+               mail_stream = mail_close (mail_stream);
+
        if (stls)
                mail_parameters(NULL, SET_SSLSTART, (void  *)stls);
 
@@ -1765,7 +2002,7 @@ FINISH_OFF:
                mail_free_envelope(&envelope);
 
        if (fpath) {
-               EM_DEBUG_LOG("REMOVE TEMP FILE  :  %s", fpath);
+               EM_DEBUG_LOG_SEC("REMOVE TEMP FILE  :  %s", fpath);
                remove(fpath);
                EM_SAFE_FREE (fpath);
        }
@@ -1791,7 +2028,7 @@ FINISH_OFF:
 
                if (err != EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE) {
                        /* Add alarm for next sending mails */
-                       if( (err2 = emcore_create_alarm_for_auto_resend(AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
+                       if( (err2 = emcore_create_alarm_for_auto_resend(multi_user_name, AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
                                if (err2 != EMAIL_ERROR_MAIL_NOT_FOUND)
                                        EM_DEBUG_EXCEPTION("emcore_create_alarm_for_auto_resend failed [%d]", err2);
                        }
@@ -1809,7 +2046,7 @@ FINISH_OFF:
 }
 
 /*  send a saved all mails */
-INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_name, int *err_code)
+INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id, char *input_mailbox_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_name[%p], err_code[%p]", account_id, input_mailbox_name, err_code);
 
@@ -1841,18 +2078,24 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
        }
 
 
-       if (!(ref_account = emcore_get_account_reference(account_id)))  {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id)))  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       FINISH_OFF_IF_CANCELED;
+/*don't delete the comment. several threads including event thread call this func */
+/*     FINISH_OFF_IF_CANCELED; */
 
        opt = &(ref_account->options);
 
+       if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, input_mailbox_name, &local_mailbox, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed : [%d]", err);
+               goto FINISH_OFF;
+       }
+
        /*  search mail. */
-       if (!emstorage_mail_search_start(NULL, account_id, input_mailbox_name, 0, &handle, &total, true, &err))  {
+       if (!emstorage_mail_search_start(multi_user_name, NULL, account_id, local_mailbox->mailbox_id, 0, &handle, &total, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1880,10 +2123,14 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
 
        mail_send_notify(EMAIL_SEND_PREPARE, 0, 0, account_id, mail_ids[total], err);
 
+       if(local_mailbox)
+               emstorage_free_mailbox(&local_mailbox, 1, NULL);
+
        for (i = 0; i < total; i++) {
-               FINISH_OFF_IF_CANCELED;
+/*don't delete the comment. several threads including event thread call this func */
+/*             FINISH_OFF_IF_CANCELED;*/
 
-               if (!emstorage_get_mail_by_id(mail_ids[i], &searched_mail_tbl_data, false, &err)) {
+               if (!emstorage_get_mail_by_id(multi_user_name, mail_ids[i], &searched_mail_tbl_data, false, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -1896,13 +2143,13 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
                        continue;
                }
 
-               if ( (err = emstorage_get_attachment_list(mail_ids[i], false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
+               if ( (err = emstorage_get_attachment_list(multi_user_name, mail_ids[i], false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                /* check for email_address validation */
-               if ( (err = em_verify_email_address_of_mail_tbl(searched_mail_tbl_data, false)) != EMAIL_ERROR_NONE ) {
+               if ( (err = em_verify_email_address_of_mail_tbl(searched_mail_tbl_data)) != EMAIL_ERROR_NONE ) {
                        err = EMAIL_ERROR_INVALID_ADDRESS;
                        EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed [%d]", err);
                        goto FINISH_OFF;
@@ -1910,12 +2157,13 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
 
                searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
 
-               if (!emcore_make_rfc822_file_from_mail(searched_mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err))  {
+               if (!emcore_make_rfc822_file_from_mail(multi_user_name, searched_mail_tbl_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fpath, opt, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail falied [%d]", err);
                        goto FINISH_OFF;
                }
 
-               FINISH_OFF_IF_CANCELED;
+/*don't delete the comment. several threads including event thread call this func */
+/*             FINISH_OFF_IF_CANCELED;*/
 
                /*  connect mail server. */
                if (!stream) {
@@ -1926,7 +2174,7 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
                        }
 
                        stream = NULL;
-                       if (!emcore_connect_to_remote_mailbox(account_id, EMAIL_CONNECT_FOR_SENDING, &tmp_stream, &err) || !tmp_stream)  {
+                       if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, EMAIL_CONNECT_FOR_SENDING, &tmp_stream, &err) || !tmp_stream)  {
                                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
 
                                if (err == EMAIL_ERROR_CONNECTION_BROKEN)
@@ -1938,7 +2186,8 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
 
                        stream = (SENDSTREAM *)tmp_stream;
 
-                       FINISH_OFF_IF_CANCELED;
+/*don't delete the comment. several threads including event thread call this func */
+/*                     FINISH_OFF_IF_CANCELED;*/
 
                        mail_send_notify(EMAIL_SEND_CONNECTION_SUCCEED, 0, 0, account_id, mail_ids[i], err);
 
@@ -1956,29 +2205,29 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
                searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENDING;
 
                /*  update mail status to sending. */
-               if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
+               if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
 
                        goto FINISH_OFF;
                }
 
-               if (!emcore_send_mail_smtp(stream, envelope, fpath, account_id, mail_ids[i], &err))  {
+               if (!emcore_send_mail_smtp(multi_user_name, stream, envelope, fpath, account_id, mail_ids[i], &err))  {
                        EM_DEBUG_EXCEPTION("emcore_send_mail_smtp failed [%d]", err);
                        if (err == SMTP_RESPONSE_EXCEED_SIZE_LIMIT) err = EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE;
 
                        searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_FAILURE;
 
                        /*  update mail status to failure. */
-                       if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err2))
+                       if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err2))
                                EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
 
-                       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
                        dst_mailbox_id = local_mailbox->mailbox_id;
 
-                       emcore_move_mail(&mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, NULL);
+                       emcore_move_mail(multi_user_name, &mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, NULL);
 
                        if(local_mailbox)
                                emstorage_free_mailbox(&local_mailbox, 1, NULL);
@@ -1989,32 +2238,32 @@ INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *input_mailbox_nam
                searched_mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SENT;
 
                /*  update mail status to sent mail. */
-               if (!emstorage_change_mail_field(mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
+               if (!emstorage_change_mail_field(multi_user_name, mail_ids[i], UPDATE_EXTRA_FLAG, searched_mail_tbl_data, true, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                /*  sent mail is moved to 'SENT' box or deleted. */
                if (opt->keep_local_copy)  {
-                       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
+                       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &local_mailbox, true, &err))  {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
                                goto FINISH_OFF;
                        }
                        dst_mailbox_id = local_mailbox->mailbox_id;
 
-                       if (!emcore_move_mail(&mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
+                       if (!emcore_move_mail(multi_user_name, &mail_ids[i], 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err))
                                EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
 
                        if(local_mailbox)
                                emstorage_free_mailbox(&local_mailbox, 1, NULL);
                }
                else  {
-                       if (!emcore_delete_mail(account_id, &mail_ids[i], 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
+                       if (!emcore_delete_mail(multi_user_name, account_id, &mail_ids[i], 1, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_AFTER_SENDING, false, &err))
                                EM_DEBUG_EXCEPTION("emcore_delete_mail falied [%d]", err);
                }
 
                /* Set the phone log */
-               if ((err = emcore_set_sent_contacts_log(searched_mail_tbl_data)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_set_sent_contacts_log(multi_user_name, searched_mail_tbl_data)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
                }
 
@@ -2074,11 +2323,11 @@ FINISH_OFF:
 
        if (ret == true) {
                mail_send_notify(EMAIL_SEND_FINISH, 0, 0, account_id, mail_ids[total], err);
-               emcore_show_user_message(account_id, EMAIL_ACTION_SEND_MAIL, err);
+               emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_SEND_MAIL, err);
        } else {
                if(mail_ids) /* prevent 34385 */
                        mail_send_notify(status, 0, 0, account_id, mail_ids[total], err);
-               emcore_show_user_message(account_id, EMAIL_ACTION_SEND_MAIL, err);
+               emcore_show_user_message(multi_user_name, account_id, EMAIL_ACTION_SEND_MAIL, err);
        }
 
        EM_SAFE_FREE(mail_ids);
@@ -2089,7 +2338,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
+static int emcore_send_mail_smtp(char *multi_user_name, SENDSTREAM *stream, ENVELOPE *env, char *data_file, int account_id, int mail_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("stream[%p], env[%p], data_file[%s], account_id[%d], mail_id[%d], err_code[%p]", stream, env, data_file, account_id, mail_id, err_code);
        EM_PROFILE_BEGIN(profile_emcore_send_mail_smtp);
@@ -2115,7 +2364,7 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
                goto FINISH_OFF;
        }
 
-       if (!(ref_account = emcore_get_account_reference(account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -2184,7 +2433,7 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
 
        if (env->to) {
                send_ret = smtp_rcpt(stream, env->to, &send_err);
-               EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->to->mailbox, env->to->host, env->to->error ? env->to->error  :  stream->reply);
+               EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->to->mailbox, env->to->host, env->to->error ? env->to->error  :  stream->reply);
                if (send_ret) {
                        err = stream->replycode;
                        goto FINISH_OFF;
@@ -2196,7 +2445,7 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
 
        if (env->cc) {
                send_ret = smtp_rcpt(stream, env->cc, &send_err);
-               EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->cc->mailbox, env->cc->host, env->cc->error ? env->cc->error  :  stream->reply);
+               EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->cc->mailbox, env->cc->host, env->cc->error ? env->cc->error  :  stream->reply);
                if (send_ret) {
                        err = stream->replycode;
                        goto FINISH_OFF;
@@ -2208,7 +2457,7 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
 
        if (env->bcc) {
                send_ret = smtp_rcpt(stream, env->bcc, &send_err);
-               EM_DEBUG_LOG("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->bcc->mailbox, env->bcc->host, env->bcc->error ? env->bcc->error  :  stream->reply);
+               EM_DEBUG_LOG_SEC("[SMTP] RCPT TO : <%s@%s> ... --------> %s", env->bcc->mailbox, env->bcc->host, env->bcc->error ? env->bcc->error  :  stream->reply);
                if (send_ret) {
                        err = stream->replycode;
                        goto FINISH_OFF;
@@ -2242,7 +2491,7 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
        }
 
        send_ret = smtp_send(stream, "DATA", 0);
-       EM_DEBUG_LOG("[SMTP] DATA --------> %s", stream->reply);
+       EM_DEBUG_LOG_SEC("[SMTP] DATA --------> %s", stream->reply);
        EM_PROFILE_END(profile_prepare_and_head);
 
        if (send_ret != SMTP_RESPONSE_READY) {
@@ -2252,9 +2501,10 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
 
        if (data_file) {
                EM_PROFILE_BEGIN(profile_open_file);
-               if (!(fp = fopen(data_file, "r+"))) {
-                       EM_DEBUG_EXCEPTION("fopen(\"%s\") failed...", data_file);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+
+               err = em_fopen(data_file, "r+", &fp);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_fopen(\"%s\") failed..., error:[%d]", data_file, err);
                        goto FINISH_OFF;
                }
                EM_PROFILE_END(profile_open_file);
@@ -2288,6 +2538,14 @@ static int emcore_send_mail_smtp(SENDSTREAM *stream, ENVELOPE *env, char *data_f
                }
 
                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
@@ -2437,7 +2695,7 @@ char *emcore_generate_content_id_string(const char *hostname, int *err)
 /*             else absolute path of file to be attached. */
 /*  data_len  :  length of data. if filename not NULL, ignored. */
 /*  file_name :  attahcment name. */
-static int attach_part (BODY *body, const unsigned char *data, int data_len, 
+static int attach_part (BODY *body, const unsigned char *data, int data_len,
                                    char *filename, char *content_sub_type, int is_inline, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("body[%p], data[%s], data_len[%d], filename[%s], content_sub_type[%s], err_code[%p]", body, data, data_len, filename, content_sub_type, err_code);
@@ -2447,6 +2705,7 @@ static int attach_part (BODY *body, const unsigned char *data, int data_len,
        int        has_special_character = 0;
        int        base64_file_name_length = 0;
        int        i= 0;
+       int        mail_type = EMAIL_SMIME_NONE;
        gsize      bytes_read;
        gsize      bytes_written;
        char      *encoded_file_name = NULL;
@@ -2555,16 +2814,35 @@ static int attach_part (BODY *body, const unsigned char *data, int data_len,
                part->body.type = em_get_content_type_from_extension_string(extension, NULL);
                if(part->body.type == TYPEIMAGE) {
                        part->body.subtype = strdup(extension);
+                       part->body.encoding = ENCBINARY;
                } else if (part->body.type == TYPEPKCS7_SIGN) {
                        part->body.subtype = strdup("pkcs7-signature");
                        part->body.type = TYPEAPPLICATION;
+                       part->body.encoding = ENCBINARY;
                } else if (part->body.type == TYPEPKCS7_MIME) {
                        part->body.subtype = strdup("pkcs7-mime");
                        part->body.type = TYPEAPPLICATION;
-               } else
+                       part->body.encoding = ENCBINARY;
+               } else if (part->body.type == TYPEPGP) {
+                       part->body.type     = TYPEAPPLICATION;
+                       part->body.subtype  = EM_SAFE_STRDUP(content_sub_type);
+                       part->body.encoding = ENC7BIT;
+               } else {
                        part->body.subtype = strdup("octet-stream");
+                       part->body.encoding = ENCBINARY;
+               }
+
+               if (!extension && content_sub_type) {
+                       char *p = NULL;
+
+                       if (strcasecmp(content_sub_type, "IMAGE") == 0) {
+                               part->body.type = TYPEIMAGE;
+                               if ((p = strstr(content_sub_type, "/"))) {
+                                       part->body.subtype = EM_SAFE_STRDUP(p+1);
+                               }
+                       }
+               }
 
-               part->body.encoding = ENCBINARY;
                part->body.size.bytes = data_len;
 
                if (data)
@@ -2617,108 +2895,125 @@ static int attach_part (BODY *body, const unsigned char *data, int data_len,
                        last_part->body.disposition.type = strdup("inline");
        }
        else  {
-               /*  text body (plain/html) */
-               part->body.type = TYPETEXT;
-               part->body.size.bytes = data_len;
-
-               if (data)
-                       part->body.sparep = EM_SAFE_STRDUP((char *)data); /*  file path */
-               else
-                       part->body.sparep = NULL;
+               if (content_sub_type && !strcasecmp(content_sub_type, "pgp-encrypted"))
+                       mail_type = EMAIL_PGP_ENCRYPTED;
 
+               if (mail_type != EMAIL_PGP_ENCRYPTED) {
+                       /*  text body (plain/html) */
+                       part->body.type = TYPETEXT;
+                       part->body.size.bytes = data_len;
 
-               if (!content_sub_type)  {
-                       /* Plain text body */
-                       part->body.encoding = ENC8BIT;
-                       part->body.subtype = cpystr("plain");
-                       last_param = part->body.parameter;
+                       if (data)
+                               part->body.sparep = EM_SAFE_STRDUP((char *)data); /*  file path */
+                       else
+                               part->body.sparep = NULL;
 
-                       if (last_param != NULL)  {
-                               while (last_param->next)
-                                       last_param = last_param->next;
-                       }
 
-                       param = mail_newbody_parameter();
+                       if (!content_sub_type)  {
+                               /* Plain text body */
+                               part->body.encoding = ENC8BIT;
+                               part->body.subtype = cpystr("plain");
+                               last_param = part->body.parameter;
 
-                       if (param == NULL)  {
-                               EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
-                               error = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
+                               if (last_param != NULL)  {
+                                       while (last_param->next)
+                                               last_param = last_param->next;
+                               }
 
-                       param->attribute = cpystr("CHARSET");
+                               param = mail_newbody_parameter();
 
-                       if (data != NULL) {
-                               gchar *extract_charset_plain = g_path_get_basename((const gchar *)data);
-                               if (extract_charset_plain != NULL && extract_charset_plain[0] != '\0')
-                                       param->value = cpystr(extract_charset_plain);
-                               g_free(extract_charset_plain);
-                       }
-                       else
-                               param->value = cpystr("UTF-8");
+                               if (param == NULL)  {
+                                       EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
+                                       error = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
+                               }
 
-                       if(!param->value)
-                               param->value = cpystr("UTF-8");
+                               param->attribute = cpystr("CHARSET");
 
-                       param->next = NULL;
+                               if (data != NULL) {
+                                       gchar *extract_charset_plain = g_path_get_basename((const gchar *)data);
+                                       if (extract_charset_plain != NULL && extract_charset_plain[0] != '\0')
+                                               param->value = cpystr(extract_charset_plain);
+                                       g_free(extract_charset_plain);
+                               }
+                               else
+                                       param->value = cpystr("UTF-8");
 
-                       if (last_param != NULL)
-                               last_param->next = param;
-                       else
-                               part->body.parameter = param;
-               }
-               else {
-                       /* HTML text body */
-                       part->body.encoding = ENC8BIT;
-                       part->body.subtype  = cpystr(content_sub_type);
+                               if(!param->value)
+                                       param->value = cpystr("UTF-8");
 
-                       last_param = part->body.parameter;
+                               param->next = NULL;
 
-                       if (last_param != NULL)  {
-                               while (last_param->next)
-                                       last_param = last_param->next;
+                               if (last_param != NULL)
+                                       last_param->next = param;
+                               else
+                                       part->body.parameter = param;
                        }
+                       else {
+                               /* HTML text body */
+                               part->body.encoding = ENC8BIT;
+                               part->body.subtype  = cpystr(content_sub_type);
 
-                       param = mail_newbody_parameter();
+                               last_param = part->body.parameter;
 
-                       if (param == NULL)  {
-                               EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
-                               error = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
+                               if (last_param != NULL)  {
+                                       while (last_param->next)
+                                               last_param = last_param->next;
+                               }
 
-                       param->attribute = cpystr("CHARSET");
+                               param = mail_newbody_parameter();
 
-                       char *pHtml = NULL;
-                       if (data != NULL) {
-                               gchar *extract_charset = g_path_get_basename((const gchar *)data);
-                               if (extract_charset != NULL) {
-                                       if ((pHtml = strstr(extract_charset, ".htm")) != NULL) {
-                                               extract_charset[pHtml-extract_charset] = '\0';
-                                               param->value = cpystr(extract_charset);
-                                       }
+                               if (param == NULL)  {
+                                       EM_DEBUG_EXCEPTION("mail_newbody_parameter failed...");
+                                       error = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
                                }
 
-                               if(!param->value)
+                               param->attribute = cpystr("CHARSET");
+
+                               char *pHtml = NULL;
+                               if (data != NULL) {
+                                       gchar *extract_charset = g_path_get_basename((const gchar *)data);
+                                       if (extract_charset != NULL) {
+                                               if ((pHtml = strstr(extract_charset, ".htm")) != NULL) {
+                                                       extract_charset[pHtml-extract_charset] = '\0';
+                                                       param->value = cpystr(extract_charset);
+                                               }
+                                       }
+
+                                       if(!param->value)
+                                               param->value = cpystr("UTF-8");
+
+                                       EM_SAFE_FREE(extract_charset);
+                               }
+                               else
                                        param->value = cpystr("UTF-8");
+                               param->next = NULL;
 
-                               EM_SAFE_FREE(extract_charset);
+                               if (last_param != NULL)
+                                       last_param->next = param;
+                               else
+                                       part->body.parameter = param;
                        }
-                       else
-                               param->value = cpystr("UTF-8");
-                       param->next = NULL;
 
-                       if (last_param != NULL)
-                               last_param->next = param;
-                       else
-                               part->body.parameter = param;
-               }
+                       /* NOTE : need to require this code. */
+                       /* sprintf(content_disposition, "%s\0", "inline"); */
+                       if (is_inline) {
+                               SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "inline");
+                               part->body.disposition.type = cpystr(content_disposition);
+                       }
+               } else {
+                       part->body.type = TYPEAPPLICATION;
+                       part->body.subtype = strdup(content_sub_type);
+                       part->body.size.bytes = data_len;
+                       part->body.encoding = ENC7BIT;
 
-               /* NOTE : need to require this code. */
-               /* sprintf(content_disposition, "%s\0", "inline"); */
-               if (is_inline) {
-                       SNPRINTF(content_disposition, sizeof(content_disposition), "%s", "inline");
-                       part->body.disposition.type = cpystr(content_disposition);
+                       if (data)
+                               part->body.sparep = EM_SAFE_STRDUP((char *)data); /*  file path */
+                       else {
+                               part->body.contents.text.data = (unsigned char *)strdup("Version: 1");
+                               part->body.contents.text.size = strlen("Version: 1");
+                       }
                }
        }
 
@@ -2786,7 +3081,7 @@ FINISH_OFF:
        return new_part;
 }
 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
-static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_tbl_count, int *err_code)
+static int attach_attachment_to_body(char *multi_user_name, BODY **multipart_body, BODY *text_body, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_tbl_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("multipart_body[%p], text_body[%p], input_attachment_tbl[%p], input_attachment_tbl_count [%d], err_code[%p]", multipart_body, text_body, input_attachment_tbl, input_attachment_tbl_count, err_code);
 
@@ -2794,6 +3089,9 @@ static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, ems
        int i = 0;
        int error = EMAIL_ERROR_NONE;
        BODY *frame_body = NULL;
+       char *prefix_path = NULL;
+       char real_file_path[MAX_PATH] = {0};
+
        /*  make multipart body(multipart frame_body..) .. that has not content..  */
 
        if (!multipart_body || !text_body || !input_attachment_tbl) {
@@ -2825,14 +3123,27 @@ static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, ems
        char *name = NULL;
        struct stat st_buf;
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        for(i = 0; i < input_attachment_tbl_count; i++) {
                temp_attachment_tbl = input_attachment_tbl + i;
 
                EM_DEBUG_LOG("insert files - attachment id[%d]", temp_attachment_tbl->attachment_id);
 
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, temp_attachment_tbl->attachment_path);
+
                if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
                        if (!temp_attachment_tbl->attachment_name)  {
-                               if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
+                               if (!emcore_get_file_name(real_file_path, &name, &error))  {
                                        EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
                                        goto FINISH_OFF;
                                }
@@ -2840,7 +3151,7 @@ static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, ems
                        else
                                name = temp_attachment_tbl->attachment_name;
 
-                       if (!attach_part(frame_body, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, false, &error))  {
+                       if (!attach_part(frame_body, (unsigned char *)real_file_path, 0, name, NULL, false, &error))  {
                                EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                goto FINISH_OFF;
                        }
@@ -2850,6 +3161,9 @@ static int attach_attachment_to_body(BODY **multipart_body, BODY *text_body, ems
        ret = true;
 
 FINISH_OFF:
+
+       EM_SAFE_FREE(prefix_path);
+
        if (ret == true)
                *multipart_body = frame_body;
        else if (frame_body != NULL)
@@ -2875,8 +3189,13 @@ static char *emcore_encode_rfc2047_text(char *utf8_text, int *err_code)
 
        EM_DEBUG_FUNC_END();
 
-       if (len > 0)
-               return g_strdup_printf("=?UTF-8?B?%s?=", g_base64_encode((const guchar  *)utf8_text, len));
+
+       if (len > 0) {
+#ifdef __FEATURE_USE_GMIME__
+               return emcore_gmime_get_encoding_to_utf8(utf8_text);
+//              return g_strdup(utf8_text);    /* emoji handle */
+#endif /* __FEATURE_USE_GMIME__ */
+        }
        else
                return strdup("");
 }
@@ -2898,7 +3217,7 @@ static void emcore_encode_rfc2047_address(ADDRESS *address, int *err_code)
 
 #define DATE_STR_LENGTH 100
 
-static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope)
+static int emcore_make_envelope_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, ENVELOPE **output_envelope)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], output_envelope[%p]", input_mail_tbl_data, output_envelope);
 
@@ -2930,7 +3249,7 @@ static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_d
        is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
 
        if (is_incomplete && (input_mail_tbl_data->account_id > 0))  {
-               ref_account = emcore_get_account_reference(input_mail_tbl_data->account_id);
+               ref_account = emcore_get_account_reference(multi_user_name, input_mail_tbl_data->account_id);
                if (!ref_account)  {
                        EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
                        error = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -2950,7 +3269,7 @@ static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_d
 
                        if (input_mail_tbl_data->full_address_from) {
                                char *temp_address_string = NULL ;
-                               em_skip_whitespace(input_mail_tbl_data->full_address_from , &temp_address_string);
+                               em_skip_whitespace_without_alias(input_mail_tbl_data->full_address_from , &temp_address_string);
                                EM_DEBUG_LOG_SEC("address[temp_address_string][%s]", temp_address_string);
                                rfc822_parse_adrlist(&envelope->from, temp_address_string, NULL);
                                EM_SAFE_FREE(temp_address_string);
@@ -2990,12 +3309,13 @@ static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_d
                }
        }
        else  {
+/*
                if (input_mail_tbl_data->full_address_from == NULL)  {
                        EM_DEBUG_EXCEPTION("input_mail_tbl_data->full_address_from[%p]", input_mail_tbl_data->full_address_from);
                        error = EMAIL_ERROR_INVALID_MAIL;
                        goto FINISH_OFF;
                }
-
+*/
                int i, j;
 
                if (input_mail_tbl_data->full_address_from)  {
@@ -3011,7 +3331,7 @@ static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_d
                                        input_mail_tbl_data->full_address_return[i] = ',';
                        }
                }
-               em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
+               em_skip_whitespace_without_alias(input_mail_tbl_data->full_address_from , &pAdd);
                EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
 
                rfc822_parse_adrlist(&envelope->from, pAdd, NULL);
@@ -3092,7 +3412,27 @@ static int emcore_make_envelope_from_mail(emstorage_mail_tbl_t *input_mail_tbl_d
 
        if (!is_incomplete)  {
                char  localtime_string[DATE_STR_LENGTH] = { 0, };
-               strftime(localtime_string, 128, "%a, %e %b %Y %H:%M:%S ", localtime(&input_mail_tbl_data->date_time));
+               time_t tn     = time(0);
+               struct tm *t  = gmtime(&tn);
+               int zone      = t->tm_hour * 60 + t->tm_min;
+               int julian    = t->tm_yday;
+
+               t = localtime(&input_mail_tbl_data->date_time);
+               zone = t->tm_hour * 60 + t->tm_min - zone;
+
+               if ((julian = t->tm_yday - julian))
+                       zone += ((julian < 0) == (abs(julian) == 1)) ? -24 * 60 : 24*60;
+
+               SNPRINTF(localtime_string, DATE_STR_LENGTH, "%s, %d %s %d %02d:%02d:%02d "
+                                                          , days[t->tm_wday]
+                                                          , t->tm_mday
+                                                          , months[t->tm_mon]
+                                                          , t->tm_year + 1900
+                                                          , t->tm_hour
+                                                          , t->tm_min
+                                                          , t->tm_sec
+               );
+
                EM_DEBUG_LOG("localtime string : [%s]", localtime_string);
                /* append last 5byes("+0900") */
                g_strlcat(localtime_string, strchr(rfc822_date_string, '+'), DATE_STR_LENGTH);
@@ -3121,18 +3461,84 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("error [%d]", error);
        return error;
 }
+
+static char *emcore_get_digest_string(int digest_type, int mime_type)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       char *digest_string = NULL;
+       char p_digest_string[100] = {0, };
+
+       switch (mime_type) {
+       case EMAIL_SMIME_SIGNED :
+       case EMAIL_SMIME_ENCRYPTED :
+       case EMAIL_SMIME_SIGNED_AND_ENCRYPTED :
+               memset(p_digest_string, 0x00, sizeof(p_digest_string));
+               break;
+       case EMAIL_PGP_SIGNED :
+       case EMAIL_PGP_ENCRYPTED :
+       case EMAIL_PGP_SIGNED_AND_ENCRYPTED :
+               memset(p_digest_string, 0x00, sizeof(p_digest_string));
+               strcpy(p_digest_string, "pgp-");
+               break;
+       }
+
+       switch (digest_type) {
+       case DIGEST_TYPE_SHA1 :
+               strcat(p_digest_string, "sha1");
+               break;
+       case DIGEST_TYPE_MD5 :
+               strcat(p_digest_string, "md5");
+               break;
+       case DIGEST_TYPE_RIPEMD160 :
+               strcat(p_digest_string, "ripemd160");
+               break;
+       case DIGEST_TYPE_MD2 :
+               strcat(p_digest_string, "md2");
+               break;
+       case DIGEST_TYPE_TIGER192 :
+               strcat(p_digest_string, "tiger192");
+               break;
+       case DIGEST_TYPE_HAVAL5160 :
+               strcat(p_digest_string, "haval5160");
+               break;
+       case DIGEST_TYPE_SHA256 :
+               strcat(p_digest_string, "sha256");
+               break;
+       case DIGEST_TYPE_SHA384 :
+               strcat(p_digest_string, "sha384");
+               break;
+       case DIGEST_TYPE_SHA512 :
+               strcat(p_digest_string, "sha512");
+               break;
+       case DIGEST_TYPE_SHA224 :
+               strcat(p_digest_string, "sha224");
+               break;
+       case DIGEST_TYPE_MD4 :
+               strcat(p_digest_string, "md4");
+               break;
+       }
+
+       digest_string = EM_SAFE_STRDUP(p_digest_string);
+
+       EM_DEBUG_FUNC_END();
+       return digest_string;
+}
+
+
 /*  Description : Make RFC822 text file from mail_tbl data */
 /*  Parameters :  */
 /*                     input_mail_tbl_data :   */
 /*                     is_draft  :  this mail is draft mail. */
 /*                     file_path :  path of file that rfc822 data will be written to. */
-INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_count, ENVELOPE **env, char **file_path, email_option_t *sending_option, int *err_code)
+INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, emstorage_attachment_tbl_t *input_attachment_tbl, int input_attachment_count, ENVELOPE **env, char **file_path, email_option_t *sending_option, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], env[%p], file_path[%p], sending_option[%p], err_code[%p]", input_mail_tbl_data, env, file_path, sending_option, err_code);
 
        int       ret = false;
        int       error = EMAIL_ERROR_NONE;
        int       i = 0;
+       char     *digest_string = NULL;
+
        ENVELOPE *envelope      = NULL;
        BODY     *root_body     = NULL;
        BODY     *text_body     = NULL;
@@ -3140,7 +3546,9 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
        PART     *part_for_alternative = NULL;
        PART     *part_for_related = NULL;
        PARAMETER *param = NULL;
-       char     *fname = NULL;
+
+       char *prefix_path = NULL;
+       char real_file_path[MAX_PATH] = {0};
 
        if (!input_mail_tbl_data)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -3154,7 +3562,7 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                goto FINISH_OFF;
        }
 
-       if ( (error = emcore_make_envelope_from_mail(input_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
+       if ( (error = emcore_make_envelope_from_mail(multi_user_name, input_mail_tbl_data, &envelope)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_make_envelope_from_mail failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -3178,6 +3586,16 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                goto FINISH_OFF;
        }
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        if (input_attachment_count > 0) {
                /* handle the Multipart/mixed, Multipart/related and S/MIME */
                EM_DEBUG_LOG("input_attachment_num [%d]", input_attachment_count);
@@ -3195,51 +3613,50 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
 
                        mail_free_body_parameter(&param);
                        param = NULL;
-
                } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED) {
-                       PARAMETER *protocol_param     = mail_newbody_parameter();
+                       PARAMETER *protocol_param   = mail_newbody_parameter();
 
-                       root_body->type               = TYPEMULTIPART;
-                       root_body->subtype            = strdup("SIGNED");
+                       root_body->type             = TYPEMULTIPART;
+                       root_body->subtype          = strdup("SIGNED");
 
-                       param->attribute       = cpystr("micalg");
-                       switch (input_mail_tbl_data->digest_type) {
-                       case DIGEST_TYPE_SHA1:
-                               param->value   = cpystr("sha1");
-                               break;
-                       case DIGEST_TYPE_MD5:
-                               param->value   = cpystr("md5");
-                               break;
-                       default:
-                               EM_DEBUG_EXCEPTION("Invalid digest type");
-                               break;
-                       }
+                       param->attribute            = cpystr("micalg");
+
+                       digest_string               = emcore_get_digest_string(input_mail_tbl_data->digest_type, input_mail_tbl_data->smime_type);
+                       param->value                = cpystr(digest_string);
 
                        protocol_param->attribute   = cpystr("protocol");
                        protocol_param->value       = cpystr("application/pkcs7-signature");
                        protocol_param->next        = NULL;
                        param->next                 = protocol_param;
-
-                       input_mail_tbl_data->file_path_plain = NULL;
-                       input_mail_tbl_data->file_path_html = NULL;
-
-                       input_attachment_tbl = input_attachment_tbl + (input_attachment_count - 1);
-
-                       input_attachment_count = 1;
-
-               } else {
+               } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED || input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
                        root_body->type    = TYPEAPPLICATION;
                        root_body->subtype = strdup("PKCS7-MIME");
 
                        param->attribute = cpystr("name");
                        param->value = cpystr("smime.p7m");
                        param->next = NULL;
+               } else if (input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED) {
+                       PARAMETER *protocol_param = mail_newbody_parameter();
 
-                       input_mail_tbl_data->file_path_plain = NULL;
-                       input_mail_tbl_data->file_path_html = NULL;
-                       input_mail_tbl_data->file_path_mime_entity = NULL;
+                       root_body->type           = TYPEMULTIPART;
+                       root_body->subtype        = strdup("SIGNED");
 
-                       input_attachment_count = 1;
+                       param->attribute          = cpystr("micalg");
+
+                       digest_string             = emcore_get_digest_string(input_mail_tbl_data->digest_type, input_mail_tbl_data->smime_type);
+                       param->value              = cpystr(digest_string);
+
+                       protocol_param->attribute = cpystr("protocol");
+                       protocol_param->value     = cpystr("application/pgp-signature");
+                       protocol_param->next      = NULL;
+                       param->next               = protocol_param;
+               } else {
+                       root_body->type           = TYPEMULTIPART;
+                       root_body->subtype        = strdup("encrypted");
+
+                       param->attribute          = cpystr("protocol");
+                       param->value              = cpystr("application/pgp-encrypted");
+                       param->next               = NULL;
                }
 
                root_body->contents.text.data = NULL;
@@ -3260,9 +3677,9 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                                }
                        }
 
-                       if (part_for_related) 
+                       if (part_for_related)
                                part_for_alternative = attach_multipart_with_sub_type(&(part_for_related->body), "ALTERNATIVE", &error);
-                       else 
+                       else
                                part_for_alternative = attach_multipart_with_sub_type(root_body, "ALTERNATIVE", &error);
                        if (!part_for_alternative) {
                                EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [alternative] failed [%d]", error);
@@ -3271,9 +3688,12 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
 
                        if (strlen(input_mail_tbl_data->file_path_plain) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
+                
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
 
                                text_body = &(part_for_alternative->body);
-                               if (!attach_part(text_body, (unsigned char *)input_mail_tbl_data->file_path_plain, 0, NULL, "plain", false, &error)) {
+                               if (!attach_part(text_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
                                        EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                        goto FINISH_OFF;
                                }
@@ -3282,19 +3702,25 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                        if (strlen(input_mail_tbl_data->file_path_html) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
 
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
+
                                html_body = &(part_for_alternative->body);
-                               if (!attach_part (html_body, (unsigned char *)input_mail_tbl_data->file_path_html, 0, NULL, "html", false, &error)) {
+                               if (!attach_part (html_body, (unsigned char *)real_file_path, 0, NULL, "html", false, &error)) {
                                        EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                        goto FINISH_OFF;
                                }
                        }
-               } 
-               else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE && 
+               }
+               else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_NONE &&
                                           (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)) {
                        if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
-                               if (!attach_part (root_body, (unsigned char *) input_mail_tbl_data->file_path_plain, 0, 
-                                                                                        NULL, "plain", false, &error)) {
+                
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
+
+                               if (!attach_part (root_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
                                        EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                        goto FINISH_OFF;
                                }
@@ -3302,53 +3728,92 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
 
                        if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0) {
                                EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
-                               if (!attach_part (root_body, (unsigned char *)input_mail_tbl_data->file_path_html, 0, 
+                               if (input_mail_tbl_data->inline_content_count > 0 && (strcasecmp(root_body->subtype, "RELATED") != 0)) {
+                                       part_for_related = attach_multipart_with_sub_type(root_body, "RELATED", &error);
+                                       if (!part_for_related) {
+                                               EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [related] failed [%d]", error);
+                                               goto FINISH_OFF;
+                                       }
+                               }
+
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
+
+                               if (part_for_related) {
+                                       if (!attach_part(&(part_for_related->body), (unsigned char *)real_file_path, 0, 
+                                                                                         NULL, "html", false, &error)) {
+                                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
+                                               goto FINISH_OFF;
+                                       }
+                               } else {
+                                       if (!attach_part (root_body, (unsigned char *)real_file_path, 0, 
                                                                                          NULL, "html", false, &error)) {
+                                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
+                                               goto FINISH_OFF;
+                                       }
+                               }
+                       }
+               } else if (input_mail_tbl_data->smime_type == EMAIL_SMIME_SIGNED || input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED) {
+                       if (input_mail_tbl_data->file_path_mime_entity && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_mime_entity) > 0) {
+                               EM_DEBUG_LOG_SEC("file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
+
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_mime_entity);
+
+                               root_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_mime_entity);
+                       }
+               } else if (input_mail_tbl_data->smime_type == EMAIL_PGP_ENCRYPTED || input_mail_tbl_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
+                       if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0) {
+                               EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
+                               if (!attach_part (root_body, NULL, 0, NULL, "pgp-encrypted", false, &error)) {
                                        EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                        goto FINISH_OFF;
                                }
                        }
-               }
-                else {
-                       EM_DEBUG_LOG("S/MIME type : [%d]", input_mail_tbl_data->smime_type);
-               }
-
-               if (input_mail_tbl_data->file_path_mime_entity && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_mime_entity) > 0) {
-                       EM_DEBUG_LOG_SEC("file_path_mime_entity[%s]", input_mail_tbl_data->file_path_mime_entity);
-                       root_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_mime_entity);
+               } else {
+                       EM_DEBUG_LOG("S/MIME encrypted type");
                }
 
                if (input_attachment_tbl && input_attachment_count) {
                        emstorage_attachment_tbl_t *temp_attachment_tbl = NULL;
                        char *name = NULL;
                        BODY *body_to_attach = NULL;
-                       struct stat st_buf;
 
                        for(i = 0; i < input_attachment_count; i++) {
                                temp_attachment_tbl = input_attachment_tbl + i;
-                               EM_DEBUG_LOG_SEC("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
-                               if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
-                                       if (!temp_attachment_tbl->attachment_name)  {
-                                               if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
-                                                       EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
-                                                       continue;
-                                               }
-                                       }
-                                       else
-                                               name = temp_attachment_tbl->attachment_name;
 
-                                       EM_DEBUG_LOG_SEC("name[%s]", name);
-
-                                       if (temp_attachment_tbl->attachment_inline_content_status && part_for_related)
-                                               body_to_attach = &(part_for_related->body);
-                                       else
-                                               body_to_attach = root_body;
+                               EM_DEBUG_LOG_SEC("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
 
-                                       if (!attach_part(body_to_attach, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, temp_attachment_tbl->attachment_inline_content_status, &error))  {
-                                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
+                               if (!temp_attachment_tbl->attachment_name)  {
+                                       if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
+                                               EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
                                                continue;
                                        }
                                }
+                               else
+                                       name = temp_attachment_tbl->attachment_name;
+
+                               EM_DEBUG_LOG_SEC("name[%s]", name);
+
+                               if (temp_attachment_tbl->attachment_inline_content_status && part_for_related)
+                                       body_to_attach = &(part_for_related->body);
+                               else
+                                       body_to_attach = root_body;
+
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, temp_attachment_tbl->attachment_path);
+
+                               if (!attach_part(body_to_attach,
+                                               (unsigned char *)real_file_path, 
+                                               0, 
+                                               name, 
+                                               temp_attachment_tbl->attachment_mime_type, 
+                                               temp_attachment_tbl->attachment_inline_content_status, 
+                                               &error))
+                               {
+                                       EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
+                                       continue;
+                               }
                        }
                }
                text_body = NULL;
@@ -3369,7 +3834,10 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                if (strlen(input_mail_tbl_data->file_path_plain) > 0) {
                        EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
 
-                       if (!attach_part(root_body, (unsigned char *)input_mail_tbl_data->file_path_plain, 0, NULL, "plain", false, &error)) {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
+
+                       if (!attach_part(root_body, (unsigned char *)real_file_path, 0, NULL, "plain", false, &error)) {
                                EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                goto FINISH_OFF;
                        }
@@ -3378,7 +3846,10 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
                if (strlen(input_mail_tbl_data->file_path_html) > 0) {
                        EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
 
-                       if (!attach_part (root_body, (unsigned char *)input_mail_tbl_data->file_path_html, 0, NULL, "html", false, &error)) {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
+
+                       if (!attach_part (root_body, (unsigned char *)real_file_path, 0, NULL, "html", false, &error)) {
                                EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
                                goto FINISH_OFF;
                        }
@@ -3387,8 +3858,17 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
        else {
                root_body->type = TYPETEXT;
                root_body->encoding = ENC8BIT;
-               if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)
-                       root_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_plain ? input_mail_tbl_data->file_path_plain  :  input_mail_tbl_data->file_path_html);
+               if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html) {
+            if (input_mail_tbl_data->file_path_plain) {
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_plain);
+            } else {
+                memset(real_file_path, 0x00, sizeof(real_file_path));
+                SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_tbl_data->file_path_html);
+            }
+
+                       root_body->sparep = EM_SAFE_STRDUP(real_file_path);
+        }
                else
                        root_body->sparep = NULL;
 
@@ -3410,13 +3890,11 @@ INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_
        if (file_path)  {
                EM_DEBUG_LOG("write rfc822 : file_path[%p]", file_path);
 
-               if ((error = emcore_write_rfc822 (envelope, root_body, input_mail_tbl_data->priority, 
-                                                    input_mail_tbl_data->report_status, &fname)) != EMAIL_ERROR_NONE) {
+               if ((error = emcore_write_rfc822 (envelope, root_body, input_mail_tbl_data->priority,
+                                                    input_mail_tbl_data->report_status, file_path)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_write_rfc822 failed [%d]", error);
                        goto FINISH_OFF;
                }
-
-               *file_path = fname;
        }
 
        ret = true;
@@ -3433,6 +3911,9 @@ FINISH_OFF:
        if (root_body != NULL)
                mail_free_body(&root_body);
 
+       EM_SAFE_FREE(digest_string);
+       EM_SAFE_FREE(prefix_path);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -3440,397 +3921,85 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emcore_make_rfc822_file(email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, char **file_path, int *err_code)
+INTERNAL_FUNC int emcore_make_rfc822_file(char *multi_user_name, email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, int mime_entity_status, char **file_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p], file_path[%p], err_code[%p]", input_mail_tbl_data, file_path, err_code);
 
-       int       ret                  = false;
-       int       error                = EMAIL_ERROR_NONE;
-       int       is_incomplete        = 0;
-       int       i                    = 0;
-       ENVELOPE *envelope             = NULL;
-       BODY     *text_body            = NULL;
-       BODY     *root_body            = NULL;
-       PART     *part_for_html        = NULL;
-       PART     *part_for_text        = NULL;
-       char      temp_file_path_plain[512];
-       char      temp_file_path_html[512];
-       char     *pAdd                 = NULL;
-       char     *fname                = NULL;
-       emstorage_account_tbl_t *ref_account     = NULL;
-
-       if (!input_mail_tbl_data)  {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               error = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       if ( (input_mail_tbl_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0 && !input_mail_tbl_data->body_download_status) {
-               EM_DEBUG_EXCEPTION("input_mail_tbl_data->body_download_status[%p]", input_mail_tbl_data->body_download_status);
-               error = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
+       int err = EMAIL_ERROR_NONE;
+       int ret = false;
+       int i = 0;
+       int attachment_count = 0;
+       int inline_content_count = 0;
 
-       if (!emstorage_get_account_by_id(input_mail_tbl_data->account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &ref_account, true, &error)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", error);
-               goto FINISH_OFF;
-       }
+       emstorage_mail_tbl_t *modified_mail_data = NULL;
+       emstorage_attachment_tbl_t *modified_attachment_data = NULL;
 
-       if (!ref_account)  {
-               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_tbl_data->account_id);
-               error = EMAIL_ERROR_INVALID_ACCOUNT;
+       /* Convert from email_mail_data_t to emstorage_mail_tbl_t */
+       if (!em_convert_mail_data_to_mail_tbl(input_mail_tbl_data, 1, &modified_mail_data, &err)) {
+               EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!(envelope = mail_newenvelope()))  {
-               EM_DEBUG_EXCEPTION("mail_newenvelope failed...");
-               error = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       is_incomplete = input_mail_tbl_data->flags_draft_field || (input_mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SENDING);
-
-       if (is_incomplete)  {
-               if (ref_account->user_email_address && ref_account->user_email_address[0] != '\0')  {
-                       char *p = cpystr(ref_account->user_email_address);
-
-                       if (p == NULL)  {
-                               EM_DEBUG_EXCEPTION("cpystr failed...");
-                               error = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
-
-                       EM_DEBUG_LOG("Assign envelop->from");
-
-                       if (input_mail_tbl_data->full_address_from) {
-                               char *temp_address_string = NULL ;
-                               em_skip_whitespace(input_mail_tbl_data->full_address_from , &temp_address_string);
-                               EM_DEBUG_LOG_SEC("address[temp_address_string][%s]", temp_address_string);
-                               rfc822_parse_adrlist(&envelope->from, temp_address_string, ref_account->outgoing_server_address);
-                               EM_SAFE_FREE(temp_address_string);
-                               temp_address_string = NULL ;
-                       }
-                       else
-                               envelope->from = rfc822_parse_mailbox(&p, NULL);
-
-                       EM_SAFE_FREE(p);
-                       if (!envelope->from)  {
-                               EM_DEBUG_EXCEPTION("rfc822_parse_mailbox failed...");
-                               error = EMAIL_ERROR_INVALID_ADDRESS;
-                               goto FINISH_OFF;
-                       }
-                       else  {
-
-                               if (envelope->from->personal == NULL) {
-                                       envelope->from->personal =
-                                               (ref_account->user_display_name && ref_account->user_display_name[0] != '\0')?
-                                               cpystr(ref_account->user_display_name)  :  NULL;
-                               }
-                       }
-               }
-       }
-
-       if (ref_account->return_address && ref_account->return_address[0] != '\0')  {
-               char *p = cpystr(ref_account->return_address);
-
-               if (p == NULL)  {
-                       EM_DEBUG_EXCEPTION("cpystr failed...");
-
-                       error = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-               envelope->return_path = rfc822_parse_mailbox(&p, NULL);
-               EM_SAFE_FREE(p);
-       }
-       else  {
-               if (!input_mail_tbl_data->full_address_from || !input_mail_tbl_data->full_address_to)  {
-                       EM_DEBUG_EXCEPTION("input_mail_tbl_data->full_address_from[%p], input_mail_tbl_data->full_address_to[%p]", input_mail_tbl_data->full_address_from, input_mail_tbl_data->full_address_to);
-                       error = EMAIL_ERROR_INVALID_MAIL;
-                       goto FINISH_OFF;
-               }
-
-               int i, j;
-
-               if (input_mail_tbl_data->full_address_from)  {
-                       for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_from); i < j; i++)  {
-                               if (input_mail_tbl_data->full_address_from[i] == ';')
-                                       input_mail_tbl_data->full_address_from[i] = ',';
-                       }
-               }
-
-               if (input_mail_tbl_data->full_address_return)  {
-                       for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_return); i < j; i++)  {
-                               if (input_mail_tbl_data->full_address_return[i] == ';')
-                                       input_mail_tbl_data->full_address_return[i] = ',';
-                       }
-               }
-                       em_skip_whitespace(input_mail_tbl_data->full_address_from , &pAdd);
-               EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
-
-               rfc822_parse_adrlist(&envelope->from, pAdd, ref_account->outgoing_server_address);
-               EM_SAFE_FREE(pAdd);
-                       pAdd = NULL ;
-
-               em_skip_whitespace(input_mail_tbl_data->full_address_return , &pAdd);
-               EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
-
-               rfc822_parse_adrlist(&envelope->return_path, pAdd, ref_account->outgoing_server_address);
-               EM_SAFE_FREE(pAdd);
-               pAdd = NULL ;
-       }
-
-       {
-               int i, j;
-
-               if (input_mail_tbl_data->full_address_to)  {
-                       for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_to); i < j; i++)  {
-                               if (input_mail_tbl_data->full_address_to[i] == ';')
-                                       input_mail_tbl_data->full_address_to[i] = ',';
-                       }
-               }
-
-               if (input_mail_tbl_data->full_address_cc)  {
-                       for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_cc); i < j; i++)  {
-                               if (input_mail_tbl_data->full_address_cc[i] == ';')
-                                       input_mail_tbl_data->full_address_cc[i] = ',';
-                       }
-               }
-
-               if (input_mail_tbl_data->full_address_bcc)  {
-                       for (i = 0, j = EM_SAFE_STRLEN(input_mail_tbl_data->full_address_bcc); i < j; i++)  {
-                               if (input_mail_tbl_data->full_address_bcc[i] == ';')
-                                       input_mail_tbl_data->full_address_bcc[i] = ',';
-                       }
-               }
-       }
-
-       em_skip_whitespace(input_mail_tbl_data->full_address_to , &pAdd);
-       EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
-
-       rfc822_parse_adrlist(&envelope->to, pAdd, ref_account->outgoing_server_address);
-       EM_SAFE_FREE(pAdd);
-       pAdd = NULL ;
-
-       EM_DEBUG_LOG_SEC("address[input_mail_tbl_data->full_address_cc][%s]", input_mail_tbl_data->full_address_cc);
-       em_skip_whitespace(input_mail_tbl_data->full_address_cc , &pAdd);
-       EM_DEBUG_LOG_SEC("address[pAdd][%s]", pAdd);
-
-       rfc822_parse_adrlist(&envelope->cc, pAdd, ref_account->outgoing_server_address);
-       EM_SAFE_FREE(pAdd);
-               pAdd = NULL ;
-
-       em_skip_whitespace(input_mail_tbl_data->full_address_bcc , &pAdd);
-       rfc822_parse_adrlist(&envelope->bcc, pAdd, ref_account->outgoing_server_address);
-       EM_SAFE_FREE(pAdd);
-               pAdd = NULL ;
-
-       emcore_encode_rfc2047_address(envelope->return_path, &error);
-       emcore_encode_rfc2047_address(envelope->from, &error);
-       emcore_encode_rfc2047_address(envelope->sender, &error);
-       emcore_encode_rfc2047_address(envelope->reply_to, &error);
-       emcore_encode_rfc2047_address(envelope->to, &error);
-       emcore_encode_rfc2047_address(envelope->cc, &error);
-       emcore_encode_rfc2047_address(envelope->bcc, &error);
-
-       if (input_mail_tbl_data->subject)
-               envelope->subject = emcore_encode_rfc2047_text(input_mail_tbl_data->subject, &error);
+       if (mime_entity_status)
+               modified_mail_data->smime_type = EMAIL_SMIME_NONE;
 
-       char rfc822_date_string[DATE_STR_LENGTH] = { 0, };
-       char localtime_string[DATE_STR_LENGTH] = {0, };
-
-       rfc822_date(rfc822_date_string);
-
-       if (!is_incomplete)  {
-               strftime(localtime_string, 128, "%a, %e %b %Y %H : %M : %S ", localtime(&input_mail_tbl_data->date_time));
-               /*  append last 5byes("+0900") */
-               g_strlcat(localtime_string, rfc822_date_string + (EM_SAFE_STRLEN(rfc822_date_string) -  5), DATE_STR_LENGTH);
-               envelope->date = (unsigned char *)cpystr((const char *)localtime_string);
-       }
-       else {
-               envelope->date = (unsigned char *)cpystr((const char *)rfc822_date_string);
-       }
-       /*  check report input_mail_tbl_data */
-
-       /* Non-report input_mail_tbl_data */
-       EM_DEBUG_LOG_SEC("input_mail_tbl_data->file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
-       EM_DEBUG_LOG_SEC("input_mail_tbl_data->file_path_html[%s]", input_mail_tbl_data->file_path_html);
-       EM_DEBUG_LOG("input_mail_tbl_data->body->attachment_num[%d]", input_mail_tbl_data->attachment_count);
-
-       if (input_mail_tbl_data->file_path_plain) {
-               memset(temp_file_path_plain, 0x00, sizeof(temp_file_path_plain));
-               SNPRINTF(temp_file_path_plain, sizeof(temp_file_path_plain), "%s%s%s", MAILTEMP, DIR_SEPERATOR, "UTF-8");
-
-               if (!emstorage_copy_file(input_mail_tbl_data->file_path_plain, temp_file_path_plain, 0, &error)) {
-                       EM_DEBUG_EXCEPTION("emstorage_copy_file failed : [%d]", error);
-                       goto FINISH_OFF;
-               }
-       }
-
-       if (input_mail_tbl_data->file_path_html) {
-               memset(temp_file_path_html, 0x00, sizeof(temp_file_path_html));
-               SNPRINTF(temp_file_path_html, sizeof(temp_file_path_html), "%s%s%s", MAILTEMP, DIR_SEPERATOR, "UTF-8.htm");
-
-               if (!emstorage_copy_file(input_mail_tbl_data->file_path_html, temp_file_path_html, 0, &error)) {
-                       EM_DEBUG_EXCEPTION("emstorage_copy_file failed : [%d]", error);
+       /* Convert from email_attachment_data_t to emstorage_attachment_tbl_t */
+       if (input_attachment_count > 0) {
+               modified_attachment_data = em_malloc(sizeof(emstorage_attachment_tbl_t) * input_attachment_count);
+               if (modified_attachment_data == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
                        goto FINISH_OFF;
                }
        }
 
-       if ((input_mail_tbl_data->attachment_count > 0) || (input_mail_tbl_data->file_path_plain && input_mail_tbl_data->file_path_html))  {
-               EM_DEBUG_LOG("attachment_num  :  %d", input_mail_tbl_data->attachment_count);
-               root_body = mail_newbody();
-
-               if (root_body == NULL)  {
-                       EM_DEBUG_EXCEPTION("mail_newbody failed...");
-                       error = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-
-               root_body->type               = TYPEMULTIPART;
-               root_body->subtype            = strdup("MIXED");
-               root_body->contents.text.data = NULL;
-               root_body->contents.text.size = 0;
-               root_body->size.bytes         = 0;
+       for (i = 0; i < input_attachment_count; i++) {
+               modified_attachment_data[i].attachment_id                    = input_attachment_tbl[i].attachment_id;
+               modified_attachment_data[i].attachment_name                  = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_name);
+               modified_attachment_data[i].attachment_path                  = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_path);
+               modified_attachment_data[i].content_id                       = EM_SAFE_STRDUP(input_attachment_tbl[i].content_id);
+               modified_attachment_data[i].attachment_id                    = input_attachment_tbl[i].attachment_size;
+               modified_attachment_data[i].mail_id                          = input_attachment_tbl[i].mail_id;
+               modified_attachment_data[i].account_id                       = input_attachment_tbl[i].account_id;
+               modified_attachment_data[i].mailbox_id                       = input_attachment_tbl[i].mailbox_id;
+               modified_attachment_data[i].attachment_save_status           = input_attachment_tbl[i].save_status;
+               modified_attachment_data[i].attachment_drm_type              = input_attachment_tbl[i].drm_status;
+               modified_attachment_data[i].attachment_inline_content_status = input_attachment_tbl[i].inline_content_status;
+               modified_attachment_data[i].attachment_mime_type             = EM_SAFE_STRDUP(input_attachment_tbl[i].attachment_mime_type);
 
-               part_for_text = attach_multipart_with_sub_type(root_body, "ALTERNATIVE", &error);
-
-               if (!part_for_text) {
-                       EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [part_for_text] failed [%d]", error);
-                       goto FINISH_OFF;
-               }
-
-               text_body = &part_for_text->body;
-
-               if (input_mail_tbl_data->file_path_plain && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_plain) > 0)  {
-                       EM_DEBUG_LOG_SEC("file_path_plain[%s]", input_mail_tbl_data->file_path_plain);
-                       if (!attach_part(text_body, (unsigned char *)temp_file_path_plain, 0, NULL, NULL, false, &error))  {
-                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
-                               goto FINISH_OFF;
-                       }
-               }
-
-               if (input_mail_tbl_data->file_path_html && EM_SAFE_STRLEN(input_mail_tbl_data->file_path_html) > 0)  {
-                       EM_DEBUG_LOG_SEC("file_path_html[%s]", input_mail_tbl_data->file_path_html);
-
-                       part_for_html = attach_multipart_with_sub_type(text_body, "RELATED", &error);
-                       if (!part_for_html) {
-                               EM_DEBUG_EXCEPTION("attach_multipart_with_sub_type [part_for_html] failed [%d]", error);
-                               goto FINISH_OFF;
-                       }
-
-                       if (!attach_part(&(part_for_html->body) , (unsigned char *)temp_file_path_html, 0, NULL, "html", false, &error))  {
-                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
-                               goto FINISH_OFF;
-                       }
-               }
-
-               if (input_attachment_tbl && input_attachment_count)  {
-                       email_attachment_data_t *temp_attachment_tbl = NULL;
-                       char *name = NULL;
-                       BODY *body_to_attach = NULL;
-                       struct stat st_buf;
-
-                       for(i = 0; i < input_attachment_count; i++) {
-                               temp_attachment_tbl = input_attachment_tbl + i;
-                               EM_DEBUG_LOG_SEC("attachment_name[%s], attachment_path[%s]", temp_attachment_tbl->attachment_name, temp_attachment_tbl->attachment_path);
-                               if (stat(temp_attachment_tbl->attachment_path, &st_buf) == 0)  {
-                                       if (!temp_attachment_tbl->attachment_name)  {
-                                               if (!emcore_get_file_name(temp_attachment_tbl->attachment_path, &name, &error))  {
-                                                       EM_DEBUG_EXCEPTION("emcore_get_file_name failed [%d]", error);
-                                                       continue;
-                                               }
-                                       }
-                                       else
-                                               name = temp_attachment_tbl->attachment_name;
-                                       EM_DEBUG_LOG_SEC("name[%s]", name);
-
-                                       if (temp_attachment_tbl->inline_content_status && part_for_html)
-                                               body_to_attach = &(part_for_html->body);
-                                       else
-                                               body_to_attach = root_body;
-
-                                       if (!attach_part(body_to_attach, (unsigned char *)temp_attachment_tbl->attachment_path, 0, name, NULL, temp_attachment_tbl->inline_content_status, &error))  {
-                                               EM_DEBUG_EXCEPTION("attach_part failed [%d]", error);
-                                               continue;
-                                       }
-                               }
-                       }
-               }
-               text_body = NULL;
-       } 
-       else  {
-               text_body = mail_newbody();
-
-               if (text_body == NULL)  {
-                       EM_DEBUG_EXCEPTION("mail_newbody failed...");
-
-                       error = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-
-               text_body->type = TYPETEXT;
-               text_body->encoding = ENC8BIT;
-               if (input_mail_tbl_data->file_path_plain || input_mail_tbl_data->file_path_html)
-                       text_body->sparep = EM_SAFE_STRDUP(input_mail_tbl_data->file_path_plain ? temp_file_path_plain  :  temp_file_path_html);
-               else
-                       text_body->sparep = NULL;
-
-               if (input_mail_tbl_data->file_path_html != NULL && input_mail_tbl_data->file_path_html[0] != '\0')
-                       text_body->subtype = strdup("html");
-               if (text_body->sparep)
-                       text_body->size.bytes = EM_SAFE_STRLEN(text_body->sparep);
+               if (input_attachment_tbl[i].inline_content_status == INLINE_ATTACHMENT)
+                       inline_content_count += 1;
                else
-                       text_body->size.bytes = 0;
-       }
-
-       if (file_path)  {
-               if ((error = emcore_write_rfc822 (envelope, (root_body? root_body: text_body), input_mail_tbl_data->priority,
-                                                   input_mail_tbl_data->report_status, &fname)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emcore_write_rfc822 failed [%d]", error);
-                       goto FINISH_OFF;
-               }
-
-               *file_path = fname;
-       }
-
-       if (EM_SAFE_STRLEN(temp_file_path_plain) > 0)  {
-               if (!emstorage_delete_file(temp_file_path_plain, &error))  {
-                       EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", error);
-                       goto FINISH_OFF;
-               }
+                       attachment_count += 1;
        }
 
-       if (EM_SAFE_STRLEN(temp_file_path_html) > 0) {
-               if (!emstorage_delete_file(temp_file_path_html, &error))  {
-                       EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", error);
-                       goto FINISH_OFF;
-               }
+       modified_mail_data->attachment_count = attachment_count;
+       modified_mail_data->inline_content_count = inline_content_count;        
+       
+       if (!emcore_make_rfc822_file_from_mail(multi_user_name, modified_mail_data, modified_attachment_data, input_attachment_count, NULL, file_path, NULL, &err)) {
+               EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed [%d]", err);
+               goto FINISH_OFF;
        }
 
        ret = true;
 
 FINISH_OFF:
 
-       emstorage_free_account(&ref_account, 1, NULL);
+       if (modified_mail_data)
+               emstorage_free_mail(&modified_mail_data, 1, NULL);
 
-       if (envelope != NULL)
-               mail_free_envelope(&envelope);
+       if (modified_attachment_data)
+               emstorage_free_attachment(&modified_attachment_data, input_attachment_count, NULL);
 
-       if (text_body != NULL)
-               mail_free_body(&text_body);
-
-       if (root_body != NULL)
-               mail_free_body(&root_body);
-
-       if (err_code != NULL)
-               *err_code = error;
+       if (err_code)
+               *err_code = err;
 
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
 #ifdef __FEATURE_SUPPORT_REPORT_MAIL__
-static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body, int *err_code)
+static int emcore_get_report_mail_body(char *multi_user_name, ENVELOPE *envelope, BODY **multipart_body, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("envelope[%p], mulitpart_body[%p], err_code[%p]", envelope, multipart_body, err_code);
 
@@ -3864,9 +4033,9 @@ static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body
                goto FINISH_OFF;
        }
 
-       if (!(fp = fopen(fname, "wb+")))  {
-               EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       err = em_fopen(fname, "wb+", &fp);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed - %s, error : [%d]", fname, err);
                goto FINISH_OFF;
        }
 
@@ -3907,9 +4076,9 @@ static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body
                goto FINISH_OFF;
        }
 
-       if (!(fp = fopen(fname, "wb+")))  {
-               EM_DEBUG_EXCEPTION(" fopen failed - %s", fname);
-               err = EMAIL_ERROR_SYSTEM_FAILURE;               /* EMAIL_ERROR_UNKNOWN; */
+       err = em_fopen(fname, "wb+", &fp);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed - %s, error : [%d]", fname, err);
                goto FINISH_OFF;
        }
 
@@ -3934,7 +4103,7 @@ static int emcore_get_report_mail_body(ENVELOPE *envelope, BODY **multipart_body
                goto FINISH_OFF;
        }
 
-       if (!attach_attachment_to_body(&m_body, text_body, &temp_attachment_tbl, 1, &err))  {
+       if (!attach_attachment_to_body(multi_user_name, &m_body, text_body, &temp_attachment_tbl, 1, &err))  {
                EM_DEBUG_EXCEPTION(" attach_attachment_to_body failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4009,11 +4178,9 @@ INTERNAL_FUNC int emcore_get_body_buff(char *file_path, char **buff)
        EM_DEBUG_FUNC_BEGIN();
 
        FILE *r_fp = NULL;
-       int read_size = 0;
        int ret = false;
        char *read_buff = NULL;
 
-
        if (file_path)
                r_fp = fopen(file_path, "r");
 
@@ -4026,7 +4193,6 @@ INTERNAL_FUNC int emcore_get_body_buff(char *file_path, char **buff)
        if (stat(file_path, &stbuf) == 0 && stbuf.st_size > 0) {
                EM_DEBUG_LOG(" File Size [ %d ] ", stbuf.st_size);
                read_buff = calloc((stbuf.st_size+ 1), sizeof(char));
-               read_size = fread(read_buff, 1, stbuf.st_size, r_fp);
                read_buff[stbuf.st_size] = '\0';
        }
 
@@ -4045,7 +4211,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int emcore_copy_attachment_from_original_mail(int input_original_mail_id, int input_target_mail_id)
+static int emcore_copy_attachment_from_original_mail(char *multi_user_name, int input_original_mail_id, int input_target_mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_original_mail_id[%d] input_target_mail_id[%d]", input_original_mail_id, input_target_mail_id);
        int err = EMAIL_ERROR_NONE;
@@ -4055,16 +4221,17 @@ static int emcore_copy_attachment_from_original_mail(int input_original_mail_id,
        int attachment_id = 0;
        char output_file_name[MAX_PATH] = { 0, };
        char output_file_path[MAX_PATH] = { 0, };
+       char virtual_file_path[MAX_PATH] = { 0, };
        emstorage_attachment_tbl_t *original_mail_attachment_array = NULL;
        emstorage_attachment_tbl_t *target_mail_attachment_array = NULL;
        emstorage_attachment_tbl_t *target_attach = NULL;
 
-       if((err = emstorage_get_attachment_list(input_original_mail_id, false, &original_mail_attachment_array, &original_mail_attachment_count)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_get_attachment_list(multi_user_name, input_original_mail_id, false, &original_mail_attachment_array, &original_mail_attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if((err = emstorage_get_attachment_list(input_target_mail_id, false, &target_mail_attachment_array, &target_mail_attachment_count)) != EMAIL_ERROR_NONE) {
+       if((err = emstorage_get_attachment_list(multi_user_name, input_target_mail_id, false, &target_mail_attachment_array, &target_mail_attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4082,19 +4249,28 @@ static int emcore_copy_attachment_from_original_mail(int input_original_mail_id,
 
                                EM_DEBUG_LOG("attachment_inline_content_status [%d] attachment_id[%d]", target_attach->attachment_inline_content_status, attachment_id);
 
-                               if(!emcore_save_mail_file(target_attach->account_id, target_attach->mail_id, attachment_id, original_mail_attachment_array[i].attachment_path, original_mail_attachment_array[i].attachment_name, output_file_path, &err)) {
+                               if(!emcore_save_mail_file(multi_user_name, 
+                                            target_attach->account_id, 
+                                            target_attach->mail_id, 
+                                            attachment_id, 
+                                            original_mail_attachment_array[i].attachment_path, 
+                                            original_mail_attachment_array[i].attachment_name, 
+                                            output_file_path, 
+                                            virtual_file_path,                  
+                                            &err)) {
                                        EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
                                        goto FINISH_OFF;
                                }
 
                                EM_SAFE_FREE(target_attach->attachment_path);
-                               target_attach->attachment_path = EM_SAFE_STRDUP(output_file_path);
+                               target_attach->attachment_path = EM_SAFE_STRDUP(virtual_file_path);
                                target_attach->attachment_save_status = 1;
 
-                               if(!emstorage_update_attachment(target_attach, false, &err)) {
+                               if(!emstorage_update_attachment(multi_user_name, target_attach, false, &err)) {
                                        EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
                                        goto FINISH_OFF;
                                }
+
                                memset(output_file_path, 0, MAX_PATH);
                                memset(output_file_name, 0, MAX_PATH);
                                break;
@@ -4114,7 +4290,7 @@ FINISH_OFF:
 }
 
 /*  send a mail */
-INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id)
+INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(char *multi_user_name, int input_mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d]", input_mail_id);
        int err = EMAIL_ERROR_NONE;
@@ -4125,7 +4301,7 @@ INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(
        email_attachment_data_t *attachment_array = NULL;
 
        /* Get mail data */
-       if((err = emcore_get_mail_data(input_mail_id, &mail_to_be_sent)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_mail_data(multi_user_name, input_mail_id, &mail_to_be_sent)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4137,13 +4313,13 @@ INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(
        }
 
        /* Get original mail data */
-       if((err = emcore_get_mail_data(mail_to_be_sent->reference_mail_id, &original_mail)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_mail_data(multi_user_name, mail_to_be_sent->reference_mail_id, &original_mail)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Check necessity of download */
-       if((err = emcore_get_attachment_data_list(original_mail->mail_id, &attachment_array, &attachment_count)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_get_attachment_data_list(multi_user_name, original_mail->mail_id, &attachment_array, &attachment_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4151,21 +4327,30 @@ INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(
        /* If need be, download attachments */
        for(i = 0; i < attachment_count; i++) {
                if(attachment_array[i].save_status != 1) {
-                       if(!emcore_download_attachment(original_mail->account_id, original_mail->mail_id, i + 1, &err)) {
+                       /* this function is not run by event thread,
+                       so cancellable should be set to 0*/
+#ifdef __FEATURE_USE_GMIME__
+                       if(!emcore_gmime_download_attachment (multi_user_name, original_mail->mail_id, i + 1, 0, -1, 0, &err)) {
+                               EM_DEBUG_EXCEPTION("emcore_download_attachment failed [%d]", err);
+                               goto FINISH_OFF;
+                       }
+#else
+                       if(!emcore_download_attachment (original_mail->account_id, original_mail->mail_id, i + 1, 0, -1, &err)) {
                                EM_DEBUG_EXCEPTION("emcore_download_attachment failed [%d]", err);
                                goto FINISH_OFF;
                        }
+#endif
                }
        }
 
        /* Copy attachment to the mail to be sent */
-       if((err = emcore_copy_attachment_from_original_mail(original_mail->mail_id, mail_to_be_sent->mail_id)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_copy_attachment_from_original_mail(multi_user_name, original_mail->mail_id, mail_to_be_sent->mail_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_download_attachment failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Send the mail */
-       if(!emcore_send_mail(mail_to_be_sent->mail_id, &err)) {
+       if(!emcore_send_mail(multi_user_name, mail_to_be_sent->mail_id, &err)) {
                EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4191,20 +4376,20 @@ FINISH_OFF:
 
 /* Scheduled sending ------------------------------------------------ */
 
-static int emcore_sending_alarm_cb(int input_alarm_id, void *user_parameter)
+static int emcore_sending_alarm_cb(email_alarm_data_t *alarm_data, void *user_parameter)
 {
-       EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] user_parameter [%p]", input_alarm_id, user_parameter);
+       EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_parameter [%p]", alarm_data, user_parameter);
        int err = EMAIL_ERROR_NONE;
        int ret = 0;
-       email_alarm_data_t *alarm_data = NULL;
+       char *multi_user_name = (char *)user_parameter;
 
-       if (((err = emcore_get_alarm_data_by_alarm_id(input_alarm_id, &alarm_data)) != EMAIL_ERROR_NONE) || alarm_data == NULL) {
-               EM_DEBUG_EXCEPTION("emcore_get_alarm_data_by_alarm_id failed [%d]", err);
+       if (alarm_data == NULL) {
+               EM_DEBUG_EXCEPTION("Invalid parameter");
                goto FINISH_OFF;
        }
 
        /* send mail here */
-       if(!emcore_send_mail(alarm_data->reference_id, &err)) {
+       if(!emcore_send_mail(multi_user_name, alarm_data->reference_id, &err)) {
                EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", ret);
                goto FINISH_OFF;
        }
@@ -4216,32 +4401,32 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emcore_schedule_sending_mail(int input_mail_id, time_t input_time_to_send)
+INTERNAL_FUNC int emcore_schedule_sending_mail(char *multi_user_name, int input_mail_id, time_t input_time_to_send)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_time_to_send[%d]", input_mail_id, input_time_to_send);
        int err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t *mail_data = NULL;
 
        /* get mail data */
-       if (!emstorage_get_mail_by_id(input_mail_id, &mail_data, true, &err) || mail_data == NULL) {
+       if (!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_data, true, &err) || mail_data == NULL) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /*
-       if (!emstorage_set_field_of_mails_with_integer_value(mail_data->account_id, &(mail_data->mail_id), 1, "save_status", EMAIL_MAIL_STATUS_SEND_SCHEDULED, true, &err)) {
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_data->account_id, &(mail_data->mail_id), 1, "save_status", EMAIL_MAIL_STATUS_SEND_SCHEDULED, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
                goto FINISH_OFF;
        }
        */
 
-       if (!emstorage_set_field_of_mails_with_integer_value(mail_data->account_id, &(mail_data->mail_id), 1, "scheduled_sending_time", input_time_to_send, true, &err)) {
+       if (!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_data->account_id, &(mail_data->mail_id), 1, "scheduled_sending_time", input_time_to_send, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
                goto FINISH_OFF;
        }
 
        /* add alarm */
-       if ((err = emcore_add_alarm(input_time_to_send, EMAIL_ALARM_CLASS_SCHEDULED_SENDING, input_mail_id, emcore_sending_alarm_cb, NULL)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_add_alarm(multi_user_name, input_time_to_send, EMAIL_ALARM_CLASS_SCHEDULED_SENDING, input_mail_id, emcore_sending_alarm_cb, NULL)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -4257,9 +4442,9 @@ FINISH_OFF:
 
 #ifdef __FEATURE_AUTO_RETRY_SEND__
 
-static int emcore_auto_resend_cb(int input_alarm_id, void *user_parameter)
+static int emcore_auto_resend_cb(email_alarm_data_t *alarm_data, void *user_parameter)
 {
-       EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] user_parameter [%p]", input_alarm_id, user_parameter);
+       EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_parameter [%p]", alarm_data, user_parameter);
        int err = EMAIL_ERROR_NONE;
        char *conditional_clause_string = NULL;
        char *attribute_field_name = NULL;
@@ -4270,6 +4455,7 @@ static int emcore_auto_resend_cb(int input_alarm_id, void *user_parameter)
        int sorting_rule_count = 2;
        int result_mail_count = 0;
        int i = 0;
+       char *multi_user_name = (char *)user_parameter;
 
        /* Get all mails have remaining resend counts in outbox with status 'EMAIL_MAIL_STATUS_SEND_FAILURE or EMAIL_MAIL_STATUS_SEND_WAIT' */
        attribute_field_name = emcore_get_mail_field_name_by_attribute_type(EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES);
@@ -4304,14 +4490,14 @@ static int emcore_auto_resend_cb(int input_alarm_id, void *user_parameter)
        sorting_rule_list[1].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_MAIL_ID;
        sorting_rule_list[1].sort_order                                    = EMAIL_SORT_ORDER_ASCEND;
 
-       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if(!emstorage_query_mail_list(conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
+       if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
                EM_DEBUG_LOG("There is no mails to be sent [%d]", err);
                goto FINISH_OFF;
        }
@@ -4319,33 +4505,32 @@ static int emcore_auto_resend_cb(int input_alarm_id, void *user_parameter)
        /* Send mails in loop */
 
        for(i = 0; i < result_mail_count; i++) {
-               if(!emcore_send_mail(result_mail_list[i].mail_id, &err)) {
+               if(!emcore_send_mail(multi_user_name, result_mail_list[i].mail_id, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
                }
 
                if(attribute_field_name) {
-                       if(!emstorage_set_field_of_mails_with_integer_value(result_mail_list[i].account_id, &(result_mail_list[i].mail_id), 1, attribute_field_name, result_mail_list[i].remaining_resend_times - 1, true, &err)) {
+                       if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, result_mail_list[i].account_id, &(result_mail_list[i].mail_id), 1, attribute_field_name, result_mail_list[i].remaining_resend_times - 1, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
                        }
                }
        }
 
 FINISH_OFF:
-
+       EM_SAFE_FREE (conditional_clause_string); /* detected by valgrind */
        EM_SAFE_FREE(result_mail_list);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(int input_alarm_interval_in_second)
+INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(char *multi_user_name, int input_alarm_interval_in_second)
 {
        EM_DEBUG_FUNC_BEGIN("input_alarm_interval_in_second[%d]", input_alarm_interval_in_second);
        int err = EMAIL_ERROR_NONE;
        time_t current_time;
        time_t trigger_at_time;
        char *conditional_clause_string = NULL;
-       char *attribute_field_name = NULL;
        email_list_filter_t filter_list[5];
        email_mail_list_item_t *result_mail_list = NULL;
        email_list_sorting_rule_t sorting_rule_list[2];
@@ -4366,7 +4551,6 @@ INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(int input_alarm_interval_i
                goto FINISH_OFF;
        }
        /* Get all mails have remaining resend counts in outbox with status 'EMAIL_MAIL_STATUS_SEND_FAILURE or EMAIL_MAIL_STATUS_SEND_WAIT' */
-       attribute_field_name = emcore_get_mail_field_name_by_attribute_type(EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES);
 
        memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
        memset(sorting_rule_list, 0 , sizeof(email_list_sorting_rule_t) * sorting_rule_count);
@@ -4398,14 +4582,14 @@ INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(int input_alarm_interval_i
        sorting_rule_list[1].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_MAIL_ID;
        sorting_rule_list[1].sort_order                                    = EMAIL_SORT_ORDER_ASCEND;
 
-       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, sorting_rule_list, sorting_rule_count, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if(!emstorage_query_mail_list(conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
+       if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
                if (err == EMAIL_ERROR_MAIL_NOT_FOUND)
                        EM_DEBUG_LOG ("no mail found");
                else
@@ -4419,7 +4603,7 @@ INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(int input_alarm_interval_i
 
                trigger_at_time = current_time + input_alarm_interval_in_second;
 
-               if ((err = emcore_add_alarm(trigger_at_time, EMAIL_ALARM_CLASS_AUTO_RESEND, 0, emcore_auto_resend_cb, NULL)) != EMAIL_ERROR_NONE) {
+               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) {
                        EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
                        goto FINISH_OFF;
                }
diff --git a/email-core/email-core-sound.c b/email-core/email-core-sound.c
deleted file mode 100755 (executable)
index 5bf7f98..0000000
+++ /dev/null
@@ -1,343 +0,0 @@
-/*
-*  email-service
-*
-* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
-*
-* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-*/
-
-
-#include <stdlib.h>
-#include <pthread.h>
-
-#include "email-debug-log.h"
-#include "email-storage.h"
-#include "email-core-utils.h"
-#include "email-core-mailbox.h"
-#include "email-core-sound.h"
-#include "email-core-alarm.h"
-#include "email-utilities.h"
-#include "email-daemon.h"
-
-#define TIMER 30000   // 30 seconds
-#define HAPTIC_TEST_ITERATION 1
-#define EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE -1
-
-static int emcore_get_alert_type(int vip_mode);
-static int emcore_alarm_timeout_cb_for_alert(int timer_id, void *user_parm);
-static int emcore_set_alarm_for_alert(int alert_time_in_minute);
-static void emcore_set_repetition_alarm(int repetition);
-static char * emcore_get_sound_file_path(int vip_mode);
-
-
-static int emcore_get_alert_type(int vip_mode)
-{
-       EM_DEBUG_FUNC_BEGIN();
-       int global_sound_status = 0;
-       int global_vibe_status = 0;
-       int email_vibe_status = 0;
-       int call_state = 0;
-       int alert_type = EMAIL_ALERT_TYPE_MUTE;
-       int voicerecoder_state = 0;
-
-       if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &global_sound_status) != 0) {
-               EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_SOUND_STATUS_BOOL failed");
-               goto FINISH_OFF;
-       }
-
-       if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &global_vibe_status) != 0) {
-               EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOLfailed");
-               goto FINISH_OFF;
-       }
-
-       EM_DEBUG_LOG("global_sound_status [%d] global_vibe_status [%d]", global_sound_status, global_vibe_status);
-
-       if (global_sound_status || global_vibe_status) {
-
-               if (vconf_get_int(VCONFKEY_VOICERECORDER_STATE, &voicerecoder_state) != 0) {
-                       EM_DEBUG_LOG("vconf_get_int for VCONFKEY_VOICERECORDER_STATE failed");
-               }
-               EM_DEBUG_LOG("voicerecoder_state [%d]", voicerecoder_state);
-
-               if (vconf_get_int(VCONFKEY_CALL_STATE, &call_state) != 0) {
-                       EM_DEBUG_LOG("vconf_get_int for VCONFKEY_CALL_STATE failed");
-               }
-               EM_DEBUG_LOG("call_state [%d] ", call_state);
-
-               if (vip_mode) {
-                       if (vconf_get_bool(VCONF_VIP_NOTI_VIBRATION_STATUS_BOOL, &email_vibe_status) != 0) {
-                               EM_DEBUG_EXCEPTION("vconf_get_bool failed");
-                               return EMAIL_ALERT_TYPE_NONE;
-                       }
-               }
-               else {
-                       if (vconf_get_bool(VCONF_EMAIL_NOTI_VIBRATION_STATUS_BOOL, &email_vibe_status) != 0) {
-                               EM_DEBUG_EXCEPTION("vconf_get_bool failed");
-                               return EMAIL_ALERT_TYPE_NONE;
-                       }
-               }
-
-               EM_DEBUG_LOG("email_vibe_status [%d] ", email_vibe_status);
-
-               if (voicerecoder_state == VCONFKEY_VOICERECORDER_RECORDING) {
-                       alert_type = EMAIL_ALERT_TYPE_VIB;
-                       EM_DEBUG_LOG("voice recorder is on recording...");
-               }
-               else if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {
-                       EM_DEBUG_LOG("Calling");
-#ifdef FEATURE_OPCO_DOCOMO
-                       alert_type = EMAIL_ALERT_TYPE_NONE;
-#else
-                       alert_type = EMAIL_ALERT_TYPE_VIB;
-#endif
-               }
-               else if (global_sound_status && email_vibe_status) {
-                       alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
-               }
-               else if (global_sound_status) {
-                       alert_type = EMAIL_ALERT_TYPE_MELODY;
-               }
-               else if (global_vibe_status) {
-                       alert_type = EMAIL_ALERT_TYPE_VIB;
-               }
-               else {
-                       alert_type = EMAIL_ALERT_TYPE_MUTE;
-               }
-       }
-
-FINISH_OFF:
-       EM_DEBUG_FUNC_END("alert_type [%d]", alert_type);
-       return alert_type;
-}
-
-static int emcore_alarm_timeout_cb_for_alert(int timer_id, void *user_parm)
-{
-       EM_DEBUG_FUNC_BEGIN();
-
-       int err = EMAIL_ERROR_NONE;
-       int total_unread_count = 0;
-       int total_mail_count = 0;
-       email_mailbox_t mailbox;
-
-       memset(&mailbox, 0x00, sizeof(email_mailbox_t));
-
-       mailbox.account_id = ALL_ACCOUNT;
-       mailbox.mailbox_name = NULL;
-
-       if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_get_mail_count failed - %d\n", err);
-               return false;
-       }
-
-       EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
-       
-       if (total_unread_count) {
-               /* emdaemon_start_alert(); */
-               /* Alarm repetition disabled */
-       }
-
-       EM_DEBUG_FUNC_END();    
-       return true;
-}
-
-/* repetition_time in minutes */
-static int emcore_set_alarm_for_alert(int alert_time_in_minute)
-{
-       EM_DEBUG_FUNC_BEGIN();
-
-       int err = EMAIL_ERROR_NONE;
-       time_t current_time;
-       time_t trigger_at_time;
-
-       time(&current_time);
-
-       trigger_at_time = current_time + (alert_time_in_minute * 60);
-
-       if ((err = emcore_add_alarm(trigger_at_time, EMAIL_ALARM_CLASS_NEW_MAIL_ALERT, EMAIL_ALARM_REFERENCE_ID_FOR_ALERT_TONE, emcore_alarm_timeout_cb_for_alert, NULL)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-FINISH_OFF:
-
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
-}
-
-static void emcore_set_repetition_alarm(int repetition)
-{
-       EM_DEBUG_FUNC_BEGIN();
-
-       int repetition_time = 0;
-       
-       switch (repetition) {
-       case EMAIL_GCONF_VALUE_REPEAT_NONE:
-               repetition_time = 0;
-               break;
-       case EMAIL_GCONF_VALUE_REPEAT_2MINS:
-               repetition_time = 2;
-               break;
-       case EMAIL_GCONF_VALUE_REPEAT_5MINS:
-               repetition_time = 5;
-               break;
-       case EMAIL_GCONF_VALUE_REPEAT_10MINS:           
-               repetition_time = 10;
-               break;
-       default:
-               EM_DEBUG_EXCEPTION("Invalid repetition time");
-               return;
-       }
-
-       EM_DEBUG_LOG("repetition time is %d", repetition_time);
-
-       if (repetition_time > 0) {
-               emcore_set_alarm_for_alert(repetition_time);
-       } 
-
-       EM_DEBUG_FUNC_END();
-}
-
-static char * emcore_get_sound_file_path(int vip_mode)
-{
-       EM_DEBUG_FUNC_BEGIN("vip_mode [%d]", vip_mode);
-       char *ret = NULL;
-       int   use_default_ring_tone = 0;
-
-       if (vip_mode) {
-               if (vconf_get_bool(VCONF_VIP_NOTI_USE_DEFAULT_RINGTONE_BOOL, &use_default_ring_tone) != 0)
-                       EM_DEBUG_LOG("vconf_get_bool for VCONF_VIP_NOTI_USE_DEFAULT_RINGTONE_BOOL failed");
-
-               EM_DEBUG_LOG("use_default_ring_tone [%d]", use_default_ring_tone);
-
-               if (use_default_ring_tone)
-                       ret = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_RINGTONE_DEFAULT_PATH_STR));
-               else
-                       ret = EM_SAFE_STRDUP(vconf_get_str(VCONF_VIP_NOTI_RINGTONE_PATH));
-       }
-       else {
-               if (vconf_get_bool(VCONF_EMAIL_NOTI_USE_DEFAULT_RINGTONE_BOOL, &use_default_ring_tone) != 0)
-                       EM_DEBUG_LOG("vconf_get_bool for VCONF_EMAIL_NOTI_USE_DEFAULT_RINGTONE_BOOL failed");
-
-               EM_DEBUG_LOG("use_default_ring_tone [%d]", use_default_ring_tone);
-
-               if (use_default_ring_tone)
-                       ret = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_RINGTONE_DEFAULT_PATH_STR));
-               else
-                       ret = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR));
-       }
-
-       EM_DEBUG_FUNC_END("ret [%s]", ret);
-       return ret;
-}
-
-
-INTERNAL_FUNC int emcore_get_alert_policy(EMAIL_ALERT_TYPE *output_alert_type, char **output_alert_tone_path)
-{
-       EM_DEBUG_FUNC_BEGIN("output_alert_type [%p] output_alert_tone_path[%p]", output_alert_type, output_alert_tone_path);
-
-       int err = EMAIL_ERROR_NONE;
-       int general_noti_status = false;
-       int vip_noti_status = false;
-       int vip_mail_count = 0;
-       int vip_unread_count = 0;
-       int vip_notification_mode = 0;
-       char *alert_tone_path = NULL;
-       EMAIL_ALERT_TYPE alert_type = EMAIL_ALERT_TYPE_NONE;
-
-       if (output_alert_type == NULL || output_alert_tone_path == NULL) {
-               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       /* get general noti status */
-       if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, &general_noti_status) != 0) {
-               EM_DEBUG_EXCEPTION("vconf_get_int failed");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       /* get vip noti status */
-       if (vconf_get_bool(VCONF_VIP_NOTI_NOTIFICATION_TICKER, &vip_noti_status) != 0) {
-               EM_DEBUG_EXCEPTION("vconf_get_int failed");
-               err = EMAIL_ERROR_DATA_NOT_FOUND;
-               goto FINISH_OFF;
-       }
-
-       if (!general_noti_status && !vip_noti_status) {
-               EM_DEBUG_LOG("both notification disabled");
-               goto FINISH_OFF;
-       }
-
-       if (vip_noti_status) {
-               if (!emcore_get_mail_count_by_query(ALL_ACCOUNT, EMAIL_MAILBOX_TYPE_INBOX, true, &vip_mail_count, &vip_unread_count, &err)) {
-                       EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
-                       err = EMAIL_ERROR_DATA_NOT_FOUND;
-               }
-       }
-
-       if (general_noti_status && !vip_noti_status) {
-               EM_DEBUG_LOG("GERNERAL NOTI MODE");
-               vip_notification_mode = 0;
-       }
-       else if (!general_noti_status && vip_noti_status) {
-               EM_DEBUG_LOG("VIP NOTI MODE");
-               if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
-                       EM_DEBUG_LOG("VIP filter not found");
-                       err = EMAIL_ERROR_DATA_NOT_FOUND;
-                       goto FINISH_OFF;
-               }
-               vip_notification_mode = 1;
-       }
-       else if (general_noti_status && vip_noti_status) {
-               if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
-                       EM_DEBUG_LOG("VIP filter not found");
-                       EM_DEBUG_LOG("GERNERAL NOTI MODE");
-                       vip_notification_mode = 0;
-               } else {
-                       //confusing..
-                       if (vip_unread_count > 0) {
-                               EM_DEBUG_LOG("VIP NOTI MODE");
-                               vip_notification_mode = 1;
-                       } else {
-                               EM_DEBUG_LOG("GERNERAL NOTI MODE");
-                               vip_notification_mode = 0;
-                       }
-               }
-       }
-
-       alert_type = emcore_get_alert_type(vip_notification_mode);
-       EM_DEBUG_LOG("alert_type [%d]", alert_type);
-
-
-
-       EM_DEBUG_LOG("vip_notification_mode [%d]", vip_notification_mode);
-
-       if (alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_MELODY)
-               alert_tone_path = EM_SAFE_STRDUP(emcore_get_sound_file_path(vip_notification_mode));
-
-       *output_alert_type = alert_type;
-       if (alert_tone_path)
-               *output_alert_tone_path = alert_tone_path;
-
-       EM_DEBUG_LOG("alert_type [%d] alert_tone_path [%s]", alert_type, alert_tone_path);
-
-FINISH_OFF:
-
-       EM_DEBUG_FUNC_END("err [%d]", err);
-
-       return err;
-}
index f3a1f7e..1c4a8f0 100755 (executable)
@@ -59,7 +59,7 @@ static int                 _task_manager_loop_availability = 1;
 static int emcore_insert_task_to_active_task_pool(int input_task_slot_index, int input_task_id, email_task_type_t input_task_type, thread_t input_thread_id);\r
 static int emcore_remove_task_from_active_task_pool(int input_task_id);\r
 static int emcore_find_available_slot_in_active_task_pool(int *result_index);\r
-static int emcore_update_task_status_on_task_table(int input_task_id, email_task_status_type_t task_status);\r
+static int emcore_update_task_status_on_task_table(char *multi_user_name, int input_task_id, email_task_status_type_t task_status);\r
 static int emcore_get_task_handler_reference(email_task_type_t input_task_type, email_task_handler_t **output_task_handler);\r
 \r
 /*- task handlers helpers - begin --------------------------------------------*/\r
@@ -109,7 +109,7 @@ static int emcore_finalize_async_task_handler(email_task_t *input_task, int inpu
        }\r
 \r
        /* remove task from task table */\r
-       if( (err = emcore_remove_task_from_task_table(input_task->task_id)) != EMAIL_ERROR_NONE) {\r
+       if( (err = emcore_remove_task_from_task_table(NULL, input_task->task_id)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emcore_remove_task_from_active_task_pool failed [%d]", err);\r
                goto FINISH_OFF;\r
        }\r
@@ -223,6 +223,7 @@ static int emcore_register_task_handler(email_task_type_t input_task_type, void*
 \r
        if (_task_handler_array == NULL) {\r
                err = EMAIL_ERROR_OUT_OF_MEMORY;\r
+        EM_SAFE_FREE(new_task_handler);\r
                goto FINISH_OFF;\r
        }\r
 \r
@@ -234,13 +235,12 @@ FINISH_OFF:
        return err;\r
 }\r
 \r
-\r
 INTERNAL_FUNC int emcore_init_task_handler_array()\r
 {\r
        EM_DEBUG_FUNC_BEGIN();\r
 \r
        if (_task_handler_array == NULL) {\r
-               _task_handler_array      = NULL;\r
+               _task_handler_array = NULL;\r
                _task_handler_array_size = 0;\r
 \r
                REGISTER_TASK_BINDER(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT);\r
@@ -368,7 +368,7 @@ int emcore_fetch_task_from_task_pool(email_task_t **output_task)
        int err = EMAIL_ERROR_NONE;\r
        int output_task_count;\r
 \r
-       if((err = emstorage_query_task("WHERE task_status == 1", " ORDER BY date_time ASC, task_priority ASC LIMIT 0, 1", output_task, &output_task_count)) != EMAIL_ERROR_NONE) {\r
+       if((err = emstorage_query_task(NULL, "WHERE task_status == 1", " ORDER BY date_time ASC, task_priority ASC LIMIT 0, 1", output_task, &output_task_count)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emstorage_query_task failed [%d]", err);\r
                goto FINISH_OFF;\r
        }\r
@@ -506,17 +506,18 @@ void* thread_func_task_manager_loop(void *arg)
                        ((err = emcore_find_available_slot_in_active_task_pool(&available_slot_index)) == EMAIL_ERROR_NONE)     ) {\r
 \r
                        /* update task status as STARTED */\r
-                       if((err = emcore_update_task_status_on_task_table(new_task->task_id, EMAIL_TASK_STATUS_STARTED)) != EMAIL_ERROR_NONE) {\r
+                       if((err = emcore_update_task_status_on_task_table(NULL, new_task->task_id, EMAIL_TASK_STATUS_STARTED)) != EMAIL_ERROR_NONE) {\r
                                EM_DEBUG_EXCEPTION("emcore_update_task_status_on_task_table failed [%d]", err);\r
                        }\r
                        new_task->active_task_id = available_slot_index;\r
 \r
                        /* create a thread to do this task */\r
                        THREAD_CREATE(new_task->thread_id, emcore_default_async_task_handler, new_task, thread_error);\r
+                       EM_DEBUG_LOG("pthread_create returns [%d]", thread_error);\r
 \r
                        /* new_task and task_parameter will be free in task handler. */\r
-                       new_task     = NULL;\r
-\r
+                        EM_SAFE_FREE(new_task->task_parameter);\r
+                        EM_SAFE_FREE(new_task);\r
                }\r
                else {\r
                        /* If there is no task or no available slot, sleep until someone wake you up. */\r
@@ -569,6 +570,12 @@ INTERNAL_FUNC int emcore_stop_task_manager_loop()
 \r
        int err = EMAIL_ERROR_NONE;\r
 \r
+       if (!_thread_task_manager_loop) {\r
+               EM_DEBUG_LOG("_thread_task_manager_loop is NULL ");\r
+               err = EMAIL_ERROR_UNKNOWN;\r
+               return err;\r
+       }\r
+\r
        /* TODO : cancel tasks */\r
 \r
        /* stop event_data loop */\r
@@ -594,12 +601,12 @@ INTERNAL_FUNC int emcore_stop_task_manager_loop()
        return err;\r
 }\r
 \r
-INTERNAL_FUNC int emcore_add_task_to_task_table(email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int *output_task_id)\r
+INTERNAL_FUNC int emcore_add_task_to_task_table(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int *output_task_id)\r
 {\r
        EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority [%d] input_task_parameter [%p] input_task_parameter_length [%d] output_task_id [%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, output_task_id);\r
        int err = EMAIL_ERROR_NONE;\r
 \r
-       if((err = emstorage_add_task(input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, false, output_task_id)) != EMAIL_ERROR_NONE) {\r
+       if((err = emstorage_add_task(multi_user_name, input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, false, output_task_id)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emstorage_add_task failed [%d]", err);\r
                goto FINISH_OFF;\r
        }\r
@@ -613,12 +620,12 @@ FINISH_OFF:
        return err;\r
 }\r
 \r
-INTERNAL_FUNC int emcore_remove_task_from_task_table(int input_task_id)\r
+INTERNAL_FUNC int emcore_remove_task_from_task_table(char *multi_user_name, int input_task_id)\r
 {\r
        EM_DEBUG_FUNC_BEGIN("input_task_id [%d]", input_task_id);\r
        int err = EMAIL_ERROR_NONE;\r
 \r
-       if((err = emstorage_delete_task(input_task_id, true)) != EMAIL_ERROR_NONE) {\r
+       if((err = emstorage_delete_task(multi_user_name, input_task_id, true)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emstorage_delete_task failed [%d]", err);\r
                goto FINISH_OFF;\r
        }\r
@@ -628,12 +635,12 @@ FINISH_OFF:
        return err;\r
 }\r
 \r
-static int emcore_update_task_status_on_task_table(int input_task_id, email_task_status_type_t task_status)\r
+static int emcore_update_task_status_on_task_table(char *multi_user_name, int input_task_id, email_task_status_type_t task_status)\r
 {\r
        EM_DEBUG_FUNC_BEGIN("input_task_id [%d] task_status [%d]", input_task_id, task_status);\r
        int err = EMAIL_ERROR_NONE;\r
 \r
-       if((err = emstorage_update_task_status(input_task_id, task_status, true)) != EMAIL_ERROR_NONE) {\r
+       if((err = emstorage_update_task_status(multi_user_name, input_task_id, task_status, true)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emstorage_update_task_status failed [%d]", err);\r
                goto FINISH_OFF;\r
        }\r
index 019fa5d..636f526 100755 (executable)
@@ -79,7 +79,7 @@ INTERNAL_FUNC int email_encode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAI
 FINISH_OFF:\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC int email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct)\r
@@ -126,7 +126,7 @@ FINISH_OFF:
                tpl_free(tn);\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void *input_param)\r
@@ -139,7 +139,7 @@ INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTH
        task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *task_param = input_param;\r
 \r
        for(i = 0; i < task_param->mail_id_count; i++) {\r
-               if((err = emcore_move_mail_to_another_account(task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {\r
+               if((err = emcore_move_mail_to_another_account(task_param->multi_user_name, task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {\r
                        EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);\r
                        goto FINISH_OFF;\r
                }\r
@@ -195,7 +195,7 @@ INTERNAL_FUNC int email_encode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX
 FINISH_OFF:\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC int email_decode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct)\r
@@ -242,7 +242,7 @@ FINISH_OFF:
                tpl_free(tn);\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)\r
@@ -281,7 +281,7 @@ INTERNAL_FUNC int email_encode_task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DO
 FINISH_OFF:\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC int email_decode_task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct)\r
@@ -318,7 +318,7 @@ FINISH_OFF:
                tpl_free(tn);\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(void *input_param)\r
@@ -357,7 +357,7 @@ INTERNAL_FUNC int email_encode_task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_M
 FINISH_OFF:\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC int email_decode_task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL(char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct)\r
@@ -394,7 +394,7 @@ FINISH_OFF:
                tpl_free(tn);\r
 \r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return EMAIL_ERROR_NONE;\r
+       return err;\r
 }\r
 \r
 INTERNAL_FUNC void* task_handler_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL(void *input_param)\r
@@ -403,7 +403,7 @@ INTERNAL_FUNC void* task_handler_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL(void *inp
        int err = EMAIL_ERROR_NONE;\r
        task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL *task_param = input_param;\r
 \r
-       if((err = emcore_schedule_sending_mail(task_param->mail_id, task_param->scheduled_time)) != EMAIL_ERROR_NONE) {\r
+       if((err = emcore_schedule_sending_mail(task_param->multi_user_name, task_param->mail_id, task_param->scheduled_time)) != EMAIL_ERROR_NONE) {\r
                EM_DEBUG_EXCEPTION("emcore_schedule_sending_mail [%d]", err);\r
                err = EMAIL_ERROR_OUT_OF_MEMORY;\r
                goto FINISH_OFF;\r
@@ -411,8 +411,8 @@ INTERNAL_FUNC void* task_handler_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL(void *inp
 \r
 FINISH_OFF:\r
 \r
-       EM_SAFE_FREE(task_param);\r
-\r
+       EM_SAFE_FREE(task_param);
+       emcore_close_smtp_stream_list ();\r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
        return (void*)err;\r
 }\r
@@ -602,28 +602,31 @@ INTERNAL_FUNC void* task_handler_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE(void *input_pa
 \r
        switch(attribute_value_type) {\r
                case EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER :\r
-                       if(!emstorage_set_field_of_mails_with_integer_value(task_param->account_id, task_param->mail_id_array, task_param->mail_id_count, field_name, task_param->value.integer_type_value, true, &err)) {\r
+                       if(!emstorage_set_field_of_mails_with_integer_value(task_param->multi_user_name, task_param->account_id, task_param->mail_id_array, task_param->mail_id_count, field_name, task_param->value.integer_type_value, true, &err)) {\r
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);\r
                                goto FINISH_OFF;\r
                        }\r
                        break;\r
                case EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING :\r
                        err = EMAIL_ERROR_NOT_SUPPORTED;\r
+                       EM_DEBUG_LOG("EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING is not supported");\r
                        break;\r
                case EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME :\r
-                       if(!emstorage_set_field_of_mails_with_integer_value(task_param->account_id, task_param->mail_id_array, task_param->mail_id_count, field_name, task_param->value.datetime_type_value, true, &err)) {\r
+                       if(!emstorage_set_field_of_mails_with_integer_value(task_param->multi_user_name, task_param->account_id, task_param->mail_id_array, task_param->mail_id_count, field_name, task_param->value.datetime_type_value, true, &err)) {\r
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);\r
                                goto FINISH_OFF;\r
                        }\r
                        break;\r
                case EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE :\r
                default :\r
+                       EM_DEBUG_LOG("EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE or default");\r
                        err = EMAIL_ERROR_INVALID_PARAM;\r
                        break;\r
        }\r
 \r
 FINISH_OFF:\r
 \r
+       EM_SAFE_FREE(task_param->multi_user_name);\r
        EM_SAFE_FREE(task_param->mail_id_array);\r
        if (attribute_value_type == EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING) {\r
                EM_SAFE_FREE(task_param->value.string_type_value);\r
@@ -631,7 +634,6 @@ FINISH_OFF:
 \r
        EM_SAFE_FREE (task_param);\r
 \r
-\r
        EM_DEBUG_FUNC_END("err [%d]", err);\r
        return (void*)err;\r
 }\r
index 04ceb95..82bfa11 100755 (executable)
@@ -32,7 +32,6 @@
 #include <sys/timeb.h>
 #include "email-core-timer.h"
 #include "email-debug-log.h"
-#include <dbus/dbus-glib.h> 
 
 
 typedef struct
@@ -48,31 +47,22 @@ INTERNAL_FUNC int emcore_timer_ex_callback(void *a_pData)
        EM_DEBUG_LOG("[emcore_timer_ex_callback] enter\n");
        void *pUserData = NULL;
 
-
-       g_thread_init(NULL);
-       dbus_g_thread_init ();
-
        em_timer_callback_data *pTimerData = (em_timer_callback_data *)a_pData;
-       if (pTimerData != NULL)
-               {
-               EMAIL_TIMER_CALLBACK pfn_UserCB = pTimerData->user_callback_function;
-                       pUserData = pTimerData->callback_user_data;
-               if (pUserData)
-                       EM_DEBUG_LOG("emcore_timer_ex_callback >>> data  :  %s", (char *)pTimerData->callback_user_data);
-                       EM_SAFE_FREE(pTimerData);
-                       pfn_UserCB(pUserData);
-               }
-               
-       g_thread_init(NULL);
-       dbus_g_thread_init ();
+       if (pTimerData != NULL) {
+        EMAIL_TIMER_CALLBACK pfn_UserCB = pTimerData->user_callback_function;
+        pUserData = pTimerData->callback_user_data;
+        if (pUserData)
+            EM_DEBUG_LOG("emcore_timer_ex_callback >>> data  :  %s", (char *)pTimerData->callback_user_data);
+            EM_SAFE_FREE(pTimerData);
+            pfn_UserCB(pUserData);
+    }
 
        EM_DEBUG_LOG("[emcore_timer_ex_callback] leave\n");
 
        if (pUserData)
                return 0;
        else
-                       return 1;
+        return 1;
 }
 
 INTERNAL_FUNC int emcore_set_timer_ex(long a_nSetTimeValue, EMAIL_TIMER_CALLBACK a_pCallBack, void *a_pData)
@@ -80,17 +70,14 @@ INTERNAL_FUNC int emcore_set_timer_ex(long a_nSetTimeValue, EMAIL_TIMER_CALLBACK
        EM_DEBUG_LOG("emcore_set_timer_ex %d", a_nSetTimeValue);
        em_timer_callback_data *pTimerData = NULL;
        pTimerData = malloc(sizeof(em_timer_callback_data));
-       char *data = NULL;
        if (!pTimerData)
                return -1;
-       memset(pTimerData, 0x00, sizeof(em_timer_callback_data));
-       if (a_pData)
-               EM_DEBUG_LOG("emcore_set_timer_ex >>> data  :  %s", (char *)a_pData);
+
+    memset(pTimerData, 0x00, sizeof(em_timer_callback_data));
 
        pTimerData->user_callback_function = a_pCallBack;
        if (a_pData) {
-               data = (char *) a_pData;
-               pTimerData->callback_user_data = EM_SAFE_STRDUP(data);
+               pTimerData->callback_user_data = a_pData;
        }
        pTimerData->time_id = g_timeout_add(a_nSetTimeValue, emcore_timer_ex_callback, pTimerData);
        return pTimerData->time_id;
index fc5ceb0..83794c2 100755 (executable)
@@ -29,7 +29,7 @@
  * History:
  *     2006.08.16 : created
  *****************************************************************************/
-#include <tzplatform_config.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
 #include <notification.h>
 #include <notification_type.h>
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
+#include <notification_internal.h>
+#endif /* __FEATURE_NOTIFICATION_ENABLE__ */
 #include <badge.h>
+#include <badge_internal.h>
+#ifdef __FEATURE_USE_DRM_API__
 #include <drm_client_types.h>
 #include <drm_client.h>
+#endif /* __FEATURE_USE_DRM_API__ */
+#include <feedback.h>
+#include <storage.h>
+#include <bundle.h>
+#include <curl/curl.h>
+#include <contacts.h>
+#include <contacts_internal.h>
 
 #include "email-types.h"
 #include "email-core-global.h"
 #include "email-core-mailbox-sync.h"
 #include "email-core-mime.h"
 #include "email-core-signal.h"
-#include "email-core-sound.h"
 #include "email-daemon.h"
 #include "email-utilities.h"
 #include "email-convert.h"
 #include "email-internal-types.h"
 #include "email-device.h"
 
+#include "email-core-container.h"
+
+#include <gio/gio.h>
+#include "email-dbus-activation.h"
+
+
 #ifdef __FEATURE_DRIVING_MODE__
-#include <app_service.h>
+#include <app_control.h>
 #endif /* __FEATURE_DRIVING_MODE__ */
 
 #define LED_TIMEOUT_SECS          12
 #define EMAIL_CH_SQUARE_BRACKET_S '['
 #define EMAIL_CH_SQUARE_BRACKET_E ']'
 #define EMAIL_CH_SPACE            ' '
-#define EMAIL_NOTI_ICON_PATH      tzplatform_mkpath(TZ_USER_DATA, "email/res/image/Q02_Notification_email.png")
+#define EMAIL_NOTI_ICON_PATH                "/usr/apps/org.tizen.quickpanel/shared/res/noti_icons/noti_email.png"
+#define EMAIL_NOTI_INDICATOR_ICON_PATH      "/usr/apps/org.tizen.indicator/res/icons/Event/B03_Event_email.png"
+#define EMAIL_NOTI_MAX_MAIL_ID   100
+
 
 typedef struct  _em_transaction_info_type_t {
        int mail_id;
@@ -100,7 +120,7 @@ typedef struct  _em_transaction_info_type_t {
 
 em_transaction_info_type_t  *g_transaction_info_list;
 
-static email_session_t g_session_list[SESSION_MAX] = { {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},};
+__thread email_session_t g_session = {0};
 static notification_h g_sending_noti_handle = NULL;
 typedef struct emcore_account_list_t emcore_account_list_t;
 struct emcore_account_list_t {
@@ -108,14 +128,95 @@ struct emcore_account_list_t {
        emcore_account_list_t *next;
 };
 
+#include <gmime/gmime.h>
+
+
 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
 {
-       EM_DEBUG_FUNC_BEGIN();
-       return (char *)(utf8_from_mutf7((unsigned char *)mailbox_name));
+       EM_DEBUG_FUNC_BEGIN("mailbox_name[%p]", mailbox_name);
+
+       iconv_t cd;
+       char *result_mailbox_name = NULL;
+       char *mutf7_text = NULL;
+       char *cursor = NULL;
+       int  is_base64 = 0;
+
+       if (mailbox_name == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               return NULL;
+       }
+
+       EM_DEBUG_LOG_SEC("mailbox_name[%s]", mailbox_name);
+
+       cursor = mutf7_text = EM_SAFE_STRDUP(mailbox_name);
+
+       if (mutf7_text == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
+               return NULL;
+       }
+
+       for (; *cursor; ++cursor)
+               switch (*cursor) {
+                       case '+':
+                               if (!is_base64) *cursor = '&';
+                               break;
+
+                       case '&':
+                               *cursor = '+';
+                               is_base64 = 1;
+                               break;
+
+                       case '-':
+                               is_base64 = 0;
+                               break;
+
+                       case ',':
+                               if (is_base64)
+                                       *cursor = '/';
+                               break;
+               }
+
+       cd = g_mime_iconv_open("UTF-8", "UTF-7");
+
+       if (cd)
+               result_mailbox_name = g_mime_iconv_strdup(cd, mutf7_text);
+
+       EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
+
+       if (result_mailbox_name) {
+               cursor = result_mailbox_name;
+               for (; *cursor; ++cursor) {
+                       switch (*cursor) {
+                         case '&':
+                               *cursor = '+';
+                               break;
+
+                         case '+':
+                               *cursor = '&';
+                               break;
+                       }
+               }
+       }
+       else {
+               result_mailbox_name = (char *)utf8_from_mutf7((unsigned char *)mailbox_name);
+               EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
+               if (result_mailbox_name == NULL)
+                       result_mailbox_name = EM_SAFE_STRDUP(mutf7_text);
+       }
+
+       EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
+
+       if (cd)
+               g_mime_iconv_close(cd);
+
+       EM_SAFE_FREE(mutf7_text);
+
+       EM_DEBUG_FUNC_END("result_mailbox_name[%p]", result_mailbox_name);
+       return result_mailbox_name;
 }
 
 /*  in smtp case, path argument must be ENCODED_PATH_SMTP */
-int emcore_get_long_encoded_path_with_account_info(email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
+int emcore_get_long_encoded_path_with_account_info(char *multi_user_name, email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
 {
        EM_PROFILE_BEGIN(emCorelongEncodedpath);
        EM_DEBUG_FUNC_BEGIN_SEC("account[%p], path[%s], delimiter[%d], long_enc_path[%p], err_code[%p]", account, path, delimiter, long_enc_path, err_code);
@@ -150,22 +251,28 @@ int emcore_get_long_encoded_path_with_account_info(email_account_t *account, cha
 
                /*  ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
 
-               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->account_id);
+        if (multi_user_name) {
+            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, 
+                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->account_id);
+        }
 
                if (account->incoming_server_secure_connection & 0x01)  {
                        strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
                        /* strcat(p, "/tryssl"); */
-               }
-
-               /* Currently, receiving servers doesn't require tls.
-               if (account->incoming_server_secure_connection & 0x02)
+               } else if (account->incoming_server_secure_connection & 0x02) {
                        strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
-               else
-                       strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
-               */
+               }
 
                authentication_method = account->incoming_server_authentication_method;
 
@@ -194,7 +301,16 @@ int emcore_get_long_encoded_path_with_account_info(email_account_t *account, cha
                        "smtp");
 
                if (account->outgoing_server_need_authentication > 0) {
-                       SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", account->account_id);
+            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, 
+                                                                                        multi_user_name);
+            } else {
+                           SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", 
+                                                                                        account->account_id);
+            }
+
                        if (account->outgoing_server_need_authentication == EMAIL_AUTHENTICATION_METHOD_XOAUTH2)  {
                                strncat(p, "/xoauth2", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
                        }
@@ -238,7 +354,7 @@ int emcore_get_long_encoded_path_with_account_info(email_account_t *account, cha
        }
 
        if(*long_enc_path)
-               EM_DEBUG_LOG("long_enc_path [%s]", *long_enc_path);
+           EM_DEBUG_LOG_SEC("long_enc_path [%s]", *long_enc_path);
 
        ret = true;
 
@@ -252,7 +368,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
+int emcore_get_long_encoded_path(char *multi_user_name, int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
 {
        EM_PROFILE_BEGIN(emCorelongEncodedpath);
        EM_DEBUG_FUNC_BEGIN("account_id[%d], delimiter[%d], long_enc_path[%p], err_code[%p]", account_id, delimiter, long_enc_path, err_code);
@@ -261,14 +377,14 @@ int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char
        int error = EMAIL_ERROR_NONE;
        email_account_t *ref_account = NULL;
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                error = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if (emcore_get_long_encoded_path_with_account_info(ref_account, path, delimiter, long_enc_path, &error) == false) {
+       if (emcore_get_long_encoded_path_with_account_info(multi_user_name, ref_account, path, delimiter, long_enc_path, &error) == false) {
                EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path_with_account_info failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -331,12 +447,12 @@ int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
                ret_enc_name = strdup(name);
                goto FINISH_OFF;
        }
-       
+
        EM_DEBUG_LOG_SEC("UTF-7 [%s](%d)->[%s](%d)",last_word, strlen(last_word), last_enc_word, strlen(last_enc_word));
 
        /* if last word of mailbox name is UTF-8, replace it */
        /* it is not a subfolder */
-       if (!last_slash) { 
+       if (!last_slash) {
                ret_enc_name = strdup(last_enc_word);
        }
        else { /* it is a subfolder */
@@ -481,9 +597,7 @@ INTERNAL_FUNC int emcore_check_drm_file(char *path, int *err_code)
        EM_DEBUG_FUNC_BEGIN("path[%s], err_code[%p]", path, err_code);
 
        int ret = false;
-       int drm_ret = false;
        int error = EMAIL_ERROR_NONE;
-       drm_bool_type_e isdrm;
 
        if (path == NULL) {
                EM_DEBUG_EXCEPTION("path[%p]", path);
@@ -491,6 +605,9 @@ INTERNAL_FUNC int emcore_check_drm_file(char *path, int *err_code)
                goto FINISH_OFF;
        }
 
+#ifdef __FEATURE_USE_DRM_API__
+       int drm_ret = false;
+       drm_bool_type_e isdrm;
        drm_ret = drm_is_drm_file(path, &isdrm);
        if (drm_ret != DRM_RETURN_SUCCESS || isdrm != DRM_TRUE) {
                EM_DEBUG_LOG("file is not drm file");
@@ -499,6 +616,7 @@ INTERNAL_FUNC int emcore_check_drm_file(char *path, int *err_code)
        }
 
        ret = true;
+#endif /* __FEATURE_USE_DRM_API__ */
 
 FINISH_OFF:
 
@@ -514,7 +632,6 @@ INTERNAL_FUNC int emcore_check_drm_is_ringtone(char *ringtone_path, int *err_cod
        EM_DEBUG_FUNC_BEGIN("ringtone_path[%s], err_code[%p]", ringtone_path, err_code);
 
        int ret = false;
-       int drm_ret = false;
        int error = EMAIL_ERROR_NONE;
 
        if (ringtone_path == NULL) {
@@ -523,6 +640,8 @@ INTERNAL_FUNC int emcore_check_drm_is_ringtone(char *ringtone_path, int *err_cod
                goto FINISH_OFF;
        }
 
+#ifdef __FEATURE_USE_DRM_API__
+       int drm_ret = false;
        drm_bool_type_e allowed = DRM_UNKNOWN;
        drm_action_allowed_data_s data;
        memset(&data, 0x00, sizeof(drm_action_allowed_data_s));
@@ -539,6 +658,7 @@ INTERNAL_FUNC int emcore_check_drm_is_ringtone(char *ringtone_path, int *err_cod
 
        EM_DEBUG_LOG("allowed [DRM_TRUE]");
        ret = true;
+#endif /* __FEATURE_USE_DRM_API__ */
 
 FINISH_OFF:
 
@@ -640,25 +760,10 @@ INTERNAL_FUNC int emcore_get_empty_session(email_session_t **session)
 {
        EM_DEBUG_FUNC_BEGIN("session[%p]", session);
 
-       /*  lock()... */
-
-       int i;
-
-       for (i = 0; i < SESSION_MAX; i++)  {
-               if (!g_session_list[i].status)  {
-                       memset(g_session_list+i, 0x00, sizeof(email_session_t));
-                       g_session_list[i].tid = GPOINTER_TO_INT(THREAD_SELF());
-                       g_session_list[i].status = true;
-                       break;
-               }
-       }
-
-       /*  unlock()... */
+       *session = &g_session;
 
-       if (session != NULL)
-               *session = (i != SESSION_MAX) ? &g_session_list[i] : NULL;
        EM_DEBUG_FUNC_END();
-       return (i != SESSION_MAX) ? true : false;
+       return true;
 }
 
 INTERNAL_FUNC int emcore_clear_session(email_session_t *session)
@@ -666,7 +771,8 @@ INTERNAL_FUNC int emcore_clear_session(email_session_t *session)
        EM_DEBUG_FUNC_BEGIN();
 
        if (session)
-               memset(session, 0x00, sizeof(email_session_t));
+               memset (session, 0x00, sizeof(email_session_t));
+
        EM_DEBUG_FUNC_END();
        return true;
 }
@@ -675,24 +781,13 @@ INTERNAL_FUNC int emcore_get_current_session(email_session_t **session)
 {
        EM_DEBUG_FUNC_BEGIN("session[%p]", session);
 
-       int i;
-
-       for (i = 0; i < SESSION_MAX; i++)  {
-               if (g_session_list[i].tid == GPOINTER_TO_INT(THREAD_SELF())) {
-                       if (session)
-                               *session = g_session_list + i;
-
-                       break;
-               }
-       }
+       *session = &g_session;
 
-       if (session)
-               *session = (i != SESSION_MAX) ? g_session_list + i : NULL;
        EM_DEBUG_FUNC_END();
-       return (i != SESSION_MAX) ? true : false;
+       return true;
 }
 
-INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_type, int priority_sender, int *total_mail, int *unread_mail, int *err_code)
+INTERNAL_FUNC int emcore_get_mail_count_by_query(char *multi_user_name, int account_id, int mailbox_type, int priority_sender, int *total_mail, int *unread_mail, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_type : [%d], priority_sender : [%d]", account_id, mailbox_type, priority_sender);
 
@@ -713,7 +808,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
 
        /* Get rule list */
        if (priority_sender) {
-               if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err)) {
+               if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_rule failed");
                        goto FINISH_OFF;
                }
@@ -727,15 +822,16 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
        EM_DEBUG_LOG_DEV ("rule count [%d]", rule_count);
 
        /* Make query for searching unread mail */
-       filter_count = 1;   /* unseen field requires one filter, "flags_seen_field = 0" */
-       
+       filter_count = 3;   /* unseen field requires one filter, "flags_seen_field = 0"
+                               and "deleted_flag = 0" */
+
        if (rule_count > 0 )
                filter_count += (rule_count * 2) + 2; /* one rule requires two filters,"A" "OR", plus two more operator "(" and ")" */
 
 
        if (account_id != ALL_ACCOUNT) {
                filter_count += 2; /* two filters, "AND" "account_id = x" */
-       } 
+       }
 
        if (mailbox_type)
                filter_count += 2; /* two filters, "AND" "mailbox_type = x" */
@@ -747,7 +843,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
                goto FINISH_OFF;
        }
 
-       int k = 0;        
+       int k = 0;
 
        /* priority sender only, convert one rule to two filters which is composed of from_address and or/and */
        /* example: ( from_A OR from_B ) AND ... */
@@ -755,7 +851,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
                /* first, add left parenthesis to from address clause, "(" */
                filter_list[0].list_filter_item_type                   = EMAIL_LIST_FILTER_ITEM_OPERATOR;
                filter_list[0].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS;
-               
+
                for (i = 0; i < rule_count; i++) {
                        /*array at odd index(1, 3, 5,..) has rule values, "from_A" */
                        k = i*2+1;
@@ -771,7 +867,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
 
                        } else { /* ")" "AND" */
                                filter_list[++k].list_filter_item_type         = EMAIL_LIST_FILTER_ITEM_OPERATOR;
-                               filter_list[k].list_filter_item.operator_type  = EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS; 
+                               filter_list[k].list_filter_item.operator_type  = EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS;
 
                                filter_list[++k].list_filter_item_type         = EMAIL_LIST_FILTER_ITEM_OPERATOR;
                                filter_list[k].list_filter_item.operator_type  = EMAIL_LIST_FILTER_OPERATOR_AND;
@@ -781,11 +877,19 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
        }
 
        /*rule value, unseen +1*/
-       filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
-       filter_list[k].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
-       filter_list[k].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD;
+       filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_RULE;
+       filter_list[k].list_filter_item.rule.rule_type                      = EMAIL_LIST_FILTER_RULE_EQUAL;
+       filter_list[k].list_filter_item.rule.target_attribute               = EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD;
        filter_list[k++].list_filter_item.rule.key_value.integer_type_value = 0;
 
+       filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_OPERATOR;
+       filter_list[k++].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
+
+       filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_RULE;
+       filter_list[k].list_filter_item.rule.rule_type                      = EMAIL_LIST_FILTER_RULE_NOT_EQUAL;
+       filter_list[k].list_filter_item.rule.target_attribute               = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
+       filter_list[k++].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAIL_STATUS_SAVED_OFFLINE;
+
        /*account_id requires two filters*/
        if (account_id != ALL_ACCOUNT) {
                /* odd index, logical operator */
@@ -803,7 +907,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
        if (mailbox_type) {
                /* odd index, logical operator */
                filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
-               filter_list[k++].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
+               filter_list[k++].list_filter_item.operator_type                   = EMAIL_LIST_FILTER_OPERATOR_AND;
 
                /* even index, rule value */
                filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
@@ -812,8 +916,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
                filter_list[k++].list_filter_item.rule.key_value.integer_type_value = mailbox_type;
        }
 
-
-       if ((err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
@@ -821,7 +924,7 @@ INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_typ
        EM_DEBUG_LOG_DEV ("conditional_clause_string[%s]", conditional_clause_string);
 
        /* Search the mail of priority sender in DB */
-       if ((err = emstorage_query_mail_count(conditional_clause_string, true, &total_count, &unread_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_query_mail_count(multi_user_name, conditional_clause_string, true, &total_count, &unread_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed:[%d]", err);
                goto FINISH_OFF;
        }
@@ -858,7 +961,7 @@ INTERNAL_FUNC int emcore_display_badge_count(int count)
        badge_error_e badge_err = BADGE_ERROR_NONE;
        bool exist;
 
-       if((badge_err = badge_is_existing("com.samsung.email", &exist)) != BADGE_ERROR_NONE) {
+       if((badge_err = badge_is_existing("org.tizen.email", &exist)) != BADGE_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("badge_is_existing failed [%d]", badge_err);
                err = EMAIL_ERROR_BADGE_API_FAILED;
                goto FINISH_OFF;
@@ -866,17 +969,27 @@ INTERNAL_FUNC int emcore_display_badge_count(int count)
 
        if (!exist) {
                /* create badge */
-               if((badge_err = badge_create("com.samsung.email", "/usr/bin/email-service")) != BADGE_ERROR_NONE) {
+               if((badge_err = badge_create("org.tizen.email", "/usr/bin/email-service")) != BADGE_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("badge_create failed [%d]", badge_err);
                        err = EMAIL_ERROR_BADGE_API_FAILED;
                        goto FINISH_OFF;
                }
        }
 
-       if((badge_err = badge_set_count("com.samsung.email", count)) != BADGE_ERROR_NONE) {
+       if((badge_err = badge_set_count("org.tizen.email", count)) != BADGE_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("badge_set_count failed [%d]", badge_err);
-               err = EMAIL_ERROR_BADGE_API_FAILED;
-               goto FINISH_OFF;
+               if (badge_err != BADGE_ERROR_SERVICE_NOT_READY) {
+                       err = EMAIL_ERROR_BADGE_API_FAILED;
+                       goto FINISH_OFF;
+               }
+
+               /* Badge callback function : When the badge service ready, call the callback function */
+               badge_err = badge_add_deferred_task(emcore_display_unread_in_badge, NULL);
+               if (badge_err != BADGE_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("badge_add_deferred_task failed : [%d]", badge_err);
+                       err = EMAIL_ERROR_BADGE_API_FAILED;
+                       goto FINISH_OFF;
+               }
        }
 
 FINISH_OFF:
@@ -885,21 +998,28 @@ FINISH_OFF:
        return err;
 }
 
-int emcore_display_unread_in_badge()
+void emcore_display_unread_in_badge(void *data)
 {
        EM_DEBUG_FUNC_BEGIN();
 
-       int ret = false;
        int err = EMAIL_ERROR_NONE;
        int total_unread_count = 0;
        int total_mail_count = 0;
        int unseen = 0;
+
+       /* UX changed : IF the NOTIFICATION is disable, the badge is disable */
+       /* UX Changed(2014/06/25) : Only using the general badge */
+#if 0
        int badge_status = false;
-       int general_noti_status = false;
-       int vip_noti_status = false;
+       int general_badge_status = false;
+       int vip_badge_status = false;
        int vip_mail_count = 0;
        int vip_unread_count = 0;
        int vip_notification_mode = 0;
+       int general_noti_status = 0;
+       int vip_noti_status = 0;
+       int general_badge_status = false;
+       int general_noti_status = 0;
 
        /* get general noti status */
        if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, &general_noti_status) != 0) {
@@ -917,47 +1037,63 @@ int emcore_display_unread_in_badge()
 
        if (!general_noti_status && !vip_noti_status) {
                EM_DEBUG_LOG("both notification disabled");
-               ret = true;
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_mail_count_by_query(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;
+       /* get general noti status */
+       if (general_noti_status) {
+               if (vconf_get_bool(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, &general_badge_status) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_get_int failed");
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
        }
 
+       /* get vip noti status */
        if (vip_noti_status) {
+               if (vconf_get_bool(VCONF_VIP_NOTI_BADGE_TICKER, &vip_badge_status) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_get_int failed");
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (!general_badge_status && !vip_badge_status) {
+               EM_DEBUG_LOG("both notification disabled");
+               goto FINISH_OFF;
+       }
+
+       if (general_badge_status) {
+               if (!emcore_get_mail_count_by_query(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;
+               }
+       }
+
+       if (vip_badge_status) {
                if (!emcore_get_mail_count_by_query(ALL_ACCOUNT, EMAIL_MAILBOX_TYPE_INBOX, true, &vip_mail_count, &vip_unread_count, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
                        goto FINISH_OFF;
                }
        }
 
-       if (general_noti_status && !vip_noti_status) {
+       if (general_badge_status && !vip_badge_status) {
                EM_DEBUG_LOG("GERNERAL NOTI MODE");
                vip_notification_mode = 0;
                unseen = total_unread_count;
-       } else if (!general_noti_status && vip_noti_status) {
+       } else if (!general_badge_status && vip_badge_status) {
                EM_DEBUG_LOG("VIP NOTI MODE");
                if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
                        EM_DEBUG_LOG("VIP filter not found");
-                       ret = true;
                        goto FINISH_OFF;
                } else {
                        unseen = vip_unread_count;
                }
                vip_notification_mode = 1;
-       } else if (general_noti_status && vip_noti_status) {
-               if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
-                       EM_DEBUG_LOG("VIP filter not found");
-                       EM_DEBUG_LOG("GERNERAL NOTI MODE");
-                       vip_notification_mode = 0;
-                       unseen = total_unread_count;
-               } else {
-                       EM_DEBUG_LOG("VIP NOTI MODE");
-                       vip_notification_mode = 1;
-                       unseen = vip_unread_count;
-               }
+       } else if (general_badge_status && vip_badge_status) {
+               EM_DEBUG_LOG("GERNERAL NOTI MODE");
+               vip_notification_mode = 0;
+               unseen = total_unread_count;
        }
 
        if (vip_notification_mode) {
@@ -976,58 +1112,72 @@ int emcore_display_unread_in_badge()
 
        if (!badge_status) {
                EM_DEBUG_LOG("badge setting OFF");
-               ret = true;
                goto FINISH_OFF;
        }
+#endif
 
-       if (unseen < 0)
-               goto FINISH_OFF;
-
-       if ((err = emcore_display_badge_count(unseen)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
+       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;
        }
-       
-       ret = true;
+
+       unseen = total_unread_count;
 
 FINISH_OFF:
-       
+
+       if (unseen <= 0) {
+               if ((err = emcore_display_badge_count(0)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
+               }
+       } else {
+               if ((err = emcore_display_badge_count(unseen)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
+               }
+       }
+
        EM_DEBUG_FUNC_END();
-       return ret;
 }
 
-static int emcore_layout_multi_noti(notification_h noti, int unread_mail, char *email_address, char *account_name)
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
+static notification_error_e emcore_layout_multi_noti(notification_h noti, int unread_mail, char *email_address, char *account_name)
 {
        EM_DEBUG_FUNC_BEGIN("unread_count %d", unread_mail);
+       char temp_buffer[512] = {0};
        notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
 
        noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
+               EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_MULTIPLE failed [%d]", noti_err);
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", "IDS_EMAIL_OPT_EMAIL", NOTIFICATION_VARIABLE_TYPE_NONE);
+       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "New email", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_EMAIL_TPOP_NEW_EMAIL_RECEIVED_ABB"), NOTIFICATION_VARIABLE_TYPE_NONE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New email received", "IDS_EMAIL_BODY_PD_NEW_EMAILS_RECEIVED", NOTIFICATION_VARIABLE_TYPE_INT, unread_mail, NOTIFICATION_VARIABLE_TYPE_NONE);
+       SNPRINTF(temp_buffer, sizeof(temp_buffer), "%d", unread_mail);
+       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, temp_buffer, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
+               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_EVENT_COUNT failed");
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, email_address, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (account_name)
+               SNPRINTF(temp_buffer, sizeof(temp_buffer), "%s, %s", email_address, account_name);
+       else
+               SNPRINTF(temp_buffer, sizeof(temp_buffer), "%s", email_address);
+
+       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, temp_buffer, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
+               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       noti_err = notification_set_time(noti, time(NULL));
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
+               EM_DEBUG_EXCEPTION("notification_set_time failed");
                goto FINISH_OFF;
        }
 
@@ -1037,7 +1187,7 @@ static int emcore_layout_multi_noti(notification_h noti, int unread_mail, char *
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_ICON_PATH);
+       noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_INDICATOR_ICON_PATH);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
                goto FINISH_OFF;
@@ -1055,7 +1205,7 @@ FINISH_OFF:
        return noti_err;
 }
 
-static int emcore_layout_single_noti(notification_h noti, char *account_name, int ids, char *display_sender, time_t time, char *subject)
+static notification_error_e emcore_layout_single_noti(notification_h noti, char *account_name, int ids, char *display_sender, time_t time, char *subject, int display_status)
 {
        EM_DEBUG_FUNC_BEGIN();
        notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
@@ -1067,15 +1217,15 @@ static int emcore_layout_single_noti(notification_h noti, char *account_name, in
        }
 
        if (ids)
-               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", "IDS_COM_BODY_EMAIL", NOTIFICATION_VARIABLE_TYPE_NONE);
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_ST_HEADER_EMAIL"), NOTIFICATION_VARIABLE_TYPE_NONE);
        else
-               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", "IDS_EMAIL_OPT_EMAIL", NOTIFICATION_VARIABLE_TYPE_NONE);
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
 
        if (noti_err != NOTIFICATION_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
                goto FINISH_OFF;
        }
-
+/*
        noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
@@ -1083,26 +1233,28 @@ static int emcore_layout_single_noti(notification_h noti, char *account_name, in
        }
 
        if (ids)
-               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, NULL, display_sender, NOTIFICATION_VARIABLE_TYPE_NONE);
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, NULL, display_sender, NOTIFICATION_VARIABLE_TYPE_NONE);
        else
-               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
 
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
+               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
                goto FINISH_OFF;
        }
+*/
+       if (display_status)
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       else
+               noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, "New email", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_EMAIL_TPOP_NEW_EMAIL_RECEIVED_ABB"), NOTIFICATION_VARIABLE_TYPE_NONE);
 
-       /*
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, time, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_SUB_1 failed");
+               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
                goto FINISH_OFF;
        }
-       */
 
-       noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       noti_err = notification_set_time(noti, time);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
+               EM_DEBUG_EXCEPTION("notification_set_time failed");
                goto FINISH_OFF;
        }
 
@@ -1112,7 +1264,7 @@ static int emcore_layout_single_noti(notification_h noti, char *account_name, in
                goto FINISH_OFF;
        }
 
-       noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_ICON_PATH);
+       noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_INDICATOR_ICON_PATH);
        if (noti_err != NOTIFICATION_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
                goto FINISH_OFF;
@@ -1129,138 +1281,900 @@ FINISH_OFF:
        return noti_err;
 }
 
-INTERNAL_FUNC int emcore_add_notification(int account_id, int mail_id, int unread_mail_count, int input_play_alert_tone, int sending_error, unsigned long display)
-{
-       EM_DEBUG_FUNC_BEGIN("account_id[%d] mail_id[%d] unread_mail_count[%d] input_play_alert_tone[%d]", account_id, mail_id, unread_mail_count, input_play_alert_tone );
-       int err = EMAIL_ERROR_NONE;
-
-       EM_DEBUG_FUNC_END("ret [%d]", err);
-       return err;
-}
-
-INTERNAL_FUNC int emcore_add_notification_for_send(int account_id, int mail_id, email_action_t action, int sending_error, unsigned long display)
-{
-       EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, action:%d", account_id, mail_id, action );
-       int err = EMAIL_ERROR_NONE;
-
-       EM_DEBUG_FUNC_END("ret [%d]", err);
-       return err;
-}
-
-INTERNAL_FUNC void emcore_update_notification_for_send(int account_id, int mail_id, double progress)
+static EMAIL_ALERT_TYPE emcore_get_alert_type(int vip_mode, int vibrate_status)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, progress: %f", account_id, mail_id, progress);
-
-       int private_id = 0;
-       char vconf_private_id[MAX_PATH] = {0, };
-       notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
-
-       SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
-       if (vconf_get_int(vconf_private_id, &private_id) != 0) {
-               EM_DEBUG_EXCEPTION("vconf_get_int failed");
+       EM_DEBUG_FUNC_BEGIN();
+       int global_sound_status = 0;
+       int global_vibe_status = 0;
+       int email_vibe_status = 0;
+       int call_state = 0;
+       int alert_type = EMAIL_ALERT_TYPE_MUTE;
+       int voicerecoder_state = 0;
+
+       if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &global_sound_status) != 0) {
+               EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_SOUND_STATUS_BOOL failed");
+               goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("Private_id = [%d]", private_id);
-       noti_err = notification_update_progress(g_sending_noti_handle, private_id, progress);
-       if (noti_err != NOTIFICATION_ERROR_NONE) {
-               EM_DEBUG_LOG("notification_update_progress failed [0x%x]", noti_err);
+       if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &global_vibe_status) != 0) {
+               EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOLfailed");
+               goto FINISH_OFF;
        }
 
-       EM_DEBUG_FUNC_END();
-}
-
-INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error)
-{
-       EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
+       EM_DEBUG_LOG("global_sound_status [%d] global_vibe_status [%d]", global_sound_status, global_vibe_status);
 
-       int ret = false;
-       int display = 0;
-       emstorage_mail_tbl_t *mail_table_data = NULL;
+       if (global_sound_status || global_vibe_status) {
 
-       if ((action == EMAIL_ACTION_SEND_MAIL || action == EMAIL_ACTION_SENDING_MAIL) && error != EMAIL_ERROR_CANCELLED) {
-       /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
+               if (vconf_get_int(VCONFKEY_VOICERECORDER_STATE, &voicerecoder_state) != 0) {
+                       EM_DEBUG_LOG("vconf_get_int for VCONFKEY_VOICERECORDER_STATE failed");
+               }
+               EM_DEBUG_LOG("voicerecoder_state [%d]", voicerecoder_state);
 
-               if (id <= 0) {
-                       EM_DEBUG_LOG("Invalid mail_id");
-                       return false;
+               if (vconf_get_int(VCONFKEY_CALL_STATE, &call_state) != 0) {
+                       EM_DEBUG_LOG("vconf_get_int for VCONFKEY_CALL_STATE failed");
                }
+               EM_DEBUG_LOG("call_state [%d] ", call_state);
 
-               if (!emstorage_get_mail_by_id(id, &mail_table_data, true, NULL)) {
-                       EM_DEBUG_LOG("Mail not found");
-                       return false;
+               if (vip_mode) {
+                       if (vconf_get_bool(VCONF_VIP_NOTI_VIBRATION_STATUS_BOOL, &email_vibe_status) != 0) {
+                               EM_DEBUG_EXCEPTION("vconf_get_bool failed");
+                               return EMAIL_ALERT_TYPE_NONE;
+                       }
+               }
+               else {
+                       email_vibe_status = vibrate_status;
                }
 
-               if (emcore_add_notification_for_send(mail_table_data->account_id, id, action, error, display) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("emcore_notification_set error");
-                       goto FINISH_OFF;
+               EM_DEBUG_LOG("email_vibe_status [%d] ", email_vibe_status);
+
+               if (voicerecoder_state == VCONFKEY_VOICERECORDER_RECORDING) {
+                       alert_type = EMAIL_ALERT_TYPE_VIB;
+                       EM_DEBUG_LOG("voice recorder is on recording...");
+               }
+               else if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {
+                       EM_DEBUG_LOG("Calling");
+                       if (global_sound_status)
+                               alert_type = EMAIL_ALERT_TYPE_MELODY;
+                       else
+                               alert_type = EMAIL_ALERT_TYPE_NONE;
+               }
+               else if (global_sound_status && email_vibe_status) {
+                       alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
+               }
+               else if (global_sound_status) {
+                       alert_type = EMAIL_ALERT_TYPE_MELODY;
+               }
+               else if (global_vibe_status) {
+                       alert_type = EMAIL_ALERT_TYPE_VIB;
                }
-               ret = true;
        }
 
 FINISH_OFF:
+       EM_DEBUG_FUNC_END("alert_type [%d]", alert_type);
+       return alert_type;
+}
 
-       if (mail_table_data && !emstorage_free_mail(&mail_table_data, 1, NULL))
-               EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
+static char *emcore_get_sound_file_path(int vip_mode, int default_ringtone_status, char *alert_ringtone_path)
+{
+       EM_DEBUG_FUNC_BEGIN("vip_mode [%d]", vip_mode);
+       char *ret = NULL;
+       int   use_default_ring_tone = 0;
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
-}
+       if (vip_mode) {
+               if (vconf_get_bool(VCONF_VIP_NOTI_USE_DEFAULT_RINGTONE_BOOL, &use_default_ring_tone) != 0)
+                       EM_DEBUG_LOG("vconf_get_bool for VCONF_VIP_NOTI_USE_DEFAULT_RINGTONE_BOOL failed");
 
+               EM_DEBUG_LOG("use_default_ring_tone [%d]", use_default_ring_tone);
 
-/* storage space handling - 210709 */
-int emcore_get_storage_status(void)
-{
-       EM_DEBUG_FUNC_BEGIN();
-       int storage_status = 0, nError = 0;
+               if (use_default_ring_tone)
+                       ret = vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
+               else
+                       ret = vconf_get_str(VCONF_VIP_NOTI_RINGTONE_PATH);
+       }
+       else {
+               use_default_ring_tone = default_ringtone_status;
 
-       g_type_init();
+               EM_DEBUG_LOG("use_default_ring_tone [%d]", use_default_ring_tone);
 
-#ifdef STORAGE_STATUS
-       nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
-                                                       &storage_status);
-#endif /*  STORAGE_STATUS */
+               if (use_default_ring_tone)
+                       ret = vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
+               else
+                       ret = EM_SAFE_STRDUP(alert_ringtone_path);
+       }
 
-       if (nError == -1) {
-               EM_DEBUG_EXCEPTION("vconf_get_int Failed");
-               return false;
+       if ((emcore_check_drm_file(ret, NULL)) && (!emcore_check_drm_is_ringtone(ret, NULL))) {
+               EM_DEBUG_LOG_DEV("The ringtone is not allowed DRM. : [%s]", ret);
+               EM_SAFE_FREE(ret);
+               ret = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR));
        }
-       EM_DEBUG_FUNC_END();
-       return storage_status;
+
+       EM_DEBUG_FUNC_END("ret [%s]", ret);
+       return ret;
 }
+#endif /* __FEATURE_NOTIFICATION_ENABLE__ */
 
-INTERNAL_FUNC int emcore_is_storage_full(int *err_code)
+#if 0 //using notification_status_message_post instead of calling email-app toast - change date: 30/9/2014
+INTERNAL_FUNC int emcore_show_toast_popup(char *input_popup_string)
 {
-       EM_DEBUG_FUNC_BEGIN();
-
-       int ret = false;
        int err = EMAIL_ERROR_NONE;
-       struct statfs buf = {0};
+       int ret = APP_CONTROL_ERROR_NONE;
+       app_control_h svc_handle = NULL;
 
-       if (statfs(DATA_PATH, &buf) == -1) {
-               EM_DEBUG_EXCEPTION("statfs(\"%s\") failed - %d", DATA_PATH, errno);
+       ret = app_control_create(&svc_handle);
+
+       if (ret != APP_CONTROL_ERROR_NONE || !svc_handle) {
+               EM_DEBUG_LOG("app_control_create() failed! ret:[%d]", ret);
                err = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
-       else  {
-               long i_free = (buf.f_bavail * buf.f_bsize) / (1024 * 1024);
-               EM_DEBUG_LOG_DEV("f_bavail[%d] f_bsize[%d]", buf.f_bavail, buf.f_bsize);
-               if (i_free < EMAIL_LIMITATION_FREE_SPACE) {
-                       EM_DEBUG_EXCEPTION("Not enough free space of storage [%ld] MB.", i_free);
-                       err = EMAIL_ERROR_MAIL_MEMORY_FULL;
-               }
+
+       ret = app_control_set_app_id(svc_handle, "com.samsung.email-tts-play");
+       if (ret != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_LOG("app_control_set_app_id() failed! ret:[%d]", ret);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
        }
 
-       if (err == EMAIL_ERROR_MAIL_MEMORY_FULL)
-               ret = true;
+       ret = app_control_add_extra_data(svc_handle, "email_misc_work_type", "1003");
+       if (ret != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_LOG("app_control_add_extra_data() failed! ret:[%d]", ret);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       ret = app_control_add_extra_data(svc_handle, "popup_string", input_popup_string);
+       if (ret != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_LOG("app_control_add_extra_data() failed! ret:[%d]", ret);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       ret = app_control_send_launch_request(svc_handle, NULL, NULL);
+       if (ret != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_LOG("app_control_send_launch_request() failed! ret:[%d]", ret);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
 
 FINISH_OFF:
-       if (err_code != NULL)
-               *err_code = err;
-       EM_DEBUG_FUNC_END("ret[%d]", ret);
+
+    if (svc_handle) {
+        ret = app_control_destroy(svc_handle);
+        if (ret != APP_CONTROL_ERROR_NONE) {
+            EM_DEBUG_LOG("app_control_destroy() failed! ret:[%d]", ret);
+            err = EMAIL_ERROR_SYSTEM_FAILURE;
+        }
+    }
+
+       return err;
+}
+#endif
+
+
+INTERNAL_FUNC int emcore_add_notification(char *multi_user_name, int account_id, int mail_id, int unread_mail_count, int vip_unread_mail_count, int input_play_alert_tone, int sending_error, unsigned long display)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d] mail_id[%d] unread_mail_count[%d] input_play_alert_tone[%d]", account_id, mail_id, unread_mail_count, input_play_alert_tone );
+       int err = EMAIL_ERROR_NONE;
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
+       int i = 0;
+       int private_id = 0;
+       int *p_mail_id = 0;
+       int unseen = 0;
+       int new_noti = 0;
+       int vip_unread_mail_count_t = 0;
+       int unread_mail_count_t = 0;
+       char *mailbox_name = NULL;
+       char vconf_private_id[MAX_PATH] = {0, };
+       notification_h noti = NULL;
+       notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
+       notification_vibration_type_e noti_vibe_type = NOTIFICATION_VIBRATION_TYPE_NONE;
+       emstorage_account_tbl_t *account_tbl = NULL;
+       emstorage_mail_tbl_t *p_mail_data = NULL;
+       int display_status = false;
+       int vip_noti_status = false;
+       int vip_notification_mode = 0;
+       EMAIL_ALERT_TYPE alert_type = EMAIL_ALERT_TYPE_NONE;
+       char *alert_tone_path = NULL;
+
+       /* For service bundle */
+       char buf[256] = {0,};
+       char **value = NULL;
+       app_control_h service = NULL;
+       bundle *b = NULL;
+
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_tbl, true, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       /* get vip noti status */
+       if (vconf_get_bool(VCONF_VIP_NOTI_NOTIFICATION_TICKER, &vip_noti_status) != 0) {
+               EM_DEBUG_EXCEPTION("vconf_get_int failed");
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (!account_tbl->options.notification_status && !vip_noti_status) {
+               EM_DEBUG_LOG("both notification disabled");
+               goto FINISH_OFF;
+       }
+
+       /* Get the unread mail id */
+       if (vip_noti_status && !emstorage_get_unread_mailid(multi_user_name, account_id, vip_noti_status, &p_mail_id, &vip_unread_mail_count_t, &err)) {
+               if (err != EMAIL_ERROR_MAIL_NOT_FOUND) {
+                       EM_DEBUG_EXCEPTION("VIP : emstorage_get_unread_mailid failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (!vip_unread_mail_count_t && !emstorage_get_unread_mailid(multi_user_name, account_id, 0, &p_mail_id, &unread_mail_count_t, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_unread_mailid failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (vip_noti_status) {
+               vip_notification_mode = (vip_unread_mail_count_t > 0) ? 1 : 0;
+       }
+
+       if (vip_notification_mode)
+               unseen = vip_unread_mail_count_t;
+       else  {
+               if (account_tbl->options.notification_status)
+                       unseen = unread_mail_count_t;
+               else {
+                       EM_DEBUG_LOG("general notification disabled");
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (vip_notification_mode) {
+               if (vconf_get_bool(VCONF_VIP_NOTI_DISPLAY_CONTENT_TICKER, &display_status) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_get_bool failed");
+                       err = EMAIL_ERROR_GCONF_FAILURE;
+                       goto FINISH_OFF;
+               }
+       } else {
+               display_status = account_tbl->options.display_content_status;
+       }
+
+       /* Load the previous noti */
+       SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
+       if (vconf_get_int(vconf_private_id, &private_id) != 0) {
+             EM_DEBUG_EXCEPTION("vconf_get_int failed");
+       }
+
+       /* Turn display on to blink LED */
+//     emdevice_change_display_state(DISPLAY_STATE_ON);
+
+       noti = notification_load(NULL, private_id);
+       if(noti == NULL) {
+               EM_DEBUG_EXCEPTION("notification_load failed");
+               noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
+               if(noti == NULL) {
+                       EM_DEBUG_EXCEPTION("notification_new failed");
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_pkgname(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_pkgname failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               new_noti = true;
+       }
+
+       if (input_play_alert_tone) {
+               alert_type = emcore_get_alert_type(vip_notification_mode, account_tbl->options.vibrate_status);
+
+               if (alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_MELODY)
+                       alert_tone_path = emcore_get_sound_file_path(vip_notification_mode, account_tbl->options.default_ringtone_status, account_tbl->options.alert_ringtone_path);
+       }
+
+       /* blocking mode : */
+       if (!emcore_get_blocking_mode_status()) {
+               if ((noti_err = notification_set_led_time_period(noti, 250, 2500)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_led_time_period failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_led failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               if ( (alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_MELODY) && EM_SAFE_STRLEN(alert_tone_path)) {
+                       if(strcmp(alert_tone_path, "silent") == 0) {
+                               if ((noti_err = notification_set_sound(noti, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL)) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_set_sound failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                       goto FINISH_OFF;
+                               }
+                       } else {
+                               if ((noti_err = notification_set_sound(noti, NOTIFICATION_SOUND_TYPE_USER_DATA, alert_tone_path)) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_set_sound failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                       goto FINISH_OFF;
+                               }
+                       }
+               }
+       }
+
+       if ( alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_VIB)
+               noti_vibe_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT;
+
+       if ((noti_err = notification_set_vibration(noti, noti_vibe_type, NULL)) != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_set_vibration failed [%d]", noti_err);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if ((noti_err = notification_set_text_domain(noti, NATIVE_EMAIL_DOMAIN, "/usr/apps/com.samsung.email/res/locale")) != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_set_text_domain failed [%d]", noti_err);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("unseen [%d] display_status [%d]", unseen, display_status);
+
+       /* set execution option to launch email-viewer when single incoming mail*/
+       app_control_create(&service);
+       if (!service) {
+               EM_DEBUG_EXCEPTION("service create failed");
+       }
+
+       if (unseen == 1) {
+               if (!emstorage_get_mail_by_id(multi_user_name, p_mail_id[0], &p_mail_data, false, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG_SEC("single layout:account_name[%s], alias_sender[%s], time[%d], subject[%s]", account_tbl->account_name, p_mail_data->alias_sender, p_mail_data->date_time, p_mail_data->subject);
+               if ((noti_err = emcore_layout_single_noti(noti, account_tbl->account_name, 0, p_mail_data->alias_sender, p_mail_data->date_time, p_mail_data->subject, display_status)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_layout_single_noti failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               snprintf(buf, sizeof(buf), "%d", 12);
+               app_control_add_extra_data(service, "RUN_TYPE", buf);
+               snprintf(buf, sizeof(buf), "%d", account_id);
+               app_control_add_extra_data(service, "ACCOUNT_ID", buf);
+               snprintf(buf, sizeof(buf), "%d", p_mail_data->mail_id);
+               app_control_add_extra_data(service, "MAIL_ID", buf);
+               snprintf(buf, sizeof(buf), "%d", p_mail_data->mailbox_id);
+               app_control_add_extra_data(service, "MAILBOX_ID", buf);
+               app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
+
+               app_control_to_bundle(service, &b);
+               noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
+               if (noti_err != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+       } else if (unseen > 1) {
+           EM_DEBUG_LOG_SEC("multi layout: unseen[%d], address[%s], account_name[%s]", unseen, account_tbl->user_email_address, account_tbl->account_name);
+               if ((noti_err = emcore_layout_multi_noti(noti, unseen, account_tbl->user_email_address, account_tbl->account_name)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_layout_multi_noti failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               snprintf(buf, sizeof(buf), "%d", 13);
+               app_control_add_extra_data(service, "RUN_TYPE", buf);
+               snprintf(buf, sizeof(buf), "%d", account_id);
+               app_control_add_extra_data(service, "ACCOUNT_ID", buf);
+
+               if (unseen > EMAIL_NOTI_MAX_MAIL_ID)
+                   unseen = EMAIL_NOTI_MAX_MAIL_ID;
+               value = (char **)em_malloc(unseen * sizeof(char *));
+               if (value == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               for (i = 0; i < unseen; i++) {
+                       memset(buf, 0x00, sizeof(buf));
+                       snprintf(buf, sizeof(buf), "%d", p_mail_id[i]);
+                       value[i] = EM_SAFE_STRDUP(buf);
+               }
+               app_control_add_extra_data_array(service, "MAIL_ID", (const char **)value, unseen);
+               app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
+
+               app_control_to_bundle(service, &b);
+               noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH, NULL, NULL, b);
+               if (noti_err != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+       } else { /* if priority sender exists and no mail of priority sender, unseen will be 0*/
+               EM_DEBUG_LOG("-unseen: [%d], priority_sender: [%d]", unseen, vip_notification_mode);
+               goto FINISH_OFF;
+               /* err = EMAIL_ERROR_PRIORITY_SENDER_MAIL_NOT_FOUND;*/ /*there is no mail for priority sender*/
+       }
+
+       if ((noti_err = notification_set_display_applist(noti, display)) != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (new_noti) {
+               if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG("Private_id = [%d]", private_id);
+
+               SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
+               if (vconf_set_int(vconf_private_id, private_id) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_set_int failed");
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+       } else {
+               if ((noti_err = notification_update(noti)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_update failed [%d]", noti_err);
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+       }
+
+       if (unseen) {
+               err = emcore_start_driving_mode(multi_user_name, p_mail_id[unseen - 1]);
+               if (err != EMAIL_ERROR_NONE) 
+                       EM_DEBUG_EXCEPTION("emcore_start_driving_mode failed : [%d]", err);
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(alert_tone_path);
+       EM_SAFE_FREE(p_mail_id); /* prevent */
+
+       if (p_mail_data)
+               emstorage_free_mail(&p_mail_data, 1, NULL);
+
+       if (account_tbl)
+               emstorage_free_account(&account_tbl, 1, NULL);
+
+       for (i = 0; i < unseen; i++) {
+               if (value)
+                       EM_SAFE_FREE(value[i]);
+       }
+       EM_SAFE_FREE(value);
+
+       EM_SAFE_FREE(mailbox_name);
+
+       notification_free(noti);
+
+       app_control_destroy(service);
+#endif /* __FEATURE_NOTIFICATION_ENABLE__ */
+
+       EM_DEBUG_FUNC_END("ret [%d]", err);
+       return err;
+}
+
+INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int account_id, int mail_id, email_action_t action, int sending_error, unsigned long display)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, action:%d", account_id, mail_id, action );
+       int err = EMAIL_ERROR_NONE;
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
+       int private_id = 0;
+       char *mailbox_name = NULL;
+       char vconf_private_id[MAX_PATH] = {0, };
+       notification_h noti = NULL;
+       notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
+       emstorage_mail_tbl_t *p_mail_data = NULL;
+       emstorage_account_tbl_t *account_tbl = NULL;
+
+       /* For service bundle */
+       char buf[256] = {0,};
+       app_control_h service = NULL;
+       bundle *b = NULL;
+
+       /* get general noti status */
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_tbl, true, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       if (!account_tbl->options.notification_status) {
+               EM_DEBUG_LOG("notification disabled");
+               goto FINISH_OFF;
+       }
+
+       if (!account_tbl->options.display_content_status) {
+               EM_DEBUG_LOG("display content disabled");
+               goto FINISH_OFF;
+       }
+
+       /* Delete the previous noti */
+       emcore_delete_notification_by_account(multi_user_name, account_id, true);
+       if (g_sending_noti_handle) {
+               if((noti_err = notification_free(g_sending_noti_handle)) != NOTIFICATION_ERROR_NONE)
+                       err = EMAIL_ERROR_NOTI;
+               g_sending_noti_handle = NULL;
+       }
+
+       if (action == EMAIL_ACTION_SENDING_MAIL)
+               noti = notification_create(NOTIFICATION_TYPE_ONGOING);
+       else
+               noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
+
+       if(noti == NULL) {
+               EM_DEBUG_EXCEPTION("notification_new failed");
+               err = EMAIL_ERROR_NOTI;
+               goto FINISH_OFF;
+       }
+
+       if ((noti_err = notification_set_pkgname(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_set_pkgname failed [%d]", noti_err);
+               err = EMAIL_ERROR_NOTI;
+               goto FINISH_OFF;
+       }
+
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &p_mail_data, false, &err)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
+               goto FINISH_OFF;
+       }
+
+       /* Create service bundle */
+       app_control_create(&service);
+       if (!service) {
+               EM_DEBUG_EXCEPTION("service create failed");
+       }
+
+       snprintf(buf, sizeof(buf), "%d", 12);
+       app_control_add_extra_data(service, "RUN_TYPE", buf);
+       snprintf(buf, sizeof(buf), "%d", account_id);
+       app_control_add_extra_data(service, "ACCOUNT_ID", buf);
+       snprintf(buf, sizeof(buf), "%d", p_mail_data->mail_id);
+       app_control_add_extra_data(service, "MAIL_ID", buf);
+       snprintf(buf, sizeof(buf), "%d", p_mail_data->mailbox_id);
+       app_control_add_extra_data(service, "MAILBOX_ID", buf);
+       app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
+
+       app_control_to_bundle(service, &b);
+       noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
+       if (noti_err != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       switch (action) {
+       case EMAIL_ACTION_SEND_MAIL :
+
+               setlocale(LC_MESSAGES, vconf_get_str(VCONFKEY_LANGSET));
+               bindtextdomain("sys_string", "/usr/share/locale");
+               textdomain("sys_string");
+
+               if (sending_error == EMAIL_ERROR_NONE) {
+                       /*if ((noti_err = emcore_layout_single_noti(noti, account_tbl->account_name, 1, "IDS_COM_POP_SENT", p_mail_data->date_time, p_mail_data->subject)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_layout_single_noti failed [%d]", err);
+                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                               goto FINISH_OFF;
+                       }*/
+
+                       if ((noti_err = notification_status_message_post(dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_EMAIL_SENT"))) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_status_message_post failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+               } else {
+                       /*if ((noti_err = emcore_layout_single_noti(noti, account_tbl->account_name, 1, "IDS_COM_POP_SENDING_FAILED", p_mail_data->date_time, p_mail_data->subject)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_layout_single_noti failed [%d]", err);
+                               err = EMAIL_ERROR_SYSTEM_FAILURE;
+                               goto FINISH_OFF;
+                       }*/
+
+                       switch(sending_error) {
+                       case EMAIL_ERROR_FLIGHT_MODE_ENABLE:
+                               EM_DEBUG_LOG("Flight mode enable");
+                               if ((noti_err = notification_status_message_post(dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_FAILED_TO_CONNECT_TO_NETWORK"))) != NOTIFICATION_ERROR_NONE)
+                               {
+                                       EM_DEBUG_EXCEPTION("notification_status_message_post failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                               break;
+
+                       case EMAIL_ERROR_NETWORK_NOT_AVAILABLE:
+                       case EMAIL_ERROR_NO_SIM_INSERTED:
+                               if ((noti_err = notification_status_message_post(dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_NETWORK_NOT_AVAILABLE_EMAIL_WILL_BE_SENT_WHEN_CONNECTED_TO_NETWORK"))) != NOTIFICATION_ERROR_NONE)
+                               {
+                                       EM_DEBUG_EXCEPTION("notification_status_message_post failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                               break;
+                       default:
+                               if ((noti_err = notification_status_message_post(dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_FAILED"))) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_status_message_post failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                       }
+                       if ((noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_set_layout failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+                       if ((noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+                       if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Sending failed", dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_FAILED"), NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+                       if (p_mail_data->subject) {
+                               if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, p_mail_data->subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                       } else {
+                               if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "No subject", NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       if (sending_error == EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE) {
+                               setlocale(LC_MESSAGES, vconf_get_str(VCONFKEY_LANGSET));
+                               bindtextdomain(NATIVE_EMAIL_DOMAIN, "/usr/apps/com.samsung.email/res/locale");
+                               textdomain(NATIVE_EMAIL_DOMAIN);
+                               if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_POP_THE_EMAIL_IS_TOO_LARGE"), NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
+                                       err = EMAIL_ERROR_NOTI;
+                                       goto FINISH_OFF;
+                               }
+                       }
+
+                       /*notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);*/
+
+                       if ((noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+                       if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+                               err = EMAIL_ERROR_NOTI;
+                               goto FINISH_OFF;
+                       }
+
+                       EM_DEBUG_LOG("Private_id = [%d]", private_id);
+
+                       SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
+                       if (vconf_set_int(vconf_private_id, private_id) != 0) {
+                               EM_DEBUG_EXCEPTION("vconf_set_int failed");
+                               err = EMAIL_ERROR_GCONF_FAILURE;
+                               goto FINISH_OFF;
+                       }
+               }
+
+               break;
+
+       case EMAIL_ACTION_SENDING_MAIL :
+
+               if ((noti_err = notification_set_text_domain(noti, NATIVE_EMAIL_DOMAIN, "/usr/apps/com.samsung.email/res/locale")) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_text_domain failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_PROGRESS)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_layout failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Sending email...", dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_EMAIL_ING"), NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, p_mail_data->subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               /*notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);*/
+
+               if ((noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+                       goto FINISH_OFF;
+               }
+
+               g_sending_noti_handle = noti;
+               EM_DEBUG_LOG("Private_id = [%d]", private_id);
+
+               SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
+               if ((err = vconf_set_int (vconf_private_id, private_id)) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_set_int failed [%d]", err);
+                       err = EMAIL_ERROR_GCONF_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               break;
+
+       default :
+               if ((noti_err = notification_free (noti)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION ("notification_free error [%d]", noti_err);
+                       err = EMAIL_ERROR_NOTI;
+               }
+               break;
+       }
+
+FINISH_OFF:
+
+    if ((action == EMAIL_ACTION_SEND_MAIL) && noti) {
+        if( (noti_err = notification_free(noti)) != NOTIFICATION_ERROR_NONE)
+            err = EMAIL_ERROR_NOTI;
+    }
+
+       if (p_mail_data)
+               emstorage_free_mail(&p_mail_data, 1, NULL);
+
+       if (account_tbl)
+               emstorage_free_account(&account_tbl, 1, NULL);
+
+       EM_SAFE_FREE(mailbox_name);
+
+       app_control_destroy(service);
+#endif /* __FEATURE_NOTIFICATION_ENABLE__ */
+       EM_DEBUG_FUNC_END("ret [%d]", err);
+       return err;
+}
+
+INTERNAL_FUNC void emcore_update_notification_for_send(int account_id, int mail_id, double progress)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, progress: %f", account_id, mail_id, progress);
+
+       int private_id = 0;
+       char vconf_private_id[MAX_PATH] = {0, };
+       notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
+
+       SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
+       if (vconf_get_int(vconf_private_id, &private_id) != 0) {
+               EM_DEBUG_EXCEPTION("vconf_get_int failed");
+       }
+
+       EM_DEBUG_LOG("Private_id = [%d]", private_id);
+       noti_err = notification_update_progress(g_sending_noti_handle, private_id, progress);
+       if (noti_err != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_LOG("notification_update_progress failed [0x%x]", noti_err);
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emcore_show_user_message(char *multi_user_name, int id, email_action_t action, int error)
+{
+       EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
+
+       int ret = false;
+       int display = 0;
+       emstorage_mail_tbl_t *mail_table_data = NULL;
+
+       if ((action == EMAIL_ACTION_SEND_MAIL || action == EMAIL_ACTION_SENDING_MAIL) && error != EMAIL_ERROR_CANCELLED) {
+       /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
+
+               if (id <= 0) {
+                       EM_DEBUG_LOG("Invalid mail_id");
+                       return false;
+               }
+
+               if (!emstorage_get_mail_by_id(multi_user_name, id, &mail_table_data, true, NULL)) {
+                       EM_DEBUG_LOG("Mail not found");
+                       return false;
+               }
+
+               EM_DEBUG_LOG ("smtp_stream : sent_end");
+               if (emcore_add_notification_for_send(multi_user_name, mail_table_data->account_id, id, action, error, display) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_notification_set error");
+                       goto FINISH_OFF;
+               }
+               ret = true;
+       }
+
+FINISH_OFF:
+
+       if (mail_table_data && !emstorage_free_mail(&mail_table_data, 1, NULL))
+               EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, int *output_size)
+
+/* storage space handling - 210709 */
+int emcore_get_storage_status(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int storage_status = 0, nError = 0;
+
+#ifdef STORAGE_STATUS
+       nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
+                                                       &storage_status);
+#endif /*  STORAGE_STATUS */
+
+       if (nError == -1) {
+               EM_DEBUG_EXCEPTION("vconf_get_int Failed");
+               return false;
+       }
+       EM_DEBUG_FUNC_END();
+       return storage_status;
+}
+
+
+
+INTERNAL_FUNC int emcore_is_storage_full()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int ret_from_storage_lib = 0;
+       struct statvfs stat_result;
+
+       ret_from_storage_lib = storage_get_internal_memory_size(&stat_result);
+       if (ret_from_storage_lib < 0) {
+               EM_DEBUG_EXCEPTION("ret_from_storage_lib [%d]", ret_from_storage_lib);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+       else {
+               unsigned long i_free = (stat_result.f_bsize*stat_result.f_bavail) / (1024 * 1024);
+               if (i_free < EMAIL_LIMITATION_FREE_SPACE) {
+                       EM_DEBUG_EXCEPTION("total[%lf] avail[%lf]", (double)stat_result.f_frsize*stat_result.f_blocks, (double)stat_result.f_bsize*stat_result.f_bavail);
+                       EM_DEBUG_EXCEPTION("Not enough free space of storage [%ld] MB.", i_free);
+                       err = EMAIL_ERROR_MAIL_MEMORY_FULL;
+               }
+       }
+
+FINISH_OFF:
+       EM_DEBUG_FUNC_END("err[%d]", err);
+       return err;
+}
+
+int emcore_calc_mail_size(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, int *output_size)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], output_size[%p]", input_mail_data, input_attachment_data_list, input_attachment_count, output_size);
 
@@ -1268,6 +2182,8 @@ int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_d
        int                    mail_size = 0; /*  size of the plain text body and attachments */
        int                    err       = EMAIL_ERROR_NONE;
        int                    i         = 0;
+    char                   *prefix_path = NULL;
+    char                   real_file_path[255] = {0};
 
        if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list) || !output_size)  {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -1275,8 +2191,21 @@ int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_d
                goto FINISH_OFF;
        }
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        if (input_mail_data->file_path_plain != NULL) {
-               if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_plain);
+
+               if (stat(real_file_path, &st_buf) < 0)  {
                        EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_plain : stat(\"%s\") failed...", input_mail_data->file_path_plain);
                        err = EMAIL_ERROR_FILE_NOT_FOUND;
                        goto FINISH_OFF;
@@ -1287,7 +2216,10 @@ int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_d
        }
 
        if (input_mail_data->file_path_html != NULL) {
-               if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_html);
+
+               if (stat(real_file_path, &st_buf) < 0) {
                        EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_html : stat(\"%s\") failed...", input_mail_data->file_path_html);
                        err = EMAIL_ERROR_FILE_NOT_FOUND;
                        goto FINISH_OFF;
@@ -1297,7 +2229,10 @@ int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_d
        }
 
        for(i = 0; i < input_attachment_count; i++)  {
-               if (stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_attachment_data_list[i].attachment_path);
+
+               if (stat(real_file_path, &st_buf) < 0)  {
                        EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
                        err = EMAIL_ERROR_FILE_NOT_FOUND;
                        goto FINISH_OFF;
@@ -1309,6 +2244,8 @@ int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_d
 
 FINISH_OFF:
 
+       EM_SAFE_FREE(prefix_path);
+
        EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
        return err;
 }
@@ -1331,6 +2268,9 @@ char *emcore_get_alias_of_mailbox(const char *mailbox_path)
 
        if(token_list == NULL) {
                EM_DEBUG_LOG("g_strsplit_set failed.");
+        if (mailbox)
+            g_free(mailbox);
+
                return NULL;
        }
 
@@ -1341,12 +2281,22 @@ char *emcore_get_alias_of_mailbox(const char *mailbox_path)
                index++;
 
        name = g_strdup(token_list[index - 1]);
-       if(!name) /* prevent 27459 */
+       if (!name) /* prevent 27459 */ {
+        if (mailbox)
+            g_free(mailbox);
+
                return NULL;
+    }
 
        g_strfreev(token_list);
 
        converted_name = emcore_convert_mutf7_to_utf8(name);
+       EM_DEBUG_LOG_DEV("converted_name:%s", converted_name);
+
+       if (!converted_name || EM_SAFE_STRLEN(converted_name) == 0) {
+        EM_SAFE_FREE(converted_name);
+               converted_name = g_strdup(name);
+       }
 
        if (name)
                g_free(name);
@@ -1356,70 +2306,53 @@ char *emcore_get_alias_of_mailbox(const char *mailbox_path)
 }
 
 
-static int emcore_get_first_address(const char *full_address, char **alias, char **address)
+INTERNAL_FUNC int emcore_get_first_address(const char *input_full_address, char **output_display_name, char **output_angle_addr)
 {
-       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_FUNC_BEGIN("input_full_address[%p], output_display_name[%p], output_angle_addr[%p]", input_full_address, output_display_name, output_angle_addr);
 
-       if (full_address == NULL || alias == NULL || address == NULL){
-               EM_DEBUG_EXCEPTION("Invalid Param  :  full_address[%p], alias[%p], address[%p]", full_address, alias, address);
-               return false;
+       int err = EMAIL_ERROR_NONE;
+       char *full_address = NULL;
+       char angle_addr[MAX_EMAIL_ADDRESS_LENGTH] = { 0, };
+       ADDRESS *address_list = NULL;
+
+       if (input_full_address == NULL || output_display_name == NULL || output_angle_addr == NULL) {
+               err = EMAIL_ERROR_INVALID_PARAM;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               goto FINISH_OFF;
        }
 
-       char *s = NULL;
-       char *alias_start = NULL;
-       char *alias_end = NULL;
-       char *alias_cursor = NULL;
-       char *address_start = NULL;
-       char *address_end = NULL;
-       char *first_address = NULL;
+       /* full_address will be tainted by rfc822_parse_adrlist */
+       /* So, input_full_address should be duplicated */
+       full_address = EM_SAFE_STRDUP(input_full_address);
 
-       if (full_address){
-               s = (char *)strchr((char *)full_address, ';');
-               if (s == NULL)
-                       first_address = strdup(full_address);   /*  only one  address */
-               else
-                       first_address = strndup(full_address, s - full_address);        /*  over two addresses */
-
-               /*  get alias */
-               *alias = NULL;
-               if ((alias_start = (char *)strchr((char *)first_address, '\"'))){
-                       alias_start++;
-                       alias_cursor = alias_start;
-                       while ((alias_cursor = (char *)strchr((char *)(alias_cursor), '\"'))){
-                               alias_end = alias_cursor;
-                               alias_cursor++;
-                               if (*alias_cursor == 0)
-                                       break;
-                       }
-                       if (alias_end)  {       /*  there is "alias" */
-                               *alias = strndup(alias_start, alias_end - alias_start);
-                               EM_DEBUG_LOG("alias [%s]", *alias);
-                       }
-               }
+       if (full_address == NULL) {
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
+               goto FINISH_OFF;
+       }
 
-               /*  get address */
-               *address = NULL;
-               if (alias_end == NULL)
-                       s = first_address;
-               else
-                       s = alias_end+1;
-               if ((address_start = (char *)strchr((char  *)s, '<'))){
-                       address_start++;
-                       if ((address_end = (char *)strchr((char  *)address_start, '>')))
-                               *address = strndup(address_start, address_end - address_start); /*  (alias) <(addr)>  ... */
-                       else
-                               *address = strdup(s);
-               }
-               else
-              *address = strdup(s);    /*  (addr) ; ...                 :  no alias */
+       rfc822_parse_adrlist(&address_list, (char*)full_address, NULL);
+
+       if (address_list) {
+               if (address_list->host)
+                       SNPRINTF(angle_addr, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", address_list->mailbox, address_list->host);
+               else /* For SMS of eas-engine  : (example : 01012345678) */
+                       SNPRINTF(angle_addr, MAX_EMAIL_ADDRESS_LENGTH, "%s", address_list->mailbox);
+
+               *output_display_name  = EM_SAFE_STRDUP(address_list->personal);
+               *output_angle_addr    = EM_SAFE_STRDUP(angle_addr);
+
+               mail_free_address(&address_list);
        }
 
-       EM_SAFE_FREE(first_address);
+FINISH_OFF:
+       EM_SAFE_FREE(full_address);
+
        EM_DEBUG_FUNC_END();
-       return true;
+       return err;
 }
 
-void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data)
+void emcore_fill_address_information_of_mail_tbl(char *multi_user_name, emstorage_mail_tbl_t *mail_data)
 {
        EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
 
@@ -1434,11 +2367,13 @@ void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data
 
        email_account_t *ref_account = NULL;
 
-       ref_account = emcore_get_account_reference(mail_data->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mail_data->account_id);
        if (!ref_account)  {
                EM_DEBUG_LOG("emcore_get_account_reference failed [%d]", mail_data->account_id);
        }
 
+       EM_DEBUG_LOG_SEC("full_address_from [%s]", mail_data->full_address_from);
+
        /*  sender alias & address */
        if (emcore_get_first_address(mail_data->full_address_from, &first_alias, &first_address) == true) {
                if (first_alias == NULL) {
@@ -1452,6 +2387,8 @@ void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data
                first_address = NULL;
        }
 
+       EM_DEBUG_LOG_SEC("full_address_from [%s]", mail_data->full_address_from);
+
        /*  recipient alias & address */
        if (mail_data->full_address_to != NULL)
                recipient = mail_data->full_address_to;
@@ -1622,7 +2559,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code)
+int emcore_strip_mail_body_from_file(char *multi_user_name, emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail[%p]", mail);
 
@@ -1631,10 +2568,12 @@ int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped
        char *buf = NULL;
        char *encoding_type = NULL;
        char *utf8_encoded_string = NULL;
+    char *prefix_path = NULL;
+    char real_html_path[255] = {0};
+    char real_plain_path[255] = {0};
        unsigned int byte_read = 0;
        unsigned int byte_written = 0;
        GError *glib_error = NULL;
-       FILE *fp_html = NULL;
        FILE *fp_plain = NULL;
        struct stat  st_buf;
 
@@ -1644,76 +2583,59 @@ int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped
                goto FINISH_OFF;
        }
 
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        /* read file to buf & strip if html text */
        if (mail->file_path_html) {
+               char result_buffer[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
+
                if (mail->preview_text) {
                        *stripped_text = EM_SAFE_STRDUP(mail->preview_text);
                        ret = true;
                        goto FINISH_OFF;
                }
 
-               if((err = em_get_encoding_type_from_file_path(mail->file_path_html, &encoding_type)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-
-               if (stat(mail->file_path_html, &st_buf) < 0) {
-                       EM_DEBUG_EXCEPTION_SEC("stat(\"%s\") failed...", mail->file_path_html);
-                       err = EMAIL_ERROR_FILE_NOT_FOUND;
-                       goto FINISH_OFF;
-               }
-
-               if (!(fp_html = fopen(mail->file_path_html, "r"))) {
-                       EM_DEBUG_EXCEPTION_SEC("fopen failed [%s]", mail->file_path_html);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
+        SNPRINTF(real_html_path, sizeof(real_html_path), "%s%s", prefix_path, mail->file_path_html);
 
-               if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
-                       EM_DEBUG_LOG("html_file is empty size");
-                       err = EMAIL_ERROR_EMPTY_FILE;
+               if ((err = em_get_encoding_type_from_file_path(mail->file_path_plain, &encoding_type)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!(buf = (char*)em_malloc(sizeof(char)*(st_buf.st_size + 1)))) {
-                       EM_DEBUG_EXCEPTION("em_malloc failed");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+               if ((err = emcore_strip_HTML_tag(real_html_path, encoding_type, result_buffer, MAX_PREVIEW_TEXT_LENGTH)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_strip_HTML_tag failed");
                        goto FINISH_OFF;
                }
 
-               byte_read = fread(buf, sizeof(char), st_buf.st_size, fp_html);
-
-               if (byte_read <= 0) {
-                       EM_SAFE_FREE(buf);
-                       if (ferror(fp_html)) {
-                               EM_DEBUG_EXCEPTION_SEC("fread failed [%s]", mail->file_path_html);
-                               err = EMAIL_ERROR_SYSTEM_FAILURE;
-                               goto FINISH_OFF;
-                       }
-               } else {
-                       if ((err = emcore_strip_HTML(&buf)) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_strip failed");
-                               goto FINISH_OFF;
-                       }
-               }
+               *stripped_text = EM_SAFE_STRDUP(result_buffer);
        }
 
        if (!buf && mail->file_path_plain) {
+        SNPRINTF(real_plain_path, sizeof(real_plain_path), "%s%s", prefix_path, mail->file_path_plain);
+
                if ((err = em_get_encoding_type_from_file_path(mail->file_path_plain, &encoding_type)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
                        goto FINISH_OFF;
                }
 
                memset(&st_buf, 0, sizeof(struct stat));
-               if (stat(mail->file_path_plain, &st_buf) < 0) {
-                       EM_DEBUG_EXCEPTION_SEC("stat(\"%s\") failed...", mail->file_path_plain);
+               if (stat(real_plain_path, &st_buf) < 0) {
+                       EM_DEBUG_EXCEPTION_SEC("stat(\"%s\") failed...", real_plain_path);
                        err = EMAIL_ERROR_INVALID_MAIL;
                        goto FINISH_OFF;
                }
 
-               if (!(fp_plain = fopen(mail->file_path_plain, "r"))) {
-                       EM_DEBUG_EXCEPTION_SEC("fopen failed [%s]", mail->file_path_plain);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+               err = em_fopen(real_plain_path, "r", &fp_plain);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION_SEC("em_fopen failed [%s]", real_plain_path);
                        goto FINISH_OFF;
                }
 
@@ -1734,7 +2656,7 @@ int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped
                        EM_SAFE_FREE(buf);
                        err = EMAIL_ERROR_NULL_VALUE;
                        if (ferror(fp_plain)) {
-                               EM_DEBUG_EXCEPTION_SEC("fread failed [%s]", mail->file_path_plain);
+                               EM_DEBUG_EXCEPTION_SEC("fread failed [%s]", real_plain_path);
                                err = EMAIL_ERROR_SYSTEM_FAILURE;
                        }
                        goto FINISH_OFF;
@@ -1775,6 +2697,7 @@ int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped
 
                        *stripped_text = EM_SAFE_STRDUP(utf8_encoded_string);
                } else {
+            EM_SAFE_FREE(*stripped_text);
                        *stripped_text = EM_SAFE_STRDUP(buf);
                }
        }
@@ -1787,12 +2710,10 @@ FINISH_OFF:
                *err_code = err;
 
        EM_SAFE_FREE(buf);
+       EM_SAFE_FREE(prefix_path);
        EM_SAFE_FREE(encoding_type);
        EM_SAFE_FREE(utf8_encoded_string);
 
-       if (fp_html != NULL)
-               fclose(fp_html);
-
        if (fp_plain != NULL)
                fclose(fp_plain);
 
@@ -1802,7 +2723,7 @@ FINISH_OFF:
 #endif
 
 
-int emcore_get_preview_text_from_file(const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer)
+int emcore_get_preview_text_from_file(char *multi_user_name, const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer)
 {
        EM_DEBUG_FUNC_BEGIN("input_plain_path[%p], input_html_path[%p], input_preview_buffer_length [%d], output_preview_buffer[%p]", input_plain_path, input_html_path, input_preview_buffer_length, output_preview_buffer);
 
@@ -1813,10 +2734,11 @@ int emcore_get_preview_text_from_file(const char *input_plain_path, const char *
        char        *local_preview_text = NULL;
        char        *encoding_type = NULL;
        char        *utf8_encoded_string = NULL;
-       FILE        *fp_html = NULL;
        FILE        *fp_plain = NULL;
        GError      *glib_error = NULL;
        struct stat  st_buf;
+    char *prefix_path = NULL;
+    char real_file_path[255] = {0};
 
        if (!output_preview_buffer) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -1826,56 +2748,40 @@ int emcore_get_preview_text_from_file(const char *input_plain_path, const char *
 
        local_preview_buffer_length = input_preview_buffer_length * 2;
 
-       if ( input_html_path ) { /*prevent 26249*/
-               /*      get preview text from html file */
-               if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-
-               if (stat(input_html_path, &st_buf) < 0)  {
-                       EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_html_path);
-                       err = EMAIL_ERROR_FILE_NOT_FOUND;
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
                        goto FINISH_OFF;
                }
+       } else {
+               prefix_path = strdup("");
+       }
+       
+       if (input_html_path) { /*prevent 26249*/
+               /*      get preview text from html file */
+               char result_buffer[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
 
-               if (!(fp_html = fopen(input_html_path, "r")))   {
-                       EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_html_path);
 
-               if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
-                       EM_DEBUG_LOG("input_html_file is empty size");
-                       err = EMAIL_ERROR_EMPTY_FILE;
+               if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!(local_preview_text = (char*)em_malloc(sizeof(char) * (st_buf.st_size + 1)))) {
-                       EM_DEBUG_EXCEPTION("em_malloc failed");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+               if ((err = emcore_strip_HTML_tag(real_file_path, encoding_type, result_buffer, MAX_PREVIEW_TEXT_LENGTH)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_strip_HTML_tag failed");
                        goto FINISH_OFF;
                }
+               local_preview_text = EM_SAFE_STRDUP(result_buffer);
+       }
 
-               byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp_html);
+       if (!local_preview_text && input_plain_path) { /*prevent 26249*/
 
-               if(byte_read <= 0) { /*prevent 26249*/
-                       EM_SAFE_FREE(local_preview_text);
-                       if (ferror(fp_html)) {
-                               EM_DEBUG_EXCEPTION("fread failed [%s]", input_html_path);
-                               err = EMAIL_ERROR_SYSTEM_FAILURE;
-                               goto FINISH_OFF;
-                       }
-               }
-               else {
-                       if ( (err = emcore_strip_HTML(&local_preview_text)) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_strip failed");
-                               goto FINISH_OFF;
-                       }
-               }
-       }
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_plain_path);
 
-       if ( !local_preview_text && input_plain_path) { /*prevent 26249*/
                /*  get preview text from plain text file */
                if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
@@ -1883,15 +2789,15 @@ int emcore_get_preview_text_from_file(const char *input_plain_path, const char *
                }
 
                memset(&st_buf, 0, sizeof(struct stat));
-               if (stat(input_plain_path, &st_buf) < 0)  {
+               if (stat(real_file_path, &st_buf) < 0)  {
                        EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_plain_path);
                        err = EMAIL_ERROR_INVALID_MAIL;
                        goto FINISH_OFF;
                }
 
-               if (!(fp_plain = fopen(input_plain_path, "r")))  {
-                       EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
-                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+               err = em_fopen(real_file_path, "r", &fp_plain);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("em_fopen failed [%s]", input_plain_path);
                        goto FINISH_OFF;
                }
 
@@ -1920,10 +2826,9 @@ int emcore_get_preview_text_from_file(const char *input_plain_path, const char *
                reg_replace(local_preview_text, CR_STRING, " ");
                reg_replace(local_preview_text, LF_STRING, " ");
                reg_replace(local_preview_text, TAB_STRING, " ");
-       }
 
-       if(local_preview_text) {
                em_trim_left(local_preview_text);
+
                if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
                        EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
 
@@ -1944,6 +2849,7 @@ int emcore_get_preview_text_from_file(const char *input_plain_path, const char *
 
                                utf8_encoded_string = (char *)g_convert(local_preview_text, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
                                if (utf8_encoded_string == NULL) {
+                                       EM_DEBUG_LOG("g_convert fail, again");
                                        goto FINISH_OFF;
                                }
                        }
@@ -1964,9 +2870,7 @@ FINISH_OFF:
 
        EM_SAFE_FREE(local_preview_text);
        EM_SAFE_FREE(encoding_type);
-
-       if (fp_html != NULL)
-               fclose(fp_html);
+       EM_SAFE_FREE(prefix_path);
 
        if (fp_plain != NULL)
                fclose(fp_plain);
@@ -2086,7 +2990,7 @@ INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
 
 #include <regex.h>
 
-int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
+INTERNAL_FUNC int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
 {
        int         error_code = EMAIL_ERROR_NONE;
        char       *pos = NULL;
@@ -2162,14 +3066,18 @@ FINISH_OFF:
 }
 
 
-int reg_replace_new (char **input_source_text, char *input_old_pattern_string, char *input_new_string)
+INTERNAL_FUNC int reg_replace_new (char **input_source_text, char *input_old_pattern_string, char *input_new_string)
 {
        char *replaced_str = NULL;
        int error_code = EMAIL_ERROR_NONE;
        GRegex *regex = NULL;
        GError *error = NULL;
 
-       regex = g_regex_new(input_old_pattern_string, G_REGEX_RAW | G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, &error);
+       if (input_source_text == NULL || *input_source_text == NULL) {
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+
+       regex = g_regex_new(input_old_pattern_string, G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &error);
 
        if (!regex) {
                EM_DEBUG_LOG("g_regex_new failed");
@@ -2177,7 +3085,7 @@ int reg_replace_new (char **input_source_text, char *input_old_pattern_string, c
                goto FINISH_OFF;
        }
 
-       replaced_str = g_regex_replace_literal(regex, *input_source_text, strlen(*input_source_text), 0, input_new_string, 0, &error);
+       replaced_str = g_regex_replace_literal(regex, *input_source_text, EM_SAFE_STRLEN(*input_source_text), 0, input_new_string, 0, &error);
 
        EM_SAFE_FREE(*input_source_text);
 
@@ -2190,50 +3098,127 @@ FINISH_OFF:
 }
 
 
-int emcore_strip_HTML(char **source_string)
+#include <libxml/HTMLparser.h>
+
+static void emcore_get_content_string(xmlNode *input_node, char *input_result_buffer, int input_result_buffer_legnth, int *input_exit_flag)
+{
+    xmlNode *cur_node = NULL;
+    char    *temp_content_string = NULL;
+
+    if (input_node == NULL || input_result_buffer == NULL || input_exit_flag == NULL) {
+       EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+       goto FINISH_OFF;
+    }
+
+    for (cur_node = input_node; cur_node; cur_node = cur_node->next) {
+               if (cur_node->name && (strcasecmp((const char *)cur_node->name, "head") == 0 || strcasecmp((const char *)cur_node->name, "title") == 0))
+                       continue;
+
+               if (cur_node->type == XML_TEXT_NODE && cur_node->content) {
+                       if ((EM_SAFE_STRLEN(cur_node->content) + EM_SAFE_STRLEN(input_result_buffer)) >= input_result_buffer_legnth) {
+                               int len1 = EM_SAFE_STRLEN(input_result_buffer);
+
+                               if (len1 >= 0 && len1 < input_result_buffer_legnth) {
+                                       int remain_size = input_result_buffer_legnth - len1 - 2;
+                                       char *remain_str = em_malloc(remain_size);
+                                       if (remain_str && cur_node->content) {
+                                               snprintf(remain_str, remain_size, "%s", (char*)(cur_node->content));
+                                       }
+
+                                       if (remain_str)
+                                               EM_SAFE_STRCAT(input_result_buffer, remain_str);
+
+                                       EM_SAFE_FREE(remain_str);
+                               }
+
+                               *input_exit_flag = 1;
+                               break;
+                       }
+                       temp_content_string = EM_SAFE_STRDUP((const char *)cur_node->content);
+                       if (temp_content_string) {
+                               reg_replace_new(&temp_content_string, "[ \t\r\n\v\f]+", " ");
+                               EM_SAFE_STRCAT(input_result_buffer, temp_content_string);
+                               EM_SAFE_FREE(temp_content_string);
+                       }
+               }
+
+               if (cur_node->children)
+                       emcore_get_content_string(cur_node->children, input_result_buffer, input_result_buffer_legnth, input_exit_flag);
+
+               if (*input_exit_flag == 1)
+                       break;
+    }
+FINISH_OFF:
+       return;
+}
+
+static void emcore_default_xml_error_handler(void *ctx, const char *msg, ...) {
+       EM_DEBUG_EXCEPTION("htmlReadFile failed");
+}
+
+int emcore_strip_HTML_tag(const char *input_html_file_path, char *input_encoding_type, char *output_result_buffer, int input_result_buffer_legnth)
 {
-       EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
+       EM_DEBUG_FUNC_BEGIN("input_html_file_path[%p] input_encoding_type[%s] output_result_buffer[%p] input_result_buffer_legnth[%d]", input_html_file_path, input_encoding_type, output_result_buffer, input_result_buffer_legnth);
+       int err = EMAIL_ERROR_NONE;
+       int exit_flag = 0;
+       char *result_string = NULL;
+       xmlNode *root_element = NULL;
+       htmlDocPtr result_html_document = NULL;
+       char *encoding_type = NULL;
+       char *utf_charset = "UTF-8";
+       char *euc_kr_charset = "euc-kr";
 
-       int result = EMAIL_ERROR_NONE;
+       if (input_html_file_path == NULL || output_result_buffer == NULL || input_result_buffer_legnth == 0) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
 
-       /* strip out CR, LF */
-       reg_replace_new(source_string, "(\r|\n)", "");
+       result_string = em_malloc(sizeof(char) * input_result_buffer_legnth);
 
-       /* strip out HEAD */
-       reg_replace_new(source_string, "<head[^>]*>.*<*/head>", "");
+       if (result_string == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
 
-       /* strip out STYLE */
-       reg_replace_new(source_string, "<style[^>]*>.*<*/style>", "");
+       if (EM_SAFE_STRCMP(input_encoding_type, "unknown") == 0)
+               encoding_type = utf_charset;
+       else if (input_encoding_type && (strcasecmp(input_encoding_type, "ks_c_5601-1987") == 0 ||
+               strcasecmp(input_encoding_type, "ksc5601") == 0 ||
+               strcasecmp(input_encoding_type, "cp949") == 0))
+               encoding_type = euc_kr_charset;
+       else
+               encoding_type = input_encoding_type;
 
-       /* strip out SCRIPT */
-       reg_replace_new(source_string, "<script[^>]*>.*<*/script>", "");
+       EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
 
-       /* strip out ALL TAG & comment */
-       reg_replace_new(source_string, "<[^>]*>", "");
+       LIBXML_TEST_VERSION
+       xmlSetGenericErrorFunc(NULL, emcore_default_xml_error_handler);
+       result_html_document = htmlReadFile(input_html_file_path, encoding_type, HTML_PARSE_RECOVER | HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR | HTML_PARSE_NONET | HTML_PARSE_NOBLANKS);
+       if (result_html_document == NULL) {
+               EM_DEBUG_EXCEPTION("htmlReadFile failed");
+               err = EMAIL_ERROR_ON_PARSING;
+               goto FINISH_OFF;
+       }
 
-       /* for remaining comment in some cases */
-       reg_replace_new(source_string, "-->", "");
+       root_element = xmlDocGetRootElement(result_html_document);
 
-       /* strip out html entities */
-       reg_replace_new(source_string, "&(quot|#34|#034);", "\"");
-       reg_replace_new(source_string, "&(#35|#035);", "#");
-       reg_replace_new(source_string, "&(#36|#036);", "$");
-       reg_replace_new(source_string, "&(#37|#037);", "%");
-       reg_replace_new(source_string, "&(amp|#38|#038);", "&");
-       reg_replace_new(source_string, "&(lt|#60|#060);", "<");
-       reg_replace_new(source_string, "&(gt|#62|#062);", ">");
-       reg_replace_new(source_string, "&(#64|#064);", "@");
-       reg_replace_new(source_string, "&(lsquo|rsquo);", "'");
-       reg_replace_new(source_string, "&(ldquo|rdquo);", "\"");
-       reg_replace_new(source_string, "&sbquo;", ",");
-       reg_replace_new(source_string, "&(trade|#x2122);", "(TM)");
 
-       /* strip out all type of spaces */
-       reg_replace_new(source_string, "&(nbsp|#160);", " ");
-       reg_replace_new(source_string, "  +", " ");
+       emcore_get_content_string(root_element, result_string, input_result_buffer_legnth, &exit_flag);
+       reg_replace_new(&result_string, "[ \t\r\n\v\f]+", " ");
+       EM_SAFE_STRCPY(output_result_buffer, result_string);
+       // em_trim_left(output_result_buffer);
 
-       EM_DEBUG_FUNC_END();
-       return result;
+FINISH_OFF:
+       if (result_html_document) {
+               xmlFreeDoc(result_html_document);
+       }
+
+       EM_SAFE_FREE(result_string);
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
@@ -2271,7 +3256,7 @@ INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_na
 FINISH_OFF:
 
        EM_SAFE_FREE(param_string);
-       EM_DEBUG_FUNC_END();
+       EM_DEBUG_FUNC_END();
        return error_code;
 }
 
@@ -2283,13 +3268,13 @@ int convert_app_err_to_email_err(int app_error)
        int err = EMAIL_ERROR_NONE;
 
        switch(app_error) {
-       case SERVICE_ERROR_NONE :
+       case APP_CONTROL_ERROR_NONE :
                err = EMAIL_ERROR_NONE;
                break;
-       case SERVICE_ERROR_INVALID_PARAMETER :
+       case APP_CONTROL_ERROR_INVALID_PARAMETER :
                err = EMAIL_ERROR_INVALID_PARAM;
                break;
-       case SERVICE_ERROR_OUT_OF_MEMORY :
+       case APP_CONTROL_ERROR_OUT_OF_MEMORY :
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                break;
        default:
@@ -2300,14 +3285,15 @@ int convert_app_err_to_email_err(int app_error)
        return err;
 }
 
-INTERNAL_FUNC int emcore_start_driving_mode(int account_id)
+INTERNAL_FUNC int emcore_start_driving_mode(char *multi_user_name, int mail_id)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
-       int err = SERVICE_ERROR_NONE;
-       int tts_enable = 0;     
-       char string_account[10] = { 0 };
-       service_h service = NULL;
+
+       int err = APP_CONTROL_ERROR_NONE;
+       int tts_enable = 0;
+       char string_mail[10] = { 0 };
+       emstorage_mail_tbl_t *p_mail_data = NULL;
+       app_control_h service = NULL;
 
        if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE, &tts_enable) != 0) {
                EM_DEBUG_EXCEPTION("vconf_get_int failed");
@@ -2330,58 +3316,83 @@ INTERNAL_FUNC int emcore_start_driving_mode(int account_id)
        if (!tts_enable)
                goto FINISH_OFF;
 
-       SNPRINTF(string_account, sizeof(string_account), "%d", account_id);
+       if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &p_mail_data, false, NULL)) {
+               EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed");
+               goto FINISH_OFF;
+       }
+
+       if (p_mail_data->tag_id >= 0)
+               goto FINISH_OFF;
 
-       err = service_create(&service);
-       if (err != SERVICE_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("service_create failed : [%d]", err);
+       SNPRINTF(string_mail, sizeof(string_mail), "%d", mail_id);
+
+       err = app_control_create(&service);
+       if (err != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("app_control_create failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       err = service_set_package(service, "com.samsung.email-tts-play");
-       if (err != SERVICE_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("service_set_package failed : [%d]", err);
+       err = app_control_set_app_id(service, "org.tizen.email-tts-play");
+       if (err != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("app_control_set_app_id failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       err = service_add_extra_data(service, "tts_email_account_id", string_account);
-       if (err != SERVICE_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("service_add_extra_data failed : [%d]", err);
+       err = app_control_add_extra_data(service, "tts_email_id", string_mail);
+       if (err != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("app_control_add_extra_data failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       err = service_send_launch_request(service, NULL, NULL);
-       if (err != SERVICE_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("service_send_launch_request failed : [%d]", err);
+       err = app_control_send_launch_request(service, NULL, NULL);
+       if (err != APP_CONTROL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("app_control_send_launch_request failed : [%d]", err);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:
 
        if (service)
-               service_destroy(service);
+               app_control_destroy(service);
+
+       if (p_mail_data)
+               emstorage_free_mail(&p_mail_data, 1, NULL);
 
        EM_DEBUG_FUNC_END();
        return convert_app_err_to_email_err(err);
 }
 #endif /* __FEATURE_DRIVING_MODE__ */
 
-INTERNAL_FUNC int emcore_clear_all_notifications()
+INTERNAL_FUNC int emcore_clear_notifications(char *multi_user_name, int account_id)
 {
        int account_count = 0, i;
        emstorage_account_tbl_t *account_list;
        int error_code = EMAIL_ERROR_NONE;
 
-       if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
-               goto FINISH_OFF;
+       if (account_id == ALL_ACCOUNT) {
+               if(!emstorage_get_account_list(multi_user_name, &account_count, &account_list, true, false, &error_code)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
+                       goto FINISH_OFF;
+               }
+
+               for(i = 0; i < account_count; i++) {
+                       error_code = emcore_delete_notification_by_account(multi_user_name, account_list[i].account_id, true);
+                       if (error_code != EMAIL_ERROR_NONE)
+                               EM_DEBUG_EXCEPTION("emcore_delete_notification_by_account failed");
+               }
+       } else {
+               error_code = emcore_delete_notification_by_account(multi_user_name, account_id, true);
+               if (error_code != EMAIL_ERROR_NONE)
+                       EM_DEBUG_EXCEPTION("emcore_delete_notification_by_account failed");
        }
 
-       for(i = 0; i < account_count; i++) {
-               emcore_delete_notification_by_account(account_list[i].account_id, true);
+       if (!emstorage_update_save_status(multi_user_name, account_id, &error_code)) {
+              EM_DEBUG_EXCEPTION("emstorage_update_save_status failed : [%d]", error_code);
+               goto FINISH_OFF;
        }
 
 FINISH_OFF:
+
        if(account_count) {
                emstorage_free_account(&account_list, account_count, NULL);
        }
@@ -2392,7 +3403,7 @@ FINISH_OFF:
 
 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
 
-INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id, int with_noti_tray)
+INTERNAL_FUNC int emcore_delete_notification_by_account(char *multi_user_name, int account_id, int with_noti_tray)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
        int error_code = EMAIL_ERROR_NONE;
@@ -2403,6 +3414,21 @@ INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id, int with
        if (vconf_get_int(vconf_private_id, &private_id) != 0) {
                EM_DEBUG_EXCEPTION("vconf_get_int failed");
        }
+#ifdef __FEATURE_NOTIFICATION_ENABLE__
+       notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
+
+       if ((noti_err = notification_delete_by_priv_id(NATIVE_EMAIL_APPLICATION_PKG, NOTIFICATION_TYPE_NOTI, private_id)) != NOTIFICATION_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("notification_delete_by_priv_id failed [%d]", noti_err);
+               error_code = EMAIL_ERROR_NOTI;
+       }
+
+       if (with_noti_tray) {
+               if ((noti_err = notification_delete_by_priv_id(NATIVE_EMAIL_APPLICATION_PKG, NOTIFICATION_TYPE_ONGOING, private_id)) != NOTIFICATION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("notification_delete_by_priv_id failed [%d]", noti_err);
+                       error_code = EMAIL_ERROR_NOTI;
+               }
+       }
+#endif /* __FEATURE_NOTIFICATION_ENABLE__ */
        EM_DEBUG_FUNC_END();
        return error_code;
 }
@@ -2834,6 +3860,12 @@ int emcore_make_attachment_file_name_with_extension(char *source_file_name, char
                EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
        else  { /* No extension attached, So add the Extension based on the subtype */
                if (sub_type) {
+            if ((MAX_PATH + 1) < (strlen(sub_type) + 1)) {
+                EM_DEBUG_EXCEPTION("Buffer overflow");
+                err = EMAIL_ERROR_OUT_OF_MEMORY;
+                goto FINISH_OFF;
+            }
+
                        strcat(attachment_file_name, ".");
                        strcat(attachment_file_name, sub_type);
                        EM_DEBUG_LOG_SEC("attachment_file_name with extension[%s] ", attachment_file_name);
@@ -2921,7 +3953,7 @@ INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
        int err = EMAIL_ERROR_NONE;
 
        if (NULL == activity_id)
-       {
+       {
                EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
 
                err = EMAIL_ERROR_INVALID_PARAM;
@@ -2977,6 +4009,7 @@ INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_s
        FILE *fp = NULL;
        char *buf = NULL;
        char *stripped = NULL;
+       char *cid_string = NULL;
        char *modified_string = NULL;
 
        if (!search_string || !file_path || !result) {
@@ -2985,15 +4018,14 @@ INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_s
                return error;
        }
 
-       fp = fopen(file_path, "r");
-       if (fp == NULL) {
-               EM_DEBUG_EXCEPTION("fopen failed");
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
+       error = em_fopen(file_path, "r", &fp);
+       if (error != EMAIL_ERROR_NONE || fp == NULL) {
+               EM_DEBUG_EXCEPTION("em_fopen error [%d] [%s]", error, file_path);
                goto FINISH_OFF;
        }
 
        if (!emcore_get_file_size(file_path, &file_size, &error)) {
-               EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
+               EM_DEBUG_EXCEPTION("emcore_get_file_size error [%s]", file_path);
                goto FINISH_OFF;
        }
 
@@ -3005,7 +4037,7 @@ INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_s
        }
 
        if (fread(buf, sizeof(char), file_size, fp) != file_size) {
-               EM_DEBUG_EXCEPTION("Get the data from file : failed");
+               EM_DEBUG_EXCEPTION("fread error [%d]", errno);
                error = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
@@ -3014,21 +4046,29 @@ INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_s
        stripped = em_replace_all_string(buf, CRLF_STRING, "");
        if (stripped) {
                if (new_string) {
-                       modified_string = em_replace_string(stripped, search_string, new_string);
+                       cid_string = em_malloc(strlen(search_string) + strlen("cid:") + 1);
+                       if (cid_string == NULL) {
+                               EM_DEBUG_EXCEPTION("em_malloc failed");
+                               error = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
+                       }
+
+                       sprintf(cid_string, "cid:%s", search_string);
+
+                       modified_string = em_replace_string(stripped, cid_string, new_string);
                        if (modified_string) {
                                if (fp)
                                        fclose(fp);
-       
-                               fp = fopen(file_path, "w");                     
-                               if (fp == NULL) {
-                                       EM_DEBUG_EXCEPTION("fopen failed");
-                                       error = EMAIL_ERROR_SYSTEM_FAILURE;
+
+                               error = em_fopen(file_path, "w", &fp);
+                               if (error != EMAIL_ERROR_NONE || fp == NULL) {
+                                       EM_DEBUG_EXCEPTION("em_fopen failed");
                                        goto FINISH_OFF;
                                }
 
-                               fprintf(fp, "%s", modified_string);                     
+                               fprintf(fp, "%s", modified_string);
 
-                               p_result = true;        
+                               p_result = true;
                        }
 
                } else {
@@ -3048,6 +4088,7 @@ FINISH_OFF:
 
        EM_SAFE_FREE(buf);
        EM_SAFE_FREE(stripped);  /*prevent 35586*/
+       EM_SAFE_FREE(cid_string);
        EM_SAFE_FREE(modified_string);
 
        EM_DEBUG_FUNC_END("error:[%d]", error);
@@ -3071,10 +4112,9 @@ INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_arr
                return error;
        }
 
-       fp = fopen(file_path, "r");
-       if (fp == NULL) {
-               EM_DEBUG_EXCEPTION("fopen failed");
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
+       error = em_fopen(file_path, "r", &fp);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_fopen failed");
                goto FINISH_OFF;
        }
 
@@ -3083,7 +4123,7 @@ INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_arr
                goto FINISH_OFF;
        }
 
-       buf = em_malloc(file_size);
+       buf = em_malloc(file_size + 1);
        if (buf == NULL) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -3261,7 +4301,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emcore_calc_next_time_to_sync(int input_account_id, time_t input_current_time, time_t *output_time)
+INTERNAL_FUNC int emcore_calc_next_time_to_sync(char *multi_user_name, int input_account_id, time_t input_current_time, time_t *output_time)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_current_time[%d] output_time[%p]", input_account_id, input_current_time, output_time);
        int err = EMAIL_ERROR_NONE;
@@ -3271,18 +4311,18 @@ INTERNAL_FUNC int emcore_calc_next_time_to_sync(int input_account_id, time_t inp
        time_t result_time = 0;
        int    is_time_in_peak_schedule = 0;
 
-       emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account, true, &err);
+       emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account, true, &err);
        if (account == NULL) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]",err);
                goto FINISH_OFF;
        }
 
-       if (account->peak_interval <= 0) {
+       if (account->peak_days < 0) {
                /* peak schedule disabled */
                if (account->check_interval > 0)
                        result_time = input_current_time + (account->check_interval * 60);
        }
-       else if(account->peak_days) {
+       else if(account->peak_days > 0) {
                /* peak schedule enabled */
 
                /* if current time is in peak schedule */
@@ -3336,7 +4376,7 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("err[%d]", err);
        return err;
 }
-#if 0
+
 static int convert_contact_err_to_email_err(int contact_err)
 {
        int err = EMAIL_ERROR_NONE;
@@ -3370,39 +4410,48 @@ static int convert_contact_err_to_email_err(int contact_err)
        }
        return err;
 }
-#endif
-int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
+
+int emcore_get_mail_contact_info(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
-       
+
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        
-       if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
+       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
                ret = true;
-       
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        return ret;
 }
-#if 0
-int emcore_search_contact_info(const char *contact_uri, int address_property_id, char *address, int favorite_property_id, bool is_favorite, int limit, contacts_record_h *contacts_record)
+
+int emcore_search_contact_info(char *multi_user_name, const char *contact_uri, int address_property_id, char *address, int favorite_property_id, bool is_favorite, int limit, contacts_record_h *contacts_record)
 {
        EM_DEBUG_FUNC_BEGIN();
        int contact_err = CONTACTS_ERROR_NONE;
-       
+
        contacts_query_h query = NULL;
        contacts_filter_h filter = NULL;
        contacts_list_h list = NULL;
 
+       vsm_zone_h join_zone = NULL;
+
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
+                       goto FINISH_OFF;
+               }
+       }
+
        if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("contacts_query_create failed");
                goto FINISH_OFF;
        }
-       
+
        if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("contacts_filter_create failed");
                goto FINISH_OFF;
@@ -3449,23 +4498,35 @@ FINISH_OFF:
        if (list != NULL)
                contacts_list_destroy(list, false);
 
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
+
        return contact_err;
 }
 
-int emcore_search_contact_info_by_address(const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
+int emcore_search_contact_info_by_address(char *multi_user_name, const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
 {
        EM_DEBUG_FUNC_BEGIN();
        int contact_err = CONTACTS_ERROR_NONE;
-       
+
        contacts_query_h query = NULL;
        contacts_filter_h filter = NULL;
        contacts_list_h list = NULL;
 
+       vsm_zone_h join_zone = NULL;
+
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
+                       goto FINISH_OFF;
+               }
+       }
+
        if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("contacts_query_create failed");
                goto FINISH_OFF;
        }
-       
+
        if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("contacts_filter_create failed");
                goto FINISH_OFF;
@@ -3487,7 +4548,7 @@ int emcore_search_contact_info_by_address(const char *contact_uri, int property_
        }
 
        if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
-               if (contact_err != CONTACTS_ERROR_NO_DATA) /* no matching record found */               
+               if (contact_err != CONTACTS_ERROR_NO_DATA) /* no matching record found */
                        EM_DEBUG_EXCEPTION ("contacts_list_get_current_record_p failed [%d]", contact_err);
                goto FINISH_OFF;
        }
@@ -3503,25 +4564,49 @@ FINISH_OFF:
        if (list != NULL)
                contacts_list_destroy(list, false);
 
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
+
        return contact_err;
 }
-#endif
-int emcore_set_contacts_log(int account_id, char *email_address, char *subject, time_t date_time, email_action_t action)
+
+typedef struct {
+       int   account_id;
+       char *email_address;
+       char *subject;
+    char *multi_user_name;
+       time_t date_time;
+       email_action_t action;
+} set_contacts_log_internal_t;
+
+gboolean emcore_set_contacts_log_internal (void* arg)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("account_id : [%d], address : [%p], subject : [%s], action : [%d], date_time : [%d]", account_id, email_address, subject, action, (int)date_time);
-       
-       int err = EMAIL_ERROR_NONE;
-#if 0
+       if (!arg) {
+               EM_DEBUG_EXCEPTION ("no contact data to add");
+               return FALSE;
+       }
+       set_contacts_log_internal_t* tmp = (set_contacts_log_internal_t*) arg;
+       int   account_id    = tmp->account_id;
+       char *email_address = tmp->email_address;
+       char *subject       = tmp->subject;
+       time_t date_time    = tmp->date_time;
+       email_action_t action = tmp->action;
+       char *multi_user_name = tmp->multi_user_name;
+
        int contacts_error = CONTACTS_ERROR_NONE;
        int person_id = 0;
        int action_type = 0;
-       
+
        contacts_record_h phone_record = NULL;
-       contacts_record_h person_record = NULL; 
+       contacts_record_h person_record = NULL;
 
-       if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
-               goto FINISH_OFF;
+       vsm_zone_h join_zone = NULL;
+
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
+                       goto FINISH_OFF;
+               }
        }
 
        if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
@@ -3536,7 +4621,7 @@ int emcore_set_contacts_log(int account_id, char *email_address, char *subject,
        }
 
        /* Search contact person info */
-       if ((contacts_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
+       if ((contacts_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
                if (contacts_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
                        EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
        } else {
@@ -3603,21 +4688,48 @@ FINISH_OFF:
        if (person_record != NULL)
                contacts_record_destroy(person_record, false);
 
-       err = convert_contact_err_to_email_err(contacts_error);
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
 
-       if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
-               err = convert_contact_err_to_email_err(contacts_error);
+       EM_SAFE_FREE (email_address);
+       EM_SAFE_FREE (subject);
+       EM_SAFE_FREE (tmp);
+
+       EM_DEBUG_FUNC_END("contact err [%d]", convert_contact_err_to_email_err(contacts_error));
+       return FALSE;
+}
+
+int emcore_set_contacts_log (char *multi_user_name,
+                                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 */
+       set_contacts_log_internal_t *arg = em_malloc (sizeof(set_contacts_log_internal_t));
+       if (!arg) {
+               EM_DEBUG_EXCEPTION ("em_malloc error");
+               return EMAIL_ERROR_OUT_OF_MEMORY;
        }
-#endif
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
+
+       /* deep copy */
+       arg->account_id      = account_id;
+       arg->email_address   = EM_SAFE_STRDUP (email_address);
+       arg->subject         = EM_SAFE_STRDUP (subject);
+       arg->date_time       = date_time;
+       arg->action          = action;
+    arg->multi_user_name = EM_SAFE_STRDUP (multi_user_name);
+
+       g_main_context_invoke (NULL, emcore_set_contacts_log_internal, arg);
+
+       return EMAIL_ERROR_NONE;
 }
 
-INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data)
+INTERNAL_FUNC int emcore_set_sent_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
-       
+
        int i = 0;
        int err = EMAIL_ERROR_NONE;
        char email_address[MAX_EMAIL_ADDRESS_LENGTH];
@@ -3630,13 +4742,18 @@ INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_
                        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(input_mail_data->account_id, email_address, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
+                               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;
                                }
                        }
-               
-                       if (addr) {     
+
+                       if (addr) {
                                mail_free_address(&addr);
                                addr = NULL;
                        }
@@ -3645,19 +4762,25 @@ INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_
 
 FINISH_OFF:
 
-       if (addr) 
+       if (addr)
                mail_free_address(&addr);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data)
+
+INTERNAL_FUNC int emcore_set_received_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
        int err = EMAIL_ERROR_NONE;
 
-       if ((err = emcore_set_contacts_log(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) {
+       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);
        }
 
@@ -3665,53 +4788,144 @@ INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_m
        return err;
 }
 
-INTERNAL_FUNC int emcore_delete_contacts_log(int account_id)
+typedef struct _contacts_delete_data
 {
-       EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
+    int account_id;
+    char *multi_user_name;
+} contacts_delete_data;
 
-       int err = EMAIL_ERROR_NONE;
-#if 0
+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;
 
-       if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
-               goto FINISH_OFF;
+       vsm_zone_h join_zone = NULL;
+
+    if (EM_SAFE_STRLEN(data->multi_user_name) > 0) {
+               if (emcore_set_join_zone(data->multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
+                       return FALSE;
+               }
        }
-       
+
        /* Delete record of the account id */
-       if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, account_id)) != CONTACTS_ERROR_NONE) {
+       if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, data->account_id)) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
        }
-       
+/*     err = convert_contact_err_to_email_err(contacts_error); */
 
-FINISH_OFF:
-       err = convert_contact_err_to_email_err(contacts_error);
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
+
+    EM_SAFE_FREE(data->multi_user_name);
+    EM_SAFE_FREE(data);
+
+       EM_DEBUG_FUNC_END();
+       return FALSE;
+}
+
+INTERNAL_FUNC int emcore_delete_contacts_log(char *multi_user_name, int account_id)
+{
+    contacts_delete_data *data = NULL;
+
+    data = (contacts_delete_data *)em_malloc(sizeof(contacts_delete_data));
+    if (data == NULL) {
+        EM_DEBUG_EXCEPTION("em_malloc failed");
+        return EMAIL_ERROR_OUT_OF_MEMORY;
+    }
+
+    data->account_id = account_id;
+    data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
+       g_main_context_invoke (NULL, emcore_delete_contacts_log_internal, (void*) data);
+
+       return EMAIL_ERROR_NONE;                
+}
+
+INTERNAL_FUNC int emcore_get_mail_display_name (char *multi_user_name, char *email_address, char **contact_display_name)
+{
+       if (!email_address || !contact_display_name) {
+               EM_DEBUG_EXCEPTION ("NULL_PARAM email_address[%p] contact_display_name[%p]",
+                                                email_address, contact_display_name);
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
 
-       if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
-               err = convert_contact_err_to_email_err(contacts_error);
+       GError   *gerror = NULL;
+       GVariant *result = NULL;
+       int ret = EMAIL_ERROR_NONE;
+
+       GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                   G_DBUS_PROXY_FLAGS_NONE,
+                                   NULL,
+                                   EMAIL_SERVICE_NAME,
+                                   EMAIL_SERVICE_PATH,
+                                   EMAIL_SERVICE_NAME,
+                                   NULL,
+                                   &gerror);
+       if (!bproxy) {
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_new_for_bus_sync error [%s]",
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               goto FINISH_OFF;
        }
-#endif
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
+
+       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]", 
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       g_variant_get (result, "(si)", contact_display_name, &ret);
+
+       /* replace "" to NULL */
+       if (!g_strcmp0 (*contact_display_name, ""))
+               EM_SAFE_FREE (*contact_display_name);
+
+       g_variant_unref (result);
+
+FINISH_OFF:
+       EM_DEBUG_LOG ("ret [%d]\n", ret);
+       if (bproxy)
+               g_object_unref (bproxy);
+       if (gerror)
+               g_error_free (gerror);
+
+       return ret;
 }
 
-INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code)
+
+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);
-       int ret = false;
-#if 0
+
        int contact_err = 0;
+       int err = EMAIL_ERROR_NONE;
+       int ret = false;
        char *display = NULL;
        /* Contact variable */
        contacts_record_h record = NULL;
 
-       if ((contact_err = contacts_connect2()) != CONTACTS_ERROR_NONE) {
+/*
+       if ((contact_err = contacts_connect_on_thread()) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_err);
                goto FINISH_OFF;
        }
-
-       if ((contact_err = emcore_search_contact_info_by_address(_contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
+*/
+       if ((contact_err = emcore_search_contact_info_by_address(multi_user_name, _contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
                if (contact_err != CONTACTS_ERROR_NO_DATA) /* no matching record found */
                        EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_err);
                goto FINISH_OFF;
@@ -3742,7 +4956,7 @@ INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **conta
 
 FINISH_OFF:
 
-       contacts_disconnect2();
+/*     contacts_disconnect_on_thread(); */
 
        if (record != NULL)
                contacts_record_destroy(record, false);
@@ -3754,12 +4968,66 @@ FINISH_OFF:
                EM_SAFE_FREE(display);
        }
 
-       if (err_code != NULL)
-               *err_code = convert_contact_err_to_email_err(contact_err);
-#endif
-       return ret;
+       err = convert_contact_err_to_email_err(contact_err);
+
+       return err;
+}
+
+INTERNAL_FUNC int emcore_connect_contacts_service(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int contact_err = 0;
+       int err = EMAIL_ERROR_NONE;
+
+       vsm_zone_h join_zone = NULL;
+
+    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;                     
+               }
+    }
+
+       if ((contact_err = contacts_connect()) != CONTACTS_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("contacts_connect failed : [%d]", contact_err);
+       }
+
+       err = convert_contact_err_to_email_err(contact_err);
+
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
+
+       EM_DEBUG_FUNC_END();
+       return err;
 }
 
+INTERNAL_FUNC int emcore_disconnect_contacts_service(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int contact_err = 0;
+       int err = EMAIL_ERROR_NONE;
+
+       vsm_zone_h join_zone = NULL;
+
+       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;                     
+               }
+    }
+
+       if ((contact_err = contacts_disconnect()) != CONTACTS_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("contacts_disconnect failed : [%d]", contact_err);
+       }
+
+       err = convert_contact_err_to_email_err(contact_err);
+
+       if (join_zone)
+               emcore_unset_join_zone(join_zone);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
 
 #ifdef __FEATURE_BLOCKING_MODE__
 
@@ -3789,11 +5057,67 @@ INTERNAL_FUNC void emcore_set_blocking_mode_status(int blocking_mode)
        blocking_mode_status = blocking_mode;
 }
 
-INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking_status)
+INTERNAL_FUNC int emcore_check_blocking_mode (char *multi_user_name, char *sender_address, int *blocking_status)
+{
+       if ( !sender_address || !blocking_status ) {
+               EM_DEBUG_EXCEPTION ("NULL_PARAM sender_address[%p] blocking_status[%p]",
+                                                   sender_address, blocking_status);
+               return EMAIL_ERROR_INVALID_PARAM;
+       }
+
+
+       GError   *gerror = NULL;
+       GVariant *result = NULL;
+       int ret = EMAIL_ERROR_NONE;
+
+       GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                   G_DBUS_PROXY_FLAGS_NONE,
+                                   NULL,
+                                   EMAIL_SERVICE_NAME,
+                                   EMAIL_SERVICE_PATH,
+                                   EMAIL_SERVICE_NAME,
+                                   NULL,
+                                   &gerror);
+       if (!bproxy) {
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_new_for_bus_sync error [%s]",
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               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, 
+                                      &gerror);
+
+
+       if (!result) {
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'CheckBlockingMode' error [%s]", 
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       g_variant_get (result, "(ii)", blocking_status, &ret);
+       g_variant_unref (result);
+
+FINISH_OFF:
+
+       if (bproxy)
+               g_object_unref (bproxy);
+       if (gerror)
+               g_error_free (gerror);
+
+       return ret;
+}
+
+INTERNAL_FUNC int emcore_check_blocking_mode_internal (char *multi_user_name, char *sender_address, int *blocking_status)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
-#if 0
        int contact_error = 0;
        int person_id = 0;
        int allowed_contact_type = 0; /* 0 : NONE, 1 : All contacts, 2 : Favorites, 3 : Custom */
@@ -3803,8 +5127,7 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
        contacts_record_h record = NULL;
 
        if (!blocking_mode_of_setting) {
-               EM_DEBUG_LOG("Blocking mode is OFF");
-               return err;             
+               return err;
        }
 
        if (vconf_get_int(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE, &allowed_contact_type) != 0) {
@@ -3813,11 +5136,12 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
                goto FINISH_OFF;
        }
 
-       if ((contact_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
+/*
+       if ((contact_error = contacts_connect_on_thread()) != CONTACTS_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_error);
                goto FINISH_OFF;
        }
-
+*/
        switch (allowed_contact_type) {
        case ALLOWED_CONTACT_TYPE_NONE :
                EM_DEBUG_LOG("allowed_contact_type is none : bloacking all(notification)");
@@ -3826,7 +5150,7 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
        case ALLOWED_CONTACT_TYPE_ALL :
                EM_DEBUG_LOG("allowed_contact_type is ALL");
                /* Search the allowed contact type in contact DB */
-               if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
+               if ((contact_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
                        if (contact_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
                                EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_error);
                        goto FINISH_OFF;
@@ -3834,21 +5158,21 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
 
                if (record == NULL) {
                        EM_DEBUG_LOG("No search contact info");
-                       goto FINISH_OFF; 
+                       goto FINISH_OFF;
                }
 
                *blocking_status = false;
                break;
 
        case ALLOWED_CONTACT_TYPE_FAVORITES :
-               if ((contact_error = emcore_search_contact_info(_contacts_person_email._uri, _contacts_person_email.email, sender_address, _contacts_person_email.is_favorite, true, 1, &record)) != CONTACTS_ERROR_NONE) {
+               if ((contact_error = emcore_search_contact_info(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, _contacts_person_email.is_favorite, true, 1, &record)) != CONTACTS_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_search_contact_info failed : [%d]", contact_error);
                        goto FINISH_OFF;
                }
 
                if (record == NULL) {
                        EM_DEBUG_LOG("No search contact info");
-                       goto FINISH_OFF; 
+                       goto FINISH_OFF;
                }
 
                *blocking_status = false;
@@ -3859,9 +5183,9 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
                if (person_id_string == NULL) {
                        EM_DEBUG_LOG("Custom allowed contact type is NULL");
                        goto FINISH_OFF;
-               }       
+               }
 
-               if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
+               if ((contact_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
                        if (contact_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
                                EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_error);
                        goto FINISH_OFF;
@@ -3869,11 +5193,11 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
 
                if (record == NULL) {
                        EM_DEBUG_LOG("No search contact info");
-                       goto FINISH_OFF; 
-               }       
+                       goto FINISH_OFF;
+               }
 
                if (contacts_record_get_int(record, _contacts_contact_email.person_id, &person_id) != CONTACTS_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
+                       EM_DEBUG_EXCEPTION("contacts_record_get_int failed");
                        goto FINISH_OFF;
                }
 
@@ -3881,7 +5205,7 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
                do {
                        if (person_id == atoi(token)) {
                                *blocking_status = false;
-                               break;                  
+                               break;
                        }
                } while ((token = strtok_r(NULL, ",", &str)));
 
@@ -3892,13 +5216,15 @@ INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking
                *blocking_status = true;
                err = EMAIL_ERROR_INVALID_PARAM;
        }
-       
-FINISH_OFF :
 
-       contacts_disconnect2();
+FINISH_OFF :
 
+/*
+       contacts_disconnect_on_thread();
+*/
        err = convert_contact_err_to_email_err(contact_error);
-#endif
+
+       EM_SAFE_FREE(person_id_string);
        EM_DEBUG_FUNC_END();
        return err;
 }
@@ -3913,7 +5239,7 @@ INTERNAL_FUNC char *emcore_set_mime_entity(char *mime_path)
        char *mime_entity = NULL;
        char *mime_entity_path = NULL;
        char temp_buffer[255] = {0,};
-       int err;
+       int err = EMAIL_ERROR_NONE;
        int searched = 0;
 
        if (!emcore_get_temp_file_name(&mime_entity_path, &err))  {
@@ -3923,20 +5249,20 @@ INTERNAL_FUNC char *emcore_set_mime_entity(char *mime_path)
 
        /* get mime entity */
        if (mime_path != NULL) {
-               fp_read = fopen(mime_path, "r");
-               if (fp_read == NULL) {
-                       EM_DEBUG_EXCEPTION_SEC("File open(read) is failed : filename [%s]", mime_path);
+               err = em_fopen(mime_path, "r", &fp_read);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION_SEC("File em_fopen(read) is failed : filename [%s]", mime_path);
                        goto FINISH_OFF;
                }
 
-               fp_write = fopen(mime_entity_path, "w");
-               if (fp_write == NULL) {
-                       EM_DEBUG_EXCEPTION_SEC("File open(write) is failed : filename [%s]", mime_entity_path);
+               err = em_fopen(mime_entity_path, "w", &fp_write);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION_SEC("File em_fopen(write) is failed : filename [%s]", mime_entity_path);
                        goto FINISH_OFF;
                }
 
                fseek(fp_read, 0, SEEK_SET);
-               fseek(fp_write, 0, SEEK_SET);           
+               fseek(fp_write, 0, SEEK_SET);
 
                while (fgets(temp_buffer, 255, fp_read) != NULL) {
                        mime_entity = strcasestr(temp_buffer, "content-type");
@@ -3947,7 +5273,7 @@ INTERNAL_FUNC char *emcore_set_mime_entity(char *mime_path)
                                fprintf(fp_write, "%s", temp_buffer);
                        }
                }
-       }       
+       }
 
 FINISH_OFF:
        if (fp_read)
@@ -3962,4 +5288,387 @@ FINISH_OFF:
        return mime_entity_path;
 }
 
+/* Feedback LED api */
+INTERNAL_FUNC void emcore_set_flash_noti()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int flash_error = FEEDBACK_ERROR_NONE;
+
+       flash_error = feedback_initialize();
+       if (flash_error != FEEDBACK_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("feedback_initialize failed : [%d]", flash_error);
+               return;
+       }
+
+#ifdef __FEATURE_FEEDBACK_TYPE_LED_ENABLE__
+       flash_error = feedback_play_type(FEEDBACK_TYPE_LED, FEEDBACK_PATTERN_EMAIL);
+       if (flash_error != FEEDBACK_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("feedback_play_type failed : [%d]", flash_error);
+       }
+#endif /* __FEATURE_FEEDBACK_TYPE_LED_ENABLE__ */
+
+       flash_error = feedback_deinitialize();
+       if (flash_error != FEEDBACK_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("feedback_play_type failed : [%d]", flash_error);
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emcore_get_content_from_file(char *filename, char **contents, int *length)
+{
+       EM_DEBUG_FUNC_BEGIN("filename[%s], contents[%p], length[%p]", filename, contents, length);
+
+       struct stat stat_buf;
+       int fd = 0;
+       int bytes_read = 0;
+       int size = 0;
+       int alloc_size = 0;
+       int err = EMAIL_ERROR_NONE;
+       char *buf = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+
+       if (filename == NULL || contents == NULL || length == NULL) {
+               err = EMAIL_ERROR_INVALID_PARAM;
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               goto FINISH_OFF;
+       }
+
+       err = em_open(filename, O_RDONLY, 0 ,&fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open error [%s][%d]", filename, err);
+               goto FINISH_OFF;
+       }
+
+       if (fstat (fd, &stat_buf) < 0) {
+               EM_DEBUG_EXCEPTION("fstat failed for fd [%d]", fd);
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       if (stat_buf.st_size > 0 && S_ISREG(stat_buf.st_mode)) {
+               size = stat_buf.st_size;
+               alloc_size = size + 1;
+               buf = em_malloc(alloc_size);
+               if (buf == NULL) {
+                       EM_DEBUG_EXCEPTION("malloc failed...");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+
+               bytes_read = 0;
+               errno = 0;
+
+               while (bytes_read < size) {
+                       ssize_t rd_size = 0;
+                       rd_size = read (fd, buf + bytes_read, size - bytes_read);
+
+                       if (rd_size < 0) {
+                               if (errno != EINTR) {
+                                       EM_DEBUG_EXCEPTION("read failed: %s", EM_STRERROR(errno_buf));
+                                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                                       goto FINISH_OFF;
+                               }
+                       }
+                       else if (rd_size == 0)
+                               break;
+                       else
+                               bytes_read += rd_size;
+               }
+
+               buf[bytes_read] = '\0';
+
+               if (length)
+                       *length = bytes_read;
+
+               *contents = buf;
+       }
+       EM_SAFE_CLOSE (fd); /* prevent 39093 */
+
+       EM_DEBUG_FUNC_END();
+       return err;
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(buf);
+       EM_SAFE_CLOSE (fd); /* prevent 39093 */
+       EM_DEBUG_FUNC_END ();
+
+       return err;
+}
+
+INTERNAL_FUNC int emcore_set_content_to_file(const char *contents, char *dest_path, int length)
+{
+       EM_DEBUG_FUNC_BEGIN("contents[%p], dest_path[%s], length[%d]", contents, dest_path, length);
+       int fd = 0;
+       int err = EMAIL_ERROR_NONE;
+       char *tmp_path = NULL;
+       char errno_buf[ERRNO_BUF_SIZE] = {0};
+       int byte_written = 0; /* 39063 */
+
+       if (contents == NULL || dest_path == NULL || length <= 0) {
+               err = EMAIL_ERROR_INVALID_PARAM;
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               goto FINISH_OFF;
+       }
+
+       tmp_path = emcore_mime_get_save_file_name(&err);
+       if (!tmp_path) { /* prevent 39114 */
+               EM_DEBUG_EXCEPTION ("tmp_path NULL");
+               goto FINISH_OFF;
+       }
+
+       err = em_open(tmp_path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fd);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("em_open error [%s][%d]", tmp_path, err);
+               goto FINISH_OFF;
+       }
+
+       errno = 0;
+       while(length > 0 && contents && errno == 0) {
+               byte_written = write(fd, (void *)contents, length);
+               if (byte_written < 0) {
+                       /* interrupted by a signal */
+                       if (errno == EINTR) {
+                               errno = 0;
+                               continue;
+                       }
+                       EM_DEBUG_EXCEPTION("write failed: %s", EM_STRERROR(errno_buf));
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
+               length -= byte_written;
+               contents += byte_written;
+       }
+
+       errno = 0;
+       if (fsync(fd) != 0){
+               EM_DEBUG_EXCEPTION("fsync failed: %s", EM_STRERROR(errno_buf));
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+       }
+
+       errno = 0;
+       if (tmp_path && dest_path && rename(tmp_path, dest_path) != 0) {
+               EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
+               err = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(tmp_path);
+       EM_SAFE_CLOSE (fd); /* prevent 39114, 39136*/
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
+#ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
+
+INTERNAL_FUNC int emcore_update_db_table_schema(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+
+       err = emstorage_update_db_table_schema(multi_user_name);
+
+       EM_DEBUG_FUNC_END("err[%d]", err);
+       return err;
+}
+
+#endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
+
+INTERNAL_FUNC int emcore_unescape_from_url(char *input_url, char **output_url)
+{
+       EM_DEBUG_FUNC_BEGIN("input_url[%p] output_url[%p]", input_url, output_url);
+       int err = EMAIL_ERROR_NONE;
+       int input_length = 0;
+       int output_length = 0;
+       char *result_string = NULL;
+       CURL *curl = NULL;
+
+       if (input_url == NULL|| output_url == NULL) {
+               err = EMAIL_ERROR_INVALID_PARAM;
+               EM_DEBUG_EXCEPTION("Invalid parameter");
+               goto FINISH_OFF;
+       }
+
+       *output_url = NULL;
+       curl = curl_easy_init();
+
+       input_length = EM_SAFE_STRLEN(input_url);
+
+       result_string = curl_easy_unescape(curl, input_url, input_length, &output_length);
+
+       if (output_length)
+               *output_url = EM_SAFE_STRDUP(result_string);
+
+FINISH_OFF:
+       if (result_string)
+               curl_free(result_string);
+
+       if(curl)
+               curl_easy_cleanup(curl);
+
+       EM_DEBUG_FUNC_END("err[%d]", err);
+       return err;
+}
+
+INTERNAL_FUNC char *__em_get_month_in_string(int month)
+{
+       EM_DEBUG_FUNC_BEGIN("month [%d]", month);
+
+       char *mon = NULL;
+
+       switch (month){
+           case 0:
+                       mon = strdup("jan");
+               break;
+           case 1:
+                       mon = strdup("feb");
+               break;
+           case 2:
+                       mon = strdup("mar");
+               break;
+           case 3:
+                       mon = strdup("apr");
+               break;
+           case 4:
+                       mon = strdup("may");
+               break;
+           case 5:
+                       mon = strdup("jun");
+               break;
+           case 6:
+                       mon = strdup("jul");
+               break;
+           case 7:
+                       mon = strdup("aug");
+               break;
+           case 8:
+                       mon = strdup("sep");
+               break;
+           case 9:
+                       mon = strdup("oct");
+               break;
+           case 10:
+                       mon = strdup("nov");
+               break;
+           case 11:
+                       mon = strdup("dec");
+               break;
+       }
+       return mon;
+}
+
+INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *output_date_string)
+{
+       EM_DEBUG_FUNC_BEGIN("input_time[%p] output_date_string[%p]", input_time, output_date_string);
+       int err = EMAIL_ERROR_NONE;
+       struct tm   *timeinfo = NULL;
+       char *mon = NULL;
+
+       EM_DEBUG_LOG("RawTime Info [%lu]", input_time);
+
+       timeinfo = localtime (&input_time);
+
+       EM_DEBUG_LOG(">>>>>Time %d %d %d %d %d %d", 1900+timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
+
+       memset(output_date_string, 0x00, 20);
+
+       mon = __em_get_month_in_string(timeinfo->tm_mon);
+
+       if (mon){
+               snprintf(output_date_string, 16, "%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
+               EM_DEBUG_LOG("DATE IS [ %s ] ", output_date_string);
+               EM_SAFE_FREE(mon);
+       }
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+INTERNAL_FUNC int emcore_make_uid_range_string(emcore_uid_list *uid_list, int total, char **output_uid_range_string)
+{
+       EM_DEBUG_FUNC_BEGIN("uid_list[%p] total[%d] output_uid_range_string[%p]", uid_list, total, output_uid_range_string);
+       int err = EMAIL_ERROR_NONE;
+       emcore_uid_list *uid_list_prev = NULL;
+       emcore_uid_list *uid_list_fast = uid_list;
+       int index = 0;
+       int msg_count = total;
+       int uid_range_size = msg_count * 8 + 1000;
+       char *uid_range = NULL;
+
+       if (uid_list == NULL || total == 0 || output_uid_range_string == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               err  = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
+       uid_range = malloc(sizeof(char) * uid_range_size);
+
+       if (uid_range == NULL){
+               EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
+               err  = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       uid_list_prev = uid_list_fast;
+
+       if (uid_list_fast->next == NULL){
+               /* Single list entry */
+               snprintf(uid_range, uid_range_size, "%d", atoi(uid_list_fast->uid));
+               EM_DEBUG_LOG("uid_range [%d]", uid_range);
+       }
+       else{
+               /* forming range of uids to be passed */
+               while (uid_list_fast  != NULL){
+                       /* uid_list_fast = uid_list_fast->next; */
+
+                       if ((uid_list_fast->next != NULL) && (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1)){
+                               index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
+
+                               uid_list_fast = uid_list_fast->next;
+                               uid_list_prev = uid_list_fast;
+
+                               /* to make UID range string "abc, XX : YY" */
+                               while (uid_list_fast  != NULL){
+                                       if (uid_list_fast->next == NULL)
+                                               break;
+                                       if (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1){
+                                               uid_list_fast = uid_list_fast->next;
+                                               uid_list_prev = uid_list_fast;
+                                       }
+                                       else
+                                               break;
+                               }
+                               if ((uid_list_fast  != NULL) && (uid_list_fast->next  != NULL))
+                                       index  += snprintf(uid_range+index, uid_range_size, ":%d,", atoi(uid_list_prev->uid));
+                               else
+                                       index  += snprintf(uid_range+index, uid_range_size, ":%d", atoi(uid_list_prev->uid));
+
+                               uid_list_fast = uid_list_fast->next;
+                               uid_list_prev = uid_list_fast;
+                       }
+                       else{
+                               if (uid_list_fast->next  != NULL)
+                                       index  += snprintf(uid_range+index, uid_range_size, "%d,", atoi(uid_list_prev->uid));
+                               else
+                                       index  += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
+                               uid_list_fast = uid_list_fast->next;
+                               uid_list_prev = uid_list_fast;
+                       }
+               }
+       }
+       EM_DEBUG_LOG("index [%d]", index);
+
+       *output_uid_range_string = uid_range;
+
+FINISH_OFF:
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
 /* EOF */
index ec1d671..85d6a79 100755 (executable)
 #include <stdlib.h>
 #include <time.h>
 #include <sys/types.h>
-#include "pmapi.h"
+#include "device/power.h"
 #include "email-device.h"
 #include "email-debug-log.h"
 
+int stay_awake_flag[STAY_AWAKE_FLAG_MAX] = { 0, };
+static pthread_mutex_t stay_awake_flag_lock = PTHREAD_MUTEX_INITIALIZER;
 
-
-INTERNAL_FUNC int emdevice_set_sleep_on_off(int on, int *error_code)
+INTERNAL_FUNC int emdevice_set_sleep_on_off(email_stay_awake_flag_owner_t input_flag_owner, int input_allow_to_sleep, int *error_code)
 {
-       EM_DEBUG_FUNC_BEGIN("on[%d], err_code[%p]", on, error_code);
+       EM_DEBUG_FUNC_BEGIN("input_flag_owner[%d] input_allow_to_sleep[%d] err_code[%p]", input_flag_owner, input_allow_to_sleep, error_code);
        int result_from_pm_api = 0;
 
-       if(on == 1) {
-               result_from_pm_api = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
-               EM_DEBUG_LOG("pm_unlock_state() returns [%d]", result_from_pm_api);
+       ENTER_CRITICAL_SECTION(stay_awake_flag_lock);
+
+       stay_awake_flag[input_flag_owner] = !input_allow_to_sleep;
+
+       if(input_allow_to_sleep == 1) {
+               int i = 0;
+               int allowed_to_sleep = 1;
+
+               for (i = 0; i < STAY_AWAKE_FLAG_MAX; i++) {
+                       if (stay_awake_flag[i] == 1) {
+                               allowed_to_sleep = 0;
+                               break;
+                       }
+               }
+
+               if (allowed_to_sleep) {
+                       /* allowed to sleep */
+                       result_from_pm_api = device_power_release_lock(POWER_LOCK_CPU);
+                       EM_DEBUG_LOG("display_unlock_state() returns [%d]", result_from_pm_api);
+               }
+               else {
+                       EM_DEBUG_LOG("other worker[%d] is working on", i);
+               }
        }
        else {
-               result_from_pm_api = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
-               EM_DEBUG_LOG("pm_lock_state() returns [%d]", result_from_pm_api);
+               /* Stay awake */
+               result_from_pm_api = device_power_request_lock(POWER_LOCK_CPU, 0);
+               EM_DEBUG_LOG("display_lock_state() returns [%d]", result_from_pm_api);
        }
 
-       EM_DEBUG_FUNC_END();
-       return true;
-}
+       LEAVE_CRITICAL_SECTION(stay_awake_flag_lock);
 
-INTERNAL_FUNC int emdevice_set_dimming_on_off(int on, int *error_code)
-{
-       EM_DEBUG_FUNC_BEGIN("on[%d], err_code[%p]", on, error_code);
        EM_DEBUG_FUNC_END();
        return true;
 }
+
+
index 7bdd818..59a88e1 100755 (executable)
@@ -43,8 +43,15 @@ extern "C"
 #define DISPLAY_STATE_DIM 1
 #define DISPLAY_STATE_ON  2
 
-INTERNAL_FUNC int emdevice_set_sleep_on_off(int on, int *error_code);
-INTERNAL_FUNC int emdevice_set_dimming_on_off(int on, int *error_code);
+typedef enum {
+       STAY_AWAKE_FLAG_FOR_RECEVING_WORKER = 0,
+       STAY_AWAKE_FLAG_FOR_SENDING_WORKER,
+       STAY_AWAKE_FLAG_FOR_PARTIAL_BODY_WORKER,
+       STAY_AWAKE_FLAG_FOR_ALARM_CALLBACK,
+       STAY_AWAKE_FLAG_MAX,
+} email_stay_awake_flag_owner_t;
+
+INTERNAL_FUNC int emdevice_set_sleep_on_off(email_stay_awake_flag_owner_t input_flag_owner, int input_allow_to_sleep, int *error_code);
 
 #ifdef __cplusplus
 }
index c75100b..04d44e8 100755 (executable)
 #include "email-core-event.h"
 #endif
 
+int network_status = 0;
+
 /* _get_network_status - Get the data network status from vconf */
-static int _get_network_status(int *network_status)
+
+INTERNAL_FUNC void emnetwork_set_network_status(int input_network_status)
 {
-       EM_DEBUG_FUNC_BEGIN("network_status [%p]", network_status);
+       EM_DEBUG_FUNC_BEGIN();
+       network_status = input_network_status;
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC int emnetwork_get_network_status()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       EM_DEBUG_FUNC_END();
+       return network_status;
+}
 
-       int value = 0;
+/* Check code for SIM status */
+static int  _get_sim_status(int *sim_status)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int value;
 
-       if(!network_status) {
+       if(!sim_status) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &value)) {
-               EM_DEBUG_EXCEPTION("Failed vconf_get_int [VCONFKEY_NETWORK_STATUS]");
+       if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &value)  != 0) {
+               EM_DEBUG_EXCEPTION("Failed vconf_get_int [VCONFKEY_TELEPHONY_SIM_SLOT]");
                return EMAIL_ERROR_SYSTEM_FAILURE;
        }
 
-       *network_status = value;
+       *sim_status = value;
 
-       EM_DEBUG_FUNC_END("network_status [%d]", value);
+       EM_DEBUG_FUNC_END("status[%d]", value);
        return EMAIL_ERROR_NONE;
 }
 
-/* Check code for SIM status */
-static int  _get_sim_status(int *sim_status)
+/* Check code for flight mode */
+static int _get_flight_mode(int *flight_mode)
 {
        EM_DEBUG_FUNC_BEGIN();
        int value;
 
-       if(!sim_status) {
+       if(!flight_mode) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &value)  != 0) {
-               EM_DEBUG_EXCEPTION("Failed vconf_get_int [VCONFKEY_TELEPHONY_SIM_SLOT]");
+       if (vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &value) != 0) {
+               EM_DEBUG_EXCEPTION("Failed vconf_get_bool [VCONFKEY_TELEPHONY_FLIGHT_MODE]");
                return EMAIL_ERROR_SYSTEM_FAILURE;
        }
 
-       *sim_status = value;
+       EM_DEBUG_LOG("flight_mode : [%d]", value);
+
+       *flight_mode = value;
 
        EM_DEBUG_FUNC_END("status[%d]", value);
        return EMAIL_ERROR_NONE;
+
 }
 
 INTERNAL_FUNC int emnetwork_get_wifi_status(int *wifi_status)
@@ -128,20 +148,26 @@ INTERNAL_FUNC int emnetwork_get_wifi_status(int *wifi_status)
 INTERNAL_FUNC int emnetwork_check_network_status(int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int network_status = 0;
        int sim_status     = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
        int wifi_status    = 0;
+       int flight_mode    = 0;
        int err            = EMAIL_ERROR_NONE;
        int ret            = false;
 
-       if ( (err = _get_network_status(&network_status)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("_get_network_status failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
        if(network_status == 0) {
                EM_DEBUG_LOG("VCONFKEY_NETWORK_STATUS is 0");
 
+               if ( (err = _get_flight_mode(&flight_mode)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("_get_flight_mode failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (flight_mode) {
+                       EM_DEBUG_LOG("Flight mode enable");
+                       err = EMAIL_ERROR_FLIGHT_MODE_ENABLE;
+                       goto FINISH_OFF;                        
+               }
+
                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 a5db10f..b932f90 100755 (executable)
@@ -42,6 +42,8 @@ INTERNAL_FUNC int emnetwork_check_network_status(int *err_code);
 INTERNAL_FUNC int emnetwork_get_roaming_status(int *output_roaming_status);
 INTERNAL_FUNC int emnetwork_get_wifi_status(int *wifi_status);
 INTERNAL_FUNC long emnetwork_callback_ssl_cert_query(char *reason, char *host, char *cert);
+INTERNAL_FUNC void emnetwork_set_network_status(int input_network_status);
+INTERNAL_FUNC int emnetwork_get_network_status();
 
 
 #ifdef __cplusplus
index 32ea681..16bbd37 100755 (executable)
@@ -47,7 +47,6 @@
 #include <sys/mman.h>
 #include <ss_manager.h>
 #include <fcntl.h>
-#include <db-util.h>
 #include <tzplatform_config.h>
 
 #define __USE_UNIX98
@@ -64,6 +63,7 @@
 #include "email-convert.h"
 #include "email-core-signal.h"
 #include "email-core-event.h"
+#include "email-core-container.h"
 
 #define DB_STMT sqlite3_stmt *
 
 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
+#define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
 #define MAILBOX_LEN_IN_MAIL_TBL                         128
 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
 
 /*  this define is used for query to change data (delete, insert, update) */
-#define EMSTORAGE_START_WRITE_TRANSACTION(transaction_flag, error_code) \
-       if (transaction_flag)\
-       {\
-               _timedlock_shm_mutex(&mapped_for_db_lock, 2);\
-               if (emstorage_begin_transaction(NULL, NULL, &error_code) == false) \
-               {\
-                       EM_DEBUG_EXCEPTION("emstorage_begin_transaction() error[%d]", error_code);\
-                       goto FINISH_OFF;\
+#define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
+       do {\
+               if (transaction_flag) {\
+                       if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) {\
+                               EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code);\
+                               goto FINISH_OFF;\
+                       }\
                }\
-       }
+       } while(0)
 
 /*  this define is used for query to change data (delete, insert, update) */
-#define EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction_flag, result_code, error_code) \
-       if (transaction_flag)\
-       {\
-               if (result_code == true)\
-               {\
-                       if (emstorage_commit_transaction(NULL, NULL, NULL) == false)\
-                       {\
-                               error_code = EMAIL_ERROR_DB_FAILURE;\
-                               result_code = false;\
+#define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
+       do {\
+               if (transaction_flag) {\
+                       if (result_code == true) {\
+                               if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
+                                       EM_DEBUG_EXCEPTION("emstorage_commit_transaction error");\
+                                       error_code = EMAIL_ERROR_DB_FAILURE;\
+                                       result_code = false;\
+                               }\
+                       }\
+                       else {\
+                               if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
+                                       EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error");\
+                                       error_code = EMAIL_ERROR_DB_FAILURE;\
+                               }\
                        }\
                }\
-               else\
-               {\
-                       if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)\
-                               error_code = EMAIL_ERROR_DB_FAILURE;\
-               }\
-               _unlockshm_mutex(&mapped_for_db_lock);\
-       }
+       } while(0)
 
 /*  this define is used for query to read (select) */
 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
        if (transaction_flag)\
        {\
-               /*_timedlock_shm_mutex(&mapped_for_db_lock, 2);*/\
+               /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
        }
 
 /*  this define is used for query to read (select) */
 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
        if (transaction_flag)\
        {\
-               /*_unlockshm_mutex(&mapped_for_db_lock);*/\
+               /*_unlockshm_mutex(mapped_for_db_lock);*/\
        }
 
 /*  for safety DB operation */
 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
 
+/* for safety secure-storage operation */
+static pthread_mutex_t _ss_handle_lock = PTHREAD_MUTEX_INITIALIZER;
+
 #define        _MULTIPLE_DB_HANDLE
 
 #ifdef _MULTIPLE_DB_HANDLE
 
-#define _DISCONNECT_DB                 /* db_util_close(_db_handle); */
-
 typedef struct
 {
+    char *user_name;
        pthread_t       thread_id;
        sqlite3 *db_handle;
 } db_handle_t;
@@ -207,9 +209,10 @@ typedef struct
 #define MAX_DB_CLIENT 100
 
 /* static int _db_handle_count = 0; */
-db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
+db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
+
 
-sqlite3 *emstorage_get_db_handle()
+sqlite3 *emstorage_get_db_handle(char *multi_user_name)
 {
        EM_DEBUG_FUNC_BEGIN();
        int i;
@@ -218,7 +221,12 @@ sqlite3 *emstorage_get_db_handle()
 
        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 (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;
@@ -233,7 +241,7 @@ sqlite3 *emstorage_get_db_handle()
        return result_db_handle;
 }
 
-int emstorage_set_db_handle(sqlite3 *db_handle)
+int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
 {
        EM_DEBUG_FUNC_BEGIN();
        int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
@@ -244,6 +252,8 @@ int emstorage_set_db_handle(sqlite3 *db_handle)
                if (_db_handle_list[i].thread_id == 0) {
                        _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");
                        EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
                        error_code =  EMAIL_ERROR_NONE;
                        break;
@@ -269,6 +279,8 @@ int emstorage_remove_db_handle()
                {
                        _db_handle_list[i].thread_id = 0;
                        _db_handle_list[i].db_handle = NULL;
+            EM_SAFE_FREE(_db_handle_list[i].user_name);
+
                        EM_DEBUG_LOG("index [%d]", i);
                        error_code = EMAIL_ERROR_NONE;
                        break;
@@ -283,7 +295,6 @@ int emstorage_remove_db_handle()
        return error_code;
 }
 
-
 int emstorage_reset_db_handle_list()
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -294,6 +305,7 @@ int emstorage_reset_db_handle_list()
        {
                _db_handle_list[i].thread_id = 0;
                _db_handle_list[i].db_handle = NULL;
+        EM_SAFE_FREE(_db_handle_list[i].user_name);
        }
        LEAVE_CRITICAL_SECTION(_db_handle_lock)
 
@@ -301,14 +313,13 @@ int emstorage_reset_db_handle_list()
        return EMAIL_ERROR_NONE;
 }
 
-sqlite3 *emstorage_get_db_connection()
+
+sqlite3 *emstorage_get_db_connection(char *multi_user_name)
 {
-       return emstorage_db_open(NULL);
+       return emstorage_db_open(multi_user_name, NULL);
 }
 
-
 #else  /*  _MULTIPLE_DB_HANDLE */
-#define _DISCONNECT_DB                 /* db_util_close(_db_handle); */
 
 sqlite3 *_db_handle = NULL;
 
@@ -338,12 +349,45 @@ int              shm_fd_for_generating_mail_id = 0;
 
 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
-       {  _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
+       do {\
+               _timedlock_shm_mutex(mapped_for_db_lock, 2);\
+               return_value = function_call;\
+               _unlockshm_mutex(mapped_for_db_lock);\
+       } while(0)
+
 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
        {  return_value = function_call; }
 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
 
+static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
+{
+       EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
+       int error = EMAIL_ERROR_NONE;
+       int rc = 0;
+       DB_STMT db_statement = NULL;
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+       ("sqlite3_step failed [%d] [%s]", rc, query_string));
+
+FINISH_OFF :
+
+       if (db_statement != NULL)  {
+               rc = sqlite3_finalize(db_statement);
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       return error;
+}
+
 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
 {
        EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
@@ -354,36 +398,38 @@ INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
-
-       if (fd > 0) {
-               fchmod(fd, 0666);
-               EM_DEBUG_LOG("** Create SHM FILE **");
-               if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
-                       EM_DEBUG_EXCEPTION("ftruncate failed: %s", EM_STRERROR(errno_buf));
-                       return EMAIL_ERROR_SYSTEM_FAILURE;
-               }
-
-               mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-               if (m == MAP_FAILED) {
-                       EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
-                       return EMAIL_ERROR_SYSTEM_FAILURE;
-               }
-
-               m->data = 0;
+       int fd = shm_open (shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
+       if (fd < 0) {
+               EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
+               return EMAIL_ERROR_SYSTEM_FAILURE;
+       }
 
-               pthread_mutexattr_t mattr;
-               pthread_mutexattr_init(&mattr);
-               pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
-               pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
-               pthread_mutex_init(&(m->mutex), &mattr);
-               pthread_mutexattr_destroy(&mattr);
+       fchmod(fd, 0666);
+       EM_DEBUG_LOG("** Create SHM FILE **");
+       if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
+               EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
+               return EMAIL_ERROR_SYSTEM_FAILURE;
        }
-       else {
-               EM_DEBUG_EXCEPTION("shm_open failed: %s", EM_STRERROR(errno_buf));
+
+       mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       if (m == MAP_FAILED) {
+               EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
                return EMAIL_ERROR_SYSTEM_FAILURE;
        }
-       close(fd);
+
+       m->data = 0;
+
+       pthread_mutexattr_t mattr;
+       pthread_mutexattr_init(&mattr);
+       pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
+       pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
+       pthread_mutex_init(&(m->mutex), &mattr);
+       pthread_mutexattr_destroy(&mattr);
+
+    pthread_mutex_destroy(&(m->mutex));
+    munmap(m, sizeof(mmapped_t));
+
+       EM_SAFE_CLOSE (fd);
        EM_DEBUG_FUNC_END();
        return EMAIL_ERROR_NONE;
 }
@@ -404,7 +450,7 @@ int emstorage_shm_file_destroy(const char *shm_file_name)
        return EMAIL_ERROR_NONE;
 }
 
-static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
+int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
 {
        EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
        char errno_buf[ERRNO_BUF_SIZE] = {0};
@@ -416,20 +462,27 @@ static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, m
 
        if (!(*param_mapped)) {
                EM_DEBUG_LOG("** mapping begin **");
-
                if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
-                       *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
+                       *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
                        if ((*param_shm_fd) == -1) {
                                EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
                                return EMAIL_ERROR_SYSTEM_FAILURE;
                        }
                }
-               mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
 
+        fchmod((*param_shm_fd), 0666);
+        EM_DEBUG_LOG("** Create SHM FILE **");
+        if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
+            EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
+            return EMAIL_ERROR_SYSTEM_FAILURE;
+        }
+
+               mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
                if (tmp == MAP_FAILED) {
                        EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
                        return EMAIL_ERROR_SYSTEM_FAILURE;
                }
+
                *param_mapped = tmp;
        }
 
@@ -437,7 +490,7 @@ static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, m
        return EMAIL_ERROR_NONE;
 }
 
-static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
+int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
 {
        EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
 
@@ -451,10 +504,10 @@ static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
        abs_time.tv_sec += sec;
        char errno_buf[ERRNO_BUF_SIZE] = {0};
 
-       int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
+       int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
 
        if (err == EOWNERDEAD) {
-               err = pthread_mutex_consistent(&((*param_mapped)->mutex));
+               err = pthread_mutex_consistent(&(param_mapped->mutex));
                EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
        }
        else if (err != 0) {
@@ -466,10 +519,10 @@ static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
        return EMAIL_ERROR_NONE;
 }
 
-void _unlockshm_mutex(mmapped_t **param_mapped)
+void _unlockshm_mutex(mmapped_t *param_mapped)
 {
        EM_DEBUG_FUNC_BEGIN();
-       pthread_mutex_unlock(&((*param_mapped)->mutex));
+       pthread_mutex_unlock(&(param_mapped->mutex));
        EM_DEBUG_FUNC_END();
 }
 /* ------------------------------------------------------------------------------ */
@@ -477,7 +530,7 @@ void _unlockshm_mutex(mmapped_t **param_mapped)
 
 static int _open_counter = 0;
 
-static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
+static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
 static int _read_password_from_secure_storage(char *file_name, char **password);
 
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
@@ -532,6 +585,12 @@ enum
        CREATE_TABLE_DUMMY_INDEX3,
 #endif
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+#else
+       CREATE_TABLE_DUMMY_INDEX4,
+#endif
+
        /*CREATE INDEX*/
        CREATE_TABLE_MAIL_ACCOUNT_IDX,
        CREATE_TABLE_MAIL_BOX_IDX,
@@ -680,8 +739,10 @@ enum
        TAG_ID_IDX_IN_MAIL_TBL,
        REPLIED_TIME_IDX_IN_MAIL_TBL,
        FORWARDED_TIME_IDX_IN_MAIL_TBL,
+       DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
        EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
        EAS_DATA_IDX_IN_MAIL_TBL,
+        USER_NAME_IDX_IN_MAIL_TBL,
        FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
 };
 
@@ -690,6 +751,7 @@ enum
        ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
        ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
        ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
+       CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
        ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
        MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
        ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
@@ -723,6 +785,19 @@ enum {
 #endif
 };
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+enum
+{
+       ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
+       STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+       ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+       MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+       SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+       MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+    MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
+};
+#endif
+
 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
 typedef struct _em_mail_id_list {
        int mail_id;
@@ -756,6 +831,7 @@ static char *g_test_query[] = {
                " keep_mails_on_pop_server_after_download, "
                " auto_resend_times, "
                " outgoing_server_size_limit, "
+               " wifi_auto_download, "
                " pop_before_smtp, "
                " incoming_server_requires_apop,"
                " logo_icon_path, "
@@ -775,6 +851,11 @@ static char *g_test_query[] = {
                " add_signature, "
                " signature"
                ", add_my_address_to_bcc"
+               ", notification_status "
+               ", vibrate_status "
+               ", display_content_status "
+               ", default_ringtone_status "
+               ", alert_ringtone_path "
                ", account_svc_id "
                ", index_color "
                ", sync_status "
@@ -901,6 +982,7 @@ static char *g_test_query[] = {
                "   activity_type, "
                "   mailbox_id, "
                "   mailbox_name "
+                "   multi_user_name "
                " FROM mail_partial_body_activity_tbl ",
 #endif
 
@@ -947,14 +1029,6 @@ static char *g_test_query[] = {
                "   filepath, "
                "   password "
                " FROM mail_certificate_tbl     ",
-#ifdef __FEATURE_BODY_SEARCH__
-               "SELECT "
-               "       mail_id, "
-               "   account_id, "
-               "   mailbox_id, "
-               "   body_text "
-               " FROM mail_text_tbl    ",
-#endif
                "SELECT "
                "       task_id, "
                "   task_type, "
@@ -964,6 +1038,25 @@ static char *g_test_query[] = {
                "   task_parameter , "
                "       date_time "
                " FROM mail_task_tbl    ",
+#ifdef __FEATURE_BODY_SEARCH__
+               "SELECT "
+               "       mail_id, "
+               "   account_id, "
+               "   mailbox_id, "
+               "   body_text "
+               " FROM mail_text_tbl    ",
+#endif
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+               "SELECT  "
+               "   activity_id, "
+               "   status, "
+               "   account_id, "
+               "   mail_id, "
+               "   server_mail_id, "
+               "   mailbox_id, "
+                "   multi_user_name, "
+               " FROM mail_auto_download_activity_tbl ",
+#endif
                NULL,
 };
 
@@ -1450,7 +1543,7 @@ INTERNAL_FUNC int emstorage_close(int *err_code)
        int ret = false;
        int error = EMAIL_ERROR_NONE;
 
-       emstorage_db_close(&error);
+       if (!emstorage_db_close(NULL, &error)) 
 
        if (--_open_counter == 0)
                _emstorage_close_once();
@@ -1464,29 +1557,56 @@ INTERNAL_FUNC int emstorage_close(int *err_code)
        return ret;
 }
 
-static void *_emstorage_open_once(int *err_code)
+static void *_emstorage_open_once(char *multi_user_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
        int error = EMAIL_ERROR_NONE;
 
-       mkdir(DATA_PATH, DIRECTORY_PERMISSION);
-       mkdir(EMAILPATH, DIRECTORY_PERMISSION);
-       mkdir(MAILHOME, DIRECTORY_PERMISSION);
-       mkdir(MAILTEMP, DIRECTORY_PERMISSION);
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+        char buf[MAX_PATH] = {0};
+               char *prefix_path = NULL;
+
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+
+        memset(buf, 0x00, sizeof(buf));
+        SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
+        mkdir(buf, DIRECTORY_PERMISSION);
 
-       _delete_temp_file(MAILTEMP);
+        memset(buf, 0x00, sizeof(buf));
+        SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
+        mkdir(buf, DIRECTORY_PERMISSION);
 
-       if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
+        memset(buf, 0x00, sizeof(buf));
+        SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
+        mkdir(buf, DIRECTORY_PERMISSION);
+
+        _delete_temp_file(buf);
+               EM_SAFE_FREE(prefix_path);
+    } else {
+        mkdir(DATA_PATH, DIRECTORY_PERMISSION);
+        mkdir(EMAILPATH, DIRECTORY_PERMISSION);
+        mkdir(MAILHOME, DIRECTORY_PERMISSION);
+        mkdir(MAILTEMP, DIRECTORY_PERMISSION);
+
+        _delete_temp_file(MAILTEMP);
+    }
+
+       if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
                EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:
+       
+
        if (err_code != NULL)
                *err_code = error;
 
-
        return NULL;
 }
 
@@ -1502,14 +1622,14 @@ static int _callback_sqlite_busy_handler(void *pData, int count)
        return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
 }
 
-static int _delete_all_files_and_directories(int *err_code)
+static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
        int error = EMAIL_ERROR_NONE;
        int ret = false;
 
-       if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
+       if (!emstorage_delete_file(db_file_path, &error)) {
                if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
                        EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
                        goto FINISH_OFF;
@@ -1530,7 +1650,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int _recovery_from_malformed_db_file(int *err_code)
+static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1538,7 +1658,7 @@ static int _recovery_from_malformed_db_file(int *err_code)
        int ret = false;
 
        /* Delete all files and directories */
-       if (!_delete_all_files_and_directories(&error)) {
+       if (!_delete_all_files_and_directories(db_file_path, &error)) {
                EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -1556,45 +1676,8 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END();
        return ret;
 }
-int _xsystem(const char *argv[])
-{
-       int status = 0;
-       pid_t pid;
-       pid = fork();
-       switch (pid) {
-       case -1:
-               perror("fork failed");
-               return -1;
-       case 0:
-               /* child */
-               execvp(argv[0], (char *const *)argv);
-               _exit(-1);
-       default:
-               /* parent */
-               break;
-       }
-       if (waitpid(pid, &status, 0) == -1)
-       {
-               perror("waitpid failed");
-               return -1;
-       }
-       if (WIFSIGNALED(status))
-       {
-               perror("signal");
-               return -1;
-       }
-       if (!WIFEXITED(status))
-       {
-               /* shouldn't happen */
-               perror("should not happen");
-               return -1;
-       }
-       return WEXITSTATUS(status);
-}
-
-#define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
 
-INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
+INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int rc = 0;
@@ -1609,35 +1692,38 @@ INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
                        *err_code = error;
                return true;
        }
-       /*Generate db file*/
-       struct stat sts;
-       ret = stat(EMAIL_SERVICE_DB_FILE_PATH , &sts);
-       if (ret == -1 && errno == ENOENT)
-       {
-               const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL };
-               ret = _xsystem(argv_script);
-       }
+
+    EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
+
        /*  db open */
-       EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
        if (SQLITE_OK != rc) {
-               EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
-               error = EMAIL_ERROR_DB_FAILURE;
-               db_util_close(*sqlite_handle);
+               EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
+               if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
+                       error = EMAIL_ERROR_PERMISSION_DENIED;
+               } else {
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+               sqlite3_close(*sqlite_handle);
                *sqlite_handle = NULL;
 
                if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
                        EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
-                       if (!_recovery_from_malformed_db_file(&error)) {
+                       if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
                                EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
                                goto FINISH_OFF;
                        }
 
                        EM_DEBUG_LOG("Open DB again");
-                       EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
+                       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
                        if (SQLITE_OK != rc) {
-                               EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
-                               error = EMAIL_ERROR_DB_FAILURE;
-                               db_util_close(*sqlite_handle);
+                               EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
+                               if (SQLITE_PERM == rc) {
+                                       error = EMAIL_ERROR_PERMISSION_DENIED;
+                               } else {
+                                       error = EMAIL_ERROR_DB_FAILURE;
+                               }
+                               sqlite3_close(*sqlite_handle);
                                *sqlite_handle = NULL;
                                goto FINISH_OFF; /*prevent 33351*/
                        }
@@ -1652,12 +1738,11 @@ INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
        if (SQLITE_OK != rc) {
                EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
                error = EMAIL_ERROR_DB_FAILURE;
-               db_util_close(*sqlite_handle);
+               sqlite3_close(*sqlite_handle);
                *sqlite_handle = NULL;
                goto FINISH_OFF;
        }
 
-
        ret = true;
 
 FINISH_OFF:
@@ -1668,33 +1753,63 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
+INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
        sqlite3 *_db_handle = NULL;
 
        int error = EMAIL_ERROR_NONE;
+       char *prefix_path = NULL;
 
-       _db_handle = emstorage_get_db_handle();
+       _db_handle = emstorage_get_db_handle(multi_user_name);
 
        if( _db_handle == NULL) {
-               if (!em_db_open(&_db_handle, &error)) {
+               char *output_file_path = NULL;
+        char temp_file_path[MAX_PATH] = {0};
+
+        if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+                       error = emcore_get_container_path(multi_user_name, &prefix_path);
+                       if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
+                               goto FINISH_OFF;
+                       }
+        } else {
+            prefix_path = strdup("");
+        }
+
+               if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
+                       if ((error = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
+                               goto FINISH_OFF;
+                       }
+
+                       SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
+                       EM_SAFE_FREE(output_file_path);
+               } else {
+                       SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
+               }
+
+               if (!em_db_open(temp_file_path, &_db_handle, &error)) {
                        EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
                        goto FINISH_OFF;
                }
+
                _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
 
 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
                _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
 
-               emstorage_set_db_handle(_db_handle);
+               emstorage_set_db_handle(multi_user_name, _db_handle);
+
                emstorage_initialize_field_count();
        }
 
-
 FINISH_OFF:
+
+       EM_SAFE_FREE(prefix_path);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -1702,21 +1817,20 @@ FINISH_OFF:
        return _db_handle;
 }
 
-INTERNAL_FUNC int emstorage_db_close(int *err_code)
+INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 #ifdef _MULTIPLE_DB_HANDLE
-       sqlite3 *_db_handle = emstorage_get_db_handle();
+       sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
 #endif
 
        int error = EMAIL_ERROR_NONE;
        int ret = false;
 
        if (_db_handle) {
-               ret = db_util_close(_db_handle);
-
+               ret = sqlite3_close(_db_handle);
                if (ret != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
+                       EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
                        error = EMAIL_ERROR_DB_FAILURE;
                        ret = false;
                        goto FINISH_OFF;
@@ -1737,38 +1851,52 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_open(int *err_code)
+INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
-       int ret = false;
-       int error = EMAIL_ERROR_NONE;
-
-       int retValue;
+    int ret = false;
+    int error = EMAIL_ERROR_NONE;
+    int retValue;
+       char *prefix_path = NULL;
+    char buf[MAX_PATH] = {0};
+
+    if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
+        SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
+    } else {
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+        SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
+    }
 
-       retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
+    retValue = mkdir(buf, DIRECTORY_PERMISSION);
 
-       EM_DEBUG_LOG("mkdir return- %d", retValue);
-       EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
+    EM_DEBUG_LOG("mkdir return- %d", retValue);
+    EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
 
-       if (emstorage_db_open(&error) == NULL) {
-               EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
-               goto FINISH_OFF;
-       }
+    if (emstorage_db_open(multi_user_name, &error) == NULL) {
+        EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
+        goto FINISH_OFF;
+    }
 
-       if (_open_counter++ == 0) {
-               _emstorage_open_once(&error);
-       }
+    if (_open_counter++ == 0) {
+        _emstorage_open_once(multi_user_name, &error);
+    }
 
-       ret = true;
+    ret = true;
 
 FINISH_OFF:
-       if (err_code != NULL)
-               *err_code = error;
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+       EM_SAFE_FREE(prefix_path);
+
+    if (err_code != NULL)
+        *err_code = error;
+
+    EM_DEBUG_FUNC_END("ret [%d]", ret);
+    return ret;
 }
 
 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
@@ -1851,7 +1979,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
+INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1860,7 +1988,8 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
        int query_len = 0;
        char sql_query_string[QUERY_SIZE] = {0, };
        char **create_table_query = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
        if (error != EMAIL_ERROR_NONE) {
@@ -1879,6 +2008,7 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
        char *sql;
        char **result = NULL;
 
+       /*  1. create mail_account_tbl */
        sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
@@ -1892,17 +2022,20 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                        ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
 
                EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
-
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
                /*  create mail_account_tbl unique index */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_account_tbl */
@@ -1929,17 +2062,19 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
                /*  create mail_local_mailbox_tbl unique index */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_box_tbl */
@@ -1966,17 +2101,20 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
                /*  create mail_read_mail_uid_tbl unique index */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_read_mail_uid_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2002,10 +2140,12 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_rule_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2029,28 +2169,35 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                /*  create mail_tbl unique index */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                /*  create mail_tbl index for date_time */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                /*  create mail_tbl index for thread_item_count */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
                /*  just one time call */
@@ -2074,20 +2221,24 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
                EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
                        ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
+
                EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                /*  create mail_attachment_tbl unique index */
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_attachment_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2114,10 +2265,12 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_rule_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2144,15 +2297,19 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_contact_sync_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2179,10 +2336,12 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                        EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
 
                        SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
+                       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                       if (error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                               goto FINISH_OFF;
+                       }
 
-                       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
                } /*  mail_rule_tbl */
                else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2207,9 +2366,12 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_contact_sync_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2232,13 +2394,19 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_task_tbl */
        else if (type == EMAIL_CREATE_DB_CHECK)  {
@@ -2262,9 +2430,11 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
                EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
 
                EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
-
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        } /*  mail_text_tbl */
@@ -2278,6 +2448,37 @@ INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *e
 
 #endif
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       /*  create mail_auto_download_activity_tbl */
+       sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
+
+       if (atoi(result[1]) < 1) {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
+               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
+
+               EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
+
+               EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
+                error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                if (error != EMAIL_ERROR_NONE) {
+                        EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                        goto FINISH_OFF;
+                }
+
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
+       } /*  mail_auto_download_activity_tbl */
+       else if (type == EMAIL_CREATE_DB_CHECK)  {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
+               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
+       }
+
+       sqlite3_free_table(result);
+       result = NULL;
+#endif
+
+
        ret = true;
 
 FINISH_OFF:
@@ -2291,8 +2492,6 @@ FINISH_OFF:
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
        }
 
-       _DISCONNECT_DB;
-
 FINISH_OFF2:
        if (create_table_query) {
                int i = 0;
@@ -2312,14 +2511,14 @@ FINISH_OFF2:
 }
 
 /* Query series --------------------------------------------------------------*/
-
-INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
+INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_transaction[%d], output_total_mail_count[%p], output_unseen_mail_count[%p]", input_conditional_clause, input_transaction, output_total_mail_count, output_unseen_mail_count);
        int rc = -1;
+       int query_size = 0;
        int error = EMAIL_ERROR_NONE;
        DB_STMT hStmt = NULL;
-       char sql_query_string[QUERY_SIZE] = {0, };
+       char *sql_query_string = NULL;
        char **result;
        sqlite3 *local_db_handle = NULL;
 
@@ -2328,12 +2527,19 @@ INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_claus
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       memset(&sql_query_string, 0x00, sizeof(sql_query_string));
-       local_db_handle = emstorage_get_db_connection();
+       query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
+       sql_query_string = em_malloc(query_size);
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(input_transaction);
 
-       SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
+       SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
        EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
 
        if (output_total_mail_count)  {
@@ -2363,19 +2569,20 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
-       _DISCONNECT_DB;
+
+       EM_SAFE_FREE(sql_query_string);
 
        EM_DEBUG_FUNC_END("error [%d]", error);
        return error;
 }
 
-INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
+INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_conditional_clause [%p], input_transaction [%d], output_mail_id_list [%p], output_mail_id_count [%p]", input_conditional_clause, input_transaction, output_mail_id_list, output_mail_id_count);
 
@@ -2384,23 +2591,32 @@ INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_cla
        int      rc = -1;
        int      cur_query = 0;
        int      col_index;
+       int      query_size = 0;
        int      error = EMAIL_ERROR_NONE;
        int     *result_mail_id_list = NULL;
        char   **result = NULL;
-       char     sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       char     *sql_query_string = NULL;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
        EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
 
+       query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
+       sql_query_string = em_malloc(query_size);
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
        EMSTORAGE_START_READ_TRANSACTION(input_transaction);
 
        /* Composing query */
-       SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
+       SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
        EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
 
-       EM_DEBUG_LOG("query[%s].", sql_query_string);
+       EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
 
        /* Performing query */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
@@ -2440,7 +2656,8 @@ FINISH_OFF:
                sqlite3_free_table(result);
 
        EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
-       _DISCONNECT_DB;
+
+       EM_SAFE_FREE(sql_query_string);
 
        if(error != EMAIL_ERROR_NONE)
                EM_SAFE_FREE(result_mail_id_list);
@@ -2449,7 +2666,7 @@ FINISH_OFF:
        return error;
 }
 
-INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
+INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
@@ -2467,7 +2684,7 @@ INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int
        char *target_field = NULL;
        char *sql_query_string = NULL;
        email_mail_list_item_t *mail_list_item_from_tbl = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
        EM_IF_NULL_RETURN_VALUE(result_count, false);
@@ -2606,8 +2823,6 @@ FINISH_OFF:
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
-
        EM_SAFE_FREE(sql_query_string);
        EM_SAFE_FREE(date_time_string);
 
@@ -2620,7 +2835,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
+INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_tbl, result_count, transaction, err_code);
 
@@ -2635,7 +2850,7 @@ INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int t
        int error = EMAIL_ERROR_NONE;
        char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
        emstorage_mail_tbl_t* p_data_tbl = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
@@ -2715,8 +2930,10 @@ INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int t
                _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].tag_id), col_index++);
                _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].replied_time), col_index++);
                _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
+               _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
                _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
                _get_table_field_data_blob  (result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
+               _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
        }
 
        ret = true;
@@ -2738,8 +2955,6 @@ FINISH_OFF:
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
-
        if (err_code != NULL)
                *err_code = error;
 
@@ -2748,7 +2963,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
+INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_text_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_text_tbl, result_count, transaction, err_code);
 
@@ -2768,13 +2983,13 @@ INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause,
        char **result = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
        emstorage_mail_text_tbl_t* p_data_tbl = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
@@ -2819,8 +3034,6 @@ FINISH_OFF:
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
-
        if (err_code != NULL)
                *err_code = error;
 
@@ -2829,7 +3042,7 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
+INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_ordering_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
 
@@ -2844,7 +3057,7 @@ INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clau
        char sql_query_string[QUERY_SIZE] = {0, };
        char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time, MBT.eas_data_length, MBT.eas_data ";
        emstorage_mailbox_tbl_t* p_data_tbl = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(input_transaction);
 
@@ -2854,7 +3067,7 @@ INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clau
        }
        else {  /* with read count and total count */
                col_index = 17;
-               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl WHERE flags_deleted_field = 0 GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
        }
 
        EM_DEBUG_LOG_DEV ("query[%s]", sql_query_string);
@@ -2866,7 +3079,7 @@ INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clau
        EM_DEBUG_LOG_DEV ("result count [%d]", count);
 
        if(count == 0) {
-               EM_DEBUG_LOG ("Can't find mailbox");
+               EM_DEBUG_LOG_SEC ("Can't find mailbox query[%s]", sql_query_string);
                error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
                goto FINISH_OFF;
        }
@@ -2917,15 +3130,12 @@ FINISH_OFF:
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
-
        EM_DEBUG_FUNC_END("error [%d]", error);
        return error;
 }
 
 /* Query series --------------------------------------------------------------*/
-
-INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int rc = -1, ret = false;
@@ -2942,7 +3152,7 @@ INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, i
                return false;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
@@ -2960,7 +3170,7 @@ INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, i
                account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
                account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
        );
-       EM_DEBUG_LOG("Query[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
                ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
@@ -2983,7 +3193,6 @@ INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, i
 FINISH_OFF:
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -2993,7 +3202,7 @@ FINISH_OFF:
 
 }
 
-INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -3008,13 +3217,13 @@ INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *
 
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-       char err_msg[1024];
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
-       EM_DEBUG_LOG("SQL STMT [%s]", sql_query_string);
-       EMSTORAGE_PROTECTED_FUNC_CALL (sqlite3_prepare_v2 (local_db_handle, sql_query_string, 
+       EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
+       EMSTORAGE_PROTECTED_FUNC_CALL (sqlite3_prepare_v2 (local_db_handle, sql_query_string,
                                                           EM_SAFE_STRLEN (sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
@@ -3031,19 +3240,15 @@ INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *
 FINISH_OFF:
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt=NULL;
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
-               EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -3052,13 +3257,13 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
+INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
        int i = 0, count = 0, rc = -1, ret = false;
        int field_index = 0;
+       int sql_len = 0;
        int error = EMAIL_ERROR_NONE;
        emstorage_account_tbl_t *p_data_tbl = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
@@ -3074,7 +3279,8 @@ INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_
                return false;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
@@ -3092,7 +3298,81 @@ INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_
                ret = true;
                goto FINISH_OFF;
        }
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
+       sql_len = EM_SAFE_STRLEN(sql_query_string);
+
+       SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
+               "account_id, "
+               "account_name, "
+               "logo_icon_path, "
+               "user_data, "
+               "user_data_length, "
+               "account_svc_id, "
+               "sync_status, "
+               "sync_disabled, "
+               "default_mail_slot_size, "
+               "roaming_option, "
+               "color_label, "
+               "user_display_name, "
+               "user_email_address, "
+               "reply_to_address, "
+               "return_address, "
+               "incoming_server_type, "
+               "incoming_server_address, "
+               "incoming_server_port_number, "
+               "incoming_server_user_name, "
+               "incoming_server_password, "
+               "incoming_server_secure_connection, "
+               "incoming_server_authentication_method, "
+               "retrieval_mode, "
+               "keep_mails_on_pop_server_after_download, "
+               "check_interval, "
+               "auto_download_size, "
+               "peak_interval, "
+               "peak_days, "
+               "peak_start_time, "
+               "peak_end_time, "
+               "outgoing_server_type, "
+               "outgoing_server_address, "
+               "outgoing_server_port_number, "
+               "outgoing_server_user_name, "
+               "outgoing_server_password, "
+               "outgoing_server_secure_connection, "
+               "outgoing_server_need_authentication, "
+               "outgoing_server_use_same_authenticator, "
+               "priority, "
+               "keep_local_copy, "
+               "req_delivery_receipt, "
+               "req_read_receipt, "
+               "download_limit, "
+               "block_address, "
+               "block_subject, "
+               "display_name_from, "
+               "reply_with_body, "
+               "forward_with_files, "
+               "add_myname_card, "
+               "add_signature, "
+               "signature, "
+               "add_my_address_to_bcc, "
+               "notification_status, "
+               "vibrate_status, "
+               "display_content_status, "
+               "default_ringtone_status, "
+               "alert_ringtone_path, "
+               "auto_resend_times, "
+               "outgoing_server_size_limit, "
+               "wifi_auto_download, "
+               "pop_before_smtp, "
+               "incoming_server_requires_apop, "
+               "smime_type, "
+               "certificate_path, "
+               "cipher_type, "
+               "digest_type"
+       );
+
+       sql_len = EM_SAFE_STRLEN(sql_query_string);
+
+       SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
 
@@ -3131,6 +3411,7 @@ INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
+               _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
@@ -3172,8 +3453,14 @@ INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_
                _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
+               _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
                _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
@@ -3184,28 +3471,36 @@ INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_
                /* EAS passwd is not accessible */
                if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        /*  get password from the secure storage */
-                       char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-                       char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
+                       char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
+                       char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
 
                        EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
                        EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
 
                        /*  get password file name */
-                       if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
+                       error = _get_password_file_name (multi_user_name, p_data_tbl[i].account_id, 
+                                                   recv_password_file_name, 
+                                                   send_password_file_name);
+                       if (error != EMAIL_ERROR_NONE){
+                               EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
                                error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
                                goto FINISH_OFF;
                        }
 
                        /*  read password from secure storage */
-                       if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
-                               EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
+                       error = _read_password_from_secure_storage (recv_password_file_name,
+                                          &(p_data_tbl[i].incoming_server_password));
+                       if (error < 0 ) {
+                               EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
+                                                    recv_password_file_name, error);
                                error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
                                goto FINISH_OFF;
                        }
-
-                       if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
-                               EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
+                       error = _read_password_from_secure_storage (send_password_file_name,
+                                          &(p_data_tbl[i].outgoing_server_password));
+                       if (error < 0) {
+                               EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
+                                                     send_password_file_name, error);
                                error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
                                goto FINISH_OFF;
                        }
@@ -3229,18 +3524,15 @@ FINISH_OFF:
                emstorage_free_account(&p_data_tbl, count, NULL);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG_DEV("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -3249,8 +3541,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(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, int mailbox_id, char *server_mail_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);
 
@@ -3271,7 +3562,7 @@ INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(int mailbox_id, char *s
 
        EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
 
-       if(!emstorage_query_mail_tbl(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;
        }
@@ -3300,12 +3591,7 @@ static int _write_conditional_clause_for_getting_mail_list(int account_id, int m
        }
 
        /*  where clause */
-       if (account_id == ALL_ACCOUNT) {
-               cur_clause += (conditional_clause_count++ == 0)?
-                       SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
-                       SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
-       }
-       else {
+       if (account_id != ALL_ACCOUNT) {
                cur_clause += (conditional_clause_count++ == 0)?
                        SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
                        SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
@@ -3316,11 +3602,6 @@ static int _write_conditional_clause_for_getting_mail_list(int account_id, int m
                        SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
                        SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
        }
-       else if(account_id != ALL_ACCOUNT) {
-               cur_clause += (conditional_clause_count++ == 0)?
-                       SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
-                       SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
-       }
 
        if (thread_id > 0) {
                cur_clause += (conditional_clause_count++ == 0)?
@@ -3446,7 +3727,7 @@ static int _write_conditional_clause_for_getting_mail_list(int account_id, int m
   *
   *
   */
-INTERNAL_FUNC int emstorage_get_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, 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_mail_list(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
@@ -3464,7 +3745,7 @@ INTERNAL_FUNC int emstorage_get_mail_list(int account_id, int mailbox_id, email_
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
+       if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
                goto FINISH_OFF;
        }
@@ -3486,7 +3767,7 @@ FINISH_OFF:
   *
   *
   */
-INTERNAL_FUNC int emstorage_get_mails(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
+INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_PROFILE_BEGIN(emStorageGetMails);
@@ -3509,7 +3790,7 @@ INTERNAL_FUNC int emstorage_get_mails(int account_id, int mailbox_id, email_emai
 
        EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
 
-       if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
+       if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -3540,7 +3821,7 @@ FINISH_OFF:
   *
   *
   */
-INTERNAL_FUNC int emstorage_get_searched_mail_list(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_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)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -3565,7 +3846,7 @@ INTERNAL_FUNC int emstorage_get_searched_mail_list(int account_id, int mailbox_i
 
        EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
 
-       if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
+       if(!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
                goto FINISH_OFF;
        }
@@ -3586,7 +3867,7 @@ FINISH_OFF:
 }
 
 
-static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
+static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
 
@@ -3595,8 +3876,16 @@ static int _get_password_file_name(int account_id, char *recv_password_file_name
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
-       sprintf(send_password_file_name, ".email_account_%d_send", account_id);
+    EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
+
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               sprintf(recv_password_file_name, ".email_account_%d_recv_%s", account_id, multi_user_name);
+               sprintf(send_password_file_name, ".email_account_%d_send_%s", account_id, multi_user_name);
+       } else {
+               sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
+               sprintf(send_password_file_name, ".email_account_%d_send", account_id);
+       }
+
        EM_DEBUG_FUNC_END();
        return EMAIL_ERROR_NONE;
 }
@@ -3649,8 +3938,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
 
@@ -3671,7 +3959,8 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
        char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
        char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        /*  Make query string */
@@ -3707,11 +3996,13 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
                        "outgoing_server_use_same_authenticator,"
                        "auto_resend_times,"
                        "outgoing_server_size_limit,"
+                       "wifi_auto_download,"
                        "pop_before_smtp,"
                        "incoming_server_requires_apop,"
                        "logo_icon_path,"
                        "user_data,"
                        "user_data_length,"
+                       "color_label,"
                        "check_interval,"
                        "sync_status,");
                sql_len = EM_SAFE_STRLEN(sql_query_string);
@@ -3739,6 +4030,11 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
                        "add_signature,"
                        "signature,"
                        "add_my_address_to_bcc,"
+                       "notification_status,"
+                       "vibrate_status,"
+                       "display_content_status,"
+                       "default_ringtone_status,"
+                       "alert_ringtone_path,"
                        "account_svc_id,"
                        "sync_disabled,"
                        "default_mail_slot_size,"
@@ -3755,7 +4051,7 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
        SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
 
        /*  FROM clause */
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
        /*  execute a sql and count rows */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -3810,11 +4106,13 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
                _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
                _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
+               _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
        }
@@ -3824,7 +4122,7 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
 
        if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
                /*  get password file name */
-               if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
+               if ((error = _get_password_file_name(multi_user_name, p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
                        goto FINISH_OFF;
                }
@@ -3837,11 +4135,13 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
 
                EM_DEBUG_LOG_DEV ("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
 
-               if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
-                       EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
-                       goto FINISH_OFF;
+               if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
+                       if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
+                               EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
+                               goto FINISH_OFF;
+                       }
+                       EM_DEBUG_LOG_DEV ("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
                }
-               EM_DEBUG_LOG_DEV ("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
        }
 
        if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
@@ -3859,6 +4159,11 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
+               _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
+               _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
+               _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
+               _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
+               _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
                _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
                _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
@@ -3881,13 +4186,12 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -3896,7 +4200,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int password_type, int *password_length, int *err_code)
+INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
 
@@ -3915,7 +4219,7 @@ INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int p
 
 
        /*  get password file name */
-       if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
+       if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
                goto FINISH_OFF;
        }
@@ -3954,7 +4258,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_account_password(int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
+INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_incoming_server_password[%p], input_outgoing_server_password[%p]", input_account_id, input_incoming_server_password, input_outgoing_server_password);
 
@@ -3963,14 +4267,14 @@ INTERNAL_FUNC int emstorage_update_account_password(int input_account_id, char *
        char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
        char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 
-       if(input_incoming_server_password == NULL || input_outgoing_server_password == NULL) {
+       if(input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
                EM_DEBUG_EXCEPTION_SEC("Invalid param");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
        /*  get password file name */
-       if ((err = _get_password_file_name(input_account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
+       if ((err = _get_password_file_name(multi_user_name, input_account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
                goto FINISH_OFF;
        }
@@ -4009,7 +4313,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
 
@@ -4026,8 +4330,9 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_account_tbl SET"
@@ -4040,6 +4345,7 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
                ", sync_disabled = ?"
                ", default_mail_slot_size = ?"
                ", roaming_option = ?"
+               ", color_label = ?"
                ", user_display_name = ?"
                ", user_email_address = ?"
                ", reply_to_address = ?"
@@ -4079,19 +4385,26 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
                ", add_signature = ?"
                ", signature = ?"
                ", add_my_address_to_bcc = ?"
+               ", notification_status = ?"
+               ", vibrate_status = ?"
+               ", display_content_status = ?"
+               ", default_ringtone_status = ?"
+               ", alert_ringtone_path = ?"
                ", auto_resend_times = ?"
                ", outgoing_server_size_limit = ?"
+               ", wifi_auto_download = ?"
                ", pop_before_smtp = ?"
                ", incoming_server_requires_apop = ?"
                ", smime_type = ?"
                ", certificate_path = ?"
                ", cipher_type = ?"
                ", digest_type = ?"
+               ", user_name = ?"
                " WHERE account_id = ?");
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
-       EM_DEBUG_LOG("SQL[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
 
@@ -4106,6 +4419,7 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
@@ -4145,14 +4459,21 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
        _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
+       _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
        _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
+       _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_int(hStmt, i++, account_id);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
@@ -4169,23 +4490,22 @@ INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl
                goto FINISH_OFF;
        }
 
-       if((error = emstorage_update_account_password(account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
-               goto FINISH_OFF;
+       if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
+               if((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
+                       goto FINISH_OFF;
+               }
        }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -4202,11 +4522,10 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
+INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
        int error = EMAIL_ERROR_NONE;
-       int rc = 0;
        int result = 0;
        char sql_query_string[QUERY_SIZE] = {0, };
        sqlite3 *local_db_handle = NULL;
@@ -4216,26 +4535,29 @@ INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        /* Write query string */
        SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
 
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
 
        /* Execute query */
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+               goto FINISH_OFF;
+       }
+
        if (sqlite3_changes(local_db_handle) == 0)
                EM_DEBUG_LOG("no mail matched...");
 
 
 FINISH_OFF:
        result = (error == EMAIL_ERROR_NONE) ? true : false;
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
 
        if (error == EMAIL_ERROR_NONE) {
                if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
@@ -4245,7 +4567,7 @@ FINISH_OFF:
        return error;
 }
 
-INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
+INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status,int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
 
@@ -4259,7 +4581,7 @@ INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *resu
        int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
        char sql_query_string[QUERY_SIZE] = {0, };
        char **result = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        if(account_id)
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
@@ -4292,7 +4614,6 @@ INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *resu
 
 FINISH_OFF:
 
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -4301,16 +4622,16 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], set_operator[%d], sync_status [%d], transaction[%d], err_code[%p]", account_id, set_operator, sync_status, transaction, err_code);
 
        int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
        char sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        if(set_operator != SET_TYPE_SET && account_id) {
-               if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
+               if(!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
                        if (err_code != NULL)
                                *err_code = error;
@@ -4331,18 +4652,20 @@ INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_
                EM_DEBUG_LOG("set_value [%d]", set_value);
        }
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        if(account_id)
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
        else
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
 
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+               goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
 
@@ -4355,8 +4678,7 @@ INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (error == EMAIL_ERROR_NONE) {
                if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
@@ -4370,7 +4692,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
 
@@ -4389,9 +4711,9 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
        char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
        char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
        char **result = NULL;
@@ -4407,7 +4729,7 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
 
        account_tbl->account_id = rc;
 
-       if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
+       if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
                goto FINISH_OFF;
        }
@@ -4426,6 +4748,7 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
                "  , ? "  /*   sync_disabled */
                "  , ? "  /*   default_mail_slot_size */
                "  , ? "  /*   roaming_option */
+               "  , ? "  /*   color_label */
                "  , ? "  /*   user_display_name */
                "  , ? "  /*   user_email_address */
                "  , ? "  /*   reply_to_address */
@@ -4469,19 +4792,25 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
                "  , ? "  /*   add_my_address_to_bcc */
                "  , ? "  /*   auto_resend_times */
                "  , ? "  /*   outgoing_server_size_limit */
+               "  , ? "  /*   wifi_auto_download */
                "  , ? "  /*   pop_before_smtp */
                "  , ? "  /*   incoming_server_requires_apop */
                "  , ? "  /*   smime_type */
                "  , ? "  /*   certificate_path */
                "  , ? "  /*   cipher_type */
                "  , ? "  /*   digest_type */
+               "  , ? "  /*   notification_status */
+               "  , ? "  /*   vibrate_status */
+               "  , ? "  /*   display_content_status */
+               "  , ? "  /*   default_ringtone_status */
+               "  , ? "  /*   alert_ringtone_path */
+               "  , ? "  /*   user_name */
                ") ");
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
        int i = 0;
 
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
@@ -4494,6 +4823,7 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
        _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
@@ -4537,12 +4867,19 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
        _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
        _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
+       _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
+       _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
+       _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
 
@@ -4568,13 +4905,12 @@ INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, in
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -4591,8 +4927,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
 
@@ -4606,17 +4941,18 @@ INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int
 
        int rc = -1, ret = false;
        int error = EMAIL_ERROR_NONE;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
-       DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
        char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
        char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 
        /*  get password file name */
-       if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
+       if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
                goto FINISH_OFF;
        }
@@ -4624,12 +4960,11 @@ INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int
        /*  delete from db */
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        /*  validate account existence */
        rc = sqlite3_changes(local_db_handle);
@@ -4650,19 +4985,11 @@ INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int
                error = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
+
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
-
-       if (hStmt != NULL)  {
-               rc = sqlite3_finalize(hStmt);
-               if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-                       error = EMAIL_ERROR_DB_FAILURE;
-               }
-       }
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -4705,7 +5032,9 @@ INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list,
                        EM_SAFE_FREE(p[i].logo_icon_path);
                        EM_SAFE_FREE(p[i].options.display_name_from);
                        EM_SAFE_FREE(p[i].options.signature);
+                       EM_SAFE_FREE(p[i].options.alert_ringtone_path);
                        EM_SAFE_FREE(p[i].certificate_path);
+                       EM_SAFE_FREE(p[i].user_name);
                }
 
                EM_SAFE_FREE(p);
@@ -4722,7 +5051,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], count[%p], transaction[%d], err_code[%p]", account_id, local_yn, count, transaction, err_code);
 
@@ -4738,7 +5067,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d", account_id, local_yn);
@@ -4758,7 +5087,6 @@ INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int
 FINISH_OFF:
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -4767,7 +5095,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_list(int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_list(char *multi_user_name, int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
 
@@ -4817,7 +5145,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_list(int account_id, int local_yn, email
 
        EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
 
-       if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
+       if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -4833,7 +5161,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
 
@@ -4867,7 +5195,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, in
        EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
        EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
 
-       if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
+       if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -4883,7 +5211,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_child_mailbox_list(char *multi_user_name, int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, parent_mailbox_name, select_num, mailbox_list, transaction, err_code);
 
@@ -4899,9 +5227,9 @@ INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_
        }
 
        sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
-       EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
+       EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
 
-       if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
+       if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -4917,7 +5245,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
        if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
@@ -4936,7 +5264,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int loc
        SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
        EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
 
-       if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
+       if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -4952,13 +5280,12 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
+INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
 
        int      result_code = false;
        int      error = EMAIL_ERROR_NONE;
-       int      rc;
        time_t   current_time = 0;
        char     sql_query_string[QUERY_SIZE] = {0, };
        sqlite3 *local_db_handle = NULL;
@@ -4970,36 +5297,35 @@ INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id
 
        time(&current_time);
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+    EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
-
-       SNPRINTF(sql_query_string, sizeof(sql_query_string),
-               "UPDATE mail_box_tbl SET"
+    SNPRINTF(sql_query_string, sizeof(sql_query_string),
+        "UPDATE mail_box_tbl SET"
                " last_sync_time = %d"
                " WHERE mailbox_id = %d"
                , (int)current_time
                , input_mailbox_id);
 
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
 FINISH_OFF:
 
        if(error == EMAIL_ERROR_NONE)
                result_code = true;
 
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
 
        EM_DEBUG_FUNC_END("error [%d]", error);
        return error;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t** result_mailbox, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **result_mailbox, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, result_mailbox, transaction, err_code);
        EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
@@ -5046,7 +5372,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, ch
 
                EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
 
-               if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
+               if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                        goto FINISH_OFF;
                }
@@ -5064,7 +5390,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], output_mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, output_mailbox, transaction, err_code);
 
@@ -5086,8 +5412,8 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_ma
 
        EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
 
-       if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
+       if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
                goto FINISH_OFF;
        }
 
@@ -5104,7 +5430,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
+INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
 
@@ -5121,7 +5447,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_ma
 
        EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
 
-       if( (ret = emstorage_query_mailbox_tbl(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;
        }
@@ -5132,7 +5458,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], keyword[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, keyword, result_mailbox, transaction, err_code);
 
@@ -5157,7 +5483,7 @@ INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(int account_id, char *keyword
 
        EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
 
-       if ((error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
+       if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -5173,7 +5499,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_id[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_id, transaction, err_code);
        if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
@@ -5189,21 +5515,21 @@ INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        /*  Check whether the account exists. */
-       if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
                goto FINISH_OFF;
        }
 
-       if (account )
+       if (account)
                emstorage_free_account(&account, 1, NULL);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_id  FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
 
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -5228,11 +5554,10 @@ INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -5244,7 +5569,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_name[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_name, transaction, err_code);
        if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
@@ -5260,21 +5585,21 @@ INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, ema
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        /*  Check whether the account exists. */
-       if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
                goto FINISH_OFF;
        }
 
-       if (account )
+       if (account)
                emstorage_free_account(&account, 1, NULL);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_name  FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
 
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -5301,13 +5626,12 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -5316,15 +5640,15 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], local_yn [%d], mailbox_name [%p], modifiable_yn [%d], transaction [%d], err_code [%p]", account_id, local_yn, mailbox_name, modifiable_yn, transaction, err_code);
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
        char *replaced_mailbox_name = NULL;
 
-       if (mailbox_name) {     
+       if (mailbox_name) {
                if (strstr(mailbox_name, "'")) {
                        replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
                } else {
@@ -5332,9 +5656,9 @@ INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int loc
                }
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_box_tbl SET"
@@ -5346,17 +5670,19 @@ INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int loc
                , account_id
                , local_yn
                , replaced_mailbox_name);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
-       EM_SAFE_FREE(replaced_mailbox_name);
+       EM_SAFE_FREE(replaced_mailbox_name);
 
        if (err_code != NULL)
                *err_code = error;
@@ -5366,11 +5692,10 @@ FINISH_OFF:
 
 }
 
-
-INTERNAL_FUNC int emstorage_update_mailbox_total_count(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);
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
@@ -5382,9 +5707,9 @@ INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input
                EM_DEBUG_FUNC_END("ret [%d]", ret);
                return false;
        }
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_box_tbl SET"
@@ -5394,16 +5719,18 @@ INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input
                , total_count_on_server
                , account_id
                , input_mailbox_id);
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -5413,8 +5740,7 @@ FINISH_OFF:
 
 }
 
-
-INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t* result_mailbox, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *result_mailbox, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, result_mailbox, transaction, err_code);
 
@@ -5433,8 +5759,8 @@ INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int inp
        DB_STMT hStmt = NULL;
        int i = 0;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        if (local_yn != -1) {
                SNPRINTF(sql_query_string, sizeof(sql_query_string),
@@ -5496,13 +5822,12 @@ INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int inp
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -5514,8 +5839,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, new_mailbox_type, transaction, err_code);
 
@@ -5531,8 +5855,8 @@ INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, in
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        EM_DEBUG_LOG("emstorage_update_mailbox_type");
 
@@ -5562,14 +5886,14 @@ INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, in
                        , input_mailbox_id);
        }
 
-       EM_DEBUG_LOG("SQL(%s)", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
 
        if(SQLITE_OK != rc) {
                EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
                error = EMAIL_ERROR_DB_FAILURE;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
 
        _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
@@ -5580,15 +5904,15 @@ INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, in
        if(rc == SQLITE_FULL) {
                EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
                error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
-       
+
        if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
                EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
                error = EMAIL_ERROR_DB_FAILURE;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
-       
+
 
        /*  Update mail_tbl */
        i = 0;
@@ -5600,40 +5924,39 @@ INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, in
                        , account_id
                        , input_mailbox_id);
 
-       EM_DEBUG_LOG("SQL[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
        if(SQLITE_OK != rc) {
                EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
                error = EMAIL_ERROR_DB_FAILURE;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
-       
+
        _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
        if(rc == SQLITE_FULL) {
                EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
                error = EMAIL_ERROR_MAIL_MEMORY_FULL;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
-       
+
        if(rc != SQLITE_ROW && rc != SQLITE_DONE){
                EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
                error = EMAIL_ERROR_DB_FAILURE;
-               goto FINISH_OFF; 
+               goto FINISH_OFF;
        }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt_box_tbl != NULL)  {
                rc = sqlite3_finalize(hStmt_box_tbl);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -5641,7 +5964,7 @@ FINISH_OFF:
        if (hStmt_mail_tbl != NULL)  {
                rc = sqlite3_finalize(hStmt_mail_tbl);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -5653,7 +5976,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
+INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", input_mailbox_id, input_is_local_mailbox, transaction);
 
@@ -5666,9 +5989,9 @@ INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        EM_DEBUG_LOG("emstorage_update_mailbox_type");
 
@@ -5682,7 +6005,7 @@ INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is
                " WHERE mailbox_id = %d"
                , input_mailbox_id);
 
-       EM_DEBUG_LOG("SQL(%s)", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
@@ -5695,26 +6018,25 @@ INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is
                ("sqlite3_step fail:%d", rc));
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d", rc));
-
+/*
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
                hStmt = NULL;
        }
-
+*/
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -5723,12 +6045,11 @@ FINISH_OFF:
        return error;
 }
 
-INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
+INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_field_name[%p] input_value[%d] err_code[%p]", input_account_id, input_mailbox_id_array, input_mailbox_id_count, input_field_name, input_value, transaction);
        int i = 0;
        int err = EMAIL_ERROR_NONE;
-       int rc = 0;
        int result = false;
        int cur_mailbox_id_string = 0;
        int mailbox_id_string_buffer_length = 0;
@@ -5742,7 +6063,7 @@ INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_ac
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        /* Generating mail id list string */
        mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
@@ -5775,25 +6096,27 @@ INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_ac
        /* Write query string */
        SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_box_tbl SET %s = %d WHERE mailbox_id in (%s) ", input_field_name, input_value, mailbox_id_string_buffer);
 
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
 
        /* Execute query */
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
+       err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                       goto FINISH_OFF;
+       }
+
        if (sqlite3_changes(local_db_handle) == 0)
                EM_DEBUG_LOG("no mail matched...");
 
        result = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
 
        if (err == EMAIL_ERROR_NONE && parameter_string) {
                if (!emcore_notify_storage_event (NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
-                       EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]", 
+                       EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
                                                                                          input_field_name, input_value);
        }
 
@@ -5804,7 +6127,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
 
@@ -5820,8 +6143,8 @@ INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, in
        char sql_query_string[QUERY_SIZE] = {0,};
        char **result = NULL;
        time_t current_time;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
 
@@ -5892,20 +6215,17 @@ INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, in
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG_DEV ("before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
-       if (error == EMAIL_ERROR_NONE) { 
-               if (!emcore_notify_storage_event (NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, 
+       if (error == EMAIL_ERROR_NONE) {
+               if (!emcore_notify_storage_event (NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
                                                                   mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
                        EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
        }
@@ -5917,7 +6237,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
 
@@ -5932,16 +6252,17 @@ INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int mo
        int rc, ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0,};
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)
@@ -5951,8 +6272,7 @@ INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int mo
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -5962,7 +6282,7 @@ FINISH_OFF:
 
 }
 
-INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, transaction, err_code);
 
@@ -5977,9 +6297,8 @@ INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int inp
        int rc, ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        if (local_yn == -1)
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
@@ -5990,10 +6309,12 @@ INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int inp
                SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(1+ EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
        }
 
-       EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
@@ -6005,8 +6326,7 @@ INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int inp
 
 FINISH_OFF:
 
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if(error == EMAIL_ERROR_NONE) {
                if (!emcore_notify_storage_event (NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
@@ -6057,7 +6377,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , count,  transaction, err_code);
 
@@ -6083,10 +6403,10 @@ INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbo
        EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
 
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'  ", account_id, replaced_mailbox_name);
-       EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
 
        char **result;
 
@@ -6101,7 +6421,6 @@ INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbo
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(replaced_mailbox_name);
 
@@ -6112,9 +6431,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-
-INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], uid[%p], exist[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , uid, exist, transaction, err_code);
 
@@ -6133,7 +6450,7 @@ INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_na
 
        EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        if (mailbox_name)  {
@@ -6167,7 +6484,6 @@ INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_na
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(replaced_mailbox_name);
 
@@ -6178,7 +6494,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
 
@@ -6194,7 +6510,7 @@ INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
@@ -6233,16 +6549,14 @@ INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_
 
 FINISH_OFF:
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -6251,7 +6565,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t** read_mail_uid, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code)
 {
        EM_PROFILE_BEGIN(emStorageGetDownloadList);
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, read_mail_uid, count, transaction, err_code);
@@ -6272,7 +6586,7 @@ INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id,
        emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
        int i = 0;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        if (mailbox_id)
@@ -6280,7 +6594,7 @@ INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id,
        else
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
 
-       EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
+       EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
 
 
 
@@ -6340,17 +6654,14 @@ FINISH_OFF:
                emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -6360,7 +6671,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size, transaction, err_code);
 
@@ -6378,7 +6689,7 @@ INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *mailb
        char sql_query_string[QUERY_SIZE] = {0, };
        char *replaced_mailbox_name = NULL;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        if (mailbox_name) {
@@ -6435,14 +6746,12 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -6451,7 +6760,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
 
@@ -6467,8 +6776,8 @@ INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* r
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
        char **result = NULL;
@@ -6505,13 +6814,10 @@ INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* r
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
-       EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
-       EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
-       EM_DEBUG_LOG_SEC("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
-       EM_DEBUG_LOG("server_uid VALUE [%s] ", read_mail_uid->server_uid);
-       EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->rfc822_size);
-       EM_DEBUG_LOG("rc VALUE [%d] ", rc);
+       EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
+                   "server_uid[%s] rfc822_size[%d] rc[%d]",
+         read_mail_uid->account_id, read_mail_uid->mailbox_id,read_mail_uid->local_uid,
+         read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
 
        _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
        _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
@@ -6534,14 +6840,11 @@ INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* r
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -6554,7 +6857,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
 
@@ -6570,8 +6873,8 @@ INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text,
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
        char **result = NULL;
@@ -6597,10 +6900,9 @@ INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text,
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("mail_id VALUE [%d] ", mail_text->mail_id);
-       EM_DEBUG_LOG("account_id VALUE [%d] ", mail_text->account_id);
-       EM_DEBUG_LOG("mailbox_id VALUE [%d] ", mail_text->mailbox_id);
-       EM_DEBUG_LOG("body_text VALUE [%s] ", mail_text->body_text);
+       EM_DEBUG_LOG ("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
+                                       mail_text->account_id, mail_text->mailbox_id);
+       EM_DEBUG_LOG_DEV ("body_text VALUE [%s] ", mail_text->body_text);
 
        _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
        _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
@@ -6616,14 +6918,11 @@ INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text,
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL) {
-               EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -6636,7 +6935,7 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emstorage_change_read_mail_uid(int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid, transaction, err_code);
 
@@ -6653,9 +6952,9 @@ INTERNAL_FUNC int emstorage_change_read_mail_uid(int account_id, int mailbox_id,
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_read_mail_uid_tbl SET"
@@ -6704,15 +7003,11 @@ INTERNAL_FUNC int emstorage_change_read_mail_uid(int account_id, int mailbox_id,
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -6724,7 +7019,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
 
@@ -6736,14 +7031,13 @@ INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox
                return false;
        }
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
        char *replaced_mailbox_name = NULL;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
 
@@ -6761,16 +7055,16 @@ INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox
                SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND server_uid='%s' ", uid);
        }
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        EM_SAFE_FREE(replaced_mailbox_name);
 
@@ -6817,11 +7111,11 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
 
-       if (!count) {           
+       if (!count) {
                EM_DEBUG_EXCEPTION("count[%p]", count);
 
                if (err_code != NULL)
@@ -6833,10 +7127,10 @@ INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *co
        int error =  EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
-       if (account_id != ALL_ACCOUNT) 
+       if (account_id != ALL_ACCOUNT)
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
        else
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
@@ -6854,7 +7148,6 @@ INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *co
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -6863,7 +7156,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_rule(int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p], transaction[%d], err_code[%p]", account_id, type, start_idx, select_num, is_completed, rule_list, transaction, err_code);
 
@@ -6885,7 +7178,7 @@ INTERNAL_FUNC int emstorage_get_rule(int account_id, int type, int start_idx, in
 
        int rc;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        if (account_id != ALL_ACCOUNT) {
@@ -6966,18 +7259,14 @@ FINISH_OFF:
                emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
 
        if (hStmt != NULL)  {
-/*             EM_DEBUG_LOG("  sqlite3_finalize hStmt = %p", hStmt); */
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
-
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -6986,7 +7275,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
 
@@ -7004,7 +7293,7 @@ INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** r
 
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
@@ -7049,18 +7338,14 @@ FINISH_OFF:
                *rule = p_data_tbl;
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -7069,7 +7354,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
 
@@ -7086,12 +7371,12 @@ INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_r
 
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_rule_tbl SET"
-               "  filter_name = ?"                
+               "  filter_name = ?"
                ", type = ?"
                ", value = ?"
                ", value2 = ?"
@@ -7099,8 +7384,10 @@ INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_r
                ", target_mailbox_id = ?"
                ", flag1 = ?"
                ", flag2 = ?"
-               " WHERE account_id = ?"
-               " AND rule_id = ?");
+               ", account_id = ?"
+               ", rule_id = ?"
+                " WHERE rule_id = %d"
+                , rule_id);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -7129,15 +7416,11 @@ INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_r
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -7149,7 +7432,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
 
@@ -7166,7 +7449,7 @@ INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transactio
        int rc = 0;
        int ret = false;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        switch(rule->action_type) {
@@ -7220,17 +7503,14 @@ INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transactio
 FINISH_OFF:
 
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code)
                *err_code = error;
@@ -7239,7 +7519,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
 
@@ -7255,10 +7535,9 @@ INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        char *sql;
        char **result;
@@ -7315,15 +7594,11 @@ INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -7335,7 +7610,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
 
@@ -7350,15 +7625,16 @@ INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_c
        int rc, ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0) {
                EM_DEBUG_EXCEPTION(" no matched rule found...");
@@ -7370,8 +7646,7 @@ INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_c
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -7413,7 +7688,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, total, unseen, transaction, err_code);
 
@@ -7428,11 +7703,10 @@ INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *
        int error = EMAIL_ERROR_NONE;
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-       char err_msg[1024];
        char *replaced_mailbox_name = NULL;
 
        memset(&sql_query_string, 0x00, sizeof(sql_query_string));
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        if (total)  {
@@ -7498,14 +7772,13 @@ FINISH_OFF2:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 #endif
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(replaced_mailbox_name);
 
@@ -7516,7 +7789,7 @@ FINISH_OFF2:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
 
@@ -7544,7 +7817,7 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorag
        int rc, ret = false;
        int error = EMAIL_ERROR_NONE;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        switch (type)  {
@@ -7574,7 +7847,7 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorag
                        goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
@@ -7636,18 +7909,15 @@ FINISH_OFF:
                emstorage_free_mail(&p_data_tbl,  1, NULL);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -7656,7 +7926,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_ids, number_of_mails, type, mail, transaction, err_code);
 
@@ -7692,15 +7962,15 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], i
                return false;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        switch (type) {
                case RETRIEVE_SUMMARY:
                        cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
-                               "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id in (");
-                       field_count = 12;
+                               "SELECT account_id, mail_id, mailbox_id, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, subject, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id in (");
+                       field_count = 14;
                        break;
 
                case RETRIEVE_FIELDS_FOR_DELETE:
@@ -7717,8 +7987,8 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], i
 
                case RETRIEVE_FLAG:
                        cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
-                               "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
-                       field_count = 4;
+                               "SELECT account_id, mail_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
+                       field_count = 5;
                        break;
 
                default :
@@ -7728,10 +7998,10 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], i
        }
 
        for(i = 0; i < number_of_mails; i++)
-               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
+               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
        sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
 
-       EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
+       EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
        if (SQLITE_OK != rc && -1 != rc) {
@@ -7761,11 +8031,13 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], i
                        case RETRIEVE_SUMMARY:
                                _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
                                _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
+                               _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
                                _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
                                _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
                                _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
                                _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
                                _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
+                               _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
                                _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
                                _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
                                _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
@@ -7787,6 +8059,7 @@ INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], i
 
                        case RETRIEVE_FLAG:
                                _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
+                               _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
                                _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
                                _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
                                _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
@@ -7806,7 +8079,6 @@ FINISH_OFF:
                sqlite3_free_table(result);
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(sql_query_string);
 
@@ -7817,7 +8089,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
 
@@ -7833,9 +8105,9 @@ INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** m
        emstorage_mail_tbl_t* p_data_tbl = NULL;
 
        SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
-       EM_DEBUG_LOG("query = [%s]", conditional_clause);
+       EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
 
-       if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
+       if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
                goto FINISH_OFF;
        }
@@ -7856,7 +8128,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
 
@@ -7874,9 +8146,9 @@ INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text
        emstorage_mail_text_tbl_t *p_data_tbl = NULL;
 
        SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
-       EM_DEBUG_LOG("query = [%s]", conditional_clause);
+       EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
 
-       if(!emstorage_query_mail_text_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
+       if(!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
                goto FINISH_OFF;
        }
@@ -7897,9 +8169,9 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search, int account_id, char *mailbox_name, int sorting, int *search_handle, int *searched, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name, emstorage_search_filter_t *search, int account_id, int mailbox_id, int sorting, int *search_handle, int *searched, int transaction, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_name[%p], sorting[%d], search_handle[%p], searched[%p], transaction[%d], err_code[%p]", search, account_id, mailbox_name, sorting, search_handle, searched, transaction, err_code);
+       EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], search_handle[%p], searched[%p], transaction[%d], err_code[%p]", search, account_id, mailbox_id, sorting, search_handle, searched, transaction, err_code);
 
        if (!search_handle || !searched)  {
                if (err_code != NULL)
@@ -7915,9 +8187,8 @@ INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search,
        char sql_query_string[QUERY_SIZE] = {0, };
        int rc, ret = false;
        int and = false, mail_count = 0;
-       char *replaced_mailbox_name = NULL;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
@@ -7927,14 +8198,8 @@ INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search,
                and = true;
        }
 
-       if (mailbox_name)  {
-               if (strstr(mailbox_name, "'")) {
-                       replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
-               } else {
-                       replaced_mailbox_name = strdup(mailbox_name);
-               }
-
-               SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", and ? "AND" : "WHERE", replaced_mailbox_name);
+       if (mailbox_id)  {
+               SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = %d", and ? "AND" : "WHERE", mailbox_id);
                and = true;
        }
 
@@ -7964,7 +8229,7 @@ INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search,
        if (sorting)
                SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " ORDER BY date_time");
 
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -7996,17 +8261,14 @@ FINISH_OFF:
                if (hStmt != NULL)  {
                        rc = sqlite3_finalize(hStmt);
                        if (rc != SQLITE_OK)  {
-                               EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                               EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                                error = EMAIL_ERROR_DB_FAILURE;
                        }
                }
 
                EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-               _DISCONNECT_DB;
        }
 
-       EM_SAFE_FREE(replaced_mailbox_name);
-
        if (err_code != NULL)
                *err_code = error;
 
@@ -8088,6 +8350,7 @@ INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail
                                _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
                                _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
                                _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
+                               _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
                        }
 
                        if (p_data_tbl->body_download_status)  {
@@ -8176,11 +8439,10 @@ INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction,
 
        DB_STMT hStmt = (DB_STMT)search_handle;
 
-       EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
 
        rc = sqlite3_finalize(hStmt);
        if (rc != SQLITE_OK)  {
-               EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+               EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                error = EMAIL_ERROR_DB_FAILURE;
        }
 
@@ -8188,7 +8450,6 @@ INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction,
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -8197,7 +8458,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
 
@@ -8215,10 +8476,10 @@ INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail,
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        int i = 0;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        char mailbox_id_param_string[10] = {0,};
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_tbl SET"
@@ -8261,6 +8522,7 @@ INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail,
                ", tag_id = ?"
                ", replied_time = ?"
                ", forwarded_time = ?"
+               ", default_charset = ?"
                ", eas_data_length = ?"
                ", eas_data = ?"
                " WHERE mail_id = %d AND account_id != 0 "
@@ -8310,6 +8572,7 @@ INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail,
        _bind_stmt_field_data_int   (hStmt, i++, mail->tag_id);
        _bind_stmt_field_data_int   (hStmt, i++, mail->replied_time);
        _bind_stmt_field_data_int   (hStmt, i++, mail->forwarded_time);
+       _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
        _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
 
@@ -8327,13 +8590,11 @@ INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail,
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -8357,7 +8618,7 @@ FINISH_OFF:
   *
   *
   **/
-INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
+INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
 
@@ -8367,15 +8628,17 @@ INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
        int error = EMAIL_ERROR_NONE;
        int rc = 0;
        char sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE save_status = %d", save_status, EMAIL_MAIL_STATUS_SENDING);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0) {
@@ -8394,19 +8657,20 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mail_ids[%p], mail_ids_count[%d], field_name[%s], value[%d], transaction[%d], err_code[%p]", account_id, mail_ids, mail_ids_count, field_name, value, transaction, err_code);
        int i = 0;
        int error = EMAIL_ERROR_NONE;
-       int rc = 0;
        int ret = false;
+       int query_size = 0;
        int cur_mail_id_string = 0;
        int mail_id_string_buffer_length = 0;
-       char  sql_query_string[QUERY_SIZE] = {0, };
+       int parameter_string_length = 0;
+       char  *sql_query_string = NULL;
        char *mail_id_string_buffer = NULL;
        char *parameter_string = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        email_mail_attribute_type target_mail_attribute_type = 0;
 
        if (!mail_ids  || !field_name || account_id == 0) {
@@ -8442,7 +8706,8 @@ INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id
                mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
 
        /* Generating notification parameter string */
-       parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
+       parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
+       parameter_string = em_malloc(parameter_string_length);
 
        if(!parameter_string) {
                EM_DEBUG_EXCEPTION("em_malloc failed");
@@ -8452,26 +8717,35 @@ INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id
                return false;
        }
 
-       SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
+       SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
+       query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
 
+       sql_query_string = em_malloc(query_size);
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
        /* Write query string */
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET %s = %d WHERE mail_id in (%s) AND account_id = %d", field_name, value, mail_id_string_buffer, account_id);
+       SNPRINTF(sql_query_string, query_size, "UPDATE mail_tbl SET %s = %d WHERE mail_id in (%s) AND account_id = %d", field_name, value, mail_id_string_buffer, account_id);
 
        EM_DEBUG_LOG_DEV ("sql_query_string [%s]", sql_query_string);
 
        /* Execute query */
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("sqlite3_exec failed [%d] [%s]", rc, sql_query_string));
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
        if (sqlite3_changes(local_db_handle) == 0)
                EM_DEBUG_LOG("no mail matched...");
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (error == EMAIL_ERROR_NONE && parameter_string) {
                if (!emcore_notify_storage_event (NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
@@ -8480,6 +8754,8 @@ FINISH_OFF:
 
        EM_SAFE_FREE(mail_id_string_buffer);
        EM_SAFE_FREE(parameter_string);
+       EM_SAFE_FREE(sql_query_string);
+
 
        if (err_code != NULL)
                *err_code = error;
@@ -8489,7 +8765,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_BODY_SEARCH__
-INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
 
@@ -8508,15 +8784,15 @@ INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_t
        int i = 0;
        int rc = 0;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_text_tbl SET"
                " body_text = ?"
                " WHERE mail_id = %d AND account_id != 0"
                , mail_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
@@ -8541,13 +8817,12 @@ INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_t
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL) {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
                hStmt = NULL;
@@ -8561,7 +8836,7 @@ FINISH_OFF:
 }
 #endif
 
-INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
 
@@ -8583,9 +8858,9 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                return false;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        switch (type) {
                case APPEND_BODY:
@@ -8640,7 +8915,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                                int err;
                                emstorage_mailbox_tbl_t *mailbox_tbl;
 
-                               if ((err = emstorage_get_mailbox_by_id(mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
+                               if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
                                        goto FINISH_OFF;
                                }
@@ -8687,7 +8962,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                                , mail->flags_draft_field
                                , mail->flags_forwarded_field
                                , mail_id);
-                       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+                       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
                        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -8711,7 +8986,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                                , mail->report_status
                                , mail->DRM_status
                                , mail_id);
-                       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+                       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
                        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -8726,7 +9001,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                                "  WHERE mail_id = %d AND account_id != 0"
                                , mail->lock_status
                                , mail_id);
-                       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+                       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
                        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -8774,6 +9049,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                                ", tag_id = ?"
                                ", replied_time = ?"
                                ", forwarded_time = ?"
+                               ", default_charset = ?"
                                ", eas_data_length = ?"
                                ", eas_data = ?"
                                " WHERE mail_id = %d AND account_id != 0"
@@ -8822,6 +9098,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                        _bind_stmt_field_data_int   (hStmt, i++, mail->tag_id);
                        _bind_stmt_field_data_int   (hStmt, i++, mail->replied_time);
                        _bind_stmt_field_data_int   (hStmt, i++, mail->forwarded_time);
+                       _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
                        _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
                        _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
                        break;
@@ -8906,7 +9183,9 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                        ", file_path_html = ?"
                        ", attachment_count = ?"
                        ", inline_content_count = ?"
-                       ", preview_text= ?"
+                       ", preview_text = ?"
+                       ", digest_type = ?"
+                        ", smime_type = ?"
                        " WHERE mail_id = %d"
                        , mail_id);
 
@@ -8922,6 +9201,8 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
                        _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
                        _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
                        _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
+                       _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
+                       _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
 
                        break;
 
@@ -8970,7 +9251,7 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
 
        if (mail->account_id == 0) {
                emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
-               if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
+               if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
                        EM_DEBUG_EXCEPTION ("emstorage_get_mail_field_by_id error [%d]", error);
                        goto FINISH_OFF;
                }
@@ -8982,19 +9263,18 @@ INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_typ
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
                hStmt = NULL;
        }
        if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
-               if (!emstorage_get_mailbox_id_by_mailbox_type( mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
+               if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
 
                if (mail->mailbox_id == mailbox_id) {
@@ -9015,7 +9295,8 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
-INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
+
+INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
 
@@ -9027,14 +9308,14 @@ INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int
        char **result = NULL;
 
 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
-       _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
+       _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
 
        ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
        if (ret < 0 || latest_mail_id == 0) {
                EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
 
-               local_db_handle = emstorage_get_db_connection();
+        local_db_handle = emstorage_get_db_connection(multi_user_name);
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
                EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -9056,13 +9337,12 @@ INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int
                *mail_id = latest_mail_id;
 
 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
-       _unlockshm_mutex(&mapped_for_generating_mail_id);
+       _unlockshm_mutex(mapped_for_generating_mail_id);
 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
 
        ret = true;
 
 FINISH_OFF:
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -9071,8 +9351,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
 {
        EM_PROFILE_BEGIN(profile_emstorage_add_mail);
        EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
@@ -9088,9 +9367,10 @@ INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int ge
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
        DB_STMT hStmt = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        if (get_id)  {
                /*  increase unique id */
@@ -9098,8 +9378,14 @@ INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int ge
                char **result;
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
-                       ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
+/*             EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+                       ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
+                       error = EMAIL_ERROR_DB_FAILURE;
+                       sqlite3_free_table(result);
+                       goto FINISH_OFF;
+               }
 
                if (NULL == result[1])
                        rc = 1;
@@ -9122,129 +9408,169 @@ INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int ge
                ", ?" /*  mailbox_id */
                ", ?" /*  mailbox_type */
                ", ?" /*  subject */
+
                ", ?" /*  date_time */
                ", ?" /*  server_mail_status */
                ", ?" /*  server_mailbox_name */
                ", ?" /*  server_mail_id */
                ", ?" /*  message_id */
+
                ", ?" /*  reference_mail_id */
                ", ?" /*  full_address_from */
                ", ?" /*  full_address_reply */
                ", ?" /*  full_address_to */
                ", ?" /*  full_address_cc */
+
                ", ?" /*  full_address_bcc */
                ", ?" /*  full_address_return */
                ", ?" /*  email_address_sender */
                ", ?" /*  email_address_recipient */
                ", ?" /*  alias_sender */
+
                ", ?" /*  alias_recipient */
                ", ?" /*  body_download_status */
                ", ?" /*  file_path_plain */
                ", ?" /*  file_path_html */
                ", ?" /*  file_path_mime_entity */
+
                ", ?" /*  mail_size */
                ", ?" /*  flags_seen_field */
                ", ?" /*  flags_deleted_field */
                ", ?" /*  flags_flagged_field */
                ", ?" /*  flags_answered_field */
+
                ", ?" /*  flags_recent_field */
                ", ?" /*  flags_draft_field */
                ", ?" /*  flags_forwarded_field */
                ", ?" /*  DRM_status */
                ", ?" /*  priority */
+
                ", ?" /*  save_status */
                ", ?" /*  lock_status */
                ", ?" /*  report_status */
                ", ?" /*  attachment_count */
                ", ?" /*  inline_content_count */
+
                ", ?" /*  thread_id */
                ", ?" /*  thread_item_count */
                ", ?" /*  preview_text */
                ", ?" /*  meeting_request_status */
                ", ?" /*  message_class */
+
                ", ?" /*  digest_type */
                ", ?" /*  smime_type */
                ", ?" /*  scheduled_sending_time */
                ", ?" /*  remaining_resend_times */
                ", ?" /*  tag_id */
+
                ", ?" /*  replied_time */
                ", ?" /*  forwarded_time */
+               ", ?" /*  default charset */
                ", ?" /*  eas_data_length */
                ", ?" /*  eas_data */
+               ", ?" /*  user_name */
                ")");
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
-       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
+                                                   sql_query_string,
+                                                   EM_SAFE_STRLEN(sql_query_string),
+                                                   &hStmt,
+                                                   NULL),
+                                    rc);
+       if (rc != SQLITE_OK) {
+               EM_DEBUG_EXCEPTION ("sqlite3_prepare error [%d] [%s] SQL(%s) ", 
+                             rc, sql_query_string, sqlite3_errmsg(local_db_handle));
+               error = EMAIL_ERROR_DB_FAILURE;
+               goto FINISH_OFF;
+       }
 
        _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
        _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
        _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
        _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
        _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
+
        _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
        _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
        _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
+
        _bind_stmt_field_data_int   (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
+
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_sender, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
+
        _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_recipient, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
        _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_string(hStmt, FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
+
        _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
        _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
        _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
        _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
        _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
+
        _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
        _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
        _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
        _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
        _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
+
        _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
        _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
        _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
        _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
        _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
+
        _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
        _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
        _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
        _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
+
        _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
        _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
        _bind_stmt_field_data_int   (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
        _bind_stmt_field_data_int   (hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
        _bind_stmt_field_data_int   (hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
+
        _bind_stmt_field_data_int   (hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
        _bind_stmt_field_data_int   (hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
+       _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_int   (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
        _bind_stmt_field_data_blob  (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
+       _bind_stmt_field_data_string(hStmt, USER_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->user_name, 0, TEXT_2_LEN_IN_MAIL_TBL);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
-       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
+       if (rc == SQLITE_FULL) {
+               EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
+               error = EMAIL_ERROR_MAIL_MEMORY_FULL;
+               goto FINISH_OFF;
+       }
+       if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
+               EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
+               error = EMAIL_ERROR_DB_FAILURE;
+               goto FINISH_OFF;
+       }
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -9257,14 +9583,15 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_source_account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", input_source_account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
 
-       int rc, ret = false, i, cur_conditional_clause = 0;
+       int ret = false, i, cur_conditional_clause = 0;
        int error = EMAIL_ERROR_NONE;
        int target_account_id;
-       char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
+       int conditional_clause_len = 0;
+       char *sql_query_string = NULL, *conditional_clause = NULL;
        emstorage_mailbox_tbl_t *result_mailbox = NULL;
        email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
        char* target_mailbox_name = NULL;
@@ -9276,9 +9603,9 @@ INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int input_source_account_i
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
+       if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
                if (err_code != NULL)
                        *err_code = error;
@@ -9297,82 +9624,101 @@ INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int input_source_account_i
        target_account_id   = result_mailbox->account_id;
        emstorage_free_mailbox(&result_mailbox, 1, NULL);
 
-       cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
+       conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
+       conditional_clause = em_malloc(conditional_clause_len);
+       if (conditional_clause == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+       cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
 
        for(i = 0; i < number_of_mails; i++)
-               cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
+               cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
 
        /* prevent 34415 */
        char *last_comma = rindex(conditional_clause, ',');
        if(last_comma) *last_comma = ')'; /* replace , with ) */
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        /* Updating a mail_tbl */
 
-       memset(sql_query_string, 0x00, QUERY_SIZE);
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       sql_query_string = em_malloc(conditional_clause_len);
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+       SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        /* Updating a mail_attachment_tbl */
-       memset(sql_query_string, 0x00, QUERY_SIZE);
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       memset(sql_query_string, 0x00, conditional_clause_len);
+       SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        /* Updating a mail_meeting_tbl */
-       memset(sql_query_string, 0x00, QUERY_SIZE);
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       memset(sql_query_string, 0x00, conditional_clause_len);
+       SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
 #ifdef __FEATURE_BODY_SEARCH__
        /* Updating mail_text_tbl */
-       memset(sql_query_string, 0x00, QUERY_SIZE);
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       memset(sql_query_string, 0x00, conditional_clause_len);
+       SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 #endif
 
        /* Updating a mail_read_mail_uid_tbl */
-       memset(conditional_clause, 0x00, QUERY_SIZE);
-       cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
+       memset(conditional_clause, 0x00, conditional_clause_len);
+       cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
 
        for(i = 0; i < number_of_mails; i++)
-               cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
+               cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
 
        /* prevent 34415 */
        last_comma = rindex(conditional_clause, ',');
        if(last_comma) *last_comma = ')'; /* replace , with ) */
 
-       memset(sql_query_string, 0x00, QUERY_SIZE);
-       SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       memset(sql_query_string, 0x00, conditional_clause_len);
+       SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        EM_SAFE_FREE(target_mailbox_name);
+       EM_SAFE_FREE(conditional_clause);
+       EM_SAFE_FREE(sql_query_string);
 
        if (err_code != NULL)
                *err_code = error;
@@ -9381,7 +9727,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
 
@@ -9392,28 +9738,26 @@ INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transa
                return false;
        }
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -9422,13 +9766,14 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
 
-       int rc, ret = false, i, cur_sql_query_string = 0;
+       int ret = false, i, cur_sql_query_string = 0;
        int error = EMAIL_ERROR_NONE;
-       char sql_query_string[QUERY_SIZE] = {0, };
+       int query_size = 0;
+       char *sql_query_string = NULL;
 
        if (!mail_ids) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -9437,47 +9782,57 @@ INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       query_size = (sizeof(char) * 8 * number_of_mails) + 512;
+       sql_query_string =  em_malloc(query_size);
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
 
-       cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
+       cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
 
        for(i = 0; i < number_of_mails; i++)
-               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
+               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
 
        /* prevent 34414 */
        char *last_comma = rindex(sql_query_string, ',');
        *last_comma = ')'; /* replace , with ) */
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
 #ifdef __FEATURE_BODY_SEARCH__
        /* delete mail_text from mail_text_tbl */
-       cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE mail_id in (");
+       cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
 
        for(i = 0; i < number_of_mails; i++)
-               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
+               cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
 
        last_comma = rindex(sql_query_string, ',');
        *last_comma = ')'; /* replace , with ) */
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 #endif
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
+       EM_SAFE_FREE(sql_query_string);
        if (err_code != NULL)
                *err_code = error;
 
@@ -9485,7 +9840,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
 
@@ -9501,15 +9856,16 @@ INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transacti
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
@@ -9519,11 +9875,12 @@ INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transacti
 
        /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
@@ -9534,11 +9891,12 @@ INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transacti
 #ifdef __FEATURE_BODY_SEARCH__
        /* Delete all mail_text in mail_text_tbl table based on account id */
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0) {
@@ -9550,8 +9908,7 @@ INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transacti
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (error == EMAIL_ERROR_NONE) {
                if (!emcore_notify_storage_event (NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
@@ -9565,58 +9922,76 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
+       EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
 
-       if (account_id < FIRST_ACCOUNT_ID || mailbox_id == 0)  {
-               EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d]", account_id, mailbox_id);
+       if (mailbox == NULL)  {
+               EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_type = %d", mailbox->account_id, mailbox->mailbox_type);
+               EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
-
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
-
-       /* Delete Mails from mail_read_mail_uid_tbl */
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+               /* Delete Mails from mail_read_mail_uid_tbl */
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", mailbox->account_id, mailbox->mailbox_name);
+               EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
+       } else {
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
+               EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               /* Delete Mails from mail_read_mail_uid_tbl */
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
+               EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 
 #ifdef __FEATURE_BODY_SEARCH__
-       /* Delete Mails from mail_text_tbl */
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               /* Delete Mails from mail_text_tbl */
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
+               EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+               }
 #endif
-
+       }
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (error == EMAIL_ERROR_NONE) {
-               if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, mailbox_id , 0, 0))
+               if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
                        EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
        }
 
@@ -9662,7 +10037,10 @@ INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int coun
                        EM_SAFE_FREE(p->preview_text);
                        EM_SAFE_FREE(p->alias_sender);
                        EM_SAFE_FREE(p->alias_recipient);
+                       EM_SAFE_FREE(p->default_charset);
+                       EM_SAFE_FREE(p->pgp_password);
                        EM_SAFE_FREE(p->eas_data);
+                       EM_SAFE_FREE(p->user_name);
                }
                EM_SAFE_FREE(*mail_list);
        }
@@ -9698,8 +10076,7 @@ INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_tex
 }
 #endif
 
-
-INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
 
@@ -9714,7 +10091,7 @@ INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int tr
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
@@ -9732,7 +10109,6 @@ INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int tr
 
 FINISH_OFF:
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -9741,7 +10117,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
+INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, input_transaction, output_attachment_list, output_attachment_count);
 
@@ -9757,7 +10133,7 @@ INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_tra
        char                        sql_query_string[QUERY_SIZE] = {0, };
        emstorage_attachment_tbl_t* p_data_tbl = NULL;
        DB_STMT hStmt = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(input_transaction);
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
@@ -9782,7 +10158,7 @@ INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_tra
        }
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
-       EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
@@ -9799,6 +10175,7 @@ INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_tra
                _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
                _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
+               _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
                _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
                _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
                _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
@@ -9824,19 +10201,20 @@ FINISH_OFF:
        if (hStmt) {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
+                       if (*output_attachment_list)
+                               emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
-       _DISCONNECT_DB;
 
        EM_DEBUG_FUNC_END("error [%d]", error);
        return error;
 }
 
-INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
 
@@ -9852,7 +10230,7 @@ INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachme
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
@@ -9885,6 +10263,7 @@ INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachme
        _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
        _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
        _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
        _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
        _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
        _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
@@ -9907,8 +10286,6 @@ FINISH_OFF:
                *attachment = p_data_tbl;
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG_DEV ("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
                        EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
@@ -9917,7 +10294,6 @@ FINISH_OFF:
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -9926,7 +10302,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], attachment_tbl[%p], transaction[%d], err_code[%p]", mail_id, nth, attachment_tbl, transaction, err_code);
 
@@ -9944,11 +10320,11 @@ INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_a
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id LIMIT %d, 1", mail_id, (nth - 1));
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
        DB_STMT hStmt = NULL;
 
@@ -9962,7 +10338,7 @@ INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_a
                ("sqlite3_step fail:%d", rc));
 
        if (rc == SQLITE_DONE)  {
-               EM_DEBUG_EXCEPTION(" no matched attachment found...");
+               EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
                error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
                goto FINISH_OFF;
        }
@@ -9978,6 +10354,8 @@ INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_a
                p_data_tbl->attachment_name = cpy_str(p);
        if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
                p_data_tbl->attachment_path = cpy_str(p);
+       if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
+               p_data_tbl->content_id = cpy_str(p);
        p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
        p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
        p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
@@ -10000,17 +10378,102 @@ FINISH_OFF:
                *attachment_tbl = p_data_tbl;
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
+               rc = sqlite3_finalize(hStmt);
+               if (rc != SQLITE_OK)  {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
+
+       if (attachment_path == NULL || !attachment)  {
+               EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
 
+       emstorage_attachment_tbl_t* p_data_tbl = NULL;
+       int rc, ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
+
+       sqlite3_stmt* hStmt = NULL;
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+       EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
+
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
+
+       if (rc == SQLITE_DONE)  {
+               EM_DEBUG_LOG ("no matched attachment found...");
+               error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
+               goto FINISH_OFF;
+       }
+
+       if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
+               EM_DEBUG_EXCEPTION("malloc failed...");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
+       _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
+
+#ifdef __ATTACHMENT_OPTI__
+               _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
+               _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
+#endif
+
+       ret = true;
+
+FINISH_OFF:
+       if (ret == true)
+               *attachment = p_data_tbl;
+
+       if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -10019,7 +10482,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t* attachment, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
 
@@ -10037,9 +10500,9 @@ INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_chan
 
        int i = 0;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        switch (type)  {
                case UPDATE_MAILBOX:
@@ -10071,7 +10534,7 @@ INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_chan
                        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                                "UPDATE mail_attachment_tbl SET"
                                "  attachment_size = ?"
-                               ", attachment_save_status = 1"
+                               ", attachment_save_status = ?"
                                ", attachment_path = ?"
                                " WHERE mail_id = %d"
                                " AND attachment_id = %d"
@@ -10085,6 +10548,7 @@ INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_chan
                        ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
                        _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
+                       _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
                        _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
                        break;
 
@@ -10093,7 +10557,7 @@ INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_chan
                        error = EMAIL_ERROR_INVALID_PARAM;
                        goto FINISH_OFF;
        }
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
@@ -10101,26 +10565,24 @@ INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_chan
        ret = true;
 
 FINISH_OFF:
+
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
-
        if (err_code != NULL)
                *err_code = error;
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
+INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_transaction);
 
@@ -10153,9 +10615,9 @@ INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new
                replaced_alias = strdup(input_new_mailbox_alias);
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
+       if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
                EM_SAFE_FREE (replaced_mailbox_name);
                EM_SAFE_FREE (replaced_alias);
@@ -10163,7 +10625,7 @@ INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new
        }
        account_id = old_mailbox_data->account_id;
 
-       EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
 
        if(input_eas_data && input_eas_data_length > 0) {
                SNPRINTF(sql_query_string, sizeof(sql_query_string),
@@ -10215,8 +10677,7 @@ INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
        if (error == EMAIL_ERROR_NONE) {
                if (!emcore_notify_storage_event (NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
                        EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
@@ -10235,7 +10696,7 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -10244,7 +10705,7 @@ FINISH_OFF:
        return error;
 }
 
-INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
+INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
        int rc, ret = false;
@@ -10261,7 +10722,7 @@ INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_c
 
        *attachment_no = -1;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
@@ -10280,7 +10741,6 @@ INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_c
        ret = true;
 
 FINISH_OFF:
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -10289,7 +10749,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
 
@@ -10297,9 +10757,10 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
        char **result;
        int rc, ret = false;
        int error = EMAIL_ERROR_NONE;
-       DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       DB_STMT hStmt = NULL;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        if (!attachment_tbl)  {
                EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
@@ -10308,7 +10769,7 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
                return false;
        }
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
 
@@ -10327,6 +10788,7 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
                "( ?"   /* attachment_id */
                ", ?"   /* attachment_name */
                ", ?"   /* attachment_path */
+               ", ?"   /* content_id */
                ", ?"   /* attachment_size */
                ", ?"   /* mail_id */
                ", ?"   /* account_id */
@@ -10350,6 +10812,7 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
        _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
        _bind_stmt_field_data_string(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
        _bind_stmt_field_data_string(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
+       _bind_stmt_field_data_string(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
        _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
        _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
        _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
@@ -10370,17 +10833,7 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
                ("sqlite3_step fail:%d", rc));
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d", rc));
-/*
-       SNPRINTF(sql_query_string, sizeof(sql_query_string),
-               "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
 
-       EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-*/
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
                EM_DEBUG_LOG(" no matched mail found...");
@@ -10391,12 +10844,12 @@ INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachmen
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -10408,7 +10861,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
 
@@ -10419,19 +10872,20 @@ INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attach
 
        if (!attachment_tbl)  {
                EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
-               if (err_code) 
+               if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_attachment_tbl SET  "
                "  attachment_name = ?"
                ", attachment_path =  ?"
+               ", content_id = ?"
                ", attachment_size = ?"
                ", mail_id = ?"
                ", account_id = ?"
@@ -10451,6 +10905,7 @@ INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attach
 
        _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
        _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
+       _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
        _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
        _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
        _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
@@ -10468,17 +10923,7 @@ INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attach
                ("sqlite3_step fail:%d", rc));
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d", rc));
-/*
-       SNPRINTF(sql_query_string, sizeof(sql_query_string),
-               "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-
-       EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-*/
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
                EM_DEBUG_LOG(" no matched mail found...");
@@ -10489,12 +10934,11 @@ INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attach
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -10506,7 +10950,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
 
@@ -10517,25 +10961,25 @@ INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int trans
                return false;
        }
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code)
                *err_code = error;
@@ -10544,10 +10988,10 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
        sqlite3 *local_db_handle = NULL;
@@ -10559,21 +11003,21 @@ INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int tran
                return false;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code)
                *err_code = error;
@@ -10582,18 +11026,17 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
 
        int error = EMAIL_ERROR_NONE;
-       int rc, ret = false;
+       int ret = false;
        char sql_query_string[QUERY_SIZE] = {0, };
        char *replaced_mailbox = NULL;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
 
@@ -10610,15 +11053,17 @@ INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *ma
                SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", replaced_mailbox);
                EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
        }
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -10645,6 +11090,7 @@ INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachm
                for (i = 0; i < count; i++)  {
                        EM_SAFE_FREE(p[i].attachment_name);
                        EM_SAFE_FREE(p[i].attachment_path);
+                       EM_SAFE_FREE(p[i].content_id);
                        EM_SAFE_FREE(p[i].attachment_mime_type);
 #ifdef __ATTACHMENT_OPTI__
                        EM_SAFE_FREE(p[i].section);
@@ -10661,14 +11107,15 @@ INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachm
        return true;
 }
 
-
-
-INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
+INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
 {
        EM_PROFILE_BEGIN(emStorageBeginTransaction);
        int ret = true;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       _timedlock_shm_mutex(mapped_for_db_lock, 2);
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
        int rc;
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
@@ -10683,13 +11130,13 @@ INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
+INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = true;
        int rc;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
@@ -10697,16 +11144,18 @@ INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code
        if (ret == false && err_code != NULL)
                *err_code = EMAIL_ERROR_DB_FAILURE;
 
+       _unlockshm_mutex(mapped_for_db_lock);
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
+INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = true;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
        int rc;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
@@ -10715,11 +11164,13 @@ INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_co
        if (ret == false && err_code != NULL)
                *err_code = EMAIL_ERROR_DB_FAILURE;
 
+       _unlockshm_mutex(mapped_for_db_lock);
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
+INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
 
@@ -10737,7 +11188,7 @@ INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mai
        int error = EMAIL_ERROR_NONE;
        int mail_count = 0;
 
-       if (!emstorage_get_mail_count(account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
+       if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -10762,19 +11213,18 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
        const email_db_object_t* tables = _g_db_tables;
        const email_db_object_t* indexes = _g_db_indexes;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        if (!emstorage_delete_dir(MAILHOME, &error)) {
                EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
@@ -10789,9 +11239,11 @@ INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
        while (indexes->object_name)  {
                if (indexes->data_flag)  {
                        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
-                       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                       if (error != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                                       goto FINISH_OFF;
+                       }
                }
                indexes++;
        }
@@ -10799,9 +11251,11 @@ INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
        while (tables->object_name)  {
                if (tables->data_flag)  {
                        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
-                       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                       if (error != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                                       goto FINISH_OFF;
+                       }
                }
 
                tables++;
@@ -10809,8 +11263,7 @@ INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -10843,9 +11296,9 @@ INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name
        return result;
 }
 
-INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
+INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
+       EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
        EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
 
        int ret = false;
@@ -10853,21 +11306,22 @@ INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_
        char *dir_name = NULL;
        char create_dir[1024]={0};
        char *temp_file = NULL;
+       char *prefix_path = NULL;
 
-       if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
-               EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p]", account_id, mail_id, atch_id, fname, name_buf);
+       if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
+               EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], move_buf[%p], path_buf[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf);
                error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       sprintf(name_buf, "%s", MAILHOME);
-       sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
+       sprintf(path_buf, "%s", MAILHOME);
+       sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
 
        if (mail_id > 0)
-               sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
+               sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
 
        if (atch_id > 0)
-               sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
+               sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
 
        if (fname) {
                temp_file = EM_SAFE_STRDUP(fname);
@@ -10877,7 +11331,7 @@ INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_
        }
 
        if (dir_name) {
-               sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
+               snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
                EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
                mkdir(create_dir, DIRECTORY_PERMISSION);
                EM_SAFE_FREE(dir_name);
@@ -10885,15 +11339,69 @@ INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_
 
        if (fname) {
                EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
-               sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
+               if (EM_SAFE_STRLEN(fname) + EM_SAFE_STRLEN(path_buf) + EM_SAFE_STRLEN(DIR_SEPERATOR) > maxlen - 1) {
+                       char *modified_name = NULL;
+                       int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - EM_SAFE_STRLEN(DIR_SEPERATOR);
+
+                       if (remain_len <= 0) {
+                               error = EMAIL_ERROR_MAX_EXCEEDED;
+                               goto FINISH_OFF;
+                       }
+
+                       if (remain_len > MAX_FILENAME) {
+                               remain_len = MAX_FILENAME;
+                       }
+
+                       modified_name = em_shrink_filename(fname, remain_len);
+
+                       if (!modified_name) {
+                               error = EMAIL_ERROR_MAX_EXCEEDED;
+                               goto FINISH_OFF;
+                       }
+
+                       sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
+                       EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
+                       EM_SAFE_FREE(modified_name);
+               } else {
+                       if (EM_SAFE_STRLEN(fname) > MAX_FILENAME - 1) {
+                               char *modified_name = NULL;
+
+                               modified_name = em_shrink_filename(fname, MAX_FILENAME);
+                               if (!modified_name) {
+                                       error = EMAIL_ERROR_MAX_EXCEEDED;
+                                       goto FINISH_OFF;
+                               }
+
+                               sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
+                               EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
+                               EM_SAFE_FREE(modified_name);
+                       } else {
+                               sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, fname);
+                       }
+               }
        }
 
-       EM_DEBUG_LOG_SEC(">>>>> name_buf [%s]", name_buf);
+       EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
+
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+        sprintf(move_buf, "%s/%s", prefix_path, path_buf);
+        EM_DEBUG_LOG("move_buf : [%s]", move_buf);
+    } else {
+        sprintf(move_buf, "%s", path_buf);
+        EM_DEBUG_LOG("move_buf : [%s]", move_buf);
+    }
 
        ret = true;
 
 FINISH_OFF:
+
        EM_SAFE_FREE(temp_file);
+       EM_SAFE_FREE(prefix_path);
 
        if (err_code != NULL)
                *err_code = error;
@@ -10903,7 +11411,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
+INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
 
@@ -10933,7 +11441,7 @@ FINISH_OFF:
        return true;
 }
 
-INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
+INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
        EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
@@ -10941,10 +11449,27 @@ INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id,
        int error = EMAIL_ERROR_NONE;
        char buf[512];
        struct stat sbuf;
+       char *prefix_path = NULL;
        char errno_buf[ERRNO_BUF_SIZE] = {0};
 
+       memset(buf, 0x00, sizeof(buf));
+
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               error = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        if (account_id >= FIRST_ACCOUNT_ID)  {
-               SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
+               SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path, 
+                                                                                               DIR_SEPERATOR, 
+                                                                                               MAILHOME, 
+                                                                                               DIR_SEPERATOR, 
+                                                                                               account_id);
 
                if (stat(buf, &sbuf) == 0) {
                        if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
@@ -11026,6 +11551,9 @@ INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id,
        ret = true;
 
 FINISH_OFF:
+
+       EM_SAFE_FREE(prefix_path);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -11046,7 +11574,6 @@ INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_s
        int fp_dst = 0;
        int nread = 0;
        char *buf =  NULL;
-       int buf_size = 0;
        char errno_buf[ERRNO_BUF_SIZE] = {0};
 
        if (!src_file || !dst_file)  {
@@ -11063,72 +11590,63 @@ INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_s
                goto FINISH_OFF;
        }
 
-       buf_size =  st_buf.st_size;
-       EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
-       buf = (char *)calloc(1, buf_size+1);
-
-       if (!buf) {
-               EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
-               error = EMAIL_ERROR_OUT_OF_MEMORY;
-               goto FINISH_OFF;
-       }
-
-       if (buf) {
-               if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
-                       EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed: %s", src_file, EM_STRERROR(errno_buf));
-                       error = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
-
-               if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
-                       EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s:  %s", dst_file, EM_STRERROR(errno_buf));
-                       error = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
-
-               while ((nread = read(fp_src, buf, buf_size)) > 0) {
-                       if (nread > 0 && nread <= buf_size)  {
-                               EM_DEBUG_LOG("Nread Value [%d]", nread);
-                               char *buf_ptr;
-                               ssize_t byte_written = 0;
-                               size_t remain_byte = nread;
-                               buf_ptr = buf;
-                               errno = 0;
-
-                               while(remain_byte > 0 && buf_ptr && errno == 0) {
-                                       byte_written = write(fp_dst, buf_ptr, remain_byte);
-
-                                       if (byte_written < 0) {
-                                               /* interrupted by a signal */
-                                               if (errno == EINTR) {
-                                                       errno = 0;
-                                                       continue;
-                                               }
-
-                                               EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
-                                               error = EMAIL_ERROR_UNKNOWN;
-                                               goto FINISH_OFF;
-                                       }
-                                       EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
-                                       remain_byte -= byte_written;
-                                       buf_ptr += byte_written;
-                               }
-                       }
-               }
+        error = em_open(src_file, O_RDONLY, 0, &fp_src);
+        if (error != EMAIL_ERROR_NONE) {
+                EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
+                goto FINISH_OFF;
+        }
+
+        error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
+        if (error != EMAIL_ERROR_NONE) {
+                EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
+                goto FINISH_OFF;
+        }
+
+        memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
+
+        while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
+                if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE)  {
+                        EM_DEBUG_LOG("Nread Value [%d]", nread);
+                        char *buf_ptr;
+                        ssize_t byte_written = 0;
+                        size_t remain_byte = nread;
+                        buf_ptr = buf;
+                        errno = 0;
+
+                        while(remain_byte > 0 && buf_ptr && errno == 0) {
+                                byte_written = write(fp_dst, buf_ptr, remain_byte);
+
+                                if (byte_written < 0) {
+                                        /* interrupted by a signal */
+                                        if (errno == EINTR) {
+                                                errno = 0;
+                                                continue;
+                                        }
+
+                                        EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
+                                        error = EMAIL_ERROR_UNKNOWN;
+                                        goto FINISH_OFF;
+                                }
+                                EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
+                                remain_byte -= byte_written;
+                                buf_ptr += byte_written;
+                        }
+                }
+
+                memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
        }
 
        ret = true;
 
 FINISH_OFF:
-       if (fp_src>0) /*prevent 24474*/
-               close(fp_src);
+       EM_SAFE_CLOSE (fp_src);
 
-       if (fp_dst>0) { /*prevent 24474*/
+       if (fp_dst >=0) { /*prevent 24474*/
                if (sync_status) {
                        EM_DEBUG_LOG("Before fsync");
                        fsync(fp_dst);
                }
-               close(fp_dst);
+               close (fp_dst);
        }
        EM_SAFE_FREE(buf);
        if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
@@ -11151,6 +11669,7 @@ INTERNAL_FUNC void emstorage_create_dir_if_delete()
 
        EM_DEBUG_FUNC_END();
 }
+
 static int _get_temp_file_name(char **filename, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
@@ -11211,7 +11730,6 @@ INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, in
        int data_count_to_written = 0;
        char *temp_file_name = NULL;
        int err = 0;
-
        FILE* fp_src = NULL;
        FILE* fp_dest = NULL;
        int nread = 0;
@@ -11219,15 +11737,12 @@ INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, in
 
        if (stat(file_path, &st_buf) < 0) {
                EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
-
-               error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
+               error = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
        buf_size =  st_buf.st_size;
-
        EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
-
        buf = (char *)calloc(1, buf_size+1);
 
        if (!buf) {
@@ -11235,71 +11750,55 @@ INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, in
                goto FINISH_OFF;
        }
 
-       if (!(fp_src = fopen(file_path, "rb"))) {
-               EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s", file_path);
-
-                       error = EMAIL_ERROR_SYSTEM_FAILURE;
-                       goto FINISH_OFF;
-               }
-
-               if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
-                       if (nread > 0 && nread <= buf_size)  {
-                               EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
-
-                               /**
-                                 *   1.Add check for whether content type is there.
-                                 *   2. If not based on the character set, Append it in File
-                                 **/
-
-                               low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
-
-                               strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
-
-                               EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
+       error = em_fopen(file_path, "rb", &fp_src);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
+               goto FINISH_OFF;
+       }
 
-                               strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
+       if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
+               if (nread > 0 && nread <= buf_size)  {
+                       EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
 
-                               EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
+                       /**
+                         *   1.Add check for whether content type is there.
+                         *   2. If not based on the character set, Append it in File
+                         **/
 
-                               EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
+                       low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
 
-                               match_str = strstr(buf, CONTENT_TYPE_DATA);
-                               EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
+                       strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
+                       EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
+                       strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
+                       EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
+                       match_str = strstr(buf, CONTENT_TYPE_DATA);
 
-                               if (match_str == NULL) {
-                                       EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
-                                       if (fp_src !=NULL) {
-                                               fclose(fp_src);fp_src = NULL;
-                                       }
+                       if (match_str == NULL) {
+                               EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
+                               if (fp_src !=NULL) {
+                                       fclose(fp_src);fp_src = NULL;
+                               }
                                data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
-                                       EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
                                buf1 = (char *)calloc(1, data_count_to_written);
-                                       EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
-
-                                       if (buf1) {
-                                               EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
-                                               strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
-
-                                               EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
-
-                                               strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
-
-                                               EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
 
+                               if (buf1) {
+                                       strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
+                                       EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
+                                       strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
+                                       EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
 
                                        /* 1. Create a temporary file name */
                                        if (!_get_temp_file_name(&temp_file_name, &err)) {
                                                        EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
                                                        if (err_code != NULL) *err_code = err;
-                                                       EM_SAFE_FREE(temp_file_name);
                                                        goto FINISH_OFF;
                                        }
                                        EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
 
                                        /* Open the Temp file in Append mode */
-                                       if (!(fp_dest = fopen(temp_file_name, "ab"))) {
-                                               EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s", temp_file_name);
-                                               error = EMAIL_ERROR_SYSTEM_FAILURE;
+                                       error = em_fopen(temp_file_name, "ab", &fp_dest);
+                                       if (error != EMAIL_ERROR_NONE) {
+                                               EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
                                                goto FINISH_OFF;
                                        }
 
@@ -11332,15 +11831,10 @@ INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, in
                                                error = EMAIL_ERROR_SYSTEM_FAILURE;
                                                goto FINISH_OFF;
                                        }
-
-                                       }
-
                                }
-                               EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
-
-
                        }
                }
+       }
 
        ret = true;
 FINISH_OFF:
@@ -11348,6 +11842,7 @@ FINISH_OFF:
        EM_SAFE_FREE(buf);
        EM_SAFE_FREE(buf1);
        EM_SAFE_FREE(low_char_set);
+       EM_SAFE_FREE(temp_file_name);
 
        if (fp_src != NULL) {
                fclose(fp_src);
@@ -11358,6 +11853,8 @@ FINISH_OFF:
                fclose(fp_dest);
                fp_dest = NULL;
        }
+       if (err_code)
+               *err_code = error;
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 
@@ -11377,7 +11874,7 @@ INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_s
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
+       EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
 
        if (strcmp(src_file, dst_file) != 0) {
                if (rename(src_file, dst_file) != 0) {
@@ -11415,7 +11912,7 @@ INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_s
                }
        }
        else {
-               EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
+               EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
        }
 
        ret = true;
@@ -11554,39 +12051,38 @@ INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
 }
 
 /* faizan.h@samsung.com */
-INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
+INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
        int transaction = true;
 
        if (!old_server_uid || !new_server_uid) {
                EM_DEBUG_EXCEPTION("Invalid parameters");
-               if (err_code) 
+               if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
 
-       EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -11596,14 +12092,13 @@ FINISH_OFF:
 
 }
 
-INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
+INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
 
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
        int transaction = true;
 
        if (!mail_id || !new_server_uid || !mbox_name)  {
@@ -11612,21 +12107,25 @@ INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_u
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
        ret     = true;
 
 FINISH_OFF:
 
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -11636,13 +12135,51 @@ FINISH_OFF:
 
 }
 
+INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
+       int ret = false;
+       int transaction = true;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0,};
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       if (account_id <= ALL_ACCOUNT)
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
+       else
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+
+}
 
-int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
+int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
-       if ((!mail_id) ||(account_id <= 0 &&  account_id != -1)) {
-               EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
+       if ((!mail_ids) ||(account_id <= 0 &&  account_id != -1)) {
+               EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
@@ -11651,38 +12188,42 @@ int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_co
        int ret = false;
        int rc = -1;
        int error = EMAIL_ERROR_NONE;
-       DB_STMT hStmt = NULL;
        int count = 0;
-       int mailid = 0;
+       int i = 0;
+       int col_index = 0;
+       int *p_mail_ids = NULL;
        int transaction = false;
+       char **result = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
+       char temp_query_string[QUERY_SIZE] = {0,};
+       char sql_select_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        if (account_id == -1)
-               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
+               SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
        else
-               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 ORDER BY mail_id DESC", account_id);
+               SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
-       EM_DEBUG_LOG("  sqlite3_prepare hStmt = %p", hStmt);
-       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       if (vip_mode) {
+               SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
+       } else {
+               SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
+       }
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
-       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("sqlite3_step fail:%d", rc));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
 
-       char **result;
+       EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
                ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       sqlite3_free_table(result);
+       EM_DEBUG_LOG("Count : %d", count);
+
        if (count == 0)  {
                EM_DEBUG_EXCEPTION("no Mails found...");
                ret = false;
@@ -11690,27 +12231,40 @@ int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_co
                goto FINISH_OFF;
        }
 
-       _get_stmt_field_data_int(hStmt, &mailid, 0);
-       EM_DEBUG_LOG("mailid [%d]", mailid);
+       p_mail_ids = em_malloc(count * sizeof(int));
+       if (p_mail_ids == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed...");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       col_index = 1;
+
+       for (i = 0; i < count; i++) {
+               _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
+       }
 
        ret = true;
 
 FINISH_OFF:
-       if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
 
-               rc = sqlite3_finalize(hStmt);
-               if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+       if (result)
+               sqlite3_free_table(result);
 
-                       error = EMAIL_ERROR_DB_FAILURE;
-               }
-       }
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
-       if (mail_id != NULL)
-               *mail_id = mailid;
+       sqlite3_db_release_memory(local_db_handle);
+
+
+       if (ret == true) {
+               if (mail_ids != NULL)
+                       *mail_ids = p_mail_ids;
+
+               if (mail_number != NULL)
+                       *mail_number = count;
+       } else {
+               EM_SAFE_FREE(p_mail_ids);
+       }
 
        if (err_code != NULL)
                *err_code = error;
@@ -11795,11 +12349,9 @@ INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_
                goto FINISH_OFF;
        }
 
-       fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
-       if (fp == NULL) {
+       error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
+       if (error != EMAIL_ERROR_NONE) {
                perror(SETTING_MEMORY_TEMP_FILE_PATH);
-
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
                goto FINISH_OFF;
        }
 
@@ -11840,7 +12392,7 @@ FINISH_OFF:
 }
 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA,"email/.email_data/7/348/UTF-8")
 
-INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
+INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
 {
        DB_STMT hStmt = NULL;
        int ret = false;
@@ -11900,14 +12452,15 @@ INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address
                ", ?" /*  tag_id   */
                ", ?" /*  replied_time */
                ", ?" /*  forwarded_time */
+               ", ?" /*  default_charset */
                ", ?" /*  eas_data_length */
                ", ?" /*  eas_data */
                ")");
 
        int transaction = true;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -11965,6 +12518,7 @@ INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address
        _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
        _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
        _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
+       _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
        _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
        _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
 
@@ -11976,12 +12530,11 @@ INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -12001,7 +12554,7 @@ INTERNAL_FUNC int emstorage_get_max_mail_count()
 
 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
 
-INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(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_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
@@ -12010,7 +12563,6 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *
        int      err_code = EMAIL_ERROR_NONE;
        int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
        time_t   latest_date_time = 0;
-       time_t   date_time = 0;
        char    *subject = NULL;
        char    *sql_query_string = NULL, *sql_account = NULL;
        char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
@@ -12018,7 +12570,8 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *
        char    *sql_format_order_by = " ORDER BY date_time DESC ";
        char   **result = NULL;
        char     stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
 
@@ -12029,23 +12582,30 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *
 
        account_id   = mail_tbl->account_id;
        subject      = mail_tbl->subject;
-       date_time    = mail_tbl->date_time;
 
-       EM_DEBUG_LOG("subject: [%s]", subject);
+       EM_DEBUG_LOG_SEC("subject: [%s]", subject);
+
+       if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
+               result_thread_id = mail_tbl->mail_id;
+               count = 1;
+               goto FINISH_OFF;
+       }
 
        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");
+               EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
                err_code =  EMAIL_ERROR_UNKNOWN;
                result_thread_id = -1;
                goto FINISH_OFF;
        }
 
+       EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
+
        if (EM_SAFE_STRLEN(stripped_subject) < 2) {
                result_thread_id = -1;
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("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);
@@ -12076,7 +12636,7 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *
        strcat(sql_query_string, sql_format_order_by);
        strcat(sql_query_string, ";");
 
-       EM_DEBUG_LOG("Query : %s", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
 
@@ -12117,8 +12677,108 @@ FINISH_OFF:
        return 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)
+{
+       EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
+       EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
+
+       int rc = 0;
+       int query_size = 0;
+       int query_size_account = 0;
+       int err_code = EMAIL_ERROR_NONE;
+       int count = 0;
+       int result_thread_id = -1;
+       char *sql_query_string = NULL;
+       char *sql_account = NULL;
+       char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
+       char *sql_format_account = " AND account_id = %d ";
+       char *sql_format_order_by = " ORDER BY date_time DESC ";
+       char **result = NULL;
+       char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
+       EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
+       EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
+
+       EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
+
+       if (em_find_pos_stripped_subject_for_thread_view(mail_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;
+               goto FINISH_OFF;
+       }
+
+       if (EM_SAFE_STRLEN(stripped_subject) < 2) {
+               result_thread_id = -1;
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("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);
+               sql_account = malloc(query_size_account);
+               if (sql_account == NULL) {
+                       EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
+                       err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
+                       goto FINISH_OFF;
+               }
+               snprintf(sql_account, query_size_account, sql_format_account, account_id);
+       }
+
+       query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /*  + query_size_mailbox; */
+       sql_query_string = malloc(query_size);
+
+       if (sql_query_string == NULL) {
+               EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
+               err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
+
+       if (account_id > 0)
+               strcat(sql_query_string, sql_account);
+
+       strcat(sql_query_string, sql_format_order_by);
+       strcat(sql_query_string, ";");
+
+       EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
+
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       EM_DEBUG_LOG("Result rows count : %d", count);
+
+       if (count == 0)
+               result_thread_id = -1;
+       else {
+               _get_table_field_data_int   (result, &result_thread_id, 1);
+       }
+
+FINISH_OFF:
+       *thread_id = result_thread_id;
+       *thread_item_count = count;
+
+       EM_DEBUG_LOG("Result thread id : %d", *thread_id);
+       EM_DEBUG_LOG("Result count : %d", *thread_item_count);
+       EM_DEBUG_LOG("err_code : %d", err_code);
+
+       EM_SAFE_FREE(sql_account);
+       EM_SAFE_FREE(sql_query_string);
+
+       sqlite3_free_table(result);
+
+       EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
+
+       return err_code;
+}
 
-INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -12132,7 +12792,7 @@ INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail
        SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
        EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
 
-       if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
+       if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
                goto FINISH_OFF;
        }
@@ -12155,8 +12815,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_sender_list(int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
+INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
                , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
@@ -12176,7 +12835,7 @@ INTERNAL_FUNC int emstorage_get_sender_list(int account_id, int mailbox_id, int
        email_sender_list_t *p_sender_list = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
        char **result = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
@@ -12231,7 +12890,7 @@ INTERNAL_FUNC int emstorage_get_sender_list(int account_id, int mailbox_id, int
                "GROUP BY email_address_sender "
                "ORDER BY UPPER(alias_sender) ");
 
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -12269,7 +12928,6 @@ FINISH_OFF:
                EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
        }
 
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -12361,7 +13019,7 @@ INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **a
 }
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
-INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
 
@@ -12379,8 +13037,9 @@ INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local
 
        char sql_query_string[QUERY_SIZE] = {0, };
        DB_STMT hStmt = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "INSERT INTO mail_partial_body_activity_tbl VALUES "
@@ -12392,6 +13051,7 @@ INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local
                ",?"  /* Activity type*/
                ",?"  /* Mailbox ID*/
                ",?"  /* Mailbox name*/
+               ",?"  /* Multi User Name */
                ") ");
 
        char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
@@ -12421,7 +13081,6 @@ INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
 
        _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
        _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
@@ -12430,6 +13089,7 @@ INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local
        _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
        _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
        _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
+       _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
@@ -12442,15 +13102,12 @@ INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -12462,12 +13119,11 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
 
-       if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
+       if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL|| NULL == count) {
                EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
@@ -12482,8 +13138,7 @@ INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_l
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
@@ -12510,12 +13165,12 @@ INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_l
        /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
 
-       EM_DEBUG_LOG(" Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
 
-       EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
+
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
@@ -12524,7 +13179,8 @@ INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_l
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d", rc));
 
-       if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
+       mbox_list = (int *)em_malloc(sizeof(int) * (*count));
+       if (NULL == mbox_list) {
                EM_DEBUG_EXCEPTION(" em_malloc failed...");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
@@ -12538,7 +13194,7 @@ INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_l
                /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
                EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                        ("sqlite3_step fail:%d", rc));
-               EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
+               EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
        }
 
        ret = true;
@@ -12550,31 +13206,27 @@ FINISH_OFF:
                EM_SAFE_FREE(mbox_list);
 
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
        if (err_code != NULL)
                *err_code = error;
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
+       EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
 
-       if (NULL == &account_list || NULL == count) {
-               EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
+       if (NULL == account_list || NULL == count) {
+               EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
                if (err_code != NULL)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
@@ -12582,21 +13234,17 @@ INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count,
 
        int ret = false;
        int error = EMAIL_ERROR_NONE;
-       char *sql;
+       char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
        char **result;
        int i = 0, rc = -1;
        int *result_account_list = NULL;
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
-
-       sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
-
-
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
                ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
@@ -12617,7 +13265,7 @@ INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count,
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -12657,27 +13305,22 @@ FINISH_OFF:
                EM_SAFE_FREE(result_account_list);
 
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
        if (err_code != NULL)
                *err_code = error;
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-
-
-INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
 
@@ -12698,7 +13341,7 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mail
        email_event_partial_body_thd* event_list = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
@@ -12713,7 +13356,7 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mail
        *count = atoi(result[1]);
        sqlite3_free_table(result);
 
-       EM_DEBUG_LOG("Query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
 
        if (!*count) {
                EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
@@ -12726,7 +13369,7 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mail
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -12756,6 +13399,7 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mail
                _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
                _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
                _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
+               _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
 
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
                /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
@@ -12779,18 +13423,15 @@ FINISH_OFF:
        }
 
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
        if (err_code != NULL)
                *err_code = error;
 
@@ -12798,9 +13439,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-
-INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
 
@@ -12817,10 +13456,9 @@ INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        if (activity_id == 0)
@@ -12828,13 +13466,12 @@ INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int
        else
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        /*  validate activity existence */
        rc = sqlite3_changes(local_db_handle);
@@ -12848,8 +13485,8 @@ INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -12857,7 +13494,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
 
@@ -12874,14 +13511,14 @@ INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int i
 
        DB_STMT hStmt = NULL;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
 
-       EM_DEBUG_LOG(" Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -12903,18 +13540,15 @@ INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int i
 FINISH_OFF:
 
        if (hStmt != NULL) {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                hStmt=NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
-               EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -12923,8 +13557,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
 
@@ -12940,7 +13573,7 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int tran
        DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
@@ -12949,13 +13582,11 @@ INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int tran
 
        EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
 
-
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_LOG_DEV ("  before sqlite3_prepare hStmt = %p", hStmt);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d", rc));
@@ -12970,19 +13601,15 @@ FINISH_OFF:
 
 
        if (hStmt != NULL) {
-/*             EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt); */
-
                rc = sqlite3_finalize(hStmt);
                hStmt=NULL;
                if (rc != SQLITE_OK) {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
-/*             EM_DEBUG_LOG("sqlite3_finalize- %d", rc); */
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
        if (err_code != NULL)
                *err_code = error;
 
@@ -12990,7 +13617,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
        if (account_id < FIRST_ACCOUNT_ID) {
@@ -13005,19 +13632,18 @@ INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int tr
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0) {
@@ -13030,8 +13656,9 @@ INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int tr
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -13042,7 +13669,7 @@ FINISH_OFF:
 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
 if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
 
-INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
+INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
 
@@ -13058,32 +13685,30 @@ INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                 "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0) {
-               EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
-               error = EMAIL_ERROR_DATA_NOT_FOUND;
-               goto FINISH_OFF;
+               EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
        }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
        if (err_code != NULL)
                *err_code = error;
 
@@ -13107,14 +13732,9 @@ INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, cha
                goto FINISH_OFF;
        }
 
-       fp_dst = fopen(dst_file_name, "w");
-
-       if (!fp_dst)  {
-               EM_DEBUG_EXCEPTION_SEC("fopen failed - %s: %s", dst_file_name, EM_STRERROR(errno_buf));
-               if (errno == 28)
-                       error = EMAIL_ERROR_MAIL_MEMORY_FULL;
-               else
-                       error = EMAIL_ERROR_SYSTEM_FAILURE;
+       error = em_fopen(dst_file_name, "w", &fp_dst);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
                goto FINISH_OFF;
        }
 
@@ -13143,8 +13763,7 @@ FINISH_OFF:
 
 
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
-
-INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
+INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        int rc = -1;
@@ -13163,37 +13782,34 @@ INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_
 
        EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
 
-        EM_DEBUG_LOG(" Query [%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-        EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-                ("sqlite3_exec fail:%d", rc));
-        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
-        rc = sqlite3_changes(local_db_handle);
-        if (rc == 0)
-        {
-                EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
-                error = EMAIL_ERROR_DATA_NOT_FOUND;
-                goto FINISH_OFF;
-        }
+       rc = sqlite3_changes(local_db_handle);
+       if (rc == 0)
+       {
+               EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
+               error = EMAIL_ERROR_DATA_NOT_FOUND;
+               goto FINISH_OFF;
+       }
 
-        ret = true;
+       ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
-       if (err_code != NULL)
+       if (err_code != NULL)
                *err_code = error;
 
        EM_DEBUG_FUNC_END("ret [%d]", ret);
@@ -13216,8 +13832,7 @@ FINISH_OFF:
  *                                                     SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
  * @return This function returns true on success or false on failure.
  */
-
-INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
+INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
@@ -13243,11 +13858,11 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_se
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid", mail_ids);
 
-       EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
 
        /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
@@ -13289,7 +13904,7 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_se
 
        ret = true;
 
-       FINISH_OFF:
+FINISH_OFF:
 
        if (ret == true)  {
                *idset = p_id_set;
@@ -13299,7 +13914,6 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_se
        else
                EM_SAFE_FREE(p_id_set);
 
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -13314,52 +13928,51 @@ INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_se
 
 #endif
 
-INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
+INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int rc, ret = true, transaction = true;
+       int ret = true, transaction = true;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_tag_id(int old_filter_id, int new_filter_id, int *err_code)
+INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
-       int rc, ret = false;
+       int ret = false;
        int error = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
-
        int transaction = true;
 
        if (old_filter_id < 0 || new_filter_id < 0) {
@@ -13368,24 +13981,24 @@ INTERNAL_FUNC int emstorage_update_tag_id(int old_filter_id, int new_filter_id,
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
 
-       EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -13394,7 +14007,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
+INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
 
@@ -13411,12 +14024,11 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
        int tag_id = rule->rule_id;
        char **result = NULL, *where_pararaph = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
 
-       where_pararaph_length = EM_SAFE_STRLEN(rule->value) + EM_SAFE_STRLEN(rule->value2) + 100;
+       where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
        where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
        if (where_pararaph == NULL) {
                EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
@@ -13424,10 +14036,10 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
                goto FINISH_OFF;
        }
 
-       if (account_id != ALL_ACCOUNT) 
-               SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5)", account_id);
+       if (account_id != ALL_ACCOUNT)
+               SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
        else
-               SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (3,5)");
+               SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
 
        if (rule->type & EMAIL_FILTER_SUBJECT) {
                if (rule->flag2 == RULE_TYPE_INCLUDES)
@@ -13439,6 +14051,10 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
        if (rule->type & EMAIL_FILTER_FROM) {
                if (rule->flag2 == RULE_TYPE_INCLUDES)
                        sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
+#ifdef __FEATURE_COMPARE_DOMAIN__
+               else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
+                       sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
+#endif /*__FEATURE_COMPARE_DOMAIN__ */
                else /*  RULE_TYPE_EXACTLY */
                        sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
        }
@@ -13447,7 +14063,7 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
                if (rule->flag2 == RULE_TYPE_INCLUDES)
                        sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
                else /*  RULE_TYPE_EXACTLY */
-                       sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
+                       sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
 
                tag_id = PRIORITY_SENDER_TAG_ID;
        }
@@ -13456,7 +14072,7 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
        if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
                strcat(sql_query_string, where_pararaph);
 
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
 
        /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
@@ -13508,9 +14124,11 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
                if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
                        strcat(sql_query_string, where_pararaph);
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                               goto FINISH_OFF;
+               }
 
 #ifdef __FEATURE_BODY_SEARCH__
                /* Updating mail_text_tbl */
@@ -13519,11 +14137,13 @@ INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbo
                        SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
                        if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
                                strcat(sql_query_string, where_pararaph);
-                       EM_DEBUG_LOG("Query [%s]", sql_query_string);
 
-                       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+                       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+                       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+                       if (error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                               goto FINISH_OFF;
+                       }
                }
 #endif
        }
@@ -13538,13 +14158,12 @@ FINISH_OFF:
 
                if (count_of_mails)
                        *count_of_mails = count;
-       } else  
+       } else
                EM_SAFE_FREE(mail_list);
 
        sqlite3_free_table(result);
        result = NULL;
 
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(where_pararaph);
 
@@ -13556,18 +14175,17 @@ FINISH_OFF:
 }
 
 #define EMAIL_SLOT_UNIT 25
-
-INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
-       int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
+       int ret = false, err = EMAIL_ERROR_NONE;
        int where_pararaph_length = 0;
        char *where_pararaph = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
        int and = 0;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
 
        if (new_slot_size > 0)
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
@@ -13612,17 +14230,17 @@ INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, i
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
+       err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
 
        EM_SAFE_FREE(where_pararaph);
 
@@ -13633,7 +14251,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
 
@@ -13655,8 +14273,8 @@ INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbo
        int col_index = 0;
        time_t temp_unix_time = 0;
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "INSERT INTO mail_meeting_tbl VALUES "
@@ -13738,14 +14356,11 @@ INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbo
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -13757,7 +14372,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_query_meeting_request(const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
+INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
 
@@ -13782,13 +14397,13 @@ INTERNAL_FUNC int emstorage_query_meeting_request(const char *conditional_clause
        col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
        EM_DEBUG_LOG("col_index [%d]", col_index);
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -13848,13 +14463,12 @@ FINISH_OFF:
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -13869,7 +14483,7 @@ INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_reque
        SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
        EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
 
-       if((error = emstorage_query_meeting_request(conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
+       if((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
                goto FINISH_OFF;
        }
@@ -13898,8 +14512,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
 
@@ -13917,8 +14530,8 @@ INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meet
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
@@ -13941,7 +14554,7 @@ INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meet
                "WHERE mail_id = %d",
                meeting_req->mail_id);
 
-       EM_DEBUG_LOG("SQL(%s)", sql_query_string);
+       EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
@@ -13988,16 +14601,12 @@ INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meet
 
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -14009,7 +14618,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
 
@@ -14021,14 +14630,13 @@ INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id,
                return false;
        }
 
-       int rc;
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        int and = false;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
 
@@ -14044,15 +14652,16 @@ INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id,
                SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
        }
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code)
                *err_code = error;
@@ -14074,8 +14683,7 @@ INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeti
        EM_DEBUG_FUNC_END();
 }
 
-
-INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
        EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
@@ -14096,9 +14704,9 @@ INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int inpu
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
 
-       EM_DEBUG_LOG("query[%s].", sql_query_string);
+       EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
@@ -14144,7 +14752,6 @@ FINISH_OFF:
                EM_SAFE_FREE(result_mail_id_list);
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -14154,8 +14761,7 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
+INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
 
@@ -14172,7 +14778,7 @@ INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        memset(sql_query_string, 0, QUERY_SIZE);
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
@@ -14197,7 +14803,6 @@ INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id
        ret = true;
 
 FINISH_OFF:
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = err;
@@ -14206,10 +14811,9 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread_id, int latest_mail_id, int thread_item_count, int transaction, 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)
 {
-       EM_DEBUG_FUNC_BEGIN("account_id [%d], thread_id[%d], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, thread_id, latest_mail_id, thread_item_count, 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;
@@ -14224,11 +14828,11 @@ INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       if (thread_item_count == 0 || latest_mail_id == 0) {
+       if (thread_item_count == 0 && latest_mail_id == 0) {
                memset(sql_query_string, 0, QUERY_SIZE);
-               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_type FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_type NOT in (3,5,7)", account_id, thread_id);
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
 
                /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
@@ -14253,32 +14857,117 @@ INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread
                sqlite3_free_table(result);
        }
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
 
-       /* if (thread_item_count > 1) */
-       /* { */
+       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("query[%s]", sql_query_string);
+               EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
+               err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                               goto FINISH_OFF;
+               }
+       } else if (thread_id != latest_mail_id) {
+               /* Initialize the thread id */
+               memset(sql_query_string, 0, QUERY_SIZE);
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
+               err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                               goto FINISH_OFF;
+               }
+
+               /* update the thread item count */
+               memset(sql_query_string, 0, QUERY_SIZE);
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
+               err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                               goto FINISH_OFF;
+               }
 
-               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                       ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-       /* } */
 
-       memset(sql_query_string, 0, QUERY_SIZE);
-       SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
+       } else {
+               memset(sql_query_string, 0, QUERY_SIZE);
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
+               EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
+               err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (err != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                               goto FINISH_OFF;
+               }
+       }
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
+
+       if (thread_id != latest_mail_id)
+       {
+               if(err == EMAIL_ERROR_NONE)
+               {
+                       EM_DEBUG_LOG("noti_type[%d]", noti_type);
+
+                       if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
+                               char mailbox_id_str[25] = {0,};
+                               snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
+                               if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
+                                       EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_THREAD_ID_CHANGED] >>>> ");
+
+                               if (updated_thread_id) *updated_thread_id = latest_mail_id;
+                       }
+               }
+       }else if(thread_item_count >= 0)
+       {
+               if(err == EMAIL_ERROR_NONE)
+               {
+                       char parameter_string[500] = {0,};
+                       SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
+                       if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
+                               EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
+               }
+       }
+
+       if (err_code != NULL)
+               *err_code = err;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
+
+       int ret = false;
+       int err = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       if (thread_id == 0) {
+               EM_DEBUG_EXCEPTION("Invalid Parameter");
+               if (err_code)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-               EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
+
+       memset(sql_query_string, 0, QUERY_SIZE);
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
+       err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+               goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
 
        if (err_code != NULL)
                *err_code = err;
@@ -14317,10 +15006,11 @@ INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activit
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       memset(sql_query_string, 0x00 , sizeof(sql_query_string));
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       memset(sql_query_string, 0x00 , sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
 
        EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
@@ -14331,14 +15021,14 @@ INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activit
        EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
        EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
 
-       EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
 
        _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
        _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
@@ -14359,14 +15049,12 @@ INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activit
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -14405,7 +15093,7 @@ INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstora
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
@@ -14418,7 +15106,7 @@ INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstora
                SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
        }
 
-       EM_DEBUG_LOG("Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
 
 
 
@@ -14512,12 +15200,12 @@ INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code
 
        if (NULL == activity_id) {
                EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
-               if (err_code) 
+               if (err_code)
                        *err_code = EMAIL_ERROR_INVALID_PARAM;
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        /*  increase unique id */
 
@@ -14549,7 +15237,7 @@ INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code
 
 }
 
-INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
 {
 
        EM_DEBUG_FUNC_BEGIN();
@@ -14569,14 +15257,14 @@ INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int ** activity
        int *activity_ids = NULL;
        int col_index = 0;
        char **result = NULL;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
 
-       EM_DEBUG_LOG(" Query [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
 
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
@@ -14676,14 +15364,14 @@ INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* loca
        int query_and = 0;
        int query_where = 0;
        char sql_query_string[8192] = { 0x00, };
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
 
-       EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
 
        if (local_activity->account_id) {
                SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
@@ -14722,11 +15410,12 @@ INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* loca
 
        }
 
-       EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        rc = sqlite3_changes(local_db_handle);
        if (rc == 0)  {
@@ -14737,7 +15426,16 @@ INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* loca
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (hStmt != NULL)  {
+               rc = sqlite3_finalize(hStmt);
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
        if (err_code != NULL)
                *err_code = err;
        EM_DEBUG_FUNC_END("ret [%d]", ret);
@@ -14773,7 +15471,7 @@ INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local
                                EM_SAFE_FREE (p[i].server_mailid);
                        }
 
-                       EM_SAFE_FREE (p);       
+                       EM_SAFE_FREE (p);
                        *local_activity_list = NULL;
                }
        }
@@ -15023,23 +15721,24 @@ FINISH_OFF:
        return ret;
 }
 
-static int _make_filter_attach_rule_string(email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
+static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
 {
        EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
 
-       int   ret = EMAIL_ERROR_NONE;
        char *field_name_string = NULL;
        char  key_value_string[QUERY_SIZE] = {0,};
        char  result_rule_string[QUERY_SIZE] = {0,};
-
        int rc = -1;
        int count = 0;
+       int query_size = 0;
+       int cur_query = 0;
        int col_index = 0;
        int error = EMAIL_ERROR_NONE;
        char **result = NULL;
        char sql_query_string[QUERY_SIZE] = {0,};
-       char sql_query_string2[QUERY_SIZE] = {0,};
+       char *sql_query_string2 = NULL;
        sqlite3 *local_db_handle = NULL;
+       int *mail_ids = NULL;
 
        if(input_list_filter_rule == NULL || output_string == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -15061,11 +15760,12 @@ static int _make_filter_attach_rule_string(email_list_filter_rule_attach_t *inpu
 
        default :
                EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
-               ret = EMAIL_ERROR_INVALID_PARAM;
+               error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
        EMSTORAGE_START_READ_TRANSACTION(true);
        SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
 
@@ -15077,7 +15777,6 @@ static int _make_filter_attach_rule_string(email_list_filter_rule_attach_t *inpu
 
        if (!count) {
                EM_DEBUG_LOG("No mail found...");
-               ret = true;
                error= EMAIL_ERROR_MAIL_NOT_FOUND;
                *output_string = strdup("mail_id IN ( ) ");
                goto FINISH_OFF;
@@ -15085,7 +15784,6 @@ static int _make_filter_attach_rule_string(email_list_filter_rule_attach_t *inpu
 
        EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
        int i = 0;
-       int *mail_ids = NULL;
        if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
                EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -15103,41 +15801,48 @@ static int _make_filter_attach_rule_string(email_list_filter_rule_attach_t *inpu
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
 
-       int cur_query = 0;
-       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "mail_id IN ( ");
+       query_size = (10 * count) + strlen("mail_id IN ( )  ");
+
+       sql_query_string2 = em_malloc(query_size);
+       if (sql_query_string2 == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
        for (i = 0; i < count-1; i++) {
-               cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d, ", mail_ids[i]);
+               cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
        }
-       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d ) ", mail_ids[count-1]);
+       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
 
        *output_string = strdup(sql_query_string2);
-       EM_SAFE_FREE(mail_ids); /*prevent 50929*/
-
 FINISH_OFF:
+
+       EM_SAFE_FREE(mail_ids); /* prevent */
+       EM_SAFE_FREE(sql_query_string2);
        EM_SAFE_FREE(field_name_string);
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
 }
 
-static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
+static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
 {
        EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
-
-       int   ret = EMAIL_ERROR_NONE;
        char *field_name_string = NULL;
        char key_value_string[QUERY_SIZE] = {0,};
        char  result_rule_string[QUERY_SIZE] = {0,};
-
        int rc = -1;
        int count = 0;
        int col_index = 0;
+       int query_size = 0;
        int error = EMAIL_ERROR_NONE;
        char **result = NULL;
        char sql_query_string[QUERY_SIZE] = {0,};
-       char sql_query_string2[QUERY_SIZE] = {0,};
+       char *sql_query_string2 = NULL;
        sqlite3 *local_db_handle = NULL;
+       int *mail_ids = NULL;
 
        if(input_list_filter_rule == NULL || output_string == NULL) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -15159,11 +15864,12 @@ static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list
 
        default :
                EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
-               ret = EMAIL_ERROR_INVALID_PARAM;
+               error = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
        EMSTORAGE_START_READ_TRANSACTION(true);
        SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
 
@@ -15175,7 +15881,6 @@ static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list
 
        if (!count) {
                EM_DEBUG_LOG("No mail found...");
-               ret = true;
                error= EMAIL_ERROR_MAIL_NOT_FOUND;
                *output_string = strdup("mail_id IN ( ) ");
                goto FINISH_OFF;
@@ -15183,7 +15888,7 @@ static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list
 
        EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
        int i = 0;
-       int *mail_ids = NULL;
+
        if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
                EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
                error = EMAIL_ERROR_OUT_OF_MEMORY;
@@ -15201,25 +15906,32 @@ static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list
 
        sqlite3_db_release_memory(local_db_handle);
 
-       _DISCONNECT_DB;
 
+       query_size = (10 * count) + strlen("mail_id IN ( )  ");
+       sql_query_string2 = em_malloc(query_size);
+       if (sql_query_string2 == NULL) {
+               EM_DEBUG_EXCEPTION("em_malloc failed");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
        int cur_query = 0;
-       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "mail_id IN ( ");
+       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
        for (i = 0; i < count-1; i++) {
-               cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d, ", mail_ids[i]);
+               cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
        }
-       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d ) ", mail_ids[count-1]);
+       cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
 
        *output_string = strdup(sql_query_string2);
-       EM_SAFE_FREE(mail_ids); /*prevent 50929*/
 
 FINISH_OFF:
+       EM_SAFE_FREE(mail_ids); /* prevent */
+       EM_SAFE_FREE(sql_query_string2);
        EM_SAFE_FREE(field_name_string);
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
-       return ret;
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
 }
 
-static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
+static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
        EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
 
        char  result_rule_string[QUERY_SIZE] = { 0 , };
@@ -15248,6 +15960,14 @@ static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule
                        EM_SAFE_STRCAT(result_rule_string, " DESC ");
                        break;
 
+               case EMAIL_SORT_ORDER_NOCASE_ASCEND :
+                       EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
+                       break;
+
+               case EMAIL_SORT_ORDER_NOCASE_DESCEND :
+                       EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
+                       break;
+
                case EMAIL_SORT_ORDER_TO_CCBCC :
                        memset(result_rule_string, 0, QUERY_SIZE);
                        if (input_sorting_rule->key_value.string_type_value)
@@ -15265,7 +15985,7 @@ static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule
                        break;
 
                case EMAIL_SORT_ORDER_TO_CCBCC_ALL :
-                       if (!emstorage_get_account_list(&count, &account_tbl_array, true, false, NULL)) {
+                       if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
                                goto FINISH_OFF;
                        }
@@ -15304,6 +16024,22 @@ static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule
                                emstorage_free_account(&account_tbl_array, count, NULL);
                        break;
 
+               case EMAIL_SORT_ORDER_LOCALIZE_ASCEND :
+                       memset(result_rule_string, 0, QUERY_SIZE);
+                       sqlite3_snprintf(QUERY_SIZE, result_rule_string,
+                               " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
+                               emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
+                               emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
+                       break;
+
+               case EMAIL_SORT_ORDER_LOCALIZE_DESCEND :
+                       memset(result_rule_string, 0, QUERY_SIZE);
+                       sqlite3_snprintf(QUERY_SIZE, result_rule_string,
+                               " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
+                               emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
+                               emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
+                       break;
+
                default :
                        EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
                        ret = EMAIL_ERROR_INVALID_PARAM;
@@ -15317,13 +16053,15 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
+INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
 {
        EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
        int ret = EMAIL_ERROR_NONE;
        int i = 0;
        int string_offset = 0;
-       char conditional_clause_string[QUERY_SIZE] = {0, };
+       int query_size = 0;
+       int new_query_size = 0;
+       char *conditional_clause_string = NULL;
        char *result_string_for_a_item = NULL;
 
        if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
@@ -15331,7 +16069,14 @@ INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
+       conditional_clause_string = em_malloc(QUERY_SIZE);
+       if (conditional_clause_string == NULL) {
+               EM_DEBUG_EXCEPTION("Memory is full");
+               return EMAIL_ERROR_OUT_OF_MEMORY;
+       }
+
        if(input_filter_count > 0) {
+               query_size = QUERY_SIZE;
                strcpy(conditional_clause_string,  " WHERE ");
 
                for ( i = 0; i < input_filter_count; i++) {
@@ -15343,12 +16088,12 @@ INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email
 
                        case EMAIL_LIST_FILTER_ITEM_RULE_FTS :
                                EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
-                               _make_filter_fts_rule_string(&(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
+                               _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
                                break;
 
                        case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH :
                                EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
-                               _make_filter_attach_rule_string(&(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
+                               _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
                                break;
 
                        case EMAIL_LIST_FILTER_ITEM_OPERATOR :
@@ -15381,11 +16126,19 @@ INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email
                                goto FINISH_OFF;
                        }
 
-                       if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
-                               EM_DEBUG_EXCEPTION("Query is too long");
-                               ret = EMAIL_ERROR_DATA_TOO_LONG;
-                               goto FINISH_OFF;
+                       if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
+                               EM_DEBUG_LOG("QUERY is too long");
+                               new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
+                               conditional_clause_string = realloc(conditional_clause_string, new_query_size);
+                               if (conditional_clause_string == NULL) {
+                                       EM_DEBUG_EXCEPTION("realloc failed");
+                                       ret = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
+                               }
+
+                               query_size = new_query_size;
                        }
+
                        strcat(conditional_clause_string, result_string_for_a_item);
                        EM_SAFE_FREE(result_string_for_a_item);
                }
@@ -15395,7 +16148,7 @@ INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email
                strcat(conditional_clause_string, "ORDER BY ");
 
                for ( i = 0; i < input_sorting_rule_count; i++) {
-                       if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
+                       if( (ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
                                EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
                                goto FINISH_OFF;
                        }
@@ -15408,15 +16161,16 @@ INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email
 
        if (input_start_index != -1 && input_limit_count != -1) {
                string_offset = strlen(conditional_clause_string);
-               SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
+               SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
        }
 
        *output_conditional_clause = strdup(conditional_clause_string);
 
 FINISH_OFF:
        EM_SAFE_FREE(result_string_for_a_item);
+       EM_SAFE_FREE(conditional_clause_string);
 
-       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
@@ -15431,7 +16185,10 @@ INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_
 
        for ( i = 0; i < input_filter_count; i++) {
                temp_filter_list = (*input_filter_list) + i;
-               if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
+               if(!temp_filter_list) {
+                       continue;
+               }
+               if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
                        switch(temp_filter_list->list_filter_item.rule.target_attribute) {
                        case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
                        case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
@@ -15451,6 +16208,12 @@ INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_
                                break;
                        }
                }
+               else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
+                               EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
+               }
+               else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
+                               EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
+               }
        }
 
        EM_SAFE_FREE (*input_filter_list);
@@ -15459,7 +16222,7 @@ INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_
        return err;
 }
 
-INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_add_certificate(char *multi_user_name, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
 
@@ -15477,9 +16240,10 @@ INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certifi
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
        char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 #endif
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
        char **result = NULL;
@@ -15523,7 +16287,7 @@ INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certifi
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
        _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
        _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
        _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
@@ -15542,23 +16306,15 @@ INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certifi
                ("sqlite3_step fail:%d", rc));
        EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
-       if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
-               EM_DEBUG_EXCEPTION_SEC("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
-               goto FINISH_OFF;
-       }
-#endif
-       ret = true;
 
+       ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -15614,7 +16370,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
+INTERNAL_FUNC int emstorage_get_certificate_list(char *multi_user_name, int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -15631,7 +16387,7 @@ INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_cert
                return false;
        }
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        char sql_query_string[QUERY_SIZE] = {0, };
@@ -15734,18 +16490,15 @@ FINISH_OFF:
        else if (p_data_tbl != NULL)
                emstorage_free_certificate(&p_data_tbl, count, NULL);
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -15754,7 +16507,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
+INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *multi_user_name, char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
 
@@ -15775,7 +16528,8 @@ INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
        char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 #endif
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        /*  Make query string */
@@ -15786,7 +16540,7 @@ INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address
        SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
 
        /*  FROM clause */
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
        /*  execute a sql and count rows */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -15848,13 +16602,12 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -15863,7 +16616,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
+INTERNAL_FUNC int emstorage_get_certificate_by_index(char *multi_user_name, int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
 
@@ -15884,7 +16637,8 @@ INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certif
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
        char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 #endif
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
        EMSTORAGE_START_READ_TRANSACTION(transaction);
 
        /*  Make query string */
@@ -15895,7 +16649,7 @@ INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certif
        SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
 
        /*  FROM clause */
-       EM_DEBUG_LOG("query = [%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
 
        /*  execute a sql and count rows */
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
@@ -15958,13 +16712,12 @@ FINISH_OFF:
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
        EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-       _DISCONNECT_DB;
 
        if (err_code != NULL)
                *err_code = error;
@@ -15973,7 +16726,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_delete_certificate(char *multi_user_name, int certificate_id, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
 
@@ -15986,11 +16739,10 @@ INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transacti
 
        int rc = -1, ret = false;
        int error = EMAIL_ERROR_NONE;
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
-       DB_STMT hStmt = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
        char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
@@ -16003,12 +16755,11 @@ INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transacti
        /*  delete from db */
        memset(sql_query_string, 0x00, sizeof(sql_query_string));
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
-               ("sqlite3_exec fail:%d", rc));
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
 
        /*  validate account existence */
        rc = sqlite3_changes(local_db_handle);
@@ -16017,27 +16768,11 @@ INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transacti
                error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
                goto FINISH_OFF;
        }
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
-       /*  delete from secure storage */
-       if (ssm_delete_file(cert_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
-               EM_DEBUG_EXCEPTION_SEC(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
-               goto FINISH_OFF;
-       }
-#endif
+
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
-
-       if (hStmt != NULL)  {
-               rc = sqlite3_finalize(hStmt);
-               if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
-                       error = EMAIL_ERROR_DB_FAILURE;
-               }
-       }
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (err_code != NULL)
                *err_code = error;
@@ -16046,7 +16781,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
+INTERNAL_FUNC int emstorage_update_certificate(char *multi_user_name, int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
 
@@ -16065,8 +16800,9 @@ INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_cer
 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
        char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
 #endif
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_certificate_tbl SET"
@@ -16115,33 +16851,15 @@ INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_cer
                goto FINISH_OFF;
        }
 
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
-       /*  get password file name */
-       if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
-               goto FINISH_OFF;
-       }
-
-       /*  save passwords to the secure storage */
-       if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
-               EM_DEBUG_EXCEPTION_SEC("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
-               error = EMAIL_ERROR_SYSTEM_FAILURE;
-               goto FINISH_OFF;
-       }
-#endif
-
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
 
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
-
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        error = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -16159,7 +16877,7 @@ FINISH_OFF:
 }
 
 /* Tasks --------------------------------------------------------------------------*/
-INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
+INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
        int ret = 0;
@@ -16178,8 +16896,8 @@ INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_ta
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -16211,7 +16929,7 @@ INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_ta
        EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
                ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
 
-       EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
+       EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
 
 
        _bind_stmt_field_data_int(hStmt, i++, task_id);
@@ -16232,13 +16950,12 @@ INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_ta
        ret = (err == EMAIL_ERROR_NONE);
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
 
        if (hStmt != NULL)  {
                rc = sqlite3_finalize(hStmt);
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        err = EMAIL_ERROR_DB_FAILURE;
                }
        }
@@ -16247,12 +16964,13 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
+INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
-       int rc, ret = false;
+       int ret = false;
        int err = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
+
        sqlite3 *local_db_handle = NULL;
 
        if (task_id < 0)  {
@@ -16260,35 +16978,35 @@ INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
-
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
-       _DISCONNECT_DB;
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
+INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
 {
        EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
-       int rc, ret = false;
+       int ret = false;
        int err = EMAIL_ERROR_NONE;
        char sql_query_string[QUERY_SIZE] = {0, };
 
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
-       EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string),
                "UPDATE mail_task_tbl SET"
@@ -16296,22 +17014,22 @@ INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_ty
                " WHERE task_id = %d"
                , task_status
                , task_id);
-       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-               ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+       err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                       goto FINISH_OFF;
+       }
 
        ret = true;
 
 FINISH_OFF:
-       EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
-       _DISCONNECT_DB;
-
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count)
+INTERNAL_FUNC int emstorage_query_task(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count)
 {
        EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], output_task_list[%p], output_task_count[%d]", input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
        int i = 0, count = 0, rc = -1;
@@ -16328,10 +17046,10 @@ INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, con
        EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
        EM_IF_NULL_RETURN_VALUE(output_task_count, false);
 
-       local_db_handle = emstorage_get_db_connection();
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
-       EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
+       EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
        EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -16349,7 +17067,7 @@ INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, con
        }
 
        SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
-       EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
+       EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
 
        EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
 
@@ -16397,26 +17115,26 @@ FINISH_OFF:
                        *output_task_list = task_item_from_tbl;
                *output_task_count = count;
        }
-       else
-               EM_SAFE_FREE(task_item_from_tbl);
-
+       else {
+                for (i = 0; i < count; i++) {
+                       EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
+                }
+        }
        if (hStmt != NULL)  {
-               EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
                rc = sqlite3_finalize(hStmt);
                hStmt = NULL;
                if (rc != SQLITE_OK)  {
-                       EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
                        err = EMAIL_ERROR_DB_FAILURE;
                }
        }
 
-       _DISCONNECT_DB;
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
+INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
 {
        EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
        int err = EMAIL_ERROR_NONE;
@@ -16434,8 +17152,7 @@ INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(int accoun
        char *where_pararaph = NULL;
        char sql_query_string[QUERY_SIZE] = {0, };
        char **result = NULL;
-
-       sqlite3 *local_db_handle = emstorage_get_db_connection();
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
 
        SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
 
@@ -16455,7 +17172,7 @@ INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(int accoun
        if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
                strcat(sql_query_string, where_pararaph);
 
-       EM_DEBUG_LOG("query[%s]", sql_query_string);
+       EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
 
         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
@@ -16474,9 +17191,11 @@ INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(int accoun
                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
                         strcat(sql_query_string, where_pararaph);
 
-                EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
-                EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
-                        ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+               err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
+                       goto FINISH_OFF;
+               }
 
        } else {
                err = EMAIL_ERROR_MAIL_NOT_FOUND;
@@ -16487,7 +17206,6 @@ FINISH_OFF:
        sqlite3_free_table(result);
        result = NULL;
 
-       _DISCONNECT_DB;
 
        EM_SAFE_FREE(where_pararaph);
 
@@ -16499,4 +17217,1063 @@ FINISH_OFF:
 }
 /* Tasks --------------------------------------------------------------------------*/
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
+
+       if (!local_activity || !activity_id) {
+               EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       int i = 0;
+
+       char sql_query_string[QUERY_SIZE] = {0, };
+       DB_STMT hStmt = NULL;
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string),
+               "INSERT INTO mail_auto_download_activity_tbl VALUES "
+               "( "
+               "? "  /* Activity ID */
+               ",?"  /* Status */
+               ",?"  /* Account ID */
+               ",?"  /* Local Mail ID */
+               ",?"  /* Server mail ID */
+               ",?"  /* Mailbox ID*/
+               ",?"  /* Multi USER NAME */
+               ") ");
+
+       char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
+       char **result = NULL;
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
+
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
+
+       if (NULL==result[1]) rc = 1;
+       else rc = atoi(result[1])+1;
+       sqlite3_free_table(result);
+       result = NULL;
+
+       *activity_id = local_activity->activity_id = rc;
+
+       EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
+                       local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
+
+       if (local_activity->mailbox_id)
+               EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
+       _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
+       _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+
+       EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d", rc));
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
+
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt = NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
+               EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
+
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       if (activity_id == 0)
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
+       else
+               SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+               goto FINISH_OFF;
+       }
+
+       /*  validate activity existence */
+       rc = sqlite3_changes(local_db_handle);
+       if (rc == 0)  {
+               EM_DEBUG_EXCEPTION("No matching activity found");
+               error = EMAIL_ERROR_DATA_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID) {
+               EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
+       rc = sqlite3_changes(local_db_handle);
+       if (rc == 0) {
+               EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
+               error = EMAIL_ERROR_DATA_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
+               EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
+       rc = sqlite3_changes(local_db_handle);
+       if (rc == 0) {
+               EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
+               error = EMAIL_ERROR_DATA_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+
+       ret = true;
+
+FINISH_OFF:
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
+               EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
+
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       char **result;
+       int error = EMAIL_ERROR_NONE;
+       int i = 0;
+       DB_STMT hStmt = NULL;
+       email_event_auto_download *event_list = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       *count = atoi(result[1]);
+       sqlite3_free_table(result);
+
+       EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
+
+       if (!*count) {
+               EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
+               error = EMAIL_ERROR_MAIL_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+       EM_DEBUG_LOG("Activity Count = %d", *count);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+
+       EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d", rc));
+
+       if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
+               EM_DEBUG_EXCEPTION("Malloc failed");
+
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       for (i=0; i < (*count); i++) {
+               _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+               _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+               _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+               _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+               _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+               _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
+
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+               EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
+                       ("sqlite3_step fail:%d", rc));
+       }
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (true == ret)
+         *event_start = event_list;
+       else {
+               EM_SAFE_FREE(event_list);
+               *event_start = NULL;
+               *count = 0;
+       }
+
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt = NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
+
+       if (!activity_count || !err_code) {
+               EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       DB_STMT hStmt = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
+
+       EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+       EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
+               ("sqlite3_step fail:%d", rc));
+
+       _get_stmt_field_data_int(hStmt, activity_count, 0);
+
+       EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt=NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
+
+       if (!account_list || !count) {
+               EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
+       char **result;
+       int i = 0, rc = -1;
+       int *result_account_list = NULL;
+       DB_STMT hStmt = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
+
+       *count = atoi(result[1]);
+       sqlite3_free_table(result);
+
+       if (!*count) {
+               EM_DEBUG_EXCEPTION("no account found...");
+               error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+
+       EM_DEBUG_LOG("Account count [%d]", *count);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+
+       EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d", rc));
+
+       if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
+               EM_DEBUG_EXCEPTION(" em_malloc failed...");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       for (i = 0; i < (*count); i++) {
+               _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
+
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+               EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+                       ("sqlite3_step fail:%d", rc));
+               EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
+       }
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (ret == true)
+               *account_list = result_account_list;
+       else
+               EM_SAFE_FREE(result_account_list);
+
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt = NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+       if (err_code != NULL)
+               *err_code = error;
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
+               EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char **result;
+       int i = 0, rc = -1;
+       int *mbox_list = NULL;
+       DB_STMT hStmt = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       *count = atoi(result[1]);
+       sqlite3_free_table(result);
+
+       if (!*count) {
+               EM_DEBUG_EXCEPTION(" no mailbox_name found...");
+               error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
+               ret = true;
+               goto FINISH_OFF;
+       }
+       EM_DEBUG_LOG("Mailbox count = %d", *count);
+
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
+
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+
+
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d", rc));
+
+       mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
+       if (mbox_list == NULL) {
+               EM_DEBUG_EXCEPTION(" em_malloc failed...");
+               error = EMAIL_ERROR_OUT_OF_MEMORY;
+               goto FINISH_OFF;
+       }
+
+       for (i = 0; i < (*count); i++) {
+               _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+
+               EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+                       ("sqlite3_step fail:%d", rc));
+               EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
+       }
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (ret == true)
+               *mailbox_list = mbox_list;
+       else
+               EM_SAFE_FREE(mbox_list);
+
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt = NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+       if (err_code != NULL)
+               *err_code = error;
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
+
+       if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
+               EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
+               if (err_code != NULL)
+                       *err_code = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       int rc = -1;
+       int ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       DB_STMT hStmt = NULL;
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_READ_TRANSACTION(transaction);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+       SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
+
+       EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
+       EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
+       EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
+       EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
+               ("sqlite3_step fail:%d", rc));
+
+       _get_stmt_field_data_int(hStmt, activity_count, 0);
+
+       EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
+
+       ret = true;
+
+FINISH_OFF:
+
+       if (hStmt != NULL) {
+               rc = sqlite3_finalize(hStmt);
+               hStmt=NULL;
+               if (rc != SQLITE_OK) {
+                       EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
+                       error = EMAIL_ERROR_DB_FAILURE;
+               }
+       }
+
+       EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+
+INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
+{
+       EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
+
+       int rc = -1, ret = false;
+       int error = EMAIL_ERROR_NONE;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       int transaction = true;
+
+       if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
+               EM_DEBUG_EXCEPTION("Invalid parameters");
+               error = EMAIL_ERROR_INVALID_PARAM;
+               return false;
+       }
+
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
+       memset(sql_query_string, 0x00, sizeof(sql_query_string));
+
+       if (mailbox_id > 0)
+               SNPRINTF(sql_query_string, sizeof(sql_query_string),
+                        "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
+       else if (mailbox_name)
+               SNPRINTF(sql_query_string, sizeof(sql_query_string),
+                        "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
+
+       EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+       if (error != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
+                       goto FINISH_OFF;
+       }
+
+       rc = sqlite3_changes(local_db_handle);
+       if (rc == 0) {
+               EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
+       }
+
+       ret = true;
+
+FINISH_OFF:
+
+       EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
+
+       if (err_code != NULL)
+               *err_code = error;
+
+       EM_DEBUG_FUNC_END("ret [%d]", ret);
+       return ret;
+}
+
+#endif
+
+#ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
+
+typedef struct {
+       char *column_name;
+       char *column_type;
+} email_column_info_t;
+
+static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
+{
+       EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
+
+       int i = 0;
+       int validated = 0;
+       char *column_name = NULL;
+       char *column_type = NULL;
+       GList *new_list = *((GList**)arg1);
+       email_column_info_t *column_info_item = NULL;
+
+       for (i = 0; i < argc; ++i)  {
+               /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
+               if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
+                       column_name = EM_SAFE_STRDUP(argv[i]);
+                       validated = 1;
+               }
+               else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
+                       column_type = EM_SAFE_STRDUP(argv[i]);
+               }
+       }
+
+       if (validated) {
+               EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
+               column_info_item = em_malloc(sizeof(email_column_info_t));
+               column_info_item->column_name = column_name;
+               column_info_item->column_type = column_type;
+               new_list = g_list_append(new_list, (gpointer)column_info_item);
+               *((GList**)arg1) = new_list;
+       }
+
+       EM_DEBUG_FUNC_END();
+       return 0;
+}
+
+static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
+{
+       EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
+       int err = EMAIL_ERROR_NONE;
+       int result_from_sqlite = 0;
+       char *error_message_from_sqlite = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+       GList *new_list = NULL;
+       sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
+
+       result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
+
+       if (result_from_sqlite != SQLITE_OK)
+               EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
+
+       EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
+
+       if (new_list && output_column_info) {
+               EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
+               *output_column_info = new_list;
+       }
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
+static  int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
+{
+       EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
+       int error = EMAIL_ERROR_NONE;
+       int rc = -1;
+       sqlite3 *local_db_handle = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               error = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
+
+       EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
+
+       EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
+       reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
+
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+FINISH_OFF:
+
+       if (error == EMAIL_ERROR_NONE) {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
+       }
+       else {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
+       }
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+
+static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
+{
+       EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
+       int error = EMAIL_ERROR_NONE;
+       int rc = -1;
+       sqlite3 *local_db_handle = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       if (input_table_name == NULL || input_new_column == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               error = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
+       SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+FINISH_OFF:
+
+       if (error == EMAIL_ERROR_NONE) {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
+       }
+       else {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
+       }
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+
+static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
+{
+       EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
+       int error = EMAIL_ERROR_NONE;
+       int rc = -1;
+       sqlite3 *local_db_handle = NULL;
+       char sql_query_string[QUERY_SIZE] = {0, };
+
+       if (input_table_name == NULL) {
+               EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
+               error = EMAIL_ERROR_INVALID_PARAM;
+               goto FINISH_OFF;
+       }
+
+       local_db_handle = emstorage_get_db_connection(multi_user_name);
+
+       EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
+       EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
+       SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
+       error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
+FINISH_OFF:
+
+       if (error == EMAIL_ERROR_NONE) {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
+       }
+       else {
+               EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
+       }
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+
+gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
+{
+       EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
+       email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
+       email_column_info_t *right_one = (email_column_info_t*)new_column_info;
+
+       if (old_column_info == NULL || new_column_info == NULL)
+               return -1;
+
+       return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
+}
+
+INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int i = 0;
+       int j = 0;
+       int error = EMAIL_ERROR_NONE;
+       int query_len = 0;
+       email_column_info_t *new_column_info = NULL;
+       email_column_info_t *p_column_info = NULL;
+       char **create_table_query = NULL;
+       GList *found_data = NULL;
+       GList *column_list_of_old_table = NULL;
+       GList *column_list_of_new_table = NULL;
+       char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
+               { "mail_box_tbl", "mail_box_tbl_new" },
+               { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
+               { "mail_rule_tbl", "mail_rule_tbl_new" },
+               { "mail_tbl", "mail_tbl_new" },
+               { "mail_attachment_tbl", "mail_attachment_tbl_new" },
+#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
+               { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
+#else
+               { "", "" },
+#endif
+               { "mail_meeting_tbl", "mail_meeting_tbl_new" },
+#ifdef __FEATURE_LOCAL_ACTIVITY__
+               { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
+#else
+               { "", "" },
+#endif
+               { "mail_certificate_tbl", "mail_certificate_tbl_new" },
+               { "mail_task_tbl", "mail_task_tbl_new" },
+#ifdef __FEATURE_BODY_SEARCH__
+               { "mail_text_tbl", "mail_text_tbl_new" },
+#else
+               { "", "" },
+#endif
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+               { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
+#else
+               { "", "" }
+#endif
+
+       };
+
+       error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
+
+       if (error != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
+               goto FINISH_OFF;
+       }
+
+       if (query_len < CREATE_TABLE_MAX) {
+               EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
+               error = EMAIL_ERROR_SYSTEM_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
+               EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
+               if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
+                       /* Check existing of _new table */
+                       emstorage_drop_table(multi_user_name, table_names[i][1]);
+                       error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
+                       if (error != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
+                               goto FINISH_OFF;
+                       }
+
+                       emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
+                       emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
+
+                       /* Compare fields and add new field */
+                       for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
+                               new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
+                               found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
+                               if (found_data == NULL) {
+                                       /* add new field*/
+                                       emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
+                               }
+                       }
+
+                       emstorage_drop_table(multi_user_name, table_names[i][1]);
+               }
+               else
+                       EM_DEBUG_LOG("Skipped");
+       }
+
+FINISH_OFF:
+       if (create_table_query) {
+               int i = 0;
+               for (i = 0; i < query_len; i++) {
+                       if (create_table_query[i]) {
+                               EM_SAFE_FREE(create_table_query[i]);
+                       }
+               }
+               EM_SAFE_FREE(create_table_query);
+       }
+
+       found_data = g_list_first(column_list_of_old_table);
+       while(found_data != NULL) {
+               p_column_info = (email_column_info_t *)found_data->data;
+               EM_SAFE_FREE(p_column_info->column_name);
+               EM_SAFE_FREE(p_column_info->column_type);
+               EM_SAFE_FREE(p_column_info);
+
+               found_data = g_list_next(found_data);
+       }
+       g_list_free(column_list_of_old_table);
+
+       found_data = g_list_first(column_list_of_new_table);
+       while(found_data != NULL) {
+               p_column_info = (email_column_info_t *)found_data->data;
+               EM_SAFE_FREE(p_column_info->column_name);
+               EM_SAFE_FREE(p_column_info->column_type);
+               EM_SAFE_FREE(p_column_info);
+
+               found_data = g_list_next(found_data);
+       }
+       g_list_free(column_list_of_new_table);
+
+       EM_DEBUG_FUNC_END("error [%d]", error);
+       return error;
+}
+#endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
+
 /*EOF*/
index f6b0154..1708379 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -24,9 +24,9 @@
  * File :  email-storage.h
  * Desc :  email-core Storage Library Header
  *
- * Auth : 
+ * Auth :
  *
- * History : 
+ * History :
  *    2006.07.28  :  created
  *****************************************************************************/
 #ifndef __EMAIL_STORAGE_H__
@@ -43,6 +43,9 @@ extern "C"
 #include "email-types.h"
 #include "email-core-tasks.h"
 #include "email-internal-types.h"
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+#include "email-core-auto-download.h"
+#endif
 
 #define FIRST_ACCOUNT_ID    1
 #define EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH tzplatform_mkpath(TZ_USER_DATA,"email/res/email-service.sql")
@@ -51,8 +54,9 @@ extern "C"
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
 #define QUERY_SIZE          8192
 #define MAX_INTEGER_LENGTH  5  /*  32767 -> 5 bytes */
+#define FILE_MAX_BUFFER_SIZE     16 * 1024 /* 16 Kbyte */
 
-typedef struct 
+typedef struct
 {
        int mail_id;
        unsigned long server_mail_id;
@@ -61,7 +65,7 @@ typedef struct
 #endif /* __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__ */
 
 
-typedef struct 
+typedef struct
 {
        /* Account information */
        int                                account_id;                               /* Account id  */
@@ -72,6 +76,7 @@ typedef struct
        int                                sync_disabled;                            /* If this attriube is set as true, email-service will not synchronize this account. */
        int                                default_mail_slot_size;
        email_roaming_option_t             roaming_option;                           /* roaming option */
+       int                                color_label;                              /* Account color label */
        void                              *user_data;                                /* binary user data */
        int                                user_data_length;                         /* user data length */
 
@@ -114,6 +119,9 @@ typedef struct
        int                                auto_resend_times;                        /* Auto retry count for sending a email */
        int                                outgoing_server_size_limit;               /* Mail size limitation for SMTP sending*/
 
+       /* Auto download */
+       int                                wifi_auto_download;                        /* auto attachment download in wifi connection */
+
        /* Authentication Options */
        int                                pop_before_smtp;                          /* POP before SMTP Authentication */
        int                                incoming_server_requires_apop;            /* APOP authentication */
@@ -124,9 +132,12 @@ typedef struct
        char                              *certificate_path;                         /* Sepeifies the certificate path of private*/
        email_cipher_type                  cipher_type;                              /* Sepeifies the encryption algorithm*/
        email_digest_type                  digest_type;                              /* Sepeifies the digest algorithm*/
+
+    /* Multi user and KNOX options */
+    char                              *user_name;                                /* Specifies the container name */ 
 } emstorage_account_tbl_t;
 
-typedef struct 
+typedef struct
 {
        int certificate_id;
        int issue_year;
@@ -142,7 +153,7 @@ typedef struct
        char *password;
 } emstorage_certificate_tbl_t;
 
-typedef struct 
+typedef struct
 {
        int   account_id;      /*  MUST BE '0'  :  currently, only global rule supported. */
        int   rule_id;
@@ -157,7 +168,7 @@ typedef struct
 } emstorage_rule_tbl_t;
 
 /* mail_box_tbl table entity */
-typedef struct 
+typedef struct
 {
        int                   mailbox_id;
        int                   account_id;
@@ -179,7 +190,7 @@ typedef struct
 } emstorage_mailbox_tbl_t;
 
 /* mail_read_uid_tbl table entity */
-typedef struct 
+typedef struct
 {
        int     account_id;
        int     mailbox_id;
@@ -202,7 +213,7 @@ typedef struct
 } emstorage_mail_text_tbl_t;
 #endif
 
-typedef struct 
+typedef struct
 {
        int                    mail_id;
        int                    account_id;
@@ -256,16 +267,20 @@ typedef struct
        int                    tag_id;
        time_t                 replied_time;
        time_t                 forwarded_time;
+       char                  *default_charset;
        int                    eas_data_length;
        char                  *eas_data;
+       char                  *pgp_password;
+    char                  *user_name;
 } emstorage_mail_tbl_t;
 
 /* mail_attachment_tbl entity */
-typedef struct 
+typedef struct
 {
        int   attachment_id;
        char *attachment_name;
        char *attachment_path;
+       char *content_id;
        int   attachment_size;
        int   mail_id;
        int   account_id;
@@ -275,10 +290,10 @@ typedef struct
        int   attachment_drm_method;               /* 0 : none, 1 : FL, 2 : CD, 3 : SSD, 4 : SD */
        int   attachment_inline_content_status;    /* 1 : inline content , 0 : not inline content */
        char *attachment_mime_type;
-#ifdef __ATTACHMENT_OPTI__     
+#ifdef __ATTACHMENT_OPTI__
        int   encoding;
-       char *section;  
-#endif 
+       char *section;
+#endif
 } emstorage_attachment_tbl_t;
 
 
@@ -300,8 +315,8 @@ typedef struct _emstorage_search_filter_t {
 } emstorage_search_filter_t;
 
 typedef enum {
-       EMAIL_CREATE_DB_NORMAL = 0, 
-       EMAIL_CREATE_DB_CHECK, 
+       EMAIL_CREATE_DB_NORMAL = 0,
+       EMAIL_CREATE_DB_CHECK,
 } emstorage_create_db_type_t;
 
 
@@ -315,8 +330,8 @@ typedef enum {
 typedef enum
 {
  ACTIVITY_FETCHIMAPFOLDER = 1,  /* Fetch Mail server Activity */
- ACTIVITY_DELETEMAIL,                  /* Delete Mail Activity */              
- ACTIVITY_MODIFYFLAG,                  /* Modify Mail flag Activity */         
+ ACTIVITY_DELETEMAIL,                  /* Delete Mail Activity */
+ ACTIVITY_MODIFYFLAG,                  /* Modify Mail flag Activity */
  ACTIVITY_MODIFYSEENFLAG,              /* Modify Mail seen flag Activity */
  ACTIVITY_MOVEMAIL,                            /* Move Mail Activity */
  ACTIVITY_DELETEALLMAIL,               /* Delete All Mails activity */
@@ -337,7 +352,7 @@ typedef struct
        int mail_id;                             /* Mail ID    */
        int activity_type;    /* Local Activity Type */
        char *server_mailid;                 /* Server Mail ID or Mailbox name */
-       char *src_mbox;                          /* Source Mailbox in case of mail_move */      
+       char *src_mbox;                          /* Source Mailbox in case of mail_move */
        char *dest_mbox;                 /* Destination Mailbox name in case of Mail move operation */
 
 } emstorage_activity_tbl_t;
@@ -352,41 +367,41 @@ INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name);
  * em_db_open
  * description  :  open db and register busy handler
  */
-INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code);
+INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code);
 
 /*
  * emstorage_db_open
  *
- * description :  open db and set global variable sqlite_emmb 
- * arguments : 
- * return  : 
+ * description :  open db and set global variable sqlite_emmb
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code);
+INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code);
 
 /*
  * emstorage_db_close
  *
  * description :  close db with global variable sqlite_emmb
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_db_close(int *err_code);
+INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code);
 
 /*
  * emstorage_open
  *
  * description :  initialize storage manager
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_open(int *err_code);
+INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code);
 
 /*
  * emstorage_close
  *
  * description :  cleanup storage manager
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
 INTERNAL_FUNC int emstorage_close(int *err_code);
 
@@ -394,25 +409,23 @@ INTERNAL_FUNC int emstorage_close(int *err_code);
  * emstorage_create_table
  *
  * description :  create account/address table in database.
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code);
-
+INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code);
 
 INTERNAL_FUNC int emstorage_initialize_field_count();
 
 /**
  * Check whether there is the same account information in the db
  *
- * @param[in] account  account that 
+ * @param[in] account  account that
  * @param[in] transaction      If the argument is true, engine commits transaction or executes rollback.
  * @param[out] err_code        Error code.
  * @remarks N/A
- * @return This function returns true if there is no duplicated account. returns false and set error code if there are duplicated accounts or error 
+ * @return This function returns true if there is no duplicated account. returns false and set error code if there are duplicated accounts or error
  */
-INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t *account, int transaction, int *err_code);
-
+INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t *account, int transaction, int *err_code);
 
 /**
  * Get number of accounts from account table.
@@ -422,7 +435,7 @@ INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t *account, i
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code);
 
 /**
  * Get account from account table.
@@ -434,24 +447,22 @@ INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code);
+INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code);
 
 /*
  * emstorage_get_account_by_name
  *
  * description :  get account from account table by account name
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    account_id  :  account id
  *    pulloption  :  option to specify fetching full or partial data
  *    account  :  buffer to hold selected account
  *              this buffer must be free after it has been used.
- * return  : 
+ * return  :
  */
 /* sowmya.kr, 281209 Adding signature to options in email_account_t changes */
-
-INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code);
-
+INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code);
 
 /*
  * emstorage_get_password_length_of_account
@@ -463,77 +474,74 @@ INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, em
  * return :
  */
 
-INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int password_type, int *password_length, int* err_code);
+INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int* err_code);
 
-INTERNAL_FUNC int emstorage_update_account_password(int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password);
+INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password);
 
 /*
  * emstorage_update_account
  *
  * description :  change a account from account table
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    account  :  buffer to hold selected account
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t *account, int transaction, int *err_code);
-
-
-INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction);
+INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account, int transaction, int *err_code);
 
+INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction);
 /*
  * emstorage_get_sync_status_of_account
  *
  * description :  get a sync status field from account table
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    result_sync_status : sync status value
- * return  : 
+ * return  :
  */
-
-INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code);
+INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status,int *err_code);
 
 /*
  * emstorage_update_sync_status_of_account
  *
  * description :  update a sync status field from account table
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    set_operator  :  set operater. refer email_set_type_t
  *    sync_status : sync status value
- * return  : 
+ * return  :
  */
+INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code);
 
 /*
  * emstorage_add_account
  *
  * description :  add a account to account table
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t *account, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account, int transaction, int *err_code);
 
 /*
  * emstorage_delete_account
  *
  * description :  delete a account from account table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    account_id  :  account id to be deteted
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code);
 
 /*
  * emstorage_free_account
  *
  * description :  free local accout memory
- * arguments : 
+ * arguments :
  *    account_list  :  double pointer
  *    count  :  count of local account
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t **account_list, int count, int *err_code);
 
@@ -544,86 +552,92 @@ INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t **account_list,
  * emstorage_get_mailbox_count
  *
  * description :  get number of mailbox from local mailbox table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    count  :  number of accounts
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code);
 
 /*
  * emstorage_get_mailbox
  *
  * description :  get local mailbox from local mailbox table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    sort_type         :      in - sorting type.
  *    select_num        :      in - number of selected account
  *                             out - number to been selected
  *    mailbox_list      :      buffer to hold selected account
  *                     this buffer must be free after it has been used.
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mailbox_list(int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t **mailbox_list,  int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mailbox_list(char *multi_user_name, int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t **mailbox_list,  int transaction, int *err_code);
 
 /*
  * emstorage_get_child_mailbox_list
  *
- * description :  get child mailbox list  from given  mailbox 
- * arguments : 
+ * description :  get child mailbox list  from given  mailbox
+ * arguments :
  *    account_id  :  in - account id
  *    parent_mailbox_name  :  in - parent_mailbox_name to fetch child list
  *    local_yn - in - local mailbox or not
  *    select_num - out   :  count of mailboxes
- *    mailbox_list - out  :    list of mailboxes        
+ *    mailbox_list - out  :    list of mailboxes
  *    err_code - out  :  error code, if any
  */
-
-
-INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_child_mailbox_list(char *multi_user_name, int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
 
 /*
  * emstorage_get_mailbox_by_name
  *
  * description :  get local mailbox from local mailbox table by mailbox name
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name
  *    mailbox  :  buffer to hold selected local mailbox
  *              this buffer must be free after it has been used.
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **mailbox, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **mailbox, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox);
-INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **mailbox, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **mailbox, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox);
+
+INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, 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);
 
 
 /*
  * emstorage_update_mailbox
  *
  * description :  change a account from account table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name
  *    mailbox  :  buffer to hold selected local mailbox
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code);
 
 /*
  * emstorage_update_mailbox_type
  *
  * description :  change a mailbox from mailbox tbl
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code);
 
 /*
  * emstorage_set_local_mailbox
@@ -632,108 +646,109 @@ INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, in
  * arguments :
  * return  :
  */
-INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction);
+INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction);
 
-INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction);
+INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction);
 
 /*
  * emstorage_add_mailbox
  *
  * description :  add a local mailbox to local mailbox table
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code);
 
 /*
  * emstorage_delete_mailbox
  *
  * description :  delete a local mailbox from local mailbox table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name of record to be deteted
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction);
+
+INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction);
-INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int modifiable_yn, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction);
+INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction);
 
 /*
  * emstorage_free_mailbox
  *
  * description :  free local mailbox memory
- * arguments : 
+ * arguments :
  *    mailbox_list  :  double pointer
  *    count  :  count of local mailbox
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t **mailbox_list, int count, int *err_code);
 
 
 /************** Read Mail UID Management ******************/
-
-
-
-INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code);
 
 
 /*
  * emstorage_check_read_mail_uid
  *
  * description :  check that this uid exists in uid table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name
  *    uid  :  uid string to be checked
  *    exist  :  variable to hold checking result. (0 : not exist, 1 : exist)
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code);
 
 /*
  * emstorage_get_read_mail_size
  *
  * description :  get mail size from read mail uid
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_id  :  local mailbox id
  *    local_uid  :  mail uid of local mailbox
  *    mailbox_name  :  server mailbox name
  *    uid  :  mail uid string of server mail
  *    read_mail_uid  :  variable to hold read mail uid information
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
 
 /*
  * emstorage_get_read_mail_size
  *
  * description :  get mail size from read mail uid
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name
  *    s_uid  :  mail uid string
  *    size  :  variable to hold mail size
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *local_mbox, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *local_mbox, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code);
 
 /*
  * emstorage_add_downloaded_mail
  *
  * description :  add read mail uid
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code);
 
 #ifdef __FEATURE_BODY_SEARCH__
 /*
@@ -743,30 +758,29 @@ INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t *r
  * arguments :
  * return  :
  */
-INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code);
 #endif
 
 /*
  * emstorage_change_read_mail_uid
  *
  * description :  modify read mail uid
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_change_read_mail_uid(int account_id, int local_mailbox_id, int local_uid, char *mailbox_name, char *uid,
+INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int account_id, int local_mailbox_id, int local_uid, char *mailbox_name, char *uid,
                                     emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code);
 
 /*
  * emstorage_remove_downloaded_mail
  *
- * description :  
- * arguments : 
- * return  : 
+ * description :
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code);
 
-
-INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code);
+INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code);
 
 /*
  *  free memroy
@@ -781,84 +795,84 @@ INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t **r
  * emstorage_get_rule_count_by_account_id
  *
  * description :  get number of rules from rule table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    count  :  number of accounts
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code);
 
 /*
  * emstorage_get_rule
  *
  * description :  get rules from rule table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    start_idx  :  the first index to be selected (min : 0)
  *    select_num  :  in - number of selected account
  *                 out - number to been selected
- *    is_completed  :  is completed ? 
- * return  : 
+ *    is_completed  :  is completed ?
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_rule(int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t **rule_list, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t **rule_list, int transaction, int *err_code);
 
 /*
  * emstorage_get_rule
  *
  * description :  get rules from rule table
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t **rule, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t **rule, int transaction, int *err_code);
 
 /*
  * emstorage_change_rule
  *
  * description :  change a account from account table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    mailbox_name  :  mailbox name
  *    rule  :  buffer to hold selected rule
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t *rule, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t *rule, int transaction, int *err_code);
 
 /*
  * emstorage_find_rule
  *
  * description :  find a rule already exists
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t *rule, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t *rule, int transaction, int *err_code);
 
 /*
  * emstorage_add_rule
  *
  * description :  add a rule to rule table
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t *rule, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t *rule, int transaction, int *err_code);
 
 /*
  * emstorage_delete_rule
  *
  * description :  delete a rule from rule table
- * arguments : 
+ * arguments :
  *    db  :  database pointer
  *    rule  :  rule to be deteted
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code);
 
 /*
  * emstorage_free_rule
  *
  * description :  free rule memory
- * arguments : 
+ * arguments :
  *    count  :  count of rule
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t **rule_list, int count, int *err_code);
 
@@ -869,23 +883,23 @@ INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t **rule_list, int coun
  * emstorage_get_mail_count
  *
  * description :  get mail total and unseen count from mail table
- * arguments : 
+ * arguments :
  *    total  :  total count
  *    unseen  :  unseen mail count
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code);
 
 /*
  * emstorage_get_mail_by_id
  *
  * description :  get mail from mail table by mail id
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id
  *    mail  :  double pointer to hold mail
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
 
 #ifdef __FEATURE_BODY_SEARCH__
 /*
@@ -897,64 +911,66 @@ INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t **m
  *    mail_text  :  double pointer to hold mail_text
  * return  :
  */
-INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code);
 #endif
 
 /*
  * emstorage_get_mail
  *
  * description :  get mail from mail table by mail sequence
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    mailbox  :  mailbox name
  *    mail_no  :  mail sequence number (not mail id)
  *    mail  :  double pointer to hold mail
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
 
 /*
  * emstorage_get_mail_field_by_multiple_mail_id
  *
- * description :  
- * arguments : 
- *    mail_ids  : 
- *    number_of_mails  :  
- *    type  :  
- *    mail  :  
+ * description :
+ * arguments :
+ *    mail_ids  :
+ *    number_of_mails  :
+ *    type  :
+ *    mail  :
  *    transaction :
  *    err_code :
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code);
 
 /*
  * emstorage_query_mail_count
  *
  * description :  query mail count
  */
-INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count);
-
+INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count);
 /*
  * emstorage_query_mail_list
  *
  * description :  query mail id list
  */
-INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count);
+INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count);
+
 
 /*
  * emstorage_query_mail_list
  *
  * description :  query mail list information
  */
-INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code);
+INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code);
+
 
 /*
  * emstorage_query_mail_tbl
  *
  * description :  query mail table information
  */
-INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code);
+INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code);
+
 
 #ifdef __FEATURE_BODY_SEARCH__
 /*
@@ -962,7 +978,7 @@ INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int t
  *
  * description :  query mail_text table information
  */
-INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code);
+INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code);
 #endif
 
 /*
@@ -970,25 +986,29 @@ INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause,
  *
  * description :  query mail box table information
  */
-
-INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count);
+INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count);
 
 /*
  * emstorage_get_mail_list
  *
  * description :  search mail list information
  */
+INTERNAL_FUNC int emstorage_get_mail_list(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, 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_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code);
 /*
  * emstorage_get_mails
  *
  * description :  search mail list information
  */
-INTERNAL_FUNC int emstorage_get_mails(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code);
-INTERNAL_FUNC int emstorage_get_searched_mail_list(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(int account_id, char *server_mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code);
+INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code);
+
+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_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code);
+
+INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code);
 
 
 /**
@@ -996,23 +1016,23 @@ INTERNAL_FUNC int emstorage_get_latest_unread_mailid(int account_id, int *mail_i
  *
  * @param[in] search                   Specifies the searching condition.
  * @param[in] account_id               Specifies the account id. if 0, all accounts.
- * @param[in] mailbox                  Specifies the mailbox name. if NULL, all mailboxes.
+ * @param[in] mailbox_id               Specifies the mailbox ID. if NULL, all mailboxes.
  * @param[in] sorting                  Specifies the sorting condition.
  * @param[out] search_handle   the searching handle is saved here.
  * @param[out] searched                        the result count is saved here.
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t *search, int account_id, char *mailbox, int sorting, int *search_handle, int *searched, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name, emstorage_search_filter_t *search, int account_id, int mailbox_id, int sorting, int *search_handle, int *searched, int transaction, int *err_code);
 
 /*
  * emstorage_mail_search_result
  *
  * description :  retrieve mail as searching result
- * arguments : 
+ * arguments :
  *    search_handle  :  handle to been gotten from emstorage_mail_search_start
  *    mail  :  double pointer to hold mail
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void **data, int transaction, int *err_code);
 
@@ -1020,27 +1040,25 @@ INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail
  * emstorage_mail_search_end
  *
  * description :  finish searching
- * arguments : 
+ * arguments :
  *    search_handle  :  handle to be finished
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code);
 
-
-
 /*
  * emstorage_set_field_of_mails_with_integer_value
  *
  * description       :  update a filed of mail
- * arguments : 
- *    account_id                :  Specifies the account id. 
+ * arguments :
+ *    account_id                :  Specifies the account id.
  *    mail_ids       :  mail id list to be changed
  *    mail_ids_count :  count of mail list
  *    field_name     :  specified field name
  *    value          :  specified value
- * return       : 
+ * return       :
  */
-INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code);
 
 #ifdef __FEATURE_BODY_SEARCH__
 /*
@@ -1052,124 +1070,125 @@ INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id
  *    mail_text  :  mail_text pointer
  * return  :
  */
-INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code);
 #endif
 
 /*
  * emstorage_change_mail_field
  *
  * description :  update partial mail data
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id
  *    type  :  changing type
  *    mail  :  mail pointer
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code);
 
 /*
  * emstorage_increase_mail_id
  *
  * description :  increase unique mail id
- * arguments : 
+ * arguments :
  *    mail_id  :  pointer to store the unique id
- * return  : 
+ * return  :
  */
 
 /*
  * emstorage_change_mail
  *
  * description :  update mail
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id to be changed
  *    mail  :  mail pointer
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code);
-INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code);
+INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code);
 
-INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code);
+
+INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code);
 
 /*
  * emstorage_add_mail
  *
  * description :  add a mail to mail table
- * arguments : 
+ * arguments :
  *    mail  :  mail pointer to be inserted
  *   get_id :  must get uinque id in function
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail, int get_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail, int get_id, int transaction, int *err_code);
 
 /*
  * emstorage_move_multiple_mails_on_db
  *
  * description :
- * arguments : 
- *    account_id  :  
+ * arguments :
+ *    account_id  :
  *   input_mailbox_id :
  *   mail_ids :
  *   number_of_mails :
  *   transaction :
  *   err_code :
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code);
 
 /*
  * emstorage_delete_mail
  *
  * description :  delete mail from mail table
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id to be deleted. if 0, all mail will be deleted.
  *    from_server  :  delete mail on server.
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code);
 
 /*
  * emstorage_delete_mail_by_account
  *
  * description :  delete mail from mail table by account id
- * arguments : 
+ * arguments :
  *    account_id  :  account id.
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code);
 
 /*
- * emstorage_delete_mail
+ * emstorage_delete_mail_by_mailbox
  *
  * description :  delete mail from mail table
- * arguments : 
- *    account_id  :  account id.
- *    mailbox  :  mail box
- * return  : 
+ * arguments :
+ *    mailbox  :  mailbox
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code);
 
 /*
  * emstorage_delete_multiple_mails
  *
- * description :  
- * arguments : 
+ * description :
+ * arguments :
  *    mail_ids  :
  *    number_of_mails  :
  *    transaction  :
  *    err_code  :
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code);
 
 /*
  * emstorage_free_mail
  *
  * description :  free memory
- * arguments : 
+ * arguments :
  *    mail_list  :  mail array
  *    count  :  the number of array element
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t **mail_list, int count, int *err_code);
 
@@ -1190,13 +1209,13 @@ INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_tex
  * emstorage_get_attachment
  *
  * description :  get attachment from attachment table
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id
  *    no  :  attachment sequence
  *    attachment  :  double pointer to hold attachment data
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code);
 
 /*
  * emstorage_get_attachment_list
@@ -1208,108 +1227,117 @@ INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int tr
  *    attachment_list         : result attachment list
  *    output_attachment_count : result attachment count
  * return  : This function returns EMAIL_ERROR_NONE on success or error code (refer to EMAIL_ERROR__XXX) on failure.
- *    
+ *
  */
-INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count);
+INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count);
 
 
 /*
  * emstorage_get_attachment
  *
  * description :  get attachment from attachment table
- * arguments : 
+ * arguments :
  *    attachment_id  :  attachment id
  *    attachment  :  double pointer to hold attachment data
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code);
 
 /*
  * emstorage_get_attachment
  *
  * description :  get nth-attachment from attachment table
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id
  *    nth  :  index of the desired attachment (min : 1)
  *    attachment  :  double pointer to hold attachment data
- * return  : 
+ * return  :
+ */
+INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code);
+
+/*
+ * emstorage_get_attachment
+ *
+ * description :  get attachment from attachment table
+ * arguments :
+ *    attachment_path  :  attachment path
+ *    attachment       :  double pointer to hold attachment data
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code);
 
 /*
  * emstorage_change_attachment_field
  *
  * description :  update partial mail attachment data
- * arguments : 
+ * arguments :
  *    mail_id  :  mail id
  *    type  :  changing type
  *    mail  :  mail pointer
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code);
 
 /* Get new attachment id */
 /*
  * emstorage_get_new_attachment_no
  *
  * description :  Get new attachment id
- * arguments : 
+ * arguments :
  *    attachment_no  :  attachment id pointer
- * return  : 
+ * return  :
  */
-
-INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code);
+INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code);
 
 /* insert attachment to mail attachment table */
 /*
  * emstorage_add_attachment
  *
  * description :  insert a attachment to attachment table
- * arguments : 
+ * arguments :
  *    attachment  :  attachment pointer
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t *attachment, int iscopy, int transaction, int *err_code);
-
+INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment, int iscopy, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t *attachment, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code);
 
 /* delete a mail from mail table */
 /*
  * emstorage_delete_attachment_on_db
  *
  * description :  delete attachment from attachment table
- * arguments : 
+ * arguments :
  *    attachment_id  :  attachment id
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code);
 
 /*
  * emstorage_delete_attachment_all_on_db
  *
  * description :  delete attachment from mail table
- * arguments : 
+ * arguments :
  *    account_id  :  account id.
  *    mailbox  :  mail box
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code);
 
 /*
  * emstorage_free_attachment
  *
  * description :  free memory
- * arguments : 
+ * arguments :
  *    mail_list  :  mail array
  *    count  :  the number of array element
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t **attachment_list, int count, int *err_code);
 
-INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code);
+INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code);
 
 INTERNAL_FUNC int emstorage_get_max_mail_count();
 
@@ -1324,8 +1352,7 @@ INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_
  * @remarks emstorage_commit_transaction or emstorage_commit_transaction must be called after this function.
  * @return This function returns 0 on success or error code on failure.
  */
-
-INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code);
+INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code);
 
 /**
  * commit a transaction.
@@ -1335,7 +1362,7 @@ INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code);
+INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code);
 
 /**
  * rollback db.
@@ -1345,18 +1372,17 @@ INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code);
+INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code);
 
 /**
  * clear mail data from db.
  *
- * @param[in]  transaction     
- * @param[out] err_code        
+ * @param[in]  transaction
+ * @param[out] err_code
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-
-INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code);
 
 
 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name);
@@ -1365,49 +1391,49 @@ INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name
  * emstorage_get_save_name
  *
  * description :  get file name for saving data
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    mail_id  :  mail id
  *    atch_id  :  attachment id
  *    fname  :  file name
  *    name_buf  :  buffer to hold file name. (MAX : 512Bytes)
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code);
+INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code);
 
 /*
  * emstorage_get_dele_name
  *
  * description :  get a name for deleting contents from file system.
- * arguments : 
+ * arguments :
  *    account_id  :  account id
  *    mail_id  :  mail id
  *    atch_id  :  attachment id
  *    fname  :  reserved
  *    name_buf  :  buffer to hold file name. (MAX : 512Bytes)
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code);
+INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code);
 
 /*
  * emstorage_create_dir
  *
  * description :  create directory
- * arguments : 
+ * arguments :
  *    name_buf  :  buffer to hold file name. (MAX : 512Bytes)
  *    no  :  attachment no.
- * return  : 
+ * return  :
  */
-INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code);
+INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code);
 
 /*
  * emstorage_copy_file
  *
  * description :  copy a attachment file
- * arguments : 
+ * arguments :
  *    src_file  :  source file
  *    dst_file  :  destination file
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_file, int *err_code);
 
@@ -1415,10 +1441,10 @@ INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_f
  * emstorage_move_file
  *
  * description :  move a file
- * arguments : 
+ * arguments :
  *    src_file  :  source file
  *    dst_file  :  destination file
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code);
 
@@ -1426,9 +1452,9 @@ INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_s
  * emstorage_move_file
  *
  * description :  delete a file
- * arguments : 
+ * arguments :
  *    src_file  :  file to be deleted
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code);
 
@@ -1436,60 +1462,78 @@ INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code);
  * emstorage_delete_dir
  *
  * description :  delete a directory
- * arguments : 
+ * arguments :
  *    src_dir  :  directory to be deleted
- * return  : 
+ * return  :
  */
 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code);
 
 
 
 INTERNAL_FUNC void emstorage_flush_db_cache();
-INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code);
 
-INTERNAL_FUNC int emstorage_get_sender_list(int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code);
+INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code);
+
 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count);
 
 /* Handling Thread mail */
-INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t **mail_table_data, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *mail_table_data, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count);
-INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code);
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread_id, int latest_mail_id, int thread_item_count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t **mail_table_data, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name, emstorage_mail_tbl_t *mail_table_data, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count);
+
+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_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);
+
+INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code);
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 
 #define BULK_PARTIAL_BODY_DOWNLOAD_COUNT 10
 enum
 {
-       ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL = 0, 
-       MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL, 
-       SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL, 
-       ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL, 
-       ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL, 
-       MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL, 
+       ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL = 0,
+       MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
+       SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
+       ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
+       ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
+       MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
        MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
+    MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL
 };
 
+INTERNAL_FUNC int   emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd *local_activity, int *activity_id, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_id_count, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code);
+
+INTERNAL_FUNC int   emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code);
 
-INTERNAL_FUNC int   emstorage_get_pbd_activity_data(int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_add_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_get_pbd_activity_count(int *activity_id_count, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code);
-INTERNAL_FUNC int   emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code);
 INTERNAL_FUNC int   emstorage_create_file(char *buf, size_t file_size, char *dst_file, int *err_code);
 
-#endif  
+#endif
 
 INTERNAL_FUNC int   emstorage_free_address_info_list(email_address_info_list_t **address_info_list);
 
 INTERNAL_FUNC void  emstorage_create_dir_if_delete();
 
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
-INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code);
+INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code);
 
 /**
  * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t **server_uids, int *id_set_count, int *err_code);
@@ -1501,43 +1545,49 @@ INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_
  * @param[out] idset                   Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
  * @param[out] id_set_count            Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
  * @param[out] err_code                Returns the error code.
- * @remarks                                    An Example of Query to be exexuted in this API : 
+ * @remarks                                    An Example of Query to be exexuted in this API :
  *                                                     SELECT local_uid, server_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
  * @return This function returns true on success or false on failure.
  */
-
-INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_set_t **idset, int *id_set_count, int *err_code);
+INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char *mail_ids, email_id_set_t **idset, int *id_set_count, int *err_code);
 
 #endif
 
 /**
  * @fn emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, int dst_mailbox_type, int reset, email_rule_t *rule, int **filtered_mail_id_list, int *count_of_mails, int err_code)
- * Move mails by specified rule for spam filtering. 
+ * Move mails by specified rule for spam filtering.
  *
  * @author                                                             kyuho.jo@samsung.com
  * @param[in] account_id                               Account id of the mails and the mailboxes.
  * @param[in] dest_mailbox_id                  Mailbox id of spam mailbox.
  * @param[in] dest_mailbox_type                        Mailbox id of spam mailbox.
- * @param[in] reset                            Tag id reset which when deleting the rule 
+ * @param[in] reset                            Tag id reset which when deleting the rule
  * @param[in] rule                                             Filtering rule.
  * @param[out] filtered_mail_id_list   Mail id list which are filtered by the rule.
  * @param[out] count_of_mails                  Count of mails which are filtered by the rule.
  * @param[out] err_code                                        Returns the error code.
 
- * @remarks                                                                    
+ * @remarks
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int **filtered_mail_id_list, int *count_of_mails, int *err_code);
+INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int **filtered_mail_id_list, int *count_of_mails, int *err_code);
+
+INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code);
+
+INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction);
+
+INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t **meeting_req, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t *meeting_req, int transaction, int *err_code);
+
+INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code);
 
-INTERNAL_FUNC int emstorage_update_tag_id(int old_filter_id, int new_filter_id, int *err_code);
-INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_query_meeting_request(const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction);
-INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t *meeting_req, int transaction, int *err_code);
-INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code);
 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req);
 
-INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause);
+INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause);
+
 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count);
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
@@ -1551,8 +1601,8 @@ INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code
  /*
  *emstorage_get_activity_id_list
  *description  :  get the list of activity ids
- *arguments  : 
- *return  : 
+ *arguments  :
+ *return  :
  *
  */
 INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int **activity_id_list, int *activity_count, int lowest_activity_type, int highest_activity_type, int transaction, int*err_code);
@@ -1560,8 +1610,8 @@ INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int **activity_
  * emstorage_add_activity
  *
  * description :  add an activity to activity table
- * arguments : 
- * return  : 
+ * arguments :
+ * return  :
  */
 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t *local_activity, int transaction, int *err_code);
 
@@ -1580,43 +1630,60 @@ INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t *loca
 
 /**
  * emstorage_free_local_activity - Free the allocated Activity data
- * 
+ *
  *
  */
 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code);
 
 /**
  * emstorage_free_activity_id_list - Free the allocated Activity List data
- * 
+ *
  *
  */
 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code);
 
 #endif
 
-INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_add_certificate(char *multi_user_name, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code);
 
 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code);
 
-INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code);
+INTERNAL_FUNC int emstorage_get_certificate_list(char *multi_user_name, int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code);
 
-INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code);
+INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *multi_user_name, char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code);
 
-INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code);
+INTERNAL_FUNC int emstorage_get_certificate_by_index(char *multi_user_name, int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code);
 
-INTERNAL_FUNC int emstorage_delete_certificate(int index, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_certificate(char *multi_user_name, int index, int transaction, int *err_code);
 
 /* task begin */
-INTERNAL_FUNC int emstorage_add_task(email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id);
+INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id);
 
-INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction);
+INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction);
 
-INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction);
+INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction);
 
-INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count);
+INTERNAL_FUNC int emstorage_query_task(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count);
 /* task end*/
 
-INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *searched_mail_id);
+INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *searched_mail_id);
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code);
+INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code);
+#endif
+
+#ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
+INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name);
+#endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
 
 #ifdef __cplusplus
 }
index 748bfc6..31c08e9 100755 (executable)
@@ -40,13 +40,13 @@ extern "C"
 {
 #endif /* __cplusplus */
 
-INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code);
+INTERNAL_FUNC int emcore_validate_account(char *multi_user_name, int account_id, int handle, int *err_code);
 
-INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, email_event_type_t event_type, int *err_code);
+INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_name, email_account_t *account, email_event_type_t event_type, char **output_imap_cap_string, int event_handle, int *err_code);
 
-INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code);
+INTERNAL_FUNC int emcore_create_account(char *multi_user_name, email_account_t *account, int add_account_to_account_svc, int *err_code);
 
-INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code);
+INTERNAL_FUNC int emcore_delete_account(char *multi_user_name, int account_id, int input_delete_from_account_svc, int *err_code);
 
 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code);
 
@@ -56,33 +56,31 @@ INTERNAL_FUNC void emcore_free_account(email_account_t *account_list);
 
 INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, email_account_t **account_dup, int *err_code);
 
-INTERNAL_FUNC int emcore_init_account_reference();
+INTERNAL_FUNC email_account_t *emcore_get_account_reference(char *multi_user_name, int account_id);
 
-INTERNAL_FUNC int emcore_free_account_reference();
-
-INTERNAL_FUNC email_account_t *emcore_get_account_reference(int account_id);
-
-INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code);
+INTERNAL_FUNC int emcore_get_account_reference_list(char *multi_user_name, email_account_t **account_list, int *count);
 
 INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info);
 
+INTERNAL_FUNC void emcore_cleanup_query_server_info(void);
+
 INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info);
 
-INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id);
+INTERNAL_FUNC int emcore_save_default_account_id(char *multi_user_name, int input_account_id);
 
-INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id);
+INTERNAL_FUNC int emcore_load_default_account_id(char *multi_user_name, int *output_account_id);
 
-INTERNAL_FUNC int emcore_recover_from_secured_storage_failure();
+INTERNAL_FUNC int emcore_recover_from_secured_storage_failure(char *multi_user_name);
 
-INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, email_set_type_t input_set_operator, int input_sync_status);
+INTERNAL_FUNC int emcore_update_sync_status_of_account(char *multi_user_name, int input_account_id, email_set_type_t input_set_operator, int input_sync_status);
 
-INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(int input_account_id);
+INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(char *multi_user_name, int input_account_id);
 
 
 #ifdef __FEATURE_BACKUP_ACCOUNT__
-INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code);
+INTERNAL_FUNC int emcore_backup_accounts(char *multi_user_name, const char *file_path, int *error_code);
 
-INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code);
+INTERNAL_FUNC int emcore_restore_accounts(char *multi_user_name, const char *file_path);
 #endif /*   __FEATURE_BACKUP_ACCOUNT_ */
 
 #ifdef __cplusplus
index 25f747d..d78f2bc 100755 (executable)
@@ -46,12 +46,13 @@ typedef struct _email_alarm_data_t {
        email_alarm_class_t           class_id;\r
        int                         reference_id;\r
        time_t                      trigger_at_time;\r
-       int                       (*alarm_callback)(int, void *);\r
+       int                       (*alarm_callback)(struct _email_alarm_data_t*, void *);\r
+       char                       *multi_user_name;\r
        void                       *user_data;\r
 } email_alarm_data_t;\r
 \r
 \r
-INTERNAL_FUNC int emcore_add_alarm(time_t input_trigger_at_time, email_alarm_class_t input_class_id, int input_reference_id, int (*input_alarm_callback)(int, void *), void *input_user_data);\r
+INTERNAL_FUNC int emcore_add_alarm(char *multi_user_name, time_t input_trigger_at_time, email_alarm_class_t input_class_id, int input_reference_id, int (*input_alarm_callback)(email_alarm_data_t*, void *), void *input_user_data);\r
 \r
 INTERNAL_FUNC int emcore_delete_alram_data_from_alarm_data_list(email_alarm_data_t *input_alarm_data);\r
 INTERNAL_FUNC int emcore_delete_alram_data_by_reference_id(email_alarm_class_t input_class_id, int input_reference_id);\r
diff --git a/email-core/include/email-core-auto-download.h b/email-core/include/email-core-auto-download.h
new file mode 100755 (executable)
index 0000000..d4b863d
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Minsoo Kim <minnsoo.kim@samsung.com>, Kyuho Jo <kyuho.jo@samsung.com>,
+* Sunghyun Kwon <sh0701.kwon@samsung.com>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+#ifndef __EMAIL_CORE_AUTO_DOWNLOAD_H__
+#define __EMAIL_CORE_AUTO_DOWNLOAD_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+typedef struct auto_download_t
+{
+       int activity_id;
+       int status;
+       int account_id;
+       int mail_id;
+       unsigned long server_mail_id;
+       int mailbox_id;
+       char *multi_user_name;
+} email_event_auto_download;
+
+INTERNAL_FUNC int emcore_start_auto_download_loop(int *err_code);
+INTERNAL_FUNC int emcore_auto_download_loop_continue(void);
+INTERNAL_FUNC int emcore_stop_auto_download_loop(int *err_code);
+INTERNAL_FUNC int emcore_insert_auto_download_event(email_event_auto_download *event_data, int *err_code);
+INTERNAL_FUNC int emcore_retrieve_auto_download_event(email_event_auto_download **event_data, int *err_code);
+INTERNAL_FUNC int emcore_is_auto_download_queue_empty(void);
+INTERNAL_FUNC int emcore_is_auto_download_queue_full(void);
+INTERNAL_FUNC int emcore_clear_auto_download_queue(void);
+
+INTERNAL_FUNC int emcore_insert_auto_download_job(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int auto_download_on, char *uid, int *err_code);
+INTERNAL_FUNC int emcore_insert_auto_download_activity(email_event_auto_download *local_activity, int *activity_id, int *err_code);
+INTERNAL_FUNC int emcore_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code);
+#endif
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+/* EOF */
old mode 100644 (file)
new mode 100755 (executable)
index 12e87b1..1257fb7
@@ -39,9 +39,9 @@
 #include "email-utilities.h"
 #include "email-types.h"
 
-INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *save_name, int *err_code);
+INTERNAL_FUNC int emcore_add_public_certificate(char *multi_user_name, char *public_cert_path, char *save_name, int *err_code);
 
-INTERNAL_FUNC int emcore_delete_public_certificate(char *email_address, int *err_code);
+INTERNAL_FUNC int emcore_delete_public_certificate(char *multi_user_name, char *email_address, int *err_code);
 
 INTERNAL_FUNC int emcore_verify_signature(char *p7s_file_path, char *mime_entity, int *validity, int *err_code);
 
@@ -53,4 +53,5 @@ INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, char *password, EVP_PK
 */
 INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X509 **cert, STACK_OF(X509) **ca, int *err_code);
 
+INTERNAL_FUNC int emcore_load_PFX_file_from_string(char *certificate, char **key_string, int *key_size, int *err_code);
 #endif
diff --git a/email-core/include/email-core-container.h b/email-core/include/email-core-container.h
new file mode 100644 (file)
index 0000000..444d6f6
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+/**
+ *
+ * This file contains functionality related to KNOX
+ * to interact with email-service.
+ * @file               email-core-container.c
+ * @author     
+ * @version    0.1
+ * @brief              This file contains functionality to provide KNOX support in email-service. 
+ */
+
+#include <glib.h>
+#include <email-internal-types.h>
+#ifdef __FEATURE_CONTAINER_ENABLE__
+#include <vasum.h>
+#else
+typedef char* vsm_zone_h; 
+#endif /* __FEATURE_CONTAINER_ENABLE__ */
+
+INTERNAL_FUNC void emcore_create_container();
+INTERNAL_FUNC void emcore_destroy_container();
+INTERNAL_FUNC void emcore_bind_vsm_context();
+INTERNAL_FUNC int  emcore_get_container_path(char *multi_user_name, char **container_path);
+INTERNAL_FUNC int  emcore_get_user_name(int pid, char **multi_user_name);
+INTERNAL_FUNC int  emcore_lookup_zone_by_name(char *user_name);
+INTERNAL_FUNC void emcore_set_declare_link(const char *file_path);
+INTERNAL_FUNC int  emcore_get_zone_name_list(GList **output_name_list);
+INTERNAL_FUNC int  emcore_get_canonicalize_path(char *db_path, char **output_path);
+INTERNAL_FUNC int  emcore_set_join_zone(char *multi_user_name, vsm_zone_h *join_zone);
+INTERNAL_FUNC void emcore_unset_join_zone(vsm_zone_h join_zone);
index 3654c2f..006107e 100755 (executable)
@@ -53,17 +53,19 @@ INTERNAL_FUNC int          emcore_cancel_all_thread(int *err_code);
 INTERNAL_FUNC int          emcore_send_event_loop_stop(int *err_code);
 INTERNAL_FUNC int          emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code);
 INTERNAL_FUNC int          emcore_cancel_all_send_mail_thread(int *err_code);
-INTERNAL_FUNC int          emcore_check_thread_status(void);
+INTERNAL_FUNC int          emcore_check_event_thread_status(int *event_type, int handle);
 INTERNAL_FUNC int          emcore_check_send_mail_thread_status(void);
 INTERNAL_FUNC void         emcore_get_event_queue_status(int *on_sending, int *on_receiving);
 INTERNAL_FUNC int          emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code);
 INTERNAL_FUNC int          emcore_get_receiving_event_queue(email_event_t **event_queue, int *event_count, int *err);
-INTERNAL_FUNC int          emcore_cancel_all_threads_of_an_account(int account_id);
+INTERNAL_FUNC int          emcore_cancel_all_threads_of_an_account(char *multi_user_name, int account_id);
 INTERNAL_FUNC int          emcore_free_event(email_event_t *event_data);
 INTERNAL_FUNC int          emcore_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count);
 
 INTERNAL_FUNC void emcore_initialize_event_callback_table();
 INTERNAL_FUNC int emcore_event_loop_continue(void);
+INTERNAL_FUNC int emcore_is_event_queue_empty(void);
+INTERNAL_FUNC int emcore_is_send_event_queue_empty(void);
 INTERNAL_FUNC int emcore_retrieve_event(email_event_t **event_data, int *err_code);
 INTERNAL_FUNC int emcore_return_handle(int handle);
 INTERNAL_FUNC int emcore_retrieve_send_event(email_event_t **event_data, int *err_code);
@@ -73,6 +75,21 @@ INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd
 INTERNAL_FUNC void emcore_pb_thd_set_local_activity_continue(int flag);
 INTERNAL_FUNC int emcore_pb_thd_can_local_activity_continue();
 INTERNAL_FUNC int emcore_set_pbd_thd_state(int flag);
+INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data);
+
+/*
+NOTE: The event is subject to event thread worker. 
+      Don't be confused with other events 
+*/
+#define FINISH_OFF_IF_EVENT_CANCELED(err, handle)     \
+               do {\
+                       int type=0;\
+                       if (!emcore_check_event_thread_status(&type, handle))     {\
+                               EM_DEBUG_LOG ("CANCELED EVENT: type [%d]", type);\
+                               err = EMAIL_ERROR_CANCELLED;\
+                               goto FINISH_OFF;\
+                       }\
+               } while(0)
 
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
diff --git a/email-core/include/email-core-gmime.h b/email-core/include/email-core-gmime.h
new file mode 100644 (file)
index 0000000..33dfc4c
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+#ifndef __EMAIL_CORE_GMIME_H__
+#define __EMAIL_CORE_GMIME_H__
+
+#include "email-core-mail.h"
+#include <gmime/gmime.h>
+
+#include "c-client.h"
+#include "lnx_inc.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+
+typedef struct _search_section {
+       GMimeObject *section_object;
+       char *section;
+} search_section;
+
+INTERNAL_FUNC void emcore_gmime_init(void);
+INTERNAL_FUNC void emcore_gmime_shutdown(void);
+
+INTERNAL_FUNC int emcore_gmime_pop3_parse_mime(char *eml_path, struct _m_content_info *cnt_info, int *err_code);
+INTERNAL_FUNC int emcore_gmime_imap_parse_mime_partial(char *rfc822header_str, char *bodytext_str, struct _m_content_info *cnt_info);
+INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path, struct _rfc822header *rfc822_header, struct _m_content_info *cnt_info, int *err_code);
+
+INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+
+INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+INTERNAL_FUNC void emcore_gmime_search_section_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data);
+
+INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart, BODY *body, const char *spec, int *total_mail_size);
+INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part, BODY *body, const char *spec, int *total_mail_size);
+INTERNAL_FUNC int emcore_gmime_construct_mime_part_with_bodystructure(BODY *mbody, GMimeMessage **message, const char *spec, int *total_mail_size);
+
+INTERNAL_FUNC int emcore_gmime_get_body_sections_from_message(GMimeMessage *message, struct _m_content_info *cnt_info, char **sections_to_fetch);
+INTERNAL_FUNC int emcore_gmime_get_attachment_section_from_message(GMimeMessage *message,
+               struct _m_content_info *cnt_info, int nth, char **section_to_fetch);
+
+INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int msg_uid, int mail_id,
+               struct _m_content_info *cnt_info, GMimeMessage *message, int event_handle, int auto_download, int *err_code);
+
+INTERNAL_FUNC int emcore_gmime_fetch_imap_attachment_section(MAILSTREAM *stream,
+               int mail_id, int uid, int nth, struct _m_content_info *cnt_info,
+               GMimeMessage *message, int auto_download, int event_handle, int *err_code);
+
+INTERNAL_FUNC int emcore_gmime_check_filename_duplication(char *source_filename, struct _m_content_info *cnt_info);
+INTERNAL_FUNC char *emcore_gmime_get_modified_filename_in_duplication(char *source_filename);
+INTERNAL_FUNC char *emcore_gmime_get_encoding_to_utf8(const char *text);
+INTERNAL_FUNC char *emcore_gmime_get_decoding_text(const char *text);
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif
+/* EOF */
index 30fd18e..96b3ab0 100755 (executable)
 /**
 
 
- * @fn emcore_create_imap_idle_thread(int *err_code)
+ * @fn emcore_create_imap_idle_thread()
  * @brief      Creates a thread that listens for IMAP IDLE Notifications.
- *
- * @param[out] err_code        Specifies the error code returned.
  * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @return Specifies the error code returned.
  */
 
-INTERNAL_FUNC int emcore_create_imap_idle_thread(int accountID, int *err_code);
+INTERNAL_FUNC int emcore_create_imap_idle_thread();
 
 /**
 
 
- * @fn emcore_kill_imap_idle_thread(int *err_code)
+ * @fn emcore_refresh_imap_idle_thread(int *err_code)
  * @brief      Kills IMAP IDLE thread
  *
- * @param[out] err_code        Specifies the error code returned.
  * @remarks N/A
- * @return This function returns true on success or false on failure.
+ * @return Specifies the error code returned.
  */
-INTERNAL_FUNC int emcore_kill_imap_idle_thread(int *err_code);
+INTERNAL_FUNC int emcore_refresh_imap_idle_thread();
index 1c17bd4..585cf8b 100755 (executable)
@@ -33,7 +33,6 @@
 #ifndef __EMAIL_CORE_IMAP_MAILBOX_H__
 #define __EMAIL_CORE_IMAP_MAILBOX_H__
 
-
 #ifdef __cplusplus
 extern "C"
 {
@@ -50,7 +49,7 @@ extern "C"
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox, int handle, int *err_code);
+INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id, char *mailbox, int event_handle, int *err_code);
 
 /**
  * Download mailbox list from imap server.
@@ -64,12 +63,12 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox, int ha
  * @return This function returns true on success or false on failure.
  */
 INTERNAL_FUNC int emcore_download_mailbox_list(void *mail_stream, char *mailbox, email_internal_mailbox_t **mailbox_list, int *count, int *err_code);
-INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code);
-INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code);
-INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published);
-INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code);
-INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code);  
-INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *output_count);
+INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code);
+INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code);
+INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published);
+INTERNAL_FUNC int emcore_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *err_code);
+INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name, emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code);   
+INTERNAL_FUNC int emcore_get_default_mail_slot_count(char *multi_user_name, int input_account_id, int *output_count);
 
 #ifdef __FEATURE_IMAP_QUOTA__
 INTERNAL_FUNC int emcore_register_quota_callback();
index a6505a6..c07df5f 100755 (executable)
 #ifndef __EMAIL_CORE_MESSAGE_H__
 #define __EMAIL_CORE_MESSAGE_H__
 
-#include "email-storage.h"
-//#include <contacts.h>
-
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
+#include "email-storage.h"
+
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
 
 #define MAX_SUBSET_STRING_SIZE 260     
@@ -58,41 +57,191 @@ typedef struct _emf_uid_range_set
 
 #endif
 
+struct attachment_info
+{
+       int   type;                 /*  1 : inline 2 : attachment */
+       char *name;                 /*  attachment filename */
+       int   size;                 /*  attachment size */
+       char *save;                 /*  content saving filename */
+       int   drm;                  /*  0 : none 1 : object 2 : rights 3 : dcf */
+       int   drm2;                 /*  0 : none 1 : FL 2 : CD 3 : SSD 4 : SD */
+       char *attachment_mime_type; /*  attachment mime type */
+       char *content_id;           /*  mime content id */
+       int   save_status;
+#ifdef __ATTACHMENT_OPTI__
+       int   encoding;         /*  encoding  */
+       char *section;          /*  section number */
+#endif
+       struct attachment_info *next;
+};
+
+/*
+       MIME Structure Example
+
+       (part 0)        multipart/mixed
+       (part 1)                multipart/alternative
+       (part 1.1)                      text/plain              <- text message
+       (part 1.2)                      text/html               <- html message
+       (part 2)                text/plain                      <- text attachment
+
+
+       (part 0)        multipart/related
+       (part 1)                multipart/alternative
+       (part 1.1)                      text/plain                      <- text message
+       (part 1.2)                      text/html                       <- html message
+       (part 2)                image/png                               <- inline image
+       (part 2)                image/png                               <- inline image
+
+
+       (part 0)        multipart/mixed
+       (part 1.1)              multipart/related
+       (part 2.1)                      multipart/alternative
+       (part 3.1)                              text/plain(body)  <- text message
+       (part 3.2)                              text/html(body) <- html message
+       (part 2.2)                      image/png(related)      <- inline image
+       (part 1.2)              image/png(attachment)   <- image attachment
+*/
+
+/*  Text and Attachment Holde */
+/* struct _m_content_info  */
+/*     int grab_type;  */              /*  1 :  text and attachment list */
+                                               /*  2 :  attachmen */
+/*     int file_no; */                 /*  attachment sequence to be downloaded (min : 1 */
+/*     struct text_data  */
+/*             char *plain;    */      /*  body plain tex */
+/*             char *plain_charset */ /*  charset of plai */
+/*             char *html; */ /*  body html tex */
+/*     } text */
+
+/*     struct attachment_info  */
+/*             int   type;     */              /*  1 : inline 2 : attachmen */
+/*             char *name;     */              /*  attachment filenam */
+/*             int   size;             */      /*  attachment siz */
+/*             char *save;     */              /*  content saving filenam */
+/*             struct attachment_info *next */
+/*     } *file */
+/* } */
+
+/* --------------------- MIME Structure --------------------------------- */
+/*  MIME Header Parameter (Content-Type, Content-Disposition, ... */
+struct _parameter {
+       char                            *name;                  /*  parameter name */
+       char                            *value;                 /*  parameter value */
+       struct _parameter       *next;                  /*  next paramete */
+};
+
+/*  Content-Disposition */
+struct _disposition {
+       char                            *type;                  /*  "inline" "attachment */
+       struct _parameter       *parameter;             /*  "filename", .. */
+};
+
+/*  RFC822 Header */
+struct _rfc822header {
+       char                            *return_path;   /*  error return path */
+       char                            *received;
+       char                            *date;
+       char                            *from;
+       char                            *subject;
+       char                            *sender;
+       char                            *to;
+       char                            *cc;
+       char                            *bcc;
+       char                            *reply_to;
+       char                            *priority;
+       char                            *ms_priority;
+       char                            *dsp_noti_to;
+       char                            *message_id;
+       char                            *content_type;
+       char                            *others;
+};
+
+/*  MIME Part Header */
+struct _m_part_header {
+       char                            *type;                      /* text, image, audio, video, application, multipart, message */
+       char                            *subtype;                   /* plain, html, jpeg, .. */
+       char                            *encoding;                  /* encoding typ */
+       struct _parameter       *parameter;                 /* content-type parameter  :  "boundary" "charset" .. */
+       char                            *desc;                      /* description */
+       char                            *disp_type;                 /* disposition type  :  "inline" "attachment", */
+       struct _parameter       *disp_parameter;    /* disposition parameter  :  "filename", .. */
+       char                            *content_id;        /* content id  :  it is inline  */
+       char                            *content_location;      /* content location  :  "inline" location  */
+       char                            *priority;              /* Priority : 1, 3, 5 */
+       char                            *ms_priority;           /* MS-Priority : HIGH, NORMAL, LOW */
+};
+
+/*  MIME Message Header */
+struct _m_mesg_header {
+       char                               *version;          /* MIME Version */
+       struct _m_part_header  *part_header;      /* MIME Part Header */
+       /* char                   *message_context; */  /* Message-Context : Voice-message, Video-message, Fax-message... */
+       /* int                     content_duration; */ /* Content-Duration */
+       /* int                     x_content_pages;  */ /* X-Content-Pages */
+       /* char                   *sensitivity; */     /* Sensitivity */
+};
+
+/*  MIME Multipart Body linked list */
+typedef struct _m_body _m_body_t;
+struct _m_part{
+       _m_body_t              *body;             /* part body */
+       struct _m_part         *next;             /* the next found part */
+};
+
+/*  MIME Multipart Body */
+struct _m_body {
+       struct _m_part_header  *part_header;      /* MIME Part Header */
+       struct _m_part          nested;           /* nested structure if contain multipart */
+       char                   *text;             /* text if not contain multipart */
+       int                     size;             /* text size if not contain multipart */
+       char                   *holdingfile;
+};
+
+/*  MIME Message */
+struct _m_mesg {
+       struct _rfc822header    *rfc822header;    /* RFC822 Header */
+       struct _m_mesg_header   *header;          /* MIME Message Header */
+       struct _m_part           nested;          /* nested structure if contain multipart */
+       char                    *text;            /* text if not contain multipart */
+       int                      size;            /* text size if not contain multipart */
+};
 
 struct _m_content_info 
 {
-       int grab_type;              /*  1 :  download text and get attachment names (no saving attachment) - #define GRAB_TYPE_TEXT       retrieve text and attachment names */
-                              /*  2 :  download attachment - #define GRAB_TYPE_ATTACHMENT retrieve only attachment */
-       int file_no;                /*  attachment no to be download (min : 1) */
-       int report;                 /*  0 : Non 1 : DSN mail 2 : MDN mail 3 : mail to require MDN */
+       int grab_type;  /*  1 :  download text and get attachment names (no saving attachment) -
+                                                       #define GRAB_TYPE_TEXT retrieve text and attachment names */
+                                       /*  2 :  download attachment - #define GRAB_TYPE_ATTACHMENT retrieve only attachment */
+       int file_no;    /*  attachment no to be download (min : 1) */
+       int report;             /*  0 : Non 1 : DSN mail 2 : MDN mail 3 : mail to require MDN */
+       int total_mail_size;
+       int total_body_size;
+       int total_attachment_size;
+       int attachment_only;
+       char *sections;
 
        struct text_data 
        {
-               char *plain;            /*  body plain text */
-               char *plain_charset;    /*  charset of body text */
-               char *html;             /*  body html text */
-               char *html_charset;     /*  charset of html text */
+               int   plain_save_status; 
+               char *plain;             /*  body plain text */
+               char *plain_charset;     /*  charset of body text */
+               int   html_save_status;
+               char *html;              /*  body html text */
+               char *html_charset;      /*  charset of html text */
        } text;
 
-       struct attachment_info 
-       {
-               int   type;                 /*  1 : inline 2 : attachment */
-               char *name;                 /*  attachment filename */
-               int   size;                 /*  attachment size */
-               char *save;                 /*  content saving filename */
-               int   drm;                  /*  0 : none 1 : object 2 : rights 3 : dcf */
-               int   drm2;                 /*  0 : none 1 : FL 2 : CD 3 : SSD 4 : SD */
-               char *attachment_mime_type; /*  attachment mime type */
-               char *content_id;           /*  mime content id */
-               int   save_status;
-#ifdef __ATTACHMENT_OPTI__
-               int   encoding;         /*  encoding  */
-               char *section;          /*  section number */
-#endif
-               struct attachment_info *next;
-       } *file;
+       struct attachment_info *file;
+       struct attachment_info *inline_file; /* only used for IMAP partial body download */
 };
 
+typedef enum {
+       IMAP4_CMD_EXPUNGE,
+       IMAP4_CMD_NOOP
+} imap4_cmd_t;
+
+typedef enum {
+       POP3_CMD_NOOP
+} pop3_cmd_t;
+
 /**
  * Download email body from server.
  *
@@ -109,7 +258,16 @@ struct _m_content_info
  * @return This function returns true on success or false on failure.
  */
 
-INTERNAL_FUNC int emcore_download_body_multi_sections_bulk(void *mail_stream, int account_id, int mail_id, int verbose, int with_attach, int limited_size, int event_handle , int *err_code);
+INTERNAL_FUNC int emcore_download_body_multi_sections_bulk ( char *multi_user_name, 
+                                                      void *mail_stream, 
+                                                      int account_id, 
+                                                      int mail_id, 
+                                                      int verbose, 
+                                                      int with_attach, 
+                                                      int limited_size, 
+                                                      int event_handle, 
+                                                      int cancellable, 
+                                                      int *err_code);
 
 
 /**
@@ -124,42 +282,50 @@ INTERNAL_FUNC int emcore_download_body_multi_sections_bulk(void *mail_stream, in
  * @remarks This function is used for only IMAP mail.
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emcore_download_attachment(int acconut_id, int mail_id, int nth, int *err_code);
-INTERNAL_FUNC int emcore_add_attachment(int mail_id, email_attachment_data_t *attachment, int *err_code);        /* TODO : Remove duplicated function */
-INTERNAL_FUNC int emcore_add_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data); /* TODO : Remove duplicated function */
-INTERNAL_FUNC int emcore_delete_mail_attachment(int attachment_id, int *err_code);
-INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment_data_t **attachment, int *err_code);
-INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
+INTERNAL_FUNC int emcore_download_attachment (char *multi_user_name, int acconut_id, int mail_id, int nth, int cancellable, int event_handle, int *err_code);
+INTERNAL_FUNC int emcore_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t *attachment, int *err_code);        /* TODO : Remove duplicated function */
+INTERNAL_FUNC int emcore_add_attachment_data(char *multi_user_name, int input_mail_id, email_attachment_data_t *input_attachment_data); /* TODO : Remove duplicated function */
+INTERNAL_FUNC int emcore_delete_mail_attachment(char *multi_user_name, int attachment_id, int *err_code);
+INTERNAL_FUNC int emcore_get_attachment_info(char *multi_user_name, int attachment_id, email_attachment_data_t **attachment, int *err_code);
+INTERNAL_FUNC int emcore_get_attachment_data_list(char *multi_user_name, int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count);
 INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code);
 
+INTERNAL_FUNC int emcore_gmime_download_attachment(char *multi_user_name, int mail_id, int nth,
+               int cancellable, int event_handle, int auto_download, int *err_code);
+
+INTERNAL_FUNC int emcore_gmime_download_body_sections(char *multi_user_name, void *mail_stream,
+               int account_id, int mail_id, int with_attach, int limited_size,
+               int event_handle, int cancellable, int auto_download, int *err_code);
 
-INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int *err_code);
+INTERNAL_FUNC int emcore_move_mail(char *multi_user_name, int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int *err_code);
 
 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
 INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code) ;
-INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code);
+INTERNAL_FUNC int emcore_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code);
 #endif 
 
-INTERNAL_FUNC int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code);
-INTERNAL_FUNC int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code);
+INTERNAL_FUNC int emcore_get_mail_contact_info(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int *err_code);
+INTERNAL_FUNC int emcore_get_mail_contact_info_with_update(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code);
 INTERNAL_FUNC int emcore_free_contact_info(email_mail_contact_info_t *contact_info, int *err_code);
-INTERNAL_FUNC int emcore_sync_contact_info(int mail_id, int *err_code);
-INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code);
-INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_info_list_t **address_info_list, int *err_code);
+INTERNAL_FUNC int emcore_sync_contact_info(char *multi_user_name, int mail_id, int *err_code);
+INTERNAL_FUNC GList *emcore_get_recipients_list(char *multi_user_name, GList *old_recipients_list, char *full_address, int *err_code);
+INTERNAL_FUNC int emcore_get_mail_address_info_list(char *multi_user_name, int mail_id, email_address_info_list_t **address_info_list, int *err_code);
 
-INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data);
-INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data);
-INTERNAL_FUNC int emcore_delete_contacts_log(int input_account_id);
+INTERNAL_FUNC int emcore_set_sent_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data);
+INTERNAL_FUNC int emcore_set_received_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data);
+INTERNAL_FUNC int emcore_delete_contacts_log(char *multi_user_name, int input_account_id);
 
-INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code);
-INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data);
 
-INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int sync_server);
+INTERNAL_FUNC int emcore_get_mail_display_name(char *multi_user_name, char *email_address, char **contact_display_name);
+INTERNAL_FUNC int emcore_get_mail_display_name_internal(char *multi_user_name, char *email_address, char **contact_display_name);
+INTERNAL_FUNC int emcore_get_mail_data(char *multi_user_name, int input_mail_id, email_mail_data_t **output_mail_data);
 
-INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code);
+INTERNAL_FUNC int emcore_update_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int sync_server);
+
+INTERNAL_FUNC int emcore_delete_mails_from_local_storage(char *multi_user_name, int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code);
 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code);
-INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int input_mailbox_id);
-INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int input_account_id, int input_mailbox_id);
+INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(char *multi_user_name, int input_mailbox_id);
+INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(char *multi_user_name, int input_account_id, int input_mailbox_id);
 
 /**
  * Delete mails.
@@ -175,7 +341,7 @@ INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int in
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emcore_delete_mail(int account_id, int mail_id[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code);
+INTERNAL_FUNC int emcore_delete_mail(char *multi_user_name, int account_id, int mail_id[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code);
 
 /**
  * Delete mails.
@@ -186,33 +352,38 @@ INTERNAL_FUNC int emcore_delete_mail(int account_id, int mail_id[], int num, int
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int   emcore_delete_all_mails_of_acount(int input_account_id);
-INTERNAL_FUNC int   emcore_delete_all_mails_of_mailbox(int input_account_id, int input_mailbox_id, int input_from_server, int *err_code);
+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 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);
 INTERNAL_FUNC void  emcore_free_content_info(struct _m_content_info *cnt_info);
 INTERNAL_FUNC void  emcore_free_attachment_info(struct attachment_info *attchment);
 
-INTERNAL_FUNC int   emcore_move_mail_on_server(int account_id, int src_mailbox_id,  int mail_ids[], int num, char *dest_mailbox, int *error_code);
-INTERNAL_FUNC int   emcore_move_mail_to_another_account(int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id);
-INTERNAL_FUNC int   emcore_sync_flag_with_server(int mail_id, int *err_code);
-INTERNAL_FUNC int   emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code);
+INTERNAL_FUNC int   emcore_move_mail_on_server(char *multi_user_name, int account_id, int src_mailbox_id,  int mail_ids[], int num, char *dest_mailbox, int *error_code);
+INTERNAL_FUNC int   emcore_move_mail_on_server_by_server_mail_id(void *mail_stream, char *server_mail_id, char *dest_mailbox_name);
+INTERNAL_FUNC int   emcore_move_mail_to_another_account(char *multi_user_name, int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id);
+INTERNAL_FUNC int   emcore_sync_flag_with_server(char *multi_user_name, int mail_id, int event_handle, int *err_code);
+INTERNAL_FUNC int   emcore_sync_seen_flag_with_server(char *multi_user_name, int mail_ids[], int num, int event_handle, int *err_code);
 
-INTERNAL_FUNC int   emcore_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code);
+INTERNAL_FUNC int   emcore_set_flags_field(char *multi_user_name, int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code);
 INTERNAL_FUNC char* emcore_convert_mutf7_to_utf8(char *mailbox_name); 
 INTERNAL_FUNC int   emcore_convert_string_to_structure(const char *encoded_string, void **struct_var, email_convert_struct_type_e type);
-INTERNAL_FUNC int   emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code);
+INTERNAL_FUNC int   emcore_save_mail_file(char *multi_user_name, int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, char *virtual_path, int *err_code);
 
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
-INTERNAL_FUNC int   emcore_sync_flags_field_with_server(int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code);
-INTERNAL_FUNC int   emcore_move_mail_on_server_ex(int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code);
+INTERNAL_FUNC int   emcore_sync_flags_field_with_server(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code);
+INTERNAL_FUNC int   emcore_move_mail_on_server_ex(char *multi_user_name, int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code);
 #endif
 
 #ifdef __ATTACHMENT_OPTI__
-INTERNAL_FUNC int emcore_download_attachment_bulk(/*email_mailbox_t *mailbox, */ int account_id, int mail_id, char *nth,  int *err_code);
+INTERNAL_FUNC int emcore_download_attachment_bulk(/*email_mailbox_t *mailbox, */ int account_id, int mail_id, char *nth, int event_handle, int *err_code);
 #endif
-INTERNAL_FUNC int   emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err_code);
+INTERNAL_FUNC int   emcore_mail_filter_by_rule(char *multi_user_name, email_rule_t *filter_info, int *err_code);
+INTERNAL_FUNC int   emcore_add_rule(char *multi_user_name, email_rule_t *filter_info);
+INTERNAL_FUNC int   emcore_update_rule(char *multi_user_name, int filter_id, email_rule_t *filter_info);
+INTERNAL_FUNC int   emcore_delete_rule(char *multi_user_name, int filter_id);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index de17e48..51ecb4a 100755 (executable)
@@ -34,6 +34,7 @@
 
 #include "email-types.h"
 #include "email-storage.h"
+#include "email-core-mail.h"
 #include "c-client.h"
 
 #ifdef __cplusplus
@@ -56,36 +57,32 @@ typedef struct
 
 typedef struct 
 {
-       char *header;
-       int header_len;
-       char *body;
-       int body_len;
+       char *bodystructure;
+       int bodystructure_len;
+       char *rfc822header;
+       int rfc822header_len;
+       char *bodytext;
+       int bodytext_len;
        unsigned long uid_no;
 } email_partial_buffer;
 
 #endif
 
-typedef struct emcore_uid_elem {
-       int msgno;
-       char *uid;
-       email_mail_flag_t flag;
-       struct emcore_uid_elem *next;
-} emcore_uid_list; 
 
 int pop3_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code);
 int pop3_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code);
 
 int imap4_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code);
-int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code);
+int imap4_mailbox_get_uids(MAILSTREAM *stream, char *input_target_uid_string, emcore_uid_list** uid_list, int *err_code);
 
 int emcore_check_rule(const char *input_full_address_from, const char *input_subject, emstorage_rule_tbl_t *rule, int rule_len, int *priority_sender, int *blocked, int *err_code);
 
-INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(int account_id, MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code);
+INTERNAL_FUNC int emcore_make_mail_tbl_data_from_envelope(char *multi_user_name, int account_id, MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code);
 
-INTERNAL_FUNC int emcore_add_mail_to_mailbox(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);
 
 #ifdef __FEATURE_BODY_SEARCH__
-int emcore_add_mail_text(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, char *stripped_text, int *err_code);
+int emcore_add_mail_text(char *multi_user_name, emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, char *stripped_text, int *err_code);
 #endif
 
 /**
@@ -99,7 +96,17 @@ int emcore_add_mail_text(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_m
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emcore_sync_header(emstorage_mailbox_tbl_t *input_mailbox_tbl, void **stream, emcore_uid_list **input_uid_list, int *mail_count, int *unread_mail, int *err_code);
+INTERNAL_FUNC int emcore_sync_header (char *multi_user_name, 
+                                  emstorage_mailbox_tbl_t *input_mailbox_tbl,
+                                  void **stream,
+                                  emcore_uid_list **input_uid_list,
+                                  int *mail_count,
+                                  int *unread_mail,
+                                  int *vip_mail_count,
+                                  int *vip_unread_mail,
+                                  int cancellable, /*if event thread calls, set it*/
+                                  int event_handle,
+                                  int *err_code);
 
 typedef enum
 {
@@ -120,7 +127,7 @@ typedef enum
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-int emcore_download_uid_all (MAILSTREAM *mail_stream, email_internal_mailbox_t *mailbox, emcore_uid_list **uid_list,
+int emcore_download_uid_all (char *multi_user_name, MAILSTREAM *mail_stream, email_internal_mailbox_t *mailbox, emcore_uid_list **uid_list,
                                 int *total, emstorage_read_mail_uid_tbl_t *read_mail_uids, int count, 
                                          emcore_get_uids_for_delete_t  for_delete, int *err_code);
 
@@ -136,7 +143,7 @@ int emcore_download_uid_all (MAILSTREAM *mail_stream, email_internal_mailbox_t *
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code);
+int emcore_download_imap_msgno(char *multi_user_name, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code);
 
 /**
  * Get a message number to be related to uid.
@@ -171,8 +178,15 @@ int emcore_get_uid(emcore_uid_list *uid_list, int msgno, char **uid, int *err_co
  */
 int emcore_free_uids(emcore_uid_list *uid_list, int *err_code);
 
-INTERNAL_FUNC char *emcore_guess_charset(char *source_string);
-INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id);
+INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(char *multi_user_name, int mail_id);
+INTERNAL_FUNC int emcore_update_attachment_except_inline(char *multi_user_name, 
+                                                        struct _m_content_info *cnt_info, 
+                                                        int account_id, 
+                                                        int mail_id, 
+                                                        int mailbox_id, 
+                                                        int *output_total_attachment_size, 
+                                                        int *output_attachment_count, 
+                                                        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);
index 037f58d..3b8aea7 100755 (executable)
@@ -53,8 +53,8 @@ INTERNAL_FUNC int emcore_remove_connection_info(int account_id);
 #endif /* __FEATURE_KEEP_CONNECTION__ */
 /*  in SMTP case, path argument must be (ENCODED_PATH_SMTP) */
 /*  ex) emcore_connect_to_remote_mailbox(xxx, (char *)ENCODED_PATH_SMTP, xxx, xxx); */
-INTERNAL_FUNC int  emcore_connect_to_remote_mailbox_with_account_info(email_account_t *ref_account, int input_mailbox_id, void **mail_stream, int *err_code);
-INTERNAL_FUNC int  emcore_connect_to_remote_mailbox(int account_id, int input_mailbox_id, void **mail_stream, int *err_code);
+INTERNAL_FUNC int  emcore_connect_to_remote_mailbox_with_account_info(char *multi_user_name, email_account_t *ref_account, int input_mailbox_id, void **mail_stream, int *err_code);
+INTERNAL_FUNC int  emcore_connect_to_remote_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, void **mail_stream, int *err_code);
 INTERNAL_FUNC int  emcore_close_mailbox(int account_id, void *mail_stream);
 #ifdef __FEATURE_KEEP_CONNECTION__
 INTERNAL_FUNC void emcore_close_mailbox_receiving_stream();
@@ -62,18 +62,18 @@ INTERNAL_FUNC void emcore_close_mailbox_partial_body_stream();
 INTERNAL_FUNC void emcore_reset_streams();
 #endif
 
-INTERNAL_FUNC int  emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code);
-INTERNAL_FUNC int  emcore_get_mailbox_list(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code);
-INTERNAL_FUNC int  emcore_get_mail_count(email_mailbox_t *mailbox, int *total, int *unseen, int *err_code);
-INTERNAL_FUNC int  emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_server, int *err_code);
-INTERNAL_FUNC int  emcore_delete_mailbox(int input_mailbox_id, int input_on_server, int input_recursive);
-INTERNAL_FUNC int  emcore_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int input_recursive);
-INTERNAL_FUNC int  emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_code);
-INTERNAL_FUNC int  emcore_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int input_recursive, int handle_to_be_published);
+INTERNAL_FUNC int  emcore_get_mailbox_list_to_be_sync(char *multi_user_name, int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code);
+INTERNAL_FUNC int  emcore_get_mailbox_list(char *multi_user_name, int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code);
+INTERNAL_FUNC int  emcore_get_mail_count(char *multi_user_name, email_mailbox_t *mailbox, int *total, int *unseen, int *err_code);
+INTERNAL_FUNC int  emcore_create_mailbox(char *multi_user_name, email_mailbox_t *new_mailbox, int on_server, int server_type, int slot_size, int *err_code);
+INTERNAL_FUNC int  emcore_delete_mailbox(char *multi_user_name, int input_mailbox_id, int input_on_server, int input_recursive);
+INTERNAL_FUNC int  emcore_delete_mailbox_ex(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int input_recursive);
+INTERNAL_FUNC int  emcore_delete_mailbox_all(char *multi_user_name, email_mailbox_t *mailbox, int *err_code);
+INTERNAL_FUNC int  emcore_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int input_recursive, int handle_to_be_published);
 INTERNAL_FUNC int  emcore_save_local_activity_sync(int account_id, int *err_code);
 INTERNAL_FUNC int  emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id , int *err_code);
-INTERNAL_FUNC int  emcore_partial_body_thd_local_activity_sync(int *is_event_inserted, int *err_code);
-INTERNAL_FUNC int  emcore_get_mailbox_by_type(int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *spam_mailbox, int *err_code);
+INTERNAL_FUNC int  emcore_partial_body_thd_local_activity_sync(char *multi_user_name, int *is_event_inserted, int *err_code);
+INTERNAL_FUNC int  emcore_get_mailbox_by_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *spam_mailbox, int *err_code);
 INTERNAL_FUNC void emcore_free_mailbox_list(email_mailbox_t **mailbox_list, int count);
 INTERNAL_FUNC void emcore_free_mailbox(email_mailbox_t *mailbox);
 
@@ -81,7 +81,8 @@ INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_li
 INTERNAL_FUNC int  emcore_free_internal_mailbox(email_internal_mailbox_t **mailbox_list, int count, int *err_code);
 
 
-
+INTERNAL_FUNC void emcore_close_recv_stream_list (void);
+INTERNAL_FUNC MAILSTREAM** emcore_get_recv_stream (char *multi_user_name, int account_id, int mailbox_id, int *error);
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
 INTERNAL_FUNC int  emcore_local_activity_sync(int account_id, int *err_code);
index a619132..6776365 100755 (executable)
@@ -48,15 +48,16 @@ INTERNAL_FUNC int   emcore_get_content_type_from_mime_string(char *input_mime_st
 INTERNAL_FUNC int   emcore_get_content_type_from_mail_bodystruct(BODY *input_body, int input_buffer_length, char *output_content_type);
 INTERNAL_FUNC int   emcore_get_attribute_value_of_body_part(PARAMETER *input_param, char *atribute_name, char *output_value, int output_buffer_length, int with_rfc2047_text, int *err_code);
 INTERNAL_FUNC int   emcore_get_body_part_list_full(MAILSTREAM *stream, int msg_uid, int account_id, int mail_id, BODY *body, struct _m_content_info *cnt_info, int *err_code, PARTLIST * section_list, int event_handle);
-INTERNAL_FUNC int   emcore_get_body(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, struct _m_content_info *cnt_info, int *err_code);
+INTERNAL_FUNC int   emcore_get_body(MAILSTREAM *stream, int account_id, int mail_id, int msg_uid, BODY *body, char *part_body_type, struct _m_content_info *cnt_info, int *err_code);
 INTERNAL_FUNC int   emcore_get_body_structure(MAILSTREAM *stream, int msg_uid, BODY **body, int *err_code);
 INTERNAL_FUNC char *emcore_decode_rfc2047_text(char *rfc2047_text, int *err_code);
 INTERNAL_FUNC int   emcore_decode_body_text(char *enc_buf, int enc_len, int enc_type, int *dec_len, int *err_code);
 INTERNAL_FUNC int   emcore_set_fetch_body_section(BODY *body, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code);
 INTERNAL_FUNC int   emcore_parse_mime_file_to_mail(char *eml_file_path, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count, int *err_code);
-INTERNAL_FUNC int   emcore_delete_parsed_data(email_mail_data_t *input_mail_data, int *err_code);
+INTERNAL_FUNC int   emcore_delete_parsed_data(char *multi_user_name, email_mail_data_t *input_mail_data, int *err_code);
 INTERNAL_FUNC int   emcore_get_mime_entity(char *mime_path, char **mime_entity, int *err_code);
 INTERNAL_FUNC int   emcore_get_utf8_address(char **dest, ADDRESS *address, int *err_code);
+INTERNAL_FUNC int   emcore_get_digest_type(char *micalg_value);
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/email-core/include/email-core-pgp.h b/email-core/include/email-core-pgp.h
new file mode 100644 (file)
index 0000000..b7d2288
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+
+/******************************************************************************
+ * File :  email-core-smime.h
+ * Desc :  Mail Operation Header
+ *
+ * Auth : 
+ *
+ * History : 
+ *    2006.08.16  :  created
+ *****************************************************************************/
+
+#ifndef EM_CORE_GPG_H_
+#define EM_CORE_GPG_H_
+
+#include <cert-service.h>
+
+#include "email-types.h"
+
+INTERNAL_FUNC int emcore_pgp_set_signed_message(char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path);
+
+INTERNAL_FUNC int emcore_pgp_set_encrypted_message(char *recipient_list, char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path);
+
+INTERNAL_FUNC int emcore_pgp_set_signed_and_encrypted_message(char *recipient_list, char *certificate, char *password, char *mime_entity, char *user_id, email_digest_type digest_type, char **file_path);
+
+INTERNAL_FUNC int emcore_pgp_get_verify_signature(char *signature_path, char *mime_entity, email_digest_type digest_type, int *verify);
+
+INTERNAL_FUNC int emcore_pgp_get_decrypted_message(char *encrypted_message, char *password, int sign, char **decrypted_file, int *verify);
+
+#endif /* EM_CORE_GPG_H_ */
index c1b84f7..547f0fa 100755 (executable)
 
 INTERNAL_FUNC int emcore_smime_set_signed_message(char *certificate, char *mime_entity, email_digest_type digest_type, char **file_path, int *err_code);
 
-INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *other_certificate_list, char *mime_entity, email_cipher_type cipher_type, char **file_path, int *err_code);
+INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *multi_user_name, char *other_certificate_list, char *mime_entity, email_cipher_type cipher_type, char **file_path, int *err_code);
 
-INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *recipient_list, char *certificate, char *mime_entity, email_cipher_type cipher_type, email_digest_type digest_type, char **file_path, int *err_code);
+INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *multi_user_name, char *recipient_list, char *certificate, char *mime_entity, email_cipher_type cipher_type, email_digest_type digest_type, char **file_path, int *err_code);
 
 INTERNAL_FUNC int emcore_smime_verify_signed_message(char *signed_message, char *ca_file, char *ca_path, int *verify);
 
-INTERNAL_FUNC int emcore_smime_set_decrypt_message(char *encrypt_message, char *from_address, char **decrypt_message, int *err_code);
+INTERNAL_FUNC int emcore_smime_get_decrypt_message(char *encrypt_message, char *from_address, char **decrypt_message, int *err_code);
 
-INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t *account_tbl_item, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data_list, int *output_attachment_count);
+INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(char *multi_user_name, emstorage_account_tbl_t *account_tbl_item, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data_list, int *output_attachment_count);
 
+INTERNAL_FUNC void emcore_init_openssl_library();
+INTERNAL_FUNC void emcore_clean_openssl_library();
 
 #endif /* EM_CORE_SMIME_H_ */
index 77eefb8..0e6f8f6 100755 (executable)
@@ -42,27 +42,31 @@ extern "C"
 #include "c-client.h"
 #include "email-internal-types.h"
 
-INTERNAL_FUNC int emcore_send_mail(int mail_id, int *err_code);
+INTERNAL_FUNC int emcore_send_mail(char *multi_user_name, int mail_id, int *err_code);
 
-INTERNAL_FUNC int emcore_send_saved_mail(int account_id, char *mailbox, int *err_code);
+INTERNAL_FUNC int emcore_send_saved_mail(char *multi_user_name, int account_id, char *mailbox, int *err_code);
 
-INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id);
+INTERNAL_FUNC int emcore_send_mail_with_downloading_attachment_of_original_mail(char *multi_user_name, int input_mail_id);
 
-INTERNAL_FUNC int emcore_schedule_sending_mail(int input_mail_id, time_t input_time_to_send);
+INTERNAL_FUNC int emcore_schedule_sending_mail(char *multi_user_name, int input_mail_id, time_t input_time_to_send);
 
-INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(emstorage_mail_tbl_t *input_mail_tbl_data, emstorage_attachment_tbl_t *input_attachment_tbl_t, int input_attachment_count, ENVELOPE **env, char **file_path, email_option_t *sending_option, int *err_code);
+INTERNAL_FUNC int emcore_make_rfc822_file_from_mail(char *multi_user_name, emstorage_mail_tbl_t *input_mail_tbl_data, emstorage_attachment_tbl_t *input_attachment_tbl_t, int input_attachment_count, ENVELOPE **env, char **file_path, email_option_t *sending_option, int *err_code);
 
-INTERNAL_FUNC int emcore_make_rfc822_file(email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, char **file_path, int *err_code);
+INTERNAL_FUNC int emcore_make_rfc822_file(char *multi_user_name, email_mail_data_t *input_mail_tbl_data, email_attachment_data_t *input_attachment_tbl, int input_attachment_count, int mime_entity_status, char **file_path, int *err_code);
 
-INTERNAL_FUNC int emcore_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas, int move_flag);
+INTERNAL_FUNC int emcore_add_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas, int move_flag);
 
-INTERNAL_FUNC int emcore_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id);
+INTERNAL_FUNC int emcore_add_read_receipt(char *multi_user_name, int input_read_mail_id, int *output_receipt_mail_id);
 
-INTERNAL_FUNC int emcore_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code);
+INTERNAL_FUNC int emcore_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int *err_code);
 
-INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(int input_alarm_interval_in_second);
+INTERNAL_FUNC int emcore_create_alarm_for_auto_resend(char *multi_user_name, int input_alarm_interval_in_second);
 
-INTERNAL_FUNC int emcore_query_mail_size_limit(int account_id, int handle, int *err_code);
+INTERNAL_FUNC int emcore_query_mail_size_limit(char *multi_user_name, int account_id, int handle, int *err_code);
+
+INTERNAL_FUNC void emcore_close_smtp_stream_list (void);
+
+INTERNAL_FUNC SENDSTREAM** emcore_get_smtp_stream (char *multi_user_name, int account_id, int *error);
 
 #ifdef __cplusplus
 }
diff --git a/email-core/include/email-core-sound.h b/email-core/include/email-core-sound.h
deleted file mode 100755 (executable)
index 9c98d7e..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
-*  email-service
-*
-* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
-*
-* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-*/
-
-
-#include <vconf-keys.h>
-#include <vconf.h>
-
-typedef enum
-{
-       EMAIL_SOUND_STATUS, 
-       EMAIL_VIBE_STATUS, 
-       EMAIL_ALERT_REP_TYPE, 
-       EMAIL_ALERT_VOLUME, 
-       EMAIL_ALERT_VIBE_STENGTH, 
-} EMAIL_SETTING_t;
-
-typedef enum
-{
-       EMAIL_ALERT_TYPE_MELODY, 
-       EMAIL_ALERT_TYPE_VIB, 
-       EMAIL_ALERT_TYPE_MELODY_AND_VIB, 
-       EMAIL_ALERT_TYPE_MUTE, 
-       EMAIL_ALERT_TYPE_NONE,
-} EMAIL_ALERT_TYPE;
-
-typedef enum
-{
-       EMAIL_GCONF_VALUE_REPEAT_NONE = 0,
-       EMAIL_GCONF_VALUE_REPEAT_2MINS,
-       EMAIL_GCONF_VALUE_REPEAT_5MINS,
-       EMAIL_GCONF_VALUE_REPEAT_10MINS,
-} EMAIL_ALERT_REPEAT_ALARM;
-
-typedef enum
-{
-       SETTING_NOTI_STATUS_OFF    = 0,
-       SETTING_NOTI_STATUS_GLOBAL = 1, 
-       SETTING_NOTI_STATUS_EMAIL  = 2, 
-} EMAIL_NOTI_STATUS;
-
-#ifdef Min
-#undef Min
-#endif
-
-INTERNAL_FUNC int emcore_get_alert_policy(EMAIL_ALERT_TYPE *output_alert_type, char **output_alert_tone_path);
-
index 19d97a4..4f7279f 100755 (executable)
@@ -38,8 +38,8 @@ INTERNAL_FUNC int  emcore_free_task_handler_array();
 INTERNAL_FUNC int  emcore_encode_task_parameter(email_task_type_t input_task_type, void *input_task_parameter_struct, char **output_byte_stream, int *output_stream_size);\r
 INTERNAL_FUNC int  emcore_decode_task_parameter(email_task_type_t input_task_type, char *input_byte_stream, int input_stream_size, void **output_task_parameter_struct);\r
 \r
-INTERNAL_FUNC int  emcore_add_task_to_task_table(email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int *output_task_id);\r
-INTERNAL_FUNC int  emcore_remove_task_from_task_table(int input_task_id);\r
+INTERNAL_FUNC int  emcore_add_task_to_task_table(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int *output_task_id);\r
+INTERNAL_FUNC int  emcore_remove_task_from_task_table(char *multi_user_name, int input_task_id);\r
 INTERNAL_FUNC int  emcore_get_active_task_by_thread_id(thread_t input_thread_id, email_active_task_t **output_active_task);\r
 \r
 INTERNAL_FUNC void* emcore_default_async_task_handler(void *intput_param);\r
index 0c4c092..dde99ef 100755 (executable)
@@ -63,6 +63,7 @@ typedef struct
        int  target_mailbox_id;\r
        int  mail_id_count;\r
        int *mail_id_array;\r
+       char *multi_user_name;\r
 } task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT;\r
 \r
 DECLARE_CONVERTER_FOR_TASK_PARAMETER(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT);\r
@@ -76,6 +77,7 @@ typedef struct
        int  mailbox_id_count;\r
        int *mailbox_id_array;\r
        int  on_server;\r
+    char *multi_user_name;\r
 } task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX;\r
 \r
 DECLARE_CONVERTER_FOR_TASK_PARAMETER(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX);\r
@@ -85,6 +87,7 @@ INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_
 typedef struct\r
 {\r
        int  mail_id;\r
+    char *multi_user_name;\r
 } task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL;\r
 \r
 DECLARE_CONVERTER_FOR_TASK_PARAMETER(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL);\r
@@ -93,6 +96,7 @@ INTERNAL_FUNC void* task_handler_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATT
 /* to handle EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL */\r
 typedef struct\r
 {\r
+    char  *multi_user_name;\r
        int    mail_id;\r
        time_t scheduled_time;\r
 } task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL;\r
@@ -109,6 +113,7 @@ typedef struct
        email_mail_attribute_type attribute_type;\r
        int  value_length;\r
        email_mail_attribute_value_t value;\r
+    char *multi_user_name;\r
 } task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE;\r
 \r
 DECLARE_CONVERTER_FOR_TASK_PARAMETER(EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE);\r
index 832324d..9df34bc 100755 (executable)
@@ -54,28 +54,30 @@ char* emcore_get_alias_of_mailbox(const char *mailbox_path);
 
 /* Parse the Mailbox Path and get the Account Email address */
 INTERNAL_FUNC int   emcore_get_temp_file_name(char **filename, int *err_code);
-int   emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code);
+int   emcore_get_long_encoded_path(char *multi_user_name, int account_id, char *path, int delimiter, char **long_enc_path, int *err_code);
 int   emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code);
 int   emcore_get_file_name(char *path, char **filename, int *err_code);
 INTERNAL_FUNC int   emcore_get_file_size(char *path, int *size, int *err_code);
 int   emcore_get_actual_mail_size(char *pBodyPlane, char *pBodyHtml, struct attachment_info *pAttachment, int *error_code);
-int   emcore_calc_mail_size(email_mail_data_t *mail_data_src, email_attachment_data_t *attachment_data_src, int attachment_count, int *error_code);
+int   emcore_calc_mail_size(char *multi_user_name, email_mail_data_t *mail_data_src, email_attachment_data_t *attachment_data_src, int attachment_count, int *error_code);
 int   emcore_get_address_count(char *addr_str, int *to_num, int *err_code);
-INTERNAL_FUNC int   emcore_is_storage_full(int *error);
-int   emcore_get_long_encoded_path_with_account_info(email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code);
-void  emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data);
+INTERNAL_FUNC int   emcore_is_storage_full();
+int   emcore_get_long_encoded_path_with_account_info(char *multi_user_name, email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code);
+void  emcore_fill_address_information_of_mail_tbl(char *multi_user_name, emstorage_mail_tbl_t *mail_data);
 
 INTERNAL_FUNC char* emcore_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type);
 INTERNAL_FUNC int emcore_get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type);
 INTERNAL_FUNC int emcore_get_mail_attribute_value_type(email_mail_attribute_type input_attribute_type, email_mail_attribute_value_type *output_value_type);
-INTERNAL_FUNC int emcore_get_preview_text_from_file(const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer);
+INTERNAL_FUNC int emcore_get_preview_text_from_file(char *multi_user_name, const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer);
 #ifdef __FEATURE_BODY_SEARCH__
-INTERNAL_FUNC int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code);
+INTERNAL_FUNC int emcore_strip_mail_body_from_file(char *multi_user_name, emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code);
 #endif
+INTERNAL_FUNC int emcore_get_first_address(const char *full_address, char **alias, char **address);
 
-int   reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string);
-int   reg_replace_new (char **input_source_text, char *input_old_pattern_string, char *input_new_string);
-int   emcore_strip_HTML(char **source_string);
+
+INTERNAL_FUNC int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string);
+INTERNAL_FUNC int reg_replace_new (char **input_source_text, char *input_old_pattern_string, char *input_new_string);
+int   emcore_strip_HTML_tag(const char *input_html_file_path, char *encoding_type, char *output_result_buffer, int input_result_buffer_legnth);
 int   emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime);
 int   emcore_make_attachment_file_name_with_extension(char *source_file_name, char *sub_type, char *result_file_name, int result_file_name_buffer_length, int *err_code);
 
@@ -84,16 +86,16 @@ INTERNAL_FUNC int   emcore_get_empty_session(email_session_t **session);
 INTERNAL_FUNC int   emcore_clear_session(email_session_t *session);
 INTERNAL_FUNC int   emcore_get_current_session(email_session_t **session);
 
-INTERNAL_FUNC int emcore_get_mail_count_by_query(int account_id, int mailbox_type, int priority_sender, int *total_mail, int *unread_mail, int *err_code);
+INTERNAL_FUNC int emcore_get_mail_count_by_query(char *multi_user_name, int account_id, int mailbox_type, int priority_sender, int *total_mail, int *unread_mail, int *err_code);
 
 INTERNAL_FUNC int emcore_check_drm_file(char *path, int *err_code);
 INTERNAL_FUNC int emcore_check_drm_is_ringtone(char *ringtone_path, int *err_code);
 
-INTERNAL_FUNC int emcore_display_unread_in_badge();
+INTERNAL_FUNC void emcore_display_unread_in_badge(void *data);
 INTERNAL_FUNC int emcore_display_badge_count(int count);
 INTERNAL_FUNC int emcore_set_network_error(int err_code);
 
-INTERNAL_FUNC int emcore_calc_next_time_to_sync(int input_account_id, time_t input_current_time, time_t *output_time);
+INTERNAL_FUNC int emcore_calc_next_time_to_sync(char *multi_user_name, int input_account_id, time_t input_current_time, time_t *output_time);
 
 /* Transaction Handling */
 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id , int handle  , int *err_code);
@@ -102,15 +104,20 @@ INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id);
 
 /* For notification bar */
 INTERNAL_FUNC int emcore_update_notification_for_unread_mail(int account_id);
-INTERNAL_FUNC int emcore_clear_all_notifications();
+INTERNAL_FUNC int emcore_clear_notifications(char *multi_user_name, int account_id);
 //INTERNAL_FUNC int emcore_add_notification_for_unread_mail(emstorage_mail_tbl_t *input_mail_tbl_data);
-INTERNAL_FUNC int emcore_add_notification(int account_id, int mail_id, int unread_mail_count, int input_play_alert_tone, int sending_error, unsigned long display);
-INTERNAL_FUNC int emcore_add_notification_for_send(int account_id, int mail_id, email_action_t action, int sending_error, unsigned long display);
+INTERNAL_FUNC int emcore_add_notification(char *multi_user_name, int account_id, int mail_id, int unread_mail_count, int vip_unread_mail_count, int input_play_alert_tone, int sending_error, unsigned long display);
+INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int account_id, int mail_id, email_action_t action, int sending_error, unsigned long display);
 INTERNAL_FUNC void emcore_update_notification_for_send(int account_id, int mail_id, double progress);
 INTERNAL_FUNC int emcore_delete_notification_for_read_mail(int mail_id);
-INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id, int with_noti_tray);
+INTERNAL_FUNC int emcore_delete_notification_by_account(char *multi_user_name, int account_id, int with_noti_tray);
+INTERNAL_FUNC void emcore_set_flash_noti();
+
+
+INTERNAL_FUNC int emcore_show_user_message(char *multi_user_name, int id, email_action_t action, int error);
 
-INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error);
+INTERNAL_FUNC int emcore_connect_contacts_service(char *multi_user_name);
+INTERNAL_FUNC int emcore_disconnect_contacts_service(char *multi_user_name);
 
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
 
@@ -208,17 +215,28 @@ INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_s
 
 INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_array, int *array_len);
 
-INTERNAL_FUNC int emcore_start_driving_mode(int account_id);
+INTERNAL_FUNC int emcore_start_driving_mode(char *multi_user_name, int mail_id);
 
 #ifdef __FEATURE_BLOCKING_MODE__
 INTERNAL_FUNC bool emcore_init_blocking_mode_status();
 INTERNAL_FUNC void emcore_set_blocking_mode_of_setting(int input_blocking_mode_of_setting);
 INTERNAL_FUNC int emcore_get_blocking_mode_status();
 INTERNAL_FUNC void emcore_set_blocking_mode_status(int blocking_mode);
-INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking_status);
+INTERNAL_FUNC int emcore_check_blocking_mode(char *multi_user_name, char *sender_address, int *blocking_status);
+INTERNAL_FUNC int emcore_check_blocking_mode_internal (char *multi_user_name, char *sender_address, int *blocking_status);
 #endif /* __FEATURE_BLOCKING_MODE__ */
 
 INTERNAL_FUNC char *emcore_set_mime_entity(char *mime_path);
+INTERNAL_FUNC int emcore_get_content_from_file(char *filename, char **contents, int *length);
+INTERNAL_FUNC int emcore_set_content_to_file(const char *contents, char *dest_path, int length);
+
+#ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
+INTERNAL_FUNC int emcore_update_db_table_schema(char *multi_user_name);
+#endif /* #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
+INTERNAL_FUNC int emcore_unescape_from_url(char *input_url, char **output_url);
+INTERNAL_FUNC char *__em_get_month_in_string(int month);
+INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *output_date_string);
+INTERNAL_FUNC int emcore_make_uid_range_string(emcore_uid_list *uid_list, int total, char **output_uid_range_string);
 
 #ifdef __cplusplus
 }
index cf1f3ec..731af23 100755 (executable)
@@ -15,6 +15,8 @@ ADD_DEFINITIONS("-DSSL_CERT_DIRECTORY=\"${TZ_SYS_ETC}/ssl/certs\"")
 ADD_DEFINITIONS("-DSSL_KEY_DIRECTORY=\"${TZ_SYS_ETC}/ssl/certs\"")
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VISIBILITY} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
+
 
 ##########################################################
 # Define Execute File
@@ -46,24 +48,21 @@ INCLUDE_DIRECTORIES(
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(main_pkgs REQUIRED glib-2.0 dlog dbus-1 gthread-2.0 db-util 
-                               openssl uw-imap-toolkit vconf vconf-internal-keys 
-                               alarm-service mm-player drm-client feedback msg-service 
-                               libwbxml2 libsmack notification)
+SET(PKG_MODULES glib-2.0 dlog dbus-1 gthread-2.0 openssl uw-imap-toolkit 
+                               vconf vconf-internal-keys contacts-service2 alarm-service 
+                               msg-service libwbxml2 libsmack notification 
+                               capi-network-connection gmime-2.6)
 
-#pkg_check_modules(main_pkgs REQUIRED glib-2.0 dlog dbus-1 gthread-2.0 db-util 
-#                              openssl uw-imap-toolkit vconf vconf-internal-keys contacts-service2 
-#                              alarm-service mm-player drm-client feedback msg-service 
-#                              libwbxml2 libsmack notification)
+pkg_check_modules(main_pkgs REQUIRED ${PKG_MODULES})
 
 FOREACH(flag ${main_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 ADD_EXECUTABLE(${PROJECT_NAME} ${MAIN-SRCS})
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${main_pkgs_LDFLAGS} dl ssl email-core email-core-sound email-storage email-network email-ipc email-common-use email-device email-smime)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${main_pkgs_LDFLAGS} dl ssl email-core email-storage email-network email-ipc email-common-use email-device email-smime)
 INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin)
 
index 6e7fe27..4c63fbf 100755 (executable)
 #include "email-debug-log.h"
 #include "email-daemon-account.h"
 #include "email-daemon-auto-poll.h"
-//#include <contacts.h>
+#include <contacts.h>
+#include <contacts_internal.h>
 #include "email-types.h"
 #include "email-core-account.h"
 #include "email-core-event.h"
 #include "email-core-utils.h"
+#include "email-core-imap-idle.h"
 #include "email-utilities.h"
 #include "email-convert.h"
 
-static int emdaemon_check_filter_id(int filter_id, int* err_code)
+static int emdaemon_check_filter_id(char *multi_user_name, int filter_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_id[%d], err_code[%p]", filter_id, err_code);
 
@@ -64,9 +66,8 @@ static int emdaemon_check_filter_id(int filter_id, int* err_code)
        int err = EMAIL_ERROR_NONE;
        emstorage_rule_tbl_t* filter = NULL;
 
-       if (!emstorage_get_rule_by_id(filter_id, &filter, true, &err)) {
+       if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &filter, true, &err)) {
                EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -83,18 +84,16 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_code)
+INTERNAL_FUNC int emdaemon_create_account(char *multi_user_name, email_account_t* account, int* err_code)
 {
        int ret = false;
        int err = EMAIL_ERROR_NONE;
 
-       if (!emcore_create_account(account, &err)) {
+       if (!emcore_create_account(multi_user_name, account, false, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_account_add failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       emcore_init_account_reference();
-
        ret = true;
 FINISH_OFF:
        if (err_code)
@@ -104,12 +103,12 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code)
+INTERNAL_FUNC int emdaemon_delete_account(char *multi_user_name, int account_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d] err_code[%p]", account_id, err_code);
        int ret = false;
 
-       ret = emcore_delete_account(account_id, err_code);
+       ret = emcore_delete_account(multi_user_name, account_id, false, err_code);
 
        EM_DEBUG_FUNC_END("ret[%d]", ret);
        return ret;
@@ -141,7 +140,7 @@ static email_account_t* duplicate_account(email_account_t *src)
        dst->reply_to_address          = EM_SAFE_STRDUP(src->reply_to_address);
        dst->return_address            = EM_SAFE_STRDUP(src->return_address);
        dst->logo_icon_path            = EM_SAFE_STRDUP(src->logo_icon_path);
-       dst->certificate_path              = EM_SAFE_STRDUP(src->certificate_path);
+       dst->certificate_path          = EM_SAFE_STRDUP(src->certificate_path);
        dst->options.display_name_from = EM_SAFE_STRDUP(src->options.display_name_from);
        dst->options.signature         = EM_SAFE_STRDUP(src->options.signature);
        dst->user_data                = (void*) em_malloc(src->user_data_length);
@@ -157,7 +156,7 @@ static email_account_t* duplicate_account(email_account_t *src)
        return dst;
 }
 
-INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_validate_account(char *multi_user_name, int account_id, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
 
@@ -172,7 +171,7 @@ INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* er
                goto FINISH_OFF;
        }
 
-       if (!(ref_account = emcore_get_account_reference(account_id))) {
+       if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -183,6 +182,7 @@ INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* er
        event_data->event_param_data_1 = NULL;
        event_data->event_param_data_3 = NULL;
        event_data->account_id = account_id;
+    event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
@@ -211,7 +211,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_validate_account_ex(email_account_t* input_account, int *output_handle)
+INTERNAL_FUNC int emdaemon_validate_account_ex(char *multi_user_name, email_account_t* input_account, int *output_handle)
 {
        EM_DEBUG_FUNC_BEGIN("input_account[%p], output_handle[%p]", input_account, output_handle);
 
@@ -231,6 +231,7 @@ INTERNAL_FUNC int emdaemon_validate_account_ex(email_account_t* input_account, i
        event_data->event_param_data_1 = (void*)input_account;
        event_data->event_param_data_3 = NULL;
        event_data->account_id         = NEW_ACCOUNT_ID;
+    event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
@@ -251,7 +252,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_account, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_validate_account_and_create(char *multi_user_name, email_account_t* new_account, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account[%p], handle[%p], err_code[%p]", new_account, handle, err_code);
 
@@ -267,10 +268,11 @@ INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_acco
                goto FINISH_OFF;
        }
 
-       event_data->type = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
+       event_data->type               = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
        event_data->event_param_data_1 = (void *)new_account;
        event_data->event_param_data_3 = NULL;
-       event_data->account_id = NEW_ACCOUNT_ID;
+       event_data->account_id         = NEW_ACCOUNT_ID;
+    event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err))  {
                EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
@@ -295,7 +297,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_account, int* err_code)
+INTERNAL_FUNC int emdaemon_update_account(char *multi_user_name, int account_id, email_account_t* new_account, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], new_account[%p], err_code[%p]", account_id, new_account, err_code);
 
@@ -303,7 +305,7 @@ INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_a
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        emstorage_account_tbl_t *new_account_tbl = NULL;
-       email_account_t old_account_info = {0};
+       email_account_t *old_account_info = NULL;
 
        if ((account_id <= 0) || !new_account)  {
                EM_DEBUG_EXCEPTION("Invalid Parameters.");
@@ -311,19 +313,26 @@ INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_a
                goto FINISH_OFF;
        }
 
-       if(!emdaemon_get_account(account_id, GET_FULL_DATA, &old_account_info, &err)) {
-               EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
+       if((old_account_info = emcore_get_account_reference(multi_user_name, account_id)) == NULL) {
+               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed ");
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG_SEC("new_account->email_addr[%s]", new_account->user_email_address);
        if(new_account->user_email_address) {
-               if ((err = em_verify_email_address (new_account->user_email_address, true)) != EMAIL_ERROR_NONE) {
+               if ((err = em_verify_email_address (new_account->user_email_address)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("em_verify_email_address error [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
+       if (EM_SAFE_STRCMP(new_account->incoming_server_password, old_account_info->incoming_server_password) == 0) {
+               EM_SAFE_FREE(new_account->incoming_server_password);
+       }
+
+       if (EM_SAFE_STRCMP(new_account->outgoing_server_password, old_account_info->outgoing_server_password) == 0) {
+               EM_SAFE_FREE(new_account->outgoing_server_password);
+       }
+
        new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
        if(!new_account_tbl) {
                EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
@@ -332,27 +341,34 @@ INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_a
 
        em_convert_account_to_account_tbl(new_account, new_account_tbl);
 
-       if (!emstorage_update_account(account_id, new_account_tbl, true, &err))  {
-               EM_DEBUG_EXCEPTION("emstorage_update_account falied [%d]", err);
+       if (!emstorage_update_account(multi_user_name, account_id, new_account_tbl, true, &err))  {
+               EM_DEBUG_EXCEPTION("emstorage_update_account failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       emcore_init_account_reference();
-
 #ifdef __FEATURE_AUTO_POLLING__
        int  change_in_auto_polling_option = 0;
 
-       change_in_auto_polling_option = (old_account_info.check_interval  != new_account->check_interval)  ||
-                                                                       (old_account_info.peak_interval   != new_account->peak_interval)   ||
-                                                                       (old_account_info.peak_start_time != new_account->peak_start_time) ||
-                                                                       (old_account_info.peak_end_time   != new_account->peak_end_time);
+       change_in_auto_polling_option = (old_account_info->check_interval  != new_account->check_interval)  ||
+                                                                       (old_account_info->peak_interval   != new_account->peak_interval)   ||
+                                                                       (old_account_info->peak_start_time != new_account->peak_start_time) ||
+                                                                       (old_account_info->peak_end_time   != new_account->peak_end_time);
+
+       EM_DEBUG_LOG("change_in_auto_polling_option [%d]", change_in_auto_polling_option);
+#endif
+
 
+#ifdef __FEATURE_AUTO_POLLING__
        if(change_in_auto_polling_option) {
                if(!emdaemon_remove_polling_alarm(account_id))
                        EM_DEBUG_LOG("emdaemon_remove_polling_alarm failed");
 
-               if(!emdaemon_add_polling_alarm(account_id))
+               if(!emdaemon_add_polling_alarm(multi_user_name, account_id))
                        EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
+
+#ifdef __FEATURE_IMAP_IDLE__
+               emcore_refresh_imap_idle_thread();
+#endif /* __FEATURE_IMAP_IDLE__ */
        }
 #endif
 
@@ -360,11 +376,14 @@ INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_a
 
 FINISH_OFF:
 
-       emcore_free_account(&old_account_info);
-
        if(new_account_tbl)
                emstorage_free_account(&new_account_tbl, 1, NULL);
 
+       if (old_account_info) {
+               emcore_free_account(old_account_info);
+               EM_SAFE_FREE(old_account_info);
+       }
+
        if (err_code)
                *err_code = err;
 
@@ -372,7 +391,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email_account_t* new_account_info, int *handle,int *err_code)
+INTERNAL_FUNC int emdaemon_validate_account_and_update(char *multi_user_name, int old_account_id, email_account_t* new_account_info, int *handle,int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account[%d], new_account_info[%p], handle[%p], err_code[%p]", old_account_id, new_account_info, handle, err_code);
 
@@ -392,6 +411,7 @@ INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email
        event_data->event_param_data_1 = (char *) duplicate_account(new_account_info);
        event_data->event_param_data_3 = NULL;
        event_data->account_id = old_account_id;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
 #if 0
        email_account_t *pAccount = (email_account_t *)em_malloc(sizeof(email_account_t));
@@ -444,7 +464,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_account_t* account, int* err_code)
+INTERNAL_FUNC int emdaemon_get_account(char *multi_user_name, int account_id, int pulloption, email_account_t* account, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption [%d], account[%p], err_code[%p]", account_id, pulloption, account, err_code);
 
@@ -459,7 +479,7 @@ INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_acc
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
+       if (!emstorage_get_account_by_id(multi_user_name, account_id, pulloption, &account_tbl, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -477,7 +497,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** account_list, int* count, int* err_code)
+INTERNAL_FUNC int emdaemon_get_account_list(char *multi_user_name, email_account_t** account_list, int* count, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
 
@@ -491,7 +511,7 @@ INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** account_list, int*
 
        *count = 1000;
 
-       if (!emstorage_get_account_list(count, &account_tbl_array, true, true, &err))  {
+       if (!emstorage_get_account_list(multi_user_name, count, &account_tbl_array, true, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -527,7 +547,7 @@ INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int coun
 }
 
 
-INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filter_info, int* err_code)
+INTERNAL_FUNC int emdaemon_get_filter(char *multi_user_name, int filter_id, email_rule_t** filter_info, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
 
@@ -543,7 +563,7 @@ INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filter_info,
        int ret = false;
        int err = EMAIL_ERROR_NONE;
 
-       if (!emstorage_get_rule_by_id(filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
+       if (!emstorage_get_rule_by_id(multi_user_name, filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
                EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -557,7 +577,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filter_info, int* count, int* err_code)
+INTERNAL_FUNC int emdaemon_get_filter_list(char *multi_user_name, email_rule_t** filter_info, int* count, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%p], err_code[%p]", filter_info, count, err_code);
 
@@ -574,7 +594,7 @@ INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filter_info, int* coun
 
        *count = 1000;
 
-       if (!emstorage_get_rule(ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
+       if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
                EM_DEBUG_EXCEPTION(" emstorage_get_rule failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -588,7 +608,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
+INTERNAL_FUNC int emdaemon_find_filter(char *multi_user_name, email_rule_t* filter_info, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
 
@@ -610,7 +630,7 @@ INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
 
        filter_info->account_id = ALL_ACCOUNT;          /*  MUST BE */
 
-       if (!emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
+       if (!emstorage_find_rule(multi_user_name, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
                goto FINISH_OFF;
        } else {
@@ -631,7 +651,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filter_info)
+INTERNAL_FUNC int emdaemon_add_filter(char *multi_user_name, email_rule_t* filter_info)
 {
        EM_DEBUG_FUNC_BEGIN("filter_info[%p]", filter_info);
 
@@ -643,48 +663,19 @@ INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filter_info)
                goto FINISH_OFF;
        }
 
-       if (!emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
-               goto FINISH_OFF;
-       } else {
-               if (err != EMAIL_ERROR_FILTER_NOT_FOUND) {
-                       EM_DEBUG_LOG("filter already exist");
-                       err = EMAIL_ERROR_ALREADY_EXISTS;
-                       goto FINISH_OFF;
-               }
-       }
-
-       switch (filter_info->faction) {
-       case EMAIL_FILTER_MOVE :
-               if (filter_info->account_id < 0) {
-                       EM_DEBUG_EXCEPTION("Invalid Param : target_mailbox_id[%d], account_id[%d]", filter_info->target_mailbox_id, filter_info->account_id);
-                       err = EMAIL_ERROR_INVALID_PARAM;
-                       goto FINISH_OFF;
-               }
-               break;  
-       case EMAIL_FILTER_BLOCK :
-               filter_info->account_id = ALL_ACCOUNT;
-               break;
-       case EMAIL_FILTER_DELETE :
-       default:
-               EM_DEBUG_LOG("filter_faction : [%d]", filter_info->faction);
-               break;
-       }
-
-       if (!emstorage_add_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_add_rule failed [%d]", err);
+       err = emcore_add_rule(multi_user_name, filter_info);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_add_rule failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG("filter_id : [%d]", filter_info->filter_id);
-
 FINISH_OFF:
 
        EM_DEBUG_FUNC_END();
        return err;
 }
 
-INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* filter_info, int* err_code)
+INTERNAL_FUNC int emdaemon_update_filter(char *multi_user_name, int filter_id, email_rule_t* filter_info, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
 
@@ -698,15 +689,16 @@ INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* filter_inf
                goto FINISH_OFF;
        }
 
-       if (!emdaemon_check_filter_id(filter_id, &err))  {
+       if (!emdaemon_check_filter_id(multi_user_name, filter_id, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_check_filter_id falied [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_change_rule(filter_id, (emstorage_rule_tbl_t*)filter_info, true, &err))  {
-               EM_DEBUG_EXCEPTION("emstorage_change_rule falied [%d]", err);
-               goto FINISH_OFF;
-       }
+        err = emcore_update_rule(multi_user_name, filter_id, filter_info);
+        if (err != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_update_rule failed : [%d]", err);
+            goto FINISH_OFF;
+        }
 
        ret = true;
 
@@ -717,14 +709,13 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code)
+INTERNAL_FUNC int emdaemon_delete_filter(char *multi_user_name, int filter_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
 
        /*  default variable */
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       emstorage_rule_tbl_t *p_rule_tbl = NULL;
 
        if (filter_id <= 0)  {
                EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
@@ -732,18 +723,9 @@ INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code)
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_rule_by_id(filter_id, &p_rule_tbl, false, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed : [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       if (!emstorage_filter_mails_by_rule(p_rule_tbl->account_id, p_rule_tbl->target_mailbox_id, false, true, p_rule_tbl, NULL, NULL, &err)) {
-               EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed : [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       if (!emstorage_delete_rule(filter_id, true, &err)) {
-               EM_DEBUG_EXCEPTION(" emstorage_delete_rule failed [%d]", err);
+       err = emcore_delete_rule(multi_user_name, filter_id);
+       if (err != EMAIL_ERROR_NONE) {
+               EM_DEBUG_EXCEPTION("emcore_delete_rule failed : [%d]", err);
                goto FINISH_OFF;
        }
 
@@ -792,7 +774,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_apply_filter(int filter_id, int *err_code)
+INTERNAL_FUNC int emdaemon_apply_filter(char *multi_user_name, int filter_id, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
 
@@ -807,12 +789,12 @@ INTERNAL_FUNC int emdaemon_apply_filter(int filter_id, int *err_code)
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_rule_by_id(filter_id, &filter_info, false, &err)) {
+       if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &filter_info, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed : [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_mail_filter_by_rule((email_rule_t *)filter_info, &err))  {
+       if (!emcore_mail_filter_by_rule(multi_user_name, (email_rule_t *)filter_info, &err))  {
                EM_DEBUG_EXCEPTION(" emstorage_mail_filter_by_rule failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -828,37 +810,6 @@ FINISH_OFF:
 
 
 /* ----- internal functions --------------------------------------------*/
-int emdaemon_initialize_account_reference()
-{
-       EM_DEBUG_FUNC_BEGIN();
-       int err = EMAIL_ERROR_NONE;
-
-       if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
-               if (err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
-                       if ((err = emcore_recover_from_secured_storage_failure()) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_recover_from_secured_storage_failure failed [%d]", err);
-                               goto FINISH_OFF;
-                       }
-
-                       if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
-                               goto FINISH_OFF;
-                       }
-               }
-               else {
-                       if (err == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
-                               EM_DEBUG_LOG ("no account found");
-                       else
-                               EM_DEBUG_EXCEPTION ("emcore_init_account_reference failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-       }
-
-FINISH_OFF:
-
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
-}
 
 INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* account)
 {
@@ -884,7 +835,7 @@ INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_ac
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(int account_id, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(char *multi_user_name, int account_id, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
 
@@ -903,6 +854,7 @@ INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(int account_id, int *handl
        event_data->event_param_data_1 = NULL;
        event_data->event_param_data_3 = NULL;
        event_data->account_id = account_id;
+   event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
index fa4f9a3..3e14fae 100755 (executable)
 #include "email-utilities.h"
 
 
-static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t  alarm_id, int *account_id, int *timer_interval);
-static int _emdaemon_create_alarm_for_auto_polling(int input_account_id);
+static int _emdaemon_get_polling_account_and_timeinterval(email_alarm_data_t *alarm_data, int *account_id, int *timer_interval);
+static int _emdaemon_create_alarm_for_auto_polling(char *multi_user_name, int input_account_id);
 
-INTERNAL_FUNC int emdaemon_add_polling_alarm(int input_account_id)
+INTERNAL_FUNC int emdaemon_add_polling_alarm(char *multi_user_name, int input_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id[%d]", input_account_id);
        int err = EMAIL_ERROR_NONE;
@@ -62,7 +62,7 @@ INTERNAL_FUNC int emdaemon_add_polling_alarm(int input_account_id)
                goto FINISH_OFF;
        }
 
-       if((err = _emdaemon_create_alarm_for_auto_polling(input_account_id)) != EMAIL_ERROR_NONE) {
+       if((err = _emdaemon_create_alarm_for_auto_polling(multi_user_name, input_account_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_emdaemon_create_alarm_for_auto_polling failed[%d]", err);
                goto FINISH_OFF;
        }
@@ -103,9 +103,9 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t alarm_id, void* user_param)
+INTERNAL_FUNC int emdaemon_alarm_polling_cb(email_alarm_data_t *alarm_data, void* user_param)
 {
-       EM_DEBUG_FUNC_BEGIN("alarm_id [%d] user_param [%d]", alarm_id, user_param);
+       EM_DEBUG_FUNC_BEGIN("alarm_data [%p] user_param [%d]", alarm_data, user_param);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
@@ -117,27 +117,20 @@ INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t alarm_id, void* user_para
        int wifi_status = 0;
        email_account_t *ref_account = NULL;
 
-       EM_DEBUG_ALARM_LOG("alarm_id [%d]", alarm_id);
-
-       if(!_emdaemon_get_polling_account_and_timeinterval(alarm_id, &account_id, &timer_interval)) {
+       if(!_emdaemon_get_polling_account_and_timeinterval(alarm_data, &account_id, &timer_interval)) {
                EM_DEBUG_EXCEPTION("email_get_polling_account failed");
                return false;
        }
 
-       /* delete from list */
-       if ((err = emdaemon_remove_polling_alarm(account_id)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("email_get_polling_account failed [%d]", err);
-       }
-
        EM_DEBUG_LOG("target account_id [%d]",account_id);
 
        /* create alarm, for polling */
-       if ((err = _emdaemon_create_alarm_for_auto_polling(account_id)) != EMAIL_ERROR_NONE) {
+       if ((err = _emdaemon_create_alarm_for_auto_polling(alarm_data->multi_user_name, account_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_emdaemon_create_alarm_for_auto_polling failed [%d]", err);
                return false;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(alarm_data->multi_user_name, account_id);
 
        if (ref_account == NULL) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
@@ -166,12 +159,12 @@ INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t alarm_id, void* user_para
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mailbox_id_by_mailbox_type(account_id,EMAIL_MAILBOX_TYPE_INBOX, &mailbox_id, false, &err))  {
+       if (!emstorage_get_mailbox_id_by_mailbox_type(alarm_data->multi_user_name, account_id,EMAIL_MAILBOX_TYPE_INBOX, &mailbox_id, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emdaemon_sync_header(account_id, mailbox_id, &handle, &err))  {
+       if (!emdaemon_sync_header(alarm_data->multi_user_name, account_id, mailbox_id, &handle, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_sync_header falied [%d]", err);
                goto FINISH_OFF;
        }
@@ -189,25 +182,19 @@ FINISH_OFF :
 }
 
 
-static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t alarm_id, int *account_id, int *timer_interval)
+static int _emdaemon_get_polling_account_and_timeinterval(email_alarm_data_t *alarm_data, int *account_id, int *timer_interval)
 {
-       EM_DEBUG_FUNC_BEGIN("alarm_id [%d] account_id[%p] timer_interval[%p]", alarm_id, account_id, timer_interval);
+       EM_DEBUG_FUNC_BEGIN("alarm_data [%p] account_id[%p] timer_interval[%p]", alarm_data, account_id, timer_interval);
        int err = EMAIL_ERROR_NONE;
-       email_alarm_data_t *alarm_data = NULL;
        email_account_t *account = NULL;
 
-       if(!alarm_id || !account_id) {
+       if(!alarm_data || !account_id) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       if (((err = emcore_get_alarm_data_by_alarm_id(alarm_id, &alarm_data)) != EMAIL_ERROR_NONE) || alarm_data == NULL) {
-               EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
-               goto FINISH_OFF;
-       }
-
-       account = emcore_get_account_reference(alarm_data->reference_id);
+       account = emcore_get_account_reference(alarm_data->multi_user_name, alarm_data->reference_id);
        if (account == NULL) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]",err);
                goto FINISH_OFF;
@@ -226,7 +213,7 @@ FINISH_OFF:
 
 
 
-static int _emdaemon_create_alarm_for_auto_polling(int input_account_id)
+static int _emdaemon_create_alarm_for_auto_polling(char *multi_user_name, int input_account_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d]", input_account_id);
        int err = EMAIL_ERROR_NONE;
@@ -241,7 +228,7 @@ static int _emdaemon_create_alarm_for_auto_polling(int input_account_id)
 
        time(&current_time);
 
-       if ((err = emcore_calc_next_time_to_sync(input_account_id, current_time, &trigger_at_time)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_calc_next_time_to_sync(multi_user_name, input_account_id, current_time, &trigger_at_time)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
                goto FINISH_OFF;
        }
@@ -249,7 +236,7 @@ static int _emdaemon_create_alarm_for_auto_polling(int input_account_id)
        if (trigger_at_time == 0) {
                EM_DEBUG_LOG("trigger_at_time is 0. It means auto polling is disabled");
        }
-       else if ((err = emcore_add_alarm(trigger_at_time, EMAIL_ALARM_CLASS_AUTO_POLLING, input_account_id, emdaemon_alarm_polling_cb, NULL)) != EMAIL_ERROR_NONE) {
+       else if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_AUTO_POLLING, input_account_id, emdaemon_alarm_polling_cb, NULL)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err);
                goto FINISH_OFF;
        }
index d193b8f..d6af25a 100755 (executable)
@@ -426,7 +426,7 @@ static int _get_emn_account(unsigned char *input_wbxml, int input_wbxml_length,
        if(incoming_server_user_name && host_addr)
                SNPRINTF((char*)email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", incoming_server_user_name, host_addr);
 
-       if (!emdaemon_get_account_list(&accounts, &count, &err)) {
+       if (!emdaemon_get_account_list(NULL, &accounts, &count, &err)) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_DB_FAILURE");
                err = EMAIL_ERROR_DB_FAILURE;
                goto FINISH_OFF;
@@ -524,19 +524,19 @@ static int emdaemon_handle_emn_notification(unsigned char* wbxml_b64, int input_
        }
 
        if (account.incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && mailbox_name)  {
-               if (!emstorage_get_mailbox_by_name(account.account_id, -1, mailbox_name, &mailbox_tbl, false, &err))  {
+               if (!emstorage_get_mailbox_by_name(multi_user_name, account.account_id, -1, mailbox_name, &mailbox_tbl, false, &err))  {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d", err);
                        goto FINISH_OFF;
                }
        }
        else {
-               if (!emstorage_get_mailbox_by_mailbox_type(account.account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl, false, &err))  {
+               if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account.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;
                }
        }
 
-       if (!emdaemon_sync_header(mailbox_tbl->account_id, mailbox_tbl->mailbox_id, &handle, &err))  {
+       if (!emdaemon_sync_header(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, &handle, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_sync_header failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -574,7 +574,7 @@ static int emdaemon_register_wap_push_callback(msg_handle_t *input_msg_handle, c
        msg_error_t msg_err = MSG_SUCCESS;
        msg_struct_t msg_struct = NULL;
        char *content_type = "application/vnd.wap.emn+wbxml";
-       char *pkg_name = "com.samsung.email";
+       char *pkg_name = "org.tizen.email";
        bool bLaunch = false;
 
        if(input_msg_handle == NULL || input_app_id == NULL) {
index 073c1bd..d1ae293 100755 (executable)
@@ -36,6 +36,7 @@
 #include <vconf.h>
 #include <vconf-keys.h>
 #include <glib.h>
+#include <gio/gio.h>
 #include <glib-object.h>
 #include <sys/smack.h>
 #include <notification.h>
 #include "email-core-utils.h"
 #include "email-core-alarm.h"
 #include "email-core-smtp.h"
-#include "email-core-sound.h"
 #include "email-utilities.h"
 #include "email-storage.h"
 #include "email-ipc.h"
+#include "email-dbus-activation.h"
 
 extern pthread_mutex_t sound_mutex;
 extern pthread_cond_t sound_condition;
@@ -98,7 +99,6 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
        int ret = false;
        int err = EMAIL_ERROR_NONE;     
        int handle = 0;
-       email_account_t* ref_account = NULL;
        
        if (account_id <= 0)  {
                EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
@@ -108,6 +108,8 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
 
 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
 
+       /* Removed below code, as it is causing struck in composer */
+#if 0
        /*      h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
                since this code has update DB operation which is failing in context of email application process 
                with an sqlite error -> sqlite3_step fail:8 */
@@ -128,7 +130,7 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
                else {                  
                        mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
 
-                       if(!emstorage_set_field_of_mails_with_integer_value(mail_tbl_data->account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_CANCELED, true, &err)) {
+                       if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_tbl_data->account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_CANCELED, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
                                goto FINISH_OFF;
                        }
@@ -136,6 +138,7 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
 
 
        }
+#endif
 
        if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_SCHEDULED_SENDING, mail_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_LOG("emcore_delete_alram_data_by_reference_id failed [%d]",err);
@@ -143,21 +146,14 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
 
 #endif
 
-       if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
+       if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle)) {
                EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
-               err = EMAIL_ERROR_HANDLE_NOT_FOUND;
-               goto FINISH_OFF;
-       }
-
-       if (!(ref_account = emcore_get_account_reference(account_id)))  {
-               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
-               err = EMAIL_ERROR_INVALID_ACCOUNT;
+               ret = true;
                goto FINISH_OFF;
        }
 
-       if (!emcore_cancel_send_mail_thread(handle, NULL, &err))  {
+       if (!emcore_cancel_send_mail_thread(handle, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
-               goto FINISH_OFF;
        }
        
        if(!emcore_delete_transaction_info_by_mailId(mail_id))
@@ -168,205 +164,17 @@ INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id,
 FINISH_OFF:
        if(err_code != NULL)
                *err_code = err;
-       
-       if (ref_account) {
-               emcore_free_account(ref_account);
-               EM_SAFE_FREE(ref_account);
-       }
-
+#if 0
 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
        if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
                EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);       
-
+#endif
 #endif
        EM_DEBUG_FUNC_END();
        return ret;
 }      
 
-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 char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
-{
-       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;
-       
-       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_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_ANSWERED :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
-               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);
-                       break;
-
-               case EMAIL_SEARCH_FILTER_TYPE_BCC :
-               case EMAIL_SEARCH_FILTER_TYPE_CC :
-               case EMAIL_SEARCH_FILTER_TYPE_TO :
-               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;
-
-               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, EM_SAFE_STRLEN(temp_criteria));
-                       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, EM_SAFE_STRLEN(temp_criteria));
-                       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, EM_SAFE_STRLEN(temp_criteria));
-                       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, EM_SAFE_STRLEN(temp_criteria));
-                       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_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, EM_SAFE_STRLEN(temp_criteria));
-                       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*/
-       }
-
-FINISH_OFF:
-       
-       EM_SAFE_FREE(temp_criteria);
-       EM_SAFE_FREE(time_string);
-
-       if (err_code != NULL)
-               *err_code = err;
-               
-       EM_DEBUG_FUNC_END();
-       return criteria;
-}
-
-INTERNAL_FUNC int emdaemon_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, unsigned int *output_handle, int *err_code)
-{
-       EM_DEBUG_FUNC_BEGIN("input_account_id [%d], mailbox_id [%d], input_search_filter [%p], input_search_filter_count [%d], output_handle [%p]", input_account_id, input_mailbox_id, input_search_filter, input_search_filter_count, output_handle);
-       int error = EMAIL_ERROR_NONE;
-       int ret = false;
-       char *criteria = NULL;
-       email_event_t *event_data = NULL;
-       
-       if (input_mailbox_id == 0 || input_account_id < 0) {
-               EM_DEBUG_EXCEPTION("Invalid parameter");
-               error = EMAIL_ERROR_INVALID_PARAM;
-               return false;
-       }
-
-       criteria = _make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &error);
-       if (criteria == NULL) {
-               EM_DEBUG_EXCEPTION("_make_criteria_to_search_filter failed");
-               goto FINISH_OFF;
-       }
-
-       event_data = em_malloc(sizeof(email_event_t));
-       event_data->type = EMAIL_EVENT_SEARCH_ON_SERVER;
-       event_data->account_id = input_account_id;
-       event_data->event_param_data_1 = EM_SAFE_STRDUP(criteria);
-       event_data->event_param_data_4 = input_mailbox_id;
-
-       if (!emcore_insert_event(event_data, (int *)output_handle, &error)) {
-               EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
-               error = EMAIL_ERROR_NONE;
-               goto FINISH_OFF;
-       }
-
-       ret = true;
-
-FINISH_OFF:
-
-       if (ret == false && event_data) {
-               emcore_free_event(event_data);
-               EM_SAFE_FREE(event_data);
-       }
-
-       EM_SAFE_FREE(criteria);
-       
-       if (err_code != NULL)
-               *err_code = error;
-
-       EM_DEBUG_FUNC_END("error [%d]", error);
-       return ret;
-}
-
+#if 0
 INTERNAL_FUNC int emdaemon_reschedule_sending_mail()
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -408,42 +216,44 @@ INTERNAL_FUNC int emdaemon_reschedule_sending_mail()
        filter_list[6].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS;
 
        /* Get scheduled mail list */
-       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_rule_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
+       if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
                goto FINISH_OFF;
        }
 
        EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
 
-       if(!emstorage_query_mail_list(conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
+       if(!emstorage_query_mail_list(NULL, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
                EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
                goto FINISH_OFF;
        }
 
        /* Add alarm for scheduled mail */
        for(i = 0; i < result_mail_count; i++) {
-               if((err = emcore_schedule_sending_mail(result_mail_list[i].mail_id, result_mail_list[i].scheduled_sending_time)) != EMAIL_ERROR_NONE) {
+               if((err = emcore_schedule_sending_mail(multi_user_name, result_mail_list[i].mail_id, result_mail_list[i].scheduled_sending_time)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_schedule_sending_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
 
 FINISH_OFF:
+       EM_SAFE_FREE (conditional_clause_string); /* detected by valgrind */
        EM_SAFE_FREE(result_mail_list);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
+#endif
 
-INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
+INTERNAL_FUNC int emdaemon_clear_all_mail_data(char *multi_user_name, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
        
        int ret = false;
        int error = EMAIL_ERROR_NONE;
        
-       if (emdaemon_initialize(&error)) {
-               if (!emstorage_clear_mail_data(true, &error))
+       if (emdaemon_initialize(multi_user_name, &error)) {
+               if (!emstorage_clear_mail_data(multi_user_name, true, &error))
                        EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
        }
        else {
@@ -453,11 +263,11 @@ INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
                return false;
        }
 
-       emcore_display_unread_in_badge();
+       emcore_display_unread_in_badge(NULL);
 
        ret = true;
 
-       if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) 
+       if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) 
                EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
        
        emdaemon_finalize(&error);
@@ -468,36 +278,12 @@ INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
     return ret;
 }
 
-INTERNAL_FUNC int emdaemon_kill_daemon_if_no_account()
-{
-       EM_DEBUG_FUNC_BEGIN();
-       int err = EMAIL_ERROR_NONE;
-       email_account_t *account_list = NULL;
-       int account_count = 0;
-
-       if (!emcore_get_account_reference_list(&account_list, &account_count, &err)) {
-               EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
-       }
-
-       EM_DEBUG_LOG("account_count [%d]", account_count);
-
-       if (account_count == 0) {
-               EM_DEBUG_LOG("kill email-service daemon");
-               exit(44); /* exit with exit code 44 to prevent restarting */
-       }
-
-       if(account_list)
-               emcore_free_account_list(&account_list, account_count, NULL);
-
-       EM_DEBUG_FUNC_END("err [%d]", err);
-       return err;
-}
 
 INTERNAL_FUNC int emdaemon_check_smack_rule(int app_sockfd, char *file_path)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("app_sockfd[%d], file_path[%s]", app_sockfd, file_path);
 
-       if (app_sockfd <= 0 || !file_path) {
+       if (app_sockfd < 0 || !file_path) {
                EM_DEBUG_LOG("Invalid parameter");
                return false;
        }
@@ -584,52 +370,54 @@ INTERNAL_FUNC int emdaemon_set_smack_label(char *file_path, char *label)
        return result;
 }
 
-INTERNAL_FUNC int emdaemon_finalize_sync(int account_id, int total_mail_count, int unread_mail_count, int *error)
+INTERNAL_FUNC int emdaemon_finalize_sync(char *multi_user_name, int account_id, int total_mail_count, int unread_mail_count, int vip_total_mail_count, int vip_unread_mail_count, int input_from_eas, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], total_mail_count [%d], unread_mail_count [%d], error [%p]", account_id, total_mail_count, unread_mail_count, error);
        int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
        int topmost = false;
 
-       if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+       if ((err = emcore_update_sync_status_of_account(multi_user_name, account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
 
-       if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
+       if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &err))
                EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
 
        /* Check the topmost of email app */
-       if (vconf_get_int(VCONF_KEY_TOPMOST_WINDOW, &topmost) != 0) {
-               EM_DEBUG_EXCEPTION("vconf_get_int failed");
+       if (input_from_eas) {
+               if (vconf_get_int(VCONF_KEY_TOPMOST_WINDOW, &topmost) != 0) {
+                       EM_DEBUG_EXCEPTION("vconf_get_int failed");
+               }
        }
-
+/*
        if (result_sync_status == SYNC_STATUS_SYNCING) {
                if (topmost) {
                        EM_DEBUG_LOG("The email app is topmost");
+                       if (!emstorage_update_save_status(multi_user_name, account_id, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_save_status failed : [%d]", err);
+                       }
                } else {
-                       if ((err = emcore_add_notification(account_id, 0, total_mail_count, 0, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_TICKER)) != EMAIL_ERROR_NONE)
+                       if ((err = emcore_add_notification(multi_user_name, account_id, 0, unread_mail_count, vip_unread_mail_count, 0, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_TICKER)) != EMAIL_ERROR_NONE)
                                EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
                }
-       } else if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
+       } else*/ if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
                if (topmost) {
                        EM_DEBUG_LOG("The email app is topmost");
                } else {
 
-                       if ((err = emcore_add_notification(account_id, 0, total_mail_count, 1, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL)) != EMAIL_ERROR_NONE)
+                       emcore_set_flash_noti();
+
+                       if ((err = emcore_add_notification(multi_user_name, account_id, 0, unread_mail_count, vip_unread_mail_count, 1, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL)) != EMAIL_ERROR_NONE)
                                EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
 
 #ifdef __FEATURE_BLOCKING_MODE__
                        emcore_set_blocking_mode_status(false);
 #endif /* __FEATURE_BLOCKING_MODE__ */
 
-                       if ((err = emcore_start_driving_mode(account_id)) != EMAIL_ERROR_NONE) {
-                               EM_DEBUG_EXCEPTION("emcore_start_driving_mode failed : [%d]", err);
-                       }
+               
+                       if ((err = emcore_update_sync_status_of_account(multi_user_name, account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
+                               EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
                }
 
-               if (!emcore_display_unread_in_badge())
-                       EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
-
-               if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
-                       EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
        } else {
                EM_DEBUG_LOG("sync status : [%d]", result_sync_status);
        }
index c3575d8..5435ebe 100755 (executable)
@@ -25,7 +25,8 @@
 #include <pthread.h>
 #include <vconf.h>
 #include <signal.h>
-//#include <contacts.h>
+#include <contacts.h>
+#include <contacts_internal.h>
 #include "c-client.h"
 #include "email-convert.h"
 #include "email-storage.h"
@@ -44,7 +45,6 @@
 #include "email-core-mailbox-sync.h"
 #include "email-core-smtp.h"
 #include "email-core-utils.h"
-#include "email-core-sound.h"
 #include "email-core-signal.h"
 #include "email-debug-log.h"
 
@@ -60,28 +60,31 @@ extern GQueue *g_event_que;
 extern int g_event_loop;
 extern int recv_thread_run;
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+extern pthread_cond_t  _auto_downalod_available_signal;
+#endif
+
 static void *worker_event_queue(void *arg);
-static int event_handler_EMAIL_EVENT_SYNC_HEADER(int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(email_account_t *input_account, int input_handle_to_be_published);
-static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id);
-static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[], int num, email_flags_field_type field_type, int value, int *error);
-static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_UPDATE_MAIL(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published);
-static int event_handler_EMAIL_EVENT_SAVE_MAIL(int input_account_id, int input_mail_id, int input_handle_to_be_published);
-static int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int recursive, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error);
-static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int input_mailbox_id, int input_from_server, int *error);
-static int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error);
-static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, int handle_to_be_published, int *error);
-static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published);
+static int event_handler_EMAIL_EVENT_SYNC_HEADER(char *multi_user_name, int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(char *multi_user_name, email_account_t *account, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(char *multi_user_name, email_account_t *input_account, int input_handle_to_be_published);
+static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(char *multi_user_name, int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id);
+static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int account_id, int mail_id, int option, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name, int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error);
+static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(char *multi_user_name, int account_id, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_UPDATE_MAIL(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published);
+static int event_handler_EMAIL_EVENT_SAVE_MAIL(char *multi_user_name, int input_account_id, int input_mail_id, int input_handle_to_be_published);
+static int event_handler_EMAIL_EVENT_MOVE_MAIL(char *multi_user_name, int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(char *multi_user_name, int mailbox_id, int on_server, int recursive, int handle_to_be_published);
+static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(char *multi_user_name, email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published);
+static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(char *multi_user_name, int mail_id, int event_handle, int *error);
+static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int input_account_id, int input_mailbox_id, int input_from_server, int *error);
+static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error);
+static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int account_id, char *maibox_name, int handle_to_be_published, int *error);
+static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published);
 
 static void* worker_send_event_queue(void *arg);
 
@@ -101,13 +104,77 @@ extern pthread_mutex_t _partial_body_thd_event_queue_lock;
 extern email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
 extern email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
 extern int g_partial_body_thd_next_event_idx;
-extern int g_partial_body_thd_loop;
+//extern int g_partial_body_thd_loop;
 extern int g_partial_body_thd_queue_empty;
 extern int g_partial_body_thd_queue_full;
 extern int g_partial_body_bulk_dwd_queue_empty;
 
+extern email_event_t *sync_failed_event_data;
+
 static gpointer partial_body_download_thread(gpointer data);
 
+#ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
+
+#include <openssl/crypto.h>
+
+#define MAX_THREAD_NUMBER   100
+
+static pthread_mutex_t *lock_cs;
+static long *lock_count;
+
+void pthreads_locking_callback(int mode, int type, char *file, int line)
+{
+       if (mode & CRYPTO_LOCK) {
+               pthread_mutex_lock(&(lock_cs[type]));
+               lock_count[type]++;
+       }
+       else {
+               pthread_mutex_unlock(&(lock_cs[type]));
+       }
+}
+
+unsigned long pthreads_thread_id(void)
+{
+       return (unsigned long)pthread_self();
+}
+
+INTERNAL_FUNC void emdaemon_setup_handler_for_open_ssl_multithread(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int i = 0;
+
+       lock_cs    = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
+       lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+
+       for (i = 0; i < CRYPTO_num_locks(); i++) {
+               lock_count[i] = 0;
+               pthread_mutex_init(&(lock_cs[i]),NULL);
+       }
+
+       CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
+       CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
+       EM_DEBUG_FUNC_END();
+}
+
+INTERNAL_FUNC void emdaemon_cleanup_handler_for_open_ssl_multithread(void)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int i = 0;
+
+       CRYPTO_set_locking_callback(NULL);
+       for (i = 0; i < CRYPTO_num_locks(); i++)
+       {
+               pthread_mutex_destroy(&(lock_cs[i]));
+               EM_DEBUG_LOG("%8ld:%s",lock_count[i], CRYPTO_get_lock_name(i));
+       }
+       OPENSSL_free(lock_cs);
+       OPENSSL_free(lock_count);
+
+       EM_DEBUG_FUNC_END();
+}
+
+#endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
+
 /* start api event_data loop */
 INTERNAL_FUNC int emdaemon_start_event_loop(int *err_code)
 {
@@ -208,8 +275,10 @@ static void* worker_event_queue(void *arg)
        email_event_t *started_event = NULL;
        emstorage_account_tbl_t *account_tbl = NULL;
        int handle_to_be_published = 0;
+       int pbd_thd_state = 0;
+       int send_event_que_state = 0;
 
-       if (!emstorage_open(&err)) {
+       if (!emstorage_open(NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
                return false;
        }
@@ -217,6 +286,9 @@ static void* worker_event_queue(void *arg)
        /* check that event_data loop is continuous */
        while (emcore_event_loop_continue()) {
 
+               pbd_thd_state = emcore_get_pbd_thd_state();
+               send_event_que_state = emcore_is_send_event_queue_empty();
+
                /* get a event_data from event_data queue */
                ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
                if (!emcore_retrieve_event(&event_data, &err)) {
@@ -227,7 +299,20 @@ static void* worker_event_queue(void *arg)
                        }
 
                        recv_thread_run = 0;
-                       emdevice_set_sleep_on_off(true, NULL);
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_RECEVING_WORKER, true, NULL);
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                       int wifi_status = 0;
+                       if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
+                       }
+
+                       EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
+
+                       if (!pbd_thd_state && send_event_que_state && wifi_status > 1) {
+                               WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+                       }
+#endif
 
                        //go to sleep when queue is empty
                        SLEEP_CONDITION_VARIABLE(_event_available_signal, *_event_queue_lock);
@@ -238,7 +323,7 @@ static void* worker_event_queue(void *arg)
                        LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
                        EM_DEBUG_LOG_DEV(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
 
-                       emdevice_set_sleep_on_off(false, NULL);
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_RECEVING_WORKER, false, NULL);
 
                        handle_to_be_published = event_data->handle;
                        EM_DEBUG_LOG("Handle to be Published  [%d]", handle_to_be_published);
@@ -251,7 +336,7 @@ static void* worker_event_queue(void *arg)
                                event_data->type == EMAIL_EVENT_DOWNLOAD_BODY ||
                                event_data->type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
 
-                               if (emcore_is_storage_full(&err)) {
+                               if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
                                        EM_DEBUG_EXCEPTION("Storage is full");
                                        switch (event_data->type) {
                                                case EMAIL_EVENT_SYNC_HEADER:
@@ -277,7 +362,7 @@ static void* worker_event_queue(void *arg)
 
 
                        if (event_data->account_id > 0) {
-                               if (!emstorage_get_account_by_id(event_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
+                               if (!emstorage_get_account_by_id(event_data->multi_user_name, event_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
                                        EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
                                }
                        }
@@ -288,118 +373,109 @@ static void* worker_event_queue(void *arg)
                        if (!account_tbl || account_tbl->sync_disabled == 0) {
                                switch (event_data->type) {
                                        case EMAIL_EVENT_SYNC_IMAP_MAILBOX:  /* get imap mailbox list  */
-                                               if (!emcore_sync_mailbox_list(event_data->account_id, event_data->event_param_data_3, handle_to_be_published, &err))
+                                               if (!emcore_sync_mailbox_list(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_3, handle_to_be_published, &err))
                                                        EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
                                                break;
 
                                        case EMAIL_EVENT_SYNC_HEADER:  /* synchronize mail header  */
                                                if (is_storage_full == false)
-                                                       event_handler_EMAIL_EVENT_SYNC_HEADER(event_data->account_id, event_data->event_param_data_5, handle_to_be_published,  &err);
+                                                       event_handler_EMAIL_EVENT_SYNC_HEADER(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_5, handle_to_be_published,  &err);
                                                break;
 
                                        case EMAIL_EVENT_SYNC_HEADER_OMA:  /*  synchronize mail header for OMA */
                                                if (is_storage_full == false)
-                                                       event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data->account_id, event_data->event_param_data_1, handle_to_be_published, &err);
+                                                       event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_1, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_DOWNLOAD_BODY:  /*  download mail body  */
                                                if (is_storage_full == false)
-                                                       event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
+                                                       event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:  /*  download attachment */
                                                if (is_storage_full == false)
-                                                       event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data->account_id, (int)event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
+                                                       event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data->multi_user_name, event_data->account_id, (int)event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
-                                               event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER((int*)event_data->event_param_data_3, event_data->event_param_data_4 , event_data->event_param_data_5, event_data->event_param_data_6, &err);
+                                               event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(event_data->multi_user_name, (int*)event_data->event_param_data_3, event_data->event_param_data_4 , event_data->event_param_data_5, event_data->event_param_data_6, &err);
                                                break;
 
                                        case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
-                                               event_handler_EMAIL_EVENT_DELETE_MAIL(event_data->account_id, (int*)event_data->event_param_data_3, event_data->event_param_data_4, &err);
+                                               event_handler_EMAIL_EVENT_DELETE_MAIL(event_data->multi_user_name, event_data->account_id, (int*)event_data->event_param_data_3, event_data->event_param_data_4, event_data->event_param_data_5, &err);
                                                break;
 
                                        case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
-                                               event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data->account_id, event_data->event_param_data_4, (int)event_data->event_param_data_5, &err);
+                                               event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, (int)event_data->event_param_data_5, &err);
                                                break;
 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
                                        case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
-                                               event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER((int)event_data->event_param_data_4, &err);
+                                               event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(event_data->multi_user_name, (int)event_data->event_param_data_4, handle_to_be_published, &err);
                                                break;
 #endif
 
-                                       case EMAIL_EVENT_CREATE_MAILBOX: {
-                                                       email_mailbox_t *new_mailbox = (email_mailbox_t*)event_data->event_param_data_1;
-
-                                                       if(new_mailbox) {
-                                                               event_handler_EMAIL_EVENT_CREATE_MAILBOX(new_mailbox, event_data->event_param_data_4, handle_to_be_published, &err);
-                                                       }
-                                               }
+                                       case EMAIL_EVENT_CREATE_MAILBOX: 
+                                               err = event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data->multi_user_name, (email_mailbox_t *)event_data->event_param_data_1, event_data->event_param_data_4, handle_to_be_published);
                                                break;
 
                                        case EMAIL_EVENT_DELETE_MAILBOX:
-                                               event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data->event_param_data_4, event_data->event_param_data_5, event_data->event_param_data_6,handle_to_be_published, &err);
+                                               err = event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data->multi_user_name, event_data->event_param_data_4, event_data->event_param_data_5, event_data->event_param_data_6,handle_to_be_published);
                                                break;
 
                                        case EMAIL_EVENT_SAVE_MAIL:
-                                               err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data->account_id, event_data->event_param_data_4, handle_to_be_published);
+                                               err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, handle_to_be_published);
                                                break;
 
                                        case EMAIL_EVENT_MOVE_MAIL:
-                                               event_handler_EMAIL_EVENT_MOVE_MAIL(event_data->account_id, (int  *)event_data->event_param_data_3, event_data->event_param_data_4, event_data->event_param_data_5, event_data->event_param_data_8, handle_to_be_published, &err);
+                                               event_handler_EMAIL_EVENT_MOVE_MAIL(event_data->multi_user_name, event_data->account_id, (int  *)event_data->event_param_data_3, event_data->event_param_data_4, event_data->event_param_data_5, event_data->event_param_data_8, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_VALIDATE_ACCOUNT:
-                                               event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data->account_id, handle_to_be_published, &err);
+                                               event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
                                                        email_account_t *account = (email_account_t *)event_data->event_param_data_1;
-                                                       event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(account, handle_to_be_published, &err);
+                                                       event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(event_data->multi_user_name, account, handle_to_be_published, &err);
                                                }
                                                break;
 
                                        case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
                                                        email_account_t *account = (email_account_t *)event_data->event_param_data_1;
-                                                       err = event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(account, handle_to_be_published);
+                                                       err = event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(event_data->multi_user_name, account, handle_to_be_published);
                                                }
                                                break;
 
                                        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->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;
 
                                        case EMAIL_EVENT_UPDATE_MAIL:
-                                               event_handler_EMAIL_EVENT_UPDATE_MAIL((email_mail_data_t*)event_data->event_param_data_1, (email_attachment_data_t*)event_data->event_param_data_2, event_data->event_param_data_4, (email_meeting_request_t*)event_data->event_param_data_3, event_data->event_param_data_5, handle_to_be_published);
+                                               event_handler_EMAIL_EVENT_UPDATE_MAIL(event_data->multi_user_name, (email_mail_data_t*)event_data->event_param_data_1, (email_attachment_data_t*)event_data->event_param_data_2, event_data->event_param_data_4, (email_meeting_request_t*)event_data->event_param_data_3, event_data->event_param_data_5, handle_to_be_published);
                                                break;
 
                                        case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
-                                               event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
+                                               event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
                                                break;
 
                                        case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
-                                               err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data->account_id, event_data->event_param_data_4);
+                                               err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4);
                                                break;
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
                                        case EMAIL_EVENT_LOCAL_ACTIVITY:
-                                               event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data->account_id, &err);
+                                               event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err);
                                                break;
 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
 
-                                       case EMAIL_EVENT_SEARCH_ON_SERVER:
-                                               event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(event_data->account_id, event_data->event_param_data_4, (char *)event_data->event_param_data_1, handle_to_be_published, &err);
-                                               break;
-
                                        case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
-                                               err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data->account_id, event_data->event_param_data_4, (char*)event_data->event_param_data_1, (char*)event_data->event_param_data_2, (char*)event_data->event_param_data_3, handle_to_be_published);
+                                               err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, (char*)event_data->event_param_data_1, (char*)event_data->event_param_data_2, (char*)event_data->event_param_data_3, handle_to_be_published);
                                                break;
 
                                        case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
-                                               if (!emcore_query_mail_size_limit(event_data->account_id, handle_to_be_published, &err))
+                                               if (!emcore_query_mail_size_limit(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err))
                                                        EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
                                                break;
 
@@ -408,6 +484,33 @@ 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)) {
+                               EM_DEBUG_LOG("retry syncing");
+                               /* 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 (event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX) {
+                                       sync_failed_event_data->type               = EMAIL_EVENT_SYNC_IMAP_MAILBOX;
+                                       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);
+                               }
+
+                               if (event_data->type == EMAIL_EVENT_SYNC_HEADER) {
+                                       sync_failed_event_data->type               = EMAIL_EVENT_SYNC_HEADER;
+                                       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_5 = event_data->event_param_data_5;
+                                       sync_failed_event_data->event_param_data_4 = event_data->event_param_data_4;
+                               }
+                       }
+
                        /* free internals in event */
                        emcore_free_event(event_data); /*detected by valgrind*/
 
@@ -437,11 +540,12 @@ static void* worker_event_queue(void *arg)
        if (!emstorage_close(&err))
                EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
 
+       emcore_close_recv_stream_list();
        EM_DEBUG_FUNC_END();
        return SUCCESS;
 }
 
-static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int input_mailbox_id, 
+static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int input_account_id, int input_mailbox_id, 
                                                                        int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", 
@@ -451,6 +555,8 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
        int mailbox_count = 0, account_count = 0;
        int counter, account_index;
        int unread = 0, total_unread = 0;
+       int vip_unread = 0, vip_total_unread = 0;
+       int vip_mail_count = 0, vip_total_mail = 0;
        int mail_count = 0, total_mail = 0;
        emcore_uid_list *uid_list = NULL;
        emstorage_account_tbl_t *account_tbl_array = NULL;
@@ -464,7 +570,25 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
        if (input_mailbox_id == 0)
                sync_type = EMAIL_SYNC_ALL_MAILBOX;
        else {
-               if (!emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
+               email_account_t *ref_account = NULL;
+               int sync_disabled = 0;
+
+               ref_account = emcore_get_account_reference(multi_user_name, input_account_id);
+               if (ref_account) {
+                       sync_disabled = ref_account->sync_disabled;
+                       emcore_free_account(ref_account);
+                       EM_SAFE_FREE(ref_account);
+               }
+
+               EM_DEBUG_LOG("sync_disabled[%d]", sync_disabled);
+
+               if (sync_disabled) {
+                       err = EMAIL_ERROR_ACCOUNT_SYNC_IS_DISALBED;
+                       EM_DEBUG_LOG("Sync disabled for this account. Do not sync.");
+                       goto FINISH_OFF;
+               }
+
+               if (!emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -486,10 +610,11 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
 
        if (sync_type != EMAIL_SYNC_ALL_MAILBOX) {      /* Sync only particular mailbox */
                EM_DEBUG_LOG_SEC ("sync start: account_id [%d] alias [%s]", input_account_id, mailbox_tbl_target->alias);
-               if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+               if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
 
-               if (!emcore_sync_header (mailbox_tbl_target, (void**) &stream, &uid_list, &mail_count, &unread, &err)) {
+               if (!emcore_sync_header (multi_user_name, mailbox_tbl_target, (void**) &stream,
+                                       &uid_list, &mail_count, &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
                        if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
                                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
@@ -499,25 +624,44 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                        if (!emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
                                EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
                }
+
                stream = mail_close (stream);
-               total_unread += unread;
-               total_mail += mail_count;
 
-               if (total_unread > 0 && (err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
-                       EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
+               total_unread     += unread;
+               vip_total_unread += vip_unread;
+               total_mail       += mail_count;
+               vip_total_mail   += vip_mail_count;
+
+               if (total_unread > 0 && (emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed");
+               } else {
+                       if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+                               EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
+               }
+
+               if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
+               }
+
+               if (account_tbl_array && account_tbl_array->auto_download_size == 0) {
+                       if (!emdaemon_finalize_sync(multi_user_name, input_account_id, 0, 0, 0, 0, true, NULL))
+                               EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
+               }
+
+               if (account_tbl_array)
+                       emstorage_free_account(&account_tbl_array, 1, NULL);
 
-               if (!emdaemon_finalize_sync(input_account_id, total_mail, total_unread, &err))
-                       EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
+               if (mailbox_tbl_target->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
+                       emcore_display_unread_in_badge(NULL);
        }
-       else /*  All Foder */ {
+       else /*  All Folder */ {
                EM_DEBUG_LOG ("sync start for all mailbox: account_id [%d]", input_account_id);
                /*  Sync of all mailbox */
 
                if (input_account_id == ALL_ACCOUNT) {
-                       if ((err = emcore_update_sync_status_of_account(ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+                       if ((err = emcore_update_sync_status_of_account(multi_user_name, ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                                EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
-
-                       if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &err)) {
+                       if (!emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_array , true, false, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
                                if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL,  handle_to_be_published, err))
                                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
@@ -525,10 +669,10 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                        }
                }
                else {
-                       if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+                       if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                                EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
 
-                       if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
+                       if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
                                if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
                                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
@@ -543,7 +687,12 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                                continue;
                        }
 
-                       if (!emstorage_get_mailbox_list (account_tbl_array[account_index].account_id, 0, 
+                       if (account_tbl_array[account_index].sync_disabled == 1) {
+                               EM_DEBUG_LOG ("account[%d] is sync-disabled. Skip  ", account_index);
+                               continue;
+                       }
+/* folder sync is also necessary */
+                       if (!emstorage_get_mailbox_list (multi_user_name, account_tbl_array[account_index].account_id, 0, 
                                   EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || 
                                                                                                   mailbox_count <= 0) {
                                EM_DEBUG_EXCEPTION ("emstorage_get_mailbox error [%d]", err);
@@ -560,7 +709,7 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                        if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
                                memset(mailbox_id_param_string, 0, 10);
                                SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
-                               if (!emcore_connect_to_remote_mailbox (account_tbl_array[account_index].account_id, 
+                               if (!emcore_connect_to_remote_mailbox (multi_user_name, account_tbl_array[account_index].account_id, 
                                                       mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err))  {
                                        EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox error [%d]", err);
                                        if (err == EMAIL_ERROR_LOGIN_FAILURE)
@@ -579,7 +728,7 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                        for (counter = 0; counter < mailbox_count; counter++) {
 
                                EM_DEBUG_LOG_SEC("mailbox_name [%s], mailbox_id [%d], mailbox_type [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, mailbox_tbl_list[counter].mailbox_type);
-                               unread = mail_count = 0;
+                               vip_unread = unread = mail_count = 0;
                                if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
                                        || mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
                                        /*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */) {
@@ -589,11 +738,19 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                                else if (!mailbox_tbl_list[counter].local_yn) {
                                        EM_DEBUG_LOG_SEC("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
 #ifdef __FEATURE_KEEP_CONNECTION__
-                                       if (!emcore_sync_header ((mailbox_tbl_list + counter), (void **)&stream, &uid_list, 
-                                                                                           &mail_count, &unread, &err))
+                                       if (!emcore_sync_header (multi_user_name,
+                                      (mailbox_tbl_list + counter), 
+                                      (void **)&stream, 
+                                      &uid_list, 
+                                      &mail_count, 
+                                      &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
 #else /*  __FEATURE_KEEP_CONNECTION__ */
-                                       if (!emcore_sync_header ((mailbox_tbl_list + counter), (void **)&stream, &uid_list, 
-                                                                                           &mail_count, &unread, &err))
+                                       if (!emcore_sync_header (multi_user_name, 
+                                      (mailbox_tbl_list + counter), 
+                                      (void **)&stream, 
+                                      &uid_list, 
+                                      &mail_count, 
+                                      &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
 #endif /*  __FEATURE_KEEP_CONNECTION__ */
                                        {       
                                                EM_DEBUG_EXCEPTION_SEC ("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", 
@@ -617,19 +774,34 @@ static int event_handler_EMAIL_EVENT_SYNC_HEADER (int input_account_id, int inpu
                                }
                                EM_DEBUG_LOG_SEC("---mailbox %s has unread %d / %d", mailbox_tbl_list[counter].mailbox_name, 
                                                                                                 unread, mail_count);
-                               total_unread  += unread;
-                               total_mail += mail_count;
+                               total_unread     += unread;
+                               vip_total_unread += vip_unread;
+                               total_mail       += mail_count;
+                               vip_total_mail   += vip_mail_count;
+
+                               if (mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
+                                       emcore_display_unread_in_badge(NULL);
+
                        }
 
                        EM_DEBUG_LOG_SEC("Sync for account_id(%d) is completed....! (unread %d/%d)", account_tbl_array[account_index].account_id, total_unread, total_mail);
+                       EM_DEBUG_LOG_SEC("Sync for account_id(%d) is completed....! (vip_unread %d/%d)", account_tbl_array[account_index].account_id, vip_total_unread, vip_total_mail);
+
                        if ((err == EMAIL_ERROR_NONE) && !emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
                                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");
 
-                       if ((total_unread > 0) && (err = emcore_update_sync_status_of_account(account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
-                               EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
+                       if ((total_unread > 0) && (emcore_update_sync_status_of_account(multi_user_name, account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed");
+                       } else {
+                               if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+                                       EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
+                       }
+
+                       if (account_tbl_array[account_index].auto_download_size == 0) {
+                               if (!emdaemon_finalize_sync(multi_user_name, account_tbl_array[account_index].account_id, 0, 0, 0, 0, true, NULL))
+                                       EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
+                       }
 
-                       if (!emdaemon_finalize_sync(account_tbl_array[account_index].account_id, total_mail, total_unread, &err))
-                               EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
 #ifndef __FEATURE_KEEP_CONNECTION__
                        if (stream)  
                                stream = mail_close (stream);
@@ -650,6 +822,9 @@ FINISH_OFF:
        if (stream)
                stream = mail_close (stream);
 #endif
+       if (error)
+               *error = err;
+
        if(mailbox_tbl_target)
                emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);
 
@@ -663,10 +838,11 @@ FINISH_OFF:
        return ret;
 }
 
-static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(char *multi_user_name, email_account_t *account, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("account [%p]", account);
        int err, ret = false;
+       char *imap_cap_string = NULL;
 
        if(!account) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -686,7 +862,7 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t
        else {
                EM_DEBUG_LOG_SEC("incoming_server_address : %s", account->incoming_server_address);
 
-               if (!emcore_validate_account_with_account_info(account, EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT, &err)) {
+               if (!emcore_validate_account_with_account_info(multi_user_name, account, EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT, &imap_cap_string, handle_to_be_published, &err)) {
                        emcore_delete_account_from_unvalidated_account_list(account->account_id);
                        EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
                        if (err == EMAIL_ERROR_CANCELLED) {
@@ -701,26 +877,24 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t
                else {
                        emcore_delete_account_from_unvalidated_account_list(account->account_id);
 
-                       if (emcore_create_account(account, &err) == false)       {
+                       if (emcore_create_account(multi_user_name, account, false, &err) == false) {
                                EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
                                goto FINISH_OFF;
                        }
 
-                       emcore_init_account_reference();
-
                        EM_DEBUG_LOG("incoming_server_type [%d]", account->incoming_server_type);
 
                        if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
-                               if (!emcore_sync_mailbox_list(account->account_id, "", handle_to_be_published, &err))  {
+                               if (!emcore_sync_mailbox_list(multi_user_name, account->account_id, "", handle_to_be_published, &err))  {
                                        EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync failed [%d]", err);
                                        /*  delete account whose mailbox couldn't be obtained from server */
-                                       emcore_delete_account(account->account_id, NULL);
+                                       emcore_delete_account(multi_user_name, account->account_id, false, NULL);
                                        goto FINISH_OFF;
                                }
                        }
 
                        EM_DEBUG_LOG("validating and creating an account are succeeded for account id  [%d]  err [%d]", account->account_id, err);
-                       if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, NULL,  handle_to_be_published, err))
+                       if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, imap_cap_string,  handle_to_be_published, err))
                                EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
                }
        }
@@ -733,6 +907,8 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
        }
 
+       EM_SAFE_FREE(imap_cap_string);
+
        if (error)
                *error = err;
 
@@ -740,10 +916,11 @@ FINISH_OFF:
        return ret;
 }
 
-static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(email_account_t *input_account, int input_handle_to_be_published)
+static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(char *multi_user_name, email_account_t *input_account, int input_handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("input_account [%p]", input_account);
        int err = EMAIL_ERROR_NONE;
+       char *server_capability_string = NULL;
 
        if(!input_account) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
@@ -763,7 +940,7 @@ static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(email_account_t *input_
        else {
                EM_DEBUG_LOG_SEC("incoming_server_address : %s", input_account->incoming_server_address);
 
-               if (!emcore_validate_account_with_account_info(input_account, EMAIL_EVENT_VALIDATE_ACCOUNT_EX, &err)) {
+               if (!emcore_validate_account_with_account_info(multi_user_name, input_account, EMAIL_EVENT_VALIDATE_ACCOUNT_EX, &server_capability_string, input_handle_to_be_published, &err)) {
                        emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
 
                        EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
@@ -781,7 +958,7 @@ static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(email_account_t *input_
                        emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
 
                        EM_DEBUG_LOG("validating an account are succeeded for account id  [%d]  err [%d]", input_account->account_id, err);
-                       if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, input_account->account_id, NULL, input_handle_to_be_published, err))
+                       if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, input_account->account_id, server_capability_string, input_handle_to_be_published, err))
                                EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
                }
        }
@@ -792,15 +969,19 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed");
        }
 
+       EM_SAFE_FREE(server_capability_string);
+
        EM_DEBUG_FUNC_END("err[%d]", err);
        return err;
 }
 
-static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(char *multi_user_name, int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
        int err, ret = false;
+       char *imap_cap_string = NULL;
        emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;
+       email_account_t *duplicated_account_info = NULL, *old_account_info = NULL;
 
        if (!new_account_info) {
                EM_DEBUG_EXCEPTION("Invalid Parameter");
@@ -811,18 +992,62 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id,
        if (!emnetwork_check_network_status(&err))  {
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
 
-               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
+               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
                goto FINISH_OFF;
        }
        else  {
                EM_DEBUG_LOG_SEC("incoming_server_address: (%s)", new_account_info->incoming_server_address);
 
-               if (!emcore_validate_account_with_account_info(new_account_info, EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT, &err)) {
+               /* If the password fields are empty, fill the fields with password of old information*/
+               if((old_account_info = emcore_get_account_reference(multi_user_name, account_id)) == NULL) {
+                       EM_DEBUG_EXCEPTION("emcore_get_account_reference failed ");
+                       goto FINISH_OFF;
+               }
+
+               EM_DEBUG_LOG_SEC("old_account_info->incoming_server_password [%s]", old_account_info->incoming_server_password);
+
+               if( EM_SAFE_STRLEN (new_account_info->incoming_server_password) == 0 ) {
+                       EM_SAFE_FREE(new_account_info->incoming_server_password); /* be allocated but has zero length */
+                       EM_DEBUG_LOG_SEC("old_account_info->incoming_server_password [%s]", old_account_info->incoming_server_password);
+                       new_account_info->incoming_server_password = EM_SAFE_STRDUP(old_account_info->incoming_server_password);
+                       if(new_account_info->incoming_server_password == NULL) {
+                               EM_DEBUG_EXCEPTION("allocation for new_account_info->password failed");
+                               err = EMAIL_ERROR_OUT_OF_MEMORY;
+                               goto FINISH_OFF;
+                       }
+               }
+
+               EM_DEBUG_LOG_SEC("old_account_info->outgoing_server_password [%s]", old_account_info->outgoing_server_password);
+
+               if( EM_SAFE_STRLEN (new_account_info->outgoing_server_password) == 0 ) {
+                       EM_SAFE_FREE(new_account_info->outgoing_server_password);
+                       if(old_account_info->outgoing_server_password) {
+                               new_account_info->outgoing_server_password = EM_SAFE_STRDUP(old_account_info->outgoing_server_password);
+                               if(new_account_info->outgoing_server_password == NULL) {
+                                       EM_DEBUG_EXCEPTION("allocation for new_account_info->outgoing_server_password failed");
+                                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+                                       goto FINISH_OFF;
+                               }
+                       }
+               }
+
+               emcore_duplicate_account(new_account_info, &duplicated_account_info, &err);
+               if (err != EMAIL_ERROR_NONE ) {
+                       EM_DEBUG_EXCEPTION("emcore_duplicate_account failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if((err = emcore_add_account_to_unvalidated_account_list(duplicated_account_info)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               if (!emcore_validate_account_with_account_info(multi_user_name, duplicated_account_info, EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT, &imap_cap_string, handle_to_be_published, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err :  %d", err);
                        if (err == EMAIL_ERROR_CANCELLED) {
                                EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
-                               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, new_account_info->account_id, NULL,  handle_to_be_published, err))
+                               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, account_id, NULL,  handle_to_be_published, err))
                                        EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
                                goto FINISH_OFF;
                        }
@@ -831,10 +1056,11 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id,
                        }
                }
                else {
-                       if (!emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
+                       if (!emstorage_get_account_by_id(multi_user_name, account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
                                /* goto FINISH_OFF; */
                        }
+                       duplicated_account_info->account_id = account_id;
 
                        new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
                        if (!new_account_tbl) {
@@ -842,14 +1068,14 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id,
                                goto FINISH_OFF;
                        }
 
-                       em_convert_account_to_account_tbl(new_account_info, new_account_tbl);
+                       em_convert_account_to_account_tbl(duplicated_account_info, new_account_tbl);
 
-                       if (emstorage_update_account(account_id, new_account_tbl, true, &err)) {
-                               emcore_init_account_reference();
+                       if (!emstorage_update_account(multi_user_name, account_id, new_account_tbl, true, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_update_account failed : [%d]", err);
                        }
 
-                       EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", new_account_info->account_id, err);
-                       if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, NULL,  handle_to_be_published, err))
+                       EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", duplicated_account_info->account_id, err);
+                       if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, imap_cap_string, handle_to_be_published, err))
                                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
                }
        }
@@ -857,16 +1083,33 @@ static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id,
        ret = true;
 
 FINISH_OFF:
+
+       if (duplicated_account_info) {
+               if (duplicated_account_info->account_id < 0)
+                       emcore_delete_account_from_unvalidated_account_list(duplicated_account_info->account_id);
+               else {
+                       emcore_free_account(duplicated_account_info);
+                       EM_SAFE_FREE(duplicated_account_info);
+               }
+       }
+
+    if (old_account_info) {
+        emcore_free_account(old_account_info);
+        EM_SAFE_FREE(old_account_info);
+    }
+
        if (old_account_tbl)
                emstorage_free_account(&old_account_tbl, 1, NULL);
        if (new_account_tbl)
                emstorage_free_account(&new_account_tbl, 1, NULL);
 
-       if (ret == false && err != EMAIL_ERROR_CANCELLED && new_account_info) {
-               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
+       if (ret == false && err != EMAIL_ERROR_CANCELLED) {
+               if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
                        EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
        }
 
+       EM_SAFE_FREE(imap_cap_string);
+
        if (error)
                *error = err;
 
@@ -874,27 +1117,27 @@ FINISH_OFF:
        return ret;
 }
 
-static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
-       emcore_set_mail_slot_size(account_id, mailbox_id, new_slot_size, error);
+       emcore_set_mail_slot_size(multi_user_name, account_id, mailbox_id, new_slot_size, error);
 
        EM_DEBUG_FUNC_END();
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id)
+static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
        int err = EMAIL_ERROR_NONE;
 
-       if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
+       if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(multi_user_name, input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
+       if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -906,7 +1149,7 @@ FINISH_OFF:
 }
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
-static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
+static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(char *multi_user_name, int account_id, int event,handle, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -921,7 +1164,7 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
        if (!emnetwork_check_network_status(&err))
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
        else {
-               if (false == emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
+               if (false == emstorage_get_activity_id_list(multi_user_name, account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
                }
                else {
@@ -932,7 +1175,7 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
                                        EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
                                        switch (local_activity[0].activity_type) {
                                                case ACTIVITY_MODIFYFLAG:  {
-                                                       if (emcore_sync_flag_with_server(local_activity[0].mail_id , &err))  {
+                                                       if (emcore_sync_flag_with_server(multi_user_name, local_activity[0].mail_id , &err))  {
                                                                if (!emcore_delete_activity(&local_activity[0], &err))
                                                                        EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
                                                        }
@@ -963,19 +1206,20 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
 
                                                                switch (local_activity[k-1].activity_type) {
                                                                        case ACTIVITY_DELETEMAIL:  {
-                                                                               if (!emcore_delete_mail(local_activity[k-1].account_id,
-                                                                                                                               mail_id_list,
-                                                                                                                               j,
-                                                                                                                               EMAIL_DELETE_LOCAL_AND_SERVER,
-                                                                                                                               EMAIL_DELETED_BY_COMMAND,
-                                                                                                                               false,
-                                                                                                                               &err))
+                                                                               if (!emcore_delete_mail(multi_user_name,
+                                                                                                                                               local_activity[k-1].account_id,
+                                                                                                                                               mail_id_list,
+                                                                                                                                               j,
+                                                                                                                                               EMAIL_DELETE_LOCAL_AND_SERVER,
+                                                                                                                                               EMAIL_DELETED_BY_COMMAND,
+                                                                                                                                               false,
+                                                                                                                                               &err))
                                                                                        EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
                                                                        }
                                                                        break;
 
                                                                        case ACTIVITY_MOVEMAIL:  {
-                                                                               if (!emcore_move_mail_on_server_ex(local_activity[k-1].account_id ,
+                                                                               if (!emcore_move_mail_on_server_ex(multi_user_name, local_activity[k-1].account_id ,
                                                                                                                                                   local_activity[k-1].src_mbox,
                                                                                                                                                   mail_id_list,
                                                                                                                                                   j,
@@ -986,7 +1230,7 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
                                                                        break;
                                                                        case ACTIVITY_MODIFYSEENFLAG:  {
                                                                                int seen_flag = atoi(local_activity[0].src_mbox);
-                                                                               if (!emcore_sync_seen_flag_with_server_ex(mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
+                                                                               if (!emcore_sync_seen_flag_with_server_ex(multi_user_name, mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
                                                                                        EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
                                                                        }
                                                                        break;
@@ -1028,7 +1272,7 @@ static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
 }
 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
 
-static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int account_id, int mail_id, int option, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1043,17 +1287,57 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id,
 
                emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
        } else {
+#ifdef __FEATURE_USE_GMIME__
+               emstorage_mail_tbl_t *mail = NULL;
+               MAILSTREAM *tmp_stream = NULL;
+
+               if (!emstorage_get_mail_by_id (multi_user_name, mail_id, &mail, true, &err) || !mail) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
+                       emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
+                       goto FINISH_OFF;
+               }
+
+
+               if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
+                       EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
+                       if(err == EMAIL_ERROR_NO_SUCH_HOST)
+                               err = EMAIL_ERROR_CONNECTION_FAILURE;
+
+                       if (mail)
+                               emstorage_free_mail(&mail, 1, NULL);
+
+                       emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
+
+                       goto FINISH_OFF;
+               }
+
+               if (!emcore_gmime_download_body_sections(multi_user_name, tmp_stream, mailbox.account_id, mail_id,
+                               option & 0x01, NO_LIMITATION, handle_to_be_published, 1, 0, &err))
+                       EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
+
+               if (tmp_stream) {
+                       tmp_stream = mail_close (tmp_stream);
+               }
+
+               if (mail)
+                       emstorage_free_mail(&mail, 1, NULL);
+
+#else
                if (!emcore_download_body_multi_sections_bulk(NULL,
-                       mailbox.account_id,
-                       mail_id,
-                       option >> 1,            /*  0 :  silent, 1 :  verbose */
-                       option & 0x01,          /*  0 :  without attachments, 1 :  with attachments */
-                       NO_LIMITATION,
-                       handle_to_be_published,
-                       &err))
+                          mailbox.account_id,
+                          mail_id,
+                          option >> 1,         /*0: silent, 1: verbose */
+                          option & 0x01,       /*0: without attachments, 1: with attachments */
+                          NO_LIMITATION,
+                          handle_to_be_published,
+                          1,              /* cancel enabled */
+                          &err))
                        EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed - %d", err);
+#endif
        }
 
+FINISH_OFF:
+
        if (error)
                *error = err;
 
@@ -1061,7 +1345,7 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id,
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name, int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1076,12 +1360,19 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mai
        else  {
 
 #ifdef __ATTACHMENT_OPTI__
-               if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, &err))
+               if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, handle_to_be_published, &err))
                        EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
 #else
-               if (!emcore_download_attachment(account_id, mail_id, attachment_no, &err))
+
+#ifdef __FEATURE_USE_GMIME__
+               if (!emcore_gmime_download_attachment(multi_user_name, mail_id, attachment_no, 1, handle_to_be_published, 0, &err))
+                       EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
+#else
+               if (!emcore_download_attachment (account_id, mail_id, attachment_no, 1, handle_to_be_published, &err))
                        EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
 #endif
+
+#endif
        }
 
        if (error)
@@ -1091,7 +1382,7 @@ static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mai
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
+static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1099,7 +1390,7 @@ static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[],
 
        if (!emnetwork_check_network_status(&err))
                EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
-       else if (!emcore_sync_flags_field_with_server(mail_ids, num, field_type, value, &err))
+       else if (!emcore_sync_flags_field_with_server(multi_user_name, mail_ids, num, field_type, value, &err))
                EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
 
        if (error)
@@ -1109,7 +1400,7 @@ static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[],
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(char *multi_user_name, int account_id, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
 
@@ -1123,7 +1414,7 @@ static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle
        }
        else  {
 
-               if (!emcore_validate_account(account_id, &err)) {
+               if (!emcore_validate_account(multi_user_name, account_id, handle_to_be_published, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_validate_account failed account id  :  %d  err :  %d", account_id, err);
 
                        if (err == EMAIL_ERROR_CANCELLED) {
@@ -1138,14 +1429,17 @@ static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle
                }
                else {
                        email_account_t *account_ref = NULL;
-                       account_ref = emcore_get_account_reference(account_id);
+                       account_ref = emcore_get_account_reference(multi_user_name, account_id);
 
                        if (account_ref) {
                                EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
                                if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
-                                       if (!emcore_check_thread_status())
+                                       int dummy=0;
+                                       if (!emcore_check_event_thread_status(&dummy, handle_to_be_published)) {
+                                               EM_DEBUG_LOG("canceled type [%d]", dummy);
                                                err = EMAIL_ERROR_CANCELLED;
-                                       else if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
+                                       }
+                                       else if (!emcore_sync_mailbox_list(multi_user_name, account_id, "", handle_to_be_published, &err))
                                                EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
                                }
 
@@ -1168,35 +1462,36 @@ static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_UPDATE_MAIL(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published)
+static int event_handler_EMAIL_EVENT_UPDATE_MAIL(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
        int err = EMAIL_ERROR_NONE;
 /*
-       if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
+       if ( (err = emcore_update_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
                EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
 */
        EM_DEBUG_FUNC_END("err [%d", err);
        return err;
 }
-static int event_handler_EMAIL_EVENT_SAVE_MAIL(int input_account_id, int input_mail_id, int input_handle_to_be_published)
+
+static int event_handler_EMAIL_EVENT_SAVE_MAIL(char *multi_user_name, int input_account_id, int input_mail_id, int input_handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mail_id [%d] input_handle_to_be_published [%d]", input_account_id, input_mail_id, input_handle_to_be_published);
        int err = EMAIL_ERROR_NONE;
 
-       err = emcore_sync_mail_from_client_to_server(input_mail_id);
+       err = emcore_sync_mail_from_client_to_server(multi_user_name, input_mail_id);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
-static int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_MOVE_MAIL(char *multi_user_name, int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE, ret = false;
        email_account_t *account_ref = NULL;
 
-       if (!(account_ref = emcore_get_account_reference(account_id))) {
+       if (!(account_ref = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
@@ -1216,10 +1511,10 @@ static int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, in
                        EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
                else {
 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
-                       if (!emcore_move_mail_on_server_ex(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
+                       if (!emcore_move_mail_on_server_ex(multi_user_name, account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
                                EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
 #else
-                       if (!emcore_move_mail_on_server(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
+                       if (!emcore_move_mail_on_server(multi_user_name, account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
                                EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
 #endif
                }
@@ -1240,46 +1535,31 @@ FINISH_OFF:
        return ret;
 }
 
-static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int recursive, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(char *multi_user_name, int mailbox_id, int on_server, int recursive, int handle_to_be_published)
 {
-       EM_DEBUG_FUNC_BEGIN("mailbox_id[%d] on_server[%d] recursive[%d] handle_to_be_published[%d] error[%p]",  mailbox_id, on_server, recursive, handle_to_be_published, error);
+       EM_DEBUG_FUNC_BEGIN("mailbox_id[%d] on_server[%d] recursive[%d] handle_to_be_published[%d]",  mailbox_id, on_server, recursive, handle_to_be_published);
        int err = EMAIL_ERROR_NONE;
 
-       if (!emnetwork_check_network_status(&err))
-               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
-       else  {
-               if (( err = emcore_delete_mailbox(mailbox_id, on_server, recursive)) != EMAIL_ERROR_NONE)
-                       EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
-       }
-
-       if (error)
-               *error = err;
+       if ((err = emcore_delete_mailbox(multi_user_name, mailbox_id, on_server, recursive)) != EMAIL_ERROR_NONE)
+               EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
 
-       EM_DEBUG_FUNC_END();
-       return true;
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
-static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published, int *error)
+static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(char *multi_user_name, email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
 
-       if (!emnetwork_check_network_status(&err))  {
-               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
-       }
-       else  {
-               if (!emcore_create_mailbox(input_new_mailbox, on_server, &err))
-                       EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
-       }
-
-       if (error)
-               *error = err;
+       if (!emcore_create_mailbox(multi_user_name, input_new_mailbox, on_server, -1, -1, &err))
+               EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
 
-       EM_DEBUG_FUNC_END();
-       return true;
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
 }
 
-static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error)
+static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(char *multi_user_name, int mail_id, int event_handle, int *error)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);
 
@@ -1288,7 +1568,7 @@ static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *
        if (!emnetwork_check_network_status(&err))
                EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
        else {
-               if (!emcore_sync_flag_with_server(mail_id, &err))
+               if (!emcore_sync_flag_with_server(multi_user_name, mail_id, event_handle, &err))
                        EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
 #ifdef __FEATURE_LOCAL_ACTIVITY__
                else {
@@ -1314,12 +1594,12 @@ static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int input_mailbox_id, int input_from_server, int *error)
+static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int input_account_id, int input_mailbox_id, int input_from_server, int *error)
 {
        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(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, input_from_server, &err))
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
 
        if (error)
@@ -1329,20 +1609,20 @@ static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int i
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error)
+static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
        int ret = false;
        email_account_t *account_ref = NULL;
 
-       if (!(account_ref = emcore_get_account_reference(account_id))) {
+       if (!(account_ref = emcore_get_account_reference(multi_user_name, account_id))) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_FROM_SERVER, EMAIL_DELETED_BY_COMMAND, false, &err)) {
+       if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, mail_id_count, from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1362,7 +1642,7 @@ FINISH_OFF:
        return ret;
 }
 
-static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int handle_to_be_published, int *error)
+static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int account_id, char *maibox_name, int handle_to_be_published, int *error)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
@@ -1374,7 +1654,7 @@ static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibo
        }
        else  {
                EM_DEBUG_LOG("Sync of all mailbox");
-               if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
+               if (!emcore_sync_mailbox_list(multi_user_name, account_id, "", handle_to_be_published, &err))
                        EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
        }
 
@@ -1385,149 +1665,20 @@ static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibo
        return true;
 }
 
-static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, 
-                                                                                 int handle_to_be_published, int *error)
-{
-       EM_DEBUG_FUNC_BEGIN_SEC("account_id : [%d], mailbox_id : [%d], criteria : [%s]", account_id, mailbox_id, criteria);
-
-       int err = EMAIL_ERROR_NONE;
-       int i = 0;
-       int mail_id = 0;
-       int thread_id = 0;
-       char temp_uid_string[20] = {0,};
-
-       emcore_uid_list uid_elem;
-       emstorage_mailbox_tbl_t *search_mailbox = NULL;
-       emstorage_mail_tbl_t *new_mail_tbl_data = 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");
-               err = EMAIL_ERROR_INVALID_PARAM;
-               goto FINISH_OFF;
-       }
-
-       if ( (err = emstorage_get_mailbox_by_id(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 (!emcore_connect_to_remote_mailbox(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, mail_criteria(criteria), SE_FREE)) {
-               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;
-       }
-
-       for (i = 1; i <= stream->nmsgs; ++i) {
-               mail_cache_element = mail_elt(stream, i);
-               if (mail_cache_element->searched) {
-                       env = mail_fetchstructure_full(stream, i, NULL, FT_PEEK);
-
-                       memset(&uid_elem, 0x00, sizeof(uid_elem));
-
-                       uid_elem.msgno = mail_cache_element->msgno;
-                       SNPRINTF(temp_uid_string, 20, "%4lu", mail_cache_element->private.uid);
-                       uid_elem.uid = temp_uid_string;
-                       uid_elem.flag.seen = mail_cache_element->seen;
-
-                       if (!emcore_make_mail_tbl_data_from_envelope(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;
-                       }
-
-                       search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
-                       if (search_mailbox == NULL) {
-                               EM_DEBUG_EXCEPTION("em_malloc failed");
-                               err = EMAIL_ERROR_OUT_OF_MEMORY;
-                               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;
-                       }
-
-                       search_mailbox->account_id = account_id;
-                       search_mailbox->mailbox_id = mailbox_id;
-                       search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
-                       search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
-
-                       if ((err = emcore_add_mail_to_mailbox(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_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 (new_mail_tbl_data) {
-                               emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
-                               new_mail_tbl_data = NULL;
-                       }
-
-                       emstorage_free_mailbox(&search_mailbox, 1, NULL); /*prevent 17942*/
-               }
-       }
-
-       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)
-               mail_close (stream);
-
-       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);
-
-       if (error)
-               *error = err;
-
-       EM_DEBUG_FUNC_END();
-       return true;
-}
-
-static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published)
+static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, 
+                                                                                                                               int input_account_id, 
+                                                                                                                               int input_mailbox_id, 
+                                                                                                                               char *input_old_mailbox_path, 
+                                                                                                                               char *input_new_mailbox_path, 
+                                                                                                                               char *input_new_mailbox_alias, 
+                                                                                                                               int handle_to_be_published)
 {
        EM_DEBUG_FUNC_BEGIN_SEC("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_path [%s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, input_new_mailbox_alias, handle_to_be_published);
        int err = EMAIL_ERROR_NONE;
 
 
        if (err == EMAIL_ERROR_NONE) {
-               if ((err = emcore_rename_mailbox(input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, NULL, 0, true, true, handle_to_be_published)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_rename_mailbox(multi_user_name, input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, NULL, 0, true, true, handle_to_be_published)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_rename_mailbox failed [%d]", err);
                }
        }
@@ -1542,13 +1693,19 @@ static void* worker_send_event_queue(void *arg)
        int err = EMAIL_ERROR_NONE;
        email_event_t *event_data = NULL;
        email_event_t *started_event = NULL;
+       int pbd_thd_state = 0;
+       int event_que_state = 0;
 
-       if (!emstorage_open(&err)) {
+       if (!emstorage_open(NULL, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
                return NULL;
        }
 
        while (g_send_event_loop) {
+
+               pbd_thd_state = emcore_get_pbd_thd_state();
+               event_que_state = emcore_is_event_queue_empty();
+
                /* get a event_data from event_data send queue */
                ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
                if (!emcore_retrieve_send_event(&event_data, &err)) {
@@ -1560,7 +1717,19 @@ static void* worker_send_event_queue(void *arg)
 
                        send_thread_run = 0;
 
-                       emdevice_set_sleep_on_off(true, NULL);
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_SENDING_WORKER, true, NULL);
+                       
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                       int wifi_status = 0;
+                       if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
+                               EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
+                       }
+                       EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
+
+                       if (!pbd_thd_state && event_que_state && wifi_status > 1) {
+                               WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+                       }
+#endif
 
                        //go to sleep when queue is empty
                        SLEEP_CONDITION_VARIABLE(_send_event_available_signal, *_send_event_queue_lock);
@@ -1569,19 +1738,19 @@ static void* worker_send_event_queue(void *arg)
                } else {
                        LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
                        EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
-                       emdevice_set_sleep_on_off(false, NULL);
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_SENDING_WORKER, false, NULL);
                        send_thread_run = 1;
 
                        switch (event_data->type) {
 
                                case EMAIL_EVENT_SEND_MAIL:
-                                       if (!emcore_send_mail(event_data->event_param_data_4, &err))
+                                       if (!emcore_send_mail(event_data->multi_user_name, event_data->event_param_data_4, &err))
                                                EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
                                        break;
 
                                case EMAIL_EVENT_SEND_MAIL_SAVED:
                                        /* send mails to been saved in off-line mode */
-                                       if (!emcore_send_saved_mail(event_data->account_id, event_data->event_param_data_3, &err))
+                                       if (!emcore_send_saved_mail(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_3, &err))
                                                EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);
                                        break;
 
@@ -1606,14 +1775,15 @@ static void* worker_send_event_queue(void *arg)
                                                                EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
                                                                switch (local_activity[0].activity_type) {
                                                                        case ACTIVITY_SAVEMAIL:  {
-                                                                               if (!emcore_sync_mail_from_client_to_server(event_data->account_id, local_activity[0].mail_id, &err)) {
+                                                                               if (!emcore_sync_mail_from_client_to_server(event_data->multi_user_name, event_data->account_id, local_activity[0].mail_id, &err)) {
                                                                                        EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
                                                                                }
                                                                        }
                                                                        break;
 
                                                                        case ACTIVITY_DELETEMAIL_SEND:                          /* New Activity Type Added for Race Condition and Crash Fix */ {
-                                                                               if (!emcore_delete_mail(local_activity[0].account_id,
+                                                                               if (!emcore_delete_mail(local_activity[0].multi_user_name,
+                                                                                                                                local_activity[0].account_id,
                                                                                                                                &local_activity[0].mail_id,
                                                                                                                                EMAIL_DELETE_FOR_SEND_THREAD,
                                                                                                                                true,
@@ -1671,6 +1841,7 @@ static void* worker_send_event_queue(void *arg)
        if (!emstorage_close(&err))
                EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
 
+       emcore_close_smtp_stream_list();
        EM_DEBUG_FUNC_END("err [%d]", err);
        return NULL;
 }
@@ -1698,7 +1869,7 @@ INTERNAL_FUNC int emdaemon_start_thread_for_downloading_partial_body(int *err_co
        }
 
        g_partial_body_thd_next_event_idx = 0;
-       g_partial_body_thd_loop = 1;
+//     g_partial_body_thd_loop = 1;
        g_partial_body_thd_queue_empty = true;
        g_partial_body_thd_queue_full = false;
 
@@ -1730,135 +1901,172 @@ static gpointer partial_body_download_thread(gpointer data)
        int err = EMAIL_ERROR_NONE;
        email_session_t *session = NULL;
        email_event_partial_body_thd partial_body_thd_event;
+       int i, account_count = 0;
+       emstorage_account_tbl_t *account_list = NULL;
+       int event_que_state = 0;
+       int send_event_que_state = 0;
 
        EM_DEBUG_LOG_DEV(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());
 
        /* Open connection with DB */
 
-       if (false == emstorage_open(&err))  {
+       if (false == emstorage_open(NULL, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
                return false;
        }
 
-       /* Start the continuous loop */
+       /* refactoring session required */
+       if (!emcore_get_empty_session(&session)) {
+               EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
+       }
 
-       while (g_partial_body_thd_loop) {
-               /*  Get an empty session  */
-               /*  TODO :  Mutex should be used in session APIs */
+       while (1) {
 
-               if (false == emcore_get_empty_session(&session))
-                       EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
-               else {  /* Get and Event from the Partial Body thread Event Queue */
-                       memset(&partial_body_thd_event, 0x00, sizeof(email_event_partial_body_thd));
+               memset(&partial_body_thd_event, 0, sizeof(email_event_partial_body_thd));
 
-                       if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
-                               if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
-                                       EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
-                               else {
-                                       EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
+               if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
+                       if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
+                               EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
+                       else {
+                               EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
 
-                                       /*  Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count  */
-                                       if (!g_partial_body_bulk_dwd_queue_empty) {
-                                               partial_body_thd_event.event_type = 0;
-                                               partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
-                                               partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
-                                               partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name); /* need to be freed */
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                               int wifi_status = 0;
+#endif
 
-                                               if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
-                                                       EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
+                               /*  Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count  */
+                               if (!g_partial_body_bulk_dwd_queue_empty) {
+                                       partial_body_thd_event.event_type = 0;
+                                       partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
+                                       partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
+                                       partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name); /* need to be freed */
+                                       partial_body_thd_event.multi_user_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].multi_user_name); /* need to be freed */
 
-                                               emcore_pb_thd_set_local_activity_continue(true);
-                                       }
 
-                                       if (true == emcore_pb_thd_can_local_activity_continue()) {
-                                               /*Check for local Activities */
-                                               int is_local_activity_event_inserted = false;
+                                       if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
+                                               EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
 
-                                               if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &err)) {
-                                                       EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
-                                               }
-                                               else {
-                                                       if (true == is_local_activity_event_inserted) {
-                                                               emcore_pb_thd_set_local_activity_continue(false);
+                                       emcore_pb_thd_set_local_activity_continue(true);
+                               }
 
-                                                               emcore_clear_session(session);
+                               if (true == emcore_pb_thd_can_local_activity_continue()) {
+                                       /*Check for local Activities */
+                                       int is_local_activity_event_inserted = false;
 
-                                                               if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
-                                                                       EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
+                                       if (false == emcore_partial_body_thd_local_activity_sync(partial_body_thd_event.multi_user_name, &is_local_activity_event_inserted, &err)) {
+                                               EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
+                                       }
+                                       else {
+                                               if (true == is_local_activity_event_inserted) {
+                                                       emcore_pb_thd_set_local_activity_continue(false);
 
-                                                               continue;
-                                                       }
+/*                                                     emcore_clear_session(session);*/
+
+                                                       if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
+                                                               EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
+
+                                                       continue;
                                                }
                                        }
+                               }
 
-                                       EM_DEBUG_LOG_DEV(" Partial Body Thread is going to sleep");
+                               EM_DEBUG_LOG_DEV(" Partial Body Thread is going to sleep");
+                               
+                               /* finalize sync */
+                               account_count = 0;
+                               account_list = NULL;
+                               if (!emstorage_get_account_list(NULL, &account_count, &account_list, false, false, &err)) {
+                                       EM_DEBUG_EXCEPTION("emstorage_get_account_list failed : [%d]", err);
+                               }
 
-                                       emcore_set_pbd_thd_state(false);
-                                       /*check: refactoring required*/
-                                       ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
-                                       SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
-                                       LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
+                               for (i = 0; i < account_count; i++) {
+                                       if (!emdaemon_finalize_sync(partial_body_thd_event.multi_user_name, account_list[i].account_id, 0, 0, 0, 0, true, NULL))
+                                               EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
+                               }
 
-                                       EM_DEBUG_LOG(" Partial Body Thread wakes up ");
+                               if (account_list)
+                                       emstorage_free_account(&account_list, account_count, NULL);
+       
+                               emcore_set_pbd_thd_state(false);
 
-                                       emcore_set_pbd_thd_state(true);
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                               if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
                                }
 
+                               EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
+
+                               event_que_state = emcore_is_event_queue_empty();
+                               send_event_que_state = emcore_is_send_event_queue_empty();
+#endif
+                               /*check: refactoring required*/
+                               ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+                               if (event_que_state && send_event_que_state && wifi_status > 1) {
+                                       WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+                               }
+#endif
+                               SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
+                               LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
+
+                               EM_DEBUG_LOG(" Partial Body Thread wakes up ");
+
+                               emcore_set_pbd_thd_state(true);
                        }
-                       else {
-                               EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
 
-                               /* Since all events are network operations dnet init and sleep control is
-                               done before entering switch block*/
+               }
+               else {
+                       EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
 
-                               emdevice_set_sleep_on_off(false, NULL);
+                       /* Since all events are network operations dnet init and sleep control is
+                       done before entering switch block*/
 
-                               if (!emnetwork_check_network_status( &err))  {
-                                       EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
-                               }
-                               else {
-                                       /*  Process events  */
-                                       EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_PARTIAL_BODY_WORKER, false, NULL);
 
-                                       switch (partial_body_thd_event.event_type) {
-                                               case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD:  {
-                                                       if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
-                                                               EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
-                                                       }
-                                                       break;
-                                               }
-                                               case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD:  {
-                                                       partial_body_thd_event.event_type = 0;
+                       if (!emnetwork_check_network_status( &err))  {
+                               EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
+                       }
+                       else {
+                               /*  Process events  */
+                               EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
 
-                                                       /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
-                                                       *   g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
-                                                       if (false == emcore_is_partial_body_thd_que_empty())
-                                                               break;
-                                                       if (!g_partial_body_bulk_dwd_queue_empty)
-                                                               break;
+                               switch (partial_body_thd_event.event_type) {
+                                       case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD:  {
+                                               if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
+                                                       EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
+                                               }
+                                               break;
+                                       }
+                                       case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD:  {
+                                               partial_body_thd_event.event_type = 0;
 
-                                                       if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
-                                                               EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
+                                               /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
+                                               *   g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
+                                               if (false == emcore_is_partial_body_thd_que_empty())
                                                        break;
-                                               }
-                                               default:
-                                                       EM_DEBUG_EXCEPTION(" Warning :  Default case entered. This should not happen ");
+                                               if (!g_partial_body_bulk_dwd_queue_empty)
                                                        break;
+
+                                               if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
+                                                       EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
+                                               break;
                                        }
+                                       default:
+                                               EM_DEBUG_EXCEPTION(" Warning :  Default case entered. This should not happen ");
+                                               break;
                                }
-
-                               if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
-                                       EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
-
-                               emdevice_set_sleep_on_off(true, NULL);
                        }
 
-                       emcore_clear_session(session);
+                       if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, NULL))
+                               EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed");
+
+                       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_PARTIAL_BODY_WORKER, true, NULL);
                }
+
+/*             emcore_clear_session(session); */
        }
 
-       /* If something is added to end thread in future for any case then if thread is holding any resources
-       define a function emcore_partial_body_thd_loop_stop to release resources and call it
-       here to end thread */
+       emcore_close_recv_stream_list();
+       emcore_clear_session(session);
        return SUCCESS;
 }
index c54aa02..08be8d9 100755 (executable)
@@ -4,7 +4,7 @@
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
-* 
+*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -39,6 +39,7 @@
 #include <vconf-internal-account-keys.h>
 #include <dbus/dbus.h>
 #include <dlfcn.h>                             /* added for Disabling the Pthread flag log */
+#include <net_connection.h>
 
 #include "email-daemon.h"
 #include "email-storage.h"
 #include "email-daemon-event.h"
 #include "email-core-utils.h"
 #include "email-core-mail.h"
-#include "email-core-event.h" 
+#include "email-core-event.h"
 #include "email-core-account.h"
 #include "email-core-mailbox.h"
 #include "email-core-smtp.h"
 #include "email-core-global.h"
+#include "email-core-imap-idle.h"
 #include "email-storage.h"
-#include "email-core-sound.h" 
 #include "email-core-task-manager.h"
 #include "email-core-alarm.h"
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+#include "email-core-auto-download.h"
+#endif
 #include "email-daemon-emn.h"
 #include "email-network.h"
 #include "email-device.h"
 #include "c-client.h"
+#include "email-core-smime.h"
+#include "email-core-container.h"
+//#include "email-core-cynara.h"
+
+connection_h conn = NULL;
 
 extern void *
 pop3_parameters(long function, void *value);
@@ -72,6 +81,10 @@ INTERNAL_FUNC int g_client_count = 0;
 extern int blocking_mode_of_setting;
 extern GList *alarm_data_list;
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+extern pthread_cond_t  _auto_downalod_available_signal;
+#endif
+
 static int default_alarm_callback(int input_timer_id, void *user_parameter);
 
 /*  static functions */
@@ -85,6 +98,10 @@ static int _emdaemon_load_email_core()
        if (!emdaemon_core_init(&err))
                goto FINISH_OFF;
 
+#ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
+       emdaemon_setup_handler_for_open_ssl_multithread();
+#endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
+
        if (emdaemon_start_event_loop(&err) < 0)
                goto FINISH_OFF;
 
@@ -111,8 +128,14 @@ static int _emdaemon_load_email_core()
        emdaemon_init_blocking_mode_status();
 #endif /* __FEATURE_BLOCKING_MODE__ */
 
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       if (!emcore_start_auto_download_loop(&err))
+               goto FINISH_OFF;
+#endif
+
 FINISH_OFF:
 
+       EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
 
@@ -125,6 +148,20 @@ static int _emdaemon_unload_email_core()
        emcore_stop_event_loop(&err);
        emcore_stop_task_manager_loop();
 
+    /* Destroy container for daemon */
+    emcore_destroy_container();
+
+       /* Finish cynara */
+       //emcore_finish_cynara();
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       emcore_stop_auto_download_loop(&err);
+#endif
+
+#ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
+       emdaemon_cleanup_handler_for_open_ssl_multithread();
+#endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
@@ -140,7 +177,7 @@ static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node,
        email_account_t         *account_list = NULL;
        emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
 
-       if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
+       if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -150,12 +187,12 @@ static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node,
 
        for(i = 0; i < account_count; i++) {
                if(sync_start_toggle == 1) {
-                       if(!emstorage_get_mailbox_by_mailbox_type(account_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl_data, true, &err)) {
+                       if(!emstorage_get_mailbox_by_mailbox_type(NULL, account_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl_data, true, &err)) {
                                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for [%d] failed [%d]", account_list[i].account_id, err);
                                continue;
                        }
 
-                       if(!emdaemon_sync_header(account_list[i].account_id, mailbox_tbl_data->mailbox_id, &handle, &err)) {
+                       if(!emdaemon_sync_header(NULL, account_list[i].account_id, mailbox_tbl_data->mailbox_id, &handle, &err)) {
                                EM_DEBUG_EXCEPTION("emdaemon_sync_header for [%d] failed [%d]", account_list[i].account_id, err);
                        }
 
@@ -163,7 +200,7 @@ static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node,
                        mailbox_tbl_data = NULL;
                }
                else {
-                       emcore_cancel_all_threads_of_an_account(account_list[i].account_id);
+                       emcore_cancel_all_threads_of_an_account(NULL, account_list[i].account_id);
                }
        }
 
@@ -186,7 +223,7 @@ static void callback_for_AUTO_SYNC_STATUS_from_account_svc(keynode_t *input_node
        email_account_t *account_list = NULL;
        email_account_t *account_info = NULL;
 
-       if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
+       if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -208,7 +245,7 @@ static void callback_for_AUTO_SYNC_STATUS_from_account_svc(keynode_t *input_node
                                account_info->check_interval = ~account_info->check_interval + 1;
                }
 
-               if(!emdaemon_update_account(account_info->account_id, account_info, &err)) {
+               if(!emdaemon_update_account(NULL, account_info->account_id, account_info, &err)) {
                        EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -221,56 +258,88 @@ FINISH_OFF:
        EM_DEBUG_FUNC_END();
 }
 
-static void callback_for_NETWORK_STATUS(keynode_t *input_node, void *input_user_data)
+static void callback_for_NETWORK_STATUS(connection_type_e new_conn_type, void *input_user_data)
 {
-       EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
+       EM_DEBUG_FUNC_BEGIN("new_conn_type [%p], input_user_data [%p]", new_conn_type, input_user_data);
        int err = EMAIL_ERROR_NONE;
        int i = 0;
        int total = 0;
        int unseen = 0;
-       int network_status = 0;
        int account_count = 0;
+       int conn_type = 0;
+       int handle = 0;
+       int send_saved_handle = 0;
+       char *multi_user_name = NULL;
+       email_event_t *event_data = NULL;
        email_account_t *account_list = NULL;
        email_account_t *account_info = NULL;
        emstorage_mailbox_tbl_t *local_mailbox = NULL;
 
-       if (input_node)
-               network_status = vconf_keynode_get_int(input_node);
+       conn_type = emnetwork_get_network_status();
 
-       if (network_status == VCONFKEY_NETWORK_OFF) {
-               EM_DEBUG_EXCEPTION("Network is OFF");
-               goto FINISH_OFF;
+       switch (new_conn_type) {
+               case CONNECTION_TYPE_WIFI:
+               case CONNECTION_TYPE_CELLULAR:
+                       if(conn_type != new_conn_type) {
+                               EM_DEBUG_LOG("Network type changed from [%d] to [%d]", conn_type, new_conn_type);
+                               emnetwork_set_network_status(new_conn_type);
+                       }
+                       break;
+
+               case CONNECTION_TYPE_DISCONNECTED:
+                       EM_DEBUG_LOG("Network is disconnected. [%d]", new_conn_type);
+                       emnetwork_set_network_status(new_conn_type);
+                       goto FINISH_OFF;
+
+               default:
+                       EM_DEBUG_LOG("Unexpected value came. Set disconnectted network. [%d]", new_conn_type);
+                       emnetwork_set_network_status(CONNECTION_TYPE_DISCONNECTED);
+                       goto FINISH_OFF;
        }
 
-       if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
+       emcore_get_sync_fail_event_data(&event_data);
+
+       if (event_data != NULL) {
+               if (!emcore_insert_event(event_data, &handle, &err)) {
+                       EM_DEBUG_LOG("emcore_insert_event failed : %d", err);
+               }
+       }
+
+       if (false == emcore_is_partial_body_thd_que_empty())
+               emcore_pb_thd_set_local_activity_continue(true);
+
+       if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
                EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
        for (i = 0; i < account_count ; i++) {
                account_info = account_list + i;
+               /* check if inbox folder sync is finished */
+               if (!emstorage_get_mailbox_by_mailbox_type (multi_user_name, account_info->account_id, EMAIL_MAILBOX_TYPE_INBOX, &local_mailbox, false, &err)) {
+                       if (err == EMAIL_ERROR_MAILBOX_NOT_FOUND) {
+                               int handle = 0; 
+                               emdaemon_get_imap_mailbox_list(multi_user_name, account_info->account_id, "", &handle, &err);
+                               if (err != EMAIL_ERROR_NONE) {
+                                       EM_DEBUG_EXCEPTION("emdaemon_get_imap_mailbox_list error [%d]", err);
+                               }
+                       }
+               }
 
-               if (!emstorage_get_mailbox_by_mailbox_type(account_info->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
-                       goto FINISH_OFF;
+               if (!emstorage_free_mailbox(&local_mailbox, 1, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_free_mailbox error [%d]", err);
                }
 
-               if (!emstorage_get_mail_count(account_info->account_id, local_mailbox->mailbox_id, &total, &unseen, false, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", err);
-                       goto FINISH_OFF;
+               /* send mails in outbox */
+               if (!emcore_get_mail_count_by_query(NULL, account_info->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, 0, &total, &unseen, &err)) {
+                       EM_DEBUG_EXCEPTION("emstorage_get_mail_count_by_query failed [%d]", err);
                }
 
                if (total <= 0)
                        continue;
 
-               if (!emcore_send_saved_mail(account_info->account_id, local_mailbox->mailbox_name, &err)) {
-                       EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed [%d]", err);
-                       goto FINISH_OFF;
-               }
-
-               if (!emstorage_free_mailbox(&local_mailbox, 1, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_free_mailbox failed [%d]", err);
-                       goto FINISH_OFF;
+               if (!emdaemon_send_mail_saved(multi_user_name, account_info->account_id, &send_saved_handle, &err)) {
+                       EM_DEBUG_EXCEPTION("emdaemon_send_mail_saved failed : [%d]", err);
                }
        }
 
@@ -295,7 +364,7 @@ INTERNAL_FUNC void callback_for_BLOCKING_MODE_STATUS(keynode_t *input_node, void
                EM_DEBUG_EXCEPTION("Invalid param");
                return;
        }
-       
+
        blocking_mode_of_setting = vconf_keynode_get_bool(input_node);
 
        emcore_set_blocking_mode_of_setting(blocking_mode_of_setting);
@@ -304,6 +373,7 @@ INTERNAL_FUNC void callback_for_BLOCKING_MODE_STATUS(keynode_t *input_node, void
 }
 #endif /* __FEATURE_BLOCKING_MODE__ */
 
+#ifdef __FEATURE_OMA_EMN__
 static void callback_for_VCONFKEY_MSG_SERVER_READY(keynode_t *input_node, void *input_user_data)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -324,6 +394,7 @@ static void callback_for_VCONFKEY_MSG_SERVER_READY(keynode_t *input_node, void *
 
        return;
 }
+#endif /* __FEATURE_OMA_EMN__ */
 
 static void callback_for_VCONFKEY_GLOBAL_BADGE_STATUS(keynode_t *input_node, void *input_user_data)
 {
@@ -340,8 +411,7 @@ static void callback_for_VCONFKEY_GLOBAL_BADGE_STATUS(keynode_t *input_node, voi
        noti_status = vconf_keynode_get_bool(input_node);
 
        if (noti_status) {
-               if (!emcore_display_unread_in_badge()) 
-                       EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
+               emcore_display_unread_in_badge(NULL);
        } else {
                if (vconf_get_bool(VCONF_VIP_NOTI_BADGE_TICKER, &badge_ticker) != 0) {
                        EM_DEBUG_EXCEPTION("vconf_get_bool failed");
@@ -350,13 +420,12 @@ static void callback_for_VCONFKEY_GLOBAL_BADGE_STATUS(keynode_t *input_node, voi
                }
                /* if priority sender is on, show the priority sender unread */
                if (badge_ticker) {
-                       if (!emcore_display_unread_in_badge()) 
-                               EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
+                       emcore_display_unread_in_badge(NULL);
                        goto FINISH_OFF;
                }
 
                /* reset badge */
-               if ((err = emcore_display_badge_count(0)) != EMAIL_ERROR_NONE) 
+               if ((err = emcore_display_badge_count(0)) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
        }
 
@@ -380,8 +449,7 @@ static void callback_for_VCONFKEY_PRIORITY_BADGE_STATUS(keynode_t *input_node, v
        noti_status = vconf_keynode_get_bool(input_node);
 
        if (noti_status) {
-               if (!emcore_display_unread_in_badge()) 
-                       EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
+               emcore_display_unread_in_badge(NULL);
        } else {
 
                if (vconf_get_bool(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, &badge_ticker) != 0) {
@@ -392,12 +460,11 @@ static void callback_for_VCONFKEY_PRIORITY_BADGE_STATUS(keynode_t *input_node, v
 
                /*if global badge is on, show the global unread*/
                if (badge_ticker) {
-                       if (!emcore_display_unread_in_badge()) 
-                               EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
+                       emcore_display_unread_in_badge(NULL);
                        goto FINISH_OFF;
                }
                /* if all badges are off, reset badge count */
-               if ((err = emcore_display_badge_count(0)) != EMAIL_ERROR_NONE) 
+               if ((err = emcore_display_badge_count(0)) != EMAIL_ERROR_NONE)
                        EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
        }
 
@@ -432,63 +499,79 @@ static void callback_for_VCONFKEY_TELEPHONY_ZONE_TYPE(keynode_t *input_node, voi
        EM_DEBUG_FUNC_END();
 }
 
-INTERNAL_FUNC int emdaemon_initialize(int* err_code)
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+static void callback_for_VCONFKEY_WIFI_STATE(keynode_t *input_node, void *input_user_data)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+       int wifi_status = 0;
+
+       if (!input_node) {
+               EM_DEBUG_EXCEPTION("Invalid param");
+               return;
+       }
+
+       wifi_status = vconf_keynode_get_int(input_node);
+
+       EM_DEBUG_LOG("wifi_status [%d]", wifi_status);
+
+       if (wifi_status > 1) {
+               if (!emcore_get_pbd_thd_state() &&
+                               emcore_is_event_queue_empty() &&
+                               emcore_is_send_event_queue_empty()) {
+                       WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
+               }
+       }
+
+       EM_DEBUG_FUNC_END();
+}
+#endif
+
+INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code)
+{
+       EM_DEBUG_FUNC_BEGIN();
+
        /*  default variable */
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
+       connection_type_e net_state = CONNECTION_TYPE_DISCONNECTED;
+
        if (g_client_count > 0)  {
                EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
                g_client_count++;
-               return true;
        }
-       else 
+       else {
                EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
 
-       dbus_threads_init_default();
-
-       g_type_init();
+        dbus_threads_init_default();
 
-       emstorage_shm_file_init(SHM_FILE_FOR_DB_LOCK);
+        g_client_count = 0;    
+        
+        if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
+            EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
+            goto FINISH_OFF;
+        }
 
-#ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
-       emstorage_shm_file_init(SHM_FILE_FOR_MAIL_ID_LOCK);
-#endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
+#ifdef __FEATURE_OMA_EMN__
+        if(emdaemon_initialize_emn() != EMAIL_ERROR_NONE) {
+            vconf_notify_key_changed(VCONFKEY_MSG_SERVER_READY, callback_for_VCONFKEY_MSG_SERVER_READY, NULL);
+        }
+#endif
+    }
 
-       /* open database */
-       if (!emstorage_open(&err))  {
+    /* open database */
+       if (!emstorage_open(multi_user_name, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_clean_save_status(EMAIL_MAIL_STATUS_SAVED, &err))
-               EM_DEBUG_EXCEPTION("emstorage_check_mail_status Failed [%d]", err );
-       
-       g_client_count = 0;    
-       
-       if (!emdaemon_initialize_account_reference())  {
-               EM_DEBUG_EXCEPTION("emdaemon_initialize_account_reference fail...");
-               err = EMAIL_ERROR_DB_FAILURE;
-               goto FINISH_OFF;
-       }
-       EM_DEBUG_LOG("emdaemon_initialize_account_reference over - g_client_count [%d]", g_client_count);
-       
-       if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
-               EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
-               goto FINISH_OFF;
-       }
+       if ((err = emstorage_update_db_table_schema(multi_user_name)) != EMAIL_ERROR_NONE) 
+               EM_DEBUG_EXCEPTION("emstorage_update_db_table_schema failed [%d]", err);
 
-#ifdef __FEATURE_OMA_EMN__
-       if(emdaemon_initialize_emn() != EMAIL_ERROR_NONE) {
-               vconf_notify_key_changed(VCONFKEY_MSG_SERVER_READY, callback_for_VCONFKEY_MSG_SERVER_READY, NULL);
-       }
-#endif
+       if (!emstorage_clean_save_status(multi_user_name, EMAIL_MAIL_STATUS_SAVED, &err))
+               EM_DEBUG_EXCEPTION("emstorage_check_mail_status Failed [%d]", err );
 
 #ifdef __FEATURE_AUTO_RETRY_SEND__
-       if ((err = emcore_create_alarm_for_auto_resend (AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_create_alarm_for_auto_resend (multi_user_name, AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
                if (err == EMAIL_ERROR_MAIL_NOT_FOUND)
                        EM_DEBUG_LOG ("no mail found");
                else
@@ -496,29 +579,67 @@ INTERNAL_FUNC int emdaemon_initialize(int* err_code)
        }
 #endif /* __FEATURE_AUTO_RETRY_SEND__ */
 
+
+       /* Start auto polling */
+#ifdef __FEATURE_AUTO_POLLING__
+       emdaemon_start_auto_polling(&err);
+#endif
+
+#ifdef __FEATURE_IMAP_IDLE__
+       emcore_create_imap_idle_thread();
+#endif /* __FEATURE_IMAP_IDLE__ */
+
        /* Subscribe Events */
+#ifdef __FEATURE_SYNC_STATUS__
+       vconf_notify_key_changed(VCONFKEY_ACCOUNT_SYNC_ALL_STATUS_INT,  callback_for_SYNC_ALL_STATUS_from_account_svc,  NULL);
+       vconf_notify_key_changed(VCONFKEY_ACCOUNT_AUTO_SYNC_STATUS_INT, callback_for_AUTO_SYNC_STATUS_from_account_svc, NULL);
+#endif /* __FEATURE_SYNC_STATUS__  */
+
+       int error_from_connection = 0;
+
+       if ((error_from_connection = connection_create(&conn)) == CONNECTION_ERROR_NONE) {
+               if (connection_get_type(conn, &net_state) != CONNECTION_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("connection_get_type failed");
+               } else {
+                       emnetwork_set_network_status(net_state);
+               }
+
+               if (connection_set_type_changed_cb(conn, callback_for_NETWORK_STATUS, NULL) != CONNECTION_ERROR_NONE)
+                       EM_DEBUG_EXCEPTION("connection_set_type_changed_cb failed");
+       }
+       else {
+               EM_DEBUG_EXCEPTION("connection_create failed[%d]", error_from_connection);
+       }
 
-       vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS, callback_for_NETWORK_STATUS, NULL);
 #ifdef __FEATURE_BLOCKING_MODE__
        vconf_notify_key_changed(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, callback_for_BLOCKING_MODE_STATUS, NULL);
 #endif
        vconf_notify_key_changed(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, callback_for_VCONFKEY_GLOBAL_BADGE_STATUS, NULL);
 
        vconf_notify_key_changed(VCONF_VIP_NOTI_BADGE_TICKER, callback_for_VCONFKEY_PRIORITY_BADGE_STATUS, NULL);
+
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, callback_for_VCONFKEY_GLOBAL_BADGE_STATUS, NULL);
+
+       vconf_notify_key_changed(VCONF_VIP_NOTI_NOTIFICATION_TICKER, callback_for_VCONFKEY_PRIORITY_BADGE_STATUS, NULL);
+
        /* VCONFKEY_TELEPHONY_SVC_ROAM */
-       /*vconf_notify_key_changed(VCONFKEY_TELEPHONY_ZONE_TYPE, callback_for_VCONFKEY_TELEPHONY_ZONE_TYPE, NULL);*/
-       
-       emcore_display_unread_in_badge();
-       
+       vconf_notify_key_changed(VCONFKEY_TELEPHONY_ZONE_TYPE, callback_for_VCONFKEY_TELEPHONY_ZONE_TYPE, NULL);
+
+#ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
+       vconf_notify_key_changed(VCONFKEY_WIFI_STATE, callback_for_VCONFKEY_WIFI_STATE, NULL);
+#endif
+
+       emcore_display_unread_in_badge(NULL);
+
        ret = true;
-       
+
 FINISH_OFF:
        if (ret == true)
                g_client_count = 1;
 
        if (err_code)
                *err_code = err;
-       
+
        EM_DEBUG_FUNC_END("ret [%d], g_client_count [%d]", ret, g_client_count);
        return ret;
 }
@@ -526,40 +647,33 @@ FINISH_OFF:
 INTERNAL_FUNC int emdaemon_finalize(int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        /*  default variable */
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       
-       if (g_client_count > 1) {
-               EM_DEBUG_EXCEPTION("engine is still used by application. decreased counter=[%d]", g_client_count);
-               g_client_count--;
-               err = EMAIL_ERROR_CLOSE_FAILURE;
-               goto FINISH_OFF;
-       }
-       
+
        if ( (err = _emdaemon_unload_email_core()) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_emdaemon_unload_email_core failed [%d]", err);
                goto FINISH_OFF;
        }
-       
-       /* free account reference list */
-       emcore_free_account_reference();
-       
+
+       connection_destroy(conn);
+
        /* close database */
        if (!emstorage_close(&err)) {
                EM_DEBUG_EXCEPTION("emstorage_close failed [%d]", err);
                goto FINISH_OFF;
        }
-       
-       g_client_count = 0;
+
+        /* Openssl clean up */
+        emcore_clean_openssl_library();
 
        ret = true;
-       
+
 FINISH_OFF:
        if (err_code)
                *err_code = err;
-       
+
        return ret;
 }
 
@@ -567,34 +681,35 @@ FINISH_OFF:
 INTERNAL_FUNC int emdaemon_start_auto_polling(int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
-       
+
        /*  default variable */
        int ret = false, count = 0, i= 0;
        int err = EMAIL_ERROR_NONE;
+    char *multi_user_name = NULL;
        emstorage_account_tbl_t* account_list = NULL;
 
        /* get account list */
-       if (!emstorage_get_account_list(&count, &account_list, false, false, &err))  {
+       if (!emstorage_get_account_list(multi_user_name, &count, &account_list, false, false, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);              
                goto FINISH_OFF;
        }
 
        for (i = 0; i < count; i++)  {
                /* start auto polling, if check_interval not zero */
-               if(account_list[i].check_interval > 0 || (account_list[i].peak_days && account_list[i].peak_interval > 0)) {
-                       if(!emdaemon_add_polling_alarm( account_list[i].account_id))
+               if(account_list[i].check_interval > 0 || ((account_list[i].peak_days > 0) && account_list[i].peak_interval > 0)) {
+                       if(!emdaemon_add_polling_alarm(multi_user_name, account_list[i].account_id))
                                EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
                }
        }
 
        ret = true;
-FINISH_OFF:  
+FINISH_OFF:
        if (account_list)
                emstorage_free_account(&account_list, count, NULL);
-       
+
        if (err_code != NULL)
                *err_code = err;
-       
+
        EM_DEBUG_FUNC_END("ret[%d]", ret);
        return ret;
 }
@@ -626,41 +741,44 @@ INTERNAL_FUNC int emdaemon_core_init(int *err_code)
        mail_parameters(NIL, SET_DISABLEPLAINTEXT, (void *) 2);
 
        /* Set max trials for login */
-       imap_parameters(SET_MAXLOGINTRIALS, (void *)3);
-       pop3_parameters(SET_MAXLOGINTRIALS, (void *)3);
-       smtp_parameters(SET_MAXLOGINTRIALS, (void *)3);
+       imap_parameters(SET_MAXLOGINTRIALS, (void *)2); /* 3-> 2*/
+       pop3_parameters(SET_MAXLOGINTRIALS, (void *)2); /* 3-> 2*/
+       smtp_parameters(SET_MAXLOGINTRIALS, (void *)1); /* 3-> 2*/
 
        mail_parameters(NIL, SET_SSLCERTIFICATEQUERY, (void *)emnetwork_callback_ssl_cert_query);
        mail_parameters(NIL, SET_SSLCAPATH, (void *)SSL_CERT_DIRECTORY);
 
        /* Set time out in second */
        mail_parameters(NIL, SET_OPENTIMEOUT  , (void *)50);
-       mail_parameters(NIL, SET_READTIMEOUT  , (void *)180);
-       mail_parameters(NIL, SET_WRITETIMEOUT , (void *)180);
-       mail_parameters(NIL, SET_CLOSETIMEOUT , (void *)30);
+       mail_parameters(NIL, SET_READTIMEOUT  , (void *)60); /* 180 -> 15 */
+       mail_parameters(NIL, SET_WRITETIMEOUT , (void *)20); /* 180 -> 15 */
+       mail_parameters(NIL, SET_CLOSETIMEOUT , (void *)20); /* 30 -> 15 */
 
        emdaemon_init_alarm_data_list();
 
+        /* Openssl library init */
+        emcore_init_openssl_library();
+
        if (err_code)
                *err_code = EMAIL_ERROR_NONE;
 
        return true;
 }
 
-#ifdef __FEATURE_BLOCKING_MODE__
 INTERNAL_FUNC bool emdaemon_init_blocking_mode_status()
 {
        EM_DEBUG_FUNC_BEGIN("blocking_mode_of_setting : [%d]", blocking_mode_of_setting);
 
-       /*if (vconf_get_bool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &blocking_mode_of_setting) != 0) {
+#ifdef __FEATURE_BLOCKING_MODE__
+       if (vconf_get_bool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &blocking_mode_of_setting) != 0) {
                EM_DEBUG_EXCEPTION("vconf_get_bool failed");
                return false;
-       }*/
+       }
+#endif /* __FEATURE_BLOCKING_MODE__ */
 
        EM_DEBUG_FUNC_END();
-       return false;
+       return true;
 }
-#endif
 
 INTERNAL_FUNC int emdaemon_init_alarm_data_list()
 {
@@ -695,27 +813,30 @@ static int default_alarm_callback(int input_timer_id, void *user_parameter)
 
        EM_DEBUG_ALARM_LOG("default_alarm_callback input_timer_id[%d]", input_timer_id);
 
-       emdevice_set_sleep_on_off(false, NULL);
+       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_ALARM_CALLBACK, false, NULL);
 
        if ((err = emcore_get_alarm_data_by_alarm_id(input_timer_id, &alarm_data)) != EMAIL_ERROR_NONE || alarm_data == NULL) {
                EM_DEBUG_EXCEPTION("emcore_get_alarm_data_by_alarm_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ((err = alarm_data->alarm_callback(input_timer_id, user_parameter)) != EMAIL_ERROR_NONE) {
+       emcore_delete_alram_data_from_alarm_data_list(alarm_data);
+
+       if ((err = alarm_data->alarm_callback(alarm_data, user_parameter)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("alarm_callback failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       emcore_delete_alram_data_from_alarm_data_list(alarm_data);
-       EM_SAFE_FREE(alarm_data);
-
 FINISH_OFF:
 
-       if(err != EMAIL_ERROR_NONE) {
-               emdevice_set_sleep_on_off(true, NULL);
+       if (alarm_data) {
+               EM_SAFE_FREE(alarm_data->user_data);
+               EM_SAFE_FREE(alarm_data);
        }
 
+       emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_ALARM_CALLBACK, true, NULL);
+
+
        EM_DEBUG_FUNC_END("err [%d]", err);
        return err;
 }
index f422ac5..51b9361 100755 (executable)
@@ -59,9 +59,9 @@
 extern int g_local_activity_run;
 extern int g_save_local_activity_run;
 #endif
-static int _emdaemon_check_mail_id(int mail_id, int* err_code);
+static int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code);
 
-INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p], err_code[%p]", mail_id, handle, err_code);
 
@@ -79,7 +79,7 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
+       if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err)) {
                EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
                goto FINISH_OFF;
        }
@@ -96,7 +96,7 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
 
        account_id = mail_table_data->account_id;
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
@@ -105,14 +105,14 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
        }
 
 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
-       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
+       if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, 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 ) {
                        /*  mail is moved to 'OUTBOX' first of all. */
-               if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
+               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);
                        goto FINISH_OFF;
                }
@@ -123,10 +123,8 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
                EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
 
        /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
-
-       if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
+       if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
-
                goto FINISH_OFF;
        }
 
@@ -135,6 +133,7 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
        event_data->account_id         = account_id;
        event_data->event_param_data_4 = mail_id;
        event_data->event_param_data_5 = mail_table_data->mailbox_id;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event_for_sending_mails(event_data, &result_handle, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
@@ -155,7 +154,7 @@ FINISH_OFF:
        if (ret == false) {
                EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
 
-               if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
+               if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
                        EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
 
                if (event_data) {
@@ -185,7 +184,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_send_mail_saved(char *multi_user_name, int account_id, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
 
@@ -201,7 +200,7 @@ INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
@@ -209,7 +208,7 @@ INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mailbox_name_by_mailbox_type(account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
+       if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -218,9 +217,10 @@ INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err
        event_data->type               = EMAIL_EVENT_SEND_MAIL_SAVED;
        event_data->account_id         = account_id;
        event_data->event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
-       if (!emcore_insert_event(event_data, (int*)handle, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
+       if (!emcore_insert_event_for_sending_mails(event_data, (int *)handle, &err)) {
+               EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
                goto FINISH_OFF;
        }
 
@@ -247,7 +247,12 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
+INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name, 
+                                        email_mail_data_t *input_mail_data, 
+                                        email_attachment_data_t *input_attachment_data_list, 
+                                        int input_attachment_count, 
+                                        email_meeting_request_t *input_meeting_request, 
+                                        int input_from_eas)
 {
        EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
 
@@ -264,14 +269,14 @@ INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_at
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(input_mail_data->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id);
        if (!ref_account)  {
                EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if ((err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false)) != EMAIL_ERROR_NONE) {
+       if ((err = emcore_add_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -282,6 +287,7 @@ INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_at
                event_data->type               = EMAIL_EVENT_SAVE_MAIL;
                event_data->account_id         = input_mail_data->account_id;
                event_data->event_param_data_4 = input_mail_data->mail_id;
+               event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
                if (!emcore_insert_event(event_data, &handle, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
@@ -310,7 +316,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
+INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
 
@@ -326,7 +332,7 @@ INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox
                goto FINISH_OFF;
        }
 
-       if (!emcore_add_meeting_request(account_id, input_mailbox_id, meeting_req, &err))  {
+       if (!emcore_add_meeting_request(multi_user_name, account_id, input_mailbox_id, meeting_req, &err))  {
                EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -340,7 +346,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_download_body(char *multi_user_name, int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], verbose[%d], with_attachment[%d], handle[%p], err_code[%p]", account_id, mail_id, verbose, with_attachment,  handle, err_code);
 
@@ -348,7 +354,7 @@ INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbos
        int err = EMAIL_ERROR_NONE;
        email_event_t *event_data = NULL;
 
-       if (!_emdaemon_check_mail_id(mail_id, &err)) {
+       if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
                EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -358,6 +364,7 @@ INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbos
        event_data->account_id = account_id;
        event_data->event_param_data_4 = mail_id;
        event_data->event_param_data_5 = (verbose << 1 | with_attachment);
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err)) {
                EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
@@ -386,7 +393,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachment, int* err_code)
+int emdaemon_get_attachment(char *multi_user_name, int attachment_id, email_attachment_data_t** attachment, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
 
@@ -400,7 +407,7 @@ int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachm
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_attachment_info(attachment_id, attachment, &err) || !attachment)  {
+       if (!emcore_get_attachment_info(multi_user_name, attachment_id, attachment, &err) || !attachment)  {
                EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -417,7 +424,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, int* err_code)
+int emdaemon_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t* attachment, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
 
@@ -431,12 +438,12 @@ int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, in
                goto FINISH_OFF;
        }
 
-       if (!_emdaemon_check_mail_id(mail_id, &err))  {
+       if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
                EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emcore_add_attachment(mail_id, attachment, &err))  {
+       if (!emcore_add_attachment(multi_user_name, mail_id, attachment, &err))  {
                EM_DEBUG_EXCEPTION(" emcore_add_attachment failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -453,7 +460,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emdaemon_delete_mail_attachment(int attachment_id, int* err_code)
+int emdaemon_delete_mail_attachment(char *multi_user_name, int attachment_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
 
@@ -467,7 +474,7 @@ int emdaemon_delete_mail_attachment(int attachment_id, int* err_code)
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mail_attachment(attachment_id, &err))  {
+       if (!emcore_delete_mail_attachment(multi_user_name, attachment_id, &err))  {
                EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -484,7 +491,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int nth, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_download_attachment(char *multi_user_name, int account_id, int mail_id, int nth, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], handle[%p], err_code[%p]", account_id, mail_id, nth, handle, err_code);
 
@@ -498,7 +505,7 @@ INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int
                goto FINISH_OFF;
        }
 
-       if (!_emdaemon_check_mail_id(mail_id, &err)) {
+       if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
                EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -508,6 +515,7 @@ INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int
        event_data->account_id         = account_id;
        event_data->event_param_data_4 = mail_id;
        event_data->event_param_data_5 = nth;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err))  {
                EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
@@ -552,14 +560,16 @@ void* thread_func_to_delete_mail(void *thread_argument)
        int  from_server = 0;
        int  noti_param_2 = 0;
        int handle = 0;
+       char *multi_user_name = NULL;
        email_event_t *event_data = (email_event_t*)thread_argument;
 
        account_id         = event_data->account_id;
        mail_id_list       = (int*)event_data->event_param_data_3;
        mail_id_count      = event_data->event_param_data_4;
        from_server        = event_data->event_param_data_5;
+       multi_user_name    = event_data->multi_user_name;
 
-       if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
+       if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
                EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
                emcore_free_event(event_data); /* prevent 17922 */
                EM_SAFE_FREE(event_data);
@@ -589,7 +599,7 @@ FINISH_OFF:
        return SUCCESS;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mail(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 mailbox_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);
 
@@ -609,7 +619,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_
                goto FINISH_OFF;
        }
 
-       if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
+       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;
        }
@@ -628,7 +638,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl_data->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl_data->account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
@@ -645,6 +655,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_
        event_data->event_param_data_3     = (char*)p;
        event_data->event_param_data_4     = mail_ids_count;
        event_data->event_param_data_5     = from_server;
+       event_data->multi_user_name        = EM_SAFE_STRDUP(multi_user_name);
 
        THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
        THREAD_DETACH(delete_thread); /* free resources used for new thread */
@@ -660,7 +671,8 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
-       if (thread_error != 0) {
+       if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+           emcore_free_event(event_data);
                EM_SAFE_FREE(event_data);
                EM_SAFE_FREE(p);
        }
@@ -671,7 +683,7 @@ FINISH_OFF:
        return ret;
 }
 
-int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
+int emdaemon_delete_mail_all(char *multi_user_name, int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_from_server[%d], handle[%p], err_code[%p]", input_mailbox_id, input_from_server, output_handle, output_err_code);
 
@@ -686,12 +698,12 @@ int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *o
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if(!emcore_delete_all_mails_of_mailbox(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, EMAIL_DELETE_LOCALLY, &err)) {
                EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -703,6 +715,7 @@ int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *o
                event_data->account_id         = mailbox_tbl->account_id;
                event_data->event_param_data_4 = input_mailbox_id;
                event_data->event_param_data_5 = input_from_server;
+               event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
                if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
@@ -719,10 +732,8 @@ int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *o
                        EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
                }
 
-               if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
+               if (!emstorage_mail_search_start(multi_user_name, NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
                        EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
-
-
                        goto FINISH_OFF;
                }
 
@@ -791,6 +802,7 @@ void* thread_func_to_move_mail(void *thread_argument)
        int handle = 0;
        email_event_t *event_data = (email_event_t*)thread_argument;
        int dst_mailbox_id = 0;
+       char *multi_user_name = NULL;
 
        if(!event_data) { /*prevent 53096*/
                EM_DEBUG_EXCEPTION("INVALID_PARMAETER");
@@ -804,9 +816,9 @@ void* thread_func_to_move_mail(void *thread_argument)
        dst_mailbox_id     = event_data->event_param_data_5;
        noti_param_1       = event_data->event_param_data_6;
        noti_param_2       = event_data->event_param_data_7;
+       multi_user_name    = event_data->multi_user_name;
 
-
-       if (!emcore_move_mail(mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
+       if (!emcore_move_mail(multi_user_name, mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
                EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -829,7 +841,7 @@ FINISH_OFF:
        return SUCCESS;
 }
 
-INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailbox_id, int* err_code)
+INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_mailbox_id, int dst_mailbox_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
 
@@ -840,7 +852,7 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
        int *p = NULL;
        int i = 0;
        int num = 0;
-       int thread_error;
+       int thread_error = 0;
        email_account_t *ref_account = NULL;
        email_mail_list_item_t *mail_list = NULL;
        email_event_t *event_data = NULL;
@@ -854,17 +866,17 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(dst_mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, dst_mailbox_tbl->account_id);
 
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
@@ -872,7 +884,7 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
                goto FINISH_OFF;
        }
 
-       if(!emstorage_get_mail_list(src_mailbox_tbl->account_id, src_mailbox_id, NULL, EMAIL_LIST_TYPE_NORMAL, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, false, &mail_list, &select_num, &err)) {
+       if(!emstorage_get_mail_list(multi_user_name, src_mailbox_tbl->account_id, src_mailbox_id, NULL, EMAIL_LIST_TYPE_NORMAL, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, false, &mail_list, &select_num, &err)) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
                goto FINISH_OFF;
        }
@@ -920,6 +932,7 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
        event_data->event_param_data_4 = num;
        event_data->event_param_data_5 = dst_mailbox_id;
        event_data->event_param_data_8 = src_mailbox_id;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        int i = 0, activityid = 0;
@@ -943,6 +956,7 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
        THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
        THREAD_DETACH(move_thread); /* free resources used for new thread */
+       EM_DEBUG_LOG("thread_error [%d]", thread_error);
        ret = true;
 
 FINISH_OFF:
@@ -972,17 +986,22 @@ FINISH_OFF:
        if(mails != NULL )
                EM_SAFE_FREE(mails);
 
+    if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+        emcore_free_event(event_data);
+        EM_SAFE_FREE(event_data);
+        EM_SAFE_FREE(p);
+    }
 
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
+INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
 
        /*  default variable */
-       int mail_id = 0, *p = NULL, thread_error;
+       int mail_id = 0, *p = NULL, thread_error = 0;
        int ret = false, err = EMAIL_ERROR_NONE;
        emstorage_mail_tbl_t* mail_table_data = NULL;
        email_account_t* ref_account = NULL;
@@ -997,12 +1016,12 @@ INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(dest_mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, dest_mailbox_tbl->account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
@@ -1013,9 +1032,8 @@ INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id
        /* Getting source mailbox name */
        mail_id = mail_ids[0];
 
-       if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
+       if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
-
                goto FINISH_OFF;
        }
 
@@ -1046,6 +1064,7 @@ INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id
        event_data->event_param_data_8 = src_mailbox_id;
        event_data->event_param_data_6 = noti_param_1;
        event_data->event_param_data_7 = noti_param_2;
+       event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
 #ifdef __FEATURE_LOCAL_ACTIVITY__
        int i = 0, activityid = 0;
@@ -1069,6 +1088,7 @@ INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id
 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
        THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
        THREAD_DETACH(move_thread); /* free resources used for new thread */
+       EM_DEBUG_LOG("thread_error [%d]", thread_error);
        ret = true;
 
 FINISH_OFF:
@@ -1089,11 +1109,17 @@ FINISH_OFF:
        if (dest_mailbox_tbl)
                emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
 
+    if (err != EMAIL_ERROR_NONE || thread_error != 0) {
+        emcore_free_event(event_data);
+        EM_SAFE_FREE(event_data);
+        EM_SAFE_FREE(p);
+    }
+
        EM_DEBUG_FUNC_END("ret [%d]", ret);
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_set_flags_field(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*/
 
@@ -1109,12 +1135,12 @@ INTERNAL_FUNC int emdaemon_set_flags_field(int account_id, int mail_ids[], int n
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_account_by_id(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;
        }
 
-       if (!emcore_set_flags_field(account_id, mail_ids, num, field_type, value, &err))  {
+       if (!emcore_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, &err))  {
                EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
                goto FINISH_OFF;
        }
@@ -1138,6 +1164,7 @@ INTERNAL_FUNC int emdaemon_set_flags_field(int account_id, int mail_ids[], int n
                event_data->event_param_data_4 = num;
                event_data->event_param_data_5 = field_type;
                event_data->event_param_data_6 = value;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
                if (!emcore_insert_event(event_data, (int*)&handle, &err))  {
                        EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
@@ -1164,11 +1191,9 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
+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);
-
-       int ret = false;
        int err = EMAIL_ERROR_NONE;
        /*email_event_t *event_data = NULL;*/
        email_account_t *ref_account = NULL;
@@ -1180,14 +1205,14 @@ INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(input_mail_data->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id);
        if (!ref_account)  {
                EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
+       if ( (err = emcore_update_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1202,6 +1227,7 @@ INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email
                event_data->event_param_data_3 = (char*)input_meeting_request;          // need to be duplicated, it is double freed
                event_data->event_param_data_4 = input_attachment_count;
                event_data->event_param_data_5 = input_from_eas;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
                if (!emcore_insert_event(event_data, &handle, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
@@ -1212,16 +1238,8 @@ INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email
 */
 #endif
 
-       ret = true;
-
 FINISH_OFF:
 
-/*prevent 64298*/
-/*     if (ret == false && event_data) {
-               emcore_free_event(event_data);
-               EM_SAFE_FREE(event_data);
-       }
-*/
        if (ref_account) {
                emcore_free_account(ref_account);
                EM_SAFE_FREE(ref_account);
@@ -1232,7 +1250,7 @@ FINISH_OFF:
 }
 
 
-int _emdaemon_check_mail_id(int mail_id, int* err_code)
+int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
 
@@ -1241,7 +1259,7 @@ int _emdaemon_check_mail_id(int mail_id, int* err_code)
 
        emstorage_mail_tbl_t* mail = NULL;
 
-       if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail, true, &err))  {
+       if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1258,33 +1276,46 @@ FINISH_OFF:
        return ret;
 }
 
+typedef struct {
+        int   mail_id;
+        char *multi_user_name;
+} email_retry_info;
 
-INTERNAL_FUNC int emdaemon_send_mail_retry(int mail_id,  int timeout_in_sec, int* err_code)
+INTERNAL_FUNC int emdaemon_send_mail_retry(char *multi_user_name, int mail_id, int timeout_in_sec, int* err_code)
 {
        int ret = false;
        int err = EMAIL_ERROR_NONE;
        long nTimerValue = 0;
-       char mail_id_string[10] = { 0, };
+       email_retry_info *retry_info = NULL;
 
-       if (!_emdaemon_check_mail_id(mail_id, &err))  {
+       if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
                EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if ( timeout_in_sec == 0 ) {
-               if(!emdaemon_send_mail(mail_id, NULL, &err)) {
+       if (timeout_in_sec == 0) {
+               if(!emdaemon_send_mail(multi_user_name, mail_id, NULL, &err)) {
                        EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
-       else if ( timeout_in_sec > 0 ) {
-               sprintf(mail_id_string,"%d",mail_id);
+       else if (timeout_in_sec > 0) {
+               retry_info = em_malloc(sizeof(email_retry_info));
+               if (retry_info == NULL) {
+                       EM_DEBUG_EXCEPTION("em_malloc failed");
+                       err = EMAIL_ERROR_OUT_OF_MEMORY;
+               }
+
+               retry_info->mail_id = mail_id;
+               retry_info->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
                nTimerValue = timeout_in_sec * 1000;
-               if ( emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)mail_id_string) <= 0) {
+
+               if (emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)retry_info) <= 0) {
                        EM_DEBUG_EXCEPTION("Failed to start timer");
                        goto FINISH_OFF;
                }
        }
+
        ret = true;
 
 FINISH_OFF:
@@ -1294,37 +1325,39 @@ FINISH_OFF:
        return ret;
 }
 
-
-INTERNAL_FUNC void _OnMailSendRetryTimerCB( void* data )
+INTERNAL_FUNC void _OnMailSendRetryTimerCB(void* data)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
-       int mail_id = 0 ;
+       email_retry_info *retry_info = NULL;
 
-       if!data ) {
+       if (!data ) {
                EM_DEBUG_LOG("Invalid param");
                goto FINISH_OFF;
        }
 
-       mail_id = atoi((char*)data);
+       retry_info = (email_retry_info *)data;
 
-       if (!_emdaemon_check_mail_id(mail_id, &err))  {
+       if (!_emdaemon_check_mail_id(retry_info->multi_user_name, retry_info->mail_id, &err))  {
                EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if(!emdaemon_send_mail(mail_id, NULL, &err)) {
+       if(!emdaemon_send_mail(retry_info->multi_user_name, retry_info->mail_id, NULL, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
                goto FINISH_OFF;
        }
 
 FINISH_OFF:
 
+       EM_SAFE_FREE(retry_info->multi_user_name);
+       EM_SAFE_FREE(retry_info);
+
        EM_DEBUG_FUNC_END();
        return;
 }
 
-INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
+INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(char *multi_user_name, int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("thread_id [%d], target_mailbox_id [%d], move_always_flag [%d], err_code [%p]", thread_id, target_mailbox_id, move_always_flag, err_code);
        int ret = false;
@@ -1340,7 +1373,7 @@ INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target
                goto FINISH_OFF;
        }
 
-       if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
+       if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
                EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
 
                goto FINISH_OFF;
@@ -1360,7 +1393,7 @@ INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target
        account_id = mail_list[0].account_id;
 
 
-       if (!emcore_get_mailbox_list(account_id, &target_mailbox_list, &mailbox_count, &err)) {
+       if (!emcore_get_mailbox_list(multi_user_name, account_id, &target_mailbox_list, &mailbox_count, &err)) {
                EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1379,7 +1412,7 @@ INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target
        }
 
 /*
-       if (!emdaemon_move_mail(mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
+       if (!emdaemon_move_mail(multi_user_name, mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1402,7 +1435,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_flag, int *handle, int *err_code)
+INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_id, int delete_always_flag, int *handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
        int ret = false;
@@ -1411,9 +1444,8 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_f
        int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
        email_mail_list_item_t *mail_list = NULL;
 
-       if (!emstorage_get_mail_list(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;
        }
 
@@ -1434,7 +1466,7 @@ INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_f
 
        // should remove requiring of mailbox information from this function.
        // email-service should find mailboxes itself by its mail id.
-       if (!emdaemon_delete_mail(mailbox_id, mail_id_list, result_count, false, handle, &err)) {
+       if (!emdaemon_delete_mail(multi_user_name, mailbox_id, mail_id_list, result_count, false, handle, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1454,7 +1486,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
+INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(char *multi_user_name, int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("thread_id [%d], seen_flag [%d], on_server [%d], handle [%p], err_code [%p]", thread_id, seen_flag, on_server, handle, err_code);
        int ret = false;
@@ -1462,9 +1494,8 @@ INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_fl
        int account_id, *mail_id_list = NULL, result_count = 0, i;
        email_mail_list_item_t *mail_list = NULL;
 
-       if (!emstorage_get_mail_list(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;
        }
 
@@ -1482,7 +1513,7 @@ INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_fl
 
        account_id = mail_list[0].account_id;
 
-       if (!emdaemon_set_flags_field(account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
+       if (!emdaemon_set_flags_field(multi_user_name, account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1502,10 +1533,9 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle)
+INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name, int input_mailbox_id, int input_on_server, int *output_handle)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
-       int ret = false;
        int err = EMAIL_ERROR_NONE;
        int handle = 0;
        int event_insert = false;
@@ -1519,12 +1549,12 @@ INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, i
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -1537,6 +1567,7 @@ INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, i
                event_data->type               = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
                event_data->account_id         = mailbox_tbl->account_id;
                event_data->event_param_data_4 = input_mailbox_id;
+               event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
 
                if (!emcore_insert_event(event_data, &handle, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
@@ -1546,16 +1577,15 @@ INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, i
        }
        else
 #endif
-               if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
+               if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-       ret = true;
-
 FINISH_OFF:
 
        if (event_insert == false && event_data) {
+               EM_SAFE_FREE(event_data->multi_user_name);
                emcore_free_event(event_data);
                EM_SAFE_FREE(event_data);
        }
index 86926f9..66c6508 100755 (executable)
 extern int g_local_activity_run;
 #endif
 
-INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(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);
 
        int ret = false;
        int err = EMAIL_ERROR_NONE;
-       email_account_t *ref_account = NULL;
        email_event_t *event_data = NULL;
 
        if (account_id <= 0 ||!mailbox)  {
@@ -64,18 +63,11 @@ INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(int account_id, char* mailbox,
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
-
-       if (!ref_account) {
-               EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
-               err = EMAIL_ERROR_INVALID_ACCOUNT;
-               goto FINISH_OFF;
-       }
-
        event_data = em_malloc(sizeof(email_event_t));
        event_data->type = EMAIL_EVENT_SYNC_IMAP_MAILBOX;
        event_data->account_id = account_id;
        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)) {
                EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
@@ -91,18 +83,13 @@ FINISH_OFF:
                EM_SAFE_FREE(event_data);
        }
 
-       if (ref_account) {
-               emcore_free_account(ref_account);
-               EM_SAFE_FREE(ref_account);
-       }
-
        if (err_code)
                *err_code = err;
        EM_DEBUG_FUNC_END();
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code)
+INTERNAL_FUNC int emdaemon_get_mailbox_list(char *multi_user_name, int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p], err_code[%p]", account_id, mailbox_list, count, err_code);
 
@@ -116,7 +103,7 @@ INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** ma
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account_id);
 
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
@@ -124,7 +111,7 @@ INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** ma
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_mailbox_list(account_id, mailbox_list, count, &err))  {
+       if (!emcore_get_mailbox_list(multi_user_name, account_id, mailbox_list, count, &err))  {
                EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -145,7 +132,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, int* total, int* unseen, int* err_code)
+INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(char *multi_user_name, email_mailbox_t* mailbox, int* total, int* unseen, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
 
@@ -160,14 +147,14 @@ INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, i
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id);
        if (ref_account == NULL)  {
                EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", mailbox->account_id);
                err = EMAIL_ERROR_INVALID_ACCOUNT;
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_mail_count(mailbox, total, unseen, &err))  {
+       if (!emcore_get_mail_count(multi_user_name, mailbox, total, unseen, &err))  {
                EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -188,7 +175,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_add_mailbox(char *multi_user_name, email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
 
@@ -205,7 +192,7 @@ INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_serv
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(new_mailbox->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, new_mailbox->account_id);
 
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", new_mailbox->account_id);
@@ -246,13 +233,14 @@ INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_serv
                event_data->account_id         = new_mailbox->account_id;
                event_data->event_param_data_1 = (char*)mailbox;
                event_data->event_param_data_4 = on_server;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);                       
 
                if(!emcore_insert_event(event_data, (int*)handle, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
                        goto FINISH_OFF;
                }
        } else {        /* sync */
-               if (!emcore_create_mailbox(new_mailbox, on_server, &err))  {
+               if (!emcore_create_mailbox(multi_user_name, new_mailbox, on_server, -1, -1, &err))  {
                        EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -284,7 +272,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
+INTERNAL_FUNC int emdaemon_set_mailbox_type(char *multi_user_name, int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
 
@@ -298,18 +286,19 @@ INTERNAL_FUNC int emdaemon_set_mailbox_type(int input_mailbox_id, email_mailbox_
                goto FINISH_OFF;
        }
 
-       if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
+       if ( (err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       if (!emstorage_update_mailbox_type(mailbox_tbl->account_id, -1, input_mailbox_id, input_mailbox_type, true, &err))  {
+       if (!emstorage_update_mailbox_type(multi_user_name, mailbox_tbl->account_id, -1, input_mailbox_id, input_mailbox_type, true, &err))  {
                EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
 
 
 FINISH_OFF:
+
        if (mailbox_tbl)
                emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
 
@@ -317,7 +306,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emdaemon_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox)
+INTERNAL_FUNC int emdaemon_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_is_local_mailbox);
 
@@ -330,7 +319,7 @@ INTERNAL_FUNC int emdaemon_set_local_mailbox(int input_mailbox_id, int input_is_
                goto FINISH_OFF;
        }
 
-       if ( (err = emstorage_set_local_mailbox(input_mailbox_id, input_is_local_mailbox, true)) != EMAIL_ERROR_NONE)  {
+       if ( (err = emstorage_set_local_mailbox(multi_user_name, input_mailbox_id, input_is_local_mailbox, true)) != EMAIL_ERROR_NONE)  {
                EM_DEBUG_EXCEPTION("emstorage_set_local_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -342,7 +331,7 @@ FINISH_OFF:
        return err;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_delete_mailbox(char *multi_user_name, int input_mailbox_id, int on_server, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
 
@@ -353,7 +342,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, i
        email_account_t *ref_account = NULL;
        email_event_t *event_data = NULL;
 
-       if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
+       if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
                EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -365,7 +354,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, i
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
@@ -376,6 +365,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, i
        /*  on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
        if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
                on_server = 0;
+               recursive = 0;
        }
 
        if ( on_server ) {      /*  async */
@@ -385,12 +375,14 @@ INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, i
                event_data->event_param_data_4 = input_mailbox_id;
                event_data->event_param_data_5 = on_server;
                event_data->event_param_data_6 = recursive;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
                if(!emcore_insert_event(event_data, (int*)handle, &err)) {
                        EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
                        goto FINISH_OFF;
                }
        } else {
-               if (!emcore_delete_mailbox(input_mailbox_id, on_server, recursive))  {
+               if (!emcore_delete_mailbox(multi_user_name, input_mailbox_id, on_server, recursive))  {
                        EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -416,7 +408,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err_code)
+INTERNAL_FUNC int emdaemon_delete_mailbox_all(char *multi_user_name, email_mailbox_t* mailbox, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("malibox[%p], err_code[%p]", mailbox, err_code);
 
@@ -436,7 +428,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(mailbox->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id);
 
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
@@ -444,7 +436,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err
                goto FINISH_OFF;
        }
 
-       if (!emcore_delete_mailbox_all(mailbox, &err))  {
+       if (!emcore_delete_mailbox_all(multi_user_name, mailbox, &err))  {
                EM_DEBUG_EXCEPTION("emcore_delete_all failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -465,7 +457,7 @@ FINISH_OFF:
 }
 
 
-INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_id, int *handle, int* err_code)
+INTERNAL_FUNC int emdaemon_sync_header(char *multi_user_name, int input_account_id, int input_mailbox_id, int *handle, int* err_code)
 {
        EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mailbox_id[%d], handle[%p], err_code[%p]", input_account_id, input_mailbox_id, handle, err_code);
 
@@ -486,6 +478,8 @@ INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_i
                event_data->type               = EMAIL_EVENT_SYNC_HEADER;
                event_data->account_id         = input_account_id;
                event_data->event_param_data_5 = input_mailbox_id;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);               
+
                /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
                if (input_mailbox_id == 0)
                        event_data->event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
@@ -499,6 +493,8 @@ INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_i
                event_data->type               = EMAIL_EVENT_SYNC_HEADER;
                event_data->account_id         = input_account_id;
                event_data->event_param_data_5 = input_mailbox_id;
+               event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);               
+
                /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
                if (input_mailbox_id == 0)
                        event_data->event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
@@ -516,7 +512,7 @@ INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_i
        if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, ((input_mailbox_id==0)? NULL:input_mailbox_id_str), *handle, 0))
                EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
 
-/*     if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
+/*     if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
                EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
 */
 
@@ -541,7 +537,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code)
+INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], handle[%p], err_code[%p]", account_id, mailbox_id, handle, err_code);
 
@@ -560,6 +556,7 @@ INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(int account_id, int mai
        event_data->account_id = account_id;
        event_data->event_param_data_4 = mailbox_id;
        event_data->event_param_data_5 = new_slot_size;
+       event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if (!emcore_insert_event(event_data, (int*)handle, &err))   {
                EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
@@ -581,7 +578,7 @@ FINISH_OFF:
        return ret;
 }
 
-INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle)
+INTERNAL_FUNC int emdaemon_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle)
 {
        EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_mailbox_path[%p] input_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_eas_data, input_eas_data_length, input_on_server, output_handle);
 
@@ -598,12 +595,12 @@ INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mail
        }
 
        if (input_on_server) {
-               if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
+               if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
                        goto FINISH_OFF;
                }
 
-               if (!emstorage_get_account_by_id(old_mailbox_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_data, true, &err)) {
+               if (!emstorage_get_account_by_id(multi_user_name, old_mailbox_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_data, true, &err)) {
                        EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -616,6 +613,7 @@ INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mail
                        event_data->event_param_data_3 = EM_SAFE_STRDUP(input_mailbox_alias);
                        event_data->event_param_data_4 = input_mailbox_id;
                        event_data->account_id         = old_mailbox_data->account_id;
+                       event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
 
                        if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
                                EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
@@ -623,7 +621,7 @@ INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mail
                        }
                }
        } else {
-               if ((err = emcore_rename_mailbox(input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_eas_data, input_eas_data_length, false, true, 0)) != EMAIL_ERROR_NONE) {
+               if ((err = emcore_rename_mailbox(multi_user_name, input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_eas_data, input_eas_data_length, false, true, 0)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_rename_mailbox failed [%d]", err);
                        goto FINISH_OFF;
                }
index b3fa69b..3910ecf 100755 (executable)
@@ -41,10 +41,9 @@ extern "C"
 #endif /* __cplusplus */
 
 
-INTERNAL_FUNC int emdaemon_add_polling_alarm(int account_id);
+INTERNAL_FUNC int emdaemon_add_polling_alarm(char *multi_user_name, int account_id);
 INTERNAL_FUNC int emdaemon_remove_polling_alarm(int account_id);
 INTERNAL_FUNC int emdaemon_check_auto_polling_started(int account_id);
-INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t  alarm_id, void* user_param);
 INTERNAL_FUNC int emdaemon_free_account_alarm_binder_list();
 
 #ifdef __cplusplus
index edb9982..fe8b435 100755 (executable)
 #ifndef __EMAIL_DAEMON_EMN_H__
 #define __EMAIL_DAEMON_EMN_H__
 
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMN EMN
-* @{
-*/
 
 #include "email-internal-types.h"
 
@@ -93,10 +88,6 @@ typedef int (*email_emn_noti_cb)(
 }
 #endif /* __cplusplus */
 
-/**
-* @} @}
-*/
-
 
 #endif /* __EMAIL_DAEMON_EMN_H__ */
 /* EOF */
index e4faa77..799338e 100755 (executable)
@@ -34,6 +34,10 @@ INTERNAL_FUNC int emdaemon_start_event_loop(int *err_code);
 INTERNAL_FUNC int emdaemon_start_event_loop_for_sending_mails(int *err_code);
 INTERNAL_FUNC int emdaemon_start_thread_for_downloading_partial_body(int *err_code);
 
+#ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
+INTERNAL_FUNC void emdaemon_setup_handler_for_open_ssl_multithread(void);
+INTERNAL_FUNC void emdaemon_cleanup_handler_for_open_ssl_multithread(void);
+#endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
 
 #ifdef __cplusplus
 }
index 9524e37..c15cf49 100755 (executable)
@@ -41,9 +41,7 @@ extern "C"
 
 INTERNAL_FUNC int emdaemon_core_init(int *err_code);
 
-#ifdef __FEATURE_BLOCKING_MODE__
 INTERNAL_FUNC bool emdaemon_init_blocking_mode_status();
-#endif
 
 INTERNAL_FUNC int emdaemon_init_alarm_data_list();
 
index e82347c..ecc49a3 100755 (executable)
 #ifndef __EMAIL_DAEMON_H__
 #define __EMAIL_DAEMON_H__
 
-/**
-* @defgroup EMAIL_SERVICE EmailFW
-* @{
-*/
-
-/**
-* @ingroup EMAIL_SERVICE
-* @defgroup EMAIL_SERVICE Email Service
-* @{
-*/
-
 #include "email-types.h"
 #include "email-internal-types.h"
 
@@ -62,7 +51,7 @@ extern "C"
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_initialize(int* err_code);
+INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code);
 
 /**
  * Finalize Email-engine.
@@ -85,7 +74,7 @@ INTERNAL_FUNC int emdaemon_finalize(int* err_code);
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_code);
+INTERNAL_FUNC int emdaemon_create_account(char *multi_user_name, email_account_t* account, int* err_code);
 
 /**
  * Delete a email account.
@@ -95,7 +84,7 @@ INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_cod
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_account(char *multi_user_name, int account_id, int* err_code);
 
 /**
  * Validate a email account.
@@ -105,9 +94,9 @@ INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code);
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_validate_account(char *multi_user_name, int account_id, int *handle, int* err_code);
 
-INTERNAL_FUNC int emdaemon_validate_account_ex(email_account_t* account, int *handle);
+INTERNAL_FUNC int emdaemon_validate_account_ex(char *multi_user_name, email_account_t* account, int *handle);
 
 /**
  * Change the information of a email account.
@@ -118,7 +107,7 @@ INTERNAL_FUNC int emdaemon_validate_account_ex(email_account_t* account, int *ha
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_account, int* err_code);
+INTERNAL_FUNC int emdaemon_update_account(char *multi_user_name, int account_id, email_account_t* new_account, int* err_code);
 
 /**
  * Change the information of a email account after validation
@@ -130,7 +119,7 @@ INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_a
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email_account_t* new_account_info, int *handle,int *err_code);
+INTERNAL_FUNC int emdaemon_validate_account_and_update(char *multi_user_name, int old_account_id, email_account_t* new_account_info, int *handle,int *err_code);
 
 /**
  * Get a email account by ID.
@@ -142,7 +131,7 @@ INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_account_t* acount, int* err_code);
+INTERNAL_FUNC int emdaemon_get_account(char *multi_user_name, int account_id, int pulloption, email_account_t* acount, int* err_code);
 
 /**
  * Get all emails.
@@ -153,7 +142,7 @@ INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_acc
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** acount_list, int* count, int* err_code);
+INTERNAL_FUNC int emdaemon_get_account_list(char *multi_user_name, email_account_t** acount_list, int* count, int* err_code);
 
 /**
  * Free allocated memory.
@@ -175,7 +164,7 @@ INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int coun
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filtering_set, int* err_code);
+INTERNAL_FUNC int emdaemon_get_filter(char *multi_user_name, int filter_id, email_rule_t** filtering_set, int* err_code);
 
 /**
  * Get all filterings.
@@ -186,7 +175,7 @@ INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filtering_se
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filtering_set, int* count, int* err_code);
+INTERNAL_FUNC int emdaemon_get_filter_list(char *multi_user_name, email_rule_t** filtering_set, int* count, int* err_code);
 
 /**
  * find a filter already exists.
@@ -196,7 +185,7 @@ INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filtering_set, int* co
  * @remarks N/A
  * @return This function returns true if enable add filter, else returns false.
  */
-INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code);
+INTERNAL_FUNC int emdaemon_find_filter(char *multi_user_name, email_rule_t* filter_info, int* err_code);
 
 /**
  * Add a filter information.
@@ -205,7 +194,7 @@ INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
  * @remarks N/A
  * @return This function returns true on success or false on failure.(only EMAIL_FILTER_BLOCK supported.)
  */
-INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filtering_set);
+INTERNAL_FUNC int emdaemon_add_filter(char *multi_user_name, email_rule_t* filtering_set);
 
 /**
  * Change a filter information.
@@ -216,7 +205,7 @@ INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filtering_set);
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* new_set, int* err_code);
+INTERNAL_FUNC int emdaemon_update_filter(char *multi_user_name, int filter_id, email_rule_t* new_set, int* err_code);
 
 /**
  * Delete a filter information.
@@ -226,7 +215,7 @@ INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* new_set, i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_filter(char *multi_user_name, int filter_id, int* err_code);
 
 /**
  * Free allocated memory.
@@ -247,7 +236,7 @@ INTERNAL_FUNC int emdaemon_free_filter (email_rule_t** filtering_set, int count,
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_apply_filter(int filter_id, int* err_code);
+INTERNAL_FUNC int emdaemon_apply_filter(char *multi_user_name, int filter_id, int* err_code);
 
 /*****************************************************************************/
 /*  Mail                                                                     */
@@ -263,7 +252,7 @@ INTERNAL_FUNC int emdaemon_apply_filter(int filter_id, int* err_code);
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *handle, int* err_code);
 
 /**
  * Send all mails to been saved in Offline-mode.
@@ -275,11 +264,11 @@ INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code);
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_send_mail_saved(char *multi_user_name, int account_id, int *handle, int* err_code);
 
-INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas);
+INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas);
 
-INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code);
+INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code);
 
 /**
  * Delete a mail or multiple mails.
@@ -295,7 +284,7 @@ INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox
  * @return This function returns true on success or false on failure.
  */
 
-INTERNAL_FUNC int emdaemon_delete_mail(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 mailbox_id, int mail_id[], int num, int from_server,  int *handle, int* err_code);
 
 /**
  * Delete all mail from a mailbox.
@@ -307,7 +296,7 @@ INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_id[], int num, i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code);
+INTERNAL_FUNC int emdaemon_delete_mail_all(char *multi_user_name, int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code);
 
 /**
  * Move a email to another mailbox.
@@ -321,7 +310,7 @@ INTERNAL_FUNC int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code);
+INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code);
 
 /**
  * Move all email to another mailbox.
@@ -333,7 +322,7 @@ INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailbox_id, int* err_code);
+INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_mailbox_id, int dst_mailbox_id, int* err_code);
 
 
 /**
@@ -347,7 +336,7 @@ INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailb
  * @remarks N/A
  * @return This function returns EMAIL_ERROR_NONE on success or error code on failure.
  */
-INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas);
+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);
 
 
 /**
@@ -370,7 +359,7 @@ INTERNAL_FUNC void _OnMailSendRetryTimerCB( void* data );
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_download_body(char *multi_user_name, int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code);
 
 /**
  * Get a mail attachment.
@@ -382,7 +371,7 @@ INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbos
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachment, int* err_code);
+INTERNAL_FUNC int emdaemon_get_attachment(char *multi_user_name, int attachment_id, email_attachment_data_t** attachment, int* err_code);
 
 /**
  * Download a email nth-attachment from server.
@@ -396,7 +385,7 @@ INTERNAL_FUNC int emdaemon_get_attachment(int attachment_id, email_attachment_da
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int nth,  int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_download_attachment(char *multi_user_name, int account_id, int mail_id, int nth,  int *handle, int* err_code);
 
 
 /**
@@ -408,7 +397,7 @@ INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, int* err_code);
+INTERNAL_FUNC int emdaemon_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t* attachment, int* err_code);
 
 /**
  * Delete a attachment from email.
@@ -419,7 +408,7 @@ INTERNAL_FUNC int emdaemon_add_attachment(int mail_id, email_attachment_data_t*
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_mail_attachment(int attachment_id, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_mail_attachment(char *multi_user_name, int attachment_id, int* err_code);
 
 /**
  * Free allocated memroy for email attachment.
@@ -443,12 +432,12 @@ INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int onserver, int* err_code);
+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);
 
 /*****************************************************************************/
 /*  Mailbox                                                                  */
 /*****************************************************************************/
-INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(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);
 
 /**
  * Download header of new emails from mail server.
@@ -460,7 +449,7 @@ INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(int account_id, char* mailbox,
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_id, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_sync_header(char *multi_user_name, int input_account_id, int input_mailbox_id, int *handle, int* err_code);
 
 
 /**
@@ -473,7 +462,7 @@ INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, int* total, int* unseen, int* err_code);
+INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(char *multi_user_name, email_mailbox_t* mailbox, int* total, int* unseen, int* err_code);
 
 /**
  * Get all mailboxes from account.
@@ -485,7 +474,7 @@ INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code);
+INTERNAL_FUNC int emdaemon_get_mailbox_list(char *multi_user_name, int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code);
 
 /**
  * Create a new mailbox.
@@ -496,7 +485,7 @@ INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** ma
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_add_mailbox(char *multi_user_name, email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code);
 
 /**
  * Delete a mailbox.
@@ -507,7 +496,7 @@ INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_serv
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_mailbox(char *multi_user_name, int input_mailbox_id, int on_server, int *handle, int* err_code);
 
 /**
  * Delete all sub-mailboxes from a specific mailbox.
@@ -517,7 +506,7 @@ INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, i
  * @remarks N/A
  * @return This function returns true on success or false on failure.
  */
-INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err_code);
+INTERNAL_FUNC int emdaemon_delete_mailbox_all(char *multi_user_name, email_mailbox_t* mailbox, int* err_code);
 
 /**
  * Free allocated memory for mailbox information.
@@ -620,31 +609,29 @@ INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* accoun
 
 INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_account, email_account_t* new_account);
 
-INTERNAL_FUNC int emdaemon_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type);
-
-INTERNAL_FUNC int emdaemon_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox);
+INTERNAL_FUNC int emdaemon_set_mailbox_type(char *multi_user_name, int input_mailbox_id, email_mailbox_type_e input_mailbox_type);
 
-INTERNAL_FUNC int emdaemon_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, unsigned int *output_handle, int *err_code);
+INTERNAL_FUNC int emdaemon_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox);
 
-INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code);
+INTERNAL_FUNC int emdaemon_search_mail_on_server(char *multi_user_name, int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, unsigned int *output_handle, int *err_code);
 
-INTERNAL_FUNC int emdaemon_send_mail_retry( int mail_id,  int timeout_in_sec, int* err_code);
+INTERNAL_FUNC int emdaemon_clear_all_mail_data(char *multi_user_name, int* err_code);
 
-INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_account, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_send_mail_retry(char *multi_user_name, int mail_id,  int timeout_in_sec, int* err_code);
 
-INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code);
+INTERNAL_FUNC int emdaemon_validate_account_and_create(char *multi_user_name, email_account_t* new_account, int *handle, int* err_code);
 
-INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle);
+INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code);
 
-INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag, int *err_code);
+INTERNAL_FUNC int emdaemon_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle);
 
-INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_flag, int *handle, int *err_code);
+INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(char *multi_user_name, int thread_id, int target_mailbox_id, int move_always_flag, int *err_code);
 
-INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server, int *handle, int *err_code);
+INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_id, int delete_always_flag, int *handle, int *err_code);
 
-INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle);
+INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(char *multi_user_name, int thread_id, int seen_flag, int on_server, int *handle, int *err_code);
 
-INTERNAL_FUNC int emdaemon_kill_daemon_if_no_account();
+INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name, int input_mailbox_id, int input_on_server, int *output_handle);
 
 INTERNAL_FUNC int emdaemon_check_smack_rule(int app_sockfd, char *file_path);
 
@@ -652,9 +639,9 @@ INTERNAL_FUNC int emdaemon_set_smack_label(char *file_path, char *label);
 
 INTERNAL_FUNC void emdaemon_start_alert(void);
 
-INTERNAL_FUNC int emdaemon_finalize_sync(int account_id, int total_mail_count, int unread_mail_count, int *error);
+INTERNAL_FUNC int emdaemon_finalize_sync(char *multi_user_name, int account_id, int total_mail_count, int unread_mail_count, int vip_total_mail_count, int vip_unread_mail_count, int from_app, int *error);
 
-INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(int account_id, int *handle, int* err_code);
+INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(char *multi_user_name, int account_id, int *handle, int* err_code);
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 5a01c29..9393cc5 100755 (executable)
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <time.h>
+#include <gio/gio.h>
 
 #include "email-daemon.h"
 #include "email-ipc.h"
 #include "email-core-mailbox.h"
 #include "email-core-utils.h"
 #include "email-core-smime.h"
+#include "email-core-pgp.h"
 #include "email-core-cert.h"
 #include "email-core-task-manager.h"
 #include "email-core-signal.h"
+#include "email-core-imap-idle.h"
+#include "email-core-gmime.h"
 #include "email-storage.h"
 #include "email-dbus-activation.h"
+#include "email-core-container.h"
 
 void stb_create_account(HIPC_API a_hAPI)
 {
@@ -65,6 +70,13 @@ void stb_create_account(HIPC_API a_hAPI)
        email_account_t account;
        int err = EMAIL_ERROR_NONE;
 
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
+        multi_user_name = NULL;
+    }
        /* Initialize the email_account_t */
        memset(&account, 0x00, sizeof(email_account_t));
 
@@ -77,11 +89,13 @@ void stb_create_account(HIPC_API a_hAPI)
        local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
        /* Convert account stream to structure */
        em_convert_byte_stream_to_account(local_account_stream, buffer_size, &account);
+    account.user_name = EM_SAFE_STRDUP(multi_user_name);
 
        EM_DEBUG_LOG_SEC("Account name - %s", account.account_name);
        EM_DEBUG_LOG_SEC("Email Address - %s", account.user_email_address);
+    EM_DEBUG_LOG("Multi user name - %s", account.user_name);
 
-       if(!emdaemon_create_account(&account, &err)) {
+       if(!emdaemon_create_account(multi_user_name, &account, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_create_account fail ");
                goto FINISH_OFF;
        }
@@ -89,14 +103,16 @@ void stb_create_account(HIPC_API a_hAPI)
 
 #ifdef __FEATURE_AUTO_POLLING__
        /* start auto polling, if check_interval not zero */
-       if(account.check_interval > 0 || (account.peak_days && account.peak_interval > 0)) {
-               if(!emdaemon_add_polling_alarm(account.account_id))
+       if(account.check_interval > 0 || (account.peak_days > 0 && account.peak_interval > 0)) {
+               if(!emdaemon_add_polling_alarm(multi_user_name, account.account_id))
                        EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
        }
-#endif
+#ifdef __FEATURE_IMAP_IDLE__
+       else if(account.check_interval == 0 || (account.peak_days > 0 && account.peak_interval == 0))
+               emcore_refresh_imap_idle_thread();
+#endif /* __FEATURE_IMAP_IDLE__ */
 
-       /* add account details to contact DB */
-       emdaemon_insert_accountinfo_to_contact(&account);
+#endif
 
        local_result = 1;
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
@@ -108,22 +124,30 @@ void stb_create_account(HIPC_API a_hAPI)
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(account.account_id), sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
 
-       /* pass result to app */
-       if (!emipc_execute_stub_api(a_hAPI))
-               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
-
 FINISH_OFF:
-       
+
        if ( local_result == 0 ) {
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
-               if (!emipc_execute_stub_api(a_hAPI))
-                       EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
        }
+       if (!emipc_execute_stub_api(a_hAPI))
+               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
        emcore_free_account(&account); /* valgrind */
+
+       //emcore_init_account_reference();
+
+       if (local_result == 1) {
+               if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD_FINISH, account.account_id, 0, NULL, 0))
+                       EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
+       } else {
+               if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD_FAIL, account.account_id, 0, NULL, 0))
+                       EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
+       }
+
+       EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -134,6 +158,13 @@ void stb_delete_account(HIPC_API a_hAPI)
        int local_result = 0;
        int *ret_nth_value = NULL;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0)))
@@ -143,9 +174,10 @@ void stb_delete_account(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
        
-       if(!emdaemon_delete_account(account_id, &err)) {
+       if(!emdaemon_delete_account(multi_user_name, account_id, &err)) {
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
                        EM_DEBUG_LOG("emipc_add_parameter failed ");
+
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                        EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
 
@@ -169,21 +201,32 @@ void stb_delete_account(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+       /* if file is not deleted, main thread kills the thread */
+       emcore_send_signal_for_del_account (EMAIL_SIGNAL_DB_DELETED);
+       EM_DEBUG_LOG ("publish db of account deleted");
+
 FINISH_OFF:
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
 void stb_update_account(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int account_id = 0, buffer_size = 0, local_result = 0, with_validation = 0;
+       int account_id = 0, buffer_size = 0, with_validation = 0;
        int *ret_nth_value = NULL;
        char* local_account_stream = NULL;
        email_account_t new_account_info = {0};
-       email_account_t old_account_info = {0};
        int err = EMAIL_ERROR_NONE;
        int handle = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0)))
                account_id = *ret_nth_value;
@@ -191,6 +234,7 @@ void stb_update_account(HIPC_API a_hAPI)
                err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
                goto FINISH_OFF;
        }
+
        /* get account structure from stream */
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
        EM_DEBUG_LOG("size [%d]", buffer_size);
@@ -205,79 +249,31 @@ void stb_update_account(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
-       if(!emdaemon_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &old_account_info, &err)) {
-               EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
-               goto FINISH_OFF;
-       }
-
-
-       if( EM_SAFE_STRLEN (new_account_info.incoming_server_password) == 0 ) {
-               EM_SAFE_FREE(new_account_info.incoming_server_password); /* be allocated but has zero length */
-               EM_DEBUG_LOG_SEC("old_account_info->incoming_server_password [%s]", old_account_info.incoming_server_password);
-               new_account_info.incoming_server_password = EM_SAFE_STRDUP(old_account_info.incoming_server_password);
-               if(new_account_info.incoming_server_password == NULL) {
-                               EM_DEBUG_EXCEPTION("allocation for new_account_info->password failed");
-                               err = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
-               }
-
-
-       if( EM_SAFE_STRLEN (new_account_info.outgoing_server_password) == 0 ) {
-               EM_SAFE_FREE(new_account_info.outgoing_server_password);
-               if(old_account_info.outgoing_server_password) {
-                       new_account_info.outgoing_server_password = strdup (old_account_info.outgoing_server_password);
-                       if(new_account_info.outgoing_server_password == NULL) {
-                               EM_DEBUG_EXCEPTION("allocation for new_account_info->outgoing_server_password failed");
-                               err = EMAIL_ERROR_OUT_OF_MEMORY;
-                               goto FINISH_OFF;
-                       }
-               }
-       }
-
        if(with_validation) {
-               emdaemon_validate_account_and_update(account_id, &new_account_info, &handle, &err);
-               local_result = 1;
+               if(!emdaemon_validate_account_and_update(multi_user_name, account_id, &new_account_info, &handle, &err)){
+                       EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_update failed [%d]", err);
+                       goto FINISH_OFF;
+               }
        }
        else {
-               if(emdaemon_update_account(account_id, &new_account_info, &err)) {
-                       emdaemon_update_accountinfo_to_contact(&old_account_info, &new_account_info);
-                       local_result = 1;
-               }
-               else {
+               if(!emdaemon_update_account(multi_user_name, account_id, &new_account_info, &err)) {
                        EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
                        goto FINISH_OFF;
                }
 
        }
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
-               EM_DEBUG_EXCEPTION("emipc_add_parameter for result failed");
+FINISH_OFF:
 
-       if(with_validation) {
-               if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter for handle failed");
-       }
+       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");
 
-FINISH_OFF:
-       if ( local_result == 0 ) {
-               if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
-
-               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");
-       }
-
-       emcore_free_account(&old_account_info);
-
        emcore_free_account(&new_account_info);
 
+       EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -289,16 +285,23 @@ void stb_validate_account(HIPC_API a_hAPI)
        int local_result = 0;
        int err_code = 0;
        int handle = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
-       local_result = emdaemon_validate_account(account_id, &handle,&err_code);
+       local_result = emdaemon_validate_account(multi_user_name, account_id, &handle, &err_code);
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
                EM_DEBUG_LOG("emipc_add_parameter failed ");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
+        EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
@@ -306,6 +309,7 @@ void stb_validate_account(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -321,8 +325,15 @@ void stb_get_account_list(HIPC_API a_hAPI)
        int count;
        int size = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
-       if(emdaemon_get_account_list(&account_list, &count, &err)) {
+       if(emdaemon_get_account_list(multi_user_name, &account_list, &count, &err)) {
                EM_DEBUG_LOG("emdaemon_get_account_list success");
                local_result = 1;
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
@@ -366,6 +377,7 @@ void stb_get_account_list(HIPC_API a_hAPI)
        }
 
        EM_SAFE_FREE(local_stream);
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -376,6 +388,13 @@ void stb_sync_header(HIPC_API a_hAPI)
        int err = EMAIL_ERROR_NONE;
        int handle = -1;
        int account_id = 0, maibox_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -383,7 +402,7 @@ void stb_sync_header(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &maibox_id);
        EM_DEBUG_LOG("account_id [%d] maibox_id [%d]", account_id, maibox_id);
 
-       if(!emdaemon_sync_header(account_id, maibox_id, &handle, &err)) {
+       if(!emdaemon_sync_header(multi_user_name, account_id, maibox_id, &handle, &err)) {
                EM_DEBUG_EXCEPTION ("emdaemon_sync_header failed [%d]", err);
        }
 
@@ -393,6 +412,8 @@ void stb_sync_header(HIPC_API a_hAPI)
                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(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -404,6 +425,13 @@ void stb_download_body(HIPC_API a_hAPI)
        int attachment_count = 0;
        int handle = 0;
        int account_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* Account Id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -415,7 +443,7 @@ void stb_download_body(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &attachment_count);
 
        /*Download Body */
-       if (!emdaemon_download_body(account_id, mail_id, 1, attachment_count, &handle, &err)) {
+       if (!emdaemon_download_body(multi_user_name, account_id, mail_id, 1, attachment_count, &handle, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_download_body - failed");
                goto FINISH_OFF;
        }
@@ -425,12 +453,16 @@ void stb_download_body(HIPC_API a_hAPI)
 FINISH_OFF:
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
-       EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
+           EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
+
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
+
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END();
 }
 
@@ -445,6 +477,13 @@ void stb_create_mailbox(HIPC_API a_hAPI)
        int                   on_server                 = 0;
        email_mailbox_t   mailbox = { 0 };
        int               handle = 0; /* Added for cancelling mailbox creating  */
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
        EM_DEBUG_LOG("size [%d]", buffer_size);
@@ -464,7 +503,7 @@ void stb_create_mailbox(HIPC_API a_hAPI)
        else
                err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
 
-       emdaemon_add_mailbox(&mailbox, on_server, &handle, &err);
+       emdaemon_add_mailbox(multi_user_name, &mailbox, on_server, &handle, &err);
 
 FINISH_OFF:
 
@@ -482,6 +521,7 @@ FINISH_OFF:
                return;
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -493,6 +533,13 @@ void stb_delete_mailbox(HIPC_API a_hAPI)
        int      on_server              = 0;
        int      handle = 0; /* Added for cancelling mailbox deleting */
        int      input_mailbox_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* src_mailbox_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &input_mailbox_id);
@@ -505,7 +552,7 @@ void stb_delete_mailbox(HIPC_API a_hAPI)
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &on_server);
 
-       if(emdaemon_delete_mailbox(input_mailbox_id, on_server, &handle, &err))
+       if(emdaemon_delete_mailbox(multi_user_name, input_mailbox_id, on_server, &handle, &err))
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -516,6 +563,7 @@ void stb_delete_mailbox(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -525,6 +573,13 @@ void stb_set_mailbox_type(HIPC_API a_hAPI)
        int     err = EMAIL_ERROR_NONE;
        int mailbox_id = 0;
        int mailbox_type = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
        EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
@@ -532,7 +587,7 @@ void stb_set_mailbox_type(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_type);
        EM_DEBUG_LOG("mailbox_type[%d]", mailbox_type);
 
-       if( (err = emdaemon_set_mailbox_type(mailbox_id, mailbox_type)) != EMAIL_ERROR_NONE)
+       if( (err = emdaemon_set_mailbox_type(multi_user_name, mailbox_id, mailbox_type)) != EMAIL_ERROR_NONE)
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -541,6 +596,7 @@ void stb_set_mailbox_type(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -550,6 +606,13 @@ void stb_set_local_mailbox(HIPC_API a_hAPI)
        int     err = EMAIL_ERROR_NONE;
        int mailbox_id = 0;
        int is_local_mailbox = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
        EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
@@ -557,7 +620,7 @@ void stb_set_local_mailbox(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &is_local_mailbox);
        EM_DEBUG_LOG("is_local_mailbox[%d]", is_local_mailbox);
 
-       if( (err = emdaemon_set_local_mailbox(mailbox_id, is_local_mailbox)) != EMAIL_ERROR_NONE)
+       if( (err = emdaemon_set_local_mailbox(multi_user_name, mailbox_id, is_local_mailbox)) != EMAIL_ERROR_NONE)
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -566,6 +629,7 @@ void stb_set_local_mailbox(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -577,6 +641,13 @@ void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
        int account_id = 0;
        int mailbox_id = 0;
        int mail_slot_size = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
        EM_DEBUG_LOG("account_id[%d]", account_id);
@@ -587,7 +658,7 @@ void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_slot_size);
        EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
 
-       if(emdaemon_set_mail_slot_size_of_mailbox(account_id, mailbox_id, mail_slot_size, &handle, &err))
+       if(emdaemon_set_mail_slot_size_of_mailbox(multi_user_name, account_id, mailbox_id, mail_slot_size, &handle, &err))
                err = EMAIL_ERROR_NONE;
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
@@ -598,6 +669,7 @@ void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -611,6 +683,13 @@ void stb_rename_mailbox(HIPC_API a_hAPI)
        int on_server = 0;
        char *mailbox_path = NULL;
        char *mailbox_alias = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
        EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
@@ -620,7 +699,7 @@ void stb_rename_mailbox(HIPC_API a_hAPI)
        if(buffer_size > 0)       {
                mailbox_path = (char*)em_malloc(buffer_size);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
-               EM_DEBUG_LOG("mailbox_path [%s]", mailbox_path);
+               EM_DEBUG_LOG_SEC("mailbox_path [%s]", mailbox_path);
        }
 
        buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
@@ -628,13 +707,13 @@ void stb_rename_mailbox(HIPC_API a_hAPI)
        if(buffer_size > 0)       {
                mailbox_alias = (char*)em_malloc(buffer_size);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
-               EM_DEBUG_LOG("mailbox_alias [%s]", mailbox_alias);
+               EM_DEBUG_LOG_SEC("mailbox_alias [%s]", mailbox_alias);
        }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &on_server);
        EM_DEBUG_LOG("on_server[%d]", on_server);
 
-       if ((err = emdaemon_rename_mailbox(mailbox_id, mailbox_path, mailbox_alias, NULL, 0, on_server, &handle)) != EMAIL_ERROR_NONE) {
+       if ((err = emdaemon_rename_mailbox(multi_user_name, mailbox_id, mailbox_path, mailbox_alias, NULL, 0, on_server, &handle)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
        }
 
@@ -650,6 +729,7 @@ void stb_rename_mailbox(HIPC_API a_hAPI)
        EM_SAFE_FREE(mailbox_alias);
        EM_SAFE_FREE(mailbox_path);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -665,6 +745,13 @@ void stb_rename_mailbox_ex(HIPC_API a_hAPI)
        char *mailbox_path = NULL;
        char *mailbox_alias = NULL;
        void *eas_data = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
        EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
@@ -674,7 +761,7 @@ void stb_rename_mailbox_ex(HIPC_API a_hAPI)
        if(buffer_size > 0)       {
                mailbox_path = (char*)em_malloc(buffer_size);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
-               EM_DEBUG_LOG("mailbox_path [%s]", mailbox_path);
+               EM_DEBUG_LOG_SEC("mailbox_path [%s]", mailbox_path);
        }
 
        buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
@@ -682,7 +769,7 @@ void stb_rename_mailbox_ex(HIPC_API a_hAPI)
        if(buffer_size > 0)       {
                mailbox_alias = (char*)em_malloc(buffer_size);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
-               EM_DEBUG_LOG("mailbox_alias [%s]", mailbox_alias);
+               EM_DEBUG_LOG_SEC("mailbox_alias [%s]", mailbox_alias);
        }
 
        eas_data_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 3);
@@ -695,7 +782,7 @@ void stb_rename_mailbox_ex(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &on_server);
        EM_DEBUG_LOG("on_server[%d]", on_server);
 
-       if ((err = emdaemon_rename_mailbox(mailbox_id, mailbox_path, mailbox_alias, eas_data, eas_data_length, on_server, &handle)) != EMAIL_ERROR_NONE) {
+       if ((err = emdaemon_rename_mailbox(multi_user_name, mailbox_id, mailbox_path, mailbox_alias, eas_data, eas_data_length, on_server, &handle)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
        }
 
@@ -712,6 +799,7 @@ void stb_rename_mailbox_ex(HIPC_API a_hAPI)
        EM_SAFE_FREE(mailbox_path);
        EM_SAFE_FREE(eas_data);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -722,12 +810,19 @@ void stb_send_mail(HIPC_API a_hAPI)
        int mail_id;
        int handle;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* Mail_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
        EM_DEBUG_LOG("mail_id [%d]", mail_id);
 
-       if(emdaemon_send_mail(mail_id, &handle, &err)) {
+       if(emdaemon_send_mail(multi_user_name, mail_id, &handle, &err)) {
                err = EMAIL_ERROR_NONE;
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                        EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
@@ -745,6 +840,7 @@ void stb_send_mail(HIPC_API a_hAPI)
 
        EM_SAFE_FREE(local_stream);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -755,12 +851,19 @@ void stb_query_smtp_mail_size_limit(HIPC_API a_hAPI)
        int account_id;
        int handle;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* Mail_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
        EM_DEBUG_LOG("account_id [%d]", account_id);
 
-       if(emdaemon_query_smtp_mail_size_limit(account_id, &handle, &err)) {
+       if(emdaemon_query_smtp_mail_size_limit(multi_user_name, account_id, &handle, &err)) {
                err = EMAIL_ERROR_NONE;
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                        EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
@@ -775,6 +878,7 @@ void stb_query_smtp_mail_size_limit(HIPC_API a_hAPI)
                        EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -789,10 +893,17 @@ void stb_get_mailbox_list(HIPC_API a_hAPI)
        email_mailbox_t* mailbox_list = NULL;
        int count = 0;
        int size = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
 
-       if(emdaemon_get_mailbox_list(account_id, &mailbox_list, &count, &err))
+       if(emdaemon_get_mailbox_list(multi_user_name, account_id, &mailbox_list, &count, &err))
                EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -827,6 +938,7 @@ void stb_get_mailbox_list(HIPC_API a_hAPI)
 
        emcore_free_mailbox_list(&mailbox_list, count);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -836,6 +948,13 @@ void stb_delete_all_mails(HIPC_API a_hAPI)
        int mailbox_id = 0;
        int from_server = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
 
        /* mailbox_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
@@ -843,13 +962,14 @@ void stb_delete_all_mails(HIPC_API a_hAPI)
        /* from_server */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &from_server);
 
-       emdaemon_delete_mail_all(mailbox_id, from_server, NULL, &err);
+       emdaemon_delete_mail_all(multi_user_name, mailbox_id, from_server, NULL, &err);
 
        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(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -862,6 +982,13 @@ void stb_delete_mail(HIPC_API a_hAPI)
        int counter = 0;
        int num;
        int *mail_ids = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
 
        /* Mailbox */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
@@ -890,7 +1017,7 @@ 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(mailbox_id, mail_ids, num, from_server, NULL, &err);
+       emdaemon_delete_mail(multi_user_name, mailbox_id, mail_ids, num, from_server, NULL, &err);
 
 FINISH_OFF:
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -901,21 +1028,33 @@ FINISH_OFF:
 
        EM_SAFE_FREE(mail_ids);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
+
 void stb_clear_mail_data (HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
 
-       if(emdaemon_clear_all_mail_data(&err)) {
-               EM_DEBUG_LOG(">>> stb_clear_mail_data Success [ %d]  >> ", err);
-       }
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
+               EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+       if(emdaemon_clear_all_mail_data(multi_user_name, &err)) {
+               EM_DEBUG_LOG(">>> stb_clear_mail_data Success [ %d]  >> ", err);
+       }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -926,6 +1065,13 @@ void stb_add_rule(HIPC_API a_hAPI)
        int err = EMAIL_ERROR_NONE;
        char* local_rule_stream = NULL;
        email_rule_t rule = { 0 };
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
        if(buffer_size <= 0) {
@@ -942,7 +1088,7 @@ void stb_add_rule(HIPC_API a_hAPI)
        EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
 
        /* call add_filter handler */
-       err = emdaemon_add_filter(&rule);
+       err = emdaemon_add_filter(multi_user_name, &rule);
 
 FINISH_OFF:
 
@@ -955,6 +1101,7 @@ FINISH_OFF:
 
        emcore_free_rule(&rule);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -969,15 +1116,22 @@ void stb_get_rule(HIPC_API a_hAPI)
        email_rule_t* rule = NULL;
        int size =0;
        char* local_rule_stream = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        if ((ret_nth_value = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0))) {
                filter_id = *ret_nth_value;
 
-               emdaemon_get_filter(filter_id, &rule, &err);
+               emdaemon_get_filter(multi_user_name, filter_id, &rule, &err);
        } else {
-               err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
+               err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
        }
-               
+
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
 
@@ -1004,6 +1158,7 @@ void stb_get_rule(HIPC_API a_hAPI)
                return;
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1017,8 +1172,15 @@ void stb_get_rule_list(HIPC_API a_hAPI)
        int count = 0;
        int size = 0;
        email_rule_t* filtering_list = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
 
-       emdaemon_get_filter_list(&filtering_list, &count, &err);
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
+
+       emdaemon_get_filter_list(multi_user_name, &filtering_list, &count, &err);
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
@@ -1047,6 +1209,7 @@ void stb_get_rule_list(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1058,6 +1221,13 @@ void stb_find_rule(HIPC_API a_hAPI)
        int err = EMAIL_ERROR_NONE;
        char* local_rule_stream = NULL;
        email_rule_t rule = { 0 };
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
 
        buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
@@ -1071,7 +1241,7 @@ void stb_find_rule(HIPC_API a_hAPI)
                        EM_SAFE_FREE(local_rule_stream);
                        EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
 
-                       if(emdaemon_find_filter(&rule, &err))
+                       if(emdaemon_find_filter(multi_user_name, &rule, &err))
                                err = EMAIL_ERROR_NONE;
 
                        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1081,6 +1251,8 @@ void stb_find_rule(HIPC_API a_hAPI)
 
                }
        }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1094,7 +1266,13 @@ void stb_update_rule(HIPC_API a_hAPI)
        int *ret_nth_value = NULL;
        char* rule_stream = NULL;
        email_rule_t rule = {0};
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
 
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* get filter_id */
        if ((ret_nth_value = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0))) {
@@ -1119,7 +1297,7 @@ void stb_update_rule(HIPC_API a_hAPI)
        em_convert_byte_stream_to_rule(rule_stream, buffer_size, &rule);
 
        /* call update handler */
-       emdaemon_update_filter(filter_id, &rule, &err);
+       emdaemon_update_filter(multi_user_name, filter_id, &rule, &err);
 
 FINISH_OFF:
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1130,6 +1308,7 @@ FINISH_OFF:
 
        emcore_free_rule(&rule);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1138,6 +1317,13 @@ void stb_move_all_mails(HIPC_API a_hAPI)
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
        int src_mailbox_id = 0, dst_mailbox_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* src_mailbox_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &src_mailbox_id);
@@ -1157,7 +1343,7 @@ void stb_move_all_mails(HIPC_API a_hAPI)
        else
                EM_DEBUG_LOG("dst_mailbox_id == 0");
 
-       if(emdaemon_move_mail_all_mails(src_mailbox_id, dst_mailbox_id, &err))
+       if(emdaemon_move_mail_all_mails(multi_user_name, src_mailbox_id, dst_mailbox_id, &err))
                EM_DEBUG_LOG("emdaemon_move_mail_all_mails:Success");
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
                EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
@@ -1168,6 +1354,8 @@ void stb_move_all_mails(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
                return;
        }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1182,6 +1370,13 @@ void stb_set_flags_field(HIPC_API a_hAPI)
        int num = 0;
        int counter = 0;
        int *mail_ids = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -1217,7 +1412,7 @@ void stb_set_flags_field(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 5, sizeof(int), &onserver);
        EM_DEBUG_LOG("onserver [%d]", onserver);
 
-       if(emdaemon_set_flags_field(account_id, mail_ids, num, field_type, value, onserver, &err))
+       if(emdaemon_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, onserver, &err))
                EM_DEBUG_LOG("emdaemon_set_flags_field - success");
 
 FINISH_OFF:
@@ -1231,6 +1426,7 @@ FINISH_OFF:
        if (mail_ids)
                EM_SAFE_FREE(mail_ids);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1243,13 +1439,21 @@ void stb_add_mail(HIPC_API a_hAPI)
        int param_index = 0;
        int sync_server = 0;
        int err = EMAIL_ERROR_NONE;
-       int mdm_err = EMAIL_ERROR_NONE;
        int i = 0;
        email_mail_data_t result_mail_data = {0};
        email_attachment_data_t *result_attachment_data = NULL;
        email_meeting_request_t result_meeting_request = {0};
        emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
+       email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+    char *prefix_path = NULL;
+    char real_file_path[255] = {0};
 
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* email_mail_data_t */;
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
@@ -1261,25 +1465,50 @@ void stb_add_mail(HIPC_API a_hAPI)
                em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
        }
 
+       if (em_get_account_server_type_by_account_id(multi_user_name, result_mail_data.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;
+       }
+
+    /* Get the absolute path */
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        /* check smack rule for accessing file path */
-       if (result_mail_data.file_path_html) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_html)) {
+       if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_html) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
                }
        }
 
-       if (result_mail_data.file_path_plain) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_plain)) {
+       if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_plain) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
                }
        }
 
-       if (result_mail_data.file_path_mime_entity) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_mime_entity)) {
+       if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_mime_entity) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
@@ -1304,14 +1533,17 @@ void stb_add_mail(HIPC_API a_hAPI)
                                err = EMAIL_ERROR_ON_PARSING;
                                goto FINISH_OFF;
                        }
-                       param_index++;
                }
        }
+       param_index++;
 
        /* check smack rule for accessing file path */
        for (i = 0; i < result_attachment_data_count ; i++) {
-               if (result_attachment_data[i].attachment_path) {
-                       if (!emdaemon_check_smack_rule(api_info->response_id, result_attachment_data[i].attachment_path)) {
+               if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_attachment_data[i].attachment_path && result_attachment_data[i].save_status) {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
+
+                       if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                                EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                                err = EMAIL_ERROR_NO_SMACK_RULE;
                                goto FINISH_OFF;
@@ -1335,7 +1567,7 @@ void stb_add_mail(HIPC_API a_hAPI)
        EM_DEBUG_LOG("sync_server");
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, param_index++, sizeof(int), &sync_server);
 
-       if( (err = emdaemon_add_mail(&result_mail_data, result_attachment_data, result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
+       if( (err = emdaemon_add_mail(multi_user_name, &result_mail_data, result_attachment_data, result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emdaemon_add_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1358,6 +1590,8 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
        }
 
+       EM_SAFE_FREE(prefix_path);
+
        emcore_free_mail_data(&result_mail_data);
 
        if(result_attachment_data)
@@ -1367,6 +1601,7 @@ FINISH_OFF:
 
        em_flush_memory();
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1386,6 +1621,15 @@ void stb_update_mail(HIPC_API a_hAPI)
        email_attachment_data_t *result_attachment_data = NULL;
        email_meeting_request_t result_meeting_request = {0};
        emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+    char *prefix_path = NULL;
+    char real_file_path[255] = {0};
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        EM_DEBUG_LOG("email_mail_data_t");
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
@@ -1395,9 +1639,23 @@ void stb_update_mail(HIPC_API a_hAPI)
                em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
        }
 
+    /* Get the absolute path */
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        /* check smack rule for accessing file path */
        if (result_mail_data.file_path_html) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_html)) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
@@ -1405,7 +1663,10 @@ void stb_update_mail(HIPC_API a_hAPI)
        }
 
        if (result_mail_data.file_path_plain) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_plain)) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
@@ -1413,7 +1674,10 @@ void stb_update_mail(HIPC_API a_hAPI)
        }
 
        if (result_mail_data.file_path_mime_entity) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_mime_entity)) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
@@ -1434,14 +1698,17 @@ void stb_update_mail(HIPC_API a_hAPI)
                                err = EMAIL_ERROR_ON_PARSING;
                                goto FINISH_OFF;
                        }
-                       param_index++;
                }
        }
+       param_index++;
 
        /* check smack rule for accessing file path */
        for (i = 0; i < result_attachment_data_count ; i++) {
                if (result_attachment_data[i].attachment_path) {
-                       if (!emdaemon_check_smack_rule(api_info->response_id, result_attachment_data[i].attachment_path)) {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
+
+                       if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                                EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                                err = EMAIL_ERROR_NO_SMACK_RULE;
                                goto FINISH_OFF;
@@ -1473,7 +1740,7 @@ void stb_update_mail(HIPC_API a_hAPI)
 
        sync_server = *temp_buffer;
 
-       if( (err = emdaemon_update_mail(&result_mail_data, result_attachment_data,
+       if( (err = emdaemon_update_mail(multi_user_name, &result_mail_data, result_attachment_data,
                        result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emdaemon_update_mail failed [%d]", err);
                goto FINISH_OFF;
@@ -1499,12 +1766,16 @@ FINISH_OFF:
 
        emcore_free_mail_data(&result_mail_data);
 
+       EM_SAFE_FREE(prefix_path);
+
        if(result_attachment_data)
                emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
 
        emstorage_free_meeting_request(&result_meeting_request);
 
        em_flush_memory();
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1515,6 +1786,13 @@ void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
        int mailbox_id = 0, thread_id = 0, move_always_flag = 0;
        int err = EMAIL_ERROR_NONE;
        char *target_mailbox_name = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
        EM_DEBUG_LOG("thread_id [%d]", thread_id);
@@ -1525,7 +1803,7 @@ void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &move_always_flag);
        EM_DEBUG_LOG("move_always_flag [%d]", move_always_flag);
 
-       if(emdaemon_move_mail_thread_to_mailbox(thread_id, mailbox_id, move_always_flag, &err))
+       if(emdaemon_move_mail_thread_to_mailbox(multi_user_name, thread_id, mailbox_id, move_always_flag, &err))
                EM_DEBUG_LOG("emdaemon_move_mail_thread_to_mailbox success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
@@ -1541,6 +1819,7 @@ void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
        }
 
        EM_SAFE_FREE(target_mailbox_name);
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1551,6 +1830,13 @@ void stb_delete_thread(HIPC_API a_hAPI)
        int thread_id = 0, delete_always_flag = 0;
        int handle = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
        EM_DEBUG_LOG("thread_id [%d]", thread_id);
@@ -1558,7 +1844,7 @@ void stb_delete_thread(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &delete_always_flag);
        EM_DEBUG_LOG("delete_always_flag [%d]", delete_always_flag);
 
-       if(emdaemon_delete_mail_thread(thread_id, delete_always_flag, &handle, &err))
+       if(emdaemon_delete_mail_thread(multi_user_name, thread_id, delete_always_flag, &handle, &err))
                EM_DEBUG_LOG("emdaemon_delete_mail_thread success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
@@ -1570,6 +1856,8 @@ void stb_delete_thread(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
                return;
        }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1580,6 +1868,13 @@ void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
        int thread_id = 0, seen_flag = 0, on_server = 0;
        int handle = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
        EM_DEBUG_LOG("thread_id [%d]", thread_id);
@@ -1590,7 +1885,7 @@ void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &on_server);
        EM_DEBUG_LOG("on_server [%d]", on_server);
 
-       if(emdaemon_modify_seen_flag_of_thread(thread_id, seen_flag, on_server, &handle, &err))
+       if(emdaemon_modify_seen_flag_of_thread(multi_user_name, thread_id, seen_flag, on_server, &handle, &err))
                EM_DEBUG_LOG("emdaemon_modify_seen_flag_of_thread success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
@@ -1602,6 +1897,8 @@ void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
                return;
        }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1612,6 +1909,13 @@ void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
        int mailbox_id = 0, on_server = 0;
        int handle = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), (void*)&mailbox_id);
        EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
@@ -1619,7 +1923,7 @@ void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
        EM_DEBUG_LOG("on_server [%d]", on_server);
 
-       if( (err = emdaemon_expunge_mails_deleted_flagged(mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
+       if( (err = emdaemon_expunge_mails_deleted_flagged(multi_user_name, mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
@@ -1636,6 +1940,8 @@ void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
                return;
        }
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1643,6 +1949,13 @@ void stb_move_mail(HIPC_API a_hAPI)
 {
        int err = EMAIL_ERROR_NONE;
        int num = 0, counter = 0, mailbox_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* Number of mail_ids */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &num);
@@ -1664,7 +1977,7 @@ void stb_move_mail(HIPC_API a_hAPI)
        else
                EM_DEBUG_LOG("mailbox_id == 0");
 
-       if(emdaemon_move_mail(mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
+       if(emdaemon_move_mail(multi_user_name, mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
                EM_DEBUG_LOG("emdaemon_move_mail success");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1672,6 +1985,8 @@ void stb_move_mail(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1681,11 +1996,18 @@ void stb_delete_rule(HIPC_API a_hAPI)
 
        int filter_id = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* filter_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
 
-       if(emdaemon_delete_filter(filter_id, &err))
+       if(emdaemon_delete_filter(multi_user_name, filter_id, &err))
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1693,6 +2015,8 @@ void stb_delete_rule(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1702,11 +2026,18 @@ void stb_apply_rule(HIPC_API a_hAPI)
 
        int filter_id = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* filter_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
 
-       if(emdaemon_apply_filter(filter_id, &err))
+       if(emdaemon_apply_filter(multi_user_name, filter_id, &err))
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1714,6 +2045,8 @@ void stb_apply_rule(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1728,6 +2061,15 @@ void stb_add_attachment(HIPC_API a_hAPI)
        char* attachment_stream = NULL;
        email_attachment_data_t* attachment = NULL;
        emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+    char *prefix_path = NULL;
+    char real_file_path[255] = {0};
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* mail_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
@@ -1751,10 +2093,24 @@ void stb_add_attachment(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
+    /* Get the absolute path */
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
        /* check smack rule for accessing file path */
        for (i = 0; i < attachment_count ; i++) {
                if (attachment[i].attachment_path) {
-                       if (!emdaemon_check_smack_rule(api_info->response_id, attachment[i].attachment_path)) {
+            memset(real_file_path, 0x00, sizeof(real_file_path));
+            SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment[i].attachment_path);
+
+                       if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                                EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                                err = EMAIL_ERROR_NO_SMACK_RULE;
                                goto FINISH_OFF;
@@ -1762,7 +2118,7 @@ void stb_add_attachment(HIPC_API a_hAPI)
                }
        }
 
-       emdaemon_add_attachment(mail_id, attachment, &err);
+       emdaemon_add_attachment(multi_user_name, mail_id, attachment, &err);
 
 FINISH_OFF:
 
@@ -1778,6 +2134,9 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
        EM_SAFE_FREE(attachment);
+    EM_SAFE_FREE(multi_user_name);
+       EM_SAFE_FREE(prefix_path);
+
        EM_DEBUG_FUNC_END();
 }
 
@@ -1789,11 +2148,18 @@ void stb_get_attachment(HIPC_API a_hAPI)
        char* attachment_stream = NULL;
        email_attachment_data_t* attachment = NULL;
        int size = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* attachment_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
 
-       emdaemon_get_attachment(attachment_id, &attachment, &err);
+       emdaemon_get_attachment(multi_user_name, attachment_id, &attachment, &err);
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
@@ -1819,6 +2185,7 @@ void stb_get_attachment(HIPC_API a_hAPI)
        EM_SAFE_FREE(attachment_stream);
        emcore_free_attachment_data(&attachment, 1, &err);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1828,11 +2195,19 @@ void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
        int err = EMAIL_ERROR_NONE;
        int account_id = 0;
        int handle = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
 
-       if(emdaemon_get_imap_mailbox_list(account_id, "", &handle, &err))
+       /*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;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1841,6 +2216,8 @@ void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
                EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_LOG("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1849,17 +2226,25 @@ void stb_delete_attachment(HIPC_API a_hAPI)
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
        int attachment_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* attachment_index */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
 
-       emdaemon_delete_mail_attachment(attachment_id, &err);
+       emdaemon_delete_mail_attachment(multi_user_name, attachment_id, &err);
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1872,6 +2257,13 @@ void stb_download_attachment(HIPC_API a_hAPI)
        int nth = 0;
        int handle = 0;
        int account_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        EM_DEBUG_LOG("account_id");
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -1882,7 +2274,7 @@ void stb_download_attachment(HIPC_API a_hAPI)
        EM_DEBUG_LOG("nth");
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &nth);
 
-       if(emdaemon_download_attachment(account_id, mail_id, nth, &handle, &err)) {
+       if(emdaemon_download_attachment(multi_user_name, account_id, mail_id, nth, &handle, &err)) {
                err = EMAIL_ERROR_NONE;
 
                if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1904,6 +2296,7 @@ void stb_download_attachment(HIPC_API a_hAPI)
                        EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1912,12 +2305,19 @@ void stb_mail_send_saved(HIPC_API a_hAPI)
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
        int account_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
 
        EM_DEBUG_LOG("calling emdaemon_send_mail_saved");
-       if(emdaemon_send_mail_saved(account_id, NULL, &err))
+       if(emdaemon_send_mail_saved(multi_user_name, account_id, NULL, &err))
                err = EMAIL_ERROR_NONE;
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1926,6 +2326,7 @@ void stb_mail_send_saved(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1934,12 +2335,19 @@ void stb_add_read_receipt(HIPC_API a_hAPI){
        int read_mail_id = 0;
        int receipt_mail_id = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* read_mail_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &read_mail_id);
        EM_DEBUG_LOG("read_mail_id [%d]", read_mail_id);
 
-       if( (err = emcore_add_read_receipt(read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
+       if( (err = emcore_add_read_receipt(multi_user_name, read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
                EM_DEBUG_EXCEPTION("emcore_add_read_receipt failed [%d]", err);
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -1953,6 +2361,7 @@ void stb_add_read_receipt(HIPC_API a_hAPI){
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END("err [%d]", err);
 }
 
@@ -1963,6 +2372,13 @@ void stb_retry_sending_mail(HIPC_API a_hAPI)
        int mail_id = 0;
        int timeout_in_sec = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* Mail_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
@@ -1972,14 +2388,16 @@ void stb_retry_sending_mail(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &timeout_in_sec);
        EM_DEBUG_LOG("timeout_in_sec [%d]", timeout_in_sec);
 
-       if(emdaemon_send_mail_retry(mail_id, timeout_in_sec,&err))
+       if(emdaemon_send_mail_retry(multi_user_name, mail_id, timeout_in_sec,&err))
                EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
 
        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  ");
+    if (!emipc_execute_stub_api(a_hAPI))
+            EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -1989,6 +2407,13 @@ void stb_cancel_job(HIPC_API a_hAPI)
        int account_id = 0;
        int handle = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &handle);
@@ -2001,6 +2426,8 @@ void stb_cancel_job(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2010,6 +2437,13 @@ void stb_cancel_send_mail_job(HIPC_API a_hAPI)
        int mail_id = 0;
        int err = EMAIL_ERROR_NONE;
        int account_id = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -2027,113 +2461,11 @@ void stb_cancel_send_mail_job(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
-       EM_DEBUG_FUNC_END();
-}
-
-void stb_search_mail_on_server(HIPC_API a_hAPI)
-{
-       EM_DEBUG_FUNC_BEGIN();
-       int                    i = 0;
-       int                    err = EMAIL_ERROR_NONE;
-       int                    account_id = 0;
-       int                                        mailbox_id = 0;
-       int                    buffer_size = 0;
-       int                    search_filter_count = 0;
-       char                  *stream_for_search_filter_list = NULL;
-       unsigned int           job_handle = 0;
-       email_search_filter_t *search_filter_list = NULL;
-
-       /* account_id */
-       emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
-       EM_DEBUG_LOG("account_id [%d]", account_id);
-
-       /* mailbox_id */
-       emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
-       EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
-
-       /* search_filter_list */
-       buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
-
-       if(buffer_size > 0)      {
-               stream_for_search_filter_list = (char*)em_malloc(buffer_size);
-               EM_NULL_CHECK_FOR_VOID(stream_for_search_filter_list);
-               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_SAFE_FREE(stream_for_search_filter_list);
-       }
-
-       if(!emdaemon_search_mail_on_server(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)))
-               EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
-
-       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))
-               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
-
-       if(search_filter_list) {
-               for(i = 0; i < search_filter_count; i++) {
-                       switch(search_filter_list[i].search_filter_type) {
-                               case EMAIL_SEARCH_FILTER_TYPE_BCC              :
-                               case EMAIL_SEARCH_FILTER_TYPE_CC               :
-                               case EMAIL_SEARCH_FILTER_TYPE_FROM             :
-                               case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
-                               case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
-                               case EMAIL_SEARCH_FILTER_TYPE_TO               :
-                               case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
-                                       EM_SAFE_FREE(search_filter_list[i].search_filter_key_value.string_type_key_value);
-                                       break;
-                               default :
-                                       break;
-                       }
-               }
-       }
-
-       EM_SAFE_FREE(search_filter_list);
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
-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;
-
-       /* account_id */
-       emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
-
-       EM_DEBUG_LOG("account_id [%d]", account_id);
-
-       if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SEARCH_RESULT, &mailbox_tbl, true, &err)) {
-               EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
-               goto FINISH_OFF;
-       }
-
-       if (!emstorage_delete_mail_by_mailbox(account_id, mailbox_tbl->mailbox_id, true, &err))
-               EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
-
-FINISH_OFF:
-
-       if (mailbox_tbl) {
-               emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
-       }
-
-       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_DEBUG_FUNC_END();
-}
-
-
-
 void stb_add_account_with_validation(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -2146,6 +2478,13 @@ void stb_add_account_with_validation(HIPC_API a_hAPI)
        int ref_check_interval = 0;
        int ref_account_id = 0;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* get account info */
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
@@ -2170,13 +2509,14 @@ void stb_add_account_with_validation(HIPC_API a_hAPI)
        }
 
        em_convert_byte_stream_to_account(stream, buffer_size, account);
+    account->user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account->account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
@@ -2186,16 +2526,20 @@ void stb_add_account_with_validation(HIPC_API a_hAPI)
        ref_check_interval = ref_account->check_interval;
        ref_account_id     = ref_account->account_id;
 
-       if(!emdaemon_validate_account_and_create(ref_account, &handle, &err)) {
+       if(!emdaemon_validate_account_and_create(multi_user_name, ref_account, &handle, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_create fail [%d]", err);
                goto FINISH_OFF;
        }
 #ifdef __FEATURE_AUTO_POLLING__
        /*  start auto polling, if check_interval not zero */
-       if(ref_check_interval > 0 || (ref_account->peak_days && ref_account->peak_interval > 0)) {
-               if(!emdaemon_add_polling_alarm(ref_account_id))
+       if(ref_check_interval > 0 || (ref_account->peak_days > 0 && ref_account->peak_interval > 0)) {
+               if(!emdaemon_add_polling_alarm(multi_user_name, ref_account_id))
                        EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
        }
+#ifdef __FEATURE_IMAP_IDLE__
+       else if(ref_check_interval == 0 || (ref_account->peak_days > 0 && ref_account->peak_interval == 0))
+               emcore_refresh_imap_idle_thread();
+#endif /* __FEATURE_IMAP_IDLE__ */
 #endif /*  __FEATURE_AUTO_POLLING__ */
        /*  add account details to contact DB  */
        /*  emdaemon_insert_accountinfo_to_contact(account); */
@@ -2221,6 +2565,7 @@ FINISH_OFF:
 /*     note: account is freed in thread_func_branch_command, which is run by other thread */
 /*     emcore_free_account(account); */
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2231,6 +2576,13 @@ void stb_backup_account(HIPC_API a_hAPI)
 #ifdef __FEATURE_BACKUP_ACCOUNT__
        char *file_path = NULL;
        int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
+            multi_user_name = NULL;
+    }
 
        /* file_path_length */
        file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
@@ -2239,7 +2591,7 @@ void stb_backup_account(HIPC_API a_hAPI)
                file_path = em_malloc(file_path_length);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
                EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
-               local_result = emcore_backup_accounts((const char*)file_path, &err_code);
+               local_result = emcore_backup_accounts(multi_user_name, (const char*)file_path, &err_code);
        }
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
@@ -2255,6 +2607,7 @@ void stb_backup_account(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
        EM_SAFE_FREE(file_path);
+    EM_SAFE_FREE(multi_user_name);
 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
        EM_DEBUG_FUNC_END();
 }
@@ -2264,7 +2617,14 @@ void stb_restore_account(HIPC_API a_hAPI)
        EM_DEBUG_FUNC_BEGIN();
 #ifdef __FEATURE_BACKUP_ACCOUNT__
        char *file_path = NULL;
-       int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
+       int result = 0, err_code = 0, handle = 0, file_path_length = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
+            multi_user_name = NULL;
+    }
 
        /* file_path_length */
        file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
@@ -2273,22 +2633,27 @@ void stb_restore_account(HIPC_API a_hAPI)
                file_path = em_malloc(file_path_length);
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
                EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
-               local_result = emcore_restore_accounts((const char*)file_path, &err_code);
        }
 
-       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
+       /* file_path could be NULL */
+       err_code = emcore_restore_accounts(multi_user_name, (const char*)file_path);
+
+       result = (err_code == EMAIL_ERROR_NONE)?1:0;
+
+       if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result, sizeof(int)))
                EM_DEBUG_LOG("emipc_add_parameter failed ");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
+               EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
                EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
 
        if (!emipc_execute_stub_api(a_hAPI))
-               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
        EM_SAFE_FREE(file_path);
+    EM_SAFE_FREE(multi_user_name);
 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
        EM_DEBUG_FUNC_END();
 }
@@ -2302,12 +2667,19 @@ void stb_get_password_length(HIPC_API a_hAPI)
        int local_result = 0;
        int err_code = 0;
        int password_length = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
+            multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &password_type);
 
-       local_result = emstorage_get_password_length_of_account(account_id, password_type, &password_length,&err_code);
+       local_result = emstorage_get_password_length_of_account(multi_user_name, account_id, password_type, &password_length,&err_code);
 
        EM_DEBUG_LOG("password_length [%d]", password_length);
 
@@ -2322,6 +2694,8 @@ void stb_get_password_length(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2333,6 +2707,13 @@ void stb_get_task_information(HIPC_API a_hAPI)
        int stream_length;
        email_task_information_t *task_information = NULL;
        char *task_information_stream = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        err = emcore_get_task_information(&task_information, &task_information_count);
 
@@ -2356,6 +2737,7 @@ void stb_get_task_information(HIPC_API a_hAPI)
                EM_SAFE_FREE(task_information);
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2367,6 +2749,26 @@ void stb_add_certificate(HIPC_API a_hAPI)
        char *cert_file_path = NULL;
        char *email_address = NULL;
        emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+    char *prefix_path = NULL;
+    char real_file_path[255] = {0};
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
+
+    /* Get the absolute path */
+    if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               prefix_path = strdup("");
+       }
 
        cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
        if (cert_file_len > 0) {
@@ -2376,7 +2778,10 @@ void stb_add_certificate(HIPC_API a_hAPI)
 
        /* check smack rule for accessing file path */
        if (cert_file_path) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, cert_file_path)) {
+        memset(real_file_path, 0x00, sizeof(real_file_path));
+        SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, cert_file_path);
+
+               if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
                        EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
                        err = EMAIL_ERROR_NO_SMACK_RULE;
                        goto FINISH_OFF;
@@ -2389,7 +2794,7 @@ void stb_add_certificate(HIPC_API a_hAPI)
                emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, email_address_len, email_address);
        }
 
-       if (!emcore_add_public_certificate(cert_file_path, email_address, &err)) {
+       if (!emcore_add_public_certificate(multi_user_name, cert_file_path, email_address, &err)) {
                EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
        }
 
@@ -2405,8 +2810,10 @@ FINISH_OFF:
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+       EM_SAFE_FREE(prefix_path);
        EM_SAFE_FREE(cert_file_path);
        EM_SAFE_FREE(email_address);
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2416,6 +2823,13 @@ void stb_delete_certificate(HIPC_API a_hAPI)
        int email_address_len = 0;
        char *email_address = NULL;
        char temp_email_address[130] = {0, };
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
        if (email_address_len > 0) {
@@ -2426,7 +2840,7 @@ void stb_delete_certificate(HIPC_API a_hAPI)
        }
 
        SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
-       if (!emcore_delete_public_certificate(temp_email_address, &err)) {
+       if (!emcore_delete_public_certificate(multi_user_name, temp_email_address, &err)) {
                EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
        }
 
@@ -2442,6 +2856,7 @@ void stb_delete_certificate(HIPC_API a_hAPI)
 
        
        EM_SAFE_FREE(email_address);
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();    
 }
 
@@ -2454,6 +2869,13 @@ void stb_verify_signature(HIPC_API a_hAPI)
        int attachment_tbl_count = 0;
        email_mail_data_t *mail_data = NULL;
        emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        err = emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
        if (err != EMAIL_ERROR_NONE) {
@@ -2461,12 +2883,12 @@ void stb_verify_signature(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
-       if (!emcore_get_mail_data(mail_id, &mail_data)) {
+       if (!emcore_get_mail_data(multi_user_name, mail_id, &mail_data)) {
                EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_get_attachment_list(mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
+       if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed : [%d]", err);
                goto FINISH_OFF;
        }
@@ -2477,16 +2899,24 @@ void stb_verify_signature(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
-       for (count = 0; count < attachment_tbl_count; count++) {
-               if (strcasestr(attachment_tbl_list[count].attachment_name, "p7s") == NULL)
-                       continue;
-
-               break;  
+       for (count = 0; count < attachment_tbl_count ; count++) {
+               if (attachment_tbl_list[count].attachment_mime_type && strcasestr(attachment_tbl_list[count].attachment_mime_type, "SIGNATURE"))
+                       break;
        }
 
-       if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
-               goto FINISH_OFF;
+       if (mail_data->smime_type == EMAIL_SMIME_SIGNED) {
+               if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
+                       EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
+                       goto FINISH_OFF;
+               }
+       } else if (mail_data->smime_type == EMAIL_PGP_SIGNED) {
+               if ((err = emcore_pgp_get_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, mail_data->digest_type, &verify)) != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_signature failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+       } else {
+               EM_DEBUG_LOG("Invalid signed mail");
+               err = EMAIL_ERROR_INVALID_PARAM;
        }
 
 FINISH_OFF:
@@ -2510,6 +2940,7 @@ FINISH_OFF:
                EM_SAFE_FREE(mail_data);
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2519,6 +2950,13 @@ void stb_verify_certificate(HIPC_API a_hAPI)
        int verify = 0;
        int cert_file_len = 0;
        char *cert_file_path = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
        if (cert_file_len > 0) {
@@ -2541,6 +2979,7 @@ void stb_verify_certificate(HIPC_API a_hAPI)
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
        EM_SAFE_FREE(cert_file_path);           
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();    
 }
 
@@ -2563,6 +3002,15 @@ void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE, account_id;
        int total_mail_count = 0, unread_mail_count = 0;
+       int input_from_eas = 0;
+    /* Get Container name */
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* account_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
@@ -2574,7 +3022,12 @@ void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &unread_mail_count);
        EM_DEBUG_LOG("unread_mail_count [%d]", unread_mail_count);
 
-       if(!emdaemon_finalize_sync(account_id, total_mail_count, unread_mail_count, &err)) {
+       emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &input_from_eas);
+       EM_DEBUG_LOG("unread_mail_count [%d]", input_from_eas);
+
+       emcore_display_unread_in_badge(multi_user_name);
+
+       if(!emdaemon_finalize_sync(multi_user_name, account_id, total_mail_count, unread_mail_count, 0, 0, input_from_eas, &err)) {
                EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
        }
 
@@ -2583,6 +3036,37 @@ void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
 
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
+
+    EM_SAFE_FREE(multi_user_name);
+       EM_DEBUG_FUNC_END();
+}
+
+void stb_clear_notification_bar(HIPC_API a_hAPI)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE, account_id;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
+
+       /* account_id */
+       emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
+       EM_DEBUG_LOG("account_id [%d]", account_id);
+
+       if((err = emcore_clear_notifications(multi_user_name, account_id)) != EMAIL_ERROR_NONE) 
+               EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
+
+       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(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
 
@@ -2593,6 +3077,13 @@ void stb_show_user_message(HIPC_API a_hAPI)
        int param_id = 0;
        int param_error = 0;
        email_action_t param_action = 0;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* param_id */
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &param_id);
@@ -2606,8 +3097,8 @@ void stb_show_user_message(HIPC_API a_hAPI)
        emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &param_error);
        EM_DEBUG_LOG("param_error [%d]", param_error);
 
-       if( (err = emcore_show_user_message(param_id, param_action, param_error)) != EMAIL_ERROR_NONE) {
-               EM_DEBUG_EXCEPTION("emcore_show_user_message failed [%d]", err);
+       if(!emcore_show_user_message(multi_user_name, param_id, param_action, param_error)) {
+               EM_DEBUG_EXCEPTION("emcore_show_user_message failed");
        }
 
        if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
@@ -2616,185 +3107,21 @@ void stb_show_user_message(HIPC_API a_hAPI)
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END("err [%d]", err);
 }
 
 void stb_write_mime_file(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
-       int i = 0;
-       int exist_file_path = 0;
-       int buffer_size = 0;
-       int local_result = 0;
-       int result_attachment_data_count = 0;
-       int param_index = 0;
        int err = EMAIL_ERROR_NONE;
-       int ret = EMAIL_ERROR_NONE;
-       char *file_path = NULL;
-       char *output_file_path = NULL;
-       email_mail_data_t result_mail_data = {0};
-       email_attachment_data_t *result_attachment_data = NULL;
-       emstorage_mail_tbl_t *input_mail_tbl_data = NULL;
-       emstorage_attachment_tbl_t *input_attachment_tbl_data = NULL;
-       emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
-       
-       /* email_mail_data_t */;
-       buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
-       EM_DEBUG_LOG("email_mail_data_t buffer_size[%d]", buffer_size);
-
-       /* mail_data */
-       if(buffer_size > 0)      {
-               char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
-               em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
-       }
-
-       /* check smack rule for accessing file path */
-       if (result_mail_data.file_path_html) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_html)) {
-                       EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
-                       err = EMAIL_ERROR_NO_SMACK_RULE;
-                       goto FINISH_OFF;
-               }
-       }
-
-       if (result_mail_data.file_path_plain) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_plain)) {
-                       EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
-                       err = EMAIL_ERROR_NO_SMACK_RULE;
-                       goto FINISH_OFF;
-               }
-       }
-
-       if (result_mail_data.file_path_mime_entity) {
-               if (!emdaemon_check_smack_rule(api_info->response_id, result_mail_data.file_path_mime_entity)) {
-                       EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
-                       err = EMAIL_ERROR_NO_SMACK_RULE;
-                       goto FINISH_OFF;
-               }
-       }
-       /* check smack rule - END */
 
-       /* attachment */
-       buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
-       EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
-
-       if(buffer_size > 0) {
-               char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
-               em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
-
-               EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
-               if (!result_attachment_data_count) {
-                       EM_DEBUG_LOG("Not include attachment data");
-               } else {
-                       if(!result_attachment_data) {
-                               EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
-                               err = EMAIL_ERROR_ON_PARSING;
-                               goto FINISH_OFF;
-                       }
-                       param_index++;
-               }
-       }
-
-       /* check smack rule for accessing file path */
-       for (i = 0; i < result_attachment_data_count ; i++) {
-               if (result_attachment_data[i].attachment_path && result_attachment_data[i].save_status) {
-                       if (!emdaemon_check_smack_rule(api_info->response_id, result_attachment_data[i].attachment_path)) {
-                               EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
-                               err = EMAIL_ERROR_NO_SMACK_RULE;
-                               goto FINISH_OFF;
-                       }
-               }
-       }
-
-       buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
-       if (buffer_size > 0) {
-               output_file_path = (char *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
-               EM_DEBUG_LOG_SEC("output_file_path : [%s]", output_file_path);
-       }
-
-       /* Convert mail_data to mail_tbl */
-       if (!em_convert_mail_data_to_mail_tbl(&result_mail_data, 1, &input_mail_tbl_data, &err)) {
-               EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed");
-               goto FINISH_OFF;
-       }
-
-       /* Convert attachment_data to attachment_tbl */
-       if (result_attachment_data_count > 0) {
-               input_attachment_tbl_data = (emstorage_attachment_tbl_t *)em_malloc(sizeof(emstorage_attachment_tbl_t) * result_attachment_data_count);
-               if (input_attachment_tbl_data == NULL) {
-                       EM_DEBUG_EXCEPTION("em_malloc failed");
-                       err = EMAIL_ERROR_OUT_OF_MEMORY;
-                       goto FINISH_OFF;
-               }
-       }
-
-       for (i = 0; i < result_attachment_data_count ; i++) {
-               input_attachment_tbl_data[i].attachment_id = result_attachment_data[i].attachment_id;
-               input_attachment_tbl_data[i].attachment_name = EM_SAFE_STRDUP(result_attachment_data[i].attachment_name);
-               input_attachment_tbl_data[i].attachment_path = EM_SAFE_STRDUP(result_attachment_data[i].attachment_path);
-               input_attachment_tbl_data[i].attachment_size = result_attachment_data[i].attachment_size;
-               input_attachment_tbl_data[i].mail_id = result_attachment_data[i].mail_id;
-               input_attachment_tbl_data[i].account_id = result_attachment_data[i].account_id;
-               input_attachment_tbl_data[i].mailbox_id = result_attachment_data[i].mailbox_id;
-               input_attachment_tbl_data[i].attachment_save_status = result_attachment_data[i].save_status;
-               input_attachment_tbl_data[i].attachment_drm_type = result_attachment_data[i].drm_status;
-               input_attachment_tbl_data[i].attachment_inline_content_status = result_attachment_data[i].inline_content_status;
-               input_attachment_tbl_data[i].attachment_mime_type = EM_SAFE_STRDUP(result_attachment_data[i].attachment_mime_type);
-       }
-
-       if (!emcore_make_rfc822_file_from_mail(input_mail_tbl_data, input_attachment_tbl_data, result_attachment_data_count, NULL, &file_path, NULL, &err)) {
-               EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed");
-               goto FINISH_OFF;
-       }
-
-       if (output_file_path) {
-               if (!emstorage_move_file(file_path, output_file_path, false, &err)) {
-                       EM_DEBUG_EXCEPTION("emstorage_move_file failed : [%d]", err);
-                       goto FINISH_OFF;
-               }
-       } else {
-               exist_file_path = 1;
-               output_file_path = EM_SAFE_STRDUP(file_path);
-       }
-
-       if (!emdaemon_set_smack_label(output_file_path, "system::media")) {
-               EM_DEBUG_EXCEPTION("emdaemon_set_smack_label failed");
-       }
-
-       local_result = 1;
-
-FINISH_OFF:
-       if ( local_result == 0 ) {
-               if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
-       } else {
-               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, (char *)output_file_path, EM_SAFE_STRLEN(output_file_path) + 1)) {
-                       EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
-                       err = EMAIL_ERROR_NULL_VALUE;
-                       goto 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");
-
-       emstorage_free_attachment(&input_attachment_tbl_data, result_attachment_data_count, &ret);
-
-       emstorage_free_mail(&input_mail_tbl_data, 1, &ret);
-
-       emcore_free_mail_data(&result_mail_data);
-
-       if(result_attachment_data)
-               emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
-
-       EM_SAFE_FREE(file_path);
-       if (exist_file_path)
-               EM_SAFE_FREE(output_file_path);
+               EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
 
-       em_flush_memory();
        EM_DEBUG_FUNC_END();
 }
 
@@ -2809,6 +3136,13 @@ void stb_validate_account_ex(HIPC_API a_hAPI)
        email_account_t *account = NULL;
        email_account_t *ref_account = NULL;
        int err = EMAIL_ERROR_NONE;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* get account info */
        buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
@@ -2833,13 +3167,14 @@ void stb_validate_account_ex(HIPC_API a_hAPI)
        }
 
        em_convert_byte_stream_to_account(stream, buffer_size, account);
+    account->user_name = EM_SAFE_STRDUP(multi_user_name);
 
        if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
                goto FINISH_OFF;
        }
 
-       ref_account = emcore_get_account_reference(account->account_id);
+       ref_account = emcore_get_account_reference(multi_user_name, account->account_id);
 
        if (!ref_account) {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
@@ -2847,7 +3182,7 @@ void stb_validate_account_ex(HIPC_API a_hAPI)
        }
 
        /* ref_account will be removed by worker_event_queue() */
-       if((err = emdaemon_validate_account_ex(ref_account, &handle)) != EMAIL_ERROR_NONE) {
+       if((err = emdaemon_validate_account_ex(multi_user_name, ref_account, &handle)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emdaemon_validate_account_ex fail [%d]", err);
                goto FINISH_OFF;
        }
@@ -2870,8 +3205,10 @@ FINISH_OFF:
                        EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
        }
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END();
 }
+
 void stb_handle_task(int task_type, HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -2893,7 +3230,7 @@ void stb_handle_task(int task_type, HIPC_API a_hAPI)
 
        if(is_async_task) {
                /* add async task */
-               if((err = emcore_add_task_to_task_table(task_type, EMAIL_TASK_PRIORITY_MID, task_parameter, task_parameter_length, &task_id)) != EMAIL_ERROR_NONE) {
+               if((err = emcore_add_task_to_task_table(NULL, task_type, EMAIL_TASK_PRIORITY_MID, task_parameter, task_parameter_length, &task_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_add_task_to_task_pool failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -2944,7 +3281,7 @@ void* thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void
                EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
 
        for(i = 0; i < task_param->mail_id_count; i++) {
-               if((err = emcore_move_mail_to_another_account(task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {
+               if((err = emcore_move_mail_to_another_account(task_param->multi_user_name, task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {
                        EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);
                        goto FINISH_OFF;
                }
@@ -2966,9 +3303,11 @@ FINISH_OFF:
        }
 
        /* Free task parameter */
+        EM_SAFE_FREE(task_param->multi_user_name);
        EM_SAFE_FREE(task_param->mail_id_array);
        EM_SAFE_FREE(task_param);
 
+       emcore_close_recv_stream_list();
        EM_DEBUG_FUNC_END("err [%d]", err);
        return SUCCESS;
 }
@@ -2984,6 +3323,14 @@ void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
        char *task_parameter = NULL;
        task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *decoded_parameter = NULL;
 
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+            multi_user_name = NULL;
+    }
+
        /* task_parameter_length */;
        task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
 
@@ -3001,6 +3348,8 @@ void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
+    decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
        THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, (void*)decoded_parameter, thread_error);
 
        if(thread_error != 0) {
@@ -3021,6 +3370,8 @@ FINISH_OFF:
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+    EM_SAFE_FREE(multi_user_name);
+
        EM_DEBUG_FUNC_END("err [%d]", err);
 }
 
@@ -3034,7 +3385,7 @@ void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
        email_account_t* ref_account = NULL;
        task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *task_param = input_param;
 
-       ref_account = emcore_get_account_reference(task_param->account_id);
+       ref_account = emcore_get_account_reference(task_param->multi_user_name, task_param->account_id);
 
        if (!ref_account)  {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", task_param->account_id);
@@ -3051,7 +3402,7 @@ void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
        if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
 
-       if((err = emcore_delete_mailbox_ex(task_param->account_id, task_param->mailbox_id_array, task_param->mailbox_id_count, task_param->on_server, recursive)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_delete_mailbox_ex(task_param->multi_user_name, task_param->account_id, task_param->mailbox_id_array, task_param->mailbox_id_count, task_param->on_server, recursive)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_delete_mailbox_ex failed[%d]", err);
                goto FINISH_OFF;
        }
@@ -3074,6 +3425,7 @@ FINISH_OFF:
 
        /* Free task parameter */
        EM_SAFE_FREE(task_param->mailbox_id_array);
+    EM_SAFE_FREE(task_param->multi_user_name);
        EM_SAFE_FREE(task_param);
 
        EM_DEBUG_FUNC_END("err [%d]", err);
@@ -3090,6 +3442,13 @@ void stb_delete_mailbox_ex(HIPC_API a_hAPI)
        thread_t task_id;
        char *task_parameter = NULL;
        task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *decoded_parameter = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* task_parameter_length */;
        task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
@@ -3108,6 +3467,8 @@ void stb_delete_mailbox_ex(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
+    decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
        THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, (void*)decoded_parameter, thread_error);
 
        if(thread_error != 0) {
@@ -3128,6 +3489,7 @@ FINISH_OFF:
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END("err [%d]", err);
 }
 
@@ -3143,7 +3505,7 @@ void* thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIG
        if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
                EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
 
-       if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->mail_id)) != EMAIL_ERROR_NONE) {
+       if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->multi_user_name, task_param->mail_id)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_send_mail_with_downloading_attachment_of_original_mail failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -3160,8 +3522,9 @@ FINISH_OFF:
        }
 
        /* Free task parameter */
+    EM_SAFE_FREE(task_param->multi_user_name);
        EM_SAFE_FREE(task_param);
-
+       emcore_close_smtp_stream_list();
        EM_DEBUG_FUNC_END("err [%d]", err);
        return SUCCESS;
 }
@@ -3176,6 +3539,13 @@ void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
        thread_t task_id;
        char *task_parameter = NULL;
        task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *decoded_parameter = NULL;
+    int nAPPID = emipc_get_app_id(a_hAPI);
+    char *multi_user_name = NULL;
+
+    if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+        multi_user_name = NULL;
+    }
 
        /* task_parameter_length */;
        task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
@@ -3194,6 +3564,8 @@ void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
                goto FINISH_OFF;
        }
 
+    decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
+
        THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, (void*)decoded_parameter, thread_error);
 
        if(thread_error != 0) {
@@ -3214,30 +3586,49 @@ FINISH_OFF:
        if (!emipc_execute_stub_api(a_hAPI))
                EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
 
+    EM_SAFE_FREE(multi_user_name);
        EM_DEBUG_FUNC_END("err [%d]", err);
 }
 
+void stb_get_user_name(HIPC_API a_hAPI)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+    int err = EMAIL_ERROR_NONE;
+    int pid = 0;
+
+    char *user_name = NULL;
+
+    pid = emipc_get_app_id(a_hAPI);
+    EM_DEBUG_LOG("Peer PID : [%d]", pid);
+
+    if (pid > 0) {
+        if ((err = emcore_get_user_name(pid, &user_name)) != EMAIL_ERROR_NONE) 
+            EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
+
+        EM_DEBUG_LOG("Domain name : [%s]", user_name);
+
+        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, user_name, EM_SAFE_STRLEN(user_name) + 1)) 
+            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(user_name);
+
+    EM_DEBUG_FUNC_END();
+}
+
 void stb_API_mapper(HIPC_API a_hAPI)
 {
        EM_DEBUG_FUNC_BEGIN();
        int err = EMAIL_ERROR_NONE;
        int nAPIID = emipc_get_api_id(a_hAPI);
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       int nAPPID = emipc_get_app_id(a_hAPI);
-               err = em_check_socket_privilege_by_pid(nAPPID);
-               if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-                       EM_DEBUG_LOG("permission denied");
-                       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");
-
-                       return;
-               }
-#endif
-
        switch(nAPIID) {
                case _EMAIL_API_ADD_ACCOUNT:
                        stb_create_account(a_hAPI);
@@ -3423,14 +3814,6 @@ void stb_API_mapper(HIPC_API a_hAPI)
                        stb_cancel_send_mail_job(a_hAPI);
                        break;
 
-               case _EMAIL_API_SEARCH_MAIL_ON_SERVER :
-                       stb_search_mail_on_server(a_hAPI);
-                       break;
-
-               case _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER :
-                       stb_clear_result_of_search_mail_on_server(a_hAPI);
-                       break;
-
                case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :
                        stb_add_account_with_validation(a_hAPI);
                        break;
@@ -3475,6 +3858,10 @@ void stb_API_mapper(HIPC_API a_hAPI)
                        stb_update_notification_bar_for_unread_mail(a_hAPI);
                        break;
 
+               case _EMAIL_API_CLEAR_NOTIFICATION_BAR :
+                       stb_clear_notification_bar(a_hAPI);
+                       break;
+
                case _EMAIL_API_SHOW_USER_MESSAGE :
                        stb_show_user_message(a_hAPI);
                        break;
@@ -3509,6 +3896,62 @@ void stb_API_mapper(HIPC_API a_hAPI)
 
 GMainLoop *g_mainloop = NULL;
 
+/* this func should be called in main_loop */
+INTERNAL_FUNC int kill_daemon_if_no_account()
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       email_account_t *account_list = NULL;
+       int account_count = 0;
+    int total_account_count = 0;
+    GList *node = NULL;
+    GList *zone_name_list = NULL;
+
+    /* each container check the account */
+    if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
+    }
+
+    if (err != EMAIL_ERROR_NONE) {
+        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);
+        }
+
+        if (account_list)
+            emcore_free_account_list(&account_list, account_count, NULL);
+
+        total_account_count += account_count;
+    } else {
+        node = g_list_first(zone_name_list);
+        while (node != NULL) {
+            if (!node->data)
+                node = g_list_next(node);
+
+            if ((err = emcore_get_account_reference_list(node->data, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
+                EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
+            }
+
+            if (account_list)
+                emcore_free_account_list(&account_list, account_count, NULL);
+
+            total_account_count += account_count;
+            EM_SAFE_FREE(node->data);
+            node = g_list_next(node);
+        }
+        g_list_free(zone_name_list);
+    }
+
+       EM_DEBUG_LOG("account_count [%d]", total_account_count);
+
+       if (total_account_count == 0) {
+               EM_DEBUG_LOG("email-service is going to shutdown");
+               g_main_loop_quit (g_mainloop);
+       }
+
+       EM_DEBUG_FUNC_END("err [%d]", err);
+       return err;
+}
+
 static void callback_for_sigterm(int signum)
 {
        EM_DEBUG_FUNC_BEGIN("signum [%d]", signum);
@@ -3519,12 +3962,14 @@ static void callback_for_sigterm(int signum)
        EM_DEBUG_FUNC_END();
 }
 
-gboolean callback_for_timeout(gpointer user_data)
+extern int launch_pid;
+
+gboolean callback_for_shutdown(gpointer user_data)
 {
        EM_DEBUG_FUNC_BEGIN("user_data[%p]", user_data);
 
-       if(emipc_get_launch_method() != EMAIL_LAUNCHED_BY_DBUS_ACTIVATION) {
-               emdaemon_kill_daemon_if_no_account();
+       if (launch_pid == 0) {
+               kill_daemon_if_no_account();
        }
 
        EM_DEBUG_FUNC_END();
@@ -3547,25 +3992,121 @@ gboolean callback_for_del_account (GIOChannel *ch, GIOCondition cond, gpointer d
        }
        EM_DEBUG_LOG ("callback_for_del_account called file_del[%d] db_del[%d]", file_del, db_del);
 
-       /* if called twice, process termination begins. 
+       /* if called twice, process termination begins.
           two threads should complete the delete task */
        if (file_del && db_del) {
-               emdaemon_kill_daemon_if_no_account();
+               kill_daemon_if_no_account();
                file_del = db_del = 0; /* if there is an account, reset status */
        }
        return TRUE;
 }
 
+/* gdbus variables */
+static GDBusNodeInfo *introspection_data = NULL;
+extern gchar introspection_xml[];
+
+static void
+handle_method_call (GDBusConnection         *connection,
+                      const gchar           *sender,
+                      const gchar           *object_path,
+                      const gchar           *interface_name,
+                      const gchar           *method_name,
+                      GVariant              *parameters,
+                      GDBusMethodInvocation *invocation,
+                      gpointer               user_data)
+{
+       /* called by emipc_launch_email_service */
+       if (g_strcmp0 (method_name, "Launch") == 0) {
+               int caller_pid = 0;
+               g_variant_get (parameters, "(i)", &caller_pid);
+
+               launch_pid = caller_pid;
+               g_dbus_method_invocation_return_value (invocation, g_variant_new ("(i)", EMAIL_ERROR_NONE));
+               EM_DEBUG_LOG ("email-service launched by pid [%d]", caller_pid);
+       }
+/*
+       else if (g_strcmp0 (method_name, "SetContactsLog") == 0) {
+               GVariant* ret = em_gdbus_set_contact_log(parameters);
+               g_dbus_method_invocation_return_value (invocation, ret);
+       }
+       else if (g_strcmp0 (method_name, "DeleteContactsLog") == 0) {
+               GVariant* ret = em_gdbus_delete_contact_log(parameters);
+               g_dbus_method_invocation_return_value (invocation, ret);
+       }
+*/
+       else if (g_strcmp0 (method_name, "GetDisplayName") == 0) {
+               GVariant* ret = em_gdbus_get_display_name(parameters);
+               g_dbus_method_invocation_return_value (invocation, ret);
+       }
+       else if (g_strcmp0 (method_name, "CheckBlockingMode") == 0) {
+               GVariant* ret = em_gdbus_check_blocking_mode(parameters);
+               g_dbus_method_invocation_return_value (invocation, ret);
+       }
+}
+
+static const
+GDBusInterfaceVTable interface_vtable =
+{
+       handle_method_call,
+       NULL,
+       NULL
+};
+
+static void
+on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+       EM_DEBUG_LOG ("on_bus_acquired begin");
+
+       guint reg_id;
+       GError *error = NULL;
+
+       reg_id = g_dbus_connection_register_object (connection,
+                                             EMAIL_SERVICE_PATH,
+                                             introspection_data->interfaces[0],
+                                             &interface_vtable,
+                                             NULL,  /* user_data */
+                                             NULL,  /* user_data_free_func */
+                                             &error); /* GError** */
+       if (reg_id == 0) {
+               EM_DEBUG_EXCEPTION ("g_dbus_connection_register_object error[%s]",error->message);
+               g_error_free (error);
+       }
+       EM_DEBUG_LOG ("on_bus_acquired end [%d]", reg_id);
+}
+
+static void on_name_acquired (GDBusConnection *connection,
+                               const gchar *name,
+                               gpointer     user_data)
+{
+       EM_DEBUG_LOG ("on_name_acquired [%s]", name);
+}
+
+static void on_name_lost (GDBusConnection *connection,
+                           const gchar *name,
+                           gpointer     user_data)
+{
+       EM_DEBUG_EXCEPTION ("on_name_lost [%p] [%s]", connection, name);
+}
+
 INTERNAL_FUNC int main(int argc, char *argv[])
 {
-       /* Do the email-service Initialization 
+       /* Do the email-service Initialization
        1. Create all DB tables and load the email engine */
        EM_DEBUG_LOG("Email service begin");
        int err = 0, ret;
-       GMainLoop *mainloop;
+       GMainLoop *mainloop = NULL;
+       int owner_id = 0;
+    GList *node = NULL;
+    GList *zone_name_list = NULL;
+
+    /* Init container for daemon */
+    emcore_create_container();
+
+       /* Init cynara */
+       //emcore_init_cynara();
 
        EM_DEBUG_LOG("ipcEmailStub_Initialize Start");
-       
+
        ret = emipc_initialize_stub(stb_API_mapper);
 
        if(ret == true)
@@ -3576,28 +4117,87 @@ INTERNAL_FUNC int main(int argc, char *argv[])
        signal(SIGPIPE, SIG_IGN);              /* to ignore signal 13(SIGPIPE) */
        signal(SIGTERM, callback_for_sigterm); /* to handle signal 15(SIGTERM) - power off case*/
 
-       emdaemon_initialize(&err);
-
-       /* Start auto polling */
-#ifdef __FEATURE_AUTO_POLLING__
-       emdaemon_start_auto_polling(&err);
-#endif
+       emcore_gmime_init();
+
+    /* each container initialize */
+    if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
+        EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
+    }
+
+    if (err != EMAIL_ERROR_NONE) {
+        emdaemon_initialize(NULL, &err);
+        emcore_connect_contacts_service(NULL);
+    } else {
+        node = g_list_first(zone_name_list);
+        while (node != NULL) {
+            if (!node->data)
+                node = g_list_next(node);
+
+            emdaemon_initialize(node->data, &err);
+                       if (EM_SAFE_STRLEN(node->data) > 0)
+                               emcore_connect_contacts_service(node->data);
+
+            EM_SAFE_FREE(node->data);
+            node = g_list_next(node);
+        }
+        g_list_free(zone_name_list);
+    }
+
+       g_timeout_add(5000, callback_for_shutdown, NULL);
+
+       /* pipe between main and del account thread */
+       int *pipefd = emcore_init_pipe_for_del_account ();
+       /* main loop uses IO channel for listening an event */
+       GIOChannel *ch = g_io_channel_unix_new (pipefd[0]);
+       /* non-blocking mode */
+       g_io_channel_set_flags (ch, g_io_channel_get_flags (ch) | G_IO_FLAG_NONBLOCK, NULL);
+       /* main loop watches the IO, and call the cb when data is ready */
+       g_io_add_watch (ch, G_IO_IN, &callback_for_del_account, NULL);
+
+       EM_DEBUG_LOG ("main pipe[%d][%d]", pipefd[0], pipefd[1]);
+
+    /* Bind container for modified domain */
+    emcore_bind_vsm_context();
+
+       /* gdbus setup */
+       GError *error = NULL;
+       introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
+       if (!introspection_data) {
+               /* introspection_xml may be invalid */
+               EM_DEBUG_EXCEPTION ("g_dbus_node_info_new_for_xml error [%s]", error->message);
+               g_error_free (error);
+       }
+
+       owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                EMAIL_SERVICE_NAME,
+                                G_BUS_NAME_OWNER_FLAGS_NONE,
+                                on_bus_acquired,
+                                on_name_acquired,
+                                on_name_lost,
+                                NULL,
+                                NULL);
+       if (!owner_id) {
+               EM_DEBUG_EXCEPTION ("g_bus_own_name error");
+       }
+       EM_DEBUG_LOG ("owner_id [%d]", owner_id);
 
        mainloop = g_main_loop_new(NULL, 0);
        g_mainloop = mainloop;
 
-       g_type_init();
-
        g_main_loop_run(mainloop);
-       
+
        /* Clean up resources */
+       g_bus_unown_name (owner_id);
        g_main_loop_unref(mainloop);
        g_mainloop = NULL;
 
        emipc_finalize_stub();
        emdaemon_finalize(NULL);
+       emcore_gmime_shutdown();
 
+       EM_DEBUG_LOG ("Goodbye, world");
        EM_DEBUG_FUNC_END();
+       exit(44); /* exit with exit code 44 to prevent restarting */
        return 0;
 }
 
index 2baadd4..796ceb8 100755 (executable)
@@ -11,6 +11,8 @@ MESSAGE("")
 MESSAGE(">>> current directory: ${CMAKE_CURRENT_SOURCE_DIR}")
 MESSAGE(">>> Build type: ${CMAKE_BUILD_TYPE}")
 
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
+
 SET(IPC-LIB "email-ipc")
 SET(IPC-SRCS
        ${CMAKE_SOURCE_DIR}/email-ipc/email-ipc-api.c
@@ -46,7 +48,7 @@ INCLUDE_DIRECTORIES(
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(ipc_pkgs REQUIRED dlog dbus-1 glib-2.0 libsystemd-daemon)
+pkg_check_modules(ipc_pkgs REQUIRED dlog dbus-1 glib-2.0 libsystemd-daemon contacts-service2)
 
 FOREACH(flag ${ipc_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index e0438db..cbdd002 100755 (executable)
-/*\r
-*  email-service\r
-*\r
-* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.\r
-*\r
-* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>\r
-*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*\r
-*/\r
-\r
-#include <glib.h>\r
-#include <pthread.h>\r
-#include <dbus/dbus-glib.h>\r
-#include <dbus/dbus-glib-bindings.h>\r
-#include <sys/utsname.h>\r
-#include "email-types.h"\r
-#include "email-debug-log.h"\r
-#include "email-dbus-activation.h"\r
-#include "email-daemon-init.h"\r
-#include "email-service-binding.h"\r
-#include "email-service-glue.h"\r
-#include <unistd.h>\r
-\r
-\r
-G_DEFINE_TYPE(EmailService, email_service, G_TYPE_OBJECT)\r
-\r
-int _launch_method = EMAIL_LAUNCHED_BY_UNKNOWN_METHOD;\r
-\r
-EXPORT_API void emipc_set_launch_method(int input_launch_method)\r
-{\r
-       EM_DEBUG_LOG("input_launch_method [%d]", input_launch_method);\r
-       _launch_method = input_launch_method;\r
-}\r
-\r
-EXPORT_API int emipc_get_launch_method()\r
-{\r
-       EM_DEBUG_LOG("_launch_method[%d]", _launch_method);\r
-       return _launch_method;\r
-}\r
-\r
-static void email_service_init(EmailService *email_service)\r
-{\r
-       EM_DEBUG_LOG("email_service_init entered");\r
-}\r
-\r
-static void email_service_class_init(EmailServiceClass *email_service_class)\r
-{\r
-       EM_DEBUG_LOG("email_service_class_init entered");\r
-\r
-       dbus_g_object_type_install_info(EMAIL_SERVICE_TYPE, &dbus_glib_email_service_object_info);\r
-}\r
-\r
-gboolean daemon_launched = false;\r
-\r
-gboolean callback_for_timeout (void *arg)\r
-{\r
-       if (!daemon_launched)\r
-               EM_DEBUG_EXCEPTION ("org_tizen_email_service_launch is not responding");\r
-       return FALSE;\r
-}\r
-\r
-EXPORT_API int emipc_launch_email_service()\r
-{\r
-       EM_DEBUG_FUNC_BEGIN();\r
-       int err = EMAIL_ERROR_NONE;\r
-\r
-       DBusGConnection *connection = NULL;\r
-       DBusGProxy *proxy = NULL;\r
-       GError *error = NULL;\r
-       guint dbus_result = 0;\r
-\r
-       g_type_init();\r
-\r
-       connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);\r
-       if (!connection) {\r
-               if (error) {\r
-                       EM_DEBUG_EXCEPTION("Unable to connect to dbus: %s", error->message);\r
-                       g_error_free(error);\r
-                       err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-                       return err;\r
-               }\r
-       }\r
-\r
-       proxy = dbus_g_proxy_new_for_name(connection, EMAIL_SERVICE_NAME, EMAIL_SERVICE_PATH, EMAIL_SERVICE_NAME);\r
-       if (!proxy) {\r
-               EM_DEBUG_EXCEPTION("dbus_g_proxy_new_for_name failed");\r
-               err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-               return err;\r
-       }\r
-\r
-       //will trigger activation if the service does not exist\r
-       if (org_tizen_email_service_launch(proxy, &dbus_result, &error) == false) {\r
-               if (error) {\r
-                       EM_DEBUG_EXCEPTION("email_service_launch failed : [%s]", error->message);\r
-                       g_error_free(error);\r
-                       err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-                       return err;\r
-               }\r
-       }\r
-\r
-       EM_DEBUG_LOG("org_tizen_email_service_launch : [%d]", dbus_result);\r
-\r
-       g_object_unref(proxy);\r
-       EM_DEBUG_FUNC_END("ret [%d]", err);\r
-       return err;\r
-}\r
-\r
-EXPORT_API int emipc_init_dbus_connection()\r
-{\r
-       EM_DEBUG_FUNC_BEGIN();\r
-\r
-       EmailService *object;\r
-       DBusGProxy *proxy = NULL;\r
-       DBusGConnection *connection = NULL;\r
-       GError *error = NULL;\r
-\r
-       guint request_ret = 0;\r
-       int err = EMAIL_ERROR_NONE;\r
-\r
-       g_type_init();\r
-\r
-       //init dbus connection\r
-       connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);\r
-       if (!connection) {\r
-               if (error) {\r
-                       EM_DEBUG_EXCEPTION("Unable to connect to dbus: %s", error->message);\r
-                       g_error_free(error);\r
-                       err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-                       return err;\r
-               }\r
-       }\r
-\r
-       //create email_service object\r
-       object = g_object_new(EMAIL_SERVICE_TYPE, NULL);\r
-\r
-       //register dbus path\r
-       dbus_g_connection_register_g_object(connection, EMAIL_SERVICE_PATH, G_OBJECT(object));\r
-\r
-       //register the service name\r
-       proxy = dbus_g_proxy_new_for_name(connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);\r
-       if (!proxy) {\r
-               EM_DEBUG_EXCEPTION("dbus_g_proxy_new_for_name failed");\r
-               err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-               return err;\r
-       }\r
-\r
-       if (!org_freedesktop_DBus_request_name(proxy, EMAIL_SERVICE_NAME, 0, &request_ret, &error)) {\r
-               if (error) {\r
-                       EM_DEBUG_EXCEPTION("Unable to register service: %s", error->message);\r
-                       g_error_free(error);\r
-                       err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;\r
-               }\r
-       }\r
-\r
-       g_object_unref(proxy);\r
-\r
-       EM_DEBUG_FUNC_END("err [%d]", err);\r
-       return err;\r
-}\r
-\r
-gboolean email_service_launch(EmailService *email_service, guint *result_val, GError **error)\r
-{\r
-       EM_DEBUG_LOG("email_service_launch entered");\r
-       EM_DEBUG_LOG("email_service_launch PID=[%ld]" , getpid());\r
-       EM_DEBUG_LOG("email_service_launch TID=[%ld]" , pthread_self());\r
-\r
-       if (result_val)\r
-               *result_val = TRUE;\r
-\r
-       return TRUE;\r
-}\r
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+#include <glib.h>
+#include "email-types.h"
+#include "email-debug-log.h"
+#include "email-dbus-activation.h"
+#include "email-utilities.h"
+#include "email-core-account.h"
+#include "email-core-utils.h"
+
+EXPORT_API int launch_pid = 0;
+EXPORT_API const gchar introspection_xml[] =
+"<node>"
+"      <interface name='org.tizen.email_service'>"
+"              <method name='Launch'>"
+"                      <arg type='i' name='caller_pid' direction='in' />"
+"                      <arg type='i' name='ret' direction='out' />"
+"              </method>"
+"              <method name='SetContactsLog'>"
+"                      <arg type='i' name='account_id' direction='in'/>"
+"                      <arg type='s' name='email_address' direction='in'/>"
+"                      <arg type='s' name='subject' direction='in'/>"
+"                      <arg type='i' name='date_time' direction='in'/>"
+"                      <arg type='i' name='action' direction='in'/>"
+"                      <arg type='i' name='err' direction='out'/>"
+"              </method>"
+"              <method name='DeleteContactsLog'>"
+"                      <arg type='i' name='account_id' direction='in' />"
+"                      <arg type='i' name='ret' direction='out' />"
+"              </method>"
+"              <method name='GetDisplayName'>"
+"                      <arg type='s' name='email_address' direction='in' />"
+"                      <arg type='s' name='multi_user_name' direction='in' />"
+"                      <arg type='s' name='contact_display_name' direction='out' />"
+"                      <arg type='i' name='ret' direction='out' />"
+"              </method>"
+"              <method name='CheckBlockingMode'>"
+"                      <arg type='s' name='sender_address' direction='in' />"
+"                      <arg type='s' name='multi_user_name' direction='in' />"
+"                      <arg type='i' name='blocking_mode' direction='out' />"
+"                      <arg type='i' name='ret' direction='out' />"
+"              </method>"
+"              <method name='QueryServerInfo'>"
+"                      <arg type='i' name='pid' direction='in'/>"
+"                      <arg type='s' name='domain_name' direction='in'/>"
+                        /* err, email_server_info_t */
+"                      <arg type='(i((sii)a(iisiii)))' name='query_ret' direction='out'/>"
+"              </method>"
+"      </interface>"
+"</node>";
+
+static gboolean on_timer_proxy_new(gpointer userdata)
+{
+       EM_DEBUG_LOG("on_timer_proxy_new");
+       GCancellable *proxy_cancel = (GCancellable *)userdata;
+
+       if (proxy_cancel) {
+               if (!g_cancellable_is_cancelled (proxy_cancel))
+                       g_cancellable_cancel(proxy_cancel);
+       }
+
+       return false;
+}
+
+/* called from clients */
+EXPORT_API int emipc_launch_email_service()
+{
+       EM_DEBUG_LOG("emipc_launch_email_service");
+       GError *gerror = NULL;
+       int ret = EMAIL_ERROR_NONE;
+       guint timer_tag = 0;
+
+       GCancellable *proxy_cancel = g_cancellable_new();
+       timer_tag = g_timeout_add(5000, on_timer_proxy_new, proxy_cancel);
+       GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, 
+                                   G_DBUS_PROXY_FLAGS_NONE, 
+                                   NULL, 
+                                   EMAIL_SERVICE_NAME,
+                                   EMAIL_SERVICE_PATH,
+                                   EMAIL_SERVICE_NAME, 
+                                   proxy_cancel,
+                                   &gerror);
+
+       g_source_remove(timer_tag);
+
+       if (!bproxy) {
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_new_for_bus_sync error [%s]", 
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       GVariant *result = g_dbus_proxy_call_sync (bproxy, 
+                        "Launch", 
+                        g_variant_new ("(i)", getpid()), 
+                        G_DBUS_CALL_FLAGS_NONE, 
+                        5000,  /* msec, 5s*/
+                        NULL, 
+                        &gerror);
+
+
+       if (!result) {
+               EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'Launch' error [%s]", 
+                                 gerror->message);
+               ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
+               goto FINISH_OFF;
+       }
+
+       g_variant_get (result, "(&d)", &ret);
+
+FINISH_OFF:
+       EM_DEBUG_LOG ("ret [%d]\n", ret);
+       if (bproxy)
+               g_object_unref (bproxy);
+
+       if (proxy_cancel)
+               g_object_unref(proxy_cancel);
+
+       if (gerror)
+               g_error_free (gerror);
+
+       return ret;
+}
+
+void cancellable_connect_cb ()
+{
+       EM_DEBUG_LOG ("Cancellable is now canceled");
+}
+
+EXPORT_API GCancellable *cancel = NULL;
+
+GVariant* em_gdbus_get_display_name (GVariant *parameters)
+{
+       char *email_address        = NULL;
+       char *multi_user_name      = NULL;
+       char *contact_display_name = NULL;
+
+       g_variant_get (parameters, "(ss)", &email_address, &multi_user_name);
+
+       /* replace "" to NULL */
+       if (!g_strcmp0(email_address,""))
+               EM_SAFE_FREE (email_address);
+
+       int err = emcore_get_mail_display_name_internal (multi_user_name, email_address, &contact_display_name);
+
+       /* make return_val */
+       if (!contact_display_name) {
+               contact_display_name = strdup("");
+       }
+       GVariant* ret = g_variant_new ("(si)", contact_display_name, err);
+
+       /* clean-up */
+       EM_SAFE_FREE (email_address);
+       EM_SAFE_FREE (contact_display_name);
+
+       return ret;
+}
+
+GVariant* em_gdbus_check_blocking_mode (GVariant *parameters)
+{
+       char *sender_address = NULL;
+       char *multi_user_name = NULL;
+       int blocking_mode = 0;
+       int err = EMAIL_ERROR_NONE;
+
+       g_variant_get (parameters, "(ss)", &sender_address, &multi_user_name);
+#ifdef __FEATURE_BLOCKING_MODE__
+       err = emcore_check_blocking_mode_internal (multi_user_name, sender_address, &blocking_mode);
+#endif /* __FEATURE_BLOCKING_MODE__ */
+       /* make return_val */
+       GVariant* ret = g_variant_new ("(ii)", blocking_mode, err);
+
+       /* clean-up string */
+       EM_SAFE_FREE (sender_address);
+       EM_SAFE_FREE (multi_user_name);
+
+       return ret;
+}
+
index de17fe1..a41c9ca 100755 (executable)
@@ -1,4 +1,5 @@
 [D-BUS Service]
 Name=org.tizen.email_service
-Exec=/usr/bin/email-service
+Exec=/bin/false
+SystemdService=email-service.service
 User=root
\ No newline at end of file
index 7e32541..82c16ea 100755 (executable)
 #ifndef __EMAIL_DBUS_ACTIVATION_H__\r
 #define __EMAIL_DBUS_ACTIVATION_H__\r
 \r
-/* standard library header */\r
-#include <glib-object.h>\r
-\r
-typedef struct _email_service_t EmailService;\r
-typedef struct _email_service_class_t EmailServiceClass;\r
+#include <gio/gio.h>\r
 \r
 #define EMAIL_SERVICE_NAME "org.tizen.email_service"\r
 #define EMAIL_SERVICE_PATH "/org/tizen/email_service"\r
 \r
-GType email_service_get_type(void);\r
-\r
-struct _email_service_t {\r
-       GObject parent;\r
-       int status;\r
-};\r
-\r
-struct _email_service_class_t {\r
-       GObjectClass parent;\r
-};\r
-\r
-#define EMAIL_SERVICE_TYPE              (email_service_get_type())\r
-#define EMAIL_SERVICE(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), EMAIL_SERVICE_TYPE, EmailService))\r
-#define EMAIL_SERVICE_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), EMAIL_SERVICE_TYPE, EmailServiceClass))\r
-#define IS_EMAIL_SERVICE(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), EMAIL_SERVICE_TYPE))\r
-#define IS_EMAIL_SERVICE_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), EMAIL_SERVICE_TYPE))\r
-#define EMAIL_SERVICE_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), EMAIL_SERVICE_TYPE, EmailServiceClass))\r
-\r
-typedef enum {\r
-       NFC_SERVICE_ERROR_INVALID_PRAM\r
-} email_servcie_error;\r
+/* \r
+note: it is not safe freeing the return-val\r
+*/\r
+#define EM_GDBUS_STR_NEW(str)    (str? str:"")\r
+/* input string is released in case of dummy val */\r
+#define EM_GDBUS_STR_GET(str)    \\r
+       ({\\r
+               char* ret;\\r
+               if (g_strcmp0(str, "")) {\\r
+                       ret = str;\\r
+               }\\r
+               else {\\r
+                       g_free(str);\\r
+                       str = NULL;\\r
+                       ret = NULL;\\r
+               }\\r
+               ret;\\r
+       })\r
 \r
-/**\r
- *     launch the email-service\r
- */\r
 \r
-gboolean email_service_launch(EmailService *email_service, guint *result_val, GError **error);\r
-EXPORT_API int emipc_init_dbus_connection();\r
 EXPORT_API int emipc_launch_email_service();\r
-\r
-EXPORT_API void emipc_set_launch_method(int input_launch_method);\r
-EXPORT_API int  emipc_get_launch_method();\r
-\r
+EXPORT_API GVariant* em_gdbus_set_contact_log (GVariant *parameters);\r
+EXPORT_API GVariant* em_gdbus_delete_contact_log (GVariant *parameters);\r
+EXPORT_API GVariant* em_gdbus_get_display_name (GVariant *parameters);\r
+EXPORT_API GVariant* em_gdbus_check_blocking_mode (GVariant *parameters);\r
 #endif /* __EMAIL_DBUS_ACTIVATION_H__ */\r
+\r
index c2d527f..2b27259 100755 (executable)
@@ -244,7 +244,7 @@ EXPORT_API int emipc_execute_proxy_task(email_task_type_t input_task_type, void
 
        emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
 
-       FINISH_OFF:
+FINISH_OFF:
        if(hAPI)
                emipc_destroy_email_api(hAPI);
 
@@ -252,3 +252,49 @@ EXPORT_API int emipc_execute_proxy_task(email_task_type_t input_task_type, void
        return err;
 }
 
+EXPORT_API int emipc_get_user_name(char **output_user_name)
+{
+    EM_DEBUG_FUNC_BEGIN();
+
+    int err = EMAIL_ERROR_NONE;
+
+    if (!output_user_name) {
+        EM_DEBUG_EXCEPTION("Invalid parameter");
+        err = EMAIL_ERROR_INVALID_PARAM;
+        return err;
+    }
+
+    int buffer_size = 0;
+    char *user_name = NULL;
+
+    HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_USER_NAME);
+    if(!emipc_execute_proxy_api(hAPI))  {
+        EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
+        err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
+        goto FINISH_OFF;
+    }
+
+    emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
+    if (err != EMAIL_ERROR_NONE) 
+        goto FINISH_OFF;
+
+    buffer_size = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 1);
+    if (buffer_size > 0) {
+        user_name = (char *)malloc(buffer_size);
+        emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, buffer_size, user_name);
+        EM_DEBUG_LOG("Name : [%s]", user_name);
+    }
+
+FINISH_OFF:
+
+    *output_user_name = EM_SAFE_STRDUP(user_name);
+
+    EM_SAFE_FREE(user_name);
+
+    if (hAPI) 
+        emipc_destroy_email_api(hAPI);
+
+    EM_DEBUG_FUNC_END();
+
+    return err;
+}
index bf8e263..76e1ee4 100755 (executable)
@@ -104,24 +104,13 @@ EXPORT_API void *emipc_get_parameters_of_api_info(emipc_email_api_info *api_info
        return api_info->params[direction];
 }
 
-EXPORT_API bool emipc_free_api_info(emipc_email_api_info *api_info)
+EXPORT_API void emipc_free_api_info(emipc_email_api_info *api_info)
 {
-       if (!api_info) {
-               EM_DEBUG_EXCEPTION("Invalid parameter");
-               return false;
-       }
-
-       if (!emipc_destroy_param_list(api_info->params[ePARAMETER_IN])) {
-               EM_DEBUG_EXCEPTION("emipc_destroy_param_list failed : ePARAMETER[%d]", ePARAMETER_IN);
-               return false;
-       }
-
-       if (!emipc_destroy_param_list(api_info->params[ePARAMETER_OUT])) {
-               EM_DEBUG_EXCEPTION("emipc_destroy_param_list failed : ePARAMETER[%d]", ePARAMETER_OUT);
-               return false;
-       }
+       if (!api_info) 
+               return;
 
-       return true;
+       emipc_destroy_param_list (api_info->params[ePARAMETER_IN]);
+       emipc_destroy_param_list (api_info->params[ePARAMETER_OUT]);
 }
 
 
index bfec354..aeb471e 100755 (executable)
@@ -50,21 +50,19 @@ EXPORT_API emipc_param_list *emipc_create_param_list()
        return new_param_list;
 }
 
-EXPORT_API bool emipc_destroy_param_list(emipc_param_list *param_list)
+EXPORT_API void emipc_destroy_param_list(emipc_param_list *param_list)
 {
        int count = 10;
        int index = 0;
 
-       if (!param_list) {
-               return false;
-       }
+       if (!param_list)
+               return;
 
        for (index = 0; index < count; index++) {
                emipc_free_param(param_list->params[index]);
        }
        EM_SAFE_FREE(param_list->byte_stream);
        EM_SAFE_FREE(param_list);
-       return true;
 }
 
 /* making stream into param length and param data */
index 35fcb66..d11ad64 100755 (executable)
@@ -45,7 +45,7 @@ EXPORT_API unsigned char *emipc_serialize_api_info(emipc_email_api_info *api_inf
 
 EXPORT_API void *emipc_get_parameters_of_api_info(emipc_email_api_info *api_info, EPARAMETER_DIRECTION direction);
 
-EXPORT_API bool emipc_free_api_info(emipc_email_api_info *api_info);
+EXPORT_API void emipc_free_api_info(emipc_email_api_info *api_info);
 
 /* don't insert empty line */
 #define EM_APIID_TO_STR(nAPIID) \
@@ -71,6 +71,9 @@ EXPORT_API bool emipc_free_api_info(emipc_email_api_info *api_info);
                        case _EMAIL_API_RENAME_MAILBOX:\
                                        s = "_EMAIL_API_RENAME_MAILBOX";\
                                        break;\
+                       case _EMAIL_API_RENAME_MAILBOX_EX:\
+                                       s = "_EMAIL_API_RENAME_MAILBOX_EX";\
+                                       break;\
                        case _EMAIL_API_SET_MAILBOX_TYPE:\
                                        s = "_EMAIL_API_SET_MAILBOX_TYPE";\
                                        break;\
@@ -191,9 +194,6 @@ EXPORT_API bool emipc_free_api_info(emipc_email_api_info *api_info);
                        case _EMAIL_API_SEND_MAIL_CANCEL_JOB :\
                                        s = "_EMAIL_API_SEND_MAIL_CANCEL_JOB";\
                                        break;\
-                       case _EMAIL_API_SEARCH_MAIL_ON_SERVER :\
-                                       s = "_EMAIL_API_SEARCH_MAIL_ON_SERVER";\
-                                       break;\
                        case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :\
                                        s = "_EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION";\
                                        break;\
@@ -209,6 +209,9 @@ EXPORT_API bool emipc_free_api_info(emipc_email_api_info *api_info);
                        case _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL :\
                                        s = "_EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL";\
                                        break;\
+                       case _EMAIL_API_CLEAR_NOTIFICATION_BAR :\
+                                       s = "_EMAIL_API_CLEAR_NOTIFICATION_BAR";\
+                                       break;\
                        case _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT:\
                                        s = "_EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT";\
                                        break;\
index 4706596..e12c17b 100755 (executable)
@@ -43,7 +43,7 @@ typedef struct {
 
 EXPORT_API emipc_param_list *emipc_create_param_list();
 
-EXPORT_API bool emipc_destroy_param_list(emipc_param_list *param_list);
+EXPORT_API void emipc_destroy_param_list(emipc_param_list *param_list);
 
 EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, void *stream_data);
 
index ccc117f..fff5db6 100755 (executable)
 #include "email-proxy-socket.h"
 
 #include "email-debug-log.h"
-#include "email-api.h"
 #include "email-types.h"
 #include "email-internal-types.h"
 #include "email-dbus-activation.h"
 #include "email-storage.h"
+#include "email-core-task-manager.h"
 
 EXPORT_API int emipc_initialize_proxy ()
 {
@@ -56,6 +56,11 @@ EXPORT_API int emipc_finalize_proxy()
        return emipc_finalize_proxy_main();
 }
 
+#define ALLOW_TO_LAUNCH_DAEMON(api_id)    \
+                                  ((api_id) == _EMAIL_API_ADD_ACCOUNT ||\
+                                   (api_id) == _EMAIL_API_VALIDATE_ACCOUNT_EX ||\
+                                   (api_id) == _EMAIL_API_RESTORE_ACCOUNTS)
+
 EXPORT_API int emipc_execute_proxy_api(HIPC_API api)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -70,26 +75,27 @@ EXPORT_API int emipc_execute_proxy_api(HIPC_API api)
                return EMAIL_ERROR_INVALID_PARAM;
        }
 
-       EM_DEBUG_LOG_SEC("Request: API_ID[%s] RES_ID[%d] APP_ID[%d]",\
-                               EM_APIID_TO_STR(api_info->api_id), api_info->response_id, api_info->app_id);
+       EM_DEBUG_LOG_SEC("Request: API_ID[%s][0x%x] RES_ID[%d] APP_ID[%d]",\
+                                          EM_APIID_TO_STR(api_info->api_id), 
+                                          api_info->api_id,
+                                          api_info->response_id, 
+                                          api_info->app_id);
 
        ret = emipc_execute_api_of_proxy_main(api_info);
 
        /* connection retry */
        if (!ret) {
-               EM_DEBUG_LOG("Connection retry");
-               emipc_finalize_proxy();
-
+               emipc_end_proxy_socket();
+               EM_DEBUG_LOG("Launch email-service daemon");
                err = emipc_initialize_proxy();
                if (err != EMAIL_ERROR_NONE) {
-                       EM_DEBUG_EXCEPTION("Failed to open the socket : [%d]", err);
-                       err = EMAIL_ERROR_CONNECTION_FAILURE;
-                       goto FINISH_OFF;
+                       EM_DEBUG_EXCEPTION ("emipc_initialize_proxy [%d]", err);
+                       goto FINISH_OFF;                        
                }
 
-               ret = emipc_execute_api_of_proxy_main(api_info);
-               if (!ret) {
-                       EM_DEBUG_EXCEPTION("emipc_execute_api_of_proxy_main failed [%d]", err);
+               err = emipc_execute_api_of_proxy_main(api_info);
+               if (!err) {
+                       EM_DEBUG_EXCEPTION("emipc_execute_api_of_proxy_main error");
                        err = EMAIL_ERROR_CONNECTION_FAILURE;
                        goto FINISH_OFF;
                }
index af730a9..fcba8d1 100755 (executable)
 EXPORT_API int emipc_initialize_proxy_main()
 {
        EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
        int sock_fd = 0;
 
        sock_fd = emipc_get_proxy_socket_id();
 
        if (sock_fd) {
                EM_DEBUG_LOG("Socket already initialized");
-               return EMAIL_ERROR_IPC_ALREADY_INITIALIZED;
+               return err;
        }
        
-       if (!emipc_start_proxy_socket()) {
+       if ((err = emipc_start_proxy_socket()) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("Socket start failed");
+               if (err == EMAIL_ERROR_PERMISSION_DENIED)
+                       return err;
+               
                return EMAIL_ERROR_IPC_CONNECTION_FAILURE;
        }
        
        EM_DEBUG_LOG("Socket ID : %d", emipc_get_proxy_socket_id());
        EM_DEBUG_FUNC_END();
-       return EMAIL_ERROR_NONE
+       return err
 }
 
 EXPORT_API int emipc_finalize_proxy_main()
old mode 100644 (file)
new mode 100755 (executable)
index 6d5fa5a..f9c2452
@@ -46,7 +46,7 @@ typedef struct {
 GList *socket_head = NULL;
 pthread_mutex_t proxy_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-EXPORT_API bool emipc_start_proxy_socket()
+EXPORT_API int emipc_start_proxy_socket()
 {
        EM_DEBUG_FUNC_BEGIN();
        int ret = true;
@@ -58,19 +58,10 @@ EXPORT_API bool emipc_start_proxy_socket()
                return false;
        }
 
-#ifdef __FEATURE_ACCESS_CONTROL__
-       int err = EMAIL_ERROR_NONE;
-       err = em_check_socket_privilege_by_pid(getpid());
-       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-               EM_DEBUG_LOG("permission denied");
-               return false;
-       }
-#endif
-
        ret = emipc_connect_email_socket(socket_fd);
-       if (!ret) {
+       if (ret != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emipc_connect_email_socket failed");
-               return false;
+               return ret;
        }
 
        thread_socket_t* cur = (thread_socket_t*) em_malloc(sizeof(thread_socket_t));
@@ -104,15 +95,6 @@ EXPORT_API bool emipc_end_proxy_socket()
 
                /* close the socket of current thread */
                if( tid == cur_socket->tid ) {
-#ifdef __FEATURE_ACCESS_CONTROL__
-                       int err = EMAIL_ERROR_NONE;
-                       err = em_check_socket_privilege_by_pid(cur_socket->pid);
-                       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-                               LEAVE_CRITICAL_SECTION(proxy_mutex); /*prevent 30968*/
-                               EM_DEBUG_LOG("permission denied");
-                               return false;
-                       }
-#endif
                        emipc_close_email_socket(&cur_socket->socket_fd);
                        EM_SAFE_FREE(cur_socket);
                        GList *del = cur;
@@ -142,16 +124,6 @@ EXPORT_API bool emipc_end_all_proxy_sockets()
 
                /* close all sockets of the pid */
                if( pid == cur_socket->pid ) {
-#ifdef __FEATURE_ACCESS_CONTROL__
-                       int err = EMAIL_ERROR_NONE;
-                       err = em_check_socket_privilege_by_pid(cur_socket->pid);
-                       if (err == EMAIL_ERROR_PERMISSION_DENIED) {
-                               LEAVE_CRITICAL_SECTION(proxy_mutex); /*prevent 30967*/
-                               EM_DEBUG_LOG("permission denied");
-                               return false;
-                       }
-#endif
-
                        emipc_close_email_socket(&cur_socket->socket_fd);
                        EM_SAFE_FREE(cur_socket);
                        GList *del = cur;
@@ -220,12 +192,11 @@ EXPORT_API int emipc_get_proxy_socket_id()
  */
 static bool wait_for_reply (int fd)
 {
-       int return_from_select = -1;
+       int err = -1;
        fd_set fds;
        struct timeval tv;
-       char errno_buf[ERRNO_BUF_SIZE] = {0};
 
-       if (fd == 0) {
+       if (fd < 0) {
                EM_DEBUG_EXCEPTION("Invalid file description : [%d]", fd);
                return false;
        }
@@ -237,13 +208,13 @@ static bool wait_for_reply (int fd)
        tv.tv_usec = 0;
 
        EM_DEBUG_LOG_DEV ("wait for response [%d]", fd);
-
-       if ((return_from_select = select(fd + 1, &fds, NULL, NULL, &tv)) == -1) {
-               EM_DEBUG_EXCEPTION("[IPCLib] select failed: %s", EM_STRERROR(errno_buf));
+       err = select(fd + 1, &fds, NULL, NULL, &tv);
+       if (err == -1) {
+               EM_DEBUG_EXCEPTION("[IPCLib] select error[%d] fd[%d]", errno, fd);
                return false;
        }
-       else if (return_from_select == 0) {
-               EM_DEBUG_EXCEPTION("[IPCLib] select: timeout");
+       else if (err == 0) {
+               EM_DEBUG_EXCEPTION("[IPCLib] select timeout fd[%d]", fd);
                return false;
        }
 
index c89917e..83b49c1 100755 (executable)
@@ -26,7 +26,7 @@
 
 #include "email-types.h"
 
-EXPORT_API bool emipc_start_proxy_socket();
+EXPORT_API int emipc_start_proxy_socket();
 
 EXPORT_API bool emipc_end_proxy_socket();
 
index cc494c8..83a806d 100755 (executable)
@@ -26,6 +26,8 @@
 #include "email-debug-log.h"
 #include "email-types.h"
 
+#include "email-core-container.h"
+
 #include <glib.h>
 
 #include <sys/socket.h>
@@ -38,6 +40,7 @@
 #include <errno.h>
 #include <unistd.h>
 
+#include <sys/smack.h>
 #include <systemd/sd-daemon.h>
 
 EXPORT_API bool emipc_init_email_socket(int *fd)
@@ -45,7 +48,7 @@ EXPORT_API bool emipc_init_email_socket(int *fd)
        bool ret = true;
        char errno_buf[ERRNO_BUF_SIZE] = {0};
 
-       *fd = socket(AF_UNIX, SOCK_STREAM, 0);
+       *fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
        if (*fd < 0) {
                EM_DEBUG_EXCEPTION("socket failed: %s", EM_STRERROR(errno_buf));
                ret = false;
@@ -60,8 +63,7 @@ EXPORT_API bool emipc_init_email_socket(int *fd)
 EXPORT_API void emipc_close_email_socket(int* fd)
 {
        EM_DEBUG_LOG("fd %d removal done", *fd);
-       close(*fd);
-       *fd = 0;
+       EM_SAFE_CLOSE (*fd);
 }
 
 /* returns positive write length,
@@ -71,12 +73,11 @@ static int emipc_writen(int fd, const char *buf, int len)
 {
        int length = len;
        int passed_len = 0;
-       char errno_buf[ERRNO_BUF_SIZE] = {0};
 
        while (length > 0) {
                passed_len = send(fd, (const void *)buf, length, MSG_NOSIGNAL);
                if (passed_len == -1) {
-                       EM_DEBUG_LOG("write : %s", EM_STRERROR(errno_buf));
+                       EM_DEBUG_EXCEPTION ("send error [%d]", errno);
                        if (errno == EINTR) continue;
                        else if (errno == EPIPE) return 0; /* connection closed */
                        else return passed_len; /* -1 */
@@ -104,8 +105,9 @@ EXPORT_API int emipc_send_email_socket(int fd, unsigned char *buf, int len)
        EM_DEBUG_LOG("Sending %dB data to [fd = %d]", len, fd);
 
        int write_len = emipc_writen(fd, (char*) buf, len);
-       if ( write_len != len) {
-               if ( write_len == 0 ) return 0;
+       if (write_len == 0) /* connection closed */
+               return 0; 
+       if (write_len != len) {
                EM_DEBUG_LOG("WARNING: buf_size [%d] != write_len[%d]", len, write_len);
                return EMAIL_ERROR_IPC_SOCKET_FAILURE;
        }
@@ -117,13 +119,13 @@ static int emipc_readn(int fd, char *buf, int len)
 {
        int length = len;
        int read_len = 0;
-       char errno_buf[ERRNO_BUF_SIZE] = {0};
 
        while (length > 0) {
                read_len = read(fd, (void *)buf, length);
                if (read_len < 0) {
-                       EM_DEBUG_EXCEPTION("Read : %s", EM_STRERROR(errno_buf));
+                       EM_DEBUG_EXCEPTION("read err %d", errno);
                        if (errno == EINTR) continue;
+                       else if (errno == EPIPE) return 0; /* connection closed */
                        return read_len;
                } else if (read_len == 0)
                        break;
@@ -169,6 +171,8 @@ EXPORT_API int emipc_recv_email_socket(int fd, char **buf)
 
        EM_DEBUG_LOG_DEV("[IPC Socket] Receiving [%d] bytes", read_len);
        int len = emipc_readn(fd, *buf, read_len);
+       if (len == 0) /* connection closed */
+               return 0; 
        if (read_len != len) {
                EM_SAFE_FREE(*buf);
                EM_DEBUG_LOG("WARNING: buf_size [%d] != read_len[%d]", read_len, len);
@@ -212,8 +216,8 @@ EXPORT_API int emipc_open_email_socket(int fd, const char *path)
 
        if (strcmp(path, EM_SOCKET_PATH) == 0 &&
                sd_listen_fds(1) == 1 &&
-               sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, -1, EM_SOCKET_PATH, 0) > 0) {
-               close(fd);
+               sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_SEQPACKET, -1, EM_SOCKET_PATH, 0) > 0) {
+               EM_SAFE_CLOSE (fd);
                sock_fd = SD_LISTEN_FDS_START + 0;
                return sock_fd;
        }
@@ -240,6 +244,8 @@ EXPORT_API int emipc_open_email_socket(int fd, const char *path)
                return EMAIL_ERROR_IPC_SOCKET_FAILURE;
        }
 
+    emcore_set_declare_link(path);
+
        /**
         * determine permission of socket file
         *
@@ -258,6 +264,18 @@ EXPORT_API int emipc_open_email_socket(int fd, const char *path)
                return EMAIL_ERROR_IPC_SOCKET_FAILURE;
        }
 
+#if 0
+       if (smack_setlabel(path, "*", SMACK_LABEL_IPIN) != 0) {
+               EM_DEBUG_EXCEPTION("smack_setlabel error");
+               return EMAIL_ERROR_SYSTEM_FAILURE;
+       }
+
+       if (smack_setlabel(path, "@", SMACK_LABEL_IPOUT) != 0) {
+               EM_DEBUG_EXCEPTION("smack_setlabel error");
+               return EMAIL_ERROR_SYSTEM_FAILURE;
+       }
+#endif
+
        if (listen(fd, 10) == -1) {
                EM_DEBUG_LOG("listen: %s", EM_STRERROR(errno_buf));
                return EMAIL_ERROR_IPC_SOCKET_FAILURE;
@@ -267,9 +285,11 @@ EXPORT_API int emipc_open_email_socket(int fd, const char *path)
        return fd;
 }
 
-EXPORT_API bool emipc_connect_email_socket(int fd)
+EXPORT_API int emipc_connect_email_socket(int fd)
 {
        EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       int p_errno = 0;
        struct sockaddr_un server;
        memset(&server, 0, sizeof(server));
        server.sun_family = AF_UNIX;
@@ -278,10 +298,17 @@ EXPORT_API bool emipc_connect_email_socket(int fd)
 
        if (connect(fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
                EM_DEBUG_EXCEPTION ("connect failed: [%s][errno=%d][fd=%d]", EM_STRERROR(errno_buf), errno, fd);
-               return false;
+
+               p_errno = errno;
+               if (p_errno == EACCES || p_errno == EPERM) 
+                       err = EMAIL_ERROR_PERMISSION_DENIED;
+               else 
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+               
+               return err;
        }
 
        EM_DEBUG_FUNC_END();
-       return true;
+       return err;
 }
 
index 10bfff7..4cc5717 100755 (executable)
@@ -46,7 +46,7 @@ EXPORT_API int emipc_accept_email_socket(int fd);
 
 EXPORT_API int emipc_open_email_socket(int fd, const char *path);
 
-EXPORT_API bool emipc_connect_email_socket(int fd);
+EXPORT_API int emipc_connect_email_socket(int fd);
 
 #endif /*  _IPC_SOCKET_H_               */
 
index 3a8027f..64e62ff 100755 (executable)
@@ -29,7 +29,6 @@
 #include "email-stub-socket.h"
 #include "email-stub-task-manager.h"
 
-#include "email-api.h"
 #include "email-debug-log.h"
 
 static bool stub_socket = false;
@@ -53,8 +52,6 @@ EXPORT_API bool emipc_initialize_stub_main(PFN_EXECUTE_API fn_api_mapper)
                return false;
        }
 
-       emipc_init_dbus_connection();
-
        EM_DEBUG_FUNC_END();
        return true;
 }
@@ -95,8 +92,11 @@ EXPORT_API bool emipc_execute_api_stub_to_proxy(emipc_email_api_info *api_info)
 {
        EM_DEBUG_FUNC_BEGIN("api_info [%p]", api_info);
        EM_IF_NULL_RETURN_VALUE(api_info, false);
-       EM_DEBUG_LOG_SEC ("Response: API_ID [%s], RES_ID [%d], APP_ID [%d]",\
-                               EM_APIID_TO_STR(api_info->api_id), api_info->response_id, api_info->app_id);
+       EM_DEBUG_LOG_SEC ("Response: API_ID [%s][0x%x], RES_ID [%d], APP_ID [%d]",\
+                                            EM_APIID_TO_STR(api_info->api_id), 
+                                            api_info->api_id, 
+                                            api_info->response_id, 
+                                            api_info->app_id);
        
        unsigned char *stream = NULL;
        int stream_length = 0;
index 9450d20..2f52c21 100755 (executable)
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <malloc.h>
 #include <sys/epoll.h>
+#include <sys/socket.h>
 #include <pthread.h>
 
 #include "email-ipc-build.h"
@@ -104,21 +105,11 @@ static void *emipc_stub_socket_thread_proc()
        return NULL;
 }
 
-static int emipc_check_connected(int fd)
-{
-       EM_DEBUG_FUNC_BEGIN ("fd[%d]", fd);
-       int found = (g_list_find (connected_fd, (gpointer)fd))? true : false;
-       EM_DEBUG_FUNC_END ("fd found?? [%d]", found);
-       return found;
-}
-
-
 EXPORT_API void emipc_wait_for_ipc_request()
 {
        struct epoll_event ev = {0};
        int epfd = 0;
        int event_num = 0;
-       char errno_buf[ERRNO_BUF_SIZE] = {0};
        struct epoll_event events[MAX_EPOLL_EVENT] = {{0}, };
 
        if (!stub_socket) {
@@ -130,8 +121,8 @@ EXPORT_API void emipc_wait_for_ipc_request()
        
        epfd = epoll_create(MAX_EPOLL_EVENT);
        if (epfd < 0) {
-               EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-               EM_DEBUG_CRITICAL_EXCEPTION("epoll_create failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+               EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
+               EM_DEBUG_CRITICAL_EXCEPTION("epoll_create error [%d]", errno);
                return;
        }
 
@@ -139,8 +130,8 @@ EXPORT_API void emipc_wait_for_ipc_request()
        ev.data.fd = stub_socket;
        
        if (epoll_ctl(epfd, EPOLL_CTL_ADD, stub_socket, &ev) == -1) {
-               EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-               EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl failed:%s[%d]", EM_STRERROR(errno_buf), errno);  
+               EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
+               EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl error [%d]", errno);     
        }
        while (!stop_thread) {
                int i = 0;
@@ -153,26 +144,31 @@ EXPORT_API void emipc_wait_for_ipc_request()
                }
 
                if (event_num == -1) {
-                       EM_DEBUG_EXCEPTION("epoll_wait failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-                       EM_DEBUG_CRITICAL_EXCEPTION("epoll_wait failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-               } else {
+                       if (errno != EINTR ) {
+                               EM_DEBUG_EXCEPTION("epoll_wait error [%d]", errno);
+                               EM_DEBUG_CRITICAL_EXCEPTION("epoll_wait error [%d]", errno);
+                       }
+               }
+               else {
                        for (i = 0; i < event_num; i++) {
                                int event_fd = events[i].data.fd;
 
                                if (event_fd == stub_socket) { /*  if it is socket connection request */
                                        int cfd = emipc_accept_email_socket (stub_socket);
                                        if (cfd < 0) {
-                                               EM_DEBUG_EXCEPTION ("emipc_accept_email_socket failed [%d]", cfd);
+                                               EM_DEBUG_EXCEPTION ("emipc_accept_email_socket error [%d]", cfd);
                                                /* EM_DEBUG_CRITICAL_EXCEPTION ("accept failed: %s[%d]", EM_STRERROR(errno_buf), errno);*/
+                                               continue;
                                        }
                                        ev.events = EPOLLIN;
                                        ev.data.fd = cfd;
                                        if (epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev) == -1) {
-                                               EM_DEBUG_EXCEPTION("epoll_ctl failed [%s][%d]", EM_STRERROR(errno_buf), errno);
+                                               EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
                                                /*EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl failed:%s[%d]", EM_STRERROR(errno_buf), errno);*/
+                                               continue;
                                        }
-                                       connected_fd = g_list_prepend (connected_fd, (gpointer)cfd);
-                               else {
+                               } 
+                               else {
                                        int recv_len;
                                        char *sz_buf = NULL;
                                        
@@ -183,18 +179,37 @@ EXPORT_API void emipc_wait_for_ipc_request()
 
                                                /* IPC request stream is at least 16byte */
                                                if (recv_len >= sizeof(long) * eSTREAM_DATA) {
-                                                       emipc_create_task((unsigned char *)sz_buf, event_fd);
-                                               } else
+                                                       int ret = 0;
+                                                       ret = emipc_create_task((unsigned char *)sz_buf, event_fd);
+                                                       if (ret == EMAIL_ERROR_PERMISSION_DENIED) {
+                                                               if (epoll_ctl(epfd, EPOLL_CTL_DEL, event_fd, events) == -1) {
+                                                                       EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
+                                                                       EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl error [%d]", errno);
+                                                               }
+                                                               EM_SAFE_CLOSE (event_fd);
+                                                       }
+                                               } 
+                                               else
                                                        EM_DEBUG_LOG("[IPCLib] Stream size is less than default size");
-                                       } else if( recv_len == 0 ) {
+                                       } 
+                                       else if( recv_len == 0 ) { /* client shut down connection */
                                                EM_DEBUG_LOG("[IPCLib] Client closed connection [%d]", event_fd);
                                                if (epoll_ctl(epfd, EPOLL_CTL_DEL, event_fd, events) == -1) {
-                                                       EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
-                                                       EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno);
+                                                       EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
+                                                       EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl error [%d]", errno);
                                                }
-                                               connected_fd = g_list_remove (connected_fd, (gpointer)event_fd);
-                                               close(event_fd);
+                                               emipc_close_fd_in_task_queue (event_fd);
+                                               EM_SAFE_CLOSE (event_fd);
                                        } 
+                                       else { /* read errs */
+                                               EM_DEBUG_EXCEPTION ("[IPCLib] read err[%d] fd[%d]", recv_len, event_fd);
+                                               if (epoll_ctl(epfd, EPOLL_CTL_DEL, event_fd, events) == -1) {
+                                                       EM_DEBUG_EXCEPTION("epoll_ctl error [%d]", errno);
+                                                       EM_DEBUG_CRITICAL_EXCEPTION("epoll_ctl error [%d]", errno);
+                                               }
+                                               emipc_close_fd_in_task_queue (event_fd);
+                                               EM_SAFE_CLOSE (event_fd);
+                                       }
                                        EM_SAFE_FREE(sz_buf);
                                }
                        }
@@ -210,7 +225,7 @@ EXPORT_API bool emipc_end_stub_socket()
        EM_DEBUG_FUNC_BEGIN ();
        
        /* stop IPC handler thread */
-       emipc_stop_stub_socket_thread (stub_socket_thread);
+       emipc_stop_stub_socket_thread();
        stub_socket_thread = 0;
 
        /* stop task thread */
@@ -225,12 +240,12 @@ EXPORT_API int emipc_send_stub_socket(int sock_fd, void *data, int len)
        EM_DEBUG_FUNC_BEGIN("Stub socket sending %d bytes", len);
 
        int sending_bytes = 0;
-       if (emipc_check_connected(sock_fd)) { /* client may be shut down and the sock_fd can be reused by another module */
+       if (sock_fd >= 0) {
                sending_bytes = emipc_send_email_socket(sock_fd, data, len);
        }
 
-       if (sending_bytes == 0) {
-               EM_DEBUG_LOG ("sending byte for fd [%d] is zero", sock_fd);
+       if (sending_bytes <= 0) {
+               EM_DEBUG_EXCEPTION ("emipc_send_email_socket error [%d] fd [%d]", sending_bytes, sock_fd);
        }
 
        EM_DEBUG_FUNC_END("sending_bytes = %d", sending_bytes);
index 3774d2a..97dcfd4 100755 (executable)
@@ -20,6 +20,8 @@
 */
 
 
+#define _GNU_SOURCE
+#include <sys/socket.h>
 
 #include <string.h>
 #include <pthread.h>
@@ -31,8 +33,9 @@
 #include "email-ipc-build.h"
 
 #include "email-debug-log.h"
-#include "email-api.h"
 #include "email-internal-types.h"
+#include "email-utilities.h"
+//#include "email-core-cynara.h"
 
 static pthread_t task_thread = 0;
 static bool stop_flag = false;
@@ -95,11 +98,16 @@ EXPORT_API void *emipc_do_task_thread()
                        break;
                }
 
-               task = (emipc_email_task *)g_queue_pop_head(task_queue);
+               task = (emipc_email_task *)g_queue_peek_head(task_queue);
                LEAVE_CRITICAL_SECTION(ipc_task_mutex);
 
                if (task) {
                        emipc_run_task(task);
+
+                       ENTER_CRITICAL_SECTION(ipc_task_mutex);
+                       task = (emipc_email_task *)g_queue_pop_head(task_queue); 
+                       LEAVE_CRITICAL_SECTION(ipc_task_mutex);
+
                        emipc_free_email_task(task);
                        EM_SAFE_FREE(task);
                }
@@ -109,33 +117,78 @@ EXPORT_API void *emipc_do_task_thread()
 }
 
 /* code for ipc handler */
-EXPORT_API bool emipc_create_task(unsigned char *task_stream, int response_channel)
+EXPORT_API int emipc_create_task(unsigned char *task_stream, int response_channel)
 {
        emipc_email_task *task = NULL;
-       bool ret = true;
+       int err = EMAIL_ERROR_NONE;
 
        task = (emipc_email_task *)malloc(sizeof(emipc_email_task));
        if (task == NULL) {
                EM_DEBUG_EXCEPTION("Malloc failed.");
-               ret = false;
+               err = EMAIL_ERROR_OUT_OF_MEMORY;
        } else {
                if (!emipc_parse_stream_email_task(task, task_stream, response_channel)) {
                        EM_DEBUG_EXCEPTION("emipc_parse_stream_email_task failed");
-                       return false;
+               emipc_free_email_task(task);
+                       EM_SAFE_FREE(task);
+                       return err;
                }
                
                EM_DEBUG_LOG_DEV ("[IPCLib] ======================================================");
-               EM_DEBUG_LOG_SEC ("[IPCLib] Register new task: API_ID[%s][0x%x] RES_ID[%d] APP_ID[%d]", EM_APIID_TO_STR(task->api_info->api_id),\
-                                                                                                       task->api_info->api_id,\
-                                                                                                       task->api_info->response_id,\
-                                                                                                       task->api_info->app_id);
+               EM_DEBUG_LOG_SEC ("[IPCLib] Register new task: API_ID[%s][0x%x] RES_ID[%d] APP_ID[%d]", 
+                                           EM_APIID_TO_STR(task->api_info->api_id),
+                                           task->api_info->api_id,
+                                           task->api_info->response_id,
+                                           task->api_info->app_id);
                EM_DEBUG_LOG_DEV ("[IPCLib] ======================================================");
 
+        struct ucred uc;
+        socklen_t uc_len = sizeof(uc);
+
+        if (getsockopt(response_channel, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len) < 0) {
+            EM_DEBUG_EXCEPTION("getsockopt error : [%d]", errno);
+            err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
+            return err;
+        }
+
+        EM_DEBUG_LOG("Peer PID : [%d]", uc.pid);
+
+        task->api_info->app_id = uc.pid;
+
+               /*check privilege*/
+#ifdef __FEATURE_ACCESS_CONTROL__
+#if 0
+               err = emcore_check_privilege(response_channel);
+               if (err == EMAIL_ERROR_PERMISSION_DENIED) {
+                       EM_DEBUG_LOG("permission denied");
+                       emipc_free_email_task(task);
+                       EM_SAFE_FREE(task);
+                       return err;
+               }
+#endif
+#endif
+
                ENTER_CRITICAL_SECTION(ipc_task_mutex);
                g_queue_push_tail(task_queue, (void *)task);
                
                WAKE_CONDITION_VARIABLE(ipc_task_cond);
                LEAVE_CRITICAL_SECTION(ipc_task_mutex);
        }
-       return ret;
+       return err;
+}
+
+
+EXPORT_API void emipc_close_fd_in_task_queue (int fd)
+{
+       emipc_email_task *task = NULL;
+       int i = 0;
+       ENTER_CRITICAL_SECTION(ipc_task_mutex);
+       while ( (task = g_queue_peek_nth (task_queue, i++)) ) {
+               if (task->api_info->response_id == fd) {
+                       task->api_info->response_id = -1;
+               }
+       }
+       LEAVE_CRITICAL_SECTION(ipc_task_mutex);
 }
+
+
index 45937f4..6c5d0a0 100755 (executable)
@@ -30,7 +30,6 @@
 #include "email-stub-task.h"
 #include "email-stub-main.h"
 
-#include "email-api.h"
 #include "email-debug-log.h"
 
 EXPORT_API void emipc_free_email_task(emipc_email_task *task)
@@ -84,8 +83,8 @@ EXPORT_API bool emipc_run_task(emipc_email_task *task)
        int app_id = task->api_info->app_id;
        int res_id = task->api_info->response_id;
 
-       EM_DEBUG_LOG_SEC("[IPCLib] Processing task: API_ID[%s][0x%x] RES_ID[%d] APP_ID[%d] ", EM_APIID_TO_STR(api_id),\
-                                                                                               api_id, res_id, app_id);
+       EM_DEBUG_LOG_SEC("[IPCLib] Processing task: API_ID[%s][0x%x] RES_ID[%d] APP_ID[%d] ", 
+                                   EM_APIID_TO_STR (api_id), api_id, res_id, app_id);
 
        if (!emipc_execute_api_proxy_to_stub(task->api_info)) {
                EM_DEBUG_EXCEPTION("emipc_execute_api_proxy_to_stub failed");
index 95c17df..b4a278d 100755 (executable)
@@ -35,7 +35,9 @@ EXPORT_API bool emipc_stop_task_thread();
 
 EXPORT_API void *emipc_do_task_thread();
 
-EXPORT_API bool emipc_create_task(unsigned char *task_stream, int response_channel);
+EXPORT_API int emipc_create_task(unsigned char *task_stream, int response_channel);
+
+EXPORT_API void emipc_close_fd_in_task_queue (int fd);
 
 #endif /* _IPC_TASK_MANAGER_H_ */
 
index 5af460c..3fb55aa 100755 (executable)
@@ -24,6 +24,7 @@
 #define __IPC_LIBRARY_H
 
 #include "email-types.h"
+#include "email-internal-types.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -81,6 +82,8 @@ EXPORT_API int emipc_get_parameter_length(HIPC_API input_api_handle, EPARAMETER_
 EXPORT_API int emipc_get_nth_parameter_length(HIPC_API input_api_handle, EPARAMETER_DIRECTION input_parameter_direction, int input_parameter_index);
 
 EXPORT_API int emipc_execute_proxy_task(email_task_type_t input_task_type, void *input_task_parameter);
+
+EXPORT_API int emipc_get_user_name(char **output_user_name);
 #ifdef __cplusplus
 }
 #endif
index c4750ef..ce54513 100755 (executable)
@@ -1,9 +1,9 @@
 prefix=/usr\r
-libdir=@LIB_INSTALL_DIR@\r
+libdir=${prefix}/lib\r
 includedir=${prefix}/include\r
  \r
 Name: email-service library\r
 Description: email-service library 1.0\r
 Version: $version\r
-Libs: -L${libdir} -lemail-storage -lemail-core -lemail-common-use -lemail-ipc -lemail-api -lemail-network -lemail-smime-api\r
+Libs: -L${libdir} -lemail-storage -lemail-core -lemail-common-use -lemail-ipc -lemail-api -lemail-network -lemail-smime-api \r
 Cflags: -I${includedir}/email-service \r
index f29981a..109b7cb 100755 (executable)
@@ -230,8 +230,7 @@ account->outgoing_server_user_name                 = strdup("tom@gmail.com");
 account->sending_password             = strdup("tioimi");
 account->auto_resend_times            = 3;
 account->incoming_server_requires_apop                         = 0;
-account->flag1                        = 2;
-account->flag2                        = 1;
+account->incoming_server_authentication_method                         = 0;
 account->is_preset_account            = 1;
 account->logo_icon_path               = strdup("Logo Icon Path");
 account->options.priority             = 3;
old mode 100644 (file)
new mode 100755 (executable)
index 017d22d..07a0884
@@ -1,5 +1,91 @@
 <manifest>
- <request>
-    <domain name="_"/>
- </request>
+        <define>
+                       <domain name="email-service"/>
+                       <provide>
+                               <label name="email-service::read"/>
+                               <label name="email-service::write"/>
+                       </provide>
+                       <!--
+                       <provide>
+                               <label name="email-service::db"/>
+                       </provide>
+                       -->
+                       <permit>
+                               <smack permit="libaccounts-svc" type="rx"/>
+                               <smack permit="org.tizen.app-selector" type="x"/>
+                               <smack permit="org.tizen.indicator" type="rwx"/>
+                               <smack permit="org.tizen.lockscreen" type="rx"/>
+                               <smack permit="org.tizen.quickpanel" type="rx"/>
+                               <smack permit="secure-storage" type="w"/>
+                               <smack permit="system::share" type="rwx"/>
+                               <smack permit="system::use_internet" type="rwx"/>
+                               <smack permit="sdbd" type="rwx"/>
+                       </permit>
+                       <request>
+                               <smack request="ail::db" type="r"/>
+                               <smack request="badge::db" type="rw"/>
+                               <smack request="connman" type="rw"/>
+                               <smack request="connman::set" type="rw"/>
+                               <smack request="connman::get" type="rw"/>
+                               <smack request="contacts-service" type="w"/>
+                               <smack request="contacts-service::svc" type="r"/>
+                               <smack request="contacts-service::phonelog" type="rw"/>
+                               <smack request="data-provider-master" type="rw"/>
+                               <smack request="data-provider-master::badge" type="w"/>
+                               <smack request="device::app_logging" type="w"/>
+                               <smack request="device::sys_logging" type="w"/>
+                               <smack request="deviced::display" type="rw"/>
+                               <smack request="email::vconf_badge" type="rw"/>
+                               <smack request="email::vconf_latest_mail_id" type="rw"/>
+                               <smack request="email::vconf_multimedia" type="rw"/>
+                               <smack request="email::vconf_notification" type="rw"/>
+                               <smack request="email::vconf_sync_status" type="rw"/>
+                               <smack request="email-service::db" type="rw"/>
+                               <smack request="email-service::write" type="rw"/>
+                               <smack request="msg-service::wappush" type="rw"/>
+                               <smack request="pkgmgr::db" type="rl"/>
+                               <smack request="sdbd" type="rwx"/>
+                               <smack request="secure-storage::email-service" type="rw"/>
+                               <smack request="secure-storage::pkcs12" type="r"/>
+                               <smack request="system::use_internet" type="w"/>
+                               <smack request="system::share" type="wx"/>
+                               <smack request="system::vconf" type="arwxt"/>
+                               <smack request="system::vconf_badge" type="r"/>
+                               <smack request="system::vconf_inhouse" type="rl"/>
+                               <smack request="system::vconf_network" type="rl"/>
+                               <smack request="system::vconf_notification" type="r"/>
+                               <smack request="system::vconf_system" type="rl"/>
+                               <smack request="system::vconf_setting" type="r"/>
+                               <smack request="telephony_framework::api_ps_public" type="rw"/>
+                       </request>
+        </define>
+        <assign>
+                       <filesystem path="/usr/lib/libemail-api.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-api.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-ipc.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-ipc.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-common-use.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-common-use.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-core.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-core.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-core-sound.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-core-sound.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-device.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-device.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-network.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-network.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-storage.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-storage.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-smime.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-smime.so.1.1.0" label="_"/>
+                       <filesystem path="/usr/lib/libemail-smime-api.so.1" label="_"/>
+                       <filesystem path="/usr/lib/libemail-smime-api.so.1.1.0" label="_"/>
+                       <filesystem path="/etc/rc.d/init.d/email-service" label="_" exec_label="none"/>
+                       <filesystem path="/usr/lib/systemd/user/email.service" label="_"/>
+                       <filesystem path="/usr/lib/systemd/user/tizen-middleware.target.wants/email.service" label="_"/>
+                       <filesystem path="/usr/share/dbus-1/services/email-service.service" label="_"/>
+        </assign>
+        <request>
+                       <domain name="email-service"/>
+        </request>
 </manifest>
diff --git a/packaging/email-service.service b/packaging/email-service.service
new file mode 100755 (executable)
index 0000000..529e9a7
--- /dev/null
@@ -0,0 +1,20 @@
+[Unit]
+Description=Email service
+After=graphical.target
+
+[Service]
+User=system
+Group=system
+SmackProcessLabel=email-service
+Type=dbus
+BusName=org.tizen.email_service
+ExecStart=/usr/bin/email-service
+Restart=on-failure
+SuccessExitStatus=0 44
+
+[Socket]
+SocketUser=system
+SocketGroup=system
+
+[Install]
+WantedBy=graphical.target
index d1a2d07..8f05ce9 100755 (executable)
@@ -11,33 +11,29 @@ Source1:    email.service
 Source2:    email-service.manifest
 Source3:    email-service_init_db.sh
 Requires: connman
-Suggests: webkit2-efl
+Requires: gmime
 Requires(post):    /sbin/ldconfig
 Requires(post):    systemd
 Requires(post):    /usr/bin/sqlite3
 Requires(post):    /usr/bin/vconftool
-Requires(post):    libss-client
-Requires(post):    ss-server
+Requires(post):    contacts-service2
+Requires(post):    msg-service
 Requires(preun):   systemd
 Requires(postun):  /sbin/ldconfig
 Requires(postun):  systemd
 BuildRequires:  cmake
+BuildRequires:  pkgconfig(gmime-2.6)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(gthread-2.0)
 BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(vconf-internal-keys)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(dlog)
-BuildRequires:  pkgconfig(db-util)
 BuildRequires:  pkgconfig(dbus-1)
-BuildRequires:  pkgconfig(dbus-glib-1)
 BuildRequires:  pkgconfig(contacts-service2)
 BuildRequires:  pkgconfig(uw-imap-toolkit)
-BuildRequires:  pkgconfig(drm-client)
 BuildRequires:  pkgconfig(openssl)
 BuildRequires:  pkgconfig(alarm-service)
-BuildRequires:  pkgconfig(mm-player)
-BuildRequires:  pkgconfig(mm-session)
 BuildRequires:  pkgconfig(secure-storage)
 BuildRequires:  pkgconfig(notification)
 BuildRequires:  pkgconfig(accounts-svc)
@@ -45,18 +41,18 @@ BuildRequires:  pkgconfig(libsystemd-daemon)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(libcurl)
 BuildRequires:  pkgconfig(libxml-2.0)
-BuildRequires:  pkgconfig(gconf-2.0)
 BuildRequires:  pkgconfig(cert-svc)
 BuildRequires:  pkgconfig(badge)
 BuildRequires:  pkgconfig(feedback)
 BuildRequires:  pkgconfig(capi-appfw-application)
 BuildRequires:  pkgconfig(libwbxml2)
 BuildRequires:  pkgconfig(msg-service)
-BuildRequires:  pkgconfig(pmapi)
-BuildRequires:  pkgconfig(libsmack)
-BuildRequires:  pkgconfig(security-server)
-BuildRequires:  pkgconfig(deviced)
+BuildRequires:  pkgconfig(cynara-client)
 BuildRequires:  pkgconfig(icu-i18n)
+BuildRequires:  pkgconfig(storage)
+BuildRequires:  pkgconfig(capi-network-connection)
+BuildRequires:  pkgconfig(capi-system-device)
+#BuildRequires:  pkgconfig(vasum)
 BuildRequires:  pkgconfig(libtzplatform-config)
 Requires: libtzplatform-config
 
@@ -79,7 +75,7 @@ cp %{SOURCE2} .
 
 %build
 
-export CFLAGS="${CFLAGS} -fPIC -Wall -g -fvisibility=hidden"
+export CFLAGS="${CFLAGS} -fPIC -Wall -g -fvisibility=hidden -fdata-sections -ffunction-sections"
 export CXXFLAGS="${CXXFLAGS} -fPIC -Wall -g -fvisibility=hidden"
 export LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--rpath=%{_libdir} -Wl,--as-needed"
 
@@ -104,6 +100,7 @@ fi
 mkdir -p %{buildroot}/usr/lib/systemd/user/tizen-middleware.target.wants
 install -m 0644 %SOURCE1 %{buildroot}/usr/lib/systemd/user/
 ln -sf ../email.service %{buildroot}/usr/lib/systemd/user/tizen-middleware.target.wants/
+
 install -m 0775 %{SOURCE3} %{buildroot}%{_bindir}/
 
 %post
@@ -151,8 +148,6 @@ systemctl daemon-reload
 %{TZ_SYS_DATA}/email/res/*
 %{_bindir}/email-service
 %{_libdir}/lib*.so.*
-%{_libdir}/libemail-core-sound.so
-%{_libdir}/libemail-core-sound.so.*
 %{_unitdir_user}/email.service
 %{_unitdir_user}/tizen-middleware.target.wants/email.service
 %{_datarootdir}/dbus-1/services/email-service.service
@@ -163,6 +158,4 @@ systemctl daemon-reload
 %files devel
 %{_includedir}/email-service/*.h
 %{_libdir}/lib*.so
-%{_libdir}/libemail-core-sound.so
-%{_libdir}/libemail-core-sound.so.*
 %{_libdir}/pkgconfig/*.pc
index 30f64ec..f12277f 100644 (file)
@@ -1,12 +1,13 @@
+
 [Unit]
 Description=Start the Email service
 
 [Service]
 ExecStart=/usr/bin/email-service
 Nice=5
-EnvironmentFile=/run/tizen-mobile-ui
 OOMScoreAdjust=100
-Restart=always
+MemoryLimit=30M
 
 [Install]
 WantedBy=tizen-middleware.target
+EnvironmentFile=/run/tizen-mobile-ui
index ff46c40..bac3073 100755 (executable)
@@ -10,6 +10,7 @@ CREATE TABLE mail_account_tbl
        sync_disabled                            INTEGER,
        default_mail_slot_size                   INTEGER,
        roaming_option                           INTEGER,
+       color_label                              INTEGER,
        user_display_name                        VARCHAR(31),
        user_email_address                       VARCHAR(129),
        reply_to_address                         VARCHAR(129),
@@ -53,12 +54,19 @@ CREATE TABLE mail_account_tbl
        add_my_address_to_bcc                    INTEGER,
        auto_resend_times                        INTEGER,
        outgoing_server_size_limit               INTEGER,
+       wifi_auto_download                       INTEGER,
        pop_before_smtp                          INTEGER,
        incoming_server_requires_apop            INTEGER,
        smime_type                               INTEGER,
        certificate_path                         VARCHAR(256),
        cipher_type                              INTEGER,
-       digest_type                              INTEGER
+       digest_type                              INTEGER,
+       notification_status                      INTEGER,
+       vibrate_status                           INTEGER,
+       display_content_status                   INTEGER,
+       default_ringtone_status                  INTEGER,
+       alert_ringtone_path                      VARCHAR(256),
+       user_name                                VARCHAR(256)
 );
 CREATE TABLE mail_box_tbl 
 (    
@@ -123,8 +131,8 @@ CREATE TABLE mail_tbl
        full_address_cc                  TEXT,
        full_address_bcc                 TEXT,
        full_address_return              TEXT,
-       email_address_sender             TEXT collation user1,
-       email_address_recipient          TEXT collation user1,
+       email_address_sender             TEXT,
+       email_address_recipient          TEXT,
        alias_sender                     TEXT,
        alias_recipient                  TEXT,
        body_download_status             INTEGER,
@@ -158,8 +166,10 @@ CREATE TABLE mail_tbl
        tag_id                           INTEGER,
        replied_time                     DATETIME,
        forwarded_time                   DATETIME,
+       default_charset                  VARCHAR(257),
        eas_data_length                  INTEGER,
        eas_data                         BLOB,
+       user_name                        VARCHAR(256),
        FOREIGN KEY(account_id)          REFERENCES mail_account_tbl(account_id)
 );
 CREATE TABLE mail_attachment_tbl 
@@ -167,6 +177,7 @@ CREATE TABLE mail_attachment_tbl
        attachment_id                            INTEGER PRIMARY KEY,
        attachment_name                          VARCHAR(257),
        attachment_path                          VARCHAR(257),
+       content_id                               VARCHAR(257),
        attachment_size                          INTEGER,
        mail_id                                  INTEGER,
        account_id                               INTEGER,
@@ -185,7 +196,8 @@ CREATE TABLE mail_partial_body_activity_tbl
        activity_id                     INTEGER PRIMARY KEY,
        activity_type                   INTEGER,
        mailbox_id                      INTEGER,
-       mailbox_name                    VARCHAR(4000)
+       mailbox_name                    VARCHAR(4000),
+       multi_user_name                 VARCHAR(64)
 );
 CREATE TABLE mail_meeting_tbl
 (
@@ -247,6 +259,16 @@ CREATE VIRTUAL TABLE mail_text_tbl USING fts4
        mailbox_id                       INTEGER,
        body_text                        TEXT
 );
+CREATE TABLE mail_auto_download_activity_tbl
+(
+       activity_id                     INTEGER PRIMARY KEY,
+       status                                                          INTEGER,
+       account_id                                              INTEGER,
+       mail_id                         INTEGER,
+       server_mail_id                  INTEGER,
+       mailbox_id                      INTEGER,
+       multi_user_name              VARCHAR(64)
+);
 CREATE UNIQUE INDEX mail_account_idx1 ON mail_account_tbl (account_id);
 CREATE UNIQUE INDEX mail_box_idx1 ON mail_box_tbl (mailbox_id);
 CREATE UNIQUE INDEX mail_read_mail_uid_idx1 ON mail_read_mail_uid_tbl (account_id, mailbox_id, local_uid, mailbox_name, server_uid);
@@ -264,3 +286,7 @@ CREATE TRIGGER update_flags_flagged_field UPDATE OF flags_flagged_field ON mail_
   BEGIN
     UPDATE mail_read_mail_uid_tbl SET flags_flagged_field = new.flags_flagged_field WHERE local_uid = old.mail_id;
   END;
+CREATE TRIGGER update_mailbox_id_field UPDATE OF mailbox_id ON mail_tbl 
+  BEGIN
+    UPDATE mail_auto_download_activity_tbl SET mailbox_id = new.mailbox_id WHERE mail_id = old.mail_id;
+  END;
index 3826c96..9009483 100755 (executable)
@@ -13,6 +13,7 @@ MESSAGE(">>> Build type: ${CMAKE_BUILD_TYPE}")
 
 SET(VISIBILITY "-DEXPORT_API=\"__attribute__((visibility(\\\"default\\\")))\"")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VISIBILITY} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--gc-sections")
 
 ##########################################################
 # Define Test App
@@ -28,6 +29,7 @@ SET(TEST-APP-SRCS
        ${CMAKE_SOURCE_DIR}/utilities/test-application/testapp-rule.c
        ${CMAKE_SOURCE_DIR}/utilities/test-application/testapp-thread.c
        ${CMAKE_SOURCE_DIR}/utilities/test-application/testapp-others.c
+#      ${CMAKE_SOURCE_DIR}/utilities/test-application/testapp-gmime.c
 )
 
 INCLUDE_DIRECTORIES(
@@ -40,16 +42,14 @@ INCLUDE_DIRECTORIES(
 )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(test_app_pkgs REQUIRED glib-2.0 gthread-2.0 vconf db-util uw-imap-toolkit drm-client pmapi)
-
-#pkg_check_modules(test_app_pkgs REQUIRED glib-2.0 gthread-2.0 vconf db-util uw-imap-toolkit contacts-service2 drm-client pmapi)
+pkg_check_modules(test_app_pkgs REQUIRED glib-2.0 gthread-2.0 vconf uw-imap-toolkit contacts-service2)
 
 FOREACH(flag ${test_app_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 ADD_EXECUTABLE(email-test-app ${TEST-APP-SRCS})
 TARGET_LINK_LIBRARIES(email-test-app ${test_app_pkgs_LDFLAGS} email-api email-smime-api)
diff --git a/utilities/test-application/include/testapp-gmime.h b/utilities/test-application/include/testapp-gmime.h
new file mode 100755 (executable)
index 0000000..0aeafee
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+*  email-service
+*
+* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+
+
+#ifndef EMAIL_TEST_GMIME_H
+#define EMAIL_TEST_GMIME_H
+
+/* export API */
+void testapp_gmime_main(void);
+
+#endif
+
index 27cbfdd..de02f6b 100755 (executable)
@@ -44,7 +44,6 @@
 #ifdef __FEATURE_USE_GMIME__
 #include "testapp-gmime.h"
 #endif /* __FEATURE_USE_GMIME__ */
-#include "db-util.h"
 
 /* function prototype */
 static void testapp_system_signal_handler (int signal_number);
@@ -184,19 +183,17 @@ int main (int argc, char *argv[])
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
 
        if ( testapp_initialize_testing() == FALSE ) {
-               testapp_print ("email-serivce is not ready\n");
+               testapp_print ("email-service is not ready\n");
                exit(0);
        }
 
        while (go_to_loop) {
                testapp_show_menu (EMAIL_MAIN_MENU);
                testapp_show_prompt (EMAIL_MAIN_MENU);
-
-               result_from_scanf = scanf ("%d", &menu_number);
-
+               if (0 >= scanf ("%d", &menu_number))
+                       testapp_print("Invalid input");
                go_to_loop = testapp_interpret_command (menu_number);
        }
 
index c1b121b..1cc5d1f 100755 (executable)
@@ -63,7 +63,6 @@ gboolean  testapp_create_account_object(email_account_t **result_account)
        char id_string[100] = { 0, }, password_string[1000] = { 0, }, address_string[100]  = { 0, };
        char accesss_token[1000] = { 0, }, refresh_token[1000] = { 0, };
        int samsung3g_account_index;
-       int result_from_scanf = 0;
        int account_type;
 
        testapp_print("1. Gawab\n");
@@ -83,14 +82,17 @@ gboolean  testapp_create_account_object(email_account_t **result_account)
        testapp_print("16. mopera\n");
        testapp_print("Choose server type: ");
 
-       result_from_scanf = scanf("%d",&account_type);
+
+       if (0 >= scanf("%d",&account_type))
+               testapp_print("Invalid input. ");
 
        switch(account_type) {
                case 4 : 
                case 5 :
                        do {
                                testapp_print("Enter your account index [1~10] : ");
-                               result_from_scanf = scanf("%d",&samsung3g_account_index);
+                               if (0 >= scanf("%d",&samsung3g_account_index))
+                                       testapp_print("Invalid input. ");
                        }while( samsung3g_account_index > 10 || samsung3g_account_index < 1);
                        sprintf(id_string, "test%02d", samsung3g_account_index);
                        sprintf(address_string, "test%02d@streaming.s3glab.net", samsung3g_account_index);
@@ -98,26 +100,32 @@ gboolean  testapp_create_account_object(email_account_t **result_account)
                        break;
                case 14 :
                        testapp_print("Enter email address : ");
-                       result_from_scanf = scanf("%s", address_string);
+                       if (0 >= scanf("%s", address_string))
+                               testapp_print("Invalid input. ");
                        strcpy(id_string, address_string);
 
                        testapp_print("Enter access token : ");
-                       result_from_scanf = scanf("%s", accesss_token);
+                       if (0 >= scanf("%s", accesss_token))
+                               testapp_print("Invalid input. ");
 
                        testapp_print("Enter refresh token : ");
-                       result_from_scanf = scanf("%s", refresh_token);
+                       if (0 >= scanf("%s", refresh_token))
+                               testapp_print("Invalid input. ");
 
                        snprintf(password_string, 100, "%s\001%s\001", accesss_token, refresh_token);
                        break;
                default:
                        testapp_print("Enter email address : ");
-                       result_from_scanf = scanf("%s", address_string);
+                       if (0 >= scanf("%s", address_string))
+                               testapp_print("Invalid input. ");
 
                        testapp_print("Enter id : ");
-                       result_from_scanf = scanf("%s", id_string);
+                       if (0 >= scanf("%s", id_string))
+                               testapp_print("Invalid input. ");
 
                        testapp_print("Enter password_string : ");
-                       result_from_scanf = scanf("%s", password_string);
+                       if (0 >= scanf("%s", password_string))
+                               testapp_print("Invalid input. ");
                        break;
        }
 
@@ -144,6 +152,7 @@ gboolean  testapp_create_account_object(email_account_t **result_account)
        account->incoming_server_requires_apop           = 0;
        account->incoming_server_authentication_method   = 0;
        account->logo_icon_path                          = NULL;
+       account->color_label                             = (128 << 16) | (128 << 8) | (128);
        account->user_data                               = malloc (data_length);
        memcpy( account->user_data, (void*) &data, data_length );
        account->user_data_length                        = data_length;
@@ -390,19 +399,18 @@ static gboolean testapp_test_add_account_with_validation()
 
 static gboolean testapp_test_update_account()
 {
-       int result_from_scanf = 0;
        int account_id;
        email_account_t *account = NULL;
-       char account_name[256];
+       //char account_name[256];
        int err = EMAIL_ERROR_NONE;
-       char signature[100] = {0};
-       char user_email_address[256] = {0,};
-       int add_my_address_to_bcc = 0;
-       int account_svc_id = 0, with_validation = 0;
-
+       //char signature[100] = {0};
+       //char user_email_address[256] = {0,};
+       //int add_my_address_to_bcc = 0;
+       int with_validation = 0; //account_svc_id = 0, 
        
-       testapp_print("\n>> Enter Account No: ");
-       result_from_scanf = scanf("%d",&account_id);
+       testapp_print("\n>> Enter Account ID: ");
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        if( (err = email_get_account(account_id, GET_FULL_DATA,&account)) != EMAIL_ERROR_NONE) {
                testapp_print ("email_get_account failed - %d\n", err);
@@ -410,54 +418,56 @@ static gboolean testapp_test_update_account()
        }
 
        testapp_print ("email_get_account result account_name - %s \n", account->account_name);
+       testapp_print ("email_get_account result address - %s \n", account->user_email_address);
        testapp_print ("email_get_account result signature - %s \n", account->options.signature);
-
-#ifdef __FEATURE_AUTO_POLLING__
        testapp_print ("email_get_account result check_interval - %d \n", account->check_interval);
-#endif
-
-       testapp_print("\n Enter new Account name:");
-       result_from_scanf = scanf("%s",account_name);
-
        
-       testapp_print("\n Enter new email addr:");
-       result_from_scanf = scanf("%s",user_email_address);
-#ifdef __FEATURE_AUTO_POLLING__
        testapp_print("\n Enter new check interval (in mins):");
-       result_from_scanf = scanf("%d",&(account->check_interval));
-
+       if (0 >= scanf("%d",&(account->check_interval)))
+               testapp_print("Invalid input. ");
+/*
        testapp_print("\n Enter new peak interval (in mins):");
-       result_from_scanf = scanf("%d",&(account->peak_interval));
+       if (0 >= scanf("%d",&(account->peak_interval));
 
        testapp_print("\n Enter new peak days:");
-       result_from_scanf = scanf("%d",&(account->peak_days));
+       if (0 >= scanf("%d",&(account->peak_days));
 
        testapp_print("\n Enter new peak start time:");
-       result_from_scanf = scanf("%d",&(account->peak_start_time));
+       if (0 >= scanf("%d",&(account->peak_start_time));
 
        testapp_print("\n Enter new peak end time:");
-       result_from_scanf = scanf("%d",&(account->peak_end_time));
-#endif
+       if (0 >= scanf("%d",&(account->peak_end_time));
+*/
+
+/*
+       testapp_print("\n Enter new Account name:");
+       if (0 >= scanf("%s",account_name);
+
+       testapp_print("\n Enter new email addr:");
+       if (0 >= scanf("%s",user_email_address);
+
        testapp_print("\n Enter new signature:");
-       result_from_scanf = scanf("%s",signature);
+       if (0 >= scanf("%s",signature);
 
        testapp_print("\n>> Enter add_my_address_to_bcc:(0:off, 1:on) ");
-       result_from_scanf = scanf("%d",&add_my_address_to_bcc);
+       if (0 >= scanf("%d",&add_my_address_to_bcc);
 
        testapp_print("\n>> Enter account_svc_id: ");
-       result_from_scanf = scanf("%d",&account_svc_id);
+       if (0 >= scanf("%d",&account_svc_id);
 
        testapp_print("\n>> With validation ? (0: No, 1:Yes) ");
-       result_from_scanf = scanf("%d",&with_validation);
-
+       if (0 >= scanf("%d",&with_validation);
+*/
     if( account )  {
-               account->account_name = strdup(account_name);
                testapp_print("\n Assigning New Account name: (%s)", account->account_name);
+               testapp_print("\n Assigning New Signature: (%s)\n", account->options.signature);
+               /*
+               account->account_name = strdup(account_name);
                account->user_email_address = strdup(user_email_address);
                account->options.signature = strdup(signature);
-               testapp_print("\n Assigning New Signature: (%s)\n", account->options.signature);
                account->options.add_my_address_to_bcc = add_my_address_to_bcc;
                account->account_svc_id = account_svc_id;
+               */
 
                if(with_validation) {
                        if((err = email_update_account_with_validation(account_id, account)) != EMAIL_ERROR_NONE){
@@ -482,10 +492,10 @@ static gboolean testapp_test_delete_account ()
        int account_id;
        email_account_t *account=NULL;
        int err = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
 
        testapp_print("\n>> Enter Account No: ");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
 /* sowmya.kr, 281209 Adding signature to options in email_account_t changes */
        if( (err = email_get_account(account_id, WITHOUT_OPTION,&account)) < 0) {
@@ -531,16 +541,17 @@ static gboolean testapp_test_validate_account ()
 
 static gboolean testapp_test_cancel_validate_account ()
 {
-       int result_from_scanf = 0;
        int account_id = 0;
        int err_code = EMAIL_ERROR_NONE;
        unsigned account_handle = 0;
 
        testapp_print("\n > Enter account_id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter handle: ");
-       result_from_scanf = scanf("%d", &account_handle);
+       if (0 >= scanf("%d", &account_handle))
+               testapp_print("Invalid input. ");
 
        err_code = email_cancel_job(account_id, account_handle, EMAIL_CANCELED_BY_USER);
        if(err_code == 0)
@@ -553,12 +564,12 @@ static gboolean testapp_test_cancel_validate_account ()
 
 static gboolean testapp_test_get_account()
 {
-       int result_from_scanf = 0;
        int account_id;
        email_account_t *account=NULL;
        int err_code = EMAIL_ERROR_NONE;
        testapp_print("\n>> Enter Account No: ");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        typedef struct {
                int is_preset_account;
@@ -595,11 +606,16 @@ static gboolean testapp_test_get_account()
                        "digest_type %d\n"
                        "auto_resend_times %d\n"
                        "roaming_option %d\n"
-
                        "peak_interval %d\n"
                        "peak_days %d\n"
                        "peak_start_time %d\n"
                        "peak_end_time %d\n"
+                       "color_label %d\n"
+                       "notification_status %d\n"
+                       "vibrate_status %d\n"
+                       "display_content_status %d\n"
+                       "default_ringtone_status %d\n"
+                       "alert_ringtone_path %s\n"
                ,
                account->account_name,
                account->user_email_address,
@@ -622,7 +638,13 @@ static gboolean testapp_test_get_account()
                account->peak_interval,
                account->peak_days,
                account->peak_start_time,
-               account->peak_end_time
+               account->peak_end_time,
+               account->color_label,
+               account->options.notification_status,
+               account->options.vibrate_status,
+               account->options.display_content_status,
+               account->options.default_ringtone_status,
+               account->options.alert_ringtone_path
                );
 
        err_code = email_free_account(&account, 1);
@@ -719,12 +741,12 @@ static gboolean testapp_test_get_account_list ()
 static gboolean testapp_test_update_check_interval()
 {
        int account_id = 0;
-       int result_from_scanf = 0;
        int err_code = EMAIL_ERROR_NONE;
        email_account_t *account = NULL;
 
        testapp_print("\n Enter account id :");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        if( (err_code = email_get_account(account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account)) != EMAIL_ERROR_NONE) {
                testapp_print ("email_get_account failed [%d]\n", err_code);
@@ -732,7 +754,8 @@ static gboolean testapp_test_update_check_interval()
        }
 
        testapp_print("\n Enter new check interval (in mins):");
-       result_from_scanf = scanf("%d",&(account->check_interval));
+       if (0 >= scanf("%d",&(account->check_interval)))
+               testapp_print("Invalid input. ");
 
        if((err_code = email_update_account(account_id, account)) != EMAIL_ERROR_NONE) {
                testapp_print ("email_update_account failed [%d]\n", err_code);
@@ -750,7 +773,7 @@ FINISH_OFF:
 
 static gboolean testapp_test_backup_account()
 {
-       char *file_name = "accounts_file";
+       char *file_name = "/opt/usr/data/email/accounts_file";
        int error_code;
        error_code = email_backup_accounts_into_secure_storage(file_name);
        testapp_print("\n email_backup_accounts_into_secure_storage returned [%d]\n",error_code);
@@ -758,7 +781,7 @@ static gboolean testapp_test_backup_account()
 }
 static gboolean testapp_test_restore_account()
 {
-       char *file_name = "accounts_file";
+       char *file_name = "/opt/usr/data/email/accounts_file";
        int error_code;
        error_code = email_restore_accounts_from_secure_storage(file_name);
        testapp_print("\n email_restore_accounts_from_secure_storage returned [%d]\n",error_code);
@@ -767,30 +790,56 @@ static gboolean testapp_test_restore_account()
 
 static gboolean testapp_test_get_password_length_of_account()
 {
-       int result_from_scanf = 0;
-       int error_code, password_length, account_id;
+       int password_length, account_id;
 
        testapp_print("\n input account id\n");
-       result_from_scanf = scanf("%d", &account_id);
-       error_code = email_get_password_length_of_account(account_id, &password_length);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
+       email_get_password_length_of_account(account_id, EMAIL_GET_INCOMING_PASSWORD_LENGTH, &password_length);
        testapp_print("testapp_test_get_password_length_of_account returned [%d]\n",password_length);
        return FALSE;
 }
 
-static gboolean testapp_test_query_server_info()
+static gboolean testapp_test_update_notification()
 {
-       int result_from_scanf = 0;
        int error_code;
-       char domain_name[255];
-       email_server_info_t *result_server_info;
+       int account_id = 0;
+       int t_mail_count = 0;
+       int input_from_eas = 0;
+       int unread_mail_count = 0;
+
+       testapp_print("\n Input account ID:\n");
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n Input mail count:\n");
+       if (0 >= scanf("%d", &t_mail_count))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n Input unread mail count:\n");
+       if (0 >= scanf("%d", &unread_mail_count))
+               testapp_print("Invalid input. ");
 
-       testapp_print("\n input domain name\n");
-       result_from_scanf = scanf("%s", domain_name);
+       testapp_print("\n Input From eas:\n");
+       if (0 >= scanf("%d", &input_from_eas))
+               testapp_print("Invalid input. ");
 
-       error_code = email_query_server_info(domain_name, &result_server_info);
-       testapp_print("email_query_server_info returned [%d]\n",error_code);
-       if(error_code == EMAIL_ERROR_NONE)
-               testapp_print("service_name [%s]\n", result_server_info->service_name);
+       error_code = email_update_notification_bar(account_id, t_mail_count, unread_mail_count, input_from_eas);
+       testapp_print("email_update_notification_bar returned [%d]\n",error_code);
+       return FALSE;
+}
+
+static gboolean testapp_test_clear_notification()
+{
+       int account_id = 0;
+       int error_code;
+
+       testapp_print("\n Input account ID:\n");
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
+
+       error_code = email_clear_notification_bar(account_id);
+       testapp_print("email_clear_notification_bar returned [%d]\n",error_code);
        return FALSE;
 }
 
@@ -798,20 +847,20 @@ static gboolean testapp_test_clear_all_notification()
 {
        int error_code;
 
-       error_code = email_clear_all_notification_bar();
-       testapp_print("email_clear_all_notification_bar returned [%d]\n",error_code);
+       error_code = email_clear_notification_bar(ALL_ACCOUNT);
+       testapp_print("email_clear_notification_bar returned [%d]\n",error_code);
        return FALSE;
 }
 
 static gboolean testapp_test_save_default_account_id()
 {
-       int result_from_scanf = 0;
        int error_code;
        int account_id = 0;
 
        testapp_print ("\nInput default account id : ");
 
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        error_code = email_save_default_account_id(account_id);
 
@@ -833,16 +882,17 @@ static gboolean testapp_test_load_default_account_id()
 
 static gboolean testapp_test_add_certificate()
 {
-       int result_from_scanf = 0;
        int ret = 0;
        char save_name[50] = {0, };
        char certificate_path[255] = {0, };
 
        testapp_print("Input cert path : ");
-       result_from_scanf = scanf("%s", certificate_path);      
+       if (0 >= scanf("%s", certificate_path))
+               testapp_print("Invalid input. ");
 
        testapp_print("Input cert email-address : ");
-       result_from_scanf = scanf("%s", save_name);
+       if (0 >= scanf("%s", save_name))
+               testapp_print("Invalid input. ");
 
        testapp_print("cert path : [%s]", certificate_path);
        testapp_print("email-address : [%s]", save_name);
@@ -859,13 +909,13 @@ static gboolean testapp_test_add_certificate()
 
 static gboolean testapp_test_get_certificate()
 {
-       int result_from_scanf = 0;
        int ret = 0;
        char save_name[20] = {0, };
        email_certificate_t *certificate = NULL;
 
        testapp_print("Input cert email-address : ");
-       result_from_scanf = scanf("%s", save_name);
+       if (0 >= scanf("%s", save_name))
+               testapp_print("Invalid input. ");
 
        ret = email_get_certificate(save_name, &certificate);
        if (ret != EMAIL_ERROR_NONE) {
@@ -893,12 +943,12 @@ static gboolean testapp_test_get_certificate()
 
 static gboolean testapp_test_delete_certificate()
 {
-       int result_from_scanf = 0;
        int ret = 0;
        char save_name[20] = {0, };
 
        testapp_print("Input cert email-address : ");
-       result_from_scanf = scanf("%s", save_name);
+       if (0 >= scanf("%s", save_name))
+               testapp_print("Invalid input. ");
 
        ret = email_delete_certificate(save_name);
        if (ret != EMAIL_ERROR_NONE) {
@@ -936,13 +986,13 @@ static gboolean testapp_test_add_account()
 
 static gboolean testapp_test_update_peak_schedule()
 {
-       int result_from_scanf = 0;
        int account_id;
        email_account_t *account = NULL;
        int err = EMAIL_ERROR_NONE;
 
        testapp_print("\n>> Enter Account No: ");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        if( (err = email_get_account(account_id, GET_FULL_DATA, &account)) != EMAIL_ERROR_NONE) {
                testapp_print ("email_get_account failed [%d]\n", err);
@@ -952,27 +1002,32 @@ static gboolean testapp_test_update_peak_schedule()
        testapp_print ("old check_interval - %d \n", account->check_interval);
 
        testapp_print("\n Enter new check interval (in mins):");
-       result_from_scanf = scanf("%d",&(account->check_interval));
+       if (0 >= scanf("%d",&(account->check_interval)))
+               testapp_print("Invalid input. ");
 
        testapp_print ("old peak_interval - %d \n", account->peak_interval);
 
        testapp_print("\n Enter new peak interval (in mins):");
-       result_from_scanf = scanf("%d",&(account->peak_interval));
+       if (0 >= scanf("%d",&(account->peak_interval)))
+               testapp_print("Invalid input. ");
 
        testapp_print ("old peak_days - %d \n", account->peak_days);
 
        testapp_print("\n Enter new peak days:");
-       result_from_scanf = scanf("%d",&(account->peak_days));
+       if (0 >= scanf("%d",&(account->peak_days)))
+               testapp_print("Invalid input. ");
 
        testapp_print ("old peak_start_time - %d \n", account->peak_start_time);
 
        testapp_print("\n Enter new peak start time:");
-       result_from_scanf = scanf("%d",&(account->peak_start_time));
+       if (0 >= scanf("%d",&(account->peak_start_time)))
+               testapp_print("Invalid input. ");
 
        testapp_print ("old peak_end_time - %d \n", account->peak_start_time);
 
        testapp_print("\n Enter new peak end time:");
-       result_from_scanf = scanf("%d",&(account->peak_end_time));
+       if (0 >= scanf("%d",&(account->peak_end_time)))
+               testapp_print("Invalid input. ");
 
        if( account )  {
                if((err = email_update_account(account_id, account)) != EMAIL_ERROR_NONE) {
@@ -1033,39 +1088,43 @@ static gboolean testapp_test_interpret_command (int selected_number)
                        testapp_test_get_password_length_of_account();
                        break;
 
-               case 12:
-                       testapp_test_query_server_info();
+               case 13:
+                       testapp_test_update_notification();
                        break;
 
-               case 13:
+               case 14:
+                       testapp_test_clear_notification();
+                       break;
+
+               case 15:
                        testapp_test_clear_all_notification();
                        break;
 
-               case 14:
+               case 16:
                        testapp_test_save_default_account_id();
                        break;
 
-               case 15:
+               case 17:
                        testapp_test_load_default_account_id();
                        break;
 
-               case 16:
+               case 18:
                        testapp_test_add_certificate();
                        break;
 
-               case 17:
+               case 19:
                        testapp_test_get_certificate();
                        break;
 
-               case 18:
+               case 20:
                        testapp_test_delete_certificate();
                        break;
 
-               case 19:
+               case 21:
                        testapp_test_add_account();
                        break;
 
-               case 20:
+               case 22:
                        testapp_test_update_peak_schedule();
                        break;
 
@@ -1084,13 +1143,13 @@ void testapp_account_main ()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
        
        while (go_to_loop) {
                testapp_show_menu (EMAIL_ACCOUNT_MENU);
                testapp_show_prompt (EMAIL_ACCOUNT_MENU);
-                       
-               result_from_scanf = scanf ("%d", &menu_number);
+
+               if (0 >= scanf("%d", &menu_number))
+                       testapp_print("Invalid input");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }       
diff --git a/utilities/test-application/testapp-gmime.c b/utilities/test-application/testapp-gmime.c
new file mode 100644 (file)
index 0000000..8493160
--- /dev/null
@@ -0,0 +1,188 @@
+#include <glib.h>
+#include <gmime/gmime.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "testapp-utility.h"
+#include "testapp-gmime.h"
+
+
+#define TEST_EML_PATH "/tmp/test.eml"
+
+static void print_depth(int depth)
+{
+       int i;
+       for (i = 0; i < depth; i++)
+               testapp_print("    \n");
+}
+
+static void print_mime_struct(GMimeObject *part, int depth)
+{
+       const GMimeContentType *type;
+       GMimeMultipart *multipart;
+       GMimeObject *subpart;
+       int i, n;
+
+       print_depth(depth);
+       type = g_mime_object_get_content_type(part);
+       testapp_print("Content-Type: %s/%s\n", type->type, type->subtype);
+
+       if (GMIME_IS_MULTIPART(part)) {
+               multipart = (GMimeMultipart *)part;
+
+               n = g_mime_multipart_get_count(multipart);
+               for (i = 0; i < n; i++) {
+                       subpart = g_mime_multipart_get_part(multipart, i);
+                       print_mime_struct(subpart, depth + 1);
+                       g_object_unref(subpart);
+               }
+       }
+}
+
+static void
+test_eml_parsing_foreach_callback(GMimeObject *parent, GMimeObject *part, gpointer user_data)
+{
+       int *cnt= user_data;
+
+       (*cnt)++;
+
+       if (GMIME_IS_PART(part)) {
+               testapp_print ("Part\n");
+               GMimePart *leaf_part = NULL;
+               leaf_part = (GMimePart *)part;
+
+               testapp_print("Content ID:%s\n", g_mime_part_get_content_id(leaf_part));
+               testapp_print("Description:%s\n", g_mime_part_get_content_description(leaf_part));
+               testapp_print("MD5:%s\n", g_mime_part_get_content_md5(leaf_part));
+               testapp_print("Location:%s\n", g_mime_part_get_content_location(leaf_part));
+
+               GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
+               switch(enc) {
+               case GMIME_CONTENT_ENCODING_DEFAULT:
+                       testapp_print("Encoding:ENCODING_DEFAULT\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_7BIT:
+                       testapp_print("Encoding:ENCODING_7BIT\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_8BIT:
+                       testapp_print("Encoding:ENCODING_8BIT\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_BINARY:
+                       testapp_print("Encoding:ENCODING_BINARY\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_BASE64:
+                       testapp_print("Encoding:ENCODING_BASE64\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
+                       testapp_print("Encoding:ENCODING_QUOTEDPRINTABLE\n");
+                       break;
+               case GMIME_CONTENT_ENCODING_UUENCODE:
+                       testapp_print("Encoding:ENCODING_UUENCODE\n");
+                       break;
+
+               default:
+                       break;
+               }
+
+               testapp_print("File name:%s\n\n\n", g_mime_part_get_filename(leaf_part));
+               GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
+
+               if (data) {
+                       char tmp_path[50] = {0,};
+                       snprintf(tmp_path, sizeof(tmp_path), "/tmp/gmime_content%d", *cnt);
+
+                       int fd;
+                       if ((fd = open(tmp_path, O_RDWR|O_CREAT, 0)) == -1) {
+                               testapp_print ("open fail\n");
+                       }
+                       GMimeStream *out_stream;
+                       out_stream = g_mime_stream_fs_new(fd);
+                       g_mime_data_wrapper_write_to_stream(data, out_stream);
+                       g_object_unref(out_stream);
+               }
+       }
+}
+
+static gboolean testapp_test_gmime_eml_parsing(void)
+{
+       GMimeMessage *message;
+       GMimeParser *parser;
+       GMimeStream *stream;
+       int fd;
+       int count = 0;
+
+       if ((fd = open(TEST_EML_PATH, O_RDONLY, 0)) == -1) {
+               testapp_print ("open fail\n");
+               return 0;
+       }
+
+       /* init the gmime library */
+       g_mime_init(0);
+
+       /* create a stream to read from the file descriptor */
+       stream = g_mime_stream_fs_new(fd);
+
+       /* create a new parser object to parse the stream */
+       parser = g_mime_parser_new_with_stream(stream);
+
+       /* unref the stream (parser owns a ref, so this object does not actually get free'd until we destroy the parser) */
+       g_object_unref(stream);
+
+       /* parse the message from the stream */
+       message = g_mime_parser_construct_message(parser);
+
+       /* free the parser (and the stream) */
+       g_object_unref(parser);
+
+       testapp_print("Sender:%s\n", g_mime_message_get_sender(message));
+       testapp_print("Reply To:%s\n", g_mime_message_get_reply_to(message));
+       testapp_print("Subject:%s\n", g_mime_message_get_subject(message));
+       testapp_print("Date:%s\n", g_mime_message_get_date_as_string(message));
+       testapp_print("Message ID:%s\n", g_mime_message_get_message_id(message));
+
+       GMimeObject *po = (GMimeObject *)message;
+       testapp_print("Header String:%s\n\n\n\n", g_mime_header_list_to_string(po->headers));
+
+       g_mime_message_foreach(message, test_eml_parsing_foreach_callback, &count);
+}
+
+static gboolean testapp_test_interpret_command (int menu_number)
+{
+       gboolean go_to_loop = TRUE;
+
+       switch (menu_number) {
+               case 1:
+                       testapp_test_gmime_eml_parsing();
+                       break;
+
+               case 0:
+                       go_to_loop = FALSE;
+                       break;
+               default:
+                       break;
+       }
+
+       return go_to_loop;
+}
+
+void testapp_gmime_main()
+{
+       gboolean go_to_loop = TRUE;
+       int menu_number = 0;
+
+       while (go_to_loop) {
+               testapp_show_menu (EMAIL_GMIME_MENU);
+               testapp_show_prompt (EMAIL_GMIME_MENU);
+
+               result_from_scanf = scanf ("%d", &menu_number);
+
+               go_to_loop = testapp_test_interpret_command (menu_number);
+       }
+}
index add534f..00fb137 100755 (executable)
@@ -112,24 +112,27 @@ static void testapp_test_print_mail_list_item(email_mail_list_item_t *mail_list_
 
 static gboolean testapp_add_mail_for_sending (int *result_mail_id)
 {
-       int                    result_from_scanf = 0;
        int                    i = 0;
        int                    account_id = 0;
        int                    err = EMAIL_ERROR_NONE;
        int                    smime_type = 0;
-       char                   receipient_address[300] = { 0 , };
+       char                   recipient_address[300] = { 0 , };
        char                   from_address[300] = { 0 , };
+       char                   passpharse[300] = {0, };
        const char            *body_file_path = MAIL_TEMP_BODY;
+       
        email_account_t       *account_data = NULL;
        email_mailbox_t       *mailbox_data = NULL;
        email_mail_data_t     *test_mail_data = NULL;
        FILE                  *body_file;
 
        testapp_print("\n > Enter account id : ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
-       testapp_print("\n > Enter receipient address : ");
-       result_from_scanf = scanf("%s", receipient_address);
+       testapp_print("\n > Enter recipient address : ");
+       if (0 >= scanf("%s", recipient_address))
+               testapp_print("Invalid input. ");
 
        email_get_account(account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_data);
 
@@ -148,7 +151,7 @@ static gboolean testapp_add_mail_for_sending (int *result_mail_id)
        test_mail_data->mailbox_id             = mailbox_data->mailbox_id;
        test_mail_data->mailbox_type           = mailbox_data->mailbox_type;
        test_mail_data->full_address_from      = strdup(from_address);
-       test_mail_data->full_address_to        = strdup(receipient_address);
+       test_mail_data->full_address_to        = strdup(recipient_address);
        test_mail_data->subject                = strdup("Read receipt request from TIZEN");
        test_mail_data->remaining_resend_times = 3;
        test_mail_data->report_status          = EMAIL_MAIL_REQUEST_DSN | EMAIL_MAIL_REQUEST_MDN;
@@ -168,10 +171,18 @@ static gboolean testapp_add_mail_for_sending (int *result_mail_id)
        fflush(body_file);
        fclose(body_file);
 
-       testapp_print(" > Select smime? [0: Normal, 1: sign, 2: Encrpyt, 3: sing + encrypt] : ");
-       result_from_scanf = scanf("%d", &smime_type);
+       testapp_print(" > Select smime? [0: Normal, 1: sign, 2: Encrpyt, 3: sing + encrypt, 4: pgp sign, 5: pgp encrypted, 6: pgp sign + encrypt] : ");
+       if (0 >= scanf("%d", &smime_type))
+               testapp_print("Invalid input. ");
        test_mail_data->smime_type = smime_type;
 
+       if (smime_type >= EMAIL_PGP_SIGNED) {
+               testapp_print(" > passpharse : ");
+               if (0 >= scanf("%s", passpharse))
+                       testapp_print("Invalid input. ");
+               test_mail_data->pgp_password = strdup(passpharse);
+       }
+
        if((err = email_add_mail(test_mail_data, NULL, 0, NULL, 0)) != EMAIL_ERROR_NONE)
                testapp_print("email_add_mail failed. [%d]\n", err);
        else
@@ -191,7 +202,6 @@ static gboolean testapp_add_mail_for_sending (int *result_mail_id)
 
 static gboolean testapp_test_add_mail (int *result_mail_id)
 {
-       int                    result_from_scanf = 0;
        int                    i = 0;
        int                    account_id = 0;
        int                    mailbox_id = 0;
@@ -208,12 +218,13 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
        FILE                  *body_file;
  
        testapp_print("\n > Enter account id : ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
-       
        memset(arg, 0x00, 50);
        testapp_print("\n > Enter mailbox id : ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        email_get_mailbox_by_mailbox_id(mailbox_id, &mailbox_data);
 
@@ -221,7 +232,8 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
        memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
        
        testapp_print("\n From EAS? [0/1]> ");
-       result_from_scanf = scanf("%d", &from_eas);
+       if (0 >= scanf("%d", &from_eas))
+               testapp_print("Invalid input. ");
 
        test_mail_data->account_id             = account_id;
        test_mail_data->save_status            = 1;
@@ -256,11 +268,13 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
        fclose(body_file);
 
        testapp_print(" > Select smime? [0: Normal, 1: sign, 2: Encrypt, 3: sing + encrypt] : ");
-       result_from_scanf = scanf("%d", &smime_type);
+       if (0 >= scanf("%d", &smime_type))
+               testapp_print("Invalid input. ");
        test_mail_data->smime_type = smime_type;
        
        testapp_print(" > How many file attachment? [>=0] : ");
-       result_from_scanf = scanf("%d",&attachment_count);
+       if (0 >= scanf("%d",&attachment_count))
+               testapp_print("Invalid input. ");
        
        test_mail_data->attachment_count  = attachment_count;
        if ( attachment_count > 0 )
@@ -270,13 +284,15 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
        for ( i = 0; i < attachment_count ; i++ ) {
                memset(arg, 0x00, 50);
                testapp_print("\n > Enter attachment name : ");
-               result_from_scanf = scanf("%s", arg);
+               if (0 >= scanf("%s", arg))
+                       testapp_print("Invalid input. ");
 
                attachment_data[i].attachment_name  = strdup(arg);
                
                memset(arg, 0x00, 50);
                testapp_print("\n > Enter attachment absolute path : ");
-               result_from_scanf = scanf("%s",arg);
+               if (0 >= scanf("%s",arg))
+                       testapp_print("Invalid input. ");
                
                attachment_data[i].attachment_path  = strdup(arg);
                attachment_data[i].save_status      = 1;
@@ -284,7 +300,8 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
        }
        
        testapp_print("\n > Meeting Request? [0: no, 1: yes (request from server), 2: yes (response from local)]");
-       result_from_scanf = scanf("%d", (int*)&(test_mail_data->meeting_request_status));
+       if (0 >= scanf("%d", (int*)&(test_mail_data->meeting_request_status)))
+               testapp_print("Invalid input. ");
        
        if ( test_mail_data->meeting_request_status == 1 
                || test_mail_data->meeting_request_status == 2 ) {
@@ -336,7 +353,6 @@ static gboolean testapp_test_add_mail (int *result_mail_id)
 
 static gboolean testapp_test_update_mail()
 {
-       int                    result_from_scanf = 0;
        int                    mail_id = 0;
        int                    err = EMAIL_ERROR_NONE;
        int                    test_attachment_data_count = 0;
@@ -347,7 +363,8 @@ static gboolean testapp_test_update_mail()
        email_meeting_request_t *meeting_req = NULL;
        
        testapp_print("\n > Enter mail id : ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        email_get_mail_data(mail_id, &test_mail_data);
 
@@ -357,7 +374,8 @@ static gboolean testapp_test_update_mail()
        }
 
        testapp_print("\n > Enter Subject: ");
-       result_from_scanf = scanf("%s", arg);
+       if (0 >= scanf("%s", arg))
+               testapp_print("Invalid input. ");
 
        test_mail_data->subject= strdup(arg);
 
@@ -378,7 +396,8 @@ static gboolean testapp_test_update_mail()
                }
        
                testapp_print("\n > Enter meeting response: ");
-               result_from_scanf = scanf("%d", (int*)&(meeting_req->meeting_response));
+               if (0 >= scanf("%d", (int*)&(meeting_req->meeting_response)))
+                       testapp_print("Invalid input. ");
        }
        
        if ( (err = email_update_mail(test_mail_data, test_attachment_data_list, test_attachment_data_count, meeting_req, 0)) != EMAIL_ERROR_NONE) 
@@ -419,29 +438,34 @@ static gboolean testapp_test_get_mails()
        int to_get_count = 0;
        int is_for_thread_view = 0;
        int list_type;
-       int result_from_scanf = 0;
        struct tm *temp_time_info;
 
        testapp_print("\n > Enter Account_id (0 = all accounts) : ");
-       result_from_scanf = scanf("%d",&account_id);
-
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
        testapp_print("\n > Enter Mailbox id (0 = all mailboxes) :");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter Sorting : ");
-       result_from_scanf = scanf("%d",&sorting);
+       if (0 >= scanf("%d",&sorting))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter Start index (starting at 0): ");
-       result_from_scanf = scanf("%d",&start_index);
+       if (0 >= scanf("%d",&start_index))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter max_count : ");
-       result_from_scanf = scanf("%d",&limit_count);
+       if (0 >= scanf("%d",&limit_count))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > For thread view : ");
-       result_from_scanf = scanf("%d",&is_for_thread_view);
+       if (0 >= scanf("%d",&is_for_thread_view))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Mail count only (0:list 1:count): ");
-       result_from_scanf = scanf("%d",&to_get_count);
+       if (0 >= scanf("%d",&to_get_count))
+               testapp_print("Invalid input. ");
 
        if(to_get_count)
                mails_pointer = NULL;
@@ -549,6 +573,7 @@ static gboolean testapp_test_get_mail_list_ex()
        int result_mail_count = 0;
        int err = EMAIL_ERROR_NONE;
        int i = 0;
+       int sort_order = 0;
 
        filter_rule_count = 3;
 
@@ -649,10 +674,17 @@ static gboolean testapp_test_get_mail_list_ex()
 
        sorting_rule_list = malloc(sizeof(email_list_sorting_rule_t) * sorting_rule_count);
        memset(sorting_rule_list, 0 , sizeof(email_list_sorting_rule_t) * sorting_rule_count);
-
+/*
        sorting_rule_list[0].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_RECIPIENT_ADDRESS;
        sorting_rule_list[0].key_value.string_type_value                   = strdup("minsoo.kimn@gmail.com");
        sorting_rule_list[0].sort_order                                    = EMAIL_SORT_ORDER_TO_CCBCC;
+*/
+       testapp_print("\n Enter the sort_order :");
+       if (0 >= scanf("%d", &sort_order))
+               testapp_print("Invalid input.");
+
+       sorting_rule_list[0].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_SUBJECT;
+       sorting_rule_list[0].sort_order                                    = sort_order;
 
        sorting_rule_list[1].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_DATE_TIME;
        sorting_rule_list[1].sort_order                                    = EMAIL_SORT_ORDER_DESCEND;
@@ -683,10 +715,10 @@ static gboolean testapp_test_send_cancel ()
        int j = 0;
        int *mailIdList = NULL;
        int mail_id = 0;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter total Number of mail  want to send: ");
-       result_from_scanf = scanf("%d", &num);
+       if (0 >= scanf("%d", &num))
+               testapp_print("Invalid input. ");
        mailIdList = (int *)malloc(sizeof(int)*num);
        if(!mailIdList)
                return false ;
@@ -701,10 +733,12 @@ static gboolean testapp_test_send_cancel ()
 
                mail_id = 0;
                testapp_print("\n > Do you want to cancel the send mail job '1' or '0': ");
-               result_from_scanf = scanf("%d", &Y);
+               if (0 >= scanf("%d", &Y))
+                       testapp_print("Invalid input. ");
                if(Y == 1) {
                        testapp_print("\n >Enter mail-id[1-%d] ",i);
-                               result_from_scanf = scanf("%d", &j);
+                       if (0 >= scanf("%d", &j))
+                               testapp_print("Invalid input. ");
                        testapp_print("\n mailIdList[%d] ",mailIdList[j]);      
                        if(email_cancel_sending_mail( mailIdList[j]) < 0)
                                testapp_print("email_cancel_sending_mail failed..!");
@@ -721,19 +755,22 @@ static gboolean testapp_test_delete()
        int mailbox_id = 0;
        int err = EMAIL_ERROR_NONE;
        int from_server = 0;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter Account_id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter Mail_id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        
        testapp_print("\n > Enter Mailbox id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
        
        testapp_print("\n > Enter from_server: ");
-       result_from_scanf = scanf("%d", &from_server);
+       if (0 >= scanf("%d", &from_server))
+               testapp_print("Invalid input. ");
 
        /* delete message */
        if( (err = email_delete_mail(mailbox_id, &mail_id, 1, from_server)) < 0)
@@ -751,21 +788,24 @@ static gboolean testapp_test_update_mail_attribute()
        int  account_id = 0;
        int *mail_id_array = NULL;
        int  mail_id_count = 0;
-       int  result_from_scanf = 0;
        email_mail_attribute_type attribute_type;
        email_mail_attribute_value_t attribute_value;
 
        testapp_print("\n > Enter account_id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter attribute_type: ");
-       result_from_scanf = scanf("%d", (int*)&attribute_type);
+       if (0 >= scanf("%d", (int*)&attribute_type))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter integer type value: ");
-       result_from_scanf = scanf("%d", (int*)&(attribute_value.integer_type_value));
+       if (0 >= scanf("%d", (int*)&(attribute_value.integer_type_value)))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter mail_id_count: ");
-       result_from_scanf = scanf("%d", &mail_id_count);
+       if (0 >= scanf("%d", &mail_id_count))
+               testapp_print("Invalid input. ");
 
        mail_id_count = (mail_id_count < 5000)?mail_id_count:5000;
 
@@ -775,7 +815,8 @@ static gboolean testapp_test_update_mail_attribute()
 
        for (i = 0; i < mail_id_count; i++) {
                testapp_print("\n > Enter mail id: ");
-               result_from_scanf = scanf("%d", (mail_id_array + i));
+               if (0 >= scanf("%d", (mail_id_array + i)))
+                       testapp_print("Invalid input. ");
        }
 
        /* delete message */
@@ -795,15 +836,16 @@ static gboolean testapp_test_move()
        int mail_id[3];
        int i = 0;
        int mailbox_id = 0;
-       int result_from_scanf = 0;
        
        for(i = 0; i< 3; i++) {
                testapp_print("\n > Enter mail_id: ");
-               result_from_scanf = scanf("%d",&mail_id[i]);
+               if (0 >= scanf("%d",&mail_id[i]))
+                       testapp_print("Invalid input. ");
        }
        
        testapp_print("\n > Enter mailbox_id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        /* move mail */
        email_move_mail_to_mailbox(mail_id, 3, mailbox_id);
@@ -814,10 +856,10 @@ static gboolean testapp_test_delete_all()
 {
        int mailbox_id =0;
        int err = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter mailbox_id: ");
-       result_from_scanf = scanf("%d",&mailbox_id);
+       if (0 >= scanf("%d",&mailbox_id))
+               testapp_print("Invalid input. ");
                                                
        /* delete all message */
        if ( (err = email_delete_all_mails_in_mailbox(mailbox_id, 0)) < 0)
@@ -832,23 +874,25 @@ static gboolean testapp_test_delete_all()
 static gboolean testapp_test_add_attachment()
 {      
        int mail_id = 0;
-       int result_from_scanf = 0;
        char arg[100];
        email_attachment_data_t attachment;
        
        testapp_print("\n > Enter Mail Id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        memset(&attachment, 0x00, sizeof(email_attachment_data_t));
        memset(arg, 0x00, 100);
        testapp_print("\n > Enter attachment name: ");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
        
        attachment.attachment_name = strdup(arg);
        
        memset(arg, 0x00, 100);
        testapp_print("\n > Enter attachment absolute path: ");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
 
        attachment.save_status = true;
        attachment.attachment_path = strdup(arg);
@@ -869,14 +913,15 @@ static gboolean testapp_test_set_deleted_flag()
        int mail_ids[100] = { 0, };
        int temp_mail_id = 0;
        int err_code = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
 
        testapp_print("\n >>> Input target account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        do {
                testapp_print("\n >>> Input target mail id ( Input 0 to terminate ) [MAX = 100]: ");
-               result_from_scanf = scanf("%d", &temp_mail_id);
+               if (0 >= scanf("%d", &temp_mail_id))
+                       testapp_print("Invalid input. ");
                mail_ids[index++] = temp_mail_id;
        } while (temp_mail_id != 0);
 
@@ -892,13 +937,14 @@ static gboolean testapp_test_expunge_mails_deleted_flagged()
        int on_server = 0;
        int err_code = EMAIL_ERROR_NONE;
        int handle = 0;
-       int result_from_scanf = 0;
 
        testapp_print("\n >>> Input target mailbox id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n >>> Expunge on server?: ");
-       result_from_scanf = scanf("%d", &on_server);
+       if (0 >= scanf("%d", &on_server))
+               testapp_print("Invalid input. ");
 
        err_code = email_expunge_mails_deleted_flagged(mailbox_id, on_server, &handle);
 
@@ -912,11 +958,11 @@ static gboolean testapp_test_send_read_receipt()
        int read_mail_id = 0;
        int receipt_mail_id = 0;
        int err_code = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        int handle = 0;
 
        testapp_print("\n >>> Input read mail id: ");
-       result_from_scanf = scanf("%d", &read_mail_id);
+       if (0 >= scanf("%d", &read_mail_id))
+               testapp_print("Invalid input. ");
 
        err_code = email_add_read_receipt(read_mail_id, &receipt_mail_id);
 
@@ -937,10 +983,10 @@ static gboolean testapp_test_delete_attachment()
 {
        int attachment_id = 0;
        int err_code = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
 
        testapp_print("\n >>> Input attachment id: ");
-       result_from_scanf = scanf("%d", &attachment_id);
+       if (0 >= scanf("%d", &attachment_id))
+               testapp_print("Invalid input. ");
 
        if( (err_code = email_delete_attachment(attachment_id)) != EMAIL_ERROR_NONE) {
                testapp_print("email_delete_attachment failed[%d]\n", err_code);
@@ -964,28 +1010,34 @@ static gboolean testapp_test_get_mail_list()
        int is_for_thread_view = 0;
        int list_type;
        struct tm *temp_time_info;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter Account_id (0 = all accounts) : ");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter Mailbox id (0 = all mailboxes) :");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter Sorting : ");
-       result_from_scanf = scanf("%d",&sorting);
+       if (0 >= scanf("%d",&sorting))
+               testapp_print("Invalid input. ");
        
        testapp_print("\n > Enter Start index (starting at 0): ");
-       result_from_scanf = scanf("%d",&start_index);
+       if (0 >= scanf("%d",&start_index))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter max_count : ");
-       result_from_scanf = scanf("%d",&limit_count);
+       if (0 >= scanf("%d",&limit_count))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > For thread view : ");
-       result_from_scanf = scanf("%d",&is_for_thread_view);
+       if (0 >= scanf("%d",&is_for_thread_view))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Count mails?(1: YES):");
-       result_from_scanf = scanf("%d",&to_get_count);
+       if (0 >= scanf("%d",&to_get_count))
+               testapp_print("Invalid input. ");
 
        if(to_get_count)
                mail_list_pointer = NULL;
@@ -1060,15 +1112,16 @@ static gboolean testapp_test_get_mail_list_for_thread_view()
        int count = 0, i = 0;
        int account_id = 0;
        int mailbox_id = 0;
-       int result_from_scanf;
        int err_code = EMAIL_ERROR_NONE;
        struct tm *time_info;
 
        testapp_print("\nEnter account id\n");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\nEnter mailbox id\n");
-       result_from_scanf = scanf("%d",&mailbox_id);
+       if (0 >= scanf("%d",&mailbox_id))
+               testapp_print("Invalid input. ");
 
 
        /* Get mail list */
@@ -1142,16 +1195,18 @@ static gboolean testapp_test_move_mails_to_mailbox_of_another_account()
        int  target_mailbox_id = 0;
        int  task_id = 0;
        int  i = 0;
-       int  result_from_scanf = 0;
 
        testapp_print("\n > Enter source mailbox id: ");
-       result_from_scanf = scanf("%d", &source_mailbox_id);
+       if (0 >= scanf("%d", &source_mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter target mailbox id: ");
-       result_from_scanf = scanf("%d", &target_mailbox_id);
+       if (0 >= scanf("%d", &target_mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter mail count: ");
-       result_from_scanf = scanf("%d", &mail_id_count);
+       if (0 >= scanf("%d", &mail_id_count))
+               testapp_print("Invalid input. ");
 
        mail_id_count = (mail_id_count < 5000)?mail_id_count:5000;
 
@@ -1161,7 +1216,8 @@ static gboolean testapp_test_move_mails_to_mailbox_of_another_account()
 
        for(i = 0; i < mail_id_count; i++) {
                testapp_print("\n > Enter mail id: ");
-               result_from_scanf = scanf("%d", (mail_id_array + i));
+               if (0 >= scanf("%d", (mail_id_array + i)))
+                       testapp_print("Invalid input. ");
        }
 
        err = email_move_mails_to_mailbox_of_another_account(source_mailbox_id, mail_id_array, mail_id_count, target_mailbox_id, &task_id);
@@ -1174,7 +1230,6 @@ static gboolean   copy_file(char *input_source_path, char *input_dest_path)
 {
     int childExitStatus;
     pid_t pid;
-    int status;
     if (!input_source_path || !input_dest_path) {
         return FALSE;
     }
@@ -1196,7 +1251,7 @@ static gboolean   copy_file(char *input_source_path, char *input_dest_path)
 
         if( WIFEXITED(childExitStatus)) /* exit code in childExitStatus */
         {
-            status = WEXITSTATUS(childExitStatus); /* zero is normal exit */
+            WEXITSTATUS(childExitStatus); /* zero is normal exit */
             testapp_print("WEXITSTATUS\n");
             /* handle non-zero as you wish */
         }
@@ -1218,7 +1273,6 @@ static gboolean   testapp_test_send_mail_with_downloading_attachment_of_original_m
 {
        int err = EMAIL_ERROR_NONE;
        int original_mail_id = 0;
-       int result_from_scanf = 0;
        int original_attachment_count = 0;
        int i = 0;
        int handle = 0;
@@ -1231,7 +1285,8 @@ static gboolean   testapp_test_send_mail_with_downloading_attachment_of_original_m
        email_attachment_data_t *original_attachment_array = NULL;
 
        testapp_print("\n > Enter original mail id: ");
-       result_from_scanf = scanf("%d", &original_mail_id);
+       if (0 >= scanf("%d", &original_mail_id))
+               testapp_print("Invalid input. ");
 
        /* Get original mail */
        if((err = email_get_mail_data(original_mail_id, &original_mail_data)) != EMAIL_ERROR_NONE || !original_mail_data) {
@@ -1343,11 +1398,11 @@ static gboolean testapp_test_get_mail_data()
 {
        int err = EMAIL_ERROR_NONE;
        int mail_id = 0;
-       int result_from_scanf = 0;
        email_mail_data_t *mail_data = NULL;
 
        testapp_print("\n > Enter mail id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        /* Get original mail */
        if((err = email_get_mail_data(mail_id, &mail_data)) != EMAIL_ERROR_NONE || !mail_data) {
@@ -1476,13 +1531,14 @@ static gboolean testapp_test_set_flags_field ()
 {
        int account_id = 0;
        int mail_id = 0;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter Account ID: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
        
        testapp_print("\n > Enter Mail ID: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        if(email_set_flags_field(account_id, &mail_id, 1, EMAIL_FLAGS_FLAGGED_FIELD, 1, 1) < 0)
                testapp_print("email_set_flags_field failed");
@@ -1496,10 +1552,10 @@ static gboolean testapp_test_download_body ()
 {
        int mail_id = 0;
        int handle = 0, err;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter Mail Id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        err = email_download_body(mail_id, 0, &handle);
        if(err  < 0)
                testapp_print("email_download_body failed");
@@ -1517,23 +1573,25 @@ static gboolean testapp_test_cancel_download_body ()
        int account_id = 0;
        int yes = 0;
        int handle = 0;
-       int result_from_scanf = 0;
        
        email_mailbox_t mailbox;
        memset(&mailbox, 0x00, sizeof(email_mailbox_t));
 
        testapp_print("\n > Enter account_id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter mail id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        
        if( email_download_body(mail_id, 0, &handle) < 0)
                testapp_print("email_download_body failed");
        else {
                testapp_print("email_download_body success\n");
                testapp_print("Do u want to cancel download job>>>>>1/0\n");
-               result_from_scanf = scanf("%d",&yes);
+               if (0 >= scanf("%d",&yes))
+                       testapp_print("Invalid input. ");
                if(1 == yes) {
                        if(email_cancel_job(account_id, handle , EMAIL_CANCELED_BY_USER) < 0)
                                testapp_print("email_cancel_job failed..!");
@@ -1551,13 +1609,14 @@ static gboolean testapp_test_download_attachment ()
        int mail_id = 0;
        int attachment_no = 0;
        int handle = 0;
-       int result_from_scanf = 0;
        
        testapp_print("\n > Enter Mail Id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter attachment number: ");
-       result_from_scanf = scanf("%d", &attachment_no);
+       if (0 >= scanf("%d", &attachment_no))
+               testapp_print("Invalid input. ");
        
        if( email_download_attachment(mail_id, attachment_no ,&handle) < 0)
                testapp_print("email_download_attachment failed");
@@ -1574,12 +1633,12 @@ static gboolean testapp_test_get_attachment_data_list()
        int err = EMAIL_ERROR_NONE;
        int i = 0;
        int mail_id = 0;
-       int result_from_scanf = 0;
        int test_attachment_data_count;
        email_attachment_data_t *test_attachment_data_list = NULL;
 
        testapp_print("\n > Enter Mail id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        if ( (err = email_get_attachment_data_list(mail_id, &test_attachment_data_list, &test_attachment_data_count)) != EMAIL_ERROR_NONE ) {
                testapp_print("email_get_attachment_data_list() failed [%d]\n", err);
@@ -1605,12 +1664,12 @@ FINISH_OFF:
 static gboolean testapp_test_get_meeting_request()
 {
        int mail_id = 0;
-       int result_from_scanf = 0;
        int err = EMAIL_ERROR_NONE;
        email_meeting_request_t *meeting_request;
 
        testapp_print("\n > Enter Mail Id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        err = email_get_meeting_request(mail_id, &meeting_request);
 
@@ -1630,16 +1689,17 @@ static gboolean testapp_test_retry_send()
 {
        int mail_id = 0;
        int timeout = 0;
-       int result_from_scanf = 0;
        
        email_mailbox_t mailbox;
        memset(&mailbox, 0x00, sizeof(email_mailbox_t));
 
        testapp_print("\n > Enter Mail Id: ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        
        testapp_print("\n > Enter timeout in seconds: ");
-       result_from_scanf = scanf("%d", &timeout);
+       if (0 >= scanf("%d", &timeout))
+               testapp_print("Invalid input. ");
 
        if( email_retry_sending_mail(mail_id, timeout) < 0)
                testapp_print("email_retry_sending_mail failed");               
@@ -1649,11 +1709,11 @@ static gboolean testapp_test_retry_send()
 static gboolean testapp_test_get_attachment_data()
 {
        int attachment_id = 0;
-       int result_from_scanf = 0;
        email_attachment_data_t *attachment = NULL;
 
        testapp_print("\n > Enter attachment id: ");
-       result_from_scanf = scanf("%d", &attachment_id);
+       if (0 >= scanf("%d", &attachment_id))
+               testapp_print("Invalid input. ");
 
        email_get_attachment_data(attachment_id, &attachment);
 
@@ -1671,15 +1731,16 @@ static gboolean testapp_test_get_attachment_data()
 static gboolean testapp_test_move_all_mails_to_mailbox()
 {
        int err = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        int src_mailbox_id;
        int dest_mailbox_id;
 
        testapp_print("src mailbox id> ");
-       result_from_scanf = scanf("%d", &src_mailbox_id);
+       if (0 >= scanf("%d", &src_mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("dest mailbox id> ");
-       result_from_scanf = scanf("%d", &dest_mailbox_id);
+       if (0 >= scanf("%d", &dest_mailbox_id))
+               testapp_print("Invalid input. ");
        
        err = email_move_all_mails_to_mailbox(src_mailbox_id, dest_mailbox_id);
        if ( err < 0 ) {
@@ -1711,33 +1772,37 @@ static gboolean testapp_test_get_totaldiskusage()
 static gboolean testapp_test_db_test()
 {
        int err = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        int mail_id;
        int account_id;
        char *to = NULL;
        char *cc = NULL;
        char *bcc = NULL;
 
-       to = (char *) malloc(500000);
-       cc = (char *) malloc(500000);
+       to  = (char *) malloc(500000);
+       cc  = (char *) malloc(500000);
        bcc = (char *) malloc(500000);
        
-       memset(to, 0x00, sizeof(to));
-       memset(cc, 0x00, sizeof(to));
-       memset(bcc, 0x00, sizeof(to));
+       memset(to,  0x00, sizeof(500000));
+       memset(cc,  0x00, sizeof(500000));
+       memset(bcc, 0x00, sizeof(500000));
 
        testapp_print("Input Mail id:\n");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        testapp_print("Input Account id:\n");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
        testapp_print("Input TO field:\n");
-       result_from_scanf = scanf("%s", to);
+       if (0 >= scanf("%s", to))
+               testapp_print("Invalid input. ");
        testapp_print("Input CC field:\n");
-       result_from_scanf = scanf("%s", cc);
+       if (0 >= scanf("%s", cc))
+               testapp_print("Invalid input. ");
        testapp_print("Input BCC field:\n");
-       result_from_scanf = scanf("%s", bcc);
-       
-       if ( emstorage_test(mail_id, account_id, to, cc, bcc, &err) == true ) {
+       if (0 >= scanf("%s", bcc))
+               testapp_print("Invalid input. ");
+
+       if ( emstorage_test(NULL, mail_id, account_id, to, cc, bcc, &err) == true ) {
                testapp_print(">> Saving Succeeded\n");
        }
        else {
@@ -1811,7 +1876,7 @@ static gboolean testapp_test_address_format_check_test()
        testapp_print("Select input method:\n");
        testapp_print("1. Test data\n");
        testapp_print("2. Keyboard\n");
-       result_from_scanf = scanf("%d", &type);
+       if (0 >= scanf("%d", &type);
 
        switch ( type ) {
                case 1:
@@ -1821,12 +1886,12 @@ static gboolean testapp_test_address_format_check_test()
                                        address_count++;
                                }
                                testapp_print("Choose address to be tested:[99:quit]\n");
-                               result_from_scanf = scanf("%d", &index);
+                               if (0 >= scanf("%d", &index);
                                if ( index == 99 )
                                        break;
 
                                testapp_print(">> [%d] Checking? (1:Yes, Other:No) [%s]\n", index, address_list[index]);
-                               result_from_scanf = scanf("%d", &check_yn);
+                               if (0 >= scanf("%d", &check_yn);
                                if ( check_yn == 1 ) {
                                        pAddress = strdup(address_list[index]);
                                        if ( em_verify_email_address(pAddress, false, &err ) == true ) {
@@ -1848,7 +1913,7 @@ static gboolean testapp_test_address_format_check_test()
                        break;
                case 2:
                        testapp_print("Input address: \n");
-                       result_from_scanf = scanf("%s", input_address);
+                       if (0 >= scanf("%s", input_address);
                        if ( em_verify_email_address(input_address, false, &err ) == true ) {
                                testapp_print(">> Saving Succeeded : addr[%s]\n", input_address);
                        }
@@ -1870,9 +1935,9 @@ static gboolean testapp_test_get_max_mail_count()
        int err = EMAIL_ERROR_NONE;
 
        err = email_get_max_mail_count(&max_count);
-       testapp_print("\n\t>>>>> email_get_max_mail_count() return [%d]\n\n", max_count);
+       testapp_print("\n\t>>>>> email_get_max_mail_count() return [%d][%d]\n\n", max_count, err);
        
-       return false;
+       return TRUE;
 }
 
 #include "email-storage.h"
@@ -1900,7 +1965,6 @@ static gboolean testapp_test_get_address_info_list()
        char buf[1024];
        int i = 0;
        int mail_id = 0;
-       int result_from_scanf = 0;
        email_address_info_list_t *address_info_list= NULL;
        email_address_info_t *p_address_info = NULL;
        GList *list = NULL;
@@ -1908,7 +1972,8 @@ static gboolean testapp_test_get_address_info_list()
 
        memset(buf, 0x00, sizeof(buf));
        testapp_print("\n > Enter mail_id: ");
-       result_from_scanf = scanf("%d",&mail_id);
+       if (0 >= scanf("%d",&mail_id))
+               testapp_print("Invalid input. ");
 
        email_get_address_info_list(mail_id, &address_info_list);
 
@@ -1953,102 +2018,6 @@ static gboolean testapp_test_get_address_info_list()
        return TRUE;
 }
 
-static gboolean testapp_test_search_mail_on_server()
-{
-       testapp_print(" >>> testapp_test_search_mail_on_server : Entered \n");
-
-       int err_code = EMAIL_ERROR_NONE;
-       int account_id = 0;
-       int mailbox_id = 0;
-       int search_key_value_integer = 0;
-       int result_from_scanf = 0;
-       email_search_filter_type search_filter_type = 0;
-       email_search_filter_t search_filter;
-       int handle = 0;
-       time_t current_time = 0;
-       char search_key_value_string[MAX_EMAIL_ADDRESS_LENGTH];
-
-       testapp_print("input account id : ");
-       result_from_scanf = scanf("%d",&account_id);
-
-       testapp_print("input mailbox id : ");
-       result_from_scanf = scanf("%d", &mailbox_id);
-
-       testapp_print(
-               "       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       =  1,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_UID              =  2,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_BCC              =  7,  ( 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_SUBJECT          = 13,  ( string type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_TO               = 15,  ( string type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      = 16,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     = 17,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE = 20,  ( time type ) \n"
-               "       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,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    = 28,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      = 30,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     = 32,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     = 34,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       = 36,  ( integet type ) \n"
-               "       EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       = 43,  ( string type ) \n");
-       testapp_print("input search filter type : ");
-       result_from_scanf = scanf("%d", (int*)&search_filter_type);
-
-       search_filter.search_filter_type = search_filter_type;
-
-       switch(search_filter_type) {
-               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_ANSWERED   :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
-               case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
-                       testapp_print("input search filter key value : ");
-                       result_from_scanf = scanf("%d", &search_key_value_integer);
-                       search_filter.search_filter_key_value.integer_type_key_value = search_key_value_integer;
-                       break;
-
-               case EMAIL_SEARCH_FILTER_TYPE_BCC              :
-               case EMAIL_SEARCH_FILTER_TYPE_CC               :
-               case EMAIL_SEARCH_FILTER_TYPE_FROM             :
-               case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
-               case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
-               case EMAIL_SEARCH_FILTER_TYPE_TO               :
-               case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
-                       testapp_print("input search filter key value : ");
-                       result_from_scanf = scanf("%s", search_key_value_string);
-                       search_filter.search_filter_key_value.string_type_key_value = search_key_value_string;
-                       break;
-
-               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
-               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
-               case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  :
-                       time(&current_time);
-                       /* TODO : write codes for converting string to time */
-                       /* search_filter.search_filter_key_value.time_type_key_value = search_key_value_string; */
-                       search_filter.search_filter_key_value.time_type_key_value = current_time;
-                       break;
-               default :
-                       testapp_print("Invalid filter type [%d]", search_filter_type);
-                       return FALSE;
-       }
-
-       if( (err_code = email_search_mail_on_server(account_id, mailbox_id, &search_filter,1, &handle)) != EMAIL_ERROR_NONE) {
-               testapp_print("email_search_mail_on_server failed [%d]", err_code);
-       }
-
-       testapp_print(" >>> testapp_test_search_mail_on_server : END \n");
-       return TRUE;
-}
-
 static gboolean testapp_test_add_mail_to_search_result_box()
 {
        int                    i = 0;
@@ -2056,7 +2025,6 @@ static gboolean testapp_test_add_mail_to_search_result_box()
        int                    from_eas = 0;
        int                    attachment_count = 0;
        int                    err = EMAIL_ERROR_NONE;
-       int                    result_from_scanf = 0;
        char                   arg[50] = { 0 , };
        char                  *body_file_path = MAIL_TEMP_BODY;
        email_mailbox_t         *mailbox_data = NULL;
@@ -2066,7 +2034,8 @@ static gboolean testapp_test_add_mail_to_search_result_box()
        FILE                  *body_file = NULL;
 
        testapp_print("\n > Enter account id : ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        email_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SEARCH_RESULT, &mailbox_data);
 
@@ -2074,7 +2043,8 @@ static gboolean testapp_test_add_mail_to_search_result_box()
        memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
 
        testapp_print("\n From EAS? [0/1]> ");
-       result_from_scanf = scanf("%d", &from_eas);
+       if (0 >= scanf("%d", &from_eas))
+               testapp_print("Invalid input. ");
 
        test_mail_data->account_id             = account_id;
        test_mail_data->save_status            = 1;
@@ -2098,12 +2068,14 @@ static gboolean testapp_test_add_mail_to_search_result_box()
        fclose(body_file);
 
        testapp_print(" > Attach file? [0/1] : ");
-       result_from_scanf = scanf("%d",&attachment_count);
+       if (0 >= scanf("%d",&attachment_count))
+               testapp_print("Invalid input. ");
 
        if ( attachment_count )  {
                memset(arg, 0x00, 50);
                testapp_print("\n > Enter attachment name : ");
-               result_from_scanf = scanf("%s", arg);
+               if (0 >= scanf("%s", arg))
+                       testapp_print("Invalid input. ");
 
                attachment_data = malloc(sizeof(email_attachment_data_t));
 
@@ -2111,7 +2083,8 @@ static gboolean testapp_test_add_mail_to_search_result_box()
 
                memset(arg, 0x00, 50);
                testapp_print("\n > Enter attachment absolute path : ");
-               result_from_scanf = scanf("%s",arg);
+               if (0 >= scanf("%s",arg))
+                       testapp_print("Invalid input. ");
 
                attachment_data->attachment_path  = strdup(arg);
                attachment_data->save_status      = 1;
@@ -2119,7 +2092,8 @@ static gboolean testapp_test_add_mail_to_search_result_box()
        }
 
        testapp_print("\n > Meeting Request? [0: no, 1: yes (request from server), 2: yes (response from local)]");
-       result_from_scanf = scanf("%d", (int*)&(test_mail_data->meeting_request_status));
+       if (0 >= scanf("%d", (int*)&(test_mail_data->meeting_request_status)))
+               testapp_print("Invalid input. ");
 
        if ( test_mail_data->meeting_request_status == 1
                || test_mail_data->meeting_request_status == 2 ) {
@@ -2169,21 +2143,24 @@ static gboolean testapp_test_add_mail_to_search_result_box()
 
 static gboolean testapp_test_email_parse_mime_file()
 {
-       email_mail_data_t *mail_data = NULL;
-       email_attachment_data_t *mail_attachment_data = NULL;
        int i = 0;
        int attachment_count = 0;
+       int output_attachment_count = 0;
        int verify = 0;
        int err = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        char eml_file_path[255] = {0, };
         struct tm *struct_time;
 
+       email_mail_data_t *mail_data = NULL;
+       email_attachment_data_t *mail_attachment_data = NULL;
+       email_mail_data_t *output_mail_data = NULL;
+       email_attachment_data_t *output_mail_attachment_data = NULL;
+
        testapp_print("Input eml file path : ");
-       result_from_scanf = scanf("%s", eml_file_path);
+       if (0 >= scanf("%s", eml_file_path))
+               testapp_print("Invalid input. ");
 
-       if ((err = email_parse_mime_file(eml_file_path, &mail_data, &mail_attachment_data, &attachment_count)) != EMAIL_ERROR_NONE)
-       {
+       if ((err = email_parse_mime_file(eml_file_path, &mail_data, &mail_attachment_data, &attachment_count)) != EMAIL_ERROR_NONE) {
                testapp_print("email_parse_mime_file failed : [%d]\n", err);
                return false;   
        }
@@ -2204,6 +2181,7 @@ static gboolean testapp_test_email_parse_mime_file()
        testapp_print("Return-Path: %s\n", mail_data->full_address_return);
        testapp_print("To: %s\n", mail_data->full_address_to);
        testapp_print("Subject: %s\n", mail_data->subject);
+       testapp_print("Priority: %d\n", mail_data->priority);
        testapp_print("From: %s\n", mail_data->full_address_from);
        testapp_print("Reply-To: %s\n", mail_data->full_address_reply);
        testapp_print("Sender: %s\n", mail_data->email_address_sender);
@@ -2211,27 +2189,35 @@ static gboolean testapp_test_email_parse_mime_file()
        testapp_print("attachment_count: %d\n", mail_data->attachment_count);
        testapp_print("SMIME type : %d\n", mail_data->smime_type);
        testapp_print("inline content count : %d\n", mail_data->inline_content_count);
+       testapp_print("mail_size : %d\n", mail_data->mail_size);
        testapp_print("download_body_status : %d\n", mail_data->body_download_status);
 
 
        for (i = 0;i < attachment_count ; i++) {
+               testapp_print("%d attachment\n", i);
                testapp_print("attachment_id: %d\n", mail_attachment_data[i].attachment_id);
+               testapp_print("attachment_size: %d\n", mail_attachment_data[i].attachment_size);
                testapp_print("inline_attachment_status: %d\n", mail_attachment_data[i].inline_content_status);
                testapp_print("attachment_name: %s\n", mail_attachment_data[i].attachment_name);
                testapp_print("attachment_path: %s\n", mail_attachment_data[i].attachment_path);
+               testapp_print("attachment_save_status: %d\n", mail_attachment_data[i].save_status);
+               testapp_print("content_id: %s\n", mail_attachment_data[i].content_id);
                testapp_print("mailbox_id: %d\n", mail_attachment_data[i].mailbox_id);
        }
 
        testapp_print("Success : Open eml file\n");
 
-       if (mail_data->smime_type == EMAIL_SMIME_SIGNED) {
-               if (!email_verify_signature_ex(mail_data, mail_attachment_data, attachment_count, &verify)) {
-                       testapp_print("email_verify_signature_ex failed\n");
-               }       
-
+       if (mail_data->smime_type != EMAIL_SMIME_NONE) {
+               if (mail_data->smime_type == EMAIL_SMIME_SIGNED || mail_data->smime_type == EMAIL_PGP_SIGNED) {
+                       if (!email_verify_signature_ex(mail_data, mail_attachment_data, attachment_count, &verify)) 
+                               testapp_print("email_verify_signature_ex failed\n");
+               } else {
+                       if ((err = email_get_decrypt_message_ex(mail_data, mail_attachment_data, attachment_count, &output_mail_data, &output_mail_attachment_data, &output_attachment_count)) != EMAIL_ERROR_NONE)
+                               testapp_print("email_get_decrypt_message_ex failed\n");
+               }
+               
                testapp_print("verify : [%d]\n", verify);
        }       
-
        
        if ((err = email_delete_parsed_data(mail_data)) != EMAIL_ERROR_NONE) {
                testapp_print("email_delete_eml_data failed : [%d]\n", err);
@@ -2258,27 +2244,18 @@ static gboolean testapp_test_email_write_mime_file()
 {
        int err = EMAIL_ERROR_NONE;
        int mail_id = 0;
-       int ret_scanf = 0;
-       int is_file_path = 0;
        int attachment_count = 0;
-       int account_id = 0;
        char *file_path = NULL;
        email_mail_data_t *mail_data = NULL;
        email_attachment_data_t *mail_attachment_data = NULL;
 
-       testapp_print("Is file path (0 or 1): ");
-       ret_scanf = scanf("%d", &is_file_path);
-       
-       if (is_file_path) {
-               file_path = malloc(512);
-               memset(file_path, 0x00, 512);
 
-               testapp_print("Input output file path : ");
-               ret_scanf = scanf("%s", file_path);     
-       }
-       testapp_print("Input mail id : ");
-       ret_scanf = scanf("%d", &mail_id);
+       file_path = malloc(512);
+       memset(file_path, 0x00, 512);
 
+       testapp_print("Input mail id : ");
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        err = email_get_mail_data(mail_id, &mail_data);
        if (err != EMAIL_ERROR_NONE || mail_data == NULL) {
@@ -2292,10 +2269,7 @@ static gboolean testapp_test_email_write_mime_file()
                return false;
        }
 
-       testapp_print("Input Account id : ");
-       ret_scanf = scanf("%d", &account_id);
-
-       mail_data->account_id = account_id;
+       snprintf(file_path, 512, "/opt/usr/data/email/.email_data/tmp/%d_%8d.eml", mail_id, (int)time(NULL));
 
        err = email_write_mime_file(mail_data, mail_attachment_data, attachment_count, &file_path);
        if (err != EMAIL_ERROR_NONE) {
@@ -2320,12 +2294,12 @@ static gboolean testapp_test_email_write_mime_file()
 static gboolean testapp_test_smime_verify_signature()
 {
        int mail_id = 0;
-       int ret_scanf = 0;
        int verify = 0;
        int err = EMAIL_ERROR_NONE;
 
        testapp_print("input mail_id :");
-       ret_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
        
        err = email_verify_signature(mail_id, &verify);
        if (err != EMAIL_ERROR_NONE) {
@@ -2337,6 +2311,208 @@ static gboolean testapp_test_smime_verify_signature()
        return true;
 }
 
+static gboolean testapp_test_email_add_mail_with_multiple_recipient()
+{
+       int                    i = 0;
+       int                    account_id = 0;
+       int                    err = EMAIL_ERROR_NONE;
+//     int                    smime_type = 0;
+       int                    recipient_addresses_count = 0;
+       char                  *recipient_addresses = NULL;
+       char                   recipient_address[234] = { 0, };
+       char                   from_address[300] = { 0 , };
+       const char            *body_file_path = MAIL_TEMP_BODY;
+       email_account_t       *account_data = NULL;
+       email_mailbox_t       *mailbox_data = NULL;
+       email_mail_data_t     *test_mail_data = NULL;
+       FILE                  *body_file;
+
+       testapp_print("\n > Enter account id : ");
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n > Enter recipient address count : ");
+       if (0 >= scanf("%d", &recipient_addresses_count))
+               testapp_print("Invalid input. ");
+
+       if(recipient_addresses_count < 1)
+               return TRUE;
+
+       recipient_addresses = malloc(sizeof(char) * 234 * recipient_addresses_count);
+
+       for (i = 0; i < recipient_addresses_count; i++) {
+               snprintf(recipient_address, 234, "\"mailtest%05d\" <mailtest%05d@a1234567890b1234567890.com>; ", i, i);
+               strcat(recipient_addresses, recipient_address);
+       }
+
+       email_get_account(account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_data);
+
+       email_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_data);
+
+       test_mail_data = malloc(sizeof(email_mail_data_t));
+       memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
+
+       SNPRINTF(from_address, 300, "<%s>", account_data->user_email_address);
+
+       test_mail_data->account_id             = account_id;
+       test_mail_data->save_status            = EMAIL_MAIL_STATUS_SEND_DELAYED;
+       test_mail_data->body_download_status   = 1;
+       test_mail_data->flags_seen_field       = 1;
+       test_mail_data->file_path_plain        = strdup(body_file_path);
+       test_mail_data->mailbox_id             = mailbox_data->mailbox_id;
+       test_mail_data->mailbox_type           = mailbox_data->mailbox_type;
+       test_mail_data->full_address_from      = strdup(from_address);
+       test_mail_data->full_address_to        = recipient_addresses;
+       test_mail_data->subject                = strdup("Read receipt request from TIZEN");
+       test_mail_data->remaining_resend_times = 3;
+       test_mail_data->report_status          = EMAIL_MAIL_REQUEST_DSN | EMAIL_MAIL_REQUEST_MDN;
+       test_mail_data->smime_type             = 0;
+
+       body_file = fopen(body_file_path, "w");
+
+       testapp_print("\n body_file [%p]\n", body_file);
+
+       if(body_file == NULL) {
+               testapp_print("\n fopen [%s]failed\n", body_file_path);
+               return FALSE;
+       }
+
+       for(i = 0; i < 100; i++)
+               fprintf(body_file, "Mail sending Test. [%d]\n", i);
+
+       fflush(body_file);
+       fclose(body_file);
+
+
+       if((err = email_add_mail(test_mail_data, NULL, 0, NULL, 0)) != EMAIL_ERROR_NONE)
+               testapp_print("email_add_mail failed. [%d]\n", err);
+       else
+               testapp_print("email_add_mail success.\n");
+
+       testapp_print("saved mail id = [%d]\n", test_mail_data->mail_id);
+
+       email_free_mail_data(&test_mail_data, 1);
+       email_free_mailbox(&mailbox_data, 1);
+       email_free_account(&account_data, 1);
+
+       return FALSE;
+}
+
+static gboolean testapp_test_send_mails_every_x_minutes()
+{
+       int                    added_mail_id = 0;
+       int                    err = EMAIL_ERROR_NONE;
+       int                    handle = 0;
+       time_t                 time_to_send;
+       int                    i = 0;
+       int                    j = 0;
+       int                    account_id = 0;
+       int                    send_interval_in_minutes = 0;
+       int                    number_of_mails = 0;
+       char                   recipient_address[300] = { 0 , };
+       char                   from_address[300] = { 0 , };
+       char                   subject_form[1024] = { 0 , };
+       const char            *body_file_path = MAIL_TEMP_BODY;
+       struct tm             *time_to_send_tm;
+       email_account_t       *account_data = NULL;
+       email_mailbox_t       *mailbox_data = NULL;
+       email_mail_data_t     *test_mail_data = NULL;
+       FILE                  *body_file;
+
+       testapp_print("\n > Enter account id : ");
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n > Enter recipient address : ");
+       if (0 >= scanf("%s", recipient_address))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n > Enter number of mails: ");
+       if (0 >= scanf("%d", &number_of_mails))
+               testapp_print("Invalid input. ");
+
+       testapp_print("\n > Enter send interval in minutes: ");
+       if (0 >= scanf("%d", &send_interval_in_minutes))
+               testapp_print("Invalid input. ");
+
+       email_get_account(account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_data);
+
+       email_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox_data);
+
+       test_mail_data = malloc(sizeof(email_mail_data_t));
+       memset(test_mail_data, 0x00, sizeof(email_mail_data_t));
+
+       SNPRINTF(from_address, 300, "<%s>", account_data->user_email_address);
+
+       test_mail_data->account_id             = account_id;
+       test_mail_data->save_status            = EMAIL_MAIL_STATUS_SEND_DELAYED;
+       test_mail_data->body_download_status   = 1;
+       test_mail_data->flags_seen_field       = 1;
+       test_mail_data->file_path_plain        = strdup(body_file_path);
+       test_mail_data->mailbox_id             = mailbox_data->mailbox_id;
+       test_mail_data->mailbox_type           = mailbox_data->mailbox_type;
+       test_mail_data->full_address_from      = strdup(from_address);
+       test_mail_data->full_address_to        = strdup(recipient_address);
+       test_mail_data->remaining_resend_times = 3;
+       test_mail_data->report_status          = EMAIL_MAIL_REPORT_NONE;
+
+
+       for (i = 0; i < number_of_mails; i++) {
+               if (test_mail_data->subject)
+                       free(test_mail_data->subject);
+
+               time(&time_to_send);
+               time_to_send += (60 * send_interval_in_minutes) * (i + 1);
+               time_to_send_tm = localtime(&time_to_send);
+
+               strftime(subject_form, 1024, "[%H:%M] TEST.", time_to_send_tm);
+               test_mail_data->subject = strdup(subject_form);
+
+               body_file = fopen(body_file_path, "w");
+
+               testapp_print("\n body_file [%p]\n", body_file);
+
+               if(body_file == NULL) {
+                       testapp_print("\n fopen [%s]failed\n", body_file_path);
+                       return FALSE;
+               }
+
+               for(j = 0; j < 100; j++)
+                       fprintf(body_file, "Mail sending Test. [%d]\n", j);
+
+               fflush(body_file);
+               fclose(body_file);
+
+               if((err = email_add_mail(test_mail_data, NULL, 0, NULL, 0)) != EMAIL_ERROR_NONE) {
+                       testapp_print("email_add_mail failed. [%d]\n", err);
+                       added_mail_id = 0;
+               }
+               else {
+                       testapp_print("email_add_mail success.\n");
+                       added_mail_id = test_mail_data->mail_id;
+               }
+
+               testapp_print("saved mail id = [%d]\n", added_mail_id);
+
+               if(added_mail_id) {
+                       testapp_print("Calling email_schedule_sending_mail...\n");
+
+                       if( email_schedule_sending_mail(added_mail_id, time_to_send) < 0) {
+                               testapp_print("email_schedule_sending_mail failed[%d]\n", err);
+                       }
+                       else  {
+                               testapp_print("Start sending. handle[%d]\n", handle);
+                       }
+               }
+       }
+
+       email_free_mail_data(&test_mail_data, 1);
+       email_free_mailbox(&mailbox_data, 1);
+       email_free_account(&account_data, 1);
+
+       return TRUE;
+}
+
 /* internal functions */
 static gboolean testapp_test_interpret_command (int menu_number)
 {
@@ -2454,20 +2630,23 @@ static gboolean testapp_test_interpret_command (int menu_number)
                case 57:
                        testapp_test_update_mail();
                        break;
-               case 58:
-                       testapp_test_search_mail_on_server();
-                       break;
                case 59:
                        testapp_test_add_mail_to_search_result_box();
                        break;
                case 60:
                        testapp_test_email_parse_mime_file();
                        break;
+               case 61:
+                       testapp_test_email_write_mime_file();
+                       break;
                case 62:
                        testapp_test_smime_verify_signature();
                        break;
-               case 61:
-                       testapp_test_email_write_mime_file();
+               case 63:
+                       testapp_test_email_add_mail_with_multiple_recipient();
+                       break;
+               case 64:
+                       testapp_test_send_mails_every_x_minutes();
                        break;
                case 0:
                        go_to_loop = FALSE;
@@ -2483,13 +2662,13 @@ void testapp_mail_main()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
        
        while (go_to_loop) {
                testapp_show_menu(EMAIL_MAIL_MENU);
                testapp_show_prompt(EMAIL_MAIL_MENU);
                        
-               result_from_scanf = scanf("%d", &menu_number);
+               if (0 >= scanf("%d", &menu_number))
+                       testapp_print("Invalid input");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }
index 6caee04..5540da4 100755 (executable)
@@ -79,30 +79,34 @@ static gboolean testapp_test_add_mailbox()
        char arg[500];
        int ret;
     int handle;
-    int result_from_scanf = 0;
 
     memset(&mailbox, 0, sizeof(email_mailbox_t));
 
        memset(arg, 0x00, 500);
        testapp_print("\n> Enter mailbox name: ");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
        mailbox.mailbox_name = strdup(arg);
        
        memset(arg, 0x00, 500);
        testapp_print("> Enter mailbox alias name: ");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
        mailbox.alias = strdup(arg);
 
        testapp_print("> Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
        mailbox.account_id = account_id;
 
        testapp_print("> Enter local_yn (1/0): ");
-       result_from_scanf = scanf("%d", &local_yn);
+       if (0 >= scanf("%d", &local_yn))
+               testapp_print("Invalid input. ");
        mailbox.local= local_yn;        
 
        testapp_print("> Enter mailbox type: ");
-       result_from_scanf = scanf("%d", &mailbox_type);
+       if (0 >= scanf("%d", &mailbox_type))
+               testapp_print("Invalid input. ");
        mailbox.mailbox_type= mailbox_type;
 
        mailbox.eas_data               = strdup("EAS DATA TEST");
@@ -130,13 +134,14 @@ static gboolean testapp_test_delete_mailbox()
        int on_server = 0;
        int ret;
        int handle;
-       int result_from_scanf = 0;
 
        testapp_print("\n> Enter mailbox id:");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input");
        
        testapp_print("> Enter on_server (1/0): ");
-       result_from_scanf = scanf("%d", &on_server);
+       if (0 >= scanf("%d", &on_server))
+               testapp_print("Invalid input");
 
        ret = email_delete_mailbox(mailbox_id, on_server, &handle);
 
@@ -158,17 +163,19 @@ static gboolean testapp_test_rename_mailbox()
        char mailbox_name[500] = { 0, };
        char mailbox_alias[500] = { 0, };
        int err;
-       int result_from_scanf = 0;
        int handle = 0;
        
        testapp_print("> Enter mailbox id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input");
 
        testapp_print("> Enter new mailbox name: ");
-       result_from_scanf = scanf("%s", mailbox_name);
+       if (0 >= scanf("%s", mailbox_name))
+               testapp_print("Invalid input");
 
        testapp_print("> Enter new mailbox name: ");
-       result_from_scanf = scanf("%s", mailbox_alias);
+       if (0 >= scanf("%s", mailbox_alias))
+               testapp_print("Invalid input");
 
        
        if ( (err = email_rename_mailbox(mailbox_id, mailbox_name, mailbox_alias, true, &handle)) < 0) {
@@ -185,10 +192,10 @@ static gboolean testapp_test_get_imap_mailbox_list()
 {
        int account_id = 0;
        int handle = 0;
-       int result_from_scanf = 0;
        
        testapp_print("> Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
        
        if(  email_sync_imap_mailbox_list(account_id, &handle) < 0)
                testapp_print("email_sync_imap_mailbox_list failed");
@@ -201,13 +208,14 @@ static gboolean testapp_test_set_local_mailbox()
 {
        int mailbox_id = 0;
        int is_local_mailbox = 0;
-       int result_from_scanf = 0;
 
        testapp_print("> Enter mailbox id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input");
 
        testapp_print("> Enter local: ");
-       result_from_scanf = scanf("%d", &is_local_mailbox);
+       if (0 >= scanf("%d", &is_local_mailbox))
+               testapp_print("Invalid input");
 
        if( email_set_local_mailbox(mailbox_id, is_local_mailbox) < 0)
                testapp_print("email_set_local_mailbox failed");
@@ -224,16 +232,18 @@ static gboolean testapp_test_delete_mailbox_ex()
        int  on_server = 0;
        int  handle = 0;
        int  i = 0;
-       int  result_from_scanf = 0;
 
        testapp_print("\n > Enter account_id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter mailbox_id_count: ");
-       result_from_scanf = scanf("%d", &mailbox_id_count);
+       if (0 >= scanf("%d", &mailbox_id_count))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter on_server: ");
-       result_from_scanf = scanf("%d", &on_server);
+       if (0 >= scanf("%d", &on_server))
+               testapp_print("Invalid input");
 
        mailbox_id_count = (mailbox_id_count < 5000)?mailbox_id_count:5000;
 
@@ -243,7 +253,8 @@ static gboolean testapp_test_delete_mailbox_ex()
 
        for(i = 0; i < mailbox_id_count; i++) {
                testapp_print("\n > Enter mailbox id: ");
-               result_from_scanf = scanf("%d", (mailbox_id_array + i));
+               if (0 >= scanf("%d", (mailbox_id_array + i)))
+                       testapp_print("Invalid input");
        }
 
        err = email_delete_mailbox_ex(account_id, mailbox_id_array, mailbox_id_count, on_server, &handle);
@@ -260,15 +271,16 @@ static gboolean testapp_test_get_mailbox_by_type()
 
        int account_id =0;      
        int err_code = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        email_mailbox_t *mailbox =NULL;
        email_mailbox_type_e mailbox_type =0;
        
        testapp_print("\n > Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter mailbox_type: ");
-       result_from_scanf = scanf("%d", (int*)&mailbox_type);
+       if (0 >= scanf("%d", (int*)&mailbox_type))
+               testapp_print("Invalid input");
 
        if( (err_code = email_get_mailbox_by_mailbox_type(account_id,mailbox_type,&mailbox)) < 0) {
                testapp_print("   email_get_mailbox_by_mailbox_type error : %d\n",err_code);
@@ -286,13 +298,14 @@ static gboolean testapp_test_set_mailbox_type()
        int  mailbox_id = 0;
        int  mailbox_type = 0;
        int  err_code = EMAIL_ERROR_NONE;
-       int  result_from_scanf = 0;
 
        testapp_print("\n > Enter mailbox id : ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter mailbox type : ");
-       result_from_scanf = scanf("%d", &mailbox_type);
+       if (0 >= scanf("%d", &mailbox_type))
+               testapp_print("Invalid input");
 
        if( (err_code = email_set_mailbox_type(mailbox_id, mailbox_type) ) != EMAIL_ERROR_NONE) {
                testapp_print("\nemail_set_mailbox_type error : %d\n", err_code);
@@ -307,16 +320,18 @@ static gboolean testapp_test_set_mail_slot_size ()
        int mailbox_id = 0;
        int mail_slot_size = 0;
        int err_code = EMAIL_ERROR_NONE;
-       int result_from_scanf = 0;
        
        testapp_print("\n > Enter account id (0: All account): ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n> Enter mailbox id (0 : All mailboxes):");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter mailbox slot size: ");
-       result_from_scanf = scanf("%d", &mail_slot_size);
+       if (0 >= scanf("%d", &mail_slot_size))
+               testapp_print("Invalid input");
 
        if( (err_code = email_set_mail_slot_size(account_id, mailbox_id, mail_slot_size) ) < 0) {
                testapp_print("   testapp_test_set_mail_slot_size error : %d\n", err_code);
@@ -328,16 +343,17 @@ static gboolean testapp_test_set_mail_slot_size ()
 
 static gboolean testapp_test_get_mailbox_list ()
 {
-       int result_from_scanf = 0;
        int account_id =0;
        int mailbox_sync_type;
        int count = 0;
        int error_code = EMAIL_ERROR_NONE;
        email_mailbox_t *mailbox_list=NULL;
        testapp_print("\n > Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
        testapp_print("\n > Enter mailbox_sync_type\n[-1 :for all mailboxes, 0 : for mailboxes from server, 1 : local mailboxes\n : ");
-       result_from_scanf = scanf("%d", &mailbox_sync_type);
+       if (0 >= scanf("%d", &mailbox_sync_type))
+               testapp_print("Invalid input");
 
        if((error_code = email_get_mailbox_list(account_id, mailbox_sync_type, &mailbox_list, &count)) < 0) {
                testapp_print("   email_get_mailbox_list error %d\n", error_code);
@@ -361,17 +377,18 @@ static gboolean testapp_test_get_mailbox_list ()
 
 static gboolean testapp_test_get_mailbox_list_by_keyword ()
 {
-       int result_from_scanf = 0;
        int account_id =0;
        int count = 0;
        char keyword[500] = { 0, };
        int error_code = EMAIL_ERROR_NONE;
        email_mailbox_t *mailbox_list = NULL;
        testapp_print("\n > Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input");
 
        testapp_print("> Enter keyword: ");
-       result_from_scanf = scanf("%s", keyword);
+       if (0 >= scanf("%s", keyword))
+               testapp_print("Invalid input");
 
        if ((error_code = email_get_mailbox_list_by_keyword(account_id, keyword, &mailbox_list, &count)) < 0) {
                testapp_print("   email_get_mailbox_list_by_keyword error %d\n", error_code);
@@ -388,16 +405,17 @@ static gboolean testapp_test_get_mailbox_list_by_keyword ()
 
 static gboolean testapp_test_sync_mailbox()
 {
-       int result_from_scanf = 0;
        int account_id = 0;
        int handle = 0;
        int mailbox_id = 0;
 
        testapp_print("\n > Enter Account id (0: for all account) : ");
-       result_from_scanf = scanf("%d",&account_id);
+       if (0 >= scanf("%d",&account_id))
+               testapp_print("Invalid input");
 
        testapp_print("\n > Enter Mailbox id (0: for all mailboxes) : ");
-       result_from_scanf = scanf("%d",&mailbox_id);
+       if (0 >= scanf("%d",&mailbox_id))
+               testapp_print("Invalid input");
 
        if(account_id == ALL_ACCOUNT) {
                if(email_sync_header_for_all_account(&handle) < 0)
@@ -417,11 +435,11 @@ static gboolean testapp_test_sync_mailbox()
 
 static gboolean testapp_test_stamp_sync_time()
 {
-       int result_from_scanf;
        int input_mailbox_id = 0;
 
        testapp_print("\n > Enter Mailbox id : ");
-       result_from_scanf = scanf("%d",&input_mailbox_id);
+       if (0 >= scanf("%d",&input_mailbox_id))
+               testapp_print("Invalid input");
 
        email_stamp_sync_time_of_mailbox(input_mailbox_id);
 
@@ -436,18 +454,19 @@ static gboolean testapp_test_rename_mailbox_ex()
        char mailbox_alias[500] = { 0, };
        char eas_data[500] = "OK. Done";
        int err;
-       int result_from_scanf = 0;
        int handle = 0;
 
        testapp_print("> Enter mailbox id: ");
-       result_from_scanf = scanf("%d", &mailbox_id);
+       if (0 >= scanf("%d", &mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("> Enter new mailbox name: ");
-       result_from_scanf = scanf("%s", mailbox_name);
+       if (0 >= scanf("%s", mailbox_name))
+               testapp_print("Invalid input. ");
 
        testapp_print("> Enter new mailbox alias: ");
-       result_from_scanf = scanf("%s", mailbox_alias);
-
+       if (0 >= scanf("%s", mailbox_alias))
+               testapp_print("Invalid input. ");
 
        if ( (err = email_rename_mailbox_ex(mailbox_id, mailbox_name, mailbox_alias, eas_data, strlen(eas_data), false, &handle)) < 0) {
                testapp_print("\n email_rename_mailbox failed[%d]\n", err);
@@ -533,13 +552,13 @@ void email_test_mailbox_main()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
        
        while (go_to_loop) {
                testapp_show_menu (EMAIL_MAILBOX_MENU);
                testapp_show_prompt (EMAIL_MAILBOX_MENU);
                        
-               result_from_scanf = scanf("%d", &menu_number);
+               if (0 >= scanf("%d", &menu_number))
+                       testapp_print("Invalid input. ");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }
index e24d32e..51c2af1 100755 (executable)
@@ -54,19 +54,28 @@ static gboolean testapp_test_cancel_job     ()
 {
        int account_id = 0;
        int handle = 0;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter account_id (0: all account): ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter handle: ");
-       result_from_scanf = scanf("%d", &handle);
+       if (0 >= scanf("%d", &handle))
+               testapp_print("Invalid input. ");
 
        if(email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER) < 0)
                testapp_print("email_cancel_job failed..!");
        return FALSE;
 }
 
+static gboolean testapp_test_init_storage()
+{
+       if(email_init_storage() < 0)
+               testapp_print("email_init_storaege failed..!");
+
+       return FALSE;
+}
+
 static gboolean testapp_test_set_dnet_proper_profile_type()
 {
        testapp_print("NOT Support\n");
@@ -83,14 +92,14 @@ static gboolean testapp_test_get_dnet_proper_profile_type()
 
 static gboolean testapp_test_get_preview_text_from_file()
 {
-       int   result_from_scanf;
        char *preview_buffer = NULL;
        char  html_file_path[1024] = { 0, };
 
        testapp_print("\n > Enter file path : ");
-       result_from_scanf = scanf("%s", html_file_path);
+       if (0 >= scanf("%s", html_file_path))
+               testapp_print("Invalid input. ");
 
-       emcore_get_preview_text_from_file(NULL, html_file_path, 1024, &preview_buffer);
+       emcore_get_preview_text_from_file(NULL, NULL, html_file_path, 1024, &preview_buffer);
 
        testapp_print("\n result :\n %s ", preview_buffer);
 
@@ -361,10 +370,10 @@ static gboolean email_test_dtt_Datastore_C()
 static gboolean testapp_test_show_user_message()
 {
        int mail_id;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter mail id : ");
-       result_from_scanf = scanf("%d", &mail_id);
+       if (0 >= scanf("%d", &mail_id))
+               testapp_print("Invalid input. ");
 
        email_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, EMAIL_ERROR_NETWORK_NOT_AVAILABLE);
        return FALSE;
@@ -373,11 +382,11 @@ static gboolean testapp_test_show_user_message()
 static gboolean testapp_test_get_mime_entity()
 {
        char mime_path[512] = {0, };
-       int result_from_scanf = 0;
        char *mime_entity = NULL;
 
        testapp_print("\n > Enter mime path for parsing : ");
-       result_from_scanf = scanf("%s", mime_path);
+       if (0 >= scanf("%s", mime_path))
+               testapp_print("Invalid input. ");
        
        email_get_mime_entity(mime_path, &mime_entity);
 
@@ -387,15 +396,32 @@ static gboolean testapp_test_get_mime_entity()
 
 static gboolean testapp_test_query_smtp_mail_size_limit()
 {
-       int result_from_scanf = 0;
        int account_id = 0;
        testapp_print("\n > Enter account id : ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
 
        email_query_smtp_mail_size_limit(account_id, NULL);
        return true;
 }
 
+static gboolean testapp_test_verify_email_address()
+{
+       int err = EMAIL_ERROR_NONE;
+       char email_address[512] = {0, };
+
+       testapp_print("\n > Enter mime path for parsing : ");
+       if (0 >= scanf("%s", email_address))
+               testapp_print("Invalid input. ");
+
+       err = email_verify_email_address(email_address);
+
+       testapp_print("\nemail_verify_email_address returns [%d]\n", err);
+       return true;
+}
+
+
+
 static gboolean testapp_test_interpret_command (int menu_number)
 {
        gboolean go_to_loop = TRUE;
@@ -404,6 +430,9 @@ static gboolean testapp_test_interpret_command (int menu_number)
                case 1:
                        testapp_test_ping_service();
                        break;
+               case 2:
+                       testapp_test_init_storage();
+                       break;
                case 3:
                        testapp_test_cancel_job ();
                        break;
@@ -439,6 +468,9 @@ static gboolean testapp_test_interpret_command (int menu_number)
                case 17:
                        testapp_test_query_smtp_mail_size_limit();
                        break;
+               case 18:
+                       testapp_test_verify_email_address();
+                       break;
                case 0:
                        go_to_loop = FALSE;
                        break;
@@ -453,13 +485,13 @@ void testapp_others_main()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
 
        while (go_to_loop) {
                testapp_show_menu (EMAIL_OTHERS_MENU);
                testapp_show_prompt (EMAIL_OTHERS_MENU);
 
-               result_from_scanf = scanf ("%d", &menu_number);
+               if (0 >= scanf ("%d", &menu_number))
+                       testapp_print("Invalid input. ");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }
index 9cb6519..e963b2e 100755 (executable)
@@ -45,38 +45,44 @@ static gboolean testapp_test_add_rule()
        int type = 0;
        int flag = 0;
        char arg[500];
-       int result_from_scanf = 0;
 
        rule = malloc(sizeof(email_rule_t));
        testapp_print("> Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
        rule->account_id = account_id;
 
        testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
-       result_from_scanf = scanf("%d", &type);
+       if (0 >= scanf("%d", &type))
+               testapp_print("Invalid input. ");
        rule->type= type;               
 
        memset(arg, 0x00, 500);
        testapp_print("\n> Enter Filtering Value:");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
        rule->value= strdup(arg);       
 
        testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
-       result_from_scanf = scanf("%d", &action);
+       if (0 >= scanf("%d", &action))
+               testapp_print("Invalid input. ");
        rule->faction= action;  
 
        if (action == 1) {
                testapp_print("\n> Enter target mailbox id:");
-               result_from_scanf = scanf("%d", &target_mailbox_id);
+               if (0 >= scanf("%d", &target_mailbox_id))
+                       testapp_print("Invalid input. ");
                rule->target_mailbox_id = target_mailbox_id;
        }
 
-       testapp_print("> Enter Flag1 value: ");
-       result_from_scanf = scanf("%d", &flag);
+       testapp_print("> Enter Flag1 value [On/Off]: ");
+       if (0 >= scanf("%d", &flag))
+               testapp_print("Invalid input. ");
        rule->flag1= flag;
 
-       testapp_print("> Enter Flag2 value: ");
-       result_from_scanf = scanf("%d", &flag);
+       testapp_print("> Enter Flag2 value [0:Exactly same as, 1:Include, 2:Compare Domain] : ");
+       if (0 >= scanf("%d", &flag))
+               testapp_print("Invalid input. ");
        rule->flag2= flag;
 
        if (email_add_rule(rule) < 0)
@@ -94,11 +100,11 @@ static gboolean testapp_test_add_rule()
 
 static gboolean testapp_test_delete_rule()
 {
-       int result_from_scanf = 0;
        int filter_id = 0;
 
        testapp_print("> Enter filter id: ");
-       result_from_scanf = scanf("%d", &filter_id);
+       if (0 >= scanf("%d", &filter_id))
+               testapp_print("Invalid input. ");
 
        if(email_delete_rule(filter_id) < 0)
                testapp_print("email_delete_rule failed..! ");
@@ -109,7 +115,6 @@ static gboolean testapp_test_delete_rule()
 
 static gboolean testapp_test_update_rule()
 {
-       int result_from_scanf = 0;
        email_rule_t*  rule = NULL;
        int account_id = 0;
        int target_mailbox_id = 0;
@@ -122,37 +127,45 @@ static gboolean testapp_test_update_rule()
        rule = malloc(sizeof(email_rule_t));
        memset(rule,0X00,sizeof(email_rule_t));
        testapp_print("> Enter filter id: ");
-       result_from_scanf = scanf("%d", &filter_id);
+       if (0 >= scanf("%d", &filter_id))
+               testapp_print("Invalid input. ");
        
        testapp_print("> Enter account id: ");
-       result_from_scanf = scanf("%d", &account_id);
+       if (0 >= scanf("%d", &account_id))
+               testapp_print("Invalid input. ");
        rule->account_id = account_id;
 
        testapp_print("> Enter Type(FROM - 1 / SUBJECT - 2): ");
-       result_from_scanf = scanf("%d", &type);
+       if (0 >= scanf("%d", &type))
+               testapp_print("Invalid input. ");
        rule->type= type;               
 
        memset(arg, 0x00, 500);
        testapp_print("\n> Enter Filtering Value:");
-       result_from_scanf = scanf("%s",arg);
+       if (0 >= scanf("%s",arg))
+               testapp_print("Invalid input. ");
        rule->value= strdup(arg);       
 
        testapp_print("> Enter Action(MOVE - 1, BLOCK - 2, DELETE - 3): ");
-       result_from_scanf = scanf("%d", &action);
+       if (0 >= scanf("%d", &action))
+               testapp_print("Invalid input. ");
        rule->faction= action;  
 
        if (action == 1) {
                testapp_print("\n> Enter target mailbox id:");
-               result_from_scanf = scanf("%d", &target_mailbox_id);
+               if (0 >= scanf("%d", &target_mailbox_id))
+                       testapp_print("Invalid input. ");
                rule->target_mailbox_id = target_mailbox_id;
        }
 
        testapp_print("> Enter Flag1 value: ");
-       result_from_scanf = scanf("%d", &flag);
+       if (0 >= scanf("%d", &flag))
+               testapp_print("Invalid input. ");
        rule->flag1= flag;
 
        testapp_print("> Enter Flag2 value: ");
-       result_from_scanf = scanf("%d", &flag);
+       if (0 >= scanf("%d", &flag))
+               testapp_print("Invalid input. ");
        rule->flag2= flag;
        
        if( !email_update_rule(filter_id, rule) < 0)
@@ -168,10 +181,10 @@ static gboolean testapp_test_get_rule(void)
 {
        email_rule_t*  rule = NULL;
        int filter_id = 0;
-       int result_from_scanf = 0;
 
        testapp_print("> Enter filter id: ");
-       result_from_scanf = scanf("%d", &filter_id);
+       if (0 >= scanf("%d", &filter_id))
+               testapp_print("Invalid input. ");
 
        if(email_get_rule(filter_id, &rule) >= 0)       
                testapp_print("\n Got rule of account_id = %d and type = %d\n", rule->account_id, rule->type);
@@ -242,13 +255,13 @@ void email_test_rule_main()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
        
        while (go_to_loop) {
                testapp_show_menu (EMAIL_RULE_MENU);
                testapp_show_prompt (EMAIL_RULE_MENU);
                        
-               result_from_scanf = scanf("%d", &menu_number);
+               if (0 >= scanf("%d", &menu_number))
+                       testapp_print("Invalid input. ");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }
index 34b913e..cfad6e2 100755 (executable)
@@ -41,19 +41,20 @@ static gboolean testapp_test_move_thread()
 {
        int thread_id, move_always_flag;
        int target_mailbox_id;
-       int result;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter thread_id: ");
-       result_from_scanf = scanf("%d", &thread_id);
+       if (0 >= scanf("%d", &thread_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter target_mailbox_id: ");
-       result_from_scanf = scanf("%d", &target_mailbox_id);
+       if (0 >= scanf("%d", &target_mailbox_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter move_always_flag: ");
-       result_from_scanf = scanf("%d", &move_always_flag);
+       if (0 >= scanf("%d", &move_always_flag))
+               testapp_print("Invalid input. ");
        
-       result = email_move_thread_to_mailbox(thread_id, target_mailbox_id, move_always_flag);
+       email_move_thread_to_mailbox(thread_id, target_mailbox_id, move_always_flag);
 
        return FALSE;
 }
@@ -61,16 +62,16 @@ static gboolean testapp_test_move_thread()
 static gboolean testapp_test_delete_thread()
 {
        int thread_id, delete_always_flag;
-       int result;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter thread_id: ");
-       result_from_scanf = scanf("%d", &thread_id);
+       if (0 >= scanf("%d", &thread_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter delete_always_flag: ");
-       result_from_scanf = scanf("%d", &delete_always_flag);
+       if (0 >= scanf("%d", &delete_always_flag))
+               testapp_print("Invalid input. ");
 
-       result = email_delete_thread(thread_id, delete_always_flag);
+       email_delete_thread(thread_id, delete_always_flag);
 
        return FALSE;
 }
@@ -78,19 +79,20 @@ static gboolean testapp_test_delete_thread()
 static gboolean testapp_test_set_seen_flag_of_thread()
 {
        int thread_id, seen_flag, on_server;
-       int result;
-       int result_from_scanf = 0;
 
        testapp_print("\n > Enter thread_id: ");
-       result_from_scanf = scanf("%d", &thread_id);
+       if (0 >= scanf("%d", &thread_id))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter seen_flag: ");
-       result_from_scanf = scanf("%d", &seen_flag);
+       if (0 >= scanf("%d", &seen_flag))
+               testapp_print("Invalid input. ");
 
        testapp_print("\n > Enter on_server: ");
-       result_from_scanf = scanf("%d", &on_server);
+       if (0 >= scanf("%d", &on_server))
+               testapp_print("Invalid input. ");
 
-       result = email_modify_seen_flag_of_thread(thread_id, seen_flag, on_server);
+       email_modify_seen_flag_of_thread(thread_id, seen_flag, on_server);
 
        return FALSE;
 }
@@ -126,13 +128,13 @@ void testapp_thread_main()
 {
        gboolean go_to_loop = TRUE;
        int menu_number = 0;
-       int result_from_scanf = 0;
 
        while (go_to_loop) {
                testapp_show_menu (EMAIL_THREAD_MENU);
                testapp_show_prompt (EMAIL_THREAD_MENU);
 
-               result_from_scanf = scanf("%d", &menu_number);
+               if (0 >= scanf("%d", &menu_number))
+                       testapp_print("Invalid input. ");
 
                go_to_loop = testapp_test_interpret_command (menu_number);
        }
index c834b7e..910b85a 100755 (executable)
@@ -76,15 +76,16 @@ void testapp_show_menu (eEMAIL_MENU menu)
                        testapp_print (" 9.  Backup All accounts\n");
                        testapp_print (" 10. Restore accounts\n");
                        testapp_print (" 11. Get password length of account\n");
-                       testapp_print (" 12. Query server info\n");
-                       testapp_print (" 13. Clear all notifications\n");
-                       testapp_print (" 14. Save default account ID\n");
-                       testapp_print (" 15. Load default account ID\n");
-                       testapp_print (" 16. Add certificate\n");
-                       testapp_print (" 17. Get certificate\n");
-                       testapp_print (" 18. Delete certificate\n");
-                       testapp_print (" 19. Add Account\n");
-                       testapp_print (" 20. Update peak schedule\n");
+                       testapp_print (" 13. Update notifications\n");
+                       testapp_print (" 14. Clear notifications\n");
+                       testapp_print (" 15. Clear all notifications\n");
+                       testapp_print (" 16. Save default account ID\n");
+                       testapp_print (" 17. Load default account ID\n");
+                       testapp_print (" 18. Add certificate\n");
+                       testapp_print (" 19. Get certificate\n");
+                       testapp_print (" 20. Delete certificate\n");
+                       testapp_print (" 21. Add Account\n");
+                       testapp_print (" 22. Update peak schedule\n");
                        testapp_print (" 0.  Go to Main Menu\n");
                        testapp_print ("------------------------------------------\n");
                        break;
@@ -134,6 +135,8 @@ void testapp_show_menu (eEMAIL_MENU menu)
                        testapp_print ("59. Add mail to search result table\n");
                        testapp_print ("60. Parse mime file\n");
                        testapp_print ("61. Write mime file\n");
+                       testapp_print ("63. Add mail with multiple recipient\n");
+                       testapp_print ("64. Send mails every x minutes\n");
                        testapp_print ("0.  Go to Main Menu\n");
                        testapp_print ("------------------------------------------\n");
                        break;
@@ -189,6 +192,7 @@ void testapp_show_menu (eEMAIL_MENU menu)
                        testapp_print ("   OTHERS\n");
                        testapp_print ("==========================================\n");
                        testapp_print ("1.  Ping service\n");
+                       testapp_print ("2.  Init storage\n"); 
                        testapp_print ("3.  Cancel Job\n");
                        testapp_print ("5.  Set DNET Proper Profile Type\n");
                        testapp_print ("6.  Get DNET Proper Profile Type\n");
@@ -200,6 +204,7 @@ void testapp_show_menu (eEMAIL_MENU menu)
                        testapp_print ("15. Show User Message\n");
                        testapp_print ("16. Get mime entity in signed file\n");
                        testapp_print ("17. Query SMTP mail size limit\n");
+                       testapp_print ("18. Verify email address\n");
                        testapp_print ("0.  Go to Main Menu\n");
                        testapp_print ("------------------------------------------\n");
                        break;