Tizen 2.0 Release
[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
23
24 /******************************************************************************
25  * File :  email-core-event_data.h
26  * Desc :  Mail Engine Event
27  *
28  * Auth :
29  *
30  * History :
31  *    2006.08.16  :  created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <glib.h>
35 #include <malloc.h>
36 #include <pthread.h>
37 #include <vconf.h>
38 #include <signal.h>
39 #include <contacts.h>
40 #include "c-client.h"
41 #include "email-convert.h"
42 #include "email-storage.h"
43 #include "email-network.h"
44 #include "email-device.h"
45 #include "email-utilities.h"
46 #include "email-daemon-auto-poll.h"
47 #include "email-core-global.h"
48 #include "email-core-account.h"
49 #include "email-core-event.h"
50 #include "email-core-utils.h"
51 #include "email-core-mailbox.h"
52 #include "email-core-imap-mailbox.h"
53 #include "email-core-mail.h"
54 #include "email-core-mailbox-sync.h"
55 #include "email-core-smtp.h"
56 #include "email-core-utils.h"
57 #include "email-core-sound.h"
58 #include "email-core-signal.h"
59 #include "email-debug-log.h"
60
61
62 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
63
64 /*[h.gahlaut] - All static variable declaration for partial body download thread are done here */
65
66 #define TOTAL_PARTIAL_BODY_EVENTS 100
67
68 static email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
69
70 static int g_partial_body_thd_next_event_idx = 0;                       /* Index of Next Event to be processed in the queue*/
71 static int g_partial_body_thd_loop = 1;                                         /* Variable to make a continuos while loop */
72 static int g_partial_body_thd_queue_empty = true;                       /* Variable to determine if event queue is empty.True means empty*/
73 static int g_partial_body_thd_queue_full = false;                       /* Variable to determine if event queue is full. True means full*/
74 static int g_pb_thd_local_activity_continue = true;                     /* Variable to control local activity sync */
75 int g_pbd_thd_state = false;                                                            /* false :  thread is sleeping , true :  thread is working */
76
77 static pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER;  /* Mutex to protect event queue */
78 static pthread_cond_t  _partial_body_thd_cond = PTHREAD_COND_INITIALIZER;                               /* Condition variable on which partial body thread is waiting  */
79 thread_t g_partial_body_thd ;                                                           /* Determines if thread is created or not. Non Null means thread is created */
80
81 email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
82 static int g_partial_body_bulk_dwd_next_event_idx = 0;          /* Index of Next Event to be processed in the queue*/
83 static int g_partial_body_bulk_dwd_queue_empty = true;
84
85 static pthread_mutex_t _state_variables_lock;
86
87 /*[h.gahlaut] - All static function declaration for partial body download thread are done here */
88
89 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code);
90 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
91 static void emcore_pb_thd_set_local_activity_continue(int flag);
92 static int emcore_set_pbd_thd_state(int flag);
93 static int emcore_clear_bulk_pbd_que(int *err_code);
94 int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code);
95
96 #endif
97
98 #ifdef ENABLE_IMAP_IDLE_THREAD
99 extern int g_imap_idle_thread_alive;
100 extern int imap_idle_thread;
101 #endif /* ENABLE_IMAP_IDLE_THREAD */
102 INTERNAL_FUNC int g_client_count = 0;
103 INTERNAL_FUNC int g_client_run = 0 ;
104
105 #ifdef __FEATURE_LOCAL_ACTIVITY__
106 INTERNAL_FUNC int g_local_activity_run = 0;
107 INTERNAL_FUNC int g_save_local_activity_run = 0;
108 #endif
109
110
111 #define EVENT_QUEUE_MAX 32
112
113 typedef struct EVENT_CALLBACK_ELEM
114 {
115         email_event_callback callback;
116         void *event_data;
117         struct EVENT_CALLBACK_ELEM *next;
118 } EVENT_CALLBACK_LIST;
119
120 static pthread_mutex_t _event_available_lock = PTHREAD_MUTEX_INITIALIZER;
121 static pthread_cond_t  _event_available_signal = PTHREAD_COND_INITIALIZER;
122 static pthread_mutex_t *_event_callback_table_lock = NULL;
123 static pthread_mutex_t *_event_queue_lock = NULL;
124 static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM];            /*  array of singly-linked list for event callbacks */
125
126 void *thread_func_branch_command(void *arg);
127
128 static email_event_t g_event_que[EVENT_QUEUE_MAX];
129
130 int send_thread_run = 0;
131 int recv_thread_run = 0;
132
133 static pthread_mutex_t _send_event_available_lock = PTHREAD_MUTEX_INITIALIZER;
134 static pthread_mutex_t *_send_event_queue_lock = NULL;  /*  MUST BE "recursive" */
135 static pthread_cond_t  _send_event_available_signal = PTHREAD_COND_INITIALIZER;
136 static thread_t g_send_srv_thread;
137 static thread_t g_srv_thread;
138
139 static email_event_t g_send_event_que[EVENT_QUEUE_MAX];
140 static int g_send_event_que_idx = 1;
141 static int g_send_event_loop = 1;
142 static int g_send_active_que = 0;
143 static int g_event_que_idx = 1;
144 static int g_event_loop = 1;
145 static int g_active_que = 0;
146 static int g_sending_busy_cnt = 0;
147 static int g_receiving_busy_cnt = 0;
148
149
150 INTERNAL_FUNC int emcore_get_current_thread_type()
151 {
152         EM_DEBUG_FUNC_BEGIN();
153         thread_t thread_id = THREAD_SELF();
154         int thread_type = -1;
155
156         if (thread_id == g_srv_thread)
157                 thread_type = _SERVICE_THREAD_TYPE_RECEIVING;
158         else if (thread_id == g_send_srv_thread)
159                 thread_type = _SERVICE_THREAD_TYPE_SENDING;
160 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
161         else if (thread_id == g_partial_body_thd)
162                 thread_type = _SERVICE_THREAD_TYPE_PBD;
163 #endif
164         EM_DEBUG_FUNC_END("thread_type [%d]", thread_type);
165         return thread_type;
166 }
167
168 static int is_gdk_lock_needed()
169 {
170         if (g_event_loop)  {
171                 return (THREAD_SELF() == g_srv_thread);
172         }
173         return false;
174 }
175
176 INTERNAL_FUNC int emcore_get_pending_event(email_action_t action, int account_id, int mail_id, email_event_status_type_t *status)
177 {
178         EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
179
180         int found = false;
181         int i;
182
183         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
184
185         for (i = 1; i < EVENT_QUEUE_MAX; i++)  {
186                 switch (g_event_que[i].type)  {
187                         case EMAIL_EVENT_SEND_MAIL:
188                         case EMAIL_EVENT_SEND_MAIL_SAVED:
189                                 if (action == EMAIL_ACTION_SEND_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
190                                         found = true;
191                                         goto EXIT;
192                                 }
193                                 break;
194
195                         case EMAIL_EVENT_SYNC_HEADER:
196                                 if (action == EMAIL_ACTION_SYNC_HEADER && account_id == g_event_que[i].account_id) {
197                                         found = true;
198                                         goto EXIT;
199                                 }
200                                 break;
201
202                         case EMAIL_EVENT_SYNC_HEADER_OMA:
203                                 if (action == EMAIL_ACTION_SYNC_HEADER_OMA && account_id == g_event_que[i].account_id) {
204                                         found = true;
205                                         goto EXIT;
206                                 }
207                                 break;
208
209                         case EMAIL_EVENT_DOWNLOAD_BODY:
210                                 if (action == EMAIL_ACTION_DOWNLOAD_BODY && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
211                                         found = true;
212                                         goto EXIT;
213                                 }
214                                 break;
215                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
216                                 if (action == EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
217                                         found = true;
218                                         goto EXIT;
219                                 }
220                                 break;
221                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
222                                 if (action == EMAIL_ACTION_SYNC_FLAGS_FIELD_TO_SERVER && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
223                                         found = true;
224                                         goto EXIT;
225                                 }
226                                 break;
227                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
228                                 if (action == EMAIL_ACTION_DOWNLOAD_ATTACHMENT && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
229                                         found = true;
230                                         goto EXIT;
231                                 }
232                                 break;
233                         case EMAIL_EVENT_DELETE_MAIL:
234                         case EMAIL_EVENT_DELETE_MAIL_ALL:
235                                 if (action == EMAIL_ACTION_DELETE_MAIL && account_id == g_event_que[i].account_id) {
236                                         found = true;
237                                         goto EXIT;
238                                 }
239                                 break;
240
241                         case EMAIL_EVENT_CREATE_MAILBOX:
242                                 if (action == EMAIL_ACTION_CREATE_MAILBOX && account_id == g_event_que[i].account_id) {
243                                         found = true;
244                                         goto EXIT;
245                                 }
246                                 break;
247
248                         case EMAIL_EVENT_DELETE_MAILBOX:
249                                 if (action == EMAIL_ACTION_DELETE_MAILBOX && account_id == g_event_que[i].account_id) {
250                                         found = true;
251                                         goto EXIT;
252                                 }
253                                 break;
254
255                         case EMAIL_EVENT_MOVE_MAIL:
256                                 if (action == EMAIL_ACTION_MOVE_MAIL && account_id == g_event_que[i].account_id && mail_id == g_event_que[i].event_param_data_4) {
257                                         found = true;
258                                         goto EXIT;
259                                 }
260                                 break;
261
262                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
263                                 if (action == EMAIL_ACTION_VALIDATE_ACCOUNT && account_id == g_event_que[i].account_id) {
264                                         found = true;
265                                         goto EXIT;
266                                 }
267                                 break;
268
269                         case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
270                                 if (action == EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT && account_id == 0) {
271                                         found = true;
272                                         goto EXIT;
273                                 }
274                                 break;
275
276                         case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
277                                 if (action == EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT && account_id == 0) {
278                                         found = true;
279                                         goto EXIT;
280                                 }
281                                 break;
282
283                         case EMAIL_EVENT_UPDATE_MAIL:
284                                 if (action == EMAIL_ACTION_UPDATE_MAIL)  {
285                                         found = true;
286                                         goto EXIT;
287                                 }
288                                 break;
289
290                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
291                                 if (action == EMAIL_ACTION_SET_MAIL_SLOT_SIZE)  {
292                                         found = true;
293                                         goto EXIT;
294                                 }
295                                 break;
296
297                         case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
298                                 if (action == EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED)  {
299                                         found = true;
300                                         goto EXIT;
301                                 }
302                                 break;
303
304                         case EMAIL_EVENT_SEARCH_ON_SERVER:
305                                 if (action == EMAIL_ACTION_SEARCH_ON_SERVER && account_id == g_event_que[i].account_id) {
306                                         found = true;
307                                         goto EXIT;
308                                 }
309                                 break;
310
311                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
312                                 if (action == EMAIL_ACTION_MOVE_MAILBOX && account_id == g_event_que[i].account_id) {
313                                         found = true;
314                                         goto EXIT;
315                                 }
316                                 break;
317
318                         default:
319                                 break;
320                 }
321         }
322
323 EXIT:
324         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
325
326         if (found) {
327                 if (status)
328                         *status = g_event_que[i].status;
329
330                 return i;
331         }
332
333         return FAILURE;
334 }
335
336 INTERNAL_FUNC void emcore_get_event_queue_status(int *on_sending, int *on_receiving)
337 {
338         if (on_sending != NULL)
339                 *on_sending = g_sending_busy_cnt;
340
341         if (on_receiving != NULL)
342                 *on_receiving = g_receiving_busy_cnt;
343 }
344
345 static void _sending_busy_ref(void)
346 {
347         g_sending_busy_cnt++;
348 }
349
350 static void _sending_busy_unref(void)
351 {
352         g_sending_busy_cnt--;
353 }
354
355 static void _receiving_busy_ref(void)
356 {
357         g_receiving_busy_cnt++;
358 }
359
360 static void _receiving_busy_unref(void)
361 {
362         g_receiving_busy_cnt--;
363 }
364
365 static void waiting_status_notify(email_event_t *event_data, int queue_idx)
366 {
367         EM_DEBUG_FUNC_BEGIN("event_data[%p], queue_idx[%d]", event_data, queue_idx);
368
369         int account_id = event_data->account_id;
370         int mail_id = event_data->event_param_data_4;           /*  NOT ALWAYS */
371
372         switch (event_data->type)  {
373                 case EMAIL_EVENT_SEND_MAIL:
374                         emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
375                         break;
376
377                 case EMAIL_EVENT_SYNC_HEADER:
378                         emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
379                         break;
380
381                 case EMAIL_EVENT_SYNC_HEADER_OMA:
382                         emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER_OMA, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
383                         break;
384
385                 case EMAIL_EVENT_DOWNLOAD_BODY:
386                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
387                         break;
388
389 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
390                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
391                         emcore_execute_event_callback(EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
392                         break;
393 #endif
394
395                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
396                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
397                         break;
398
399                 case EMAIL_EVENT_DELETE_MAIL:
400                 case EMAIL_EVENT_DELETE_MAIL_ALL:
401                         emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
402                         break;
403
404                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
405                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
406                         break;
407
408                 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
409                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
410                         break;
411
412                 case EMAIL_EVENT_MOVE_MAIL:
413                         emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAIL, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
414                         break;
415
416                 case EMAIL_EVENT_CREATE_MAILBOX:
417                         emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
418                         break;
419
420                 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
421                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
422                         break;
423
424                 case EMAIL_EVENT_UPDATE_MAIL:
425                         break;
426
427                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
428                         emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
429                         break;
430
431                 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
432                         emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_WAITING, account_id, event_data->event_param_data_4, queue_idx, EMAIL_ERROR_NONE);
433                         break;
434
435                 case EMAIL_EVENT_SEARCH_ON_SERVER:
436                         emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
437                         break;
438
439                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
440                         /* emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE); */
441                         break;
442
443                 default:
444                         break;
445         }
446         EM_DEBUG_FUNC_END();
447 }
448
449 static void fail_status_notify(email_event_t *event_data, int error)
450 {
451         EM_DEBUG_FUNC_BEGIN();
452         int account_id, mail_id;
453
454         if(!event_data) {
455                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
456                 return;
457         }
458         account_id = event_data->account_id;
459         mail_id  = event_data->event_param_data_4;
460
461         EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);
462
463         switch (event_data->type)  {
464                 case EMAIL_EVENT_SEND_MAIL:
465                         /* case EMAIL_EVENT_SEND_MAIL_SAVED:  */
466                         /* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
467                         emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, error);
468                         break;
469
470                 case EMAIL_EVENT_SYNC_HEADER:
471                         emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
472                         emcore_show_user_message(account_id, EMAIL_ACTION_SYNC_HEADER, error);
473                         break;
474
475                 case EMAIL_EVENT_DOWNLOAD_BODY:
476                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
477                         emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
478                         break;
479
480                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
481                         emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
482                         emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
483                         break;
484
485                 case EMAIL_EVENT_DELETE_MAIL:
486                 case EMAIL_EVENT_DELETE_MAIL_ALL:
487                         emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
488                         emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, error);
489                         break;
490
491                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
492                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
493                         emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
494                         break;
495
496                 case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
497                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
498                         emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
499                         break;
500
501                 case EMAIL_EVENT_CREATE_MAILBOX:
502                         emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
503                         emcore_show_user_message(account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
504                         break;
505
506                 case EMAIL_EVENT_DELETE_MAILBOX:
507                         emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
508                         emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
509                         break;
510
511                 case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
512                         emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
513                         emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
514                         break;
515
516                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
517                         emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
518                         break;
519
520                 case EMAIL_EVENT_SEARCH_ON_SERVER:
521                         emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
522                         emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
523                         break;
524
525                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
526                         emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
527                         emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
528                         break;
529
530                 case EMAIL_EVENT_UPDATE_MAIL:
531                         emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
532                         break;
533
534                 case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
535                         emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL, account_id, event_data->event_param_data_4, -1, EMAIL_ERROR_NONE);
536                         break;
537
538                 default:
539                         break;
540         }
541         EM_DEBUG_FUNC_END();
542 }
543
544
545 static void emcore_initialize_event_callback_table()
546 {
547         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
548
549         int i;
550
551         for (i = 0; i < EMAIL_ACTION_NUM; i++)
552                 _event_callback_table[i] = NULL;
553
554         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
555 }
556
557 int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
558 {
559         EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);
560
561         if (callback == NULL)
562                 return false;
563
564         int ret = false;
565
566         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
567
568         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
569
570         while (node != NULL) {
571                 if (node->callback == callback && node->event_data == event_data)               /*  already registered */
572                         goto EXIT;
573
574                 node = node->next;
575         }
576
577         /*  not found, so keep going */
578
579         node = em_malloc(sizeof(EVENT_CALLBACK_LIST));
580
581         if (node == NULL)               /*  not enough memory */
582                 goto EXIT;
583
584         node->callback = callback;
585         node->event_data = event_data;
586         node->next = _event_callback_table[action];
587
588         _event_callback_table[action] = node;
589
590         ret = true;
591
592 EXIT :
593         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
594         EM_DEBUG_FUNC_END();
595         return ret;
596 }
597
598 int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
599 {
600         EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);
601
602         if (callback == NULL)
603                 return false;
604
605         int ret = false;
606
607         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
608
609         EVENT_CALLBACK_LIST *prev = NULL;
610         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
611
612         while (node != NULL) {
613                 if (node->callback == callback) {
614                         if (prev != NULL)
615                                 prev->next = node->next;
616                         else
617                                 _event_callback_table[action] = node->next;
618
619                         free(node);
620
621                         ret = true;
622                         break;
623                 }
624
625                 prev = node;
626                 node = node->next;
627         }
628
629         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
630         EM_DEBUG_FUNC_END();
631         return ret;
632 }
633
634 void emcore_execute_event_callback(email_action_t action, int total, int done, int status, int account_id, int mail_id, int handle, int error)
635 {
636         EM_DEBUG_FUNC_BEGIN("action[%d], total[%d], done[%d], status[%d], account_id[%d], mail_id[%d], handle[%d], error[%d]", action, total, done, status, account_id, mail_id, handle, error);
637
638         int lock_needed = 0;
639         lock_needed = is_gdk_lock_needed();
640
641         if (lock_needed)  {
642                 /*  Todo  :  g_thread_yield */
643         }
644
645         ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
646
647         EVENT_CALLBACK_LIST *node = _event_callback_table[action];
648
649         while (node != NULL)  {
650                 if (node->callback != NULL)
651                         node->callback(total, done, status, account_id, mail_id, (handle == -1 ? emcore_get_active_queue_idx()  :  handle), node->event_data, error);
652                 node = node->next;
653         }
654
655         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
656
657         if (lock_needed)  {
658         }
659         EM_DEBUG_FUNC_END();
660 }
661
662 /* insert a event to event queue */
663 INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
664 {
665         EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
666
667         if (!event_data)  {
668                 EM_DEBUG_EXCEPTION("Invalid Parameter");
669                 if (err_code != NULL)
670                         *err_code = EMAIL_ERROR_INVALID_PARAM;
671                 return false;
672         }
673
674         if (!g_srv_thread)  {
675                 EM_DEBUG_EXCEPTION("email-service is not ready");
676                 if (err_code != NULL)
677                         *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
678                 return false;
679         }
680
681         int ret = true;
682         int error = EMAIL_ERROR_NONE;
683
684         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
685
686         if (!g_event_que[g_event_que_idx].type)  {      /*  if current buffer has not event, insert event data to current buffer  */
687                 EM_DEBUG_LOG("Current buffer has not a event. [%d]", g_event_que_idx);
688                 memcpy(g_event_que+g_event_que_idx, event_data, sizeof(email_event_t));
689                 g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
690                 waiting_status_notify(event_data, g_event_que_idx);
691                 if (handle)
692                         *handle = g_event_que_idx;
693         }
694         else  { /*  if current buffer has event, find the empty buffer */
695                 EM_DEBUG_LOG("Current buffer has a event. [%d]", g_event_que_idx);
696                 int i, j = g_event_que_idx + 1;
697
698                 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++)  {
699                         if (j >= EVENT_QUEUE_MAX)
700                                 j = 1;
701
702                         if (!g_event_que[j].type)
703                                         break;
704                 }
705
706                 if (i < EVENT_QUEUE_MAX)  {
707                         EM_DEBUG_LOG("I found available buffer. [%d]", g_event_que + j);
708                         memcpy(g_event_que+j, event_data, sizeof(email_event_t));
709                         g_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
710                         waiting_status_notify(event_data, j);
711
712                         if (handle)
713                                 *handle = j;
714                 }
715                 else  {
716                         EM_DEBUG_EXCEPTION("event que is full...");
717                         error = EMAIL_ERROR_EVENT_QUEUE_FULL;
718                         ret = false;
719                 }
720         }
721
722         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
723
724         if (ret == true)  {
725                 event_data->event_param_data_1 = NULL;          /*  MUST BE - to prevent double-free */
726
727                 switch (event_data->type)  {
728                         case EMAIL_EVENT_SEND_MAIL:
729                         case EMAIL_EVENT_SEND_MAIL_SAVED:
730                                 _sending_busy_ref();
731                                 break;
732
733                         case EMAIL_EVENT_SYNC_HEADER:
734                         case EMAIL_EVENT_DOWNLOAD_BODY:
735                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
736                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
737                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
738                         case EMAIL_EVENT_ISSUE_IDLE:
739                         case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
740                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
741                         case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
742                         case EMAIL_EVENT_SAVE_MAIL:
743                         case EMAIL_EVENT_MOVE_MAIL:
744                         case EMAIL_EVENT_DELETE_MAIL:
745                         case EMAIL_EVENT_DELETE_MAIL_ALL:
746                         case EMAIL_EVENT_SYNC_HEADER_OMA:
747                         case EMAIL_EVENT_CREATE_MAILBOX:
748                         case EMAIL_EVENT_DELETE_MAILBOX:
749                         case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
750                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
751                         case EMAIL_EVENT_UPDATE_MAIL:
752                         case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
753                         case EMAIL_EVENT_SEARCH_ON_SERVER:
754                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
755                                 _receiving_busy_ref();
756                                 break;
757                         default:
758                                 break;
759                 }
760
761                 ENTER_CRITICAL_SECTION(_event_available_lock);
762                 WAKE_CONDITION_VARIABLE(_event_available_signal);
763                 LEAVE_CRITICAL_SECTION(_event_available_lock);
764         }
765
766 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
767         {
768                 int is_local_activity_event_inserted = false;
769
770                 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &error))
771                         EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
772                 else {
773                         if (true == is_local_activity_event_inserted)
774                                 emcore_pb_thd_set_local_activity_continue(false);
775                 }
776         }
777 #endif
778
779         if (err_code != NULL)
780                 *err_code = error;
781
782         EM_DEBUG_LOG("Finish with [%d]", ret);
783         return ret;
784 }
785
786 /* get a event from event_data queue */
787 static int emcore_retrieve_event(email_event_t *event_data, int *err_code)
788 {
789         EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
790
791         int ret = false;
792         int error = EMAIL_ERROR_NONE;
793
794         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
795
796         /*  get a event_data if this queue is not empty */
797         if (g_event_que[g_event_que_idx].type)  {
798                 memcpy(event_data, g_event_que+g_event_que_idx, sizeof(email_event_t));
799
800                 if (event_data->status != EMAIL_EVENT_STATUS_WAIT)  {   /*  EMAIL_EVENT_STATUS_CANCELED */
801                         memset(g_event_que+g_event_que_idx, 0x00, sizeof(email_event_t));
802                         g_active_que = 0;
803                 }
804                 else  {
805                         EM_DEBUG_LINE;
806                         g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
807                         g_active_que = g_event_que_idx;
808                         ret = true;
809                 }
810
811                 if (++g_event_que_idx >= EVENT_QUEUE_MAX)
812                         g_event_que_idx = 1;
813
814                 EM_DEBUG_LOG("g_event_que[%d].type [%d]", g_active_que, g_event_que[g_active_que].type);
815         }
816         else  {
817                 g_active_que = 0;
818                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
819         }
820
821         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
822
823         if (err_code != NULL)
824                 *err_code = error;
825
826         EM_DEBUG_FUNC_END("ret [%d]", ret);
827         return ret;
828 }
829
830 /* check that event_data loop is continuous */
831 static int emcore_event_loop_continue()
832 {
833     return g_event_loop;
834 }
835
836
837
838 INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
839 {
840         EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);
841
842         if (!event_data)  {
843                 EM_DEBUG_EXCEPTION("\t event_data[%p], handle[%p]", event_data, handle);
844
845                 if (err_code != NULL)
846                         *err_code = EMAIL_ERROR_INVALID_PARAM;
847                 return false;
848         }
849
850         int ret = true;
851         int error = EMAIL_ERROR_NONE;
852
853         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
854
855         if (!g_send_event_que[g_send_event_que_idx].type)  {
856                 /* if current buffer has not event_data, insert event_data data to current buffer */
857                 EM_DEBUG_LOG("Current buffer has not a event_data. [%d]", g_send_event_que_idx);
858                 memcpy(g_send_event_que+g_send_event_que_idx, event_data, sizeof(email_event_t));
859
860                 g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
861
862                 if (handle)
863                         *handle = g_send_event_que_idx;
864         }
865         else  {
866                 /* if current buffer has event_data, find the empty buffer */
867                 EM_DEBUG_LOG("Current buffer has a event_data. [%d]", g_send_event_que_idx);
868                 int i, j = g_send_event_que_idx + 1;
869
870                 for (i = 1; i < EVENT_QUEUE_MAX; i++, j++)  {
871                         if (j >= EVENT_QUEUE_MAX)
872                                 j = 1;
873
874                         if (!g_send_event_que[j].type)
875                                         break;
876                         }
877
878                 if (i < EVENT_QUEUE_MAX)  {
879                         EM_DEBUG_LOG("I found available buffer. [%d]", j);
880                         memcpy(g_send_event_que+j, event_data, sizeof(email_event_t));
881                         g_send_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
882                         if (handle) *handle = j;
883                 }
884                 else  {
885                         EM_DEBUG_EXCEPTION("event_data queue is full...");
886                         error = EMAIL_ERROR_EVENT_QUEUE_FULL;
887                         ret = false;
888                 }
889         }
890
891         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
892
893         if (ret == true)  {
894                 ENTER_CRITICAL_SECTION(_send_event_available_lock);
895                 WAKE_CONDITION_VARIABLE(_send_event_available_signal);
896                 LEAVE_CRITICAL_SECTION(_send_event_available_lock);
897         }
898
899         if (handle)
900         EM_DEBUG_LOG("emcore_insert_event_for_sending_mails-handle[%d]", *handle);
901
902         if (err_code != NULL)
903                 *err_code = error;
904
905         /* EM_DEBUG_FUNC_BEGIN(); */
906         return ret;
907 }
908
909
910 static int emcore_retrieve_send_event(email_event_t *event_data, int *err_code)
911 {
912         EM_DEBUG_FUNC_BEGIN();
913
914         int ret = false;
915         int error = EMAIL_ERROR_NONE;
916
917         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
918 /* get a event_data if this queue is not empty */
919         if (g_send_event_que[g_send_event_que_idx].type)  {
920                 memcpy(event_data, g_send_event_que+g_send_event_que_idx, sizeof(email_event_t));
921
922                 if (event_data->status != EMAIL_EVENT_STATUS_WAIT) {
923                         memset(g_send_event_que+g_send_event_que_idx, 0x00, sizeof(email_event_t));
924                         g_send_active_que = 0;
925                 }
926                 else  {
927                         g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
928                         EM_DEBUG_LOG("g_send_event_que_idx[%d]", g_send_event_que_idx);
929                         g_send_active_que = g_send_event_que_idx;
930
931                         ret = true;
932                 }
933
934                 if (++g_send_event_que_idx >= EVENT_QUEUE_MAX)
935                         g_send_event_que_idx = 1;
936
937                         EM_DEBUG_LOG("\t g_send_event_que[%d].type = %d", g_send_active_que, g_send_event_que[g_send_active_que].type);
938         }
939         else  {
940                         EM_DEBUG_LOG("\t send event_data queue is empty...");
941                         g_send_active_que = 0;
942                         error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
943         }
944
945         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
946
947         if (err_code != NULL)
948                 *err_code = error;
949
950         return ret;
951 }
952
953
954 void* thread_func_branch_command_for_sending_mails(void *arg)
955 {
956         EM_DEBUG_FUNC_BEGIN();
957         int err = EMAIL_ERROR_NONE;
958         email_event_t event_data;
959         email_session_t *session = NULL;
960
961         if (!emstorage_open(&err))  {
962                 EM_DEBUG_EXCEPTION("\t emstorage_open falied - %d", err);
963                 return NULL;
964         }
965
966         while (g_send_event_loop) {
967                 if (!emcore_get_empty_session(&session))
968                         EM_DEBUG_EXCEPTION("\t SEND THREAD emcore_get_empty_session failed...");
969
970                 if (!emcore_retrieve_send_event(&event_data, NULL))  {
971                         EM_DEBUG_LOG(">>>> waiting for send event_data>>>>>>>>>");
972 #ifdef __FEATURE_LOCAL_ACTIVITY__
973                         if (send_thread_run && g_save_local_activity_run) {
974                                 emstorage_account_tbl_t *account_list = NULL;
975                                 int count = 0, i;
976                                 if (!emstorage_get_account_list(&count, &account_list, true, true, &err)) {
977                                         EM_DEBUG_LOG("\t emstorage_get_account_list failed - %d", err);
978                                 }
979                                 else {
980                                         for (i = 0; i < count; i++) {
981                                                 if (emcore_save_local_activity_sync(account_list[i].account_id, &err)) {
982                                                         EM_DEBUG_LOG("Found local activity...!");
983                                                         EM_DEBUG_LOG("Resetting g_save_local_activity_run ");
984                                                         g_save_local_activity_run = 0;
985                                                         emcore_clear_session(session);
986                                                 }
987                                         }
988
989                                         emstorage_free_account(&account_list, count, &err);
990
991                                         if (!g_save_local_activity_run) {
992                                                 continue;
993                                         }
994                                 }
995                         }
996 #endif
997                         send_thread_run = 0;
998
999                         ENTER_CRITICAL_SECTION(_send_event_available_lock);
1000                         SLEEP_CONDITION_VARIABLE(_send_event_available_signal, _send_event_available_lock);
1001                         LEAVE_CRITICAL_SECTION(_send_event_available_lock);
1002                 }
1003                 else {
1004                         EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
1005                         send_thread_run = 1;
1006                         g_client_run = 1;
1007
1008                         if (!emnetwork_check_network_status( &err))  {
1009                                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1010
1011                                 emcore_show_user_message(event_data.event_param_data_4, EMAIL_ACTION_SEND_MAIL, err);
1012                                 if (!emcore_notify_network_event(NOTI_SEND_FAIL, event_data.account_id, NULL , event_data.event_param_data_4, err))
1013                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_FAIL] Failed >>>> ");
1014                                 goto FINISH_OFF;
1015                         }
1016
1017                         switch (event_data.type)  {
1018
1019                                 case EMAIL_EVENT_SEND_MAIL:
1020                                         emdevice_set_dimming_on_off(false, NULL);
1021
1022                                         if (!emcore_send_mail(event_data.account_id, event_data.event_param_data_5, event_data.event_param_data_4, &err))
1023                                                 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
1024
1025                                         emdevice_set_dimming_on_off(true, NULL);
1026                                         break;
1027
1028                                 case EMAIL_EVENT_SEND_MAIL_SAVED:  /* send mails to been saved in off-line mode */
1029                                         emdevice_set_dimming_on_off(false, NULL);
1030
1031                                         if (!emcore_send_saved_mail(event_data.account_id, event_data.event_param_data_3, &err))
1032                                                 EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);
1033
1034                                         emdevice_set_dimming_on_off(true, NULL);
1035                                         break;
1036
1037 #ifdef __FEATURE_LOCAL_ACTIVITY__
1038
1039                                 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1040                                         emdevice_set_dimming_on_off(false, NULL);
1041                                         emstorage_activity_tbl_t *local_activity = NULL;
1042                                         int activity_id_count = 0;
1043                                         int activity_chunk_count = 0;
1044                                         int *activity_id_list = NULL;
1045                                         int i = 0;
1046
1047                                         if (false == emstorage_get_activity_id_list(event_data.account_id, &activity_id_list, &activity_id_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1048                                                 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1049                                         }
1050                                         else {
1051                                                 for (i = 0; i < activity_id_count; ++i) {
1052                                                         if ((false == emstorage_get_activity(event_data.account_id, activity_id_list[i], &local_activity, &activity_chunk_count, true,  &err)) || (NULL == local_activity) || (0 == activity_chunk_count)) {
1053                                                                 EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
1054                                                         }
1055                                                         else {
1056                                                                 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1057                                                                 switch (local_activity[0].activity_type) {
1058                                                                         case ACTIVITY_SAVEMAIL:  {
1059                                                                                 if (!emcore_sync_mail_from_client_to_server(event_data.account_id, local_activity[0].mail_id, &err)) {
1060                                                                                         EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
1061                                                                                 }
1062                                                                         }
1063                                                                         break;
1064
1065                                                                         case ACTIVITY_DELETEMAIL_SEND:                          /* New Activity Type Added for Race Condition and Crash Fix */ {
1066                                                                                 if (!emcore_delete_mail(local_activity[0].account_id,
1067                                                                                                                                 &local_activity[0].mail_id,
1068                                                                                                                                 EMAIL_DELETE_FOR_SEND_THREAD,
1069                                                                                                                                 true,
1070                                                                                                                                 EMAIL_DELETED_BY_COMMAND,
1071                                                                                                                                 false,
1072                                                                                                                                 &err))  {
1073                                                                                         EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1074                                                                                 }
1075                                                                         }
1076                                                                         break;
1077
1078                                                                         default:  {
1079                                                                                 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1080                                                                         }
1081                                                                         break;
1082                                                                 }
1083
1084                                                                 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1085
1086                                                                 if (g_save_local_activity_run == 1) {
1087                                                                         EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1088                                                                         break;
1089                                                                 }
1090                                                         }
1091
1092                                                 }
1093                                                 if (false == emstorage_free_activity_id_list(activity_id_list, &err)) {
1094                                                         EM_DEBUG_LOG("emstorage_free_activity_id_list failed");
1095                                                 }
1096                                         }
1097
1098                                         emdevice_set_dimming_on_off(true, NULL);
1099                                 }
1100                                 break;
1101 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1102                                 default:
1103                                         EM_DEBUG_LOG("Others not supported by Send Thread..! [%d]", event_data.type);
1104                                         break;
1105                         }
1106
1107                         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1108                         memset(g_send_event_que+g_send_active_que, 0x00, sizeof(email_event_t));
1109                         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1110
1111 FINISH_OFF:
1112                         ;
1113                 }
1114                 emcore_clear_session(session);
1115         }
1116
1117         if (!emstorage_close(&err))
1118                 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
1119
1120         EM_DEBUG_FUNC_END("err [%d]", err);
1121         return NULL;
1122 }
1123
1124
1125 int event_handler_EMAIL_EVENT_SYNC_HEADER(int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error)
1126 {
1127         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", input_account_id, input_mailbox_id, handle_to_be_published, error);
1128
1129         int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
1130         int mailbox_count = 0, account_count = 0;
1131         int counter, account_index;
1132         int unread = 0, total_unread = 0;
1133         emcore_uid_list *uid_list = NULL;
1134         emstorage_account_tbl_t *account_tbl_array = NULL;
1135         emstorage_mailbox_tbl_t *mailbox_tbl_target = NULL, *mailbox_tbl_spam = NULL, *mailbox_tbl_list = NULL;
1136 #ifndef __FEATURE_KEEP_CONNECTION__
1137         MAILSTREAM *stream = NULL;
1138 #endif
1139         char mailbox_id_param_string[10] = {0,};
1140         char *input_mailbox_id_str = NULL;
1141
1142         if (input_mailbox_id == 0)
1143                 sync_type = EMAIL_SYNC_ALL_MAILBOX;
1144         else {
1145                 if (!emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
1146                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1147                         goto FINISH_OFF;
1148                 }
1149         }
1150         if(mailbox_tbl_target)
1151                 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_target->mailbox_id);
1152
1153         input_mailbox_id_str = (input_mailbox_id == 0)? NULL: mailbox_id_param_string;
1154
1155         if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, input_mailbox_id_str, handle_to_be_published, 0))
1156                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
1157
1158         if (!emnetwork_check_network_status(&err)) {
1159                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1160
1161                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
1162                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1163         }
1164         else {
1165                 if (sync_type != EMAIL_SYNC_ALL_MAILBOX) {      /* Sync only particular mailbox */
1166
1167                         if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1168                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1169
1170                         if (!emstorage_get_mailbox_by_mailbox_type(input_account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1171                                 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1172                         }
1173
1174                         if (!emcore_sync_header(mailbox_tbl_target, mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1175                                 EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
1176                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
1177                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1178                         }
1179                         else {
1180                                 EM_DEBUG_LOG("emcore_sync_header succeeded [%d]", err);
1181                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
1182                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1183                         }
1184
1185                         total_unread += unread;
1186
1187                         if (total_unread > 0 && (err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1188                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1189
1190                         if (!emcore_finalize_sync(input_account_id, &err))
1191                                 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1192                 }
1193                 else /*  All Foder */ {
1194                         EM_DEBUG_LOG(">>>> SYNC ALL MAILBOX ");
1195                         /*  Sync of all mailbox */
1196
1197                         if (input_account_id == ALL_ACCOUNT) {
1198                                 if ((err = emcore_update_sync_status_of_account(ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1199                                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1200
1201                                 if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &err)) {
1202                                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
1203                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL,  handle_to_be_published, err))
1204                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1205                                         goto FINISH_OFF;
1206                                 }
1207                         }
1208                         else {
1209                                 EM_DEBUG_LOG("Sync all mailbox of an account[%d].", input_account_id);
1210
1211                                 if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1212                                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1213
1214                                 if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
1215                                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
1216                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
1217                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1218                                         goto FINISH_OFF;
1219                                 }
1220                                 account_count = 1;
1221                         }
1222
1223                         for (account_index = 0 ; account_index < account_count; account_index++) {
1224                                 if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1225                                         EM_DEBUG_LOG("account[%d] is for ActiveSync. Skip  ", account_index);
1226                                         continue;
1227                                 }
1228
1229                                 if (!emstorage_get_mailbox_list(account_tbl_array[account_index].account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || mailbox_count <= 0) {
1230                                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
1231
1232                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, input_mailbox_id_str, handle_to_be_published, err))
1233                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1234
1235                                         continue;
1236                                 }
1237
1238                                 EM_DEBUG_LOG("emcore_get_mailbox_list_to_be_sync returns [%d] mailboxes", mailbox_count);
1239
1240                                 if(mailbox_tbl_spam) {
1241                                         if (!emstorage_free_mailbox(&mailbox_tbl_spam, 1, &err)) {
1242                                                 EM_DEBUG_EXCEPTION("emstorage_free_mailbox failed [%d]", err);
1243                                         }
1244                                         mailbox_tbl_spam = NULL;
1245                                 }
1246
1247                                 if (!emstorage_get_mailbox_by_mailbox_type(account_tbl_array[account_index].account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &mailbox_tbl_spam, false, &err)) {
1248                                         EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1249                                 }
1250
1251                                 if (mailbox_count > 0) {
1252 #ifndef __FEATURE_KEEP_CONNECTION__
1253                                         if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1254                                                 memset(mailbox_id_param_string, 0, 10);
1255                                                 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
1256                                                 if (!emcore_connect_to_remote_mailbox(account_tbl_array[account_index].account_id, mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err))  {
1257                                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1258                                                         if (err == EMAIL_ERROR_LOGIN_FAILURE)
1259                                                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
1260                                                         /* continue; */
1261                                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string,  handle_to_be_published, err))
1262                                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1263                                                         continue;
1264                                                 }
1265                                                 EM_DEBUG_LOG("emcore_connect_to_remote_mailbox returns [%d] : ", err);
1266                                         }
1267                                         else
1268                                                 stream = NULL;
1269 #endif
1270                                 }
1271
1272                                 for (counter = 0; counter < mailbox_count; counter++) {
1273
1274                                         EM_DEBUG_LOG("maiblox_name [%s], mailbox_id [%d], mailbox_type [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, mailbox_tbl_list[counter].mailbox_type);
1275
1276                                         if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
1277                                                         || mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
1278                                                 /*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */)
1279                                                 EM_DEBUG_LOG("Skipped for all emails or trash");
1280                                         else if (!mailbox_tbl_list[counter].local_yn) {
1281                                                 EM_DEBUG_LOG("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
1282 #ifdef __FEATURE_KEEP_CONNECTION__
1283                                                 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, NULL, &uid_list, &unread, &err)) {
1284 #else /*  __FEATURE_KEEP_CONNECTION__ */
1285                                                 if (!emcore_sync_header((mailbox_tbl_list + counter) , mailbox_tbl_spam, (void *)stream, &uid_list, &unread, &err)) {
1286 #endif /*  __FEATURE_KEEP_CONNECTION__ */
1287                                                         EM_DEBUG_EXCEPTION("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, err);
1288
1289 #ifndef __FEATURE_KEEP_CONNECTION__
1290                                                         if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || err == EMAIL_ERROR_SOCKET_FAILURE)
1291                                                                 stream = NULL;    /*  Don't retry to connect for broken connection. It might cause crash.  */
1292 #endif /*  __FEATURE_KEEP_CONNECTION__ */
1293                                                         memset(mailbox_id_param_string, 0, 10);
1294                                                         SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
1295                                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string,  handle_to_be_published, err))
1296                                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
1297
1298                                                         break;
1299                                                 }
1300                                         }
1301                                         total_unread  += unread;
1302                                 }
1303
1304                                 EM_DEBUG_LOG("Sync for account_id(%d) is completed....!", account_tbl_array[account_index].account_id);
1305                                 if ((err == EMAIL_ERROR_NONE) && !emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
1306                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");
1307
1308                                 if ((total_unread > 0) && (err = emcore_update_sync_status_of_account(account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1309                                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1310
1311                                 if (!emcore_finalize_sync(account_tbl_array[account_index].account_id, &err))
1312                                         EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
1313 #ifndef __FEATURE_KEEP_CONNECTION__
1314                                 if (stream)  {
1315                                         emcore_close_mailbox(0, stream);
1316                                         stream = NULL;
1317                                 }
1318 #endif
1319                                 if (mailbox_tbl_list) {
1320                                         emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1321                                         mailbox_tbl_list = NULL;
1322                                         mailbox_count = 0;
1323                                 }
1324                         }
1325                 }
1326
1327                 ret = true;
1328
1329 FINISH_OFF:
1330
1331 #ifndef __FEATURE_KEEP_CONNECTION__
1332                 if (stream)
1333                         emcore_close_mailbox(0, stream);
1334 #endif
1335                 if(mailbox_tbl_target)
1336                         emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);
1337
1338                 if (mailbox_tbl_list)
1339                         emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
1340
1341                 if (account_tbl_array)
1342                         emstorage_free_account(&account_tbl_array, account_count, NULL);
1343         }
1344
1345         EM_DEBUG_FUNC_END();
1346         return ret;
1347 }
1348
1349 int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error)
1350 {
1351         EM_DEBUG_FUNC_BEGIN("account [%p]", account);
1352         int err, ret = false;
1353
1354         if(!account) {
1355                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1356                 err = EMAIL_ERROR_INVALID_PARAM;
1357                 goto FINISH_OFF;
1358         }
1359
1360         EM_DEBUG_LOG("incoming_server_address  :  %s", account->incoming_server_address);
1361
1362         if (!emnetwork_check_network_status(&err)) {
1363                 emcore_delete_account_from_unvalidated_account_list(account->account_id);
1364                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1365                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
1366                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1367                 goto FINISH_OFF;
1368         }
1369         else {
1370                 EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);
1371
1372                 if (!emcore_validate_account_with_account_info(account, &err)) {
1373                         emcore_delete_account_from_unvalidated_account_list(account->account_id);
1374                         EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
1375                         if (err == EMAIL_ERROR_CANCELLED) {
1376                                 EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1377                                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL, account->account_id, NULL,  handle_to_be_published, err))
1378                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL] Failed");
1379                                 goto FINISH_OFF;
1380                         }
1381                         else
1382                                 goto FINISH_OFF;
1383                 }
1384                 else {
1385                         emcore_delete_account_from_unvalidated_account_list(account->account_id);
1386
1387                         if (emcore_create_account(account, &err) == false)       {
1388                                 EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
1389                                 goto FINISH_OFF;
1390                         }
1391
1392                         emcore_init_account_reference();
1393
1394                         EM_DEBUG_LOG("incoming_server_type [%d]", account->incoming_server_type);
1395
1396                         if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
1397                                 if (!emcore_sync_mailbox_list(account->account_id, "", handle_to_be_published, &err))  {
1398                                         EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync failed [%d]", err);
1399                                         /*  delete account whose mailbox couldn't be obtained from server */
1400                                         emcore_delete_account(account->account_id, NULL);
1401                                         goto FINISH_OFF;
1402                                 }
1403                         }
1404
1405                         EM_DEBUG_LOG("validating and creating an account are succeeded for account id  [%d]  err [%d]", account->account_id, err);
1406                         if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, NULL,  handle_to_be_published, err))
1407                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
1408                 }
1409         }
1410
1411         ret = true;
1412
1413 FINISH_OFF:
1414         if (ret == false && err != EMAIL_ERROR_CANCELLED && account) {
1415                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
1416                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1417         }
1418
1419         if (error)
1420                 *error = err;
1421
1422         EM_DEBUG_FUNC_END();
1423         return ret;
1424 }
1425
1426 int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
1427 {
1428         EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
1429         int err, ret = false;
1430         emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;
1431
1432         if (!new_account_info) {
1433                 EM_DEBUG_EXCEPTION("Invalid Parameter");
1434                 err = EMAIL_ERROR_INVALID_PARAM;
1435                 goto FINISH_OFF;
1436         }
1437
1438         if (!emnetwork_check_network_status(&err))  {
1439                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1440
1441                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
1442                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
1443                 goto FINISH_OFF;
1444         }
1445         else  {
1446                 EM_DEBUG_LOG("incoming_server_address: (%s)", new_account_info->incoming_server_address);
1447
1448                 if (!emcore_validate_account_with_account_info(new_account_info, &err)) {
1449                         EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err :  %d", err);
1450                         if (err == EMAIL_ERROR_CANCELLED) {
1451                                 EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1452                                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, new_account_info->account_id, NULL,  handle_to_be_published, err))
1453                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
1454                                 goto FINISH_OFF;
1455                         }
1456                         else {
1457                                 goto FINISH_OFF;
1458                         }
1459                 }
1460                 else {
1461                         if (!emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
1462                                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1463                                 /* goto FINISH_OFF; */
1464                         }
1465
1466                         new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
1467                         if (!new_account_tbl) {
1468                                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
1469                                 goto FINISH_OFF;
1470                         }
1471
1472                         em_convert_account_to_account_tbl(new_account_info, new_account_tbl);
1473
1474                         if (emstorage_update_account(account_id, new_account_tbl, true, &err)) {
1475                                 emcore_init_account_reference();
1476                         }
1477
1478                         EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", new_account_info->account_id, err);
1479                         if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, NULL,  handle_to_be_published, err))
1480                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
1481                 }
1482         }
1483
1484         ret = true;
1485
1486 FINISH_OFF:
1487         if (old_account_tbl)
1488                 emstorage_free_account(&old_account_tbl, 1, NULL);
1489         if (new_account_tbl)
1490                 emstorage_free_account(&new_account_tbl, 1, NULL);
1491
1492         if (ret == false && err != EMAIL_ERROR_CANCELLED && new_account_info) {
1493                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
1494                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1495         }
1496
1497         if (error)
1498                 *error = err;
1499
1500         EM_DEBUG_FUNC_END();
1501         return ret;
1502 }
1503
1504 int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
1505 {
1506         EM_DEBUG_FUNC_BEGIN();
1507
1508         emcore_set_mail_slot_size(account_id, mailbox_id, new_slot_size, error);
1509
1510         EM_DEBUG_FUNC_END();
1511         return true;
1512 }
1513
1514 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id)
1515 {
1516         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
1517         int err = EMAIL_ERROR_NONE;
1518
1519         if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1520                 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
1521                 goto FINISH_OFF;
1522         }
1523
1524         if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1525                 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1526                 goto FINISH_OFF;
1527         }
1528
1529 FINISH_OFF:
1530
1531         EM_DEBUG_FUNC_END("err [%d]", err);
1532         return err;
1533 }
1534
1535 #ifdef __FEATURE_LOCAL_ACTIVITY__
1536 int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
1537 {
1538         EM_DEBUG_FUNC_BEGIN();
1539
1540         int err = EMAIL_ERROR_NONE;
1541         email_mailbox_t mailbox;
1542         emstorage_activity_tbl_t *local_activity = NULL;
1543         int activity_id_count = 0;
1544         int activity_chunk_count = 0;
1545         int *activity_id_list = NULL;
1546         int i = 0;
1547
1548         if (!emnetwork_check_network_status(&err))
1549                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1550         else {
1551                 if (false == emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1552                         EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1553                 }
1554                 else {
1555                         for (i = 0; i < activity_id_count; ++i) {
1556                                 if ((false == emstorage_get_activity(account_id , activity_id_list[i], &local_activity, &activity_chunk_count, true,  &err)) || (NULL == local_activity) || (0 == activity_chunk_count))
1557                                         EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
1558                                 else {
1559                                         EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1560                                         switch (local_activity[0].activity_type) {
1561                                                 case ACTIVITY_MODIFYFLAG:  {
1562                                                         if (emcore_sync_flag_with_server(local_activity[0].mail_id , &err))  {
1563                                                                 if (!emcore_delete_activity(&local_activity[0], &err))
1564                                                                         EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1565                                                         }
1566                                                 }
1567                                                 break;
1568
1569                                                 case ACTIVITY_DELETEMAIL:
1570                                                 case ACTIVITY_MOVEMAIL:
1571                                                 case ACTIVITY_MODIFYSEENFLAG:
1572                                                 case ACTIVITY_COPYMAIL:  {
1573
1574                                                         int j = 0, k = 0;
1575                                                         int total_mail_ids = activity_chunk_count;
1576
1577                                                         int *mail_id_list = NULL;
1578
1579                                                         mail_id_list = (int *)em_malloc(sizeof(int) * total_mail_ids);
1580
1581                                                         if (NULL == mail_id_list) {
1582                                                                 EM_DEBUG_EXCEPTION("malloc failed... ");
1583                                                                 break;
1584                                                         }
1585
1586                                                         do {
1587
1588                                                                 for (j = 0; j < BULK_OPERATION_COUNT && (k < total_mail_ids); ++j, ++k)
1589                                                                         mail_id_list[j] = local_activity[k].mail_id;
1590
1591                                                                 switch (local_activity[k-1].activity_type) {
1592                                                                         case ACTIVITY_DELETEMAIL:  {
1593                                                                                 if (!emcore_delete_mail(local_activity[k-1].account_id,
1594                                                                                                                                 mail_id_list,
1595                                                                                                                                 j,
1596                                                                                                                                 EMAIL_DELETE_LOCAL_AND_SERVER,
1597                                                                                                                                 EMAIL_DELETED_BY_COMMAND,
1598                                                                                                                                 false,
1599                                                                                                                                 &err))
1600                                                                                         EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1601                                                                         }
1602                                                                         break;
1603
1604                                                                         case ACTIVITY_MOVEMAIL:  {
1605                                                                                 if (!emcore_move_mail_on_server_ex(local_activity[k-1].account_id ,
1606                                                                                                                                                    local_activity[k-1].src_mbox,
1607                                                                                                                                                    mail_id_list,
1608                                                                                                                                                    j,
1609                                                                                                                                                    local_activity[k-1].dest_mbox,
1610                                                                                                                                                    &err))
1611                                                                                         EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
1612                                                                         }
1613                                                                         break;
1614                                                                         case ACTIVITY_MODIFYSEENFLAG:  {
1615                                                                                 int seen_flag = atoi(local_activity[0].src_mbox);
1616                                                                                 if (!emcore_sync_seen_flag_with_server_ex(mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
1617                                                                                         EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
1618                                                                         }
1619                                                                         break;
1620                                                                 }
1621
1622                                                         } while (k < total_mail_ids);
1623
1624                                                         EM_SAFE_FREE(mail_id_list);
1625                                                 }
1626
1627                                                 break;
1628
1629                                                 default:
1630                                                         EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1631                                                 break;
1632                                         }
1633
1634                                         emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1635
1636                                         if (g_local_activity_run == 1) {
1637                                                 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1638                                                 break;
1639                                         }
1640                                 }
1641                         }
1642                 }
1643         }
1644         if (activity_id_list) {
1645                 if (false == emstorage_free_activity_id_list(activity_id_list, &err))
1646                         EM_DEBUG_EXCEPTION("emstorage_free_activity_id_list failed");
1647         }
1648
1649         if (error)
1650                 *error = err;
1651
1652         EM_DEBUG_FUNC_END();
1653
1654         return true;
1655 }
1656 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1657
1658 int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error)
1659 {
1660         EM_DEBUG_FUNC_BEGIN();
1661
1662         int err = EMAIL_ERROR_NONE;
1663         email_mailbox_t mailbox;
1664
1665         memset(&mailbox, 0x00, sizeof(mailbox));
1666         mailbox.account_id = account_id;
1667
1668         if (!emnetwork_check_network_status(&err))  {
1669                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1670
1671                 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1672         }
1673         else  {
1674                 if (!emcore_download_body_multi_sections_bulk(NULL,
1675                         mailbox.account_id,
1676                         mail_id,
1677                         option >> 1,            /*  0 :  silent, 1 :  verbose */
1678                         option & 0x01,          /*  0 :  without attachments, 1 :  with attachments */
1679                         NO_LIMITATION,
1680                         handle_to_be_published,
1681                         &err))
1682                         EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed - %d", err);
1683         }
1684
1685         if (error)
1686                 *error = err;
1687
1688         EM_DEBUG_FUNC_END();
1689         return true;
1690 }
1691
1692 int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
1693 {
1694         EM_DEBUG_FUNC_BEGIN();
1695
1696         int err = EMAIL_ERROR_NONE;
1697
1698         EM_DEBUG_LOG("attachment_no is %d", attachment_no);
1699
1700         if (!emnetwork_check_network_status(&err))  {
1701                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1702                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
1703         }
1704         else  {
1705
1706 #ifdef __ATTACHMENT_OPTI__
1707                 if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, &err))
1708                         EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1709 #else
1710                 if (!emcore_download_attachment(account_id, mail_id, attachment_no, &err))
1711                         EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
1712 #endif
1713         }
1714
1715         if (error)
1716                 *error = err;
1717
1718         EM_DEBUG_FUNC_END();
1719         return true;
1720 }
1721
1722 int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
1723 {
1724         EM_DEBUG_FUNC_BEGIN();
1725
1726         int err = EMAIL_ERROR_NONE;
1727
1728         if (!emnetwork_check_network_status(&err))
1729                 EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
1730         else if (!emcore_sync_flags_field_with_server(mail_ids, num, field_type, value, &err))
1731                 EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
1732
1733         if (error)
1734                 *error = err;
1735
1736         EM_DEBUG_FUNC_END();
1737         return true;
1738 }
1739
1740 int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error)
1741 {
1742         EM_DEBUG_FUNC_BEGIN();
1743
1744         int err = EMAIL_ERROR_NONE;
1745
1746         if (!emnetwork_check_network_status(&err))  {
1747                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1748
1749                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1750                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>>");
1751         }
1752         else  {
1753
1754                 if (!emcore_validate_account(account_id, &err)) {
1755                         EM_DEBUG_EXCEPTION("emcore_validate_account failed account id  :  %d  err :  %d", account_id, err);
1756
1757                         if (err == EMAIL_ERROR_CANCELLED) {
1758                                 EM_DEBUG_EXCEPTION("notify  :  NOTI_VALIDATE_ACCOUNT_CANCEL ");
1759                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, account_id, NULL,  handle_to_be_published, err))
1760                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
1761                         }
1762                         else {
1763                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1764                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1765                         }
1766                 }
1767                 else {
1768                         email_account_t *account_ref = NULL;
1769                         account_ref = emcore_get_account_reference(account_id);
1770
1771                         if (account_ref) {
1772                                 EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
1773                                 if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
1774                                         if (!emcore_check_thread_status())
1775                                                 err = EMAIL_ERROR_CANCELLED;
1776                                         else if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
1777                                                 EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
1778                                 }
1779
1780                                 if (err > 0) {
1781                                         EM_DEBUG_EXCEPTION("emcore_validate_account succeeded account id  :  %d  err :  %d", account_id, err);
1782                                         if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, account_id, NULL,  handle_to_be_published, err))
1783                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success >>>>");
1784                                 }
1785
1786                                 emcore_free_account(account_ref);
1787                                 EM_SAFE_FREE(account_ref);
1788                         }
1789                 }
1790         }
1791
1792         if (error)
1793                 *error = err;
1794
1795         EM_DEBUG_FUNC_END();
1796         return true;
1797 }
1798
1799 int event_handler_EMAIL_EVENT_UPDATE_MAIL(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published)
1800 {
1801         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
1802         int err = EMAIL_ERROR_NONE;
1803
1804         if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
1805                 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1806
1807         if(input_mail_data)
1808                 emcore_free_mail_data_list(&input_mail_data, 1);
1809
1810         if(input_attachment_data_list)
1811                 emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);
1812
1813         if(input_meeting_request)
1814                 emstorage_free_meeting_request(input_meeting_request);
1815
1816         EM_DEBUG_FUNC_END("err [%d", err);
1817         return err;
1818 }
1819 int event_handler_EMAIL_EVENT_SAVE_MAIL(int input_account_id, int input_mail_id, int input_handle_to_be_published)
1820 {
1821         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mail_id [%d] input_handle_to_be_published [%d]", input_account_id, input_mail_id, input_handle_to_be_published);
1822         int err = EMAIL_ERROR_NONE;
1823
1824         err = emcore_sync_mail_from_client_to_server(input_mail_id);
1825
1826         EM_DEBUG_FUNC_END("err [%d]", err);
1827         return err;
1828 }
1829
1830 int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error)
1831 {
1832         EM_DEBUG_FUNC_BEGIN();
1833         int err = EMAIL_ERROR_NONE, ret = false;
1834         email_account_t *account_ref = NULL;
1835
1836         if (!(account_ref = emcore_get_account_reference(account_id))) {
1837                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1838                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1839                 goto FINISH_OFF;
1840         }
1841
1842         /* Move mail local */
1843         /*
1844         if (!emcore_mail_move(mail_ids, mail_id_count, dest_mailbox.mailbox_name, EMAIL_MOVED_BY_COMMAND, 0, &err)) {
1845                 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
1846                 goto FINISH_OFF;
1847         }
1848         */
1849
1850         if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1851                 /* Move mail on server */
1852                 if (!emnetwork_check_network_status(&err))
1853                         EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1854                 else {
1855 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1856                         if (!emcore_move_mail_on_server_ex(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1857                                 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
1858 #else
1859                         if (!emcore_move_mail_on_server(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1860                                 EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
1861 #endif
1862                 }
1863         }
1864
1865         ret = true;
1866 FINISH_OFF:
1867         EM_SAFE_FREE(mail_ids); /*prevent 33693*/
1868
1869         if (account_ref) {
1870                 emcore_free_account(account_ref);
1871                 EM_SAFE_FREE(account_ref);
1872         }
1873
1874         if (error)
1875                 *error = err;
1876
1877         EM_DEBUG_FUNC_END();
1878         return ret;
1879 }
1880
1881 int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int handle_to_be_published, int *error)
1882 {
1883         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], on_server [%d], handle_to_be_published [%d], error [%p]",  mailbox_id, on_server, handle_to_be_published, error);
1884         int err = EMAIL_ERROR_NONE;
1885
1886         if (!emnetwork_check_network_status(&err))
1887                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1888         else  {
1889                 if (!emcore_delete_mailbox(mailbox_id, on_server, &err))
1890                         EM_DEBUG_LOG("emcore_delete failed - %d", err);
1891         }
1892
1893         if (error)
1894                 *error = err;
1895
1896         EM_DEBUG_FUNC_END();
1897         return true;
1898 }
1899
1900 int event_handler_EMAIL_EVENT_CREATE_MAILBOX(int account_id, char *mailbox_name, char *mailbox_alias, int mailbox_type, int on_server, int handle_to_be_published, int *error)
1901 {
1902         EM_DEBUG_FUNC_BEGIN();
1903         int err = EMAIL_ERROR_NONE;
1904         email_mailbox_t mailbox;
1905
1906         memset(&mailbox, 0x00, sizeof(mailbox));
1907
1908         mailbox.account_id = account_id;
1909         mailbox.mailbox_name = mailbox_name;
1910         mailbox.alias = mailbox_alias;
1911         mailbox.mailbox_type = mailbox_type;
1912
1913         if (!emnetwork_check_network_status(&err))  {
1914                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1915         }
1916         else  {
1917                 if (!emcore_create_mailbox(&mailbox, on_server, &err))
1918                         EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
1919         }
1920
1921
1922         if (error)
1923                 *error = err;
1924
1925         EM_DEBUG_FUNC_END();
1926         return true;
1927 }
1928
1929 int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error)
1930 {
1931         EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);
1932
1933         int err = EMAIL_ERROR_NONE;
1934
1935         if (!emnetwork_check_network_status(&err))
1936                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1937         else {
1938                 if (!emcore_sync_flag_with_server(mail_id, &err))
1939                         EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
1940 #ifdef __FEATURE_LOCAL_ACTIVITY__
1941                 else {
1942                         emstorage_activity_tbl_t new_activity;
1943                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1944                         new_activity.activity_type = ACTIVITY_MODIFYFLAG;
1945                         new_activity.account_id    = event_data.account_id;
1946                         new_activity.mail_id       = event_data.event_param_data_4;
1947                         new_activity.dest_mbox     = NULL;
1948                         new_activity.server_mailid = NULL;
1949                         new_activity.src_mbox      = NULL;
1950
1951                         if (!emcore_delete_activity(&new_activity, &err))
1952                                 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1953                 }
1954 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
1955         }
1956
1957         if (error)
1958                 *error = err;
1959
1960         EM_DEBUG_FUNC_END();
1961         return true;
1962 }
1963
1964 int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int input_mailbox_id, int input_from_server, int *error)
1965 {
1966         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id [%d], input_from_server [%d], error [%p]", input_account_id, input_mailbox_id, input_from_server, error);
1967         int err = EMAIL_ERROR_NONE;
1968
1969         if (!emcore_delete_all_mails_of_mailbox(input_account_id, input_mailbox_id, input_from_server, &err))
1970                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
1971
1972         if (error)
1973                 *error = err;
1974
1975         EM_DEBUG_FUNC_END("err [%d]", err);
1976         return true;
1977 }
1978
1979 int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error)
1980 {
1981         EM_DEBUG_FUNC_BEGIN();
1982         int err = EMAIL_ERROR_NONE;
1983         int ret = false;
1984         email_account_t *account_ref = NULL;
1985
1986         if (!(account_ref = emcore_get_account_reference(account_id))) {
1987                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1988                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1989                 goto FINISH_OFF;
1990         }
1991
1992         if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_FROM_SERVER, EMAIL_DELETED_BY_COMMAND, false, &err)) {
1993                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
1994                 goto FINISH_OFF;
1995         }
1996
1997         ret = true;
1998 FINISH_OFF:
1999
2000         if (account_ref) {
2001                 emcore_free_account(account_ref);
2002                 EM_SAFE_FREE(account_ref);
2003         }
2004
2005         if (error)
2006                 *error = err;
2007
2008         EM_DEBUG_FUNC_END();
2009         return ret;
2010 }
2011
2012 int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int handle_to_be_published, int *error)
2013 {
2014         EM_DEBUG_FUNC_BEGIN();
2015         int err = EMAIL_ERROR_NONE;
2016
2017         if (!emnetwork_check_network_status(&err))  {
2018                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2019                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_id, maibox_name,  0, err))
2020                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
2021         }
2022         else  {
2023                 EM_DEBUG_LOG("Sync of all mailbox");
2024                 if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
2025                         EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2026         }
2027
2028         if (error)
2029                 *error = err;
2030
2031         EM_DEBUG_FUNC_END();
2032         return true;
2033 }
2034
2035 int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, int handle_to_be_published, int *error)
2036 {
2037         EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], criteria : [%s]", account_id, mailbox_id, criteria);
2038
2039         int err = EMAIL_ERROR_NONE;
2040         int i = 0;
2041         int mail_id = 0;
2042         int thread_id = 0;
2043         char temp_uid_string[20] = {0,};
2044
2045         emcore_uid_list uid_elem;
2046         emstorage_mailbox_tbl_t *search_mailbox = NULL;
2047         emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
2048
2049         MAILSTREAM *stream = NULL;
2050         MESSAGECACHE *mail_cache_element = NULL;
2051         ENVELOPE *env = NULL;
2052         emstorage_mailbox_tbl_t* local_mailbox = NULL;
2053         char mailbox_id_param_string[10] = {0,};
2054
2055         if (account_id < 0 || mailbox_id == 0) {
2056                 EM_DEBUG_EXCEPTION("Invalid parameter");
2057                 err = EMAIL_ERROR_INVALID_PARAM;
2058                 goto FINISH_OFF;
2059         }
2060
2061         if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
2062                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
2063                 goto FINISH_OFF;
2064         }
2065
2066         SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);
2067
2068         if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_START, account_id, mailbox_id_param_string, handle_to_be_published, 0))
2069                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_START] failed >>>>");
2070
2071         if (!emnetwork_check_network_status(&err)) {
2072                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
2073                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string,  0, err))
2074                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
2075                 goto FINISH_OFF;
2076         }
2077
2078         if (!emcore_connect_to_remote_mailbox(account_id, mailbox_id, (void **)&stream, &err)) {
2079                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
2080                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2081                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2082                 goto FINISH_OFF;
2083         }
2084
2085         if (!mail_search_full(stream, NIL, mail_criteria(criteria), SE_FREE)) {
2086                 EM_DEBUG_EXCEPTION("mail_search failed");
2087                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2088                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2089                 goto FINISH_OFF;
2090         }
2091
2092         for (i = 1; i <= stream->nmsgs; ++i) {
2093                 mail_cache_element = mail_elt(stream, i);
2094                 if (mail_cache_element->searched) {
2095                         env = mail_fetchstructure_full(stream, i, NULL, FT_PEEK);
2096
2097                         memset(&uid_elem, 0x00, sizeof(uid_elem));
2098
2099                         uid_elem.msgno = mail_cache_element->msgno;
2100                         SNPRINTF(temp_uid_string, 20, "%4lu", mail_cache_element->private.uid);
2101                         uid_elem.uid = temp_uid_string;
2102                         uid_elem.flag.seen = mail_cache_element->seen;
2103
2104                         if (!emcore_make_mail_tbl_data_from_envelope(stream, env, &uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
2105                                 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
2106                                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2107                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2108                                 goto FINISH_OFF;
2109                         }
2110
2111                         search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
2112                         if (search_mailbox == NULL) {
2113                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2114                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2115                                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2116                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2117                                 goto FINISH_OFF;
2118                         }
2119
2120                         search_mailbox->account_id = account_id;
2121                         search_mailbox->mailbox_id = mailbox_id;
2122                         search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
2123                         search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
2124
2125                         if ((err = emcore_add_mail_to_mailbox(search_mailbox, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
2126                                 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
2127                                 if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
2128                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
2129                                 goto FINISH_OFF;
2130                         }
2131                         memset(mailbox_id_param_string, 0, 10);
2132                         SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
2133                         if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id)) {
2134                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
2135                         }
2136
2137                         if (new_mail_tbl_data) {
2138                                 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2139                                 new_mail_tbl_data = NULL;
2140                         }
2141                 }
2142         }
2143
2144         if (err == EMAIL_ERROR_NONE && !emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH, account_id, NULL, handle_to_be_published, 0))
2145                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");
2146
2147 FINISH_OFF:
2148
2149         if (search_mailbox != NULL)
2150                 emstorage_free_mailbox(&search_mailbox, 1, NULL);
2151
2152         if (new_mail_tbl_data)
2153                 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
2154
2155         if (local_mailbox)
2156                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
2157
2158         if (error)
2159                 *error = err;
2160
2161         EM_DEBUG_FUNC_END();
2162         return true;
2163 }
2164
2165 static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published)
2166 {
2167         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_path [%s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, input_new_mailbox_alias, handle_to_be_published);
2168         int err = EMAIL_ERROR_NONE;
2169
2170         if ((err = emcore_move_mailbox_on_imap_server(input_account_id, input_old_mailbox_path, input_new_mailbox_path)) != EMAIL_ERROR_NONE) {
2171                 EM_DEBUG_EXCEPTION("emcore_move_mailbox_on_imap_server failed [%d]", err);
2172         }
2173
2174         if (err == EMAIL_ERROR_NONE) {
2175                 if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
2176                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
2177
2178                 if ((err = emstorage_rename_mailbox(input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, true)) != EMAIL_ERROR_NONE) {
2179                         EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
2180                 }
2181         }
2182         else if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0)) {
2183                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
2184         }
2185
2186         EM_DEBUG_FUNC_END("err [%d]", err);
2187         return err;
2188 }
2189
2190 void* thread_func_branch_command(void *arg)
2191 {
2192         EM_DEBUG_FUNC_BEGIN();
2193
2194         int err = EMAIL_ERROR_NONE;
2195         int is_storage_full = false;
2196         int noti_id = 0;
2197         email_event_t event_data;
2198         email_session_t *session = NULL;
2199         emstorage_account_tbl_t *account_tbl = NULL;
2200         int handle_to_be_published = 0;
2201
2202         if (!emstorage_open(&err))  {
2203                 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
2204                 return false;
2205         }
2206
2207         /* check that event_data loop is continuous */
2208         while (emcore_event_loop_continue())  {
2209                 if (!emcore_get_empty_session(&session))
2210                         EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
2211
2212                 /* get a event_data from event_data queue */
2213                 if (!emcore_retrieve_event(&event_data, NULL))  {       /*  no event_data pending */
2214                         EM_DEBUG_LOG("For handle g_event_que[g_event_que_idx].type [%d], g_event_que_idx [%d]", g_event_que[g_event_que_idx].type, g_event_que_idx);
2215 #ifdef ENABLE_IMAP_IDLE_THREAD
2216                         if ( !emnetwork_check_network_status(&err))  {
2217                                 EM_DEBUG_LOG(">>>> Data Networking ON ");
2218                                 if (g_client_run) {
2219                                         if (g_imap_idle_thread_alive) {
2220                                                 /* emcore_kill_imap_idle_thread(NULL); */
2221                                                 /* emcore_create_imap_idle_thread(NULL); */
2222                                         }
2223                                         else {
2224                                                 if (!send_thread_run)
2225                                                         emcore_create_imap_idle_thread(event_data.account_id, NULL);
2226                                         }
2227                                 }
2228                         }
2229 #endif /*  ENABLE_IMAP_IDLE_THREAD */
2230 #ifdef __FEATURE_LOCAL_ACTIVITY__
2231                         /*  Local activity sync */
2232                         if (g_client_run && g_local_activity_run) {
2233                                 emstorage_account_tbl_t *account_list = NULL;
2234                                 int count = 0, i;
2235                                 if (!emstorage_get_account_list(&count, &account_list, true, true, &err))
2236                                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
2237                                 else {
2238                                         for (i = 0; i < count; i++) {
2239                                                 if (emcore_local_activity_sync(account_list[i].account_id, &err)) {
2240                                                         EM_DEBUG_LOG("Found local activity...!");
2241                                                         EM_DEBUG_LOG("Resetting g_local_activity_run ");
2242                                                         g_local_activity_run = 0;
2243                                                         emcore_clear_session(session);
2244                                                 }
2245                                         }
2246
2247                                         emstorage_free_account(&account_list, count, &err);
2248
2249                                         if (!g_local_activity_run)
2250                                                 continue;
2251                                 }
2252                         }
2253 #endif
2254
2255                         recv_thread_run = 0;
2256
2257                         ENTER_CRITICAL_SECTION(_event_available_lock);
2258                         SLEEP_CONDITION_VARIABLE(_event_available_signal, _event_available_lock);
2259                         EM_DEBUG_LOG("Wake up by _event_available_signal");
2260                         LEAVE_CRITICAL_SECTION(_event_available_lock);
2261                 }
2262                 else  {
2263                         EM_DEBUG_LOG(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
2264                         EM_DEBUG_LOG("For handle g_event_que_idx - %d", g_event_que_idx);
2265
2266                         if (g_event_que_idx == 1)
2267                                 handle_to_be_published = 31;
2268                         else
2269                                 handle_to_be_published = g_event_que_idx - 1 ;
2270
2271                         EM_DEBUG_LOG("Handle to be Published  [%d]", handle_to_be_published);
2272                         recv_thread_run = 1;
2273                         g_client_run = 1;
2274
2275                         /*  Handling storage full */
2276                         is_storage_full = false;
2277                         if (event_data.type == EMAIL_EVENT_SYNC_HEADER || event_data.type == EMAIL_EVENT_SYNC_HEADER_OMA ||
2278                                 event_data.type == EMAIL_EVENT_DOWNLOAD_BODY || event_data.type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
2279                                 if (emcore_is_storage_full(&err) == true) {
2280                                         EM_DEBUG_EXCEPTION("Storage is full");
2281                                         switch (event_data.type) {
2282                                                 case EMAIL_EVENT_SYNC_HEADER:
2283                                                 case EMAIL_EVENT_SYNC_HEADER_OMA:
2284                                                         noti_id = NOTI_DOWNLOAD_FAIL;
2285                                                         break;
2286                                                 case EMAIL_EVENT_DOWNLOAD_BODY:
2287                                                         noti_id = NOTI_DOWNLOAD_BODY_FAIL;
2288                                                         break;
2289                                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2290                                                         noti_id = NOTI_DOWNLOAD_ATTACH_FAIL;
2291                                                         break;
2292                                                 default:
2293                                                         break;
2294                                         }
2295
2296                                         if (!emcore_notify_network_event(noti_id, event_data.account_id, NULL,  handle_to_be_published, err))
2297                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
2298                                         is_storage_full = true;
2299                                 }
2300                         }
2301
2302                         emdevice_set_dimming_on_off(false, NULL);
2303
2304                         if (event_data.account_id > 0) {
2305                                 if (!emstorage_get_account_by_id(event_data.account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
2306                                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
2307                                 }
2308                         }
2309
2310                         if (account_tbl)
2311                                 EM_DEBUG_LOG("account_id : [%d], sync_disabled : [%d]", event_data.account_id, account_tbl->sync_disabled);
2312
2313                         if (!account_tbl || account_tbl->sync_disabled == 0) {
2314                                 switch (event_data.type)  {
2315                                         case EMAIL_EVENT_SYNC_IMAP_MAILBOX:  /*  get imap mailbox list  */
2316                                                 if (!emcore_sync_mailbox_list(event_data.account_id, event_data.event_param_data_3, handle_to_be_published, &err))
2317                                                         EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
2318                                                 EM_SAFE_FREE(event_data.event_param_data_3);
2319                                                 break;
2320
2321                                         case EMAIL_EVENT_SYNC_HEADER:  /*  synchronize mail header  */
2322                                                 if (is_storage_full == false)
2323                                                         event_handler_EMAIL_EVENT_SYNC_HEADER(event_data.account_id, event_data.event_param_data_5, handle_to_be_published,  &err);
2324                                                 break;
2325
2326                                         case EMAIL_EVENT_SYNC_HEADER_OMA:  /*  synchronize mail header for OMA */
2327                                                 if (is_storage_full == false)
2328                                                         event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data.account_id, event_data.event_param_data_1, handle_to_be_published, &err);
2329                                                 EM_SAFE_FREE(event_data.event_param_data_1);
2330                                                 break;
2331
2332                                         case EMAIL_EVENT_DOWNLOAD_BODY:  /*  download mail body  */
2333                                                 if (is_storage_full == false)
2334                                                         event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data.account_id, (int)event_data.event_param_data_4, (int)event_data.event_param_data_3, handle_to_be_published, &err);
2335                                                 event_data.event_param_data_3 = NULL;           /*  MUST BE */
2336                                                 break;
2337
2338                                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:  /*  download attachment */
2339                                                 if (is_storage_full == false)
2340                                                         event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data.account_id, (int)event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
2341                                                 break;
2342
2343                                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
2344                                                 event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER((int*)event_data.event_param_data_3, event_data.event_param_data_4 , event_data.event_param_data_5, event_data.event_param_data_6, &err);
2345                                                 EM_SAFE_FREE(event_data.event_param_data_3);
2346                                                 break;
2347
2348                                         case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
2349                                                 event_handler_EMAIL_EVENT_DELETE_MAIL(event_data.account_id, (int*)event_data.event_param_data_3, event_data.event_param_data_4, &err);
2350                                                 EM_SAFE_FREE(event_data.event_param_data_3);
2351                                                 break;
2352
2353                                         case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
2354                                                 event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data.account_id, event_data.event_param_data_4, (int)event_data.event_param_data_5, &err);
2355                                                 break;
2356 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
2357                                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2358                                                 event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER((int)event_data.event_param_data_4, &err);
2359                                                 break;
2360 #endif
2361
2362                                         case EMAIL_EVENT_CREATE_MAILBOX:
2363                                                 event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data.account_id, event_data.event_param_data_1, event_data.event_param_data_2, GPOINTER_TO_INT(event_data.event_param_data_3), event_data.event_param_data_4, handle_to_be_published, &err);
2364                                                 EM_SAFE_FREE(event_data.event_param_data_1);
2365                                                 EM_SAFE_FREE(event_data.event_param_data_2);
2366                                                 break;
2367
2368                                         case EMAIL_EVENT_DELETE_MAILBOX:
2369                                                 event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data.event_param_data_4, event_data.event_param_data_4, handle_to_be_published, &err);
2370                                                 break;
2371
2372                                         case EMAIL_EVENT_SAVE_MAIL:
2373                                                 err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data.account_id, event_data.event_param_data_4, handle_to_be_published);
2374                                                 break;
2375
2376                                         case EMAIL_EVENT_MOVE_MAIL:
2377                                                 event_handler_EMAIL_EVENT_MOVE_MAIL(event_data.account_id, (int  *)event_data.event_param_data_3, event_data.event_param_data_4, event_data.event_param_data_5, event_data.event_param_data_8, handle_to_be_published, &err);
2378                                                 event_data.event_param_data_3 = NULL; /*prevent 33693*/
2379                                                 break;
2380
2381                                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
2382                                                 event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data.account_id, handle_to_be_published, &err);
2383                                                 break;
2384
2385                                         case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
2386 /*                                              event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(emcore_get_account_from_unvalidated_account_list(), handle_to_be_published, &err);*/
2387                                                         email_account_t *account = (email_account_t *)event_data.event_param_data_1;
2388                                                         event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(account, handle_to_be_published, &err);
2389                                                         if(account) {
2390                                                                 emcore_free_account(account);
2391                                                                 EM_SAFE_FREE(account);
2392                                                         }
2393                                                 }
2394                                                 break;
2395
2396                                         case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
2397                                                         email_account_t *account = (email_account_t *)event_data.event_param_data_1;
2398                                                         event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data.account_id, account, handle_to_be_published, &err);
2399                                                         emcore_free_account(account);
2400                                                         EM_SAFE_FREE(account);
2401                                                 }
2402                                                 break;
2403
2404                                         case EMAIL_EVENT_UPDATE_MAIL:
2405                                                 event_handler_EMAIL_EVENT_UPDATE_MAIL((email_mail_data_t*)event_data.event_param_data_1, (email_attachment_data_t*)event_data.event_param_data_2, event_data.event_param_data_4, (email_meeting_request_t*)event_data.event_param_data_3, event_data.event_param_data_5, handle_to_be_published);
2406
2407                                                 event_data.event_param_data_1 = NULL;
2408                                                 event_data.event_param_data_2 = NULL;
2409                                                 event_data.event_param_data_3 = NULL;
2410                                                 break;
2411
2412                                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2413                                                 event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data.account_id, event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
2414                                                 break;
2415
2416                                         case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
2417                                                 err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data.account_id, event_data.event_param_data_4);
2418                                                 break;
2419
2420         #ifdef __FEATURE_LOCAL_ACTIVITY__
2421                                         case EMAIL_EVENT_LOCAL_ACTIVITY:
2422                                                 event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data.account_id, &err);
2423                                                 break;
2424         #endif /* __FEATURE_LOCAL_ACTIVITY__*/
2425
2426                                         case EMAIL_EVENT_SEARCH_ON_SERVER:
2427                                                 event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(event_data.account_id, event_data.event_param_data_4, (char *)event_data.event_param_data_1, handle_to_be_published, &err);
2428                                                 EM_SAFE_FREE(event_data.event_param_data_1);
2429                                                 break;
2430
2431                                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2432                                                 err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data.account_id, event_data.event_param_data_4, (char*)event_data.event_param_data_1, (char*)event_data.event_param_data_2, (char*)event_data.event_param_data_3, handle_to_be_published);
2433                                                 EM_SAFE_FREE(event_data.event_param_data_1);
2434                                                 EM_SAFE_FREE(event_data.event_param_data_2);
2435                                                 EM_SAFE_FREE(event_data.event_param_data_3);
2436                                                 break;
2437
2438                                         default:
2439                                                 break;
2440                                 }
2441                         }
2442
2443                         if (account_tbl) {
2444                                 emstorage_free_account(&account_tbl, 1, NULL);
2445                                 account_tbl = NULL;
2446                         }
2447
2448                         if (!emcore_notify_response_to_api(event_data.type, handle_to_be_published, err))
2449                                 EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");
2450
2451                         emdevice_set_dimming_on_off(true, NULL);
2452                         em_flush_memory();
2453
2454                         switch (event_data.type)  {
2455                                 case EMAIL_EVENT_SEND_MAIL:
2456                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
2457                                         _sending_busy_unref();
2458                                         break;
2459
2460                                 case EMAIL_EVENT_SYNC_HEADER:
2461                                 case EMAIL_EVENT_SYNC_HEADER_OMA:
2462                                 case EMAIL_EVENT_DOWNLOAD_BODY:
2463                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2464                                 case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2465                                 case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2466                                 case EMAIL_EVENT_DELETE_MAIL:
2467                                 case EMAIL_EVENT_DELETE_MAIL_ALL:
2468                                 case EMAIL_EVENT_VALIDATE_ACCOUNT:
2469                                 case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2470                                 case EMAIL_EVENT_SAVE_MAIL:
2471                                 case EMAIL_EVENT_MOVE_MAIL:
2472                                 case EMAIL_EVENT_CREATE_MAILBOX:
2473                                 case EMAIL_EVENT_DELETE_MAILBOX:
2474                                 case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2475                                 case EMAIL_EVENT_SEARCH_ON_SERVER:
2476                                 case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2477                                         _receiving_busy_unref();
2478                                         break;
2479
2480                                 default:
2481                                         break;
2482                         }
2483
2484                         event_data.type = 0;
2485
2486                         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2487                         memset(g_event_que+g_active_que, 0x00, sizeof(email_event_t));
2488                         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2489                 }
2490
2491                 emcore_clear_session(session);
2492         }
2493
2494         if (!emstorage_close(&err))
2495                 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
2496
2497         EM_DEBUG_FUNC_END();
2498         return SUCCESS;
2499 }
2500 /*Send event_data loop*/
2501 INTERNAL_FUNC int emcore_start_event_loop_for_sending_mails(int *err_code)
2502 {
2503         EM_DEBUG_FUNC_BEGIN();
2504         int thread_error = -1;
2505
2506         if (err_code != NULL)
2507                 *err_code = EMAIL_ERROR_NONE;
2508
2509         memset(&g_send_event_que, 0x00, sizeof(g_send_event_que));
2510
2511         if (g_send_srv_thread)  {
2512                 EM_DEBUG_EXCEPTION("\t send service thread is already running...");
2513                 if (err_code != NULL)
2514                         *err_code = EMAIL_ERROR_UNKNOWN;
2515                 return true;
2516         }
2517
2518         g_send_event_que_idx = 1;
2519         g_send_event_loop = 1;
2520         g_send_active_que = 0;
2521
2522         /* initialize lock */
2523         /*  INITIALIZE_CRITICAL_SECTION(_send_event_available_lock); */
2524         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2525         INITIALIZE_CONDITION_VARIABLE(_send_event_available_signal);
2526
2527         /* create thread */
2528         THREAD_CREATE_JOINABLE(g_send_srv_thread, thread_func_branch_command_for_sending_mails, thread_error);
2529
2530         if (thread_error != 0) {
2531                 EM_DEBUG_EXCEPTION("cannot make thread...");
2532                 if (err_code != NULL)
2533                         *err_code = EMAIL_ERROR_UNKNOWN;
2534                 return FAILURE;
2535         }
2536
2537         if (err_code != NULL)
2538                 *err_code = EMAIL_ERROR_NONE;
2539         EM_DEBUG_FUNC_END();
2540         return SUCCESS;
2541 }
2542
2543 /* finish api event_data loop */
2544 INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
2545 {
2546     EM_DEBUG_FUNC_BEGIN();
2547
2548         if (err_code != NULL)
2549                 *err_code = EMAIL_ERROR_NONE;
2550
2551         if (!g_send_srv_thread)          {
2552                 if (err_code != NULL)
2553                         *err_code = EMAIL_ERROR_UNKNOWN;
2554                 return false;
2555         }
2556
2557     /* stop event_data loop */
2558     g_send_event_loop = 0;
2559
2560         emcore_cancel_send_mail_thread(g_send_active_que, NULL, err_code);
2561         ENTER_CRITICAL_SECTION(_send_event_available_lock);
2562         WAKE_CONDITION_VARIABLE(_send_event_available_signal);          /*  MUST BE HERE */
2563         LEAVE_CRITICAL_SECTION(_send_event_available_lock);
2564
2565         /* wait for thread finished */
2566         THREAD_JOIN(g_send_srv_thread);
2567
2568         g_send_srv_thread = 0;
2569
2570         DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2571         DELETE_CRITICAL_SECTION(_send_event_available_lock);
2572         DELETE_CONDITION_VARIABLE(_send_event_available_signal);
2573
2574         g_send_event_que_idx = 1;
2575         g_send_active_que = 0;
2576
2577         if (err_code != NULL)
2578                 *err_code = EMAIL_ERROR_NONE;
2579
2580     return true;
2581 }
2582
2583 /* start api event_data loop */
2584 INTERNAL_FUNC int emcore_start_event_loop(int *err_code)
2585 {
2586     EM_DEBUG_FUNC_BEGIN();
2587         int thread_error;
2588
2589         if (err_code != NULL)
2590                 *err_code = EMAIL_ERROR_NONE;
2591
2592     memset(&g_event_que, 0x00, sizeof(g_event_que));
2593
2594         if (g_srv_thread) {
2595                 EM_DEBUG_EXCEPTION("service thread is already running...");
2596                 if (err_code != NULL)
2597                         *err_code = EMAIL_ERROR_UNKNOWN;
2598                 return true;
2599         }
2600
2601         g_event_que_idx = 1;
2602         g_event_loop = 1;
2603         g_active_que = 0;
2604
2605     /* initialize lock */
2606         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2607         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2608
2609         emcore_initialize_event_callback_table();
2610
2611     /* create thread */
2612         THREAD_CREATE(g_srv_thread, thread_func_branch_command, NULL, thread_error);
2613
2614         if (thread_error != 0) {
2615         EM_DEBUG_EXCEPTION("cannot create thread");
2616                 if (err_code != NULL)
2617                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
2618         return FAILURE;
2619     }
2620
2621         if (err_code != NULL)
2622                 *err_code = EMAIL_ERROR_NONE;
2623
2624     return false;
2625 }
2626
2627 /* finish api event_data loop */
2628 INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
2629 {
2630     EM_DEBUG_FUNC_BEGIN();
2631
2632         if (err_code != NULL)
2633                 *err_code = EMAIL_ERROR_NONE;
2634
2635         if (!g_srv_thread)  {
2636                 if (err_code != NULL)
2637                         *err_code = EMAIL_ERROR_UNKNOWN;
2638                 return false;
2639         }
2640
2641     /* stop event_data loop */
2642     g_event_loop = 0;
2643
2644         /*      pthread_kill(g_srv_thread, SIGINT); */
2645         emcore_cancel_thread(g_active_que, NULL, err_code);
2646
2647         ENTER_CRITICAL_SECTION(_event_available_lock);
2648         WAKE_CONDITION_VARIABLE(_event_available_signal);
2649         LEAVE_CRITICAL_SECTION(_event_available_lock);
2650
2651         /* wait for thread finished */
2652         THREAD_JOIN(g_srv_thread);
2653
2654         g_srv_thread = 0;
2655
2656         DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2657         DELETE_CRITICAL_SECTION(_event_available_lock);
2658         DELETE_CONDITION_VARIABLE(_event_available_signal);
2659         DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
2660
2661         g_event_que_idx = 1;
2662         g_active_que = 0;
2663
2664         if (err_code != NULL)
2665                 *err_code = EMAIL_ERROR_NONE;
2666         EM_DEBUG_FUNC_END();
2667     return true;
2668 }
2669
2670
2671 int emcore_get_active_queue_idx()
2672 {
2673         return g_send_active_que;
2674 }
2675
2676 /* check thread status
2677 * 0 : stop job 1 : continue job
2678 */
2679 INTERNAL_FUNC int emcore_check_thread_status()
2680 {
2681         if (g_active_que <= 0)
2682                 return true;
2683
2684         return (g_event_que[g_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2685 }
2686
2687 /* cancel a job  */
2688 INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
2689 {
2690         EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2691
2692         int ret = false;
2693         int err = EMAIL_ERROR_NONE;
2694
2695         if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1))  {
2696                 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2697                 err = EMAIL_ERROR_INVALID_PARAM;
2698                 goto FINISH_OFF;
2699         }
2700
2701         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2702
2703         EM_DEBUG_LOG("status[%d], type[%d], handle[%d]", g_event_que[handle].status, g_event_que[handle].type, handle);
2704
2705         if (g_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT)  {
2706                 fail_status_notify(&g_event_que[handle], EMAIL_ERROR_CANCELLED);
2707
2708                 switch (g_event_que[handle].type)  {
2709                         case EMAIL_EVENT_SEND_MAIL:
2710                         case EMAIL_EVENT_SEND_MAIL_SAVED:
2711                                 EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
2712                                 _sending_busy_unref();
2713                                 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2714                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2715                                 break;
2716                         case EMAIL_EVENT_DOWNLOAD_BODY:
2717                                 EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
2718                                 _receiving_busy_unref();
2719                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2720                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2721                                 break;
2722
2723                         case EMAIL_EVENT_SYNC_HEADER:
2724                         case EMAIL_EVENT_SYNC_HEADER_OMA:
2725                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
2726                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
2727                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
2728                                 EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
2729                                 _receiving_busy_unref();
2730                                 break;
2731
2732                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
2733                                 EM_DEBUG_LOG(" validate account waiting  :  cancel acc id  :  %d", g_event_que[handle].account_id);
2734                                 _receiving_busy_unref();
2735                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
2736                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
2737                                 break;
2738
2739                         case EMAIL_EVENT_DELETE_MAIL:
2740                         case EMAIL_EVENT_DELETE_MAIL_ALL:
2741                         case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
2742                         case EMAIL_EVENT_SAVE_MAIL:
2743                         case EMAIL_EVENT_MOVE_MAIL:
2744                         case EMAIL_EVENT_CREATE_MAILBOX:
2745                         case EMAIL_EVENT_DELETE_MAILBOX:
2746                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
2747                         case EMAIL_EVENT_SEARCH_ON_SERVER:
2748                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
2749                                 EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
2750                                 _receiving_busy_unref();
2751                                 break;
2752                         default:
2753                                 break;
2754                 }
2755         }
2756
2757         memset(g_event_que+handle, 0x00, sizeof(email_event_t));
2758         g_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2759
2760         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
2761
2762         ret = true;
2763
2764 FINISH_OFF:
2765         if (err_code != NULL)
2766                 *err_code = err;
2767         EM_DEBUG_FUNC_END();
2768         return ret;
2769 }
2770
2771 /* check thread status
2772 * 0 : stop job 1 : continue job
2773 */
2774 int emcore_check_send_mail_thread_status()
2775 {
2776         EM_DEBUG_FUNC_BEGIN();
2777
2778         if (g_send_active_que <= 0)
2779                 return true;
2780         EM_DEBUG_LOG("g_send_event_que[g_send_active_que[%d]].status[%d]", g_send_active_que, g_send_event_que[g_send_active_que].status);
2781         EM_DEBUG_FUNC_END();
2782         return (g_send_event_que[g_send_active_que].status == EMAIL_EVENT_STATUS_STARTED);
2783 }
2784
2785 INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
2786 {
2787         EM_DEBUG_FUNC_BEGIN();
2788         int error_code = EMAIL_ERROR_NONE;
2789         int i, event_count = EVENT_QUEUE_MAX, exit_flag = 0, sleep_count = 0;
2790
2791         for (i = 0 ; i < event_count; i++) {
2792                 if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2793                         EM_DEBUG_LOG("There is a live thread. %d", i);
2794                         if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2795                                 EM_DEBUG_LOG("And it is for account %d", g_event_que[i].account_id);
2796                                 emcore_cancel_thread(i, NULL, &error_code);
2797                         }
2798                 }
2799         }
2800
2801         while (exit_flag == 0 && sleep_count < 30) {
2802                 EM_DEBUG_LOG("Sleeping...");
2803                 usleep(100000);
2804                 EM_DEBUG_LOG("Wake up!");
2805                 sleep_count++;
2806                 exit_flag = 1;
2807                 for (i = 0 ; i < event_count; i++) {
2808                         if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
2809                                 EM_DEBUG_LOG("There is still a live thread. %d", i);
2810                                 if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
2811                                         EM_DEBUG_LOG("And it is for account %d. So, I should sleep for a while.", g_event_que[i].account_id);
2812                                         exit_flag = 0;
2813                                 }
2814                         }
2815                 }
2816         }
2817
2818         EM_DEBUG_LOG("Sleep count %d", sleep_count);
2819
2820         if (sleep_count >= 30)
2821                 error_code = EMAIL_ERROR_CANNOT_STOP_THREAD;
2822         else
2823                 error_code = EMAIL_ERROR_NONE;
2824         EM_DEBUG_FUNC_END();
2825         return error_code;
2826 }
2827
2828
2829 /* cancel send mail job  */
2830 INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
2831 {
2832         EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);
2833
2834         int ret = false;
2835         int err = EMAIL_ERROR_NONE;
2836
2837         if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1))  {
2838                 EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
2839                 err = EMAIL_ERROR_INVALID_PARAM;
2840                 goto FINISH_OFF;
2841         }
2842
2843         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2844
2845         EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2846
2847         if (g_send_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT)  {
2848                 fail_status_notify(&g_send_event_que[handle], EMAIL_ERROR_CANCELLED);
2849
2850                 switch (g_send_event_que[handle].type)  {
2851                         case EMAIL_EVENT_SEND_MAIL:
2852                         case EMAIL_EVENT_SEND_MAIL_SAVED:
2853                                 _sending_busy_unref();
2854                                 g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2855                                 if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_send_event_que[handle].account_id, NULL , g_send_event_que[handle].event_param_data_4, err))
2856                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
2857                                 break;
2858                         default:
2859                                 break;
2860                 }
2861         }
2862
2863         EM_DEBUG_LOG("send_mail_cancel");
2864         memset(g_send_event_que+handle, 0x00, sizeof(email_event_t));
2865         g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
2866
2867         EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);
2868
2869
2870         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
2871
2872         ret = true;
2873
2874 FINISH_OFF:
2875         if (err_code != NULL)
2876                 *err_code = err;
2877         EM_DEBUG_FUNC_END("ret [%d]", ret);
2878         return ret;
2879 }
2880
2881
2882 INTERNAL_FUNC int emcore_get_receiving_event_queue(email_event_t **event_queue, int *event_active_queue, int *err)
2883 {
2884         if (event_queue == NULL || event_active_queue == NULL) {
2885                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM event_queue[%p] event_active_queue[%p]", event_queue, event_active_queue);
2886
2887                 if (err)
2888                         *err = EMAIL_ERROR_INVALID_PARAM;
2889
2890                 return false;
2891         }
2892
2893         *event_queue = g_event_que;
2894         *event_active_queue = g_active_que;
2895
2896         return true;
2897 }
2898
2899 INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
2900 {
2901         EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);
2902
2903         if(event_data) {
2904                 EM_SAFE_FREE(event_data->event_param_data_1);
2905                 EM_SAFE_FREE(event_data->event_param_data_2);
2906                 EM_SAFE_FREE(event_data->event_param_data_3);
2907         }
2908
2909         EM_DEBUG_FUNC_END();
2910         return true;
2911 }
2912
2913 #ifdef __FEATURE_KEEP_CONNECTION__
2914 INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
2915 {
2916         return (unsigned int)g_srv_thread;
2917 }
2918 #endif /*  __FEATURE_KEEP_CONNECTION__ */
2919
2920 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2921
2922 INTERNAL_FUNC int emcore_get_pbd_thd_state()
2923 {
2924         int pbd_thd_state = false;
2925         ENTER_CRITICAL_SECTION(_state_variables_lock);
2926         pbd_thd_state = g_pbd_thd_state;
2927         LEAVE_CRITICAL_SECTION(_state_variables_lock);
2928         return pbd_thd_state;
2929 }
2930
2931 static int emcore_set_pbd_thd_state(int flag)
2932 {
2933         ENTER_CRITICAL_SECTION(_state_variables_lock);
2934         g_pbd_thd_state = flag;
2935         LEAVE_CRITICAL_SECTION(_state_variables_lock);
2936
2937         return g_pbd_thd_state;
2938 }
2939
2940 INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
2941 {
2942         EM_DEBUG_FUNC_BEGIN();
2943         EM_DEBUG_FUNC_END();
2944         return (unsigned int)g_partial_body_thd;
2945 }
2946
2947 static int emcore_clear_bulk_pbd_que(int *err_code)
2948 {
2949         EM_DEBUG_FUNC_BEGIN();
2950
2951         int ret = true;
2952         int error = EMAIL_ERROR_NONE;
2953         int i = 0;
2954
2955         for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
2956                 if (g_partial_body_bulk_dwd_que[i].event_type) {
2957                         if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error))                                        {
2958                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
2959                                 ret = false;
2960                                 break;
2961                         }
2962                 }
2963         }
2964
2965         if (NULL != err_code)
2966                 *err_code = error;
2967         EM_DEBUG_FUNC_END();
2968         return ret;
2969 }
2970
2971 static void emcore_pb_thd_set_local_activity_continue(int flag)
2972 {
2973         EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);
2974
2975         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2976
2977         g_pb_thd_local_activity_continue = flag;
2978
2979         if (true == flag) {
2980                 WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
2981         }
2982
2983         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2984         EM_DEBUG_FUNC_END();
2985 }
2986
2987 static
2988 int emcore_pb_thd_can_local_activity_continue()
2989 {
2990         EM_DEBUG_FUNC_BEGIN();
2991
2992         int ret = false;
2993
2994         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2995
2996         ret = g_pb_thd_local_activity_continue;
2997
2998         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2999         EM_DEBUG_FUNC_END();
3000         return ret;
3001
3002 }
3003
3004 INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
3005 {
3006         EM_DEBUG_FUNC_BEGIN();
3007
3008         int ret = true;
3009         int error = EMAIL_ERROR_NONE;
3010         int i = 0;
3011
3012         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3013
3014         if (true == g_partial_body_thd_queue_empty) {
3015                 EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
3016         }
3017         else {
3018                 for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
3019                         if (g_partial_body_thd_event_que[i].event_type) {
3020                                 if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error))                                       {
3021                                         EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
3022                                         ret = false;
3023                                         break;
3024                                 }
3025                         }
3026                 }
3027
3028                 g_partial_body_thd_queue_empty = true;
3029                 g_partial_body_thd_queue_full = false;
3030         }
3031         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3032
3033         if (NULL != err_code)
3034                 *err_code = error;
3035         EM_DEBUG_FUNC_END();
3036         return ret;
3037 }
3038
3039 INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
3040 {
3041         EM_DEBUG_FUNC_BEGIN();
3042
3043         int ret = false;
3044
3045         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3046
3047         ret = g_partial_body_thd_queue_empty;
3048
3049         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3050         EM_DEBUG_FUNC_END("ret [%d]", ret);
3051         return ret;
3052 }
3053 INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
3054 {
3055         EM_DEBUG_FUNC_BEGIN();
3056
3057         int ret = false;
3058
3059         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3060
3061         ret = g_partial_body_thd_queue_full;
3062
3063         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3064         EM_DEBUG_FUNC_END();
3065         return ret;
3066 }
3067
3068 /*
3069 Himanshu[h.gahalut] :  If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
3070 then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
3071 through _partial_body_thd_event_queue_lock mutex.
3072
3073 No mutex is used inside this API so that we can also use it to copy partial body events which are not a part of global event_data queue
3074
3075 Precautions :
3076
3077 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3078 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3079
3080 */
3081
3082 static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
3083 {
3084         EM_DEBUG_FUNC_BEGIN();
3085         int error = EMAIL_ERROR_NONE;
3086         int ret = false;
3087
3088         if (NULL == src || NULL == dest) {
3089                 EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
3090                 error = EMAIL_ERROR_INVALID_PARAM;
3091                 goto FINISH_OFF;
3092         }
3093
3094         dest->account_id = src->account_id;
3095         dest->mail_id = src->mail_id;
3096         dest->server_mail_id = src->server_mail_id;
3097         dest->activity_id = src->activity_id;
3098         dest->mailbox_id = src->mailbox_id;
3099         dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
3100         dest->activity_type = src->activity_type;
3101         dest->event_type = src->event_type;
3102
3103         EM_DEBUG_LOG("dest->account_id[%d], dest->mail_id[%d], dest->server_mail_id [%lu]", dest->account_id, dest->mail_id , dest->server_mail_id);
3104
3105         ret = true;
3106
3107         FINISH_OFF:
3108
3109         if (NULL != error_code)
3110                 *error_code = error;
3111
3112         return ret;
3113
3114 }
3115
3116 /*
3117 Himanshu[h.gahalut] :  If emcore_free_partial_body_thd_event_cell API is used to free a cell of partial body thread event_data queue,
3118 it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.
3119
3120 No mutex is used inside this API so that we can also use it to free partial body events which are not a part of global event_data queue
3121
3122 Precautions :
3123
3124 _partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
3125 Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.
3126
3127 */
3128
3129 INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3130 {
3131         EM_DEBUG_FUNC_BEGIN();
3132
3133         if (NULL == partial_body_thd_event) {
3134                 *error_code = EMAIL_ERROR_INVALID_PARAM;
3135                 return false;
3136         }
3137
3138         email_event_partial_body_thd *pbd_event = partial_body_thd_event;
3139
3140         /*Free character pointers in event_data cell */
3141         EM_SAFE_FREE(pbd_event->mailbox_name);
3142         memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
3143         EM_DEBUG_FUNC_END();
3144         return true;
3145 }
3146
3147 INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3148 {
3149         EM_DEBUG_FUNC_BEGIN();
3150
3151         if (NULL == partial_body_thd_event)  {
3152                 EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);
3153
3154                 if (error_code != NULL) {
3155                         *error_code = EMAIL_ERROR_INVALID_PARAM;
3156                 }
3157                 return false;
3158         }
3159
3160         int ret = false;
3161         int error = EMAIL_ERROR_NONE;
3162         int empty_cell_index = -1;
3163         int index = 0;
3164         int count = 0;
3165
3166         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3167
3168         /* find a cell in queue which is empty */
3169
3170         for (count = 0, index =  g_partial_body_thd_next_event_idx; count < TOTAL_PARTIAL_BODY_EVENTS;) {
3171                 if  (g_partial_body_thd_event_que[index].event_type) {
3172                         ++index;
3173                         ++count;
3174
3175                         if (index == TOTAL_PARTIAL_BODY_EVENTS) {
3176                                 index = 0;
3177                         }
3178                 }
3179                 else {
3180                         /*Found empty Cell*/
3181
3182                         empty_cell_index =       index;
3183                         break;
3184                 }
3185         }
3186
3187         if (-1 != empty_cell_index) {
3188                 if (false == emcore_copy_partial_body_thd_event(partial_body_thd_event, g_partial_body_thd_event_que+empty_cell_index , &error)) {
3189                         EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
3190                 }
3191                 else {
3192                         g_partial_body_thd_queue_empty = false;
3193
3194                         if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
3195                                 /*This is the last event_data inserted in queue after its insertion, queue is full */
3196                                 g_partial_body_thd_queue_full = true;
3197
3198                         }
3199
3200                         WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
3201
3202                         ret = true;
3203                 }
3204         }
3205         else {
3206                 EM_DEBUG_LOG(" partial body thread event_data queue is full ");
3207                 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
3208
3209                 g_partial_body_thd_queue_full = true;
3210                 g_partial_body_thd_queue_empty = false;
3211
3212         }
3213
3214         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3215
3216         if (NULL != error_code) {
3217                 *error_code = error;
3218         }
3219
3220         return ret;
3221
3222 }
3223
3224 /* h.gahlaut :  Return true only if event_data is retrieved successfully */
3225
3226 static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
3227 {
3228         EM_DEBUG_FUNC_BEGIN();
3229
3230         int ret = false;
3231         int error = EMAIL_ERROR_NONE;
3232         int index = 0;
3233
3234         /* Lock Mutex to protect event_data queue and associated global variables variables*/
3235
3236         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3237
3238         index = g_partial_body_thd_next_event_idx;
3239
3240         if (0 == g_partial_body_thd_event_que[index].event_type) {
3241                 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
3242                 g_partial_body_thd_queue_empty = true;
3243                 g_partial_body_thd_queue_full = false;
3244         }
3245         else {
3246                 /*Copy the event_data from queue to return it and free the event_data in queue */
3247                 if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
3248                         EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
3249                 else {
3250                         if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
3251                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
3252                         else {
3253
3254                                 g_partial_body_thd_queue_full = false;
3255                                 g_partial_body_thd_next_event_idx = ++index;
3256
3257                                 if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
3258                                         g_partial_body_thd_next_event_idx = 0;
3259
3260                                 /* If the event_data retrieved was the only event_data present in queue,
3261                                 we need to set g_partial_body_thd_queue_empty to true
3262                                 */
3263
3264                                 if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
3265                                         g_partial_body_thd_queue_empty = true;
3266                                 }
3267
3268                                 ret = true;
3269                         }
3270                 }
3271         }
3272
3273         /* Unlock Mutex */
3274
3275         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3276
3277         if (error_code)
3278                 *error_code = error;
3279
3280         return ret;
3281
3282 }
3283
3284 gpointer partial_body_download_thread(gpointer data)
3285 {
3286         EM_DEBUG_FUNC_BEGIN();
3287
3288         int err = EMAIL_ERROR_NONE;
3289         email_session_t *session = NULL;
3290         email_event_partial_body_thd partial_body_thd_event;
3291
3292         EM_DEBUG_LOG(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());
3293
3294         /* Open connection with DB */
3295
3296         if (false == emstorage_open(&err))  {
3297                 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
3298                 return false;
3299         }
3300
3301         /* Start the continuous loop */
3302
3303         while (g_partial_body_thd_loop) {
3304                 /*  Get an empty session  */
3305                 /*  TODO :  Mutex should be used in session APIs */
3306
3307                 if (false == emcore_get_empty_session(&session))
3308                         EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
3309                 else {  /* Get and Event from the Partial Body thread Event Queue */
3310                         memset(&partial_body_thd_event, 0x00, sizeof(email_event_partial_body_thd));
3311
3312                         if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
3313                                 if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
3314                                         EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
3315                                 else {
3316                                         EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
3317
3318                                         /*  Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count  */
3319                                         if (!g_partial_body_bulk_dwd_queue_empty) {
3320                                                 partial_body_thd_event.event_type = 0;
3321                                                 partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
3322                                                 partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
3323                                                 partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name);
3324
3325                                                 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3326                                                         EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3327
3328                                                 emcore_pb_thd_set_local_activity_continue(true);
3329                                         }
3330
3331                                         if (true == emcore_pb_thd_can_local_activity_continue()) {
3332                                                 /*Check for local Activities */
3333                                                 int is_local_activity_event_inserted = false;
3334
3335                                                 if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &err)) {
3336                                                         EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
3337                                                 }
3338                                                 else {
3339                                                         if (true == is_local_activity_event_inserted) {
3340                                                                 emcore_pb_thd_set_local_activity_continue(false);
3341
3342                                                                 emcore_clear_session(session);
3343                                                                 continue;
3344                                                         }
3345                                                 }
3346                                         }
3347
3348                                         EM_DEBUG_LOG(" Partial Body Thread is going to sleep");
3349
3350                                         emcore_set_pbd_thd_state(false);
3351
3352                                         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3353                                         SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
3354                                         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
3355
3356                                         EM_DEBUG_LOG(" Partial Body Thread wakes up ");
3357
3358                                         emcore_set_pbd_thd_state(true);
3359                                 }
3360
3361                         }
3362                         else {
3363                                 EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
3364
3365                                 /* Since all events are network operations dnet init and sleep control is
3366                                 done before entering switch block*/
3367
3368                                 emdevice_set_dimming_on_off(false, NULL);
3369
3370                                 if (!emnetwork_check_network_status( &err))  {
3371                                         EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
3372                                 }
3373                                 else {
3374                                         /*  Process events  */
3375                                         EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
3376
3377                                         switch (partial_body_thd_event.event_type) {
3378                                                 case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD:  {
3379                                                         if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
3380                                                                 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
3381                                                         }
3382                                                         break;
3383                                                 }
3384                                                 case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD:  {
3385                                                         partial_body_thd_event.event_type = 0;
3386
3387                                                         /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
3388                                                         *   g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
3389                                                         if (false == emcore_is_partial_body_thd_que_empty())
3390                                                                 break;
3391                                                         if (!g_partial_body_bulk_dwd_queue_empty)
3392                                                                 break;
3393
3394                                                         if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
3395                                                                 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
3396                                                         break;
3397                                                 }
3398                                                 default:
3399                                                         EM_DEBUG_EXCEPTION(" Warning :  Default case entered. This should not happen ");
3400                                                         break;
3401                                         }
3402                                 }
3403
3404                                 if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
3405                                         EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
3406
3407                                 emdevice_set_dimming_on_off(true, NULL);
3408                        }
3409
3410                        emcore_clear_session(session);
3411                }
3412         }
3413
3414         /* If something is added to end thread in future for any case then if thread is holding any resources
3415         define a function emcore_partial_body_thd_loop_stop to release resources and call it
3416         here to end thread */
3417         return SUCCESS;
3418 }
3419
3420 INTERNAL_FUNC int emcore_start_thread_for_downloading_partial_body(int *err_code)
3421 {
3422         EM_DEBUG_FUNC_BEGIN();
3423
3424         int i = 0, thread_error = -1;
3425
3426         /* Clear Partial Body Event Queue*/
3427         memset(&g_partial_body_thd_event_que, 0x00, sizeof(g_partial_body_thd_event_que));
3428
3429         for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
3430                 g_partial_body_thd_event_que[i].mailbox_name = NULL;
3431         g_partial_body_thd_event_que[i].mailbox_id = 0;
3432     }
3433
3434         if (g_partial_body_thd)  {
3435                 EM_DEBUG_EXCEPTION("partial body thread is already running...");
3436                 if (err_code != NULL)
3437                         *err_code = EMAIL_ERROR_UNKNOWN;
3438
3439                 return true;
3440         }
3441
3442         g_partial_body_thd_next_event_idx = 0;
3443         g_partial_body_thd_loop = 1;
3444         g_partial_body_thd_queue_empty = true;
3445         g_partial_body_thd_queue_full = false;
3446
3447         INITIALIZE_CONDITION_VARIABLE(_partial_body_thd_cond);
3448
3449         /* create thread */
3450         /* THREAD_CREATE_JOINABLE(g_partial_body_thd, partial_body_download_thread, thread_error); */
3451         THREAD_CREATE(g_partial_body_thd, partial_body_download_thread, NULL, thread_error);
3452
3453         if (thread_error != 0) {
3454                 EM_DEBUG_EXCEPTION("cannot make thread...");
3455                 if (err_code != NULL)
3456                         *err_code = EMAIL_ERROR_UNKNOWN;
3457                 return FAILURE;
3458         }
3459
3460         if (err_code != NULL)
3461                 *err_code = EMAIL_ERROR_NONE;
3462
3463         return false;
3464
3465 }
3466
3467 /*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
3468 static int emcore_partial_body_bulk_flush(int *error_code)
3469 {
3470         EM_DEBUG_FUNC_BEGIN();
3471         int error = EMAIL_ERROR_NONE;
3472         int ret = false;
3473         MAILSTREAM *stream = NULL;
3474         void *tmp_stream = NULL;
3475
3476         if (!emcore_connect_to_remote_mailbox(g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
3477                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3478                 goto FINISH_OFF;
3479         }
3480         stream = (MAILSTREAM *)tmp_stream;
3481
3482         /*  Call bulk download here */
3483         if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
3484                 EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3485                 goto FINISH_OFF;
3486         }
3487
3488         ret = true;
3489 FINISH_OFF:
3490
3491         emcore_close_mailbox(0, stream);
3492         stream = NULL;
3493
3494         g_partial_body_bulk_dwd_next_event_idx = 0;
3495         g_partial_body_bulk_dwd_queue_empty = true;
3496
3497         if (false == emcore_clear_bulk_pbd_que(&error))
3498                 EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);
3499
3500         if (NULL != error_code)
3501                 *error_code = error;
3502
3503         EM_DEBUG_FUNC_END("ret [%d]", ret);
3504         return ret;
3505 }
3506
3507
3508 /* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
3509 INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
3510 {
3511         EM_DEBUG_FUNC_BEGIN();
3512         int error = EMAIL_ERROR_NONE;
3513         int num_activity = 0;
3514         int ret = false;
3515         int count = 0;
3516         int i = 0, m = 0;
3517         MAILSTREAM *stream = NULL;
3518         void *tmp_stream = NULL;
3519         email_event_partial_body_thd *activity_data_list = NULL;
3520         int *mailbox_list = NULL;
3521
3522         if (NULL == pbd_event)
3523     {
3524             EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);
3525
3526             error = EMAIL_ERROR_INVALID_PARAM;
3527             goto FINISH_OFF;
3528     }
3529
3530         /*Check if the event_data is to flush the event_data que array */
3531         if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
3532                 EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
3533                 /*Check if the mailbox name and account id for this event_data is same as the mailbox name and account id for earlier events saved in download que array
3534                 then append this event_data also to download que array */
3535                 if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
3536                         (g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
3537                         EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
3538                         EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
3539                         if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
3540                                 if (false == emcore_partial_body_bulk_flush(&error)) {
3541                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3542                                         goto FINISH_OFF;
3543                                 }
3544                         }
3545                 }
3546                 else  {
3547                         EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
3548                         EM_DEBUG_LOG("Flush the current que if not empty");
3549                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
3550                         if (!g_partial_body_bulk_dwd_queue_empty) {
3551                                 if (false == emcore_partial_body_bulk_flush(&error)) {
3552                                         EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3553                                         goto FINISH_OFF;
3554                                 }
3555                         }
3556                 }
3557                 /*Add the event_data to the download que array */
3558                 if (false == emcore_copy_partial_body_thd_event(pbd_event, g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
3559                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
3560                 else {
3561                         g_partial_body_bulk_dwd_queue_empty = false;
3562                         g_partial_body_bulk_dwd_next_event_idx++;
3563                         EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
3564                 }
3565         }
3566         else if (pbd_event->activity_type) {
3567                 int *account_list = NULL;
3568                 int account_count = 0;
3569
3570                 EM_DEBUG_LOG("Event is coming from local activity.");
3571                 /* Get all the accounts for which local activities are pending */
3572                 if (false == emstorage_get_pbd_account_list(&account_list, &account_count, false, &error)) {
3573                                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3574                                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3575                                 goto FINISH_OFF;
3576                 }
3577
3578                 for (m = 0; m < account_count; ++m) {
3579                         /* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
3580                         if (false == emstorage_get_pbd_mailbox_list(account_list[m], &mailbox_list, &count, false, &error)) {
3581                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
3582                                         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3583                                         goto FINISH_OFF;
3584                         }
3585
3586                         for (i = 0; i < count; i++) {
3587                                 int k = 0;
3588                                 int activity_count = 0;
3589
3590                                 if (!emcore_connect_to_remote_mailbox(account_list[m], mailbox_list[i], (void **)&tmp_stream, &error))  {
3591                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
3592                                         stream = NULL;
3593                                         goto FINISH_OFF;
3594                                 }
3595
3596                                 stream = (MAILSTREAM *)tmp_stream;
3597
3598                                 if (false == emstorage_get_mailbox_pbd_activity_count(account_list[m], mailbox_list[i], &activity_count, false, &error)) {
3599                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
3600                                         continue;
3601                                 }
3602
3603                                 if (activity_count > 0) {
3604                                         int temp_error = EMAIL_ERROR_NONE;
3605                                         int j = 0;
3606                                         int iter = 0;
3607                                         int remainder = 0;
3608                                         int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3609                                         int index = 0;
3610
3611                                         if (false == emstorage_get_pbd_activity_data(account_list[j], mailbox_list[i], &activity_data_list, &num_activity,  false, &error))
3612                                                 EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);
3613
3614                                         if (NULL == activity_data_list) {
3615                                                 EM_DEBUG_EXCEPTION(" activity_data_list is null..");
3616                                                 continue;
3617                                         }
3618
3619                                         remainder = num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
3620                                         iter = num_activity/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1  :  0);
3621
3622                                         for (j = 0; j < iter; j++) {
3623                                                 if ((iter == (j+1)) && (remainder != 0))
3624                                                         num = remainder;
3625
3626                                                 /*Call bulk download here */
3627                                                 if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
3628                                                         EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
3629                                                         temp_error = EMAIL_ERROR_NO_RESPONSE;
3630                                                 }
3631
3632                                                 for (k = 0; k < num; k++) {
3633                                                         if (activity_data_list[index + k].activity_type)
3634                                                                 emcore_free_partial_body_thd_event(activity_data_list + index + k, &error);
3635                                                         else
3636                                                                 break;
3637                                                 }
3638                                                 index += num;
3639
3640                                                 if (false == emcore_is_partial_body_thd_que_empty()) {
3641                                                         ret = true;
3642                                                         goto FINISH_OFF;                /* Stop Local Activity Sync */
3643                                                 }
3644                                                 if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
3645                                                         temp_error = EMAIL_ERROR_NONE;
3646                                                         break;
3647                                                 }
3648                                         }
3649                                 }
3650                         }
3651                         emcore_close_mailbox(0, stream);
3652                         stream = NULL;
3653                         tmp_stream = NULL;
3654                 }
3655
3656                 /* After completing one cycle of local activity sync ,
3657                 local activity continue variable should be set to false. */
3658
3659                 emcore_pb_thd_set_local_activity_continue(false);
3660         }
3661         else /* Event-type is 0 which means Event is to flush the que when no more events are present in g_partial_body_thd_event_que */ {
3662                 /*Check if events have arrived in g_partial_body_thd_event_que */
3663                 if (false == emcore_is_partial_body_thd_que_empty()) {
3664                         EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
3665                         ret = true;
3666                         goto FINISH_OFF;                /* Stop Local Activity Sync */
3667                 }
3668                 if (false == emcore_partial_body_bulk_flush(&error)) {
3669                         EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
3670                         goto FINISH_OFF;
3671                 }
3672         }
3673
3674         ret = true;
3675
3676 FINISH_OFF:
3677
3678         EM_SAFE_FREE(mailbox_list);
3679
3680         if (activity_data_list) {
3681                 for (i = 0; i < num_activity; i++) {
3682                         if (activity_data_list[i].activity_type)
3683                                 emcore_free_partial_body_thd_event(activity_data_list + i, &error);
3684                         else
3685                                 break;
3686                 }
3687         }
3688
3689         emcore_close_mailbox(0, stream);
3690         stream = NULL;
3691
3692         if (NULL != error_code)
3693                 *error_code = error;
3694         EM_DEBUG_FUNC_END("ret [%d]", ret);
3695         return ret;
3696 }
3697
3698 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */