Tizen 2.0 Release
[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
43 #include "email-daemon.h"
44 #include "email-storage.h"
45 #include "email-debug-log.h"
46 #include "email-daemon-account.h"
47 #include "email-daemon-auto-poll.h"   
48 #include "email-core-utils.h"
49 #include "email-core-mail.h"
50 #include "email-core-event.h" 
51 #include "email-core-account.h"    
52 #include "email-core-mailbox.h"    
53 #include "email-core-api.h"
54 #include "email-core-smtp.h"    
55 #include "email-core-global.h"
56 #include "email-storage.h"   
57 #include "email-core-sound.h" 
58 #include "email-core-task-manager.h"
59
60 extern int g_client_count ;
61
62 /*  static functions */
63 static int _emdaemon_load_email_core()
64 {
65         EM_DEBUG_FUNC_BEGIN();
66
67         int err = EMAIL_ERROR_NONE;
68
69         /* initialize mail core */
70         if (!emcore_init(&err))
71                 goto FINISH_OFF;
72
73         if (emcore_start_event_loop(&err) < 0)
74                 goto FINISH_OFF;
75
76         if (emcore_start_event_loop_for_sending_mails(&err) < 0)
77                 goto FINISH_OFF;
78
79         /* Disabled task manager
80         if ((err = emcore_start_task_manager_loop()) != EMAIL_ERROR_NONE) {
81                 EM_DEBUG_EXCEPTION("emcore_start_task_manager_loop failed [%d]",err);
82                 goto FINISH_OFF;
83         }
84         */
85
86 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
87         if (emcore_start_thread_for_downloading_partial_body(&err) < 0) {
88                 EM_DEBUG_EXCEPTION("emcore_start_thread_for_downloading_partial_body failed [%d]",err);
89                 goto FINISH_OFF;
90         }
91 #endif
92         if (emcore_start_thread_for_alerting_new_mails(&err) < 0)
93                 goto FINISH_OFF;
94
95 FINISH_OFF:
96
97         return err;
98 }
99
100 static int _emdaemon_unload_email_core()
101 {
102         EM_DEBUG_FUNC_BEGIN();
103         int err = EMAIL_ERROR_NONE;
104
105         /* finish event loop */
106         emcore_stop_event_loop(&err);
107         emcore_stop_task_manager_loop();
108
109         EM_DEBUG_FUNC_END("err [%d]", err);
110         return err;
111 }
112
113 static void callback_for_SYNC_ALL_STATUS_from_account_svc(keynode_t *input_node, void *input_user_data)
114 {
115         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
116         int handle = 0;
117         int i = 0;
118         int err = EMAIL_ERROR_NONE;
119         int account_count = 0;
120         int sync_start_toggle = 0;
121         email_account_t         *account_list = NULL;
122         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
123
124         if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
125                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
126                 goto FINISH_OFF;
127         }
128
129         if(input_node)
130                 sync_start_toggle = vconf_keynode_get_int(input_node);
131
132         for(i = 0; i < account_count; i++) {
133                 if(sync_start_toggle == 1) {
134                         if(!emstorage_get_mailbox_by_mailbox_type(account_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl_data, true, &err)) {
135                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for [%d] failed [%d]", account_list[i].account_id, err);
136                                 continue;
137                         }
138
139                         if(!emdaemon_sync_header(account_list[i].account_id, mailbox_tbl_data->mailbox_id, &handle, &err)) {
140                                 EM_DEBUG_EXCEPTION("emdaemon_sync_header for [%d] failed [%d]", account_list[i].account_id, err);
141                         }
142
143                         emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL); /* prevent 27459: remove unnecesary if clause */
144                         mailbox_tbl_data = NULL;
145                 }
146                 else {
147                         emcore_cancel_all_threads_of_an_account(account_list[i].account_id);
148                 }
149         }
150
151 FINISH_OFF:
152         if(account_list)
153                 emdaemon_free_account(&account_list, account_count, NULL);
154         if(mailbox_tbl_data)
155                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
156
157         EM_DEBUG_FUNC_END();
158 }
159
160 static void callback_for_AUTO_SYNC_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 err = EMAIL_ERROR_NONE;
164         int i = 0;
165         int auto_sync_toggle = 0;
166         int account_count = 0;
167         email_account_t *account_list = NULL;
168         email_account_t *account_info = NULL;
169
170         if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
171                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
172                 goto FINISH_OFF;
173         }
174
175         if(input_node)
176                 auto_sync_toggle = vconf_keynode_get_int(input_node);
177
178         for(i = 0; i < account_count; i++) {
179                 account_info = account_list + i;
180
181                 if(auto_sync_toggle == 1) { /* on */
182                         /* start sync */
183                         if(account_info->check_interval < 0)
184                                 account_info->check_interval = ~account_info->check_interval + 1;
185                 }
186                 else { /* off */
187                         /* terminate sync */
188                         if(account_info->check_interval > 0)
189                                 account_info->check_interval = ~account_info->check_interval + 1;
190                 }
191
192                 if(!emdaemon_update_account(account_info->account_id, account_info, &err)) {
193                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
194                         goto FINISH_OFF;
195                 }
196         }
197
198 FINISH_OFF:
199         if(account_list)
200                 emdaemon_free_account(&account_list, account_count, NULL);
201
202         EM_DEBUG_FUNC_END();
203 }
204
205 static void callback_for_NETWORK_STATUS(keynode_t *input_node, void *input_user_data)
206 {
207         EM_DEBUG_FUNC_BEGIN("input_node [%p], input_user_data [%p]", input_node, input_user_data);
208         int err = EMAIL_ERROR_NONE;
209         int i = 0;
210         int total = 0;
211         int unseen = 0;
212         int network_status = 0;
213         int account_count = 0;
214         email_account_t *account_list = NULL;
215         email_account_t *account_info = NULL;
216         emstorage_mailbox_tbl_t *local_mailbox = NULL;
217
218         if (input_node)
219                 network_status = vconf_keynode_get_int(input_node);
220
221         if (network_status == VCONFKEY_NETWORK_OFF) {
222                 EM_DEBUG_EXCEPTION("Network is OFF");
223                 goto FINISH_OFF;
224         }
225
226         if (!emdaemon_get_account_list(&account_list, &account_count, &err))  {
227                 EM_DEBUG_EXCEPTION("emdaemon_get_account_list failed [%d]", err);
228                 goto FINISH_OFF;
229         }
230
231         for (i = 0; i < account_count ; i++) {
232                 account_info = account_list + i;
233
234                 if (!emstorage_get_mailbox_by_mailbox_type(account_info->account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, false, &err)) {
235                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
236                         goto FINISH_OFF;
237                 }
238
239                 if (!emstorage_get_mail_count(account_info->account_id, local_mailbox->mailbox_name, &total, &unseen, false, &err)) {
240                         EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", err);
241                         goto FINISH_OFF;
242                 }
243
244                 if (total <= 0)
245                         continue;
246
247                 if (!emcore_send_saved_mail(account_info->account_id, local_mailbox->mailbox_name, &err)) {
248                         EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed [%d]", err);
249                         goto FINISH_OFF;
250                 }
251
252                 if (!emstorage_free_mailbox(&local_mailbox, 1, &err)) {
253                         EM_DEBUG_EXCEPTION("emstorage_free_mailbox failed [%d]", err);
254                         goto FINISH_OFF;
255                 }
256         }
257
258 FINISH_OFF:
259
260         if (local_mailbox)
261                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
262
263         if (account_list)
264                 emdaemon_free_account(&account_list, account_count, NULL);
265
266         EM_DEBUG_FUNC_END("Error : [%d]", err);
267 }
268
269 INTERNAL_FUNC int emdaemon_initialize(int* err_code)
270 {
271         EM_DEBUG_FUNC_BEGIN();
272         
273         /*  default variable */
274         int ret = false;
275         int err = EMAIL_ERROR_NONE;
276         
277         if (g_client_count > 0)  {
278                 EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
279                 g_client_count++;
280                 return true;
281         }
282         else 
283                 EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
284
285         dbus_threads_init_default();
286
287         g_type_init();
288
289         emstorage_shm_file_init(SHM_FILE_FOR_DB_LOCK);
290
291 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
292         emstorage_shm_file_init(SHM_FILE_FOR_MAIL_ID_LOCK);
293 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
294
295         /* open database */
296         if (!emstorage_open(&err))  {
297                 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
298                 goto FINISH_OFF;
299         }
300
301         if (!emstorage_clean_save_status(EMAIL_MAIL_STATUS_SAVED, &err))
302                 EM_DEBUG_EXCEPTION("emstorage_check_mail_status Failed [%d]", err );
303         
304         g_client_count = 0;    
305         
306         if (!emdaemon_initialize_account_reference())  {
307                 EM_DEBUG_EXCEPTION("emdaemon_initialize_account_reference fail...");
308                 err = EMAIL_ERROR_DB_FAILURE;
309                 goto FINISH_OFF;
310         }
311     EM_DEBUG_LOG("emdaemon_initialize_account_reference over - g_client_count [%d]", g_client_count);   
312         
313         if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
314                 EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
315                 goto FINISH_OFF;
316         }
317
318         /* Subscribe Events */
319         vconf_notify_key_changed(VCONFKEY_ACCOUNT_SYNC_ALL_STATUS_INT,  callback_for_SYNC_ALL_STATUS_from_account_svc,  NULL);
320         vconf_notify_key_changed(VCONFKEY_ACCOUNT_AUTO_SYNC_STATUS_INT, callback_for_AUTO_SYNC_STATUS_from_account_svc, NULL);
321         vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS, callback_for_NETWORK_STATUS, NULL);
322
323         emcore_display_unread_in_badge();
324         
325         ret = true;
326         
327 FINISH_OFF:
328         if (ret == true)
329                 g_client_count = 1;
330
331         if (err_code)
332                 *err_code = err;
333         
334         EM_DEBUG_FUNC_END("ret [%d], g_client_count [%d]", ret, g_client_count);
335         return ret;
336 }
337
338 INTERNAL_FUNC int emdaemon_finalize(int* err_code)
339 {
340         EM_DEBUG_FUNC_BEGIN();
341         
342         /*  default variable */
343         int ret = false;
344         int err = EMAIL_ERROR_NONE;
345         
346         if (g_client_count > 1) {
347                 EM_DEBUG_EXCEPTION("engine is still used by application. decreased counter=[%d]", g_client_count);
348                 g_client_count--;
349                 err = EMAIL_ERROR_CLOSE_FAILURE;
350                 goto FINISH_OFF;
351         }
352         
353         if ( (err = _emdaemon_unload_email_core()) != EMAIL_ERROR_NONE) {
354                 EM_DEBUG_EXCEPTION("_emdaemon_unload_email_core failed [%d]", err);
355                 goto FINISH_OFF;
356         }
357         
358         /* free account reference list */
359         emcore_free_account_reference();
360         
361         /* close database */
362         if (!emstorage_close(&err)) {
363                 EM_DEBUG_EXCEPTION("emstorage_close failed [%d]", err);
364                 goto FINISH_OFF;
365         }
366         
367         g_client_count = 0;
368         
369 #ifdef __FEATURE_AUTO_POLLING__
370         emdaemon_free_account_alarm_binder_list();
371 #endif
372
373         ret = true;
374         
375 FINISH_OFF:
376         if (err_code)
377                 *err_code = err;
378         
379         return ret;
380 }
381
382 #ifdef __FEATURE_AUTO_POLLING__
383 INTERNAL_FUNC int emdaemon_start_auto_polling(int* err_code)
384 {
385         EM_DEBUG_FUNC_BEGIN();
386         
387         /*  default variable */
388         int ret = false, count = 0, i= 0;
389         int err = EMAIL_ERROR_NONE;
390         emstorage_account_tbl_t* account_list = NULL;
391
392         /* get account list */
393         if (!emstorage_get_account_list(&count, &account_list, false, false, &err))  {
394                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);              
395                 goto FINISH_OFF;
396         }
397
398         for (i = 0; i < count; i++)  {
399                 /* start auto polling, if check_interval not zero */
400                 if(account_list[i].check_interval > 0) {
401                         if(!emdaemon_add_polling_alarm( account_list[i].account_id,account_list[i].check_interval))
402                                 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
403                 }
404         }
405
406         ret = true;
407 FINISH_OFF:  
408         if (account_list)
409                 emstorage_free_account(&account_list, count, NULL);
410         
411         if (err_code != NULL)
412                 *err_code = err;
413         
414         return ret;
415 }
416 #endif /* __FEATURE_AUTO_POLLING__ */
417