upload tizen1.0 source
[framework/messaging/email-service.git] / email-api / email-api-network.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
24 /**
25  *
26  * This file contains the data structures and interfaces needed for application,
27  * to interact with email-service.
28  * @file                email-api-network.c
29  * @brief               This file contains the data structures and interfaces of Network related Functionality provided by 
30  *                      email-service . 
31  */
32  
33 #include "email-api.h"
34 #include "string.h"
35 #include "email-convert.h"
36 #include "email-api-mailbox.h"
37 #include "email-types.h"
38 #include "email-utilities.h"
39 #include "email-ipc.h"
40 #include "email-storage.h"
41
42 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
43 #include <vconf.h>
44 #include <dbus/dbus.h>
45 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
46
47 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
48
49
50 #define ACTIVE_SYNC_HANDLE_INIT_VALUE           (-1)
51 #define ACTIVE_SYNC_HANDLE_BOUNDARY                     (-100000000)
52
53
54 static int email_get_account_server_type_by_account_id(int account_id, emf_account_server_t* account_server_type, int flag, int *error)
55 {
56         EM_DEBUG_FUNC_BEGIN();
57         emf_account_t *account=NULL;
58         int ret = false;
59         int err= EMF_ERROR_NONE;
60
61         if (account_server_type == NULL ) {
62                 EM_DEBUG_EXCEPTION("account_server_type is NULL");
63                 err = EMF_ERROR_INVALID_PARAM;
64                 ret = false;
65                 goto FINISH_OFF;
66         }
67                 
68         if( (err = email_get_account(account_id,WITHOUT_OPTION,&account)) < 0) {
69                 EM_DEBUG_EXCEPTION ("email_get_account failed [%d] ", err);
70                 ret = false;
71                 goto FINISH_OFF;
72         }
73
74         if ( flag == false )  { /*  sending server */
75                 *account_server_type = account->sending_server_type;
76         } else if ( flag == true ) {    /*  receiving server */
77                 *account_server_type = account->receiving_server_type;
78         }
79
80         ret = true;
81
82 FINISH_OFF:
83         if ( account != NULL ) {
84                 email_free_account(&account, 1);
85         }
86         if ( error != NULL ) {
87                 *error = err;
88         }
89
90         return ret;
91 }
92
93 static int email_get_handle_for_activesync(int *handle, int *error)
94 {
95         EM_DEBUG_FUNC_BEGIN();
96
97         static int next_handle = 0;
98         int ret = false;
99         int err = EMF_ERROR_NONE;
100                         
101         if ( handle == NULL ) {
102                 EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed : handle is NULL");
103                 err = EMF_ERROR_INVALID_PARAM;
104                 goto FINISH_OFF;
105         }
106         
107         if ( vconf_get_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, &next_handle)  != 0 ) {
108                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
109                 if ( next_handle != 0 ) {
110                         err = EMF_ERROR_GCONF_FAILURE;
111                         goto FINISH_OFF;
112                 }
113         }
114
115         EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
116
117         /*  set the value of the handle for active sync */
118         next_handle--;
119         if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
120                 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
121         }
122         if ( vconf_set_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, next_handle) != 0) {
123                 EM_DEBUG_EXCEPTION("vconf_set_int failed");
124                 err = EMF_ERROR_GCONF_FAILURE;
125                 goto FINISH_OFF;
126         }
127         ret = true;
128         *handle = next_handle;
129         EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
130         
131 FINISH_OFF:
132         if ( error != NULL ) {
133                 *error = err;
134         }
135         
136         return ret;
137 }
138
139 static int email_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
140 {
141         EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
142
143         DBusConnection     *connection;
144         DBusMessage        *signal = NULL;
145         DBusError           error;
146         const char         *nullString = "";
147         int                 i = 0;
148         dbus_int32_t        array_for_time_type[9] = { 0 , };
149
150         dbus_error_init (&error);
151         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
152
153         if(connection == NULL)
154                 goto FINISH_OFF;
155
156         signal = dbus_message_new_signal("/User/Email/ActiveSync", EMF_ACTIVE_SYNC_NOTI, "email");
157
158         dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
159         switch ( subType ) {
160                 case ACTIVE_SYNC_NOTI_SEND_MAIL:
161                         EM_DEBUG_LOG("handle:[%d]", data->send_mail.handle);
162                         EM_DEBUG_LOG("account_id:[%d]", data->send_mail.account_id);
163                         EM_DEBUG_LOG("mailbox_name:[%s]", data->send_mail.mailbox_name);
164                         EM_DEBUG_LOG("mail_id:[%d]", data->send_mail.mail_id);
165                         EM_DEBUG_LOG("options.priority:[%d]", data->send_mail.options.priority);
166                         EM_DEBUG_LOG("options.keep_local_copy:[%d]", data->send_mail.options.keep_local_copy);
167                         EM_DEBUG_LOG("options.req_delivery_receipt:[%d]", data->send_mail.options.req_delivery_receipt);
168                         EM_DEBUG_LOG("options.req_read_receipt:[%d]", data->send_mail.options.req_read_receipt);
169                         /* download_limit, block_address, block_subject might not be needed */
170                         EM_DEBUG_LOG("options.download_limit:[%d]", data->send_mail.options.download_limit);
171                         EM_DEBUG_LOG("options.block_address:[%d]", data->send_mail.options.block_address);
172                         EM_DEBUG_LOG("options.block_subject:[%d]", data->send_mail.options.block_subject);
173                         EM_DEBUG_LOG("options.display_name_from:[%s]", data->send_mail.options.display_name_from);
174                         EM_DEBUG_LOG("options.reply_with_body:[%d]", data->send_mail.options.reply_with_body);
175                         EM_DEBUG_LOG("options.forward_with_files:[%d]", data->send_mail.options.forward_with_files);
176                         EM_DEBUG_LOG("options.add_myname_card:[%d]", data->send_mail.options.add_myname_card);
177                         EM_DEBUG_LOG("options.add_signature:[%d]", data->send_mail.options.add_signature);
178                         EM_DEBUG_LOG("options.signature:[%s]", data->send_mail.options.signature);
179
180                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.handle), DBUS_TYPE_INVALID);
181                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.account_id), DBUS_TYPE_INVALID);
182                         if ( data->send_mail.mailbox_name == NULL )
183                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
184                         else
185                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.mailbox_name), DBUS_TYPE_INVALID);
186                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.mail_id), DBUS_TYPE_INVALID);
187                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.priority), DBUS_TYPE_INVALID);
188                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.keep_local_copy), DBUS_TYPE_INVALID);
189                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.req_delivery_receipt), DBUS_TYPE_INVALID);
190                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.req_read_receipt), DBUS_TYPE_INVALID);
191                         if ( data->send_mail.options.display_name_from == NULL )
192                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
193                         else
194                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.options.display_name_from), DBUS_TYPE_INVALID);
195
196                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.reply_with_body), DBUS_TYPE_INVALID);
197                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.forward_with_files), DBUS_TYPE_INVALID);
198                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.add_myname_card), DBUS_TYPE_INVALID);
199                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.add_signature), DBUS_TYPE_INVALID);
200                         if ( data->send_mail.options.signature == NULL )
201                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
202                         else
203                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.options.signature), DBUS_TYPE_INVALID);
204
205                         break;
206                 case ACTIVE_SYNC_NOTI_SEND_SAVED:                               /*  publish a send notification to ASE (active sync engine) */
207                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_SAVED]", subType);
208                         break;
209                 case ACTIVE_SYNC_NOTI_SEND_REPORT:
210                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
211                         break;
212                 case ACTIVE_SYNC_NOTI_SYNC_HEADER:
213                         EM_DEBUG_LOG("handle:[%d]", data->sync_header.handle);
214                         EM_DEBUG_LOG("account_id:[%d]", data->sync_header.account_id);
215                         EM_DEBUG_LOG("mailbox_name:[%s]", data->sync_header.mailbox_name);
216                         
217                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.handle ), DBUS_TYPE_INVALID);
218                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.account_id ), DBUS_TYPE_INVALID);
219                         if ( data->sync_header.mailbox_name == NULL )
220                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
221                         else
222                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->sync_header.mailbox_name), DBUS_TYPE_INVALID);
223                         break;
224                 case ACTIVE_SYNC_NOTI_DOWNLOAD_BODY:                    /*  publish a download body notification to ASE */
225                         EM_DEBUG_LOG("handle:[%d]", data->download_body.handle);
226                         EM_DEBUG_LOG("account_id:[%d]", data->download_body.account_id);
227                         EM_DEBUG_LOG("mail_id:[%d]", data->download_body.mail_id);
228                         EM_DEBUG_LOG("with_attachment:[%d]", data->download_body.with_attachment);
229                         
230                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.handle  ), DBUS_TYPE_INVALID);
231                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.account_id  ), DBUS_TYPE_INVALID);
232                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.mail_id ), DBUS_TYPE_INVALID);
233                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.with_attachment  ), DBUS_TYPE_INVALID);
234                         break;
235                 case ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT:
236                         EM_DEBUG_LOG("handle:[%d]", data->download_attachment.handle);
237                         EM_DEBUG_LOG("account_id:[%d]", data->download_attachment.account_id );
238                         EM_DEBUG_LOG("mail_id:[%d]", data->download_attachment.mail_id);
239                         EM_DEBUG_LOG("with_attachment:[%s]", data->download_attachment.attachment_order );
240
241                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.handle  ), DBUS_TYPE_INVALID);
242                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.account_id  ), DBUS_TYPE_INVALID);
243                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.mail_id ), DBUS_TYPE_INVALID);
244                         if ( data->download_attachment.attachment_order == NULL )
245                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
246                         else
247                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->download_attachment.attachment_order), DBUS_TYPE_INVALID);
248                         break;
249                 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
250                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
251                         break;
252                 case ACTIVE_SYNC_NOTI_CANCEL_JOB:
253                         EM_DEBUG_LOG("account_id:[%d]",       data->cancel_job.account_id );
254                         EM_DEBUG_LOG("handle to cancel:[%d]", data->cancel_job.handle);
255
256                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.account_id  ), DBUS_TYPE_INVALID);
257                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.handle  ), DBUS_TYPE_INVALID);
258                         break;
259                 case ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER:
260                         EM_DEBUG_LOG("account_id:[%d]",          data->search_mail_on_server.account_id );
261                         EM_DEBUG_LOG("mailbox_name:[%s]",        data->search_mail_on_server.mailbox_name );
262                         EM_DEBUG_LOG("search_filter_count:[%d]", data->search_mail_on_server.search_filter_count );
263                         EM_DEBUG_LOG("handle to cancel:[%d]",    data->search_mail_on_server.handle);
264
265                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
266                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.mailbox_name), DBUS_TYPE_INVALID);
267                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.search_filter_count), DBUS_TYPE_INVALID);
268                         for(i = 0; i < data->search_mail_on_server.search_filter_count; i++) {
269                                 dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.search_filter_list[i].search_filter_type), DBUS_TYPE_INVALID);
270                                 switch(data->search_mail_on_server.search_filter_list[i].search_filter_type) {
271                                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
272                                         case EMAIL_SEARCH_FILTER_TYPE_UID              :
273                                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
274                                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
275                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
276                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
277                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
278                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
279                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
280                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
281                                                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.integer_type_key_value), DBUS_TYPE_INVALID);
282                                                 break;
283
284                                         case EMAIL_SEARCH_FILTER_TYPE_BCC              :
285                                         case EMAIL_SEARCH_FILTER_TYPE_CC               :
286                                         case EMAIL_SEARCH_FILTER_TYPE_FROM             :
287                                         case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
288                                         case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
289                                         case EMAIL_SEARCH_FILTER_TYPE_TO               :
290                                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
291                                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.string_type_key_value), DBUS_TYPE_INVALID);
292                                                 break;
293
294                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
295                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
296                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  : {
297                                                         array_for_time_type[0] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_sec;
298                                                         array_for_time_type[1] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_min;
299                                                         array_for_time_type[2] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_hour;
300                                                         array_for_time_type[3] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_mday;
301                                                         array_for_time_type[4] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_mon;
302                                                         array_for_time_type[5] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_year;
303                                                         array_for_time_type[6] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_wday;
304                                                         array_for_time_type[7] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_yday;
305                                                         array_for_time_type[8] = data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value.tm_isdst;
306                                                         dbus_message_append_args(signal, DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, array_for_time_type, 9, DBUS_TYPE_INVALID);
307                                                 }
308                                                 break;
309                                         default :
310                                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
311                                                 break;
312                                 }
313                         }
314                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
315                         break;
316                 default:
317                         EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
318                         return FAILURE;
319         }
320
321         if(!dbus_connection_send (connection, signal, NULL)) {
322                 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
323                 return FAILURE;
324         } else
325                 EM_DEBUG_LOG("dbus_connection_send is successful");
326
327         dbus_connection_flush(connection);
328
329 FINISH_OFF:
330         
331         if(signal)
332                 dbus_message_unref(signal);
333
334         EM_DEBUG_FUNC_END();
335         return true;
336 }
337 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
338
339
340 EXPORT_API int email_send_mail( emf_mailbox_t* mailbox, int mail_id, emf_option_t* sending_option, unsigned* handle)
341 {
342         EM_DEBUG_FUNC_BEGIN("mailbox[%p], mail_id[%d], sending_option[%p], handle[%p]", mailbox, mail_id, sending_option, handle);
343         
344         char* mailbox_stream = NULL;
345         char* pSendingOption = NULL;
346         int size = 0;
347         int err = EMF_ERROR_NONE;
348                 
349         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);
350         EM_IF_NULL_RETURN_VALUE(mailbox->name, EMF_ERROR_INVALID_PARAM);
351         EM_IF_ACCOUNT_ID_NULL(mailbox->account_id, EMF_ERROR_INVALID_PARAM);
352
353         EM_DEBUG_LOG("Account ID [ %d],mailbox->name[%s], mailbox->account_id[%d] ", mailbox->account_id, mailbox->name, mailbox->account_id);
354
355 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
356         emf_account_server_t account_server_type;
357         HIPC_API hAPI = NULL;
358         ASNotiData as_noti_data;
359         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
360
361         /*  check account bind type and branch off  */
362         if ( email_get_account_server_type_by_account_id(mailbox->account_id, &account_server_type, false, &err) == false ) {
363                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
364                 err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
365                 goto FINISH_OFF;
366         }
367
368         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
369                 int as_handle;
370                 if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
371                         EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
372                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
373                         goto FINISH_OFF;
374                 }
375                 
376                 /*  noti to active sync */
377                 as_noti_data.send_mail.handle = as_handle;
378                 as_noti_data.send_mail.account_id = mailbox->account_id;
379                 as_noti_data.send_mail.mailbox_name = strdup(mailbox->name);
380                 as_noti_data.send_mail.mail_id = mail_id;
381
382                 memcpy(&as_noti_data.send_mail.options, sending_option, sizeof(emf_option_t));
383
384                 if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEND_MAIL, &as_noti_data) == false) {
385                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
386                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
387                         goto FINISH_OFF;
388                 }
389
390                 if(handle)
391                         *handle = as_handle;
392         } else {                
393                 hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL);    
394
395                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
396
397                 /* Mailbox */
398                 mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
399
400                 EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
401
402                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)){
403                         EM_DEBUG_EXCEPTION("email_send_mail--Add Param mailbox failed");
404                         EM_SAFE_FREE(mailbox_stream);
405                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
406                 }
407                 
408                 /* mail_id */
409                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))){
410                         EM_DEBUG_EXCEPTION("email_send_mail--Add Param mail_id failed");
411                         EM_SAFE_FREE(mailbox_stream);
412                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
413                 }
414
415                 /* sending options */
416                 pSendingOption = em_convert_option_to_byte_stream(sending_option, &size);
417
418                 if ( NULL == pSendingOption)     {
419                         EM_SAFE_FREE(mailbox_stream);
420                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
421                 }
422
423                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, pSendingOption, size)){
424                         EM_DEBUG_EXCEPTION("email_send_mail--Add Param Sending_Option failed  ");
425                         EM_SAFE_FREE(mailbox_stream);
426                         EM_SAFE_FREE(pSendingOption);
427                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
428                 }
429
430                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
431                         EM_DEBUG_EXCEPTION("email_send_mail--emipc_execute_proxy_api failed  ");
432                         EM_SAFE_FREE(pSendingOption); 
433                         EM_SAFE_FREE(mailbox_stream);
434                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_CRASH);
435                 }
436
437                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
438                 if (err == EMF_ERROR_NONE) {
439                         if(handle)
440                                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
441                 }
442         }
443
444 FINISH_OFF:
445         emipc_destroy_email_api(hAPI);
446         hAPI = (HIPC_API)NULL;
447         EM_SAFE_FREE(pSendingOption); 
448         EM_SAFE_FREE(mailbox_stream);
449         EM_SAFE_FREE(as_noti_data.send_mail.mailbox_name);
450 #else   /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
451         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_MAIL);   
452
453         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
454
455         /* Mailbox */
456         mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
457
458         EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
459
460         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)){
461                 EM_DEBUG_EXCEPTION("Add Param mailbox failed  ");
462                 EM_SAFE_FREE(mailbox_stream);
463                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
464         }
465         
466         /* mail_id */
467         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))){
468                 EM_DEBUG_EXCEPTION("Add Param mail_id failed  ");
469                 EM_SAFE_FREE(mailbox_stream);
470                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
471         }
472
473         /* sending options */
474         pSendingOption = em_convert_option_to_byte_stream(sending_option, &size);
475
476         if ( NULL == pSendingOption)
477                 EM_SAFE_FREE(mailbox_stream);
478                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
479         }
480
481         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, pSendingOption, size)){
482                 EM_DEBUG_EXCEPTION("Add Param Sending_Option failed  ");
483                 EM_SAFE_FREE(mailbox_stream);
484                 EM_SAFE_FREE(pSendingOption);
485                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
486         }
487
488         if(!emipc_execute_proxy_api(hAPI, NULL)) {
489                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed  ");
490                 EM_SAFE_FREE(pSendingOption);
491                 EM_SAFE_FREE(mailbox_stream);
492                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
493         }
494
495         err  emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), );
496         if (err == EMF_ERROR_NONE)
497         {
498                 if(handle)
499                         handle = *(unsigned int*)emipc_get_parameter(hAPI, ePARAMETER_OUT, 1);  /* Warning removal changes  */
500         }
501                 
502         EM_DEBUG_LOG(" >>>>>> ERROR CODE : %d ", err);
503
504         emipc_destroy_email_api(hAPI);
505         hAPI = NULL;
506         EM_SAFE_FREE(pSendingOption);
507         EM_SAFE_FREE(mailbox_stream);
508 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
509         EM_DEBUG_FUNC_END("err [%d]", err);  
510         return err;     
511 }
512
513 EXPORT_API int email_send_saved(int account_id, emf_option_t* sending_option, unsigned* handle)
514 {
515         EM_DEBUG_FUNC_BEGIN("account_id[%d], sending_option[%p], handle[%p]", account_id, sending_option, handle);
516         
517         char* pOptionStream = NULL;
518         int err = EMF_ERROR_NONE;
519         int size = 0;
520         
521         EM_IF_NULL_RETURN_VALUE(account_id, EMF_ERROR_INVALID_PARAM);
522         EM_IF_NULL_RETURN_VALUE(sending_option, EMF_ERROR_INVALID_PARAM);
523         EM_IF_ACCOUNT_ID_NULL(account_id, EMF_ERROR_INVALID_PARAM);
524         
525         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SEND_SAVED);
526
527         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
528         
529         /* Account ID */
530         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(account_id), sizeof(int))) {
531                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed");
532                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
533         }
534
535         /* Sending Option */
536         pOptionStream = em_convert_option_to_byte_stream(sending_option, &size);
537
538         EM_PROXY_IF_NULL_RETURN_VALUE(pOptionStream, hAPI, EMF_ERROR_NULL_VALUE);
539
540         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, pOptionStream, size)) {
541                 EM_DEBUG_EXCEPTION("Add Param sending option failed");
542                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
543         }
544
545         /* Execute API */
546         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
547                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
548                 EM_SAFE_FREE(pOptionStream);
549                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
550         }
551
552         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
553         emipc_destroy_email_api(hAPI);
554         
555         hAPI = NULL;
556         EM_SAFE_FREE(pOptionStream);
557
558         EM_DEBUG_FUNC_END("err [%d]", err);  
559         return err;
560 }
561
562 EXPORT_API int email_sync_local_activity(int account_id)
563 {
564         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
565         
566         int err = EMF_ERROR_NONE;
567
568         if (account_id < 0 || account_id == 0)  {               
569                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
570                 
571                 return EMF_ERROR_INVALID_PARAM;         
572         }
573         
574         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SYNC_LOCAL_ACTIVITY);
575
576         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
577
578         /* account_id */
579         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
580                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
581                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
582         }
583         
584         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
585                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
586                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
587         }
588
589         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
590
591         emipc_destroy_email_api(hAPI);
592
593         hAPI = NULL;
594         EM_DEBUG_FUNC_END("err [%d]", err);  
595         return err;
596 }
597
598 int EmfMailboxSyncHeader(emf_mailbox_t* mailbox, unsigned* handle, int* err_code)
599 {
600         EM_DEBUG_FUNC_BEGIN("mailbox[%p], handle[%p], err_code[%p]", mailbox, handle, err_code);
601         int err = EMF_ERROR_NONE;
602
603         err = email_sync_header(mailbox,handle);
604
605         if (err_code != NULL)
606                 *err_code = err;
607         
608         return (err >= 0);
609 }
610
611 EXPORT_API int email_sync_header(emf_mailbox_t* mailbox, unsigned* handle)
612 {
613         EM_DEBUG_FUNC_BEGIN("mailbox[%p], handle[%p]", mailbox, handle);
614         char* mailbox_stream = NULL;
615         int err = EMF_ERROR_NONE;       
616         int size = 0;
617         /* int total_count = 0; */
618         
619         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);
620         EM_IF_ACCOUNT_ID_NULL(mailbox->account_id, EMF_ERROR_INVALID_PARAM);
621
622 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
623         emf_account_server_t account_server_type;
624         HIPC_API hAPI = NULL;
625         ASNotiData as_noti_data;
626         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
627
628         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
629         if ( email_get_account_server_type_by_account_id(mailbox->account_id, &account_server_type, true, &err) == false ) {
630                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
631                 goto FINISH_OFF;
632         }
633
634         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
635                 int as_handle;
636                 if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
637                         EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
638                         goto FINISH_OFF;
639                 }
640                 
641                 /*  noti to active sync */
642                 as_noti_data.sync_header.handle = as_handle;
643                 as_noti_data.sync_header.account_id = mailbox->account_id;
644                 /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
645                 as_noti_data.sync_header.mailbox_name = (mailbox && mailbox->name) ? strdup(mailbox->name) : NULL;
646
647                 if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
648                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
649                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
650                         goto FINISH_OFF;
651                 }
652
653                 if(handle)
654                         *handle = as_handle;
655
656         }
657         else {
658                 hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);  
659
660                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
661
662                 mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
663
664                 EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
665
666                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
667                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
668                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
669                 }
670
671                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
672                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
673                         EM_SAFE_FREE(mailbox_stream);
674                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
675                 }
676                         
677                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
678
679                 if (err != EMF_ERROR_NONE)
680                         goto FINISH_OFF;
681
682                 if(handle)
683                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);      
684         }
685
686 FINISH_OFF:
687         emipc_destroy_email_api(hAPI);
688         hAPI = NULL;
689         EM_SAFE_FREE(mailbox_stream);
690         EM_SAFE_FREE(as_noti_data.sync_header.mailbox_name);
691 #else   /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
692         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER); 
693
694         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
695
696         mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
697
698         EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
699
700         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
701                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
702                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
703         }
704
705         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
706                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
707                 EM_SAFE_FREE(mailbox_stream);
708                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
709         }
710                 
711          emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
712
713          EM_DEBUG_LOG("RETURN VALUE : %d ", err);
714          
715           if (err != EMF_ERROR_NONE)
716                  goto FINISH_OFF;
717          
718          if(handle)
719                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);      
720 FINISH_OFF:
721         emipc_destroy_email_api(hAPI);
722         hAPI = NULL;
723         EM_SAFE_FREE(mailbox_stream);
724 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
725
726         EM_DEBUG_FUNC_END("err [%d]", err);  
727         return err;
728 }
729
730
731 EXPORT_API int email_sync_header_for_all_account(unsigned* handle)
732 {
733         EM_DEBUG_FUNC_BEGIN("handle[%p]", handle);
734         char* mailbox_stream = NULL;
735         emf_mailbox_t mailbox;
736         int err = EMF_ERROR_NONE;       
737         int size = 0;
738         HIPC_API hAPI = NULL;
739         int return_handle;
740 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
741         ASNotiData as_noti_data;
742         int i, account_count = 0;
743         emstorage_account_tbl_t *account_tbl_array = NULL;
744         int as_err;
745 #endif
746
747         hAPI = emipc_create_email_api(_EMAIL_API_SYNC_HEADER);  
748
749         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
750
751         mailbox.account_id = ALL_ACCOUNT;
752         mailbox.name = NULL;
753         mailbox.alias = NULL;
754         mailbox.account_name = NULL;
755
756         mailbox_stream = em_convert_mailbox_to_byte_stream(&mailbox, &size);
757
758         EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
759
760         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
761                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
762                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
763         }
764
765         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
766                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
767                 EM_SAFE_FREE(mailbox_stream);
768                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
769         }
770                 
771          emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
772
773          if (err != EMF_ERROR_NONE)
774                  goto FINISH_OFF;
775
776            emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), &return_handle);
777
778 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
779         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
780
781         /*  Get all accounts for sending notification to active sync engine. */
782         if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &as_err)) {
783                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ]  ", as_err);
784
785                 goto FINISH_OFF;
786         }
787
788         for(i = 0; i < account_count; i++) {
789                 if ( account_tbl_array[i].receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
790                         /*  use returned handle value for a active sync handle */
791                         /* int as_handle; */
792                         /*
793                         if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
794                                 EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
795                                 goto FINISH_OFF;
796                         }
797                         */
798                         
799                         /*  noti to active sync */
800                         as_noti_data.sync_header.handle = return_handle;
801                         as_noti_data.sync_header.account_id = account_tbl_array[i].account_id;
802                         /* In case that Mailbox is NULL,   SYNC ALL MAILBOX */
803                         as_noti_data.sync_header.mailbox_name = NULL;
804
805                         if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SYNC_HEADER, &as_noti_data) == false) {
806                                 EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
807                                 err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
808                                 goto FINISH_OFF;
809                         }
810                 }
811         }
812 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__ */
813
814          
815          if(handle)
816                 *handle = return_handle;
817
818 FINISH_OFF:
819
820         emipc_destroy_email_api(hAPI);
821         hAPI = NULL;
822         EM_SAFE_FREE(mailbox_stream);
823         if ( account_tbl_array )
824                 emstorage_free_account(&account_tbl_array, account_count, NULL);
825         
826         EM_DEBUG_FUNC_END("err [%d]", err);  
827         return err;
828 }
829
830 EXPORT_API int email_download_body(emf_mailbox_t* mailbox, int mail_id, int with_attachment, unsigned* handle)
831 {
832         EM_DEBUG_FUNC_BEGIN("mailbox[%p], mail_id[%d],with_attachment[%d]", mailbox, mail_id, with_attachment);
833         char* mailbox_stream = NULL;
834         int err = EMF_ERROR_NONE;
835         int size = 0;
836                 
837         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);
838         EM_IF_NULL_RETURN_VALUE(mail_id, EMF_ERROR_INVALID_PARAM);
839         EM_IF_ACCOUNT_ID_NULL(mailbox->account_id, EMF_ERROR_INVALID_PARAM);
840                 
841 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
842         emf_account_server_t account_server_type;
843         HIPC_API hAPI = NULL;
844         ASNotiData as_noti_data;
845         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
846
847         /*  2010/02/12 ch715.lee : check account bind type and branch off  */
848         if ( email_get_account_server_type_by_account_id(mailbox->account_id, &account_server_type, true, &err) == false ) {
849                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
850                 goto FINISH_OFF;
851         }
852
853         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
854                 int as_handle;
855                 if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
856                         EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
857                         goto FINISH_OFF;
858                 }
859                 
860                 /*  noti to active sync */
861                 as_noti_data.download_body.handle = as_handle;
862                 as_noti_data.download_body.account_id = mailbox->account_id;
863                 as_noti_data.download_body.mail_id = mail_id;
864                 as_noti_data.download_body.with_attachment = with_attachment;
865
866                 if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_BODY, &as_noti_data) == false) {
867                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
868                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
869                         goto FINISH_OFF;
870                 }
871
872                 if(handle)
873                         *handle = as_handle;
874         } else {
875                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_BODY);
876
877                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
878
879                 mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
880
881                 EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
882
883                 /* MailBox Information */
884                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
885                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
886                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
887                 }
888
889                 /* Mail Id */
890                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, sizeof(int))) {
891                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
892                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
893                 }
894
895                 /* with_attachment */
896                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&with_attachment, sizeof(int))) {
897                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
898                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
899                 }
900                 
901                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
902                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
903                         EM_SAFE_FREE(mailbox_stream);
904                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
905                 }
906                         
907                  emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
908                  if (err != EMF_ERROR_NONE)             
909                          goto FINISH_OFF;
910                  
911                  if(handle)     
912                  {
913                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
914                         EM_DEBUG_LOG("RETURN VALUE : %d  handle %d", err, *handle);
915
916                  }
917         }
918
919 FINISH_OFF:
920         emipc_destroy_email_api(hAPI);
921         hAPI = NULL;
922         EM_SAFE_FREE(mailbox_stream);
923 #else   /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
924         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_BODY);
925
926         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
927
928         mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
929
930         EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
931
932         /* MailBox Information */
933         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
934                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
935                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
936         }
937
938         /* Mail Id */
939         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&mail_id, size)) {
940                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
941                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
942         }
943
944         /* with_attachment */
945         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&with_attachment, size)) {
946                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
947                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
948         }
949         
950         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
951                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
952                 EM_SAFE_FREE(mailbox_stream);
953                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
954         }
955                 
956          emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);       
957          if (err != EMF_ERROR_NONE)      
958                  goto FINISH_OFF;
959          
960          /* Download handle */
961          if(handle) {
962                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
963                 EM_DEBUG_LOG("RETURN VALUE : %d handle %d", err, *handle);
964
965          }
966 FINISH_OFF:
967                         
968         emipc_destroy_email_api(hAPI);
969         hAPI = NULL;
970         EM_SAFE_FREE(mailbox_stream);
971
972 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
973
974         EM_DEBUG_FUNC_END("err [%d]", err);  
975         return err;
976
977 }
978
979
980
981
982
983 /* API - Downloads the Email Attachment Information [ INTERNAL ] */
984
985 EXPORT_API int email_download_attachment(emf_mailbox_t* mailbox,int mail_id, const char* nth,unsigned* handle)
986 {
987         EM_DEBUG_FUNC_BEGIN("mailbox[%p], mail_id[%d], nth[%p], handle[%p]", mailbox, mail_id, nth, handle);
988         char* mailbox_stream = NULL;
989         int err = EMF_ERROR_NONE;
990         int size = 0;
991                 
992         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);
993         EM_IF_NULL_RETURN_VALUE(mail_id, EMF_ERROR_INVALID_PARAM);
994         EM_IF_ACCOUNT_ID_NULL(mailbox->account_id, EMF_ERROR_INVALID_PARAM);
995         /* EM_IF_NULL_RETURN_VALUE(nth, EMF_ERROR_INVALID_PARAM); */
996         
997 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
998         emf_account_server_t account_server_type;
999         HIPC_API hAPI = NULL;
1000         ASNotiData as_noti_data;
1001         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
1002
1003         if ( email_get_account_server_type_by_account_id(mailbox->account_id, &account_server_type, true, &err) == false ) {
1004                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
1005                 goto FINISH_OFF;
1006         }
1007
1008         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
1009                 int as_handle;
1010                 if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
1011                         EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
1012                         goto FINISH_OFF;
1013                 }
1014                 
1015                 /*  noti to active sync */
1016                 as_noti_data.download_attachment.handle = as_handle;
1017                 as_noti_data.download_attachment.account_id = mailbox->account_id;
1018                 as_noti_data.download_attachment.mail_id = mail_id;
1019                 as_noti_data.download_attachment.attachment_order = strdup(nth);
1020                 if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT, &as_noti_data) == false) {
1021                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
1022                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1023                         goto FINISH_OFF;
1024                 }
1025
1026                 if(handle)
1027                         *handle = as_handle;
1028         } else {
1029                 hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_ATTACHMENT);
1030
1031                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1032
1033                 /* Mailbox */
1034                 mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
1035
1036                 EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
1037                 
1038                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
1039                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1040                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1041                 }
1042
1043                 /* Mail ID */
1044                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1045                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
1046                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1047                 }
1048
1049                 /* nth */
1050                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*) nth, sizeof(nth))) {
1051                         EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
1052                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1053                 }
1054
1055                 /* Execute API */
1056                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1057                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1058                         EM_SAFE_FREE(mailbox_stream);
1059                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1060                 }
1061
1062                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1063                 if (err != EMF_ERROR_NONE)              
1064                         goto FINISH_OFF;
1065                  
1066                 if(handle)
1067                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
1068                 
1069         }
1070         
1071 FINISH_OFF:
1072         emipc_destroy_email_api(hAPI);
1073         hAPI = NULL;
1074         EM_SAFE_FREE(mailbox_stream);
1075         EM_SAFE_FREE(as_noti_data.download_attachment.attachment_order);
1076 #else   /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
1077         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DOWNLOAD_ATTACHMENT);
1078
1079         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1080
1081         /* Mailbox */
1082         mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
1083
1084         EM_PROXY_IF_NULL_RETURN_VALUE(mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
1085         
1086         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_stream, size)) {
1087                 EM_DEBUG_EXCEPTION("Add Param mailbox failed");
1088                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1089         }
1090
1091         /* Mail ID */
1092         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &(mail_id), sizeof(int))) {
1093                 EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
1094                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1095         }
1096
1097         /* nth */
1098         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, nth, sizeof(nth))) {
1099                 EM_DEBUG_EXCEPTION("emipc_add_parameter mail_id failed");
1100                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1101         }
1102
1103         /* Execute API */
1104         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1105                 EM_DEBUG_EXCEPTION("EmfDownloadAttachment--emipc_execute_proxy_api failed");
1106                 EM_SAFE_FREE(mailbox_stream);
1107                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1108         }
1109
1110         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
1111
1112         EM_DEBUG_LOG(" >>>>> EmfDownloadAttachment RETURN VALUE : %d ", err);
1113
1114          if (err != EMF_ERROR_NONE)             
1115                  goto FINISH_OFF;
1116          
1117          if(handle)
1118                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
1119         
1120         /*  Prevent defect 36700 */
1121         /* EM_DEBUG_LOG(" >>>>> Handle_proxy : %d ", *handle); */
1122 FINISH_OFF:
1123         emipc_destroy_email_api(hAPI);
1124         hAPI = NULL;
1125         EM_SAFE_FREE(mailbox_stream);
1126 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
1127
1128         EM_DEBUG_FUNC_END("err [%d]", err);  
1129         return err;
1130         
1131 }
1132
1133
1134 EXPORT_API int email_cancel_job(int account_id, int handle)
1135 {
1136         EM_DEBUG_FUNC_BEGIN("account_id [%d], handle [%d]", account_id, handle);
1137         int err = EMF_ERROR_NONE;
1138
1139         if(account_id < 0)
1140                 return EMF_ERROR_INVALID_PARAM;
1141 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
1142         emf_account_server_t account_server_type;
1143         HIPC_API hAPI = NULL;
1144         ASNotiData as_noti_data;
1145         emstorage_account_tbl_t *account_list = NULL;
1146         int i, account_count = 0;
1147
1148         if ( account_id == ALL_ACCOUNT ) {      /*  this means that job is executed with all account */
1149                 /*  Get all accounts for sending notification to active sync engine. */
1150                 if (!emstorage_get_account_list(&account_count, &account_list , true, false, &err)) {
1151                         EM_DEBUG_EXCEPTION("email_get_account_list-- Failed [ %d ]  ", err);
1152         
1153                         goto FINISH_OFF;
1154                 }
1155
1156                 for(i = 0; i < account_count; i++) {
1157                         if ( email_get_account_server_type_by_account_id(account_list[i].account_id, &account_server_type, true, &err) == false ) {
1158                                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
1159                                 goto FINISH_OFF;
1160                         }
1161
1162                         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
1163                                 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
1164                                 as_noti_data.cancel_job.account_id = account_list[i].account_id;
1165                                 as_noti_data.cancel_job.handle = handle;
1166
1167                                 if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
1168                                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
1169                                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1170                                         goto FINISH_OFF;
1171                                 }
1172                         }
1173                 }
1174
1175                 /*  request canceling to stub */
1176                 hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
1177
1178                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1179
1180                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {               /*  account_id == 0 */
1181                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1182                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1183                 }
1184
1185                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &handle, sizeof(int))) {
1186                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1187                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1188                 }
1189
1190                 /* Execute API */
1191                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1192                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1193                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1194                 }
1195
1196                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1197                 emipc_destroy_email_api(hAPI);
1198                 hAPI = NULL;
1199         }
1200         else {
1201                 if ( email_get_account_server_type_by_account_id(account_id, &account_server_type, true, &err) == false ) {
1202                         EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
1203                         goto FINISH_OFF;
1204                 }
1205
1206                 if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
1207                         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
1208                         as_noti_data.cancel_job.account_id = account_id;
1209                         as_noti_data.cancel_job.handle = handle;
1210
1211                         if ( email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_CANCEL_JOB, &as_noti_data) == false) {
1212                                 EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
1213                                 err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1214                                 goto FINISH_OFF;
1215                         }
1216                 } else {
1217                         hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
1218
1219                         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1220
1221                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
1222                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1223                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1224                         }
1225
1226                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &handle, sizeof(int))) {
1227                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1228                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1229                         }
1230
1231                         /* Execute API */
1232                         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1233                                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1234                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1235                         }
1236                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1237                         emipc_destroy_email_api(hAPI);
1238                         hAPI = NULL;
1239                 }
1240         }
1241
1242 FINISH_OFF:
1243         emipc_destroy_email_api(hAPI);
1244         hAPI = NULL;
1245         if ( account_list )
1246                 emstorage_free_account(&account_list, account_count, NULL);
1247                 
1248 #else   /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
1249         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_CANCEL_JOB);
1250
1251         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1252
1253         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
1254                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1255                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1256         }
1257
1258         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &handle, sizeof(int))) {
1259                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1260                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1261         }
1262
1263         /* Execute API */
1264         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1265                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1266                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1267         }
1268
1269         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);        
1270
1271         EM_DEBUG_LOG(" >>>>> EmfCancelJob RETURN VALUE : %d ", err);
1272
1273         emipc_destroy_email_api(hAPI);
1274         hAPI = NULL;
1275 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__              */
1276
1277         EM_DEBUG_FUNC_END("err [%d]", err);  
1278         return err;
1279 }
1280
1281
1282
1283 EXPORT_API int email_get_pending_job(emf_action_t action, int account_id, int mail_id, emf_event_status_type_t * status)
1284 {
1285         EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d], status[%p]", action, account_id, mail_id, status);
1286         
1287         int err = EMF_ERROR_NONE;
1288
1289         EM_IF_ACCOUNT_ID_NULL(account_id, EMF_ERROR_NULL_VALUE);
1290         
1291         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_PENDING_JOB);
1292
1293         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1294
1295         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &action, sizeof(int))) {
1296                 EM_DEBUG_EXCEPTION("emipc_add_parameter action failed ");
1297                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1298         }
1299
1300         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
1301                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed ");
1302                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1303         }
1304
1305         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &mail_id, sizeof(int))) {
1306                 EM_DEBUG_EXCEPTION("emipc_add_parameter account_id failed ");
1307                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1308         }
1309
1310         /* Execute API */
1311         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1312                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed  ");
1313                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1314         }
1315
1316         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1317
1318         if(status) {
1319                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), status);
1320                 EM_DEBUG_LOG("status : %d ", *status);
1321         }
1322         
1323         emipc_destroy_email_api(hAPI);
1324         hAPI = NULL;
1325
1326         EM_DEBUG_FUNC_END("err [%d]", err);  
1327         return err;
1328
1329 }
1330
1331
1332
1333 EXPORT_API int email_get_network_status(int* on_sending, int* on_receiving)
1334 {
1335         EM_DEBUG_FUNC_BEGIN("on_sending[%p], on_receiving[%p]", on_sending, on_receiving);
1336         int err = EMF_ERROR_NONE;
1337         
1338         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_NETWORK_GET_STATUS);
1339
1340         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1341
1342         /* Execute API */
1343         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1344                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1345                 emipc_destroy_email_api(hAPI);
1346                 hAPI = NULL;
1347                 err = EMF_ERROR_IPC_SOCKET_FAILURE ;
1348                 EM_DEBUG_FUNC_END("err [%d]", err); return err;
1349         }
1350
1351         if(on_sending)
1352                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), on_sending );
1353         if(on_receiving)        
1354                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), on_receiving);
1355
1356         emipc_destroy_email_api(hAPI);
1357         hAPI = NULL;
1358         EM_DEBUG_FUNC_END("err [%d]", err);  
1359         return err;
1360 }
1361
1362 EXPORT_API int email_get_imap_mailbox_list(int account_id, const char* mailbox, unsigned* handle)
1363 {
1364         EM_DEBUG_FUNC_BEGIN();
1365
1366         int err = EMF_ERROR_NONE;
1367
1368         if(account_id <= 0 || !mailbox) {
1369                 EM_DEBUG_LOG("invalid parameters");
1370                 return EMF_ERROR_INVALID_PARAM;
1371         }
1372
1373         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_GET_IMAP_MAILBOX_LIST);       
1374
1375         /* account_id */
1376         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&account_id, sizeof(int))) {
1377                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
1378                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
1379         }
1380
1381         if(!emipc_execute_proxy_api(hAPI))  {
1382                 EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
1383                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1384         }
1385
1386         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1387         if(handle)
1388         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
1389
1390         emipc_destroy_email_api(hAPI);
1391         hAPI = NULL;
1392
1393         EM_DEBUG_FUNC_END("err [%d]", err);  
1394         return err;
1395 }
1396
1397 EXPORT_API int email_search_mail_on_server(int input_account_id, const char *input_mailbox_name, email_search_filter_t *input_search_filter_list, int input_search_filter_count, unsigned *output_handle)
1398 {
1399         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_name [%p], input_search_filter_list [%p], input_search_filter_count [%d], output_handle [%p]", input_account_id, input_mailbox_name, input_search_filter_list, input_search_filter_count, output_handle);
1400
1401         int       err = EMF_ERROR_NONE;
1402         int       return_value = 0;
1403         int       stream_size_for_search_filter_list = 0;
1404         char     *stream_for_search_filter_list = NULL;
1405         HIPC_API  hAPI = NULL;
1406
1407         EM_IF_NULL_RETURN_VALUE(input_account_id,         EMF_ERROR_INVALID_PARAM);
1408         EM_IF_NULL_RETURN_VALUE(input_mailbox_name,       EMF_ERROR_INVALID_PARAM);
1409         EM_IF_NULL_RETURN_VALUE(input_search_filter_list, EMF_ERROR_INVALID_PARAM);
1410
1411 #ifdef __FEATURE_SUPPORT_ACTIVE_SYNC__
1412         emf_account_server_t account_server_type = EMF_SERVER_TYPE_NONE;
1413         ASNotiData as_noti_data;
1414
1415         memset(&as_noti_data, 0, sizeof(ASNotiData)); /* initialization of union members */
1416
1417         if ( email_get_account_server_type_by_account_id(input_account_id, &account_server_type, true, &err) == false ) {
1418                 EM_DEBUG_EXCEPTION("email_get_account_server_type_by_account_id failed[%d]", err);
1419                 goto FINISH_OFF;
1420         }
1421
1422         if ( account_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC ) {
1423                 int as_handle = 0;
1424
1425                 if ( email_get_handle_for_activesync(&as_handle, &err) == false ) {
1426                         EM_DEBUG_EXCEPTION("email_get_handle_for_activesync failed[%d].", err);
1427                         goto FINISH_OFF;
1428                 }
1429
1430                 /*  noti to active sync */
1431                 as_noti_data.search_mail_on_server.handle              = as_handle;
1432                 as_noti_data.search_mail_on_server.account_id          = input_account_id;
1433                 as_noti_data.search_mail_on_server.mailbox_name        = EM_SAFE_STRDUP(input_mailbox_name);
1434                 as_noti_data.search_mail_on_server.search_filter_list  = input_search_filter_list;
1435                 as_noti_data.search_mail_on_server.search_filter_count = input_search_filter_count;
1436
1437                 return_value = email_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER, &as_noti_data);
1438
1439                 EM_SAFE_FREE(as_noti_data.search_mail_on_server.mailbox_name);
1440
1441                 if ( return_value == false ) {
1442                         EM_DEBUG_EXCEPTION("email_send_notification_to_active_sync_engine failed.");
1443                         err = EMF_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
1444                         goto FINISH_OFF;
1445                 }
1446
1447                 if(output_handle)
1448                         *output_handle = as_handle;
1449         }
1450         else
1451 #endif  /*  __FEATURE_SUPPORT_ACTIVE_SYNC__     */
1452         {
1453                 hAPI = emipc_create_email_api(_EMAIL_API_SEARCH_MAIL_ON_SERVER);
1454
1455                 EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
1456
1457                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)&input_account_id, sizeof(int))) {
1458                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1459                         err = EMF_ERROR_IPC_PROTOCOL_FAILURE;
1460                         goto FINISH_OFF;
1461                 }
1462
1463                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (void*)input_mailbox_name, strlen(input_mailbox_name))){
1464                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1465                         err = EMF_ERROR_IPC_PROTOCOL_FAILURE;
1466                         goto FINISH_OFF;
1467                 }
1468
1469                 stream_for_search_filter_list = em_convert_search_filter_to_byte_stream(input_search_filter_list, input_search_filter_count, &stream_size_for_search_filter_list);
1470
1471                 EM_PROXY_IF_NULL_RETURN_VALUE(stream_for_search_filter_list, hAPI, EMF_ERROR_NULL_VALUE);
1472
1473                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, stream_for_search_filter_list, stream_size_for_search_filter_list)) {
1474                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1475                         err = EMF_ERROR_IPC_PROTOCOL_FAILURE;
1476                         goto FINISH_OFF;
1477                 }
1478
1479                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
1480                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
1481                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
1482                 }
1483
1484                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
1485
1486                 if (err != EMF_ERROR_NONE) {
1487                         EM_DEBUG_EXCEPTION("_EMAIL_API_SEARCH_MAIL_ON_SERVER failed [%d]", err);
1488                         goto FINISH_OFF;
1489                 }
1490
1491                 if(output_handle)
1492                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
1493         }
1494
1495 FINISH_OFF:
1496         if(hAPI) {
1497                 emipc_destroy_email_api(hAPI);
1498                 hAPI = NULL;
1499         }
1500
1501         EM_DEBUG_FUNC_END("err [%d]", err);
1502         return err;
1503 }