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