Fixed the bug for tizen 3.0
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-init.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 /******************************************************************************
25  * File: email-daemon-init.c
26  * Desc: email-daemon Initialization
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <glib.h>
37 #include <glib-object.h>
38 #include <vconf.h>
39 #include <vconf-internal-account-keys.h>
40 #include <dbus/dbus.h>
41 #include <dlfcn.h>                              /* added for Disabling the Pthread flag log */
42 #include <net_connection.h>
43
44 #include "email-daemon.h"
45 #include "email-storage.h"
46 #include "email-debug-log.h"
47 #include "email-daemon-init.h"
48 #include "email-daemon-account.h"
49 #include "email-daemon-auto-poll.h"
50 #include "email-daemon-event.h"
51 #include "email-core-utils.h"
52 #include "email-core-mail.h"
53 #include "email-core-event.h"
54 #include "email-core-account.h"
55 #include "email-core-mailbox.h"
56 #include "email-core-smtp.h"
57 #include "email-core-global.h"
58 #include "email-core-imap-idle.h"
59 #include "email-storage.h"
60 #include "email-core-task-manager.h"
61 #include "email-core-alarm.h"
62 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
63 #include "email-core-auto-download.h"
64 #endif
65 #include "email-daemon-emn.h"
66 #include "email-network.h"
67 #include "email-device.h"
68 #include "email-core-cynara.h"
69 #include "c-client.h"
70 #include "email-core-smime.h"
71 #include "email-core-container.h"
72
73 connection_h conn = NULL;
74
75 extern void *
76 pop3_parameters(long function, void *value);
77 extern void *
78 imap_parameters(long function, void *value);
79
80 INTERNAL_FUNC int g_client_count = 0;
81 extern int blocking_mode_of_setting;
82 extern GList *alarm_data_list;
83
84 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
85 extern pthread_cond_t  _auto_downalod_available_signal;
86 #endif
87
88 static int default_alarm_callback(int input_timer_id, void *user_parameter);
89
90 /*  static functions */
91 static int _emdaemon_load_email_core()
92 {
93         EM_DEBUG_FUNC_BEGIN();
94
95         int err = EMAIL_ERROR_NONE;
96
97         /* initialize mail core */
98         if (!emdaemon_core_init(&err))
99                 goto FINISH_OFF;
100
101 #ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
102         emdaemon_setup_handler_for_open_ssl_multithread();
103 #endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
104
105         if (emdaemon_start_event_loop(&err) < 0)
106                 goto FINISH_OFF;
107
108         if (emdaemon_start_event_loop_for_sending_mails(&err) < 0)
109                 goto FINISH_OFF;
110
111         emcore_init_task_handler_array();
112
113         /* Disabled task manager
114         if ((err = emcore_start_task_manager_loop()) != EMAIL_ERROR_NONE) {
115                 EM_DEBUG_EXCEPTION("emcore_start_task_manager_loop failed [%d]",err);
116                 goto FINISH_OFF;
117         }
118         */
119
120 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
121         if (emdaemon_start_thread_for_downloading_partial_body(&err) < 0) {
122                 EM_DEBUG_EXCEPTION("emcore_start_thread_for_downloading_partial_body failed [%d]",err);
123                 goto FINISH_OFF;
124         }
125 #endif
126
127 #ifdef __FEATURE_BLOCKING_MODE__
128         emdaemon_init_blocking_mode_status();
129 #endif /* __FEATURE_BLOCKING_MODE__ */
130
131 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
132         if (!emcore_start_auto_download_loop(&err))
133                 goto FINISH_OFF;
134 #endif
135
136 FINISH_OFF:
137
138         EM_DEBUG_FUNC_END("err [%d]", err);
139         return err;
140 }
141
142 static int _emdaemon_unload_email_core()
143 {
144         EM_DEBUG_FUNC_BEGIN();
145         int err = EMAIL_ERROR_NONE;
146
147         /* finish event loop */
148         emcore_stop_event_loop(&err);
149         emcore_stop_task_manager_loop();
150
151     /* Destroy container for daemon */
152     emcore_destroy_container();
153
154 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
155         emcore_stop_auto_download_loop(&err);
156 #endif
157
158 #ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
159         emdaemon_cleanup_handler_for_open_ssl_multithread();
160 #endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
161
162         EM_DEBUG_FUNC_END("err [%d]", err);
163         return err;
164 }
165
166 #ifdef __FEATURE_SYNC_STATUS__
167 static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node, void *input_user_data)
168 {
169         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
170         int handle = 0;
171         int i = 0;
172         int err = EMAIL_ERROR_NONE;
173         int account_count = 0;
174         int sync_start_toggle = 0;
175         email_account_t         *account_list = NULL;
176         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
177
178         if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
179                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
180                 goto FINISH_OFF;
181         }
182
183         if(input_node)
184                 sync_start_toggle = vconf_keynode_get_int(input_node);
185
186         for(i = 0; i < account_count; i++) {
187                 if(sync_start_toggle == 1) {
188                         if(!emstorage_get_mailbox_by_mailbox_type(NULL, account_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl_data, true, &err)) {
189                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for [%d] failed [%d]", account_list[i].account_id, err);
190                                 continue;
191                         }
192
193                         if(!emdaemon_sync_header(NULL, account_list[i].account_id, mailbox_tbl_data->mailbox_id, &handle, &err)) {
194                                 EM_DEBUG_EXCEPTION("emdaemon_sync_header for [%d] failed [%d]", account_list[i].account_id, err);
195                         }
196
197                         emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL); /* prevent 27459: remove unnecessary if clause */
198                         mailbox_tbl_data = NULL;
199                 }
200                 else {
201                         emcore_cancel_all_threads_of_an_account(NULL, account_list[i].account_id);
202                 }
203         }
204
205 FINISH_OFF:
206         if(account_list)
207                 emdaemon_free_account(&account_list, account_count, NULL);
208         if(mailbox_tbl_data)
209                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
210
211         EM_DEBUG_FUNC_END();
212 }
213
214 static void callback_for_AUTO_SYNC_STATUS_from_account_svc(keynode_t *input_node, void *input_user_data)
215 {
216         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
217         int err = EMAIL_ERROR_NONE;
218         int i = 0;
219         int auto_sync_toggle = 0;
220         int account_count = 0;
221         email_account_t *account_list = NULL;
222         email_account_t *account_info = NULL;
223
224         if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
225                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
226                 goto FINISH_OFF;
227         }
228
229         if(input_node)
230                 auto_sync_toggle = vconf_keynode_get_int(input_node);
231
232         for(i = 0; i < account_count; i++) {
233                 account_info = account_list + i;
234
235                 if(auto_sync_toggle == 1) { /* on */
236                         /* start sync */
237                         if(account_info->check_interval < 0)
238                                 account_info->check_interval = ~account_info->check_interval + 1;
239                 }
240                 else { /* off */
241                         /* terminate sync */
242                         if(account_info->check_interval > 0)
243                                 account_info->check_interval = ~account_info->check_interval + 1;
244                 }
245
246                 if(!emdaemon_update_account(NULL, account_info->account_id, account_info, &err)) {
247                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
248                         goto FINISH_OFF;
249                 }
250         }
251
252 FINISH_OFF:
253         if(account_list)
254                 emdaemon_free_account(&account_list, account_count, NULL);
255
256         EM_DEBUG_FUNC_END();
257 }
258 #endif /* __FEATURE_SYNC_STATUS__ */
259
260 static void callback_for_NETWORK_STATUS(connection_type_e new_conn_type, void *input_user_data)
261 {
262         EM_DEBUG_FUNC_BEGIN("new_conn_type [%p], input_user_data [%p]", new_conn_type, input_user_data);
263         int err = EMAIL_ERROR_NONE;
264         int i = 0;
265         int total = 0;
266         int unseen = 0;
267         int account_count = 0;
268         int conn_type = 0;
269         int handle = 0;
270         int send_saved_handle = 0;
271         char *multi_user_name = NULL;
272         email_event_t *event_data = NULL;
273         email_account_t *account_list = NULL;
274         email_account_t *account_info = NULL;
275         emstorage_mailbox_tbl_t *local_mailbox = NULL;
276
277         conn_type = emnetwork_get_network_status();
278
279         switch (new_conn_type) {
280                 case CONNECTION_TYPE_WIFI:
281                 case CONNECTION_TYPE_CELLULAR:
282                         if(conn_type != new_conn_type) {
283                                 EM_DEBUG_LOG("Network type changed from [%d] to [%d]", conn_type, new_conn_type);
284                                 emnetwork_set_network_status(new_conn_type);
285                         }
286                         break;
287
288                 case CONNECTION_TYPE_DISCONNECTED:
289                         EM_DEBUG_LOG("Network is disconnected. [%d]", new_conn_type);
290                         emnetwork_set_network_status(new_conn_type);
291                         goto FINISH_OFF;
292
293                 default:
294                         EM_DEBUG_LOG("Unexpected value came. Set disconnectted network. [%d]", new_conn_type);
295                         emnetwork_set_network_status(CONNECTION_TYPE_DISCONNECTED);
296                         goto FINISH_OFF;
297         }
298
299         emcore_get_sync_fail_event_data(&event_data);
300
301         if (event_data != NULL) {
302                 if (!emcore_insert_event(event_data, &handle, &err)) {
303                         EM_DEBUG_LOG("emcore_insert_event failed : %d", err);
304                 }
305         }
306
307         if (false == emcore_is_partial_body_thd_que_empty())
308                 emcore_pb_thd_set_local_activity_continue(true);
309
310         if (!emdaemon_get_account_list(NULL, &account_list, &account_count, &err))  {
311                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
312                 goto FINISH_OFF;
313         }
314
315         for (i = 0; i < account_count ; i++) {
316                 account_info = account_list + i;
317                 /* check if inbox folder sync is finished */
318                 if (!emstorage_get_mailbox_by_mailbox_type (multi_user_name, account_info->account_id, EMAIL_MAILBOX_TYPE_INBOX, &local_mailbox, false, &err)) {
319                         if (err == EMAIL_ERROR_MAILBOX_NOT_FOUND) {
320                                 int handle = 0; 
321                                 emdaemon_get_imap_mailbox_list(multi_user_name, account_info->account_id, "", &handle, &err);
322                                 if (err != EMAIL_ERROR_NONE) {
323                                         EM_DEBUG_EXCEPTION("emdaemon_get_imap_mailbox_list error [%d]", err);
324                                 }
325                         }
326                 }
327
328                 if (!emstorage_free_mailbox(&local_mailbox, 1, &err)) {
329                         EM_DEBUG_EXCEPTION("emstorage_free_mailbox error [%d]", err);
330                 }
331
332                 /* send mails in outbox */
333                 if (!emcore_get_mail_count_by_query(NULL, account_info->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, 0, &total, &unseen, &err)) {
334                         EM_DEBUG_EXCEPTION("emstorage_get_mail_count_by_query failed [%d]", err);
335                 }
336
337                 if (total <= 0)
338                         continue;
339
340                 if (!emdaemon_send_mail_saved(multi_user_name, account_info->account_id, &send_saved_handle, &err)) {
341                         EM_DEBUG_EXCEPTION("emdaemon_send_mail_saved failed : [%d]", err);
342                 }
343         }
344
345 FINISH_OFF:
346
347         if (local_mailbox)
348                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
349
350         if (account_list)
351                 emdaemon_free_account(&account_list, account_count, NULL);
352
353         EM_DEBUG_FUNC_END("Error : [%d]", err);
354 }
355
356 #ifdef __FEATURE_BLOCKING_MODE__
357 INTERNAL_FUNC void callback_for_BLOCKING_MODE_STATUS(keynode_t *input_node, void *input_user_data)
358 {
359         EM_DEBUG_FUNC_BEGIN();
360         int blocking_mode_of_setting = 0;
361
362         if (!input_node) {
363                 EM_DEBUG_EXCEPTION("Invalid param");
364                 return;
365         }
366
367         blocking_mode_of_setting = vconf_keynode_get_bool(input_node);
368
369         emcore_set_blocking_mode_of_setting(blocking_mode_of_setting);
370
371         EM_DEBUG_FUNC_END();
372 }
373 #endif /* __FEATURE_BLOCKING_MODE__ */
374
375 #ifdef __FEATURE_OMA_EMN__
376 static void callback_for_VCONFKEY_MSG_SERVER_READY(keynode_t *input_node, void *input_user_data)
377 {
378         EM_DEBUG_FUNC_BEGIN();
379         int msg_server_ready = 0;
380
381         if (!input_node) {
382                 EM_DEBUG_EXCEPTION("Invalid param");
383                 return;
384         }
385
386         msg_server_ready = vconf_keynode_get_bool(input_node);
387
388         if(msg_server_ready) {
389                 if(emdaemon_initialize_emn() != EMAIL_ERROR_NONE) {
390                         EM_DEBUG_EXCEPTION("emdaemon_initialize_emn failed");
391                 }
392         }
393
394         return;
395 }
396 #endif /* __FEATURE_OMA_EMN__ */
397
398 static void callback_for_VCONFKEY_GLOBAL_BADGE_STATUS(keynode_t *input_node, void *input_user_data)
399 {
400         EM_DEBUG_FUNC_BEGIN();
401         int noti_status = 0;
402         int err = EMAIL_ERROR_NONE;
403         int badge_ticker = 0;
404
405         if (!input_node) {
406                 EM_DEBUG_EXCEPTION("Invalid param");
407                 return;
408         }
409
410         noti_status = vconf_keynode_get_bool(input_node);
411
412         if (noti_status) {
413                 emcore_display_unread_in_badge((char *)input_user_data);
414         } else {
415                 if (vconf_get_bool(VCONF_VIP_NOTI_BADGE_TICKER, &badge_ticker) != 0) {
416                         EM_DEBUG_EXCEPTION("vconf_get_bool failed");
417                         err = EMAIL_ERROR_GCONF_FAILURE;
418                         goto FINISH_OFF;
419                 }
420                 /* if priority sender is on, show the priority sender unread */
421                 if (badge_ticker) {
422                         emcore_display_unread_in_badge((char *)input_user_data);
423                         goto FINISH_OFF;
424                 }
425
426                 /* reset badge */
427                 if ((err = emcore_display_badge_count((char *)input_user_data, 0)) != EMAIL_ERROR_NONE)
428                         EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
429         }
430
431 FINISH_OFF:
432         EM_DEBUG_FUNC_END();
433 }
434
435
436 static void callback_for_VCONFKEY_PRIORITY_BADGE_STATUS(keynode_t *input_node, void *input_user_data)
437 {
438         EM_DEBUG_FUNC_BEGIN();
439         int noti_status = 0;
440         int err = EMAIL_ERROR_NONE;
441         int badge_ticker = 0;
442
443         if (!input_node) {
444                 EM_DEBUG_EXCEPTION("Invalid param");
445                 return;
446         }
447
448         noti_status = vconf_keynode_get_bool(input_node);
449
450         if (noti_status) {
451                 emcore_display_unread_in_badge((char *)input_user_data);
452         } else {
453
454                 if (vconf_get_bool(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, &badge_ticker) != 0) {
455                         EM_DEBUG_EXCEPTION("vconf_get_bool failed");
456                         err = EMAIL_ERROR_GCONF_FAILURE;
457                         goto FINISH_OFF;
458                 }
459
460                 /*if global badge is on, show the global unread*/
461                 if (badge_ticker) {
462                         emcore_display_unread_in_badge((char *)input_user_data);
463                         goto FINISH_OFF;
464                 }
465                 /* if all badges are off, reset badge count */
466                 if ((err = emcore_display_badge_count((char *)input_user_data, 0)) != EMAIL_ERROR_NONE)
467                         EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
468         }
469
470 FINISH_OFF:
471         EM_DEBUG_FUNC_END();
472 }
473
474 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
475 static void callback_for_VCONFKEY_WIFI_STATE(keynode_t *input_node, void *input_user_data)
476 {
477         EM_DEBUG_FUNC_BEGIN();
478         int wifi_status = 0;
479
480         if (!input_node) {
481                 EM_DEBUG_EXCEPTION("Invalid param");
482                 return;
483         }
484
485         wifi_status = vconf_keynode_get_int(input_node);
486
487         EM_DEBUG_LOG("wifi_status [%d]", wifi_status);
488
489         if (wifi_status > 1) {
490                 if (!emcore_get_pbd_thd_state() &&
491                                 emcore_is_event_queue_empty() &&
492                                 emcore_is_send_event_queue_empty()) {
493                         WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
494                 }
495         }
496
497         EM_DEBUG_FUNC_END();
498 }
499 #endif
500
501 INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code)
502 {
503         EM_DEBUG_FUNC_BEGIN();
504
505         /*  default variable */
506         int ret = false;
507         int err = EMAIL_ERROR_NONE;
508         connection_type_e net_state = CONNECTION_TYPE_DISCONNECTED;
509
510         if (!g_client_count) {
511                 EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
512
513                 EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
514
515         dbus_threads_init_default();
516
517 #if !GLIB_CHECK_VERSION(2, 36, 0) 
518                 g_type_init(); 
519 #endif
520         if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
521             EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
522             goto FINISH_OFF;
523         }
524
525                 err = emcore_init_cynara();
526                 if (err != EMAIL_ERROR_NONE) {
527                         EM_DEBUG_EXCEPTION("emcore_init_cynara failed : [%d]", err);
528                         goto FINISH_OFF;
529                 }
530
531 #ifdef __FEATURE_OMA_EMN__
532         if(emdaemon_initialize_emn() != EMAIL_ERROR_NONE) {
533             vconf_notify_key_changed(VCONFKEY_MSG_SERVER_READY, callback_for_VCONFKEY_MSG_SERVER_READY, NULL);
534         }
535 #endif
536     }
537
538     /* open database */
539         if (!emstorage_open(multi_user_name, &err))  {
540                 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
541                 goto FINISH_OFF;
542         }
543
544         if ((err = emstorage_update_db_table_schema(multi_user_name)) != EMAIL_ERROR_NONE) 
545                 EM_DEBUG_EXCEPTION("emstorage_update_db_table_schema failed [%d]", err);
546
547         if (!emstorage_clean_save_status(multi_user_name, EMAIL_MAIL_STATUS_SAVED, &err))
548                 EM_DEBUG_EXCEPTION("emstorage_check_mail_status Failed [%d]", err );
549
550 #ifdef __FEATURE_AUTO_RETRY_SEND__
551         if ((err = emcore_create_alarm_for_auto_resend (multi_user_name, AUTO_RESEND_INTERVAL)) != EMAIL_ERROR_NONE) {
552                 if (err == EMAIL_ERROR_MAIL_NOT_FOUND)
553                         EM_DEBUG_LOG ("no mail found");
554                 else
555                         EM_DEBUG_EXCEPTION("emcore_create_alarm_for_auto_resend failed [%d]", err);
556         }
557 #endif /* __FEATURE_AUTO_RETRY_SEND__ */
558
559
560         /* Start auto polling */
561 #ifdef __FEATURE_AUTO_POLLING__
562         emdaemon_start_auto_polling(multi_user_name, &err);
563 #endif
564
565 #ifdef __FEATURE_IMAP_IDLE__
566         if (!g_client_count)
567                 emcore_create_imap_idle_thread();
568 #endif /* __FEATURE_IMAP_IDLE__ */
569
570         /* Subscribe Events */
571 #ifdef __FEATURE_SYNC_STATUS__
572         vconf_notify_key_changed(VCONFKEY_ACCOUNT_SYNC_ALL_STATUS_INT,  callback_for_SYNC_ALL_STATUS_from_account_svc,  NULL);
573         vconf_notify_key_changed(VCONFKEY_ACCOUNT_AUTO_SYNC_STATUS_INT, callback_for_AUTO_SYNC_STATUS_from_account_svc, NULL);
574 #endif /* __FEATURE_SYNC_STATUS__  */
575
576         int error_from_connection = 0;
577
578         if ((error_from_connection = connection_create(&conn)) == CONNECTION_ERROR_NONE) {
579                 if (connection_get_type(conn, &net_state) != CONNECTION_ERROR_NONE) {
580                         EM_DEBUG_EXCEPTION("connection_get_type failed");
581                 } else {
582                         emnetwork_set_network_status(net_state);
583                 }
584
585                 if (connection_set_type_changed_cb(conn, callback_for_NETWORK_STATUS, NULL) != CONNECTION_ERROR_NONE)
586                         EM_DEBUG_EXCEPTION("connection_set_type_changed_cb failed");
587         }
588         else {
589                 EM_DEBUG_EXCEPTION("connection_create failed[%d]", error_from_connection);
590         }
591
592 #ifdef __FEATURE_BLOCKING_MODE__
593         vconf_notify_key_changed(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, callback_for_BLOCKING_MODE_STATUS, NULL);
594 #endif
595         vconf_notify_key_changed(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, callback_for_VCONFKEY_GLOBAL_BADGE_STATUS, multi_user_name);
596         vconf_notify_key_changed(VCONF_VIP_NOTI_BADGE_TICKER, callback_for_VCONFKEY_PRIORITY_BADGE_STATUS, multi_user_name);
597         vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, callback_for_VCONFKEY_GLOBAL_BADGE_STATUS, multi_user_name);
598         vconf_notify_key_changed(VCONF_VIP_NOTI_NOTIFICATION_TICKER, callback_for_VCONFKEY_PRIORITY_BADGE_STATUS, multi_user_name);
599 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
600         vconf_notify_key_changed(VCONFKEY_WIFI_STATE, callback_for_VCONFKEY_WIFI_STATE, NULL);
601 #endif
602
603         emcore_display_unread_in_badge(multi_user_name);
604
605         ret = true;
606         g_client_count++;
607
608 FINISH_OFF:
609
610         if (err_code)
611                 *err_code = err;
612
613         EM_DEBUG_FUNC_END("ret [%d], g_client_count [%d]", ret, g_client_count);
614         return ret;
615 }
616
617 INTERNAL_FUNC int emdaemon_finalize(int* err_code)
618 {
619         EM_DEBUG_FUNC_BEGIN();
620
621         /*  default variable */
622         int ret = false;
623         int err = EMAIL_ERROR_NONE;
624
625         if ( (err = _emdaemon_unload_email_core()) != EMAIL_ERROR_NONE) {
626                 EM_DEBUG_EXCEPTION("_emdaemon_unload_email_core failed [%d]", err);
627                 goto FINISH_OFF;
628         }
629
630         connection_destroy(conn);
631
632         /* Finish cynara */
633         emcore_finish_cynara();
634
635         /* close database */
636         if (!emstorage_close(&err)) {
637                 EM_DEBUG_EXCEPTION("emstorage_close failed [%d]", err);
638                 goto FINISH_OFF;
639         }
640
641         /* Openssl clean up */
642         emcore_clean_openssl_library();
643
644         ret = true;
645
646 FINISH_OFF:
647         if (err_code)
648                 *err_code = err;
649
650         return ret;
651 }
652
653 #ifdef __FEATURE_AUTO_POLLING__
654 INTERNAL_FUNC int emdaemon_start_auto_polling(char *multi_user_name, int* err_code)
655 {
656         EM_DEBUG_FUNC_BEGIN();
657
658         /*  default variable */
659         int ret = false, count = 0, i= 0;
660         int err = EMAIL_ERROR_NONE;
661         emstorage_account_tbl_t* account_list = NULL;
662
663         /* get account list */
664         if (!emstorage_get_account_list(multi_user_name, &count, &account_list, false, false, &err))  {
665                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);              
666                 goto FINISH_OFF;
667         }
668
669         for (i = 0; i < count; i++)  {
670                 /* start auto polling, if check_interval not zero */
671                 if(account_list[i].check_interval > 0 || ((account_list[i].peak_days > 0) && account_list[i].peak_interval > 0)) {
672                         if(!emdaemon_add_polling_alarm(multi_user_name, account_list[i].account_id))
673                                 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
674                 }
675         }
676
677         ret = true;
678 FINISH_OFF:
679         if (account_list)
680                 emstorage_free_account(&account_list, count, NULL);
681
682         if (err_code != NULL)
683                 *err_code = err;
684
685         EM_DEBUG_FUNC_END("ret[%d]", ret);
686         return ret;
687 }
688 #endif /* __FEATURE_AUTO_POLLING__ */
689
690 /* initialize mail core */
691 INTERNAL_FUNC int emdaemon_core_init(int *err_code)
692 {
693         EM_DEBUG_FUNC_BEGIN();
694
695         if (err_code != NULL) {
696                 *err_code = EMAIL_ERROR_NONE;
697         }
698
699         mail_link(&imapdriver);    /*  link in the imap driver  */
700         mail_link(&pop3driver);    /*  link in the pop3 driver  */
701
702         mail_link(&unixdriver);    /*  link in the unix driver  */
703         mail_link(&dummydriver);   /*  link in the dummy driver  */
704
705         ssl_onceonlyinit();
706
707         auth_link(&auth_xoauth2);  /*  link in the xoauth2 authenticator  */
708         auth_link(&auth_md5);      /*  link in the md5 authenticator  */
709         auth_link(&auth_pla);      /*  link in the pla authenticator  */
710         auth_link(&auth_log);      /*  link in the log authenticator  */
711
712         /* Disabled to authenticate with plain text */
713         mail_parameters(NIL, SET_DISABLEPLAINTEXT, (void *) 2);
714
715         /* Set max trials for login */
716         imap_parameters(SET_MAXLOGINTRIALS, (void *)2); /* 3-> 2*/
717         pop3_parameters(SET_MAXLOGINTRIALS, (void *)2); /* 3-> 2*/
718         smtp_parameters(SET_MAXLOGINTRIALS, (void *)1); /* 3-> 2*/
719
720         mail_parameters(NIL, SET_SSLCERTIFICATEQUERY, (void *)emnetwork_callback_ssl_cert_query);
721         mail_parameters(NIL, SET_SSLCAPATH, (void *)SSL_CERT_DIRECTORY);
722
723         /* Set time out in second */
724         mail_parameters(NIL, SET_OPENTIMEOUT  , (void *)50);
725         mail_parameters(NIL, SET_READTIMEOUT  , (void *)60); /* 180 -> 15 */
726         mail_parameters(NIL, SET_WRITETIMEOUT , (void *)20); /* 180 -> 15 */
727         mail_parameters(NIL, SET_CLOSETIMEOUT , (void *)20); /* 30 -> 15 */
728
729         emdaemon_init_alarm_data_list();
730
731         /* Openssl library init */
732         emcore_init_openssl_library();
733
734         if (err_code)
735                 *err_code = EMAIL_ERROR_NONE;
736
737         return true;
738 }
739
740 INTERNAL_FUNC bool emdaemon_init_blocking_mode_status()
741 {
742         EM_DEBUG_FUNC_BEGIN("blocking_mode_of_setting : [%d]", blocking_mode_of_setting);
743
744 #ifdef __FEATURE_BLOCKING_MODE__
745         if (vconf_get_bool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &blocking_mode_of_setting) != 0) {
746                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
747                 return false;
748         }
749 #endif /* __FEATURE_BLOCKING_MODE__ */
750
751         EM_DEBUG_FUNC_END();
752         return true;
753 }
754
755 INTERNAL_FUNC int emdaemon_init_alarm_data_list()
756 {
757         EM_DEBUG_FUNC_BEGIN();
758         int ret = ALARMMGR_RESULT_SUCCESS;
759         int err = EMAIL_ERROR_NONE;
760         alarm_data_list = NULL;
761
762         if ((ret = alarmmgr_init(EMAIL_ALARM_DESTINATION)) != ALARMMGR_RESULT_SUCCESS) {
763                 EM_DEBUG_EXCEPTION("alarmmgr_init failed [%d]",ret);
764                 err = EMAIL_ERROR_SYSTEM_FAILURE;
765                 goto FINISH_OFF;
766         }
767
768         if ((ret = alarmmgr_set_cb(default_alarm_callback, NULL)) != ALARMMGR_RESULT_SUCCESS) {
769                 EM_DEBUG_EXCEPTION("alarmmgr_set_cb() failed [%d]", ret);
770                 err = EMAIL_ERROR_SYSTEM_FAILURE;
771                 goto FINISH_OFF;
772         }
773
774 FINISH_OFF:
775
776         EM_DEBUG_FUNC_END("err [%d]", err);
777         return err;
778 }
779
780 static int default_alarm_callback(int input_timer_id, void *user_parameter)
781 {
782         EM_DEBUG_FUNC_BEGIN("input_timer_id [%d] user_parameter [%p]", input_timer_id, user_parameter);
783         int err = EMAIL_ERROR_NONE;
784         email_alarm_data_t *alarm_data = NULL;
785
786         EM_DEBUG_ALARM_LOG("default_alarm_callback input_timer_id[%d]", input_timer_id);
787
788         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_ALARM_CALLBACK, false, NULL);
789
790         if ((err = emcore_get_alarm_data_by_alarm_id(input_timer_id, &alarm_data)) != EMAIL_ERROR_NONE || alarm_data == NULL) {
791                 EM_DEBUG_EXCEPTION("emcore_get_alarm_data_by_alarm_id failed [%d]", err);
792                 goto FINISH_OFF;
793         }
794
795         emcore_delete_alram_data_from_alarm_data_list(alarm_data);
796
797         if ((err = alarm_data->alarm_callback(alarm_data, user_parameter)) != EMAIL_ERROR_NONE) {
798                 EM_DEBUG_EXCEPTION("alarm_callback failed [%d]", err);
799                 goto FINISH_OFF;
800         }
801
802 FINISH_OFF:
803
804         if (alarm_data) {
805                 EM_SAFE_FREE(alarm_data->user_data);
806                 EM_SAFE_FREE(alarm_data);
807         }
808
809         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_ALARM_CALLBACK, true, NULL);
810
811
812         EM_DEBUG_FUNC_END("err [%d]", err);
813         return err;
814 }
815