Fixed the bug for tizen 3.0
[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
1191         EM_SAFE_FREE(local_rule_stream);        
1192     EM_SAFE_FREE(multi_user_name);
1193
1194         EM_DEBUG_FUNC_END();
1195 }
1196
1197 /* obsolete - there is no api calling this function */
1198 void stb_get_rule_list(HIPC_API a_hAPI)
1199 {
1200         EM_DEBUG_FUNC_BEGIN();
1201         int err = EMAIL_ERROR_NONE;
1202         int i = 0;
1203         char* local_stream = NULL;
1204         int count = 0;
1205         int size = 0;
1206         email_rule_t* filtering_list = NULL;
1207     int nAPPID = emipc_get_app_id(a_hAPI);
1208     char *multi_user_name = NULL;
1209
1210     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1211         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1212         multi_user_name = NULL;
1213     }
1214
1215         emdaemon_get_filter_list(multi_user_name, &filtering_list, &count, &err);
1216
1217         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1218                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1219
1220         /* insert rules if there exist rules*/
1221         if( count > 0 ) {
1222                 EM_DEBUG_LOG("num of rules [%d]", count);
1223                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
1224                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1225
1226                 for(i=0; i<count; i++) {
1227                         EM_DEBUG_LOG("Value - %s", filtering_list[i].value);
1228
1229                         local_stream = em_convert_rule_to_byte_stream(filtering_list+i, &size);
1230
1231                         if(!local_stream) break;
1232
1233                         if(!emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
1234                                 EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed  ");
1235
1236                         size = 0;
1237                 }
1238         }
1239
1240         if (!emipc_execute_stub_api(a_hAPI)) {
1241                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1242         }
1243
1244     EM_SAFE_FREE(multi_user_name);
1245         EM_DEBUG_FUNC_END();
1246 }
1247
1248 /* obsolete - there is no api calling this function */
1249 void stb_find_rule(HIPC_API a_hAPI)
1250 {
1251         EM_DEBUG_FUNC_BEGIN();
1252         int buffer_size = 0;
1253         int err = EMAIL_ERROR_NONE;
1254         char* local_rule_stream = NULL;
1255         email_rule_t rule = { 0 };
1256     int nAPPID = emipc_get_app_id(a_hAPI);
1257     char *multi_user_name = NULL;
1258
1259     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1260         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1261         multi_user_name = NULL;
1262     }
1263
1264         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
1265         if(buffer_size > 0)      {
1266                 local_rule_stream = (char*)em_malloc(buffer_size);
1267                 if (local_rule_stream == NULL) {
1268                         EM_DEBUG_EXCEPTION("Out of memory");
1269                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1270                         goto FINISH_OFF;
1271                 }
1272
1273                 if (local_rule_stream) {
1274                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, buffer_size, local_rule_stream);
1275                         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
1276                         EM_SAFE_FREE(local_rule_stream);
1277                         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
1278
1279                         if (!emdaemon_find_filter(multi_user_name, &rule, &err)) {
1280                                 EM_DEBUG_EXCEPTION("emdaemon_find_filter failed : [%d]", err);
1281                         }
1282                 }
1283         }
1284
1285 FINISH_OFF:
1286
1287         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1288                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1289
1290         if (!emipc_execute_stub_api(a_hAPI))
1291                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1292
1293     EM_SAFE_FREE(multi_user_name);
1294         EM_DEBUG_FUNC_END();
1295 }
1296
1297 void stb_update_rule(HIPC_API a_hAPI)
1298 {
1299         EM_DEBUG_FUNC_BEGIN();
1300
1301         int filter_id = 0;
1302         int buffer_size = 0;
1303         int err = EMAIL_ERROR_NONE;
1304         int *ret_nth_value = NULL;
1305         char* rule_stream = NULL;
1306         email_rule_t rule = {0};
1307     int nAPPID = emipc_get_app_id(a_hAPI);
1308     char *multi_user_name = NULL;
1309
1310     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1311         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1312         multi_user_name = NULL;
1313     }
1314
1315         /* get filter_id */
1316         if ((ret_nth_value = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0))) {
1317                 filter_id = *ret_nth_value;
1318         } else {
1319                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
1320                 goto FINISH_OFF;
1321         }
1322
1323         /* get rule */
1324         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
1325         EM_DEBUG_LOG("size [%d]", buffer_size);
1326         if(buffer_size <= 0)  {
1327                 err = EMAIL_ERROR_INVALID_PARAM;
1328                 goto FINISH_OFF;
1329         }
1330         rule_stream     = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
1331         if(!rule_stream) {
1332                 err = EMAIL_ERROR_INVALID_PARAM;
1333                 goto FINISH_OFF;
1334         }
1335         em_convert_byte_stream_to_rule(rule_stream, buffer_size, &rule);
1336
1337         /* call update handler */
1338         emdaemon_update_filter(multi_user_name, filter_id, &rule, &err);
1339
1340 FINISH_OFF:
1341         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1342                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1343
1344         if (!emipc_execute_stub_api(a_hAPI))
1345                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1346
1347         emcore_free_rule(&rule);
1348
1349     EM_SAFE_FREE(multi_user_name);
1350         EM_DEBUG_FUNC_END();
1351 }
1352
1353 void stb_move_all_mails(HIPC_API a_hAPI)
1354 {
1355         EM_DEBUG_FUNC_BEGIN();
1356         int err = EMAIL_ERROR_NONE;
1357         int src_mailbox_id = 0, dst_mailbox_id = 0;
1358     int nAPPID = emipc_get_app_id(a_hAPI);
1359     char *multi_user_name = NULL;
1360
1361     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1362         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1363         multi_user_name = NULL;
1364     }
1365
1366         /* src_mailbox_id */
1367         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &src_mailbox_id);
1368         EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1369
1370         if (src_mailbox_id > 0)
1371                 EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1372         else
1373                 EM_DEBUG_LOG("src_mailbox_id == 0");
1374
1375         /* dst_mailbox_id */
1376         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &dst_mailbox_id);
1377         EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1378
1379         if (dst_mailbox_id > 0)
1380                 EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1381         else
1382                 EM_DEBUG_LOG("dst_mailbox_id == 0");
1383
1384         if(emdaemon_move_mail_all_mails(multi_user_name, src_mailbox_id, dst_mailbox_id, &err))
1385                 EM_DEBUG_LOG("emdaemon_move_mail_all_mails:Success");
1386         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1387                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1388                 return;
1389         }
1390
1391         if (!emipc_execute_stub_api(a_hAPI)) {
1392                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1393                 return;
1394         }
1395
1396     EM_SAFE_FREE(multi_user_name);
1397         EM_DEBUG_FUNC_END();
1398 }
1399
1400 void stb_set_flags_field(HIPC_API a_hAPI)
1401 {
1402         EM_DEBUG_FUNC_BEGIN();
1403         int err = EMAIL_ERROR_NONE;
1404         email_flags_field_type field_type = 0;
1405         int account_id;
1406         int value = 0;
1407         int onserver = 0;
1408         int num = 0;
1409         int counter = 0;
1410         int *mail_ids = NULL;
1411     int nAPPID = emipc_get_app_id(a_hAPI);
1412     char *multi_user_name = NULL;
1413
1414     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1415         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1416         multi_user_name = NULL;
1417     }
1418
1419         /* account_id */
1420         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1421         EM_DEBUG_LOG("account_id [%d]", account_id);
1422
1423         /* Number of mail_ids */
1424         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
1425         EM_DEBUG_LOG("number of mails [%d]", num);
1426
1427         /* mail_id */
1428         mail_ids = em_malloc(sizeof(int) * num);
1429
1430         if(!mail_ids) {
1431                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1432                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1433                 goto FINISH_OFF;
1434         }
1435
1436         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
1437
1438         for(counter=0; counter < num; counter++)
1439                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1440
1441         /* field type */
1442         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &field_type);
1443         EM_DEBUG_LOG("field_type [%d]", field_type);
1444
1445         /* value */
1446         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &value);
1447         EM_DEBUG_LOG("value [%d]", value);
1448
1449         /*  on server */
1450         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 5, sizeof(int), &onserver);
1451         EM_DEBUG_LOG("onserver [%d]", onserver);
1452
1453         if(emdaemon_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, onserver, &err))
1454                 EM_DEBUG_LOG("emdaemon_set_flags_field - success");
1455
1456 FINISH_OFF:
1457
1458         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1459                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1460
1461         if (!emipc_execute_stub_api(a_hAPI))
1462                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1463
1464         if (mail_ids)
1465                 EM_SAFE_FREE(mail_ids);
1466
1467     EM_SAFE_FREE(multi_user_name);
1468         EM_DEBUG_FUNC_END();
1469 }
1470
1471 void stb_add_mail(HIPC_API a_hAPI)
1472 {
1473         EM_DEBUG_FUNC_BEGIN();
1474         int buffer_size = 0;
1475         int local_result = 0;
1476         int result_attachment_data_count = 0;
1477         int param_index = 0;
1478         int sync_server = 0;
1479         int err = EMAIL_ERROR_NONE;
1480         int i = 0;
1481         email_mail_data_t result_mail_data = {0};
1482         email_attachment_data_t *result_attachment_data = NULL;
1483         email_meeting_request_t result_meeting_request = {0};
1484         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
1485         email_account_server_t account_server_type = EMAIL_SERVER_TYPE_NONE;
1486     int nAPPID = emipc_get_app_id(a_hAPI);
1487     char *multi_user_name = NULL;
1488     char *prefix_path = NULL;
1489     char real_file_path[255] = {0};
1490
1491     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1492         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1493         multi_user_name = NULL;
1494     }
1495
1496         /* email_mail_data_t */;
1497         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1498         EM_DEBUG_LOG("email_mail_data_t buffer_size[%d]", buffer_size);
1499
1500         /* mail_data */
1501         if(buffer_size > 0)      {
1502                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1503                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1504         }
1505
1506         if (em_get_account_server_type_by_account_id(multi_user_name, result_mail_data.account_id, &account_server_type, true, &err) == false) {
1507                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
1508                 goto FINISH_OFF;
1509         }
1510
1511     /* Get the absolute path */
1512     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1513                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1514                 if (err != EMAIL_ERROR_NONE) {
1515                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
1516                         goto FINISH_OFF;
1517                 }
1518         } else {
1519                 prefix_path = strdup("");
1520         }
1521
1522         /* check smack rule for accessing file path */
1523         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_html) {
1524         memset(real_file_path, 0x00, sizeof(real_file_path));
1525         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
1526
1527                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1528                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1529                         err = EMAIL_ERROR_NO_SMACK_RULE;
1530                         goto FINISH_OFF;
1531                 }
1532         }
1533
1534         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_plain) {
1535         memset(real_file_path, 0x00, sizeof(real_file_path));
1536         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
1537
1538                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1539                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1540                         err = EMAIL_ERROR_NO_SMACK_RULE;
1541                         goto FINISH_OFF;
1542                 }
1543         }
1544
1545         if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_mail_data.file_path_mime_entity) {
1546         memset(real_file_path, 0x00, sizeof(real_file_path));
1547         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
1548
1549                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1550                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1551                         err = EMAIL_ERROR_NO_SMACK_RULE;
1552                         goto FINISH_OFF;
1553                 }
1554         }
1555         /* check smack rule - END */
1556
1557         /* attachment */
1558         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1559         EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1560
1561         if(buffer_size > 0)      {
1562                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1563                 em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1564
1565                 EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1566                 if (!result_attachment_data_count) {
1567                         EM_DEBUG_LOG("Not include attachment data");
1568                 } else {
1569                         if(!result_attachment_data) {
1570                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1571                                 err = EMAIL_ERROR_ON_PARSING;
1572                                 goto FINISH_OFF;
1573                         }
1574                 }
1575         }
1576         param_index++;
1577
1578         /* check smack rule for accessing file path */
1579         for (i = 0; i < result_attachment_data_count ; i++) {
1580                 if ((account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) && result_attachment_data[i].attachment_path && result_attachment_data[i].save_status) {
1581             memset(real_file_path, 0x00, sizeof(real_file_path));
1582             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
1583
1584                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1585                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1586                                 err = EMAIL_ERROR_NO_SMACK_RULE;
1587                                 goto FINISH_OFF;
1588                         }
1589                 }
1590         }
1591
1592         /* meeting request */
1593         EM_DEBUG_LOG("email_meeting_request_t");
1594         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1595                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1596                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1597                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1598
1599                 if(buffer_size > 0) {
1600                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1601                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1602                 }
1603         }
1604
1605         EM_DEBUG_LOG("sync_server");
1606         emipc_get_parameter(a_hAPI, ePARAMETER_IN, param_index++, sizeof(int), &sync_server);
1607
1608         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) {
1609                 EM_DEBUG_EXCEPTION("emdaemon_add_mail failed [%d]", err);
1610                 goto FINISH_OFF;
1611         }
1612
1613         local_result = 1;
1614         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1615                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1616         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1617                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1618         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1619                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1620         if (!emipc_execute_stub_api(a_hAPI))
1621                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1622
1623 FINISH_OFF:
1624         if ( local_result == 0 ) {
1625                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1626                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1627                 if (!emipc_execute_stub_api(a_hAPI))
1628                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1629         }
1630
1631         EM_SAFE_FREE(prefix_path);
1632
1633         emcore_free_mail_data(&result_mail_data);
1634
1635         if(result_attachment_data)
1636                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1637
1638         emstorage_free_meeting_request(&result_meeting_request);
1639
1640         em_flush_memory();
1641
1642     EM_SAFE_FREE(multi_user_name);
1643         EM_DEBUG_FUNC_END();
1644 }
1645
1646
1647 void stb_update_mail(HIPC_API a_hAPI)
1648 {
1649         EM_DEBUG_FUNC_BEGIN();
1650         int  buffer_size = 0;
1651         int  local_result = 0;
1652         int  result_attachment_data_count = 0;
1653         int  param_index = 0;
1654         int  sync_server = 0;
1655         int  *temp_buffer = NULL;
1656         int  err = EMAIL_ERROR_NONE;
1657         int  i = 0;
1658         email_mail_data_t result_mail_data = {0};
1659         email_attachment_data_t *result_attachment_data = NULL;
1660         email_meeting_request_t result_meeting_request = {0};
1661         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
1662     int nAPPID = emipc_get_app_id(a_hAPI);
1663     char *multi_user_name = NULL;
1664     char *prefix_path = NULL;
1665     char real_file_path[255] = {0};
1666
1667     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1668         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1669         multi_user_name = NULL;
1670     }
1671
1672         EM_DEBUG_LOG("email_mail_data_t");
1673         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1674
1675         if(buffer_size > 0)      {
1676                 char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1677                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1678         }
1679
1680     /* Get the absolute path */
1681     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1682                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1683                 if (err != EMAIL_ERROR_NONE) {
1684                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
1685                         goto FINISH_OFF;
1686                 }
1687         } else {
1688                 prefix_path = strdup("");
1689         }
1690
1691         /* check smack rule for accessing file path */
1692         if (result_mail_data.file_path_html) {
1693         memset(real_file_path, 0x00, sizeof(real_file_path));
1694         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_html);
1695
1696                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1697                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1698                         err = EMAIL_ERROR_NO_SMACK_RULE;
1699                         goto FINISH_OFF;
1700                 }
1701         }
1702
1703         if (result_mail_data.file_path_plain) {
1704         memset(real_file_path, 0x00, sizeof(real_file_path));
1705         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_plain);
1706
1707                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1708                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1709                         err = EMAIL_ERROR_NO_SMACK_RULE;
1710                         goto FINISH_OFF;
1711                 }
1712         }
1713
1714         if (result_mail_data.file_path_mime_entity) {
1715         memset(real_file_path, 0x00, sizeof(real_file_path));
1716         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_mail_data.file_path_mime_entity);
1717
1718                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1719                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1720                         err = EMAIL_ERROR_NO_SMACK_RULE;
1721                         goto FINISH_OFF;
1722                 }
1723         }
1724         /* check smack rule - END */
1725         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1726         if(buffer_size > 0)      {
1727                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1728                 em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1729
1730                 EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1731                 if (!result_attachment_data_count) {
1732                         EM_DEBUG_LOG("Not include attachment data");
1733                 } else {
1734                         if(!result_attachment_data) {
1735                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1736                                 err = EMAIL_ERROR_ON_PARSING;
1737                                 goto FINISH_OFF;
1738                         }
1739                 }
1740         }
1741         param_index++;
1742
1743         /* check smack rule for accessing file path */
1744         for (i = 0; i < result_attachment_data_count ; i++) {
1745                 if (result_attachment_data[i].attachment_path) {
1746             memset(real_file_path, 0x00, sizeof(real_file_path));
1747             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, result_attachment_data[i].attachment_path);
1748
1749                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
1750                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
1751                                 err = EMAIL_ERROR_NO_SMACK_RULE;
1752                                 goto FINISH_OFF;
1753                         }
1754                 }
1755         }
1756
1757         EM_DEBUG_LOG("email_meeting_request_t");
1758         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1759                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1760                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1761                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1762
1763                 if(buffer_size > 0) {
1764                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1765                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1766                 }
1767         }
1768
1769         EM_DEBUG_LOG("sync_server");
1770
1771         temp_buffer = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1772
1773         if(!temp_buffer) {
1774                 EM_DEBUG_EXCEPTION("emipc_get_nth_parameter_data[%d] failed", param_index - 1);
1775                 err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1776                 goto FINISH_OFF;
1777         }
1778
1779         sync_server = *temp_buffer;
1780
1781         if( (err = emdaemon_update_mail(multi_user_name, &result_mail_data, result_attachment_data,
1782                         result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1783                 EM_DEBUG_EXCEPTION("emdaemon_update_mail failed [%d]", err);
1784                 goto FINISH_OFF;
1785         }
1786
1787         local_result = 1;
1788         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1789                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1790         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1791                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1792         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1793                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1794         if (!emipc_execute_stub_api(a_hAPI))
1795                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1796
1797 FINISH_OFF:
1798         if ( local_result == 0 ) {
1799                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1800                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1801                 if (!emipc_execute_stub_api(a_hAPI))
1802                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1803         }
1804
1805         emcore_free_mail_data(&result_mail_data);
1806
1807         EM_SAFE_FREE(prefix_path);
1808
1809         if(result_attachment_data)
1810                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1811
1812         emstorage_free_meeting_request(&result_meeting_request);
1813
1814         em_flush_memory();
1815
1816     EM_SAFE_FREE(multi_user_name);
1817         EM_DEBUG_FUNC_END();
1818 }
1819
1820
1821 void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
1822 {
1823         EM_DEBUG_FUNC_BEGIN();
1824         int mailbox_id = 0, thread_id = 0, move_always_flag = 0;
1825         int err = EMAIL_ERROR_NONE;
1826         char *target_mailbox_name = NULL;
1827     int nAPPID = emipc_get_app_id(a_hAPI);
1828     char *multi_user_name = NULL;
1829
1830     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1831         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1832         multi_user_name = NULL;
1833     }
1834
1835         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1836         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1837
1838         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1839         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1840
1841         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &move_always_flag);
1842         EM_DEBUG_LOG("move_always_flag [%d]", move_always_flag);
1843
1844         if(emdaemon_move_mail_thread_to_mailbox(multi_user_name, thread_id, mailbox_id, move_always_flag, &err))
1845                 EM_DEBUG_LOG("emdaemon_move_mail_thread_to_mailbox success");
1846
1847         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1848                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1849                 EM_SAFE_FREE(target_mailbox_name);
1850                 EM_SAFE_FREE(multi_user_name);
1851                 return;
1852         }
1853
1854         if (!emipc_execute_stub_api(a_hAPI)) {
1855                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1856                 EM_SAFE_FREE(target_mailbox_name);
1857                 EM_SAFE_FREE(multi_user_name);
1858                 return;
1859         }
1860
1861         EM_SAFE_FREE(target_mailbox_name);
1862     EM_SAFE_FREE(multi_user_name);
1863         EM_DEBUG_FUNC_END();
1864 }
1865
1866 void stb_delete_thread(HIPC_API a_hAPI)
1867 {
1868         EM_DEBUG_FUNC_BEGIN();
1869
1870         int thread_id = 0, delete_always_flag = 0;
1871         int handle = 0;
1872         int err = EMAIL_ERROR_NONE;
1873     int nAPPID = emipc_get_app_id(a_hAPI);
1874     char *multi_user_name = NULL;
1875
1876     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1877         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1878         multi_user_name = NULL;
1879     }
1880
1881         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1882         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1883
1884         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &delete_always_flag);
1885         EM_DEBUG_LOG("delete_always_flag [%d]", delete_always_flag);
1886
1887         if(emdaemon_delete_mail_thread(multi_user_name, thread_id, delete_always_flag, &handle, &err))
1888                 EM_DEBUG_LOG("emdaemon_delete_mail_thread success");
1889
1890         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1891                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1892                 EM_SAFE_FREE(multi_user_name);
1893                 return;
1894         }
1895
1896         if (!emipc_execute_stub_api(a_hAPI)) {
1897                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1898                 EM_SAFE_FREE(multi_user_name);
1899                 return;
1900         }
1901
1902     EM_SAFE_FREE(multi_user_name);
1903         EM_DEBUG_FUNC_END();
1904 }
1905
1906 void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
1907 {
1908         EM_DEBUG_FUNC_BEGIN();
1909
1910         int thread_id = 0, seen_flag = 0, on_server = 0;
1911         int handle = 0;
1912         int err = EMAIL_ERROR_NONE;
1913     int nAPPID = emipc_get_app_id(a_hAPI);
1914     char *multi_user_name = NULL;
1915
1916     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1917         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1918         multi_user_name = NULL;
1919     }
1920
1921         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1922         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1923
1924         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &seen_flag);
1925         EM_DEBUG_LOG("seen_flag [%d]", seen_flag);
1926
1927         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &on_server);
1928         EM_DEBUG_LOG("on_server [%d]", on_server);
1929
1930         if(emdaemon_modify_seen_flag_of_thread(multi_user_name, thread_id, seen_flag, on_server, &handle, &err))
1931                 EM_DEBUG_LOG("emdaemon_modify_seen_flag_of_thread success");
1932
1933         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1934                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1935                 EM_SAFE_FREE(multi_user_name);
1936                 return;
1937         }
1938
1939         if (!emipc_execute_stub_api(a_hAPI)) {
1940                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1941                 EM_SAFE_FREE(multi_user_name);
1942                 return;
1943         }
1944
1945     EM_SAFE_FREE(multi_user_name);
1946         EM_DEBUG_FUNC_END();
1947 }
1948
1949 void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
1950 {
1951         EM_DEBUG_FUNC_BEGIN();
1952
1953         int mailbox_id = 0, on_server = 0;
1954         int handle = 0;
1955         int err = EMAIL_ERROR_NONE;
1956     int nAPPID = emipc_get_app_id(a_hAPI);
1957     char *multi_user_name = NULL;
1958
1959     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
1960         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
1961         multi_user_name = NULL;
1962     }
1963
1964         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), (void*)&mailbox_id);
1965         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1966
1967         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
1968         EM_DEBUG_LOG("on_server [%d]", on_server);
1969
1970         if( (err = emdaemon_expunge_mails_deleted_flagged(multi_user_name, mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
1971                 EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
1972
1973         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1974                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1975                 return;
1976         }
1977
1978         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
1979                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1980                 return;
1981         }
1982
1983         if (!emipc_execute_stub_api(a_hAPI)) {
1984                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1985                 return;
1986         }
1987
1988     EM_SAFE_FREE(multi_user_name);
1989         EM_DEBUG_FUNC_END();
1990 }
1991
1992 void stb_move_mail(HIPC_API a_hAPI)
1993 {
1994         int err = EMAIL_ERROR_NONE;
1995         int num = 0, counter = 0, mailbox_id = 0;
1996     int nAPPID = emipc_get_app_id(a_hAPI);
1997     char *multi_user_name = NULL;
1998
1999     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2000         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2001         multi_user_name = NULL;
2002     }
2003
2004         /* Number of mail_ids */
2005         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &num);
2006         EM_DEBUG_LOG("number of mails [%d]", num);
2007
2008         /* mail_id */
2009         int mail_ids[num];
2010         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, num * sizeof(int), mail_ids);
2011
2012         for(counter = 0; counter < num; counter++)
2013                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
2014
2015         /* target_mailbox_id */
2016         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mailbox_id);
2017         EM_DEBUG_LOG("target_mailbox_id [%d]", mailbox_id);
2018
2019         if (mailbox_id > 0)
2020                 EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
2021         else
2022                 EM_DEBUG_LOG("mailbox_id == 0");
2023
2024         if(emdaemon_move_mail(multi_user_name, mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
2025                 EM_DEBUG_LOG("emdaemon_move_mail success");
2026
2027         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2028                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
2029
2030         if (!emipc_execute_stub_api(a_hAPI))
2031                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
2032
2033     EM_SAFE_FREE(multi_user_name);
2034         EM_DEBUG_FUNC_END();
2035 }
2036
2037 void stb_delete_rule(HIPC_API a_hAPI)
2038 {
2039         EM_DEBUG_FUNC_BEGIN();
2040
2041         int filter_id = 0;
2042         int err = EMAIL_ERROR_NONE;
2043     int nAPPID = emipc_get_app_id(a_hAPI);
2044     char *multi_user_name = NULL;
2045
2046     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2047         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2048         multi_user_name = NULL;
2049     }
2050
2051         /* filter_id */
2052         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
2053
2054         if(emdaemon_delete_filter(multi_user_name, filter_id, &err))
2055                 err = EMAIL_ERROR_NONE;
2056
2057         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2058                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2059
2060         if (!emipc_execute_stub_api(a_hAPI))
2061                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2062
2063     EM_SAFE_FREE(multi_user_name);
2064         EM_DEBUG_FUNC_END();
2065 }
2066
2067 void stb_apply_rule(HIPC_API a_hAPI)
2068 {
2069         EM_DEBUG_FUNC_BEGIN();
2070
2071         int filter_id = 0;
2072         int err = EMAIL_ERROR_NONE;
2073     int nAPPID = emipc_get_app_id(a_hAPI);
2074     char *multi_user_name = NULL;
2075
2076     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2077         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2078         multi_user_name = NULL;
2079     }
2080
2081         /* filter_id */
2082         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
2083
2084         if(emdaemon_apply_filter(multi_user_name, filter_id, &err))
2085                 err = EMAIL_ERROR_NONE;
2086
2087         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2088                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2089
2090         if (!emipc_execute_stub_api(a_hAPI))
2091                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2092
2093     EM_SAFE_FREE(multi_user_name);
2094         EM_DEBUG_FUNC_END();
2095 }
2096
2097 void stb_add_attachment(HIPC_API a_hAPI)
2098 {
2099         EM_DEBUG_FUNC_BEGIN();
2100         int buffer_size = 0;
2101         int err = EMAIL_ERROR_NONE;
2102         int mail_id = -1;
2103         int attachment_count = 0;
2104         int i = 0;
2105         char* attachment_stream = NULL;
2106         email_attachment_data_t* attachment = NULL;
2107         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
2108     int nAPPID = emipc_get_app_id(a_hAPI);
2109     char *multi_user_name = NULL;
2110     char *prefix_path = NULL;
2111     char real_file_path[255] = {0};
2112
2113     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2114         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2115         multi_user_name = NULL;
2116     }
2117
2118         /* mail_id */
2119         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2120
2121         /* attachment */
2122         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2123
2124         if(buffer_size > 0)      {
2125                 attachment_stream = (char*)em_malloc(buffer_size);
2126                 if (attachment_stream == NULL) {
2127                         EM_DEBUG_EXCEPTION("em_malloc failed");
2128                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2129                         goto FINISH_OFF;
2130                 }
2131
2132                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, attachment_stream);
2133                 em_convert_byte_stream_to_attachment_data(attachment_stream, buffer_size, &attachment, &attachment_count);
2134                 EM_SAFE_FREE(attachment_stream);
2135         }
2136
2137         if (!attachment) {
2138                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed  ");
2139                 err = EMAIL_ERROR_ON_PARSING;
2140                 goto FINISH_OFF;
2141         }
2142
2143     /* Get the absolute path */
2144     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2145                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2146                 if (err != EMAIL_ERROR_NONE) {
2147                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
2148                         goto FINISH_OFF;
2149                 }
2150         } else {
2151                 prefix_path = strdup("");
2152         }
2153
2154         /* check smack rule for accessing file path */
2155         for (i = 0; i < attachment_count ; i++) {
2156                 if (attachment[i].attachment_path) {
2157             memset(real_file_path, 0x00, sizeof(real_file_path));
2158             SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, attachment[i].attachment_path);
2159
2160                         if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
2161                                 EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
2162                                 err = EMAIL_ERROR_NO_SMACK_RULE;
2163                                 goto FINISH_OFF;
2164                         }
2165                 }
2166         }
2167
2168         emdaemon_add_attachment(multi_user_name, mail_id, attachment, &err);
2169
2170 FINISH_OFF:
2171
2172         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2173                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2174         if(EMAIL_ERROR_NONE == err) {
2175                 EM_DEBUG_LOG("emdaemon_add_attachment -Success");
2176
2177                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(attachment->attachment_id), sizeof(int)))
2178                         EM_DEBUG_EXCEPTION("emipc_add_parameter attachment_id failed ");
2179         }
2180                 if (!emipc_execute_stub_api(a_hAPI))
2181                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2182
2183         EM_SAFE_FREE(attachment);
2184     EM_SAFE_FREE(multi_user_name);
2185         EM_SAFE_FREE(prefix_path);
2186
2187         EM_DEBUG_FUNC_END();
2188 }
2189
2190 void stb_get_attachment(HIPC_API a_hAPI)
2191 {
2192         EM_DEBUG_FUNC_BEGIN();
2193         int err = EMAIL_ERROR_NONE;
2194         int attachment_id;
2195         char* attachment_stream = NULL;
2196         email_attachment_data_t* attachment = NULL;
2197         int size = 0;
2198     int nAPPID = emipc_get_app_id(a_hAPI);
2199     char *multi_user_name = NULL;
2200
2201     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2202         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2203         multi_user_name = NULL;
2204     }
2205
2206         /* attachment_id */
2207         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
2208
2209         emdaemon_get_attachment(multi_user_name, attachment_id, &attachment, &err);
2210
2211         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2212                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2213
2214         if(EMAIL_ERROR_NONE == err) {
2215                 EM_DEBUG_LOG("emdaemon_get_attachment - Success");
2216                 /* attachment */
2217                 attachment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
2218                 if(!attachment_stream) { /*prevent 26263*/
2219                         emcore_free_attachment_data(&attachment, 1, &err);
2220                         EM_SAFE_FREE(multi_user_name);
2221                         return;
2222                 }
2223                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
2224
2225                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, attachment_stream, size))
2226                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
2227         }
2228
2229         if (!emipc_execute_stub_api(a_hAPI)) {
2230                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2231         }
2232
2233         EM_SAFE_FREE(attachment_stream);
2234         emcore_free_attachment_data(&attachment, 1, &err);
2235
2236     EM_SAFE_FREE(multi_user_name);
2237         EM_DEBUG_FUNC_END();
2238 }
2239
2240 void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
2241 {
2242         EM_DEBUG_FUNC_BEGIN();
2243         int err = EMAIL_ERROR_NONE;
2244         int account_id = 0;
2245         int handle = 0;
2246     int nAPPID = emipc_get_app_id(a_hAPI);
2247     char *multi_user_name = NULL;
2248
2249     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2250             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2251             multi_user_name = NULL;
2252     }
2253
2254         /* account_id */
2255         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2256
2257         /*need to check: why err value is changed? */
2258         if(emdaemon_get_imap_mailbox_list(multi_user_name, account_id, "", &handle, &err))
2259                 err = EMAIL_ERROR_NONE;
2260
2261         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2262                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
2263         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2264                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
2265         if (!emipc_execute_stub_api(a_hAPI))
2266                 EM_DEBUG_LOG("emipc_execute_stub_api failed  ");
2267
2268     EM_SAFE_FREE(multi_user_name);
2269         EM_DEBUG_FUNC_END();
2270 }
2271
2272 void stb_delete_attachment(HIPC_API a_hAPI)
2273 {
2274         EM_DEBUG_FUNC_BEGIN();
2275         int err = EMAIL_ERROR_NONE;
2276         int attachment_id = 0;
2277     int nAPPID = emipc_get_app_id(a_hAPI);
2278     char *multi_user_name = NULL;
2279
2280     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2281         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2282         multi_user_name = NULL;
2283     }
2284
2285         /* attachment_index */
2286         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
2287
2288         emdaemon_delete_mail_attachment(multi_user_name, attachment_id, &err);
2289
2290         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2291                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2292         if (!emipc_execute_stub_api(a_hAPI))
2293                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2294
2295     EM_SAFE_FREE(multi_user_name);
2296         EM_DEBUG_FUNC_END();
2297 }
2298
2299 void stb_download_attachment(HIPC_API a_hAPI)
2300 {
2301         EM_DEBUG_FUNC_BEGIN();
2302
2303         int err = EMAIL_ERROR_NONE;
2304         int mail_id = 0;
2305         int nth = 0;
2306         int handle = 0;
2307         int account_id = 0;
2308     int nAPPID = emipc_get_app_id(a_hAPI);
2309     char *multi_user_name = NULL;
2310
2311     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2312         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2313         multi_user_name = NULL;
2314     }
2315
2316         EM_DEBUG_LOG("account_id");
2317         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2318
2319         EM_DEBUG_LOG("mail_id");
2320         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
2321
2322         EM_DEBUG_LOG("nth");
2323         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &nth);
2324
2325         if(emdaemon_download_attachment(multi_user_name, account_id, mail_id, nth, &handle, &err)) {
2326                 err = EMAIL_ERROR_NONE;
2327
2328                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2329                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2330                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
2331                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2332                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
2333                 if (!emipc_execute_stub_api(a_hAPI))
2334                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2335         }
2336         else {
2337                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2338                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2339                 /* Download handle - 17-Apr-09 */
2340                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
2341                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2342                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
2343                 if (!emipc_execute_stub_api(a_hAPI))
2344                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2345         }
2346
2347     EM_SAFE_FREE(multi_user_name);
2348         EM_DEBUG_FUNC_END();
2349 }
2350
2351 void stb_mail_send_saved(HIPC_API a_hAPI)
2352 {
2353         EM_DEBUG_FUNC_BEGIN();
2354         int err = EMAIL_ERROR_NONE;
2355         int account_id = 0;
2356     int nAPPID = emipc_get_app_id(a_hAPI);
2357     char *multi_user_name = NULL;
2358
2359     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2360         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2361         multi_user_name = NULL;
2362     }
2363
2364         /* account_id */
2365         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2366
2367         EM_DEBUG_LOG("calling emdaemon_send_mail_saved");
2368         if(emdaemon_send_mail_saved(multi_user_name, account_id, NULL, &err))
2369                 err = EMAIL_ERROR_NONE;
2370
2371         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2372                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2373
2374         if (!emipc_execute_stub_api(a_hAPI))
2375                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2376
2377     EM_SAFE_FREE(multi_user_name);
2378         EM_DEBUG_FUNC_END();
2379 }
2380
2381 void stb_add_read_receipt(HIPC_API a_hAPI){
2382         EM_DEBUG_FUNC_BEGIN();
2383         int read_mail_id = 0;
2384         int receipt_mail_id = 0;
2385         int err = EMAIL_ERROR_NONE;
2386     int nAPPID = emipc_get_app_id(a_hAPI);
2387     char *multi_user_name = NULL;
2388
2389     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2390         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2391         multi_user_name = NULL;
2392     }
2393
2394         /* read_mail_id */
2395         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &read_mail_id);
2396         EM_DEBUG_LOG("read_mail_id [%d]", read_mail_id);
2397
2398         if( (err = emcore_add_read_receipt(multi_user_name, read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
2399                 EM_DEBUG_EXCEPTION("emcore_add_read_receipt failed [%d]", err);
2400
2401         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2402                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2403
2404         if (err == EMAIL_ERROR_NONE) {
2405                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &receipt_mail_id, sizeof(int)))
2406                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2407         }
2408
2409         if (!emipc_execute_stub_api(a_hAPI))
2410                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2411
2412     EM_SAFE_FREE(multi_user_name);
2413         EM_DEBUG_FUNC_END("err [%d]", err);
2414 }
2415
2416 void stb_retry_sending_mail(HIPC_API a_hAPI)
2417 {
2418
2419         EM_DEBUG_FUNC_BEGIN();
2420         int mail_id = 0;
2421         int timeout_in_sec = 0;
2422         int err = EMAIL_ERROR_NONE;
2423     int nAPPID = emipc_get_app_id(a_hAPI);
2424     char *multi_user_name = NULL;
2425
2426     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2427         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2428         multi_user_name = NULL;
2429     }
2430
2431         /* Mail_id */
2432         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2433         EM_DEBUG_LOG("mail_id [%d]", mail_id);
2434
2435         /* timeout_in_sec */
2436         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &timeout_in_sec);
2437         EM_DEBUG_LOG("timeout_in_sec [%d]", timeout_in_sec);
2438
2439         if(emdaemon_send_mail_retry(multi_user_name, mail_id, timeout_in_sec,&err))
2440                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
2441
2442         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2443                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2444
2445     if (!emipc_execute_stub_api(a_hAPI))
2446             EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2447
2448     EM_SAFE_FREE(multi_user_name);
2449         EM_DEBUG_FUNC_END();
2450 }
2451
2452 void stb_cancel_job(HIPC_API a_hAPI)
2453 {
2454         EM_DEBUG_FUNC_BEGIN();
2455         int account_id = 0;
2456         int handle = 0;
2457         int err = EMAIL_ERROR_NONE;
2458     int nAPPID = emipc_get_app_id(a_hAPI);
2459     char *multi_user_name = NULL;
2460
2461     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2462         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2463         multi_user_name = NULL;
2464     }
2465
2466         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2467         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &handle);
2468         EM_DEBUG_LOG("account_id [%d] handle [%d]", account_id, handle);
2469
2470         if(emdaemon_cancel_job(account_id, handle, &err))
2471                 err = EMAIL_ERROR_NONE;
2472
2473         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2474                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2475         if (!emipc_execute_stub_api(a_hAPI))
2476                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2477
2478     EM_SAFE_FREE(multi_user_name);
2479         EM_DEBUG_FUNC_END();
2480 }
2481
2482 void stb_cancel_send_mail_job(HIPC_API a_hAPI)
2483 {
2484         EM_DEBUG_FUNC_BEGIN();
2485         int mail_id = 0;
2486         int err = EMAIL_ERROR_NONE;
2487         int account_id = 0;
2488     int nAPPID = emipc_get_app_id(a_hAPI);
2489     char *multi_user_name = NULL;
2490
2491     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2492         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2493         multi_user_name = NULL;
2494     }
2495
2496         /* account_id */
2497         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2498         EM_DEBUG_LOG("account_id [%d]", account_id);
2499
2500         /* Mail_id */
2501         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
2502         EM_DEBUG_LOG("mail_id [%d]", mail_id);
2503
2504         if(emdaemon_cancel_sending_mail_job(account_id,mail_id,&err))
2505                 EM_DEBUG_LOG("success");
2506
2507         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2508                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2509
2510         if (!emipc_execute_stub_api(a_hAPI))
2511                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2512
2513     EM_SAFE_FREE(multi_user_name);
2514         EM_DEBUG_FUNC_END();
2515 }
2516
2517 void stb_search_mail_on_server(HIPC_API a_hAPI)
2518 {
2519         EM_DEBUG_FUNC_BEGIN();
2520         int                    err = EMAIL_ERROR_NONE;
2521         int                    account_id = 0;
2522         int                                        mailbox_id = 0;
2523         int                    buffer_size = 0;
2524         int                    search_filter_count = 0;
2525         char                  *stream_for_search_filter_list = NULL;
2526         unsigned int           job_handle = 0;
2527         email_search_filter_t *search_filter_list = NULL;
2528     int nAPPID = emipc_get_app_id(a_hAPI);
2529     char *multi_user_name = NULL;
2530
2531     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2532         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2533         multi_user_name = NULL;
2534     }
2535
2536         /* account_id */
2537         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2538         EM_DEBUG_LOG("account_id [%d]", account_id);
2539
2540         /* mailbox_id */
2541         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
2542         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
2543
2544         /* search_filter_list */
2545         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
2546
2547         if(buffer_size > 0)      {
2548                 stream_for_search_filter_list = (char*)em_malloc(buffer_size);
2549                 if (stream_for_search_filter_list == NULL) {
2550                         EM_DEBUG_EXCEPTION("out of memory");
2551                         EM_SAFE_FREE(multi_user_name);
2552                         return;
2553                 }
2554
2555                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, stream_for_search_filter_list);
2556                 em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, &search_filter_list, &search_filter_count);
2557                 EM_SAFE_FREE(stream_for_search_filter_list);
2558         }
2559
2560         if(!emdaemon_search_mail_on_server(multi_user_name, account_id ,mailbox_id, search_filter_list, search_filter_count, &job_handle, &err))
2561                 EM_DEBUG_LOG("success");
2562
2563         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2564                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2565
2566         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
2567                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2568
2569         if (!emipc_execute_stub_api(a_hAPI))
2570                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2571
2572     EM_SAFE_FREE(multi_user_name);
2573         EM_DEBUG_FUNC_END();
2574 }
2575
2576 void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
2577 {
2578         EM_DEBUG_FUNC_BEGIN();
2579         int                      err = EMAIL_ERROR_NONE;
2580         int                      account_id = 0;
2581         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
2582     int nAPPID = emipc_get_app_id(a_hAPI);
2583     char *multi_user_name = NULL;
2584
2585     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2586         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2587         multi_user_name = NULL;
2588     }
2589
2590         /* account_id */
2591         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2592
2593         EM_DEBUG_LOG("account_id [%d]", account_id);
2594
2595         if (!emstorage_get_mailbox_by_name(multi_user_name, account_id, -1, EMAIL_SEARCH_RESULT_MAILBOX_NAME, &mailbox_tbl, false, &err)) {
2596                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed [%d]", err);
2597                 goto FINISH_OFF;
2598         }
2599
2600         if (!emstorage_delete_mail_by_mailbox(multi_user_name, mailbox_tbl, true, &err))
2601                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
2602
2603 FINISH_OFF:
2604
2605         if (mailbox_tbl) {
2606                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
2607         }
2608
2609         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2610                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2611
2612         if (!emipc_execute_stub_api(a_hAPI))
2613                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2614
2615         EM_SAFE_FREE(multi_user_name);
2616         EM_DEBUG_FUNC_END();
2617 }
2618
2619
2620
2621 void stb_add_account_with_validation(HIPC_API a_hAPI)
2622 {
2623         EM_DEBUG_FUNC_BEGIN();
2624         int buffer_size = 0;
2625         int local_result = 0;
2626         int handle = 0;
2627         char* stream = NULL;
2628         email_account_t *account = NULL;
2629         email_account_t *ref_account = NULL;
2630         int ref_check_interval = 0;
2631         int ref_account_id = 0;
2632         int err = EMAIL_ERROR_NONE;
2633     int nAPPID = emipc_get_app_id(a_hAPI);
2634     char *multi_user_name = NULL;
2635
2636     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2637         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2638         multi_user_name = NULL;
2639     }
2640
2641         /* get account info */
2642         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2643         EM_DEBUG_LOG("size [%d]", buffer_size);
2644         if(buffer_size <= 0) {
2645                 err = EMAIL_ERROR_INVALID_PARAM;
2646                 goto FINISH_OFF;
2647         }
2648
2649         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
2650         if(!stream) {
2651                 err = EMAIL_ERROR_INVALID_PARAM;
2652                 goto FINISH_OFF;
2653         }
2654
2655         account = em_malloc(sizeof(email_account_t));
2656
2657         if(account == NULL) {
2658                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2659                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2660                 goto FINISH_OFF;
2661         }
2662
2663         em_convert_byte_stream_to_account(stream, buffer_size, account);
2664     account->user_name = EM_SAFE_STRDUP(multi_user_name);
2665
2666         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
2667                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
2668                 goto FINISH_OFF;
2669         }
2670
2671         ref_account = emcore_get_account_reference(multi_user_name, account->account_id, false);
2672         if (!ref_account) {
2673                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
2674                 goto FINISH_OFF;
2675         }
2676
2677         ref_check_interval = ref_account->check_interval;
2678         ref_account_id     = ref_account->account_id;
2679
2680         if(!emdaemon_validate_account_and_create(multi_user_name, ref_account, &handle, &err)) {
2681                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_create fail [%d]", err);
2682                 goto FINISH_OFF;
2683         }
2684 #ifdef __FEATURE_AUTO_POLLING__
2685         /*  start auto polling, if check_interval not zero */
2686         if(ref_check_interval > 0 || (ref_account->peak_days > 0 && ref_account->peak_interval > 0)) {
2687                 if(!emdaemon_add_polling_alarm(multi_user_name, ref_account_id))
2688                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
2689         }
2690 #ifdef __FEATURE_IMAP_IDLE__
2691         else if(ref_check_interval == 0 || (ref_account->peak_days > 0 && ref_account->peak_interval == 0))
2692                 emcore_refresh_imap_idle_thread();
2693 #endif /* __FEATURE_IMAP_IDLE__ */
2694 #endif /*  __FEATURE_AUTO_POLLING__ */
2695         /*  add account details to contact DB  */
2696         /*  emdaemon_insert_accountinfo_to_contact(account); */
2697
2698         local_result = 1;
2699         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2700                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2701         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2702                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2703         if (!emipc_execute_stub_api(a_hAPI))
2704                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2705
2706 FINISH_OFF:
2707         if ( local_result == 0 ) { /* there is an error */
2708                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2709                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
2710                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2711                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
2712                 if (!emipc_execute_stub_api(a_hAPI))
2713                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2714         }
2715
2716 /*      note: account is freed in thread_func_branch_command, which is run by other thread */
2717 /*      emcore_free_account(account); */
2718
2719     EM_SAFE_FREE(multi_user_name);
2720         EM_DEBUG_FUNC_END();
2721 }
2722
2723 void stb_backup_account(HIPC_API a_hAPI)
2724 {
2725         EM_DEBUG_FUNC_BEGIN();
2726
2727 #ifdef __FEATURE_BACKUP_ACCOUNT__
2728         char *file_path = NULL;
2729         int local_result = 0, err_code = 0, file_path_length = 0;
2730     int nAPPID = emipc_get_app_id(a_hAPI);
2731     char *multi_user_name = NULL;
2732
2733     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2734             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2735             multi_user_name = NULL;
2736     }
2737
2738         /* file_path_length */
2739         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2740         if(file_path_length > 0) {
2741                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2742                 file_path = em_malloc(file_path_length);
2743                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2744                 EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
2745                 local_result = emcore_backup_accounts(multi_user_name, (const char*)file_path, &err_code);
2746         }
2747
2748         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2749                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2750
2751         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2752                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2753
2754         if (!emipc_execute_stub_api(a_hAPI))
2755                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2756
2757         EM_SAFE_FREE(file_path);
2758     EM_SAFE_FREE(multi_user_name);
2759 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2760         EM_DEBUG_FUNC_END();
2761 }
2762
2763 void stb_restore_account(HIPC_API a_hAPI)
2764 {
2765         EM_DEBUG_FUNC_BEGIN();
2766 #ifdef __FEATURE_BACKUP_ACCOUNT__
2767         char *file_path = NULL;
2768         int result = 0, err_code = 0, file_path_length = 0;
2769     int nAPPID = emipc_get_app_id(a_hAPI);
2770     char *multi_user_name = NULL;
2771
2772     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2773             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2774             multi_user_name = NULL;
2775     }
2776
2777         /* file_path_length */
2778         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2779         if(file_path_length > 0)  {
2780                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2781                 file_path = em_malloc(file_path_length);
2782                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2783                 EM_DEBUG_LOG_SEC("file_path [%s]", file_path);
2784         }
2785
2786         /* file_path could be NULL */
2787         err_code = emcore_restore_accounts(multi_user_name, (const char*)file_path);
2788
2789         result = (err_code == EMAIL_ERROR_NONE)?1:0;
2790
2791         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result, sizeof(int)))
2792                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2793
2794         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2795                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2796
2797         if (!emipc_execute_stub_api(a_hAPI))
2798                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2799
2800         EM_SAFE_FREE(file_path);
2801     EM_SAFE_FREE(multi_user_name);
2802 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2803         EM_DEBUG_FUNC_END();
2804 }
2805
2806
2807 void stb_get_password_length(HIPC_API a_hAPI)
2808 {
2809         EM_DEBUG_FUNC_BEGIN();
2810         int account_id = 0;
2811         int password_type = 0;
2812         int local_result = 0;
2813         int err_code = 0;
2814         int password_length = 0;
2815     int nAPPID = emipc_get_app_id(a_hAPI);
2816     char *multi_user_name = NULL;
2817
2818     if ((err_code = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2819             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err_code);
2820             multi_user_name = NULL;
2821     }
2822
2823         /* account_id */
2824         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2825         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &password_type);
2826
2827         local_result = emstorage_get_password_length_of_account(multi_user_name, account_id, password_type, &password_length,&err_code);
2828
2829         EM_DEBUG_LOG("password_length [%d]", password_length);
2830
2831         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2832                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2833
2834         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2835                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2836
2837         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &password_length, sizeof(int)))
2838                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2839
2840         if (!emipc_execute_stub_api(a_hAPI))
2841                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2842
2843     EM_SAFE_FREE(multi_user_name);
2844         EM_DEBUG_FUNC_END();
2845 }
2846
2847 void stb_get_task_information(HIPC_API a_hAPI)
2848 {
2849         EM_DEBUG_FUNC_BEGIN();
2850         int err = 0;
2851         int task_information_count = 0;
2852         int stream_length;
2853         email_task_information_t *task_information = NULL;
2854         char *task_information_stream = NULL;
2855     int nAPPID = emipc_get_app_id(a_hAPI);
2856     char *multi_user_name = NULL;
2857
2858     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2859         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2860         multi_user_name = NULL;
2861     }
2862
2863         err = emcore_get_task_information(&task_information, &task_information_count);
2864
2865         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2866                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2867
2868         /* email_task_information_t */
2869         if(task_information_count) {
2870                 task_information_stream = em_convert_task_information_to_byte_stream(task_information, task_information_count, &stream_length);
2871
2872                 if((stream_length > 0) && !emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, task_information_stream, stream_length)) {
2873                         EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
2874                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2875                 }
2876         }
2877
2878         if (!emipc_execute_stub_api(a_hAPI))
2879                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2880
2881         if(task_information) {
2882                 EM_SAFE_FREE(task_information);
2883         }
2884
2885     EM_SAFE_FREE(multi_user_name);
2886         EM_DEBUG_FUNC_END();
2887 }
2888
2889 void stb_add_certificate(HIPC_API a_hAPI)
2890 {
2891         int err = EMAIL_ERROR_NONE;
2892         int cert_file_len = 0;
2893         int email_address_len = 0;
2894         char *cert_file_path = NULL;
2895         char *email_address = NULL;
2896         emipc_email_api_info *api_info = (emipc_email_api_info *)a_hAPI;
2897     int nAPPID = emipc_get_app_id(a_hAPI);
2898     char *multi_user_name = NULL;
2899     char *prefix_path = NULL;
2900     char real_file_path[255] = {0};
2901
2902     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2903         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2904         multi_user_name = NULL;
2905     }
2906
2907     /* Get the absolute path */
2908     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2909                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2910                 if (err != EMAIL_ERROR_NONE) {
2911                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", err);
2912                         goto FINISH_OFF;
2913                 }
2914         } else {
2915                 prefix_path = strdup("");
2916         }
2917
2918         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2919         if (cert_file_len > 0) {
2920                 cert_file_path = em_malloc(cert_file_len + 1);
2921                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2922         }
2923
2924         /* check smack rule for accessing file path */
2925         if (cert_file_path) {
2926         memset(real_file_path, 0x00, sizeof(real_file_path));
2927         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, cert_file_path);
2928
2929                 if (!emdaemon_check_smack_rule(api_info->response_id, real_file_path)) {
2930                         EM_DEBUG_EXCEPTION("emdaemon_check_smack_rule fail");
2931                         err = EMAIL_ERROR_NO_SMACK_RULE;
2932                         goto FINISH_OFF;
2933                 }
2934         }
2935
2936         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2937         if (email_address_len > 0) {
2938                 email_address = em_malloc(email_address_len + 1);
2939                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, email_address_len, email_address);
2940         }
2941
2942         if (!emcore_add_public_certificate(multi_user_name, cert_file_path, email_address, &err)) {
2943                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2944         }
2945
2946 FINISH_OFF:
2947
2948         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2949                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2950
2951         if (EMAIL_ERROR_NONE == err) {
2952                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2953         }
2954
2955         if (!emipc_execute_stub_api(a_hAPI))
2956                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2957
2958         EM_SAFE_FREE(prefix_path);
2959         EM_SAFE_FREE(cert_file_path);
2960         EM_SAFE_FREE(email_address);
2961     EM_SAFE_FREE(multi_user_name);
2962         EM_DEBUG_FUNC_END();
2963 }
2964
2965 void stb_delete_certificate(HIPC_API a_hAPI)
2966 {
2967         int err = EMAIL_ERROR_NONE;
2968         int email_address_len = 0;
2969         char *email_address = NULL;
2970         char temp_email_address[130] = {0, };
2971     int nAPPID = emipc_get_app_id(a_hAPI);
2972     char *multi_user_name = NULL;
2973
2974     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
2975         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
2976         multi_user_name = NULL;
2977     }
2978
2979         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2980         if (email_address_len > 0) {
2981                 EM_DEBUG_LOG("email address string length [%d]", email_address_len);
2982                 email_address = em_malloc(email_address_len + 1);
2983                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, email_address_len, email_address);
2984                 EM_DEBUG_LOG_SEC("email address [%s]", email_address);
2985         }
2986
2987         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
2988         if (!emcore_delete_public_certificate(multi_user_name, temp_email_address, &err)) {
2989                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2990         }
2991
2992         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2993                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2994
2995         if (EMAIL_ERROR_NONE == err) {
2996                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2997         }
2998
2999         if (!emipc_execute_stub_api(a_hAPI))
3000                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3001
3002         
3003         EM_SAFE_FREE(email_address);
3004     EM_SAFE_FREE(multi_user_name);
3005         EM_DEBUG_FUNC_END();    
3006 }
3007
3008 void stb_verify_signature(HIPC_API a_hAPI)
3009 {
3010         int err = EMAIL_ERROR_NONE;
3011         int verify = 0;
3012         int mail_id = 0;
3013         int count = 0;
3014         int attachment_tbl_count = 0;
3015         email_mail_data_t *mail_data = NULL;
3016         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
3017     int nAPPID = emipc_get_app_id(a_hAPI);
3018     char *multi_user_name = NULL;
3019
3020     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3021         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3022         multi_user_name = NULL;
3023     }
3024
3025         err = emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
3026         if (err != EMAIL_ERROR_NONE) {
3027                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
3028                 goto FINISH_OFF;
3029         }
3030
3031         if (!emcore_get_mail_data(multi_user_name, mail_id, &mail_data)) {
3032                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
3033                 goto FINISH_OFF;
3034         }
3035
3036         if ((err = emstorage_get_attachment_list(multi_user_name, mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
3037                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed : [%d]", err);
3038                 goto FINISH_OFF;
3039         }
3040
3041         if (attachment_tbl_count <= 0) {
3042                 EM_DEBUG_EXCEPTION("Invalid signed mail");
3043                 err = EMAIL_ERROR_INVALID_MAIL;
3044                 goto FINISH_OFF;
3045         }
3046
3047         for (count = 0; count < attachment_tbl_count ; count++) {
3048                 if (attachment_tbl_list[count].attachment_mime_type && strcasestr(attachment_tbl_list[count].attachment_mime_type, "SIGNATURE"))
3049                         break;
3050         }
3051
3052         if (mail_data->smime_type == EMAIL_SMIME_SIGNED) {
3053                 if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
3054                         EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
3055                         goto FINISH_OFF;
3056                 }
3057         } else if (mail_data->smime_type == EMAIL_PGP_SIGNED) {
3058                 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) {
3059                         EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_signature failed : [%d]", err);
3060                         goto FINISH_OFF;
3061                 }
3062         } else {
3063                 EM_DEBUG_LOG("Invalid signed mail");
3064                 err = EMAIL_ERROR_INVALID_PARAM;
3065         }
3066
3067 FINISH_OFF:
3068
3069         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
3070                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
3071
3072         if (verify) {
3073                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
3074         }
3075
3076         if (!emipc_execute_stub_api(a_hAPI))
3077                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3078
3079
3080         if (attachment_tbl_list)
3081                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
3082
3083         if (mail_data) {
3084                 emcore_free_mail_data(mail_data);
3085                 EM_SAFE_FREE(mail_data);
3086         }
3087
3088     EM_SAFE_FREE(multi_user_name);
3089         EM_DEBUG_FUNC_END();
3090 }
3091
3092 void stb_verify_certificate(HIPC_API a_hAPI)
3093 {
3094         int err = EMAIL_ERROR_NONE;
3095         int verify = 0;
3096         int cert_file_len = 0;
3097         char *cert_file_path = 0;
3098     int nAPPID = emipc_get_app_id(a_hAPI);
3099     char *multi_user_name = NULL;
3100
3101     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3102         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3103         multi_user_name = NULL;
3104     }
3105
3106         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
3107         if (cert_file_len > 0) {
3108                 cert_file_path = em_malloc(cert_file_len + 1);
3109                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
3110         }
3111
3112         if (!emcore_verify_certificate(cert_file_path, &verify, &err)) {
3113                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
3114         }
3115
3116         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
3117                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
3118
3119         if (verify) {
3120                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
3121         }
3122
3123         if (!emipc_execute_stub_api(a_hAPI))
3124                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3125
3126         EM_SAFE_FREE(cert_file_path);           
3127     EM_SAFE_FREE(multi_user_name);
3128         EM_DEBUG_FUNC_END();    
3129 }
3130
3131 void stb_ping_service(HIPC_API a_hAPI)
3132 {
3133         EM_DEBUG_FUNC_BEGIN();
3134         int err = EMAIL_ERROR_NONE;
3135
3136         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3137                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3138
3139         if (!emipc_execute_stub_api(a_hAPI))
3140                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3141
3142         EM_DEBUG_FUNC_END();
3143 }
3144
3145 void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
3146 {
3147         EM_DEBUG_FUNC_BEGIN();
3148         int err = EMAIL_ERROR_NONE, account_id;
3149         int total_mail_count = 0, unread_mail_count = 0;
3150         int input_from_eas = 0;
3151     /* Get Container name */
3152     int nAPPID = emipc_get_app_id(a_hAPI);
3153     char *multi_user_name = NULL;
3154
3155     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3156         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3157         multi_user_name = NULL;
3158     }
3159
3160         /* account_id */
3161         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3162         EM_DEBUG_LOG("account_id [%d]", account_id);
3163
3164         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &total_mail_count);
3165         EM_DEBUG_LOG("total_mail_count [%d]", total_mail_count);
3166
3167         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &unread_mail_count);
3168         EM_DEBUG_LOG("unread_mail_count [%d]", unread_mail_count);
3169
3170         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &input_from_eas);
3171         EM_DEBUG_LOG("unread_mail_count [%d]", input_from_eas);
3172
3173         emcore_display_unread_in_badge(multi_user_name);
3174
3175         if(!emdaemon_finalize_sync(multi_user_name, account_id, total_mail_count, unread_mail_count, 0, 0, input_from_eas, &err)) {
3176                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
3177         }
3178
3179         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3180                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3181
3182         if (!emipc_execute_stub_api(a_hAPI))
3183                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3184
3185     EM_SAFE_FREE(multi_user_name);
3186         EM_DEBUG_FUNC_END();
3187 }
3188
3189 void stb_clear_notification_bar(HIPC_API a_hAPI)
3190 {
3191         EM_DEBUG_FUNC_BEGIN();
3192         int err = EMAIL_ERROR_NONE, account_id;
3193     int nAPPID = emipc_get_app_id(a_hAPI);
3194     char *multi_user_name = NULL;
3195
3196     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3197         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3198         multi_user_name = NULL;
3199     }
3200
3201         /* account_id */
3202         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3203         EM_DEBUG_LOG("account_id [%d]", account_id);
3204
3205         if((err = emcore_clear_notifications(multi_user_name, account_id)) != EMAIL_ERROR_NONE) 
3206                 EM_DEBUG_EXCEPTION("emdaemon_finalize_sync failed [%d]", err);
3207
3208         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3209                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3210
3211         if (!emipc_execute_stub_api(a_hAPI))
3212                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3213
3214     EM_SAFE_FREE(multi_user_name);
3215         EM_DEBUG_FUNC_END();
3216 }
3217
3218 void stb_show_user_message(HIPC_API a_hAPI)
3219 {
3220         EM_DEBUG_FUNC_BEGIN();
3221         int err = EMAIL_ERROR_NONE;
3222         int param_id = 0;
3223         int param_error = 0;
3224         email_action_t param_action = 0;
3225     int nAPPID = emipc_get_app_id(a_hAPI);
3226     char *multi_user_name = NULL;
3227
3228     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3229         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3230         multi_user_name = NULL;
3231     }
3232
3233         /* param_id */
3234         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &param_id);
3235         EM_DEBUG_LOG("param_id [%d]", param_id);
3236
3237         /* param_action */
3238         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &param_action);
3239         EM_DEBUG_LOG("param_action [%d]", param_action);
3240
3241         /* param_error */
3242         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &param_error);
3243         EM_DEBUG_LOG("param_error [%d]", param_error);
3244
3245         if(!emcore_show_user_message(multi_user_name, param_id, param_action, param_error)) {
3246                 EM_DEBUG_EXCEPTION("emcore_show_user_message failed");
3247         }
3248
3249         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3250                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3251
3252         if (!emipc_execute_stub_api(a_hAPI))
3253                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3254
3255     EM_SAFE_FREE(multi_user_name);
3256         EM_DEBUG_FUNC_END("err [%d]", err);
3257 }
3258
3259 void stb_write_mime_file(HIPC_API a_hAPI)
3260 {
3261         EM_DEBUG_FUNC_BEGIN();
3262         int err = EMAIL_ERROR_NONE;
3263
3264         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3265                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3266
3267         if (!emipc_execute_stub_api(a_hAPI))
3268                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3269
3270         EM_DEBUG_FUNC_END();
3271 }
3272
3273
3274 void stb_validate_account_ex(HIPC_API a_hAPI)
3275 {
3276         EM_DEBUG_FUNC_BEGIN();
3277         int buffer_size = 0;
3278         int local_result = 0;
3279         int handle = 0;
3280         char* stream = NULL;
3281         email_account_t *account = NULL;
3282         email_account_t *ref_account = NULL;
3283         int err = EMAIL_ERROR_NONE;
3284     int nAPPID = emipc_get_app_id(a_hAPI);
3285     char *multi_user_name = NULL;
3286
3287     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3288         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3289         multi_user_name = NULL;
3290     }
3291
3292         /* get account info */
3293         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
3294         EM_DEBUG_LOG("size [%d]", buffer_size);
3295         if(buffer_size <= 0) {
3296                 err = EMAIL_ERROR_INVALID_PARAM;
3297                 goto FINISH_OFF;
3298         }
3299
3300         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
3301         if(!stream) {
3302                 err = EMAIL_ERROR_INVALID_PARAM;
3303                 goto FINISH_OFF;
3304         }
3305
3306         account = em_malloc(sizeof(email_account_t));
3307
3308         if(account == NULL) {
3309                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
3310                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3311                 goto FINISH_OFF;
3312         }
3313
3314         em_convert_byte_stream_to_account(stream, buffer_size, account);
3315     account->user_name = EM_SAFE_STRDUP(multi_user_name);
3316
3317         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
3318                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
3319                 goto FINISH_OFF;
3320         }
3321
3322         ref_account = emcore_get_account_reference(multi_user_name, account->account_id, false);
3323
3324         if (!ref_account) {
3325                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
3326                 goto FINISH_OFF;
3327         }
3328
3329         /* ref_account will be removed by worker_event_queue() */
3330         if((err = emdaemon_validate_account_ex(multi_user_name, ref_account, &handle)) != EMAIL_ERROR_NONE) {
3331                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_ex fail [%d]", err);
3332                 goto FINISH_OFF;
3333         }
3334
3335         local_result = 1;
3336         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
3337                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3338         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
3339                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
3340         if (!emipc_execute_stub_api(a_hAPI))
3341                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3342
3343 FINISH_OFF:
3344         if ( local_result == 0 ) { /* there is an error */
3345                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
3346                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
3347                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3348                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
3349                 if (!emipc_execute_stub_api(a_hAPI))
3350                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
3351         }
3352
3353     EM_SAFE_FREE(multi_user_name);
3354         EM_DEBUG_FUNC_END();
3355 }
3356
3357 void stb_handle_task(int task_type, HIPC_API a_hAPI)
3358 {
3359         EM_DEBUG_FUNC_BEGIN();
3360         int param_index = 0;
3361         int is_async_task = (task_type > EMAIL_ASYNC_TASK_BOUNDARY_START)?1:0;
3362         int err = EMAIL_ERROR_NONE;
3363         int task_id = 0;
3364         int task_parameter_length = 0;
3365         char *task_parameter = NULL;
3366
3367         /* task_parameter_length */;
3368         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3369         EM_DEBUG_LOG("task_parameter_length [%d]", task_parameter_length);
3370
3371         /* task_parameter */
3372         if(task_parameter_length > 0)    {
3373                 task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3374         }
3375
3376         if(is_async_task) {
3377                 /* add async task */
3378                 if((err = emcore_add_task_to_task_table(NULL, task_type, EMAIL_TASK_PRIORITY_MID, task_parameter, task_parameter_length, &task_id)) != EMAIL_ERROR_NONE) {
3379                         EM_DEBUG_EXCEPTION("emcore_add_task_to_task_pool failed [%d]", err);
3380                         goto FINISH_OFF;
3381                 }
3382         }
3383         else {
3384                 /* do sync task */
3385                 email_task_t sync_task;
3386
3387                 memset(&sync_task, 0, sizeof(email_task_t));
3388                 sync_task.task_type             = task_type;
3389                 sync_task.task_priority         = EMAIL_TASK_PRIORITY_MID;
3390                 sync_task.task_parameter        = task_parameter;
3391                 sync_task.task_parameter_length = task_parameter_length;
3392
3393                 if((err = (int)emcore_default_sync_task_handler(&sync_task)) != EMAIL_ERROR_NONE) {
3394                         EM_DEBUG_EXCEPTION("emcore_default_sync_task_handler failed [%d]", err);
3395                         goto FINISH_OFF;
3396                 }
3397         }
3398
3399 FINISH_OFF:
3400         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3401                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3402
3403         if(is_async_task) {
3404                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3405                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3406         }
3407
3408         if (!emipc_execute_stub_api(a_hAPI))
3409                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3410
3411         em_flush_memory();
3412         EM_DEBUG_FUNC_END("err [%d]", err);
3413 }
3414
3415 void* thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void *input_param)
3416 {
3417         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3418         int err = EMAIL_ERROR_NONE;
3419         int err_for_signal = EMAIL_ERROR_NONE;
3420         int i = 0;
3421         int task_id = THREAD_SELF();
3422         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *task_param = input_param;
3423
3424         /* Send start signal */
3425         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)
3426                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3427
3428         for(i = 0; i < task_param->mail_id_count; i++) {
3429                 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) {
3430                         EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);
3431                         goto FINISH_OFF;
3432                 }
3433
3434                 /* Send progress signal */
3435                 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)
3436                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3437         }
3438
3439 FINISH_OFF:
3440         /* Send finish signal */
3441         if(err == EMAIL_ERROR_NONE) {
3442                 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)
3443                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3444         }
3445         else {
3446                 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)
3447                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3448         }
3449
3450         /* Free task parameter */
3451         EM_SAFE_FREE(task_param->multi_user_name);
3452         EM_SAFE_FREE(task_param->mail_id_array);
3453         EM_SAFE_FREE(task_param);
3454
3455         emcore_close_recv_stream_list();
3456         EM_DEBUG_FUNC_END("err [%d]", err);
3457         return SUCCESS;
3458 }
3459
3460 void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
3461 {
3462         EM_DEBUG_FUNC_BEGIN();
3463         int param_index = 0;
3464         int err = EMAIL_ERROR_NONE;
3465         int task_parameter_length = 0;
3466         int thread_error;
3467         thread_t task_id;
3468         char *task_parameter = NULL;
3469         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *decoded_parameter = NULL;
3470
3471     int nAPPID = emipc_get_app_id(a_hAPI);
3472     char *multi_user_name = NULL;
3473
3474     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3475             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3476             multi_user_name = NULL;
3477     }
3478
3479         /* task_parameter_length */;
3480         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3481
3482         if(task_parameter_length <= 0) {
3483                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3484                 err = EMAIL_ERROR_INVALID_PARAM;
3485                 goto FINISH_OFF;
3486         }
3487
3488         /* task_parameter */
3489         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3490
3491         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) {
3492                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3493                 goto FINISH_OFF;
3494         }
3495
3496     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3497
3498         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, (void*)decoded_parameter, thread_error);
3499
3500         if(thread_error != 0) {
3501                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3502                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3503                 goto FINISH_OFF;
3504         }
3505
3506         THREAD_DETACH(task_id);
3507
3508 FINISH_OFF:
3509         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3510                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3511
3512         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3513                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3514
3515         if (!emipc_execute_stub_api(a_hAPI))
3516                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3517
3518     EM_SAFE_FREE(multi_user_name);
3519
3520         EM_DEBUG_FUNC_END("err [%d]", err);
3521 }
3522
3523 void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
3524 {
3525         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3526         int err = EMAIL_ERROR_NONE;
3527         int err_for_signal = EMAIL_ERROR_NONE;
3528         int recursive = 1;
3529         int task_id = THREAD_SELF();
3530         email_account_t* ref_account = NULL;
3531         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *task_param = input_param;
3532
3533         ref_account = emcore_get_account_reference(task_param->multi_user_name, task_param->account_id, false);
3534         if (!ref_account)  {
3535                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", task_param->account_id);
3536                 err = EMAIL_ERROR_INVALID_ACCOUNT;
3537                 goto FINISH_OFF;
3538         }
3539
3540         /*  on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
3541         if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
3542                 recursive = 0;
3543         }
3544
3545         /* Send start signal */
3546         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)
3547                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3548
3549         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) {
3550                 EM_DEBUG_EXCEPTION("emcore_delete_mailbox_ex failed[%d]", err);
3551                 goto FINISH_OFF;
3552         }
3553
3554 FINISH_OFF:
3555         /* Send finish signal */
3556         if(err == EMAIL_ERROR_NONE) {
3557                 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)
3558                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3559         }
3560         else {
3561                 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)
3562                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3563         }
3564
3565         if (ref_account) { /*prevent 49435*/
3566                 emcore_free_account(ref_account);
3567                 EM_SAFE_FREE(ref_account);
3568         }
3569
3570         /* Free task parameter */
3571         EM_SAFE_FREE(task_param->mailbox_id_array);
3572     EM_SAFE_FREE(task_param->multi_user_name);
3573         EM_SAFE_FREE(task_param);
3574
3575         EM_DEBUG_FUNC_END("err [%d]", err);
3576         return SUCCESS;
3577 }
3578
3579 void stb_delete_mailbox_ex(HIPC_API a_hAPI)
3580 {
3581         EM_DEBUG_FUNC_BEGIN();
3582         int param_index = 0;
3583         int err = EMAIL_ERROR_NONE;
3584         int task_parameter_length = 0;
3585         int thread_error = 0;
3586         thread_t task_id;
3587         char *task_parameter = NULL;
3588         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *decoded_parameter = NULL;
3589     int nAPPID = emipc_get_app_id(a_hAPI);
3590     char *multi_user_name = NULL;
3591
3592     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3593         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3594         multi_user_name = NULL;
3595     }
3596
3597         /* task_parameter_length */;
3598         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3599
3600         if(task_parameter_length <= 0) {
3601                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3602                 err = EMAIL_ERROR_INVALID_PARAM;
3603                 goto FINISH_OFF;
3604         }
3605
3606         /* task_parameter */
3607         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3608
3609         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
3610                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3611                 goto FINISH_OFF;
3612         }
3613
3614     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3615
3616         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, (void*)decoded_parameter, thread_error);
3617
3618         if(thread_error != 0) {
3619                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3620                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3621                 goto FINISH_OFF;
3622         }
3623
3624         THREAD_DETACH(task_id);
3625
3626 FINISH_OFF:
3627         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3628                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3629
3630         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3631                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3632
3633         if (!emipc_execute_stub_api(a_hAPI))
3634                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3635
3636     EM_SAFE_FREE(multi_user_name);
3637         EM_DEBUG_FUNC_END("err [%d]", err);
3638 }
3639
3640 void* thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(void *input_param)
3641 {
3642         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
3643         int err = EMAIL_ERROR_NONE;
3644         int err_for_signal = EMAIL_ERROR_NONE;
3645         int task_id = THREAD_SELF();
3646         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *task_param = input_param;
3647
3648         /* Send start signal */
3649         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)
3650                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3651
3652         if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->multi_user_name, task_param->mail_id)) != EMAIL_ERROR_NONE) {
3653                 EM_DEBUG_EXCEPTION("emcore_send_mail_with_downloading_attachment_of_original_mail failed [%d]", err);
3654                 goto FINISH_OFF;
3655         }
3656
3657 FINISH_OFF:
3658         /* Send finish signal */
3659         if(err == EMAIL_ERROR_NONE) {
3660                 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)
3661                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3662         }
3663         else {
3664                 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)
3665                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
3666         }
3667
3668         /* Free task parameter */
3669     EM_SAFE_FREE(task_param->multi_user_name);
3670         EM_SAFE_FREE(task_param);
3671         emcore_close_smtp_stream_list();
3672         EM_DEBUG_FUNC_END("err [%d]", err);
3673         return SUCCESS;
3674 }
3675
3676 void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
3677 {
3678         EM_DEBUG_FUNC_BEGIN();
3679         int param_index = 0;
3680         int err = EMAIL_ERROR_NONE;
3681         int task_parameter_length = 0;
3682         int thread_error = 0;
3683         thread_t task_id;
3684         char *task_parameter = NULL;
3685         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *decoded_parameter = NULL;
3686     int nAPPID = emipc_get_app_id(a_hAPI);
3687     char *multi_user_name = NULL;
3688
3689     if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3690         EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3691         multi_user_name = NULL;
3692     }
3693
3694         /* task_parameter_length */;
3695         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
3696
3697         if(task_parameter_length <= 0) {
3698                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
3699                 err = EMAIL_ERROR_INVALID_PARAM;
3700                 goto FINISH_OFF;
3701         }
3702
3703         /* task_parameter */
3704         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
3705
3706         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) {
3707                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
3708                 goto FINISH_OFF;
3709         }
3710
3711     decoded_parameter->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
3712
3713         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, (void*)decoded_parameter, thread_error);
3714
3715         if(thread_error != 0) {
3716                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
3717                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3718                 goto FINISH_OFF;
3719         }
3720
3721         THREAD_DETACH(task_id);
3722
3723 FINISH_OFF:
3724         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3725                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3726
3727         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
3728                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3729
3730         if (!emipc_execute_stub_api(a_hAPI))
3731                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3732
3733     EM_SAFE_FREE(multi_user_name);
3734         EM_DEBUG_FUNC_END("err [%d]", err);
3735 }
3736
3737 void stb_save_default_account_id(HIPC_API a_hAPI)
3738 {
3739         EM_DEBUG_FUNC_BEGIN();
3740         int err = EMAIL_ERROR_NONE;
3741         int account_id = 0;
3742
3743     int nAPPID = emipc_get_app_id(a_hAPI);
3744         char *multi_user_name = NULL;
3745
3746         if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3747         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3748         multi_user_name = NULL;
3749         }
3750
3751         /* account_id */
3752         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
3753         EM_DEBUG_LOG("account_id [%d]", account_id);
3754
3755         err = emcore_save_default_account_id(multi_user_name, account_id);
3756         if (err != EMAIL_ERROR_NONE) EM_DEBUG_EXCEPTION("emcore_save_default_account_id failed [%d]", err);
3757
3758         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3759                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3760
3761         if (!emipc_execute_stub_api(a_hAPI))
3762                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3763
3764         EM_SAFE_FREE(multi_user_name);
3765         EM_DEBUG_FUNC_END();
3766 }
3767
3768 void stb_load_default_account_id(HIPC_API a_hAPI)
3769 {
3770         EM_DEBUG_FUNC_BEGIN();
3771         int err = EMAIL_ERROR_NONE;
3772         int account_id = 0;
3773
3774     int nAPPID = emipc_get_app_id(a_hAPI);
3775         char *multi_user_name = NULL;
3776
3777         if ((err = emcore_get_user_name(nAPPID, &multi_user_name)) != EMAIL_ERROR_NONE) {
3778         EM_DEBUG_EXCEPTION("emcore_get_user_name failed : [%d]", err);
3779         multi_user_name = NULL;
3780         }
3781
3782         err = emcore_load_default_account_id(multi_user_name, &account_id);
3783         if (err != EMAIL_ERROR_NONE) { 
3784                 EM_DEBUG_EXCEPTION("emcore_save_default_account_id failed [%d]", err);
3785         }
3786
3787         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3788                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3789
3790         if (err == EMAIL_ERROR_NONE) {
3791                 if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &account_id, sizeof(int))) 
3792                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3793         }
3794
3795         if (!emipc_execute_stub_api(a_hAPI))
3796                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3797
3798         EM_SAFE_FREE(multi_user_name);
3799         EM_DEBUG_FUNC_END();
3800 }
3801
3802 void stb_get_user_name(HIPC_API a_hAPI)
3803 {
3804     EM_DEBUG_FUNC_BEGIN();
3805
3806     int err = EMAIL_ERROR_NONE;
3807     int pid = 0;
3808
3809     char *user_name = NULL;
3810
3811     pid = emipc_get_app_id(a_hAPI);
3812     EM_DEBUG_LOG("Peer PID : [%d]", pid);
3813
3814     if (pid > 0) {
3815         if ((err = emcore_get_user_name(pid, &user_name)) != EMAIL_ERROR_NONE) 
3816             EM_DEBUG_EXCEPTION("emcore_get_user_info failed : [%d]", err);
3817
3818         EM_DEBUG_LOG("Domain name : [%s]", user_name);
3819
3820         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) 
3821                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3822
3823                 if (user_name) {
3824                         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, user_name, EM_SAFE_STRLEN(user_name) + 1)) 
3825                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
3826                 }
3827     }
3828
3829     if (!emipc_execute_stub_api(a_hAPI))
3830         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3831
3832     EM_SAFE_FREE(user_name);
3833
3834     EM_DEBUG_FUNC_END();
3835 }
3836
3837 void stb_API_mapper(HIPC_API a_hAPI)
3838 {
3839         EM_DEBUG_FUNC_BEGIN();
3840         int err = EMAIL_ERROR_NONE;
3841         int nAPIID = emipc_get_api_id(a_hAPI);
3842         int client_fd = emipc_get_response_id(a_hAPI);
3843
3844         err = emcore_check_privilege(client_fd);
3845         if (err != EMAIL_ERROR_NONE) {
3846                 EM_DEBUG_EXCEPTION("emcore_check_privilege failed : [%d]", err);
3847
3848                 if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
3849                         EM_DEBUG_EXCEPTION("emipc_add_paramter failed");
3850
3851                 if (!emipc_execute_stub_api(a_hAPI))
3852                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
3853
3854                 return;
3855         }
3856
3857         switch(nAPIID) {
3858                 case _EMAIL_API_ADD_ACCOUNT:
3859                         stb_create_account(a_hAPI);
3860                         break;
3861
3862                 case _EMAIL_API_ADD_MAILBOX:
3863                         stb_create_mailbox(a_hAPI);
3864                         break;
3865
3866                 case _EMAIL_API_DELETE_MAILBOX:
3867                         stb_delete_mailbox(a_hAPI);
3868                         break;
3869
3870                 case _EMAIL_API_SET_MAILBOX_TYPE:
3871                         stb_set_mailbox_type(a_hAPI);
3872                         break;
3873
3874                 case _EMAIL_API_SET_LOCAL_MAILBOX:
3875                         stb_set_local_mailbox(a_hAPI);
3876                         break;
3877
3878                 case _EMAIL_API_STAMP_SYNC_TIME_OF_MAILBOX:
3879                         stb_stamp_sync_time_of_mailbox(a_hAPI);
3880                         break;
3881
3882                 case _EMAIL_API_SET_MAIL_SLOT_SIZE:
3883                         stb_set_mail_slot_size_of_mailbox(a_hAPI);
3884                         break;
3885
3886                 case _EMAIL_API_RENAME_MAILBOX:
3887                         stb_rename_mailbox(a_hAPI);
3888                         break;
3889
3890                 case _EMAIL_API_RENAME_MAILBOX_EX:
3891                         stb_rename_mailbox_ex(a_hAPI);
3892                         break;
3893
3894                 case _EMAIL_API_SEND_MAIL:
3895                         stb_send_mail(a_hAPI);
3896                         break;
3897
3898                 case _EMAIL_API_QUERY_SMTP_MAIL_SIZE_LIMIT:
3899                         stb_query_smtp_mail_size_limit(a_hAPI);
3900                         break;
3901
3902                 case _EMAIL_API_GET_MAILBOX_LIST:
3903                         stb_get_mailbox_list(a_hAPI);
3904                         break;
3905
3906                 case _EMAIL_API_SYNC_HEADER:
3907                         stb_sync_header(a_hAPI);
3908                         break;
3909
3910                 case _EMAIL_API_DOWNLOAD_BODY:
3911                         stb_download_body(a_hAPI);
3912                         break;
3913
3914                 case _EMAIL_API_CLEAR_DATA:
3915                         stb_clear_mail_data (a_hAPI);
3916                         break;
3917
3918                 case _EMAIL_API_DELETE_ALL_MAIL:
3919                         stb_delete_all_mails(a_hAPI);
3920                         break;
3921
3922                 case _EMAIL_API_DELETE_MAIL:
3923                         stb_delete_mail(a_hAPI);
3924                         break;
3925
3926                 case _EMAIL_API_ADD_RULE:
3927                         stb_add_rule(a_hAPI);
3928                         break;
3929
3930                 case _EMAIL_API_GET_RULE:
3931                         stb_get_rule(a_hAPI);
3932                         break;
3933
3934                 case _EMAIL_API_GET_RULE_LIST:
3935                         stb_get_rule_list(a_hAPI);
3936                         break;
3937
3938                 case _EMAIL_API_FIND_RULE:
3939                         stb_find_rule(a_hAPI);
3940                         break;
3941
3942                 case _EMAIL_API_UPDATE_RULE:
3943                         stb_update_rule(a_hAPI);
3944                         break;
3945
3946                 case _EMAIL_API_DELETE_RULE:
3947                         stb_delete_rule(a_hAPI);
3948                         break;
3949
3950                 case _EMAIL_API_APPLY_RULE:
3951                         stb_apply_rule(a_hAPI);
3952                         break;
3953
3954                 case _EMAIL_API_MOVE_MAIL:
3955                         stb_move_mail(a_hAPI);
3956                         break;
3957
3958                 case _EMAIL_API_MOVE_ALL_MAIL:
3959                         stb_move_all_mails(a_hAPI);
3960                         break;
3961
3962                 case _EMAIL_API_SET_FLAGS_FIELD:
3963                         stb_set_flags_field(a_hAPI);
3964                         break;
3965
3966                 case _EMAIL_API_ADD_MAIL:
3967                         stb_add_mail(a_hAPI);
3968                         break;
3969
3970                 case _EMAIL_API_UPDATE_MAIL:
3971                         stb_update_mail(a_hAPI);
3972                         break;
3973
3974                 case _EMAIL_API_MOVE_THREAD_TO_MAILBOX:
3975                         stb_move_thread_to_mailbox(a_hAPI);
3976                         break;
3977
3978                 case _EMAIL_API_DELETE_THREAD:
3979                         stb_delete_thread(a_hAPI);
3980                         break;
3981
3982                 case _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD:
3983                         stb_modify_seen_flag_of_thread(a_hAPI);
3984                         break;
3985
3986                 case _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED:
3987                         stb_expunge_mails_deleted_flagged(a_hAPI);
3988                         break;
3989
3990                 case _EMAIL_API_DELETE_ACCOUNT:
3991                         stb_delete_account(a_hAPI);
3992                         break;
3993
3994                 case _EMAIL_API_UPDATE_ACCOUNT:
3995                         stb_update_account(a_hAPI);
3996                         break;
3997
3998                 case _EMAIL_API_ADD_ATTACHMENT:
3999                         stb_add_attachment(a_hAPI);
4000                         break;
4001
4002                 case _EMAIL_API_GET_IMAP_MAILBOX_LIST:
4003                         stb_get_imap_mailbox_list(a_hAPI);
4004                         break;
4005
4006                 case _EMAIL_API_GET_ATTACHMENT:
4007                         stb_get_attachment(a_hAPI);
4008                         break;
4009
4010                 case _EMAIL_API_DELETE_ATTACHMENT:
4011                         stb_delete_attachment(a_hAPI);
4012                         break;
4013
4014                 case _EMAIL_API_DOWNLOAD_ATTACHMENT:
4015                         stb_download_attachment(a_hAPI);
4016                         break;
4017
4018                 case _EMAIL_API_GET_ACCOUNT_LIST:
4019                         stb_get_account_list(a_hAPI);
4020                         break;
4021
4022                 case _EMAIL_API_SEND_SAVED:
4023                         stb_mail_send_saved(a_hAPI);
4024                         break;
4025
4026                 case _EMAIL_API_ADD_READ_RECEIPT:
4027                         stb_add_read_receipt(a_hAPI);
4028                         break;
4029
4030                 case _EMAIL_API_CANCEL_JOB:
4031                         stb_cancel_job(a_hAPI);
4032                         break;
4033
4034                 case _EMAIL_API_SEND_RETRY:
4035                         stb_retry_sending_mail(a_hAPI);
4036                         break;
4037
4038                 case _EMAIL_API_VALIDATE_ACCOUNT :
4039                         stb_validate_account(a_hAPI);
4040                         break;
4041
4042                 case _EMAIL_API_SEND_MAIL_CANCEL_JOB :
4043                         stb_cancel_send_mail_job(a_hAPI);
4044                         break;
4045
4046                 case _EMAIL_API_SEARCH_MAIL_ON_SERVER :
4047                         stb_search_mail_on_server(a_hAPI);
4048                         break;
4049
4050                 case _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER :
4051                         stb_clear_result_of_search_mail_on_server(a_hAPI);
4052                         break;
4053
4054                 case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :
4055                         stb_add_account_with_validation(a_hAPI);
4056                         break;
4057
4058                 case _EMAIL_API_BACKUP_ACCOUNTS:
4059                         stb_backup_account(a_hAPI);
4060                         break;
4061
4062                 case _EMAIL_API_RESTORE_ACCOUNTS:
4063                         stb_restore_account(a_hAPI);
4064                         break;
4065
4066                 case _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT:
4067                         stb_get_password_length(a_hAPI);
4068                         break;
4069
4070                 case _EMAIL_API_GET_TASK_INFORMATION:
4071                         stb_get_task_information(a_hAPI);
4072                         break;
4073
4074                 case _EMAIL_API_ADD_CERTIFICATE:
4075                         stb_add_certificate(a_hAPI);
4076                         break;
4077
4078                 case _EMAIL_API_DELETE_CERTIFICATE:
4079                         stb_delete_certificate(a_hAPI);
4080                         break;
4081
4082                 case _EMAIL_API_VERIFY_SIGNATURE:
4083                         stb_verify_signature(a_hAPI);
4084                         break;
4085
4086                 case _EMAIL_API_VERIFY_CERTIFICATE:
4087                         stb_verify_certificate(a_hAPI);
4088                         break;
4089
4090                 case _EMAIL_API_PING_SERVICE :
4091                         stb_ping_service(a_hAPI);
4092                         break;
4093
4094                 case _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL :
4095                         stb_update_notification_bar_for_unread_mail(a_hAPI);
4096                         break;
4097
4098                 case _EMAIL_API_CLEAR_NOTIFICATION_BAR :
4099                         stb_clear_notification_bar(a_hAPI);
4100                         break;
4101
4102                 case _EMAIL_API_SHOW_USER_MESSAGE :
4103                         stb_show_user_message(a_hAPI);
4104                         break;
4105
4106                 case _EMAIL_API_WRITE_MIME_FILE :
4107                         stb_write_mime_file(a_hAPI);
4108                         break;
4109
4110                 case _EMAIL_API_VALIDATE_ACCOUNT_EX :
4111                         stb_validate_account_ex(a_hAPI);
4112                         break;
4113
4114                 case EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT :
4115                         stb_move_mails_to_mailbox_of_another_account(a_hAPI);
4116                         break;
4117
4118                 case EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX :
4119                         stb_delete_mailbox_ex(a_hAPI);
4120                         break;
4121
4122                 case EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL :
4123                         stb_send_mail_with_downloading_attachment_of_original_mail(a_hAPI);
4124                         break;
4125
4126         case _EMAIL_API_GET_USER_NAME :
4127             stb_get_user_name(a_hAPI);
4128             break;
4129
4130                 case _EMAIL_API_SAVE_DEFAULT_ACCOUNT_ID :
4131                         stb_save_default_account_id(a_hAPI);
4132                         break;
4133
4134                 case _EMAIL_API_LOAD_DEFAULT_ACCOUNT_ID :
4135                         stb_load_default_account_id(a_hAPI);
4136                         break;
4137
4138                 default :
4139                         if(EMAIL_SYNC_TASK_BOUNDARY_START < nAPIID && nAPIID < EMAIL_ASYNC_TASK_BOUNDARY_END)
4140                                 stb_handle_task(nAPIID, a_hAPI);
4141                         break;
4142         }
4143         EM_DEBUG_FUNC_END();
4144 }
4145
4146 GMainLoop *g_mainloop = NULL;
4147
4148 /* this func should be called in main_loop */
4149 INTERNAL_FUNC int kill_daemon_if_no_account()
4150 {
4151         EM_DEBUG_FUNC_BEGIN();
4152         int err = EMAIL_ERROR_NONE;
4153         email_account_t *account_list = NULL;
4154         int account_count = 0;
4155     int total_account_count = 0;
4156     GList *node = NULL;
4157     GList *zone_name_list = NULL;
4158
4159     /* each container check the account */
4160     if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
4161         EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
4162                 if (zone_name_list) g_list_free(zone_name_list);
4163                 goto FINISH_OFF;
4164     }
4165
4166     if (g_list_length(zone_name_list) == 1) {
4167         if ((err = emcore_get_account_reference_list(NULL, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
4168             EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
4169         }
4170
4171         if (account_list)
4172             emcore_free_account_list(&account_list, account_count, NULL);
4173
4174         total_account_count += account_count;
4175     } else {
4176         node = g_list_first(zone_name_list);
4177         while (node != NULL) {
4178             if (!node->data) {
4179                 node = g_list_next(node);
4180                                 continue;
4181                         }
4182
4183             if ((err = emcore_get_account_reference_list(node->data, &account_list, &account_count)) != EMAIL_ERROR_NONE) {
4184                 EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err);
4185             }
4186
4187             if (account_list)
4188                 emcore_free_account_list(&account_list, account_count, NULL);
4189
4190             total_account_count += account_count;
4191             EM_SAFE_FREE(node->data);
4192             node = g_list_next(node);
4193         }
4194     }
4195
4196         if (zone_name_list)
4197                 g_list_free(zone_name_list);
4198
4199         EM_DEBUG_LOG("account_count [%d]", total_account_count);
4200
4201         if (total_account_count == 0) {
4202                 EM_DEBUG_LOG("email-service is going to shutdown");
4203                 g_main_loop_quit (g_mainloop);
4204         }
4205
4206 FINISH_OFF:
4207
4208         EM_DEBUG_FUNC_END("err [%d]", err);
4209         return err;
4210 }
4211
4212 static void callback_for_sigterm(int signum)
4213 {
4214         EM_DEBUG_FUNC_BEGIN("signum [%d]", signum);
4215
4216         if (g_mainloop)
4217                 g_main_loop_quit(g_mainloop);
4218
4219         EM_DEBUG_FUNC_END();
4220 }
4221
4222 extern int launch_pid;
4223
4224 gboolean callback_for_shutdown(gpointer user_data)
4225 {
4226         EM_DEBUG_FUNC_BEGIN("user_data[%p]", user_data);
4227
4228         if (launch_pid == 0) {
4229                 kill_daemon_if_no_account();
4230         }
4231
4232         EM_DEBUG_FUNC_END();
4233         return FALSE;
4234 }
4235
4236 gboolean callback_for_del_account (GIOChannel *ch, GIOCondition cond, gpointer data)
4237 {
4238         static int file_del = 0;
4239         static int db_del = 0;
4240         int event = 0;
4241         gsize len = 0;
4242         g_io_channel_read_chars (ch, (gchar*) &event, sizeof (event), &len, NULL);
4243
4244         if (event==EMAIL_SIGNAL_DB_DELETED) {
4245                 db_del = 1;
4246         }
4247         else if (event==EMAIL_SIGNAL_FILE_DELETED) {
4248                 file_del = 1;
4249         }
4250         EM_DEBUG_LOG ("callback_for_del_account called file_del[%d] db_del[%d]", file_del, db_del);
4251
4252         /* if called twice, process termination begins.
4253            two threads should complete the delete task */
4254         if (file_del && db_del) {
4255                 kill_daemon_if_no_account();
4256                 file_del = db_del = 0; /* if there is an account, reset status */
4257         }
4258         return TRUE;
4259 }
4260
4261 /* gdbus variables */
4262 static GDBusNodeInfo *introspection_data = NULL;
4263 extern gchar introspection_xml[];
4264
4265 static void
4266 handle_method_call (GDBusConnection         *connection,
4267                       const gchar           *sender,
4268                       const gchar           *object_path,
4269                       const gchar           *interface_name,
4270                       const gchar           *method_name,
4271                       GVariant              *parameters,
4272                       GDBusMethodInvocation *invocation,
4273                       gpointer               user_data)
4274 {
4275         /* called by emipc_launch_email_service */
4276         if (g_strcmp0 (method_name, "Launch") == 0) {
4277                 int caller_pid = 0;
4278                 g_variant_get (parameters, "(i)", &caller_pid);
4279
4280                 launch_pid = caller_pid;
4281                 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(i)", EMAIL_ERROR_NONE));
4282                 EM_DEBUG_LOG ("email-service launched by pid [%d]", caller_pid);
4283         }
4284 /*
4285         else if (g_strcmp0 (method_name, "SetContactsLog") == 0) {
4286                 GVariant* ret = em_gdbus_set_contact_log(parameters);
4287                 g_dbus_method_invocation_return_value (invocation, ret);
4288         }
4289         else if (g_strcmp0 (method_name, "DeleteContactsLog") == 0) {
4290                 GVariant* ret = em_gdbus_delete_contact_log(parameters);
4291                 g_dbus_method_invocation_return_value (invocation, ret);
4292         }
4293 */
4294         else if (g_strcmp0 (method_name, "GetDisplayName") == 0) {
4295                 GVariant* ret = em_gdbus_get_display_name(parameters);
4296                 g_dbus_method_invocation_return_value (invocation, ret);
4297         }
4298         else if (g_strcmp0 (method_name, "CheckBlockingMode") == 0) {
4299                 GVariant* ret = em_gdbus_check_blocking_mode(parameters);
4300                 g_dbus_method_invocation_return_value (invocation, ret);
4301         }
4302 }
4303
4304 static const
4305 GDBusInterfaceVTable interface_vtable =
4306 {
4307         handle_method_call,
4308         NULL,
4309         NULL
4310 };
4311
4312 static void
4313 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
4314 {
4315         EM_DEBUG_LOG ("on_bus_acquired begin");
4316
4317         guint reg_id;
4318         GError *error = NULL;
4319
4320         reg_id = g_dbus_connection_register_object (connection,
4321                                              EMAIL_SERVICE_PATH,
4322                                              introspection_data->interfaces[0],
4323                                              &interface_vtable,
4324                                              NULL,  /* user_data */
4325                                              NULL,  /* user_data_free_func */
4326                                              &error); /* GError** */
4327         if (reg_id == 0) {
4328                 EM_DEBUG_EXCEPTION ("g_dbus_connection_register_object error[%s]",error->message);
4329                 g_error_free (error);
4330         }
4331         EM_DEBUG_LOG ("on_bus_acquired end [%d]", reg_id);
4332 }
4333
4334 static void on_name_acquired (GDBusConnection *connection,
4335                                const gchar *name,
4336                                gpointer     user_data)
4337 {
4338         EM_DEBUG_LOG ("on_name_acquired [%s]", name);
4339 }
4340
4341 static void on_name_lost (GDBusConnection *connection,
4342                            const gchar *name,
4343                            gpointer     user_data)
4344 {
4345         EM_DEBUG_EXCEPTION ("on_name_lost [%p] [%s]", connection, name);
4346 }
4347
4348 INTERNAL_FUNC int main(int argc, char *argv[])
4349 {
4350         /* Do the email-service Initialization
4351        1. Create all DB tables and load the email engine */
4352         EM_DEBUG_LOG("Email service begin");
4353         int err = 0, ret;
4354         GMainLoop *mainloop = NULL;
4355         int owner_id = 0;
4356     GList *node = NULL;
4357     GList *zone_name_list = NULL;
4358
4359 #if !GLIB_CHECK_VERSION(2, 36, 0) 
4360         g_type_init(); 
4361 #endif
4362
4363     /* Init container for daemon */
4364     emcore_create_container();
4365
4366         EM_DEBUG_LOG("ipcEmailStub_Initialize Start");
4367
4368         ret = emipc_initialize_stub(stb_API_mapper);
4369
4370         if(ret == true)
4371                 EM_DEBUG_LOG("ipcEmailStub_Initialize Success");
4372         else
4373                 EM_DEBUG_EXCEPTION("ipcEmailStub_Initialize failed");
4374
4375         signal(SIGPIPE, SIG_IGN);              /* to ignore signal 13(SIGPIPE) */
4376         signal(SIGTERM, callback_for_sigterm); /* to handle signal 15(SIGTERM) - power off case*/
4377
4378         emcore_gmime_init();
4379
4380     /* each container initialize */
4381     if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) {
4382         EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err);
4383                 if (zone_name_list) g_list_free(zone_name_list);
4384                 goto FINISH_OFF;
4385     }
4386
4387     if (g_list_length(zone_name_list) <= 1) {
4388         emdaemon_initialize(NULL, &err);
4389         emcore_connect_contacts_service(NULL);
4390     } else {
4391                 node = g_list_first(zone_name_list);
4392         while (node != NULL) {
4393             if (!node->data)
4394                 node = g_list_next(node);
4395
4396                         emdaemon_initialize(node->data, &err);
4397                         emcore_connect_contacts_service(node->data);
4398
4399             EM_SAFE_FREE(node->data);
4400             node = g_list_next(node);
4401         }
4402     }
4403
4404         if (zone_name_list)
4405                 g_list_free(zone_name_list);
4406
4407         g_timeout_add(5000, callback_for_shutdown, NULL);
4408
4409         /* pipe between main and del account thread */
4410         int *pipefd = emcore_init_pipe_for_del_account ();
4411         /* main loop uses IO channel for listening an event */
4412         if (pipefd) {
4413                 GIOChannel *ch = g_io_channel_unix_new (pipefd[0]);
4414                 EM_DEBUG_LOG ("main pipe[%d][%d]", pipefd[0], pipefd[1]);
4415                 /* non-blocking mode */
4416                 g_io_channel_set_flags (ch, g_io_channel_get_flags (ch) | G_IO_FLAG_NONBLOCK, NULL);
4417                 /* main loop watches the IO, and call the cb when data is ready */
4418                 g_io_add_watch (ch, G_IO_IN, &callback_for_del_account, NULL);
4419         }
4420
4421         /* gdbus setup */
4422         GError *error = NULL;
4423         introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
4424         if (!introspection_data) {
4425                 /* introspection_xml may be invalid */
4426                 EM_DEBUG_EXCEPTION ("g_dbus_node_info_new_for_xml error [%s]", error->message);
4427                 g_error_free (error);
4428         }
4429
4430         owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
4431                                 EMAIL_SERVICE_NAME,
4432                                 G_BUS_NAME_OWNER_FLAGS_NONE,
4433                                 on_bus_acquired,
4434                                 on_name_acquired,
4435                                 on_name_lost,
4436                                 NULL,
4437                                 NULL);
4438         if (!owner_id) {
4439                 EM_DEBUG_EXCEPTION ("g_bus_own_name error");
4440         }
4441         EM_DEBUG_LOG ("owner_id [%d]", owner_id);
4442
4443         mainloop = g_main_loop_new(NULL, 0);
4444         g_mainloop = mainloop;
4445
4446         g_main_loop_run(mainloop);
4447
4448         /* Clean up resources */
4449         g_bus_unown_name (owner_id);
4450         g_main_loop_unref(mainloop);
4451         g_mainloop = NULL;
4452
4453 FINISH_OFF:
4454
4455         emipc_finalize_stub();
4456         emdaemon_finalize(NULL);
4457         emcore_gmime_shutdown();
4458
4459         EM_DEBUG_LOG ("Goodbye, world");
4460         EM_DEBUG_FUNC_END();
4461         exit(44); /* exit with exit code 44 to prevent restarting */
4462         return 0;
4463 }
4464