4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 /******************************************************************************
24 * File : email-core-account.c
25 * Desc : Account Management
30 * 2010.08.25 : created
31 *****************************************************************************/
36 #include <sys/types.h>
39 #include "email-convert.h"
40 #include "email-types.h"
41 #include "email-daemon.h"
42 #include "email-debug-log.h"
43 #include "email-storage.h"
44 #include "email-network.h"
45 #include "email-utilities.h"
46 #include "email-core-utils.h"
47 #include "email-core-event.h"
48 #include "email-core-global.h"
49 #include "email-core-account.h"
50 #include "email-core-mailbox.h"
51 #include "email-core-signal.h"
52 #include "email-core-imap-mailbox.h"
54 #ifdef __FEATURE_USING_ACCOUNT_SVC__
56 #include "account-types.h"
57 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
59 char *g_default_mbox_alias[MAILBOX_COUNT] =
61 EMAIL_INBOX_DISPLAY_NAME,
62 EMAIL_DRAFTBOX_DISPLAY_NAME,
63 EMAIL_OUTBOX_DISPLAY_NAME,
64 EMAIL_SENTBOX_DISPLAY_NAME,
65 EMAIL_TRASH_DISPLAY_NAME,
66 EMAIL_SPAMBOX_DISPLAY_NAME,
69 char *g_default_mbox_name[MAILBOX_COUNT] =
75 EMAIL_TRASH_DISPLAY_NAME,
79 email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] =
81 EMAIL_MAILBOX_TYPE_INBOX,
82 EMAIL_MAILBOX_TYPE_DRAFT,
83 EMAIL_MAILBOX_TYPE_OUTBOX,
84 EMAIL_MAILBOX_TYPE_SENTBOX,
85 EMAIL_MAILBOX_TYPE_TRASH,
86 EMAIL_MAILBOX_TYPE_SPAMBOX,
89 static email_account_list_t *g_account_list = NULL;
90 static int g_account_num = 0;
91 static pthread_mutex_t _account_ref_lock = PTHREAD_MUTEX_INITIALIZER;
93 INTERNAL_FUNC email_account_t* emcore_get_account_reference(int account_id)
95 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
96 email_account_list_t **p;
97 email_account_t *result_account = NULL;
100 emcore_get_account_from_unvalidated_account_list(account_id, &result_account);
101 return result_account;
103 else if (account_id > 0) {
104 ENTER_CRITICAL_SECTION(_account_ref_lock);
107 if ((*p)->account->account_id == account_id) {
108 emcore_duplicate_account((*p)->account, &result_account, NULL);
113 LEAVE_CRITICAL_SECTION(_account_ref_lock);
118 /* refresh and check once again */
119 emcore_init_account_reference();
120 ENTER_CRITICAL_SECTION(_account_ref_lock);
121 if (g_account_num > 0 && g_account_list) {
124 if ((*p)->account->account_id == account_id) {
125 emcore_duplicate_account((*p)->account, &result_account, NULL);
131 LEAVE_CRITICAL_SECTION(_account_ref_lock);
135 EM_DEBUG_FUNC_END("[%p]", result_account);
136 return result_account;
140 INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, int *err_code)
142 EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p], incoming_server_address [%s]", account, err_code, account->incoming_server_address);
145 int err = EMAIL_ERROR_NONE;
146 email_session_t *session = NULL;
147 SENDSTREAM *stream = NULL;
148 MAILSTREAM *tmp_stream = NULL;
150 if (!emcore_check_thread_status()) {
151 err = EMAIL_ERROR_CANCELLED;
155 if (!emnetwork_check_network_status(&err)) {
156 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
159 EM_DEBUG_LOG("Network available");
161 if (!emcore_check_thread_status()) {
162 err = EMAIL_ERROR_CANCELLED;
166 if (!emcore_get_empty_session(&session)) {
167 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
168 err = EMAIL_ERROR_SESSION_NOT_FOUND;
172 #ifdef _SMTP_ACCOUNT_VALIDATION_
173 /* validate connection for smt */
174 EM_DEBUG_LOG("Validate connection for SMTP");
176 if (!emcore_check_thread_status()) {
177 err = EMAIL_ERROR_CANCELLED;
180 if (!emcore_connect_to_remote_mailbox_with_account_info(account, (char *)ENCODED_PATH_SMTP, (void **)&stream, &err) || !stream) {
181 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed 1 - %d", err);
182 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
183 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication fail 1- %d", err);
187 if (account->outgoing_server_secure_connection == 0x01) /* 0x01 == ss */ {
189 EM_DEBUG_LOG("Retry with TLS");
190 account->outgoing_server_secure_connection = 0x02; /* 0x02 == tl */
191 if (!emcore_check_thread_status()) {
192 err = EMAIL_ERROR_CANCELLED;
196 if (!emcore_connect_to_remote_mailbox_with_account_info(account, (char *)ENCODED_PATH_SMTP, (void **)&stream, &err) || !stream) {
197 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed 2 - %d", err);
198 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
199 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication fail 2 - %d", err);
201 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
202 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
204 account->outgoing_server_secure_connection = 0x01; /* restore to the previous value */
208 if (!emcore_check_thread_status()) {
209 err = EMAIL_ERROR_CANCELLED;
213 /* save outgoing_server_secure_connection = 0x02 (tls) to the d */
214 if (!emstorage_update_account(account_id, (emstorage_account_tbl_t *)account, true, &err)) {
215 EM_DEBUG_EXCEPTION("emstorage_update_account failed - %d", err);
216 account->outgoing_server_secure_connection = 0x01; /* restore to the previous value */
217 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
222 if (EMAIL_ERROR_CONNECTION_FAILURE != err)
223 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
229 /* validate connection for pop3/ima */
230 EM_DEBUG_LOG("Validate connection for POP3/IMAP4");
231 if (EMAIL_ERROR_NONE == err) {
232 if (!emcore_check_thread_status()) {
233 err = EMAIL_ERROR_CANCELLED;
237 if (!emcore_connect_to_remote_mailbox_with_account_info(account, 0, (void **)&tmp_stream, &err) || !tmp_stream)
239 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
240 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
241 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
243 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
244 /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
250 if (!emcore_check_thread_status()) {
251 if (!emcore_delete_account(account->account_id, NULL))
252 EM_DEBUG_EXCEPTION("emdaemon_delete_account failed [%d]", account->account_id);
253 err = EMAIL_ERROR_CANCELLED;
264 emcore_close_mailbox(0 , tmp_stream);
266 if (err_code != NULL)
269 emcore_clear_session(session);
276 INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
278 EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
280 int err = EMAIL_ERROR_NONE, ret = false;
281 email_account_t *ref_account = NULL;
286 EM_DEBUG_EXCEPTION("account_id[%p]", account_id);
287 err = EMAIL_ERROR_INVALID_PARAM;
291 ref_account = emcore_get_account_reference(account_id);
293 if (ref_account && emcore_validate_account_with_account_info(ref_account, &err) == false) {
294 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed (%d)", err);
303 emcore_free_account(ref_account);
304 EM_SAFE_FREE(ref_account);
315 INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
317 EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
319 /* default variabl */
321 int err = EMAIL_ERROR_NONE;
323 if (account_id < FIRST_ACCOUNT_ID) {
324 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
325 err = EMAIL_ERROR_INVALID_PARAM;
329 #ifdef __FEATURE_LOCAL_ACTIVITY__
330 /* Delete all local activities of previous account */
333 emstorage_activity_tbl_t activity;
334 memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
335 activity.account_id = account_id;
337 if (!emcore_delete_activity(&activity, &err)) {
338 EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
344 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
345 if (false == emcore_clear_partial_body_thd_event_que(&err))
346 EM_DEBUG_EXCEPTION(" emcore_clear_partial_body_thd_event_que [%d]", err);
348 if (false == emstorage_delete_full_pbd_activity_data(account_id, true, &err))
349 EM_DEBUG_EXCEPTION("emstorage_delete_full_pbd_activity_data failed [%d]", err);
353 #ifdef __FEATURE_USING_ACCOUNT_SVC__
356 email_account_t *account_to_be_deleted;
358 account_to_be_deleted = emcore_get_account_reference(account_id);
359 if (account_to_be_deleted && account_to_be_deleted->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
360 EM_DEBUG_LOG("Calling account_svc_delete with account_svc_id[%d]", account_to_be_deleted->account_svc_id);
361 error_code = account_connect();
362 EM_DEBUG_LOG("account_connect returns [%d]", error_code);
363 error_code = account_delete_from_db_by_id(account_to_be_deleted->account_svc_id);
364 EM_DEBUG_LOG("account_delete_from_db_by_id returns [%d]", error_code);
365 error_code = account_disconnect();
366 EM_DEBUG_LOG("account_disconnect returns [%d]", error_code);
369 if (account_to_be_deleted) {
370 emcore_free_account(account_to_be_deleted);
371 EM_SAFE_FREE(account_to_be_deleted);
375 if (emcore_cancel_all_threads_of_an_account(account_id) < EMAIL_ERROR_NONE) {
376 EM_DEBUG_EXCEPTION("There are some remaining jobs. I couldn't stop them.");
377 err = EMAIL_ERROR_CANNOT_STOP_THREAD;
381 /* Delete contact log */
382 if ( ((err = emcore_delete_contacts_log(account_id)) != EMAIL_ERROR_NONE) && (err != EMAIL_ERROR_DATA_NOT_FOUND) ) {
383 EM_DEBUG_EXCEPTION("emcore_delete_contacts_log failed : [%d]", err);
386 /* BEGIN TRANSACTION; */
387 emstorage_begin_transaction(NULL, NULL, NULL);
389 if (!emstorage_delete_account(account_id, false, &err)) {
390 EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
394 #ifdef __FEATURE_KEEP_CONNECTION__
395 /* emcore_reset_streams(); */
396 emcore_remove_connection_info(account_id);
399 if ((err = emcore_delete_all_mails_of_acount(account_id)) != EMAIL_ERROR_NONE) {
400 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_acount failed [%d]", err);
404 /* delete all mailboxes */
405 if (!emstorage_delete_mailbox(account_id, -1, 0, false, &err)) {
406 EM_DEBUG_EXCEPTION("emstorage_delete_mailbox failed - %d", err);
410 /* delete local imap sync mailbox from imap mailbox tabl */
411 if (!emstorage_remove_downloaded_mail(account_id, NULL, NULL, false, &err)) {
412 EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
416 emcore_display_unread_in_badge();
417 emcore_delete_notification_by_account(account_id);
418 emcore_init_account_reference();
423 if (ret == true) { /* COMMIT TRANSACTION; */
424 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
425 err = EMAIL_ERROR_DB_FAILURE;
428 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE, account_id, 0, NULL, 0))
429 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
432 else { /* ROLLBACK TRANSACTION; */
433 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
434 err = EMAIL_ERROR_DB_FAILURE;
435 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE_FAIL, account_id, err, NULL, 0))
436 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
447 INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
449 EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p]", account, err_code);
452 int err = EMAIL_ERROR_NONE;
455 char vconf_private_id[MAX_PATH] = {0, };
456 email_mailbox_t local_mailbox = {0};
457 emstorage_account_tbl_t *temp_account_tbl = NULL;
459 if (account == NULL) {
460 EM_DEBUG_EXCEPTION("account[%p]", account);
461 err = EMAIL_ERROR_INVALID_PARAM;
465 if (!emstorage_get_account_count(&count, true, &err)) {
466 EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
471 if (count >= EMAIL_ACCOUNT_MAX) {
472 EM_DEBUG_EXCEPTION("too many accounts...");
473 err = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
477 account->account_id = 0;
479 /* Temporarily code - begin */
480 if (account->auto_download_size == 0) {
481 account->auto_download_size = PARTIAL_BODY_SIZE_IN_BYTES;
482 EM_DEBUG_LOG("account->auto_download_size [%d]", account->auto_download_size);
485 if (account->default_mail_slot_size == 0) {
486 account->default_mail_slot_size = 50;
487 EM_DEBUG_LOG("account->default_mail_slot_size [%d]", account->default_mail_slot_size);
489 /* Temporarily code - end */
491 /* check for email address validation */
492 EM_DEBUG_LOG("account->user_email_address[%s]", account->user_email_address);
493 if (account->user_email_address) {
494 if (!em_verify_email_address(account->user_email_address, true, &err)) {
495 err = EMAIL_ERROR_INVALID_ADDRESS;
496 EM_DEBUG_EXCEPTION("Invalid Email Address");
501 #ifdef __FEATURE_USING_ACCOUNT_SVC__
502 if (account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
503 int account_svc_id = 0;
505 account_h account_handle = NULL;
507 error_code = account_connect();
508 if(error_code != ACCOUNT_ERROR_NONE) {
509 EM_DEBUG_EXCEPTION("account_connect failed [%d]", error_code);
514 error_code = account_create(&account_handle);
515 if(error_code != ACCOUNT_ERROR_NONE) {
516 EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
518 account_disconnect();
522 account_set_user_name(account_handle, account->incoming_server_user_name);
523 account_set_domain_name(account_handle, account->account_name);
524 account_set_email_address(account_handle, account->user_email_address);
525 account_set_source(account_handle, "SLP EMAIL");
526 account_set_package_name(account_handle, "email-setting-efl");
527 /* account_set_capability(account_handle , ACCOUNT_CAPABILITY_EMAIL, ACCOUNT_CAPABILITY_ENABLED); OLD API */
528 account_set_capability(account_handle , ACCOUNT_SUPPORTS_CAPABILITY_EMAIL , ACCOUNT_CAPABILITY_ENABLED);
529 account_set_sync_support(account_handle, ACCOUNT_SYNC_STATUS_IDLE); /* This means "The account is supporting 'sync' and initialized as idle status" */
530 if (account->logo_icon_path)
531 account_set_icon_path(account_handle, account->logo_icon_path);
532 error_code = account_insert_to_db(account_handle, &account_svc_id);
534 if (error_code != ACCOUNT_ERROR_NONE) {
535 EM_DEBUG_EXCEPTION("account_insert_to_db failed [%d]", error_code);
538 account_destroy(account_handle);
540 account_disconnect();
544 account->account_svc_id = account_svc_id;
546 EM_DEBUG_LOG("account_insert_to_db succeed");
549 account_destroy(account_handle);
550 account_disconnect();
552 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
554 temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
555 if (!temp_account_tbl) {
556 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
559 em_convert_account_to_account_tbl(account, temp_account_tbl);
561 if (!emstorage_add_account(temp_account_tbl, true, &err)) {
562 EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
565 account->account_id = temp_account_tbl->account_id;
567 if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
568 /* 1. create default local mailbox
569 * (Inbox, Draft, Outbox, Sentbox) */
570 for (i = 0; i < MAILBOX_COUNT; i++) {
571 EM_DEBUG_LOG("g_default_mbox_name [%d/%d] is [%s]", i, MAILBOX_COUNT, g_default_mbox_name[i]);
572 local_mailbox.account_id = temp_account_tbl->account_id;
573 local_mailbox.mailbox_name = g_default_mbox_name[i];
574 local_mailbox.mailbox_type = g_default_mbox_type[i];
575 if (local_mailbox.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) {
576 local_mailbox.local = EMAIL_MAILBOX_FROM_SERVER;
579 local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
581 local_mailbox.alias = g_default_mbox_alias[i];
582 local_mailbox.mail_slot_size = temp_account_tbl->default_mail_slot_size;
584 if (!emcore_create_mailbox(&local_mailbox, 0, &err)) {
585 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
592 /* Initialize the noti private id */
593 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account->account_id);
594 if (vconf_get_int(vconf_private_id, &private_id) != 0) {
595 EM_DEBUG_EXCEPTION("vconf_get_int failed");
597 if (vconf_set_int(vconf_private_id, 0) != 0) {
598 EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
605 if (temp_account_tbl)
606 emstorage_free_account(&temp_account_tbl, 1, NULL);
608 if (ret == false && account != NULL) {
609 if (!emcore_delete_account(account->account_id, NULL))
610 EM_DEBUG_EXCEPTION("emdaemon_delete_account Failed [%d]", account->account_id);
616 EM_DEBUG_FUNC_END("Return value [%d]", ret);
621 INTERNAL_FUNC int emcore_init_account_reference()
623 EM_DEBUG_FUNC_BEGIN();
625 int err = EMAIL_ERROR_NONE;
627 email_account_list_t *account_list = NULL;
628 email_account_list_t **p = NULL;
629 email_account_t *account = NULL;
630 emstorage_account_tbl_t *account_tbl_array = NULL;
634 /* free account reference if any */
635 emcore_free_account_reference();
637 if (!emstorage_get_account_list(&count, &account_tbl_array, true, true, &err)) {
638 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
642 for (p = &account_list, i = 0; i < count; i++) {
643 account = em_malloc(sizeof(email_account_t));
645 EM_DEBUG_EXCEPTION("malloc failed...");
646 err = EMAIL_ERROR_OUT_OF_MEMORY;
650 em_convert_account_tbl_to_account(account_tbl_array + i, account);
652 *p = (email_account_list_t*) em_malloc(sizeof(email_account_list_t));
654 EM_DEBUG_EXCEPTION("malloc failed...");
655 emcore_free_account(account);
656 EM_SAFE_FREE(account);
657 err = EMAIL_ERROR_OUT_OF_MEMORY;
661 (*p)->account = account;
666 ENTER_CRITICAL_SECTION(_account_ref_lock);
667 g_account_num = count;
668 g_account_list = account_list;
669 LEAVE_CRITICAL_SECTION(_account_ref_lock);
672 if (account_tbl_array)
673 emstorage_free_account(&account_tbl_array, count, NULL);
675 if (err != EMAIL_ERROR_NONE && account_list && i > 0) {
676 g_account_list = account_list;
677 emcore_free_account_reference();
680 EM_DEBUG_FUNC_END("err [%d]", err);
684 INTERNAL_FUNC int emcore_free_account_reference()
686 EM_DEBUG_FUNC_BEGIN();
687 ENTER_CRITICAL_SECTION(_account_ref_lock);
689 email_account_list_t *p = g_account_list;
690 email_account_list_t *p_next = NULL;
692 emcore_free_account(p->account);
693 EM_SAFE_FREE(p->account);
701 g_account_list = NULL;
703 LEAVE_CRITICAL_SECTION(_account_ref_lock);
708 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
710 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
713 int err = EMAIL_ERROR_NONE;
715 if (count <= 0 || !account_list || !*account_list) {
716 err = EMAIL_ERROR_INVALID_PARAM;
720 email_account_t *p = *account_list;
722 for (i = 0; i < count; i++)
723 emcore_free_account(p+i);
726 *account_list = NULL;
737 INTERNAL_FUNC void emcore_free_option(email_option_t *option)
739 EM_SAFE_FREE(option->display_name_from);
740 EM_SAFE_FREE(option->signature);
744 INTERNAL_FUNC void emcore_free_account(email_account_t *account)
748 EM_SAFE_FREE(account->account_name);
749 EM_SAFE_FREE(account->incoming_server_address);
750 EM_SAFE_FREE(account->user_email_address);
751 EM_SAFE_FREE(account->incoming_server_user_name);
752 EM_SAFE_FREE(account->incoming_server_password);
753 EM_SAFE_FREE(account->outgoing_server_address);
754 EM_SAFE_FREE(account->outgoing_server_user_name);
755 EM_SAFE_FREE(account->outgoing_server_password);
756 EM_SAFE_FREE(account->user_display_name);
757 EM_SAFE_FREE(account->reply_to_address);
758 EM_SAFE_FREE(account->return_address);
759 EM_SAFE_FREE(account->logo_icon_path);
760 EM_SAFE_FREE(account->certificate_path);
761 EM_SAFE_FREE(account->user_data);
762 account->user_data_length = 0;
763 emcore_free_option(&account->options);
770 INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, email_account_t **account_dup, int *err_code)
772 EM_DEBUG_FUNC_BEGIN("account[%p]", account);
773 email_account_t *temp_account = NULL;
775 if(!account || !account_dup) { /*prevent 40514*/
776 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
777 if(err_code) *err_code = EMAIL_ERROR_INVALID_PARAM;
781 *account_dup = em_malloc(sizeof(email_account_t));
782 if(!*account_dup) { /*prevent 40514*/
783 EM_DEBUG_EXCEPTION("malloc failed...");
784 if(err_code) *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
788 memcpy(*account_dup, account , sizeof(email_account_t));
789 temp_account = *account_dup;
791 temp_account->account_name = EM_SAFE_STRDUP(account->account_name);
792 temp_account->incoming_server_address = EM_SAFE_STRDUP(account->incoming_server_address);
793 temp_account->user_email_address = EM_SAFE_STRDUP(account->user_email_address);
794 temp_account->incoming_server_user_name = EM_SAFE_STRDUP(account->incoming_server_user_name);
795 temp_account->incoming_server_password = EM_SAFE_STRDUP(account->incoming_server_password);
796 temp_account->outgoing_server_address = EM_SAFE_STRDUP(account->outgoing_server_address);
797 temp_account->outgoing_server_user_name = EM_SAFE_STRDUP(account->outgoing_server_user_name);
798 temp_account->outgoing_server_password = EM_SAFE_STRDUP(account->outgoing_server_password);
799 temp_account->user_display_name = EM_SAFE_STRDUP(account->user_display_name);
800 temp_account->reply_to_address = EM_SAFE_STRDUP(account->reply_to_address);
801 temp_account->return_address = EM_SAFE_STRDUP(account->return_address);
802 temp_account->logo_icon_path = EM_SAFE_STRDUP(account->logo_icon_path);
803 temp_account->user_data = em_memdup(account->user_data, account->user_data_length);
804 temp_account->options.display_name_from = EM_SAFE_STRDUP(account->options.display_name_from);
805 temp_account->options.signature = EM_SAFE_STRDUP(account->options.signature);
806 temp_account->certificate_path = EM_SAFE_STRDUP(account->certificate_path);
808 if (err_code != NULL)
809 *err_code = EMAIL_ERROR_NONE;
814 INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code)
816 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
817 int i, countOfAccounts = 0;
819 int err = EMAIL_ERROR_NONE;
820 email_account_t *accountRef;
821 email_account_list_t *p;
823 ENTER_CRITICAL_SECTION(_account_ref_lock);
825 if (!account_list || !count) {
826 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
827 err = EMAIL_ERROR_INVALID_PARAM;
838 EM_DEBUG_LOG("Result count[%d]", countOfAccounts);
839 *count = countOfAccounts;
841 if (countOfAccounts > 0) {
842 *account_list = malloc(sizeof(email_account_t) * countOfAccounts);
843 if (!*account_list) {
844 EM_DEBUG_LOG("malloc failed...");
845 err = EMAIL_ERROR_OUT_OF_MEMORY;
851 for (i = 0; i < countOfAccounts; i++) {
852 accountRef = (*account_list) + i;
853 memcpy(accountRef, p->account , sizeof(email_account_t));
854 EM_DEBUG_LOG("Result account id[%d], name[%s]", accountRef->account_id, accountRef->account_name);
862 if (account_list && *account_list)
863 EM_SAFE_FREE(*account_list);
866 LEAVE_CRITICAL_SECTION(_account_ref_lock);
868 if (err_code != NULL)
874 #ifdef __FEATURE_BACKUP_ACCOUNT__
875 #include <ss_manager.h>
877 static int append_data_into_buffer(char **target_buffer, int *target_buffer_lenth, char *input_data, int input_data_length, int *error_code)
879 EM_DEBUG_FUNC_BEGIN("target_buffer [%p], target_buffer_lenth [%p], input_data [%p], input_data_length[%d]", target_buffer, target_buffer_lenth, input_data, input_data_length);
880 int local_error_code = EMAIL_ERROR_NONE, ret_code = false;
882 if (!target_buffer || !target_buffer_lenth || !input_data) {
883 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
884 local_error_code = EMAIL_ERROR_INVALID_PARAM;
888 if (*target_buffer_lenth > 0 && input_data_length) {
889 EM_DEBUG_LOG("*target_buffer_lenth [%d]", *target_buffer_lenth);
890 *target_buffer = realloc(*target_buffer, (*target_buffer_lenth) + input_data_length);
891 if (!*target_buffer) {
892 EM_DEBUG_EXCEPTION("realloc failed");
893 local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
896 memcpy(*target_buffer + (*target_buffer_lenth), input_data, input_data_length);
897 *target_buffer_lenth += input_data_length;
898 EM_DEBUG_LOG("*target_buffer_lenth [%d] input_data_length [%d]", *target_buffer_lenth, input_data_length);
901 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
902 local_error_code = EMAIL_ERROR_INVALID_PARAM;
911 *error_code = local_error_code;
912 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
918 static int emcore_write_account_into_buffer(char **target_buffer, int *target_buffer_lenth, emstorage_account_tbl_t *account_tbl_ptr, int *error_code)
920 EM_DEBUG_FUNC_BEGIN("target_buffer [%p], target_buffer_lenth [%p], account_tbl_ptr [%p], error_code [%p]", target_buffer, target_buffer_lenth, account_tbl_ptr, error_code);
921 int local_error_code = EMAIL_ERROR_NONE, ret_code = false, stream_length = 0;
922 email_account_t temp_account = {0};
923 char *byte_stream = NULL;
925 if (em_convert_account_tbl_to_account(account_tbl_ptr, &temp_account)) {
926 byte_stream = em_convert_account_to_byte_stream(&temp_account, &stream_length);
927 EM_DEBUG_LOG("stream_length [%d]", stream_length);
928 /* EM_DEBUG_LOG("incoming_server_password [%s]", temp_account->password) */
931 if (!append_data_into_buffer(target_buffer, target_buffer_lenth, (char *)&stream_length, sizeof(int), &local_error_code)) {
932 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
935 EM_DEBUG_LOG("append_data_into_buffer succeed for stream_length");
937 if (!append_data_into_buffer(target_buffer, target_buffer_lenth, byte_stream, stream_length, &local_error_code)) {
938 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
941 EM_DEBUG_LOG("append_data_into_buffer succeed for byte_stream");
945 EM_DEBUG_EXCEPTION("em_convert_account_tbl_to_account failed");
946 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
952 emcore_free_account(&temp_account);
954 *error_code = local_error_code;
956 EM_SAFE_FREE(byte_stream);
958 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
962 INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
964 EM_DEBUG_FUNC_BEGIN("file_path [%s], error_code [%p]", file_path, error_code);
965 int local_error_code = EMAIL_ERROR_NONE, local_error_code_2 = EMAIL_ERROR_NONE, ret_code = false;
966 int select_num, i, target_buff_length = 0;
967 char *target_buffer = NULL;
968 emstorage_account_tbl_t *account_list = NULL;
971 local_error_code = EMAIL_ERROR_INVALID_PARAM;
972 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
978 if (!emstorage_get_account_list(&select_num, &account_list, true, true, &local_error_code)) {
979 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", local_error_code);
983 EM_DEBUG_LOG("select_num [%d]", select_num);
986 target_buffer = em_malloc(sizeof(int));
987 if (!target_buffer) {
988 EM_DEBUG_EXCEPTION("malloc failed");
989 local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
993 memcpy(target_buffer, (char *)&select_num, sizeof(int));
994 target_buff_length = sizeof(int);
996 for (i = 0; i < select_num; i++) {
997 if (!emcore_write_account_into_buffer(&target_buffer, &target_buff_length, account_list + i, &local_error_code)) {
998 EM_DEBUG_EXCEPTION("emcore_write_account_into_buffer failed [%d]", local_error_code);
1003 EM_DEBUG_LOG("target_buff_length [%d]", target_buff_length);
1005 ssm_delete_file(file_path, SSM_FLAG_SECRET_OPERATION, NULL);
1007 if (ssm_write_buffer(target_buffer, target_buff_length, file_path, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
1008 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d]", local_error_code);
1009 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
1018 EM_SAFE_FREE(target_buffer);
1020 emstorage_free_account(&account_list, select_num, &local_error_code_2);
1023 *error_code = local_error_code;
1025 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1029 INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code)
1031 EM_DEBUG_FUNC_BEGIN("file_path [%s], error_code [%p]", file_path, error_code);
1032 int local_error_code = EMAIL_ERROR_NONE, ret_code = false, buffer_length = 0, read_length = 0;
1033 int account_count = 0, i = 0, account_stream_length = 0;
1034 char *temp_buffer = NULL, *account_stream = NULL, *buffer_ptr = NULL;
1035 email_account_t temp_account = {0};
1036 email_account_t *account_list = NULL;
1038 ssm_file_info_t sfi;
1041 local_error_code = EMAIL_ERROR_INVALID_PARAM;
1042 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1046 if (emcore_get_account_reference_list(&account_list, &account_count, &ret_code)) {
1047 for (i = 0; i < account_count; i++) {
1048 if (account_list[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1049 if (!emcore_delete_account(account_list[i].account_id, &ret_code)) {
1050 local_error_code = EMAIL_ERROR_INVALID_ACCOUNT;
1051 EM_DEBUG_EXCEPTION("emcore_delete_account failed");
1057 EM_SAFE_FREE(account_list);
1060 if (ssm_getinfo(file_path, &sfi, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
1061 EM_DEBUG_EXCEPTION("ssm_getinfo() failed.");
1062 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1066 buffer_length = sfi.originSize;
1067 EM_DEBUG_LOG("account buffer_length[%d]", buffer_length);
1068 if ((temp_buffer = (char *)em_malloc(buffer_length + 1)) == NULL) {
1069 EM_DEBUG_EXCEPTION("em_malloc failed...");
1070 ret_code = EMAIL_ERROR_OUT_OF_MEMORY;
1074 if (ssm_read(file_path, temp_buffer, buffer_length, (size_t *)&read_length, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
1075 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1076 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1080 EM_DEBUG_LOG("read_length[%d]", read_length);
1082 if (buffer_length == read_length) {
1083 memcpy((void *)&account_count, temp_buffer, sizeof(int));
1084 buffer_ptr = temp_buffer + sizeof(int);
1086 EM_DEBUG_LOG("account_count[%d]", account_count);
1088 for (i = 0; i < account_count; i++) {
1089 memcpy((void *)&account_stream_length, buffer_ptr, sizeof(int));
1090 buffer_ptr += sizeof(int);
1091 EM_DEBUG_LOG("account_stream_length [%d]", account_stream_length);
1092 if (account_stream_length) {
1093 account_stream = em_malloc(account_stream_length);
1094 if (!account_stream) {
1095 EM_DEBUG_EXCEPTION("em_malloc() failed.");
1096 ret_code = EMAIL_ERROR_OUT_OF_MEMORY ;
1099 memcpy(account_stream, buffer_ptr, account_stream_length);
1101 em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
1102 EM_SAFE_FREE(account_stream);
1104 if (!emcore_create_account(&temp_account, &ret_code)) {
1105 EM_DEBUG_EXCEPTION("emcore_create_account() failed.");
1109 emcore_free_account(&temp_account);
1111 buffer_ptr += account_stream_length;
1112 account_stream_length = 0;
1115 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1116 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1121 emcore_free_account(&temp_account);
1122 EM_SAFE_FREE(account_stream);
1123 EM_SAFE_FREE(temp_buffer);
1125 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1129 #endif /* __FEATURE_BACKUP_ACCOUNT_ */
1133 INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
1135 EM_DEBUG_FUNC_BEGIN("domain_name [%s], result_server_info [%p]", domain_name, result_server_info);
1136 int ret_code = EMAIL_ERROR_NONE;
1138 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1143 INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info)
1145 EM_DEBUG_FUNC_BEGIN("result_server_info [%p]", target_server_info);
1146 int i, ret_code = EMAIL_ERROR_NONE;
1147 email_server_info_t *server_info = NULL;
1149 if(target_server_info && *target_server_info) {
1150 server_info = *target_server_info;
1151 EM_SAFE_FREE(server_info->service_name);
1152 for(i = 0; i < server_info->protocol_conf_count; i++) {
1153 EM_SAFE_FREE(server_info->protocol_config_array[i].server_addr);
1155 EM_SAFE_FREE(server_info->protocol_config_array);
1156 EM_SAFE_FREE(server_info);
1158 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1162 INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
1164 EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
1165 int ret_code = EMAIL_ERROR_NONE, result_value = 0;
1167 result_value = vconf_set_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, input_account_id);
1168 if (result_value < 0) {
1169 EM_DEBUG_EXCEPTION("vconf_set_int failed [%d]", result_value);
1170 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1173 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1177 static int _recover_from_invalid_default_account_id(int *output_account_id)
1179 EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1180 int ret_code = EMAIL_ERROR_NONE;
1181 int account_count = 100;
1182 emstorage_account_tbl_t *result_account_list = NULL;
1184 if (output_account_id == NULL) {
1185 ret_code = EMAIL_ERROR_INVALID_PARAM;
1189 if(!emstorage_get_account_list(&account_count, &result_account_list, false, false, &ret_code) || !result_account_list) {
1190 EM_DEBUG_EXCEPTION("emstorage_get_account_list() failed [%d]", ret_code);
1191 *output_account_id = 0;
1195 if (account_count > 0) {
1196 *output_account_id = result_account_list[0].account_id;
1199 EM_DEBUG_LOG("output_account_id [%d]", *output_account_id);
1203 if (result_account_list)
1204 emstorage_free_account(&result_account_list, account_count, NULL);
1206 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1210 INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
1212 EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1213 int ret_code = EMAIL_ERROR_NONE;
1214 int result_value = 0;
1215 emstorage_account_tbl_t *result_account = NULL;
1217 if (output_account_id == NULL) {
1218 ret_code = EMAIL_ERROR_INVALID_PARAM;
1222 result_value = vconf_get_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, output_account_id);
1224 if (result_value < 0) {
1225 EM_DEBUG_EXCEPTION("vconf_get_int() failed [%d]", result_value);
1226 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1227 *output_account_id = 0;
1230 if (*output_account_id != 0) {
1231 if (!emstorage_get_account_by_id(*output_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &result_account, false, &ret_code)) {
1232 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id() failed [%d]", ret_code);
1233 if(ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
1234 *output_account_id = 0;
1240 if (*output_account_id == 0) {
1241 if ( (ret_code = _recover_from_invalid_default_account_id(output_account_id)) != EMAIL_ERROR_NONE) {
1242 EM_DEBUG_EXCEPTION("_recover_from_invalid_default_account() failed [%d]", ret_code);
1243 *output_account_id = 0;
1249 emstorage_free_account(&result_account, 1, NULL);
1251 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1255 INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
1257 EM_DEBUG_FUNC_BEGIN();
1258 int err = EMAIL_ERROR_NONE;
1260 int account_count = 50;
1261 emstorage_account_tbl_t *temp_account_tbl_list = NULL;
1262 emstorage_account_tbl_t *temp_account_tbl = NULL;
1264 if (!emstorage_get_account_list(&account_count, &temp_account_tbl_list, true, false, &err)) {
1265 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1269 for (i = 0; i < account_count; i++) {
1270 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)) {
1271 if(err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
1272 if(!emcore_delete_account(temp_account_tbl_list[i].account_id, &err)) {
1273 EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
1282 emstorage_free_account(&temp_account_tbl_list, account_count, NULL);
1283 emstorage_free_account(&temp_account_tbl, 1, NULL);
1285 EM_DEBUG_FUNC_END("err [%d]", err);
1289 INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, email_set_type_t input_set_operator, int input_sync_status)
1291 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);
1292 int err = EMAIL_ERROR_NONE;
1294 #ifdef __FEATURE_USING_ACCOUNT_SVC__
1295 int err_from_account_svc = 0;
1296 emstorage_account_tbl_t *account_tbl_data = NULL;
1297 if (input_account_id != ALL_ACCOUNT && (input_sync_status == SYNC_STATUS_SYNCING)) {
1298 if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_data, true, &err)) {
1299 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1303 err_from_account_svc = account_connect();
1304 EM_DEBUG_LOG("account_connect returns [%d]", err_from_account_svc);
1306 EM_DEBUG_LOG("account_tbl_data->account_svc_id [%d]", account_tbl_data->account_svc_id);
1308 if (input_set_operator == SET_TYPE_SET)
1309 err_from_account_svc = account_update_sync_status_by_id(account_tbl_data->account_svc_id, ACCOUNT_SYNC_STATUS_RUNNING);
1310 else if(input_set_operator == SET_TYPE_MINUS)
1311 err_from_account_svc = account_update_sync_status_by_id(account_tbl_data->account_svc_id, ACCOUNT_SYNC_STATUS_IDLE);
1313 EM_DEBUG_LOG("account_update_sync_status_by_id returns [%d]", err_from_account_svc);
1315 err_from_account_svc = account_disconnect();
1316 EM_DEBUG_LOG("account_disconnect returns [%d]", err_from_account_svc);
1318 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
1320 if (!emstorage_update_sync_status_of_account(input_account_id, input_set_operator, input_sync_status, true, &err))
1321 EM_DEBUG_EXCEPTION("emstorage_update_sync_status_of_account failed [%d]", err);
1323 #ifdef __FEATURE_USING_ACCOUNT_SVC__
1325 if (account_tbl_data)
1326 emstorage_free_account(&account_tbl_data, 1, NULL);
1327 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
1329 EM_DEBUG_FUNC_END("err [%d]", err);