tizen beta release
[framework/messaging/email-service.git] / email-engine / emf-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: emf-init.c
26  * Desc: Mail Framework 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 <dbus/dbus.h>
40
41 #include "emflib.h"
42 #include "em-storage.h"
43 #include "emf-global.h"
44 #include "emf-dbglog.h"
45 #include "emf-account.h"
46 #include "emf-auto-poll.h"   
47 #include "em-core-utils.h"
48 #include "em-core-mesg.h"
49 #include "em-core-event.h" 
50 #include "em-core-account.h"    
51 #include "em-core-mailbox.h"    
52 #include "em-core-api.h"    
53 #include "em-core-global.h"
54 #include "em-storage.h"   
55 #include "em-core-sound.h" 
56
57
58 /* added for Disabling the Pthread flag log */
59
60 #ifdef API_TEST
61 #include "emf-api-test.h"
62 #endif
63
64 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
65 #include <contacts-svc.h>
66 #endif
67
68 #include <dlfcn.h>
69 #define ENGINE_PATH_EM_CORE     "libemail-core.so"
70
71 extern int g_client_count ;
72 extern int g_client_run;
73
74 /*  static functions */
75 static int _emf_load_engine(emf_engine_type_t type, int* err_code);
76 static int _emf_unload_engine(emf_engine_type_t type, int* err_code);
77
78 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
79 static void _emf_contact_change_cb(void *data)
80 {
81         EM_DEBUG_FUNC_BEGIN();
82         /*  Run new thread and contact sync */
83         EM_DEBUG_LOG("Contact Changed.");
84         em_core_contact_sync_handler();
85 }
86 #endif
87
88 EXPORT_API int emf_init(int* err_code)
89 {
90         EM_DEBUG_FUNC_BEGIN();
91         
92         /*  default variable */
93         int ret = false;
94         int err = EMF_ERROR_NONE;
95         
96         if (g_client_count > 0)  {
97                 EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
98                 g_client_count++;
99                 return true;
100         }
101         else 
102                 EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
103
104 #ifdef __FEATURE_USE_PTHREAD__
105         dbus_threads_init_default();
106 #else   /*  __FEATURE_USE_PTHREAD__ */
107         if (!g_thread_supported())
108                 g_thread_init(NULL);
109         dbus_g_thread_init ();
110 #endif  /*  __FEATURE_USE_PTHREAD__ */
111         
112         g_type_init();
113
114
115         em_storage_shm_file_init(SHM_FILE_FOR_DB_LOCK);
116
117 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
118         em_storage_shm_file_init(SHM_FILE_FOR_MAIL_ID_LOCK);
119 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
120
121         /* open database */
122         if (!em_storage_open(&err))  {
123                 EM_DEBUG_EXCEPTION("em_storage_open failed [%d]", err);
124                 goto FINISH_OFF;
125         }
126
127         if (!em_storage_clean_save_status(EMF_MAIL_STATUS_SAVED, &err))
128                 EM_DEBUG_EXCEPTION("em_storage_check_mail_status Failed [%d]", err );
129         
130         g_client_count = 0;    
131         
132         if (!emf_init_account_reference())  {
133                 EM_DEBUG_EXCEPTION("emf_init_account_reference fail...");
134                 err = EMF_ERROR_DB_FAILURE;
135                 goto FINISH_OFF;
136         }
137     EM_DEBUG_LOG("emf_init_account_reference over - g_client_count [%d]", g_client_count);      
138         
139         if (!_emf_load_engine(EMF_ENGINE_TYPE_EM_CORE, &err))  {
140                 EM_DEBUG_EXCEPTION("_emf_load_engine failed [%d]", err);
141                 
142                 /* err = EMF_ERROR_DB_FAILURE; */
143                 goto FINISH_OFF;
144         }
145
146         em_core_check_unread_mail(); 
147         
148 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
149         em_core_init_last_sync_time();
150         contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, _emf_contact_change_cb, NULL);
151 #endif
152         
153         ret = true;
154         
155 FINISH_OFF:
156         if (ret == true)
157                 g_client_count = 1;
158
159         EM_DEBUG_LOG(">>>>>>>>> ret value : %d g_client_count [%d]", ret, g_client_count);      
160 #ifdef API_TEST
161         emf_api_test_send_mail();
162 #endif
163         
164         if (err_code)
165                 *err_code = err;
166         
167         EM_DEBUG_FUNC_END();
168         return ret;
169 }
170
171 EXPORT_API int emf_close(int* err_code)
172 {
173         EM_DEBUG_FUNC_BEGIN();
174         
175         /*  default variable */
176         int ret = false;
177         int err = EMF_ERROR_NONE;
178         
179         if (g_client_count > 1) {
180                 EM_DEBUG_EXCEPTION("engine is still used by application. decreased counter=[%d]", g_client_count);
181                 
182                 g_client_count--;
183                 
184                 /* err = EMF_ERROR_CLOSE_FAILURE; */
185                 goto FINISH_OFF;
186         }
187         
188         if (!_emf_unload_engine(EMF_ENGINE_TYPE_EM_CORE, &err)) {
189                 EM_DEBUG_EXCEPTION("_emf_unload_engine failed [%d]", err);
190                 goto FINISH_OFF;
191         }
192         
193         /* free account reference list */
194         emf_free_account_reference();
195         
196         /* close database */
197         if (!em_storage_close(&err)) {
198                 EM_DEBUG_EXCEPTION("em_storage_close failed [%d]", err);
199                 goto FINISH_OFF;
200         }
201         
202         g_client_count = 0;
203         
204 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
205         contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, _emf_contact_change_cb);
206 #endif
207         
208 #ifdef __FEATURE_AUTO_POLLING__
209         emf_free_account_alarm_binder_list();
210 #endif
211
212         
213         ret = true;
214         
215 FINISH_OFF:
216         if (err_code)
217                 *err_code = err;
218         
219         return ret;
220 }
221
222 static int _emf_load_engine(emf_engine_type_t type, int* err_code)
223 {
224         EM_DEBUG_FUNC_BEGIN();
225         
226         int err = EMF_ERROR_NONE;
227         int ret = false;
228         
229         switch (type)  {
230                 case 1:
231                         /* initialize mail core */
232                         if (!em_core_init(&err)) 
233                                 goto FINISH_OFF;
234                         
235                         if (em_core_event_loop_start(&err) < 0) 
236                                 goto FINISH_OFF;
237
238                         if (em_core_send_event_loop_start(&err) < 0) 
239                                 goto FINISH_OFF;
240
241 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
242                         if (em_core_partial_body_thread_loop_start(&err) < 0) {
243                                 EM_DEBUG_EXCEPTION("em_core_partial_body_thread_loop_start failed [%d]",err);
244                                 goto FINISH_OFF;
245                         }
246 #endif
247 /*
248                         if ( em_core_open_contact_db_library() == false )
249                                 goto FINISH_OFF;
250 */
251                         if (em_core_alert_loop_start(&err) < 0)         
252                                 goto FINISH_OFF;
253                 
254                         break;
255                 
256                 default:
257                         return false;
258         }
259         
260         ret = true;
261         
262 FINISH_OFF:
263         if (err_code)
264                 *err_code = err;
265         
266         return ret;
267 }
268
269 static int _emf_unload_engine(emf_engine_type_t type, int* err_code)
270 {
271         EM_DEBUG_FUNC_BEGIN();
272         
273         int ret = false;
274         int err = EMF_ERROR_NONE;
275         
276         switch (type)  {
277                 case EMF_ENGINE_TYPE_EM_CORE:
278                                 /* finish event loop */
279                                 em_core_event_loop_stop(&err);
280 /*                              
281                         em_core_close_contact_db_library();
282 */                      
283                         ret = true;
284                         break;
285                         
286                 default:
287                         err = EMF_ERROR_NOT_SUPPORTED;
288                         break;
289         }
290         
291         if (err_code != NULL)
292                 *err_code = err;
293         
294         return ret;
295 }
296
297 #ifdef __FEATURE_AUTO_POLLING__
298 EXPORT_API int emf_auto_polling(int* err_code)
299 {
300         EM_DEBUG_FUNC_BEGIN();
301         
302         /*  default variable */
303         int ret = false, count = 0, i= 0;
304         int err = EMF_ERROR_NONE;
305         emf_mail_account_tbl_t* account_list = NULL;
306
307         /* get account list */
308         if (!em_storage_get_account_list(&count, &account_list, false, false, &err))  {
309                 EM_DEBUG_EXCEPTION("em_storage_get_account_list failed [%d]", err);             
310                 goto FINISH_OFF;
311         }
312
313         for (i = 0; i < count; i++)  {
314                 /* start auto poll */
315                 /* start auto polling, if check_interval not zero */
316                 if(account_list[i].check_interval > 0) {
317                         if(!emf_add_polling_alarm( account_list[i].account_id,account_list[i].check_interval))
318                                 EM_DEBUG_EXCEPTION("emf_add_polling_alarm[ NOTI_ACCOUNT_ADD] : start auto poll Failed >>> ");
319                 }
320         }
321
322         ret = true;
323 FINISH_OFF:  
324         if (account_list)
325                 em_storage_free_account(&account_list, count, NULL);
326         
327         if (err_code != NULL)
328                 *err_code = err;
329         
330         return ret;
331 }
332 #endif
333