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