699443cf55ec42ed0e3ca49fd71b2b2f9d6c630c
[platform/core/messaging/email-service.git] / email-core / email-core-event.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22 #include <stdio.h>
23 #include <glib.h>
24 #include <malloc.h>
25 #include <pthread.h>
26 #include <vconf.h>
27 #include <signal.h>
28 #include <contacts.h>
29 #include <contacts_internal.h>
30 #include "c-client.h"
31 #include "email-convert.h"
32 #include "email-storage.h"
33 #include "email-network.h"
34 #include "email-device.h"
35 #include "email-utilities.h"
36 #include "email-daemon-auto-poll.h"
37 #include "email-core-global.h"
38 #include "email-core-account.h"
39 #include "email-core-event.h"
40 #include "email-core-utils.h"
41 #include "email-core-mailbox.h"
42 #include "email-core-imap-mailbox.h"
43 #include "email-core-mail.h"
44 #include "email-core-mailbox-sync.h"
45 #include "email-core-smtp.h"
46 #include "email-core-utils.h"
47 #include "email-core-signal.h"
48 #include "email-debug-log.h"
49
50
51 /*-----------------------------------------------------------------------------
52  * Receving Event Queue
53  *---------------------------------------------------------------------------*/
54 INTERNAL_FUNC thread_t g_srv_thread;
55 INTERNAL_FUNC pthread_cond_t  _event_available_signal = PTHREAD_COND_INITIALIZER;
56 INTERNAL_FUNC pthread_mutex_t *_event_queue_lock = NULL;
57 INTERNAL_FUNC pthread_mutex_t *_event_handle_map_lock = NULL;
58
59 INTERNAL_FUNC GQueue *g_event_que = NULL;
60 INTERNAL_FUNC int g_event_loop = 1;
61 INTERNAL_FUNC int handle_map[EVENT_QUEUE_MAX] = {0,};
62 INTERNAL_FUNC int recv_thread_run = 0;
63
64 static void fail_status_notify(email_event_t *event_data, int error);
65 static int emcore_get_new_handle(void);
66 static void emcore_adjust_sync_mail_flag_event_priority();
67
68
69 /*-----------------------------------------------------------------------------
70  * Sending Event Queue
71  *---------------------------------------------------------------------------*/
72 INTERNAL_FUNC thread_t g_send_srv_thread;
73 INTERNAL_FUNC pthread_cond_t  _send_event_available_signal = PTHREAD_COND_INITIALIZER;
74 INTERNAL_FUNC pthread_mutex_t *_send_event_queue_lock = NULL;
75 INTERNAL_FUNC pthread_mutex_t *_send_event_handle_map_lock = NULL;
76
77 INTERNAL_FUNC GQueue *g_send_event_que = NULL;
78 INTERNAL_FUNC int g_send_event_loop = 1;
79 INTERNAL_FUNC int send_handle_map[EVENT_QUEUE_MAX] = {0,};
80 INTERNAL_FUNC int send_thread_run = 0;
81
82 static int emcore_get_new_send_handle(void);
83
84 /*-----------------------------------------------------------------------------
85  * Partial Body Event Queue
86  *---------------------------------------------------------------------------*/
87
88 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
89
90 INTERNAL_FUNC thread_t g_partial_body_thd ;                                                             /* Determines if thread is created or not. Non Null means thread is created */
91 INTERNAL_FUNC pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER;   /* Mutex to protect event queue */
92 INTERNAL_FUNC pthread_cond_t  _partial_body_thd_cond = PTHREAD_COND_INITIALIZER;                                /* Condition variable on which partial body thread is waiting  */
93 INTERNAL_FUNC pthread_mutex_t _state_variables_lock;
94
95 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
96 INTERNAL_FUNC int g_partial_body_thd_next_event_idx = 0;                        /* Index of Next Event to be processed in the queue*/
97 //INTERNAL_FUNC int g_partial_body_thd_loop = 1;                                                /* Variable to make a continuos while loop */
98 INTERNAL_FUNC int g_partial_body_thd_queue_empty = true;                        /* Variable to determine if event queue is empty.True means empty*/
99 INTERNAL_FUNC int g_partial_body_thd_queue_full = false;                        /* Variable to determine if event queue is full. True means full*/
100 INTERNAL_FUNC int g_pb_thd_local_activity_continue = true;                      /* Variable to control local activity sync */
101 INTERNAL_FUNC int g_pbd_thd_state = false;                                                              /* false :  thread is sleeping , true :  thread is working */
102
103 INTERNAL_FUNC email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
104 static int g_partial_body_bulk_dwd_next_event_idx = 0;          /* Index of Next Event to be processed in the queue*/
105 INTERNAL_FUNC int g_partial_body_bulk_dwd_queue_empty = true;
106
107 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
108 static int emcore_clear_bulk_pbd_que(int *err_code);
109
110 INTERNAL_FUNC email_event_t *sync_failed_event_data = NULL;
111
112 #endif
113
114 /*-----------------------------------------------------------------------------
115  * Unused
116  *---------------------------------------------------------------------------*/
117 typedef struct EVENT_CALLBACK_ELEM {
118         email_event_callback callback;
119         void *event_data;
120         struct EVENT_CALLBACK_ELEM *next;
121 } EVENT_CALLBACK_LIST;
122
123 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM];            /*  array of singly-linked list for event callbacks */
124 INTERNAL_FUNC pthread_mutex_t *_event_callback_table_lock = NULL;
125
126 #ifdef __FEATURE_LOCAL_ACTIVITY__
127 INTERNAL_FUNC int g_local_activity_run = 0;
128 INTERNAL_FUNC int g_save_local_activity_run = 0;
129 static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error);
130 #endif
131
132 #ifdef ENABLE_IMAP_IDLE_THREAD
133 extern int g_imap_idle_thread_alive;
134 extern int imap_idle_thread;
135 #endif /* ENABLE_IMAP_IDLE_THREAD */
136
137 static int is_gdk_lock_needed()
138 {
139         if (g_event_loop)  {
140                 return (THREAD_SELF() == g_srv_thread);
141         }
142         return false;
143 }
144
145 static void fail_status_notify(email_event_t *event_data, int error)
146 {
147         EM_DEBUG_FUNC_BEGIN();
148         int account_id, mail_id;
149
150         if (!event_data) {
151                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
152                 return;
153         }
154         account_id = event_data->account_id;
155         mail_id  = event_data->event_param_data_4;
156
157         EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
158
159         switch (event_data->type)  {
160                 case EMAIL_EVENT_SEND_MAIL:
161                         /* case EMAIL_EVENT_SEND_MAIL_SAVED:  */
162                         /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
163                         emcore_show_user_message(event_data->multi_user_name, mail_id, EMAIL_ACTION_SEND_MAIL, error);
164                         break;
165
166                 case EMAIL_EVENT_SYNC_HEADER:
167                         emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
168                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SYNC_HEADER, error);
169                         break;
170
171                 case EMAIL_EVENT_DOWNLOAD_BODY:
172                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
173                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
174                         break;
175
176                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
177                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
178                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
179                         break;
180
181                 case EMAIL_EVENT_DELETE_MAIL:
182                 case EMAIL_EVENT_DELETE_MAIL_ALL:
183                         emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
184                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAIL, error);
185                         break;
186
187                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
188                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
189                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
190                         break;
191
192                 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
193                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
194                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
195                         break;
196
197                 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
198                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
199                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
200                         break;
201
202                 case EMAIL_EVENT_CREATE_MAILBOX:
203                         emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
204                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
205                         break;
206
207                 case EMAIL_EVENT_DELETE_MAILBOX:
208                         emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
209                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
210                         break;
211
212                 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
213                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
214                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
215                         break;
216
217                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
218                         emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
219                         break;
220
221                 case EMAIL_EVENT_SEARCH_ON_SERVER:
222                         emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
223                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
224                         break;
225
226                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
227                         emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
228                         emcore_show_user_message(event_data->multi_user_name, account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
229                         break;
230
231                 case EMAIL_EVENT_UPDATE_MAIL:
232                         emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
233                         break;
234
235                 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
236                         emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL, account_id, event_data->event_param_data_4, -1, EMAIL_ERROR_NONE);
237                         break;
238
239                 default:
240                         break;
241         }
242         EM_DEBUG_FUNC_END();
243 }
244
245
246 INTERNAL_FUNC void emcore_initialize_event_callback_table()
247 {
248         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
249
250         int i;
251
252         for (i = 0; i < EMAIL_ACTION_NUM; i++)
253                 _event_callback_table[i] = NULL;
254
255         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
256 }
257
258 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
259 {
260         EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
261
262         if (callback == NULL)
263                 return false;
264
265         int ret = false;
266
267         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
268
269         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
270
271         while (node != NULL) {
272                 if (node->callback == callback && node->event_data == event_data)               /*  already registered */
273                         goto EXIT;
274
275                 node = node->next;
276         }
277
278         /*  not found, so keep going */
279
280         node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
281
282         if (node == NULL)               /*  not enough memory */
283                 goto EXIT;
284
285         node->callback = callback;
286         node->event_data = event_data;
287         node->next = _event_callback_table[action];
288
289         _event_callback_table[action] = node;
290
291         ret = true;
292
293 EXIT:
294
295         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
296         EM_DEBUG_FUNC_END();
297         return ret;
298 }
299
300 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
301 {
302         EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
303
304         if (callback == NULL)
305                 return false;
306
307         int ret = false;
308
309         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
310
311         EVENT_CALLBACK_LIST *prev = NULL;
312         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
313
314         while (node != NULL) {
315                 if (node->callback == callback) {
316                         if (prev != NULL)
317                                 prev->next = node->next;
318                         else
319                                 _event_callback_table[action] = node->next;
320
321                         EM_SAFE_FREE(node);
322
323                         ret = true;
324                         break;
325                 }
326
327                 prev = node;
328                 node = node->next;
329         }
330
331         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
332         EM_DEBUG_FUNC_END();
333         return ret;
334 }
335
336 void emcore_execute_event_callback(email_action_t action, int total, int done, int status, int account_id, int mail_id, int handle, int error)
337 {
338         EM_DEBUG_FUNC_BEGIN("action[%d], total[%d], done[%d], status[%d], account_id[%d], mail_id[%d], handle[%d], error[%d]", action, total, done, status, account_id, mail_id, handle, error);
339
340         int lock_needed = 0;
341         lock_needed = is_gdk_lock_needed();
342
343         if (lock_needed)  {
344                 /*  Todo  :  g_thread_yield */
345         }
346
347         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
348
349         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
350
351         while (node != NULL)  {
352                 if (node->callback != NULL)
353                         node->callback(total, done, status, account_id, mail_id, handle, node->event_data, error);
354                 node = node->next;
355         }
356
357         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
358
359         if (lock_needed)  {
360         }
361         EM_DEBUG_FUNC_END();
362 }
363
364 /* insert a event to event queue */
365 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
366 {
367         EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
368
369         if (!event_data) {
370                 EM_DEBUG_EXCEPTION("Invalid Parameter");
371                 if (err_code != NULL)
372                         *err_code = EMAIL_ERROR_INVALID_PARAM;
373                 return false;
374         }
375
376         if (!g_srv_thread) {
377                 EM_DEBUG_EXCEPTION("email-service is not ready");
378                 if (err_code != NULL)
379                         *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
380                 return false;
381         }
382
383         int ret = false;
384         int error = EMAIL_ERROR_NONE;
385         int q_length = 0;
386         int new_handle = 0;
387         char *multi_user_name = NULL;
388
389         multi_user_name = EM_SAFE_STRDUP(event_data->multi_user_name);
390
391         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
392
393         if (g_event_que)
394                 q_length = g_queue_get_length(g_event_que);
395
396         EM_DEBUG_LOG("Q Length : [%d]", q_length);
397         EM_DEBUG_LOG("Event type: [%d]", event_data->type);
398
399         if (q_length > EVENT_QUEUE_MAX) {
400                 EM_DEBUG_EXCEPTION("event que is full...");
401                 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
402                 ret = false;
403         } else {
404                 new_handle = emcore_get_new_handle();
405
406                 if (new_handle) {
407                         event_data->status = EMAIL_EVENT_STATUS_WAIT;
408                         event_data->handle = new_handle;
409                         g_queue_push_tail(g_event_que, event_data);
410                         emcore_adjust_sync_mail_flag_event_priority();
411                         WAKE_CONDITION_VARIABLE(_event_available_signal);
412                         ret = true;
413                 } else {
414                         EM_DEBUG_EXCEPTION("event queue is full...");
415                         error = EMAIL_ERROR_EVENT_QUEUE_FULL;
416                         ret = false;
417                 }
418
419         }
420
421         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
422
423         if (handle)
424                 *handle = new_handle;
425
426         switch (event_data->type)  {
427                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
428                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
429                 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
430                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
431                 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
432                 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
433                 case EMAIL_EVENT_SAVE_MAIL:
434                 case EMAIL_EVENT_MOVE_MAIL:
435                 case EMAIL_EVENT_DELETE_MAIL:
436                 case EMAIL_EVENT_DELETE_MAIL_ALL:
437                 case EMAIL_EVENT_CREATE_MAILBOX:
438                 case EMAIL_EVENT_DELETE_MAILBOX:
439                 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
440                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
441                 case EMAIL_EVENT_UPDATE_MAIL:
442                 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
443                 case EMAIL_EVENT_SEARCH_ON_SERVER:
444                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
445                         break;
446
447                 default:
448 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
449                 {
450                         int is_local_activity_event_inserted = false;
451                         emcore_partial_body_thd_local_activity_sync(multi_user_name,
452                                                                                                                  &is_local_activity_event_inserted,
453                                                                                                                  &error);
454                         if (error != EMAIL_ERROR_NONE) {
455                                 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
456                         } else {
457                                 if (true == is_local_activity_event_inserted)
458                                         emcore_pb_thd_set_local_activity_continue(false);
459                         }
460                 }
461 #endif
462                 break;
463         }
464
465         EM_SAFE_FREE(multi_user_name);
466
467         if (err_code) {
468                 EM_DEBUG_LOG("ERR [%d]", error);
469                 *err_code = error;
470         }
471
472         return ret;
473 }
474
475 /* get a event from event_data queue */
476 INTERNAL_FUNC int emcore_retrieve_event(email_event_t **event_data, int *err_code)
477 {
478         EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
479
480         int ret = false;
481         int error = EMAIL_ERROR_NONE;
482         int q_length = 0;
483         email_event_t *poped = NULL;
484         email_event_t *head_event = NULL;
485
486         if (g_event_que)
487                 q_length = g_queue_get_length(g_event_que);
488
489         EM_DEBUG_LOG("Q Length : [%d]", q_length);
490
491         if (!q_length) {
492                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
493                 EM_DEBUG_LOG("QUEUE is empty");
494                 goto FINISH_OFF;
495         }
496
497         while (1) {
498                 head_event = (email_event_t *)g_queue_peek_head(g_event_que);
499                 if (!head_event) {
500                         error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
501                         EM_DEBUG_LOG_DEV("QUEUE is empty");
502                         break;
503                 }
504                 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
505                         EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
506                         poped = g_queue_pop_head(g_event_que);
507                         if (poped) {
508                                 emcore_return_handle(poped->handle);
509                                 emcore_free_event(poped);
510                                 EM_SAFE_FREE(poped);
511                         } else {
512                                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
513                                 EM_DEBUG_LOG("QUEUE is empty");
514                                 break;
515                         }
516                 } else {
517                         head_event->status = EMAIL_EVENT_STATUS_STARTED;
518                         EM_DEBUG_LOG("Event retrieved: [%d]", head_event->type);
519                         *event_data = head_event;
520                         ret = true;
521                         break;
522                 }
523         }
524
525 FINISH_OFF:
526
527         if (err_code != NULL)
528                 *err_code = error;
529
530         EM_DEBUG_FUNC_END("ret [%d]", ret);
531         return ret;
532 }
533
534 /* get a event from event_data queue for download_attachment*/
535 INTERNAL_FUNC int emcore_retrieve_event_for_download_attachment(email_event_t **event_data, int *err_code)
536 {
537         EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
538
539         int ret = false;
540         int error = EMAIL_ERROR_NONE;
541         int q_length = 0;
542         email_event_t *head_event = NULL;
543
544         if (g_event_que)
545                 q_length = g_queue_get_length(g_event_que);
546
547         EM_DEBUG_LOG("Q Length : [%d]", q_length);
548
549         if (!q_length) {
550                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
551                 EM_DEBUG_LOG("QUEUE is empty");
552                 goto FINISH_OFF;
553         }
554
555         head_event = (email_event_t *)g_queue_peek_head(g_event_que);
556         if (!head_event) {
557                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
558                 EM_DEBUG_LOG_DEV("QUEUE is empty");
559
560         } else if (head_event->status != EMAIL_EVENT_STATUS_STARTED) {
561                 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
562                 error = EMAIL_ERROR_NO_MORE_DATA;
563                 EM_DEBUG_LOG("get event data err");
564
565         } else {
566                 *event_data = head_event;
567                 ret = true;
568
569         }
570
571
572 FINISH_OFF:
573
574         if (err_code != NULL)
575                 *err_code = error;
576
577         EM_DEBUG_FUNC_END("ret [%d]", ret);
578         return ret;
579 }
580
581
582
583
584 /* check that event_data loop is continuous */
585 INTERNAL_FUNC int emcore_event_loop_continue(void)
586 {
587         return g_event_loop;
588 }
589
590 INTERNAL_FUNC int emcore_is_event_queue_empty(void)
591 {
592         int q_length = 0;
593         int ret = false;
594
595         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
596         if (g_event_que)
597                 q_length = g_queue_get_length(g_event_que);
598         EM_DEBUG_LOG("Q Length : [%d]", q_length);
599         if (q_length > 0) {
600                 ret = false;
601         } else {
602                 EM_DEBUG_LOG("event que is empty");
603                 ret = true;
604         }
605         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
606
607         return ret;
608 }
609
610
611 INTERNAL_FUNC int emcore_is_send_event_queue_empty(void)
612 {
613         int q_length = 0;
614         int ret = false;
615
616         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
617         if (g_send_event_que)
618                 q_length = g_queue_get_length(g_send_event_que);
619         EM_DEBUG_LOG("Q Length : [%d]", q_length);
620         if (q_length > 0) {
621                 ret = false;
622         } else {
623                 EM_DEBUG_LOG("send event que is empty");
624                 ret = true;
625         }
626         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
627
628         return ret;
629 }
630
631
632 INTERNAL_FUNC void emcore_get_sync_fail_event_data(email_event_t **event_data)
633 {
634         if (!sync_failed_event_data) {
635                 EM_DEBUG_EXCEPTION("sync_failed_event_data is NULL");
636                 return;
637         }
638
639         email_event_t *new_event = NULL;
640
641         new_event = em_malloc(sizeof(email_event_t));
642         if (new_event == NULL) {
643                 EM_DEBUG_EXCEPTION("em_mallocfailed");
644                 return;
645         }
646
647         new_event->account_id = sync_failed_event_data->account_id;
648         new_event->type = sync_failed_event_data->type;
649         if (sync_failed_event_data->event_param_data_3)
650                 new_event->event_param_data_3 = EM_SAFE_STRDUP(sync_failed_event_data->event_param_data_3);
651         new_event->event_param_data_4 = sync_failed_event_data->event_param_data_4;
652         new_event->event_param_data_5 = sync_failed_event_data->event_param_data_5;
653         new_event->multi_user_name = EM_SAFE_STRDUP(sync_failed_event_data->multi_user_name);
654
655         *event_data = new_event;
656
657         emcore_free_event(sync_failed_event_data);
658         EM_SAFE_FREE(sync_failed_event_data);
659 }
660
661 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
662 {
663         EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
664
665         if (!event_data) {
666                 EM_DEBUG_EXCEPTION("Invalid Parameter");
667                 if (err_code != NULL)
668                         *err_code = EMAIL_ERROR_INVALID_PARAM;
669                 return false;
670         }
671
672         if (!g_send_srv_thread) {
673                 EM_DEBUG_EXCEPTION("email-service is not ready");
674                 if (err_code != NULL)
675                         *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
676                 return false;
677         }
678
679         int ret = false;
680         int error = EMAIL_ERROR_NONE;
681         int q_length = 0;
682         int new_handle = 0;
683
684         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
685
686         if (g_send_event_que)
687                 q_length = g_queue_get_length(g_send_event_que);
688
689         EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
690         EM_DEBUG_LOG("SEND Event type: [%d]", event_data->type);
691
692         if (q_length > EVENT_QUEUE_MAX) {
693                 EM_DEBUG_EXCEPTION("send event que is full...");
694                 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
695                 ret = false;
696         } else {
697                 new_handle = emcore_get_new_send_handle();
698
699                 if (new_handle) {
700                         event_data->status = EMAIL_EVENT_STATUS_WAIT;
701                         event_data->handle = new_handle;
702                         g_queue_push_tail(g_send_event_que, event_data);
703                         WAKE_CONDITION_VARIABLE(_send_event_available_signal);
704                         ret = true;
705                 }
706         }
707
708         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
709
710         if (handle)
711                 *handle = new_handle;
712
713         if (err_code != NULL)
714                 *err_code = error;
715
716         EM_DEBUG_LOG("Finish with [%d]", ret);
717         return ret;
718 }
719
720
721 INTERNAL_FUNC int emcore_retrieve_send_event(email_event_t **event_data, int *err_code)
722 {
723         EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
724
725         int ret = false;
726         int error = EMAIL_ERROR_NONE;
727         int q_length = 0;
728         email_event_t *poped = NULL;
729         email_event_t *head_event = NULL;
730
731         /* g_queue_get_length is aborted when param is null */
732         q_length = g_send_event_que ? g_queue_get_length(g_send_event_que) : 0; /*prevent 35141*/
733
734         EM_DEBUG_LOG("SEND Q Length : [%d]", q_length);
735
736         if (!q_length) {
737                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
738                 EM_DEBUG_LOG_DEV("SEND QUEUE is empty");
739                 goto FINISH_OFF;
740         }
741
742         while (1) {
743                 head_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
744                 if (!head_event) {
745                         error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
746                         EM_DEBUG_LOG("SEND QUEUE is empty");
747                         break;
748                 }
749                 if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
750                         EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
751                         poped = g_queue_pop_head(g_send_event_que);
752                         if (poped) {
753                                 emcore_return_send_handle(poped->handle);
754                                 emcore_free_event(poped);
755                                 EM_SAFE_FREE(poped);
756                         } else {
757                                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
758                                 EM_DEBUG_LOG("SEND QUEUE is empty");
759                                 break;
760                         }
761                 } else {
762                         head_event->status = EMAIL_EVENT_STATUS_STARTED;
763                         EM_DEBUG_LOG("SEND Event retrieved: [%d]", head_event->type);
764                         *event_data = head_event;
765                         ret = true;
766                         break;
767                 }
768         }
769
770 FINISH_OFF:
771
772         if (err_code != NULL)
773                 *err_code = error;
774
775         EM_DEBUG_FUNC_END("ret [%d]", ret);
776         return ret;
777 }
778
779 /* finish api event_data loop */
780 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
781 {
782     EM_DEBUG_FUNC_BEGIN();
783
784         if (err_code != NULL)
785                 *err_code = EMAIL_ERROR_NONE;
786
787         if (!g_send_srv_thread)          {
788                 if (err_code != NULL)
789                         *err_code = EMAIL_ERROR_UNKNOWN;
790                 return false;
791         }
792
793         /* stop event_data loop */
794         g_send_event_loop = 0;
795
796         emcore_cancel_all_send_mail_thread(err_code);
797
798         WAKE_CONDITION_VARIABLE(_send_event_available_signal);          /*  MUST BE HERE */
799
800         /* wait for thread finished */
801         THREAD_JOIN(g_send_srv_thread);
802
803         g_queue_free(g_send_event_que);
804
805         g_send_srv_thread = 0;
806
807         DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
808         DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
809         DELETE_CONDITION_VARIABLE(_send_event_available_signal);
810
811         if (err_code != NULL)
812                 *err_code = EMAIL_ERROR_NONE;
813
814         return true;
815 }
816
817 /* finish api event_data loop */
818 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
819 {
820         EM_DEBUG_FUNC_BEGIN();
821
822         if (err_code != NULL)
823                 *err_code = EMAIL_ERROR_NONE;
824
825         if (!g_srv_thread) {
826                 if (err_code != NULL)
827                         *err_code = EMAIL_ERROR_UNKNOWN;
828                 return false;
829         }
830
831         /* stop event_data loop */
832         g_event_loop = 0;
833
834         /* pthread_kill(g_srv_thread, SIGINT); */
835         emcore_cancel_all_thread(err_code);
836
837         WAKE_CONDITION_VARIABLE(_event_available_signal);
838
839         /* wait for thread finished */
840         THREAD_JOIN(g_srv_thread);
841
842         g_queue_free(g_event_que);
843
844         g_srv_thread = 0;
845
846         DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
847         DELETE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
848         DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
849         DELETE_CONDITION_VARIABLE(_event_available_signal);
850
851         if (err_code != NULL)
852                 *err_code = EMAIL_ERROR_NONE;
853         EM_DEBUG_FUNC_END();
854         return true;
855 }
856
857 /* check event thread status (worker_event_queue)
858 * 0 : stop job 1 : continue job
859 event list handled by event thread :
860 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
861 case EMAIL_EVENT_SYNC_HEADER:
862 case EMAIL_EVENT_SYNC_HEADER_OMA:
863 case EMAIL_EVENT_DOWNLOAD_BODY:
864 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
865 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
866 case EMAIL_EVENT_DELETE_MAIL:
867 case EMAIL_EVENT_DELETE_MAIL_ALL:
868 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
869 case EMAIL_EVENT_CREATE_MAILBOX:
870 case EMAIL_EVENT_DELETE_MAILBOX:
871 case EMAIL_EVENT_SAVE_MAIL:
872 case EMAIL_EVENT_MOVE_MAIL:
873 case EMAIL_EVENT_VALIDATE_ACCOUNT:
874 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
875 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
876 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
877 case EMAIL_EVENT_UPDATE_MAIL:
878 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
879 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
880 case EMAIL_EVENT_LOCAL_ACTIVITY:
881 case EMAIL_EVENT_SEARCH_ON_SERVER:
882 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
883 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
884 */
885 INTERNAL_FUNC int emcore_check_event_thread_status(int *event_type, int handle)
886 {
887         EM_DEBUG_FUNC_BEGIN();
888
889         int ret = false;
890         int q_length = 0;
891         email_event_t *active_event = NULL;
892
893         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
894
895         if (g_event_que)
896                 q_length = g_queue_get_length(g_event_que);
897
898         if (q_length) {
899                 active_event = (email_event_t *)g_queue_peek_head(g_event_que);
900                 if (active_event) {
901                         if (active_event->handle == handle) {
902                                 if (event_type)
903                                         *event_type = active_event->type;
904                                 if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
905                                         ret = true;
906                                 else
907                                         ret = false;
908                         } else {
909                                 ret = true;
910                         }
911                 }
912         } else {
913                 EM_DEBUG_LOG("Rcv Queue is empty [%d]", q_length);
914                 ret = true;
915         }
916
917         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
918         EM_DEBUG_FUNC_END("ret [%d]", ret);
919         return ret;
920 }
921
922 /* cancel a job  */
923 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
924 {
925         EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
926
927         int ret = false;
928         int err = EMAIL_ERROR_NONE;
929         int found = 0;
930         int i = 0;
931         int q_length = 0;
932         email_event_t *found_elm = NULL;
933         email_event_t *pop_elm = NULL;
934
935         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
936
937         if (g_event_que)
938                 q_length = g_queue_get_length(g_event_que);
939
940         for (i = 0; i < q_length; i++) {
941                 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
942                 if (found_elm && found_elm->handle == handle) {
943                         EM_DEBUG_LOG("Found Queue element[%d] with handle[%d]", i, handle);
944                         found = 1;
945                         break;
946                 }
947         }
948
949         if (found) {
950                 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
951                         fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
952
953                         switch (found_elm->type) {
954
955                                 case EMAIL_EVENT_SEND_MAIL:
956                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
957                                         EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
958                                         if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
959                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
960                                         break;
961
962                                 case EMAIL_EVENT_DOWNLOAD_BODY:
963                                         EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
964                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
965                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_BODY_CANCEL] Failed >>>>");
966                                         break;
967
968                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
969                                         EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
970                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
971                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
972                                         break;
973
974                                 case EMAIL_EVENT_SYNC_HEADER:
975                                 case EMAIL_EVENT_SYNC_HEADER_OMA:
976                                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
977                                 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
978                                         EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
979                                         break;
980
981                                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
982                                         EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
983                                         if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
984                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
985                                         break;
986
987                                 case EMAIL_EVENT_DELETE_MAIL:
988                                 case EMAIL_EVENT_DELETE_MAIL_ALL:
989                                 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
990                                 case EMAIL_EVENT_SAVE_MAIL:
991                                 case EMAIL_EVENT_MOVE_MAIL:
992                                 case EMAIL_EVENT_CREATE_MAILBOX:
993                                 case EMAIL_EVENT_DELETE_MAILBOX:
994                                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
995                                 case EMAIL_EVENT_SEARCH_ON_SERVER:
996                                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
997                                         EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
998                                         break;
999
1000                                 default:
1001                                         break;
1002                         }
1003
1004                         pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1005                         if (pop_elm) {
1006                                 emcore_return_handle(pop_elm->handle);
1007                                 emcore_free_event(pop_elm);
1008                                 EM_SAFE_FREE(pop_elm);
1009                         } else {
1010                                 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1011                         }
1012                 } else {
1013                         switch (found_elm->type) {
1014                                 case EMAIL_EVENT_SYNC_HEADER:
1015                                         EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER");
1016                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, err))
1017                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1018                                         if ((err = emcore_update_sync_status_of_account(found_elm->multi_user_name, found_elm->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1019                                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1020                                         break;
1021
1022                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1023                                         EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1024                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_CANCEL, found_elm->event_param_data_4, NULL , found_elm->event_param_data_5, err))
1025                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_DOWNLOAD_ATTACH_CANCEL] Failed >>>>");
1026                                         break;
1027
1028                                 default:
1029                                         break;
1030                         }
1031                         found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1032                 }
1033
1034                 ret = true;
1035         }
1036
1037         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1038
1039         if (err_code != NULL)
1040                 *err_code = err;
1041         EM_DEBUG_FUNC_END();
1042         return ret;
1043 }
1044
1045 /* cancel all job  */
1046 INTERNAL_FUNC int emcore_cancel_all_thread(int *err_code)
1047 {
1048         EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1049
1050         int ret = false;
1051         int err = EMAIL_ERROR_NONE;
1052         int q_length = 0;
1053         int i = 0;
1054         email_event_t *pop_elm = NULL;
1055
1056         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1057
1058         /* g_queue_get_length is aborted when param is null */
1059         q_length = g_event_que ? g_queue_get_length(g_event_que) : 0; /*prevent 35142 */
1060
1061         for (i = 0; i < q_length; i++) {
1062                 pop_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1063
1064                 if (pop_elm && (pop_elm->status == EMAIL_EVENT_STATUS_WAIT)) {
1065
1066                         fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1067
1068                         switch (pop_elm->type) {
1069
1070                                 case EMAIL_EVENT_SEND_MAIL:
1071                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
1072                                         EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1073                                         if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1074                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1075                                         break;
1076
1077                                 case EMAIL_EVENT_DOWNLOAD_BODY:
1078                                         EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1079                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1080                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1081                                         break;
1082
1083                                 case EMAIL_EVENT_SYNC_HEADER:
1084                                 case EMAIL_EVENT_SYNC_HEADER_OMA:
1085                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1086                                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1087                                 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1088                                         EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1089                                         break;
1090
1091                                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1092                                         EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", pop_elm->account_id);
1093                                         if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, pop_elm->account_id, NULL , pop_elm->event_param_data_4, err))
1094                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1095                                         break;
1096
1097                                 case EMAIL_EVENT_DELETE_MAIL:
1098                                 case EMAIL_EVENT_DELETE_MAIL_ALL:
1099                                 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1100                                 case EMAIL_EVENT_SAVE_MAIL:
1101                                 case EMAIL_EVENT_MOVE_MAIL:
1102                                 case EMAIL_EVENT_CREATE_MAILBOX:
1103                                 case EMAIL_EVENT_DELETE_MAILBOX:
1104                                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1105                                 case EMAIL_EVENT_SEARCH_ON_SERVER:
1106                                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1107                                         EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1108                                         break;
1109
1110                                 default:
1111                                         break;
1112                         }
1113
1114                         pop_elm = (email_event_t *)g_queue_pop_nth(g_event_que, i);
1115                         if (pop_elm) {
1116                                 emcore_return_handle(pop_elm->handle);
1117                                 emcore_free_event(pop_elm);
1118                                 EM_SAFE_FREE(pop_elm);
1119                                 i--;
1120                         }
1121                         if (g_event_que)
1122                                 q_length = g_queue_get_length(g_event_que);
1123
1124                 } else {
1125                         if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1126                 }
1127         }
1128         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1129
1130         ret = true;
1131
1132         if (err_code != NULL)
1133                 *err_code = err;
1134         EM_DEBUG_FUNC_END();
1135         return ret;
1136 }
1137
1138 /* check thread status
1139 * 0 : stop job 1 : continue job
1140 */
1141 INTERNAL_FUNC int emcore_check_send_mail_thread_status(void)
1142 {
1143         EM_DEBUG_FUNC_BEGIN();
1144
1145         int ret = false;
1146         int q_length = 0;
1147         email_event_t *active_event = NULL;
1148
1149         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1150
1151         if (g_send_event_que)
1152                 q_length = g_queue_get_length(g_send_event_que);
1153
1154         if (q_length) {
1155                 active_event = (email_event_t *)g_queue_peek_head(g_send_event_que);
1156                 if (active_event) {
1157                         if (active_event->status == EMAIL_EVENT_STATUS_STARTED)
1158                                 ret = true;
1159                         else
1160                                 ret = false;
1161                 }
1162         } else {
1163                 EM_DEBUG_LOG("Send Queue is empty [%d]", q_length);
1164                 ret = true;
1165         }
1166
1167         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1168         EM_DEBUG_FUNC_END("ret [%d]", ret);
1169         return ret;
1170 }
1171
1172 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(char *multi_user_name, int account_id)
1173 {
1174         EM_DEBUG_FUNC_BEGIN();
1175         int error_code = EMAIL_ERROR_NONE;
1176         int i = 0;
1177         int q_length = 0;
1178         email_event_t *found_elm = NULL;
1179         email_event_t *pop_elm = NULL;
1180
1181         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1182
1183         if (g_event_que)
1184                 q_length = g_queue_get_length(g_event_que);
1185
1186         for (i = 0; i < q_length; i++) {
1187                 found_elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1188                 if (found_elm && (found_elm->account_id == account_id || found_elm->account_id == ALL_ACCOUNT) &&
1189                         ((!found_elm->multi_user_name && !multi_user_name) || (
1190                                 (multi_user_name) && !EM_SAFE_STRCASECMP(found_elm->multi_user_name, multi_user_name)))) {
1191                         EM_DEBUG_LOG("Found Queue element[%d]", i);
1192
1193                         if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1194                                 fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1195
1196                                 switch (found_elm->type) {
1197
1198                                         case EMAIL_EVENT_SEND_MAIL:
1199                                         case EMAIL_EVENT_SEND_MAIL_SAVED:
1200                                                 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
1201                                                 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL ,
1202                                                                                                                 found_elm->event_param_data_4, error_code))
1203                                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1204                                                 break;
1205
1206                                         case EMAIL_EVENT_DOWNLOAD_BODY:
1207                                                 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
1208                                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, found_elm->account_id, NULL,
1209                                                                                                                 found_elm->event_param_data_4, error_code))
1210                                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_SEND_CANCEL] Failed >>>>");
1211                                                 break;
1212
1213                                         case EMAIL_EVENT_SYNC_HEADER:
1214                                         case EMAIL_EVENT_SYNC_HEADER_OMA:
1215                                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
1216                                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1217                                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
1218                                                 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
1219                                                 break;
1220
1221                                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
1222                                                 EM_DEBUG_LOG("validate account waiting: cancel account id[%d]", found_elm->account_id);
1223                                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, found_elm->account_id, NULL , found_elm->event_param_data_4, error_code))
1224                                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>>");
1225                                                 break;
1226
1227                                         case EMAIL_EVENT_DELETE_MAIL:
1228                                         case EMAIL_EVENT_DELETE_MAIL_ALL:
1229                                         case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1230                                         case EMAIL_EVENT_SAVE_MAIL:
1231                                         case EMAIL_EVENT_MOVE_MAIL:
1232                                         case EMAIL_EVENT_CREATE_MAILBOX:
1233                                         case EMAIL_EVENT_DELETE_MAILBOX:
1234                                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1235                                         case EMAIL_EVENT_SEARCH_ON_SERVER:
1236                                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1237                                                 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
1238                                                 break;
1239
1240                                         default:
1241                                                 break;
1242                                 }
1243
1244                                 pop_elm = g_queue_pop_nth(g_event_que, i);
1245                                 if (pop_elm) {
1246                                         emcore_return_handle(pop_elm->handle);
1247                                         emcore_free_event(pop_elm);
1248                                         EM_SAFE_FREE(pop_elm);
1249                                         i--;
1250                                 } else {
1251                                         EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1252                                 }
1253                                 if (g_event_que)
1254                                         q_length = g_queue_get_length(g_event_que);
1255
1256                         } else {
1257                                 found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1258                         }
1259                 }
1260         }
1261
1262         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
1263
1264         EM_DEBUG_FUNC_END();
1265         return error_code;
1266 }
1267
1268
1269 /* cancel send mail job  */
1270 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
1271 {
1272         EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
1273
1274         int ret = false;
1275         int err = EMAIL_ERROR_NONE;
1276         int found = 0;
1277         int i = 0;
1278         int q_length = 0;
1279         email_event_t *found_elm = NULL;
1280         email_event_t *pop_elm = NULL;
1281
1282         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1283
1284         if (g_send_event_que)
1285                 q_length = g_queue_get_length(g_send_event_que);
1286
1287         for (i = 0; i < q_length; i++) {
1288                 found_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1289                 if (found_elm && (found_elm->handle == handle)) {
1290                         EM_DEBUG_LOG("Found Send Queue element[%d] with handle[%d]", i, handle);
1291                         found = 1;
1292                         break;
1293                 }
1294         }
1295
1296         if (found) {
1297                 if (found_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1298                         fail_status_notify(found_elm, EMAIL_ERROR_CANCELLED);
1299
1300                         switch (found_elm->type) {
1301                                 case EMAIL_EVENT_SEND_MAIL:
1302                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
1303                                         found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1304                                         if (!emcore_notify_network_event(NOTI_SEND_CANCEL, found_elm->account_id, NULL, found_elm->event_param_data_4, err))
1305                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1306                                         break;
1307                                 default:
1308                                         break;
1309                         }
1310
1311                         pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1312                         if (pop_elm) {
1313                                 emcore_return_send_handle(pop_elm->handle);
1314                                 emcore_free_event(pop_elm);
1315                                 EM_SAFE_FREE(pop_elm);
1316                         } else {
1317                                 EM_DEBUG_LOG("Failed to g_queue_pop_nth [%d] element", i);
1318                         }
1319                 } else {
1320                         found_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1321                 }
1322
1323                 ret = true;
1324         }
1325
1326         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1327
1328         if (err_code != NULL)
1329                 *err_code = err;
1330         EM_DEBUG_FUNC_END();
1331         return ret;
1332 }
1333
1334 INTERNAL_FUNC int emcore_cancel_all_send_mail_thread(int *err_code)
1335 {
1336         EM_DEBUG_FUNC_BEGIN("err_code[%p]", err_code);
1337
1338         int ret = false;
1339         int err = EMAIL_ERROR_NONE;
1340         int q_length = 0;
1341         int i = 0;
1342         email_event_t *pop_elm = NULL;
1343
1344         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1345
1346         if (g_send_event_que)
1347                 q_length = g_queue_get_length(g_send_event_que);
1348
1349         for (i = 0; i < q_length; i++) {
1350                 pop_elm = (email_event_t *)g_queue_peek_nth(g_send_event_que, i);
1351
1352                 if (pop_elm && pop_elm->status == EMAIL_EVENT_STATUS_WAIT) {
1353
1354                         fail_status_notify(pop_elm, EMAIL_ERROR_CANCELLED);
1355
1356                         switch (pop_elm->type) {
1357                                 case EMAIL_EVENT_SEND_MAIL:
1358                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
1359                                         pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1360                                         if (!emcore_notify_network_event(NOTI_SEND_CANCEL, pop_elm->account_id, NULL, pop_elm->event_param_data_4, err))
1361                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEND_CANCEL] Failed >>>>");
1362                                         break;
1363                                 default:
1364                                         break;
1365                         }
1366
1367                         pop_elm = (email_event_t *)g_queue_pop_nth(g_send_event_que, i);
1368                         if (pop_elm) {
1369                                 emcore_return_send_handle(pop_elm->handle);
1370                                 emcore_free_event(pop_elm);
1371                                 EM_SAFE_FREE(pop_elm);
1372                                 i--;
1373                         }
1374
1375                         if (g_send_event_que)
1376                                 q_length = g_queue_get_length(g_send_event_que);
1377
1378                 } else {
1379                         if (pop_elm) pop_elm->status = EMAIL_EVENT_STATUS_CANCELED;
1380                 }
1381         }
1382         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1383
1384         ret = true;
1385
1386         if (err_code != NULL)
1387                 *err_code = err;
1388         EM_DEBUG_FUNC_END();
1389         return ret;
1390 }
1391
1392 INTERNAL_FUNC int emcore_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count)
1393 {
1394         EM_DEBUG_FUNC_BEGIN("output_task_information[%p] output_task_information_count[%p]", output_task_information, output_task_information_count);
1395         int err = EMAIL_ERROR_NONE;
1396         int i = 0;
1397         int index = 0;
1398         int q_length = 0;
1399         email_task_information_t *task_information = NULL;
1400         email_event_t *elm = NULL;
1401
1402         if (output_task_information == NULL || output_task_information_count == NULL) {
1403                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1404                 err = EMAIL_ERROR_INVALID_PARAM;
1405                 goto FINISH_OFF;
1406         }
1407
1408         if (g_event_que)
1409                 q_length = g_queue_get_length(g_event_que);
1410
1411         if (!q_length) {
1412                 err = EMAIL_ERROR_DATA_NOT_FOUND;
1413                 goto FINISH_OFF;
1414         }
1415
1416         if ((task_information = em_malloc(sizeof(email_task_information_t) * q_length)) == NULL) {
1417                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1418                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1419                 goto FINISH_OFF;
1420         }
1421
1422         for (i = 0; i < q_length; i++) {
1423                 elm = (email_event_t *)g_queue_peek_nth(g_event_que, i);
1424                 if (!elm){
1425                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
1426                         err = EMAIL_ERROR_DATA_NOT_FOUND;
1427                         goto FINISH_OFF;
1428                 }
1429                 if(elm->type != EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1430                         if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1431                                 task_information[index].handle     = elm->handle;
1432                                 task_information[index].account_id = elm->account_id;
1433                                 task_information[index].type       = elm->type;
1434                                 task_information[index].status     = elm->status;
1435                                 task_information[index].task_data1 = 0;
1436                                 task_information[index].task_data2 = 0;
1437                                 task_information[index].task_data3 = 0;
1438                                 index++;
1439
1440                         }
1441                 }else if(elm->type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
1442                         if (elm->type != EMAIL_EVENT_NONE && elm->status != EMAIL_EVENT_STATUS_CANCELED) {
1443
1444                                 task_information[index].task_data1 = (void *)elm->event_param_data_4; /* mail_id */
1445                                 task_information[index].task_data2 = (void *)elm->event_param_data_5; /* attachment_nth */
1446                                 task_information[index].task_data3 = (void *)elm->event_param_data_8; /* download progress */
1447
1448                                 task_information[index].handle     = elm->handle;
1449                                 task_information[index].account_id = elm->account_id;
1450                                 task_information[index].type       = elm->type;
1451                                 task_information[index].status     = elm->status;
1452
1453                                 index++;
1454                         }
1455
1456                 }
1457         }
1458
1459         *output_task_information_count = q_length;
1460         *output_task_information       = task_information;
1461
1462 FINISH_OFF:
1463
1464         EM_DEBUG_FUNC_END("err [%d]", err);
1465         return err;
1466 }
1467
1468 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
1469 {
1470         EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
1471         if (!event_data)
1472                 return EMAIL_ERROR_INVALID_PARAM;
1473
1474         switch (event_data->type) {
1475                 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
1476                         EM_SAFE_FREE(event_data->event_param_data_3);
1477                         break;
1478
1479                 case EMAIL_EVENT_SYNC_HEADER:  /* synchronize mail header  */
1480                         break;
1481
1482                 case EMAIL_EVENT_SYNC_HEADER_OMA:  /*  synchronize mail header for OMA */
1483                         EM_SAFE_FREE(event_data->event_param_data_1);
1484                         break;
1485
1486                 case EMAIL_EVENT_DOWNLOAD_BODY:  /*  download mail body  */
1487                         break;
1488
1489                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:  /*  download attachment */
1490                         break;
1491
1492                 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
1493                         EM_SAFE_FREE(event_data->event_param_data_3);
1494                         break;
1495
1496                 case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
1497                         EM_SAFE_FREE(event_data->event_param_data_3);
1498                         break;
1499
1500                 case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
1501                         break;
1502
1503 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1504                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
1505                         break;
1506 #endif
1507
1508                 case EMAIL_EVENT_CREATE_MAILBOX:
1509                         emcore_free_mailbox((email_mailbox_t*)event_data->event_param_data_1);
1510                         EM_SAFE_FREE(event_data->event_param_data_1);
1511                         break;
1512
1513                 case EMAIL_EVENT_DELETE_MAILBOX:
1514                         break;
1515
1516                 case EMAIL_EVENT_SAVE_MAIL:
1517                         break;
1518
1519                 case EMAIL_EVENT_MOVE_MAIL:
1520                         EM_SAFE_FREE(event_data->event_param_data_3);
1521                         break;
1522
1523                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
1524                         break;
1525
1526                 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
1527                         emcore_free_account((email_account_t *)event_data->event_param_data_1);
1528                         EM_SAFE_FREE(event_data->event_param_data_1);
1529
1530                         break;
1531
1532                 case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
1533                         emcore_free_account((email_account_t *)event_data->event_param_data_1);
1534                         EM_SAFE_FREE(event_data->event_param_data_1);
1535                         break;
1536
1537                 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
1538                         emcore_free_account((email_account_t *)event_data->event_param_data_1);
1539                         EM_SAFE_FREE(event_data->event_param_data_1);
1540                         break;
1541
1542                 case EMAIL_EVENT_UPDATE_MAIL: {
1543                                 email_mail_data_t* input_mail_data = (email_mail_data_t*) event_data->event_param_data_1;
1544                                 email_attachment_data_t* input_attachment_data_list = (email_attachment_data_t*) event_data->event_param_data_2;
1545                                 email_meeting_request_t* input_meeting_request = (email_meeting_request_t*) event_data->event_param_data_3;
1546                                 int input_attachment_count = event_data->event_param_data_4;
1547                                 emcore_free_mail_data_list(&input_mail_data, 1);
1548                                 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1549                                 emstorage_free_meeting_request(input_meeting_request);
1550                         }
1551                         break;
1552
1553                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
1554                         break;
1555
1556                 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
1557                         break;
1558
1559 #ifdef __FEATURE_LOCAL_ACTIVITY__
1560                 case EMAIL_EVENT_LOCAL_ACTIVITY:
1561                         break;
1562 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
1563
1564                 case EMAIL_EVENT_SEARCH_ON_SERVER: {
1565                                 int i = 0;
1566                                 int search_filter_count = event_data->event_param_data_5;
1567                                 email_search_filter_t *search_filter = (email_search_filter_t *)event_data->event_param_data_1;
1568
1569                                 if (event_data->event_param_data_1) {
1570                                         for (i = 0; i < search_filter_count; i++) {
1571                                                 switch (search_filter[i].search_filter_type) {
1572                                                 case EMAIL_SEARCH_FILTER_TYPE_BCC:
1573                                                 case EMAIL_SEARCH_FILTER_TYPE_BODY:
1574                                                 case EMAIL_SEARCH_FILTER_TYPE_CC:
1575                                                 case EMAIL_SEARCH_FILTER_TYPE_FROM:
1576                                                 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD:
1577                                                 case EMAIL_SEARCH_FILTER_TYPE_TEXT:
1578                                                 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT:
1579                                                 case EMAIL_SEARCH_FILTER_TYPE_TO:
1580                                                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID:
1581                                                 case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME:
1582                                                 case EMAIL_SEARCH_FILTER_TYPE_CHARSET:
1583                                                 case EMAIL_SEARCH_FILTER_TYPE_USER_DEFINED:
1584                                                         EM_SAFE_FREE(search_filter[i].search_filter_key_value.string_type_key_value);
1585                                                         break;
1586                                                 default:
1587                                                         break;
1588                                                 }
1589                                         }
1590                                 }
1591                         }
1592                         EM_SAFE_FREE(event_data->event_param_data_1);
1593                         break;
1594
1595                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
1596                         EM_SAFE_FREE(event_data->event_param_data_1);
1597                         EM_SAFE_FREE(event_data->event_param_data_2);
1598                         EM_SAFE_FREE(event_data->event_param_data_3);
1599                         break;
1600
1601
1602                 case EMAIL_EVENT_SEND_MAIL:
1603                         break;
1604
1605                 case EMAIL_EVENT_SEND_MAIL_SAVED:
1606                         EM_SAFE_FREE(event_data->event_param_data_3);
1607                         break;
1608
1609                 case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
1610                         break;
1611
1612 #ifdef __FEATURE_LOCAL_ACTIVITY__
1613                 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1614                 break;
1615 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1616
1617                 default: /*free function is not implemented*/
1618                         EM_DEBUG_EXCEPTION("event %d is NOT freed, possibly memory leaks", event_data->type);
1619         }
1620
1621         EM_SAFE_FREE(event_data->multi_user_name);
1622         event_data->event_param_data_1 = event_data->event_param_data_2 = event_data->event_param_data_3 = NULL;
1623
1624         EM_DEBUG_FUNC_END();
1625         return true;
1626 }
1627
1628 static int emcore_get_new_handle(void)
1629 {
1630         EM_DEBUG_FUNC_BEGIN();
1631         int i = 0;
1632         int ret_handle = 0;
1633
1634         ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1635         for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1636                 if (handle_map[i] == 0) {
1637                         handle_map[i] = 1;
1638                         break;
1639                 }
1640         }
1641         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1642
1643         if (i < EVENT_QUEUE_MAX) {
1644                 ret_handle = i+1;
1645                 EM_DEBUG_LOG("New handle [%d]", ret_handle);
1646         } else {
1647                 ret_handle = 0;
1648                 EM_DEBUG_EXCEPTION("there is no available handle");
1649         }
1650
1651         EM_DEBUG_FUNC_END();
1652
1653         return ret_handle;
1654 }
1655
1656 static int emcore_get_new_send_handle(void)
1657 {
1658         EM_DEBUG_FUNC_BEGIN();
1659         int i = 0;
1660         int ret_handle = 0;
1661
1662         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1663         for (i = 0; i < EVENT_QUEUE_MAX; i++) {
1664                 if (send_handle_map[i] == 0) {
1665                         send_handle_map[i] = 1;
1666                         break;
1667                 }
1668         }
1669         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1670
1671         if (i < EVENT_QUEUE_MAX) {
1672                 ret_handle = i+1;
1673                 EM_DEBUG_LOG("New send handle [%d]", ret_handle);
1674         } else {
1675                 ret_handle = 0;
1676                 EM_DEBUG_LOG("there is no available send handle");
1677         }
1678
1679         EM_DEBUG_FUNC_END();
1680
1681         return ret_handle;
1682 }
1683
1684
1685
1686 static void emcore_adjust_sync_mail_flag_event_priority()
1687 {
1688         EM_DEBUG_FUNC_BEGIN();
1689         GList *sync_flag_event_list = NULL;
1690         int q_length = g_queue_get_length(g_event_que);
1691         int i;
1692         int is_sync_mail_flag_event = 0;
1693
1694         // find sync flag event.
1695         for (i = 0; i < q_length; i++) {
1696                 email_event_t *p = g_queue_peek_nth(g_event_que, i);
1697                 if (p->type == EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER) {
1698                         sync_flag_event_list = g_list_append(sync_flag_event_list, p);
1699                 }
1700         }
1701
1702         // re-arrange sync flag events to 2nd head of event queue.
1703         // first element (working element) of event queue should be kept.
1704         int nth = 1;
1705         GList *cur = g_list_last(sync_flag_event_list);
1706         while (cur) {
1707                 email_event_t *p = (email_event_t*)(cur->data);
1708                 g_queue_remove(g_event_que, p);
1709                 g_queue_push_nth(g_event_que, p, nth++);
1710                 int *mail_ids = (int*)(p->event_param_data_3);
1711
1712                 EM_DEBUG_LOG("Sync flag event: change the position of queue - mail_id: [%d] - to [%d] th", mail_ids[0], nth - 1);
1713                 is_sync_mail_flag_event = 1;
1714                 cur = g_list_previous(cur);
1715         }
1716
1717         email_event_t *first = g_queue_peek_nth(g_event_que, 0);
1718         if (is_sync_mail_flag_event && first->type == EMAIL_EVENT_SYNC_HEADER) {
1719                 // copy sync event and push to event queue
1720                 email_event_t *sync_event = em_malloc(sizeof(email_event_t));
1721                 memcpy(sync_event, first, sizeof(email_event_t));
1722                 sync_event->multi_user_name = g_strdup(first->multi_user_name);
1723                 sync_event->event_param_data_1= g_strdup(first->event_param_data_1);
1724                 sync_event->event_param_data_2= g_strdup(first->event_param_data_2);
1725                 sync_event->event_param_data_3= g_strdup(first->event_param_data_3);
1726                 sync_event->status = EMAIL_EVENT_STATUS_WAIT;
1727                 g_queue_push_nth(g_event_que, sync_event, nth);
1728                 EM_DEBUG_LOG("Sync header event: change the position of queue -  account_id: [%d] - to [%d] th", sync_event->event_param_data_5, nth);
1729
1730                 // cancel sync header event
1731                 int err = EMAIL_ERROR_NONE;
1732                 EM_DEBUG_LOG("Sync header event: change the status: from [%d] - to [%d]", first->status, EMAIL_EVENT_STATUS_CANCELED);
1733                 first->status = EMAIL_EVENT_STATUS_CANCELED;
1734                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, first->account_id, NULL , first->event_param_data_4, err))
1735                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1736                 if ((err = emcore_update_sync_status_of_account(first->multi_user_name, first->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1737                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1738         }
1739         EM_DEBUG_FUNC_END();
1740 }
1741
1742 INTERNAL_FUNC int emcore_return_handle(int handle)
1743 {
1744         EM_DEBUG_FUNC_BEGIN();
1745         int ret = false;
1746
1747         ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1748         if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1749                 handle_map[handle-1] = 0;
1750                 ret = true;
1751                 EM_DEBUG_LOG("handle [%d] is returned", handle);
1752         }
1753         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1754
1755         EM_DEBUG_FUNC_END();
1756
1757         return ret;
1758 }
1759
1760 INTERNAL_FUNC int emcore_return_send_handle(int handle)
1761 {
1762         EM_DEBUG_FUNC_BEGIN();
1763         int ret = false;
1764
1765         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1766         if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1767                 send_handle_map[handle-1] = 0;
1768                 ret = true;
1769                 EM_DEBUG_LOG("send handle [%d] is returned", handle);
1770         }
1771         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1772
1773         EM_DEBUG_FUNC_END();
1774
1775         return ret;
1776 }
1777
1778 #ifdef __FEATURE_KEEP_CONNECTION__
1779 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
1780 {
1781         return (unsigned int)g_srv_thread;
1782 }
1783 #endif /*  __FEATURE_KEEP_CONNECTION__ */
1784
1785 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1786
1787 INTERNAL_FUNC int emcore_get_pbd_thd_state()
1788 {
1789         int pbd_thd_state = false;
1790         ENTER_CRITICAL_SECTION(_state_variables_lock);
1791         pbd_thd_state = g_pbd_thd_state;
1792         LEAVE_CRITICAL_SECTION(_state_variables_lock);
1793         return pbd_thd_state;
1794 }
1795
1796 INTERNAL_FUNC int emcore_set_pbd_thd_state(int flag)
1797 {
1798         ENTER_CRITICAL_SECTION(_state_variables_lock);
1799         g_pbd_thd_state = flag;
1800         LEAVE_CRITICAL_SECTION(_state_variables_lock);
1801
1802         return g_pbd_thd_state;
1803 }
1804
1805 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
1806 {
1807         EM_DEBUG_FUNC_BEGIN();
1808         EM_DEBUG_FUNC_END();
1809         return (unsigned int)g_partial_body_thd;
1810 }
1811
1812 static int emcore_clear_bulk_pbd_que(int *err_code)
1813 {
1814         EM_DEBUG_FUNC_BEGIN();
1815
1816         int ret = true;
1817         int error = EMAIL_ERROR_NONE;
1818         int i = 0;
1819
1820         for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
1821                 if (g_partial_body_bulk_dwd_que[i].event_type) {
1822                         if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error))                                        {
1823                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1824                                 ret = false;
1825                                 break;
1826                         }
1827                 }
1828         }
1829
1830         if (NULL != err_code)
1831                 *err_code = error;
1832         EM_DEBUG_FUNC_END();
1833         return ret;
1834 }
1835
1836 INTERNAL_FUNC void emcore_pb_thd_set_local_activity_continue(int flag)
1837 {
1838         EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
1839
1840         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1841
1842         g_pb_thd_local_activity_continue = flag;
1843
1844         if (true == flag) {
1845                 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
1846         }
1847
1848         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1849         EM_DEBUG_FUNC_END();
1850 }
1851
1852 INTERNAL_FUNC int emcore_pb_thd_can_local_activity_continue()
1853 {
1854         EM_DEBUG_FUNC_BEGIN();
1855
1856         int ret = false;
1857
1858         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1859
1860         ret = g_pb_thd_local_activity_continue;
1861
1862         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1863         EM_DEBUG_FUNC_END();
1864         return ret;
1865
1866 }
1867
1868 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
1869 {
1870         EM_DEBUG_FUNC_BEGIN();
1871
1872         int ret = true;
1873         int error = EMAIL_ERROR_NONE;
1874         int i = 0;
1875
1876         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1877
1878         if (true == g_partial_body_thd_queue_empty) {
1879                 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
1880         } else {
1881                 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
1882                         if (g_partial_body_thd_event_que[i].event_type) {
1883                                 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error))                                       {
1884                                         EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1885                                         ret = false;
1886                                         break;
1887                                 }
1888                         }
1889                 }
1890
1891                 g_partial_body_thd_queue_empty = true;
1892                 g_partial_body_thd_queue_full = false;
1893         }
1894         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1895
1896         if (NULL != err_code)
1897                 *err_code = error;
1898         EM_DEBUG_FUNC_END();
1899         return ret;
1900 }
1901
1902 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
1903 {
1904         EM_DEBUG_FUNC_BEGIN();
1905
1906         int ret = false;
1907
1908         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1909
1910         ret = g_partial_body_thd_queue_empty;
1911
1912         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1913         EM_DEBUG_FUNC_END("ret [%d]", ret);
1914         return ret;
1915 }
1916 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
1917 {
1918         EM_DEBUG_FUNC_BEGIN();
1919
1920         int ret = false;
1921
1922         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1923
1924         ret = g_partial_body_thd_queue_full;
1925
1926         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1927         EM_DEBUG_FUNC_END();
1928         return ret;
1929 }
1930
1931 /*
1932 Himanshu[h.gahalut] :  If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
1933 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
1934 through _partial_body_thd_event_queue_lock mutex.
1935
1936 No mutex is used inside this API so that we can also use it to copy partial body events which are not a part of global event_data queue
1937
1938 Precautions :
1939
1940 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1941 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1942
1943 */
1944
1945 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
1946 {
1947         EM_DEBUG_FUNC_BEGIN();
1948         int error = EMAIL_ERROR_NONE;
1949         int ret = false;
1950
1951         if (NULL == src || NULL == dest) {
1952                 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
1953                 error = EMAIL_ERROR_INVALID_PARAM;
1954                 goto FINISH_OFF;
1955         }
1956
1957         dest->account_id = src->account_id;
1958         dest->mail_id = src->mail_id;
1959         dest->server_mail_id = src->server_mail_id;
1960         dest->activity_id = src->activity_id;
1961         dest->mailbox_id = src->mailbox_id;
1962         dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
1963         dest->activity_type = src->activity_type;
1964         dest->event_type = src->event_type;
1965         dest->multi_user_name = EM_SAFE_STRDUP(src->multi_user_name);
1966
1967
1968         ret = true;
1969
1970         FINISH_OFF:
1971
1972         if (NULL != error_code)
1973                 *error_code = error;
1974
1975         return ret;
1976
1977 }
1978
1979 /*
1980 Himanshu[h.gahalut] :  If emcore_free_partial_body_thd_event_cell API is used to free a cell of partial body thread event_data queue,
1981 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
1982
1983 No mutex is used inside this API so that we can also use it to free partial body events which are not a part of global event_data queue
1984
1985 Precautions :
1986
1987 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1988 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1989
1990 */
1991
1992 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
1993 {
1994         EM_DEBUG_FUNC_BEGIN();
1995
1996         if (NULL == partial_body_thd_event) {
1997                 if (error_code != NULL) {
1998                         *error_code = EMAIL_ERROR_INVALID_PARAM;
1999                 }
2000                 return false;
2001         }
2002
2003         email_event_partial_body_thd *pbd_event = partial_body_thd_event;
2004
2005         /*Free character pointers in event_data cell */
2006         EM_SAFE_FREE(pbd_event->mailbox_name);
2007         EM_SAFE_FREE(pbd_event->multi_user_name);
2008         memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
2009         EM_DEBUG_FUNC_END();
2010         return true;
2011 }
2012
2013 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(
2014                               email_event_partial_body_thd *partial_body_thd_event,
2015                               int                          *error_code)
2016 {
2017         EM_DEBUG_FUNC_BEGIN();
2018
2019         if (NULL == partial_body_thd_event)  {
2020                 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
2021
2022                 if (error_code != NULL) {
2023                         *error_code = EMAIL_ERROR_INVALID_PARAM;
2024                 }
2025                 return false;
2026         }
2027
2028         int ret = false;
2029         int error = EMAIL_ERROR_NONE;
2030         int empty_cell_index = -1;
2031         int index = 0;
2032         int count = 0;
2033
2034         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2035
2036         /* find a cell in queue which is empty */
2037         index = g_partial_body_thd_next_event_idx;
2038         for (count = 0; count < TOTAL_PARTIAL_BODY_EVENTS; count++) {
2039                 /*Found empty Cell*/
2040                 if (g_partial_body_thd_event_que[index].event_type == EMAIL_EVENT_NONE) {
2041                         empty_cell_index = index;
2042                         break;
2043                 }
2044                 index++;
2045                 index = index % TOTAL_PARTIAL_BODY_EVENTS;
2046         }
2047
2048         if (empty_cell_index != -1) {
2049                 emcore_copy_partial_body_thd_event(partial_body_thd_event,
2050                                  g_partial_body_thd_event_que + empty_cell_index,
2051                                  &error);
2052                 if (error != EMAIL_ERROR_NONE) {
2053                         EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
2054                 } else {
2055                         g_partial_body_thd_queue_empty = false;
2056                         if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
2057                                 /*This is the last event_data inserted in queue after its insertion, queue is full */
2058                                 g_partial_body_thd_queue_full = true;
2059                         }
2060                         WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2061                         ret = true;
2062                 }
2063         } else {
2064                 EM_DEBUG_LOG("partial body thread event_data queue is full ");
2065                 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
2066
2067                 g_partial_body_thd_queue_full = true;
2068                 g_partial_body_thd_queue_empty = false;
2069         }
2070
2071         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2072
2073         if (NULL != error_code) {
2074                 *error_code = error;
2075         }
2076
2077         return ret;
2078
2079 }
2080
2081 /* h.gahlaut :  Return true only if event_data is retrieved successfully */
2082
2083 INTERNAL_FUNC int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
2084 {
2085         EM_DEBUG_FUNC_BEGIN();
2086
2087         int ret = false;
2088         int error = EMAIL_ERROR_NONE;
2089         int index = 0;
2090
2091         /* Lock Mutex to protect event_data queue and associated global variables variables*/
2092
2093         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2094
2095         index = g_partial_body_thd_next_event_idx;
2096
2097         if (0 == g_partial_body_thd_event_que[index].event_type) {
2098                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
2099                 g_partial_body_thd_queue_empty = true;
2100                 g_partial_body_thd_queue_full = false;
2101         } else {
2102                 /*Copy the event_data from queue to return it and free the event_data in queue */
2103                 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
2104                         EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
2105                 else {
2106                         if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
2107                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2108                         else {
2109                                 g_partial_body_thd_queue_full = false;
2110                                 g_partial_body_thd_next_event_idx = ++index;
2111
2112                                 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
2113                                         g_partial_body_thd_next_event_idx = 0;
2114
2115                                 /* If the event_data retrieved was the only event_data present in queue,
2116                                 we need to set g_partial_body_thd_queue_empty to true
2117                                 */
2118
2119                                 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
2120                                         g_partial_body_thd_queue_empty = true;
2121                                 }
2122
2123                                 ret = true;
2124                         }
2125                 }
2126         }
2127
2128         /* Unlock Mutex */
2129
2130         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2131
2132         if (error_code)
2133                 *error_code = error;
2134
2135         return ret;
2136
2137 }
2138
2139 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
2140 static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code)
2141 {
2142         EM_DEBUG_FUNC_BEGIN();
2143         int error = EMAIL_ERROR_NONE;
2144         int ret = false;
2145         MAILSTREAM *stream = NULL;
2146         void *tmp_stream = NULL;
2147
2148         if (!emcore_connect_to_remote_mailbox(multi_user_name,
2149                                                                                         g_partial_body_bulk_dwd_que[0].account_id,
2150                                                                                         g_partial_body_bulk_dwd_que[0].mailbox_id,
2151                                                                                         true,
2152                                                                                         (void **)&tmp_stream,
2153                                                                                         &error) || (NULL == tmp_stream)) {
2154                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2155                 goto FINISH_OFF;
2156         }
2157         stream = (MAILSTREAM *)tmp_stream;
2158
2159         /*  Call bulk download here */
2160         if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
2161                 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2162                 goto FINISH_OFF;
2163         }
2164
2165         ret = true;
2166 FINISH_OFF:
2167
2168         stream = mail_close(stream);
2169
2170         g_partial_body_bulk_dwd_next_event_idx = 0;
2171         g_partial_body_bulk_dwd_queue_empty = true;
2172
2173         if (false == emcore_clear_bulk_pbd_que(&error))
2174                 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
2175
2176         if (NULL != error_code)
2177                 *error_code = error;
2178
2179         EM_DEBUG_FUNC_END("ret [%d]", ret);
2180         return ret;
2181 }
2182
2183
2184 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
2185 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
2186 {
2187         EM_DEBUG_FUNC_BEGIN();
2188         int error = EMAIL_ERROR_NONE;
2189         int ret = false;
2190         int count = 0;
2191         int i = 0, m = 0;
2192         MAILSTREAM *stream = NULL;
2193         email_event_partial_body_thd *activity_data_list = NULL;
2194         int *mailbox_list = NULL;
2195         int *account_list = NULL;
2196
2197         if (NULL == pbd_event) {
2198             EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
2199
2200             error = EMAIL_ERROR_INVALID_PARAM;
2201             goto FINISH_OFF;
2202         }
2203
2204         /*Check if the event_data is to flush the event_data que array */
2205         if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
2206                 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
2207                 /*Check if the mailbox name and account id for this event_data is same as the mailbox name and account id for earlier events saved in download que array
2208                 then append this event_data also to download que array */
2209                 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
2210                         (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
2211                         EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
2212                         EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
2213                         if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
2214                                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2215                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2216                                         goto FINISH_OFF;
2217                                 }
2218                         }
2219                 } else  {
2220                         EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
2221                         EM_DEBUG_LOG("Flush the current que if not empty");
2222                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
2223                         if (!g_partial_body_bulk_dwd_queue_empty) {
2224                                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2225                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2226                                         goto FINISH_OFF;
2227                                 }
2228                         }
2229                 }
2230                 /*Add the event_data to the download que array */
2231                 if (false == emcore_copy_partial_body_thd_event(pbd_event,
2232                                  g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
2233                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
2234                 else {
2235                         g_partial_body_bulk_dwd_queue_empty = false;
2236                         g_partial_body_bulk_dwd_next_event_idx++;
2237                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
2238                 }
2239         } else if (pbd_event->activity_type) {
2240                 int account_count = 0;
2241
2242                 EM_DEBUG_LOG("Event is coming from local activity.");
2243                 /* Get all the accounts for which local activities are pending */
2244                 if (false == emstorage_get_pbd_account_list(pbd_event->multi_user_name, &account_list, &account_count, false, &error)) {
2245                                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2246                                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2247                                 goto FINISH_OFF;
2248                 }
2249
2250                 for (m = 0; m < account_count; ++m) {
2251                         /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
2252                         if (false == emstorage_get_pbd_mailbox_list(pbd_event->multi_user_name, account_list[m], &mailbox_list, &count, false, &error)) {
2253                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2254                                         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2255                                         goto FINISH_OFF;
2256                         }
2257
2258                         for (i = 0; i < count; i++) {
2259                                 int k = 0;
2260                                 int activity_count = 0;
2261
2262                                 if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name,
2263                                                                                                                 account_list[m],
2264                                                                                                                 mailbox_list[i],
2265                                                                                                                 true,
2266                                                                                                                 (void **)&stream,
2267                                                                                                                 &error)) {
2268                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2269                                         stream = mail_close(stream);
2270                                         goto FINISH_OFF;
2271                                 }
2272
2273                                 if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[m], mailbox_list[i], &activity_data_list, &activity_count,  false, &error))
2274                                         EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2275
2276                                 /* it is duplicated with emstorage_get_pbd_activity_data
2277                                 if (false == emstorage_get_mailbox_pbd_activity_count(pbd_evnet->multi_user_name, account_list[m], mailbox_list[i], &activity_count, false, &error)) {
2278                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
2279                                         continue;
2280                                 }
2281                                 */
2282                                 if (activity_count > 0 && activity_data_list) {
2283                                         int temp_error = EMAIL_ERROR_NONE;
2284                                         int j = 0;
2285                                         int iter = 0;
2286                                         int remainder = 0;
2287                                         int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2288                                         int index = 0;
2289
2290 /*
2291                                         if (false == emstorage_get_pbd_activity_data(pbd_event->multi_user_name, account_list[j], mailbox_list[i], &activity_data_list, &num_activity,  false, &error))
2292                                                 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2293
2294                                         if (NULL == activity_data_list) {
2295                                                 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
2296                                                 continue;
2297                                         }
2298 */
2299                                         remainder = activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2300                                         iter = activity_count/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1  :  0);
2301
2302                                         for (j = 0; j < iter; j++) {
2303                                                 if ((iter == (j+1)) && (remainder != 0))
2304                                                         num = remainder;
2305
2306                                                 /*Call bulk download here */
2307                                                 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
2308                                                         EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2309                                                         temp_error = EMAIL_ERROR_NO_RESPONSE;
2310                                                 }
2311
2312                                                 index += num;
2313
2314                                                 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
2315                                                         temp_error = EMAIL_ERROR_NONE;
2316                                                         break;
2317                                                 }
2318                                         }
2319
2320                                         for (k = 0; k < activity_count; k++)
2321                                                 emcore_free_partial_body_thd_event(activity_data_list + k, &error);
2322
2323                                         EM_SAFE_FREE(activity_data_list);
2324
2325                                         /*check: empty check required?*/
2326                                         if (false == emcore_is_partial_body_thd_que_empty()) {
2327                                                 ret = true;
2328                                                 goto FINISH_OFF;                /* Stop Local Activity Sync */
2329                                         }
2330                                 }
2331                                 stream = mail_close(stream);
2332                         }
2333                 }
2334
2335                 /* After completing one cycle of local activity sync ,
2336                 local activity continue variable should be set to false. */
2337
2338                 emcore_pb_thd_set_local_activity_continue(false);
2339         } else /* Event-type is 0 which means Event is to flush the que when no more events are present in g_partial_body_thd_event_que */ {
2340                 /*Check if events have arrived in g_partial_body_thd_event_que */
2341                 if (false == emcore_is_partial_body_thd_que_empty()) {
2342                         EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
2343                         ret = true;
2344                         goto FINISH_OFF;                /* Stop Local Activity Sync */
2345                 }
2346
2347                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2348                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2349                         goto FINISH_OFF;
2350                 }
2351         }
2352
2353         ret = true;
2354
2355 FINISH_OFF:
2356
2357         EM_SAFE_FREE(mailbox_list);
2358         EM_SAFE_FREE(account_list);
2359         stream = mail_close(stream);
2360
2361         if (error_code)
2362                 *error_code = error;
2363         EM_DEBUG_FUNC_END("ret [%d]", ret);
2364         return ret;
2365 }
2366
2367 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */