Fixed the invalid casting and remove the flash noti
[platform/core/messaging/email-service.git] / email-daemon / main.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 /* Email service Main .c */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <malloc.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <gio/gio.h>
35
36 #include "email-daemon.h"
37 #include "email-ipc.h"
38 #include "email-ipc-api-info.h"
39 #include "email-utilities.h"
40 #include "email-debug-log.h"
41 #include "email-daemon-auto-poll.h"
42 #include "email-daemon-account.h"
43 #include "email-convert.h"
44 #include "email-internal-types.h"
45 #include "email-types.h"
46 #include "email-core-account.h"
47 #include "email-core-mail.h"
48 #include "email-core-smtp.h"
49 #include "email-core-event.h"
50 #include "email-core-global.h"
51 #include "email-core-mailbox.h"
52 #include "email-core-utils.h"
53 #include "email-core-smime.h"
54 #include "email-core-pgp.h"
55 #include "email-core-cert.h"
56 #include "email-core-task-manager.h"
57 #include "email-core-signal.h"
58 #include "email-core-imap-idle.h"
59 #include "email-core-gmime.h"
60 #include "email-storage.h"
61 #include "email-dbus-activation.h"
62 #include "email-core-container.h"
63 #include "email-core-cynara.h"
64
65 void stb_create_account(HIPC_API a_hAPI)
66 {
67         EM_DEBUG_FUNC_BEGIN();
68         int buffer_size = 0;
69         int local_result = 0;
70         char* local_account_stream = NULL;
71         email_account_t account;
72         int err = EMAIL_ERROR_NONE;
73
74     int nAPPID = emipc_get_app_id(a_hAPI);
75     char *multi_user_name = NULL;
76
77     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
78         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
79         multi_user_name = NULL;
80     }
81         /* Initialize the email_account_t */
82         memset(&account, 0x00, sizeof(email_account_t));
83
84         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
85         EM_DEBUG_LOG("size [%d]", buffer_size);
86         if(buffer_size <= 0)    {
87                 err = EMAIL_ERROR_INVALID_PARAM;
88                 goto FINISH_OFF;
89         }
90         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
91         /* Convert account stream to structure */
92         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &account);
93     account.user_name = EM_SAFE_STRDUP(multi_user_name);
94
95         EM_DEBUG_LOG_SEC("Account name - %s", account.account_name);
96         EM_DEBUG_LOG_SEC("Email Address - %s", account.user_email_address);
97     EM_DEBUG_LOG("Multi user name - %s", account.user_name);
98
99         if(!emdaemon_create_account(multi_user_name, &account, &err)) {
100                 EM_DEBUG_EXCEPTION("emdaemon_create_account fail ");
101                 goto FINISH_OFF;
102         }
103
104
105 #ifdef __FEATURE_AUTO_POLLING__
106         /* start auto polling, if check_interval not zero */
107         if(account.check_interval > 0 || (account.peak_days > 0 && account.peak_interval > 0)) {
108                 if(!emdaemon_add_polling_alarm(multi_user_name, account.account_id))
109                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
110         }
111 #ifdef __FEATURE_IMAP_IDLE__
112         else if(account.check_interval == 0 || (account.peak_days > 0 && account.peak_interval == 0))
113                 emcore_refresh_imap_idle_thread();
114 #endif /* __FEATURE_IMAP_IDLE__ */
115
116 #endif
117
118         local_result = 1;
119         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
120                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
121
122         EM_DEBUG_LOG("[3] APPID[%d], APIID [%d]", emipc_get_app_id(a_hAPI), emipc_get_api_id(a_hAPI));
123         EM_DEBUG_LOG("account id[%d]", account.account_id);
124
125         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(account.account_id), sizeof(int)))
126                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
127
128 FINISH_OFF:
129
130         if ( local_result == 0 ) {
131                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
132                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
133                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
134                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
135         }
136         if (!emipc_execute_stub_api(a_hAPI))
137                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
138
139         emcore_free_account(&account); /* valgrind */
140
141         //emcore_init_account_reference();
142
143         if (local_result == 1) {
144                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD_FINISH, account.account_id, 0, NULL, 0))
145                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
146         } else {
147                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD_FAIL, account.account_id, 0, NULL, 0))
148                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
149         }
150
151         EM_SAFE_FREE(multi_user_name);
152         EM_DEBUG_FUNC_END();
153 }
154
155 void stb_delete_account(HIPC_API a_hAPI)
156 {
157         EM_DEBUG_FUNC_BEGIN();
158         int account_id = 0;
159         int local_result = 0;
160         int *ret_nth_value = NULL;
161         int err = EMAIL_ERROR_NONE;
162     int nAPPID = emipc_get_app_id(a_hAPI);
163     char *multi_user_name = NULL;
164
165     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
166         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
167         multi_user_name = NULL;
168     }
169
170         /* account_id */
171         if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0)))
172                 account_id = *ret_nth_value;
173         else {
174                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
175                 goto FINISH_OFF;
176         }
177         
178         if(!emdaemon_delete_account(multi_user_name, account_id, &err)) {
179                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
180                         EM_DEBUG_LOG("emipc_add_parameter failed ");
181
182                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
183                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
184
185                 if (!emipc_execute_stub_api(a_hAPI))
186                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
187                 goto FINISH_OFF;
188         }
189
190 #ifdef __FEATURE_AUTO_POLLING__
191         /* stop auto polling for this acount */
192         if(emdaemon_check_auto_polling_started(account_id)) {
193                 if(!emdaemon_remove_polling_alarm(account_id))
194                         EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ NOTI_ACCOUNT_DELETE] : remove auto poll failed >>> ");
195         }
196 #endif
197         local_result = 1;
198
199         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
200                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
201
202         if (!emipc_execute_stub_api(a_hAPI))
203                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
204
205         /* if file is not deleted, main thread kills the thread */
206         emcore_send_signal_for_del_account (EMAIL_SIGNAL_DB_DELETED);
207         EM_DEBUG_LOG ("publish db of account deleted");
208
209 FINISH_OFF:
210
211     EM_SAFE_FREE(multi_user_name);
212         EM_DEBUG_FUNC_END();
213 }
214
215 void stb_update_account(HIPC_API a_hAPI)
216 {
217         EM_DEBUG_FUNC_BEGIN();
218         int account_id = 0, buffer_size = 0, with_validation = 0;
219         int *ret_nth_value = NULL;
220         char* local_account_stream = NULL;
221         email_account_t new_account_info = {0};
222         int err = EMAIL_ERROR_NONE;
223         int handle = 0;
224     int nAPPID = emipc_get_app_id(a_hAPI);
225     char *multi_user_name = NULL;
226
227     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
228         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
229         multi_user_name = NULL;
230     }
231
232         if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0)))
233                 account_id = *ret_nth_value;
234         else {
235                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
236                 goto FINISH_OFF;
237         }
238
239         /* get account structure from stream */
240         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
241         EM_DEBUG_LOG("size [%d]", buffer_size);
242         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
243         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &new_account_info);
244
245         /*get validation flag */
246         if ((ret_nth_value = (int *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 2))) {
247                 with_validation = *ret_nth_value;
248         } else {
249                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
250                 goto FINISH_OFF;
251         }
252
253         if(with_validation) {
254                 if(!emdaemon_validate_account_and_update(multi_user_name, account_id, &new_account_info, &handle, &err)){
255                         EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_update failed [%d]", err);
256                         goto FINISH_OFF;
257                 }
258         }
259         else {
260                 if(!emdaemon_update_account(multi_user_name, account_id, &new_account_info, &err)) {
261                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
262                         goto FINISH_OFF;
263                 }
264
265         }
266
267 FINISH_OFF:
268
269         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
270                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
271
272         if (!emipc_execute_stub_api(a_hAPI))
273                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
274
275         emcore_free_account(&new_account_info);
276
277         EM_SAFE_FREE(multi_user_name);
278         EM_DEBUG_FUNC_END();
279 }
280
281 void stb_validate_account(HIPC_API a_hAPI)
282 {
283         EM_DEBUG_FUNC_BEGIN();
284
285         int account_id = 0;
286         int local_result = 0;
287         int err_code = 0;
288         int handle = 0;
289     int nAPPID = emipc_get_app_id(a_hAPI);
290     char *multi_user_name = NULL;
291
292     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
293         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
294         multi_user_name = NULL;
295     }
296
297         /* account_id */
298         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
299         local_result = emdaemon_validate_account(multi_user_name, account_id, &handle, &err_code);
300
301         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
302                 EM_DEBUG_LOG("emipc_add_parameter failed ");
303
304         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
305         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
306
307         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
308                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
309
310         if (!emipc_execute_stub_api(a_hAPI))
311                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
312
313     EM_SAFE_FREE(multi_user_name);
314         EM_DEBUG_FUNC_END();
315 }
316
317
318 void stb_get_account_list(HIPC_API a_hAPI)
319 {
320         EM_DEBUG_FUNC_BEGIN();
321
322         int local_result = 0;
323         int i= 0;
324         char* local_stream = NULL;
325         email_account_t* account_list;
326         int count;
327         int size = 0;
328         int err = EMAIL_ERROR_NONE;
329     int nAPPID = emipc_get_app_id(a_hAPI);
330     char *multi_user_name = NULL;
331
332     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
333         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
334         multi_user_name = NULL;
335     }
336
337         if(emdaemon_get_account_list(multi_user_name, &account_list, &count, &err)) {
338                 EM_DEBUG_LOG("emdaemon_get_account_list success");
339                 local_result = 1;
340                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
341                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
342
343                 EM_DEBUG_LOG("Count [%d]", count);
344                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
345                         EM_DEBUG_EXCEPTION("emipc_add_parameter count failed ");
346
347                 for(i=0; i<count; i++) {
348                         EM_DEBUG_LOG_SEC("Name - %s", account_list[i].account_name);
349
350                         local_stream = em_convert_account_to_byte_stream(account_list+i, &size);
351
352                         if (!local_stream) {
353                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
354                                 emcore_free_account_list(&account_list, count, NULL);
355                                 return;
356                         }
357
358                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
359                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
360
361                         size = 0;
362                         EM_SAFE_FREE(local_stream);
363                 }
364
365                 emcore_free_account_list(&account_list, count, NULL);
366                 if (!emipc_execute_stub_api(a_hAPI))
367                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
368         }
369
370         else {
371                 EM_DEBUG_LOG("emdaemon_get_account_list failed");
372                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
373                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
374                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
375                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
376                 if (!emipc_execute_stub_api(a_hAPI))
377                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
378         }
379
380         EM_SAFE_FREE(local_stream);
381     EM_SAFE_FREE(multi_user_name);
382         EM_DEBUG_FUNC_END();
383 }
384
385 /* sowmya.kr, 10-May-2010, changes for API improvement */
386 void stb_sync_header(HIPC_API a_hAPI)
387 {
388         EM_DEBUG_FUNC_BEGIN();
389         int err = EMAIL_ERROR_NONE;
390         int handle = -1;
391         int account_id = 0, maibox_id = 0;
392     int nAPPID = emipc_get_app_id(a_hAPI);
393     char *multi_user_name = NULL;
394
395     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
396         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
397         multi_user_name = NULL;
398     }
399
400         /* account_id */
401         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
402         /* maibox_id */
403         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &maibox_id);
404         EM_DEBUG_LOG("account_id [%d] maibox_id [%d]", account_id, maibox_id);
405
406         if(!emdaemon_sync_header(multi_user_name, account_id, maibox_id, &handle, &err)) {
407                 EM_DEBUG_EXCEPTION ("emdaemon_sync_header failed [%d]", err);
408         }
409
410         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
411                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
412         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
413                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
414         if (!emipc_execute_stub_api(a_hAPI))
415                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
416
417     EM_SAFE_FREE(multi_user_name);
418         EM_DEBUG_FUNC_END();
419 }
420
421 void stb_download_body(HIPC_API a_hAPI)
422 {
423         EM_DEBUG_FUNC_BEGIN();
424         int err = EMAIL_ERROR_NONE;
425         int mail_id = 0;
426         int attachment_count = 0;
427         int handle = 0;
428         int account_id = 0;
429     int nAPPID = emipc_get_app_id(a_hAPI);
430     char *multi_user_name = NULL;
431
432     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
433         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
434         multi_user_name = NULL;
435     }
436
437         /* Account Id */
438         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
439
440         /* Mail Id */
441         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
442
443         /* Has Attachment */
444         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &attachment_count);
445
446         /*Download Body */
447         if (!emdaemon_download_body(multi_user_name, account_id, mail_id, 1, attachment_count, &handle, &err)) {
448                 EM_DEBUG_EXCEPTION("emdaemon_download_body - failed");
449                 goto FINISH_OFF;
450         }
451
452         err = EMAIL_ERROR_NONE;
453
454 FINISH_OFF:
455
456         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
457             EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
458
459         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
460                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
461
462         if (!emipc_execute_stub_api(a_hAPI))
463                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
464
465     EM_SAFE_FREE(multi_user_name);
466
467         EM_DEBUG_FUNC_END();
468 }
469
470
471 void stb_create_mailbox(HIPC_API a_hAPI)
472 {
473         EM_DEBUG_FUNC_BEGIN();
474         int                   buffer_size                       = 0;
475         int                   err = EMAIL_ERROR_NONE;
476         int                  *ret_nth_value = NULL;
477         char                 *local_stream  = NULL;
478         int                   on_server                 = 0;
479         email_mailbox_t   mailbox = { 0 };
480         int               handle = 0; /* Added for cancelling mailbox creating  */
481     int nAPPID = emipc_get_app_id(a_hAPI);
482     char *multi_user_name = NULL;
483
484     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
485         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
486         multi_user_name = NULL;
487     }
488
489         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
490         EM_DEBUG_LOG("size [%d]", buffer_size);
491
492         if(buffer_size <= 0) {
493                 EM_DEBUG_EXCEPTION("buffer_size(%d) should be greater than 0", buffer_size);
494                 err = EMAIL_ERROR_INVALID_PARAM;
495                 goto FINISH_OFF;
496         }
497
498         local_stream = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
499         em_convert_byte_stream_to_mailbox(local_stream, buffer_size, &mailbox);
500         EM_DEBUG_LOG_SEC("Mailbox name - %s", mailbox.mailbox_name);
501
502         if ((ret_nth_value = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1)))
503                 on_server = *ret_nth_value;
504         else
505                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
506
507         emdaemon_add_mailbox(multi_user_name, &mailbox, on_server, &handle, &err);
508
509 FINISH_OFF:
510
511         emcore_free_mailbox(&mailbox);
512     EM_SAFE_FREE(multi_user_name);
513
514         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
515                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
516         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
517                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
518         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &mailbox.mailbox_id, sizeof(int)))
519                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 3");
520
521         if (!emipc_execute_stub_api(a_hAPI)) {
522                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
523                 return;
524         }
525
526         EM_DEBUG_FUNC_END();
527 }
528
529
530 void stb_delete_mailbox(HIPC_API a_hAPI)
531 {
532         EM_DEBUG_FUNC_BEGIN();
533         int              err = EMAIL_ERROR_NONE;
534         int      on_server              = 0;
535         int      handle = 0; /* Added for cancelling mailbox deleting */
536         int      input_mailbox_id = 0;
537     int nAPPID = emipc_get_app_id(a_hAPI);
538     char *multi_user_name = NULL;
539
540     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
541         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
542         multi_user_name = NULL;
543     }
544
545         /* src_mailbox_id */
546         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &input_mailbox_id);
547         EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
548
549         if (input_mailbox_id > 0)
550                 EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
551         else
552                 EM_DEBUG_LOG("input_mailbox_id == 0");
553
554         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &on_server);
555
556         if(emdaemon_delete_mailbox(multi_user_name, input_mailbox_id, on_server, &handle, &err))
557                 err = EMAIL_ERROR_NONE;
558
559         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
560                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
561         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
562                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
563
564         if (!emipc_execute_stub_api(a_hAPI))
565                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
566
567     EM_SAFE_FREE(multi_user_name);
568         EM_DEBUG_FUNC_END();
569 }
570
571 void stb_set_mailbox_type(HIPC_API a_hAPI)
572 {
573         EM_DEBUG_FUNC_BEGIN();
574         int     err = EMAIL_ERROR_NONE;
575         int mailbox_id = 0;
576         int mailbox_type = 0;
577     int nAPPID = emipc_get_app_id(a_hAPI);
578     char *multi_user_name = NULL;
579
580     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
581             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
582             multi_user_name = NULL;
583     }
584
585         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
586         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
587
588         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_type);
589         EM_DEBUG_LOG("mailbox_type[%d]", mailbox_type);
590
591         if( (err = emdaemon_set_mailbox_type(multi_user_name, mailbox_id, mailbox_type)) != EMAIL_ERROR_NONE)
592                 err = EMAIL_ERROR_NONE;
593
594         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
595                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
596
597         if (!emipc_execute_stub_api(a_hAPI))
598                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
599
600     EM_SAFE_FREE(multi_user_name);
601         EM_DEBUG_FUNC_END();
602 }
603
604 void stb_set_local_mailbox(HIPC_API a_hAPI)
605 {
606         EM_DEBUG_FUNC_BEGIN();
607         int     err = EMAIL_ERROR_NONE;
608         int mailbox_id = 0;
609         int is_local_mailbox = 0;
610     int nAPPID = emipc_get_app_id(a_hAPI);
611     char *multi_user_name = NULL;
612
613     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
614         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
615         multi_user_name = NULL;
616     }
617
618         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
619         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
620
621         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &is_local_mailbox);
622         EM_DEBUG_LOG("is_local_mailbox[%d]", is_local_mailbox);
623
624         if( (err = emdaemon_set_local_mailbox(multi_user_name, mailbox_id, is_local_mailbox)) != EMAIL_ERROR_NONE)
625                 err = EMAIL_ERROR_NONE;
626
627         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
628                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
629
630         if (!emipc_execute_stub_api(a_hAPI))
631                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
632
633     EM_SAFE_FREE(multi_user_name);
634         EM_DEBUG_FUNC_END();
635 }
636
637 void stb_stamp_sync_time_of_mailbox(HIPC_API a_hAPI)
638 {
639         EM_DEBUG_FUNC_BEGIN();
640         int err = EMAIL_ERROR_NONE;
641         int mailbox_id = 0;
642         int nAPPID = emipc_get_app_id(a_hAPI);
643     char *multi_user_name = NULL;
644
645     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
646         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
647         multi_user_name = NULL;
648     }
649
650         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
651         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
652
653         err = emstorage_stamp_last_sync_time_of_mailbox(multi_user_name, mailbox_id, 1);
654
655         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
656                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
657
658         if (!emipc_execute_stub_api(a_hAPI))
659                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
660
661         EM_SAFE_FREE(multi_user_name);
662         EM_DEBUG_FUNC_END();
663 }
664
665 void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
666 {
667         EM_DEBUG_FUNC_BEGIN();
668         int     err    = EMAIL_ERROR_NONE;
669         int handle = 0;
670         int account_id = 0;
671         int mailbox_id = 0;
672         int mail_slot_size = 0;
673     int nAPPID = emipc_get_app_id(a_hAPI);
674     char *multi_user_name = NULL;
675
676     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
677         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
678         multi_user_name = NULL;
679     }
680
681         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
682         EM_DEBUG_LOG("account_id[%d]", account_id);
683
684         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
685         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
686
687         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_slot_size);
688         EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
689
690         if(emdaemon_set_mail_slot_size_of_mailbox(multi_user_name, account_id, mailbox_id, mail_slot_size, &handle, &err))
691                 err = EMAIL_ERROR_NONE;
692         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
693                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
694
695         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
696                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
697
698         if (!emipc_execute_stub_api(a_hAPI))
699                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
700
701     EM_SAFE_FREE(multi_user_name);
702         EM_DEBUG_FUNC_END();
703 }
704
705 void stb_rename_mailbox(HIPC_API a_hAPI)
706 {
707         EM_DEBUG_FUNC_BEGIN();
708         int buffer_size  = 0;
709         int     err = EMAIL_ERROR_NONE;
710         int handle = 0;
711         int mailbox_id = 0;
712         int on_server = 0;
713         char *mailbox_path = NULL;
714         char *mailbox_alias = NULL;
715     int nAPPID = emipc_get_app_id(a_hAPI);
716     char *multi_user_name = NULL;
717
718     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
719         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
720         multi_user_name = NULL;
721     }
722
723         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
724         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
725
726         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
727         EM_DEBUG_LOG("mailbox_path string size[%d]", buffer_size);
728         if(buffer_size > 0)       {
729                 mailbox_path = (char*)em_malloc(buffer_size);
730                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
731                 EM_DEBUG_LOG_SEC("mailbox_path [%s]", mailbox_path);
732         }
733
734         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
735         EM_DEBUG_LOG("mailbox_alias string size[%d]", buffer_size);
736         if(buffer_size > 0)       {
737                 mailbox_alias = (char*)em_malloc(buffer_size);
738                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
739                 EM_DEBUG_LOG_SEC("mailbox_alias [%s]", mailbox_alias);
740         }
741
742         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &on_server);
743         EM_DEBUG_LOG("on_server[%d]", on_server);
744
745         if ((err = emdaemon_rename_mailbox(multi_user_name, mailbox_id, mailbox_path, mailbox_alias, NULL, 0, on_server, &handle)) != EMAIL_ERROR_NONE) {
746                 EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
747         }
748
749         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
750                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
751
752         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
753                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
754
755         if (!emipc_execute_stub_api(a_hAPI))
756                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
757
758         EM_SAFE_FREE(mailbox_alias);
759         EM_SAFE_FREE(mailbox_path);
760
761     EM_SAFE_FREE(multi_user_name);
762         EM_DEBUG_FUNC_END();
763 }
764
765 void stb_rename_mailbox_ex(HIPC_API a_hAPI)
766 {
767         EM_DEBUG_FUNC_BEGIN();
768         int buffer_size  = 0;
769         int     err = EMAIL_ERROR_NONE;
770         int handle = 0;
771         int mailbox_id = 0;
772         int on_server = 0;
773         int eas_data_length = 0;
774         char *mailbox_path = NULL;
775         char *mailbox_alias = NULL;
776         void *eas_data = NULL;
777     int nAPPID = emipc_get_app_id(a_hAPI);
778     char *multi_user_name = NULL;
779
780     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
781         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
782         multi_user_name = NULL;
783     }
784
785         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
786         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
787
788         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
789         EM_DEBUG_LOG("mailbox_path string size[%d]", buffer_size);
790         if(buffer_size > 0)       {
791                 mailbox_path = (char*)em_malloc(buffer_size);
792                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
793                 EM_DEBUG_LOG_SEC("mailbox_path [%s]", mailbox_path);
794         }
795
796         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
797         EM_DEBUG_LOG("mailbox_alias string size[%d]", buffer_size);
798         if(buffer_size > 0)       {
799                 mailbox_alias = (char*)em_malloc(buffer_size);
800                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
801                 EM_DEBUG_LOG_SEC("mailbox_alias [%s]", mailbox_alias);
802         }
803
804         eas_data_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 3);
805         EM_DEBUG_LOG("eas_data_length  size[%d]", eas_data_length);
806         if(eas_data_length > 0)   {
807                 eas_data = (char*)em_malloc(eas_data_length);
808                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, eas_data_length, eas_data);
809         }
810
811         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &on_server);
812         EM_DEBUG_LOG("on_server[%d]", on_server);
813
814         if ((err = emdaemon_rename_mailbox(multi_user_name, mailbox_id, mailbox_path, mailbox_alias, eas_data, eas_data_length, on_server, &handle)) != EMAIL_ERROR_NONE) {
815                 EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
816         }
817
818         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
819                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
820
821         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
822                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
823
824         if (!emipc_execute_stub_api(a_hAPI))
825                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
826
827         EM_SAFE_FREE(mailbox_alias);
828         EM_SAFE_FREE(mailbox_path);
829         EM_SAFE_FREE(eas_data);
830
831     EM_SAFE_FREE(multi_user_name);
832         EM_DEBUG_FUNC_END();
833 }
834
835 void stb_send_mail(HIPC_API a_hAPI)
836 {
837         EM_DEBUG_FUNC_BEGIN();
838         char* local_stream = NULL;
839         int mail_id;
840         int handle;
841         int err = EMAIL_ERROR_NONE;
842     int nAPPID = emipc_get_app_id(a_hAPI);
843     char *multi_user_name = NULL;
844
845     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
846         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
847         multi_user_name = NULL;
848     }
849
850         /* Mail_id */
851         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
852         EM_DEBUG_LOG("mail_id [%d]", mail_id);
853
854         if(emdaemon_send_mail(multi_user_name, mail_id, &handle, &err)) {
855                 err = EMAIL_ERROR_NONE;
856                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
857                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
858                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
859                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
860                 if (!emipc_execute_stub_api(a_hAPI))
861                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
862         }
863         else {
864                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
865                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
866                 if (!emipc_execute_stub_api(a_hAPI))
867                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
868         }
869
870         EM_SAFE_FREE(local_stream);
871
872     EM_SAFE_FREE(multi_user_name);
873         EM_DEBUG_FUNC_END();
874 }
875
876 void stb_query_smtp_mail_size_limit(HIPC_API a_hAPI)
877 {
878         EM_DEBUG_FUNC_BEGIN();
879
880         int account_id;
881         int handle;
882         int err = EMAIL_ERROR_NONE;
883     int nAPPID = emipc_get_app_id(a_hAPI);
884     char *multi_user_name = NULL;
885
886     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
887         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
888         multi_user_name = NULL;
889     }
890
891         /* Mail_id */
892         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
893         EM_DEBUG_LOG("account_id [%d]", account_id);
894
895         if(emdaemon_query_smtp_mail_size_limit(multi_user_name, account_id, &handle, &err)) {
896                 err = EMAIL_ERROR_NONE;
897                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
898                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
899                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
900                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
901                 if (!emipc_execute_stub_api(a_hAPI))
902                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
903         } else {
904                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
905                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
906                 if (!emipc_execute_stub_api(a_hAPI))
907                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
908         }
909
910     EM_SAFE_FREE(multi_user_name);
911         EM_DEBUG_FUNC_END();
912 }
913
914 /* obsolete - there is no api calling this function */
915 void stb_get_mailbox_list(HIPC_API a_hAPI)
916 {
917         EM_DEBUG_FUNC_BEGIN();
918         int err = EMAIL_ERROR_NONE;
919         int counter = 0;
920         char* local_stream = NULL;
921         int account_id;
922         email_mailbox_t* mailbox_list = NULL;
923         int count = 0;
924         int size = 0;
925     int nAPPID = emipc_get_app_id(a_hAPI);
926     char *multi_user_name = NULL;
927
928     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
929             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
930             multi_user_name = NULL;
931     }
932
933         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
934
935         if(emdaemon_get_mailbox_list(multi_user_name, account_id, &mailbox_list, &count, &err))
936                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
937
938         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
939                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
940
941         if(EMAIL_ERROR_NONE == err) {
942                 EM_DEBUG_LOG("Count [%d]", count);
943                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
944                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
945
946                 for(counter=0; counter<count; counter++) {
947                         EM_DEBUG_LOG_SEC("Name - %s", mailbox_list[counter].mailbox_name);
948
949                         local_stream = em_convert_mailbox_to_byte_stream(mailbox_list+counter, &size);
950
951                         if (!local_stream) {
952                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
953                                 emcore_free_mailbox_list(&mailbox_list, count);
954                                 return;
955                         }
956
957                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
958                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
959
960                         EM_SAFE_FREE(local_stream);
961                         size = 0;
962                 }
963         }
964
965         if (!emipc_execute_stub_api(a_hAPI))
966                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
967
968         emcore_free_mailbox_list(&mailbox_list, count);
969
970     EM_SAFE_FREE(multi_user_name);
971         EM_DEBUG_FUNC_END();
972 }
973
974 void stb_delete_all_mails(HIPC_API a_hAPI)
975 {
976         EM_DEBUG_FUNC_BEGIN();
977         int mailbox_id = 0;
978         int from_server = 0;
979         int err = EMAIL_ERROR_NONE;
980     int nAPPID = emipc_get_app_id(a_hAPI);
981     char *multi_user_name = NULL;
982
983     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
984             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
985             multi_user_name = NULL;
986     }
987
988         /* mailbox_id */
989         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
990
991         /* from_server */
992         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &from_server);
993
994         emdaemon_delete_mail_all(multi_user_name, mailbox_id, from_server, NULL, &err);
995
996         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
997                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
998         if (!emipc_execute_stub_api(a_hAPI))
999                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1000
1001     EM_SAFE_FREE(multi_user_name);
1002         EM_DEBUG_FUNC_END();
1003 }
1004
1005 void stb_delete_mail(HIPC_API a_hAPI)
1006 {
1007         EM_DEBUG_FUNC_BEGIN();
1008         int err = EMAIL_ERROR_NONE;
1009         int mailbox_id = 0;
1010         int from_server = 0;
1011         int counter = 0;
1012         int num;
1013         int *mail_ids = NULL;
1014     int nAPPID = emipc_get_app_id(a_hAPI);
1015     char *multi_user_name = NULL;
1016
1017     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1018             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1019             multi_user_name = NULL;
1020     }
1021
1022         /* Mailbox */
1023         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
1024
1025         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1026
1027         /* Number of mail_ids */
1028         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
1029         EM_DEBUG_LOG("number of mails [%d]", num);
1030
1031         if (num > 0) {
1032                 mail_ids = em_malloc(sizeof(int) * num);
1033                 if (!mail_ids) {
1034                         EM_DEBUG_EXCEPTION("em_malloc failed");
1035                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1036                         goto FINISH_OFF;
1037                 }
1038         }
1039
1040         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
1041
1042         for(counter = 0; counter < num; counter++)
1043                 EM_DEBUG_LOG("mail_ids [%d]", mail_ids[counter]);
1044
1045         /* from_server */
1046         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &from_server);
1047         EM_DEBUG_LOG("from_server [%d]", from_server);
1048
1049         emdaemon_delete_mail(multi_user_name, mailbox_id, mail_ids, num, from_server, NULL, &err);
1050
1051 FINISH_OFF:
1052         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1053                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1054
1055         if (!emipc_execute_stub_api(a_hAPI))
1056                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1057
1058         EM_SAFE_FREE(mail_ids);
1059
1060     EM_SAFE_FREE(multi_user_name);
1061         EM_DEBUG_FUNC_END();
1062 }
1063
1064 void stb_clear_mail_data (HIPC_API a_hAPI)
1065 {
1066         EM_DEBUG_FUNC_BEGIN();
1067         int err = EMAIL_ERROR_NONE;
1068
1069     int nAPPID = emipc_get_app_id(a_hAPI);
1070     char *multi_user_name = NULL;
1071
1072     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1073         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1074         multi_user_name = NULL;
1075     }
1076
1077         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1078                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
1079         if (!emipc_execute_stub_api(a_hAPI))
1080                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1081
1082         if(emdaemon_clear_all_mail_data(multi_user_name, &err)) {
1083                 EM_DEBUG_LOG(">>> stb_clear_mail_data Success [ %d]  >> ", err);
1084         }
1085
1086     EM_SAFE_FREE(multi_user_name);
1087         EM_DEBUG_FUNC_END();
1088 }
1089
1090 void stb_add_rule(HIPC_API a_hAPI)
1091 {
1092         EM_DEBUG_FUNC_BEGIN();
1093         int buffer_size = 0;
1094         int err = EMAIL_ERROR_NONE;
1095         char* local_rule_stream = NULL;
1096         email_rule_t rule = { 0 };
1097     int nAPPID = emipc_get_app_id(a_hAPI);
1098     char *multi_user_name = NULL;
1099
1100     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1101         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1102         multi_user_name = NULL;
1103     }
1104
1105         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
1106         if(buffer_size <= 0) {
1107                 err = EMAIL_ERROR_INVALID_PARAM;
1108                 goto FINISH_OFF;
1109         }
1110
1111         local_rule_stream = (char*)     emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
1112         if(!local_rule_stream) {
1113                 err = EMAIL_ERROR_INVALID_PARAM;
1114                 goto FINISH_OFF;
1115         }
1116         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
1117         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
1118
1119         /* call add_filter handler */
1120         err = emdaemon_add_filter(multi_user_name, &rule);
1121
1122 FINISH_OFF:
1123
1124         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1125                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1126         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &rule.filter_id, sizeof(int)))
1127                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1128         if (!emipc_execute_stub_api(a_hAPI))
1129                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed ");
1130
1131         emcore_free_rule(&rule);
1132
1133     EM_SAFE_FREE(multi_user_name);
1134         EM_DEBUG_FUNC_END();
1135 }
1136
1137
1138 /* obsolete - there is no api calling this function */
1139 void stb_get_rule(HIPC_API a_hAPI)
1140 {
1141         EM_DEBUG_FUNC_BEGIN();
1142         int err = EMAIL_ERROR_NONE;
1143         int filter_id = 0;
1144         email_rule_t* rule = NULL;
1145         int size =0;
1146         char* local_rule_stream = NULL;
1147     int nAPPID = emipc_get_app_id(a_hAPI);
1148     char *multi_user_name = NULL;
1149
1150     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1151         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1152         multi_user_name = NULL;
1153     }
1154
1155         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
1156         EM_DEBUG_LOG("Filter ID : [%d]", filter_id);
1157
1158         if (!emdaemon_get_filter(multi_user_name, filter_id, &rule, &err)) {
1159                 EM_DEBUG_EXCEPTION("emdaemon_get_filter failed : [%d]", err);
1160                 goto FINISH_OFF;
1161         }
1162
1163         /* insert a rule if there exists a rule */
1164         if (rule) {
1165                 local_rule_stream = em_convert_rule_to_byte_stream(rule, &size);
1166                 if (!local_rule_stream) { /*prevent 26265*/
1167                         EM_DEBUG_EXCEPTION("em_convert_rule_to_byte_stream failed");
1168                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1169                         goto FINISH_OFF;
1170                 }
1171
1172         }
1173
1174 FINISH_OFF:
1175
1176         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1177                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1178
1179         if (err == EMAIL_ERROR_NONE) {
1180                 if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_rule_stream, size))
1181                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1182         }
1183
1184         if (!emipc_execute_stub_api(a_hAPI)) {
1185                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1186         }
1187
1188         if (rule) {
1189                 emcore_free_rule(rule);
1190                 free(rule);
1191         }
1192
1193         EM_SAFE_FREE(local_rule_stream);        
1194     EM_SAFE_FREE(multi_user_name);
1195
1196         EM_DEBUG_FUNC_END();
1197 }
1198
1199 /* obsolete - there is no api calling this function */
1200 void stb_get_rule_list(HIPC_API a_hAPI)
1201 {
1202         EM_DEBUG_FUNC_BEGIN();
1203         int err = EMAIL_ERROR_NONE;
1204         int i = 0;
1205         char* local_stream = NULL;
1206         int count = 0;
1207         int size = 0;
1208         email_rule_t* filtering_list = NULL;
1209     int nAPPID = emipc_get_app_id(a_hAPI);
1210     char *multi_user_name = NULL;
1211
1212     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1213         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1214         multi_user_name = NULL;
1215     }
1216
1217         emdaemon_get_filter_list(multi_user_name, &filtering_list, &count, &err);
1218
1219         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1220                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1221
1222         /* insert rules if there exist rules*/
1223         if( count > 0 ) {
1224                 EM_DEBUG_LOG("num of rules [%d]", count);
1225                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
1226                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1227
1228                 for(i=0; i<count; i++) {
1229                         EM_DEBUG_LOG("Value - %s", filtering_list[i].value);
1230
1231                         local_stream = em_convert_rule_to_byte_stream(filtering_list+i, &size);
1232
1233                         if(!local_stream) break;
1234
1235                         if(!emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
1236                                 EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed  ");
1237
1238                         size = 0;
1239                 }
1240         }
1241
1242         if (!emipc_execute_stub_api(a_hAPI)) {
1243                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1244         }
1245
1246     EM_SAFE_FREE(multi_user_name);
1247         EM_DEBUG_FUNC_END();
1248 }
1249
1250 /* obsolete - there is no api calling this function */
1251 void stb_find_rule(HIPC_API a_hAPI)
1252 {
1253         EM_DEBUG_FUNC_BEGIN();
1254         int buffer_size = 0;
1255         int err = EMAIL_ERROR_NONE;
1256         char* local_rule_stream = NULL;
1257         email_rule_t rule = { 0 };
1258     int nAPPID = emipc_get_app_id(a_hAPI);
1259     char *multi_user_name = NULL;
1260
1261     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1262         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1263         multi_user_name = NULL;
1264     }
1265
1266         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
1267         if(buffer_size > 0)      {
1268                 local_rule_stream = (char*)em_malloc(buffer_size);
1269                 if (local_rule_stream == NULL) {
1270                         EM_DEBUG_EXCEPTION("Out of memory");
1271                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1272                         goto FINISH_OFF;
1273                 }
1274
1275                 if (local_rule_stream) {
1276                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, buffer_size, local_rule_stream);
1277                         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
1278                         EM_SAFE_FREE(local_rule_stream);
1279                         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
1280
1281                         if (!emdaemon_find_filter(multi_user_name, &rule, &err)) {
1282                                 EM_DEBUG_EXCEPTION("emdaemon_find_filter failed : [%d]", err);
1283                         }
1284                 }
1285         }
1286
1287 FINISH_OFF:
1288
1289         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1290                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1291
1292         if (!emipc_execute_stub_api(a_hAPI))
1293                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1294
1295     EM_SAFE_FREE(multi_user_name);
1296         EM_DEBUG_FUNC_END();
1297 }
1298
1299 void stb_update_rule(HIPC_API a_hAPI)
1300 {
1301         EM_DEBUG_FUNC_BEGIN();
1302
1303         int filter_id = 0;
1304         int buffer_size = 0;
1305         int err = EMAIL_ERROR_NONE;
1306         int *ret_nth_value = NULL;
1307         char* rule_stream = NULL;
1308         email_rule_t rule = {0};
1309     int nAPPID = emipc_get_app_id(a_hAPI);
1310     char *multi_user_name = NULL;
1311
1312     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1313         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1314         multi_user_name = NULL;
1315     }
1316
1317         /* get filter_id */
1318         if ((ret_nth_value = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0))) {
1319                 filter_id = *ret_nth_value;
1320         } else {
1321                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1322                 goto FINISH_OFF;
1323         }
1324
1325         /* get rule */
1326         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
1327         EM_DEBUG_LOG("size [%d]", buffer_size);
1328         if(buffer_size <= 0)  {
1329                 err = EMAIL_ERROR_INVALID_PARAM;
1330                 goto FINISH_OFF;
1331         }
1332         rule_stream     = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
1333         if(!rule_stream) {
1334                 err = EMAIL_ERROR_INVALID_PARAM;
1335                 goto FINISH_OFF;
1336         }
1337         em_convert_byte_stream_to_rule(rule_stream, buffer_size, &rule);
1338
1339         /* call update handler */
1340         emdaemon_update_filter(multi_user_name, filter_id, &rule, &err);
1341
1342 FINISH_OFF:
1343         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1344                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1345
1346         if (!emipc_execute_stub_api(a_hAPI))
1347                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1348
1349         emcore_free_rule(&rule);
1350
1351     EM_SAFE_FREE(multi_user_name);
1352         EM_DEBUG_FUNC_END();
1353 }
1354
1355 void stb_move_all_mails(HIPC_API a_hAPI)
1356 {
1357         EM_DEBUG_FUNC_BEGIN();
1358         int err = EMAIL_ERROR_NONE;
1359         int src_mailbox_id = 0, dst_mailbox_id = 0;
1360     int nAPPID = emipc_get_app_id(a_hAPI);
1361     char *multi_user_name = NULL;
1362
1363     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1364         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1365         multi_user_name = NULL;
1366     }
1367
1368         /* src_mailbox_id */
1369         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &src_mailbox_id);
1370         EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1371
1372         if (src_mailbox_id > 0)
1373                 EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1374         else
1375                 EM_DEBUG_LOG("src_mailbox_id == 0");
1376
1377         /* dst_mailbox_id */
1378         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &dst_mailbox_id);
1379         EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1380
1381         if (dst_mailbox_id > 0)
1382                 EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1383         else
1384                 EM_DEBUG_LOG("dst_mailbox_id == 0");
1385
1386         if(emdaemon_move_mail_all_mails(multi_user_name, src_mailbox_id, dst_mailbox_id, &err))
1387                 EM_DEBUG_LOG("emdaemon_move_mail_all_mails:Success");
1388
1389         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1390                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1391         }
1392
1393         if (!emipc_execute_stub_api(a_hAPI)) {
1394                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1395         }
1396
1397     EM_SAFE_FREE(multi_user_name);
1398         EM_DEBUG_FUNC_END();
1399 }
1400
1401 void stb_set_flags_field(HIPC_API a_hAPI)
1402 {
1403         EM_DEBUG_FUNC_BEGIN();
1404         int err = EMAIL_ERROR_NONE;
1405         email_flags_field_type field_type = 0;
1406         int account_id;
1407         int value = 0;
1408         int onserver = 0;
1409         int num = 0;
1410         int counter = 0;
1411         int *mail_ids = NULL;
1412     int nAPPID = emipc_get_app_id(a_hAPI);
1413     char *multi_user_name = NULL;
1414
1415     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1416         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1417         multi_user_name = NULL;
1418     }
1419
1420         /* account_id */
1421         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1422         EM_DEBUG_LOG("account_id [%d]", account_id);
1423
1424         /* Number of mail_ids */
1425         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
1426         EM_DEBUG_LOG("number of mails [%d]", num);
1427
1428         /* mail_id */
1429         mail_ids = em_malloc(sizeof(int) * num);
1430
1431         if(!mail_ids) {
1432                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1433                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1434                 goto FINISH_OFF;
1435         }
1436
1437         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
1438
1439         for(counter=0; counter < num; counter++)
1440                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1441
1442         /* field type */
1443         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &field_type);
1444         EM_DEBUG_LOG("field_type [%d]", field_type);
1445
1446         /* value */
1447         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &value);
1448         EM_DEBUG_LOG("value [%d]", value);
1449
1450         /*  on server */
1451         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 5, sizeof(int), &onserver);
1452         EM_DEBUG_LOG("onserver [%d]", onserver);
1453
1454         if(emdaemon_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, onserver, &err))
1455                 EM_DEBUG_LOG("emdaemon_set_flags_field - success");
1456
1457 FINISH_OFF:
1458
1459         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1460                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1461
1462         if (!emipc_execute_stub_api(a_hAPI))
1463                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1464
1465         if (mail_ids)
1466                 EM_SAFE_FREE(mail_ids);
1467
1468     EM_SAFE_FREE(multi_user_name);
1469         EM_DEBUG_FUNC_END();
1470 }
1471
1472 void stb_add_mail(HIPC_API a_hAPI)
1473 {
1474         EM_DEBUG_FUNC_BEGIN();
1475         int buffer_size = 0;
1476         int local_result = 0;
1477         int result_attachment_data_count = 0;
1478         int param_index = 0;
1479         int sync_server = 0;
1480         int err = EMAIL_ERROR_NONE;
1481         int i = 0;
1482         email_mail_data_t result_mail_data = {0};
1483         email_attachment_data_t *result_attachment_data = NULL;
1484         email_meeting_request_t result_meeting_request = {0};
1485         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
1486         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1487     int nAPPID = emipc_get_app_id(a_hAPI);
1488     char *multi_user_name = NULL;
1489     char *prefix_path = NULL;
1490     char real_file_path[255] = {0};
1491
1492     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1493         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1494         multi_user_name = NULL;
1495     }
1496
1497         /* email_mail_data_t */;
1498         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1499         EM_DEBUG_LOG("email_mail_data_t buffer_size[%d]", buffer_size);
1500
1501         /* mail_data */
1502         if(buffer_size > 0)      {
1503                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1504                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1505         }
1506
1507         if (em_get_account_server_type_by_account_id(multi_user_name, result_mail_data.account_id, &account_server_type, true, &err) == false) {
1508                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1509                 goto FINISH_OFF;
1510         }
1511
1512     /* Get the absolute path */
1513     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1514                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1515                 if (err != EMAIL_ERROR_NONE) {
1516                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
1517                         goto FINISH_OFF;
1518                 }
1519         } else {
1520                 prefix_path = strdup("");
1521         }
1522
1523         /* check smack rule for accessing file path */
1524         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_html) {
1525         memset(real_file_path, 0x00, sizeof(real_file_path));
1526         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
1527
1528                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1529                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1530                         err = EMAIL_ERROR_NO_SMACK_RULE;
1531                         goto FINISH_OFF;
1532                 }
1533         }
1534
1535         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_plain) {
1536         memset(real_file_path, 0x00, sizeof(real_file_path));
1537         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
1538
1539                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1540                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1541                         err = EMAIL_ERROR_NO_SMACK_RULE;
1542                         goto FINISH_OFF;
1543                 }
1544         }
1545
1546         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_mime_entity) {
1547         memset(real_file_path, 0x00, sizeof(real_file_path));
1548         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
1549
1550                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1551                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1552                         err = EMAIL_ERROR_NO_SMACK_RULE;
1553                         goto FINISH_OFF;
1554                 }
1555         }
1556         /* check smack rule - END */
1557
1558         /* attachment */
1559         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1560         EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1561
1562         if(buffer_size > 0)      {
1563                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1564                 em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1565
1566                 EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1567                 if (!result_attachment_data_count) {
1568                         EM_DEBUG_LOG("Not include attachment data");
1569                 } else {
1570                         if(!result_attachment_data) {
1571                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1572                                 err = EMAIL_ERROR_ON_PARSING;
1573                                 goto FINISH_OFF;
1574                         }
1575                 }
1576         }
1577         param_index++;
1578
1579         /* check smack rule for accessing file path */
1580         for (i = 0; i < result_attachment_data_count ; i++) {
1581                 if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_attachment_data[i].attachment_path && result_attachment_data[i].save_status) {
1582             memset(real_file_path, 0x00, sizeof(real_file_path));
1583             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
1584
1585                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1586                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1587                                 err = EMAIL_ERROR_NO_SMACK_RULE;
1588                                 goto FINISH_OFF;
1589                         }
1590                 }
1591         }
1592
1593         /* meeting request */
1594         EM_DEBUG_LOG("email_meeting_request_t");
1595         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1596                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1597                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1598                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1599
1600                 if(buffer_size > 0) {
1601                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1602                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1603                 }
1604         }
1605
1606         EM_DEBUG_LOG("sync_server");
1607         emipc_get_parameter(a_hAPI, ePARAMETER_IN, param_index++, sizeof(int), &sync_server);
1608
1609         if( (err = emdaemon_add_mail(multi_user_name, &result_mail_data, result_attachment_data, result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1610                 EM_DEBUG_EXCEPTION("emdaemon_add_mail failed [%d]", err);
1611                 goto FINISH_OFF;
1612         }
1613
1614         local_result = 1;
1615         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1616                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1617         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1618                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1619         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1620                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1621         if (!emipc_execute_stub_api(a_hAPI))
1622                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1623
1624 FINISH_OFF:
1625         if ( local_result == 0 ) {
1626                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1627                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1628                 if (!emipc_execute_stub_api(a_hAPI))
1629                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1630         }
1631
1632         EM_SAFE_FREE(prefix_path);
1633
1634         emcore_free_mail_data(&result_mail_data);
1635
1636         if(result_attachment_data)
1637                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1638
1639         emstorage_free_meeting_request(&result_meeting_request);
1640
1641     EM_SAFE_FREE(multi_user_name);
1642         EM_DEBUG_FUNC_END();
1643 }
1644
1645
1646 void stb_update_mail(HIPC_API a_hAPI)
1647 {
1648         EM_DEBUG_FUNC_BEGIN();
1649         int  buffer_size = 0;
1650         int  local_result = 0;
1651         int  result_attachment_data_count = 0;
1652         int  param_index = 0;
1653         int  sync_server = 0;
1654         int  *temp_buffer = NULL;
1655         int  err = EMAIL_ERROR_NONE;
1656         int  i = 0;
1657         email_mail_data_t result_mail_data = {0};
1658         email_attachment_data_t *result_attachment_data = NULL;
1659         email_meeting_request_t result_meeting_request = {0};
1660         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
1661     int nAPPID = emipc_get_app_id(a_hAPI);
1662     char *multi_user_name = NULL;
1663     char *prefix_path = NULL;
1664     char real_file_path[255] = {0};
1665
1666     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1667         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1668         multi_user_name = NULL;
1669     }
1670
1671         EM_DEBUG_LOG("email_mail_data_t");
1672         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1673
1674         if(buffer_size > 0)      {
1675                 char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1676                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1677         }
1678
1679     /* Get the absolute path */
1680     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1681                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1682                 if (err != EMAIL_ERROR_NONE) {
1683                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
1684                         goto FINISH_OFF;
1685                 }
1686         } else {
1687                 prefix_path = strdup("");
1688         }
1689
1690         /* check smack rule for accessing file path */
1691         if (result_mail_data.file_path_html) {
1692         memset(real_file_path, 0x00, sizeof(real_file_path));
1693         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
1694
1695                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1696                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1697                         err = EMAIL_ERROR_NO_SMACK_RULE;
1698                         goto FINISH_OFF;
1699                 }
1700         }
1701
1702         if (result_mail_data.file_path_plain) {
1703         memset(real_file_path, 0x00, sizeof(real_file_path));
1704         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
1705
1706                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1707                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1708                         err = EMAIL_ERROR_NO_SMACK_RULE;
1709                         goto FINISH_OFF;
1710                 }
1711         }
1712
1713         if (result_mail_data.file_path_mime_entity) {
1714         memset(real_file_path, 0x00, sizeof(real_file_path));
1715         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
1716
1717                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1718                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1719                         err = EMAIL_ERROR_NO_SMACK_RULE;
1720                         goto FINISH_OFF;
1721                 }
1722         }
1723         /* check smack rule - END */
1724         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1725         if(buffer_size > 0)      {
1726                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1727                 em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1728
1729                 EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1730                 if (!result_attachment_data_count) {
1731                         EM_DEBUG_LOG("Not include attachment data");
1732                 } else {
1733                         if(!result_attachment_data) {
1734                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1735                                 err = EMAIL_ERROR_ON_PARSING;
1736                                 goto FINISH_OFF;
1737                         }
1738                 }
1739         }
1740         param_index++;
1741
1742         /* check smack rule for accessing file path */
1743         for (i = 0; i < result_attachment_data_count ; i++) {
1744                 if (result_attachment_data[i].attachment_path) {
1745             memset(real_file_path, 0x00, sizeof(real_file_path));
1746             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
1747
1748                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1749                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1750                                 err = EMAIL_ERROR_NO_SMACK_RULE;
1751                                 goto FINISH_OFF;
1752                         }
1753                 }
1754         }
1755
1756         EM_DEBUG_LOG("email_meeting_request_t");
1757         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1758                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1759                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1760                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1761
1762                 if(buffer_size > 0) {
1763                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1764                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1765                 }
1766         }
1767
1768         EM_DEBUG_LOG("sync_server");
1769
1770         temp_buffer = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1771
1772         if(!temp_buffer) {
1773                 EM_DEBUG_EXCEPTION("emipc_get_nth_parameter_data[%d] failed", param_index - 1);
1774                 err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1775                 goto FINISH_OFF;
1776         }
1777
1778         sync_server = *temp_buffer;
1779
1780         if( (err = emdaemon_update_mail(multi_user_name, &result_mail_data, result_attachment_data,
1781                         result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1782                 EM_DEBUG_EXCEPTION("emdaemon_update_mail failed [%d]", err);
1783                 goto FINISH_OFF;
1784         }
1785
1786         local_result = 1;
1787         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1788                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1789         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1790                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1791         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1792                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1793         if (!emipc_execute_stub_api(a_hAPI))
1794                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1795
1796 FINISH_OFF:
1797         if ( local_result == 0 ) {
1798                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1799                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1800                 if (!emipc_execute_stub_api(a_hAPI))
1801                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1802         }
1803
1804         emcore_free_mail_data(&result_mail_data);
1805
1806         EM_SAFE_FREE(prefix_path);
1807
1808         if(result_attachment_data)
1809                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1810
1811         emstorage_free_meeting_request(&result_meeting_request);
1812
1813     EM_SAFE_FREE(multi_user_name);
1814         EM_DEBUG_FUNC_END();
1815 }
1816
1817
1818 void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
1819 {
1820         EM_DEBUG_FUNC_BEGIN();
1821         int mailbox_id = 0, thread_id = 0, move_always_flag = 0;
1822         int err = EMAIL_ERROR_NONE;
1823         char *target_mailbox_name = NULL;
1824     int nAPPID = emipc_get_app_id(a_hAPI);
1825     char *multi_user_name = NULL;
1826
1827     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1828         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1829         multi_user_name = NULL;
1830     }
1831
1832         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1833         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1834
1835         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1836         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1837
1838         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &move_always_flag);
1839         EM_DEBUG_LOG("move_always_flag [%d]", move_always_flag);
1840
1841         if(emdaemon_move_mail_thread_to_mailbox(multi_user_name, thread_id, mailbox_id, move_always_flag, &err))
1842                 EM_DEBUG_LOG("emdaemon_move_mail_thread_to_mailbox success");
1843
1844         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1845                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1846                 EM_SAFE_FREE(target_mailbox_name);
1847                 EM_SAFE_FREE(multi_user_name);
1848                 return;
1849         }
1850
1851         if (!emipc_execute_stub_api(a_hAPI)) {
1852                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1853                 EM_SAFE_FREE(target_mailbox_name);
1854                 EM_SAFE_FREE(multi_user_name);
1855                 return;
1856         }
1857
1858         EM_SAFE_FREE(target_mailbox_name);
1859     EM_SAFE_FREE(multi_user_name);
1860         EM_DEBUG_FUNC_END();
1861 }
1862
1863 void stb_delete_thread(HIPC_API a_hAPI)
1864 {
1865         EM_DEBUG_FUNC_BEGIN();
1866
1867         int thread_id = 0, delete_always_flag = 0;
1868         int handle = 0;
1869         int err = EMAIL_ERROR_NONE;
1870     int nAPPID = emipc_get_app_id(a_hAPI);
1871     char *multi_user_name = NULL;
1872
1873     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1874         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1875         multi_user_name = NULL;
1876     }
1877
1878         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1879         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1880
1881         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &delete_always_flag);
1882         EM_DEBUG_LOG("delete_always_flag [%d]", delete_always_flag);
1883
1884         if(emdaemon_delete_mail_thread(multi_user_name, thread_id, delete_always_flag, &handle, &err))
1885                 EM_DEBUG_LOG("emdaemon_delete_mail_thread success");
1886
1887         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1888                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1889                 EM_SAFE_FREE(multi_user_name);
1890                 return;
1891         }
1892
1893         if (!emipc_execute_stub_api(a_hAPI)) {
1894                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1895                 EM_SAFE_FREE(multi_user_name);
1896                 return;
1897         }
1898
1899     EM_SAFE_FREE(multi_user_name);
1900         EM_DEBUG_FUNC_END();
1901 }
1902
1903 void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
1904 {
1905         EM_DEBUG_FUNC_BEGIN();
1906
1907         int thread_id = 0, seen_flag = 0, on_server = 0;
1908         int handle = 0;
1909         int err = EMAIL_ERROR_NONE;
1910     int nAPPID = emipc_get_app_id(a_hAPI);
1911     char *multi_user_name = NULL;
1912
1913     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1914         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1915         multi_user_name = NULL;
1916     }
1917
1918         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1919         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1920
1921         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &seen_flag);
1922         EM_DEBUG_LOG("seen_flag [%d]", seen_flag);
1923
1924         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &on_server);
1925         EM_DEBUG_LOG("on_server [%d]", on_server);
1926
1927         if(emdaemon_modify_seen_flag_of_thread(multi_user_name, thread_id, seen_flag, on_server, &handle, &err))
1928                 EM_DEBUG_LOG("emdaemon_modify_seen_flag_of_thread success");
1929
1930         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1931                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1932                 EM_SAFE_FREE(multi_user_name);
1933                 return;
1934         }
1935
1936         if (!emipc_execute_stub_api(a_hAPI)) {
1937                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1938                 EM_SAFE_FREE(multi_user_name);
1939                 return;
1940         }
1941
1942     EM_SAFE_FREE(multi_user_name);
1943         EM_DEBUG_FUNC_END();
1944 }
1945
1946 void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
1947 {
1948         EM_DEBUG_FUNC_BEGIN();
1949
1950         int mailbox_id = 0, on_server = 0;
1951         int handle = 0;
1952         int err = EMAIL_ERROR_NONE;
1953     int nAPPID = emipc_get_app_id(a_hAPI);
1954     char *multi_user_name = NULL;
1955
1956     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1957         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1958                 multi_user_name = NULL;
1959     }
1960
1961         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), (void*)&mailbox_id);
1962         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1963
1964         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
1965         EM_DEBUG_LOG("on_server [%d]", on_server);
1966
1967         if( (err = emdaemon_expunge_mails_deleted_flagged(multi_user_name, mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
1968                 EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
1969
1970         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1971                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1972         }
1973
1974         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
1975                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1976         }
1977
1978         if (!emipc_execute_stub_api(a_hAPI)) {
1979                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1980         }
1981
1982     EM_SAFE_FREE(multi_user_name);
1983         EM_DEBUG_FUNC_END();
1984 }
1985
1986 void stb_move_mail(HIPC_API a_hAPI)
1987 {
1988         int err = EMAIL_ERROR_NONE;
1989         int num = 0, counter = 0, mailbox_id = 0;
1990     int nAPPID = emipc_get_app_id(a_hAPI);
1991     char *multi_user_name = NULL;
1992
1993     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1994         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1995         multi_user_name = NULL;
1996     }
1997
1998         /* Number of mail_ids */
1999         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &num);
2000         EM_DEBUG_LOG("number of mails [%d]", num);
2001
2002         /* mail_id */
2003         int mail_ids[num];
2004         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, num * sizeof(int), mail_ids);
2005
2006         for(counter = 0; counter < num; counter++)
2007                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
2008
2009         /* target_mailbox_id */
2010         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mailbox_id);
2011         EM_DEBUG_LOG("target_mailbox_id [%d]", mailbox_id);
2012
2013         if (mailbox_id > 0)
2014                 EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
2015         else
2016                 EM_DEBUG_LOG("mailbox_id == 0");
2017
2018         if(emdaemon_move_mail(multi_user_name, mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
2019                 EM_DEBUG_LOG("emdaemon_move_mail success");
2020
2021         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2022                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
2023
2024         if (!emipc_execute_stub_api(a_hAPI))
2025                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
2026
2027     EM_SAFE_FREE(multi_user_name);
2028         EM_DEBUG_FUNC_END();
2029 }
2030
2031 void stb_delete_rule(HIPC_API a_hAPI)
2032 {
2033         EM_DEBUG_FUNC_BEGIN();
2034
2035         int filter_id = 0;
2036         int err = EMAIL_ERROR_NONE;
2037     int nAPPID = emipc_get_app_id(a_hAPI);
2038     char *multi_user_name = NULL;
2039
2040     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2041         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2042         multi_user_name = NULL;
2043     }
2044
2045         /* filter_id */
2046         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
2047
2048         if(emdaemon_delete_filter(multi_user_name, filter_id, &err))
2049                 err = EMAIL_ERROR_NONE;
2050
2051         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2052                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2053
2054         if (!emipc_execute_stub_api(a_hAPI))
2055                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2056
2057     EM_SAFE_FREE(multi_user_name);
2058         EM_DEBUG_FUNC_END();
2059 }
2060
2061 void stb_apply_rule(HIPC_API a_hAPI)
2062 {
2063         EM_DEBUG_FUNC_BEGIN();
2064
2065         int filter_id = 0;
2066         int err = EMAIL_ERROR_NONE;
2067     int nAPPID = emipc_get_app_id(a_hAPI);
2068     char *multi_user_name = NULL;
2069
2070     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2071         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2072         multi_user_name = NULL;
2073     }
2074
2075         /* filter_id */
2076         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
2077
2078         if(emdaemon_apply_filter(multi_user_name, filter_id, &err))
2079                 err = EMAIL_ERROR_NONE;
2080
2081         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2082                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2083
2084         if (!emipc_execute_stub_api(a_hAPI))
2085                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2086
2087     EM_SAFE_FREE(multi_user_name);
2088         EM_DEBUG_FUNC_END();
2089 }
2090
2091 void stb_add_attachment(HIPC_API a_hAPI)
2092 {
2093         EM_DEBUG_FUNC_BEGIN();
2094         int buffer_size = 0;
2095         int err = EMAIL_ERROR_NONE;
2096         int mail_id = -1;
2097         int attachment_count = 0;
2098         int i = 0;
2099         char* attachment_stream = NULL;
2100         email_attachment_data_t* attachment = NULL;
2101         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
2102     int nAPPID = emipc_get_app_id(a_hAPI);
2103     char *multi_user_name = NULL;
2104     char *prefix_path = NULL;
2105     char real_file_path[255] = {0};
2106
2107     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2108         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2109         multi_user_name = NULL;
2110     }
2111
2112         /* mail_id */
2113         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2114
2115         /* attachment */
2116         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2117
2118         if(buffer_size > 0)      {
2119                 attachment_stream = (char*)em_malloc(buffer_size);
2120                 if (attachment_stream == NULL) {
2121                         EM_DEBUG_EXCEPTION("em_malloc failed");
2122                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2123                         goto FINISH_OFF;
2124                 }
2125
2126                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, attachment_stream);
2127                 em_convert_byte_stream_to_attachment_data(attachment_stream, buffer_size, &attachment, &attachment_count);
2128                 EM_SAFE_FREE(attachment_stream);
2129         }
2130
2131         if (!attachment) {
2132                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed  ");
2133                 err = EMAIL_ERROR_ON_PARSING;
2134                 goto FINISH_OFF;
2135         }
2136
2137     /* Get the absolute path */
2138     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2139                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2140                 if (err != EMAIL_ERROR_NONE) {
2141                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
2142                         goto FINISH_OFF;
2143                 }
2144         } else {
2145                 prefix_path = strdup("");
2146         }
2147
2148         /* check smack rule for accessing file path */
2149         for (i = 0; i < attachment_count ; i++) {
2150                 if (attachment[i].attachment_path) {
2151             memset(real_file_path, 0x00, sizeof(real_file_path));
2152             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment[i].attachment_path);
2153
2154                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
2155                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
2156                                 err = EMAIL_ERROR_NO_SMACK_RULE;
2157                                 goto FINISH_OFF;
2158                         }
2159                 }
2160         }
2161
2162         emdaemon_add_attachment(multi_user_name, mail_id, attachment, &err);
2163
2164 FINISH_OFF:
2165
2166         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2167                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2168         if(EMAIL_ERROR_NONE == err) {
2169                 EM_DEBUG_LOG("emdaemon_add_attachment -Success");
2170
2171                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(attachment->attachment_id), sizeof(int)))
2172                         EM_DEBUG_EXCEPTION("emipc_add_parameter attachment_id failed ");
2173         }
2174                 if (!emipc_execute_stub_api(a_hAPI))
2175                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2176
2177         EM_SAFE_FREE(attachment);
2178     EM_SAFE_FREE(multi_user_name);
2179         EM_SAFE_FREE(prefix_path);
2180
2181         EM_DEBUG_FUNC_END();
2182 }
2183
2184 void stb_get_attachment(HIPC_API a_hAPI)
2185 {
2186         EM_DEBUG_FUNC_BEGIN();
2187         int err = EMAIL_ERROR_NONE;
2188         int attachment_id;
2189         char* attachment_stream = NULL;
2190         email_attachment_data_t* attachment = NULL;
2191         int size = 0;
2192     int nAPPID = emipc_get_app_id(a_hAPI);
2193     char *multi_user_name = NULL;
2194
2195     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2196         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2197         multi_user_name = NULL;
2198     }
2199
2200         /* attachment_id */
2201         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
2202
2203         emdaemon_get_attachment(multi_user_name, attachment_id, &attachment, &err);
2204
2205         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2206                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2207
2208         if(EMAIL_ERROR_NONE == err) {
2209                 EM_DEBUG_LOG("emdaemon_get_attachment - Success");
2210                 /* attachment */
2211                 attachment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
2212                 if(!attachment_stream) { /*prevent 26263*/
2213                         emcore_free_attachment_data(&attachment, 1, &err);
2214                         EM_SAFE_FREE(multi_user_name);
2215                         return;
2216                 }
2217                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
2218
2219                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, attachment_stream, size))
2220                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
2221         }
2222
2223         if (!emipc_execute_stub_api(a_hAPI)) {
2224                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2225         }
2226
2227         EM_SAFE_FREE(attachment_stream);
2228         emcore_free_attachment_data(&attachment, 1, &err);
2229
2230     EM_SAFE_FREE(multi_user_name);
2231         EM_DEBUG_FUNC_END();
2232 }
2233
2234 void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
2235 {
2236         EM_DEBUG_FUNC_BEGIN();
2237         int err = EMAIL_ERROR_NONE;
2238         int account_id = 0;
2239         int handle = 0;
2240     int nAPPID = emipc_get_app_id(a_hAPI);
2241     char *multi_user_name = NULL;
2242
2243     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2244             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2245             multi_user_name = NULL;
2246     }
2247
2248         /* account_id */
2249         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2250
2251         /*need to check: why err value is changed? */
2252         if(emdaemon_get_imap_mailbox_list(multi_user_name, account_id, "", &handle, &err))
2253                 err = EMAIL_ERROR_NONE;
2254
2255         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2256                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
2257         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2258                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
2259         if (!emipc_execute_stub_api(a_hAPI))
2260                 EM_DEBUG_LOG("emipc_execute_stub_api failed  ");
2261
2262     EM_SAFE_FREE(multi_user_name);
2263         EM_DEBUG_FUNC_END();
2264 }
2265
2266 void stb_delete_attachment(HIPC_API a_hAPI)
2267 {
2268         EM_DEBUG_FUNC_BEGIN();
2269         int err = EMAIL_ERROR_NONE;
2270         int attachment_id = 0;
2271     int nAPPID = emipc_get_app_id(a_hAPI);
2272     char *multi_user_name = NULL;
2273
2274     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2275         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2276         multi_user_name = NULL;
2277     }
2278
2279         /* attachment_index */
2280         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
2281
2282         emdaemon_delete_mail_attachment(multi_user_name, attachment_id, &err);
2283
2284         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2285                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2286         if (!emipc_execute_stub_api(a_hAPI))
2287                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2288
2289     EM_SAFE_FREE(multi_user_name);
2290         EM_DEBUG_FUNC_END();
2291 }
2292
2293 void stb_download_attachment(HIPC_API a_hAPI)
2294 {
2295         EM_DEBUG_FUNC_BEGIN();
2296
2297         int err = EMAIL_ERROR_NONE;
2298         int mail_id = 0;
2299         int nth = 0;
2300         int handle = 0;
2301         int account_id = 0;
2302     int nAPPID = emipc_get_app_id(a_hAPI);
2303     char *multi_user_name = NULL;
2304
2305     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2306         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2307         multi_user_name = NULL;
2308     }
2309
2310         EM_DEBUG_LOG("account_id");
2311         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2312
2313         EM_DEBUG_LOG("mail_id");
2314         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
2315
2316         EM_DEBUG_LOG("nth");
2317         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &nth);
2318
2319         if(emdaemon_download_attachment(multi_user_name, account_id, mail_id, nth, &handle, &err)) {
2320                 err = EMAIL_ERROR_NONE;
2321
2322                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2323                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2324                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
2325                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2326                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
2327                 if (!emipc_execute_stub_api(a_hAPI))
2328                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2329         }
2330         else {
2331                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2332                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2333                 /* Download handle - 17-Apr-09 */
2334                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
2335                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2336                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
2337                 if (!emipc_execute_stub_api(a_hAPI))
2338                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2339         }
2340
2341     EM_SAFE_FREE(multi_user_name);
2342         EM_DEBUG_FUNC_END();
2343 }
2344
2345 void stb_mail_send_saved(HIPC_API a_hAPI)
2346 {
2347         EM_DEBUG_FUNC_BEGIN();
2348         int err = EMAIL_ERROR_NONE;
2349         int account_id = 0;
2350     int nAPPID = emipc_get_app_id(a_hAPI);
2351     char *multi_user_name = NULL;
2352
2353     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2354         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2355         multi_user_name = NULL;
2356     }
2357
2358         /* account_id */
2359         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2360
2361         EM_DEBUG_LOG("calling emdaemon_send_mail_saved");
2362         if(emdaemon_send_mail_saved(multi_user_name, account_id, NULL, &err))
2363                 err = EMAIL_ERROR_NONE;
2364
2365         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2366                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2367
2368         if (!emipc_execute_stub_api(a_hAPI))
2369                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2370
2371     EM_SAFE_FREE(multi_user_name);
2372         EM_DEBUG_FUNC_END();
2373 }
2374
2375 void stb_add_read_receipt(HIPC_API a_hAPI){
2376         EM_DEBUG_FUNC_BEGIN();
2377         int read_mail_id = 0;
2378         int receipt_mail_id = 0;
2379         int err = EMAIL_ERROR_NONE;
2380     int nAPPID = emipc_get_app_id(a_hAPI);
2381     char *multi_user_name = NULL;
2382
2383     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2384         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2385         multi_user_name = NULL;
2386     }
2387
2388         /* read_mail_id */
2389         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &read_mail_id);
2390         EM_DEBUG_LOG("read_mail_id [%d]", read_mail_id);
2391
2392         if( (err = emcore_add_read_receipt(multi_user_name, read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
2393                 EM_DEBUG_EXCEPTION("emcore_add_read_receipt failed [%d]", err);
2394
2395         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2396                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2397
2398         if (err == EMAIL_ERROR_NONE) {
2399                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &receipt_mail_id, sizeof(int)))
2400                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2401         }
2402
2403         if (!emipc_execute_stub_api(a_hAPI))
2404                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2405
2406     EM_SAFE_FREE(multi_user_name);
2407         EM_DEBUG_FUNC_END("err [%d]", err);
2408 }
2409
2410 void stb_retry_sending_mail(HIPC_API a_hAPI)
2411 {
2412
2413         EM_DEBUG_FUNC_BEGIN();
2414         int mail_id = 0;
2415         int timeout_in_sec = 0;
2416         int err = EMAIL_ERROR_NONE;
2417     int nAPPID = emipc_get_app_id(a_hAPI);
2418     char *multi_user_name = NULL;
2419
2420     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2421         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2422         multi_user_name = NULL;
2423     }
2424
2425         /* Mail_id */
2426         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2427         EM_DEBUG_LOG("mail_id [%d]", mail_id);
2428
2429         /* timeout_in_sec */
2430         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &timeout_in_sec);
2431         EM_DEBUG_LOG("timeout_in_sec [%d]", timeout_in_sec);
2432
2433         if(emdaemon_send_mail_retry(multi_user_name, mail_id, timeout_in_sec,&err))
2434                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
2435
2436         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2437                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2438
2439     if (!emipc_execute_stub_api(a_hAPI))
2440             EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2441
2442     EM_SAFE_FREE(multi_user_name);
2443         EM_DEBUG_FUNC_END();
2444 }
2445
2446 void stb_cancel_job(HIPC_API a_hAPI)
2447 {
2448         EM_DEBUG_FUNC_BEGIN();
2449         int account_id = 0;
2450         int handle = 0;
2451         int err = EMAIL_ERROR_NONE;
2452     int nAPPID = emipc_get_app_id(a_hAPI);
2453     char *multi_user_name = NULL;
2454
2455     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2456         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2457         multi_user_name = NULL;
2458     }
2459
2460         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2461         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &handle);
2462         EM_DEBUG_LOG("account_id [%d] handle [%d]", account_id, handle);
2463
2464         if(emdaemon_cancel_job(account_id, handle, &err))
2465                 err = EMAIL_ERROR_NONE;
2466
2467         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2468                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2469         if (!emipc_execute_stub_api(a_hAPI))
2470                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2471
2472     EM_SAFE_FREE(multi_user_name);
2473         EM_DEBUG_FUNC_END();
2474 }
2475
2476 void stb_cancel_send_mail_job(HIPC_API a_hAPI)
2477 {
2478         EM_DEBUG_FUNC_BEGIN();
2479         int mail_id = 0;
2480         int err = EMAIL_ERROR_NONE;
2481         int account_id = 0;
2482     int nAPPID = emipc_get_app_id(a_hAPI);
2483     char *multi_user_name = NULL;
2484
2485     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2486         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2487         multi_user_name = NULL;
2488     }
2489
2490         /* account_id */
2491         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2492         EM_DEBUG_LOG("account_id [%d]", account_id);
2493
2494         /* Mail_id */
2495         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
2496         EM_DEBUG_LOG("mail_id [%d]", mail_id);
2497
2498         if(emdaemon_cancel_sending_mail_job(account_id,mail_id,&err))
2499                 EM_DEBUG_LOG("success");
2500
2501         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2502                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2503
2504         if (!emipc_execute_stub_api(a_hAPI))
2505                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2506
2507     EM_SAFE_FREE(multi_user_name);
2508         EM_DEBUG_FUNC_END();
2509 }
2510
2511 void stb_search_mail_on_server(HIPC_API a_hAPI)
2512 {
2513         EM_DEBUG_FUNC_BEGIN();
2514         int                    err = EMAIL_ERROR_NONE;
2515         int                    account_id = 0;
2516         int                                        mailbox_id = 0;
2517         int                    buffer_size = 0;
2518         int                    search_filter_count = 0;
2519         char                  *stream_for_search_filter_list = NULL;
2520         unsigned int           job_handle = 0;
2521         email_search_filter_t *search_filter_list = NULL;
2522     int nAPPID = emipc_get_app_id(a_hAPI);
2523     char *multi_user_name = NULL;
2524
2525     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2526         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2527         multi_user_name = NULL;
2528     }
2529
2530         /* account_id */
2531         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2532         EM_DEBUG_LOG("account_id [%d]", account_id);
2533
2534         /* mailbox_id */
2535         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
2536         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
2537
2538         /* search_filter_list */
2539         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
2540
2541         if(buffer_size > 0)      {
2542                 stream_for_search_filter_list = (char*)em_malloc(buffer_size);
2543                 if (stream_for_search_filter_list == NULL) {
2544                         EM_DEBUG_EXCEPTION("out of memory");
2545                         EM_SAFE_FREE(multi_user_name);
2546                         return;
2547                 }
2548
2549                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, stream_for_search_filter_list);
2550                 em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, &search_filter_list, &search_filter_count);
2551                 EM_SAFE_FREE(stream_for_search_filter_list);
2552         }
2553
2554         if(!emdaemon_search_mail_on_server(multi_user_name, account_id ,mailbox_id, search_filter_list, search_filter_count, &job_handle, &err))
2555                 EM_DEBUG_LOG("success");
2556
2557         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2558                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2559
2560         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
2561                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2562
2563         if (!emipc_execute_stub_api(a_hAPI))
2564                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2565
2566     EM_SAFE_FREE(multi_user_name);
2567         EM_DEBUG_FUNC_END();
2568 }
2569
2570 void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
2571 {
2572         EM_DEBUG_FUNC_BEGIN();
2573         int                      err = EMAIL_ERROR_NONE;
2574         int                      account_id = 0;
2575         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
2576     int nAPPID = emipc_get_app_id(a_hAPI);
2577     char *multi_user_name = NULL;
2578
2579     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2580         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2581         multi_user_name = NULL;
2582     }
2583
2584         /* account_id */
2585         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2586
2587         EM_DEBUG_LOG("account_id [%d]", account_id);
2588
2589         if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, EMAIL_SEARCH_RESULT_MAILBOX_NAME, &mailbox_tbl, false, &err)) {
2590                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed [%d]", err);
2591                 goto FINISH_OFF;
2592         }
2593
2594         if (!emstorage_delete_mail_by_mailbox(multi_user_name, mailbox_tbl, true, &err))
2595                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
2596
2597 FINISH_OFF:
2598
2599         if (mailbox_tbl) {
2600                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
2601         }
2602
2603         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2604                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2605
2606         if (!emipc_execute_stub_api(a_hAPI))
2607                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2608
2609         EM_SAFE_FREE(multi_user_name);
2610         EM_DEBUG_FUNC_END();
2611 }
2612
2613
2614
2615 void stb_add_account_with_validation(HIPC_API a_hAPI)
2616 {
2617         EM_DEBUG_FUNC_BEGIN();
2618         int buffer_size = 0;
2619         int local_result = 0;
2620         int handle = 0;
2621         char* stream = NULL;
2622         email_account_t *account = NULL;
2623         email_account_t *ref_account = NULL;
2624         int ref_check_interval = 0;
2625         int ref_account_id = 0;
2626         int err = EMAIL_ERROR_NONE;
2627     int nAPPID = emipc_get_app_id(a_hAPI);
2628     char *multi_user_name = NULL;
2629
2630     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2631         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2632         multi_user_name = NULL;
2633     }
2634
2635         /* get account info */
2636         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2637         EM_DEBUG_LOG("size [%d]", buffer_size);
2638         if(buffer_size <= 0) {
2639                 err = EMAIL_ERROR_INVALID_PARAM;
2640                 goto FINISH_OFF;
2641         }
2642
2643         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
2644         if(!stream) {
2645                 err = EMAIL_ERROR_INVALID_PARAM;
2646                 goto FINISH_OFF;
2647         }
2648
2649         account = em_malloc(sizeof(email_account_t));
2650         if (account == NULL) {
2651                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2652                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2653                 goto FINISH_OFF;
2654         }
2655
2656         em_convert_byte_stream_to_account(stream, buffer_size, account);
2657     account->user_name = EM_SAFE_STRDUP(multi_user_name);
2658
2659         if ((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
2660                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
2661                 goto FINISH_OFF;
2662         }
2663
2664         ref_account = emcore_get_account_reference(multi_user_name, account->account_id, false);
2665         if (!ref_account) {
2666                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
2667                 goto FINISH_OFF;
2668         }
2669
2670         ref_check_interval = ref_account->check_interval;
2671         ref_account_id     = ref_account->account_id;
2672
2673         if (!emdaemon_validate_account_and_create(multi_user_name, ref_account, &handle, &err)) {
2674                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_create fail [%d]", err);
2675                 goto FINISH_OFF;
2676         }
2677 #ifdef __FEATURE_AUTO_POLLING__
2678         /*  start auto polling, if check_interval not zero */
2679         if (ref_check_interval > 0 || (ref_account->peak_days > 0 && ref_account->peak_interval > 0)) {
2680                 if(!emdaemon_add_polling_alarm(multi_user_name, ref_account_id))
2681                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
2682         }
2683 #ifdef __FEATURE_IMAP_IDLE__
2684         else if (ref_check_interval == 0 || (ref_account->peak_days > 0 && ref_account->peak_interval == 0))
2685                 emcore_refresh_imap_idle_thread();
2686 #endif /* __FEATURE_IMAP_IDLE__ */
2687 #endif /*  __FEATURE_AUTO_POLLING__ */
2688         /*  add account details to contact DB  */
2689         /*  emdaemon_insert_accountinfo_to_contact(account); */
2690
2691         local_result = 1;
2692         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2693                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2694         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2695                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2696         if (!emipc_execute_stub_api(a_hAPI))
2697                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2698
2699 FINISH_OFF:
2700         if ( local_result == 0 ) { /* there is an error */
2701                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2702                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
2703                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2704                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
2705                 if (!emipc_execute_stub_api(a_hAPI))
2706                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2707         }
2708
2709 /*      note: account is freed in thread_func_branch_command, which is run by other thread */
2710 /*      emcore_free_account(account); */
2711
2712     EM_SAFE_FREE(multi_user_name);
2713         EM_DEBUG_FUNC_END();
2714 }
2715
2716 void stb_backup_account(HIPC_API a_hAPI)
2717 {
2718         EM_DEBUG_FUNC_BEGIN();
2719
2720 #ifdef __FEATURE_BACKUP_ACCOUNT__
2721         char *file_path = NULL;
2722         int local_result = 0, err_code = 0, file_path_length = 0;
2723     int nAPPID = emipc_get_app_id(a_hAPI);
2724     char *multi_user_name = NULL;
2725
2726     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2727             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2728             multi_user_name = NULL;
2729     }
2730
2731         /* file_path_length */
2732         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2733         if(file_path_length > 0) {
2734                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2735                 file_path = em_malloc(file_path_length);
2736                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2737                 EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
2738                 local_result = emcore_backup_accounts(multi_user_name, (const char*)file_path, &err_code);
2739         }
2740
2741         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2742                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2743
2744         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2745                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2746
2747         if (!emipc_execute_stub_api(a_hAPI))
2748                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2749
2750         EM_SAFE_FREE(file_path);
2751     EM_SAFE_FREE(multi_user_name);
2752 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2753         EM_DEBUG_FUNC_END();
2754 }
2755
2756 void stb_restore_account(HIPC_API a_hAPI)
2757 {
2758         EM_DEBUG_FUNC_BEGIN();
2759 #ifdef __FEATURE_BACKUP_ACCOUNT__
2760         char *file_path = NULL;
2761         int result = 0, err_code = 0, file_path_length = 0;
2762     int nAPPID = emipc_get_app_id(a_hAPI);
2763     char *multi_user_name = NULL;
2764
2765     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2766             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2767             multi_user_name = NULL;
2768     }
2769
2770         /* file_path_length */
2771         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2772         if(file_path_length > 0)  {
2773                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2774                 file_path = em_malloc(file_path_length);
2775                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2776                 EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
2777         }
2778
2779         /* file_path could be NULL */
2780         err_code = emcore_restore_accounts(multi_user_name, (const char*)file_path);
2781
2782         result = (err_code == EMAIL_ERROR_NONE)?1:0;
2783
2784         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result, sizeof(int)))
2785                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2786
2787         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2788                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2789
2790         if (!emipc_execute_stub_api(a_hAPI))
2791                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2792
2793         EM_SAFE_FREE(file_path);
2794     EM_SAFE_FREE(multi_user_name);
2795 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2796         EM_DEBUG_FUNC_END();
2797 }
2798
2799
2800 void stb_get_password_length(HIPC_API a_hAPI)
2801 {
2802         EM_DEBUG_FUNC_BEGIN();
2803         int account_id = 0;
2804         int password_type = 0;
2805         int local_result = 0;
2806         int err_code = 0;
2807         int password_length = 0;
2808     int nAPPID = emipc_get_app_id(a_hAPI);
2809     char *multi_user_name = NULL;
2810
2811     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2812             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2813             multi_user_name = NULL;
2814     }
2815
2816         /* account_id */
2817         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2818         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &password_type);
2819
2820         local_result = emstorage_get_password_length_of_account(multi_user_name, account_id, password_type, &password_length,&err_code);
2821
2822         EM_DEBUG_LOG("password_length [%d]", password_length);
2823
2824         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2825                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2826
2827         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2828                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2829
2830         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &password_length, sizeof(int)))
2831                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2832
2833         if (!emipc_execute_stub_api(a_hAPI))
2834                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2835
2836     EM_SAFE_FREE(multi_user_name);
2837         EM_DEBUG_FUNC_END();
2838 }
2839
2840 void stb_get_task_information(HIPC_API a_hAPI)
2841 {
2842         EM_DEBUG_FUNC_BEGIN();
2843         int err = 0;
2844         int task_information_count = 0;
2845         int stream_length;
2846         email_task_information_t *task_information = NULL;
2847         char *task_information_stream = NULL;
2848     int nAPPID = emipc_get_app_id(a_hAPI);
2849     char *multi_user_name = NULL;
2850
2851     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2852         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2853         multi_user_name = NULL;
2854     }
2855
2856         err = emcore_get_task_information(&task_information, &task_information_count);
2857
2858         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2859                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2860
2861         /* email_task_information_t */
2862         if(task_information_count) {
2863                 task_information_stream = em_convert_task_information_to_byte_stream(task_information, task_information_count, &stream_length);
2864
2865                 if((stream_length > 0) && !emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, task_information_stream, stream_length)) {
2866                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
2867                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2868                 }
2869         }
2870
2871         if (!emipc_execute_stub_api(a_hAPI))
2872                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2873
2874         if(task_information) {
2875                 EM_SAFE_FREE(task_information);
2876         }
2877
2878     EM_SAFE_FREE(multi_user_name);
2879         EM_DEBUG_FUNC_END();
2880 }
2881
2882 void stb_add_certificate(HIPC_API a_hAPI)
2883 {
2884         int err = EMAIL_ERROR_NONE;
2885         int cert_file_len = 0;
2886         int email_address_len = 0;
2887         char *cert_file_path = NULL;
2888         char *email_address = NULL;
2889         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
2890     int nAPPID = emipc_get_app_id(a_hAPI);
2891     char *multi_user_name = NULL;
2892     char *prefix_path = NULL;
2893     char real_file_path[255] = {0};
2894
2895     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2896         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2897         multi_user_name = NULL;
2898     }
2899
2900     /* Get the absolute path */
2901     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2902                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2903                 if (err != EMAIL_ERROR_NONE) {
2904                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
2905                         goto FINISH_OFF;
2906                 }
2907         } else {
2908                 prefix_path = strdup("");
2909         }
2910
2911         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2912         if (cert_file_len > 0) {
2913                 cert_file_path = em_malloc(cert_file_len + 1);
2914                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2915         }
2916
2917         /* check smack rule for accessing file path */
2918         if (cert_file_path) {
2919         memset(real_file_path, 0x00, sizeof(real_file_path));
2920         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, cert_file_path);
2921
2922                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
2923                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
2924                         err = EMAIL_ERROR_NO_SMACK_RULE;
2925                         goto FINISH_OFF;
2926                 }
2927         }
2928
2929         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2930         if (email_address_len > 0) {
2931                 email_address = em_malloc(email_address_len + 1);
2932                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, email_address_len, email_address);
2933         }
2934
2935         if (!emcore_add_public_certificate(multi_user_name, cert_file_path, email_address, &err)) {
2936                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2937         }
2938
2939 FINISH_OFF:
2940
2941         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2942                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2943
2944         if (EMAIL_ERROR_NONE == err) {
2945                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2946         }
2947
2948         if (!emipc_execute_stub_api(a_hAPI))
2949                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2950
2951         EM_SAFE_FREE(prefix_path);
2952         EM_SAFE_FREE(cert_file_path);
2953         EM_SAFE_FREE(email_address);
2954     EM_SAFE_FREE(multi_user_name);
2955         EM_DEBUG_FUNC_END();
2956 }
2957
2958 void stb_delete_certificate(HIPC_API a_hAPI)
2959 {
2960         int err = EMAIL_ERROR_NONE;
2961         int email_address_len = 0;
2962         char *email_address = NULL;
2963         char temp_email_address[130] = {0, };
2964     int nAPPID = emipc_get_app_id(a_hAPI);
2965     char *multi_user_name = NULL;
2966
2967     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2968         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2969         multi_user_name = NULL;
2970     }
2971
2972         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2973         if (email_address_len > 0) {
2974                 EM_DEBUG_LOG("email address string length [%d]", email_address_len);
2975                 email_address = em_malloc(email_address_len + 1);
2976                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, email_address_len, email_address);
2977                 EM_DEBUG_LOG_SEC("email address [%s]", email_address);
2978         }
2979
2980         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
2981         if (!emcore_delete_public_certificate(multi_user_name, temp_email_address, &err)) {
2982                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2983         }
2984
2985         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2986                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2987
2988         if (EMAIL_ERROR_NONE == err) {
2989                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2990         }
2991
2992         if (!emipc_execute_stub_api(a_hAPI))
2993                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2994
2995         
2996         EM_SAFE_FREE(email_address);
2997     EM_SAFE_FREE(multi_user_name);
2998         EM_DEBUG_FUNC_END();    
2999 }
3000
3001 void stb_verify_signature(HIPC_API a_hAPI)
3002 {
3003         int err = EMAIL_ERROR_NONE;
3004         int verify = 0;
3005         int mail_id = 0;
3006         int count = 0;
3007         int attachment_tbl_count = 0;
3008         email_mail_data_t *mail_data = NULL;
3009         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
3010     int nAPPID = emipc_get_app_id(a_hAPI);
3011     char *multi_user_name = NULL;
3012
3013     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3014         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3015         multi_user_name = NULL;
3016     }
3017
3018         err = emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
3019         if (err != EMAIL_ERROR_NONE) {
3020                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
3021                 goto FINISH_OFF;
3022         }
3023
3024         if (!emcore_get_mail_data(multi_user_name, mail_id, &mail_data)) {
3025                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
3026                 goto FINISH_OFF;
3027         }
3028
3029         if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
3030                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed : [%d]", err);
3031                 goto FINISH_OFF;
3032         }
3033
3034         if (attachment_tbl_count <= 0) {
3035                 EM_DEBUG_EXCEPTION("Invalid signed mail");
3036                 err = EMAIL_ERROR_INVALID_MAIL;
3037                 goto FINISH_OFF;
3038         }
3039
3040         for (count = 0; count < attachment_tbl_count ; count++) {
3041                 if (attachment_tbl_list[count].attachment_mime_type && strcasestr(attachment_tbl_list[count].attachment_mime_type, "SIGNATURE"))
3042                         break;
3043         }
3044
3045         if (mail_data->smime_type == EMAIL_SMIME_SIGNED) {
3046                 if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
3047                         EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
3048                         goto FINISH_OFF;
3049                 }
3050         } else if (mail_data->smime_type == EMAIL_PGP_SIGNED) {
3051                 if ((err = emcore_pgp_get_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, mail_data->digest_type, &verify)) != EMAIL_ERROR_NONE) {
3052                         EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_signature failed : [%d]", err);
3053                         goto FINISH_OFF;
3054                 }
3055         } else {
3056                 EM_DEBUG_LOG("Invalid signed mail");
3057                 err = EMAIL_ERROR_INVALID_PARAM;
3058         }
3059
3060 FINISH_OFF:
3061
3062         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
3063                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
3064
3065         if (verify) {
3066                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
3067         }
3068
3069         if (!emipc_execute_stub_api(a_hAPI))
3070                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3071
3072
3073         if (attachment_tbl_list)
3074                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
3075
3076         if (mail_data) {
3077                 emcore_free_mail_data(mail_data);
3078                 EM_SAFE_FREE(mail_data);
3079         }
3080
3081     EM_SAFE_FREE(multi_user_name);
3082         EM_DEBUG_FUNC_END();
3083 }
3084
3085 void stb_verify_certificate(HIPC_API a_hAPI)
3086 {
3087         int err = EMAIL_ERROR_NONE;
3088         int verify = 0;
3089         int cert_file_len = 0;
3090         char *cert_file_path = 0;
3091     int nAPPID = emipc_get_app_id(a_hAPI);
3092     char *multi_user_name = NULL;
3093
3094     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3095         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3096         multi_user_name = NULL;
3097     }
3098
3099         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
3100         if (cert_file_len > 0) {
3101                 cert_file_path = em_malloc(cert_file_len + 1);
3102                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
3103         }
3104
3105         if (!emcore_verify_certificate(cert_file_path, &verify, &err)) {
3106                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
3107         }
3108
3109         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
3110                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
3111
3112         if (verify) {
3113                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
3114         }
3115
3116         if (!emipc_execute_stub_api(a_hAPI))
3117                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3118
3119         EM_SAFE_FREE(cert_file_path);           
3120     EM_SAFE_FREE(multi_user_name);
3121         EM_DEBUG_FUNC_END();    
3122 }
3123
3124 void stb_ping_service(HIPC_API a_hAPI)
3125 {
3126         EM_DEBUG_FUNC_BEGIN();
3127         int err = EMAIL_ERROR_NONE;
3128
3129         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3130                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3131
3132         if (!emipc_execute_stub_api(a_hAPI))
3133                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3134
3135         EM_DEBUG_FUNC_END();
3136 }
3137
3138 void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
3139 {
3140         EM_DEBUG_FUNC_BEGIN();
3141         int err = EMAIL_ERROR_NONE, account_id;
3142         int total_mail_count = 0, unread_mail_count = 0;
3143         int input_from_eas = 0;
3144     /* Get Container name */
3145     int nAPPID = emipc_get_app_id(a_hAPI);
3146     char *multi_user_name = NULL;
3147
3148     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3149         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3150         multi_user_name = NULL;
3151     }
3152
3153         /* account_id */
3154         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3155         EM_DEBUG_LOG("account_id [%d]", account_id);
3156
3157         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &total_mail_count);
3158         EM_DEBUG_LOG("total_mail_count [%d]", total_mail_count);
3159
3160         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &unread_mail_count);
3161         EM_DEBUG_LOG("unread_mail_count [%d]", unread_mail_count);
3162
3163         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &input_from_eas);
3164         EM_DEBUG_LOG("unread_mail_count [%d]", input_from_eas);
3165
3166         emcore_display_unread_in_badge(multi_user_name);
3167
3168         if(!emdaemon_finalize_sync(multi_user_name, account_id, total_mail_count, unread_mail_count, 0, 0, input_from_eas, &err)) {
3169                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
3170         }
3171
3172         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3173                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3174
3175         if (!emipc_execute_stub_api(a_hAPI))
3176                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3177
3178     EM_SAFE_FREE(multi_user_name);
3179         EM_DEBUG_FUNC_END();
3180 }
3181
3182 void stb_clear_notification_bar(HIPC_API a_hAPI)
3183 {
3184         EM_DEBUG_FUNC_BEGIN();
3185         int err = EMAIL_ERROR_NONE, account_id;
3186     int nAPPID = emipc_get_app_id(a_hAPI);
3187     char *multi_user_name = NULL;
3188
3189     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3190         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3191         multi_user_name = NULL;
3192     }
3193
3194         /* account_id */
3195         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3196         EM_DEBUG_LOG("account_id [%d]", account_id);
3197
3198         if((err = emcore_clear_notifications(multi_user_name, account_id)) != EMAIL_ERROR_NONE) 
3199                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
3200
3201         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3202                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3203
3204         if (!emipc_execute_stub_api(a_hAPI))
3205                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3206
3207     EM_SAFE_FREE(multi_user_name);
3208         EM_DEBUG_FUNC_END();
3209 }
3210
3211 void stb_show_user_message(HIPC_API a_hAPI)
3212 {
3213         EM_DEBUG_FUNC_BEGIN();
3214         int err = EMAIL_ERROR_NONE;
3215         int param_id = 0;
3216         int param_error = 0;
3217         email_action_t param_action = 0;
3218     int nAPPID = emipc_get_app_id(a_hAPI);
3219     char *multi_user_name = NULL;
3220
3221     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3222         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3223         multi_user_name = NULL;
3224     }
3225
3226         /* param_id */
3227         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &param_id);
3228         EM_DEBUG_LOG("param_id [%d]", param_id);
3229
3230         /* param_action */
3231         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &param_action);
3232         EM_DEBUG_LOG("param_action [%d]", param_action);
3233
3234         /* param_error */
3235         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &param_error);
3236         EM_DEBUG_LOG("param_error [%d]", param_error);
3237
3238         if(!emcore_show_user_message(multi_user_name, param_id, param_action, param_error)) {
3239                 EM_DEBUG_EXCEPTION("emcore_show_user_message failed");
3240         }
3241
3242         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3243                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3244
3245         if (!emipc_execute_stub_api(a_hAPI))
3246                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3247
3248     EM_SAFE_FREE(multi_user_name);
3249         EM_DEBUG_FUNC_END("err [%d]", err);
3250 }
3251
3252 void stb_write_mime_file(HIPC_API a_hAPI)
3253 {
3254         EM_DEBUG_FUNC_BEGIN();
3255         int err = EMAIL_ERROR_NONE;
3256
3257         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3258                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3259
3260         if (!emipc_execute_stub_api(a_hAPI))
3261                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3262
3263         EM_DEBUG_FUNC_END();
3264 }
3265
3266
3267 void stb_validate_account_ex(HIPC_API a_hAPI)
3268 {
3269         EM_DEBUG_FUNC_BEGIN();
3270         int buffer_size = 0;
3271         int local_result = 0;
3272         int handle = 0;
3273         char* stream = NULL;
3274         email_account_t *account = NULL;
3275         email_account_t *ref_account = NULL;
3276         int err = EMAIL_ERROR_NONE;
3277     int nAPPID = emipc_get_app_id(a_hAPI);
3278     char *multi_user_name = NULL;
3279
3280     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3281         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3282         multi_user_name = NULL;
3283     }
3284
3285         /* get account info */
3286         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
3287         EM_DEBUG_LOG("size [%d]", buffer_size);
3288         if(buffer_size <= 0) {
3289                 err = EMAIL_ERROR_INVALID_PARAM;
3290                 goto FINISH_OFF;
3291         }
3292
3293         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
3294         if(!stream) {
3295                 err = EMAIL_ERROR_INVALID_PARAM;
3296                 goto FINISH_OFF;
3297         }
3298
3299         account = em_malloc(sizeof(email_account_t));
3300
3301         if(account == NULL) {
3302                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
3303                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3304                 goto FINISH_OFF;
3305         }
3306
3307         em_convert_byte_stream_to_account(stream, buffer_size, account);
3308     account->user_name = EM_SAFE_STRDUP(multi_user_name);
3309
3310         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
3311                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
3312                 goto FINISH_OFF;
3313         }
3314
3315         ref_account = emcore_get_account_reference(multi_user_name, account->account_id, false);
3316
3317         if (!ref_account) {
3318                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
3319                 goto FINISH_OFF;
3320         }
3321
3322         /* ref_account will be removed by worker_event_queue() */
3323         if((err = emdaemon_validate_account_ex(multi_user_name, ref_account, &handle)) != EMAIL_ERROR_NONE) {
3324                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_ex fail [%d]", err);
3325                 goto FINISH_OFF;
3326         }
3327
3328         local_result = 1;
3329         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
3330                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3331         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
3332                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3333         if (!emipc_execute_stub_api(a_hAPI))
3334                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3335
3336 FINISH_OFF:
3337         if ( local_result == 0 ) { /* there is an error */
3338                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
3339                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
3340                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3341                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
3342                 if (!emipc_execute_stub_api(a_hAPI))
3343                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3344         }
3345
3346     EM_SAFE_FREE(multi_user_name);
3347         EM_DEBUG_FUNC_END();
3348 }
3349
3350 void stb_handle_task(int task_type, HIPC_API a_hAPI)
3351 {
3352         EM_DEBUG_FUNC_BEGIN();
3353         int param_index = 0;
3354         int is_async_task = (task_type > EMAIL_ASYNC_TASK_BOUNDARY_START)?1:0;
3355         int err = EMAIL_ERROR_NONE;
3356         int task_id = 0;
3357         int task_parameter_length = 0;
3358         char *task_parameter = NULL;
3359
3360         /* task_parameter_length */;
3361         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3362         EM_DEBUG_LOG("task_parameter_length [%d]", task_parameter_length);
3363
3364         /* task_parameter */
3365         if(task_parameter_length > 0)    {
3366                 task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3367         }
3368
3369         if(is_async_task) {
3370                 /* add async task */
3371                 if((err = emcore_add_task_to_task_table(NULL, 
3372                                                                                                 task_type, 
3373                                                                                                 EMAIL_TASK_PRIORITY_MID, 
3374                                                                                                 task_parameter, 
3375                                                                                                 task_parameter_length, 
3376                                                                                                 &task_id)) != EMAIL_ERROR_NONE) {
3377                         EM_DEBUG_EXCEPTION("emcore_add_task_to_task_pool failed [%d]", err);
3378                         goto FINISH_OFF;
3379                 }
3380         }
3381         else {
3382                 /* do sync task */
3383                 email_task_t sync_task;
3384
3385                 memset(&sync_task, 0, sizeof(email_task_t));
3386                 sync_task.task_type             = task_type;
3387                 sync_task.task_priority         = EMAIL_TASK_PRIORITY_MID;
3388                 sync_task.task_parameter        = task_parameter;
3389                 sync_task.task_parameter_length = task_parameter_length;
3390
3391                 if((err = emcore_default_sync_task_handler(&sync_task)) != EMAIL_ERROR_NONE) {
3392                         EM_DEBUG_EXCEPTION("emcore_default_sync_task_handler failed [%d]", err);
3393                         goto FINISH_OFF;
3394                 }
3395         }
3396
3397 FINISH_OFF:
3398         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3399                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3400
3401         if(is_async_task) {
3402                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3403                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3404         }
3405
3406         if (!emipc_execute_stub_api(a_hAPI))
3407                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3408
3409         EM_DEBUG_FUNC_END("err [%d]", err);
3410 }
3411
3412 void* thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void *input_param)
3413 {
3414         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3415         int err = EMAIL_ERROR_NONE;
3416         int err_for_signal = EMAIL_ERROR_NONE;
3417         int i = 0;
3418         int task_id = THREAD_SELF();
3419         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *task_param = input_param;
3420
3421         /* Send start signal */
3422         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3423                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3424
3425         for(i = 0; i < task_param->mail_id_count; i++) {
3426                 if((err = emcore_move_mail_to_another_account(task_param->multi_user_name, task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {
3427                         EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);
3428                         goto FINISH_OFF;
3429                 }
3430
3431                 /* Send progress signal */
3432                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_IN_PROGRESS, i, task_param->mail_id_count)) != EMAIL_ERROR_NONE)
3433                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3434         }
3435
3436 FINISH_OFF:
3437         /* Send finish signal */
3438         if(err == EMAIL_ERROR_NONE) {
3439                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3440                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3441         }
3442         else {
3443                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
3444                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3445         }
3446
3447         /* Free task parameter */
3448         EM_SAFE_FREE(task_param->multi_user_name);
3449         EM_SAFE_FREE(task_param->mail_id_array);
3450         EM_SAFE_FREE(task_param);
3451
3452         emcore_close_recv_stream_list();
3453         EM_DEBUG_FUNC_END("err [%d]", err);
3454         return SUCCESS;
3455 }
3456
3457 void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
3458 {
3459         EM_DEBUG_FUNC_BEGIN();
3460         int param_index = 0;
3461         int err = EMAIL_ERROR_NONE;
3462         int task_parameter_length = 0;
3463         int thread_error;
3464         thread_t task_id;
3465         char *task_parameter = NULL;
3466         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *decoded_parameter = NULL;
3467
3468     int nAPPID = emipc_get_app_id(a_hAPI);
3469     char *multi_user_name = NULL;
3470
3471     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3472             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3473             multi_user_name = NULL;
3474     }
3475
3476         /* task_parameter_length */;
3477         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3478
3479         if(task_parameter_length <= 0) {
3480                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3481                 err = EMAIL_ERROR_INVALID_PARAM;
3482                 goto FINISH_OFF;
3483         }
3484
3485         /* task_parameter */
3486         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3487
3488         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
3489                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3490                 goto FINISH_OFF;
3491         }
3492
3493     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3494
3495         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, (void*)decoded_parameter, thread_error);
3496
3497         if(thread_error != 0) {
3498                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3499                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3500                 goto FINISH_OFF;
3501         }
3502
3503         THREAD_DETACH(task_id);
3504
3505 FINISH_OFF:
3506         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3507                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3508
3509         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3510                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3511
3512         if (!emipc_execute_stub_api(a_hAPI))
3513                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3514
3515     EM_SAFE_FREE(multi_user_name);
3516
3517         EM_DEBUG_FUNC_END("err [%d]", err);
3518 }
3519
3520 void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
3521 {
3522         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3523         int err = EMAIL_ERROR_NONE;
3524         int err_for_signal = EMAIL_ERROR_NONE;
3525         int recursive = 1;
3526         int task_id = THREAD_SELF();
3527         email_account_t* ref_account = NULL;
3528         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *task_param = input_param;
3529
3530         ref_account = emcore_get_account_reference(task_param->multi_user_name, task_param->account_id, false);
3531         if (!ref_account)  {
3532                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", task_param->account_id);
3533                 err = EMAIL_ERROR_INVALID_ACCOUNT;
3534                 goto FINISH_OFF;
3535         }
3536
3537         /*  on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
3538         if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
3539                 recursive = 0;
3540         }
3541
3542         /* Send start signal */
3543         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3544                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3545
3546         if((err = emcore_delete_mailbox_ex(task_param->multi_user_name, task_param->account_id, task_param->mailbox_id_array, task_param->mailbox_id_count, task_param->on_server, recursive)) != EMAIL_ERROR_NONE) {
3547                 EM_DEBUG_EXCEPTION("emcore_delete_mailbox_ex failed[%d]", err);
3548                 goto FINISH_OFF;
3549         }
3550
3551 FINISH_OFF:
3552         /* Send finish signal */
3553         if(err == EMAIL_ERROR_NONE) {
3554                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3555                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3556         }
3557         else {
3558                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
3559                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3560         }
3561
3562         if (ref_account) { /*prevent 49435*/
3563                 emcore_free_account(ref_account);
3564                 EM_SAFE_FREE(ref_account);
3565         }
3566
3567         /* Free task parameter */
3568         EM_SAFE_FREE(task_param->mailbox_id_array);
3569     EM_SAFE_FREE(task_param->multi_user_name);
3570         EM_SAFE_FREE(task_param);
3571
3572         EM_DEBUG_FUNC_END("err [%d]", err);
3573         return SUCCESS;
3574 }
3575
3576 void stb_delete_mailbox_ex(HIPC_API a_hAPI)
3577 {
3578         EM_DEBUG_FUNC_BEGIN();
3579         int param_index = 0;
3580         int err = EMAIL_ERROR_NONE;
3581         int task_parameter_length = 0;
3582         int thread_error = 0;
3583         thread_t task_id;
3584         char *task_parameter = NULL;
3585         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *decoded_parameter = NULL;
3586     int nAPPID = emipc_get_app_id(a_hAPI);
3587     char *multi_user_name = NULL;
3588
3589     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3590         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3591         multi_user_name = NULL;
3592     }
3593
3594         /* task_parameter_length */;
3595         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3596
3597         if(task_parameter_length <= 0) {
3598                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3599                 err = EMAIL_ERROR_INVALID_PARAM;
3600                 goto FINISH_OFF;
3601         }
3602
3603         /* task_parameter */
3604         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3605
3606         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
3607                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3608                 goto FINISH_OFF;
3609         }
3610
3611     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3612
3613         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, (void*)decoded_parameter, thread_error);
3614
3615         if(thread_error != 0) {
3616                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3617                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3618                 goto FINISH_OFF;
3619         }
3620
3621         THREAD_DETACH(task_id);
3622
3623 FINISH_OFF:
3624         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3625                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3626
3627         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3628                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3629
3630         if (!emipc_execute_stub_api(a_hAPI))
3631                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3632
3633     EM_SAFE_FREE(multi_user_name);
3634         EM_DEBUG_FUNC_END("err [%d]", err);
3635 }
3636
3637 void* thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(void *input_param)
3638 {
3639         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3640         int err = EMAIL_ERROR_NONE;
3641         int err_for_signal = EMAIL_ERROR_NONE;
3642         int task_id = THREAD_SELF();
3643         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *task_param = input_param;
3644
3645         /* Send start signal */
3646         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3647                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3648
3649         if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->multi_user_name, task_param->mail_id)) != EMAIL_ERROR_NONE) {
3650                 EM_DEBUG_EXCEPTION("emcore_send_mail_with_downloading_attachment_of_original_mail failed [%d]", err);
3651                 goto FINISH_OFF;
3652         }
3653
3654 FINISH_OFF:
3655         /* Send finish signal */
3656         if(err == EMAIL_ERROR_NONE) {
3657                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
3658                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3659         }
3660         else {
3661                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
3662                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3663         }
3664
3665         /* Free task parameter */
3666     EM_SAFE_FREE(task_param->multi_user_name);
3667         EM_SAFE_FREE(task_param);
3668         emcore_close_smtp_stream_list();
3669         EM_DEBUG_FUNC_END("err [%d]", err);
3670         return SUCCESS;
3671 }
3672
3673 void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
3674 {
3675         EM_DEBUG_FUNC_BEGIN();
3676         int param_index = 0;
3677         int err = EMAIL_ERROR_NONE;
3678         int task_parameter_length = 0;
3679         int thread_error = 0;
3680         thread_t task_id;
3681         char *task_parameter = NULL;
3682         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *decoded_parameter = NULL;
3683     int nAPPID = emipc_get_app_id(a_hAPI);
3684     char *multi_user_name = NULL;
3685
3686     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3687         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3688         multi_user_name = NULL;
3689     }
3690
3691         /* task_parameter_length */;
3692         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3693
3694         if(task_parameter_length <= 0) {
3695                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3696                 err = EMAIL_ERROR_INVALID_PARAM;
3697                 goto FINISH_OFF;
3698         }
3699
3700         /* task_parameter */
3701         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3702
3703         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
3704                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3705                 goto FINISH_OFF;
3706         }
3707
3708     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3709
3710         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, (void*)decoded_parameter, thread_error);
3711
3712         if(thread_error != 0) {
3713                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3714                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3715                 goto FINISH_OFF;
3716         }
3717
3718         THREAD_DETACH(task_id);
3719
3720 FINISH_OFF:
3721         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3722                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3723
3724         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3725                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3726
3727         if (!emipc_execute_stub_api(a_hAPI))
3728                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3729
3730     EM_SAFE_FREE(multi_user_name);
3731         EM_DEBUG_FUNC_END("err [%d]", err);
3732 }
3733
3734 void stb_save_default_account_id(HIPC_API a_hAPI)
3735 {
3736         EM_DEBUG_FUNC_BEGIN();
3737         int err = EMAIL_ERROR_NONE;
3738         int account_id = 0;
3739
3740     int nAPPID = emipc_get_app_id(a_hAPI);
3741         char *multi_user_name = NULL;
3742
3743         if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3744         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3745         multi_user_name = NULL;
3746         }
3747
3748         /* account_id */
3749         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3750         EM_DEBUG_LOG("account_id [%d]", account_id);
3751
3752         err = emcore_save_default_account_id(multi_user_name, account_id);
3753         if (err != EMAIL_ERROR_NONE) EM_DEBUG_EXCEPTION("emcore_save_default_account_id failed [%d]", err);
3754
3755         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3756                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3757
3758         if (!emipc_execute_stub_api(a_hAPI))
3759                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3760
3761         EM_SAFE_FREE(multi_user_name);
3762         EM_DEBUG_FUNC_END();
3763 }
3764
3765 void stb_load_default_account_id(HIPC_API a_hAPI)
3766 {
3767         EM_DEBUG_FUNC_BEGIN();
3768         int err = EMAIL_ERROR_NONE;
3769         int account_id = 0;
3770
3771     int nAPPID = emipc_get_app_id(a_hAPI);
3772         char *multi_user_name = NULL;
3773
3774         if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3775         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3776         multi_user_name = NULL;
3777         }
3778
3779         err = emcore_load_default_account_id(multi_user_name, &account_id);
3780         if (err != EMAIL_ERROR_NONE) { 
3781                 EM_DEBUG_EXCEPTION("emcore_save_default_account_id failed [%d]", err);
3782         }
3783
3784         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3785                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3786
3787         if (err == EMAIL_ERROR_NONE) {
3788                 if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &account_id, sizeof(int))) 
3789                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3790         }
3791
3792         if (!emipc_execute_stub_api(a_hAPI))
3793                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3794
3795         EM_SAFE_FREE(multi_user_name);
3796         EM_DEBUG_FUNC_END();
3797 }
3798
3799 void stb_get_user_name(HIPC_API a_hAPI)
3800 {
3801     EM_DEBUG_FUNC_BEGIN();
3802
3803     int err = EMAIL_ERROR_NONE;
3804     int pid = 0;
3805
3806     char *user_name = NULL;
3807
3808     pid = emipc_get_app_id(a_hAPI);
3809     EM_DEBUG_LOG("Peer PID : [%d]", pid);
3810
3811     if (pid > 0) {
3812         if ((err = emcore_get_user_name(pid, &user_name)) != EMAIL_ERROR_NONE) 
3813             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3814
3815         EM_DEBUG_LOG("Domain name : [%s]", user_name);
3816
3817         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) 
3818                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3819
3820                 if (user_name) {
3821                         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, user_name, EM_SAFE_STRLEN(user_name) + 1)) 
3822                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3823                 }
3824     }
3825
3826     if (!emipc_execute_stub_api(a_hAPI))
3827         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3828
3829     EM_SAFE_FREE(user_name);
3830
3831     EM_DEBUG_FUNC_END();
3832 }
3833
3834 void stb_API_mapper(HIPC_API a_hAPI)
3835 {
3836         EM_DEBUG_FUNC_BEGIN();
3837         int err = EMAIL_ERROR_NONE;
3838         int nAPIID = emipc_get_api_id(a_hAPI);
3839         int client_fd = emipc_get_response_id(a_hAPI);
3840
3841         err = emcore_check_privilege(client_fd);
3842         if (err != EMAIL_ERROR_NONE) {
3843                 EM_DEBUG_EXCEPTION("emcore_check_privilege failed : [%d]", err);
3844
3845                 if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3846                         EM_DEBUG_EXCEPTION("emipc_add_paramter failed");
3847
3848                 if (!emipc_execute_stub_api(a_hAPI))
3849                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3850
3851                 return;
3852         }
3853
3854         switch(nAPIID) {
3855                 case _EMAIL_API_ADD_ACCOUNT:
3856                         stb_create_account(a_hAPI);
3857                         break;
3858
3859                 case _EMAIL_API_ADD_MAILBOX:
3860                         stb_create_mailbox(a_hAPI);
3861                         break;
3862
3863                 case _EMAIL_API_DELETE_MAILBOX:
3864                         stb_delete_mailbox(a_hAPI);
3865                         break;
3866
3867                 case _EMAIL_API_SET_MAILBOX_TYPE:
3868                         stb_set_mailbox_type(a_hAPI);
3869                         break;
3870
3871                 case _EMAIL_API_SET_LOCAL_MAILBOX:
3872                         stb_set_local_mailbox(a_hAPI);
3873                         break;
3874
3875                 case _EMAIL_API_STAMP_SYNC_TIME_OF_MAILBOX:
3876                         stb_stamp_sync_time_of_mailbox(a_hAPI);
3877                         break;
3878
3879                 case _EMAIL_API_SET_MAIL_SLOT_SIZE:
3880                         stb_set_mail_slot_size_of_mailbox(a_hAPI);
3881                         break;
3882
3883                 case _EMAIL_API_RENAME_MAILBOX:
3884                         stb_rename_mailbox(a_hAPI);
3885                         break;
3886
3887                 case _EMAIL_API_RENAME_MAILBOX_EX:
3888                         stb_rename_mailbox_ex(a_hAPI);
3889                         break;
3890
3891                 case _EMAIL_API_SEND_MAIL:
3892                         stb_send_mail(a_hAPI);
3893                         break;
3894
3895                 case _EMAIL_API_QUERY_SMTP_MAIL_SIZE_LIMIT:
3896                         stb_query_smtp_mail_size_limit(a_hAPI);
3897                         break;
3898
3899                 case _EMAIL_API_GET_MAILBOX_LIST:
3900                         stb_get_mailbox_list(a_hAPI);
3901                         break;
3902
3903                 case _EMAIL_API_SYNC_HEADER:
3904                         stb_sync_header(a_hAPI);
3905                         break;
3906
3907                 case _EMAIL_API_DOWNLOAD_BODY:
3908                         stb_download_body(a_hAPI);
3909                         break;
3910
3911                 case _EMAIL_API_CLEAR_DATA:
3912                         stb_clear_mail_data (a_hAPI);
3913                         break;
3914
3915                 case _EMAIL_API_DELETE_ALL_MAIL:
3916                         stb_delete_all_mails(a_hAPI);
3917                         break;
3918
3919                 case _EMAIL_API_DELETE_MAIL:
3920                         stb_delete_mail(a_hAPI);
3921                         break;
3922
3923                 case _EMAIL_API_ADD_RULE:
3924                         stb_add_rule(a_hAPI);
3925                         break;
3926
3927                 case _EMAIL_API_GET_RULE:
3928                         stb_get_rule(a_hAPI);
3929                         break;
3930
3931                 case _EMAIL_API_GET_RULE_LIST:
3932                         stb_get_rule_list(a_hAPI);
3933                         break;
3934
3935                 case _EMAIL_API_FIND_RULE:
3936                         stb_find_rule(a_hAPI);
3937                         break;
3938
3939                 case _EMAIL_API_UPDATE_RULE:
3940                         stb_update_rule(a_hAPI);
3941                         break;
3942
3943                 case _EMAIL_API_DELETE_RULE:
3944                         stb_delete_rule(a_hAPI);
3945                         break;
3946
3947                 case _EMAIL_API_APPLY_RULE:
3948                         stb_apply_rule(a_hAPI);
3949                         break;
3950
3951                 case _EMAIL_API_MOVE_MAIL:
3952                         stb_move_mail(a_hAPI);
3953                         break;
3954
3955                 case _EMAIL_API_MOVE_ALL_MAIL:
3956                         stb_move_all_mails(a_hAPI);
3957                         break;
3958
3959                 case _EMAIL_API_SET_FLAGS_FIELD:
3960                         stb_set_flags_field(a_hAPI);
3961                         break;
3962
3963                 case _EMAIL_API_ADD_MAIL:
3964                         stb_add_mail(a_hAPI);
3965                         break;
3966
3967                 case _EMAIL_API_UPDATE_MAIL:
3968                         stb_update_mail(a_hAPI);
3969                         break;
3970
3971                 case _EMAIL_API_MOVE_THREAD_TO_MAILBOX:
3972                         stb_move_thread_to_mailbox(a_hAPI);
3973                         break;
3974
3975                 case _EMAIL_API_DELETE_THREAD:
3976                         stb_delete_thread(a_hAPI);
3977                         break;
3978
3979                 case _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD:
3980                         stb_modify_seen_flag_of_thread(a_hAPI);
3981                         break;
3982
3983                 case _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED:
3984                         stb_expunge_mails_deleted_flagged(a_hAPI);
3985                         break;
3986
3987                 case _EMAIL_API_DELETE_ACCOUNT:
3988                         stb_delete_account(a_hAPI);
3989                         break;
3990
3991                 case _EMAIL_API_UPDATE_ACCOUNT:
3992                         stb_update_account(a_hAPI);
3993                         break;
3994
3995                 case _EMAIL_API_ADD_ATTACHMENT:
3996                         stb_add_attachment(a_hAPI);
3997                         break;
3998
3999                 case _EMAIL_API_GET_IMAP_MAILBOX_LIST:
4000                         stb_get_imap_mailbox_list(a_hAPI);
4001                         break;
4002
4003                 case _EMAIL_API_GET_ATTACHMENT:
4004                         stb_get_attachment(a_hAPI);
4005                         break;
4006
4007                 case _EMAIL_API_DELETE_ATTACHMENT:
4008                         stb_delete_attachment(a_hAPI);
4009                         break;
4010
4011                 case _EMAIL_API_DOWNLOAD_ATTACHMENT:
4012                         stb_download_attachment(a_hAPI);
4013                         break;
4014
4015                 case _EMAIL_API_GET_ACCOUNT_LIST:
4016                         stb_get_account_list(a_hAPI);
4017                         break;
4018
4019                 case _EMAIL_API_SEND_SAVED:
4020                         stb_mail_send_saved(a_hAPI);
4021                         break;
4022
4023                 case _EMAIL_API_ADD_READ_RECEIPT:
4024                         stb_add_read_receipt(a_hAPI);
4025                         break;
4026
4027                 case _EMAIL_API_CANCEL_JOB:
4028                         stb_cancel_job(a_hAPI);
4029                         break;
4030
4031                 case _EMAIL_API_SEND_RETRY:
4032                         stb_retry_sending_mail(a_hAPI);
4033                         break;
4034
4035                 case _EMAIL_API_VALIDATE_ACCOUNT :
4036                         stb_validate_account(a_hAPI);
4037                         break;
4038
4039                 case _EMAIL_API_SEND_MAIL_CANCEL_JOB :
4040                         stb_cancel_send_mail_job(a_hAPI);
4041                         break;
4042
4043                 case _EMAIL_API_SEARCH_MAIL_ON_SERVER :
4044                         stb_search_mail_on_server(a_hAPI);
4045                         break;
4046
4047                 case _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER :
4048                         stb_clear_result_of_search_mail_on_server(a_hAPI);
4049                         break;
4050
4051                 case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :
4052                         stb_add_account_with_validation(a_hAPI);
4053                         break;
4054
4055                 case _EMAIL_API_BACKUP_ACCOUNTS:
4056                         stb_backup_account(a_hAPI);
4057                         break;
4058
4059                 case _EMAIL_API_RESTORE_ACCOUNTS:
4060                         stb_restore_account(a_hAPI);
4061                         break;
4062
4063                 case _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT:
4064                         stb_get_password_length(a_hAPI);
4065                         break;
4066
4067                 case _EMAIL_API_GET_TASK_INFORMATION:
4068                         stb_get_task_information(a_hAPI);
4069                         break;
4070
4071                 case _EMAIL_API_ADD_CERTIFICATE:
4072                         stb_add_certificate(a_hAPI);
4073                         break;
4074
4075                 case _EMAIL_API_DELETE_CERTIFICATE:
4076                         stb_delete_certificate(a_hAPI);
4077                         break;
4078
4079                 case _EMAIL_API_VERIFY_SIGNATURE:
4080                         stb_verify_signature(a_hAPI);
4081                         break;
4082
4083                 case _EMAIL_API_VERIFY_CERTIFICATE:
4084                         stb_verify_certificate(a_hAPI);
4085                         break;
4086
4087                 case _EMAIL_API_PING_SERVICE :
4088                         stb_ping_service(a_hAPI);
4089                         break;
4090
4091                 case _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL :
4092                         stb_update_notification_bar_for_unread_mail(a_hAPI);
4093                         break;
4094
4095                 case _EMAIL_API_CLEAR_NOTIFICATION_BAR :
4096                         stb_clear_notification_bar(a_hAPI);
4097                         break;
4098
4099                 case _EMAIL_API_SHOW_USER_MESSAGE :
4100                         stb_show_user_message(a_hAPI);
4101                         break;
4102
4103                 case _EMAIL_API_WRITE_MIME_FILE :
4104                         stb_write_mime_file(a_hAPI);
4105                         break;
4106
4107                 case _EMAIL_API_VALIDATE_ACCOUNT_EX :
4108                         stb_validate_account_ex(a_hAPI);
4109                         break;
4110
4111                 case EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT :
4112                         stb_move_mails_to_mailbox_of_another_account(a_hAPI);
4113                         break;
4114
4115                 case EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX :
4116                         stb_delete_mailbox_ex(a_hAPI);
4117                         break;
4118
4119                 case EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL :
4120                         stb_send_mail_with_downloading_attachment_of_original_mail(a_hAPI);
4121                         break;
4122
4123         case _EMAIL_API_GET_USER_NAME :
4124             stb_get_user_name(a_hAPI);
4125             break;
4126
4127                 case _EMAIL_API_SAVE_DEFAULT_ACCOUNT_ID :
4128                         stb_save_default_account_id(a_hAPI);
4129                         break;
4130
4131                 case _EMAIL_API_LOAD_DEFAULT_ACCOUNT_ID :
4132                         stb_load_default_account_id(a_hAPI);
4133                         break;
4134
4135                 default :
4136                         if(EMAIL_SYNC_TASK_BOUNDARY_START < nAPIID && nAPIID < EMAIL_ASYNC_TASK_BOUNDARY_END)
4137                                 stb_handle_task(nAPIID, a_hAPI);
4138                         break;
4139         }
4140         EM_DEBUG_FUNC_END();
4141 }
4142
4143 GMainLoop *g_mainloop = NULL;
4144
4145 /* this func should be called in main_loop */
4146 INTERNAL_FUNC int kill_daemon_if_no_account()
4147 {
4148         EM_DEBUG_FUNC_BEGIN();
4149         int err = EMAIL_ERROR_NONE;
4150         email_account_t *account_list = NULL;
4151         int account_count = 0;
4152     int total_account_count = 0;
4153     GList *node = NULL;
4154     GList *zone_name_list = NULL;
4155
4156     /* each container check the account */
4157     if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
4158         EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
4159                 if (zone_name_list) g_list_free(zone_name_list);
4160                 goto FINISH_OFF;
4161     }
4162
4163     if (g_list_length(zone_name_list) == 1) {
4164         if ((err = emcore_get_account_reference_list(NULL, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
4165             EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
4166         }
4167
4168         if (account_list)
4169             emcore_free_account_list(&account_list, account_count, NULL);
4170
4171         total_account_count += account_count;
4172     } else {
4173         node = g_list_first(zone_name_list);
4174         while (node != NULL) {
4175             if (!node->data) {
4176                 node = g_list_next(node);
4177                                 continue;
4178                         }
4179
4180             if ((err = emcore_get_account_reference_list(node->data, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
4181                 EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
4182             }
4183
4184             if (account_list)
4185                 emcore_free_account_list(&account_list, account_count, NULL);
4186
4187             total_account_count += account_count;
4188             EM_SAFE_FREE(node->data);
4189             node = g_list_next(node);
4190         }
4191     }
4192
4193         if (zone_name_list)
4194                 g_list_free(zone_name_list);
4195
4196         EM_DEBUG_LOG("account_count [%d]", total_account_count);
4197
4198         if (total_account_count == 0) {
4199                 EM_DEBUG_LOG("email-service is going to shutdown");
4200                 g_main_loop_quit (g_mainloop);
4201         }
4202
4203 FINISH_OFF:
4204
4205         EM_DEBUG_FUNC_END("err [%d]", err);
4206         return err;
4207 }
4208
4209 static void callback_for_sigterm(int signum)
4210 {
4211         EM_DEBUG_FUNC_BEGIN("signum [%d]", signum);
4212
4213         if (g_mainloop)
4214                 g_main_loop_quit(g_mainloop);
4215
4216         EM_DEBUG_FUNC_END();
4217 }
4218
4219 extern int launch_pid;
4220
4221 gboolean callback_for_shutdown(gpointer user_data)
4222 {
4223         EM_DEBUG_FUNC_BEGIN("user_data[%p]", user_data);
4224
4225         if (launch_pid == 0) {
4226                 kill_daemon_if_no_account();
4227         }
4228
4229         EM_DEBUG_FUNC_END();
4230         return FALSE;
4231 }
4232
4233 gboolean callback_for_del_account (GIOChannel *ch, GIOCondition cond, gpointer data)
4234 {
4235         static int file_del = 0;
4236         static int db_del = 0;
4237         int event = 0;
4238         gsize len = 0;
4239         g_io_channel_read_chars (ch, (gchar*) &event, sizeof (event), &len, NULL);
4240
4241         if (event==EMAIL_SIGNAL_DB_DELETED) {
4242                 db_del = 1;
4243         }
4244         else if (event==EMAIL_SIGNAL_FILE_DELETED) {
4245                 file_del = 1;
4246         }
4247         EM_DEBUG_LOG ("callback_for_del_account called file_del[%d] db_del[%d]", file_del, db_del);
4248
4249         /* if called twice, process termination begins.
4250            two threads should complete the delete task */
4251         if (file_del && db_del) {
4252                 kill_daemon_if_no_account();
4253                 file_del = db_del = 0; /* if there is an account, reset status */
4254         }
4255         return TRUE;
4256 }
4257
4258 /* gdbus variables */
4259 static GDBusNodeInfo *introspection_data = NULL;
4260 extern gchar introspection_xml[];
4261
4262 static void
4263 handle_method_call (GDBusConnection         *connection,
4264                       const gchar           *sender,
4265                       const gchar           *object_path,
4266                       const gchar           *interface_name,
4267                       const gchar           *method_name,
4268                       GVariant              *parameters,
4269                       GDBusMethodInvocation *invocation,
4270                       gpointer               user_data)
4271 {
4272         /* called by emipc_launch_email_service */
4273         if (g_strcmp0 (method_name, "Launch") == 0) {
4274                 int caller_pid = 0;
4275                 g_variant_get (parameters, "(i)", &caller_pid);
4276
4277                 launch_pid = caller_pid;
4278                 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(i)", EMAIL_ERROR_NONE));
4279                 EM_DEBUG_LOG ("email-service launched by pid [%d]", caller_pid);
4280         }
4281 /*
4282         else if (g_strcmp0 (method_name, "SetContactsLog") == 0) {
4283                 GVariant* ret = em_gdbus_set_contact_log(parameters);
4284                 g_dbus_method_invocation_return_value (invocation, ret);
4285         }
4286         else if (g_strcmp0 (method_name, "DeleteContactsLog") == 0) {
4287                 GVariant* ret = em_gdbus_delete_contact_log(parameters);
4288                 g_dbus_method_invocation_return_value (invocation, ret);
4289         }
4290 */
4291         else if (g_strcmp0 (method_name, "GetDisplayName") == 0) {
4292                 GVariant* ret = em_gdbus_get_display_name(parameters);
4293                 g_dbus_method_invocation_return_value (invocation, ret);
4294         }
4295         else if (g_strcmp0 (method_name, "CheckBlockingMode") == 0) {
4296                 GVariant* ret = em_gdbus_check_blocking_mode(parameters);
4297                 g_dbus_method_invocation_return_value (invocation, ret);
4298         }
4299 }
4300
4301 static const
4302 GDBusInterfaceVTable interface_vtable =
4303 {
4304         handle_method_call,
4305         NULL,
4306         NULL
4307 };
4308
4309 static void
4310 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
4311 {
4312         EM_DEBUG_LOG ("on_bus_acquired begin");
4313
4314         guint reg_id;
4315         GError *error = NULL;
4316
4317         reg_id = g_dbus_connection_register_object (connection,
4318                                              EMAIL_SERVICE_PATH,
4319                                              introspection_data->interfaces[0],
4320                                              &interface_vtable,
4321                                              NULL,  /* user_data */
4322                                              NULL,  /* user_data_free_func */
4323                                              &error); /* GError** */
4324         if (reg_id == 0) {
4325                 EM_DEBUG_EXCEPTION ("g_dbus_connection_register_object error[%s]",error->message);
4326                 g_error_free (error);
4327         }
4328         EM_DEBUG_LOG ("on_bus_acquired end [%d]", reg_id);
4329 }
4330
4331 static void on_name_acquired (GDBusConnection *connection,
4332                                const gchar *name,
4333                                gpointer     user_data)
4334 {
4335         EM_DEBUG_LOG ("on_name_acquired [%s]", name);
4336 }
4337
4338 static void on_name_lost (GDBusConnection *connection,
4339                            const gchar *name,
4340                            gpointer     user_data)
4341 {
4342         EM_DEBUG_EXCEPTION ("on_name_lost [%p] [%s]", connection, name);
4343 }
4344
4345 INTERNAL_FUNC int main(int argc, char *argv[])
4346 {
4347         /* Do the email-service Initialization
4348        1. Create all DB tables and load the email engine */
4349         EM_DEBUG_LOG("Email service begin");
4350         int err = 0, ret;
4351         GMainLoop *mainloop = NULL;
4352         int owner_id = 0;
4353     GList *node = NULL;
4354     GList *zone_name_list = NULL;
4355
4356 #if !GLIB_CHECK_VERSION(2, 36, 0) 
4357         g_type_init(); 
4358 #endif
4359
4360     /* Init container for daemon */
4361     emcore_create_container();
4362
4363         EM_DEBUG_LOG("ipcEmailStub_Initialize Start");
4364
4365         ret = emipc_initialize_stub(stb_API_mapper);
4366
4367         if(ret == true)
4368                 EM_DEBUG_LOG("ipcEmailStub_Initialize Success");
4369         else
4370                 EM_DEBUG_EXCEPTION("ipcEmailStub_Initialize failed");
4371
4372         signal(SIGPIPE, SIG_IGN);              /* to ignore signal 13(SIGPIPE) */
4373         signal(SIGTERM, callback_for_sigterm); /* to handle signal 15(SIGTERM) - power off case*/
4374
4375         emcore_gmime_init();
4376
4377     /* each container initialize */
4378     if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
4379         EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
4380                 if (zone_name_list) g_list_free(zone_name_list);
4381                 goto FINISH_OFF;
4382     }
4383
4384     if (g_list_length(zone_name_list) <= 1) {
4385         emdaemon_initialize(NULL, &err);
4386         emcore_connect_contacts_service(NULL);
4387     } else {
4388                 node = g_list_first(zone_name_list);
4389         while (node != NULL) {
4390             if (!node->data)
4391                 node = g_list_next(node);
4392
4393                         emdaemon_initialize(node->data, &err);
4394                         emcore_connect_contacts_service(node->data);
4395
4396             EM_SAFE_FREE(node->data);
4397             node = g_list_next(node);
4398         }
4399     }
4400
4401         if (zone_name_list)
4402                 g_list_free(zone_name_list);
4403
4404         g_timeout_add(5000, callback_for_shutdown, NULL);
4405
4406         /* pipe between main and del account thread */
4407         int *pipefd = emcore_init_pipe_for_del_account ();
4408         /* main loop uses IO channel for listening an event */
4409         if (pipefd) {
4410                 GIOChannel *ch = g_io_channel_unix_new (pipefd[0]);
4411                 EM_DEBUG_LOG ("main pipe[%d][%d]", pipefd[0], pipefd[1]);
4412                 /* non-blocking mode */
4413                 g_io_channel_set_flags (ch, g_io_channel_get_flags (ch) | G_IO_FLAG_NONBLOCK, NULL);
4414                 /* main loop watches the IO, and call the cb when data is ready */
4415                 g_io_add_watch (ch, G_IO_IN, &callback_for_del_account, NULL);
4416         }
4417
4418         /* gdbus setup */
4419         GError *error = NULL;
4420         introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
4421         if (!introspection_data) {
4422                 /* introspection_xml may be invalid */
4423                 EM_DEBUG_EXCEPTION ("g_dbus_node_info_new_for_xml error [%s]", error->message);
4424                 g_error_free (error);
4425         }
4426
4427         owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
4428                                 EMAIL_SERVICE_NAME,
4429                                 G_BUS_NAME_OWNER_FLAGS_NONE,
4430                                 on_bus_acquired,
4431                                 on_name_acquired,
4432                                 on_name_lost,
4433                                 NULL,
4434                                 NULL);
4435         if (!owner_id) {
4436                 EM_DEBUG_EXCEPTION ("g_bus_own_name error");
4437         }
4438         EM_DEBUG_LOG ("owner_id [%d]", owner_id);
4439
4440         mainloop = g_main_loop_new(NULL, 0);
4441         g_mainloop = mainloop;
4442
4443         g_main_loop_run(mainloop);
4444
4445         /* Clean up resources */
4446         g_bus_unown_name (owner_id);
4447         g_main_loop_unref(mainloop);
4448         g_mainloop = NULL;
4449
4450 FINISH_OFF:
4451
4452         emipc_finalize_stub();
4453         emdaemon_finalize(NULL);
4454         emcore_gmime_shutdown();
4455
4456         EM_DEBUG_LOG ("Goodbye, world");
4457         EM_DEBUG_FUNC_END();
4458         exit(44); /* exit with exit code 44 to prevent restarting */
4459         return 0;
4460 }
4461