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