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