summaryrefslogtreecommitdiff
path: root/email-api/email-api-mailbox.c
blob: b89d4c482259f155d01becf81c609009c179c626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
*  email-service
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/



/**
 *
 * This file contains the data structures and interfaces needed for application,
 * to interact with email-service.
 * @file		email_api_mailbox.c
 * @brief 		This file contains the data structures and interfaces of mailbox related Functionality provided by 
 *			email-service . 
 */
 
#include "email-api.h"
#include "string.h"
#include "email-convert.h"
#include "email-storage.h"
#include "email-core-utils.h"
#include "email-utilities.h"
#include "email-ipc.h"
#include "db-util.h"

/* API - Create a mailbox */

EXPORT_API int email_add_mailbox(email_mailbox_t* new_mailbox, int on_server, unsigned* handle)
{
	EM_DEBUG_FUNC_BEGIN();

	int size = 0;
	int err = EMAIL_ERROR_NONE;
	char* local_mailbox_stream = NULL;
	email_account_server_t account_server_type;
	HIPC_API hAPI = NULL;
	ASNotiData as_noti_data;

	memset(&as_noti_data, 0x00, sizeof(ASNotiData));

	EM_IF_NULL_RETURN_VALUE(new_mailbox, EMAIL_ERROR_INVALID_PARAM);

	/*  check account bind type and branch off  */
	if ( em_get_account_server_type_by_account_id(new_mailbox->account_id, &account_server_type, false, &err) == false ) {
		EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
		err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
		goto FINISH_OFF;
	}

	if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && on_server) {
		int as_handle;

		if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
			EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		/*  noti to active sync */
		as_noti_data.add_mailbox.handle        = as_handle;
		as_noti_data.add_mailbox.account_id    = new_mailbox->account_id;
		as_noti_data.add_mailbox.mailbox_alias = new_mailbox->alias;
		as_noti_data.add_mailbox.mailbox_path  = new_mailbox->mailbox_name;

		if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_ADD_MAILBOX, &as_noti_data) == false) {
			EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		if(handle)
			*handle = as_handle;
	}
	else {
		hAPI = emipc_create_email_api(_EMAIL_API_ADD_MAILBOX);

		EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);

		local_mailbox_stream = em_convert_mailbox_to_byte_stream(new_mailbox, &size);

		EM_PROXY_IF_NULL_RETURN_VALUE(local_mailbox_stream, hAPI, EMAIL_ERROR_NULL_VALUE);

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, local_mailbox_stream, size)) {
			EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
			EM_SAFE_FREE(local_mailbox_stream);
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &on_server, sizeof(int))) {
			EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
		}

		emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
		EM_DEBUG_LOG(" >>>>> error VALUE [%d]", err);
	
		if(handle) {
			emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
			EM_DEBUG_LOG(" >>>>> Handle [%d]", *handle);
		}

		emipc_get_parameter(hAPI, ePARAMETER_OUT, 2, sizeof(int), &(new_mailbox->mailbox_id));
		EM_DEBUG_LOG(" >>>>> new_mailbox->mailbox_id [%d]", new_mailbox->mailbox_id);

		emipc_destroy_email_api(hAPI);
		hAPI = NULL;
	}

FINISH_OFF:
	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

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)
{
	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);

	int err = EMAIL_ERROR_NONE;
	email_account_server_t account_server_type;
	HIPC_API hAPI = NULL;
	ASNotiData as_noti_data;
	emstorage_mailbox_tbl_t *mailbox_tbl = NULL;

	EM_IF_NULL_RETURN_VALUE(input_mailbox_id,    EMAIL_ERROR_INVALID_PARAM);
	EM_IF_NULL_RETURN_VALUE(input_mailbox_name,  EMAIL_ERROR_INVALID_PARAM);
	EM_IF_NULL_RETURN_VALUE(input_mailbox_alias, EMAIL_ERROR_INVALID_PARAM);

	if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
		goto FINISH_OFF;
	}

	/*  check account bind type and branch off  */
	if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
		EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
		err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
		goto FINISH_OFF;
	}

	if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
		int as_handle;

		if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
			EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		/*  noti to active sync */
		as_noti_data.rename_mailbox.handle        = as_handle;
		as_noti_data.rename_mailbox.account_id    = mailbox_tbl->account_id;
		as_noti_data.rename_mailbox.mailbox_id    = input_mailbox_id;
		as_noti_data.rename_mailbox.mailbox_name  = input_mailbox_name;
		as_noti_data.rename_mailbox.mailbox_alias = input_mailbox_alias;

		if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RENAME_MAILBOX, &as_noti_data) == false) {
			EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		if(output_handle)
			*output_handle = as_handle;
	}
	else {
		hAPI = emipc_create_email_api(_EMAIL_API_RENAME_MAILBOX);

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_id failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_name, strlen(input_mailbox_name)+1 )) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_path failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, input_mailbox_alias, strlen(input_mailbox_alias)+1 )) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for input_mailbox_alias failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
			EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
		}

		emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);

		EM_DEBUG_LOG("error VALUE [%d]", err);
		emipc_destroy_email_api(hAPI);
	}
FINISH_OFF:
	if (mailbox_tbl)
		emstorage_free_mailbox(&mailbox_tbl, 1, NULL);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}


/* API - Delete a mailbox */

EXPORT_API int email_delete_mailbox(int input_mailbox_id, int input_on_server, unsigned* output_handle)
{
	EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_on_server, output_handle);
	
	int err = EMAIL_ERROR_NONE;
	email_account_server_t account_server_type;
	emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
	HIPC_API hAPI = NULL;
	ASNotiData as_noti_data;
	
	EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);

	if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed[%d]", err);
		goto FINISH_OFF;
	}

	/*  check account bind type and branch off  */
	if ( em_get_account_server_type_by_account_id(mailbox_tbl->account_id, &account_server_type, false, &err) == false ) {
		EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
		err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
		goto FINISH_OFF;
	}

	if ( account_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC && input_on_server) {
		int as_handle;

		if ( em_get_handle_for_activesync(&as_handle, &err) == false ) {
			EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed[%d].", err);
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		/*  noti to active sync */
		as_noti_data.delete_mailbox.handle        = as_handle;
		as_noti_data.delete_mailbox.account_id    = mailbox_tbl->account_id;
		as_noti_data.delete_mailbox.mailbox_id    = input_mailbox_id;

		if ( em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_DELETE_MAILBOX, &as_noti_data) == false) {
			EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed.");
			err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
			goto FINISH_OFF;
		}

		if(output_handle)
			*output_handle = as_handle;
	}
	else {
		hAPI = emipc_create_email_api(_EMAIL_API_DELETE_MAILBOX);

		EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
			EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_on_server, sizeof(int))) {
			EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
		}

		emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
		EM_DEBUG_LOG("error VALUE [%d]", err);

		if(input_on_server) {
			emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), output_handle);
			EM_DEBUG_LOG("output_handle [%d]", output_handle);
		}

		emipc_destroy_email_api(hAPI);
	}

FINISH_OFF:
	if (mailbox_tbl) {
		emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
	}

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
{
	EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
	int err = EMAIL_ERROR_NONE;

	EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);

	HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAILBOX_TYPE);

	EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);

	if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_id, sizeof(int))) {
		EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
	}

	if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &input_mailbox_type, sizeof(int))) {
		EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
	}

	if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
	}

	emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);

	EM_DEBUG_LOG("error VALUE [%d]", err);
	emipc_destroy_email_api(hAPI);
	hAPI = NULL;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;



}

EXPORT_API int email_get_sync_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count)
{
	EM_DEBUG_FUNC_BEGIN();
	int mailbox_count = 0;
	int err = EMAIL_ERROR_NONE ;
	int i = 0;
	emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL;

	EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);

	if (!emstorage_get_mailbox_list(account_id, 0, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);


		goto FINISH_OFF;
	} else
		err = EMAIL_ERROR_NONE;
	
	if (mailbox_count > 0)  {
		if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
			EM_DEBUG_EXCEPTION("malloc failed...");
			err= EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}
		for (i = 0; i < mailbox_count; i++)
			em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
	} else
		*mailbox_list = NULL;
	
	*count = mailbox_count;
	
	FINISH_OFF:
	if (mailbox_tbl_list != NULL)
		emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}


EXPORT_API int email_get_mailbox_list(int account_id, int mailbox_sync_type, email_mailbox_t** mailbox_list, int* count)
{
	EM_DEBUG_FUNC_BEGIN();	

	int mailbox_count = 0;
	emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
	int err =EMAIL_ERROR_NONE;
	int i;
	
	EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);

	if (!emstorage_get_mailbox_list(account_id, mailbox_sync_type, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err))  {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);

		goto FINISH_OFF;
	} else
		err = EMAIL_ERROR_NONE;
	
	if (mailbox_count > 0)  {
		if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
			EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
			err= EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}

		for (i = 0; i < mailbox_count; i++)
			em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
	} else
		*mailbox_list = NULL;
	
	*count = mailbox_count;

FINISH_OFF:
	if (mailbox_tbl_list != NULL)
		emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}



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)
{
	EM_DEBUG_FUNC_BEGIN();	

	int mailbox_count = 0;
	emstorage_mailbox_tbl_t* mailbox_tbl_list = NULL; 
	int err =EMAIL_ERROR_NONE;
	int i;
	
	EM_IF_NULL_RETURN_VALUE(mailbox_list, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_ACCOUNT_ID_NULL(account_id, EMAIL_ERROR_INVALID_PARAM);
	EM_IF_NULL_RETURN_VALUE(count, EMAIL_ERROR_INVALID_PARAM);

	if (!emstorage_get_mailbox_list_ex(account_id, mailbox_sync_type, with_count, &mailbox_count, &mailbox_tbl_list, true, &err))  {	
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list_ex failed [%d]", err);

		goto FINISH_OFF;
	} else
		err = EMAIL_ERROR_NONE;
	
	if (mailbox_count > 0)  {
		if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * mailbox_count)))  {
			EM_DEBUG_EXCEPTION("malloc failed for mailbox_list");
			err= EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}

		for (i = 0; i < mailbox_count; i++)
			em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, (*mailbox_list) + i);
	}
	else
		*mailbox_list = NULL;

	if ( count )	
		*count = mailbox_count;

FINISH_OFF:
	if (mailbox_tbl_list != NULL)
		emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}


/* sowmya.kr, 05-Apr-2010, new MAPI*/

EXPORT_API int email_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type,  email_mailbox_t** mailbox)
{
	EM_DEBUG_FUNC_BEGIN();
	
	int err = EMAIL_ERROR_NONE;
	email_mailbox_t* curr_mailbox = NULL;
	emstorage_mailbox_tbl_t* local_mailbox = NULL;

	EM_IF_NULL_RETURN_VALUE(mailbox, EMAIL_ERROR_INVALID_PARAM);	
	EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM)	;


	if(mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS)
		return EMAIL_ERROR_INVALID_PARAM;
	if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &local_mailbox, true, &err))  {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);

		goto FINISH_OFF;
	} else {
		err = EMAIL_ERROR_NONE;
		curr_mailbox = em_malloc(sizeof(email_mailbox_t));
		memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));
		em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
	}

	*mailbox = curr_mailbox;	
FINISH_OFF:

	if(local_mailbox)
		emstorage_free_mailbox(&local_mailbox, 1, NULL);
	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_get_mailbox_by_mailbox_id(int input_mailbox_id, email_mailbox_t** output_mailbox)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;
	email_mailbox_t* curr_mailbox = NULL;
	emstorage_mailbox_tbl_t* local_mailbox = NULL;

	EM_IF_NULL_RETURN_VALUE(output_mailbox, EMAIL_ERROR_INVALID_PARAM);

	if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
		return err;
	} else {
		curr_mailbox = em_malloc(sizeof(email_mailbox_t));

		memset(curr_mailbox, 0x00, sizeof(email_mailbox_t));

		em_convert_mailbox_tbl_to_mailbox(local_mailbox, curr_mailbox);
	}

	*output_mailbox = curr_mailbox;

	emstorage_free_mailbox(&local_mailbox, 1, &err);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size)
{
	EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], new_slot_size[%d]", account_id, mailbox_id, new_slot_size);

	int err = EMAIL_ERROR_NONE, *handle = NULL;

	if(new_slot_size < 0) {
		EM_DEBUG_EXCEPTION("new_slot_size should be greater than 0 or should be equal to 0");
		return EMAIL_ERROR_INVALID_PARAM;
	}
	
	HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SET_MAIL_SLOT_SIZE);	

	EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);

	if (hAPI) {
		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &account_id, sizeof(int))) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &mailbox_id, sizeof(int))) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for account_id failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(!emipc_add_parameter(hAPI, ePARAMETER_IN, &new_slot_size, sizeof(int))) {
			EM_DEBUG_EXCEPTION(" emipc_add_parameter for new_slot_size failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
		}

		if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
			EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
		} 
	
	 	emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
		EM_DEBUG_LOG("email_set_mail_slot_size error VALUE [%d]", err);
		if(handle) {
			emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
			EM_DEBUG_LOG("email_set_mail_slot_size handle VALUE [%d]", handle);
		}
		emipc_destroy_email_api(hAPI);
		hAPI = NULL;
		
	}
	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_stamp_sync_time_of_mailbox(int input_mailbox_id)
{
	EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d]", input_mailbox_id);

	int err = EMAIL_ERROR_NONE;

	EM_IF_NULL_RETURN_VALUE(input_mailbox_id, EMAIL_ERROR_INVALID_PARAM);

	err = emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_id, 1);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_free_mailbox(email_mailbox_t** mailbox_list, int count)
{
	EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
	int err = EMAIL_ERROR_NONE;

	if (count <= 0 || !mailbox_list || !*mailbox_list) {
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	email_mailbox_t *p = *mailbox_list;
	int i;

	for (i = 0; i < count; i++)  {
		EM_SAFE_FREE(p[i].mailbox_name);
		EM_SAFE_FREE(p[i].alias);
	}

	EM_SAFE_FREE(p);
	*mailbox_list = NULL;

FINISH_OFF:
	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;

}