2.0_alpha release commit
[framework/messaging/email-service.git] / email-api / email-api-mailbox.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_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, unsigned* 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_DEBUG_FUNC_END("err [%d]", err);
132         return err;
133 }
134
135 EXPORT_API int email_rename_mailbox(int input_mailbox_id, char *input_mailbox_name, char *input_mailbox_alias, int input_on_server, unsigned *output_handle)
136 {
137         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);
138
139         int err = EMAIL_ERROR_NONE;
140         email_account_server_t account_server_type;
141         HIPC_API hAPI = NULL;
142         ASNotiData as_noti_data;
143         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
144
145         EM_IF_NULL_RETURN_VALUE(input_mailbox_id,    EMAIL_ERROR_INVALID_PARAM);
146         EM_IF_NULL_RETURN_VALUE(input_mailbox_name,  EMAIL_ERROR_INVALID_PARAM);
147         EM_IF_NULL_RETURN_VALUE(input_mailbox_alias, EMAIL_ERROR_INVALID_PARAM);
148
149         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
150                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
151                 goto FINISH_OFF;
152         }
153
154         /*  check account bind type and branch off  */
155         if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
156                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
157                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
158                 goto FINISH_OFF;
159         }
160
161         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
162                 int as_handle;
163
164                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
165                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
166                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
167                         goto FINISH_OFF;
168                 }
169
170                 /*  noti to active sync */
171                 as_noti_data.rename_mailbox.handle        = as_handle;
172                 as_noti_data.rename_mailbox.account_id    = mailbox_tbl->account_id;
173                 as_noti_data.rename_mailbox.mailbox_id    = input_mailbox_id;
174                 as_noti_data.rename_mailbox.mailbox_name  = input_mailbox_name;
175                 as_noti_data.rename_mailbox.mailbox_alias = input_mailbox_alias;
176
177                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RENAME_MAILBOX, &as_noti_data) == false) {
178                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
179                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
180                         goto FINISH_OFF;
181                 }
182
183                 if(output_handle)
184                         *output_handle = as_handle;
185         }
186         else {
187                 hAPI = emipc_create_email_api(_EMAIL_API_RENAME_MAILBOX);
188
189                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
190                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_id failed");
191                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
192                 }
193
194                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_name, strlen(input_mailbox_name)+1 )) {
195                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_path failed");
196                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
197                 }
198
199                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_alias, strlen(input_mailbox_alias)+1 )) {
200                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_alias failed");
201                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
202                 }
203
204                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
205                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
206                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
207                 }
208
209                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
210                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
211                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
212                 }
213
214                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
215
216                 EM_DEBUG_LOG("error VALUE [%d]", err);
217                 emipc_destroy_email_api(hAPI);
218         }
219 FINISH_OFF:
220         if (mailbox_tbl)
221                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
222
223         EM_DEBUG_FUNC_END("err [%d]", err);
224         return err;
225 }
226
227
228 /* API - Delete a mailbox */
229
230 EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, unsigned* output_handle)
231 {
232         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
233         
234         int err = EMAIL_ERROR_NONE;
235         email_account_server_t account_server_type;
236         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
237         HIPC_API hAPI = NULL;
238         ASNotiData as_noti_data;
239         
240         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
241
242         if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
243                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
244                 goto FINISH_OFF;
245         }
246
247         /*  check account bind type and branch off  */
248         if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
249                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
250                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
251                 goto FINISH_OFF;
252         }
253
254         if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
255                 int as_handle;
256
257                 if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
258                         EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
259                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
260                         goto FINISH_OFF;
261                 }
262
263                 /*  noti to active sync */
264                 as_noti_data.delete_mailbox.handle        = as_handle;
265                 as_noti_data.delete_mailbox.account_id    = mailbox_tbl->account_id;
266                 as_noti_data.delete_mailbox.mailbox_id    = input_mailbox_id;
267
268                 if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX, &as_noti_data) == false) {
269                         EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
270                         err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
271                         goto FINISH_OFF;
272                 }
273
274                 if(output_handle)
275                         *output_handle = as_handle;
276         }
277         else {
278                 hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAILBOX);
279
280                 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
281
282                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
283                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
284                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
285                 }
286
287                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
288                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
289                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
290                 }
291
292                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
293                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
294                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
295                 }
296
297                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
298                 EM_DEBUG_LOG("error VALUE [%d]", err);
299
300                 if(input_on_server) {
301                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
302                         EM_DEBUG_LOG("output_handle [%d]", output_handle);
303                 }
304
305                 emipc_destroy_email_api(hAPI);
306         }
307
308 FINISH_OFF:
309         if (mailbox_tbl) {
310                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
311         }
312
313         EM_DEBUG_FUNC_END("err [%d]", err);
314         return err;
315 }
316
317 EXPORT_API int email_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
318 {
319         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
320         int err = EMAIL_ERROR_NONE;
321
322         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
323
324         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAILBOX_TYPE);
325
326         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
327
328         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
329                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
330                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
331         }
332
333         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_type, sizeof(int))) {
334                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
335                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
336         }
337
338         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
339                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
340                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
341         }
342
343         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
344
345         EM_DEBUG_LOG("error VALUE [%d]", err);
346         emipc_destroy_email_api(hAPI);
347         hAPI = NULL;
348
349         EM_DEBUG_FUNC_END("err [%d]", err);
350         return err;
351
352
353
354 }
355
356 EXPORT_API int email_get_sync_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count)
357 {
358         EM_DEBUG_FUNC_BEGIN();
359         int mailbox_count = 0;
360         int err = EMAIL_ERROR_NONE ;
361         int i = 0;
362         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL;
363
364         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
365         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
366         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
367
368         if (!emstorage_get_mailbox_list(account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
369                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
370
371
372                 goto FINISH_OFF;
373         } else
374                 err = EMAIL_ERROR_NONE;
375         
376         if (mailbox_count > 0)  {
377                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
378                         EM_DEBUG_EXCEPTION("malloc failed...");
379                         err= EMAIL_ERROR_OUT_OF_MEMORY;
380                         goto FINISH_OFF;
381                 }
382                 for (i = 0; i < mailbox_count; i++)
383                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
384         } else
385                 *mailbox_list = NULL;
386         
387         *count = mailbox_count;
388         
389         FINISH_OFF:
390         if (mailbox_tbl_list != NULL)
391                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
392         EM_DEBUG_FUNC_END("err [%d]", err);
393         return err;
394 }
395
396
397 EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, email_mailbox_t** mailbox_list, int* count)
398 {
399         EM_DEBUG_FUNC_BEGIN();  
400
401         int mailbox_count = 0;
402         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
403         int err =EMAIL_ERROR_NONE;
404         int i;
405         
406         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
407         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
408         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
409
410         if (!emstorage_get_mailbox_list(account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {
411                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
412
413                 goto FINISH_OFF;
414         } else
415                 err = EMAIL_ERROR_NONE;
416         
417         if (mailbox_count > 0)  {
418                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
419                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
420                         err= EMAIL_ERROR_OUT_OF_MEMORY;
421                         goto FINISH_OFF;
422                 }
423
424                 for (i = 0; i < mailbox_count; i++)
425                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
426         } else
427                 *mailbox_list = NULL;
428         
429         *count = mailbox_count;
430
431 FINISH_OFF:
432         if (mailbox_tbl_list != NULL)
433                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
434
435         EM_DEBUG_FUNC_END("err [%d]", err);
436         return err;
437 }
438
439
440
441 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)
442 {
443         EM_DEBUG_FUNC_BEGIN();  
444
445         int mailbox_count = 0;
446         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
447         int err =EMAIL_ERROR_NONE;
448         int i;
449         
450         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
451         EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
452         EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);
453
454         if (!emstorage_get_mailbox_list_ex(account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {        
455                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list_ex failed [%d]", err);
456
457                 goto FINISH_OFF;
458         } else
459                 err = EMAIL_ERROR_NONE;
460         
461         if (mailbox_count > 0)  {
462                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
463                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
464                         err= EMAIL_ERROR_OUT_OF_MEMORY;
465                         goto FINISH_OFF;
466                 }
467
468                 for (i = 0; i < mailbox_count; i++)
469                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
470         }
471         else
472                 *mailbox_list = NULL;
473
474         if ( count )    
475                 *count = mailbox_count;
476
477 FINISH_OFF:
478         if (mailbox_tbl_list != NULL)
479                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
480
481         EM_DEBUG_FUNC_END("err [%d]", err);
482         return err;
483 }
484
485
486 /* sowmya.kr, 05-Apr-2010, new MAPI*/
487
488 EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type,  email_mailbox_t** mailbox)
489 {
490         EM_DEBUG_FUNC_BEGIN();
491         
492         int err = EMAIL_ERROR_NONE;
493         email_mailbox_t* curr_mailbox = NULL;
494         emstorage_mailbox_tbl_t* local_mailbox = NULL;
495
496         EM_IF_NULL_RETURN_VALUE(mailbox, EMAIL_ERROR_INVALID_PARAM);    
497         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM)  ;
498
499
500         if(mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS)
501                 return EMAIL_ERROR_INVALID_PARAM;
502         if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &local_mailbox, true, &err))  {
503                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
504
505                 goto FINISH_OFF;
506         } else {
507                 err = EMAIL_ERROR_NONE;
508                 curr_mailbox = em_malloc(sizeof(email_mailbox_t));
509                 memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));
510                 em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
511         }
512
513         *mailbox = curr_mailbox;        
514 FINISH_OFF:
515
516         if(local_mailbox)
517                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
518         EM_DEBUG_FUNC_END("err [%d]", err);
519         return err;
520 }
521
522 EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailbox_t** output_mailbox)
523 {
524         EM_DEBUG_FUNC_BEGIN();
525
526         int err = EMAIL_ERROR_NONE;
527         email_mailbox_t* curr_mailbox = NULL;
528         emstorage_mailbox_tbl_t* local_mailbox = NULL;
529
530         EM_IF_NULL_RETURN_VALUE(output_mailbox, EMAIL_ERROR_INVALID_PARAM);
531
532         if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE) {
533                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
534                 return err;
535         } else {
536                 curr_mailbox = em_malloc(sizeof(email_mailbox_t));
537
538                 memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));
539
540                 em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
541         }
542
543         *output_mailbox = curr_mailbox;
544
545         emstorage_free_mailbox(&local_mailbox, 1, &err);
546
547         EM_DEBUG_FUNC_END("err [%d]", err);
548         return err;
549 }
550
551 EXPORT_API int email_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size)
552 {
553         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], new_slot_size[%d]", account_id, mailbox_id, new_slot_size);
554
555         int err = EMAIL_ERROR_NONE, *handle = NULL;
556
557         if(new_slot_size < 0) {
558                 EM_DEBUG_EXCEPTION("new_slot_size should be greater than 0 or should be equal to 0");
559                 return EMAIL_ERROR_INVALID_PARAM;
560         }
561         
562         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAIL_SLOT_SIZE);  
563
564         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
565
566         if (hAPI) {
567                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
568                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
569                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
570                 }
571
572                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &mailbox_id, sizeof(int))) {
573                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
574                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
575                 }
576
577                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &new_slot_size, sizeof(int))) {
578                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for new_slot_size failed");
579                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
580                 }
581
582                 if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
583                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
584                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
585                 } 
586         
587                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
588                 EM_DEBUG_LOG("email_set_mail_slot_size error VALUE [%d]", err);
589                 if(handle) {
590                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
591                         EM_DEBUG_LOG("email_set_mail_slot_size handle VALUE [%d]", handle);
592                 }
593                 emipc_destroy_email_api(hAPI);
594                 hAPI = NULL;
595                 
596         }
597         EM_DEBUG_FUNC_END("err [%d]", err);
598         return err;
599 }
600
601 EXPORT_API int email_stamp_sync_time_of_mailbox(int input_mailbox_id)
602 {
603         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d]", input_mailbox_id);
604
605         int err = EMAIL_ERROR_NONE;
606
607         EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);
608
609         err = emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_id, 1);
610
611         EM_DEBUG_FUNC_END("err [%d]", err);
612         return err;
613 }
614
615 EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count)
616 {
617         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
618         int err = EMAIL_ERROR_NONE;
619
620         if (count <= 0 || !mailbox_list || !*mailbox_list) {
621                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
622                 err = EMAIL_ERROR_INVALID_PARAM;
623                 goto FINISH_OFF;
624         }
625
626         email_mailbox_t *p = *mailbox_list;
627         int i;
628
629         for (i = 0; i < count; i++)  {
630                 EM_SAFE_FREE(p[i].mailbox_name);
631                 EM_SAFE_FREE(p[i].alias);
632         }
633
634         EM_SAFE_FREE(p);
635         *mailbox_list = NULL;
636
637 FINISH_OFF:
638         EM_DEBUG_FUNC_END("err [%d]", err);
639         return err;
640
641 }