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