cannot read draft mail because of error of sorting events with priority(TSAM_10281)
[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                 // working element is index = 0, except working element
1698                 if (p->type == EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER && i > 0) {
1699                         sync_flag_event_list = g_list_append(sync_flag_event_list, p);
1700                 }
1701         }
1702
1703         // re-arrange sync flag events to 2nd head of event queue.
1704         // first element (working element) of event queue should be kept.
1705         int nth = 1;
1706         GList *cur = g_list_last(sync_flag_event_list);
1707         while (cur) {
1708                 email_event_t *p = (email_event_t*)(cur->data);
1709                 g_queue_remove(g_event_que, p);
1710                 g_queue_push_nth(g_event_que, p, nth++);
1711                 int *mail_ids = (int*)(p->event_param_data_3);
1712
1713                 EM_DEBUG_LOG("Sync flag event: change the position of queue - mail_id: [%d] - to [%d] th", mail_ids[0], nth - 1);
1714                 is_sync_mail_flag_event = 1;
1715                 cur = g_list_previous(cur);
1716         }
1717
1718         email_event_t *first = g_queue_peek_nth(g_event_que, 0);
1719         if (is_sync_mail_flag_event && first->type == EMAIL_EVENT_SYNC_HEADER) {
1720                 // copy sync event and push to event queue
1721                 email_event_t *sync_event = em_malloc(sizeof(email_event_t));
1722                 memcpy(sync_event, first, sizeof(email_event_t));
1723                 sync_event->multi_user_name = g_strdup(first->multi_user_name);
1724                 sync_event->event_param_data_1= g_strdup(first->event_param_data_1);
1725                 sync_event->event_param_data_2= g_strdup(first->event_param_data_2);
1726                 sync_event->event_param_data_3= g_strdup(first->event_param_data_3);
1727                 sync_event->status = EMAIL_EVENT_STATUS_WAIT;
1728                 g_queue_push_nth(g_event_que, sync_event, nth);
1729                 EM_DEBUG_LOG("Sync header event: change the position of queue -  account_id: [%d] - to [%d] th", sync_event->event_param_data_5, nth);
1730
1731                 // cancel sync header event
1732                 int err = EMAIL_ERROR_NONE;
1733                 EM_DEBUG_LOG("Sync header event: change the status: from [%d] - to [%d]", first->status, EMAIL_EVENT_STATUS_CANCELED);
1734                 first->status = EMAIL_EVENT_STATUS_CANCELED;
1735                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_CANCEL, first->account_id, NULL , first->event_param_data_4, err))
1736                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_CANCEL] Failed >>>>");
1737                 if ((err = emcore_update_sync_status_of_account(first->multi_user_name, first->account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1738                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1739         }
1740         EM_DEBUG_FUNC_END();
1741 }
1742
1743 INTERNAL_FUNC int emcore_return_handle(int handle)
1744 {
1745         EM_DEBUG_FUNC_BEGIN();
1746         int ret = false;
1747
1748         ENTER_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1749         if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1750                 handle_map[handle-1] = 0;
1751                 ret = true;
1752                 EM_DEBUG_LOG("handle [%d] is returned", handle);
1753         }
1754         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
1755
1756         EM_DEBUG_FUNC_END();
1757
1758         return ret;
1759 }
1760
1761 INTERNAL_FUNC int emcore_return_send_handle(int handle)
1762 {
1763         EM_DEBUG_FUNC_BEGIN();
1764         int ret = false;
1765
1766         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1767         if (handle > 0 && handle <= EVENT_QUEUE_MAX) {
1768                 send_handle_map[handle-1] = 0;
1769                 ret = true;
1770                 EM_DEBUG_LOG("send handle [%d] is returned", handle);
1771         }
1772         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
1773
1774         EM_DEBUG_FUNC_END();
1775
1776         return ret;
1777 }
1778
1779 #ifdef __FEATURE_KEEP_CONNECTION__
1780 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
1781 {
1782         return (unsigned int)g_srv_thread;
1783 }
1784 #endif /*  __FEATURE_KEEP_CONNECTION__ */
1785
1786 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1787
1788 INTERNAL_FUNC int emcore_get_pbd_thd_state()
1789 {
1790         int pbd_thd_state = false;
1791         ENTER_CRITICAL_SECTION(_state_variables_lock);
1792         pbd_thd_state = g_pbd_thd_state;
1793         LEAVE_CRITICAL_SECTION(_state_variables_lock);
1794         return pbd_thd_state;
1795 }
1796
1797 INTERNAL_FUNC int emcore_set_pbd_thd_state(int flag)
1798 {
1799         ENTER_CRITICAL_SECTION(_state_variables_lock);
1800         g_pbd_thd_state = flag;
1801         LEAVE_CRITICAL_SECTION(_state_variables_lock);
1802
1803         return g_pbd_thd_state;
1804 }
1805
1806 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
1807 {
1808         EM_DEBUG_FUNC_BEGIN();
1809         EM_DEBUG_FUNC_END();
1810         return (unsigned int)g_partial_body_thd;
1811 }
1812
1813 static int emcore_clear_bulk_pbd_que(int *err_code)
1814 {
1815         EM_DEBUG_FUNC_BEGIN();
1816
1817         int ret = true;
1818         int error = EMAIL_ERROR_NONE;
1819         int i = 0;
1820
1821         for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
1822                 if (g_partial_body_bulk_dwd_que[i].event_type) {
1823                         if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error))                                        {
1824                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1825                                 ret = false;
1826                                 break;
1827                         }
1828                 }
1829         }
1830
1831         if (NULL != err_code)
1832                 *err_code = error;
1833         EM_DEBUG_FUNC_END();
1834         return ret;
1835 }
1836
1837 INTERNAL_FUNC void emcore_pb_thd_set_local_activity_continue(int flag)
1838 {
1839         EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
1840
1841         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1842
1843         g_pb_thd_local_activity_continue = flag;
1844
1845         if (true == flag) {
1846                 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
1847         }
1848
1849         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1850         EM_DEBUG_FUNC_END();
1851 }
1852
1853 INTERNAL_FUNC int emcore_pb_thd_can_local_activity_continue()
1854 {
1855         EM_DEBUG_FUNC_BEGIN();
1856
1857         int ret = false;
1858
1859         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1860
1861         ret = g_pb_thd_local_activity_continue;
1862
1863         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1864         EM_DEBUG_FUNC_END();
1865         return ret;
1866
1867 }
1868
1869 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
1870 {
1871         EM_DEBUG_FUNC_BEGIN();
1872
1873         int ret = true;
1874         int error = EMAIL_ERROR_NONE;
1875         int i = 0;
1876
1877         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1878
1879         if (true == g_partial_body_thd_queue_empty) {
1880                 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
1881         } else {
1882                 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
1883                         if (g_partial_body_thd_event_que[i].event_type) {
1884                                 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error))                                       {
1885                                         EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
1886                                         ret = false;
1887                                         break;
1888                                 }
1889                         }
1890                 }
1891
1892                 g_partial_body_thd_queue_empty = true;
1893                 g_partial_body_thd_queue_full = false;
1894         }
1895         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1896
1897         if (NULL != err_code)
1898                 *err_code = error;
1899         EM_DEBUG_FUNC_END();
1900         return ret;
1901 }
1902
1903 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
1904 {
1905         EM_DEBUG_FUNC_BEGIN();
1906
1907         int ret = false;
1908
1909         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1910
1911         ret = g_partial_body_thd_queue_empty;
1912
1913         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1914         EM_DEBUG_FUNC_END("ret [%d]", ret);
1915         return ret;
1916 }
1917 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
1918 {
1919         EM_DEBUG_FUNC_BEGIN();
1920
1921         int ret = false;
1922
1923         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1924
1925         ret = g_partial_body_thd_queue_full;
1926
1927         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1928         EM_DEBUG_FUNC_END();
1929         return ret;
1930 }
1931
1932 /*
1933 Himanshu[h.gahalut] :  If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
1934 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
1935 through _partial_body_thd_event_queue_lock mutex.
1936
1937 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
1938
1939 Precautions :
1940
1941 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1942 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1943
1944 */
1945
1946 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
1947 {
1948         EM_DEBUG_FUNC_BEGIN();
1949         int error = EMAIL_ERROR_NONE;
1950         int ret = false;
1951
1952         if (NULL == src || NULL == dest) {
1953                 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
1954                 error = EMAIL_ERROR_INVALID_PARAM;
1955                 goto FINISH_OFF;
1956         }
1957
1958         dest->account_id = src->account_id;
1959         dest->mail_id = src->mail_id;
1960         dest->server_mail_id = src->server_mail_id;
1961         dest->activity_id = src->activity_id;
1962         dest->mailbox_id = src->mailbox_id;
1963         dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
1964         dest->activity_type = src->activity_type;
1965         dest->event_type = src->event_type;
1966         dest->multi_user_name = EM_SAFE_STRDUP(src->multi_user_name);
1967
1968
1969         ret = true;
1970
1971         FINISH_OFF:
1972
1973         if (NULL != error_code)
1974                 *error_code = error;
1975
1976         return ret;
1977
1978 }
1979
1980 /*
1981 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,
1982 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
1983
1984 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
1985
1986 Precautions :
1987
1988 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
1989 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
1990
1991 */
1992
1993 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
1994 {
1995         EM_DEBUG_FUNC_BEGIN();
1996
1997         if (NULL == partial_body_thd_event) {
1998                 if (error_code != NULL) {
1999                         *error_code = EMAIL_ERROR_INVALID_PARAM;
2000                 }
2001                 return false;
2002         }
2003
2004         email_event_partial_body_thd *pbd_event = partial_body_thd_event;
2005
2006         /*Free character pointers in event_data cell */
2007         EM_SAFE_FREE(pbd_event->mailbox_name);
2008         EM_SAFE_FREE(pbd_event->multi_user_name);
2009         memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
2010         EM_DEBUG_FUNC_END();
2011         return true;
2012 }
2013
2014 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(
2015                               email_event_partial_body_thd *partial_body_thd_event,
2016                               int                          *error_code)
2017 {
2018         EM_DEBUG_FUNC_BEGIN();
2019
2020         if (NULL == partial_body_thd_event)  {
2021                 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
2022
2023                 if (error_code != NULL) {
2024                         *error_code = EMAIL_ERROR_INVALID_PARAM;
2025                 }
2026                 return false;
2027         }
2028
2029         int ret = false;
2030         int error = EMAIL_ERROR_NONE;
2031         int empty_cell_index = -1;
2032         int index = 0;
2033         int count = 0;
2034
2035         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2036
2037         /* find a cell in queue which is empty */
2038         index = g_partial_body_thd_next_event_idx;
2039         for (count = 0; count < TOTAL_PARTIAL_BODY_EVENTS; count++) {
2040                 /*Found empty Cell*/
2041                 if (g_partial_body_thd_event_que[index].event_type == EMAIL_EVENT_NONE) {
2042                         empty_cell_index = index;
2043                         break;
2044                 }
2045                 index++;
2046                 index = index % TOTAL_PARTIAL_BODY_EVENTS;
2047         }
2048
2049         if (empty_cell_index != -1) {
2050                 emcore_copy_partial_body_thd_event(partial_body_thd_event,
2051                                  g_partial_body_thd_event_que + empty_cell_index,
2052                                  &error);
2053                 if (error != EMAIL_ERROR_NONE) {
2054                         EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
2055                 } else {
2056                         g_partial_body_thd_queue_empty = false;
2057                         if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
2058                                 /*This is the last event_data inserted in queue after its insertion, queue is full */
2059                                 g_partial_body_thd_queue_full = true;
2060                         }
2061                         WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2062                         ret = true;
2063                 }
2064         } else {
2065                 EM_DEBUG_LOG("partial body thread event_data queue is full ");
2066                 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
2067
2068                 g_partial_body_thd_queue_full = true;
2069                 g_partial_body_thd_queue_empty = false;
2070         }
2071
2072         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2073
2074         if (NULL != error_code) {
2075                 *error_code = error;
2076         }
2077
2078         return ret;
2079
2080 }
2081
2082 /* h.gahlaut :  Return true only if event_data is retrieved successfully */
2083
2084 INTERNAL_FUNC int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
2085 {
2086         EM_DEBUG_FUNC_BEGIN();
2087
2088         int ret = false;
2089         int error = EMAIL_ERROR_NONE;
2090         int index = 0;
2091
2092         /* Lock Mutex to protect event_data queue and associated global variables variables*/
2093
2094         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2095
2096         index = g_partial_body_thd_next_event_idx;
2097
2098         if (0 == g_partial_body_thd_event_que[index].event_type) {
2099                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
2100                 g_partial_body_thd_queue_empty = true;
2101                 g_partial_body_thd_queue_full = false;
2102         } else {
2103                 /*Copy the event_data from queue to return it and free the event_data in queue */
2104                 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
2105                         EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
2106                 else {
2107                         if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
2108                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2109                         else {
2110                                 g_partial_body_thd_queue_full = false;
2111                                 g_partial_body_thd_next_event_idx = ++index;
2112
2113                                 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
2114                                         g_partial_body_thd_next_event_idx = 0;
2115
2116                                 /* If the event_data retrieved was the only event_data present in queue,
2117                                 we need to set g_partial_body_thd_queue_empty to true
2118                                 */
2119
2120                                 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
2121                                         g_partial_body_thd_queue_empty = true;
2122                                 }
2123
2124                                 ret = true;
2125                         }
2126                 }
2127         }
2128
2129         /* Unlock Mutex */
2130
2131         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2132
2133         if (error_code)
2134                 *error_code = error;
2135
2136         return ret;
2137
2138 }
2139
2140 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
2141 static int emcore_partial_body_bulk_flush(char *multi_user_name, int *error_code)
2142 {
2143         EM_DEBUG_FUNC_BEGIN();
2144         int error = EMAIL_ERROR_NONE;
2145         int ret = false;
2146         MAILSTREAM *stream = NULL;
2147         void *tmp_stream = NULL;
2148
2149         if (!emcore_connect_to_remote_mailbox(multi_user_name,
2150                                                                                         g_partial_body_bulk_dwd_que[0].account_id,
2151                                                                                         g_partial_body_bulk_dwd_que[0].mailbox_id,
2152                                                                                         true,
2153                                                                                         (void **)&tmp_stream,
2154                                                                                         &error) || (NULL == tmp_stream)) {
2155                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2156                 goto FINISH_OFF;
2157         }
2158         stream = (MAILSTREAM *)tmp_stream;
2159
2160         /*  Call bulk download here */
2161         if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
2162                 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2163                 goto FINISH_OFF;
2164         }
2165
2166         ret = true;
2167 FINISH_OFF:
2168
2169         stream = mail_close(stream);
2170
2171         g_partial_body_bulk_dwd_next_event_idx = 0;
2172         g_partial_body_bulk_dwd_queue_empty = true;
2173
2174         if (false == emcore_clear_bulk_pbd_que(&error))
2175                 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
2176
2177         if (NULL != error_code)
2178                 *error_code = error;
2179
2180         EM_DEBUG_FUNC_END("ret [%d]", ret);
2181         return ret;
2182 }
2183
2184
2185 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
2186 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
2187 {
2188         EM_DEBUG_FUNC_BEGIN();
2189         int error = EMAIL_ERROR_NONE;
2190         int ret = false;
2191         int count = 0;
2192         int i = 0, m = 0;
2193         MAILSTREAM *stream = NULL;
2194         email_event_partial_body_thd *activity_data_list = NULL;
2195         int *mailbox_list = NULL;
2196         int *account_list = NULL;
2197
2198         if (NULL == pbd_event) {
2199             EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
2200
2201             error = EMAIL_ERROR_INVALID_PARAM;
2202             goto FINISH_OFF;
2203         }
2204
2205         /*Check if the event_data is to flush the event_data que array */
2206         if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
2207                 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
2208                 /*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
2209                 then append this event_data also to download que array */
2210                 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
2211                         (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
2212                         EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
2213                         EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
2214                         if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
2215                                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2216                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2217                                         goto FINISH_OFF;
2218                                 }
2219                         }
2220                 } else  {
2221                         EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
2222                         EM_DEBUG_LOG("Flush the current que if not empty");
2223                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
2224                         if (!g_partial_body_bulk_dwd_queue_empty) {
2225                                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2226                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2227                                         goto FINISH_OFF;
2228                                 }
2229                         }
2230                 }
2231                 /*Add the event_data to the download que array */
2232                 if (false == emcore_copy_partial_body_thd_event(pbd_event,
2233                                  g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
2234                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
2235                 else {
2236                         g_partial_body_bulk_dwd_queue_empty = false;
2237                         g_partial_body_bulk_dwd_next_event_idx++;
2238                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
2239                 }
2240         } else if (pbd_event->activity_type) {
2241                 int account_count = 0;
2242
2243                 EM_DEBUG_LOG("Event is coming from local activity.");
2244                 /* Get all the accounts for which local activities are pending */
2245                 if (false == emstorage_get_pbd_account_list(pbd_event->multi_user_name, &account_list, &account_count, false, &error)) {
2246                                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2247                                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2248                                 goto FINISH_OFF;
2249                 }
2250
2251                 for (m = 0; m < account_count; ++m) {
2252                         /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
2253                         if (false == emstorage_get_pbd_mailbox_list(pbd_event->multi_user_name, account_list[m], &mailbox_list, &count, false, &error)) {
2254                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
2255                                         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2256                                         goto FINISH_OFF;
2257                         }
2258
2259                         for (i = 0; i < count; i++) {
2260                                 int k = 0;
2261                                 int activity_count = 0;
2262
2263                                 if (!emcore_connect_to_remote_mailbox(pbd_event->multi_user_name,
2264                                                                                                                 account_list[m],
2265                                                                                                                 mailbox_list[i],
2266                                                                                                                 true,
2267                                                                                                                 (void **)&stream,
2268                                                                                                                 &error)) {
2269                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
2270                                         stream = mail_close(stream);
2271                                         goto FINISH_OFF;
2272                                 }
2273
2274                                 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))
2275                                         EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2276
2277                                 /* it is duplicated with emstorage_get_pbd_activity_data
2278                                 if (false == emstorage_get_mailbox_pbd_activity_count(pbd_evnet->multi_user_name, account_list[m], mailbox_list[i], &activity_count, false, &error)) {
2279                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
2280                                         continue;
2281                                 }
2282                                 */
2283                                 if (activity_count > 0 && activity_data_list) {
2284                                         int temp_error = EMAIL_ERROR_NONE;
2285                                         int j = 0;
2286                                         int iter = 0;
2287                                         int remainder = 0;
2288                                         int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2289                                         int index = 0;
2290
2291 /*
2292                                         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))
2293                                                 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
2294
2295                                         if (NULL == activity_data_list) {
2296                                                 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
2297                                                 continue;
2298                                         }
2299 */
2300                                         remainder = activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
2301                                         iter = activity_count/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((activity_count%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1  :  0);
2302
2303                                         for (j = 0; j < iter; j++) {
2304                                                 if ((iter == (j+1)) && (remainder != 0))
2305                                                         num = remainder;
2306
2307                                                 /*Call bulk download here */
2308                                                 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
2309                                                         EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
2310                                                         temp_error = EMAIL_ERROR_NO_RESPONSE;
2311                                                 }
2312
2313                                                 index += num;
2314
2315                                                 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
2316                                                         temp_error = EMAIL_ERROR_NONE;
2317                                                         break;
2318                                                 }
2319                                         }
2320
2321                                         for (k = 0; k < activity_count; k++)
2322                                                 emcore_free_partial_body_thd_event(activity_data_list + k, &error);
2323
2324                                         EM_SAFE_FREE(activity_data_list);
2325
2326                                         /*check: empty check required?*/
2327                                         if (false == emcore_is_partial_body_thd_que_empty()) {
2328                                                 ret = true;
2329                                                 goto FINISH_OFF;                /* Stop Local Activity Sync */
2330                                         }
2331                                 }
2332                                 stream = mail_close(stream);
2333                         }
2334                 }
2335
2336                 /* After completing one cycle of local activity sync ,
2337                 local activity continue variable should be set to false. */
2338
2339                 emcore_pb_thd_set_local_activity_continue(false);
2340         } 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 */ {
2341                 /*Check if events have arrived in g_partial_body_thd_event_que */
2342                 if (false == emcore_is_partial_body_thd_que_empty()) {
2343                         EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
2344                         ret = true;
2345                         goto FINISH_OFF;                /* Stop Local Activity Sync */
2346                 }
2347
2348                 if (false == emcore_partial_body_bulk_flush(pbd_event->multi_user_name, &error)) {
2349                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
2350                         goto FINISH_OFF;
2351                 }
2352         }
2353
2354         ret = true;
2355
2356 FINISH_OFF:
2357
2358         EM_SAFE_FREE(mailbox_list);
2359         EM_SAFE_FREE(account_list);
2360         stream = mail_close(stream);
2361
2362         if (error_code)
2363                 *error_code = error;
2364         EM_DEBUG_FUNC_END("ret [%d]", ret);
2365         return ret;
2366 }
2367
2368 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */