2.0_alpha release commit
[framework/messaging/email-service.git] / email-api / email-api-mail.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /**
25  *
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 #include "email-api.h"
37 #include "email-ipc.h"
38 #include "email-convert.h"
39 #include "email-core-utils.h"
40 #include "email-core-mail.h"
41 #include "email-core-smtp.h"
42 #include "email-storage.h"
43 #include "email-utilities.h"
44 #include "db-util.h"
45
46 #define  DIR_SEPERATOR_CH '/'
47
48 #define _DIRECT_TO_DB
49
50 #define MAX_SEARCH_LEN 1000
51
52 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)
53 {
54         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_request [%p], input_from_eas [%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
55
56         EM_IF_NULL_RETURN_VALUE(input_mail_data,               EMAIL_ERROR_INVALID_PARAM);
57         EM_IF_NULL_RETURN_VALUE(input_mail_data->account_id,   EMAIL_ERROR_INVALID_PARAM);
58         EM_IF_NULL_RETURN_VALUE(input_mail_data->mailbox_id,   EMAIL_ERROR_INVALID_PARAM);
59
60         if(input_attachment_count > 0 && !input_attachment_data_list) {
61                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
62                 return EMAIL_ERROR_INVALID_PARAM;
63         }
64
65         int       err = EMAIL_ERROR_NONE;
66         int       size = 0;
67         char     *mail_data_stream = NULL;
68         char     *attachment_data_list_stream  = NULL;
69         char     *meeting_request_stream = NULL;
70         HIPC_API  hAPI = NULL;
71         
72         if(input_from_eas == 0) { /* native emails */
73                 hAPI = emipc_create_email_api(_EMAIL_API_ADD_MAIL);
74
75                 if(!hAPI) {
76                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
77                         err = EMAIL_ERROR_NULL_VALUE;           
78                         goto FINISH_OFF;
79                 }
80
81                 /* email_mail_data_t */
82                 mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
83
84                 if(!mail_data_stream) {
85                         EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
86                         err = EMAIL_ERROR_NULL_VALUE;
87                         goto FINISH_OFF;
88                 }
89                 emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size);
90                 
91                 /* email_attachment_data_t */
92                 attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data_list, input_attachment_count, &size);
93
94                 if(!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
95                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
96                         err = EMAIL_ERROR_OUT_OF_MEMORY;
97                         goto FINISH_OFF;
98                 }
99
100
101                 /*  email_meeting_request_t */
102                 if ( input_mail_data->meeting_request_status != EMAIL_MAIL_TYPE_NORMAL ) {
103                         meeting_request_stream = em_convert_meeting_req_to_byte_stream(input_meeting_request, &size);
104
105                         if(!meeting_request_stream) {
106                                 EM_DEBUG_EXCEPTION("em_convert_meeting_req_to_byte_stream failed");
107                                 err = EMAIL_ERROR_NULL_VALUE;
108                                 goto FINISH_OFF;
109                         }
110
111                         emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, meeting_request_stream, size);
112                 }
113
114                 /* input_from_eas */
115                 emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_eas, sizeof(int));
116
117                 /* Execute API */
118                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
119                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
120                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
121                         goto FINISH_OFF;
122                 }
123
124                 /* get result from service */
125                 err = *((int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 0));
126                 if(err == EMAIL_ERROR_NONE) {
127                         /* result mail_id */
128                         input_mail_data->mail_id = *((int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 1));
129                         /* result thread_id */
130                         input_mail_data->thread_id = *((int*)emipc_get_nth_parameter_data(hAPI, ePARAMETER_OUT, 2));
131                 }
132         } 
133         else { /* take care of mails from eas */
134                 err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
135                 if(err != EMAIL_ERROR_NONE) {
136                         EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
137                         goto FINISH_OFF;
138                 }
139         }
140
141 FINISH_OFF:
142         if(hAPI) 
143                 emipc_destroy_email_api(hAPI);
144
145         EM_DEBUG_FUNC_END("err [%d]", err);
146         return err;
147
148 }
149
150 EXPORT_API int email_add_read_receipt(int input_read_mail_id, int *output_receipt_mail_id)
151 {
152         EM_DEBUG_FUNC_BEGIN("input_read_mail_id [%d], output_receipt_mail_id [%p]", input_read_mail_id, output_receipt_mail_id);
153
154         EM_IF_NULL_RETURN_VALUE(output_receipt_mail_id, EMAIL_ERROR_INVALID_PARAM);
155
156         int      err = EMAIL_ERROR_NONE;
157         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_READ_RECEIPT);
158
159         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
160
161         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_read_mail_id, sizeof(int))) {
162                 EM_DEBUG_EXCEPTION("Add Param mail body Fail");
163                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
164         }
165
166         /* Execute API */
167         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
168                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
169                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
170         }
171
172         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
173
174         EM_DEBUG_LOG("err [%d]", err);
175
176         if(err == EMAIL_ERROR_NONE) {
177                 /* Get receipt mail id */
178                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_receipt_mail_id);
179                 EM_DEBUG_LOG("output_receipt_mail_id [%d]", *output_receipt_mail_id);
180         }
181
182         emipc_destroy_email_api(hAPI);
183
184         hAPI = NULL;
185
186         EM_DEBUG_FUNC_END("err [%d]", err);
187         return err;
188 }
189
190
191
192 int email_create_db_full()
193 {
194         int mailbox_index, mail_index, mailbox_count, mail_slot_size;
195         emstorage_mail_tbl_t mail_table_data = {0};
196         email_mailbox_t *mailbox_list = NULL;
197         int err = EMAIL_ERROR_NONE;
198
199         if ( (err = email_open_db()) != EMAIL_ERROR_NONE) {
200                 EM_DEBUG_EXCEPTION("email_open_db failed [%d]", err);
201                 return err;
202         }
203
204         mail_table_data.subject = (char*) em_malloc(50); 
205         mail_table_data.full_address_from = strdup("<dummy_from@nowhere.com>");
206         mail_table_data.full_address_to = strdup("<dummy_to@nowhere.com>");
207         mail_table_data.account_id =1;
208
209         if( (err = email_get_mailbox_list_ex(1, -1, 0, &mailbox_list, &mailbox_count)) < EMAIL_ERROR_NONE) {
210                 EM_DEBUG_EXCEPTION("email_get_mailbox_list_ex failed [%d]", err);
211                 goto FINISH_OFF;
212         }
213
214         for(mailbox_index = 0; mailbox_index < mailbox_count; mailbox_index++) {
215                 mail_slot_size= mailbox_list[mailbox_index].mail_slot_size;
216                 for(mail_index = 0; mail_index < mail_slot_size; mail_index++) {
217                         sprintf(mail_table_data.subject, "Subject #%d",mail_index);
218                         mail_table_data.mailbox_id   = mailbox_list[mailbox_index].mailbox_id;
219                         mail_table_data.mailbox_type = mailbox_list[mailbox_index].mailbox_type;
220                         if( !emstorage_add_mail(&mail_table_data, 1, true, &err))  {
221                                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]",err);
222                 
223                                 goto FINISH_OFF;
224                         }
225                 }
226         }
227
228 FINISH_OFF:
229         if ( (err = email_close_db()) != EMAIL_ERROR_NONE) {
230                 EM_DEBUG_EXCEPTION("email_close_db failed [%d]", err);
231         }       
232         
233         if(mailbox_list)
234                 email_free_mailbox(&mailbox_list, mailbox_count);
235
236         EM_SAFE_FREE(mail_table_data.subject);
237         EM_SAFE_FREE(mail_table_data.full_address_from);
238         EM_SAFE_FREE(mail_table_data.full_address_to);
239
240         return err;
241 }
242
243 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)
244 {
245         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_request [%p], input_from_eas [%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
246
247         EM_IF_NULL_RETURN_VALUE(input_mail_data,               EMAIL_ERROR_INVALID_PARAM);
248         EM_IF_NULL_RETURN_VALUE(input_mail_data->account_id,   EMAIL_ERROR_INVALID_PARAM);
249
250         if(input_attachment_count > 0 && !input_attachment_data_list) {
251                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
252                 return EMAIL_ERROR_INVALID_PARAM;
253         }
254
255         int       err = EMAIL_ERROR_NONE;
256         int       size = 0;
257         char     *mail_data_stream = NULL;
258         char     *attachment_data_list_stream  = NULL;
259         char     *meeting_request_stream = NULL;
260
261         HIPC_API  hAPI = NULL;
262         
263         if(input_from_eas == 0) {
264                 hAPI = emipc_create_email_api(_EMAIL_API_UPDATE_MAIL);  
265
266                 if(!hAPI) {
267                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
268                         err = EMAIL_ERROR_NULL_VALUE;           
269                         goto FINISH_OFF;
270                 }
271
272                 /* email_mail_data_t */
273                 mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
274
275                 if(!mail_data_stream) {
276                         EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
277                         err = EMAIL_ERROR_NULL_VALUE;
278                         goto FINISH_OFF;
279                 }
280                 
281                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size)) {
282                         EM_DEBUG_EXCEPTION("emipc_add_parameter for head failed");
283                         err = EMAIL_ERROR_OUT_OF_MEMORY;
284                         goto FINISH_OFF;
285                 }
286                 
287                 /* email_attachment_data_t */
288                 attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data_list, input_attachment_count, &size);
289
290                 if(!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
291                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
292                         err = EMAIL_ERROR_OUT_OF_MEMORY;
293                         goto FINISH_OFF;
294                 }
295
296                 /*  email_meeting_request_t */
297                 if ( input_mail_data->meeting_request_status != EMAIL_MAIL_TYPE_NORMAL ) {
298                         meeting_request_stream = em_convert_meeting_req_to_byte_stream(input_meeting_request, &size);
299
300                         if(!meeting_request_stream) {
301                                 EM_DEBUG_EXCEPTION("em_convert_meeting_req_to_byte_stream failed");
302                                 err = EMAIL_ERROR_NULL_VALUE;
303                                 goto FINISH_OFF;
304                         }
305
306                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, meeting_request_stream, size)) {
307                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
308                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
309                                 goto FINISH_OFF;
310                         }
311                 }
312
313                 /* input_from_eas */
314                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_eas, sizeof(int))) {
315                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
316                         err = EMAIL_ERROR_OUT_OF_MEMORY;
317                         goto FINISH_OFF;
318                 }
319
320                 /* Execute API */
321                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
322                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
323                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
324                         goto FINISH_OFF;
325                 }
326
327                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
328                 
329                 if(err == EMAIL_ERROR_NONE) {
330                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &input_mail_data->mail_id);
331                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 2, sizeof(int), &input_mail_data->thread_id);
332                 }
333         } 
334         else {
335                 if( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
336                         EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
337                         goto FINISH_OFF;
338                 }
339         }
340
341 FINISH_OFF:
342         if(hAPI) 
343                 emipc_destroy_email_api(hAPI);
344
345         EM_SAFE_FREE(mail_data_stream);
346
347         EM_DEBUG_FUNC_END("err [%d]", err);
348         return err;
349
350 }
351
352 EXPORT_API int email_clear_mail_data()
353 {
354         EM_DEBUG_FUNC_BEGIN();
355         int err = EMAIL_ERROR_NONE;
356         
357         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_DATA);  
358
359         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
360         
361         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
362                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api Fail");
363                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
364         }
365         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
366         
367         if(hAPI)
368                 emipc_destroy_email_api(hAPI);
369
370         EM_DEBUG_FUNC_END("err [%d]", err);
371         return err;
372 }
373
374
375 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)
376 {
377         EM_DEBUG_FUNC_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);
378         
379         int total_count = 0;
380         int unread = 0;
381         int err = EMAIL_ERROR_NONE;
382         char *conditional_clause_string = NULL;
383
384         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
385         EM_IF_NULL_RETURN_VALUE(input_filter_count, EMAIL_ERROR_INVALID_PARAM);
386         EM_IF_NULL_RETURN_VALUE(output_total_mail_count, EMAIL_ERROR_INVALID_PARAM);
387         EM_IF_NULL_RETURN_VALUE(output_unseen_mail_count, EMAIL_ERROR_INVALID_PARAM);
388
389         if( (err = emstorage_write_conditional_clause_for_getting_mail_list(input_filter_list, input_filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
390                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
391                 goto FINISH_OFF;
392         }
393
394         if ((err = emstorage_query_mail_count(conditional_clause_string, true, &total_count, &unread)) != EMAIL_ERROR_NONE) {
395                 EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed [%d]", err);
396                 goto FINISH_OFF;
397         }
398
399         *output_total_mail_count = total_count;
400         *output_unseen_mail_count = unread;
401
402 FINISH_OFF:
403
404         EM_DEBUG_FUNC_END("err [%d]", err);
405         return err;
406 }
407
408
409 EXPORT_API int email_delete_mail(int input_mailbox_id, int *input_mail_ids, int input_num, int input_from_server)
410 {
411         EM_DEBUG_FUNC_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);
412
413         int err = EMAIL_ERROR_NONE;
414         HIPC_API hAPI = NULL;
415
416         EM_IF_NULL_RETURN_VALUE(input_mail_ids, EMAIL_ERROR_INVALID_PARAM);
417         
418         if (input_mailbox_id <= 0) {
419                 EM_DEBUG_EXCEPTION("input_mailbox_id = %d", input_mailbox_id);
420                 err = EMAIL_ERROR_INVALID_PARAM;
421                 return err;
422         }
423
424         if (input_num <= 0) {
425                 EM_DEBUG_EXCEPTION("input_num = %d", input_num);
426                 err = EMAIL_ERROR_INVALID_PARAM;                
427                 return err;
428         }
429
430         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAIL);
431         
432         if(!hAPI) {
433                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
434                 err = EMAIL_ERROR_NULL_VALUE;           
435                 goto FINISH_OFF;
436         }
437
438         /* mailbox id*/
439         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
440                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
441                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
442                 goto FINISH_OFF;
443         }
444
445         /* Number of mail_ids */
446         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_num, sizeof(int))) {
447                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
448                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
449                 goto FINISH_OFF;
450         }
451         
452         /* set of mail_ids */
453         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)input_mail_ids, input_num * sizeof(int))) {
454                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
455                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
456                 goto FINISH_OFF;
457         }
458         
459         /* from-server */
460         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_server, sizeof(int))) {
461                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
462                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
463                 goto FINISH_OFF;
464         }
465         
466         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
467                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
468                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
469                 goto FINISH_OFF;
470         }
471
472         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
473
474 FINISH_OFF:
475         if(hAPI)
476                 emipc_destroy_email_api(hAPI);
477
478         EM_DEBUG_FUNC_END("err [%d]", err);
479         return err;
480 }
481
482
483 EXPORT_API int email_delete_all_mails_in_mailbox(int input_mailbox_id, int input_from_server)
484 {
485         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_from_server[%d]", input_mailbox_id, input_from_server);
486
487         int err = EMAIL_ERROR_NONE;
488         HIPC_API hAPI = NULL;
489         
490         if(input_mailbox_id <= 0) {
491                 EM_DEBUG_EXCEPTION("input_mailbox_id = %d", input_mailbox_id);
492                 err = EMAIL_ERROR_INVALID_PARAM;
493                 return err;
494         }
495         
496         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_ALL_MAIL);
497
498         if(!hAPI) {
499                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
500                 err = EMAIL_ERROR_NULL_VALUE;           
501                 goto FINISH_OFF;
502         }
503         
504         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
505                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
506                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
507                 goto FINISH_OFF;
508         }
509
510         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_from_server, sizeof(int))){
511                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
512                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
513                 goto FINISH_OFF;
514         }
515         
516         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
517                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
518                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
519                 goto FINISH_OFF;
520         }
521         
522         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int),&err );
523
524 FINISH_OFF:
525
526         if(hAPI)        
527                 emipc_destroy_email_api(hAPI);
528         
529         EM_DEBUG_FUNC_END("err [%d]", err);
530         return err;
531 }
532
533
534
535 EXPORT_API int email_add_attachment(int mail_id, email_attachment_data_t* attachment)
536 {
537         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p]", mail_id, attachment);
538         int err = EMAIL_ERROR_NONE;
539         char *attchment_stream = NULL;
540         int size = 0;
541         HIPC_API hAPI = NULL;
542         
543         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
544         EM_IF_NULL_RETURN_VALUE(attachment, EMAIL_ERROR_INVALID_PARAM);
545         
546         hAPI = emipc_create_email_api(_EMAIL_API_ADD_ATTACHMENT);
547
548         if(!hAPI) {
549                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
550                 err = EMAIL_ERROR_NULL_VALUE;           
551                 goto FINISH_OFF;
552         }
553
554         /* mail_id */
555         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
556                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
557                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
558                 goto FINISH_OFF;
559         }
560
561         /* Attachment */
562         attchment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
563
564         if(!attchment_stream) {
565                 EM_DEBUG_EXCEPTION("em_convert_attachment_data_to_byte_stream failed");
566                 err = EMAIL_ERROR_NULL_VALUE;           
567                 goto FINISH_OFF;
568         }
569
570         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, attchment_stream, size)){
571                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
572                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
573                 goto FINISH_OFF;
574         }
575
576         /* Execute API */
577         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
578                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
579                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
580                 goto FINISH_OFF;
581         }
582
583         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
584         
585         if(EMAIL_ERROR_NONE == err) {
586                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &attachment->attachment_id);
587         }
588
589 FINISH_OFF:
590         EM_SAFE_FREE(attchment_stream);
591         
592         if(hAPI)
593                 emipc_destroy_email_api(hAPI);
594         
595         EM_DEBUG_FUNC_END("err [%d]", err);
596         return err;
597  }
598
599
600 EXPORT_API int email_delete_attachment(int attachment_id)
601 {
602         EM_DEBUG_FUNC_BEGIN("attachment_id[%d]", attachment_id);
603         int err = EMAIL_ERROR_NONE;
604         HIPC_API hAPI = NULL;
605         
606         EM_IF_NULL_RETURN_VALUE(attachment_id, EMAIL_ERROR_INVALID_PARAM);
607         
608         hAPI = emipc_create_email_api(_EMAIL_API_DELETE_ATTACHMENT);
609         
610         if(!hAPI) {
611                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
612                 err = EMAIL_ERROR_NULL_VALUE;           
613                 goto FINISH_OFF;
614         }
615
616         /* attachment_index */
617         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&attachment_id, sizeof(int))) {
618                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
619                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
620                 goto FINISH_OFF;
621         }
622
623         /* Execute API */
624         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
625                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
626                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
627                 goto FINISH_OFF;
628         }
629
630         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
631
632 FINISH_OFF:
633         if(hAPI)
634                 emipc_destroy_email_api(hAPI);
635
636         return err;
637 }
638
639 /* -----------------------------------------------------------
640                                               Mail Search API
641     -----------------------------------------------------------*/
642
643 EXPORT_API int email_query_mails(char *conditional_clause_string, email_mail_data_t** mail_list,  int *result_count)
644 {
645         EM_DEBUG_FUNC_BEGIN("conditional_clause_string [%s], mail_list [%p], result_count [%p]", conditional_clause_string, mail_list, result_count);
646
647         int err = EMAIL_ERROR_NONE;
648         emstorage_mail_tbl_t *result_mail_tbl;
649         
650         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
651         EM_IF_NULL_RETURN_VALUE(conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
652
653         if (!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, result_count, &err)) {
654                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
655
656                 goto FINISH_OFF;
657         }
658
659         if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, *result_count, mail_list, &err)) {
660                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
661                 goto FINISH_OFF;
662         }
663
664 FINISH_OFF:     
665         if(result_mail_tbl && !emstorage_free_mail(&result_mail_tbl, *result_count, &err))
666                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed [%d]", err);
667
668         EM_DEBUG_FUNC_END("err [%d]", err);
669         return err;
670 }
671
672 EXPORT_API int email_query_mail_list(char *input_conditional_clause_string, email_mail_list_item_t** output_mail_list,  int *output_result_count)
673 {
674         EM_DEBUG_FUNC_BEGIN("input_conditional_clause_string [%s], output_mail_list [%p], output_result_count [%p]", input_conditional_clause_string, output_mail_list, output_result_count);
675
676         int err = EMAIL_ERROR_NONE;
677         
678         EM_IF_NULL_RETURN_VALUE(input_conditional_clause_string, EMAIL_ERROR_INVALID_PARAM);
679         EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
680
681         if (!emstorage_query_mail_list(input_conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
682                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list failed [%d]", err);
683                 goto FINISH_OFF;
684         }
685
686 FINISH_OFF:     
687         EM_DEBUG_FUNC_END("err [%d]", err);
688         return err;
689 }
690
691
692 /* -----------------------------------------------------------
693                                               Mail Get Info API
694     -----------------------------------------------------------*/
695 EXPORT_API int email_get_attachment_data(int attachment_id, email_attachment_data_t** attachment)
696 {
697         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p]", attachment_id, attachment);
698         
699         int err = EMAIL_ERROR_NONE;
700         int nSize = 0;
701         int attachment_count = 0;
702         char* attchment_stream = NULL;
703         email_attachment_data_t* attachment_data = NULL;
704
705         EM_IF_NULL_RETURN_VALUE(attachment_id, EMAIL_ERROR_INVALID_PARAM);
706         EM_IF_NULL_RETURN_VALUE(attachment, EMAIL_ERROR_INVALID_PARAM);
707
708         
709         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_ATTACHMENT);
710
711         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
712
713         /* attachment_id */
714         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&attachment_id, sizeof(int))) {
715                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
716                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
717                 goto FINISH_OFF;
718         }
719
720         /* Execute API */
721         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
722                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
723                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
724                 goto FINISH_OFF;
725         }
726
727         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
728
729         if(EMAIL_ERROR_NONE == err) {
730                 nSize = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 1);
731                 if(nSize > 0) {
732                         attchment_stream = (char*)em_malloc(nSize+1);
733
734                         if(!attchment_stream) {
735                                 EM_DEBUG_EXCEPTION("em_malloc failed");
736                                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
737                                 goto FINISH_OFF;
738                         }       
739                 
740                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, nSize, attchment_stream);
741                         em_convert_byte_stream_to_attachment_data(attchment_stream, nSize, &attachment_data, &attachment_count);
742                 }
743                 
744                 if(!attachment_data) {
745                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NULL_VALUE");
746                         err = EMAIL_ERROR_NULL_VALUE;           
747                         goto FINISH_OFF;
748                 }
749
750                 *attachment = attachment_data;
751         }
752
753 FINISH_OFF:
754         EM_SAFE_FREE(attchment_stream);
755
756         if(hAPI)
757                 emipc_destroy_email_api(hAPI);
758
759         EM_DEBUG_FUNC_END("err [%d]", err);
760         return err;
761
762 }
763
764 EXPORT_API int email_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
765 {
766         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_attachment_data[%p], output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
767         int err = EMAIL_ERROR_NONE;
768         
769         if((err = emcore_get_attachment_data_list(input_mail_id, output_attachment_data, output_attachment_count)) != EMAIL_ERROR_NONE) {
770                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
771         }
772
773         EM_DEBUG_FUNC_END("err [%d]", err);
774         return err;
775 }
776
777 EXPORT_API int email_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count)
778 {
779         EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count[%d]", attachment_data_list, attachment_data_count);        
780         
781         int err = EMAIL_ERROR_NONE;
782
783         emcore_free_attachment_data(attachment_data_list, attachment_data_count, &err);
784         
785         EM_DEBUG_FUNC_END("err [%d]", err);
786         return err;
787 }
788
789 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)
790 {
791         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);
792
793         int err = EMAIL_ERROR_NONE;
794         char *conditional_clause_string = NULL;
795
796         EM_IF_NULL_RETURN_VALUE(output_mail_list, EMAIL_ERROR_INVALID_PARAM);
797         EM_IF_NULL_RETURN_VALUE(output_result_count, EMAIL_ERROR_INVALID_PARAM);
798
799         if( (err = emstorage_write_conditional_clause_for_getting_mail_list(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) {
800                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
801                 goto FINISH_OFF;
802         }
803
804         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
805
806         if(!emstorage_query_mail_list(conditional_clause_string, true, output_mail_list, output_result_count, &err)) {
807                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
808                 goto FINISH_OFF;
809         }
810
811 FINISH_OFF:
812         EM_SAFE_FREE(conditional_clause_string);
813
814         EM_DEBUG_FUNC_END("err [%d]", err);
815         return err;
816 }
817
818 EXPORT_API int email_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
819 {
820         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
821         int err = EMAIL_ERROR_NONE;
822
823         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
824
825         err = emstorage_free_list_filter(input_filter_list, input_filter_count);
826
827         EM_DEBUG_FUNC_END("err [%d]", err);
828         return err;
829 }
830
831 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)
832 {
833         EM_DEBUG_FUNC_BEGIN();
834
835         int err = EMAIL_ERROR_NONE;
836         emstorage_mail_tbl_t *mail_tbl_list = NULL;
837         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
838
839         if( account_id < ALL_ACCOUNT) {
840                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
841                 err = EMAIL_ERROR_INVALID_PARAM ;
842                 goto FINISH_OFF;
843         }
844
845         if (!emstorage_get_mails(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, sorting, true, &mail_tbl_list, result_count, &err))  {
846                 EM_DEBUG_EXCEPTION("emstorage_get_mails failed [%d]", err);
847
848                 goto FINISH_OFF;
849         }
850
851         if(!em_convert_mail_tbl_to_mail_data(mail_tbl_list, *result_count, mail_list, &err)) {
852                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
853                 goto FINISH_OFF;
854         }
855
856 FINISH_OFF:     
857         if(mail_tbl_list && !emstorage_free_mail(&mail_tbl_list, *result_count, &err))
858                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed [%d]", err);
859
860         EM_DEBUG_FUNC_END("err [%d]", err);
861         return err;
862 }
863
864 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)
865 {
866         EM_DEBUG_FUNC_BEGIN();
867
868         int err = EMAIL_ERROR_NONE;
869         
870         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
871
872         if( account_id < ALL_ACCOUNT) {
873                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
874                 return EMAIL_ERROR_INVALID_PARAM;
875         }
876
877         if (!emstorage_get_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, 0, NULL, sorting, true, mail_list, result_count, &err))  {
878                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
879
880                 goto FINISH_OFF;
881         }
882
883 FINISH_OFF:     
884         EM_DEBUG_FUNC_END("err [%d]", err);
885         return err;
886 }
887
888 EXPORT_API int email_get_mail_by_address(int account_id , int mailbox_id, email_email_address_list_t* addr_list,
889                                                                         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)
890 {
891         EM_DEBUG_FUNC_BEGIN();
892         int err = EMAIL_ERROR_NONE;
893
894         email_mail_list_item_t* mail_list_item = NULL;
895         
896         EM_IF_NULL_RETURN_VALUE(mail_list, EMAIL_ERROR_INVALID_PARAM);
897         EM_IF_NULL_RETURN_VALUE(result_count, EMAIL_ERROR_INVALID_PARAM);
898
899         if( account_id < ALL_ACCOUNT) {
900                 EM_DEBUG_EXCEPTION("Invalid account id param");
901                 err = EMAIL_ERROR_INVALID_PARAM ;
902                 return err;
903         }
904
905         if (!emstorage_get_mail_list(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)) {
906                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
907
908                 goto FINISH_OFF;
909         }
910
911         *mail_list = mail_list_item;
912
913 FINISH_OFF:
914         EM_DEBUG_FUNC_END("err [%d]", err);
915         return err;
916 }
917
918 EXPORT_API int email_get_thread_information_by_thread_id(int thread_id, email_mail_data_t** thread_info)
919 {
920         EM_DEBUG_FUNC_BEGIN();
921         int err = EMAIL_ERROR_NONE;
922         emstorage_mail_tbl_t *mail_table_data = NULL;
923         
924         EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
925
926         if (!emstorage_get_thread_information(thread_id, &mail_table_data , true, &err)) {
927                 EM_DEBUG_EXCEPTION("emstorage_get_thread_information  failed [%d]", err);
928                 goto FINISH_OFF;
929         }
930
931         if(!em_convert_mail_tbl_to_mail_data(mail_table_data, 1, thread_info, &err)) {
932                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data failed [%d]", err);
933                 goto FINISH_OFF;
934         }
935
936 FINISH_OFF:     
937
938         if(mail_table_data && !emstorage_free_mail(&mail_table_data, 1, &err)) 
939                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed [%d]", err);
940
941         EM_DEBUG_FUNC_END("err [%d]", err);
942         return err;
943 }
944
945 EXPORT_API int email_get_thread_information_ex(int thread_id, email_mail_list_item_t** thread_info)
946 {
947         EM_DEBUG_FUNC_BEGIN();
948         int err = EMAIL_ERROR_NONE;
949         emstorage_mail_tbl_t *mail_table_data = NULL;
950         email_mail_list_item_t *temp_thread_info = NULL;
951         
952         EM_IF_NULL_RETURN_VALUE(thread_info, EMAIL_ERROR_INVALID_PARAM);
953
954         if (!emstorage_get_thread_information(thread_id, &mail_table_data , true, &err)) {
955                 EM_DEBUG_EXCEPTION("emstorage_get_thread_information -- failed [%d]", err);
956                 goto FINISH_OFF;
957         }
958
959         temp_thread_info = em_malloc(sizeof(email_mail_list_item_t));
960
961         if(!temp_thread_info) {
962                 EM_DEBUG_EXCEPTION("em_malloc failed");
963                 err = EMAIL_ERROR_OUT_OF_MEMORY;
964                 goto FINISH_OFF;
965         }
966
967         EM_SAFE_STRNCPY(temp_thread_info->from               , mail_table_data->full_address_from, STRING_LENGTH_FOR_DISPLAY);
968         EM_SAFE_STRNCPY(temp_thread_info->from_email_address , mail_table_data->email_address_sender, MAX_EMAIL_ADDRESS_LENGTH);
969         EM_SAFE_STRNCPY(temp_thread_info->recipients         , mail_table_data->email_address_recipient, STRING_LENGTH_FOR_DISPLAY);
970         EM_SAFE_STRNCPY(temp_thread_info->subject            , mail_table_data->subject, STRING_LENGTH_FOR_DISPLAY);
971         EM_SAFE_STRNCPY(temp_thread_info->previewBodyText    , mail_table_data->preview_text, MAX_PREVIEW_TEXT_LENGTH);
972         temp_thread_info->mail_id                            = mail_table_data->mail_id;
973         temp_thread_info->mailbox_id                         = mail_table_data->mailbox_id;
974         temp_thread_info->account_id                         = mail_table_data->account_id;
975         temp_thread_info->date_time                          = mail_table_data->date_time;
976         temp_thread_info->is_text_downloaded                 = mail_table_data->body_download_status;
977         temp_thread_info->flags_seen_field                   = mail_table_data->flags_seen_field;
978         temp_thread_info->priority                           = mail_table_data->priority;
979         temp_thread_info->save_status                        = mail_table_data->save_status;
980         temp_thread_info->is_locked                          = mail_table_data->lock_status;
981         temp_thread_info->is_report_mail                     = mail_table_data->report_status;
982         temp_thread_info->has_attachment                     = mail_table_data->attachment_count;
983         temp_thread_info->has_drm_attachment                 = mail_table_data->DRM_status;
984         temp_thread_info->thread_id                          = mail_table_data->thread_id;
985         temp_thread_info->thread_item_count                  = mail_table_data->thread_item_count;
986         temp_thread_info->is_meeting_request                 = mail_table_data->meeting_request_status;
987
988         *thread_info = temp_thread_info;
989
990 FINISH_OFF:     
991
992         if(mail_table_data)
993                 emstorage_free_mail(&mail_table_data, 1, NULL);
994
995         EM_DEBUG_FUNC_END("err [%d]", err);
996         return err;
997 }
998
999 EXPORT_API int email_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
1000 {
1001         EM_DEBUG_FUNC_BEGIN();
1002         int err = EMAIL_ERROR_NONE;
1003         
1004         if ( ((err = emcore_get_mail_data(input_mail_id, output_mail_data)) != EMAIL_ERROR_NONE) || !output_mail_data) 
1005                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);    
1006                 
1007         EM_DEBUG_FUNC_END("err [%d]", err);     
1008         return err;
1009 }
1010
1011
1012 /* -----------------------------------------------------------
1013                                               Mail Flag API
1014     -----------------------------------------------------------*/
1015
1016 EXPORT_API int email_modify_seen_flag(int *mail_ids, int num, int seen_flag, int onserver)
1017 {
1018         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d],seen_flag[%d], on_server [ %d]", mail_ids, num, seen_flag, onserver);
1019         EM_DEBUG_FUNC_END("EMAIL_ERROR_NOT_IMPLEMENTED");
1020         return EMAIL_ERROR_NOT_IMPLEMENTED;
1021 }
1022
1023 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)
1024 {
1025         EM_DEBUG_FUNC_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);
1026         
1027         int err = EMAIL_ERROR_NONE;
1028
1029                 
1030         EM_IF_NULL_RETURN_VALUE(mail_ids, EMAIL_ERROR_INVALID_PARAM);
1031         if (account_id == 0 || num <= 0 || (onserver != 0 && onserver != 1)) {
1032                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1033                 return EMAIL_ERROR_INVALID_PARAM;                       
1034         }
1035         
1036         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_FLAGS_FIELD);
1037         
1038         if(!hAPI) {
1039                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1040                 err = EMAIL_ERROR_NULL_VALUE;           
1041                 goto FINISH_OFF;
1042         }
1043         
1044         /* account_id*/
1045         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
1046                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1047                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1048                 goto FINISH_OFF;
1049         }
1050
1051         /* Number of mail_ids */
1052         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&num, sizeof(int))) {
1053                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1054                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1055                 goto FINISH_OFF;
1056         }
1057         
1058         /* set of mail_ids */
1059         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)mail_ids, num * sizeof(int))) {
1060                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1061                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1062                 goto FINISH_OFF;
1063         }
1064         
1065         /* field_type */
1066         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&field_type, sizeof(int))) {
1067                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1068                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1069                 goto FINISH_OFF;
1070         }
1071         
1072         /* value */
1073         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&value, sizeof(int))) {
1074                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1075                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1076                 goto FINISH_OFF;
1077         }
1078
1079         /* onserver  */
1080         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(onserver), sizeof(int))) {
1081                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1082                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1083                 goto FINISH_OFF;
1084         }
1085         
1086         /* Execute API */
1087         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1088                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1089                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1090                 goto FINISH_OFF;
1091         }
1092         
1093         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1094
1095 FINISH_OFF:
1096         if(hAPI)
1097                 emipc_destroy_email_api(hAPI);
1098
1099         EM_DEBUG_FUNC_END("err [%d]", err);
1100         return err;
1101 }
1102
1103
1104 /* -----------------------------------------------------------
1105                                               Mail Move API
1106     -----------------------------------------------------------*/
1107 EXPORT_API int email_move_mail_to_mailbox(int *mail_ids, int num, int input_target_mailbox_id)
1108 {
1109         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num [%d], input_target_mailbox_id[%d]",  mail_ids, num, input_target_mailbox_id);
1110         
1111         int err = EMAIL_ERROR_NONE;
1112         HIPC_API hAPI = NULL;
1113
1114         if(input_target_mailbox_id <= 0) {
1115                 EM_DEBUG_EXCEPTION("input_target_mailbox_id is invalid parameter");
1116                 err = EMAIL_ERROR_INVALID_PARAM;
1117                 return err;
1118         }
1119         EM_IF_NULL_RETURN_VALUE(mail_ids, EMAIL_ERROR_INVALID_PARAM);
1120         
1121         if (num <= 0)  {
1122                 EM_DEBUG_LOG("num = %d", num);
1123                 err = EMAIL_ERROR_INVALID_PARAM;
1124                 return err;
1125         }
1126
1127         hAPI = emipc_create_email_api(_EMAIL_API_MOVE_MAIL);
1128
1129         if(!hAPI) {
1130                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1131                 err = EMAIL_ERROR_NULL_VALUE;           
1132                 goto FINISH_OFF;
1133         }
1134         
1135         /* Number of mail_ids */
1136         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&num, sizeof(int))) {
1137                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1138                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1139                 goto FINISH_OFF;
1140         }
1141         
1142         /* set of mail_ids */
1143         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)mail_ids, num * sizeof(int))) {
1144                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1145                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1146                 goto FINISH_OFF;
1147         }
1148         
1149         /* input_target_mailbox_id */
1150         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_target_mailbox_id, sizeof(int))) {
1151                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1152                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1153                 goto FINISH_OFF;
1154         }
1155
1156         /* Execute API */
1157         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1158                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1159                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
1160                 goto FINISH_OFF;
1161         }
1162
1163         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1164
1165 FINISH_OFF:             
1166         if(hAPI)
1167                 emipc_destroy_email_api(hAPI);
1168
1169         EM_DEBUG_FUNC_END("err [%d]", err);
1170         return err;
1171 }
1172
1173
1174 EXPORT_API int  email_move_all_mails_to_mailbox(int input_source_mailbox_id, int input_target_mailbox_id)
1175 {
1176         EM_DEBUG_FUNC_BEGIN("input_source_mailbox_id[%d] , input_target_mailbox_id[%d]",  input_source_mailbox_id, input_target_mailbox_id);
1177         
1178         int err = EMAIL_ERROR_NONE;
1179         HIPC_API hAPI = NULL;
1180         
1181         if(input_source_mailbox_id <= 0 || input_target_mailbox_id <= 0) {
1182                 EM_DEBUG_EXCEPTION("mailbox_id is invalid parameter");
1183                 err = EMAIL_ERROR_INVALID_PARAM;
1184                 return err;
1185         }
1186         
1187         hAPI = emipc_create_email_api(_EMAIL_API_MOVE_ALL_MAIL);
1188
1189         if(!hAPI) {
1190                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1191                 err = EMAIL_ERROR_NULL_VALUE;           
1192                 goto FINISH_OFF;
1193         }
1194         
1195         /* input_source_mailbox_id */
1196         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_source_mailbox_id, sizeof(int))) {
1197                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1198                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1199                 goto FINISH_OFF;
1200         }
1201         
1202         /* input_target_mailbox_id */
1203         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_target_mailbox_id, sizeof(int))) {
1204                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1205                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1206                 goto FINISH_OFF;
1207         }
1208
1209         /* Execute API */
1210         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1211                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1212                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;           
1213                 goto FINISH_OFF;
1214         }
1215
1216         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1217
1218 FINISH_OFF:
1219         if(hAPI)
1220                 emipc_destroy_email_api(hAPI);
1221         
1222         EM_DEBUG_FUNC_END("err [%d]", err);
1223         return err;
1224 }
1225
1226  
1227 EXPORT_API int email_free_mail_data(email_mail_data_t** mail_list, int count)
1228 {
1229         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d]", mail_list, count);
1230         int err = EMAIL_ERROR_NONE;
1231         emcore_free_mail_data_list(mail_list, count);
1232         EM_DEBUG_FUNC_END("err [%d]", err);     
1233         return err;
1234 }
1235
1236 /* Convert Modified UTF-7 mailbox name to UTF-8 */
1237 /* returns modified UTF-8 Name if success else NULL */
1238
1239 EXPORT_API int email_cancel_sending_mail( int mail_id)
1240 {
1241         EM_DEBUG_FUNC_BEGIN("Mail ID [ %d]", mail_id);
1242         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
1243         
1244         int err = EMAIL_ERROR_NONE;
1245         int account_id = 0;
1246         email_mail_data_t* mail_data = NULL;
1247
1248         HIPC_API hAPI = NULL;
1249         
1250         
1251         if ((err = emcore_get_mail_data(mail_id, &mail_data)) != EMAIL_ERROR_NONE)  {
1252                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
1253                 goto FINISH_OFF;
1254         }
1255
1256         if (!mail_data) {
1257                 EM_DEBUG_EXCEPTION("mail_data is null");
1258                 err = EMAIL_ERROR_NULL_VALUE;           
1259                 goto FINISH_OFF;
1260         }
1261
1262         account_id = mail_data->account_id;
1263
1264         hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL_CANCEL_JOB);
1265
1266         if(!hAPI) {
1267                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1268                 err = EMAIL_ERROR_NULL_VALUE;           
1269                 goto FINISH_OFF;
1270         }
1271
1272         /* Account_id */
1273         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
1274                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1275                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1276                 goto FINISH_OFF;
1277         }
1278
1279         /* Mail ID */
1280         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1281                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1282                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1283                 goto FINISH_OFF;
1284         }
1285
1286         /* Execute API */
1287         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1288                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1289                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1290                 goto FINISH_OFF;
1291         }
1292         
1293         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1294
1295 FINISH_OFF:
1296         if(hAPI)
1297                 emipc_destroy_email_api(hAPI);
1298
1299         emcore_free_mail_data_list(&mail_data, 1);
1300
1301         EM_DEBUG_FUNC_END("err [%d]", err);
1302         return err;
1303
1304 }
1305
1306 EXPORT_API int email_retry_sending_mail( int mail_id, int timeout_in_sec)
1307 {
1308         EM_DEBUG_FUNC_BEGIN();
1309         
1310         int err = EMAIL_ERROR_NONE;
1311
1312
1313         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
1314         if( timeout_in_sec < 0 )  {
1315                 EM_DEBUG_EXCEPTION("Invalid timeout_in_sec");
1316                 err = EMAIL_ERROR_INVALID_PARAM;
1317                 return err;
1318         }
1319         
1320         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_RETRY);
1321
1322         if(!hAPI) {
1323                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1324                 err = EMAIL_ERROR_NULL_VALUE;           
1325                 goto FINISH_OFF;
1326         }
1327
1328         /* Mail ID */
1329         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1330                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1331                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1332                 goto FINISH_OFF;
1333         }
1334
1335         /* timeout */
1336         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(timeout_in_sec), sizeof(int))) {
1337                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1338                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1339                 goto FINISH_OFF;
1340         }
1341         
1342         /* Execute API */
1343         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1344                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1345                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1346                 goto FINISH_OFF;
1347         }
1348         
1349         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1350
1351 FINISH_OFF:
1352         if(hAPI)
1353                 emipc_destroy_email_api(hAPI);
1354
1355         EM_DEBUG_FUNC_END("err [%d]", err);
1356         return err;
1357
1358 }
1359
1360 EXPORT_API int email_get_max_mail_count(int *Count)
1361 {
1362         EM_DEBUG_FUNC_BEGIN();
1363         int err = EMAIL_ERROR_NONE;
1364         EM_IF_NULL_RETURN_VALUE(Count, EMAIL_ERROR_INVALID_PARAM);
1365         *Count = emstorage_get_max_mail_count();
1366         EM_DEBUG_FUNC_END("err [%d]", err);
1367         return err;
1368 }
1369
1370
1371
1372 /* for setting application,disk usage of email in KB */
1373 EXPORT_API int email_get_disk_space_usage(unsigned long *total_size)
1374 {
1375         EM_DEBUG_FUNC_BEGIN("total_size[%p]", total_size);
1376         int err = EMAIL_ERROR_NONE;
1377
1378         EM_IF_NULL_RETURN_VALUE(total_size, EMAIL_ERROR_INVALID_PARAM);
1379
1380         if (!emstorage_mail_get_total_diskspace_usage(total_size,true,&err))  {
1381                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage failed [%d]", err);
1382
1383                 goto FINISH_OFF;
1384         }
1385 FINISH_OFF :    
1386         EM_DEBUG_FUNC_END("err [%d]", err);
1387         return err;
1388 }
1389
1390 EXPORT_API int email_get_address_info_list(int mail_id, email_address_info_list_t** address_info_list)
1391 {
1392         EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1393
1394         int err = EMAIL_ERROR_NONE;
1395
1396         email_address_info_list_t *temp_address_info_list = NULL;
1397
1398         EM_IF_NULL_RETURN_VALUE(address_info_list, EMAIL_ERROR_INVALID_PARAM);
1399         if( mail_id <= 0) {
1400                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1401                 err = EMAIL_ERROR_INVALID_PARAM ;
1402                 return err;
1403         }
1404         
1405         if ( !emcore_get_mail_address_info_list(mail_id, &temp_address_info_list, &err) ) {
1406                 EM_DEBUG_EXCEPTION("emcore_get_mail_address_info_list failed [%d]", err);
1407
1408                 goto FINISH_OFF;
1409         }
1410
1411         if ( address_info_list ) {
1412                 *address_info_list = temp_address_info_list;
1413                 temp_address_info_list = NULL;
1414         }
1415
1416 FINISH_OFF:
1417         if ( temp_address_info_list )
1418                 emstorage_free_address_info_list(&temp_address_info_list);
1419         EM_DEBUG_FUNC_END("err [%d]", err);
1420         return err;
1421 }
1422
1423 EXPORT_API int email_free_address_info_list(email_address_info_list_t **address_info_list)
1424 {
1425         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
1426
1427         int err = EMAIL_ERROR_NONE;
1428
1429         if ( (err = emstorage_free_address_info_list(address_info_list)) != EMAIL_ERROR_NONE ) {
1430                 EM_DEBUG_EXCEPTION("address_info_list[%p] free failed.", address_info_list);
1431         }
1432         EM_DEBUG_FUNC_END("err [%d]", err);
1433         return err;
1434 }
1435
1436 EXPORT_API int email_get_structure(const char*encoded_string, void **struct_var, email_convert_struct_type_e type)
1437 {
1438         EM_DEBUG_FUNC_BEGIN("encoded_string[%s], struct_var[%p], type[%d]", encoded_string, struct_var, type);
1439
1440         int err = EMAIL_ERROR_NONE;
1441         void * temp_struct = NULL;
1442
1443         EM_IF_NULL_RETURN_VALUE(encoded_string, EMAIL_ERROR_INVALID_PARAM);
1444         EM_IF_NULL_RETURN_VALUE(struct_var, EMAIL_ERROR_INVALID_PARAM);
1445
1446         if ( (err = emcore_convert_string_to_structure((char*)encoded_string, &temp_struct, type)) != EMAIL_ERROR_NONE )  {
1447                 EM_DEBUG_EXCEPTION("emcore_convert_string_to_structure failed[%d]", err);
1448                 goto FINISH_OFF;
1449         }
1450
1451         if ( struct_var )
1452                 *struct_var = temp_struct;
1453
1454 FINISH_OFF:
1455         EM_DEBUG_FUNC_END("err [%d]", err);
1456         return err;
1457 }
1458
1459 EXPORT_API int email_get_meeting_request(int mail_id, email_meeting_request_t **meeting_req)
1460 {
1461         EM_DEBUG_FUNC_BEGIN("mail_id[%d],meeting_req[%p]",  mail_id, meeting_req);
1462
1463         int err = EMAIL_ERROR_NONE;
1464
1465         email_meeting_request_t *temp_meeting_req = NULL;
1466
1467         EM_IF_NULL_RETURN_VALUE(meeting_req, EMAIL_ERROR_INVALID_PARAM);
1468         if( mail_id <= 0 ) {
1469                 EM_DEBUG_EXCEPTION(" Invalid Mail Id Param ");
1470                 err = EMAIL_ERROR_INVALID_PARAM ;
1471                 return err;
1472         }
1473         
1474         if ( !emstorage_get_meeting_request(mail_id, &temp_meeting_req, 1, &err) ) {
1475                 EM_DEBUG_EXCEPTION("emstorage_get_meeting_request -- Failed [%d]", err);
1476
1477                 goto FINISH_OFF;
1478         }
1479
1480         if ( meeting_req )
1481                 *meeting_req = temp_meeting_req;
1482
1483 FINISH_OFF:
1484         EM_DEBUG_FUNC_END("err [%d]", err);
1485         return err;
1486 }
1487
1488 EXPORT_API int email_free_meeting_request(email_meeting_request_t** meeting_req, int count)
1489 {
1490         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], count[%d]", meeting_req, count);
1491         if( !meeting_req | !*meeting_req ) {
1492                 EM_DEBUG_EXCEPTION("NULL PARAM");
1493                 return EMAIL_ERROR_INVALID_PARAM;
1494         }
1495
1496         int i = 0;
1497         email_meeting_request_t *cur = *meeting_req;
1498         for ( ; i < count ; i++ )
1499                 emstorage_free_meeting_request(cur + i);
1500
1501         EM_SAFE_FREE(*meeting_req);
1502
1503         EM_DEBUG_FUNC_END();
1504         return EMAIL_ERROR_NONE;
1505 }
1506
1507 EXPORT_API int email_move_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag)
1508 {
1509         EM_DEBUG_FUNC_BEGIN("thread_id[%d], target_mailbox_id[%d], move_always_flag[%d]", thread_id, target_mailbox_id, move_always_flag);
1510         int err = EMAIL_ERROR_NONE;
1511         
1512
1513         EM_IF_NULL_RETURN_VALUE(target_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
1514         
1515         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_MOVE_THREAD_TO_MAILBOX);
1516
1517         if(!hAPI) {
1518                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1519                 err = EMAIL_ERROR_NULL_VALUE;           
1520                 goto FINISH_OFF;
1521         }
1522
1523         /* thread_id */
1524         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))){
1525                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1526                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1527                 goto FINISH_OFF;
1528         }
1529         
1530         /* target mailbox information */
1531         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&target_mailbox_id, sizeof(int))){
1532                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1533                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1534                         goto FINISH_OFF;
1535         }
1536
1537         /* move_always_flag */
1538         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&move_always_flag, sizeof(int))){
1539                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1540                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1541                 goto FINISH_OFF;
1542         }
1543         
1544         /* Execute API */
1545         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1546                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1547                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1548                 goto FINISH_OFF;
1549         }
1550         
1551         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1552
1553 FINISH_OFF:
1554         if(hAPI)
1555                 emipc_destroy_email_api(hAPI);
1556
1557         EM_DEBUG_FUNC_END("err [%d]", err);
1558         return err;
1559 }
1560
1561 EXPORT_API int email_delete_thread(int thread_id, int delete_always_flag)
1562 {
1563         EM_DEBUG_FUNC_BEGIN("thread_id[%d], delete_always_flag[%d]", thread_id, delete_always_flag);
1564         int err = EMAIL_ERROR_NONE;
1565
1566         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_THREAD);
1567
1568         if(!hAPI) {
1569                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1570                 err = EMAIL_ERROR_NULL_VALUE;           
1571                 goto FINISH_OFF;
1572         }
1573
1574         /* thread_id */
1575         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))){
1576                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1577                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1578                 goto FINISH_OFF;
1579         }
1580
1581         /* delete_always_flag */
1582         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&delete_always_flag, sizeof(int))){
1583                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1584                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1585                 goto FINISH_OFF;
1586         }
1587         
1588         /* Execute API */
1589         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1590                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1591                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1592                 goto FINISH_OFF;
1593         }
1594         
1595         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1596
1597 FINISH_OFF:
1598         if(hAPI)
1599                 emipc_destroy_email_api(hAPI);
1600
1601         EM_DEBUG_FUNC_END("err [%d]", err);
1602         return err;
1603 }
1604
1605 EXPORT_API int email_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server)
1606 {
1607         EM_DEBUG_FUNC_BEGIN("thread_id[%d], seen_flag[%d], on_server[%d]", thread_id, seen_flag, on_server);
1608         int err = EMAIL_ERROR_NONE;
1609         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD);
1610
1611         if(!hAPI) {
1612                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1613                 err = EMAIL_ERROR_NULL_VALUE;           
1614                 goto FINISH_OFF;
1615         }
1616
1617         /* thread_id */
1618         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&thread_id, sizeof(int))) {
1619                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1620                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1621                 goto FINISH_OFF;
1622         }
1623
1624         /* seen_flag */
1625         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&seen_flag, sizeof(int))) {
1626                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1627                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1628                 goto FINISH_OFF;
1629         }
1630
1631         /* on_server */
1632         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&on_server, sizeof(int))) {
1633                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1634                 err = EMAIL_ERROR_OUT_OF_MEMORY;                
1635                 goto FINISH_OFF;
1636         }
1637         
1638         /* Execute API */
1639         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1640                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1641                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;   
1642                 goto FINISH_OFF;
1643         }
1644         
1645         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1646
1647 FINISH_OFF:
1648         if(hAPI)
1649                 emipc_destroy_email_api(hAPI);
1650         EM_DEBUG_FUNC_END("err [%d]", err);
1651         return err;
1652 }
1653
1654 EXPORT_API int email_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, unsigned *output_handle)
1655 {
1656         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_on_server[%d], output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
1657         int err = EMAIL_ERROR_NONE;
1658         int return_value = 0;
1659         HIPC_API hAPI = NULL;
1660         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
1661
1662         if( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
1663                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
1664                 goto FINISH_OFF;
1665         }
1666
1667         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1668         ASNotiData as_noti_data;
1669
1670         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
1671
1672         if ( em_get_account_server_type_by_account_id(mailbox_tbl_data->account_id, &account_server_type, true, &err) == false ) {
1673                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1674                 goto FINISH_OFF;
1675         }
1676
1677         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server == true) {
1678                 int as_handle = 0;
1679
1680                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
1681                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
1682                         goto FINISH_OFF;
1683                 }
1684
1685                 /*  noti to active sync */
1686                 as_noti_data.expunge_mails_deleted_flagged.handle        = as_handle;
1687                 as_noti_data.expunge_mails_deleted_flagged.mailbox_id    = input_mailbox_id;
1688                 as_noti_data.expunge_mails_deleted_flagged.on_server     = input_on_server;
1689
1690                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED, &as_noti_data);
1691
1692                 if ( return_value == false ) {
1693                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
1694                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1695                         goto FINISH_OFF;
1696                 }
1697
1698                 if(output_handle)
1699                         *output_handle = as_handle;
1700         }
1701         else
1702         {
1703                 hAPI = emipc_create_email_api(_EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED);
1704
1705                 if(!hAPI) {
1706                         EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1707                         err = EMAIL_ERROR_NULL_VALUE;
1708                         goto FINISH_OFF;
1709                 }
1710
1711                 /* input_mailbox_id */
1712                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
1713                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1714                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1715                         goto FINISH_OFF;
1716                 }
1717
1718                 /* input_on_server */
1719                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_on_server, sizeof(int))) {
1720                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1721                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1722                         goto FINISH_OFF;
1723                 }
1724
1725                 /* Execute API */
1726                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1727                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1728                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1729                         goto FINISH_OFF;
1730                 }
1731
1732                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1733                 if (err == EMAIL_ERROR_NONE) {
1734                         if(output_handle)
1735                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
1736                 }
1737         }
1738
1739 FINISH_OFF:
1740         if(hAPI)
1741                 emipc_destroy_email_api(hAPI);
1742
1743         if(mailbox_tbl_data) {
1744                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
1745         }
1746
1747         EM_DEBUG_FUNC_END("err [%d]", err);
1748         return err;
1749 }