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