4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 /******************************************************************************
26 * Desc: Mail Framework Initialization
31 * 2006.08.16 : created
32 *****************************************************************************/
37 #include <glib-object.h>
39 #include <dbus/dbus.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"
58 /* added for Disabling the Pthread flag log */
61 #include "emf-api-test.h"
64 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
65 #include <contacts-svc.h>
69 #define ENGINE_PATH_EM_CORE "libemail-core.so"
71 extern int g_client_count ;
72 extern int g_client_run;
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);
78 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
79 static void _emf_contact_change_cb(void *data)
81 EM_DEBUG_FUNC_BEGIN();
82 /* Run new thread and contact sync */
83 EM_DEBUG_LOG("Contact Changed.");
84 em_core_contact_sync_handler();
88 EXPORT_API int emf_init(int* err_code)
90 EM_DEBUG_FUNC_BEGIN();
92 /* default variable */
94 int err = EMF_ERROR_NONE;
96 if (g_client_count > 0) {
97 EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
102 EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
104 #ifdef __FEATURE_USE_PTHREAD__
105 dbus_threads_init_default();
106 #else /* __FEATURE_USE_PTHREAD__ */
107 if (!g_thread_supported())
109 dbus_g_thread_init ();
110 #endif /* __FEATURE_USE_PTHREAD__ */
115 em_storage_shm_file_init(SHM_FILE_FOR_DB_LOCK);
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__ */
122 if (!em_storage_open(&err)) {
123 EM_DEBUG_EXCEPTION("em_storage_open failed [%d]", err);
127 if (!em_storage_clean_save_status(EMF_MAIL_STATUS_SAVED, &err))
128 EM_DEBUG_EXCEPTION("em_storage_check_mail_status Failed [%d]", err );
132 if (!emf_init_account_reference()) {
133 EM_DEBUG_EXCEPTION("emf_init_account_reference fail...");
134 err = EMF_ERROR_DB_FAILURE;
137 EM_DEBUG_LOG("emf_init_account_reference over - g_client_count [%d]", g_client_count);
139 if (!_emf_load_engine(EMF_ENGINE_TYPE_EM_CORE, &err)) {
140 EM_DEBUG_EXCEPTION("_emf_load_engine failed [%d]", err);
142 /* err = EMF_ERROR_DB_FAILURE; */
146 em_core_check_unread_mail();
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);
159 EM_DEBUG_LOG(">>>>>>>>> ret value : %d g_client_count [%d]", ret, g_client_count);
161 emf_api_test_send_mail();
171 EXPORT_API int emf_close(int* err_code)
173 EM_DEBUG_FUNC_BEGIN();
175 /* default variable */
177 int err = EMF_ERROR_NONE;
179 if (g_client_count > 1) {
180 EM_DEBUG_EXCEPTION("engine is still used by application. decreased counter=[%d]", g_client_count);
184 /* err = EMF_ERROR_CLOSE_FAILURE; */
188 if (!_emf_unload_engine(EMF_ENGINE_TYPE_EM_CORE, &err)) {
189 EM_DEBUG_EXCEPTION("_emf_unload_engine failed [%d]", err);
193 /* free account reference list */
194 emf_free_account_reference();
197 if (!em_storage_close(&err)) {
198 EM_DEBUG_EXCEPTION("em_storage_close failed [%d]", err);
204 #ifdef _CONTACT_SUBSCRIBE_CHANGE_
205 contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, _emf_contact_change_cb);
208 #ifdef __FEATURE_AUTO_POLLING__
209 emf_free_account_alarm_binder_list();
222 static int _emf_load_engine(emf_engine_type_t type, int* err_code)
224 EM_DEBUG_FUNC_BEGIN();
226 int err = EMF_ERROR_NONE;
231 /* initialize mail core */
232 if (!em_core_init(&err))
235 if (em_core_event_loop_start(&err) < 0)
238 if (em_core_send_event_loop_start(&err) < 0)
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);
248 if ( em_core_open_contact_db_library() == false )
251 if (em_core_alert_loop_start(&err) < 0)
269 static int _emf_unload_engine(emf_engine_type_t type, int* err_code)
271 EM_DEBUG_FUNC_BEGIN();
274 int err = EMF_ERROR_NONE;
277 case EMF_ENGINE_TYPE_EM_CORE:
278 /* finish event loop */
279 em_core_event_loop_stop(&err);
281 em_core_close_contact_db_library();
287 err = EMF_ERROR_NOT_SUPPORTED;
291 if (err_code != NULL)
297 #ifdef __FEATURE_AUTO_POLLING__
298 EXPORT_API int emf_auto_polling(int* err_code)
300 EM_DEBUG_FUNC_BEGIN();
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;
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);
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 >>> ");
325 em_storage_free_account(&account_list, count, NULL);
327 if (err_code != NULL)