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