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