add excluding coverage
[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 "string.h"
34 #include "email-convert.h"
35 #include "email-api-mailbox.h"
36 #include "email-types.h"
37 #include "email-core-signal.h"
38 #include "email-utilities.h"
39 #include "email-ipc.h"
40 #include "email-storage.h"
41
42
43 EXPORT_API int email_send_mail(int mail_id, int *handle)
44 {
45         EM_DEBUG_API_BEGIN("mail_id[%d] handle[%p]", mail_id, handle);
46
47         int err = EMAIL_ERROR_NONE;
48     char *multi_user_name = NULL;
49         emstorage_mail_tbl_t* mail_table_data = NULL;
50         email_account_server_t account_server_type;
51         HIPC_API hAPI = NULL;
52         ASNotiData as_noti_data;
53
54     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
55         /* LCOV_EXCL_START */
56         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
57         goto FINISH_OFF;
58         /* LCOV_EXCL_STOP */
59     }
60
61         if (mail_id <= 0) {
62                 EM_DEBUG_EXCEPTION("mail_id is not valid");
63                 err = EMAIL_ERROR_INVALID_PARAM;
64                 goto FINISH_OFF;
65         }
66
67         if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data) {
68                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
69                 goto FINISH_OFF;
70         }
71
72         if (mail_table_data->account_id <= 0) {
73                 /* LCOV_EXCL_START */
74                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
75                 emstorage_free_mail(&mail_table_data, 1, NULL);
76                 return EMAIL_ERROR_INVALID_PARAM;
77                 /* LCOV_EXCL_STOP */
78         }
79
80         EM_DEBUG_LOG("mail_table_data->account_id[%d], mail_table_data->mailbox_id[%d]", mail_table_data->account_id, mail_table_data->mailbox_id);
81
82         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
83
84         /*  check account bind type and branch off  */
85         if (em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->account_id, &account_server_type, false, &err) == false) {
86                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
87                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
88                 goto FINISH_OFF;
89         }
90
91         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
92                 int as_handle;
93                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
94                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
95                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
96                         goto FINISH_OFF;
97                 }
98
99                 /*  noti to active sync */
100                 as_noti_data.send_mail.handle          = as_handle;
101                 as_noti_data.send_mail.account_id      = mail_table_data->account_id;
102                 as_noti_data.send_mail.mail_id         = mail_id;
103         as_noti_data.send_mail.multi_user_name = multi_user_name;
104
105                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL, &as_noti_data) == false) {
106                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
107                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
108                         goto FINISH_OFF;
109                 }
110
111                 if (handle)
112                         *handle = as_handle;
113         } else {
114                 hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL);
115
116                 if (!hAPI) {
117                         /* LCOV_EXCL_START */
118                         EM_DEBUG_EXCEPTION("INVALID PARAM: hAPI NULL ");
119                         emstorage_free_mail(&mail_table_data, 1, NULL);
120                         return EMAIL_ERROR_NULL_VALUE;
121                         /* LCOV_EXCL_STOP */
122                 }
123
124                 /* mail_id */
125                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
126                         /* LCOV_EXCL_START */
127                         EM_DEBUG_EXCEPTION("email_send_mail--Add Param mail_id failed");
128                         emstorage_free_mail(&mail_table_data, 1, NULL);
129                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
130                         /* LCOV_EXCL_STOP */
131                 }
132
133                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
134                         /* LCOV_EXCL_START */
135                         EM_DEBUG_EXCEPTION("email_send_mail--emipc_execute_proxy_api failed  ");
136                         emstorage_free_mail(&mail_table_data, 1, NULL);
137                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_CRASH);
138                         /* LCOV_EXCL_STOP */
139                 }
140
141                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
142                 if (err == EMAIL_ERROR_NONE) {
143                         if (handle)
144                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
145                 }
146         }
147
148 FINISH_OFF:
149         emipc_destroy_email_api(hAPI);
150         hAPI = (HIPC_API)NULL;
151
152         emstorage_free_mail(&mail_table_data, 1, NULL);
153
154     EM_SAFE_FREE(multi_user_name);
155
156         EM_DEBUG_API_END("err[%d]", err);
157         return err;
158 }
159
160 EXPORT_API int email_send_mail_with_downloading_attachment_of_original_mail(int input_mail_id, int *output_handle)
161 {
162         EM_DEBUG_API_BEGIN("input_mail_id[%d] output_handle[%p]", input_mail_id, output_handle);
163
164         int err = EMAIL_ERROR_NONE;
165     char *multi_user_name = NULL;
166         emstorage_mail_tbl_t* mail_table_data = NULL;
167         email_account_server_t account_server_type;
168         HIPC_API hAPI = NULL;
169         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL task_parameter;
170
171     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
172         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
173         goto FINISH_OFF;
174     }
175
176         if (input_mail_id <= 0) {
177                 EM_DEBUG_EXCEPTION("mail_id is not valid");
178                 err = EMAIL_ERROR_INVALID_PARAM;
179                 goto FINISH_OFF;
180         }
181
182         if (!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
183                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
184                 goto FINISH_OFF;
185         }
186
187         if (mail_table_data->account_id <= 0) {
188                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
189                 emstorage_free_mail(&mail_table_data, 1, NULL);
190                 return EMAIL_ERROR_INVALID_PARAM;
191         }
192
193         EM_DEBUG_LOG("mail_table_data->account_id[%d], mail_table_data->mailbox_id[%d]", mail_table_data->account_id, mail_table_data->mailbox_id);
194
195         /*  check account bind type and branch off  */
196         if (em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->account_id, &account_server_type, false, &err) == false) {
197                 /* LCOV_EXCL_START */
198                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
199                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
200                 goto FINISH_OFF;
201                 /* LCOV_EXCL_STOP */
202         }
203
204         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
205                 int as_handle;
206                 ASNotiData as_noti_data;
207
208                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
209
210                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
211                         /* LCOV_EXCL_START */
212                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
213                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
214                         goto FINISH_OFF;
215                         /* LCOV_EXCL_STOP */
216                 }
217
218                 /*  noti to active sync */
219                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.handle          = as_handle;
220                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.mail_id         = input_mail_id;
221                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.account_id      = mail_table_data->account_id;
222                 as_noti_data.send_mail_with_downloading_attachment_of_original_mail.multi_user_name = multi_user_name;
223
224                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL, &as_noti_data) == false) {
225                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
226                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
227                         goto FINISH_OFF;
228                 }
229
230                 if (output_handle)
231                         *output_handle = as_handle;
232         } else {
233                 task_parameter.mail_id        = input_mail_id;
234
235                 if ((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, &task_parameter)) != EMAIL_ERROR_NONE) {
236                         EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
237                         goto FINISH_OFF;
238                 }
239         }
240
241 FINISH_OFF:
242         emipc_destroy_email_api(hAPI);
243         hAPI = (HIPC_API)NULL;
244
245         emstorage_free_mail(&mail_table_data, 1, NULL);
246
247     EM_SAFE_FREE(multi_user_name);
248
249         EM_DEBUG_API_END("err[%d]", err);
250         return err;
251 }
252
253 EXPORT_API int email_schedule_sending_mail(int input_mail_id, time_t input_scheduled_time)
254 {
255         EM_DEBUG_API_BEGIN("mail_id[%d] input_time[%d]", input_mail_id, input_scheduled_time);
256
257         int err = EMAIL_ERROR_NONE;
258     char *multi_user_name = NULL;
259         emstorage_mail_tbl_t* mail_table_data = NULL;
260         email_account_server_t account_server_type;
261         HIPC_API hAPI = NULL;
262         task_parameter_EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL task_parameter;
263
264     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
265         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
266         goto FINISH_OFF;
267     }
268
269         if (input_mail_id <= 0) {
270                 EM_DEBUG_EXCEPTION("mail_id is not valid");
271                 err = EMAIL_ERROR_INVALID_PARAM;
272                 goto FINISH_OFF;
273         }
274
275         if (!emstorage_get_mail_by_id(multi_user_name, input_mail_id, &mail_table_data, true, &err) || !mail_table_data) {
276                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
277                 goto FINISH_OFF;
278         }
279
280         /*  check account bind type and branch off  */
281         if (em_get_account_server_type_by_account_id(multi_user_name, mail_table_data->account_id, &account_server_type, false, &err) == false) {
282                 /* LCOV_EXCL_START */
283                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
284                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
285                 goto FINISH_OFF;
286                 /* LCOV_EXCL_STOP */
287         }
288
289         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
290                 int as_handle;
291                 ASNotiData as_noti_data;
292
293                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
294                 /* LCOV_EXCL_START */
295                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
296                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
297                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
298                         goto FINISH_OFF;
299                 }
300                 /* LCOV_EXCL_STOP */
301                 /*  noti to active sync */
302                 as_noti_data.schedule_sending_mail.handle          = as_handle;
303                 as_noti_data.schedule_sending_mail.account_id      = mail_table_data->account_id;
304                 as_noti_data.schedule_sending_mail.mail_id         = input_mail_id;
305                 as_noti_data.schedule_sending_mail.scheduled_time  = input_scheduled_time;
306         as_noti_data.schedule_sending_mail.multi_user_name = multi_user_name;
307
308                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL, &as_noti_data) == false) {
309                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
310                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
311                         goto FINISH_OFF;
312                 }
313         } else {
314                 task_parameter.mail_id         = input_mail_id;
315                 task_parameter.scheduled_time  = input_scheduled_time;
316
317                 if ((err = emipc_execute_proxy_task(EMAIL_SYNC_TASK_SCHEDULE_SENDING_MAIL, &task_parameter)) != EMAIL_ERROR_NONE) {
318                         EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
319                         goto FINISH_OFF;
320                 }
321         }
322
323 FINISH_OFF:
324         emipc_destroy_email_api(hAPI);
325         hAPI = (HIPC_API)NULL;
326
327         emstorage_free_mail(&mail_table_data, 1, NULL);
328
329     EM_SAFE_FREE(multi_user_name);
330
331         EM_DEBUG_API_END("err[%d]", err);
332         return err;
333 }
334
335 EXPORT_API int email_send_saved(int account_id, int *handle)
336 {
337         EM_DEBUG_API_BEGIN("account_id[%d] handle[%p]", account_id, handle);
338
339         char* pOptionStream = NULL;
340         int err = EMAIL_ERROR_NONE;
341
342         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
343         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
344
345         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_SAVED);
346
347         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
348
349         /* Account ID */
350         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(account_id), sizeof(int))) {
351                 /* LCOV_EXCL_START */
352                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed");
353                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
354                 /* LCOV_EXCL_STOP */
355         }
356
357         /* Execute API */
358         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
359                 /* LCOV_EXCL_START */
360                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
361                 EM_SAFE_FREE(pOptionStream);
362                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
363                 /* LCOV_EXCL_STOP */
364         }
365
366         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
367         emipc_destroy_email_api(hAPI);
368
369         hAPI = NULL;
370         EM_SAFE_FREE(pOptionStream);
371
372         EM_DEBUG_API_END("err[%d]", err);
373         return err;
374 }
375
376 EXPORT_API int email_sync_header(int input_account_id, int input_mailbox_id, int *handle)
377 {
378         EM_DEBUG_API_BEGIN("input_account_id[%d] input_mailbox_id[%d] handle[%p]", input_account_id, input_mailbox_id, handle);
379         int err = EMAIL_ERROR_NONE;
380     char *multi_user_name = NULL;
381         /* int total_count = 0; */
382
383         EM_IF_ACCOUNT_ID_NULL(input_account_id, EMAIL_ERROR_INVALID_PARAM);
384
385         email_account_server_t account_server_type;
386         HIPC_API hAPI = NULL;
387         ASNotiData as_noti_data;
388         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
389
390     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
391         /* LCOV_EXCL_START */
392         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
393         goto FINISH_OFF;
394         /* LCOV_EXCL_STOP */
395     }
396
397         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
398         if (em_get_account_server_type_by_account_id(multi_user_name, input_account_id, &account_server_type, true, &err) == false) {
399                 /* LCOV_EXCL_START */
400                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
401                 goto FINISH_OFF;
402                 /* LCOV_EXCL_STOP */
403         }
404
405         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
406                 int as_handle;
407                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
408                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
409                         goto FINISH_OFF;
410                 }
411
412                 /*  noti to active sync */
413                 as_noti_data.sync_header.handle          = as_handle;
414                 as_noti_data.sync_header.account_id      = input_account_id;
415                 /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
416                 as_noti_data.sync_header.mailbox_id      = input_mailbox_id;
417         as_noti_data.sync_header.multi_user_name = multi_user_name;
418
419                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
420                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
421                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
422                         goto FINISH_OFF;
423                 }
424
425                 if (handle)
426                         *handle = as_handle;
427
428         } else {
429                 hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);
430
431                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
432
433                 /* input_account_id */
434                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_account_id, sizeof(int))) {
435                         /* LCOV_EXCL_START */
436                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
437                         err = EMAIL_ERROR_OUT_OF_MEMORY;
438                         goto FINISH_OFF;
439                         /* LCOV_EXCL_STOP */
440                 }
441
442                 /* input_mailbox_id */
443                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
444                         /* LCOV_EXCL_START */
445                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
446                         err = EMAIL_ERROR_OUT_OF_MEMORY;
447                         goto FINISH_OFF;
448                         /* LCOV_EXCL_STOP */
449                 }
450
451                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
452                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
453                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
454                 }
455
456                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
457
458                 if (err != EMAIL_ERROR_NONE)
459                         goto FINISH_OFF;
460
461                 if (handle)
462                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
463         }
464
465 FINISH_OFF:
466
467         emipc_destroy_email_api(hAPI);
468         hAPI = NULL;
469
470     EM_SAFE_FREE(multi_user_name);
471
472         EM_DEBUG_API_END("err[%d]", err);
473         return err;
474 }
475
476
477 EXPORT_API int email_sync_header_for_all_account(int *handle)
478 {
479         EM_DEBUG_API_BEGIN("handle[%p]", handle);
480     char *multi_user_name = NULL;
481         int err = EMAIL_ERROR_NONE;
482         HIPC_API hAPI = NULL;
483         int return_handle;
484         ASNotiData as_noti_data;
485         int i, account_count = 0;
486         emstorage_account_tbl_t *account_tbl_array = NULL;
487         int as_err;
488         int input_account_id = ALL_ACCOUNT;
489         int input_mailbox_id = 0; /* all case */
490
491     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
492         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
493         goto FINISH_OFF;
494     }
495
496         hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);
497
498         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
499
500         /* input_account_id */
501         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_account_id, sizeof(int))) {
502                 /* LCOV_EXCL_START */
503                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
504                 err = EMAIL_ERROR_OUT_OF_MEMORY;
505                 goto FINISH_OFF;
506                 /* LCOV_EXCL_STOP */
507         }
508
509         /* input_account_id */
510         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&input_mailbox_id, sizeof(int))) {
511                 /* LCOV_EXCL_START */
512                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
513                 err = EMAIL_ERROR_OUT_OF_MEMORY;
514                 goto FINISH_OFF;
515                 /* LCOV_EXCL_STOP */
516         }
517
518         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
519                 /* LCOV_EXCL_START */
520                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
521                 EM_SAFE_FREE(multi_user_name);
522                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
523                 /* LCOV_EXCL_STOP */
524         }
525
526          emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
527
528          if (err != EMAIL_ERROR_NONE)
529                  goto FINISH_OFF;
530
531     emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &return_handle);
532
533         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
534
535         /*  Get all accounts for sending notification to active sync engine. */
536         if (!emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_array , true, false, &as_err)) {
537                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ]  ", as_err);
538                 goto FINISH_OFF;
539         }
540
541         for (i = 0; i < account_count; i++) {
542                 if (account_tbl_array[i].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
543                         /*  use returned handle value for a active sync handle */
544                         /* int as_handle; */
545                         /*
546                         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
547                                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
548                                 goto FINISH_OFF;
549                         }
550                         */
551
552                         /*  noti to active sync */
553                         as_noti_data.sync_header.handle          = return_handle;
554                         as_noti_data.sync_header.account_id      = account_tbl_array[i].account_id;
555                         /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
556                         as_noti_data.sync_header.mailbox_id      = 0;
557             as_noti_data.sync_header.multi_user_name = multi_user_name;
558
559                         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
560                                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
561                                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
562                                 goto FINISH_OFF;
563                         }
564                 }
565         }
566
567          if (handle)
568                 *handle = return_handle;
569
570 FINISH_OFF:
571
572         emipc_destroy_email_api(hAPI);
573         hAPI = NULL;
574
575         if (account_tbl_array)
576                 emstorage_free_account(&account_tbl_array, account_count, NULL);
577
578     EM_SAFE_FREE(multi_user_name);
579         EM_DEBUG_API_END("err[%d]", err);
580         return err;
581 }
582
583 EXPORT_API int email_download_body(int mail_id, int with_attachment, int *handle)
584 {
585         EM_DEBUG_API_BEGIN("mail_id[%d] with_attachment[%d]", mail_id, with_attachment);
586         int err = EMAIL_ERROR_NONE;
587         emstorage_mail_tbl_t* mail_table_data = NULL;
588         int account_id = 0;
589         email_account_server_t account_server_type;
590         HIPC_API hAPI = NULL;
591         ASNotiData as_noti_data;
592     char *multi_user_name = NULL;
593
594     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
595         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
596         goto FINISH_OFF;
597     }
598
599         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
600
601         if (mail_id <= 0) {
602                 EM_DEBUG_EXCEPTION("mail_id is not valid");
603                 err = EMAIL_ERROR_INVALID_PARAM;
604                 goto FINISH_OFF;
605         }
606
607         if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data) {
608                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
609                 goto FINISH_OFF;
610         }
611
612         if (mail_table_data->account_id <= 0) {
613                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
614                 goto FINISH_OFF;
615         }
616
617         account_id = mail_table_data->account_id;
618
619         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
620         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, true, &err) == false) {
621                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
622                 goto FINISH_OFF;
623         }
624
625         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
626                 int as_handle;
627                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
628                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
629                         goto FINISH_OFF;
630                 }
631
632                 /*  noti to active sync */
633                 as_noti_data.download_body.handle          = as_handle;
634                 as_noti_data.download_body.account_id      = account_id;
635                 as_noti_data.download_body.mail_id         = mail_id;
636                 as_noti_data.download_body.with_attachment = with_attachment;
637         as_noti_data.download_body.multi_user_name = multi_user_name;
638
639                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_BODY, &as_noti_data) == false) {
640                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
641                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
642                         goto FINISH_OFF;
643                 }
644
645                 if (handle)
646                         *handle = as_handle;
647         } else {
648                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_BODY);
649
650                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
651
652                 /* Account Id */
653                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
654                         /* LCOV_EXCL_START */
655                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
656                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
657                         /* LCOV_EXCL_STOP */
658                 }
659                 /* Mail Id */
660                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
661                         /* LCOV_EXCL_START */
662                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
663                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
664                         /* LCOV_EXCL_STOP */
665                 }
666
667                 /* with_attachment */
668                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&with_attachment, sizeof(int))) {
669                         /* LCOV_EXCL_START */
670                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
671                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
672                         /* LCOV_EXCL_STOP */
673                 }
674
675                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
676                         /* LCOV_EXCL_START */
677                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
678                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
679                         /* LCOV_EXCL_STOP */
680                 }
681
682                  emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
683                  if (err != EMAIL_ERROR_NONE)
684                          goto FINISH_OFF;
685
686                  if (handle)    {
687                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
688                         EM_DEBUG_LOG("RETURN VALUE : %d  handle %d", err, *handle);
689                  }
690         }
691
692 FINISH_OFF:
693         emipc_destroy_email_api(hAPI);
694         hAPI = NULL;
695
696         if (mail_table_data) {
697                 emstorage_free_mail(&mail_table_data, 1, &err);
698         }
699
700     EM_SAFE_FREE(multi_user_name);
701
702         EM_DEBUG_API_END("err[%d]", err);
703         return err;
704
705 }
706
707
708 /* API - Downloads the Email Attachment Information [ INTERNAL ] */
709
710 EXPORT_API int email_download_attachment(int mail_id, int nth, int *handle)
711 {
712         EM_DEBUG_API_BEGIN("mail_id[%d] nth[%d] handle[%p]", mail_id, nth, handle);
713         int err = EMAIL_ERROR_NONE;
714         emstorage_mail_tbl_t* mail_table_data = NULL;
715         int account_id = 0;
716         email_account_server_t account_server_type;
717         HIPC_API hAPI = NULL;
718         ASNotiData as_noti_data;
719     char *multi_user_name = NULL;
720
721     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
722         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
723         goto FINISH_OFF;
724     }
725
726         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
727
728         if (mail_id <= 0) {
729                 EM_DEBUG_EXCEPTION("mail_id is not valid");
730                 err = EMAIL_ERROR_INVALID_PARAM;
731                 goto FINISH_OFF;
732         }
733
734         if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err) || !mail_table_data) {
735                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
736                 goto FINISH_OFF;
737         }
738
739         if (mail_table_data->account_id <= 0) {
740                 EM_DEBUG_EXCEPTION("EM_IF_ACCOUNT_ID_NULL: Account ID [ %d ]  ", mail_table_data->account_id);
741                 goto FINISH_OFF;
742         }
743
744         account_id = mail_table_data->account_id;
745
746         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, true, &err) == false) {
747                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
748                 goto FINISH_OFF;
749         }
750
751         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
752                 int as_handle;
753                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
754                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
755                         goto FINISH_OFF;
756                 }
757
758                 /*  noti to active sync */
759                 as_noti_data.download_attachment.handle           = as_handle;
760                 as_noti_data.download_attachment.account_id       = account_id;
761                 as_noti_data.download_attachment.mail_id          = mail_id;
762                 as_noti_data.download_attachment.attachment_order = nth;
763         as_noti_data.download_attachment.multi_user_name  = multi_user_name;
764
765                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT, &as_noti_data) == false) {
766                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
767                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
768                         goto FINISH_OFF;
769                 }
770
771                 if (handle)
772                         *handle = as_handle;
773         } else {
774                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_ATTACHMENT);
775
776                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
777
778                 /* Account Id */
779                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
780                         /* LCOV_EXCL_START */
781                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
782                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
783                         /* LCOV_EXCL_STOP */
784                 }
785
786                 /* Mail ID */
787                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
788                         /* LCOV_EXCL_START */
789                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
790                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
791                         /* LCOV_EXCL_STOP */
792                 }
793
794                 /* nth */
795                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &(nth), sizeof(int))) {
796                         /* LCOV_EXCL_START */
797                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
798                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
799                         /* LCOV_EXCL_STOP */
800                 }
801
802                 /* Execute API */
803                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
804                         /* LCOV_EXCL_START */
805                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
806                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
807                         /* LCOV_EXCL_STOP */
808                 }
809
810                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
811                 if (err != EMAIL_ERROR_NONE)
812                         goto FINISH_OFF;
813
814                 if (handle)
815                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
816
817         }
818
819 FINISH_OFF:
820         emipc_destroy_email_api(hAPI);
821         hAPI = NULL;
822
823         if (mail_table_data) {
824                 emstorage_free_mail(&mail_table_data, 1, &err);
825         }
826
827     EM_SAFE_FREE(multi_user_name);
828
829         EM_DEBUG_API_END("err[%d]", err);
830         return err;
831
832 }
833
834
835 EXPORT_API int email_cancel_job(int input_account_id, int input_handle, email_cancelation_type input_cancel_type)
836 {
837         EM_DEBUG_API_BEGIN("input_account_id[%d] input_handle[%d] input_cancel_type[%d]", input_account_id, input_handle, input_cancel_type);
838         int err = EMAIL_ERROR_NONE;
839         email_account_server_t account_server_type;
840         HIPC_API hAPI = NULL;
841         ASNotiData as_noti_data;
842         emstorage_account_tbl_t *account_list = NULL;
843         int i, account_count = 0;
844     char *multi_user_name = NULL;
845
846         if (input_account_id < 0)
847                 return EMAIL_ERROR_INVALID_PARAM;
848
849     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
850         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
851         goto FINISH_OFF;
852     }
853
854         if (input_account_id == ALL_ACCOUNT) {
855                 /*  this means that job is executed with all account */
856                 /*  Get all accounts for sending notification to active sync engine. */
857                 if (!emstorage_get_account_list(multi_user_name, &account_count, &account_list , true, false, &err)) {
858                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
859                         goto FINISH_OFF;
860                 }
861
862                 for (i = 0; i < account_count; i++) {
863                         if (em_get_account_server_type_by_account_id(multi_user_name, account_list[i].account_id, &account_server_type, true, &err) == false) {
864                                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
865                                 goto FINISH_OFF;
866                         }
867
868                         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
869                                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
870                                 as_noti_data.cancel_job.account_id      = account_list[i].account_id;
871                                 as_noti_data.cancel_job.handle          = input_handle;
872                                 as_noti_data.cancel_job.cancel_type     = input_cancel_type;
873                 as_noti_data.cancel_job.multi_user_name = multi_user_name;
874
875                                 if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
876                                         /* LCOV_EXCL_START */
877                                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
878                                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
879                                         goto FINISH_OFF;
880                                         /* LCOV_EXCL_STOP */
881                                 }
882                         }
883                 }
884
885                 /*  request canceling to stub */
886                 hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
887
888                 if (!hAPI) {
889                         /* LCOV_EXCL_START */
890                         EM_DEBUG_EXCEPTION("INVALID PARAM: hAPI NULL ");
891                         err = EMAIL_ERROR_NULL_VALUE;
892                         goto FINISH_OFF;
893                         /* LCOV_EXCL_STOP */
894                 }
895
896                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_account_id, sizeof(int))) {                /*  input_account_id == 0 */
897                         /* LCOV_EXCL_START */
898                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
899                         err = EMAIL_ERROR_NULL_VALUE;
900                         goto FINISH_OFF;
901                         /* LCOV_EXCL_STOP */
902                 }
903
904                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_handle, sizeof(int))) {
905                         /* LCOV_EXCL_START */
906                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
907                         err = EMAIL_ERROR_NULL_VALUE;
908                         goto FINISH_OFF;
909                         /* LCOV_EXCL_STOP */
910                 }
911
912                 /* Execute API */
913                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
914                         /* LCOV_EXCL_START */
915                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
916                         err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
917                         goto FINISH_OFF;
918                         /* LCOV_EXCL_STOP */
919                 }
920
921                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
922                 emipc_destroy_email_api(hAPI);
923                 hAPI = NULL;
924         } else {
925                 if (em_get_account_server_type_by_account_id(multi_user_name, input_account_id, &account_server_type, true, &err) == false) {
926                         EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
927                         goto FINISH_OFF;
928                 }
929
930                 if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
931                         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
932                         as_noti_data.cancel_job.account_id      = input_account_id;
933                         as_noti_data.cancel_job.handle          = input_handle;
934             as_noti_data.cancel_job.multi_user_name = multi_user_name;
935
936                         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
937                                 /* LCOV_EXCL_START */
938                                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
939                                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
940                                 goto FINISH_OFF;
941                                 /* LCOV_EXCL_STOP */
942                         }
943                 } else {
944                         hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
945
946                         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
947
948                         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_account_id, sizeof(int))) {
949                                 /* LCOV_EXCL_START */
950                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
951                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
952                                 /* LCOV_EXCL_STOP */
953                         }
954
955                         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_handle, sizeof(int))) {
956                                 /* LCOV_EXCL_START */
957                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
958                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
959                                 /* LCOV_EXCL_STOP */
960                         }
961
962                         /* Execute API */
963                         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
964                                 /* LCOV_EXCL_START */
965                                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
966                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
967                                 /* LCOV_EXCL_STOP */
968                         }
969                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
970                         emipc_destroy_email_api(hAPI);
971                         hAPI = NULL;
972                 }
973         }
974
975 FINISH_OFF:
976         emipc_destroy_email_api(hAPI);
977         hAPI = NULL;
978         if (account_list)
979                 emstorage_free_account(&account_list, account_count, NULL);
980
981     EM_SAFE_FREE(multi_user_name);
982         EM_DEBUG_API_END("err[%d]", err);
983         return err;
984 }
985
986
987
988 EXPORT_API int email_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t * status)
989 {
990         EM_DEBUG_API_BEGIN("action[%d] account_id[%d] mail_id[%d] status[%p]", action, account_id, mail_id, status);
991         int err = EMAIL_ERROR_NOT_SUPPORTED;
992         EM_DEBUG_API_END("err[%d]", err);
993         return err;
994
995 }
996
997 EXPORT_API int email_get_network_status(int* on_sending, int* on_receiving)
998 {
999         EM_DEBUG_API_BEGIN("on_sending[%p] on_receiving[%p]", on_sending, on_receiving);
1000         int err = EMAIL_ERROR_NOT_SUPPORTED;
1001         EM_DEBUG_API_END("err[%d]", err);
1002         return err;
1003 }
1004
1005 EXPORT_API int email_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count)
1006 {
1007         EM_DEBUG_API_BEGIN("output_task_information[%p] output_task_information_count[%p]", output_task_information, output_task_information_count);
1008         int err = EMAIL_ERROR_NONE;
1009         int task_information_stream_length = 0;
1010         HIPC_API hAPI = NULL;
1011         char *task_information_stream = NULL;
1012
1013         if (output_task_information == NULL || output_task_information_count == NULL) {
1014                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1015                 return EMAIL_ERROR_INVALID_PARAM;
1016         }
1017
1018         if ((hAPI = emipc_create_email_api(_EMAIL_API_GET_TASK_INFORMATION)) == NULL) {
1019                 /* LCOV_EXCL_START */
1020                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
1021                 err = EMAIL_ERROR_IPC_CRASH;
1022                 goto FINISH_OFF;
1023                 /* LCOV_EXCL_STOP */
1024         }
1025
1026         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1027                 /* LCOV_EXCL_START */
1028                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1029                 err = EMAIL_ERROR_IPC_CRASH;
1030                 goto FINISH_OFF;
1031                 /* LCOV_EXCL_STOP */
1032         }
1033         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1034
1035         if (EMAIL_ERROR_NONE == err) {
1036                 task_information_stream_length = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 1);
1037                 if (task_information_stream_length > 0) {
1038                         task_information_stream = (char*)em_malloc(task_information_stream_length + 1);
1039
1040                         if (!task_information_stream) {
1041                                 /* LCOV_EXCL_START */
1042                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1043                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1044                                 goto FINISH_OFF;
1045                                 /* LCOV_EXCL_STOP */
1046                         }
1047
1048                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, task_information_stream_length, task_information_stream);
1049                         em_convert_byte_stream_to_task_information(task_information_stream, task_information_stream_length, output_task_information, output_task_information_count);
1050                         EM_SAFE_FREE(task_information_stream); /*prevent 18951*/
1051                 }
1052
1053                 if (!output_task_information) {
1054                         /* LCOV_EXCL_START */
1055                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NULL_VALUE");
1056                         err = EMAIL_ERROR_NULL_VALUE;
1057                         goto FINISH_OFF;
1058                         /* LCOV_EXCL_STOP */
1059                 }
1060         }
1061
1062         if (err == EMAIL_ERROR_DATA_NOT_FOUND)
1063             err = EMAIL_ERROR_NONE;
1064
1065 FINISH_OFF:
1066         if (hAPI)
1067                 emipc_destroy_email_api(hAPI);
1068
1069         EM_DEBUG_API_END("err[%d]", err);
1070         return err;
1071 }
1072
1073 EXPORT_API int email_sync_imap_mailbox_list(int account_id, int *handle)
1074 {
1075         EM_DEBUG_API_BEGIN();
1076
1077         int err = EMAIL_ERROR_NONE;
1078
1079         if (account_id <= 0) {
1080                 EM_DEBUG_EXCEPTION("invalid parameters");
1081                 return EMAIL_ERROR_INVALID_PARAM;
1082         }
1083
1084         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_IMAP_MAILBOX_LIST);
1085
1086         /* account_id */
1087         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
1088                 /* LCOV_EXCL_START */
1089                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
1090                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
1091                 /* LCOV_EXCL_STOP */
1092         }
1093
1094         if (!emipc_execute_proxy_api(hAPI))  {
1095                 /* LCOV_EXCL_START */
1096                 EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
1097                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
1098                 /* LCOV_EXCL_STOP */
1099         }
1100
1101         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1102         if (handle)
1103         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
1104
1105         emipc_destroy_email_api(hAPI);
1106         hAPI = NULL;
1107
1108         EM_DEBUG_API_END("err[%d]", err);
1109         return err;
1110 }
1111
1112 EXPORT_API int email_search_mail_on_server(int input_account_id,
1113                                                                                 int input_mailbox_id,
1114                                                                                 email_search_filter_t *input_search_filter_list,
1115                                                                                 int input_search_filter_count,
1116                                                                                 int *output_handle)
1117 {
1118         EM_DEBUG_API_BEGIN("input_account_id[%d] input_mailbox_id[%d] input_search_filter_list[%p] "
1119                                                 "input_search_filter_count[%d] output_handle[%p]",
1120                                                 input_account_id, input_mailbox_id, input_search_filter_list,
1121                                                 input_search_filter_count, output_handle);
1122
1123         int       err = EMAIL_ERROR_NONE;
1124         int       return_value = 0;
1125         int       stream_size_for_search_filter_list = 0;
1126         char     *stream_for_search_filter_list = NULL;
1127     char     *multi_user_name = NULL;
1128         HIPC_API  hAPI = NULL;
1129         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1130         ASNotiData as_noti_data;
1131
1132         EM_IF_NULL_RETURN_VALUE(input_account_id,         EMAIL_ERROR_INVALID_PARAM);
1133         EM_IF_NULL_RETURN_VALUE(input_mailbox_id,         EMAIL_ERROR_INVALID_PARAM);
1134         EM_IF_NULL_RETURN_VALUE(input_search_filter_list, EMAIL_ERROR_INVALID_PARAM);
1135
1136     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1137         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1138         goto FINISH_OFF;
1139     }
1140
1141         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
1142
1143         if (em_get_account_server_type_by_account_id(multi_user_name,
1144                                                                                                 input_account_id,
1145                                                                                                 &account_server_type,
1146                                                                                                 true,
1147                                                                                                 &err) == false) {
1148                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1149                 goto FINISH_OFF;
1150         }
1151
1152         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1153                 int as_handle = 0;
1154
1155                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
1156                         /* LCOV_EXCL_START */
1157                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
1158                         goto FINISH_OFF;
1159                         /* LCOV_EXCL_STOP */
1160                 }
1161
1162                 /*  noti to active sync */
1163                 as_noti_data.search_mail_on_server.handle              = as_handle;
1164                 as_noti_data.search_mail_on_server.account_id          = input_account_id;
1165                 as_noti_data.search_mail_on_server.mailbox_id          = input_mailbox_id;
1166                 as_noti_data.search_mail_on_server.search_filter_list  = input_search_filter_list;
1167                 as_noti_data.search_mail_on_server.search_filter_count = input_search_filter_count;
1168                 as_noti_data.search_mail_on_server.multi_user_name     = multi_user_name;
1169
1170                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER, &as_noti_data);
1171
1172                 if (return_value == false) {
1173                         /* LCOV_EXCL_START */
1174                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
1175                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1176                         goto FINISH_OFF;
1177                         /* LCOV_EXCL_STOP */
1178                 }
1179
1180                 if (output_handle)
1181                         *output_handle = as_handle;
1182         } else {
1183                 hAPI = emipc_create_email_api(_EMAIL_API_SEARCH_MAIL_ON_SERVER);
1184
1185                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
1186
1187                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
1188                         /* LCOV_EXCL_START */
1189                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1190                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1191                         goto FINISH_OFF;
1192                         /* LCOV_EXCL_STOP */
1193                 }
1194
1195                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_mailbox_id, sizeof(int))) {
1196                         /* LCOV_EXCL_START */
1197                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1198                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1199                         goto FINISH_OFF;
1200                         /* LCOV_EXCL_STOP */
1201                 }
1202
1203                 stream_for_search_filter_list = em_convert_search_filter_to_byte_stream(input_search_filter_list,
1204                                                                                                                                                                 input_search_filter_count,
1205                                                                                                                                                                 &stream_size_for_search_filter_list);
1206
1207                 EM_PROXY_IF_NULL_RETURN_VALUE(stream_for_search_filter_list, hAPI, EMAIL_ERROR_NULL_VALUE);
1208
1209                 if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN,
1210                                                                                 stream_for_search_filter_list,
1211                                                                                 stream_size_for_search_filter_list)) { /*prevent 18950*/
1212                         /* LCOV_EXCL_START */
1213                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1214                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1215                         goto FINISH_OFF;
1216                         /* LCOV_EXCL_STOP */
1217                 }
1218
1219                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1220                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1221                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
1222                 }
1223
1224                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1225
1226                 if (err != EMAIL_ERROR_NONE) {
1227                         EM_DEBUG_EXCEPTION("_EMAIL_API_SEARCH_MAIL_ON_SERVER failed [%d]", err);
1228                         goto FINISH_OFF;
1229                 }
1230
1231                 if (output_handle)
1232                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
1233         }
1234
1235 FINISH_OFF:
1236         if (hAPI) {
1237                 emipc_destroy_email_api(hAPI);
1238                 hAPI = NULL;
1239         }
1240
1241     EM_SAFE_FREE(multi_user_name);
1242         EM_DEBUG_API_END("err[%d]", err);
1243         return err;
1244 }
1245
1246 EXPORT_API int email_clear_result_of_search_mail_on_server(int input_account_id)
1247 {
1248         EM_DEBUG_API_BEGIN("input_account_id[%d]", input_account_id);
1249
1250         int       err = EMAIL_ERROR_NONE;
1251         int       return_value = 0;
1252         HIPC_API  hAPI = NULL;
1253     char     *multi_user_name = NULL;
1254         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1255         ASNotiData as_noti_data;
1256
1257         EM_IF_NULL_RETURN_VALUE(input_account_id, EMAIL_ERROR_INVALID_PARAM);
1258
1259     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1260         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1261         goto FINISH_OFF;
1262     }
1263
1264         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
1265
1266         if (em_get_account_server_type_by_account_id(multi_user_name,
1267                                                                                                 input_account_id,
1268                                                                                                 &account_server_type,
1269                                                                                                 true,
1270                                                                                                 &err) == false) {
1271                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1272                 goto FINISH_OFF;
1273         }
1274
1275         if (account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1276                 int as_handle = 0;
1277
1278                 if (em_get_handle_for_activesync(&as_handle, &err) == false) {
1279                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
1280                         goto FINISH_OFF;
1281                 }
1282
1283                 /*  noti to active sync */
1284                 as_noti_data.clear_result_of_search_mail_on_server.handle              = as_handle;
1285                 as_noti_data.clear_result_of_search_mail_on_server.account_id          = input_account_id;
1286                 as_noti_data.clear_result_of_search_mail_on_server.multi_user_name     = multi_user_name;
1287
1288                 return_value = em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER,
1289                                                                                                                                         &as_noti_data);
1290                 if (return_value == false) {
1291                         /* LCOV_EXCL_START */
1292                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
1293                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1294                         goto FINISH_OFF;
1295                         /* LCOV_EXCL_STOP */
1296                 }
1297         } else {
1298                 hAPI = emipc_create_email_api(_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER);
1299
1300                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
1301
1302                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
1303                         /* LCOV_EXCL_START */
1304                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1305                         err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1306                         goto FINISH_OFF;
1307                         /* LCOV_EXCL_STOP */
1308                 }
1309
1310                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1311                         /* LCOV_EXCL_START */
1312                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1313                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
1314                         /* LCOV_EXCL_STOP */
1315                 }
1316
1317                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1318                 if (err != EMAIL_ERROR_NONE) {
1319                         /* LCOV_EXCL_START */
1320                         EM_DEBUG_EXCEPTION("_EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER failed [%d]", err);
1321                         goto FINISH_OFF;
1322                         /* LCOV_EXCL_STOP */
1323                 }
1324         }
1325
1326 FINISH_OFF:
1327         if (hAPI) {
1328                 emipc_destroy_email_api(hAPI);
1329                 hAPI = NULL;
1330         }
1331
1332     EM_SAFE_FREE(multi_user_name);
1333         EM_DEBUG_API_END("err[%d]", err);
1334         return err;
1335 }
1336
1337 EXPORT_API int email_query_smtp_mail_size_limit(int account_id, int *handle)
1338 {
1339         EM_DEBUG_API_BEGIN("account_id[%d] handle[%p]", account_id, handle);
1340         int err = EMAIL_ERROR_NONE;
1341     char *multi_user_name = NULL;
1342         email_account_server_t account_server_type;
1343         HIPC_API hAPI = NULL;
1344
1345         if (account_id <= 0) {
1346                 EM_DEBUG_EXCEPTION("account_id is not valid");
1347                 err = EMAIL_ERROR_INVALID_PARAM;
1348                 goto FINISH_OFF;
1349         }
1350
1351     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
1352         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
1353         goto FINISH_OFF;
1354     }
1355
1356         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
1357                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1358                 goto FINISH_OFF;
1359         }
1360
1361         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1362                 hAPI = emipc_create_email_api(_EMAIL_API_QUERY_SMTP_MAIL_SIZE_LIMIT);
1363
1364                 if (!hAPI) {
1365                         EM_DEBUG_EXCEPTION("INVALID PARAM: hAPI NULL");
1366                         return EMAIL_ERROR_NULL_VALUE;
1367                 }
1368
1369                 /*account_id */
1370                 if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
1371                         EM_DEBUG_EXCEPTION("email_query_smtp_mail_size_limit--Add Param mail_id failed");
1372                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
1373                 }
1374
1375                 if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
1376                         EM_DEBUG_EXCEPTION("email_query_smtp_mail_size_limit--emipc_execute_proxy_api failed  ");
1377                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_CRASH);
1378                 }
1379
1380                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1381                 if (err == EMAIL_ERROR_NONE) {
1382                         if (handle)
1383                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
1384                 }
1385         }
1386
1387 FINISH_OFF:
1388         emipc_destroy_email_api(hAPI);
1389         hAPI = (HIPC_API)NULL;
1390
1391     EM_SAFE_FREE(multi_user_name);
1392         EM_DEBUG_API_END("err[%d]", err);
1393         return err;
1394 }