4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File : email-core-event_data.h
26 * Desc : Mail Engine Event
31 * 2006.08.16 : created
32 *****************************************************************************/
39 #include <contacts-svc.h>
41 #include "email-convert.h"
42 #include "email-storage.h"
43 #include "email-network.h"
44 #include "email-device.h"
45 #include "email-utilities.h"
46 #include "email-daemon-auto-poll.h"
47 #include "email-core-global.h"
48 #include "email-core-account.h"
49 #include "email-core-event.h"
50 #include "email-core-utils.h"
51 #include "email-core-mailbox.h"
52 #include "email-core-imap-mailbox.h"
53 #include "email-core-mail.h"
54 #include "email-core-mailbox-sync.h"
55 #include "email-core-smtp.h"
56 #include "email-core-utils.h"
57 #include "email-core-sound.h"
58 #include "email-debug-log.h"
61 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
63 /*[h.gahlaut] - All static variable declaration for partial body download thread are done here */
65 #define TOTAL_PARTIAL_BODY_EVENTS 100
67 static email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
69 static int g_partial_body_thd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
70 static int g_partial_body_thd_loop = 1; /* Variable to make a continuos while loop */
71 static int g_partial_body_thd_queue_empty = true; /* Variable to determine if event queue is empty.True means empty*/
72 static int g_partial_body_thd_queue_full = false; /* Variable to determine if event queue is full. True means full*/
73 static int g_pb_thd_local_activity_continue = true; /* Variable to control local activity sync */
74 int g_pbd_thd_state = false; /* false : thread is sleeping , true : thread is working */
76 static pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect event queue */
77 static pthread_cond_t _partial_body_thd_cond = PTHREAD_COND_INITIALIZER; /* Condition variable on which partial body thread is waiting */
78 thread_t g_partial_body_thd ; /* Determines if thread is created or not. Non Null means thread is created */
80 email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
81 static int g_partial_body_bulk_dwd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
82 static int g_partial_body_bulk_dwd_queue_empty = true;
84 static pthread_mutex_t _state_variables_lock;
86 /*[h.gahlaut] - All static function declaration for partial body download thread are done here */
88 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code);
89 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
90 static void emcore_pb_thd_set_local_activity_continue(int flag);
91 static int emcore_set_pbd_thd_state(int flag);
92 static int emcore_clear_bulk_pbd_que(int *err_code);
93 int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code);
97 #ifdef ENABLE_IMAP_IDLE_THREAD
98 extern int g_imap_idle_thread_alive;
99 extern int imap_idle_thread;
100 #endif /* ENABLE_IMAP_IDLE_THREAD */
101 INTERNAL_FUNC int g_client_count = 0;
102 INTERNAL_FUNC int g_client_run = 0 ;
104 #ifdef __FEATURE_LOCAL_ACTIVITY__
105 INTERNAL_FUNC int g_local_activity_run = 0;
106 INTERNAL_FUNC int g_save_local_activity_run = 0;
110 #define EVENT_QUEUE_MAX 32
112 typedef struct EVENT_CALLBACK_ELEM
114 email_event_callback callback;
116 struct EVENT_CALLBACK_ELEM *next;
117 } EVENT_CALLBACK_LIST;
119 static pthread_mutex_t _event_available_lock = PTHREAD_MUTEX_INITIALIZER;
120 static pthread_cond_t _event_available_signal = PTHREAD_COND_INITIALIZER;
121 static pthread_mutex_t *_event_queue_lock = NULL;
122 static pthread_mutex_t *_event_callback_table_lock = NULL;
123 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM]; /* array of singly-linked list for event callbacks */
125 void *thread_func_branch_command(void *arg);
127 static email_event_t g_event_que[EVENT_QUEUE_MAX];
129 int send_thread_run = 0;
130 int recv_thread_run = 0;
132 static pthread_mutex_t _send_event_available_lock = PTHREAD_MUTEX_INITIALIZER;
133 static pthread_mutex_t *_send_event_queue_lock = NULL; /* MUST BE "recursive" */
134 static pthread_cond_t _send_event_available_signal = PTHREAD_COND_INITIALIZER;
135 static thread_t g_send_srv_thread;
136 static thread_t g_srv_thread;
138 static email_event_t g_send_event_que[EVENT_QUEUE_MAX];
139 static int g_send_event_que_idx = 1;
140 static int g_send_event_loop = 1;
141 static int g_send_active_que = 0;
142 static int g_event_que_idx = 1;
143 static int g_event_loop = 1;
144 static int g_active_que = 0;
145 static int g_sending_busy_cnt = 0;
146 static int g_receiving_busy_cnt = 0;
149 INTERNAL_FUNC int emcore_get_current_thread_type()
151 EM_DEBUG_FUNC_BEGIN();
152 thread_t thread_id = THREAD_SELF();
153 int thread_type = -1;
155 if (thread_id == g_srv_thread)
156 thread_type = _SERVICE_THREAD_TYPE_RECEIVING;
157 else if (thread_id == g_send_srv_thread)
158 thread_type = _SERVICE_THREAD_TYPE_SENDING;
159 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
160 else if (thread_id == g_partial_body_thd)
161 thread_type = _SERVICE_THREAD_TYPE_PBD;
163 EM_DEBUG_FUNC_END("thread_type [%d]", thread_type);
167 static int is_gdk_lock_needed()
170 return (THREAD_SELF() == g_srv_thread);
175 INTERNAL_FUNC int emcore_get_pending_event(email_action_t action, int account_id, int mail_id, email_event_status_type_t *status)
177 EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
182 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
184 for (i = 1; i < EVENT_QUEUE_MAX; i++) {
185 switch (g_event_que[i].type) {
186 case EMAIL_EVENT_SEND_MAIL:
187 case EMAIL_EVENT_SEND_MAIL_SAVED:
188 if (action == EMAIL_ACTION_SEND_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
194 case EMAIL_EVENT_SYNC_HEADER:
195 if (action == EMAIL_ACTION_SYNC_HEADER && account_id == g_event_que[i].account_id) {
201 case EMAIL_EVENT_SYNC_HEADER_OMA:
202 if (action == EMAIL_ACTION_SYNC_HEADER_OMA && account_id == g_event_que[i].account_id) {
208 case EMAIL_EVENT_DOWNLOAD_BODY:
209 if (action == EMAIL_ACTION_DOWNLOAD_BODY && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
214 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
215 if (action == EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
220 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
221 if (action == EMAIL_ACTION_SYNC_FLAGS_FIELD_TO_SERVER && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
226 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
227 if (action == EMAIL_ACTION_DOWNLOAD_ATTACHMENT && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
232 case EMAIL_EVENT_DELETE_MAIL:
233 case EMAIL_EVENT_DELETE_MAIL_ALL:
234 if (action == EMAIL_ACTION_DELETE_MAIL && account_id == g_event_que[i].account_id) {
240 case EMAIL_EVENT_CREATE_MAILBOX:
241 if (action == EMAIL_ACTION_CREATE_MAILBOX && account_id == g_event_que[i].account_id) {
247 case EMAIL_EVENT_DELETE_MAILBOX:
248 if (action == EMAIL_ACTION_DELETE_MAILBOX && account_id == g_event_que[i].account_id) {
254 case EMAIL_EVENT_MOVE_MAIL:
255 if (action == EMAIL_ACTION_MOVE_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
261 case EMAIL_EVENT_VALIDATE_ACCOUNT:
262 if (action == EMAIL_ACTION_VALIDATE_ACCOUNT && account_id == g_event_que[i].account_id) {
268 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
269 if (action == EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT && account_id == 0) {
275 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
276 if (action == EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT && account_id == 0) {
282 case EMAIL_EVENT_UPDATE_MAIL:
283 if (action == EMAIL_ACTION_UPDATE_MAIL) {
289 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
290 if (action == EMAIL_ACTION_SET_MAIL_SLOT_SIZE) {
296 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
297 if (action == EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED) {
303 case EMAIL_EVENT_SEARCH_ON_SERVER:
304 if (action == EMAIL_ACTION_SEARCH_ON_SERVER && account_id == g_event_que[i].account_id) {
310 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
311 if (action == EMAIL_ACTION_MOVE_MAILBOX && account_id == g_event_que[i].account_id) {
323 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
327 *status = g_event_que[i].status;
335 INTERNAL_FUNC void emcore_get_event_queue_status(int *on_sending, int *on_receiving)
337 if (on_sending != NULL)
338 *on_sending = g_sending_busy_cnt;
340 if (on_receiving != NULL)
341 *on_receiving = g_receiving_busy_cnt;
344 static void _sending_busy_ref(void)
346 g_sending_busy_cnt++;
349 static void _sending_busy_unref(void)
351 g_sending_busy_cnt--;
354 static void _receiving_busy_ref(void)
356 g_receiving_busy_cnt++;
359 static void _receiving_busy_unref(void)
361 g_receiving_busy_cnt--;
364 static void waiting_status_notify(email_event_t *event_data, int queue_idx)
366 EM_DEBUG_FUNC_BEGIN("event_data[%p], queue_idx[%d]", event_data, queue_idx);
368 int account_id = event_data->account_id;
369 int mail_id = event_data->event_param_data_4; /* NOT ALWAYS */
371 switch (event_data->type) {
372 case EMAIL_EVENT_SEND_MAIL:
373 emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
376 case EMAIL_EVENT_SYNC_HEADER:
377 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
380 case EMAIL_EVENT_SYNC_HEADER_OMA:
381 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER_OMA, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
384 case EMAIL_EVENT_DOWNLOAD_BODY:
385 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
388 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
389 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
390 emcore_execute_event_callback(EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
394 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
395 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
398 case EMAIL_EVENT_DELETE_MAIL:
399 case EMAIL_EVENT_DELETE_MAIL_ALL:
400 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
403 case EMAIL_EVENT_VALIDATE_ACCOUNT:
404 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
407 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
408 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
411 case EMAIL_EVENT_MOVE_MAIL:
412 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAIL, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
415 case EMAIL_EVENT_CREATE_MAILBOX:
416 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
419 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
420 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
423 case EMAIL_EVENT_UPDATE_MAIL:
426 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
427 emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
430 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
431 emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_WAITING, account_id, event_data->event_param_data_4, queue_idx, EMAIL_ERROR_NONE);
434 case EMAIL_EVENT_SEARCH_ON_SERVER:
435 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
438 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
439 /* emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE); */
448 static void fail_status_notify(email_event_t *event_data, int error)
450 EM_DEBUG_FUNC_BEGIN();
451 int account_id, mail_id;
454 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
457 account_id = event_data->account_id;
458 mail_id = event_data->event_param_data_4;
460 EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
462 switch (event_data->type) {
463 case EMAIL_EVENT_SEND_MAIL:
464 /* case EMAIL_EVENT_SEND_MAIL_SAVED: */
465 /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
466 emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, error);
469 case EMAIL_EVENT_SYNC_HEADER:
470 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
471 emcore_show_user_message(account_id, EMAIL_ACTION_SYNC_HEADER, error);
474 case EMAIL_EVENT_DOWNLOAD_BODY:
475 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
476 emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
479 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
480 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
481 emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
484 case EMAIL_EVENT_DELETE_MAIL:
485 case EMAIL_EVENT_DELETE_MAIL_ALL:
486 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
487 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, error);
490 case EMAIL_EVENT_VALIDATE_ACCOUNT:
491 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
492 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
495 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
496 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
497 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
500 case EMAIL_EVENT_CREATE_MAILBOX:
501 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
502 emcore_show_user_message(account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
505 case EMAIL_EVENT_DELETE_MAILBOX:
506 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
507 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
510 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
511 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
512 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
515 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
516 emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
519 case EMAIL_EVENT_SEARCH_ON_SERVER:
520 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
521 emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
524 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
525 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
526 emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
529 case EMAIL_EVENT_UPDATE_MAIL:
530 emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
533 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
534 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);
544 static void emcore_initialize_event_callback_table()
546 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
550 for (i = 0; i < EMAIL_ACTION_NUM; i++)
551 _event_callback_table[i] = NULL;
553 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
556 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
558 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
560 if (callback == NULL)
565 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
567 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
569 while (node != NULL) {
570 if (node->callback == callback && node->event_data == event_data) /* already registered */
576 /* not found, so keep going */
578 node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
580 if (node == NULL) /* not enough memory */
583 node->callback = callback;
584 node->event_data = event_data;
585 node->next = _event_callback_table[action];
587 _event_callback_table[action] = node;
592 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
597 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
599 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
601 if (callback == NULL)
606 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
608 EVENT_CALLBACK_LIST *prev = NULL;
609 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
611 while (node != NULL) {
612 if (node->callback == callback) {
614 prev->next = node->next;
616 _event_callback_table[action] = node->next;
628 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
633 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)
635 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);
638 lock_needed = is_gdk_lock_needed();
641 /* Todo : g_thread_yield */
644 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
646 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
648 while (node != NULL) {
649 if (node->callback != NULL)
650 node->callback(total, done, status, account_id, mail_id, (handle == -1 ? emcore_get_active_queue_idx() : handle), node->event_data, error);
654 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
661 /* insert a event to event queue */
662 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
664 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
667 EM_DEBUG_EXCEPTION("Invalid Parameter");
668 if (err_code != NULL)
669 *err_code = EMAIL_ERROR_INVALID_PARAM;
674 EM_DEBUG_EXCEPTION("email-service is not ready");
675 if (err_code != NULL)
676 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
681 int error = EMAIL_ERROR_NONE;
683 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
685 if (!g_event_que[g_event_que_idx].type) { /* if current buffer has not event, insert event data to current buffer */
686 EM_DEBUG_LOG("Current buffer has not a event. [%d]", g_event_que_idx);
687 memcpy(g_event_que+g_event_que_idx, event_data, sizeof(email_event_t));
688 g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
689 waiting_status_notify(event_data, g_event_que_idx);
691 *handle = g_event_que_idx;
693 else { /* if current buffer has event, find the empty buffer */
694 EM_DEBUG_LOG("Current buffer has a event. [%d]", g_event_que_idx);
695 int i, j = g_event_que_idx + 1;
697 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++) {
698 if (j >= EVENT_QUEUE_MAX)
701 if (!g_event_que[j].type)
705 if (i < EVENT_QUEUE_MAX) {
706 EM_DEBUG_LOG("I found available buffer. [%d]", g_event_que + j);
707 memcpy(g_event_que+j, event_data, sizeof(email_event_t));
708 g_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
709 waiting_status_notify(event_data, j);
715 EM_DEBUG_EXCEPTION("event que is full...");
716 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
721 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
724 event_data->event_param_data_1 = NULL; /* MUST BE - to prevent double-free */
726 switch (event_data->type) {
727 case EMAIL_EVENT_SEND_MAIL:
728 case EMAIL_EVENT_SEND_MAIL_SAVED:
732 case EMAIL_EVENT_SYNC_HEADER:
733 case EMAIL_EVENT_DOWNLOAD_BODY:
734 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
735 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
736 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
737 case EMAIL_EVENT_ISSUE_IDLE:
738 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
739 case EMAIL_EVENT_VALIDATE_ACCOUNT:
740 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
741 case EMAIL_EVENT_MOVE_MAIL:
742 case EMAIL_EVENT_DELETE_MAIL:
743 case EMAIL_EVENT_DELETE_MAIL_ALL:
744 case EMAIL_EVENT_SYNC_HEADER_OMA:
745 case EMAIL_EVENT_CREATE_MAILBOX:
746 case EMAIL_EVENT_DELETE_MAILBOX:
747 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
748 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
749 case EMAIL_EVENT_UPDATE_MAIL:
750 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
751 case EMAIL_EVENT_SEARCH_ON_SERVER:
752 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
753 _receiving_busy_ref();
759 ENTER_CRITICAL_SECTION(_event_available_lock);
760 WAKE_CONDITION_VARIABLE(_event_available_signal);
761 LEAVE_CRITICAL_SECTION(_event_available_lock);
764 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
766 int is_local_activity_event_inserted = false;
768 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &error))
769 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
771 if (true == is_local_activity_event_inserted)
772 emcore_pb_thd_set_local_activity_continue(false);
777 if (err_code != NULL)
780 EM_DEBUG_LOG("Finish with [%d]", ret);
784 /* get a event from event_data queue */
785 static int emcore_retrieve_event(email_event_t *event_data, int *err_code)
787 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
790 int error = EMAIL_ERROR_NONE;
792 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
794 /* get a event_data if this queue is not empty */
795 if (g_event_que[g_event_que_idx].type) {
796 memcpy(event_data, g_event_que+g_event_que_idx, sizeof(email_event_t));
798 if (event_data->status != EMAIL_EVENT_STATUS_WAIT) { /* EMAIL_EVENT_STATUS_CANCELED */
799 memset(g_event_que+g_event_que_idx, 0x00, sizeof(email_event_t));
804 g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
805 g_active_que = g_event_que_idx;
809 if (++g_event_que_idx >= EVENT_QUEUE_MAX)
812 EM_DEBUG_LOG("g_event_que[%d].type [%d]", g_active_que, g_event_que[g_active_que].type);
816 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
819 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
821 if (err_code != NULL)
824 EM_DEBUG_FUNC_END("ret [%d]", ret);
828 /* check that event_data loop is continuous */
829 static int emcore_event_loop_continue()
836 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
838 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
841 EM_DEBUG_EXCEPTION("\t event_data[%p], handle[%p]", event_data, handle);
843 if (err_code != NULL)
844 *err_code = EMAIL_ERROR_INVALID_PARAM;
849 int error = EMAIL_ERROR_NONE;
851 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
853 if (!g_send_event_que[g_send_event_que_idx].type) {
854 /* if current buffer has not event_data, insert event_data data to current buffer */
855 EM_DEBUG_LOG("Current buffer has not a event_data. [%d]", g_send_event_que_idx);
856 memcpy(g_send_event_que+g_send_event_que_idx, event_data, sizeof(email_event_t));
858 g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
861 *handle = g_send_event_que_idx;
864 /* if current buffer has event_data, find the empty buffer */
865 EM_DEBUG_LOG("Current buffer has a event_data. [%d]", g_send_event_que_idx);
866 int i, j = g_send_event_que_idx + 1;
868 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++) {
869 if (j >= EVENT_QUEUE_MAX)
872 if (!g_send_event_que[j].type)
876 if (i < EVENT_QUEUE_MAX) {
877 EM_DEBUG_LOG("I found available buffer. [%d]", j);
878 memcpy(g_send_event_que+j, event_data, sizeof(email_event_t));
879 g_send_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
880 if (handle) *handle = j;
883 EM_DEBUG_EXCEPTION("event_data queue is full...");
884 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
889 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
892 ENTER_CRITICAL_SECTION(_send_event_available_lock);
893 WAKE_CONDITION_VARIABLE(_send_event_available_signal);
894 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
898 EM_DEBUG_LOG("emcore_insert_event_for_sending_mails-handle[%d]", *handle);
900 if (err_code != NULL)
903 /* EM_DEBUG_FUNC_BEGIN(); */
908 static int emcore_retrieve_send_event(email_event_t *event_data, int *err_code)
910 EM_DEBUG_FUNC_BEGIN();
913 int error = EMAIL_ERROR_NONE;
915 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
916 /* get a event_data if this queue is not empty */
917 if (g_send_event_que[g_send_event_que_idx].type) {
918 memcpy(event_data, g_send_event_que+g_send_event_que_idx, sizeof(email_event_t));
920 if (event_data->status != EMAIL_EVENT_STATUS_WAIT) {
921 memset(g_send_event_que+g_send_event_que_idx, 0x00, sizeof(email_event_t));
922 g_send_active_que = 0;
925 g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
926 EM_DEBUG_LOG("g_send_event_que_idx[%d]", g_send_event_que_idx);
927 g_send_active_que = g_send_event_que_idx;
932 if (++g_send_event_que_idx >= EVENT_QUEUE_MAX)
933 g_send_event_que_idx = 1;
935 EM_DEBUG_LOG("\t g_send_event_que[%d].type = %d", g_send_active_que, g_send_event_que[g_send_active_que].type);
938 EM_DEBUG_LOG("\t send event_data queue is empty...");
939 g_send_active_que = 0;
940 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
943 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
945 if (err_code != NULL)
952 void* thread_func_branch_command_for_sending_mails(void *arg)
954 EM_DEBUG_FUNC_BEGIN();
955 int err = EMAIL_ERROR_NONE;
956 email_event_t event_data;
957 email_session_t *session = NULL;
959 if (!emstorage_open(&err)) {
960 EM_DEBUG_EXCEPTION("\t emstorage_open falied - %d", err);
964 while (g_send_event_loop) {
965 if (!emcore_get_empty_session(&session))
966 EM_DEBUG_EXCEPTION("\t SEND THREAD emcore_get_empty_session failed...");
968 if (!emcore_retrieve_send_event(&event_data, NULL)) {
969 EM_DEBUG_LOG(">>>> waiting for send event_data>>>>>>>>>");
970 #ifdef __FEATURE_LOCAL_ACTIVITY__
971 if (send_thread_run && g_save_local_activity_run) {
972 emstorage_account_tbl_t *account_list = NULL;
974 if (!emstorage_get_account_list(&count, &account_list, true, true, &err)) {
975 EM_DEBUG_LOG("\t emstorage_get_account_list failed - %d", err);
978 for (i = 0; i < count; i++) {
979 if (emcore_save_local_activity_sync(account_list[i].account_id, &err)) {
980 EM_DEBUG_LOG("Found local activity...!");
981 EM_DEBUG_LOG("Resetting g_save_local_activity_run ");
982 g_save_local_activity_run = 0;
983 emcore_clear_session(session);
987 emstorage_free_account(&account_list, count, &err);
989 if (!g_save_local_activity_run) {
997 ENTER_CRITICAL_SECTION(_send_event_available_lock);
998 SLEEP_CONDITION_VARIABLE(_send_event_available_signal, _send_event_available_lock);
999 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
1002 EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
1003 send_thread_run = 1;
1005 email_option_t *option = NULL;
1007 if (!emnetwork_check_network_status( &err)) {
1008 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1010 emcore_show_user_message(event_data.event_param_data_4, EMAIL_ACTION_SEND_MAIL, err);
1011 if (!emstorage_notify_network_event(NOTI_SEND_FAIL, event_data.account_id, NULL , event_data.event_param_data_4, err))
1012 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_SEND_FAIL] Failed >>>> ");
1016 switch (event_data.type) {
1018 case EMAIL_EVENT_SEND_MAIL:
1019 emdevice_set_dimming_on_off(false, NULL);
1020 option = (email_option_t *)event_data.event_param_data_1;
1022 if (!emcore_send_mail(event_data.account_id, event_data.event_param_data_5, event_data.event_param_data_4, option, &err))
1023 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
1026 EM_SAFE_FREE(option->display_name_from);
1027 emdevice_set_dimming_on_off(true, NULL);
1030 case EMAIL_EVENT_SEND_MAIL_SAVED: /* send mails to been saved in offline-mode */
1031 emdevice_set_dimming_on_off(false, NULL);
1033 email_option_t *option = (email_option_t *)event_data.event_param_data_1;
1035 if (!emcore_send_saved_mail(event_data.account_id, event_data.event_param_data_3, option, &err))
1036 EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);
1039 EM_SAFE_FREE(option->display_name_from);
1041 emdevice_set_dimming_on_off(true, NULL);
1043 #ifdef __FEATURE_LOCAL_ACTIVITY__
1045 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1046 emdevice_set_dimming_on_off(false, NULL);
1047 emstorage_activity_tbl_t *local_activity = NULL;
1048 int activity_id_count = 0;
1049 int activity_chunk_count = 0;
1050 int *activity_id_list = NULL;
1053 if (false == emstorage_get_activity_id_list(event_data.account_id, &activity_id_list, &activity_id_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1054 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1057 for (i = 0; i < activity_id_count; ++i) {
1058 if ((false == emstorage_get_activity(event_data.account_id, activity_id_list[i], &local_activity, &activity_chunk_count, true, &err)) || (NULL == local_activity) || (0 == activity_chunk_count)) {
1059 EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
1062 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1063 switch (local_activity[0].activity_type) {
1064 case ACTIVITY_SAVEMAIL: {
1065 if (!emcore_sync_mail_from_client_to_server(event_data.account_id, local_activity[0].mail_id, &err)) {
1066 EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
1071 case ACTIVITY_DELETEMAIL_SEND: /* New Activity Type Added for Race Condition and Crash Fix */ {
1072 if (!emcore_delete_mail(local_activity[0].account_id,
1073 &local_activity[0].mail_id,
1074 EMAIL_DELETE_FOR_SEND_THREAD,
1076 EMAIL_DELETED_BY_COMMAND,
1079 EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1085 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1090 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1092 if (g_save_local_activity_run == 1) {
1093 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1099 if (false == emstorage_free_activity_id_list(activity_id_list, &err)) {
1100 EM_DEBUG_LOG("emstorage_free_activity_id_list failed");
1104 emdevice_set_dimming_on_off(true, NULL);
1107 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1109 EM_DEBUG_LOG("Others not supported by Send Thread..! ");
1114 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1116 memset(g_send_event_que+g_send_active_que, 0x00, sizeof(email_event_t));
1118 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1123 emcore_clear_session(session);
1126 if (!emstorage_close(&err))
1127 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
1129 EM_DEBUG_FUNC_END();
1134 int event_handler_EMAIL_EVENT_SYNC_HEADER(int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error)
1136 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", input_account_id, input_mailbox_id, handle_to_be_published, error);
1138 int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
1139 int mailbox_count = 0, account_count = 0;
1140 int counter, account_index;
1141 int unread = 0, total_unread = 0;
1142 emcore_uid_list *uid_list = NULL;
1143 emstorage_account_tbl_t *account_tbl_array = NULL;
1144 emstorage_mailbox_tbl_t *mailbox_tbl_target = NULL, *mailbox_tbl_spam = NULL, *mailbox_tbl_list = NULL;
1145 #ifndef __FEATURE_KEEP_CONNECTION__
1146 MAILSTREAM *stream = NULL;
1148 char mailbox_id_param_string[10] = {0,};
1150 if (input_mailbox_id == 0)
1151 sync_type = EMAIL_SYNC_ALL_MAILBOX;
1153 if (!emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
1154 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1158 if(mailbox_tbl_target)
1159 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_target->mailbox_id);
1161 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, (input_mailbox_id == 0 )? NULL : mailbox_id_param_string, handle_to_be_published, 0))
1162 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
1164 if (!emnetwork_check_network_status(&err)) {
1165 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1167 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, (input_mailbox_id == 0 )? NULL : mailbox_id_param_string, handle_to_be_published, err))
1168 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1171 if (sync_type != EMAIL_SYNC_ALL_MAILBOX) { /* Sync only particular mailbox */
1173 if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1174 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1176 if (!emstorage_get_mailbox_by_mailbox_type(input_account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1177 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1180 if (!emcore_sync_header(mailbox_tbl_target, mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1181 EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
1182 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
1183 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1186 EM_DEBUG_LOG("emcore_sync_header succeeded [%d]", err);
1187 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
1188 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1191 total_unread += unread;
1193 if (total_unread > 0 && (err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1194 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1196 if (!emcore_finalize_sync(input_account_id, &err))
1197 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1199 else /* All Foder */ {
1200 EM_DEBUG_LOG(">>>> SYNC ALL MAILBOX ");
1201 /* Sync of all mailbox */
1203 if (input_account_id == ALL_ACCOUNT) {
1204 if ((err = emcore_update_sync_status_of_account(ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1205 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1207 if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &err)) {
1208 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
1209 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL, handle_to_be_published, err))
1210 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1215 EM_DEBUG_LOG("Sync all mailbox of an account[%d].", input_account_id);
1217 if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1218 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1220 if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
1221 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
1222 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, (input_mailbox_id == 0 )? NULL : mailbox_id_param_string, handle_to_be_published, err))
1223 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1229 for (account_index = 0 ; account_index < account_count; account_index++) {
1230 if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1231 EM_DEBUG_LOG("account[%d] is for ActiveSync. Skip ", account_index);
1235 if (!emstorage_get_mailbox_list(account_tbl_array[account_index].account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || mailbox_count <= 0) {
1236 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
1238 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, (input_mailbox_id == 0 )? NULL : mailbox_id_param_string, handle_to_be_published, err))
1239 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1244 EM_DEBUG_LOG("emcore_get_mailbox_list_to_be_sync returns [%d] mailboxes", mailbox_count);
1246 if(mailbox_tbl_spam) {
1247 if (!emstorage_free_mailbox(&mailbox_tbl_spam, 1, &err)) {
1248 EM_DEBUG_EXCEPTION("emstorage_free_mailbox failed [%d]", err);
1250 mailbox_tbl_spam = NULL;
1253 if (!emstorage_get_mailbox_by_mailbox_type(account_tbl_array[account_index].account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1254 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1257 if (mailbox_count > 0) {
1258 #ifndef __FEATURE_KEEP_CONNECTION__
1259 if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1260 memset(mailbox_id_param_string, 0, 10);
1261 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
1262 if (!emcore_connect_to_remote_mailbox(account_tbl_array[account_index].account_id, mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err)) {
1263 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1264 if (err == EMAIL_ERROR_LOGIN_FAILURE)
1265 EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
1267 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string, handle_to_be_published, err))
1268 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1271 EM_DEBUG_LOG("emcore_connect_to_remote_mailbox returns [%d] : ", err);
1278 for (counter = 0; counter < mailbox_count; counter++) {
1279 if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
1280 || mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
1281 /*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */)
1282 EM_DEBUG_LOG("Skipped for all emails or trash");
1283 else if (!mailbox_tbl_list[counter].local_yn) {
1284 EM_DEBUG_LOG("..........syncing %s mailbox( mailbox_id = %d)......", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id);
1285 #ifdef __FEATURE_KEEP_CONNECTION__
1286 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1287 #else /* __FEATURE_KEEP_CONNECTION__ */
1288 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, (void *)stream, &uid_list, &unread, &err)) {
1289 #endif /* __FEATURE_KEEP_CONNECTION__ */
1290 EM_DEBUG_EXCEPTION("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, err);
1292 #ifndef __FEATURE_KEEP_CONNECTION__
1293 if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || err == EMAIL_ERROR_SOCKET_FAILURE)
1294 stream = NULL; /* Don't retry to connect for broken connection. It might cause crash. */
1295 #endif /* __FEATURE_KEEP_CONNECTION__ */
1296 memset(mailbox_id_param_string, 0, 10);
1297 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
1298 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string, handle_to_be_published, err))
1299 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1304 total_unread += unread;
1307 EM_DEBUG_LOG("Sync for account_id(%d) is completed....!", account_tbl_array[account_index].account_id);
1308 if ((err == EMAIL_ERROR_NONE) && !emstorage_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
1309 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1311 if ((total_unread > 0) && (err = emcore_update_sync_status_of_account(account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1312 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1314 if (!emcore_finalize_sync(account_tbl_array[account_index].account_id, &err))
1315 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1316 #ifndef __FEATURE_KEEP_CONNECTION__
1318 emcore_close_mailbox(0, stream);
1322 if (mailbox_tbl_list) {
1323 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1324 mailbox_tbl_list = NULL;
1334 #ifndef __FEATURE_KEEP_CONNECTION__
1336 emcore_close_mailbox(0, stream);
1338 if(mailbox_tbl_target)
1339 emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);
1341 if (mailbox_tbl_list)
1342 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1344 if (account_tbl_array)
1345 emstorage_free_account(&account_tbl_array, account_count, NULL);
1348 EM_DEBUG_FUNC_END();
1352 int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error)
1354 EM_DEBUG_FUNC_BEGIN("account [%p]", account);
1355 int err, ret = false;
1358 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1359 err = EMAIL_ERROR_INVALID_PARAM;
1363 EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);
1365 if (!emnetwork_check_network_status(&err)) {
1366 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1368 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL, handle_to_be_published, err))
1369 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1373 EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);
1375 if (!emcore_validate_account_with_account_info(account, &err)) {
1376 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err : %d", err);
1377 if (err == EMAIL_ERROR_CANCELLED) {
1378 EM_DEBUG_EXCEPTION(" notify : NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1379 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL, account->account_id, NULL, handle_to_be_published, err))
1380 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL] Failed");
1387 if (emcore_create_account(account, &err) == false) {
1388 EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
1392 emcore_refresh_account_reference();
1394 if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
1395 if (!emcore_sync_mailbox_list(account->account_id, "", &err)) {
1396 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync falied [%d]", err);
1397 /* delete account whose mailbox couldn't be obtained from server */
1398 emcore_delete_account(account->account_id, NULL);
1404 EM_DEBUG_LOG("validating and creating an account are succeeded for account id [%d] err [%d]", account->account_id, err);
1405 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, NULL, handle_to_be_published, err))
1406 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
1414 if (ret == false && err != EMAIL_ERROR_CANCELLED) {
1415 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL, handle_to_be_published, err))
1416 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1421 EM_DEBUG_FUNC_END();
1425 int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
1427 EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
1428 int err, ret = false;
1429 emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;
1431 if (!new_account_info) {
1432 EM_DEBUG_EXCEPTION("Invalid Parameter");
1433 err = EMAIL_ERROR_INVALID_PARAM;
1437 if (!emnetwork_check_network_status(&err)) {
1438 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1440 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL, handle_to_be_published, err))
1441 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
1445 EM_DEBUG_LOG("incoming_server_address: (%s)", new_account_info->incoming_server_address);
1447 if (!emcore_validate_account_with_account_info(new_account_info, &err)) {
1448 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err : %d", err);
1449 if (err == EMAIL_ERROR_CANCELLED) {
1450 EM_DEBUG_EXCEPTION(" notify : NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1451 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, new_account_info->account_id, NULL, handle_to_be_published, err))
1452 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
1460 if (!emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
1461 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1462 /* goto FINISH_OFF; */
1465 new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
1466 if (!new_account_tbl) {
1467 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
1471 em_convert_account_to_account_tbl(new_account_info, new_account_tbl);
1473 if (emstorage_update_account(account_id, new_account_tbl, true, &err)) {
1474 emcore_refresh_account_reference();
1477 EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", new_account_info->account_id, err);
1478 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, NULL, handle_to_be_published, err))
1479 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
1486 if (old_account_tbl)
1487 emstorage_free_account(&old_account_tbl, 1, NULL);
1488 if (new_account_tbl)
1489 emstorage_free_account(&new_account_tbl, 1, NULL);
1491 if (ret == false && err != EMAIL_ERROR_CANCELLED) {
1492 if (!emstorage_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL, handle_to_be_published, err))
1493 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1499 EM_DEBUG_FUNC_END();
1503 int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
1505 EM_DEBUG_FUNC_BEGIN();
1507 emcore_set_mail_slot_size(account_id, mailbox_id, new_slot_size, error);
1509 EM_DEBUG_FUNC_END();
1513 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id)
1515 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
1516 int err = EMAIL_ERROR_NONE;
1518 if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1519 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
1523 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1524 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1530 EM_DEBUG_FUNC_END("err [%d]", err);
1534 #ifdef __FEATURE_LOCAL_ACTIVITY__
1535 int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
1537 EM_DEBUG_FUNC_BEGIN();
1539 int err = EMAIL_ERROR_NONE;
1540 email_mailbox_t mailbox;
1541 emstorage_activity_tbl_t *local_activity = NULL;
1542 int activity_id_count = 0;
1543 int activity_chunk_count = 0;
1544 int *activity_id_list = NULL;
1547 if (!emnetwork_check_network_status(&err))
1548 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1550 if (false == emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1551 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1554 for (i = 0; i < activity_id_count; ++i) {
1555 if ((false == emstorage_get_activity(account_id , activity_id_list[i], &local_activity, &activity_chunk_count, true, &err)) || (NULL == local_activity) || (0 == activity_chunk_count))
1556 EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
1558 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1559 switch (local_activity[0].activity_type) {
1560 case ACTIVITY_MODIFYFLAG: {
1561 if (emcore_sync_flag_with_server(local_activity[0].mail_id , &err)) {
1562 if (!emcore_delete_activity(&local_activity[0], &err))
1563 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1568 case ACTIVITY_DELETEMAIL:
1569 case ACTIVITY_MOVEMAIL:
1570 case ACTIVITY_MODIFYSEENFLAG:
1571 case ACTIVITY_COPYMAIL: {
1574 int total_mail_ids = activity_chunk_count;
1576 int *mail_id_list = NULL;
1578 mail_id_list = (int *)em_malloc(sizeof(int) * total_mail_ids);
1580 if (NULL == mail_id_list) {
1581 EM_DEBUG_EXCEPTION("malloc failed... ");
1587 for (j = 0; j < BULK_OPERATION_COUNT && (k < total_mail_ids); ++j, ++k)
1588 mail_id_list[j] = local_activity[k].mail_id;
1590 switch (local_activity[k-1].activity_type) {
1591 case ACTIVITY_DELETEMAIL: {
1592 if (!emcore_delete_mail(local_activity[k-1].account_id,
1595 EMAIL_DELETE_LOCAL_AND_SERVER,
1596 EMAIL_DELETED_BY_COMMAND,
1599 EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1603 case ACTIVITY_MOVEMAIL: {
1604 if (!emcore_move_mail_on_server_ex(local_activity[k-1].account_id ,
1605 local_activity[k-1].src_mbox,
1608 local_activity[k-1].dest_mbox,
1610 EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
1613 case ACTIVITY_MODIFYSEENFLAG: {
1614 int seen_flag = atoi(local_activity[0].src_mbox);
1615 if (!emcore_sync_seen_flag_with_server_ex(mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
1616 EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
1621 } while (k < total_mail_ids);
1623 EM_SAFE_FREE(mail_id_list);
1629 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1633 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1635 if (g_local_activity_run == 1) {
1636 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1643 if (activity_id_list) {
1644 if (false == emstorage_free_activity_id_list(activity_id_list, &err))
1645 EM_DEBUG_EXCEPTION("emstorage_free_activity_id_list failed");
1651 EM_DEBUG_FUNC_END();
1655 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1657 int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error)
1659 EM_DEBUG_FUNC_BEGIN();
1661 int err = EMAIL_ERROR_NONE;
1662 email_mailbox_t mailbox;
1664 memset(&mailbox, 0x00, sizeof(mailbox));
1665 mailbox.account_id = account_id;
1667 if (!emnetwork_check_network_status(&err)) {
1668 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1670 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1673 if (!emcore_download_body_multi_sections_bulk(NULL,
1676 option >> 1, /* 0 : silent, 1 : verbose */
1677 option & 0x01, /* 0 : without attachments, 1 : with attachments */
1679 handle_to_be_published,
1681 EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed - %d", err);
1687 EM_DEBUG_FUNC_END();
1691 int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
1693 EM_DEBUG_FUNC_BEGIN();
1695 int err = EMAIL_ERROR_NONE;
1697 EM_DEBUG_LOG("attachment_no is %d", attachment_no);
1699 if (!emnetwork_check_network_status(&err)) {
1700 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1701 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
1705 #ifdef __ATTACHMENT_OPTI__
1706 if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, &err))
1707 EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1709 if (!emcore_download_attachment(account_id, mail_id, attachment_no, &err))
1710 EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1717 EM_DEBUG_FUNC_END();
1721 int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
1723 EM_DEBUG_FUNC_BEGIN();
1725 int err = EMAIL_ERROR_NONE;
1727 if (!emnetwork_check_network_status(&err))
1728 EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
1729 else if (!emcore_sync_flags_field_with_server(mail_ids, num, field_type, value, &err))
1730 EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
1735 EM_DEBUG_FUNC_END();
1739 int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error)
1741 EM_DEBUG_FUNC_BEGIN();
1743 int err = EMAIL_ERROR_NONE;
1745 if (!emnetwork_check_network_status(&err)) {
1746 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1748 if (!emstorage_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL, handle_to_be_published, err))
1749 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>>");
1753 if (!emcore_validate_account(account_id, &err)) {
1754 EM_DEBUG_EXCEPTION("emcore_validate_account failed account id : %d err : %d", account_id, err);
1756 if (err == EMAIL_ERROR_CANCELLED) {
1757 EM_DEBUG_EXCEPTION("notify : NOTI_VALIDATE_ACCOUNT_CANCEL ");
1758 if (!emstorage_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, account_id, NULL, handle_to_be_published, err))
1759 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
1762 if (!emstorage_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL, handle_to_be_published, err))
1763 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1767 email_account_t *account_ref;
1768 account_ref = emcore_get_account_reference(account_id);
1771 EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
1772 if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
1773 if (!emcore_check_thread_status())
1774 err = EMAIL_ERROR_CANCELLED;
1775 else if (!emcore_sync_mailbox_list(account_id, "", &err))
1776 EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
1780 EM_DEBUG_EXCEPTION("emcore_validate_account succeeded account id : %d err : %d", account_id, err);
1781 if (!emstorage_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, account_id, NULL, handle_to_be_published, err))
1782 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success >>>>");
1791 EM_DEBUG_FUNC_END();
1795 int event_handler_EMAIL_EVENT_UPDATE_MAIL(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published)
1797 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
1798 int err = EMAIL_ERROR_NONE;
1800 if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
1801 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1804 emcore_free_mail_data_list(&input_mail_data, 1);
1806 if(input_attachment_data_list)
1807 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1809 if(input_meeting_request)
1810 emstorage_free_meeting_request(input_meeting_request);
1812 EM_DEBUG_FUNC_END("err [%d", err);
1816 int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error)
1818 EM_DEBUG_FUNC_BEGIN();
1819 int err = EMAIL_ERROR_NONE, ret = false;
1820 email_account_t *account_ref = NULL;
1822 if (!(account_ref = emcore_get_account_reference(account_id))) {
1823 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1824 err = EMAIL_ERROR_INVALID_ACCOUNT;
1828 /* Move mail local */
1830 if (!emcore_mail_move(mail_ids, mail_id_count, dest_mailbox.mailbox_name, EMAIL_MOVED_BY_COMMAND, 0, &err)) {
1831 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
1836 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1837 /* Move mail on server */
1838 if (!emnetwork_check_network_status(&err))
1839 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1841 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1842 if (!emcore_move_mail_on_server_ex(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1843 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
1845 if (!emcore_move_mail_on_server(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1846 EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
1857 EM_DEBUG_FUNC_END();
1861 int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int handle_to_be_published, int *error)
1863 EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], on_server [%d], handle_to_be_published [%d], error [%p]", mailbox_id, on_server, handle_to_be_published, error);
1864 int err = EMAIL_ERROR_NONE;
1866 if (!emnetwork_check_network_status(&err))
1867 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1869 if (!emcore_delete_mailbox(mailbox_id, on_server, &err))
1870 EM_DEBUG_LOG("emcore_delete failed - %d", err);
1876 EM_DEBUG_FUNC_END();
1880 int event_handler_EMAIL_EVENT_CREATE_MAILBOX(int account_id, char *mailbox_name, char *mailbox_alias, int mailbox_type, int on_server, int handle_to_be_published, int *error)
1882 EM_DEBUG_FUNC_BEGIN();
1883 int err = EMAIL_ERROR_NONE;
1884 email_mailbox_t mailbox;
1886 memset(&mailbox, 0x00, sizeof(mailbox));
1888 mailbox.account_id = account_id;
1889 mailbox.mailbox_name = mailbox_name;
1890 mailbox.alias = mailbox_alias;
1891 mailbox.mailbox_type = mailbox_type;
1893 if (!emnetwork_check_network_status(&err)) {
1894 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1897 if (!emcore_create_mailbox(&mailbox, on_server, &err))
1898 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
1905 EM_DEBUG_FUNC_END();
1909 int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error)
1911 EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);
1913 int err = EMAIL_ERROR_NONE;
1915 if (!emnetwork_check_network_status(&err))
1916 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1918 if (!emcore_sync_flag_with_server(mail_id, &err))
1919 EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
1920 #ifdef __FEATURE_LOCAL_ACTIVITY__
1922 emstorage_activity_tbl_t new_activity;
1923 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1924 new_activity.activity_type = ACTIVITY_MODIFYFLAG;
1925 new_activity.account_id = event_data.account_id;
1926 new_activity.mail_id = event_data.event_param_data_4;
1927 new_activity.dest_mbox = NULL;
1928 new_activity.server_mailid = NULL;
1929 new_activity.src_mbox = NULL;
1931 if (!emcore_delete_activity(&new_activity, &err))
1932 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1934 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1940 EM_DEBUG_FUNC_END();
1944 int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_mailbox_id, int input_from_server, int *error)
1946 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_from_server [%d], error [%p]", input_mailbox_id, input_from_server, error);
1947 int err = EMAIL_ERROR_NONE;
1949 if (!emcore_delete_all_mails_of_mailbox(input_mailbox_id, input_from_server, &err))
1950 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
1955 EM_DEBUG_FUNC_END("err [%d]", err);
1959 int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error)
1961 EM_DEBUG_FUNC_BEGIN();
1962 int err = EMAIL_ERROR_NONE;
1964 email_account_t *account_ref = NULL;
1966 if (!(account_ref = emcore_get_account_reference(account_id))) {
1967 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1968 err = EMAIL_ERROR_INVALID_ACCOUNT;
1972 if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_FROM_SERVER, EMAIL_DELETED_BY_COMMAND, false, &err)) {
1973 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
1983 EM_DEBUG_FUNC_END();
1987 int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int *error)
1989 EM_DEBUG_FUNC_BEGIN();
1990 int err = EMAIL_ERROR_NONE;
1992 if (!emnetwork_check_network_status(&err)) {
1993 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1994 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_FAIL, account_id, maibox_name, 0, err))
1995 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
1998 EM_DEBUG_LOG("Sync of all mailbox");
1999 if (!emcore_sync_mailbox_list(account_id, "", &err))
2000 EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2006 EM_DEBUG_FUNC_END();
2010 int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, int handle_to_be_published, int *error)
2012 EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], criteria : [%s]", account_id, mailbox_id, criteria);
2014 int err = EMAIL_ERROR_NONE;
2018 char temp_uid_string[20] = {0,};
2020 emcore_uid_list uid_elem;
2021 emstorage_mailbox_tbl_t *search_mailbox = NULL;
2022 emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
2024 MAILSTREAM *stream = NULL;
2025 MESSAGECACHE *mail_cache_element = NULL;
2026 ENVELOPE *env = NULL;
2027 emstorage_mailbox_tbl_t* local_mailbox = NULL;
2028 char mailbox_id_param_string[10] = {0,};
2030 if (account_id < 0 || mailbox_id == 0) {
2031 EM_DEBUG_EXCEPTION("Invalid parameter");
2032 err = EMAIL_ERROR_INVALID_PARAM;
2036 if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
2037 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
2041 SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);
2043 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_START, account_id, mailbox_id_param_string, handle_to_be_published, 0))
2044 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_START] failed >>>>");
2046 if (!emnetwork_check_network_status(&err)) {
2047 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2048 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, 0, err))
2049 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
2053 if (!emcore_connect_to_remote_mailbox(account_id, mailbox_id, (void **)&stream, &err)) {
2054 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
2055 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2056 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2060 if (!mail_search_full(stream, NIL, mail_criteria(criteria), SE_FREE)) {
2061 EM_DEBUG_EXCEPTION("mail_search failed");
2062 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2063 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2067 for (i = 1; i <= stream->nmsgs; ++i) {
2068 mail_cache_element = mail_elt(stream, i);
2069 if (mail_cache_element->searched) {
2070 env = mail_fetchstructure_full(stream, i, NULL, FT_PEEK);
2072 memset(&uid_elem, 0x00, sizeof(uid_elem));
2074 uid_elem.msgno = mail_cache_element->msgno;
2075 SNPRINTF(temp_uid_string, 20, "%4lu", mail_cache_element->private.uid);
2076 uid_elem.uid = temp_uid_string;
2077 uid_elem.flag.seen = mail_cache_element->seen;
2079 if (!emcore_make_mail_tbl_data_from_envelope(stream, env, &uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
2080 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
2081 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2082 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2086 if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SEARCH_RESULT, &search_mailbox, false, &err)) {
2087 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
2088 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2089 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2093 if ((err = emcore_add_mail_to_mailbox(search_mailbox, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
2094 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
2095 if (!emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2096 EM_DEBUG_EXCEPTION("emstorage_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2099 memset(mailbox_id_param_string, 0, 10);
2100 SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
2101 if (!emstorage_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id)) {
2102 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event [NOTI_MAIL_ADD] failed");
2105 if (new_mail_tbl_data) {
2106 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2107 new_mail_tbl_data = NULL;
2112 if (err == EMAIL_ERROR_NONE && !emstorage_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH, account_id, NULL, handle_to_be_published, 0))
2113 EM_DEBUG_EXCEPTION("emstorage_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");
2117 if (search_mailbox != NULL)
2118 emstorage_free_mailbox(&search_mailbox, 1, NULL);
2120 if (new_mail_tbl_data)
2121 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2124 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2129 EM_DEBUG_FUNC_END();
2133 static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
2135 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, handle_to_be_published);
2136 int err = EMAIL_ERROR_NONE;
2138 if ((err = emcore_move_mailbox_on_imap_server(input_account_id, input_old_mailbox_path, input_new_mailbox_path)) != EMAIL_ERROR_NONE) {
2139 EM_DEBUG_EXCEPTION("emcore_move_mailbox_on_imap_server failed [%d]", err);
2143 if (err == EMAIL_ERROR_NONE) {
2144 if(!emstorage_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
2145 EM_DEBUG_EXCEPTION("emstorage_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
2147 else if (!emstorage_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
2148 EM_DEBUG_EXCEPTION("emstorage_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
2151 EM_DEBUG_FUNC_END("err [%d]", err);
2155 void* thread_func_branch_command(void *arg)
2157 EM_DEBUG_FUNC_BEGIN();
2159 int err = EMAIL_ERROR_NONE;
2160 int is_storage_full = false;
2162 email_event_t event_data;
2163 email_session_t *session = NULL;
2164 emstorage_account_tbl_t *account_tbl = NULL;
2165 int handle_to_be_published = 0;
2167 if (!emstorage_open(&err)) {
2168 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
2172 /* check that event_data loop is continuous */
2173 while (emcore_event_loop_continue()) {
2174 if (!emcore_get_empty_session(&session))
2175 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
2177 /* get a event_data from event_data queue */
2178 if (!emcore_retrieve_event(&event_data, NULL)) { /* no event_data pending */
2179 EM_DEBUG_LOG("For handle g_event_que[g_event_que_idx].type [%d], g_event_que_idx [%d]", g_event_que[g_event_que_idx].type, g_event_que_idx);
2180 #ifdef ENABLE_IMAP_IDLE_THREAD
2181 if ( !emnetwork_check_network_status(&err)) {
2182 EM_DEBUG_LOG(">>>> Data Networking ON ");
2184 if (g_imap_idle_thread_alive) {
2185 /* emcore_kill_imap_idle_thread(NULL); */
2186 /* emcore_create_imap_idle_thread(NULL); */
2189 if (!send_thread_run)
2190 emcore_create_imap_idle_thread(event_data.account_id, NULL);
2194 #endif /* ENABLE_IMAP_IDLE_THREAD */
2195 #ifdef __FEATURE_LOCAL_ACTIVITY__
2196 /* Local activity sync */
2197 if (g_client_run && g_local_activity_run) {
2198 emstorage_account_tbl_t *account_list = NULL;
2200 if (!emstorage_get_account_list(&count, &account_list, true, true, &err))
2201 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
2203 for (i = 0; i < count; i++) {
2204 if (emcore_local_activity_sync(account_list[i].account_id, &err)) {
2205 EM_DEBUG_LOG("Found local activity...!");
2206 EM_DEBUG_LOG("Resetting g_local_activity_run ");
2207 g_local_activity_run = 0;
2208 emcore_clear_session(session);
2212 emstorage_free_account(&account_list, count, &err);
2214 if (!g_local_activity_run)
2220 recv_thread_run = 0;
2222 ENTER_CRITICAL_SECTION(_event_available_lock);
2223 SLEEP_CONDITION_VARIABLE(_event_available_signal, _event_available_lock);
2224 EM_DEBUG_LOG("Wake up by _event_available_signal");
2225 LEAVE_CRITICAL_SECTION(_event_available_lock);
2228 EM_DEBUG_LOG(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
2229 EM_DEBUG_LOG("For handle g_event_que_idx - %d", g_event_que_idx);
2231 if (g_event_que_idx == 1)
2232 handle_to_be_published = 31;
2234 handle_to_be_published = g_event_que_idx - 1 ;
2236 EM_DEBUG_LOG("Handle to be Published [%d]", handle_to_be_published);
2237 recv_thread_run = 1;
2240 /* Handling storage full */
2241 is_storage_full = false;
2242 if (event_data.type == EMAIL_EVENT_SYNC_HEADER || event_data.type == EMAIL_EVENT_SYNC_HEADER_OMA ||
2243 event_data.type == EMAIL_EVENT_DOWNLOAD_BODY || event_data.type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
2244 if (emcore_is_storage_full(&err) == true) {
2245 EM_DEBUG_EXCEPTION("Storage is full");
2246 switch (event_data.type) {
2247 case EMAIL_EVENT_SYNC_HEADER:
2248 case EMAIL_EVENT_SYNC_HEADER_OMA:
2249 noti_id = NOTI_DOWNLOAD_FAIL;
2251 case EMAIL_EVENT_DOWNLOAD_BODY:
2252 noti_id = NOTI_DOWNLOAD_BODY_FAIL;
2254 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2255 noti_id = NOTI_DOWNLOAD_ATTACH_FAIL;
2261 if (!emstorage_notify_network_event(noti_id, event_data.account_id, NULL, handle_to_be_published, err))
2262 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
2263 is_storage_full = true;
2267 emdevice_set_dimming_on_off(false, NULL);
2269 if (event_data.account_id > 0) {
2270 if (!emstorage_get_account_by_id(event_data.account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
2271 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
2276 EM_DEBUG_LOG("account_id : [%d], sync_disabled : [%d]", event_data.account_id, account_tbl->sync_disabled);
2278 if (!account_tbl || account_tbl->sync_disabled == 0) {
2279 switch (event_data.type) {
2280 case EMAIL_EVENT_SYNC_IMAP_MAILBOX: /* get imap mailbox list */
2281 if (!emnetwork_check_network_status(&err))
2282 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2284 if (!emcore_sync_mailbox_list(event_data.account_id, event_data.event_param_data_3, &err))
2285 EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2287 EM_SAFE_FREE(event_data.event_param_data_3);
2290 case EMAIL_EVENT_SYNC_HEADER: /* synchronize mail header */
2291 if (is_storage_full == false)
2292 event_handler_EMAIL_EVENT_SYNC_HEADER(event_data.account_id, event_data.event_param_data_5, handle_to_be_published, &err);
2295 case EMAIL_EVENT_SYNC_HEADER_OMA: /* synchronize mail header for OMA */
2296 if (is_storage_full == false)
2297 event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data.account_id, event_data.event_param_data_1, &err);
2298 EM_SAFE_FREE(event_data.event_param_data_1);
2301 case EMAIL_EVENT_DOWNLOAD_BODY: /* download mail body */
2302 if (is_storage_full == false)
2303 event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data.account_id, (int)event_data.event_param_data_4, (int)event_data.event_param_data_3, handle_to_be_published, &err);
2304 event_data.event_param_data_3 = NULL; /* MUST BE */
2307 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT: /* download attachment */
2308 if (is_storage_full == false)
2309 event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data.account_id, (int)event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
2312 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER: /* Sync flags field */
2313 event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER((int*)event_data.event_param_data_3, event_data.event_param_data_4 , event_data.event_param_data_5, event_data.event_param_data_6, &err);
2314 EM_SAFE_FREE(event_data.event_param_data_3);
2317 case EMAIL_EVENT_DELETE_MAIL: /* delete mails */
2318 event_handler_EMAIL_EVENT_DELETE_MAIL(event_data.account_id, (int *)event_data.event_param_data_3, event_data.event_param_data_4, &err);
2319 EM_SAFE_FREE(event_data.event_param_data_3);
2322 case EMAIL_EVENT_DELETE_MAIL_ALL: /* delete all mails */
2323 event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data.event_param_data_4, (int)event_data.event_param_data_5, &err);
2325 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
2326 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2327 event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER((int)event_data.event_param_data_4, &err);
2331 case EMAIL_EVENT_CREATE_MAILBOX:
2332 event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data.account_id, event_data.event_param_data_1, event_data.event_param_data_2, GPOINTER_TO_INT(event_data.event_param_data_3), event_data.event_param_data_4, handle_to_be_published, &err);
2333 EM_SAFE_FREE(event_data.event_param_data_1);
2334 EM_SAFE_FREE(event_data.event_param_data_2);
2337 case EMAIL_EVENT_DELETE_MAILBOX:
2338 event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data.event_param_data_4, event_data.event_param_data_4, handle_to_be_published, &err);
2341 case EMAIL_EVENT_MOVE_MAIL:
2342 event_handler_EMAIL_EVENT_MOVE_MAIL(event_data.account_id, (int *)event_data.event_param_data_3, event_data.event_param_data_4, event_data.event_param_data_5, event_data.event_param_data_8, handle_to_be_published, &err);
2344 EM_SAFE_FREE(event_data.event_param_data_3);
2347 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2348 event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data.account_id, handle_to_be_published, &err);
2351 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
2352 /* event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(emcore_get_new_account_reference(), handle_to_be_published, &err);*/
2353 email_account_t *account = (email_account_t *)event_data.event_param_data_1;
2354 event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(account, handle_to_be_published, &err);
2355 emcore_free_account(account);
2356 memset(account, 0, sizeof(email_account_t)); /*global account */
2360 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
2361 email_account_t *pAccount = (email_account_t *)event_data.event_param_data_1;
2362 event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data.account_id, pAccount, handle_to_be_published, &err);
2363 emcore_free_account(pAccount);
2364 EM_SAFE_FREE(pAccount);
2368 case EMAIL_EVENT_UPDATE_MAIL:
2369 event_handler_EMAIL_EVENT_UPDATE_MAIL((email_mail_data_t*)event_data.event_param_data_1, (email_attachment_data_t*)event_data.event_param_data_2, event_data.event_param_data_4, (email_meeting_request_t*)event_data.event_param_data_3, event_data.event_param_data_5, handle_to_be_published);
2371 event_data.event_param_data_1 = NULL;
2372 event_data.event_param_data_2 = NULL;
2373 event_data.event_param_data_3 = NULL;
2376 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2377 event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data.account_id, event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
2380 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
2381 err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data.account_id, event_data.event_param_data_4);
2384 #ifdef __FEATURE_LOCAL_ACTIVITY__
2385 case EMAIL_EVENT_LOCAL_ACTIVITY:
2386 event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data.account_id, &err);
2388 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
2390 case EMAIL_EVENT_SEARCH_ON_SERVER:
2391 event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(event_data.account_id, event_data.event_param_data_4, (char *)event_data.event_param_data_1, handle_to_be_published, &err);
2392 EM_SAFE_FREE(event_data.event_param_data_1);
2395 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2396 err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data.account_id, event_data.event_param_data_4, (char*)event_data.event_param_data_1, (char*)event_data.event_param_data_2, handle_to_be_published);
2397 EM_SAFE_FREE(event_data.event_param_data_1);
2398 EM_SAFE_FREE(event_data.event_param_data_2);
2407 emstorage_free_account(&account_tbl, 1, NULL);
2411 if (!emstorage_notify_response_to_api(event_data.type, handle_to_be_published, err))
2412 EM_DEBUG_EXCEPTION("emstorage_notify_response_to_api failed");
2414 emdevice_set_dimming_on_off(true, NULL);
2417 switch (event_data.type) {
2418 case EMAIL_EVENT_SEND_MAIL:
2419 case EMAIL_EVENT_SEND_MAIL_SAVED:
2420 _sending_busy_unref();
2423 case EMAIL_EVENT_SYNC_HEADER:
2424 case EMAIL_EVENT_SYNC_HEADER_OMA:
2425 case EMAIL_EVENT_DOWNLOAD_BODY:
2426 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2427 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2428 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2429 case EMAIL_EVENT_DELETE_MAIL:
2430 case EMAIL_EVENT_DELETE_MAIL_ALL:
2431 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2432 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2433 case EMAIL_EVENT_MOVE_MAIL:
2434 case EMAIL_EVENT_CREATE_MAILBOX:
2435 case EMAIL_EVENT_DELETE_MAILBOX:
2436 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2437 case EMAIL_EVENT_SEARCH_ON_SERVER:
2438 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2439 _receiving_busy_unref();
2446 event_data.type = 0;
2448 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2449 memset(g_event_que+g_active_que, 0x00, sizeof(email_event_t));
2450 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2453 emcore_clear_session(session);
2456 if (!emstorage_close(&err))
2457 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
2459 EM_DEBUG_FUNC_END();
2462 /*Send event_data loop*/
2463 INTERNAL_FUNC int emcore_start_event_loop_for_sending_mails(int *err_code)
2465 EM_DEBUG_FUNC_BEGIN();
2466 int thread_error = -1;
2468 if (err_code != NULL)
2469 *err_code = EMAIL_ERROR_NONE;
2471 memset(&g_send_event_que, 0x00, sizeof(g_send_event_que));
2473 if (g_send_srv_thread) {
2474 EM_DEBUG_EXCEPTION("\t send service thread is already running...");
2475 if (err_code != NULL)
2476 *err_code = EMAIL_ERROR_UNKNOWN;
2480 g_send_event_que_idx = 1;
2481 g_send_event_loop = 1;
2482 g_send_active_que = 0;
2484 /* initialize lock */
2485 /* INITIALIZE_CRITICAL_SECTION(_send_event_available_lock); */
2486 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2487 INITIALIZE_CONDITION_VARIABLE(_send_event_available_signal);
2490 THREAD_CREATE_JOINABLE(g_send_srv_thread, thread_func_branch_command_for_sending_mails, thread_error);
2492 if (thread_error != 0) {
2493 EM_DEBUG_EXCEPTION("cannot make thread...");
2494 if (err_code != NULL)
2495 *err_code = EMAIL_ERROR_UNKNOWN;
2499 if (err_code != NULL)
2500 *err_code = EMAIL_ERROR_NONE;
2501 EM_DEBUG_FUNC_END();
2505 /* finish api event_data loop */
2506 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
2508 EM_DEBUG_FUNC_BEGIN();
2510 if (err_code != NULL)
2511 *err_code = EMAIL_ERROR_NONE;
2513 if (!g_send_srv_thread) {
2514 if (err_code != NULL)
2515 *err_code = EMAIL_ERROR_UNKNOWN;
2519 /* stop event_data loop */
2520 g_send_event_loop = 0;
2522 emcore_cancel_send_mail_thread(g_send_active_que, NULL, err_code);
2523 ENTER_CRITICAL_SECTION(_send_event_available_lock);
2524 WAKE_CONDITION_VARIABLE(_send_event_available_signal); /* MUST BE HERE */
2525 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
2527 /* wait for thread finished */
2528 THREAD_JOIN(g_send_srv_thread);
2530 g_send_srv_thread = 0;
2532 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2533 DELETE_CRITICAL_SECTION(_send_event_available_lock);
2534 DELETE_CONDITION_VARIABLE(_send_event_available_signal);
2536 g_send_event_que_idx = 1;
2537 g_send_active_que = 0;
2539 if (err_code != NULL)
2540 *err_code = EMAIL_ERROR_NONE;
2545 /* start api event_data loop */
2546 INTERNAL_FUNC int emcore_start_event_loop(int *err_code)
2548 EM_DEBUG_FUNC_BEGIN();
2551 if (err_code != NULL)
2552 *err_code = EMAIL_ERROR_NONE;
2554 memset(&g_event_que, 0x00, sizeof(g_event_que));
2557 EM_DEBUG_EXCEPTION("service thread is already running...");
2558 if (err_code != NULL)
2559 *err_code = EMAIL_ERROR_UNKNOWN;
2563 g_event_que_idx = 1;
2567 /* initialize lock */
2568 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2569 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2571 emcore_initialize_event_callback_table();
2574 THREAD_CREATE(g_srv_thread, thread_func_branch_command, NULL, thread_error);
2576 if (thread_error != 0) {
2577 EM_DEBUG_EXCEPTION("cannot create thread");
2578 if (err_code != NULL)
2579 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
2583 if (err_code != NULL)
2584 *err_code = EMAIL_ERROR_NONE;
2589 /* finish api event_data loop */
2590 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
2592 EM_DEBUG_FUNC_BEGIN();
2594 if (err_code != NULL)
2595 *err_code = EMAIL_ERROR_NONE;
2597 if (!g_srv_thread) {
2598 if (err_code != NULL)
2599 *err_code = EMAIL_ERROR_UNKNOWN;
2603 /* stop event_data loop */
2606 /* pthread_kill(g_srv_thread, SIGINT); */
2607 emcore_cancel_thread(g_active_que, NULL, err_code);
2609 ENTER_CRITICAL_SECTION(_event_available_lock);
2610 WAKE_CONDITION_VARIABLE(_event_available_signal);
2611 LEAVE_CRITICAL_SECTION(_event_available_lock);
2613 /* wait for thread finished */
2614 THREAD_JOIN(g_srv_thread);
2618 DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2619 DELETE_CRITICAL_SECTION(_event_available_lock);
2620 DELETE_CONDITION_VARIABLE(_event_available_signal);
2621 DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2623 g_event_que_idx = 1;
2626 if (err_code != NULL)
2627 *err_code = EMAIL_ERROR_NONE;
2628 EM_DEBUG_FUNC_END();
2633 int emcore_get_active_queue_idx()
2635 return g_send_active_que;
2638 /* check thread status
2639 * 0 : stop job 1 : continue job
2641 INTERNAL_FUNC int emcore_check_thread_status()
2643 if (g_active_que <= 0)
2646 return (g_event_que[g_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2650 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
2652 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2655 int err = EMAIL_ERROR_NONE;
2657 if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1)) {
2658 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2659 err = EMAIL_ERROR_INVALID_PARAM;
2663 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2665 EM_DEBUG_LOG("status[%d], type[%d], handle[%d]", g_event_que[handle].status, g_event_que[handle].type, handle);
2667 if (g_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT) {
2668 fail_status_notify(&g_event_que[handle], EMAIL_ERROR_CANCELLED);
2670 switch (g_event_que[handle].type) {
2671 case EMAIL_EVENT_SEND_MAIL:
2672 case EMAIL_EVENT_SEND_MAIL_SAVED:
2673 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
2674 _sending_busy_unref();
2675 if (!emstorage_notify_network_event(NOTI_SEND_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2676 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2678 case EMAIL_EVENT_DOWNLOAD_BODY:
2679 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
2680 _receiving_busy_unref();
2681 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2682 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2685 case EMAIL_EVENT_SYNC_HEADER:
2686 case EMAIL_EVENT_SYNC_HEADER_OMA:
2687 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2688 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2689 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2690 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
2691 _receiving_busy_unref();
2694 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2695 EM_DEBUG_LOG(" validate account waiting : cancel acc id : %d", g_event_que[handle].account_id);
2696 _receiving_busy_unref();
2697 if (!emstorage_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2698 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
2701 case EMAIL_EVENT_DELETE_MAIL:
2702 case EMAIL_EVENT_DELETE_MAIL_ALL:
2703 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2704 case EMAIL_EVENT_MOVE_MAIL:
2705 case EMAIL_EVENT_CREATE_MAILBOX:
2706 case EMAIL_EVENT_DELETE_MAILBOX:
2707 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2708 case EMAIL_EVENT_SEARCH_ON_SERVER:
2709 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2710 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
2711 _receiving_busy_unref();
2718 memset(g_event_que+handle, 0x00, sizeof(email_event_t));
2719 g_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2721 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2726 if (err_code != NULL)
2728 EM_DEBUG_FUNC_END();
2732 /* check thread status
2733 * 0 : stop job 1 : continue job
2735 int emcore_check_send_mail_thread_status()
2737 EM_DEBUG_FUNC_BEGIN();
2739 if (g_send_active_que <= 0)
2741 EM_DEBUG_LOG("g_send_event_que[g_send_active_que[%d]].status[%d]", g_send_active_que, g_send_event_que[g_send_active_que].status);
2742 EM_DEBUG_FUNC_END();
2743 return (g_send_event_que[g_send_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2746 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
2748 EM_DEBUG_FUNC_BEGIN();
2749 int error_code = EMAIL_ERROR_NONE;
2750 int i, event_count = EVENT_QUEUE_MAX, exit_flag = 0, sleep_count = 0;
2752 for (i = 0 ; i < event_count; i++) {
2753 if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2754 EM_DEBUG_LOG("There is a live thread. %d", i);
2755 if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2756 EM_DEBUG_LOG("And it is for account %d", g_event_que[i].account_id);
2757 emcore_cancel_thread(i, NULL, &error_code);
2762 while (exit_flag == 0 && sleep_count < 30) {
2763 EM_DEBUG_LOG("Sleeping...");
2765 EM_DEBUG_LOG("Wake up!");
2768 for (i = 0 ; i < event_count; i++) {
2769 if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2770 EM_DEBUG_LOG("There is still a live thread. %d", i);
2771 if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2772 EM_DEBUG_LOG("And it is for account %d. So, I should sleep for a while.", g_event_que[i].account_id);
2779 EM_DEBUG_LOG("Sleep count %d", sleep_count);
2781 if (sleep_count >= 30)
2782 error_code = EMAIL_ERROR_CANNOT_STOP_THREAD;
2784 error_code = EMAIL_ERROR_NONE;
2785 EM_DEBUG_FUNC_END();
2790 /* cancel send mail job */
2791 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
2793 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2796 int err = EMAIL_ERROR_NONE;
2798 if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1)) {
2799 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2800 err = EMAIL_ERROR_INVALID_PARAM;
2804 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2806 EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2808 if (g_send_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT) {
2809 fail_status_notify(&g_send_event_que[handle], EMAIL_ERROR_CANCELLED);
2811 switch (g_send_event_que[handle].type) {
2812 case EMAIL_EVENT_SEND_MAIL:
2813 case EMAIL_EVENT_SEND_MAIL_SAVED:
2814 _sending_busy_unref();
2815 g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2816 if (!emstorage_notify_network_event(NOTI_SEND_CANCEL, g_send_event_que[handle].account_id, NULL , g_send_event_que[handle].event_param_data_4, err))
2817 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2824 EM_DEBUG_LOG("send_mail_cancel");
2825 memset(g_send_event_que+handle, 0x00, sizeof(email_event_t));
2826 g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2828 EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2831 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2836 if (err_code != NULL)
2838 EM_DEBUG_FUNC_END("ret [%d]", ret);
2843 INTERNAL_FUNC int emcore_get_receiving_event_queue(email_event_t **event_queue, int *event_active_queue, int *err)
2845 if (event_queue == NULL || event_active_queue == NULL) {
2846 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM event_queue[%p] event_active_queue[%p]", event_queue, event_active_queue);
2849 *err = EMAIL_ERROR_INVALID_PARAM;
2854 *event_queue = g_event_que;
2855 *event_active_queue = g_active_que;
2860 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
2862 EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
2865 EM_SAFE_FREE(event_data->event_param_data_1);
2866 EM_SAFE_FREE(event_data->event_param_data_2);
2867 EM_SAFE_FREE(event_data->event_param_data_3);
2870 EM_DEBUG_FUNC_END();
2874 #ifdef __FEATURE_KEEP_CONNECTION__
2875 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
2877 return (unsigned int)g_srv_thread;
2879 #endif /* __FEATURE_KEEP_CONNECTION__ */
2881 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2883 INTERNAL_FUNC int emcore_get_pbd_thd_state()
2885 int pbd_thd_state = false;
2886 ENTER_CRITICAL_SECTION(_state_variables_lock);
2887 pbd_thd_state = g_pbd_thd_state;
2888 LEAVE_CRITICAL_SECTION(_state_variables_lock);
2889 return pbd_thd_state;
2892 static int emcore_set_pbd_thd_state(int flag)
2894 ENTER_CRITICAL_SECTION(_state_variables_lock);
2895 g_pbd_thd_state = flag;
2896 LEAVE_CRITICAL_SECTION(_state_variables_lock);
2898 return g_pbd_thd_state;
2901 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
2903 EM_DEBUG_FUNC_BEGIN();
2904 EM_DEBUG_FUNC_END();
2905 return (unsigned int)g_partial_body_thd;
2908 static int emcore_clear_bulk_pbd_que(int *err_code)
2910 EM_DEBUG_FUNC_BEGIN();
2913 int error = EMAIL_ERROR_NONE;
2916 for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
2917 if (g_partial_body_bulk_dwd_que[i].event_type) {
2918 if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error)) {
2919 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2926 if (NULL != err_code)
2928 EM_DEBUG_FUNC_END();
2932 static void emcore_pb_thd_set_local_activity_continue(int flag)
2934 EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
2936 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2938 g_pb_thd_local_activity_continue = flag;
2941 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2944 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2945 EM_DEBUG_FUNC_END();
2949 int emcore_pb_thd_can_local_activity_continue()
2951 EM_DEBUG_FUNC_BEGIN();
2955 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2957 ret = g_pb_thd_local_activity_continue;
2959 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2960 EM_DEBUG_FUNC_END();
2965 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
2967 EM_DEBUG_FUNC_BEGIN();
2970 int error = EMAIL_ERROR_NONE;
2973 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2975 if (true == g_partial_body_thd_queue_empty) {
2976 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
2979 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
2980 if (g_partial_body_thd_event_que[i].event_type) {
2981 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error)) {
2982 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2989 g_partial_body_thd_queue_empty = true;
2990 g_partial_body_thd_queue_full = false;
2992 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2994 if (NULL != err_code)
2996 EM_DEBUG_FUNC_END();
3000 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
3002 EM_DEBUG_FUNC_BEGIN();
3006 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3008 ret = g_partial_body_thd_queue_empty;
3010 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3011 EM_DEBUG_FUNC_END("ret [%d]", ret);
3014 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
3016 EM_DEBUG_FUNC_BEGIN();
3020 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3022 ret = g_partial_body_thd_queue_full;
3024 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3025 EM_DEBUG_FUNC_END();
3030 Himanshu[h.gahalut] : If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
3031 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
3032 through _partial_body_thd_event_queue_lock mutex.
3034 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
3038 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3039 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3043 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
3045 EM_DEBUG_FUNC_BEGIN();
3046 int error = EMAIL_ERROR_NONE;
3049 if (NULL == src || NULL == dest) {
3050 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
3051 error = EMAIL_ERROR_INVALID_PARAM;
3055 dest->account_id = src->account_id;
3056 dest->mail_id = src->mail_id;
3057 dest->server_mail_id = src->server_mail_id;
3058 dest->activity_id = src->activity_id;
3059 dest->mailbox_id = src->mailbox_id;
3060 dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
3061 dest->activity_type = src->activity_type;
3062 dest->event_type = src->event_type;
3064 EM_DEBUG_LOG("dest->account_id[%d], dest->mail_id[%d], dest->server_mail_id [%lu]", dest->account_id, dest->mail_id , dest->server_mail_id);
3070 if (NULL != error_code)
3071 *error_code = error;
3078 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,
3079 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
3081 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
3085 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3086 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3090 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3092 EM_DEBUG_FUNC_BEGIN();
3094 if (NULL == partial_body_thd_event) {
3095 *error_code = EMAIL_ERROR_INVALID_PARAM;
3099 email_event_partial_body_thd *pbd_event = partial_body_thd_event;
3101 /*Free character pointers in event_data cell */
3102 EM_SAFE_FREE(pbd_event->mailbox_name);
3103 memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
3104 EM_DEBUG_FUNC_END();
3108 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3110 EM_DEBUG_FUNC_BEGIN();
3112 if (NULL == partial_body_thd_event) {
3113 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
3115 if (error_code != NULL) {
3116 *error_code = EMAIL_ERROR_INVALID_PARAM;
3122 int error = EMAIL_ERROR_NONE;
3123 int empty_cell_index = -1;
3127 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3129 /* find a cell in queue which is empty */
3131 for (count = 0, index = g_partial_body_thd_next_event_idx; count < TOTAL_PARTIAL_BODY_EVENTS;) {
3132 if (g_partial_body_thd_event_que[index].event_type) {
3136 if (index == TOTAL_PARTIAL_BODY_EVENTS) {
3141 /*Found empty Cell*/
3143 empty_cell_index = index;
3148 if (-1 != empty_cell_index) {
3149 if (false == emcore_copy_partial_body_thd_event(partial_body_thd_event, g_partial_body_thd_event_que+empty_cell_index , &error)) {
3150 EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
3153 g_partial_body_thd_queue_empty = false;
3155 if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
3156 /*This is the last event_data inserted in queue after its insertion, queue is full */
3157 g_partial_body_thd_queue_full = true;
3161 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
3167 EM_DEBUG_LOG(" partial body thread event_data queue is full ");
3168 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
3170 g_partial_body_thd_queue_full = true;
3171 g_partial_body_thd_queue_empty = false;
3175 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3177 if (NULL != error_code) {
3178 *error_code = error;
3185 /* h.gahlaut : Return true only if event_data is retrieved successfully */
3187 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3189 EM_DEBUG_FUNC_BEGIN();
3192 int error = EMAIL_ERROR_NONE;
3195 /* Lock Mutex to protect event_data queue and associated global variables variables*/
3197 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3199 index = g_partial_body_thd_next_event_idx;
3201 if (0 == g_partial_body_thd_event_que[index].event_type) {
3202 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
3203 g_partial_body_thd_queue_empty = true;
3204 g_partial_body_thd_queue_full = false;
3207 /*Copy the event_data from queue to return it and free the event_data in queue */
3208 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
3209 EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
3211 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
3212 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
3215 g_partial_body_thd_queue_full = false;
3216 g_partial_body_thd_next_event_idx = ++index;
3218 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
3219 g_partial_body_thd_next_event_idx = 0;
3221 /* If the event_data retrieved was the only event_data present in queue,
3222 we need to set g_partial_body_thd_queue_empty to true
3225 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
3226 g_partial_body_thd_queue_empty = true;
3236 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3239 *error_code = error;
3245 gpointer partial_body_download_thread(gpointer data)
3247 EM_DEBUG_FUNC_BEGIN();
3249 int err = EMAIL_ERROR_NONE;
3250 email_session_t *session = NULL;
3251 email_event_partial_body_thd partial_body_thd_event;
3253 EM_DEBUG_LOG(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());
3255 /* Open connection with DB */
3257 if (false == emstorage_open(&err)) {
3258 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
3262 /* Start the continuous loop */
3264 while (g_partial_body_thd_loop) {
3265 /* Get an empty session */
3266 /* TODO : Mutex should be used in session APIs */
3268 if (false == emcore_get_empty_session(&session))
3269 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
3270 else { /* Get and Event from the Partial Body thread Event Queue */
3271 memset(&partial_body_thd_event, 0x00, sizeof(email_event_partial_body_thd));
3273 if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
3274 if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
3275 EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
3277 EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
3279 /* Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count */
3280 if (!g_partial_body_bulk_dwd_queue_empty) {
3281 partial_body_thd_event.event_type = 0;
3282 partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
3283 partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
3284 partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name);
3286 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3287 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3289 emcore_pb_thd_set_local_activity_continue(true);
3292 if (true == emcore_pb_thd_can_local_activity_continue()) {
3293 /*Check for local Activities */
3294 int is_local_activity_event_inserted = false;
3296 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &err)) {
3297 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
3300 if (true == is_local_activity_event_inserted) {
3301 emcore_pb_thd_set_local_activity_continue(false);
3303 emcore_clear_session(session);
3309 EM_DEBUG_LOG(" Partial Body Thread is going to sleep");
3311 emcore_set_pbd_thd_state(false);
3313 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3314 SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
3315 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3317 EM_DEBUG_LOG(" Partial Body Thread wakes up ");
3319 emcore_set_pbd_thd_state(true);
3324 EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
3326 /* Since all events are network operations dnet init and sleep control is
3327 done before entering switch block*/
3329 emdevice_set_dimming_on_off(false, NULL);
3331 if (!emnetwork_check_network_status( &err)) {
3332 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
3335 /* Process events */
3336 EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
3338 switch (partial_body_thd_event.event_type) {
3339 case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD: {
3340 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
3341 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3345 case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD: {
3346 partial_body_thd_event.event_type = 0;
3348 /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
3349 * g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
3350 if (false == emcore_is_partial_body_thd_que_empty())
3352 if (!g_partial_body_bulk_dwd_queue_empty)
3355 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3356 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
3360 EM_DEBUG_EXCEPTION(" Warning : Default case entered. This should not happen ");
3365 if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
3366 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
3368 emdevice_set_dimming_on_off(true, NULL);
3371 emcore_clear_session(session);
3375 /* If something is added to end thread in future for any case then if thread is holding any resources
3376 define a function emcore_partial_body_thd_loop_stop to release resources and call it
3377 here to end thread */
3381 INTERNAL_FUNC int emcore_start_thread_for_downloading_partial_body(int *err_code)
3383 EM_DEBUG_FUNC_BEGIN();
3385 int i = 0, thread_error = -1;
3387 /* Clear Partial Body Event Queue*/
3388 memset(&g_partial_body_thd_event_que, 0x00, sizeof(g_partial_body_thd_event_que));
3390 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
3391 g_partial_body_thd_event_que[i].mailbox_name = NULL;
3392 g_partial_body_thd_event_que[i].mailbox_id = 0;
3395 if (g_partial_body_thd) {
3396 EM_DEBUG_EXCEPTION("partial body thread is already running...");
3397 if (err_code != NULL)
3398 *err_code = EMAIL_ERROR_UNKNOWN;
3403 g_partial_body_thd_next_event_idx = 0;
3404 g_partial_body_thd_loop = 1;
3405 g_partial_body_thd_queue_empty = true;
3406 g_partial_body_thd_queue_full = false;
3408 INITIALIZE_CONDITION_VARIABLE(_partial_body_thd_cond);
3411 /* THREAD_CREATE_JOINABLE(g_partial_body_thd, partial_body_download_thread, thread_error); */
3412 THREAD_CREATE(g_partial_body_thd, partial_body_download_thread, NULL, thread_error);
3414 if (thread_error != 0) {
3415 EM_DEBUG_EXCEPTION("cannot make thread...");
3416 if (err_code != NULL)
3417 *err_code = EMAIL_ERROR_UNKNOWN;
3421 if (err_code != NULL)
3422 *err_code = EMAIL_ERROR_NONE;
3428 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
3429 static int emcore_partial_body_bulk_flush(int *error_code)
3431 EM_DEBUG_FUNC_BEGIN();
3432 int error = EMAIL_ERROR_NONE;
3434 MAILSTREAM *stream = NULL;
3435 void *tmp_stream = NULL;
3437 if (!emcore_connect_to_remote_mailbox(g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
3438 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3441 stream = (MAILSTREAM *)tmp_stream;
3443 /* Call bulk download here */
3444 if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
3445 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3452 emcore_close_mailbox(0, stream);
3455 g_partial_body_bulk_dwd_next_event_idx = 0;
3456 g_partial_body_bulk_dwd_queue_empty = true;
3458 if (false == emcore_clear_bulk_pbd_que(&error))
3459 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
3461 if (NULL != error_code)
3462 *error_code = error;
3464 EM_DEBUG_FUNC_END("ret [%d]", ret);
3469 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
3470 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
3472 EM_DEBUG_FUNC_BEGIN();
3473 int error = EMAIL_ERROR_NONE;
3474 int num_activity = 0;
3478 MAILSTREAM *stream = NULL;
3479 void *tmp_stream = NULL;
3480 email_event_partial_body_thd *activity_data_list = NULL;
3481 int *mailbox_list = NULL;
3483 if (NULL == pbd_event)
3485 EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
3487 error = EMAIL_ERROR_INVALID_PARAM;
3491 /*Check if the event_data is to flush the event_data que array */
3492 if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
3493 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
3494 /*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
3495 then append this event_data also to download que array */
3496 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
3497 (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
3498 EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
3499 EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
3500 if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
3501 if (false == emcore_partial_body_bulk_flush(&error)) {
3502 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3508 EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
3509 EM_DEBUG_LOG("Flush the current que if not empty");
3510 EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
3511 if (!g_partial_body_bulk_dwd_queue_empty) {
3512 if (false == emcore_partial_body_bulk_flush(&error)) {
3513 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3518 /*Add the event_data to the download que array */
3519 if (false == emcore_copy_partial_body_thd_event(pbd_event, g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
3520 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
3522 g_partial_body_bulk_dwd_queue_empty = false;
3523 g_partial_body_bulk_dwd_next_event_idx++;
3524 EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
3527 else if (pbd_event->activity_type) {
3528 int *account_list = NULL;
3529 int account_count = 0;
3531 EM_DEBUG_LOG("Event is coming from local activity.");
3532 /* Get all the accounts for which local activities are pending */
3533 if (false == emstorage_get_pbd_account_list(&account_list, &account_count, false, &error)) {
3534 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3535 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3539 for (m = 0; m < account_count; ++m) {
3540 /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
3541 if (false == emstorage_get_pbd_mailbox_list(account_list[m], &mailbox_list, &count, false, &error)) {
3542 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3543 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3547 for (i = 0; i < count; i++) {
3549 int activity_count = 0;
3551 if (!emcore_connect_to_remote_mailbox(account_list[m], mailbox_list[i], (void **)&tmp_stream, &error)) {
3552 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3557 stream = (MAILSTREAM *)tmp_stream;
3559 if (false == emstorage_get_mailbox_pbd_activity_count(account_list[m], mailbox_list[i], &activity_count, false, &error)) {
3560 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
3564 if (activity_count > 0) {
3565 int temp_error = EMAIL_ERROR_NONE;
3569 int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3572 if (false == emstorage_get_pbd_activity_data(account_list[j], mailbox_list[i], &activity_data_list, &num_activity, false, &error))
3573 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
3575 if (NULL == activity_data_list) {
3576 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
3580 remainder = num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3581 iter = num_activity/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1 : 0);
3583 for (j = 0; j < iter; j++) {
3584 if ((iter == (j+1)) && (remainder != 0))
3587 /*Call bulk download here */
3588 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
3589 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3590 temp_error = EMAIL_ERROR_NO_RESPONSE;
3593 for (k = 0; k < num; k++) {
3594 if (activity_data_list[index + k].activity_type)
3595 emcore_free_partial_body_thd_event(activity_data_list + index + k, &error);
3601 if (false == emcore_is_partial_body_thd_que_empty()) {
3603 goto FINISH_OFF; /* Stop Local Activity Sync */
3605 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
3606 temp_error = EMAIL_ERROR_NONE;
3612 emcore_close_mailbox(0, stream);
3617 /* After completing one cycle of local activity sync ,
3618 local activity continue variable should be set to false. */
3620 emcore_pb_thd_set_local_activity_continue(false);
3622 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 */ {
3623 /*Check if events have arrived in g_partial_body_thd_event_que */
3624 if (false == emcore_is_partial_body_thd_que_empty()) {
3625 EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
3627 goto FINISH_OFF; /* Stop Local Activity Sync */
3629 if (false == emcore_partial_body_bulk_flush(&error)) {
3630 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3639 EM_SAFE_FREE(mailbox_list);
3641 if (activity_data_list) {
3642 for (i = 0; i < num_activity; i++) {
3643 if (activity_data_list[i].activity_type)
3644 emcore_free_partial_body_thd_event(activity_data_list + i, &error);
3650 emcore_close_mailbox(0, stream);
3653 if (NULL != error_code)
3654 *error_code = error;
3655 EM_DEBUG_FUNC_END("ret [%d]", ret);
3659 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */