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);
68 /*-----------------------------------------------------------------------------
70 *---------------------------------------------------------------------------*/
71 INTERNAL_FUNC thread_t g_send_srv_thread;
72 INTERNAL_FUNC pthread_cond_t _send_event_available_signal = PTHREAD_COND_INITIALIZER;
73 INTERNAL_FUNC pthread_mutex_t *_send_event_queue_lock = NULL;
74 INTERNAL_FUNC pthread_mutex_t *_send_event_handle_map_lock = NULL;
76 INTERNAL_FUNC GQueue *g_send_event_que = NULL;
77 INTERNAL_FUNC int g_send_event_loop = 1;
78 INTERNAL_FUNC int send_handle_map[EVENT_QUEUE_MAX] = {0,};
79 INTERNAL_FUNC int send_thread_run = 0;
81 static int emcore_get_new_send_handle(void);
83 /*-----------------------------------------------------------------------------
84 * Partial Body Event Queue
85 *---------------------------------------------------------------------------*/
87 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
89 INTERNAL_FUNC thread_t g_partial_body_thd ; /* Determines if thread is created or not. Non Null means thread is created */
90 INTERNAL_FUNC pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect event queue */
91 INTERNAL_FUNC pthread_cond_t _partial_body_thd_cond = PTHREAD_COND_INITIALIZER; /* Condition variable on which partial body thread is waiting */
92 INTERNAL_FUNC pthread_mutex_t _state_variables_lock;
94 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
95 INTERNAL_FUNC int g_partial_body_thd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
96 //INTERNAL_FUNC int g_partial_body_thd_loop = 1; /* Variable to make a continuos while loop */
97 INTERNAL_FUNC int g_partial_body_thd_queue_empty = true; /* Variable to determine if event queue is empty.True means empty*/
98 INTERNAL_FUNC int g_partial_body_thd_queue_full = false; /* Variable to determine if event queue is full. True means full*/
99 INTERNAL_FUNC int g_pb_thd_local_activity_continue = true; /* Variable to control local activity sync */
100 INTERNAL_FUNC int g_pbd_thd_state = false; /* false : thread is sleeping , true : thread is working */
102 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
103 static int g_partial_body_bulk_dwd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
104 INTERNAL_FUNC int g_partial_body_bulk_dwd_queue_empty = true;
106 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
107 static int emcore_clear_bulk_pbd_que(int *err_code);
109 INTERNAL_FUNC email_event_t *sync_failed_event_data = NULL;
113 /*-----------------------------------------------------------------------------
115 *---------------------------------------------------------------------------*/
116 typedef struct EVENT_CALLBACK_ELEM {
117 email_event_callback callback;
119 struct EVENT_CALLBACK_ELEM *next;
120 } EVENT_CALLBACK_LIST;
122 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM]; /* array of singly-linked list for event callbacks */
123 INTERNAL_FUNC pthread_mutex_t *_event_callback_table_lock = NULL;
125 #ifdef __FEATURE_LOCAL_ACTIVITY__
126 INTERNAL_FUNC int g_local_activity_run = 0;
127 INTERNAL_FUNC int g_save_local_activity_run = 0;
128 static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error);
131 #ifdef ENABLE_IMAP_IDLE_THREAD
132 extern int g_imap_idle_thread_alive;
133 extern int imap_idle_thread;
134 #endif /* ENABLE_IMAP_IDLE_THREAD */
136 static int is_gdk_lock_needed()
139 return (THREAD_SELF() == g_srv_thread);
144 static void fail_status_notify(email_event_t *event_data, int error)
146 EM_DEBUG_FUNC_BEGIN();
147 int account_id, mail_id;
150 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
153 account_id = event_data->account_id;
154 mail_id = event_data->event_param_data_4;
156 EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
158 switch (event_data->type) {
159 case EMAIL_EVENT_SEND_MAIL:
160 /* case EMAIL_EVENT_SEND_MAIL_SAVED: */
161 /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
162 emcore_show_user_message(event_data->multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, error);
165 case EMAIL_EVENT_SYNC_HEADER:
166 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
167 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SYNC_HEADER, error);
170 case EMAIL_EVENT_DOWNLOAD_BODY:
171 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
172 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
175 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
176 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
177 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
180 case EMAIL_EVENT_DELETE_MAIL:
181 case EMAIL_EVENT_DELETE_MAIL_ALL:
182 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
183 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, error);
186 case EMAIL_EVENT_VALIDATE_ACCOUNT:
187 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
188 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
191 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
192 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
193 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
196 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
197 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
198 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
201 case EMAIL_EVENT_CREATE_MAILBOX:
202 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
203 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
206 case EMAIL_EVENT_DELETE_MAILBOX:
207 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
208 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
211 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
212 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
213 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
216 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
217 emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
220 case EMAIL_EVENT_SEARCH_ON_SERVER:
221 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
222 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
225 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
226 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
227 emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
230 case EMAIL_EVENT_UPDATE_MAIL:
231 emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
234 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
235 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);
245 INTERNAL_FUNC void emcore_initialize_event_callback_table()
247 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
251 for (i = 0; i < EMAIL_ACTION_NUM; i++)
252 _event_callback_table[i] = NULL;
254 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
257 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
259 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
261 if (callback == NULL)
266 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
268 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
270 while (node != NULL) {
271 if (node->callback == callback && node->event_data == event_data) /* already registered */
277 /* not found, so keep going */
279 node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
281 if (node == NULL) /* not enough memory */
284 node->callback = callback;
285 node->event_data = event_data;
286 node->next = _event_callback_table[action];
288 _event_callback_table[action] = node;
294 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
299 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
301 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
303 if (callback == NULL)
308 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
310 EVENT_CALLBACK_LIST *prev = NULL;
311 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
313 while (node != NULL) {
314 if (node->callback == callback) {
316 prev->next = node->next;
318 _event_callback_table[action] = node->next;
330 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
335 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)
337 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);
340 lock_needed = is_gdk_lock_needed();
343 /* Todo : g_thread_yield */
346 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
348 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
350 while (node != NULL) {
351 if (node->callback != NULL)
352 node->callback(total, done, status, account_id, mail_id, handle, node->event_data, error);
356 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
363 /* insert a event to event queue */
364 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
366 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
369 EM_DEBUG_EXCEPTION("Invalid Parameter");
370 if (err_code != NULL)
371 *err_code = EMAIL_ERROR_INVALID_PARAM;
376 EM_DEBUG_EXCEPTION("email-service is not ready");
377 if (err_code != NULL)
378 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
383 int error = EMAIL_ERROR_NONE;
386 char *multi_user_name = NULL;
388 multi_user_name = EM_SAFE_STRDUP(event_data->multi_user_name);
390 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
393 q_length = g_queue_get_length(g_event_que);
395 EM_DEBUG_LOG("Q Length : [%d]", q_length);
397 if (q_length > EVENT_QUEUE_MAX) {
398 EM_DEBUG_EXCEPTION("event que is full...");
399 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
402 new_handle = emcore_get_new_handle();
405 event_data->status = EMAIL_EVENT_STATUS_WAIT;
406 event_data->handle = new_handle;
407 g_queue_push_tail(g_event_que, event_data);
408 WAKE_CONDITION_VARIABLE(_event_available_signal);
411 EM_DEBUG_EXCEPTION("event queue is full...");
412 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
418 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
421 *handle = new_handle;
423 switch (event_data->type) {
424 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
425 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
426 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
427 case EMAIL_EVENT_VALIDATE_ACCOUNT:
428 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
429 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
430 case EMAIL_EVENT_SAVE_MAIL:
431 case EMAIL_EVENT_MOVE_MAIL:
432 case EMAIL_EVENT_DELETE_MAIL:
433 case EMAIL_EVENT_DELETE_MAIL_ALL:
434 case EMAIL_EVENT_CREATE_MAILBOX:
435 case EMAIL_EVENT_DELETE_MAILBOX:
436 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
437 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
438 case EMAIL_EVENT_UPDATE_MAIL:
439 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
440 case EMAIL_EVENT_SEARCH_ON_SERVER:
441 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
445 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
447 int is_local_activity_event_inserted = false;
448 emcore_partial_body_thd_local_activity_sync(multi_user_name,
449 &is_local_activity_event_inserted,
451 if (error != EMAIL_ERROR_NONE) {
452 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
454 if (true == is_local_activity_event_inserted)
455 emcore_pb_thd_set_local_activity_continue(false);
462 EM_SAFE_FREE(multi_user_name);
465 EM_DEBUG_LOG("ERR [%d]", error);
472 /* get a event from event_data queue */
473 INTERNAL_FUNC int emcore_retrieve_event(email_event_t **event_data, int *err_code)
475 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
478 int error = EMAIL_ERROR_NONE;
480 email_event_t *poped = NULL;
481 email_event_t *head_event = NULL;
484 q_length = g_queue_get_length(g_event_que);
486 EM_DEBUG_LOG("Q Length : [%d]", q_length);
489 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
490 EM_DEBUG_LOG("QUEUE is empty");
495 head_event = (email_event_t *)g_queue_peek_head(g_event_que);
497 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
498 EM_DEBUG_LOG_DEV("QUEUE is empty");
501 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
502 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
503 poped = g_queue_pop_head(g_event_que);
505 emcore_return_handle(poped->handle);
506 emcore_free_event(poped);
509 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
510 EM_DEBUG_LOG("QUEUE is empty");
514 head_event->status = EMAIL_EVENT_STATUS_STARTED;
515 *event_data = head_event;
523 if (err_code != NULL)
526 EM_DEBUG_FUNC_END("ret [%d]", ret);
530 /* get a event from event_data queue for download_attachment*/
531 INTERNAL_FUNC int emcore_retrieve_event_for_download_attachment(email_event_t **event_data, int *err_code)
533 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
536 int error = EMAIL_ERROR_NONE;
538 email_event_t *poped = NULL;
539 email_event_t *head_event = NULL;
542 q_length = g_queue_get_length(g_event_que);
544 EM_DEBUG_LOG("Q Length : [%d]", q_length);
547 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
548 EM_DEBUG_LOG("QUEUE is empty");
552 head_event = (email_event_t *)g_queue_peek_head(g_event_que);
554 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
555 EM_DEBUG_LOG_DEV("QUEUE is empty");
557 } else if (head_event->status != EMAIL_EVENT_STATUS_STARTED) {
558 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
559 error = EMAIL_ERROR_NO_MORE_DATA;
560 EM_DEBUG_LOG("get event data err");
563 *event_data = head_event;
571 if (err_code != NULL)
574 EM_DEBUG_FUNC_END("ret [%d]", ret);
581 /* check that event_data loop is continuous */
582 INTERNAL_FUNC int emcore_event_loop_continue(void)
587 INTERNAL_FUNC int emcore_is_event_queue_empty(void)
592 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
594 q_length = g_queue_get_length(g_event_que);
595 EM_DEBUG_LOG("Q Length : [%d]", q_length);
599 EM_DEBUG_LOG("event que is empty");
602 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
608 INTERNAL_FUNC int emcore_is_send_event_queue_empty(void)
613 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
614 if (g_send_event_que)
615 q_length = g_queue_get_length(g_send_event_que);
616 EM_DEBUG_LOG("Q Length : [%d]", q_length);
620 EM_DEBUG_LOG("send event que is empty");
623 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
629 INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data)
631 if (!sync_failed_event_data) {
632 EM_DEBUG_EXCEPTION("sync_failed_event_data is NULL");
636 email_event_t *new_event = NULL;
638 new_event = em_malloc(sizeof(email_event_t));
639 if (new_event == NULL) {
640 EM_DEBUG_EXCEPTION("em_mallocfailed");
644 new_event->account_id = sync_failed_event_data->account_id;
645 new_event->type = sync_failed_event_data->type;
646 if (sync_failed_event_data->event_param_data_3)
647 new_event->event_param_data_3 = EM_SAFE_STRDUP(sync_failed_event_data->event_param_data_3);
648 new_event->event_param_data_4 = sync_failed_event_data->event_param_data_4;
649 new_event->event_param_data_5 = sync_failed_event_data->event_param_data_5;
650 new_event->multi_user_name = EM_SAFE_STRDUP(sync_failed_event_data->multi_user_name);
652 *event_data = new_event;
654 emcore_free_event(sync_failed_event_data);
655 EM_SAFE_FREE(sync_failed_event_data);
658 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
660 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
663 EM_DEBUG_EXCEPTION("Invalid Parameter");
664 if (err_code != NULL)
665 *err_code = EMAIL_ERROR_INVALID_PARAM;
669 if (!g_send_srv_thread) {
670 EM_DEBUG_EXCEPTION("email-service is not ready");
671 if (err_code != NULL)
672 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
677 int error = EMAIL_ERROR_NONE;
681 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
683 if (g_send_event_que)
684 q_length = g_queue_get_length(g_send_event_que);
686 EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
688 if (q_length > EVENT_QUEUE_MAX) {
689 EM_DEBUG_EXCEPTION("send event que is full...");
690 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
693 new_handle = emcore_get_new_send_handle();
696 event_data->status = EMAIL_EVENT_STATUS_WAIT;
697 event_data->handle = new_handle;
698 g_queue_push_tail(g_send_event_que, event_data);
699 WAKE_CONDITION_VARIABLE(_send_event_available_signal);
704 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
707 *handle = new_handle;
709 if (err_code != NULL)
712 EM_DEBUG_LOG("Finish with [%d]", ret);
717 INTERNAL_FUNC int emcore_retrieve_send_event(email_event_t **event_data, int *err_code)
719 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
722 int error = EMAIL_ERROR_NONE;
724 email_event_t *poped = NULL;
725 email_event_t *head_event = NULL;
727 /* g_queue_get_length is aborted when param is null */
728 q_length = g_send_event_que ? g_queue_get_length(g_send_event_que) : 0; /*prevent 35141*/
730 EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
733 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
734 EM_DEBUG_LOG_DEV("SEND QUEUE is empty");
739 head_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
741 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
742 EM_DEBUG_LOG("SEND QUEUE is empty");
745 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
746 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
747 poped = g_queue_pop_head(g_send_event_que);
749 emcore_return_send_handle(poped->handle);
750 emcore_free_event(poped);
753 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
754 EM_DEBUG_LOG("SEND QUEUE is empty");
758 head_event->status = EMAIL_EVENT_STATUS_STARTED;
759 *event_data = head_event;
767 if (err_code != NULL)
770 EM_DEBUG_FUNC_END("ret [%d]", ret);
774 /* finish api event_data loop */
775 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
777 EM_DEBUG_FUNC_BEGIN();
779 if (err_code != NULL)
780 *err_code = EMAIL_ERROR_NONE;
782 if (!g_send_srv_thread) {
783 if (err_code != NULL)
784 *err_code = EMAIL_ERROR_UNKNOWN;
788 /* stop event_data loop */
789 g_send_event_loop = 0;
791 emcore_cancel_all_send_mail_thread(err_code);
793 WAKE_CONDITION_VARIABLE(_send_event_available_signal); /* MUST BE HERE */
795 /* wait for thread finished */
796 THREAD_JOIN(g_send_srv_thread);
798 g_queue_free(g_send_event_que);
800 g_send_srv_thread = 0;
802 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
803 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
804 DELETE_CONDITION_VARIABLE(_send_event_available_signal);
806 if (err_code != NULL)
807 *err_code = EMAIL_ERROR_NONE;
812 /* finish api event_data loop */
813 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
815 EM_DEBUG_FUNC_BEGIN();
817 if (err_code != NULL)
818 *err_code = EMAIL_ERROR_NONE;
821 if (err_code != NULL)
822 *err_code = EMAIL_ERROR_UNKNOWN;
826 /* stop event_data loop */
829 /* pthread_kill(g_srv_thread, SIGINT); */
830 emcore_cancel_all_thread(err_code);
832 WAKE_CONDITION_VARIABLE(_event_available_signal);
834 /* wait for thread finished */
835 THREAD_JOIN(g_srv_thread);
837 g_queue_free(g_event_que);
841 DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
842 DELETE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
843 DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
844 DELETE_CONDITION_VARIABLE(_event_available_signal);
846 if (err_code != NULL)
847 *err_code = EMAIL_ERROR_NONE;
852 /* check event thread status (worker_event_queue)
853 * 0 : stop job 1 : continue job
854 event list handled by event thread :
855 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
856 case EMAIL_EVENT_SYNC_HEADER:
857 case EMAIL_EVENT_SYNC_HEADER_OMA:
858 case EMAIL_EVENT_DOWNLOAD_BODY:
859 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
860 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
861 case EMAIL_EVENT_DELETE_MAIL:
862 case EMAIL_EVENT_DELETE_MAIL_ALL:
863 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
864 case EMAIL_EVENT_CREATE_MAILBOX:
865 case EMAIL_EVENT_DELETE_MAILBOX:
866 case EMAIL_EVENT_SAVE_MAIL:
867 case EMAIL_EVENT_MOVE_MAIL:
868 case EMAIL_EVENT_VALIDATE_ACCOUNT:
869 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
870 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
871 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
872 case EMAIL_EVENT_UPDATE_MAIL:
873 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
874 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
875 case EMAIL_EVENT_LOCAL_ACTIVITY:
876 case EMAIL_EVENT_SEARCH_ON_SERVER:
877 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
878 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
880 INTERNAL_FUNC int emcore_check_event_thread_status(int *event_type, int handle)
882 EM_DEBUG_FUNC_BEGIN();
886 email_event_t *active_event = NULL;
888 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
891 q_length = g_queue_get_length(g_event_que);
894 active_event = (email_event_t *)g_queue_peek_head(g_event_que);
896 if (active_event->handle == handle) {
898 *event_type = active_event->type;
899 if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
908 EM_DEBUG_LOG("Rcv Queue is empty [%d]", q_length);
912 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
913 EM_DEBUG_FUNC_END("ret [%d]", ret);
918 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
920 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
923 int err = EMAIL_ERROR_NONE;
927 email_event_t *found_elm = NULL;
928 email_event_t *pop_elm = NULL;
930 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
933 q_length = g_queue_get_length(g_event_que);
935 for (i = 0; i < q_length; i++) {
936 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
937 if (found_elm && found_elm->handle == handle) {
938 EM_DEBUG_LOG("Found Queue element[%d] with handle[%d]", i, handle);
945 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
946 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
948 switch (found_elm->type) {
950 case EMAIL_EVENT_SEND_MAIL:
951 case EMAIL_EVENT_SEND_MAIL_SAVED:
952 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
953 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
954 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
957 case EMAIL_EVENT_DOWNLOAD_BODY:
958 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
959 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
960 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_BODY_CANCEL] Failed >>>>");
963 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
964 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
965 if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
966 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
969 case EMAIL_EVENT_SYNC_HEADER:
970 case EMAIL_EVENT_SYNC_HEADER_OMA:
971 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
972 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
973 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
976 case EMAIL_EVENT_VALIDATE_ACCOUNT:
977 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
978 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
979 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
982 case EMAIL_EVENT_DELETE_MAIL:
983 case EMAIL_EVENT_DELETE_MAIL_ALL:
984 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
985 case EMAIL_EVENT_SAVE_MAIL:
986 case EMAIL_EVENT_MOVE_MAIL:
987 case EMAIL_EVENT_CREATE_MAILBOX:
988 case EMAIL_EVENT_DELETE_MAILBOX:
989 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
990 case EMAIL_EVENT_SEARCH_ON_SERVER:
991 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
992 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
999 pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1001 emcore_return_handle(pop_elm->handle);
1002 emcore_free_event(pop_elm);
1003 EM_SAFE_FREE(pop_elm);
1005 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1008 switch (found_elm->type) {
1009 case EMAIL_EVENT_SYNC_HEADER:
1010 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER");
1011 if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
1012 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1013 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)
1014 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1017 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1018 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1019 if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
1020 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
1026 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1032 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1034 if (err_code != NULL)
1036 EM_DEBUG_FUNC_END();
1040 /* cancel all job */
1041 INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
1043 EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1046 int err = EMAIL_ERROR_NONE;
1049 email_event_t *pop_elm = NULL;
1051 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1053 /* g_queue_get_length is aborted when param is null */
1054 q_length = g_event_que ? g_queue_get_length(g_event_que) : 0; /*prevent 35142 */
1056 for (i = 0; i < q_length; i++) {
1057 pop_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1059 if (pop_elm && (pop_elm->status == EMAIL_EVENT_STATUS_WAIT)) {
1061 fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1063 switch (pop_elm->type) {
1065 case EMAIL_EVENT_SEND_MAIL:
1066 case EMAIL_EVENT_SEND_MAIL_SAVED:
1067 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1068 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1069 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1072 case EMAIL_EVENT_DOWNLOAD_BODY:
1073 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1074 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1075 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1078 case EMAIL_EVENT_SYNC_HEADER:
1079 case EMAIL_EVENT_SYNC_HEADER_OMA:
1080 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1081 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1082 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1083 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1086 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1087 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", pop_elm->account_id);
1088 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1089 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1092 case EMAIL_EVENT_DELETE_MAIL:
1093 case EMAIL_EVENT_DELETE_MAIL_ALL:
1094 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1095 case EMAIL_EVENT_SAVE_MAIL:
1096 case EMAIL_EVENT_MOVE_MAIL:
1097 case EMAIL_EVENT_CREATE_MAILBOX:
1098 case EMAIL_EVENT_DELETE_MAILBOX:
1099 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1100 case EMAIL_EVENT_SEARCH_ON_SERVER:
1101 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1102 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1109 pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1111 emcore_return_handle(pop_elm->handle);
1112 emcore_free_event(pop_elm);
1113 EM_SAFE_FREE(pop_elm);
1117 q_length = g_queue_get_length(g_event_que);
1120 if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1123 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1127 if (err_code != NULL)
1129 EM_DEBUG_FUNC_END();
1133 /* check thread status
1134 * 0 : stop job 1 : continue job
1136 INTERNAL_FUNC int emcore_check_send_mail_thread_status(void)
1138 EM_DEBUG_FUNC_BEGIN();
1142 email_event_t *active_event = NULL;
1144 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1146 if (g_send_event_que)
1147 q_length = g_queue_get_length(g_send_event_que);
1150 active_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
1152 if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
1158 EM_DEBUG_LOG("Send Queue is empty [%d]", q_length);
1162 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1163 EM_DEBUG_FUNC_END("ret [%d]", ret);
1167 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(char *multi_user_name, int account_id)
1169 EM_DEBUG_FUNC_BEGIN();
1170 int error_code = EMAIL_ERROR_NONE;
1173 email_event_t *found_elm = NULL;
1174 email_event_t *pop_elm = NULL;
1176 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1179 q_length = g_queue_get_length(g_event_que);
1181 for (i = 0; i < q_length; i++) {
1182 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1183 if (found_elm && (found_elm->account_id == account_id || found_elm->account_id == ALL_ACCOUNT) &&
1184 ((!found_elm->multi_user_name && !multi_user_name) || (
1185 (multi_user_name) && !EM_SAFE_STRCASECMP(found_elm->multi_user_name, multi_user_name)))) {
1186 EM_DEBUG_LOG("Found Queue element[%d]", i);
1188 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1189 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1191 switch (found_elm->type) {
1193 case EMAIL_EVENT_SEND_MAIL:
1194 case EMAIL_EVENT_SEND_MAIL_SAVED:
1195 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1196 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL ,
1197 found_elm->event_param_data_4, error_code))
1198 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1201 case EMAIL_EVENT_DOWNLOAD_BODY:
1202 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1203 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL,
1204 found_elm->event_param_data_4, error_code))
1205 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1208 case EMAIL_EVENT_SYNC_HEADER:
1209 case EMAIL_EVENT_SYNC_HEADER_OMA:
1210 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1211 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1212 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1213 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1216 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1217 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
1218 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, error_code))
1219 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1222 case EMAIL_EVENT_DELETE_MAIL:
1223 case EMAIL_EVENT_DELETE_MAIL_ALL:
1224 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1225 case EMAIL_EVENT_SAVE_MAIL:
1226 case EMAIL_EVENT_MOVE_MAIL:
1227 case EMAIL_EVENT_CREATE_MAILBOX:
1228 case EMAIL_EVENT_DELETE_MAILBOX:
1229 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1230 case EMAIL_EVENT_SEARCH_ON_SERVER:
1231 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1232 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1239 pop_elm = g_queue_pop_nth(g_event_que, i);
1241 emcore_return_handle(pop_elm->handle);
1242 emcore_free_event(pop_elm);
1243 EM_SAFE_FREE(pop_elm);
1246 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1249 q_length = g_queue_get_length(g_event_que);
1252 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1257 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1259 EM_DEBUG_FUNC_END();
1264 /* cancel send mail job */
1265 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
1267 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
1270 int err = EMAIL_ERROR_NONE;
1274 email_event_t *found_elm = NULL;
1275 email_event_t *pop_elm = NULL;
1277 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1279 if (g_send_event_que)
1280 q_length = g_queue_get_length(g_send_event_que);
1282 for (i = 0; i < q_length; i++) {
1283 found_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1284 if (found_elm && (found_elm->handle == handle)) {
1285 EM_DEBUG_LOG("Found Send Queue element[%d] with handle[%d]", i, handle);
1292 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1293 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1295 switch (found_elm->type) {
1296 case EMAIL_EVENT_SEND_MAIL:
1297 case EMAIL_EVENT_SEND_MAIL_SAVED:
1298 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1299 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL, found_elm->event_param_data_4, err))
1300 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1306 pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1308 emcore_return_send_handle(pop_elm->handle);
1309 emcore_free_event(pop_elm);
1310 EM_SAFE_FREE(pop_elm);
1312 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1315 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1321 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1323 if (err_code != NULL)
1325 EM_DEBUG_FUNC_END();
1329 INTERNAL_FUNC int emcore_cancel_all_send_mail_thread(int *err_code)
1331 EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1334 int err = EMAIL_ERROR_NONE;
1337 email_event_t *pop_elm = NULL;
1339 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1341 if (g_send_event_que)
1342 q_length = g_queue_get_length(g_send_event_que);
1344 for (i = 0; i < q_length; i++) {
1345 pop_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1347 if (pop_elm && pop_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1349 fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1351 switch (pop_elm->type) {
1352 case EMAIL_EVENT_SEND_MAIL:
1353 case EMAIL_EVENT_SEND_MAIL_SAVED:
1354 pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1355 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL, pop_elm->event_param_data_4, err))
1356 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1362 pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1364 emcore_return_send_handle(pop_elm->handle);
1365 emcore_free_event(pop_elm);
1366 EM_SAFE_FREE(pop_elm);
1370 if (g_send_event_que)
1371 q_length = g_queue_get_length(g_send_event_que);
1374 if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1377 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1381 if (err_code != NULL)
1383 EM_DEBUG_FUNC_END();
1387 INTERNAL_FUNC int emcore_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count)
1389 EM_DEBUG_FUNC_BEGIN("output_task_information[%p] output_task_information_count[%p]", output_task_information, output_task_information_count);
1390 int err = EMAIL_ERROR_NONE;
1394 email_task_information_t *task_information = NULL;
1395 email_event_t *elm = NULL;
1397 if (output_task_information == NULL || output_task_information_count == NULL) {
1398 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1399 err = EMAIL_ERROR_INVALID_PARAM;
1404 q_length = g_queue_get_length(g_event_que);
1407 err = EMAIL_ERROR_DATA_NOT_FOUND;
1411 if ((task_information = em_malloc(sizeof(email_task_information_t) * q_length)) == NULL) {
1412 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1413 err = EMAIL_ERROR_OUT_OF_MEMORY;
1417 for (i = 0; i < q_length; i++) {
1418 elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1420 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
1421 err = EMAIL_ERROR_DATA_NOT_FOUND;
1424 if(elm->type != EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1425 if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1426 task_information[index].handle = elm->handle;
1427 task_information[index].account_id = elm->account_id;
1428 task_information[index].type = elm->type;
1429 task_information[index].status = elm->status;
1430 task_information[index].task_data1 = 0;
1431 task_information[index].task_data2 = 0;
1432 task_information[index].task_data3 = 0;
1436 }else if(elm->type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1437 if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1439 task_information[index].task_data1 = (void *)elm->event_param_data_4; /* mail_id */
1440 task_information[index].task_data2 = (void *)elm->event_param_data_5; /* attachment_nth */
1441 task_information[index].task_data3 = (void *)elm->event_param_data_8; /* download progress */
1443 task_information[index].handle = elm->handle;
1444 task_information[index].account_id = elm->account_id;
1445 task_information[index].type = elm->type;
1446 task_information[index].status = elm->status;
1454 *output_task_information_count = q_length;
1455 *output_task_information = task_information;
1459 EM_DEBUG_FUNC_END("err [%d]", err);
1463 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
1465 EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
1467 return EMAIL_ERROR_INVALID_PARAM;
1469 switch (event_data->type) {
1470 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1471 EM_SAFE_FREE(event_data->event_param_data_3);
1474 case EMAIL_EVENT_SYNC_HEADER: /* synchronize mail header */
1477 case EMAIL_EVENT_SYNC_HEADER_OMA: /* synchronize mail header for OMA */
1478 EM_SAFE_FREE(event_data->event_param_data_1);
1481 case EMAIL_EVENT_DOWNLOAD_BODY: /* download mail body */
1484 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT: /* download attachment */
1487 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER: /* Sync flags field */
1488 EM_SAFE_FREE(event_data->event_param_data_3);
1491 case EMAIL_EVENT_DELETE_MAIL: /* delete mails */
1492 EM_SAFE_FREE(event_data->event_param_data_3);
1495 case EMAIL_EVENT_DELETE_MAIL_ALL: /* delete all mails */
1498 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1499 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1503 case EMAIL_EVENT_CREATE_MAILBOX:
1504 emcore_free_mailbox((email_mailbox_t*)event_data->event_param_data_1);
1505 EM_SAFE_FREE(event_data->event_param_data_1);
1508 case EMAIL_EVENT_DELETE_MAILBOX:
1511 case EMAIL_EVENT_SAVE_MAIL:
1514 case EMAIL_EVENT_MOVE_MAIL:
1515 EM_SAFE_FREE(event_data->event_param_data_3);
1518 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1521 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
1522 emcore_free_account((email_account_t *)event_data->event_param_data_1);
1523 EM_SAFE_FREE(event_data->event_param_data_1);
1527 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
1528 emcore_free_account((email_account_t *)event_data->event_param_data_1);
1529 EM_SAFE_FREE(event_data->event_param_data_1);
1532 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
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_UPDATE_MAIL: {
1538 email_mail_data_t* input_mail_data = (email_mail_data_t*) event_data->event_param_data_1;
1539 email_attachment_data_t* input_attachment_data_list = (email_attachment_data_t*) event_data->event_param_data_2;
1540 email_meeting_request_t* input_meeting_request = (email_meeting_request_t*) event_data->event_param_data_3;
1541 int input_attachment_count = event_data->event_param_data_4;
1542 emcore_free_mail_data_list(&input_mail_data, 1);
1543 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1544 emstorage_free_meeting_request(input_meeting_request);
1548 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1551 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
1554 #ifdef __FEATURE_LOCAL_ACTIVITY__
1555 case EMAIL_EVENT_LOCAL_ACTIVITY:
1557 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
1559 case EMAIL_EVENT_SEARCH_ON_SERVER: {
1561 int search_filter_count = event_data->event_param_data_5;
1562 email_search_filter_t *search_filter = (email_search_filter_t *)event_data->event_param_data_1;
1564 if (event_data->event_param_data_1) {
1565 for (i = 0; i < search_filter_count; i++) {
1566 switch (search_filter[i].search_filter_type) {
1567 case EMAIL_SEARCH_FILTER_TYPE_BCC:
1568 case EMAIL_SEARCH_FILTER_TYPE_BODY:
1569 case EMAIL_SEARCH_FILTER_TYPE_CC:
1570 case EMAIL_SEARCH_FILTER_TYPE_FROM:
1571 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD:
1572 case EMAIL_SEARCH_FILTER_TYPE_TEXT:
1573 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT:
1574 case EMAIL_SEARCH_FILTER_TYPE_TO:
1575 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID:
1576 case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME:
1577 case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
1578 case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED:
1579 EM_SAFE_FREE(search_filter[i].search_filter_key_value.string_type_key_value);
1587 EM_SAFE_FREE(event_data->event_param_data_1);
1590 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1591 EM_SAFE_FREE(event_data->event_param_data_1);
1592 EM_SAFE_FREE(event_data->event_param_data_2);
1593 EM_SAFE_FREE(event_data->event_param_data_3);
1597 case EMAIL_EVENT_SEND_MAIL:
1600 case EMAIL_EVENT_SEND_MAIL_SAVED:
1601 EM_SAFE_FREE(event_data->event_param_data_3);
1604 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
1607 #ifdef __FEATURE_LOCAL_ACTIVITY__
1608 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1610 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1612 default: /*free function is not implemented*/
1613 EM_DEBUG_EXCEPTION("event %d is NOT freed, possibly memory leaks", event_data->type);
1616 EM_SAFE_FREE(event_data->multi_user_name);
1617 event_data->event_param_data_1 = event_data->event_param_data_2 = event_data->event_param_data_3 = NULL;
1619 EM_DEBUG_FUNC_END();
1623 static int emcore_get_new_handle(void)
1625 EM_DEBUG_FUNC_BEGIN();
1629 ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1630 for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1631 if (handle_map[i] == 0) {
1636 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1638 if (i < EVENT_QUEUE_MAX) {
1640 EM_DEBUG_LOG("New handle [%d]", ret_handle);
1643 EM_DEBUG_EXCEPTION("there is no available handle");
1646 EM_DEBUG_FUNC_END();
1651 static int emcore_get_new_send_handle(void)
1653 EM_DEBUG_FUNC_BEGIN();
1657 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1658 for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1659 if (send_handle_map[i] == 0) {
1660 send_handle_map[i] = 1;
1664 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1666 if (i < EVENT_QUEUE_MAX) {
1668 EM_DEBUG_LOG("New send handle [%d]", ret_handle);
1671 EM_DEBUG_LOG("there is no available send handle");
1674 EM_DEBUG_FUNC_END();
1679 INTERNAL_FUNC int emcore_return_handle(int handle)
1681 EM_DEBUG_FUNC_BEGIN();
1684 ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1685 if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1686 handle_map[handle-1] = 0;
1688 EM_DEBUG_LOG("handle [%d] is returned", handle);
1690 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1692 EM_DEBUG_FUNC_END();
1697 INTERNAL_FUNC int emcore_return_send_handle(int handle)
1699 EM_DEBUG_FUNC_BEGIN();
1702 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1703 if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1704 send_handle_map[handle-1] = 0;
1706 EM_DEBUG_LOG("send handle [%d] is returned", handle);
1708 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1710 EM_DEBUG_FUNC_END();
1715 #ifdef __FEATURE_KEEP_CONNECTION__
1716 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
1718 return (unsigned int)g_srv_thread;
1720 #endif /* __FEATURE_KEEP_CONNECTION__ */
1722 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1724 INTERNAL_FUNC int emcore_get_pbd_thd_state()
1726 int pbd_thd_state = false;
1727 ENTER_CRITICAL_SECTION(_state_variables_lock);
1728 pbd_thd_state = g_pbd_thd_state;
1729 LEAVE_CRITICAL_SECTION(_state_variables_lock);
1730 return pbd_thd_state;
1733 INTERNAL_FUNC int emcore_set_pbd_thd_state(int flag)
1735 ENTER_CRITICAL_SECTION(_state_variables_lock);
1736 g_pbd_thd_state = flag;
1737 LEAVE_CRITICAL_SECTION(_state_variables_lock);
1739 return g_pbd_thd_state;
1742 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
1744 EM_DEBUG_FUNC_BEGIN();
1745 EM_DEBUG_FUNC_END();
1746 return (unsigned int)g_partial_body_thd;
1749 static int emcore_clear_bulk_pbd_que(int *err_code)
1751 EM_DEBUG_FUNC_BEGIN();
1754 int error = EMAIL_ERROR_NONE;
1757 for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
1758 if (g_partial_body_bulk_dwd_que[i].event_type) {
1759 if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error)) {
1760 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1767 if (NULL != err_code)
1769 EM_DEBUG_FUNC_END();
1773 INTERNAL_FUNC void emcore_pb_thd_set_local_activity_continue(int flag)
1775 EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
1777 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1779 g_pb_thd_local_activity_continue = flag;
1782 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
1785 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1786 EM_DEBUG_FUNC_END();
1789 INTERNAL_FUNC int emcore_pb_thd_can_local_activity_continue()
1791 EM_DEBUG_FUNC_BEGIN();
1795 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1797 ret = g_pb_thd_local_activity_continue;
1799 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1800 EM_DEBUG_FUNC_END();
1805 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
1807 EM_DEBUG_FUNC_BEGIN();
1810 int error = EMAIL_ERROR_NONE;
1813 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1815 if (true == g_partial_body_thd_queue_empty) {
1816 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
1818 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
1819 if (g_partial_body_thd_event_que[i].event_type) {
1820 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error)) {
1821 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1828 g_partial_body_thd_queue_empty = true;
1829 g_partial_body_thd_queue_full = false;
1831 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1833 if (NULL != err_code)
1835 EM_DEBUG_FUNC_END();
1839 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
1841 EM_DEBUG_FUNC_BEGIN();
1845 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1847 ret = g_partial_body_thd_queue_empty;
1849 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1850 EM_DEBUG_FUNC_END("ret [%d]", ret);
1853 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
1855 EM_DEBUG_FUNC_BEGIN();
1859 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1861 ret = g_partial_body_thd_queue_full;
1863 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1864 EM_DEBUG_FUNC_END();
1869 Himanshu[h.gahalut] : If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
1870 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
1871 through _partial_body_thd_event_queue_lock mutex.
1873 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
1877 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1878 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1882 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
1884 EM_DEBUG_FUNC_BEGIN();
1885 int error = EMAIL_ERROR_NONE;
1888 if (NULL == src || NULL == dest) {
1889 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
1890 error = EMAIL_ERROR_INVALID_PARAM;
1894 dest->account_id = src->account_id;
1895 dest->mail_id = src->mail_id;
1896 dest->server_mail_id = src->server_mail_id;
1897 dest->activity_id = src->activity_id;
1898 dest->mailbox_id = src->mailbox_id;
1899 dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
1900 dest->activity_type = src->activity_type;
1901 dest->event_type = src->event_type;
1902 dest->multi_user_name = EM_SAFE_STRDUP(src->multi_user_name);
1909 if (NULL != error_code)
1910 *error_code = error;
1917 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,
1918 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
1920 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
1924 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1925 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1929 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
1931 EM_DEBUG_FUNC_BEGIN();
1933 if (NULL == partial_body_thd_event) {
1934 *error_code = EMAIL_ERROR_INVALID_PARAM;
1938 email_event_partial_body_thd *pbd_event = partial_body_thd_event;
1940 /*Free character pointers in event_data cell */
1941 EM_SAFE_FREE(pbd_event->mailbox_name);
1942 EM_SAFE_FREE(pbd_event->multi_user_name);
1943 memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
1944 EM_DEBUG_FUNC_END();
1948 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(
1949 email_event_partial_body_thd *partial_body_thd_event,
1952 EM_DEBUG_FUNC_BEGIN();
1954 if (NULL == partial_body_thd_event) {
1955 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
1957 if (error_code != NULL) {
1958 *error_code = EMAIL_ERROR_INVALID_PARAM;
1964 int error = EMAIL_ERROR_NONE;
1965 int empty_cell_index = -1;
1969 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1971 /* find a cell in queue which is empty */
1972 index = g_partial_body_thd_next_event_idx;
1973 for (count = 0; count < TOTAL_PARTIAL_BODY_EVENTS; count++) {
1974 /*Found empty Cell*/
1975 if (g_partial_body_thd_event_que[index].event_type == EMAIL_EVENT_NONE) {
1976 empty_cell_index = index;
1980 index = index % TOTAL_PARTIAL_BODY_EVENTS;
1983 if (empty_cell_index != -1) {
1984 emcore_copy_partial_body_thd_event(partial_body_thd_event,
1985 g_partial_body_thd_event_que + empty_cell_index,
1987 if (error != EMAIL_ERROR_NONE) {
1988 EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
1990 g_partial_body_thd_queue_empty = false;
1991 if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
1992 /*This is the last event_data inserted in queue after its insertion, queue is full */
1993 g_partial_body_thd_queue_full = true;
1995 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
1999 EM_DEBUG_LOG("partial body thread event_data queue is full ");
2000 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
2002 g_partial_body_thd_queue_full = true;
2003 g_partial_body_thd_queue_empty = false;
2006 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2008 if (NULL != error_code) {
2009 *error_code = error;
2016 /* h.gahlaut : Return true only if event_data is retrieved successfully */
2018 INTERNAL_FUNC int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
2020 EM_DEBUG_FUNC_BEGIN();
2023 int error = EMAIL_ERROR_NONE;
2026 /* Lock Mutex to protect event_data queue and associated global variables variables*/
2028 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2030 index = g_partial_body_thd_next_event_idx;
2032 if (0 == g_partial_body_thd_event_que[index].event_type) {
2033 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
2034 g_partial_body_thd_queue_empty = true;
2035 g_partial_body_thd_queue_full = false;
2037 /*Copy the event_data from queue to return it and free the event_data in queue */
2038 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
2039 EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
2041 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
2042 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2044 g_partial_body_thd_queue_full = false;
2045 g_partial_body_thd_next_event_idx = ++index;
2047 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
2048 g_partial_body_thd_next_event_idx = 0;
2050 /* If the event_data retrieved was the only event_data present in queue,
2051 we need to set g_partial_body_thd_queue_empty to true
2054 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
2055 g_partial_body_thd_queue_empty = true;
2065 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2068 *error_code = error;
2074 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
2075 static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code)
2077 EM_DEBUG_FUNC_BEGIN();
2078 int error = EMAIL_ERROR_NONE;
2080 MAILSTREAM *stream = NULL;
2081 void *tmp_stream = NULL;
2083 if (!emcore_connect_to_remote_mailbox(multi_user_name,
2084 g_partial_body_bulk_dwd_que[0].account_id,
2085 g_partial_body_bulk_dwd_que[0].mailbox_id,
2087 (void **)&tmp_stream,
2088 &error) || (NULL == tmp_stream)) {
2089 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2092 stream = (MAILSTREAM *)tmp_stream;
2094 /* Call bulk download here */
2095 if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
2096 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2103 stream = mail_close(stream);
2105 g_partial_body_bulk_dwd_next_event_idx = 0;
2106 g_partial_body_bulk_dwd_queue_empty = true;
2108 if (false == emcore_clear_bulk_pbd_que(&error))
2109 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
2111 if (NULL != error_code)
2112 *error_code = error;
2114 EM_DEBUG_FUNC_END("ret [%d]", ret);
2119 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
2120 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
2122 EM_DEBUG_FUNC_BEGIN();
2123 int error = EMAIL_ERROR_NONE;
2127 MAILSTREAM *stream = NULL;
2128 email_event_partial_body_thd *activity_data_list = NULL;
2129 int *mailbox_list = NULL;
2130 int *account_list = NULL;
2132 if (NULL == pbd_event) {
2133 EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
2135 error = EMAIL_ERROR_INVALID_PARAM;
2139 /*Check if the event_data is to flush the event_data que array */
2140 if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
2141 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
2142 /*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
2143 then append this event_data also to download que array */
2144 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
2145 (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
2146 EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
2147 EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
2148 if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
2149 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2150 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2155 EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
2156 EM_DEBUG_LOG("Flush the current que if not empty");
2157 EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
2158 if (!g_partial_body_bulk_dwd_queue_empty) {
2159 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2160 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2165 /*Add the event_data to the download que array */
2166 if (false == emcore_copy_partial_body_thd_event(pbd_event,
2167 g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
2168 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
2170 g_partial_body_bulk_dwd_queue_empty = false;
2171 g_partial_body_bulk_dwd_next_event_idx++;
2172 EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
2174 } else if (pbd_event->activity_type) {
2175 int account_count = 0;
2177 EM_DEBUG_LOG("Event is coming from local activity.");
2178 /* Get all the accounts for which local activities are pending */
2179 if (false == emstorage_get_pbd_account_list(pbd_event->multi_user_name, &account_list, &account_count, false, &error)) {
2180 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2181 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2185 for (m = 0; m < account_count; ++m) {
2186 /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
2187 if (false == emstorage_get_pbd_mailbox_list(pbd_event->multi_user_name, account_list[m], &mailbox_list, &count, false, &error)) {
2188 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2189 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2193 for (i = 0; i < count; i++) {
2195 int activity_count = 0;
2197 if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name,
2203 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2204 stream = mail_close(stream);
2208 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))
2209 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2211 /* it is duplicated with emstorage_get_pbd_activity_data
2212 if (false == emstorage_get_mailbox_pbd_activity_count(pbd_evnet->multi_user_name, account_list[m], mailbox_list[i], &activity_count, false, &error)) {
2213 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
2217 if (activity_count > 0 && activity_data_list) {
2218 int temp_error = EMAIL_ERROR_NONE;
2222 int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2226 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))
2227 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2229 if (NULL == activity_data_list) {
2230 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
2234 remainder = activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2235 iter = activity_count/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1 : 0);
2237 for (j = 0; j < iter; j++) {
2238 if ((iter == (j+1)) && (remainder != 0))
2241 /*Call bulk download here */
2242 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
2243 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2244 temp_error = EMAIL_ERROR_NO_RESPONSE;
2249 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
2250 temp_error = EMAIL_ERROR_NONE;
2255 for (k = 0; k < activity_count; k++)
2256 emcore_free_partial_body_thd_event(activity_data_list + k, &error);
2258 EM_SAFE_FREE(activity_data_list);
2260 /*check: empty check required?*/
2261 if (false == emcore_is_partial_body_thd_que_empty()) {
2263 goto FINISH_OFF; /* Stop Local Activity Sync */
2266 stream = mail_close(stream);
2270 /* After completing one cycle of local activity sync ,
2271 local activity continue variable should be set to false. */
2273 emcore_pb_thd_set_local_activity_continue(false);
2274 } 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 */ {
2275 /*Check if events have arrived in g_partial_body_thd_event_que */
2276 if (false == emcore_is_partial_body_thd_que_empty()) {
2277 EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
2279 goto FINISH_OFF; /* Stop Local Activity Sync */
2282 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2283 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2292 EM_SAFE_FREE(mailbox_list);
2293 EM_SAFE_FREE(account_list);
2294 stream = mail_close(stream);
2297 *error_code = error;
2298 EM_DEBUG_FUNC_END("ret [%d]", ret);
2302 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */