4 * Copyright (c) 2012 - 2013 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.
29 #include <contacts_internal.h>
31 #include "email-convert.h"
32 #include "email-storage.h"
33 #include "email-network.h"
34 #include "email-device.h"
35 #include "email-utilities.h"
36 #include "email-daemon-auto-poll.h"
37 #include "email-core-global.h"
38 #include "email-core-account.h"
39 #include "email-core-event.h"
40 #include "email-core-utils.h"
41 #include "email-core-mailbox.h"
42 #include "email-core-imap-mailbox.h"
43 #include "email-core-mail.h"
44 #include "email-core-mailbox-sync.h"
45 #include "email-core-smtp.h"
46 #include "email-core-utils.h"
47 #include "email-core-signal.h"
48 #include "email-debug-log.h"
51 /*-----------------------------------------------------------------------------
52 * Receving Event Queue
53 *---------------------------------------------------------------------------*/
54 INTERNAL_FUNC thread_t g_srv_thread;
55 INTERNAL_FUNC pthread_cond_t _event_available_signal = PTHREAD_COND_INITIALIZER;
56 INTERNAL_FUNC pthread_mutex_t *_event_queue_lock = NULL;
57 INTERNAL_FUNC pthread_mutex_t *_event_handle_map_lock = NULL;
59 INTERNAL_FUNC GQueue *g_event_que = NULL;
60 INTERNAL_FUNC int g_event_loop = 1;
61 INTERNAL_FUNC int handle_map[EVENT_QUEUE_MAX] = {0,};
62 INTERNAL_FUNC int recv_thread_run = 0;
64 static void fail_status_notify(email_event_t *event_data, int error);
65 static int emcore_get_new_handle(void);
66 static void emcore_adjust_sync_mail_flag_event_priority();
69 /*-----------------------------------------------------------------------------
71 *---------------------------------------------------------------------------*/
72 INTERNAL_FUNC thread_t g_send_srv_thread;
73 INTERNAL_FUNC pthread_cond_t _send_event_available_signal = PTHREAD_COND_INITIALIZER;
74 INTERNAL_FUNC pthread_mutex_t *_send_event_queue_lock = NULL;
75 INTERNAL_FUNC pthread_mutex_t *_send_event_handle_map_lock = NULL;
77 INTERNAL_FUNC GQueue *g_send_event_que = NULL;
78 INTERNAL_FUNC int g_send_event_loop = 1;
79 INTERNAL_FUNC int send_handle_map[EVENT_QUEUE_MAX] = {0,};
80 INTERNAL_FUNC int send_thread_run = 0;
82 static int emcore_get_new_send_handle(void);
84 /*-----------------------------------------------------------------------------
85 * Partial Body Event Queue
86 *---------------------------------------------------------------------------*/
88 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
90 INTERNAL_FUNC thread_t g_partial_body_thd ; /* Determines if thread is created or not. Non Null means thread is created */
91 INTERNAL_FUNC pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect event queue */
92 INTERNAL_FUNC pthread_cond_t _partial_body_thd_cond = PTHREAD_COND_INITIALIZER; /* Condition variable on which partial body thread is waiting */
93 INTERNAL_FUNC pthread_mutex_t _state_variables_lock;
95 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
96 INTERNAL_FUNC int g_partial_body_thd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
97 //INTERNAL_FUNC int g_partial_body_thd_loop = 1; /* Variable to make a continuos while loop */
98 INTERNAL_FUNC int g_partial_body_thd_queue_empty = true; /* Variable to determine if event queue is empty.True means empty*/
99 INTERNAL_FUNC int g_partial_body_thd_queue_full = false; /* Variable to determine if event queue is full. True means full*/
100 INTERNAL_FUNC int g_pb_thd_local_activity_continue = true; /* Variable to control local activity sync */
101 INTERNAL_FUNC int g_pbd_thd_state = false; /* false : thread is sleeping , true : thread is working */
103 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
104 static int g_partial_body_bulk_dwd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
105 INTERNAL_FUNC int g_partial_body_bulk_dwd_queue_empty = true;
107 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
108 static int emcore_clear_bulk_pbd_que(int *err_code);
110 INTERNAL_FUNC email_event_t *sync_failed_event_data = NULL;
114 /*-----------------------------------------------------------------------------
116 *---------------------------------------------------------------------------*/
117 typedef struct EVENT_CALLBACK_ELEM {
118 email_event_callback callback;
120 struct EVENT_CALLBACK_ELEM *next;
121 } EVENT_CALLBACK_LIST;
123 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM]; /* array of singly-linked list for event callbacks */
124 INTERNAL_FUNC pthread_mutex_t *_event_callback_table_lock = NULL;
126 #ifdef __FEATURE_LOCAL_ACTIVITY__
127 INTERNAL_FUNC int g_local_activity_run = 0;
128 INTERNAL_FUNC int g_save_local_activity_run = 0;
129 static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error);
132 #ifdef ENABLE_IMAP_IDLE_THREAD
133 extern int g_imap_idle_thread_alive;
134 extern int imap_idle_thread;
135 #endif /* ENABLE_IMAP_IDLE_THREAD */
137 static int is_gdk_lock_needed()
140 return (THREAD_SELF() == g_srv_thread);
145 static void fail_status_notify(email_event_t *event_data, int error)
147 EM_DEBUG_FUNC_BEGIN();
148 int account_id, mail_id;
151 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
154 account_id = event_data->account_id;
155 mail_id = event_data->event_param_data_4;
157 EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
159 switch (event_data->type) {
160 case EMAIL_EVENT_SEND_MAIL:
161 /* case EMAIL_EVENT_SEND_MAIL_SAVED: */
162 /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
163 emcore_show_user_message(event_data->multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, error);
166 case EMAIL_EVENT_SYNC_HEADER:
167 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
168 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SYNC_HEADER, error);
171 case EMAIL_EVENT_DOWNLOAD_BODY:
172 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
173 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
176 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
177 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
178 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
181 case EMAIL_EVENT_DELETE_MAIL:
182 case EMAIL_EVENT_DELETE_MAIL_ALL:
183 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
184 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, error);
187 case EMAIL_EVENT_VALIDATE_ACCOUNT:
188 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
189 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
192 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
193 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
194 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
197 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
198 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
199 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
202 case EMAIL_EVENT_CREATE_MAILBOX:
203 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
204 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
207 case EMAIL_EVENT_DELETE_MAILBOX:
208 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
209 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
212 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
213 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
214 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
217 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
218 emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
221 case EMAIL_EVENT_SEARCH_ON_SERVER:
222 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
223 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
226 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
227 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
228 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
231 case EMAIL_EVENT_UPDATE_MAIL:
232 emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
235 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
236 emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL, account_id, event_data->event_param_data_4, -1, EMAIL_ERROR_NONE);
246 INTERNAL_FUNC void emcore_initialize_event_callback_table()
248 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
252 for (i = 0; i < EMAIL_ACTION_NUM; i++)
253 _event_callback_table[i] = NULL;
255 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
258 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
260 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
262 if (callback == NULL)
267 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
269 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
271 while (node != NULL) {
272 if (node->callback == callback && node->event_data == event_data) /* already registered */
278 /* not found, so keep going */
280 node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
282 if (node == NULL) /* not enough memory */
285 node->callback = callback;
286 node->event_data = event_data;
287 node->next = _event_callback_table[action];
289 _event_callback_table[action] = node;
295 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
300 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
302 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
304 if (callback == NULL)
309 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
311 EVENT_CALLBACK_LIST *prev = NULL;
312 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
314 while (node != NULL) {
315 if (node->callback == callback) {
317 prev->next = node->next;
319 _event_callback_table[action] = node->next;
331 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
336 void emcore_execute_event_callback(email_action_t action, int total, int done, int status, int account_id, int mail_id, int handle, int error)
338 EM_DEBUG_FUNC_BEGIN("action[%d], total[%d], done[%d], status[%d], account_id[%d], mail_id[%d], handle[%d], error[%d]", action, total, done, status, account_id, mail_id, handle, error);
341 lock_needed = is_gdk_lock_needed();
344 /* Todo : g_thread_yield */
347 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
349 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
351 while (node != NULL) {
352 if (node->callback != NULL)
353 node->callback(total, done, status, account_id, mail_id, handle, node->event_data, error);
357 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
364 /* insert a event to event queue */
365 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
367 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
370 EM_DEBUG_EXCEPTION("Invalid Parameter");
371 if (err_code != NULL)
372 *err_code = EMAIL_ERROR_INVALID_PARAM;
377 EM_DEBUG_EXCEPTION("email-service is not ready");
378 if (err_code != NULL)
379 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
384 int error = EMAIL_ERROR_NONE;
387 char *multi_user_name = NULL;
389 multi_user_name = EM_SAFE_STRDUP(event_data->multi_user_name);
391 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
394 q_length = g_queue_get_length(g_event_que);
396 EM_DEBUG_LOG("Q Length : [%d]", q_length);
397 EM_DEBUG_LOG("Event type: [%d]", event_data->type);
399 if (q_length > EVENT_QUEUE_MAX) {
400 EM_DEBUG_EXCEPTION("event que is full...");
401 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
404 new_handle = emcore_get_new_handle();
407 event_data->status = EMAIL_EVENT_STATUS_WAIT;
408 event_data->handle = new_handle;
409 g_queue_push_tail(g_event_que, event_data);
410 emcore_adjust_sync_mail_flag_event_priority();
411 WAKE_CONDITION_VARIABLE(_event_available_signal);
414 EM_DEBUG_EXCEPTION("event queue is full...");
415 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
421 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
424 *handle = new_handle;
426 switch (event_data->type) {
427 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
428 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
429 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
430 case EMAIL_EVENT_VALIDATE_ACCOUNT:
431 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
432 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
433 case EMAIL_EVENT_SAVE_MAIL:
434 case EMAIL_EVENT_MOVE_MAIL:
435 case EMAIL_EVENT_DELETE_MAIL:
436 case EMAIL_EVENT_DELETE_MAIL_ALL:
437 case EMAIL_EVENT_CREATE_MAILBOX:
438 case EMAIL_EVENT_DELETE_MAILBOX:
439 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
440 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
441 case EMAIL_EVENT_UPDATE_MAIL:
442 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
443 case EMAIL_EVENT_SEARCH_ON_SERVER:
444 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
448 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
450 int is_local_activity_event_inserted = false;
451 emcore_partial_body_thd_local_activity_sync(multi_user_name,
452 &is_local_activity_event_inserted,
454 if (error != EMAIL_ERROR_NONE) {
455 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
457 if (true == is_local_activity_event_inserted)
458 emcore_pb_thd_set_local_activity_continue(false);
465 EM_SAFE_FREE(multi_user_name);
468 EM_DEBUG_LOG("ERR [%d]", error);
475 /* get a event from event_data queue */
476 INTERNAL_FUNC int emcore_retrieve_event(email_event_t **event_data, int *err_code)
478 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
481 int error = EMAIL_ERROR_NONE;
483 email_event_t *poped = NULL;
484 email_event_t *head_event = NULL;
487 q_length = g_queue_get_length(g_event_que);
489 EM_DEBUG_LOG("Q Length : [%d]", q_length);
492 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
493 EM_DEBUG_LOG("QUEUE is empty");
498 head_event = (email_event_t *)g_queue_peek_head(g_event_que);
500 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
501 EM_DEBUG_LOG_DEV("QUEUE is empty");
504 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
505 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
506 poped = g_queue_pop_head(g_event_que);
508 emcore_return_handle(poped->handle);
509 emcore_free_event(poped);
512 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
513 EM_DEBUG_LOG("QUEUE is empty");
517 head_event->status = EMAIL_EVENT_STATUS_STARTED;
518 EM_DEBUG_LOG("Event retrieved: [%d]", head_event->type);
519 *event_data = head_event;
527 if (err_code != NULL)
530 EM_DEBUG_FUNC_END("ret [%d]", ret);
534 /* get a event from event_data queue for download_attachment*/
535 INTERNAL_FUNC int emcore_retrieve_event_for_download_attachment(email_event_t **event_data, int *err_code)
537 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
540 int error = EMAIL_ERROR_NONE;
542 email_event_t *head_event = NULL;
545 q_length = g_queue_get_length(g_event_que);
547 EM_DEBUG_LOG("Q Length : [%d]", q_length);
550 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
551 EM_DEBUG_LOG("QUEUE is empty");
555 head_event = (email_event_t *)g_queue_peek_head(g_event_que);
557 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
558 EM_DEBUG_LOG_DEV("QUEUE is empty");
560 } else if (head_event->status != EMAIL_EVENT_STATUS_STARTED) {
561 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
562 error = EMAIL_ERROR_NO_MORE_DATA;
563 EM_DEBUG_LOG("get event data err");
566 *event_data = head_event;
574 if (err_code != NULL)
577 EM_DEBUG_FUNC_END("ret [%d]", ret);
584 /* check that event_data loop is continuous */
585 INTERNAL_FUNC int emcore_event_loop_continue(void)
590 INTERNAL_FUNC int emcore_is_event_queue_empty(void)
595 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
597 q_length = g_queue_get_length(g_event_que);
598 EM_DEBUG_LOG("Q Length : [%d]", q_length);
602 EM_DEBUG_LOG("event que is empty");
605 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
611 INTERNAL_FUNC int emcore_is_send_event_queue_empty(void)
616 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
617 if (g_send_event_que)
618 q_length = g_queue_get_length(g_send_event_que);
619 EM_DEBUG_LOG("Q Length : [%d]", q_length);
623 EM_DEBUG_LOG("send event que is empty");
626 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
632 INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data)
634 if (!sync_failed_event_data) {
635 EM_DEBUG_EXCEPTION("sync_failed_event_data is NULL");
639 email_event_t *new_event = NULL;
641 new_event = em_malloc(sizeof(email_event_t));
642 if (new_event == NULL) {
643 EM_DEBUG_EXCEPTION("em_mallocfailed");
647 new_event->account_id = sync_failed_event_data->account_id;
648 new_event->type = sync_failed_event_data->type;
649 if (sync_failed_event_data->event_param_data_3)
650 new_event->event_param_data_3 = EM_SAFE_STRDUP(sync_failed_event_data->event_param_data_3);
651 new_event->event_param_data_4 = sync_failed_event_data->event_param_data_4;
652 new_event->event_param_data_5 = sync_failed_event_data->event_param_data_5;
653 new_event->multi_user_name = EM_SAFE_STRDUP(sync_failed_event_data->multi_user_name);
655 *event_data = new_event;
657 emcore_free_event(sync_failed_event_data);
658 EM_SAFE_FREE(sync_failed_event_data);
661 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
663 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
666 EM_DEBUG_EXCEPTION("Invalid Parameter");
667 if (err_code != NULL)
668 *err_code = EMAIL_ERROR_INVALID_PARAM;
672 if (!g_send_srv_thread) {
673 EM_DEBUG_EXCEPTION("email-service is not ready");
674 if (err_code != NULL)
675 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
680 int error = EMAIL_ERROR_NONE;
684 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
686 if (g_send_event_que)
687 q_length = g_queue_get_length(g_send_event_que);
689 EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
690 EM_DEBUG_LOG("SEND Event type: [%d]", event_data->type);
692 if (q_length > EVENT_QUEUE_MAX) {
693 EM_DEBUG_EXCEPTION("send event que is full...");
694 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
697 new_handle = emcore_get_new_send_handle();
700 event_data->status = EMAIL_EVENT_STATUS_WAIT;
701 event_data->handle = new_handle;
702 g_queue_push_tail(g_send_event_que, event_data);
703 WAKE_CONDITION_VARIABLE(_send_event_available_signal);
708 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
711 *handle = new_handle;
713 if (err_code != NULL)
716 EM_DEBUG_LOG("Finish with [%d]", ret);
721 INTERNAL_FUNC int emcore_retrieve_send_event(email_event_t **event_data, int *err_code)
723 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
726 int error = EMAIL_ERROR_NONE;
728 email_event_t *poped = NULL;
729 email_event_t *head_event = NULL;
731 /* g_queue_get_length is aborted when param is null */
732 q_length = g_send_event_que ? g_queue_get_length(g_send_event_que) : 0; /*prevent 35141*/
734 EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
737 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
738 EM_DEBUG_LOG_DEV("SEND QUEUE is empty");
743 head_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
745 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
746 EM_DEBUG_LOG("SEND QUEUE is empty");
749 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
750 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
751 poped = g_queue_pop_head(g_send_event_que);
753 emcore_return_send_handle(poped->handle);
754 emcore_free_event(poped);
757 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
758 EM_DEBUG_LOG("SEND QUEUE is empty");
762 head_event->status = EMAIL_EVENT_STATUS_STARTED;
763 EM_DEBUG_LOG("SEND Event retrieved: [%d]", head_event->type);
764 *event_data = head_event;
772 if (err_code != NULL)
775 EM_DEBUG_FUNC_END("ret [%d]", ret);
779 /* finish api event_data loop */
780 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
782 EM_DEBUG_FUNC_BEGIN();
784 if (err_code != NULL)
785 *err_code = EMAIL_ERROR_NONE;
787 if (!g_send_srv_thread) {
788 if (err_code != NULL)
789 *err_code = EMAIL_ERROR_UNKNOWN;
793 /* stop event_data loop */
794 g_send_event_loop = 0;
796 emcore_cancel_all_send_mail_thread(err_code);
798 WAKE_CONDITION_VARIABLE(_send_event_available_signal); /* MUST BE HERE */
800 /* wait for thread finished */
801 THREAD_JOIN(g_send_srv_thread);
803 g_queue_free(g_send_event_que);
805 g_send_srv_thread = 0;
807 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
808 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
809 DELETE_CONDITION_VARIABLE(_send_event_available_signal);
811 if (err_code != NULL)
812 *err_code = EMAIL_ERROR_NONE;
817 /* finish api event_data loop */
818 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
820 EM_DEBUG_FUNC_BEGIN();
822 if (err_code != NULL)
823 *err_code = EMAIL_ERROR_NONE;
826 if (err_code != NULL)
827 *err_code = EMAIL_ERROR_UNKNOWN;
831 /* stop event_data loop */
834 /* pthread_kill(g_srv_thread, SIGINT); */
835 emcore_cancel_all_thread(err_code);
837 WAKE_CONDITION_VARIABLE(_event_available_signal);
839 /* wait for thread finished */
840 THREAD_JOIN(g_srv_thread);
842 g_queue_free(g_event_que);
846 DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
847 DELETE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
848 DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
849 DELETE_CONDITION_VARIABLE(_event_available_signal);
851 if (err_code != NULL)
852 *err_code = EMAIL_ERROR_NONE;
857 /* check event thread status (worker_event_queue)
858 * 0 : stop job 1 : continue job
859 event list handled by event thread :
860 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
861 case EMAIL_EVENT_SYNC_HEADER:
862 case EMAIL_EVENT_SYNC_HEADER_OMA:
863 case EMAIL_EVENT_DOWNLOAD_BODY:
864 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
865 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
866 case EMAIL_EVENT_DELETE_MAIL:
867 case EMAIL_EVENT_DELETE_MAIL_ALL:
868 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
869 case EMAIL_EVENT_CREATE_MAILBOX:
870 case EMAIL_EVENT_DELETE_MAILBOX:
871 case EMAIL_EVENT_SAVE_MAIL:
872 case EMAIL_EVENT_MOVE_MAIL:
873 case EMAIL_EVENT_VALIDATE_ACCOUNT:
874 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
875 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
876 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
877 case EMAIL_EVENT_UPDATE_MAIL:
878 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
879 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
880 case EMAIL_EVENT_LOCAL_ACTIVITY:
881 case EMAIL_EVENT_SEARCH_ON_SERVER:
882 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
883 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
885 INTERNAL_FUNC int emcore_check_event_thread_status(int *event_type, int handle)
887 EM_DEBUG_FUNC_BEGIN();
891 email_event_t *active_event = NULL;
893 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
896 q_length = g_queue_get_length(g_event_que);
899 active_event = (email_event_t *)g_queue_peek_head(g_event_que);
901 if (active_event->handle == handle) {
903 *event_type = active_event->type;
904 if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
913 EM_DEBUG_LOG("Rcv Queue is empty [%d]", q_length);
917 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
918 EM_DEBUG_FUNC_END("ret [%d]", ret);
923 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
925 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
928 int err = EMAIL_ERROR_NONE;
932 email_event_t *found_elm = NULL;
933 email_event_t *pop_elm = NULL;
935 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
938 q_length = g_queue_get_length(g_event_que);
940 for (i = 0; i < q_length; i++) {
941 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
942 if (found_elm && found_elm->handle == handle) {
943 EM_DEBUG_LOG("Found Queue element[%d] with handle[%d]", i, handle);
950 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
951 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
953 switch (found_elm->type) {
955 case EMAIL_EVENT_SEND_MAIL:
956 case EMAIL_EVENT_SEND_MAIL_SAVED:
957 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
958 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
959 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
962 case EMAIL_EVENT_DOWNLOAD_BODY:
963 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
964 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
965 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_BODY_CANCEL] Failed >>>>");
968 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
969 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
970 if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
971 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
974 case EMAIL_EVENT_SYNC_HEADER:
975 case EMAIL_EVENT_SYNC_HEADER_OMA:
976 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
977 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
978 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
981 case EMAIL_EVENT_VALIDATE_ACCOUNT:
982 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
983 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
984 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
987 case EMAIL_EVENT_DELETE_MAIL:
988 case EMAIL_EVENT_DELETE_MAIL_ALL:
989 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
990 case EMAIL_EVENT_SAVE_MAIL:
991 case EMAIL_EVENT_MOVE_MAIL:
992 case EMAIL_EVENT_CREATE_MAILBOX:
993 case EMAIL_EVENT_DELETE_MAILBOX:
994 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
995 case EMAIL_EVENT_SEARCH_ON_SERVER:
996 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
997 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1004 pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1006 emcore_return_handle(pop_elm->handle);
1007 emcore_free_event(pop_elm);
1008 EM_SAFE_FREE(pop_elm);
1010 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1013 switch (found_elm->type) {
1014 case EMAIL_EVENT_SYNC_HEADER:
1015 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER");
1016 if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
1017 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1018 if ((err = emcore_update_sync_status_of_account(found_elm->multi_user_name, found_elm->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1019 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1022 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1023 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1024 if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
1025 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
1031 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1037 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1039 if (err_code != NULL)
1041 EM_DEBUG_FUNC_END();
1045 /* cancel all job */
1046 INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
1048 EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1051 int err = EMAIL_ERROR_NONE;
1054 email_event_t *pop_elm = NULL;
1056 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1058 /* g_queue_get_length is aborted when param is null */
1059 q_length = g_event_que ? g_queue_get_length(g_event_que) : 0; /*prevent 35142 */
1061 for (i = 0; i < q_length; i++) {
1062 pop_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1064 if (pop_elm && (pop_elm->status == EMAIL_EVENT_STATUS_WAIT)) {
1066 fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1068 switch (pop_elm->type) {
1070 case EMAIL_EVENT_SEND_MAIL:
1071 case EMAIL_EVENT_SEND_MAIL_SAVED:
1072 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1073 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1074 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1077 case EMAIL_EVENT_DOWNLOAD_BODY:
1078 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1079 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1080 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1083 case EMAIL_EVENT_SYNC_HEADER:
1084 case EMAIL_EVENT_SYNC_HEADER_OMA:
1085 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1086 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1087 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1088 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1091 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1092 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", pop_elm->account_id);
1093 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1094 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1097 case EMAIL_EVENT_DELETE_MAIL:
1098 case EMAIL_EVENT_DELETE_MAIL_ALL:
1099 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1100 case EMAIL_EVENT_SAVE_MAIL:
1101 case EMAIL_EVENT_MOVE_MAIL:
1102 case EMAIL_EVENT_CREATE_MAILBOX:
1103 case EMAIL_EVENT_DELETE_MAILBOX:
1104 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1105 case EMAIL_EVENT_SEARCH_ON_SERVER:
1106 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1107 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1114 pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1116 emcore_return_handle(pop_elm->handle);
1117 emcore_free_event(pop_elm);
1118 EM_SAFE_FREE(pop_elm);
1122 q_length = g_queue_get_length(g_event_que);
1125 if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1128 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1132 if (err_code != NULL)
1134 EM_DEBUG_FUNC_END();
1138 /* check thread status
1139 * 0 : stop job 1 : continue job
1141 INTERNAL_FUNC int emcore_check_send_mail_thread_status(void)
1143 EM_DEBUG_FUNC_BEGIN();
1147 email_event_t *active_event = NULL;
1149 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1151 if (g_send_event_que)
1152 q_length = g_queue_get_length(g_send_event_que);
1155 active_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
1157 if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
1163 EM_DEBUG_LOG("Send Queue is empty [%d]", q_length);
1167 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1168 EM_DEBUG_FUNC_END("ret [%d]", ret);
1172 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(char *multi_user_name, int account_id)
1174 EM_DEBUG_FUNC_BEGIN();
1175 int error_code = EMAIL_ERROR_NONE;
1178 email_event_t *found_elm = NULL;
1179 email_event_t *pop_elm = NULL;
1181 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1184 q_length = g_queue_get_length(g_event_que);
1186 for (i = 0; i < q_length; i++) {
1187 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1188 if (found_elm && (found_elm->account_id == account_id || found_elm->account_id == ALL_ACCOUNT) &&
1189 ((!found_elm->multi_user_name && !multi_user_name) || (
1190 (multi_user_name) && !EM_SAFE_STRCASECMP(found_elm->multi_user_name, multi_user_name)))) {
1191 EM_DEBUG_LOG("Found Queue element[%d]", i);
1193 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1194 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1196 switch (found_elm->type) {
1198 case EMAIL_EVENT_SEND_MAIL:
1199 case EMAIL_EVENT_SEND_MAIL_SAVED:
1200 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1201 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL ,
1202 found_elm->event_param_data_4, error_code))
1203 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1206 case EMAIL_EVENT_DOWNLOAD_BODY:
1207 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1208 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL,
1209 found_elm->event_param_data_4, error_code))
1210 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1213 case EMAIL_EVENT_SYNC_HEADER:
1214 case EMAIL_EVENT_SYNC_HEADER_OMA:
1215 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1216 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1217 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1218 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1221 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1222 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
1223 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, error_code))
1224 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1227 case EMAIL_EVENT_DELETE_MAIL:
1228 case EMAIL_EVENT_DELETE_MAIL_ALL:
1229 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1230 case EMAIL_EVENT_SAVE_MAIL:
1231 case EMAIL_EVENT_MOVE_MAIL:
1232 case EMAIL_EVENT_CREATE_MAILBOX:
1233 case EMAIL_EVENT_DELETE_MAILBOX:
1234 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1235 case EMAIL_EVENT_SEARCH_ON_SERVER:
1236 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1237 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1244 pop_elm = g_queue_pop_nth(g_event_que, i);
1246 emcore_return_handle(pop_elm->handle);
1247 emcore_free_event(pop_elm);
1248 EM_SAFE_FREE(pop_elm);
1251 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1254 q_length = g_queue_get_length(g_event_que);
1257 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1262 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1264 EM_DEBUG_FUNC_END();
1269 /* cancel send mail job */
1270 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
1272 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
1275 int err = EMAIL_ERROR_NONE;
1279 email_event_t *found_elm = NULL;
1280 email_event_t *pop_elm = NULL;
1282 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1284 if (g_send_event_que)
1285 q_length = g_queue_get_length(g_send_event_que);
1287 for (i = 0; i < q_length; i++) {
1288 found_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1289 if (found_elm && (found_elm->handle == handle)) {
1290 EM_DEBUG_LOG("Found Send Queue element[%d] with handle[%d]", i, handle);
1297 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1298 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1300 switch (found_elm->type) {
1301 case EMAIL_EVENT_SEND_MAIL:
1302 case EMAIL_EVENT_SEND_MAIL_SAVED:
1303 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1304 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL, found_elm->event_param_data_4, err))
1305 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1311 pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1313 emcore_return_send_handle(pop_elm->handle);
1314 emcore_free_event(pop_elm);
1315 EM_SAFE_FREE(pop_elm);
1317 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1320 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1326 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1328 if (err_code != NULL)
1330 EM_DEBUG_FUNC_END();
1334 INTERNAL_FUNC int emcore_cancel_all_send_mail_thread(int *err_code)
1336 EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1339 int err = EMAIL_ERROR_NONE;
1342 email_event_t *pop_elm = NULL;
1344 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1346 if (g_send_event_que)
1347 q_length = g_queue_get_length(g_send_event_que);
1349 for (i = 0; i < q_length; i++) {
1350 pop_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1352 if (pop_elm && pop_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1354 fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1356 switch (pop_elm->type) {
1357 case EMAIL_EVENT_SEND_MAIL:
1358 case EMAIL_EVENT_SEND_MAIL_SAVED:
1359 pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1360 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL, pop_elm->event_param_data_4, err))
1361 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1367 pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1369 emcore_return_send_handle(pop_elm->handle);
1370 emcore_free_event(pop_elm);
1371 EM_SAFE_FREE(pop_elm);
1375 if (g_send_event_que)
1376 q_length = g_queue_get_length(g_send_event_que);
1379 if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1382 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1386 if (err_code != NULL)
1388 EM_DEBUG_FUNC_END();
1392 INTERNAL_FUNC int emcore_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count)
1394 EM_DEBUG_FUNC_BEGIN("output_task_information[%p] output_task_information_count[%p]", output_task_information, output_task_information_count);
1395 int err = EMAIL_ERROR_NONE;
1399 email_task_information_t *task_information = NULL;
1400 email_event_t *elm = NULL;
1402 if (output_task_information == NULL || output_task_information_count == NULL) {
1403 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1404 err = EMAIL_ERROR_INVALID_PARAM;
1409 q_length = g_queue_get_length(g_event_que);
1412 err = EMAIL_ERROR_DATA_NOT_FOUND;
1416 if ((task_information = em_malloc(sizeof(email_task_information_t) * q_length)) == NULL) {
1417 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1418 err = EMAIL_ERROR_OUT_OF_MEMORY;
1422 for (i = 0; i < q_length; i++) {
1423 elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1425 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
1426 err = EMAIL_ERROR_DATA_NOT_FOUND;
1429 if(elm->type != EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1430 if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1431 task_information[index].handle = elm->handle;
1432 task_information[index].account_id = elm->account_id;
1433 task_information[index].type = elm->type;
1434 task_information[index].status = elm->status;
1435 task_information[index].task_data1 = 0;
1436 task_information[index].task_data2 = 0;
1437 task_information[index].task_data3 = 0;
1441 }else if(elm->type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1442 if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1444 task_information[index].task_data1 = (void *)elm->event_param_data_4; /* mail_id */
1445 task_information[index].task_data2 = (void *)elm->event_param_data_5; /* attachment_nth */
1446 task_information[index].task_data3 = (void *)elm->event_param_data_8; /* download progress */
1448 task_information[index].handle = elm->handle;
1449 task_information[index].account_id = elm->account_id;
1450 task_information[index].type = elm->type;
1451 task_information[index].status = elm->status;
1459 *output_task_information_count = q_length;
1460 *output_task_information = task_information;
1464 EM_DEBUG_FUNC_END("err [%d]", err);
1468 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
1470 EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
1472 return EMAIL_ERROR_INVALID_PARAM;
1474 switch (event_data->type) {
1475 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1476 EM_SAFE_FREE(event_data->event_param_data_3);
1479 case EMAIL_EVENT_SYNC_HEADER: /* synchronize mail header */
1482 case EMAIL_EVENT_SYNC_HEADER_OMA: /* synchronize mail header for OMA */
1483 EM_SAFE_FREE(event_data->event_param_data_1);
1486 case EMAIL_EVENT_DOWNLOAD_BODY: /* download mail body */
1489 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT: /* download attachment */
1492 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER: /* Sync flags field */
1493 EM_SAFE_FREE(event_data->event_param_data_3);
1496 case EMAIL_EVENT_DELETE_MAIL: /* delete mails */
1497 EM_SAFE_FREE(event_data->event_param_data_3);
1500 case EMAIL_EVENT_DELETE_MAIL_ALL: /* delete all mails */
1503 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1504 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1508 case EMAIL_EVENT_CREATE_MAILBOX:
1509 emcore_free_mailbox((email_mailbox_t*)event_data->event_param_data_1);
1510 EM_SAFE_FREE(event_data->event_param_data_1);
1513 case EMAIL_EVENT_DELETE_MAILBOX:
1516 case EMAIL_EVENT_SAVE_MAIL:
1519 case EMAIL_EVENT_MOVE_MAIL:
1520 EM_SAFE_FREE(event_data->event_param_data_3);
1523 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1526 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
1527 emcore_free_account((email_account_t *)event_data->event_param_data_1);
1528 EM_SAFE_FREE(event_data->event_param_data_1);
1532 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
1533 emcore_free_account((email_account_t *)event_data->event_param_data_1);
1534 EM_SAFE_FREE(event_data->event_param_data_1);
1537 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
1538 emcore_free_account((email_account_t *)event_data->event_param_data_1);
1539 EM_SAFE_FREE(event_data->event_param_data_1);
1542 case EMAIL_EVENT_UPDATE_MAIL: {
1543 email_mail_data_t* input_mail_data = (email_mail_data_t*) event_data->event_param_data_1;
1544 email_attachment_data_t* input_attachment_data_list = (email_attachment_data_t*) event_data->event_param_data_2;
1545 email_meeting_request_t* input_meeting_request = (email_meeting_request_t*) event_data->event_param_data_3;
1546 int input_attachment_count = event_data->event_param_data_4;
1547 emcore_free_mail_data_list(&input_mail_data, 1);
1548 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1549 emstorage_free_meeting_request(input_meeting_request);
1553 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1556 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
1559 #ifdef __FEATURE_LOCAL_ACTIVITY__
1560 case EMAIL_EVENT_LOCAL_ACTIVITY:
1562 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
1564 case EMAIL_EVENT_SEARCH_ON_SERVER: {
1566 int search_filter_count = event_data->event_param_data_5;
1567 email_search_filter_t *search_filter = (email_search_filter_t *)event_data->event_param_data_1;
1569 if (event_data->event_param_data_1) {
1570 for (i = 0; i < search_filter_count; i++) {
1571 switch (search_filter[i].search_filter_type) {
1572 case EMAIL_SEARCH_FILTER_TYPE_BCC:
1573 case EMAIL_SEARCH_FILTER_TYPE_BODY:
1574 case EMAIL_SEARCH_FILTER_TYPE_CC:
1575 case EMAIL_SEARCH_FILTER_TYPE_FROM:
1576 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD:
1577 case EMAIL_SEARCH_FILTER_TYPE_TEXT:
1578 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT:
1579 case EMAIL_SEARCH_FILTER_TYPE_TO:
1580 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID:
1581 case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME:
1582 case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
1583 case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED:
1584 EM_SAFE_FREE(search_filter[i].search_filter_key_value.string_type_key_value);
1592 EM_SAFE_FREE(event_data->event_param_data_1);
1595 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1596 EM_SAFE_FREE(event_data->event_param_data_1);
1597 EM_SAFE_FREE(event_data->event_param_data_2);
1598 EM_SAFE_FREE(event_data->event_param_data_3);
1602 case EMAIL_EVENT_SEND_MAIL:
1605 case EMAIL_EVENT_SEND_MAIL_SAVED:
1606 EM_SAFE_FREE(event_data->event_param_data_3);
1609 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
1612 #ifdef __FEATURE_LOCAL_ACTIVITY__
1613 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1615 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1617 default: /*free function is not implemented*/
1618 EM_DEBUG_EXCEPTION("event %d is NOT freed, possibly memory leaks", event_data->type);
1621 EM_SAFE_FREE(event_data->multi_user_name);
1622 event_data->event_param_data_1 = event_data->event_param_data_2 = event_data->event_param_data_3 = NULL;
1624 EM_DEBUG_FUNC_END();
1628 static int emcore_get_new_handle(void)
1630 EM_DEBUG_FUNC_BEGIN();
1634 ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1635 for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1636 if (handle_map[i] == 0) {
1641 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1643 if (i < EVENT_QUEUE_MAX) {
1645 EM_DEBUG_LOG("New handle [%d]", ret_handle);
1648 EM_DEBUG_EXCEPTION("there is no available handle");
1651 EM_DEBUG_FUNC_END();
1656 static int emcore_get_new_send_handle(void)
1658 EM_DEBUG_FUNC_BEGIN();
1662 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1663 for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1664 if (send_handle_map[i] == 0) {
1665 send_handle_map[i] = 1;
1669 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1671 if (i < EVENT_QUEUE_MAX) {
1673 EM_DEBUG_LOG("New send handle [%d]", ret_handle);
1676 EM_DEBUG_LOG("there is no available send handle");
1679 EM_DEBUG_FUNC_END();
1686 static void emcore_adjust_sync_mail_flag_event_priority()
1688 EM_DEBUG_FUNC_BEGIN();
1689 GList *sync_flag_event_list = NULL;
1690 int q_length = g_queue_get_length(g_event_que);
1692 int is_sync_mail_flag_event = 0;
1694 // find sync flag event.
1695 for (i = 0; i < q_length; i++) {
1696 email_event_t *p = g_queue_peek_nth(g_event_que, i);
1697 if (p->type == EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER) {
1698 sync_flag_event_list = g_list_append(sync_flag_event_list, p);
1702 // re-arrange sync flag events to 2nd head of event queue.
1703 // first element (working element) of event queue should be kept.
1705 GList *cur = g_list_last(sync_flag_event_list);
1707 email_event_t *p = (email_event_t*)(cur->data);
1708 g_queue_remove(g_event_que, p);
1709 g_queue_push_nth(g_event_que, p, nth++);
1710 int *mail_ids = (int*)(p->event_param_data_3);
1712 EM_DEBUG_LOG("Sync flag event: change the position of queue - mail_id: [%d] - to [%d] th", mail_ids[0], nth - 1);
1713 is_sync_mail_flag_event = 1;
1714 cur = g_list_previous(cur);
1717 email_event_t *first = g_queue_peek_nth(g_event_que, 0);
1718 if (is_sync_mail_flag_event && first->type == EMAIL_EVENT_SYNC_HEADER) {
1719 // copy sync event and push to event queue
1720 email_event_t *sync_event = em_malloc(sizeof(email_event_t));
1721 memcpy(sync_event, first, sizeof(email_event_t));
1722 sync_event->multi_user_name = g_strdup(first->multi_user_name);
1723 sync_event->event_param_data_1= g_strdup(first->event_param_data_1);
1724 sync_event->event_param_data_2= g_strdup(first->event_param_data_2);
1725 sync_event->event_param_data_3= g_strdup(first->event_param_data_3);
1726 sync_event->status = EMAIL_EVENT_STATUS_WAIT;
1727 g_queue_push_nth(g_event_que, sync_event, nth);
1728 EM_DEBUG_LOG("Sync header event: change the position of queue - account_id: [%d] - to [%d] th", sync_event->event_param_data_5, nth);
1730 // cancel sync header event
1731 int err = EMAIL_ERROR_NONE;
1732 EM_DEBUG_LOG("Sync header event: change the status: from [%d] - to [%d]", first->status, EMAIL_EVENT_STATUS_CANCELED);
1733 first->status = EMAIL_EVENT_STATUS_CANCELED;
1734 if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, first->account_id, NULL , first->event_param_data_4, err))
1735 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1736 if ((err = emcore_update_sync_status_of_account(first->multi_user_name, first->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1737 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1739 EM_DEBUG_FUNC_END();
1742 INTERNAL_FUNC int emcore_return_handle(int handle)
1744 EM_DEBUG_FUNC_BEGIN();
1747 ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1748 if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1749 handle_map[handle-1] = 0;
1751 EM_DEBUG_LOG("handle [%d] is returned", handle);
1753 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1755 EM_DEBUG_FUNC_END();
1760 INTERNAL_FUNC int emcore_return_send_handle(int handle)
1762 EM_DEBUG_FUNC_BEGIN();
1765 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1766 if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1767 send_handle_map[handle-1] = 0;
1769 EM_DEBUG_LOG("send handle [%d] is returned", handle);
1771 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1773 EM_DEBUG_FUNC_END();
1778 #ifdef __FEATURE_KEEP_CONNECTION__
1779 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
1781 return (unsigned int)g_srv_thread;
1783 #endif /* __FEATURE_KEEP_CONNECTION__ */
1785 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1787 INTERNAL_FUNC int emcore_get_pbd_thd_state()
1789 int pbd_thd_state = false;
1790 ENTER_CRITICAL_SECTION(_state_variables_lock);
1791 pbd_thd_state = g_pbd_thd_state;
1792 LEAVE_CRITICAL_SECTION(_state_variables_lock);
1793 return pbd_thd_state;
1796 INTERNAL_FUNC int emcore_set_pbd_thd_state(int flag)
1798 ENTER_CRITICAL_SECTION(_state_variables_lock);
1799 g_pbd_thd_state = flag;
1800 LEAVE_CRITICAL_SECTION(_state_variables_lock);
1802 return g_pbd_thd_state;
1805 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
1807 EM_DEBUG_FUNC_BEGIN();
1808 EM_DEBUG_FUNC_END();
1809 return (unsigned int)g_partial_body_thd;
1812 static int emcore_clear_bulk_pbd_que(int *err_code)
1814 EM_DEBUG_FUNC_BEGIN();
1817 int error = EMAIL_ERROR_NONE;
1820 for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
1821 if (g_partial_body_bulk_dwd_que[i].event_type) {
1822 if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error)) {
1823 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1830 if (NULL != err_code)
1832 EM_DEBUG_FUNC_END();
1836 INTERNAL_FUNC void emcore_pb_thd_set_local_activity_continue(int flag)
1838 EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
1840 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1842 g_pb_thd_local_activity_continue = flag;
1845 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
1848 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1849 EM_DEBUG_FUNC_END();
1852 INTERNAL_FUNC int emcore_pb_thd_can_local_activity_continue()
1854 EM_DEBUG_FUNC_BEGIN();
1858 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1860 ret = g_pb_thd_local_activity_continue;
1862 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1863 EM_DEBUG_FUNC_END();
1868 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
1870 EM_DEBUG_FUNC_BEGIN();
1873 int error = EMAIL_ERROR_NONE;
1876 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1878 if (true == g_partial_body_thd_queue_empty) {
1879 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
1881 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
1882 if (g_partial_body_thd_event_que[i].event_type) {
1883 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error)) {
1884 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1891 g_partial_body_thd_queue_empty = true;
1892 g_partial_body_thd_queue_full = false;
1894 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1896 if (NULL != err_code)
1898 EM_DEBUG_FUNC_END();
1902 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
1904 EM_DEBUG_FUNC_BEGIN();
1908 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1910 ret = g_partial_body_thd_queue_empty;
1912 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1913 EM_DEBUG_FUNC_END("ret [%d]", ret);
1916 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
1918 EM_DEBUG_FUNC_BEGIN();
1922 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1924 ret = g_partial_body_thd_queue_full;
1926 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1927 EM_DEBUG_FUNC_END();
1932 Himanshu[h.gahalut] : If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
1933 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
1934 through _partial_body_thd_event_queue_lock mutex.
1936 No mutex is used inside this API so that we can also use it to copy partial body events which are not a part of global event_data queue
1940 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1941 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1945 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
1947 EM_DEBUG_FUNC_BEGIN();
1948 int error = EMAIL_ERROR_NONE;
1951 if (NULL == src || NULL == dest) {
1952 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
1953 error = EMAIL_ERROR_INVALID_PARAM;
1957 dest->account_id = src->account_id;
1958 dest->mail_id = src->mail_id;
1959 dest->server_mail_id = src->server_mail_id;
1960 dest->activity_id = src->activity_id;
1961 dest->mailbox_id = src->mailbox_id;
1962 dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
1963 dest->activity_type = src->activity_type;
1964 dest->event_type = src->event_type;
1965 dest->multi_user_name = EM_SAFE_STRDUP(src->multi_user_name);
1972 if (NULL != error_code)
1973 *error_code = error;
1980 Himanshu[h.gahalut] : If emcore_free_partial_body_thd_event_cell API is used to free a cell of partial body thread event_data queue,
1981 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
1983 No mutex is used inside this API so that we can also use it to free partial body events which are not a part of global event_data queue
1987 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1988 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1992 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
1994 EM_DEBUG_FUNC_BEGIN();
1996 if (NULL == partial_body_thd_event) {
1997 if (error_code != NULL) {
1998 *error_code = EMAIL_ERROR_INVALID_PARAM;
2003 email_event_partial_body_thd *pbd_event = partial_body_thd_event;
2005 /*Free character pointers in event_data cell */
2006 EM_SAFE_FREE(pbd_event->mailbox_name);
2007 EM_SAFE_FREE(pbd_event->multi_user_name);
2008 memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
2009 EM_DEBUG_FUNC_END();
2013 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(
2014 email_event_partial_body_thd *partial_body_thd_event,
2017 EM_DEBUG_FUNC_BEGIN();
2019 if (NULL == partial_body_thd_event) {
2020 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
2022 if (error_code != NULL) {
2023 *error_code = EMAIL_ERROR_INVALID_PARAM;
2029 int error = EMAIL_ERROR_NONE;
2030 int empty_cell_index = -1;
2034 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2036 /* find a cell in queue which is empty */
2037 index = g_partial_body_thd_next_event_idx;
2038 for (count = 0; count < TOTAL_PARTIAL_BODY_EVENTS; count++) {
2039 /*Found empty Cell*/
2040 if (g_partial_body_thd_event_que[index].event_type == EMAIL_EVENT_NONE) {
2041 empty_cell_index = index;
2045 index = index % TOTAL_PARTIAL_BODY_EVENTS;
2048 if (empty_cell_index != -1) {
2049 emcore_copy_partial_body_thd_event(partial_body_thd_event,
2050 g_partial_body_thd_event_que + empty_cell_index,
2052 if (error != EMAIL_ERROR_NONE) {
2053 EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
2055 g_partial_body_thd_queue_empty = false;
2056 if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
2057 /*This is the last event_data inserted in queue after its insertion, queue is full */
2058 g_partial_body_thd_queue_full = true;
2060 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2064 EM_DEBUG_LOG("partial body thread event_data queue is full ");
2065 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
2067 g_partial_body_thd_queue_full = true;
2068 g_partial_body_thd_queue_empty = false;
2071 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2073 if (NULL != error_code) {
2074 *error_code = error;
2081 /* h.gahlaut : Return true only if event_data is retrieved successfully */
2083 INTERNAL_FUNC int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
2085 EM_DEBUG_FUNC_BEGIN();
2088 int error = EMAIL_ERROR_NONE;
2091 /* Lock Mutex to protect event_data queue and associated global variables variables*/
2093 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2095 index = g_partial_body_thd_next_event_idx;
2097 if (0 == g_partial_body_thd_event_que[index].event_type) {
2098 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
2099 g_partial_body_thd_queue_empty = true;
2100 g_partial_body_thd_queue_full = false;
2102 /*Copy the event_data from queue to return it and free the event_data in queue */
2103 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
2104 EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
2106 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
2107 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2109 g_partial_body_thd_queue_full = false;
2110 g_partial_body_thd_next_event_idx = ++index;
2112 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
2113 g_partial_body_thd_next_event_idx = 0;
2115 /* If the event_data retrieved was the only event_data present in queue,
2116 we need to set g_partial_body_thd_queue_empty to true
2119 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
2120 g_partial_body_thd_queue_empty = true;
2130 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2133 *error_code = error;
2139 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
2140 static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code)
2142 EM_DEBUG_FUNC_BEGIN();
2143 int error = EMAIL_ERROR_NONE;
2145 MAILSTREAM *stream = NULL;
2146 void *tmp_stream = NULL;
2148 if (!emcore_connect_to_remote_mailbox(multi_user_name,
2149 g_partial_body_bulk_dwd_que[0].account_id,
2150 g_partial_body_bulk_dwd_que[0].mailbox_id,
2152 (void **)&tmp_stream,
2153 &error) || (NULL == tmp_stream)) {
2154 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2157 stream = (MAILSTREAM *)tmp_stream;
2159 /* Call bulk download here */
2160 if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
2161 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2168 stream = mail_close(stream);
2170 g_partial_body_bulk_dwd_next_event_idx = 0;
2171 g_partial_body_bulk_dwd_queue_empty = true;
2173 if (false == emcore_clear_bulk_pbd_que(&error))
2174 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
2176 if (NULL != error_code)
2177 *error_code = error;
2179 EM_DEBUG_FUNC_END("ret [%d]", ret);
2184 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
2185 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
2187 EM_DEBUG_FUNC_BEGIN();
2188 int error = EMAIL_ERROR_NONE;
2192 MAILSTREAM *stream = NULL;
2193 email_event_partial_body_thd *activity_data_list = NULL;
2194 int *mailbox_list = NULL;
2195 int *account_list = NULL;
2197 if (NULL == pbd_event) {
2198 EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
2200 error = EMAIL_ERROR_INVALID_PARAM;
2204 /*Check if the event_data is to flush the event_data que array */
2205 if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
2206 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
2207 /*Check if the mailbox name and account id for this event_data is same as the mailbox name and account id for earlier events saved in download que array
2208 then append this event_data also to download que array */
2209 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
2210 (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
2211 EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
2212 EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
2213 if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
2214 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2215 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2220 EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
2221 EM_DEBUG_LOG("Flush the current que if not empty");
2222 EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
2223 if (!g_partial_body_bulk_dwd_queue_empty) {
2224 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2225 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2230 /*Add the event_data to the download que array */
2231 if (false == emcore_copy_partial_body_thd_event(pbd_event,
2232 g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
2233 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
2235 g_partial_body_bulk_dwd_queue_empty = false;
2236 g_partial_body_bulk_dwd_next_event_idx++;
2237 EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
2239 } else if (pbd_event->activity_type) {
2240 int account_count = 0;
2242 EM_DEBUG_LOG("Event is coming from local activity.");
2243 /* Get all the accounts for which local activities are pending */
2244 if (false == emstorage_get_pbd_account_list(pbd_event->multi_user_name, &account_list, &account_count, false, &error)) {
2245 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2246 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2250 for (m = 0; m < account_count; ++m) {
2251 /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
2252 if (false == emstorage_get_pbd_mailbox_list(pbd_event->multi_user_name, account_list[m], &mailbox_list, &count, false, &error)) {
2253 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2254 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2258 for (i = 0; i < count; i++) {
2260 int activity_count = 0;
2262 if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name,
2268 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2269 stream = mail_close(stream);
2273 if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[m], mailbox_list[i], &activity_data_list, &activity_count, false, &error))
2274 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2276 /* it is duplicated with emstorage_get_pbd_activity_data
2277 if (false == emstorage_get_mailbox_pbd_activity_count(pbd_evnet->multi_user_name, account_list[m], mailbox_list[i], &activity_count, false, &error)) {
2278 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
2282 if (activity_count > 0 && activity_data_list) {
2283 int temp_error = EMAIL_ERROR_NONE;
2287 int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2291 if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[j], mailbox_list[i], &activity_data_list, &num_activity, false, &error))
2292 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2294 if (NULL == activity_data_list) {
2295 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
2299 remainder = activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2300 iter = activity_count/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1 : 0);
2302 for (j = 0; j < iter; j++) {
2303 if ((iter == (j+1)) && (remainder != 0))
2306 /*Call bulk download here */
2307 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
2308 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2309 temp_error = EMAIL_ERROR_NO_RESPONSE;
2314 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
2315 temp_error = EMAIL_ERROR_NONE;
2320 for (k = 0; k < activity_count; k++)
2321 emcore_free_partial_body_thd_event(activity_data_list + k, &error);
2323 EM_SAFE_FREE(activity_data_list);
2325 /*check: empty check required?*/
2326 if (false == emcore_is_partial_body_thd_que_empty()) {
2328 goto FINISH_OFF; /* Stop Local Activity Sync */
2331 stream = mail_close(stream);
2335 /* After completing one cycle of local activity sync ,
2336 local activity continue variable should be set to false. */
2338 emcore_pb_thd_set_local_activity_continue(false);
2339 } else /* Event-type is 0 which means Event is to flush the que when no more events are present in g_partial_body_thd_event_que */ {
2340 /*Check if events have arrived in g_partial_body_thd_event_que */
2341 if (false == emcore_is_partial_body_thd_que_empty()) {
2342 EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
2344 goto FINISH_OFF; /* Stop Local Activity Sync */
2347 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2348 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2357 EM_SAFE_FREE(mailbox_list);
2358 EM_SAFE_FREE(account_list);
2359 stream = mail_close(stream);
2362 *error_code = error;
2363 EM_DEBUG_FUNC_END("ret [%d]", ret);
2367 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */