Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-api / email-api-network.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-network.c
29  * @brief               This file contains the data structures and interfaces of Network related Functionality provided by 
30  *                      email-service . 
31  */
32  
33 #include "email-api.h"
34 #include "string.h"
35 #include "email-convert.h"
36 #include "email-api-mailbox.h"
37 #include "email-types.h"
38 #include "email-utilities.h"
39 #include "email-ipc.h"
40 #include "email-storage.h"
41
42
43
44
45 EXPORT_API int email_send_mail(int mail_id, int *handle)
46 {
47         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, handle);
48         
49         int err = EMAIL_ERROR_NONE;
50         emstorage_mail_tbl_t* mail_table_data = NULL;
51         email_account_server_t account_server_type;
52         HIPC_API hAPI = NULL;
53         ASNotiData as_noti_data;
54
55         if(mail_id <= 0) {
56                 EM_DEBUG_EXCEPTION("mail_id is not valid");
57                 err= EMAIL_ERROR_INVALID_PARAM;
58                 goto FINISH_OFF;
59         }
60
61         if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data) {
62                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
63                 goto FINISH_OFF;
64         }
65
66         if (mail_table_data->account_id <= 0) {
67                 EM_DEBUG_EXCEPTION ("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
68                 emstorage_free_mail(&mail_table_data, 1, NULL);
69                 return EMAIL_ERROR_INVALID_PARAM;
70         }
71
72         EM_DEBUG_LOG("mail_table_data->account_id[%d], mail_table_data->mailbox_name[%s]", mail_table_data->account_id, mail_table_data->mailbox_name);
73
74         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
75
76         /*  check account bind type and branch off  */
77         if ( em_get_account_server_type_by_account_id(mail_table_data->account_id, &account_server_type, false, &err) == false ) {
78                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
79                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
80                 goto FINISH_OFF;
81         }
82
83         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
84                 int as_handle;
85                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
86                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
87                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
88                         goto FINISH_OFF;
89                 }
90                 
91                 /*  noti to active sync */
92                 as_noti_data.send_mail.handle     = as_handle;
93                 as_noti_data.send_mail.account_id = mail_table_data->account_id;
94                 as_noti_data.send_mail.mail_id    = mail_id;
95
96                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL, &as_noti_data) == false) {
97                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
98                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
99                         goto FINISH_OFF;
100                 }
101
102                 if(handle)
103                         *handle = as_handle;
104         }
105         else {
106                 hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL);    
107
108                 if (!hAPI ) {
109                         EM_DEBUG_EXCEPTION ("INVALID PARAM: hAPI NULL ");
110                         emstorage_free_mail(&mail_table_data, 1, NULL);
111                         return EMAIL_ERROR_NULL_VALUE;
112                 }
113
114                 /* mail_id */
115                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))){
116                         EM_DEBUG_EXCEPTION("email_send_mail--Add Param mail_id failed");
117                         emstorage_free_mail(&mail_table_data, 1, NULL);
118                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
119                 }
120
121                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
122                         EM_DEBUG_EXCEPTION("email_send_mail--emipc_execute_proxy_api failed  ");
123                         emstorage_free_mail(&mail_table_data, 1, NULL);
124                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_CRASH);
125                 }
126
127                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
128                 if (err == EMAIL_ERROR_NONE) {
129                         if(handle)
130                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
131                 }
132         }
133
134 FINISH_OFF:
135         emipc_destroy_email_api(hAPI);
136         hAPI = (HIPC_API)NULL;
137
138         emstorage_free_mail(&mail_table_data, 1, NULL);
139
140         EM_DEBUG_FUNC_END("err [%d]", err);  
141         return err;     
142 }
143
144 EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id, int *output_handle)
145 {
146         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_handle[%p]", input_mail_id, output_handle);
147
148         int err = EMAIL_ERROR_NONE;
149         emstorage_mail_tbl_t* mail_table_data = NULL;
150         email_account_server_t account_server_type;
151         HIPC_API hAPI = NULL;
152         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL task_parameter;
153
154         if(input_mail_id <= 0) {
155                 EM_DEBUG_EXCEPTION("mail_id is not valid");
156                 err= EMAIL_ERROR_INVALID_PARAM;
157                 goto FINISH_OFF;
158         }
159
160         if(!emstorage_get_mail_by_id(input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
161                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
162                 goto FINISH_OFF;
163         }
164
165         if (mail_table_data->account_id <= 0) {
166                 EM_DEBUG_EXCEPTION ("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
167                 emstorage_free_mail(&mail_table_data, 1, NULL);
168                 return EMAIL_ERROR_INVALID_PARAM;
169         }
170
171         EM_DEBUG_LOG("mail_table_data->account_id[%d], mail_table_data->mailbox_name[%s]", mail_table_data->account_id, mail_table_data->mailbox_name);
172
173         /*  check account bind type and branch off  */
174         if ( em_get_account_server_type_by_account_id(mail_table_data->account_id, &account_server_type, false, &err) == false ) {
175                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
176                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
177                 goto FINISH_OFF;
178         }
179
180         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
181                 int as_handle;
182                 ASNotiData as_noti_data;
183
184                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
185
186                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
187                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
188                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
189                         goto FINISH_OFF;
190                 }
191
192                 /*  noti to active sync */
193                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.handle     = as_handle;
194                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.mail_id    = input_mail_id;
195
196                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL, &as_noti_data) == false) {
197                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
198                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
199                         goto FINISH_OFF;
200                 }
201
202                 if(output_handle)
203                         *output_handle = as_handle;
204         }
205         else {
206                 task_parameter.mail_id        = input_mail_id;
207
208                 if((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, &task_parameter)) != EMAIL_ERROR_NONE) {
209                         EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
210                         goto FINISH_OFF;
211                 }
212         }
213
214 FINISH_OFF:
215         emipc_destroy_email_api(hAPI);
216         hAPI = (HIPC_API)NULL;
217
218         emstorage_free_mail(&mail_table_data, 1, NULL);
219
220         EM_DEBUG_FUNC_END("err [%d]", err);
221         return err;
222 }
223
224 EXPORT_API int email_send_saved(int account_id, int *handle)
225 {
226         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p]", account_id, handle);
227         
228         char* pOptionStream = NULL;
229         int err = EMAIL_ERROR_NONE;
230         
231         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
232         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
233         
234         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_SAVED);
235
236         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
237         
238         /* Account ID */
239         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(account_id), sizeof(int))) {
240                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed");
241                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
242         }
243
244         /* Execute API */
245         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
246                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
247                 EM_SAFE_FREE(pOptionStream);
248                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
249         }
250
251         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
252         emipc_destroy_email_api(hAPI);
253         
254         hAPI = NULL;
255         EM_SAFE_FREE(pOptionStream);
256
257         EM_DEBUG_FUNC_END("err [%d]", err);  
258         return err;
259 }
260
261 EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int *handle)
262 {
263         EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mailbox_id[%d] handle[%p]", input_account_id, input_mailbox_id, handle);
264         int err = EMAIL_ERROR_NONE;     
265         /* int total_count = 0; */
266         
267         EM_IF_ACCOUNT_ID_NULL(input_account_id, EMAIL_ERROR_INVALID_PARAM);
268
269         email_account_server_t account_server_type;
270         HIPC_API hAPI = NULL;
271         ASNotiData as_noti_data;
272         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
273
274         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
275         if ( em_get_account_server_type_by_account_id(input_account_id, &account_server_type, true, &err) == false ) {
276                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
277                 goto FINISH_OFF;
278         }
279
280         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
281                 int as_handle;
282                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
283                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
284                         goto FINISH_OFF;
285                 }
286                 
287                 /*  noti to active sync */
288                 as_noti_data.sync_header.handle = as_handle;
289                 as_noti_data.sync_header.account_id = input_account_id;
290                 /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
291                 as_noti_data.sync_header.mailbox_id = input_mailbox_id;
292
293                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
294                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
295                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
296                         goto FINISH_OFF;
297                 }
298
299                 if(handle)
300                         *handle = as_handle;
301
302         }
303         else {
304                 hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);  
305
306                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
307
308                 /* input_account_id */
309                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_account_id, sizeof(int))) {
310                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
311                         err = EMAIL_ERROR_OUT_OF_MEMORY;
312                         goto FINISH_OFF;
313                 }
314
315                 /* input_mailbox_id */
316                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
317                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
318                         err = EMAIL_ERROR_OUT_OF_MEMORY;
319                         goto FINISH_OFF;
320                 }
321
322                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
323                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
324                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
325                 }
326                         
327                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
328
329                 if (err != EMAIL_ERROR_NONE)
330                         goto FINISH_OFF;
331
332                 if(handle)
333                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);      
334         }
335
336 FINISH_OFF:
337         emipc_destroy_email_api(hAPI);
338         hAPI = NULL;
339
340         EM_DEBUG_FUNC_END("err [%d]", err);  
341         return err;
342 }
343
344
345 EXPORT_API int email_sync_header_for_all_account(int *handle)
346 {
347         EM_DEBUG_FUNC_BEGIN("handle[%p]", handle);
348         char* mailbox_stream = NULL;
349         int err = EMAIL_ERROR_NONE;     
350         HIPC_API hAPI = NULL;
351         int return_handle;
352         ASNotiData as_noti_data;
353         int i, account_count = 0;
354         emstorage_account_tbl_t *account_tbl_array = NULL;
355         int as_err;
356         int input_account_id = ALL_ACCOUNT;
357         int input_mailbox_id = 0; /* all case */
358
359         hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);  
360
361         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
362
363         /* input_account_id */
364         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_account_id, sizeof(int))) {
365                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
366                 err = EMAIL_ERROR_OUT_OF_MEMORY;
367                 goto FINISH_OFF;
368         }
369
370         /* input_account_id */
371         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
372                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
373                 err = EMAIL_ERROR_OUT_OF_MEMORY;
374                 goto FINISH_OFF;
375         }
376
377         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
378                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
379                 EM_SAFE_FREE(mailbox_stream);
380                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
381         }
382                 
383          emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
384
385          if (err != EMAIL_ERROR_NONE)
386                  goto FINISH_OFF;
387
388            emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &return_handle);
389
390         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
391
392         /*  Get all accounts for sending notification to active sync engine. */
393         if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &as_err)) {
394                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ]  ", as_err);
395
396                 goto FINISH_OFF;
397         }
398
399         for(i = 0; i < account_count; i++) {
400                 if ( account_tbl_array[i].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
401                         /*  use returned handle value for a active sync handle */
402                         /* int as_handle; */
403                         /*
404                         if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
405                                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
406                                 goto FINISH_OFF;
407                         }
408                         */
409                         
410                         /*  noti to active sync */
411                         as_noti_data.sync_header.handle = return_handle;
412                         as_noti_data.sync_header.account_id = account_tbl_array[i].account_id;
413                         /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
414                         as_noti_data.sync_header.mailbox_id = 0;
415
416                         if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
417                                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
418                                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
419                                 goto FINISH_OFF;
420                         }
421                 }
422         }
423          
424          if(handle)
425                 *handle = return_handle;
426
427 FINISH_OFF:
428
429         emipc_destroy_email_api(hAPI);
430         hAPI = NULL;
431
432         if ( account_tbl_array )
433                 emstorage_free_account(&account_tbl_array, account_count, NULL);
434         
435         EM_DEBUG_FUNC_END("err [%d]", err);  
436         return err;
437 }
438
439 EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle)
440 {
441         EM_DEBUG_FUNC_BEGIN("mail_id[%d],with_attachment[%d]", mail_id, with_attachment);
442         int err = EMAIL_ERROR_NONE;
443         emstorage_mail_tbl_t* mail_table_data = NULL;
444         int account_id = 0;
445         email_account_server_t account_server_type;
446         HIPC_API hAPI = NULL;
447         ASNotiData as_noti_data;
448         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
449
450         if(mail_id <= 0) {
451                 EM_DEBUG_EXCEPTION("mail_id is not valid");
452                 err= EMAIL_ERROR_INVALID_PARAM;
453                 goto FINISH_OFF;
454         }
455
456         if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
457                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
458                 goto FINISH_OFF;
459         }
460
461         if (mail_table_data->account_id <= 0) {
462                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
463                 goto FINISH_OFF;
464         }
465
466         account_id = mail_table_data->account_id;
467                 
468         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
469         if ( em_get_account_server_type_by_account_id(account_id, &account_server_type, true, &err) == false ) {
470                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
471                 goto FINISH_OFF;
472         }
473
474         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
475                 int as_handle;
476                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
477                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
478                         goto FINISH_OFF;
479                 }
480                 
481                 /*  noti to active sync */
482                 as_noti_data.download_body.handle = as_handle;
483                 as_noti_data.download_body.account_id = account_id;
484                 as_noti_data.download_body.mail_id = mail_id;
485                 as_noti_data.download_body.with_attachment = with_attachment;
486
487                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_BODY, &as_noti_data) == false) {
488                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
489                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
490                         goto FINISH_OFF;
491                 }
492
493                 if(handle)
494                         *handle = as_handle;
495         } else {
496                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_BODY);
497
498                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
499
500                 /* Account Id */
501                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
502                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
503                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
504                 }
505                 /* Mail Id */
506                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
507                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
508                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
509                 }
510
511                 /* with_attachment */
512                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&with_attachment, sizeof(int))) {
513                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
514                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
515                 }
516                 
517                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
518                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
519                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
520                 }
521                         
522                  emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
523                  if (err != EMAIL_ERROR_NONE)           
524                          goto FINISH_OFF;
525                  
526                  if(handle)     
527                  {
528                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
529                         EM_DEBUG_LOG("RETURN VALUE : %d  handle %d", err, *handle);
530
531                  }
532         }
533
534 FINISH_OFF:
535         emipc_destroy_email_api(hAPI);
536         hAPI = NULL;
537
538         if(mail_table_data) {
539                 emstorage_free_mail(&mail_table_data, 1, &err);
540         }
541
542         EM_DEBUG_FUNC_END("err [%d]", err);  
543         return err;
544
545 }
546
547
548 /* API - Downloads the Email Attachment Information [ INTERNAL ] */
549
550 EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
551 {
552         EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], handle[%p]", mail_id, nth, handle);
553         char* mailbox_stream = NULL;
554         int err = EMAIL_ERROR_NONE;
555         emstorage_mail_tbl_t* mail_table_data = NULL;
556         int account_id = 0;
557         email_account_server_t account_server_type;
558         HIPC_API hAPI = NULL;
559         ASNotiData as_noti_data;
560         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
561
562         if(mail_id <= 0) {
563                 EM_DEBUG_EXCEPTION("mail_id is not valid");
564                 err= EMAIL_ERROR_INVALID_PARAM;
565                 goto FINISH_OFF;
566         }
567
568         if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err) || !mail_table_data ) {
569                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
570                 goto FINISH_OFF;
571         }
572
573         if (mail_table_data->account_id <= 0) {
574                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
575                 goto FINISH_OFF;
576         }
577
578         account_id = mail_table_data->account_id;
579         
580         if ( em_get_account_server_type_by_account_id(account_id, &account_server_type, true, &err) == false ) {
581                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
582                 goto FINISH_OFF;
583         }
584
585         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
586                 int as_handle;
587                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
588                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
589                         goto FINISH_OFF;
590                 }
591                 
592                 /*  noti to active sync */
593                 as_noti_data.download_attachment.handle = as_handle;
594                 as_noti_data.download_attachment.account_id = account_id;
595                 as_noti_data.download_attachment.mail_id = mail_id;
596                 as_noti_data.download_attachment.attachment_order = nth;
597                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT, &as_noti_data) == false) {
598                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
599                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
600                         goto FINISH_OFF;
601                 }
602
603                 if(handle)
604                         *handle = as_handle;
605         } else {
606                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_ATTACHMENT);
607
608                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
609
610                 /* Account Id */
611                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
612                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
613                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
614                 }
615
616                 /* Mail ID */
617                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
618                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
619                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
620                 }
621
622                 /* nth */
623                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(nth), sizeof(int))) {
624                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
625                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
626                 }
627
628                 /* Execute API */
629                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
630                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
631                         EM_SAFE_FREE(mailbox_stream);
632                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
633                 }
634
635                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
636                 if (err != EMAIL_ERROR_NONE)            
637                         goto FINISH_OFF;
638                  
639                 if(handle)
640                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
641                 
642         }
643         
644 FINISH_OFF:
645         emipc_destroy_email_api(hAPI);
646         hAPI = NULL;
647
648         if(mail_table_data) {
649                 emstorage_free_mail(&mail_table_data, 1, &err);
650         }
651
652         EM_DEBUG_FUNC_END("err [%d]", err);  
653         return err;
654         
655 }
656
657
658 EXPORT_API int email_cancel_job(int input_account_id, int input_handle, email_cancelation_type input_cancel_type)
659 {
660         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_handle [%d], input_cancel_type [%d]", input_account_id, input_handle, input_cancel_type);
661         int err = EMAIL_ERROR_NONE;
662         email_account_server_t account_server_type;
663         HIPC_API hAPI = NULL;
664         ASNotiData as_noti_data;
665         emstorage_account_tbl_t *account_list = NULL;
666         int i, account_count = 0;
667
668         if(input_account_id < 0)
669                 return EMAIL_ERROR_INVALID_PARAM;
670
671         if ( input_account_id == ALL_ACCOUNT ) {
672                 /*  this means that job is executed with all account */
673                 /*  Get all accounts for sending notification to active sync engine. */
674                 if (!emstorage_get_account_list(&account_count, &account_list , true, false, &err)) {
675                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
676                         goto FINISH_OFF;
677                 }
678
679                 for(i = 0; i < account_count; i++) {
680                         if ( em_get_account_server_type_by_account_id(account_list[i].account_id, &account_server_type, true, &err) == false ) {
681                                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
682                                 goto FINISH_OFF;
683                         }
684
685                         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
686                                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
687                                 as_noti_data.cancel_job.account_id  = account_list[i].account_id;
688                                 as_noti_data.cancel_job.handle      = input_handle;
689                                 as_noti_data.cancel_job.cancel_type = input_cancel_type;
690
691
692                                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
693                                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
694                                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
695                                         goto FINISH_OFF;
696                                 }
697                         }
698                 }
699
700                 /*  request canceling to stub */
701                 hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
702
703                 if (!hAPI) {
704                         EM_DEBUG_EXCEPTION ("INVALID PARAM: hAPI NULL ");
705                         err = EMAIL_ERROR_NULL_VALUE;
706                         goto FINISH_OFF;
707                 }
708
709                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_account_id, sizeof(int))) {         /*  input_account_id == 0 */
710                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
711                         err = EMAIL_ERROR_NULL_VALUE;
712                         goto FINISH_OFF;
713                 }
714
715                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_handle, sizeof(int))) {
716                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
717                         err = EMAIL_ERROR_NULL_VALUE;
718                         goto FINISH_OFF;
719                 }
720
721                 /* Execute API */
722                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
723                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
724                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
725                         goto FINISH_OFF;
726                 }
727
728                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
729                 emipc_destroy_email_api(hAPI);
730                 hAPI = NULL;
731         }
732         else {
733                 if ( em_get_account_server_type_by_account_id(input_account_id, &account_server_type, true, &err) == false ) {
734                         EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
735                         goto FINISH_OFF;
736                 }
737
738                 if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
739                         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
740                         as_noti_data.cancel_job.account_id = input_account_id;
741                         as_noti_data.cancel_job.handle = input_handle;
742
743                         if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
744                                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
745                                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
746                                 goto FINISH_OFF;
747                         }
748                 } else {
749                         hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
750
751                         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
752
753                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_account_id, sizeof(int))) {
754                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
755                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
756                         }
757
758                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_handle, sizeof(int))) {
759                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
760                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
761                         }
762
763                         /* Execute API */
764                         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
765                                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
766                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
767                         }
768                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
769                         emipc_destroy_email_api(hAPI);
770                         hAPI = NULL;
771                 }
772         }
773
774 FINISH_OFF:
775         emipc_destroy_email_api(hAPI);
776         hAPI = NULL;
777         if (account_list)
778                 emstorage_free_account(&account_list, account_count, NULL);
779                 
780         EM_DEBUG_FUNC_END("err [%d]", err);  
781         return err;
782 }
783
784
785
786 EXPORT_API int email_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t * status)
787 {
788         EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d], status[%p]", action, account_id, mail_id, status);
789         
790         int err = EMAIL_ERROR_NONE;
791
792         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_NULL_VALUE);
793         
794         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_PENDING_JOB);
795
796         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
797
798         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &action, sizeof(int))) {
799                 EM_DEBUG_EXCEPTION("emipc_add_parameter action failed ");
800                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
801         }
802
803         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
804                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed ");
805                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
806         }
807
808         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &mail_id, sizeof(int))) {
809                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed ");
810                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
811         }
812
813         /* Execute API */
814         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
815                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed  ");
816                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
817         }
818
819         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
820
821         if(status) {
822                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), status);
823                 EM_DEBUG_LOG("status : %d ", *status);
824         }
825         
826         emipc_destroy_email_api(hAPI);
827         hAPI = NULL;
828
829         EM_DEBUG_FUNC_END("err [%d]", err);  
830         return err;
831
832 }
833
834
835
836 EXPORT_API int email_get_network_status(int* on_sending, int* on_receiving)
837 {
838         EM_DEBUG_FUNC_BEGIN("on_sending[%p], on_receiving[%p]", on_sending, on_receiving);
839         int err = EMAIL_ERROR_NONE;
840         
841         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_NETWORK_GET_STATUS);
842
843         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
844
845         /* Execute API */
846         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
847                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
848                 emipc_destroy_email_api(hAPI);
849                 hAPI = NULL;
850                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE ;
851                 EM_DEBUG_FUNC_END("err [%d]", err); return err;
852         }
853
854         if(on_sending)
855                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), on_sending );
856         if(on_receiving)        
857                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), on_receiving);
858
859         emipc_destroy_email_api(hAPI);
860         hAPI = NULL;
861         EM_DEBUG_FUNC_END("err [%d]", err);  
862         return err;
863 }
864
865 EXPORT_API int email_sync_imap_mailbox_list(int account_id, int *handle)
866 {
867         EM_DEBUG_FUNC_BEGIN();
868
869         int err = EMAIL_ERROR_NONE;
870
871         if(account_id <= 0) {
872                 EM_DEBUG_LOG("invalid parameters");
873                 return EMAIL_ERROR_INVALID_PARAM;
874         }
875
876         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_IMAP_MAILBOX_LIST);       
877
878         /* account_id */
879         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
880                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
881                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
882         }
883
884         if(!emipc_execute_proxy_api(hAPI))  {
885                 EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
886                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
887         }
888
889         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
890         if(handle)
891         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
892
893         emipc_destroy_email_api(hAPI);
894         hAPI = NULL;
895
896         EM_DEBUG_FUNC_END("err [%d]", err);  
897         return err;
898 }
899
900 EXPORT_API int email_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter_list, int input_search_filter_count, int *output_handle)
901 {
902         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_search_filter_list [%p], input_search_filter_count [%d], output_handle [%p]", input_account_id, input_mailbox_id, input_search_filter_list, input_search_filter_count, output_handle);
903
904         int       err = EMAIL_ERROR_NONE;
905         int       return_value = 0;
906         int       stream_size_for_search_filter_list = 0;
907         char     *stream_for_search_filter_list = NULL;
908         HIPC_API  hAPI = NULL;
909         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
910         ASNotiData as_noti_data;
911
912         EM_IF_NULL_RETURN_VALUE(input_account_id,         EMAIL_ERROR_INVALID_PARAM);
913         EM_IF_NULL_RETURN_VALUE(input_mailbox_id,         EMAIL_ERROR_INVALID_PARAM);
914         EM_IF_NULL_RETURN_VALUE(input_search_filter_list, EMAIL_ERROR_INVALID_PARAM);
915
916         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
917
918         if ( em_get_account_server_type_by_account_id(input_account_id, &account_server_type, true, &err) == false ) {
919                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
920                 goto FINISH_OFF;
921         }
922
923         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
924                 int as_handle = 0;
925
926                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
927                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
928                         goto FINISH_OFF;
929                 }
930
931                 /*  noti to active sync */
932                 as_noti_data.search_mail_on_server.handle              = as_handle;
933                 as_noti_data.search_mail_on_server.account_id          = input_account_id;
934                 as_noti_data.search_mail_on_server.mailbox_id          = input_mailbox_id;
935                 as_noti_data.search_mail_on_server.search_filter_list  = input_search_filter_list;
936                 as_noti_data.search_mail_on_server.search_filter_count = input_search_filter_count;
937
938                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER, &as_noti_data);
939
940                 if ( return_value == false ) {
941                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
942                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
943                         goto FINISH_OFF;
944                 }
945
946                 if(output_handle)
947                         *output_handle = as_handle;
948         }
949         else
950         {
951                 hAPI = emipc_create_email_api(_EMAIL_API_SEARCH_MAIL_ON_SERVER);
952
953                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
954
955                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
956                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
957                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
958                         goto FINISH_OFF;
959                 }
960
961                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_mailbox_id, sizeof(int))){
962                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
963                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
964                         goto FINISH_OFF;
965                 }
966
967                 stream_for_search_filter_list = em_convert_search_filter_to_byte_stream(input_search_filter_list, input_search_filter_count, &stream_size_for_search_filter_list);
968
969                 EM_PROXY_IF_NULL_RETURN_VALUE(stream_for_search_filter_list, hAPI, EMAIL_ERROR_NULL_VALUE);
970
971                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, stream_for_search_filter_list, stream_size_for_search_filter_list)) {
972                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
973                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
974                         goto FINISH_OFF;
975                 }
976
977                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
978                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
979                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
980                 }
981
982                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
983
984                 if (err != EMAIL_ERROR_NONE) {
985                         EM_DEBUG_EXCEPTION("_EMAIL_API_SEARCH_MAIL_ON_SERVER failed [%d]", err);
986                         goto FINISH_OFF;
987                 }
988
989                 if(output_handle)
990                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
991         }
992
993 FINISH_OFF:
994         if(hAPI) {
995                 emipc_destroy_email_api(hAPI);
996                 hAPI = NULL;
997         }
998
999         EM_DEBUG_FUNC_END("err [%d]", err);
1000         return err;
1001 }
1002
1003 EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
1004 {
1005         EM_DEBUG_FUNC_BEGIN("input_account_id [%d]", input_account_id);
1006
1007         int       err = EMAIL_ERROR_NONE;
1008         int       return_value = 0;
1009         HIPC_API  hAPI = NULL;
1010         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1011         ASNotiData as_noti_data;
1012
1013         EM_IF_NULL_RETURN_VALUE(input_account_id,         EMAIL_ERROR_INVALID_PARAM);
1014
1015         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
1016
1017         if ( em_get_account_server_type_by_account_id(input_account_id, &account_server_type, true, &err) == false ) {
1018                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1019                 goto FINISH_OFF;
1020         }
1021
1022         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
1023                 int as_handle = 0;
1024
1025                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
1026                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
1027                         goto FINISH_OFF;
1028                 }
1029
1030                 /*  noti to active sync */
1031                 as_noti_data.clear_result_of_search_mail_on_server.handle              = as_handle;
1032                 as_noti_data.clear_result_of_search_mail_on_server.account_id          = input_account_id;
1033
1034                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER, &as_noti_data);
1035
1036                 if ( return_value == false ) {
1037                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
1038                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1039                         goto FINISH_OFF;
1040                 }
1041         }
1042         else {
1043                 hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER);
1044
1045                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
1046
1047                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
1048                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1049                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1050                         goto FINISH_OFF;
1051                 }
1052
1053                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1054                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1055                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
1056                 }
1057
1058                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1059
1060                 if (err != EMAIL_ERROR_NONE) {
1061                         EM_DEBUG_EXCEPTION("_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER failed [%d]", err);
1062                         goto FINISH_OFF;
1063                 }
1064         }
1065
1066 FINISH_OFF:
1067         if(hAPI) {
1068                 emipc_destroy_email_api(hAPI);
1069                 hAPI = NULL;
1070         }
1071
1072         EM_DEBUG_FUNC_END("err [%d]", err);
1073         return err;
1074 }