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