Fixed the bug for tizen 3.0
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-event.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22 #include <stdio.h>
23 #include <glib.h>
24 #include <malloc.h>
25 #include <pthread.h>
26 #include <vconf.h>
27 #include <signal.h>
28 #include <contacts.h>
29 #include <contacts_internal.h>
30 #include "c-client.h"
31 #include "email-convert.h"
32 #include "email-storage.h"
33 #include "email-network.h"
34 #include "email-device.h"
35 #include "email-utilities.h"
36 #include "email-daemon.h"
37 #include "email-daemon-auto-poll.h"
38 #include "email-core-global.h"
39 #include "email-core-account.h"
40 #include "email-core-event.h"
41 #include "email-core-utils.h"
42 #include "email-core-mailbox.h"
43 #include "email-core-imap-mailbox.h"
44 #include "email-core-mail.h"
45 #include "email-core-mailbox-sync.h"
46 #include "email-core-smtp.h"
47 #include "email-core-utils.h"
48 #include "email-core-signal.h"
49 #include "email-debug-log.h"
50
51 #include "email-types.h"
52 #include "email-internal-types.h"
53
54 extern thread_t g_srv_thread;
55 extern pthread_cond_t  _event_available_signal;
56 extern pthread_mutex_t *_event_queue_lock;
57 extern pthread_mutex_t *_event_handle_map_lock;
58 extern pthread_mutex_t *_event_callback_table_lock;
59 extern GQueue *g_event_que;
60 extern int g_event_loop;
61 extern int recv_thread_run;
62
63 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
64 extern pthread_cond_t  _auto_downalod_available_signal;
65 #endif
66
67 static void *worker_event_queue(void *arg);
68 static int event_handler_EMAIL_EVENT_SYNC_HEADER(char *multi_user_name, int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error);
69 static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(char *multi_user_name, email_account_t *account, int handle_to_be_published, int *error);
70 static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(char *multi_user_name, email_account_t *input_account, int input_handle_to_be_published);
71 static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(char *multi_user_name, int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error);
72 static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error);
73 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id);
74 static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int account_id, int mail_id, int option, int handle_to_be_published, int *error);
75 static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name, int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error);
76 static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error);
77 static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(char *multi_user_name, int account_id, int handle_to_be_published, int *error);
78 static int event_handler_EMAIL_EVENT_UPDATE_MAIL(char *multi_user_name, 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);
79 static int event_handler_EMAIL_EVENT_SAVE_MAIL(char *multi_user_name, int input_account_id, int input_mail_id, int input_handle_to_be_published);
80 static int event_handler_EMAIL_EVENT_MOVE_MAIL(char *multi_user_name, 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);
81 static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(char *multi_user_name, int mailbox_id, int on_server, int recursive, int handle_to_be_published);
82 static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(char *multi_user_name, email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published);
83 static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(char *multi_user_name, int mail_id, int event_handle, int *error);
84 static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int input_account_id, int input_mailbox_id, int input_from_server, int *error);
85 static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error);
86 static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int account_id, char *maibox_name, int handle_to_be_published, int *error);
87 static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int account_id, int mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, int handle_to_be_published);
88 static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published);
89
90 static void* worker_send_event_queue(void *arg);
91
92 extern thread_t g_send_srv_thread;
93 extern pthread_cond_t  _send_event_available_signal;
94 extern pthread_mutex_t *_send_event_queue_lock;
95 extern pthread_mutex_t *_send_event_handle_map_lock;
96
97 extern GQueue *g_send_event_que;
98 extern int g_send_event_loop;
99 extern int send_thread_run;
100
101 extern thread_t g_partial_body_thd;
102 extern pthread_cond_t  _partial_body_thd_cond;
103 extern pthread_mutex_t _partial_body_thd_event_queue_lock;
104
105 extern email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];
106 extern email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
107 extern int g_partial_body_thd_next_event_idx;
108 //extern int g_partial_body_thd_loop;
109 extern int g_partial_body_thd_queue_empty;
110 extern int g_partial_body_thd_queue_full;
111 extern int g_partial_body_bulk_dwd_queue_empty;
112
113 extern email_event_t *sync_failed_event_data;
114
115 static gpointer partial_body_download_thread(gpointer data);
116
117 #ifdef __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__
118
119 #include <openssl/crypto.h>
120
121 #define MAX_THREAD_NUMBER   100
122
123 static pthread_mutex_t *lock_cs;
124 static long *lock_count;
125
126 void pthreads_locking_callback(int mode, int type, char *file, int line)
127 {
128         if (mode & CRYPTO_LOCK) {
129                 pthread_mutex_lock(&(lock_cs[type]));
130                 lock_count[type]++;
131         }
132         else {
133                 pthread_mutex_unlock(&(lock_cs[type]));
134         }
135 }
136
137 unsigned long pthreads_thread_id(void)
138 {
139         return (unsigned long)pthread_self();
140 }
141
142 INTERNAL_FUNC void emdaemon_setup_handler_for_open_ssl_multithread(void)
143 {
144         EM_DEBUG_FUNC_BEGIN();
145         int i = 0;
146
147         lock_cs    = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
148         lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
149
150         for (i = 0; i < CRYPTO_num_locks(); i++) {
151                 lock_count[i] = 0;
152                 pthread_mutex_init(&(lock_cs[i]),NULL);
153         }
154
155         CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
156         CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
157         EM_DEBUG_FUNC_END();
158 }
159
160 INTERNAL_FUNC void emdaemon_cleanup_handler_for_open_ssl_multithread(void)
161 {
162         EM_DEBUG_FUNC_BEGIN();
163         int i = 0;
164
165         CRYPTO_set_locking_callback(NULL);
166         for (i = 0; i < CRYPTO_num_locks(); i++)
167         {
168                 pthread_mutex_destroy(&(lock_cs[i]));
169                 EM_DEBUG_LOG("%8ld:%s",lock_count[i], CRYPTO_get_lock_name(i));
170         }
171         OPENSSL_free(lock_cs);
172         OPENSSL_free(lock_count);
173
174         EM_DEBUG_FUNC_END();
175 }
176
177 #endif /* __FEATURE_OPEN_SSL_MULTIHREAD_HANDLE__ */
178
179 /* start api event_data loop */
180 INTERNAL_FUNC int emdaemon_start_event_loop(int *err_code)
181 {
182         EM_DEBUG_FUNC_BEGIN();
183         int thread_error;
184
185         if (err_code != NULL)
186                 *err_code = EMAIL_ERROR_NONE;
187
188         g_event_que = g_queue_new();
189         g_queue_init(g_event_que);
190
191         if (g_srv_thread) {
192                 EM_DEBUG_EXCEPTION("service thread is already running...");
193                 if (err_code != NULL)
194                         *err_code = EMAIL_ERROR_UNKNOWN;
195                 return true;
196         }
197
198         g_event_loop = 1;
199
200         /* initialize lock */
201         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
202         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_handle_map_lock);
203         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
204
205         emcore_initialize_event_callback_table();
206
207         /* create thread */
208         THREAD_CREATE(g_srv_thread, worker_event_queue, NULL, thread_error);
209
210         if (thread_error != 0) {
211                 EM_DEBUG_EXCEPTION("cannot create thread");
212                 g_event_loop = 0;
213                 if (err_code != NULL)
214                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
215                 return FAILURE;
216         }
217
218         if (err_code != NULL)
219                 *err_code = EMAIL_ERROR_NONE;
220
221         return false;
222 }
223
224 /*Send event_data loop*/
225 INTERNAL_FUNC int emdaemon_start_event_loop_for_sending_mails(int *err_code)
226 {
227         EM_DEBUG_FUNC_BEGIN();
228         int thread_error = -1;
229
230         if (err_code != NULL)
231                 *err_code = EMAIL_ERROR_NONE;
232
233         g_send_event_que = g_queue_new();
234         g_queue_init(g_send_event_que);
235
236         if (g_send_srv_thread) {
237                 EM_DEBUG_EXCEPTION("send service thread is already running...");
238                 if (err_code != NULL)
239                         *err_code = EMAIL_ERROR_UNKNOWN;
240                 return true;
241         }
242
243         g_send_event_loop = 1;
244
245         /* initialize lock */
246         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
247         INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_handle_map_lock);
248         INITIALIZE_CONDITION_VARIABLE(_send_event_available_signal);
249
250         /* create thread */
251         THREAD_CREATE_JOINABLE(g_send_srv_thread, worker_send_event_queue, thread_error);
252
253         if (thread_error != 0) {
254                 EM_DEBUG_EXCEPTION("cannot make thread...");
255                 g_send_event_loop = 0;
256                 if (err_code != NULL)
257                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
258                 return FAILURE;
259         }
260
261         if (err_code != NULL)
262                 *err_code = EMAIL_ERROR_NONE;
263
264         EM_DEBUG_FUNC_END();
265         return SUCCESS;
266 }
267
268 static void* worker_event_queue(void *arg)
269 {
270         EM_DEBUG_FUNC_BEGIN();
271
272         int err = EMAIL_ERROR_NONE;
273         int is_storage_full = false;
274         int noti_id = 0;
275         email_event_t *event_data = NULL;
276         email_event_t *started_event = NULL;
277         emstorage_account_tbl_t *account_tbl = NULL;
278         int handle_to_be_published = 0;
279         int pbd_thd_state = 0;
280         int send_event_que_state = 0;
281
282         if (!emstorage_open(NULL, &err)) {
283                 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
284                 return false;
285         }
286
287         /* check that event_data loop is continuous */
288         while (emcore_event_loop_continue()) {
289
290                 pbd_thd_state = emcore_get_pbd_thd_state();
291                 send_event_que_state = emcore_is_send_event_queue_empty();
292
293                 /* get a event_data from event_data queue */
294                 ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
295                 if (!emcore_retrieve_event(&event_data, &err)) {
296                         /* no event_data pending */
297                         if (err != EMAIL_ERROR_EVENT_QUEUE_EMPTY) {
298                                 LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
299                                 continue;
300                         }
301
302                         recv_thread_run = 0;
303                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_RECEVING_WORKER, true, NULL);
304
305 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
306                         int wifi_status = 0;
307                         if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
308                                 EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
309                         }
310
311                         EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
312
313                         if (!pbd_thd_state && send_event_que_state && wifi_status > 1) {
314                                 WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
315                         }
316 #endif
317
318                         //go to sleep when queue is empty
319                         SLEEP_CONDITION_VARIABLE(_event_available_signal, *_event_queue_lock);
320                         EM_DEBUG_LOG("Wake up by _event_available_signal");
321                         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
322                 }
323                 else {
324                         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
325                         EM_DEBUG_LOG_DEV(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
326
327                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_RECEVING_WORKER, false, NULL);
328
329                         handle_to_be_published = event_data->handle;
330                         EM_DEBUG_LOG("Handle to be Published  [%d]", handle_to_be_published);
331                         recv_thread_run = 1;
332
333                         /* Handling storage full */
334                         is_storage_full = false;
335                         if (event_data->type == EMAIL_EVENT_SYNC_HEADER ||
336                                 event_data->type == EMAIL_EVENT_SYNC_HEADER_OMA ||
337                                 event_data->type == EMAIL_EVENT_DOWNLOAD_BODY ||
338                                 event_data->type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
339
340                                 if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
341                                         EM_DEBUG_EXCEPTION("Storage is full");
342                                         switch (event_data->type) {
343                                                 case EMAIL_EVENT_SYNC_HEADER:
344                                                 case EMAIL_EVENT_SYNC_HEADER_OMA:
345                                                         noti_id = NOTI_DOWNLOAD_FAIL;
346                                                         break;
347                                                 case EMAIL_EVENT_DOWNLOAD_BODY:
348                                                         noti_id = NOTI_DOWNLOAD_BODY_FAIL;
349                                                         break;
350                                                 case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
351                                                         noti_id = NOTI_DOWNLOAD_ATTACH_FAIL;
352                                                         break;
353                                                 default:
354                                                         break;
355                                         }
356
357                                         if (!emcore_notify_network_event(noti_id, event_data->account_id, NULL,  handle_to_be_published, err))
358                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
359                                         is_storage_full = true;
360                                 }
361                         }
362
363
364
365                         if (event_data->account_id > 0) {
366                                 if (!emstorage_get_account_by_id(event_data->multi_user_name, event_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
367                                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
368                                 }
369                         }
370
371                         if (account_tbl)
372                                 EM_DEBUG_LOG("account_id : [%d], sync_disabled : [%d]", event_data->account_id, account_tbl->sync_disabled);
373
374                         if (!account_tbl || account_tbl->sync_disabled == 0) {
375                                 switch (event_data->type) {
376                                         case EMAIL_EVENT_SYNC_IMAP_MAILBOX:  /* get imap mailbox list  */
377                                                 if (!emcore_sync_mailbox_list(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_3, handle_to_be_published, &err))
378                                                         EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
379                                                 break;
380
381                                         case EMAIL_EVENT_SYNC_HEADER:  /* synchronize mail header  */
382                                                 if (is_storage_full == false)
383                                                         event_handler_EMAIL_EVENT_SYNC_HEADER(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_5, handle_to_be_published,  &err);
384                                                 break;
385
386                                         case EMAIL_EVENT_SYNC_HEADER_OMA:  /*  synchronize mail header for OMA */
387                                                 if (is_storage_full == false)
388                                                         event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_1, handle_to_be_published, &err);
389                                                 break;
390
391                                         case EMAIL_EVENT_DOWNLOAD_BODY:  /*  download mail body  */
392                                                 if (is_storage_full == false)
393                                                         event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
394                                                 break;
395
396                                         case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:  /*  download attachment */
397                                                 if (is_storage_full == false)
398                                                         event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data->multi_user_name, event_data->account_id, (int)event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
399                                                 break;
400
401                                         case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
402                                                 event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(event_data->multi_user_name, (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);
403                                                 break;
404
405                                         case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
406                                                 event_handler_EMAIL_EVENT_DELETE_MAIL(event_data->multi_user_name, event_data->account_id, (int*)event_data->event_param_data_3, event_data->event_param_data_4, event_data->event_param_data_5, &err);
407                                                 break;
408
409                                         case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
410                                                 event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, (int)event_data->event_param_data_5, &err);
411                                                 break;
412 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
413                                         case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
414                                                 event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(event_data->multi_user_name, (int)event_data->event_param_data_4, handle_to_be_published, &err);
415                                                 break;
416 #endif
417
418                                         case EMAIL_EVENT_CREATE_MAILBOX: 
419                                                 err = event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data->multi_user_name, (email_mailbox_t *)event_data->event_param_data_1, event_data->event_param_data_4, handle_to_be_published);
420                                                 break;
421
422                                         case EMAIL_EVENT_DELETE_MAILBOX:
423                                                 err = event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data->multi_user_name, event_data->event_param_data_4, event_data->event_param_data_5, event_data->event_param_data_6,handle_to_be_published);
424                                                 break;
425
426                                         case EMAIL_EVENT_SAVE_MAIL:
427                                                 err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, handle_to_be_published);
428                                                 break;
429
430                                         case EMAIL_EVENT_MOVE_MAIL:
431                                                 event_handler_EMAIL_EVENT_MOVE_MAIL(event_data->multi_user_name, 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);
432                                                 break;
433
434                                         case EMAIL_EVENT_VALIDATE_ACCOUNT:
435                                                 event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err);
436                                                 break;
437
438                                         case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
439                                                         email_account_t *account = (email_account_t *)event_data->event_param_data_1;
440                                                         event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(event_data->multi_user_name, account, handle_to_be_published, &err);
441                                                 }
442                                                 break;
443
444                                         case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
445                                                         email_account_t *account = (email_account_t *)event_data->event_param_data_1;
446                                                         err = event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(event_data->multi_user_name, account, handle_to_be_published);
447                                                 }
448                                                 break;
449
450                                         case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
451                                                         email_account_t *account = (email_account_t *)event_data->event_param_data_1;
452                                                         event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data->multi_user_name, event_data->account_id, account, handle_to_be_published, &err);
453                                                 }
454                                                 break;
455
456                                         case EMAIL_EVENT_UPDATE_MAIL:
457                                                 event_handler_EMAIL_EVENT_UPDATE_MAIL(event_data->multi_user_name, (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);
458                                                 break;
459
460                                         case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
461                                                 event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, event_data->event_param_data_5, handle_to_be_published, &err);
462                                                 break;
463
464                                         case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
465                                                 err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4);
466                                                 break;
467
468 #ifdef __FEATURE_LOCAL_ACTIVITY__
469                                         case EMAIL_EVENT_LOCAL_ACTIVITY:
470                                                 event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err);
471                                                 break;
472 #endif /* __FEATURE_LOCAL_ACTIVITY__*/
473
474                                         case EMAIL_EVENT_SEARCH_ON_SERVER:
475                                                 err = event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, (email_search_filter_t *)event_data->event_param_data_1, event_data->event_param_data_5, handle_to_be_published);
476                                                 break;
477
478                                         case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
479                                                 err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_4, (char*)event_data->event_param_data_1, (char*)event_data->event_param_data_2, (char*)event_data->event_param_data_3, handle_to_be_published);
480                                                 break;
481
482                                         case EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT:
483                                                 if (!emcore_query_mail_size_limit(event_data->multi_user_name, event_data->account_id, handle_to_be_published, &err))
484                                                         EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
485                                                 break;
486
487                                         default:
488                                                 break;
489                                 }
490                         }
491
492                         if ((event_data->type == EMAIL_EVENT_SYNC_HEADER || event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX) && (err != EMAIL_ERROR_NONE)) {
493                                 EM_DEBUG_LOG("retry syncing");
494                                 /* for the retry syncing */
495                                 if (sync_failed_event_data) {
496                                         emcore_free_event(sync_failed_event_data);
497                                         EM_SAFE_FREE(sync_failed_event_data);
498                                         sync_failed_event_data = NULL;
499                                 }
500
501                                 sync_failed_event_data = em_malloc(sizeof(email_event_t));
502                                 if (sync_failed_event_data == NULL) {
503                                         EM_DEBUG_EXCEPTION("em_malloc failed");
504                                         err = EMAIL_ERROR_OUT_OF_MEMORY;
505                                 }
506
507                                 if (event_data->type == EMAIL_EVENT_SYNC_IMAP_MAILBOX && sync_failed_event_data) {
508                                         sync_failed_event_data->type               = EMAIL_EVENT_SYNC_IMAP_MAILBOX;
509                                         sync_failed_event_data->account_id         = event_data->account_id;
510                                         sync_failed_event_data->status             = EMAIL_EVENT_STATUS_WAIT;
511                                         sync_failed_event_data->event_param_data_3 = EM_SAFE_STRDUP(event_data->event_param_data_3);
512                                 }
513
514                                 if (event_data->type == EMAIL_EVENT_SYNC_HEADER && sync_failed_event_data) {
515                                         sync_failed_event_data->type               = EMAIL_EVENT_SYNC_HEADER;
516                                         sync_failed_event_data->account_id         = event_data->account_id;
517                                         sync_failed_event_data->status             = EMAIL_EVENT_STATUS_WAIT;
518                                         sync_failed_event_data->event_param_data_5 = event_data->event_param_data_5;
519                                         sync_failed_event_data->event_param_data_4 = event_data->event_param_data_4;
520                                 }
521                         }
522
523                         /* free internals in event */
524                         emcore_free_event(event_data); /*detected by valgrind*/
525
526                         if (account_tbl) {
527                                 emstorage_free_account(&account_tbl, 1, NULL);
528                                 account_tbl = NULL;
529                         }
530
531                         if (!emcore_notify_response_to_api(event_data->type, handle_to_be_published, err))
532                                 EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");
533
534                         /* free event itself */
535                         ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
536                         started_event = g_queue_pop_head(g_event_que);
537                         LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
538                         if (!started_event) {
539                                 EM_DEBUG_EXCEPTION("Failed to g_queue_pop_head");
540                         }
541                         else {
542                                 emcore_return_handle(started_event->handle);
543                                 EM_SAFE_FREE(started_event);
544                         }
545                         em_flush_memory();
546                 }
547         }
548
549         if (!emstorage_close(&err))
550                 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
551
552         emcore_close_recv_stream_list();
553         EM_DEBUG_FUNC_END();
554         return SUCCESS;
555 }
556
557 static int event_handler_EMAIL_EVENT_SYNC_HEADER (char *multi_user_name, int input_account_id, int input_mailbox_id, 
558                                                                        int handle_to_be_published, int *error)
559 {
560         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", 
561                                                    input_account_id, input_mailbox_id, handle_to_be_published, error);
562
563         int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
564         int mailbox_count = 0, account_count = 0;
565         int counter, account_index;
566         int unread = 0, total_unread = 0;
567         int vip_unread = 0, vip_total_unread = 0;
568         int vip_mail_count = 0, vip_total_mail = 0;
569         int mail_count = 0, total_mail = 0;
570         emcore_uid_list *uid_list = NULL;
571         emstorage_account_tbl_t *account_tbl_array = NULL;
572         emstorage_mailbox_tbl_t *mailbox_tbl_target = NULL, *mailbox_tbl_list = NULL;
573 #ifndef __FEATURE_KEEP_CONNECTION__
574         MAILSTREAM *stream = NULL;
575 #endif
576         char mailbox_id_param_string[10] = {0,};
577         char *input_mailbox_id_str = NULL;
578
579         if (input_mailbox_id == 0)
580                 sync_type = EMAIL_SYNC_ALL_MAILBOX;
581         else {
582                 email_account_t *ref_account = NULL;
583                 int sync_disabled = 0;
584
585                 ref_account = emcore_get_account_reference(multi_user_name, input_account_id, false);
586                 if (ref_account) {
587                         sync_disabled = ref_account->sync_disabled;
588                         emcore_free_account(ref_account);
589                         EM_SAFE_FREE(ref_account);
590                 }
591
592                 EM_DEBUG_LOG("sync_disabled[%d]", sync_disabled);
593
594                 if (sync_disabled) {
595                         err = EMAIL_ERROR_ACCOUNT_SYNC_IS_DISALBED;
596                         EM_DEBUG_LOG("Sync disabled for this account. Do not sync.");
597                         goto FINISH_OFF;
598                 }
599
600                 if (!emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
601                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
602                         goto FINISH_OFF;
603                 }
604         }
605         if(mailbox_tbl_target)
606                 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_target->mailbox_id);
607
608         input_mailbox_id_str = (input_mailbox_id == 0)? NULL: mailbox_id_param_string;
609
610 /*      if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, input_mailbox_id_str, handle_to_be_published, 0))
611                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
612 */
613         if (!emnetwork_check_network_status(&err)) {
614                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status error [%d]", err);
615                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
616                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
617                 goto FINISH_OFF;
618         }
619
620         if (sync_type != EMAIL_SYNC_ALL_MAILBOX) {      /* Sync only particular mailbox */
621                 EM_DEBUG_LOG_SEC ("sync start: account_id [%d] alias [%s]", input_account_id, mailbox_tbl_target->alias);
622                 if ((err = emcore_update_sync_status_of_account(multi_user_name, 
623                                                                                                                 input_account_id, 
624                                                                                                                 SET_TYPE_UNION, 
625                                                                                                                 SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
626                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
627
628                 if (!emcore_sync_header (multi_user_name, mailbox_tbl_target, (void**) &stream,
629                                                                    &uid_list, &mail_count, &unread, &vip_mail_count, 
630                                                                    &vip_unread, 1, handle_to_be_published, &err)) {
631                         EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
632                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
633                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
634                 }
635                 else {
636                         EM_DEBUG_LOG("emcore_sync_header succeeded [%d]", err);
637                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
638                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
639                 }
640
641                 stream = mail_close (stream);
642
643                 total_unread     += unread;
644                 vip_total_unread += vip_unread;
645                 total_mail       += mail_count;
646                 vip_total_mail   += vip_mail_count;
647
648                 if (total_unread > 0 && (emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE) {
649                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed");
650                 } else {
651                         if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
652                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
653                 }
654
655                 if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
656                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
657                 }
658
659                 if (account_tbl_array && account_tbl_array->auto_download_size == 0) {
660                         if (!emdaemon_finalize_sync(multi_user_name, input_account_id, 0, 0, 0, 0, true, NULL))
661                                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
662                 }
663
664                 if (account_tbl_array)
665                         emstorage_free_account(&account_tbl_array, 1, NULL);
666
667                 if (mailbox_tbl_target->mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
668                         emcore_display_unread_in_badge(multi_user_name);
669         }
670         else /*  All Folder */ {
671                 EM_DEBUG_LOG ("sync start for all mailbox: account_id [%d]", input_account_id);
672                 /*  Sync of all mailbox */
673
674                 if (input_account_id == ALL_ACCOUNT) {
675                         if ((err = emcore_update_sync_status_of_account(multi_user_name, ALL_ACCOUNT, SET_TYPE_UNION, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
676                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
677                         if (!emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_array , true, false, &err)) {
678                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
679                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL,  handle_to_be_published, err))
680                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
681                                 goto FINISH_OFF;
682                         }
683                 }
684                 else {
685                         if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_UNION, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
686                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
687
688                         if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
689                                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
690                                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
691                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
692                                 goto FINISH_OFF;
693                         }
694                         account_count = 1;
695                 }
696
697                 for (account_index = 0 ; account_index < account_count; account_index++) {
698                         if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
699                                 EM_DEBUG_LOG ("account[%d] is for ActiveSync. Skip  ", account_index);
700                                 continue;
701                         }
702
703                         if (account_tbl_array[account_index].sync_disabled == 1) {
704                                 EM_DEBUG_LOG ("account[%d] is sync-disabled. Skip  ", account_index);
705                                 continue;
706                         }
707 /* folder sync is also necessary */
708                         if (!emstorage_get_mailbox_list (multi_user_name, account_tbl_array[account_index].account_id, 0, 
709                                   EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || 
710                                                                                                   mailbox_count <= 0) {
711                                 EM_DEBUG_EXCEPTION ("emstorage_get_mailbox error [%d]", err);
712                                 if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, 
713                                                                      input_mailbox_id_str, handle_to_be_published, err))
714                                         EM_DEBUG_EXCEPTION ("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] error >>>> ");
715                                 continue;
716                         }
717
718                         EM_DEBUG_LOG("emcore_get_mailbox_list_to_be_sync returns [%d] mailboxes", mailbox_count);
719
720
721 #ifndef __FEATURE_KEEP_CONNECTION__
722                         if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
723                                 memset(mailbox_id_param_string, 0, 10);
724                                 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
725                                 if (!emcore_connect_to_remote_mailbox (multi_user_name, account_tbl_array[account_index].account_id, 
726                                                       mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err))  {
727                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox error [%d]", err);
728                                         if (err == EMAIL_ERROR_LOGIN_FAILURE)
729                                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
730                                         /* continue; */
731                                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string,  handle_to_be_published, err))
732                                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
733                                         continue;
734                                 }
735                                 EM_DEBUG_LOG ("emcore_connect_to_remote_mailbox returns [%d]", err);
736                         }
737                         else
738                                 stream = mail_close (stream);
739 #endif
740
741                         for (counter = 0; counter < mailbox_count; counter++) {
742
743                                 EM_DEBUG_LOG_SEC("mailbox_name [%s], mailbox_id [%d], mailbox_type [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, mailbox_tbl_list[counter].mailbox_type);
744                                 vip_unread = unread = mail_count = 0;
745                                 if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
746                                         || mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
747                                         /*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */) {
748                                         EM_DEBUG_LOG("Skipped for all emails or trash");
749                                         continue;
750                                 }
751                                 else if (!mailbox_tbl_list[counter].local_yn) {
752                                         EM_DEBUG_LOG_SEC("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
753 #ifdef __FEATURE_KEEP_CONNECTION__
754                                         if (!emcore_sync_header (multi_user_name,
755                                       (mailbox_tbl_list + counter), 
756                                       (void **)&stream, 
757                                       &uid_list, 
758                                       &mail_count, 
759                                       &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
760 #else /*  __FEATURE_KEEP_CONNECTION__ */
761                                         if (!emcore_sync_header (multi_user_name, 
762                                       (mailbox_tbl_list + counter), 
763                                       (void **)&stream, 
764                                       &uid_list, 
765                                       &mail_count, 
766                                       &unread, &vip_mail_count, &vip_unread, 1, handle_to_be_published, &err))
767 #endif /*  __FEATURE_KEEP_CONNECTION__ */
768                                         {       
769                                                 EM_DEBUG_EXCEPTION_SEC ("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", 
770                                                         mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, err);
771
772 #ifndef __FEATURE_KEEP_CONNECTION__
773                                                 if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || 
774                                                                                  err == EMAIL_ERROR_SOCKET_FAILURE)
775                                                         stream = mail_close (stream);    
776 #endif /*  __FEATURE_KEEP_CONNECTION__ */
777                                                 memset(mailbox_id_param_string, 0, 10);
778                                                 SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
779                                                 if (!emcore_notify_network_event (NOTI_DOWNLOAD_FAIL, 
780                                                                            account_tbl_array[account_index].account_id, 
781                                                                            mailbox_id_param_string,
782                                                                            handle_to_be_published, err))
783                                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
784
785                                                 break;
786                                         }
787                                 }
788                                 EM_DEBUG_LOG_SEC("---mailbox %s has unread %d / %d", mailbox_tbl_list[counter].mailbox_name, 
789                                                                                                 unread, mail_count);
790                                 total_unread     += unread;
791                                 vip_total_unread += vip_unread;
792                                 total_mail       += mail_count;
793                                 vip_total_mail   += vip_mail_count;
794
795                                 if (mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
796                                         emcore_display_unread_in_badge(multi_user_name);
797
798                         }
799
800                         EM_DEBUG_LOG_SEC("Sync for account_id(%d) is completed....! (unread %d/%d)", account_tbl_array[account_index].account_id, total_unread, total_mail);
801                         EM_DEBUG_LOG_SEC("Sync for account_id(%d) is completed....! (vip_unread %d/%d)", account_tbl_array[account_index].account_id, vip_total_unread, vip_total_mail);
802
803                         if ((err == EMAIL_ERROR_NONE) && !emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
804                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");
805
806                         if ((total_unread > 0) && (emcore_update_sync_status_of_account(multi_user_name, account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE) {
807                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed");
808                         } else {
809                                 if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
810                                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
811                         }
812
813                         if (account_tbl_array[account_index].auto_download_size == 0) {
814                                 if (!emdaemon_finalize_sync(multi_user_name, account_tbl_array[account_index].account_id, 0, 0, 0, 0, true, NULL))
815                                         EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
816                         }
817
818 #ifndef __FEATURE_KEEP_CONNECTION__
819                         if (stream)  
820                                 stream = mail_close (stream);
821 #endif
822                         if (mailbox_tbl_list) {
823                                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
824                                 mailbox_tbl_list = NULL;
825                                 mailbox_count = 0;
826                         }
827                 }
828         }
829
830         ret = true;
831
832 FINISH_OFF:
833
834 #ifndef __FEATURE_KEEP_CONNECTION__
835         if (stream)
836                 stream = mail_close (stream);
837 #endif
838         if (error)
839                 *error = err;
840
841         if(mailbox_tbl_target)
842                 emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);
843
844         if (mailbox_tbl_list)
845                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
846
847         if (account_tbl_array)
848                 emstorage_free_account(&account_tbl_array, account_count, NULL);
849
850         EM_DEBUG_FUNC_END();
851         return ret;
852 }
853
854 static int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(char *multi_user_name, email_account_t *account, int handle_to_be_published, int *error)
855 {
856         EM_DEBUG_FUNC_BEGIN("account [%p]", account);
857         int err, ret = false;
858         char *imap_cap_string = NULL;
859
860         if(!account) {
861                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
862                 err = EMAIL_ERROR_INVALID_PARAM;
863                 goto FINISH_OFF;
864         }
865
866         EM_DEBUG_LOG_SEC("incoming_server_address  :  %s", account->incoming_server_address);
867
868         if (!emnetwork_check_network_status(&err)) {
869                 emcore_delete_account_from_unvalidated_account_list(account->account_id);
870                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
871                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
872                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
873                 goto FINISH_OFF;
874         }
875         else {
876                 EM_DEBUG_LOG_SEC("incoming_server_address : %s", account->incoming_server_address);
877
878                 if (!emcore_validate_account_with_account_info(multi_user_name, account, EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT, &imap_cap_string, handle_to_be_published, &err)) {
879                         emcore_delete_account_from_unvalidated_account_list(account->account_id);
880                         EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
881                         if (err == EMAIL_ERROR_CANCELLED) {
882                                 EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
883                                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL, account->account_id, NULL,  handle_to_be_published, err))
884                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL] Failed");
885                                 goto FINISH_OFF;
886                         }
887                         else
888                                 goto FINISH_OFF;
889                 }
890                 else {
891                         emcore_delete_account_from_unvalidated_account_list(account->account_id);
892
893                         if (emcore_create_account(multi_user_name, account, false, &err) == false) {
894                                 EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
895                                 goto FINISH_OFF;
896                         }
897
898                         EM_DEBUG_LOG("incoming_server_type [%d]", account->incoming_server_type);
899
900                         if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
901                                 if (!emcore_sync_mailbox_list(multi_user_name, account->account_id, "", handle_to_be_published, &err))  {
902                                         EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync failed [%d]", err);
903                                         /*  delete account whose mailbox couldn't be obtained from server */
904                                         emcore_delete_account(multi_user_name, account->account_id, false, NULL);
905                                         goto FINISH_OFF;
906                                 }
907                         }
908
909                         EM_DEBUG_LOG("validating and creating an account are succeeded for account id  [%d]  err [%d]", account->account_id, err);
910                         if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, imap_cap_string,  handle_to_be_published, err))
911                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
912                 }
913         }
914
915         ret = true;
916
917 FINISH_OFF:
918         if (ret == false && err != EMAIL_ERROR_CANCELLED && account) {
919                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
920                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
921         }
922
923         EM_SAFE_FREE(imap_cap_string);
924
925         if (error)
926                 *error = err;
927
928         EM_DEBUG_FUNC_END();
929         return ret;
930 }
931
932 static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(char *multi_user_name, email_account_t *input_account, int input_handle_to_be_published)
933 {
934         EM_DEBUG_FUNC_BEGIN("input_account [%p]", input_account);
935         int err = EMAIL_ERROR_NONE;
936         char *server_capability_string = NULL;
937
938         if(!input_account) {
939                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
940                 err = EMAIL_ERROR_INVALID_PARAM;
941                 goto FINISH_OFF;
942         }
943
944         EM_DEBUG_LOG_SEC("incoming_server_address [%s]", input_account->incoming_server_address);
945
946         if (!emnetwork_check_network_status(&err)) {
947                 emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
948                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
949                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, input_account->account_id, NULL, input_handle_to_be_published, err))
950                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
951                 goto FINISH_OFF;
952         }
953         else {
954                 EM_DEBUG_LOG_SEC("incoming_server_address : %s", input_account->incoming_server_address);
955
956                 if (!emcore_validate_account_with_account_info(multi_user_name, input_account, EMAIL_EVENT_VALIDATE_ACCOUNT_EX, &server_capability_string, input_handle_to_be_published, &err)) {
957                         emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
958
959                         EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
960
961                         if (err == EMAIL_ERROR_CANCELLED) {
962                                 EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_ACCOUNT_CANCEL ");
963                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, input_account->account_id, NULL, input_handle_to_be_published, err))
964                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed");
965                                 goto FINISH_OFF;
966                         }
967                         else
968                                 goto FINISH_OFF;
969                 }
970                 else {
971                         emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
972
973                         EM_DEBUG_LOG("validating an account are succeeded for account id  [%d]  err [%d]", input_account->account_id, err);
974                         if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, input_account->account_id, server_capability_string, input_handle_to_be_published, err))
975                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
976                 }
977         }
978
979 FINISH_OFF:
980         if (err != EMAIL_ERROR_NONE && err != EMAIL_ERROR_CANCELLED && input_account) {
981                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, input_account->account_id, NULL, input_handle_to_be_published, err))
982                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed");
983         }
984
985         EM_SAFE_FREE(server_capability_string);
986
987         EM_DEBUG_FUNC_END("err[%d]", err);
988         return err;
989 }
990
991 static int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(char *multi_user_name, int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
992 {
993         EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
994         int err, ret = false;
995         char *imap_cap_string = NULL;
996         emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;
997         email_account_t *duplicated_account_info = NULL, *old_account_info = NULL;
998
999         if (!new_account_info) {
1000                 EM_DEBUG_EXCEPTION("Invalid Parameter");
1001                 err = EMAIL_ERROR_INVALID_PARAM;
1002                 goto FINISH_OFF;
1003         }
1004
1005         if (!emnetwork_check_network_status(&err))  {
1006                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1007
1008                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1009                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
1010                 goto FINISH_OFF;
1011         }
1012         else  {
1013                 EM_DEBUG_LOG_SEC("incoming_server_address: (%s)", new_account_info->incoming_server_address);
1014
1015                 /* If the password fields are empty, fill the fields with password of old information*/
1016                 if((old_account_info = emcore_get_account_reference(multi_user_name, account_id, true)) == NULL) {
1017                         EM_DEBUG_EXCEPTION("emcore_get_account_reference failed ");
1018                         goto FINISH_OFF;
1019                 }
1020
1021                 EM_DEBUG_LOG_SEC("old_account_info->incoming_server_password [%s]", old_account_info->incoming_server_password);
1022
1023                 if( EM_SAFE_STRLEN (new_account_info->incoming_server_password) == 0 ) {
1024                         EM_SAFE_FREE(new_account_info->incoming_server_password); /* be allocated but has zero length */
1025                         EM_DEBUG_LOG_SEC("old_account_info->incoming_server_password [%s]", old_account_info->incoming_server_password);
1026                         new_account_info->incoming_server_password = EM_SAFE_STRDUP(old_account_info->incoming_server_password);
1027                         if(new_account_info->incoming_server_password == NULL) {
1028                                 EM_DEBUG_EXCEPTION("allocation for new_account_info->password failed");
1029                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1030                                 goto FINISH_OFF;
1031                         }
1032                 }
1033
1034                 EM_DEBUG_LOG_SEC("old_account_info->outgoing_server_password [%s]", old_account_info->outgoing_server_password);
1035
1036                 if( EM_SAFE_STRLEN (new_account_info->outgoing_server_password) == 0 ) {
1037                         EM_SAFE_FREE(new_account_info->outgoing_server_password);
1038                         if(old_account_info->outgoing_server_password) {
1039                                 new_account_info->outgoing_server_password = EM_SAFE_STRDUP(old_account_info->outgoing_server_password);
1040                                 if(new_account_info->outgoing_server_password == NULL) {
1041                                         EM_DEBUG_EXCEPTION("allocation for new_account_info->outgoing_server_password failed");
1042                                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1043                                         goto FINISH_OFF;
1044                                 }
1045                         }
1046                 }
1047
1048                 emcore_duplicate_account(new_account_info, &duplicated_account_info, &err);
1049                 if (err != EMAIL_ERROR_NONE ) {
1050                         EM_DEBUG_EXCEPTION("emcore_duplicate_account failed [%d]", err);
1051                         goto FINISH_OFF;
1052                 }
1053
1054                 if((err = emcore_add_account_to_unvalidated_account_list(duplicated_account_info)) != EMAIL_ERROR_NONE) {
1055                         EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
1056                         goto FINISH_OFF;
1057                 }
1058
1059                 if (!emcore_validate_account_with_account_info(multi_user_name, duplicated_account_info, EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT, &imap_cap_string, handle_to_be_published, &err)) {
1060                         EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err :  %d", err);
1061                         if (err == EMAIL_ERROR_CANCELLED) {
1062                                 EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
1063                                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, account_id, NULL,  handle_to_be_published, err))
1064                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
1065                                 goto FINISH_OFF;
1066                         }
1067                         else {
1068                                 goto FINISH_OFF;
1069                         }
1070                 }
1071                 else {
1072                         if (!emstorage_get_account_by_id(multi_user_name, account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
1073                                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1074                                 /* goto FINISH_OFF; */
1075                         }
1076                         duplicated_account_info->account_id = account_id;
1077
1078                         new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
1079                         if (!new_account_tbl) {
1080                                 EM_DEBUG_EXCEPTION("allocation failed");
1081                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1082                                 goto FINISH_OFF;
1083                         }
1084
1085                         em_convert_account_to_account_tbl(duplicated_account_info, new_account_tbl);
1086
1087                         if (!emstorage_update_account(multi_user_name, account_id, new_account_tbl, true, &err)) {
1088                                 EM_DEBUG_EXCEPTION("emstorage_update_account failed : [%d]", err);
1089                         }
1090
1091                         EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", duplicated_account_info->account_id, err);
1092                         if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, imap_cap_string, handle_to_be_published, err))
1093                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
1094                 }
1095         }
1096
1097         ret = true;
1098
1099 FINISH_OFF:
1100
1101         if (duplicated_account_info) {
1102                 if (duplicated_account_info->account_id < 0)
1103                         emcore_delete_account_from_unvalidated_account_list(duplicated_account_info->account_id);
1104                 else {
1105                         emcore_free_account(duplicated_account_info);
1106                         EM_SAFE_FREE(duplicated_account_info);
1107                 }
1108         }
1109
1110     if (old_account_info) {
1111         emcore_free_account(old_account_info);
1112         EM_SAFE_FREE(old_account_info);
1113     }
1114
1115         if (old_account_tbl)
1116                 emstorage_free_account(&old_account_tbl, 1, NULL);
1117         if (new_account_tbl)
1118                 emstorage_free_account(&new_account_tbl, 1, NULL);
1119
1120         if (ret == false && err != EMAIL_ERROR_CANCELLED) {
1121                 if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1122                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
1123         }
1124
1125         EM_SAFE_FREE(imap_cap_string);
1126
1127         if (error)
1128                 *error = err;
1129
1130         EM_DEBUG_FUNC_END();
1131         return ret;
1132 }
1133
1134 static int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
1135 {
1136         EM_DEBUG_FUNC_BEGIN();
1137
1138         emcore_set_mail_slot_size(multi_user_name, account_id, mailbox_id, new_slot_size, error);
1139
1140         EM_DEBUG_FUNC_END();
1141         return true;
1142 }
1143
1144 static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(char *multi_user_name, int input_account_id, int input_mailbox_id)
1145 {
1146         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
1147         int err = EMAIL_ERROR_NONE;
1148
1149         if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(multi_user_name, input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1150                 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
1151                 goto FINISH_OFF;
1152         }
1153
1154         if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1155                 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1156                 goto FINISH_OFF;
1157         }
1158
1159 FINISH_OFF:
1160
1161         EM_DEBUG_FUNC_END("err [%d]", err);
1162         return err;
1163 }
1164
1165 #ifdef __FEATURE_LOCAL_ACTIVITY__
1166 static int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(char *multi_user_name, int account_id, int event,handle, int *error)
1167 {
1168         EM_DEBUG_FUNC_BEGIN();
1169
1170         int err = EMAIL_ERROR_NONE;
1171         email_mailbox_t mailbox;
1172         emstorage_activity_tbl_t *local_activity = NULL;
1173         int activity_id_count = 0;
1174         int activity_chunk_count = 0;
1175         int *activity_id_list = NULL;
1176         int i = 0;
1177
1178         if (!emnetwork_check_network_status(&err))
1179                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1180         else {
1181                 if (false == emstorage_get_activity_id_list(multi_user_name, account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1182                         EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1183                 }
1184                 else {
1185                         for (i = 0; i < activity_id_count; ++i) {
1186                                 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))
1187                                         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);
1188                                 else {
1189                                         EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1190                                         switch (local_activity[0].activity_type) {
1191                                                 case ACTIVITY_MODIFYFLAG:  {
1192                                                         if (emcore_sync_flag_with_server(multi_user_name, local_activity[0].mail_id , &err))  {
1193                                                                 if (!emcore_delete_activity(&local_activity[0], &err))
1194                                                                         EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1195                                                         }
1196                                                 }
1197                                                 break;
1198
1199                                                 case ACTIVITY_DELETEMAIL:
1200                                                 case ACTIVITY_MOVEMAIL:
1201                                                 case ACTIVITY_MODIFYSEENFLAG:
1202                                                 case ACTIVITY_COPYMAIL:  {
1203
1204                                                         int j = 0, k = 0;
1205                                                         int total_mail_ids = activity_chunk_count;
1206
1207                                                         int *mail_id_list = NULL;
1208
1209                                                         mail_id_list = (int *)em_malloc(sizeof(int) * total_mail_ids);
1210                                                         if (mail_id_list == NULL) {
1211                                                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1212                                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1213                                                                 break;
1214                                                         }
1215
1216                                                         if (NULL == mail_id_list) {
1217                                                                 EM_DEBUG_EXCEPTION("malloc failed... ");
1218                                                                 break;
1219                                                         }
1220
1221                                                         do {
1222
1223                                                                 for (j = 0; j < BULK_OPERATION_COUNT && (k < total_mail_ids); ++j, ++k)
1224                                                                         mail_id_list[j] = local_activity[k].mail_id;
1225
1226                                                                 switch (local_activity[k-1].activity_type) {
1227                                                                         case ACTIVITY_DELETEMAIL:  {
1228                                                                                 if (!emcore_delete_mail(multi_user_name,
1229                                                                                                                                                 local_activity[k-1].account_id,
1230                                                                                                                                                 mail_id_list,
1231                                                                                                                                                 j,
1232                                                                                                                                                 EMAIL_DELETE_LOCAL_AND_SERVER,
1233                                                                                                                                                 EMAIL_DELETED_BY_COMMAND,
1234                                                                                                                                                 false,
1235                                                                                                                                                 &err))
1236                                                                                         EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1237                                                                         }
1238                                                                         break;
1239
1240                                                                         case ACTIVITY_MOVEMAIL:  {
1241                                                                                 if (!emcore_move_mail_on_server_ex(multi_user_name, local_activity[k-1].account_id ,
1242                                                                                                                                                    local_activity[k-1].src_mbox,
1243                                                                                                                                                    mail_id_list,
1244                                                                                                                                                    j,
1245                                                                                                                                                    local_activity[k-1].dest_mbox,
1246                                                                                                                                                    &err))
1247                                                                                         EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
1248                                                                         }
1249                                                                         break;
1250                                                                         case ACTIVITY_MODIFYSEENFLAG:  {
1251                                                                                 int seen_flag = atoi(local_activity[0].src_mbox);
1252                                                                                 if (!emcore_sync_seen_flag_with_server_ex(multi_user_name, mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
1253                                                                                         EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
1254                                                                         }
1255                                                                         break;
1256                                                                 }
1257
1258                                                         } while (k < total_mail_ids);
1259
1260                                                         EM_SAFE_FREE(mail_id_list);
1261                                                 }
1262
1263                                                 break;
1264
1265                                                 default:
1266                                                         EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1267                                                 break;
1268                                         }
1269
1270                                         emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1271
1272                                         if (g_local_activity_run == 1) {
1273                                                 EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1274                                                 break;
1275                                         }
1276                                 }
1277                         }
1278                 }
1279         }
1280         if (activity_id_list) {
1281                 if (false == emstorage_free_activity_id_list(activity_id_list, &err))
1282                         EM_DEBUG_EXCEPTION("emstorage_free_activity_id_list failed");
1283         }
1284
1285         if (error)
1286                 *error = err;
1287
1288         EM_DEBUG_FUNC_END();
1289
1290         return true;
1291 }
1292 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1293
1294 static int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(char *multi_user_name, int account_id, int mail_id, int option, int handle_to_be_published, int *error)
1295 {
1296         EM_DEBUG_FUNC_BEGIN();
1297
1298         int err = EMAIL_ERROR_NONE;
1299         email_mailbox_t mailbox;
1300
1301         memset(&mailbox, 0x00, sizeof(mailbox));
1302         mailbox.account_id = account_id;
1303
1304         if (!emnetwork_check_network_status(&err))  {
1305                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1306
1307                 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1308         } else {
1309                 emstorage_mail_tbl_t *mail = NULL;
1310                 MAILSTREAM *tmp_stream = NULL;
1311
1312                 if (!emstorage_get_mail_by_id (multi_user_name, mail_id, &mail, true, &err) || !mail) {
1313                         EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1314                         emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1315                         goto FINISH_OFF;
1316                 }
1317
1318
1319                 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
1320                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1321                         if(err == EMAIL_ERROR_NO_SUCH_HOST)
1322                                 err = EMAIL_ERROR_CONNECTION_FAILURE;
1323
1324                         if (mail)
1325                                 emstorage_free_mail(&mail, 1, NULL);
1326
1327                         emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
1328
1329                         goto FINISH_OFF;
1330                 }
1331
1332                 if (!emcore_gmime_download_body_sections(multi_user_name, tmp_stream, mailbox.account_id, mail_id,
1333                                 option & 0x01, NO_LIMITATION, handle_to_be_published, 1, 0, &err))
1334                         EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
1335
1336                 if (tmp_stream) {
1337                         tmp_stream = mail_close (tmp_stream);
1338                 }
1339
1340                 if (mail)
1341                         emstorage_free_mail(&mail, 1, NULL);
1342         }
1343
1344 FINISH_OFF:
1345
1346         if (error)
1347                 *error = err;
1348
1349         EM_DEBUG_FUNC_END();
1350         return true;
1351 }
1352
1353 static int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(char *multi_user_name, int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
1354 {
1355         EM_DEBUG_FUNC_BEGIN();
1356
1357         int err = EMAIL_ERROR_NONE;
1358
1359         EM_DEBUG_LOG("attachment_no is %d", attachment_no);
1360
1361         if (!emnetwork_check_network_status(&err))  {
1362                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1363                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
1364         }
1365         else  {
1366
1367 #ifdef __ATTACHMENT_OPTI__
1368                 if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, handle_to_be_published, &err))
1369                         EM_DEBUG_EXCEPTION("\t emcore_download_attachment_bulk failed [%d]", err);
1370 #else
1371
1372                 if (!emcore_gmime_download_attachment(multi_user_name, mail_id, attachment_no, 1, handle_to_be_published, 0, &err))
1373                         EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
1374 #endif
1375         }
1376
1377         if (error)
1378                 *error = err;
1379
1380         EM_DEBUG_FUNC_END();
1381         return true;
1382 }
1383
1384 static int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(char *multi_user_name, int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
1385 {
1386         EM_DEBUG_FUNC_BEGIN();
1387
1388         int err = EMAIL_ERROR_NONE;
1389
1390         if (!emnetwork_check_network_status(&err))
1391                 EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
1392         else if (!emcore_sync_flags_field_with_server(multi_user_name, mail_ids, num, field_type, value, &err))
1393                 EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);
1394
1395         if (error)
1396                 *error = err;
1397
1398         EM_DEBUG_FUNC_END();
1399         return true;
1400 }
1401
1402 static int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(char *multi_user_name, int account_id, int handle_to_be_published, int *error)
1403 {
1404         EM_DEBUG_FUNC_BEGIN();
1405
1406         int err = EMAIL_ERROR_NONE;
1407
1408         if (!emnetwork_check_network_status(&err))  {
1409                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1410
1411                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1412                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>>");
1413         }
1414         else  {
1415
1416                 if (!emcore_validate_account(multi_user_name, account_id, handle_to_be_published, &err)) {
1417                         EM_DEBUG_EXCEPTION("emcore_validate_account failed account id  :  %d  err :  %d", account_id, err);
1418
1419                         if (err == EMAIL_ERROR_CANCELLED) {
1420                                 EM_DEBUG_EXCEPTION("notify  :  NOTI_VALIDATE_ACCOUNT_CANCEL ");
1421                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, account_id, NULL,  handle_to_be_published, err))
1422                                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
1423                         }
1424                         else {
1425                                 if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
1426                                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
1427                         }
1428                 }
1429                 else {
1430                         email_account_t *account_ref = NULL;
1431                         account_ref = emcore_get_account_reference(multi_user_name, account_id, false);
1432
1433                         if (account_ref) {
1434                                 EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
1435                                 if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
1436                                         int dummy=0;
1437                                         if (!emcore_check_event_thread_status(&dummy, handle_to_be_published)) {
1438                                                 EM_DEBUG_LOG("canceled type [%d]", dummy);
1439                                                 err = EMAIL_ERROR_CANCELLED;
1440                                         }
1441                                         else if (!emcore_sync_mailbox_list(multi_user_name, account_id, "", handle_to_be_published, &err))
1442                                                 EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
1443                                 }
1444
1445                                 if (err > 0) {
1446                                         EM_DEBUG_EXCEPTION("emcore_validate_account succeeded account id  :  %d  err :  %d", account_id, err);
1447                                         if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, account_id, NULL,  handle_to_be_published, err))
1448                                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success >>>>");
1449                                 }
1450
1451                                 emcore_free_account(account_ref);
1452                                 EM_SAFE_FREE(account_ref);
1453                         }
1454                 }
1455         }
1456
1457         if (error)
1458                 *error = err;
1459
1460         EM_DEBUG_FUNC_END();
1461         return true;
1462 }
1463
1464 static int event_handler_EMAIL_EVENT_UPDATE_MAIL(char *multi_user_name, 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)
1465 {
1466         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);
1467         int err = EMAIL_ERROR_NONE;
1468 /*
1469         if ( (err = emcore_update_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
1470                 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1471 */
1472         EM_DEBUG_FUNC_END("err [%d", err);
1473         return err;
1474 }
1475
1476 static int event_handler_EMAIL_EVENT_SAVE_MAIL(char *multi_user_name, int input_account_id, int input_mail_id, int input_handle_to_be_published)
1477 {
1478         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mail_id [%d] input_handle_to_be_published [%d]", input_account_id, input_mail_id, input_handle_to_be_published);
1479         int err = EMAIL_ERROR_NONE;
1480
1481         err = emcore_sync_mail_from_client_to_server(multi_user_name, input_mail_id);
1482
1483         EM_DEBUG_FUNC_END("err [%d]", err);
1484         return err;
1485 }
1486
1487 static int event_handler_EMAIL_EVENT_MOVE_MAIL(char *multi_user_name, 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)
1488 {
1489         EM_DEBUG_FUNC_BEGIN();
1490         int err = EMAIL_ERROR_NONE, ret = false;
1491         email_account_t *account_ref = NULL;
1492
1493         if (!(account_ref = emcore_get_account_reference(multi_user_name, account_id, false))) {
1494                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1495                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1496                 goto FINISH_OFF;
1497         }
1498
1499         /* Move mail local */
1500         /*
1501         if (!emcore_mail_move(mail_ids, mail_id_count, dest_mailbox.mailbox_name, EMAIL_MOVED_BY_COMMAND, 0, &err)) {
1502                 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
1503                 goto FINISH_OFF;
1504         }
1505         */
1506
1507         if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1508                 /* Move mail on server */
1509                 if (!emnetwork_check_network_status(&err))
1510                         EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1511                 else {
1512 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1513                         if (!emcore_move_mail_on_server_ex(multi_user_name, account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1514                                 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
1515 #else
1516                         if (!emcore_move_mail_on_server(multi_user_name, account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
1517                                 EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
1518 #endif
1519                 }
1520         }
1521
1522         ret = true;
1523 FINISH_OFF:
1524
1525         if (account_ref) {
1526                 emcore_free_account(account_ref);
1527                 EM_SAFE_FREE(account_ref);
1528         }
1529
1530         if (error)
1531                 *error = err;
1532
1533         EM_DEBUG_FUNC_END();
1534         return ret;
1535 }
1536
1537 static int event_handler_EMAIL_EVENT_DELETE_MAILBOX(char *multi_user_name, int mailbox_id, int on_server, int recursive, int handle_to_be_published)
1538 {
1539         EM_DEBUG_FUNC_BEGIN("mailbox_id[%d] on_server[%d] recursive[%d] handle_to_be_published[%d]",  mailbox_id, on_server, recursive, handle_to_be_published);
1540         int err = EMAIL_ERROR_NONE;
1541
1542         if ((err = emcore_delete_mailbox(multi_user_name, mailbox_id, on_server, recursive)) != EMAIL_ERROR_NONE)
1543                 EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
1544
1545         EM_DEBUG_FUNC_END("err [%d]", err);
1546         return err;
1547 }
1548
1549 static int event_handler_EMAIL_EVENT_CREATE_MAILBOX(char *multi_user_name, email_mailbox_t *input_new_mailbox, int on_server, int handle_to_be_published)
1550 {
1551         EM_DEBUG_FUNC_BEGIN();
1552         int err = EMAIL_ERROR_NONE;
1553
1554         if (!emcore_create_mailbox(multi_user_name, input_new_mailbox, on_server, -1, -1, &err))
1555                 EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
1556
1557         EM_DEBUG_FUNC_END("err [%d]", err);
1558         return err;
1559 }
1560
1561 static int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(char *multi_user_name, int mail_id, int event_handle, int *error)
1562 {
1563         EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);
1564
1565         int err = EMAIL_ERROR_NONE;
1566
1567         if (!emnetwork_check_network_status(&err))
1568                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1569         else {
1570                 if (!emcore_sync_flag_with_server(multi_user_name, mail_id, event_handle, &err))
1571                         EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
1572 #ifdef __FEATURE_LOCAL_ACTIVITY__
1573                 else {
1574                         emstorage_activity_tbl_t new_activity;
1575                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
1576                         new_activity.activity_type = ACTIVITY_MODIFYFLAG;
1577                         new_activity.account_id    = event_data.account_id;
1578                         new_activity.mail_id       = event_data.event_param_data_4;
1579                         new_activity.dest_mbox     = NULL;
1580                         new_activity.server_mailid = NULL;
1581                         new_activity.src_mbox      = NULL;
1582
1583                         if (!emcore_delete_activity(&new_activity, &err))
1584                                 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
1585                 }
1586 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
1587         }
1588
1589         if (error)
1590                 *error = err;
1591
1592         EM_DEBUG_FUNC_END();
1593         return true;
1594 }
1595
1596 static int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(char *multi_user_name, int input_account_id, int input_mailbox_id, int input_from_server, int *error)
1597 {
1598         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id [%d], input_from_server [%d], error [%p]", input_account_id, input_mailbox_id, input_from_server, error);
1599         int err = EMAIL_ERROR_NONE;
1600
1601         if (!emcore_delete_all_mails_of_mailbox(multi_user_name, input_account_id, input_mailbox_id, input_from_server, &err))
1602                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
1603
1604         if (error)
1605                 *error = err;
1606
1607         EM_DEBUG_FUNC_END("err [%d]", err);
1608         return true;
1609 }
1610
1611 static int event_handler_EMAIL_EVENT_DELETE_MAIL(char *multi_user_name, int account_id, int *mail_id_list, int mail_id_count, int from_server, int *error)
1612 {
1613         EM_DEBUG_FUNC_BEGIN();
1614         int err = EMAIL_ERROR_NONE;
1615         int ret = false;
1616         email_account_t *account_ref = NULL;
1617
1618         if (!(account_ref = emcore_get_account_reference(multi_user_name, account_id, false))) {
1619                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
1620                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1621                 goto FINISH_OFF;
1622         }
1623
1624         if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, mail_id_count, from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
1625                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
1626                 goto FINISH_OFF;
1627         }
1628
1629         ret = true;
1630 FINISH_OFF:
1631
1632         if (account_ref) {
1633                 emcore_free_account(account_ref);
1634                 EM_SAFE_FREE(account_ref);
1635         }
1636
1637         if (error)
1638                 *error = err;
1639
1640         EM_DEBUG_FUNC_END();
1641         return ret;
1642 }
1643
1644 static int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(char *multi_user_name, int account_id, char *maibox_name, int handle_to_be_published, int *error)
1645 {
1646         EM_DEBUG_FUNC_BEGIN();
1647         int err = EMAIL_ERROR_NONE;
1648
1649         if (!emnetwork_check_network_status(&err))  {
1650                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
1651                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_id, maibox_name,  0, err))
1652                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
1653         }
1654         else  {
1655                 EM_DEBUG_LOG("Sync of all mailbox");
1656                 if (!emcore_sync_mailbox_list(multi_user_name, account_id, "", handle_to_be_published, &err))
1657                         EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
1658         }
1659
1660         if (error)
1661                 *error = err;
1662
1663         EM_DEBUG_FUNC_END();
1664         return true;
1665 }
1666
1667 static int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(char *multi_user_name, int account_id, int mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, int handle_to_be_published)
1668 {
1669         EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], input_search_filter : [%p], input_search_filter_count : [%d], handle_to_be_published [%d]", account_id, mailbox_id, input_search_filter, input_search_filter_count, handle_to_be_published);
1670         int err = EMAIL_ERROR_NONE;
1671         
1672         if (err == EMAIL_ERROR_NONE) {
1673                 if ((err = emcore_search_on_server(multi_user_name, account_id, mailbox_id, input_search_filter, input_search_filter_count, handle_to_be_published)) != EMAIL_ERROR_NONE) {
1674                         EM_DEBUG_EXCEPTION("emcore_search_on_server failed [%d]", err);
1675                 }
1676         }
1677
1678         EM_DEBUG_FUNC_END();
1679         return err;
1680 }
1681
1682 static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(char *multi_user_name, 
1683                                                                                                                                 int input_account_id, 
1684                                                                                                                                 int input_mailbox_id, 
1685                                                                                                                                 char *input_old_mailbox_path, 
1686                                                                                                                                 char *input_new_mailbox_path, 
1687                                                                                                                                 char *input_new_mailbox_alias, 
1688                                                                                                                                 int handle_to_be_published)
1689 {
1690         EM_DEBUG_FUNC_BEGIN_SEC("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_path [%s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, input_new_mailbox_alias, handle_to_be_published);
1691         int err = EMAIL_ERROR_NONE;
1692
1693
1694         if (err == EMAIL_ERROR_NONE) {
1695                 if ((err = emcore_rename_mailbox(multi_user_name, input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, NULL, 0, true, true, handle_to_be_published)) != EMAIL_ERROR_NONE) {
1696                         EM_DEBUG_EXCEPTION("emcore_rename_mailbox failed [%d]", err);
1697                 }
1698         }
1699
1700         EM_DEBUG_FUNC_END("err [%d]", err);
1701         return err;
1702 }
1703
1704 static void* worker_send_event_queue(void *arg)
1705 {
1706         EM_DEBUG_FUNC_BEGIN();
1707         int err = EMAIL_ERROR_NONE;
1708         email_event_t *event_data = NULL;
1709         email_event_t *started_event = NULL;
1710         int pbd_thd_state = 0;
1711         int event_que_state = 0;
1712
1713         if (!emstorage_open(NULL, &err)) {
1714                 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
1715                 return NULL;
1716         }
1717
1718         while (g_send_event_loop) {
1719
1720                 pbd_thd_state = emcore_get_pbd_thd_state();
1721                 event_que_state = emcore_is_event_queue_empty();
1722
1723                 /* get a event_data from event_data send queue */
1724                 ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1725                 if (!emcore_retrieve_send_event(&event_data, &err)) {
1726                         /* no event_data pending */
1727                         if (err != EMAIL_ERROR_EVENT_QUEUE_EMPTY) {
1728                                 LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1729                                 continue;
1730                         }
1731
1732                         send_thread_run = 0;
1733
1734                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_SENDING_WORKER, true, NULL);
1735                         
1736 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1737                         int wifi_status = 0;
1738                         if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
1739                                 EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
1740                         }
1741                         EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
1742
1743                         if (!pbd_thd_state && event_que_state && wifi_status > 1) {
1744                                 WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
1745                         }
1746 #endif
1747
1748                         //go to sleep when queue is empty
1749                         SLEEP_CONDITION_VARIABLE(_send_event_available_signal, *_send_event_queue_lock);
1750                         EM_DEBUG_LOG("Wake up by _send_event_available_signal");
1751                         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1752                 } else {
1753                         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1754                         EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
1755                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_SENDING_WORKER, false, NULL);
1756                         send_thread_run = 1;
1757
1758                         switch (event_data->type) {
1759
1760                                 case EMAIL_EVENT_SEND_MAIL:
1761                                         if (!emcore_send_mail(event_data->multi_user_name, event_data->event_param_data_4, &err))
1762                                                 EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);
1763                                         break;
1764
1765                                 case EMAIL_EVENT_SEND_MAIL_SAVED:
1766                                         /* send mails to been saved in off-line mode */
1767                                         if (!emcore_send_saved_mail(event_data->multi_user_name, event_data->account_id, event_data->event_param_data_3, &err))
1768                                                 EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);
1769                                         break;
1770
1771 #ifdef __FEATURE_LOCAL_ACTIVITY__
1772                                 case EMAIL_EVENT_LOCAL_ACTIVITY: {
1773                                         emdevice_set_sleep_on_off(false, NULL);
1774                                         emstorage_activity_tbl_t *local_activity = NULL;
1775                                         int activity_id_count = 0;
1776                                         int activity_chunk_count = 0;
1777                                         int *activity_id_list = NULL;
1778                                         int i = 0;
1779
1780                                         if (false == emstorage_get_activity_id_list(event_data->account_id, &activity_id_list, &activity_id_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1781                                                 EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
1782                                         }
1783                                         else {
1784                                                 for (i = 0; i < activity_id_count; ++i) {
1785                                                         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)) {
1786                                                                 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);
1787                                                         }
1788                                                         else {
1789                                                                 EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
1790                                                                 switch (local_activity[0].activity_type) {
1791                                                                         case ACTIVITY_SAVEMAIL:  {
1792                                                                                 if (!emcore_sync_mail_from_client_to_server(event_data->multi_user_name, event_data->account_id, local_activity[0].mail_id, &err)) {
1793                                                                                         EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
1794                                                                                 }
1795                                                                         }
1796                                                                         break;
1797
1798                                                                         case ACTIVITY_DELETEMAIL_SEND:                          /* New Activity Type Added for Race Condition and Crash Fix */ {
1799                                                                                 if (!emcore_delete_mail(local_activity[0].multi_user_name,
1800                                                                                                                                 local_activity[0].account_id,
1801                                                                                                                                 &local_activity[0].mail_id,
1802                                                                                                                                 EMAIL_DELETE_FOR_SEND_THREAD,
1803                                                                                                                                 true,
1804                                                                                                                                 EMAIL_DELETED_BY_COMMAND,
1805                                                                                                                                 false,
1806                                                                                                                                 &err))  {
1807                                                                                         EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
1808                                                                                 }
1809                                                                         }
1810                                                                         break;
1811
1812                                                                         default:  {
1813                                                                                 EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
1814                                                                         }
1815                                                                         break;
1816                                                                 }
1817
1818                                                                 emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);
1819
1820                                                                 if (g_save_local_activity_run == 1) {
1821                                                                         EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
1822                                                                         break;
1823                                                                 }
1824                                                         }
1825
1826                                                 }
1827                                                 if (false == emstorage_free_activity_id_list(activity_id_list, &err)) {
1828                                                         EM_DEBUG_LOG("emstorage_free_activity_id_list failed");
1829                                                 }
1830                                         }
1831
1832                                         emdevice_set_sleep_on_off(true, NULL);
1833                                 }
1834                                 break;
1835 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1836
1837                                 default:
1838                                         EM_DEBUG_LOG("Others not supported by Send Thread..! [%d]", event_data->type);
1839                                         break;
1840                         }
1841
1842                         ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1843                         started_event = g_queue_pop_head(g_send_event_que);
1844                         LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
1845                         if (!started_event) {
1846                                 EM_DEBUG_EXCEPTION("Failed to g_queue_pop_head");
1847                         } else {
1848                                 emcore_return_send_handle(started_event->handle);
1849                                 emcore_free_event(started_event);
1850                                 EM_SAFE_FREE(started_event);
1851                         }
1852                 }
1853         }
1854
1855         if (!emstorage_close(&err))
1856                 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
1857
1858         emcore_close_smtp_stream_list();
1859         EM_DEBUG_FUNC_END("err [%d]", err);
1860         return NULL;
1861 }
1862
1863 INTERNAL_FUNC int emdaemon_start_thread_for_downloading_partial_body(int *err_code)
1864 {
1865         EM_DEBUG_FUNC_BEGIN();
1866
1867         int i = 0, thread_error = -1;
1868
1869         /* Clear Partial Body Event Queue*/
1870         memset(&g_partial_body_thd_event_que, 0x00, sizeof(g_partial_body_thd_event_que));
1871
1872         for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
1873                 g_partial_body_thd_event_que[i].mailbox_name = NULL;
1874                 g_partial_body_thd_event_que[i].mailbox_id = 0;
1875         }
1876
1877         if (g_partial_body_thd)  {
1878                 EM_DEBUG_EXCEPTION("partial body thread is already running...");
1879                 if (err_code != NULL)
1880                         *err_code = EMAIL_ERROR_UNKNOWN;
1881
1882                 return true;
1883         }
1884
1885
1886         ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1887         g_partial_body_thd_next_event_idx = 0;
1888 //      g_partial_body_thd_loop = 1;
1889         g_partial_body_thd_queue_empty = true;
1890         g_partial_body_thd_queue_full = false;
1891         LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
1892
1893         INITIALIZE_CONDITION_VARIABLE(_partial_body_thd_cond);
1894
1895         /* create thread */
1896         /* THREAD_CREATE_JOINABLE(g_partial_body_thd, partial_body_download_thread, thread_error); */
1897         THREAD_CREATE(g_partial_body_thd, partial_body_download_thread, NULL, thread_error);
1898
1899         if (thread_error != 0) {
1900                 EM_DEBUG_EXCEPTION("cannot make thread...");
1901                 if (err_code != NULL)
1902                         *err_code = EMAIL_ERROR_UNKNOWN;
1903                 return FAILURE;
1904         }
1905
1906         if (err_code != NULL)
1907                 *err_code = EMAIL_ERROR_NONE;
1908
1909         return false;
1910
1911 }
1912
1913
1914 static gpointer partial_body_download_thread(gpointer data)
1915 {
1916         EM_DEBUG_FUNC_BEGIN();
1917
1918         int err = EMAIL_ERROR_NONE;
1919         email_session_t *session = NULL;
1920         email_event_partial_body_thd partial_body_thd_event;
1921         int i, account_count = 0;
1922         emstorage_account_tbl_t *account_list = NULL;
1923         int event_que_state = 0;
1924         int send_event_que_state = 0;
1925
1926         EM_DEBUG_LOG_DEV(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());
1927
1928         /* Open connection with DB */
1929
1930         if (false == emstorage_open(NULL, &err))  {
1931                 EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
1932                 return false;
1933         }
1934
1935         /* refactoring session required */
1936         if (!emcore_get_empty_session(&session)) {
1937                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
1938         }
1939
1940         while (1) {
1941
1942                 memset(&partial_body_thd_event, 0, sizeof(email_event_partial_body_thd));
1943
1944                 if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
1945                         if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
1946                                 EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
1947                         else {
1948                                 EM_DEBUG_LOG(" partial body thread event_data queue is empty.");
1949
1950 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1951                                 int wifi_status = 0;
1952 #endif
1953
1954                                 /*  Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count  */
1955                                 if (!g_partial_body_bulk_dwd_queue_empty) {
1956                                         partial_body_thd_event.event_type = 0;
1957                                         partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
1958                                         partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
1959                                         partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name); /* need to be freed */
1960                                         partial_body_thd_event.multi_user_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].multi_user_name); /* need to be freed */
1961
1962
1963                                         if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
1964                                                 EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
1965
1966                                         emcore_pb_thd_set_local_activity_continue(true);
1967                                 }
1968
1969                                 if (true == emcore_pb_thd_can_local_activity_continue()) {
1970                                         /*Check for local Activities */
1971                                         int is_local_activity_event_inserted = false;
1972
1973                                         if (false == emcore_partial_body_thd_local_activity_sync(partial_body_thd_event.multi_user_name, &is_local_activity_event_inserted, &err)) {
1974                                                 EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
1975                                         }
1976                                         else {
1977                                                 if (true == is_local_activity_event_inserted) {
1978                                                         emcore_pb_thd_set_local_activity_continue(false);
1979
1980 /*                                                      emcore_clear_session(session);*/
1981
1982                                                         if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
1983                                                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);
1984
1985                                                         continue;
1986                                                 }
1987                                         }
1988                                 }
1989
1990                                 EM_DEBUG_LOG_DEV(" Partial Body Thread is going to sleep");
1991                                 
1992                                 /* finalize sync */
1993                                 account_count = 0;
1994                                 account_list = NULL;
1995                                 if (!emstorage_get_account_list(NULL, &account_count, &account_list, false, false, &err)) {
1996                                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed : [%d]", err);
1997                                 }
1998
1999                                 for (i = 0; i < account_count; i++) {
2000                                         if (!emdaemon_finalize_sync(partial_body_thd_event.multi_user_name, account_list[i].account_id, 0, 0, 0, 0, true, NULL))
2001                                                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed");
2002                                 }
2003
2004                                 if (account_list)
2005                                         emstorage_free_account(&account_list, account_count, NULL);
2006         
2007                                 emcore_set_pbd_thd_state(false);
2008
2009 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2010                                 if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
2011                                         EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
2012                                 }
2013
2014                                 EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
2015
2016                                 event_que_state = emcore_is_event_queue_empty();
2017                                 send_event_que_state = emcore_is_send_event_queue_empty();
2018 #endif
2019                                 /*check: refactoring required*/
2020                                 ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2021 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2022                                 if (event_que_state && send_event_que_state && wifi_status > 1) {
2023                                         WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
2024                                 }
2025 #endif
2026                                 SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
2027                                 LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
2028
2029                                 EM_DEBUG_LOG(" Partial Body Thread wakes up ");
2030
2031                                 emcore_set_pbd_thd_state(true);
2032                         }
2033
2034                 }
2035                 else {
2036                         EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");
2037
2038                         /* Since all events are network operations dnet init and sleep control is
2039                         done before entering switch block*/
2040
2041                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_PARTIAL_BODY_WORKER, false, NULL);
2042
2043                         if (!emnetwork_check_network_status( &err))  {
2044                                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
2045                         }
2046                         else {
2047                                 /*  Process events  */
2048                                 EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);
2049
2050                                 switch (partial_body_thd_event.event_type) {
2051                                         case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD:  {
2052                                                 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
2053                                                         EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
2054                                                 }
2055                                                 break;
2056                                         }
2057                                         case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD:  {
2058                                                 partial_body_thd_event.event_type = 0;
2059
2060                                                 /* Both the checks below make sure that before starting local activity there is no new/pending event_data in
2061                                                 *   g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
2062                                                 if (false == emcore_is_partial_body_thd_que_empty())
2063                                                         break;
2064                                                 if (!g_partial_body_bulk_dwd_queue_empty)
2065                                                         break;
2066
2067                                                 if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
2068                                                         EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
2069                                                 break;
2070                                         }
2071                                         default:
2072                                                 EM_DEBUG_EXCEPTION(" Warning :  Default case entered. This should not happen ");
2073                                                 break;
2074                                 }
2075                         }
2076
2077                         if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, NULL))
2078                                 EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed");
2079
2080                         emdevice_set_sleep_on_off(STAY_AWAKE_FLAG_FOR_PARTIAL_BODY_WORKER, true, NULL);
2081                 }
2082
2083 /*              emcore_clear_session(session); */
2084         }
2085
2086         emcore_close_recv_stream_list();
2087         emcore_clear_session(session);
2088         return SUCCESS;
2089 }