Tizen 2.0 Release
[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
35 #include "email-daemon.h"
36 #include "email-ipc.h"
37 #include "email-utilities.h"
38 #include "email-debug-log.h"
39 #include "email-daemon-auto-poll.h"
40 #include "email-daemon-account.h"
41 #include "email-convert.h"
42 #include "email-internal-types.h"
43 #include "email-types.h"
44 #include "email-core-account.h"
45 #include "email-core-mail.h"
46 #include "email-core-smtp.h"
47 #include "email-core-event.h"
48 #include "email-core-global.h"
49 #include "email-core-mailbox.h"
50 #include "email-core-utils.h"
51 #include "email-core-smime.h"
52 #include "email-core-cert.h"
53 #include "email-core-task-manager.h"
54 #include "email-core-signal.h"
55 #include "email-storage.h"
56
57 void stb_create_account(HIPC_API a_hAPI)
58 {
59         EM_DEBUG_FUNC_BEGIN();
60         int buffer_size = 0;
61         int local_result = 0;
62         char* local_account_stream = NULL;
63         email_account_t account;
64         int err = EMAIL_ERROR_NONE;
65
66         /* Initialize the email_account_t */
67         memset(&account, 0x00, sizeof(email_account_t));
68
69         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
70         EM_DEBUG_LOG("size [%d]", buffer_size);
71         if(buffer_size <= 0)    {
72                 err = EMAIL_ERROR_INVALID_PARAM;
73                 goto FINISH_OFF;
74         }
75         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
76         /* Convert account stream to structure */
77         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &account);
78
79
80         EM_DEBUG_LOG("Account name - %s", account.account_name);
81         EM_DEBUG_LOG("Email Address - %s", account.user_email_address);
82
83         if(!emdaemon_create_account(&account, &err)) {
84                 EM_DEBUG_EXCEPTION("emdaemon_create_account fail ");
85                 goto FINISH_OFF;
86         }
87
88
89 #ifdef __FEATURE_AUTO_POLLING__
90         /* start auto polling, if check_interval not zero */
91         if(account.check_interval > 0) {
92                 if(!emdaemon_add_polling_alarm( account.account_id,account.check_interval))
93                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
94         }
95 #endif
96
97         /* add account details to contact DB */
98         emdaemon_insert_accountinfo_to_contact(&account);
99
100         local_result = 1;
101         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
102                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
103
104         EM_DEBUG_LOG("[3] APPID[%d], APIID [%d]", emipc_get_app_id(a_hAPI), emipc_get_api_id(a_hAPI));
105         EM_DEBUG_LOG("account id[%d]", account.account_id);
106
107         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(account.account_id), sizeof(int)))
108                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
109
110         /* pass result to app */
111         if (!emipc_execute_stub_api(a_hAPI))
112                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
113
114 FINISH_OFF:
115         if ( local_result == 0 ) {
116                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
117                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
118                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
119                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
120                 if (!emipc_execute_stub_api(a_hAPI))
121                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
122         }
123         EM_DEBUG_FUNC_END();
124 }
125
126 void stb_delete_account(HIPC_API a_hAPI)
127 {
128         EM_DEBUG_FUNC_BEGIN();
129         int account_id = 0;
130         int local_result = 0;
131         int err = EMAIL_ERROR_NONE;
132
133         /* account_id */
134         account_id = *((int*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
135         if(!emdaemon_delete_account(account_id, &err)) {
136                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
137                         EM_DEBUG_LOG("emipc_add_parameter failed ");
138                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
139                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
140
141                 if (!emipc_execute_stub_api(a_hAPI))
142                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
143                 goto FINISH_OFF;
144         }
145
146 #ifdef __FEATURE_AUTO_POLLING__
147         /* stop auto polling for this acount */
148         if(emdaemon_check_auto_polling_started(account_id)) {
149                 if(!emdaemon_remove_polling_alarm(account_id))
150                         EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ NOTI_ACCOUNT_DELETE] : remove auto poll failed >>> ");
151         }
152 #endif
153         local_result = 1;
154
155         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
156                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
157
158         if (!emipc_execute_stub_api(a_hAPI))
159                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
160
161 FINISH_OFF:
162
163         EM_DEBUG_FUNC_END();
164 }
165
166 void stb_update_account(HIPC_API a_hAPI)
167 {
168         EM_DEBUG_FUNC_BEGIN();
169         int account_id = 0, buffer_size = 0, local_result = 0, with_validation = 0;
170         char* local_account_stream = NULL;
171         email_account_t new_account_info = {0};
172         email_account_t old_account_info = {0};
173         int err = EMAIL_ERROR_NONE;
174         int handle = 0;
175
176         account_id = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
177
178         /* get account structure from stream */
179         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
180         EM_DEBUG_LOG("size [%d]", buffer_size);
181         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
182         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &new_account_info);
183
184         /*get validation flag */
185         with_validation = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 2));
186
187
188         if(!emdaemon_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &old_account_info, &err)) {
189                 EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
190                 goto FINISH_OFF;
191         }
192
193
194         if( EM_SAFE_STRLEN (new_account_info.incoming_server_password) == 0 ) {
195                 EM_SAFE_FREE(new_account_info.incoming_server_password); /* be allocated but has zero length */
196                 EM_DEBUG_LOG("old_account_info->incoming_server_password [%s]", old_account_info.incoming_server_password);
197                 new_account_info.incoming_server_password = EM_SAFE_STRDUP(old_account_info.incoming_server_password);
198                 if(new_account_info.incoming_server_password == NULL) {
199                                 EM_DEBUG_EXCEPTION("allocation for new_account_info->password failed");
200                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
201                                 goto FINISH_OFF;
202                         }
203                 }
204
205
206         if( EM_SAFE_STRLEN (new_account_info.outgoing_server_password) == 0 ) {
207                 EM_SAFE_FREE(new_account_info.outgoing_server_password);
208                 if(old_account_info.outgoing_server_password) {
209                         new_account_info.outgoing_server_password = strdup (old_account_info.outgoing_server_password);
210                         if(new_account_info.outgoing_server_password == NULL) {
211                                 EM_DEBUG_EXCEPTION("allocation for new_account_info->outgoing_server_password failed");
212                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
213                                 goto FINISH_OFF;
214                         }
215                 }
216         }
217
218         if(with_validation) {
219                 emdaemon_validate_account_and_update(account_id, &new_account_info, &handle, &err);
220                 local_result = 1;
221         }
222         else {
223                 if(emdaemon_update_account(account_id, &new_account_info, &err)) {
224                         emdaemon_update_accountinfo_to_contact(&old_account_info, &new_account_info);
225                         local_result = 1;
226                 }
227                 else {
228                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
229                         goto FINISH_OFF;
230                 }
231
232         }
233
234         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
235                 EM_DEBUG_EXCEPTION("emipc_add_parameter for result failed");
236
237         if(with_validation) {
238                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
239                         EM_DEBUG_EXCEPTION("emipc_add_parameter for handle failed");
240         }
241
242         if (!emipc_execute_stub_api(a_hAPI))
243                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
244
245 FINISH_OFF:
246         if ( local_result == 0 ) {
247                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
248                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
249
250                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
251                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
252
253                 if (!emipc_execute_stub_api(a_hAPI))
254                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
255         }
256
257         emcore_free_account(&old_account_info);
258
259         emcore_free_account(&new_account_info);
260
261         EM_DEBUG_FUNC_END();
262 }
263
264 void stb_validate_account(HIPC_API a_hAPI)
265 {
266         EM_DEBUG_FUNC_BEGIN();
267
268         int account_id = 0;
269         int local_result = 0;
270         int err_code = 0;
271         int handle = 0;
272
273         /* account_id */
274         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
275         local_result = emdaemon_validate_account(account_id, &handle,&err_code);
276
277         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
278                 EM_DEBUG_LOG("emipc_add_parameter failed ");
279
280         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
281                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
282
283         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
284                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
285
286         if (!emipc_execute_stub_api(a_hAPI))
287                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
288
289         EM_DEBUG_FUNC_END();
290 }
291
292
293 void stb_get_account_list(HIPC_API a_hAPI)
294 {
295         EM_DEBUG_FUNC_BEGIN();
296
297         int local_result = 0;
298         int i= 0;
299         char* local_stream = NULL;
300         email_account_t* account_list;
301         int count;
302         int size = 0;
303         int err = EMAIL_ERROR_NONE;
304
305         if(emdaemon_get_account_list(&account_list, &count, &err)) {
306                 EM_DEBUG_LOG("emdaemon_get_account_list success");
307                 local_result = 1;
308                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
309                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
310
311                 EM_DEBUG_LOG("Count [%d]", count);
312                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
313                         EM_DEBUG_EXCEPTION("emipc_add_parameter count failed ");
314
315                 for(i=0; i<count; i++) {
316                         EM_DEBUG_LOG("Name - %s", account_list[i].account_name);
317
318                         local_stream = em_convert_account_to_byte_stream(account_list+i, &size);
319
320                         if (!local_stream) {
321                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
322                                 emcore_free_account_list(&account_list, count, NULL);
323                                 return;
324                         }
325
326                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
327                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
328
329                         size = 0;
330                         EM_SAFE_FREE(local_stream);
331                 }
332
333                 emcore_free_account_list(&account_list, count, NULL);
334                 if (!emipc_execute_stub_api(a_hAPI))
335                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
336         }
337
338         else {
339                 EM_DEBUG_LOG("emdaemon_get_account_list failed");
340                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
341                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
342                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
343                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
344                 if (!emipc_execute_stub_api(a_hAPI))
345                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
346         }
347
348         EM_SAFE_FREE(local_stream);
349         EM_DEBUG_FUNC_END();
350 }
351
352 /* sowmya.kr, 10-May-2010, changes for API improvement */
353 void stb_sync_header(HIPC_API a_hAPI)
354 {
355         EM_DEBUG_FUNC_BEGIN();
356         int err = EMAIL_ERROR_NONE;
357         int handle = -1;
358         int account_id = 0, maibox_id = 0;
359
360         /* account_id */
361         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
362         EM_DEBUG_LOG("account_id [%d]", account_id);
363
364         /* maibox_id */
365         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &maibox_id);
366         EM_DEBUG_LOG("maibox_id [%d]", maibox_id);
367
368         if(emdaemon_sync_header(account_id, maibox_id, &handle, &err)) {
369                 EM_DEBUG_LOG("emdaemon_sync_header success ");
370         }
371
372         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
373                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
374         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
375                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
376         if (!emipc_execute_stub_api(a_hAPI))
377                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
378         EM_DEBUG_FUNC_END();
379 }
380
381 void stb_download_body(HIPC_API a_hAPI)
382 {
383         EM_DEBUG_FUNC_BEGIN();
384         int err = EMAIL_ERROR_NONE;
385         int mail_id = 0;
386         int attachment_count = 0;
387         int handle = 0;
388         int account_id = 0;
389
390         /* Account Id */
391         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
392
393         /* Mail Id */
394         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
395
396         /* Has Attachment */
397         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &attachment_count);
398
399         /*Download Body */
400         if (!emdaemon_download_body(account_id, mail_id, 1, attachment_count, &handle, &err)) {
401                 EM_DEBUG_EXCEPTION("emdaemon_download_body - failed");
402                 goto FINISH_OFF;
403         }
404
405         err = EMAIL_ERROR_NONE;
406
407 FINISH_OFF:
408
409         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
410         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
411         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
412                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
413         if (!emipc_execute_stub_api(a_hAPI))
414                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
415
416         EM_DEBUG_FUNC_END();
417 }
418
419
420 void stb_create_mailbox(HIPC_API a_hAPI)
421 {
422         EM_DEBUG_FUNC_BEGIN();
423         int                 buffer_size                         = 0;
424         int                 err = EMAIL_ERROR_NONE;
425         char               *local_stream  = NULL;
426         int                 on_server           = 0;
427         email_mailbox_t mailbox = {0};
428         int             handle = 0; /* Added for cancelling mailbox creating  */
429
430         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
431         EM_DEBUG_LOG("size [%d]", buffer_size);
432
433         if(buffer_size <= 0) {
434                 EM_DEBUG_EXCEPTION("buffer_size(%d) should be greater than 0", buffer_size);
435                 err = EMAIL_ERROR_INVALID_PARAM;
436                 goto FINISH_OFF;
437         }
438
439         local_stream = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
440         em_convert_byte_stream_to_mailbox(local_stream, buffer_size, &mailbox);
441         EM_DEBUG_LOG("Mailbox name - %s", mailbox.mailbox_name);
442
443         on_server = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1));
444
445         emdaemon_add_mailbox(&mailbox, on_server, &handle, &err);
446
447 FINISH_OFF:
448         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
449                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
450         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
451                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
452         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &mailbox.mailbox_id, sizeof(int)))
453                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 3");
454
455         if (!emipc_execute_stub_api(a_hAPI)) {
456                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
457                 return;
458         }
459
460         EM_DEBUG_FUNC_END();
461 }
462
463
464 void stb_delete_mailbox(HIPC_API a_hAPI)
465 {
466         EM_DEBUG_FUNC_BEGIN();
467         int              err = EMAIL_ERROR_NONE;
468         int      on_server              = 0;
469         int      handle = 0; /* Added for cancelling mailbox deleting */
470         int      input_mailbox_id = 0;
471
472         /* src_mailbox_id */
473         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &input_mailbox_id);
474         EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
475
476         if (input_mailbox_id > 0)
477                 EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
478         else
479                 EM_DEBUG_LOG("input_mailbox_id == 0");
480
481         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &on_server);
482
483         if(emdaemon_delete_mailbox(input_mailbox_id, on_server, &handle, &err))
484                 err = EMAIL_ERROR_NONE;
485
486         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
487                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
488         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
489                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
490
491         if (!emipc_execute_stub_api(a_hAPI))
492                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
493
494         EM_DEBUG_FUNC_END();
495 }
496
497 void stb_set_mailbox_type(HIPC_API a_hAPI)
498 {
499         EM_DEBUG_FUNC_BEGIN();
500         int     err = EMAIL_ERROR_NONE;
501         int mailbox_id = 0;
502         int mailbox_type = 0;
503
504         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
505         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
506
507         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_type);
508         EM_DEBUG_LOG("mailbox_type[%d]", mailbox_type);
509
510         if( (err = emdaemon_set_mailbox_type(mailbox_id, mailbox_type)) != EMAIL_ERROR_NONE)
511                 err = EMAIL_ERROR_NONE;
512
513         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
514                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
515
516         if (!emipc_execute_stub_api(a_hAPI))
517                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
518
519         EM_DEBUG_FUNC_END();
520 }
521
522 void stb_set_local_mailbox(HIPC_API a_hAPI)
523 {
524         EM_DEBUG_FUNC_BEGIN();
525         int     err = EMAIL_ERROR_NONE;
526         int mailbox_id = 0;
527         int is_local_mailbox = 0;
528
529         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
530         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
531
532         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &is_local_mailbox);
533         EM_DEBUG_LOG("is_local_mailbox[%d]", is_local_mailbox);
534
535         if( (err = emdaemon_set_local_mailbox(mailbox_id, is_local_mailbox)) != EMAIL_ERROR_NONE)
536                 err = EMAIL_ERROR_NONE;
537
538         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
539                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
540
541         if (!emipc_execute_stub_api(a_hAPI))
542                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
543
544         EM_DEBUG_FUNC_END();
545 }
546
547 void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
548 {
549         EM_DEBUG_FUNC_BEGIN();
550         int     err    = EMAIL_ERROR_NONE;
551         int handle = 0;
552         int account_id = 0;
553         int mailbox_id = 0;
554         int mail_slot_size = 0;
555
556         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
557         EM_DEBUG_LOG("account_id[%d]", account_id);
558
559         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
560         EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
561
562         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_slot_size);
563         EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
564
565         if(emdaemon_set_mail_slot_size_of_mailbox(account_id, mailbox_id, mail_slot_size, &handle, &err))
566                 err = EMAIL_ERROR_NONE;
567         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
568                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
569
570         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
571                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
572
573         if (!emipc_execute_stub_api(a_hAPI))
574                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
575
576         EM_DEBUG_FUNC_END();
577 }
578
579 void stb_rename_mailbox(HIPC_API a_hAPI)
580 {
581         EM_DEBUG_FUNC_BEGIN();
582         int buffer_size  = 0;
583         int     err = EMAIL_ERROR_NONE;
584         int handle = 0;
585         int mailbox_id = 0;
586         int on_server = 0;
587         char *mailbox_path = NULL;
588         char *mailbox_alias = NULL;
589
590         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
591         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
592
593         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
594         EM_DEBUG_LOG("mailbox_path string size[%d]", buffer_size);
595         if(buffer_size > 0)       {
596                 mailbox_path = (char*)em_malloc(buffer_size);
597                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
598                 EM_DEBUG_LOG("mailbox_path [%s]", mailbox_path);
599         }
600
601         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
602         EM_DEBUG_LOG("mailbox_alias string size[%d]", buffer_size);
603         if(buffer_size > 0)       {
604                 mailbox_alias = (char*)em_malloc(buffer_size);
605                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
606                 EM_DEBUG_LOG("mailbox_alias [%s]", mailbox_alias);
607         }
608
609         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &on_server);
610         EM_DEBUG_LOG("on_server[%d]", on_server);
611
612         if ((err = emdaemon_rename_mailbox(mailbox_id, mailbox_path, mailbox_alias, on_server, &handle)) != EMAIL_ERROR_NONE) {
613                 EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
614         }
615
616         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
617                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
618
619         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
620                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
621
622         if (!emipc_execute_stub_api(a_hAPI))
623                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
624
625         EM_SAFE_FREE(mailbox_alias);
626         EM_SAFE_FREE(mailbox_path);
627
628         EM_DEBUG_FUNC_END();
629 }
630
631 void stb_send_mail(HIPC_API a_hAPI)
632 {
633         EM_DEBUG_FUNC_BEGIN();
634         char* local_stream = NULL;
635         int mail_id;
636         int handle;
637         int err = EMAIL_ERROR_NONE;
638
639         /* Mail_id */
640         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
641         EM_DEBUG_LOG("mail_id [%d]", mail_id);
642
643         if(emdaemon_send_mail(mail_id, &handle, &err)) {
644                 err = EMAIL_ERROR_NONE;
645                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
646                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
647                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
648                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
649                 if (!emipc_execute_stub_api(a_hAPI))
650                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
651         }
652         else {
653                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
654                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
655                 if (!emipc_execute_stub_api(a_hAPI))
656                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
657         }
658
659         EM_SAFE_FREE(local_stream);
660
661         EM_DEBUG_FUNC_END();
662 }
663
664
665 /* obsolete - there is no api calling this function */
666 void stb_get_mailbox_list(HIPC_API a_hAPI)
667 {
668         EM_DEBUG_FUNC_BEGIN();
669         int err = EMAIL_ERROR_NONE;
670         int counter = 0;
671         char* local_stream = NULL;
672         int account_id;
673         email_mailbox_t* mailbox_list = NULL;
674         int count = 0;
675         int size = 0;
676
677         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
678
679         if(emdaemon_get_mailbox_list(account_id, &mailbox_list, &count, &err))
680                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
681
682         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
683                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
684
685         if(EMAIL_ERROR_NONE == err) {
686                 EM_DEBUG_LOG("Count [%d]", count);
687                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
688                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
689
690                 for(counter=0; counter<count; counter++) {
691                         EM_DEBUG_LOG("Name - %s", mailbox_list[counter].mailbox_name);
692
693                         local_stream = em_convert_mailbox_to_byte_stream(mailbox_list+counter, &size);
694
695                         if (!local_stream) {
696                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
697                                 emcore_free_mailbox_list(&mailbox_list, count);
698                                 return;
699                         }
700
701                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
702                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
703
704                         EM_SAFE_FREE(local_stream);
705                         size = 0;
706                 }
707         }
708
709         if (!emipc_execute_stub_api(a_hAPI))
710                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
711
712         emcore_free_mailbox_list(&mailbox_list, count);
713
714         EM_DEBUG_FUNC_END();
715 }
716
717 void stb_delete_all_mails(HIPC_API a_hAPI)
718 {
719         EM_DEBUG_FUNC_BEGIN();
720         int mailbox_id = 0;
721         int from_server = 0;
722         int err = EMAIL_ERROR_NONE;
723
724         /* mailbox_id */
725         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
726
727         /* from_server */
728         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &from_server);
729
730         emdaemon_delete_mail_all(mailbox_id, from_server, NULL, &err);
731
732         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
733                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
734         if (!emipc_execute_stub_api(a_hAPI))
735                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
736
737         EM_DEBUG_FUNC_END();
738 }
739
740 void stb_delete_mail(HIPC_API a_hAPI)
741 {
742         EM_DEBUG_FUNC_BEGIN();
743         int err = EMAIL_ERROR_NONE;
744         int mailbox_id = 0;
745         int from_server = 0;
746         int counter = 0;
747         int num;
748         int *mail_ids = NULL;
749
750         /* Mailbox */
751         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
752
753         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
754
755         /* Number of mail_ids */
756         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
757         EM_DEBUG_LOG("number of mails [%d]", num);
758
759         if (num > 0) {
760                 mail_ids = em_malloc(sizeof(int) * num);
761                 if (!mail_ids) {
762                         EM_DEBUG_EXCEPTION("em_malloc failed");
763                         err = EMAIL_ERROR_OUT_OF_MEMORY;
764                         goto FINISH_OFF;
765                 }
766         }
767
768         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
769
770         for(counter = 0; counter < num; counter++)
771                 EM_DEBUG_LOG("mail_ids [%d]", mail_ids[counter]);
772
773         /* from_server */
774         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &from_server);
775         EM_DEBUG_LOG("from_server [%d]", from_server);
776
777         emdaemon_delete_mail(mailbox_id, mail_ids, num, from_server, NULL, &err);
778
779 FINISH_OFF:
780         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
781                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
782
783         if (!emipc_execute_stub_api(a_hAPI))
784                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
785
786         EM_SAFE_FREE(mail_ids);
787
788         EM_DEBUG_FUNC_END();
789 }
790 void stb_clear_mail_data (HIPC_API a_hAPI)
791 {
792         EM_DEBUG_FUNC_BEGIN();
793         int err = EMAIL_ERROR_NONE;
794
795         if(emdaemon_clear_all_mail_data(&err)) {
796                 EM_DEBUG_LOG(">>> stb_clear_mail_data Success [ %d]  >> ", err);
797         }
798
799         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
800                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
801         if (!emipc_execute_stub_api(a_hAPI))
802                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
803         EM_DEBUG_FUNC_END();
804 }
805
806 void stb_add_rule(HIPC_API a_hAPI)
807 {
808         EM_DEBUG_FUNC_BEGIN();
809         int buffer_size = 0;
810         int err = EMAIL_ERROR_NONE;
811         char* local_rule_stream = NULL;
812         email_rule_t rule = { 0 };
813
814         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
815         if(buffer_size <= 0) {
816                 err = EMAIL_ERROR_INVALID_PARAM;
817                 goto FINISH_OFF;
818         }
819
820         local_rule_stream = (char*)     emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
821         if(!local_rule_stream) {
822                 err = EMAIL_ERROR_INVALID_PARAM;
823                 goto FINISH_OFF;
824         }
825         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
826         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
827
828         /* call add_filter handler */
829         err = emdaemon_add_filter(&rule);
830
831 FINISH_OFF:
832         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
833                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
834         if (!emipc_execute_stub_api(a_hAPI))
835                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
836
837         emcore_free_rule(&rule);
838
839         EM_DEBUG_FUNC_END();
840 }
841
842
843 /* obsolete - there is no api calling this function */
844 void stb_get_rule(HIPC_API a_hAPI)
845 {
846         EM_DEBUG_FUNC_BEGIN();
847         int err = EMAIL_ERROR_NONE;
848         int filter_id = 0;
849         email_rule_t* rule = NULL;
850         int size =0;
851         char* local_rule_stream = NULL;
852
853         filter_id = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
854
855         emdaemon_get_filter(filter_id, &rule, &err);
856
857         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
858                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
859
860         /* insert a rule if there exists a rule */
861         if ( rule ) {
862                 local_rule_stream = em_convert_rule_to_byte_stream(rule, &size);
863                 if(!local_rule_stream) { /*prevent 26265*/
864                         EM_DEBUG_EXCEPTION("em_convert_rule_to_byte_stream failed");
865                         emcore_free_rule(rule);
866                         EM_SAFE_FREE(rule);
867                         return;
868                 }
869
870                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_rule_stream, size))
871                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
872
873                 EM_SAFE_FREE(local_rule_stream);
874                 emcore_free_rule(rule);
875                 EM_SAFE_FREE(rule);
876         }
877
878         if (!emipc_execute_stub_api(a_hAPI)) {
879                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
880                 return;
881         }
882
883         EM_DEBUG_FUNC_END();
884 }
885
886 /* obsolete - there is no api calling this function */
887 void stb_get_rule_list(HIPC_API a_hAPI)
888 {
889         EM_DEBUG_FUNC_BEGIN();
890         int err = EMAIL_ERROR_NONE;
891         int i = 0;
892         char* local_stream = NULL;
893         int count = 0;
894         int size = 0;
895         email_rule_t* filtering_list = NULL;
896
897         emdaemon_get_filter_list(&filtering_list, &count, &err);
898
899         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
900                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
901
902         /* insert rules if there exist rules*/
903         if( count > 0 ) {
904                 EM_DEBUG_LOG("num of rules [%d]", count);
905                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
906                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
907
908                 for(i=0; i<count; i++) {
909                         EM_DEBUG_LOG("Value - %s", filtering_list[i].value);
910
911                         local_stream = em_convert_rule_to_byte_stream(filtering_list+i, &size);
912
913                         if(!local_stream) break;
914
915                         if(!emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
916                                 EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed  ");
917
918                         size = 0;
919                 }
920         }
921
922         if (!emipc_execute_stub_api(a_hAPI)) {
923                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
924         }
925
926         EM_DEBUG_FUNC_END();
927 }
928
929 /* obsolete - there is no api calling this function */
930 void stb_find_rule(HIPC_API a_hAPI)
931 {
932         EM_DEBUG_FUNC_BEGIN();
933         int buffer_size = 0;
934         int err = EMAIL_ERROR_NONE;
935         char* local_rule_stream = NULL;
936         email_rule_t rule = { 0 };
937
938
939         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
940
941         if(buffer_size > 0)      {
942                 local_rule_stream = (char*)em_malloc(buffer_size);
943                 EM_NULL_CHECK_FOR_VOID(local_rule_stream);
944                 if(local_rule_stream) {
945                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, buffer_size, local_rule_stream);
946                         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
947                         EM_SAFE_FREE(local_rule_stream);
948                         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
949
950                         if(emdaemon_find_filter(&rule, &err))
951                                 err = EMAIL_ERROR_NONE;
952
953                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
954                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
955                         if (!emipc_execute_stub_api(a_hAPI))
956                                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
957
958                 }
959         }
960         EM_DEBUG_FUNC_END();
961 }
962
963 void stb_update_rule(HIPC_API a_hAPI)
964 {
965         EM_DEBUG_FUNC_BEGIN();
966
967         int filter_id = 0;
968         int buffer_size = 0;
969         int err = EMAIL_ERROR_NONE;
970         char* rule_stream = NULL;
971         email_rule_t rule = {0};
972
973
974         /* get filter_id */
975         filter_id = *((int*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
976
977         /* get rule */
978         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
979         EM_DEBUG_LOG("size [%d]", buffer_size);
980         if(buffer_size <= 0)  {
981                 err = EMAIL_ERROR_INVALID_PARAM;
982                 goto FINISH_OFF;
983         }
984         rule_stream     = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
985         if(!rule_stream) {
986                 err = EMAIL_ERROR_INVALID_PARAM;
987                 goto FINISH_OFF;
988         }
989         em_convert_byte_stream_to_rule(rule_stream, buffer_size, &rule);
990
991         /* call update handler */
992         emdaemon_update_filter(filter_id, &rule, &err);
993
994 FINISH_OFF:
995         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
996                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
997
998         if (!emipc_execute_stub_api(a_hAPI))
999                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1000
1001         emcore_free_rule(&rule);
1002
1003         EM_DEBUG_FUNC_END();
1004 }
1005
1006 void stb_move_all_mails(HIPC_API a_hAPI)
1007 {
1008         EM_DEBUG_FUNC_BEGIN();
1009         int err = EMAIL_ERROR_NONE;
1010         int src_mailbox_id = 0, dst_mailbox_id = 0;
1011
1012         /* src_mailbox_id */
1013         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &src_mailbox_id);
1014         EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1015
1016         if (src_mailbox_id > 0)
1017                 EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1018         else
1019                 EM_DEBUG_LOG("src_mailbox_id == 0");
1020
1021         /* dst_mailbox_id */
1022         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &dst_mailbox_id);
1023         EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1024
1025         if (dst_mailbox_id > 0)
1026                 EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1027         else
1028                 EM_DEBUG_LOG("dst_mailbox_id == 0");
1029
1030         if(emdaemon_move_mail_all_mails(src_mailbox_id, dst_mailbox_id, &err))
1031                 EM_DEBUG_LOG("emdaemon_move_mail_all_mails:Success");
1032         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1033                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1034                 return;
1035         }
1036
1037         if (!emipc_execute_stub_api(a_hAPI)) {
1038                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1039                 return;
1040         }
1041         EM_DEBUG_FUNC_END();
1042 }
1043
1044 void stb_set_flags_field(HIPC_API a_hAPI)
1045 {
1046         EM_DEBUG_FUNC_BEGIN();
1047         int err = EMAIL_ERROR_NONE;
1048         email_flags_field_type field_type = 0;
1049         int account_id;
1050         int value = 0;
1051         int onserver = 0;
1052         int num = 0;
1053         int counter = 0;
1054         int *mail_ids = NULL;
1055
1056         /* account_id */
1057         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1058         EM_DEBUG_LOG("account_id [%d]", account_id);
1059
1060         /* Number of mail_ids */
1061         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
1062         EM_DEBUG_LOG("number of mails [%d]", num);
1063
1064         /* mail_id */
1065         mail_ids = em_malloc(sizeof(int) * num);
1066
1067         if(!mail_ids) {
1068                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1069                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1070                 goto FINISH_OFF;
1071         }
1072
1073         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
1074
1075         for(counter=0; counter < num; counter++)
1076                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1077
1078         /* field type */
1079         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &field_type);
1080         EM_DEBUG_LOG("field_type [%d]", field_type);
1081
1082         /* value */
1083         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &value);
1084         EM_DEBUG_LOG("value [%d]", value);
1085
1086         /*  on server */
1087         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 5, sizeof(int), &onserver);
1088         EM_DEBUG_LOG("onserver [%d]", onserver);
1089
1090         if(emdaemon_set_flags_field(account_id, mail_ids, num, field_type, value, onserver, &err))
1091                 EM_DEBUG_LOG("emdaemon_set_flags_field - success");
1092
1093 FINISH_OFF:
1094
1095         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1096                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1097
1098         if (!emipc_execute_stub_api(a_hAPI))
1099                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1100
1101         if (mail_ids)
1102                 EM_SAFE_FREE(mail_ids);
1103
1104         EM_DEBUG_FUNC_END();
1105 }
1106
1107 void stb_add_mail(HIPC_API a_hAPI)
1108 {
1109         EM_DEBUG_FUNC_BEGIN();
1110         int  buffer_size = 0;
1111         int  local_result = 0;
1112         int  result_attachment_data_count = 0;
1113         int  param_index = 0;
1114         int  sync_server = 0;
1115         int  err = EMAIL_ERROR_NONE;
1116         email_mail_data_t result_mail_data = {0};
1117         email_attachment_data_t *result_attachment_data = NULL;
1118         email_meeting_request_t result_meeting_request = {0};
1119
1120
1121         /* email_mail_data_t */;
1122         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1123         EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1124
1125         /* mail_data */
1126         if(buffer_size > 0)      {
1127                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1128                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1129         }
1130
1131         /* attachment */
1132         if (result_mail_data.attachment_count > 0) {
1133                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1134                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1135
1136                 if(buffer_size > 0)      {
1137                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1138                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1139
1140                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1141
1142                         if(result_attachment_data_count && !result_attachment_data) {
1143                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1144                                 err = EMAIL_ERROR_ON_PARSING;
1145                                 goto FINISH_OFF;
1146                         }
1147                 }
1148
1149                 param_index++;
1150         }
1151
1152
1153         /* meeting request */
1154         EM_DEBUG_LOG("email_meeting_request_t");
1155         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1156                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1157                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1158                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1159
1160                 if(buffer_size > 0) {
1161                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1162                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1163                 }
1164         }
1165
1166         EM_DEBUG_LOG("sync_server");
1167         emipc_get_parameter(a_hAPI, ePARAMETER_IN, param_index++, sizeof(int), &sync_server);
1168
1169         if( (err = emdaemon_add_mail(&result_mail_data, result_attachment_data, result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1170                 EM_DEBUG_EXCEPTION("emdaemon_add_mail failed [%d]", err);
1171                 goto FINISH_OFF;
1172         }
1173
1174         local_result = 1;
1175         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1176                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1177         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1178                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1179         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1180                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1181         if (!emipc_execute_stub_api(a_hAPI))
1182                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1183
1184 FINISH_OFF:
1185         if ( local_result == 0 ) {
1186                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1187                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1188                 if (!emipc_execute_stub_api(a_hAPI))
1189                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1190         }
1191
1192         emcore_free_mail_data(&result_mail_data);
1193
1194         if(result_attachment_data)
1195                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1196
1197         emstorage_free_meeting_request(&result_meeting_request);
1198
1199         em_flush_memory();
1200
1201         EM_DEBUG_FUNC_END();
1202 }
1203
1204
1205 void stb_update_mail(HIPC_API a_hAPI)
1206 {
1207         EM_DEBUG_FUNC_BEGIN();
1208         int  buffer_size = 0;
1209         int  local_result = 0;
1210         int  result_attachment_data_count = 0;
1211         int  param_index = 0;
1212         int  sync_server = 0;
1213         int  *temp_buffer = NULL;
1214         int  err = EMAIL_ERROR_NONE;
1215         email_mail_data_t result_mail_data = {0};
1216         email_attachment_data_t *result_attachment_data = NULL;
1217         email_meeting_request_t result_meeting_request = {0};
1218
1219         EM_DEBUG_LOG("email_mail_data_t");
1220         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1221
1222         if(buffer_size > 0)      {
1223                 char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1224                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1225         }
1226
1227         if (result_mail_data.attachment_count > 0) {
1228                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1229                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1230
1231                 if(buffer_size > 0) {
1232                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1233                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1234
1235                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1236
1237                         if(result_attachment_data_count && !result_attachment_data) {
1238                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1239                                 err = EMAIL_ERROR_ON_PARSING;
1240                                 goto FINISH_OFF;
1241                         }
1242                 }
1243                 param_index++;
1244         }
1245
1246         EM_DEBUG_LOG("email_meeting_request_t");
1247
1248         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1249                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1250                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1251                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1252
1253                 if(buffer_size > 0) {
1254                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1255                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1256                 }
1257         }
1258
1259         EM_DEBUG_LOG("sync_server");
1260
1261         temp_buffer = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1262
1263         if(!temp_buffer) {
1264                 EM_DEBUG_EXCEPTION("emipc_get_nth_parameter_data[%d] failed", param_index - 1);
1265                 err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1266                 goto FINISH_OFF;
1267         }
1268
1269         sync_server = *temp_buffer;
1270
1271         if( (err = emdaemon_update_mail(&result_mail_data, result_attachment_data,
1272                         result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1273                 EM_DEBUG_EXCEPTION("emdaemon_update_mail failed [%d]", err);
1274                 goto FINISH_OFF;
1275         }
1276
1277         local_result = 1;
1278         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1279                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1280         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1281                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1282         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1283                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1284         if (!emipc_execute_stub_api(a_hAPI))
1285                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1286
1287 FINISH_OFF:
1288         if ( local_result == 0 ) {
1289                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1290                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1291                 if (!emipc_execute_stub_api(a_hAPI))
1292                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1293         }
1294
1295         emcore_free_mail_data(&result_mail_data);
1296
1297         if(result_attachment_data)
1298                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1299
1300         emstorage_free_meeting_request(&result_meeting_request);
1301
1302         em_flush_memory();
1303         EM_DEBUG_FUNC_END();
1304 }
1305
1306
1307 void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
1308 {
1309         EM_DEBUG_FUNC_BEGIN();
1310         int mailbox_id = 0, thread_id = 0, move_always_flag = 0;
1311         int err = EMAIL_ERROR_NONE;
1312         char *target_mailbox_name = NULL;
1313
1314         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1315         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1316
1317         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1318         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1319
1320         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &move_always_flag);
1321         EM_DEBUG_LOG("move_always_flag [%d]", move_always_flag);
1322
1323         if(emdaemon_move_mail_thread_to_mailbox(thread_id, mailbox_id, move_always_flag, &err))
1324                 EM_DEBUG_LOG("emdaemon_move_mail_thread_to_mailbox success");
1325
1326         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1327                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1328                 EM_SAFE_FREE(target_mailbox_name);
1329                 return;
1330         }
1331
1332         if (!emipc_execute_stub_api(a_hAPI)) {
1333                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1334                 EM_SAFE_FREE(target_mailbox_name);
1335                 return;
1336         }
1337
1338         EM_SAFE_FREE(target_mailbox_name);
1339         EM_DEBUG_FUNC_END();
1340 }
1341
1342 void stb_delete_thread(HIPC_API a_hAPI)
1343 {
1344         EM_DEBUG_FUNC_BEGIN();
1345
1346         int thread_id = 0, delete_always_flag = 0;
1347         int handle = 0;
1348         int err = EMAIL_ERROR_NONE;
1349
1350         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1351         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1352
1353         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &delete_always_flag);
1354         EM_DEBUG_LOG("delete_always_flag [%d]", delete_always_flag);
1355
1356         if(emdaemon_delete_mail_thread(thread_id, delete_always_flag, &handle, &err))
1357                 EM_DEBUG_LOG("emdaemon_delete_mail_thread success");
1358
1359         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1360                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1361                 return;
1362         }
1363
1364         if (!emipc_execute_stub_api(a_hAPI)) {
1365                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1366                 return;
1367         }
1368         EM_DEBUG_FUNC_END();
1369 }
1370
1371 void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
1372 {
1373         EM_DEBUG_FUNC_BEGIN();
1374
1375         int thread_id = 0, seen_flag = 0, on_server = 0;
1376         int handle = 0;
1377         int err = EMAIL_ERROR_NONE;
1378
1379         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1380         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1381
1382         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &seen_flag);
1383         EM_DEBUG_LOG("seen_flag [%d]", seen_flag);
1384
1385         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &on_server);
1386         EM_DEBUG_LOG("on_server [%d]", on_server);
1387
1388         if(emdaemon_modify_seen_flag_of_thread(thread_id, seen_flag, on_server, &handle, &err))
1389                 EM_DEBUG_LOG("emdaemon_modify_seen_flag_of_thread success");
1390
1391         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1392                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1393                 return;
1394         }
1395
1396         if (!emipc_execute_stub_api(a_hAPI)) {
1397                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1398                 return;
1399         }
1400         EM_DEBUG_FUNC_END();
1401 }
1402
1403 void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
1404 {
1405         EM_DEBUG_FUNC_BEGIN();
1406
1407         int mailbox_id = 0, on_server = 0;
1408         int handle = 0;
1409         int err = EMAIL_ERROR_NONE;
1410
1411         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), (void*)&mailbox_id);
1412         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1413
1414         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
1415         EM_DEBUG_LOG("on_server [%d]", on_server);
1416
1417         if( (err = emdaemon_expunge_mails_deleted_flagged(mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
1418                 EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
1419
1420         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1421                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1422                 return;
1423         }
1424
1425         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
1426                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1427                 return;
1428         }
1429
1430         if (!emipc_execute_stub_api(a_hAPI)) {
1431                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1432                 return;
1433         }
1434         EM_DEBUG_FUNC_END();
1435 }
1436
1437 void stb_update_mail_attribute(HIPC_API a_hAPI)
1438 {
1439         EM_DEBUG_FUNC_BEGIN();
1440         int err = EMAIL_ERROR_NONE;
1441         int mail_id_count = 0, counter = 0;
1442         email_mail_attribute_type attribute;
1443         /* email_mail_attribute_value_t value; */
1444
1445         /* mail_id_count */
1446         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id_count);
1447         EM_DEBUG_LOG("mail_id_count [%d]", mail_id_count);
1448
1449         /* mail_id_array */
1450         int mail_id_array[mail_id_count];
1451         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, mail_id_count * sizeof(int), mail_id_array);
1452
1453         for(counter = 0; counter < mail_id_count; counter++)
1454                 EM_DEBUG_LOG("mail_id[%d] [%d]", counter, mail_id_array[counter]);
1455
1456         /* attribute */
1457         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &attribute);
1458         EM_DEBUG_LOG("attribute [%d]", attribute);
1459
1460         /* value */
1461         /*
1462         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &value);
1463         EM_DEBUG_LOG("target_mailbox_id [%d]", mailbox_id);
1464         */
1465
1466         /*
1467         if(emdaemon_move_mail(mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
1468                 EM_DEBUG_LOG("emdaemon_move_mail success");
1469         */
1470
1471         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1472                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1473
1474         if (!emipc_execute_stub_api(a_hAPI))
1475                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1476         EM_DEBUG_FUNC_END();
1477 }
1478
1479 void stb_move_mail(HIPC_API a_hAPI)
1480 {
1481         int err = EMAIL_ERROR_NONE;
1482         int num = 0, counter = 0, mailbox_id = 0;
1483
1484         /* Number of mail_ids */
1485         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &num);
1486         EM_DEBUG_LOG("number of mails [%d]", num);
1487
1488         /* mail_id */
1489         int mail_ids[num];
1490         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, num * sizeof(int), mail_ids);
1491
1492         for(counter = 0; counter < num; counter++)
1493                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1494
1495         /* target_mailbox_id */
1496         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mailbox_id);
1497         EM_DEBUG_LOG("target_mailbox_id [%d]", mailbox_id);
1498
1499         if (mailbox_id > 0)
1500                 EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1501         else
1502                 EM_DEBUG_LOG("mailbox_id == 0");
1503
1504         if(emdaemon_move_mail(mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
1505                 EM_DEBUG_LOG("emdaemon_move_mail success");
1506
1507         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1508                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1509
1510         if (!emipc_execute_stub_api(a_hAPI))
1511                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1512         EM_DEBUG_FUNC_END();
1513 }
1514
1515 void stb_delete_rule(HIPC_API a_hAPI)
1516 {
1517         EM_DEBUG_FUNC_BEGIN();
1518
1519         int filter_id = 0;
1520         int err = EMAIL_ERROR_NONE;
1521
1522         /* filter_id */
1523         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
1524
1525         if(emdaemon_delete_filter(filter_id, &err))
1526                 err = EMAIL_ERROR_NONE;
1527
1528         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1529                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1530
1531         if (!emipc_execute_stub_api(a_hAPI))
1532                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1533         EM_DEBUG_FUNC_END();
1534 }
1535
1536 void stb_add_attachment(HIPC_API a_hAPI)
1537 {
1538         EM_DEBUG_FUNC_BEGIN();
1539         int buffer_size = 0;
1540         int err = EMAIL_ERROR_NONE;
1541         int mail_id = -1;
1542         int attachment_count = 0;
1543         char* attachment_stream = NULL;
1544         email_attachment_data_t* attachment = NULL;
1545
1546         /* mail_id */
1547         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
1548
1549         /* attachment */
1550         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
1551
1552         if(buffer_size > 0)      {
1553                 attachment_stream = (char*)em_malloc(buffer_size);
1554                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
1555                 if(attachment_stream) {
1556                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, attachment_stream);
1557                         em_convert_byte_stream_to_attachment_data(attachment_stream, buffer_size, &attachment, &attachment_count);
1558                         EM_SAFE_FREE(attachment_stream);
1559                 }
1560         }
1561
1562         if (!attachment) {
1563                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed  ");
1564                 return;
1565         }
1566
1567         emdaemon_add_attachment(mail_id, attachment, &err);
1568
1569         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1570                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1571         if(EMAIL_ERROR_NONE == err) {
1572                 EM_DEBUG_LOG("emdaemon_add_attachment -Success");
1573
1574                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(attachment->attachment_id), sizeof(int)))
1575                         EM_DEBUG_EXCEPTION("emipc_add_parameter attachment_id failed ");
1576         }
1577                 if (!emipc_execute_stub_api(a_hAPI))
1578                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1579
1580         EM_SAFE_FREE(attachment);
1581         EM_DEBUG_FUNC_END();
1582 }
1583
1584 void stb_get_attachment(HIPC_API a_hAPI)
1585 {
1586         EM_DEBUG_FUNC_BEGIN();
1587         int err = EMAIL_ERROR_NONE;
1588         int attachment_id;
1589         char* attachment_stream = NULL;
1590         email_attachment_data_t* attachment = NULL;
1591         int size = 0;
1592
1593         /* attachment_id */
1594         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
1595
1596         emdaemon_get_attachment(attachment_id, &attachment, &err);
1597
1598         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1599                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1600
1601         if(EMAIL_ERROR_NONE == err) {
1602                 EM_DEBUG_LOG("emdaemon_get_attachment - Success");
1603                 /* attachment */
1604                 attachment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
1605                 if(!attachment_stream) { /*prevent 26263*/
1606                         emcore_free_attachment_data(&attachment, 1, &err);
1607                         return;
1608                 }
1609                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
1610
1611                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, attachment_stream, size))
1612                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1613         }
1614
1615         if (!emipc_execute_stub_api(a_hAPI)) {
1616                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1617         }
1618
1619         EM_SAFE_FREE(attachment_stream);
1620         emcore_free_attachment_data(&attachment, 1, &err);
1621
1622         EM_DEBUG_FUNC_END();
1623 }
1624
1625 void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
1626 {
1627         EM_DEBUG_FUNC_BEGIN();
1628         int err = EMAIL_ERROR_NONE;
1629         int account_id = 0;
1630         int handle = 0;
1631
1632         /* account_id */
1633         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1634
1635         if(emdaemon_get_imap_mailbox_list(account_id, "", &handle, &err))
1636                 err = EMAIL_ERROR_NONE;
1637
1638         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1639                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1640         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1641                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1642         if (!emipc_execute_stub_api(a_hAPI))
1643                 EM_DEBUG_LOG("emipc_execute_stub_api failed  ");
1644         EM_DEBUG_FUNC_END();
1645 }
1646
1647 void stb_delete_attachment(HIPC_API a_hAPI)
1648 {
1649         EM_DEBUG_FUNC_BEGIN();
1650         int err = EMAIL_ERROR_NONE;
1651         int attachment_id = 0;
1652
1653         /* attachment_index */
1654         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
1655
1656         emdaemon_delete_mail_attachment(attachment_id, &err);
1657
1658         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1659                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1660         if (!emipc_execute_stub_api(a_hAPI))
1661                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1662
1663         EM_DEBUG_FUNC_END();
1664 }
1665
1666 void stb_download_attachment(HIPC_API a_hAPI)
1667 {
1668         EM_DEBUG_FUNC_BEGIN();
1669
1670         int err = EMAIL_ERROR_NONE;
1671         int mail_id = 0;
1672         int nth = 0;
1673         int handle = 0;
1674         int account_id = 0;
1675
1676         EM_DEBUG_LOG("account_id");
1677         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1678
1679         EM_DEBUG_LOG("mail_id");
1680         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
1681
1682         EM_DEBUG_LOG("nth");
1683         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &nth);
1684
1685         if(emdaemon_download_attachment(account_id, mail_id, nth, &handle, &err)) {
1686                 err = EMAIL_ERROR_NONE;
1687
1688                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1689                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1690                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
1691                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1692                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
1693                 if (!emipc_execute_stub_api(a_hAPI))
1694                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1695         }
1696         else {
1697                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1698                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1699                 /* Download handle - 17-Apr-09 */
1700                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
1701                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1702                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
1703                 if (!emipc_execute_stub_api(a_hAPI))
1704                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1705         }
1706
1707         EM_DEBUG_FUNC_END();
1708 }
1709
1710 void stb_mail_send_saved(HIPC_API a_hAPI)
1711 {
1712         EM_DEBUG_FUNC_BEGIN();
1713         int err = EMAIL_ERROR_NONE;
1714         int account_id = 0;
1715
1716         /* account_id */
1717         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1718
1719         EM_DEBUG_LOG("calling emdaemon_send_mail_saved");
1720         if(emdaemon_send_mail_saved(account_id, NULL, &err))
1721                 err = EMAIL_ERROR_NONE;
1722
1723         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1724                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
1725
1726         if (!emipc_execute_stub_api(a_hAPI))
1727                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1728
1729         EM_DEBUG_FUNC_END();
1730 }
1731
1732 void stb_add_read_receipt(HIPC_API a_hAPI){
1733         EM_DEBUG_FUNC_BEGIN();
1734         int read_mail_id = 0;
1735         int receipt_mail_id = 0;
1736         int err = EMAIL_ERROR_NONE;
1737
1738         /* read_mail_id */
1739         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &read_mail_id);
1740         EM_DEBUG_LOG("read_mail_id [%d]", read_mail_id);
1741
1742         if( (err = emcore_add_read_receipt(read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
1743                 EM_DEBUG_EXCEPTION("emcore_add_read_receipt failed [%d]", err);
1744
1745         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1746                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1747
1748         if (err == EMAIL_ERROR_NONE) {
1749                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &receipt_mail_id, sizeof(int)))
1750                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1751         }
1752
1753         if (!emipc_execute_stub_api(a_hAPI))
1754                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1755
1756         EM_DEBUG_FUNC_END("err [%d]", err);
1757 }
1758
1759 void stb_retry_sending_mail(HIPC_API a_hAPI)
1760 {
1761
1762         EM_DEBUG_FUNC_BEGIN();
1763         int mail_id = 0;
1764         int timeout_in_sec = 0;
1765         int err = EMAIL_ERROR_NONE;
1766
1767         /* Mail_id */
1768         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
1769         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1770
1771         /* timeout_in_sec */
1772         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &timeout_in_sec);
1773         EM_DEBUG_LOG("timeout_in_sec [%d]", timeout_in_sec);
1774
1775         if(emdaemon_send_mail_retry(mail_id, timeout_in_sec,&err))
1776                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
1777
1778         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1779                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1780
1781                 if (!emipc_execute_stub_api(a_hAPI))
1782                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1783         EM_DEBUG_FUNC_END();
1784 }
1785
1786 void stb_get_event_queue_status(HIPC_API a_hAPI)
1787 {
1788         EM_DEBUG_FUNC_BEGIN();
1789         int on_sending = 0;
1790         int on_receiving = 0;
1791
1792         /*get the network status */
1793         emdaemon_get_event_queue_status(&on_sending, &on_receiving);
1794
1795         /* on_sending */
1796         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &on_sending, sizeof(int)))
1797                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1798
1799         /* on_receving */
1800         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &on_receiving, sizeof(int)))
1801                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1802
1803         EM_DEBUG_LOG("stb_get_event_queue_status - Before Execute API");
1804         if (!emipc_execute_stub_api(a_hAPI))
1805                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1806
1807         EM_DEBUG_FUNC_END();
1808 }
1809
1810 void stb_cancel_job(HIPC_API a_hAPI)
1811 {
1812         EM_DEBUG_FUNC_BEGIN();
1813         int account_id = 0;
1814         int handle = 0;
1815         int err = EMAIL_ERROR_NONE;
1816
1817         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1818         EM_DEBUG_LOG("account_id [%d]", account_id);
1819
1820         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &handle);
1821         EM_DEBUG_LOG("handle [%d]", handle);
1822
1823         if(emdaemon_cancel_job(account_id, handle, &err))
1824                 err = EMAIL_ERROR_NONE;
1825
1826         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1827                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1828         if (!emipc_execute_stub_api(a_hAPI))
1829                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1830         EM_DEBUG_FUNC_END();
1831 }
1832
1833 void stb_get_pending_job(HIPC_API a_hAPI)
1834 {
1835         EM_DEBUG_FUNC_BEGIN();
1836         email_action_t action = -1;
1837         int account_id = 0;
1838         int mail_id = -1;
1839         int err = EMAIL_ERROR_NONE;
1840         email_event_status_type_t status = 0;
1841
1842         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &action);
1843         EM_DEBUG_LOG("action [%d]", action);
1844
1845         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &account_id);
1846         EM_DEBUG_LOG("account_id [%d]", account_id);
1847
1848         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_id);
1849         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1850
1851         if(emdaemon_get_pending_job(action, account_id, mail_id, &status))
1852                 err = EMAIL_ERROR_NONE;
1853         else
1854                 err = EMAIL_ERROR_UNKNOWN;
1855
1856         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1857                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1858         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &status, sizeof(int)))
1859                 EM_DEBUG_EXCEPTION("emipc_add_parameter status failed ");
1860         if (!emipc_execute_stub_api(a_hAPI))
1861                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1862         EM_DEBUG_FUNC_END();
1863 }
1864
1865 void stb_print_receiving_event_queue_via_debug_msg(HIPC_API a_hAPI)
1866 {
1867         email_event_t *event_queue = NULL;
1868         int event_active_queue = 0, err, i;
1869
1870         emcore_get_receiving_event_queue(&event_queue, &event_active_queue, &err);
1871
1872         EM_DEBUG_LOG("======================================================================");
1873         EM_DEBUG_LOG("Event active index [%d]", event_active_queue);
1874         EM_DEBUG_LOG("======================================================================");
1875         for(i = 1; i < 32; i++)
1876                 EM_DEBUG_LOG("event[%d] : type[%d], account_id[%d], arg[%d], status[%d]", i, event_queue[i].type, event_queue[i].account_id, event_queue[i].event_param_data_4, event_queue[i].status);
1877         EM_DEBUG_LOG("======================================================================");
1878         EM_DEBUG_LOG("======================================================================");
1879
1880         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1881                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1882
1883         if (!emipc_execute_stub_api(a_hAPI))
1884                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1885         EM_DEBUG_FUNC_END();
1886 }
1887
1888 void stb_cancel_send_mail_job(HIPC_API a_hAPI)
1889 {
1890         EM_DEBUG_FUNC_BEGIN();
1891         int mail_id = 0;
1892         int err = EMAIL_ERROR_NONE;
1893         int account_id = 0;
1894
1895         /* account_id */
1896         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1897         EM_DEBUG_LOG("account_id [%d]", account_id);
1898
1899         /* Mail_id */
1900         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
1901         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1902
1903         if(emdaemon_cancel_sending_mail_job(account_id,mail_id,&err))
1904                 EM_DEBUG_LOG("success");
1905
1906         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1907                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1908
1909         if (!emipc_execute_stub_api(a_hAPI))
1910                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1911         EM_DEBUG_FUNC_END();
1912 }
1913
1914 void stb_search_mail_on_server(HIPC_API a_hAPI)
1915 {
1916         EM_DEBUG_FUNC_BEGIN();
1917         int                    i = 0;
1918         int                    err = EMAIL_ERROR_NONE;
1919         int                    account_id = 0;
1920         int                                        mailbox_id = 0;
1921         int                    buffer_size = 0;
1922         int                    search_filter_count = 0;
1923         char                  *stream_for_search_filter_list = NULL;
1924         unsigned int           job_handle = 0;
1925         email_search_filter_t *search_filter_list = NULL;
1926
1927         /* account_id */
1928         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1929         EM_DEBUG_LOG("account_id [%d]", account_id);
1930
1931         /* mailbox_id */
1932         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1933         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1934
1935         /* search_filter_list */
1936         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
1937
1938         if(buffer_size > 0)      {
1939                 stream_for_search_filter_list = (char*)em_malloc(buffer_size);
1940                 EM_NULL_CHECK_FOR_VOID(stream_for_search_filter_list);
1941                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, stream_for_search_filter_list);
1942                 em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, &search_filter_list, &search_filter_count);
1943                 EM_SAFE_FREE(stream_for_search_filter_list);
1944         }
1945
1946         if(!emdaemon_search_mail_on_server(account_id ,mailbox_id, search_filter_list, search_filter_count, &job_handle, &err))
1947                 EM_DEBUG_LOG("success");
1948
1949         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1950                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1951
1952         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
1953                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1954
1955         if (!emipc_execute_stub_api(a_hAPI))
1956                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1957
1958         if(search_filter_list) {
1959                 for(i = 0; i < search_filter_count; i++) {
1960                         switch(search_filter_list[i].search_filter_type) {
1961                                 case EMAIL_SEARCH_FILTER_TYPE_BCC              :
1962                                 case EMAIL_SEARCH_FILTER_TYPE_CC               :
1963                                 case EMAIL_SEARCH_FILTER_TYPE_FROM             :
1964                                 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
1965                                 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
1966                                 case EMAIL_SEARCH_FILTER_TYPE_TO               :
1967                                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
1968                                         EM_SAFE_FREE(search_filter_list[i].search_filter_key_value.string_type_key_value);
1969                                         break;
1970                                 default :
1971                                         break;
1972                         }
1973                 }
1974         }
1975
1976         EM_SAFE_FREE(search_filter_list);
1977
1978         EM_DEBUG_FUNC_END();
1979 }
1980
1981 void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
1982 {
1983         EM_DEBUG_FUNC_BEGIN();
1984         int                      err = EMAIL_ERROR_NONE;
1985         int                      account_id = 0;
1986
1987         /* account_id */
1988         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1989
1990         EM_DEBUG_LOG("account_id [%d]", account_id);
1991
1992         if (!emstorage_delete_mail_by_mailbox(account_id, EMAIL_SEARCH_RESULT_MAILBOX_NAME, true, &err)) 
1993                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1994
1995         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1996                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1997
1998         if (!emipc_execute_stub_api(a_hAPI))
1999                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2000
2001         EM_DEBUG_FUNC_END();
2002 }
2003
2004
2005
2006 void stb_add_account_with_validation(HIPC_API a_hAPI)
2007 {
2008         EM_DEBUG_FUNC_BEGIN();
2009         int buffer_size = 0;
2010         int local_result = 0;
2011         int handle = 0;
2012         char* stream = NULL;
2013         email_account_t *account = NULL;
2014         email_account_t *ref_account = NULL;
2015         int ref_check_interval = 0;
2016         int ref_account_id = 0;
2017         int err = EMAIL_ERROR_NONE;
2018
2019         /* get account info */
2020         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2021         EM_DEBUG_LOG("size [%d]", buffer_size);
2022         if(buffer_size <= 0) {
2023                 err = EMAIL_ERROR_INVALID_PARAM;
2024                 goto FINISH_OFF;
2025         }
2026
2027         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
2028         if(!stream) {
2029                 err = EMAIL_ERROR_INVALID_PARAM;
2030                 goto FINISH_OFF;
2031         }
2032
2033         account = em_malloc(sizeof(email_account_t));
2034
2035         if(account == NULL) {
2036                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2037                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2038                 goto FINISH_OFF;
2039         }
2040
2041         em_convert_byte_stream_to_account(stream, buffer_size, account);
2042
2043         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
2044                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
2045                 goto FINISH_OFF;
2046         }
2047
2048         ref_account = emcore_get_account_reference(account->account_id);
2049
2050         if (!ref_account) {
2051                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
2052                 goto FINISH_OFF;
2053         }
2054
2055         ref_check_interval = ref_account->check_interval;
2056         ref_account_id = ref_account->account_id;
2057
2058         if(!emdaemon_validate_account_and_create(ref_account, &handle, &err)) {
2059                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_create fail [%d]", err);
2060                 goto FINISH_OFF;
2061         }
2062 #ifdef __FEATURE_AUTO_POLLING__
2063         /*  start auto polling, if check_interval not zero */
2064         if(ref_check_interval > 0) {
2065                 if(!emdaemon_add_polling_alarm(ref_account_id, ref_check_interval))
2066                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
2067         }
2068 #endif /*  __FEATURE_AUTO_POLLING__ */
2069         /*  add account details to contact DB  */
2070         /*  emdaemon_insert_accountinfo_to_contact(account); */
2071
2072         local_result = 1;
2073         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2074                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2075         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2076                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2077         if (!emipc_execute_stub_api(a_hAPI))
2078                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2079
2080 FINISH_OFF:
2081         if ( local_result == 0 ) { /* there is an error */
2082                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2083                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
2084                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2085                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
2086                 if (!emipc_execute_stub_api(a_hAPI))
2087                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2088         }
2089
2090 /*      note: account is freed in thread_func_branch_command, which is run by other thread */
2091 /*      emcore_free_account(account); */
2092
2093         EM_DEBUG_FUNC_END();
2094 }
2095
2096 void stb_backup_account(HIPC_API a_hAPI)
2097 {
2098         EM_DEBUG_FUNC_BEGIN();
2099
2100 #ifdef __FEATURE_BACKUP_ACCOUNT__
2101         char *file_path = NULL;
2102         int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
2103
2104         /* file_path_length */
2105         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2106         if(file_path_length > 0) {
2107                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2108                 file_path = em_malloc(file_path_length);
2109                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2110                 EM_DEBUG_LOG("file_path [%s]", file_path);
2111                 local_result = emcore_backup_accounts((const char*)file_path, &err_code);
2112         }
2113
2114         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2115                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2116
2117         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2118                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2119
2120         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2121                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2122
2123         if (!emipc_execute_stub_api(a_hAPI))
2124                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2125
2126         EM_SAFE_FREE(file_path);
2127 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2128         EM_DEBUG_FUNC_END();
2129 }
2130
2131 void stb_restore_account(HIPC_API a_hAPI)
2132 {
2133         EM_DEBUG_FUNC_BEGIN();
2134 #ifdef __FEATURE_BACKUP_ACCOUNT__
2135         char *file_path = NULL;
2136         int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
2137
2138         /* file_path_length */
2139         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2140         if(file_path_length > 0)  {
2141                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2142                 file_path = em_malloc(file_path_length);
2143                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2144                 EM_DEBUG_LOG("file_path [%s]", file_path);
2145                 local_result = emcore_restore_accounts((const char*)file_path, &err_code);
2146         }
2147
2148         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2149                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2150
2151         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2152                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2153
2154         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2155                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2156
2157         if (!emipc_execute_stub_api(a_hAPI))
2158                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2159
2160         EM_SAFE_FREE(file_path);
2161 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2162         EM_DEBUG_FUNC_END();
2163 }
2164
2165
2166 void stb_get_password_length(HIPC_API a_hAPI)
2167 {
2168         EM_DEBUG_FUNC_BEGIN();
2169         int account_id = 0;
2170         int local_result = 0;
2171         int err_code = 0;
2172         int password_length = 0;
2173
2174         /* account_id */
2175         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2176         local_result = emstorage_get_password_length_of_account(account_id, &password_length,&err_code);
2177
2178         EM_DEBUG_LOG("password_length [%d]", password_length);
2179
2180         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2181                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2182
2183         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2184                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2185
2186         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &password_length, sizeof(int)))
2187                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2188
2189         if (!emipc_execute_stub_api(a_hAPI))
2190                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2191         EM_DEBUG_FUNC_END();
2192 }
2193
2194 void stb_add_certificate(HIPC_API a_hAPI)
2195 {
2196         int err = EMAIL_ERROR_NONE;
2197         int cert_file_len = 0;
2198         int email_address_len = 0;
2199         char *cert_file_path = NULL;
2200         char *email_address = NULL;
2201
2202         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2203         if (cert_file_len > 0) {
2204                 cert_file_path = em_malloc(cert_file_len + 1);
2205                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2206         }
2207
2208         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2209         if (email_address_len > 0) {
2210                 email_address = em_malloc(email_address_len + 1);
2211                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, email_address_len, email_address);
2212         }
2213
2214         if (!emcore_add_public_certificate(cert_file_path, email_address, &err)) {
2215                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2216         }
2217
2218         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2219                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2220
2221         if (EMAIL_ERROR_NONE == err) {
2222                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2223         }
2224
2225         if (!emipc_execute_stub_api(a_hAPI))
2226                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2227
2228         EM_SAFE_FREE(cert_file_path);          
2229         EM_SAFE_FREE(email_address); 
2230         EM_DEBUG_FUNC_END();    
2231 }
2232
2233 void stb_delete_certificate(HIPC_API a_hAPI)
2234 {
2235         int err = EMAIL_ERROR_NONE;
2236         int email_address_len = 0;
2237         char *email_address = NULL;
2238         char temp_email_address[130] = {0, };
2239
2240         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2241         if (email_address_len > 0) {
2242                 EM_DEBUG_LOG("email address string length [%d]", email_address_len);
2243                 email_address = em_malloc(email_address_len + 1);
2244                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, email_address_len, email_address);
2245                 EM_DEBUG_LOG("email address [%s]", email_address);
2246         }
2247
2248         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
2249         if (!emcore_delete_public_certificate(temp_email_address, &err)) {
2250                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2251         }
2252
2253         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2254                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2255
2256         if (EMAIL_ERROR_NONE == err) {
2257                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2258         }
2259
2260         if (!emipc_execute_stub_api(a_hAPI))
2261                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2262
2263         
2264         EM_SAFE_FREE(email_address);
2265         EM_DEBUG_FUNC_END();    
2266 }
2267
2268 void stb_verify_signature(HIPC_API a_hAPI)
2269 {
2270         int err = EMAIL_ERROR_NONE;
2271         int verify = 0;
2272         int mail_id = 0;
2273         int count = 0;
2274         int attachment_tbl_count = 0;
2275         email_mail_data_t *mail_data = NULL;
2276         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2277
2278         err = emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2279         if (err != EMAIL_ERROR_NONE) {
2280                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
2281                 goto FINISH_OFF;
2282         }
2283
2284         if (!emcore_get_mail_data(mail_id, &mail_data)) {
2285                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
2286                 goto FINISH_OFF;
2287         }
2288
2289         if ((err = emstorage_get_attachment_list(mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
2290                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed : [%d]", err);
2291                 goto FINISH_OFF;
2292         }
2293
2294         if (attachment_tbl_count <= 0) {
2295                 EM_DEBUG_EXCEPTION("Invalid signed mail");
2296                 err = EMAIL_ERROR_INVALID_MAIL;
2297                 goto FINISH_OFF;
2298         }
2299
2300         for (count = 0; count < attachment_tbl_count; count++) {
2301                 if (strcasestr(attachment_tbl_list[count].attachment_name, "p7s") == NULL)
2302                         continue;
2303
2304                 break;  
2305         }
2306
2307         if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
2308                 EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
2309                 goto FINISH_OFF;
2310         }
2311
2312 FINISH_OFF:
2313
2314         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
2315                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2316
2317         if (verify) {
2318                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
2319         }
2320
2321         if (!emipc_execute_stub_api(a_hAPI))
2322                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2323
2324
2325         if (attachment_tbl_list)
2326                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2327
2328         if (mail_data) {
2329                 emcore_free_mail_data(mail_data);
2330                 EM_SAFE_FREE(mail_data);
2331         }
2332
2333         EM_DEBUG_FUNC_END();
2334 }
2335
2336 void stb_verify_certificate(HIPC_API a_hAPI)
2337 {
2338         int err = EMAIL_ERROR_NONE;
2339         int verify = 0;
2340         int cert_file_len = 0;
2341         char *cert_file_path = 0;
2342
2343         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2344         if (cert_file_len > 0) {
2345                 cert_file_path = em_malloc(cert_file_len + 1);
2346                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2347         }
2348
2349         if (!emcore_verify_certificate(cert_file_path, &verify, &err)) {
2350                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2351         }
2352
2353         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
2354                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2355
2356         if (verify) {
2357                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
2358         }
2359
2360         if (!emipc_execute_stub_api(a_hAPI))
2361                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2362
2363         EM_SAFE_FREE(cert_file_path);           
2364         EM_DEBUG_FUNC_END();    
2365 }
2366
2367 void stb_ping_service(HIPC_API a_hAPI)
2368 {
2369         EM_DEBUG_FUNC_BEGIN();
2370         int err = EMAIL_ERROR_NONE;
2371         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2372                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2373
2374         if (!emipc_execute_stub_api(a_hAPI))
2375                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");  
2376         EM_DEBUG_FUNC_END();
2377 }
2378
2379 void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
2380 {
2381         EM_DEBUG_FUNC_BEGIN();
2382         int err = EMAIL_ERROR_NONE, account_id;
2383
2384         /* account_id */
2385         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2386         EM_DEBUG_LOG("account_id [%d]", account_id);
2387
2388         if(!emcore_finalize_sync(account_id, &err)) {
2389                 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
2390         }
2391
2392         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2393                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2394
2395         if (!emipc_execute_stub_api(a_hAPI))
2396                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2397         EM_DEBUG_FUNC_END();
2398 }
2399
2400 void stb_show_user_message(HIPC_API a_hAPI)
2401 {
2402         EM_DEBUG_FUNC_BEGIN();
2403         int err = EMAIL_ERROR_NONE;
2404         int param_id = 0;
2405         int param_error = 0;
2406         email_action_t param_action = 0;
2407
2408         /* param_id */
2409         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &param_id);
2410         EM_DEBUG_LOG("param_id [%d]", param_id);
2411
2412         /* param_action */
2413         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &param_action);
2414         EM_DEBUG_LOG("param_action [%d]", param_action);
2415
2416         /* param_error */
2417         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &param_error);
2418         EM_DEBUG_LOG("param_error [%d]", param_error);
2419
2420         if( (err = emcore_show_user_message(param_id, param_action, param_error)) != EMAIL_ERROR_NONE) {
2421                 EM_DEBUG_EXCEPTION("emcore_show_user_message failed [%d]", err);
2422         }
2423
2424         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2425                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2426
2427         if (!emipc_execute_stub_api(a_hAPI))
2428                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2429
2430         EM_DEBUG_FUNC_END("err [%d]", err);
2431 }
2432
2433 void stb_write_mime_file(HIPC_API a_hAPI)
2434 {
2435         EM_DEBUG_FUNC_BEGIN();
2436         int i = 0;
2437         int exist_file_path = 0;
2438         int buffer_size = 0;
2439         int local_result = 0;
2440         int result_attachment_data_count = 0;
2441         int param_index = 0;
2442         int err = EMAIL_ERROR_NONE;
2443         int ret = EMAIL_ERROR_NONE;
2444         char *file_path = NULL;
2445         char *output_file_path = NULL;
2446         email_mail_data_t result_mail_data = {0};
2447         email_attachment_data_t *result_attachment_data = NULL;
2448         emstorage_mail_tbl_t *input_mail_tbl_data = NULL;
2449         emstorage_attachment_tbl_t *input_attachment_tbl_data = NULL;
2450         
2451         /* email_mail_data_t */;
2452         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2453         EM_DEBUG_LOG("email_mail_data_t buffer_size[%d]", buffer_size);
2454
2455         /* mail_data */
2456         if(buffer_size > 0)      {
2457                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2458                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
2459         }
2460
2461         /* attachment */
2462         if (result_mail_data.attachment_count > 0) {
2463                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2464                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
2465
2466                 if(buffer_size > 0) {
2467                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
2468                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
2469
2470                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
2471
2472                         if(result_attachment_data_count && !result_attachment_data) {
2473                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
2474                                 err = EMAIL_ERROR_ON_PARSING;
2475                                 goto FINISH_OFF;
2476                         }
2477                 }
2478
2479                 param_index++;
2480         }
2481
2482         
2483         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2484         if (buffer_size > 0) {
2485                 output_file_path = (char *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
2486                 EM_DEBUG_LOG("output_file_path : [%s]", output_file_path);
2487         }
2488
2489         /* Convert mail_data to mail_tbl */
2490         if (!em_convert_mail_data_to_mail_tbl(&result_mail_data, 1, &input_mail_tbl_data, &err)) {
2491                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed");
2492                 goto FINISH_OFF;
2493         }
2494
2495         /* Convert attachment_data to attachment_tbl */
2496         if (result_attachment_data_count > 0) {
2497                 input_attachment_tbl_data = (emstorage_attachment_tbl_t *)em_malloc(sizeof(emstorage_attachment_tbl_t) * result_attachment_data_count);
2498                 if (input_attachment_tbl_data == NULL) {
2499                         EM_DEBUG_EXCEPTION("em_malloc failed");
2500                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2501                         goto FINISH_OFF;
2502                 }
2503         }
2504
2505         for (i = 0; i < result_attachment_data_count ; i++) {
2506                 input_attachment_tbl_data[i].attachment_id = result_attachment_data[i].attachment_id;
2507                 input_attachment_tbl_data[i].attachment_name = EM_SAFE_STRDUP(result_attachment_data[i].attachment_name);
2508                 input_attachment_tbl_data[i].attachment_path = EM_SAFE_STRDUP(result_attachment_data[i].attachment_path);
2509                 input_attachment_tbl_data[i].attachment_size = result_attachment_data[i].attachment_size;
2510                 input_attachment_tbl_data[i].mail_id = result_attachment_data[i].mail_id;
2511                 input_attachment_tbl_data[i].account_id = result_attachment_data[i].account_id;
2512                 input_attachment_tbl_data[i].mailbox_id = result_attachment_data[i].mailbox_id;
2513                 input_attachment_tbl_data[i].attachment_save_status = result_attachment_data[i].save_status;
2514                 input_attachment_tbl_data[i].attachment_drm_type = result_attachment_data[i].drm_status;
2515                 input_attachment_tbl_data[i].attachment_inline_content_status = result_attachment_data[i].inline_content_status;
2516                 input_attachment_tbl_data[i].attachment_mime_type = EM_SAFE_STRDUP(result_attachment_data[i].attachment_mime_type);
2517         }
2518
2519         if ((err = em_verify_email_address_of_mail_tbl(input_mail_tbl_data, false)) != EMAIL_ERROR_NONE) {
2520                 EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed : [%d]", err);
2521                 goto FINISH_OFF;
2522         }
2523
2524         if (!emcore_make_rfc822_file_from_mail(input_mail_tbl_data, input_attachment_tbl_data, result_attachment_data_count, NULL, &file_path, NULL, &err)) {
2525                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed");
2526                 goto FINISH_OFF;
2527         }
2528
2529         if (output_file_path) {
2530                 if (!emstorage_move_file(file_path, output_file_path, false, &err)) {
2531                         EM_DEBUG_EXCEPTION("emstorage_move_file failed : [%d]", err);
2532                         goto FINISH_OFF;
2533                 }
2534         } else {
2535                 exist_file_path = 1;
2536                 output_file_path = EM_SAFE_STRDUP(file_path);
2537         }
2538
2539
2540         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, (char *)output_file_path, EM_SAFE_STRLEN(output_file_path) + 1)) {
2541                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2542                 err = EMAIL_ERROR_NULL_VALUE;
2543                 goto FINISH_OFF;
2544         }
2545
2546         local_result = 1;
2547
2548 FINISH_OFF:
2549         if ( local_result == 0 ) {
2550                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2551                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2552         }
2553
2554         if (!emipc_execute_stub_api(a_hAPI))
2555                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2556
2557         emstorage_free_attachment(&input_attachment_tbl_data, result_attachment_data_count, &ret);
2558
2559         emstorage_free_mail(&input_mail_tbl_data, 1, &ret);
2560
2561         emcore_free_mail_data(&result_mail_data);
2562
2563         if(result_attachment_data)
2564                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
2565
2566         EM_SAFE_FREE(file_path);
2567         if (exist_file_path)
2568                 EM_SAFE_FREE(output_file_path);
2569
2570         em_flush_memory();
2571         EM_DEBUG_FUNC_END();
2572 }
2573
2574 void stb_handle_task(int task_type, HIPC_API a_hAPI)
2575 {
2576         EM_DEBUG_FUNC_BEGIN();
2577         int param_index = 0;
2578         int is_async_task = (task_type > EMAIL_ASYNC_TASK_BOUNDARY)?1:0;
2579         int err = EMAIL_ERROR_NONE;
2580         int task_id = 0;
2581         int task_parameter_length = 0;
2582         char *task_parameter = NULL;
2583
2584         /* task_parameter_length */;
2585         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2586         EM_DEBUG_LOG("task_parameter_length [%d]", task_parameter_length);
2587
2588         /* task_parameter */
2589         if(task_parameter_length > 0)    {
2590                 task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2591         }
2592
2593         if(is_async_task) {
2594                 /* add async task */
2595                 if((err = emcore_add_task_to_task_table(task_type, EMAIL_TASK_PRIORITY_MID, task_parameter, task_parameter_length, &task_id)) != EMAIL_ERROR_NONE) {
2596                         EM_DEBUG_EXCEPTION("emcore_add_task_to_task_pool failed [%d]", err);
2597                         goto FINISH_OFF;
2598                 }
2599         }
2600         else {
2601                 /* do sync task */
2602         }
2603
2604 FINISH_OFF:
2605         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2606                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2607
2608         if(is_async_task) {
2609                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2610                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2611         }
2612
2613         if (!emipc_execute_stub_api(a_hAPI))
2614                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2615
2616         em_flush_memory();
2617         EM_DEBUG_FUNC_END("err [%d]", err);
2618 }
2619
2620 void* thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void *input_param)
2621 {
2622         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2623         int err = EMAIL_ERROR_NONE;
2624         int err_for_signal = EMAIL_ERROR_NONE;
2625         int i = 0;
2626         int task_id = THREAD_SELF();
2627         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *task_param = input_param;
2628
2629         /* Send start signal */
2630         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)
2631                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2632
2633         for(i = 0; i < task_param->mail_id_count; i++) {
2634                 if((err = emcore_move_mail_to_another_account(task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {
2635                         EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);
2636                         goto FINISH_OFF;
2637                 }
2638
2639                 /* Send progress signal */
2640                 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)
2641                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2642         }
2643
2644 FINISH_OFF:
2645         /* Send finish signal */
2646         if(err == EMAIL_ERROR_NONE) {
2647                 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)
2648                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2649         }
2650         else {
2651                 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)
2652                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2653         }
2654
2655         /* Free task parameter */
2656         EM_SAFE_FREE(task_param->mail_id_array);
2657         EM_SAFE_FREE(task_param);
2658
2659         EM_DEBUG_FUNC_END("err [%d]", err);
2660         return SUCCESS;
2661 }
2662
2663 void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
2664 {
2665         EM_DEBUG_FUNC_BEGIN();
2666         int param_index = 0;
2667         int err = EMAIL_ERROR_NONE;
2668         int task_parameter_length = 0;
2669         int thread_error;
2670         thread_t task_id;
2671         char *task_parameter = NULL;
2672         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *decoded_parameter = NULL;
2673
2674         /* task_parameter_length */;
2675         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2676
2677         if(task_parameter_length <= 0) {
2678                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2679                 err = EMAIL_ERROR_INVALID_PARAM;
2680                 goto FINISH_OFF;
2681         }
2682
2683         /* task_parameter */
2684         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2685
2686         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) {
2687                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2688                 goto FINISH_OFF;
2689         }
2690
2691         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, (void*)decoded_parameter, thread_error);
2692
2693         if(thread_error != 0) {
2694                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2695                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2696                 goto FINISH_OFF;
2697         }
2698
2699         THREAD_DETACH(task_id);
2700
2701 FINISH_OFF:
2702         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2703                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2704
2705         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2706                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2707
2708         if (!emipc_execute_stub_api(a_hAPI))
2709                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2710
2711         EM_DEBUG_FUNC_END("err [%d]", err);
2712 }
2713
2714 void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
2715 {
2716         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2717         int err = EMAIL_ERROR_NONE;
2718         int err_for_signal = EMAIL_ERROR_NONE;
2719         int task_id = THREAD_SELF();
2720         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *task_param = input_param;
2721
2722         /* Send start signal */
2723         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)
2724                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2725
2726         if((err = emcore_delete_mailbox_ex(task_param->account_id, task_param->mailbox_id_array, task_param->mailbox_id_count, task_param->on_server)) != EMAIL_ERROR_NONE) {
2727                 EM_DEBUG_EXCEPTION("emcore_delete_mailbox_ex failed[%d]", err);
2728                 goto FINISH_OFF;
2729         }
2730
2731 FINISH_OFF:
2732         /* Send finish signal */
2733         if(err == EMAIL_ERROR_NONE) {
2734                 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)
2735                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2736         }
2737         else {
2738                 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)
2739                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2740         }
2741
2742         /* Free task parameter */
2743         EM_SAFE_FREE(task_param->mailbox_id_array);
2744         EM_SAFE_FREE(task_param);
2745
2746         EM_DEBUG_FUNC_END("err [%d]", err);
2747         return SUCCESS;
2748 }
2749
2750 void stb_delete_mailbox_ex(HIPC_API a_hAPI)
2751 {
2752         EM_DEBUG_FUNC_BEGIN();
2753         int param_index = 0;
2754         int err = EMAIL_ERROR_NONE;
2755         int task_parameter_length = 0;
2756         int thread_error = 0;
2757         thread_t task_id;
2758         char *task_parameter = NULL;
2759         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *decoded_parameter = NULL;
2760
2761         /* task_parameter_length */;
2762         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2763
2764         if(task_parameter_length <= 0) {
2765                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2766                 err = EMAIL_ERROR_INVALID_PARAM;
2767                 goto FINISH_OFF;
2768         }
2769
2770         /* task_parameter */
2771         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2772
2773         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
2774                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2775                 goto FINISH_OFF;
2776         }
2777
2778         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, (void*)decoded_parameter, thread_error);
2779
2780         if(thread_error != 0) {
2781                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2782                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2783                 goto FINISH_OFF;
2784         }
2785
2786         THREAD_DETACH(task_id);
2787
2788 FINISH_OFF:
2789         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2790                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2791
2792         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2793                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2794
2795         if (!emipc_execute_stub_api(a_hAPI))
2796                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2797
2798         EM_DEBUG_FUNC_END("err [%d]", err);
2799 }
2800
2801 void* thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(void *input_param)
2802 {
2803         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2804         int err = EMAIL_ERROR_NONE;
2805         int err_for_signal = EMAIL_ERROR_NONE;
2806         int task_id = THREAD_SELF();
2807         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *task_param = input_param;
2808
2809         /* Send start signal */
2810         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)
2811                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2812
2813         if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->mail_id)) != EMAIL_ERROR_NONE) {
2814                 EM_DEBUG_EXCEPTION("emcore_send_mail_with_downloading_attachment_of_original_mail failed [%d]", err);
2815                 goto FINISH_OFF;
2816         }
2817
2818 FINISH_OFF:
2819         /* Send finish signal */
2820         if(err == EMAIL_ERROR_NONE) {
2821                 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)
2822                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2823         }
2824         else {
2825                 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)
2826                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2827         }
2828
2829         /* Free task parameter */
2830         EM_SAFE_FREE(task_param);
2831
2832         EM_DEBUG_FUNC_END("err [%d]", err);
2833         return SUCCESS;
2834 }
2835
2836 void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
2837 {
2838         EM_DEBUG_FUNC_BEGIN();
2839         int param_index = 0;
2840         int err = EMAIL_ERROR_NONE;
2841         int task_parameter_length = 0;
2842         int thread_error = 0;
2843         thread_t task_id;
2844         char *task_parameter = NULL;
2845         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *decoded_parameter = NULL;
2846
2847         /* task_parameter_length */;
2848         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2849
2850         if(task_parameter_length <= 0) {
2851                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2852                 err = EMAIL_ERROR_INVALID_PARAM;
2853                 goto FINISH_OFF;
2854         }
2855
2856         /* task_parameter */
2857         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2858
2859         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) {
2860                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2861                 goto FINISH_OFF;
2862         }
2863
2864         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, (void*)decoded_parameter, thread_error);
2865
2866         if(thread_error != 0) {
2867                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2868                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2869                 goto FINISH_OFF;
2870         }
2871
2872         THREAD_DETACH(task_id);
2873
2874 FINISH_OFF:
2875         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2876                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2877
2878         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2879                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2880
2881         if (!emipc_execute_stub_api(a_hAPI))
2882                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2883
2884         EM_DEBUG_FUNC_END("err [%d]", err);
2885 }
2886
2887 void stb_API_mapper(HIPC_API a_hAPI)
2888 {
2889         EM_DEBUG_FUNC_BEGIN();
2890         int nAPIID = emipc_get_api_id(a_hAPI);
2891
2892         switch(nAPIID) {
2893                 case _EMAIL_API_ADD_ACCOUNT:
2894                         stb_create_account(a_hAPI);
2895                         break;
2896
2897                 case _EMAIL_API_ADD_MAILBOX:
2898                         stb_create_mailbox(a_hAPI);
2899                         break;
2900
2901                 case _EMAIL_API_DELETE_MAILBOX:
2902                         stb_delete_mailbox(a_hAPI);
2903                         break;
2904
2905                 case _EMAIL_API_SET_MAILBOX_TYPE:
2906                         stb_set_mailbox_type(a_hAPI);
2907                         break;
2908
2909                 case _EMAIL_API_SET_LOCAL_MAILBOX:
2910                         stb_set_local_mailbox(a_hAPI);
2911                         break;
2912
2913                 case _EMAIL_API_SET_MAIL_SLOT_SIZE:
2914                         stb_set_mail_slot_size_of_mailbox(a_hAPI);
2915                         break;
2916
2917                 case _EMAIL_API_RENAME_MAILBOX:
2918                         stb_rename_mailbox(a_hAPI);
2919                         break;
2920
2921                 case _EMAIL_API_SEND_MAIL:
2922                         stb_send_mail(a_hAPI);
2923                         break;
2924
2925                 case _EMAIL_API_GET_MAILBOX_LIST:
2926                         stb_get_mailbox_list(a_hAPI);
2927                         break;
2928
2929                 case _EMAIL_API_SYNC_HEADER:
2930                         stb_sync_header(a_hAPI);
2931                         break;
2932
2933                 case _EMAIL_API_DOWNLOAD_BODY:
2934                         stb_download_body(a_hAPI);
2935                         break;
2936
2937                 case _EMAIL_API_CLEAR_DATA:
2938                         stb_clear_mail_data (a_hAPI);
2939                         break;
2940
2941                 case _EMAIL_API_DELETE_ALL_MAIL:
2942                         stb_delete_all_mails(a_hAPI);
2943                         break;
2944
2945                 case _EMAIL_API_DELETE_MAIL:
2946                         stb_delete_mail(a_hAPI);
2947                         break;
2948
2949                 case _EMAIL_API_ADD_RULE:
2950                         stb_add_rule(a_hAPI);
2951                         break;
2952
2953                 case _EMAIL_API_GET_RULE:
2954                         stb_get_rule(a_hAPI);
2955                         break;
2956
2957                 case _EMAIL_API_GET_RULE_LIST:
2958                         stb_get_rule_list(a_hAPI);
2959                         break;
2960
2961                 case _EMAIL_API_FIND_RULE:
2962                         stb_find_rule(a_hAPI);
2963                         break;
2964
2965                 case _EMAIL_API_UPDATE_RULE:
2966                         stb_update_rule(a_hAPI);
2967                         break;
2968
2969                 case _EMAIL_API_DELETE_RULE:
2970                         stb_delete_rule(a_hAPI);
2971                         break;
2972
2973                 case _EMAIL_API_MOVE_MAIL:
2974                         stb_move_mail(a_hAPI);
2975                         break;
2976
2977                 case _EMAIL_API_MOVE_ALL_MAIL:
2978                         stb_move_all_mails(a_hAPI);
2979                         break;
2980
2981                 case _EMAIL_API_SET_FLAGS_FIELD:
2982                         stb_set_flags_field(a_hAPI);
2983                         break;
2984
2985                 case _EMAIL_API_ADD_MAIL:
2986                         stb_add_mail(a_hAPI);
2987                         break;
2988
2989                 case _EMAIL_API_UPDATE_MAIL:
2990                         stb_update_mail(a_hAPI);
2991                         break;
2992
2993                 case _EMAIL_API_MOVE_THREAD_TO_MAILBOX:
2994                         stb_move_thread_to_mailbox(a_hAPI);
2995                         break;
2996
2997                 case _EMAIL_API_DELETE_THREAD:
2998                         stb_delete_thread(a_hAPI);
2999                         break;
3000
3001                 case _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD:
3002                         stb_modify_seen_flag_of_thread(a_hAPI);
3003                         break;
3004
3005                 case _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED:
3006                         stb_expunge_mails_deleted_flagged(a_hAPI);
3007                         break;
3008
3009                 case _EMAIL_API_UPDATE_MAIL_ATTRIBUTE:
3010                         stb_update_mail_attribute(a_hAPI);
3011                         break;
3012
3013                 case _EMAIL_API_DELETE_ACCOUNT:
3014                         stb_delete_account(a_hAPI);
3015                         break;
3016
3017                 case _EMAIL_API_UPDATE_ACCOUNT:
3018                         stb_update_account(a_hAPI);
3019                         break;
3020
3021                 case _EMAIL_API_ADD_ATTACHMENT:
3022                         stb_add_attachment(a_hAPI);
3023                         break;
3024
3025                 case _EMAIL_API_GET_IMAP_MAILBOX_LIST:
3026                         stb_get_imap_mailbox_list(a_hAPI);
3027                         break;
3028
3029                 case _EMAIL_API_GET_ATTACHMENT:
3030                         stb_get_attachment(a_hAPI);
3031                         break;
3032
3033                 case _EMAIL_API_DELETE_ATTACHMENT:
3034                         stb_delete_attachment(a_hAPI);
3035                         break;
3036
3037                 case _EMAIL_API_DOWNLOAD_ATTACHMENT:
3038                         stb_download_attachment(a_hAPI);
3039                         break;
3040
3041                 case _EMAIL_API_GET_ACCOUNT_LIST:
3042                         stb_get_account_list(a_hAPI);
3043                         break;
3044
3045                 case _EMAIL_API_SEND_SAVED:
3046                         stb_mail_send_saved(a_hAPI);
3047                         break;
3048
3049                 case _EMAIL_API_ADD_READ_RECEIPT:
3050                         stb_add_read_receipt(a_hAPI);
3051                         break;
3052
3053                 case _EMAIL_API_CANCEL_JOB:
3054                         stb_cancel_job(a_hAPI);
3055                         break;
3056
3057                 case _EMAIL_API_GET_PENDING_JOB:
3058                         stb_get_pending_job(a_hAPI);
3059                         break;
3060
3061                 case _EMAIL_API_NETWORK_GET_STATUS:
3062                         stb_get_event_queue_status(a_hAPI);
3063                         break;
3064
3065                 case _EMAIL_API_SEND_RETRY:
3066                         stb_retry_sending_mail(a_hAPI);
3067                         break;
3068
3069                 case _EMAIL_API_VALIDATE_ACCOUNT :
3070                         stb_validate_account(a_hAPI);
3071                         break;
3072
3073                 case _EMAIL_API_SEND_MAIL_CANCEL_JOB :
3074                         stb_cancel_send_mail_job(a_hAPI);
3075                         break;
3076
3077                 case _EMAIL_API_SEARCH_MAIL_ON_SERVER :
3078                         stb_search_mail_on_server(a_hAPI);
3079                         break;
3080
3081                 case _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER :
3082                         stb_clear_result_of_search_mail_on_server(a_hAPI);
3083                         break;
3084
3085                 case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :
3086                         stb_add_account_with_validation(a_hAPI);
3087                         break;
3088
3089                 case _EMAIL_API_BACKUP_ACCOUNTS:
3090                         stb_backup_account(a_hAPI);
3091                         break;
3092
3093                 case _EMAIL_API_RESTORE_ACCOUNTS:
3094                         stb_restore_account(a_hAPI);
3095                         break;
3096
3097                 case _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT:
3098                         stb_get_password_length(a_hAPI);
3099                         break;
3100
3101                 case _EMAIL_API_PRINT_RECEIVING_EVENT_QUEUE :
3102                         stb_print_receiving_event_queue_via_debug_msg(a_hAPI);
3103                         break;
3104
3105                 case _EMAIL_API_ADD_CERTIFICATE:
3106                         stb_add_certificate(a_hAPI);
3107                         break;
3108
3109                 case _EMAIL_API_DELETE_CERTIFICATE:
3110                         stb_delete_certificate(a_hAPI);
3111                         break;
3112
3113                 case _EMAIL_API_VERIFY_SIGNATURE:
3114                         stb_verify_signature(a_hAPI);
3115                         break;
3116
3117                 case _EMAIL_API_VERIFY_CERTIFICATE:
3118                         stb_verify_certificate(a_hAPI);
3119                         break;
3120
3121                 case _EMAIL_API_PING_SERVICE :
3122                         stb_ping_service(a_hAPI);
3123                         break;
3124
3125                 case _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL :
3126                         stb_update_notification_bar_for_unread_mail(a_hAPI);
3127                         break;
3128
3129                 case _EMAIL_API_SHOW_USER_MESSAGE :
3130                         stb_show_user_message(a_hAPI);
3131                         break;
3132
3133                 case _EMAIL_API_WRITE_MIME_FILE :
3134                         stb_write_mime_file(a_hAPI);
3135                         break;
3136
3137                 case EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT :
3138                         stb_move_mails_to_mailbox_of_another_account(a_hAPI);
3139                         break;
3140
3141                 case EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX :
3142                         stb_delete_mailbox_ex(a_hAPI);
3143                         break;
3144
3145                 case EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL :
3146                         stb_send_mail_with_downloading_attachment_of_original_mail(a_hAPI);
3147                         break;
3148         }
3149         EM_DEBUG_FUNC_END();
3150 }
3151
3152 GMainLoop *g_mainloop = NULL;
3153
3154 static void callback_for_sigterm(int signum)
3155 {
3156         EM_DEBUG_FUNC_BEGIN("signum [%d]", signum);
3157
3158         if (g_mainloop)
3159                 g_main_loop_quit(g_mainloop);
3160
3161         EM_DEBUG_FUNC_END();
3162 }
3163
3164 INTERNAL_FUNC int main(int argc, char *argv[])
3165 {
3166         /* Do the email-service Initialization 
3167        1. Create all DB tables and load the email engine */
3168         EM_DEBUG_LOG("Email service begin");
3169         int err = 0, ret;
3170         GMainLoop *mainloop;
3171
3172 #ifdef USE_OMA_EMN
3173         EM_DEBUG_LOG("emdaemon_initialize_emn Start");
3174         emdaemon_initialize_emn();
3175 #endif
3176
3177         EM_DEBUG_LOG("ipcEmailStub_Initialize Start");
3178         
3179         ret = emipc_initialize_stub(stb_API_mapper);
3180
3181         if(ret == true)
3182                 EM_DEBUG_LOG("ipcEmailStub_Initialize Success");
3183         else
3184                 EM_DEBUG_EXCEPTION("ipcEmailStub_Initialize failed");
3185
3186         signal(SIGPIPE, SIG_IGN);              /* to ignore signal 13(SIGPIPE) */
3187         signal(SIGTERM, callback_for_sigterm); /* to handle signal 15(SIGTERM) - power off case*/
3188
3189         emdaemon_initialize(&err);
3190
3191         /* Start auto polling */
3192 #ifdef __FEATURE_AUTO_POLLING__
3193         emdaemon_start_auto_polling(&err);
3194 #endif
3195
3196         mainloop = g_main_loop_new(NULL, 0);
3197         g_mainloop = mainloop;
3198
3199         g_type_init();
3200
3201         g_main_loop_run(mainloop);
3202         
3203         /* Clean up resources */
3204         g_main_loop_unref(mainloop);
3205
3206         g_mainloop = NULL;
3207
3208         emipc_finalize_stub();
3209
3210         emdaemon_finalize(NULL);
3211
3212         EM_DEBUG_FUNC_END();
3213         return 0;
3214 }
3215