Delete the sync all for account
[framework/messaging/email-service.git] / email-core / email-core-account.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 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 #endif /*  __FEATURE_USING_ACCOUNT_SVC__ */
57
58 char *g_default_mbox_alias[MAILBOX_COUNT] =
59 {
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,
66 };
67
68 char *g_default_mbox_name[MAILBOX_COUNT]  =
69 {
70         EMAIL_INBOX_NAME,
71         EMAIL_DRAFTBOX_NAME,
72         EMAIL_OUTBOX_NAME,
73         EMAIL_SENTBOX_NAME,
74         EMAIL_TRASH_DISPLAY_NAME,
75         EMAIL_SPAMBOX_NAME,
76 };
77
78 email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] =
79 {
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,
86 };
87
88 INTERNAL_FUNC email_account_t* emcore_get_account_reference(int account_id)
89 {
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;
93
94         if (account_id == NEW_ACCOUNT_ID)
95                 return emcore_get_new_account_reference();
96
97         if (account_id > 0)  {
98                 p = &g_account_list;
99                 while (*p)  {
100                         if ((*p)->account->account_id == account_id)
101                                 return ((*p)->account);
102                         p = &(*p)->next;
103                 }
104
105                 /*  refresh and check once agai */
106                 if (emcore_refresh_account_reference() == true) {
107                         p = &g_account_list;
108                         while (*p)  {
109                                 if ((*p)->account->account_id == account_id)
110                                         return ((*p)->account);
111
112                                 p = &(*p)->next;
113                         }
114                 }
115         }
116
117         EM_PROFILE_END(profile_emcore_get_account_reference);
118         EM_DEBUG_FUNC_END();
119         return NULL;
120 }
121
122
123 INTERNAL_FUNC int emcore_validate_account_with_account_info(email_account_t *account, int *err_code)
124 {
125         EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p], incoming_server_address [%s]", account, err_code, account->incoming_server_address);
126
127         int ret = false;
128         int err = EMAIL_ERROR_NONE;
129         email_session_t *session = NULL;
130         SENDSTREAM *stream = NULL;
131         MAILSTREAM *tmp_stream = NULL;
132
133         if (!emcore_check_thread_status())  {
134                 err = EMAIL_ERROR_CANCELLED;
135                 goto FINISH_OFF;
136         }
137
138         if (!emnetwork_check_network_status(&err))  {
139                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
140                 goto FINISH_OFF;
141         }
142         EM_DEBUG_LOG("Network available");
143
144         if (!emcore_check_thread_status())  {
145                 err = EMAIL_ERROR_CANCELLED;
146                 goto FINISH_OFF;
147         }
148
149         if (!emcore_get_empty_session(&session))  {
150                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
151                 err = EMAIL_ERROR_SESSION_NOT_FOUND;
152                 goto FINISH_OFF;
153         }
154
155 #ifdef _SMTP_ACCOUNT_VALIDATION_
156         /* validate connection for smt */
157         EM_DEBUG_LOG("Validate connection for SMTP");
158
159         if (!emcore_check_thread_status()) {
160                 err = EMAIL_ERROR_CANCELLED;
161                 goto FINISH_OFF;
162         }
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);
167                         goto FINISH_OFF;
168                 }
169
170                 if (account->outgoing_server_secure_connection == 0x01) /*  0x01 == ss */ {
171                         /*  retry with tl */
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;
176                                 goto FINISH_OFF;
177                         }
178
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);
183                                 }
184                                 else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
185                                         err = EMAIL_ERROR_VALIDATE_ACCOUNT;
186                                 }
187                                 account->outgoing_server_secure_connection = 0x01;      /*  restore to the previous value */
188                                 goto FINISH_OFF;
189                     }
190
191                         if (!emcore_check_thread_status())  {
192                                 err = EMAIL_ERROR_CANCELLED;
193                                 goto FINISH_OFF;
194                         }
195
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;
201                                 goto FINISH_OFF;
202                         }
203                 }
204                 else {
205                         if (EMAIL_ERROR_CONNECTION_FAILURE != err)
206                                 err = EMAIL_ERROR_VALIDATE_ACCOUNT;
207                         goto FINISH_OFF;
208                 }
209         }
210 #endif
211
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;
217                         goto FINISH_OFF;
218                 }
219
220                  if (!emcore_connect_to_remote_mailbox_with_account_info(account, 0, (void **)&tmp_stream, &err) || !tmp_stream)
221                  {
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);
225                         }
226                         else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
227                                 /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
228                         }
229                         goto FINISH_OFF;
230                 }
231         }
232
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;
237                 goto FINISH_OFF;
238         }
239
240         ret = true;
241
242 FINISH_OFF:
243         if (stream)
244                 smtp_close(stream);
245
246         if (tmp_stream)
247                 emcore_close_mailbox(0 , tmp_stream);
248
249         if (err_code != NULL)
250                 *err_code = err;
251
252         emcore_clear_session(session);
253
254         EM_DEBUG_FUNC_END();
255         return ret;
256 }
257
258
259 INTERNAL_FUNC int emcore_validate_account(int account_id, int *err_code)
260 {
261         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
262
263         int err = EMAIL_ERROR_NONE, ret = false;
264         email_account_t *ref_account = NULL;
265
266
267         if (account_id <= 0)
268     {
269                 EM_DEBUG_EXCEPTION("account_id[%p]", account_id);
270                 err = EMAIL_ERROR_INVALID_PARAM;
271                 goto FINISH_OFF;
272         }
273
274         ref_account = emcore_get_account_reference(account_id);
275
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);
278                 goto FINISH_OFF;
279         }
280
281         ret = true;
282
283 FINISH_OFF:
284
285         if (err_code)
286                 *err_code = err;
287
288         EM_DEBUG_FUNC_END();
289
290         return ret;
291 }
292
293 INTERNAL_FUNC int emcore_delete_account(int account_id, int *err_code)
294 {
295         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
296
297         /*  default variabl */
298         int ret = false;
299         int err = EMAIL_ERROR_NONE;
300         char vconf_private_id[MAX_PATH] = {0,};
301
302         if (account_id < FIRST_ACCOUNT_ID)  {
303                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
304                 err = EMAIL_ERROR_INVALID_PARAM;
305                 goto FINISH_OFF;
306         }
307
308 #ifdef __FEATURE_LOCAL_ACTIVITY__
309         /* Delete all local activities of previous account */
310         
311
312         emstorage_activity_tbl_t activity;
313         memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
314         activity.account_id = account_id;
315
316         if (!emcore_delete_activity(&activity, &err)) {
317                 EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
318
319                 goto FINISH_OFF;
320         }
321 #endif
322
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);
326
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);
329
330 #endif
331
332 #ifdef __FEATURE_USING_ACCOUNT_SVC__
333         {
334                 int error_code;
335                 email_account_t *account_to_be_deleted;
336
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);
346                 }
347         }
348 #endif
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;
352                 goto FINISH_OFF;
353         }
354
355         /*  BEGIN TRANSACTION;           */
356         emstorage_begin_transaction(NULL, NULL, NULL);
357         
358         if (!emstorage_delete_account(account_id, false, &err))  {
359                 EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
360                 goto FINISH_OFF;
361         }
362
363 #ifdef __FEATURE_KEEP_CONNECTION__
364         /* emcore_reset_streams(); */
365         emcore_remove_connection_info(account_id);
366 #endif
367         
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);
370                 goto FINISH_OFF;
371         }
372
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);
376                 goto FINISH_OFF;
377         }
378
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);
382                 goto FINISH_OFF;
383         }
384
385         emcore_display_unread_in_badge();
386         emcore_delete_notification_by_account(account_id);
387         emcore_refresh_account_reference();
388
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);
392                         goto FINISH_OFF;
393                 }
394
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");
399
400         ret = true;
401
402 FINISH_OFF:
403         if (ret == true) {      /*  COMMIT TRANSACTION; */
404                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
405                         err = EMAIL_ERROR_DB_FAILURE;
406                         ret = false;
407                 }
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 >>> ");
410
411         }
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 >>> ");
417         }
418
419         if (err_code)
420                 *err_code = err;
421
422         EM_DEBUG_FUNC_END();
423
424         return ret;
425 }
426
427 INTERNAL_FUNC int emcore_create_account(email_account_t *account, int *err_code)
428 {
429         EM_DEBUG_FUNC_BEGIN("account[%p], err_code[%p]", account, err_code);
430
431         int ret = false;
432         int err = EMAIL_ERROR_NONE;
433         int i, count = 0, is_preset_IMAP_account = false;
434         int private_id = 0;
435         char vconf_private_id[MAX_PATH] = {0, };
436         email_mailbox_t local_mailbox = {0};
437         emstorage_account_tbl_t *temp_account_tbl = NULL;
438
439         if (account == NULL)  {
440                 EM_DEBUG_EXCEPTION("account[%p]", account);
441                 err = EMAIL_ERROR_INVALID_PARAM;
442                 goto FINISH_OFF;
443         }
444
445         if (!emstorage_get_account_count(&count, true, &err))  {
446                 EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
447                 goto FINISH_OFF;
448         }
449
450
451         if (count >= EMAIL_ACCOUNT_MAX)  {
452                 EM_DEBUG_EXCEPTION("too many accounts...");
453                 err = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
454                 goto FINISH_OFF;
455         }
456
457         account->account_id = 0;
458
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);
463         }
464
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);
468         }
469         /* Temporarily code - end */
470
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");
477                         goto FINISH_OFF;
478                 }
479         }
480
481         temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
482         if (!temp_account_tbl) {
483                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
484                 goto FINISH_OFF;
485         }
486         em_convert_account_to_account_tbl(account, temp_account_tbl);
487
488         if (!emstorage_add_account(temp_account_tbl, true, &err))  {
489                 EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
490                 goto FINISH_OFF;
491         }
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 */
494
495         EM_DEBUG_LOG("is_preset_IMAP_account  :  %d", is_preset_IMAP_account);
496
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;
507                         }
508                         else {
509                                 local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
510                         }
511                         local_mailbox.alias = g_default_mbox_alias[i];
512                         emcore_get_default_mail_slot_count(&local_mailbox.mail_slot_size, NULL);
513
514                         if (!emcore_create_mailbox(&local_mailbox, 0, &err))  {
515                                 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
516                                 goto FINISH_OFF;
517                         }
518                         
519                 }
520         }
521
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");
526         } else {
527                 if (vconf_set_int(vconf_private_id, 0) != 0) {
528                         EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
529                 }
530         }
531         
532         ret = true;
533         
534 FINISH_OFF: 
535         if (temp_account_tbl)
536                 emstorage_free_account(&temp_account_tbl, 1, NULL);
537         
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);
541         }
542         
543         if (err_code)
544                 *err_code = err;
545
546         EM_DEBUG_FUNC_END("Return value [%d]", ret);
547         return ret;
548 }
549
550
551 INTERNAL_FUNC int emcore_init_account_reference()
552 {
553         EM_DEBUG_FUNC_BEGIN();
554         
555         int err = EMAIL_ERROR_NONE;
556         
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;
561         int count = 0;          
562         int i = 0;
563         
564         if (!g_account_retrieved)  {
565                 count = 1000;
566                 if (!emstorage_get_account_list(&count, &account_tbl_array, true, true, &err))  {
567                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
568                         goto FINISH_OFF;
569                 }
570                 
571                 for (p = &account_list, i = 0; i < count; i++)  {
572                         account = em_malloc(sizeof(email_account_t));
573                         if (!account)  {        
574                                 EM_DEBUG_EXCEPTION("malloc failed...");
575                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
576                                 goto FINISH_OFF;
577                         }
578                         
579                         em_convert_account_tbl_to_account(account_tbl_array + i, account);
580
581                         /* memcpy(account, accounts + i, sizeof(email_account_t)) */
582                         /* memset(accounts + i, 0x00, sizeof(email_account_t)) */
583                         
584                         *p = (email_account_list_t*) em_malloc(sizeof(email_account_list_t));
585                         if (!(*p))  {   
586                                 EM_DEBUG_EXCEPTION("malloc failed...");
587                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
588                                 goto FINISH_OFF;
589                         }
590                         
591                         
592                         (*p)->account = account;
593                         
594                         p = &(*p)->next;
595                 }
596                 if (g_account_num)
597                         emcore_free_account_reference();
598                 g_account_retrieved = 1;
599                 g_account_num = count;
600                 g_account_list = account_list;
601         }
602         
603 FINISH_OFF: 
604         if (account_tbl_array)
605                 emstorage_free_account(&account_tbl_array, count, NULL);
606         
607         if (err != EMAIL_ERROR_NONE)  {
608                 g_account_list = account_list;
609                 emcore_free_account_reference();
610         }
611
612         EM_DEBUG_FUNC_END("err [%d]", err);
613         return err;
614 }
615
616 INTERNAL_FUNC int emcore_refresh_account_reference()
617 {
618         EM_DEBUG_FUNC_BEGIN();
619         
620         if (g_account_retrieved && g_account_num)
621                 emcore_free_account_reference();
622         
623         g_account_retrieved = 0;
624         g_account_num = 0;
625         g_account_list = NULL;
626         
627         if (emcore_init_account_reference() != EMAIL_ERROR_NONE)  {
628                 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed...");
629                 return false;
630         }
631         EM_DEBUG_FUNC_END();
632         return true;
633 }
634
635 INTERNAL_FUNC int emcore_free_account_reference()
636 {
637         EM_DEBUG_FUNC_BEGIN();
638         
639         email_account_list_t *p = g_account_list;
640         email_account_list_t *p_next = NULL;
641         while (p)  {
642                 emcore_free_account(p->account);
643                 EM_SAFE_FREE(p->account);
644                 
645                 p_next = p->next;
646                 EM_SAFE_FREE(p);
647                 p = p_next;
648         }
649
650         g_account_retrieved = 0;
651         g_account_num = 0;
652         g_account_list = NULL;
653         EM_DEBUG_FUNC_END();
654         return true;
655 }
656
657 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
658 {
659         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
660         
661         int ret = false;
662         int err = EMAIL_ERROR_NONE;
663         
664         if (count <= 0 || !account_list || !*account_list)  {
665                         err = EMAIL_ERROR_INVALID_PARAM;
666                         goto FINISH_OFF;
667         }
668                 
669         email_account_t *p = *account_list;
670         int i;
671         for (i = 0; i < count; i++)
672                 emcore_free_account(p+i);
673                 
674         EM_SAFE_FREE(p);
675         *account_list = NULL;
676         
677         ret = true;
678         
679 FINISH_OFF: 
680         if (err_code)
681                 *err_code = err;
682         EM_DEBUG_FUNC_END();
683         return ret;
684 }
685
686 INTERNAL_FUNC void emcore_free_option(email_option_t *option)
687 {
688         EM_SAFE_FREE(option->display_name_from);
689         EM_SAFE_FREE(option->signature);
690 }
691
692
693 INTERNAL_FUNC void emcore_free_account(email_account_t *account)
694 {
695         if(!account) return;
696
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);
713
714
715         EM_DEBUG_FUNC_END();
716 }
717
718
719 INTERNAL_FUNC int emcore_get_account_reference_list(email_account_t **account_list, int *count, int *err_code)
720 {
721         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
722         int i, countOfAccounts = 0;
723         int ret = false;
724         int err = EMAIL_ERROR_NONE;
725         email_account_t *accountRef;
726         email_account_list_t *p;
727
728         if (!account_list || !count)  {
729                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
730                 err = EMAIL_ERROR_INVALID_PARAM;
731                 goto FINISH_OFF;
732         }
733
734         p = g_account_list;
735
736         while (p) {
737                 countOfAccounts++;
738                 p = p->next;
739         }
740         
741         EM_DEBUG_LOG("Result count[%d]", countOfAccounts);
742         *count = countOfAccounts;
743
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;
749                         goto FINISH_OFF;
750                 }
751         }
752
753         p = g_account_list;
754         for (i = 0; i < countOfAccounts; i++)  {
755                 accountRef = (*account_list) + i;
756                 memcpy(accountRef, p->account , sizeof(email_account_t));
757                 p = p->next;
758         }
759
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);
763         }
764
765         ret = true;
766
767 FINISH_OFF: 
768         if (ret == false) {
769                 if (account_list) /*  Warn! this is not *account_list. Just account_list */
770                         EM_SAFE_FREE(*account_list);
771         }
772
773         if (err_code != NULL)
774                 *err_code = err;
775         EM_DEBUG_FUNC_END();
776         return ret;
777 }
778         
779
780 #ifdef __FEATURE_BACKUP_ACCOUNT__
781 #include <ss_manager.h>
782
783 static int append_data_into_buffer(char **target_buffer, int *target_buffer_lenth, char *input_data, int input_data_length, int *error_code)
784 {
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;
787
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;
791                 goto FINISH_OFF;
792         }
793
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;
800                         goto FINISH_OFF;
801                 }
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);
805         }
806         else {
807                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
808                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
809                 goto FINISH_OFF;
810         }
811
812         ret_code = true;
813
814 FINISH_OFF: 
815
816         if (error_code)
817                 *error_code = local_error_code;
818         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
819
820         return ret_code;
821 }
822
823
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)
825 {
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;
830
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) */
835
836                 if (byte_stream) {
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");
839                                 goto FINISH_OFF;
840                         }
841                         EM_DEBUG_LOG("append_data_into_buffer succeed for stream_length");
842
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");
845                                 goto FINISH_OFF;
846                         }
847                         EM_DEBUG_LOG("append_data_into_buffer succeed for byte_stream");
848                 }
849         }
850         else {
851                 EM_DEBUG_EXCEPTION("em_convert_account_tbl_to_account failed");
852                 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
853                 goto FINISH_OFF;
854         }
855
856         ret_code = true;
857 FINISH_OFF: 
858         emcore_free_account(&temp_account);
859         if (error_code)
860                 *error_code = local_error_code;
861
862         EM_SAFE_FREE(byte_stream);
863
864         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
865         return ret_code;
866 }
867
868 INTERNAL_FUNC int emcore_backup_accounts(const char *file_path, int *error_code)
869 {
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;
875
876         if (!file_path) {
877                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
878                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
879                 goto FINISH_OFF;        
880         }
881
882         select_num = 1000;
883         
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);
886                 goto FINISH_OFF;        
887         }
888         
889         EM_DEBUG_LOG("select_num [%d]", select_num);
890         
891         if (account_list) {
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;
896                         goto FINISH_OFF;
897                 }
898
899                 memcpy(target_buffer, (char *)&select_num, sizeof(int));
900                 target_buff_length = sizeof(int);
901
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);
905                                 goto FINISH_OFF;        
906                         }
907                 }
908
909                 EM_DEBUG_LOG("target_buff_length [%d]", target_buff_length);
910
911                 ssm_delete_file(file_path, SSM_FLAG_SECRET_OPERATION, NULL);
912                 
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;
916                         goto FINISH_OFF;        
917                 }
918                 
919         }
920
921         ret_code = true;        
922 FINISH_OFF: 
923
924         EM_SAFE_FREE(target_buffer);
925         if (account_list)
926                 emstorage_free_account(&account_list, select_num, &local_error_code_2);
927
928         if (error_code)
929                 *error_code = local_error_code;
930
931         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
932         return ret_code;
933 }
934
935 INTERNAL_FUNC int emcore_restore_accounts(const char *file_path, int *error_code)
936 {
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;
943
944         ssm_file_info_t sfi;
945
946         if (!file_path) {
947                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
948                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
949                 goto FINISH_OFF;        
950         }
951
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");
958                                         goto FINISH_OFF;
959                                 }
960                         }
961                 }
962         }
963
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;
967                 goto FINISH_OFF;
968         }
969
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;
975                 goto FINISH_OFF;
976         }
977
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;
981                 goto FINISH_OFF;
982         }
983
984         EM_DEBUG_LOG("read_length[%d]", read_length);
985
986         if (buffer_length == read_length) {
987                 memcpy((void *)&account_count, temp_buffer, sizeof(int));
988                 buffer_ptr = temp_buffer + sizeof(int);
989
990                 EM_DEBUG_LOG("account_count[%d]", account_count);               
991
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 ;
1001                                         goto FINISH_OFF;
1002                                 }
1003                                 memcpy(account_stream, buffer_ptr, account_stream_length);
1004
1005                                 em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
1006                                 EM_SAFE_FREE(account_stream);
1007                         
1008                                 if (!emcore_create_account(&temp_account, &ret_code)) {
1009                                         EM_DEBUG_EXCEPTION("emcore_create_account() failed.");
1010                                         goto FINISH_OFF;
1011                                 }
1012
1013                                 emcore_free_account(&temp_account);
1014                         }
1015                         buffer_ptr += account_stream_length;
1016                         account_stream_length = 0;
1017                 }
1018         } else {
1019                 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1020                 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1021                 goto FINISH_OFF;
1022         }
1023         ret_code = true;        
1024 FINISH_OFF: 
1025         emcore_free_account(&temp_account);
1026         EM_SAFE_FREE(account_stream);
1027         EM_SAFE_FREE(temp_buffer);
1028
1029         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1030         return ret_code;
1031 }
1032
1033 #endif /*  __FEATURE_BACKUP_ACCOUNT_ */
1034
1035 INTERNAL_FUNC int emcore_query_server_info(const char* domain_name, email_server_info_t **result_server_info)
1036 {
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);
1040         return ret_code;
1041 }
1042
1043
1044 INTERNAL_FUNC int emcore_free_server_info(email_server_info_t **target_server_info)
1045 {
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;
1049
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);
1055                 }
1056                 EM_SAFE_FREE(server_info->protocol_config_array);
1057                 EM_SAFE_FREE(server_info);
1058         }
1059         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1060         return ret_code;        
1061 }
1062
1063 INTERNAL_FUNC int emcore_save_default_account_id(int input_account_id)
1064 {
1065         EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
1066         int ret_code = EMAIL_ERROR_NONE, result_value = 0;
1067
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;
1072         }
1073
1074         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1075         return ret_code;        
1076 }
1077
1078 static int _recover_from_invalid_default_account_id(int *output_account_id)
1079 {
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;
1084
1085         if (output_account_id == NULL) {
1086                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1087                 goto FINISH_OFF;
1088         }
1089
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;
1093                 goto FINISH_OFF;
1094         }
1095
1096         if (account_count > 0) {
1097                 *output_account_id = result_account_list[0].account_id;
1098         }
1099
1100         EM_DEBUG_LOG("output_account_id [%d]", *output_account_id);
1101
1102 FINISH_OFF:
1103
1104         if (result_account_list)
1105                 emstorage_free_account(&result_account_list, account_count, NULL);
1106
1107         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1108         return ret_code;
1109 }
1110
1111 INTERNAL_FUNC int emcore_load_default_account_id(int *output_account_id)
1112 {
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;
1117         
1118         if (output_account_id == NULL) {
1119                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1120                 goto FINISH_OFF;
1121         }
1122
1123         result_value = vconf_get_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, output_account_id);
1124
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;
1129         }
1130
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;
1136                         else
1137                                 goto FINISH_OFF;
1138                 }
1139         }
1140
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;
1145                 }
1146         }
1147
1148 FINISH_OFF:
1149         if (result_account)
1150                 emstorage_free_account(&result_account, 1, NULL);
1151         
1152         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1153         return ret_code;        
1154 }
1155
1156 INTERNAL_FUNC int emcore_recover_from_secured_storage_failure()
1157 {
1158         EM_DEBUG_FUNC_BEGIN();
1159         int err = EMAIL_ERROR_NONE;
1160         int i = 0;
1161         int account_count = 50;
1162         emstorage_account_tbl_t *temp_account_tbl_list = NULL;
1163         emstorage_account_tbl_t *temp_account_tbl      = NULL;
1164
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);
1167                 goto FINISH_OFF;
1168         }
1169
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);
1175                                         goto FINISH_OFF;
1176                                 }
1177                         }
1178                 }
1179         }
1180
1181 FINISH_OFF:
1182
1183         emstorage_free_account(&temp_account_tbl_list, account_count, NULL);
1184         emstorage_free_account(&temp_account_tbl, 1, NULL);
1185
1186         EM_DEBUG_FUNC_END("err [%d]", err);
1187         return err;
1188 }
1189
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)
1191 {
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;
1196
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);
1199
1200 FINISH_OFF:
1201
1202         if (account_tbl_data)
1203                 emstorage_free_account(&account_tbl_data, 1, NULL);
1204
1205         EM_DEBUG_FUNC_END("err [%d]", err);
1206         return err;
1207 }