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