Fixed the invalid casting and remove the flash noti
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-mail.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /******************************************************************************
25  * File: email-daemon-mail.c
26  * Desc: email-daemon Mail Operation
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <glib.h>
39 #include <unistd.h>
40 #include <malloc.h>
41 #include <pthread.h>
42
43 #include "email-internal-types.h"
44 #include "email-daemon.h"
45 #include "email-core-event.h"
46 #include "email-daemon-account.h"
47 #include "email-debug-log.h"
48 #include "email-storage.h"
49 #include "email-utilities.h"
50 #include "email-core-account.h"
51 #include "email-core-mail.h"
52 #include "email-core-mailbox.h"
53 #include "email-core-utils.h"
54 #include "email-core-smtp.h"
55 #include "email-core-timer.h"
56 #include "email-core-signal.h"
57
58 #ifdef __FEATURE_LOCAL_ACTIVITY__
59 extern int g_local_activity_run;
60 extern int g_save_local_activity_run;
61 #endif
62 static int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code);
63
64 INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *handle, int* err_code)
65 {
66         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p], err_code[%p]", mail_id, handle, err_code);
67
68         int ret = false, err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
69         int  result_handle = 0, account_id = 0;
70         email_event_t *event_data = NULL;
71         emstorage_mail_tbl_t* mail_table_data = NULL;
72         emstorage_mailbox_tbl_t* local_mailbox = NULL;
73         email_account_t* ref_account = NULL;
74         int dst_mailbox_id = 0;
75
76         if(mail_id <= 0) {
77                 EM_DEBUG_EXCEPTION("mail_id is not valid");
78                 err= EMAIL_ERROR_INVALID_PARAM;
79                 goto FINISH_OFF;
80         }
81
82         if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err)) {
83                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
84                 goto FINISH_OFF;
85         }
86
87         if (!mail_table_data || mail_table_data->mailbox_id <= 0 || mail_table_data->account_id <= 0) {
88                 if (mail_table_data != NULL)
89                         EM_DEBUG_EXCEPTION(" mail_table_data->mailbox_id[%d], mail_table_data->account_id[%d]", mail_table_data->mailbox_id, mail_table_data->account_id);
90                 if (err_code)
91                         *err_code = EMAIL_ERROR_INVALID_MAILBOX;
92                 if(mail_table_data)
93                         emstorage_free_mail(&mail_table_data, 1, &err);
94                 return false;
95         }
96
97         account_id = mail_table_data->account_id;
98
99         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
100         if (!ref_account) {
101                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
102                 err = EMAIL_ERROR_INVALID_ACCOUNT;
103                 goto FINISH_OFF;
104         }
105
106 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
107         if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
108                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
109                 goto FINISH_OFF;
110         }
111         dst_mailbox_id = local_mailbox->mailbox_id;
112         if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
113                         /*  mail is moved to 'OUTBOX' first of all. */
114                 if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
115                         EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
116                         goto FINISH_OFF;
117                 }
118         }
119 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
120
121         if(!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
122                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
123
124         /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
125         if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
126                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
127                 goto FINISH_OFF;
128         }
129
130         event_data = em_malloc(sizeof(email_event_t));
131         if (event_data == NULL) {
132                 EM_DEBUG_EXCEPTION("em_malloc failed");
133                 err = EMAIL_ERROR_OUT_OF_MEMORY;
134                 goto FINISH_OFF;
135         }
136
137         event_data->type               = EMAIL_EVENT_SEND_MAIL;
138         event_data->account_id         = account_id;
139         event_data->event_param_data_4 = mail_id;
140         event_data->event_param_data_5 = mail_table_data->mailbox_id;
141         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
142
143         if (!emcore_insert_event_for_sending_mails(event_data, &result_handle, &err)) {
144                 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
145                 goto FINISH_OFF;
146         }
147
148 #ifdef __FEATURE_LOCAL_ACTIVITY__
149                         EM_DEBUG_LOG("Setting g_save_local_activity_run ");
150                         g_save_local_activity_run = 1;
151 #endif
152
153         if ( handle )
154                 *handle = result_handle;
155
156         ret = true;
157
158 FINISH_OFF:
159         if (ret == false) {
160                 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
161
162                 if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
163                         EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
164
165                 if (event_data) {
166                         emcore_free_event(event_data);
167                         EM_SAFE_FREE(event_data);
168                 }
169         }
170
171         if (ref_account) {
172                 emcore_free_account(ref_account);
173                 EM_SAFE_FREE(ref_account);
174         }
175
176         if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
177                 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
178
179         if(local_mailbox)
180                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
181
182         if (err_code != NULL)
183                 *err_code = err;
184
185         if(mail_table_data)
186                 emstorage_free_mail(&mail_table_data, 1, &err);
187
188         EM_DEBUG_FUNC_END();
189         return ret;
190 }
191
192 INTERNAL_FUNC int emdaemon_send_mail_saved(char *multi_user_name, int account_id, int *handle, int* err_code)
193 {
194         EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
195
196         int ret = false;
197         int err = EMAIL_ERROR_NONE;
198         email_event_t *event_data = NULL;
199         email_account_t *ref_account = NULL;
200         char *mailbox_name = NULL;
201
202         if (account_id <= 0) {
203                 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
204                 err = EMAIL_ERROR_INVALID_PARAM;
205                 goto FINISH_OFF;
206         }
207
208         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
209         if (!ref_account) {
210                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
211                 err = EMAIL_ERROR_INVALID_ACCOUNT;
212                 goto FINISH_OFF;
213         }
214
215         if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
216                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
217                 goto FINISH_OFF;
218         }
219
220         event_data = em_malloc(sizeof(email_event_t));
221         if (event_data == NULL) {
222                 EM_DEBUG_EXCEPTION("em_malloc failed");
223                 err = EMAIL_ERROR_OUT_OF_MEMORY;
224                 goto FINISH_OFF;
225         }
226
227         event_data->type               = EMAIL_EVENT_SEND_MAIL_SAVED;
228         event_data->account_id         = account_id;
229         event_data->event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
230         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
231
232         if (!emcore_insert_event_for_sending_mails(event_data, (int *)handle, &err)) {
233                 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
234                 goto FINISH_OFF;
235         }
236
237         ret = true;
238
239 FINISH_OFF:
240
241         if (ret == false && event_data) {
242                 emcore_free_event(event_data);
243                 EM_SAFE_FREE(event_data);
244         }
245
246         if (ref_account) {
247                 emcore_free_account(ref_account);
248                 EM_SAFE_FREE(ref_account);
249         }
250
251         EM_SAFE_FREE(mailbox_name);
252
253         if (err_code)
254                 *err_code = err;
255
256         EM_DEBUG_FUNC_END();
257         return ret;
258 }
259
260 INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name, 
261                                         email_mail_data_t *input_mail_data, 
262                                         email_attachment_data_t *input_attachment_data_list, 
263                                         int input_attachment_count, 
264                                         email_meeting_request_t *input_meeting_request, 
265                                         int input_from_eas)
266 {
267         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
268
269         int ret = false;
270         int err = EMAIL_ERROR_NONE;
271         int handle = 0;
272         email_event_t *event_data = NULL;
273         email_account_t *ref_account = NULL;
274
275         if (!input_mail_data || input_mail_data->account_id <= 0 ||
276                 ( ((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to))  {
277                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
278                 err = EMAIL_ERROR_INVALID_PARAM;
279                 goto FINISH_OFF;
280         }
281
282         ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
283         if (!ref_account)  {
284                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
285                 err = EMAIL_ERROR_INVALID_ACCOUNT;
286                 goto FINISH_OFF;
287         }
288
289         if ((err = emcore_add_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false)) != EMAIL_ERROR_NONE) {
290                 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
291                 goto FINISH_OFF;
292         }
293
294 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
295         if ( input_from_eas == 0 && ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
296                 event_data = em_malloc(sizeof(email_event_t));
297                 if (event_data == NULL) {
298                         EM_DEBUG_EXCEPTION("em_malloc failed");
299                         err = EMAIL_ERROR_OUT_OF_MEMORY;
300                         goto FINISH_OFF;
301                 }
302
303                 event_data->type               = EMAIL_EVENT_SAVE_MAIL;
304                 event_data->account_id         = input_mail_data->account_id;
305                 event_data->event_param_data_4 = input_mail_data->mail_id;
306                 event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
307
308                 if (!emcore_insert_event(event_data, &handle, &err)) {
309                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
310                         goto FINISH_OFF;
311                 }
312         }
313 #endif
314
315         ret = true;
316
317 FINISH_OFF:
318
319         if (ret == false && event_data) {
320                 emcore_free_event(event_data);
321                 EM_SAFE_FREE(event_data);
322         }
323
324         if (ref_account) {
325                 emcore_free_account(ref_account);
326                 EM_SAFE_FREE(ref_account);
327         }
328
329         EM_DEBUG_FUNC_END("err [%d]", err);
330
331         return err;
332 }
333
334
335 INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
336 {
337         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
338
339         /*  default variable */
340         int ret = false;
341         int err = EMAIL_ERROR_NONE;
342
343         if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 )  {
344                 if(meeting_req)
345                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
346
347                 err = EMAIL_ERROR_INVALID_PARAM;
348                 goto FINISH_OFF;
349         }
350
351         if (!emcore_add_meeting_request(multi_user_name, account_id, input_mailbox_id, meeting_req, &err))  {
352                 EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
353                 goto FINISH_OFF;
354         }
355
356         ret = true;
357
358 FINISH_OFF:
359         if (err_code)
360                 *err_code = err;
361         EM_DEBUG_FUNC_END();
362         return ret;
363 }
364
365 INTERNAL_FUNC int emdaemon_download_body(char *multi_user_name, int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code)
366 {
367         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], verbose[%d], with_attachment[%d], handle[%p], err_code[%p]", account_id, mail_id, verbose, with_attachment,  handle, err_code);
368
369         int ret = false;
370         int err = EMAIL_ERROR_NONE;
371         email_event_t *event_data = NULL;
372
373         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
374                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
375                 goto FINISH_OFF;
376         }
377
378         event_data = em_malloc(sizeof(email_event_t));
379         if (event_data == NULL) {
380                 EM_DEBUG_EXCEPTION("em_malloc failed");
381                 err = EMAIL_ERROR_OUT_OF_MEMORY;
382                 goto FINISH_OFF;
383         }
384
385         event_data->type = EMAIL_EVENT_DOWNLOAD_BODY;
386         event_data->account_id = account_id;
387         event_data->event_param_data_4 = mail_id;
388         event_data->event_param_data_5 = (verbose << 1 | with_attachment);
389         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
390
391         if (!emcore_insert_event(event_data, (int*)handle, &err)) {
392                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
393                 err = EMAIL_ERROR_DB_FAILURE;
394                 goto FINISH_OFF;
395         }
396
397 #ifdef __FEATURE_LOCAL_ACTIVITY__
398         EM_DEBUG_LOG("Setting g_local_activity_run ");
399         g_local_activity_run = 1;
400 #endif
401         ret = true;
402
403 FINISH_OFF:
404
405         if (ret == false && event_data) {
406                 emcore_free_event(event_data);
407                 EM_SAFE_FREE(event_data);
408         }
409
410         if (err_code)
411                 *err_code = err;
412
413         EM_DEBUG_FUNC_END();
414
415         return ret;
416 }
417
418 int emdaemon_get_attachment(char *multi_user_name, int attachment_id, email_attachment_data_t** attachment, int* err_code)
419 {
420         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
421
422         /*  default variable */
423         int ret = false;
424         int err = EMAIL_ERROR_NONE;
425
426         if (!attachment_id || !attachment)  {
427                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%d], attachment[%p]\n", attachment_id, attachment);
428                 err = EMAIL_ERROR_INVALID_PARAM;
429                 goto FINISH_OFF;
430         }
431
432         if (!emcore_get_attachment_info(multi_user_name, attachment_id, attachment, &err) || !attachment)  {
433                 EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
434                 goto FINISH_OFF;
435         }
436
437         ret = true;
438
439 FINISH_OFF:
440
441         if (err_code)
442                 *err_code = err;
443
444         EM_DEBUG_FUNC_END();
445
446         return ret;
447 }
448
449 int emdaemon_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t* attachment, int* err_code)
450 {
451         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
452
453         /*  default variable */
454         int ret = false;
455         int err = EMAIL_ERROR_NONE;
456
457         if (!attachment)  {
458                 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
459                 err = EMAIL_ERROR_INVALID_PARAM;
460                 goto FINISH_OFF;
461         }
462
463         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
464                 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
465                 goto FINISH_OFF;
466         }
467
468         if (!emcore_add_attachment(multi_user_name, mail_id, attachment, &err))  {
469                 EM_DEBUG_EXCEPTION(" emcore_add_attachment failed [%d]", err);
470                 goto FINISH_OFF;
471         }
472
473         ret = true;
474
475 FINISH_OFF:
476
477         if (err_code)
478                 *err_code = err;
479
480         EM_DEBUG_FUNC_END();
481
482         return ret;
483 }
484
485 int emdaemon_delete_mail_attachment(char *multi_user_name, int attachment_id, int* err_code)
486 {
487         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
488
489         /*  default variable */
490         int ret = false;
491         int err = EMAIL_ERROR_NONE;
492
493         if (!attachment_id)  {
494                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
495                 err = EMAIL_ERROR_INVALID_PARAM;
496                 goto FINISH_OFF;
497         }
498
499         if (!emcore_delete_mail_attachment(multi_user_name, attachment_id, &err))  {
500                 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
501                 goto FINISH_OFF;
502         }
503
504         ret = true;
505
506 FINISH_OFF:
507
508         if (err_code)
509                 *err_code = err;
510
511         EM_DEBUG_FUNC_END();
512
513         return ret;
514 }
515
516 INTERNAL_FUNC int emdaemon_download_attachment(char *multi_user_name, int account_id, int mail_id, int nth, int *handle, int* err_code)
517 {
518         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], handle[%p], err_code[%p]", account_id, mail_id, nth, handle, err_code);
519
520         int ret = false;
521         int err = EMAIL_ERROR_NONE;
522         email_event_t *event_data = NULL;
523
524         if (!nth) {
525                 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
526                 err = EMAIL_ERROR_INVALID_PARAM;
527                 goto FINISH_OFF;
528         }
529
530         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
531                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
532                 goto FINISH_OFF;
533         }
534
535         event_data = em_malloc(sizeof(email_event_t));
536         if (event_data == NULL) {
537                 EM_DEBUG_EXCEPTION("em_malloc failed");
538                 err = EMAIL_ERROR_OUT_OF_MEMORY;
539                 goto FINISH_OFF;
540         }
541
542         event_data->type = EMAIL_EVENT_DOWNLOAD_ATTACHMENT;
543         event_data->account_id         = account_id;
544         event_data->event_param_data_4 = mail_id;
545         event_data->event_param_data_5 = nth;
546         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
547
548         if (!emcore_insert_event(event_data, (int*)handle, &err))  {
549                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
550                 goto FINISH_OFF;
551         }
552
553 #ifdef __FEATURE_LOCAL_ACTIVITY__
554         EM_DEBUG_LOG("Setting g_local_activity_run ");
555         g_local_activity_run = 1;
556 #endif
557
558         ret = true;
559
560 FINISH_OFF:
561
562         if (ret == false && event_data) {
563                 emcore_free_event(event_data);
564                 EM_SAFE_FREE(event_data);
565         }
566
567         if (err_code)
568                 *err_code = err;
569
570         EM_DEBUG_FUNC_END();
571
572         return ret;
573 }
574
575 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
576 {
577         EM_DEBUG_FUNC_BEGIN();
578         return emcore_free_attachment_data(atch_data, 1, err_code);
579 }
580
581 void* thread_func_to_delete_mail(void *thread_argument)
582 {
583         EM_DEBUG_FUNC_BEGIN();
584         int  err = EMAIL_ERROR_NONE;
585         int *mail_id_list = NULL;
586         int  mail_id_count = 0;
587         int  account_id = 0;
588         int  from_server = 0;
589         int  noti_param_2 = 0;
590         int  handle = 0;
591         char *multi_user_name = NULL;
592         email_event_t *event_data = (email_event_t*)thread_argument;
593
594         account_id         = event_data->account_id;
595         mail_id_list       = (int*)event_data->event_param_data_3;
596         mail_id_count      = event_data->event_param_data_4;
597         from_server        = event_data->event_param_data_5;
598         multi_user_name    = event_data->multi_user_name;
599
600         if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, 
601                                                         mail_id_count, EMAIL_DELETE_LOCALLY, 
602                                                         EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
603                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
604                 emcore_free_event(event_data); /* prevent 17922 */
605                 EM_SAFE_FREE(event_data);
606                 goto FINISH_OFF;
607         }
608
609         if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
610                 if (!emcore_insert_event(event_data, &handle, &err)) {
611                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
612 /*                      if (from_server != EMAIL_DELETE_LOCAL_AND_SERVER && from_server != EMAIL_DELETE_FROM_SERVER) {
613                                 EM_SAFE_FREE(event_data->event_param_data_3);
614                         } */
615                         emcore_free_event(event_data); /* prevent 17922 */
616                         EM_SAFE_FREE(event_data);
617                         goto FINISH_OFF;
618                 }
619         }
620         else {
621                 emcore_free_event(event_data); /* prevent 17922 */
622                 EM_SAFE_FREE(event_data);
623         }
624
625 FINISH_OFF:
626         /* Don't free event_data as if the data is destined to be passed to event handler through emcore_insert_event.*/
627
628         EM_DEBUG_FUNC_END();
629         return SUCCESS;
630 }
631
632 INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, int *handle, int* err_code)
633 {
634         EM_DEBUG_FUNC_BEGIN("mailbox_id[%d], mail_ids[%p], mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]", mailbox_id, mail_ids, mail_ids_count, from_server, handle, err_code);
635
636         int ret = false;
637         int err = EMAIL_ERROR_NONE;
638         int *p = NULL;
639         int thread_error = 0;
640         email_account_t *ref_account = NULL;
641         email_event_t *event_data = NULL;
642         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
643         thread_t delete_thread;
644
645         /* mailbox can be NULL for deleting thread mail. */
646         if (mail_ids_count <= 0) {
647                 EM_DEBUG_EXCEPTION("mail_ids_count [%d]", mail_ids_count);
648                 err = EMAIL_ERROR_INVALID_PARAM;
649                 goto FINISH_OFF;
650         }
651
652         if ( (err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
653                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
654                 goto FINISH_OFF;
655         }
656
657         if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
658                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
659                 err = EMAIL_ERROR_OUT_OF_MEMORY;
660                 goto FINISH_OFF;
661         }
662
663         memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
664
665         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
666                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
667                 err = EMAIL_ERROR_OUT_OF_MEMORY;
668                 goto FINISH_OFF;
669         }
670
671         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl_data->account_id, false);
672         if (!ref_account) {
673                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
674                 err = EMAIL_ERROR_INVALID_ACCOUNT;
675                 goto FINISH_OFF;
676         }
677
678         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
679                 from_server = EMAIL_DELETE_LOCALLY;
680         }
681
682         event_data->type                   = EMAIL_EVENT_DELETE_MAIL;
683         event_data->account_id             = mailbox_tbl_data->account_id;
684         event_data->event_param_data_3     = (char*)p;
685         event_data->event_param_data_4     = mail_ids_count;
686         event_data->event_param_data_5     = from_server;
687         event_data->multi_user_name        = EM_SAFE_STRDUP(multi_user_name);
688
689         THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
690         THREAD_DETACH(delete_thread); /* free resources used for new thread */
691         ret = true;
692
693 FINISH_OFF:
694
695         if (mailbox_tbl_data)
696                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
697
698         if (ref_account) {
699                 emcore_free_account(ref_account);
700                 EM_SAFE_FREE(ref_account);
701         }
702
703         if (err != EMAIL_ERROR_NONE || thread_error != 0) {
704             emcore_free_event(event_data);
705                 EM_SAFE_FREE(event_data);
706                 EM_SAFE_FREE(p);
707         }
708
709         if (err_code)
710                 *err_code = err;
711
712         return ret;
713 }
714
715 int emdaemon_delete_mail_all(char *multi_user_name, int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
716 {
717         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_from_server[%d], handle[%p], err_code[%p]", input_mailbox_id, input_from_server, output_handle, output_err_code);
718
719         int ret = false;
720         int err = EMAIL_ERROR_NONE;
721         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
722         email_event_t *event_data = NULL;
723
724         if (!input_mailbox_id) {
725                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
726                 err = EMAIL_ERROR_INVALID_PARAM;
727                 goto FINISH_OFF;
728         }
729
730         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
731                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
732                 goto FINISH_OFF;
733         }
734
735         if(!emcore_delete_all_mails_of_mailbox(multi_user_name, mailbox_tbl->account_id, input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
736                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
737                 goto FINISH_OFF;
738         }
739
740 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
741         if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
742                 event_data = em_malloc(sizeof(email_event_t));
743                 if (event_data == NULL) {
744                         EM_DEBUG_EXCEPTION("em_malloc failed");
745                         err = EMAIL_ERROR_OUT_OF_MEMORY;
746                         goto FINISH_OFF;
747                 }
748
749                 event_data->type               = EMAIL_EVENT_DELETE_MAIL_ALL;
750                 event_data->account_id         = mailbox_tbl->account_id;
751                 event_data->event_param_data_4 = input_mailbox_id;
752                 event_data->event_param_data_5 = input_from_server;
753                 event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
754
755                 if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
756                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
757                         goto FINISH_OFF;
758                 }
759
760 #ifdef __FEATURE_LOCAL_ACTIVITY__
761                 int i, total = 0;
762                 DB_STMT search_handle = 0;
763                 int *mail_ids = NULL;
764                 emstorage_activity_tbl_t new_activity;
765                 int activityid = 0;
766
767                 if (false == emcore_get_next_activity_id(&activityid,&err)) {
768                         EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
769                 }
770
771                 if (!emstorage_mail_search_start(multi_user_name, NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
772                         EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
773                         goto FINISH_OFF;
774                 }
775
776                 mail_ids = em_malloc(sizeof(int) * total);
777                 if (mail_ids == NULL)  {
778                         EM_DEBUG_EXCEPTION(" mailloc failed...");
779                         err = EMAIL_ERROR_OUT_OF_MEMORY;
780                         goto FINISH_OFF;
781                 }
782
783                 for (i = 0; i < total; i++)  {
784                         if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err))  {
785                                 EM_DEBUG_EXCEPTION(" emstorage_mail_search_result failed [%d]", err);
786
787
788                                 EM_SAFE_FREE(mail_ids);
789                                 goto FINISH_OFF;
790                         }
791
792                         new_activity.activity_id        = activityid;
793                         new_activity.activity_type      = ACTIVITY_DELETEMAIL;
794                         new_activity.mail_id            = mail_ids[i];
795                         new_activity.server_mailid      = NULL;
796                         new_activity.src_mbox           = mailbox->mailbox_name;
797                         new_activity.dest_mbox          = NULL;
798                         new_activity.account_id         = mailbox->account_id;
799
800                         if (! emcore_add_activity(&new_activity, &err))
801                                 EM_DEBUG_EXCEPTION(" emcore_add_activity  Failed  - %d ", err);
802
803                 }
804
805                 EM_SAFE_FREE(mail_ids);
806
807                 EM_DEBUG_LOG("Setting g_local_activity_run ");
808                 g_local_activity_run = 1;
809 #endif
810         }
811 #endif  /*  __FEATURE_SYNC_CLIENT_TO_SERVER__ */
812
813         ret = true;
814
815 FINISH_OFF:
816
817         if (ret == false && event_data) {
818                 emcore_free_event(event_data);
819                 EM_SAFE_FREE(event_data);
820         }
821
822         if (mailbox_tbl)
823                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
824
825         if (output_err_code)
826                 *output_err_code = err;
827
828         return ret;
829 }
830
831 void* thread_func_to_move_mail(void *thread_argument)
832 {
833         EM_DEBUG_FUNC_BEGIN();
834
835         int ret = false;
836         int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
837         int handle = 0;
838         email_event_t *event_data = (email_event_t*)thread_argument;
839         int dst_mailbox_id = 0;
840         char *multi_user_name = NULL;
841
842         if(!event_data) { /*prevent 53096*/
843                 EM_DEBUG_EXCEPTION("INVALID_PARMAETER");
844                 return NULL;
845         }
846
847
848 /*      dst_mailbox_name   = (char*)event_data->event_param_data_1; */ /*prevent 33693*/
849         mail_ids           = (int*)event_data->event_param_data_3;
850         mail_ids_count     = event_data->event_param_data_4;
851         dst_mailbox_id     = event_data->event_param_data_5;
852         noti_param_1       = event_data->event_param_data_6;
853         noti_param_2       = event_data->event_param_data_7;
854         multi_user_name    = event_data->multi_user_name;
855
856         if (!emcore_move_mail(multi_user_name, mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
857                 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
858                 goto FINISH_OFF;
859         }
860
861         if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
862                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
863                 goto FINISH_OFF;
864         }
865
866         ret = true;
867
868 FINISH_OFF:
869
870         if (ret == false && event_data) {
871                 emcore_free_event(event_data);
872                 EM_SAFE_FREE(event_data);
873         }
874
875         EM_DEBUG_FUNC_END();
876         return SUCCESS;
877 }
878
879 INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_mailbox_id, int dst_mailbox_id, int* err_code)
880 {
881         EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
882
883         int ret = false;
884         int err = EMAIL_ERROR_NONE;
885         int select_num = 0;
886         int *mails = NULL;
887         int *p = NULL;
888         int i = 0;
889         int num = 0;
890         int thread_error = 0;
891         email_account_t *ref_account = NULL;
892         email_mail_list_item_t *mail_list = NULL;
893         email_event_t *event_data = NULL;
894         thread_t move_thread;
895         emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
896         emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
897
898         if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0)  {
899                 EM_DEBUG_EXCEPTION("Invalid Parameter");
900                 err = EMAIL_ERROR_INVALID_PARAM;
901                 goto FINISH_OFF;
902         }
903
904         if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
905                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
906                 goto FINISH_OFF;
907         }
908
909         if ((err = emstorage_get_mailbox_by_id(multi_user_name, src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
910                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
911                 goto FINISH_OFF;
912         }
913
914         ref_account = emcore_get_account_reference(multi_user_name, dst_mailbox_tbl->account_id, false);
915         if (!ref_account)  {
916                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
917                 err = EMAIL_ERROR_INVALID_ACCOUNT;
918                 goto FINISH_OFF;
919         }
920
921         if(!emstorage_get_mail_list(multi_user_name, src_mailbox_tbl->account_id, src_mailbox_id, NULL, EMAIL_LIST_TYPE_NORMAL, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, false, &mail_list, &select_num, &err)) {
922                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
923                 goto FINISH_OFF;
924         }
925
926         mails = malloc(sizeof(int) * select_num);
927
928         if( !mails ) {
929                 EM_DEBUG_EXCEPTION("Malloc failed...!");
930                 err = EMAIL_ERROR_OUT_OF_MEMORY;
931                 goto FINISH_OFF;
932         }
933
934         memset(mails, 0x00, sizeof(int) * select_num);
935
936         for(i = 0 ; i < select_num ; i++) {
937                 if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
938                         mails[num] = mail_list[i].mail_id;
939                         num++;
940                 }
941         }
942
943         if( num <= 0) {
944                 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
945                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
946                 goto FINISH_OFF;
947         }
948
949         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
950                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
951                 err = EMAIL_ERROR_OUT_OF_MEMORY;
952                 goto FINISH_OFF;
953         }
954
955         if ((p = em_malloc(sizeof(int) * num)) == NULL) {
956                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
957                 err = EMAIL_ERROR_OUT_OF_MEMORY;
958                 goto FINISH_OFF;
959         }
960
961         memcpy(p, mails, sizeof(int) * num);
962
963         event_data->account_id         = dst_mailbox_tbl->account_id;
964         event_data->type               = EMAIL_EVENT_MOVE_MAIL;
965         event_data->event_param_data_3 = (char*)p;
966         event_data->event_param_data_4 = num;
967         event_data->event_param_data_5 = dst_mailbox_id;
968         event_data->event_param_data_8 = src_mailbox_id;
969         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
970
971 #ifdef __FEATURE_LOCAL_ACTIVITY__
972         int i = 0, activityid = 0;
973
974         if (false == emcore_get_next_activity_id(&activityid,&err))
975                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
976
977         for (i =0; i < event_data.event_param_data_4; i++) {
978                 emstorage_activity_tbl_t        new_activity;
979                 new_activity.activity_id = activityid;
980                 new_activity.activity_type = ACTIVITY_MOVEMAIL;
981                 new_activity.account_id    = event_data.account_id;
982                 new_activity.mail_id       = mail_ids[i];
983                 new_activity.dest_mbox     = event_data.event_param_data_1;
984                 new_activity.server_mailid = NULL;
985                 new_activity.src_mbox      = event_data.event_param_data_2;
986
987                 if (!emcore_add_activity(&new_activity, &err))
988                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
989         }
990 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
991         THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
992         THREAD_DETACH(move_thread); /* free resources used for new thread */
993         EM_DEBUG_LOG("thread_error [%d]", thread_error);
994         ret = true;
995
996 FINISH_OFF:
997
998 #ifdef __FEATURE_LOCAL_ACTIVITY__
999         EM_DEBUG_LOG("Setting g_local_activity_run ");
1000         g_local_activity_run = 1;
1001 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1002
1003         if (err_code)
1004                 *err_code = err;
1005
1006         if (ref_account) {
1007                 emcore_free_account(ref_account);
1008                 EM_SAFE_FREE(ref_account);
1009         }
1010
1011         if (dst_mailbox_tbl)
1012                 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
1013
1014         if (src_mailbox_tbl)
1015                 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
1016
1017         if(mail_list)
1018                 EM_SAFE_FREE(mail_list);
1019
1020         if(mails != NULL )
1021                 EM_SAFE_FREE(mails);
1022
1023     if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1024         emcore_free_event(event_data);
1025         EM_SAFE_FREE(event_data);
1026         EM_SAFE_FREE(p);
1027     }
1028
1029         EM_DEBUG_FUNC_END("ret [%d]", ret);
1030         return ret;
1031 }
1032
1033 INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
1034 {
1035         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
1036
1037         /*  default variable */
1038         int mail_id = 0, *p = NULL, thread_error = 0;
1039         int ret = false, err = EMAIL_ERROR_NONE;
1040         emstorage_mail_tbl_t* mail_table_data = NULL;
1041         email_account_t* ref_account = NULL;
1042         email_event_t *event_data = NULL;
1043         thread_t move_thread;
1044         emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
1045         int src_mailbox_id = 0;
1046
1047         if (num <= 0 || dst_mailbox_id <= 0) {
1048                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1049                 err = EMAIL_ERROR_INVALID_PARAM;
1050                 goto FINISH_OFF;
1051         }
1052
1053         if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
1054                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1055                 goto FINISH_OFF;
1056         }
1057
1058         ref_account = emcore_get_account_reference(multi_user_name, dest_mailbox_tbl->account_id, false);
1059
1060         if (!ref_account) {
1061                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
1062                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1063                 goto FINISH_OFF;
1064         }
1065
1066         /* Getting source mailbox name */
1067         mail_id = mail_ids[0];
1068
1069         if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
1070                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1071                 goto FINISH_OFF;
1072         }
1073
1074         if (src_mailbox_id == 0)
1075                 src_mailbox_id = mail_table_data->mailbox_id;
1076
1077         emstorage_free_mail(&mail_table_data, 1, NULL);
1078
1079         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
1080                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
1081                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1082                 goto FINISH_OFF;
1083         }
1084
1085         if ((p = em_malloc(sizeof(int) * num)) == NULL) {
1086                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
1087                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1088                 goto FINISH_OFF;
1089         }
1090
1091         memcpy(p, mail_ids, sizeof(int) * num);
1092
1093         event_data->account_id        = dest_mailbox_tbl->account_id;
1094         event_data->type               = EMAIL_EVENT_MOVE_MAIL;
1095         event_data->event_param_data_3 = (char*)p;
1096         event_data->event_param_data_4 = num;
1097         event_data->event_param_data_5 = dst_mailbox_id;
1098         event_data->event_param_data_8 = src_mailbox_id;
1099         event_data->event_param_data_6 = noti_param_1;
1100         event_data->event_param_data_7 = noti_param_2;
1101         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
1102
1103 #ifdef __FEATURE_LOCAL_ACTIVITY__
1104         int i = 0, activityid = 0;
1105
1106         if (false == emcore_get_next_activity_id(&activityid,&err))
1107                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
1108
1109         for (i =0; i < event_data.event_param_data_4; i++) {
1110                 emstorage_activity_tbl_t        new_activity;
1111                 new_activity.activity_id = activityid;
1112                 new_activity.activity_type = ACTIVITY_MOVEMAIL;
1113                 new_activity.account_id    = event_data.account_id;
1114                 new_activity.mail_id       = mail_ids[i];
1115                 new_activity.dest_mbox     = event_data.event_param_data_1;
1116                 new_activity.server_mailid = NULL;
1117                 new_activity.src_mbox      = event_data.event_param_data_2;
1118
1119                 if (!emcore_add_activity(&new_activity, &err))
1120                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
1121         }
1122 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1123         THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
1124         THREAD_DETACH(move_thread); /* free resources used for new thread */
1125         EM_DEBUG_LOG("thread_error [%d]", thread_error);
1126         ret = true;
1127
1128 FINISH_OFF:
1129
1130 #ifdef __FEATURE_LOCAL_ACTIVITY__
1131         EM_DEBUG_LOG("Setting g_local_activity_run ");
1132         g_local_activity_run = 1;
1133 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1134
1135         if (err_code)
1136                 *err_code = err;
1137
1138         if (ref_account) {
1139                 emcore_free_account(ref_account);
1140                 EM_SAFE_FREE(ref_account);
1141         }
1142
1143         if (dest_mailbox_tbl)
1144                 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1145
1146     if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1147         emcore_free_event(event_data);
1148         EM_SAFE_FREE(event_data);
1149         EM_SAFE_FREE(p);
1150     }
1151
1152         EM_DEBUG_FUNC_END("ret [%d]", ret);
1153         return ret;
1154 }
1155
1156 INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int onserver, int* err_code)
1157 {
1158         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], field_type [%d], value[%d], err_code[%p]", mail_ids, num, field_type, value, err_code); /*prevent 27460*/
1159
1160         int ret = false, err = EMAIL_ERROR_NONE;
1161         emstorage_account_tbl_t *account_tbl = NULL;
1162         int *mail_id_array = NULL;
1163         email_event_t *event_data = NULL;
1164         int handle = 0;
1165
1166         if(account_id <= 0 || !mail_ids || num <= 0) {
1167                 err = EMAIL_ERROR_INVALID_PARAM;
1168                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1169                 goto FINISH_OFF;
1170         }
1171
1172         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
1173                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
1174                 goto FINISH_OFF;
1175         }
1176
1177         if (!emcore_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, &err))  {
1178                 EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
1179                 goto FINISH_OFF;
1180         }
1181
1182         if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
1183                 mail_id_array = em_malloc(sizeof(int) * num);
1184
1185                 if (mail_id_array == NULL)  {
1186                         EM_DEBUG_EXCEPTION("em_malloc failed...");
1187                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1188                         goto FINISH_OFF;
1189                 }
1190
1191                 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1192
1193                 event_data = em_malloc(sizeof(email_event_t));
1194                 if (event_data == NULL) {
1195                         EM_DEBUG_EXCEPTION("em_malloc failed");
1196                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1197                         goto FINISH_OFF;
1198                 }
1199
1200                 event_data->type               = EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER;
1201                 event_data->account_id         = account_id;
1202                 event_data->event_param_data_1 = NULL;
1203                 event_data->event_param_data_3 = (char*)mail_id_array;
1204                 event_data->event_param_data_4 = num;
1205                 event_data->event_param_data_5 = field_type;
1206                 event_data->event_param_data_6 = value;
1207                 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1208
1209                 if (!emcore_insert_event(event_data, (int*)&handle, &err))  {
1210                         EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1211                         goto FINISH_OFF;
1212                 }
1213         }
1214
1215         ret = true;
1216
1217 FINISH_OFF:
1218
1219         if (ret == false && event_data) {
1220                 emcore_free_event(event_data);
1221                 EM_SAFE_FREE(event_data);
1222         }
1223
1224         if (account_tbl)
1225                 emstorage_free_account(&account_tbl, 1, NULL);
1226
1227         if (err_code)
1228                 *err_code = err;
1229         EM_DEBUG_FUNC_END();
1230         return ret;
1231 }
1232
1233
1234 INTERNAL_FUNC int emdaemon_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)
1235 {
1236         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
1237         int err = EMAIL_ERROR_NONE;
1238         /*email_event_t *event_data = NULL;*/
1239         email_account_t *ref_account = NULL;
1240
1241         if (!input_mail_data || input_mail_data->account_id <= 0 || input_mail_data->mail_id == 0 ||
1242                 (((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to))  {
1243                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1244                 err = EMAIL_ERROR_INVALID_PARAM;
1245                 goto FINISH_OFF;
1246         }
1247
1248         ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
1249         if (!ref_account)  {
1250                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
1251                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1252                 goto FINISH_OFF;
1253         }
1254
1255         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) {
1256                 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1257                 goto FINISH_OFF;
1258         }
1259
1260 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1261 /*      if ( input_from_eas == 0) {
1262                 event_data = em_malloc(sizeof(email_event_t));
1263                 if (event_data == NULL) {
1264                         EM_DEBUG_EXCEPTION("em_malloc failed");
1265                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1266                         goto FINISH_OFF;
1267                 }
1268
1269                 event_data->type               = EMAIL_EVENT_UPDATE_MAIL;
1270                 event_data->account_id         = input_mail_data->account_id;
1271                 event_data->event_param_data_1 = (char*)input_mail_data;                        // need to be duplicated, it is double freed
1272                 event_data->event_param_data_2 = (char*)input_attachment_data_list;     // need to be duplicated, it is double freed
1273                 event_data->event_param_data_3 = (char*)input_meeting_request;          // need to be duplicated, it is double freed
1274                 event_data->event_param_data_4 = input_attachment_count;
1275                 event_data->event_param_data_5 = input_from_eas;
1276                 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1277
1278                 if (!emcore_insert_event(event_data, &handle, &err))  {
1279                         EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
1280                         err = EMAIL_ERROR_NONE;
1281                         goto FINISH_OFF;
1282                 }
1283         }
1284 */
1285 #endif
1286
1287 FINISH_OFF:
1288
1289         if (ref_account) {
1290                 emcore_free_account(ref_account);
1291                 EM_SAFE_FREE(ref_account);
1292         }
1293
1294         EM_DEBUG_FUNC_END("err [%d]", err);
1295         return err;
1296 }
1297
1298
1299 int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code)
1300 {
1301         EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1302
1303         int ret = false;
1304         int err = EMAIL_ERROR_NONE;
1305
1306         emstorage_mail_tbl_t* mail = NULL;
1307
1308         if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail, true, &err))  {
1309                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1310                 goto FINISH_OFF;
1311         }
1312
1313         ret = true;
1314
1315 FINISH_OFF:
1316         if (mail != NULL)
1317                 emstorage_free_mail(&mail, 1, NULL);
1318
1319         if (err_code != NULL)
1320                 *err_code = err;
1321         EM_DEBUG_FUNC_END();
1322         return ret;
1323 }
1324
1325 typedef struct {
1326         int   mail_id;
1327         char *multi_user_name;
1328 } email_retry_info;
1329
1330 INTERNAL_FUNC int emdaemon_send_mail_retry(char *multi_user_name, int mail_id, int timeout_in_sec, int* err_code)
1331 {
1332         int ret = false;
1333         int err = EMAIL_ERROR_NONE;
1334         long nTimerValue = 0;
1335         email_retry_info *retry_info = NULL;
1336
1337         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
1338                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1339                 goto FINISH_OFF;
1340         }
1341
1342         if (timeout_in_sec == 0) {
1343                 if(!emdaemon_send_mail(multi_user_name, mail_id, NULL, &err)) {
1344                         EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1345                         goto FINISH_OFF;
1346                 }
1347         }
1348         else if (timeout_in_sec > 0) {
1349                 retry_info = em_malloc(sizeof(email_retry_info));
1350                 if (retry_info == NULL) {
1351                         EM_DEBUG_EXCEPTION("em_malloc failed");
1352                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1353                         goto FINISH_OFF;
1354                 }
1355
1356                 retry_info->mail_id = mail_id;
1357                 retry_info->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1358                 nTimerValue = timeout_in_sec * 1000;
1359
1360                 if (emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)retry_info) <= 0) {
1361                         EM_DEBUG_EXCEPTION("Failed to start timer");
1362                         goto FINISH_OFF;
1363                 }
1364         }
1365
1366         ret = true;
1367
1368 FINISH_OFF:
1369         if (err_code)
1370                 *err_code = err;
1371
1372         return ret;
1373 }
1374
1375 INTERNAL_FUNC void _OnMailSendRetryTimerCB(void* data)
1376 {
1377         EM_DEBUG_FUNC_BEGIN();
1378         int err = EMAIL_ERROR_NONE;
1379         email_retry_info *retry_info = NULL;
1380
1381         if (!data) {
1382                 EM_DEBUG_LOG("Invalid param");
1383                 return;
1384         }
1385
1386         retry_info = (email_retry_info *)data;
1387
1388         if (!_emdaemon_check_mail_id(retry_info->multi_user_name, retry_info->mail_id, &err))  {
1389                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1390                 goto FINISH_OFF;
1391         }
1392
1393         if(!emdaemon_send_mail(retry_info->multi_user_name, retry_info->mail_id, NULL, &err)) {
1394                 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1395                 goto FINISH_OFF;
1396         }
1397
1398 FINISH_OFF:
1399
1400         free(retry_info->multi_user_name);
1401         free(retry_info);
1402
1403         EM_DEBUG_FUNC_END();
1404         return;
1405 }
1406
1407 INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(char *multi_user_name, int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
1408 {
1409         EM_DEBUG_FUNC_BEGIN("thread_id [%d], target_mailbox_id [%d], move_always_flag [%d], err_code [%p]", thread_id, target_mailbox_id, move_always_flag, err_code);
1410         int ret = false;
1411         int err = EMAIL_ERROR_NONE;
1412         int *mail_id_list = NULL, result_count = 0, i, mailbox_count = 0, account_id;
1413         email_mail_list_item_t *mail_list = NULL;
1414         email_mailbox_t *target_mailbox_list = NULL, *target_mailbox = NULL;
1415         char mailbox_id_param_string[10] = {0,};
1416
1417         if (target_mailbox_id == 0) {
1418                 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1419                 err = EMAIL_ERROR_INVALID_PARAM;
1420                 goto FINISH_OFF;
1421         }
1422
1423         if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1424                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1425
1426                 goto FINISH_OFF;
1427         }
1428
1429         mail_id_list = em_malloc(sizeof(int) * result_count);
1430
1431         if (mail_id_list == NULL) {
1432                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1433                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1434                 goto FINISH_OFF;
1435         }
1436
1437         for(i = 0; i < result_count; i++) {
1438                 mail_id_list[i] = mail_list[i].mail_id;
1439         }
1440         account_id = mail_list[0].account_id;
1441
1442
1443         if (!emcore_get_mailbox_list(multi_user_name, account_id, &target_mailbox_list, &mailbox_count, &err)) {
1444                 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
1445                 goto FINISH_OFF;
1446         }
1447
1448         for(i = 0; i < mailbox_count; i++) {
1449                 EM_DEBUG_LOG_SEC("%s %d", target_mailbox_list[i].mailbox_name, target_mailbox_id);
1450                 if(target_mailbox_list[i].mailbox_id == target_mailbox_id) {
1451                         target_mailbox = (target_mailbox_list + i);
1452                         break;
1453                 }
1454         }
1455
1456         if(!target_mailbox) {
1457                 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1458                 goto FINISH_OFF;
1459         }
1460
1461 /*
1462         if (!emdaemon_move_mail(multi_user_name, mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
1463                 EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
1464                 goto FINISH_OFF;
1465         }
1466 */
1467
1468         SNPRINTF(mailbox_id_param_string, 10, "%d", target_mailbox->mailbox_id);
1469         if (!emcore_notify_storage_event(NOTI_THREAD_MOVE, account_id, thread_id, mailbox_id_param_string, move_always_flag))
1470                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1471
1472         ret = true;
1473
1474 FINISH_OFF:
1475         emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1476         EM_SAFE_FREE(mail_list);
1477         EM_SAFE_FREE(mail_id_list);
1478
1479         if (err_code)
1480                 *err_code = err;
1481         EM_DEBUG_FUNC_END();
1482         return ret;
1483 }
1484
1485 INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_id, int delete_always_flag, int *handle, int *err_code)
1486 {
1487         EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1488         int ret = false;
1489         int err = EMAIL_ERROR_NONE;
1490         int account_id = 0;
1491         int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1492         email_mail_list_item_t *mail_list = NULL;
1493
1494         if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_ID_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1495                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1496                 goto FINISH_OFF;
1497         }
1498
1499         mail_id_list = em_malloc(sizeof(int) * result_count);
1500
1501         if (mail_id_list == NULL) {
1502                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1503                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1504                 goto FINISH_OFF;
1505         }
1506
1507         for(i = 0; i < result_count; i++) {
1508                 mail_id_list[i] = mail_list[i].mail_id;
1509         }
1510
1511         account_id = mail_list[0].account_id;
1512         mailbox_id = mail_list[0].mailbox_id;
1513
1514         // should remove requiring of mailbox information from this function.
1515         // email-service should find mailboxes itself by its mail id.
1516         if (!emdaemon_delete_mail(multi_user_name, mailbox_id, mail_id_list, result_count, false, handle, &err)) {
1517                 EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
1518                 goto FINISH_OFF;
1519         }
1520
1521         if (!emcore_notify_storage_event(NOTI_THREAD_DELETE, account_id, thread_id, NULL, delete_always_flag))
1522                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_THREAD_DELETE] >>>> ");
1523
1524         ret = true;
1525
1526 FINISH_OFF:
1527         EM_SAFE_FREE(mail_list);
1528         EM_SAFE_FREE(mail_id_list);
1529
1530         if (err_code)
1531                 *err_code = err;
1532         EM_DEBUG_FUNC_END();
1533         return ret;
1534 }
1535
1536 INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(char *multi_user_name, int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
1537 {
1538         EM_DEBUG_FUNC_BEGIN("thread_id [%d], seen_flag [%d], on_server [%d], handle [%p], err_code [%p]", thread_id, seen_flag, on_server, handle, err_code);
1539         int ret = false;
1540         int err = EMAIL_ERROR_NONE;
1541         int account_id, *mail_id_list = NULL, result_count = 0, i;
1542         email_mail_list_item_t *mail_list = NULL;
1543
1544         if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_ID_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1545                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1546                 goto FINISH_OFF;
1547         }
1548
1549         mail_id_list = em_malloc(sizeof(int) * result_count);
1550
1551         if (mail_id_list == NULL) {
1552                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1553                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1554                 goto FINISH_OFF;
1555         }
1556
1557         for(i = 0; i < result_count; i++) {
1558                 mail_id_list[i] = mail_list[i].mail_id;
1559         }
1560
1561         account_id = mail_list[0].account_id;
1562
1563         if (!emdaemon_set_flags_field(multi_user_name, account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
1564                 EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
1565                 goto FINISH_OFF;
1566         }
1567
1568         if (!emcore_notify_storage_event(NOTI_THREAD_MODIFY_SEEN_FLAG, account_id, thread_id, NULL, seen_flag))
1569                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1570
1571         ret = true;
1572
1573 FINISH_OFF:
1574
1575         EM_SAFE_FREE(mail_list);
1576         EM_SAFE_FREE(mail_id_list);
1577
1578         if (err_code)
1579                 *err_code = err;
1580
1581         EM_DEBUG_FUNC_END();
1582         return ret;
1583 }
1584
1585 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name, int input_mailbox_id, int input_on_server, int *output_handle)
1586 {
1587         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
1588         int err = EMAIL_ERROR_NONE;
1589         int handle = 0;
1590         int event_insert = false;
1591         email_event_t *event_data = NULL;
1592         email_account_t *ref_account = NULL;
1593         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1594
1595         if (input_mailbox_id <= 0)  {
1596                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1597                 err = EMAIL_ERROR_INVALID_PARAM;
1598                 goto FINISH_OFF;
1599         }
1600
1601         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
1602                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1603                 goto FINISH_OFF;
1604         }
1605
1606         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1607         if (!ref_account)  {
1608                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
1609                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1610                 goto FINISH_OFF;
1611         }
1612
1613 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1614         if(input_on_server) {
1615                 event_data = em_malloc(sizeof(email_event_t));
1616                 if (event_data == NULL) {
1617                         EM_DEBUG_EXCEPTION("em_malloc failed");
1618                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1619                         goto FINISH_OFF;
1620                 }
1621
1622                 event_data->type               = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
1623                 event_data->account_id         = mailbox_tbl->account_id;
1624                 event_data->event_param_data_4 = input_mailbox_id;
1625                 event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
1626
1627                 if (!emcore_insert_event(event_data, &handle, &err)) {
1628                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1629                         goto FINISH_OFF;
1630                 }
1631                 event_insert = true;
1632         }
1633         else
1634 #endif
1635                 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1636                         EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1637                         goto FINISH_OFF;
1638                 }
1639
1640 FINISH_OFF:
1641
1642         if (event_insert == false && event_data) {
1643                 EM_SAFE_FREE(event_data->multi_user_name);
1644                 emcore_free_event(event_data);
1645                 EM_SAFE_FREE(event_data);
1646         }
1647
1648         if (ref_account) {
1649                 emcore_free_account(ref_account);
1650                 EM_SAFE_FREE(ref_account);
1651         }
1652
1653         if(mailbox_tbl)
1654                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1655
1656         EM_DEBUG_FUNC_END("err [%d]", err);
1657         return err;
1658 }