4 * Copyright (c) 2000 - 2011 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 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
58 char *g_default_mbox_alias[MAILBOX_COUNT] =
60 EMAIL_INBOX_DISPLAY_NAME,
61 EMAIL_DRAFTBOX_DISPLAY_NAME,
62 EMAIL_OUTBOX_DISPLAY_NAME,
63 EMAIL_SENTBOX_DISPLAY_NAME,
64 EMAIL_TRASH_DISPLAY_NAME,
65 EMAIL_SPAMBOX_DISPLAY_NAME,
68 char *g_default_mbox_name[MAILBOX_COUNT] =
74 EMAIL_TRASH_DISPLAY_NAME,
78 email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] =
80 EMAIL_MAILBOX_TYPE_INBOX,
81 EMAIL_MAILBOX_TYPE_DRAFT,
82 EMAIL_MAILBOX_TYPE_OUTBOX,
83 EMAIL_MAILBOX_TYPE_SENTBOX,
84 EMAIL_MAILBOX_TYPE_TRASH,
85 EMAIL_MAILBOX_TYPE_SPAMBOX,
88 INTERNAL_FUNC email_account_t* emcore_get_account_reference(int account_id)
90 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
91 EM_PROFILE_BEGIN(profile_emcore_get_account_reference);
92 email_account_list_t **p;
94 if (account_id == NEW_ACCOUNT_ID)
95 return emcore_get_new_account_reference();
100 if ((*p)->account->account_id == account_id)
101 return ((*p)->account);
105 /* refresh and check once agai */
106 if (emcore_refresh_account_reference() == true) {
109 if ((*p)->account->account_id == account_id)
110 return ((*p)->account);
117 EM_PROFILE_END(profile_emcore_get_account_reference);
123 INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, int *err_code)
125 EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p], incoming_server_address [%s]", account, err_code, account->incoming_server_address);
128 int err = EMAIL_ERROR_NONE;
129 email_session_t *session = NULL;
130 SENDSTREAM *stream = NULL;
131 MAILSTREAM *tmp_stream = NULL;
133 if (!emcore_check_thread_status()) {
134 err = EMAIL_ERROR_CANCELLED;
138 if (!emnetwork_check_network_status(&err)) {
139 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
142 EM_DEBUG_LOG("Network available");
144 if (!emcore_check_thread_status()) {
145 err = EMAIL_ERROR_CANCELLED;
149 if (!emcore_get_empty_session(&session)) {
150 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
151 err = EMAIL_ERROR_SESSION_NOT_FOUND;
155 #ifdef _SMTP_ACCOUNT_VALIDATION_
156 /* validate connection for smt */
157 EM_DEBUG_LOG("Validate connection for SMTP");
159 if (!emcore_check_thread_status()) {
160 err = EMAIL_ERROR_CANCELLED;
163 if (!emcore_connect_to_remote_mailbox_with_account_info(account, (char *)ENCODED_PATH_SMTP, (void **)&stream, &err) || !stream) {
164 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed 1 - %d", err);
165 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
166 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication fail 1- %d", err);
170 if (account->outgoing_server_secure_connection == 0x01) /* 0x01 == ss */ {
172 EM_DEBUG_LOG("Retry with TLS");
173 account->outgoing_server_secure_connection = 0x02; /* 0x02 == tl */
174 if (!emcore_check_thread_status()) {
175 err = EMAIL_ERROR_CANCELLED;
179 if (!emcore_connect_to_remote_mailbox_with_account_info(account, (char *)ENCODED_PATH_SMTP, (void **)&stream, &err) || !stream) {
180 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed 2 - %d", err);
181 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
182 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication fail 2 - %d", err);
184 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
185 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
187 account->outgoing_server_secure_connection = 0x01; /* restore to the previous value */
191 if (!emcore_check_thread_status()) {
192 err = EMAIL_ERROR_CANCELLED;
196 /* save outgoing_server_secure_connection = 0x02 (tls) to the d */
197 if (!emstorage_update_account(account_id, (emstorage_account_tbl_t *)account, true, &err)) {
198 EM_DEBUG_EXCEPTION("emstorage_update_account failed - %d", err);
199 account->outgoing_server_secure_connection = 0x01; /* restore to the previous value */
200 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
205 if (EMAIL_ERROR_CONNECTION_FAILURE != err)
206 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
212 /* validate connection for pop3/ima */
213 EM_DEBUG_LOG("Validate connection for POP3/IMAP4");
214 if (EMAIL_ERROR_NONE == err) {
215 if (!emcore_check_thread_status()) {
216 err = EMAIL_ERROR_CANCELLED;
220 if (!emcore_connect_to_remote_mailbox_with_account_info(account, 0, (void **)&tmp_stream, &err) || !tmp_stream)
222 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
223 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) { /* wrong password or etc */
224 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
226 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
227 /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
233 if (!emcore_check_thread_status()) {
234 if (!emcore_delete_account(account->account_id, NULL))
235 EM_DEBUG_EXCEPTION("emdaemon_delete_account failed [%d]", account->account_id);
236 err = EMAIL_ERROR_CANCELLED;
247 emcore_close_mailbox(0 , tmp_stream);
249 if (err_code != NULL)
252 emcore_clear_session(session);
259 INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
261 EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
263 int err = EMAIL_ERROR_NONE, ret = false;
264 email_account_t *ref_account = NULL;
269 EM_DEBUG_EXCEPTION("account_id[%p]", account_id);
270 err = EMAIL_ERROR_INVALID_PARAM;
274 ref_account = emcore_get_account_reference(account_id);
276 if (ref_account && emcore_validate_account_with_account_info(ref_account, &err) == false) {
277 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed (%d)", err);
293 INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
295 EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
297 /* default variabl */
299 int err = EMAIL_ERROR_NONE;
300 char vconf_private_id[MAX_PATH] = {0,};
302 if (account_id < FIRST_ACCOUNT_ID) {
303 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
304 err = EMAIL_ERROR_INVALID_PARAM;
308 #ifdef __FEATURE_LOCAL_ACTIVITY__
309 /* Delete all local activities of previous account */
312 emstorage_activity_tbl_t activity;
313 memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
314 activity.account_id = account_id;
316 if (!emcore_delete_activity(&activity, &err)) {
317 EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
323 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
324 if (false == emcore_clear_partial_body_thd_event_que(&err))
325 EM_DEBUG_EXCEPTION(" emcore_clear_partial_body_thd_event_que [%d]", err);
327 if (false == emstorage_delete_full_pbd_activity_data(account_id, true, &err))
328 EM_DEBUG_EXCEPTION("emstorage_delete_full_pbd_activity_data failed [%d]", err);
332 #ifdef __FEATURE_USING_ACCOUNT_SVC__
335 email_account_t *account_to_be_deleted;
337 account_to_be_deleted = emcore_get_account_reference(account_id);
338 if (account_to_be_deleted && account_to_be_deleted->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
339 EM_DEBUG_LOG("Calling account_svc_delete with account_svc_id[%d]", account_to_be_deleted->account_svc_id);
340 error_code = account_connect();
341 EM_DEBUG_LOG("account_connect returns [%d]", error_code);
342 error_code = account_delete_from_db_by_id(account_to_be_deleted->account_svc_id);
343 EM_DEBUG_LOG("account_delete_from_db_by_id returns [%d]", error_code);
344 error_code = account_disconnect();
345 EM_DEBUG_LOG("account_disconnect returns [%d]", error_code);
349 if (emcore_cancel_all_threads_of_an_account(account_id) < EMAIL_ERROR_NONE) {
350 EM_DEBUG_EXCEPTION("There are some remaining jobs. I couldn't stop them.");
351 err = EMAIL_ERROR_CANNOT_STOP_THREAD;
355 /* BEGIN TRANSACTION; */
356 emstorage_begin_transaction(NULL, NULL, NULL);
358 if (!emstorage_delete_account(account_id, false, &err)) {
359 EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
363 #ifdef __FEATURE_KEEP_CONNECTION__
364 /* emcore_reset_streams(); */
365 emcore_remove_connection_info(account_id);
368 if ((err = emcore_delete_all_mails_of_acount(account_id)) != EMAIL_ERROR_NONE) {
369 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_acount failed [%d]", err);
373 /* delete all mailboxes */
374 if (!emstorage_delete_mailbox(account_id, -1, 0, false, &err)) {
375 EM_DEBUG_EXCEPTION("emstorage_delete_mailbox failed - %d", err);
379 /* delete local imap sync mailbox from imap mailbox tabl */
380 if (!emstorage_remove_downloaded_mail(account_id, NULL, NULL, false, &err)) {
381 EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
385 emcore_display_unread_in_badge();
386 emcore_delete_notification_by_account(account_id);
387 emcore_refresh_account_reference();
389 /* Delete contact log */
390 if ( ((err = emcore_delete_contacts_log(account_id)) != EMAIL_ERROR_NONE) && (err != EMAIL_ERROR_DATA_NOT_FOUND) ) {
391 EM_DEBUG_EXCEPTION("emcore_delete_contacts_log failed : [%d]", err);
395 /* Delete Noti private ID */
396 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
397 if (vconf_unset_recursive(vconf_private_id) != 0)
398 EM_DEBUG_EXCEPTION("vconf_unset_recursive failed");
403 if (ret == true) { /* COMMIT TRANSACTION; */
404 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
405 err = EMAIL_ERROR_DB_FAILURE;
408 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE, account_id, 0, NULL, 0))
409 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
412 else { /* ROLLBACK TRANSACTION; */
413 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
414 err = EMAIL_ERROR_DB_FAILURE;
415 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE_FAIL, account_id, err, NULL, 0))
416 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
427 INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
429 EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p]", account, err_code);
432 int err = EMAIL_ERROR_NONE;
433 int i, count = 0, is_preset_IMAP_account = false;
435 char vconf_private_id[MAX_PATH] = {0, };
436 email_mailbox_t local_mailbox = {0};
437 emstorage_account_tbl_t *temp_account_tbl = NULL;
439 if (account == NULL) {
440 EM_DEBUG_EXCEPTION("account[%p]", account);
441 err = EMAIL_ERROR_INVALID_PARAM;
445 if (!emstorage_get_account_count(&count, true, &err)) {
446 EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
451 if (count >= EMAIL_ACCOUNT_MAX) {
452 EM_DEBUG_EXCEPTION("too many accounts...");
453 err = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
457 account->account_id = 0;
459 /* Temporarily code - begin */
460 if (account->auto_download_size == 0) {
461 account->auto_download_size = PARTIAL_BODY_SIZE_IN_BYTES;
462 EM_DEBUG_LOG("account->auto_download_size [%d]", account->auto_download_size);
465 if (account->default_mail_slot_size == 0) {
466 account->default_mail_slot_size = 50;
467 EM_DEBUG_LOG("account->default_mail_slot_size [%d]", account->default_mail_slot_size);
469 /* Temporarily code - end */
471 /* check for email address validation */
472 EM_DEBUG_LOG("account->user_email_address[%s]", account->user_email_address);
473 if (account->user_email_address) {
474 if (!em_verify_email_address(account->user_email_address, true, &err)) {
475 err = EMAIL_ERROR_INVALID_ADDRESS;
476 EM_DEBUG_EXCEPTION("Invalid Email Address");
481 temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
482 if (!temp_account_tbl) {
483 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
486 em_convert_account_to_account_tbl(account, temp_account_tbl);
488 if (!emstorage_add_account(temp_account_tbl, true, &err)) {
489 EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
492 account->account_id = temp_account_tbl->account_id;
493 is_preset_IMAP_account = ((account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)) ? true : false;/* && (account->is_preset_account)) ? true : false */
495 EM_DEBUG_LOG("is_preset_IMAP_account : %d", is_preset_IMAP_account);
497 if ((account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && (!is_preset_IMAP_account)) {
498 /* 1. create default local mailbox
499 * (Inbox, Draft, Outbox, Sentbox) */
500 for (i = 0; i < MAILBOX_COUNT; i++) {
501 EM_DEBUG_LOG("g_default_mbox_name [%d/%d] is [%s]", i, MAILBOX_COUNT, g_default_mbox_name[i]);
502 local_mailbox.account_id = temp_account_tbl->account_id;
503 local_mailbox.mailbox_name = g_default_mbox_name[i];
504 local_mailbox.mailbox_type = g_default_mbox_type[i];
505 if (local_mailbox.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) {
506 local_mailbox.local = EMAIL_MAILBOX_FROM_SERVER;
509 local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
511 local_mailbox.alias = g_default_mbox_alias[i];
512 emcore_get_default_mail_slot_count(&local_mailbox.mail_slot_size, NULL);
514 if (!emcore_create_mailbox(&local_mailbox, 0, &err)) {
515 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
522 /* Initialize the noti private id */
523 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account->account_id);
524 if (vconf_get_int(vconf_private_id, &private_id) != 0) {
525 EM_DEBUG_EXCEPTION("vconf_get_int failed");
527 if (vconf_set_int(vconf_private_id, 0) != 0) {
528 EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
535 if (temp_account_tbl)
536 emstorage_free_account(&temp_account_tbl, 1, NULL);
538 if (ret == false && account != NULL) {
539 if (!emcore_delete_account(account->account_id, NULL))
540 EM_DEBUG_EXCEPTION("emdaemon_delete_account Failed [%d]", account->account_id);
546 EM_DEBUG_FUNC_END("Return value [%d]", ret);
551 INTERNAL_FUNC int emcore_init_account_reference()
553 EM_DEBUG_FUNC_BEGIN();
555 int err = EMAIL_ERROR_NONE;
557 email_account_list_t *account_list = NULL;
558 email_account_list_t **p = NULL;
559 email_account_t *account = NULL;
560 emstorage_account_tbl_t *account_tbl_array = NULL;
564 if (!g_account_retrieved) {
566 if (!emstorage_get_account_list(&count, &account_tbl_array, true, true, &err)) {
567 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
571 for (p = &account_list, i = 0; i < count; i++) {
572 account = em_malloc(sizeof(email_account_t));
574 EM_DEBUG_EXCEPTION("malloc failed...");
575 err = EMAIL_ERROR_OUT_OF_MEMORY;
579 em_convert_account_tbl_to_account(account_tbl_array + i, account);
581 /* memcpy(account, accounts + i, sizeof(email_account_t)) */
582 /* memset(accounts + i, 0x00, sizeof(email_account_t)) */
584 *p = (email_account_list_t*) em_malloc(sizeof(email_account_list_t));
586 EM_DEBUG_EXCEPTION("malloc failed...");
587 err = EMAIL_ERROR_OUT_OF_MEMORY;
592 (*p)->account = account;
597 emcore_free_account_reference();
598 g_account_retrieved = 1;
599 g_account_num = count;
600 g_account_list = account_list;
604 if (account_tbl_array)
605 emstorage_free_account(&account_tbl_array, count, NULL);
607 if (err != EMAIL_ERROR_NONE) {
608 g_account_list = account_list;
609 emcore_free_account_reference();
612 EM_DEBUG_FUNC_END("err [%d]", err);
616 INTERNAL_FUNC int emcore_refresh_account_reference()
618 EM_DEBUG_FUNC_BEGIN();
620 if (g_account_retrieved && g_account_num)
621 emcore_free_account_reference();
623 g_account_retrieved = 0;
625 g_account_list = NULL;
627 if (emcore_init_account_reference() != EMAIL_ERROR_NONE) {
628 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed...");
635 INTERNAL_FUNC int emcore_free_account_reference()
637 EM_DEBUG_FUNC_BEGIN();
639 email_account_list_t *p = g_account_list;
640 email_account_list_t *p_next = NULL;
642 emcore_free_account(p->account);
643 EM_SAFE_FREE(p->account);
650 g_account_retrieved = 0;
652 g_account_list = NULL;
657 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
659 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
662 int err = EMAIL_ERROR_NONE;
664 if (count <= 0 || !account_list || !*account_list) {
665 err = EMAIL_ERROR_INVALID_PARAM;
669 email_account_t *p = *account_list;
671 for (i = 0; i < count; i++)
672 emcore_free_account(p+i);
675 *account_list = NULL;
686 INTERNAL_FUNC void emcore_free_option(email_option_t *option)
688 EM_SAFE_FREE(option->display_name_from);
689 EM_SAFE_FREE(option->signature);
693 INTERNAL_FUNC void emcore_free_account(email_account_t *account)
697 EM_SAFE_FREE(account->account_name);
698 EM_SAFE_FREE(account->incoming_server_address);
699 EM_SAFE_FREE(account->user_email_address);
700 EM_SAFE_FREE(account->incoming_server_user_name);
701 EM_SAFE_FREE(account->incoming_server_password);
702 EM_SAFE_FREE(account->outgoing_server_address);
703 EM_SAFE_FREE(account->outgoing_server_user_name);
704 EM_SAFE_FREE(account->outgoing_server_password);
705 EM_SAFE_FREE(account->user_display_name);
706 EM_SAFE_FREE(account->reply_to_address);
707 EM_SAFE_FREE(account->return_address);
708 EM_SAFE_FREE(account->logo_icon_path);
709 EM_SAFE_FREE(account->certificate_path);
710 EM_SAFE_FREE(account->user_data);
711 account->user_data_length = 0;
712 emcore_free_option(&account->options);
719 INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code)
721 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
722 int i, countOfAccounts = 0;
724 int err = EMAIL_ERROR_NONE;
725 email_account_t *accountRef;
726 email_account_list_t *p;
728 if (!account_list || !count) {
729 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
730 err = EMAIL_ERROR_INVALID_PARAM;
741 EM_DEBUG_LOG("Result count[%d]", countOfAccounts);
742 *count = countOfAccounts;
744 if (countOfAccounts > 0) {
745 *account_list = malloc(sizeof(email_account_t) * countOfAccounts);
746 if (!*account_list) {
747 EM_DEBUG_LOG("malloc failed...");
748 err = EMAIL_ERROR_OUT_OF_MEMORY;
754 for (i = 0; i < countOfAccounts; i++) {
755 accountRef = (*account_list) + i;
756 memcpy(accountRef, p->account , sizeof(email_account_t));
760 for (i = 0; i < countOfAccounts; i++) {
761 accountRef = (*account_list) + i;
762 EM_DEBUG_LOG("Result account id[%d], name[%s]", accountRef->account_id, accountRef->account_name);
769 if (account_list) /* Warn! this is not *account_list. Just account_list */
770 EM_SAFE_FREE(*account_list);
773 if (err_code != NULL)
780 #ifdef __FEATURE_BACKUP_ACCOUNT__
781 #include <ss_manager.h>
783 static int append_data_into_buffer(char **target_buffer, int *target_buffer_lenth, char *input_data, int input_data_length, int *error_code)
785 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);
786 int local_error_code = EMAIL_ERROR_NONE, ret_code = false;
788 if (!target_buffer || !target_buffer_lenth || !input_data) {
789 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
790 local_error_code = EMAIL_ERROR_INVALID_PARAM;
794 if (*target_buffer_lenth > 0 && input_data_length) {
795 EM_DEBUG_LOG("*target_buffer_lenth [%d]", *target_buffer_lenth);
796 *target_buffer = realloc(*target_buffer, (*target_buffer_lenth) + input_data_length);
797 if (!*target_buffer) {
798 EM_DEBUG_EXCEPTION("realloc failed");
799 local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
802 memcpy(*target_buffer + (*target_buffer_lenth), input_data, input_data_length);
803 *target_buffer_lenth += input_data_length;
804 EM_DEBUG_LOG("*target_buffer_lenth [%d] input_data_length [%d]", *target_buffer_lenth, input_data_length);
807 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
808 local_error_code = EMAIL_ERROR_INVALID_PARAM;
817 *error_code = local_error_code;
818 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
824 static int emcore_write_account_into_buffer(char **target_buffer, int *target_buffer_lenth, emstorage_account_tbl_t *account_tbl_ptr, int *error_code)
826 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);
827 int local_error_code = EMAIL_ERROR_NONE, ret_code = false, stream_length = 0;
828 email_account_t temp_account = {0};
829 char *byte_stream = NULL;
831 if (em_convert_account_tbl_to_account(account_tbl_ptr, &temp_account)) {
832 byte_stream = em_convert_account_to_byte_stream(&temp_account, &stream_length);
833 EM_DEBUG_LOG("stream_length [%d]", stream_length);
834 /* EM_DEBUG_LOG("incoming_server_password [%s]", temp_account->password) */
837 if (!append_data_into_buffer(target_buffer, target_buffer_lenth, (char *)&stream_length, sizeof(int), &local_error_code)) {
838 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
841 EM_DEBUG_LOG("append_data_into_buffer succeed for stream_length");
843 if (!append_data_into_buffer(target_buffer, target_buffer_lenth, byte_stream, stream_length, &local_error_code)) {
844 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
847 EM_DEBUG_LOG("append_data_into_buffer succeed for byte_stream");
851 EM_DEBUG_EXCEPTION("em_convert_account_tbl_to_account failed");
852 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
858 emcore_free_account(&temp_account);
860 *error_code = local_error_code;
862 EM_SAFE_FREE(byte_stream);
864 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
868 INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
870 EM_DEBUG_FUNC_BEGIN("file_path [%s], error_code [%p]", file_path, error_code);
871 int local_error_code = EMAIL_ERROR_NONE, local_error_code_2 = EMAIL_ERROR_NONE, ret_code = false;
872 int select_num, i, target_buff_length = 0;
873 char *target_buffer = NULL;
874 emstorage_account_tbl_t *account_list = NULL;
877 local_error_code = EMAIL_ERROR_INVALID_PARAM;
878 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
884 if (!emstorage_get_account_list(&select_num, &account_list, true, true, &local_error_code)) {
885 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", local_error_code);
889 EM_DEBUG_LOG("select_num [%d]", select_num);
892 target_buffer = em_malloc(sizeof(int));
893 if (!target_buffer) {
894 EM_DEBUG_EXCEPTION("malloc failed");
895 local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
899 memcpy(target_buffer, (char *)&select_num, sizeof(int));
900 target_buff_length = sizeof(int);
902 for (i = 0; i < select_num; i++) {
903 if (!emcore_write_account_into_buffer(&target_buffer, &target_buff_length, account_list + i, &local_error_code)) {
904 EM_DEBUG_EXCEPTION("emcore_write_account_into_buffer failed [%d]", local_error_code);
909 EM_DEBUG_LOG("target_buff_length [%d]", target_buff_length);
911 ssm_delete_file(file_path, SSM_FLAG_SECRET_OPERATION, NULL);
913 if (ssm_write_buffer(target_buffer, target_buff_length, file_path, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
914 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d]", local_error_code);
915 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
924 EM_SAFE_FREE(target_buffer);
926 emstorage_free_account(&account_list, select_num, &local_error_code_2);
929 *error_code = local_error_code;
931 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
935 INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code)
937 EM_DEBUG_FUNC_BEGIN("file_path [%s], error_code [%p]", file_path, error_code);
938 int local_error_code = EMAIL_ERROR_NONE, ret_code = false, buffer_length = 0, read_length = 0;
939 int account_count = 0, i = 0, account_stream_length = 0;
940 char *temp_buffer = NULL, *account_stream = NULL, *buffer_ptr = NULL;
941 email_account_t temp_account = {0};
942 email_account_t *account_list = NULL;
947 local_error_code = EMAIL_ERROR_INVALID_PARAM;
948 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
952 if (emcore_get_account_reference_list(&account_list, &account_count, &ret_code)) {
953 for (i = 0; i < account_count; i++) {
954 if (account_list[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
955 if (!emcore_delete_account(account_list[i].account_id, &ret_code)) {
956 local_error_code = EMAIL_ERROR_INVALID_ACCOUNT;
957 EM_DEBUG_EXCEPTION("emcore_delete_account failed");
964 if (ssm_getinfo(file_path, &sfi, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
965 EM_DEBUG_EXCEPTION("ssm_getinfo() failed.");
966 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
970 buffer_length = sfi.originSize;
971 EM_DEBUG_LOG("account buffer_length[%d]", buffer_length);
972 if ((temp_buffer = (char *)em_malloc(buffer_length + 1)) == NULL) {
973 EM_DEBUG_EXCEPTION("em_malloc failed...");
974 ret_code = EMAIL_ERROR_OUT_OF_MEMORY;
978 if (ssm_read(file_path, temp_buffer, buffer_length, (size_t *)&read_length, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
979 EM_DEBUG_EXCEPTION("ssm_read() failed.");
980 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
984 EM_DEBUG_LOG("read_length[%d]", read_length);
986 if (buffer_length == read_length) {
987 memcpy((void *)&account_count, temp_buffer, sizeof(int));
988 buffer_ptr = temp_buffer + sizeof(int);
990 EM_DEBUG_LOG("account_count[%d]", account_count);
992 for (i = 0; i < account_count; i++) {
993 memcpy((void *)&account_stream_length, buffer_ptr, sizeof(int));
994 buffer_ptr += sizeof(int);
995 EM_DEBUG_LOG("account_stream_length [%d]", account_stream_length);
996 if (account_stream_length) {
997 account_stream = em_malloc(account_stream_length);
998 if (!account_stream) {
999 EM_DEBUG_EXCEPTION("em_malloc() failed.");
1000 ret_code = EMAIL_ERROR_OUT_OF_MEMORY ;
1003 memcpy(account_stream, buffer_ptr, account_stream_length);
1005 em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
1006 EM_SAFE_FREE(account_stream);
1008 if (!emcore_create_account(&temp_account, &ret_code)) {
1009 EM_DEBUG_EXCEPTION("emcore_create_account() failed.");
1013 emcore_free_account(&temp_account);
1015 buffer_ptr += account_stream_length;
1016 account_stream_length = 0;
1019 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1020 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1025 emcore_free_account(&temp_account);
1026 EM_SAFE_FREE(account_stream);
1027 EM_SAFE_FREE(temp_buffer);
1029 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1033 #endif /* __FEATURE_BACKUP_ACCOUNT_ */
1035 INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
1037 EM_DEBUG_FUNC_BEGIN("domain_name [%s], result_server_info [%p]", domain_name, result_server_info);
1038 int ret_code = EMAIL_ERROR_NONE;
1039 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1044 INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info)
1046 EM_DEBUG_FUNC_BEGIN("result_server_info [%p]", target_server_info);
1047 int i, ret_code = EMAIL_ERROR_NONE;
1048 email_server_info_t *server_info = NULL;
1050 if(target_server_info && *target_server_info) {
1051 server_info = *target_server_info;
1052 EM_SAFE_FREE(server_info->service_name);
1053 for(i = 0; i < server_info->protocol_conf_count; i++) {
1054 EM_SAFE_FREE(server_info->protocol_config_array[i].server_addr);
1056 EM_SAFE_FREE(server_info->protocol_config_array);
1057 EM_SAFE_FREE(server_info);
1059 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1063 INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
1065 EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
1066 int ret_code = EMAIL_ERROR_NONE, result_value = 0;
1068 result_value = vconf_set_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, input_account_id);
1069 if (result_value < 0) {
1070 EM_DEBUG_EXCEPTION("vconf_set_int failed [%d]", result_value);
1071 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1074 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1078 static int _recover_from_invalid_default_account_id(int *output_account_id)
1080 EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1081 int ret_code = EMAIL_ERROR_NONE;
1082 int account_count = 100;
1083 emstorage_account_tbl_t *result_account_list = NULL;
1085 if (output_account_id == NULL) {
1086 ret_code = EMAIL_ERROR_INVALID_PARAM;
1090 if(!emstorage_get_account_list(&account_count, &result_account_list, false, false, &ret_code) || !result_account_list) {
1091 EM_DEBUG_EXCEPTION("emstorage_get_account_list() failed [%d]", ret_code);
1092 *output_account_id = 0;
1096 if (account_count > 0) {
1097 *output_account_id = result_account_list[0].account_id;
1100 EM_DEBUG_LOG("output_account_id [%d]", *output_account_id);
1104 if (result_account_list)
1105 emstorage_free_account(&result_account_list, account_count, NULL);
1107 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1111 INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
1113 EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1114 int ret_code = EMAIL_ERROR_NONE;
1115 int result_value = 0;
1116 emstorage_account_tbl_t *result_account = NULL;
1118 if (output_account_id == NULL) {
1119 ret_code = EMAIL_ERROR_INVALID_PARAM;
1123 result_value = vconf_get_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, output_account_id);
1125 if (result_value < 0) {
1126 EM_DEBUG_EXCEPTION("vconf_get_int() failed [%d]", result_value);
1127 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1128 *output_account_id = 0;
1131 if (*output_account_id != 0) {
1132 if (!emstorage_get_account_by_id(*output_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &result_account, false, &ret_code)) {
1133 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id() failed [%d]", ret_code);
1134 if(ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
1135 *output_account_id = 0;
1141 if (*output_account_id == 0) {
1142 if ( (ret_code = _recover_from_invalid_default_account_id(output_account_id)) != EMAIL_ERROR_NONE) {
1143 EM_DEBUG_EXCEPTION("_recover_from_invalid_default_account() failed [%d]", ret_code);
1144 *output_account_id = 0;
1150 emstorage_free_account(&result_account, 1, NULL);
1152 EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1156 INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
1158 EM_DEBUG_FUNC_BEGIN();
1159 int err = EMAIL_ERROR_NONE;
1161 int account_count = 50;
1162 emstorage_account_tbl_t *temp_account_tbl_list = NULL;
1163 emstorage_account_tbl_t *temp_account_tbl = NULL;
1165 if (!emstorage_get_account_list(&account_count, &temp_account_tbl_list, true, false, &err)) {
1166 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1170 for (i = 0; i < account_count; i++) {
1171 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)) {
1172 if(err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
1173 if(!emcore_delete_account(temp_account_tbl_list[i].account_id, &err)) {
1174 EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
1183 emstorage_free_account(&temp_account_tbl_list, account_count, NULL);
1184 emstorage_free_account(&temp_account_tbl, 1, NULL);
1186 EM_DEBUG_FUNC_END("err [%d]", err);
1190 INTERNAL_FUNC int emcore_update_sync_status_of_account(int input_account_id, email_set_type_t input_set_operator, int input_sync_status)
1192 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);
1193 int err = EMAIL_ERROR_NONE;
1194 int err_from_account_svc = 0;
1195 emstorage_account_tbl_t *account_tbl_data = NULL;
1197 if (!emstorage_update_sync_status_of_account(input_account_id, input_set_operator, input_sync_status, true, &err))
1198 EM_DEBUG_EXCEPTION("emstorage_update_sync_status_of_account failed [%d]", err);
1202 if (account_tbl_data)
1203 emstorage_free_account(&account_tbl_data, 1, NULL);
1205 EM_DEBUG_FUNC_END("err [%d]", err);