Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-api / email-api-mailbox.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /**
25  *
26  * This file contains the data structures and interfaces needed for application,
27  * to interact with email-service.
28  * @file                email_api_mailbox.c
29  * @brief               This file contains the data structures and interfaces of mailbox 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-storage.h"
37 #include "email-core-utils.h"
38 #include "email-utilities.h"
39 #include "email-ipc.h"
40 #include "db-util.h"
41
42 /* API - Create a mailbox */
43
44 EXPORT_API int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle)
45 {
46         EM_DEBUG_FUNC_BEGIN();
47
48         int size = 0;
49         int err = EMAIL_ERROR_NONE;
50         char* local_mailbox_stream = NULL;
51         email_account_server_t account_server_type;
52         HIPC_API hAPI = NULL;
53         ASNotiData as_noti_data;
54
55         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
56
57         EM_IF_NULL_RETURN_VALUE(new_mailbox, EMAIL_ERROR_INVALID_PARAM);
58
59         /*  check account bind type and branch off  */
60         if ( em_get_account_server_type_by_account_id(new_mailbox->account_id, &account_server_type, false, &err) == false ) {
61                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
62                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
63                 goto FINISH_OFF;
64         }
65
66         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && on_server) {
67                 int as_handle;
68
69                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
70                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
71                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
72                         goto FINISH_OFF;
73                 }
74
75                 /*  noti to active sync */
76                 as_noti_data.add_mailbox.handle        = as_handle;
77                 as_noti_data.add_mailbox.account_id    = new_mailbox->account_id;
78                 as_noti_data.add_mailbox.mailbox_alias = new_mailbox->alias;
79                 as_noti_data.add_mailbox.mailbox_path  = new_mailbox->mailbox_name;
80
81                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_ADD_MAILBOX, &as_noti_data) == false) {
82                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
83                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
84                         goto FINISH_OFF;
85                 }
86
87                 if(handle)
88                         *handle = as_handle;
89         }
90         else {
91                 hAPI = emipc_create_email_api(_EMAIL_API_ADD_MAILBOX);
92
93                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
94
95                 local_mailbox_stream = em_convert_mailbox_to_byte_stream(new_mailbox, &size);
96
97                 EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMAIL_ERROR_NULL_VALUE);
98
99                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
100                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
101                         EM_SAFE_FREE(local_mailbox_stream);
102                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
103                 }
104
105                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &on_server, sizeof(int))) {
106                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
107                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
108                 }
109
110                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
111                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
112                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
113                 }
114
115                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
116                 EM_DEBUG_LOG(" >>>>> error VALUE [%d]", err);
117         
118                 if(handle) {
119                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
120                         EM_DEBUG_LOG(" >>>>> Handle [%d]", *handle);
121                 }
122
123                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 2, sizeof(int), &(new_mailbox->mailbox_id));
124                 EM_DEBUG_LOG(" >>>>> new_mailbox->mailbox_id [%d]", new_mailbox->mailbox_id);
125
126                 emipc_destroy_email_api(hAPI);
127                 hAPI = NULL;
128         }
129
130 FINISH_OFF:
131         EM_SAFE_FREE(local_mailbox_stream);
132
133         EM_DEBUG_FUNC_END("err [%d]", err);
134         return err;
135 }
136
137 EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias, int input_on_server, int *output_handle)
138 {
139         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_name [%p], input_mailbox_alias [%p], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_mailbox_name, input_mailbox_alias, input_on_server, output_handle);
140
141         int err = EMAIL_ERROR_NONE;
142         email_account_server_t account_server_type;
143         HIPC_API hAPI = NULL;
144         ASNotiData as_noti_data;
145         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
146
147         EM_IF_NULL_RETURN_VALUE(input_mailbox_id,    EMAIL_ERROR_INVALID_PARAM);
148         EM_IF_NULL_RETURN_VALUE(input_mailbox_name,  EMAIL_ERROR_INVALID_PARAM);
149         EM_IF_NULL_RETURN_VALUE(input_mailbox_alias, EMAIL_ERROR_INVALID_PARAM);
150
151         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
152                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
153                 goto FINISH_OFF;
154         }
155
156         /*  check account bind type and branch off  */
157         if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
158                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
159                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
160                 goto FINISH_OFF;
161         }
162
163         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
164                 int as_handle;
165
166                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
167                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
168                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
169                         goto FINISH_OFF;
170                 }
171
172                 /*  noti to active sync */
173                 as_noti_data.rename_mailbox.handle        = as_handle;
174                 as_noti_data.rename_mailbox.account_id    = mailbox_tbl->account_id;
175                 as_noti_data.rename_mailbox.mailbox_id    = input_mailbox_id;
176                 as_noti_data.rename_mailbox.mailbox_name  = input_mailbox_name;
177                 as_noti_data.rename_mailbox.mailbox_alias = input_mailbox_alias;
178
179                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RENAME_MAILBOX, &as_noti_data) == false) {
180                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
181                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
182                         goto FINISH_OFF;
183                 }
184
185                 if(output_handle)
186                         *output_handle = as_handle;
187         }
188         else {
189                 hAPI = emipc_create_email_api(_EMAIL_API_RENAME_MAILBOX);
190
191                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
192                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_id failed");
193                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
194                 }
195
196                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_name, EM_SAFE_STRLEN(input_mailbox_name)+1 )) {
197                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_path failed");
198                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
199                 }
200
201                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_alias, EM_SAFE_STRLEN(input_mailbox_alias)+1 )) {
202                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_alias failed");
203                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
204                 }
205
206                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
207                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
208                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
209                 }
210
211                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
212                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
213                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
214                 }
215
216                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
217
218                 EM_DEBUG_LOG("error VALUE [%d]", err);
219                 emipc_destroy_email_api(hAPI);
220         }
221 FINISH_OFF:
222         if (mailbox_tbl)
223                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
224
225         EM_DEBUG_FUNC_END("err [%d]", err);
226         return err;
227 }
228
229
230 /* API - Delete a mailbox */
231
232 EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, int *output_handle)
233 {
234         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
235         
236         int err = EMAIL_ERROR_NONE;
237         email_account_server_t account_server_type;
238         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
239         HIPC_API hAPI = NULL;
240         ASNotiData as_noti_data;
241         
242         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
243
244         if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
245                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
246                 goto FINISH_OFF;
247         }
248
249         /*  check account bind type and branch off  */
250         if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
251                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
252                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
253                 goto FINISH_OFF;
254         }
255
256         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
257                 int as_handle;
258
259                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
260                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
261                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
262                         goto FINISH_OFF;
263                 }
264
265                 /*  noti to active sync */
266                 as_noti_data.delete_mailbox.handle        = as_handle;
267                 as_noti_data.delete_mailbox.account_id    = mailbox_tbl->account_id;
268                 as_noti_data.delete_mailbox.mailbox_id    = input_mailbox_id;
269
270                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX, &as_noti_data) == false) {
271                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
272                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
273                         goto FINISH_OFF;
274                 }
275
276                 if(output_handle)
277                         *output_handle = as_handle;
278         }
279         else {
280                 hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAILBOX);
281
282                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
283
284                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
285                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
286                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
287                 }
288
289                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
290                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
291                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
292                 }
293
294                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
295                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
296                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
297                 }
298
299                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
300                 EM_DEBUG_LOG("error VALUE [%d]", err);
301
302                 if(input_on_server) {
303                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
304                         EM_DEBUG_LOG("output_handle [%d]", output_handle);
305                 }
306
307                 emipc_destroy_email_api(hAPI);
308         }
309
310 FINISH_OFF:
311         if (mailbox_tbl) {
312                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
313         }
314
315         EM_DEBUG_FUNC_END("err [%d]", err);
316         return err;
317 }
318
319 EXPORT_API int email_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server, int *output_handle)
320 {
321         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id_array, input_mailbox_id_array, input_mailbox_id_count, input_on_server, output_handle);
322         int err = EMAIL_ERROR_NONE;
323         email_account_server_t account_server_type;
324         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX task_parameter;
325
326         if(input_account_id == 0 || input_mailbox_id_count <= 0 || input_mailbox_id_array == NULL) {
327                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
328                 err = EMAIL_ERROR_INVALID_PARAM;
329                 goto FINISH_OFF;
330         }
331
332         /*  check account bind type and branch off  */
333         if ( em_get_account_server_type_by_account_id(input_account_id, &account_server_type, false, &err) == false ) {
334                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
335                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
336                 goto FINISH_OFF;
337         }
338
339         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
340                 int as_handle;
341                 ASNotiData as_noti_data;
342
343                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
344                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
345                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
346                         goto FINISH_OFF;
347                 }
348
349                 /*  noti to active sync */
350                 as_noti_data.delete_mailbox_ex.handle            = as_handle;
351                 as_noti_data.delete_mailbox_ex.account_id        = input_account_id;
352                 as_noti_data.delete_mailbox_ex.mailbox_id_array  = input_mailbox_id_array;
353                 as_noti_data.delete_mailbox_ex.mailbox_id_count  = input_mailbox_id_count;
354                 as_noti_data.delete_mailbox_ex.on_server         = input_on_server;
355
356                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX, &as_noti_data) == false) {
357                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
358                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
359                         goto FINISH_OFF;
360                 }
361
362                 if(output_handle)
363                         *output_handle = as_handle;
364         }
365         else {
366                 task_parameter.account_id        = input_account_id;
367                 task_parameter.mailbox_id_array  = input_mailbox_id_array;
368                 task_parameter.mailbox_id_count  = input_mailbox_id_count;
369                 task_parameter.on_server         = input_on_server;
370
371                 if((err = emipc_execute_proxy_task(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, &task_parameter)) != EMAIL_ERROR_NONE) {
372                         EM_DEBUG_EXCEPTION("execute_proxy_task failed [%d]", err);
373                         goto FINISH_OFF;
374                 }
375         }
376
377 FINISH_OFF:
378
379         EM_DEBUG_FUNC_END("err [%d]", err);
380         return err;
381 }
382
383 EXPORT_API int email_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
384 {
385         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
386         int err = EMAIL_ERROR_NONE;
387
388         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
389
390         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAILBOX_TYPE);
391
392         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
393
394         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
395                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
396                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
397         }
398
399         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_type, sizeof(int))) {
400                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
401                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
402         }
403
404         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
405                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
406                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
407         }
408
409         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
410
411         EM_DEBUG_LOG("error VALUE [%d]", err);
412         emipc_destroy_email_api(hAPI);
413         hAPI = NULL;
414
415         EM_DEBUG_FUNC_END("err [%d]", err);
416         return err;
417 }
418
419 EXPORT_API int email_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox)
420 {
421         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_is_local_mailbox [%d]", input_mailbox_id, input_is_local_mailbox);
422         int err = EMAIL_ERROR_NONE;
423
424         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
425
426         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_LOCAL_MAILBOX);
427
428         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
429
430         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
431                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
432                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
433         }
434
435         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_is_local_mailbox, sizeof(int))) {
436                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
437                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
438         }
439
440         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
441                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
442                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
443         }
444
445         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
446
447         EM_DEBUG_LOG("error VALUE [%d]", err);
448         emipc_destroy_email_api(hAPI);
449         hAPI = NULL;
450
451         EM_DEBUG_FUNC_END("err [%d]", err);
452         return err;
453 }
454
455 EXPORT_API int email_get_sync_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count)
456 {
457         EM_DEBUG_FUNC_BEGIN();
458         int mailbox_count = 0;
459         int err = EMAIL_ERROR_NONE ;
460         int i = 0;
461         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL;
462
463         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
464         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
465         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
466
467         if (!emstorage_get_mailbox_list(account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
468                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
469
470
471                 goto FINISH_OFF;
472         } else
473                 err = EMAIL_ERROR_NONE;
474         
475         if (mailbox_count > 0)  {
476                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
477                         EM_DEBUG_EXCEPTION("malloc failed...");
478                         err= EMAIL_ERROR_OUT_OF_MEMORY;
479                         goto FINISH_OFF;
480                 }
481                 for (i = 0; i < mailbox_count; i++)
482                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
483         } else
484                 *mailbox_list = NULL;
485         
486         *count = mailbox_count;
487         
488         FINISH_OFF:
489         if (mailbox_tbl_list != NULL)
490                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
491         EM_DEBUG_FUNC_END("err [%d]", err);
492         return err;
493 }
494
495
496 EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, email_mailbox_t** mailbox_list, int* count)
497 {
498         EM_DEBUG_FUNC_BEGIN();  
499
500         int mailbox_count = 0;
501         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
502         int err =EMAIL_ERROR_NONE;
503         int i;
504         
505         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
506         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
507         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
508
509         if (!emstorage_get_mailbox_list(account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {
510                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
511
512                 goto FINISH_OFF;
513         } else
514                 err = EMAIL_ERROR_NONE;
515         
516         if (mailbox_count > 0)  {
517                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
518                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
519                         err= EMAIL_ERROR_OUT_OF_MEMORY;
520                         goto FINISH_OFF;
521                 }
522
523                 for (i = 0; i < mailbox_count; i++)
524                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
525         } else
526                 *mailbox_list = NULL;
527         
528         *count = mailbox_count;
529
530 FINISH_OFF:
531         if (mailbox_tbl_list != NULL)
532                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
533
534         EM_DEBUG_FUNC_END("err [%d]", err);
535         return err;
536 }
537
538
539
540 EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type, int with_count, email_mailbox_t** mailbox_list, int* count)
541 {
542         EM_DEBUG_FUNC_BEGIN();  
543
544         int mailbox_count = 0;
545         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
546         int err =EMAIL_ERROR_NONE;
547         int i;
548         
549         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
550         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
551         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
552
553         if (!emstorage_get_mailbox_list_ex(account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {        
554                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list_ex failed [%d]", err);
555
556                 goto FINISH_OFF;
557         } else
558                 err = EMAIL_ERROR_NONE;
559         
560         if (mailbox_count > 0)  {
561                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
562                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
563                         err= EMAIL_ERROR_OUT_OF_MEMORY;
564                         goto FINISH_OFF;
565                 }
566
567                 for (i = 0; i < mailbox_count; i++)
568                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
569         }
570         else
571                 *mailbox_list = NULL;
572
573         if ( count )    
574                 *count = mailbox_count;
575
576 FINISH_OFF:
577         if (mailbox_tbl_list != NULL)
578                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
579
580         EM_DEBUG_FUNC_END("err [%d]", err);
581         return err;
582 }
583
584
585 /* sowmya.kr, 05-Apr-2010, new MAPI*/
586
587 EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type,  email_mailbox_t** mailbox)
588 {
589         EM_DEBUG_FUNC_BEGIN();
590         
591         int err = EMAIL_ERROR_NONE;
592         email_mailbox_t* curr_mailbox = NULL;
593         emstorage_mailbox_tbl_t* local_mailbox = NULL;
594
595         EM_IF_NULL_RETURN_VALUE(mailbox, EMAIL_ERROR_INVALID_PARAM);    
596         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM)  ;
597
598
599         if(mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED)
600                 return EMAIL_ERROR_INVALID_PARAM;
601         if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &local_mailbox, true, &err))  {
602                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
603
604                 goto FINISH_OFF;
605         } else {
606                 err = EMAIL_ERROR_NONE;
607                 curr_mailbox = em_malloc(sizeof(email_mailbox_t));
608                 memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));
609                 em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
610         }
611
612         *mailbox = curr_mailbox;        
613 FINISH_OFF:
614
615         if(local_mailbox)
616                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
617         EM_DEBUG_FUNC_END("err [%d]", err);
618         return err;
619 }
620
621 EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailbox_t** output_mailbox)
622 {
623         EM_DEBUG_FUNC_BEGIN();
624
625         int err = EMAIL_ERROR_NONE;
626         email_mailbox_t* curr_mailbox = NULL;
627         emstorage_mailbox_tbl_t* local_mailbox = NULL;
628
629         EM_IF_NULL_RETURN_VALUE(output_mailbox, EMAIL_ERROR_INVALID_PARAM);
630
631         if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE) {
632                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
633                 return err;
634         } else {
635                 curr_mailbox = em_malloc(sizeof(email_mailbox_t));
636
637                 memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));
638
639                 em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
640         }
641
642         *output_mailbox = curr_mailbox;
643
644         emstorage_free_mailbox(&local_mailbox, 1, &err);
645
646         EM_DEBUG_FUNC_END("err [%d]", err);
647         return err;
648 }
649
650 EXPORT_API int email_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size)
651 {
652         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], new_slot_size[%d]", account_id, mailbox_id, new_slot_size);
653
654         int err = EMAIL_ERROR_NONE;
655
656         if(new_slot_size < 0) {
657                 EM_DEBUG_EXCEPTION("new_slot_size should be greater than 0 or should be equal to 0");
658                 return EMAIL_ERROR_INVALID_PARAM;
659         }
660         
661         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAIL_SLOT_SIZE);  
662
663         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
664
665         if (hAPI) {
666                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
667                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
668                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
669                 }
670
671                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &mailbox_id, sizeof(int))) {
672                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
673                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
674                 }
675
676                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &new_slot_size, sizeof(int))) {
677                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for new_slot_size failed");
678                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
679                 }
680
681                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
682                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
683                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
684                 } 
685         
686                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
687                 EM_DEBUG_LOG("email_set_mail_slot_size error VALUE [%d]", err);
688                 /*prevent 23139*/
689 /*              if(handle) {
690                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
691                         EM_DEBUG_LOG("email_set_mail_slot_size handle VALUE [%d]", handle);
692                 } */
693                 emipc_destroy_email_api(hAPI);
694                 hAPI = NULL;
695                 
696         }
697         EM_DEBUG_FUNC_END("err [%d]", err);
698         return err;
699 }
700
701 EXPORT_API int email_stamp_sync_time_of_mailbox(int input_mailbox_id)
702 {
703         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d]", input_mailbox_id);
704
705         int err = EMAIL_ERROR_NONE;
706
707         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
708
709         err = emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_id, 1);
710
711         EM_DEBUG_FUNC_END("err [%d]", err);
712         return err;
713 }
714
715 EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count)
716 {
717         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
718         int err = EMAIL_ERROR_NONE;
719
720         if (count <= 0 || !mailbox_list || !*mailbox_list) {
721                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
722                 err = EMAIL_ERROR_INVALID_PARAM;
723                 goto FINISH_OFF;
724         }
725
726         email_mailbox_t *p = *mailbox_list;
727         int i;
728
729         for (i = 0; i < count; i++)  {
730                 EM_SAFE_FREE(p[i].mailbox_name);
731                 EM_SAFE_FREE(p[i].alias);
732         }
733
734         EM_SAFE_FREE(p);
735         *mailbox_list = NULL;
736
737 FINISH_OFF:
738         EM_DEBUG_FUNC_END("err [%d]", err);
739         return err;
740
741 }