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