Fixed the build error for gcc-14
[platform/core/messaging/email-service.git] / email-api / email-api-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  *
26  * This file contains the data structures and interfaces needed for application,
27  * to interact with email-service.
28  * @file                email-api-mail.c
29  * @brief               This file contains the data structures and interfaces of Message related Functionality provided by
30  *                      email-service .
31  */
32
33 #include <stdlib.h>
34 #include <time.h>
35 #include <string.h>
36
37 #include "email-ipc.h"
38 #include "email-api-init.h"
39 #include "email-api-mailbox.h"
40 #include "email-api-private.h"
41 #include "email-convert.h"
42 #include "email-core-tasks.h"
43 #include "email-core-utils.h"
44 #include "email-core-mail.h"
45 #include "email-core-smtp.h"
46 #include "email-core-account.h"
47 #include "email-core-task-manager.h"
48 #include "email-storage.h"
49 #include "email-core-signal.h"
50 #include "email-utilities.h"
51 #include "email-core-smime.h"
52
53 #define  DIR_SEPERATOR_CH '/'
54
55 #define _DIRECT_TO_DB
56
57 #define MAX_SEARCH_LEN 1000
58
59 EXPORT_API int email_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)
60 {
61         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
62         EM_DEBUG_API_BEGIN("input_mail_data[%p] input_attachment_data_list[%p] input_attachment_count[%d] input_meeting_request[%p] input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
63
64         EM_IF_NULL_RETURN_VALUE(input_mail_data,               EMAIL_ERROR_INVALID_PARAM);
65         EM_IF_NULL_RETURN_VALUE(input_mail_data->account_id,   EMAIL_ERROR_INVALID_PARAM);
66         EM_IF_NULL_RETURN_VALUE(input_mail_data->mailbox_id,   EMAIL_ERROR_INVALID_PARAM);
67
68         if (input_attachment_count > 0 && !input_attachment_data_list) {
69                         /* LCOV_EXCL_START */
70                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
71                 return EMAIL_ERROR_INVALID_PARAM;
72                         /* LCOV_EXCL_STOP */
73         }
74
75         int       err = EMAIL_ERROR_NONE;
76         int      *ret_nth_value = NULL;
77         int       size = 0;
78         char     *rfc822_file = NULL;
79     char     *multi_user_name = NULL;
80         char     *mail_data_stream = NULL;
81         char     *attachment_data_list_stream  = NULL;
82         char     *meeting_request_stream = NULL;
83         HIPC_API  hAPI = NULL;
84
85     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
86                         /* LCOV_EXCL_START */
87         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
88         goto FINISH_OFF;
89                         /* LCOV_EXCL_STOP */
90     }
91
92         if (input_from_eas == 0) { /* native emails */
93                 if ((input_mail_data->smime_type != EMAIL_SMIME_NONE) && (input_mail_data->mailbox_type != EMAIL_MAILBOX_TYPE_DRAFT)) {
94                         /* LCOV_EXCL_START */
95                         if (!emcore_make_rfc822_file(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, true, &rfc822_file, &err)) {
96                                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file failed [%d]", err);
97                                 goto FINISH_OFF;
98                         }
99                         input_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(emcore_set_mime_entity(rfc822_file));
100
101                         emstorage_delete_file(rfc822_file, NULL);
102                         EM_SAFE_FREE(rfc822_file);
103                         /* LCOV_EXCL_STOP */
104                 }
105
106                 hAPI = emipc_create_email_api(_EMAIL_API_ADD_MAIL);
107
108                 if (!hAPI) {
109                         /* LCOV_EXCL_START */
110                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
111                         err = EMAIL_ERROR_NULL_VALUE;
112                         goto FINISH_OFF;
113                         /* LCOV_EXCL_STOP */
114                 }
115
116                 /* email_mail_data_t */
117                 mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
118
119                 if (!mail_data_stream) {
120                         /* LCOV_EXCL_START */
121                         EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
122                         err = EMAIL_ERROR_NULL_VALUE;
123                         goto FINISH_OFF;
124                         /* LCOV_EXCL_STOP */
125                 }
126
127                 if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size))
128                         /* LCOV_EXCL_START */
129                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
130                         /* LCOV_EXCL_STOP */
131
132                 /* email_attachment_data_t */
133                 attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data_list, input_attachment_count, &size);
134
135                 if ((size > 0) && !emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
136                         /* LCOV_EXCL_START */
137                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
138                         err = EMAIL_ERROR_OUT_OF_MEMORY;
139                         goto FINISH_OFF;
140                         /* LCOV_EXCL_STOP */
141                 }
142
143                 /*  email_meeting_request_t */
144                 if (input_mail_data->meeting_request_status != EMAIL_MAIL_TYPE_NORMAL) {
145                         /* LCOV_EXCL_START */
146                         meeting_request_stream = em_convert_meeting_req_to_byte_stream(input_meeting_request, &size);
147
148                         if (!meeting_request_stream) {
149                                 EM_DEBUG_EXCEPTION("em_convert_meeting_req_to_byte_stream failed");
150                                 err = EMAIL_ERROR_NULL_VALUE;
151                                 goto FINISH_OFF;
152                         }
153                         if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, meeting_request_stream, size))
154                                 EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
155                         /* LCOV_EXCL_STOP */
156                 }
157
158                 /* input_from_eas */
159                 if (emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_eas, sizeof(int)) == false)
160                         /* LCOV_EXCL_START */
161                         EM_DEBUG_EXCEPTION("emipc_add_parameter() failed");
162                         /* LCOV_EXCL_STOP */
163
164                 /* Execute API */
165                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
166                         /* LCOV_EXCL_START */
167                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
168                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
169                         goto FINISH_OFF;
170                         /* LCOV_EXCL_STOP */
171                 }
172
173                 /* get result from service */
174                 if ((ret_nth_value = (int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 0))) {
175                         err = *ret_nth_value;
176
177                         if (err == EMAIL_ERROR_NONE) {
178                                 if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 1))) {
179                                         input_mail_data->mail_id = *ret_nth_value;
180                                 } else {
181                         /* LCOV_EXCL_START */
182                                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
183                         /* LCOV_EXCL_STOP */
184                                         goto FINISH_OFF;
185                                 }
186
187                                 if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 2))) {
188                                         input_mail_data->thread_id = *ret_nth_value;
189                                 } else {
190                                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
191                                         goto FINISH_OFF;
192                                 }
193                         }
194                 } else {
195                         /* LCOV_EXCL_START */
196                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
197                         goto FINISH_OFF;
198                         /* LCOV_EXCL_STOP */
199                 }
200         } else { /* take care of mails from eas */
201                 /* LCOV_EXCL_START */
202                 err = emcore_add_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false);
203                 if (err != EMAIL_ERROR_NONE) {
204                         EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
205                         goto FINISH_OFF;
206                         /* LCOV_EXCL_STOP */
207                 }
208         }
209                         /* LCOV_EXCL_START */
210 FINISH_OFF:
211                         /* LCOV_EXCL_STOP */
212         if (hAPI)
213                 emipc_destroy_email_api(hAPI);
214
215     EM_SAFE_FREE(multi_user_name);
216         EM_DEBUG_API_END("err[%d]", err);
217         return err;
218
219 }
220                         /* LCOV_EXCL_START */
221 EXPORT_API int email_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id)
222 {
223         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
224         EM_DEBUG_API_BEGIN("input_read_mail_id[%d] output_receipt_mail_id[%p]", input_read_mail_id, output_receipt_mail_id);
225
226         EM_IF_NULL_RETURN_VALUE(output_receipt_mail_id, EMAIL_ERROR_INVALID_PARAM);
227
228         int      err = EMAIL_ERROR_NONE;
229         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_READ_RECEIPT);
230
231         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
232
233         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_read_mail_id, sizeof(int))) {
234                 EM_DEBUG_EXCEPTION("Add Param mail body Fail");
235                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
236         }
237
238         /* Execute API */
239         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
240                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
241                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
242         }
243
244         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
245
246         EM_DEBUG_LOG("err [%d]", err);
247
248         if (err == EMAIL_ERROR_NONE) {
249                 /* Get receipt mail id */
250                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_receipt_mail_id);
251                 EM_DEBUG_LOG("output_receipt_mail_id [%d]", *output_receipt_mail_id);
252         }
253
254         emipc_destroy_email_api(hAPI);
255
256         hAPI = NULL;
257
258         EM_DEBUG_API_END("err[%d]", err);
259         return err;
260 }
261
262
263 #define TMP_BODY_PATH "/tmp/UTF-8"
264 EXPORT_API int email_create_db_full()
265 {
266         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
267         int mailbox_index, mail_index, mailbox_count, mail_slot_size;
268         int account_id = 0;
269         emstorage_mail_tbl_t mail_table_data = {0};
270         email_mailbox_t *mailbox_list = NULL;
271         int err = EMAIL_ERROR_NONE;
272         int i = 0;
273         FILE *body_file = NULL;
274
275         if ((err = email_open_db()) != EMAIL_ERROR_NONE) {
276                 EM_DEBUG_EXCEPTION("email_open_db failed [%d]", err);
277                 return err;
278         }
279
280         if ((err = emcore_load_default_account_id(NULL, &account_id)) != EMAIL_ERROR_NONE) {
281                 EM_DEBUG_EXCEPTION("emcore_load_default_account_id failed : [%d]", err);
282                 goto FINISH_OFF;
283         }
284
285         err = em_fopen(TMP_BODY_PATH, "w", &body_file);
286         if (err != EMAIL_ERROR_NONE) {
287                 EM_DEBUG_EXCEPTION("em_fopen failed : [%s][%d]", TMP_BODY_PATH, err);
288                 goto FINISH_OFF;
289         }
290
291         for (i = 0; i < 10; i++)
292                 fprintf(body_file, "Dummy test. [line:%d]\n", i);
293
294
295         fflush(body_file);
296
297         mail_table_data.subject = (char*) em_malloc(50);
298         mail_table_data.full_address_from = strdup("<dummy_from@nowhere.com>");
299         mail_table_data.full_address_to = strdup("<dummy_to@nowhere.com>");
300         mail_table_data.account_id = account_id;
301         mail_table_data.file_path_plain = strdup(TMP_BODY_PATH);
302         mail_table_data.body_download_status = 1;
303
304         if ((err = email_get_mailbox_list_ex(1, -1, 0, &mailbox_list, &mailbox_count)) < EMAIL_ERROR_NONE) {
305                 EM_DEBUG_EXCEPTION("email_get_mailbox_list_ex failed [%d]", err);
306                 goto FINISH_OFF;
307         }
308         for (mailbox_index = 0; mailbox_index < mailbox_count; mailbox_index++) {
309                 mail_slot_size = mailbox_list[mailbox_index].mail_slot_size;
310                 for (mail_index = 0; mail_index < mail_slot_size; mail_index++) {
311                         snprintf(mail_table_data.subject, 50,"Subject #%d", mail_index);
312                         mail_table_data.mailbox_id   = mailbox_list[mailbox_index].mailbox_id;
313                         mail_table_data.mailbox_type = mailbox_list[mailbox_index].mailbox_type;
314
315                         if (!emstorage_add_mail(NULL, &mail_table_data, 1, true, &err))  {
316                                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
317                                 goto FINISH_OFF;
318                         }
319                 }
320         }
321 FINISH_OFF:
322         if ((err = email_close_db()) != EMAIL_ERROR_NONE) {
323                 EM_DEBUG_EXCEPTION("email_close_db failed [%d]", err);
324         }
325
326         if (body_file) fclose(body_file); /*prevent 39446*/
327
328         if (mailbox_list)
329                 email_free_mailbox(&mailbox_list, mailbox_count);
330
331         EM_SAFE_FREE(mail_table_data.subject);
332         EM_SAFE_FREE(mail_table_data.full_address_from);
333         EM_SAFE_FREE(mail_table_data.full_address_to);
334         EM_SAFE_FREE(mail_table_data.file_path_plain);
335
336         return err;
337 }
338
339 EXPORT_API int email_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)
340 {
341         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
342         EM_DEBUG_API_BEGIN("input_mail_data[%p] input_attachment_data_list[%p] input_attachment_count[%d] input_meeting_request[%p] input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
343
344         EM_IF_NULL_RETURN_VALUE(input_mail_data,               EMAIL_ERROR_INVALID_PARAM);
345         EM_IF_NULL_RETURN_VALUE(input_mail_data->account_id,   EMAIL_ERROR_INVALID_PARAM);
346
347         if (input_attachment_count > 0 && !input_attachment_data_list) {
348                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
349                 return EMAIL_ERROR_INVALID_PARAM;
350         }
351
352         int       err = EMAIL_ERROR_NONE;
353         int       size = 0;
354         char     *mail_data_stream = NULL;
355         char     *attachment_data_list_stream  = NULL;
356         char     *meeting_request_stream = NULL;
357
358         HIPC_API  hAPI = NULL;
359
360         if (input_from_eas == 0) {
361                 hAPI = emipc_create_email_api(_EMAIL_API_UPDATE_MAIL);
362
363                 if (!hAPI) {
364                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
365                         err = EMAIL_ERROR_NULL_VALUE;
366                         goto FINISH_OFF;
367                 }
368
369                 /* email_mail_data_t */
370                 mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
371
372                 if (!mail_data_stream) {
373                         EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
374                         err = EMAIL_ERROR_NULL_VALUE;
375                         goto FINISH_OFF;
376                 }
377
378                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size)) {
379                         EM_DEBUG_EXCEPTION("emipc_add_parameter for head failed");
380                         err = EMAIL_ERROR_OUT_OF_MEMORY;
381                         goto FINISH_OFF;
382                 }
383
384                 /* email_attachment_data_t */
385                 attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data_list, input_attachment_count, &size);
386                 if ((size > 0) && !emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
387                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
388                         err = EMAIL_ERROR_OUT_OF_MEMORY;
389                         goto FINISH_OFF;
390                 }
391
392                 /*  email_meeting_request_t */
393                 if (input_mail_data->meeting_request_status != EMAIL_MAIL_TYPE_NORMAL) {
394                         meeting_request_stream = em_convert_meeting_req_to_byte_stream(input_meeting_request, &size);
395
396                         if (!meeting_request_stream) {
397                                 EM_DEBUG_EXCEPTION("em_convert_meeting_req_to_byte_stream failed");
398                                 err = EMAIL_ERROR_NULL_VALUE;
399                                 goto FINISH_OFF;
400                         }
401
402                         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, meeting_request_stream, size)) {
403                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
404                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
405                                 goto FINISH_OFF;
406                         }
407                 }
408
409                 /* input_from_eas */
410                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_eas, sizeof(int))) {
411                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
412                         err = EMAIL_ERROR_OUT_OF_MEMORY;
413                         goto FINISH_OFF;
414                 }
415
416                 /* Execute API */
417                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
418                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
419                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
420                         goto FINISH_OFF;
421                 }
422
423                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
424
425                 if (err == EMAIL_ERROR_NONE) {
426                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &input_mail_data->mail_id);
427                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 2, sizeof(int), &input_mail_data->thread_id);
428                 }
429         } else {
430                 if ((err = emcore_update_mail(NULL, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
431                         EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
432                         goto FINISH_OFF;
433                 }
434         }
435
436 FINISH_OFF:
437         if (hAPI)
438                 emipc_destroy_email_api(hAPI);
439
440         EM_SAFE_FREE(mail_data_stream);
441
442         EM_DEBUG_API_END("err[%d]", err);
443         return err;
444 }
445
446 EXPORT_API int email_update_mail_attribute(int input_account_id, int *input_mail_id_array, int input_mail_id_count, email_mail_attribute_type input_attribute_type, email_mail_attribute_value_t input_value)
447 {
448         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
449         EM_DEBUG_API_BEGIN("input_account_id[%d] input_mail_id_array[%p] input_mail_id_count[%d] input_attribute_type[%d]", input_account_id, input_mail_id_array, input_mail_id_count, input_attribute_type);
450
451         int err = EMAIL_ERROR_NONE;
452         task_parameter_EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE task_parameter;
453         email_mail_attribute_value_type value_type;
454
455         if (input_mail_id_count <= 0 || input_mail_id_array == NULL) {
456                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
457                 err = EMAIL_ERROR_INVALID_PARAM;
458                 goto FINISH_OFF;
459         }
460
461         if ((err = emcore_get_mail_attribute_value_type(input_attribute_type, &value_type)) != EMAIL_ERROR_NONE) {
462                 EM_DEBUG_EXCEPTION("emcore_get_mail_attribute_value_type failed [%d]", err);
463                 goto FINISH_OFF;
464         }
465
466         task_parameter.account_id     = input_account_id;
467         task_parameter.mail_id_count  = input_mail_id_count;
468         task_parameter.mail_id_array  = input_mail_id_array;
469         task_parameter.attribute_type = input_attribute_type;
470         task_parameter.value          = input_value;
471
472         if (value_type == EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING)
473                 task_parameter.value_length = EM_SAFE_STRLEN(input_value.string_type_value);
474         else
475                 task_parameter.value_length = 4;
476
477         if ((err = emipc_execute_proxy_task(EMAIL_SYNC_TASK_UPDATE_ATTRIBUTE, &task_parameter)) != EMAIL_ERROR_NONE) {
478                 EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
479                 goto FINISH_OFF;
480         }
481
482 FINISH_OFF:
483
484         EM_DEBUG_API_END("err[%d]", err);
485         return err;
486 }
487 EXPORT_API int email_clear_mail_data()
488 {
489         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
490         EM_DEBUG_API_BEGIN();
491         int err = EMAIL_ERROR_NONE;
492
493         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_DATA);
494
495         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
496
497         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
498                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api Fail");
499                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
500         }
501         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
502
503         if (hAPI)
504                 emipc_destroy_email_api(hAPI);
505
506         EM_DEBUG_API_END("err[%d]", err);
507         return err;
508 }
509
510
511 EXPORT_API int email_count_mail(email_list_filter_t *input_filter_list, int input_filter_count, int *output_total_mail_count, int *output_unseen_mail_count)
512 {
513         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
514         EM_DEBUG_API_BEGIN("input_filter_list[%p] input_filter_count[%d] output_total_mail_count[%p] output_unseen_mail_count[%p]", input_filter_list, input_filter_count, output_total_mail_count, output_unseen_mail_count);
515
516         int total_count = 0;
517         int unread = 0;
518         int err = EMAIL_ERROR_NONE;
519     char *multi_user_name = NULL;
520         char *conditional_clause_string = NULL;
521
522         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
523         EM_IF_NULL_RETURN_VALUE(input_filter_count, EMAIL_ERROR_INVALID_PARAM);
524         EM_IF_NULL_RETURN_VALUE(output_total_mail_count, EMAIL_ERROR_INVALID_PARAM);
525         EM_IF_NULL_RETURN_VALUE(output_unseen_mail_count, EMAIL_ERROR_INVALID_PARAM);
526
527     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
528         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
529         goto FINISH_OFF;
530     }
531
532         if ((err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, input_filter_list, input_filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
533                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
534                 goto FINISH_OFF;
535         }
536
537         if ((err = emstorage_query_mail_count(multi_user_name, conditional_clause_string, true, &total_count, &unread)) != EMAIL_ERROR_NONE) {
538                 EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed [%d]", err);
539                 goto FINISH_OFF;
540         }
541
542         *output_total_mail_count = total_count;
543         *output_unseen_mail_count = unread;
544
545 FINISH_OFF:
546
547     EM_SAFE_FREE(multi_user_name);
548         EM_SAFE_FREE(conditional_clause_string); /* detected by valgrind */
549         EM_DEBUG_API_END("err[%d]", err);
550         return err;
551 }
552
553
554 EXPORT_API int email_delete_mail(int input_mailbox_id, int *input_mail_ids, int input_num, int input_from_server)
555 {
556         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
557         EM_DEBUG_API_BEGIN("input_mailbox_id[%d] input_mail_ids[%p] input_num[%d] input_from_server[%d]", input_mailbox_id, input_mail_ids, input_num, input_from_server);
558
559         int err = EMAIL_ERROR_NONE;
560         HIPC_API hAPI = NULL;
561
562         EM_IF_NULL_RETURN_VALUE(input_mail_ids, EMAIL_ERROR_INVALID_PARAM);
563
564         if (input_mailbox_id <= 0) {
565                 EM_DEBUG_EXCEPTION("input_mailbox_id = %d", input_mailbox_id);
566                 err = EMAIL_ERROR_INVALID_PARAM;
567                 return err;
568         }
569
570         if (input_num <= 0) {
571                 EM_DEBUG_EXCEPTION("input_num = %d", input_num);
572                 err = EMAIL_ERROR_INVALID_PARAM;
573                 return err;
574         }
575
576         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAIL);
577
578         if (!hAPI) {
579                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
580                 err = EMAIL_ERROR_NULL_VALUE;
581                 goto FINISH_OFF;
582         }
583
584         /* mailbox id*/
585         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
586                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
587                 err = EMAIL_ERROR_OUT_OF_MEMORY;
588                 goto FINISH_OFF;
589         }
590
591         /* Number of mail_ids */
592         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_num, sizeof(int))) {
593                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
594                 err = EMAIL_ERROR_OUT_OF_MEMORY;
595                 goto FINISH_OFF;
596         }
597
598         /* set of mail_ids */
599         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)input_mail_ids, input_num * sizeof(int))) {
600                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
601                 err = EMAIL_ERROR_OUT_OF_MEMORY;
602                 goto FINISH_OFF;
603         }
604
605         /* from-server */
606         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_server, sizeof(int))) {
607                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
608                 err = EMAIL_ERROR_OUT_OF_MEMORY;
609                 goto FINISH_OFF;
610         }
611
612         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
613                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
614                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
615                 goto FINISH_OFF;
616         }
617
618         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
619
620 FINISH_OFF:
621         if (hAPI)
622                 emipc_destroy_email_api(hAPI);
623
624         EM_DEBUG_API_END("err[%d]", err);
625         return err;
626 }
627
628
629 EXPORT_API int email_delete_all_mails_in_mailbox(int input_mailbox_id, int input_from_server)
630 {
631         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
632         EM_DEBUG_API_BEGIN("input_mailbox_id[%d] input_from_server[%d]", input_mailbox_id, input_from_server);
633
634         int err = EMAIL_ERROR_NONE;
635         HIPC_API hAPI = NULL;
636
637         if (input_mailbox_id <= 0) {
638                 EM_DEBUG_EXCEPTION("input_mailbox_id = %d", input_mailbox_id);
639                 err = EMAIL_ERROR_INVALID_PARAM;
640                 return err;
641         }
642
643         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_ALL_MAIL);
644
645         if (!hAPI) {
646                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
647                 err = EMAIL_ERROR_NULL_VALUE;
648                 goto FINISH_OFF;
649         }
650
651         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
652                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
653                 err = EMAIL_ERROR_OUT_OF_MEMORY;
654                 goto FINISH_OFF;
655         }
656
657         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_server, sizeof(int))) {
658                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
659                 err = EMAIL_ERROR_OUT_OF_MEMORY;
660                 goto FINISH_OFF;
661         }
662
663         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
664                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
665                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
666                 goto FINISH_OFF;
667         }
668
669         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
670
671 FINISH_OFF:
672
673         if (hAPI)
674                 emipc_destroy_email_api(hAPI);
675
676         EM_DEBUG_API_END("err[%d]", err);
677         return err;
678 }
679
680 EXPORT_API int email_add_attachment(int mail_id, email_attachment_data_t* attachment)
681 {
682         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
683         EM_DEBUG_API_BEGIN("mail_id[%d] attachment[%p]", mail_id, attachment);
684         int err = EMAIL_ERROR_NONE;
685         char *attchment_stream = NULL;
686         int size = 0;
687         HIPC_API hAPI = NULL;
688
689         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
690         EM_IF_NULL_RETURN_VALUE(attachment, EMAIL_ERROR_INVALID_PARAM);
691
692         hAPI = emipc_create_email_api(_EMAIL_API_ADD_ATTACHMENT);
693
694         if (!hAPI) {
695                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
696                 err = EMAIL_ERROR_NULL_VALUE;
697                 goto FINISH_OFF;
698         }
699
700         /* mail_id */
701         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
702                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
703                 err = EMAIL_ERROR_OUT_OF_MEMORY;
704                 goto FINISH_OFF;
705         }
706
707         /* Attachment */
708         attchment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
709
710         if (!attchment_stream) {
711                 EM_DEBUG_EXCEPTION("em_convert_attachment_data_to_byte_stream failed");
712                 err = EMAIL_ERROR_NULL_VALUE;
713                 goto FINISH_OFF;
714         }
715
716         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, attchment_stream, size)) {
717                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
718                 err = EMAIL_ERROR_OUT_OF_MEMORY;
719                 goto FINISH_OFF;
720         }
721
722         /* Execute API */
723         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
724                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
725                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
726                 goto FINISH_OFF;
727         }
728
729         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
730
731         if (EMAIL_ERROR_NONE == err) {
732                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &attachment->attachment_id);
733         }
734
735 FINISH_OFF:
736         EM_SAFE_FREE(attchment_stream);
737
738         if (hAPI)
739                 emipc_destroy_email_api(hAPI);
740
741         EM_DEBUG_API_END("err[%d]", err);
742         return err;
743  }
744
745
746 EXPORT_API int email_delete_attachment(int attachment_id)
747 {
748         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
749         EM_DEBUG_API_BEGIN("attachment_id[%d]", attachment_id);
750         int err = EMAIL_ERROR_NONE;
751         HIPC_API hAPI = NULL;
752
753         EM_IF_NULL_RETURN_VALUE(attachment_id, EMAIL_ERROR_INVALID_PARAM);
754
755         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_ATTACHMENT);
756
757         if (!hAPI) {
758                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
759                 err = EMAIL_ERROR_NULL_VALUE;
760                 goto FINISH_OFF;
761         }
762
763         /* attachment_index */
764         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&attachment_id, sizeof(int))) {
765                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
766                 err = EMAIL_ERROR_OUT_OF_MEMORY;
767                 goto FINISH_OFF;
768         }
769
770         /* Execute API */
771         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
772                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
773                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
774                 goto FINISH_OFF;
775         }
776
777         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
778
779 FINISH_OFF:
780         if (hAPI)
781                 emipc_destroy_email_api(hAPI);
782
783         EM_DEBUG_API_END("err[%d]", err);
784         return err;
785 }
786
787 /* -----------------------------------------------------------
788                                               Mail Search API
789     -----------------------------------------------------------*/
790
791 EXPORT_API int email_query_mails(char *conditional_clause_string, email_mail_data_t** mail_list,  int *result_count)
792 {
793         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
794         EM_DEBUG_API_BEGIN("conditional_clause_string[%s] mail_list[%p] result_count[%p]", conditional_clause_string, mail_list, result_count);
795
796         int err = EMAIL_ERROR_NONE;
797     char *multi_user_name = NULL;
798         emstorage_mail_tbl_t *result_mail_tbl = NULL;
799
800         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
801         EM_IF_NULL_RETURN_VALUE(conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
802
803     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
804         EM_DEBUG_EXCEPTION("emipc_get_user_name failed :[%d]", err);
805         goto FINISH_OFF;
806     }
807
808         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, true, &result_mail_tbl, result_count, &err)) {
809                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
810                 goto FINISH_OFF;
811         }
812
813         if (!em_convert_mail_tbl_to_mail_data(result_mail_tbl, *result_count, mail_list, &err)) {
814                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
815                 goto FINISH_OFF;
816         }
817
818 FINISH_OFF:
819
820         if (result_mail_tbl && !emstorage_free_mail(&result_mail_tbl, *result_count, NULL))
821                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
822
823     EM_SAFE_FREE(multi_user_name);
824
825         EM_DEBUG_API_END("err[%d]", err);
826         return err;
827 }
828
829 EXPORT_API int email_query_mail_list(char *input_conditional_clause_string, email_mail_list_item_t** output_mail_list,  int *output_result_count)
830 {
831         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
832         EM_DEBUG_API_BEGIN("input_conditional_clause_string[%s] output_mail_list[%p] output_result_count[%p]", input_conditional_clause_string, output_mail_list, output_result_count);
833
834         int err = EMAIL_ERROR_NONE;
835     char *multi_user_name = NULL;
836
837         EM_IF_NULL_RETURN_VALUE(input_conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
838         EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
839
840     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
841         EM_DEBUG_EXCEPTION("emipc_get_user_name failed :[%d]", err);
842         goto FINISH_OFF;
843     }
844
845         if (!emstorage_query_mail_list(multi_user_name, input_conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
846                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
847                 goto FINISH_OFF;
848         }
849
850 FINISH_OFF:
851
852     EM_SAFE_FREE(multi_user_name);
853         EM_DEBUG_API_END("err [%d]", err);
854         return err;
855 }
856
857
858 /* -----------------------------------------------------------
859                                               Mail Get Info API
860     -----------------------------------------------------------*/
861 EXPORT_API int email_get_attachment_data(int attachment_id, email_attachment_data_t** attachment)
862 {
863         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
864         EM_DEBUG_API_BEGIN("attachment_id[%d] attachment[%p]", attachment_id, attachment);
865
866         int err = EMAIL_ERROR_NONE;
867         int nSize = 0;
868         int attachment_count = 0;
869         char* attchment_stream = NULL;
870         email_attachment_data_t* attachment_data = NULL;
871
872         EM_IF_NULL_RETURN_VALUE(attachment_id, EMAIL_ERROR_INVALID_PARAM);
873         EM_IF_NULL_RETURN_VALUE(attachment, EMAIL_ERROR_INVALID_PARAM);
874
875
876         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_ATTACHMENT);
877
878         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
879
880         /* attachment_id */
881         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&attachment_id, sizeof(int))) {
882                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
883                 err = EMAIL_ERROR_OUT_OF_MEMORY;
884                 goto FINISH_OFF;
885         }
886
887         /* Execute API */
888         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
889                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
890                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
891                 goto FINISH_OFF;
892         }
893
894         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
895
896         if (EMAIL_ERROR_NONE == err) {
897                 nSize = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 1);
898                 if (nSize > 0) {
899                         attchment_stream = (char*)em_malloc(nSize+1);
900
901                         if (!attchment_stream) {
902                                 EM_DEBUG_EXCEPTION("em_malloc failed");
903                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
904                                 goto FINISH_OFF;
905                         }
906
907                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, nSize, attchment_stream);
908                         em_convert_byte_stream_to_attachment_data(attchment_stream, nSize, &attachment_data, &attachment_count);
909                 }
910
911                 if (!attachment_data) {
912                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NULL_VALUE");
913                         err = EMAIL_ERROR_NULL_VALUE;
914                         goto FINISH_OFF;
915                 }
916
917                 *attachment = attachment_data;
918         }
919
920 FINISH_OFF:
921         EM_SAFE_FREE(attchment_stream);
922
923         if (hAPI)
924                 emipc_destroy_email_api(hAPI);
925
926         EM_DEBUG_API_END("err[%d]", err);
927         return err;
928
929 }
930
931 EXPORT_API int email_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
932 {
933         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
934         EM_DEBUG_API_BEGIN("input_mail_id[%d] output_attachment_data[%p] output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
935         int   err             = EMAIL_ERROR_NONE;
936     char *multi_user_name = NULL;
937
938     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
939         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
940         return err;
941     }
942
943         if ((err = emcore_get_attachment_data_list(multi_user_name, input_mail_id, output_attachment_data, output_attachment_count)) != EMAIL_ERROR_NONE) {
944                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
945         }
946
947     EM_SAFE_FREE(multi_user_name);
948         EM_DEBUG_API_END("err[%d]", err);
949         return err;
950 }
951
952 EXPORT_API int email_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count)
953 {
954         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
955         EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p] attachment_data_count[%d]", attachment_data_list, attachment_data_count);
956
957         int err = EMAIL_ERROR_NONE;
958
959         emcore_free_attachment_data(attachment_data_list, attachment_data_count, &err);
960
961         EM_DEBUG_FUNC_END("err[%d]", err);
962         return err;
963 }
964
965 EXPORT_API int email_get_mail_list_ex(email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, email_mail_list_item_t** output_mail_list, int *output_result_count)
966 {
967         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
968         EM_DEBUG_FUNC_BEGIN("input_filter_list[%p] input_filter_count[%d] input_sorting_rule_list[%p] input_sorting_rule_count[%d] input_start_index[%d] input_limit_count[%d] output_mail_list[%p] output_result_count[%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_mail_list, output_result_count);
969
970         int err = EMAIL_ERROR_NONE;
971     char *multi_user_name = NULL;
972         char *conditional_clause_string = NULL;
973
974         EM_IF_NULL_RETURN_VALUE(output_mail_list, EMAIL_ERROR_INVALID_PARAM);
975         EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
976
977     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
978         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
979         goto FINISH_OFF;
980     }
981
982         if ((err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
983                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
984                 goto FINISH_OFF;
985         }
986
987         EM_DEBUG_LOG_DEV("conditional_clause_string[%s].", conditional_clause_string);
988
989         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
990                 if (err != EMAIL_ERROR_MAIL_NOT_FOUND) /* there is no mail and it is definitely common */
991                         EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
992                 goto FINISH_OFF;
993         }
994
995 FINISH_OFF:
996
997         EM_SAFE_FREE(conditional_clause_string);
998     EM_SAFE_FREE(multi_user_name);
999
1000         EM_DEBUG_FUNC_END("err[%d]", err);
1001         return err;
1002 }
1003
1004 EXPORT_API int email_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
1005 {
1006         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1007         EM_DEBUG_FUNC_BEGIN("input_filter_list[%p] input_filter_count[%d]", input_filter_list, input_filter_count);
1008         int err = EMAIL_ERROR_NONE;
1009
1010         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
1011
1012         err = emstorage_free_list_filter(input_filter_list, input_filter_count);
1013
1014         EM_DEBUG_FUNC_END("err[%d]", err);
1015         return err;
1016 }
1017
1018 EXPORT_API int email_get_mails(int account_id , int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_data_t** mail_list,  int* result_count)
1019 {
1020         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1021         EM_DEBUG_API_BEGIN("account_id[%d] mailbox_id[%d] thread_id[%d]", account_id, mailbox_id, thread_id);
1022
1023         int err = EMAIL_ERROR_NONE;
1024     char *multi_user_name = NULL;
1025         emstorage_mail_tbl_t *mail_tbl_list = NULL;
1026         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
1027
1028         if (account_id < ALL_ACCOUNT) {
1029                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1030                 err = EMAIL_ERROR_INVALID_PARAM ;
1031                 goto FINISH_OFF;
1032         }
1033
1034     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1035         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1036         goto FINISH_OFF;
1037     }
1038
1039         if (!emstorage_get_mails(multi_user_name, account_id, mailbox_id, NULL, thread_id, start_index, limit_count, sorting, true, &mail_tbl_list, result_count, &err))  {
1040                 EM_DEBUG_EXCEPTION("emstorage_get_mails failed [%d]", err);
1041                 goto FINISH_OFF;
1042         }
1043
1044         if (!em_convert_mail_tbl_to_mail_data(mail_tbl_list, *result_count, mail_list, &err)) {
1045                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
1046                 goto FINISH_OFF;
1047         }
1048
1049 FINISH_OFF:
1050
1051         if (mail_tbl_list && !emstorage_free_mail(&mail_tbl_list, *result_count, NULL))
1052                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
1053
1054     EM_SAFE_FREE(multi_user_name);
1055
1056         EM_DEBUG_API_END("err[%d]", err);
1057         return err;
1058 }
1059
1060 EXPORT_API int email_get_mail_list(int account_id , int mailbox_id, int thread_id, int start_index, int limit_count, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count)
1061 {
1062         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1063         EM_DEBUG_API_BEGIN("account_id[%d] mailbox_id[%d] thread_id[%d]", account_id, mailbox_id, thread_id);
1064
1065         int err = EMAIL_ERROR_NONE;
1066     char *multi_user_name = NULL;
1067
1068         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
1069
1070         if (account_id < ALL_ACCOUNT) {
1071                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1072                 return EMAIL_ERROR_INVALID_PARAM;
1073         }
1074
1075     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1076         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1077         goto FINISH_OFF;
1078     }
1079
1080         if (!emstorage_get_mail_list(multi_user_name, account_id, mailbox_id, NULL, thread_id, start_index, limit_count, 0, NULL, sorting, true, mail_list, result_count, &err))  {
1081                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1082                 goto FINISH_OFF;
1083         }
1084
1085 FINISH_OFF:
1086
1087     EM_SAFE_FREE(multi_user_name);
1088
1089         EM_DEBUG_API_END("err[%d]", err);
1090         return err;
1091 }
1092
1093 EXPORT_API int email_get_mail_by_address(int account_id , int mailbox_id, email_email_address_list_t* addr_list,
1094                                                                         int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, email_mail_list_item_t** mail_list,  int* result_count)
1095 {
1096         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1097         EM_DEBUG_API_BEGIN("account_id[%d] mailbox_id[%d]", account_id, mailbox_id);
1098         int err = EMAIL_ERROR_NONE;
1099     char *multi_user_name = NULL;
1100         email_mail_list_item_t* mail_list_item = NULL;
1101
1102         EM_IF_NULL_RETURN_VALUE(mail_list, EMAIL_ERROR_INVALID_PARAM);
1103         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
1104
1105         if (account_id < ALL_ACCOUNT) {
1106                 EM_DEBUG_EXCEPTION("Invalid account id param");
1107                 err = EMAIL_ERROR_INVALID_PARAM ;
1108                 return err;
1109         }
1110
1111     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1112         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1113         goto FINISH_OFF;
1114     }
1115
1116         if (!emstorage_get_mail_list(multi_user_name, account_id, mailbox_id, addr_list, EMAIL_LIST_TYPE_NORMAL, start_index, limit_count, search_type, search_value, sorting, true, &mail_list_item, result_count, &err)) {
1117                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1118                 goto FINISH_OFF;
1119         }
1120
1121         *mail_list = mail_list_item;
1122
1123 FINISH_OFF:
1124
1125     EM_SAFE_FREE(multi_user_name);
1126
1127         EM_DEBUG_API_END("err[%d]", err);
1128         return err;
1129 }
1130
1131 EXPORT_API int email_get_thread_information_by_thread_id(int thread_id, email_mail_data_t** thread_info)
1132 {
1133         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1134         EM_DEBUG_API_BEGIN("thread_id[%d]", thread_id);
1135         int err = EMAIL_ERROR_NONE;
1136     char *multi_user_name = NULL;
1137         emstorage_mail_tbl_t *mail_table_data = NULL;
1138
1139         EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
1140
1141     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1142         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1143         goto FINISH_OFF;
1144     }
1145
1146         if (!emstorage_get_thread_information(multi_user_name, thread_id, &mail_table_data , true, &err)) {
1147                 EM_DEBUG_EXCEPTION("emstorage_get_thread_information  failed [%d]", err);
1148                 goto FINISH_OFF;
1149         }
1150
1151         if (!em_convert_mail_tbl_to_mail_data(mail_table_data, 1, thread_info, &err)) {
1152                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
1153                 goto FINISH_OFF;
1154         }
1155
1156 FINISH_OFF:
1157
1158         if (mail_table_data && !emstorage_free_mail(&mail_table_data, 1, NULL))
1159                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed");
1160
1161     EM_SAFE_FREE(multi_user_name);
1162
1163         EM_DEBUG_API_END("err[%d]", err);
1164         return err;
1165 }
1166
1167 EXPORT_API int email_get_thread_information_ex(int thread_id, email_mail_list_item_t** thread_info)
1168 {
1169         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1170         EM_DEBUG_API_BEGIN("thread_id[%d]", thread_id);
1171         int err = EMAIL_ERROR_NONE;
1172     char *multi_user_name = NULL;
1173         emstorage_mail_tbl_t *mail_table_data = NULL;
1174         email_mail_list_item_t *temp_thread_info = NULL;
1175
1176         EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
1177
1178     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1179         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1180         goto FINISH_OFF;
1181     }
1182
1183         if (!emstorage_get_thread_information(multi_user_name, thread_id, &mail_table_data , true, &err)) {
1184                 EM_DEBUG_EXCEPTION("emstorage_get_thread_information -- failed [%d]", err);
1185                 goto FINISH_OFF;
1186         }
1187
1188         temp_thread_info = em_malloc(sizeof(email_mail_list_item_t));
1189
1190         if (!temp_thread_info) {
1191                 EM_DEBUG_EXCEPTION("em_malloc failed");
1192                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1193                 goto FINISH_OFF;
1194         }
1195
1196         EM_SAFE_STRNCPY(temp_thread_info->full_address_from       , mail_table_data->full_address_from, STRING_LENGTH_FOR_DISPLAY);
1197         EM_SAFE_STRNCPY(temp_thread_info->email_address_sender    , mail_table_data->email_address_sender, MAX_EMAIL_ADDRESS_LENGTH);
1198         EM_SAFE_STRNCPY(temp_thread_info->email_address_recipient , mail_table_data->email_address_recipient, STRING_LENGTH_FOR_DISPLAY);
1199         EM_SAFE_STRNCPY(temp_thread_info->subject                 , mail_table_data->subject, STRING_LENGTH_FOR_DISPLAY);
1200         EM_SAFE_STRNCPY(temp_thread_info->preview_text            , mail_table_data->preview_text, MAX_PREVIEW_TEXT_LENGTH);
1201         temp_thread_info->mail_id                                 = mail_table_data->mail_id;
1202         temp_thread_info->mailbox_id                              = mail_table_data->mailbox_id;
1203         temp_thread_info->mailbox_type                            = mail_table_data->mailbox_type;
1204         temp_thread_info->account_id                              = mail_table_data->account_id;
1205         temp_thread_info->date_time                               = mail_table_data->date_time;
1206         temp_thread_info->body_download_status                    = mail_table_data->body_download_status;
1207         temp_thread_info->flags_seen_field                        = mail_table_data->flags_seen_field;
1208         temp_thread_info->priority                                = mail_table_data->priority;
1209         temp_thread_info->save_status                             = mail_table_data->save_status;
1210         temp_thread_info->lock_status                             = mail_table_data->lock_status;
1211         temp_thread_info->report_status                           = mail_table_data->report_status;
1212         temp_thread_info->attachment_count                        = mail_table_data->attachment_count;
1213         temp_thread_info->DRM_status                              = mail_table_data->DRM_status;
1214         temp_thread_info->thread_id                               = mail_table_data->thread_id;
1215         temp_thread_info->thread_item_count                       = mail_table_data->thread_item_count;
1216         temp_thread_info->meeting_request_status                  = mail_table_data->meeting_request_status;
1217
1218         *thread_info = temp_thread_info;
1219
1220 FINISH_OFF:
1221
1222         if (mail_table_data)
1223                 emstorage_free_mail(&mail_table_data, 1, NULL);
1224
1225     EM_SAFE_FREE(multi_user_name);
1226
1227         EM_DEBUG_API_END("err[%d]", err);
1228         return err;
1229 }
1230
1231 EXPORT_API int email_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
1232 {
1233         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1234         EM_DEBUG_API_BEGIN("input_mail_id[%d]", input_mail_id);
1235         int err = EMAIL_ERROR_NONE;
1236     char *multi_user_name = NULL;
1237
1238     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1239         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1240         return err;
1241     }
1242
1243         if (((err = emcore_get_mail_data(multi_user_name, input_mail_id, output_mail_data)) != EMAIL_ERROR_NONE) || !output_mail_data)
1244                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
1245
1246     EM_SAFE_FREE(multi_user_name);
1247
1248         EM_DEBUG_API_END("err[%d]", err);
1249         return err;
1250 }
1251
1252
1253 /* -----------------------------------------------------------
1254                                               Mail Flag API
1255     -----------------------------------------------------------*/
1256
1257 EXPORT_API int email_modify_seen_flag(int *mail_ids, int num, int seen_flag, int onserver)
1258 {
1259         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1260         EM_DEBUG_API_BEGIN("mail_ids[%p] num[%d] seen_flag[%d] on_server[%d]", mail_ids, num, seen_flag, onserver);
1261         EM_DEBUG_API_END("EMAIL_ERROR_NOT_IMPLEMENTED");
1262         return EMAIL_ERROR_NOT_IMPLEMENTED;
1263 }
1264
1265 EXPORT_API int email_set_flags_field(int account_id, int *mail_ids, int num, email_flags_field_type field_type, int value, int onserver)
1266 {
1267         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1268         EM_DEBUG_API_BEGIN("account_id[%d] mail_ids[%p] num[%d] field_type[%d] seen_flag[%d] on_server[%d]", account_id, mail_ids, num, field_type, value, onserver);
1269
1270         int err = EMAIL_ERROR_NONE;
1271
1272
1273         EM_IF_NULL_RETURN_VALUE(mail_ids, EMAIL_ERROR_INVALID_PARAM);
1274         if (account_id == 0 || num <= 0 || (onserver != 0 && onserver != 1)) {
1275                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1276                 return EMAIL_ERROR_INVALID_PARAM;
1277         }
1278
1279         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_FLAGS_FIELD);
1280
1281         if (!hAPI) {
1282                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1283                 err = EMAIL_ERROR_NULL_VALUE;
1284                 goto FINISH_OFF;
1285         }
1286
1287         /* account_id*/
1288         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
1289                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1290                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1291                 goto FINISH_OFF;
1292         }
1293
1294         /* Number of mail_ids */
1295         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&num, sizeof(int))) {
1296                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1297                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1298                 goto FINISH_OFF;
1299         }
1300
1301         /* set of mail_ids */
1302         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)mail_ids, num * sizeof(int))) {
1303                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1304                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1305                 goto FINISH_OFF;
1306         }
1307
1308         /* field_type */
1309         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&field_type, sizeof(int))) {
1310                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1311                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1312                 goto FINISH_OFF;
1313         }
1314
1315         /* value */
1316         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&value, sizeof(int))) {
1317                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1318                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1319                 goto FINISH_OFF;
1320         }
1321
1322         /* onserver  */
1323         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(onserver), sizeof(int))) {
1324                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1325                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1326                 goto FINISH_OFF;
1327         }
1328
1329         /* Execute API */
1330         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1331                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1332                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1333                 goto FINISH_OFF;
1334         }
1335
1336         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1337
1338 FINISH_OFF:
1339         if (hAPI)
1340                 emipc_destroy_email_api(hAPI);
1341
1342         EM_DEBUG_API_END("err[%d]", err);
1343         return err;
1344 }
1345
1346
1347 /* -----------------------------------------------------------
1348                                               Mail Move API
1349     -----------------------------------------------------------*/
1350 EXPORT_API int email_move_mail_to_mailbox(int *mail_ids, int num, int input_target_mailbox_id)
1351 {
1352         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1353         EM_DEBUG_API_BEGIN("mail_ids[%p] num[%d] input_target_mailbox_id[%d]",  mail_ids, num, input_target_mailbox_id);
1354
1355         int err = EMAIL_ERROR_NONE;
1356         HIPC_API hAPI = NULL;
1357
1358         if (input_target_mailbox_id <= 0) {
1359                 EM_DEBUG_EXCEPTION("input_target_mailbox_id is invalid parameter");
1360                 err = EMAIL_ERROR_INVALID_PARAM;
1361                 return err;
1362         }
1363         EM_IF_NULL_RETURN_VALUE(mail_ids, EMAIL_ERROR_INVALID_PARAM);
1364
1365         if (num <= 0)  {
1366                 EM_DEBUG_LOG("num = %d", num);
1367                 err = EMAIL_ERROR_INVALID_PARAM;
1368                 return err;
1369         }
1370
1371         hAPI = emipc_create_email_api(_EMAIL_API_MOVE_MAIL);
1372
1373         if (!hAPI) {
1374                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1375                 err = EMAIL_ERROR_NULL_VALUE;
1376                 goto FINISH_OFF;
1377         }
1378
1379         /* Number of mail_ids */
1380         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&num, sizeof(int))) {
1381                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1382                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1383                 goto FINISH_OFF;
1384         }
1385
1386         /* set of mail_ids */
1387         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)mail_ids, num * sizeof(int))) {
1388                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1389                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1390                 goto FINISH_OFF;
1391         }
1392
1393         /* input_target_mailbox_id */
1394         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_target_mailbox_id, sizeof(int))) {
1395                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1396                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1397                 goto FINISH_OFF;
1398         }
1399
1400         /* Execute API */
1401         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1402                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1403                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1404                 goto FINISH_OFF;
1405         }
1406
1407         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1408
1409 FINISH_OFF:
1410         if (hAPI)
1411                 emipc_destroy_email_api(hAPI);
1412
1413         EM_DEBUG_API_END("err[%d]", err);
1414         return err;
1415 }
1416
1417
1418 EXPORT_API int  email_move_all_mails_to_mailbox(int input_source_mailbox_id, int input_target_mailbox_id)
1419 {
1420         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1421         EM_DEBUG_API_BEGIN("input_source_mailbox_id[%d] input_target_mailbox_id[%d]", input_source_mailbox_id, input_target_mailbox_id);
1422
1423         int err = EMAIL_ERROR_NONE;
1424         HIPC_API hAPI = NULL;
1425
1426         if (input_source_mailbox_id <= 0 || input_target_mailbox_id <= 0) {
1427                 EM_DEBUG_EXCEPTION("mailbox_id is invalid parameter");
1428                 err = EMAIL_ERROR_INVALID_PARAM;
1429                 return err;
1430         }
1431
1432         hAPI = emipc_create_email_api(_EMAIL_API_MOVE_ALL_MAIL);
1433
1434         if (!hAPI) {
1435                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1436                 err = EMAIL_ERROR_NULL_VALUE;
1437                 goto FINISH_OFF;
1438         }
1439
1440         /* input_source_mailbox_id */
1441         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_source_mailbox_id, sizeof(int))) {
1442                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1443                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1444                 goto FINISH_OFF;
1445         }
1446
1447         /* input_target_mailbox_id */
1448         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_target_mailbox_id, sizeof(int))) {
1449                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1450                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1451                 goto FINISH_OFF;
1452         }
1453
1454         /* Execute API */
1455         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1456                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1457                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1458                 goto FINISH_OFF;
1459         }
1460
1461         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1462
1463 FINISH_OFF:
1464         if (hAPI)
1465                 emipc_destroy_email_api(hAPI);
1466
1467         EM_DEBUG_API_END("err[%d]", err);
1468         return err;
1469 }
1470
1471 EXPORT_API int email_move_mails_to_mailbox_of_another_account(int input_source_mailbox_id, int *input_mail_id_array, int input_mail_id_count, int input_target_mailbox_id, int *output_task_id)
1472 {
1473         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1474         EM_DEBUG_API_BEGIN("input_source_mailbox_id[%d] input_mail_id_array[%p] input_mail_id_count[%d] input_target_mailbox_id[%d] output_task_id[%p]",  input_source_mailbox_id, input_mail_id_array, input_mail_id_count, input_target_mailbox_id, output_task_id);
1475
1476         int err = EMAIL_ERROR_NONE;
1477         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT task_parameter;
1478
1479         if (input_source_mailbox_id <= 0 || input_target_mailbox_id <= 0 || input_mail_id_array == NULL || input_mail_id_count == 0) {
1480                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1481                 err = EMAIL_ERROR_INVALID_PARAM;
1482                 return err;
1483         }
1484
1485         task_parameter.source_mailbox_id = input_source_mailbox_id;
1486         task_parameter.mail_id_array     = input_mail_id_array;
1487         task_parameter.mail_id_count     = input_mail_id_count;
1488         task_parameter.target_mailbox_id = input_target_mailbox_id;
1489
1490         if ((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, &task_parameter)) != EMAIL_ERROR_NONE) {
1491                 EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
1492                 goto FINISH_OFF;
1493         }
1494
1495 FINISH_OFF:
1496
1497         EM_DEBUG_API_END("err[%d]", err);
1498         return err;
1499 }
1500                 /* LCOV_EXCL_STOP */
1501 EXPORT_API int email_free_mail_data(email_mail_data_t** mail_list, int count)
1502 {
1503         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1504         EM_DEBUG_FUNC_BEGIN("mail_list[%p] count[%d]", mail_list, count);
1505         int err = EMAIL_ERROR_NONE;
1506
1507         EM_IF_NULL_RETURN_VALUE(mail_list, EMAIL_ERROR_INVALID_PARAM);
1508
1509         emcore_free_mail_data_list(mail_list, count);
1510         EM_DEBUG_FUNC_END("err[%d]", err);
1511         return err;
1512 }
1513
1514 /* Convert Modified UTF-7 mailbox name to UTF-8 */
1515 /* returns modified UTF-8 Name if success else NULL */
1516                 /* LCOV_EXCL_START */
1517 EXPORT_API int email_cancel_sending_mail(int mail_id)
1518 {
1519         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1520         EM_DEBUG_API_BEGIN("mail_id[%d]", mail_id);
1521         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
1522
1523         int err = EMAIL_ERROR_NONE;
1524         int account_id = 0;
1525     char *multi_user_name = NULL;
1526         email_mail_data_t* mail_data = NULL;
1527         email_account_server_t account_server_type;
1528         HIPC_API hAPI = NULL;
1529
1530     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1531         EM_DEBUG_EXCEPTION("emipc_get_container_name error : [%d]", err);
1532         goto FINISH_OFF;
1533     }
1534
1535         if (((err = emcore_get_mail_data(multi_user_name, mail_id, &mail_data)) != EMAIL_ERROR_NONE) || mail_data == NULL)  {
1536                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
1537                 goto FINISH_OFF;
1538         }
1539
1540         account_id = mail_data->account_id;
1541
1542         /*  check account bind type and branch off  */
1543         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
1544                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1545                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1546                 goto FINISH_OFF;
1547         }
1548
1549         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1550                 int as_handle;
1551                 ASNotiData as_noti_data;
1552                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
1553                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
1554                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1555                         goto FINISH_OFF;
1556                 }
1557
1558                 /*  noti to active sync */
1559                 as_noti_data.cancel_sending_mail.handle          = as_handle;
1560                 as_noti_data.cancel_sending_mail.mail_id         = mail_id;
1561                 as_noti_data.cancel_sending_mail.account_id      = account_id;
1562                 as_noti_data.cancel_sending_mail.multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1563
1564                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL, &as_noti_data) == false) {
1565                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
1566                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1567                         goto FINISH_OFF;
1568                 }
1569         } else {
1570                 hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL_CANCEL_JOB);
1571
1572                 if (!hAPI) {
1573                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1574                         err = EMAIL_ERROR_NULL_VALUE;
1575                         goto FINISH_OFF;
1576                 }
1577
1578                 /* Account_id */
1579                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
1580                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1581                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1582                         goto FINISH_OFF;
1583                 }
1584
1585                 /* Mail ID */
1586                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1587                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1588                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1589                         goto FINISH_OFF;
1590                 }
1591
1592                 /* Execute API */
1593                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1594                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1595                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1596                         goto FINISH_OFF;
1597                 }
1598
1599                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1600         }
1601
1602 FINISH_OFF:
1603         if (hAPI)
1604                 emipc_destroy_email_api(hAPI);
1605
1606         if (mail_data)
1607                 emcore_free_mail_data_list(&mail_data, 1);
1608
1609         EM_SAFE_FREE(multi_user_name);
1610
1611         EM_DEBUG_API_END("err[%d]", err);
1612         return err;
1613
1614 }
1615
1616 EXPORT_API int email_retry_sending_mail(int mail_id, int timeout_in_sec)
1617 {
1618         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1619         EM_DEBUG_API_BEGIN("mail_id[%d]", mail_id);
1620
1621         int err = EMAIL_ERROR_NONE;
1622
1623         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
1624         if (timeout_in_sec < 0)  {
1625                 EM_DEBUG_EXCEPTION("Invalid timeout_in_sec");
1626                 err = EMAIL_ERROR_INVALID_PARAM;
1627                 return err;
1628         }
1629
1630         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_RETRY);
1631
1632         if (!hAPI) {
1633                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1634                 err = EMAIL_ERROR_NULL_VALUE;
1635                 goto FINISH_OFF;
1636         }
1637
1638         /* Mail ID */
1639         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1640                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1641                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1642                 goto FINISH_OFF;
1643         }
1644
1645         /* timeout */
1646         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(timeout_in_sec), sizeof(int))) {
1647                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1648                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1649                 goto FINISH_OFF;
1650         }
1651
1652         /* Execute API */
1653         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1654                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1655                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1656                 goto FINISH_OFF;
1657         }
1658
1659         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1660
1661 FINISH_OFF:
1662         if (hAPI)
1663                 emipc_destroy_email_api(hAPI);
1664
1665         EM_DEBUG_API_END("err[%d]", err);
1666         return err;
1667
1668 }
1669
1670 EXPORT_API int email_get_max_mail_count(int *Count)
1671 {
1672         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1673         EM_DEBUG_API_BEGIN();
1674         int err = EMAIL_ERROR_NONE;
1675         EM_IF_NULL_RETURN_VALUE(Count, EMAIL_ERROR_INVALID_PARAM);
1676         *Count = emstorage_get_max_mail_count();
1677         EM_DEBUG_API_END("err[%d]", err);
1678         return err;
1679 }
1680
1681
1682
1683 /* for setting application,disk usage of email in KB */
1684 EXPORT_API int email_get_disk_space_usage(unsigned long *total_size)
1685 {
1686         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1687         EM_DEBUG_API_BEGIN("total_size[%p]", total_size);
1688         int err = EMAIL_ERROR_NONE;
1689
1690         EM_IF_NULL_RETURN_VALUE(total_size, EMAIL_ERROR_INVALID_PARAM);
1691
1692         if (!emstorage_mail_get_total_diskspace_usage(total_size, true, &err))  {
1693                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage failed [%d]", err);
1694
1695                 goto FINISH_OFF;
1696         }
1697
1698 FINISH_OFF:
1699
1700         EM_DEBUG_API_END("err[%d]", err);
1701         return err;
1702 }
1703
1704 EXPORT_API int email_get_address_info_list(int mail_id, email_address_info_list_t** address_info_list)
1705 {
1706         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1707         EM_DEBUG_FUNC_BEGIN("mail_id[%d] address_info_list[%p]", mail_id, address_info_list);
1708
1709         int err = EMAIL_ERROR_NONE;
1710     char *multi_user_name = NULL;
1711
1712         email_address_info_list_t *temp_address_info_list = NULL;
1713
1714         EM_IF_NULL_RETURN_VALUE(address_info_list, EMAIL_ERROR_INVALID_PARAM);
1715         if (mail_id <= 0) {
1716                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1717                 err = EMAIL_ERROR_INVALID_PARAM ;
1718                 return err;
1719         }
1720
1721     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1722         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1723         goto FINISH_OFF;
1724     }
1725
1726         if (!emcore_get_mail_address_info_list(multi_user_name, mail_id, &temp_address_info_list, &err)) {
1727                 EM_DEBUG_EXCEPTION("emcore_get_mail_address_info_list failed [%d]", err);
1728                 goto FINISH_OFF;
1729         }
1730
1731         if (address_info_list) {
1732                 *address_info_list = temp_address_info_list;
1733                 temp_address_info_list = NULL;
1734         }
1735
1736 FINISH_OFF:
1737
1738         if (temp_address_info_list)
1739                 emstorage_free_address_info_list(&temp_address_info_list);
1740
1741     EM_SAFE_FREE(multi_user_name);
1742         EM_DEBUG_FUNC_END("err[%d]", err);
1743         return err;
1744 }
1745
1746 EXPORT_API int email_free_address_info_list(email_address_info_list_t **address_info_list)
1747 {
1748         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1749         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
1750
1751         int err = EMAIL_ERROR_NONE;
1752
1753         if ((err = emstorage_free_address_info_list(address_info_list)) != EMAIL_ERROR_NONE) {
1754                 EM_DEBUG_EXCEPTION("address_info_list[%p] free failed.", address_info_list);
1755         }
1756         EM_DEBUG_FUNC_END("err[%d]", err);
1757         return err;
1758 }
1759
1760 EXPORT_API int email_query_meeting_request(char *input_conditional_clause_string, email_meeting_request_t **output_meeting_req, int *output_count)
1761 {
1762         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1763         EM_DEBUG_API_BEGIN("input_conditional_clause_string[%s] output_meeting_req[%p] output_count[%p]", input_conditional_clause_string, output_meeting_req, output_count);
1764         int err = EMAIL_ERROR_NONE;
1765     char *multi_user_name = NULL;
1766
1767         EM_IF_NULL_RETURN_VALUE(input_conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
1768         EM_IF_NULL_RETURN_VALUE(output_count, EMAIL_ERROR_INVALID_PARAM);
1769
1770     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1771         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1772         return err;
1773     }
1774
1775         if ((err = emstorage_query_meeting_request(multi_user_name, input_conditional_clause_string, output_meeting_req, output_count, true)) != EMAIL_ERROR_NONE) {
1776                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed [%d]", err);
1777         }
1778
1779     EM_SAFE_FREE(multi_user_name);
1780
1781         EM_DEBUG_API_END("err[%d]", err);
1782         return err;
1783 }
1784
1785 EXPORT_API int email_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req)
1786 {
1787         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1788         EM_DEBUG_API_BEGIN("mail_id[%d] meeting_req[%p]", mail_id, meeting_req);
1789
1790         int err = EMAIL_ERROR_NONE;
1791     char *multi_user_name = NULL;
1792         email_meeting_request_t *temp_meeting_req = NULL;
1793
1794         EM_IF_NULL_RETURN_VALUE(meeting_req, EMAIL_ERROR_INVALID_PARAM);
1795         if (mail_id <= 0) {
1796                 EM_DEBUG_EXCEPTION(" Invalid Mail Id Param ");
1797                 err = EMAIL_ERROR_INVALID_PARAM ;
1798                 return err;
1799         }
1800
1801     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1802         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1803         return err;
1804     }
1805
1806         if (!emstorage_get_meeting_request(multi_user_name, mail_id, &temp_meeting_req, 1, &err)) {
1807                 EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed[%d]", err);
1808                 goto FINISH_OFF;
1809         }
1810
1811         if (meeting_req)
1812                 *meeting_req = temp_meeting_req;
1813
1814 FINISH_OFF:
1815
1816     EM_SAFE_FREE(multi_user_name);
1817
1818         EM_DEBUG_API_END("err[%d]", err);
1819         return err;
1820 }
1821
1822 EXPORT_API int email_free_meeting_request(email_meeting_request_t** meeting_req, int count)
1823 {
1824         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1825         EM_DEBUG_FUNC_BEGIN("meeting_req[%p] count[%d]", meeting_req, count);
1826         if (!meeting_req || !*meeting_req) {
1827                 EM_DEBUG_EXCEPTION("NULL PARAM");
1828                 return EMAIL_ERROR_INVALID_PARAM;
1829         }
1830
1831         int i = 0;
1832         email_meeting_request_t *cur = *meeting_req;
1833         for (; i < count ; i++)
1834                 emstorage_free_meeting_request(cur + i);
1835
1836         EM_SAFE_FREE(*meeting_req);
1837
1838         EM_DEBUG_FUNC_END();
1839         return EMAIL_ERROR_NONE;
1840 }
1841
1842 EXPORT_API int email_move_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag)
1843 {
1844         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1845         EM_DEBUG_API_BEGIN("thread_id[%d] target_mailbox_id[%d] move_always_flag[%d]", thread_id, target_mailbox_id, move_always_flag);
1846         int err = EMAIL_ERROR_NONE;
1847
1848         EM_IF_NULL_RETURN_VALUE(target_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
1849
1850         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_MOVE_THREAD_TO_MAILBOX);
1851
1852         if (!hAPI) {
1853                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1854                 err = EMAIL_ERROR_NULL_VALUE;
1855                 goto FINISH_OFF;
1856         }
1857
1858         /* thread_id */
1859         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))) {
1860                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1861                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1862                 goto FINISH_OFF;
1863         }
1864
1865         /* target mailbox information */
1866         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&target_mailbox_id, sizeof(int))) {
1867                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1868                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1869                         goto FINISH_OFF;
1870         }
1871
1872         /* move_always_flag */
1873         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&move_always_flag, sizeof(int))) {
1874                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1875                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1876                 goto FINISH_OFF;
1877         }
1878
1879         /* Execute API */
1880         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1881                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1882                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1883                 goto FINISH_OFF;
1884         }
1885
1886         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1887
1888 FINISH_OFF:
1889         if (hAPI)
1890                 emipc_destroy_email_api(hAPI);
1891
1892         EM_DEBUG_API_END("err[%d]", err);
1893         return err;
1894 }
1895
1896 EXPORT_API int email_delete_thread(int thread_id, int delete_always_flag)
1897 {
1898         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1899         EM_DEBUG_API_BEGIN("thread_id[%d] delete_always_flag[%d]", thread_id, delete_always_flag);
1900         int err = EMAIL_ERROR_NONE;
1901
1902         if (thread_id <= 0) {
1903                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1904                 err = EMAIL_ERROR_INVALID_PARAM;
1905                 return err;
1906         }
1907
1908         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_THREAD);
1909
1910         if (!hAPI) {
1911                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1912                 err = EMAIL_ERROR_NULL_VALUE;
1913                 goto FINISH_OFF;
1914         }
1915
1916         /* thread_id */
1917         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))) {
1918                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1919                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1920                 goto FINISH_OFF;
1921         }
1922
1923         /* delete_always_flag */
1924         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&delete_always_flag, sizeof(int))) {
1925                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1926                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1927                 goto FINISH_OFF;
1928         }
1929
1930         /* Execute API */
1931         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1932                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1933                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1934                 goto FINISH_OFF;
1935         }
1936
1937         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1938
1939 FINISH_OFF:
1940
1941         if (hAPI)
1942                 emipc_destroy_email_api(hAPI);
1943
1944         EM_DEBUG_API_END("err[%d]", err);
1945         return err;
1946 }
1947
1948 EXPORT_API int email_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server)
1949 {
1950         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
1951         EM_DEBUG_API_BEGIN("thread_id[%d] seen_flag[%d] on_server[%d]", thread_id, seen_flag, on_server);
1952         int err = EMAIL_ERROR_NONE;
1953
1954         if (thread_id <= 0) {
1955                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1956                 err = EMAIL_ERROR_INVALID_PARAM;
1957                 return err;
1958         }
1959
1960         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD);
1961
1962         if (!hAPI) {
1963                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1964                 err = EMAIL_ERROR_NULL_VALUE;
1965                 goto FINISH_OFF;
1966         }
1967
1968         /* thread_id */
1969         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))) {
1970                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1971                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1972                 goto FINISH_OFF;
1973         }
1974
1975         /* seen_flag */
1976         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&seen_flag, sizeof(int))) {
1977                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1978                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1979                 goto FINISH_OFF;
1980         }
1981
1982         /* on_server */
1983         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&on_server, sizeof(int))) {
1984                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1985                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1986                 goto FINISH_OFF;
1987         }
1988
1989         /* Execute API */
1990         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1991                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1992                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1993                 goto FINISH_OFF;
1994         }
1995
1996         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1997
1998 FINISH_OFF:
1999         if (hAPI)
2000                 emipc_destroy_email_api(hAPI);
2001         EM_DEBUG_API_END("err[%d]", err);
2002         return err;
2003 }
2004
2005 EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle)
2006 {
2007         CHECK_EMAILS_SUPPORTED(EMAIL_FEATURE);
2008         EM_DEBUG_API_BEGIN("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
2009         int err = EMAIL_ERROR_NONE;
2010         int return_value = 0;
2011     char *multi_user_name = NULL;
2012         HIPC_API hAPI = NULL;
2013         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
2014
2015     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
2016         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
2017         goto FINISH_OFF;
2018     }
2019
2020         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
2021                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
2022                 goto FINISH_OFF;
2023         }
2024
2025         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
2026         ASNotiData as_noti_data;
2027
2028         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
2029
2030         if (em_get_account_server_type_by_account_id(multi_user_name, mailbox_tbl_data->account_id, &account_server_type, true, &err) == false) {
2031                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
2032                 goto FINISH_OFF;
2033         }
2034
2035         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server == true) {
2036                 int as_handle = 0;
2037
2038                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
2039                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
2040                         goto FINISH_OFF;
2041                 }
2042
2043                 /*  noti to active sync */
2044                 as_noti_data.expunge_mails_deleted_flagged.handle          = as_handle;
2045                 as_noti_data.expunge_mails_deleted_flagged.account_id      = mailbox_tbl_data->account_id;
2046                 as_noti_data.expunge_mails_deleted_flagged.mailbox_id      = input_mailbox_id;
2047                 as_noti_data.expunge_mails_deleted_flagged.on_server       = input_on_server;
2048                 as_noti_data.expunge_mails_deleted_flagged.multi_user_name = multi_user_name;
2049
2050                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED, &as_noti_data);
2051
2052                 if (return_value == false) {
2053                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
2054                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
2055                         goto FINISH_OFF;
2056                 }
2057
2058                 if (output_handle)
2059                         *output_handle = as_handle;
2060
2061         } else {
2062                 hAPI = emipc_create_email_api(_EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED);
2063
2064                 if (!hAPI) {
2065                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
2066                         err = EMAIL_ERROR_NULL_VALUE;
2067                         goto FINISH_OFF;
2068                 }
2069
2070                 /* input_mailbox_id */
2071                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
2072                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2073                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2074                         goto FINISH_OFF;
2075                 }
2076
2077                 /* input_on_server */
2078                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_on_server, sizeof(int))) {
2079                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2080                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2081                         goto FINISH_OFF;
2082                 }
2083
2084                 /* Execute API */
2085                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
2086                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
2087                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
2088                         goto FINISH_OFF;
2089                 }
2090
2091                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
2092                 if (err == EMAIL_ERROR_NONE) {
2093                         if (output_handle)
2094                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
2095                 }
2096         }
2097
2098 FINISH_OFF:
2099
2100         if (hAPI)
2101                 emipc_destroy_email_api(hAPI);
2102
2103         if (mailbox_tbl_data) {
2104                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
2105         }
2106
2107     EM_SAFE_FREE(multi_user_name);
2108         EM_DEBUG_API_END("err[%d]", err);
2109         return err;
2110 }
2111 /* LCOV_EXCL_STOP */
2112