Coverity issue fixes for email service
[platform/core/messaging/email-service.git] / email-core / email-core-imap-mailbox.c
index aa3b5c8..4a96d77 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,8 +52,8 @@
 
 #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);
 
        int err = EMAIL_ERROR_NONE;
@@ -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, false);
        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,23 +84,25 @@ 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 *input_mailbox_tbl,
+                                                                                                       int *err_code)
 {
-       EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
+       EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], err_code[%p]", input_mailbox_tbl, err_code);
 
-       int ret = false; 
+       int ret = false;
        int *mail_id_list = NULL, mail_id_list_count = 0;
        int err = EMAIL_ERROR_NONE;
        email_account_t *account_ref = NULL;
-       
-       if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
-               if (intput_mailbox_tbl)
-               EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
+
+       if (!input_mailbox_tbl || input_mailbox_tbl->account_id < 1) {
+               if (input_mailbox_tbl)
+               EM_DEBUG_EXCEPTION("Invalid Parameter. input_mailbox_tbl->account_id [%d]", input_mailbox_tbl->account_id);
                err = EMAIL_ERROR_INVALID_PARAM;
                goto FINISH_OFF;
        }
 
-       account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
+       account_ref = emcore_get_account_reference(multi_user_name, input_mailbox_tbl->account_id, false);
        if (account_ref) {
                if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
                        EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
@@ -107,27 +110,42 @@ 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,
+                                                                                               input_mailbox_tbl->account_id,
+                                                                                               input_mailbox_tbl->mailbox_id,
+                                                                                               input_mailbox_tbl->mail_slot_size,
+                                                                                               &mail_id_list,
+                                                                                               &mail_id_list_count,
+                                                                                               false,
+                                                                                               &err)) {
                if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
-                       EM_DEBUG_LOG_SEC("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
+                       EM_DEBUG_LOG_SEC("There are enough slot in input_mailbox_tbl [%s]", input_mailbox_tbl->mailbox_name);
                        err = EMAIL_ERROR_NONE;
                        ret = true;
-               }
-               else
+               } else
                        EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
+
                goto FINISH_OFF;
        }
 
        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_mails_from_local_storage(multi_user_name,
+                                                                                                       input_mailbox_tbl->account_id,
+                                                                                                       mail_id_list,
+                                                                                                       mail_id_list_count,
+                                                                                                       EMAIL_DELETE_LOCALLY,
+                                                                                                       EMAIL_DELETED_BY_COMMAND,
+                                                                                                       &err)) {
                        EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
                        goto FINISH_OFF;
                }
        }
-       
+
        ret = true;
-FINISH_OFF: 
+
+FINISH_OFF:
+
        EM_SAFE_FREE(mail_id_list);
 
        if (account_ref) {
@@ -143,11 +161,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,31 +174,30 @@ 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, false);
                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;
                        goto FINISH_OFF;
-               }
-               else if (!account_ref) {
+               } else if (!account_ref) {
                        EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
                        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;
                        }
                }
-       }
-       else {
+       } 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) {
+                       for (i = 0; i < account_count; i++) {
+                               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 +205,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;
        }
@@ -198,31 +214,29 @@ INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int
                mailbox_count = 1;
                if (new_slot_size > 0) {
                        mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
-                       if(!mailbox_tbl_list) {
-                               EM_DEBUG_EXCEPTION("em_malloc failed");
+                       if (!mailbox_tbl_list) {
+                               EM_DEBUG_EXCEPTION("em_mallocfailed");
                                goto FINISH_OFF;
                        }
                        mailbox_tbl_list->account_id = account_id;
                        mailbox_tbl_list->mailbox_id = mailbox_id;
-                       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) {
+                       mailbox_tbl_list->mail_slot_size = new_slot_size;
+               } else {        /*  read information from DB */
+                       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)) {
+       } else {
+               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 +245,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 +265,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 +273,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, false);
        if (!ref_account)        {
                EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
                goto FINISH_OFF;
@@ -271,7 +285,7 @@ static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name
        *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
                                                                /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
        if (!*path) {
-               EM_DEBUG_EXCEPTION("em_malloc failed");
+               EM_DEBUG_EXCEPTION("em_mallocfailed");
                err = EMAIL_ERROR_OUT_OF_MEMORY;
                goto FINISH_OFF;
        }
@@ -310,17 +324,20 @@ 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 ) {
+       if (!remote_box_only || !local_box_only) {
                return ;
        }
        int i = 0;
@@ -329,57 +346,58 @@ static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_ma
        GList *remote_p    = NULL;
        GList *local_p     = NULL;
 
-       EM_DEBUG_LOG ("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
+       EM_DEBUG_LOG("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
 
        if (local_box_count == 0) {
-               for (i=0 ; i<remote_box_count ; i++) {
-                       *remote_box_only = g_list_prepend (*remote_box_only, remote_box_list+i);
+               for (i = 0; i < remote_box_count; i++) {
+                       *remote_box_only = g_list_prepend(*remote_box_only, remote_box_list+i);
                        *local_box_only = NULL;
                }
                return;
        }
 
-       for (i=0; i<remote_box_count; i++)
-               remote_head = g_list_prepend (remote_head, remote_box_list+i);
+       for (i = 0; i < remote_box_count; i++)
+               remote_head = g_list_prepend(remote_head, remote_box_list+i);
 
-       for (i=0; i<local_box_count; i++)
-               local_head = g_list_prepend (local_head, local_box_list+i);
+       for (i = 0; i < local_box_count; i++)
+               local_head = g_list_prepend(local_head, local_box_list+i);
 
        int matched = false;
-       for (remote_p = remote_head; remote_p ; remote_p = g_list_next (remote_p)) {
+       for (remote_p = remote_head; remote_p; remote_p = g_list_next(remote_p)) {
                matched = false ; /* initialized not matched for each iteration */
-               email_internal_mailbox_t *remote_box = (email_internal_mailbox_t *) g_list_nth_data (remote_p, 0);
-               EM_DEBUG_LOG_DEV ("remote [%s]",  remote_box->mailbox_name );
+               email_internal_mailbox_t *remote_box = (email_internal_mailbox_t *)g_list_nth_data(remote_p, 0);
+               EM_DEBUG_LOG_DEV("remote [%s]",  remote_box->mailbox_name);
                /* find matching mailbox in local box */
                for (local_p = local_head; local_p ; local_p = g_list_next(local_p)) {
-                       emstorage_mailbox_tbl_t *local_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (local_p, 0);
+                       emstorage_mailbox_tbl_t *local_box = (emstorage_mailbox_tbl_t *)g_list_nth_data(local_p, 0);
                        /* if match found */
-                       EM_DEBUG_LOG_DEV ("vs local [%s]", local_box->mailbox_name);
-                       if (!EM_SAFE_STRCMP (remote_box->mailbox_name, local_box->mailbox_name)) {
+                       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;
                        }
                }
                /* if matching not found, add it to remote box */
                if (matched == false) {
-                       EM_DEBUG_LOG ("New box: name[%s] alias[%s]", remote_box->mailbox_name, remote_box->alias);
-                       *remote_box_only = g_list_prepend (*remote_box_only, remote_box);
+                       EM_DEBUG_LOG("New box: name[%s] alias[%s]", remote_box->mailbox_name, remote_box->alias);
+                       *remote_box_only = g_list_prepend(*remote_box_only, remote_box);
                }
        }
 
        /* 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 +410,64 @@ 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))
-               EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
+       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 >>>> ");
+
+       FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
        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, false);
        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) {
+       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,
+                                                                                       true,
+                                                                                       (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,50 +475,41 @@ 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);
-               }
-               else
-                       EM_DEBUG_LOG ("mailbox not found");
+               } else
+                       EM_DEBUG_LOG("mailbox not found");
        }
 
-       if (!emcore_check_thread_status())  {
-               err = EMAIL_ERROR_CANCELLED;
-               goto FINISH_OFF;
-       }
-       
-       emcore_find_mailbox_diff_between_local_and_remote (mailbox_list, count, local_mailbox_list, local_mailbox_count,
+       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);
 
        /* for remote_box_only, add new mailbox to DB */
        GList *p = remote_box_only;
-       for (; p; p = g_list_next (p)) {
-               email_internal_mailbox_t *new_mailbox = (email_internal_mailbox_t *) g_list_nth_data (p, 0);
+       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;
                }
 
-               /* EM_DEBUG_LOG_SEC ("mailbox name [%s]", new_mailbox->mailbox_name); */
+               /* EM_DEBUG_LOG_SEC("mailbox name [%s]", new_mailbox->mailbox_name); */
                new_mailbox->mail_slot_size = ref_account->default_mail_slot_size;
 
                if (new_mailbox->mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
-                       emcore_bind_mailbox_type (new_mailbox);
+                       emcore_bind_mailbox_type(new_mailbox);
 
                if (new_mailbox->mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) {       /* if result mailbox type is duplicated,  */
                        if (mailbox_type_list[new_mailbox->mailbox_type] != -1) {
-                               EM_DEBUG_LOG_SEC ("Mailbox type [%d] of [%s] is duplicated", new_mailbox->mailbox_type, new_mailbox->mailbox_name);
+                               EM_DEBUG_LOG_SEC("Mailbox type [%d] of [%s] is duplicated", new_mailbox->mailbox_type, new_mailbox->mailbox_name);
                                new_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one  */
-                       }
-                       else
+                       } else
                                mailbox_type_list[new_mailbox->mailbox_type] = 1;
                }
 
@@ -521,35 +526,46 @@ INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, i
 
                /* Get the Alias Name after Parsing the Full mailbox Path */
                if (new_mailbox->alias == NULL)
-                       new_mailbox->alias = emcore_get_alias_of_mailbox ((const char *)new_mailbox->mailbox_name);
+                       new_mailbox->alias = emcore_get_alias_of_mailbox((const char *)new_mailbox->mailbox_name);
 
                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)) {
-                               EM_DEBUG_EXCEPTION ("emstorage_add_mailbox error [%d]", err);
+
+                       if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
+                               EM_DEBUG_EXCEPTION("emstorage_add_mailbox error [%d]", err);
                                goto FINISH_OFF;
                        }
-                       EM_DEBUG_LOG_SEC ("MAILBOX ADDED: mailbox_name [%s] alias [%s] mailbox_type [%d]", new_mailbox->mailbox_name,\
+
+                       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);
                }
        }
 
        /* delete all local boxes and mails */
        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)) {
-                       EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
+       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(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);
                }
-               EM_DEBUG_LOG_SEC ("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
+
+#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);
        }
 
        for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
@@ -558,8 +574,8 @@ 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(result_mailbox_tbl) {
+                       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,38 +624,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))
-                       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))
-                       EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
+               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, 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);
        EM_SAFE_FREE(mbox_path);
@@ -649,20 +664,20 @@ FINISH_OFF:
                EM_SAFE_FREE(ref_account);
        }
 
-       if (stream) 
-               stream = mail_close (stream);
-       
-       if (mailbox_list) 
-               emcore_free_internal_mailbox (&mailbox_list, count, NULL);
+       if (stream)
+               stream = mail_close(stream);
 
-       if (local_mailbox_list) 
-               emstorage_free_mailbox (&local_mailbox_list, local_mailbox_count, NULL);
+       if (mailbox_list)
+               emcore_free_internal_mailbox(&mailbox_list, count, NULL);
 
-       if (local_box_only) 
-               g_list_free (local_box_only);
+       if (local_mailbox_list)
+               emstorage_free_mailbox(&local_mailbox_list, local_mailbox_count, NULL);
+
+       if (local_box_only)
+               g_list_free(local_box_only);
 
        if (remote_box_only)
-               g_list_free (remote_box_only);
+               g_list_free(remote_box_only);
 
        if (err_code != NULL)
                *err_code = err;
@@ -670,10 +685,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 +704,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") */
@@ -700,10 +715,9 @@ int emcore_download_mailbox_list(void *mail_stream,
                        strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
                        if ((s = strchr(reference, '}')))
                                *(++s) = '\0';
-                       strcat(reference, mailbox_name);
+                       EM_SAFE_STRNCAT(reference, mailbox_name, (EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1) - EM_SAFE_STRLEN(reference) - 1);
                }
-       }
-       else
+       } else
                reference = EM_SAFE_STRDUP(stream->original_mailbox);
 
        pattern        = "*";
@@ -711,7 +725,7 @@ int emcore_download_mailbox_list(void *mail_stream,
 
        /*  imap command : tag LIST reference * */
        /*  see callback function mm_list */
-       mail_list (stream, reference, pattern);
+       mail_list(stream, reference, pattern);
 
        stream->sparep = NULL;
 
@@ -720,9 +734,31 @@ int emcore_download_mailbox_list(void *mail_stream,
        *count        = holder.num;
        *mailbox_list = (email_internal_mailbox_t*) holder.data;
 
-       ret = true;
 
-FINISH_OFF: 
+       EM_DEBUG_LOG("count: [%d], mailbox_list[%p]", *count, *mailbox_list);
+       if (*count <= 0 || *mailbox_list == NULL) {
+               err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
+               ret = false;
+       } else {
+               ret = true;
+
+               /* checking once more if there is a INBOX or not */
+               int i = 0;
+               bool is_found_inbox = false;
+               for (i = 0; i < *count; i++) {
+                       if ((*mailbox_list)[i].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX || !g_ascii_strcasecmp((*mailbox_list)[i].mailbox_name, "INBOX")) {
+                               is_found_inbox = true;
+                               break;
+                       }
+               }
+               if (!is_found_inbox) {
+                       EM_DEBUG_CRITICAL_EXCEPTION("There is no INBOX!");
+                       err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
+                       ret = false;
+               }
+       }
+
+FINISH_OFF:
        if (err_code)
                *err_code = err;
 
@@ -737,13 +773,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 +789,17 @@ 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,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -762,7 +807,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 +815,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;
        }
 
@@ -780,20 +836,21 @@ INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_
 
 FINISH_OFF:
        if (stream) {
-               stream = mail_close (stream);
+               stream = mail_close(stream);
        }
 
        EM_SAFE_FREE(long_enc_path);
 
        if (mailbox) {
                if (err == EMAIL_ERROR_NONE) {
-                       if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
+                       if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
                                EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
-               }
-               else if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
+               } else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
        }
 
+       emcore_clear_session(session);
+
        if (err_code)
                *err_code = err;
 
@@ -809,7 +866,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();
 
@@ -821,16 +878,15 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code
        int err = EMAIL_ERROR_NONE;
        emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
 
-       if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
+       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, false);
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -838,7 +894,12 @@ 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,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -846,7 +907,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;
        }
@@ -864,7 +925,7 @@ INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code
 
 FINISH_OFF:
        if (stream) {
-               stream = mail_close (stream);
+               stream = mail_close(stream);
        }
 
        if (ref_account) {
@@ -878,10 +939,9 @@ FINISH_OFF:
                emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
 
        if (err == EMAIL_ERROR_NONE) {
-               if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
+               if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
-       }
-       else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
+       } else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
                EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
 
        if (err_code)
@@ -891,7 +951,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,8 +967,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, false);
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -917,7 +976,12 @@ 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,
+                                                                                       true,
+                                                                                       (void **)&tmp_stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
                goto FINISH_OFF;
        }
@@ -925,13 +989,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;
        }
@@ -944,11 +1008,10 @@ INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int
 
 FINISH_OFF:
 
-       if (err  == EMAIL_ERROR_NONE) {
-               if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
+       if (err == EMAIL_ERROR_NONE) {
+               if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
-       }
-       else {
+       } else {
                if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
                        EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
        }
@@ -961,7 +1024,7 @@ FINISH_OFF:
        }
 
        if (stream) {
-               stream = mail_close (stream);
+               stream = mail_close(stream);
        }
 
        EM_DEBUG_FUNC_END("err [%d]", err);
@@ -974,7 +1037,7 @@ FINISH_OFF:
 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
 {
        EM_DEBUG_FUNC_BEGIN();
-       quota_t ret_quota;
+       quota_t ret_quota = NIL;
        EM_DEBUG_FUNC_END();
        return ret_quota;
 }
@@ -982,7 +1045,7 @@ quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox,
 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
 {
        EM_DEBUG_FUNC_BEGIN();
-       quota_t ret_quota;
+       quota_t ret_quota = NIL;
        EM_DEBUG_FUNC_END();
        return ret_quota;
 }
@@ -1008,13 +1071,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, false);
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
                err = EMAIL_ERROR_INVALID_ACCOUNT;
@@ -1022,7 +1084,12 @@ 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,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }
@@ -1043,12 +1110,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, false);
 
        if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
                EM_DEBUG_EXCEPTION("Invalid account information");
@@ -1057,7 +1124,12 @@ 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,
+                                                                                       true,
+                                                                                       (void **)&stream,
+                                                                                       &err)) {
                EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
                goto FINISH_OFF;
        }