Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-account.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23 /******************************************************************************
24  * File :  email-core-account.c
25  * Desc :  Account Management
26  *
27  * Auth :  Kyuho Jo
28  *
29  * History :
30  *    2010.08.25  :  created
31  *****************************************************************************/
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <vconf.h>
38
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"
53
54 #ifdef __FEATURE_USING_ACCOUNT_SVC__
55 #include "account.h"
56 #include "account-types.h"
57 #endif /*  __FEATURE_USING_ACCOUNT_SVC__ */
58
59 char *g_default_mbox_alias[MAILBOX_COUNT] =
60 {
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,
67 };
68
69 char *g_default_mbox_name[MAILBOX_COUNT]  =
70 {
71         EMAIL_INBOX_NAME,
72         EMAIL_DRAFTBOX_NAME,
73         EMAIL_OUTBOX_NAME,
74         EMAIL_SENTBOX_NAME,
75         EMAIL_TRASH_DISPLAY_NAME,
76         EMAIL_SPAMBOX_NAME,
77 };
78
79 email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] =
80 {
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,
87 };
88
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;
92
93 INTERNAL_FUNC email_account_t* emcore_get_account_reference(int account_id)
94 {
95         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
96         email_account_list_t **p;
97         email_account_t *result_account = NULL;
98
99         if (account_id < 0){
100                 emcore_get_account_from_unvalidated_account_list(account_id, &result_account);
101                 return result_account;
102         }
103         else if (account_id > 0)  {
104                 ENTER_CRITICAL_SECTION(_account_ref_lock);
105                 p = &g_account_list;
106                 while (*p)  {
107                         if ((*p)->account->account_id == account_id) {
108                                 emcore_duplicate_account((*p)->account, &result_account, NULL);
109                                 break;
110                         }
111                         p = &(*p)->next;
112                 }
113                 LEAVE_CRITICAL_SECTION(_account_ref_lock);
114
115                 if (result_account)
116                         goto FINISH_OFF;
117
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) {
122                         p = &g_account_list;
123                         while (*p)  {
124                                 if ((*p)->account->account_id == account_id) {
125                                         emcore_duplicate_account((*p)->account, &result_account, NULL);
126                                         break;
127                                 }
128                                 p = &(*p)->next;
129                         }
130                 }
131                 LEAVE_CRITICAL_SECTION(_account_ref_lock);
132         }
133
134 FINISH_OFF:
135         EM_DEBUG_FUNC_END("[%p]", result_account);
136         return result_account;
137 }
138
139
140 INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, int *err_code)
141 {
142         EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p], incoming_server_address [%s]", account, err_code, account->incoming_server_address);
143
144         int ret = false;
145         int err = EMAIL_ERROR_NONE;
146         email_session_t *session = NULL;
147         SENDSTREAM *stream = NULL;
148         MAILSTREAM *tmp_stream = NULL;
149
150         if (!emcore_check_thread_status())  {
151                 err = EMAIL_ERROR_CANCELLED;
152                 goto FINISH_OFF;
153         }
154
155         if (!emnetwork_check_network_status(&err))  {
156                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
157                 goto FINISH_OFF;
158         }
159         EM_DEBUG_LOG("Network available");
160
161         if (!emcore_check_thread_status())  {
162                 err = EMAIL_ERROR_CANCELLED;
163                 goto FINISH_OFF;
164         }
165
166         if (!emcore_get_empty_session(&session))  {
167                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
168                 err = EMAIL_ERROR_SESSION_NOT_FOUND;
169                 goto FINISH_OFF;
170         }
171
172 #ifdef _SMTP_ACCOUNT_VALIDATION_
173         /* validate connection for smt */
174         EM_DEBUG_LOG("Validate connection for SMTP");
175
176         if (!emcore_check_thread_status()) {
177                 err = EMAIL_ERROR_CANCELLED;
178                 goto FINISH_OFF;
179         }
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);
184                         goto FINISH_OFF;
185                 }
186
187                 if (account->outgoing_server_secure_connection == 0x01) /*  0x01 == ss */ {
188                         /*  retry with tl */
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;
193                                 goto FINISH_OFF;
194                         }
195
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);
200                                 }
201                                 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
202                                         err = EMAIL_ERROR_VALIDATE_ACCOUNT;
203                                 }
204                                 account->outgoing_server_secure_connection = 0x01;      /*  restore to the previous value */
205                                 goto FINISH_OFF;
206                     }
207
208                         if (!emcore_check_thread_status())  {
209                                 err = EMAIL_ERROR_CANCELLED;
210                                 goto FINISH_OFF;
211                         }
212
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;
218                                 goto FINISH_OFF;
219                         }
220                 }
221                 else {
222                         if (EMAIL_ERROR_CONNECTION_FAILURE != err)
223                                 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
224                         goto FINISH_OFF;
225                 }
226         }
227 #endif
228
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;
234                         goto FINISH_OFF;
235                 }
236
237                  if (!emcore_connect_to_remote_mailbox_with_account_info(account, 0, (void **)&tmp_stream, &err) || !tmp_stream)
238                  {
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);
242                         }
243                         else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
244                                 /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
245                         }
246                         goto FINISH_OFF;
247                 }
248         }
249
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;
254                 goto FINISH_OFF;
255         }
256
257         ret = true;
258
259 FINISH_OFF:
260         if (stream)
261                 smtp_close(stream);
262
263         if (tmp_stream)
264                 emcore_close_mailbox(0 , tmp_stream);
265
266         if (err_code != NULL)
267                 *err_code = err;
268
269         emcore_clear_session(session);
270
271         EM_DEBUG_FUNC_END();
272         return ret;
273 }
274
275
276 INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
277 {
278         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
279
280         int err = EMAIL_ERROR_NONE, ret = false;
281         email_account_t *ref_account = NULL;
282
283
284         if (account_id <= 0)
285     {
286                 EM_DEBUG_EXCEPTION("account_id[%p]", account_id);
287                 err = EMAIL_ERROR_INVALID_PARAM;
288                 goto FINISH_OFF;
289         }
290
291         ref_account = emcore_get_account_reference(account_id);
292
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);
295                 goto FINISH_OFF;
296         }
297
298         ret = true;
299
300 FINISH_OFF:
301
302         if (ref_account) {
303                 emcore_free_account(ref_account);
304                 EM_SAFE_FREE(ref_account);
305         }
306
307         if (err_code)
308                 *err_code = err;
309
310         EM_DEBUG_FUNC_END();
311
312         return ret;
313 }
314
315 INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
316 {
317         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
318
319         /*  default variabl */
320         int ret = false;
321         int err = EMAIL_ERROR_NONE;
322
323         if (account_id < FIRST_ACCOUNT_ID)  {
324                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
325                 err = EMAIL_ERROR_INVALID_PARAM;
326                 goto FINISH_OFF;
327         }
328
329 #ifdef __FEATURE_LOCAL_ACTIVITY__
330         /* Delete all local activities of previous account */
331         
332
333         emstorage_activity_tbl_t activity;
334         memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
335         activity.account_id = account_id;
336
337         if (!emcore_delete_activity(&activity, &err)) {
338                 EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
339
340                 goto FINISH_OFF;
341         }
342 #endif
343
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);
347
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);
350
351 #endif
352
353 #ifdef __FEATURE_USING_ACCOUNT_SVC__
354         {
355                 int error_code;
356                 email_account_t *account_to_be_deleted;
357
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);
367                 }
368
369                 if (account_to_be_deleted) {
370                         emcore_free_account(account_to_be_deleted);
371                         EM_SAFE_FREE(account_to_be_deleted);
372                 }
373         }
374 #endif
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;
378                 goto FINISH_OFF;
379         }
380
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);
384         }
385
386         /*  BEGIN TRANSACTION;           */
387         emstorage_begin_transaction(NULL, NULL, NULL);
388         
389         if (!emstorage_delete_account(account_id, false, &err))  {
390                 EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
391                 goto FINISH_OFF;
392         }
393
394 #ifdef __FEATURE_KEEP_CONNECTION__
395         /* emcore_reset_streams(); */
396         emcore_remove_connection_info(account_id);
397 #endif
398         
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);
401                 goto FINISH_OFF;
402         }
403
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);
407                 goto FINISH_OFF;
408         }
409
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);
413                 goto FINISH_OFF;
414         }
415
416         emcore_display_unread_in_badge();
417         emcore_delete_notification_by_account(account_id);
418         emcore_init_account_reference();
419
420         ret = true;
421
422 FINISH_OFF:
423         if (ret == true) {      /*  COMMIT TRANSACTION; */
424                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
425                         err = EMAIL_ERROR_DB_FAILURE;
426                         ret = false;
427                 }
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 >>> ");
430
431         }
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 >>> ");
437         }
438
439         if (err_code)
440                 *err_code = err;
441
442         EM_DEBUG_FUNC_END();
443
444         return ret;
445 }
446
447 INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
448 {
449         EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p]", account, err_code);
450
451         int ret = false;
452         int err = EMAIL_ERROR_NONE;
453         int i, count = 0;
454         int private_id = 0;
455         char vconf_private_id[MAX_PATH] = {0, };
456         email_mailbox_t local_mailbox = {0};
457         emstorage_account_tbl_t *temp_account_tbl = NULL;
458
459         if (account == NULL)  {
460                 EM_DEBUG_EXCEPTION("account[%p]", account);
461                 err = EMAIL_ERROR_INVALID_PARAM;
462                 goto FINISH_OFF;
463         }
464
465         if (!emstorage_get_account_count(&count, true, &err))  {
466                 EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
467                 goto FINISH_OFF;
468         }
469
470
471         if (count >= EMAIL_ACCOUNT_MAX)  {
472                 EM_DEBUG_EXCEPTION("too many accounts...");
473                 err = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
474                 goto FINISH_OFF;
475         }
476
477         account->account_id = 0;
478
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);
483         }
484
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);
488         }
489         /* Temporarily code - end */
490
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");
497                         goto FINISH_OFF;
498                 }
499         }
500
501 #ifdef __FEATURE_USING_ACCOUNT_SVC__
502         if (account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
503                 int account_svc_id = 0;
504                 int error_code;
505                 account_h account_handle = NULL;
506
507                 error_code = account_connect();
508                 if(error_code != ACCOUNT_ERROR_NONE) {
509                         EM_DEBUG_EXCEPTION("account_connect failed [%d]", error_code);
510                         err = error_code;
511                         goto FINISH_OFF;
512                 }
513
514                 error_code = account_create(&account_handle);
515                 if(error_code != ACCOUNT_ERROR_NONE) {
516                         EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
517                         err = error_code;
518                         account_disconnect();
519                         goto FINISH_OFF;
520                 }
521
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); 
533
534                 if (error_code != ACCOUNT_ERROR_NONE) {
535                         EM_DEBUG_EXCEPTION("account_insert_to_db failed [%d]", error_code);
536                         err = error_code;
537                         if (account_handle)
538                                 account_destroy(account_handle);
539
540                         account_disconnect();
541                         goto FINISH_OFF;
542                 }
543
544                 account->account_svc_id = account_svc_id;
545                         
546                 EM_DEBUG_LOG("account_insert_to_db succeed");
547
548                 if (account_handle)
549                         account_destroy(account_handle);
550                 account_disconnect();
551         }
552 #endif  /*  __FEATURE_USING_ACCOUNT_SVC__ */
553
554         temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
555         if (!temp_account_tbl) {
556                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
557                 goto FINISH_OFF;
558         }
559         em_convert_account_to_account_tbl(account, temp_account_tbl);
560
561         if (!emstorage_add_account(temp_account_tbl, true, &err))  {
562                 EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
563                 goto FINISH_OFF;
564         }
565         account->account_id = temp_account_tbl->account_id;
566
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;
577                         }
578                         else {
579                                 local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
580                         }
581                         local_mailbox.alias = g_default_mbox_alias[i];
582                         local_mailbox.mail_slot_size = temp_account_tbl->default_mail_slot_size;
583
584                         if (!emcore_create_mailbox(&local_mailbox, 0, &err))  {
585                                 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
586                                 goto FINISH_OFF;
587                         }
588                         
589                 }
590         }
591
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");
596         } else {
597                 if (vconf_set_int(vconf_private_id, 0) != 0) {
598                         EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
599                 }
600         }
601         
602         ret = true;
603         
604 FINISH_OFF: 
605         if (temp_account_tbl)
606                 emstorage_free_account(&temp_account_tbl, 1, NULL);
607         
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);
611         }
612         
613         if (err_code)
614                 *err_code = err;
615
616         EM_DEBUG_FUNC_END("Return value [%d]", ret);
617         return ret;
618 }
619
620
621 INTERNAL_FUNC int emcore_init_account_reference()
622 {
623         EM_DEBUG_FUNC_BEGIN();
624         
625         int err = EMAIL_ERROR_NONE;
626         
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;
631         int count = 0;          
632         int i = 0;
633         
634         /* free account reference if any */
635         emcore_free_account_reference();
636
637         if (!emstorage_get_account_list(&count, &account_tbl_array, true, true, &err)) {
638                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
639                 goto FINISH_OFF;
640         }
641
642         for (p = &account_list, i = 0; i < count; i++) {
643                 account = em_malloc(sizeof(email_account_t));
644                 if (!account) {
645                         EM_DEBUG_EXCEPTION("malloc failed...");
646                         err = EMAIL_ERROR_OUT_OF_MEMORY;
647                         goto FINISH_OFF;
648                 }
649                 
650                 em_convert_account_tbl_to_account(account_tbl_array + i, account);
651
652                 *p = (email_account_list_t*) em_malloc(sizeof(email_account_list_t));
653                 if (!(*p)) {
654                         EM_DEBUG_EXCEPTION("malloc failed...");
655                         emcore_free_account(account);
656                         EM_SAFE_FREE(account);
657                         err = EMAIL_ERROR_OUT_OF_MEMORY;
658                         goto FINISH_OFF;
659                 }
660
661                 (*p)->account = account;
662
663                 p = &(*p)->next;
664         }
665
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);
670
671 FINISH_OFF: 
672         if (account_tbl_array)
673                 emstorage_free_account(&account_tbl_array, count, NULL);
674         
675         if (err != EMAIL_ERROR_NONE && account_list && i > 0) {
676                 g_account_list = account_list;
677                 emcore_free_account_reference();
678         }
679
680         EM_DEBUG_FUNC_END("err [%d]", err);
681         return err;
682 }
683
684 INTERNAL_FUNC int emcore_free_account_reference()
685 {
686         EM_DEBUG_FUNC_BEGIN();
687         ENTER_CRITICAL_SECTION(_account_ref_lock);
688
689         email_account_list_t *p = g_account_list;
690         email_account_list_t *p_next = NULL;
691         while (p) {
692                 emcore_free_account(p->account);
693                 EM_SAFE_FREE(p->account);
694                 
695                 p_next = p->next;
696                 EM_SAFE_FREE(p);
697                 p = p_next;
698         }
699
700         g_account_num = 0;
701         g_account_list = NULL;
702
703         LEAVE_CRITICAL_SECTION(_account_ref_lock);
704         EM_DEBUG_FUNC_END();
705         return true;
706 }
707
708 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
709 {
710         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
711         
712         int ret = false;
713         int err = EMAIL_ERROR_NONE;
714         
715         if (count <= 0 || !account_list || !*account_list)  {
716                         err = EMAIL_ERROR_INVALID_PARAM;
717                         goto FINISH_OFF;
718         }
719                 
720         email_account_t *p = *account_list;
721         int i;
722         for (i = 0; i < count; i++)
723                 emcore_free_account(p+i);
724                 
725         EM_SAFE_FREE(p);
726         *account_list = NULL;
727         
728         ret = true;
729         
730 FINISH_OFF: 
731         if (err_code)
732                 *err_code = err;
733         EM_DEBUG_FUNC_END();
734         return ret;
735 }
736
737 INTERNAL_FUNC void emcore_free_option(email_option_t *option)
738 {
739         EM_SAFE_FREE(option->display_name_from);
740         EM_SAFE_FREE(option->signature);
741 }
742
743
744 INTERNAL_FUNC void emcore_free_account(email_account_t *account)
745 {
746         if(!account) return;
747
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);
764
765
766         EM_DEBUG_FUNC_END();
767 }
768
769
770 INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, email_account_t **account_dup, int *err_code)
771 {
772         EM_DEBUG_FUNC_BEGIN("account[%p]", account);
773         email_account_t *temp_account = NULL;
774
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; 
778                 return;
779         }
780
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;
785                 return;
786         }
787
788         memcpy(*account_dup, account , sizeof(email_account_t));
789         temp_account = *account_dup;
790
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);
807
808         if (err_code != NULL)
809                 *err_code = EMAIL_ERROR_NONE;
810
811         EM_DEBUG_FUNC_END();
812 }
813
814 INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code)
815 {
816         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
817         int i, countOfAccounts = 0;
818         int ret = false;
819         int err = EMAIL_ERROR_NONE;
820         email_account_t *accountRef;
821         email_account_list_t *p;
822
823         ENTER_CRITICAL_SECTION(_account_ref_lock);
824
825         if (!account_list || !count)  {
826                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
827                 err = EMAIL_ERROR_INVALID_PARAM;
828                 goto FINISH_OFF;
829         }
830
831         p = g_account_list;
832
833         while (p) {
834                 countOfAccounts++;
835                 p = p->next;
836         }
837
838         EM_DEBUG_LOG("Result count[%d]", countOfAccounts);
839         *count = countOfAccounts;
840
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;
846                         goto FINISH_OFF;
847                 }
848         }
849
850         p = g_account_list;
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);
855                 p = p->next;
856         }
857
858         ret = true;
859
860 FINISH_OFF:
861         if (ret == false) {
862                 if (account_list && *account_list)
863                         EM_SAFE_FREE(*account_list);
864         }
865
866         LEAVE_CRITICAL_SECTION(_account_ref_lock);
867
868         if (err_code != NULL)
869                 *err_code = err;
870         EM_DEBUG_FUNC_END();
871         return ret;
872 }
873
874 #ifdef __FEATURE_BACKUP_ACCOUNT__
875 #include <ss_manager.h>
876
877 static int append_data_into_buffer(char **target_buffer, int *target_buffer_lenth, char *input_data, int input_data_length, int *error_code)
878 {
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;
881
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;
885                 goto FINISH_OFF;
886         }
887
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;
894                         goto FINISH_OFF;
895                 }
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);
899         }
900         else {
901                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
902                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
903                 goto FINISH_OFF;
904         }
905
906         ret_code = true;
907
908 FINISH_OFF: 
909
910         if (error_code)
911                 *error_code = local_error_code;
912         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
913
914         return ret_code;
915 }
916
917
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)
919 {
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;
924
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) */
929
930                 if (byte_stream) {
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");
933                                 goto FINISH_OFF;
934                         }
935                         EM_DEBUG_LOG("append_data_into_buffer succeed for stream_length");
936
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");
939                                 goto FINISH_OFF;
940                         }
941                         EM_DEBUG_LOG("append_data_into_buffer succeed for byte_stream");
942                 }
943         }
944         else {
945                 EM_DEBUG_EXCEPTION("em_convert_account_tbl_to_account failed");
946                 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
947                 goto FINISH_OFF;
948         }
949
950         ret_code = true;
951 FINISH_OFF: 
952         emcore_free_account(&temp_account);
953         if (error_code)
954                 *error_code = local_error_code;
955
956         EM_SAFE_FREE(byte_stream);
957
958         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
959         return ret_code;
960 }
961
962 INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
963 {
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;
969
970         if (!file_path) {
971                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
972                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
973                 goto FINISH_OFF;        
974         }
975
976         select_num = 1000;
977         
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);
980                 goto FINISH_OFF;        
981         }
982         
983         EM_DEBUG_LOG("select_num [%d]", select_num);
984         
985         if (account_list) {
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;
990                         goto FINISH_OFF;
991                 }
992
993                 memcpy(target_buffer, (char *)&select_num, sizeof(int));
994                 target_buff_length = sizeof(int);
995
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);
999                                 goto FINISH_OFF;        
1000                         }
1001                 }
1002
1003                 EM_DEBUG_LOG("target_buff_length [%d]", target_buff_length);
1004
1005                 ssm_delete_file(file_path, SSM_FLAG_SECRET_OPERATION, NULL);
1006                 
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;
1010                         goto FINISH_OFF;        
1011                 }
1012                 
1013         }
1014
1015         ret_code = true;        
1016 FINISH_OFF: 
1017
1018         EM_SAFE_FREE(target_buffer);
1019         if (account_list)
1020                 emstorage_free_account(&account_list, select_num, &local_error_code_2);
1021
1022         if (error_code)
1023                 *error_code = local_error_code;
1024
1025         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1026         return ret_code;
1027 }
1028
1029 INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code)
1030 {
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;
1037
1038         ssm_file_info_t sfi;
1039
1040         if (!file_path) {
1041                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
1042                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1043                 goto FINISH_OFF;        
1044         }
1045
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");
1052                                         goto FINISH_OFF;
1053                                 }
1054                         }
1055                 }
1056
1057                 EM_SAFE_FREE(account_list);
1058         }
1059
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;
1063                 goto FINISH_OFF;
1064         }
1065
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;
1071                 goto FINISH_OFF;
1072         }
1073
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;
1077                 goto FINISH_OFF;
1078         }
1079
1080         EM_DEBUG_LOG("read_length[%d]", read_length);
1081
1082         if (buffer_length == read_length) {
1083                 memcpy((void *)&account_count, temp_buffer, sizeof(int));
1084                 buffer_ptr = temp_buffer + sizeof(int);
1085
1086                 EM_DEBUG_LOG("account_count[%d]", account_count);               
1087
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 ;
1097                                         goto FINISH_OFF;
1098                                 }
1099                                 memcpy(account_stream, buffer_ptr, account_stream_length);
1100
1101                                 em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
1102                                 EM_SAFE_FREE(account_stream);
1103                         
1104                                 if (!emcore_create_account(&temp_account, &ret_code)) {
1105                                         EM_DEBUG_EXCEPTION("emcore_create_account() failed.");
1106                                         goto FINISH_OFF;
1107                                 }
1108
1109                                 emcore_free_account(&temp_account);
1110                         }
1111                         buffer_ptr += account_stream_length;
1112                         account_stream_length = 0;
1113                 }
1114         } else {
1115                 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1116                 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1117                 goto FINISH_OFF;
1118         }
1119         ret_code = true;        
1120 FINISH_OFF: 
1121         emcore_free_account(&temp_account);
1122         EM_SAFE_FREE(account_stream);
1123         EM_SAFE_FREE(temp_buffer);
1124
1125         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1126         return ret_code;
1127 }
1128
1129 #endif /*  __FEATURE_BACKUP_ACCOUNT_ */
1130
1131
1132
1133 INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
1134 {
1135         EM_DEBUG_FUNC_BEGIN("domain_name [%s], result_server_info [%p]", domain_name, result_server_info);
1136         int ret_code = EMAIL_ERROR_NONE;
1137
1138         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1139         return ret_code;
1140 }
1141
1142
1143 INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info)
1144 {
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;
1148
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);
1154                 }
1155                 EM_SAFE_FREE(server_info->protocol_config_array);
1156                 EM_SAFE_FREE(server_info);
1157         }
1158         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1159         return ret_code;        
1160 }
1161
1162 INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
1163 {
1164         EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
1165         int ret_code = EMAIL_ERROR_NONE, result_value = 0;
1166
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;
1171         }
1172
1173         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1174         return ret_code;        
1175 }
1176
1177 static int _recover_from_invalid_default_account_id(int *output_account_id)
1178 {
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;
1183
1184         if (output_account_id == NULL) {
1185                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1186                 goto FINISH_OFF;
1187         }
1188
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;
1192                 goto FINISH_OFF;
1193         }
1194
1195         if (account_count > 0) {
1196                 *output_account_id = result_account_list[0].account_id;
1197         }
1198
1199         EM_DEBUG_LOG("output_account_id [%d]", *output_account_id);
1200
1201 FINISH_OFF:
1202
1203         if (result_account_list)
1204                 emstorage_free_account(&result_account_list, account_count, NULL);
1205
1206         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1207         return ret_code;
1208 }
1209
1210 INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
1211 {
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;
1216         
1217         if (output_account_id == NULL) {
1218                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1219                 goto FINISH_OFF;
1220         }
1221
1222         result_value = vconf_get_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, output_account_id);
1223
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;
1228         }
1229
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;
1235                         else
1236                                 goto FINISH_OFF;
1237                 }
1238         }
1239
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;
1244                 }
1245         }
1246
1247 FINISH_OFF:
1248         if (result_account)
1249                 emstorage_free_account(&result_account, 1, NULL);
1250         
1251         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1252         return ret_code;        
1253 }
1254
1255 INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
1256 {
1257         EM_DEBUG_FUNC_BEGIN();
1258         int err = EMAIL_ERROR_NONE;
1259         int i = 0;
1260         int account_count = 50;
1261         emstorage_account_tbl_t *temp_account_tbl_list = NULL;
1262         emstorage_account_tbl_t *temp_account_tbl      = NULL;
1263
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);
1266                 goto FINISH_OFF;
1267         }
1268
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);
1274                                         goto FINISH_OFF;
1275                                 }
1276                         }
1277                 }
1278         }
1279
1280 FINISH_OFF:
1281
1282         emstorage_free_account(&temp_account_tbl_list, account_count, NULL);
1283         emstorage_free_account(&temp_account_tbl, 1, NULL);
1284
1285         EM_DEBUG_FUNC_END("err [%d]", err);
1286         return err;
1287 }
1288
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)
1290 {
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;
1293
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);
1300                         goto FINISH_OFF;
1301                 }
1302
1303                 err_from_account_svc = account_connect();
1304                 EM_DEBUG_LOG("account_connect returns [%d]", err_from_account_svc);
1305
1306                 EM_DEBUG_LOG("account_tbl_data->account_svc_id [%d]", account_tbl_data->account_svc_id);
1307
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);
1312
1313                 EM_DEBUG_LOG("account_update_sync_status_by_id returns [%d]", err_from_account_svc);
1314
1315                 err_from_account_svc = account_disconnect();
1316                 EM_DEBUG_LOG("account_disconnect returns [%d]", err_from_account_svc);
1317         }
1318 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
1319
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);
1322
1323 #ifdef __FEATURE_USING_ACCOUNT_SVC__
1324 FINISH_OFF:
1325         if (account_tbl_data)
1326                 emstorage_free_account(&account_tbl_data, 1, NULL);
1327 #endif /* __FEATURE_USING_ACCOUNT_SVC__ */
1328
1329         EM_DEBUG_FUNC_END("err [%d]", err);
1330         return err;
1331 }