update for beta universally
[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(emf_mailbox_t* new_mailbox, int on_server, unsigned* handle)
45 {
46         EM_DEBUG_FUNC_BEGIN();
47         
48         char* local_mailbox_stream = NULL;
49         int size = 0;
50         int err = EMF_ERROR_NONE;
51         EM_IF_NULL_RETURN_VALUE(new_mailbox, EMF_ERROR_INVALID_PARAM);
52
53         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_MAILBOX);
54
55         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
56         
57         local_mailbox_stream = em_convert_mailbox_to_byte_stream(new_mailbox, &size);
58
59         EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
60
61         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
62                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
63                 EM_SAFE_FREE(local_mailbox_stream); 
64                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
65         }
66
67         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &on_server, sizeof(int))) {
68                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
69                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
70         }
71
72         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
73                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
74                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
75         }
76
77         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
78         EM_DEBUG_LOG(" >>>>> error VALUE [%d]", err);
79
80         if(handle) {
81                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
82                 EM_DEBUG_LOG(" >>>>> Handle [%d]", *handle);
83         }       
84         
85
86         emipc_destroy_email_api(hAPI);
87         hAPI = NULL;
88         EM_DEBUG_FUNC_END("err [%d]", err);
89         return err;
90 }
91
92
93 /* API - Delete a mailbox */
94
95 EXPORT_API int email_delete_mailbox(emf_mailbox_t* mailbox, int on_server,  unsigned* handle)
96 {
97         EM_DEBUG_FUNC_BEGIN("mailbox[%p]", mailbox);
98         
99         char* local_mailbox_stream = NULL;
100         int err = EMF_ERROR_NONE;
101         int size = 0;
102         
103         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);
104         EM_IF_NULL_RETURN_VALUE(mailbox->account_id, EMF_ERROR_INVALID_PARAM)   ;
105
106         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAILBOX);
107
108         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
109
110         local_mailbox_stream = em_convert_mailbox_to_byte_stream(mailbox, &size);
111         
112         EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
113
114         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
115                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
116                 EM_SAFE_FREE(local_mailbox_stream);
117                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
118         }
119         
120         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &on_server, sizeof(int))) {
121                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
122                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
123         }
124
125         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
126                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
127                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
128         }
129
130         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
131         EM_DEBUG_LOG("error VALUE [%d]", err);
132
133         if(handle) {
134                 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
135                 EM_DEBUG_LOG("Handle [%d]", handle);
136         }       
137
138         emipc_destroy_email_api(hAPI);
139         hAPI = NULL;
140         EM_DEBUG_FUNC_END("err [%d]", err);
141         return err;
142 }
143
144 /* API - Update a mailbox */
145
146
147 EXPORT_API int email_update_mailbox(emf_mailbox_t* old_mailbox, emf_mailbox_t* new_mailbox)
148 {
149         EM_DEBUG_FUNC_BEGIN();
150         
151         char* local_mailbox_stream = NULL;
152         int err = EMF_ERROR_NONE;       
153         int size = 0;
154         int on_server = 0;
155         EM_DEBUG_LOG(" old_mailbox[%p], new_mailbox[%p]", old_mailbox, new_mailbox);
156
157         EM_IF_NULL_RETURN_VALUE(old_mailbox, EMF_ERROR_INVALID_PARAM);
158         EM_IF_NULL_RETURN_VALUE(old_mailbox->account_id, EMF_ERROR_INVALID_PARAM);
159         EM_IF_NULL_RETURN_VALUE(new_mailbox, EMF_ERROR_INVALID_PARAM);
160         EM_IF_ACCOUNT_ID_NULL(new_mailbox->account_id, EMF_ERROR_INVALID_PARAM);
161         
162         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_UPDATE_MAILBOX);      
163
164         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
165
166
167         local_mailbox_stream = em_convert_mailbox_to_byte_stream(old_mailbox, &size);
168
169         EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
170                 
171         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
172                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
173                 EM_SAFE_FREE(local_mailbox_stream); 
174                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
175         }
176         
177         local_mailbox_stream = em_convert_mailbox_to_byte_stream(new_mailbox, &size);
178
179         EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMF_ERROR_NULL_VALUE);
180
181         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
182                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
183                 EM_SAFE_FREE(local_mailbox_stream); 
184                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
185         }
186
187         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &on_server, sizeof(int))) {
188                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
189                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
190         }
191
192         if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
193                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
194                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_IPC_SOCKET_FAILURE);
195         } 
196
197         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
198         
199         EM_DEBUG_LOG("error VALUE [%d]", err);
200         emipc_destroy_email_api(hAPI);
201         hAPI = NULL;
202         
203         EM_DEBUG_FUNC_END("err [%d]", err);
204         return err;
205 }
206
207
208 EXPORT_API int email_get_sync_mailbox_list(int account_id, emf_mailbox_t** mailbox_list, int* count)
209 {
210         EM_DEBUG_FUNC_BEGIN();
211         int counter = 0;
212         emstorage_mailbox_tbl_t* mailbox = NULL; 
213         int err = EMF_ERROR_NONE ;
214         
215         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMF_ERROR_INVALID_PARAM);
216         EM_IF_ACCOUNT_ID_NULL(account_id, EMF_ERROR_INVALID_PARAM);
217         EM_IF_NULL_RETURN_VALUE(count, EMF_ERROR_INVALID_PARAM);
218
219         if (!emstorage_get_mailbox(account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &counter, &mailbox, true, &err)) {
220                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
221
222
223                 goto FINISH_OFF;
224         } else
225                 err = EMF_ERROR_NONE;
226         
227         if (counter > 0)  {
228                 if (!(*mailbox_list = em_malloc(sizeof(emf_mailbox_t) * counter)))  {
229                         EM_DEBUG_EXCEPTION("malloc failed...");
230                         err= EMF_ERROR_OUT_OF_MEMORY;
231                         goto FINISH_OFF;
232                 }
233         } else
234                 *mailbox_list = NULL;
235         
236         *count = counter;
237         
238         FINISH_OFF:
239         if (mailbox != NULL)
240                 emstorage_free_mailbox(&mailbox, counter, NULL);
241         EM_DEBUG_FUNC_END("err [%d]", err);
242         return err;
243 }
244
245
246 EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, emf_mailbox_t** mailbox_list, int* count)
247 {
248         EM_DEBUG_FUNC_BEGIN();  
249
250         int mailbox_count = 0;
251         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
252         int err =EMF_ERROR_NONE;
253         int i;
254         
255         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMF_ERROR_INVALID_PARAM);
256         EM_IF_ACCOUNT_ID_NULL(account_id, EMF_ERROR_INVALID_PARAM);
257         EM_IF_NULL_RETURN_VALUE(count, EMF_ERROR_INVALID_PARAM);
258
259         if (!emstorage_get_mailbox(account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {    
260                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
261
262                 goto FINISH_OFF;
263         } else
264                 err = EMF_ERROR_NONE;
265         
266         if (mailbox_count > 0)  {
267                 if (!(*mailbox_list = em_malloc(sizeof(emf_mailbox_t) * mailbox_count)))  {
268                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
269                         err= EMF_ERROR_OUT_OF_MEMORY;
270                         goto FINISH_OFF;
271                 }
272
273                 for (i = 0; i < mailbox_count; i++)  {
274                         (*mailbox_list)[i].mailbox_id = mailbox_tbl_list[i].mailbox_id;
275                         (*mailbox_list)[i].account_id = account_id;
276                         (*mailbox_list)[i].name = mailbox_tbl_list[i].mailbox_name; 
277                         mailbox_tbl_list[i].mailbox_name = NULL;
278                         (*mailbox_list)[i].alias = mailbox_tbl_list[i].alias; 
279                         mailbox_tbl_list[i].alias = NULL;
280                         (*mailbox_list)[i].local = mailbox_tbl_list[i].local_yn;
281                         (*mailbox_list)[i].mailbox_type = mailbox_tbl_list[i].mailbox_type;
282                         (*mailbox_list)[i].synchronous = mailbox_tbl_list[i].sync_with_server_yn;
283                         (*mailbox_list)[i].unread_count = mailbox_tbl_list[i].unread_count;
284                         (*mailbox_list)[i].total_mail_count_on_local = mailbox_tbl_list[i].total_mail_count_on_local;
285                         (*mailbox_list)[i].total_mail_count_on_server = mailbox_tbl_list[i].total_mail_count_on_server;
286                         (*mailbox_list)[i].has_archived_mails = mailbox_tbl_list[i].has_archived_mails;
287                         (*mailbox_list)[i].mail_slot_size = mailbox_tbl_list[i].mail_slot_size;
288                 }
289         } else
290                 *mailbox_list = NULL;
291         
292         *count = mailbox_count;
293
294 FINISH_OFF:
295         if (mailbox_tbl_list != NULL)
296                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
297
298         EM_DEBUG_FUNC_END("err [%d]", err);
299         return err;
300 }
301
302
303
304 EXPORT_API int email_get_mailbox_list_ex(int account_id, int mailbox_sync_type, int with_count, emf_mailbox_t** mailbox_list, int* count)
305 {
306         EM_DEBUG_FUNC_BEGIN();  
307
308         int mailbox_count = 0;
309         emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
310         int err =EMF_ERROR_NONE;
311         int i;
312         
313         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMF_ERROR_INVALID_PARAM);
314         EM_IF_ACCOUNT_ID_NULL(account_id, EMF_ERROR_INVALID_PARAM);
315         EM_IF_NULL_RETURN_VALUE(count, EMF_ERROR_INVALID_PARAM);
316
317         if (!emstorage_get_mailbox_ex(account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {     
318                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_ex failed [%d]", err);
319
320                 goto FINISH_OFF;
321         } else
322                 err = EMF_ERROR_NONE;
323         
324         if (mailbox_count > 0)  {
325                 if (!(*mailbox_list = em_malloc(sizeof(emf_mailbox_t) * mailbox_count)))  {
326                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
327                         err= EMF_ERROR_OUT_OF_MEMORY;
328                         goto FINISH_OFF;
329                 }
330
331                 for (i = 0; i < mailbox_count; i++)  {
332                         (*mailbox_list)[i].mailbox_id = mailbox_tbl_list[i].mailbox_id;
333                         (*mailbox_list)[i].account_id = account_id;
334                         (*mailbox_list)[i].name = mailbox_tbl_list[i].mailbox_name; 
335                         mailbox_tbl_list[i].mailbox_name = NULL;
336                         (*mailbox_list)[i].alias = mailbox_tbl_list[i].alias; 
337                         mailbox_tbl_list[i].alias = NULL;
338                         (*mailbox_list)[i].local = mailbox_tbl_list[i].local_yn;
339                         (*mailbox_list)[i].mailbox_type = mailbox_tbl_list[i].mailbox_type;
340                         (*mailbox_list)[i].synchronous = mailbox_tbl_list[i].sync_with_server_yn;
341                         (*mailbox_list)[i].unread_count = mailbox_tbl_list[i].unread_count;
342                         (*mailbox_list)[i].total_mail_count_on_local = mailbox_tbl_list[i].total_mail_count_on_local;
343                         (*mailbox_list)[i].total_mail_count_on_server = mailbox_tbl_list[i].total_mail_count_on_server;
344                         (*mailbox_list)[i].has_archived_mails = mailbox_tbl_list[i].has_archived_mails;
345                         (*mailbox_list)[i].mail_slot_size = mailbox_tbl_list[i].mail_slot_size;
346                 }
347         } else
348                 *mailbox_list = NULL;
349
350         if ( count )    
351                 *count = mailbox_count;
352
353 FINISH_OFF:
354         if (mailbox_tbl_list != NULL)
355                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
356
357         EM_DEBUG_FUNC_END("err [%d]", err);
358         return err;
359 }
360
361
362 EXPORT_API int email_get_mailbox_by_name(int account_id, const char *pMailboxName, emf_mailbox_t **pMailbox)
363 {
364         EM_DEBUG_FUNC_BEGIN();
365         
366         int err = EMF_ERROR_NONE;
367         emf_mailbox_t* curr_mailbox = NULL;
368         emstorage_mailbox_tbl_t* local_mailbox = NULL;
369
370         EM_IF_NULL_RETURN_VALUE(pMailbox, EMF_ERROR_INVALID_PARAM);
371         if(!pMailboxName)
372                 return EMF_ERROR_INVALID_PARAM; 
373         
374         if (!emstorage_get_mailbox_by_name(account_id, -1, (char*)pMailboxName, &local_mailbox, true, &err)) {
375                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
376                 return err;
377         } else {
378                 curr_mailbox = em_malloc(sizeof(emf_mailbox_t));
379                 memset(curr_mailbox, 0x00, sizeof(emf_mailbox_t));
380
381                 curr_mailbox->account_id = local_mailbox->account_id;
382                 if(local_mailbox->mailbox_name)
383                         curr_mailbox->name = strdup(local_mailbox->mailbox_name);
384                 curr_mailbox->local = local_mailbox->local_yn;
385                 if(local_mailbox->alias)
386                         curr_mailbox->alias= strdup(local_mailbox->alias);
387                 curr_mailbox->synchronous = local_mailbox->sync_with_server_yn;
388                 curr_mailbox->mailbox_type = local_mailbox->mailbox_type;
389                 curr_mailbox->unread_count = local_mailbox->unread_count;
390                 curr_mailbox->total_mail_count_on_local= local_mailbox->total_mail_count_on_local;
391                 curr_mailbox->total_mail_count_on_server= local_mailbox->total_mail_count_on_server;
392                 curr_mailbox->has_archived_mails = local_mailbox->has_archived_mails;
393                 curr_mailbox->mail_slot_size = local_mailbox->mail_slot_size;
394         }
395
396         *pMailbox = curr_mailbox;
397
398         emstorage_free_mailbox(&local_mailbox, 1, &err);
399         EM_DEBUG_FUNC_END("err [%d]", err);
400         return err;
401 }
402
403
404 EXPORT_API int email_get_child_mailbox_list(int account_id, const char *parent_mailbox,  emf_mailbox_t** mailbox_list, int* count)
405 {
406         EM_DEBUG_FUNC_BEGIN("account_id[%d], parent_mailbox[%p], mailbox_list[%p], count[%p]", account_id, parent_mailbox, mailbox_list, count);
407         
408         int err = EMF_ERROR_NONE;
409         int counter = 0;
410         emstorage_mailbox_tbl_t* mailbox_tbl = NULL; 
411         int i =0;
412         
413         EM_IF_NULL_RETURN_VALUE(account_id, EMF_ERROR_INVALID_PARAM);
414         EM_IF_NULL_RETURN_VALUE(mailbox_list, EMF_ERROR_INVALID_PARAM);
415         EM_IF_NULL_RETURN_VALUE(count, EMF_ERROR_INVALID_PARAM);
416                 
417         *mailbox_list = NULL;
418         *count = 0;
419         
420         if (!emstorage_get_child_mailbox_list(account_id,(char*)parent_mailbox, &counter, &mailbox_tbl, true, &err))   {
421                 EM_DEBUG_EXCEPTION("emstorage_get_child_mailbox_list failed[%d]", err);
422                 
423
424                 goto FINISH_OFF;
425         } else
426                 err = EMF_ERROR_NONE;
427         
428         if (counter > 0)  {
429                 if (!(*mailbox_list = em_malloc(sizeof(emf_mailbox_t) * counter)))  {
430                         EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
431                         
432                         err= EMF_ERROR_OUT_OF_MEMORY;
433                         goto FINISH_OFF;
434                 }
435
436                 memset(*mailbox_list, 0x00, (sizeof(emf_mailbox_t) * counter));
437                 
438                 for (i = 0; i < counter; i++)  {
439                         (*mailbox_list)[i].mailbox_id = mailbox_tbl[i].mailbox_id;
440                         (*mailbox_list)[i].account_id = account_id;
441                         (*mailbox_list)[i].name = mailbox_tbl[i].mailbox_name; mailbox_tbl[i].mailbox_name = NULL;
442                         (*mailbox_list)[i].alias = mailbox_tbl[i].alias; mailbox_tbl[i].alias = NULL;
443                         (*mailbox_list)[i].local = mailbox_tbl[i].local_yn;
444                         (*mailbox_list)[i].mailbox_type = mailbox_tbl[i].mailbox_type;
445                         (*mailbox_list)[i].synchronous = mailbox_tbl[i].sync_with_server_yn;
446                         (*mailbox_list)[i].unread_count = mailbox_tbl[i].unread_count;
447                         (*mailbox_list)[i].total_mail_count_on_local = mailbox_tbl[i].total_mail_count_on_local;
448                         (*mailbox_list)[i].total_mail_count_on_server = mailbox_tbl[i].total_mail_count_on_server;
449                         (*mailbox_list)[i].has_archived_mails = mailbox_tbl[i].has_archived_mails;
450                         (*mailbox_list)[i].mail_slot_size = mailbox_tbl[i].mail_slot_size;
451                 }
452         } else
453                 *mailbox_list = NULL;
454         
455         *count = counter;
456
457 FINISH_OFF:
458         if (mailbox_tbl != NULL)
459                 emstorage_free_mailbox(&mailbox_tbl, counter, NULL);
460         EM_DEBUG_FUNC_END("err [%d]", err);
461         return err;
462 }
463
464 /* sowmya.kr, 05-Apr-2010, new MAPI*/
465
466 EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, emf_mailbox_type_e mailbox_type,  emf_mailbox_t** mailbox)
467 {
468         EM_DEBUG_FUNC_BEGIN();
469         
470         int err = EMF_ERROR_NONE;
471         emf_mailbox_t* curr_mailbox = NULL;
472         emstorage_mailbox_tbl_t* local_mailbox = NULL;
473
474         EM_IF_NULL_RETURN_VALUE(mailbox, EMF_ERROR_INVALID_PARAM);      
475         EM_IF_NULL_RETURN_VALUE(account_id, EMF_ERROR_INVALID_PARAM)    ;
476
477
478         if(mailbox_type < EMF_MAILBOX_TYPE_INBOX || mailbox_type > EMF_MAILBOX_TYPE_ALL_EMAILS)
479                 return EMF_ERROR_INVALID_PARAM;
480         if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &local_mailbox, true, &err))  {
481                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
482
483                 goto FINISH_OFF;
484         } else {
485                 err = EMF_ERROR_NONE;
486                 curr_mailbox = em_malloc(sizeof(emf_mailbox_t));
487                 memset(curr_mailbox, 0x00, sizeof(emf_mailbox_t));
488
489                 curr_mailbox->account_id = local_mailbox->account_id;
490                 if(local_mailbox->mailbox_name)
491                         curr_mailbox->name = strdup(local_mailbox->mailbox_name);
492                 curr_mailbox->local = local_mailbox->local_yn;
493                 if(local_mailbox->alias)
494                         curr_mailbox->alias= strdup(local_mailbox->alias);
495                 curr_mailbox->synchronous = 1;
496                 curr_mailbox->mailbox_type = local_mailbox->mailbox_type;
497                 curr_mailbox->unread_count = local_mailbox->unread_count;
498                 curr_mailbox->total_mail_count_on_local = local_mailbox->total_mail_count_on_local;
499                 curr_mailbox->total_mail_count_on_server = local_mailbox->total_mail_count_on_server;
500                 curr_mailbox->has_archived_mails = local_mailbox->has_archived_mails;
501                 curr_mailbox->mail_slot_size = local_mailbox->mail_slot_size;
502         }
503
504         *mailbox = curr_mailbox;        
505 FINISH_OFF:
506
507         if(local_mailbox)
508                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
509         EM_DEBUG_FUNC_END("err [%d]", err);
510         return err;
511 }
512
513 EXPORT_API int email_set_mail_slot_size(int account_id, char* mailbox_name, int new_slot_size/*, unsigned* handle*/)
514 {
515         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], new_slot_size[%d]", account_id, mailbox_name, new_slot_size);
516
517         int err = EMF_ERROR_NONE, *handle = NULL;
518
519         if(new_slot_size < 0) {
520                 EM_DEBUG_EXCEPTION("new_slot_size should be greater than 0 or should be equal to 0");
521                 return EMF_ERROR_INVALID_PARAM;
522         }
523         
524         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAIL_SLOT_SIZE);  
525
526         EM_IF_NULL_RETURN_VALUE(hAPI, EMF_ERROR_NULL_VALUE);
527
528         if (hAPI) {
529                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
530                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
531                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
532                 }
533
534                 if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &new_slot_size, sizeof(int))) {
535                         EM_DEBUG_EXCEPTION(" emipc_add_parameter for new_slot_size failed");
536                         EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
537                 }
538
539                 if(mailbox_name) {
540                         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, mailbox_name, strlen(mailbox_name) )) {
541                                 EM_DEBUG_EXCEPTION(" emipc_add_parameter for mailbox_name failed");
542                                 EM_SAFE_FREE(mailbox_name);
543                                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMF_ERROR_NULL_VALUE);
544                         }
545                 }
546                 
547                 if(emipc_execute_proxy_api(hAPI) != EMF_ERROR_NONE) {
548                         EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
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                 EM_DEBUG_LOG("email_set_mail_slot_size error VALUE [%d]", err);
554                 if(handle) {
555                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
556                         EM_DEBUG_LOG("email_set_mail_slot_size handle VALUE [%d]", handle);
557                 }
558                 emipc_destroy_email_api(hAPI);
559                 hAPI = NULL;
560                 
561         }
562         EM_DEBUG_FUNC_END("err [%d]", err);
563         return err;
564 }
565
566 EXPORT_API int email_free_mailbox(emf_mailbox_t** mailbox_list, int count)
567 {
568         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
569         int err = EMF_ERROR_NONE;
570
571         if (count <= 0 || !mailbox_list || !*mailbox_list) {
572                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
573                 err = EMF_ERROR_INVALID_PARAM;
574                 goto FINISH_OFF;
575         }
576
577         emf_mailbox_t *p = *mailbox_list;
578         int i;
579
580         for (i = 0; i < count; i++)  {
581                 EM_SAFE_FREE(p[i].name);
582                 EM_SAFE_FREE(p[i].alias);
583                 EM_SAFE_FREE(p[i].account_name);
584         }
585
586         EM_SAFE_FREE(p);
587         *mailbox_list = NULL;
588
589 FINISH_OFF:
590         EM_DEBUG_FUNC_END("err [%d]", err);
591         return err;
592
593 }