4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File : email-core-event_data.h
26 * Desc : Mail Engine Event
31 * 2006.08.16 : created
32 *****************************************************************************/
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-core-signal.h"
59 #include "email-debug-log.h"
62 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
64 /*[h.gahlaut] - All static variable declaration for partial body download thread are done here */
66 #define TOTAL_PARTIAL_BODY_EVENTS 100
68 static email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
70 static int g_partial_body_thd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
71 static int g_partial_body_thd_loop = 1; /* Variable to make a continuos while loop */
72 static int g_partial_body_thd_queue_empty = true; /* Variable to determine if event queue is empty.True means empty*/
73 static int g_partial_body_thd_queue_full = false; /* Variable to determine if event queue is full. True means full*/
74 static int g_pb_thd_local_activity_continue = true; /* Variable to control local activity sync */
75 int g_pbd_thd_state = false; /* false : thread is sleeping , true : thread is working */
77 static pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect event queue */
78 static pthread_cond_t _partial_body_thd_cond = PTHREAD_COND_INITIALIZER; /* Condition variable on which partial body thread is waiting */
79 thread_t g_partial_body_thd ; /* Determines if thread is created or not. Non Null means thread is created */
81 email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
82 static int g_partial_body_bulk_dwd_next_event_idx = 0; /* Index of Next Event to be processed in the queue*/
83 static int g_partial_body_bulk_dwd_queue_empty = true;
85 static pthread_mutex_t _state_variables_lock;
87 /*[h.gahlaut] - All static function declaration for partial body download thread are done here */
89 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code);
90 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
91 static void emcore_pb_thd_set_local_activity_continue(int flag);
92 static int emcore_set_pbd_thd_state(int flag);
93 static int emcore_clear_bulk_pbd_que(int *err_code);
94 int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code);
98 #ifdef ENABLE_IMAP_IDLE_THREAD
99 extern int g_imap_idle_thread_alive;
100 extern int imap_idle_thread;
101 #endif /* ENABLE_IMAP_IDLE_THREAD */
102 INTERNAL_FUNC int g_client_count = 0;
103 INTERNAL_FUNC int g_client_run = 0 ;
105 #ifdef __FEATURE_LOCAL_ACTIVITY__
106 INTERNAL_FUNC int g_local_activity_run = 0;
107 INTERNAL_FUNC int g_save_local_activity_run = 0;
111 #define EVENT_QUEUE_MAX 32
113 typedef struct EVENT_CALLBACK_ELEM
115 email_event_callback callback;
117 struct EVENT_CALLBACK_ELEM *next;
118 } EVENT_CALLBACK_LIST;
120 static pthread_mutex_t _event_available_lock = PTHREAD_MUTEX_INITIALIZER;
121 static pthread_cond_t _event_available_signal = PTHREAD_COND_INITIALIZER;
122 static pthread_mutex_t *_event_callback_table_lock = NULL;
123 static pthread_mutex_t *_event_queue_lock = NULL;
124 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM]; /* array of singly-linked list for event callbacks */
126 void *thread_func_branch_command(void *arg);
128 static email_event_t g_event_que[EVENT_QUEUE_MAX];
130 int send_thread_run = 0;
131 int recv_thread_run = 0;
133 static pthread_mutex_t _send_event_available_lock = PTHREAD_MUTEX_INITIALIZER;
134 static pthread_mutex_t *_send_event_queue_lock = NULL; /* MUST BE "recursive" */
135 static pthread_cond_t _send_event_available_signal = PTHREAD_COND_INITIALIZER;
136 static thread_t g_send_srv_thread;
137 static thread_t g_srv_thread;
139 static email_event_t g_send_event_que[EVENT_QUEUE_MAX];
140 static int g_send_event_que_idx = 1;
141 static int g_send_event_loop = 1;
142 static int g_send_active_que = 0;
143 static int g_event_que_idx = 1;
144 static int g_event_loop = 1;
145 static int g_active_que = 0;
146 static int g_sending_busy_cnt = 0;
147 static int g_receiving_busy_cnt = 0;
150 INTERNAL_FUNC int emcore_get_current_thread_type()
152 EM_DEBUG_FUNC_BEGIN();
153 thread_t thread_id = THREAD_SELF();
154 int thread_type = -1;
156 if (thread_id == g_srv_thread)
157 thread_type = _SERVICE_THREAD_TYPE_RECEIVING;
158 else if (thread_id == g_send_srv_thread)
159 thread_type = _SERVICE_THREAD_TYPE_SENDING;
160 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
161 else if (thread_id == g_partial_body_thd)
162 thread_type = _SERVICE_THREAD_TYPE_PBD;
164 EM_DEBUG_FUNC_END("thread_type [%d]", thread_type);
168 static int is_gdk_lock_needed()
171 return (THREAD_SELF() == g_srv_thread);
176 INTERNAL_FUNC int emcore_get_pending_event(email_action_t action, int account_id, int mail_id, email_event_status_type_t *status)
178 EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
183 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
185 for (i = 1; i < EVENT_QUEUE_MAX; i++) {
186 switch (g_event_que[i].type) {
187 case EMAIL_EVENT_SEND_MAIL:
188 case EMAIL_EVENT_SEND_MAIL_SAVED:
189 if (action == EMAIL_ACTION_SEND_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
195 case EMAIL_EVENT_SYNC_HEADER:
196 if (action == EMAIL_ACTION_SYNC_HEADER && account_id == g_event_que[i].account_id) {
202 case EMAIL_EVENT_SYNC_HEADER_OMA:
203 if (action == EMAIL_ACTION_SYNC_HEADER_OMA && account_id == g_event_que[i].account_id) {
209 case EMAIL_EVENT_DOWNLOAD_BODY:
210 if (action == EMAIL_ACTION_DOWNLOAD_BODY && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
215 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
216 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) {
221 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
222 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) {
227 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
228 if (action == EMAIL_ACTION_DOWNLOAD_ATTACHMENT && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
233 case EMAIL_EVENT_DELETE_MAIL:
234 case EMAIL_EVENT_DELETE_MAIL_ALL:
235 if (action == EMAIL_ACTION_DELETE_MAIL && account_id == g_event_que[i].account_id) {
241 case EMAIL_EVENT_CREATE_MAILBOX:
242 if (action == EMAIL_ACTION_CREATE_MAILBOX && account_id == g_event_que[i].account_id) {
248 case EMAIL_EVENT_DELETE_MAILBOX:
249 if (action == EMAIL_ACTION_DELETE_MAILBOX && account_id == g_event_que[i].account_id) {
255 case EMAIL_EVENT_MOVE_MAIL:
256 if (action == EMAIL_ACTION_MOVE_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
262 case EMAIL_EVENT_VALIDATE_ACCOUNT:
263 if (action == EMAIL_ACTION_VALIDATE_ACCOUNT && account_id == g_event_que[i].account_id) {
269 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
270 if (action == EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT && account_id == 0) {
276 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
277 if (action == EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT && account_id == 0) {
283 case EMAIL_EVENT_UPDATE_MAIL:
284 if (action == EMAIL_ACTION_UPDATE_MAIL) {
290 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
291 if (action == EMAIL_ACTION_SET_MAIL_SLOT_SIZE) {
297 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
298 if (action == EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED) {
304 case EMAIL_EVENT_SEARCH_ON_SERVER:
305 if (action == EMAIL_ACTION_SEARCH_ON_SERVER && account_id == g_event_que[i].account_id) {
311 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
312 if (action == EMAIL_ACTION_MOVE_MAILBOX && account_id == g_event_que[i].account_id) {
324 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
328 *status = g_event_que[i].status;
336 INTERNAL_FUNC void emcore_get_event_queue_status(int *on_sending, int *on_receiving)
338 if (on_sending != NULL)
339 *on_sending = g_sending_busy_cnt;
341 if (on_receiving != NULL)
342 *on_receiving = g_receiving_busy_cnt;
345 static void _sending_busy_ref(void)
347 g_sending_busy_cnt++;
350 static void _sending_busy_unref(void)
352 g_sending_busy_cnt--;
355 static void _receiving_busy_ref(void)
357 g_receiving_busy_cnt++;
360 static void _receiving_busy_unref(void)
362 g_receiving_busy_cnt--;
365 static void waiting_status_notify(email_event_t *event_data, int queue_idx)
367 EM_DEBUG_FUNC_BEGIN("event_data[%p], queue_idx[%d]", event_data, queue_idx);
369 int account_id = event_data->account_id;
370 int mail_id = event_data->event_param_data_4; /* NOT ALWAYS */
372 switch (event_data->type) {
373 case EMAIL_EVENT_SEND_MAIL:
374 emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
377 case EMAIL_EVENT_SYNC_HEADER:
378 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
381 case EMAIL_EVENT_SYNC_HEADER_OMA:
382 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER_OMA, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
385 case EMAIL_EVENT_DOWNLOAD_BODY:
386 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
389 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
390 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
391 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);
395 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
396 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
399 case EMAIL_EVENT_DELETE_MAIL:
400 case EMAIL_EVENT_DELETE_MAIL_ALL:
401 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
404 case EMAIL_EVENT_VALIDATE_ACCOUNT:
405 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
408 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
409 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
412 case EMAIL_EVENT_MOVE_MAIL:
413 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAIL, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
416 case EMAIL_EVENT_CREATE_MAILBOX:
417 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
420 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
421 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
424 case EMAIL_EVENT_UPDATE_MAIL:
427 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
428 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);
431 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
432 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);
435 case EMAIL_EVENT_SEARCH_ON_SERVER:
436 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
439 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
440 /* emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE); */
449 static void fail_status_notify(email_event_t *event_data, int error)
451 EM_DEBUG_FUNC_BEGIN();
452 int account_id, mail_id;
455 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
458 account_id = event_data->account_id;
459 mail_id = event_data->event_param_data_4;
461 EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
463 switch (event_data->type) {
464 case EMAIL_EVENT_SEND_MAIL:
465 /* case EMAIL_EVENT_SEND_MAIL_SAVED: */
466 /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
467 emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, error);
470 case EMAIL_EVENT_SYNC_HEADER:
471 emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
472 emcore_show_user_message(account_id, EMAIL_ACTION_SYNC_HEADER, error);
475 case EMAIL_EVENT_DOWNLOAD_BODY:
476 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
477 emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
480 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
481 emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
482 emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
485 case EMAIL_EVENT_DELETE_MAIL:
486 case EMAIL_EVENT_DELETE_MAIL_ALL:
487 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
488 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, error);
491 case EMAIL_EVENT_VALIDATE_ACCOUNT:
492 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
493 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
496 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
497 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
498 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
501 case EMAIL_EVENT_CREATE_MAILBOX:
502 emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
503 emcore_show_user_message(account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
506 case EMAIL_EVENT_DELETE_MAILBOX:
507 emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
508 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
511 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
512 emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
513 emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
516 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
517 emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
520 case EMAIL_EVENT_SEARCH_ON_SERVER:
521 emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
522 emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
525 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
526 emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
527 emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
530 case EMAIL_EVENT_UPDATE_MAIL:
531 emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
534 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
535 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);
545 static void emcore_initialize_event_callback_table()
547 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
551 for (i = 0; i < EMAIL_ACTION_NUM; i++)
552 _event_callback_table[i] = NULL;
554 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
557 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
559 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
561 if (callback == NULL)
566 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
568 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
570 while (node != NULL) {
571 if (node->callback == callback && node->event_data == event_data) /* already registered */
577 /* not found, so keep going */
579 node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
581 if (node == NULL) /* not enough memory */
584 node->callback = callback;
585 node->event_data = event_data;
586 node->next = _event_callback_table[action];
588 _event_callback_table[action] = node;
593 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
598 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
600 EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
602 if (callback == NULL)
607 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
609 EVENT_CALLBACK_LIST *prev = NULL;
610 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
612 while (node != NULL) {
613 if (node->callback == callback) {
615 prev->next = node->next;
617 _event_callback_table[action] = node->next;
629 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
634 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)
636 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);
639 lock_needed = is_gdk_lock_needed();
642 /* Todo : g_thread_yield */
645 ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
647 EVENT_CALLBACK_LIST *node = _event_callback_table[action];
649 while (node != NULL) {
650 if (node->callback != NULL)
651 node->callback(total, done, status, account_id, mail_id, (handle == -1 ? emcore_get_active_queue_idx() : handle), node->event_data, error);
655 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
662 /* insert a event to event queue */
663 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
665 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
668 EM_DEBUG_EXCEPTION("Invalid Parameter");
669 if (err_code != NULL)
670 *err_code = EMAIL_ERROR_INVALID_PARAM;
675 EM_DEBUG_EXCEPTION("email-service is not ready");
676 if (err_code != NULL)
677 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
682 int error = EMAIL_ERROR_NONE;
684 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
686 if (!g_event_que[g_event_que_idx].type) { /* if current buffer has not event, insert event data to current buffer */
687 EM_DEBUG_LOG("Current buffer has not a event. [%d]", g_event_que_idx);
688 memcpy(g_event_que+g_event_que_idx, event_data, sizeof(email_event_t));
689 g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
690 waiting_status_notify(event_data, g_event_que_idx);
692 *handle = g_event_que_idx;
694 else { /* if current buffer has event, find the empty buffer */
695 EM_DEBUG_LOG("Current buffer has a event. [%d]", g_event_que_idx);
696 int i, j = g_event_que_idx + 1;
698 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++) {
699 if (j >= EVENT_QUEUE_MAX)
702 if (!g_event_que[j].type)
706 if (i < EVENT_QUEUE_MAX) {
707 EM_DEBUG_LOG("I found available buffer. [%d]", g_event_que + j);
708 memcpy(g_event_que+j, event_data, sizeof(email_event_t));
709 g_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
710 waiting_status_notify(event_data, j);
716 EM_DEBUG_EXCEPTION("event que is full...");
717 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
722 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
725 event_data->event_param_data_1 = NULL; /* MUST BE - to prevent double-free */
727 switch (event_data->type) {
728 case EMAIL_EVENT_SEND_MAIL:
729 case EMAIL_EVENT_SEND_MAIL_SAVED:
733 case EMAIL_EVENT_SYNC_HEADER:
734 case EMAIL_EVENT_DOWNLOAD_BODY:
735 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
736 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
737 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
738 case EMAIL_EVENT_ISSUE_IDLE:
739 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
740 case EMAIL_EVENT_VALIDATE_ACCOUNT:
741 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
742 case EMAIL_EVENT_SAVE_MAIL:
743 case EMAIL_EVENT_MOVE_MAIL:
744 case EMAIL_EVENT_DELETE_MAIL:
745 case EMAIL_EVENT_DELETE_MAIL_ALL:
746 case EMAIL_EVENT_SYNC_HEADER_OMA:
747 case EMAIL_EVENT_CREATE_MAILBOX:
748 case EMAIL_EVENT_DELETE_MAILBOX:
749 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
750 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
751 case EMAIL_EVENT_UPDATE_MAIL:
752 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
753 case EMAIL_EVENT_SEARCH_ON_SERVER:
754 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
755 _receiving_busy_ref();
761 ENTER_CRITICAL_SECTION(_event_available_lock);
762 WAKE_CONDITION_VARIABLE(_event_available_signal);
763 LEAVE_CRITICAL_SECTION(_event_available_lock);
766 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
768 int is_local_activity_event_inserted = false;
770 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &error))
771 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
773 if (true == is_local_activity_event_inserted)
774 emcore_pb_thd_set_local_activity_continue(false);
779 if (err_code != NULL)
782 EM_DEBUG_LOG("Finish with [%d]", ret);
786 /* get a event from event_data queue */
787 static int emcore_retrieve_event(email_event_t *event_data, int *err_code)
789 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
792 int error = EMAIL_ERROR_NONE;
794 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
796 /* get a event_data if this queue is not empty */
797 if (g_event_que[g_event_que_idx].type) {
798 memcpy(event_data, g_event_que+g_event_que_idx, sizeof(email_event_t));
800 if (event_data->status != EMAIL_EVENT_STATUS_WAIT) { /* EMAIL_EVENT_STATUS_CANCELED */
801 memset(g_event_que+g_event_que_idx, 0x00, sizeof(email_event_t));
806 g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
807 g_active_que = g_event_que_idx;
811 if (++g_event_que_idx >= EVENT_QUEUE_MAX)
814 EM_DEBUG_LOG("g_event_que[%d].type [%d]", g_active_que, g_event_que[g_active_que].type);
818 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
821 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
823 if (err_code != NULL)
826 EM_DEBUG_FUNC_END("ret [%d]", ret);
830 /* check that event_data loop is continuous */
831 static int emcore_event_loop_continue()
838 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
840 EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
843 EM_DEBUG_EXCEPTION("\t event_data[%p], handle[%p]", event_data, handle);
845 if (err_code != NULL)
846 *err_code = EMAIL_ERROR_INVALID_PARAM;
851 int error = EMAIL_ERROR_NONE;
853 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
855 if (!g_send_event_que[g_send_event_que_idx].type) {
856 /* if current buffer has not event_data, insert event_data data to current buffer */
857 EM_DEBUG_LOG("Current buffer has not a event_data. [%d]", g_send_event_que_idx);
858 memcpy(g_send_event_que+g_send_event_que_idx, event_data, sizeof(email_event_t));
860 g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
863 *handle = g_send_event_que_idx;
866 /* if current buffer has event_data, find the empty buffer */
867 EM_DEBUG_LOG("Current buffer has a event_data. [%d]", g_send_event_que_idx);
868 int i, j = g_send_event_que_idx + 1;
870 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++) {
871 if (j >= EVENT_QUEUE_MAX)
874 if (!g_send_event_que[j].type)
878 if (i < EVENT_QUEUE_MAX) {
879 EM_DEBUG_LOG("I found available buffer. [%d]", j);
880 memcpy(g_send_event_que+j, event_data, sizeof(email_event_t));
881 g_send_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
882 if (handle) *handle = j;
885 EM_DEBUG_EXCEPTION("event_data queue is full...");
886 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
891 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
894 ENTER_CRITICAL_SECTION(_send_event_available_lock);
895 WAKE_CONDITION_VARIABLE(_send_event_available_signal);
896 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
900 EM_DEBUG_LOG("emcore_insert_event_for_sending_mails-handle[%d]", *handle);
902 if (err_code != NULL)
905 /* EM_DEBUG_FUNC_BEGIN(); */
910 static int emcore_retrieve_send_event(email_event_t *event_data, int *err_code)
912 EM_DEBUG_FUNC_BEGIN();
915 int error = EMAIL_ERROR_NONE;
917 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
918 /* get a event_data if this queue is not empty */
919 if (g_send_event_que[g_send_event_que_idx].type) {
920 memcpy(event_data, g_send_event_que+g_send_event_que_idx, sizeof(email_event_t));
922 if (event_data->status != EMAIL_EVENT_STATUS_WAIT) {
923 memset(g_send_event_que+g_send_event_que_idx, 0x00, sizeof(email_event_t));
924 g_send_active_que = 0;
927 g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
928 EM_DEBUG_LOG("g_send_event_que_idx[%d]", g_send_event_que_idx);
929 g_send_active_que = g_send_event_que_idx;
934 if (++g_send_event_que_idx >= EVENT_QUEUE_MAX)
935 g_send_event_que_idx = 1;
937 EM_DEBUG_LOG("\t g_send_event_que[%d].type = %d", g_send_active_que, g_send_event_que[g_send_active_que].type);
940 EM_DEBUG_LOG("\t send event_data queue is empty...");
941 g_send_active_que = 0;
942 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
945 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
947 if (err_code != NULL)
954 void* thread_func_branch_command_for_sending_mails(void *arg)
956 EM_DEBUG_FUNC_BEGIN();
957 int err = EMAIL_ERROR_NONE;
958 email_event_t event_data;
959 email_session_t *session = NULL;
961 if (!emstorage_open(&err)) {
962 EM_DEBUG_EXCEPTION("\t emstorage_open falied - %d", err);
966 while (g_send_event_loop) {
967 if (!emcore_get_empty_session(&session))
968 EM_DEBUG_EXCEPTION("\t SEND THREAD emcore_get_empty_session failed...");
970 if (!emcore_retrieve_send_event(&event_data, NULL)) {
971 EM_DEBUG_LOG(">>>> waiting for send event_data>>>>>>>>>");
972 #ifdef __FEATURE_LOCAL_ACTIVITY__
973 if (send_thread_run && g_save_local_activity_run) {
974 emstorage_account_tbl_t *account_list = NULL;
976 if (!emstorage_get_account_list(&count, &account_list, true, true, &err)) {
977 EM_DEBUG_LOG("\t emstorage_get_account_list failed - %d", err);
980 for (i = 0; i < count; i++) {
981 if (emcore_save_local_activity_sync(account_list[i].account_id, &err)) {
982 EM_DEBUG_LOG("Found local activity...!");
983 EM_DEBUG_LOG("Resetting g_save_local_activity_run ");
984 g_save_local_activity_run = 0;
985 emcore_clear_session(session);
989 emstorage_free_account(&account_list, count, &err);
991 if (!g_save_local_activity_run) {
999 ENTER_CRITICAL_SECTION(_send_event_available_lock);
1000 SLEEP_CONDITION_VARIABLE(_send_event_available_signal, _send_event_available_lock);
1001 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
1004 EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
1005 send_thread_run = 1;
1008 if (!emnetwork_check_network_status( &err)) {
1009 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1011 emcore_show_user_message(event_data.event_param_data_4, EMAIL_ACTION_SEND_MAIL, err);
1012 if (!emcore_notify_network_event(NOTI_SEND_FAIL, event_data.account_id, NULL , event_data.event_param_data_4, err))
1013 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_FAIL] Failed >>>> ");
1017 switch (event_data.type) {
1019 case EMAIL_EVENT_SEND_MAIL:
1020 emdevice_set_dimming_on_off(false, NULL);
1022 if (!emcore_send_mail(event_data.account_id, event_data.event_param_data_5, event_data.event_param_data_4, &err))
1023 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
1025 emdevice_set_dimming_on_off(true, NULL);
1028 case EMAIL_EVENT_SEND_MAIL_SAVED: /* send mails to been saved in off-line mode */
1029 emdevice_set_dimming_on_off(false, NULL);
1031 if (!emcore_send_saved_mail(event_data.account_id, event_data.event_param_data_3, &err))
1032 EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);
1034 emdevice_set_dimming_on_off(true, NULL);
1037 #ifdef __FEATURE_LOCAL_ACTIVITY__
1039 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1040 emdevice_set_dimming_on_off(false, NULL);
1041 emstorage_activity_tbl_t *local_activity = NULL;
1042 int activity_id_count = 0;
1043 int activity_chunk_count = 0;
1044 int *activity_id_list = NULL;
1047 if (false == emstorage_get_activity_id_list(event_data.account_id, &activity_id_list, &activity_id_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1048 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1051 for (i = 0; i < activity_id_count; ++i) {
1052 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)) {
1053 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);
1056 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1057 switch (local_activity[0].activity_type) {
1058 case ACTIVITY_SAVEMAIL: {
1059 if (!emcore_sync_mail_from_client_to_server(event_data.account_id, local_activity[0].mail_id, &err)) {
1060 EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
1065 case ACTIVITY_DELETEMAIL_SEND: /* New Activity Type Added for Race Condition and Crash Fix */ {
1066 if (!emcore_delete_mail(local_activity[0].account_id,
1067 &local_activity[0].mail_id,
1068 EMAIL_DELETE_FOR_SEND_THREAD,
1070 EMAIL_DELETED_BY_COMMAND,
1073 EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1079 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1084 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1086 if (g_save_local_activity_run == 1) {
1087 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1093 if (false == emstorage_free_activity_id_list(activity_id_list, &err)) {
1094 EM_DEBUG_LOG("emstorage_free_activity_id_list failed");
1098 emdevice_set_dimming_on_off(true, NULL);
1101 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1103 EM_DEBUG_LOG("Others not supported by Send Thread..! [%d]", event_data.type);
1107 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1108 memset(g_send_event_que+g_send_active_que, 0x00, sizeof(email_event_t));
1109 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1114 emcore_clear_session(session);
1117 if (!emstorage_close(&err))
1118 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
1120 EM_DEBUG_FUNC_END("err [%d]", err);
1125 int event_handler_EMAIL_EVENT_SYNC_HEADER(int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error)
1127 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);
1129 int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
1130 int mailbox_count = 0, account_count = 0;
1131 int counter, account_index;
1132 int unread = 0, total_unread = 0;
1133 emcore_uid_list *uid_list = NULL;
1134 emstorage_account_tbl_t *account_tbl_array = NULL;
1135 emstorage_mailbox_tbl_t *mailbox_tbl_target = NULL, *mailbox_tbl_spam = NULL, *mailbox_tbl_list = NULL;
1136 #ifndef __FEATURE_KEEP_CONNECTION__
1137 MAILSTREAM *stream = NULL;
1139 char mailbox_id_param_string[10] = {0,};
1140 char *input_mailbox_id_str = NULL;
1142 if (input_mailbox_id == 0)
1143 sync_type = EMAIL_SYNC_ALL_MAILBOX;
1145 if (!emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
1146 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1150 if(mailbox_tbl_target)
1151 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_target->mailbox_id);
1153 input_mailbox_id_str = (input_mailbox_id == 0)? NULL: mailbox_id_param_string;
1155 if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, input_mailbox_id_str, handle_to_be_published, 0))
1156 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
1158 if (!emnetwork_check_network_status(&err)) {
1159 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1161 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
1162 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1165 if (sync_type != EMAIL_SYNC_ALL_MAILBOX) { /* Sync only particular mailbox */
1167 if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1168 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1170 if (!emstorage_get_mailbox_by_mailbox_type(input_account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1171 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1174 if (!emcore_sync_header(mailbox_tbl_target, mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1175 EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
1176 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
1177 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1180 EM_DEBUG_LOG("emcore_sync_header succeeded [%d]", err);
1181 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
1182 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1185 total_unread += unread;
1187 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)
1188 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1190 if (!emcore_finalize_sync(input_account_id, &err))
1191 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1193 else /* All Foder */ {
1194 EM_DEBUG_LOG(">>>> SYNC ALL MAILBOX ");
1195 /* Sync of all mailbox */
1197 if (input_account_id == ALL_ACCOUNT) {
1198 if ((err = emcore_update_sync_status_of_account(ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1199 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1201 if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &err)) {
1202 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
1203 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL, handle_to_be_published, err))
1204 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1209 EM_DEBUG_LOG("Sync all mailbox of an account[%d].", input_account_id);
1211 if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1212 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1214 if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
1215 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
1216 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
1217 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1223 for (account_index = 0 ; account_index < account_count; account_index++) {
1224 if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1225 EM_DEBUG_LOG("account[%d] is for ActiveSync. Skip ", account_index);
1229 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) {
1230 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
1232 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, input_mailbox_id_str, handle_to_be_published, err))
1233 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1238 EM_DEBUG_LOG("emcore_get_mailbox_list_to_be_sync returns [%d] mailboxes", mailbox_count);
1240 if(mailbox_tbl_spam) {
1241 if (!emstorage_free_mailbox(&mailbox_tbl_spam, 1, &err)) {
1242 EM_DEBUG_EXCEPTION("emstorage_free_mailbox failed [%d]", err);
1244 mailbox_tbl_spam = NULL;
1247 if (!emstorage_get_mailbox_by_mailbox_type(account_tbl_array[account_index].account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1248 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1251 if (mailbox_count > 0) {
1252 #ifndef __FEATURE_KEEP_CONNECTION__
1253 if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1254 memset(mailbox_id_param_string, 0, 10);
1255 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
1256 if (!emcore_connect_to_remote_mailbox(account_tbl_array[account_index].account_id, mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err)) {
1257 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1258 if (err == EMAIL_ERROR_LOGIN_FAILURE)
1259 EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
1261 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string, handle_to_be_published, err))
1262 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1265 EM_DEBUG_LOG("emcore_connect_to_remote_mailbox returns [%d] : ", err);
1272 for (counter = 0; counter < mailbox_count; counter++) {
1274 EM_DEBUG_LOG("maiblox_name [%s], mailbox_id [%d], mailbox_type [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, mailbox_tbl_list[counter].mailbox_type);
1276 if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
1277 || mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
1278 /*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */)
1279 EM_DEBUG_LOG("Skipped for all emails or trash");
1280 else if (!mailbox_tbl_list[counter].local_yn) {
1281 EM_DEBUG_LOG("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
1282 #ifdef __FEATURE_KEEP_CONNECTION__
1283 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1284 #else /* __FEATURE_KEEP_CONNECTION__ */
1285 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, (void *)stream, &uid_list, &unread, &err)) {
1286 #endif /* __FEATURE_KEEP_CONNECTION__ */
1287 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);
1289 #ifndef __FEATURE_KEEP_CONNECTION__
1290 if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || err == EMAIL_ERROR_SOCKET_FAILURE)
1291 stream = NULL; /* Don't retry to connect for broken connection. It might cause crash. */
1292 #endif /* __FEATURE_KEEP_CONNECTION__ */
1293 memset(mailbox_id_param_string, 0, 10);
1294 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
1295 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string, handle_to_be_published, err))
1296 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1301 total_unread += unread;
1304 EM_DEBUG_LOG("Sync for account_id(%d) is completed....!", account_tbl_array[account_index].account_id);
1305 if ((err == EMAIL_ERROR_NONE) && !emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
1306 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1308 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)
1309 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1311 if (!emcore_finalize_sync(account_tbl_array[account_index].account_id, &err))
1312 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1313 #ifndef __FEATURE_KEEP_CONNECTION__
1315 emcore_close_mailbox(0, stream);
1319 if (mailbox_tbl_list) {
1320 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1321 mailbox_tbl_list = NULL;
1331 #ifndef __FEATURE_KEEP_CONNECTION__
1333 emcore_close_mailbox(0, stream);
1335 if(mailbox_tbl_target)
1336 emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);
1338 if (mailbox_tbl_list)
1339 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1341 if (account_tbl_array)
1342 emstorage_free_account(&account_tbl_array, account_count, NULL);
1345 EM_DEBUG_FUNC_END();
1349 int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error)
1351 EM_DEBUG_FUNC_BEGIN("account [%p]", account);
1352 int err, ret = false;
1355 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1356 err = EMAIL_ERROR_INVALID_PARAM;
1360 EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);
1362 if (!emnetwork_check_network_status(&err)) {
1363 emcore_delete_account_from_unvalidated_account_list(account->account_id);
1364 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1365 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL, handle_to_be_published, err))
1366 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1370 EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);
1372 if (!emcore_validate_account_with_account_info(account, &err)) {
1373 emcore_delete_account_from_unvalidated_account_list(account->account_id);
1374 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err : %d", err);
1375 if (err == EMAIL_ERROR_CANCELLED) {
1376 EM_DEBUG_EXCEPTION(" notify : NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1377 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL, account->account_id, NULL, handle_to_be_published, err))
1378 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL] Failed");
1385 emcore_delete_account_from_unvalidated_account_list(account->account_id);
1387 if (emcore_create_account(account, &err) == false) {
1388 EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
1392 emcore_init_account_reference();
1394 EM_DEBUG_LOG("incoming_server_type [%d]", account->incoming_server_type);
1396 if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
1397 if (!emcore_sync_mailbox_list(account->account_id, "", handle_to_be_published, &err)) {
1398 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync failed [%d]", err);
1399 /* delete account whose mailbox couldn't be obtained from server */
1400 emcore_delete_account(account->account_id, NULL);
1405 EM_DEBUG_LOG("validating and creating an account are succeeded for account id [%d] err [%d]", account->account_id, err);
1406 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, NULL, handle_to_be_published, err))
1407 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
1414 if (ret == false && err != EMAIL_ERROR_CANCELLED && account) {
1415 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL, handle_to_be_published, err))
1416 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1422 EM_DEBUG_FUNC_END();
1426 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)
1428 EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
1429 int err, ret = false;
1430 emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;
1432 if (!new_account_info) {
1433 EM_DEBUG_EXCEPTION("Invalid Parameter");
1434 err = EMAIL_ERROR_INVALID_PARAM;
1438 if (!emnetwork_check_network_status(&err)) {
1439 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1441 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL, handle_to_be_published, err))
1442 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
1446 EM_DEBUG_LOG("incoming_server_address: (%s)", new_account_info->incoming_server_address);
1448 if (!emcore_validate_account_with_account_info(new_account_info, &err)) {
1449 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err : %d", err);
1450 if (err == EMAIL_ERROR_CANCELLED) {
1451 EM_DEBUG_EXCEPTION(" notify : NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1452 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, new_account_info->account_id, NULL, handle_to_be_published, err))
1453 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
1461 if (!emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
1462 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1463 /* goto FINISH_OFF; */
1466 new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
1467 if (!new_account_tbl) {
1468 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
1472 em_convert_account_to_account_tbl(new_account_info, new_account_tbl);
1474 if (emstorage_update_account(account_id, new_account_tbl, true, &err)) {
1475 emcore_init_account_reference();
1478 EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", new_account_info->account_id, err);
1479 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, NULL, handle_to_be_published, err))
1480 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
1487 if (old_account_tbl)
1488 emstorage_free_account(&old_account_tbl, 1, NULL);
1489 if (new_account_tbl)
1490 emstorage_free_account(&new_account_tbl, 1, NULL);
1492 if (ret == false && err != EMAIL_ERROR_CANCELLED && new_account_info) {
1493 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL, handle_to_be_published, err))
1494 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1500 EM_DEBUG_FUNC_END();
1504 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)
1506 EM_DEBUG_FUNC_BEGIN();
1508 emcore_set_mail_slot_size(account_id, mailbox_id, new_slot_size, error);
1510 EM_DEBUG_FUNC_END();
1514 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id)
1516 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
1517 int err = EMAIL_ERROR_NONE;
1519 if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1520 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
1524 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1525 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1531 EM_DEBUG_FUNC_END("err [%d]", err);
1535 #ifdef __FEATURE_LOCAL_ACTIVITY__
1536 int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
1538 EM_DEBUG_FUNC_BEGIN();
1540 int err = EMAIL_ERROR_NONE;
1541 email_mailbox_t mailbox;
1542 emstorage_activity_tbl_t *local_activity = NULL;
1543 int activity_id_count = 0;
1544 int activity_chunk_count = 0;
1545 int *activity_id_list = NULL;
1548 if (!emnetwork_check_network_status(&err))
1549 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1551 if (false == emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1552 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1555 for (i = 0; i < activity_id_count; ++i) {
1556 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))
1557 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);
1559 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1560 switch (local_activity[0].activity_type) {
1561 case ACTIVITY_MODIFYFLAG: {
1562 if (emcore_sync_flag_with_server(local_activity[0].mail_id , &err)) {
1563 if (!emcore_delete_activity(&local_activity[0], &err))
1564 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1569 case ACTIVITY_DELETEMAIL:
1570 case ACTIVITY_MOVEMAIL:
1571 case ACTIVITY_MODIFYSEENFLAG:
1572 case ACTIVITY_COPYMAIL: {
1575 int total_mail_ids = activity_chunk_count;
1577 int *mail_id_list = NULL;
1579 mail_id_list = (int *)em_malloc(sizeof(int) * total_mail_ids);
1581 if (NULL == mail_id_list) {
1582 EM_DEBUG_EXCEPTION("malloc failed... ");
1588 for (j = 0; j < BULK_OPERATION_COUNT && (k < total_mail_ids); ++j, ++k)
1589 mail_id_list[j] = local_activity[k].mail_id;
1591 switch (local_activity[k-1].activity_type) {
1592 case ACTIVITY_DELETEMAIL: {
1593 if (!emcore_delete_mail(local_activity[k-1].account_id,
1596 EMAIL_DELETE_LOCAL_AND_SERVER,
1597 EMAIL_DELETED_BY_COMMAND,
1600 EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1604 case ACTIVITY_MOVEMAIL: {
1605 if (!emcore_move_mail_on_server_ex(local_activity[k-1].account_id ,
1606 local_activity[k-1].src_mbox,
1609 local_activity[k-1].dest_mbox,
1611 EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
1614 case ACTIVITY_MODIFYSEENFLAG: {
1615 int seen_flag = atoi(local_activity[0].src_mbox);
1616 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 */
1617 EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
1622 } while (k < total_mail_ids);
1624 EM_SAFE_FREE(mail_id_list);
1630 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1634 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1636 if (g_local_activity_run == 1) {
1637 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1644 if (activity_id_list) {
1645 if (false == emstorage_free_activity_id_list(activity_id_list, &err))
1646 EM_DEBUG_EXCEPTION("emstorage_free_activity_id_list failed");
1652 EM_DEBUG_FUNC_END();
1656 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1658 int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error)
1660 EM_DEBUG_FUNC_BEGIN();
1662 int err = EMAIL_ERROR_NONE;
1663 email_mailbox_t mailbox;
1665 memset(&mailbox, 0x00, sizeof(mailbox));
1666 mailbox.account_id = account_id;
1668 if (!emnetwork_check_network_status(&err)) {
1669 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1671 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1674 if (!emcore_download_body_multi_sections_bulk(NULL,
1677 option >> 1, /* 0 : silent, 1 : verbose */
1678 option & 0x01, /* 0 : without attachments, 1 : with attachments */
1680 handle_to_be_published,
1682 EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed - %d", err);
1688 EM_DEBUG_FUNC_END();
1692 int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
1694 EM_DEBUG_FUNC_BEGIN();
1696 int err = EMAIL_ERROR_NONE;
1698 EM_DEBUG_LOG("attachment_no is %d", attachment_no);
1700 if (!emnetwork_check_network_status(&err)) {
1701 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1702 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
1706 #ifdef __ATTACHMENT_OPTI__
1707 if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, &err))
1708 EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1710 if (!emcore_download_attachment(account_id, mail_id, attachment_no, &err))
1711 EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1718 EM_DEBUG_FUNC_END();
1722 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)
1724 EM_DEBUG_FUNC_BEGIN();
1726 int err = EMAIL_ERROR_NONE;
1728 if (!emnetwork_check_network_status(&err))
1729 EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
1730 else if (!emcore_sync_flags_field_with_server(mail_ids, num, field_type, value, &err))
1731 EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
1736 EM_DEBUG_FUNC_END();
1740 int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error)
1742 EM_DEBUG_FUNC_BEGIN();
1744 int err = EMAIL_ERROR_NONE;
1746 if (!emnetwork_check_network_status(&err)) {
1747 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1749 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL, handle_to_be_published, err))
1750 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>>");
1754 if (!emcore_validate_account(account_id, &err)) {
1755 EM_DEBUG_EXCEPTION("emcore_validate_account failed account id : %d err : %d", account_id, err);
1757 if (err == EMAIL_ERROR_CANCELLED) {
1758 EM_DEBUG_EXCEPTION("notify : NOTI_VALIDATE_ACCOUNT_CANCEL ");
1759 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, account_id, NULL, handle_to_be_published, err))
1760 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
1763 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL, handle_to_be_published, err))
1764 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1768 email_account_t *account_ref = NULL;
1769 account_ref = emcore_get_account_reference(account_id);
1772 EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
1773 if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
1774 if (!emcore_check_thread_status())
1775 err = EMAIL_ERROR_CANCELLED;
1776 else if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
1777 EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
1781 EM_DEBUG_EXCEPTION("emcore_validate_account succeeded account id : %d err : %d", account_id, err);
1782 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, account_id, NULL, handle_to_be_published, err))
1783 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success >>>>");
1786 emcore_free_account(account_ref);
1787 EM_SAFE_FREE(account_ref);
1795 EM_DEBUG_FUNC_END();
1799 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)
1801 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);
1802 int err = EMAIL_ERROR_NONE;
1804 if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
1805 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1808 emcore_free_mail_data_list(&input_mail_data, 1);
1810 if(input_attachment_data_list)
1811 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1813 if(input_meeting_request)
1814 emstorage_free_meeting_request(input_meeting_request);
1816 EM_DEBUG_FUNC_END("err [%d", err);
1819 int event_handler_EMAIL_EVENT_SAVE_MAIL(int input_account_id, int input_mail_id, int input_handle_to_be_published)
1821 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mail_id [%d] input_handle_to_be_published [%d]", input_account_id, input_mail_id, input_handle_to_be_published);
1822 int err = EMAIL_ERROR_NONE;
1824 err = emcore_sync_mail_from_client_to_server(input_mail_id);
1826 EM_DEBUG_FUNC_END("err [%d]", err);
1830 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)
1832 EM_DEBUG_FUNC_BEGIN();
1833 int err = EMAIL_ERROR_NONE, ret = false;
1834 email_account_t *account_ref = NULL;
1836 if (!(account_ref = emcore_get_account_reference(account_id))) {
1837 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1838 err = EMAIL_ERROR_INVALID_ACCOUNT;
1842 /* Move mail local */
1844 if (!emcore_mail_move(mail_ids, mail_id_count, dest_mailbox.mailbox_name, EMAIL_MOVED_BY_COMMAND, 0, &err)) {
1845 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
1850 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1851 /* Move mail on server */
1852 if (!emnetwork_check_network_status(&err))
1853 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1855 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1856 if (!emcore_move_mail_on_server_ex(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1857 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
1859 if (!emcore_move_mail_on_server(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1860 EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
1867 EM_SAFE_FREE(mail_ids); /*prevent 33693*/
1870 emcore_free_account(account_ref);
1871 EM_SAFE_FREE(account_ref);
1877 EM_DEBUG_FUNC_END();
1881 int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int handle_to_be_published, int *error)
1883 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);
1884 int err = EMAIL_ERROR_NONE;
1886 if (!emnetwork_check_network_status(&err))
1887 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1889 if (!emcore_delete_mailbox(mailbox_id, on_server, &err))
1890 EM_DEBUG_LOG("emcore_delete failed - %d", err);
1896 EM_DEBUG_FUNC_END();
1900 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)
1902 EM_DEBUG_FUNC_BEGIN();
1903 int err = EMAIL_ERROR_NONE;
1904 email_mailbox_t mailbox;
1906 memset(&mailbox, 0x00, sizeof(mailbox));
1908 mailbox.account_id = account_id;
1909 mailbox.mailbox_name = mailbox_name;
1910 mailbox.alias = mailbox_alias;
1911 mailbox.mailbox_type = mailbox_type;
1913 if (!emnetwork_check_network_status(&err)) {
1914 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1917 if (!emcore_create_mailbox(&mailbox, on_server, &err))
1918 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
1925 EM_DEBUG_FUNC_END();
1929 int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error)
1931 EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);
1933 int err = EMAIL_ERROR_NONE;
1935 if (!emnetwork_check_network_status(&err))
1936 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1938 if (!emcore_sync_flag_with_server(mail_id, &err))
1939 EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
1940 #ifdef __FEATURE_LOCAL_ACTIVITY__
1942 emstorage_activity_tbl_t new_activity;
1943 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1944 new_activity.activity_type = ACTIVITY_MODIFYFLAG;
1945 new_activity.account_id = event_data.account_id;
1946 new_activity.mail_id = event_data.event_param_data_4;
1947 new_activity.dest_mbox = NULL;
1948 new_activity.server_mailid = NULL;
1949 new_activity.src_mbox = NULL;
1951 if (!emcore_delete_activity(&new_activity, &err))
1952 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1954 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1960 EM_DEBUG_FUNC_END();
1964 int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int input_mailbox_id, int input_from_server, int *error)
1966 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id [%d], input_from_server [%d], error [%p]", input_account_id, input_mailbox_id, input_from_server, error);
1967 int err = EMAIL_ERROR_NONE;
1969 if (!emcore_delete_all_mails_of_mailbox(input_account_id, input_mailbox_id, input_from_server, &err))
1970 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
1975 EM_DEBUG_FUNC_END("err [%d]", err);
1979 int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error)
1981 EM_DEBUG_FUNC_BEGIN();
1982 int err = EMAIL_ERROR_NONE;
1984 email_account_t *account_ref = NULL;
1986 if (!(account_ref = emcore_get_account_reference(account_id))) {
1987 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1988 err = EMAIL_ERROR_INVALID_ACCOUNT;
1992 if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_FROM_SERVER, EMAIL_DELETED_BY_COMMAND, false, &err)) {
1993 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
2001 emcore_free_account(account_ref);
2002 EM_SAFE_FREE(account_ref);
2008 EM_DEBUG_FUNC_END();
2012 int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int handle_to_be_published, int *error)
2014 EM_DEBUG_FUNC_BEGIN();
2015 int err = EMAIL_ERROR_NONE;
2017 if (!emnetwork_check_network_status(&err)) {
2018 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2019 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_id, maibox_name, 0, err))
2020 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
2023 EM_DEBUG_LOG("Sync of all mailbox");
2024 if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
2025 EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2031 EM_DEBUG_FUNC_END();
2035 int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, int handle_to_be_published, int *error)
2037 EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], criteria : [%s]", account_id, mailbox_id, criteria);
2039 int err = EMAIL_ERROR_NONE;
2043 char temp_uid_string[20] = {0,};
2045 emcore_uid_list uid_elem;
2046 emstorage_mailbox_tbl_t *search_mailbox = NULL;
2047 emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
2049 MAILSTREAM *stream = NULL;
2050 MESSAGECACHE *mail_cache_element = NULL;
2051 ENVELOPE *env = NULL;
2052 emstorage_mailbox_tbl_t* local_mailbox = NULL;
2053 char mailbox_id_param_string[10] = {0,};
2055 if (account_id < 0 || mailbox_id == 0) {
2056 EM_DEBUG_EXCEPTION("Invalid parameter");
2057 err = EMAIL_ERROR_INVALID_PARAM;
2061 if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
2062 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
2066 SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);
2068 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_START, account_id, mailbox_id_param_string, handle_to_be_published, 0))
2069 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_START] failed >>>>");
2071 if (!emnetwork_check_network_status(&err)) {
2072 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2073 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, 0, err))
2074 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
2078 if (!emcore_connect_to_remote_mailbox(account_id, mailbox_id, (void **)&stream, &err)) {
2079 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
2080 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2081 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2085 if (!mail_search_full(stream, NIL, mail_criteria(criteria), SE_FREE)) {
2086 EM_DEBUG_EXCEPTION("mail_search failed");
2087 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2088 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2092 for (i = 1; i <= stream->nmsgs; ++i) {
2093 mail_cache_element = mail_elt(stream, i);
2094 if (mail_cache_element->searched) {
2095 env = mail_fetchstructure_full(stream, i, NULL, FT_PEEK);
2097 memset(&uid_elem, 0x00, sizeof(uid_elem));
2099 uid_elem.msgno = mail_cache_element->msgno;
2100 SNPRINTF(temp_uid_string, 20, "%4lu", mail_cache_element->private.uid);
2101 uid_elem.uid = temp_uid_string;
2102 uid_elem.flag.seen = mail_cache_element->seen;
2104 if (!emcore_make_mail_tbl_data_from_envelope(stream, env, &uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
2105 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
2106 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2107 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2111 search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
2112 if (search_mailbox == NULL) {
2113 EM_DEBUG_EXCEPTION("em_malloc failed");
2114 err = EMAIL_ERROR_OUT_OF_MEMORY;
2115 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2116 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2120 search_mailbox->account_id = account_id;
2121 search_mailbox->mailbox_id = mailbox_id;
2122 search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
2123 search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
2125 if ((err = emcore_add_mail_to_mailbox(search_mailbox, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
2126 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
2127 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2128 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2131 memset(mailbox_id_param_string, 0, 10);
2132 SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
2133 if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id)) {
2134 EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
2137 if (new_mail_tbl_data) {
2138 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2139 new_mail_tbl_data = NULL;
2144 if (err == EMAIL_ERROR_NONE && !emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH, account_id, NULL, handle_to_be_published, 0))
2145 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");
2149 if (search_mailbox != NULL)
2150 emstorage_free_mailbox(&search_mailbox, 1, NULL);
2152 if (new_mail_tbl_data)
2153 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2156 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2161 EM_DEBUG_FUNC_END();
2165 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, char *input_new_mailbox_alias, int handle_to_be_published)
2167 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_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, input_new_mailbox_alias, handle_to_be_published);
2168 int err = EMAIL_ERROR_NONE;
2170 if ((err = emcore_move_mailbox_on_imap_server(input_account_id, input_old_mailbox_path, input_new_mailbox_path)) != EMAIL_ERROR_NONE) {
2171 EM_DEBUG_EXCEPTION("emcore_move_mailbox_on_imap_server failed [%d]", err);
2174 if (err == EMAIL_ERROR_NONE) {
2175 if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
2176 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
2178 if ((err = emstorage_rename_mailbox(input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, true)) != EMAIL_ERROR_NONE) {
2179 EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
2182 else if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0)) {
2183 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
2186 EM_DEBUG_FUNC_END("err [%d]", err);
2190 void* thread_func_branch_command(void *arg)
2192 EM_DEBUG_FUNC_BEGIN();
2194 int err = EMAIL_ERROR_NONE;
2195 int is_storage_full = false;
2197 email_event_t event_data;
2198 email_session_t *session = NULL;
2199 emstorage_account_tbl_t *account_tbl = NULL;
2200 int handle_to_be_published = 0;
2202 if (!emstorage_open(&err)) {
2203 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
2207 /* check that event_data loop is continuous */
2208 while (emcore_event_loop_continue()) {
2209 if (!emcore_get_empty_session(&session))
2210 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
2212 /* get a event_data from event_data queue */
2213 if (!emcore_retrieve_event(&event_data, NULL)) { /* no event_data pending */
2214 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);
2215 #ifdef ENABLE_IMAP_IDLE_THREAD
2216 if ( !emnetwork_check_network_status(&err)) {
2217 EM_DEBUG_LOG(">>>> Data Networking ON ");
2219 if (g_imap_idle_thread_alive) {
2220 /* emcore_kill_imap_idle_thread(NULL); */
2221 /* emcore_create_imap_idle_thread(NULL); */
2224 if (!send_thread_run)
2225 emcore_create_imap_idle_thread(event_data.account_id, NULL);
2229 #endif /* ENABLE_IMAP_IDLE_THREAD */
2230 #ifdef __FEATURE_LOCAL_ACTIVITY__
2231 /* Local activity sync */
2232 if (g_client_run && g_local_activity_run) {
2233 emstorage_account_tbl_t *account_list = NULL;
2235 if (!emstorage_get_account_list(&count, &account_list, true, true, &err))
2236 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
2238 for (i = 0; i < count; i++) {
2239 if (emcore_local_activity_sync(account_list[i].account_id, &err)) {
2240 EM_DEBUG_LOG("Found local activity...!");
2241 EM_DEBUG_LOG("Resetting g_local_activity_run ");
2242 g_local_activity_run = 0;
2243 emcore_clear_session(session);
2247 emstorage_free_account(&account_list, count, &err);
2249 if (!g_local_activity_run)
2255 recv_thread_run = 0;
2257 ENTER_CRITICAL_SECTION(_event_available_lock);
2258 SLEEP_CONDITION_VARIABLE(_event_available_signal, _event_available_lock);
2259 EM_DEBUG_LOG("Wake up by _event_available_signal");
2260 LEAVE_CRITICAL_SECTION(_event_available_lock);
2263 EM_DEBUG_LOG(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
2264 EM_DEBUG_LOG("For handle g_event_que_idx - %d", g_event_que_idx);
2266 if (g_event_que_idx == 1)
2267 handle_to_be_published = 31;
2269 handle_to_be_published = g_event_que_idx - 1 ;
2271 EM_DEBUG_LOG("Handle to be Published [%d]", handle_to_be_published);
2272 recv_thread_run = 1;
2275 /* Handling storage full */
2276 is_storage_full = false;
2277 if (event_data.type == EMAIL_EVENT_SYNC_HEADER || event_data.type == EMAIL_EVENT_SYNC_HEADER_OMA ||
2278 event_data.type == EMAIL_EVENT_DOWNLOAD_BODY || event_data.type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
2279 if (emcore_is_storage_full(&err) == true) {
2280 EM_DEBUG_EXCEPTION("Storage is full");
2281 switch (event_data.type) {
2282 case EMAIL_EVENT_SYNC_HEADER:
2283 case EMAIL_EVENT_SYNC_HEADER_OMA:
2284 noti_id = NOTI_DOWNLOAD_FAIL;
2286 case EMAIL_EVENT_DOWNLOAD_BODY:
2287 noti_id = NOTI_DOWNLOAD_BODY_FAIL;
2289 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2290 noti_id = NOTI_DOWNLOAD_ATTACH_FAIL;
2296 if (!emcore_notify_network_event(noti_id, event_data.account_id, NULL, handle_to_be_published, err))
2297 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
2298 is_storage_full = true;
2302 emdevice_set_dimming_on_off(false, NULL);
2304 if (event_data.account_id > 0) {
2305 if (!emstorage_get_account_by_id(event_data.account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
2306 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
2311 EM_DEBUG_LOG("account_id : [%d], sync_disabled : [%d]", event_data.account_id, account_tbl->sync_disabled);
2313 if (!account_tbl || account_tbl->sync_disabled == 0) {
2314 switch (event_data.type) {
2315 case EMAIL_EVENT_SYNC_IMAP_MAILBOX: /* get imap mailbox list */
2316 if (!emcore_sync_mailbox_list(event_data.account_id, event_data.event_param_data_3, handle_to_be_published, &err))
2317 EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2318 EM_SAFE_FREE(event_data.event_param_data_3);
2321 case EMAIL_EVENT_SYNC_HEADER: /* synchronize mail header */
2322 if (is_storage_full == false)
2323 event_handler_EMAIL_EVENT_SYNC_HEADER(event_data.account_id, event_data.event_param_data_5, handle_to_be_published, &err);
2326 case EMAIL_EVENT_SYNC_HEADER_OMA: /* synchronize mail header for OMA */
2327 if (is_storage_full == false)
2328 event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data.account_id, event_data.event_param_data_1, handle_to_be_published, &err);
2329 EM_SAFE_FREE(event_data.event_param_data_1);
2332 case EMAIL_EVENT_DOWNLOAD_BODY: /* download mail body */
2333 if (is_storage_full == false)
2334 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);
2335 event_data.event_param_data_3 = NULL; /* MUST BE */
2338 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT: /* download attachment */
2339 if (is_storage_full == false)
2340 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);
2343 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER: /* Sync flags field */
2344 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);
2345 EM_SAFE_FREE(event_data.event_param_data_3);
2348 case EMAIL_EVENT_DELETE_MAIL: /* delete mails */
2349 event_handler_EMAIL_EVENT_DELETE_MAIL(event_data.account_id, (int*)event_data.event_param_data_3, event_data.event_param_data_4, &err);
2350 EM_SAFE_FREE(event_data.event_param_data_3);
2353 case EMAIL_EVENT_DELETE_MAIL_ALL: /* delete all mails */
2354 event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data.account_id, event_data.event_param_data_4, (int)event_data.event_param_data_5, &err);
2356 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
2357 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2358 event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER((int)event_data.event_param_data_4, &err);
2362 case EMAIL_EVENT_CREATE_MAILBOX:
2363 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);
2364 EM_SAFE_FREE(event_data.event_param_data_1);
2365 EM_SAFE_FREE(event_data.event_param_data_2);
2368 case EMAIL_EVENT_DELETE_MAILBOX:
2369 event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data.event_param_data_4, event_data.event_param_data_4, handle_to_be_published, &err);
2372 case EMAIL_EVENT_SAVE_MAIL:
2373 err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data.account_id, event_data.event_param_data_4, handle_to_be_published);
2376 case EMAIL_EVENT_MOVE_MAIL:
2377 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);
2378 event_data.event_param_data_3 = NULL; /*prevent 33693*/
2381 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2382 event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data.account_id, handle_to_be_published, &err);
2385 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
2386 /* event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(emcore_get_account_from_unvalidated_account_list(), handle_to_be_published, &err);*/
2387 email_account_t *account = (email_account_t *)event_data.event_param_data_1;
2388 event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(account, handle_to_be_published, &err);
2390 emcore_free_account(account);
2391 EM_SAFE_FREE(account);
2396 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
2397 email_account_t *account = (email_account_t *)event_data.event_param_data_1;
2398 event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data.account_id, account, handle_to_be_published, &err);
2399 emcore_free_account(account);
2400 EM_SAFE_FREE(account);
2404 case EMAIL_EVENT_UPDATE_MAIL:
2405 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);
2407 event_data.event_param_data_1 = NULL;
2408 event_data.event_param_data_2 = NULL;
2409 event_data.event_param_data_3 = NULL;
2412 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2413 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);
2416 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
2417 err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data.account_id, event_data.event_param_data_4);
2420 #ifdef __FEATURE_LOCAL_ACTIVITY__
2421 case EMAIL_EVENT_LOCAL_ACTIVITY:
2422 event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data.account_id, &err);
2424 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
2426 case EMAIL_EVENT_SEARCH_ON_SERVER:
2427 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);
2428 EM_SAFE_FREE(event_data.event_param_data_1);
2431 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2432 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, (char*)event_data.event_param_data_3, handle_to_be_published);
2433 EM_SAFE_FREE(event_data.event_param_data_1);
2434 EM_SAFE_FREE(event_data.event_param_data_2);
2435 EM_SAFE_FREE(event_data.event_param_data_3);
2444 emstorage_free_account(&account_tbl, 1, NULL);
2448 if (!emcore_notify_response_to_api(event_data.type, handle_to_be_published, err))
2449 EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");
2451 emdevice_set_dimming_on_off(true, NULL);
2454 switch (event_data.type) {
2455 case EMAIL_EVENT_SEND_MAIL:
2456 case EMAIL_EVENT_SEND_MAIL_SAVED:
2457 _sending_busy_unref();
2460 case EMAIL_EVENT_SYNC_HEADER:
2461 case EMAIL_EVENT_SYNC_HEADER_OMA:
2462 case EMAIL_EVENT_DOWNLOAD_BODY:
2463 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2464 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2465 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2466 case EMAIL_EVENT_DELETE_MAIL:
2467 case EMAIL_EVENT_DELETE_MAIL_ALL:
2468 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2469 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2470 case EMAIL_EVENT_SAVE_MAIL:
2471 case EMAIL_EVENT_MOVE_MAIL:
2472 case EMAIL_EVENT_CREATE_MAILBOX:
2473 case EMAIL_EVENT_DELETE_MAILBOX:
2474 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2475 case EMAIL_EVENT_SEARCH_ON_SERVER:
2476 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2477 _receiving_busy_unref();
2484 event_data.type = 0;
2486 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2487 memset(g_event_que+g_active_que, 0x00, sizeof(email_event_t));
2488 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2491 emcore_clear_session(session);
2494 if (!emstorage_close(&err))
2495 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
2497 EM_DEBUG_FUNC_END();
2500 /*Send event_data loop*/
2501 INTERNAL_FUNC int emcore_start_event_loop_for_sending_mails(int *err_code)
2503 EM_DEBUG_FUNC_BEGIN();
2504 int thread_error = -1;
2506 if (err_code != NULL)
2507 *err_code = EMAIL_ERROR_NONE;
2509 memset(&g_send_event_que, 0x00, sizeof(g_send_event_que));
2511 if (g_send_srv_thread) {
2512 EM_DEBUG_EXCEPTION("\t send service thread is already running...");
2513 if (err_code != NULL)
2514 *err_code = EMAIL_ERROR_UNKNOWN;
2518 g_send_event_que_idx = 1;
2519 g_send_event_loop = 1;
2520 g_send_active_que = 0;
2522 /* initialize lock */
2523 /* INITIALIZE_CRITICAL_SECTION(_send_event_available_lock); */
2524 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2525 INITIALIZE_CONDITION_VARIABLE(_send_event_available_signal);
2528 THREAD_CREATE_JOINABLE(g_send_srv_thread, thread_func_branch_command_for_sending_mails, thread_error);
2530 if (thread_error != 0) {
2531 EM_DEBUG_EXCEPTION("cannot make thread...");
2532 if (err_code != NULL)
2533 *err_code = EMAIL_ERROR_UNKNOWN;
2537 if (err_code != NULL)
2538 *err_code = EMAIL_ERROR_NONE;
2539 EM_DEBUG_FUNC_END();
2543 /* finish api event_data loop */
2544 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
2546 EM_DEBUG_FUNC_BEGIN();
2548 if (err_code != NULL)
2549 *err_code = EMAIL_ERROR_NONE;
2551 if (!g_send_srv_thread) {
2552 if (err_code != NULL)
2553 *err_code = EMAIL_ERROR_UNKNOWN;
2557 /* stop event_data loop */
2558 g_send_event_loop = 0;
2560 emcore_cancel_send_mail_thread(g_send_active_que, NULL, err_code);
2561 ENTER_CRITICAL_SECTION(_send_event_available_lock);
2562 WAKE_CONDITION_VARIABLE(_send_event_available_signal); /* MUST BE HERE */
2563 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
2565 /* wait for thread finished */
2566 THREAD_JOIN(g_send_srv_thread);
2568 g_send_srv_thread = 0;
2570 DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2571 DELETE_CRITICAL_SECTION(_send_event_available_lock);
2572 DELETE_CONDITION_VARIABLE(_send_event_available_signal);
2574 g_send_event_que_idx = 1;
2575 g_send_active_que = 0;
2577 if (err_code != NULL)
2578 *err_code = EMAIL_ERROR_NONE;
2583 /* start api event_data loop */
2584 INTERNAL_FUNC int emcore_start_event_loop(int *err_code)
2586 EM_DEBUG_FUNC_BEGIN();
2589 if (err_code != NULL)
2590 *err_code = EMAIL_ERROR_NONE;
2592 memset(&g_event_que, 0x00, sizeof(g_event_que));
2595 EM_DEBUG_EXCEPTION("service thread is already running...");
2596 if (err_code != NULL)
2597 *err_code = EMAIL_ERROR_UNKNOWN;
2601 g_event_que_idx = 1;
2605 /* initialize lock */
2606 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2607 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2609 emcore_initialize_event_callback_table();
2612 THREAD_CREATE(g_srv_thread, thread_func_branch_command, NULL, thread_error);
2614 if (thread_error != 0) {
2615 EM_DEBUG_EXCEPTION("cannot create thread");
2616 if (err_code != NULL)
2617 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
2621 if (err_code != NULL)
2622 *err_code = EMAIL_ERROR_NONE;
2627 /* finish api event_data loop */
2628 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
2630 EM_DEBUG_FUNC_BEGIN();
2632 if (err_code != NULL)
2633 *err_code = EMAIL_ERROR_NONE;
2635 if (!g_srv_thread) {
2636 if (err_code != NULL)
2637 *err_code = EMAIL_ERROR_UNKNOWN;
2641 /* stop event_data loop */
2644 /* pthread_kill(g_srv_thread, SIGINT); */
2645 emcore_cancel_thread(g_active_que, NULL, err_code);
2647 ENTER_CRITICAL_SECTION(_event_available_lock);
2648 WAKE_CONDITION_VARIABLE(_event_available_signal);
2649 LEAVE_CRITICAL_SECTION(_event_available_lock);
2651 /* wait for thread finished */
2652 THREAD_JOIN(g_srv_thread);
2656 DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2657 DELETE_CRITICAL_SECTION(_event_available_lock);
2658 DELETE_CONDITION_VARIABLE(_event_available_signal);
2659 DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2661 g_event_que_idx = 1;
2664 if (err_code != NULL)
2665 *err_code = EMAIL_ERROR_NONE;
2666 EM_DEBUG_FUNC_END();
2671 int emcore_get_active_queue_idx()
2673 return g_send_active_que;
2676 /* check thread status
2677 * 0 : stop job 1 : continue job
2679 INTERNAL_FUNC int emcore_check_thread_status()
2681 if (g_active_que <= 0)
2684 return (g_event_que[g_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2688 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
2690 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2693 int err = EMAIL_ERROR_NONE;
2695 if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1)) {
2696 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2697 err = EMAIL_ERROR_INVALID_PARAM;
2701 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2703 EM_DEBUG_LOG("status[%d], type[%d], handle[%d]", g_event_que[handle].status, g_event_que[handle].type, handle);
2705 if (g_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT) {
2706 fail_status_notify(&g_event_que[handle], EMAIL_ERROR_CANCELLED);
2708 switch (g_event_que[handle].type) {
2709 case EMAIL_EVENT_SEND_MAIL:
2710 case EMAIL_EVENT_SEND_MAIL_SAVED:
2711 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
2712 _sending_busy_unref();
2713 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2714 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2716 case EMAIL_EVENT_DOWNLOAD_BODY:
2717 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
2718 _receiving_busy_unref();
2719 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2720 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2723 case EMAIL_EVENT_SYNC_HEADER:
2724 case EMAIL_EVENT_SYNC_HEADER_OMA:
2725 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2726 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2727 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2728 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
2729 _receiving_busy_unref();
2732 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2733 EM_DEBUG_LOG(" validate account waiting : cancel acc id : %d", g_event_que[handle].account_id);
2734 _receiving_busy_unref();
2735 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2736 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
2739 case EMAIL_EVENT_DELETE_MAIL:
2740 case EMAIL_EVENT_DELETE_MAIL_ALL:
2741 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2742 case EMAIL_EVENT_SAVE_MAIL:
2743 case EMAIL_EVENT_MOVE_MAIL:
2744 case EMAIL_EVENT_CREATE_MAILBOX:
2745 case EMAIL_EVENT_DELETE_MAILBOX:
2746 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2747 case EMAIL_EVENT_SEARCH_ON_SERVER:
2748 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2749 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
2750 _receiving_busy_unref();
2757 memset(g_event_que+handle, 0x00, sizeof(email_event_t));
2758 g_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2760 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2765 if (err_code != NULL)
2767 EM_DEBUG_FUNC_END();
2771 /* check thread status
2772 * 0 : stop job 1 : continue job
2774 int emcore_check_send_mail_thread_status()
2776 EM_DEBUG_FUNC_BEGIN();
2778 if (g_send_active_que <= 0)
2780 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);
2781 EM_DEBUG_FUNC_END();
2782 return (g_send_event_que[g_send_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2785 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
2787 EM_DEBUG_FUNC_BEGIN();
2788 int error_code = EMAIL_ERROR_NONE;
2789 int i, event_count = EVENT_QUEUE_MAX, exit_flag = 0, sleep_count = 0;
2791 for (i = 0 ; i < event_count; i++) {
2792 if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2793 EM_DEBUG_LOG("There is a live thread. %d", i);
2794 if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2795 EM_DEBUG_LOG("And it is for account %d", g_event_que[i].account_id);
2796 emcore_cancel_thread(i, NULL, &error_code);
2801 while (exit_flag == 0 && sleep_count < 30) {
2802 EM_DEBUG_LOG("Sleeping...");
2804 EM_DEBUG_LOG("Wake up!");
2807 for (i = 0 ; i < event_count; i++) {
2808 if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2809 EM_DEBUG_LOG("There is still a live thread. %d", i);
2810 if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2811 EM_DEBUG_LOG("And it is for account %d. So, I should sleep for a while.", g_event_que[i].account_id);
2818 EM_DEBUG_LOG("Sleep count %d", sleep_count);
2820 if (sleep_count >= 30)
2821 error_code = EMAIL_ERROR_CANNOT_STOP_THREAD;
2823 error_code = EMAIL_ERROR_NONE;
2824 EM_DEBUG_FUNC_END();
2829 /* cancel send mail job */
2830 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
2832 EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2835 int err = EMAIL_ERROR_NONE;
2837 if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1)) {
2838 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2839 err = EMAIL_ERROR_INVALID_PARAM;
2843 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2845 EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2847 if (g_send_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT) {
2848 fail_status_notify(&g_send_event_que[handle], EMAIL_ERROR_CANCELLED);
2850 switch (g_send_event_que[handle].type) {
2851 case EMAIL_EVENT_SEND_MAIL:
2852 case EMAIL_EVENT_SEND_MAIL_SAVED:
2853 _sending_busy_unref();
2854 g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2855 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_send_event_que[handle].account_id, NULL , g_send_event_que[handle].event_param_data_4, err))
2856 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2863 EM_DEBUG_LOG("send_mail_cancel");
2864 memset(g_send_event_que+handle, 0x00, sizeof(email_event_t));
2865 g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2867 EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2870 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2875 if (err_code != NULL)
2877 EM_DEBUG_FUNC_END("ret [%d]", ret);
2882 INTERNAL_FUNC int emcore_get_receiving_event_queue(email_event_t **event_queue, int *event_active_queue, int *err)
2884 if (event_queue == NULL || event_active_queue == NULL) {
2885 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM event_queue[%p] event_active_queue[%p]", event_queue, event_active_queue);
2888 *err = EMAIL_ERROR_INVALID_PARAM;
2893 *event_queue = g_event_que;
2894 *event_active_queue = g_active_que;
2899 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
2901 EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
2904 EM_SAFE_FREE(event_data->event_param_data_1);
2905 EM_SAFE_FREE(event_data->event_param_data_2);
2906 EM_SAFE_FREE(event_data->event_param_data_3);
2909 EM_DEBUG_FUNC_END();
2913 #ifdef __FEATURE_KEEP_CONNECTION__
2914 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
2916 return (unsigned int)g_srv_thread;
2918 #endif /* __FEATURE_KEEP_CONNECTION__ */
2920 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2922 INTERNAL_FUNC int emcore_get_pbd_thd_state()
2924 int pbd_thd_state = false;
2925 ENTER_CRITICAL_SECTION(_state_variables_lock);
2926 pbd_thd_state = g_pbd_thd_state;
2927 LEAVE_CRITICAL_SECTION(_state_variables_lock);
2928 return pbd_thd_state;
2931 static int emcore_set_pbd_thd_state(int flag)
2933 ENTER_CRITICAL_SECTION(_state_variables_lock);
2934 g_pbd_thd_state = flag;
2935 LEAVE_CRITICAL_SECTION(_state_variables_lock);
2937 return g_pbd_thd_state;
2940 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
2942 EM_DEBUG_FUNC_BEGIN();
2943 EM_DEBUG_FUNC_END();
2944 return (unsigned int)g_partial_body_thd;
2947 static int emcore_clear_bulk_pbd_que(int *err_code)
2949 EM_DEBUG_FUNC_BEGIN();
2952 int error = EMAIL_ERROR_NONE;
2955 for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
2956 if (g_partial_body_bulk_dwd_que[i].event_type) {
2957 if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error)) {
2958 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2965 if (NULL != err_code)
2967 EM_DEBUG_FUNC_END();
2971 static void emcore_pb_thd_set_local_activity_continue(int flag)
2973 EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
2975 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2977 g_pb_thd_local_activity_continue = flag;
2980 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2983 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2984 EM_DEBUG_FUNC_END();
2988 int emcore_pb_thd_can_local_activity_continue()
2990 EM_DEBUG_FUNC_BEGIN();
2994 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2996 ret = g_pb_thd_local_activity_continue;
2998 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2999 EM_DEBUG_FUNC_END();
3004 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
3006 EM_DEBUG_FUNC_BEGIN();
3009 int error = EMAIL_ERROR_NONE;
3012 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3014 if (true == g_partial_body_thd_queue_empty) {
3015 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
3018 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
3019 if (g_partial_body_thd_event_que[i].event_type) {
3020 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error)) {
3021 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
3028 g_partial_body_thd_queue_empty = true;
3029 g_partial_body_thd_queue_full = false;
3031 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3033 if (NULL != err_code)
3035 EM_DEBUG_FUNC_END();
3039 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
3041 EM_DEBUG_FUNC_BEGIN();
3045 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3047 ret = g_partial_body_thd_queue_empty;
3049 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3050 EM_DEBUG_FUNC_END("ret [%d]", ret);
3053 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
3055 EM_DEBUG_FUNC_BEGIN();
3059 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3061 ret = g_partial_body_thd_queue_full;
3063 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3064 EM_DEBUG_FUNC_END();
3069 Himanshu[h.gahalut] : If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
3070 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
3071 through _partial_body_thd_event_queue_lock mutex.
3073 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
3077 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3078 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3082 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
3084 EM_DEBUG_FUNC_BEGIN();
3085 int error = EMAIL_ERROR_NONE;
3088 if (NULL == src || NULL == dest) {
3089 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
3090 error = EMAIL_ERROR_INVALID_PARAM;
3094 dest->account_id = src->account_id;
3095 dest->mail_id = src->mail_id;
3096 dest->server_mail_id = src->server_mail_id;
3097 dest->activity_id = src->activity_id;
3098 dest->mailbox_id = src->mailbox_id;
3099 dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
3100 dest->activity_type = src->activity_type;
3101 dest->event_type = src->event_type;
3103 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);
3109 if (NULL != error_code)
3110 *error_code = error;
3117 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,
3118 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
3120 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
3124 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3125 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3129 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3131 EM_DEBUG_FUNC_BEGIN();
3133 if (NULL == partial_body_thd_event) {
3134 *error_code = EMAIL_ERROR_INVALID_PARAM;
3138 email_event_partial_body_thd *pbd_event = partial_body_thd_event;
3140 /*Free character pointers in event_data cell */
3141 EM_SAFE_FREE(pbd_event->mailbox_name);
3142 memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
3143 EM_DEBUG_FUNC_END();
3147 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3149 EM_DEBUG_FUNC_BEGIN();
3151 if (NULL == partial_body_thd_event) {
3152 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
3154 if (error_code != NULL) {
3155 *error_code = EMAIL_ERROR_INVALID_PARAM;
3161 int error = EMAIL_ERROR_NONE;
3162 int empty_cell_index = -1;
3166 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3168 /* find a cell in queue which is empty */
3170 for (count = 0, index = g_partial_body_thd_next_event_idx; count < TOTAL_PARTIAL_BODY_EVENTS;) {
3171 if (g_partial_body_thd_event_que[index].event_type) {
3175 if (index == TOTAL_PARTIAL_BODY_EVENTS) {
3180 /*Found empty Cell*/
3182 empty_cell_index = index;
3187 if (-1 != empty_cell_index) {
3188 if (false == emcore_copy_partial_body_thd_event(partial_body_thd_event, g_partial_body_thd_event_que+empty_cell_index , &error)) {
3189 EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
3192 g_partial_body_thd_queue_empty = false;
3194 if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
3195 /*This is the last event_data inserted in queue after its insertion, queue is full */
3196 g_partial_body_thd_queue_full = true;
3200 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
3206 EM_DEBUG_LOG(" partial body thread event_data queue is full ");
3207 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
3209 g_partial_body_thd_queue_full = true;
3210 g_partial_body_thd_queue_empty = false;
3214 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3216 if (NULL != error_code) {
3217 *error_code = error;
3224 /* h.gahlaut : Return true only if event_data is retrieved successfully */
3226 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3228 EM_DEBUG_FUNC_BEGIN();
3231 int error = EMAIL_ERROR_NONE;
3234 /* Lock Mutex to protect event_data queue and associated global variables variables*/
3236 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3238 index = g_partial_body_thd_next_event_idx;
3240 if (0 == g_partial_body_thd_event_que[index].event_type) {
3241 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
3242 g_partial_body_thd_queue_empty = true;
3243 g_partial_body_thd_queue_full = false;
3246 /*Copy the event_data from queue to return it and free the event_data in queue */
3247 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
3248 EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
3250 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
3251 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
3254 g_partial_body_thd_queue_full = false;
3255 g_partial_body_thd_next_event_idx = ++index;
3257 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
3258 g_partial_body_thd_next_event_idx = 0;
3260 /* If the event_data retrieved was the only event_data present in queue,
3261 we need to set g_partial_body_thd_queue_empty to true
3264 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
3265 g_partial_body_thd_queue_empty = true;
3275 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3278 *error_code = error;
3284 gpointer partial_body_download_thread(gpointer data)
3286 EM_DEBUG_FUNC_BEGIN();
3288 int err = EMAIL_ERROR_NONE;
3289 email_session_t *session = NULL;
3290 email_event_partial_body_thd partial_body_thd_event;
3292 EM_DEBUG_LOG(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());
3294 /* Open connection with DB */
3296 if (false == emstorage_open(&err)) {
3297 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
3301 /* Start the continuous loop */
3303 while (g_partial_body_thd_loop) {
3304 /* Get an empty session */
3305 /* TODO : Mutex should be used in session APIs */
3307 if (false == emcore_get_empty_session(&session))
3308 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
3309 else { /* Get and Event from the Partial Body thread Event Queue */
3310 memset(&partial_body_thd_event, 0x00, sizeof(email_event_partial_body_thd));
3312 if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
3313 if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
3314 EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
3316 EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
3318 /* Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count */
3319 if (!g_partial_body_bulk_dwd_queue_empty) {
3320 partial_body_thd_event.event_type = 0;
3321 partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
3322 partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
3323 partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name);
3325 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3326 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3328 emcore_pb_thd_set_local_activity_continue(true);
3331 if (true == emcore_pb_thd_can_local_activity_continue()) {
3332 /*Check for local Activities */
3333 int is_local_activity_event_inserted = false;
3335 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &err)) {
3336 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
3339 if (true == is_local_activity_event_inserted) {
3340 emcore_pb_thd_set_local_activity_continue(false);
3342 emcore_clear_session(session);
3348 EM_DEBUG_LOG(" Partial Body Thread is going to sleep");
3350 emcore_set_pbd_thd_state(false);
3352 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3353 SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
3354 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3356 EM_DEBUG_LOG(" Partial Body Thread wakes up ");
3358 emcore_set_pbd_thd_state(true);
3363 EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
3365 /* Since all events are network operations dnet init and sleep control is
3366 done before entering switch block*/
3368 emdevice_set_dimming_on_off(false, NULL);
3370 if (!emnetwork_check_network_status( &err)) {
3371 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
3374 /* Process events */
3375 EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
3377 switch (partial_body_thd_event.event_type) {
3378 case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD: {
3379 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
3380 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3384 case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD: {
3385 partial_body_thd_event.event_type = 0;
3387 /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
3388 * g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
3389 if (false == emcore_is_partial_body_thd_que_empty())
3391 if (!g_partial_body_bulk_dwd_queue_empty)
3394 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3395 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
3399 EM_DEBUG_EXCEPTION(" Warning : Default case entered. This should not happen ");
3404 if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
3405 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
3407 emdevice_set_dimming_on_off(true, NULL);
3410 emcore_clear_session(session);
3414 /* If something is added to end thread in future for any case then if thread is holding any resources
3415 define a function emcore_partial_body_thd_loop_stop to release resources and call it
3416 here to end thread */
3420 INTERNAL_FUNC int emcore_start_thread_for_downloading_partial_body(int *err_code)
3422 EM_DEBUG_FUNC_BEGIN();
3424 int i = 0, thread_error = -1;
3426 /* Clear Partial Body Event Queue*/
3427 memset(&g_partial_body_thd_event_que, 0x00, sizeof(g_partial_body_thd_event_que));
3429 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
3430 g_partial_body_thd_event_que[i].mailbox_name = NULL;
3431 g_partial_body_thd_event_que[i].mailbox_id = 0;
3434 if (g_partial_body_thd) {
3435 EM_DEBUG_EXCEPTION("partial body thread is already running...");
3436 if (err_code != NULL)
3437 *err_code = EMAIL_ERROR_UNKNOWN;
3442 g_partial_body_thd_next_event_idx = 0;
3443 g_partial_body_thd_loop = 1;
3444 g_partial_body_thd_queue_empty = true;
3445 g_partial_body_thd_queue_full = false;
3447 INITIALIZE_CONDITION_VARIABLE(_partial_body_thd_cond);
3450 /* THREAD_CREATE_JOINABLE(g_partial_body_thd, partial_body_download_thread, thread_error); */
3451 THREAD_CREATE(g_partial_body_thd, partial_body_download_thread, NULL, thread_error);
3453 if (thread_error != 0) {
3454 EM_DEBUG_EXCEPTION("cannot make thread...");
3455 if (err_code != NULL)
3456 *err_code = EMAIL_ERROR_UNKNOWN;
3460 if (err_code != NULL)
3461 *err_code = EMAIL_ERROR_NONE;
3467 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
3468 static int emcore_partial_body_bulk_flush(int *error_code)
3470 EM_DEBUG_FUNC_BEGIN();
3471 int error = EMAIL_ERROR_NONE;
3473 MAILSTREAM *stream = NULL;
3474 void *tmp_stream = NULL;
3476 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)) {
3477 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3480 stream = (MAILSTREAM *)tmp_stream;
3482 /* Call bulk download here */
3483 if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
3484 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3491 emcore_close_mailbox(0, stream);
3494 g_partial_body_bulk_dwd_next_event_idx = 0;
3495 g_partial_body_bulk_dwd_queue_empty = true;
3497 if (false == emcore_clear_bulk_pbd_que(&error))
3498 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
3500 if (NULL != error_code)
3501 *error_code = error;
3503 EM_DEBUG_FUNC_END("ret [%d]", ret);
3508 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
3509 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
3511 EM_DEBUG_FUNC_BEGIN();
3512 int error = EMAIL_ERROR_NONE;
3513 int num_activity = 0;
3517 MAILSTREAM *stream = NULL;
3518 void *tmp_stream = NULL;
3519 email_event_partial_body_thd *activity_data_list = NULL;
3520 int *mailbox_list = NULL;
3522 if (NULL == pbd_event)
3524 EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
3526 error = EMAIL_ERROR_INVALID_PARAM;
3530 /*Check if the event_data is to flush the event_data que array */
3531 if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
3532 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
3533 /*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
3534 then append this event_data also to download que array */
3535 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
3536 (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
3537 EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
3538 EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
3539 if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
3540 if (false == emcore_partial_body_bulk_flush(&error)) {
3541 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3547 EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
3548 EM_DEBUG_LOG("Flush the current que if not empty");
3549 EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
3550 if (!g_partial_body_bulk_dwd_queue_empty) {
3551 if (false == emcore_partial_body_bulk_flush(&error)) {
3552 EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3557 /*Add the event_data to the download que array */
3558 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))
3559 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
3561 g_partial_body_bulk_dwd_queue_empty = false;
3562 g_partial_body_bulk_dwd_next_event_idx++;
3563 EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
3566 else if (pbd_event->activity_type) {
3567 int *account_list = NULL;
3568 int account_count = 0;
3570 EM_DEBUG_LOG("Event is coming from local activity.");
3571 /* Get all the accounts for which local activities are pending */
3572 if (false == emstorage_get_pbd_account_list(&account_list, &account_count, false, &error)) {
3573 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3574 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3578 for (m = 0; m < account_count; ++m) {
3579 /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
3580 if (false == emstorage_get_pbd_mailbox_list(account_list[m], &mailbox_list, &count, false, &error)) {
3581 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3582 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3586 for (i = 0; i < count; i++) {
3588 int activity_count = 0;
3590 if (!emcore_connect_to_remote_mailbox(account_list[m], mailbox_list[i], (void **)&tmp_stream, &error)) {
3591 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3596 stream = (MAILSTREAM *)tmp_stream;
3598 if (false == emstorage_get_mailbox_pbd_activity_count(account_list[m], mailbox_list[i], &activity_count, false, &error)) {
3599 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
3603 if (activity_count > 0) {
3604 int temp_error = EMAIL_ERROR_NONE;
3608 int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3611 if (false == emstorage_get_pbd_activity_data(account_list[j], mailbox_list[i], &activity_data_list, &num_activity, false, &error))
3612 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
3614 if (NULL == activity_data_list) {
3615 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
3619 remainder = num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3620 iter = num_activity/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1 : 0);
3622 for (j = 0; j < iter; j++) {
3623 if ((iter == (j+1)) && (remainder != 0))
3626 /*Call bulk download here */
3627 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
3628 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3629 temp_error = EMAIL_ERROR_NO_RESPONSE;
3632 for (k = 0; k < num; k++) {
3633 if (activity_data_list[index + k].activity_type)
3634 emcore_free_partial_body_thd_event(activity_data_list + index + k, &error);
3640 if (false == emcore_is_partial_body_thd_que_empty()) {
3642 goto FINISH_OFF; /* Stop Local Activity Sync */
3644 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
3645 temp_error = EMAIL_ERROR_NONE;
3651 emcore_close_mailbox(0, stream);
3656 /* After completing one cycle of local activity sync ,
3657 local activity continue variable should be set to false. */
3659 emcore_pb_thd_set_local_activity_continue(false);
3661 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 */ {
3662 /*Check if events have arrived in g_partial_body_thd_event_que */
3663 if (false == emcore_is_partial_body_thd_que_empty()) {
3664 EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
3666 goto FINISH_OFF; /* Stop Local Activity Sync */
3668 if (false == emcore_partial_body_bulk_flush(&error)) {
3669 EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3678 EM_SAFE_FREE(mailbox_list);
3680 if (activity_data_list) {
3681 for (i = 0; i < num_activity; i++) {
3682 if (activity_data_list[i].activity_type)
3683 emcore_free_partial_body_thd_event(activity_data_list + i, &error);
3689 emcore_close_mailbox(0, stream);
3692 if (NULL != error_code)
3693 *error_code = error;
3694 EM_DEBUG_FUNC_END("ret [%d]", ret);
3698 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */