2.0_alpha release commit
[framework/messaging/email-service.git] / email-daemon / email-daemon-init.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /******************************************************************************
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 <heynoti/heynoti.h>
43
44 #include "email-daemon.h"
45 #include "email-storage.h"
46 #include "email-debug-log.h"
47 #include "email-daemon-account.h"
48 #include "email-daemon-auto-poll.h"   
49 #include "email-core-utils.h"
50 #include "email-core-mail.h"
51 #include "email-core-event.h" 
52 #include "email-core-account.h"    
53 #include "email-core-mailbox.h"    
54 #include "email-core-api.h"    
55 #include "email-core-global.h"
56 #include "email-storage.h"   
57 #include "email-core-sound.h" 
58
59 extern int g_client_count ;
60
61 int fd_HibernationNoti;
62
63 /*  static functions */
64 static int _emdaemon_load_email_core()
65 {
66         EM_DEBUG_FUNC_BEGIN();
67
68         int err = EMAIL_ERROR_NONE;
69
70         /* initialize mail core */
71         if (!emcore_init(&err))
72                 goto FINISH_OFF;
73
74         if (emcore_start_event_loop(&err) < 0)
75                 goto FINISH_OFF;
76
77         if (emcore_start_event_loop_for_sending_mails(&err) < 0)
78                 goto FINISH_OFF;
79
80 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
81         if (emcore_start_thread_for_downloading_partial_body(&err) < 0) {
82                 EM_DEBUG_EXCEPTION("emcore_start_thread_for_downloading_partial_body failed [%d]",err);
83                 goto FINISH_OFF;
84         }
85 #endif
86         if (emcore_start_thread_for_alerting_new_mails(&err) < 0)
87                 goto FINISH_OFF;
88
89 FINISH_OFF:
90
91         return err;
92 }
93
94 static int _emdaemon_unload_email_core()
95 {
96         EM_DEBUG_FUNC_BEGIN();
97         int err = EMAIL_ERROR_NONE;
98
99         /* finish event loop */
100         emcore_stop_event_loop(&err);
101
102         EM_DEBUG_FUNC_END("err [%d]", err);
103         return err;
104 }
105
106 static void hibernation_enter_callback()
107 {
108         EM_DEBUG_FUNC_BEGIN();
109         emstorage_db_close(NULL);
110         EM_DEBUG_FUNC_END();
111 }
112
113 static void hibernation_leave_callback()
114 {
115         EM_DEBUG_FUNC_BEGIN();
116         emstorage_db_open(NULL);
117         EM_DEBUG_FUNC_END();
118 }
119
120 static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node, void *input_user_data)
121 {
122         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
123         unsigned handle = 0;
124         int i = 0;
125         int err = EMAIL_ERROR_NONE;
126         int account_count = 0;
127         email_account_t         *account_list = NULL;
128         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
129
130         if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
131                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
132                 goto FINISH_OFF;
133         }
134
135         for(i = 0; i < account_count; i++) {
136                 if(!emstorage_get_mailbox_by_mailbox_type(account_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl_data, true, &err)) {
137                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for [%d] failed [%d]", account_list[i].account_id, err);
138                         continue;
139                 }
140
141                 if(!emdaemon_sync_header(account_list[i].account_id, mailbox_tbl_data->mailbox_id, &handle, &err)) {
142                         EM_DEBUG_EXCEPTION("emdaemon_sync_header for [%d] failed [%d]", account_list[i].account_id, err);
143                 }
144                 if(mailbox_tbl_data)
145                         emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
146                 mailbox_tbl_data = NULL;
147         }
148
149 FINISH_OFF:
150         if(account_list)
151                 emdaemon_free_account(&account_list, account_count, NULL);
152         if(mailbox_tbl_data)
153                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
154
155         EM_DEBUG_FUNC_END();
156 }
157
158 static void callback_for_AUTO_SYNC_STATUS_from_account_svc(keynode_t *input_node, void *input_user_data)
159 {
160         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
161         int err = EMAIL_ERROR_NONE;
162         int i = 0;
163         int auto_sync_toggle = 0;
164         int account_count = 0;
165         email_account_t *account_list = NULL;
166         email_account_t *account_info = NULL;
167
168         if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
169                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
170                 goto FINISH_OFF;
171         }
172
173         if(input_node)
174                 auto_sync_toggle = vconf_keynode_get_int(input_node);
175
176         for(i = 0; i < account_count; i++) {
177                 account_info = account_list + i;
178
179                 if(auto_sync_toggle == 1) { /* on */
180                         /* start sync */
181                         if(account_info->check_interval < 0)
182                                 account_info->check_interval = ~account_info->check_interval + 1;
183                 }
184                 else { /* off */
185                         /* terminate sync */
186                         if(account_info->check_interval > 0)
187                                 account_info->check_interval = ~account_info->check_interval + 1;
188                 }
189
190                 if(!emdaemon_update_account(account_info->account_id, account_info, &err)) {
191                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
192                         goto FINISH_OFF;
193                 }
194         }
195
196 FINISH_OFF:
197         if(account_list)
198                 emdaemon_free_account(&account_list, account_count, NULL);
199
200         EM_DEBUG_FUNC_END();
201 }
202
203 INTERNAL_FUNC int emdaemon_initialize(int* err_code)
204 {
205         EM_DEBUG_FUNC_BEGIN();
206         
207         /*  default variable */
208         int ret = false;
209         int err = EMAIL_ERROR_NONE;
210         
211         if (g_client_count > 0)  {
212                 EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
213                 g_client_count++;
214                 return true;
215         }
216         else 
217                 EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
218
219         dbus_threads_init_default();
220         
221         g_type_init();
222
223         emstorage_shm_file_init(SHM_FILE_FOR_DB_LOCK);
224
225 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
226         emstorage_shm_file_init(SHM_FILE_FOR_MAIL_ID_LOCK);
227 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
228
229         /* open database */
230         if (!emstorage_open(&err))  {
231                 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
232                 goto FINISH_OFF;
233         }
234
235         if (!emstorage_clean_save_status(EMAIL_MAIL_STATUS_SAVED, &err))
236                 EM_DEBUG_EXCEPTION("emstorage_check_mail_status Failed [%d]", err );
237         
238         g_client_count = 0;    
239         
240         if (!emdaemon_initialize_account_reference())  {
241                 EM_DEBUG_EXCEPTION("emdaemon_initialize_account_reference fail...");
242                 err = EMAIL_ERROR_DB_FAILURE;
243                 goto FINISH_OFF;
244         }
245     EM_DEBUG_LOG("emdaemon_initialize_account_reference over - g_client_count [%d]", g_client_count);   
246         
247         if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
248                 EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
249                 goto FINISH_OFF;
250         }
251
252         /* Subscribe Events */
253         fd_HibernationNoti = heynoti_init();
254
255         if(fd_HibernationNoti == -1)
256                 EM_DEBUG_EXCEPTION("heynoti_init failed");
257         else {
258                 EM_DEBUG_LOG("heynoti_init Success");
259                 ret = heynoti_subscribe(fd_HibernationNoti, "HIBERNATION_ENTER", hibernation_enter_callback, (void *)fd_HibernationNoti);
260                 EM_DEBUG_LOG("heynoti_subscribe returns %d", ret);
261                 ret = heynoti_subscribe(fd_HibernationNoti, "HIBERNATION_LEAVE", hibernation_leave_callback, (void *)fd_HibernationNoti);
262                 EM_DEBUG_LOG("heynoti_subscribe returns %d", ret);
263                 ret = heynoti_attach_handler(fd_HibernationNoti);
264                 EM_DEBUG_LOG("heynoti_attach_handler returns %d", ret);
265         }
266
267         vconf_notify_key_changed(VCONFKEY_ACCOUNT_SYNC_ALL_STATUS_INT,  callback_for_SYNC_ALL_STATUS_from_account_svc,  NULL);
268         vconf_notify_key_changed(VCONFKEY_ACCOUNT_AUTO_SYNC_STATUS_INT, callback_for_AUTO_SYNC_STATUS_from_account_svc, NULL);
269
270         emcore_check_unread_mail();
271         
272         ret = true;
273         
274 FINISH_OFF:
275         if (ret == true)
276                 g_client_count = 1;
277
278         if (err_code)
279                 *err_code = err;
280         
281         EM_DEBUG_FUNC_END("ret [%d], g_client_count [%d]", ret, g_client_count);
282         return ret;
283 }
284
285 INTERNAL_FUNC int emdaemon_finalize(int* err_code)
286 {
287         EM_DEBUG_FUNC_BEGIN();
288         
289         /*  default variable */
290         int ret = false;
291         int err = EMAIL_ERROR_NONE;
292         
293         if (g_client_count > 1) {
294                 EM_DEBUG_EXCEPTION("engine is still used by application. decreased counter=[%d]", g_client_count);
295                 g_client_count--;
296                 err = EMAIL_ERROR_CLOSE_FAILURE;
297                 goto FINISH_OFF;
298         }
299         
300         if ( (err = _emdaemon_unload_email_core()) != EMAIL_ERROR_NONE) {
301                 EM_DEBUG_EXCEPTION("_emdaemon_unload_email_core failed [%d]", err);
302                 goto FINISH_OFF;
303         }
304         
305         /* free account reference list */
306         emdaemon_free_account_reference();
307         
308         /* close database */
309         if (!emstorage_close(&err)) {
310                 EM_DEBUG_EXCEPTION("emstorage_close failed [%d]", err);
311                 goto FINISH_OFF;
312         }
313         
314         /* Unsubscribe Events */
315
316         if(fd_HibernationNoti != -1) {
317                 heynoti_unsubscribe(fd_HibernationNoti, "HIBERNATION_ENTER", hibernation_enter_callback);
318                 heynoti_unsubscribe(fd_HibernationNoti, "HIBERNATION_LEAVE", hibernation_leave_callback);
319                 heynoti_close(fd_HibernationNoti);
320         }
321
322         g_client_count = 0;
323         
324 #ifdef __FEATURE_AUTO_POLLING__
325         emdaemon_free_account_alarm_binder_list();
326 #endif
327
328         ret = true;
329         
330 FINISH_OFF:
331         if (err_code)
332                 *err_code = err;
333         
334         return ret;
335 }
336
337 #ifdef __FEATURE_AUTO_POLLING__
338 INTERNAL_FUNC int emdaemon_start_auto_polling(int* err_code)
339 {
340         EM_DEBUG_FUNC_BEGIN();
341         
342         /*  default variable */
343         int ret = false, count = 0, i= 0;
344         int err = EMAIL_ERROR_NONE;
345         emstorage_account_tbl_t* account_list = NULL;
346
347         /* get account list */
348         if (!emstorage_get_account_list(&count, &account_list, false, false, &err))  {
349                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);              
350                 goto FINISH_OFF;
351         }
352
353         for (i = 0; i < count; i++)  {
354                 /* start auto polling, if check_interval not zero */
355                 if(account_list[i].check_interval > 0) {
356                         if(!emdaemon_add_polling_alarm( account_list[i].account_id,account_list[i].check_interval))
357                                 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
358                 }
359         }
360
361         ret = true;
362 FINISH_OFF:  
363         if (account_list)
364                 emstorage_free_account(&account_list, count, NULL);
365         
366         if (err_code != NULL)
367                 *err_code = err;
368         
369         return ret;
370 }
371 #endif /* __FEATURE_AUTO_POLLING__ */
372