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